blob: b1f1eb404010ed713fb87853c0ef70eae4b610f4 [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 Bradbury9d3f1252017-09-28 08:26:24 +000010#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000011#include "MCTargetDesc/RISCVMCTargetDesc.h"
Alex Bradburybca0c3c2018-05-11 17:30:28 +000012#include "MCTargetDesc/RISCVTargetStreamer.h"
Ana Pazos9d6c5532018-10-04 21:50:54 +000013#include "Utils/RISCVBaseInfo.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000014#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/StringSwitch.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000019#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000020#include "llvm/MC/MCParser/MCAsmLexer.h"
21#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000023#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000026#include "llvm/Support/Casting.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000027#include "llvm/Support/MathExtras.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000028#include "llvm/Support/TargetRegistry.h"
29
Alex Bradbury6a4b5442018-06-07 15:35:47 +000030#include <limits>
31
Alex Bradbury04f06d92017-08-08 14:43:36 +000032using namespace llvm;
33
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000034// Include the auto-generated portion of the compress emitter.
35#define GEN_COMPRESS_INSTR
36#include "RISCVGenCompressInstEmitter.inc"
37
Alex Bradbury04f06d92017-08-08 14:43:36 +000038namespace {
39struct RISCVOperand;
40
41class RISCVAsmParser : public MCTargetAsmParser {
42 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000043 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000044
Alex Bradburybca0c3c2018-05-11 17:30:28 +000045 RISCVTargetStreamer &getTargetStreamer() {
46 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
47 return static_cast<RISCVTargetStreamer &>(TS);
48 }
49
Alex Bradbury7bc2a952017-12-07 10:46:23 +000050 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
51 unsigned Kind) override;
52
Alex Bradbury6758ecb2017-09-17 14:27:35 +000053 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury6a4b5442018-06-07 15:35:47 +000054 int64_t Lower, int64_t Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000055
Alex Bradbury04f06d92017-08-08 14:43:36 +000056 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
57 OperandVector &Operands, MCStreamer &Out,
58 uint64_t &ErrorInfo,
59 bool MatchingInlineAsm) override;
60
61 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
62
63 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
64 SMLoc NameLoc, OperandVector &Operands) override;
65
66 bool ParseDirective(AsmToken DirectiveID) override;
67
Alex Bradbury6a4b5442018-06-07 15:35:47 +000068 // Helper to actually emit an instruction to the MCStreamer. Also, when
69 // possible, compression of the instruction is performed.
70 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
71
72 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
73 // synthesize the desired immedate value into the destination register.
74 void emitLoadImm(unsigned DestReg, int64_t Value, MCStreamer &Out);
75
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +000076 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
77 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
78
Alex Bradbury6a4b5442018-06-07 15:35:47 +000079 /// Helper for processing MC instructions that have been successfully matched
80 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
81 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
82 /// in this method.
83 bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
84
Alex Bradbury04f06d92017-08-08 14:43:36 +000085// Auto-generated instruction matching functions
86#define GET_ASSEMBLER_HEADER
87#include "RISCVGenAsmMatcher.inc"
88
Ana Pazos9d6c5532018-10-04 21:50:54 +000089 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000090 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +000091 OperandMatchResultTy parseRegister(OperandVector &Operands,
92 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000093 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +000094 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +000095 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +000096 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000097
Alex Bradbury68f73c12018-09-18 15:18:16 +000098 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +000099
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000100 bool parseDirectiveOption();
101
102 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
103 if (!(getSTI().getFeatureBits()[Feature])) {
104 MCSubtargetInfo &STI = copySTI();
105 setAvailableFeatures(
106 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
107 }
108 }
109
110 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
111 if (getSTI().getFeatureBits()[Feature]) {
112 MCSubtargetInfo &STI = copySTI();
113 setAvailableFeatures(
114 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
115 }
116 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000117
Alex Bradbury04f06d92017-08-08 14:43:36 +0000118public:
119 enum RISCVMatchResultTy {
120 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
121#define GET_OPERAND_DIAGNOSTIC_TYPES
122#include "RISCVGenAsmMatcher.inc"
123#undef GET_OPERAND_DIAGNOSTIC_TYPES
124 };
125
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000126 static bool classifySymbolRef(const MCExpr *Expr,
127 RISCVMCExpr::VariantKind &Kind,
128 int64_t &Addend);
129
Alex Bradbury04f06d92017-08-08 14:43:36 +0000130 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
131 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000132 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000133 Parser.addAliasForDirective(".half", ".2byte");
134 Parser.addAliasForDirective(".hword", ".2byte");
135 Parser.addAliasForDirective(".word", ".4byte");
136 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000137 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
138 }
139};
140
141/// RISCVOperand - Instances of this class represent a parsed machine
142/// instruction
143struct RISCVOperand : public MCParsedAsmOperand {
144
145 enum KindTy {
146 Token,
147 Register,
148 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000149 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000150 } Kind;
151
Alex Bradburya6e62482017-12-07 10:53:48 +0000152 bool IsRV64;
153
Alex Bradbury04f06d92017-08-08 14:43:36 +0000154 struct RegOp {
155 unsigned RegNum;
156 };
157
158 struct ImmOp {
159 const MCExpr *Val;
160 };
161
Ana Pazos9d6c5532018-10-04 21:50:54 +0000162 struct SysRegOp {
163 const char *Data;
164 unsigned Length;
165 unsigned Encoding;
166 // FIXME: Add the Encoding parsed fields as needed for checks,
167 // e.g.: read/write or user/supervisor/machine privileges.
168 };
169
Alex Bradbury04f06d92017-08-08 14:43:36 +0000170 SMLoc StartLoc, EndLoc;
171 union {
172 StringRef Tok;
173 RegOp Reg;
174 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000175 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000176 };
177
178 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
179
180public:
181 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
182 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000183 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000184 StartLoc = o.StartLoc;
185 EndLoc = o.EndLoc;
186 switch (Kind) {
187 case Register:
188 Reg = o.Reg;
189 break;
190 case Immediate:
191 Imm = o.Imm;
192 break;
193 case Token:
194 Tok = o.Tok;
195 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000196 case SystemRegister:
197 SysReg = o.SysReg;
198 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000199 }
200 }
201
202 bool isToken() const override { return Kind == Token; }
203 bool isReg() const override { return Kind == Register; }
204 bool isImm() const override { return Kind == Immediate; }
205 bool isMem() const override { return false; }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000206 bool isSystemRegister() const { return Kind == SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000207
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000208 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
209 RISCVMCExpr::VariantKind &VK) {
210 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000211 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000212 return RE->evaluateAsConstant(Imm);
213 }
214
215 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000216 VK = RISCVMCExpr::VK_RISCV_None;
217 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000218 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000219 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000220
221 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000222 }
223
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000224 // True if operand is a symbol with no modifiers, or a constant with no
225 // modifiers and isShiftedInt<N-1, 1>(Op).
226 template <int N> bool isBareSimmNLsb0() const {
227 int64_t Imm;
228 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000229 if (!isImm())
230 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000231 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000232 bool IsValid;
233 if (!IsConstantImm)
234 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
235 else
236 IsValid = isShiftedInt<N - 1, 1>(Imm);
237 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000238 }
239
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000240 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
241
Shiva Chen98f93892018-04-25 14:18:55 +0000242 bool isBareSymbol() const {
243 int64_t Imm;
244 RISCVMCExpr::VariantKind VK;
245 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000246 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000247 return false;
248 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
249 VK == RISCVMCExpr::VK_RISCV_None;
250 }
251
Ana Pazos9d6c5532018-10-04 21:50:54 +0000252 bool isCSRSystemRegister() const { return isSystemRegister(); }
253
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000254 /// Return true if the operand is a valid for the fence instruction e.g.
255 /// ('iorw').
256 bool isFenceArg() const {
257 if (!isImm())
258 return false;
259 const MCExpr *Val = getImm();
260 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
261 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
262 return false;
263
264 StringRef Str = SVal->getSymbol().getName();
265 // Letters must be unique, taken from 'iorw', and in ascending order. This
266 // holds as long as each individual character is one of 'iorw' and is
267 // greater than the previous character.
268 char Prev = '\0';
269 for (char c : Str) {
270 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
271 return false;
272 if (c <= Prev)
273 return false;
274 Prev = c;
275 }
276 return true;
277 }
278
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000279 /// Return true if the operand is a valid floating point rounding mode.
280 bool isFRMArg() const {
281 if (!isImm())
282 return false;
283 const MCExpr *Val = getImm();
284 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
285 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
286 return false;
287
288 StringRef Str = SVal->getSymbol().getName();
289
290 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
291 }
292
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000293 bool isImmXLen() const {
294 int64_t Imm;
295 RISCVMCExpr::VariantKind VK;
296 if (!isImm())
297 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000298 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000299 // Given only Imm, ensuring that the actually specified constant is either
300 // a signed or unsigned 64-bit number is unfortunately impossible.
301 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
302 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
303 }
304
Alex Bradburya6e62482017-12-07 10:53:48 +0000305 bool isUImmLog2XLen() const {
306 int64_t Imm;
307 RISCVMCExpr::VariantKind VK;
308 if (!isImm())
309 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000310 if (!evaluateConstantImm(getImm(), Imm, VK) ||
311 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000312 return false;
313 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
314 }
315
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000316 bool isUImmLog2XLenNonZero() const {
317 int64_t Imm;
318 RISCVMCExpr::VariantKind VK;
319 if (!isImm())
320 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000321 if (!evaluateConstantImm(getImm(), Imm, VK) ||
322 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000323 return false;
324 if (Imm == 0)
325 return false;
326 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
327 }
328
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000329 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000330 int64_t Imm;
331 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000332 if (!isImm())
333 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000334 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000335 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000336 }
337
Alex Bradbury60714f92017-12-13 09:32:55 +0000338 bool isUImm5NonZero() const {
339 int64_t Imm;
340 RISCVMCExpr::VariantKind VK;
341 if (!isImm())
342 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000343 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000344 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
345 VK == RISCVMCExpr::VK_RISCV_None;
346 }
347
Alex Bradbury581d6b02017-12-13 09:41:21 +0000348 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000349 if (!isImm())
350 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000351 RISCVMCExpr::VariantKind VK;
352 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000353 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000354 return IsConstantImm && isInt<6>(Imm) &&
355 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000356 }
357
Shiva Chenb22c1d22018-02-02 02:43:23 +0000358 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000359 if (!isImm())
360 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000361 RISCVMCExpr::VariantKind VK;
362 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000363 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000364 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
365 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000366 }
367
Shiva Chen7c172422018-02-22 15:02:28 +0000368 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000369 if (!isImm())
370 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000371 int64_t Imm;
372 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000373 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000374 return IsConstantImm && (Imm != 0) &&
375 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000376 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000377 }
378
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000379 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000380 if (!isImm())
381 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000382 int64_t Imm;
383 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000384 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000385 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
386 VK == RISCVMCExpr::VK_RISCV_None;
387 }
388
389 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000390 if (!isImm())
391 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000392 int64_t Imm;
393 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000394 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000395 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
396 VK == RISCVMCExpr::VK_RISCV_None;
397 }
398
399 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000400 if (!isImm())
401 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000402 int64_t Imm;
403 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000404 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000405 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
406 VK == RISCVMCExpr::VK_RISCV_None;
407 }
408
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000409 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
410
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000411 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000412 if (!isImm())
413 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000414 int64_t Imm;
415 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000416 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000417 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
418 VK == RISCVMCExpr::VK_RISCV_None;
419 }
420
Alex Bradbury60714f92017-12-13 09:32:55 +0000421 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000422 if (!isImm())
423 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000424 int64_t Imm;
425 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000426 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000427 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
428 VK == RISCVMCExpr::VK_RISCV_None;
429 }
430
Alex Bradbury04f06d92017-08-08 14:43:36 +0000431 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000432 RISCVMCExpr::VariantKind VK;
433 int64_t Imm;
434 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000435 if (!isImm())
436 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000437 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000438 if (!IsConstantImm)
439 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
440 else
441 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000442 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000443 VK == RISCVMCExpr::VK_RISCV_LO ||
444 VK == RISCVMCExpr::VK_RISCV_PCREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000445 }
446
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000447 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
448
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000449 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000450
Shiva Chenb22c1d22018-02-02 02:43:23 +0000451 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000452 if (!isImm())
453 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000454 int64_t Imm;
455 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000456 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000457 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000458 VK == RISCVMCExpr::VK_RISCV_None;
459 }
460
Alex Bradbury74340f12018-09-18 15:08:35 +0000461 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000462 RISCVMCExpr::VariantKind VK;
463 int64_t Imm;
464 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000465 if (!isImm())
466 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000467 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000468 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000469 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury74340f12018-09-18 15:08:35 +0000470 return IsValid && VK == RISCVMCExpr::VK_RISCV_HI;
471 } else {
472 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
473 VK == RISCVMCExpr::VK_RISCV_HI);
474 }
475 }
476
477 bool isUImm20AUIPC() const {
478 RISCVMCExpr::VariantKind VK;
479 int64_t Imm;
480 bool IsValid;
481 if (!isImm())
482 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000483 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000484 if (!IsConstantImm) {
485 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
486 return IsValid && VK == RISCVMCExpr::VK_RISCV_PCREL_HI;
487 } else {
488 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
489 VK == RISCVMCExpr::VK_RISCV_PCREL_HI);
490 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000491 }
492
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000493 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000494
Alex Bradbury04f06d92017-08-08 14:43:36 +0000495 /// getStartLoc - Gets location of the first token of this operand
496 SMLoc getStartLoc() const override { return StartLoc; }
497 /// getEndLoc - Gets location of the last token of this operand
498 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000499 /// True if this operand is for an RV64 instruction
500 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000501
502 unsigned getReg() const override {
503 assert(Kind == Register && "Invalid type access!");
504 return Reg.RegNum;
505 }
506
Ana Pazos9d6c5532018-10-04 21:50:54 +0000507 StringRef getSysReg() const {
508 assert(Kind == SystemRegister && "Invalid access!");
509 return StringRef(SysReg.Data, SysReg.Length);
510 }
511
Alex Bradbury04f06d92017-08-08 14:43:36 +0000512 const MCExpr *getImm() const {
513 assert(Kind == Immediate && "Invalid type access!");
514 return Imm.Val;
515 }
516
517 StringRef getToken() const {
518 assert(Kind == Token && "Invalid type access!");
519 return Tok;
520 }
521
522 void print(raw_ostream &OS) const override {
523 switch (Kind) {
524 case Immediate:
525 OS << *getImm();
526 break;
527 case Register:
528 OS << "<register x";
529 OS << getReg() << ">";
530 break;
531 case Token:
532 OS << "'" << getToken() << "'";
533 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000534 case SystemRegister:
535 OS << "<sysreg: " << getSysReg() << '>';
536 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000537 }
538 }
539
Alex Bradburya6e62482017-12-07 10:53:48 +0000540 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
541 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000542 auto Op = make_unique<RISCVOperand>(Token);
543 Op->Tok = Str;
544 Op->StartLoc = S;
545 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000546 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000547 return Op;
548 }
549
550 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000551 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000552 auto Op = make_unique<RISCVOperand>(Register);
553 Op->Reg.RegNum = RegNo;
554 Op->StartLoc = S;
555 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000556 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000557 return Op;
558 }
559
560 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000561 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000562 auto Op = make_unique<RISCVOperand>(Immediate);
563 Op->Imm.Val = Val;
564 Op->StartLoc = S;
565 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000566 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000567 return Op;
568 }
569
Ana Pazos9d6c5532018-10-04 21:50:54 +0000570 static std::unique_ptr<RISCVOperand>
571 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
572 auto Op = make_unique<RISCVOperand>(SystemRegister);
573 Op->SysReg.Data = Str.data();
574 Op->SysReg.Length = Str.size();
575 Op->SysReg.Encoding = Encoding;
576 Op->StartLoc = S;
577 Op->IsRV64 = IsRV64;
578 return Op;
579 }
580
Alex Bradbury04f06d92017-08-08 14:43:36 +0000581 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
582 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000583 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000584 RISCVMCExpr::VariantKind VK;
585 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000586
587 if (IsConstant)
588 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000589 else
590 Inst.addOperand(MCOperand::createExpr(Expr));
591 }
592
593 // Used by the TableGen Code
594 void addRegOperands(MCInst &Inst, unsigned N) const {
595 assert(N == 1 && "Invalid number of operands!");
596 Inst.addOperand(MCOperand::createReg(getReg()));
597 }
598
599 void addImmOperands(MCInst &Inst, unsigned N) const {
600 assert(N == 1 && "Invalid number of operands!");
601 addExpr(Inst, getImm());
602 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000603
604 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
605 assert(N == 1 && "Invalid number of operands!");
606 // isFenceArg has validated the operand, meaning this cast is safe
607 auto SE = cast<MCSymbolRefExpr>(getImm());
608
609 unsigned Imm = 0;
610 for (char c : SE->getSymbol().getName()) {
611 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000612 default:
613 llvm_unreachable("FenceArg must contain only [iorw]");
614 case 'i': Imm |= RISCVFenceField::I; break;
615 case 'o': Imm |= RISCVFenceField::O; break;
616 case 'r': Imm |= RISCVFenceField::R; break;
617 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000618 }
619 }
620 Inst.addOperand(MCOperand::createImm(Imm));
621 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000622
Ana Pazos9d6c5532018-10-04 21:50:54 +0000623 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
624 assert(N == 1 && "Invalid number of operands!");
625 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
626 }
627
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000628 // Returns the rounding mode represented by this RISCVOperand. Should only
629 // be called after checking isFRMArg.
630 RISCVFPRndMode::RoundingMode getRoundingMode() const {
631 // isFRMArg has validated the operand, meaning this cast is safe.
632 auto SE = cast<MCSymbolRefExpr>(getImm());
633 RISCVFPRndMode::RoundingMode FRM =
634 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
635 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
636 return FRM;
637 }
638
639 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
640 assert(N == 1 && "Invalid number of operands!");
641 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
642 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000643};
644} // end anonymous namespace.
645
646#define GET_REGISTER_MATCHER
647#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000648#include "RISCVGenAsmMatcher.inc"
649
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000650// Return the matching FPR64 register for the given FPR32.
651// FIXME: Ideally this function could be removed in favour of using
652// information from TableGen.
653unsigned convertFPR32ToFPR64(unsigned Reg) {
654 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000655 default:
656 llvm_unreachable("Not a recognised FPR32 register");
657 case RISCV::F0_32: return RISCV::F0_64;
658 case RISCV::F1_32: return RISCV::F1_64;
659 case RISCV::F2_32: return RISCV::F2_64;
660 case RISCV::F3_32: return RISCV::F3_64;
661 case RISCV::F4_32: return RISCV::F4_64;
662 case RISCV::F5_32: return RISCV::F5_64;
663 case RISCV::F6_32: return RISCV::F6_64;
664 case RISCV::F7_32: return RISCV::F7_64;
665 case RISCV::F8_32: return RISCV::F8_64;
666 case RISCV::F9_32: return RISCV::F9_64;
667 case RISCV::F10_32: return RISCV::F10_64;
668 case RISCV::F11_32: return RISCV::F11_64;
669 case RISCV::F12_32: return RISCV::F12_64;
670 case RISCV::F13_32: return RISCV::F13_64;
671 case RISCV::F14_32: return RISCV::F14_64;
672 case RISCV::F15_32: return RISCV::F15_64;
673 case RISCV::F16_32: return RISCV::F16_64;
674 case RISCV::F17_32: return RISCV::F17_64;
675 case RISCV::F18_32: return RISCV::F18_64;
676 case RISCV::F19_32: return RISCV::F19_64;
677 case RISCV::F20_32: return RISCV::F20_64;
678 case RISCV::F21_32: return RISCV::F21_64;
679 case RISCV::F22_32: return RISCV::F22_64;
680 case RISCV::F23_32: return RISCV::F23_64;
681 case RISCV::F24_32: return RISCV::F24_64;
682 case RISCV::F25_32: return RISCV::F25_64;
683 case RISCV::F26_32: return RISCV::F26_64;
684 case RISCV::F27_32: return RISCV::F27_64;
685 case RISCV::F28_32: return RISCV::F28_64;
686 case RISCV::F29_32: return RISCV::F29_64;
687 case RISCV::F30_32: return RISCV::F30_64;
688 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000689 }
690}
691
692unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
693 unsigned Kind) {
694 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
695 if (!Op.isReg())
696 return Match_InvalidOperand;
697
698 unsigned Reg = Op.getReg();
699 bool IsRegFPR32 =
700 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000701 bool IsRegFPR32C =
702 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000703
704 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000705 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
706 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
707 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000708 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
709 return Match_Success;
710 }
711 return Match_InvalidOperand;
712}
713
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000714bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000715 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000716 Twine Msg = "immediate must be an integer in the range") {
717 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
718 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
719}
720
Alex Bradbury04f06d92017-08-08 14:43:36 +0000721bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
722 OperandVector &Operands,
723 MCStreamer &Out,
724 uint64_t &ErrorInfo,
725 bool MatchingInlineAsm) {
726 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000727
Ana Pazos6b34051b2018-08-30 19:43:19 +0000728 auto Result =
729 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
730 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000731 default:
732 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000733 case Match_Success:
734 return processInstruction(Inst, IDLoc, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000735 case Match_MissingFeature:
736 return Error(IDLoc, "instruction use requires an option to be enabled");
737 case Match_MnemonicFail:
738 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000739 case Match_InvalidOperand: {
740 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000741 if (ErrorInfo != ~0U) {
742 if (ErrorInfo >= Operands.size())
743 return Error(ErrorLoc, "too few operands for instruction");
744
745 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
746 if (ErrorLoc == SMLoc())
747 ErrorLoc = IDLoc;
748 }
749 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000750 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000751 }
752
753 // Handle the case when the error message is of specific type
754 // other than the generic Match_InvalidOperand, and the
755 // corresponding operand is missing.
756 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
757 SMLoc ErrorLoc = IDLoc;
758 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
759 return Error(ErrorLoc, "too few operands for instruction");
760 }
761
762 switch(Result) {
763 default:
764 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000765 case Match_InvalidImmXLen:
766 if (isRV64()) {
767 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
768 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
769 }
770 return generateImmOutOfRangeError(Operands, ErrorInfo,
771 std::numeric_limits<int32_t>::min(),
772 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000773 case Match_InvalidUImmLog2XLen:
774 if (isRV64())
775 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
776 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000777 case Match_InvalidUImmLog2XLenNonZero:
778 if (isRV64())
779 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
780 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000781 case Match_InvalidUImm5:
782 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000783 case Match_InvalidSImm6:
784 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
785 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000786 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000787 return generateImmOutOfRangeError(
788 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000789 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000790 case Match_InvalidCLUIImm:
791 return generateImmOutOfRangeError(
792 Operands, ErrorInfo, 1, (1 << 5) - 1,
793 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000794 case Match_InvalidUImm7Lsb00:
795 return generateImmOutOfRangeError(
796 Operands, ErrorInfo, 0, (1 << 7) - 4,
797 "immediate must be a multiple of 4 bytes in the range");
798 case Match_InvalidUImm8Lsb00:
799 return generateImmOutOfRangeError(
800 Operands, ErrorInfo, 0, (1 << 8) - 4,
801 "immediate must be a multiple of 4 bytes in the range");
802 case Match_InvalidUImm8Lsb000:
803 return generateImmOutOfRangeError(
804 Operands, ErrorInfo, 0, (1 << 8) - 8,
805 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000806 case Match_InvalidSImm9Lsb0:
807 return generateImmOutOfRangeError(
808 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
809 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000810 case Match_InvalidUImm9Lsb000:
811 return generateImmOutOfRangeError(
812 Operands, ErrorInfo, 0, (1 << 9) - 8,
813 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000814 case Match_InvalidUImm10Lsb00NonZero:
815 return generateImmOutOfRangeError(
816 Operands, ErrorInfo, 4, (1 << 10) - 4,
817 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000818 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000819 return generateImmOutOfRangeError(
820 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000821 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000822 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000823 return generateImmOutOfRangeError(
824 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
825 "operand must be a symbol with %lo/%pcrel_lo modifier or an integer in "
826 "the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000827 case Match_InvalidSImm12Lsb0:
828 return generateImmOutOfRangeError(
829 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
830 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000831 case Match_InvalidSImm13Lsb0:
832 return generateImmOutOfRangeError(
833 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
834 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000835 case Match_InvalidUImm20LUI:
836 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
837 "operand must be a symbol with %hi() "
838 "modifier or an integer in the range");
839 case Match_InvalidUImm20AUIPC:
840 return generateImmOutOfRangeError(
841 Operands, ErrorInfo, 0, (1 << 20) - 1,
842 "operand must be a symbol with %pcrel_hi() modifier or an integer in "
843 "the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000844 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000845 return generateImmOutOfRangeError(
846 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
847 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000848 case Match_InvalidCSRSystemRegister: {
849 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
850 "operand must be a valid system register "
851 "name or an integer in the range");
852 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000853 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000854 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000855 return Error(
856 ErrorLoc,
857 "operand must be formed of letters selected in-order from 'iorw'");
858 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000859 case Match_InvalidFRMArg: {
860 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
861 return Error(
862 ErrorLoc,
863 "operand must be a valid floating point rounding mode mnemonic");
864 }
Shiva Chen98f93892018-04-25 14:18:55 +0000865 case Match_InvalidBareSymbol: {
866 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
867 return Error(ErrorLoc, "operand must be a bare symbol name");
868 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000869 }
870
871 llvm_unreachable("Unknown match type detected!");
872}
873
874bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
875 SMLoc &EndLoc) {
876 const AsmToken &Tok = getParser().getTok();
877 StartLoc = Tok.getLoc();
878 EndLoc = Tok.getEndLoc();
879 RegNo = 0;
880 StringRef Name = getLexer().getTok().getIdentifier();
881
882 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
883 getParser().Lex(); // Eat identifier token.
884 return false;
885 }
886
887 return Error(StartLoc, "invalid register name");
888}
889
Alex Bradbury8c345c52017-11-09 15:00:03 +0000890OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
891 bool AllowParens) {
892 SMLoc FirstS = getLoc();
893 bool HadParens = false;
894 AsmToken Buf[2];
895
896 // If this a parenthesised register name is allowed, parse it atomically
897 if (AllowParens && getLexer().is(AsmToken::LParen)) {
898 size_t ReadCount = getLexer().peekTokens(Buf);
899 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
900 HadParens = true;
901 getParser().Lex(); // Eat '('
902 }
903 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000904
905 switch (getLexer().getKind()) {
906 default:
907 return MatchOperand_NoMatch;
908 case AsmToken::Identifier:
909 StringRef Name = getLexer().getTok().getIdentifier();
910 unsigned RegNo = MatchRegisterName(Name);
911 if (RegNo == 0) {
912 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000913 if (RegNo == 0) {
914 if (HadParens)
915 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000916 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000917 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000918 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000919 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000920 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000921 SMLoc S = getLoc();
922 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000923 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000924 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000925 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000926
927 if (HadParens) {
928 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000929 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000930 }
931
Alex Bradbury04f06d92017-08-08 14:43:36 +0000932 return MatchOperand_Success;
933}
934
Ana Pazos9d6c5532018-10-04 21:50:54 +0000935OperandMatchResultTy
936RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
937 SMLoc S = getLoc();
938 const MCExpr *Res;
939
940 switch (getLexer().getKind()) {
941 default:
942 return MatchOperand_NoMatch;
943 case AsmToken::LParen:
944 case AsmToken::Minus:
945 case AsmToken::Plus:
946 case AsmToken::Integer:
947 case AsmToken::String: {
948 if (getParser().parseExpression(Res))
949 return MatchOperand_ParseFail;
950
951 auto *CE = dyn_cast<MCConstantExpr>(Res);
952 if (CE) {
953 int64_t Imm = CE->getValue();
954 if (isUInt<12>(Imm)) {
955 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
956 // Accept an immediate representing a named or un-named Sys Reg
957 // if the range is valid, regardless of the required features.
958 Operands.push_back(RISCVOperand::createSysReg(
959 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
960 return MatchOperand_Success;
961 }
962 }
963
964 Twine Msg = "immediate must be an integer in the range";
965 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
966 return MatchOperand_ParseFail;
967 }
968 case AsmToken::Identifier: {
969 StringRef Identifier;
970 if (getParser().parseIdentifier(Identifier))
971 return MatchOperand_ParseFail;
972
973 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
974 // Accept a named Sys Reg if the required features are present.
975 if (SysReg) {
976 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
977 Error(S, "system register use requires an option to be enabled");
978 return MatchOperand_ParseFail;
979 }
980 Operands.push_back(RISCVOperand::createSysReg(
981 Identifier, S, SysReg->Encoding, isRV64()));
982 return MatchOperand_Success;
983 }
984
985 Twine Msg = "operand must be a valid system register name "
986 "or an integer in the range";
987 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
988 return MatchOperand_ParseFail;
989 }
990 case AsmToken::Percent: {
991 // Discard operand with modifier.
992 Twine Msg = "immediate must be an integer in the range";
993 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
994 return MatchOperand_ParseFail;
995 }
996 }
997
998 return MatchOperand_NoMatch;
999}
1000
Alex Bradbury04f06d92017-08-08 14:43:36 +00001001OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001002 SMLoc S = getLoc();
1003 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1004 const MCExpr *Res;
1005
Alex Bradbury04f06d92017-08-08 14:43:36 +00001006 switch (getLexer().getKind()) {
1007 default:
1008 return MatchOperand_NoMatch;
1009 case AsmToken::LParen:
1010 case AsmToken::Minus:
1011 case AsmToken::Plus:
1012 case AsmToken::Integer:
1013 case AsmToken::String:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001014 if (getParser().parseExpression(Res))
1015 return MatchOperand_ParseFail;
1016 break;
1017 case AsmToken::Identifier: {
1018 StringRef Identifier;
1019 if (getParser().parseIdentifier(Identifier))
1020 return MatchOperand_ParseFail;
1021 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1022 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury04f06d92017-08-08 14:43:36 +00001023 break;
1024 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001025 case AsmToken::Percent:
1026 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001027 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001028
Alex Bradburya6e62482017-12-07 10:53:48 +00001029 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001030 return MatchOperand_Success;
1031}
1032
1033OperandMatchResultTy
1034RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1035 SMLoc S = getLoc();
1036 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1037
1038 if (getLexer().getKind() != AsmToken::Percent) {
1039 Error(getLoc(), "expected '%' for operand modifier");
1040 return MatchOperand_ParseFail;
1041 }
1042
1043 getParser().Lex(); // Eat '%'
1044
1045 if (getLexer().getKind() != AsmToken::Identifier) {
1046 Error(getLoc(), "expected valid identifier for operand modifier");
1047 return MatchOperand_ParseFail;
1048 }
1049 StringRef Identifier = getParser().getTok().getIdentifier();
1050 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1051 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1052 Error(getLoc(), "unrecognized operand modifier");
1053 return MatchOperand_ParseFail;
1054 }
1055
1056 getParser().Lex(); // Eat the identifier
1057 if (getLexer().getKind() != AsmToken::LParen) {
1058 Error(getLoc(), "expected '('");
1059 return MatchOperand_ParseFail;
1060 }
1061 getParser().Lex(); // Eat '('
1062
1063 const MCExpr *SubExpr;
1064 if (getParser().parseParenExpression(SubExpr, E)) {
1065 return MatchOperand_ParseFail;
1066 }
1067
1068 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001069 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001070 return MatchOperand_Success;
1071}
1072
Alex Bradbury68f73c12018-09-18 15:18:16 +00001073OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1074 SMLoc S = getLoc();
1075 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1076 const MCExpr *Res;
1077
1078 if (getLexer().getKind() != AsmToken::Identifier)
1079 return MatchOperand_NoMatch;
1080
1081 StringRef Identifier;
1082 if (getParser().parseIdentifier(Identifier))
1083 return MatchOperand_ParseFail;
1084
1085 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1086 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1087 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1088 return MatchOperand_Success;
1089}
1090
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001091OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1092 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1093 // both being acceptable forms. When parsing `jal ra, foo` this function
1094 // will be called for the `ra` register operand in an attempt to match the
1095 // single-operand alias. parseJALOffset must fail for this case. It would
1096 // seem logical to try parse the operand using parseImmediate and return
1097 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1098 // the second form rather than the first). We can't do this as there's no
1099 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1100 // is an identifier and is followed by a comma.
1101 if (getLexer().is(AsmToken::Identifier) &&
1102 getLexer().peekTok().is(AsmToken::Comma))
1103 return MatchOperand_NoMatch;
1104
1105 return parseImmediate(Operands);
1106}
1107
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001108OperandMatchResultTy
1109RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1110 if (getLexer().isNot(AsmToken::LParen)) {
1111 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001112 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001113 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001114
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001115 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001116 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001117
1118 if (parseRegister(Operands) != MatchOperand_Success) {
1119 Error(getLoc(), "expected register");
1120 return MatchOperand_ParseFail;
1121 }
1122
1123 if (getLexer().isNot(AsmToken::RParen)) {
1124 Error(getLoc(), "expected ')'");
1125 return MatchOperand_ParseFail;
1126 }
1127
1128 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001129 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001130
Alex Bradbury04f06d92017-08-08 14:43:36 +00001131 return MatchOperand_Success;
1132}
1133
Alex Bradburycd8688a2018-04-25 17:25:29 +00001134/// Looks at a token type and creates the relevant operand from this
1135/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001136/// true.
1137bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1138 // Check if the current operand has a custom associated parser, if so, try to
1139 // custom parse the operand, or fallback to the general approach.
1140 OperandMatchResultTy Result =
1141 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1142 if (Result == MatchOperand_Success)
1143 return false;
1144 if (Result == MatchOperand_ParseFail)
1145 return true;
1146
1147 // Attempt to parse token as a register.
1148 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001149 return false;
1150
1151 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001152 if (parseImmediate(Operands) == MatchOperand_Success) {
1153 // Parse memory base register if present
1154 if (getLexer().is(AsmToken::LParen))
1155 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001156 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001157 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001158
1159 // Finally we have exhausted all options and must declare defeat.
1160 Error(getLoc(), "unknown operand");
1161 return true;
1162}
1163
1164bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1165 StringRef Name, SMLoc NameLoc,
1166 OperandVector &Operands) {
1167 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001168 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001169
1170 // If there are no more operands, then finish
1171 if (getLexer().is(AsmToken::EndOfStatement))
1172 return false;
1173
1174 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001175 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001176 return true;
1177
1178 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001179 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001180 while (getLexer().is(AsmToken::Comma)) {
1181 // Consume comma token
1182 getLexer().Lex();
1183
1184 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001185 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001186 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001187
1188 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001189 }
1190
1191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1192 SMLoc Loc = getLexer().getLoc();
1193 getParser().eatToEndOfStatement();
1194 return Error(Loc, "unexpected token");
1195 }
1196
1197 getParser().Lex(); // Consume the EndOfStatement.
1198 return false;
1199}
1200
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001201bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1202 RISCVMCExpr::VariantKind &Kind,
1203 int64_t &Addend) {
1204 Kind = RISCVMCExpr::VK_RISCV_None;
1205 Addend = 0;
1206
1207 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1208 Kind = RE->getKind();
1209 Expr = RE->getSubExpr();
1210 }
1211
1212 // It's a simple symbol reference or constant with no addend.
1213 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1214 return true;
1215
1216 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1217 if (!BE)
1218 return false;
1219
1220 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1221 return false;
1222
1223 if (BE->getOpcode() != MCBinaryExpr::Add &&
1224 BE->getOpcode() != MCBinaryExpr::Sub)
1225 return false;
1226
1227 // We are able to support the subtraction of two symbol references
1228 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1229 isa<MCSymbolRefExpr>(BE->getRHS()))
1230 return true;
1231
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001232 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001233 // on here than we can deal with.
1234 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1235 if (!AddendExpr)
1236 return false;
1237
1238 Addend = AddendExpr->getValue();
1239 if (BE->getOpcode() == MCBinaryExpr::Sub)
1240 Addend = -Addend;
1241
1242 // It's some symbol reference + a constant addend
1243 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1244}
1245
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001246bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1247 // This returns false if this function recognizes the directive
1248 // regardless of whether it is successfully handles or reports an
1249 // error. Otherwise it returns true to give the generic parser a
1250 // chance at recognizing it.
1251 StringRef IDVal = DirectiveID.getString();
1252
1253 if (IDVal == ".option")
1254 return parseDirectiveOption();
1255
1256 return true;
1257}
1258
1259bool RISCVAsmParser::parseDirectiveOption() {
1260 MCAsmParser &Parser = getParser();
1261 // Get the option token.
1262 AsmToken Tok = Parser.getTok();
1263 // At the moment only identifiers are supported.
1264 if (Tok.isNot(AsmToken::Identifier))
1265 return Error(Parser.getTok().getLoc(),
1266 "unexpected token, expected identifier");
1267
1268 StringRef Option = Tok.getIdentifier();
1269
1270 if (Option == "rvc") {
1271 getTargetStreamer().emitDirectiveOptionRVC();
1272
1273 Parser.Lex();
1274 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1275 return Error(Parser.getTok().getLoc(),
1276 "unexpected token, expected end of statement");
1277
1278 setFeatureBits(RISCV::FeatureStdExtC, "c");
1279 return false;
1280 }
1281
1282 if (Option == "norvc") {
1283 getTargetStreamer().emitDirectiveOptionNoRVC();
1284
1285 Parser.Lex();
1286 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1287 return Error(Parser.getTok().getLoc(),
1288 "unexpected token, expected end of statement");
1289
1290 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1291 return false;
1292 }
1293
1294 // Unknown option.
1295 Warning(Parser.getTok().getLoc(),
1296 "unknown option, expected 'rvc' or 'norvc'");
1297 Parser.eatToEndOfStatement();
1298 return false;
1299}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001300
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001301void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1302 MCInst CInst;
1303 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1304 CInst.setLoc(Inst.getLoc());
1305 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1306}
1307
1308void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1309 MCStreamer &Out) {
1310 if (isInt<32>(Value)) {
1311 // Emits the MC instructions for loading a 32-bit constant into a register.
1312 //
1313 // Depending on the active bits in the immediate Value v, the following
1314 // instruction sequences are emitted:
1315 //
1316 // v == 0 : ADDI(W)
1317 // v[0,12) != 0 && v[12,32) == 0 : ADDI(W)
1318 // v[0,12) == 0 && v[12,32) != 0 : LUI
1319 // v[0,32) != 0 : LUI+ADDI(W)
1320 //
1321 int64_t Hi20 = ((Value + 0x800) >> 12) & 0xFFFFF;
1322 int64_t Lo12 = SignExtend64<12>(Value);
1323 unsigned SrcReg = RISCV::X0;
1324
1325 if (Hi20) {
1326 emitToStreamer(Out,
1327 MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Hi20));
1328 SrcReg = DestReg;
1329 }
1330
1331 if (Lo12 || Hi20 == 0) {
1332 unsigned AddiOpcode =
1333 STI->hasFeature(RISCV::Feature64Bit) ? RISCV::ADDIW : RISCV::ADDI;
1334 emitToStreamer(Out, MCInstBuilder(AddiOpcode)
1335 .addReg(DestReg)
1336 .addReg(SrcReg)
1337 .addImm(Lo12));
1338 }
1339 return;
1340 }
1341 assert(STI->hasFeature(RISCV::Feature64Bit) &&
1342 "Target must be 64-bit to support a >32-bit constant");
1343
1344 // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
1345 // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emmitted. Note
1346 // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits
1347 // while the following ADDI instructions contribute up to 12 bits each.
1348 //
1349 // On the first glance, implementing this seems to be possible by simply
1350 // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left
1351 // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the
1352 // fact that ADDI performs a sign extended addition, doing it like that would
1353 // only be possible when at most 11 bits of the ADDI instructions are used.
1354 // Using all 12 bits of the ADDI instructions, like done by GAS, actually
1355 // requires that the constant is processed starting with the least significant
1356 // bit.
1357 //
1358 // In the following, constants are processed from LSB to MSB but instruction
1359 // emission is performed from MSB to LSB by recursively calling
1360 // emitLoadImm. In each recursion, first the lowest 12 bits are removed
1361 // from the constant and the optimal shift amount, which can be greater than
1362 // 12 bits if the constant is sparse, is determined. Then, the shifted
1363 // remaining constant is processed recursively and gets emitted as soon as it
1364 // fits into 32 bits. The emission of the shifts and additions is subsequently
1365 // performed when the recursion returns.
1366 //
1367 int64_t Lo12 = SignExtend64<12>(Value);
1368 int64_t Hi52 = (Value + 0x800) >> 12;
1369 int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52);
1370 Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount);
1371
1372 emitLoadImm(DestReg, Hi52, Out);
1373
1374 emitToStreamer(Out, MCInstBuilder(RISCV::SLLI)
1375 .addReg(DestReg)
1376 .addReg(DestReg)
1377 .addImm(ShiftAmount));
1378
1379 if (Lo12)
1380 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1381 .addReg(DestReg)
1382 .addReg(DestReg)
1383 .addImm(Lo12));
1384}
1385
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001386void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1387 MCStreamer &Out) {
1388 // The local load address pseudo-instruction "lla" is used in PC-relative
1389 // addressing of symbols:
1390 // lla rdest, symbol
1391 // expands to
1392 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1393 // ADDI rdest, %pcrel_lo(TmpLabel)
1394 MCContext &Ctx = getContext();
1395
1396 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1397 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1398 Out.EmitLabel(TmpLabel);
1399
1400 MCOperand DestReg = Inst.getOperand(0);
1401 const RISCVMCExpr *Symbol = RISCVMCExpr::create(
1402 Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx);
1403
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001404 emitToStreamer(
1405 Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001406
1407 const MCExpr *RefToLinkTmpLabel =
1408 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1409 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1410
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001411 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1412 .addOperand(DestReg)
1413 .addOperand(DestReg)
1414 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001415}
1416
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001417bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1418 MCStreamer &Out) {
1419 Inst.setLoc(IDLoc);
1420
1421 if (Inst.getOpcode() == RISCV::PseudoLI) {
1422 auto Reg = Inst.getOperand(0).getReg();
1423 int64_t Imm = Inst.getOperand(1).getImm();
1424 // On RV32 the immediate here can either be a signed or an unsigned
1425 // 32-bit number. Sign extension has to be performed to ensure that Imm
1426 // represents the expected signed 64-bit number.
1427 if (!isRV64())
1428 Imm = SignExtend64<32>(Imm);
1429 emitLoadImm(Reg, Imm, Out);
1430 return false;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001431 } else if (Inst.getOpcode() == RISCV::PseudoLLA) {
1432 emitLoadLocalAddress(Inst, IDLoc, Out);
1433 return false;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001434 }
1435
1436 emitToStreamer(Out, Inst);
1437 return false;
1438}
1439
Alex Bradbury04f06d92017-08-08 14:43:36 +00001440extern "C" void LLVMInitializeRISCVAsmParser() {
1441 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1442 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1443}