[PATCH] D134804: [clang][Interp] Implement bitwise Not operations
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 28 05:40:33 PDT 2022
tbaeder updated this revision to Diff 463515.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D134804/new/
https://reviews.llvm.org/D134804
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
@@ -66,6 +66,13 @@
static_assert(-true, "");
static_assert(-false, ""); //expected-error{{failed}} ref-error{{failed}}
+static_assert(~0 == -1, "");
+static_assert(~1 == -2, "");
+static_assert(~-1 == 0, "");
+static_assert(~255 == -256, "");
+
+
+
constexpr int m = 10;
constexpr const int *p = &m;
static_assert(p != nullptr, "");
Index: clang/lib/AST/Interp/Opcodes.td
===================================================================
--- clang/lib/AST/Interp/Opcodes.td
+++ clang/lib/AST/Interp/Opcodes.td
@@ -422,6 +422,12 @@
let HasGroup = 1;
}
+// [Real] -> [Real]
+def Not: Opcode {
+ let Types = [NumberTypeClass];
+ let HasGroup = 1;
+}
+
//===----------------------------------------------------------------------===//
// Cast.
//===----------------------------------------------------------------------===//
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -238,6 +238,20 @@
return true;
}
+/// 1) Pops the value from the stack.
+/// 2) Pushes the bitwise negated value on the stack (~V).
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool Not(InterpState &S, CodePtr OpPC) {
+ const T &Val = S.Stk.pop<T>();
+ T Result;
+ if (!T::Not(Val, &Result)) {
+ S.Stk.push<T>(Result);
+ return true;
+ }
+
+ return false;
+}
+
//===----------------------------------------------------------------------===//
// EQ, NE, GT, GE, LT, LE
//===----------------------------------------------------------------------===//
Index: clang/lib/AST/Interp/Integral.h
===================================================================
--- clang/lib/AST/Interp/Integral.h
+++ clang/lib/AST/Interp/Integral.h
@@ -225,6 +225,11 @@
return false;
}
+ static bool Not(Integral A, Integral *R) {
+ *R = Integral(~A.V);
+ return false;
+ }
+
private:
template <typename T> static bool CheckAddUB(T A, T B, T &R) {
if constexpr (std::is_signed_v<T>) {
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1049,6 +1049,11 @@
return DiscardResult ? this->emitPop(T, E) : true;
});
case UO_Not: // ~x
+ if (!this->Visit(SubExpr))
+ return false;
+ if (Optional<PrimType> T = classify(E->getType()))
+ return this->emitNot(*T, E);
+ return false;
case UO_Real: // __real x
case UO_Imag: // __imag x
case UO_Extension:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134804.463515.patch
Type: text/x-patch
Size: 2812 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220928/46ec52d2/attachment.bin>
More information about the cfe-commits
mailing list