Skip to content

Commit

Permalink
[clang][Interp] Implement bitXor opcode
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D136956
  • Loading branch information
tbaederr committed Oct 30, 2022
1 parent 6d965c9 commit 74fb770
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Expand Up @@ -241,6 +241,8 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
return Discard(this->emitShl(*LT, *RT, BO));
case BO_Shr:
return Discard(this->emitShr(*LT, *RT, BO));
case BO_Xor:
return Discard(this->emitBitXor(*T, BO));
case BO_LAnd:
case BO_LOr:
default:
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/Interp/Integral.h
Expand Up @@ -227,6 +227,11 @@ template <unsigned Bits, bool Signed> class Integral final {
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;
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/AST/Interp/Interp.h
Expand Up @@ -211,6 +211,23 @@ bool BitOr(InterpState &S, CodePtr OpPC) {
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).
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/Interp/Opcodes.td
Expand Up @@ -419,6 +419,7 @@ def Div : Opcode {
let Types = [NumberTypeClass];
let HasGroup = 1;
}
def BitXor : IntegerOpcode;

//===----------------------------------------------------------------------===//
// Unary operators.
Expand Down
17 changes: 17 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Expand Up @@ -279,6 +279,23 @@ namespace bitOr {
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, "");

enum {
ONE = 1,
};

static_assert((1337 ^ -1) == -1338, "");
static_assert((0 | gimme(12)) == 12, "");
static_assert((12 ^ true) == 13, "");
static_assert((12 ^ ONE) == 13, "");
#pragma clang diagnostic pop
};

#if __cplusplus >= 201402L
constexpr bool IgnoredUnary() {
bool bo = true;
Expand Down

0 comments on commit 74fb770

Please sign in to comment.