blob: 64f59c230ec0257303a4d47ab0107ede43d8f1da [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
29namespace {
30struct RISCVOperand;
31
32class RISCVAsmParser : public MCTargetAsmParser {
33 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000034 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000035
Alex Bradbury7bc2a952017-12-07 10:46:23 +000036 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
37 unsigned Kind) override;
38
Alex Bradbury6758ecb2017-09-17 14:27:35 +000039 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
40 int Lower, int Upper, Twine Msg);
41
Alex Bradbury04f06d92017-08-08 14:43:36 +000042 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
43 OperandVector &Operands, MCStreamer &Out,
44 uint64_t &ErrorInfo,
45 bool MatchingInlineAsm) override;
46
47 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
48
49 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
50 SMLoc NameLoc, OperandVector &Operands) override;
51
52 bool ParseDirective(AsmToken DirectiveID) override;
53
54// Auto-generated instruction matching functions
55#define GET_ASSEMBLER_HEADER
56#include "RISCVGenAsmMatcher.inc"
57
58 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +000059 OperandMatchResultTy parseRegister(OperandVector &Operands,
60 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000061 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +000062 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000063
64 bool parseOperand(OperandVector &Operands);
65
66public:
67 enum RISCVMatchResultTy {
68 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
69#define GET_OPERAND_DIAGNOSTIC_TYPES
70#include "RISCVGenAsmMatcher.inc"
71#undef GET_OPERAND_DIAGNOSTIC_TYPES
72 };
73
Alex Bradbury9d3f1252017-09-28 08:26:24 +000074 static bool classifySymbolRef(const MCExpr *Expr,
75 RISCVMCExpr::VariantKind &Kind,
76 int64_t &Addend);
77
Alex Bradbury04f06d92017-08-08 14:43:36 +000078 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
79 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +000080 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradbury04f06d92017-08-08 14:43:36 +000081 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
82 }
83};
84
85/// RISCVOperand - Instances of this class represent a parsed machine
86/// instruction
87struct RISCVOperand : public MCParsedAsmOperand {
88
89 enum KindTy {
90 Token,
91 Register,
92 Immediate,
93 } Kind;
94
Alex Bradburya6e62482017-12-07 10:53:48 +000095 bool IsRV64;
96
Alex Bradbury04f06d92017-08-08 14:43:36 +000097 struct RegOp {
98 unsigned RegNum;
99 };
100
101 struct ImmOp {
102 const MCExpr *Val;
103 };
104
105 SMLoc StartLoc, EndLoc;
106 union {
107 StringRef Tok;
108 RegOp Reg;
109 ImmOp Imm;
110 };
111
112 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
113
114public:
115 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
116 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000117 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000118 StartLoc = o.StartLoc;
119 EndLoc = o.EndLoc;
120 switch (Kind) {
121 case Register:
122 Reg = o.Reg;
123 break;
124 case Immediate:
125 Imm = o.Imm;
126 break;
127 case Token:
128 Tok = o.Tok;
129 break;
130 }
131 }
132
133 bool isToken() const override { return Kind == Token; }
134 bool isReg() const override { return Kind == Register; }
135 bool isImm() const override { return Kind == Immediate; }
136 bool isMem() const override { return false; }
137
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000138 bool evaluateConstantImm(int64_t &Imm, RISCVMCExpr::VariantKind &VK) const {
139 const MCExpr *Val = getImm();
140 bool Ret = false;
141 if (auto *RE = dyn_cast<RISCVMCExpr>(Val)) {
142 Ret = RE->evaluateAsConstant(Imm);
143 VK = RE->getKind();
144 } else if (auto CE = dyn_cast<MCConstantExpr>(Val)) {
145 Ret = true;
146 VK = RISCVMCExpr::VK_RISCV_None;
147 Imm = CE->getValue();
148 }
149 return Ret;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000150 }
151
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000152 // True if operand is a symbol with no modifiers, or a constant with no
153 // modifiers and isShiftedInt<N-1, 1>(Op).
154 template <int N> bool isBareSimmNLsb0() const {
155 int64_t Imm;
156 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000157 if (!isImm())
158 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000159 bool IsConstantImm = evaluateConstantImm(Imm, VK);
160 bool IsValid;
161 if (!IsConstantImm)
162 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
163 else
164 IsValid = isShiftedInt<N - 1, 1>(Imm);
165 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000166 }
167
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000168 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
169
170 /// Return true if the operand is a valid for the fence instruction e.g.
171 /// ('iorw').
172 bool isFenceArg() const {
173 if (!isImm())
174 return false;
175 const MCExpr *Val = getImm();
176 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
177 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
178 return false;
179
180 StringRef Str = SVal->getSymbol().getName();
181 // Letters must be unique, taken from 'iorw', and in ascending order. This
182 // holds as long as each individual character is one of 'iorw' and is
183 // greater than the previous character.
184 char Prev = '\0';
185 for (char c : Str) {
186 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
187 return false;
188 if (c <= Prev)
189 return false;
190 Prev = c;
191 }
192 return true;
193 }
194
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000195 /// Return true if the operand is a valid floating point rounding mode.
196 bool isFRMArg() const {
197 if (!isImm())
198 return false;
199 const MCExpr *Val = getImm();
200 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
201 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
202 return false;
203
204 StringRef Str = SVal->getSymbol().getName();
205
206 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
207 }
208
Alex Bradburya6e62482017-12-07 10:53:48 +0000209 bool isUImmLog2XLen() const {
210 int64_t Imm;
211 RISCVMCExpr::VariantKind VK;
212 if (!isImm())
213 return false;
214 if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
215 return false;
216 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
217 }
218
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000219 bool isUImmLog2XLenNonZero() const {
220 int64_t Imm;
221 RISCVMCExpr::VariantKind VK;
222 if (!isImm())
223 return false;
224 if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
225 return false;
226 if (Imm == 0)
227 return false;
228 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
229 }
230
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000231 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000232 int64_t Imm;
233 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000234 if (!isImm())
235 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000236 bool IsConstantImm = evaluateConstantImm(Imm, VK);
237 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000238 }
239
Alex Bradbury60714f92017-12-13 09:32:55 +0000240 bool isUImm5NonZero() const {
241 int64_t Imm;
242 RISCVMCExpr::VariantKind VK;
243 if (!isImm())
244 return false;
245 bool IsConstantImm = evaluateConstantImm(Imm, VK);
246 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
247 VK == RISCVMCExpr::VK_RISCV_None;
248 }
249
Alex Bradbury581d6b02017-12-13 09:41:21 +0000250 bool isSImm6() const {
251 RISCVMCExpr::VariantKind VK;
252 int64_t Imm;
253 bool IsValid;
254 bool IsConstantImm = evaluateConstantImm(Imm, VK);
255 if (!IsConstantImm)
256 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
257 else
258 IsValid = isInt<6>(Imm);
259 return IsValid &&
260 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
261 }
262
Shiva Chenb22c1d22018-02-02 02:43:23 +0000263 bool isSImm6NonZero() const {
264 RISCVMCExpr::VariantKind VK;
265 int64_t Imm;
266 bool IsValid;
267 bool IsConstantImm = evaluateConstantImm(Imm, VK);
268 if (!IsConstantImm)
269 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
270 else
271 IsValid = ((Imm != 0) && isInt<6>(Imm));
272 return IsValid &&
273 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
274 }
275
Alex Bradbury60714f92017-12-13 09:32:55 +0000276 bool isUImm6NonZero() const {
277 int64_t Imm;
278 RISCVMCExpr::VariantKind VK;
279 bool IsConstantImm = evaluateConstantImm(Imm, VK);
280 return IsConstantImm && isUInt<6>(Imm) && (Imm != 0) &&
281 VK == RISCVMCExpr::VK_RISCV_None;
282 }
283
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000284 bool isUImm7Lsb00() const {
285 int64_t Imm;
286 RISCVMCExpr::VariantKind VK;
287 bool IsConstantImm = evaluateConstantImm(Imm, VK);
288 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
289 VK == RISCVMCExpr::VK_RISCV_None;
290 }
291
292 bool isUImm8Lsb00() const {
293 int64_t Imm;
294 RISCVMCExpr::VariantKind VK;
295 bool IsConstantImm = evaluateConstantImm(Imm, VK);
296 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
297 VK == RISCVMCExpr::VK_RISCV_None;
298 }
299
300 bool isUImm8Lsb000() const {
301 int64_t Imm;
302 RISCVMCExpr::VariantKind VK;
303 bool IsConstantImm = evaluateConstantImm(Imm, VK);
304 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
305 VK == RISCVMCExpr::VK_RISCV_None;
306 }
307
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000308 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
309
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000310 bool isUImm9Lsb000() const {
311 int64_t Imm;
312 RISCVMCExpr::VariantKind VK;
313 bool IsConstantImm = evaluateConstantImm(Imm, VK);
314 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
315 VK == RISCVMCExpr::VK_RISCV_None;
316 }
317
Alex Bradbury60714f92017-12-13 09:32:55 +0000318 bool isUImm10Lsb00NonZero() const {
319 int64_t Imm;
320 RISCVMCExpr::VariantKind VK;
321 bool IsConstantImm = evaluateConstantImm(Imm, VK);
322 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
323 VK == RISCVMCExpr::VK_RISCV_None;
324 }
325
Alex Bradbury04f06d92017-08-08 14:43:36 +0000326 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000327 RISCVMCExpr::VariantKind VK;
328 int64_t Imm;
329 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000330 if (!isImm())
331 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000332 bool IsConstantImm = evaluateConstantImm(Imm, VK);
333 if (!IsConstantImm)
334 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
335 else
336 IsValid = isInt<12>(Imm);
337 return IsValid &&
338 (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000339 }
340
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000341 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
342
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000343 bool isUImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000344 int64_t Imm;
345 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000346 if (!isImm())
347 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000348 bool IsConstantImm = evaluateConstantImm(Imm, VK);
349 return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000350 }
351
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000352 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000353
Shiva Chenb22c1d22018-02-02 02:43:23 +0000354 bool isSImm10Lsb0000NonZero() const {
Alex Bradbury60714f92017-12-13 09:32:55 +0000355 int64_t Imm;
356 RISCVMCExpr::VariantKind VK;
357 bool IsConstantImm = evaluateConstantImm(Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000358 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000359 VK == RISCVMCExpr::VK_RISCV_None;
360 }
361
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000362 bool isUImm20() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000363 RISCVMCExpr::VariantKind VK;
364 int64_t Imm;
365 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000366 if (!isImm())
367 return false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000368 bool IsConstantImm = evaluateConstantImm(Imm, VK);
369 if (!IsConstantImm)
370 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
371 else
372 IsValid = isUInt<20>(Imm);
373 return IsValid && (VK == RISCVMCExpr::VK_RISCV_None ||
374 VK == RISCVMCExpr::VK_RISCV_HI ||
375 VK == RISCVMCExpr::VK_RISCV_PCREL_HI);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000376 }
377
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000378 bool isSImm21Lsb0() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000379
Alex Bradbury04f06d92017-08-08 14:43:36 +0000380 /// getStartLoc - Gets location of the first token of this operand
381 SMLoc getStartLoc() const override { return StartLoc; }
382 /// getEndLoc - Gets location of the last token of this operand
383 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000384 /// True if this operand is for an RV64 instruction
385 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000386
387 unsigned getReg() const override {
388 assert(Kind == Register && "Invalid type access!");
389 return Reg.RegNum;
390 }
391
392 const MCExpr *getImm() const {
393 assert(Kind == Immediate && "Invalid type access!");
394 return Imm.Val;
395 }
396
397 StringRef getToken() const {
398 assert(Kind == Token && "Invalid type access!");
399 return Tok;
400 }
401
402 void print(raw_ostream &OS) const override {
403 switch (Kind) {
404 case Immediate:
405 OS << *getImm();
406 break;
407 case Register:
408 OS << "<register x";
409 OS << getReg() << ">";
410 break;
411 case Token:
412 OS << "'" << getToken() << "'";
413 break;
414 }
415 }
416
Alex Bradburya6e62482017-12-07 10:53:48 +0000417 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
418 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000419 auto Op = make_unique<RISCVOperand>(Token);
420 Op->Tok = Str;
421 Op->StartLoc = S;
422 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000423 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000424 return Op;
425 }
426
427 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000428 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000429 auto Op = make_unique<RISCVOperand>(Register);
430 Op->Reg.RegNum = RegNo;
431 Op->StartLoc = S;
432 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000433 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000434 return Op;
435 }
436
437 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000438 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000439 auto Op = make_unique<RISCVOperand>(Immediate);
440 Op->Imm.Val = Val;
441 Op->StartLoc = S;
442 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000443 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000444 return Op;
445 }
446
447 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
448 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000449 int64_t Imm = 0;
450 bool IsConstant = false;
451 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
452 IsConstant = RE->evaluateAsConstant(Imm);
453 } else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
454 IsConstant = true;
455 Imm = CE->getValue();
456 }
457
458 if (IsConstant)
459 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000460 else
461 Inst.addOperand(MCOperand::createExpr(Expr));
462 }
463
464 // Used by the TableGen Code
465 void addRegOperands(MCInst &Inst, unsigned N) const {
466 assert(N == 1 && "Invalid number of operands!");
467 Inst.addOperand(MCOperand::createReg(getReg()));
468 }
469
470 void addImmOperands(MCInst &Inst, unsigned N) const {
471 assert(N == 1 && "Invalid number of operands!");
472 addExpr(Inst, getImm());
473 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000474
475 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
476 assert(N == 1 && "Invalid number of operands!");
477 // isFenceArg has validated the operand, meaning this cast is safe
478 auto SE = cast<MCSymbolRefExpr>(getImm());
479
480 unsigned Imm = 0;
481 for (char c : SE->getSymbol().getName()) {
482 switch (c) {
483 default: llvm_unreachable("FenceArg must contain only [iorw]");
484 case 'i': Imm |= RISCVFenceField::I; break;
485 case 'o': Imm |= RISCVFenceField::O; break;
486 case 'r': Imm |= RISCVFenceField::R; break;
487 case 'w': Imm |= RISCVFenceField::W; break;
488 }
489 }
490 Inst.addOperand(MCOperand::createImm(Imm));
491 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000492
493 // Returns the rounding mode represented by this RISCVOperand. Should only
494 // be called after checking isFRMArg.
495 RISCVFPRndMode::RoundingMode getRoundingMode() const {
496 // isFRMArg has validated the operand, meaning this cast is safe.
497 auto SE = cast<MCSymbolRefExpr>(getImm());
498 RISCVFPRndMode::RoundingMode FRM =
499 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
500 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
501 return FRM;
502 }
503
504 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
505 assert(N == 1 && "Invalid number of operands!");
506 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
507 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000508};
509} // end anonymous namespace.
510
511#define GET_REGISTER_MATCHER
512#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000513#include "RISCVGenAsmMatcher.inc"
514
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000515// Return the matching FPR64 register for the given FPR32.
516// FIXME: Ideally this function could be removed in favour of using
517// information from TableGen.
518unsigned convertFPR32ToFPR64(unsigned Reg) {
519 switch (Reg) {
520 default:
521 llvm_unreachable("Not a recognised FPR32 register");
522 case RISCV::F0_32: return RISCV::F0_64;
523 case RISCV::F1_32: return RISCV::F1_64;
524 case RISCV::F2_32: return RISCV::F2_64;
525 case RISCV::F3_32: return RISCV::F3_64;
526 case RISCV::F4_32: return RISCV::F4_64;
527 case RISCV::F5_32: return RISCV::F5_64;
528 case RISCV::F6_32: return RISCV::F6_64;
529 case RISCV::F7_32: return RISCV::F7_64;
530 case RISCV::F8_32: return RISCV::F8_64;
531 case RISCV::F9_32: return RISCV::F9_64;
532 case RISCV::F10_32: return RISCV::F10_64;
533 case RISCV::F11_32: return RISCV::F11_64;
534 case RISCV::F12_32: return RISCV::F12_64;
535 case RISCV::F13_32: return RISCV::F13_64;
536 case RISCV::F14_32: return RISCV::F14_64;
537 case RISCV::F15_32: return RISCV::F15_64;
538 case RISCV::F16_32: return RISCV::F16_64;
539 case RISCV::F17_32: return RISCV::F17_64;
540 case RISCV::F18_32: return RISCV::F18_64;
541 case RISCV::F19_32: return RISCV::F19_64;
542 case RISCV::F20_32: return RISCV::F20_64;
543 case RISCV::F21_32: return RISCV::F21_64;
544 case RISCV::F22_32: return RISCV::F22_64;
545 case RISCV::F23_32: return RISCV::F23_64;
546 case RISCV::F24_32: return RISCV::F24_64;
547 case RISCV::F25_32: return RISCV::F25_64;
548 case RISCV::F26_32: return RISCV::F26_64;
549 case RISCV::F27_32: return RISCV::F27_64;
550 case RISCV::F28_32: return RISCV::F28_64;
551 case RISCV::F29_32: return RISCV::F29_64;
552 case RISCV::F30_32: return RISCV::F30_64;
553 case RISCV::F31_32: return RISCV::F31_64;
554 }
555}
556
557unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
558 unsigned Kind) {
559 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
560 if (!Op.isReg())
561 return Match_InvalidOperand;
562
563 unsigned Reg = Op.getReg();
564 bool IsRegFPR32 =
565 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000566 bool IsRegFPR32C =
567 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000568
569 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000570 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
571 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
572 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000573 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
574 return Match_Success;
575 }
576 return Match_InvalidOperand;
577}
578
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000579bool RISCVAsmParser::generateImmOutOfRangeError(
580 OperandVector &Operands, uint64_t ErrorInfo, int Lower, int Upper,
581 Twine Msg = "immediate must be an integer in the range") {
582 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
583 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
584}
585
Alex Bradbury04f06d92017-08-08 14:43:36 +0000586bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
587 OperandVector &Operands,
588 MCStreamer &Out,
589 uint64_t &ErrorInfo,
590 bool MatchingInlineAsm) {
591 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000592
593 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
594 default:
595 break;
596 case Match_Success:
597 Inst.setLoc(IDLoc);
598 Out.EmitInstruction(Inst, getSTI());
599 return false;
600 case Match_MissingFeature:
601 return Error(IDLoc, "instruction use requires an option to be enabled");
602 case Match_MnemonicFail:
603 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000604 case Match_InvalidOperand: {
605 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000606 if (ErrorInfo != ~0U) {
607 if (ErrorInfo >= Operands.size())
608 return Error(ErrorLoc, "too few operands for instruction");
609
610 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
611 if (ErrorLoc == SMLoc())
612 ErrorLoc = IDLoc;
613 }
614 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000615 }
Alex Bradburya6e62482017-12-07 10:53:48 +0000616 case Match_InvalidUImmLog2XLen:
617 if (isRV64())
618 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
619 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000620 case Match_InvalidUImmLog2XLenNonZero:
621 if (isRV64())
622 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
623 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000624 case Match_InvalidUImm5:
625 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000626 case Match_InvalidSImm6:
627 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
628 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000629 case Match_InvalidSImm6NonZero:
630 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
631 (1 << 5) - 1,
632 "immediate must be non-zero in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000633 case Match_InvalidUImm6NonZero:
634 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000635 case Match_InvalidUImm7Lsb00:
636 return generateImmOutOfRangeError(
637 Operands, ErrorInfo, 0, (1 << 7) - 4,
638 "immediate must be a multiple of 4 bytes in the range");
639 case Match_InvalidUImm8Lsb00:
640 return generateImmOutOfRangeError(
641 Operands, ErrorInfo, 0, (1 << 8) - 4,
642 "immediate must be a multiple of 4 bytes in the range");
643 case Match_InvalidUImm8Lsb000:
644 return generateImmOutOfRangeError(
645 Operands, ErrorInfo, 0, (1 << 8) - 8,
646 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000647 case Match_InvalidSImm9Lsb0:
648 return generateImmOutOfRangeError(
649 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
650 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000651 case Match_InvalidUImm9Lsb000:
652 return generateImmOutOfRangeError(
653 Operands, ErrorInfo, 0, (1 << 9) - 8,
654 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000655 case Match_InvalidUImm10Lsb00NonZero:
656 return generateImmOutOfRangeError(
657 Operands, ErrorInfo, 4, (1 << 10) - 4,
658 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000659 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000660 return generateImmOutOfRangeError(
661 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000662 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000663 case Match_InvalidSImm12:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000664 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 11),
665 (1 << 11) - 1);
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000666 case Match_InvalidSImm12Lsb0:
667 return generateImmOutOfRangeError(
668 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
669 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000670 case Match_InvalidUImm12:
671 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1);
672 case Match_InvalidSImm13Lsb0:
673 return generateImmOutOfRangeError(
674 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
675 "immediate must be a multiple of 2 bytes in the range");
676 case Match_InvalidUImm20:
677 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1);
678 case Match_InvalidSImm21Lsb0:
679 return generateImmOutOfRangeError(
680 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
681 "immediate must be a multiple of 2 bytes in the range");
682 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000683 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000684 return Error(
685 ErrorLoc,
686 "operand must be formed of letters selected in-order from 'iorw'");
687 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000688 case Match_InvalidFRMArg: {
689 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
690 return Error(
691 ErrorLoc,
692 "operand must be a valid floating point rounding mode mnemonic");
693 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000694 }
695
696 llvm_unreachable("Unknown match type detected!");
697}
698
699bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
700 SMLoc &EndLoc) {
701 const AsmToken &Tok = getParser().getTok();
702 StartLoc = Tok.getLoc();
703 EndLoc = Tok.getEndLoc();
704 RegNo = 0;
705 StringRef Name = getLexer().getTok().getIdentifier();
706
707 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
708 getParser().Lex(); // Eat identifier token.
709 return false;
710 }
711
712 return Error(StartLoc, "invalid register name");
713}
714
Alex Bradbury8c345c52017-11-09 15:00:03 +0000715OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
716 bool AllowParens) {
717 SMLoc FirstS = getLoc();
718 bool HadParens = false;
719 AsmToken Buf[2];
720
721 // If this a parenthesised register name is allowed, parse it atomically
722 if (AllowParens && getLexer().is(AsmToken::LParen)) {
723 size_t ReadCount = getLexer().peekTokens(Buf);
724 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
725 HadParens = true;
726 getParser().Lex(); // Eat '('
727 }
728 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000729
730 switch (getLexer().getKind()) {
731 default:
732 return MatchOperand_NoMatch;
733 case AsmToken::Identifier:
734 StringRef Name = getLexer().getTok().getIdentifier();
735 unsigned RegNo = MatchRegisterName(Name);
736 if (RegNo == 0) {
737 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000738 if (RegNo == 0) {
739 if (HadParens)
740 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000741 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000742 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000743 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000744 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000745 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000746 SMLoc S = getLoc();
747 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000748 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000749 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000750 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000751
752 if (HadParens) {
753 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000754 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000755 }
756
Alex Bradbury04f06d92017-08-08 14:43:36 +0000757 return MatchOperand_Success;
758}
759
760OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000761 SMLoc S = getLoc();
762 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
763 const MCExpr *Res;
764
Alex Bradbury04f06d92017-08-08 14:43:36 +0000765 switch (getLexer().getKind()) {
766 default:
767 return MatchOperand_NoMatch;
768 case AsmToken::LParen:
769 case AsmToken::Minus:
770 case AsmToken::Plus:
771 case AsmToken::Integer:
772 case AsmToken::String:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000773 if (getParser().parseExpression(Res))
774 return MatchOperand_ParseFail;
775 break;
776 case AsmToken::Identifier: {
777 StringRef Identifier;
778 if (getParser().parseIdentifier(Identifier))
779 return MatchOperand_ParseFail;
780 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
781 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury04f06d92017-08-08 14:43:36 +0000782 break;
783 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000784 case AsmToken::Percent:
785 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000786 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000787
Alex Bradburya6e62482017-12-07 10:53:48 +0000788 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000789 return MatchOperand_Success;
790}
791
792OperandMatchResultTy
793RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
794 SMLoc S = getLoc();
795 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
796
797 if (getLexer().getKind() != AsmToken::Percent) {
798 Error(getLoc(), "expected '%' for operand modifier");
799 return MatchOperand_ParseFail;
800 }
801
802 getParser().Lex(); // Eat '%'
803
804 if (getLexer().getKind() != AsmToken::Identifier) {
805 Error(getLoc(), "expected valid identifier for operand modifier");
806 return MatchOperand_ParseFail;
807 }
808 StringRef Identifier = getParser().getTok().getIdentifier();
809 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
810 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
811 Error(getLoc(), "unrecognized operand modifier");
812 return MatchOperand_ParseFail;
813 }
814
815 getParser().Lex(); // Eat the identifier
816 if (getLexer().getKind() != AsmToken::LParen) {
817 Error(getLoc(), "expected '('");
818 return MatchOperand_ParseFail;
819 }
820 getParser().Lex(); // Eat '('
821
822 const MCExpr *SubExpr;
823 if (getParser().parseParenExpression(SubExpr, E)) {
824 return MatchOperand_ParseFail;
825 }
826
827 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +0000828 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000829 return MatchOperand_Success;
830}
831
832OperandMatchResultTy
833RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
834 if (getLexer().isNot(AsmToken::LParen)) {
835 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000836 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000837 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000838
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000839 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +0000840 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000841
842 if (parseRegister(Operands) != MatchOperand_Success) {
843 Error(getLoc(), "expected register");
844 return MatchOperand_ParseFail;
845 }
846
847 if (getLexer().isNot(AsmToken::RParen)) {
848 Error(getLoc(), "expected ')'");
849 return MatchOperand_ParseFail;
850 }
851
852 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000853 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000854
Alex Bradbury04f06d92017-08-08 14:43:36 +0000855 return MatchOperand_Success;
856}
857
858/// Looks at a token type and creates the relevant operand
859/// from this information, adding to Operands.
860/// If operand was parsed, returns false, else true.
861bool RISCVAsmParser::parseOperand(OperandVector &Operands) {
862 // Attempt to parse token as register
Alex Bradbury8c345c52017-11-09 15:00:03 +0000863 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +0000864 return false;
865
866 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000867 if (parseImmediate(Operands) == MatchOperand_Success) {
868 // Parse memory base register if present
869 if (getLexer().is(AsmToken::LParen))
870 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000871 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000872 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000873
874 // Finally we have exhausted all options and must declare defeat.
875 Error(getLoc(), "unknown operand");
876 return true;
877}
878
879bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
880 StringRef Name, SMLoc NameLoc,
881 OperandVector &Operands) {
882 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +0000883 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000884
885 // If there are no more operands, then finish
886 if (getLexer().is(AsmToken::EndOfStatement))
887 return false;
888
889 // Parse first operand
890 if (parseOperand(Operands))
891 return true;
892
893 // Parse until end of statement, consuming commas between operands
894 while (getLexer().is(AsmToken::Comma)) {
895 // Consume comma token
896 getLexer().Lex();
897
898 // Parse next operand
899 if (parseOperand(Operands))
900 return true;
901 }
902
903 if (getLexer().isNot(AsmToken::EndOfStatement)) {
904 SMLoc Loc = getLexer().getLoc();
905 getParser().eatToEndOfStatement();
906 return Error(Loc, "unexpected token");
907 }
908
909 getParser().Lex(); // Consume the EndOfStatement.
910 return false;
911}
912
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000913bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
914 RISCVMCExpr::VariantKind &Kind,
915 int64_t &Addend) {
916 Kind = RISCVMCExpr::VK_RISCV_None;
917 Addend = 0;
918
919 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
920 Kind = RE->getKind();
921 Expr = RE->getSubExpr();
922 }
923
924 // It's a simple symbol reference or constant with no addend.
925 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
926 return true;
927
928 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
929 if (!BE)
930 return false;
931
932 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
933 return false;
934
935 if (BE->getOpcode() != MCBinaryExpr::Add &&
936 BE->getOpcode() != MCBinaryExpr::Sub)
937 return false;
938
939 // We are able to support the subtraction of two symbol references
940 if (BE->getOpcode() == MCBinaryExpr::Sub &&
941 isa<MCSymbolRefExpr>(BE->getRHS()))
942 return true;
943
944 // See if the addend is is a constant, otherwise there's more going
945 // on here than we can deal with.
946 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
947 if (!AddendExpr)
948 return false;
949
950 Addend = AddendExpr->getValue();
951 if (BE->getOpcode() == MCBinaryExpr::Sub)
952 Addend = -Addend;
953
954 // It's some symbol reference + a constant addend
955 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
956}
957
Alex Bradbury04f06d92017-08-08 14:43:36 +0000958bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) { return true; }
959
960extern "C" void LLVMInitializeRISCVAsmParser() {
961 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
962 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
963}