[llvm] r204899 - Fix for pr18931: Crash using integrated assembler with immediate arithmetic

Stepan Dyatkovskiy stpworld at narod.ru
Thu Mar 27 00:49:39 PDT 2014


Author: dyatkovskiy
Date: Thu Mar 27 02:49:39 2014
New Revision: 204899

URL: http://llvm.org/viewvc/llvm-project?rev=204899&view=rev
Log:
Fix for pr18931: Crash using integrated assembler with immediate arithmetic

Fix description:
Expressions like 'cmp r0, #(l1 - l2) >> 3' could not be evaluated on asm parsing stage,
since it is impossible to resolve labels on this stage. In the end of stage we still have
expression (MCExpr).
Then, when we want to encode it, we expect it to be an immediate, but it still an expression.
Patch introduces a Fixup (MCFixup instance), that is processed after main encoding stage.


Added:
    llvm/trunk/test/MC/ARM/cmp-immediate-fixup.s
Modified:
    llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp

Modified: llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp?rev=204899&r1=204898&r2=204899&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp (original)
+++ llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp Thu Mar 27 02:49:39 2014
@@ -271,7 +271,23 @@ public:
   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
                            SmallVectorImpl<MCFixup> &Fixups,
                            const MCSubtargetInfo &STI) const {
-    unsigned SoImm = MI.getOperand(Op).getImm();
+
+    const MCOperand &MO = MI.getOperand(Op);
+
+    // We expect MO to be an immediate or an expression,
+    // if it is an immediate - that's fine, just encode the value.
+    // Otherwise - create a Fixup.
+    if (MO.isExpr()) {
+      const MCExpr *Expr = MO.getExpr();
+      // In instruction code this value always encoded as lowest 12 bits,
+      // so we don't have to perform any specific adjustments and
+      // can use just 2-bytes fixup.
+      MCFixupKind Kind = MCFixupKind(FK_Data_2);
+      Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
+      return 0;
+    }
+
+    unsigned SoImm = MO.getImm();
     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
     assert(SoImmVal != -1 && "Not a valid so_imm value!");
 

Added: llvm/trunk/test/MC/ARM/cmp-immediate-fixup.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ARM/cmp-immediate-fixup.s?rev=204899&view=auto
==============================================================================
--- llvm/trunk/test/MC/ARM/cmp-immediate-fixup.s (added)
+++ llvm/trunk/test/MC/ARM/cmp-immediate-fixup.s Thu Mar 27 02:49:39 2014
@@ -0,0 +1,9 @@
+// PR18931
+// RUN: llvm-mc < %s -triple=arm-linux-gnueabi -filetype=obj -o - \
+// RUN: | llvm-objdump --disassemble -arch=arm - | FileCheck %s
+
+    .text
+// CHECK: cmp r2, #1
+    cmp r2, #(l2 - l1 + 4) >> 2
+l1:
+l2:





More information about the llvm-commits mailing list