1 //===-- RISCVAsmParser.cpp - Parse RISCV 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 
9 #include "MCTargetDesc/RISCVAsmBackend.h"
10 #include "MCTargetDesc/RISCVBaseInfo.h"
11 #include "MCTargetDesc/RISCVInstPrinter.h"
12 #include "MCTargetDesc/RISCVMCExpr.h"
13 #include "MCTargetDesc/RISCVMCTargetDesc.h"
14 #include "MCTargetDesc/RISCVMatInt.h"
15 #include "MCTargetDesc/RISCVTargetStreamer.h"
16 #include "TargetInfo/RISCVTargetInfo.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallBitVector.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstBuilder.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCObjectFileInfo.h"
29 #include "llvm/MC/MCParser/MCAsmLexer.h"
30 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
31 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
32 #include "llvm/MC/MCRegisterInfo.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSubtargetInfo.h"
35 #include "llvm/MC/MCValue.h"
36 #include "llvm/MC/TargetRegistry.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/RISCVAttributes.h"
40 #include "llvm/Support/RISCVISAInfo.h"
41 
42 #include <limits>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "riscv-asm-parser"
47 
48 // Include the auto-generated portion of the compress emitter.
49 #define GEN_COMPRESS_INSTR
50 #include "RISCVGenCompressInstEmitter.inc"
51 
52 STATISTIC(RISCVNumInstrsCompressed,
53           "Number of RISC-V Compressed instructions emitted");
54 
55 namespace llvm {
56 extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
57 } // namespace llvm
58 
59 namespace {
60 struct RISCVOperand;
61 
62 struct ParserOptionsSet {
63   bool IsPicEnabled;
64 };
65 
66 class RISCVAsmParser : public MCTargetAsmParser {
67   SmallVector<FeatureBitset, 4> FeatureBitStack;
68 
69   SmallVector<ParserOptionsSet, 4> ParserOptionsStack;
70   ParserOptionsSet ParserOptions;
71 
72   SMLoc getLoc() const { return getParser().getTok().getLoc(); }
73   bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
74   bool isRV32E() const { return getSTI().hasFeature(RISCV::FeatureRV32E); }
75 
76   RISCVTargetStreamer &getTargetStreamer() {
77     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
78     return static_cast<RISCVTargetStreamer &>(TS);
79   }
80 
81   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
82                                       unsigned Kind) override;
83 
84   bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
85                                   int64_t Lower, int64_t Upper, Twine Msg);
86 
87   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
88                                OperandVector &Operands, MCStreamer &Out,
89                                uint64_t &ErrorInfo,
90                                bool MatchingInlineAsm) override;
91 
92   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
93   OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc,
94                                         SMLoc &EndLoc) override;
95 
96   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
97                         SMLoc NameLoc, OperandVector &Operands) override;
98 
99   bool ParseDirective(AsmToken DirectiveID) override;
100 
101   // Helper to actually emit an instruction to the MCStreamer. Also, when
102   // possible, compression of the instruction is performed.
103   void emitToStreamer(MCStreamer &S, const MCInst &Inst);
104 
105   // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that
106   // synthesize the desired immedate value into the destination register.
107   void emitLoadImm(MCRegister DestReg, int64_t Value, MCStreamer &Out);
108 
109   // Helper to emit a combination of AUIPC and SecondOpcode. Used to implement
110   // helpers such as emitLoadLocalAddress and emitLoadAddress.
111   void emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
112                          const MCExpr *Symbol, RISCVMCExpr::VariantKind VKHi,
113                          unsigned SecondOpcode, SMLoc IDLoc, MCStreamer &Out);
114 
115   // Helper to emit pseudo instruction "lla" used in PC-rel addressing.
116   void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
117 
118   // Helper to emit pseudo instruction "la" used in GOT/PC-rel addressing.
119   void emitLoadAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
120 
121   // Helper to emit pseudo instruction "la.tls.ie" used in initial-exec TLS
122   // addressing.
123   void emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
124 
125   // Helper to emit pseudo instruction "la.tls.gd" used in global-dynamic TLS
126   // addressing.
127   void emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
128 
129   // Helper to emit pseudo load/store instruction with a symbol.
130   void emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
131                            MCStreamer &Out, bool HasTmpReg);
132 
133   // Helper to emit pseudo sign/zero extend instruction.
134   void emitPseudoExtend(MCInst &Inst, bool SignExtend, int64_t Width,
135                         SMLoc IDLoc, MCStreamer &Out);
136 
137   // Helper to emit pseudo vmsge{u}.vx instruction.
138   void emitVMSGE(MCInst &Inst, unsigned Opcode, SMLoc IDLoc, MCStreamer &Out);
139 
140   // Checks that a PseudoAddTPRel is using x4/tp in its second input operand.
141   // Enforcing this using a restricted register class for the second input
142   // operand of PseudoAddTPRel results in a poor diagnostic due to the fact
143   // 'add' is an overloaded mnemonic.
144   bool checkPseudoAddTPRel(MCInst &Inst, OperandVector &Operands);
145 
146   // Check instruction constraints.
147   bool validateInstruction(MCInst &Inst, OperandVector &Operands);
148 
149   /// Helper for processing MC instructions that have been successfully matched
150   /// by MatchAndEmitInstruction. Modifications to the emitted instructions,
151   /// like the expansion of pseudo instructions (e.g., "li"), can be performed
152   /// in this method.
153   bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
154                           MCStreamer &Out);
155 
156 // Auto-generated instruction matching functions
157 #define GET_ASSEMBLER_HEADER
158 #include "RISCVGenAsmMatcher.inc"
159 
160   OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands);
161   OperandMatchResultTy parseImmediate(OperandVector &Operands);
162   OperandMatchResultTy parseRegister(OperandVector &Operands,
163                                      bool AllowParens = false);
164   OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands);
165   OperandMatchResultTy parseZeroOffsetMemOp(OperandVector &Operands);
166   OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands);
167   OperandMatchResultTy parseBareSymbol(OperandVector &Operands);
168   OperandMatchResultTy parseCallSymbol(OperandVector &Operands);
169   OperandMatchResultTy parsePseudoJumpSymbol(OperandVector &Operands);
170   OperandMatchResultTy parseJALOffset(OperandVector &Operands);
171   OperandMatchResultTy parseVTypeI(OperandVector &Operands);
172   OperandMatchResultTy parseMaskReg(OperandVector &Operands);
173   OperandMatchResultTy parseInsnDirectiveOpcode(OperandVector &Operands);
174   OperandMatchResultTy parseGPRAsFPR(OperandVector &Operands);
175 
176   bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
177 
178   bool parseDirectiveOption();
179   bool parseDirectiveAttribute();
180   bool parseDirectiveInsn(SMLoc L);
181 
182   void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
183     if (!(getSTI().getFeatureBits()[Feature])) {
184       MCSubtargetInfo &STI = copySTI();
185       setAvailableFeatures(
186           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
187     }
188   }
189 
190   bool getFeatureBits(uint64_t Feature) {
191     return getSTI().getFeatureBits()[Feature];
192   }
193 
194   void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
195     if (getSTI().getFeatureBits()[Feature]) {
196       MCSubtargetInfo &STI = copySTI();
197       setAvailableFeatures(
198           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
199     }
200   }
201 
202   void pushFeatureBits() {
203     assert(FeatureBitStack.size() == ParserOptionsStack.size() &&
204            "These two stacks must be kept synchronized");
205     FeatureBitStack.push_back(getSTI().getFeatureBits());
206     ParserOptionsStack.push_back(ParserOptions);
207   }
208 
209   bool popFeatureBits() {
210     assert(FeatureBitStack.size() == ParserOptionsStack.size() &&
211            "These two stacks must be kept synchronized");
212     if (FeatureBitStack.empty())
213       return true;
214 
215     FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
216     copySTI().setFeatureBits(FeatureBits);
217     setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
218 
219     ParserOptions = ParserOptionsStack.pop_back_val();
220 
221     return false;
222   }
223 
224   std::unique_ptr<RISCVOperand> defaultMaskRegOp() const;
225 
226 public:
227   enum RISCVMatchResultTy {
228     Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
229 #define GET_OPERAND_DIAGNOSTIC_TYPES
230 #include "RISCVGenAsmMatcher.inc"
231 #undef GET_OPERAND_DIAGNOSTIC_TYPES
232   };
233 
234   static bool classifySymbolRef(const MCExpr *Expr,
235                                 RISCVMCExpr::VariantKind &Kind);
236 
237   RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
238                  const MCInstrInfo &MII, const MCTargetOptions &Options)
239       : MCTargetAsmParser(Options, STI, MII) {
240     Parser.addAliasForDirective(".half", ".2byte");
241     Parser.addAliasForDirective(".hword", ".2byte");
242     Parser.addAliasForDirective(".word", ".4byte");
243     Parser.addAliasForDirective(".dword", ".8byte");
244     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
245 
246     auto ABIName = StringRef(Options.ABIName);
247     if (ABIName.endswith("f") &&
248         !getSTI().getFeatureBits()[RISCV::FeatureStdExtF]) {
249       errs() << "Hard-float 'f' ABI can't be used for a target that "
250                 "doesn't support the F instruction set extension (ignoring "
251                 "target-abi)\n";
252     } else if (ABIName.endswith("d") &&
253                !getSTI().getFeatureBits()[RISCV::FeatureStdExtD]) {
254       errs() << "Hard-float 'd' ABI can't be used for a target that "
255                 "doesn't support the D instruction set extension (ignoring "
256                 "target-abi)\n";
257     }
258 
259     // Use computeTargetABI to check if ABIName is valid. If invalid, output
260     // error message.
261     RISCVABI::computeTargetABI(STI.getTargetTriple(), STI.getFeatureBits(),
262                                ABIName);
263 
264     const MCObjectFileInfo *MOFI = Parser.getContext().getObjectFileInfo();
265     ParserOptions.IsPicEnabled = MOFI->isPositionIndependent();
266   }
267 };
268 
269 /// RISCVOperand - Instances of this class represent a parsed machine
270 /// instruction
271 struct RISCVOperand : public MCParsedAsmOperand {
272 
273   enum class KindTy {
274     Token,
275     Register,
276     Immediate,
277     SystemRegister,
278     VType,
279   } Kind;
280 
281   bool IsRV64;
282 
283   bool IsGPRAsFPR;
284 
285   struct RegOp {
286     MCRegister RegNum;
287   };
288 
289   struct ImmOp {
290     const MCExpr *Val;
291   };
292 
293   struct SysRegOp {
294     const char *Data;
295     unsigned Length;
296     unsigned Encoding;
297     // FIXME: Add the Encoding parsed fields as needed for checks,
298     // e.g.: read/write or user/supervisor/machine privileges.
299   };
300 
301   struct VTypeOp {
302     unsigned Val;
303   };
304 
305   SMLoc StartLoc, EndLoc;
306   union {
307     StringRef Tok;
308     RegOp Reg;
309     ImmOp Imm;
310     struct SysRegOp SysReg;
311     struct VTypeOp VType;
312   };
313 
314   RISCVOperand(KindTy K) : Kind(K) {}
315 
316 public:
317   RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() {
318     Kind = o.Kind;
319     IsRV64 = o.IsRV64;
320     StartLoc = o.StartLoc;
321     EndLoc = o.EndLoc;
322     switch (Kind) {
323     case KindTy::Register:
324       Reg = o.Reg;
325       break;
326     case KindTy::Immediate:
327       Imm = o.Imm;
328       break;
329     case KindTy::Token:
330       Tok = o.Tok;
331       break;
332     case KindTy::SystemRegister:
333       SysReg = o.SysReg;
334       break;
335     case KindTy::VType:
336       VType = o.VType;
337       break;
338     }
339   }
340 
341   bool isToken() const override { return Kind == KindTy::Token; }
342   bool isReg() const override { return Kind == KindTy::Register; }
343   bool isV0Reg() const {
344     return Kind == KindTy::Register && Reg.RegNum == RISCV::V0;
345   }
346   bool isImm() const override { return Kind == KindTy::Immediate; }
347   bool isMem() const override { return false; }
348   bool isSystemRegister() const { return Kind == KindTy::SystemRegister; }
349 
350   bool isGPR() const {
351     return Kind == KindTy::Register &&
352            RISCVMCRegisterClasses[RISCV::GPRRegClassID].contains(Reg.RegNum);
353   }
354 
355   bool isGPRAsFPR() const { return isGPR() && IsGPRAsFPR; }
356 
357   bool isGPRF64AsFPR() const { return isGPR() && IsGPRAsFPR && IsRV64; }
358 
359   bool isGPRPF64AsFPR() const {
360     return isGPR() && IsGPRAsFPR && !IsRV64 && !((Reg.RegNum - RISCV::X0) & 1);
361   }
362 
363   static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
364                                   RISCVMCExpr::VariantKind &VK) {
365     if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
366       VK = RE->getKind();
367       return RE->evaluateAsConstant(Imm);
368     }
369 
370     if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
371       VK = RISCVMCExpr::VK_RISCV_None;
372       Imm = CE->getValue();
373       return true;
374     }
375 
376     return false;
377   }
378 
379   // True if operand is a symbol with no modifiers, or a constant with no
380   // modifiers and isShiftedInt<N-1, 1>(Op).
381   template <int N> bool isBareSimmNLsb0() const {
382     int64_t Imm;
383     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
384     if (!isImm())
385       return false;
386     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
387     bool IsValid;
388     if (!IsConstantImm)
389       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
390     else
391       IsValid = isShiftedInt<N - 1, 1>(Imm);
392     return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
393   }
394 
395   // Predicate methods for AsmOperands defined in RISCVInstrInfo.td
396 
397   bool isBareSymbol() const {
398     int64_t Imm;
399     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
400     // Must be of 'immediate' type but not a constant.
401     if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
402       return false;
403     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
404            VK == RISCVMCExpr::VK_RISCV_None;
405   }
406 
407   bool isCallSymbol() const {
408     int64_t Imm;
409     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
410     // Must be of 'immediate' type but not a constant.
411     if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
412       return false;
413     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
414            (VK == RISCVMCExpr::VK_RISCV_CALL ||
415             VK == RISCVMCExpr::VK_RISCV_CALL_PLT);
416   }
417 
418   bool isPseudoJumpSymbol() const {
419     int64_t Imm;
420     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
421     // Must be of 'immediate' type but not a constant.
422     if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
423       return false;
424     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
425            VK == RISCVMCExpr::VK_RISCV_CALL;
426   }
427 
428   bool isTPRelAddSymbol() const {
429     int64_t Imm;
430     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
431     // Must be of 'immediate' type but not a constant.
432     if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
433       return false;
434     return RISCVAsmParser::classifySymbolRef(getImm(), VK) &&
435            VK == RISCVMCExpr::VK_RISCV_TPREL_ADD;
436   }
437 
438   bool isCSRSystemRegister() const { return isSystemRegister(); }
439 
440   bool isVTypeImm(unsigned N) const {
441     int64_t Imm;
442     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
443     if (!isImm())
444       return false;
445     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
446     return IsConstantImm && isUIntN(N, Imm) && VK == RISCVMCExpr::VK_RISCV_None;
447   }
448 
449   // If the last operand of the vsetvli/vsetvli instruction is a constant
450   // expression, KindTy is Immediate.
451   bool isVTypeI10() const {
452     if (Kind == KindTy::Immediate)
453       return isVTypeImm(10);
454     return Kind == KindTy::VType;
455   }
456   bool isVTypeI11() const {
457     if (Kind == KindTy::Immediate)
458       return isVTypeImm(11);
459     return Kind == KindTy::VType;
460   }
461 
462   /// Return true if the operand is a valid for the fence instruction e.g.
463   /// ('iorw').
464   bool isFenceArg() const {
465     if (!isImm())
466       return false;
467 
468     int64_t Imm;
469     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
470     if (evaluateConstantImm(getImm(), Imm, VK)) {
471       // Only accept 0 as a constant immediate.
472       return VK == RISCVMCExpr::VK_RISCV_None && Imm == 0;
473     }
474 
475     auto *SVal = dyn_cast<MCSymbolRefExpr>(getImm());
476 
477     if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
478       return false;
479 
480     StringRef Str = SVal->getSymbol().getName();
481     // Letters must be unique, taken from 'iorw', and in ascending order. This
482     // holds as long as each individual character is one of 'iorw' and is
483     // greater than the previous character.
484     char Prev = '\0';
485     for (char c : Str) {
486       if (c != 'i' && c != 'o' && c != 'r' && c != 'w')
487         return false;
488       if (c <= Prev)
489         return false;
490       Prev = c;
491     }
492     return true;
493   }
494 
495   /// Return true if the operand is a valid floating point rounding mode.
496   bool isFRMArg() const {
497     if (!isImm())
498       return false;
499     const MCExpr *Val = getImm();
500     auto *SVal = dyn_cast<MCSymbolRefExpr>(Val);
501     if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None)
502       return false;
503 
504     StringRef Str = SVal->getSymbol().getName();
505 
506     return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid;
507   }
508 
509   bool isImmXLenLI() const {
510     int64_t Imm;
511     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
512     if (!isImm())
513       return false;
514     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
515     if (VK == RISCVMCExpr::VK_RISCV_LO || VK == RISCVMCExpr::VK_RISCV_PCREL_LO)
516       return true;
517     // Given only Imm, ensuring that the actually specified constant is either
518     // a signed or unsigned 64-bit number is unfortunately impossible.
519     return IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None &&
520            (isRV64() || (isInt<32>(Imm) || isUInt<32>(Imm)));
521   }
522 
523   bool isUImmLog2XLen() const {
524     int64_t Imm;
525     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
526     if (!isImm())
527       return false;
528     if (!evaluateConstantImm(getImm(), Imm, VK) ||
529         VK != RISCVMCExpr::VK_RISCV_None)
530       return false;
531     return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
532   }
533 
534   bool isUImmLog2XLenNonZero() const {
535     int64_t Imm;
536     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
537     if (!isImm())
538       return false;
539     if (!evaluateConstantImm(getImm(), Imm, VK) ||
540         VK != RISCVMCExpr::VK_RISCV_None)
541       return false;
542     if (Imm == 0)
543       return false;
544     return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
545   }
546 
547   bool isUImmLog2XLenHalf() const {
548     int64_t Imm;
549     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
550     if (!isImm())
551       return false;
552     if (!evaluateConstantImm(getImm(), Imm, VK) ||
553         VK != RISCVMCExpr::VK_RISCV_None)
554       return false;
555     return (isRV64() && isUInt<5>(Imm)) || isUInt<4>(Imm);
556   }
557 
558   bool isUImm2() const {
559     int64_t Imm;
560     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
561     if (!isImm())
562       return false;
563     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
564     return IsConstantImm && isUInt<2>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
565   }
566 
567   bool isUImm3() const {
568     int64_t Imm;
569     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
570     if (!isImm())
571       return false;
572     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
573     return IsConstantImm && isUInt<3>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
574   }
575 
576   bool isUImm5() const {
577     int64_t Imm;
578     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
579     if (!isImm())
580       return false;
581     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
582     return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
583   }
584 
585   bool isUImm7() const {
586     int64_t Imm;
587     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
588     if (!isImm())
589       return false;
590     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
591     return IsConstantImm && isUInt<7>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
592   }
593 
594   bool isRnumArg() const {
595     int64_t Imm;
596     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
597     if (!isImm())
598       return false;
599     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
600     return IsConstantImm && Imm >= INT64_C(0) && Imm <= INT64_C(10) &&
601            VK == RISCVMCExpr::VK_RISCV_None;
602   }
603 
604   bool isSImm5() const {
605     if (!isImm())
606       return false;
607     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
608     int64_t Imm;
609     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
610     return IsConstantImm && isInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
611   }
612 
613   bool isSImm6() const {
614     if (!isImm())
615       return false;
616     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
617     int64_t Imm;
618     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
619     return IsConstantImm && isInt<6>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
620   }
621 
622   bool isSImm6NonZero() const {
623     if (!isImm())
624       return false;
625     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
626     int64_t Imm;
627     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
628     return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
629            VK == RISCVMCExpr::VK_RISCV_None;
630   }
631 
632   bool isCLUIImm() const {
633     if (!isImm())
634       return false;
635     int64_t Imm;
636     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
637     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
638     return IsConstantImm && (Imm != 0) &&
639            (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
640            VK == RISCVMCExpr::VK_RISCV_None;
641   }
642 
643   bool isUImm7Lsb00() const {
644     if (!isImm())
645       return false;
646     int64_t Imm;
647     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
648     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
649     return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
650            VK == RISCVMCExpr::VK_RISCV_None;
651   }
652 
653   bool isUImm8Lsb00() const {
654     if (!isImm())
655       return false;
656     int64_t Imm;
657     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
658     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
659     return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
660            VK == RISCVMCExpr::VK_RISCV_None;
661   }
662 
663   bool isUImm8Lsb000() const {
664     if (!isImm())
665       return false;
666     int64_t Imm;
667     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
668     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
669     return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
670            VK == RISCVMCExpr::VK_RISCV_None;
671   }
672 
673   bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
674 
675   bool isUImm9Lsb000() const {
676     if (!isImm())
677       return false;
678     int64_t Imm;
679     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
680     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
681     return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
682            VK == RISCVMCExpr::VK_RISCV_None;
683   }
684 
685   bool isUImm10Lsb00NonZero() const {
686     if (!isImm())
687       return false;
688     int64_t Imm;
689     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
690     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
691     return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
692            VK == RISCVMCExpr::VK_RISCV_None;
693   }
694 
695   bool isSImm12() const {
696     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
697     int64_t Imm;
698     bool IsValid;
699     if (!isImm())
700       return false;
701     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
702     if (!IsConstantImm)
703       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
704     else
705       IsValid = isInt<12>(Imm);
706     return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) ||
707                        VK == RISCVMCExpr::VK_RISCV_LO ||
708                        VK == RISCVMCExpr::VK_RISCV_PCREL_LO ||
709                        VK == RISCVMCExpr::VK_RISCV_TPREL_LO);
710   }
711 
712   bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); }
713 
714   bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
715 
716   bool isSImm10Lsb0000NonZero() const {
717     if (!isImm())
718       return false;
719     int64_t Imm;
720     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
721     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
722     return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
723            VK == RISCVMCExpr::VK_RISCV_None;
724   }
725 
726   bool isUImm20LUI() const {
727     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
728     int64_t Imm;
729     bool IsValid;
730     if (!isImm())
731       return false;
732     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
733     if (!IsConstantImm) {
734       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
735       return IsValid && (VK == RISCVMCExpr::VK_RISCV_HI ||
736                          VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
737     } else {
738       return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
739                                  VK == RISCVMCExpr::VK_RISCV_HI ||
740                                  VK == RISCVMCExpr::VK_RISCV_TPREL_HI);
741     }
742   }
743 
744   bool isUImm20AUIPC() const {
745     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
746     int64_t Imm;
747     bool IsValid;
748     if (!isImm())
749       return false;
750     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
751     if (!IsConstantImm) {
752       IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
753       return IsValid && (VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
754                          VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
755                          VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
756                          VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
757     } else {
758       return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None ||
759                                  VK == RISCVMCExpr::VK_RISCV_PCREL_HI ||
760                                  VK == RISCVMCExpr::VK_RISCV_GOT_HI ||
761                                  VK == RISCVMCExpr::VK_RISCV_TLS_GOT_HI ||
762                                  VK == RISCVMCExpr::VK_RISCV_TLS_GD_HI);
763     }
764   }
765 
766   bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); }
767 
768   bool isImmZero() const {
769     if (!isImm())
770       return false;
771     int64_t Imm;
772     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
773     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
774     return IsConstantImm && (Imm == 0) && VK == RISCVMCExpr::VK_RISCV_None;
775   }
776 
777   bool isSImm5Plus1() const {
778     if (!isImm())
779       return false;
780     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
781     int64_t Imm;
782     bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
783     return IsConstantImm && isInt<5>(Imm - 1) &&
784            VK == RISCVMCExpr::VK_RISCV_None;
785   }
786 
787   /// getStartLoc - Gets location of the first token of this operand
788   SMLoc getStartLoc() const override { return StartLoc; }
789   /// getEndLoc - Gets location of the last token of this operand
790   SMLoc getEndLoc() const override { return EndLoc; }
791   /// True if this operand is for an RV64 instruction
792   bool isRV64() const { return IsRV64; }
793 
794   unsigned getReg() const override {
795     assert(Kind == KindTy::Register && "Invalid type access!");
796     return Reg.RegNum.id();
797   }
798 
799   StringRef getSysReg() const {
800     assert(Kind == KindTy::SystemRegister && "Invalid type access!");
801     return StringRef(SysReg.Data, SysReg.Length);
802   }
803 
804   const MCExpr *getImm() const {
805     assert(Kind == KindTy::Immediate && "Invalid type access!");
806     return Imm.Val;
807   }
808 
809   StringRef getToken() const {
810     assert(Kind == KindTy::Token && "Invalid type access!");
811     return Tok;
812   }
813 
814   unsigned getVType() const {
815     assert(Kind == KindTy::VType && "Invalid type access!");
816     return VType.Val;
817   }
818 
819   void print(raw_ostream &OS) const override {
820     auto RegName = [](unsigned Reg) {
821       if (Reg)
822         return RISCVInstPrinter::getRegisterName(Reg);
823       else
824         return "noreg";
825     };
826 
827     switch (Kind) {
828     case KindTy::Immediate:
829       OS << *getImm();
830       break;
831     case KindTy::Register:
832       OS << "<register " << RegName(getReg()) << ">";
833       break;
834     case KindTy::Token:
835       OS << "'" << getToken() << "'";
836       break;
837     case KindTy::SystemRegister:
838       OS << "<sysreg: " << getSysReg() << '>';
839       break;
840     case KindTy::VType:
841       OS << "<vtype: ";
842       RISCVVType::printVType(getVType(), OS);
843       OS << '>';
844       break;
845     }
846   }
847 
848   static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S,
849                                                    bool IsRV64) {
850     auto Op = std::make_unique<RISCVOperand>(KindTy::Token);
851     Op->Tok = Str;
852     Op->StartLoc = S;
853     Op->EndLoc = S;
854     Op->IsRV64 = IsRV64;
855     return Op;
856   }
857 
858   static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S,
859                                                  SMLoc E, bool IsRV64,
860                                                  bool IsGPRAsFPR = false) {
861     auto Op = std::make_unique<RISCVOperand>(KindTy::Register);
862     Op->Reg.RegNum = RegNo;
863     Op->StartLoc = S;
864     Op->EndLoc = E;
865     Op->IsRV64 = IsRV64;
866     Op->IsGPRAsFPR = IsGPRAsFPR;
867     return Op;
868   }
869 
870   static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S,
871                                                  SMLoc E, bool IsRV64) {
872     auto Op = std::make_unique<RISCVOperand>(KindTy::Immediate);
873     Op->Imm.Val = Val;
874     Op->StartLoc = S;
875     Op->EndLoc = E;
876     Op->IsRV64 = IsRV64;
877     return Op;
878   }
879 
880   static std::unique_ptr<RISCVOperand>
881   createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) {
882     auto Op = std::make_unique<RISCVOperand>(KindTy::SystemRegister);
883     Op->SysReg.Data = Str.data();
884     Op->SysReg.Length = Str.size();
885     Op->SysReg.Encoding = Encoding;
886     Op->StartLoc = S;
887     Op->EndLoc = S;
888     Op->IsRV64 = IsRV64;
889     return Op;
890   }
891 
892   static std::unique_ptr<RISCVOperand> createVType(unsigned VTypeI, SMLoc S,
893                                                    bool IsRV64) {
894     auto Op = std::make_unique<RISCVOperand>(KindTy::VType);
895     Op->VType.Val = VTypeI;
896     Op->StartLoc = S;
897     Op->EndLoc = S;
898     Op->IsRV64 = IsRV64;
899     return Op;
900   }
901 
902   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
903     assert(Expr && "Expr shouldn't be null!");
904     int64_t Imm = 0;
905     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
906     bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
907 
908     if (IsConstant)
909       Inst.addOperand(MCOperand::createImm(Imm));
910     else
911       Inst.addOperand(MCOperand::createExpr(Expr));
912   }
913 
914   // Used by the TableGen Code
915   void addRegOperands(MCInst &Inst, unsigned N) const {
916     assert(N == 1 && "Invalid number of operands!");
917     Inst.addOperand(MCOperand::createReg(getReg()));
918   }
919 
920   void addImmOperands(MCInst &Inst, unsigned N) const {
921     assert(N == 1 && "Invalid number of operands!");
922     addExpr(Inst, getImm());
923   }
924 
925   void addFenceArgOperands(MCInst &Inst, unsigned N) const {
926     assert(N == 1 && "Invalid number of operands!");
927 
928     int64_t Constant = 0;
929     RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
930     if (evaluateConstantImm(getImm(), Constant, VK)) {
931       if (Constant == 0) {
932         Inst.addOperand(MCOperand::createImm(Constant));
933         return;
934       }
935       llvm_unreachable("FenceArg must contain only [iorw] or be 0");
936     }
937 
938     // isFenceArg has validated the operand, meaning this cast is safe
939     auto SE = cast<MCSymbolRefExpr>(getImm());
940 
941     unsigned Imm = 0;
942     for (char c : SE->getSymbol().getName()) {
943       switch (c) {
944       default:
945         llvm_unreachable("FenceArg must contain only [iorw] or be 0");
946       case 'i':
947         Imm |= RISCVFenceField::I;
948         break;
949       case 'o':
950         Imm |= RISCVFenceField::O;
951         break;
952       case 'r':
953         Imm |= RISCVFenceField::R;
954         break;
955       case 'w':
956         Imm |= RISCVFenceField::W;
957         break;
958       }
959     }
960     Inst.addOperand(MCOperand::createImm(Imm));
961   }
962 
963   void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
964     assert(N == 1 && "Invalid number of operands!");
965     Inst.addOperand(MCOperand::createImm(SysReg.Encoding));
966   }
967 
968   // Support non-canonical syntax:
969   // "vsetivli rd, uimm, 0xabc" or "vsetvli rd, rs1, 0xabc"
970   // "vsetivli rd, uimm, (0xc << N)" or "vsetvli rd, rs1, (0xc << N)"
971   void addVTypeIOperands(MCInst &Inst, unsigned N) const {
972     assert(N == 1 && "Invalid number of operands!");
973     int64_t Imm = 0;
974     if (Kind == KindTy::Immediate) {
975       RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
976       bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
977       (void)IsConstantImm;
978       assert(IsConstantImm && "Invalid VTypeI Operand!");
979     } else {
980       Imm = getVType();
981     }
982     Inst.addOperand(MCOperand::createImm(Imm));
983   }
984 
985   // Returns the rounding mode represented by this RISCVOperand. Should only
986   // be called after checking isFRMArg.
987   RISCVFPRndMode::RoundingMode getRoundingMode() const {
988     // isFRMArg has validated the operand, meaning this cast is safe.
989     auto SE = cast<MCSymbolRefExpr>(getImm());
990     RISCVFPRndMode::RoundingMode FRM =
991         RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName());
992     assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode");
993     return FRM;
994   }
995 
996   void addFRMArgOperands(MCInst &Inst, unsigned N) const {
997     assert(N == 1 && "Invalid number of operands!");
998     Inst.addOperand(MCOperand::createImm(getRoundingMode()));
999   }
1000 };
1001 } // end anonymous namespace.
1002 
1003 #define GET_REGISTER_MATCHER
1004 #define GET_SUBTARGET_FEATURE_NAME
1005 #define GET_MATCHER_IMPLEMENTATION
1006 #define GET_MNEMONIC_SPELL_CHECKER
1007 #include "RISCVGenAsmMatcher.inc"
1008 
1009 static MCRegister convertFPR64ToFPR16(MCRegister Reg) {
1010   assert(Reg >= RISCV::F0_D && Reg <= RISCV::F31_D && "Invalid register");
1011   return Reg - RISCV::F0_D + RISCV::F0_H;
1012 }
1013 
1014 static MCRegister convertFPR64ToFPR32(MCRegister Reg) {
1015   assert(Reg >= RISCV::F0_D && Reg <= RISCV::F31_D && "Invalid register");
1016   return Reg - RISCV::F0_D + RISCV::F0_F;
1017 }
1018 
1019 static MCRegister convertVRToVRMx(const MCRegisterInfo &RI, MCRegister Reg,
1020                                   unsigned Kind) {
1021   unsigned RegClassID;
1022   if (Kind == MCK_VRM2)
1023     RegClassID = RISCV::VRM2RegClassID;
1024   else if (Kind == MCK_VRM4)
1025     RegClassID = RISCV::VRM4RegClassID;
1026   else if (Kind == MCK_VRM8)
1027     RegClassID = RISCV::VRM8RegClassID;
1028   else
1029     return 0;
1030   return RI.getMatchingSuperReg(Reg, RISCV::sub_vrm1_0,
1031                                 &RISCVMCRegisterClasses[RegClassID]);
1032 }
1033 
1034 unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1035                                                     unsigned Kind) {
1036   RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp);
1037   if (!Op.isReg())
1038     return Match_InvalidOperand;
1039 
1040   MCRegister Reg = Op.getReg();
1041   bool IsRegFPR64 =
1042       RISCVMCRegisterClasses[RISCV::FPR64RegClassID].contains(Reg);
1043   bool IsRegFPR64C =
1044       RISCVMCRegisterClasses[RISCV::FPR64CRegClassID].contains(Reg);
1045   bool IsRegVR = RISCVMCRegisterClasses[RISCV::VRRegClassID].contains(Reg);
1046 
1047   // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the
1048   // register from FPR64 to FPR32 or FPR64C to FPR32C if necessary.
1049   if ((IsRegFPR64 && Kind == MCK_FPR32) ||
1050       (IsRegFPR64C && Kind == MCK_FPR32C)) {
1051     Op.Reg.RegNum = convertFPR64ToFPR32(Reg);
1052     return Match_Success;
1053   }
1054   // As the parser couldn't differentiate an FPR16 from an FPR64, coerce the
1055   // register from FPR64 to FPR16 if necessary.
1056   if (IsRegFPR64 && Kind == MCK_FPR16) {
1057     Op.Reg.RegNum = convertFPR64ToFPR16(Reg);
1058     return Match_Success;
1059   }
1060   // As the parser couldn't differentiate an VRM2/VRM4/VRM8 from an VR, coerce
1061   // the register from VR to VRM2/VRM4/VRM8 if necessary.
1062   if (IsRegVR && (Kind == MCK_VRM2 || Kind == MCK_VRM4 || Kind == MCK_VRM8)) {
1063     Op.Reg.RegNum = convertVRToVRMx(*getContext().getRegisterInfo(), Reg, Kind);
1064     if (Op.Reg.RegNum == 0)
1065       return Match_InvalidOperand;
1066     return Match_Success;
1067   }
1068   return Match_InvalidOperand;
1069 }
1070 
1071 bool RISCVAsmParser::generateImmOutOfRangeError(
1072     OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
1073     Twine Msg = "immediate must be an integer in the range") {
1074   SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1075   return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
1076 }
1077 
1078 bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1079                                              OperandVector &Operands,
1080                                              MCStreamer &Out,
1081                                              uint64_t &ErrorInfo,
1082                                              bool MatchingInlineAsm) {
1083   MCInst Inst;
1084   FeatureBitset MissingFeatures;
1085 
1086   auto Result = MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,
1087                                      MatchingInlineAsm);
1088   switch (Result) {
1089   default:
1090     break;
1091   case Match_Success:
1092     if (validateInstruction(Inst, Operands))
1093       return true;
1094     return processInstruction(Inst, IDLoc, Operands, Out);
1095   case Match_MissingFeature: {
1096     assert(MissingFeatures.any() && "Unknown missing features!");
1097     bool FirstFeature = true;
1098     std::string Msg = "instruction requires the following:";
1099     for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i) {
1100       if (MissingFeatures[i]) {
1101         Msg += FirstFeature ? " " : ", ";
1102         Msg += getSubtargetFeatureName(i);
1103         FirstFeature = false;
1104       }
1105     }
1106     return Error(IDLoc, Msg);
1107   }
1108   case Match_MnemonicFail: {
1109     FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
1110     std::string Suggestion = RISCVMnemonicSpellCheck(
1111         ((RISCVOperand &)*Operands[0]).getToken(), FBS, 0);
1112     return Error(IDLoc, "unrecognized instruction mnemonic" + Suggestion);
1113   }
1114   case Match_InvalidOperand: {
1115     SMLoc ErrorLoc = IDLoc;
1116     if (ErrorInfo != ~0ULL) {
1117       if (ErrorInfo >= Operands.size())
1118         return Error(ErrorLoc, "too few operands for instruction");
1119 
1120       ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1121       if (ErrorLoc == SMLoc())
1122         ErrorLoc = IDLoc;
1123     }
1124     return Error(ErrorLoc, "invalid operand for instruction");
1125   }
1126   }
1127 
1128   // Handle the case when the error message is of specific type
1129   // other than the generic Match_InvalidOperand, and the
1130   // corresponding operand is missing.
1131   if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
1132     SMLoc ErrorLoc = IDLoc;
1133     if (ErrorInfo != ~0ULL && ErrorInfo >= Operands.size())
1134       return Error(ErrorLoc, "too few operands for instruction");
1135   }
1136 
1137   switch (Result) {
1138   default:
1139     break;
1140   case Match_InvalidImmXLenLI:
1141     if (isRV64()) {
1142       SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1143       return Error(ErrorLoc, "operand must be a constant 64-bit integer");
1144     }
1145     return generateImmOutOfRangeError(Operands, ErrorInfo,
1146                                       std::numeric_limits<int32_t>::min(),
1147                                       std::numeric_limits<uint32_t>::max());
1148   case Match_InvalidImmZero: {
1149     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1150     return Error(ErrorLoc, "immediate must be zero");
1151   }
1152   case Match_InvalidUImmLog2XLen:
1153     if (isRV64())
1154       return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
1155     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
1156   case Match_InvalidUImmLog2XLenNonZero:
1157     if (isRV64())
1158       return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
1159     return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
1160   case Match_InvalidUImmLog2XLenHalf:
1161     if (isRV64())
1162       return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
1163     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 4) - 1);
1164   case Match_InvalidUImm2:
1165     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 2) - 1);
1166   case Match_InvalidUImm3:
1167     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 3) - 1);
1168   case Match_InvalidUImm5:
1169     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
1170   case Match_InvalidUImm7:
1171     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 7) - 1);
1172   case Match_InvalidSImm5:
1173     return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 4),
1174                                       (1 << 4) - 1);
1175   case Match_InvalidSImm6:
1176     return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
1177                                       (1 << 5) - 1);
1178   case Match_InvalidSImm6NonZero:
1179     return generateImmOutOfRangeError(
1180         Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
1181         "immediate must be non-zero in the range");
1182   case Match_InvalidCLUIImm:
1183     return generateImmOutOfRangeError(
1184         Operands, ErrorInfo, 1, (1 << 5) - 1,
1185         "immediate must be in [0xfffe0, 0xfffff] or");
1186   case Match_InvalidUImm7Lsb00:
1187     return generateImmOutOfRangeError(
1188         Operands, ErrorInfo, 0, (1 << 7) - 4,
1189         "immediate must be a multiple of 4 bytes in the range");
1190   case Match_InvalidUImm8Lsb00:
1191     return generateImmOutOfRangeError(
1192         Operands, ErrorInfo, 0, (1 << 8) - 4,
1193         "immediate must be a multiple of 4 bytes in the range");
1194   case Match_InvalidUImm8Lsb000:
1195     return generateImmOutOfRangeError(
1196         Operands, ErrorInfo, 0, (1 << 8) - 8,
1197         "immediate must be a multiple of 8 bytes in the range");
1198   case Match_InvalidSImm9Lsb0:
1199     return generateImmOutOfRangeError(
1200         Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
1201         "immediate must be a multiple of 2 bytes in the range");
1202   case Match_InvalidUImm9Lsb000:
1203     return generateImmOutOfRangeError(
1204         Operands, ErrorInfo, 0, (1 << 9) - 8,
1205         "immediate must be a multiple of 8 bytes in the range");
1206   case Match_InvalidUImm10Lsb00NonZero:
1207     return generateImmOutOfRangeError(
1208         Operands, ErrorInfo, 4, (1 << 10) - 4,
1209         "immediate must be a multiple of 4 bytes in the range");
1210   case Match_InvalidSImm10Lsb0000NonZero:
1211     return generateImmOutOfRangeError(
1212         Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
1213         "immediate must be a multiple of 16 bytes and non-zero in the range");
1214   case Match_InvalidSImm12:
1215     return generateImmOutOfRangeError(
1216         Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
1217         "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an "
1218         "integer in the range");
1219   case Match_InvalidSImm12Lsb0:
1220     return generateImmOutOfRangeError(
1221         Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
1222         "immediate must be a multiple of 2 bytes in the range");
1223   case Match_InvalidSImm13Lsb0:
1224     return generateImmOutOfRangeError(
1225         Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
1226         "immediate must be a multiple of 2 bytes in the range");
1227   case Match_InvalidUImm20LUI:
1228     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
1229                                       "operand must be a symbol with "
1230                                       "%hi/%tprel_hi modifier or an integer in "
1231                                       "the range");
1232   case Match_InvalidUImm20AUIPC:
1233     return generateImmOutOfRangeError(
1234         Operands, ErrorInfo, 0, (1 << 20) - 1,
1235         "operand must be a symbol with a "
1236         "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi modifier or "
1237         "an integer in the range");
1238   case Match_InvalidSImm21Lsb0JAL:
1239     return generateImmOutOfRangeError(
1240         Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
1241         "immediate must be a multiple of 2 bytes in the range");
1242   case Match_InvalidCSRSystemRegister: {
1243     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
1244                                       "operand must be a valid system register "
1245                                       "name or an integer in the range");
1246   }
1247   case Match_InvalidFenceArg: {
1248     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1249     return Error(ErrorLoc, "operand must be formed of letters selected "
1250                            "in-order from 'iorw' or be 0");
1251   }
1252   case Match_InvalidFRMArg: {
1253     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1254     return Error(
1255         ErrorLoc,
1256         "operand must be a valid floating point rounding mode mnemonic");
1257   }
1258   case Match_InvalidBareSymbol: {
1259     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1260     return Error(ErrorLoc, "operand must be a bare symbol name");
1261   }
1262   case Match_InvalidPseudoJumpSymbol: {
1263     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1264     return Error(ErrorLoc, "operand must be a valid jump target");
1265   }
1266   case Match_InvalidCallSymbol: {
1267     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1268     return Error(ErrorLoc, "operand must be a bare symbol name");
1269   }
1270   case Match_InvalidTPRelAddSymbol: {
1271     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1272     return Error(ErrorLoc, "operand must be a symbol with %tprel_add modifier");
1273   }
1274   case Match_InvalidVTypeI: {
1275     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1276     return Error(
1277         ErrorLoc,
1278         "operand must be "
1279         "e[8|16|32|64|128|256|512|1024],m[1|2|4|8|f2|f4|f8],[ta|tu],[ma|mu]");
1280   }
1281   case Match_InvalidVMaskRegister: {
1282     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
1283     return Error(ErrorLoc, "operand must be v0.t");
1284   }
1285   case Match_InvalidSImm5Plus1: {
1286     return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 4) + 1,
1287                                       (1 << 4),
1288                                       "immediate must be in the range");
1289   }
1290   case Match_InvalidRnumArg: {
1291     return generateImmOutOfRangeError(Operands, ErrorInfo, 0, 10);
1292   }
1293   }
1294 
1295   llvm_unreachable("Unknown match type detected!");
1296 }
1297 
1298 // Attempts to match Name as a register (either using the default name or
1299 // alternative ABI names), setting RegNo to the matching register. Upon
1300 // failure, returns true and sets RegNo to 0. If IsRV32E then registers
1301 // x16-x31 will be rejected.
1302 static bool matchRegisterNameHelper(bool IsRV32E, MCRegister &RegNo,
1303                                     StringRef Name) {
1304   RegNo = MatchRegisterName(Name);
1305   // The 16-/32- and 64-bit FPRs have the same asm name. Check that the initial
1306   // match always matches the 64-bit variant, and not the 16/32-bit one.
1307   assert(!(RegNo >= RISCV::F0_H && RegNo <= RISCV::F31_H));
1308   assert(!(RegNo >= RISCV::F0_F && RegNo <= RISCV::F31_F));
1309   // The default FPR register class is based on the tablegen enum ordering.
1310   static_assert(RISCV::F0_D < RISCV::F0_H, "FPR matching must be updated");
1311   static_assert(RISCV::F0_D < RISCV::F0_F, "FPR matching must be updated");
1312   if (RegNo == RISCV::NoRegister)
1313     RegNo = MatchRegisterAltName(Name);
1314   if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
1315     RegNo = RISCV::NoRegister;
1316   return RegNo == RISCV::NoRegister;
1317 }
1318 
1319 bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1320                                    SMLoc &EndLoc) {
1321   if (tryParseRegister(RegNo, StartLoc, EndLoc) != MatchOperand_Success)
1322     return Error(StartLoc, "invalid register name");
1323   return false;
1324 }
1325 
1326 OperandMatchResultTy RISCVAsmParser::tryParseRegister(unsigned &RegNo,
1327                                                       SMLoc &StartLoc,
1328                                                       SMLoc &EndLoc) {
1329   const AsmToken &Tok = getParser().getTok();
1330   StartLoc = Tok.getLoc();
1331   EndLoc = Tok.getEndLoc();
1332   RegNo = 0;
1333   StringRef Name = getLexer().getTok().getIdentifier();
1334 
1335   if (matchRegisterNameHelper(isRV32E(), (MCRegister &)RegNo, Name))
1336     return MatchOperand_NoMatch;
1337 
1338   getParser().Lex(); // Eat identifier token.
1339   return MatchOperand_Success;
1340 }
1341 
1342 OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands,
1343                                                    bool AllowParens) {
1344   SMLoc FirstS = getLoc();
1345   bool HadParens = false;
1346   AsmToken LParen;
1347 
1348   // If this is an LParen and a parenthesised register name is allowed, parse it
1349   // atomically.
1350   if (AllowParens && getLexer().is(AsmToken::LParen)) {
1351     AsmToken Buf[2];
1352     size_t ReadCount = getLexer().peekTokens(Buf);
1353     if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
1354       HadParens = true;
1355       LParen = getParser().getTok();
1356       getParser().Lex(); // Eat '('
1357     }
1358   }
1359 
1360   switch (getLexer().getKind()) {
1361   default:
1362     if (HadParens)
1363       getLexer().UnLex(LParen);
1364     return MatchOperand_NoMatch;
1365   case AsmToken::Identifier:
1366     StringRef Name = getLexer().getTok().getIdentifier();
1367     MCRegister RegNo;
1368     matchRegisterNameHelper(isRV32E(), RegNo, Name);
1369 
1370     if (RegNo == RISCV::NoRegister) {
1371       if (HadParens)
1372         getLexer().UnLex(LParen);
1373       return MatchOperand_NoMatch;
1374     }
1375     if (HadParens)
1376       Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64()));
1377     SMLoc S = getLoc();
1378     SMLoc E = SMLoc::getFromPointer(S.getPointer() + Name.size());
1379     getLexer().Lex();
1380     Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
1381   }
1382 
1383   if (HadParens) {
1384     getParser().Lex(); // Eat ')'
1385     Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
1386   }
1387 
1388   return MatchOperand_Success;
1389 }
1390 
1391 OperandMatchResultTy
1392 RISCVAsmParser::parseInsnDirectiveOpcode(OperandVector &Operands) {
1393   SMLoc S = getLoc();
1394   SMLoc E;
1395   const MCExpr *Res;
1396 
1397   switch (getLexer().getKind()) {
1398   default:
1399     return MatchOperand_NoMatch;
1400   case AsmToken::LParen:
1401   case AsmToken::Minus:
1402   case AsmToken::Plus:
1403   case AsmToken::Exclaim:
1404   case AsmToken::Tilde:
1405   case AsmToken::Integer:
1406   case AsmToken::String: {
1407     if (getParser().parseExpression(Res, E))
1408       return MatchOperand_ParseFail;
1409 
1410     auto *CE = dyn_cast<MCConstantExpr>(Res);
1411     if (CE) {
1412       int64_t Imm = CE->getValue();
1413       if (isUInt<7>(Imm)) {
1414         Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1415         return MatchOperand_Success;
1416       }
1417     }
1418 
1419     Twine Msg = "immediate must be an integer in the range";
1420     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 7) - 1) + "]");
1421     return MatchOperand_ParseFail;
1422   }
1423   case AsmToken::Identifier: {
1424     StringRef Identifier;
1425     if (getParser().parseIdentifier(Identifier))
1426       return MatchOperand_ParseFail;
1427 
1428     auto Opcode = RISCVInsnOpcode::lookupRISCVOpcodeByName(Identifier);
1429     if (Opcode) {
1430       Res = MCConstantExpr::create(Opcode->Value, getContext());
1431       E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
1432       Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1433       return MatchOperand_Success;
1434     }
1435 
1436     Twine Msg = "operand must be a valid opcode name or an "
1437                 "integer in the range";
1438     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 7) - 1) + "]");
1439     return MatchOperand_ParseFail;
1440   }
1441   case AsmToken::Percent: {
1442     // Discard operand with modifier.
1443     Twine Msg = "immediate must be an integer in the range";
1444     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 7) - 1) + "]");
1445     return MatchOperand_ParseFail;
1446   }
1447   }
1448 
1449   return MatchOperand_NoMatch;
1450 }
1451 
1452 OperandMatchResultTy
1453 RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
1454   SMLoc S = getLoc();
1455   const MCExpr *Res;
1456 
1457   switch (getLexer().getKind()) {
1458   default:
1459     return MatchOperand_NoMatch;
1460   case AsmToken::LParen:
1461   case AsmToken::Minus:
1462   case AsmToken::Plus:
1463   case AsmToken::Exclaim:
1464   case AsmToken::Tilde:
1465   case AsmToken::Integer:
1466   case AsmToken::String: {
1467     if (getParser().parseExpression(Res))
1468       return MatchOperand_ParseFail;
1469 
1470     auto *CE = dyn_cast<MCConstantExpr>(Res);
1471     if (CE) {
1472       int64_t Imm = CE->getValue();
1473       if (isUInt<12>(Imm)) {
1474         auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm);
1475         // Accept an immediate representing a named or un-named Sys Reg
1476         // if the range is valid, regardless of the required features.
1477         Operands.push_back(RISCVOperand::createSysReg(
1478             SysReg ? SysReg->Name : "", S, Imm, isRV64()));
1479         return MatchOperand_Success;
1480       }
1481     }
1482 
1483     Twine Msg = "immediate must be an integer in the range";
1484     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1485     return MatchOperand_ParseFail;
1486   }
1487   case AsmToken::Identifier: {
1488     StringRef Identifier;
1489     if (getParser().parseIdentifier(Identifier))
1490       return MatchOperand_ParseFail;
1491 
1492     auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1493     if (!SysReg)
1494       SysReg = RISCVSysReg::lookupSysRegByAltName(Identifier);
1495     if (!SysReg)
1496       if ((SysReg = RISCVSysReg::lookupSysRegByDeprecatedName(Identifier)))
1497         Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
1498                        SysReg->Name + "'");
1499 
1500     // Accept a named Sys Reg if the required features are present.
1501     if (SysReg) {
1502       if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1503         Error(S, "system register use requires an option to be enabled");
1504         return MatchOperand_ParseFail;
1505       }
1506       Operands.push_back(RISCVOperand::createSysReg(
1507           Identifier, S, SysReg->Encoding, isRV64()));
1508       return MatchOperand_Success;
1509     }
1510 
1511     Twine Msg = "operand must be a valid system register name "
1512                 "or an integer in the range";
1513     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1514     return MatchOperand_ParseFail;
1515   }
1516   case AsmToken::Percent: {
1517     // Discard operand with modifier.
1518     Twine Msg = "immediate must be an integer in the range";
1519     Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]");
1520     return MatchOperand_ParseFail;
1521   }
1522   }
1523 
1524   return MatchOperand_NoMatch;
1525 }
1526 
1527 OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) {
1528   SMLoc S = getLoc();
1529   SMLoc E;
1530   const MCExpr *Res;
1531 
1532   switch (getLexer().getKind()) {
1533   default:
1534     return MatchOperand_NoMatch;
1535   case AsmToken::LParen:
1536   case AsmToken::Dot:
1537   case AsmToken::Minus:
1538   case AsmToken::Plus:
1539   case AsmToken::Exclaim:
1540   case AsmToken::Tilde:
1541   case AsmToken::Integer:
1542   case AsmToken::String:
1543   case AsmToken::Identifier:
1544     if (getParser().parseExpression(Res, E))
1545       return MatchOperand_ParseFail;
1546     break;
1547   case AsmToken::Percent:
1548     return parseOperandWithModifier(Operands);
1549   }
1550 
1551   Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1552   return MatchOperand_Success;
1553 }
1554 
1555 OperandMatchResultTy
1556 RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) {
1557   SMLoc S = getLoc();
1558   SMLoc E;
1559 
1560   if (getLexer().getKind() != AsmToken::Percent) {
1561     Error(getLoc(), "expected '%' for operand modifier");
1562     return MatchOperand_ParseFail;
1563   }
1564 
1565   getParser().Lex(); // Eat '%'
1566 
1567   if (getLexer().getKind() != AsmToken::Identifier) {
1568     Error(getLoc(), "expected valid identifier for operand modifier");
1569     return MatchOperand_ParseFail;
1570   }
1571   StringRef Identifier = getParser().getTok().getIdentifier();
1572   RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier);
1573   if (VK == RISCVMCExpr::VK_RISCV_Invalid) {
1574     Error(getLoc(), "unrecognized operand modifier");
1575     return MatchOperand_ParseFail;
1576   }
1577 
1578   getParser().Lex(); // Eat the identifier
1579   if (getLexer().getKind() != AsmToken::LParen) {
1580     Error(getLoc(), "expected '('");
1581     return MatchOperand_ParseFail;
1582   }
1583   getParser().Lex(); // Eat '('
1584 
1585   const MCExpr *SubExpr;
1586   if (getParser().parseParenExpression(SubExpr, E)) {
1587     return MatchOperand_ParseFail;
1588   }
1589 
1590   const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext());
1591   Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
1592   return MatchOperand_Success;
1593 }
1594 
1595 OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
1596   SMLoc S = getLoc();
1597   const MCExpr *Res;
1598 
1599   if (getLexer().getKind() != AsmToken::Identifier)
1600     return MatchOperand_NoMatch;
1601 
1602   StringRef Identifier;
1603   AsmToken Tok = getLexer().getTok();
1604 
1605   if (getParser().parseIdentifier(Identifier))
1606     return MatchOperand_ParseFail;
1607 
1608   SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
1609 
1610   if (Identifier.consume_back("@plt")) {
1611     Error(getLoc(), "'@plt' operand not valid for instruction");
1612     return MatchOperand_ParseFail;
1613   }
1614 
1615   MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1616 
1617   if (Sym->isVariable()) {
1618     const MCExpr *V = Sym->getVariableValue(/*SetUsed=*/false);
1619     if (!isa<MCSymbolRefExpr>(V)) {
1620       getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1621       return MatchOperand_NoMatch;
1622     }
1623     Res = V;
1624   } else
1625     Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1626 
1627   MCBinaryExpr::Opcode Opcode;
1628   switch (getLexer().getKind()) {
1629   default:
1630     Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1631     return MatchOperand_Success;
1632   case AsmToken::Plus:
1633     Opcode = MCBinaryExpr::Add;
1634     getLexer().Lex();
1635     break;
1636   case AsmToken::Minus:
1637     Opcode = MCBinaryExpr::Sub;
1638     getLexer().Lex();
1639     break;
1640   }
1641 
1642   const MCExpr *Expr;
1643   if (getParser().parseExpression(Expr, E))
1644     return MatchOperand_ParseFail;
1645   Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
1646   Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1647   return MatchOperand_Success;
1648 }
1649 
1650 OperandMatchResultTy RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
1651   SMLoc S = getLoc();
1652   const MCExpr *Res;
1653 
1654   if (getLexer().getKind() != AsmToken::Identifier)
1655     return MatchOperand_NoMatch;
1656 
1657   // Avoid parsing the register in `call rd, foo` as a call symbol.
1658   if (getLexer().peekTok().getKind() != AsmToken::EndOfStatement)
1659     return MatchOperand_NoMatch;
1660 
1661   StringRef Identifier;
1662   if (getParser().parseIdentifier(Identifier))
1663     return MatchOperand_ParseFail;
1664 
1665   SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
1666 
1667   RISCVMCExpr::VariantKind Kind = RISCVMCExpr::VK_RISCV_CALL;
1668   if (Identifier.consume_back("@plt"))
1669     Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
1670 
1671   MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1672   Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1673   Res = RISCVMCExpr::create(Res, Kind, getContext());
1674   Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1675   return MatchOperand_Success;
1676 }
1677 
1678 OperandMatchResultTy
1679 RISCVAsmParser::parsePseudoJumpSymbol(OperandVector &Operands) {
1680   SMLoc S = getLoc();
1681   SMLoc E;
1682   const MCExpr *Res;
1683 
1684   if (getParser().parseExpression(Res, E))
1685     return MatchOperand_ParseFail;
1686 
1687   if (Res->getKind() != MCExpr::ExprKind::SymbolRef ||
1688       cast<MCSymbolRefExpr>(Res)->getKind() ==
1689           MCSymbolRefExpr::VariantKind::VK_PLT) {
1690     Error(S, "operand must be a valid jump target");
1691     return MatchOperand_ParseFail;
1692   }
1693 
1694   Res = RISCVMCExpr::create(Res, RISCVMCExpr::VK_RISCV_CALL, getContext());
1695   Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1696   return MatchOperand_Success;
1697 }
1698 
1699 OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) {
1700   // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo`
1701   // both being acceptable forms. When parsing `jal ra, foo` this function
1702   // will be called for the `ra` register operand in an attempt to match the
1703   // single-operand alias. parseJALOffset must fail for this case. It would
1704   // seem logical to try parse the operand using parseImmediate and return
1705   // NoMatch if the next token is a comma (meaning we must be parsing a jal in
1706   // the second form rather than the first). We can't do this as there's no
1707   // way of rewinding the lexer state. Instead, return NoMatch if this operand
1708   // is an identifier and is followed by a comma.
1709   if (getLexer().is(AsmToken::Identifier) &&
1710       getLexer().peekTok().is(AsmToken::Comma))
1711     return MatchOperand_NoMatch;
1712 
1713   return parseImmediate(Operands);
1714 }
1715 
1716 OperandMatchResultTy RISCVAsmParser::parseVTypeI(OperandVector &Operands) {
1717   SMLoc S = getLoc();
1718   if (getLexer().isNot(AsmToken::Identifier))
1719     return MatchOperand_NoMatch;
1720 
1721   SmallVector<AsmToken, 7> VTypeIElements;
1722   // Put all the tokens for vtypei operand into VTypeIElements vector.
1723   while (getLexer().isNot(AsmToken::EndOfStatement)) {
1724     VTypeIElements.push_back(getLexer().getTok());
1725     getLexer().Lex();
1726     if (getLexer().is(AsmToken::EndOfStatement))
1727       break;
1728     if (getLexer().isNot(AsmToken::Comma))
1729       goto MatchFail;
1730     AsmToken Comma = getLexer().getTok();
1731     VTypeIElements.push_back(Comma);
1732     getLexer().Lex();
1733   }
1734 
1735   if (VTypeIElements.size() == 7) {
1736     // The VTypeIElements layout is:
1737     // SEW comma LMUL comma TA comma MA
1738     //  0    1    2     3    4   5    6
1739     StringRef Name = VTypeIElements[0].getIdentifier();
1740     if (!Name.consume_front("e"))
1741       goto MatchFail;
1742     unsigned Sew;
1743     if (Name.getAsInteger(10, Sew))
1744       goto MatchFail;
1745     if (!RISCVVType::isValidSEW(Sew))
1746       goto MatchFail;
1747 
1748     Name = VTypeIElements[2].getIdentifier();
1749     if (!Name.consume_front("m"))
1750       goto MatchFail;
1751     // "m" or "mf"
1752     bool Fractional = Name.consume_front("f");
1753     unsigned Lmul;
1754     if (Name.getAsInteger(10, Lmul))
1755       goto MatchFail;
1756     if (!RISCVVType::isValidLMUL(Lmul, Fractional))
1757       goto MatchFail;
1758 
1759     // ta or tu
1760     Name = VTypeIElements[4].getIdentifier();
1761     bool TailAgnostic;
1762     if (Name == "ta")
1763       TailAgnostic = true;
1764     else if (Name == "tu")
1765       TailAgnostic = false;
1766     else
1767       goto MatchFail;
1768 
1769     // ma or mu
1770     Name = VTypeIElements[6].getIdentifier();
1771     bool MaskAgnostic;
1772     if (Name == "ma")
1773       MaskAgnostic = true;
1774     else if (Name == "mu")
1775       MaskAgnostic = false;
1776     else
1777       goto MatchFail;
1778 
1779     unsigned LmulLog2 = Log2_32(Lmul);
1780     RISCVII::VLMUL VLMUL =
1781         static_cast<RISCVII::VLMUL>(Fractional ? 8 - LmulLog2 : LmulLog2);
1782 
1783     unsigned VTypeI =
1784         RISCVVType::encodeVTYPE(VLMUL, Sew, TailAgnostic, MaskAgnostic);
1785     Operands.push_back(RISCVOperand::createVType(VTypeI, S, isRV64()));
1786     return MatchOperand_Success;
1787   }
1788 
1789 // If NoMatch, unlex all the tokens that comprise a vtypei operand
1790 MatchFail:
1791   while (!VTypeIElements.empty())
1792     getLexer().UnLex(VTypeIElements.pop_back_val());
1793   return MatchOperand_NoMatch;
1794 }
1795 
1796 OperandMatchResultTy RISCVAsmParser::parseMaskReg(OperandVector &Operands) {
1797   switch (getLexer().getKind()) {
1798   default:
1799     return MatchOperand_NoMatch;
1800   case AsmToken::Identifier:
1801     StringRef Name = getLexer().getTok().getIdentifier();
1802     if (!Name.consume_back(".t")) {
1803       Error(getLoc(), "expected '.t' suffix");
1804       return MatchOperand_ParseFail;
1805     }
1806     MCRegister RegNo;
1807     matchRegisterNameHelper(isRV32E(), RegNo, Name);
1808 
1809     if (RegNo == RISCV::NoRegister)
1810       return MatchOperand_NoMatch;
1811     if (RegNo != RISCV::V0)
1812       return MatchOperand_NoMatch;
1813     SMLoc S = getLoc();
1814     SMLoc E = SMLoc::getFromPointer(S.getPointer() + Name.size());
1815     getLexer().Lex();
1816     Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
1817   }
1818 
1819   return MatchOperand_Success;
1820 }
1821 
1822 OperandMatchResultTy RISCVAsmParser::parseGPRAsFPR(OperandVector &Operands) {
1823   switch (getLexer().getKind()) {
1824   default:
1825     return MatchOperand_NoMatch;
1826   case AsmToken::Identifier:
1827     StringRef Name = getLexer().getTok().getIdentifier();
1828     MCRegister RegNo;
1829     matchRegisterNameHelper(isRV32E(), RegNo, Name);
1830 
1831     if (RegNo == RISCV::NoRegister)
1832       return MatchOperand_NoMatch;
1833     SMLoc S = getLoc();
1834     SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1835     getLexer().Lex();
1836     Operands.push_back(RISCVOperand::createReg(
1837         RegNo, S, E, isRV64(), !getSTI().hasFeature(RISCV::FeatureStdExtF)));
1838   }
1839   return MatchOperand_Success;
1840 }
1841 
1842 OperandMatchResultTy
1843 RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) {
1844   if (getLexer().isNot(AsmToken::LParen)) {
1845     Error(getLoc(), "expected '('");
1846     return MatchOperand_ParseFail;
1847   }
1848 
1849   getParser().Lex(); // Eat '('
1850   Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64()));
1851 
1852   if (parseRegister(Operands) != MatchOperand_Success) {
1853     Error(getLoc(), "expected register");
1854     return MatchOperand_ParseFail;
1855   }
1856 
1857   if (getLexer().isNot(AsmToken::RParen)) {
1858     Error(getLoc(), "expected ')'");
1859     return MatchOperand_ParseFail;
1860   }
1861 
1862   getParser().Lex(); // Eat ')'
1863   Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64()));
1864 
1865   return MatchOperand_Success;
1866 }
1867 
1868 OperandMatchResultTy
1869 RISCVAsmParser::parseZeroOffsetMemOp(OperandVector &Operands) {
1870   // Atomic operations such as lr.w, sc.w, and amo*.w accept a "memory operand"
1871   // as one of their register operands, such as `(a0)`. This just denotes that
1872   // the register (in this case `a0`) contains a memory address.
1873   //
1874   // Normally, we would be able to parse these by putting the parens into the
1875   // instruction string. However, GNU as also accepts a zero-offset memory
1876   // operand (such as `0(a0)`), and ignores the 0. Normally this would be parsed
1877   // with parseImmediate followed by parseMemOpBaseReg, but these instructions
1878   // do not accept an immediate operand, and we do not want to add a "dummy"
1879   // operand that is silently dropped.
1880   //
1881   // Instead, we use this custom parser. This will: allow (and discard) an
1882   // offset if it is zero; require (and discard) parentheses; and add only the
1883   // parsed register operand to `Operands`.
1884   //
1885   // These operands are printed with RISCVInstPrinter::printZeroOffsetMemOp,
1886   // which will only print the register surrounded by parentheses (which GNU as
1887   // also uses as its canonical representation for these operands).
1888   std::unique_ptr<RISCVOperand> OptionalImmOp;
1889 
1890   if (getLexer().isNot(AsmToken::LParen)) {
1891     // Parse an Integer token. We do not accept arbritrary constant expressions
1892     // in the offset field (because they may include parens, which complicates
1893     // parsing a lot).
1894     int64_t ImmVal;
1895     SMLoc ImmStart = getLoc();
1896     if (getParser().parseIntToken(ImmVal,
1897                                   "expected '(' or optional integer offset"))
1898       return MatchOperand_ParseFail;
1899 
1900     // Create a RISCVOperand for checking later (so the error messages are
1901     // nicer), but we don't add it to Operands.
1902     SMLoc ImmEnd = getLoc();
1903     OptionalImmOp =
1904         RISCVOperand::createImm(MCConstantExpr::create(ImmVal, getContext()),
1905                                 ImmStart, ImmEnd, isRV64());
1906   }
1907 
1908   if (getLexer().isNot(AsmToken::LParen)) {
1909     Error(getLoc(), OptionalImmOp ? "expected '(' after optional integer offset"
1910                                   : "expected '(' or optional integer offset");
1911     return MatchOperand_ParseFail;
1912   }
1913   getParser().Lex(); // Eat '('
1914 
1915   if (parseRegister(Operands) != MatchOperand_Success) {
1916     Error(getLoc(), "expected register");
1917     return MatchOperand_ParseFail;
1918   }
1919 
1920   if (getLexer().isNot(AsmToken::RParen)) {
1921     Error(getLoc(), "expected ')'");
1922     return MatchOperand_ParseFail;
1923   }
1924   getParser().Lex(); // Eat ')'
1925 
1926   // Deferred Handling of non-zero offsets. This makes the error messages nicer.
1927   if (OptionalImmOp && !OptionalImmOp->isImmZero()) {
1928     Error(OptionalImmOp->getStartLoc(), "optional integer offset must be 0",
1929           SMRange(OptionalImmOp->getStartLoc(), OptionalImmOp->getEndLoc()));
1930     return MatchOperand_ParseFail;
1931   }
1932 
1933   return MatchOperand_Success;
1934 }
1935 
1936 /// Looks at a token type and creates the relevant operand from this
1937 /// information, adding to Operands. If operand was parsed, returns false, else
1938 /// true.
1939 bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1940   // Check if the current operand has a custom associated parser, if so, try to
1941   // custom parse the operand, or fallback to the general approach.
1942   OperandMatchResultTy Result =
1943       MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1944   if (Result == MatchOperand_Success)
1945     return false;
1946   if (Result == MatchOperand_ParseFail)
1947     return true;
1948 
1949   // Attempt to parse token as a register.
1950   if (parseRegister(Operands, true) == MatchOperand_Success)
1951     return false;
1952 
1953   // Attempt to parse token as an immediate
1954   if (parseImmediate(Operands) == MatchOperand_Success) {
1955     // Parse memory base register if present
1956     if (getLexer().is(AsmToken::LParen))
1957       return parseMemOpBaseReg(Operands) != MatchOperand_Success;
1958     return false;
1959   }
1960 
1961   // Finally we have exhausted all options and must declare defeat.
1962   Error(getLoc(), "unknown operand");
1963   return true;
1964 }
1965 
1966 bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1967                                       StringRef Name, SMLoc NameLoc,
1968                                       OperandVector &Operands) {
1969   // Ensure that if the instruction occurs when relaxation is enabled,
1970   // relocations are forced for the file. Ideally this would be done when there
1971   // is enough information to reliably determine if the instruction itself may
1972   // cause relaxations. Unfortunately instruction processing stage occurs in the
1973   // same pass as relocation emission, so it's too late to set a 'sticky bit'
1974   // for the entire file.
1975   if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1976     auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1977     if (Assembler != nullptr) {
1978       RISCVAsmBackend &MAB =
1979           static_cast<RISCVAsmBackend &>(Assembler->getBackend());
1980       MAB.setForceRelocs();
1981     }
1982   }
1983 
1984   // First operand is token for instruction
1985   Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
1986 
1987   // If there are no more operands, then finish
1988   if (getLexer().is(AsmToken::EndOfStatement)) {
1989     getParser().Lex(); // Consume the EndOfStatement.
1990     return false;
1991   }
1992 
1993   // Parse first operand
1994   if (parseOperand(Operands, Name))
1995     return true;
1996 
1997   // Parse until end of statement, consuming commas between operands
1998   unsigned OperandIdx = 1;
1999   while (getLexer().is(AsmToken::Comma)) {
2000     // Consume comma token
2001     getLexer().Lex();
2002 
2003     // Parse next operand
2004     if (parseOperand(Operands, Name))
2005       return true;
2006 
2007     ++OperandIdx;
2008   }
2009 
2010   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2011     SMLoc Loc = getLexer().getLoc();
2012     getParser().eatToEndOfStatement();
2013     return Error(Loc, "unexpected token");
2014   }
2015 
2016   getParser().Lex(); // Consume the EndOfStatement.
2017   return false;
2018 }
2019 
2020 bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr,
2021                                        RISCVMCExpr::VariantKind &Kind) {
2022   Kind = RISCVMCExpr::VK_RISCV_None;
2023 
2024   if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
2025     Kind = RE->getKind();
2026     Expr = RE->getSubExpr();
2027   }
2028 
2029   MCValue Res;
2030   MCFixup Fixup;
2031   if (Expr->evaluateAsRelocatable(Res, nullptr, &Fixup))
2032     return Res.getRefKind() == RISCVMCExpr::VK_RISCV_None;
2033   return false;
2034 }
2035 
2036 bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) {
2037   // This returns false if this function recognizes the directive
2038   // regardless of whether it is successfully handles or reports an
2039   // error. Otherwise it returns true to give the generic parser a
2040   // chance at recognizing it.
2041   StringRef IDVal = DirectiveID.getString();
2042 
2043   if (IDVal == ".option")
2044     return parseDirectiveOption();
2045   if (IDVal == ".attribute")
2046     return parseDirectiveAttribute();
2047   if (IDVal == ".insn")
2048     return parseDirectiveInsn(DirectiveID.getLoc());
2049 
2050   return true;
2051 }
2052 
2053 bool RISCVAsmParser::parseDirectiveOption() {
2054   MCAsmParser &Parser = getParser();
2055   // Get the option token.
2056   AsmToken Tok = Parser.getTok();
2057   // At the moment only identifiers are supported.
2058   if (Tok.isNot(AsmToken::Identifier))
2059     return Error(Parser.getTok().getLoc(),
2060                  "unexpected token, expected identifier");
2061 
2062   StringRef Option = Tok.getIdentifier();
2063 
2064   if (Option == "push") {
2065     getTargetStreamer().emitDirectiveOptionPush();
2066 
2067     Parser.Lex();
2068     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2069       return Error(Parser.getTok().getLoc(),
2070                    "unexpected token, expected end of statement");
2071 
2072     pushFeatureBits();
2073     return false;
2074   }
2075 
2076   if (Option == "pop") {
2077     SMLoc StartLoc = Parser.getTok().getLoc();
2078     getTargetStreamer().emitDirectiveOptionPop();
2079 
2080     Parser.Lex();
2081     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2082       return Error(Parser.getTok().getLoc(),
2083                    "unexpected token, expected end of statement");
2084 
2085     if (popFeatureBits())
2086       return Error(StartLoc, ".option pop with no .option push");
2087 
2088     return false;
2089   }
2090 
2091   if (Option == "rvc") {
2092     getTargetStreamer().emitDirectiveOptionRVC();
2093 
2094     Parser.Lex();
2095     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2096       return Error(Parser.getTok().getLoc(),
2097                    "unexpected token, expected end of statement");
2098 
2099     setFeatureBits(RISCV::FeatureStdExtC, "c");
2100     return false;
2101   }
2102 
2103   if (Option == "norvc") {
2104     getTargetStreamer().emitDirectiveOptionNoRVC();
2105 
2106     Parser.Lex();
2107     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2108       return Error(Parser.getTok().getLoc(),
2109                    "unexpected token, expected end of statement");
2110 
2111     clearFeatureBits(RISCV::FeatureStdExtC, "c");
2112     return false;
2113   }
2114 
2115   if (Option == "pic") {
2116     getTargetStreamer().emitDirectiveOptionPIC();
2117 
2118     Parser.Lex();
2119     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2120       return Error(Parser.getTok().getLoc(),
2121                    "unexpected token, expected end of statement");
2122 
2123     ParserOptions.IsPicEnabled = true;
2124     return false;
2125   }
2126 
2127   if (Option == "nopic") {
2128     getTargetStreamer().emitDirectiveOptionNoPIC();
2129 
2130     Parser.Lex();
2131     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2132       return Error(Parser.getTok().getLoc(),
2133                    "unexpected token, expected end of statement");
2134 
2135     ParserOptions.IsPicEnabled = false;
2136     return false;
2137   }
2138 
2139   if (Option == "relax") {
2140     getTargetStreamer().emitDirectiveOptionRelax();
2141 
2142     Parser.Lex();
2143     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2144       return Error(Parser.getTok().getLoc(),
2145                    "unexpected token, expected end of statement");
2146 
2147     setFeatureBits(RISCV::FeatureRelax, "relax");
2148     return false;
2149   }
2150 
2151   if (Option == "norelax") {
2152     getTargetStreamer().emitDirectiveOptionNoRelax();
2153 
2154     Parser.Lex();
2155     if (Parser.getTok().isNot(AsmToken::EndOfStatement))
2156       return Error(Parser.getTok().getLoc(),
2157                    "unexpected token, expected end of statement");
2158 
2159     clearFeatureBits(RISCV::FeatureRelax, "relax");
2160     return false;
2161   }
2162 
2163   // Unknown option.
2164   Warning(Parser.getTok().getLoc(),
2165           "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or "
2166           "'norelax'");
2167   Parser.eatToEndOfStatement();
2168   return false;
2169 }
2170 
2171 /// parseDirectiveAttribute
2172 ///  ::= .attribute expression ',' ( expression | "string" )
2173 ///  ::= .attribute identifier ',' ( expression | "string" )
2174 bool RISCVAsmParser::parseDirectiveAttribute() {
2175   MCAsmParser &Parser = getParser();
2176   int64_t Tag;
2177   SMLoc TagLoc;
2178   TagLoc = Parser.getTok().getLoc();
2179   if (Parser.getTok().is(AsmToken::Identifier)) {
2180     StringRef Name = Parser.getTok().getIdentifier();
2181     Optional<unsigned> Ret =
2182         ELFAttrs::attrTypeFromString(Name, RISCVAttrs::getRISCVAttributeTags());
2183     if (!Ret.hasValue()) {
2184       Error(TagLoc, "attribute name not recognised: " + Name);
2185       return false;
2186     }
2187     Tag = Ret.getValue();
2188     Parser.Lex();
2189   } else {
2190     const MCExpr *AttrExpr;
2191 
2192     TagLoc = Parser.getTok().getLoc();
2193     if (Parser.parseExpression(AttrExpr))
2194       return true;
2195 
2196     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
2197     if (check(!CE, TagLoc, "expected numeric constant"))
2198       return true;
2199 
2200     Tag = CE->getValue();
2201   }
2202 
2203   if (Parser.parseToken(AsmToken::Comma, "comma expected"))
2204     return true;
2205 
2206   StringRef StringValue;
2207   int64_t IntegerValue = 0;
2208   bool IsIntegerValue = true;
2209 
2210   // RISC-V attributes have a string value if the tag number is odd
2211   // and an integer value if the tag number is even.
2212   if (Tag % 2)
2213     IsIntegerValue = false;
2214 
2215   SMLoc ValueExprLoc = Parser.getTok().getLoc();
2216   if (IsIntegerValue) {
2217     const MCExpr *ValueExpr;
2218     if (Parser.parseExpression(ValueExpr))
2219       return true;
2220 
2221     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
2222     if (!CE)
2223       return Error(ValueExprLoc, "expected numeric constant");
2224     IntegerValue = CE->getValue();
2225   } else {
2226     if (Parser.getTok().isNot(AsmToken::String))
2227       return Error(Parser.getTok().getLoc(), "expected string constant");
2228 
2229     StringValue = Parser.getTok().getStringContents();
2230     Parser.Lex();
2231   }
2232 
2233   if (Parser.parseToken(AsmToken::EndOfStatement,
2234                         "unexpected token in '.attribute' directive"))
2235     return true;
2236 
2237   if (IsIntegerValue)
2238     getTargetStreamer().emitAttribute(Tag, IntegerValue);
2239   else if (Tag != RISCVAttrs::ARCH)
2240     getTargetStreamer().emitTextAttribute(Tag, StringValue);
2241   else {
2242     StringRef Arch = StringValue;
2243     for (auto Feature : RISCVFeatureKV)
2244       if (llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key))
2245         clearFeatureBits(Feature.Value, Feature.Key);
2246 
2247     auto ParseResult = llvm::RISCVISAInfo::parseArchString(
2248         StringValue, /*EnableExperimentalExtension=*/true,
2249         /*ExperimentalExtensionVersionCheck=*/true);
2250     if (!ParseResult) {
2251       std::string Buffer;
2252       raw_string_ostream OutputErrMsg(Buffer);
2253       handleAllErrors(ParseResult.takeError(), [&](llvm::StringError &ErrMsg) {
2254         OutputErrMsg << "invalid arch name '" << Arch << "', "
2255                      << ErrMsg.getMessage();
2256       });
2257 
2258       return Error(ValueExprLoc, OutputErrMsg.str());
2259     }
2260     auto &ISAInfo = *ParseResult;
2261 
2262     for (auto Feature : RISCVFeatureKV)
2263       if (ISAInfo->hasExtension(Feature.Key))
2264         setFeatureBits(Feature.Value, Feature.Key);
2265 
2266     if (ISAInfo->getXLen() == 32)
2267       clearFeatureBits(RISCV::Feature64Bit, "64bit");
2268     else if (ISAInfo->getXLen() == 64)
2269       setFeatureBits(RISCV::Feature64Bit, "64bit");
2270     else
2271       return Error(ValueExprLoc, "bad arch string " + Arch);
2272 
2273     // Then emit the arch string.
2274     getTargetStreamer().emitTextAttribute(Tag, ISAInfo->toString());
2275   }
2276 
2277   return false;
2278 }
2279 
2280 /// parseDirectiveInsn
2281 /// ::= .insn [ format encoding, (operands (, operands)*) ]
2282 bool RISCVAsmParser::parseDirectiveInsn(SMLoc L) {
2283   MCAsmParser &Parser = getParser();
2284 
2285   // Expect instruction format as identifier.
2286   StringRef Format;
2287   SMLoc ErrorLoc = Parser.getTok().getLoc();
2288   if (Parser.parseIdentifier(Format))
2289     return Error(ErrorLoc, "expected instruction format");
2290 
2291   if (Format != "r" && Format != "r4" && Format != "i" && Format != "b" &&
2292       Format != "sb" && Format != "u" && Format != "j" && Format != "uj" &&
2293       Format != "s")
2294     return Error(ErrorLoc, "invalid instruction format");
2295 
2296   std::string FormatName = (".insn_" + Format).str();
2297 
2298   ParseInstructionInfo Info;
2299   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> Operands;
2300 
2301   if (ParseInstruction(Info, FormatName, L, Operands))
2302     return true;
2303 
2304   unsigned Opcode;
2305   uint64_t ErrorInfo;
2306   return MatchAndEmitInstruction(L, Opcode, Operands, Parser.getStreamer(),
2307                                  ErrorInfo,
2308                                  /*MatchingInlineAsm=*/false);
2309 }
2310 
2311 void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
2312   MCInst CInst;
2313   bool Res = compressInst(CInst, Inst, getSTI(), S.getContext());
2314   if (Res)
2315     ++RISCVNumInstrsCompressed;
2316   S.emitInstruction((Res ? CInst : Inst), getSTI());
2317 }
2318 
2319 void RISCVAsmParser::emitLoadImm(MCRegister DestReg, int64_t Value,
2320                                  MCStreamer &Out) {
2321   RISCVMatInt::InstSeq Seq =
2322       RISCVMatInt::generateInstSeq(Value, getSTI().getFeatureBits());
2323 
2324   MCRegister SrcReg = RISCV::X0;
2325   for (RISCVMatInt::Inst &Inst : Seq) {
2326     if (Inst.Opc == RISCV::LUI) {
2327       emitToStreamer(
2328           Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
2329     } else if (Inst.Opc == RISCV::ADD_UW) {
2330       emitToStreamer(Out, MCInstBuilder(RISCV::ADD_UW)
2331                               .addReg(DestReg)
2332                               .addReg(SrcReg)
2333                               .addReg(RISCV::X0));
2334     } else if (Inst.Opc == RISCV::SH1ADD || Inst.Opc == RISCV::SH2ADD ||
2335                Inst.Opc == RISCV::SH3ADD) {
2336       emitToStreamer(
2337           Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addReg(
2338                    SrcReg));
2339     } else {
2340       emitToStreamer(
2341           Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
2342                    Inst.Imm));
2343     }
2344 
2345     // Only the first instruction has X0 as its source.
2346     SrcReg = DestReg;
2347   }
2348 }
2349 
2350 void RISCVAsmParser::emitAuipcInstPair(MCOperand DestReg, MCOperand TmpReg,
2351                                        const MCExpr *Symbol,
2352                                        RISCVMCExpr::VariantKind VKHi,
2353                                        unsigned SecondOpcode, SMLoc IDLoc,
2354                                        MCStreamer &Out) {
2355   // A pair of instructions for PC-relative addressing; expands to
2356   //   TmpLabel: AUIPC TmpReg, VKHi(symbol)
2357   //             OP DestReg, TmpReg, %pcrel_lo(TmpLabel)
2358   MCContext &Ctx = getContext();
2359 
2360   MCSymbol *TmpLabel = Ctx.createNamedTempSymbol("pcrel_hi");
2361   Out.emitLabel(TmpLabel);
2362 
2363   const RISCVMCExpr *SymbolHi = RISCVMCExpr::create(Symbol, VKHi, Ctx);
2364   emitToStreamer(
2365       Out, MCInstBuilder(RISCV::AUIPC).addOperand(TmpReg).addExpr(SymbolHi));
2366 
2367   const MCExpr *RefToLinkTmpLabel =
2368       RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx),
2369                           RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
2370 
2371   emitToStreamer(Out, MCInstBuilder(SecondOpcode)
2372                           .addOperand(DestReg)
2373                           .addOperand(TmpReg)
2374                           .addExpr(RefToLinkTmpLabel));
2375 }
2376 
2377 void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc,
2378                                           MCStreamer &Out) {
2379   // The load local address pseudo-instruction "lla" is used in PC-relative
2380   // addressing of local symbols:
2381   //   lla rdest, symbol
2382   // expands to
2383   //   TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
2384   //             ADDI rdest, rdest, %pcrel_lo(TmpLabel)
2385   MCOperand DestReg = Inst.getOperand(0);
2386   const MCExpr *Symbol = Inst.getOperand(1).getExpr();
2387   emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
2388                     RISCV::ADDI, IDLoc, Out);
2389 }
2390 
2391 void RISCVAsmParser::emitLoadAddress(MCInst &Inst, SMLoc IDLoc,
2392                                      MCStreamer &Out) {
2393   // The load address pseudo-instruction "la" is used in PC-relative and
2394   // GOT-indirect addressing of global symbols:
2395   //   la rdest, symbol
2396   // expands to either (for non-PIC)
2397   //   TmpLabel: AUIPC rdest, %pcrel_hi(symbol)
2398   //             ADDI rdest, rdest, %pcrel_lo(TmpLabel)
2399   // or (for PIC)
2400   //   TmpLabel: AUIPC rdest, %got_pcrel_hi(symbol)
2401   //             Lx rdest, %pcrel_lo(TmpLabel)(rdest)
2402   MCOperand DestReg = Inst.getOperand(0);
2403   const MCExpr *Symbol = Inst.getOperand(1).getExpr();
2404   unsigned SecondOpcode;
2405   RISCVMCExpr::VariantKind VKHi;
2406   if (ParserOptions.IsPicEnabled) {
2407     SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
2408     VKHi = RISCVMCExpr::VK_RISCV_GOT_HI;
2409   } else {
2410     SecondOpcode = RISCV::ADDI;
2411     VKHi = RISCVMCExpr::VK_RISCV_PCREL_HI;
2412   }
2413   emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
2414 }
2415 
2416 void RISCVAsmParser::emitLoadTLSIEAddress(MCInst &Inst, SMLoc IDLoc,
2417                                           MCStreamer &Out) {
2418   // The load TLS IE address pseudo-instruction "la.tls.ie" is used in
2419   // initial-exec TLS model addressing of global symbols:
2420   //   la.tls.ie rdest, symbol
2421   // expands to
2422   //   TmpLabel: AUIPC rdest, %tls_ie_pcrel_hi(symbol)
2423   //             Lx rdest, %pcrel_lo(TmpLabel)(rdest)
2424   MCOperand DestReg = Inst.getOperand(0);
2425   const MCExpr *Symbol = Inst.getOperand(1).getExpr();
2426   unsigned SecondOpcode = isRV64() ? RISCV::LD : RISCV::LW;
2427   emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GOT_HI,
2428                     SecondOpcode, IDLoc, Out);
2429 }
2430 
2431 void RISCVAsmParser::emitLoadTLSGDAddress(MCInst &Inst, SMLoc IDLoc,
2432                                           MCStreamer &Out) {
2433   // The load TLS GD address pseudo-instruction "la.tls.gd" is used in
2434   // global-dynamic TLS model addressing of global symbols:
2435   //   la.tls.gd rdest, symbol
2436   // expands to
2437   //   TmpLabel: AUIPC rdest, %tls_gd_pcrel_hi(symbol)
2438   //             ADDI rdest, rdest, %pcrel_lo(TmpLabel)
2439   MCOperand DestReg = Inst.getOperand(0);
2440   const MCExpr *Symbol = Inst.getOperand(1).getExpr();
2441   emitAuipcInstPair(DestReg, DestReg, Symbol, RISCVMCExpr::VK_RISCV_TLS_GD_HI,
2442                     RISCV::ADDI, IDLoc, Out);
2443 }
2444 
2445 void RISCVAsmParser::emitLoadStoreSymbol(MCInst &Inst, unsigned Opcode,
2446                                          SMLoc IDLoc, MCStreamer &Out,
2447                                          bool HasTmpReg) {
2448   // The load/store pseudo-instruction does a pc-relative load with
2449   // a symbol.
2450   //
2451   // The expansion looks like this
2452   //
2453   //   TmpLabel: AUIPC tmp, %pcrel_hi(symbol)
2454   //             [S|L]X    rd, %pcrel_lo(TmpLabel)(tmp)
2455   unsigned DestRegOpIdx = HasTmpReg ? 1 : 0;
2456   MCOperand DestReg = Inst.getOperand(DestRegOpIdx);
2457   unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
2458   MCOperand TmpReg = Inst.getOperand(0);
2459   const MCExpr *Symbol = Inst.getOperand(SymbolOpIdx).getExpr();
2460   emitAuipcInstPair(DestReg, TmpReg, Symbol, RISCVMCExpr::VK_RISCV_PCREL_HI,
2461                     Opcode, IDLoc, Out);
2462 }
2463 
2464 void RISCVAsmParser::emitPseudoExtend(MCInst &Inst, bool SignExtend,
2465                                       int64_t Width, SMLoc IDLoc,
2466                                       MCStreamer &Out) {
2467   // The sign/zero extend pseudo-instruction does two shifts, with the shift
2468   // amounts dependent on the XLEN.
2469   //
2470   // The expansion looks like this
2471   //
2472   //    SLLI rd, rs, XLEN - Width
2473   //    SR[A|R]I rd, rd, XLEN - Width
2474   MCOperand DestReg = Inst.getOperand(0);
2475   MCOperand SourceReg = Inst.getOperand(1);
2476 
2477   unsigned SecondOpcode = SignExtend ? RISCV::SRAI : RISCV::SRLI;
2478   int64_t ShAmt = (isRV64() ? 64 : 32) - Width;
2479 
2480   assert(ShAmt > 0 && "Shift amount must be non-zero.");
2481 
2482   emitToStreamer(Out, MCInstBuilder(RISCV::SLLI)
2483                           .addOperand(DestReg)
2484                           .addOperand(SourceReg)
2485                           .addImm(ShAmt));
2486 
2487   emitToStreamer(Out, MCInstBuilder(SecondOpcode)
2488                           .addOperand(DestReg)
2489                           .addOperand(DestReg)
2490                           .addImm(ShAmt));
2491 }
2492 
2493 void RISCVAsmParser::emitVMSGE(MCInst &Inst, unsigned Opcode, SMLoc IDLoc,
2494                                MCStreamer &Out) {
2495   if (Inst.getNumOperands() == 3) {
2496     // unmasked va >= x
2497     //
2498     //  pseudoinstruction: vmsge{u}.vx vd, va, x
2499     //  expansion: vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd
2500     emitToStreamer(Out, MCInstBuilder(Opcode)
2501                             .addOperand(Inst.getOperand(0))
2502                             .addOperand(Inst.getOperand(1))
2503                             .addOperand(Inst.getOperand(2))
2504                             .addReg(RISCV::NoRegister));
2505     emitToStreamer(Out, MCInstBuilder(RISCV::VMNAND_MM)
2506                             .addOperand(Inst.getOperand(0))
2507                             .addOperand(Inst.getOperand(0))
2508                             .addOperand(Inst.getOperand(0)));
2509   } else if (Inst.getNumOperands() == 4) {
2510     // masked va >= x, vd != v0
2511     //
2512     //  pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t
2513     //  expansion: vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0
2514     assert(Inst.getOperand(0).getReg() != RISCV::V0 &&
2515            "The destination register should not be V0.");
2516     emitToStreamer(Out, MCInstBuilder(Opcode)
2517                             .addOperand(Inst.getOperand(0))
2518                             .addOperand(Inst.getOperand(1))
2519                             .addOperand(Inst.getOperand(2))
2520                             .addOperand(Inst.getOperand(3)));
2521     emitToStreamer(Out, MCInstBuilder(RISCV::VMXOR_MM)
2522                             .addOperand(Inst.getOperand(0))
2523                             .addOperand(Inst.getOperand(0))
2524                             .addReg(RISCV::V0));
2525   } else if (Inst.getNumOperands() == 5 &&
2526              Inst.getOperand(0).getReg() == RISCV::V0) {
2527     // masked va >= x, vd == v0
2528     //
2529     //  pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t, vt
2530     //  expansion: vmslt{u}.vx vt, va, x;  vmandn.mm vd, vd, vt
2531     assert(Inst.getOperand(0).getReg() == RISCV::V0 &&
2532            "The destination register should be V0.");
2533     assert(Inst.getOperand(1).getReg() != RISCV::V0 &&
2534            "The temporary vector register should not be V0.");
2535     emitToStreamer(Out, MCInstBuilder(Opcode)
2536                             .addOperand(Inst.getOperand(1))
2537                             .addOperand(Inst.getOperand(2))
2538                             .addOperand(Inst.getOperand(3))
2539                             .addOperand(Inst.getOperand(4)));
2540     emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
2541                             .addOperand(Inst.getOperand(0))
2542                             .addOperand(Inst.getOperand(0))
2543                             .addOperand(Inst.getOperand(1)));
2544   } else if (Inst.getNumOperands() == 5) {
2545     // masked va >= x, any vd
2546     //
2547     // pseudoinstruction: vmsge{u}.vx vd, va, x, v0.t, vt
2548     // expansion: vmslt{u}.vx vt, va, x; vmandn.mm vt, v0, vt; vmandn.mm vd,
2549     // vd, v0; vmor.mm vd, vt, vd
2550     assert(Inst.getOperand(1).getReg() != RISCV::V0 &&
2551            "The temporary vector register should not be V0.");
2552     emitToStreamer(Out, MCInstBuilder(Opcode)
2553                             .addOperand(Inst.getOperand(1))
2554                             .addOperand(Inst.getOperand(2))
2555                             .addOperand(Inst.getOperand(3))
2556                             .addReg(RISCV::NoRegister));
2557     emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
2558                             .addOperand(Inst.getOperand(1))
2559                             .addReg(RISCV::V0)
2560                             .addOperand(Inst.getOperand(1)));
2561     emitToStreamer(Out, MCInstBuilder(RISCV::VMANDN_MM)
2562                             .addOperand(Inst.getOperand(0))
2563                             .addOperand(Inst.getOperand(0))
2564                             .addReg(RISCV::V0));
2565     emitToStreamer(Out, MCInstBuilder(RISCV::VMOR_MM)
2566                             .addOperand(Inst.getOperand(0))
2567                             .addOperand(Inst.getOperand(1))
2568                             .addOperand(Inst.getOperand(0)));
2569   }
2570 }
2571 
2572 bool RISCVAsmParser::checkPseudoAddTPRel(MCInst &Inst,
2573                                          OperandVector &Operands) {
2574   assert(Inst.getOpcode() == RISCV::PseudoAddTPRel && "Invalid instruction");
2575   assert(Inst.getOperand(2).isReg() && "Unexpected second operand kind");
2576   if (Inst.getOperand(2).getReg() != RISCV::X4) {
2577     SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
2578     return Error(ErrorLoc, "the second input operand must be tp/x4 when using "
2579                            "%tprel_add modifier");
2580   }
2581 
2582   return false;
2583 }
2584 
2585 std::unique_ptr<RISCVOperand> RISCVAsmParser::defaultMaskRegOp() const {
2586   return RISCVOperand::createReg(RISCV::NoRegister, llvm::SMLoc(),
2587                                  llvm::SMLoc(), isRV64());
2588 }
2589 
2590 bool RISCVAsmParser::validateInstruction(MCInst &Inst,
2591                                          OperandVector &Operands) {
2592   if (Inst.getOpcode() == RISCV::PseudoVMSGEU_VX_M_T ||
2593       Inst.getOpcode() == RISCV::PseudoVMSGE_VX_M_T) {
2594     unsigned DestReg = Inst.getOperand(0).getReg();
2595     unsigned TempReg = Inst.getOperand(1).getReg();
2596     if (DestReg == TempReg) {
2597       SMLoc Loc = Operands.back()->getStartLoc();
2598       return Error(Loc, "The temporary vector register cannot be the same as "
2599                         "the destination register.");
2600     }
2601   }
2602 
2603   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
2604   RISCVII::VConstraintType Constraints =
2605       RISCVII::getConstraint(MCID.TSFlags);
2606   if (Constraints == RISCVII::NoConstraint)
2607     return false;
2608 
2609   unsigned DestReg = Inst.getOperand(0).getReg();
2610   // Operands[1] will be the first operand, DestReg.
2611   SMLoc Loc = Operands[1]->getStartLoc();
2612   if (Constraints & RISCVII::VS2Constraint) {
2613     unsigned CheckReg = Inst.getOperand(1).getReg();
2614     if (DestReg == CheckReg)
2615       return Error(Loc, "The destination vector register group cannot overlap"
2616                         " the source vector register group.");
2617   }
2618   if ((Constraints & RISCVII::VS1Constraint) && (Inst.getOperand(2).isReg())) {
2619     unsigned CheckReg = Inst.getOperand(2).getReg();
2620     if (DestReg == CheckReg)
2621       return Error(Loc, "The destination vector register group cannot overlap"
2622                         " the source vector register group.");
2623   }
2624   if ((Constraints & RISCVII::VMConstraint) && (DestReg == RISCV::V0)) {
2625     // vadc, vsbc are special cases. These instructions have no mask register.
2626     // The destination register could not be V0.
2627     unsigned Opcode = Inst.getOpcode();
2628     if (Opcode == RISCV::VADC_VVM || Opcode == RISCV::VADC_VXM ||
2629         Opcode == RISCV::VADC_VIM || Opcode == RISCV::VSBC_VVM ||
2630         Opcode == RISCV::VSBC_VXM || Opcode == RISCV::VFMERGE_VFM ||
2631         Opcode == RISCV::VMERGE_VIM || Opcode == RISCV::VMERGE_VVM ||
2632         Opcode == RISCV::VMERGE_VXM)
2633       return Error(Loc, "The destination vector register group cannot be V0.");
2634 
2635     // Regardless masked or unmasked version, the number of operands is the
2636     // same. For example, "viota.m v0, v2" is "viota.m v0, v2, NoRegister"
2637     // actually. We need to check the last operand to ensure whether it is
2638     // masked or not.
2639     unsigned CheckReg = Inst.getOperand(Inst.getNumOperands() - 1).getReg();
2640     assert((CheckReg == RISCV::V0 || CheckReg == RISCV::NoRegister) &&
2641            "Unexpected register for mask operand");
2642 
2643     if (DestReg == CheckReg)
2644       return Error(Loc, "The destination vector register group cannot overlap"
2645                         " the mask register.");
2646   }
2647   return false;
2648 }
2649 
2650 bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
2651                                         OperandVector &Operands,
2652                                         MCStreamer &Out) {
2653   Inst.setLoc(IDLoc);
2654 
2655   switch (Inst.getOpcode()) {
2656   default:
2657     break;
2658   case RISCV::PseudoLI: {
2659     MCRegister Reg = Inst.getOperand(0).getReg();
2660     const MCOperand &Op1 = Inst.getOperand(1);
2661     if (Op1.isExpr()) {
2662       // We must have li reg, %lo(sym) or li reg, %pcrel_lo(sym) or similar.
2663       // Just convert to an addi. This allows compatibility with gas.
2664       emitToStreamer(Out, MCInstBuilder(RISCV::ADDI)
2665                               .addReg(Reg)
2666                               .addReg(RISCV::X0)
2667                               .addExpr(Op1.getExpr()));
2668       return false;
2669     }
2670     int64_t Imm = Inst.getOperand(1).getImm();
2671     // On RV32 the immediate here can either be a signed or an unsigned
2672     // 32-bit number. Sign extension has to be performed to ensure that Imm
2673     // represents the expected signed 64-bit number.
2674     if (!isRV64())
2675       Imm = SignExtend64<32>(Imm);
2676     emitLoadImm(Reg, Imm, Out);
2677     return false;
2678   }
2679   case RISCV::PseudoLLA:
2680     emitLoadLocalAddress(Inst, IDLoc, Out);
2681     return false;
2682   case RISCV::PseudoLA:
2683     emitLoadAddress(Inst, IDLoc, Out);
2684     return false;
2685   case RISCV::PseudoLA_TLS_IE:
2686     emitLoadTLSIEAddress(Inst, IDLoc, Out);
2687     return false;
2688   case RISCV::PseudoLA_TLS_GD:
2689     emitLoadTLSGDAddress(Inst, IDLoc, Out);
2690     return false;
2691   case RISCV::PseudoLB:
2692     emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out, /*HasTmpReg=*/false);
2693     return false;
2694   case RISCV::PseudoLBU:
2695     emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out, /*HasTmpReg=*/false);
2696     return false;
2697   case RISCV::PseudoLH:
2698     emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out, /*HasTmpReg=*/false);
2699     return false;
2700   case RISCV::PseudoLHU:
2701     emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out, /*HasTmpReg=*/false);
2702     return false;
2703   case RISCV::PseudoLW:
2704     emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out, /*HasTmpReg=*/false);
2705     return false;
2706   case RISCV::PseudoLWU:
2707     emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out, /*HasTmpReg=*/false);
2708     return false;
2709   case RISCV::PseudoLD:
2710     emitLoadStoreSymbol(Inst, RISCV::LD, IDLoc, Out, /*HasTmpReg=*/false);
2711     return false;
2712   case RISCV::PseudoFLH:
2713     emitLoadStoreSymbol(Inst, RISCV::FLH, IDLoc, Out, /*HasTmpReg=*/true);
2714     return false;
2715   case RISCV::PseudoFLW:
2716     emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out, /*HasTmpReg=*/true);
2717     return false;
2718   case RISCV::PseudoFLD:
2719     emitLoadStoreSymbol(Inst, RISCV::FLD, IDLoc, Out, /*HasTmpReg=*/true);
2720     return false;
2721   case RISCV::PseudoSB:
2722     emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out, /*HasTmpReg=*/true);
2723     return false;
2724   case RISCV::PseudoSH:
2725     emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out, /*HasTmpReg=*/true);
2726     return false;
2727   case RISCV::PseudoSW:
2728     emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out, /*HasTmpReg=*/true);
2729     return false;
2730   case RISCV::PseudoSD:
2731     emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out, /*HasTmpReg=*/true);
2732     return false;
2733   case RISCV::PseudoFSH:
2734     emitLoadStoreSymbol(Inst, RISCV::FSH, IDLoc, Out, /*HasTmpReg=*/true);
2735     return false;
2736   case RISCV::PseudoFSW:
2737     emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out, /*HasTmpReg=*/true);
2738     return false;
2739   case RISCV::PseudoFSD:
2740     emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out, /*HasTmpReg=*/true);
2741     return false;
2742   case RISCV::PseudoAddTPRel:
2743     if (checkPseudoAddTPRel(Inst, Operands))
2744       return true;
2745     break;
2746   case RISCV::PseudoSEXT_B:
2747     emitPseudoExtend(Inst, /*SignExtend=*/true, /*Width=*/8, IDLoc, Out);
2748     return false;
2749   case RISCV::PseudoSEXT_H:
2750     emitPseudoExtend(Inst, /*SignExtend=*/true, /*Width=*/16, IDLoc, Out);
2751     return false;
2752   case RISCV::PseudoZEXT_H:
2753     emitPseudoExtend(Inst, /*SignExtend=*/false, /*Width=*/16, IDLoc, Out);
2754     return false;
2755   case RISCV::PseudoZEXT_W:
2756     emitPseudoExtend(Inst, /*SignExtend=*/false, /*Width=*/32, IDLoc, Out);
2757     return false;
2758   case RISCV::PseudoVMSGEU_VX:
2759   case RISCV::PseudoVMSGEU_VX_M:
2760   case RISCV::PseudoVMSGEU_VX_M_T:
2761     emitVMSGE(Inst, RISCV::VMSLTU_VX, IDLoc, Out);
2762     return false;
2763   case RISCV::PseudoVMSGE_VX:
2764   case RISCV::PseudoVMSGE_VX_M:
2765   case RISCV::PseudoVMSGE_VX_M_T:
2766     emitVMSGE(Inst, RISCV::VMSLT_VX, IDLoc, Out);
2767     return false;
2768   case RISCV::PseudoVMSGE_VI:
2769   case RISCV::PseudoVMSLT_VI: {
2770     // These instructions are signed and so is immediate so we can subtract one
2771     // and change the opcode.
2772     int64_t Imm = Inst.getOperand(2).getImm();
2773     unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGE_VI ? RISCV::VMSGT_VI
2774                                                              : RISCV::VMSLE_VI;
2775     emitToStreamer(Out, MCInstBuilder(Opc)
2776                             .addOperand(Inst.getOperand(0))
2777                             .addOperand(Inst.getOperand(1))
2778                             .addImm(Imm - 1)
2779                             .addOperand(Inst.getOperand(3)));
2780     return false;
2781   }
2782   case RISCV::PseudoVMSGEU_VI:
2783   case RISCV::PseudoVMSLTU_VI: {
2784     int64_t Imm = Inst.getOperand(2).getImm();
2785     // Unsigned comparisons are tricky because the immediate is signed. If the
2786     // immediate is 0 we can't just subtract one. vmsltu.vi v0, v1, 0 is always
2787     // false, but vmsle.vi v0, v1, -1 is always true. Instead we use
2788     // vmsne v0, v1, v1 which is always false.
2789     if (Imm == 0) {
2790       unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGEU_VI
2791                          ? RISCV::VMSEQ_VV
2792                          : RISCV::VMSNE_VV;
2793       emitToStreamer(Out, MCInstBuilder(Opc)
2794                               .addOperand(Inst.getOperand(0))
2795                               .addOperand(Inst.getOperand(1))
2796                               .addOperand(Inst.getOperand(1))
2797                               .addOperand(Inst.getOperand(3)));
2798     } else {
2799       // Other immediate values can subtract one like signed.
2800       unsigned Opc = Inst.getOpcode() == RISCV::PseudoVMSGEU_VI
2801                          ? RISCV::VMSGTU_VI
2802                          : RISCV::VMSLEU_VI;
2803       emitToStreamer(Out, MCInstBuilder(Opc)
2804                               .addOperand(Inst.getOperand(0))
2805                               .addOperand(Inst.getOperand(1))
2806                               .addImm(Imm - 1)
2807                               .addOperand(Inst.getOperand(3)));
2808     }
2809 
2810     return false;
2811   }
2812   }
2813 
2814   emitToStreamer(Out, Inst);
2815   return false;
2816 }
2817 
2818 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmParser() {
2819   RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target());
2820   RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target());
2821 }
2822