[PATCH] D135361: [clang][Interp] Implement bitwise Or operations

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 6 06:18:30 PDT 2022


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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
@@ -271,6 +271,15 @@
   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, "");
+};
+
 namespace floats {
   constexpr int i = 2;
   constexpr float f = 1.0f;
Index: clang/lib/AST/Interp/Opcodes.td
===================================================================
--- clang/lib/AST/Interp/Opcodes.td
+++ clang/lib/AST/Interp/Opcodes.td
@@ -432,6 +432,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
@@ -175,6 +175,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
@@ -217,6 +217,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
@@ -273,6 +273,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.465711.patch
Type: text/x-patch
Size: 2594 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221006/87f5469a/attachment.bin>


More information about the cfe-commits mailing list