blob: a7580c52d6f890174c734130d6f3d2c1210042fc [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
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +000099 // Checks that a PseudoAddTPRel is using x4/tp in its second input operand.
100 // Enforcing this using a restricted register class for the second input
101 // operand of PseudoAddTPRel results in a poor diagnostic due to the fact
102 // 'add' is an overloaded mnemonic.
103 bool checkPseudoAddTPRel(MCInst &Inst, OperandVector &Operands);
104
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000105 /// Helper for processing MC instructions that have been successfully matched
106 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
107 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
108 /// in this method.
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000109 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
110 MCStreamer &Out);
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000111
Alex Bradbury04f06d92017-08-08 14:43:36 +0000112// Auto-generated instruction matching functions
113#define GET_ASSEMBLER_HEADER
114#include "RISCVGenAsmMatcher.inc"
115
Ana Pazos9d6c5532018-10-04 21:50:54 +0000116 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000117 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000118 OperandMatchResultTy parseRegister(OperandVector &Operands,
119 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000120 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000121 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +0000122 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000123 OperandMatchResultTy parseCallSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000124 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000125
Alex Bradbury68f73c12018-09-18 15:18:16 +0000126 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000127
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000128 bool parseDirectiveOption();
129
130 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
131 if (!(getSTI().getFeatureBits()[Feature])) {
132 MCSubtargetInfo &STI = copySTI();
133 setAvailableFeatures(
134 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
135 }
136 }
137
138 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
139 if (getSTI().getFeatureBits()[Feature]) {
140 MCSubtargetInfo &STI = copySTI();
141 setAvailableFeatures(
142 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
143 }
144 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000145
Alex Bradbury893e5bc2018-11-28 16:39:14 +0000146 void pushFeatureBits() {
147 FeatureBitStack.push_back(getSTI().getFeatureBits());
148 }
149
150 bool popFeatureBits() {
151 if (FeatureBitStack.empty())
152 return true;
153
154 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
155 copySTI().setFeatureBits(FeatureBits);
156 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
157
158 return false;
159 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000160public:
161 enum RISCVMatchResultTy {
162 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
163#define GET_OPERAND_DIAGNOSTIC_TYPES
164#include "RISCVGenAsmMatcher.inc"
165#undef GET_OPERAND_DIAGNOSTIC_TYPES
166 };
167
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000168 static bool classifySymbolRef(const MCExpr *Expr,
169 RISCVMCExpr::VariantKind &Kind,
170 int64_t &Addend);
171
Alex Bradbury04f06d92017-08-08 14:43:36 +0000172 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
173 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000174 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000175 Parser.addAliasForDirective(".half", ".2byte");
176 Parser.addAliasForDirective(".hword", ".2byte");
177 Parser.addAliasForDirective(".word", ".4byte");
178 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000179 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
180 }
181};
182
183/// RISCVOperand - Instances of this class represent a parsed machine
184/// instruction
185struct RISCVOperand : public MCParsedAsmOperand {
186
187 enum KindTy {
188 Token,
189 Register,
190 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000191 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000192 } Kind;
193
Alex Bradburya6e62482017-12-07 10:53:48 +0000194 bool IsRV64;
195
Alex Bradbury04f06d92017-08-08 14:43:36 +0000196 struct RegOp {
197 unsigned RegNum;
198 };
199
200 struct ImmOp {
201 const MCExpr *Val;
202 };
203
Ana Pazos9d6c5532018-10-04 21:50:54 +0000204 struct SysRegOp {
205 const char *Data;
206 unsigned Length;
207 unsigned Encoding;
208 // FIXME: Add the Encoding parsed fields as needed for checks,
209 // e.g.: read/write or user/supervisor/machine privileges.
210 };
211
Alex Bradbury04f06d92017-08-08 14:43:36 +0000212 SMLoc StartLoc, EndLoc;
213 union {
214 StringRef Tok;
215 RegOp Reg;
216 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000217 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000218 };
219
220 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
221
222public:
223 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
224 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000225 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000226 StartLoc = o.StartLoc;
227 EndLoc = o.EndLoc;
228 switch (Kind) {
229 case Register:
230 Reg = o.Reg;
231 break;
232 case Immediate:
233 Imm = o.Imm;
234 break;
235 case Token:
236 Tok = o.Tok;
237 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000238 case SystemRegister:
239 SysReg = o.SysReg;
240 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000241 }
242 }
243
244 bool isToken() const override { return Kind == Token; }
245 bool isReg() const override { return Kind == Register; }
246 bool isImm() const override { return Kind == Immediate; }
247 bool isMem() const override { return false; }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000248 bool isSystemRegister() const { return Kind == SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000249
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000250 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
251 RISCVMCExpr::VariantKind &VK) {
252 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000253 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000254 return RE->evaluateAsConstant(Imm);
255 }
256
257 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000258 VK = RISCVMCExpr::VK_RISCV_None;
259 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000260 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000261 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000262
263 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000264 }
265
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000266 // True if operand is a symbol with no modifiers, or a constant with no
267 // modifiers and isShiftedInt<N-1, 1>(Op).
268 template <int N> bool isBareSimmNLsb0() const {
269 int64_t Imm;
270 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000271 if (!isImm())
272 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000273 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000274 bool IsValid;
275 if (!IsConstantImm)
276 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
277 else
278 IsValid = isShiftedInt<N - 1, 1>(Imm);
279 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000280 }
281
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000282 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
283
Shiva Chen98f93892018-04-25 14:18:55 +0000284 bool isBareSymbol() const {
285 int64_t Imm;
286 RISCVMCExpr::VariantKind VK;
287 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000288 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000289 return false;
290 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
291 VK == RISCVMCExpr::VK_RISCV_None;
292 }
293
Alex Bradbury44668ae2019-04-01 14:53:17 +0000294 bool isCallSymbol() const {
295 int64_t Imm;
296 RISCVMCExpr::VariantKind VK;
297 // Must be of 'immediate' type but not a constant.
298 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
299 return false;
300 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
Alex Bradburyf8078f62019-04-02 12:47:20 +0000301 (VK == RISCVMCExpr::VK_RISCV_CALL ||
302 VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000303 }
304
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000305 bool isTPRelAddSymbol() const {
306 int64_t Imm;
307 RISCVMCExpr::VariantKind VK;
308 // Must be of 'immediate' type but not a constant.
309 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
310 return false;
311 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
312 VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
313 }
314
Ana Pazos9d6c5532018-10-04 21:50:54 +0000315 bool isCSRSystemRegister() const { return isSystemRegister(); }
316
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000317 /// Return true if the operand is a valid for the fence instruction e.g.
318 /// ('iorw').
319 bool isFenceArg() const {
320 if (!isImm())
321 return false;
322 const MCExpr *Val = getImm();
323 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
324 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
325 return false;
326
327 StringRef Str = SVal->getSymbol().getName();
328 // Letters must be unique, taken from 'iorw', and in ascending order. This
329 // holds as long as each individual character is one of 'iorw' and is
330 // greater than the previous character.
331 char Prev = '\0';
332 for (char c : Str) {
333 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
334 return false;
335 if (c <= Prev)
336 return false;
337 Prev = c;
338 }
339 return true;
340 }
341
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000342 /// Return true if the operand is a valid floating point rounding mode.
343 bool isFRMArg() const {
344 if (!isImm())
345 return false;
346 const MCExpr *Val = getImm();
347 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
348 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
349 return false;
350
351 StringRef Str = SVal->getSymbol().getName();
352
353 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
354 }
355
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000356 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000357 int64_t Imm;
358 RISCVMCExpr::VariantKind VK;
359 if (!isImm())
360 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000361 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000362 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
363 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000364 // Given only Imm, ensuring that the actually specified constant is either
365 // a signed or unsigned 64-bit number is unfortunately impossible.
366 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
367 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
368 }
369
Alex Bradburya6e62482017-12-07 10:53:48 +0000370 bool isUImmLog2XLen() const {
371 int64_t Imm;
372 RISCVMCExpr::VariantKind VK;
373 if (!isImm())
374 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000375 if (!evaluateConstantImm(getImm(), Imm, VK) ||
376 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000377 return false;
378 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
379 }
380
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000381 bool isUImmLog2XLenNonZero() const {
382 int64_t Imm;
383 RISCVMCExpr::VariantKind VK;
384 if (!isImm())
385 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000386 if (!evaluateConstantImm(getImm(), Imm, VK) ||
387 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000388 return false;
389 if (Imm == 0)
390 return false;
391 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
392 }
393
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000394 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000395 int64_t Imm;
396 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000397 if (!isImm())
398 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000399 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000400 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000401 }
402
Alex Bradbury60714f92017-12-13 09:32:55 +0000403 bool isUImm5NonZero() const {
404 int64_t Imm;
405 RISCVMCExpr::VariantKind VK;
406 if (!isImm())
407 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000408 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000409 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
410 VK == RISCVMCExpr::VK_RISCV_None;
411 }
412
Alex Bradbury581d6b02017-12-13 09:41:21 +0000413 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000414 if (!isImm())
415 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000416 RISCVMCExpr::VariantKind VK;
417 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000418 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000419 return IsConstantImm && isInt<6>(Imm) &&
420 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000421 }
422
Shiva Chenb22c1d22018-02-02 02:43:23 +0000423 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000424 if (!isImm())
425 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000426 RISCVMCExpr::VariantKind VK;
427 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000428 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000429 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
430 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000431 }
432
Shiva Chen7c172422018-02-22 15:02:28 +0000433 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000434 if (!isImm())
435 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000436 int64_t Imm;
437 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000438 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000439 return IsConstantImm && (Imm != 0) &&
440 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000441 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000442 }
443
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000444 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000445 if (!isImm())
446 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000447 int64_t Imm;
448 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000449 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000450 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
451 VK == RISCVMCExpr::VK_RISCV_None;
452 }
453
454 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000455 if (!isImm())
456 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000457 int64_t Imm;
458 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000459 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000460 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
461 VK == RISCVMCExpr::VK_RISCV_None;
462 }
463
464 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000465 if (!isImm())
466 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000467 int64_t Imm;
468 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000469 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000470 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
471 VK == RISCVMCExpr::VK_RISCV_None;
472 }
473
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000474 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
475
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000476 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000477 if (!isImm())
478 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000479 int64_t Imm;
480 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000481 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000482 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
483 VK == RISCVMCExpr::VK_RISCV_None;
484 }
485
Alex Bradbury60714f92017-12-13 09:32:55 +0000486 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000487 if (!isImm())
488 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000489 int64_t Imm;
490 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000491 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000492 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
493 VK == RISCVMCExpr::VK_RISCV_None;
494 }
495
Alex Bradbury04f06d92017-08-08 14:43:36 +0000496 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000497 RISCVMCExpr::VariantKind VK;
498 int64_t Imm;
499 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000500 if (!isImm())
501 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000502 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000503 if (!IsConstantImm)
504 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
505 else
506 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000507 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000508 VK == RISCVMCExpr::VK_RISCV_LO ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000509 VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
510 VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000511 }
512
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000513 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
514
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000515 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000516
Shiva Chenb22c1d22018-02-02 02:43:23 +0000517 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000518 if (!isImm())
519 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000520 int64_t Imm;
521 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000522 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000523 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000524 VK == RISCVMCExpr::VK_RISCV_None;
525 }
526
Alex Bradbury74340f12018-09-18 15:08:35 +0000527 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000528 RISCVMCExpr::VariantKind VK;
529 int64_t Imm;
530 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000531 if (!isImm())
532 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000533 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000534 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000535 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000536 return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
537 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000538 } else {
539 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000540 VK == RISCVMCExpr::VK_RISCV_HI ||
541 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000542 }
543 }
544
545 bool isUImm20AUIPC() const {
546 RISCVMCExpr::VariantKind VK;
547 int64_t Imm;
548 bool IsValid;
549 if (!isImm())
550 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000551 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000552 if (!IsConstantImm) {
553 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000554 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
555 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000556 } else {
557 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000558 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
559 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000560 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000561 }
562
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000563 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000564
Alex Bradbury04f06d92017-08-08 14:43:36 +0000565 /// getStartLoc - Gets location of the first token of this operand
566 SMLoc getStartLoc() const override { return StartLoc; }
567 /// getEndLoc - Gets location of the last token of this operand
568 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000569 /// True if this operand is for an RV64 instruction
570 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000571
572 unsigned getReg() const override {
573 assert(Kind == Register && "Invalid type access!");
574 return Reg.RegNum;
575 }
576
Ana Pazos9d6c5532018-10-04 21:50:54 +0000577 StringRef getSysReg() const {
578 assert(Kind == SystemRegister && "Invalid access!");
579 return StringRef(SysReg.Data, SysReg.Length);
580 }
581
Alex Bradbury04f06d92017-08-08 14:43:36 +0000582 const MCExpr *getImm() const {
583 assert(Kind == Immediate && "Invalid type access!");
584 return Imm.Val;
585 }
586
587 StringRef getToken() const {
588 assert(Kind == Token && "Invalid type access!");
589 return Tok;
590 }
591
592 void print(raw_ostream &OS) const override {
593 switch (Kind) {
594 case Immediate:
595 OS << *getImm();
596 break;
597 case Register:
598 OS << "<register x";
599 OS << getReg() << ">";
600 break;
601 case Token:
602 OS << "'" << getToken() << "'";
603 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000604 case SystemRegister:
605 OS << "<sysreg: " << getSysReg() << '>';
606 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000607 }
608 }
609
Alex Bradburya6e62482017-12-07 10:53:48 +0000610 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
611 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000612 auto Op = make_unique<RISCVOperand>(Token);
613 Op->Tok = Str;
614 Op->StartLoc = S;
615 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000616 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000617 return Op;
618 }
619
620 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000621 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000622 auto Op = make_unique<RISCVOperand>(Register);
623 Op->Reg.RegNum = RegNo;
624 Op->StartLoc = S;
625 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000626 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000627 return Op;
628 }
629
630 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000631 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000632 auto Op = make_unique<RISCVOperand>(Immediate);
633 Op->Imm.Val = Val;
634 Op->StartLoc = S;
635 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000636 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000637 return Op;
638 }
639
Ana Pazos9d6c5532018-10-04 21:50:54 +0000640 static std::unique_ptr<RISCVOperand>
641 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
642 auto Op = make_unique<RISCVOperand>(SystemRegister);
643 Op->SysReg.Data = Str.data();
644 Op->SysReg.Length = Str.size();
645 Op->SysReg.Encoding = Encoding;
646 Op->StartLoc = S;
647 Op->IsRV64 = IsRV64;
648 return Op;
649 }
650
Alex Bradbury04f06d92017-08-08 14:43:36 +0000651 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
652 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000653 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000654 RISCVMCExpr::VariantKind VK;
655 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000656
657 if (IsConstant)
658 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000659 else
660 Inst.addOperand(MCOperand::createExpr(Expr));
661 }
662
663 // Used by the TableGen Code
664 void addRegOperands(MCInst &Inst, unsigned N) const {
665 assert(N == 1 && "Invalid number of operands!");
666 Inst.addOperand(MCOperand::createReg(getReg()));
667 }
668
669 void addImmOperands(MCInst &Inst, unsigned N) const {
670 assert(N == 1 && "Invalid number of operands!");
671 addExpr(Inst, getImm());
672 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000673
674 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
675 assert(N == 1 && "Invalid number of operands!");
676 // isFenceArg has validated the operand, meaning this cast is safe
677 auto SE = cast<MCSymbolRefExpr>(getImm());
678
679 unsigned Imm = 0;
680 for (char c : SE->getSymbol().getName()) {
681 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000682 default:
683 llvm_unreachable("FenceArg must contain only [iorw]");
684 case 'i': Imm |= RISCVFenceField::I; break;
685 case 'o': Imm |= RISCVFenceField::O; break;
686 case 'r': Imm |= RISCVFenceField::R; break;
687 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000688 }
689 }
690 Inst.addOperand(MCOperand::createImm(Imm));
691 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000692
Ana Pazos9d6c5532018-10-04 21:50:54 +0000693 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
694 assert(N == 1 && "Invalid number of operands!");
695 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
696 }
697
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000698 // Returns the rounding mode represented by this RISCVOperand. Should only
699 // be called after checking isFRMArg.
700 RISCVFPRndMode::RoundingMode getRoundingMode() const {
701 // isFRMArg has validated the operand, meaning this cast is safe.
702 auto SE = cast<MCSymbolRefExpr>(getImm());
703 RISCVFPRndMode::RoundingMode FRM =
704 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
705 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
706 return FRM;
707 }
708
709 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
710 assert(N == 1 && "Invalid number of operands!");
711 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
712 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000713};
714} // end anonymous namespace.
715
716#define GET_REGISTER_MATCHER
717#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000718#include "RISCVGenAsmMatcher.inc"
719
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000720// Return the matching FPR64 register for the given FPR32.
721// FIXME: Ideally this function could be removed in favour of using
722// information from TableGen.
723unsigned convertFPR32ToFPR64(unsigned Reg) {
724 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000725 default:
726 llvm_unreachable("Not a recognised FPR32 register");
727 case RISCV::F0_32: return RISCV::F0_64;
728 case RISCV::F1_32: return RISCV::F1_64;
729 case RISCV::F2_32: return RISCV::F2_64;
730 case RISCV::F3_32: return RISCV::F3_64;
731 case RISCV::F4_32: return RISCV::F4_64;
732 case RISCV::F5_32: return RISCV::F5_64;
733 case RISCV::F6_32: return RISCV::F6_64;
734 case RISCV::F7_32: return RISCV::F7_64;
735 case RISCV::F8_32: return RISCV::F8_64;
736 case RISCV::F9_32: return RISCV::F9_64;
737 case RISCV::F10_32: return RISCV::F10_64;
738 case RISCV::F11_32: return RISCV::F11_64;
739 case RISCV::F12_32: return RISCV::F12_64;
740 case RISCV::F13_32: return RISCV::F13_64;
741 case RISCV::F14_32: return RISCV::F14_64;
742 case RISCV::F15_32: return RISCV::F15_64;
743 case RISCV::F16_32: return RISCV::F16_64;
744 case RISCV::F17_32: return RISCV::F17_64;
745 case RISCV::F18_32: return RISCV::F18_64;
746 case RISCV::F19_32: return RISCV::F19_64;
747 case RISCV::F20_32: return RISCV::F20_64;
748 case RISCV::F21_32: return RISCV::F21_64;
749 case RISCV::F22_32: return RISCV::F22_64;
750 case RISCV::F23_32: return RISCV::F23_64;
751 case RISCV::F24_32: return RISCV::F24_64;
752 case RISCV::F25_32: return RISCV::F25_64;
753 case RISCV::F26_32: return RISCV::F26_64;
754 case RISCV::F27_32: return RISCV::F27_64;
755 case RISCV::F28_32: return RISCV::F28_64;
756 case RISCV::F29_32: return RISCV::F29_64;
757 case RISCV::F30_32: return RISCV::F30_64;
758 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000759 }
760}
761
762unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
763 unsigned Kind) {
764 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
765 if (!Op.isReg())
766 return Match_InvalidOperand;
767
768 unsigned Reg = Op.getReg();
769 bool IsRegFPR32 =
770 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000771 bool IsRegFPR32C =
772 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000773
774 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000775 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
776 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
777 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000778 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
779 return Match_Success;
780 }
781 return Match_InvalidOperand;
782}
783
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000784bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000785 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000786 Twine Msg = "immediate must be an integer in the range") {
787 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
788 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
789}
790
Alex Bradbury04f06d92017-08-08 14:43:36 +0000791bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
792 OperandVector &Operands,
793 MCStreamer &Out,
794 uint64_t &ErrorInfo,
795 bool MatchingInlineAsm) {
796 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000797
Ana Pazos6b34051b2018-08-30 19:43:19 +0000798 auto Result =
799 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
800 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000801 default:
802 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000803 case Match_Success:
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000804 return processInstruction(Inst, IDLoc, Operands, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000805 case Match_MissingFeature:
806 return Error(IDLoc, "instruction use requires an option to be enabled");
807 case Match_MnemonicFail:
808 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000809 case Match_InvalidOperand: {
810 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000811 if (ErrorInfo != ~0U) {
812 if (ErrorInfo >= Operands.size())
813 return Error(ErrorLoc, "too few operands for instruction");
814
815 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
816 if (ErrorLoc == SMLoc())
817 ErrorLoc = IDLoc;
818 }
819 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000820 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000821 }
822
823 // Handle the case when the error message is of specific type
824 // other than the generic Match_InvalidOperand, and the
825 // corresponding operand is missing.
826 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
827 SMLoc ErrorLoc = IDLoc;
828 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
829 return Error(ErrorLoc, "too few operands for instruction");
830 }
831
832 switch(Result) {
833 default:
834 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000835 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000836 if (isRV64()) {
837 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
838 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
839 }
840 return generateImmOutOfRangeError(Operands, ErrorInfo,
841 std::numeric_limits<int32_t>::min(),
842 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000843 case Match_InvalidUImmLog2XLen:
844 if (isRV64())
845 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
846 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000847 case Match_InvalidUImmLog2XLenNonZero:
848 if (isRV64())
849 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
850 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000851 case Match_InvalidUImm5:
852 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000853 case Match_InvalidSImm6:
854 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
855 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000856 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000857 return generateImmOutOfRangeError(
858 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000859 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000860 case Match_InvalidCLUIImm:
861 return generateImmOutOfRangeError(
862 Operands, ErrorInfo, 1, (1 << 5) - 1,
863 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000864 case Match_InvalidUImm7Lsb00:
865 return generateImmOutOfRangeError(
866 Operands, ErrorInfo, 0, (1 << 7) - 4,
867 "immediate must be a multiple of 4 bytes in the range");
868 case Match_InvalidUImm8Lsb00:
869 return generateImmOutOfRangeError(
870 Operands, ErrorInfo, 0, (1 << 8) - 4,
871 "immediate must be a multiple of 4 bytes in the range");
872 case Match_InvalidUImm8Lsb000:
873 return generateImmOutOfRangeError(
874 Operands, ErrorInfo, 0, (1 << 8) - 8,
875 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000876 case Match_InvalidSImm9Lsb0:
877 return generateImmOutOfRangeError(
878 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
879 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000880 case Match_InvalidUImm9Lsb000:
881 return generateImmOutOfRangeError(
882 Operands, ErrorInfo, 0, (1 << 9) - 8,
883 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000884 case Match_InvalidUImm10Lsb00NonZero:
885 return generateImmOutOfRangeError(
886 Operands, ErrorInfo, 4, (1 << 10) - 4,
887 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000888 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000889 return generateImmOutOfRangeError(
890 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000891 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000892 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000893 return generateImmOutOfRangeError(
894 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000895 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
896 "integer in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000897 case Match_InvalidSImm12Lsb0:
898 return generateImmOutOfRangeError(
899 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
900 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000901 case Match_InvalidSImm13Lsb0:
902 return generateImmOutOfRangeError(
903 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
904 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000905 case Match_InvalidUImm20LUI:
906 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000907 "operand must be a symbol with "
908 "%hi/%tprel_hi modifier or an integer in "
909 "the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000910 case Match_InvalidUImm20AUIPC:
911 return generateImmOutOfRangeError(
912 Operands, ErrorInfo, 0, (1 << 20) - 1,
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000913 "operand must be a symbol with a %pcrel_hi/%got_pcrel_hi modifier "
914 "or an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000915 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000916 return generateImmOutOfRangeError(
917 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
918 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000919 case Match_InvalidCSRSystemRegister: {
920 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
921 "operand must be a valid system register "
922 "name or an integer in the range");
923 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000924 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000925 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000926 return Error(
927 ErrorLoc,
928 "operand must be formed of letters selected in-order from 'iorw'");
929 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000930 case Match_InvalidFRMArg: {
931 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
932 return Error(
933 ErrorLoc,
934 "operand must be a valid floating point rounding mode mnemonic");
935 }
Shiva Chen98f93892018-04-25 14:18:55 +0000936 case Match_InvalidBareSymbol: {
937 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
938 return Error(ErrorLoc, "operand must be a bare symbol name");
939 }
Alex Bradbury44668ae2019-04-01 14:53:17 +0000940 case Match_InvalidCallSymbol: {
941 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
942 return Error(ErrorLoc, "operand must be a bare symbol name");
943 }
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000944 case Match_InvalidTPRelAddSymbol: {
945 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
946 return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
947 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000948 }
949
950 llvm_unreachable("Unknown match type detected!");
951}
952
Alex Bradbury99794792019-03-17 12:02:32 +0000953// Attempts to match Name as a register (either using the default name or
954// alternative ABI names), setting RegNo to the matching register. Upon
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000955// failure, returns true and sets RegNo to 0. If IsRV32E then registers
956// x16-x31 will be rejected.
957static bool matchRegisterNameHelper(bool IsRV32E, unsigned &RegNo,
958 StringRef Name) {
Alex Bradbury99794792019-03-17 12:02:32 +0000959 RegNo = MatchRegisterName(Name);
960 if (RegNo == 0)
961 RegNo = MatchRegisterAltName(Name);
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000962 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
963 RegNo = 0;
Alex Bradbury99794792019-03-17 12:02:32 +0000964 return RegNo == 0;
965}
966
Alex Bradbury04f06d92017-08-08 14:43:36 +0000967bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
968 SMLoc &EndLoc) {
969 const AsmToken &Tok = getParser().getTok();
970 StartLoc = Tok.getLoc();
971 EndLoc = Tok.getEndLoc();
972 RegNo = 0;
973 StringRef Name = getLexer().getTok().getIdentifier();
974
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000975 if (matchRegisterNameHelper(isRV32E(), RegNo, Name))
Alex Bradburyb18e3142019-03-17 12:00:58 +0000976 return Error(StartLoc, "invalid register name");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000977
Alex Bradburyb18e3142019-03-17 12:00:58 +0000978 getParser().Lex(); // Eat identifier token.
979 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000980}
981
Alex Bradbury8c345c52017-11-09 15:00:03 +0000982OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
983 bool AllowParens) {
984 SMLoc FirstS = getLoc();
985 bool HadParens = false;
986 AsmToken Buf[2];
987
988 // If this a parenthesised register name is allowed, parse it atomically
989 if (AllowParens && getLexer().is(AsmToken::LParen)) {
990 size_t ReadCount = getLexer().peekTokens(Buf);
991 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
992 HadParens = true;
993 getParser().Lex(); // Eat '('
994 }
995 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000996
997 switch (getLexer().getKind()) {
998 default:
999 return MatchOperand_NoMatch;
1000 case AsmToken::Identifier:
1001 StringRef Name = getLexer().getTok().getIdentifier();
Alex Bradbury99794792019-03-17 12:02:32 +00001002 unsigned RegNo;
Alex Bradburydab1f6f2019-03-22 11:21:40 +00001003 matchRegisterNameHelper(isRV32E(), RegNo, Name);
Alex Bradbury99794792019-03-17 12:02:32 +00001004
Alex Bradbury04f06d92017-08-08 14:43:36 +00001005 if (RegNo == 0) {
Alex Bradbury99794792019-03-17 12:02:32 +00001006 if (HadParens)
1007 getLexer().UnLex(Buf[0]);
1008 return MatchOperand_NoMatch;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001009 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001010 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +00001011 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001012 SMLoc S = getLoc();
1013 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001014 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +00001015 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001016 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001017
1018 if (HadParens) {
1019 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001020 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001021 }
1022
Alex Bradbury04f06d92017-08-08 14:43:36 +00001023 return MatchOperand_Success;
1024}
1025
Ana Pazos9d6c5532018-10-04 21:50:54 +00001026OperandMatchResultTy
1027RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1028 SMLoc S = getLoc();
1029 const MCExpr *Res;
1030
1031 switch (getLexer().getKind()) {
1032 default:
1033 return MatchOperand_NoMatch;
1034 case AsmToken::LParen:
1035 case AsmToken::Minus:
1036 case AsmToken::Plus:
1037 case AsmToken::Integer:
1038 case AsmToken::String: {
1039 if (getParser().parseExpression(Res))
1040 return MatchOperand_ParseFail;
1041
1042 auto *CE = dyn_cast<MCConstantExpr>(Res);
1043 if (CE) {
1044 int64_t Imm = CE->getValue();
1045 if (isUInt<12>(Imm)) {
1046 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1047 // Accept an immediate representing a named or un-named Sys Reg
1048 // if the range is valid, regardless of the required features.
1049 Operands.push_back(RISCVOperand::createSysReg(
1050 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1051 return MatchOperand_Success;
1052 }
1053 }
1054
1055 Twine Msg = "immediate must be an integer in the range";
1056 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1057 return MatchOperand_ParseFail;
1058 }
1059 case AsmToken::Identifier: {
1060 StringRef Identifier;
1061 if (getParser().parseIdentifier(Identifier))
1062 return MatchOperand_ParseFail;
1063
1064 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1065 // Accept a named Sys Reg if the required features are present.
1066 if (SysReg) {
1067 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1068 Error(S, "system register use requires an option to be enabled");
1069 return MatchOperand_ParseFail;
1070 }
1071 Operands.push_back(RISCVOperand::createSysReg(
1072 Identifier, S, SysReg->Encoding, isRV64()));
1073 return MatchOperand_Success;
1074 }
1075
1076 Twine Msg = "operand must be a valid system register name "
1077 "or an integer in the range";
1078 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1079 return MatchOperand_ParseFail;
1080 }
1081 case AsmToken::Percent: {
1082 // Discard operand with modifier.
1083 Twine Msg = "immediate must be an integer in the range";
1084 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1085 return MatchOperand_ParseFail;
1086 }
1087 }
1088
1089 return MatchOperand_NoMatch;
1090}
1091
Alex Bradbury04f06d92017-08-08 14:43:36 +00001092OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001093 SMLoc S = getLoc();
1094 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1095 const MCExpr *Res;
1096
Alex Bradbury04f06d92017-08-08 14:43:36 +00001097 switch (getLexer().getKind()) {
1098 default:
1099 return MatchOperand_NoMatch;
1100 case AsmToken::LParen:
1101 case AsmToken::Minus:
1102 case AsmToken::Plus:
1103 case AsmToken::Integer:
1104 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001105 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001106 if (getParser().parseExpression(Res))
1107 return MatchOperand_ParseFail;
1108 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001109 case AsmToken::Percent:
1110 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001111 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001112
Alex Bradburya6e62482017-12-07 10:53:48 +00001113 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001114 return MatchOperand_Success;
1115}
1116
1117OperandMatchResultTy
1118RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1119 SMLoc S = getLoc();
1120 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1121
1122 if (getLexer().getKind() != AsmToken::Percent) {
1123 Error(getLoc(), "expected '%' for operand modifier");
1124 return MatchOperand_ParseFail;
1125 }
1126
1127 getParser().Lex(); // Eat '%'
1128
1129 if (getLexer().getKind() != AsmToken::Identifier) {
1130 Error(getLoc(), "expected valid identifier for operand modifier");
1131 return MatchOperand_ParseFail;
1132 }
1133 StringRef Identifier = getParser().getTok().getIdentifier();
1134 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1135 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1136 Error(getLoc(), "unrecognized operand modifier");
1137 return MatchOperand_ParseFail;
1138 }
1139
1140 getParser().Lex(); // Eat the identifier
1141 if (getLexer().getKind() != AsmToken::LParen) {
1142 Error(getLoc(), "expected '('");
1143 return MatchOperand_ParseFail;
1144 }
1145 getParser().Lex(); // Eat '('
1146
1147 const MCExpr *SubExpr;
1148 if (getParser().parseParenExpression(SubExpr, E)) {
1149 return MatchOperand_ParseFail;
1150 }
1151
1152 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001153 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001154 return MatchOperand_Success;
1155}
1156
Alex Bradbury68f73c12018-09-18 15:18:16 +00001157OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1158 SMLoc S = getLoc();
1159 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1160 const MCExpr *Res;
1161
1162 if (getLexer().getKind() != AsmToken::Identifier)
1163 return MatchOperand_NoMatch;
1164
1165 StringRef Identifier;
Kito Cheng303217e2019-02-20 03:31:32 +00001166 AsmToken Tok = getLexer().getTok();
1167
Alex Bradbury68f73c12018-09-18 15:18:16 +00001168 if (getParser().parseIdentifier(Identifier))
1169 return MatchOperand_ParseFail;
1170
Alex Bradburyf8078f62019-04-02 12:47:20 +00001171 if (Identifier.consume_back("@plt")) {
1172 Error(getLoc(), "'@plt' operand not valid for instruction");
1173 return MatchOperand_ParseFail;
1174 }
1175
Alex Bradbury68f73c12018-09-18 15:18:16 +00001176 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Kito Cheng303217e2019-02-20 03:31:32 +00001177
1178 if (Sym->isVariable()) {
1179 const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1180 if (!isa<MCSymbolRefExpr>(V)) {
1181 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1182 return MatchOperand_NoMatch;
1183 }
1184 Res = V;
1185 } else
1186 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury68f73c12018-09-18 15:18:16 +00001187 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1188 return MatchOperand_Success;
1189}
1190
Alex Bradbury44668ae2019-04-01 14:53:17 +00001191OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1192 SMLoc S = getLoc();
1193 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1194 const MCExpr *Res;
1195
1196 if (getLexer().getKind() != AsmToken::Identifier)
1197 return MatchOperand_NoMatch;
1198
1199 StringRef Identifier;
1200 if (getParser().parseIdentifier(Identifier))
1201 return MatchOperand_ParseFail;
1202
Alex Bradburyf8078f62019-04-02 12:47:20 +00001203 RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1204 if (Identifier.consume_back("@plt"))
1205 Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1206
Alex Bradbury44668ae2019-04-01 14:53:17 +00001207 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1208 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradburyf8078f62019-04-02 12:47:20 +00001209 Res = RISCVMCExpr::create(Res, Kind, getContext());
Alex Bradbury44668ae2019-04-01 14:53:17 +00001210 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1211 return MatchOperand_Success;
1212}
1213
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001214OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1215 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1216 // both being acceptable forms. When parsing `jal ra, foo` this function
1217 // will be called for the `ra` register operand in an attempt to match the
1218 // single-operand alias. parseJALOffset must fail for this case. It would
1219 // seem logical to try parse the operand using parseImmediate and return
1220 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1221 // the second form rather than the first). We can't do this as there's no
1222 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1223 // is an identifier and is followed by a comma.
1224 if (getLexer().is(AsmToken::Identifier) &&
1225 getLexer().peekTok().is(AsmToken::Comma))
1226 return MatchOperand_NoMatch;
1227
1228 return parseImmediate(Operands);
1229}
1230
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001231OperandMatchResultTy
1232RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1233 if (getLexer().isNot(AsmToken::LParen)) {
1234 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001235 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001236 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001237
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001238 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001239 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001240
1241 if (parseRegister(Operands) != MatchOperand_Success) {
1242 Error(getLoc(), "expected register");
1243 return MatchOperand_ParseFail;
1244 }
1245
1246 if (getLexer().isNot(AsmToken::RParen)) {
1247 Error(getLoc(), "expected ')'");
1248 return MatchOperand_ParseFail;
1249 }
1250
1251 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001252 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001253
Alex Bradbury04f06d92017-08-08 14:43:36 +00001254 return MatchOperand_Success;
1255}
1256
Alex Bradburycd8688a2018-04-25 17:25:29 +00001257/// Looks at a token type and creates the relevant operand from this
1258/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001259/// true.
1260bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1261 // Check if the current operand has a custom associated parser, if so, try to
1262 // custom parse the operand, or fallback to the general approach.
1263 OperandMatchResultTy Result =
1264 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1265 if (Result == MatchOperand_Success)
1266 return false;
1267 if (Result == MatchOperand_ParseFail)
1268 return true;
1269
1270 // Attempt to parse token as a register.
1271 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001272 return false;
1273
1274 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001275 if (parseImmediate(Operands) == MatchOperand_Success) {
1276 // Parse memory base register if present
1277 if (getLexer().is(AsmToken::LParen))
1278 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001279 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001280 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001281
1282 // Finally we have exhausted all options and must declare defeat.
1283 Error(getLoc(), "unknown operand");
1284 return true;
1285}
1286
1287bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1288 StringRef Name, SMLoc NameLoc,
1289 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001290 // Ensure that if the instruction occurs when relaxation is enabled,
1291 // relocations are forced for the file. Ideally this would be done when there
1292 // is enough information to reliably determine if the instruction itself may
1293 // cause relaxations. Unfortunately instruction processing stage occurs in the
1294 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1295 // for the entire file.
1296 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1297 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1298 if (Assembler != nullptr) {
1299 RISCVAsmBackend &MAB =
1300 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1301 MAB.setForceRelocs();
1302 }
1303 }
1304
Alex Bradbury04f06d92017-08-08 14:43:36 +00001305 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001306 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001307
1308 // If there are no more operands, then finish
1309 if (getLexer().is(AsmToken::EndOfStatement))
1310 return false;
1311
1312 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001313 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001314 return true;
1315
1316 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001317 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001318 while (getLexer().is(AsmToken::Comma)) {
1319 // Consume comma token
1320 getLexer().Lex();
1321
1322 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001323 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001324 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001325
1326 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001327 }
1328
1329 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1330 SMLoc Loc = getLexer().getLoc();
1331 getParser().eatToEndOfStatement();
1332 return Error(Loc, "unexpected token");
1333 }
1334
1335 getParser().Lex(); // Consume the EndOfStatement.
1336 return false;
1337}
1338
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001339bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1340 RISCVMCExpr::VariantKind &Kind,
1341 int64_t &Addend) {
1342 Kind = RISCVMCExpr::VK_RISCV_None;
1343 Addend = 0;
1344
1345 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1346 Kind = RE->getKind();
1347 Expr = RE->getSubExpr();
1348 }
1349
1350 // It's a simple symbol reference or constant with no addend.
1351 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1352 return true;
1353
1354 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1355 if (!BE)
1356 return false;
1357
1358 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1359 return false;
1360
1361 if (BE->getOpcode() != MCBinaryExpr::Add &&
1362 BE->getOpcode() != MCBinaryExpr::Sub)
1363 return false;
1364
1365 // We are able to support the subtraction of two symbol references
1366 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1367 isa<MCSymbolRefExpr>(BE->getRHS()))
1368 return true;
1369
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001370 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001371 // on here than we can deal with.
1372 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1373 if (!AddendExpr)
1374 return false;
1375
1376 Addend = AddendExpr->getValue();
1377 if (BE->getOpcode() == MCBinaryExpr::Sub)
1378 Addend = -Addend;
1379
1380 // It's some symbol reference + a constant addend
1381 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1382}
1383
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001384bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1385 // This returns false if this function recognizes the directive
1386 // regardless of whether it is successfully handles or reports an
1387 // error. Otherwise it returns true to give the generic parser a
1388 // chance at recognizing it.
1389 StringRef IDVal = DirectiveID.getString();
1390
1391 if (IDVal == ".option")
1392 return parseDirectiveOption();
1393
1394 return true;
1395}
1396
1397bool RISCVAsmParser::parseDirectiveOption() {
1398 MCAsmParser &Parser = getParser();
1399 // Get the option token.
1400 AsmToken Tok = Parser.getTok();
1401 // At the moment only identifiers are supported.
1402 if (Tok.isNot(AsmToken::Identifier))
1403 return Error(Parser.getTok().getLoc(),
1404 "unexpected token, expected identifier");
1405
1406 StringRef Option = Tok.getIdentifier();
1407
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001408 if (Option == "push") {
1409 getTargetStreamer().emitDirectiveOptionPush();
1410
1411 Parser.Lex();
1412 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1413 return Error(Parser.getTok().getLoc(),
1414 "unexpected token, expected end of statement");
1415
1416 pushFeatureBits();
1417 return false;
1418 }
1419
1420 if (Option == "pop") {
1421 SMLoc StartLoc = Parser.getTok().getLoc();
1422 getTargetStreamer().emitDirectiveOptionPop();
1423
1424 Parser.Lex();
1425 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1426 return Error(Parser.getTok().getLoc(),
1427 "unexpected token, expected end of statement");
1428
1429 if (popFeatureBits())
1430 return Error(StartLoc, ".option pop with no .option push");
1431
1432 return false;
1433 }
1434
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001435 if (Option == "rvc") {
1436 getTargetStreamer().emitDirectiveOptionRVC();
1437
1438 Parser.Lex();
1439 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1440 return Error(Parser.getTok().getLoc(),
1441 "unexpected token, expected end of statement");
1442
1443 setFeatureBits(RISCV::FeatureStdExtC, "c");
1444 return false;
1445 }
1446
1447 if (Option == "norvc") {
1448 getTargetStreamer().emitDirectiveOptionNoRVC();
1449
1450 Parser.Lex();
1451 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1452 return Error(Parser.getTok().getLoc(),
1453 "unexpected token, expected end of statement");
1454
1455 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1456 return false;
1457 }
1458
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001459 if (Option == "relax") {
1460 getTargetStreamer().emitDirectiveOptionRelax();
1461
1462 Parser.Lex();
1463 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1464 return Error(Parser.getTok().getLoc(),
1465 "unexpected token, expected end of statement");
1466
1467 setFeatureBits(RISCV::FeatureRelax, "relax");
1468 return false;
1469 }
1470
1471 if (Option == "norelax") {
1472 getTargetStreamer().emitDirectiveOptionNoRelax();
1473
1474 Parser.Lex();
1475 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1476 return Error(Parser.getTok().getLoc(),
1477 "unexpected token, expected end of statement");
1478
1479 clearFeatureBits(RISCV::FeatureRelax, "relax");
1480 return false;
1481 }
1482
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001483 // Unknown option.
1484 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001485 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1486 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001487 Parser.eatToEndOfStatement();
1488 return false;
1489}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001490
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001491void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1492 MCInst CInst;
1493 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1494 CInst.setLoc(Inst.getLoc());
1495 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1496}
1497
1498void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1499 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001500 RISCVMatInt::InstSeq Seq;
1501 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001502
Alex Bradbury22c091f2018-11-15 10:11:31 +00001503 unsigned SrcReg = RISCV::X0;
1504 for (RISCVMatInt::Inst &Inst : Seq) {
1505 if (Inst.Opc == RISCV::LUI) {
1506 emitToStreamer(
1507 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1508 } else {
1509 emitToStreamer(
1510 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1511 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001512 }
1513
Alex Bradbury22c091f2018-11-15 10:11:31 +00001514 // Only the first instruction has X0 as its source.
1515 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001516 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001517}
1518
Alex Bradbury22531c42019-02-15 09:53:32 +00001519void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
1520 const MCExpr *Symbol,
1521 RISCVMCExpr::VariantKind VKHi,
1522 unsigned SecondOpcode, SMLoc IDLoc,
1523 MCStreamer &Out) {
1524 // A pair of instructions for PC-relative addressing; expands to
1525 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
1526 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001527 MCContext &Ctx = getContext();
1528
1529 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1530 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1531 Out.EmitLabel(TmpLabel);
1532
Alex Bradbury22531c42019-02-15 09:53:32 +00001533 const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001534 emitToStreamer(
Alex Bradbury22531c42019-02-15 09:53:32 +00001535 Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001536
1537 const MCExpr *RefToLinkTmpLabel =
1538 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1539 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1540
Alex Bradbury22531c42019-02-15 09:53:32 +00001541 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001542 .addOperand(DestReg)
Alex Bradbury22531c42019-02-15 09:53:32 +00001543 .addOperand(TmpReg)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001544 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001545}
1546
Alex Bradbury22531c42019-02-15 09:53:32 +00001547void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1548 MCStreamer &Out) {
1549 // The load local address pseudo-instruction "lla" is used in PC-relative
1550 // addressing of local symbols:
1551 // lla rdest, symbol
1552 // expands to
1553 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1554 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1555 MCOperand DestReg = Inst.getOperand(0);
1556 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1557 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1558 RISCV::ADDI, IDLoc, Out);
1559}
1560
1561void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
1562 MCStreamer &Out) {
1563 // The load address pseudo-instruction "la" is used in PC-relative and
1564 // GOT-indirect addressing of global symbols:
1565 // la rdest, symbol
1566 // expands to either (for non-PIC)
1567 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1568 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1569 // or (for PIC)
1570 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
1571 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1572 MCOperand DestReg = Inst.getOperand(0);
1573 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1574 unsigned SecondOpcode;
1575 RISCVMCExpr::VariantKind VKHi;
1576 // FIXME: Should check .option (no)pic when implemented
1577 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1578 SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1579 VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
1580 } else {
1581 SecondOpcode = RISCV::ADDI;
1582 VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
1583 }
1584 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1585}
1586
Kito Cheng303217e2019-02-20 03:31:32 +00001587void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
1588 SMLoc IDLoc, MCStreamer &Out,
1589 bool HasTmpReg) {
1590 // The load/store pseudo-instruction does a pc-relative load with
1591 // a symbol.
1592 //
1593 // The expansion looks like this
1594 //
1595 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
1596 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
1597 MCOperand DestReg = Inst.getOperand(0);
1598 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1599 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1600 MCOperand TmpReg = Inst.getOperand(TmpRegOpIdx);
1601 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
1602 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1603 Opcode, IDLoc, Out);
1604}
1605
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001606bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
1607 OperandVector &Operands) {
1608 assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
1609 assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
1610 if (Inst.getOperand(2).getReg() != RISCV::X4) {
1611 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
1612 return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
1613 "%tprel_add modifier");
1614 }
1615
1616 return false;
1617}
1618
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001619bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001620 OperandVector &Operands,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001621 MCStreamer &Out) {
1622 Inst.setLoc(IDLoc);
1623
Alex Bradbury22531c42019-02-15 09:53:32 +00001624 switch (Inst.getOpcode()) {
1625 default:
1626 break;
1627 case RISCV::PseudoLI: {
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001628 unsigned Reg = Inst.getOperand(0).getReg();
1629 const MCOperand &Op1 = Inst.getOperand(1);
1630 if (Op1.isExpr()) {
1631 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1632 // Just convert to an addi. This allows compatibility with gas.
1633 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1634 .addReg(Reg)
1635 .addReg(RISCV::X0)
1636 .addExpr(Op1.getExpr()));
1637 return false;
1638 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001639 int64_t Imm = Inst.getOperand(1).getImm();
1640 // On RV32 the immediate here can either be a signed or an unsigned
1641 // 32-bit number. Sign extension has to be performed to ensure that Imm
1642 // represents the expected signed 64-bit number.
1643 if (!isRV64())
1644 Imm = SignExtend64<32>(Imm);
1645 emitLoadImm(Reg, Imm, Out);
1646 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001647 }
1648 case RISCV::PseudoLLA:
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001649 emitLoadLocalAddress(Inst, IDLoc, Out);
1650 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001651 case RISCV::PseudoLA:
1652 emitLoadAddress(Inst, IDLoc, Out);
1653 return false;
Kito Cheng303217e2019-02-20 03:31:32 +00001654 case RISCV::PseudoLB:
1655 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
1656 return false;
1657 case RISCV::PseudoLBU:
1658 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
1659 return false;
1660 case RISCV::PseudoLH:
1661 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
1662 return false;
1663 case RISCV::PseudoLHU:
1664 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
1665 return false;
1666 case RISCV::PseudoLW:
1667 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
1668 return false;
1669 case RISCV::PseudoLWU:
1670 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
1671 return false;
1672 case RISCV::PseudoLD:
1673 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
1674 return false;
1675 case RISCV::PseudoFLW:
1676 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
1677 return false;
1678 case RISCV::PseudoFLD:
1679 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
1680 return false;
1681 case RISCV::PseudoSB:
1682 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
1683 return false;
1684 case RISCV::PseudoSH:
1685 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
1686 return false;
1687 case RISCV::PseudoSW:
1688 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
1689 return false;
1690 case RISCV::PseudoSD:
1691 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
1692 return false;
1693 case RISCV::PseudoFSW:
1694 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
1695 return false;
1696 case RISCV::PseudoFSD:
1697 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
1698 return false;
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001699 case RISCV::PseudoAddTPRel:
1700 if (checkPseudoAddTPRel(Inst, Operands))
1701 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001702 }
1703
1704 emitToStreamer(Out, Inst);
1705 return false;
1706}
1707
Alex Bradbury04f06d92017-08-08 14:43:36 +00001708extern "C" void LLVMInitializeRISCVAsmParser() {
1709 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1710 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1711}