diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -220,6 +220,11 @@ if (!this->emitStore(*T, BO)) return false; return DiscardResult ? this->emitPopPtr(BO) : true; + case BO_And: + return Discard(this->emitBitAnd(*T, BO)); + case BO_Or: + case BO_LAnd: + case BO_LOr: default: return this->bail(BO); } diff --git a/clang/lib/AST/Interp/Integral.h b/clang/lib/AST/Interp/Integral.h --- a/clang/lib/AST/Interp/Integral.h +++ b/clang/lib/AST/Interp/Integral.h @@ -217,6 +217,11 @@ return false; } + static bool bitAnd(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; diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -153,6 +153,23 @@ return AddSubMulHelper(S, OpPC, Bits, LHS, RHS); } +/// 1) Pops the RHS from the stack. +/// 2) Pops the LHS from the stack. +/// 3) Pushes 'LHS & RHS' on the stack +template ::T> +bool BitAnd(InterpState &S, CodePtr OpPC) { + const T &RHS = S.Stk.pop(); + const T &LHS = S.Stk.pop(); + + unsigned Bits = RHS.bitWidth(); + T Result; + if (!T::bitAnd(LHS, RHS, Bits, &Result)) { + S.Stk.push(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). diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td --- a/clang/lib/AST/Interp/Opcodes.td +++ b/clang/lib/AST/Interp/Opcodes.td @@ -59,6 +59,11 @@ Uint32, Sint64, Uint64]; } +def IntegerTypeClass : TypeClass { + let Types = [Sint8, Uint8, Sint16, Uint16, Sint32, + Uint32, Sint64, Uint64]; +} + def AluTypeClass : TypeClass { let Types = !listconcat(NumberTypeClass.Types, [Bool]); } @@ -103,6 +108,11 @@ let HasGroup = 1; } +class IntegerOpcode : Opcode { + let Types = [IntegerTypeClass]; + let HasGroup = 1; +} + //===----------------------------------------------------------------------===// // Jump opcodes //===----------------------------------------------------------------------===// @@ -401,6 +411,7 @@ let Types = [NumberTypeClass]; let HasGroup = 1; } +def BitAnd : IntegerOpcode; def Div : Opcode { let Types = [NumberTypeClass]; let HasGroup = 1; diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp --- a/clang/test/AST/Interp/literals.cpp +++ b/clang/test/AST/Interp/literals.cpp @@ -261,3 +261,11 @@ #endif }; + +namespace band { + static_assert((10 & 1) == 0, ""); + static_assert((10 & 10) == 10, ""); + + static_assert((1337 & -1) == 1337, ""); + static_assert((0 & gimme(12)) == 0, ""); +};