[clang] 5421234 - [clang][Interp] Implement bitwise not operations
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 14 04:01:09 PDT 2022
Author: Timm Bäder
Date: 2022-10-14T12:57:57+02:00
New Revision: 542123465f9e523ccd82bd91ee17f407ea4b0cd1
URL: https://github.com/llvm/llvm-project/commit/542123465f9e523ccd82bd91ee17f407ea4b0cd1
DIFF: https://github.com/llvm/llvm-project/commit/542123465f9e523ccd82bd91ee17f407ea4b0cd1.diff
LOG: [clang][Interp] Implement bitwise not operations
Differential Revision: https://reviews.llvm.org/D134804
Added:
Modified:
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
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 904a99b4f872..06963562638e 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1022,6 +1022,11 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
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->emitComp(*T, E);
+ return false;
case UO_Real: // __real x
case UO_Imag: // __imag x
case UO_Extension:
diff --git a/clang/lib/AST/Interp/Integral.h b/clang/lib/AST/Interp/Integral.h
index b5d20fb8c49a..d02b68ac8379 100644
--- a/clang/lib/AST/Interp/Integral.h
+++ b/clang/lib/AST/Interp/Integral.h
@@ -212,6 +212,11 @@ template <unsigned Bits, bool Signed> class Integral final {
return false;
}
+ static bool comp(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>) {
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 5d5164a95f00..f94fcc955d40 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -183,6 +183,20 @@ bool Neg(InterpState &S, CodePtr OpPC) {
return true;
}
+/// 1) Pops the value from the stack.
+/// 2) Pushes the bitwise complemented value on the stack (~V).
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool Comp(InterpState &S, CodePtr OpPC) {
+ const T &Val = S.Stk.pop<T>();
+ T Result;
+ if (!T::comp(Val, &Result)) {
+ S.Stk.push<T>(Result);
+ return true;
+ }
+
+ return false;
+}
+
//===----------------------------------------------------------------------===//
// EQ, NE, GT, GE, LT, LE
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 7591c3dc7c30..64d7e31d013c 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -411,6 +411,12 @@ def Neg: Opcode {
let HasGroup = 1;
}
+// [Real] -> [Real]
+def Comp: Opcode {
+ let Types = [NumberTypeClass];
+ let HasGroup = 1;
+}
+
//===----------------------------------------------------------------------===//
// Cast.
//===----------------------------------------------------------------------===//
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 5de727f005c8..592d393fad80 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -3,6 +3,10 @@
// RUN: %clang_cc1 -std=c++11 -verify=ref %s
// RUN: %clang_cc1 -std=c++20 -verify=ref %s
+#define INT_MIN (~__INT_MAX__)
+#define INT_MAX __INT_MAX__
+
+
static_assert(true, "");
static_assert(false, ""); // expected-error{{failed}} ref-error{{failed}}
static_assert(nullptr == nullptr, "");
@@ -66,6 +70,18 @@ static_assert(!0, "");
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, "");
+static_assert(~INT_MIN == INT_MAX, "");
+static_assert(~INT_MAX == INT_MIN, "");
+
+enum E {};
+constexpr E e = static_cast<E>(0);
+static_assert(~e == -1, "");
+
+
constexpr int m = 10;
constexpr const int *p = &m;
static_assert(p != nullptr, "");
More information about the cfe-commits
mailing list