[llvm] e27e3ba - [RISCVAsmParser] Allow a SymbolRef operand to be a complex expression

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 1 16:08:18 PST 2020


Author: Fangrui Song
Date: 2020-12-01T16:08:09-08:00
New Revision: e27e3ba9c9d8e3f25d4b03a71cdb6b47cbff2c8b

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

LOG: [RISCVAsmParser] Allow a SymbolRef operand to be a complex expression

So that instructions like `lla a5, (0xFF + end) - 4` (supported by GNU as) can
be parsed.

Add a missing test that an operand like `foo + foo` is not allowed.

Reviewed By: jrtc27

Differential Revision: https://reviews.llvm.org/D92293

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
    llvm/test/MC/RISCV/rvi-pseudos-invalid.s
    llvm/test/MC/RISCV/rvi-pseudos.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index cb94d05d9dd4..7874d46386d3 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -31,6 +31,7 @@
 #include "llvm/MC/MCRegisterInfo.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCValue.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/RISCVAttributes.h"
@@ -1856,34 +1857,11 @@ bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
     Expr = RE->getSubExpr();
   }
 
-  // It's a simple symbol reference or constant with no addend.
-  if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
-    return true;
-
-  const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
-  if (!BE)
-    return false;
-
-  if (!isa<MCSymbolRefExpr>(BE->getLHS()))
-    return false;
-
-  if (BE->getOpcode() != MCBinaryExpr::Add &&
-      BE->getOpcode() != MCBinaryExpr::Sub)
-    return false;
-
-  // We are able to support the subtraction of two symbol references
-  if (BE->getOpcode() == MCBinaryExpr::Sub &&
-      isa<MCSymbolRefExpr>(BE->getRHS()))
-    return true;
-
-  // See if the addend is a constant, otherwise there's more going
-  // on here than we can deal with.
-  auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
-  if (!AddendExpr)
-    return false;
-
-  // It's some symbol reference + a constant addend
-  return Kind != RISCVMCExpr::VK_RISCV_Invalid;
+  MCValue Res;
+  MCFixup Fixup;
+  if (Expr->evaluateAsRelocatable(Res, nullptr, &Fixup))
+    return Res.getRefKind() == RISCVMCExpr::VK_RISCV_None;
+  return false;
 }
 
 bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {

diff  --git a/llvm/test/MC/RISCV/rvi-pseudos-invalid.s b/llvm/test/MC/RISCV/rvi-pseudos-invalid.s
index 926d26f0649c..9d9849ba0ac0 100644
--- a/llvm/test/MC/RISCV/rvi-pseudos-invalid.s
+++ b/llvm/test/MC/RISCV/rvi-pseudos-invalid.s
@@ -27,3 +27,5 @@ sw a2, %lo(a_symbol)(a4), a3 # CHECK: :[[@LINE]]:27: error: invalid operand for
 
 # Too few operands must be rejected
 sw a2, a_symbol # CHECK: :[[@LINE]]:1: error: too few operands for instruction
+
+la a1, foo+foo # CHECK: :[[@LINE]]:8: error: operand must be a bare symbol name

diff  --git a/llvm/test/MC/RISCV/rvi-pseudos.s b/llvm/test/MC/RISCV/rvi-pseudos.s
index 37501a00e3ea..859f5fe640f6 100644
--- a/llvm/test/MC/RISCV/rvi-pseudos.s
+++ b/llvm/test/MC/RISCV/rvi-pseudos.s
@@ -181,3 +181,9 @@ lw a2, zero
 # CHECK: auipc a4, %pcrel_hi(zero)
 # CHECK: sw  a3, %pcrel_lo(.Lpcrel_hi29)(a4)
 sw a3, zero, a4
+
+## Check that a complex expression can be simplified and matched.
+# CHECK: .Lpcrel_hi30:
+# CHECK: auipc a5, %pcrel_hi((255+a_symbol)-4)
+# CHECK: addi  a5, a5, %pcrel_lo(.Lpcrel_hi30)
+lla a5, (0xFF + a_symbol) - 4


        


More information about the llvm-commits mailing list