[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
Sun Nov 29 20:53:49 PST 2020
MaskRay created this revision.
MaskRay added reviewers: asb, jrtc27, luismarques.
Herald added subscribers: llvm-commits, frasercrmck, NickHung, evandro, apazos, sameer.abuasal, pzheng, s.egerton, lenary, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya.
Herald added a project: LLVM.
MaskRay requested review of this revision.
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.
Repository:
rG LLVM Github Monorepo
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.308258.patch
Type: text/x-patch
Size: 2658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201130/89e2156f/attachment.bin>
More information about the llvm-commits
mailing list