blob: bac43187ca770be866eca6ef9e66843f8454fe0e [file] [log] [blame]
Alex Bradbury04f06d92017-08-08 14:43:36 +00001//===-- RISCVAsmParser.cpp - Parse RISCV assembly to MCInst instructions --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Bradbury04f06d92017-08-08 14:43:36 +00006//
7//===----------------------------------------------------------------------===//
8
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00009#include "MCTargetDesc/RISCVAsmBackend.h"
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"
Richard Trieu51fc56d2019-05-15 00:24:15 +000013#include "TargetInfo/RISCVTargetInfo.h"
Ana Pazos9d6c5532018-10-04 21:50:54 +000014#include "Utils/RISCVBaseInfo.h"
Alex Bradbury22c091f2018-11-15 10:11:31 +000015#include "Utils/RISCVMatInt.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000016#include "llvm/ADT/STLExtras.h"
Alex Bradbury893e5bc2018-11-28 16:39:14 +000017#include "llvm/ADT/SmallVector.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000018#include "llvm/ADT/StringSwitch.h"
Alex Bradbury9c03e4c2018-11-12 14:25:07 +000019#include "llvm/MC/MCAssembler.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000023#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury22531c42019-02-15 09:53:32 +000024#include "llvm/MC/MCObjectFileInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000025#include "llvm/MC/MCParser/MCAsmLexer.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000028#include "llvm/MC/MCRegisterInfo.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000031#include "llvm/Support/Casting.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000032#include "llvm/Support/MathExtras.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000033#include "llvm/Support/TargetRegistry.h"
34
Alex Bradbury6a4b5442018-06-07 15:35:47 +000035#include <limits>
36
Alex Bradbury04f06d92017-08-08 14:43:36 +000037using namespace llvm;
38
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000039// Include the auto-generated portion of the compress emitter.
40#define GEN_COMPRESS_INSTR
41#include "RISCVGenCompressInstEmitter.inc"
42
Alex Bradbury04f06d92017-08-08 14:43:36 +000043namespace {
44struct RISCVOperand;
45
46class RISCVAsmParser : public MCTargetAsmParser {
Alex Bradbury893e5bc2018-11-28 16:39:14 +000047 SmallVector<FeatureBitset, 4> FeatureBitStack;
48
Alex Bradbury04f06d92017-08-08 14:43:36 +000049 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000050 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradburydab1f6f2019-03-22 11:21:40 +000051 bool isRV32E() const { return getSTI().hasFeature(RISCV::FeatureRV32E); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000052
Alex Bradburybca0c3c2018-05-11 17:30:28 +000053 RISCVTargetStreamer &getTargetStreamer() {
54 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
55 return static_cast<RISCVTargetStreamer &>(TS);
56 }
57
Alex Bradbury7bc2a952017-12-07 10:46:23 +000058 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
59 unsigned Kind) override;
60
Alex Bradbury6758ecb2017-09-17 14:27:35 +000061 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury6a4b5442018-06-07 15:35:47 +000062 int64_t Lower, int64_t Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000063
Alex Bradbury04f06d92017-08-08 14:43:36 +000064 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
65 OperandVector &Operands, MCStreamer &Out,
66 uint64_t &ErrorInfo,
67 bool MatchingInlineAsm) override;
68
69 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
70
71 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
72 SMLoc NameLoc, OperandVector &Operands) override;
73
74 bool ParseDirective(AsmToken DirectiveID) override;
75
Alex Bradbury6a4b5442018-06-07 15:35:47 +000076 // Helper to actually emit an instruction to the MCStreamer. Also, when
77 // possible, compression of the instruction is performed.
78 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
79
80 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
81 // synthesize the desired immedate value into the destination register.
82 void emitLoadImm(unsigned DestReg, int64_t Value, MCStreamer &Out);
83
Alex Bradbury22531c42019-02-15 09:53:32 +000084 // Helper to emit a combination of AUIPC and SecondOpcode. Used to implement
85 // helpers such as emitLoadLocalAddress and emitLoadAddress.
86 void emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
87 const MCExpr *Symbol, RISCVMCExpr::VariantKind VKHi,
88 unsigned SecondOpcode, SMLoc IDLoc, MCStreamer &Out);
89
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +000090 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
91 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
92
Alex Bradbury22531c42019-02-15 09:53:32 +000093 // Helper to emit pseudo instruction "la" used in GOT/PC-rel addressing.
94 void emitLoadAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
95
Lewis Revill74927552019-05-23 14:46:27 +000096 // Helper to emit pseudo instruction "la.tls.ie" used in initial-exec TLS
97 // addressing.
98 void emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
99
100 // Helper to emit pseudo instruction "la.tls.gd" used in global-dynamic TLS
101 // addressing.
102 void emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
103
Kito Cheng303217e2019-02-20 03:31:32 +0000104 // Helper to emit pseudo load/store instruction with a symbol.
105 void emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
106 MCStreamer &Out, bool HasTmpReg);
107
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000108 // Checks that a PseudoAddTPRel is using x4/tp in its second input operand.
109 // Enforcing this using a restricted register class for the second input
110 // operand of PseudoAddTPRel results in a poor diagnostic due to the fact
111 // 'add' is an overloaded mnemonic.
112 bool checkPseudoAddTPRel(MCInst &Inst, OperandVector &Operands);
113
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000114 /// Helper for processing MC instructions that have been successfully matched
115 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
116 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
117 /// in this method.
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000118 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
119 MCStreamer &Out);
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000120
Alex Bradbury04f06d92017-08-08 14:43:36 +0000121// Auto-generated instruction matching functions
122#define GET_ASSEMBLER_HEADER
123#include "RISCVGenAsmMatcher.inc"
124
Ana Pazos9d6c5532018-10-04 21:50:54 +0000125 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000126 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000127 OperandMatchResultTy parseRegister(OperandVector &Operands,
128 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000129 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000130 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +0000131 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000132 OperandMatchResultTy parseCallSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000133 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000134
Alex Bradbury68f73c12018-09-18 15:18:16 +0000135 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000136
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000137 bool parseDirectiveOption();
138
139 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
140 if (!(getSTI().getFeatureBits()[Feature])) {
141 MCSubtargetInfo &STI = copySTI();
142 setAvailableFeatures(
143 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
144 }
145 }
146
147 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
148 if (getSTI().getFeatureBits()[Feature]) {
149 MCSubtargetInfo &STI = copySTI();
150 setAvailableFeatures(
151 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
152 }
153 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000154
Alex Bradbury893e5bc2018-11-28 16:39:14 +0000155 void pushFeatureBits() {
156 FeatureBitStack.push_back(getSTI().getFeatureBits());
157 }
158
159 bool popFeatureBits() {
160 if (FeatureBitStack.empty())
161 return true;
162
163 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
164 copySTI().setFeatureBits(FeatureBits);
165 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
166
167 return false;
168 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000169public:
170 enum RISCVMatchResultTy {
171 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
172#define GET_OPERAND_DIAGNOSTIC_TYPES
173#include "RISCVGenAsmMatcher.inc"
174#undef GET_OPERAND_DIAGNOSTIC_TYPES
175 };
176
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000177 static bool classifySymbolRef(const MCExpr *Expr,
178 RISCVMCExpr::VariantKind &Kind,
179 int64_t &Addend);
180
Alex Bradbury04f06d92017-08-08 14:43:36 +0000181 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
182 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000183 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000184 Parser.addAliasForDirective(".half", ".2byte");
185 Parser.addAliasForDirective(".hword", ".2byte");
186 Parser.addAliasForDirective(".word", ".4byte");
187 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000188 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
189 }
190};
191
192/// RISCVOperand - Instances of this class represent a parsed machine
193/// instruction
194struct RISCVOperand : public MCParsedAsmOperand {
195
196 enum KindTy {
197 Token,
198 Register,
199 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000200 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000201 } Kind;
202
Alex Bradburya6e62482017-12-07 10:53:48 +0000203 bool IsRV64;
204
Alex Bradbury04f06d92017-08-08 14:43:36 +0000205 struct RegOp {
206 unsigned RegNum;
207 };
208
209 struct ImmOp {
210 const MCExpr *Val;
211 };
212
Ana Pazos9d6c5532018-10-04 21:50:54 +0000213 struct SysRegOp {
214 const char *Data;
215 unsigned Length;
216 unsigned Encoding;
217 // FIXME: Add the Encoding parsed fields as needed for checks,
218 // e.g.: read/write or user/supervisor/machine privileges.
219 };
220
Alex Bradbury04f06d92017-08-08 14:43:36 +0000221 SMLoc StartLoc, EndLoc;
222 union {
223 StringRef Tok;
224 RegOp Reg;
225 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000226 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000227 };
228
229 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
230
231public:
232 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
233 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000234 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000235 StartLoc = o.StartLoc;
236 EndLoc = o.EndLoc;
237 switch (Kind) {
238 case Register:
239 Reg = o.Reg;
240 break;
241 case Immediate:
242 Imm = o.Imm;
243 break;
244 case Token:
245 Tok = o.Tok;
246 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000247 case SystemRegister:
248 SysReg = o.SysReg;
249 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000250 }
251 }
252
253 bool isToken() const override { return Kind == Token; }
254 bool isReg() const override { return Kind == Register; }
255 bool isImm() const override { return Kind == Immediate; }
256 bool isMem() const override { return false; }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000257 bool isSystemRegister() const { return Kind == SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000258
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000259 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
260 RISCVMCExpr::VariantKind &VK) {
261 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000262 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000263 return RE->evaluateAsConstant(Imm);
264 }
265
266 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000267 VK = RISCVMCExpr::VK_RISCV_None;
268 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000269 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000270 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000271
272 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000273 }
274
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000275 // True if operand is a symbol with no modifiers, or a constant with no
276 // modifiers and isShiftedInt<N-1, 1>(Op).
277 template <int N> bool isBareSimmNLsb0() const {
278 int64_t Imm;
279 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000280 if (!isImm())
281 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000282 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000283 bool IsValid;
284 if (!IsConstantImm)
285 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
286 else
287 IsValid = isShiftedInt<N - 1, 1>(Imm);
288 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000289 }
290
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000291 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
292
Shiva Chen98f93892018-04-25 14:18:55 +0000293 bool isBareSymbol() const {
294 int64_t Imm;
295 RISCVMCExpr::VariantKind VK;
296 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000297 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000298 return false;
299 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
300 VK == RISCVMCExpr::VK_RISCV_None;
301 }
302
Alex Bradbury44668ae2019-04-01 14:53:17 +0000303 bool isCallSymbol() const {
304 int64_t Imm;
305 RISCVMCExpr::VariantKind VK;
306 // Must be of 'immediate' type but not a constant.
307 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
308 return false;
309 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
Alex Bradburyf8078f62019-04-02 12:47:20 +0000310 (VK == RISCVMCExpr::VK_RISCV_CALL ||
311 VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000312 }
313
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000314 bool isTPRelAddSymbol() const {
315 int64_t Imm;
316 RISCVMCExpr::VariantKind VK;
317 // Must be of 'immediate' type but not a constant.
318 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
319 return false;
320 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
321 VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
322 }
323
Ana Pazos9d6c5532018-10-04 21:50:54 +0000324 bool isCSRSystemRegister() const { return isSystemRegister(); }
325
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000326 /// Return true if the operand is a valid for the fence instruction e.g.
327 /// ('iorw').
328 bool isFenceArg() const {
329 if (!isImm())
330 return false;
331 const MCExpr *Val = getImm();
332 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
333 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
334 return false;
335
336 StringRef Str = SVal->getSymbol().getName();
337 // Letters must be unique, taken from 'iorw', and in ascending order. This
338 // holds as long as each individual character is one of 'iorw' and is
339 // greater than the previous character.
340 char Prev = '\0';
341 for (char c : Str) {
342 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
343 return false;
344 if (c <= Prev)
345 return false;
346 Prev = c;
347 }
348 return true;
349 }
350
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000351 /// Return true if the operand is a valid floating point rounding mode.
352 bool isFRMArg() const {
353 if (!isImm())
354 return false;
355 const MCExpr *Val = getImm();
356 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
357 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
358 return false;
359
360 StringRef Str = SVal->getSymbol().getName();
361
362 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
363 }
364
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000365 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000366 int64_t Imm;
367 RISCVMCExpr::VariantKind VK;
368 if (!isImm())
369 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000370 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000371 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
372 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000373 // Given only Imm, ensuring that the actually specified constant is either
374 // a signed or unsigned 64-bit number is unfortunately impossible.
375 bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
376 return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None;
377 }
378
Alex Bradburya6e62482017-12-07 10:53:48 +0000379 bool isUImmLog2XLen() const {
380 int64_t Imm;
381 RISCVMCExpr::VariantKind VK;
382 if (!isImm())
383 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000384 if (!evaluateConstantImm(getImm(), Imm, VK) ||
385 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000386 return false;
387 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
388 }
389
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000390 bool isUImmLog2XLenNonZero() const {
391 int64_t Imm;
392 RISCVMCExpr::VariantKind VK;
393 if (!isImm())
394 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000395 if (!evaluateConstantImm(getImm(), Imm, VK) ||
396 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000397 return false;
398 if (Imm == 0)
399 return false;
400 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
401 }
402
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000403 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000404 int64_t Imm;
405 RISCVMCExpr::VariantKind VK;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000406 if (!isImm())
407 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000408 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000409 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000410 }
411
Alex Bradbury60714f92017-12-13 09:32:55 +0000412 bool isUImm5NonZero() const {
413 int64_t Imm;
414 RISCVMCExpr::VariantKind VK;
415 if (!isImm())
416 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000417 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000418 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
419 VK == RISCVMCExpr::VK_RISCV_None;
420 }
421
Alex Bradbury581d6b02017-12-13 09:41:21 +0000422 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000423 if (!isImm())
424 return false;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000425 RISCVMCExpr::VariantKind VK;
426 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000427 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000428 return IsConstantImm && isInt<6>(Imm) &&
429 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000430 }
431
Shiva Chenb22c1d22018-02-02 02:43:23 +0000432 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000433 if (!isImm())
434 return false;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000435 RISCVMCExpr::VariantKind VK;
436 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000437 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000438 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
439 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000440 }
441
Shiva Chen7c172422018-02-22 15:02:28 +0000442 bool isCLUIImm() 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 Chen7c172422018-02-22 15:02:28 +0000448 return IsConstantImm && (Imm != 0) &&
449 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000450 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000451 }
452
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000453 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000454 if (!isImm())
455 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000456 int64_t Imm;
457 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000458 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000459 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
460 VK == RISCVMCExpr::VK_RISCV_None;
461 }
462
463 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000464 if (!isImm())
465 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000466 int64_t Imm;
467 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000468 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000469 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
470 VK == RISCVMCExpr::VK_RISCV_None;
471 }
472
473 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000474 if (!isImm())
475 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000476 int64_t Imm;
477 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000478 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000479 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
480 VK == RISCVMCExpr::VK_RISCV_None;
481 }
482
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000483 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
484
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000485 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000486 if (!isImm())
487 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000488 int64_t Imm;
489 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000490 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000491 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
492 VK == RISCVMCExpr::VK_RISCV_None;
493 }
494
Alex Bradbury60714f92017-12-13 09:32:55 +0000495 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000496 if (!isImm())
497 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000498 int64_t Imm;
499 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000500 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000501 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
502 VK == RISCVMCExpr::VK_RISCV_None;
503 }
504
Alex Bradbury04f06d92017-08-08 14:43:36 +0000505 bool isSImm12() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000506 RISCVMCExpr::VariantKind VK;
507 int64_t Imm;
508 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000509 if (!isImm())
510 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000511 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000512 if (!IsConstantImm)
513 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
514 else
515 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000516 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000517 VK == RISCVMCExpr::VK_RISCV_LO ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000518 VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
519 VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000520 }
521
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000522 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
523
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000524 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000525
Shiva Chenb22c1d22018-02-02 02:43:23 +0000526 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000527 if (!isImm())
528 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000529 int64_t Imm;
530 RISCVMCExpr::VariantKind VK;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000531 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000532 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000533 VK == RISCVMCExpr::VK_RISCV_None;
534 }
535
Alex Bradbury74340f12018-09-18 15:08:35 +0000536 bool isUImm20LUI() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000537 RISCVMCExpr::VariantKind VK;
538 int64_t Imm;
539 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000540 if (!isImm())
541 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000542 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000543 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000544 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000545 return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
546 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000547 } else {
548 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000549 VK == RISCVMCExpr::VK_RISCV_HI ||
550 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000551 }
552 }
553
554 bool isUImm20AUIPC() const {
555 RISCVMCExpr::VariantKind VK;
556 int64_t Imm;
557 bool IsValid;
558 if (!isImm())
559 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000560 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000561 if (!IsConstantImm) {
562 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000563 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000564 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
565 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
566 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000567 } else {
568 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000569 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000570 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
571 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
572 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000573 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000574 }
575
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000576 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000577
Alex Bradbury04f06d92017-08-08 14:43:36 +0000578 /// getStartLoc - Gets location of the first token of this operand
579 SMLoc getStartLoc() const override { return StartLoc; }
580 /// getEndLoc - Gets location of the last token of this operand
581 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000582 /// True if this operand is for an RV64 instruction
583 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000584
585 unsigned getReg() const override {
586 assert(Kind == Register && "Invalid type access!");
587 return Reg.RegNum;
588 }
589
Ana Pazos9d6c5532018-10-04 21:50:54 +0000590 StringRef getSysReg() const {
591 assert(Kind == SystemRegister && "Invalid access!");
592 return StringRef(SysReg.Data, SysReg.Length);
593 }
594
Alex Bradbury04f06d92017-08-08 14:43:36 +0000595 const MCExpr *getImm() const {
596 assert(Kind == Immediate && "Invalid type access!");
597 return Imm.Val;
598 }
599
600 StringRef getToken() const {
601 assert(Kind == Token && "Invalid type access!");
602 return Tok;
603 }
604
605 void print(raw_ostream &OS) const override {
606 switch (Kind) {
607 case Immediate:
608 OS << *getImm();
609 break;
610 case Register:
611 OS << "<register x";
612 OS << getReg() << ">";
613 break;
614 case Token:
615 OS << "'" << getToken() << "'";
616 break;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000617 case SystemRegister:
618 OS << "<sysreg: " << getSysReg() << '>';
619 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000620 }
621 }
622
Alex Bradburya6e62482017-12-07 10:53:48 +0000623 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
624 bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000625 auto Op = make_unique<RISCVOperand>(Token);
626 Op->Tok = Str;
627 Op->StartLoc = S;
628 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000629 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000630 return Op;
631 }
632
633 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000634 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000635 auto Op = make_unique<RISCVOperand>(Register);
636 Op->Reg.RegNum = RegNo;
637 Op->StartLoc = S;
638 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000639 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000640 return Op;
641 }
642
643 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000644 SMLoc E, bool IsRV64) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000645 auto Op = make_unique<RISCVOperand>(Immediate);
646 Op->Imm.Val = Val;
647 Op->StartLoc = S;
648 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000649 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000650 return Op;
651 }
652
Ana Pazos9d6c5532018-10-04 21:50:54 +0000653 static std::unique_ptr<RISCVOperand>
654 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
655 auto Op = make_unique<RISCVOperand>(SystemRegister);
656 Op->SysReg.Data = Str.data();
657 Op->SysReg.Length = Str.size();
658 Op->SysReg.Encoding = Encoding;
659 Op->StartLoc = S;
660 Op->IsRV64 = IsRV64;
661 return Op;
662 }
663
Alex Bradbury04f06d92017-08-08 14:43:36 +0000664 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
665 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000666 int64_t Imm = 0;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000667 RISCVMCExpr::VariantKind VK;
668 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000669
670 if (IsConstant)
671 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000672 else
673 Inst.addOperand(MCOperand::createExpr(Expr));
674 }
675
676 // Used by the TableGen Code
677 void addRegOperands(MCInst &Inst, unsigned N) const {
678 assert(N == 1 && "Invalid number of operands!");
679 Inst.addOperand(MCOperand::createReg(getReg()));
680 }
681
682 void addImmOperands(MCInst &Inst, unsigned N) const {
683 assert(N == 1 && "Invalid number of operands!");
684 addExpr(Inst, getImm());
685 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000686
687 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
688 assert(N == 1 && "Invalid number of operands!");
689 // isFenceArg has validated the operand, meaning this cast is safe
690 auto SE = cast<MCSymbolRefExpr>(getImm());
691
692 unsigned Imm = 0;
693 for (char c : SE->getSymbol().getName()) {
694 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000695 default:
696 llvm_unreachable("FenceArg must contain only [iorw]");
697 case 'i': Imm |= RISCVFenceField::I; break;
698 case 'o': Imm |= RISCVFenceField::O; break;
699 case 'r': Imm |= RISCVFenceField::R; break;
700 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000701 }
702 }
703 Inst.addOperand(MCOperand::createImm(Imm));
704 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000705
Ana Pazos9d6c5532018-10-04 21:50:54 +0000706 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
707 assert(N == 1 && "Invalid number of operands!");
708 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
709 }
710
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000711 // Returns the rounding mode represented by this RISCVOperand. Should only
712 // be called after checking isFRMArg.
713 RISCVFPRndMode::RoundingMode getRoundingMode() const {
714 // isFRMArg has validated the operand, meaning this cast is safe.
715 auto SE = cast<MCSymbolRefExpr>(getImm());
716 RISCVFPRndMode::RoundingMode FRM =
717 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
718 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
719 return FRM;
720 }
721
722 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
723 assert(N == 1 && "Invalid number of operands!");
724 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
725 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000726};
727} // end anonymous namespace.
728
729#define GET_REGISTER_MATCHER
730#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000731#include "RISCVGenAsmMatcher.inc"
732
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000733// Return the matching FPR64 register for the given FPR32.
734// FIXME: Ideally this function could be removed in favour of using
735// information from TableGen.
736unsigned convertFPR32ToFPR64(unsigned Reg) {
737 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000738 default:
739 llvm_unreachable("Not a recognised FPR32 register");
740 case RISCV::F0_32: return RISCV::F0_64;
741 case RISCV::F1_32: return RISCV::F1_64;
742 case RISCV::F2_32: return RISCV::F2_64;
743 case RISCV::F3_32: return RISCV::F3_64;
744 case RISCV::F4_32: return RISCV::F4_64;
745 case RISCV::F5_32: return RISCV::F5_64;
746 case RISCV::F6_32: return RISCV::F6_64;
747 case RISCV::F7_32: return RISCV::F7_64;
748 case RISCV::F8_32: return RISCV::F8_64;
749 case RISCV::F9_32: return RISCV::F9_64;
750 case RISCV::F10_32: return RISCV::F10_64;
751 case RISCV::F11_32: return RISCV::F11_64;
752 case RISCV::F12_32: return RISCV::F12_64;
753 case RISCV::F13_32: return RISCV::F13_64;
754 case RISCV::F14_32: return RISCV::F14_64;
755 case RISCV::F15_32: return RISCV::F15_64;
756 case RISCV::F16_32: return RISCV::F16_64;
757 case RISCV::F17_32: return RISCV::F17_64;
758 case RISCV::F18_32: return RISCV::F18_64;
759 case RISCV::F19_32: return RISCV::F19_64;
760 case RISCV::F20_32: return RISCV::F20_64;
761 case RISCV::F21_32: return RISCV::F21_64;
762 case RISCV::F22_32: return RISCV::F22_64;
763 case RISCV::F23_32: return RISCV::F23_64;
764 case RISCV::F24_32: return RISCV::F24_64;
765 case RISCV::F25_32: return RISCV::F25_64;
766 case RISCV::F26_32: return RISCV::F26_64;
767 case RISCV::F27_32: return RISCV::F27_64;
768 case RISCV::F28_32: return RISCV::F28_64;
769 case RISCV::F29_32: return RISCV::F29_64;
770 case RISCV::F30_32: return RISCV::F30_64;
771 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000772 }
773}
774
775unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
776 unsigned Kind) {
777 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
778 if (!Op.isReg())
779 return Match_InvalidOperand;
780
781 unsigned Reg = Op.getReg();
782 bool IsRegFPR32 =
783 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000784 bool IsRegFPR32C =
785 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000786
787 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000788 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
789 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
790 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000791 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
792 return Match_Success;
793 }
794 return Match_InvalidOperand;
795}
796
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000797bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000798 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000799 Twine Msg = "immediate must be an integer in the range") {
800 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
801 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
802}
803
Alex Bradbury04f06d92017-08-08 14:43:36 +0000804bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
805 OperandVector &Operands,
806 MCStreamer &Out,
807 uint64_t &ErrorInfo,
808 bool MatchingInlineAsm) {
809 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000810
Ana Pazos6b34051b2018-08-30 19:43:19 +0000811 auto Result =
812 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
813 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000814 default:
815 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000816 case Match_Success:
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000817 return processInstruction(Inst, IDLoc, Operands, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000818 case Match_MissingFeature:
819 return Error(IDLoc, "instruction use requires an option to be enabled");
820 case Match_MnemonicFail:
821 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000822 case Match_InvalidOperand: {
823 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000824 if (ErrorInfo != ~0U) {
825 if (ErrorInfo >= Operands.size())
826 return Error(ErrorLoc, "too few operands for instruction");
827
828 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
829 if (ErrorLoc == SMLoc())
830 ErrorLoc = IDLoc;
831 }
832 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000833 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000834 }
835
836 // Handle the case when the error message is of specific type
837 // other than the generic Match_InvalidOperand, and the
838 // corresponding operand is missing.
839 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
840 SMLoc ErrorLoc = IDLoc;
841 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
842 return Error(ErrorLoc, "too few operands for instruction");
843 }
844
845 switch(Result) {
846 default:
847 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000848 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000849 if (isRV64()) {
850 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
851 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
852 }
853 return generateImmOutOfRangeError(Operands, ErrorInfo,
854 std::numeric_limits<int32_t>::min(),
855 std::numeric_limits<uint32_t>::max());
Alex Bradburya6e62482017-12-07 10:53:48 +0000856 case Match_InvalidUImmLog2XLen:
857 if (isRV64())
858 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
859 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000860 case Match_InvalidUImmLog2XLenNonZero:
861 if (isRV64())
862 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
863 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000864 case Match_InvalidUImm5:
865 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000866 case Match_InvalidSImm6:
867 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
868 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000869 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000870 return generateImmOutOfRangeError(
871 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000872 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000873 case Match_InvalidCLUIImm:
874 return generateImmOutOfRangeError(
875 Operands, ErrorInfo, 1, (1 << 5) - 1,
876 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000877 case Match_InvalidUImm7Lsb00:
878 return generateImmOutOfRangeError(
879 Operands, ErrorInfo, 0, (1 << 7) - 4,
880 "immediate must be a multiple of 4 bytes in the range");
881 case Match_InvalidUImm8Lsb00:
882 return generateImmOutOfRangeError(
883 Operands, ErrorInfo, 0, (1 << 8) - 4,
884 "immediate must be a multiple of 4 bytes in the range");
885 case Match_InvalidUImm8Lsb000:
886 return generateImmOutOfRangeError(
887 Operands, ErrorInfo, 0, (1 << 8) - 8,
888 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000889 case Match_InvalidSImm9Lsb0:
890 return generateImmOutOfRangeError(
891 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
892 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000893 case Match_InvalidUImm9Lsb000:
894 return generateImmOutOfRangeError(
895 Operands, ErrorInfo, 0, (1 << 9) - 8,
896 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000897 case Match_InvalidUImm10Lsb00NonZero:
898 return generateImmOutOfRangeError(
899 Operands, ErrorInfo, 4, (1 << 10) - 4,
900 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000901 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000902 return generateImmOutOfRangeError(
903 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000904 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000905 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000906 return generateImmOutOfRangeError(
907 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000908 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
909 "integer in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000910 case Match_InvalidSImm12Lsb0:
911 return generateImmOutOfRangeError(
912 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
913 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000914 case Match_InvalidSImm13Lsb0:
915 return generateImmOutOfRangeError(
916 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
917 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000918 case Match_InvalidUImm20LUI:
919 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000920 "operand must be a symbol with "
921 "%hi/%tprel_hi modifier or an integer in "
922 "the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000923 case Match_InvalidUImm20AUIPC:
924 return generateImmOutOfRangeError(
925 Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revilldf3cb472019-04-23 14:46:13 +0000926 "operand must be a symbol with a "
927 "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi modifier or "
928 "an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000929 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000930 return generateImmOutOfRangeError(
931 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
932 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000933 case Match_InvalidCSRSystemRegister: {
934 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
935 "operand must be a valid system register "
936 "name or an integer in the range");
937 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000938 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000939 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000940 return Error(
941 ErrorLoc,
942 "operand must be formed of letters selected in-order from 'iorw'");
943 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000944 case Match_InvalidFRMArg: {
945 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
946 return Error(
947 ErrorLoc,
948 "operand must be a valid floating point rounding mode mnemonic");
949 }
Shiva Chen98f93892018-04-25 14:18:55 +0000950 case Match_InvalidBareSymbol: {
951 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
952 return Error(ErrorLoc, "operand must be a bare symbol name");
953 }
Alex Bradbury44668ae2019-04-01 14:53:17 +0000954 case Match_InvalidCallSymbol: {
955 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
956 return Error(ErrorLoc, "operand must be a bare symbol name");
957 }
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000958 case Match_InvalidTPRelAddSymbol: {
959 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
960 return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
961 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000962 }
963
964 llvm_unreachable("Unknown match type detected!");
965}
966
Alex Bradbury99794792019-03-17 12:02:32 +0000967// Attempts to match Name as a register (either using the default name or
968// alternative ABI names), setting RegNo to the matching register. Upon
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000969// failure, returns true and sets RegNo to 0. If IsRV32E then registers
970// x16-x31 will be rejected.
971static bool matchRegisterNameHelper(bool IsRV32E, unsigned &RegNo,
972 StringRef Name) {
Alex Bradbury99794792019-03-17 12:02:32 +0000973 RegNo = MatchRegisterName(Name);
974 if (RegNo == 0)
975 RegNo = MatchRegisterAltName(Name);
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000976 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
977 RegNo = 0;
Alex Bradbury99794792019-03-17 12:02:32 +0000978 return RegNo == 0;
979}
980
Alex Bradbury04f06d92017-08-08 14:43:36 +0000981bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
982 SMLoc &EndLoc) {
983 const AsmToken &Tok = getParser().getTok();
984 StartLoc = Tok.getLoc();
985 EndLoc = Tok.getEndLoc();
986 RegNo = 0;
987 StringRef Name = getLexer().getTok().getIdentifier();
988
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000989 if (matchRegisterNameHelper(isRV32E(), RegNo, Name))
Alex Bradburyb18e3142019-03-17 12:00:58 +0000990 return Error(StartLoc, "invalid register name");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000991
Alex Bradburyb18e3142019-03-17 12:00:58 +0000992 getParser().Lex(); // Eat identifier token.
993 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000994}
995
Alex Bradbury8c345c52017-11-09 15:00:03 +0000996OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
997 bool AllowParens) {
998 SMLoc FirstS = getLoc();
999 bool HadParens = false;
1000 AsmToken Buf[2];
1001
1002 // If this a parenthesised register name is allowed, parse it atomically
1003 if (AllowParens && getLexer().is(AsmToken::LParen)) {
1004 size_t ReadCount = getLexer().peekTokens(Buf);
1005 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
1006 HadParens = true;
1007 getParser().Lex(); // Eat '('
1008 }
1009 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001010
1011 switch (getLexer().getKind()) {
1012 default:
1013 return MatchOperand_NoMatch;
1014 case AsmToken::Identifier:
1015 StringRef Name = getLexer().getTok().getIdentifier();
Alex Bradbury99794792019-03-17 12:02:32 +00001016 unsigned RegNo;
Alex Bradburydab1f6f2019-03-22 11:21:40 +00001017 matchRegisterNameHelper(isRV32E(), RegNo, Name);
Alex Bradbury99794792019-03-17 12:02:32 +00001018
Alex Bradbury04f06d92017-08-08 14:43:36 +00001019 if (RegNo == 0) {
Alex Bradbury99794792019-03-17 12:02:32 +00001020 if (HadParens)
1021 getLexer().UnLex(Buf[0]);
1022 return MatchOperand_NoMatch;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001023 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001024 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +00001025 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001026 SMLoc S = getLoc();
1027 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001028 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +00001029 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001030 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001031
1032 if (HadParens) {
1033 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001034 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001035 }
1036
Alex Bradbury04f06d92017-08-08 14:43:36 +00001037 return MatchOperand_Success;
1038}
1039
Ana Pazos9d6c5532018-10-04 21:50:54 +00001040OperandMatchResultTy
1041RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1042 SMLoc S = getLoc();
1043 const MCExpr *Res;
1044
1045 switch (getLexer().getKind()) {
1046 default:
1047 return MatchOperand_NoMatch;
1048 case AsmToken::LParen:
1049 case AsmToken::Minus:
1050 case AsmToken::Plus:
1051 case AsmToken::Integer:
1052 case AsmToken::String: {
1053 if (getParser().parseExpression(Res))
1054 return MatchOperand_ParseFail;
1055
1056 auto *CE = dyn_cast<MCConstantExpr>(Res);
1057 if (CE) {
1058 int64_t Imm = CE->getValue();
1059 if (isUInt<12>(Imm)) {
1060 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1061 // Accept an immediate representing a named or un-named Sys Reg
1062 // if the range is valid, regardless of the required features.
1063 Operands.push_back(RISCVOperand::createSysReg(
1064 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1065 return MatchOperand_Success;
1066 }
1067 }
1068
1069 Twine Msg = "immediate must be an integer in the range";
1070 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1071 return MatchOperand_ParseFail;
1072 }
1073 case AsmToken::Identifier: {
1074 StringRef Identifier;
1075 if (getParser().parseIdentifier(Identifier))
1076 return MatchOperand_ParseFail;
1077
1078 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1079 // Accept a named Sys Reg if the required features are present.
1080 if (SysReg) {
1081 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1082 Error(S, "system register use requires an option to be enabled");
1083 return MatchOperand_ParseFail;
1084 }
1085 Operands.push_back(RISCVOperand::createSysReg(
1086 Identifier, S, SysReg->Encoding, isRV64()));
1087 return MatchOperand_Success;
1088 }
1089
1090 Twine Msg = "operand must be a valid system register name "
1091 "or an integer in the range";
1092 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1093 return MatchOperand_ParseFail;
1094 }
1095 case AsmToken::Percent: {
1096 // Discard operand with modifier.
1097 Twine Msg = "immediate must be an integer in the range";
1098 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1099 return MatchOperand_ParseFail;
1100 }
1101 }
1102
1103 return MatchOperand_NoMatch;
1104}
1105
Alex Bradbury04f06d92017-08-08 14:43:36 +00001106OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001107 SMLoc S = getLoc();
1108 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1109 const MCExpr *Res;
1110
Alex Bradbury04f06d92017-08-08 14:43:36 +00001111 switch (getLexer().getKind()) {
1112 default:
1113 return MatchOperand_NoMatch;
1114 case AsmToken::LParen:
1115 case AsmToken::Minus:
1116 case AsmToken::Plus:
1117 case AsmToken::Integer:
1118 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001119 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001120 if (getParser().parseExpression(Res))
1121 return MatchOperand_ParseFail;
1122 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001123 case AsmToken::Percent:
1124 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001125 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001126
Alex Bradburya6e62482017-12-07 10:53:48 +00001127 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001128 return MatchOperand_Success;
1129}
1130
1131OperandMatchResultTy
1132RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1133 SMLoc S = getLoc();
1134 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1135
1136 if (getLexer().getKind() != AsmToken::Percent) {
1137 Error(getLoc(), "expected '%' for operand modifier");
1138 return MatchOperand_ParseFail;
1139 }
1140
1141 getParser().Lex(); // Eat '%'
1142
1143 if (getLexer().getKind() != AsmToken::Identifier) {
1144 Error(getLoc(), "expected valid identifier for operand modifier");
1145 return MatchOperand_ParseFail;
1146 }
1147 StringRef Identifier = getParser().getTok().getIdentifier();
1148 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1149 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1150 Error(getLoc(), "unrecognized operand modifier");
1151 return MatchOperand_ParseFail;
1152 }
1153
1154 getParser().Lex(); // Eat the identifier
1155 if (getLexer().getKind() != AsmToken::LParen) {
1156 Error(getLoc(), "expected '('");
1157 return MatchOperand_ParseFail;
1158 }
1159 getParser().Lex(); // Eat '('
1160
1161 const MCExpr *SubExpr;
1162 if (getParser().parseParenExpression(SubExpr, E)) {
1163 return MatchOperand_ParseFail;
1164 }
1165
1166 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001167 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001168 return MatchOperand_Success;
1169}
1170
Alex Bradbury68f73c12018-09-18 15:18:16 +00001171OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1172 SMLoc S = getLoc();
1173 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1174 const MCExpr *Res;
1175
1176 if (getLexer().getKind() != AsmToken::Identifier)
1177 return MatchOperand_NoMatch;
1178
1179 StringRef Identifier;
Kito Cheng303217e2019-02-20 03:31:32 +00001180 AsmToken Tok = getLexer().getTok();
1181
Alex Bradbury68f73c12018-09-18 15:18:16 +00001182 if (getParser().parseIdentifier(Identifier))
1183 return MatchOperand_ParseFail;
1184
Alex Bradburyf8078f62019-04-02 12:47:20 +00001185 if (Identifier.consume_back("@plt")) {
1186 Error(getLoc(), "'@plt' operand not valid for instruction");
1187 return MatchOperand_ParseFail;
1188 }
1189
Alex Bradbury68f73c12018-09-18 15:18:16 +00001190 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Kito Cheng303217e2019-02-20 03:31:32 +00001191
1192 if (Sym->isVariable()) {
1193 const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1194 if (!isa<MCSymbolRefExpr>(V)) {
1195 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1196 return MatchOperand_NoMatch;
1197 }
1198 Res = V;
1199 } else
1200 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradbury68f73c12018-09-18 15:18:16 +00001201 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1202 return MatchOperand_Success;
1203}
1204
Alex Bradbury44668ae2019-04-01 14:53:17 +00001205OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1206 SMLoc S = getLoc();
1207 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1208 const MCExpr *Res;
1209
1210 if (getLexer().getKind() != AsmToken::Identifier)
1211 return MatchOperand_NoMatch;
1212
1213 StringRef Identifier;
1214 if (getParser().parseIdentifier(Identifier))
1215 return MatchOperand_ParseFail;
1216
Alex Bradburyf8078f62019-04-02 12:47:20 +00001217 RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1218 if (Identifier.consume_back("@plt"))
1219 Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1220
Alex Bradbury44668ae2019-04-01 14:53:17 +00001221 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1222 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradburyf8078f62019-04-02 12:47:20 +00001223 Res = RISCVMCExpr::create(Res, Kind, getContext());
Alex Bradbury44668ae2019-04-01 14:53:17 +00001224 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1225 return MatchOperand_Success;
1226}
1227
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001228OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1229 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1230 // both being acceptable forms. When parsing `jal ra, foo` this function
1231 // will be called for the `ra` register operand in an attempt to match the
1232 // single-operand alias. parseJALOffset must fail for this case. It would
1233 // seem logical to try parse the operand using parseImmediate and return
1234 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1235 // the second form rather than the first). We can't do this as there's no
1236 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1237 // is an identifier and is followed by a comma.
1238 if (getLexer().is(AsmToken::Identifier) &&
1239 getLexer().peekTok().is(AsmToken::Comma))
1240 return MatchOperand_NoMatch;
1241
1242 return parseImmediate(Operands);
1243}
1244
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001245OperandMatchResultTy
1246RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1247 if (getLexer().isNot(AsmToken::LParen)) {
1248 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001249 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001250 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001251
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001252 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001253 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001254
1255 if (parseRegister(Operands) != MatchOperand_Success) {
1256 Error(getLoc(), "expected register");
1257 return MatchOperand_ParseFail;
1258 }
1259
1260 if (getLexer().isNot(AsmToken::RParen)) {
1261 Error(getLoc(), "expected ')'");
1262 return MatchOperand_ParseFail;
1263 }
1264
1265 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001266 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001267
Alex Bradbury04f06d92017-08-08 14:43:36 +00001268 return MatchOperand_Success;
1269}
1270
Alex Bradburycd8688a2018-04-25 17:25:29 +00001271/// Looks at a token type and creates the relevant operand from this
1272/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001273/// true.
1274bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1275 // Check if the current operand has a custom associated parser, if so, try to
1276 // custom parse the operand, or fallback to the general approach.
1277 OperandMatchResultTy Result =
1278 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1279 if (Result == MatchOperand_Success)
1280 return false;
1281 if (Result == MatchOperand_ParseFail)
1282 return true;
1283
1284 // Attempt to parse token as a register.
1285 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001286 return false;
1287
1288 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001289 if (parseImmediate(Operands) == MatchOperand_Success) {
1290 // Parse memory base register if present
1291 if (getLexer().is(AsmToken::LParen))
1292 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001293 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001294 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001295
1296 // Finally we have exhausted all options and must declare defeat.
1297 Error(getLoc(), "unknown operand");
1298 return true;
1299}
1300
1301bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1302 StringRef Name, SMLoc NameLoc,
1303 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001304 // Ensure that if the instruction occurs when relaxation is enabled,
1305 // relocations are forced for the file. Ideally this would be done when there
1306 // is enough information to reliably determine if the instruction itself may
1307 // cause relaxations. Unfortunately instruction processing stage occurs in the
1308 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1309 // for the entire file.
1310 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1311 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1312 if (Assembler != nullptr) {
1313 RISCVAsmBackend &MAB =
1314 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1315 MAB.setForceRelocs();
1316 }
1317 }
1318
Alex Bradbury04f06d92017-08-08 14:43:36 +00001319 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001320 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001321
1322 // If there are no more operands, then finish
1323 if (getLexer().is(AsmToken::EndOfStatement))
1324 return false;
1325
1326 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001327 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001328 return true;
1329
1330 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001331 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001332 while (getLexer().is(AsmToken::Comma)) {
1333 // Consume comma token
1334 getLexer().Lex();
1335
1336 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001337 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001338 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001339
1340 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001341 }
1342
1343 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1344 SMLoc Loc = getLexer().getLoc();
1345 getParser().eatToEndOfStatement();
1346 return Error(Loc, "unexpected token");
1347 }
1348
1349 getParser().Lex(); // Consume the EndOfStatement.
1350 return false;
1351}
1352
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001353bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1354 RISCVMCExpr::VariantKind &Kind,
1355 int64_t &Addend) {
1356 Kind = RISCVMCExpr::VK_RISCV_None;
1357 Addend = 0;
1358
1359 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1360 Kind = RE->getKind();
1361 Expr = RE->getSubExpr();
1362 }
1363
1364 // It's a simple symbol reference or constant with no addend.
1365 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1366 return true;
1367
1368 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1369 if (!BE)
1370 return false;
1371
1372 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1373 return false;
1374
1375 if (BE->getOpcode() != MCBinaryExpr::Add &&
1376 BE->getOpcode() != MCBinaryExpr::Sub)
1377 return false;
1378
1379 // We are able to support the subtraction of two symbol references
1380 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1381 isa<MCSymbolRefExpr>(BE->getRHS()))
1382 return true;
1383
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001384 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001385 // on here than we can deal with.
1386 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1387 if (!AddendExpr)
1388 return false;
1389
1390 Addend = AddendExpr->getValue();
1391 if (BE->getOpcode() == MCBinaryExpr::Sub)
1392 Addend = -Addend;
1393
1394 // It's some symbol reference + a constant addend
1395 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1396}
1397
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001398bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1399 // This returns false if this function recognizes the directive
1400 // regardless of whether it is successfully handles or reports an
1401 // error. Otherwise it returns true to give the generic parser a
1402 // chance at recognizing it.
1403 StringRef IDVal = DirectiveID.getString();
1404
1405 if (IDVal == ".option")
1406 return parseDirectiveOption();
1407
1408 return true;
1409}
1410
1411bool RISCVAsmParser::parseDirectiveOption() {
1412 MCAsmParser &Parser = getParser();
1413 // Get the option token.
1414 AsmToken Tok = Parser.getTok();
1415 // At the moment only identifiers are supported.
1416 if (Tok.isNot(AsmToken::Identifier))
1417 return Error(Parser.getTok().getLoc(),
1418 "unexpected token, expected identifier");
1419
1420 StringRef Option = Tok.getIdentifier();
1421
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001422 if (Option == "push") {
1423 getTargetStreamer().emitDirectiveOptionPush();
1424
1425 Parser.Lex();
1426 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1427 return Error(Parser.getTok().getLoc(),
1428 "unexpected token, expected end of statement");
1429
1430 pushFeatureBits();
1431 return false;
1432 }
1433
1434 if (Option == "pop") {
1435 SMLoc StartLoc = Parser.getTok().getLoc();
1436 getTargetStreamer().emitDirectiveOptionPop();
1437
1438 Parser.Lex();
1439 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1440 return Error(Parser.getTok().getLoc(),
1441 "unexpected token, expected end of statement");
1442
1443 if (popFeatureBits())
1444 return Error(StartLoc, ".option pop with no .option push");
1445
1446 return false;
1447 }
1448
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001449 if (Option == "rvc") {
1450 getTargetStreamer().emitDirectiveOptionRVC();
1451
1452 Parser.Lex();
1453 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1454 return Error(Parser.getTok().getLoc(),
1455 "unexpected token, expected end of statement");
1456
1457 setFeatureBits(RISCV::FeatureStdExtC, "c");
1458 return false;
1459 }
1460
1461 if (Option == "norvc") {
1462 getTargetStreamer().emitDirectiveOptionNoRVC();
1463
1464 Parser.Lex();
1465 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1466 return Error(Parser.getTok().getLoc(),
1467 "unexpected token, expected end of statement");
1468
1469 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1470 return false;
1471 }
1472
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001473 if (Option == "relax") {
1474 getTargetStreamer().emitDirectiveOptionRelax();
1475
1476 Parser.Lex();
1477 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1478 return Error(Parser.getTok().getLoc(),
1479 "unexpected token, expected end of statement");
1480
1481 setFeatureBits(RISCV::FeatureRelax, "relax");
1482 return false;
1483 }
1484
1485 if (Option == "norelax") {
1486 getTargetStreamer().emitDirectiveOptionNoRelax();
1487
1488 Parser.Lex();
1489 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1490 return Error(Parser.getTok().getLoc(),
1491 "unexpected token, expected end of statement");
1492
1493 clearFeatureBits(RISCV::FeatureRelax, "relax");
1494 return false;
1495 }
1496
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001497 // Unknown option.
1498 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001499 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1500 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001501 Parser.eatToEndOfStatement();
1502 return false;
1503}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001504
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001505void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1506 MCInst CInst;
1507 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1508 CInst.setLoc(Inst.getLoc());
1509 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1510}
1511
1512void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value,
1513 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001514 RISCVMatInt::InstSeq Seq;
1515 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001516
Alex Bradbury22c091f2018-11-15 10:11:31 +00001517 unsigned SrcReg = RISCV::X0;
1518 for (RISCVMatInt::Inst &Inst : Seq) {
1519 if (Inst.Opc == RISCV::LUI) {
1520 emitToStreamer(
1521 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1522 } else {
1523 emitToStreamer(
1524 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1525 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001526 }
1527
Alex Bradbury22c091f2018-11-15 10:11:31 +00001528 // Only the first instruction has X0 as its source.
1529 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001530 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001531}
1532
Alex Bradbury22531c42019-02-15 09:53:32 +00001533void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
1534 const MCExpr *Symbol,
1535 RISCVMCExpr::VariantKind VKHi,
1536 unsigned SecondOpcode, SMLoc IDLoc,
1537 MCStreamer &Out) {
1538 // A pair of instructions for PC-relative addressing; expands to
1539 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
1540 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001541 MCContext &Ctx = getContext();
1542
1543 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1544 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1545 Out.EmitLabel(TmpLabel);
1546
Alex Bradbury22531c42019-02-15 09:53:32 +00001547 const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001548 emitToStreamer(
Alex Bradbury22531c42019-02-15 09:53:32 +00001549 Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001550
1551 const MCExpr *RefToLinkTmpLabel =
1552 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1553 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1554
Alex Bradbury22531c42019-02-15 09:53:32 +00001555 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001556 .addOperand(DestReg)
Alex Bradbury22531c42019-02-15 09:53:32 +00001557 .addOperand(TmpReg)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001558 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001559}
1560
Alex Bradbury22531c42019-02-15 09:53:32 +00001561void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1562 MCStreamer &Out) {
1563 // The load local address pseudo-instruction "lla" is used in PC-relative
1564 // addressing of local symbols:
1565 // lla rdest, symbol
1566 // expands to
1567 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1568 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1569 MCOperand DestReg = Inst.getOperand(0);
1570 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1571 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1572 RISCV::ADDI, IDLoc, Out);
1573}
1574
1575void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
1576 MCStreamer &Out) {
1577 // The load address pseudo-instruction "la" is used in PC-relative and
1578 // GOT-indirect addressing of global symbols:
1579 // la rdest, symbol
1580 // expands to either (for non-PIC)
1581 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1582 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1583 // or (for PIC)
1584 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
1585 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1586 MCOperand DestReg = Inst.getOperand(0);
1587 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1588 unsigned SecondOpcode;
1589 RISCVMCExpr::VariantKind VKHi;
1590 // FIXME: Should check .option (no)pic when implemented
1591 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1592 SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1593 VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
1594 } else {
1595 SecondOpcode = RISCV::ADDI;
1596 VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
1597 }
1598 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1599}
1600
Lewis Revill74927552019-05-23 14:46:27 +00001601void RISCVAsmParser::emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc,
1602 MCStreamer &Out) {
1603 // The load TLS IE address pseudo-instruction "la.tls.ie" is used in
1604 // initial-exec TLS model addressing of global symbols:
1605 // la.tls.ie rdest, symbol
1606 // expands to
1607 // TmpLabel: AUIPC rdest, %tls_ie_pcrel_hi(symbol)
1608 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1609 MCOperand DestReg = Inst.getOperand(0);
1610 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1611 unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1612 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GOT_HI,
1613 SecondOpcode, IDLoc, Out);
1614}
1615
1616void RISCVAsmParser::emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc,
1617 MCStreamer &Out) {
1618 // The load TLS GD address pseudo-instruction "la.tls.gd" is used in
1619 // global-dynamic TLS model addressing of global symbols:
1620 // la.tls.gd rdest, symbol
1621 // expands to
1622 // TmpLabel: AUIPC rdest, %tls_gd_pcrel_hi(symbol)
1623 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1624 MCOperand DestReg = Inst.getOperand(0);
1625 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1626 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GD_HI,
1627 RISCV::ADDI, IDLoc, Out);
1628}
1629
Kito Cheng303217e2019-02-20 03:31:32 +00001630void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
1631 SMLoc IDLoc, MCStreamer &Out,
1632 bool HasTmpReg) {
1633 // The load/store pseudo-instruction does a pc-relative load with
1634 // a symbol.
1635 //
1636 // The expansion looks like this
1637 //
1638 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
1639 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
1640 MCOperand DestReg = Inst.getOperand(0);
1641 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1642 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1643 MCOperand TmpReg = Inst.getOperand(TmpRegOpIdx);
1644 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
1645 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1646 Opcode, IDLoc, Out);
1647}
1648
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001649bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
1650 OperandVector &Operands) {
1651 assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
1652 assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
1653 if (Inst.getOperand(2).getReg() != RISCV::X4) {
1654 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
1655 return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
1656 "%tprel_add modifier");
1657 }
1658
1659 return false;
1660}
1661
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001662bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001663 OperandVector &Operands,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001664 MCStreamer &Out) {
1665 Inst.setLoc(IDLoc);
1666
Alex Bradbury22531c42019-02-15 09:53:32 +00001667 switch (Inst.getOpcode()) {
1668 default:
1669 break;
1670 case RISCV::PseudoLI: {
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001671 unsigned Reg = Inst.getOperand(0).getReg();
1672 const MCOperand &Op1 = Inst.getOperand(1);
1673 if (Op1.isExpr()) {
1674 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1675 // Just convert to an addi. This allows compatibility with gas.
1676 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1677 .addReg(Reg)
1678 .addReg(RISCV::X0)
1679 .addExpr(Op1.getExpr()));
1680 return false;
1681 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001682 int64_t Imm = Inst.getOperand(1).getImm();
1683 // On RV32 the immediate here can either be a signed or an unsigned
1684 // 32-bit number. Sign extension has to be performed to ensure that Imm
1685 // represents the expected signed 64-bit number.
1686 if (!isRV64())
1687 Imm = SignExtend64<32>(Imm);
1688 emitLoadImm(Reg, Imm, Out);
1689 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001690 }
1691 case RISCV::PseudoLLA:
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001692 emitLoadLocalAddress(Inst, IDLoc, Out);
1693 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001694 case RISCV::PseudoLA:
1695 emitLoadAddress(Inst, IDLoc, Out);
1696 return false;
Lewis Revill74927552019-05-23 14:46:27 +00001697 case RISCV::PseudoLA_TLS_IE:
1698 emitLoadTLSIEAddress(Inst, IDLoc, Out);
1699 return false;
1700 case RISCV::PseudoLA_TLS_GD:
1701 emitLoadTLSGDAddress(Inst, IDLoc, Out);
1702 return false;
Kito Cheng303217e2019-02-20 03:31:32 +00001703 case RISCV::PseudoLB:
1704 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
1705 return false;
1706 case RISCV::PseudoLBU:
1707 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
1708 return false;
1709 case RISCV::PseudoLH:
1710 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
1711 return false;
1712 case RISCV::PseudoLHU:
1713 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
1714 return false;
1715 case RISCV::PseudoLW:
1716 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
1717 return false;
1718 case RISCV::PseudoLWU:
1719 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
1720 return false;
1721 case RISCV::PseudoLD:
1722 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
1723 return false;
1724 case RISCV::PseudoFLW:
1725 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
1726 return false;
1727 case RISCV::PseudoFLD:
1728 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
1729 return false;
1730 case RISCV::PseudoSB:
1731 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
1732 return false;
1733 case RISCV::PseudoSH:
1734 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
1735 return false;
1736 case RISCV::PseudoSW:
1737 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
1738 return false;
1739 case RISCV::PseudoSD:
1740 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
1741 return false;
1742 case RISCV::PseudoFSW:
1743 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
1744 return false;
1745 case RISCV::PseudoFSD:
1746 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
1747 return false;
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001748 case RISCV::PseudoAddTPRel:
1749 if (checkPseudoAddTPRel(Inst, Operands))
1750 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001751 }
1752
1753 emitToStreamer(Out, Inst);
1754 return false;
1755}
1756
Tom Stellard4b0b2612019-06-11 03:21:13 +00001757extern "C" void LLVMInitializeRISCVAsmParser() {
Alex Bradbury04f06d92017-08-08 14:43:36 +00001758 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1759 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1760}