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

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 2 04:40:21 PDT 2023


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.

  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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149645

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/c.c


Index: clang/test/AST/Interp/c.c
===================================================================
--- /dev/null
+++ clang/test/AST/Interp/c.c
@@ -0,0 +1,9 @@
+// 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, "");
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -319,19 +319,29 @@
   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 (T != PT_Bool)
+      return this->emitCast(PT_Bool, *T, BO);
+    return true;
+  };
+
   switch (Op) {
   case BO_EQ:
-    return this->emitEQ(*LT, BO);
+    return MaybeCastToBool(this->emitEQ(*LT, BO));
   case BO_NE:
-    return this->emitNE(*LT, BO);
+    return MaybeCastToBool(this->emitNE(*LT, BO));
   case BO_LT:
-    return this->emitLT(*LT, BO);
+    return MaybeCastToBool(this->emitLT(*LT, BO));
   case BO_LE:
-    return this->emitLE(*LT, BO);
+    return MaybeCastToBool(this->emitLE(*LT, BO));
   case BO_GT:
-    return this->emitGT(*LT, BO);
+    return MaybeCastToBool(this->emitGT(*LT, BO));
   case BO_GE:
-    return this->emitGE(*LT, BO);
+    return MaybeCastToBool(this->emitGE(*LT, BO));
   case BO_Sub:
     if (T == PT_Float)
       return this->emitSubf(getRoundingMode(BO), BO);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149645.518682.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230502/1e407e02/attachment.bin>


More information about the cfe-commits mailing list