[PATCH] D135361: [clang][Interp] Implement bitwise Or operations
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 14 05:17:16 PDT 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGce4d5ae9dcf6: [clang][Interp] Implement bitwise Or operations (authored by tbaeder).
Changed prior to commit:
https://reviews.llvm.org/D135361?vs=465711&id=467747#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D135361/new/
https://reviews.llvm.org/D135361
Files:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/lib/AST/Interp/Integral.h
clang/lib/AST/Interp/Interp.h
clang/lib/AST/Interp/Opcodes.td
clang/test/AST/Interp/literals.cpp
Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -269,3 +269,12 @@
static_assert((1337 & -1) == 1337, "");
static_assert((0 & gimme(12)) == 0, "");
};
+
+namespace bitOr {
+ static_assert((10 | 1) == 11, "");
+ static_assert((10 | 10) == 10, "");
+
+ static_assert((1337 | -1) == -1, "");
+ static_assert((0 | gimme(12)) == 12, "");
+ static_assert((12 | true) == 13, "");
+};
Index: clang/lib/AST/Interp/Opcodes.td
===================================================================
--- clang/lib/AST/Interp/Opcodes.td
+++ clang/lib/AST/Interp/Opcodes.td
@@ -412,6 +412,7 @@
let HasGroup = 1;
}
def BitAnd : IntegerOpcode;
+def BitOr : IntegerOpcode;
def Div : Opcode {
let Types = [NumberTypeClass];
let HasGroup = 1;
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -170,6 +170,23 @@
return false;
}
+/// 1) Pops the RHS from the stack.
+/// 2) Pops the LHS from the stack.
+/// 3) Pushes 'LHS | RHS' on the stack
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool BitOr(InterpState &S, CodePtr OpPC) {
+ const T &RHS = S.Stk.pop<T>();
+ const T &LHS = S.Stk.pop<T>();
+
+ unsigned Bits = RHS.bitWidth();
+ T Result;
+ if (!T::bitOr(LHS, RHS, Bits, &Result)) {
+ S.Stk.push<T>(Result);
+ return true;
+ }
+ return false;
+}
+
/// 1) Pops the RHS from the stack.
/// 2) Pops the LHS from the stack.
/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).
Index: clang/lib/AST/Interp/Integral.h
===================================================================
--- clang/lib/AST/Interp/Integral.h
+++ clang/lib/AST/Interp/Integral.h
@@ -222,6 +222,11 @@
return false;
}
+ static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) {
+ *R = Integral(A.V | B.V);
+ return false;
+ }
+
static bool neg(Integral A, Integral *R) {
*R = -A;
return false;
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -223,6 +223,7 @@
case BO_And:
return Discard(this->emitBitAnd(*T, BO));
case BO_Or:
+ return Discard(this->emitBitOr(*T, BO));
case BO_LAnd:
case BO_LOr:
default:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135361.467747.patch
Type: text/x-patch
Size: 2562 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221014/710189f7/attachment.bin>
More information about the cfe-commits
mailing list