[clang][Interp] Implement bitwise and operations
authorTimm Bäder <tbaeder@redhat.com>
Fri, 30 Sep 2022 14:59:50 +0000 (16:59 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Fri, 14 Oct 2022 12:00:07 +0000 (14:00 +0200)
Differential Revision: https://reviews.llvm.org/D135012

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 d504ff3..820f2f5 100644 (file)
@@ -220,6 +220,11 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
       if (!this->emitStore(*T, BO))
         return false;
       return DiscardResult ? this->emitPopPtr(BO) : true;
+    case BO_And:
+      return Discard(this->emitBitAnd(*T, BO));
+    case BO_Or:
+    case BO_LAnd:
+    case BO_LOr:
     default:
       return this->bail(BO);
     }
index b50a567..4e7fff6 100644 (file)
@@ -217,6 +217,11 @@ public:
     return false;
   }
 
+  static bool bitAnd(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 6ad3a6a..073c7db 100644 (file)
@@ -155,6 +155,23 @@ bool Mul(InterpState &S, CodePtr OpPC) {
 
 /// 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 BitAnd(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::bitAnd(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).
 template <PrimType Name, class T = typename PrimConv<Name>::T>
 bool Rem(InterpState &S, CodePtr OpPC) {
index f47f649..11d069c 100644 (file)
@@ -59,6 +59,11 @@ def NumberTypeClass : TypeClass {
                Uint32, Sint64, Uint64];
 }
 
+def IntegerTypeClass : TypeClass {
+  let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
+               Uint32, Sint64, Uint64];
+}
+
 def AluTypeClass : TypeClass {
   let Types = !listconcat(NumberTypeClass.Types, [Bool]);
 }
@@ -103,6 +108,11 @@ class AluOpcode : Opcode {
   let HasGroup = 1;
 }
 
+class IntegerOpcode : Opcode {
+  let Types = [IntegerTypeClass];
+  let HasGroup = 1;
+}
+
 //===----------------------------------------------------------------------===//
 // Jump opcodes
 //===----------------------------------------------------------------------===//
@@ -401,6 +411,7 @@ def Rem : Opcode {
   let Types = [NumberTypeClass];
   let HasGroup = 1;
 }
+def BitAnd : IntegerOpcode;
 def Div : Opcode {
   let Types = [NumberTypeClass];
   let HasGroup = 1;
index f11107c..1e1ef49 100644 (file)
@@ -261,3 +261,11 @@ namespace cond {
 #endif
 
 };
+
+namespace band {
+  static_assert((10 & 1) == 0, "");
+  static_assert((10 & 10) == 10, "");
+
+  static_assert((1337 & -1) == 1337, "");
+  static_assert((0 & gimme(12)) == 0, "");
+};