blob: fe0455fb22286edb1b95373452ee71fdb2fa361f [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
James Clarke0113cf12020-01-13 00:50:37 +0000261 bool isGPR() const {
262 return Kind == KindTy::Register &&
263 RISCVMCRegisterClasses[RISCV::GPRRegClassID].contains(Reg.RegNum);
264 }
265
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000266 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
267 RISCVMCExpr::VariantKind &VK) {
268 if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000269 VK = RE->getKind();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000270 return RE->evaluateAsConstant(Imm);
271 }
272
273 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000274 VK = RISCVMCExpr::VK_RISCV_None;
275 Imm = CE->getValue();
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000276 return true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000277 }
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000278
279 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000280 }
281
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000282 // True if operand is a symbol with no modifiers, or a constant with no
283 // modifiers and isShiftedInt<N-1, 1>(Op).
284 template <int N> bool isBareSimmNLsb0() const {
285 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000286 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000287 if (!isImm())
288 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000289 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000290 bool IsValid;
291 if (!IsConstantImm)
292 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
293 else
294 IsValid = isShiftedInt<N - 1, 1>(Imm);
295 return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000296 }
297
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000298 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
299
Shiva Chen98f93892018-04-25 14:18:55 +0000300 bool isBareSymbol() const {
301 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000302 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Shiva Chen98f93892018-04-25 14:18:55 +0000303 // Must be of 'immediate' type but not a constant.
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000304 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
Shiva Chen98f93892018-04-25 14:18:55 +0000305 return false;
306 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
307 VK == RISCVMCExpr::VK_RISCV_None;
308 }
309
Alex Bradbury44668ae2019-04-01 14:53:17 +0000310 bool isCallSymbol() const {
311 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000312 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury44668ae2019-04-01 14:53:17 +0000313 // Must be of 'immediate' type but not a constant.
314 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
315 return false;
316 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
Alex Bradburyf8078f62019-04-02 12:47:20 +0000317 (VK == RISCVMCExpr::VK_RISCV_CALL ||
318 VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
Alex Bradbury44668ae2019-04-01 14:53:17 +0000319 }
320
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000321 bool isTPRelAddSymbol() const {
322 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000323 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000324 // Must be of 'immediate' type but not a constant.
325 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
326 return false;
327 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
328 VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
329 }
330
Ana Pazos9d6c5532018-10-04 21:50:54 +0000331 bool isCSRSystemRegister() const { return isSystemRegister(); }
332
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000333 /// Return true if the operand is a valid for the fence instruction e.g.
334 /// ('iorw').
335 bool isFenceArg() const {
336 if (!isImm())
337 return false;
338 const MCExpr *Val = getImm();
339 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
340 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
341 return false;
342
343 StringRef Str = SVal->getSymbol().getName();
344 // Letters must be unique, taken from 'iorw', and in ascending order. This
345 // holds as long as each individual character is one of 'iorw' and is
346 // greater than the previous character.
347 char Prev = '\0';
348 for (char c : Str) {
349 if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
350 return false;
351 if (c <= Prev)
352 return false;
353 Prev = c;
354 }
355 return true;
356 }
357
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000358 /// Return true if the operand is a valid floating point rounding mode.
359 bool isFRMArg() const {
360 if (!isImm())
361 return false;
362 const MCExpr *Val = getImm();
363 auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
364 if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
365 return false;
366
367 StringRef Str = SVal->getSymbol().getName();
368
369 return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
370 }
371
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000372 bool isImmXLenLI() const {
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000373 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000374 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000375 if (!isImm())
376 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000377 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000378 if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
379 return true;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000380 // Given only Imm, ensuring that the actually specified constant is either
381 // a signed or unsigned 64-bit number is unfortunately impossible.
Luis Marques2d0cd6c2019-09-20 13:48:02 +0000382 return IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None &&
383 (isRV64() || (isInt<32>(Imm) || isUInt<32>(Imm)));
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000384 }
385
Alex Bradburya6e62482017-12-07 10:53:48 +0000386 bool isUImmLog2XLen() const {
387 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000388 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradburya6e62482017-12-07 10:53:48 +0000389 if (!isImm())
390 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000391 if (!evaluateConstantImm(getImm(), Imm, VK) ||
392 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradburya6e62482017-12-07 10:53:48 +0000393 return false;
394 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
395 }
396
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000397 bool isUImmLog2XLenNonZero() const {
398 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000399 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000400 if (!isImm())
401 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000402 if (!evaluateConstantImm(getImm(), Imm, VK) ||
403 VK != RISCVMCExpr::VK_RISCV_None)
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000404 return false;
405 if (Imm == 0)
406 return false;
407 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
408 }
409
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000410 bool isUImm5() const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000411 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000412 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000413 if (!isImm())
414 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000415 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000416 return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000417 }
418
Alex Bradbury60714f92017-12-13 09:32:55 +0000419 bool isUImm5NonZero() const {
420 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000421 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000422 if (!isImm())
423 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000424 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000425 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
426 VK == RISCVMCExpr::VK_RISCV_None;
427 }
428
Alex Bradbury581d6b02017-12-13 09:41:21 +0000429 bool isSImm6() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000430 if (!isImm())
431 return false;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000432 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000433 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000434 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000435 return IsConstantImm && isInt<6>(Imm) &&
436 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury581d6b02017-12-13 09:41:21 +0000437 }
438
Shiva Chenb22c1d22018-02-02 02:43:23 +0000439 bool isSImm6NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000440 if (!isImm())
441 return false;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000442 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000443 int64_t Imm;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000444 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Ana Pazos065b0882018-09-13 18:37:23 +0000445 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
446 VK == RISCVMCExpr::VK_RISCV_None;
Shiva Chenb22c1d22018-02-02 02:43:23 +0000447 }
448
Shiva Chen7c172422018-02-22 15:02:28 +0000449 bool isCLUIImm() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000450 if (!isImm())
451 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000452 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000453 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000454 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chen7c172422018-02-22 15:02:28 +0000455 return IsConstantImm && (Imm != 0) &&
456 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
Ana Pazos9d6c5532018-10-04 21:50:54 +0000457 VK == RISCVMCExpr::VK_RISCV_None;
Alex Bradbury60714f92017-12-13 09:32:55 +0000458 }
459
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000460 bool isUImm7Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000461 if (!isImm())
462 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000463 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000464 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000465 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000466 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
467 VK == RISCVMCExpr::VK_RISCV_None;
468 }
469
470 bool isUImm8Lsb00() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000471 if (!isImm())
472 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000473 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000474 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000475 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000476 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
477 VK == RISCVMCExpr::VK_RISCV_None;
478 }
479
480 bool isUImm8Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000481 if (!isImm())
482 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000483 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000484 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000485 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000486 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
487 VK == RISCVMCExpr::VK_RISCV_None;
488 }
489
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000490 bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
491
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000492 bool isUImm9Lsb000() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000493 if (!isImm())
494 return false;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000495 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000496 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000497 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000498 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
499 VK == RISCVMCExpr::VK_RISCV_None;
500 }
501
Alex Bradbury60714f92017-12-13 09:32:55 +0000502 bool isUImm10Lsb00NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000503 if (!isImm())
504 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000505 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000506 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000507 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury60714f92017-12-13 09:32:55 +0000508 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
509 VK == RISCVMCExpr::VK_RISCV_None;
510 }
511
Alex Bradbury04f06d92017-08-08 14:43:36 +0000512 bool isSImm12() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000513 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000514 int64_t Imm;
515 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000516 if (!isImm())
517 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000518 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000519 if (!IsConstantImm)
520 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
521 else
522 IsValid = isInt<12>(Imm);
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000523 return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
Ahmed Charles646ab872018-02-06 00:55:23 +0000524 VK == RISCVMCExpr::VK_RISCV_LO ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000525 VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
526 VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000527 }
528
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000529 bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
530
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000531 bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000532
Shiva Chenb22c1d22018-02-02 02:43:23 +0000533 bool isSImm10Lsb0000NonZero() const {
Ana Pazosecc65ed2018-08-24 23:47:49 +0000534 if (!isImm())
535 return false;
Alex Bradbury60714f92017-12-13 09:32:55 +0000536 int64_t Imm;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000537 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000538 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000539 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
Alex Bradbury60714f92017-12-13 09:32:55 +0000540 VK == RISCVMCExpr::VK_RISCV_None;
541 }
542
Alex Bradbury74340f12018-09-18 15:08:35 +0000543 bool isUImm20LUI() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000544 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000545 int64_t Imm;
546 bool IsValid;
Alex Bradbury3c941e72017-10-19 16:22:51 +0000547 if (!isImm())
548 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000549 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000550 if (!IsConstantImm) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000551 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000552 return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
553 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000554 } else {
555 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000556 VK == RISCVMCExpr::VK_RISCV_HI ||
557 VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000558 }
559 }
560
561 bool isUImm20AUIPC() const {
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000562 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury74340f12018-09-18 15:08:35 +0000563 int64_t Imm;
564 bool IsValid;
565 if (!isImm())
566 return false;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000567 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
Alex Bradbury74340f12018-09-18 15:08:35 +0000568 if (!IsConstantImm) {
569 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000570 return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000571 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
572 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
573 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000574 } else {
575 return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000576 VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
Lewis Revilldf3cb472019-04-23 14:46:13 +0000577 VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
578 VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
579 VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
Alex Bradbury74340f12018-09-18 15:08:35 +0000580 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000581 }
582
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000583 bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000584
Sam Elliottf596f452019-08-01 12:42:31 +0000585 bool isImmZero() const {
586 if (!isImm())
587 return false;
588 int64_t Imm;
589 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
590 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
591 return IsConstantImm && (Imm == 0) && VK == RISCVMCExpr::VK_RISCV_None;
592 }
593
Alex Bradbury04f06d92017-08-08 14:43:36 +0000594 /// getStartLoc - Gets location of the first token of this operand
595 SMLoc getStartLoc() const override { return StartLoc; }
596 /// getEndLoc - Gets location of the last token of this operand
597 SMLoc getEndLoc() const override { return EndLoc; }
Alex Bradburya6e62482017-12-07 10:53:48 +0000598 /// True if this operand is for an RV64 instruction
599 bool isRV64() const { return IsRV64; }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000600
601 unsigned getReg() const override {
Luis Marquesfa06e952019-08-16 14:27:50 +0000602 assert(Kind == KindTy::Register && "Invalid type access!");
603 return Reg.RegNum.id();
Alex Bradbury04f06d92017-08-08 14:43:36 +0000604 }
605
Ana Pazos9d6c5532018-10-04 21:50:54 +0000606 StringRef getSysReg() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000607 assert(Kind == KindTy::SystemRegister && "Invalid access!");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000608 return StringRef(SysReg.Data, SysReg.Length);
609 }
610
Alex Bradbury04f06d92017-08-08 14:43:36 +0000611 const MCExpr *getImm() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000612 assert(Kind == KindTy::Immediate && "Invalid type access!");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000613 return Imm.Val;
614 }
615
616 StringRef getToken() const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000617 assert(Kind == KindTy::Token && "Invalid type access!");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000618 return Tok;
619 }
620
621 void print(raw_ostream &OS) const override {
622 switch (Kind) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000623 case KindTy::Immediate:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000624 OS << *getImm();
625 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000626 case KindTy::Register:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000627 OS << "<register x";
628 OS << getReg() << ">";
629 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000630 case KindTy::Token:
Alex Bradbury04f06d92017-08-08 14:43:36 +0000631 OS << "'" << getToken() << "'";
632 break;
Luis Marquesfa06e952019-08-16 14:27:50 +0000633 case KindTy::SystemRegister:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000634 OS << "<sysreg: " << getSysReg() << '>';
635 break;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000636 }
637 }
638
Alex Bradburya6e62482017-12-07 10:53:48 +0000639 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
640 bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000641 auto Op = std::make_unique<RISCVOperand>(KindTy::Token);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000642 Op->Tok = Str;
643 Op->StartLoc = S;
644 Op->EndLoc = S;
Alex Bradburya6e62482017-12-07 10:53:48 +0000645 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000646 return Op;
647 }
648
649 static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000650 SMLoc E, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000651 auto Op = std::make_unique<RISCVOperand>(KindTy::Register);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000652 Op->Reg.RegNum = RegNo;
653 Op->StartLoc = S;
654 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000655 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000656 return Op;
657 }
658
659 static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
Alex Bradburya6e62482017-12-07 10:53:48 +0000660 SMLoc E, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000661 auto Op = std::make_unique<RISCVOperand>(KindTy::Immediate);
Alex Bradbury04f06d92017-08-08 14:43:36 +0000662 Op->Imm.Val = Val;
663 Op->StartLoc = S;
664 Op->EndLoc = E;
Alex Bradburya6e62482017-12-07 10:53:48 +0000665 Op->IsRV64 = IsRV64;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000666 return Op;
667 }
668
Ana Pazos9d6c5532018-10-04 21:50:54 +0000669 static std::unique_ptr<RISCVOperand>
670 createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000671 auto Op = std::make_unique<RISCVOperand>(KindTy::SystemRegister);
Ana Pazos9d6c5532018-10-04 21:50:54 +0000672 Op->SysReg.Data = Str.data();
673 Op->SysReg.Length = Str.size();
674 Op->SysReg.Encoding = Encoding;
675 Op->StartLoc = S;
676 Op->IsRV64 = IsRV64;
677 return Op;
678 }
679
Alex Bradbury04f06d92017-08-08 14:43:36 +0000680 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
681 assert(Expr && "Expr shouldn't be null!");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000682 int64_t Imm = 0;
Francis Visoiu Mistrihd42289e2019-07-29 15:52:13 +0000683 RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
Alex Bradbury96ed75d2018-09-20 11:40:43 +0000684 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000685
686 if (IsConstant)
687 Inst.addOperand(MCOperand::createImm(Imm));
Alex Bradbury04f06d92017-08-08 14:43:36 +0000688 else
689 Inst.addOperand(MCOperand::createExpr(Expr));
690 }
691
692 // Used by the TableGen Code
693 void addRegOperands(MCInst &Inst, unsigned N) const {
694 assert(N == 1 && "Invalid number of operands!");
695 Inst.addOperand(MCOperand::createReg(getReg()));
696 }
697
698 void addImmOperands(MCInst &Inst, unsigned N) const {
699 assert(N == 1 && "Invalid number of operands!");
700 addExpr(Inst, getImm());
701 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000702
703 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
704 assert(N == 1 && "Invalid number of operands!");
705 // isFenceArg has validated the operand, meaning this cast is safe
706 auto SE = cast<MCSymbolRefExpr>(getImm());
707
708 unsigned Imm = 0;
709 for (char c : SE->getSymbol().getName()) {
710 switch (c) {
Ana Pazos9d6c5532018-10-04 21:50:54 +0000711 default:
712 llvm_unreachable("FenceArg must contain only [iorw]");
713 case 'i': Imm |= RISCVFenceField::I; break;
714 case 'o': Imm |= RISCVFenceField::O; break;
715 case 'r': Imm |= RISCVFenceField::R; break;
716 case 'w': Imm |= RISCVFenceField::W; break;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000717 }
718 }
719 Inst.addOperand(MCOperand::createImm(Imm));
720 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000721
Ana Pazos9d6c5532018-10-04 21:50:54 +0000722 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
723 assert(N == 1 && "Invalid number of operands!");
724 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
725 }
726
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000727 // Returns the rounding mode represented by this RISCVOperand. Should only
728 // be called after checking isFRMArg.
729 RISCVFPRndMode::RoundingMode getRoundingMode() const {
730 // isFRMArg has validated the operand, meaning this cast is safe.
731 auto SE = cast<MCSymbolRefExpr>(getImm());
732 RISCVFPRndMode::RoundingMode FRM =
733 RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
734 assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
735 return FRM;
736 }
737
738 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
739 assert(N == 1 && "Invalid number of operands!");
740 Inst.addOperand(MCOperand::createImm(getRoundingMode()));
741 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000742};
743} // end anonymous namespace.
744
745#define GET_REGISTER_MATCHER
Simon Cooka6e50e42019-12-10 16:44:48 +0000746#define GET_SUBTARGET_FEATURE_NAME
Alex Bradbury04f06d92017-08-08 14:43:36 +0000747#define GET_MATCHER_IMPLEMENTATION
Simon Cookeedb9642019-11-18 10:58:00 +0000748#define GET_MNEMONIC_SPELL_CHECKER
Alex Bradbury04f06d92017-08-08 14:43:36 +0000749#include "RISCVGenAsmMatcher.inc"
750
Luis Marquesaae97bf2019-09-27 15:49:10 +0000751static Register convertFPR64ToFPR32(Register Reg) {
752 assert(Reg >= RISCV::F0_D && Reg <= RISCV::F31_D && "Invalid register");
753 return Reg - RISCV::F0_D + RISCV::F0_F;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000754}
755
756unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
757 unsigned Kind) {
758 RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
759 if (!Op.isReg())
760 return Match_InvalidOperand;
761
Luis Marquesfa06e952019-08-16 14:27:50 +0000762 Register Reg = Op.getReg();
Luis Marquesaae97bf2019-09-27 15:49:10 +0000763 bool IsRegFPR64 =
764 RISCVMCRegisterClasses[RISCV::FPR64RegClassID].contains(Reg);
765 bool IsRegFPR64C =
766 RISCVMCRegisterClasses[RISCV::FPR64CRegClassID].contains(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000767
768 // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
Luis Marquesaae97bf2019-09-27 15:49:10 +0000769 // register from FPR64 to FPR32 or FPR64C to FPR32C if necessary.
770 if ((IsRegFPR64 && Kind == MCK_FPR32) ||
771 (IsRegFPR64C && Kind == MCK_FPR32C)) {
772 Op.Reg.RegNum = convertFPR64ToFPR32(Reg);
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000773 return Match_Success;
774 }
775 return Match_InvalidOperand;
776}
777
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000778bool RISCVAsmParser::generateImmOutOfRangeError(
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000779 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000780 Twine Msg = "immediate must be an integer in the range") {
781 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
782 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
783}
784
Simon Cookeedb9642019-11-18 10:58:00 +0000785static std::string RISCVMnemonicSpellCheck(StringRef S,
786 const FeatureBitset &FBS,
787 unsigned VariantID = 0);
788
Alex Bradbury04f06d92017-08-08 14:43:36 +0000789bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
790 OperandVector &Operands,
791 MCStreamer &Out,
792 uint64_t &ErrorInfo,
793 bool MatchingInlineAsm) {
794 MCInst Inst;
Simon Cooka6e50e42019-12-10 16:44:48 +0000795 FeatureBitset MissingFeatures;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000796
Ana Pazos6b34051b2018-08-30 19:43:19 +0000797 auto Result =
Simon Cooka6e50e42019-12-10 16:44:48 +0000798 MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,
799 MatchingInlineAsm);
Ana Pazos6b34051b2018-08-30 19:43:19 +0000800 switch (Result) {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000801 default:
802 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000803 case Match_Success:
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +0000804 return processInstruction(Inst, IDLoc, Operands, Out);
Simon Cooka6e50e42019-12-10 16:44:48 +0000805 case Match_MissingFeature: {
806 assert(MissingFeatures.any() && "Unknown missing features!");
807 bool FirstFeature = true;
808 std::string Msg = "instruction requires the following:";
809 for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i) {
810 if (MissingFeatures[i]) {
811 Msg += FirstFeature ? " " : ", ";
812 Msg += getSubtargetFeatureName(i);
813 FirstFeature = false;
814 }
815 }
816 return Error(IDLoc, Msg);
817 }
Simon Cookeedb9642019-11-18 10:58:00 +0000818 case Match_MnemonicFail: {
819 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
820 std::string Suggestion = RISCVMnemonicSpellCheck(
821 ((RISCVOperand &)*Operands[0]).getToken(), FBS);
822 return Error(IDLoc, "unrecognized instruction mnemonic" + Suggestion);
823 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000824 case Match_InvalidOperand: {
825 SMLoc ErrorLoc = IDLoc;
Alex Bradbury04f06d92017-08-08 14:43:36 +0000826 if (ErrorInfo != ~0U) {
827 if (ErrorInfo >= Operands.size())
828 return Error(ErrorLoc, "too few operands for instruction");
829
830 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
831 if (ErrorLoc == SMLoc())
832 ErrorLoc = IDLoc;
833 }
834 return Error(ErrorLoc, "invalid operand for instruction");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000835 }
Ana Pazos6b34051b2018-08-30 19:43:19 +0000836 }
837
838 // Handle the case when the error message is of specific type
839 // other than the generic Match_InvalidOperand, and the
840 // corresponding operand is missing.
841 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
842 SMLoc ErrorLoc = IDLoc;
843 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
844 return Error(ErrorLoc, "too few operands for instruction");
845 }
846
847 switch(Result) {
848 default:
849 break;
Alex Bradbury2ba76be2019-01-03 14:41:41 +0000850 case Match_InvalidImmXLenLI:
Alex Bradbury6a4b5442018-06-07 15:35:47 +0000851 if (isRV64()) {
852 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
853 return Error(ErrorLoc, "operand must be a constant 64-bit integer");
854 }
855 return generateImmOutOfRangeError(Operands, ErrorInfo,
856 std::numeric_limits<int32_t>::min(),
857 std::numeric_limits<uint32_t>::max());
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000858 case Match_InvalidImmZero: {
859 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
860 return Error(ErrorLoc, "immediate must be zero");
861 }
Alex Bradburya6e62482017-12-07 10:53:48 +0000862 case Match_InvalidUImmLog2XLen:
863 if (isRV64())
864 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
865 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury0ad4c262017-12-15 10:20:51 +0000866 case Match_InvalidUImmLog2XLenNonZero:
867 if (isRV64())
868 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
869 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000870 case Match_InvalidUImm5:
871 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
Alex Bradbury581d6b02017-12-13 09:41:21 +0000872 case Match_InvalidSImm6:
873 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
874 (1 << 5) - 1);
Shiva Chenb22c1d22018-02-02 02:43:23 +0000875 case Match_InvalidSImm6NonZero:
Ana Pazos9d6c5532018-10-04 21:50:54 +0000876 return generateImmOutOfRangeError(
877 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000878 "immediate must be non-zero in the range");
Shiva Chen7c172422018-02-22 15:02:28 +0000879 case Match_InvalidCLUIImm:
880 return generateImmOutOfRangeError(
881 Operands, ErrorInfo, 1, (1 << 5) - 1,
882 "immediate must be in [0xfffe0, 0xfffff] or");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000883 case Match_InvalidUImm7Lsb00:
884 return generateImmOutOfRangeError(
885 Operands, ErrorInfo, 0, (1 << 7) - 4,
886 "immediate must be a multiple of 4 bytes in the range");
887 case Match_InvalidUImm8Lsb00:
888 return generateImmOutOfRangeError(
889 Operands, ErrorInfo, 0, (1 << 8) - 4,
890 "immediate must be a multiple of 4 bytes in the range");
891 case Match_InvalidUImm8Lsb000:
892 return generateImmOutOfRangeError(
893 Operands, ErrorInfo, 0, (1 << 8) - 8,
894 "immediate must be a multiple of 8 bytes in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000895 case Match_InvalidSImm9Lsb0:
896 return generateImmOutOfRangeError(
897 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
898 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000899 case Match_InvalidUImm9Lsb000:
900 return generateImmOutOfRangeError(
901 Operands, ErrorInfo, 0, (1 << 9) - 8,
902 "immediate must be a multiple of 8 bytes in the range");
Alex Bradbury60714f92017-12-13 09:32:55 +0000903 case Match_InvalidUImm10Lsb00NonZero:
904 return generateImmOutOfRangeError(
905 Operands, ErrorInfo, 4, (1 << 10) - 4,
906 "immediate must be a multiple of 4 bytes in the range");
Shiva Chenb22c1d22018-02-02 02:43:23 +0000907 case Match_InvalidSImm10Lsb0000NonZero:
Alex Bradbury60714f92017-12-13 09:32:55 +0000908 return generateImmOutOfRangeError(
909 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
Shiva Chenb22c1d22018-02-02 02:43:23 +0000910 "immediate must be a multiple of 16 bytes and non-zero in the range");
Alex Bradbury04f06d92017-08-08 14:43:36 +0000911 case Match_InvalidSImm12:
Alex Bradbury7d0e18d2018-09-18 15:13:29 +0000912 return generateImmOutOfRangeError(
913 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000914 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
915 "integer in the range");
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000916 case Match_InvalidSImm12Lsb0:
917 return generateImmOutOfRangeError(
918 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
919 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000920 case Match_InvalidSImm13Lsb0:
921 return generateImmOutOfRangeError(
922 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
923 "immediate must be a multiple of 2 bytes in the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000924 case Match_InvalidUImm20LUI:
925 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000926 "operand must be a symbol with "
927 "%hi/%tprel_hi modifier or an integer in "
928 "the range");
Alex Bradbury74340f12018-09-18 15:08:35 +0000929 case Match_InvalidUImm20AUIPC:
930 return generateImmOutOfRangeError(
931 Operands, ErrorInfo, 0, (1 << 20) - 1,
Lewis Revilldf3cb472019-04-23 14:46:13 +0000932 "operand must be a symbol with a "
933 "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi modifier or "
934 "an integer in the range");
Alex Bradbury226f3ef2018-09-20 08:10:35 +0000935 case Match_InvalidSImm21Lsb0JAL:
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000936 return generateImmOutOfRangeError(
937 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
938 "immediate must be a multiple of 2 bytes in the range");
Ana Pazos9d6c5532018-10-04 21:50:54 +0000939 case Match_InvalidCSRSystemRegister: {
940 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
941 "operand must be a valid system register "
942 "name or an integer in the range");
943 }
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000944 case Match_InvalidFenceArg: {
Alex Bradbury04f06d92017-08-08 14:43:36 +0000945 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000946 return Error(
947 ErrorLoc,
948 "operand must be formed of letters selected in-order from 'iorw'");
949 }
Alex Bradbury0d6cf902017-12-07 10:26:05 +0000950 case Match_InvalidFRMArg: {
951 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
952 return Error(
953 ErrorLoc,
954 "operand must be a valid floating point rounding mode mnemonic");
955 }
Shiva Chen98f93892018-04-25 14:18:55 +0000956 case Match_InvalidBareSymbol: {
957 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
958 return Error(ErrorLoc, "operand must be a bare symbol name");
959 }
Alex Bradbury44668ae2019-04-01 14:53:17 +0000960 case Match_InvalidCallSymbol: {
961 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
962 return Error(ErrorLoc, "operand must be a bare symbol name");
963 }
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000964 case Match_InvalidTPRelAddSymbol: {
965 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
966 return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
967 }
Alex Bradbury04f06d92017-08-08 14:43:36 +0000968 }
969
970 llvm_unreachable("Unknown match type detected!");
971}
972
Alex Bradbury99794792019-03-17 12:02:32 +0000973// Attempts to match Name as a register (either using the default name or
974// alternative ABI names), setting RegNo to the matching register. Upon
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000975// failure, returns true and sets RegNo to 0. If IsRV32E then registers
976// x16-x31 will be rejected.
Luis Marquesfa06e952019-08-16 14:27:50 +0000977static bool matchRegisterNameHelper(bool IsRV32E, Register &RegNo,
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000978 StringRef Name) {
Alex Bradbury99794792019-03-17 12:02:32 +0000979 RegNo = MatchRegisterName(Name);
Luis Marquesaae97bf2019-09-27 15:49:10 +0000980 // The 32- and 64-bit FPRs have the same asm name. Check that the initial
981 // match always matches the 64-bit variant, and not the 32-bit one.
982 assert(!(RegNo >= RISCV::F0_F && RegNo <= RISCV::F31_F));
983 // The default FPR register class is based on the tablegen enum ordering.
984 static_assert(RISCV::F0_D < RISCV::F0_F, "FPR matching must be updated");
Benjamin Kramer167b3022019-09-17 14:27:31 +0000985 if (RegNo == RISCV::NoRegister)
Alex Bradbury99794792019-03-17 12:02:32 +0000986 RegNo = MatchRegisterAltName(Name);
Alex Bradburydab1f6f2019-03-22 11:21:40 +0000987 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
Benjamin Kramer167b3022019-09-17 14:27:31 +0000988 RegNo = RISCV::NoRegister;
989 return RegNo == RISCV::NoRegister;
Alex Bradbury99794792019-03-17 12:02:32 +0000990}
991
Alex Bradbury04f06d92017-08-08 14:43:36 +0000992bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
993 SMLoc &EndLoc) {
994 const AsmToken &Tok = getParser().getTok();
995 StartLoc = Tok.getLoc();
996 EndLoc = Tok.getEndLoc();
997 RegNo = 0;
998 StringRef Name = getLexer().getTok().getIdentifier();
999
Luis Marquesfa06e952019-08-16 14:27:50 +00001000 if (matchRegisterNameHelper(isRV32E(), (Register&)RegNo, Name))
Alex Bradburyb18e3142019-03-17 12:00:58 +00001001 return Error(StartLoc, "invalid register name");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001002
Alex Bradburyb18e3142019-03-17 12:00:58 +00001003 getParser().Lex(); // Eat identifier token.
1004 return false;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001005}
1006
Alex Bradbury8c345c52017-11-09 15:00:03 +00001007OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
1008 bool AllowParens) {
1009 SMLoc FirstS = getLoc();
1010 bool HadParens = false;
Lewis Revill218aa0e2019-06-19 10:11:13 +00001011 AsmToken LParen;
Alex Bradbury8c345c52017-11-09 15:00:03 +00001012
Lewis Revill218aa0e2019-06-19 10:11:13 +00001013 // If this is an LParen and a parenthesised register name is allowed, parse it
1014 // atomically.
Alex Bradbury8c345c52017-11-09 15:00:03 +00001015 if (AllowParens && getLexer().is(AsmToken::LParen)) {
Lewis Revill218aa0e2019-06-19 10:11:13 +00001016 AsmToken Buf[2];
Alex Bradbury8c345c52017-11-09 15:00:03 +00001017 size_t ReadCount = getLexer().peekTokens(Buf);
1018 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
1019 HadParens = true;
Lewis Revill218aa0e2019-06-19 10:11:13 +00001020 LParen = getParser().getTok();
Alex Bradbury8c345c52017-11-09 15:00:03 +00001021 getParser().Lex(); // Eat '('
1022 }
1023 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001024
1025 switch (getLexer().getKind()) {
1026 default:
Lewis Revill218aa0e2019-06-19 10:11:13 +00001027 if (HadParens)
1028 getLexer().UnLex(LParen);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001029 return MatchOperand_NoMatch;
1030 case AsmToken::Identifier:
1031 StringRef Name = getLexer().getTok().getIdentifier();
Luis Marquesfa06e952019-08-16 14:27:50 +00001032 Register RegNo;
Alex Bradburydab1f6f2019-03-22 11:21:40 +00001033 matchRegisterNameHelper(isRV32E(), RegNo, Name);
Alex Bradbury99794792019-03-17 12:02:32 +00001034
Benjamin Kramer167b3022019-09-17 14:27:31 +00001035 if (RegNo == RISCV::NoRegister) {
Alex Bradbury99794792019-03-17 12:02:32 +00001036 if (HadParens)
Lewis Revill218aa0e2019-06-19 10:11:13 +00001037 getLexer().UnLex(LParen);
Alex Bradbury99794792019-03-17 12:02:32 +00001038 return MatchOperand_NoMatch;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001039 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001040 if (HadParens)
Alex Bradburya6e62482017-12-07 10:53:48 +00001041 Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001042 SMLoc S = getLoc();
1043 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
Alex Bradbury04f06d92017-08-08 14:43:36 +00001044 getLexer().Lex();
Alex Bradburya6e62482017-12-07 10:53:48 +00001045 Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001046 }
Alex Bradbury8c345c52017-11-09 15:00:03 +00001047
1048 if (HadParens) {
1049 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001050 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury8c345c52017-11-09 15:00:03 +00001051 }
1052
Alex Bradbury04f06d92017-08-08 14:43:36 +00001053 return MatchOperand_Success;
1054}
1055
Ana Pazos9d6c5532018-10-04 21:50:54 +00001056OperandMatchResultTy
1057RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1058 SMLoc S = getLoc();
1059 const MCExpr *Res;
1060
1061 switch (getLexer().getKind()) {
1062 default:
1063 return MatchOperand_NoMatch;
1064 case AsmToken::LParen:
1065 case AsmToken::Minus:
1066 case AsmToken::Plus:
Lewis Revill18737e82019-06-19 10:27:24 +00001067 case AsmToken::Exclaim:
1068 case AsmToken::Tilde:
Ana Pazos9d6c5532018-10-04 21:50:54 +00001069 case AsmToken::Integer:
1070 case AsmToken::String: {
1071 if (getParser().parseExpression(Res))
1072 return MatchOperand_ParseFail;
1073
1074 auto *CE = dyn_cast<MCConstantExpr>(Res);
1075 if (CE) {
1076 int64_t Imm = CE->getValue();
1077 if (isUInt<12>(Imm)) {
1078 auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1079 // Accept an immediate representing a named or un-named Sys Reg
1080 // if the range is valid, regardless of the required features.
1081 Operands.push_back(RISCVOperand::createSysReg(
1082 SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1083 return MatchOperand_Success;
1084 }
1085 }
1086
1087 Twine Msg = "immediate must be an integer in the range";
1088 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1089 return MatchOperand_ParseFail;
1090 }
1091 case AsmToken::Identifier: {
1092 StringRef Identifier;
1093 if (getParser().parseIdentifier(Identifier))
1094 return MatchOperand_ParseFail;
1095
1096 auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1097 // Accept a named Sys Reg if the required features are present.
1098 if (SysReg) {
1099 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1100 Error(S, "system register use requires an option to be enabled");
1101 return MatchOperand_ParseFail;
1102 }
1103 Operands.push_back(RISCVOperand::createSysReg(
1104 Identifier, S, SysReg->Encoding, isRV64()));
1105 return MatchOperand_Success;
1106 }
1107
1108 Twine Msg = "operand must be a valid system register name "
1109 "or an integer in the range";
1110 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1111 return MatchOperand_ParseFail;
1112 }
1113 case AsmToken::Percent: {
1114 // Discard operand with modifier.
1115 Twine Msg = "immediate must be an integer in the range";
1116 Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1117 return MatchOperand_ParseFail;
1118 }
1119 }
1120
1121 return MatchOperand_NoMatch;
1122}
1123
Alex Bradbury04f06d92017-08-08 14:43:36 +00001124OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001125 SMLoc S = getLoc();
1126 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1127 const MCExpr *Res;
1128
Alex Bradbury04f06d92017-08-08 14:43:36 +00001129 switch (getLexer().getKind()) {
1130 default:
1131 return MatchOperand_NoMatch;
1132 case AsmToken::LParen:
Sam Elliottfafec512019-07-12 08:36:07 +00001133 case AsmToken::Dot:
Alex Bradbury04f06d92017-08-08 14:43:36 +00001134 case AsmToken::Minus:
1135 case AsmToken::Plus:
Lewis Revill18737e82019-06-19 10:27:24 +00001136 case AsmToken::Exclaim:
1137 case AsmToken::Tilde:
Alex Bradbury04f06d92017-08-08 14:43:36 +00001138 case AsmToken::Integer:
1139 case AsmToken::String:
Alex Bradbury6f302b82019-01-10 15:33:17 +00001140 case AsmToken::Identifier:
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001141 if (getParser().parseExpression(Res))
1142 return MatchOperand_ParseFail;
1143 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001144 case AsmToken::Percent:
1145 return parseOperandWithModifier(Operands);
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001146 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001147
Alex Bradburya6e62482017-12-07 10:53:48 +00001148 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001149 return MatchOperand_Success;
1150}
1151
1152OperandMatchResultTy
1153RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1154 SMLoc S = getLoc();
1155 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1156
1157 if (getLexer().getKind() != AsmToken::Percent) {
1158 Error(getLoc(), "expected '%' for operand modifier");
1159 return MatchOperand_ParseFail;
1160 }
1161
1162 getParser().Lex(); // Eat '%'
1163
1164 if (getLexer().getKind() != AsmToken::Identifier) {
1165 Error(getLoc(), "expected valid identifier for operand modifier");
1166 return MatchOperand_ParseFail;
1167 }
1168 StringRef Identifier = getParser().getTok().getIdentifier();
1169 RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1170 if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1171 Error(getLoc(), "unrecognized operand modifier");
1172 return MatchOperand_ParseFail;
1173 }
1174
1175 getParser().Lex(); // Eat the identifier
1176 if (getLexer().getKind() != AsmToken::LParen) {
1177 Error(getLoc(), "expected '('");
1178 return MatchOperand_ParseFail;
1179 }
1180 getParser().Lex(); // Eat '('
1181
1182 const MCExpr *SubExpr;
1183 if (getParser().parseParenExpression(SubExpr, E)) {
1184 return MatchOperand_ParseFail;
1185 }
1186
1187 const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
Alex Bradburya6e62482017-12-07 10:53:48 +00001188 Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001189 return MatchOperand_Success;
1190}
1191
Alex Bradbury68f73c12018-09-18 15:18:16 +00001192OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1193 SMLoc S = getLoc();
1194 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1195 const MCExpr *Res;
1196
1197 if (getLexer().getKind() != AsmToken::Identifier)
1198 return MatchOperand_NoMatch;
1199
1200 StringRef Identifier;
Kito Cheng303217e2019-02-20 03:31:32 +00001201 AsmToken Tok = getLexer().getTok();
1202
Alex Bradbury68f73c12018-09-18 15:18:16 +00001203 if (getParser().parseIdentifier(Identifier))
1204 return MatchOperand_ParseFail;
1205
Alex Bradburyf8078f62019-04-02 12:47:20 +00001206 if (Identifier.consume_back("@plt")) {
1207 Error(getLoc(), "'@plt' operand not valid for instruction");
1208 return MatchOperand_ParseFail;
1209 }
1210
Alex Bradbury68f73c12018-09-18 15:18:16 +00001211 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
Kito Cheng303217e2019-02-20 03:31:32 +00001212
1213 if (Sym->isVariable()) {
1214 const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1215 if (!isa<MCSymbolRefExpr>(V)) {
1216 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1217 return MatchOperand_NoMatch;
1218 }
1219 Res = V;
1220 } else
1221 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Lewis Revilld3f774d2019-08-16 12:00:56 +00001222
1223 MCBinaryExpr::Opcode Opcode;
1224 switch (getLexer().getKind()) {
1225 default:
1226 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1227 return MatchOperand_Success;
1228 case AsmToken::Plus:
1229 Opcode = MCBinaryExpr::Add;
1230 break;
1231 case AsmToken::Minus:
1232 Opcode = MCBinaryExpr::Sub;
1233 break;
1234 }
1235
1236 const MCExpr *Expr;
1237 if (getParser().parseExpression(Expr))
1238 return MatchOperand_ParseFail;
1239 Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
Alex Bradbury68f73c12018-09-18 15:18:16 +00001240 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1241 return MatchOperand_Success;
1242}
1243
Alex Bradbury44668ae2019-04-01 14:53:17 +00001244OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1245 SMLoc S = getLoc();
1246 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1247 const MCExpr *Res;
1248
1249 if (getLexer().getKind() != AsmToken::Identifier)
1250 return MatchOperand_NoMatch;
1251
Lewis Revillcf748812019-06-26 10:35:58 +00001252 // Avoid parsing the register in `call rd, foo` as a call symbol.
1253 if (getLexer().peekTok().getKind() != AsmToken::EndOfStatement)
1254 return MatchOperand_NoMatch;
1255
Alex Bradbury44668ae2019-04-01 14:53:17 +00001256 StringRef Identifier;
1257 if (getParser().parseIdentifier(Identifier))
1258 return MatchOperand_ParseFail;
1259
Alex Bradburyf8078f62019-04-02 12:47:20 +00001260 RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1261 if (Identifier.consume_back("@plt"))
1262 Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1263
Alex Bradbury44668ae2019-04-01 14:53:17 +00001264 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1265 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Alex Bradburyf8078f62019-04-02 12:47:20 +00001266 Res = RISCVMCExpr::create(Res, Kind, getContext());
Alex Bradbury44668ae2019-04-01 14:53:17 +00001267 Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1268 return MatchOperand_Success;
1269}
1270
Alex Bradbury226f3ef2018-09-20 08:10:35 +00001271OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1272 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1273 // both being acceptable forms. When parsing `jal ra, foo` this function
1274 // will be called for the `ra` register operand in an attempt to match the
1275 // single-operand alias. parseJALOffset must fail for this case. It would
1276 // seem logical to try parse the operand using parseImmediate and return
1277 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1278 // the second form rather than the first). We can't do this as there's no
1279 // way of rewinding the lexer state. Instead, return NoMatch if this operand
1280 // is an identifier and is followed by a comma.
1281 if (getLexer().is(AsmToken::Identifier) &&
1282 getLexer().peekTok().is(AsmToken::Comma))
1283 return MatchOperand_NoMatch;
1284
1285 return parseImmediate(Operands);
1286}
1287
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001288OperandMatchResultTy
1289RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1290 if (getLexer().isNot(AsmToken::LParen)) {
1291 Error(getLoc(), "expected '('");
Alex Bradbury04f06d92017-08-08 14:43:36 +00001292 return MatchOperand_ParseFail;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001293 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001294
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001295 getParser().Lex(); // Eat '('
Alex Bradburya6e62482017-12-07 10:53:48 +00001296 Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001297
1298 if (parseRegister(Operands) != MatchOperand_Success) {
1299 Error(getLoc(), "expected register");
1300 return MatchOperand_ParseFail;
1301 }
1302
1303 if (getLexer().isNot(AsmToken::RParen)) {
1304 Error(getLoc(), "expected ')'");
1305 return MatchOperand_ParseFail;
1306 }
1307
1308 getParser().Lex(); // Eat ')'
Alex Bradburya6e62482017-12-07 10:53:48 +00001309 Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001310
Alex Bradbury04f06d92017-08-08 14:43:36 +00001311 return MatchOperand_Success;
1312}
1313
Sam Elliottf596f452019-08-01 12:42:31 +00001314OperandMatchResultTy RISCVAsmParser::parseAtomicMemOp(OperandVector &Operands) {
1315 // Atomic operations such as lr.w, sc.w, and amo*.w accept a "memory operand"
1316 // as one of their register operands, such as `(a0)`. This just denotes that
1317 // the register (in this case `a0`) contains a memory address.
1318 //
1319 // Normally, we would be able to parse these by putting the parens into the
1320 // instruction string. However, GNU as also accepts a zero-offset memory
1321 // operand (such as `0(a0)`), and ignores the 0. Normally this would be parsed
1322 // with parseImmediate followed by parseMemOpBaseReg, but these instructions
1323 // do not accept an immediate operand, and we do not want to add a "dummy"
1324 // operand that is silently dropped.
1325 //
1326 // Instead, we use this custom parser. This will: allow (and discard) an
1327 // offset if it is zero; require (and discard) parentheses; and add only the
1328 // parsed register operand to `Operands`.
1329 //
1330 // These operands are printed with RISCVInstPrinter::printAtomicMemOp, which
1331 // will only print the register surrounded by parentheses (which GNU as also
1332 // uses as its canonical representation for these operands).
1333 std::unique_ptr<RISCVOperand> OptionalImmOp;
1334
1335 if (getLexer().isNot(AsmToken::LParen)) {
1336 // Parse an Integer token. We do not accept arbritrary constant expressions
1337 // in the offset field (because they may include parens, which complicates
1338 // parsing a lot).
1339 int64_t ImmVal;
1340 SMLoc ImmStart = getLoc();
1341 if (getParser().parseIntToken(ImmVal,
1342 "expected '(' or optional integer offset"))
1343 return MatchOperand_ParseFail;
1344
1345 // Create a RISCVOperand for checking later (so the error messages are
1346 // nicer), but we don't add it to Operands.
1347 SMLoc ImmEnd = getLoc();
1348 OptionalImmOp =
1349 RISCVOperand::createImm(MCConstantExpr::create(ImmVal, getContext()),
1350 ImmStart, ImmEnd, isRV64());
1351 }
1352
1353 if (getLexer().isNot(AsmToken::LParen)) {
1354 Error(getLoc(), OptionalImmOp ? "expected '(' after optional integer offset"
1355 : "expected '(' or optional integer offset");
1356 return MatchOperand_ParseFail;
1357 }
1358 getParser().Lex(); // Eat '('
1359
1360 if (parseRegister(Operands) != MatchOperand_Success) {
1361 Error(getLoc(), "expected register");
1362 return MatchOperand_ParseFail;
1363 }
1364
1365 if (getLexer().isNot(AsmToken::RParen)) {
1366 Error(getLoc(), "expected ')'");
1367 return MatchOperand_ParseFail;
1368 }
1369 getParser().Lex(); // Eat ')'
1370
1371 // Deferred Handling of non-zero offsets. This makes the error messages nicer.
1372 if (OptionalImmOp && !OptionalImmOp->isImmZero()) {
1373 Error(OptionalImmOp->getStartLoc(), "optional integer offset must be 0",
1374 SMRange(OptionalImmOp->getStartLoc(), OptionalImmOp->getEndLoc()));
1375 return MatchOperand_ParseFail;
1376 }
1377
1378 return MatchOperand_Success;
1379}
1380
Alex Bradburycd8688a2018-04-25 17:25:29 +00001381/// Looks at a token type and creates the relevant operand from this
1382/// information, adding to Operands. If operand was parsed, returns false, else
Alex Bradbury68f73c12018-09-18 15:18:16 +00001383/// true.
1384bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1385 // Check if the current operand has a custom associated parser, if so, try to
1386 // custom parse the operand, or fallback to the general approach.
1387 OperandMatchResultTy Result =
1388 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1389 if (Result == MatchOperand_Success)
1390 return false;
1391 if (Result == MatchOperand_ParseFail)
1392 return true;
1393
1394 // Attempt to parse token as a register.
1395 if (parseRegister(Operands, true) == MatchOperand_Success)
Alex Bradbury04f06d92017-08-08 14:43:36 +00001396 return false;
1397
1398 // Attempt to parse token as an immediate
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001399 if (parseImmediate(Operands) == MatchOperand_Success) {
1400 // Parse memory base register if present
1401 if (getLexer().is(AsmToken::LParen))
1402 return parseMemOpBaseReg(Operands) != MatchOperand_Success;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001403 return false;
Alex Bradbury6758ecb2017-09-17 14:27:35 +00001404 }
Alex Bradbury04f06d92017-08-08 14:43:36 +00001405
1406 // Finally we have exhausted all options and must declare defeat.
1407 Error(getLoc(), "unknown operand");
1408 return true;
1409}
1410
1411bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1412 StringRef Name, SMLoc NameLoc,
1413 OperandVector &Operands) {
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001414 // Ensure that if the instruction occurs when relaxation is enabled,
1415 // relocations are forced for the file. Ideally this would be done when there
1416 // is enough information to reliably determine if the instruction itself may
1417 // cause relaxations. Unfortunately instruction processing stage occurs in the
1418 // same pass as relocation emission, so it's too late to set a 'sticky bit'
1419 // for the entire file.
1420 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1421 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1422 if (Assembler != nullptr) {
1423 RISCVAsmBackend &MAB =
1424 static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1425 MAB.setForceRelocs();
1426 }
1427 }
1428
Alex Bradbury04f06d92017-08-08 14:43:36 +00001429 // First operand is token for instruction
Alex Bradburya6e62482017-12-07 10:53:48 +00001430 Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
Alex Bradbury04f06d92017-08-08 14:43:36 +00001431
1432 // If there are no more operands, then finish
1433 if (getLexer().is(AsmToken::EndOfStatement))
1434 return false;
1435
1436 // Parse first operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001437 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001438 return true;
1439
1440 // Parse until end of statement, consuming commas between operands
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001441 unsigned OperandIdx = 1;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001442 while (getLexer().is(AsmToken::Comma)) {
1443 // Consume comma token
1444 getLexer().Lex();
1445
1446 // Parse next operand
Alex Bradbury68f73c12018-09-18 15:18:16 +00001447 if (parseOperand(Operands, Name))
Alex Bradbury04f06d92017-08-08 14:43:36 +00001448 return true;
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001449
1450 ++OperandIdx;
Alex Bradbury04f06d92017-08-08 14:43:36 +00001451 }
1452
1453 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1454 SMLoc Loc = getLexer().getLoc();
1455 getParser().eatToEndOfStatement();
1456 return Error(Loc, "unexpected token");
1457 }
1458
1459 getParser().Lex(); // Consume the EndOfStatement.
1460 return false;
1461}
1462
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001463bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
1464 RISCVMCExpr::VariantKind &Kind,
1465 int64_t &Addend) {
1466 Kind = RISCVMCExpr::VK_RISCV_None;
1467 Addend = 0;
1468
1469 if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1470 Kind = RE->getKind();
1471 Expr = RE->getSubExpr();
1472 }
1473
1474 // It's a simple symbol reference or constant with no addend.
1475 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1476 return true;
1477
1478 const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
1479 if (!BE)
1480 return false;
1481
1482 if (!isa<MCSymbolRefExpr>(BE->getLHS()))
1483 return false;
1484
1485 if (BE->getOpcode() != MCBinaryExpr::Add &&
1486 BE->getOpcode() != MCBinaryExpr::Sub)
1487 return false;
1488
1489 // We are able to support the subtraction of two symbol references
1490 if (BE->getOpcode() == MCBinaryExpr::Sub &&
1491 isa<MCSymbolRefExpr>(BE->getRHS()))
1492 return true;
1493
Hiroshi Inoue9ff23802018-04-09 04:37:53 +00001494 // See if the addend is a constant, otherwise there's more going
Alex Bradbury9d3f1252017-09-28 08:26:24 +00001495 // on here than we can deal with.
1496 auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
1497 if (!AddendExpr)
1498 return false;
1499
1500 Addend = AddendExpr->getValue();
1501 if (BE->getOpcode() == MCBinaryExpr::Sub)
1502 Addend = -Addend;
1503
1504 // It's some symbol reference + a constant addend
1505 return Kind != RISCVMCExpr::VK_RISCV_Invalid;
1506}
1507
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001508bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
1509 // This returns false if this function recognizes the directive
1510 // regardless of whether it is successfully handles or reports an
1511 // error. Otherwise it returns true to give the generic parser a
1512 // chance at recognizing it.
1513 StringRef IDVal = DirectiveID.getString();
1514
1515 if (IDVal == ".option")
1516 return parseDirectiveOption();
1517
1518 return true;
1519}
1520
1521bool RISCVAsmParser::parseDirectiveOption() {
1522 MCAsmParser &Parser = getParser();
1523 // Get the option token.
1524 AsmToken Tok = Parser.getTok();
1525 // At the moment only identifiers are supported.
1526 if (Tok.isNot(AsmToken::Identifier))
1527 return Error(Parser.getTok().getLoc(),
1528 "unexpected token, expected identifier");
1529
1530 StringRef Option = Tok.getIdentifier();
1531
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001532 if (Option == "push") {
1533 getTargetStreamer().emitDirectiveOptionPush();
1534
1535 Parser.Lex();
1536 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1537 return Error(Parser.getTok().getLoc(),
1538 "unexpected token, expected end of statement");
1539
1540 pushFeatureBits();
1541 return false;
1542 }
1543
1544 if (Option == "pop") {
1545 SMLoc StartLoc = Parser.getTok().getLoc();
1546 getTargetStreamer().emitDirectiveOptionPop();
1547
1548 Parser.Lex();
1549 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1550 return Error(Parser.getTok().getLoc(),
1551 "unexpected token, expected end of statement");
1552
1553 if (popFeatureBits())
1554 return Error(StartLoc, ".option pop with no .option push");
1555
1556 return false;
1557 }
1558
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001559 if (Option == "rvc") {
1560 getTargetStreamer().emitDirectiveOptionRVC();
1561
1562 Parser.Lex();
1563 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1564 return Error(Parser.getTok().getLoc(),
1565 "unexpected token, expected end of statement");
1566
1567 setFeatureBits(RISCV::FeatureStdExtC, "c");
1568 return false;
1569 }
1570
1571 if (Option == "norvc") {
1572 getTargetStreamer().emitDirectiveOptionNoRVC();
1573
1574 Parser.Lex();
1575 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1576 return Error(Parser.getTok().getLoc(),
1577 "unexpected token, expected end of statement");
1578
1579 clearFeatureBits(RISCV::FeatureStdExtC, "c");
1580 return false;
1581 }
1582
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001583 if (Option == "relax") {
1584 getTargetStreamer().emitDirectiveOptionRelax();
1585
1586 Parser.Lex();
1587 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1588 return Error(Parser.getTok().getLoc(),
1589 "unexpected token, expected end of statement");
1590
1591 setFeatureBits(RISCV::FeatureRelax, "relax");
1592 return false;
1593 }
1594
1595 if (Option == "norelax") {
1596 getTargetStreamer().emitDirectiveOptionNoRelax();
1597
1598 Parser.Lex();
1599 if (Parser.getTok().isNot(AsmToken::EndOfStatement))
1600 return Error(Parser.getTok().getLoc(),
1601 "unexpected token, expected end of statement");
1602
1603 clearFeatureBits(RISCV::FeatureRelax, "relax");
1604 return false;
1605 }
1606
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001607 // Unknown option.
1608 Warning(Parser.getTok().getLoc(),
Alex Bradbury893e5bc2018-11-28 16:39:14 +00001609 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
1610 "'norelax'");
Alex Bradburybca0c3c2018-05-11 17:30:28 +00001611 Parser.eatToEndOfStatement();
1612 return false;
1613}
Alex Bradbury04f06d92017-08-08 14:43:36 +00001614
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001615void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1616 MCInst CInst;
1617 bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001618 S.EmitInstruction((Res ? CInst : Inst), getSTI());
1619}
1620
Luis Marquesfa06e952019-08-16 14:27:50 +00001621void RISCVAsmParser::emitLoadImm(Register DestReg, int64_t Value,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001622 MCStreamer &Out) {
Alex Bradbury22c091f2018-11-15 10:11:31 +00001623 RISCVMatInt::InstSeq Seq;
1624 RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001625
Luis Marquesfa06e952019-08-16 14:27:50 +00001626 Register SrcReg = RISCV::X0;
Alex Bradbury22c091f2018-11-15 10:11:31 +00001627 for (RISCVMatInt::Inst &Inst : Seq) {
1628 if (Inst.Opc == RISCV::LUI) {
1629 emitToStreamer(
1630 Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1631 } else {
1632 emitToStreamer(
1633 Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1634 Inst.Imm));
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001635 }
1636
Alex Bradbury22c091f2018-11-15 10:11:31 +00001637 // Only the first instruction has X0 as its source.
1638 SrcReg = DestReg;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001639 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001640}
1641
Alex Bradbury22531c42019-02-15 09:53:32 +00001642void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
1643 const MCExpr *Symbol,
1644 RISCVMCExpr::VariantKind VKHi,
1645 unsigned SecondOpcode, SMLoc IDLoc,
1646 MCStreamer &Out) {
1647 // A pair of instructions for PC-relative addressing; expands to
1648 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
1649 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001650 MCContext &Ctx = getContext();
1651
1652 MCSymbol *TmpLabel = Ctx.createTempSymbol(
1653 "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false);
1654 Out.EmitLabel(TmpLabel);
1655
Alex Bradbury22531c42019-02-15 09:53:32 +00001656 const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001657 emitToStreamer(
Alex Bradbury22531c42019-02-15 09:53:32 +00001658 Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001659
1660 const MCExpr *RefToLinkTmpLabel =
1661 RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
1662 RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
1663
Alex Bradbury22531c42019-02-15 09:53:32 +00001664 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001665 .addOperand(DestReg)
Alex Bradbury22531c42019-02-15 09:53:32 +00001666 .addOperand(TmpReg)
Roger Ferrer Ibanezc8f4dbb2018-08-14 08:30:42 +00001667 .addExpr(RefToLinkTmpLabel));
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001668}
1669
Alex Bradbury22531c42019-02-15 09:53:32 +00001670void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
1671 MCStreamer &Out) {
1672 // The load local address pseudo-instruction "lla" is used in PC-relative
1673 // addressing of local symbols:
1674 // lla rdest, symbol
1675 // expands to
1676 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1677 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1678 MCOperand DestReg = Inst.getOperand(0);
1679 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1680 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1681 RISCV::ADDI, IDLoc, Out);
1682}
1683
1684void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
1685 MCStreamer &Out) {
1686 // The load address pseudo-instruction "la" is used in PC-relative and
1687 // GOT-indirect addressing of global symbols:
1688 // la rdest, symbol
1689 // expands to either (for non-PIC)
1690 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
1691 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1692 // or (for PIC)
1693 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
1694 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1695 MCOperand DestReg = Inst.getOperand(0);
1696 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1697 unsigned SecondOpcode;
1698 RISCVMCExpr::VariantKind VKHi;
1699 // FIXME: Should check .option (no)pic when implemented
1700 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1701 SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1702 VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
1703 } else {
1704 SecondOpcode = RISCV::ADDI;
1705 VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
1706 }
1707 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1708}
1709
Lewis Revill74927552019-05-23 14:46:27 +00001710void RISCVAsmParser::emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc,
1711 MCStreamer &Out) {
1712 // The load TLS IE address pseudo-instruction "la.tls.ie" is used in
1713 // initial-exec TLS model addressing of global symbols:
1714 // la.tls.ie rdest, symbol
1715 // expands to
1716 // TmpLabel: AUIPC rdest, %tls_ie_pcrel_hi(symbol)
1717 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
1718 MCOperand DestReg = Inst.getOperand(0);
1719 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1720 unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
1721 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GOT_HI,
1722 SecondOpcode, IDLoc, Out);
1723}
1724
1725void RISCVAsmParser::emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc,
1726 MCStreamer &Out) {
1727 // The load TLS GD address pseudo-instruction "la.tls.gd" is used in
1728 // global-dynamic TLS model addressing of global symbols:
1729 // la.tls.gd rdest, symbol
1730 // expands to
1731 // TmpLabel: AUIPC rdest, %tls_gd_pcrel_hi(symbol)
1732 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
1733 MCOperand DestReg = Inst.getOperand(0);
1734 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1735 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GD_HI,
1736 RISCV::ADDI, IDLoc, Out);
1737}
1738
Kito Cheng303217e2019-02-20 03:31:32 +00001739void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
1740 SMLoc IDLoc, MCStreamer &Out,
1741 bool HasTmpReg) {
1742 // The load/store pseudo-instruction does a pc-relative load with
1743 // a symbol.
1744 //
1745 // The expansion looks like this
1746 //
1747 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
1748 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
1749 MCOperand DestReg = Inst.getOperand(0);
1750 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1751 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1752 MCOperand TmpReg = Inst.getOperand(TmpRegOpIdx);
1753 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
1754 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
1755 Opcode, IDLoc, Out);
1756}
1757
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001758bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
1759 OperandVector &Operands) {
1760 assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
1761 assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
1762 if (Inst.getOperand(2).getReg() != RISCV::X4) {
1763 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
1764 return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
1765 "%tprel_add modifier");
1766 }
1767
1768 return false;
1769}
1770
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001771bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001772 OperandVector &Operands,
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001773 MCStreamer &Out) {
1774 Inst.setLoc(IDLoc);
1775
Alex Bradbury22531c42019-02-15 09:53:32 +00001776 switch (Inst.getOpcode()) {
1777 default:
1778 break;
1779 case RISCV::PseudoLI: {
Luis Marquesfa06e952019-08-16 14:27:50 +00001780 Register Reg = Inst.getOperand(0).getReg();
Alex Bradbury2ba76be2019-01-03 14:41:41 +00001781 const MCOperand &Op1 = Inst.getOperand(1);
1782 if (Op1.isExpr()) {
1783 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
1784 // Just convert to an addi. This allows compatibility with gas.
1785 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
1786 .addReg(Reg)
1787 .addReg(RISCV::X0)
1788 .addExpr(Op1.getExpr()));
1789 return false;
1790 }
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001791 int64_t Imm = Inst.getOperand(1).getImm();
1792 // On RV32 the immediate here can either be a signed or an unsigned
1793 // 32-bit number. Sign extension has to be performed to ensure that Imm
1794 // represents the expected signed 64-bit number.
1795 if (!isRV64())
1796 Imm = SignExtend64<32>(Imm);
1797 emitLoadImm(Reg, Imm, Out);
1798 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001799 }
1800 case RISCV::PseudoLLA:
Roger Ferrer Ibanez577a97e2018-08-09 07:08:20 +00001801 emitLoadLocalAddress(Inst, IDLoc, Out);
1802 return false;
Alex Bradbury22531c42019-02-15 09:53:32 +00001803 case RISCV::PseudoLA:
1804 emitLoadAddress(Inst, IDLoc, Out);
1805 return false;
Lewis Revill74927552019-05-23 14:46:27 +00001806 case RISCV::PseudoLA_TLS_IE:
1807 emitLoadTLSIEAddress(Inst, IDLoc, Out);
1808 return false;
1809 case RISCV::PseudoLA_TLS_GD:
1810 emitLoadTLSGDAddress(Inst, IDLoc, Out);
1811 return false;
Kito Cheng303217e2019-02-20 03:31:32 +00001812 case RISCV::PseudoLB:
1813 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
1814 return false;
1815 case RISCV::PseudoLBU:
1816 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
1817 return false;
1818 case RISCV::PseudoLH:
1819 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
1820 return false;
1821 case RISCV::PseudoLHU:
1822 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
1823 return false;
1824 case RISCV::PseudoLW:
1825 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
1826 return false;
1827 case RISCV::PseudoLWU:
1828 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
1829 return false;
1830 case RISCV::PseudoLD:
1831 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
1832 return false;
1833 case RISCV::PseudoFLW:
1834 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
1835 return false;
1836 case RISCV::PseudoFLD:
1837 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
1838 return false;
1839 case RISCV::PseudoSB:
1840 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
1841 return false;
1842 case RISCV::PseudoSH:
1843 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
1844 return false;
1845 case RISCV::PseudoSW:
1846 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
1847 return false;
1848 case RISCV::PseudoSD:
1849 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
1850 return false;
1851 case RISCV::PseudoFSW:
1852 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
1853 return false;
1854 case RISCV::PseudoFSD:
1855 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
1856 return false;
Roger Ferrer Ibanezb621f042019-04-11 15:13:12 +00001857 case RISCV::PseudoAddTPRel:
1858 if (checkPseudoAddTPRel(Inst, Operands))
1859 return true;
Fangrui Song92e78b72019-07-01 11:41:07 +00001860 break;
Alex Bradbury6a4b5442018-06-07 15:35:47 +00001861 }
1862
1863 emitToStreamer(Out, Inst);
1864 return false;
1865}
1866
Tom Stellard4b0b2612019-06-11 03:21:13 +00001867extern "C" void LLVMInitializeRISCVAsmParser() {
Alex Bradbury04f06d92017-08-08 14:43:36 +00001868 RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
1869 RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
1870}