Skip to content

Commit 62a5805

Browse files
committed
[clang][Interp] Implement bitwise and operations
Differential Revision: https://reviews.llvm.org/D135012
1 parent ea9cae0 commit 62a5805

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
220220
if (!this->emitStore(*T, BO))
221221
return false;
222222
return DiscardResult ? this->emitPopPtr(BO) : true;
223+
case BO_And:
224+
return Discard(this->emitBitAnd(*T, BO));
225+
case BO_Or:
226+
case BO_LAnd:
227+
case BO_LOr:
223228
default:
224229
return this->bail(BO);
225230
}

clang/lib/AST/Interp/Integral.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ template <unsigned Bits, bool Signed> class Integral final {
217217
return false;
218218
}
219219

220+
static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R) {
221+
*R = Integral(A.V & B.V);
222+
return false;
223+
}
224+
220225
static bool neg(Integral A, Integral *R) {
221226
*R = -A;
222227
return false;

clang/lib/AST/Interp/Interp.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ bool Mul(InterpState &S, CodePtr OpPC) {
153153
return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
154154
}
155155

156+
/// 1) Pops the RHS from the stack.
157+
/// 2) Pops the LHS from the stack.
158+
/// 3) Pushes 'LHS & RHS' on the stack
159+
template <PrimType Name, class T = typename PrimConv<Name>::T>
160+
bool BitAnd(InterpState &S, CodePtr OpPC) {
161+
const T &RHS = S.Stk.pop<T>();
162+
const T &LHS = S.Stk.pop<T>();
163+
164+
unsigned Bits = RHS.bitWidth();
165+
T Result;
166+
if (!T::bitAnd(LHS, RHS, Bits, &Result)) {
167+
S.Stk.push<T>(Result);
168+
return true;
169+
}
170+
return false;
171+
}
172+
156173
/// 1) Pops the RHS from the stack.
157174
/// 2) Pops the LHS from the stack.
158175
/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).

clang/lib/AST/Interp/Opcodes.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def NumberTypeClass : TypeClass {
5959
Uint32, Sint64, Uint64];
6060
}
6161

62+
def IntegerTypeClass : TypeClass {
63+
let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
64+
Uint32, Sint64, Uint64];
65+
}
66+
6267
def AluTypeClass : TypeClass {
6368
let Types = !listconcat(NumberTypeClass.Types, [Bool]);
6469
}
@@ -103,6 +108,11 @@ class AluOpcode : Opcode {
103108
let HasGroup = 1;
104109
}
105110

111+
class IntegerOpcode : Opcode {
112+
let Types = [IntegerTypeClass];
113+
let HasGroup = 1;
114+
}
115+
106116
//===----------------------------------------------------------------------===//
107117
// Jump opcodes
108118
//===----------------------------------------------------------------------===//
@@ -401,6 +411,7 @@ def Rem : Opcode {
401411
let Types = [NumberTypeClass];
402412
let HasGroup = 1;
403413
}
414+
def BitAnd : IntegerOpcode;
404415
def Div : Opcode {
405416
let Types = [NumberTypeClass];
406417
let HasGroup = 1;

clang/test/AST/Interp/literals.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,11 @@ namespace cond {
261261
#endif
262262

263263
};
264+
265+
namespace band {
266+
static_assert((10 & 1) == 0, "");
267+
static_assert((10 & 10) == 10, "");
268+
269+
static_assert((1337 & -1) == 1337, "");
270+
static_assert((0 & gimme(12)) == 0, "");
271+
};

0 commit comments

Comments
 (0)