aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChenyang Gao <cygao09@gmail.com>2023-12-20 16:43:18 +0800
committerGitHub <noreply@github.com>2023-12-20 16:43:18 +0800
commitf72b65499156171eca25ad0e7becb274347c7c02 (patch)
treedb29bb0a694745f33e0a1456cff7246126279eff
parentbbe6c81f808093c4030d7904136ff2f9dad6e73b (diff)
downloadllvm-f72b65499156171eca25ad0e7becb274347c7c02.zip
llvm-f72b65499156171eca25ad0e7becb274347c7c02.tar.gz
llvm-f72b65499156171eca25ad0e7becb274347c7c02.tar.bz2
[MC][x86] Allow non-MCTargetExpr RHS when the LHS of a MCBinaryExpr is MCTargetExpr (#75693)
This fixes #73109. In instruction `addl %eax %rax`, because there is a missing comma in the middle of two registers, the asm parser will treat it as a binary expression. ``` %rax % rax --> register mod identifier ``` However, In `MCExpr::evaluateAsRelocatableImpl`, it only checks the left side of the expression. This patch ensures the right side will also be checked.
-rw-r--r--llvm/lib/MC/MCExpr.cpp21
-rw-r--r--llvm/test/MC/X86/register-assignment-error.s8
2 files changed, 19 insertions, 10 deletions
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 061f2ad..a85182a 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -943,16 +943,17 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
Addrs, InSet)) {
// Check if both are Target Expressions, see if we can compare them.
if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS())) {
- const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS());
- switch (ABE->getOpcode()) {
- case MCBinaryExpr::EQ:
- Res = MCValue::get(L->isEqualTo(R) ? -1 : 0);
- return true;
- case MCBinaryExpr::NE:
- Res = MCValue::get(L->isEqualTo(R) ? 0 : -1);
- return true;
- default:
- break;
+ if (const MCTargetExpr *R = dyn_cast<MCTargetExpr>(ABE->getRHS())) {
+ switch (ABE->getOpcode()) {
+ case MCBinaryExpr::EQ:
+ Res = MCValue::get(L->isEqualTo(R) ? -1 : 0);
+ return true;
+ case MCBinaryExpr::NE:
+ Res = MCValue::get(L->isEqualTo(R) ? 0 : -1);
+ return true;
+ default:
+ break;
+ }
}
}
return false;
diff --git a/llvm/test/MC/X86/register-assignment-error.s b/llvm/test/MC/X86/register-assignment-error.s
new file mode 100644
index 0000000..6c5fcf3
--- /dev/null
+++ b/llvm/test/MC/X86/register-assignment-error.s
@@ -0,0 +1,8 @@
+// RUN: not llvm-mc -triple x86_64 %s -o /dev/null 2>&1 | FileCheck %s
+
+var_xdata = %rcx
+
+// This used to crash.
+.if var_xdata == 1
+.endif
+// CHECK: error: expected absolute expression \ No newline at end of file