[llvm] f72b654 - [MC][x86] Allow non-MCTargetExpr RHS when the LHS of a MCBinaryExpr is MCTargetExpr (#75693)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 20 00:43:22 PST 2023


Author: Chenyang Gao
Date: 2023-12-20T16:43:18+08:00
New Revision: f72b65499156171eca25ad0e7becb274347c7c02

URL: https://github.com/llvm/llvm-project/commit/f72b65499156171eca25ad0e7becb274347c7c02
DIFF: https://github.com/llvm/llvm-project/commit/f72b65499156171eca25ad0e7becb274347c7c02.diff

LOG: [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.

Added: 
    llvm/test/MC/X86/register-assignment-error.s

Modified: 
    llvm/lib/MC/MCExpr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 061f2ad13ffa78..a85182aa06ad52 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 00000000000000..6c5fcf3cae9e1f
--- /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


        


More information about the llvm-commits mailing list