This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG542123465f9e: [clang][Interp] Implement bitwise not operations (authored by tbaeder).
Changed prior to commit: https://reviews.llvm.org/D134804?vs=464155&id=467731#toc Repository: rG LLVM Github Monorepo 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 @@ -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(-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, ""); Index: clang/lib/AST/Interp/Opcodes.td =================================================================== --- clang/lib/AST/Interp/Opcodes.td +++ clang/lib/AST/Interp/Opcodes.td @@ -411,6 +411,12 @@ let HasGroup = 1; } +// [Real] -> [Real] +def Comp: 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 @@ -183,6 +183,20 @@ 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 //===----------------------------------------------------------------------===// Index: clang/lib/AST/Interp/Integral.h =================================================================== --- clang/lib/AST/Interp/Integral.h +++ clang/lib/AST/Interp/Integral.h @@ -212,6 +212,11 @@ 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>) { Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -1022,6 +1022,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->emitComp(*T, E); + return false; case UO_Real: // __real x case UO_Imag: // __imag x case UO_Extension:
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits