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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe27e3ba9c9d8: [RISCVAsmParser] Allow a SymbolRef operand to be a complex expression (authored by MaskRay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92293/new/

https://reviews.llvm.org/D92293

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


Index: llvm/test/MC/RISCV/rvi-pseudos.s
===================================================================
--- llvm/test/MC/RISCV/rvi-pseudos.s
+++ llvm/test/MC/RISCV/rvi-pseudos.s
@@ -181,3 +181,9 @@
 # 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
Index: llvm/test/MC/RISCV/rvi-pseudos-invalid.s
===================================================================
--- llvm/test/MC/RISCV/rvi-pseudos-invalid.s
+++ llvm/test/MC/RISCV/rvi-pseudos-invalid.s
@@ -27,3 +27,5 @@
 
 # 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
Index: llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
===================================================================
--- llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ 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 @@
     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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92293.308807.patch
Type: text/x-patch
Size: 2658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201202/fc09f6ac/attachment.bin>


More information about the llvm-commits mailing list