[PATCH] D134749: [clang][Interp] Implement Div opcode
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 27 09:20:36 PDT 2022
tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Same as the `rem` patch basically.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D134749
Files:
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
Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -167,3 +167,19 @@
// expected-error {{not an integral constant expression}} \
// expected-note {{division by zero}}
};
+
+namespace div {
+ constexpr int zero() { return 0; }
+ static_assert(12 / 3 == 4, "");
+ static_assert(12 / 0 == 12, ""); // 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(12 / -3 == -4, "");
+ static_assert(-12 / 3 == -4, "");
+
+
+ constexpr int LHS = 12;
+ constexpr long unsigned RHS = 3;
+ static_assert(LHS / RHS == 4, "");
+};
Index: clang/lib/AST/Interp/Opcodes.td
===================================================================
--- clang/lib/AST/Interp/Opcodes.td
+++ clang/lib/AST/Interp/Opcodes.td
@@ -401,7 +401,10 @@
let Types = [NumberTypeClass];
let HasGroup = 1;
}
-
+def Div : Opcode {
+ let Types = [NumberTypeClass];
+ let HasGroup = 1;
+}
//===----------------------------------------------------------------------===//
// Unary operators.
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -175,6 +175,25 @@
return false;
}
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool Div(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;
+ }
+ const unsigned Bits = RHS.bitWidth() * 2;
+ T Result;
+ if (!T::div(LHS, RHS, Bits, &Result)) {
+ S.Stk.push<T>(Result);
+ return true;
+ }
+ return false;
+}
+
//===----------------------------------------------------------------------===//
// Inv
//===----------------------------------------------------------------------===//
Index: clang/lib/AST/Interp/Integral.h
===================================================================
--- clang/lib/AST/Interp/Integral.h
+++ clang/lib/AST/Interp/Integral.h
@@ -215,6 +215,11 @@
return false;
}
+ static bool div(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;
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -213,6 +213,8 @@
return Discard(this->emitMul(*T, BO));
case BO_Rem:
return Discard(this->emitRem(*T, BO));
+ case BO_Div:
+ return Discard(this->emitDiv(*T, BO));
case BO_Assign:
if (!this->emitStore(*T, BO))
return false;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134749.463258.patch
Type: text/x-patch
Size: 3257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220927/42ad0a23/attachment.bin>
More information about the cfe-commits
mailing list