LLVM 24.0.0git
RISCVAsmParser.cpp
Go to the documentation of this file.
1//===-- RISCVAsmParser.cpp - Parse RISC-V assembly to MCInst instructions -===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
18#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/Statistic.h"
24#include "llvm/MC/MCAssembler.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCInstrInfo.h"
35#include "llvm/MC/MCStreamer.h"
37#include "llvm/MC/MCValue.h"
41#include "llvm/Support/Debug.h"
45
46#include <limits>
47#include <map>
48#include <optional>
49
50using namespace llvm;
51
52#define DEBUG_TYPE "riscv-asm-parser"
53
54STATISTIC(RISCVNumInstrsCompressed,
55 "Number of RISC-V Compressed instructions emitted");
56
57namespace {
58struct RISCVOperand;
59
60struct ParserOptionsSet {
61 bool IsPicEnabled;
62};
63
64class RISCVAsmParser : public MCTargetAsmParser {
65 const RISCVMCOptions &CLOpts;
66
67 // This tracks the parsing of the 4 optional operands that make up the vtype
68 // portion of vset(i)vli instructions which are separated by commas.
69 enum class VTypeState {
70 SeenNothingYet,
71 SeenSew,
72 SeenLmul,
73 SeenTailPolicy,
74 SeenMaskPolicy,
75 };
76
77 SmallVector<FeatureBitset, 4> FeatureBitStack;
78
79 SmallVector<ParserOptionsSet, 4> ParserOptionsStack;
80 ParserOptionsSet ParserOptions;
81
82 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
83 bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
84 bool isRVE() const { return getSTI().hasFeature(RISCV::FeatureStdExtE); }
85 bool enableExperimentalExtension() const {
86 return getSTI().hasFeature(RISCV::Experimental);
87 }
88
89 RISCVTargetStreamer &getTargetStreamer() {
90 assert(getParser().getStreamer().getTargetStreamer() &&
91 "do not have a target streamer");
92 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
93 return static_cast<RISCVTargetStreamer &>(TS);
94 }
95
96 bool generateImmOutOfRangeError(SMLoc ErrorLoc, int64_t Lower, int64_t Upper,
97 const Twine &Msg);
98
99 struct NearMissMessage {
100 SMLoc Loc;
101 std::string Message;
102 };
103
104 std::string getCustomOperandDiag(unsigned MatchError);
105
106 void FilterNearMisses(SmallVectorImpl<NearMissInfo> &NearMissesIn,
107 SmallVectorImpl<NearMissMessage> &NearMissesOut,
108 SMLoc IDLoc, OperandVector &Operands);
109 void ReportNearMisses(SmallVectorImpl<NearMissInfo> &NearMisses, SMLoc IDLoc,
111
112 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
113 OperandVector &Operands, MCStreamer &Out,
114 uint64_t &ErrorInfo,
115 bool MatchingInlineAsm) override;
116
117 MCRegister matchRegisterNameHelper(StringRef Name) const;
118 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
119 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
120 SMLoc &EndLoc) override;
121
122 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
123 SMLoc NameLoc, OperandVector &Operands) override;
124
125 ParseStatus parseDirective(AsmToken DirectiveID) override;
126
127 bool parseVTypeToken(const AsmToken &Tok, VTypeState &State, unsigned &Sew,
128 unsigned &Lmul, bool &Fractional, bool &TailAgnostic,
129 bool &MaskAgnostic, bool &AltFmt);
130 bool generateVTypeError(SMLoc ErrorLoc);
131
132 bool generateXSfmmVTypeError(SMLoc ErrorLoc);
133 // Helper to actually emit an instruction to the MCStreamer. Also, when
134 // possible, compression of the instruction is performed.
135 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
136
137 // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
138 // synthesize the desired immediate value into the destination register.
139 void emitLoadImm(MCRegister DestReg, int64_t Value, MCStreamer &Out);
140
141 // Helper to emit a combination of AUIPC and SecondOpcode. Used to implement
142 // helpers such as emitLoadLocalAddress and emitLoadAddress.
143 void emitAuipcInstPair(MCRegister DestReg, MCRegister TmpReg,
144 const MCExpr *Symbol, RISCV::Specifier VKHi,
145 unsigned SecondOpcode, SMLoc IDLoc, MCStreamer &Out);
146
147 // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
148 void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
149
150 // Helper to emit pseudo instruction "lga" used in GOT-rel addressing.
151 void emitLoadGlobalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
152
153 // Helper to emit pseudo instruction "la" used in GOT/PC-rel addressing.
154 void emitLoadAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
155
156 // Helper to emit pseudo instruction "la.tls.ie" used in initial-exec TLS
157 // addressing.
158 void emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
159
160 // Helper to emit pseudo instruction "la.tls.gd" used in global-dynamic TLS
161 // addressing.
162 void emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
163
164 // Helper to emit pseudo load/store instruction with a symbol.
165 void emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
166 MCStreamer &Out, bool HasTmpReg);
167
168 // Helper to emit Xqcilo pseudo load/store as qc.e.li + PseudoQCAccess pair.
169 // For loads: qc.e.li rd, sym; lx rd, 0(rd), %qc.access(sym)
170 // For stores: qc.e.li rt, sym; sx rs, 0(rt), %qc.access(sym)
171 void emitQCELILoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
172 MCStreamer &Out, bool HasTmpReg);
173
174 // Helper to emit pseudo sign/zero extend instruction.
175 void emitPseudoExtend(MCInst &Inst, bool SignExtend, int64_t Width,
176 SMLoc IDLoc, MCStreamer &Out);
177
178 // Helper to emit pseudo vmsge{u}.vx instruction.
179 void emitVMSGE(MCInst &Inst, unsigned Opcode, SMLoc IDLoc, MCStreamer &Out);
180
181 // Checks that a PseudoAddTPRel is using x4/tp in its second input operand.
182 // Enforcing this using a restricted register class for the second input
183 // operand of PseudoAddTPRel results in a poor diagnostic due to the fact
184 // 'add' is an overloaded mnemonic.
185 bool checkPseudoAddTPRel(MCInst &Inst, OperandVector &Operands);
186
187 // Checks that a PseudoTLSDESCCall is using x5/t0 in its output operand.
188 // Enforcing this using a restricted register class for the output
189 // operand of PseudoTLSDESCCall results in a poor diagnostic due to the fact
190 // 'jalr' is an overloaded mnemonic.
191 bool checkPseudoTLSDESCCall(MCInst &Inst, OperandVector &Operands);
192
193 // Check instruction constraints.
194 bool validateInstruction(MCInst &Inst, OperandVector &Operands);
195
196 /// Helper for processing MC instructions that have been successfully matched
197 /// by matchAndEmitInstruction. Modifications to the emitted instructions,
198 /// like the expansion of pseudo instructions (e.g., "li"), can be performed
199 /// in this method.
200 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
201 MCStreamer &Out);
202
203// Auto-generated instruction matching functions
204#define GET_ASSEMBLER_HEADER
205#include "RISCVGenAsmMatcher.inc"
206
207 ParseStatus parseCSRSystemRegister(OperandVector &Operands);
209 ParseStatus parseExpression(OperandVector &Operands);
210 ParseStatus parseRegister(OperandVector &Operands, bool AllowParens = false);
211 ParseStatus parseMemOpBaseReg(OperandVector &Operands);
212 ParseStatus parseZeroOffsetMemOp(OperandVector &Operands);
213 ParseStatus parseOperandWithSpecifier(OperandVector &Operands);
214 ParseStatus parseBareSymbol(OperandVector &Operands);
215 ParseStatus parseCallSymbol(OperandVector &Operands);
216 ParseStatus parseTailCallSymbol(OperandVector &Operands);
217 ParseStatus parsePseudoJumpSymbol(OperandVector &Operands);
218 ParseStatus parseJALOffset(OperandVector &Operands);
219 ParseStatus parseVTypeI(OperandVector &Operands);
220 ParseStatus parseMaskReg(OperandVector &Operands);
221 ParseStatus parseVScaleReg(OperandVector &Operands);
222 ParseStatus parseTileLambda(OperandVector &Operands);
223 ParseStatus parseInsnDirectiveOpcode(OperandVector &Operands);
224 ParseStatus parseInsnCDirectiveOpcode(OperandVector &Operands);
225 ParseStatus parseFRMArg(OperandVector &Operands);
226 ParseStatus parseSMTVType(OperandVector &Operands);
227 ParseStatus parseFenceArg(OperandVector &Operands);
228 ParseStatus parseRegList(OperandVector &Operands, bool MustIncludeS0 = false);
229 ParseStatus parseRegListS0(OperandVector &Operands) {
230 return parseRegList(Operands, /*MustIncludeS0=*/true);
231 }
232
233 ParseStatus parseRegReg(OperandVector &Operands);
234 ParseStatus parseXSfmmVType(OperandVector &Operands);
235 ParseStatus parseZcmpStackAdj(OperandVector &Operands,
236 bool ExpectNegative = false);
237 ParseStatus parseZcmpNegStackAdj(OperandVector &Operands) {
238 return parseZcmpStackAdj(Operands, /*ExpectNegative*/ true);
239 }
240
241 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
242 bool parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E);
243 bool parseDataExpr(const MCExpr *&Res) override;
244
245 bool parseDirectiveOption();
246 bool parseDirectiveAttribute();
247 bool parseDirectiveInsn(SMLoc L);
248 bool parseDirectiveVariantCC();
249
250 /// Helper to reset target features for a new arch string. It
251 /// also records the new arch string that is expanded by RISCVISAInfo
252 /// and reports error for invalid arch string.
253 bool resetToArch(StringRef Arch, SMLoc Loc, std::string &Result,
254 bool FromOptionDirective);
255
256 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
257 if (!(getSTI().hasFeature(Feature))) {
258 MCSubtargetInfo &STI = copySTI();
259 STI.ToggleFeature(FeatureString);
260
261 // Update the C and Zce implications.
263
264 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
265 }
266 }
267
268 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
269 if (getSTI().hasFeature(Feature)) {
270 MCSubtargetInfo &STI = copySTI();
271 setAvailableFeatures(
272 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
273 }
274 }
275
276 void pushFeatureBits() {
277 assert(FeatureBitStack.size() == ParserOptionsStack.size() &&
278 "These two stacks must be kept synchronized");
279 FeatureBitStack.push_back(getSTI().getFeatureBits());
280 ParserOptionsStack.push_back(ParserOptions);
281 }
282
283 bool popFeatureBits() {
284 assert(FeatureBitStack.size() == ParserOptionsStack.size() &&
285 "These two stacks must be kept synchronized");
286 if (FeatureBitStack.empty())
287 return true;
288
289 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
290 copySTI().setFeatureBits(FeatureBits);
291 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
292
293 ParserOptions = ParserOptionsStack.pop_back_val();
294
295 return false;
296 }
297
298 std::unique_ptr<RISCVOperand> defaultMaskRegOp() const;
299 std::unique_ptr<RISCVOperand> defaultFRMArgOp() const;
300 std::unique_ptr<RISCVOperand> defaultFRMArgLegacyOp() const;
301 std::unique_ptr<RISCVOperand> defaultSMTVType();
302 std::unique_ptr<RISCVOperand> defaultZeroOffset();
303
304public:
305 enum RISCVMatchResultTy : unsigned {
306 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
307#define GET_OPERAND_DIAGNOSTIC_TYPES
308#include "RISCVGenAsmMatcher.inc"
309#undef GET_OPERAND_DIAGNOSTIC_TYPES
310 };
311
312 static bool classifySymbolRef(const MCExpr *Expr, RISCV::Specifier &Kind);
313 static bool isSymbolDiff(const MCExpr *Expr);
314
315 RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
316 const MCInstrInfo &MII)
317 : MCTargetAsmParser(STI, MII),
318 CLOpts(RISCV_MC::getCLOpts(Parser.getContext().getTargetOptions())) {
320
321 Parser.addAliasForDirective(".half", ".2byte");
322 Parser.addAliasForDirective(".hword", ".2byte");
323 Parser.addAliasForDirective(".word", ".4byte");
324 Parser.addAliasForDirective(".dword", ".8byte");
325 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
326
327 const MCObjectFileInfo *MOFI = Parser.getContext().getObjectFileInfo();
328 ParserOptions.IsPicEnabled = MOFI->isPositionIndependent();
329
330 if (CLOpts.add_build_attributes)
331 getTargetStreamer().emitTargetAttributes(STI, /*EmitStackAlign*/ false);
332 }
333
334 // Validate the requested -target-abi now that the lexer has been primed
335 // with the first token, so diagnostics can be reported with a real source
336 // location instead of being printed with no location information.
337 void onBeginOfFile() override {
338 // If the target streamer already has a resolved ABI (e.g. set by
339 // RISCVAsmPrinter during codegen), skip ABI validation.
340 if (getTargetStreamer().hasTargetABI())
341 return;
342
343 Expected<RISCVABI::ABI> ABIOrErr =
344 RISCVABI::computeTargetABI(getSTI(), getTargetOptions().ABIName);
345 if (!ABIOrErr) {
346 getParser().printError(getLoc(), toString(ABIOrErr.takeError()));
347 getTargetStreamer().setTargetABI(
348 cantFail(RISCVABI::computeTargetABI(getSTI(), "")));
349 return;
350 }
351 getTargetStreamer().setTargetABI(*ABIOrErr);
352 }
353};
354
355/// RISCVOperand - Instances of this class represent a parsed machine
356/// instruction
357struct RISCVOperand final : public MCParsedAsmOperand {
358
359 enum class KindTy {
360 Token,
361 Register,
362 Expression,
363 FPImmediate,
364 SystemRegister,
365 VType,
366 SMTVType,
367 FRM,
368 Fence,
369 RegList,
370 StackAdj,
371 RegReg,
372 } Kind;
373
374 struct ExprOp {
375 const MCExpr *Expr;
376 bool IsRV64;
377 };
378
379 struct FPImmOp {
380 uint64_t Val;
381 };
382
383 struct SysRegOp {
384 const char *Data;
385 unsigned Length;
386 unsigned Encoding;
387 // FIXME: Add the Encoding parsed fields as needed for checks,
388 // e.g.: read/write or user/supervisor/machine privileges.
389 };
390
391 struct VTypeOp {
392 unsigned Val;
393 };
394
395 struct SMTVTypeOp {
397 };
398
399 struct FRMOp {
401 };
402
403 struct FenceOp {
404 unsigned Val;
405 };
406
407 struct RegListOp {
408 unsigned Encoding;
409 };
410
411 struct StackAdjOp {
412 unsigned Val;
413 };
414
415 struct RegRegOp {
416 MCRegister BaseReg;
417 MCRegister OffsetReg;
418 };
419
420 SMLoc StartLoc, EndLoc;
421 union {
422 StringRef Tok;
423 MCRegister Reg;
424 ExprOp Expr;
425 FPImmOp FPImm;
426 SysRegOp SysReg;
427 VTypeOp VType;
428 SMTVTypeOp SMTVType;
429 FRMOp FRM;
430 FenceOp Fence;
431 RegListOp RegList;
432 StackAdjOp StackAdj;
433 RegRegOp RegReg;
434 };
435
436 RISCVOperand(KindTy K) : Kind(K) {}
437
438public:
439 RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
440 Kind = o.Kind;
441 StartLoc = o.StartLoc;
442 EndLoc = o.EndLoc;
443 switch (Kind) {
444 case KindTy::Register:
445 Reg = o.Reg;
446 break;
447 case KindTy::Expression:
448 Expr = o.Expr;
449 break;
450 case KindTy::FPImmediate:
451 FPImm = o.FPImm;
452 break;
453 case KindTy::Token:
454 Tok = o.Tok;
455 break;
456 case KindTy::SystemRegister:
457 SysReg = o.SysReg;
458 break;
459 case KindTy::VType:
460 VType = o.VType;
461 break;
462 case KindTy::SMTVType:
463 SMTVType = o.SMTVType;
464 break;
465 case KindTy::FRM:
466 FRM = o.FRM;
467 break;
468 case KindTy::Fence:
469 Fence = o.Fence;
470 break;
471 case KindTy::RegList:
472 RegList = o.RegList;
473 break;
474 case KindTy::StackAdj:
475 StackAdj = o.StackAdj;
476 break;
477 case KindTy::RegReg:
478 RegReg = o.RegReg;
479 break;
480 }
481 }
482
483 bool isToken() const override { return Kind == KindTy::Token; }
484 bool isReg() const override { return Kind == KindTy::Register; }
485 bool isExpr() const { return Kind == KindTy::Expression; }
486 bool isV0Reg() const { return Kind == KindTy::Register && Reg == RISCV::V0; }
487 bool isAnyReg() const {
488 return Kind == KindTy::Register &&
489 (getRISCVMCRegisterClass(RISCV::GPRRegClassID).contains(Reg) ||
490 getRISCVMCRegisterClass(RISCV::FPR64RegClassID).contains(Reg) ||
491 getRISCVMCRegisterClass(RISCV::VRRegClassID).contains(Reg));
492 }
493 bool isAnyRegC() const {
494 return Kind == KindTy::Register &&
495 (getRISCVMCRegisterClass(RISCV::GPRCRegClassID).contains(Reg) ||
496 getRISCVMCRegisterClass(RISCV::FPR64CRegClassID).contains(Reg));
497 }
498 bool isImm() const override { return isExpr(); }
499 bool isMem() const override { return false; }
500 bool isSystemRegister() const { return Kind == KindTy::SystemRegister; }
501 bool isRegReg() const { return Kind == KindTy::RegReg; }
502 bool isRegList() const { return Kind == KindTy::RegList; }
503 bool isRegListS0() const {
504 return Kind == KindTy::RegList && RegList.Encoding != RISCVZC::RA;
505 }
506 bool isStackAdj() const { return Kind == KindTy::StackAdj; }
507
508 bool isGPR() const {
509 return Kind == KindTy::Register &&
510 getRISCVMCRegisterClass(RISCV::GPRRegClassID).contains(Reg);
511 }
512
513 bool isGPRForFP(const MCSubtargetInfo &STI) const {
514 // Check !F rather than Zfinx/Zdinx/Zhinx so GPR operands still match *inx
515 // variants when no FP extension is enabled, letting the matcher report the
516 // missing *inx extension instead of an invalid operand.
517 return isGPR() && !STI.hasFeature(RISCV::FeatureStdExtF);
518 }
519
520 bool isGPRForFP64(const MCSubtargetInfo &STI) const {
521 // Accept single GPRs on RV32 Zdinx as well so RV64-only instructions (which
522 // have no GPRPairAsFPR variant) report the missing RV64 feature rather than
523 // an invalid operand.
524 return isGPR() && !STI.hasFeature(RISCV::FeatureStdExtF) &&
525 (STI.hasFeature(RISCV::Feature64Bit) ||
526 STI.hasFeature(RISCV::FeatureStdExtZdinx));
527 }
528
529 bool isEvenGPRForFP64(const MCSubtargetInfo &STI) const {
530 return isGPR() && (Reg - RISCV::X0) % 2 == 0 &&
531 !STI.hasFeature(RISCV::Feature64Bit) &&
532 !STI.hasFeature(RISCV::FeatureStdExtF);
533 }
534
535 static bool evaluateConstantExpr(const MCExpr *Expr, int64_t &Imm) {
536 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
537 Imm = CE->getValue();
538 return true;
539 }
540
541 return false;
542 }
543
544 // True if operand is a symbol with no modifiers, or a constant with no
545 // modifiers and isShiftedInt<N-1, 1>(Op).
546 template <int N> bool isBareSimmNLsb0() const {
547 if (!isExpr())
548 return false;
549
550 int64_t Imm;
551 if (evaluateConstantExpr(getExpr(), Imm))
552 return isShiftedInt<N - 1, 1>(fixImmediateForRV32(Imm, isRV64Expr()));
553
555 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
556 VK == RISCV::S_None;
557 }
558
559 // True if operand is a symbol with no modifiers, or a constant with no
560 // modifiers and isInt<N>(Op).
561 template <int N> bool isBareSimmN() const {
562 if (!isExpr())
563 return false;
564
565 int64_t Imm;
566 if (evaluateConstantExpr(getExpr(), Imm))
567 return isInt<N>(fixImmediateForRV32(Imm, isRV64Expr()));
568
570 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
571 VK == RISCV::S_None;
572 }
573
574 // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
575
576 bool isBareSymbol() const {
577 int64_t Imm;
578 // Must be of 'immediate' type but not a constant.
579 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
580 return false;
581
583 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
584 VK == RISCV::S_None;
585 }
586
587 bool isCallSymbol() const {
588 int64_t Imm;
589 // Must be of 'immediate' type but not a constant.
590 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
591 return false;
592
594 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
595 VK == RISCV::S_CALL_PLT;
596 }
597
598 bool isTailCallSymbol() const { return isCallSymbol(); }
599
600 bool isPseudoJumpSymbol() const {
601 int64_t Imm;
602 // Must be of 'immediate' type but not a constant.
603 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
604 return false;
605
607 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
608 VK == RISCV::S_CALL_PLT;
609 }
610
611 bool isTPRelAddSymbol() const {
612 int64_t Imm;
613 // Must be of 'immediate' type but not a constant.
614 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
615 return false;
616
618 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
619 VK == ELF::R_RISCV_TPREL_ADD;
620 }
621
622 bool isTLSDESCCallSymbol() const {
623 int64_t Imm;
624 // Must be of 'immediate' type but not a constant.
625 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
626 return false;
627
629 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
630 VK == ELF::R_RISCV_TLSDESC_CALL;
631 }
632
633 bool isQCAccessSymbol() const {
634 int64_t Imm;
635 // Must be of 'immediate' type but not a constant.
636 if (!isExpr() || evaluateConstantExpr(getExpr(), Imm))
637 return false;
638
640 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
641 VK == RISCV::S_QC_ACCESS;
642 }
643
644 bool isCSRSystemRegister() const { return isSystemRegister(); }
645
646 // If the last operand of the vsetvli/vsetvli instruction is a constant
647 // expression, KindTy is Immediate.
648 bool isVTypeI10() const {
649 if (Kind == KindTy::VType)
650 return true;
651 return isUImm<10>();
652 }
653 bool isVTypeI11() const {
654 if (Kind == KindTy::VType)
655 return true;
656 return isUImm<11>();
657 }
658
659 bool isXSfmmVType() const {
660 return Kind == KindTy::VType && RISCVVType::isValidXSfmmVType(VType.Val);
661 }
662
663 bool isTileLambda() const {
664 return isUImmPred([](int64_t Imm) { return Imm && isUInt<3>(Imm); });
665 }
666
667 /// Return true if the operand is a valid for the fence instruction e.g.
668 /// ('iorw').
669 bool isFenceArg() const { return Kind == KindTy::Fence; }
670
671 /// Return true if the operand is a valid floating point rounding mode.
672 bool isFRMArg() const { return Kind == KindTy::FRM; }
673 bool isFRMArgLegacy() const { return Kind == KindTy::FRM; }
674 bool isRTZArg() const { return isFRMArg() && FRM.FRM == RISCVFPRndMode::RTZ; }
675
676 // Return true if the operand is a valid SpacemiT's Integer Matrix
677 // VType(i4/i8).
678 bool isSMTVType() const {
679 return Kind == KindTy::SMTVType &&
680 XSMTVTypeMode::isValidSMTVTypeMode(SMTVType.SMTVType);
681 }
682
683 bool isSMTI8() const {
684 return isSMTVType() && SMTVType.SMTVType == XSMTVTypeMode::SMT_I8;
685 }
686
687 /// Return true if the operand is a valid fli.s floating-point immediate.
688 bool isLoadFPImm() const {
689 if (isExpr())
690 return isUImm<5>();
691 if (Kind != KindTy::FPImmediate)
692 return false;
694 APFloat(APFloat::IEEEdouble(), APInt(64, getFPConst())));
695 // Don't allow decimal version of the minimum value. It is a different value
696 // for each supported data type.
697 return Idx >= 0 && Idx != 1;
698 }
699
700 bool isImmXLenLI() const {
701 int64_t Imm;
702 if (!isExpr())
703 return false;
704 // Given only Imm, ensuring that the actually specified constant is either
705 // a signed or unsigned 64-bit number is unfortunately impossible.
706 if (evaluateConstantExpr(getExpr(), Imm))
707 return isRV64Expr() || (isInt<32>(Imm) || isUInt<32>(Imm));
708
709 return RISCVAsmParser::isSymbolDiff(getExpr());
710 }
711
712 bool isImmXLenLI_Restricted() const {
713 int64_t Imm;
714 if (!isExpr())
715 return false;
716 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
717 // 'la imm' supports constant immediates only.
718 return IsConstantImm &&
719 (isRV64Expr() || (isInt<32>(Imm) || isUInt<32>(Imm)));
720 }
721
722 template <unsigned N> bool isUImm() const {
723 int64_t Imm;
724 if (!isExpr())
725 return false;
726 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
727 return IsConstantImm && isUInt<N>(Imm);
728 }
729
730 template <unsigned N, unsigned S> bool isUImmShifted() const {
731 int64_t Imm;
732 if (!isExpr())
733 return false;
734 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
735 return IsConstantImm && isShiftedUInt<N, S>(Imm);
736 }
737
738 template <class Pred> bool isUImmPred(Pred p) const {
739 int64_t Imm;
740 if (!isExpr())
741 return false;
742 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
743 return IsConstantImm && p(Imm);
744 }
745
746 bool isUImmLog2XLen() const {
747 if (isExpr() && isRV64Expr())
748 return isUImm<6>();
749 return isUImm<5>();
750 }
751
752 bool isUImmLog2XLenNonZero() const {
753 if (isExpr() && isRV64Expr())
754 return isUImmPred([](int64_t Imm) { return Imm != 0 && isUInt<6>(Imm); });
755 return isUImmPred([](int64_t Imm) { return Imm != 0 && isUInt<5>(Imm); });
756 }
757
758 bool isUImmLog2XLenHalf() const {
759 if (isExpr() && isRV64Expr())
760 return isUImm<5>();
761 return isUImm<4>();
762 }
763
764 bool isUImm5NonZero() const {
765 return isUImmPred([](int64_t Imm) { return Imm != 0 && isUInt<5>(Imm); });
766 }
767
768 bool isUImm5GT3() const {
769 return isUImmPred([](int64_t Imm) { return isUInt<5>(Imm) && Imm > 3; });
770 }
771
772 bool isUImm4Plus1() const {
773 return isUImmPred(
774 [](int64_t Imm) { return Imm > 0 && isUInt<4>(Imm - 1); });
775 }
776
777 bool isUImm5Plus1() const {
778 return isUImmPred(
779 [](int64_t Imm) { return Imm > 0 && isUInt<5>(Imm - 1); });
780 }
781
782 bool isUImm6Plus1() const {
783 return isUImmPred(
784 [](int64_t Imm) { return Imm > 0 && isUInt<6>(Imm - 1); });
785 }
786
787 bool isUImm5GE6Plus1() const {
788 return isUImmPred(
789 [](int64_t Imm) { return Imm >= 6 && isUInt<5>(Imm - 1); });
790 }
791
792 bool isUImm5Slist() const {
793 return isUImmPred([](int64_t Imm) {
794 return (Imm == 0) || (Imm == 1) || (Imm == 2) || (Imm == 4) ||
795 (Imm == 8) || (Imm == 16) || (Imm == 15) || (Imm == 31);
796 });
797 }
798
799 bool isUImm8GE32() const {
800 return isUImmPred([](int64_t Imm) { return isUInt<8>(Imm) && Imm >= 32; });
801 }
802
803 bool isRnumArg() const {
804 return isUImmPred(
805 [](int64_t Imm) { return Imm >= INT64_C(0) && Imm <= INT64_C(10); });
806 }
807
808 bool isRnumArg_0_7() const {
809 return isUImmPred(
810 [](int64_t Imm) { return Imm >= INT64_C(0) && Imm <= INT64_C(7); });
811 }
812
813 bool isRnumArg_1_10() const {
814 return isUImmPred(
815 [](int64_t Imm) { return Imm >= INT64_C(1) && Imm <= INT64_C(10); });
816 }
817
818 bool isRnumArg_2_14() const {
819 return isUImmPred(
820 [](int64_t Imm) { return Imm >= INT64_C(2) && Imm <= INT64_C(14); });
821 }
822
823 template <unsigned N> bool isSImm() const {
824 int64_t Imm;
825 if (!isExpr())
826 return false;
827 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
828 return IsConstantImm && isInt<N>(fixImmediateForRV32(Imm, isRV64Expr()));
829 }
830
831 bool isYBNDSWImm() const {
832 if (!isExpr())
833 return false;
834
835 int64_t Imm;
836 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
837 return IsConstantImm && RISCV::isValidYBNDSWImm(Imm);
838 }
839
840 template <class Pred> bool isSImmPred(Pred p) const {
841 int64_t Imm;
842 if (!isExpr())
843 return false;
844 bool IsConstantImm = evaluateConstantExpr(getExpr(), Imm);
845 return IsConstantImm && p(fixImmediateForRV32(Imm, isRV64Expr()));
846 }
847
848 bool isSImm5NonZero() const {
849 return isSImmPred([](int64_t Imm) { return Imm != 0 && isInt<5>(Imm); });
850 }
851
852 bool isSImm6NonZero() const {
853 return isSImmPred([](int64_t Imm) { return Imm != 0 && isInt<6>(Imm); });
854 }
855
856 bool isCLUIImm() const {
857 return isUImmPred([](int64_t Imm) {
858 return (isUInt<5>(Imm) && Imm != 0) || (Imm >= 0xfffe0 && Imm <= 0xfffff);
859 });
860 }
861
862 bool isUImm10Lsb00NonZero() const {
863 return isUImmPred(
864 [](int64_t Imm) { return isShiftedUInt<8, 2>(Imm) && (Imm != 0); });
865 }
866
867 // If this a RV32 and the immediate is a uimm32, sign extend it to 32 bits.
868 // This allows writing 'addi a0, a0, 0xffffffff'.
869 static int64_t fixImmediateForRV32(int64_t Imm, bool IsRV64Imm) {
870 if (IsRV64Imm || !isUInt<32>(Imm))
871 return Imm;
872 return SignExtend64<32>(Imm);
873 }
874
875 bool isSImm12LO() const {
876 if (!isExpr())
877 return false;
878
879 int64_t Imm;
880 if (evaluateConstantExpr(getExpr(), Imm))
881 return isInt<12>(fixImmediateForRV32(Imm, isRV64Expr()));
882
884 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
885 (VK == RISCV::S_LO || VK == RISCV::S_PCREL_LO ||
886 VK == RISCV::S_TPREL_LO || VK == ELF::R_RISCV_TLSDESC_LOAD_LO12 ||
887 VK == ELF::R_RISCV_TLSDESC_ADD_LO12);
888 }
889
890 /// Returns NoMatch rather than the NearMatch of the underlying predicate
891 /// for anything that is not an immediate at all (such as the '(' token of an
892 /// offset-less memory operand). This lets the matcher skip this optional
893 /// operand and insert the default 0 offset. An immediate that fails Pred
894 /// (e.g. out of range) still reports the wrapped class diagnostic.
895 template <bool (RISCVOperand::*Pred)() const>
896 DiagnosticPredicate isOptionalMemOffset() const {
897 if (!isImm())
899 return (this->*Pred)() ? DiagnosticPredicate::Match
900 : DiagnosticPredicate::NearMatch;
901 }
902
903 bool isSImm12Lsb00000() const {
904 return isSImmPred([](int64_t Imm) { return isShiftedInt<7, 5>(Imm); });
905 }
906
907 bool isSImm10Lsb0000NonZero() const {
908 return isSImmPred(
909 [](int64_t Imm) { return Imm != 0 && isShiftedInt<6, 4>(Imm); });
910 }
911
912 bool isSImm16NonZero() const {
913 return isSImmPred([](int64_t Imm) { return Imm != 0 && isInt<16>(Imm); });
914 }
915
916 bool isUImm16NonZero() const {
917 return isUImmPred([](int64_t Imm) { return isUInt<16>(Imm) && Imm != 0; });
918 }
919
920 bool isSImm20LI() const {
921 if (!isExpr())
922 return false;
923
924 int64_t Imm;
925 if (evaluateConstantExpr(getExpr(), Imm))
926 return isInt<20>(fixImmediateForRV32(Imm, isRV64Expr()));
927
929 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
930 VK == RISCV::S_QC_ABS20;
931 }
932
933 bool isSImm8PLI_B() const { return isSImm<8>() || isUImm<8>(); }
934 bool isSImm10PLUI() const { return isSImm<10>() || isUImm<10>(); }
935
936 bool isSImm10PLI_H() const {
937 return isSImm<10>() || isUImmPred([](int64_t Imm) {
939 });
940 }
941 bool isSImm10PLI_W() const {
942 return isSImm<10>() || isUImmPred([](int64_t Imm) {
944 });
945 }
946
947 bool isUImm20LUI() const {
948 if (!isExpr())
949 return false;
950
951 int64_t Imm;
952 if (evaluateConstantExpr(getExpr(), Imm))
953 return isUInt<20>(Imm);
954
956 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
957 (VK == ELF::R_RISCV_HI20 || VK == ELF::R_RISCV_TPREL_HI20);
958 }
959
960 bool isUImm20AUIPC() const {
961 if (!isExpr())
962 return false;
963
964 int64_t Imm;
965 if (evaluateConstantExpr(getExpr(), Imm))
966 return isUInt<20>(Imm);
967
969 return RISCVAsmParser::classifySymbolRef(getExpr(), VK) &&
970 (VK == RISCV::S_PCREL_HI || VK == RISCV::S_GOT_HI ||
971 VK == ELF::R_RISCV_TLS_GOT_HI20 || VK == ELF::R_RISCV_TLS_GD_HI20 ||
972 VK == ELF::R_RISCV_TLSDESC_HI20);
973 }
974
975 bool isImmZero() const {
976 return isUImmPred([](int64_t Imm) { return 0 == Imm; });
977 }
978
979 bool isImmThree() const {
980 return isUImmPred([](int64_t Imm) { return 3 == Imm; });
981 }
982
983 bool isImmFour() const {
984 return isUImmPred([](int64_t Imm) { return 4 == Imm; });
985 }
986
987 bool isImm5Zibi() const {
988 return isUImmPred(
989 [](int64_t Imm) { return (Imm != 0 && isUInt<5>(Imm)) || Imm == -1; });
990 }
991
992 bool isSImm5Plus1() const {
993 return isSImmPred(
994 [](int64_t Imm) { return Imm != INT64_MIN && isInt<5>(Imm - 1); });
995 }
996
997 bool isSImm18Lsb0() const {
998 return isSImmPred([](int64_t Imm) { return isShiftedInt<17, 1>(Imm); });
999 }
1000
1001 bool isSImm19Lsb00() const {
1002 return isSImmPred([](int64_t Imm) { return isShiftedInt<17, 2>(Imm); });
1003 }
1004
1005 bool isSImm20Lsb000() const {
1006 return isSImmPred([](int64_t Imm) { return isShiftedInt<17, 3>(Imm); });
1007 }
1008
1009 bool isSImm32Lsb0() const {
1010 return isSImmPred([](int64_t Imm) { return isShiftedInt<31, 1>(Imm); });
1011 }
1012
1013 /// getStartLoc - Gets location of the first token of this operand
1014 SMLoc getStartLoc() const override { return StartLoc; }
1015 /// getEndLoc - Gets location of the last token of this operand
1016 SMLoc getEndLoc() const override { return EndLoc; }
1017
1018 /// True if this operand is for an RV64 instruction
1019 bool isRV64Expr() const {
1020 assert(Kind == KindTy::Expression && "Invalid type access!");
1021 return Expr.IsRV64;
1022 }
1023
1024 MCRegister getReg() const override {
1025 assert(Kind == KindTy::Register && "Invalid type access!");
1026 return Reg;
1027 }
1028
1029 StringRef getSysReg() const {
1030 assert(Kind == KindTy::SystemRegister && "Invalid type access!");
1031 return StringRef(SysReg.Data, SysReg.Length);
1032 }
1033
1034 const MCExpr *getExpr() const {
1035 assert(Kind == KindTy::Expression && "Invalid type access!");
1036 return Expr.Expr;
1037 }
1038
1039 uint64_t getFPConst() const {
1040 assert(Kind == KindTy::FPImmediate && "Invalid type access!");
1041 return FPImm.Val;
1042 }
1043
1044 StringRef getToken() const {
1045 assert(Kind == KindTy::Token && "Invalid type access!");
1046 return Tok;
1047 }
1048
1049 unsigned getVType() const {
1050 assert(Kind == KindTy::VType && "Invalid type access!");
1051 return VType.Val;
1052 }
1053
1054 RISCVFPRndMode::RoundingMode getFRM() const {
1055 assert(Kind == KindTy::FRM && "Invalid type access!");
1056 return FRM.FRM;
1057 }
1058
1059 unsigned getFence() const {
1060 assert(Kind == KindTy::Fence && "Invalid type access!");
1061 return Fence.Val;
1062 }
1063
1064 XSMTVTypeMode::SMTVTypeMode getSMTVType() const {
1065 assert(Kind == KindTy::SMTVType && "Invalid type access!");
1066 return SMTVType.SMTVType;
1067 }
1068
1069 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
1070 const RISCVMCOptions &CLOpts = RISCV_MC::getCLOpts(MAI.getTargetOptions());
1071 auto RegName = [&](MCRegister Reg) {
1072 if (Reg)
1073 return RISCVInstPrinter::getRegisterName(CLOpts, Reg);
1074 else
1075 return "noreg";
1076 };
1077
1078 switch (Kind) {
1079 case KindTy::Expression:
1080 OS << "<imm: ";
1081 MAI.printExpr(OS, *Expr.Expr);
1082 OS << ' ' << (Expr.IsRV64 ? "rv64" : "rv32") << '>';
1083 break;
1084 case KindTy::FPImmediate:
1085 OS << "<fpimm: " << FPImm.Val << ">";
1086 break;
1087 case KindTy::Register:
1088 OS << "<reg: " << RegName(Reg) << " (" << Reg.id() << ")>";
1089 break;
1090 case KindTy::Token:
1091 OS << "'" << getToken() << "'";
1092 break;
1093 case KindTy::SystemRegister:
1094 OS << "<sysreg: " << getSysReg() << " (" << SysReg.Encoding << ")>";
1095 break;
1096 case KindTy::VType:
1097 OS << "<vtype: ";
1098 RISCVVType::printVType(getVType(), OS);
1099 OS << '>';
1100 break;
1101 case KindTy::FRM:
1102 OS << "<frm: ";
1103 OS << roundingModeToString(getFRM());
1104 OS << '>';
1105 break;
1106 case KindTy::SMTVType:
1107 OS << "<smtvtype: ";
1108 OS << SMTVTypeModeToString(getSMTVType());
1109 OS << '>';
1110 break;
1111 case KindTy::Fence:
1112 OS << "<fence: ";
1113 OS << getFence();
1114 OS << '>';
1115 break;
1116 case KindTy::RegList:
1117 OS << "<reglist: ";
1118 RISCVZC::printRegList(RegList.Encoding, OS);
1119 OS << '>';
1120 break;
1121 case KindTy::StackAdj:
1122 OS << "<stackadj: ";
1123 OS << StackAdj.Val;
1124 OS << '>';
1125 break;
1126 case KindTy::RegReg:
1127 OS << "<RegReg: BaseReg " << RegName(RegReg.BaseReg) << " OffsetReg "
1128 << RegName(RegReg.OffsetReg);
1129 break;
1130 }
1131 }
1132
1133 static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S) {
1134 auto Op = std::make_unique<RISCVOperand>(KindTy::Token);
1135 Op->Tok = Str;
1136 Op->StartLoc = S;
1137 Op->EndLoc = S;
1138 return Op;
1139 }
1140
1141 static std::unique_ptr<RISCVOperand> createReg(MCRegister Reg, SMLoc S,
1142 SMLoc E) {
1143 auto Op = std::make_unique<RISCVOperand>(KindTy::Register);
1144 Op->Reg = Reg;
1145 Op->StartLoc = S;
1146 Op->EndLoc = E;
1147 return Op;
1148 }
1149
1150 static std::unique_ptr<RISCVOperand> createExpr(const MCExpr *Val, SMLoc S,
1151 SMLoc E, bool IsRV64) {
1152 auto Op = std::make_unique<RISCVOperand>(KindTy::Expression);
1153 Op->Expr.Expr = Val;
1154 Op->Expr.IsRV64 = IsRV64;
1155 Op->StartLoc = S;
1156 Op->EndLoc = E;
1157 return Op;
1158 }
1159
1160 static std::unique_ptr<RISCVOperand> createFPImm(uint64_t Val, SMLoc S) {
1161 auto Op = std::make_unique<RISCVOperand>(KindTy::FPImmediate);
1162 Op->FPImm.Val = Val;
1163 Op->StartLoc = S;
1164 Op->EndLoc = S;
1165 return Op;
1166 }
1167
1168 static std::unique_ptr<RISCVOperand> createSysReg(StringRef Str, SMLoc S,
1169 unsigned Encoding) {
1170 auto Op = std::make_unique<RISCVOperand>(KindTy::SystemRegister);
1171 Op->SysReg.Data = Str.data();
1172 Op->SysReg.Length = Str.size();
1173 Op->SysReg.Encoding = Encoding;
1174 Op->StartLoc = S;
1175 Op->EndLoc = S;
1176 return Op;
1177 }
1178
1179 static std::unique_ptr<RISCVOperand>
1180 createFRMArg(RISCVFPRndMode::RoundingMode FRM, SMLoc S) {
1181 auto Op = std::make_unique<RISCVOperand>(KindTy::FRM);
1182 Op->FRM.FRM = FRM;
1183 Op->StartLoc = S;
1184 Op->EndLoc = S;
1185 return Op;
1186 }
1187
1188 static std::unique_ptr<RISCVOperand>
1189 createSMTVType(XSMTVTypeMode::SMTVTypeMode VType, SMLoc S) {
1190 auto Op = std::make_unique<RISCVOperand>(KindTy::SMTVType);
1191 Op->SMTVType.SMTVType = VType;
1192 Op->StartLoc = S;
1193 Op->EndLoc = S;
1194 return Op;
1195 }
1196
1197 static std::unique_ptr<RISCVOperand> createFenceArg(unsigned Val, SMLoc S) {
1198 auto Op = std::make_unique<RISCVOperand>(KindTy::Fence);
1199 Op->Fence.Val = Val;
1200 Op->StartLoc = S;
1201 Op->EndLoc = S;
1202 return Op;
1203 }
1204
1205 static std::unique_ptr<RISCVOperand> createVType(unsigned VTypeI, SMLoc S) {
1206 auto Op = std::make_unique<RISCVOperand>(KindTy::VType);
1207 Op->VType.Val = VTypeI;
1208 Op->StartLoc = S;
1209 Op->EndLoc = S;
1210 return Op;
1211 }
1212
1213 static std::unique_ptr<RISCVOperand> createRegList(unsigned RlistEncode,
1214 SMLoc S) {
1215 auto Op = std::make_unique<RISCVOperand>(KindTy::RegList);
1216 Op->RegList.Encoding = RlistEncode;
1217 Op->StartLoc = S;
1218 return Op;
1219 }
1220
1221 static std::unique_ptr<RISCVOperand>
1222 createRegReg(MCRegister BaseReg, MCRegister OffsetReg, SMLoc S) {
1223 auto Op = std::make_unique<RISCVOperand>(KindTy::RegReg);
1224 Op->RegReg.BaseReg = BaseReg;
1225 Op->RegReg.OffsetReg = OffsetReg;
1226 Op->StartLoc = S;
1227 Op->EndLoc = S;
1228 return Op;
1229 }
1230
1231 static std::unique_ptr<RISCVOperand> createStackAdj(unsigned StackAdj, SMLoc S) {
1232 auto Op = std::make_unique<RISCVOperand>(KindTy::StackAdj);
1233 Op->StackAdj.Val = StackAdj;
1234 Op->StartLoc = S;
1235 return Op;
1236 }
1237
1238 static void addExpr(MCInst &Inst, const MCExpr *Expr, bool IsRV64Imm) {
1239 assert(Expr && "Expr shouldn't be null!");
1240 int64_t Imm = 0;
1241 bool IsConstant = evaluateConstantExpr(Expr, Imm);
1242
1243 if (IsConstant)
1244 Inst.addOperand(
1245 MCOperand::createImm(fixImmediateForRV32(Imm, IsRV64Imm)));
1246 else
1248 }
1249
1250 // Used by the TableGen Code
1251 void addRegOperands(MCInst &Inst, unsigned N) const {
1252 assert(N == 1 && "Invalid number of operands!");
1254 }
1255
1256 void addGPRAsFPR16Operands(MCInst &Inst, unsigned N) const {
1257 assert(N == 1 && "Invalid number of operands!");
1258 Inst.addOperand(MCOperand::createReg(getReg() - RISCV::X0 + RISCV::X0_H));
1259 }
1260
1261 void addGPRAsFPR32Operands(MCInst &Inst, unsigned N) const {
1262 assert(N == 1 && "Invalid number of operands!");
1263 Inst.addOperand(MCOperand::createReg(getReg() - RISCV::X0 + RISCV::X0_W));
1264 }
1265
1266 void addGPRPairOperands(MCInst &Inst, unsigned N) const;
1267
1268 void addImmOperands(MCInst &Inst, unsigned N) const {
1269 assert(N == 1 && "Invalid number of operands!");
1270 addExpr(Inst, getExpr(), isRV64Expr());
1271 }
1272
1273 template <unsigned Bits>
1274 void addSExtImmOperands(MCInst &Inst, unsigned N) const {
1275 assert(N == 1 && "Invalid number of operands!");
1276 int64_t Imm;
1277 [[maybe_unused]] bool IsConstant = evaluateConstantExpr(getExpr(), Imm);
1278 assert(IsConstant);
1280 }
1281
1282 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1283 assert(N == 1 && "Invalid number of operands!");
1284 if (isExpr()) {
1285 addExpr(Inst, getExpr(), isRV64Expr());
1286 return;
1287 }
1288
1290 APFloat(APFloat::IEEEdouble(), APInt(64, getFPConst())));
1292 }
1293
1294 void addFenceArgOperands(MCInst &Inst, unsigned N) const {
1295 assert(N == 1 && "Invalid number of operands!");
1297 }
1298
1299 void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
1300 assert(N == 1 && "Invalid number of operands!");
1301 Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
1302 }
1303
1304 // Support non-canonical syntax:
1305 // "vsetivli rd, uimm, 0xabc" or "vsetvli rd, rs1, 0xabc"
1306 // "vsetivli rd, uimm, (0xc << N)" or "vsetvli rd, rs1, (0xc << N)"
1307 void addVTypeIOperands(MCInst &Inst, unsigned N) const {
1308 assert(N == 1 && "Invalid number of operands!");
1309 int64_t Imm = 0;
1310 if (Kind == KindTy::Expression) {
1311 [[maybe_unused]] bool IsConstantImm =
1312 evaluateConstantExpr(getExpr(), Imm);
1313 assert(IsConstantImm && "Invalid VTypeI Operand!");
1314 } else {
1315 Imm = getVType();
1316 }
1318 }
1319
1320 void addRegListOperands(MCInst &Inst, unsigned N) const {
1321 assert(N == 1 && "Invalid number of operands!");
1322 Inst.addOperand(MCOperand::createImm(RegList.Encoding));
1323 }
1324
1325 void addRegRegOperands(MCInst &Inst, unsigned N) const {
1326 assert(N == 2 && "Invalid number of operands!");
1327 Inst.addOperand(MCOperand::createReg(RegReg.BaseReg));
1328 Inst.addOperand(MCOperand::createReg(RegReg.OffsetReg));
1329 }
1330
1331 void addStackAdjOperands(MCInst &Inst, unsigned N) const {
1332 assert(N == 1 && "Invalid number of operands!");
1333 Inst.addOperand(MCOperand::createImm(StackAdj.Val));
1334 }
1335
1336 void addFRMArgOperands(MCInst &Inst, unsigned N) const {
1337 assert(N == 1 && "Invalid number of operands!");
1338 Inst.addOperand(MCOperand::createImm(getFRM()));
1339 }
1340
1341 void addSMTVTypeOperand(MCInst &Inst, unsigned N) const {
1342 assert(N == 1 && "Invalid number of operands!");
1343 Inst.addOperand(MCOperand::createImm(getSMTVType()));
1344 }
1345};
1346} // end anonymous namespace.
1347
1348#define GET_REGISTER_MATCHER
1349#define GET_SUBTARGET_FEATURE_NAME
1350#define GET_MATCHER_IMPLEMENTATION
1351#define GET_MNEMONIC_SPELL_CHECKER
1352#include "RISCVGenAsmMatcher.inc"
1353
1354void RISCVOperand::addGPRPairOperands(MCInst &Inst, unsigned N) const {
1355 assert(N == 1 && "Invalid number of operands!");
1356 Inst.addOperand(MCOperand::createReg(convertToGPRPair(getReg())));
1357}
1358
1359bool RISCVAsmParser::generateImmOutOfRangeError(
1360 SMLoc ErrorLoc, int64_t Lower, int64_t Upper,
1361 const Twine &Msg = "immediate must be an integer in the range") {
1362 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
1363}
1364
1365// Some diagnostics need to vary with subtarget features, so they are handled
1366// here. For example, several immediate ranges depend on whether the target is
1367// RV32 or RV64.
1368std::string RISCVAsmParser::getCustomOperandDiag(unsigned MatchError) {
1369 auto Range = [](int64_t Lower, int64_t Upper,
1370 StringRef Msg = "immediate must be an integer in the range") {
1371 return (Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]").str();
1372 };
1373
1374 switch (MatchError) {
1375 default:
1376 // For all other operand diagnostics, use the static string generated by
1377 // TableGen from the DiagnosticString field, if any.
1378 if (const char *Diag = getMatchKindDiag((RISCVMatchResultTy)MatchError))
1379 return Diag;
1380 return std::string();
1381 case Match_InvalidImmXLenLI:
1382 if (isRV64())
1383 return "operand must be a constant 64-bit integer";
1384 return Range(std::numeric_limits<int32_t>::min(),
1385 std::numeric_limits<uint32_t>::max());
1386 case Match_InvalidImmXLenLI_Restricted:
1387 if (isRV64())
1388 return "operand either must be a constant 64-bit integer "
1389 "or a bare symbol name";
1390 return Range(std::numeric_limits<int32_t>::min(),
1391 std::numeric_limits<uint32_t>::max(),
1392 "operand either must be a bare symbol name or an immediate "
1393 "integer in the range");
1394 case Match_InvalidUImmLog2XLen:
1395 if (isRV64())
1396 return Range(0, (1 << 6) - 1);
1397 return Range(0, (1 << 5) - 1);
1398 case Match_InvalidUImmLog2XLenNonZero:
1399 if (isRV64())
1400 return Range(1, (1 << 6) - 1);
1401 return Range(1, (1 << 5) - 1);
1402 case Match_InvalidUImm1:
1403 return Range(0, (1 << 1) - 1);
1404 case Match_InvalidUImm2:
1405 return Range(0, (1 << 2) - 1);
1406 case Match_InvalidUImm2Lsb0:
1407 return Range(0, 2, "immediate must be one of");
1408 case Match_InvalidUImm3:
1409 return Range(0, (1 << 3) - 1);
1410 case Match_InvalidUImm4:
1411 return Range(0, (1 << 4) - 1);
1412 case Match_InvalidUImm4Plus1:
1413 return Range(1, (1 << 4));
1414 case Match_InvalidUImm5:
1415 return Range(0, (1 << 5) - 1);
1416 case Match_InvalidUImm5NonZero:
1417 return Range(1, (1 << 5) - 1);
1418 case Match_InvalidUImm5GT3:
1419 return Range(4, (1 << 5) - 1);
1420 case Match_InvalidUImm5Plus1:
1421 return Range(1, (1 << 5));
1422 case Match_InvalidUImm5GE6Plus1:
1423 return Range(6, (1 << 5));
1424 case Match_InvalidUImm5Slist:
1425 return "immediate must be one of: 0, 1, 2, 4, 8, 15, 16, 31";
1426 case Match_InvalidUImm6:
1427 return Range(0, (1 << 6) - 1);
1428 case Match_InvalidUImm6Plus1:
1429 return Range(1, (1 << 6));
1430 case Match_InvalidUImm7:
1431 return Range(0, (1 << 7) - 1);
1432 case Match_InvalidUImm8:
1433 return Range(0, (1 << 8) - 1);
1434 case Match_InvalidUImm8GE32:
1435 return Range(32, (1 << 8) - 1);
1436 case Match_InvalidSImm5:
1437 return Range(-(1 << 4), (1 << 4) - 1);
1438 case Match_InvalidSImm5NonZero:
1439 return Range(-(1 << 4), (1 << 4) - 1,
1440 "immediate must be non-zero in the range");
1441 case Match_InvalidSImm6:
1442 return Range(-(1 << 5), (1 << 5) - 1);
1443 case Match_InvalidSImm6NonZero:
1444 return Range(-(1 << 5), (1 << 5) - 1,
1445 "immediate must be non-zero in the range");
1446 case Match_InvalidCLUIImm:
1447 return Range(1, (1 << 5) - 1, "immediate must be in [0xfffe0, 0xfffff] or");
1448 case Match_InvalidUImm5Lsb0:
1449 return Range(0, (1 << 5) - 2,
1450 "immediate must be a multiple of 2 bytes in the range");
1451 case Match_InvalidUImm6Lsb0:
1452 return Range(0, (1 << 6) - 2,
1453 "immediate must be a multiple of 2 bytes in the range");
1454 case Match_InvalidUImm6Lsb000:
1455 return Range(0, (1 << 6) - 8,
1456 "immediate must be a multiple of 8 in the range");
1457 case Match_InvalidUImm7Lsb00:
1458 return Range(0, (1 << 7) - 4,
1459 "immediate must be a multiple of 4 bytes in the range");
1460 case Match_InvalidUImm8Lsb00:
1461 return Range(0, (1 << 8) - 4,
1462 "immediate must be a multiple of 4 bytes in the range");
1463 case Match_InvalidUImm8Lsb000:
1464 return Range(0, (1 << 8) - 8,
1465 "immediate must be a multiple of 8 bytes in the range");
1466 case Match_InvalidUImm9:
1467 return Range(0, (1 << 9) - 1, "immediate offset must be in the range");
1468 case Match_InvalidBareSImm9Lsb0:
1469 return Range(-(1 << 8), (1 << 8) - 2,
1470 "immediate must be a multiple of 2 bytes in the range");
1471 case Match_InvalidUImm9Lsb000:
1472 return Range(0, (1 << 9) - 8,
1473 "immediate must be a multiple of 8 bytes in the range");
1474 case Match_InvalidSImm8PLI_B:
1475 return Range(-(1 << 7), (1 << 8) - 1);
1476 case Match_InvalidSImm10:
1477 case Match_InvalidSImm10PLI_H:
1478 case Match_InvalidSImm10PLI_W:
1479 return Range(-(1 << 9), (1 << 9) - 1);
1480 case Match_InvalidSImm10PLUI:
1481 return Range(-(1 << 9), (1 << 10) - 1);
1482 case Match_InvalidUImm10Lsb00NonZero:
1483 return Range(4, (1 << 10) - 4,
1484 "immediate must be a multiple of 4 bytes in the range");
1485 case Match_InvalidSImm10Lsb0000NonZero:
1486 return Range(
1487 -(1 << 9), (1 << 9) - 16,
1488 "immediate must be a multiple of 16 bytes and non-zero in the range");
1489 case Match_InvalidSImm11:
1490 return Range(-(1 << 10), (1 << 10) - 1);
1491 case Match_InvalidBareSImm11Lsb0:
1492 return Range(-(1 << 10), (1 << 10) - 2,
1493 "immediate must be a multiple of 2 bytes in the range");
1494 case Match_InvalidUImm10:
1495 return Range(0, (1 << 10) - 1);
1496 case Match_InvalidUImm11:
1497 return Range(0, (1 << 11) - 1);
1498 case Match_InvalidUImm14Lsb00:
1499 return Range(0, (1 << 14) - 4,
1500 "immediate must be a multiple of 4 bytes in the range");
1501 case Match_InvalidUImm16NonZero:
1502 return Range(1, (1 << 16) - 1);
1503 case Match_InvalidSImm12:
1504 return Range(-(1 << 11), (1 << 11) - 1);
1505 case Match_InvalidSImm12LO:
1506 return Range(-(1 << 11), (1 << 11) - 1,
1507 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo "
1508 "specifier or an integer in the range");
1509 case Match_InvalidBareSImm12Lsb0:
1510 return Range(-(1 << 11), (1 << 11) - 2,
1511 "immediate must be a multiple of 2 bytes in the range");
1512 case Match_InvalidSImm12Lsb00000:
1513 return Range(-(1 << 11), (1 << 11) - 32,
1514 "immediate must be a multiple of 32 bytes in the range");
1515 case Match_InvalidBareSImm13Lsb0:
1516 return Range(-(1 << 12), (1 << 12) - 2,
1517 "immediate must be a multiple of 2 bytes in the range");
1518 case Match_InvalidSImm16:
1519 return Range(-(1 << 15), (1 << 15) - 1);
1520 case Match_InvalidSImm16NonZero:
1521 return Range(-(1 << 15), (1 << 15) - 1,
1522 "immediate must be non-zero in the range");
1523 case Match_InvalidSImm20LI:
1524 return Range(-(1 << 19), (1 << 19) - 1,
1525 "operand must be a symbol with a %qc.abs20 specifier or an "
1526 "integer in the range");
1527 case Match_InvalidUImm20LUI:
1528 return Range(0, (1 << 20) - 1,
1529 "operand must be a symbol with %hi/%tprel_hi specifier or an "
1530 "integer in the range");
1531 case Match_InvalidUImm20:
1532 return Range(0, (1 << 20) - 1);
1533 case Match_InvalidUImm20AUIPC:
1534 return Range(
1535 0, (1 << 20) - 1,
1536 "operand must be a symbol with a "
1537 "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi specifier "
1538 "or an integer in the range");
1539 case Match_InvalidBareSImm21Lsb0:
1540 return Range(-(1 << 20), (1 << 20) - 2,
1541 "immediate must be a multiple of 2 bytes in the range");
1542 case Match_InvalidCSRSystemRegister:
1543 return Range(0, (1 << 12) - 1,
1544 "operand must be a valid system register name or an integer "
1545 "in the range");
1546 case Match_InvalidImm5Zibi:
1547 return Range(-1, (1 << 5) - 1, "immediate must be non-zero in the range");
1548 case Match_InvalidVTypeI:
1549 return "operand must be "
1550 "e[8|8alt|16|16alt|32|64],m[1|2|4|8|f2|f4|f8],[ta|tu],[ma|mu]";
1551 case Match_InvalidSImm5Plus1:
1552 return Range(-(1 << 4) + 1, (1 << 4), "immediate must be in the range");
1553 case Match_InvalidSImm18:
1554 return Range(-(1 << 17), (1 << 17) - 1);
1555 case Match_InvalidSImm18Lsb0:
1556 return Range(-(1 << 17), (1 << 17) - 2,
1557 "immediate must be a multiple of 2 bytes in the range");
1558 case Match_InvalidSImm19Lsb00:
1559 return Range(-(1 << 18), (1 << 18) - 4,
1560 "immediate must be a multiple of 4 bytes in the range");
1561 case Match_InvalidSImm20Lsb000:
1562 return Range(-(1 << 19), (1 << 19) - 8,
1563 "immediate must be a multiple of 8 bytes in the range");
1564 case Match_InvalidSImm26:
1565 return Range(-(1 << 25), (1 << 25) - 1);
1566 // HACK: See comment before `BareSymbolQC_E_LI` in RISCVInstrInfoXqci.td.
1567 case Match_InvalidBareSymbolQC_E_LI:
1568 [[fallthrough]];
1569 // END HACK
1570 case Match_InvalidBareSImm32:
1571 return Range(std::numeric_limits<int32_t>::min(),
1572 std::numeric_limits<uint32_t>::max());
1573 case Match_InvalidBareSImm32Lsb0:
1574 return Range(std::numeric_limits<int32_t>::min(),
1575 std::numeric_limits<int32_t>::max() - 1,
1576 "operand must be a multiple of 2 bytes in the range");
1577 case Match_InvalidRnumArg:
1578 return Range(0, 10);
1579 case Match_InvalidStackAdj:
1580 return "stack adjustment is invalid for this instruction and register "
1581 "list";
1582 case Match_InvalidYBNDSWImm:
1583 return "immediate must be an integer in the range "
1584 "[1, 255], a multiple of 8 in the range [256, 504], "
1585 "or a multiple of 16 in the range [512, 4096]";
1586 }
1587}
1588
1589// Process the list of near-misses, throwing away ones we don't want to report
1590// to the user, and converting the rest to a source location and string that
1591// should be reported.
1592void RISCVAsmParser::FilterNearMisses(
1593 SmallVectorImpl<NearMissInfo> &NearMissesIn,
1594 SmallVectorImpl<NearMissMessage> &NearMissesOut, SMLoc IDLoc,
1596 // Record some information about near-misses that we have already seen, so
1597 // that we can avoid reporting redundant ones.
1598 std::multimap<unsigned, unsigned> OperandMissesSeen;
1599 SmallSet<FeatureBitset, 4> FeatureMissesSeen;
1600 bool ReportedTooFewOperands = false;
1601 bool ReportedTooManyOperands = false;
1602
1603 for (NearMissInfo &I : NearMissesIn) {
1604 switch (I.getKind()) {
1606 SMLoc OperandLoc =
1607 ((RISCVOperand &)*Operands[I.getOperandIndex()]).getStartLoc();
1608
1609 // When the matcher finds surplus operands, it records them as
1610 // NearMissOperand with InvalidMatchClass. We detect this and report
1611 // "unexpected extra operand" instead of "invalid operand".
1612 if (I.getOperandClass() == InvalidMatchClass) {
1613 if (!ReportedTooManyOperands) {
1614 NearMissesOut.emplace_back(NearMissMessage{
1615 OperandLoc, "unexpected extra operand for instruction"});
1616 ReportedTooManyOperands = true;
1617 }
1618 break;
1619 }
1620
1621 std::string OperandDiag = getCustomOperandDiag(I.getOperandError());
1622
1623 // If we have already emitted a message for a superclass on this operand,
1624 // don't also report the sub-class.
1625 unsigned DupCheckMatchClass =
1626 OperandDiag.empty() ? ~0U : I.getOperandClass();
1627 auto PrevReports = OperandMissesSeen.equal_range(I.getOperandIndex());
1628 if (std::any_of(
1629 PrevReports.first, PrevReports.second,
1630 [DupCheckMatchClass](const std::pair<unsigned, unsigned> Pair) {
1631 if (DupCheckMatchClass == ~0U || Pair.second == ~0U)
1632 return Pair.second == DupCheckMatchClass;
1633 return isSubclass((MatchClassKind)DupCheckMatchClass,
1634 (MatchClassKind)Pair.second);
1635 }))
1636 break;
1637 OperandMissesSeen.insert(
1638 std::make_pair(I.getOperandIndex(), DupCheckMatchClass));
1639
1640 NearMissMessage Message;
1641 Message.Loc = OperandLoc;
1642 if (!OperandDiag.empty()) {
1643 Message.Message = OperandDiag;
1644 } else {
1645 Message.Message = "invalid operand for instruction";
1646 LLVM_DEBUG(
1647 dbgs() << "Missing diagnostic string for operand class "
1648 << getMatchClassName((MatchClassKind)I.getOperandClass())
1649 << I.getOperandClass() << ", error " << I.getOperandError()
1650 << ", opcode " << MII.getName(I.getOpcode()) << "\n");
1651 }
1652 NearMissesOut.emplace_back(Message);
1653 break;
1654 }
1656 const FeatureBitset &MissingFeatures = I.getFeatures();
1657 // Don't report the same set of features twice.
1658 if (!FeatureMissesSeen.insert(MissingFeatures).second)
1659 break;
1660
1661 NearMissMessage Message;
1662 Message.Loc = IDLoc;
1663 bool FirstFeature = true;
1664 Message.Message = "instruction requires the following:";
1665 for (unsigned Feature : MissingFeatures) {
1666 Message.Message += FirstFeature ? " " : ", ";
1667 Message.Message += getSubtargetFeatureName(Feature);
1668 FirstFeature = false;
1669 }
1670 NearMissesOut.emplace_back(Message);
1671 break;
1672 }
1674 // RISC-V does not define any target match predicates.
1675 llvm_unreachable("RISC-V has no target predicate near-misses");
1676 break;
1678 if (!ReportedTooFewOperands) {
1679 SMLoc EndLoc = ((RISCVOperand &)*Operands.back()).getEndLoc();
1680 NearMissesOut.emplace_back(
1681 NearMissMessage{EndLoc, "too few operands for instruction"});
1682 ReportedTooFewOperands = true;
1683 }
1684 break;
1685 }
1687 // This should never leave the matcher.
1688 llvm_unreachable("not a near-miss");
1689 break;
1690 }
1691 }
1692}
1693
1694void RISCVAsmParser::ReportNearMisses(SmallVectorImpl<NearMissInfo> &NearMisses,
1695 SMLoc IDLoc, OperandVector &Operands) {
1697 FilterNearMisses(NearMisses, Messages, IDLoc, Operands);
1698
1699 if (Messages.empty()) {
1700 // No near-misses were found, so the best we can do is "invalid
1701 // instruction".
1702 Error(IDLoc, "invalid instruction");
1703 } else if (Messages.size() == 1) {
1704 // One near miss was found, report it as the sole error.
1705 Error(Messages[0].Loc, Messages[0].Message);
1706 } else {
1707 // More than one near miss, so report a generic "invalid instruction"
1708 // error, followed by notes for each of the near-misses.
1709 Error(IDLoc,
1710 "invalid instruction, any one of the following would fix this:");
1711 for (auto &M : Messages)
1712 Note(M.Loc, M.Message);
1713 }
1714}
1715
1716bool RISCVAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1718 MCStreamer &Out,
1719 uint64_t &ErrorInfo,
1720 bool MatchingInlineAsm) {
1721 MCInst Inst;
1723
1724 auto Result =
1725 MatchInstructionImpl(Operands, Inst, &NearMisses, MatchingInlineAsm);
1726 switch (Result) {
1727 default:
1728 break;
1729 case Match_Success:
1730 if (validateInstruction(Inst, Operands))
1731 return true;
1732 return processInstruction(Inst, IDLoc, Operands, Out);
1733 case Match_MnemonicFail: {
1734 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
1735 std::string Suggestion = RISCVMnemonicSpellCheck(
1736 ((RISCVOperand &)*Operands[0]).getToken(), FBS, 0);
1737 return Error(IDLoc, "unrecognized instruction mnemonic" + Suggestion);
1738 }
1739 case Match_NearMisses:
1740 ReportNearMisses(NearMisses, IDLoc, Operands);
1741 return true;
1742 }
1743
1744 llvm_unreachable("Unknown match type detected!");
1745}
1746
1747// Attempts to match Name as a register (either using the default name or
1748// alternative ABI names), returning the matching register. Upon failure,
1749// returns a non-valid MCRegister. If IsRVE, then registers x16-x31 will be
1750// rejected.
1751MCRegister RISCVAsmParser::matchRegisterNameHelper(StringRef Name) const {
1752 MCRegister Reg = MatchRegisterName(Name);
1753 // The 16-/32-/128- and 64-bit FPRs have the same asm name. Check
1754 // that the initial match always matches the 64-bit variant, and
1755 // not the 16/32/128-bit one.
1756 assert(!(Reg >= RISCV::F0_H && Reg <= RISCV::F31_H));
1757 assert(!(Reg >= RISCV::F0_F && Reg <= RISCV::F31_F));
1758 assert(!(Reg >= RISCV::F0_Q && Reg <= RISCV::F31_Q));
1759 // The default FPR register class is based on the tablegen enum ordering.
1760 static_assert(RISCV::F0_D < RISCV::F0_H, "FPR matching must be updated");
1761 static_assert(RISCV::F0_D < RISCV::F0_F, "FPR matching must be updated");
1762 static_assert(RISCV::F0_D < RISCV::F0_Q, "FPR matching must be updated");
1763 if (!Reg)
1764 Reg = MatchRegisterAltName(Name);
1765 if (isRVE() && Reg >= RISCV::X16 && Reg <= RISCV::X31)
1766 Reg = MCRegister();
1767 return Reg;
1768}
1769
1770bool RISCVAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
1771 SMLoc &EndLoc) {
1772 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
1773 return Error(StartLoc, "invalid register name");
1774 return false;
1775}
1776
1777ParseStatus RISCVAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
1778 SMLoc &EndLoc) {
1779 const AsmToken &Tok = getParser().getTok();
1780 StartLoc = Tok.getLoc();
1781 EndLoc = Tok.getEndLoc();
1782 StringRef Name = getLexer().getTok().getIdentifier();
1783
1785 if (!Reg)
1786 return ParseStatus::NoMatch;
1787
1788 getParser().Lex(); // Eat identifier token.
1789 return ParseStatus::Success;
1790}
1791
1792ParseStatus RISCVAsmParser::parseRegister(OperandVector &Operands,
1793 bool AllowParens) {
1794 SMLoc FirstS = getLoc();
1795 bool HadParens = false;
1796 AsmToken LParen;
1797
1798 // If this is an LParen and a parenthesised register name is allowed, parse it
1799 // atomically.
1800 if (AllowParens && getLexer().is(AsmToken::LParen)) {
1801 AsmToken Buf[2];
1802 size_t ReadCount = getLexer().peekTokens(Buf);
1803 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
1804 HadParens = true;
1805 LParen = getParser().getTok();
1806 getParser().Lex(); // Eat '('
1807 }
1808 }
1809
1810 switch (getLexer().getKind()) {
1811 default:
1812 if (HadParens)
1813 getLexer().UnLex(LParen);
1814 return ParseStatus::NoMatch;
1816 StringRef Name = getLexer().getTok().getIdentifier();
1817 MCRegister Reg = matchRegisterNameHelper(Name);
1818
1819 if (!Reg) {
1820 if (HadParens)
1821 getLexer().UnLex(LParen);
1822 return ParseStatus::NoMatch;
1823 }
1824 if (HadParens)
1825 Operands.push_back(RISCVOperand::createToken("(", FirstS));
1826 SMLoc S = getLoc();
1827 SMLoc E = getTok().getEndLoc();
1828 getLexer().Lex();
1829 Operands.push_back(RISCVOperand::createReg(Reg, S, E));
1830 }
1831
1832 if (HadParens) {
1833 getParser().Lex(); // Eat ')'
1834 Operands.push_back(RISCVOperand::createToken(")", getLoc()));
1835 }
1836
1837 return ParseStatus::Success;
1838}
1839
1840ParseStatus RISCVAsmParser::parseInsnDirectiveOpcode(OperandVector &Operands) {
1841 SMLoc S = getLoc();
1842 SMLoc E;
1843 const MCExpr *Res;
1844
1845 switch (getLexer().getKind()) {
1846 default:
1847 return ParseStatus::NoMatch;
1848 case AsmToken::LParen:
1849 case AsmToken::Minus:
1850 case AsmToken::Plus:
1851 case AsmToken::Exclaim:
1852 case AsmToken::Tilde:
1853 case AsmToken::Integer:
1854 case AsmToken::String: {
1855 if (getParser().parseExpression(Res, E))
1856 return ParseStatus::Failure;
1857
1858 auto *CE = dyn_cast<MCConstantExpr>(Res);
1859 if (CE) {
1860 int64_t Imm = CE->getValue();
1861 if (isUInt<7>(Imm)) {
1862 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
1863 return ParseStatus::Success;
1864 }
1865 }
1866
1867 break;
1868 }
1869 case AsmToken::Identifier: {
1870 StringRef Identifier;
1871 if (getParser().parseIdentifier(Identifier))
1872 return ParseStatus::Failure;
1873
1874 auto Opcode = RISCVInsnOpcode::lookupRISCVOpcodeByName(Identifier);
1875 if (Opcode) {
1876 assert(isUInt<7>(Opcode->Value) && (Opcode->Value & 0x3) == 3 &&
1877 "Unexpected opcode");
1878 Res = MCConstantExpr::create(Opcode->Value, getContext());
1880 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
1881 return ParseStatus::Success;
1882 }
1883
1884 break;
1885 }
1886 case AsmToken::Percent:
1887 break;
1888 }
1889
1890 return generateImmOutOfRangeError(
1891 S, 0, 127,
1892 "opcode must be a valid opcode name or an immediate in the range");
1893}
1894
1895ParseStatus RISCVAsmParser::parseInsnCDirectiveOpcode(OperandVector &Operands) {
1896 SMLoc S = getLoc();
1897 SMLoc E;
1898 const MCExpr *Res;
1899
1900 switch (getLexer().getKind()) {
1901 default:
1902 return ParseStatus::NoMatch;
1903 case AsmToken::LParen:
1904 case AsmToken::Minus:
1905 case AsmToken::Plus:
1906 case AsmToken::Exclaim:
1907 case AsmToken::Tilde:
1908 case AsmToken::Integer:
1909 case AsmToken::String: {
1910 if (getParser().parseExpression(Res, E))
1911 return ParseStatus::Failure;
1912
1913 auto *CE = dyn_cast<MCConstantExpr>(Res);
1914 if (CE) {
1915 int64_t Imm = CE->getValue();
1916 if (Imm >= 0 && Imm <= 2) {
1917 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
1918 return ParseStatus::Success;
1919 }
1920 }
1921
1922 break;
1923 }
1924 case AsmToken::Identifier: {
1925 StringRef Identifier;
1926 if (getParser().parseIdentifier(Identifier))
1927 return ParseStatus::Failure;
1928
1929 unsigned Opcode;
1930 if (Identifier == "C0")
1931 Opcode = 0;
1932 else if (Identifier == "C1")
1933 Opcode = 1;
1934 else if (Identifier == "C2")
1935 Opcode = 2;
1936 else
1937 break;
1938
1939 Res = MCConstantExpr::create(Opcode, getContext());
1941 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
1942 return ParseStatus::Success;
1943 }
1944 case AsmToken::Percent: {
1945 // Discard operand with modifier.
1946 break;
1947 }
1948 }
1949
1950 return generateImmOutOfRangeError(
1951 S, 0, 2,
1952 "opcode must be a valid opcode name or an immediate in the range");
1953}
1954
1955ParseStatus RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1956 SMLoc S = getLoc();
1957 const MCExpr *Res;
1958
1959 auto SysRegFromConstantInt = [this](const MCExpr *E, SMLoc S) {
1960 if (auto *CE = dyn_cast<MCConstantExpr>(E)) {
1961 int64_t Imm = CE->getValue();
1962 if (isUInt<12>(Imm)) {
1963 auto Range = RISCVSysReg::lookupSysRegByEncoding(Imm);
1964 // Accept an immediate representing a named Sys Reg if it satisfies the
1965 // the required features.
1966 for (auto &Reg : Range) {
1967 if (Reg.IsAltName || Reg.IsDeprecatedName)
1968 continue;
1969 if (Reg.haveRequiredFeatures(STI->getFeatureBits()))
1970 return RISCVOperand::createSysReg(
1971 RISCVSysReg::getSysRegStr(Reg.Name), S, Imm);
1972 }
1973 // Accept an immediate representing an un-named Sys Reg if the range is
1974 // valid, regardless of the required features.
1975 return RISCVOperand::createSysReg("", S, Imm);
1976 }
1977 }
1978 return std::unique_ptr<RISCVOperand>();
1979 };
1980
1981 switch (getLexer().getKind()) {
1982 default:
1983 return ParseStatus::NoMatch;
1984 case AsmToken::LParen:
1985 case AsmToken::Minus:
1986 case AsmToken::Plus:
1987 case AsmToken::Exclaim:
1988 case AsmToken::Tilde:
1989 case AsmToken::Integer:
1990 case AsmToken::String: {
1991 if (getParser().parseExpression(Res))
1992 return ParseStatus::Failure;
1993
1994 if (auto SysOpnd = SysRegFromConstantInt(Res, S)) {
1995 Operands.push_back(std::move(SysOpnd));
1996 return ParseStatus::Success;
1997 }
1998
1999 return generateImmOutOfRangeError(S, 0, (1 << 12) - 1);
2000 }
2001 case AsmToken::Identifier: {
2002 StringRef Identifier;
2003 if (getParser().parseIdentifier(Identifier))
2004 return ParseStatus::Failure;
2005
2006 const auto *SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
2007
2008 if (SysReg) {
2009 if (SysReg->IsDeprecatedName) {
2010 // Lookup the undeprecated name.
2011 auto Range = RISCVSysReg::lookupSysRegByEncoding(SysReg->Encoding);
2012 for (auto &Reg : Range) {
2013 if (Reg.IsAltName || Reg.IsDeprecatedName)
2014 continue;
2015 Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
2016 RISCVSysReg::getSysRegStr(Reg.Name) + "'");
2017 }
2018 }
2019
2020 // Accept a named Sys Reg if the required features are present.
2021 const auto &FeatureBits = getSTI().getFeatureBits();
2022 const auto &AllFeatures = getSTI().getAllProcessorFeatures();
2023 if (!SysReg->haveRequiredFeatures(FeatureBits)) {
2024 const auto *Feature =
2025 llvm::find_if(AllFeatures, [&](const auto &Feature) {
2026 return SysReg->FeaturesRequired[Feature.Value];
2027 });
2028 std::string ErrorMsg =
2029 std::string("system register '") +
2030 std::string(RISCVSysReg::getSysRegStr(SysReg->Name)) + "' ";
2031 if (SysReg->IsRV32Only && FeatureBits[RISCV::Feature64Bit]) {
2032 ErrorMsg += "is RV32 only";
2033 if (Feature != std::end(AllFeatures))
2034 ErrorMsg += " and ";
2035 }
2036 if (Feature != std::end(AllFeatures)) {
2037 ErrorMsg +=
2038 "requires '" + std::string(Feature->key()) + "' to be enabled";
2039 }
2040
2041 return Error(S, ErrorMsg);
2042 }
2043 Operands.push_back(
2044 RISCVOperand::createSysReg(Identifier, S, SysReg->Encoding));
2045 return ParseStatus::Success;
2046 }
2047
2048 // Accept a symbol name that evaluates to an absolute value.
2049 MCSymbol *Sym = getContext().lookupSymbol(Identifier);
2050 if (Sym && Sym->isVariable()) {
2051 // Pass false for SetUsed, since redefining the value later does not
2052 // affect this instruction.
2053 if (auto SysOpnd = SysRegFromConstantInt(Sym->getVariableValue(), S)) {
2054 Operands.push_back(std::move(SysOpnd));
2055 return ParseStatus::Success;
2056 }
2057 }
2058
2059 return generateImmOutOfRangeError(S, 0, (1 << 12) - 1,
2060 "operand must be a valid system register "
2061 "name or an integer in the range");
2062 }
2063 case AsmToken::Percent: {
2064 // Discard operand with modifier.
2065 return generateImmOutOfRangeError(S, 0, (1 << 12) - 1);
2066 }
2067 }
2068
2069 return ParseStatus::NoMatch;
2070}
2071
2072ParseStatus RISCVAsmParser::parseFPImm(OperandVector &Operands) {
2073 SMLoc S = getLoc();
2074
2075 // Parse special floats (inf/nan/min) representation.
2076 if (getTok().is(AsmToken::Identifier)) {
2077 StringRef Identifier = getTok().getIdentifier();
2078 if (Identifier.compare_insensitive("inf") == 0) {
2079 Operands.push_back(
2080 RISCVOperand::createExpr(MCConstantExpr::create(30, getContext()), S,
2081 getTok().getEndLoc(), isRV64()));
2082 } else if (Identifier.compare_insensitive("nan") == 0) {
2083 Operands.push_back(
2084 RISCVOperand::createExpr(MCConstantExpr::create(31, getContext()), S,
2085 getTok().getEndLoc(), isRV64()));
2086 } else if (Identifier.compare_insensitive("min") == 0) {
2087 Operands.push_back(
2088 RISCVOperand::createExpr(MCConstantExpr::create(1, getContext()), S,
2089 getTok().getEndLoc(), isRV64()));
2090 } else {
2091 return TokError("invalid floating point literal");
2092 }
2093
2094 Lex(); // Eat the token.
2095
2096 return ParseStatus::Success;
2097 }
2098
2099 // Handle negation, as that still comes through as a separate token.
2100 bool IsNegative = parseOptionalToken(AsmToken::Minus);
2101
2102 const AsmToken &Tok = getTok();
2103 if (!Tok.is(AsmToken::Real))
2104 return TokError("invalid floating point immediate");
2105
2106 // Parse FP representation.
2107 APFloat RealVal(APFloat::IEEEdouble());
2108 auto StatusOrErr =
2109 RealVal.convertFromString(Tok.getString(), APFloat::rmTowardZero);
2110 if (errorToBool(StatusOrErr.takeError()))
2111 return TokError("invalid floating point representation");
2112
2113 if (IsNegative)
2114 RealVal.changeSign();
2115
2116 Operands.push_back(RISCVOperand::createFPImm(
2117 RealVal.bitcastToAPInt().getZExtValue(), S));
2118
2119 Lex(); // Eat the token.
2120
2121 return ParseStatus::Success;
2122}
2123
2124ParseStatus RISCVAsmParser::parseExpression(OperandVector &Operands) {
2125 SMLoc S = getLoc();
2126 SMLoc E;
2127 const MCExpr *Res;
2128
2129 switch (getLexer().getKind()) {
2130 default:
2131 return ParseStatus::NoMatch;
2132 case AsmToken::LParen:
2133 case AsmToken::Dot:
2134 case AsmToken::Minus:
2135 case AsmToken::Plus:
2136 case AsmToken::Exclaim:
2137 case AsmToken::Tilde:
2138 case AsmToken::Integer:
2139 case AsmToken::String:
2141 if (getParser().parseExpression(Res, E))
2142 return ParseStatus::Failure;
2143 break;
2144 case AsmToken::Percent:
2145 return parseOperandWithSpecifier(Operands);
2146 }
2147
2148 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
2149 return ParseStatus::Success;
2150}
2151
2152ParseStatus RISCVAsmParser::parseOperandWithSpecifier(OperandVector &Operands) {
2153 SMLoc S = getLoc();
2154 SMLoc E;
2155
2156 if (parseToken(AsmToken::Percent, "expected '%' relocation specifier"))
2157 return ParseStatus::Failure;
2158 const MCExpr *Expr = nullptr;
2159 bool Failed = parseExprWithSpecifier(Expr, E);
2160 if (!Failed)
2161 Operands.push_back(RISCVOperand::createExpr(Expr, S, E, isRV64()));
2162 return Failed;
2163}
2164
2165bool RISCVAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
2166 SMLoc Loc = getLoc();
2167 if (getLexer().getKind() != AsmToken::Identifier)
2168 return TokError("expected '%' relocation specifier");
2169 StringRef Identifier = getParser().getTok().getIdentifier();
2170 auto Spec = RISCV::parseSpecifierName(Identifier);
2171 if (!Spec)
2172 return TokError("invalid relocation specifier");
2173
2174 getParser().Lex(); // Eat the identifier
2175 if (parseToken(AsmToken::LParen, "expected '('"))
2176 return true;
2177
2178 const MCExpr *SubExpr;
2179 if (getParser().parseParenExpression(SubExpr, E))
2180 return true;
2181
2182 Res = MCSpecifierExpr::create(SubExpr, Spec, getContext(), Loc);
2183 return false;
2184}
2185
2186bool RISCVAsmParser::parseDataExpr(const MCExpr *&Res) {
2187 SMLoc E;
2188 if (parseOptionalToken(AsmToken::Percent))
2189 return parseExprWithSpecifier(Res, E);
2190 return getParser().parseExpression(Res);
2191}
2192
2193ParseStatus RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
2194 SMLoc S = getLoc();
2195 const MCExpr *Res;
2196
2197 if (getLexer().getKind() != AsmToken::Identifier)
2198 return ParseStatus::NoMatch;
2199
2200 StringRef Identifier = getTok().getIdentifier();
2201 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
2202
2203 if (Sym->isVariable()) {
2204 const MCExpr *V = Sym->getVariableValue();
2205 if (!isa<MCSymbolRefExpr>(V))
2206 return ParseStatus::NoMatch;
2207 }
2208
2209 SMLoc E;
2210 if (getParser().parseExpression(Res, E))
2211 return ParseStatus::Failure;
2212
2213 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
2214 return ParseStatus::Success;
2215}
2216
2217ParseStatus RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
2218 SMLoc S = getLoc();
2219 const MCExpr *Res;
2220
2221 if (getLexer().getKind() != AsmToken::Identifier)
2222 return ParseStatus::NoMatch;
2223 std::string Identifier(getTok().getIdentifier());
2224
2225 if (getLexer().peekTok().is(AsmToken::At)) {
2226 Lex();
2227 Lex();
2228 StringRef PLT;
2229 SMLoc Loc = getLoc();
2230 if (getParser().parseIdentifier(PLT) || PLT != "plt")
2231 return Error(Loc, "@ (except the deprecated/ignored @plt) is disallowed");
2232 } else if (!getLexer().peekTok().is(AsmToken::EndOfStatement)) {
2233 // Avoid parsing the register in `call rd, foo` as a call symbol.
2234 return ParseStatus::NoMatch;
2235 } else {
2236 Lex();
2237 }
2238
2239 SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
2241
2242 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
2243 Res = MCSymbolRefExpr::create(Sym, getContext());
2244 Res = MCSpecifierExpr::create(Res, Kind, getContext());
2245 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
2246 return ParseStatus::Success;
2247}
2248
2249// Like parseCallSymbol but allows the symbol to be followed by a comma
2250// (for "tail address, register" form where the symbol is not the last operand).
2251ParseStatus RISCVAsmParser::parseTailCallSymbol(OperandVector &Operands) {
2252 SMLoc S = getLoc();
2253 const MCExpr *Res;
2254
2255 if (getLexer().getKind() != AsmToken::Identifier)
2256 return ParseStatus::NoMatch;
2257 std::string Identifier(getTok().getIdentifier());
2258
2259 if (getLexer().peekTok().is(AsmToken::At)) {
2260 Lex();
2261 Lex();
2262 StringRef PLT;
2263 SMLoc Loc = getLoc();
2264 if (getParser().parseIdentifier(PLT) || PLT != "plt")
2265 return Error(Loc, "@ (except the deprecated/ignored @plt) is disallowed");
2266 } else if (!getLexer().peekTok().is(AsmToken::EndOfStatement) &&
2267 !getLexer().peekTok().is(AsmToken::Comma)) {
2268 return ParseStatus::NoMatch;
2269 } else {
2270 Lex();
2271 }
2272
2273 SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
2275
2276 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
2277 Res = MCSymbolRefExpr::create(Sym, getContext());
2278 Res = MCSpecifierExpr::create(Res, Kind, getContext());
2279 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
2280 return ParseStatus::Success;
2281}
2282
2283ParseStatus RISCVAsmParser::parsePseudoJumpSymbol(OperandVector &Operands) {
2284 SMLoc S = getLoc();
2285 SMLoc E;
2286 const MCExpr *Res;
2287
2288 if (getParser().parseExpression(Res, E))
2289 return ParseStatus::Failure;
2290
2291 if (Res->getKind() != MCExpr::ExprKind::SymbolRef)
2292 return Error(S, "operand must be a valid jump target");
2293
2295 Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
2296 return ParseStatus::Success;
2297}
2298
2299ParseStatus RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
2300 // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
2301 // both being acceptable forms. When parsing `jal ra, foo` this function
2302 // will be called for the `ra` register operand in an attempt to match the
2303 // single-operand alias. parseJALOffset must fail for this case. It would
2304 // seem logical to try parse the operand using parseExpression and return
2305 // NoMatch if the next token is a comma (meaning we must be parsing a jal in
2306 // the second form rather than the first). We can't do this as there's no
2307 // way of rewinding the lexer state. Instead, return NoMatch if this operand
2308 // is an identifier and is followed by a comma.
2309 if (getLexer().is(AsmToken::Identifier) &&
2310 getLexer().peekTok().is(AsmToken::Comma))
2311 return ParseStatus::NoMatch;
2312
2313 return parseExpression(Operands);
2314}
2315
2316bool RISCVAsmParser::parseVTypeToken(const AsmToken &Tok, VTypeState &State,
2317 unsigned &Sew, unsigned &Lmul,
2318 bool &Fractional, bool &TailAgnostic,
2319 bool &MaskAgnostic, bool &AltFmt) {
2320 if (Tok.isNot(AsmToken::Identifier))
2321 return true;
2322
2323 StringRef Identifier = Tok.getIdentifier();
2324 if (State < VTypeState::SeenSew && Identifier.consume_front("e")) {
2325 if (Identifier.getAsInteger(10, Sew)) {
2326 if (Identifier == "16alt") {
2327 AltFmt = true;
2328 Sew = 16;
2329 } else if (Identifier == "8alt") {
2330 AltFmt = true;
2331 Sew = 8;
2332 } else {
2333 return true;
2334 }
2335 }
2336 if (!RISCVVType::isValidSEW(Sew))
2337 return true;
2338
2339 State = VTypeState::SeenSew;
2340 return false;
2341 }
2342
2343 if (State < VTypeState::SeenLmul && Identifier.consume_front("m")) {
2344 // Might arrive here if lmul and tail policy unspecified, if so we're
2345 // parsing a MaskPolicy not an LMUL.
2346 if (Identifier == "a" || Identifier == "u") {
2347 MaskAgnostic = (Identifier == "a");
2348 State = VTypeState::SeenMaskPolicy;
2349 return false;
2350 }
2351
2352 Fractional = Identifier.consume_front("f");
2353 if (Identifier.getAsInteger(10, Lmul))
2354 return true;
2355 if (!RISCVVType::isValidLMUL(Lmul, Fractional))
2356 return true;
2357
2358 if (Fractional) {
2359 unsigned ELEN = STI->hasFeature(RISCV::FeatureStdExtZve64x) ? 64 : 32;
2360 unsigned MinLMUL = ELEN / 8;
2361 if (Lmul > MinLMUL)
2362 Warning(Tok.getLoc(),
2363 "use of vtype encodings with LMUL < SEWMIN/ELEN == mf" +
2364 Twine(MinLMUL) + " is reserved");
2365 }
2366
2367 State = VTypeState::SeenLmul;
2368 return false;
2369 }
2370
2371 if (State < VTypeState::SeenTailPolicy && Identifier.starts_with("t")) {
2372 if (Identifier == "ta")
2373 TailAgnostic = true;
2374 else if (Identifier == "tu")
2375 TailAgnostic = false;
2376 else
2377 return true;
2378
2379 State = VTypeState::SeenTailPolicy;
2380 return false;
2381 }
2382
2383 if (State < VTypeState::SeenMaskPolicy && Identifier.starts_with("m")) {
2384 if (Identifier == "ma")
2385 MaskAgnostic = true;
2386 else if (Identifier == "mu")
2387 MaskAgnostic = false;
2388 else
2389 return true;
2390
2391 State = VTypeState::SeenMaskPolicy;
2392 return false;
2393 }
2394
2395 return true;
2396}
2397
2398ParseStatus RISCVAsmParser::parseVTypeI(OperandVector &Operands) {
2399 SMLoc S = getLoc();
2400
2401 // Default values
2402 unsigned Sew = 8;
2403 unsigned Lmul = 1;
2404 bool Fractional = false;
2405 bool TailAgnostic = false;
2406 bool MaskAgnostic = false;
2407 bool AltFmt = false;
2408
2409 VTypeState State = VTypeState::SeenNothingYet;
2410 do {
2411 if (parseVTypeToken(getTok(), State, Sew, Lmul, Fractional, TailAgnostic,
2412 MaskAgnostic, AltFmt)) {
2413 // The first time, errors return NoMatch rather than Failure
2414 if (State == VTypeState::SeenNothingYet)
2415 return ParseStatus::NoMatch;
2416 break;
2417 }
2418
2419 getLexer().Lex();
2420 } while (parseOptionalToken(AsmToken::Comma));
2421
2422 if (!getLexer().is(AsmToken::EndOfStatement) ||
2423 State == VTypeState::SeenNothingYet)
2424 return generateVTypeError(S);
2425
2427 if (Fractional) {
2428 unsigned ELEN = STI->hasFeature(RISCV::FeatureStdExtZve64x) ? 64 : 32;
2429 unsigned MaxSEW = ELEN / Lmul;
2430 // If MaxSEW < 8, we should have printed warning about reserved LMUL.
2431 if (MaxSEW >= 8 && Sew > MaxSEW)
2432 Warning(S, "use of vtype encodings with SEW > " + Twine(MaxSEW) +
2433 " and LMUL == mf" + Twine(Lmul) +
2434 " may not be compatible with all RVV implementations");
2435 }
2436
2437 unsigned VTypeI =
2438 RISCVVType::encodeVTYPE(VLMUL, Sew, TailAgnostic, MaskAgnostic, AltFmt);
2439 Operands.push_back(RISCVOperand::createVType(VTypeI, S));
2440 return ParseStatus::Success;
2441}
2442
2443bool RISCVAsmParser::generateVTypeError(SMLoc ErrorLoc) {
2444 return Error(ErrorLoc,
2445 "operand must be "
2446 "e[8|8alt|16|16alt|32|64],m[1|2|4|8|f2|f4|f8],[ta|tu],[ma|mu]");
2447}
2448
2449ParseStatus RISCVAsmParser::parseXSfmmVType(OperandVector &Operands) {
2450 SMLoc S = getLoc();
2451
2452 unsigned Widen = 0;
2453 unsigned SEW = 0;
2454 bool AltFmt = false;
2455 StringRef Identifier;
2456
2457 if (getTok().isNot(AsmToken::Identifier))
2458 goto Fail;
2459
2460 Identifier = getTok().getIdentifier();
2461
2462 if (!Identifier.consume_front("e"))
2463 goto Fail;
2464
2465 if (Identifier.getAsInteger(10, SEW)) {
2466 if (Identifier != "16alt")
2467 goto Fail;
2468
2469 AltFmt = true;
2470 SEW = 16;
2471 }
2472 if (!RISCVVType::isValidSEW(SEW))
2473 goto Fail;
2474
2475 Lex();
2476
2477 if (!parseOptionalToken(AsmToken::Comma))
2478 goto Fail;
2479
2480 if (getTok().isNot(AsmToken::Identifier))
2481 goto Fail;
2482
2483 Identifier = getTok().getIdentifier();
2484
2485 if (!Identifier.consume_front("w"))
2486 goto Fail;
2487 if (Identifier.getAsInteger(10, Widen))
2488 goto Fail;
2489 if (Widen != 1 && Widen != 2 && Widen != 4)
2490 goto Fail;
2491
2492 Lex();
2493
2494 if (getLexer().is(AsmToken::EndOfStatement)) {
2495 Operands.push_back(RISCVOperand::createVType(
2496 RISCVVType::encodeXSfmmVType(SEW, Widen, AltFmt), S));
2497 return ParseStatus::Success;
2498 }
2499
2500Fail:
2501 return generateXSfmmVTypeError(S);
2502}
2503
2504bool RISCVAsmParser::generateXSfmmVTypeError(SMLoc ErrorLoc) {
2505 return Error(ErrorLoc, "operand must be e[8|16|16alt|32|64],w[1|2|4]");
2506}
2507
2508ParseStatus RISCVAsmParser::parseMaskReg(OperandVector &Operands) {
2509 if (getLexer().isNot(AsmToken::Identifier))
2510 return ParseStatus::NoMatch;
2511
2512 StringRef Name = getLexer().getTok().getIdentifier();
2513 if (!Name.consume_back(".t")) {
2514 // Non-register identifiers may belong to another optional operand in an
2515 // overloaded mnemonic. Let the matcher try those alternatives.
2516 if (matchRegisterNameHelper(Name))
2517 return Error(getLoc(), "expected '.t' suffix");
2518 return ParseStatus::NoMatch;
2519 }
2520 MCRegister Reg = matchRegisterNameHelper(Name);
2521
2522 if (!Reg)
2523 return ParseStatus::NoMatch;
2524 if (Reg != RISCV::V0)
2525 return ParseStatus::NoMatch;
2526 SMLoc S = getLoc();
2527 SMLoc E = getTok().getEndLoc();
2528 getLexer().Lex();
2529 Operands.push_back(RISCVOperand::createReg(Reg, S, E));
2530 return ParseStatus::Success;
2531}
2532
2533ParseStatus RISCVAsmParser::parseVScaleReg(OperandVector &Operands) {
2534 if (getLexer().isNot(AsmToken::Identifier))
2535 return ParseStatus::NoMatch;
2536
2537 StringRef Name = getLexer().getTok().getIdentifier();
2538 if (!Name.consume_back(".scale"))
2539 return Error(getLoc(), "expected '.scale' suffix");
2540 MCRegister Reg = matchRegisterNameHelper(Name);
2541
2542 if (!Reg)
2543 return ParseStatus::NoMatch;
2544 if (Reg != RISCV::V0)
2545 return ParseStatus::NoMatch;
2546 SMLoc S = getLoc();
2547 SMLoc E = getTok().getEndLoc();
2548 getLexer().Lex();
2549 Operands.push_back(RISCVOperand::createReg(Reg, S, E));
2550 return ParseStatus::Success;
2551}
2552
2553ParseStatus RISCVAsmParser::parseTileLambda(OperandVector &Operands) {
2554 if (getLexer().isNot(AsmToken::Identifier))
2555 return ParseStatus::NoMatch;
2556
2557 SMLoc S = getLoc();
2558 StringRef Name = getLexer().getTok().getIdentifier();
2559 if (!Name.consume_front("L") && !Name.consume_front("l"))
2560 return ParseStatus::NoMatch;
2561
2562 unsigned Lambda;
2563 if (Name.getAsInteger(10, Lambda) || !isPowerOf2_32(Lambda) || Lambda >= 128)
2564 return Error(S, "operand must be L1, L2, L4, L8, L16, L32, or L64");
2565
2566 unsigned EncodedLambda = Log2_32(Lambda) + 1;
2567
2568 SMLoc E = getTok().getEndLoc();
2569 getLexer().Lex();
2570 Operands.push_back(RISCVOperand::createExpr(
2571 MCConstantExpr::create(EncodedLambda, getContext()), S, E, isRV64()));
2572 return ParseStatus::Success;
2573}
2574
2575ParseStatus RISCVAsmParser::parseSMTVType(OperandVector &Operands) {
2576 if (getLexer().isNot(AsmToken::Identifier))
2577 return TokError(
2578 "operand must be a valid SpacemiT's Integer Matrix VType mnemonic");
2579
2580 StringRef Str = getLexer().getTok().getIdentifier();
2582
2583 if (!isValidSMTVTypeMode(VType))
2584 return TokError("SpacemiT's Integer Matrix only supports [i4|i8] mode");
2585
2586 Operands.push_back(RISCVOperand::createSMTVType(VType, getLoc()));
2587 Lex(); // Eat identifier token.
2588 return ParseStatus::Success;
2589}
2590
2591ParseStatus RISCVAsmParser::parseFRMArg(OperandVector &Operands) {
2592 if (getLexer().isNot(AsmToken::Identifier))
2593 return TokError(
2594 "operand must be a valid floating point rounding mode mnemonic");
2595
2596 StringRef Str = getLexer().getTok().getIdentifier();
2598
2599 if (FRM == RISCVFPRndMode::Invalid)
2600 return TokError(
2601 "operand must be a valid floating point rounding mode mnemonic");
2602
2603 Operands.push_back(RISCVOperand::createFRMArg(FRM, getLoc()));
2604 Lex(); // Eat identifier token.
2605 return ParseStatus::Success;
2606}
2607
2608std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultSMTVType() {
2609 return RISCVOperand::createSMTVType(XSMTVTypeMode::SMTVTypeMode::SMT_I8,
2610 SMLoc());
2611}
2612
2613ParseStatus RISCVAsmParser::parseFenceArg(OperandVector &Operands) {
2614 const AsmToken &Tok = getLexer().getTok();
2615
2616 if (Tok.is(AsmToken::Integer)) {
2617 if (Tok.getIntVal() != 0)
2618 goto ParseFail;
2619
2620 Operands.push_back(RISCVOperand::createFenceArg(0, getLoc()));
2621 Lex();
2622 return ParseStatus::Success;
2623 }
2624
2625 if (Tok.is(AsmToken::Identifier)) {
2626 StringRef Str = Tok.getIdentifier();
2627
2628 // Letters must be unique, taken from 'iorw', and in ascending order. This
2629 // holds as long as each individual character is one of 'iorw' and is
2630 // greater than the previous character.
2631 unsigned Imm = 0;
2632 bool Valid = true;
2633 char Prev = '\0';
2634 for (char c : Str) {
2635 switch (c) {
2636 default:
2637 Valid = false;
2638 break;
2639 case 'i':
2641 break;
2642 case 'o':
2644 break;
2645 case 'r':
2647 break;
2648 case 'w':
2650 break;
2651 }
2652
2653 if (c <= Prev) {
2654 Valid = false;
2655 break;
2656 }
2657 Prev = c;
2658 }
2659
2660 if (!Valid)
2661 goto ParseFail;
2662
2663 Operands.push_back(RISCVOperand::createFenceArg(Imm, getLoc()));
2664 Lex();
2665 return ParseStatus::Success;
2666 }
2667
2668ParseFail:
2669 return TokError("operand must be formed of letters selected in-order from "
2670 "'iorw' or be 0");
2671}
2672
2673ParseStatus RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
2674 if (parseToken(AsmToken::LParen, "expected '('"))
2675 return ParseStatus::Failure;
2676 Operands.push_back(RISCVOperand::createToken("(", getLoc()));
2677
2678 if (!parseRegister(Operands).isSuccess())
2679 return Error(getLoc(), "expected register");
2680
2681 if (parseToken(AsmToken::RParen, "expected ')'"))
2682 return ParseStatus::Failure;
2683 Operands.push_back(RISCVOperand::createToken(")", getLoc()));
2684
2685 return ParseStatus::Success;
2686}
2687
2688ParseStatus RISCVAsmParser::parseZeroOffsetMemOp(OperandVector &Operands) {
2689 // Atomic operations such as lr.w, sc.w, and amo*.w accept a "memory operand"
2690 // as one of their register operands, such as `(a0)`. This just denotes that
2691 // the register (in this case `a0`) contains a memory address.
2692 //
2693 // Normally, we would be able to parse these by putting the parens into the
2694 // instruction string. However, GNU as also accepts a zero-offset memory
2695 // operand (such as `0(a0)`), and ignores the 0. Normally this would be parsed
2696 // with parseExpression followed by parseMemOpBaseReg, but these instructions
2697 // do not accept an immediate operand, and we do not want to add a "dummy"
2698 // operand that is silently dropped.
2699 //
2700 // Instead, we use this custom parser. This will: allow (and discard) an
2701 // offset if it is zero; require (and discard) parentheses; and add only the
2702 // parsed register operand to `Operands`.
2703 //
2704 // These operands are printed with RISCVInstPrinter::printZeroOffsetMemOp,
2705 // which will only print the register surrounded by parentheses (which GNU as
2706 // also uses as its canonical representation for these operands).
2707 std::unique_ptr<RISCVOperand> OptionalImmOp;
2708
2709 if (getLexer().isNot(AsmToken::LParen)) {
2710 // Parse an Integer token. We do not accept arbitrary constant expressions
2711 // in the offset field (because they may include parens, which complicates
2712 // parsing a lot).
2713 int64_t ImmVal;
2714 SMLoc ImmStart = getLoc();
2715 if (getParser().parseIntToken(ImmVal,
2716 "expected '(' or optional integer offset"))
2717 return ParseStatus::Failure;
2718
2719 // Create a RISCVOperand for checking later (so the error messages are
2720 // nicer), but we don't add it to Operands.
2721 SMLoc ImmEnd = getLoc();
2722 OptionalImmOp =
2723 RISCVOperand::createExpr(MCConstantExpr::create(ImmVal, getContext()),
2724 ImmStart, ImmEnd, isRV64());
2725 }
2726
2727 if (parseToken(AsmToken::LParen,
2728 OptionalImmOp ? "expected '(' after optional integer offset"
2729 : "expected '(' or optional integer offset"))
2730 return ParseStatus::Failure;
2731
2732 if (!parseRegister(Operands).isSuccess())
2733 return Error(getLoc(), "expected register");
2734
2735 if (parseToken(AsmToken::RParen, "expected ')'"))
2736 return ParseStatus::Failure;
2737
2738 // Deferred Handling of non-zero offsets. This makes the error messages nicer.
2739 if (OptionalImmOp && !OptionalImmOp->isImmZero())
2740 return Error(
2741 OptionalImmOp->getStartLoc(), "optional integer offset must be 0",
2742 SMRange(OptionalImmOp->getStartLoc(), OptionalImmOp->getEndLoc()));
2743
2744 return ParseStatus::Success;
2745}
2746
2747ParseStatus RISCVAsmParser::parseRegReg(OperandVector &Operands) {
2748 // RR : a2(a1)
2749 if (getLexer().getKind() != AsmToken::Identifier)
2750 return ParseStatus::NoMatch;
2751
2752 SMLoc S = getLoc();
2753 StringRef OffsetRegName = getLexer().getTok().getIdentifier();
2754 MCRegister OffsetReg = matchRegisterNameHelper(OffsetRegName);
2755 if (!OffsetReg ||
2756 !getRISCVMCRegisterClass(RISCV::GPRRegClassID).contains(OffsetReg))
2757 return Error(getLoc(), "expected GPR register");
2758 getLexer().Lex();
2759
2760 if (parseToken(AsmToken::LParen, "expected '(' or invalid operand"))
2761 return ParseStatus::Failure;
2762
2763 if (getLexer().getKind() != AsmToken::Identifier)
2764 return Error(getLoc(), "expected GPR register");
2765
2766 StringRef BaseRegName = getLexer().getTok().getIdentifier();
2767 MCRegister BaseReg = matchRegisterNameHelper(BaseRegName);
2768 if (!BaseReg ||
2769 !getRISCVMCRegisterClass(RISCV::GPRRegClassID).contains(BaseReg))
2770 return Error(getLoc(), "expected GPR register");
2771 getLexer().Lex();
2772
2773 if (parseToken(AsmToken::RParen, "expected ')'"))
2774 return ParseStatus::Failure;
2775
2776 Operands.push_back(RISCVOperand::createRegReg(BaseReg, OffsetReg, S));
2777
2778 return ParseStatus::Success;
2779}
2780
2781// RegList: {ra [, s0[-sN]]}
2782// XRegList: {x1 [, x8[-x9][, x18[-xN]]]}
2783
2784// When MustIncludeS0 = true (not the default) (used for `qc.cm.pushfp`) which
2785// must include `fp`/`s0` in the list:
2786// RegList: {ra, s0[-sN]}
2787// XRegList: {x1, x8[-x9][, x18[-xN]]}
2788ParseStatus RISCVAsmParser::parseRegList(OperandVector &Operands,
2789 bool MustIncludeS0) {
2790 if (getTok().isNot(AsmToken::LCurly))
2791 return ParseStatus::NoMatch;
2792
2793 SMLoc S = getLoc();
2794
2795 Lex();
2796
2797 bool UsesXRegs;
2798 MCRegister RegEnd;
2799 do {
2800 if (getTok().isNot(AsmToken::Identifier))
2801 return Error(getLoc(), "invalid register");
2802
2803 StringRef RegName = getTok().getIdentifier();
2804 MCRegister Reg = matchRegisterNameHelper(RegName);
2805 if (!Reg)
2806 return Error(getLoc(), "invalid register");
2807
2808 if (!RegEnd) {
2809 UsesXRegs = RegName[0] == 'x';
2810 if (Reg != RISCV::X1)
2811 return Error(getLoc(), "register list must start from 'ra' or 'x1'");
2812 } else if (RegEnd == RISCV::X1) {
2813 if (Reg != RISCV::X8 || (UsesXRegs != (RegName[0] == 'x')))
2814 return Error(getLoc(), Twine("register must be '") +
2815 (UsesXRegs ? "x8" : "s0") + "'");
2816 } else if (RegEnd == RISCV::X9 && UsesXRegs) {
2817 if (Reg != RISCV::X18 || (RegName[0] != 'x'))
2818 return Error(getLoc(), "register must be 'x18'");
2819 } else {
2820 return Error(getLoc(), "too many register ranges");
2821 }
2822
2823 RegEnd = Reg;
2824
2825 Lex();
2826
2827 SMLoc MinusLoc = getLoc();
2828 if (parseOptionalToken(AsmToken::Minus)) {
2829 if (RegEnd == RISCV::X1)
2830 return Error(MinusLoc, Twine("register '") + (UsesXRegs ? "x1" : "ra") +
2831 "' cannot start a multiple register range");
2832
2833 if (getTok().isNot(AsmToken::Identifier))
2834 return Error(getLoc(), "invalid register");
2835
2836 StringRef RegName = getTok().getIdentifier();
2837 MCRegister Reg = matchRegisterNameHelper(RegName);
2838 if (!Reg)
2839 return Error(getLoc(), "invalid register");
2840
2841 if (RegEnd == RISCV::X8) {
2842 if ((Reg != RISCV::X9 &&
2843 (UsesXRegs || Reg < RISCV::X18 || Reg > RISCV::X27)) ||
2844 (UsesXRegs != (RegName[0] == 'x'))) {
2845 if (UsesXRegs)
2846 return Error(getLoc(), "register must be 'x9'");
2847 return Error(getLoc(), "register must be in the range 's1' to 's11'");
2848 }
2849 } else if (RegEnd == RISCV::X18) {
2850 if (Reg < RISCV::X19 || Reg > RISCV::X27 || (RegName[0] != 'x'))
2851 return Error(getLoc(),
2852 "register must be in the range 'x19' to 'x27'");
2853 } else
2854 llvm_unreachable("unexpected register");
2855
2856 RegEnd = Reg;
2857
2858 Lex();
2859 }
2860 } while (parseOptionalToken(AsmToken::Comma));
2861
2862 if (parseToken(AsmToken::RCurly, "expected ',' or '}'"))
2863 return ParseStatus::Failure;
2864
2865 if (RegEnd == RISCV::X26)
2866 return Error(S, "invalid register list, '{ra, s0-s10}' or '{x1, x8-x9, "
2867 "x18-x26}' is not supported");
2868
2869 auto Encode = RISCVZC::encodeRegList(RegEnd, isRVE());
2870 assert(Encode != RISCVZC::INVALID_RLIST);
2871
2872 if (MustIncludeS0 && Encode == RISCVZC::RA)
2873 return Error(S, "register list must include 's0' or 'x8'");
2874
2875 Operands.push_back(RISCVOperand::createRegList(Encode, S));
2876
2877 return ParseStatus::Success;
2878}
2879
2880ParseStatus RISCVAsmParser::parseZcmpStackAdj(OperandVector &Operands,
2881 bool ExpectNegative) {
2882 SMLoc S = getLoc();
2883 bool Negative = parseOptionalToken(AsmToken::Minus);
2884
2885 if (getTok().isNot(AsmToken::Integer))
2886 return ParseStatus::NoMatch;
2887
2888 int64_t StackAdjustment = getTok().getIntVal();
2889
2890 auto *RegListOp = static_cast<RISCVOperand *>(Operands.back().get());
2891 if (!RegListOp->isRegList())
2892 return ParseStatus::NoMatch;
2893
2894 unsigned RlistEncode = RegListOp->RegList.Encoding;
2895
2896 assert(RlistEncode != RISCVZC::INVALID_RLIST);
2897 unsigned StackAdjBase = RISCVZC::getStackAdjBase(RlistEncode, isRV64());
2898 if (Negative != ExpectNegative || StackAdjustment % 16 != 0 ||
2899 StackAdjustment < StackAdjBase || (StackAdjustment - StackAdjBase) > 48) {
2900 int64_t Lower = StackAdjBase;
2901 int64_t Upper = StackAdjBase + 48;
2902 if (ExpectNegative) {
2903 Lower = -Lower;
2904 Upper = -Upper;
2906 }
2907 return generateImmOutOfRangeError(S, Lower, Upper,
2908 "stack adjustment for register list must "
2909 "be a multiple of 16 bytes in the range");
2910 }
2911
2912 unsigned StackAdj = (StackAdjustment - StackAdjBase);
2913 Operands.push_back(RISCVOperand::createStackAdj(StackAdj, S));
2914 Lex();
2915 return ParseStatus::Success;
2916}
2917
2918/// Looks at a token type and creates the relevant operand from this
2919/// information, adding to Operands. If operand was parsed, returns false, else
2920/// true.
2921bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
2922 // Check if the current operand has a custom associated parser, if so, try to
2923 // custom parse the operand, or fallback to the general approach.
2924 ParseStatus Result =
2925 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
2926 if (Result.isSuccess())
2927 return false;
2928 if (Result.isFailure())
2929 return true;
2930
2931 // Attempt to parse token as a register.
2932 if (parseRegister(Operands, true).isSuccess())
2933 return false;
2934
2935 // Attempt to parse token as an expression
2936 if (parseExpression(Operands).isSuccess()) {
2937 // Parse memory base register if present
2938 if (getLexer().is(AsmToken::LParen))
2939 return !parseMemOpBaseReg(Operands).isSuccess();
2940 return false;
2941 }
2942
2943 // Finally we have exhausted all options and must declare defeat.
2944 Error(getLoc(), "unknown operand");
2945 return true;
2946}
2947
2948bool RISCVAsmParser::parseInstruction(ParseInstructionInfo &Info,
2949 StringRef Name, SMLoc NameLoc,
2951 // Apply mnemonic aliases because the destination mnemonic may have require
2952 // custom operand parsing. The generic tblgen'erated code does this later, at
2953 // the start of MatchInstructionImpl(), but that's too late for custom
2954 // operand parsing.
2955 const FeatureBitset &AvailableFeatures = getAvailableFeatures();
2956 applyMnemonicAliases(Name, AvailableFeatures, 0);
2957
2958 // First operand is token for instruction
2959 Operands.push_back(RISCVOperand::createToken(Name, NameLoc));
2960
2961 // If there are no more operands, then finish
2962 if (getLexer().is(AsmToken::EndOfStatement)) {
2963 getParser().Lex(); // Consume the EndOfStatement.
2964 return false;
2965 }
2966
2967 // Parse first operand
2968 if (parseOperand(Operands, Name))
2969 return true;
2970
2971 // Parse until end of statement, consuming commas between operands
2972 while (parseOptionalToken(AsmToken::Comma)) {
2973 // Parse next operand
2974 if (parseOperand(Operands, Name))
2975 return true;
2976 }
2977
2978 if (getParser().parseEOL("unexpected token")) {
2979 getParser().eatToEndOfStatement();
2980 return true;
2981 }
2982 return false;
2983}
2984
2985bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
2986 RISCV::Specifier &Kind) {
2988 if (const auto *RE = dyn_cast<MCSpecifierExpr>(Expr)) {
2989 Kind = RE->getSpecifier();
2990 Expr = RE->getSubExpr();
2991 }
2992
2993 MCValue Res;
2994 if (Expr->evaluateAsRelocatable(Res, nullptr))
2995 return Res.getSpecifier() == RISCV::S_None;
2996 return false;
2997}
2998
2999bool RISCVAsmParser::isSymbolDiff(const MCExpr *Expr) {
3000 MCValue Res;
3001 if (Expr->evaluateAsRelocatable(Res, nullptr)) {
3002 return Res.getSpecifier() == RISCV::S_None && Res.getAddSym() &&
3003 Res.getSubSym();
3004 }
3005 return false;
3006}
3007
3008ParseStatus RISCVAsmParser::parseDirective(AsmToken DirectiveID) {
3009 StringRef IDVal = DirectiveID.getString();
3010
3011 if (IDVal == ".option")
3012 return parseDirectiveOption();
3013 if (IDVal == ".attribute")
3014 return parseDirectiveAttribute();
3015 if (IDVal == ".insn")
3016 return parseDirectiveInsn(DirectiveID.getLoc());
3017 if (IDVal == ".variant_cc")
3018 return parseDirectiveVariantCC();
3019
3020 return ParseStatus::NoMatch;
3021}
3022
3023bool RISCVAsmParser::resetToArch(StringRef Arch, SMLoc Loc, std::string &Result,
3024 bool FromOptionDirective) {
3025 auto ParseResult = llvm::RISCVISAInfo::parseArchString(
3026 Arch, /*EnableExperimentalExtension=*/true,
3027 /*ExperimentalExtensionVersionCheck=*/true);
3028 if (!ParseResult) {
3029 std::string Buffer;
3030 raw_string_ostream OutputErrMsg(Buffer);
3031 handleAllErrors(ParseResult.takeError(), [&](llvm::StringError &ErrMsg) {
3032 OutputErrMsg << "invalid arch name '" << Arch << "', "
3033 << ErrMsg.getMessage();
3034 });
3035
3036 return Error(Loc, OutputErrMsg.str());
3037 }
3038 auto &ISAInfo = *ParseResult;
3039
3040 if (FromOptionDirective) {
3041 if (ISAInfo->getXLen() == 32 && isRV64())
3042 return Error(Loc, "bad arch string switching from rv64 to rv32");
3043 else if (ISAInfo->getXLen() == 64 && !isRV64())
3044 return Error(Loc, "bad arch string switching from rv32 to rv64");
3045 }
3046 if (ISAInfo->getXLen() != 32 && ISAInfo->getXLen() != 64)
3047 return Error(Loc, "bad arch string " + Arch);
3048
3049 // RISCVISAInfo::parseArchString already expands all implied extensions
3050 // (including conditional C/Zce implications like Zcd and Zcf), so we can
3051 // toggle the feature bits directly without calling setFeatureBits() (which
3052 // would run updateCZceFeatureImplications() on an incomplete feature set).
3053 MCSubtargetInfo &STI = copySTI();
3054 for (auto &Feature : STI.getAllProcessorFeatures()) {
3056 continue;
3057 if (ISAInfo->hasExtension(Feature.key()) != STI.hasFeature(Feature.Value))
3058 STI.ToggleFeature(Feature.Value);
3059 }
3060 if ((ISAInfo->getXLen() == 64) != STI.hasFeature(RISCV::Feature64Bit))
3061 STI.ToggleFeature(RISCV::Feature64Bit);
3062 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
3063
3064 Result = ISAInfo->toString();
3065 return false;
3066}
3067
3068bool RISCVAsmParser::parseDirectiveOption() {
3069 MCAsmParser &Parser = getParser();
3070 // Get the option token.
3071 AsmToken Tok = Parser.getTok();
3072
3073 // At the moment only identifiers are supported.
3074 if (parseToken(AsmToken::Identifier, "expected identifier"))
3075 return true;
3076
3077 StringRef Option = Tok.getIdentifier();
3078
3079 if (Option == "push") {
3080 if (Parser.parseEOL())
3081 return true;
3082
3083 getTargetStreamer().emitDirectiveOptionPush();
3084 pushFeatureBits();
3085 return false;
3086 }
3087
3088 if (Option == "pop") {
3089 SMLoc StartLoc = Parser.getTok().getLoc();
3090 if (Parser.parseEOL())
3091 return true;
3092
3093 getTargetStreamer().emitDirectiveOptionPop();
3094 if (popFeatureBits())
3095 return Error(StartLoc, ".option pop with no .option push");
3096
3097 return false;
3098 }
3099
3100 if (Option == "arch") {
3102 do {
3103 if (Parser.parseComma())
3104 return true;
3105
3107 if (parseOptionalToken(AsmToken::Plus))
3108 Type = RISCVOptionArchArgType::Plus;
3109 else if (parseOptionalToken(AsmToken::Minus))
3110 Type = RISCVOptionArchArgType::Minus;
3111 else if (!Args.empty())
3112 return Error(Parser.getTok().getLoc(),
3113 "unexpected token, expected + or -");
3114 else
3115 Type = RISCVOptionArchArgType::Full;
3116
3117 if (Parser.getTok().isNot(AsmToken::Identifier))
3118 return Error(Parser.getTok().getLoc(),
3119 "unexpected token, expected identifier");
3120
3121 StringRef Arch = Parser.getTok().getString();
3122 SMLoc Loc = Parser.getTok().getLoc();
3123 Parser.Lex();
3124
3125 if (Type == RISCVOptionArchArgType::Full) {
3126 std::string Result;
3127 if (resetToArch(Arch, Loc, Result, true))
3128 return true;
3129
3130 Args.emplace_back(Type, Result);
3131 break;
3132 }
3133
3134 if (isDigit(Arch.back()))
3135 return Error(
3136 Loc, "extension version number parsing not currently implemented");
3137
3138 std::string Feature = RISCVISAInfo::getTargetFeatureForExtension(Arch);
3139 if (!enableExperimentalExtension() &&
3140 StringRef(Feature).starts_with("experimental-"))
3141 return Error(Loc, "unexpected experimental extensions");
3142 const auto &AllFeatures = getSTI().getAllProcessorFeatures();
3143 auto Ext = llvm::lower_bound(AllFeatures, Feature);
3144 if (Ext == std::end(AllFeatures) || StringRef(Ext->key()) != Feature)
3145 return Error(Loc, "unknown extension feature");
3146
3147 if (Ext->Value == RISCV::FeatureStdExtY)
3148 return Error(Loc, "passing '+y'/'-y' is not supported yet; specify a "
3149 "full arch string instead");
3150
3151 Args.emplace_back(Type, Arch.str());
3152
3153 if (Type == RISCVOptionArchArgType::Plus) {
3154 FeatureBitset OldFeatureBits = STI->getFeatureBits();
3155
3156 setFeatureBits(Ext->Value, Ext->key());
3157 auto ParseResult = RISCVFeatures::parseFeatureBits(*STI);
3158 if (!ParseResult) {
3159 copySTI().setFeatureBits(OldFeatureBits);
3160 setAvailableFeatures(ComputeAvailableFeatures(OldFeatureBits));
3161
3162 std::string Buffer;
3163 raw_string_ostream OutputErrMsg(Buffer);
3164 handleAllErrors(ParseResult.takeError(), [&](llvm::StringError &ErrMsg) {
3165 OutputErrMsg << ErrMsg.getMessage();
3166 });
3167
3168 return Error(Loc, OutputErrMsg.str());
3169 }
3170 } else {
3171 assert(Type == RISCVOptionArchArgType::Minus);
3172 // It is invalid to disable an extension that there are other enabled
3173 // extensions depend on it.
3174 // TODO: Make use of RISCVISAInfo to handle this
3175 for (auto &Feature : AllFeatures) {
3176 if (getSTI().hasFeature(Feature.Value) &&
3177 Feature.Implies.test(Ext->Value))
3178 return Error(Loc, Twine("can't disable ") + Ext->key() +
3179 " extension; " + Feature.key() +
3180 " extension requires " + Ext->key() +
3181 " extension");
3182 }
3183
3184 clearFeatureBits(Ext->Value, Ext->key());
3185 }
3186 } while (Parser.getTok().isNot(AsmToken::EndOfStatement));
3187
3188 if (Parser.parseEOL())
3189 return true;
3190
3191 getTargetStreamer().emitDirectiveOptionArch(Args);
3192
3193 if (auto ParseResult = RISCVFeatures::parseFeatureBits(*STI))
3194 getTargetStreamer().setArchString((*ParseResult)->toString());
3195 return false;
3196 }
3197
3198 if (Option == "exact") {
3199 if (Parser.parseEOL())
3200 return true;
3201
3202 getTargetStreamer().emitDirectiveOptionExact();
3203 setFeatureBits(RISCV::FeatureExactAssembly, "exact-asm");
3204 clearFeatureBits(RISCV::FeatureRelax, "relax");
3205 return false;
3206 }
3207
3208 if (Option == "noexact") {
3209 if (Parser.parseEOL())
3210 return true;
3211
3212 getTargetStreamer().emitDirectiveOptionNoExact();
3213 clearFeatureBits(RISCV::FeatureExactAssembly, "exact-asm");
3214 setFeatureBits(RISCV::FeatureRelax, "relax");
3215 return false;
3216 }
3217
3218 if (Option == "rvc") {
3219 if (Parser.parseEOL())
3220 return true;
3221
3222 getTargetStreamer().emitDirectiveOptionRVC();
3223 setFeatureBits(RISCV::FeatureStdExtC, "c");
3224 if (auto ParseResult = RISCVFeatures::parseFeatureBits(*STI))
3225 getTargetStreamer().setArchString((*ParseResult)->toString());
3226 return false;
3227 }
3228
3229 if (Option == "norvc") {
3230 if (Parser.parseEOL())
3231 return true;
3232
3233 getTargetStreamer().emitDirectiveOptionNoRVC();
3234 clearFeatureBits(RISCV::FeatureStdExtC, "c");
3235 clearFeatureBits(RISCV::FeatureStdExtZca, "zca");
3236 if (auto ParseResult = RISCVFeatures::parseFeatureBits(*STI))
3237 getTargetStreamer().setArchString((*ParseResult)->toString());
3238 return false;
3239 }
3240
3241 if (Option == "pic") {
3242 if (Parser.parseEOL())
3243 return true;
3244
3245 getTargetStreamer().emitDirectiveOptionPIC();
3246 ParserOptions.IsPicEnabled = true;
3247 return false;
3248 }
3249
3250 if (Option == "nopic") {
3251 if (Parser.parseEOL())
3252 return true;
3253
3254 getTargetStreamer().emitDirectiveOptionNoPIC();
3255 ParserOptions.IsPicEnabled = false;
3256 return false;
3257 }
3258
3259 if (Option == "relax") {
3260 if (Parser.parseEOL())
3261 return true;
3262
3263 getTargetStreamer().emitDirectiveOptionRelax();
3264 setFeatureBits(RISCV::FeatureRelax, "relax");
3265 return false;
3266 }
3267
3268 if (Option == "norelax") {
3269 if (Parser.parseEOL())
3270 return true;
3271
3272 getTargetStreamer().emitDirectiveOptionNoRelax();
3273 clearFeatureBits(RISCV::FeatureRelax, "relax");
3274 return false;
3275 }
3276
3277 // Unknown option.
3278 Warning(Parser.getTok().getLoc(),
3279 "unknown option, expected 'push', 'pop', "
3280 "'rvc', 'norvc', 'arch', 'relax', 'norelax', "
3281 "'exact', or 'noexact'");
3282 Parser.eatToEndOfStatement();
3283 return false;
3284}
3285
3286/// parseDirectiveAttribute
3287/// ::= .attribute expression ',' ( expression | "string" )
3288/// ::= .attribute identifier ',' ( expression | "string" )
3289bool RISCVAsmParser::parseDirectiveAttribute() {
3290 MCAsmParser &Parser = getParser();
3291 int64_t Tag;
3292 SMLoc TagLoc;
3293 TagLoc = Parser.getTok().getLoc();
3294 if (Parser.getTok().is(AsmToken::Identifier)) {
3295 StringRef Name = Parser.getTok().getIdentifier();
3296 std::optional<unsigned> Ret =
3298 if (!Ret)
3299 return Error(TagLoc, "attribute name not recognised: " + Name);
3300 Tag = *Ret;
3301 Parser.Lex();
3302 } else {
3303 const MCExpr *AttrExpr;
3304
3305 TagLoc = Parser.getTok().getLoc();
3306 if (Parser.parseExpression(AttrExpr))
3307 return true;
3308
3309 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
3310 if (check(!CE, TagLoc, "expected numeric constant"))
3311 return true;
3312
3313 Tag = CE->getValue();
3314 }
3315
3316 if (Parser.parseComma())
3317 return true;
3318
3319 StringRef StringValue;
3320 int64_t IntegerValue = 0;
3321 bool IsIntegerValue = true;
3322
3323 // RISC-V attributes have a string value if the tag number is odd
3324 // and an integer value if the tag number is even.
3325 if (Tag % 2)
3326 IsIntegerValue = false;
3327
3328 SMLoc ValueExprLoc = Parser.getTok().getLoc();
3329 if (IsIntegerValue) {
3330 const MCExpr *ValueExpr;
3331 if (Parser.parseExpression(ValueExpr))
3332 return true;
3333
3334 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
3335 if (!CE)
3336 return Error(ValueExprLoc, "expected numeric constant");
3337 IntegerValue = CE->getValue();
3338 } else {
3339 if (Parser.getTok().isNot(AsmToken::String))
3340 return Error(Parser.getTok().getLoc(), "expected string constant");
3341
3342 StringValue = Parser.getTok().getStringContents();
3343 Parser.Lex();
3344 }
3345
3346 if (Parser.parseEOL())
3347 return true;
3348
3349 if (IsIntegerValue)
3350 getTargetStreamer().emitAttribute(Tag, IntegerValue);
3351 else if (Tag != RISCVAttrs::ARCH)
3352 getTargetStreamer().emitTextAttribute(Tag, StringValue);
3353 else {
3354 std::string Result;
3355 if (resetToArch(StringValue, ValueExprLoc, Result, false))
3356 return true;
3357
3358 // Then emit the arch string.
3359 getTargetStreamer().emitTextAttribute(Tag, Result);
3360
3361 // And then update the active ISA so the next instruction-run emits
3362 // an ISA-specific mapping symbol.
3363 getTargetStreamer().setArchString(Result);
3364 }
3365
3366 return false;
3367}
3368
3370 return StringSwitch<bool>(Format)
3371 .Cases({"r", "r4", "i", "b", "sb", "u", "j", "uj", "s"}, true)
3372 .Cases({"cr", "ci", "ciw", "css", "cl", "cs", "ca", "cb", "cj"},
3373 STI.hasFeature(RISCV::FeatureStdExtZca))
3374 .Cases({"qc.eai", "qc.ei", "qc.eb", "qc.ej", "qc.es"},
3375 !STI.hasFeature(RISCV::Feature64Bit))
3376 .Default(false);
3377}
3378
3379/// parseDirectiveInsn
3380/// ::= .insn [ format encoding, (operands (, operands)*) ]
3381/// ::= .insn [ length, value ]
3382/// ::= .insn [ value ]
3383bool RISCVAsmParser::parseDirectiveInsn(SMLoc L) {
3384 MCAsmParser &Parser = getParser();
3385
3386 // Expect instruction format as identifier.
3387 StringRef Format;
3388 SMLoc ErrorLoc = Parser.getTok().getLoc();
3389 if (Parser.parseIdentifier(Format)) {
3390 // Try parsing .insn [ length , ] value
3391 std::optional<int64_t> Length;
3392 int64_t Value = 0;
3393 if (Parser.parseAbsoluteExpression(Value))
3394 return true;
3395 if (Parser.parseOptionalToken(AsmToken::Comma)) {
3396 Length = Value;
3397 if (Parser.parseAbsoluteExpression(Value))
3398 return true;
3399
3400 if (*Length == 0 || (*Length % 2) != 0)
3401 return Error(ErrorLoc,
3402 "instruction lengths must be a non-zero multiple of two");
3403
3404 // TODO: Support Instructions > 64 bits.
3405 if (*Length > 8)
3406 return Error(ErrorLoc,
3407 "instruction lengths over 64 bits are not supported");
3408 }
3409
3410 // We only derive a length from the encoding for 16- and 32-bit
3411 // instructions, as the encodings for longer instructions are not frozen in
3412 // the spec.
3413 int64_t EncodingDerivedLength = ((Value & 0b11) == 0b11) ? 4 : 2;
3414
3415 if (Length) {
3416 // Only check the length against the encoding if the length is present and
3417 // could match
3418 if ((*Length <= 4) && (*Length != EncodingDerivedLength))
3419 return Error(ErrorLoc,
3420 "instruction length does not match the encoding");
3421
3422 if (!isUIntN(*Length * 8, Value))
3423 return Error(ErrorLoc, "encoding value does not fit into instruction");
3424 } else {
3425 if (!isUIntN(EncodingDerivedLength * 8, Value))
3426 return Error(ErrorLoc, "encoding value does not fit into instruction");
3427 }
3428
3429 if (!getSTI().hasFeature(RISCV::FeatureStdExtZca) &&
3430 (EncodingDerivedLength == 2))
3431 return Error(ErrorLoc, "compressed instructions are not allowed");
3432
3433 if (getParser().parseEOL("invalid operand for instruction")) {
3434 getParser().eatToEndOfStatement();
3435 return true;
3436 }
3437
3438 unsigned Opcode;
3439 if (Length) {
3440 switch (*Length) {
3441 case 2:
3442 Opcode = RISCV::Insn16;
3443 break;
3444 case 4:
3445 Opcode = RISCV::Insn32;
3446 break;
3447 case 6:
3448 Opcode = RISCV::Insn48;
3449 break;
3450 case 8:
3451 Opcode = RISCV::Insn64;
3452 break;
3453 default:
3454 llvm_unreachable("Error should have already been emitted");
3455 }
3456 } else
3457 Opcode = (EncodingDerivedLength == 2) ? RISCV::Insn16 : RISCV::Insn32;
3458
3459 emitToStreamer(getStreamer(), MCInstBuilder(Opcode).addImm(Value));
3460 return false;
3461 }
3462
3463 if (!isValidInsnFormat(Format, getSTI()))
3464 return Error(ErrorLoc, "invalid instruction format");
3465
3466 std::string FormatName = (".insn_" + Format).str();
3467
3468 ParseInstructionInfo Info;
3470
3471 if (parseInstruction(Info, FormatName, L, Operands))
3472 return true;
3473
3474 unsigned Opcode;
3475 uint64_t ErrorInfo;
3476 return matchAndEmitInstruction(L, Opcode, Operands, Parser.getStreamer(),
3477 ErrorInfo,
3478 /*MatchingInlineAsm=*/false);
3479}
3480
3481/// parseDirectiveVariantCC
3482/// ::= .variant_cc symbol
3483bool RISCVAsmParser::parseDirectiveVariantCC() {
3484 StringRef Name;
3485 if (getParser().parseIdentifier(Name))
3486 return TokError("expected symbol name");
3487 if (parseEOL())
3488 return true;
3489 getTargetStreamer().emitDirectiveVariantCC(
3490 *getContext().getOrCreateSymbol(Name));
3491 return false;
3492}
3493
3494void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
3495 MCInst CInst;
3496 bool Res = false;
3497 const MCSubtargetInfo &STI = getSTI();
3498 if (!STI.hasFeature(RISCV::FeatureExactAssembly))
3499 Res = RISCVRVC::compress(CInst, Inst, STI);
3500 if (Res)
3501 ++RISCVNumInstrsCompressed;
3502 S.emitInstruction((Res ? CInst : Inst), STI);
3503}
3504
3505void RISCVAsmParser::emitLoadImm(MCRegister DestReg, int64_t Value,
3506 MCStreamer &Out) {
3508 RISCVMatInt::generateMCInstSeq(Value, getSTI(), DestReg, Seq);
3509
3510 for (MCInst &Inst : Seq) {
3511 emitToStreamer(Out, Inst);
3512 }
3513}
3514
3515void RISCVAsmParser::emitAuipcInstPair(MCRegister DestReg, MCRegister TmpReg,
3516 const MCExpr *Symbol,
3517 RISCV::Specifier VKHi,
3518 unsigned SecondOpcode, SMLoc IDLoc,
3519 MCStreamer &Out) {
3520 // A pair of instructions for PC-relative addressing; expands to
3521 // TmpLabel: AUIPC TmpReg, VKHi(symbol)
3522 // OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
3523 MCContext &Ctx = getContext();
3524
3525 MCSymbol *TmpLabel = Ctx.createNamedTempSymbol("pcrel_hi");
3526 Out.emitLabel(TmpLabel);
3527
3528 const auto *SymbolHi = MCSpecifierExpr::create(Symbol, VKHi, Ctx);
3529 emitToStreamer(Out,
3530 MCInstBuilder(RISCV::AUIPC).addReg(TmpReg).addExpr(SymbolHi));
3531
3532 const MCExpr *RefToLinkTmpLabel = MCSpecifierExpr::create(
3533 MCSymbolRefExpr::create(TmpLabel, Ctx), RISCV::S_PCREL_LO, Ctx);
3534
3535 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
3536 .addReg(DestReg)
3537 .addReg(TmpReg)
3538 .addExpr(RefToLinkTmpLabel));
3539}
3540
3541void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
3542 MCStreamer &Out) {
3543 // The load local address pseudo-instruction "lla" is used in PC-relative
3544 // addressing of local symbols:
3545 // lla rdest, symbol
3546 // expands to
3547 // TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
3548 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
3549 MCRegister DestReg = Inst.getOperand(0).getReg();
3550 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
3551 if (STI->hasFeature(RISCV::Feature32Bit) &&
3552 STI->hasFeature(RISCV::FeatureVendorXqcili) &&
3553 !ParserOptions.IsPicEnabled)
3554 emitToStreamer(
3555 Out, MCInstBuilder(RISCV::QC_E_LI).addReg(DestReg).addExpr(Symbol));
3556 else
3557 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCV::S_PCREL_HI, RISCV::ADDI,
3558 IDLoc, Out);
3559}
3560
3561void RISCVAsmParser::emitLoadGlobalAddress(MCInst &Inst, SMLoc IDLoc,
3562 MCStreamer &Out) {
3563 // The load global address pseudo-instruction "lga" is used in GOT-indirect
3564 // addressing of global symbols:
3565 // lga rdest, symbol
3566 // expands to
3567 // TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
3568 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
3569 MCRegister DestReg = Inst.getOperand(0).getReg();
3570 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
3571 unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
3572 emitAuipcInstPair(DestReg, DestReg, Symbol, RISCV::S_GOT_HI, SecondOpcode,
3573 IDLoc, Out);
3574}
3575
3576void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
3577 MCStreamer &Out) {
3578 // The load address pseudo-instruction "la" is used in PC-relative and
3579 // GOT-indirect addressing of global symbols:
3580 // la rdest, symbol
3581 // is an alias for either (for non-PIC)
3582 // lla rdest, symbol
3583 // or (for PIC)
3584 // lga rdest, symbol
3585 if (ParserOptions.IsPicEnabled)
3586 emitLoadGlobalAddress(Inst, IDLoc, Out);
3587 else
3588 emitLoadLocalAddress(Inst, IDLoc, Out);
3589}
3590
3591void RISCVAsmParser::emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc,
3592 MCStreamer &Out) {
3593 // The load TLS IE address pseudo-instruction "la.tls.ie" is used in
3594 // initial-exec TLS model addressing of global symbols:
3595 // la.tls.ie rdest, symbol
3596 // expands to
3597 // TmpLabel: AUIPC rdest, %tls_ie_pcrel_hi(symbol)
3598 // Lx rdest, %pcrel_lo(TmpLabel)(rdest)
3599 MCRegister DestReg = Inst.getOperand(0).getReg();
3600 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
3601 unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
3602 emitAuipcInstPair(DestReg, DestReg, Symbol, ELF::R_RISCV_TLS_GOT_HI20,
3603 SecondOpcode, IDLoc, Out);
3604}
3605
3606void RISCVAsmParser::emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc,
3607 MCStreamer &Out) {
3608 // The load TLS GD address pseudo-instruction "la.tls.gd" is used in
3609 // global-dynamic TLS model addressing of global symbols:
3610 // la.tls.gd rdest, symbol
3611 // expands to
3612 // TmpLabel: AUIPC rdest, %tls_gd_pcrel_hi(symbol)
3613 // ADDI rdest, rdest, %pcrel_lo(TmpLabel)
3614 MCRegister DestReg = Inst.getOperand(0).getReg();
3615 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
3616 emitAuipcInstPair(DestReg, DestReg, Symbol, ELF::R_RISCV_TLS_GD_HI20,
3617 RISCV::ADDI, IDLoc, Out);
3618}
3619
3620void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
3621 SMLoc IDLoc, MCStreamer &Out,
3622 bool HasTmpReg) {
3623 // The load/store pseudo-instruction does a pc-relative load with
3624 // a symbol.
3625 //
3626 // The expansion looks like this
3627 //
3628 // TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
3629 // [S|L]X rd, %pcrel_lo(TmpLabel)(tmp)
3630 unsigned DestRegOpIdx = HasTmpReg ? 1 : 0;
3631 MCRegister DestReg = Inst.getOperand(DestRegOpIdx).getReg();
3632 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
3633 MCRegister TmpReg = Inst.getOperand(0).getReg();
3634
3635 // If TmpReg is a GPR pair, get the even register.
3636 if (getRISCVMCRegisterClass(RISCV::GPRPairRegClassID).contains(TmpReg)) {
3637 const MCRegisterInfo *RI = getContext().getRegisterInfo();
3638 TmpReg = RI->getSubReg(TmpReg, RISCV::sub_gpr_even);
3639 }
3640
3641 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
3642 emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCV::S_PCREL_HI, Opcode, IDLoc,
3643 Out);
3644}
3645
3646void RISCVAsmParser::emitQCELILoadStoreSymbol(MCInst &Inst, unsigned Opcode,
3647 SMLoc IDLoc, MCStreamer &Out,
3648 bool HasTmpReg) {
3649 // For loads (HasTmpReg=false): operands are [rd, symbol]
3650 // qc.e.li rd, symbol
3651 // lx rd, 0(rd), %qc.access(symbol) [possibly compressed]
3652 //
3653 // For stores (HasTmpReg=true): operands are [rt, rs, symbol]
3654 // qc.e.li rt, symbol
3655 // sx rs, 0(rt), %qc.access(symbol) [possibly compressed]
3656 MCRegister AddrReg = Inst.getOperand(0).getReg();
3657 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
3658 const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
3659
3660 emitToStreamer(Out,
3661 MCInstBuilder(RISCV::QC_E_LI).addReg(AddrReg).addExpr(Symbol));
3662
3663 MCContext &Ctx = getContext();
3664 const MCExpr *AccessExpr =
3666
3667 // We have to manually compress the QCAccess pseudos as the current
3668 // CompressPat mechanism does not support them. Each entry pairs the
3669 // compressed opcode with the subtarget feature it requires.
3670 struct CompressedForm {
3671 unsigned Opcode;
3672 unsigned Feature;
3673 };
3674 std::optional<CompressedForm> Compressed;
3675 switch (Opcode) {
3676 default:
3677 break;
3678 case RISCV::PseudoQCAccessLBU:
3679 Compressed = {RISCV::PseudoQCAccessC_LBU, RISCV::FeatureStdExtZcb};
3680 break;
3681 case RISCV::PseudoQCAccessLH:
3682 Compressed = {RISCV::PseudoQCAccessC_LH, RISCV::FeatureStdExtZcb};
3683 break;
3684 case RISCV::PseudoQCAccessLHU:
3685 Compressed = {RISCV::PseudoQCAccessC_LHU, RISCV::FeatureStdExtZcb};
3686 break;
3687 case RISCV::PseudoQCAccessLW:
3688 Compressed = {RISCV::PseudoQCAccessC_LW, RISCV::FeatureStdExtZca};
3689 break;
3690 case RISCV::PseudoQCAccessSB:
3691 Compressed = {RISCV::PseudoQCAccessC_SB, RISCV::FeatureStdExtZcb};
3692 break;
3693 case RISCV::PseudoQCAccessSH:
3694 Compressed = {RISCV::PseudoQCAccessC_SH, RISCV::FeatureStdExtZcb};
3695 break;
3696 case RISCV::PseudoQCAccessSW:
3697 Compressed = {RISCV::PseudoQCAccessC_SW, RISCV::FeatureStdExtZca};
3698 break;
3699 }
3700
3701 // For stores, both the data register and the address register must be in
3702 // GPRC for the compressed form; for loads AddrReg serves as both.
3703 bool CanUseGPRC =
3704 getRISCVMCRegisterClass(RISCV::GPRCRegClassID).contains(AddrReg);
3705 if (HasTmpReg && CanUseGPRC) {
3706 MCRegister DataReg = Inst.getOperand(1).getReg();
3707 CanUseGPRC =
3708 getRISCVMCRegisterClass(RISCV::GPRCRegClassID).contains(DataReg);
3709 }
3710
3711 bool UseCompressed =
3712 Compressed && getSTI().hasFeature(Compressed->Feature) && CanUseGPRC;
3713
3714 unsigned ActualOpcode = UseCompressed ? Compressed->Opcode : Opcode;
3715 if (HasTmpReg) {
3716 MCRegister DataReg = Inst.getOperand(1).getReg();
3717 emitToStreamer(Out, MCInstBuilder(ActualOpcode)
3718 .addReg(DataReg)
3719 .addReg(AddrReg)
3720 .addImm(0)
3721 .addExpr(AccessExpr));
3722 } else {
3723 emitToStreamer(Out, MCInstBuilder(ActualOpcode)
3724 .addReg(AddrReg)
3725 .addReg(AddrReg)
3726 .addImm(0)
3727 .addExpr(AccessExpr));
3728 }
3729}
3730
3731void RISCVAsmParser::emitPseudoExtend(MCInst &Inst, bool SignExtend,
3732 int64_t Width, SMLoc IDLoc,
3733 MCStreamer &Out) {
3734 // The sign/zero extend pseudo-instruction does two shifts, with the shift
3735 // amounts dependent on the XLEN.
3736 //
3737 // The expansion looks like this
3738 //
3739 // SLLI rd, rs, XLEN - Width
3740 // SR[A|R]I rd, rd, XLEN - Width
3741 const MCOperand &DestReg = Inst.getOperand(0);
3742 const MCOperand &SourceReg = Inst.getOperand(1);
3743
3744 unsigned SecondOpcode = SignExtend ? RISCV::SRAI : RISCV::SRLI;
3745 int64_t ShAmt = (isRV64() ? 64 : 32) - Width;
3746
3747 assert(ShAmt > 0 && "Shift amount must be non-zero.");
3748
3749 emitToStreamer(Out, MCInstBuilder(RISCV::SLLI)
3750 .addOperand(DestReg)
3751 .addOperand(SourceReg)
3752 .addImm(ShAmt));
3753
3754 emitToStreamer(Out, MCInstBuilder(SecondOpcode)
3755 .addOperand(DestReg)
3756 .addOperand(DestReg)
3757 .addImm(ShAmt));
3758}
3759
3760void RISCVAsmParser::emitVMSGE(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
3761 MCStreamer &Out) {
3762 if (Inst.getNumOperands() == 4 && !Inst.getOperand(3).getReg()) {
3763 // unmasked va >= x
3764 //
3765 // pseudoinstruction: vmsge{u}.vx vd, va, x
3766 // expansion: vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd
3767 emitToStreamer(Out, MCInstBuilder(Opcode)
3768 .addOperand(Inst.getOperand(0))
3769 .addOperand(Inst.getOperand(1))
3770 .addOperand(Inst.getOperand(2))
3771 .addReg(MCRegister())
3772 .setLoc(IDLoc));
3773 emitToStreamer(Out, MCInstBuilder(RISCV::VMNAND_MM)
3774 .addOperand(Inst.getOperand(0))
3775 .addOperand(Inst.getOperand(0))
3776 .addOperand(Inst.getOperand(0))
3777 .setLoc(IDLoc));
3778 } else if (Inst.getNumOperands() == 4) {
3779 // masked va >= x, vd != v0
3780 //
3781 // pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t
3782 // expansion: vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0
3783 assert(Inst.getOperand(0).getReg() != RISCV::V0 &&
3784 "The destination register should not be V0.");
3785 assert(Inst.getOperand(3).getReg() == RISCV::V0 && "Expected a mask");
3786 emitToStreamer(Out, MCInstBuilder(Opcode)
3787 .addOperand(Inst.getOperand(0))
3788 .addOperand(Inst.getOperand(1))
3789 .addOperand(Inst.getOperand(2))
3790 .addOperand(Inst.getOperand(3))
3791 .setLoc(IDLoc));
3792 emitToStreamer(Out, MCInstBuilder(RISCV::VMXOR_MM)
3793 .addOperand(Inst.getOperand(0))
3794 .addOperand(Inst.getOperand(0))
3795 .addReg(RISCV::V0)
3796 .setLoc(IDLoc));
3797 } else if (Inst.getNumOperands() == 5 &&
3798 Inst.getOperand(0).getReg() == RISCV::V0) {
3799 // masked va >= x, vd == v0
3800 //
3801 // pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t, vt
3802 // expansion: vmslt{u}.vx vt, va, x; vmandn.mm vd, vd, vt
3803 assert(Inst.getOperand(1).getReg() != RISCV::V0 &&
3804 "The temporary vector register should not be V0.");
3805 emitToStreamer(Out, MCInstBuilder(Opcode)
3806 .addOperand(Inst.getOperand(1))
3807 .addOperand(Inst.getOperand(2))
3808 .addOperand(Inst.getOperand(3))
3809 .addReg(MCRegister())
3810 .setLoc(IDLoc));
3811 emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
3812 .addOperand(Inst.getOperand(0))
3813 .addOperand(Inst.getOperand(0))
3814 .addOperand(Inst.getOperand(1))
3815 .setLoc(IDLoc));
3816 } else if (Inst.getNumOperands() == 5) {
3817 // masked va >= x, any vd
3818 //
3819 // pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t, vt
3820 // expansion: vmslt{u}.vx vt, va, x; vmandn.mm vt, v0, vt;
3821 // vmandn.mm vd, vd, v0; vmor.mm vd, vt, vd
3822 assert(Inst.getOperand(1).getReg() != RISCV::V0 &&
3823 "The temporary vector register should not be V0.");
3824 emitToStreamer(Out, MCInstBuilder(Opcode)
3825 .addOperand(Inst.getOperand(1))
3826 .addOperand(Inst.getOperand(2))
3827 .addOperand(Inst.getOperand(3))
3828 .addReg(MCRegister())
3829 .setLoc(IDLoc));
3830 emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
3831 .addOperand(Inst.getOperand(1))
3832 .addReg(RISCV::V0)
3833 .addOperand(Inst.getOperand(1))
3834 .setLoc(IDLoc));
3835 emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
3836 .addOperand(Inst.getOperand(0))
3837 .addOperand(Inst.getOperand(0))
3838 .addReg(RISCV::V0)
3839 .setLoc(IDLoc));
3840 emitToStreamer(Out, MCInstBuilder(RISCV::VMOR_MM)
3841 .addOperand(Inst.getOperand(0))
3842 .addOperand(Inst.getOperand(1))
3843 .addOperand(Inst.getOperand(0))
3844 .setLoc(IDLoc));
3845 }
3846}
3847
3848bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
3850 assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
3851 assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
3852 if (Inst.getOperand(2).getReg() != RISCV::X4) {
3853 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
3854 return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
3855 "%tprel_add specifier");
3856 }
3857
3858 return false;
3859}
3860
3861bool RISCVAsmParser::checkPseudoTLSDESCCall(MCInst &Inst,
3863 assert(Inst.getOpcode() == RISCV::PseudoTLSDESCCall && "Invalid instruction");
3864 assert(Inst.getOperand(0).isReg() && "Unexpected operand kind");
3865 if (Inst.getOperand(0).getReg() != RISCV::X5) {
3866 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
3867 return Error(ErrorLoc, "the output operand must be t0/x5 when using "
3868 "%tlsdesc_call specifier");
3869 }
3870
3871 return false;
3872}
3873
3874std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultMaskRegOp() const {
3875 return RISCVOperand::createReg(MCRegister(), llvm::SMLoc(), llvm::SMLoc());
3876}
3877
3878std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultFRMArgOp() const {
3879 return RISCVOperand::createFRMArg(RISCVFPRndMode::RoundingMode::DYN,
3880 llvm::SMLoc());
3881}
3882
3883std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultFRMArgLegacyOp() const {
3884 return RISCVOperand::createFRMArg(RISCVFPRndMode::RoundingMode::RNE,
3885 llvm::SMLoc());
3886}
3887
3888std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultZeroOffset() {
3889 return RISCVOperand::createExpr(MCConstantExpr::create(0, getContext()),
3890 llvm::SMLoc(), llvm::SMLoc(), isRV64());
3891}
3892
3893static unsigned getNFforLXSEG(unsigned Opcode) {
3894 switch (Opcode) {
3895 default:
3896 return 1;
3897 case RISCV::VLOXSEG2EI8_V:
3898 case RISCV::VLOXSEG2EI16_V:
3899 case RISCV::VLOXSEG2EI32_V:
3900 case RISCV::VLOXSEG2EI64_V:
3901 case RISCV::VLUXSEG2EI8_V:
3902 case RISCV::VLUXSEG2EI16_V:
3903 case RISCV::VLUXSEG2EI32_V:
3904 case RISCV::VLUXSEG2EI64_V:
3905 return 2;
3906 case RISCV::VLOXSEG3EI8_V:
3907 case RISCV::VLOXSEG3EI16_V:
3908 case RISCV::VLOXSEG3EI32_V:
3909 case RISCV::VLOXSEG3EI64_V:
3910 case RISCV::VLUXSEG3EI8_V:
3911 case RISCV::VLUXSEG3EI16_V:
3912 case RISCV::VLUXSEG3EI32_V:
3913 case RISCV::VLUXSEG3EI64_V:
3914 return 3;
3915 case RISCV::VLOXSEG4EI8_V:
3916 case RISCV::VLOXSEG4EI16_V:
3917 case RISCV::VLOXSEG4EI32_V:
3918 case RISCV::VLOXSEG4EI64_V:
3919 case RISCV::VLUXSEG4EI8_V:
3920 case RISCV::VLUXSEG4EI16_V:
3921 case RISCV::VLUXSEG4EI32_V:
3922 case RISCV::VLUXSEG4EI64_V:
3923 return 4;
3924 case RISCV::VLOXSEG5EI8_V:
3925 case RISCV::VLOXSEG5EI16_V:
3926 case RISCV::VLOXSEG5EI32_V:
3927 case RISCV::VLOXSEG5EI64_V:
3928 case RISCV::VLUXSEG5EI8_V:
3929 case RISCV::VLUXSEG5EI16_V:
3930 case RISCV::VLUXSEG5EI32_V:
3931 case RISCV::VLUXSEG5EI64_V:
3932 return 5;
3933 case RISCV::VLOXSEG6EI8_V:
3934 case RISCV::VLOXSEG6EI16_V:
3935 case RISCV::VLOXSEG6EI32_V:
3936 case RISCV::VLOXSEG6EI64_V:
3937 case RISCV::VLUXSEG6EI8_V:
3938 case RISCV::VLUXSEG6EI16_V:
3939 case RISCV::VLUXSEG6EI32_V:
3940 case RISCV::VLUXSEG6EI64_V:
3941 return 6;
3942 case RISCV::VLOXSEG7EI8_V:
3943 case RISCV::VLOXSEG7EI16_V:
3944 case RISCV::VLOXSEG7EI32_V:
3945 case RISCV::VLOXSEG7EI64_V:
3946 case RISCV::VLUXSEG7EI8_V:
3947 case RISCV::VLUXSEG7EI16_V:
3948 case RISCV::VLUXSEG7EI32_V:
3949 case RISCV::VLUXSEG7EI64_V:
3950 return 7;
3951 case RISCV::VLOXSEG8EI8_V:
3952 case RISCV::VLOXSEG8EI16_V:
3953 case RISCV::VLOXSEG8EI32_V:
3954 case RISCV::VLOXSEG8EI64_V:
3955 case RISCV::VLUXSEG8EI8_V:
3956 case RISCV::VLUXSEG8EI16_V:
3957 case RISCV::VLUXSEG8EI32_V:
3958 case RISCV::VLUXSEG8EI64_V:
3959 return 8;
3960 }
3961}
3962
3964 if (getRISCVMCRegisterClass(RISCV::VRM2RegClassID).contains(Reg))
3965 return 2;
3966 if (getRISCVMCRegisterClass(RISCV::VRM4RegClassID).contains(Reg))
3967 return 4;
3968 if (getRISCVMCRegisterClass(RISCV::VRM8RegClassID).contains(Reg))
3969 return 8;
3970 return 1;
3971}
3972
3973static bool isZvvfmmScaleOpcode(unsigned Opcode) {
3974 switch (Opcode) {
3975 case RISCV::VFWMMACC_VV_SCALE:
3976 case RISCV::VFQMMACC_VV_SCALE:
3977 case RISCV::VF8WMMACC_VV_SCALE:
3978 case RISCV::VFWIMMACC_VV:
3979 case RISCV::VFQIMMACC_VV:
3980 case RISCV::VF8WIMMACC_VV:
3981 return true;
3982 default:
3983 return false;
3984 }
3985}
3986
3987bool RISCVAsmParser::validateInstruction(MCInst &Inst,
3989 unsigned Opcode = Inst.getOpcode();
3990
3991 if (Opcode == RISCV::PseudoVMSGEU_VX_M_T ||
3992 Opcode == RISCV::PseudoVMSGE_VX_M_T) {
3993 MCRegister DestReg = Inst.getOperand(0).getReg();
3994 MCRegister TempReg = Inst.getOperand(1).getReg();
3995 if (DestReg == TempReg) {
3996 SMLoc Loc = Operands.back()->getStartLoc();
3997 return Error(Loc, "the temporary vector register cannot be the same as "
3998 "the destination register");
3999 }
4000 }
4001
4002 if (Opcode == RISCV::PseudoVMSGEU_VX_M || Opcode == RISCV::PseudoVMSGE_VX_M) {
4003 MCRegister DestReg = Inst.getOperand(0).getReg();
4004 MCRegister MaskReg = Inst.getOperand(3).getReg();
4005 if (MaskReg == RISCV::V0 && DestReg == RISCV::V0) {
4006 SMLoc Loc = Operands.back()->getStartLoc();
4007 return Error(Loc, "the destination vector register cannot overlap the "
4008 "mask register unless a temporary register is "
4009 "provided");
4010 }
4011 }
4012
4013 if (Opcode == RISCV::CV_INSERT &&
4014 Inst.getOperand(3).getImm() + Inst.getOperand(4).getImm() >= 32)
4015 return Error(Operands[3]->getStartLoc(),
4016 "the sum of the immediate operands must be less than 32");
4017
4018 switch (Opcode) {
4019 default:
4020 break;
4021 case RISCV::TH_LBIA:
4022 case RISCV::TH_LBIB:
4023 case RISCV::TH_LBUIA:
4024 case RISCV::TH_LBUIB:
4025 case RISCV::TH_LHIA:
4026 case RISCV::TH_LHIB:
4027 case RISCV::TH_LHUIA:
4028 case RISCV::TH_LHUIB:
4029 case RISCV::TH_LWIA:
4030 case RISCV::TH_LWIB:
4031 case RISCV::TH_LWUIA:
4032 case RISCV::TH_LWUIB:
4033 case RISCV::TH_LDIA:
4034 case RISCV::TH_LDIB:
4035 if (Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg())
4036 return Error(Operands[1]->getStartLoc(), "rd and rs1 must be different");
4037 break;
4038 case RISCV::TH_LDD:
4039 case RISCV::TH_LWUD:
4040 case RISCV::TH_LWD: {
4041 MCRegister Rd1 = Inst.getOperand(0).getReg();
4042 MCRegister Rd2 = Inst.getOperand(1).getReg();
4043 MCRegister Rs1 = Inst.getOperand(2).getReg();
4044 // The encoding with overlapping rs1, rd1, and rd2 is reserved for XTHead
4045 // load pair.
4046 if (Rs1 == Rd1 || Rs1 == Rd2 || Rd1 == Rd2) {
4047 SMLoc Loc = Operands[1]->getStartLoc();
4048 return Error(Loc, "rs1, rd1, and rd2 cannot overlap");
4049 }
4050 break;
4051 }
4052 }
4053
4054 if (Opcode == RISCV::CM_MVSA01 || Opcode == RISCV::QC_CM_MVSA01) {
4055 MCRegister Rs1 = Inst.getOperand(0).getReg();
4056 MCRegister Rs2 = Inst.getOperand(1).getReg();
4057 if (Rs1 == Rs2) {
4058 SMLoc Loc = Operands[1]->getStartLoc();
4059 return Error(Loc, "rs1 and rs2 must be different");
4060 }
4061 }
4062
4063 if (isZvvfmmScaleOpcode(Opcode)) {
4064 auto CheckOperandDoesNotOverlapV0 = [&](int OperandIdx,
4065 unsigned ParsedIdx) {
4066 if (Inst.getOperand(OperandIdx).getReg() == RISCV::V0)
4067 return Error(Operands[ParsedIdx]->getStartLoc(),
4068 "vd, vs1, and vs2 cannot overlap v0.scale");
4069 return false;
4070 };
4071
4072 int DestIdx =
4073 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vd);
4074 int VS1Idx =
4075 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vs1);
4076 int VS2Idx =
4077 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vs2);
4078 assert(DestIdx >= 0 && VS1Idx >= 0 && VS2Idx >= 0 &&
4079 "Unexpected Zvvfmm scaled operand list");
4080
4081 if (CheckOperandDoesNotOverlapV0(DestIdx, 1) ||
4082 CheckOperandDoesNotOverlapV0(VS1Idx, 2) ||
4083 CheckOperandDoesNotOverlapV0(VS2Idx, 3))
4084 return true;
4085 }
4086
4087 const MCInstrDesc &MCID = MII.get(Opcode);
4088 if (!(MCID.TSFlags & RISCVII::RVVConstraintMask))
4089 return false;
4090
4091 int DestIdx = RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vd);
4092 MCRegister DestReg = Inst.getOperand(DestIdx).getReg();
4093
4094 // Operands[1] or Operands[2] will be the first operand, DestReg.
4095 const MCParsedAsmOperand *ParsedOp = Operands[1].get();
4096 if (!ParsedOp->isReg()) {
4097 // XSfvcp instructions may have an immediate before vd.
4098 // FIXME: Is there a better way to do this?
4099 ParsedOp = Operands[2].get();
4100 }
4101 const MCRegisterInfo *RI = getContext().getRegisterInfo();
4102 unsigned DestEncoding = RI->getEncodingValue(DestReg);
4103 assert(RI->getEncodingValue(ParsedOp->getReg()) == DestEncoding &&
4104 "Can't find parsed dest operand");
4105 SMLoc Loc = ParsedOp->getStartLoc();
4106
4107 unsigned Lmul = getLMULFromVectorRegister(DestReg);
4108 if (MCID.TSFlags & RISCVII::VS2Constraint) {
4109 int VS2Idx =
4110 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vs2);
4111 assert(VS2Idx >= 0 && "No vs2 operand?");
4112 unsigned CheckEncoding =
4113 RI->getEncodingValue(Inst.getOperand(VS2Idx).getReg());
4114 unsigned NF = getNFforLXSEG(Opcode);
4115 for (unsigned i = 0; i < std::max(NF, Lmul); i++) {
4116 if ((DestEncoding + i) == CheckEncoding)
4117 return Error(Loc, "the destination vector register group cannot overlap"
4118 " the source vector register group");
4119 }
4120 }
4121 if (MCID.TSFlags & RISCVII::VS1Constraint) {
4122 int VS1Idx =
4123 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vs1);
4124 // FIXME: The vs1 constraint is used on scalar and imm instructions so we
4125 // need to check that the operand exists.
4126 if (VS1Idx >= 0) {
4127 unsigned CheckEncoding =
4128 RI->getEncodingValue(Inst.getOperand(VS1Idx).getReg());
4129 for (unsigned i = 0; i < Lmul; i++) {
4130 if ((DestEncoding + i) == CheckEncoding)
4131 return Error(Loc,
4132 "the destination vector register group cannot overlap"
4133 " the source vector register group");
4134 }
4135 }
4136 }
4137
4138 if (MCID.TSFlags & RISCVII::VMConstraint) {
4139 int VMIdx = RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vm);
4140 assert(VMIdx >= 0 && "No vm operand?");
4141
4142 if (DestReg == RISCV::V0) {
4143 if (MCID.operands()[Inst.getNumOperands() - 1].OperandType !=
4145 return Error(Loc, "the destination vector register group cannot be V0");
4146
4147 // Regardless masked or unmasked version, the number of operands is the
4148 // same. For example, "viota.m v0, v2" is "viota.m v0, v2, NoRegister"
4149 // actually. We need to check the operand to see whether it is masked or
4150 // not.
4151 MCRegister CheckReg = Inst.getOperand(VMIdx).getReg();
4152 assert((!CheckReg.isValid() || CheckReg == RISCV::V0) &&
4153 "Unexpected mask operand register");
4154 if (CheckReg.isValid())
4155 return Error(Loc, "the destination vector register group cannot overlap"
4156 " the mask register");
4157 }
4158 }
4159
4161 // smt.vmadot with sp and hp: the vmask operand (only use V0 or V1) must not
4162 // overlap with any of vd, vs1, or vs2.
4163 int VMaskIdx =
4164 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::vmask);
4165 MCRegister MaskReg = Inst.getOperand(VMaskIdx).getReg();
4166 if (MaskReg != RISCV::V0 && MaskReg != RISCV::V1)
4167 return Error(Operands[VMaskIdx]->getStartLoc(),
4168 "vmask operand only supports v0 or v1");
4169
4170 unsigned MaskEnc = RI->getEncodingValue(MaskReg);
4171 RISCV::OpName RegOps[] = {RISCV::OpName::vd, RISCV::OpName::vs1,
4172 RISCV::OpName::vs2};
4173 for (RISCV::OpName OpN : RegOps) {
4174 int Idx = RISCV::getNamedOperandIdx(Inst.getOpcode(), OpN);
4175 if (Idx < 0 || !Inst.getOperand(Idx).isReg())
4176 continue;
4177 MCRegister Reg = Inst.getOperand(Idx).getReg();
4178 unsigned RegEnc = RI->getEncodingValue(Reg);
4179 unsigned RegLmul = getLMULFromVectorRegister(Reg);
4180 for (unsigned i = 0; i < RegLmul; i++) {
4181 if ((RegEnc + i) == MaskEnc) {
4182 SMLoc Loc = Operands[Idx]->getStartLoc();
4183 return Error(Loc,
4184 Twine("register conflicts with vmask register ") +
4185 RISCVInstPrinter::getRegisterName(CLOpts, MaskReg));
4186 }
4187 }
4188 }
4189 }
4190
4191 return false;
4192}
4193
4194bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
4196 MCStreamer &Out) {
4197 Inst.setLoc(IDLoc);
4198
4199 switch (Inst.getOpcode()) {
4200 default:
4201 break;
4202 case RISCV::MOP_RR_7: {
4203 // Remap mop.rr.7 x0, x0, x1/x5 to sspush x1/x5.
4204 if (Inst.getOperand(0).getReg() == RISCV::X0 &&
4205 Inst.getOperand(1).getReg() == RISCV::X0 &&
4206 (Inst.getOperand(2).getReg() == RISCV::X1 ||
4207 Inst.getOperand(2).getReg() == RISCV::X5)) {
4208 emitToStreamer(
4209 Out, MCInstBuilder(RISCV::SSPUSH).addOperand(Inst.getOperand(2)));
4210 return false;
4211 }
4212 break;
4213 }
4214 case RISCV::MOP_R_28: {
4215 // Remap mop.r.28 x0, x1/x5 to sspopchk x1/x5.
4216 if (Inst.getOperand(0).getReg() == RISCV::X0 &&
4217 (Inst.getOperand(1).getReg() == RISCV::X1 ||
4218 Inst.getOperand(1).getReg() == RISCV::X5)) {
4219 emitToStreamer(
4220 Out, MCInstBuilder(RISCV::SSPOPCHK).addOperand(Inst.getOperand(1)));
4221 return false;
4222 }
4223 // Remap mop.r.28 rN, x0 to ssrdp rN.
4224 if (Inst.getOperand(0).getReg() != RISCV::X0 &&
4225 Inst.getOperand(1).getReg() == RISCV::X0) {
4226 emitToStreamer(
4227 Out, MCInstBuilder(RISCV::SSRDP).addOperand(Inst.getOperand(0)));
4228 return false;
4229 }
4230 break;
4231 }
4232 case RISCV::PseudoC_ADDI_NOP: {
4233 if (Inst.getOperand(2).getImm() == 0)
4234 emitToStreamer(Out, MCInstBuilder(RISCV::C_NOP));
4235 else
4236 emitToStreamer(
4237 Out, MCInstBuilder(RISCV::C_NOP_HINT).addOperand(Inst.getOperand(2)));
4238 return false;
4239 }
4240 case RISCV::PACK: {
4241 // Convert PACK wth RS2==X0 to ZEXT_H_RV32 to match disassembler output.
4242 if (Inst.getOperand(2).getReg() != RISCV::X0)
4243 break;
4244 if (getSTI().hasFeature(RISCV::Feature64Bit))
4245 break;
4246 emitToStreamer(Out, MCInstBuilder(RISCV::ZEXT_H_RV32)
4247 .addOperand(Inst.getOperand(0))
4248 .addOperand(Inst.getOperand(1)));
4249 return false;
4250 }
4251 case RISCV::PACKW: {
4252 // Convert PACKW with RS2==X0 to ZEXT_H_RV64 to match disassembler output.
4253 if (Inst.getOperand(2).getReg() != RISCV::X0)
4254 break;
4255 emitToStreamer(Out, MCInstBuilder(RISCV::ZEXT_H_RV64)
4256 .addOperand(Inst.getOperand(0))
4257 .addOperand(Inst.getOperand(1)));
4258 return false;
4259 }
4260 case RISCV::PseudoLLAImm:
4261 case RISCV::PseudoLAImm:
4262 case RISCV::PseudoLI: {
4263 MCRegister Reg = Inst.getOperand(0).getReg();
4264 const MCOperand &Op1 = Inst.getOperand(1);
4265 if (Op1.isExpr()) {
4266 // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
4267 // Just convert to an addi. This allows compatibility with gas.
4268 emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
4269 .addReg(Reg)
4270 .addReg(RISCV::X0)
4271 .addExpr(Op1.getExpr()));
4272 return false;
4273 }
4274 int64_t Imm = Inst.getOperand(1).getImm();
4275 // On RV32 the immediate here can either be a signed or an unsigned
4276 // 32-bit number. Sign extension has to be performed to ensure that Imm
4277 // represents the expected signed 64-bit number.
4278 if (!isRV64())
4280 emitLoadImm(Reg, Imm, Out);
4281 return false;
4282 }
4283 case RISCV::PseudoLLA:
4284 emitLoadLocalAddress(Inst, IDLoc, Out);
4285 return false;
4286 case RISCV::PseudoLGA:
4287 emitLoadGlobalAddress(Inst, IDLoc, Out);
4288 return false;
4289 case RISCV::PseudoLA:
4290 emitLoadAddress(Inst, IDLoc, Out);
4291 return false;
4292 case RISCV::PseudoLA_TLS_IE:
4293 emitLoadTLSIEAddress(Inst, IDLoc, Out);
4294 return false;
4295 case RISCV::PseudoLA_TLS_GD:
4296 emitLoadTLSGDAddress(Inst, IDLoc, Out);
4297 return false;
4298 case RISCV::PseudoLB:
4299 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
4300 return false;
4301 case RISCV::PseudoLBU:
4302 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
4303 return false;
4304 case RISCV::PseudoLH:
4305 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
4306 return false;
4307 case RISCV::PseudoLHU:
4308 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
4309 return false;
4310 case RISCV::PseudoLW:
4311 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
4312 return false;
4313 case RISCV::PseudoLWU:
4314 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
4315 return false;
4316 case RISCV::PseudoLD:
4317 emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
4318 return false;
4319 case RISCV::PseudoLD_RV32:
4320 emitLoadStoreSymbol(Inst, RISCV::LD_RV32, IDLoc, Out, /*HasTmpReg=*/false);
4321 return false;
4322 case RISCV::PseudoFLH:
4323 emitLoadStoreSymbol(Inst, RISCV::FLH, IDLoc, Out, /*HasTmpReg=*/true);
4324 return false;
4325 case RISCV::PseudoFLW:
4326 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
4327 return false;
4328 case RISCV::PseudoFLD:
4329 emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
4330 return false;
4331 case RISCV::PseudoFLQ:
4332 emitLoadStoreSymbol(Inst, RISCV::FLQ, IDLoc, Out, /*HasTmpReg=*/true);
4333 return false;
4334 case RISCV::PseudoSB:
4335 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
4336 return false;
4337 case RISCV::PseudoSH:
4338 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
4339 return false;
4340 case RISCV::PseudoSW:
4341 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
4342 return false;
4343 case RISCV::PseudoSD:
4344 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
4345 return false;
4346 case RISCV::PseudoSD_RV32:
4347 emitLoadStoreSymbol(Inst, RISCV::SD_RV32, IDLoc, Out, /*HasTmpReg=*/true);
4348 return false;
4349 case RISCV::PseudoQC_E_LB:
4350 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessLB, IDLoc, Out,
4351 /*HasTmpReg=*/false);
4352 return false;
4353 case RISCV::PseudoQC_E_LBU:
4354 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessLBU, IDLoc, Out,
4355 /*HasTmpReg=*/false);
4356 return false;
4357 case RISCV::PseudoQC_E_LH:
4358 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessLH, IDLoc, Out,
4359 /*HasTmpReg=*/false);
4360 return false;
4361 case RISCV::PseudoQC_E_LHU:
4362 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessLHU, IDLoc, Out,
4363 /*HasTmpReg=*/false);
4364 return false;
4365 case RISCV::PseudoQC_E_LW:
4366 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessLW, IDLoc, Out,
4367 /*HasTmpReg=*/false);
4368 return false;
4369 case RISCV::PseudoQC_E_SB:
4370 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessSB, IDLoc, Out,
4371 /*HasTmpReg=*/true);
4372 return false;
4373 case RISCV::PseudoQC_E_SH:
4374 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessSH, IDLoc, Out,
4375 /*HasTmpReg=*/true);
4376 return false;
4377 case RISCV::PseudoQC_E_SW:
4378 emitQCELILoadStoreSymbol(Inst, RISCV::PseudoQCAccessSW, IDLoc, Out,
4379 /*HasTmpReg=*/true);
4380 return false;
4381 case RISCV::PseudoFSH:
4382 emitLoadStoreSymbol(Inst, RISCV::FSH, IDLoc, Out, /*HasTmpReg=*/true);
4383 return false;
4384 case RISCV::PseudoFSW:
4385 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
4386 return false;
4387 case RISCV::PseudoFSD:
4388 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
4389 return false;
4390 case RISCV::PseudoFSQ:
4391 emitLoadStoreSymbol(Inst, RISCV::FSQ, IDLoc, Out, /*HasTmpReg=*/true);
4392 return false;
4393 case RISCV::PseudoAddTPRel:
4394 if (checkPseudoAddTPRel(Inst, Operands))
4395 return true;
4396 break;
4397 case RISCV::PseudoTLSDESCCall:
4398 if (checkPseudoTLSDESCCall(Inst, Operands))
4399 return true;
4400 break;
4401 case RISCV::PseudoSEXT_B:
4402 emitPseudoExtend(Inst, /*SignExtend=*/true, /*Width=*/8, IDLoc, Out);
4403 return false;
4404 case RISCV::PseudoSEXT_H:
4405 emitPseudoExtend(Inst, /*SignExtend=*/true, /*Width=*/16, IDLoc, Out);
4406 return false;
4407 case RISCV::PseudoZEXT_H:
4408 emitPseudoExtend(Inst, /*SignExtend=*/false, /*Width=*/16, IDLoc, Out);
4409 return false;
4410 case RISCV::PseudoZEXT_W:
4411 emitPseudoExtend(Inst, /*SignExtend=*/false, /*Width=*/32, IDLoc, Out);
4412 return false;
4413 case RISCV::PseudoVMSGEU_VX_M:
4414 case RISCV::PseudoVMSGEU_VX_M_T:
4415 emitVMSGE(Inst, RISCV::VMSLTU_VX, IDLoc, Out);
4416 return false;
4417 case RISCV::PseudoVMSGE_VX_M:
4418 case RISCV::PseudoVMSGE_VX_M_T:
4419 emitVMSGE(Inst, RISCV::VMSLT_VX, IDLoc, Out);
4420 return false;
4421 case RISCV::PseudoVMSGE_VI:
4422 case RISCV::PseudoVMSLT_VI: {
4423 // These instructions are signed and so is immediate so we can subtract one
4424 // and change the opcode.
4425 int64_t Imm = Inst.getOperand(2).getImm();
4426 unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGE_VI ? RISCV::VMSGT_VI
4427 : RISCV::VMSLE_VI;
4428 emitToStreamer(Out, MCInstBuilder(Opc)
4429 .addOperand(Inst.getOperand(0))
4430 .addOperand(Inst.getOperand(1))
4431 .addImm(Imm - 1)
4432 .addOperand(Inst.getOperand(3))
4433 .setLoc(IDLoc));
4434 return false;
4435 }
4436 case RISCV::PseudoVMSGEU_VI:
4437 case RISCV::PseudoVMSLTU_VI: {
4438 int64_t Imm = Inst.getOperand(2).getImm();
4439 // Unsigned comparisons are tricky because the immediate is signed. If the
4440 // immediate is 0 we can't just subtract one. vmsltu.vi v0, v1, 0 is always
4441 // false, but vmsle.vi v0, v1, -1 is always true. Instead we use
4442 // vmsne v0, v1, v1 which is always false.
4443 if (Imm == 0) {
4444 unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGEU_VI
4445 ? RISCV::VMSEQ_VV
4446 : RISCV::VMSNE_VV;
4447 emitToStreamer(Out, MCInstBuilder(Opc)
4448 .addOperand(Inst.getOperand(0))
4449 .addOperand(Inst.getOperand(1))
4450 .addOperand(Inst.getOperand(1))
4451 .addOperand(Inst.getOperand(3))
4452 .setLoc(IDLoc));
4453 } else {
4454 // Other immediate values can subtract one like signed.
4455 unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGEU_VI
4456 ? RISCV::VMSGTU_VI
4457 : RISCV::VMSLEU_VI;
4458 emitToStreamer(Out, MCInstBuilder(Opc)
4459 .addOperand(Inst.getOperand(0))
4460 .addOperand(Inst.getOperand(1))
4461 .addImm(Imm - 1)
4462 .addOperand(Inst.getOperand(3))
4463 .setLoc(IDLoc));
4464 }
4465
4466 return false;
4467 }
4468 case RISCV::PseudoCV_ELW:
4469 emitLoadStoreSymbol(Inst, RISCV::CV_ELW, IDLoc, Out, /*HasTmpReg=*/false);
4470 return false;
4471 }
4472
4473 emitToStreamer(Out, Inst);
4474 return false;
4475}
4476
4477extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
static MCRegister MatchRegisterName(StringRef Name)
static const char * getSubtargetFeatureName(uint64_t Val)
#define Fail
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void applyMnemonicAliases(StringRef &Mnemonic, const FeatureBitset &Features, unsigned VariantID)
uint64_t uint64_t
uint64_t Imm
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
bpf unwind BPF Unwind Lower
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool matchRegisterNameHelper(const MCSubtargetInfo &STI, MCRegister &Reg, StringRef Name)
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
const FeatureInfo AllFeatures[]
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
bool isValidInsnFormat(StringRef Format, const MCSubtargetInfo &STI)
static bool isZvvfmmScaleOpcode(unsigned Opcode)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmParser()
static unsigned getNFforLXSEG(unsigned Opcode)
unsigned getLMULFromVectorRegister(MCRegister Reg)
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file implements the SmallBitVector class.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
int64_t getIntVal() const
Definition MCAsmMacro.h:108
bool isNot(TokenKind K) const
Definition MCAsmMacro.h:76
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
StringRef getStringContents() const
Get the contents of a string token (without quotes).
Definition MCAsmMacro.h:83
bool is(TokenKind K) const
Definition MCAsmMacro.h:75
LLVM_ABI SMLoc getEndLoc() const
Definition AsmLexer.cpp:33
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Definition MCAsmMacro.h:92
Encoding
Size and signedness of expression operations' operands.
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
void printExpr(raw_ostream &, const MCExpr &) const
const MCTargetOptions & getTargetOptions() const
Definition MCAsmInfo.h:452
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
MCContext & getContext()
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents.
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
MCStreamer & getStreamer()
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:412
LLVM_ABI MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
ExprKind getKind() const
Definition MCExpr.h:85
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getNumOperands() const
Definition MCInst.h:212
void setLoc(SMLoc loc)
Definition MCInst.h:207
unsigned getOpcode() const
Definition MCInst.h:202
void addOperand(const MCOperand Op)
Definition MCInst.h:215
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
ArrayRef< MCOperandInfo > operands() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition MCInstrInfo.h:96
bool isPositionIndependent() const
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
virtual SMLoc getStartLoc() const =0
getStartLoc - Get the location of the first token of this operand.
virtual bool isReg() const =0
isReg - Is this a register operand?
virtual MCRegister getReg() const =0
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isValid() const
Definition MCRegister.h:84
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const FeatureBitset & getFeatureBits() const
const FeatureBitset & ToggleFeature(uint64_t FB)
Toggle a feature and return the re-computed feature bits.
ArrayRef< SubtargetFeatureKV > getAllProcessorFeatures() const
Return processor features.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
MCTargetAsmParser - Generic interface to target specific assembly parsers.
const MCSymbol * getAddSym() const
Definition MCValue.h:49
uint32_t getSpecifier() const
Definition MCValue.h:46
const MCSymbol * getSubSym() const
Definition MCValue.h:51
Ternary parse status returned by various parse* methods.
static constexpr StatusTy Failure
static constexpr StatusTy Success
static constexpr StatusTy NoMatch
static LLVM_ABI bool isSupportedExtensionFeature(StringRef Ext)
static LLVM_ABI std::string getTargetFeatureForExtension(StringRef Ext)
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true)
Parse RISC-V ISA info from arch string.
static const char * getRegisterName(const RISCVMCOptions &CLOpts, MCRegister Reg)
constexpr unsigned id() const
Definition Register.h:100
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
constexpr const char * getPointer() const
Definition SMLoc.h:33
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
reference emplace_back(ArgTypes &&... Args)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
char back() const
Get the last character in the string.
Definition StringRef.h:153
A switch()-like statement whose cases are string literals.
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
#define INT64_MIN
Definition DataTypes.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const AArch64MCOptions & getCLOpts(const MCTargetOptions &Options)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
uint16_t StackAdjustment(const RuntimeFunction &RF)
StackAdjustment - calculated stack adjustment in words.
Definition ARMWinEH.h:200
LLVM_ABI std::optional< unsigned > attrTypeFromString(StringRef tag, TagNameMap tagNameMap)
MCExpr const & getExpr(MCExpr const &Expr)
bool isRVE(ABI TargetABI)
Expected< ABI > computeTargetABI(const MCSubtargetInfo &STI, StringRef ABIName)
LLVM_ABI const TagNameMap & getRISCVAttributeTags()
static RoundingMode stringToRoundingMode(StringRef Str)
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(const MCSubtargetInfo &STI)
int getLoadFPImm(APFloat FPImm)
getLoadFPImm - Return a 5-bit binary encoding of the floating-point immediate value.
void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI, MCRegister DestReg, SmallVectorImpl< MCInst > &Insts)
bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
static VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt)
static bool isValidLMUL(unsigned LMUL, bool Fractional)
static bool isValidSEW(unsigned SEW)
LLVM_ABI void printVType(unsigned VType, raw_ostream &OS)
static bool isValidXSfmmVType(unsigned VTypeI)
LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic, bool AltFmt=false)
unsigned encodeRegList(MCRegister EndReg, bool IsRVE=false)
static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64)
void printRegList(unsigned RlistEncode, raw_ostream &OS)
const RISCVMCOptions & getCLOpts(const MCTargetOptions &Options)
Specifier parseSpecifierName(StringRef name)
void updateCZceFeatureImplications(MCSubtargetInfo &STI)
uint16_t Specifier
bool isValidYBNDSWImm(int64_t Imm)
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
static SMTVTypeMode stringToSMTVTypeMode(StringRef Str)
static bool isValidSMTVTypeMode(unsigned Mode)
@ Valid
The data is already valid.
LLVM_ABI TargetOptions getTargetOptions(const cl::LibraryOptionsContext &LibraryOptions, const llvm::Triple &TheTriple)
Common utility function tightly tied to the options listed here.
std::function< llvm::json::Value()> Lambda
Definition Mustache.h:84
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition Error.h:1129
@ Length
Definition DWP.cpp:577
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
static bool isMem(const MachineInstr &MI, unsigned Op)
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
testing::Matcher< const detail::ErrorHolder & > Failed()
Definition Error.h:198
Target & getTheRISCV32Target()
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
Target & getTheRISCV64beTarget()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2068
DWARFExpression::Operation Op
Target & getTheRISCV64Target()
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:199
Target & getTheRISCV32beTarget()
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...