blob: 5e89b4c8d987ddb9cfa841c5e2ef7bbcada53638 [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 Bradbury04f06d92017-08-08 14:43:36 +000023#include "llvm/MC/MCParser/MCAsmLexer.h"
24#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000026#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000029#include "llvm/Support/Casting.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000030#include "llvm/Support/MathExtras.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000031#include "llvm/Support/TargetRegistry.h"
32
Alex Bradbury6a4b5442018-06-07 15:35:47 +000033#include <limits>
34
Alex Bradbury04f06d92017-08-08 14:43:36 +000035using namespace llvm;
36
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000037// Include the auto-generated portion of the compress emitter.
38#define GEN_COMPRESS_INSTR
39#include "RISCVGenCompressInstEmitter.inc"
40
Alex Bradbury04f06d92017-08-08 14:43:36 +000041namespace {
42struct RISCVOperand;
43
44class RISCVAsmParser : public MCTargetAsmParser {
Alex Bradbury893e5bc2018-11-28 16:39:14 +000045 SmallVector<FeatureBitset, 4> FeatureBitStack;
46
Alex Bradbury04f06d92017-08-08 14:43:36 +000047 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000048 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000049
Alex Bradburybca0c3c2018-05-11 17:30:28 +000050 RISCVTargetStreamer &getTargetStreamer() {
51 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
52 return static_cast<RISCVTargetStreamer &>(TS);
53 }
54
Alex Bradbury7bc2a952017-12-07 10:46:23 +000055 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
56 unsigned Kind) override;
57
Alex Bradbury6758ecb2017-09-17 14:27:35 +000058 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury6a4b5442018-06-07 15:35:47 +000059 int64_t Lower, int64_t Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000060
Alex Bradbury04f06d92017-08-08 14:43:36 +000061 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
62 OperandVector &Operands, MCStreamer &Out,
63 uint64_t &ErrorInfo,
64 bool MatchingInlineAsm) override;
65
66 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
67
68 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
69 SMLoc NameLoc, OperandVector &Operands) override;
70
71 bool ParseDirective(AsmToken DirectiveID) override;
72
Alex Bradbury6a4b5442018-06-07 15:35:47 +000073 // Helper to actually emit an instruction to the MCStreamer. Also, when
74 // possible, compression of the instruction is performed.
75 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
76
77 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
78 // synthesize the desired immedate value into the destination register.
79 void emitLoadImm(unsigned DestReg, int64_t Value, MCStreamer &Out);
80
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +000081 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
82 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
83
Alex Bradbury6a4b5442018-06-07 15:35:47 +000084 /// Helper for processing MC instructions that have been successfully matched
85 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
86 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
87 /// in this method.
88 bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
89
Alex Bradbury04f06d92017-08-08 14:43:36 +000090// Auto-generated instruction matching functions
91#define GET_ASSEMBLER_HEADER
92#include "RISCVGenAsmMatcher.inc"
93
Ana Pazos9d6c5532018-10-04 21:50:54 +000094 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000095 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +000096 OperandMatchResultTy parseRegister(OperandVector &Operands,
97 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000098 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +000099 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +0000100 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000101 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000102
Alex Bradbury68f73c12018-09-18 15:18:16 +0000103 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000104
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000105 bool parseDirectiveOption();
106
107 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
108 if (!(getSTI().getFeatureBits()[Feature])) {
109 MCSubtargetInfo &STI = copySTI();
110 setAvailableFeatures(
111 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
112 }
113 }
114
115 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
116 if (getSTI().getFeatureBits()[Feature]) {
117 MCSubtargetInfo &STI = copySTI();
118 setAvailableFeatures(
119 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
120 }
121 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000122
Alex Bradbury893e5bc2018-11-28 16:39:14 +0000123 void pushFeatureBits() {
124 FeatureBitStack.push_back(getSTI().getFeatureBits());
125 }
126
127 bool popFeatureBits() {
128 if (FeatureBitStack.empty())
129 return true;
130
131 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
132 copySTI().setFeatureBits(FeatureBits);
133 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
134
135 return false;
136 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000137public:
138 enum RISCVMatchResultTy {
139 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
140#define GET_OPERAND_DIAGNOSTIC_TYPES
141#include "RISCVGenAsmMatcher.inc"
142#undef GET_OPERAND_DIAGNOSTIC_TYPES
143 };
144
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000145 static bool classifySymbolRef(const MCExpr *Expr,
146 RISCVMCExpr::VariantKind &Kind,
147 int64_t &Addend);
148
Alex Bradbury04f06d92017-08-08 14:43:36 +0000149 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
150 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000151 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000152 Parser.addAliasForDirective(".half", ".2byte");
153 Parser.addAliasForDirective(".hword", ".2byte");
154 Parser.addAliasForDirective(".word", ".4byte");
155 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000156 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
157 }
158};
159
160/// RISCVOperand - Instances of this class represent a parsed machine
161/// instruction
162struct RISCVOperand : public MCParsedAsmOperand {
163
164 enum KindTy {
165 Token,
166 Register,
167 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000168 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000169 } Kind;
170
Alex Bradburya6e62482017-12-07 10:53:48 +0000171 bool IsRV64;
172
Alex Bradbury04f06d92017-08-08 14:43:36 +0000173 struct RegOp {
174 unsigned RegNum;
175 };
176
177 struct ImmOp {
178 const MCExpr *Val;
179 };
180
Ana Pazos9d6c5532018-10-04 21:50:54 +0000181 struct SysRegOp {
182 const char *Data;
183 unsigned Length;
184 unsigned Encoding;
185 // FIXME: Add the Encoding parsed fields as needed for checks,
186 // e.g.: read/write or user/supervisor/machine privileges.
187 };
188
Alex Bradbury04f06d92017-08-08 14:43:36 +0000189 SMLoc StartLoc, EndLoc;
190 union {
191 StringRef Tok;
192 RegOp Reg;
193 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000194 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000195 };
196
197 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
198
199public:
200 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
201 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000202 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000203 StartLoc = o.StartLoc;
204 EndLoc = o.EndLoc;
205 switch (Kind) {
206 case Register:
207 Reg = o.Reg;
208 break;
209 case Immediate:
210 Imm = o.Imm;
211 break;
212 case Token:
213 Tok = o.Tok;
214 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000215 case SystemRegister:
216 SysReg = o.SysReg;
217 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000218 }
219 }
220
221 bool isToken() const override { return Kind == Token; }
222 bool isReg() const override { return Kind == Register; }
223 bool isImm() const override { return Kind == Immediate; }
224 bool isMem() const override { return false; }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000225 bool isSystemRegister() const { return Kind == SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000226
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000227 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
228 RISCVMCExpr::VariantKind &VK) {
229 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000230 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000231 return RE->evaluateAsConstant(Imm);
232 }
233
234 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000235 VK = RISCVMCExpr::VK_RISCV_None;
236 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000237 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000238 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000239
240 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000241 }
242
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000243 // True if operand is a symbol with no modifiers, or a constant with no
244 // modifiers and isShiftedInt<N-1, 1>(Op).
245 template <int N> bool isBareSimmNLsb0() const {
246 int64_t Imm;
247 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000248 if (!isImm())
249 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000250 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000251 bool IsValid;
252 if (!IsConstantImm)
253 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
254 else
255 IsValid = isShiftedInt<N - 1, 1>(Imm);
256 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000257 }
258
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000259 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
260
Shiva Chen98f93892018-04-25 14:18:55 +0000261 bool isBareSymbol() const {
262 int64_t Imm;
263 RISCVMCExpr::VariantKind VK;
264 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000265 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000266 return false;
267 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
268 VK == RISCVMCExpr::VK_RISCV_None;
269 }
270
Ana Pazos9d6c5532018-10-04 21:50:54 +0000271 bool isCSRSystemRegister() const { return isSystemRegister(); }
272
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000273 /// Return true if the operand is a valid for the fence instruction e.g.
274 /// ('iorw').
275 bool isFenceArg() const {
276 if (!isImm())
277 return false;
278 const MCExpr *Val = getImm();
279 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
280 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
281 return false;
282
283 StringRef Str = SVal->getSymbol().getName();
284 // Letters must be unique, taken from 'iorw', and in ascending order. This
285 // holds as long as each individual character is one of 'iorw' and is
286 // greater than the previous character.
287 char Prev = '\0';
288 for (char c : Str) {
289 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
290 return false;
291 if (c <= Prev)
292 return false;
293 Prev = c;
294 }
295 return true;
296 }
297
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000298 /// Return true if the operand is a valid floating point rounding mode.
299 bool isFRMArg() const {
300 if (!isImm())
301 return false;
302 const MCExpr *Val = getImm();
303 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
304 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
305 return false;
306
307 StringRef Str = SVal->getSymbol().getName();
308
309 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
310 }
311
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000312 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000313 int64_t Imm;
314 RISCVMCExpr::VariantKind VK;
315 if (!isImm())
316 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000317 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000318 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
319 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000320 // Given only Imm, ensuring that the actually specified constant is either
321 // a signed or unsigned 64-bit number is unfortunately impossible.
322 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
323 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
324 }
325
Alex Bradburya6e62482017-12-07 10:53:48 +0000326 bool isUImmLog2XLen() const {
327 int64_t Imm;
328 RISCVMCExpr::VariantKind VK;
329 if (!isImm())
330 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000331 if (!evaluateConstantImm(getImm(), Imm, VK) ||
332 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000333 return false;
334 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
335 }
336
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000337 bool isUImmLog2XLenNonZero() const {
338 int64_t Imm;
339 RISCVMCExpr::VariantKind VK;
340 if (!isImm())
341 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000342 if (!evaluateConstantImm(getImm(), Imm, VK) ||
343 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000344 return false;
345 if (Imm == 0)
346 return false;
347 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
348 }
349
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000350 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000351 int64_t Imm;
352 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000353 if (!isImm())
354 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000355 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000356 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000357 }
358
Alex Bradbury60714f92017-12-13 09:32:55 +0000359 bool isUImm5NonZero() const {
360 int64_t Imm;
361 RISCVMCExpr::VariantKind VK;
362 if (!isImm())
363 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000364 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000365 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
366 VK == RISCVMCExpr::VK_RISCV_None;
367 }
368
Alex Bradbury581d6b02017-12-13 09:41:21 +0000369 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000370 if (!isImm())
371 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000372 RISCVMCExpr::VariantKind VK;
373 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000374 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000375 return IsConstantImm && isInt<6>(Imm) &&
376 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000377 }
378
Shiva Chenb22c1d22018-02-02 02:43:23 +0000379 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000380 if (!isImm())
381 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000382 RISCVMCExpr::VariantKind VK;
383 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000384 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000385 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
386 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000387 }
388
Shiva Chen7c172422018-02-22 15:02:28 +0000389 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000390 if (!isImm())
391 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000392 int64_t Imm;
393 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000394 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000395 return IsConstantImm && (Imm != 0) &&
396 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000397 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000398 }
399
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000400 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000401 if (!isImm())
402 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000403 int64_t Imm;
404 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000405 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000406 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
407 VK == RISCVMCExpr::VK_RISCV_None;
408 }
409
410 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000411 if (!isImm())
412 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000413 int64_t Imm;
414 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000415 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000416 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
417 VK == RISCVMCExpr::VK_RISCV_None;
418 }
419
420 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000421 if (!isImm())
422 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000423 int64_t Imm;
424 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000425 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000426 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
427 VK == RISCVMCExpr::VK_RISCV_None;
428 }
429
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000430 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
431
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000432 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000433 if (!isImm())
434 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000435 int64_t Imm;
436 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000437 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000438 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
439 VK == RISCVMCExpr::VK_RISCV_None;
440 }
441
Alex Bradbury60714f92017-12-13 09:32:55 +0000442 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000443 if (!isImm())
444 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000445 int64_t Imm;
446 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000447 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000448 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
449 VK == RISCVMCExpr::VK_RISCV_None;
450 }
451
Alex Bradbury04f06d92017-08-08 14:43:36 +0000452 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000453 RISCVMCExpr::VariantKind VK;
454 int64_t Imm;
455 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000456 if (!isImm())
457 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000458 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000459 if (!IsConstantImm)
460 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
461 else
462 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000463 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000464 VK == RISCVMCExpr::VK_RISCV_LO ||
465 VK == RISCVMCExpr::VK_RISCV_PCREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000466 }
467
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000468 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
469
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000470 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000471
Shiva Chenb22c1d22018-02-02 02:43:23 +0000472 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000473 if (!isImm())
474 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000475 int64_t Imm;
476 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000477 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000478 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000479 VK == RISCVMCExpr::VK_RISCV_None;
480 }
481
Alex Bradbury74340f12018-09-18 15:08:35 +0000482 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000483 RISCVMCExpr::VariantKind VK;
484 int64_t Imm;
485 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000486 if (!isImm())
487 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000488 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000489 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000490 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury74340f12018-09-18 15:08:35 +0000491 return IsValid && VK == RISCVMCExpr::VK_RISCV_HI;
492 } else {
493 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
494 VK == RISCVMCExpr::VK_RISCV_HI);
495 }
496 }
497
498 bool isUImm20AUIPC() const {
499 RISCVMCExpr::VariantKind VK;
500 int64_t Imm;
501 bool IsValid;
502 if (!isImm())
503 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000504 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000505 if (!IsConstantImm) {
506 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000507 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
508 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000509 } else {
510 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000511 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
512 VK == RISCVMCExpr::VK_RISCV_GOT_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000513 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000514 }
515
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000516 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000517
Alex Bradbury04f06d92017-08-08 14:43:36 +0000518 /// getStartLoc - Gets location of the first token of this operand
519 SMLoc getStartLoc() const override { return StartLoc; }
520 /// getEndLoc - Gets location of the last token of this operand
521 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000522 /// True if this operand is for an RV64 instruction
523 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000524
525 unsigned getReg() const override {
526 assert(Kind == Register && "Invalid type access!");
527 return Reg.RegNum;
528 }
529
Ana Pazos9d6c5532018-10-04 21:50:54 +0000530 StringRef getSysReg() const {
531 assert(Kind == SystemRegister && "Invalid access!");
532 return StringRef(SysReg.Data, SysReg.Length);
533 }
534
Alex Bradbury04f06d92017-08-08 14:43:36 +0000535 const MCExpr *getImm() const {
536 assert(Kind == Immediate && "Invalid type access!");
537 return Imm.Val;
538 }
539
540 StringRef getToken() const {
541 assert(Kind == Token && "Invalid type access!");
542 return Tok;
543 }
544
545 void print(raw_ostream &OS) const override {
546 switch (Kind) {
547 case Immediate:
548 OS << *getImm();
549 break;
550 case Register:
551 OS << "<register x";
552 OS << getReg() << ">";
553 break;
554 case Token:
555 OS << "'" << getToken() << "'";
556 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000557 case SystemRegister:
558 OS << "<sysreg: " << getSysReg() << '>';
559 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000560 }
561 }
562
Alex Bradburya6e62482017-12-07 10:53:48 +0000563 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
564 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000565 auto Op = make_unique<RISCVOperand>(Token);
566 Op->Tok = Str;
567 Op->StartLoc = S;
568 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000569 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000570 return Op;
571 }
572
573 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000574 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000575 auto Op = make_unique<RISCVOperand>(Register);
576 Op->Reg.RegNum = RegNo;
577 Op->StartLoc = S;
578 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000579 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000580 return Op;
581 }
582
583 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000584 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000585 auto Op = make_unique<RISCVOperand>(Immediate);
586 Op->Imm.Val = Val;
587 Op->StartLoc = S;
588 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000589 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000590 return Op;
591 }
592
Ana Pazos9d6c5532018-10-04 21:50:54 +0000593 static std::unique_ptr<RISCVOperand>
594 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
595 auto Op = make_unique<RISCVOperand>(SystemRegister);
596 Op->SysReg.Data = Str.data();
597 Op->SysReg.Length = Str.size();
598 Op->SysReg.Encoding = Encoding;
599 Op->StartLoc = S;
600 Op->IsRV64 = IsRV64;
601 return Op;
602 }
603
Alex Bradbury04f06d92017-08-08 14:43:36 +0000604 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
605 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000606 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000607 RISCVMCExpr::VariantKind VK;
608 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000609
610 if (IsConstant)
611 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000612 else
613 Inst.addOperand(MCOperand::createExpr(Expr));
614 }
615
616 // Used by the TableGen Code
617 void addRegOperands(MCInst &Inst, unsigned N) const {
618 assert(N == 1 && "Invalid number of operands!");
619 Inst.addOperand(MCOperand::createReg(getReg()));
620 }
621
622 void addImmOperands(MCInst &Inst, unsigned N) const {
623 assert(N == 1 && "Invalid number of operands!");
624 addExpr(Inst, getImm());
625 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000626
627 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
628 assert(N == 1 && "Invalid number of operands!");
629 // isFenceArg has validated the operand, meaning this cast is safe
630 auto SE = cast<MCSymbolRefExpr>(getImm());
631
632 unsigned Imm = 0;
633 for (char c : SE->getSymbol().getName()) {
634 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000635 default:
636 llvm_unreachable("FenceArg must contain only [iorw]");
637 case 'i': Imm |= RISCVFenceField::I; break;
638 case 'o': Imm |= RISCVFenceField::O; break;
639 case 'r': Imm |= RISCVFenceField::R; break;
640 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000641 }
642 }
643 Inst.addOperand(MCOperand::createImm(Imm));
644 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000645
Ana Pazos9d6c5532018-10-04 21:50:54 +0000646 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
647 assert(N == 1 && "Invalid number of operands!");
648 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
649 }
650
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000651 // Returns the rounding mode represented by this RISCVOperand. Should only
652 // be called after checking isFRMArg.
653 RISCVFPRndMode::RoundingMode getRoundingMode() const {
654 // isFRMArg has validated the operand, meaning this cast is safe.
655 auto SE = cast<MCSymbolRefExpr>(getImm());
656 RISCVFPRndMode::RoundingMode FRM =
657 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
658 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
659 return FRM;
660 }
661
662 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
663 assert(N == 1 && "Invalid number of operands!");
664 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
665 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000666};
667} // end anonymous namespace.
668
669#define GET_REGISTER_MATCHER
670#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000671#include "RISCVGenAsmMatcher.inc"
672
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000673// Return the matching FPR64 register for the given FPR32.
674// FIXME: Ideally this function could be removed in favour of using
675// information from TableGen.
676unsigned convertFPR32ToFPR64(unsigned Reg) {
677 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000678 default:
679 llvm_unreachable("Not a recognised FPR32 register");
680 case RISCV::F0_32: return RISCV::F0_64;
681 case RISCV::F1_32: return RISCV::F1_64;
682 case RISCV::F2_32: return RISCV::F2_64;
683 case RISCV::F3_32: return RISCV::F3_64;
684 case RISCV::F4_32: return RISCV::F4_64;
685 case RISCV::F5_32: return RISCV::F5_64;
686 case RISCV::F6_32: return RISCV::F6_64;
687 case RISCV::F7_32: return RISCV::F7_64;
688 case RISCV::F8_32: return RISCV::F8_64;
689 case RISCV::F9_32: return RISCV::F9_64;
690 case RISCV::F10_32: return RISCV::F10_64;
691 case RISCV::F11_32: return RISCV::F11_64;
692 case RISCV::F12_32: return RISCV::F12_64;
693 case RISCV::F13_32: return RISCV::F13_64;
694 case RISCV::F14_32: return RISCV::F14_64;
695 case RISCV::F15_32: return RISCV::F15_64;
696 case RISCV::F16_32: return RISCV::F16_64;
697 case RISCV::F17_32: return RISCV::F17_64;
698 case RISCV::F18_32: return RISCV::F18_64;
699 case RISCV::F19_32: return RISCV::F19_64;
700 case RISCV::F20_32: return RISCV::F20_64;
701 case RISCV::F21_32: return RISCV::F21_64;
702 case RISCV::F22_32: return RISCV::F22_64;
703 case RISCV::F23_32: return RISCV::F23_64;
704 case RISCV::F24_32: return RISCV::F24_64;
705 case RISCV::F25_32: return RISCV::F25_64;
706 case RISCV::F26_32: return RISCV::F26_64;
707 case RISCV::F27_32: return RISCV::F27_64;
708 case RISCV::F28_32: return RISCV::F28_64;
709 case RISCV::F29_32: return RISCV::F29_64;
710 case RISCV::F30_32: return RISCV::F30_64;
711 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000712 }
713}
714
715unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
716 unsigned Kind) {
717 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
718 if (!Op.isReg())
719 return Match_InvalidOperand;
720
721 unsigned Reg = Op.getReg();
722 bool IsRegFPR32 =
723 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000724 bool IsRegFPR32C =
725 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000726
727 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000728 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
729 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
730 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000731 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
732 return Match_Success;
733 }
734 return Match_InvalidOperand;
735}
736
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000737bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000738 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000739 Twine Msg = "immediate must be an integer in the range") {
740 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
741 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
742}
743
Alex Bradbury04f06d92017-08-08 14:43:36 +0000744bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
745 OperandVector &Operands,
746 MCStreamer &Out,
747 uint64_t &ErrorInfo,
748 bool MatchingInlineAsm) {
749 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000750
Ana Pazos6b34051b2018-08-30 19:43:19 +0000751 auto Result =
752 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
753 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000754 default:
755 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000756 case Match_Success:
757 return processInstruction(Inst, IDLoc, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000758 case Match_MissingFeature:
759 return Error(IDLoc, "instruction use requires an option to be enabled");
760 case Match_MnemonicFail:
761 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000762 case Match_InvalidOperand: {
763 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000764 if (ErrorInfo != ~0U) {
765 if (ErrorInfo >= Operands.size())
766 return Error(ErrorLoc, "too few operands for instruction");
767
768 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
769 if (ErrorLoc == SMLoc())
770 ErrorLoc = IDLoc;
771 }
772 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000773 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000774 }
775
776 // Handle the case when the error message is of specific type
777 // other than the generic Match_InvalidOperand, and the
778 // corresponding operand is missing.
779 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
780 SMLoc ErrorLoc = IDLoc;
781 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
782 return Error(ErrorLoc, "too few operands for instruction");
783 }
784
785 switch(Result) {
786 default:
787 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000788 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000789 if (isRV64()) {
790 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
791 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
792 }
793 return generateImmOutOfRangeError(Operands, ErrorInfo,
794 std::numeric_limits<int32_t>::min(),
795 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000796 case Match_InvalidUImmLog2XLen:
797 if (isRV64())
798 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
799 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000800 case Match_InvalidUImmLog2XLenNonZero:
801 if (isRV64())
802 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
803 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000804 case Match_InvalidUImm5:
805 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000806 case Match_InvalidSImm6:
807 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
808 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000809 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000810 return generateImmOutOfRangeError(
811 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000812 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000813 case Match_InvalidCLUIImm:
814 return generateImmOutOfRangeError(
815 Operands, ErrorInfo, 1, (1 << 5) - 1,
816 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000817 case Match_InvalidUImm7Lsb00:
818 return generateImmOutOfRangeError(
819 Operands, ErrorInfo, 0, (1 << 7) - 4,
820 "immediate must be a multiple of 4 bytes in the range");
821 case Match_InvalidUImm8Lsb00:
822 return generateImmOutOfRangeError(
823 Operands, ErrorInfo, 0, (1 << 8) - 4,
824 "immediate must be a multiple of 4 bytes in the range");
825 case Match_InvalidUImm8Lsb000:
826 return generateImmOutOfRangeError(
827 Operands, ErrorInfo, 0, (1 << 8) - 8,
828 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000829 case Match_InvalidSImm9Lsb0:
830 return generateImmOutOfRangeError(
831 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
832 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000833 case Match_InvalidUImm9Lsb000:
834 return generateImmOutOfRangeError(
835 Operands, ErrorInfo, 0, (1 << 9) - 8,
836 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000837 case Match_InvalidUImm10Lsb00NonZero:
838 return generateImmOutOfRangeError(
839 Operands, ErrorInfo, 4, (1 << 10) - 4,
840 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000841 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000842 return generateImmOutOfRangeError(
843 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000844 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000845 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000846 return generateImmOutOfRangeError(
847 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
848 "operand must be a symbol with %lo/%pcrel_lo modifier or an integer in "
849 "the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000850 case Match_InvalidSImm12Lsb0:
851 return generateImmOutOfRangeError(
852 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
853 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000854 case Match_InvalidSImm13Lsb0:
855 return generateImmOutOfRangeError(
856 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
857 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000858 case Match_InvalidUImm20LUI:
859 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
860 "operand must be a symbol with %hi() "
861 "modifier or an integer in the range");
862 case Match_InvalidUImm20AUIPC:
863 return generateImmOutOfRangeError(
864 Operands, ErrorInfo, 0, (1 << 20) - 1,
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000865 "operand must be a symbol with a %pcrel_hi/%got_pcrel_hi modifier "
866 "or an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000867 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000868 return generateImmOutOfRangeError(
869 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
870 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000871 case Match_InvalidCSRSystemRegister: {
872 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
873 "operand must be a valid system register "
874 "name or an integer in the range");
875 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000876 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000877 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000878 return Error(
879 ErrorLoc,
880 "operand must be formed of letters selected in-order from 'iorw'");
881 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000882 case Match_InvalidFRMArg: {
883 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
884 return Error(
885 ErrorLoc,
886 "operand must be a valid floating point rounding mode mnemonic");
887 }
Shiva Chen98f93892018-04-25 14:18:55 +0000888 case Match_InvalidBareSymbol: {
889 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
890 return Error(ErrorLoc, "operand must be a bare symbol name");
891 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000892 }
893
894 llvm_unreachable("Unknown match type detected!");
895}
896
897bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
898 SMLoc &EndLoc) {
899 const AsmToken &Tok = getParser().getTok();
900 StartLoc = Tok.getLoc();
901 EndLoc = Tok.getEndLoc();
902 RegNo = 0;
903 StringRef Name = getLexer().getTok().getIdentifier();
904
905 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
906 getParser().Lex(); // Eat identifier token.
907 return false;
908 }
909
910 return Error(StartLoc, "invalid register name");
911}
912
Alex Bradbury8c345c52017-11-09 15:00:03 +0000913OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
914 bool AllowParens) {
915 SMLoc FirstS = getLoc();
916 bool HadParens = false;
917 AsmToken Buf[2];
918
919 // If this a parenthesised register name is allowed, parse it atomically
920 if (AllowParens && getLexer().is(AsmToken::LParen)) {
921 size_t ReadCount = getLexer().peekTokens(Buf);
922 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
923 HadParens = true;
924 getParser().Lex(); // Eat '('
925 }
926 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000927
928 switch (getLexer().getKind()) {
929 default:
930 return MatchOperand_NoMatch;
931 case AsmToken::Identifier:
932 StringRef Name = getLexer().getTok().getIdentifier();
933 unsigned RegNo = MatchRegisterName(Name);
934 if (RegNo == 0) {
935 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000936 if (RegNo == 0) {
937 if (HadParens)
938 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000939 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000940 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000941 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000942 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000943 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000944 SMLoc S = getLoc();
945 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000946 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000947 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000948 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000949
950 if (HadParens) {
951 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000952 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000953 }
954
Alex Bradbury04f06d92017-08-08 14:43:36 +0000955 return MatchOperand_Success;
956}
957
Ana Pazos9d6c5532018-10-04 21:50:54 +0000958OperandMatchResultTy
959RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
960 SMLoc S = getLoc();
961 const MCExpr *Res;
962
963 switch (getLexer().getKind()) {
964 default:
965 return MatchOperand_NoMatch;
966 case AsmToken::LParen:
967 case AsmToken::Minus:
968 case AsmToken::Plus:
969 case AsmToken::Integer:
970 case AsmToken::String: {
971 if (getParser().parseExpression(Res))
972 return MatchOperand_ParseFail;
973
974 auto *CE = dyn_cast<MCConstantExpr>(Res);
975 if (CE) {
976 int64_t Imm = CE->getValue();
977 if (isUInt<12>(Imm)) {
978 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
979 // Accept an immediate representing a named or un-named Sys Reg
980 // if the range is valid, regardless of the required features.
981 Operands.push_back(RISCVOperand::createSysReg(
982 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
983 return MatchOperand_Success;
984 }
985 }
986
987 Twine Msg = "immediate must be an integer in the range";
988 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
989 return MatchOperand_ParseFail;
990 }
991 case AsmToken::Identifier: {
992 StringRef Identifier;
993 if (getParser().parseIdentifier(Identifier))
994 return MatchOperand_ParseFail;
995
996 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
997 // Accept a named Sys Reg if the required features are present.
998 if (SysReg) {
999 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1000 Error(S, "system register use requires an option to be enabled");
1001 return MatchOperand_ParseFail;
1002 }
1003 Operands.push_back(RISCVOperand::createSysReg(
1004 Identifier, S, SysReg->Encoding, isRV64()));
1005 return MatchOperand_Success;
1006 }
1007
1008 Twine Msg = "operand must be a valid system register name "
1009 "or an integer in the range";
1010 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1011 return MatchOperand_ParseFail;
1012 }
1013 case AsmToken::Percent: {
1014 // Discard operand with modifier.
1015 Twine Msg = "immediate must be an integer in the range";
1016 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1017 return MatchOperand_ParseFail;
1018 }
1019 }
1020
1021 return MatchOperand_NoMatch;
1022}
1023
Alex Bradbury04f06d92017-08-08 14:43:36 +00001024OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001025 SMLoc S = getLoc();
1026 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1027 const MCExpr *Res;
1028
Alex Bradbury04f06d92017-08-08 14:43:36 +00001029 switch (getLexer().getKind()) {
1030 default:
1031 return MatchOperand_NoMatch;
1032 case AsmToken::LParen:
1033 case AsmToken::Minus:
1034 case AsmToken::Plus:
1035 case AsmToken::Integer:
1036 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001037 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001038 if (getParser().parseExpression(Res))
1039 return MatchOperand_ParseFail;
1040 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001041 case AsmToken::Percent:
1042 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001043 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001044
Alex Bradburya6e62482017-12-07 10:53:48 +00001045 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001046 return MatchOperand_Success;
1047}
1048
1049OperandMatchResultTy
1050RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1051 SMLoc S = getLoc();
1052 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1053
1054 if (getLexer().getKind() != AsmToken::Percent) {
1055 Error(getLoc(), "expected '%' for operand modifier");
1056 return MatchOperand_ParseFail;
1057 }
1058
1059 getParser().Lex(); // Eat '%'
1060
1061 if (getLexer().getKind() != AsmToken::Identifier) {
1062 Error(getLoc(), "expected valid identifier for operand modifier");
1063 return MatchOperand_ParseFail;
1064 }
1065 StringRef Identifier = getParser().getTok().getIdentifier();
1066 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1067 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1068 Error(getLoc(), "unrecognized operand modifier");
1069 return MatchOperand_ParseFail;
1070 }
1071
1072 getParser().Lex(); // Eat the identifier
1073 if (getLexer().getKind() != AsmToken::LParen) {
1074 Error(getLoc(), "expected '('");
1075 return MatchOperand_ParseFail;
1076 }
1077 getParser().Lex(); // Eat '('
1078
1079 const MCExpr *SubExpr;
1080 if (getParser().parseParenExpression(SubExpr, E)) {
1081 return MatchOperand_ParseFail;
1082 }
1083
1084 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001085 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001086 return MatchOperand_Success;
1087}
1088
Alex Bradbury68f73c12018-09-18 15:18:16 +00001089OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1090 SMLoc S = getLoc();
1091 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1092 const MCExpr *Res;
1093
1094 if (getLexer().getKind() != AsmToken::Identifier)
1095 return MatchOperand_NoMatch;
1096
1097 StringRef Identifier;
1098 if (getParser().parseIdentifier(Identifier))
1099 return MatchOperand_ParseFail;
1100
1101 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1102 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1103 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1104 return MatchOperand_Success;
1105}
1106
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001107OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1108 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1109 // both being acceptable forms. When parsing `jal ra, foo` this function
1110 // will be called for the `ra` register operand in an attempt to match the
1111 // single-operand alias. parseJALOffset must fail for this case. It would
1112 // seem logical to try parse the operand using parseImmediate and return
1113 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1114 // the second form rather than the first). We can't do this as there's no
1115 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1116 // is an identifier and is followed by a comma.
1117 if (getLexer().is(AsmToken::Identifier) &&
1118 getLexer().peekTok().is(AsmToken::Comma))
1119 return MatchOperand_NoMatch;
1120
1121 return parseImmediate(Operands);
1122}
1123
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001124OperandMatchResultTy
1125RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1126 if (getLexer().isNot(AsmToken::LParen)) {
1127 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001128 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001129 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001130
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001131 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001132 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001133
1134 if (parseRegister(Operands) != MatchOperand_Success) {
1135 Error(getLoc(), "expected register");
1136 return MatchOperand_ParseFail;
1137 }
1138
1139 if (getLexer().isNot(AsmToken::RParen)) {
1140 Error(getLoc(), "expected ')'");
1141 return MatchOperand_ParseFail;
1142 }
1143
1144 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001145 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001146
Alex Bradbury04f06d92017-08-08 14:43:36 +00001147 return MatchOperand_Success;
1148}
1149
Alex Bradburycd8688a2018-04-25 17:25:29 +00001150/// Looks at a token type and creates the relevant operand from this
1151/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001152/// true.
1153bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1154 // Check if the current operand has a custom associated parser, if so, try to
1155 // custom parse the operand, or fallback to the general approach.
1156 OperandMatchResultTy Result =
1157 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1158 if (Result == MatchOperand_Success)
1159 return false;
1160 if (Result == MatchOperand_ParseFail)
1161 return true;
1162
1163 // Attempt to parse token as a register.
1164 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001165 return false;
1166
1167 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001168 if (parseImmediate(Operands) == MatchOperand_Success) {
1169 // Parse memory base register if present
1170 if (getLexer().is(AsmToken::LParen))
1171 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001172 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001173 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001174
1175 // Finally we have exhausted all options and must declare defeat.
1176 Error(getLoc(), "unknown operand");
1177 return true;
1178}
1179
1180bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1181 StringRef Name, SMLoc NameLoc,
1182 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001183 // Ensure that if the instruction occurs when relaxation is enabled,
1184 // relocations are forced for the file. Ideally this would be done when there
1185 // is enough information to reliably determine if the instruction itself may
1186 // cause relaxations. Unfortunately instruction processing stage occurs in the
1187 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1188 // for the entire file.
1189 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1190 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1191 if (Assembler != nullptr) {
1192 RISCVAsmBackend &MAB =
1193 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1194 MAB.setForceRelocs();
1195 }
1196 }
1197
Alex Bradbury04f06d92017-08-08 14:43:36 +00001198 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001199 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001200
1201 // If there are no more operands, then finish
1202 if (getLexer().is(AsmToken::EndOfStatement))
1203 return false;
1204
1205 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001206 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001207 return true;
1208
1209 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001210 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001211 while (getLexer().is(AsmToken::Comma)) {
1212 // Consume comma token
1213 getLexer().Lex();
1214
1215 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001216 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001217 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001218
1219 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001220 }
1221
1222 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1223 SMLoc Loc = getLexer().getLoc();
1224 getParser().eatToEndOfStatement();
1225 return Error(Loc, "unexpected token");
1226 }
1227
1228 getParser().Lex(); // Consume the EndOfStatement.
1229 return false;
1230}
1231
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001232bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1233 RISCVMCExpr::VariantKind &Kind,
1234 int64_t &Addend) {
1235 Kind = RISCVMCExpr::VK_RISCV_None;
1236 Addend = 0;
1237
1238 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1239 Kind = RE->getKind();
1240 Expr = RE->getSubExpr();
1241 }
1242
1243 // It's a simple symbol reference or constant with no addend.
1244 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1245 return true;
1246
1247 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1248 if (!BE)
1249 return false;
1250
1251 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1252 return false;
1253
1254 if (BE->getOpcode() != MCBinaryExpr::Add &&
1255 BE->getOpcode() != MCBinaryExpr::Sub)
1256 return false;
1257
1258 // We are able to support the subtraction of two symbol references
1259 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1260 isa<MCSymbolRefExpr>(BE->getRHS()))
1261 return true;
1262
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001263 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001264 // on here than we can deal with.
1265 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1266 if (!AddendExpr)
1267 return false;
1268
1269 Addend = AddendExpr->getValue();
1270 if (BE->getOpcode() == MCBinaryExpr::Sub)
1271 Addend = -Addend;
1272
1273 // It's some symbol reference + a constant addend
1274 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1275}
1276
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001277bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1278 // This returns false if this function recognizes the directive
1279 // regardless of whether it is successfully handles or reports an
1280 // error. Otherwise it returns true to give the generic parser a
1281 // chance at recognizing it.
1282 StringRef IDVal = DirectiveID.getString();
1283
1284 if (IDVal == ".option")
1285 return parseDirectiveOption();
1286
1287 return true;
1288}
1289
1290bool RISCVAsmParser::parseDirectiveOption() {
1291 MCAsmParser &Parser = getParser();
1292 // Get the option token.
1293 AsmToken Tok = Parser.getTok();
1294 // At the moment only identifiers are supported.
1295 if (Tok.isNot(AsmToken::Identifier))
1296 return Error(Parser.getTok().getLoc(),
1297 "unexpected token, expected identifier");
1298
1299 StringRef Option = Tok.getIdentifier();
1300
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001301 if (Option == "push") {
1302 getTargetStreamer().emitDirectiveOptionPush();
1303
1304 Parser.Lex();
1305 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1306 return Error(Parser.getTok().getLoc(),
1307 "unexpected token, expected end of statement");
1308
1309 pushFeatureBits();
1310 return false;
1311 }
1312
1313 if (Option == "pop") {
1314 SMLoc StartLoc = Parser.getTok().getLoc();
1315 getTargetStreamer().emitDirectiveOptionPop();
1316
1317 Parser.Lex();
1318 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1319 return Error(Parser.getTok().getLoc(),
1320 "unexpected token, expected end of statement");
1321
1322 if (popFeatureBits())
1323 return Error(StartLoc, ".option pop with no .option push");
1324
1325 return false;
1326 }
1327
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001328 if (Option == "rvc") {
1329 getTargetStreamer().emitDirectiveOptionRVC();
1330
1331 Parser.Lex();
1332 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1333 return Error(Parser.getTok().getLoc(),
1334 "unexpected token, expected end of statement");
1335
1336 setFeatureBits(RISCV::FeatureStdExtC, "c");
1337 return false;
1338 }
1339
1340 if (Option == "norvc") {
1341 getTargetStreamer().emitDirectiveOptionNoRVC();
1342
1343 Parser.Lex();
1344 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1345 return Error(Parser.getTok().getLoc(),
1346 "unexpected token, expected end of statement");
1347
1348 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1349 return false;
1350 }
1351
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001352 if (Option == "relax") {
1353 getTargetStreamer().emitDirectiveOptionRelax();
1354
1355 Parser.Lex();
1356 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1357 return Error(Parser.getTok().getLoc(),
1358 "unexpected token, expected end of statement");
1359
1360 setFeatureBits(RISCV::FeatureRelax, "relax");
1361 return false;
1362 }
1363
1364 if (Option == "norelax") {
1365 getTargetStreamer().emitDirectiveOptionNoRelax();
1366
1367 Parser.Lex();
1368 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1369 return Error(Parser.getTok().getLoc(),
1370 "unexpected token, expected end of statement");
1371
1372 clearFeatureBits(RISCV::FeatureRelax, "relax");
1373 return false;
1374 }
1375
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001376 // Unknown option.
1377 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001378 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1379 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001380 Parser.eatToEndOfStatement();
1381 return false;
1382}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001383
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001384void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1385 MCInst CInst;
1386 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1387 CInst.setLoc(Inst.getLoc());
1388 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1389}
1390
1391void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1392 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001393 RISCVMatInt::InstSeq Seq;
1394 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001395
Alex Bradbury22c091f2018-11-15 10:11:31 +00001396 unsigned SrcReg = RISCV::X0;
1397 for (RISCVMatInt::Inst &Inst : Seq) {
1398 if (Inst.Opc == RISCV::LUI) {
1399 emitToStreamer(
1400 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1401 } else {
1402 emitToStreamer(
1403 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1404 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001405 }
1406
Alex Bradbury22c091f2018-11-15 10:11:31 +00001407 // Only the first instruction has X0 as its source.
1408 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001409 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001410}
1411
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001412void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1413 MCStreamer &Out) {
1414 // The local load address pseudo-instruction "lla" is used in PC-relative
1415 // addressing of symbols:
1416 // lla rdest, symbol
1417 // expands to
1418 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1419 // ADDI rdest, %pcrel_lo(TmpLabel)
1420 MCContext &Ctx = getContext();
1421
1422 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1423 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1424 Out.EmitLabel(TmpLabel);
1425
1426 MCOperand DestReg = Inst.getOperand(0);
1427 const RISCVMCExpr *Symbol = RISCVMCExpr::create(
1428 Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx);
1429
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001430 emitToStreamer(
1431 Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001432
1433 const MCExpr *RefToLinkTmpLabel =
1434 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1435 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1436
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001437 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1438 .addOperand(DestReg)
1439 .addOperand(DestReg)
1440 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001441}
1442
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001443bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1444 MCStreamer &Out) {
1445 Inst.setLoc(IDLoc);
1446
1447 if (Inst.getOpcode() == RISCV::PseudoLI) {
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001448 unsigned Reg = Inst.getOperand(0).getReg();
1449 const MCOperand &Op1 = Inst.getOperand(1);
1450 if (Op1.isExpr()) {
1451 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1452 // Just convert to an addi. This allows compatibility with gas.
1453 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1454 .addReg(Reg)
1455 .addReg(RISCV::X0)
1456 .addExpr(Op1.getExpr()));
1457 return false;
1458 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001459 int64_t Imm = Inst.getOperand(1).getImm();
1460 // On RV32 the immediate here can either be a signed or an unsigned
1461 // 32-bit number. Sign extension has to be performed to ensure that Imm
1462 // represents the expected signed 64-bit number.
1463 if (!isRV64())
1464 Imm = SignExtend64<32>(Imm);
1465 emitLoadImm(Reg, Imm, Out);
1466 return false;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001467 } else if (Inst.getOpcode() == RISCV::PseudoLLA) {
1468 emitLoadLocalAddress(Inst, IDLoc, Out);
1469 return false;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001470 }
1471
1472 emitToStreamer(Out, Inst);
1473 return false;
1474}
1475
Alex Bradbury04f06d92017-08-08 14:43:36 +00001476extern "C" void LLVMInitializeRISCVAsmParser() {
1477 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1478 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1479}