[PATCH] D136956: [clang][Interp] Implement BitXor opcode

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 28 08:22:24 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/D136956

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
@@ -279,6 +279,18 @@
   static_assert((12 | true) == 13, "");
 };
 
+namespace bitXor {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wxor-used-as-pow"
+  static_assert((10 ^ 1) == 11, "");
+  static_assert((10 ^ 10) == 0, "");
+
+  static_assert((1337 ^ -1) == -1338, "");
+  static_assert((0 | gimme(12)) == 12, "");
+  static_assert((12 ^ true) == 13, "");
+#pragma clang diagnostic pop
+};
+
 #if __cplusplus >= 201402L
 constexpr bool IgnoredUnary() {
   bool bo = true;
Index: clang/lib/AST/Interp/Opcodes.td
===================================================================
--- clang/lib/AST/Interp/Opcodes.td
+++ clang/lib/AST/Interp/Opcodes.td
@@ -419,6 +419,7 @@
   let Types = [NumberTypeClass];
   let HasGroup = 1;
 }
+def BitXor : IntegerOpcode;
 
 //===----------------------------------------------------------------------===//
 // Unary operators.
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -211,6 +211,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 BitXor(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::bitXor(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
@@ -227,6 +227,11 @@
     return false;
   }
 
+  static bool bitXor(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
@@ -241,6 +241,8 @@
     return Discard(this->emitBitAnd(*T, BO));
   case BO_Or:
     return Discard(this->emitBitOr(*T, BO));
+  case BO_Xor:
+    return Discard(this->emitBitXor(*T, BO));
   case BO_LAnd:
   case BO_LOr:
   default:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136956.471560.patch
Type: text/x-patch
Size: 2788 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221028/4671e6d6/attachment.bin>


More information about the cfe-commits mailing list