From f72b65499156171eca25ad0e7becb274347c7c02 Mon Sep 17 00:00:00 2001 From: Chenyang Gao Date: Wed, 20 Dec 2023 16:43:18 +0800 Subject: [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. --- llvm/lib/MC/MCExpr.cpp | 21 +++++++++++---------- llvm/test/MC/X86/register-assignment-error.s | 8 ++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 llvm/test/MC/X86/register-assignment-error.s 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(ABE->getLHS())) { - const MCTargetExpr *R = cast(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(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 -- cgit v1.1