blob: 521d72b546dcf75ae9e547b45ccd58a1f5775624 [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
174 /// Return true if the operand is a valid for the fence instruction e.g.
175 /// ('iorw').
176 bool isFenceArg() const {
177 if (!isImm())
178 return false;
179 const MCExpr *Val = getImm();
180 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
181 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
182 return false;
183
184 StringRef Str = SVal->getSymbol().getName();
185 // Letters must be unique, taken from 'iorw', and in ascending order. This
186 // holds as long as each individual character is one of 'iorw' and is
187 // greater than the previous character.
188 char Prev = '\0';
189 for (char c : Str) {
190 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
191 return false;
192 if (c <= Prev)
193 return false;
194 Prev = c;
195 }
196 return true;
197 }
198
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000199 /// Return true if the operand is a valid floating point rounding mode.
200 bool isFRMArg() const {
201 if (!isImm())
202 return false;
203 const MCExpr *Val = getImm();
204 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
205 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
206 return false;
207
208 StringRef Str = SVal->getSymbol().getName();
209
210 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
211 }
212
Alex Bradburya6e62482017-12-07 10:53:48 +0000213 bool isUImmLog2XLen() const {
214 int64_t Imm;
215 RISCVMCExpr::VariantKind VK;
216 if (!isImm())
217 return false;
218 if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
219 return false;
220 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
221 }
222
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000223 bool isUImmLog2XLenNonZero() 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 if (Imm == 0)
231 return false;
232 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
233 }
234
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000235 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000236 int64_t Imm;
237 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000238 if (!isImm())
239 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000240 bool IsConstantImm = evaluateConstantImm(Imm, VK);
241 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000242 }
243
Alex Bradbury60714f92017-12-13 09:32:55 +0000244 bool isUImm5NonZero() const {
245 int64_t Imm;
246 RISCVMCExpr::VariantKind VK;
247 if (!isImm())
248 return false;
249 bool IsConstantImm = evaluateConstantImm(Imm, VK);
250 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
251 VK == RISCVMCExpr::VK_RISCV_None;
252 }
253
Alex Bradbury581d6b02017-12-13 09:41:21 +0000254 bool isSImm6() const {
255 RISCVMCExpr::VariantKind VK;
256 int64_t Imm;
257 bool IsValid;
258 bool IsConstantImm = evaluateConstantImm(Imm, VK);
259 if (!IsConstantImm)
260 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
261 else
262 IsValid = isInt<6>(Imm);
263 return IsValid &&
264 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
265 }
266
Shiva Chenb22c1d22018-02-02 02:43:23 +0000267 bool isSImm6NonZero() const {
268 RISCVMCExpr::VariantKind VK;
269 int64_t Imm;
270 bool IsValid;
271 bool IsConstantImm = evaluateConstantImm(Imm, VK);
272 if (!IsConstantImm)
273 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
274 else
275 IsValid = ((Imm != 0) && isInt<6>(Imm));
276 return IsValid &&
277 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
278 }
279
Shiva Chen7c172422018-02-22 15:02:28 +0000280 bool isCLUIImm() const {
Alex Bradbury60714f92017-12-13 09:32:55 +0000281 int64_t Imm;
282 RISCVMCExpr::VariantKind VK;
283 bool IsConstantImm = evaluateConstantImm(Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000284 return IsConstantImm && (Imm != 0) &&
285 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
286 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000287 }
288
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000289 bool isUImm7Lsb00() const {
290 int64_t Imm;
291 RISCVMCExpr::VariantKind VK;
292 bool IsConstantImm = evaluateConstantImm(Imm, VK);
293 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
294 VK == RISCVMCExpr::VK_RISCV_None;
295 }
296
297 bool isUImm8Lsb00() const {
298 int64_t Imm;
299 RISCVMCExpr::VariantKind VK;
300 bool IsConstantImm = evaluateConstantImm(Imm, VK);
301 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
302 VK == RISCVMCExpr::VK_RISCV_None;
303 }
304
305 bool isUImm8Lsb000() const {
306 int64_t Imm;
307 RISCVMCExpr::VariantKind VK;
308 bool IsConstantImm = evaluateConstantImm(Imm, VK);
309 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
310 VK == RISCVMCExpr::VK_RISCV_None;
311 }
312
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000313 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
314
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000315 bool isUImm9Lsb000() const {
316 int64_t Imm;
317 RISCVMCExpr::VariantKind VK;
318 bool IsConstantImm = evaluateConstantImm(Imm, VK);
319 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
320 VK == RISCVMCExpr::VK_RISCV_None;
321 }
322
Alex Bradbury60714f92017-12-13 09:32:55 +0000323 bool isUImm10Lsb00NonZero() const {
324 int64_t Imm;
325 RISCVMCExpr::VariantKind VK;
326 bool IsConstantImm = evaluateConstantImm(Imm, VK);
327 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
328 VK == RISCVMCExpr::VK_RISCV_None;
329 }
330
Alex Bradbury04f06d92017-08-08 14:43:36 +0000331 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000332 RISCVMCExpr::VariantKind VK;
333 int64_t Imm;
334 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000335 if (!isImm())
336 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000337 bool IsConstantImm = evaluateConstantImm(Imm, VK);
338 if (!IsConstantImm)
339 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
340 else
341 IsValid = isInt<12>(Imm);
Ahmed Charles646ab872018-02-06 00:55:23 +0000342 return IsValid && (VK == RISCVMCExpr::VK_RISCV_None ||
343 VK == RISCVMCExpr::VK_RISCV_LO ||
344 VK == RISCVMCExpr::VK_RISCV_PCREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000345 }
346
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000347 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
348
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000349 bool isUImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000350 int64_t Imm;
351 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000352 if (!isImm())
353 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000354 bool IsConstantImm = evaluateConstantImm(Imm, VK);
355 return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000356 }
357
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000358 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000359
Shiva Chenb22c1d22018-02-02 02:43:23 +0000360 bool isSImm10Lsb0000NonZero() const {
Alex Bradbury60714f92017-12-13 09:32:55 +0000361 int64_t Imm;
362 RISCVMCExpr::VariantKind VK;
363 bool IsConstantImm = evaluateConstantImm(Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000364 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000365 VK == RISCVMCExpr::VK_RISCV_None;
366 }
367
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000368 bool isUImm20() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000369 RISCVMCExpr::VariantKind VK;
370 int64_t Imm;
371 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000372 if (!isImm())
373 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000374 bool IsConstantImm = evaluateConstantImm(Imm, VK);
375 if (!IsConstantImm)
376 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
377 else
378 IsValid = isUInt<20>(Imm);
379 return IsValid && (VK == RISCVMCExpr::VK_RISCV_None ||
380 VK == RISCVMCExpr::VK_RISCV_HI ||
381 VK == RISCVMCExpr::VK_RISCV_PCREL_HI);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000382 }
383
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000384 bool isSImm21Lsb0() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000385
Alex Bradbury04f06d92017-08-08 14:43:36 +0000386 /// getStartLoc - Gets location of the first token of this operand
387 SMLoc getStartLoc() const override { return StartLoc; }
388 /// getEndLoc - Gets location of the last token of this operand
389 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000390 /// True if this operand is for an RV64 instruction
391 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000392
393 unsigned getReg() const override {
394 assert(Kind == Register && "Invalid type access!");
395 return Reg.RegNum;
396 }
397
398 const MCExpr *getImm() const {
399 assert(Kind == Immediate && "Invalid type access!");
400 return Imm.Val;
401 }
402
403 StringRef getToken() const {
404 assert(Kind == Token && "Invalid type access!");
405 return Tok;
406 }
407
408 void print(raw_ostream &OS) const override {
409 switch (Kind) {
410 case Immediate:
411 OS << *getImm();
412 break;
413 case Register:
414 OS << "<register x";
415 OS << getReg() << ">";
416 break;
417 case Token:
418 OS << "'" << getToken() << "'";
419 break;
420 }
421 }
422
Alex Bradburya6e62482017-12-07 10:53:48 +0000423 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
424 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000425 auto Op = make_unique<RISCVOperand>(Token);
426 Op->Tok = Str;
427 Op->StartLoc = S;
428 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000429 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000430 return Op;
431 }
432
433 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000434 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000435 auto Op = make_unique<RISCVOperand>(Register);
436 Op->Reg.RegNum = RegNo;
437 Op->StartLoc = S;
438 Op->EndLoc = E;
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> createImm(const MCExpr *Val, 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>(Immediate);
446 Op->Imm.Val = Val;
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 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
454 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000455 int64_t Imm = 0;
456 bool IsConstant = false;
457 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
458 IsConstant = RE->evaluateAsConstant(Imm);
459 } else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
460 IsConstant = true;
461 Imm = CE->getValue();
462 }
463
464 if (IsConstant)
465 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000466 else
467 Inst.addOperand(MCOperand::createExpr(Expr));
468 }
469
470 // Used by the TableGen Code
471 void addRegOperands(MCInst &Inst, unsigned N) const {
472 assert(N == 1 && "Invalid number of operands!");
473 Inst.addOperand(MCOperand::createReg(getReg()));
474 }
475
476 void addImmOperands(MCInst &Inst, unsigned N) const {
477 assert(N == 1 && "Invalid number of operands!");
478 addExpr(Inst, getImm());
479 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000480
481 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
482 assert(N == 1 && "Invalid number of operands!");
483 // isFenceArg has validated the operand, meaning this cast is safe
484 auto SE = cast<MCSymbolRefExpr>(getImm());
485
486 unsigned Imm = 0;
487 for (char c : SE->getSymbol().getName()) {
488 switch (c) {
489 default: llvm_unreachable("FenceArg must contain only [iorw]");
490 case 'i': Imm |= RISCVFenceField::I; break;
491 case 'o': Imm |= RISCVFenceField::O; break;
492 case 'r': Imm |= RISCVFenceField::R; break;
493 case 'w': Imm |= RISCVFenceField::W; break;
494 }
495 }
496 Inst.addOperand(MCOperand::createImm(Imm));
497 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000498
499 // Returns the rounding mode represented by this RISCVOperand. Should only
500 // be called after checking isFRMArg.
501 RISCVFPRndMode::RoundingMode getRoundingMode() const {
502 // isFRMArg has validated the operand, meaning this cast is safe.
503 auto SE = cast<MCSymbolRefExpr>(getImm());
504 RISCVFPRndMode::RoundingMode FRM =
505 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
506 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
507 return FRM;
508 }
509
510 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
511 assert(N == 1 && "Invalid number of operands!");
512 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
513 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000514};
515} // end anonymous namespace.
516
517#define GET_REGISTER_MATCHER
518#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000519#include "RISCVGenAsmMatcher.inc"
520
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000521// Return the matching FPR64 register for the given FPR32.
522// FIXME: Ideally this function could be removed in favour of using
523// information from TableGen.
524unsigned convertFPR32ToFPR64(unsigned Reg) {
525 switch (Reg) {
526 default:
527 llvm_unreachable("Not a recognised FPR32 register");
528 case RISCV::F0_32: return RISCV::F0_64;
529 case RISCV::F1_32: return RISCV::F1_64;
530 case RISCV::F2_32: return RISCV::F2_64;
531 case RISCV::F3_32: return RISCV::F3_64;
532 case RISCV::F4_32: return RISCV::F4_64;
533 case RISCV::F5_32: return RISCV::F5_64;
534 case RISCV::F6_32: return RISCV::F6_64;
535 case RISCV::F7_32: return RISCV::F7_64;
536 case RISCV::F8_32: return RISCV::F8_64;
537 case RISCV::F9_32: return RISCV::F9_64;
538 case RISCV::F10_32: return RISCV::F10_64;
539 case RISCV::F11_32: return RISCV::F11_64;
540 case RISCV::F12_32: return RISCV::F12_64;
541 case RISCV::F13_32: return RISCV::F13_64;
542 case RISCV::F14_32: return RISCV::F14_64;
543 case RISCV::F15_32: return RISCV::F15_64;
544 case RISCV::F16_32: return RISCV::F16_64;
545 case RISCV::F17_32: return RISCV::F17_64;
546 case RISCV::F18_32: return RISCV::F18_64;
547 case RISCV::F19_32: return RISCV::F19_64;
548 case RISCV::F20_32: return RISCV::F20_64;
549 case RISCV::F21_32: return RISCV::F21_64;
550 case RISCV::F22_32: return RISCV::F22_64;
551 case RISCV::F23_32: return RISCV::F23_64;
552 case RISCV::F24_32: return RISCV::F24_64;
553 case RISCV::F25_32: return RISCV::F25_64;
554 case RISCV::F26_32: return RISCV::F26_64;
555 case RISCV::F27_32: return RISCV::F27_64;
556 case RISCV::F28_32: return RISCV::F28_64;
557 case RISCV::F29_32: return RISCV::F29_64;
558 case RISCV::F30_32: return RISCV::F30_64;
559 case RISCV::F31_32: return RISCV::F31_64;
560 }
561}
562
563unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
564 unsigned Kind) {
565 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
566 if (!Op.isReg())
567 return Match_InvalidOperand;
568
569 unsigned Reg = Op.getReg();
570 bool IsRegFPR32 =
571 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000572 bool IsRegFPR32C =
573 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000574
575 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000576 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
577 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
578 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000579 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
580 return Match_Success;
581 }
582 return Match_InvalidOperand;
583}
584
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000585bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury099c7202018-04-18 19:02:31 +0000586 OperandVector &Operands, uint64_t ErrorInfo, int Lower, int Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000587 Twine Msg = "immediate must be an integer in the range") {
588 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
589 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
590}
591
Alex Bradbury04f06d92017-08-08 14:43:36 +0000592bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
593 OperandVector &Operands,
594 MCStreamer &Out,
595 uint64_t &ErrorInfo,
596 bool MatchingInlineAsm) {
597 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000598
599 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
600 default:
601 break;
Alex Bradbury099c7202018-04-18 19:02:31 +0000602 case Match_Success: {
603 MCInst CInst;
604 bool Res = compressInst(CInst, Inst, getSTI(), Out.getContext());
605 CInst.setLoc(IDLoc);
606 Inst.setLoc(IDLoc);
607 Out.EmitInstruction((Res ? CInst : Inst), getSTI());
608 return false;
609 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000610 case Match_MissingFeature:
611 return Error(IDLoc, "instruction use requires an option to be enabled");
612 case Match_MnemonicFail:
613 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000614 case Match_InvalidOperand: {
615 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000616 if (ErrorInfo != ~0U) {
617 if (ErrorInfo >= Operands.size())
618 return Error(ErrorLoc, "too few operands for instruction");
619
620 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
621 if (ErrorLoc == SMLoc())
622 ErrorLoc = IDLoc;
623 }
624 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000625 }
Alex Bradburya6e62482017-12-07 10:53:48 +0000626 case Match_InvalidUImmLog2XLen:
627 if (isRV64())
628 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
629 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000630 case Match_InvalidUImmLog2XLenNonZero:
631 if (isRV64())
632 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
633 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000634 case Match_InvalidUImm5:
635 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000636 case Match_InvalidSImm6:
637 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
638 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000639 case Match_InvalidSImm6NonZero:
640 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
641 (1 << 5) - 1,
642 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000643 case Match_InvalidCLUIImm:
644 return generateImmOutOfRangeError(
645 Operands, ErrorInfo, 1, (1 << 5) - 1,
646 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000647 case Match_InvalidUImm7Lsb00:
648 return generateImmOutOfRangeError(
649 Operands, ErrorInfo, 0, (1 << 7) - 4,
650 "immediate must be a multiple of 4 bytes in the range");
651 case Match_InvalidUImm8Lsb00:
652 return generateImmOutOfRangeError(
653 Operands, ErrorInfo, 0, (1 << 8) - 4,
654 "immediate must be a multiple of 4 bytes in the range");
655 case Match_InvalidUImm8Lsb000:
656 return generateImmOutOfRangeError(
657 Operands, ErrorInfo, 0, (1 << 8) - 8,
658 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000659 case Match_InvalidSImm9Lsb0:
660 return generateImmOutOfRangeError(
661 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
662 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000663 case Match_InvalidUImm9Lsb000:
664 return generateImmOutOfRangeError(
665 Operands, ErrorInfo, 0, (1 << 9) - 8,
666 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000667 case Match_InvalidUImm10Lsb00NonZero:
668 return generateImmOutOfRangeError(
669 Operands, ErrorInfo, 4, (1 << 10) - 4,
670 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000671 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000672 return generateImmOutOfRangeError(
673 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000674 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000675 case Match_InvalidSImm12:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000676 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 11),
677 (1 << 11) - 1);
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000678 case Match_InvalidSImm12Lsb0:
679 return generateImmOutOfRangeError(
680 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
681 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000682 case Match_InvalidUImm12:
683 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1);
684 case Match_InvalidSImm13Lsb0:
685 return generateImmOutOfRangeError(
686 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
687 "immediate must be a multiple of 2 bytes in the range");
688 case Match_InvalidUImm20:
689 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1);
690 case Match_InvalidSImm21Lsb0:
691 return generateImmOutOfRangeError(
692 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
693 "immediate must be a multiple of 2 bytes in the range");
694 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000695 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000696 return Error(
697 ErrorLoc,
698 "operand must be formed of letters selected in-order from 'iorw'");
699 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000700 case Match_InvalidFRMArg: {
701 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
702 return Error(
703 ErrorLoc,
704 "operand must be a valid floating point rounding mode mnemonic");
705 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000706 }
707
708 llvm_unreachable("Unknown match type detected!");
709}
710
711bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
712 SMLoc &EndLoc) {
713 const AsmToken &Tok = getParser().getTok();
714 StartLoc = Tok.getLoc();
715 EndLoc = Tok.getEndLoc();
716 RegNo = 0;
717 StringRef Name = getLexer().getTok().getIdentifier();
718
719 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
720 getParser().Lex(); // Eat identifier token.
721 return false;
722 }
723
724 return Error(StartLoc, "invalid register name");
725}
726
Alex Bradbury8c345c52017-11-09 15:00:03 +0000727OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
728 bool AllowParens) {
729 SMLoc FirstS = getLoc();
730 bool HadParens = false;
731 AsmToken Buf[2];
732
733 // If this a parenthesised register name is allowed, parse it atomically
734 if (AllowParens && getLexer().is(AsmToken::LParen)) {
735 size_t ReadCount = getLexer().peekTokens(Buf);
736 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
737 HadParens = true;
738 getParser().Lex(); // Eat '('
739 }
740 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000741
742 switch (getLexer().getKind()) {
743 default:
744 return MatchOperand_NoMatch;
745 case AsmToken::Identifier:
746 StringRef Name = getLexer().getTok().getIdentifier();
747 unsigned RegNo = MatchRegisterName(Name);
748 if (RegNo == 0) {
749 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000750 if (RegNo == 0) {
751 if (HadParens)
752 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000753 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000754 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000755 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000756 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000757 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000758 SMLoc S = getLoc();
759 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000760 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000761 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000762 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000763
764 if (HadParens) {
765 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000766 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000767 }
768
Alex Bradbury04f06d92017-08-08 14:43:36 +0000769 return MatchOperand_Success;
770}
771
772OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000773 SMLoc S = getLoc();
774 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
775 const MCExpr *Res;
776
Alex Bradbury04f06d92017-08-08 14:43:36 +0000777 switch (getLexer().getKind()) {
778 default:
779 return MatchOperand_NoMatch;
780 case AsmToken::LParen:
781 case AsmToken::Minus:
782 case AsmToken::Plus:
783 case AsmToken::Integer:
784 case AsmToken::String:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000785 if (getParser().parseExpression(Res))
786 return MatchOperand_ParseFail;
787 break;
788 case AsmToken::Identifier: {
789 StringRef Identifier;
790 if (getParser().parseIdentifier(Identifier))
791 return MatchOperand_ParseFail;
792 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
793 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury04f06d92017-08-08 14:43:36 +0000794 break;
795 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000796 case AsmToken::Percent:
797 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000798 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000799
Alex Bradburya6e62482017-12-07 10:53:48 +0000800 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000801 return MatchOperand_Success;
802}
803
804OperandMatchResultTy
805RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
806 SMLoc S = getLoc();
807 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
808
809 if (getLexer().getKind() != AsmToken::Percent) {
810 Error(getLoc(), "expected '%' for operand modifier");
811 return MatchOperand_ParseFail;
812 }
813
814 getParser().Lex(); // Eat '%'
815
816 if (getLexer().getKind() != AsmToken::Identifier) {
817 Error(getLoc(), "expected valid identifier for operand modifier");
818 return MatchOperand_ParseFail;
819 }
820 StringRef Identifier = getParser().getTok().getIdentifier();
821 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
822 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
823 Error(getLoc(), "unrecognized operand modifier");
824 return MatchOperand_ParseFail;
825 }
826
827 getParser().Lex(); // Eat the identifier
828 if (getLexer().getKind() != AsmToken::LParen) {
829 Error(getLoc(), "expected '('");
830 return MatchOperand_ParseFail;
831 }
832 getParser().Lex(); // Eat '('
833
834 const MCExpr *SubExpr;
835 if (getParser().parseParenExpression(SubExpr, E)) {
836 return MatchOperand_ParseFail;
837 }
838
839 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +0000840 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000841 return MatchOperand_Success;
842}
843
844OperandMatchResultTy
845RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
846 if (getLexer().isNot(AsmToken::LParen)) {
847 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000848 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000849 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000850
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000851 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +0000852 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000853
854 if (parseRegister(Operands) != MatchOperand_Success) {
855 Error(getLoc(), "expected register");
856 return MatchOperand_ParseFail;
857 }
858
859 if (getLexer().isNot(AsmToken::RParen)) {
860 Error(getLoc(), "expected ')'");
861 return MatchOperand_ParseFail;
862 }
863
864 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000865 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000866
Alex Bradbury04f06d92017-08-08 14:43:36 +0000867 return MatchOperand_Success;
868}
869
870/// Looks at a token type and creates the relevant operand
871/// from this information, adding to Operands.
872/// If operand was parsed, returns false, else true.
873bool RISCVAsmParser::parseOperand(OperandVector &Operands) {
874 // Attempt to parse token as register
Alex Bradbury8c345c52017-11-09 15:00:03 +0000875 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +0000876 return false;
877
878 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000879 if (parseImmediate(Operands) == MatchOperand_Success) {
880 // Parse memory base register if present
881 if (getLexer().is(AsmToken::LParen))
882 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000883 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000884 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000885
886 // Finally we have exhausted all options and must declare defeat.
887 Error(getLoc(), "unknown operand");
888 return true;
889}
890
891bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
892 StringRef Name, SMLoc NameLoc,
893 OperandVector &Operands) {
894 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +0000895 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000896
897 // If there are no more operands, then finish
898 if (getLexer().is(AsmToken::EndOfStatement))
899 return false;
900
901 // Parse first operand
902 if (parseOperand(Operands))
903 return true;
904
905 // Parse until end of statement, consuming commas between operands
906 while (getLexer().is(AsmToken::Comma)) {
907 // Consume comma token
908 getLexer().Lex();
909
910 // Parse next operand
911 if (parseOperand(Operands))
912 return true;
913 }
914
915 if (getLexer().isNot(AsmToken::EndOfStatement)) {
916 SMLoc Loc = getLexer().getLoc();
917 getParser().eatToEndOfStatement();
918 return Error(Loc, "unexpected token");
919 }
920
921 getParser().Lex(); // Consume the EndOfStatement.
922 return false;
923}
924
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000925bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
926 RISCVMCExpr::VariantKind &Kind,
927 int64_t &Addend) {
928 Kind = RISCVMCExpr::VK_RISCV_None;
929 Addend = 0;
930
931 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
932 Kind = RE->getKind();
933 Expr = RE->getSubExpr();
934 }
935
936 // It's a simple symbol reference or constant with no addend.
937 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
938 return true;
939
940 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
941 if (!BE)
942 return false;
943
944 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
945 return false;
946
947 if (BE->getOpcode() != MCBinaryExpr::Add &&
948 BE->getOpcode() != MCBinaryExpr::Sub)
949 return false;
950
951 // We are able to support the subtraction of two symbol references
952 if (BE->getOpcode() == MCBinaryExpr::Sub &&
953 isa<MCSymbolRefExpr>(BE->getRHS()))
954 return true;
955
Hiroshi Inoue9ff23802018-04-09 04:37:53 +0000956 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000957 // on here than we can deal with.
958 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
959 if (!AddendExpr)
960 return false;
961
962 Addend = AddendExpr->getValue();
963 if (BE->getOpcode() == MCBinaryExpr::Sub)
964 Addend = -Addend;
965
966 // It's some symbol reference + a constant addend
967 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
968}
969
Alex Bradbury04f06d92017-08-08 14:43:36 +0000970bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) { return true; }
971
972extern "C" void LLVMInitializeRISCVAsmParser() {
973 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
974 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
975}