[PATCH] D43631: [MC] Don't crash on modulo by zero (PR35650)

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 22 10:10:42 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL325810: [MC] Don't crash on modulo by zero (PR35650) (authored by RKSimon, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D43631?vs=135448&id=135459#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D43631

Files:
  llvm/trunk/lib/MC/MCExpr.cpp
  llvm/trunk/test/MC/ELF/div-by-zero.s


Index: llvm/trunk/lib/MC/MCExpr.cpp
===================================================================
--- llvm/trunk/lib/MC/MCExpr.cpp
+++ llvm/trunk/lib/MC/MCExpr.cpp
@@ -754,15 +754,19 @@
     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
     case MCBinaryExpr::And:  Result = LHS & RHS; break;
     case MCBinaryExpr::Div:
+    case MCBinaryExpr::Mod:
       // Handle division by zero. gas just emits a warning and keeps going,
       // we try to be stricter.
       // FIXME: Currently the caller of this function has no way to understand
       // we're bailing out because of 'division by zero'. Therefore, it will
       // emit a 'expected relocatable expression' error. It would be nice to
       // change this code to emit a better diagnostic.
       if (RHS == 0)
         return false;
-      Result = LHS / RHS;
+      if (ABE->getOpcode() == MCBinaryExpr::Div)
+        Result = LHS / RHS;
+      else
+        Result = LHS % RHS;
       break;
     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
@@ -772,7 +776,6 @@
     case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
-    case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
Index: llvm/trunk/test/MC/ELF/div-by-zero.s
===================================================================
--- llvm/trunk/test/MC/ELF/div-by-zero.s
+++ llvm/trunk/test/MC/ELF/div-by-zero.s
@@ -4,3 +4,6 @@
 
 // CHECK: expected relocatable expression
 .int 1/0
+
+// CHECK: expected relocatable expression
+.int 2%0


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43631.135459.patch
Type: text/x-patch
Size: 1848 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180222/3ad1729e/attachment.bin>


More information about the llvm-commits mailing list