blob: 5f93db484b3ab9853a1b903e225ed43f72d771d0 [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"
Luis Marquesfa06e952019-08-16 14:27:50 +000019#include "llvm/CodeGen/Register.h"
Alex Bradbury9c03e4c2018-11-12 14:25:07 +000020#include "llvm/MC/MCAssembler.h"
Alex Bradbury4f7f0da2017-09-06 09:21:21 +000021#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000024#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury22531c42019-02-15 09:53:32 +000025#include "llvm/MC/MCObjectFileInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000026#include "llvm/MC/MCParser/MCAsmLexer.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
28#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000029#include "llvm/MC/MCRegisterInfo.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSubtargetInfo.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000032#include "llvm/Support/Casting.h"
Alex Bradbury6a4b5442018-06-07 15:35:47 +000033#include "llvm/Support/MathExtras.h"
Alex Bradbury04f06d92017-08-08 14:43:36 +000034#include "llvm/Support/TargetRegistry.h"
35
Alex Bradbury6a4b5442018-06-07 15:35:47 +000036#include <limits>
37
Alex Bradbury04f06d92017-08-08 14:43:36 +000038using namespace llvm;
39
Sameer AbuAsalc1b0e662018-04-06 21:07:05 +000040// Include the auto-generated portion of the compress emitter.
41#define GEN_COMPRESS_INSTR
42#include "RISCVGenCompressInstEmitter.inc"
43
Alex Bradbury04f06d92017-08-08 14:43:36 +000044namespace {
45struct RISCVOperand;
46
47class RISCVAsmParser : public MCTargetAsmParser {
Alex Bradbury893e5bc2018-11-28 16:39:14 +000048 SmallVector<FeatureBitset, 4> FeatureBitStack;
49
Alex Bradbury04f06d92017-08-08 14:43:36 +000050 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
Alex Bradburya6e62482017-12-07 10:53:48 +000051 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
Alex Bradburydab1f6f2019-03-22 11:21:40 +000052 bool isRV32E() const { return getSTI().hasFeature(RISCV::FeatureRV32E); }
Alex Bradbury04f06d92017-08-08 14:43:36 +000053
Alex Bradburybca0c3c2018-05-11 17:30:28 +000054 RISCVTargetStreamer &getTargetStreamer() {
55 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
56 return static_cast<RISCVTargetStreamer &>(TS);
57 }
58
Alex Bradbury7bc2a952017-12-07 10:46:23 +000059 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
60 unsigned Kind) override;
61
Alex Bradbury6758ecb2017-09-17 14:27:35 +000062 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
Alex Bradbury6a4b5442018-06-07 15:35:47 +000063 int64_t Lower, int64_t Upper, Twine Msg);
Alex Bradbury6758ecb2017-09-17 14:27:35 +000064
Alex Bradbury04f06d92017-08-08 14:43:36 +000065 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
66 OperandVector &Operands, MCStreamer &Out,
67 uint64_t &ErrorInfo,
68 bool MatchingInlineAsm) override;
69
70 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
71
72 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
73 SMLoc NameLoc, OperandVector &Operands) override;
74
75 bool ParseDirective(AsmToken DirectiveID) override;
76
Alex Bradbury6a4b5442018-06-07 15:35:47 +000077 // Helper to actually emit an instruction to the MCStreamer. Also, when
78 // possible, compression of the instruction is performed.
79 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
80
81 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
82 // synthesize the desired immedate value into the destination register.
Luis Marquesfa06e952019-08-16 14:27:50 +000083 void emitLoadImm(Register DestReg, int64_t Value, MCStreamer &Out);
Alex Bradbury6a4b5442018-06-07 15:35:47 +000084
Alex Bradbury22531c42019-02-15 09:53:32 +000085 // Helper to emit a combination of AUIPC and SecondOpcode. Used to implement
86 // helpers such as emitLoadLocalAddress and emitLoadAddress.
87 void emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
88 const MCExpr *Symbol, RISCVMCExpr::VariantKind VKHi,
89 unsigned SecondOpcode, SMLoc IDLoc, MCStreamer &Out);
90
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +000091 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
92 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
93
Alex Bradbury22531c42019-02-15 09:53:32 +000094 // Helper to emit pseudo instruction "la" used in GOT/PC-rel addressing.
95 void emitLoadAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
96
Lewis Revill74927552019-05-23 14:46:27 +000097 // Helper to emit pseudo instruction "la.tls.ie" used in initial-exec TLS
98 // addressing.
99 void emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
100
101 // Helper to emit pseudo instruction "la.tls.gd" used in global-dynamic TLS
102 // addressing.
103 void emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
104
Kito Cheng303217e2019-02-20 03:31:32 +0000105 // Helper to emit pseudo load/store instruction with a symbol.
106 void emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
107 MCStreamer &Out, bool HasTmpReg);
108
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000109 // Checks that a PseudoAddTPRel is using x4/tp in its second input operand.
110 // Enforcing this using a restricted register class for the second input
111 // operand of PseudoAddTPRel results in a poor diagnostic due to the fact
112 // 'add' is an overloaded mnemonic.
113 bool checkPseudoAddTPRel(MCInst &Inst, OperandVector &Operands);
114
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000115 /// Helper for processing MC instructions that have been successfully matched
116 /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
117 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
118 /// in this method.
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000119 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
120 MCStreamer &Out);
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000121
Alex Bradbury04f06d92017-08-08 14:43:36 +0000122// Auto-generated instruction matching functions
123#define GET_ASSEMBLER_HEADER
124#include "RISCVGenAsmMatcher.inc"
125
Ana Pazos9d6c5532018-10-04 21:50:54 +0000126 OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000127 OperandMatchResultTy parseImmediate(OperandVector &Operands);
Alex Bradbury8c345c52017-11-09 15:00:03 +0000128 OperandMatchResultTy parseRegister(OperandVector &Operands,
129 bool AllowParens = false);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000130 OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
Sam Elliottf596f452019-08-01 12:42:31 +0000131 OperandMatchResultTy parseAtomicMemOp(OperandVector &Operands);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000132 OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
Alex Bradbury68f73c12018-09-18 15:18:16 +0000133 OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000134 OperandMatchResultTy parseCallSymbol(OperandVector &Operands);
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000135 OperandMatchResultTy parseJALOffset(OperandVector &Operands);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000136
Alex Bradbury68f73c12018-09-18 15:18:16 +0000137 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000138
Alex Bradburybca0c3c2018-05-11 17:30:28 +0000139 bool parseDirectiveOption();
140
141 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
142 if (!(getSTI().getFeatureBits()[Feature])) {
143 MCSubtargetInfo &STI = copySTI();
144 setAvailableFeatures(
145 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
146 }
147 }
148
149 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
150 if (getSTI().getFeatureBits()[Feature]) {
151 MCSubtargetInfo &STI = copySTI();
152 setAvailableFeatures(
153 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
154 }
155 }
Ana Pazos9d6c5532018-10-04 21:50:54 +0000156
Alex Bradbury893e5bc2018-11-28 16:39:14 +0000157 void pushFeatureBits() {
158 FeatureBitStack.push_back(getSTI().getFeatureBits());
159 }
160
161 bool popFeatureBits() {
162 if (FeatureBitStack.empty())
163 return true;
164
165 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
166 copySTI().setFeatureBits(FeatureBits);
167 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
168
169 return false;
170 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000171public:
172 enum RISCVMatchResultTy {
173 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
174#define GET_OPERAND_DIAGNOSTIC_TYPES
175#include "RISCVGenAsmMatcher.inc"
176#undef GET_OPERAND_DIAGNOSTIC_TYPES
177 };
178
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000179 static bool classifySymbolRef(const MCExpr *Expr,
180 RISCVMCExpr::VariantKind &Kind,
181 int64_t &Addend);
182
Alex Bradbury04f06d92017-08-08 14:43:36 +0000183 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
184 const MCInstrInfo &MII, const MCTargetOptions &Options)
Oliver Stannard4191b9e2017-10-11 09:17:43 +0000185 : MCTargetAsmParser(Options, STI, MII) {
Alex Bradburycea6db02018-05-17 05:58:08 +0000186 Parser.addAliasForDirective(".half", ".2byte");
187 Parser.addAliasForDirective(".hword", ".2byte");
188 Parser.addAliasForDirective(".word", ".4byte");
189 Parser.addAliasForDirective(".dword", ".8byte");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000190 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
191 }
192};
193
194/// RISCVOperand - Instances of this class represent a parsed machine
195/// instruction
196struct RISCVOperand : public MCParsedAsmOperand {
197
Luis Marquesfa06e952019-08-16 14:27:50 +0000198 enum class KindTy {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000199 Token,
200 Register,
201 Immediate,
Ana Pazos9d6c5532018-10-04 21:50:54 +0000202 SystemRegister
Alex Bradbury04f06d92017-08-08 14:43:36 +0000203 } Kind;
204
Alex Bradburya6e62482017-12-07 10:53:48 +0000205 bool IsRV64;
206
Alex Bradbury04f06d92017-08-08 14:43:36 +0000207 struct RegOp {
Luis Marquesfa06e952019-08-16 14:27:50 +0000208 Register RegNum;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000209 };
210
211 struct ImmOp {
212 const MCExpr *Val;
213 };
214
Ana Pazos9d6c5532018-10-04 21:50:54 +0000215 struct SysRegOp {
216 const char *Data;
217 unsigned Length;
218 unsigned Encoding;
219 // FIXME: Add the Encoding parsed fields as needed for checks,
220 // e.g.: read/write or user/supervisor/machine privileges.
221 };
222
Alex Bradbury04f06d92017-08-08 14:43:36 +0000223 SMLoc StartLoc, EndLoc;
224 union {
225 StringRef Tok;
226 RegOp Reg;
227 ImmOp Imm;
Ana Pazos9d6c5532018-10-04 21:50:54 +0000228 struct SysRegOp SysReg;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000229 };
230
231 RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
232
233public:
234 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
235 Kind = o.Kind;
Alex Bradburya6e62482017-12-07 10:53:48 +0000236 IsRV64 = o.IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000237 StartLoc = o.StartLoc;
238 EndLoc = o.EndLoc;
239 switch (Kind) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000240 case KindTy::Register:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000241 Reg = o.Reg;
242 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000243 case KindTy::Immediate:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000244 Imm = o.Imm;
245 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000246 case KindTy::Token:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000247 Tok = o.Tok;
248 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000249 case KindTy::SystemRegister:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000250 SysReg = o.SysReg;
251 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000252 }
253 }
254
Luis Marquesfa06e952019-08-16 14:27:50 +0000255 bool isToken() const override { return Kind == KindTy::Token; }
256 bool isReg() const override { return Kind == KindTy::Register; }
257 bool isImm() const override { return Kind == KindTy::Immediate; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000258 bool isMem() const override { return false; }
Luis Marquesfa06e952019-08-16 14:27:50 +0000259 bool isSystemRegister() const { return Kind == KindTy::SystemRegister; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000260
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000261 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
262 RISCVMCExpr::VariantKind &VK) {
263 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000264 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000265 return RE->evaluateAsConstant(Imm);
266 }
267
268 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000269 VK = RISCVMCExpr::VK_RISCV_None;
270 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000271 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000272 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000273
274 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000275 }
276
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000277 // True if operand is a symbol with no modifiers, or a constant with no
278 // modifiers and isShiftedInt<N-1, 1>(Op).
279 template <int N> bool isBareSimmNLsb0() const {
280 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000281 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000282 if (!isImm())
283 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000284 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000285 bool IsValid;
286 if (!IsConstantImm)
287 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
288 else
289 IsValid = isShiftedInt<N - 1, 1>(Imm);
290 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000291 }
292
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000293 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
294
Shiva Chen98f93892018-04-25 14:18:55 +0000295 bool isBareSymbol() const {
296 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000297 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Shiva Chen98f93892018-04-25 14:18:55 +0000298 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000299 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000300 return false;
301 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
302 VK == RISCVMCExpr::VK_RISCV_None;
303 }
304
Alex Bradbury44668ae2019-04-01 14:53:17 +0000305 bool isCallSymbol() const {
306 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000307 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury44668ae2019-04-01 14:53:17 +0000308 // Must be of 'immediate' type but not a constant.
309 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
310 return false;
311 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
Alex Bradburyf8078f62019-04-02 12:47:20 +0000312 (VK == RISCVMCExpr::VK_RISCV_CALL ||
313 VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000314 }
315
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000316 bool isTPRelAddSymbol() const {
317 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000318 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000319 // Must be of 'immediate' type but not a constant.
320 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
321 return false;
322 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
323 VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
324 }
325
Ana Pazos9d6c5532018-10-04 21:50:54 +0000326 bool isCSRSystemRegister() const { return isSystemRegister(); }
327
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000328 /// Return true if the operand is a valid for the fence instruction e.g.
329 /// ('iorw').
330 bool isFenceArg() const {
331 if (!isImm())
332 return false;
333 const MCExpr *Val = getImm();
334 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
335 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
336 return false;
337
338 StringRef Str = SVal->getSymbol().getName();
339 // Letters must be unique, taken from 'iorw', and in ascending order. This
340 // holds as long as each individual character is one of 'iorw' and is
341 // greater than the previous character.
342 char Prev = '\0';
343 for (char c : Str) {
344 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
345 return false;
346 if (c <= Prev)
347 return false;
348 Prev = c;
349 }
350 return true;
351 }
352
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000353 /// Return true if the operand is a valid floating point rounding mode.
354 bool isFRMArg() const {
355 if (!isImm())
356 return false;
357 const MCExpr *Val = getImm();
358 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
359 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
360 return false;
361
362 StringRef Str = SVal->getSymbol().getName();
363
364 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
365 }
366
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000367 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000368 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000369 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000370 if (!isImm())
371 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000372 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000373 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
374 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000375 // Given only Imm, ensuring that the actually specified constant is either
376 // a signed or unsigned 64-bit number is unfortunately impossible.
Luis Marques2d0cd6c2019-09-20 13:48:02 +0000377 return IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None &&
378 (isRV64() || (isInt<32>(Imm) || isUInt<32>(Imm)));
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000379 }
380
Alex Bradburya6e62482017-12-07 10:53:48 +0000381 bool isUImmLog2XLen() const {
382 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000383 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradburya6e62482017-12-07 10:53:48 +0000384 if (!isImm())
385 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000386 if (!evaluateConstantImm(getImm(), Imm, VK) ||
387 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000388 return false;
389 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
390 }
391
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000392 bool isUImmLog2XLenNonZero() const {
393 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000394 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000395 if (!isImm())
396 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000397 if (!evaluateConstantImm(getImm(), Imm, VK) ||
398 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000399 return false;
400 if (Imm == 0)
401 return false;
402 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
403 }
404
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000405 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000406 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000407 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000408 if (!isImm())
409 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000410 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000411 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000412 }
413
Alex Bradbury60714f92017-12-13 09:32:55 +0000414 bool isUImm5NonZero() const {
415 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000416 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000417 if (!isImm())
418 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000419 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000420 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
421 VK == RISCVMCExpr::VK_RISCV_None;
422 }
423
Alex Bradbury581d6b02017-12-13 09:41:21 +0000424 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000425 if (!isImm())
426 return false;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000427 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000428 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000429 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000430 return IsConstantImm && isInt<6>(Imm) &&
431 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000432 }
433
Shiva Chenb22c1d22018-02-02 02:43:23 +0000434 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000435 if (!isImm())
436 return false;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000437 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000438 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000439 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000440 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
441 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000442 }
443
Shiva Chen7c172422018-02-22 15:02:28 +0000444 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000445 if (!isImm())
446 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000447 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000448 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000449 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000450 return IsConstantImm && (Imm != 0) &&
451 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000452 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000453 }
454
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000455 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000456 if (!isImm())
457 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000458 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000459 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000460 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000461 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
462 VK == RISCVMCExpr::VK_RISCV_None;
463 }
464
465 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000466 if (!isImm())
467 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000468 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000469 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000470 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000471 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
472 VK == RISCVMCExpr::VK_RISCV_None;
473 }
474
475 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000476 if (!isImm())
477 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000478 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000479 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000480 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000481 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
482 VK == RISCVMCExpr::VK_RISCV_None;
483 }
484
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000485 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
486
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000487 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000488 if (!isImm())
489 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000490 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000491 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000492 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000493 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
494 VK == RISCVMCExpr::VK_RISCV_None;
495 }
496
Alex Bradbury60714f92017-12-13 09:32:55 +0000497 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000498 if (!isImm())
499 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000500 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000501 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000502 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000503 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
504 VK == RISCVMCExpr::VK_RISCV_None;
505 }
506
Alex Bradbury04f06d92017-08-08 14:43:36 +0000507 bool isSImm12() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000508 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000509 int64_t Imm;
510 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000511 if (!isImm())
512 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000513 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000514 if (!IsConstantImm)
515 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
516 else
517 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000518 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000519 VK == RISCVMCExpr::VK_RISCV_LO ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000520 VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
521 VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000522 }
523
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000524 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
525
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000526 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000527
Shiva Chenb22c1d22018-02-02 02:43:23 +0000528 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000529 if (!isImm())
530 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000531 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000532 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000533 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000534 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000535 VK == RISCVMCExpr::VK_RISCV_None;
536 }
537
Alex Bradbury74340f12018-09-18 15:08:35 +0000538 bool isUImm20LUI() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000539 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000540 int64_t Imm;
541 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000542 if (!isImm())
543 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000544 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000545 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000546 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000547 return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
548 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000549 } else {
550 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000551 VK == RISCVMCExpr::VK_RISCV_HI ||
552 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000553 }
554 }
555
556 bool isUImm20AUIPC() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000557 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury74340f12018-09-18 15:08:35 +0000558 int64_t Imm;
559 bool IsValid;
560 if (!isImm())
561 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000562 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000563 if (!IsConstantImm) {
564 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000565 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000566 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
567 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
568 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000569 } else {
570 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000571 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000572 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
573 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
574 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000575 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000576 }
577
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000578 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000579
Sam Elliottf596f452019-08-01 12:42:31 +0000580 bool isImmZero() const {
581 if (!isImm())
582 return false;
583 int64_t Imm;
584 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
585 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
586 return IsConstantImm && (Imm == 0) && VK == RISCVMCExpr::VK_RISCV_None;
587 }
588
Alex Bradbury04f06d92017-08-08 14:43:36 +0000589 /// getStartLoc - Gets location of the first token of this operand
590 SMLoc getStartLoc() const override { return StartLoc; }
591 /// getEndLoc - Gets location of the last token of this operand
592 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000593 /// True if this operand is for an RV64 instruction
594 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000595
596 unsigned getReg() const override {
Luis Marquesfa06e952019-08-16 14:27:50 +0000597 assert(Kind == KindTy::Register && "Invalid type access!");
598 return Reg.RegNum.id();
Alex Bradbury04f06d92017-08-08 14:43:36 +0000599 }
600
Ana Pazos9d6c5532018-10-04 21:50:54 +0000601 StringRef getSysReg() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000602 assert(Kind == KindTy::SystemRegister && "Invalid access!");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000603 return StringRef(SysReg.Data, SysReg.Length);
604 }
605
Alex Bradbury04f06d92017-08-08 14:43:36 +0000606 const MCExpr *getImm() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000607 assert(Kind == KindTy::Immediate && "Invalid type access!");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000608 return Imm.Val;
609 }
610
611 StringRef getToken() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000612 assert(Kind == KindTy::Token && "Invalid type access!");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000613 return Tok;
614 }
615
616 void print(raw_ostream &OS) const override {
617 switch (Kind) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000618 case KindTy::Immediate:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000619 OS << *getImm();
620 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000621 case KindTy::Register:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000622 OS << "<register x";
623 OS << getReg() << ">";
624 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000625 case KindTy::Token:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000626 OS << "'" << getToken() << "'";
627 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000628 case KindTy::SystemRegister:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000629 OS << "<sysreg: " << getSysReg() << '>';
630 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000631 }
632 }
633
Alex Bradburya6e62482017-12-07 10:53:48 +0000634 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
635 bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000636 auto Op = std::make_unique<RISCVOperand>(KindTy::Token);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000637 Op->Tok = Str;
638 Op->StartLoc = S;
639 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000640 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000641 return Op;
642 }
643
644 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000645 SMLoc E, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000646 auto Op = std::make_unique<RISCVOperand>(KindTy::Register);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000647 Op->Reg.RegNum = RegNo;
648 Op->StartLoc = S;
649 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000650 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000651 return Op;
652 }
653
654 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000655 SMLoc E, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000656 auto Op = std::make_unique<RISCVOperand>(KindTy::Immediate);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000657 Op->Imm.Val = Val;
658 Op->StartLoc = S;
659 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000660 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000661 return Op;
662 }
663
Ana Pazos9d6c5532018-10-04 21:50:54 +0000664 static std::unique_ptr<RISCVOperand>
665 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000666 auto Op = std::make_unique<RISCVOperand>(KindTy::SystemRegister);
Ana Pazos9d6c5532018-10-04 21:50:54 +0000667 Op->SysReg.Data = Str.data();
668 Op->SysReg.Length = Str.size();
669 Op->SysReg.Encoding = Encoding;
670 Op->StartLoc = S;
671 Op->IsRV64 = IsRV64;
672 return Op;
673 }
674
Alex Bradbury04f06d92017-08-08 14:43:36 +0000675 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
676 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000677 int64_t Imm = 0;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000678 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000679 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000680
681 if (IsConstant)
682 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000683 else
684 Inst.addOperand(MCOperand::createExpr(Expr));
685 }
686
687 // Used by the TableGen Code
688 void addRegOperands(MCInst &Inst, unsigned N) const {
689 assert(N == 1 && "Invalid number of operands!");
690 Inst.addOperand(MCOperand::createReg(getReg()));
691 }
692
693 void addImmOperands(MCInst &Inst, unsigned N) const {
694 assert(N == 1 && "Invalid number of operands!");
695 addExpr(Inst, getImm());
696 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000697
698 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
699 assert(N == 1 && "Invalid number of operands!");
700 // isFenceArg has validated the operand, meaning this cast is safe
701 auto SE = cast<MCSymbolRefExpr>(getImm());
702
703 unsigned Imm = 0;
704 for (char c : SE->getSymbol().getName()) {
705 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000706 default:
707 llvm_unreachable("FenceArg must contain only [iorw]");
708 case 'i': Imm |= RISCVFenceField::I; break;
709 case 'o': Imm |= RISCVFenceField::O; break;
710 case 'r': Imm |= RISCVFenceField::R; break;
711 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000712 }
713 }
714 Inst.addOperand(MCOperand::createImm(Imm));
715 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000716
Ana Pazos9d6c5532018-10-04 21:50:54 +0000717 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
718 assert(N == 1 && "Invalid number of operands!");
719 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
720 }
721
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000722 // Returns the rounding mode represented by this RISCVOperand. Should only
723 // be called after checking isFRMArg.
724 RISCVFPRndMode::RoundingMode getRoundingMode() const {
725 // isFRMArg has validated the operand, meaning this cast is safe.
726 auto SE = cast<MCSymbolRefExpr>(getImm());
727 RISCVFPRndMode::RoundingMode FRM =
728 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
729 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
730 return FRM;
731 }
732
733 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
734 assert(N == 1 && "Invalid number of operands!");
735 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
736 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000737};
738} // end anonymous namespace.
739
740#define GET_REGISTER_MATCHER
741#define GET_MATCHER_IMPLEMENTATION
Alex Bradbury04f06d92017-08-08 14:43:36 +0000742#include "RISCVGenAsmMatcher.inc"
743
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000744// Return the matching FPR64 register for the given FPR32.
745// FIXME: Ideally this function could be removed in favour of using
746// information from TableGen.
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000747static Register convertFPR32ToFPR64(Register Reg) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000748 switch (Reg) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000749 default:
750 llvm_unreachable("Not a recognised FPR32 register");
751 case RISCV::F0_32: return RISCV::F0_64;
752 case RISCV::F1_32: return RISCV::F1_64;
753 case RISCV::F2_32: return RISCV::F2_64;
754 case RISCV::F3_32: return RISCV::F3_64;
755 case RISCV::F4_32: return RISCV::F4_64;
756 case RISCV::F5_32: return RISCV::F5_64;
757 case RISCV::F6_32: return RISCV::F6_64;
758 case RISCV::F7_32: return RISCV::F7_64;
759 case RISCV::F8_32: return RISCV::F8_64;
760 case RISCV::F9_32: return RISCV::F9_64;
761 case RISCV::F10_32: return RISCV::F10_64;
762 case RISCV::F11_32: return RISCV::F11_64;
763 case RISCV::F12_32: return RISCV::F12_64;
764 case RISCV::F13_32: return RISCV::F13_64;
765 case RISCV::F14_32: return RISCV::F14_64;
766 case RISCV::F15_32: return RISCV::F15_64;
767 case RISCV::F16_32: return RISCV::F16_64;
768 case RISCV::F17_32: return RISCV::F17_64;
769 case RISCV::F18_32: return RISCV::F18_64;
770 case RISCV::F19_32: return RISCV::F19_64;
771 case RISCV::F20_32: return RISCV::F20_64;
772 case RISCV::F21_32: return RISCV::F21_64;
773 case RISCV::F22_32: return RISCV::F22_64;
774 case RISCV::F23_32: return RISCV::F23_64;
775 case RISCV::F24_32: return RISCV::F24_64;
776 case RISCV::F25_32: return RISCV::F25_64;
777 case RISCV::F26_32: return RISCV::F26_64;
778 case RISCV::F27_32: return RISCV::F27_64;
779 case RISCV::F28_32: return RISCV::F28_64;
780 case RISCV::F29_32: return RISCV::F29_64;
781 case RISCV::F30_32: return RISCV::F30_64;
782 case RISCV::F31_32: return RISCV::F31_64;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000783 }
784}
785
786unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
787 unsigned Kind) {
788 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
789 if (!Op.isReg())
790 return Match_InvalidOperand;
791
Luis Marquesfa06e952019-08-16 14:27:50 +0000792 Register Reg = Op.getReg();
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000793 bool IsRegFPR32 =
794 RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg);
Alex Bradbury60714f92017-12-13 09:32:55 +0000795 bool IsRegFPR32C =
796 RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000797
798 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Alex Bradbury60714f92017-12-13 09:32:55 +0000799 // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary.
800 if ((IsRegFPR32 && Kind == MCK_FPR64) ||
801 (IsRegFPR32C && Kind == MCK_FPR64C)) {
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000802 Op.Reg.RegNum = convertFPR32ToFPR64(Reg);
803 return Match_Success;
804 }
805 return Match_InvalidOperand;
806}
807
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000808bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000809 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000810 Twine Msg = "immediate must be an integer in the range") {
811 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
812 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
813}
814
Alex Bradbury04f06d92017-08-08 14:43:36 +0000815bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
816 OperandVector &Operands,
817 MCStreamer &Out,
818 uint64_t &ErrorInfo,
819 bool MatchingInlineAsm) {
820 MCInst Inst;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000821
Ana Pazos6b34051b2018-08-30 19:43:19 +0000822 auto Result =
823 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
824 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000825 default:
826 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000827 case Match_Success:
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000828 return processInstruction(Inst, IDLoc, Operands, Out);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000829 case Match_MissingFeature:
830 return Error(IDLoc, "instruction use requires an option to be enabled");
831 case Match_MnemonicFail:
832 return Error(IDLoc, "unrecognized instruction mnemonic");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000833 case Match_InvalidOperand: {
834 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000835 if (ErrorInfo != ~0U) {
836 if (ErrorInfo >= Operands.size())
837 return Error(ErrorLoc, "too few operands for instruction");
838
839 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
840 if (ErrorLoc == SMLoc())
841 ErrorLoc = IDLoc;
842 }
843 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000844 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000845 }
846
847 // Handle the case when the error message is of specific type
848 // other than the generic Match_InvalidOperand, and the
849 // corresponding operand is missing.
850 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
851 SMLoc ErrorLoc = IDLoc;
852 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
853 return Error(ErrorLoc, "too few operands for instruction");
854 }
855
856 switch(Result) {
857 default:
858 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000859 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000860 if (isRV64()) {
861 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
862 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
863 }
864 return generateImmOutOfRangeError(Operands, ErrorInfo,
865 std::numeric_limits<int32_t>::min(),
866 std::numeric_limits<uint32_t>::max());
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000867 case Match_InvalidImmZero: {
868 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
869 return Error(ErrorLoc, "immediate must be zero");
870 }
Alex Bradburya6e62482017-12-07 10:53:48 +0000871 case Match_InvalidUImmLog2XLen:
872 if (isRV64())
873 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
874 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000875 case Match_InvalidUImmLog2XLenNonZero:
876 if (isRV64())
877 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
878 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000879 case Match_InvalidUImm5:
880 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000881 case Match_InvalidSImm6:
882 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
883 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000884 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000885 return generateImmOutOfRangeError(
886 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000887 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000888 case Match_InvalidCLUIImm:
889 return generateImmOutOfRangeError(
890 Operands, ErrorInfo, 1, (1 << 5) - 1,
891 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000892 case Match_InvalidUImm7Lsb00:
893 return generateImmOutOfRangeError(
894 Operands, ErrorInfo, 0, (1 << 7) - 4,
895 "immediate must be a multiple of 4 bytes in the range");
896 case Match_InvalidUImm8Lsb00:
897 return generateImmOutOfRangeError(
898 Operands, ErrorInfo, 0, (1 << 8) - 4,
899 "immediate must be a multiple of 4 bytes in the range");
900 case Match_InvalidUImm8Lsb000:
901 return generateImmOutOfRangeError(
902 Operands, ErrorInfo, 0, (1 << 8) - 8,
903 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000904 case Match_InvalidSImm9Lsb0:
905 return generateImmOutOfRangeError(
906 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
907 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000908 case Match_InvalidUImm9Lsb000:
909 return generateImmOutOfRangeError(
910 Operands, ErrorInfo, 0, (1 << 9) - 8,
911 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000912 case Match_InvalidUImm10Lsb00NonZero:
913 return generateImmOutOfRangeError(
914 Operands, ErrorInfo, 4, (1 << 10) - 4,
915 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000916 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000917 return generateImmOutOfRangeError(
918 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000919 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000920 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000921 return generateImmOutOfRangeError(
922 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000923 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
924 "integer in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000925 case Match_InvalidSImm12Lsb0:
926 return generateImmOutOfRangeError(
927 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
928 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000929 case Match_InvalidSImm13Lsb0:
930 return generateImmOutOfRangeError(
931 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
932 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000933 case Match_InvalidUImm20LUI:
934 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000935 "operand must be a symbol with "
936 "%hi/%tprel_hi modifier or an integer in "
937 "the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000938 case Match_InvalidUImm20AUIPC:
939 return generateImmOutOfRangeError(
940 Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revilldf3cb472019-04-23 14:46:13 +0000941 "operand must be a symbol with a "
942 "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi modifier or "
943 "an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000944 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000945 return generateImmOutOfRangeError(
946 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
947 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000948 case Match_InvalidCSRSystemRegister: {
949 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
950 "operand must be a valid system register "
951 "name or an integer in the range");
952 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000953 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000954 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000955 return Error(
956 ErrorLoc,
957 "operand must be formed of letters selected in-order from 'iorw'");
958 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000959 case Match_InvalidFRMArg: {
960 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
961 return Error(
962 ErrorLoc,
963 "operand must be a valid floating point rounding mode mnemonic");
964 }
Shiva Chen98f93892018-04-25 14:18:55 +0000965 case Match_InvalidBareSymbol: {
966 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
967 return Error(ErrorLoc, "operand must be a bare symbol name");
968 }
Alex Bradbury44668ae2019-04-01 14:53:17 +0000969 case Match_InvalidCallSymbol: {
970 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
971 return Error(ErrorLoc, "operand must be a bare symbol name");
972 }
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000973 case Match_InvalidTPRelAddSymbol: {
974 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
975 return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
976 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000977 }
978
979 llvm_unreachable("Unknown match type detected!");
980}
981
Alex Bradbury99794792019-03-17 12:02:32 +0000982// Attempts to match Name as a register (either using the default name or
983// alternative ABI names), setting RegNo to the matching register. Upon
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000984// failure, returns true and sets RegNo to 0. If IsRV32E then registers
985// x16-x31 will be rejected.
Luis Marquesfa06e952019-08-16 14:27:50 +0000986static bool matchRegisterNameHelper(bool IsRV32E, Register &RegNo,
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000987 StringRef Name) {
Alex Bradbury99794792019-03-17 12:02:32 +0000988 RegNo = MatchRegisterName(Name);
Benjamin Kramer167b3022019-09-17 14:27:31 +0000989 if (RegNo == RISCV::NoRegister)
Alex Bradbury99794792019-03-17 12:02:32 +0000990 RegNo = MatchRegisterAltName(Name);
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000991 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
Benjamin Kramer167b3022019-09-17 14:27:31 +0000992 RegNo = RISCV::NoRegister;
993 return RegNo == RISCV::NoRegister;
Alex Bradbury99794792019-03-17 12:02:32 +0000994}
995
Alex Bradbury04f06d92017-08-08 14:43:36 +0000996bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
997 SMLoc &EndLoc) {
998 const AsmToken &Tok = getParser().getTok();
999 StartLoc = Tok.getLoc();
1000 EndLoc = Tok.getEndLoc();
1001 RegNo = 0;
1002 StringRef Name = getLexer().getTok().getIdentifier();
1003
Luis Marquesfa06e952019-08-16 14:27:50 +00001004 if (matchRegisterNameHelper(isRV32E(), (Register&)RegNo, Name))
Alex Bradburyb18e3142019-03-17 12:00:58 +00001005 return Error(StartLoc, "invalid register name");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001006
Alex Bradburyb18e3142019-03-17 12:00:58 +00001007 getParser().Lex(); // Eat identifier token.
1008 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001009}
1010
Alex Bradbury8c345c52017-11-09 15:00:03 +00001011OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
1012 bool AllowParens) {
1013 SMLoc FirstS = getLoc();
1014 bool HadParens = false;
Lewis Revill218aa0e2019-06-19 10:11:13 +00001015 AsmToken LParen;
Alex Bradbury8c345c52017-11-09 15:00:03 +00001016
Lewis Revill218aa0e2019-06-19 10:11:13 +00001017 // If this is an LParen and a parenthesised register name is allowed, parse it
1018 // atomically.
Alex Bradbury8c345c52017-11-09 15:00:03 +00001019 if (AllowParens && getLexer().is(AsmToken::LParen)) {
Lewis Revill218aa0e2019-06-19 10:11:13 +00001020 AsmToken Buf[2];
Alex Bradbury8c345c52017-11-09 15:00:03 +00001021 size_t ReadCount = getLexer().peekTokens(Buf);
1022 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
1023 HadParens = true;
Lewis Revill218aa0e2019-06-19 10:11:13 +00001024 LParen = getParser().getTok();
Alex Bradbury8c345c52017-11-09 15:00:03 +00001025 getParser().Lex(); // Eat '('
1026 }
1027 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001028
1029 switch (getLexer().getKind()) {
1030 default:
Lewis Revill218aa0e2019-06-19 10:11:13 +00001031 if (HadParens)
1032 getLexer().UnLex(LParen);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001033 return MatchOperand_NoMatch;
1034 case AsmToken::Identifier:
1035 StringRef Name = getLexer().getTok().getIdentifier();
Luis Marquesfa06e952019-08-16 14:27:50 +00001036 Register RegNo;
Alex Bradburydab1f6f2019-03-22 11:21:40 +00001037 matchRegisterNameHelper(isRV32E(), RegNo, Name);
Alex Bradbury99794792019-03-17 12:02:32 +00001038
Benjamin Kramer167b3022019-09-17 14:27:31 +00001039 if (RegNo == RISCV::NoRegister) {
Alex Bradbury99794792019-03-17 12:02:32 +00001040 if (HadParens)
Lewis Revill218aa0e2019-06-19 10:11:13 +00001041 getLexer().UnLex(LParen);
Alex Bradbury99794792019-03-17 12:02:32 +00001042 return MatchOperand_NoMatch;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001043 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001044 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +00001045 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001046 SMLoc S = getLoc();
1047 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001048 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +00001049 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001050 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001051
1052 if (HadParens) {
1053 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001054 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001055 }
1056
Alex Bradbury04f06d92017-08-08 14:43:36 +00001057 return MatchOperand_Success;
1058}
1059
Ana Pazos9d6c5532018-10-04 21:50:54 +00001060OperandMatchResultTy
1061RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1062 SMLoc S = getLoc();
1063 const MCExpr *Res;
1064
1065 switch (getLexer().getKind()) {
1066 default:
1067 return MatchOperand_NoMatch;
1068 case AsmToken::LParen:
1069 case AsmToken::Minus:
1070 case AsmToken::Plus:
Lewis Revill18737e82019-06-19 10:27:24 +00001071 case AsmToken::Exclaim:
1072 case AsmToken::Tilde:
Ana Pazos9d6c5532018-10-04 21:50:54 +00001073 case AsmToken::Integer:
1074 case AsmToken::String: {
1075 if (getParser().parseExpression(Res))
1076 return MatchOperand_ParseFail;
1077
1078 auto *CE = dyn_cast<MCConstantExpr>(Res);
1079 if (CE) {
1080 int64_t Imm = CE->getValue();
1081 if (isUInt<12>(Imm)) {
1082 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1083 // Accept an immediate representing a named or un-named Sys Reg
1084 // if the range is valid, regardless of the required features.
1085 Operands.push_back(RISCVOperand::createSysReg(
1086 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1087 return MatchOperand_Success;
1088 }
1089 }
1090
1091 Twine Msg = "immediate must be an integer in the range";
1092 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1093 return MatchOperand_ParseFail;
1094 }
1095 case AsmToken::Identifier: {
1096 StringRef Identifier;
1097 if (getParser().parseIdentifier(Identifier))
1098 return MatchOperand_ParseFail;
1099
1100 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1101 // Accept a named Sys Reg if the required features are present.
1102 if (SysReg) {
1103 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1104 Error(S, "system register use requires an option to be enabled");
1105 return MatchOperand_ParseFail;
1106 }
1107 Operands.push_back(RISCVOperand::createSysReg(
1108 Identifier, S, SysReg->Encoding, isRV64()));
1109 return MatchOperand_Success;
1110 }
1111
1112 Twine Msg = "operand must be a valid system register name "
1113 "or an integer in the range";
1114 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1115 return MatchOperand_ParseFail;
1116 }
1117 case AsmToken::Percent: {
1118 // Discard operand with modifier.
1119 Twine Msg = "immediate must be an integer in the range";
1120 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1121 return MatchOperand_ParseFail;
1122 }
1123 }
1124
1125 return MatchOperand_NoMatch;
1126}
1127
Alex Bradbury04f06d92017-08-08 14:43:36 +00001128OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001129 SMLoc S = getLoc();
1130 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1131 const MCExpr *Res;
1132
Alex Bradbury04f06d92017-08-08 14:43:36 +00001133 switch (getLexer().getKind()) {
1134 default:
1135 return MatchOperand_NoMatch;
1136 case AsmToken::LParen:
Sam Elliottfafec512019-07-12 08:36:07 +00001137 case AsmToken::Dot:
Alex Bradbury04f06d92017-08-08 14:43:36 +00001138 case AsmToken::Minus:
1139 case AsmToken::Plus:
Lewis Revill18737e82019-06-19 10:27:24 +00001140 case AsmToken::Exclaim:
1141 case AsmToken::Tilde:
Alex Bradbury04f06d92017-08-08 14:43:36 +00001142 case AsmToken::Integer:
1143 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001144 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001145 if (getParser().parseExpression(Res))
1146 return MatchOperand_ParseFail;
1147 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001148 case AsmToken::Percent:
1149 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001150 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001151
Alex Bradburya6e62482017-12-07 10:53:48 +00001152 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001153 return MatchOperand_Success;
1154}
1155
1156OperandMatchResultTy
1157RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1158 SMLoc S = getLoc();
1159 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1160
1161 if (getLexer().getKind() != AsmToken::Percent) {
1162 Error(getLoc(), "expected '%' for operand modifier");
1163 return MatchOperand_ParseFail;
1164 }
1165
1166 getParser().Lex(); // Eat '%'
1167
1168 if (getLexer().getKind() != AsmToken::Identifier) {
1169 Error(getLoc(), "expected valid identifier for operand modifier");
1170 return MatchOperand_ParseFail;
1171 }
1172 StringRef Identifier = getParser().getTok().getIdentifier();
1173 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1174 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1175 Error(getLoc(), "unrecognized operand modifier");
1176 return MatchOperand_ParseFail;
1177 }
1178
1179 getParser().Lex(); // Eat the identifier
1180 if (getLexer().getKind() != AsmToken::LParen) {
1181 Error(getLoc(), "expected '('");
1182 return MatchOperand_ParseFail;
1183 }
1184 getParser().Lex(); // Eat '('
1185
1186 const MCExpr *SubExpr;
1187 if (getParser().parseParenExpression(SubExpr, E)) {
1188 return MatchOperand_ParseFail;
1189 }
1190
1191 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001192 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001193 return MatchOperand_Success;
1194}
1195
Alex Bradbury68f73c12018-09-18 15:18:16 +00001196OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1197 SMLoc S = getLoc();
1198 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1199 const MCExpr *Res;
1200
1201 if (getLexer().getKind() != AsmToken::Identifier)
1202 return MatchOperand_NoMatch;
1203
1204 StringRef Identifier;
Kito Cheng303217e2019-02-20 03:31:32 +00001205 AsmToken Tok = getLexer().getTok();
1206
Alex Bradbury68f73c12018-09-18 15:18:16 +00001207 if (getParser().parseIdentifier(Identifier))
1208 return MatchOperand_ParseFail;
1209
Alex Bradburyf8078f62019-04-02 12:47:20 +00001210 if (Identifier.consume_back("@plt")) {
1211 Error(getLoc(), "'@plt' operand not valid for instruction");
1212 return MatchOperand_ParseFail;
1213 }
1214
Alex Bradbury68f73c12018-09-18 15:18:16 +00001215 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Kito Cheng303217e2019-02-20 03:31:32 +00001216
1217 if (Sym->isVariable()) {
1218 const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1219 if (!isa<MCSymbolRefExpr>(V)) {
1220 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1221 return MatchOperand_NoMatch;
1222 }
1223 Res = V;
1224 } else
1225 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Lewis Revilld3f774d2019-08-16 12:00:56 +00001226
1227 MCBinaryExpr::Opcode Opcode;
1228 switch (getLexer().getKind()) {
1229 default:
1230 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1231 return MatchOperand_Success;
1232 case AsmToken::Plus:
1233 Opcode = MCBinaryExpr::Add;
1234 break;
1235 case AsmToken::Minus:
1236 Opcode = MCBinaryExpr::Sub;
1237 break;
1238 }
1239
1240 const MCExpr *Expr;
1241 if (getParser().parseExpression(Expr))
1242 return MatchOperand_ParseFail;
1243 Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
Alex Bradbury68f73c12018-09-18 15:18:16 +00001244 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1245 return MatchOperand_Success;
1246}
1247
Alex Bradbury44668ae2019-04-01 14:53:17 +00001248OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1249 SMLoc S = getLoc();
1250 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1251 const MCExpr *Res;
1252
1253 if (getLexer().getKind() != AsmToken::Identifier)
1254 return MatchOperand_NoMatch;
1255
Lewis Revillcf748812019-06-26 10:35:58 +00001256 // Avoid parsing the register in `call rd, foo` as a call symbol.
1257 if (getLexer().peekTok().getKind() != AsmToken::EndOfStatement)
1258 return MatchOperand_NoMatch;
1259
Alex Bradbury44668ae2019-04-01 14:53:17 +00001260 StringRef Identifier;
1261 if (getParser().parseIdentifier(Identifier))
1262 return MatchOperand_ParseFail;
1263
Alex Bradburyf8078f62019-04-02 12:47:20 +00001264 RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1265 if (Identifier.consume_back("@plt"))
1266 Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1267
Alex Bradbury44668ae2019-04-01 14:53:17 +00001268 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1269 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradburyf8078f62019-04-02 12:47:20 +00001270 Res = RISCVMCExpr::create(Res, Kind, getContext());
Alex Bradbury44668ae2019-04-01 14:53:17 +00001271 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1272 return MatchOperand_Success;
1273}
1274
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001275OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1276 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1277 // both being acceptable forms. When parsing `jal ra, foo` this function
1278 // will be called for the `ra` register operand in an attempt to match the
1279 // single-operand alias. parseJALOffset must fail for this case. It would
1280 // seem logical to try parse the operand using parseImmediate and return
1281 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1282 // the second form rather than the first). We can't do this as there's no
1283 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1284 // is an identifier and is followed by a comma.
1285 if (getLexer().is(AsmToken::Identifier) &&
1286 getLexer().peekTok().is(AsmToken::Comma))
1287 return MatchOperand_NoMatch;
1288
1289 return parseImmediate(Operands);
1290}
1291
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001292OperandMatchResultTy
1293RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1294 if (getLexer().isNot(AsmToken::LParen)) {
1295 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001296 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001297 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001298
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001299 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001300 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001301
1302 if (parseRegister(Operands) != MatchOperand_Success) {
1303 Error(getLoc(), "expected register");
1304 return MatchOperand_ParseFail;
1305 }
1306
1307 if (getLexer().isNot(AsmToken::RParen)) {
1308 Error(getLoc(), "expected ')'");
1309 return MatchOperand_ParseFail;
1310 }
1311
1312 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001313 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001314
Alex Bradbury04f06d92017-08-08 14:43:36 +00001315 return MatchOperand_Success;
1316}
1317
Sam Elliottf596f452019-08-01 12:42:31 +00001318OperandMatchResultTy RISCVAsmParser::parseAtomicMemOp(OperandVector &Operands) {
1319 // Atomic operations such as lr.w, sc.w, and amo*.w accept a "memory operand"
1320 // as one of their register operands, such as `(a0)`. This just denotes that
1321 // the register (in this case `a0`) contains a memory address.
1322 //
1323 // Normally, we would be able to parse these by putting the parens into the
1324 // instruction string. However, GNU as also accepts a zero-offset memory
1325 // operand (such as `0(a0)`), and ignores the 0. Normally this would be parsed
1326 // with parseImmediate followed by parseMemOpBaseReg, but these instructions
1327 // do not accept an immediate operand, and we do not want to add a "dummy"
1328 // operand that is silently dropped.
1329 //
1330 // Instead, we use this custom parser. This will: allow (and discard) an
1331 // offset if it is zero; require (and discard) parentheses; and add only the
1332 // parsed register operand to `Operands`.
1333 //
1334 // These operands are printed with RISCVInstPrinter::printAtomicMemOp, which
1335 // will only print the register surrounded by parentheses (which GNU as also
1336 // uses as its canonical representation for these operands).
1337 std::unique_ptr<RISCVOperand> OptionalImmOp;
1338
1339 if (getLexer().isNot(AsmToken::LParen)) {
1340 // Parse an Integer token. We do not accept arbritrary constant expressions
1341 // in the offset field (because they may include parens, which complicates
1342 // parsing a lot).
1343 int64_t ImmVal;
1344 SMLoc ImmStart = getLoc();
1345 if (getParser().parseIntToken(ImmVal,
1346 "expected '(' or optional integer offset"))
1347 return MatchOperand_ParseFail;
1348
1349 // Create a RISCVOperand for checking later (so the error messages are
1350 // nicer), but we don't add it to Operands.
1351 SMLoc ImmEnd = getLoc();
1352 OptionalImmOp =
1353 RISCVOperand::createImm(MCConstantExpr::create(ImmVal, getContext()),
1354 ImmStart, ImmEnd, isRV64());
1355 }
1356
1357 if (getLexer().isNot(AsmToken::LParen)) {
1358 Error(getLoc(), OptionalImmOp ? "expected '(' after optional integer offset"
1359 : "expected '(' or optional integer offset");
1360 return MatchOperand_ParseFail;
1361 }
1362 getParser().Lex(); // Eat '('
1363
1364 if (parseRegister(Operands) != MatchOperand_Success) {
1365 Error(getLoc(), "expected register");
1366 return MatchOperand_ParseFail;
1367 }
1368
1369 if (getLexer().isNot(AsmToken::RParen)) {
1370 Error(getLoc(), "expected ')'");
1371 return MatchOperand_ParseFail;
1372 }
1373 getParser().Lex(); // Eat ')'
1374
1375 // Deferred Handling of non-zero offsets. This makes the error messages nicer.
1376 if (OptionalImmOp && !OptionalImmOp->isImmZero()) {
1377 Error(OptionalImmOp->getStartLoc(), "optional integer offset must be 0",
1378 SMRange(OptionalImmOp->getStartLoc(), OptionalImmOp->getEndLoc()));
1379 return MatchOperand_ParseFail;
1380 }
1381
1382 return MatchOperand_Success;
1383}
1384
Alex Bradburycd8688a2018-04-25 17:25:29 +00001385/// Looks at a token type and creates the relevant operand from this
1386/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001387/// true.
1388bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1389 // Check if the current operand has a custom associated parser, if so, try to
1390 // custom parse the operand, or fallback to the general approach.
1391 OperandMatchResultTy Result =
1392 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1393 if (Result == MatchOperand_Success)
1394 return false;
1395 if (Result == MatchOperand_ParseFail)
1396 return true;
1397
1398 // Attempt to parse token as a register.
1399 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001400 return false;
1401
1402 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001403 if (parseImmediate(Operands) == MatchOperand_Success) {
1404 // Parse memory base register if present
1405 if (getLexer().is(AsmToken::LParen))
1406 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001407 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001408 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001409
1410 // Finally we have exhausted all options and must declare defeat.
1411 Error(getLoc(), "unknown operand");
1412 return true;
1413}
1414
1415bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1416 StringRef Name, SMLoc NameLoc,
1417 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001418 // Ensure that if the instruction occurs when relaxation is enabled,
1419 // relocations are forced for the file. Ideally this would be done when there
1420 // is enough information to reliably determine if the instruction itself may
1421 // cause relaxations. Unfortunately instruction processing stage occurs in the
1422 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1423 // for the entire file.
1424 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1425 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1426 if (Assembler != nullptr) {
1427 RISCVAsmBackend &MAB =
1428 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1429 MAB.setForceRelocs();
1430 }
1431 }
1432
Alex Bradbury04f06d92017-08-08 14:43:36 +00001433 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001434 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001435
1436 // If there are no more operands, then finish
1437 if (getLexer().is(AsmToken::EndOfStatement))
1438 return false;
1439
1440 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001441 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001442 return true;
1443
1444 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001445 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001446 while (getLexer().is(AsmToken::Comma)) {
1447 // Consume comma token
1448 getLexer().Lex();
1449
1450 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001451 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001452 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001453
1454 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001455 }
1456
1457 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1458 SMLoc Loc = getLexer().getLoc();
1459 getParser().eatToEndOfStatement();
1460 return Error(Loc, "unexpected token");
1461 }
1462
1463 getParser().Lex(); // Consume the EndOfStatement.
1464 return false;
1465}
1466
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001467bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1468 RISCVMCExpr::VariantKind &Kind,
1469 int64_t &Addend) {
1470 Kind = RISCVMCExpr::VK_RISCV_None;
1471 Addend = 0;
1472
1473 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1474 Kind = RE->getKind();
1475 Expr = RE->getSubExpr();
1476 }
1477
1478 // It's a simple symbol reference or constant with no addend.
1479 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1480 return true;
1481
1482 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1483 if (!BE)
1484 return false;
1485
1486 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1487 return false;
1488
1489 if (BE->getOpcode() != MCBinaryExpr::Add &&
1490 BE->getOpcode() != MCBinaryExpr::Sub)
1491 return false;
1492
1493 // We are able to support the subtraction of two symbol references
1494 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1495 isa<MCSymbolRefExpr>(BE->getRHS()))
1496 return true;
1497
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001498 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001499 // on here than we can deal with.
1500 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1501 if (!AddendExpr)
1502 return false;
1503
1504 Addend = AddendExpr->getValue();
1505 if (BE->getOpcode() == MCBinaryExpr::Sub)
1506 Addend = -Addend;
1507
1508 // It's some symbol reference + a constant addend
1509 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1510}
1511
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001512bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1513 // This returns false if this function recognizes the directive
1514 // regardless of whether it is successfully handles or reports an
1515 // error. Otherwise it returns true to give the generic parser a
1516 // chance at recognizing it.
1517 StringRef IDVal = DirectiveID.getString();
1518
1519 if (IDVal == ".option")
1520 return parseDirectiveOption();
1521
1522 return true;
1523}
1524
1525bool RISCVAsmParser::parseDirectiveOption() {
1526 MCAsmParser &Parser = getParser();
1527 // Get the option token.
1528 AsmToken Tok = Parser.getTok();
1529 // At the moment only identifiers are supported.
1530 if (Tok.isNot(AsmToken::Identifier))
1531 return Error(Parser.getTok().getLoc(),
1532 "unexpected token, expected identifier");
1533
1534 StringRef Option = Tok.getIdentifier();
1535
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001536 if (Option == "push") {
1537 getTargetStreamer().emitDirectiveOptionPush();
1538
1539 Parser.Lex();
1540 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1541 return Error(Parser.getTok().getLoc(),
1542 "unexpected token, expected end of statement");
1543
1544 pushFeatureBits();
1545 return false;
1546 }
1547
1548 if (Option == "pop") {
1549 SMLoc StartLoc = Parser.getTok().getLoc();
1550 getTargetStreamer().emitDirectiveOptionPop();
1551
1552 Parser.Lex();
1553 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1554 return Error(Parser.getTok().getLoc(),
1555 "unexpected token, expected end of statement");
1556
1557 if (popFeatureBits())
1558 return Error(StartLoc, ".option pop with no .option push");
1559
1560 return false;
1561 }
1562
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001563 if (Option == "rvc") {
1564 getTargetStreamer().emitDirectiveOptionRVC();
1565
1566 Parser.Lex();
1567 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1568 return Error(Parser.getTok().getLoc(),
1569 "unexpected token, expected end of statement");
1570
1571 setFeatureBits(RISCV::FeatureStdExtC, "c");
1572 return false;
1573 }
1574
1575 if (Option == "norvc") {
1576 getTargetStreamer().emitDirectiveOptionNoRVC();
1577
1578 Parser.Lex();
1579 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1580 return Error(Parser.getTok().getLoc(),
1581 "unexpected token, expected end of statement");
1582
1583 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1584 return false;
1585 }
1586
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001587 if (Option == "relax") {
1588 getTargetStreamer().emitDirectiveOptionRelax();
1589
1590 Parser.Lex();
1591 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1592 return Error(Parser.getTok().getLoc(),
1593 "unexpected token, expected end of statement");
1594
1595 setFeatureBits(RISCV::FeatureRelax, "relax");
1596 return false;
1597 }
1598
1599 if (Option == "norelax") {
1600 getTargetStreamer().emitDirectiveOptionNoRelax();
1601
1602 Parser.Lex();
1603 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1604 return Error(Parser.getTok().getLoc(),
1605 "unexpected token, expected end of statement");
1606
1607 clearFeatureBits(RISCV::FeatureRelax, "relax");
1608 return false;
1609 }
1610
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001611 // Unknown option.
1612 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001613 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1614 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001615 Parser.eatToEndOfStatement();
1616 return false;
1617}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001618
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001619void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1620 MCInst CInst;
1621 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
1622 CInst.setLoc(Inst.getLoc());
1623 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1624}
1625
Luis Marquesfa06e952019-08-16 14:27:50 +00001626void RISCVAsmParser::emitLoadImm(Register DestReg, int64_t Value,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001627 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001628 RISCVMatInt::InstSeq Seq;
1629 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001630
Luis Marquesfa06e952019-08-16 14:27:50 +00001631 Register SrcReg = RISCV::X0;
Alex Bradbury22c091f2018-11-15 10:11:31 +00001632 for (RISCVMatInt::Inst &Inst : Seq) {
1633 if (Inst.Opc == RISCV::LUI) {
1634 emitToStreamer(
1635 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1636 } else {
1637 emitToStreamer(
1638 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1639 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001640 }
1641
Alex Bradbury22c091f2018-11-15 10:11:31 +00001642 // Only the first instruction has X0 as its source.
1643 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001644 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001645}
1646
Alex Bradbury22531c42019-02-15 09:53:32 +00001647void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
1648 const MCExpr *Symbol,
1649 RISCVMCExpr::VariantKind VKHi,
1650 unsigned SecondOpcode, SMLoc IDLoc,
1651 MCStreamer &Out) {
1652 // A pair of instructions for PC-relative addressing; expands to
1653 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
1654 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001655 MCContext &Ctx = getContext();
1656
1657 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1658 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1659 Out.EmitLabel(TmpLabel);
1660
Alex Bradbury22531c42019-02-15 09:53:32 +00001661 const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001662 emitToStreamer(
Alex Bradbury22531c42019-02-15 09:53:32 +00001663 Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001664
1665 const MCExpr *RefToLinkTmpLabel =
1666 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1667 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1668
Alex Bradbury22531c42019-02-15 09:53:32 +00001669 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001670 .addOperand(DestReg)
Alex Bradbury22531c42019-02-15 09:53:32 +00001671 .addOperand(TmpReg)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001672 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001673}
1674
Alex Bradbury22531c42019-02-15 09:53:32 +00001675void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1676 MCStreamer &Out) {
1677 // The load local address pseudo-instruction "lla" is used in PC-relative
1678 // addressing of local symbols:
1679 // lla rdest, symbol
1680 // expands to
1681 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1682 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1683 MCOperand DestReg = Inst.getOperand(0);
1684 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1685 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1686 RISCV::ADDI, IDLoc, Out);
1687}
1688
1689void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
1690 MCStreamer &Out) {
1691 // The load address pseudo-instruction "la" is used in PC-relative and
1692 // GOT-indirect addressing of global symbols:
1693 // la rdest, symbol
1694 // expands to either (for non-PIC)
1695 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1696 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1697 // or (for PIC)
1698 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
1699 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1700 MCOperand DestReg = Inst.getOperand(0);
1701 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1702 unsigned SecondOpcode;
1703 RISCVMCExpr::VariantKind VKHi;
1704 // FIXME: Should check .option (no)pic when implemented
1705 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1706 SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1707 VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
1708 } else {
1709 SecondOpcode = RISCV::ADDI;
1710 VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
1711 }
1712 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1713}
1714
Lewis Revill74927552019-05-23 14:46:27 +00001715void RISCVAsmParser::emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc,
1716 MCStreamer &Out) {
1717 // The load TLS IE address pseudo-instruction "la.tls.ie" is used in
1718 // initial-exec TLS model addressing of global symbols:
1719 // la.tls.ie rdest, symbol
1720 // expands to
1721 // TmpLabel: AUIPC rdest, %tls_ie_pcrel_hi(symbol)
1722 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1723 MCOperand DestReg = Inst.getOperand(0);
1724 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1725 unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1726 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GOT_HI,
1727 SecondOpcode, IDLoc, Out);
1728}
1729
1730void RISCVAsmParser::emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc,
1731 MCStreamer &Out) {
1732 // The load TLS GD address pseudo-instruction "la.tls.gd" is used in
1733 // global-dynamic TLS model addressing of global symbols:
1734 // la.tls.gd rdest, symbol
1735 // expands to
1736 // TmpLabel: AUIPC rdest, %tls_gd_pcrel_hi(symbol)
1737 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1738 MCOperand DestReg = Inst.getOperand(0);
1739 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1740 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GD_HI,
1741 RISCV::ADDI, IDLoc, Out);
1742}
1743
Kito Cheng303217e2019-02-20 03:31:32 +00001744void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
1745 SMLoc IDLoc, MCStreamer &Out,
1746 bool HasTmpReg) {
1747 // The load/store pseudo-instruction does a pc-relative load with
1748 // a symbol.
1749 //
1750 // The expansion looks like this
1751 //
1752 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
1753 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
1754 MCOperand DestReg = Inst.getOperand(0);
1755 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1756 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1757 MCOperand TmpReg = Inst.getOperand(TmpRegOpIdx);
1758 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
1759 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1760 Opcode, IDLoc, Out);
1761}
1762
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001763bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
1764 OperandVector &Operands) {
1765 assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
1766 assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
1767 if (Inst.getOperand(2).getReg() != RISCV::X4) {
1768 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
1769 return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
1770 "%tprel_add modifier");
1771 }
1772
1773 return false;
1774}
1775
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001776bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001777 OperandVector &Operands,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001778 MCStreamer &Out) {
1779 Inst.setLoc(IDLoc);
1780
Alex Bradbury22531c42019-02-15 09:53:32 +00001781 switch (Inst.getOpcode()) {
1782 default:
1783 break;
1784 case RISCV::PseudoLI: {
Luis Marquesfa06e952019-08-16 14:27:50 +00001785 Register Reg = Inst.getOperand(0).getReg();
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001786 const MCOperand &Op1 = Inst.getOperand(1);
1787 if (Op1.isExpr()) {
1788 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1789 // Just convert to an addi. This allows compatibility with gas.
1790 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1791 .addReg(Reg)
1792 .addReg(RISCV::X0)
1793 .addExpr(Op1.getExpr()));
1794 return false;
1795 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001796 int64_t Imm = Inst.getOperand(1).getImm();
1797 // On RV32 the immediate here can either be a signed or an unsigned
1798 // 32-bit number. Sign extension has to be performed to ensure that Imm
1799 // represents the expected signed 64-bit number.
1800 if (!isRV64())
1801 Imm = SignExtend64<32>(Imm);
1802 emitLoadImm(Reg, Imm, Out);
1803 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001804 }
1805 case RISCV::PseudoLLA:
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001806 emitLoadLocalAddress(Inst, IDLoc, Out);
1807 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001808 case RISCV::PseudoLA:
1809 emitLoadAddress(Inst, IDLoc, Out);
1810 return false;
Lewis Revill74927552019-05-23 14:46:27 +00001811 case RISCV::PseudoLA_TLS_IE:
1812 emitLoadTLSIEAddress(Inst, IDLoc, Out);
1813 return false;
1814 case RISCV::PseudoLA_TLS_GD:
1815 emitLoadTLSGDAddress(Inst, IDLoc, Out);
1816 return false;
Kito Cheng303217e2019-02-20 03:31:32 +00001817 case RISCV::PseudoLB:
1818 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
1819 return false;
1820 case RISCV::PseudoLBU:
1821 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
1822 return false;
1823 case RISCV::PseudoLH:
1824 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
1825 return false;
1826 case RISCV::PseudoLHU:
1827 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
1828 return false;
1829 case RISCV::PseudoLW:
1830 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
1831 return false;
1832 case RISCV::PseudoLWU:
1833 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
1834 return false;
1835 case RISCV::PseudoLD:
1836 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
1837 return false;
1838 case RISCV::PseudoFLW:
1839 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
1840 return false;
1841 case RISCV::PseudoFLD:
1842 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
1843 return false;
1844 case RISCV::PseudoSB:
1845 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
1846 return false;
1847 case RISCV::PseudoSH:
1848 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
1849 return false;
1850 case RISCV::PseudoSW:
1851 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
1852 return false;
1853 case RISCV::PseudoSD:
1854 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
1855 return false;
1856 case RISCV::PseudoFSW:
1857 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
1858 return false;
1859 case RISCV::PseudoFSD:
1860 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
1861 return false;
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001862 case RISCV::PseudoAddTPRel:
1863 if (checkPseudoAddTPRel(Inst, Operands))
1864 return true;
Fangrui Song92e78b72019-07-01 11:41:07 +00001865 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001866 }
1867
1868 emitToStreamer(Out, Inst);
1869 return false;
1870}
1871
Tom Stellard4b0b2612019-06-11 03:21:13 +00001872extern "C" void LLVMInitializeRISCVAsmParser() {
Alex Bradbury04f06d92017-08-08 14:43:36 +00001873 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1874 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1875}