[clang] 710749f - [clang][Interp] Optionally cast comparison result to non-bool

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 1 01:36:49 PDT 2023


Author: Timm Bäder
Date: 2023-06-01T10:36:33+02:00
New Revision: 710749f78695b0e00026e5ff7c9f94e08e7b482a

URL: https://github.com/llvm/llvm-project/commit/710749f78695b0e00026e5ff7c9f94e08e7b482a
DIFF: https://github.com/llvm/llvm-project/commit/710749f78695b0e00026e5ff7c9f94e08e7b482a.diff

LOG: [clang][Interp] Optionally cast comparison result to non-bool

Our comparison opcodes always produce a Boolean value and push it on the
stack. However, the result of such a comparison in C is int, so the
later code expects an integer value on the stack.

Work around this problem by casting the boolean value to int in those
cases. This is not ideal for C however. The comparison is usually
wrapped in a IntegerToBool cast anyway.

Differential Revision: https://reviews.llvm.org/D149645

Added: 
    clang/test/AST/Interp/c.c

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index df7c4a72f21a7..1be131be66e3b 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -237,19 +237,31 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
   if (!visit(LHS) || !visit(RHS))
     return false;
 
+  // For languages such as C, cast the result of one
+  // of our comparision opcodes to T (which is usually int).
+  auto MaybeCastToBool = [this, T, BO](bool Result) {
+    if (!Result)
+      return false;
+    if (DiscardResult)
+      return this->emitPop(*T, BO);
+    if (T != PT_Bool)
+      return this->emitCast(PT_Bool, *T, BO);
+    return true;
+  };
+
   switch (BO->getOpcode()) {
   case BO_EQ:
-    return Discard(this->emitEQ(*LT, BO));
+    return MaybeCastToBool(this->emitEQ(*LT, BO));
   case BO_NE:
-    return Discard(this->emitNE(*LT, BO));
+    return MaybeCastToBool(this->emitNE(*LT, BO));
   case BO_LT:
-    return Discard(this->emitLT(*LT, BO));
+    return MaybeCastToBool(this->emitLT(*LT, BO));
   case BO_LE:
-    return Discard(this->emitLE(*LT, BO));
+    return MaybeCastToBool(this->emitLE(*LT, BO));
   case BO_GT:
-    return Discard(this->emitGT(*LT, BO));
+    return MaybeCastToBool(this->emitGT(*LT, BO));
   case BO_GE:
-    return Discard(this->emitGE(*LT, BO));
+    return MaybeCastToBool(this->emitGE(*LT, BO));
   case BO_Sub:
     if (BO->getType()->isFloatingType())
       return Discard(this->emitSubf(getRoundingMode(BO), BO));
@@ -925,6 +937,15 @@ bool ByteCodeExprGen<Emitter>::visitConditional(
 
   if (!this->visit(Condition))
     return false;
+
+  // C special case: Convert to bool because our jump ops need that.
+  // TODO: We probably want this to be done in visitBool().
+  if (std::optional<PrimType> CondT = classify(Condition->getType());
+      CondT && CondT != PT_Bool) {
+    if (!this->emitCast(*CondT, PT_Bool, E))
+      return false;
+  }
+
   if (!this->jumpFalse(LabelFalse))
     return false;
 

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
new file mode 100644
index 0000000000000..fe794bb014e7c
--- /dev/null
+++ b/clang/test/AST/Interp/c.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 -verify=ref %s
+
+/// expected-no-diagnostics
+/// ref-no-diagnostics
+
+_Static_assert(1, "");
+_Static_assert(0 != 1, "");
+_Static_assert(1.0 == 1.0, "");
+_Static_assert( (5 > 4) + (3 > 2) == 2, "");
+
+int a = (1 == 1 ? 5 : 3);


        


More information about the cfe-commits mailing list