[clang] c9ad877 - [clang][Interp] Implement rem opcode
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 14 04:01:11 PDT 2022
Author: Timm Bäder
Date: 2022-10-14T12:57:57+02:00
New Revision: c9ad877844a7fd52726ed3f11bb6e7fb90e9358e
URL: https://github.com/llvm/llvm-project/commit/c9ad877844a7fd52726ed3f11bb6e7fb90e9358e
DIFF: https://github.com/llvm/llvm-project/commit/c9ad877844a7fd52726ed3f11bb6e7fb90e9358e.diff
LOG: [clang][Interp] Implement rem opcode
Implement an opcode to get the remainder of the divison between LHS and
RHS.
Differential Revision: https://reviews.llvm.org/D134744
Added:
Modified:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/lib/AST/Interp/Integral.h
clang/lib/AST/Interp/Interp.h
clang/lib/AST/Interp/Opcodes.td
clang/test/AST/Interp/literals.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 06963562638e..1574f8b6c6c8 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -212,6 +212,8 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
return Discard(this->emitAdd(*T, BO));
case BO_Mul:
return Discard(this->emitMul(*T, BO));
+ case BO_Rem:
+ return Discard(this->emitRem(*T, BO));
case BO_Assign:
if (!this->emitStore(*T, BO))
return false;
diff --git a/clang/lib/AST/Interp/Integral.h b/clang/lib/AST/Interp/Integral.h
index d02b68ac8379..c7e558965531 100644
--- a/clang/lib/AST/Interp/Integral.h
+++ b/clang/lib/AST/Interp/Integral.h
@@ -207,6 +207,11 @@ template <unsigned Bits, bool Signed> class Integral final {
return CheckMulUB(A.V, B.V, R->V);
}
+ static bool rem(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
index f94fcc955d40..4d0c7f62f46c 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -153,6 +153,39 @@ bool Mul(InterpState &S, CodePtr OpPC) {
return AddSubMulHelper<T, T::mul, std::multiplies>(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 (the remainder of dividing LHS by RHS).
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool Rem(InterpState &S, CodePtr OpPC) {
+ const T &RHS = S.Stk.pop<T>();
+ const T &LHS = S.Stk.pop<T>();
+
+ if (RHS.isZero()) {
+ const SourceInfo &Loc = S.Current->getSource(OpPC);
+ S.FFDiag(Loc, diag::note_expr_divide_by_zero);
+ return false;
+ }
+
+ if (LHS.isSigned() && LHS.isMin() && RHS.isNegative() && RHS.isMinusOne()) {
+ APSInt LHSInt = LHS.toAPSInt();
+ SmallString<32> Trunc;
+ (-LHSInt.extend(LHSInt.getBitWidth() + 1)).toString(Trunc, 10);
+ const SourceInfo &Loc = S.Current->getSource(OpPC);
+ const Expr *E = S.Current->getExpr(OpPC);
+ S.CCEDiag(Loc, diag::note_constexpr_overflow) << Trunc << E->getType();
+ return false;
+ }
+
+ const unsigned Bits = RHS.bitWidth() * 2;
+ T Result;
+ if (!T::rem(LHS, RHS, Bits, &Result)) {
+ S.Stk.push<T>(Result);
+ return true;
+ }
+ return false;
+}
+
//===----------------------------------------------------------------------===//
// Inv
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 64d7e31d013c..61f97337fd04 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -54,9 +54,13 @@ class TypeClass {
list<Type> Types;
}
-def AluTypeClass : TypeClass {
+def NumberTypeClass : TypeClass {
let Types = [Sint8, Uint8, Sint16, Uint16, Sint32,
- Uint32, Sint64, Uint64, Bool];
+ Uint32, Sint64, Uint64];
+}
+
+def AluTypeClass : TypeClass {
+ let Types = !listconcat(NumberTypeClass.Types, [Bool]);
}
def PtrTypeClass : TypeClass {
@@ -393,6 +397,10 @@ def SubOffset : AluOpcode;
def Sub : AluOpcode;
def Add : AluOpcode;
def Mul : AluOpcode;
+def Rem : Opcode {
+ let Types = [NumberTypeClass];
+ let HasGroup = 1;
+}
//===----------------------------------------------------------------------===//
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 592d393fad80..19c80f478d1a 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -161,3 +161,30 @@ namespace SizeOf {
#endif
};
+
+namespace rem {
+ static_assert(2 % 2 == 0, "");
+ static_assert(2 % 1 == 0, "");
+ static_assert(-3 % 4 == -3, "");
+ static_assert(4 % -2 == 0, "");
+ static_assert(-3 % -4 == -3, "");
+
+ constexpr int zero() { return 0; }
+ static_assert(10 % zero() == 20, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note {{division by zero}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note {{division by zero}}
+
+
+ static_assert(true % true == 0, "");
+ static_assert(false % true == 0, "");
+ static_assert(true % false == 10, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note {{division by zero}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note {{division by zero}}
+ constexpr int x = INT_MIN % - 1; // ref-error {{must be initialized by a constant expression}} \
+ // ref-note {{value 2147483648 is outside the range}} \
+ // expected-error {{must be initialized by a constant expression}} \
+ // expected-note {{value 2147483648 is outside the range}} \
+
+};
More information about the cfe-commits
mailing list