Skip to content

Commit ce4d5ae

Browse files
committed
[clang][Interp] Implement bitwise Or operations
Analogous to the bitAnd implementation, do the same for bitwise or. Differential Revision: https://reviews.llvm.org/D135361
1 parent 62a5805 commit ce4d5ae

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
223223
case BO_And:
224224
return Discard(this->emitBitAnd(*T, BO));
225225
case BO_Or:
226+
return Discard(this->emitBitOr(*T, BO));
226227
case BO_LAnd:
227228
case BO_LOr:
228229
default:

clang/lib/AST/Interp/Integral.h

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ template <unsigned Bits, bool Signed> class Integral final {
222222
return false;
223223
}
224224

225+
static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) {
226+
*R = Integral(A.V | B.V);
227+
return false;
228+
}
229+
225230
static bool neg(Integral A, Integral *R) {
226231
*R = -A;
227232
return false;

clang/lib/AST/Interp/Interp.h

+17
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ bool BitAnd(InterpState &S, CodePtr OpPC) {
170170
return false;
171171
}
172172

173+
/// 1) Pops the RHS from the stack.
174+
/// 2) Pops the LHS from the stack.
175+
/// 3) Pushes 'LHS | RHS' on the stack
176+
template <PrimType Name, class T = typename PrimConv<Name>::T>
177+
bool BitOr(InterpState &S, CodePtr OpPC) {
178+
const T &RHS = S.Stk.pop<T>();
179+
const T &LHS = S.Stk.pop<T>();
180+
181+
unsigned Bits = RHS.bitWidth();
182+
T Result;
183+
if (!T::bitOr(LHS, RHS, Bits, &Result)) {
184+
S.Stk.push<T>(Result);
185+
return true;
186+
}
187+
return false;
188+
}
189+
173190
/// 1) Pops the RHS from the stack.
174191
/// 2) Pops the LHS from the stack.
175192
/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).

clang/lib/AST/Interp/Opcodes.td

+1
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ def Rem : Opcode {
412412
let HasGroup = 1;
413413
}
414414
def BitAnd : IntegerOpcode;
415+
def BitOr : IntegerOpcode;
415416
def Div : Opcode {
416417
let Types = [NumberTypeClass];
417418
let HasGroup = 1;

clang/test/AST/Interp/literals.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,12 @@ namespace band {
269269
static_assert((1337 & -1) == 1337, "");
270270
static_assert((0 & gimme(12)) == 0, "");
271271
};
272+
273+
namespace bitOr {
274+
static_assert((10 | 1) == 11, "");
275+
static_assert((10 | 10) == 10, "");
276+
277+
static_assert((1337 | -1) == -1, "");
278+
static_assert((0 | gimme(12)) == 12, "");
279+
static_assert((12 | true) == 13, "");
280+
};

0 commit comments

Comments
 (0)