Recommitted fix for PR18931, with extended tests set.
Issue subject: 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.
llvm-svn: 205094
diff --git a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
index 2997c61..a0842b5 100644
--- a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
+++ b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
@@ -9359,7 +9359,7 @@
const MCExpr *SOExpr = Op->getImm();
int64_t Value;
if (!SOExpr->EvaluateAsAbsolute(Value))
- return Match_InvalidOperand;
+ return Match_Success;
assert((Value >= INT32_MIN && Value <= INT32_MAX) &&
"expression value must be representiable in 32 bits");
}
diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp
index 12cd336..7ce9104 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp
@@ -272,7 +272,25 @@
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.
+ // Due to requirements of relocatable records we have to use FK_Data_4.
+ // See ARMELFObjectWriter::ExplicitRelSym and
+ // ARMELFObjectWriter::GetRelocTypeInner for more details.
+ MCFixupKind Kind = MCFixupKind(FK_Data_4);
+ 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!");
diff --git a/llvm/test/MC/ARM/cmp-immediate-fixup-error.s b/llvm/test/MC/ARM/cmp-immediate-fixup-error.s
new file mode 100644
index 0000000..25a2368
--- /dev/null
+++ b/llvm/test/MC/ARM/cmp-immediate-fixup-error.s
@@ -0,0 +1,7 @@
+@ RUN: not llvm-mc -triple=arm-linux-gnueabi -filetype=obj < %s 2>&1 | FileCheck %s
+
+.text
+ cmp r0, #(l1 - unknownLabel + 4) >> 2
+@ CHECK: error: expected relocatable expression
+
+l1:
diff --git a/llvm/test/MC/ARM/cmp-immediate-fixup-error2.s b/llvm/test/MC/ARM/cmp-immediate-fixup-error2.s
new file mode 100644
index 0000000..71f7fa1
--- /dev/null
+++ b/llvm/test/MC/ARM/cmp-immediate-fixup-error2.s
@@ -0,0 +1,7 @@
+@ RUN: not llvm-mc -triple=arm-linux-gnueabi -filetype=obj < %s 2>&1 | FileCheck %s
+
+.text
+ cmp r0, #(l1 - unknownLabel)
+@ CHECK: error: symbol 'unknownLabel' can not be undefined in a subtraction expression
+
+l1:
diff --git a/llvm/test/MC/ARM/cmp-immediate-fixup.s b/llvm/test/MC/ARM/cmp-immediate-fixup.s
new file mode 100644
index 0000000..e21d5c2
--- /dev/null
+++ b/llvm/test/MC/ARM/cmp-immediate-fixup.s
@@ -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:
diff --git a/llvm/test/MC/ARM/cmp-immediate-fixup2.s b/llvm/test/MC/ARM/cmp-immediate-fixup2.s
new file mode 100644
index 0000000..c091145
--- /dev/null
+++ b/llvm/test/MC/ARM/cmp-immediate-fixup2.s
@@ -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, #0
+ cmp r2, #(l2 - l1)
+l1:
+l2:
diff --git a/llvm/test/MC/ARM/label_offset.s b/llvm/test/MC/ARM/label_offset.s
deleted file mode 100644
index 0aeb3b6..0000000
--- a/llvm/test/MC/ARM/label_offset.s
+++ /dev/null
@@ -1,8 +0,0 @@
-@ RUN: not llvm-mc -triple=armv7-linux-gnuabi -filetype=obj < %s 2>&1 | FileCheck %s
-
-.text
- cmp r2, #(l2 - l1) >> 6
-@ CHECK: error: invalid operand for instruction
-
-l1:
-l2: