blob: 2e80dca45407bcd692d704713e16591ab71841b7 [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 Bradburybca0c3c2018-05-11 17:30:28 +000013#include "MCTargetDesc/RISCVTargetStreamer.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
89 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +000090 OperandMatchResultTy parseRegister(OperandVector &Operands,
91 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000092 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +000093 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +000094 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +000095 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +000096
Alex Bradbury68f73c12018-09-18 15:18:16 +000097 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +000098
Alex Bradburybca0c3c2018-05-11 17:30:28 +000099 bool parseDirectiveOption();
100
101 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
102 if (!(getSTI().getFeatureBits()[Feature])) {
103 MCSubtargetInfo &STI = copySTI();
104 setAvailableFeatures(
105 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
106 }
107 }
108
109 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
110 if (getSTI().getFeatureBits()[Feature]) {
111 MCSubtargetInfo &STI = copySTI();
112 setAvailableFeatures(
113 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
114 }
115 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000116public:
117 enum RISCVMatchResultTy {
118 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
119#define GET_OPERAND_DIAGNOSTIC_TYPES
120#include "RISCVGenAsmMatcher.inc"
121#undef GET_OPERAND_DIAGNOSTIC_TYPES
122 };
123
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000124 static bool classifySymbolRef(const MCExpr *Expr,
125 RISCVMCExpr::VariantKind &Kind,
126 int64_t &Addend);
127
Alex Bradbury04f06d92017-08-08 14:43:36 +0000128 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
129 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000130 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000131 Parser.addAliasForDirective(".half", ".2byte");
132 Parser.addAliasForDirective(".hword", ".2byte");
133 Parser.addAliasForDirective(".word", ".4byte");
134 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000135 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
136 }
137};
138
139/// RISCVOperand - Instances of this class represent a parsed machine
140/// instruction
141struct RISCVOperand : public MCParsedAsmOperand {
142
143 enum KindTy {
144 Token,
145 Register,
146 Immediate,
147 } Kind;
148
Alex Bradburya6e62482017-12-07 10:53:48 +0000149 bool IsRV64;
150
Alex Bradbury04f06d92017-08-08 14:43:36 +0000151 struct RegOp {
152 unsigned RegNum;
153 };
154
155 struct ImmOp {
156 const MCExpr *Val;
157 };
158
159 SMLoc StartLoc, EndLoc;
160 union {
161 StringRef Tok;
162 RegOp Reg;
163 ImmOp Imm;
164 };
165
166 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
167
168public:
169 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
170 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000171 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000172 StartLoc = o.StartLoc;
173 EndLoc = o.EndLoc;
174 switch (Kind) {
175 case Register:
176 Reg = o.Reg;
177 break;
178 case Immediate:
179 Imm = o.Imm;
180 break;
181 case Token:
182 Tok = o.Tok;
183 break;
184 }
185 }
186
187 bool isToken() const override { return Kind == Token; }
188 bool isReg() const override { return Kind == Register; }
189 bool isImm() const override { return Kind == Immediate; }
190 bool isMem() const override { return false; }
191
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000192 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
193 RISCVMCExpr::VariantKind &VK) {
194 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000195 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000196 return RE->evaluateAsConstant(Imm);
197 }
198
199 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000200 VK = RISCVMCExpr::VK_RISCV_None;
201 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000202 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000203 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000204
205 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000206 }
207
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000208 // True if operand is a symbol with no modifiers, or a constant with no
209 // modifiers and isShiftedInt<N-1, 1>(Op).
210 template <int N> bool isBareSimmNLsb0() const {
211 int64_t Imm;
212 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000213 if (!isImm())
214 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000215 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000216 bool IsValid;
217 if (!IsConstantImm)
218 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
219 else
220 IsValid = isShiftedInt<N - 1, 1>(Imm);
221 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000222 }
223
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000224 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
225
Shiva Chen98f93892018-04-25 14:18:55 +0000226 bool isBareSymbol() const {
227 int64_t Imm;
228 RISCVMCExpr::VariantKind VK;
229 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000230 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000231 return false;
232 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
233 VK == RISCVMCExpr::VK_RISCV_None;
234 }
235
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000236 /// Return true if the operand is a valid for the fence instruction e.g.
237 /// ('iorw').
238 bool isFenceArg() const {
239 if (!isImm())
240 return false;
241 const MCExpr *Val = getImm();
242 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
243 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
244 return false;
245
246 StringRef Str = SVal->getSymbol().getName();
247 // Letters must be unique, taken from 'iorw', and in ascending order. This
248 // holds as long as each individual character is one of 'iorw' and is
249 // greater than the previous character.
250 char Prev = '\0';
251 for (char c : Str) {
252 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
253 return false;
254 if (c <= Prev)
255 return false;
256 Prev = c;
257 }
258 return true;
259 }
260
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000261 /// Return true if the operand is a valid floating point rounding mode.
262 bool isFRMArg() const {
263 if (!isImm())
264 return false;
265 const MCExpr *Val = getImm();
266 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
267 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
268 return false;
269
270 StringRef Str = SVal->getSymbol().getName();
271
272 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
273 }
274
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000275 bool isImmXLen() const {
276 int64_t Imm;
277 RISCVMCExpr::VariantKind VK;
278 if (!isImm())
279 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000280 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000281 // Given only Imm, ensuring that the actually specified constant is either
282 // a signed or unsigned 64-bit number is unfortunately impossible.
283 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
284 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
285 }
286
Alex Bradburya6e62482017-12-07 10:53:48 +0000287 bool isUImmLog2XLen() const {
288 int64_t Imm;
289 RISCVMCExpr::VariantKind VK;
290 if (!isImm())
291 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000292 if (!evaluateConstantImm(getImm(), Imm, VK) ||
293 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000294 return false;
295 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
296 }
297
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000298 bool isUImmLog2XLenNonZero() const {
299 int64_t Imm;
300 RISCVMCExpr::VariantKind VK;
301 if (!isImm())
302 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000303 if (!evaluateConstantImm(getImm(), Imm, VK) ||
304 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000305 return false;
306 if (Imm == 0)
307 return false;
308 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
309 }
310
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000311 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000312 int64_t Imm;
313 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000314 if (!isImm())
315 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000316 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000317 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000318 }
319
Alex Bradbury60714f92017-12-13 09:32:55 +0000320 bool isUImm5NonZero() const {
321 int64_t Imm;
322 RISCVMCExpr::VariantKind VK;
323 if (!isImm())
324 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000325 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000326 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
327 VK == RISCVMCExpr::VK_RISCV_None;
328 }
329
Alex Bradbury581d6b02017-12-13 09:41:21 +0000330 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000331 if (!isImm())
332 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000333 RISCVMCExpr::VariantKind VK;
334 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000335 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000336 return IsConstantImm && isInt<6>(Imm) &&
337 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000338 }
339
Shiva Chenb22c1d22018-02-02 02:43:23 +0000340 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000341 if (!isImm())
342 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000343 RISCVMCExpr::VariantKind VK;
344 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000345 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000346 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
347 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000348 }
349
Shiva Chen7c172422018-02-22 15:02:28 +0000350 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000351 if (!isImm())
352 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000353 int64_t Imm;
354 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000355 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000356 return IsConstantImm && (Imm != 0) &&
357 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
358 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000359 }
360
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000361 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000362 if (!isImm())
363 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000364 int64_t Imm;
365 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000366 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000367 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
368 VK == RISCVMCExpr::VK_RISCV_None;
369 }
370
371 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000372 if (!isImm())
373 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000374 int64_t Imm;
375 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000376 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000377 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
378 VK == RISCVMCExpr::VK_RISCV_None;
379 }
380
381 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000382 if (!isImm())
383 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000384 int64_t Imm;
385 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000386 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000387 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
388 VK == RISCVMCExpr::VK_RISCV_None;
389 }
390
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000391 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
392
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000393 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000394 if (!isImm())
395 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000396 int64_t Imm;
397 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000398 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000399 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
400 VK == RISCVMCExpr::VK_RISCV_None;
401 }
402
Alex Bradbury60714f92017-12-13 09:32:55 +0000403 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000404 if (!isImm())
405 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000406 int64_t Imm;
407 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000408 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000409 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
410 VK == RISCVMCExpr::VK_RISCV_None;
411 }
412
Alex Bradbury04f06d92017-08-08 14:43:36 +0000413 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000414 RISCVMCExpr::VariantKind VK;
415 int64_t Imm;
416 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000417 if (!isImm())
418 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000419 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000420 if (!IsConstantImm)
421 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
422 else
423 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000424 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000425 VK == RISCVMCExpr::VK_RISCV_LO ||
426 VK == RISCVMCExpr::VK_RISCV_PCREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000427 }
428
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000429 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
430
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000431 bool isUImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000432 int64_t Imm;
433 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000434 if (!isImm())
435 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000436 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000437 return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000438 }
439
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000440 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000441
Shiva Chenb22c1d22018-02-02 02:43:23 +0000442 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000443 if (!isImm())
444 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000445 int64_t Imm;
446 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000447 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000448 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000449 VK == RISCVMCExpr::VK_RISCV_None;
450 }
451
Alex Bradbury74340f12018-09-18 15:08:35 +0000452 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000453 RISCVMCExpr::VariantKind VK;
454 int64_t Imm;
455 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000456 if (!isImm())
457 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000458 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000459 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000460 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury74340f12018-09-18 15:08:35 +0000461 return IsValid && VK == RISCVMCExpr::VK_RISCV_HI;
462 } else {
463 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
464 VK == RISCVMCExpr::VK_RISCV_HI);
465 }
466 }
467
468 bool isUImm20AUIPC() const {
469 RISCVMCExpr::VariantKind VK;
470 int64_t Imm;
471 bool IsValid;
472 if (!isImm())
473 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000474 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000475 if (!IsConstantImm) {
476 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
477 return IsValid && VK == RISCVMCExpr::VK_RISCV_PCREL_HI;
478 } else {
479 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
480 VK == RISCVMCExpr::VK_RISCV_PCREL_HI);
481 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000482 }
483
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000484 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000485
Alex Bradbury04f06d92017-08-08 14:43:36 +0000486 /// getStartLoc - Gets location of the first token of this operand
487 SMLoc getStartLoc() const override { return StartLoc; }
488 /// getEndLoc - Gets location of the last token of this operand
489 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000490 /// True if this operand is for an RV64 instruction
491 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000492
493 unsigned getReg() const override {
494 assert(Kind == Register && "Invalid type access!");
495 return Reg.RegNum;
496 }
497
498 const MCExpr *getImm() const {
499 assert(Kind == Immediate && "Invalid type access!");
500 return Imm.Val;
501 }
502
503 StringRef getToken() const {
504 assert(Kind == Token && "Invalid type access!");
505 return Tok;
506 }
507
508 void print(raw_ostream &OS) const override {
509 switch (Kind) {
510 case Immediate:
511 OS << *getImm();
512 break;
513 case Register:
514 OS << "<register x";
515 OS << getReg() << ">";
516 break;
517 case Token:
518 OS << "'" << getToken() << "'";
519 break;
520 }
521 }
522
Alex Bradburya6e62482017-12-07 10:53:48 +0000523 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
524 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000525 auto Op = make_unique<RISCVOperand>(Token);
526 Op->Tok = Str;
527 Op->StartLoc = S;
528 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000529 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000530 return Op;
531 }
532
533 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000534 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000535 auto Op = make_unique<RISCVOperand>(Register);
536 Op->Reg.RegNum = RegNo;
537 Op->StartLoc = S;
538 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000539 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000540 return Op;
541 }
542
543 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000544 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000545 auto Op = make_unique<RISCVOperand>(Immediate);
546 Op->Imm.Val = Val;
547 Op->StartLoc = S;
548 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000549 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000550 return Op;
551 }
552
553 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
554 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000555 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000556 RISCVMCExpr::VariantKind VK;
557 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000558
559 if (IsConstant)
560 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000561 else
562 Inst.addOperand(MCOperand::createExpr(Expr));
563 }
564
565 // Used by the TableGen Code
566 void addRegOperands(MCInst &Inst, unsigned N) const {
567 assert(N == 1 && "Invalid number of operands!");
568 Inst.addOperand(MCOperand::createReg(getReg()));
569 }
570
571 void addImmOperands(MCInst &Inst, unsigned N) const {
572 assert(N == 1 && "Invalid number of operands!");
573 addExpr(Inst, getImm());
574 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000575
576 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
577 assert(N == 1 && "Invalid number of operands!");
578 // isFenceArg has validated the operand, meaning this cast is safe
579 auto SE = cast<MCSymbolRefExpr>(getImm());
580
581 unsigned Imm = 0;
582 for (char c : SE->getSymbol().getName()) {
583 switch (c) {
584 default: llvm_unreachable("FenceArg must contain only [iorw]");
585 case 'i': Imm |= RISCVFenceField::I; break;
586 case 'o': Imm |= RISCVFenceField::O; break;
587 case 'r': Imm |= RISCVFenceField::R; break;
588 case 'w': Imm |= RISCVFenceField::W; break;
589 }
590 }
591 Inst.addOperand(MCOperand::createImm(Imm));
592 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000593
594 // Returns the rounding mode represented by this RISCVOperand. Should only
595 // be called after checking isFRMArg.
596 RISCVFPRndMode::RoundingMode getRoundingMode() const {
597 // isFRMArg has validated the operand, meaning this cast is safe.
598 auto SE = cast<MCSymbolRefExpr>(getImm());
599 RISCVFPRndMode::RoundingMode FRM =
600 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
601 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
602 return FRM;
603 }
604
605 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
606 assert(N == 1 && "Invalid number of operands!");
607 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
608 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000609};
610} // end anonymous namespace.
611
612#define GET_REGISTER_MATCHER
613#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000614#include "RISCVGenAsmMatcher.inc"
615
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000616// Return the matching FPR64 register for the given FPR32.
617// FIXME: Ideally this function could be removed in favour of using
618// information from TableGen.
619unsigned convertFPR32ToFPR64(unsigned Reg) {
620 switch (Reg) {
621 default:
622 llvm_unreachable("Not a recognised FPR32 register");
623 case RISCV::F0_32: return RISCV::F0_64;
624 case RISCV::F1_32: return RISCV::F1_64;
625 case RISCV::F2_32: return RISCV::F2_64;
626 case RISCV::F3_32: return RISCV::F3_64;
627 case RISCV::F4_32: return RISCV::F4_64;
628 case RISCV::F5_32: return RISCV::F5_64;
629 case RISCV::F6_32: return RISCV::F6_64;
630 case RISCV::F7_32: return RISCV::F7_64;
631 case RISCV::F8_32: return RISCV::F8_64;
632 case RISCV::F9_32: return RISCV::F9_64;
633 case RISCV::F10_32: return RISCV::F10_64;
634 case RISCV::F11_32: return RISCV::F11_64;
635 case RISCV::F12_32: return RISCV::F12_64;
636 case RISCV::F13_32: return RISCV::F13_64;
637 case RISCV::F14_32: return RISCV::F14_64;
638 case RISCV::F15_32: return RISCV::F15_64;
639 case RISCV::F16_32: return RISCV::F16_64;
640 case RISCV::F17_32: return RISCV::F17_64;
641 case RISCV::F18_32: return RISCV::F18_64;
642 case RISCV::F19_32: return RISCV::F19_64;
643 case RISCV::F20_32: return RISCV::F20_64;
644 case RISCV::F21_32: return RISCV::F21_64;
645 case RISCV::F22_32: return RISCV::F22_64;
646 case RISCV::F23_32: return RISCV::F23_64;
647 case RISCV::F24_32: return RISCV::F24_64;
648 case RISCV::F25_32: return RISCV::F25_64;
649 case RISCV::F26_32: return RISCV::F26_64;
650 case RISCV::F27_32: return RISCV::F27_64;
651 case RISCV::F28_32: return RISCV::F28_64;
652 case RISCV::F29_32: return RISCV::F29_64;
653 case RISCV::F30_32: return RISCV::F30_64;
654 case RISCV::F31_32: return RISCV::F31_64;
655 }
656}
657
658unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
659 unsigned Kind) {
660 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
661 if (!Op.isReg())
662 return Match_InvalidOperand;
663
664 unsigned Reg = Op.getReg();
665 bool IsRegFPR32 =
666 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000667 bool IsRegFPR32C =
668 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000669
670 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000671 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
672 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
673 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000674 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
675 return Match_Success;
676 }
677 return Match_InvalidOperand;
678}
679
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000680bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000681 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000682 Twine Msg = "immediate must be an integer in the range") {
683 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
684 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
685}
686
Alex Bradbury04f06d92017-08-08 14:43:36 +0000687bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
688 OperandVector &Operands,
689 MCStreamer &Out,
690 uint64_t &ErrorInfo,
691 bool MatchingInlineAsm) {
692 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000693
Ana Pazos6b34051b2018-08-30 19:43:19 +0000694 auto Result =
695 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
696 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000697 default:
698 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000699 case Match_Success:
700 return processInstruction(Inst, IDLoc, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000701 case Match_MissingFeature:
702 return Error(IDLoc, "instruction use requires an option to be enabled");
703 case Match_MnemonicFail:
704 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000705 case Match_InvalidOperand: {
706 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000707 if (ErrorInfo != ~0U) {
708 if (ErrorInfo >= Operands.size())
709 return Error(ErrorLoc, "too few operands for instruction");
710
711 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
712 if (ErrorLoc == SMLoc())
713 ErrorLoc = IDLoc;
714 }
715 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000716 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000717 }
718
719 // Handle the case when the error message is of specific type
720 // other than the generic Match_InvalidOperand, and the
721 // corresponding operand is missing.
722 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
723 SMLoc ErrorLoc = IDLoc;
724 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
725 return Error(ErrorLoc, "too few operands for instruction");
726 }
727
728 switch(Result) {
729 default:
730 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000731 case Match_InvalidImmXLen:
732 if (isRV64()) {
733 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
734 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
735 }
736 return generateImmOutOfRangeError(Operands, ErrorInfo,
737 std::numeric_limits<int32_t>::min(),
738 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000739 case Match_InvalidUImmLog2XLen:
740 if (isRV64())
741 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
742 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000743 case Match_InvalidUImmLog2XLenNonZero:
744 if (isRV64())
745 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
746 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000747 case Match_InvalidUImm5:
748 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000749 case Match_InvalidSImm6:
750 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
751 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000752 case Match_InvalidSImm6NonZero:
753 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
754 (1 << 5) - 1,
755 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000756 case Match_InvalidCLUIImm:
757 return generateImmOutOfRangeError(
758 Operands, ErrorInfo, 1, (1 << 5) - 1,
759 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000760 case Match_InvalidUImm7Lsb00:
761 return generateImmOutOfRangeError(
762 Operands, ErrorInfo, 0, (1 << 7) - 4,
763 "immediate must be a multiple of 4 bytes in the range");
764 case Match_InvalidUImm8Lsb00:
765 return generateImmOutOfRangeError(
766 Operands, ErrorInfo, 0, (1 << 8) - 4,
767 "immediate must be a multiple of 4 bytes in the range");
768 case Match_InvalidUImm8Lsb000:
769 return generateImmOutOfRangeError(
770 Operands, ErrorInfo, 0, (1 << 8) - 8,
771 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000772 case Match_InvalidSImm9Lsb0:
773 return generateImmOutOfRangeError(
774 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
775 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000776 case Match_InvalidUImm9Lsb000:
777 return generateImmOutOfRangeError(
778 Operands, ErrorInfo, 0, (1 << 9) - 8,
779 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000780 case Match_InvalidUImm10Lsb00NonZero:
781 return generateImmOutOfRangeError(
782 Operands, ErrorInfo, 4, (1 << 10) - 4,
783 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000784 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000785 return generateImmOutOfRangeError(
786 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000787 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000788 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000789 return generateImmOutOfRangeError(
790 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
791 "operand must be a symbol with %lo/%pcrel_lo modifier or an integer in "
792 "the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000793 case Match_InvalidSImm12Lsb0:
794 return generateImmOutOfRangeError(
795 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
796 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000797 case Match_InvalidUImm12:
798 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1);
799 case Match_InvalidSImm13Lsb0:
800 return generateImmOutOfRangeError(
801 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
802 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000803 case Match_InvalidUImm20LUI:
804 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
805 "operand must be a symbol with %hi() "
806 "modifier or an integer in the range");
807 case Match_InvalidUImm20AUIPC:
808 return generateImmOutOfRangeError(
809 Operands, ErrorInfo, 0, (1 << 20) - 1,
810 "operand must be a symbol with %pcrel_hi() modifier or an integer in "
811 "the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000812 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000813 return generateImmOutOfRangeError(
814 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
815 "immediate must be a multiple of 2 bytes in the range");
816 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000817 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000818 return Error(
819 ErrorLoc,
820 "operand must be formed of letters selected in-order from 'iorw'");
821 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000822 case Match_InvalidFRMArg: {
823 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
824 return Error(
825 ErrorLoc,
826 "operand must be a valid floating point rounding mode mnemonic");
827 }
Shiva Chen98f93892018-04-25 14:18:55 +0000828 case Match_InvalidBareSymbol: {
829 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
830 return Error(ErrorLoc, "operand must be a bare symbol name");
831 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000832 }
833
834 llvm_unreachable("Unknown match type detected!");
835}
836
837bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
838 SMLoc &EndLoc) {
839 const AsmToken &Tok = getParser().getTok();
840 StartLoc = Tok.getLoc();
841 EndLoc = Tok.getEndLoc();
842 RegNo = 0;
843 StringRef Name = getLexer().getTok().getIdentifier();
844
845 if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) {
846 getParser().Lex(); // Eat identifier token.
847 return false;
848 }
849
850 return Error(StartLoc, "invalid register name");
851}
852
Alex Bradbury8c345c52017-11-09 15:00:03 +0000853OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
854 bool AllowParens) {
855 SMLoc FirstS = getLoc();
856 bool HadParens = false;
857 AsmToken Buf[2];
858
859 // If this a parenthesised register name is allowed, parse it atomically
860 if (AllowParens && getLexer().is(AsmToken::LParen)) {
861 size_t ReadCount = getLexer().peekTokens(Buf);
862 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
863 HadParens = true;
864 getParser().Lex(); // Eat '('
865 }
866 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000867
868 switch (getLexer().getKind()) {
869 default:
870 return MatchOperand_NoMatch;
871 case AsmToken::Identifier:
872 StringRef Name = getLexer().getTok().getIdentifier();
873 unsigned RegNo = MatchRegisterName(Name);
874 if (RegNo == 0) {
875 RegNo = MatchRegisterAltName(Name);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000876 if (RegNo == 0) {
877 if (HadParens)
878 getLexer().UnLex(Buf[0]);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000879 return MatchOperand_NoMatch;
Alex Bradbury8c345c52017-11-09 15:00:03 +0000880 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000881 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000882 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +0000883 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000884 SMLoc S = getLoc();
885 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000886 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +0000887 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000888 }
Alex Bradbury8c345c52017-11-09 15:00:03 +0000889
890 if (HadParens) {
891 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +0000892 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +0000893 }
894
Alex Bradbury04f06d92017-08-08 14:43:36 +0000895 return MatchOperand_Success;
896}
897
898OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000899 SMLoc S = getLoc();
900 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
901 const MCExpr *Res;
902
Alex Bradbury04f06d92017-08-08 14:43:36 +0000903 switch (getLexer().getKind()) {
904 default:
905 return MatchOperand_NoMatch;
906 case AsmToken::LParen:
907 case AsmToken::Minus:
908 case AsmToken::Plus:
909 case AsmToken::Integer:
910 case AsmToken::String:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000911 if (getParser().parseExpression(Res))
912 return MatchOperand_ParseFail;
913 break;
914 case AsmToken::Identifier: {
915 StringRef Identifier;
916 if (getParser().parseIdentifier(Identifier))
917 return MatchOperand_ParseFail;
918 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
919 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury04f06d92017-08-08 14:43:36 +0000920 break;
921 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000922 case AsmToken::Percent:
923 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000924 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000925
Alex Bradburya6e62482017-12-07 10:53:48 +0000926 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000927 return MatchOperand_Success;
928}
929
930OperandMatchResultTy
931RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
932 SMLoc S = getLoc();
933 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
934
935 if (getLexer().getKind() != AsmToken::Percent) {
936 Error(getLoc(), "expected '%' for operand modifier");
937 return MatchOperand_ParseFail;
938 }
939
940 getParser().Lex(); // Eat '%'
941
942 if (getLexer().getKind() != AsmToken::Identifier) {
943 Error(getLoc(), "expected valid identifier for operand modifier");
944 return MatchOperand_ParseFail;
945 }
946 StringRef Identifier = getParser().getTok().getIdentifier();
947 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
948 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
949 Error(getLoc(), "unrecognized operand modifier");
950 return MatchOperand_ParseFail;
951 }
952
953 getParser().Lex(); // Eat the identifier
954 if (getLexer().getKind() != AsmToken::LParen) {
955 Error(getLoc(), "expected '('");
956 return MatchOperand_ParseFail;
957 }
958 getParser().Lex(); // Eat '('
959
960 const MCExpr *SubExpr;
961 if (getParser().parseParenExpression(SubExpr, E)) {
962 return MatchOperand_ParseFail;
963 }
964
965 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +0000966 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000967 return MatchOperand_Success;
968}
969
Alex Bradbury68f73c12018-09-18 15:18:16 +0000970OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
971 SMLoc S = getLoc();
972 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
973 const MCExpr *Res;
974
975 if (getLexer().getKind() != AsmToken::Identifier)
976 return MatchOperand_NoMatch;
977
978 StringRef Identifier;
979 if (getParser().parseIdentifier(Identifier))
980 return MatchOperand_ParseFail;
981
982 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
983 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
984 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
985 return MatchOperand_Success;
986}
987
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000988OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
989 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
990 // both being acceptable forms. When parsing `jal ra, foo` this function
991 // will be called for the `ra` register operand in an attempt to match the
992 // single-operand alias. parseJALOffset must fail for this case. It would
993 // seem logical to try parse the operand using parseImmediate and return
994 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
995 // the second form rather than the first). We can't do this as there's no
996 // way of rewinding the lexer state. Instead, return NoMatch if this operand
997 // is an identifier and is followed by a comma.
998 if (getLexer().is(AsmToken::Identifier) &&
999 getLexer().peekTok().is(AsmToken::Comma))
1000 return MatchOperand_NoMatch;
1001
1002 return parseImmediate(Operands);
1003}
1004
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001005OperandMatchResultTy
1006RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1007 if (getLexer().isNot(AsmToken::LParen)) {
1008 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001009 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001010 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001011
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001012 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001013 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001014
1015 if (parseRegister(Operands) != MatchOperand_Success) {
1016 Error(getLoc(), "expected register");
1017 return MatchOperand_ParseFail;
1018 }
1019
1020 if (getLexer().isNot(AsmToken::RParen)) {
1021 Error(getLoc(), "expected ')'");
1022 return MatchOperand_ParseFail;
1023 }
1024
1025 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001026 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001027
Alex Bradbury04f06d92017-08-08 14:43:36 +00001028 return MatchOperand_Success;
1029}
1030
Alex Bradburycd8688a2018-04-25 17:25:29 +00001031/// Looks at a token type and creates the relevant operand from this
1032/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001033/// true.
1034bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1035 // Check if the current operand has a custom associated parser, if so, try to
1036 // custom parse the operand, or fallback to the general approach.
1037 OperandMatchResultTy Result =
1038 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1039 if (Result == MatchOperand_Success)
1040 return false;
1041 if (Result == MatchOperand_ParseFail)
1042 return true;
1043
1044 // Attempt to parse token as a register.
1045 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001046 return false;
1047
1048 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001049 if (parseImmediate(Operands) == MatchOperand_Success) {
1050 // Parse memory base register if present
1051 if (getLexer().is(AsmToken::LParen))
1052 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001053 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001054 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001055
1056 // Finally we have exhausted all options and must declare defeat.
1057 Error(getLoc(), "unknown operand");
1058 return true;
1059}
1060
1061bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1062 StringRef Name, SMLoc NameLoc,
1063 OperandVector &Operands) {
1064 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001065 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001066
1067 // If there are no more operands, then finish
1068 if (getLexer().is(AsmToken::EndOfStatement))
1069 return false;
1070
1071 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001072 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001073 return true;
1074
1075 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001076 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001077 while (getLexer().is(AsmToken::Comma)) {
1078 // Consume comma token
1079 getLexer().Lex();
1080
1081 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001082 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001083 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001084
1085 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001086 }
1087
1088 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1089 SMLoc Loc = getLexer().getLoc();
1090 getParser().eatToEndOfStatement();
1091 return Error(Loc, "unexpected token");
1092 }
1093
1094 getParser().Lex(); // Consume the EndOfStatement.
1095 return false;
1096}
1097
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001098bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1099 RISCVMCExpr::VariantKind &Kind,
1100 int64_t &Addend) {
1101 Kind = RISCVMCExpr::VK_RISCV_None;
1102 Addend = 0;
1103
1104 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1105 Kind = RE->getKind();
1106 Expr = RE->getSubExpr();
1107 }
1108
1109 // It's a simple symbol reference or constant with no addend.
1110 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1111 return true;
1112
1113 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1114 if (!BE)
1115 return false;
1116
1117 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1118 return false;
1119
1120 if (BE->getOpcode() != MCBinaryExpr::Add &&
1121 BE->getOpcode() != MCBinaryExpr::Sub)
1122 return false;
1123
1124 // We are able to support the subtraction of two symbol references
1125 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1126 isa<MCSymbolRefExpr>(BE->getRHS()))
1127 return true;
1128
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001129 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001130 // on here than we can deal with.
1131 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1132 if (!AddendExpr)
1133 return false;
1134
1135 Addend = AddendExpr->getValue();
1136 if (BE->getOpcode() == MCBinaryExpr::Sub)
1137 Addend = -Addend;
1138
1139 // It's some symbol reference + a constant addend
1140 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1141}
1142
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001143bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1144 // This returns false if this function recognizes the directive
1145 // regardless of whether it is successfully handles or reports an
1146 // error. Otherwise it returns true to give the generic parser a
1147 // chance at recognizing it.
1148 StringRef IDVal = DirectiveID.getString();
1149
1150 if (IDVal == ".option")
1151 return parseDirectiveOption();
1152
1153 return true;
1154}
1155
1156bool RISCVAsmParser::parseDirectiveOption() {
1157 MCAsmParser &Parser = getParser();
1158 // Get the option token.
1159 AsmToken Tok = Parser.getTok();
1160 // At the moment only identifiers are supported.
1161 if (Tok.isNot(AsmToken::Identifier))
1162 return Error(Parser.getTok().getLoc(),
1163 "unexpected token, expected identifier");
1164
1165 StringRef Option = Tok.getIdentifier();
1166
1167 if (Option == "rvc") {
1168 getTargetStreamer().emitDirectiveOptionRVC();
1169
1170 Parser.Lex();
1171 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1172 return Error(Parser.getTok().getLoc(),
1173 "unexpected token, expected end of statement");
1174
1175 setFeatureBits(RISCV::FeatureStdExtC, "c");
1176 return false;
1177 }
1178
1179 if (Option == "norvc") {
1180 getTargetStreamer().emitDirectiveOptionNoRVC();
1181
1182 Parser.Lex();
1183 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1184 return Error(Parser.getTok().getLoc(),
1185 "unexpected token, expected end of statement");
1186
1187 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1188 return false;
1189 }
1190
1191 // Unknown option.
1192 Warning(Parser.getTok().getLoc(),
1193 "unknown option, expected 'rvc' or 'norvc'");
1194 Parser.eatToEndOfStatement();
1195 return false;
1196}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001197
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001198void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1199 MCInst CInst;
1200 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1201 CInst.setLoc(Inst.getLoc());
1202 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1203}
1204
1205void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1206 MCStreamer &Out) {
1207 if (isInt<32>(Value)) {
1208 // Emits the MC instructions for loading a 32-bit constant into a register.
1209 //
1210 // Depending on the active bits in the immediate Value v, the following
1211 // instruction sequences are emitted:
1212 //
1213 // v == 0 : ADDI(W)
1214 // v[0,12) != 0 && v[12,32) == 0 : ADDI(W)
1215 // v[0,12) == 0 && v[12,32) != 0 : LUI
1216 // v[0,32) != 0 : LUI+ADDI(W)
1217 //
1218 int64_t Hi20 = ((Value + 0x800) >> 12) & 0xFFFFF;
1219 int64_t Lo12 = SignExtend64<12>(Value);
1220 unsigned SrcReg = RISCV::X0;
1221
1222 if (Hi20) {
1223 emitToStreamer(Out,
1224 MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Hi20));
1225 SrcReg = DestReg;
1226 }
1227
1228 if (Lo12 || Hi20 == 0) {
1229 unsigned AddiOpcode =
1230 STI->hasFeature(RISCV::Feature64Bit) ? RISCV::ADDIW : RISCV::ADDI;
1231 emitToStreamer(Out, MCInstBuilder(AddiOpcode)
1232 .addReg(DestReg)
1233 .addReg(SrcReg)
1234 .addImm(Lo12));
1235 }
1236 return;
1237 }
1238 assert(STI->hasFeature(RISCV::Feature64Bit) &&
1239 "Target must be 64-bit to support a >32-bit constant");
1240
1241 // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
1242 // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emmitted. Note
1243 // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits
1244 // while the following ADDI instructions contribute up to 12 bits each.
1245 //
1246 // On the first glance, implementing this seems to be possible by simply
1247 // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left
1248 // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the
1249 // fact that ADDI performs a sign extended addition, doing it like that would
1250 // only be possible when at most 11 bits of the ADDI instructions are used.
1251 // Using all 12 bits of the ADDI instructions, like done by GAS, actually
1252 // requires that the constant is processed starting with the least significant
1253 // bit.
1254 //
1255 // In the following, constants are processed from LSB to MSB but instruction
1256 // emission is performed from MSB to LSB by recursively calling
1257 // emitLoadImm. In each recursion, first the lowest 12 bits are removed
1258 // from the constant and the optimal shift amount, which can be greater than
1259 // 12 bits if the constant is sparse, is determined. Then, the shifted
1260 // remaining constant is processed recursively and gets emitted as soon as it
1261 // fits into 32 bits. The emission of the shifts and additions is subsequently
1262 // performed when the recursion returns.
1263 //
1264 int64_t Lo12 = SignExtend64<12>(Value);
1265 int64_t Hi52 = (Value + 0x800) >> 12;
1266 int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52);
1267 Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount);
1268
1269 emitLoadImm(DestReg, Hi52, Out);
1270
1271 emitToStreamer(Out, MCInstBuilder(RISCV::SLLI)
1272 .addReg(DestReg)
1273 .addReg(DestReg)
1274 .addImm(ShiftAmount));
1275
1276 if (Lo12)
1277 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1278 .addReg(DestReg)
1279 .addReg(DestReg)
1280 .addImm(Lo12));
1281}
1282
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001283void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1284 MCStreamer &Out) {
1285 // The local load address pseudo-instruction "lla" is used in PC-relative
1286 // addressing of symbols:
1287 // lla rdest, symbol
1288 // expands to
1289 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1290 // ADDI rdest, %pcrel_lo(TmpLabel)
1291 MCContext &Ctx = getContext();
1292
1293 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1294 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1295 Out.EmitLabel(TmpLabel);
1296
1297 MCOperand DestReg = Inst.getOperand(0);
1298 const RISCVMCExpr *Symbol = RISCVMCExpr::create(
1299 Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx);
1300
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001301 emitToStreamer(
1302 Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001303
1304 const MCExpr *RefToLinkTmpLabel =
1305 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1306 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1307
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001308 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1309 .addOperand(DestReg)
1310 .addOperand(DestReg)
1311 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001312}
1313
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001314bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1315 MCStreamer &Out) {
1316 Inst.setLoc(IDLoc);
1317
1318 if (Inst.getOpcode() == RISCV::PseudoLI) {
1319 auto Reg = Inst.getOperand(0).getReg();
1320 int64_t Imm = Inst.getOperand(1).getImm();
1321 // On RV32 the immediate here can either be a signed or an unsigned
1322 // 32-bit number. Sign extension has to be performed to ensure that Imm
1323 // represents the expected signed 64-bit number.
1324 if (!isRV64())
1325 Imm = SignExtend64<32>(Imm);
1326 emitLoadImm(Reg, Imm, Out);
1327 return false;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001328 } else if (Inst.getOpcode() == RISCV::PseudoLLA) {
1329 emitLoadLocalAddress(Inst, IDLoc, Out);
1330 return false;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001331 }
1332
1333 emitToStreamer(Out, Inst);
1334 return false;
1335}
1336
Alex Bradbury04f06d92017-08-08 14:43:36 +00001337extern "C" void LLVMInitializeRISCVAsmParser() {
1338 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1339 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1340}