Skip to content

Commit

Permalink
[clang][Interp] Implement bitwise Or operations
Browse files Browse the repository at this point in the history
Analogous to the bitAnd implementation, do the same for bitwise or.

Differential Revision: https://reviews.llvm.org/D135361
  • Loading branch information
tbaederr committed Oct 14, 2022
1 parent 62a5805 commit ce4d5ae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
case BO_And:
return Discard(this->emitBitAnd(*T, BO));
case BO_Or:
return Discard(this->emitBitOr(*T, BO));
case BO_LAnd:
case BO_LOr:
default:
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/Interp/Integral.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ template <unsigned Bits, bool Signed> class Integral final {
return false;
}

static bool bitOr(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
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ bool BitAnd(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 BitOr(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::bitOr(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
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def Rem : Opcode {
let HasGroup = 1;
}
def BitAnd : IntegerOpcode;
def BitOr : IntegerOpcode;
def Div : Opcode {
let Types = [NumberTypeClass];
let HasGroup = 1;
Expand Down
9 changes: 9 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ namespace band {
static_assert((1337 & -1) == 1337, "");
static_assert((0 & gimme(12)) == 0, "");
};

namespace bitOr {
static_assert((10 | 1) == 11, "");
static_assert((10 | 10) == 10, "");

static_assert((1337 | -1) == -1, "");
static_assert((0 | gimme(12)) == 12, "");
static_assert((12 | true) == 13, "");
};

0 comments on commit ce4d5ae

Please sign in to comment.