blob: 02f075a8532783e28c3dfe38842f4f7c91b53316 [file] [log] [blame]
Alex Bradbury04f06d92017-08-08 14:43:36 +00001//===-- RISCVAsmParser.cpp - Parse RISCV assembly to MCInst instructions --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alex Bradbury6758ecb2017-09-17 14:27:35 +000010#include "MCTargetDesc/RISCVBaseInfo.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000011#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000012#include "MCTargetDesc/RISCVMCTargetDesc.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/StringSwitch.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
20#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000021#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000024#include "llvm/Support/Casting.h"
25#include "llvm/Support/TargetRegistry.h"
26
27using namespace llvm;
28
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000029// Include the auto-generated portion of the compress emitter.
30#define GEN_COMPRESS_INSTR
31#include "RISCVGenCompressInstEmitter.inc"
32
Alex Bradbury04f06d92017-08-08 14:43:36 +000033namespace {
34struct RISCVOperand;
35
36class RISCVAsmParser : public MCTargetAsmParser {
37 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000038 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000039
Alex Bradbury7bc2a952017-12-07 10:46:23 +000040 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
41 unsigned Kind) override;
42
Alex Bradbury6758ecb2017-09-17 14:27:35 +000043 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury099c7202018-04-18 19:02:31 +000044 int Lower, int Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000045
Alex Bradbury04f06d92017-08-08 14:43:36 +000046 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
47 OperandVector &Operands, MCStreamer &Out,
48 uint64_t &ErrorInfo,
49 bool MatchingInlineAsm) override;
50
51 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
52
53 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
54 SMLoc NameLoc, OperandVector &Operands) override;
55
56 bool ParseDirective(AsmToken DirectiveID) override;
57
58// Auto-generated instruction matching functions
59#define GET_ASSEMBLER_HEADER
60#include "RISCVGenAsmMatcher.inc"
61
62 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +000063 OperandMatchResultTy parseRegister(OperandVector &Operands,
64 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000065 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +000066 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000067
68 bool parseOperand(OperandVector &Operands);
69
70public:
71 enum RISCVMatchResultTy {
72 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
73#define GET_OPERAND_DIAGNOSTIC_TYPES
74#include "RISCVGenAsmMatcher.inc"
75#undef GET_OPERAND_DIAGNOSTIC_TYPES
76 };
77
Alex Bradbury9d3f1252017-09-28 08:26:24 +000078 static bool classifySymbolRef(const MCExpr *Expr,
79 RISCVMCExpr::VariantKind &Kind,
80 int64_t &Addend);
81
Alex Bradbury04f06d92017-08-08 14:43:36 +000082 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
83 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +000084 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradbury04f06d92017-08-08 14:43:36 +000085 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
86 }
87};
88
89/// RISCVOperand - Instances of this class represent a parsed machine
90/// instruction
91struct RISCVOperand : public MCParsedAsmOperand {
92
93 enum KindTy {
94 Token,
95 Register,
96 Immediate,
97 } Kind;
98
Alex Bradburya6e62482017-12-07 10:53:48 +000099 bool IsRV64;
100
Alex Bradbury04f06d92017-08-08 14:43:36 +0000101 struct RegOp {
102 unsigned RegNum;
103 };
104
105 struct ImmOp {
106 const MCExpr *Val;
107 };
108
109 SMLoc StartLoc, EndLoc;
110 union {
111 StringRef Tok;
112 RegOp Reg;
113 ImmOp Imm;
114 };
115
116 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
117
118public:
119 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
120 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000121 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000122 StartLoc = o.StartLoc;
123 EndLoc = o.EndLoc;
124 switch (Kind) {
125 case Register:
126 Reg = o.Reg;
127 break;
128 case Immediate:
129 Imm = o.Imm;
130 break;
131 case Token:
132 Tok = o.Tok;
133 break;
134 }
135 }
136
137 bool isToken() const override { return Kind == Token; }
138 bool isReg() const override { return Kind == Register; }
139 bool isImm() const override { return Kind == Immediate; }
140 bool isMem() const override { return false; }
141
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000142 bool evaluateConstantImm(int64_t &Imm, RISCVMCExpr::VariantKind &VK) const {
143 const MCExpr *Val = getImm();
144 bool Ret = false;
145 if (auto *RE = dyn_cast<RISCVMCExpr>(Val)) {
146 Ret = RE->evaluateAsConstant(Imm);
147 VK = RE->getKind();
148 } else if (auto CE = dyn_cast<MCConstantExpr>(Val)) {
149 Ret = true;
150 VK = RISCVMCExpr::VK_RISCV_None;
151 Imm = CE->getValue();
152 }
153 return Ret;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000154 }
155
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000156 // True if operand is a symbol with no modifiers, or a constant with no
157 // modifiers and isShiftedInt<N-1, 1>(Op).
158 template <int N> bool isBareSimmNLsb0() const {
159 int64_t Imm;
160 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000161 if (!isImm())
162 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000163 bool IsConstantImm = evaluateConstantImm(Imm, VK);
164 bool IsValid;
165 if (!IsConstantImm)
166 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
167 else
168 IsValid = isShiftedInt<N - 1, 1>(Imm);
169 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000170 }
171
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000172 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
173
Shiva Chen98f93892018-04-25 14:18:55 +0000174 bool isBareSymbol() const {
175 int64_t Imm;
176 RISCVMCExpr::VariantKind VK;
177 // Must be of 'immediate' type but not a constant.
178 if (!isImm() || evaluateConstantImm(Imm, VK))
179 return false;
180 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
181 VK == RISCVMCExpr::VK_RISCV_None;
182 }
183
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000184 /// Return true if the operand is a valid for the fence instruction e.g.
185 /// ('iorw').
186 bool isFenceArg() const {
187 if (!isImm())
188 return false;
189 const MCExpr *Val = getImm();
190 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
191 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
192 return false;
193
194 StringRef Str = SVal->getSymbol().getName();
195 // Letters must be unique, taken from 'iorw', and in ascending order. This
196 // holds as long as each individual character is one of 'iorw' and is
197 // greater than the previous character.
198 char Prev = '\0';
199 for (char c : Str) {
200 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
201 return false;
202 if (c <= Prev)
203 return false;
204 Prev = c;
205 }
206 return true;
207 }
208
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000209 /// Return true if the operand is a valid floating point rounding mode.
210 bool isFRMArg() const {
211 if (!isImm())
212 return false;
213 const MCExpr *Val = getImm();
214 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
215 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
216 return false;
217
218 StringRef Str = SVal->getSymbol().getName();
219
220 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
221 }
222
Alex Bradburya6e62482017-12-07 10:53:48 +0000223 bool isUImmLog2XLen() const {
224 int64_t Imm;
225 RISCVMCExpr::VariantKind VK;
226 if (!isImm())
227 return false;
228 if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
229 return false;
230 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
231 }
232
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000233 bool isUImmLog2XLenNonZero() const {
234 int64_t Imm;
235 RISCVMCExpr::VariantKind VK;
236 if (!isImm())
237 return false;
238 if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
239 return false;
240 if (Imm == 0)
241 return false;
242 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
243 }
244
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000245 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000246 int64_t Imm;
247 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000248 if (!isImm())
249 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000250 bool IsConstantImm = evaluateConstantImm(Imm, VK);
251 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000252 }
253
Alex Bradbury60714f92017-12-13 09:32:55 +0000254 bool isUImm5NonZero() const {
255 int64_t Imm;
256 RISCVMCExpr::VariantKind VK;
257 if (!isImm())
258 return false;
259 bool IsConstantImm = evaluateConstantImm(Imm, VK);
260 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
261 VK == RISCVMCExpr::VK_RISCV_None;
262 }
263
Alex Bradbury581d6b02017-12-13 09:41:21 +0000264 bool isSImm6() const {
265 RISCVMCExpr::VariantKind VK;
266 int64_t Imm;
267 bool IsValid;
268 bool IsConstantImm = evaluateConstantImm(Imm, VK);
269 if (!IsConstantImm)
270 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
271 else
272 IsValid = isInt<6>(Imm);
273 return IsValid &&
274 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
275 }
276
Shiva Chenb22c1d22018-02-02 02:43:23 +0000277 bool isSImm6NonZero() const {
278 RISCVMCExpr::VariantKind VK;
279 int64_t Imm;
280 bool IsValid;
281 bool IsConstantImm = evaluateConstantImm(Imm, VK);
282 if (!IsConstantImm)
283 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
284 else
285 IsValid = ((Imm != 0) && isInt<6>(Imm));
286 return IsValid &&
287 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
288 }
289
Shiva Chen7c172422018-02-22 15:02:28 +0000290 bool isCLUIImm() const {
Alex Bradbury60714f92017-12-13 09:32:55 +0000291 int64_t Imm;
292 RISCVMCExpr::VariantKind VK;
293 bool IsConstantImm = evaluateConstantImm(Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000294 return IsConstantImm && (Imm != 0) &&
295 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
296 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000297 }
298
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000299 bool isUImm7Lsb00() const {
300 int64_t Imm;
301 RISCVMCExpr::VariantKind VK;
302 bool IsConstantImm = evaluateConstantImm(Imm, VK);
303 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
304 VK == RISCVMCExpr::VK_RISCV_None;
305 }
306
307 bool isUImm8Lsb00() const {
308 int64_t Imm;
309 RISCVMCExpr::VariantKind VK;
310 bool IsConstantImm = evaluateConstantImm(Imm, VK);
311 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
312 VK == RISCVMCExpr::VK_RISCV_None;
313 }
314
315 bool isUImm8Lsb000() const {
316 int64_t Imm;
317 RISCVMCExpr::VariantKind VK;
318 bool IsConstantImm = evaluateConstantImm(Imm, VK);
319 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
320 VK == RISCVMCExpr::VK_RISCV_None;
321 }
322
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000323 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
324
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000325 bool isUImm9Lsb000() const {
326 int64_t Imm;
327 RISCVMCExpr::VariantKind VK;
328 bool IsConstantImm = evaluateConstantImm(Imm, VK);
329 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
330 VK == RISCVMCExpr::VK_RISCV_None;
331 }
332
Alex Bradbury60714f92017-12-13 09:32:55 +0000333 bool isUImm10Lsb00NonZero() const {
334 int64_t Imm;
335 RISCVMCExpr::VariantKind VK;
336 bool IsConstantImm = evaluateConstantImm(Imm, VK);
337 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
338 VK == RISCVMCExpr::VK_RISCV_None;
339 }
340
Alex Bradbury04f06d92017-08-08 14:43:36 +0000341 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000342 RISCVMCExpr::VariantKind VK;
343 int64_t Imm;
344 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000345 if (!isImm())
346 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000347 bool IsConstantImm = evaluateConstantImm(Imm, VK);
348 if (!IsConstantImm)
349 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
350 else
351 IsValid = isInt<12>(Imm);
Ahmed Charles646ab872018-02-06 00:55:23 +0000352 return IsValid && (VK == RISCVMCExpr::VK_RISCV_None ||
353 VK == RISCVMCExpr::VK_RISCV_LO ||
354 VK == RISCVMCExpr::VK_RISCV_PCREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000355 }
356
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000357 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
358
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000359 bool isUImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000360 int64_t Imm;
361 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000362 if (!isImm())
363 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000364 bool IsConstantImm = evaluateConstantImm(Imm, VK);
365 return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000366 }
367
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000368 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000369
Shiva Chenb22c1d22018-02-02 02:43:23 +0000370 bool isSImm10Lsb0000NonZero() const {
Alex Bradbury60714f92017-12-13 09:32:55 +0000371 int64_t Imm;
372 RISCVMCExpr::VariantKind VK;
373 bool IsConstantImm = evaluateConstantImm(Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000374 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000375 VK == RISCVMCExpr::VK_RISCV_None;
376 }
377
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000378 bool isUImm20() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000379 RISCVMCExpr::VariantKind VK;
380 int64_t Imm;
381 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000382 if (!isImm())
383 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000384 bool IsConstantImm = evaluateConstantImm(Imm, VK);
385 if (!IsConstantImm)
386 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
387 else
388 IsValid = isUInt<20>(Imm);
389 return IsValid && (VK == RISCVMCExpr::VK_RISCV_None ||
390 VK == RISCVMCExpr::VK_RISCV_HI ||
391 VK == RISCVMCExpr::VK_RISCV_PCREL_HI);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000392 }
393
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000394 bool isSImm21Lsb0() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000395
Alex Bradbury04f06d92017-08-08 14:43:36 +0000396 /// getStartLoc - Gets location of the first token of this operand
397 SMLoc getStartLoc() const override { return StartLoc; }
398 /// getEndLoc - Gets location of the last token of this operand
399 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000400 /// True if this operand is for an RV64 instruction
401 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000402
403 unsigned getReg() const override {
404 assert(Kind == Register && "Invalid type access!");
405 return Reg.RegNum;
406 }
407
408 const MCExpr *getImm() const {
409 assert(Kind == Immediate && "Invalid type access!");
410 return Imm.Val;
411 }
412
413 StringRef getToken() const {
414 assert(Kind == Token && "Invalid type access!");
415 return Tok;
416 }
417
418 void print(raw_ostream &OS) const override {
419 switch (Kind) {
420 case Immediate:
421 OS << *getImm();
422 break;
423 case Register:
424 OS << "<register x";
425 OS << getReg() << ">";
426 break;
427 case Token:
428 OS << "'" << getToken() << "'";
429 break;
430 }
431 }
432
Alex Bradburya6e62482017-12-07 10:53:48 +0000433 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
434 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000435 auto Op = make_unique<RISCVOperand>(Token);
436 Op->Tok = Str;
437 Op->StartLoc = S;
438 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000439 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000440 return Op;
441 }
442
443 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000444 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000445 auto Op = make_unique<RISCVOperand>(Register);
446 Op->Reg.RegNum = RegNo;
447 Op->StartLoc = S;
448 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000449 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000450 return Op;
451 }
452
453 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000454 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000455 auto Op = make_unique<RISCVOperand>(Immediate);
456 Op->Imm.Val = Val;
457 Op->StartLoc = S;
458 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000459 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000460 return Op;
461 }
462
463 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
464 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000465 int64_t Imm = 0;
466 bool IsConstant = false;
467 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
468 IsConstant = RE->evaluateAsConstant(Imm);
469 } else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
470 IsConstant = true;
471 Imm = CE->getValue();
472 }
473
474 if (IsConstant)
475 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000476 else
477 Inst.addOperand(MCOperand::createExpr(Expr));
478 }
479
480 // Used by the TableGen Code
481 void addRegOperands(MCInst &Inst, unsigned N) const {
482 assert(N == 1 && "Invalid number of operands!");
483 Inst.addOperand(MCOperand::createReg(getReg()));
484 }
485
486 void addImmOperands(MCInst &Inst, unsigned N) const {
487 assert(N == 1 && "Invalid number of operands!");
488 addExpr(Inst, getImm());
489 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000490
491 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
492 assert(N == 1 && "Invalid number of operands!");
493 // isFenceArg has validated the operand, meaning this cast is safe
494 auto SE = cast<MCSymbolRefExpr>(getImm());
495
496 unsigned Imm = 0;
497 for (char c : SE->getSymbol().getName()) {
498 switch (c) {
499 default: llvm_unreachable("FenceArg must contain only [iorw]");
500 case 'i': Imm |= RISCVFenceField::I; break;
501 case 'o': Imm |= RISCVFenceField::O; break;
502 case 'r': Imm |= RISCVFenceField::R; break;
503 case 'w': Imm |= RISCVFenceField::W; break;
504 }
505 }
506 Inst.addOperand(MCOperand::createImm(Imm));
507 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000508
509 // Returns the rounding mode represented by this RISCVOperand. Should only
510 // be called after checking isFRMArg.
511 RISCVFPRndMode::RoundingMode getRoundingMode() const {
512 // isFRMArg has validated the operand, meaning this cast is safe.
513 auto SE = cast<MCSymbolRefExpr>(getImm());
514 RISCVFPRndMode::RoundingMode FRM =
515 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
516 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
517 return FRM;
518 }
519
520 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
521 assert(N == 1 && "Invalid number of operands!");
522 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
523 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000524};
525} // end anonymous namespace.
526
527#define GET_REGISTER_MATCHER
528#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000529#include "RISCVGenAsmMatcher.inc"
530
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000531// Return the matching FPR64 register for the given FPR32.
532// FIXME: Ideally this function could be removed in favour of using
533// information from TableGen.
534unsigned convertFPR32ToFPR64(unsigned Reg) {
535 switch (Reg) {
536 default:
537 llvm_unreachable("Not a recognised FPR32 register");
538 case RISCV::F0_32: return RISCV::F0_64;
539 case RISCV::F1_32: return RISCV::F1_64;
540 case RISCV::F2_32: return RISCV::F2_64;
541 case RISCV::F3_32: return RISCV::F3_64;
542 case RISCV::F4_32: return RISCV::F4_64;
543 case RISCV::F5_32: return RISCV::F5_64;
544 case RISCV::F6_32: return RISCV::F6_64;
545 case RISCV::F7_32: return RISCV::F7_64;
546 case RISCV::F8_32: return RISCV::F8_64;
547 case RISCV::F9_32: return RISCV::F9_64;
548 case RISCV::F10_32: return RISCV::F10_64;
549 case RISCV::F11_32: return RISCV::F11_64;
550 case RISCV::F12_32: return RISCV::F12_64;
551 case RISCV::F13_32: return RISCV::F13_64;
552 case RISCV::F14_32: return RISCV::F14_64;
553 case RISCV::F15_32: return RISCV::F15_64;
554 case RISCV::F16_32: return RISCV::F16_64;
555 case RISCV::F17_32: return RISCV::F17_64;
556 case RISCV::F18_32: return RISCV::F18_64;
557 case RISCV::F19_32: return RISCV::F19_64;
558 case RISCV::F20_32: return RISCV::F20_64;
559 case RISCV::F21_32: return RISCV::F21_64;
560 case RISCV::F22_32: return RISCV::F22_64;
561 case RISCV::F23_32: return RISCV::F23_64;
562 case RISCV::F24_32: return RISCV::F24_64;
563 case RISCV::F25_32: return RISCV::F25_64;
564 case RISCV::F26_32: return RISCV::F26_64;
565 case RISCV::F27_32: return RISCV::F27_64;
566 case RISCV::F28_32: return RISCV::F28_64;
567 case RISCV::F29_32: return RISCV::F29_64;
568 case RISCV::F30_32: return RISCV::F30_64;
569 case RISCV::F31_32: return RISCV::F31_64;
570 }
571}
572
573unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
574 unsigned Kind) {
575 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
576 if (!Op.isReg())
577 return Match_InvalidOperand;
578
579 unsigned Reg = Op.getReg();
580 bool IsRegFPR32 =
581 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000582 bool IsRegFPR32C =
583 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000584
585 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000586 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
587 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
588 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000589 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
590 return Match_Success;
591 }
592 return Match_InvalidOperand;
593}
594
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000595bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury099c7202018-04-18 19:02:31 +0000596 OperandVector &Operands, uint64_t ErrorInfo, int Lower, int Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000597 Twine Msg = "immediate must be an integer in the range") {
598 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
599 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
600}
601
Alex Bradbury04f06d92017-08-08 14:43:36 +0000602bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
603 OperandVector &Operands,
604 MCStreamer &Out,
605 uint64_t &ErrorInfo,
606 bool MatchingInlineAsm) {
607 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000608
609 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
610 default:
611 break;
Alex Bradbury099c7202018-04-18 19:02:31 +0000612 case Match_Success: {
613 MCInst CInst;
614 bool Res = compressInst(CInst, Inst, getSTI(), Out.getContext());
615 CInst.setLoc(IDLoc);
616 Inst.setLoc(IDLoc);
617 Out.EmitInstruction((Res ? CInst : Inst), getSTI());
618 return false;
619 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000620 case Match_MissingFeature:
621 return Error(IDLoc, "instruction use requires an option to be enabled");
622 case Match_MnemonicFail:
623 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000624 case Match_InvalidOperand: {
625 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000626 if (ErrorInfo != ~0U) {
627 if (ErrorInfo >= Operands.size())
628 return Error(ErrorLoc, "too few operands for instruction");
629
630 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
631 if (ErrorLoc == SMLoc())
632 ErrorLoc = IDLoc;
633 }
634 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000635 }
Alex Bradburya6e62482017-12-07 10:53:48 +0000636 case Match_InvalidUImmLog2XLen:
637 if (isRV64())
638 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
639 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000640 case Match_InvalidUImmLog2XLenNonZero:
641 if (isRV64())
642 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
643 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000644 case Match_InvalidUImm5:
645 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000646 case Match_InvalidSImm6:
647 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
648 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000649 case Match_InvalidSImm6NonZero:
650 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
651 (1 << 5) - 1,
652 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000653 case Match_InvalidCLUIImm:
654 return generateImmOutOfRangeError(
655 Operands, ErrorInfo, 1, (1 << 5) - 1,
656 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000657 case Match_InvalidUImm7Lsb00:
658 return generateImmOutOfRangeError(
659 Operands, ErrorInfo, 0, (1 << 7) - 4,
660 "immediate must be a multiple of 4 bytes in the range");
661 case Match_InvalidUImm8Lsb00:
662 return generateImmOutOfRangeError(
663 Operands, ErrorInfo, 0, (1 << 8) - 4,
664 "immediate must be a multiple of 4 bytes in the range");
665 case Match_InvalidUImm8Lsb000:
666 return generateImmOutOfRangeError(
667 Operands, ErrorInfo, 0, (1 << 8) - 8,
668 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000669 case Match_InvalidSImm9Lsb0:
670 return generateImmOutOfRangeError(
671 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
672 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000673 case Match_InvalidUImm9Lsb000:
674 return generateImmOutOfRangeError(
675 Operands, ErrorInfo, 0, (1 << 9) - 8,
676 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000677 case Match_InvalidUImm10Lsb00NonZero:
678 return generateImmOutOfRangeError(
679 Operands, ErrorInfo, 4, (1 << 10) - 4,
680 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000681 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000682 return generateImmOutOfRangeError(
683 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000684 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000685 case Match_InvalidSImm12:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000686 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 11),
687 (1 << 11) - 1);
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000688 case Match_InvalidSImm12Lsb0:
689 return generateImmOutOfRangeError(
690 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
691 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000692 case Match_InvalidUImm12:
693 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1);
694 case Match_InvalidSImm13Lsb0:
695 return generateImmOutOfRangeError(
696 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
697 "immediate must be a multiple of 2 bytes in the range");
698 case Match_InvalidUImm20:
699 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1);
700 case Match_InvalidSImm21Lsb0:
701 return generateImmOutOfRangeError(
702 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
703 "immediate must be a multiple of 2 bytes in the range");
704 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000705 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000706 return Error(
707 ErrorLoc,
708 "operand must be formed of letters selected in-order from 'iorw'");
709 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000710 case Match_InvalidFRMArg: {
711 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
712 return Error(
713 ErrorLoc,
714 "operand must be a valid floating point rounding mode mnemonic");
715 }
Shiva Chen98f93892018-04-25 14:18:55 +0000716 case Match_InvalidBareSymbol: {
717 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
718 return Error(ErrorLoc, "operand must be a bare symbol name");
719 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000720 }
721
722 llvm_unreachable("Unknown match type detected!");
723}
724
725bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
726 SMLoc &EndLoc) {
727 const AsmToken &Tok = getParser().getTok();
728 StartLoc = Tok.getLoc();
729 EndLoc = Tok.getEndLoc();
730 RegNo = 0;
731 StringRef Name = getLexer().getTok().getIdentifier();
732
733 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
734 getParser().Lex(); // Eat identifier token.
735 return false;
736 }
737
738 return Error(StartLoc, "invalid register name");
739}
740
Alex Bradbury8c345c52017-11-09 15:00:03 +0000741OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
742 bool AllowParens) {
743 SMLoc FirstS = getLoc();
744 bool HadParens = false;
745 AsmToken Buf[2];
746
747 // If this a parenthesised register name is allowed, parse it atomically
748 if (AllowParens && getLexer().is(AsmToken::LParen)) {
749 size_t ReadCount = getLexer().peekTokens(Buf);
750 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
751 HadParens = true;
752 getParser().Lex(); // Eat '('
753 }
754 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000755
756 switch (getLexer().getKind()) {
757 default:
758 return MatchOperand_NoMatch;
759 case AsmToken::Identifier:
760 StringRef Name = getLexer().getTok().getIdentifier();
761 unsigned RegNo = MatchRegisterName(Name);
762 if (RegNo == 0) {
763 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000764 if (RegNo == 0) {
765 if (HadParens)
766 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000767 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000768 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000769 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000770 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000771 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000772 SMLoc S = getLoc();
773 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000774 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000775 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000776 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000777
778 if (HadParens) {
779 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000780 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000781 }
782
Alex Bradbury04f06d92017-08-08 14:43:36 +0000783 return MatchOperand_Success;
784}
785
786OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000787 SMLoc S = getLoc();
788 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
789 const MCExpr *Res;
790
Alex Bradbury04f06d92017-08-08 14:43:36 +0000791 switch (getLexer().getKind()) {
792 default:
793 return MatchOperand_NoMatch;
794 case AsmToken::LParen:
795 case AsmToken::Minus:
796 case AsmToken::Plus:
797 case AsmToken::Integer:
798 case AsmToken::String:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000799 if (getParser().parseExpression(Res))
800 return MatchOperand_ParseFail;
801 break;
802 case AsmToken::Identifier: {
803 StringRef Identifier;
804 if (getParser().parseIdentifier(Identifier))
805 return MatchOperand_ParseFail;
806 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
807 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury04f06d92017-08-08 14:43:36 +0000808 break;
809 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000810 case AsmToken::Percent:
811 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000812 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000813
Alex Bradburya6e62482017-12-07 10:53:48 +0000814 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000815 return MatchOperand_Success;
816}
817
818OperandMatchResultTy
819RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
820 SMLoc S = getLoc();
821 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
822
823 if (getLexer().getKind() != AsmToken::Percent) {
824 Error(getLoc(), "expected '%' for operand modifier");
825 return MatchOperand_ParseFail;
826 }
827
828 getParser().Lex(); // Eat '%'
829
830 if (getLexer().getKind() != AsmToken::Identifier) {
831 Error(getLoc(), "expected valid identifier for operand modifier");
832 return MatchOperand_ParseFail;
833 }
834 StringRef Identifier = getParser().getTok().getIdentifier();
835 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
836 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
837 Error(getLoc(), "unrecognized operand modifier");
838 return MatchOperand_ParseFail;
839 }
840
841 getParser().Lex(); // Eat the identifier
842 if (getLexer().getKind() != AsmToken::LParen) {
843 Error(getLoc(), "expected '('");
844 return MatchOperand_ParseFail;
845 }
846 getParser().Lex(); // Eat '('
847
848 const MCExpr *SubExpr;
849 if (getParser().parseParenExpression(SubExpr, E)) {
850 return MatchOperand_ParseFail;
851 }
852
853 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +0000854 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000855 return MatchOperand_Success;
856}
857
858OperandMatchResultTy
859RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
860 if (getLexer().isNot(AsmToken::LParen)) {
861 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000862 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000863 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000864
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000865 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +0000866 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000867
868 if (parseRegister(Operands) != MatchOperand_Success) {
869 Error(getLoc(), "expected register");
870 return MatchOperand_ParseFail;
871 }
872
873 if (getLexer().isNot(AsmToken::RParen)) {
874 Error(getLoc(), "expected ')'");
875 return MatchOperand_ParseFail;
876 }
877
878 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000879 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000880
Alex Bradbury04f06d92017-08-08 14:43:36 +0000881 return MatchOperand_Success;
882}
883
884/// Looks at a token type and creates the relevant operand
885/// from this information, adding to Operands.
886/// If operand was parsed, returns false, else true.
887bool RISCVAsmParser::parseOperand(OperandVector &Operands) {
888 // Attempt to parse token as register
Alex Bradbury8c345c52017-11-09 15:00:03 +0000889 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +0000890 return false;
891
892 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000893 if (parseImmediate(Operands) == MatchOperand_Success) {
894 // Parse memory base register if present
895 if (getLexer().is(AsmToken::LParen))
896 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000897 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000898 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000899
900 // Finally we have exhausted all options and must declare defeat.
901 Error(getLoc(), "unknown operand");
902 return true;
903}
904
905bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
906 StringRef Name, SMLoc NameLoc,
907 OperandVector &Operands) {
908 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +0000909 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000910
911 // If there are no more operands, then finish
912 if (getLexer().is(AsmToken::EndOfStatement))
913 return false;
914
915 // Parse first operand
916 if (parseOperand(Operands))
917 return true;
918
919 // Parse until end of statement, consuming commas between operands
920 while (getLexer().is(AsmToken::Comma)) {
921 // Consume comma token
922 getLexer().Lex();
923
924 // Parse next operand
925 if (parseOperand(Operands))
926 return true;
927 }
928
929 if (getLexer().isNot(AsmToken::EndOfStatement)) {
930 SMLoc Loc = getLexer().getLoc();
931 getParser().eatToEndOfStatement();
932 return Error(Loc, "unexpected token");
933 }
934
935 getParser().Lex(); // Consume the EndOfStatement.
936 return false;
937}
938
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000939bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
940 RISCVMCExpr::VariantKind &Kind,
941 int64_t &Addend) {
942 Kind = RISCVMCExpr::VK_RISCV_None;
943 Addend = 0;
944
945 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
946 Kind = RE->getKind();
947 Expr = RE->getSubExpr();
948 }
949
950 // It's a simple symbol reference or constant with no addend.
951 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
952 return true;
953
954 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
955 if (!BE)
956 return false;
957
958 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
959 return false;
960
961 if (BE->getOpcode() != MCBinaryExpr::Add &&
962 BE->getOpcode() != MCBinaryExpr::Sub)
963 return false;
964
965 // We are able to support the subtraction of two symbol references
966 if (BE->getOpcode() == MCBinaryExpr::Sub &&
967 isa<MCSymbolRefExpr>(BE->getRHS()))
968 return true;
969
Hiroshi Inoue9ff23802018-04-09 04:37:53 +0000970 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000971 // on here than we can deal with.
972 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
973 if (!AddendExpr)
974 return false;
975
976 Addend = AddendExpr->getValue();
977 if (BE->getOpcode() == MCBinaryExpr::Sub)
978 Addend = -Addend;
979
980 // It's some symbol reference + a constant addend
981 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
982}
983
Alex Bradbury04f06d92017-08-08 14:43:36 +0000984bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) { return true; }
985
986extern "C" void LLVMInitializeRISCVAsmParser() {
987 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
988 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
989}