blob: 7113adcf65b21ab3dc63b0c2fe706d7eaa4bf2b4 [file] [log] [blame]
Alex Bradbury04f06d92017-08-08 14:43:36 +00001//===-- RISCVAsmParser.cpp - Parse RISCV assembly to MCInst instructions --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Bradbury04f06d92017-08-08 14:43:36 +00006//
7//===----------------------------------------------------------------------===//
8
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00009#include "MCTargetDesc/RISCVAsmBackend.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000010#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000011#include "MCTargetDesc/RISCVMCTargetDesc.h"
Alex Bradburybca0c3c2018-05-11 17:30:28 +000012#include "MCTargetDesc/RISCVTargetStreamer.h"
Ana Pazos9d6c5532018-10-04 21:50:54 +000013#include "Utils/RISCVBaseInfo.h"
Alex Bradbury22c091f2018-11-15 10:11:31 +000014#include "Utils/RISCVMatInt.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000015#include "llvm/ADT/STLExtras.h"
Alex Bradbury893e5bc2018-11-28 16:39:14 +000016#include "llvm/ADT/SmallVector.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000017#include "llvm/ADT/StringSwitch.h"
Alex Bradbury9c03e4c2018-11-12 14:25:07 +000018#include "llvm/MC/MCAssembler.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000022#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury22531c42019-02-15 09:53:32 +000023#include "llvm/MC/MCObjectFileInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000024#include "llvm/MC/MCParser/MCAsmLexer.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000027#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000030#include "llvm/Support/Casting.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000031#include "llvm/Support/MathExtras.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000032#include "llvm/Support/TargetRegistry.h"
33
Alex Bradbury6a4b5442018-06-07 15:35:47 +000034#include <limits>
35
Alex Bradbury04f06d92017-08-08 14:43:36 +000036using namespace llvm;
37
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000038// Include the auto-generated portion of the compress emitter.
39#define GEN_COMPRESS_INSTR
40#include "RISCVGenCompressInstEmitter.inc"
41
Alex Bradbury04f06d92017-08-08 14:43:36 +000042namespace {
43struct RISCVOperand;
44
45class RISCVAsmParser : public MCTargetAsmParser {
Alex Bradbury893e5bc2018-11-28 16:39:14 +000046 SmallVector<FeatureBitset, 4> FeatureBitStack;
47
Alex Bradbury04f06d92017-08-08 14:43:36 +000048 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000049 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradburydab1f6f2019-03-22 11:21:40 +000050 bool isRV32E() const { return getSTI().hasFeature(RISCV::FeatureRV32E); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000051
Alex Bradburybca0c3c2018-05-11 17:30:28 +000052 RISCVTargetStreamer &getTargetStreamer() {
53 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
54 return static_cast<RISCVTargetStreamer &>(TS);
55 }
56
Alex Bradbury7bc2a952017-12-07 10:46:23 +000057 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
58 unsigned Kind) override;
59
Alex Bradbury6758ecb2017-09-17 14:27:35 +000060 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury6a4b5442018-06-07 15:35:47 +000061 int64_t Lower, int64_t Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000062
Alex Bradbury04f06d92017-08-08 14:43:36 +000063 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
64 OperandVector &Operands, MCStreamer &Out,
65 uint64_t &ErrorInfo,
66 bool MatchingInlineAsm) override;
67
68 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
69
70 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
71 SMLoc NameLoc, OperandVector &Operands) override;
72
73 bool ParseDirective(AsmToken DirectiveID) override;
74
Alex Bradbury6a4b5442018-06-07 15:35:47 +000075 // Helper to actually emit an instruction to the MCStreamer. Also, when
76 // possible, compression of the instruction is performed.
77 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
78
79 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
80 // synthesize the desired immedate value into the destination register.
81 void emitLoadImm(unsigned DestReg, int64_t Value, MCStreamer &Out);
82
Alex Bradbury22531c42019-02-15 09:53:32 +000083 // Helper to emit a combination of AUIPC and SecondOpcode. Used to implement
84 // helpers such as emitLoadLocalAddress and emitLoadAddress.
85 void emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
86 const MCExpr *Symbol, RISCVMCExpr::VariantKind VKHi,
87 unsigned SecondOpcode, SMLoc IDLoc, MCStreamer &Out);
88
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +000089 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
90 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
91
Alex Bradbury22531c42019-02-15 09:53:32 +000092 // Helper to emit pseudo instruction "la" used in GOT/PC-rel addressing.
93 void emitLoadAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
94
Kito Cheng303217e2019-02-20 03:31:32 +000095 // Helper to emit pseudo load/store instruction with a symbol.
96 void emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
97 MCStreamer &Out, bool HasTmpReg);
98
Alex Bradbury6a4b5442018-06-07 15:35:47 +000099 /// Helper for processing MC instructions that have been successfully matched
100 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
101 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
102 /// in this method.
103 bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
104
Alex Bradbury04f06d92017-08-08 14:43:36 +0000105// Auto-generated instruction matching functions
106#define GET_ASSEMBLER_HEADER
107#include "RISCVGenAsmMatcher.inc"
108
Ana Pazos9d6c5532018-10-04 21:50:54 +0000109 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000110 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000111 OperandMatchResultTy parseRegister(OperandVector &Operands,
112 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000113 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000114 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +0000115 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000116 OperandMatchResultTy parseCallSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000117 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000118
Alex Bradbury68f73c12018-09-18 15:18:16 +0000119 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000120
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000121 bool parseDirectiveOption();
122
123 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
124 if (!(getSTI().getFeatureBits()[Feature])) {
125 MCSubtargetInfo &STI = copySTI();
126 setAvailableFeatures(
127 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
128 }
129 }
130
131 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
132 if (getSTI().getFeatureBits()[Feature]) {
133 MCSubtargetInfo &STI = copySTI();
134 setAvailableFeatures(
135 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
136 }
137 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000138
Alex Bradbury893e5bc2018-11-28 16:39:14 +0000139 void pushFeatureBits() {
140 FeatureBitStack.push_back(getSTI().getFeatureBits());
141 }
142
143 bool popFeatureBits() {
144 if (FeatureBitStack.empty())
145 return true;
146
147 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
148 copySTI().setFeatureBits(FeatureBits);
149 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
150
151 return false;
152 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000153public:
154 enum RISCVMatchResultTy {
155 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
156#define GET_OPERAND_DIAGNOSTIC_TYPES
157#include "RISCVGenAsmMatcher.inc"
158#undef GET_OPERAND_DIAGNOSTIC_TYPES
159 };
160
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000161 static bool classifySymbolRef(const MCExpr *Expr,
162 RISCVMCExpr::VariantKind &Kind,
163 int64_t &Addend);
164
Alex Bradbury04f06d92017-08-08 14:43:36 +0000165 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
166 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000167 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000168 Parser.addAliasForDirective(".half", ".2byte");
169 Parser.addAliasForDirective(".hword", ".2byte");
170 Parser.addAliasForDirective(".word", ".4byte");
171 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000172 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
173 }
174};
175
176/// RISCVOperand - Instances of this class represent a parsed machine
177/// instruction
178struct RISCVOperand : public MCParsedAsmOperand {
179
180 enum KindTy {
181 Token,
182 Register,
183 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000184 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000185 } Kind;
186
Alex Bradburya6e62482017-12-07 10:53:48 +0000187 bool IsRV64;
188
Alex Bradbury04f06d92017-08-08 14:43:36 +0000189 struct RegOp {
190 unsigned RegNum;
191 };
192
193 struct ImmOp {
194 const MCExpr *Val;
195 };
196
Ana Pazos9d6c5532018-10-04 21:50:54 +0000197 struct SysRegOp {
198 const char *Data;
199 unsigned Length;
200 unsigned Encoding;
201 // FIXME: Add the Encoding parsed fields as needed for checks,
202 // e.g.: read/write or user/supervisor/machine privileges.
203 };
204
Alex Bradbury04f06d92017-08-08 14:43:36 +0000205 SMLoc StartLoc, EndLoc;
206 union {
207 StringRef Tok;
208 RegOp Reg;
209 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000210 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000211 };
212
213 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
214
215public:
216 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
217 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000218 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000219 StartLoc = o.StartLoc;
220 EndLoc = o.EndLoc;
221 switch (Kind) {
222 case Register:
223 Reg = o.Reg;
224 break;
225 case Immediate:
226 Imm = o.Imm;
227 break;
228 case Token:
229 Tok = o.Tok;
230 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000231 case SystemRegister:
232 SysReg = o.SysReg;
233 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000234 }
235 }
236
237 bool isToken() const override { return Kind == Token; }
238 bool isReg() const override { return Kind == Register; }
239 bool isImm() const override { return Kind == Immediate; }
240 bool isMem() const override { return false; }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000241 bool isSystemRegister() const { return Kind == SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000242
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000243 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
244 RISCVMCExpr::VariantKind &VK) {
245 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000246 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000247 return RE->evaluateAsConstant(Imm);
248 }
249
250 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000251 VK = RISCVMCExpr::VK_RISCV_None;
252 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000253 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000254 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000255
256 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000257 }
258
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000259 // True if operand is a symbol with no modifiers, or a constant with no
260 // modifiers and isShiftedInt<N-1, 1>(Op).
261 template <int N> bool isBareSimmNLsb0() const {
262 int64_t Imm;
263 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000264 if (!isImm())
265 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000266 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000267 bool IsValid;
268 if (!IsConstantImm)
269 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
270 else
271 IsValid = isShiftedInt<N - 1, 1>(Imm);
272 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000273 }
274
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000275 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
276
Shiva Chen98f93892018-04-25 14:18:55 +0000277 bool isBareSymbol() const {
278 int64_t Imm;
279 RISCVMCExpr::VariantKind VK;
280 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000281 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000282 return false;
283 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
284 VK == RISCVMCExpr::VK_RISCV_None;
285 }
286
Alex Bradbury44668ae2019-04-01 14:53:17 +0000287 bool isCallSymbol() const {
288 int64_t Imm;
289 RISCVMCExpr::VariantKind VK;
290 // Must be of 'immediate' type but not a constant.
291 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
292 return false;
293 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
Alex Bradburyf8078f62019-04-02 12:47:20 +0000294 (VK == RISCVMCExpr::VK_RISCV_CALL ||
295 VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000296 }
297
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000298 bool isTPRelAddSymbol() const {
299 int64_t Imm;
300 RISCVMCExpr::VariantKind VK;
301 // Must be of 'immediate' type but not a constant.
302 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
303 return false;
304 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
305 VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
306 }
307
Ana Pazos9d6c5532018-10-04 21:50:54 +0000308 bool isCSRSystemRegister() const { return isSystemRegister(); }
309
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000310 /// Return true if the operand is a valid for the fence instruction e.g.
311 /// ('iorw').
312 bool isFenceArg() const {
313 if (!isImm())
314 return false;
315 const MCExpr *Val = getImm();
316 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
317 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
318 return false;
319
320 StringRef Str = SVal->getSymbol().getName();
321 // Letters must be unique, taken from 'iorw', and in ascending order. This
322 // holds as long as each individual character is one of 'iorw' and is
323 // greater than the previous character.
324 char Prev = '\0';
325 for (char c : Str) {
326 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
327 return false;
328 if (c <= Prev)
329 return false;
330 Prev = c;
331 }
332 return true;
333 }
334
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000335 /// Return true if the operand is a valid floating point rounding mode.
336 bool isFRMArg() const {
337 if (!isImm())
338 return false;
339 const MCExpr *Val = getImm();
340 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
341 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
342 return false;
343
344 StringRef Str = SVal->getSymbol().getName();
345
346 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
347 }
348
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000349 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000350 int64_t Imm;
351 RISCVMCExpr::VariantKind VK;
352 if (!isImm())
353 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000354 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000355 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
356 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000357 // Given only Imm, ensuring that the actually specified constant is either
358 // a signed or unsigned 64-bit number is unfortunately impossible.
359 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
360 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
361 }
362
Alex Bradburya6e62482017-12-07 10:53:48 +0000363 bool isUImmLog2XLen() const {
364 int64_t Imm;
365 RISCVMCExpr::VariantKind VK;
366 if (!isImm())
367 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000368 if (!evaluateConstantImm(getImm(), Imm, VK) ||
369 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000370 return false;
371 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
372 }
373
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000374 bool isUImmLog2XLenNonZero() const {
375 int64_t Imm;
376 RISCVMCExpr::VariantKind VK;
377 if (!isImm())
378 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000379 if (!evaluateConstantImm(getImm(), Imm, VK) ||
380 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000381 return false;
382 if (Imm == 0)
383 return false;
384 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
385 }
386
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000387 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000388 int64_t Imm;
389 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000390 if (!isImm())
391 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000392 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000393 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000394 }
395
Alex Bradbury60714f92017-12-13 09:32:55 +0000396 bool isUImm5NonZero() const {
397 int64_t Imm;
398 RISCVMCExpr::VariantKind VK;
399 if (!isImm())
400 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000401 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000402 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
403 VK == RISCVMCExpr::VK_RISCV_None;
404 }
405
Alex Bradbury581d6b02017-12-13 09:41:21 +0000406 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000407 if (!isImm())
408 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000409 RISCVMCExpr::VariantKind VK;
410 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000411 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000412 return IsConstantImm && isInt<6>(Imm) &&
413 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000414 }
415
Shiva Chenb22c1d22018-02-02 02:43:23 +0000416 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000417 if (!isImm())
418 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000419 RISCVMCExpr::VariantKind VK;
420 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000421 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000422 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
423 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000424 }
425
Shiva Chen7c172422018-02-22 15:02:28 +0000426 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000427 if (!isImm())
428 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000429 int64_t Imm;
430 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000431 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000432 return IsConstantImm && (Imm != 0) &&
433 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000434 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000435 }
436
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000437 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000438 if (!isImm())
439 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000440 int64_t Imm;
441 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000442 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000443 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
444 VK == RISCVMCExpr::VK_RISCV_None;
445 }
446
447 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000448 if (!isImm())
449 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000450 int64_t Imm;
451 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000452 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000453 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
454 VK == RISCVMCExpr::VK_RISCV_None;
455 }
456
457 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000458 if (!isImm())
459 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000460 int64_t Imm;
461 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000462 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000463 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
464 VK == RISCVMCExpr::VK_RISCV_None;
465 }
466
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000467 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
468
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000469 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000470 if (!isImm())
471 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000472 int64_t Imm;
473 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000474 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000475 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
476 VK == RISCVMCExpr::VK_RISCV_None;
477 }
478
Alex Bradbury60714f92017-12-13 09:32:55 +0000479 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000480 if (!isImm())
481 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000482 int64_t Imm;
483 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000484 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000485 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
486 VK == RISCVMCExpr::VK_RISCV_None;
487 }
488
Alex Bradbury04f06d92017-08-08 14:43:36 +0000489 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000490 RISCVMCExpr::VariantKind VK;
491 int64_t Imm;
492 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000493 if (!isImm())
494 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000495 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000496 if (!IsConstantImm)
497 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
498 else
499 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000500 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000501 VK == RISCVMCExpr::VK_RISCV_LO ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000502 VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
503 VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000504 }
505
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000506 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
507
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000508 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000509
Shiva Chenb22c1d22018-02-02 02:43:23 +0000510 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000511 if (!isImm())
512 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000513 int64_t Imm;
514 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000515 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000516 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000517 VK == RISCVMCExpr::VK_RISCV_None;
518 }
519
Alex Bradbury74340f12018-09-18 15:08:35 +0000520 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000521 RISCVMCExpr::VariantKind VK;
522 int64_t Imm;
523 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000524 if (!isImm())
525 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000526 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000527 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000528 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000529 return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
530 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000531 } else {
532 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000533 VK == RISCVMCExpr::VK_RISCV_HI ||
534 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000535 }
536 }
537
538 bool isUImm20AUIPC() const {
539 RISCVMCExpr::VariantKind VK;
540 int64_t Imm;
541 bool IsValid;
542 if (!isImm())
543 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000544 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000545 if (!IsConstantImm) {
546 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000547 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
548 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000549 } else {
550 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000551 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
552 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000553 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000554 }
555
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000556 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000557
Alex Bradbury04f06d92017-08-08 14:43:36 +0000558 /// getStartLoc - Gets location of the first token of this operand
559 SMLoc getStartLoc() const override { return StartLoc; }
560 /// getEndLoc - Gets location of the last token of this operand
561 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000562 /// True if this operand is for an RV64 instruction
563 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000564
565 unsigned getReg() const override {
566 assert(Kind == Register && "Invalid type access!");
567 return Reg.RegNum;
568 }
569
Ana Pazos9d6c5532018-10-04 21:50:54 +0000570 StringRef getSysReg() const {
571 assert(Kind == SystemRegister && "Invalid access!");
572 return StringRef(SysReg.Data, SysReg.Length);
573 }
574
Alex Bradbury04f06d92017-08-08 14:43:36 +0000575 const MCExpr *getImm() const {
576 assert(Kind == Immediate && "Invalid type access!");
577 return Imm.Val;
578 }
579
580 StringRef getToken() const {
581 assert(Kind == Token && "Invalid type access!");
582 return Tok;
583 }
584
585 void print(raw_ostream &OS) const override {
586 switch (Kind) {
587 case Immediate:
588 OS << *getImm();
589 break;
590 case Register:
591 OS << "<register x";
592 OS << getReg() << ">";
593 break;
594 case Token:
595 OS << "'" << getToken() << "'";
596 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000597 case SystemRegister:
598 OS << "<sysreg: " << getSysReg() << '>';
599 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000600 }
601 }
602
Alex Bradburya6e62482017-12-07 10:53:48 +0000603 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
604 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000605 auto Op = make_unique<RISCVOperand>(Token);
606 Op->Tok = Str;
607 Op->StartLoc = S;
608 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000609 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000610 return Op;
611 }
612
613 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000614 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000615 auto Op = make_unique<RISCVOperand>(Register);
616 Op->Reg.RegNum = RegNo;
617 Op->StartLoc = S;
618 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000619 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000620 return Op;
621 }
622
623 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000624 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000625 auto Op = make_unique<RISCVOperand>(Immediate);
626 Op->Imm.Val = Val;
627 Op->StartLoc = S;
628 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000629 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000630 return Op;
631 }
632
Ana Pazos9d6c5532018-10-04 21:50:54 +0000633 static std::unique_ptr<RISCVOperand>
634 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
635 auto Op = make_unique<RISCVOperand>(SystemRegister);
636 Op->SysReg.Data = Str.data();
637 Op->SysReg.Length = Str.size();
638 Op->SysReg.Encoding = Encoding;
639 Op->StartLoc = S;
640 Op->IsRV64 = IsRV64;
641 return Op;
642 }
643
Alex Bradbury04f06d92017-08-08 14:43:36 +0000644 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
645 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000646 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000647 RISCVMCExpr::VariantKind VK;
648 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000649
650 if (IsConstant)
651 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000652 else
653 Inst.addOperand(MCOperand::createExpr(Expr));
654 }
655
656 // Used by the TableGen Code
657 void addRegOperands(MCInst &Inst, unsigned N) const {
658 assert(N == 1 && "Invalid number of operands!");
659 Inst.addOperand(MCOperand::createReg(getReg()));
660 }
661
662 void addImmOperands(MCInst &Inst, unsigned N) const {
663 assert(N == 1 && "Invalid number of operands!");
664 addExpr(Inst, getImm());
665 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000666
667 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
668 assert(N == 1 && "Invalid number of operands!");
669 // isFenceArg has validated the operand, meaning this cast is safe
670 auto SE = cast<MCSymbolRefExpr>(getImm());
671
672 unsigned Imm = 0;
673 for (char c : SE->getSymbol().getName()) {
674 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000675 default:
676 llvm_unreachable("FenceArg must contain only [iorw]");
677 case 'i': Imm |= RISCVFenceField::I; break;
678 case 'o': Imm |= RISCVFenceField::O; break;
679 case 'r': Imm |= RISCVFenceField::R; break;
680 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000681 }
682 }
683 Inst.addOperand(MCOperand::createImm(Imm));
684 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000685
Ana Pazos9d6c5532018-10-04 21:50:54 +0000686 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
687 assert(N == 1 && "Invalid number of operands!");
688 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
689 }
690
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000691 // Returns the rounding mode represented by this RISCVOperand. Should only
692 // be called after checking isFRMArg.
693 RISCVFPRndMode::RoundingMode getRoundingMode() const {
694 // isFRMArg has validated the operand, meaning this cast is safe.
695 auto SE = cast<MCSymbolRefExpr>(getImm());
696 RISCVFPRndMode::RoundingMode FRM =
697 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
698 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
699 return FRM;
700 }
701
702 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
703 assert(N == 1 && "Invalid number of operands!");
704 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
705 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000706};
707} // end anonymous namespace.
708
709#define GET_REGISTER_MATCHER
710#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000711#include "RISCVGenAsmMatcher.inc"
712
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000713// Return the matching FPR64 register for the given FPR32.
714// FIXME: Ideally this function could be removed in favour of using
715// information from TableGen.
716unsigned convertFPR32ToFPR64(unsigned Reg) {
717 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000718 default:
719 llvm_unreachable("Not a recognised FPR32 register");
720 case RISCV::F0_32: return RISCV::F0_64;
721 case RISCV::F1_32: return RISCV::F1_64;
722 case RISCV::F2_32: return RISCV::F2_64;
723 case RISCV::F3_32: return RISCV::F3_64;
724 case RISCV::F4_32: return RISCV::F4_64;
725 case RISCV::F5_32: return RISCV::F5_64;
726 case RISCV::F6_32: return RISCV::F6_64;
727 case RISCV::F7_32: return RISCV::F7_64;
728 case RISCV::F8_32: return RISCV::F8_64;
729 case RISCV::F9_32: return RISCV::F9_64;
730 case RISCV::F10_32: return RISCV::F10_64;
731 case RISCV::F11_32: return RISCV::F11_64;
732 case RISCV::F12_32: return RISCV::F12_64;
733 case RISCV::F13_32: return RISCV::F13_64;
734 case RISCV::F14_32: return RISCV::F14_64;
735 case RISCV::F15_32: return RISCV::F15_64;
736 case RISCV::F16_32: return RISCV::F16_64;
737 case RISCV::F17_32: return RISCV::F17_64;
738 case RISCV::F18_32: return RISCV::F18_64;
739 case RISCV::F19_32: return RISCV::F19_64;
740 case RISCV::F20_32: return RISCV::F20_64;
741 case RISCV::F21_32: return RISCV::F21_64;
742 case RISCV::F22_32: return RISCV::F22_64;
743 case RISCV::F23_32: return RISCV::F23_64;
744 case RISCV::F24_32: return RISCV::F24_64;
745 case RISCV::F25_32: return RISCV::F25_64;
746 case RISCV::F26_32: return RISCV::F26_64;
747 case RISCV::F27_32: return RISCV::F27_64;
748 case RISCV::F28_32: return RISCV::F28_64;
749 case RISCV::F29_32: return RISCV::F29_64;
750 case RISCV::F30_32: return RISCV::F30_64;
751 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000752 }
753}
754
755unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
756 unsigned Kind) {
757 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
758 if (!Op.isReg())
759 return Match_InvalidOperand;
760
761 unsigned Reg = Op.getReg();
762 bool IsRegFPR32 =
763 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000764 bool IsRegFPR32C =
765 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000766
767 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000768 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
769 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
770 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000771 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
772 return Match_Success;
773 }
774 return Match_InvalidOperand;
775}
776
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000777bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000778 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000779 Twine Msg = "immediate must be an integer in the range") {
780 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
781 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
782}
783
Alex Bradbury04f06d92017-08-08 14:43:36 +0000784bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
785 OperandVector &Operands,
786 MCStreamer &Out,
787 uint64_t &ErrorInfo,
788 bool MatchingInlineAsm) {
789 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000790
Ana Pazos6b34051b2018-08-30 19:43:19 +0000791 auto Result =
792 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
793 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000794 default:
795 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000796 case Match_Success:
797 return processInstruction(Inst, IDLoc, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000798 case Match_MissingFeature:
799 return Error(IDLoc, "instruction use requires an option to be enabled");
800 case Match_MnemonicFail:
801 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000802 case Match_InvalidOperand: {
803 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000804 if (ErrorInfo != ~0U) {
805 if (ErrorInfo >= Operands.size())
806 return Error(ErrorLoc, "too few operands for instruction");
807
808 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
809 if (ErrorLoc == SMLoc())
810 ErrorLoc = IDLoc;
811 }
812 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000813 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000814 }
815
816 // Handle the case when the error message is of specific type
817 // other than the generic Match_InvalidOperand, and the
818 // corresponding operand is missing.
819 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
820 SMLoc ErrorLoc = IDLoc;
821 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
822 return Error(ErrorLoc, "too few operands for instruction");
823 }
824
825 switch(Result) {
826 default:
827 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000828 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000829 if (isRV64()) {
830 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
831 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
832 }
833 return generateImmOutOfRangeError(Operands, ErrorInfo,
834 std::numeric_limits<int32_t>::min(),
835 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000836 case Match_InvalidUImmLog2XLen:
837 if (isRV64())
838 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
839 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000840 case Match_InvalidUImmLog2XLenNonZero:
841 if (isRV64())
842 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
843 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000844 case Match_InvalidUImm5:
845 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000846 case Match_InvalidSImm6:
847 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
848 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000849 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000850 return generateImmOutOfRangeError(
851 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000852 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000853 case Match_InvalidCLUIImm:
854 return generateImmOutOfRangeError(
855 Operands, ErrorInfo, 1, (1 << 5) - 1,
856 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000857 case Match_InvalidUImm7Lsb00:
858 return generateImmOutOfRangeError(
859 Operands, ErrorInfo, 0, (1 << 7) - 4,
860 "immediate must be a multiple of 4 bytes in the range");
861 case Match_InvalidUImm8Lsb00:
862 return generateImmOutOfRangeError(
863 Operands, ErrorInfo, 0, (1 << 8) - 4,
864 "immediate must be a multiple of 4 bytes in the range");
865 case Match_InvalidUImm8Lsb000:
866 return generateImmOutOfRangeError(
867 Operands, ErrorInfo, 0, (1 << 8) - 8,
868 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000869 case Match_InvalidSImm9Lsb0:
870 return generateImmOutOfRangeError(
871 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
872 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000873 case Match_InvalidUImm9Lsb000:
874 return generateImmOutOfRangeError(
875 Operands, ErrorInfo, 0, (1 << 9) - 8,
876 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000877 case Match_InvalidUImm10Lsb00NonZero:
878 return generateImmOutOfRangeError(
879 Operands, ErrorInfo, 4, (1 << 10) - 4,
880 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000881 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000882 return generateImmOutOfRangeError(
883 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000884 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000885 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000886 return generateImmOutOfRangeError(
887 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000888 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
889 "integer in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000890 case Match_InvalidSImm12Lsb0:
891 return generateImmOutOfRangeError(
892 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
893 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000894 case Match_InvalidSImm13Lsb0:
895 return generateImmOutOfRangeError(
896 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
897 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000898 case Match_InvalidUImm20LUI:
899 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000900 "operand must be a symbol with "
901 "%hi/%tprel_hi modifier or an integer in "
902 "the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000903 case Match_InvalidUImm20AUIPC:
904 return generateImmOutOfRangeError(
905 Operands, ErrorInfo, 0, (1 << 20) - 1,
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000906 "operand must be a symbol with a %pcrel_hi/%got_pcrel_hi modifier "
907 "or an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000908 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000909 return generateImmOutOfRangeError(
910 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
911 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000912 case Match_InvalidCSRSystemRegister: {
913 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
914 "operand must be a valid system register "
915 "name or an integer in the range");
916 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000917 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000918 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000919 return Error(
920 ErrorLoc,
921 "operand must be formed of letters selected in-order from 'iorw'");
922 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000923 case Match_InvalidFRMArg: {
924 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
925 return Error(
926 ErrorLoc,
927 "operand must be a valid floating point rounding mode mnemonic");
928 }
Shiva Chen98f93892018-04-25 14:18:55 +0000929 case Match_InvalidBareSymbol: {
930 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
931 return Error(ErrorLoc, "operand must be a bare symbol name");
932 }
Alex Bradbury44668ae2019-04-01 14:53:17 +0000933 case Match_InvalidCallSymbol: {
934 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
935 return Error(ErrorLoc, "operand must be a bare symbol name");
936 }
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000937 case Match_InvalidTPRelAddSymbol: {
938 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
939 return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
940 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000941 }
942
943 llvm_unreachable("Unknown match type detected!");
944}
945
Alex Bradbury99794792019-03-17 12:02:32 +0000946// Attempts to match Name as a register (either using the default name or
947// alternative ABI names), setting RegNo to the matching register. Upon
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000948// failure, returns true and sets RegNo to 0. If IsRV32E then registers
949// x16-x31 will be rejected.
950static bool matchRegisterNameHelper(bool IsRV32E, unsigned &RegNo,
951 StringRef Name) {
Alex Bradbury99794792019-03-17 12:02:32 +0000952 RegNo = MatchRegisterName(Name);
953 if (RegNo == 0)
954 RegNo = MatchRegisterAltName(Name);
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000955 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
956 RegNo = 0;
Alex Bradbury99794792019-03-17 12:02:32 +0000957 return RegNo == 0;
958}
959
Alex Bradbury04f06d92017-08-08 14:43:36 +0000960bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
961 SMLoc &EndLoc) {
962 const AsmToken &Tok = getParser().getTok();
963 StartLoc = Tok.getLoc();
964 EndLoc = Tok.getEndLoc();
965 RegNo = 0;
966 StringRef Name = getLexer().getTok().getIdentifier();
967
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000968 if (matchRegisterNameHelper(isRV32E(), RegNo, Name))
Alex Bradburyb18e3142019-03-17 12:00:58 +0000969 return Error(StartLoc, "invalid register name");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000970
Alex Bradburyb18e3142019-03-17 12:00:58 +0000971 getParser().Lex(); // Eat identifier token.
972 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000973}
974
Alex Bradbury8c345c52017-11-09 15:00:03 +0000975OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
976 bool AllowParens) {
977 SMLoc FirstS = getLoc();
978 bool HadParens = false;
979 AsmToken Buf[2];
980
981 // If this a parenthesised register name is allowed, parse it atomically
982 if (AllowParens && getLexer().is(AsmToken::LParen)) {
983 size_t ReadCount = getLexer().peekTokens(Buf);
984 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
985 HadParens = true;
986 getParser().Lex(); // Eat '('
987 }
988 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000989
990 switch (getLexer().getKind()) {
991 default:
992 return MatchOperand_NoMatch;
993 case AsmToken::Identifier:
994 StringRef Name = getLexer().getTok().getIdentifier();
Alex Bradbury99794792019-03-17 12:02:32 +0000995 unsigned RegNo;
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000996 matchRegisterNameHelper(isRV32E(), RegNo, Name);
Alex Bradbury99794792019-03-17 12:02:32 +0000997
Alex Bradbury04f06d92017-08-08 14:43:36 +0000998 if (RegNo == 0) {
Alex Bradbury99794792019-03-17 12:02:32 +0000999 if (HadParens)
1000 getLexer().UnLex(Buf[0]);
1001 return MatchOperand_NoMatch;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001002 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001003 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +00001004 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001005 SMLoc S = getLoc();
1006 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001007 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +00001008 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001009 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001010
1011 if (HadParens) {
1012 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001013 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001014 }
1015
Alex Bradbury04f06d92017-08-08 14:43:36 +00001016 return MatchOperand_Success;
1017}
1018
Ana Pazos9d6c5532018-10-04 21:50:54 +00001019OperandMatchResultTy
1020RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1021 SMLoc S = getLoc();
1022 const MCExpr *Res;
1023
1024 switch (getLexer().getKind()) {
1025 default:
1026 return MatchOperand_NoMatch;
1027 case AsmToken::LParen:
1028 case AsmToken::Minus:
1029 case AsmToken::Plus:
1030 case AsmToken::Integer:
1031 case AsmToken::String: {
1032 if (getParser().parseExpression(Res))
1033 return MatchOperand_ParseFail;
1034
1035 auto *CE = dyn_cast<MCConstantExpr>(Res);
1036 if (CE) {
1037 int64_t Imm = CE->getValue();
1038 if (isUInt<12>(Imm)) {
1039 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1040 // Accept an immediate representing a named or un-named Sys Reg
1041 // if the range is valid, regardless of the required features.
1042 Operands.push_back(RISCVOperand::createSysReg(
1043 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1044 return MatchOperand_Success;
1045 }
1046 }
1047
1048 Twine Msg = "immediate must be an integer in the range";
1049 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1050 return MatchOperand_ParseFail;
1051 }
1052 case AsmToken::Identifier: {
1053 StringRef Identifier;
1054 if (getParser().parseIdentifier(Identifier))
1055 return MatchOperand_ParseFail;
1056
1057 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1058 // Accept a named Sys Reg if the required features are present.
1059 if (SysReg) {
1060 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1061 Error(S, "system register use requires an option to be enabled");
1062 return MatchOperand_ParseFail;
1063 }
1064 Operands.push_back(RISCVOperand::createSysReg(
1065 Identifier, S, SysReg->Encoding, isRV64()));
1066 return MatchOperand_Success;
1067 }
1068
1069 Twine Msg = "operand must be a valid system register name "
1070 "or an integer in the range";
1071 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1072 return MatchOperand_ParseFail;
1073 }
1074 case AsmToken::Percent: {
1075 // Discard operand with modifier.
1076 Twine Msg = "immediate must be an integer in the range";
1077 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1078 return MatchOperand_ParseFail;
1079 }
1080 }
1081
1082 return MatchOperand_NoMatch;
1083}
1084
Alex Bradbury04f06d92017-08-08 14:43:36 +00001085OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001086 SMLoc S = getLoc();
1087 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1088 const MCExpr *Res;
1089
Alex Bradbury04f06d92017-08-08 14:43:36 +00001090 switch (getLexer().getKind()) {
1091 default:
1092 return MatchOperand_NoMatch;
1093 case AsmToken::LParen:
1094 case AsmToken::Minus:
1095 case AsmToken::Plus:
1096 case AsmToken::Integer:
1097 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001098 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001099 if (getParser().parseExpression(Res))
1100 return MatchOperand_ParseFail;
1101 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001102 case AsmToken::Percent:
1103 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001104 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001105
Alex Bradburya6e62482017-12-07 10:53:48 +00001106 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001107 return MatchOperand_Success;
1108}
1109
1110OperandMatchResultTy
1111RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1112 SMLoc S = getLoc();
1113 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1114
1115 if (getLexer().getKind() != AsmToken::Percent) {
1116 Error(getLoc(), "expected '%' for operand modifier");
1117 return MatchOperand_ParseFail;
1118 }
1119
1120 getParser().Lex(); // Eat '%'
1121
1122 if (getLexer().getKind() != AsmToken::Identifier) {
1123 Error(getLoc(), "expected valid identifier for operand modifier");
1124 return MatchOperand_ParseFail;
1125 }
1126 StringRef Identifier = getParser().getTok().getIdentifier();
1127 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1128 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1129 Error(getLoc(), "unrecognized operand modifier");
1130 return MatchOperand_ParseFail;
1131 }
1132
1133 getParser().Lex(); // Eat the identifier
1134 if (getLexer().getKind() != AsmToken::LParen) {
1135 Error(getLoc(), "expected '('");
1136 return MatchOperand_ParseFail;
1137 }
1138 getParser().Lex(); // Eat '('
1139
1140 const MCExpr *SubExpr;
1141 if (getParser().parseParenExpression(SubExpr, E)) {
1142 return MatchOperand_ParseFail;
1143 }
1144
1145 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001146 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001147 return MatchOperand_Success;
1148}
1149
Alex Bradbury68f73c12018-09-18 15:18:16 +00001150OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1151 SMLoc S = getLoc();
1152 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1153 const MCExpr *Res;
1154
1155 if (getLexer().getKind() != AsmToken::Identifier)
1156 return MatchOperand_NoMatch;
1157
1158 StringRef Identifier;
Kito Cheng303217e2019-02-20 03:31:32 +00001159 AsmToken Tok = getLexer().getTok();
1160
Alex Bradbury68f73c12018-09-18 15:18:16 +00001161 if (getParser().parseIdentifier(Identifier))
1162 return MatchOperand_ParseFail;
1163
Alex Bradburyf8078f62019-04-02 12:47:20 +00001164 if (Identifier.consume_back("@plt")) {
1165 Error(getLoc(), "'@plt' operand not valid for instruction");
1166 return MatchOperand_ParseFail;
1167 }
1168
Alex Bradbury68f73c12018-09-18 15:18:16 +00001169 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Kito Cheng303217e2019-02-20 03:31:32 +00001170
1171 if (Sym->isVariable()) {
1172 const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1173 if (!isa<MCSymbolRefExpr>(V)) {
1174 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1175 return MatchOperand_NoMatch;
1176 }
1177 Res = V;
1178 } else
1179 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury68f73c12018-09-18 15:18:16 +00001180 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1181 return MatchOperand_Success;
1182}
1183
Alex Bradbury44668ae2019-04-01 14:53:17 +00001184OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1185 SMLoc S = getLoc();
1186 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1187 const MCExpr *Res;
1188
1189 if (getLexer().getKind() != AsmToken::Identifier)
1190 return MatchOperand_NoMatch;
1191
1192 StringRef Identifier;
1193 if (getParser().parseIdentifier(Identifier))
1194 return MatchOperand_ParseFail;
1195
Alex Bradburyf8078f62019-04-02 12:47:20 +00001196 RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1197 if (Identifier.consume_back("@plt"))
1198 Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1199
Alex Bradbury44668ae2019-04-01 14:53:17 +00001200 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1201 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradburyf8078f62019-04-02 12:47:20 +00001202 Res = RISCVMCExpr::create(Res, Kind, getContext());
Alex Bradbury44668ae2019-04-01 14:53:17 +00001203 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1204 return MatchOperand_Success;
1205}
1206
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001207OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1208 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1209 // both being acceptable forms. When parsing `jal ra, foo` this function
1210 // will be called for the `ra` register operand in an attempt to match the
1211 // single-operand alias. parseJALOffset must fail for this case. It would
1212 // seem logical to try parse the operand using parseImmediate and return
1213 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1214 // the second form rather than the first). We can't do this as there's no
1215 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1216 // is an identifier and is followed by a comma.
1217 if (getLexer().is(AsmToken::Identifier) &&
1218 getLexer().peekTok().is(AsmToken::Comma))
1219 return MatchOperand_NoMatch;
1220
1221 return parseImmediate(Operands);
1222}
1223
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001224OperandMatchResultTy
1225RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1226 if (getLexer().isNot(AsmToken::LParen)) {
1227 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001228 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001229 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001230
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001231 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001232 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001233
1234 if (parseRegister(Operands) != MatchOperand_Success) {
1235 Error(getLoc(), "expected register");
1236 return MatchOperand_ParseFail;
1237 }
1238
1239 if (getLexer().isNot(AsmToken::RParen)) {
1240 Error(getLoc(), "expected ')'");
1241 return MatchOperand_ParseFail;
1242 }
1243
1244 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001245 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001246
Alex Bradbury04f06d92017-08-08 14:43:36 +00001247 return MatchOperand_Success;
1248}
1249
Alex Bradburycd8688a2018-04-25 17:25:29 +00001250/// Looks at a token type and creates the relevant operand from this
1251/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001252/// true.
1253bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1254 // Check if the current operand has a custom associated parser, if so, try to
1255 // custom parse the operand, or fallback to the general approach.
1256 OperandMatchResultTy Result =
1257 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1258 if (Result == MatchOperand_Success)
1259 return false;
1260 if (Result == MatchOperand_ParseFail)
1261 return true;
1262
1263 // Attempt to parse token as a register.
1264 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001265 return false;
1266
1267 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001268 if (parseImmediate(Operands) == MatchOperand_Success) {
1269 // Parse memory base register if present
1270 if (getLexer().is(AsmToken::LParen))
1271 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001272 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001273 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001274
1275 // Finally we have exhausted all options and must declare defeat.
1276 Error(getLoc(), "unknown operand");
1277 return true;
1278}
1279
1280bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1281 StringRef Name, SMLoc NameLoc,
1282 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001283 // Ensure that if the instruction occurs when relaxation is enabled,
1284 // relocations are forced for the file. Ideally this would be done when there
1285 // is enough information to reliably determine if the instruction itself may
1286 // cause relaxations. Unfortunately instruction processing stage occurs in the
1287 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1288 // for the entire file.
1289 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1290 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1291 if (Assembler != nullptr) {
1292 RISCVAsmBackend &MAB =
1293 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1294 MAB.setForceRelocs();
1295 }
1296 }
1297
Alex Bradbury04f06d92017-08-08 14:43:36 +00001298 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001299 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001300
1301 // If there are no more operands, then finish
1302 if (getLexer().is(AsmToken::EndOfStatement))
1303 return false;
1304
1305 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001306 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001307 return true;
1308
1309 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001310 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001311 while (getLexer().is(AsmToken::Comma)) {
1312 // Consume comma token
1313 getLexer().Lex();
1314
1315 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001316 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001317 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001318
1319 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001320 }
1321
1322 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1323 SMLoc Loc = getLexer().getLoc();
1324 getParser().eatToEndOfStatement();
1325 return Error(Loc, "unexpected token");
1326 }
1327
1328 getParser().Lex(); // Consume the EndOfStatement.
1329 return false;
1330}
1331
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001332bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1333 RISCVMCExpr::VariantKind &Kind,
1334 int64_t &Addend) {
1335 Kind = RISCVMCExpr::VK_RISCV_None;
1336 Addend = 0;
1337
1338 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1339 Kind = RE->getKind();
1340 Expr = RE->getSubExpr();
1341 }
1342
1343 // It's a simple symbol reference or constant with no addend.
1344 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1345 return true;
1346
1347 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1348 if (!BE)
1349 return false;
1350
1351 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1352 return false;
1353
1354 if (BE->getOpcode() != MCBinaryExpr::Add &&
1355 BE->getOpcode() != MCBinaryExpr::Sub)
1356 return false;
1357
1358 // We are able to support the subtraction of two symbol references
1359 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1360 isa<MCSymbolRefExpr>(BE->getRHS()))
1361 return true;
1362
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001363 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001364 // on here than we can deal with.
1365 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1366 if (!AddendExpr)
1367 return false;
1368
1369 Addend = AddendExpr->getValue();
1370 if (BE->getOpcode() == MCBinaryExpr::Sub)
1371 Addend = -Addend;
1372
1373 // It's some symbol reference + a constant addend
1374 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1375}
1376
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001377bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1378 // This returns false if this function recognizes the directive
1379 // regardless of whether it is successfully handles or reports an
1380 // error. Otherwise it returns true to give the generic parser a
1381 // chance at recognizing it.
1382 StringRef IDVal = DirectiveID.getString();
1383
1384 if (IDVal == ".option")
1385 return parseDirectiveOption();
1386
1387 return true;
1388}
1389
1390bool RISCVAsmParser::parseDirectiveOption() {
1391 MCAsmParser &Parser = getParser();
1392 // Get the option token.
1393 AsmToken Tok = Parser.getTok();
1394 // At the moment only identifiers are supported.
1395 if (Tok.isNot(AsmToken::Identifier))
1396 return Error(Parser.getTok().getLoc(),
1397 "unexpected token, expected identifier");
1398
1399 StringRef Option = Tok.getIdentifier();
1400
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001401 if (Option == "push") {
1402 getTargetStreamer().emitDirectiveOptionPush();
1403
1404 Parser.Lex();
1405 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1406 return Error(Parser.getTok().getLoc(),
1407 "unexpected token, expected end of statement");
1408
1409 pushFeatureBits();
1410 return false;
1411 }
1412
1413 if (Option == "pop") {
1414 SMLoc StartLoc = Parser.getTok().getLoc();
1415 getTargetStreamer().emitDirectiveOptionPop();
1416
1417 Parser.Lex();
1418 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1419 return Error(Parser.getTok().getLoc(),
1420 "unexpected token, expected end of statement");
1421
1422 if (popFeatureBits())
1423 return Error(StartLoc, ".option pop with no .option push");
1424
1425 return false;
1426 }
1427
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001428 if (Option == "rvc") {
1429 getTargetStreamer().emitDirectiveOptionRVC();
1430
1431 Parser.Lex();
1432 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1433 return Error(Parser.getTok().getLoc(),
1434 "unexpected token, expected end of statement");
1435
1436 setFeatureBits(RISCV::FeatureStdExtC, "c");
1437 return false;
1438 }
1439
1440 if (Option == "norvc") {
1441 getTargetStreamer().emitDirectiveOptionNoRVC();
1442
1443 Parser.Lex();
1444 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1445 return Error(Parser.getTok().getLoc(),
1446 "unexpected token, expected end of statement");
1447
1448 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1449 return false;
1450 }
1451
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001452 if (Option == "relax") {
1453 getTargetStreamer().emitDirectiveOptionRelax();
1454
1455 Parser.Lex();
1456 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1457 return Error(Parser.getTok().getLoc(),
1458 "unexpected token, expected end of statement");
1459
1460 setFeatureBits(RISCV::FeatureRelax, "relax");
1461 return false;
1462 }
1463
1464 if (Option == "norelax") {
1465 getTargetStreamer().emitDirectiveOptionNoRelax();
1466
1467 Parser.Lex();
1468 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1469 return Error(Parser.getTok().getLoc(),
1470 "unexpected token, expected end of statement");
1471
1472 clearFeatureBits(RISCV::FeatureRelax, "relax");
1473 return false;
1474 }
1475
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001476 // Unknown option.
1477 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001478 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1479 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001480 Parser.eatToEndOfStatement();
1481 return false;
1482}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001483
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001484void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1485 MCInst CInst;
1486 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1487 CInst.setLoc(Inst.getLoc());
1488 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1489}
1490
1491void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1492 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001493 RISCVMatInt::InstSeq Seq;
1494 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001495
Alex Bradbury22c091f2018-11-15 10:11:31 +00001496 unsigned SrcReg = RISCV::X0;
1497 for (RISCVMatInt::Inst &Inst : Seq) {
1498 if (Inst.Opc == RISCV::LUI) {
1499 emitToStreamer(
1500 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1501 } else {
1502 emitToStreamer(
1503 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1504 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001505 }
1506
Alex Bradbury22c091f2018-11-15 10:11:31 +00001507 // Only the first instruction has X0 as its source.
1508 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001509 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001510}
1511
Alex Bradbury22531c42019-02-15 09:53:32 +00001512void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
1513 const MCExpr *Symbol,
1514 RISCVMCExpr::VariantKind VKHi,
1515 unsigned SecondOpcode, SMLoc IDLoc,
1516 MCStreamer &Out) {
1517 // A pair of instructions for PC-relative addressing; expands to
1518 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
1519 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001520 MCContext &Ctx = getContext();
1521
1522 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1523 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1524 Out.EmitLabel(TmpLabel);
1525
Alex Bradbury22531c42019-02-15 09:53:32 +00001526 const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001527 emitToStreamer(
Alex Bradbury22531c42019-02-15 09:53:32 +00001528 Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001529
1530 const MCExpr *RefToLinkTmpLabel =
1531 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1532 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1533
Alex Bradbury22531c42019-02-15 09:53:32 +00001534 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001535 .addOperand(DestReg)
Alex Bradbury22531c42019-02-15 09:53:32 +00001536 .addOperand(TmpReg)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001537 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001538}
1539
Alex Bradbury22531c42019-02-15 09:53:32 +00001540void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1541 MCStreamer &Out) {
1542 // The load local address pseudo-instruction "lla" is used in PC-relative
1543 // addressing of local symbols:
1544 // lla rdest, symbol
1545 // expands to
1546 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1547 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1548 MCOperand DestReg = Inst.getOperand(0);
1549 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1550 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1551 RISCV::ADDI, IDLoc, Out);
1552}
1553
1554void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
1555 MCStreamer &Out) {
1556 // The load address pseudo-instruction "la" is used in PC-relative and
1557 // GOT-indirect addressing of global symbols:
1558 // la rdest, symbol
1559 // expands to either (for non-PIC)
1560 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1561 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1562 // or (for PIC)
1563 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
1564 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1565 MCOperand DestReg = Inst.getOperand(0);
1566 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1567 unsigned SecondOpcode;
1568 RISCVMCExpr::VariantKind VKHi;
1569 // FIXME: Should check .option (no)pic when implemented
1570 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1571 SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1572 VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
1573 } else {
1574 SecondOpcode = RISCV::ADDI;
1575 VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
1576 }
1577 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1578}
1579
Kito Cheng303217e2019-02-20 03:31:32 +00001580void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
1581 SMLoc IDLoc, MCStreamer &Out,
1582 bool HasTmpReg) {
1583 // The load/store pseudo-instruction does a pc-relative load with
1584 // a symbol.
1585 //
1586 // The expansion looks like this
1587 //
1588 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
1589 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
1590 MCOperand DestReg = Inst.getOperand(0);
1591 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1592 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1593 MCOperand TmpReg = Inst.getOperand(TmpRegOpIdx);
1594 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
1595 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1596 Opcode, IDLoc, Out);
1597}
1598
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001599bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1600 MCStreamer &Out) {
1601 Inst.setLoc(IDLoc);
1602
Alex Bradbury22531c42019-02-15 09:53:32 +00001603 switch (Inst.getOpcode()) {
1604 default:
1605 break;
1606 case RISCV::PseudoLI: {
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001607 unsigned Reg = Inst.getOperand(0).getReg();
1608 const MCOperand &Op1 = Inst.getOperand(1);
1609 if (Op1.isExpr()) {
1610 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1611 // Just convert to an addi. This allows compatibility with gas.
1612 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1613 .addReg(Reg)
1614 .addReg(RISCV::X0)
1615 .addExpr(Op1.getExpr()));
1616 return false;
1617 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001618 int64_t Imm = Inst.getOperand(1).getImm();
1619 // On RV32 the immediate here can either be a signed or an unsigned
1620 // 32-bit number. Sign extension has to be performed to ensure that Imm
1621 // represents the expected signed 64-bit number.
1622 if (!isRV64())
1623 Imm = SignExtend64<32>(Imm);
1624 emitLoadImm(Reg, Imm, Out);
1625 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001626 }
1627 case RISCV::PseudoLLA:
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001628 emitLoadLocalAddress(Inst, IDLoc, Out);
1629 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001630 case RISCV::PseudoLA:
1631 emitLoadAddress(Inst, IDLoc, Out);
1632 return false;
Kito Cheng303217e2019-02-20 03:31:32 +00001633 case RISCV::PseudoLB:
1634 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
1635 return false;
1636 case RISCV::PseudoLBU:
1637 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
1638 return false;
1639 case RISCV::PseudoLH:
1640 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
1641 return false;
1642 case RISCV::PseudoLHU:
1643 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
1644 return false;
1645 case RISCV::PseudoLW:
1646 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
1647 return false;
1648 case RISCV::PseudoLWU:
1649 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
1650 return false;
1651 case RISCV::PseudoLD:
1652 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
1653 return false;
1654 case RISCV::PseudoFLW:
1655 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
1656 return false;
1657 case RISCV::PseudoFLD:
1658 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
1659 return false;
1660 case RISCV::PseudoSB:
1661 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
1662 return false;
1663 case RISCV::PseudoSH:
1664 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
1665 return false;
1666 case RISCV::PseudoSW:
1667 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
1668 return false;
1669 case RISCV::PseudoSD:
1670 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
1671 return false;
1672 case RISCV::PseudoFSW:
1673 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
1674 return false;
1675 case RISCV::PseudoFSD:
1676 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
1677 return false;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001678 }
1679
1680 emitToStreamer(Out, Inst);
1681 return false;
1682}
1683
Alex Bradbury04f06d92017-08-08 14:43:36 +00001684extern "C" void LLVMInitializeRISCVAsmParser() {
1685 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1686 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1687}