41 #define DEBUG_TYPE "riscv-asm-parser" 44 #define GEN_COMPRESS_INSTR 45 #include "RISCVGenCompressInstEmitter.inc" 48 "Number of RISC-V Compressed instructions emitted");
56 SMLoc getLoc()
const {
return getParser().getTok().getLoc(); }
57 bool isRV64()
const {
return getSTI().hasFeature(RISCV::Feature64Bit); }
58 bool isRV32E()
const {
return getSTI().hasFeature(RISCV::FeatureRV32E); }
66 unsigned Kind)
override;
71 bool MatchAndEmitInstruction(
SMLoc IDLoc,
unsigned &Opcode,
74 bool MatchingInlineAsm)
override;
76 bool ParseRegister(
unsigned &RegNo,
SMLoc &StartLoc,
SMLoc &EndLoc)
override;
81 bool ParseDirective(
AsmToken DirectiveID)
override;
112 void emitLoadStoreSymbol(
MCInst &Inst,
unsigned Opcode,
SMLoc IDLoc,
129 #define GET_ASSEMBLER_HEADER 130 #include "RISCVGenAsmMatcher.inc" 135 bool AllowParens =
false);
145 bool parseDirectiveOption();
147 void setFeatureBits(uint64_t Feature,
StringRef FeatureString) {
148 if (!(getSTI().getFeatureBits()[Feature])) {
150 setAvailableFeatures(
155 void clearFeatureBits(uint64_t Feature,
StringRef FeatureString) {
156 if (getSTI().getFeatureBits()[Feature]) {
158 setAvailableFeatures(
163 void pushFeatureBits() {
164 FeatureBitStack.
push_back(getSTI().getFeatureBits());
167 bool popFeatureBits() {
168 if (FeatureBitStack.
empty())
172 copySTI().setFeatureBits(FeatureBits);
173 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
178 enum RISCVMatchResultTy {
179 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
180 #define GET_OPERAND_DIAGNOSTIC_TYPES 181 #include "RISCVGenAsmMatcher.inc" 182 #undef GET_OPERAND_DIAGNOSTIC_TYPES 185 static bool classifySymbolRef(
const MCExpr *Expr,
196 setAvailableFeatures(ComputeAvailableFeatures(STI.
getFeatureBits()));
199 if (ABIName.endswith(
"f") &&
200 !getSTI().getFeatureBits()[RISCV::FeatureStdExtF]) {
201 errs() <<
"Hard-float 'f' ABI can't be used for a target that " 202 "doesn't support the F instruction set extension (ignoring " 204 }
else if (ABIName.endswith(
"d") &&
205 !getSTI().getFeatureBits()[RISCV::FeatureStdExtD]) {
206 errs() <<
"Hard-float 'd' ABI can't be used for a target that " 207 "doesn't support the D instruction set extension (ignoring " 242 SMLoc StartLoc, EndLoc;
247 struct SysRegOp SysReg;
256 StartLoc = o.StartLoc;
262 case KindTy::Immediate:
268 case KindTy::SystemRegister:
274 bool isToken()
const override {
return Kind == KindTy::Token; }
276 bool isImm()
const override {
return Kind == KindTy::Immediate; }
277 bool isMem()
const override {
return false; }
278 bool isSystemRegister()
const {
return Kind == KindTy::SystemRegister; }
282 RISCVMCRegisterClasses[RISCV::GPRRegClassID].contains(
Reg.RegNum);
285 static bool evaluateConstantImm(
const MCExpr *Expr, int64_t &Imm,
287 if (
auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
289 return RE->evaluateAsConstant(Imm);
292 if (
auto CE = dyn_cast<MCConstantExpr>(Expr)) {
294 Imm = CE->getValue();
303 template <
int N>
bool isBareSimmNLsb0()
const {
308 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
311 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
319 bool isBareSymbol()
const {
323 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
325 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
329 bool isCallSymbol()
const {
333 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
335 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
340 bool isTPRelAddSymbol()
const {
344 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
346 return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
350 bool isCSRSystemRegister()
const {
return isSystemRegister(); }
354 bool isFenceArg()
const {
357 const MCExpr *Val = getImm();
362 StringRef Str = SVal->getSymbol().getName();
368 if (c !=
'i' && c !=
'o' && c !=
'r' && c !=
'w')
378 bool isFRMArg()
const {
381 const MCExpr *Val = getImm();
386 StringRef Str = SVal->getSymbol().getName();
391 bool isImmXLenLI()
const {
396 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
405 bool isUImmLog2XLen()
const {
410 if (!evaluateConstantImm(getImm(), Imm, VK) ||
413 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
416 bool isUImmLog2XLenNonZero()
const {
421 if (!evaluateConstantImm(getImm(), Imm, VK) ||
426 return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
429 bool isUImm5()
const {
434 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
438 bool isUImm5NonZero()
const {
443 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
444 return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
448 bool isSImm6()
const {
453 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
454 return IsConstantImm && isInt<6>(Imm) &&
458 bool isSImm6NonZero()
const {
463 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
464 return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
468 bool isCLUIImm()
const {
473 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
474 return IsConstantImm && (Imm != 0) &&
475 (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
479 bool isUImm7Lsb00()
const {
484 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
485 return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
489 bool isUImm8Lsb00()
const {
494 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
495 return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
499 bool isUImm8Lsb000()
const {
504 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
505 return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
509 bool isSImm9Lsb0()
const {
return isBareSimmNLsb0<9>(); }
511 bool isUImm9Lsb000()
const {
516 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
517 return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
521 bool isUImm10Lsb00NonZero()
const {
526 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
527 return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
531 bool isSImm12()
const {
537 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
539 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
541 IsValid = isInt<12>(Imm);
548 bool isSImm12Lsb0()
const {
return isBareSimmNLsb0<12>(); }
550 bool isSImm13Lsb0()
const {
return isBareSimmNLsb0<13>(); }
552 bool isSImm10Lsb0000NonZero()
const {
557 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
558 return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
562 bool isUImm20LUI()
const {
568 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
569 if (!IsConstantImm) {
570 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
580 bool isUImm20AUIPC()
const {
586 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
587 if (!IsConstantImm) {
588 IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
602 bool isSImm21Lsb0JAL()
const {
return isBareSimmNLsb0<21>(); }
604 bool isImmZero()
const {
609 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
614 SMLoc getStartLoc()
const override {
return StartLoc; }
616 SMLoc getEndLoc()
const override {
return EndLoc; }
618 bool isRV64()
const {
return IsRV64; }
620 unsigned getReg()
const override {
622 return Reg.RegNum.id();
626 assert(Kind == KindTy::SystemRegister &&
"Invalid access!");
627 return StringRef(SysReg.Data, SysReg.Length);
630 const MCExpr *getImm()
const {
631 assert(Kind == KindTy::Immediate &&
"Invalid type access!");
636 assert(Kind == KindTy::Token &&
"Invalid type access!");
642 case KindTy::Immediate:
652 case KindTy::SystemRegister:
653 OS <<
"<sysreg: " << getSysReg() <<
'>';
658 static std::unique_ptr<RISCVOperand> createToken(
StringRef Str,
SMLoc S,
660 auto Op = std::make_unique<RISCVOperand>(KindTy::Token);
668 static std::unique_ptr<RISCVOperand> createReg(
unsigned RegNo,
SMLoc S,
671 Op->Reg.RegNum = RegNo;
678 static std::unique_ptr<RISCVOperand> createImm(
const MCExpr *Val,
SMLoc S,
680 auto Op = std::make_unique<RISCVOperand>(KindTy::Immediate);
688 static std::unique_ptr<RISCVOperand>
689 createSysReg(
StringRef Str,
SMLoc S,
unsigned Encoding,
bool IsRV64) {
690 auto Op = std::make_unique<RISCVOperand>(KindTy::SystemRegister);
691 Op->SysReg.Data = Str.
data();
692 Op->SysReg.Length = Str.
size();
693 Op->SysReg.Encoding = Encoding;
700 assert(Expr &&
"Expr shouldn't be null!");
703 bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
712 void addRegOperands(
MCInst &Inst,
unsigned N)
const {
713 assert(N == 1 &&
"Invalid number of operands!");
717 void addImmOperands(
MCInst &Inst,
unsigned N)
const {
718 assert(N == 1 &&
"Invalid number of operands!");
719 addExpr(Inst, getImm());
722 void addFenceArgOperands(
MCInst &Inst,
unsigned N)
const {
723 assert(N == 1 &&
"Invalid number of operands!");
725 auto SE = cast<MCSymbolRefExpr>(getImm());
728 for (
char c : SE->getSymbol().getName()) {
741 void addCSRSystemRegisterOperands(
MCInst &Inst,
unsigned N)
const {
742 assert(N == 1 &&
"Invalid number of operands!");
750 auto SE = cast<MCSymbolRefExpr>(getImm());
757 void addFRMArgOperands(
MCInst &Inst,
unsigned N)
const {
758 assert(N == 1 &&
"Invalid number of operands!");
764 #define GET_REGISTER_MATCHER 765 #define GET_SUBTARGET_FEATURE_NAME 766 #define GET_MATCHER_IMPLEMENTATION 767 #define GET_MNEMONIC_SPELL_CHECKER 768 #include "RISCVGenAsmMatcher.inc" 771 assert(Reg >= RISCV::F0_D && Reg <= RISCV::F31_D &&
"Invalid register");
772 return Reg - RISCV::F0_D + RISCV::F0_F;
777 RISCVOperand &
Op =
static_cast<RISCVOperand &
>(AsmOp);
779 return Match_InvalidOperand;
783 RISCVMCRegisterClasses[RISCV::FPR64RegClassID].contains(Reg);
785 RISCVMCRegisterClasses[RISCV::FPR64CRegClassID].contains(Reg);
789 if ((IsRegFPR64 && Kind == MCK_FPR32) ||
790 (IsRegFPR64C && Kind == MCK_FPR32C)) {
792 return Match_Success;
794 return Match_InvalidOperand;
797 bool RISCVAsmParser::generateImmOutOfRangeError(
799 Twine Msg =
"immediate must be an integer in the range") {
800 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
801 return Error(ErrorLoc, Msg +
" [" +
Twine(Lower) +
", " +
Twine(Upper) +
"]");
806 unsigned VariantID = 0);
808 bool RISCVAsmParser::MatchAndEmitInstruction(
SMLoc IDLoc,
unsigned &Opcode,
812 bool MatchingInlineAsm) {
817 MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,
823 return processInstruction(Inst, IDLoc, Operands, Out);
824 case Match_MissingFeature: {
825 assert(MissingFeatures.
any() &&
"Unknown missing features!");
826 bool FirstFeature =
true;
827 std::string Msg =
"instruction requires the following:";
828 for (
unsigned i = 0,
e = MissingFeatures.
size(); i !=
e; ++i) {
829 if (MissingFeatures[i]) {
830 Msg += FirstFeature ?
" " :
", ";
832 FirstFeature =
false;
835 return Error(IDLoc, Msg);
837 case Match_MnemonicFail: {
838 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
840 ((RISCVOperand &)*Operands[0]).
getToken(), FBS);
841 return Error(IDLoc,
"unrecognized instruction mnemonic" + Suggestion);
843 case Match_InvalidOperand: {
844 SMLoc ErrorLoc = IDLoc;
845 if (ErrorInfo != ~0U) {
846 if (ErrorInfo >= Operands.
size())
847 return Error(ErrorLoc,
"too few operands for instruction");
849 ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
850 if (ErrorLoc ==
SMLoc())
853 return Error(ErrorLoc,
"invalid operand for instruction");
860 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
861 SMLoc ErrorLoc = IDLoc;
862 if (ErrorInfo != ~0U && ErrorInfo >= Operands.
size())
863 return Error(ErrorLoc,
"too few operands for instruction");
869 case Match_InvalidImmXLenLI:
871 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
872 return Error(ErrorLoc,
"operand must be a constant 64-bit integer");
874 return generateImmOutOfRangeError(Operands, ErrorInfo,
875 std::numeric_limits<int32_t>::min(),
877 case Match_InvalidImmZero: {
878 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
879 return Error(ErrorLoc,
"immediate must be zero");
881 case Match_InvalidUImmLog2XLen:
883 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
884 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
885 case Match_InvalidUImmLog2XLenNonZero:
887 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1);
888 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1);
889 case Match_InvalidUImm5:
890 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
891 case Match_InvalidSImm6:
892 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5),
894 case Match_InvalidSImm6NonZero:
895 return generateImmOutOfRangeError(
896 Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1,
897 "immediate must be non-zero in the range");
898 case Match_InvalidCLUIImm:
899 return generateImmOutOfRangeError(
900 Operands, ErrorInfo, 1, (1 << 5) - 1,
901 "immediate must be in [0xfffe0, 0xfffff] or");
902 case Match_InvalidUImm7Lsb00:
903 return generateImmOutOfRangeError(
904 Operands, ErrorInfo, 0, (1 << 7) - 4,
905 "immediate must be a multiple of 4 bytes in the range");
906 case Match_InvalidUImm8Lsb00:
907 return generateImmOutOfRangeError(
908 Operands, ErrorInfo, 0, (1 << 8) - 4,
909 "immediate must be a multiple of 4 bytes in the range");
910 case Match_InvalidUImm8Lsb000:
911 return generateImmOutOfRangeError(
912 Operands, ErrorInfo, 0, (1 << 8) - 8,
913 "immediate must be a multiple of 8 bytes in the range");
914 case Match_InvalidSImm9Lsb0:
915 return generateImmOutOfRangeError(
916 Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2,
917 "immediate must be a multiple of 2 bytes in the range");
918 case Match_InvalidUImm9Lsb000:
919 return generateImmOutOfRangeError(
920 Operands, ErrorInfo, 0, (1 << 9) - 8,
921 "immediate must be a multiple of 8 bytes in the range");
922 case Match_InvalidUImm10Lsb00NonZero:
923 return generateImmOutOfRangeError(
924 Operands, ErrorInfo, 4, (1 << 10) - 4,
925 "immediate must be a multiple of 4 bytes in the range");
926 case Match_InvalidSImm10Lsb0000NonZero:
927 return generateImmOutOfRangeError(
928 Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16,
929 "immediate must be a multiple of 16 bytes and non-zero in the range");
930 case Match_InvalidSImm12:
931 return generateImmOutOfRangeError(
932 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1,
933 "operand must be a symbol with %lo/%pcrel_lo/%tprel_lo modifier or an " 934 "integer in the range");
935 case Match_InvalidSImm12Lsb0:
936 return generateImmOutOfRangeError(
937 Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2,
938 "immediate must be a multiple of 2 bytes in the range");
939 case Match_InvalidSImm13Lsb0:
940 return generateImmOutOfRangeError(
941 Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,
942 "immediate must be a multiple of 2 bytes in the range");
943 case Match_InvalidUImm20LUI:
944 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1,
945 "operand must be a symbol with " 946 "%hi/%tprel_hi modifier or an integer in " 948 case Match_InvalidUImm20AUIPC:
949 return generateImmOutOfRangeError(
950 Operands, ErrorInfo, 0, (1 << 20) - 1,
951 "operand must be a symbol with a " 952 "%pcrel_hi/%got_pcrel_hi/%tls_ie_pcrel_hi/%tls_gd_pcrel_hi modifier or " 953 "an integer in the range");
954 case Match_InvalidSImm21Lsb0JAL:
955 return generateImmOutOfRangeError(
956 Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2,
957 "immediate must be a multiple of 2 bytes in the range");
958 case Match_InvalidCSRSystemRegister: {
959 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1,
960 "operand must be a valid system register " 961 "name or an integer in the range");
963 case Match_InvalidFenceArg: {
964 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
967 "operand must be formed of letters selected in-order from 'iorw'");
969 case Match_InvalidFRMArg: {
970 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
973 "operand must be a valid floating point rounding mode mnemonic");
975 case Match_InvalidBareSymbol: {
976 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
977 return Error(ErrorLoc,
"operand must be a bare symbol name");
979 case Match_InvalidCallSymbol: {
980 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
981 return Error(ErrorLoc,
"operand must be a bare symbol name");
983 case Match_InvalidTPRelAddSymbol: {
984 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc();
985 return Error(ErrorLoc,
"operand must be a symbol with %tprel_add modifier");
1001 assert(!(RegNo >= RISCV::F0_F && RegNo <= RISCV::F31_F));
1003 static_assert(RISCV::F0_D < RISCV::F0_F,
"FPR matching must be updated");
1004 if (RegNo == RISCV::NoRegister)
1006 if (IsRV32E && RegNo >= RISCV::X16 && RegNo <= RISCV::X31)
1007 RegNo = RISCV::NoRegister;
1008 return RegNo == RISCV::NoRegister;
1011 bool RISCVAsmParser::ParseRegister(
unsigned &RegNo,
SMLoc &StartLoc,
1013 const AsmToken &Tok = getParser().getTok();
1020 return Error(StartLoc,
"invalid register name");
1028 SMLoc FirstS = getLoc();
1029 bool HadParens =
false;
1036 size_t ReadCount = getLexer().peekTokens(Buf);
1039 LParen = getParser().getTok();
1044 switch (getLexer().getKind()) {
1047 getLexer().UnLex(LParen);
1054 if (RegNo == RISCV::NoRegister) {
1056 getLexer().UnLex(LParen);
1060 Operands.
push_back(RISCVOperand::createToken(
"(", FirstS, isRV64()));
1064 Operands.
push_back(RISCVOperand::createReg(RegNo, S, E, isRV64()));
1069 Operands.
push_back(RISCVOperand::createToken(
")", getLoc(), isRV64()));
1076 RISCVAsmParser::parseCSRSystemRegister(
OperandVector &Operands) {
1080 switch (getLexer().getKind()) {
1090 if (getParser().parseExpression(Res))
1095 int64_t Imm = CE->getValue();
1096 if (isUInt<12>(Imm)) {
1100 Operands.
push_back(RISCVOperand::createSysReg(
1101 SysReg ? SysReg->Name :
"", S, Imm, isRV64()));
1106 Twine Msg =
"immediate must be an integer in the range";
1112 if (getParser().parseIdentifier(Identifier))
1118 if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) {
1119 Error(S,
"system register use requires an option to be enabled");
1122 Operands.
push_back(RISCVOperand::createSysReg(
1123 Identifier, S, SysReg->Encoding, isRV64()));
1127 Twine Msg =
"operand must be a valid system register name " 1128 "or an integer in the range";
1134 Twine Msg =
"immediate must be an integer in the range";
1148 switch (getLexer().getKind()) {
1160 if (getParser().parseExpression(Res))
1164 return parseOperandWithModifier(Operands);
1167 Operands.
push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1172 RISCVAsmParser::parseOperandWithModifier(
OperandVector &Operands) {
1177 Error(getLoc(),
"expected '%' for operand modifier");
1184 Error(getLoc(),
"expected valid identifier for operand modifier");
1190 Error(getLoc(),
"unrecognized operand modifier");
1196 Error(getLoc(),
"expected '('");
1202 if (getParser().parseParenExpression(SubExpr, E)) {
1207 Operands.
push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64()));
1220 AsmToken Tok = getLexer().getTok();
1222 if (getParser().parseIdentifier(Identifier))
1226 Error(getLoc(),
"'@plt' operand not valid for instruction");
1230 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1234 if (!isa<MCSymbolRefExpr>(V)) {
1235 getLexer().UnLex(Tok);
1243 switch (getLexer().getKind()) {
1245 Operands.
push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1256 if (getParser().parseExpression(Expr))
1259 Operands.
push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1276 if (getParser().parseIdentifier(Identifier))
1283 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
1286 Operands.
push_back(RISCVOperand::createImm(Res, S, E, isRV64()));
1308 RISCVAsmParser::parseMemOpBaseReg(
OperandVector &Operands) {
1310 Error(getLoc(),
"expected '('");
1315 Operands.
push_back(RISCVOperand::createToken(
"(", getLoc(), isRV64()));
1318 Error(getLoc(),
"expected register");
1323 Error(getLoc(),
"expected ')'");
1328 Operands.
push_back(RISCVOperand::createToken(
")", getLoc(), isRV64()));
1352 std::unique_ptr<RISCVOperand> OptionalImmOp;
1359 SMLoc ImmStart = getLoc();
1360 if (getParser().parseIntToken(ImmVal,
1361 "expected '(' or optional integer offset"))
1366 SMLoc ImmEnd = getLoc();
1369 ImmStart, ImmEnd, isRV64());
1373 Error(getLoc(), OptionalImmOp ?
"expected '(' after optional integer offset" 1374 :
"expected '(' or optional integer offset");
1380 Error(getLoc(),
"expected register");
1385 Error(getLoc(),
"expected ')'");
1391 if (OptionalImmOp && !OptionalImmOp->isImmZero()) {
1392 Error(OptionalImmOp->getStartLoc(),
"optional integer offset must be 0",
1393 SMRange(OptionalImmOp->getStartLoc(), OptionalImmOp->getEndLoc()));
1407 MatchOperandParserImpl(Operands, Mnemonic,
true);
1426 Error(getLoc(),
"unknown operand");
1439 if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) {
1440 auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr();
1441 if (Assembler !=
nullptr) {
1449 Operands.
push_back(RISCVOperand::createToken(Name, NameLoc, isRV64()));
1456 if (parseOperand(Operands, Name))
1460 unsigned OperandIdx = 1;
1466 if (parseOperand(Operands, Name))
1473 SMLoc Loc = getLexer().getLoc();
1474 getParser().eatToEndOfStatement();
1475 return Error(Loc,
"unexpected token");
1482 bool RISCVAsmParser::classifySymbolRef(
const MCExpr *Expr,
1488 if (
const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) {
1489 Kind = RE->getKind();
1490 Expr = RE->getSubExpr();
1494 if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr))
1501 if (!isa<MCSymbolRefExpr>(BE->
getLHS()))
1510 isa<MCSymbolRefExpr>(BE->
getRHS()))
1527 bool RISCVAsmParser::ParseDirective(
AsmToken DirectiveID) {
1534 if (IDVal ==
".option")
1535 return parseDirectiveOption();
1540 bool RISCVAsmParser::parseDirectiveOption() {
1547 "unexpected token, expected identifier");
1551 if (Option ==
"push") {
1552 getTargetStreamer().emitDirectiveOptionPush();
1557 "unexpected token, expected end of statement");
1563 if (Option ==
"pop") {
1565 getTargetStreamer().emitDirectiveOptionPop();
1570 "unexpected token, expected end of statement");
1572 if (popFeatureBits())
1573 return Error(StartLoc,
".option pop with no .option push");
1578 if (Option ==
"rvc") {
1579 getTargetStreamer().emitDirectiveOptionRVC();
1584 "unexpected token, expected end of statement");
1586 setFeatureBits(RISCV::FeatureStdExtC,
"c");
1590 if (Option ==
"norvc") {
1591 getTargetStreamer().emitDirectiveOptionNoRVC();
1596 "unexpected token, expected end of statement");
1598 clearFeatureBits(RISCV::FeatureStdExtC,
"c");
1602 if (Option ==
"relax") {
1603 getTargetStreamer().emitDirectiveOptionRelax();
1608 "unexpected token, expected end of statement");
1610 setFeatureBits(RISCV::FeatureRelax,
"relax");
1614 if (Option ==
"norelax") {
1615 getTargetStreamer().emitDirectiveOptionNoRelax();
1620 "unexpected token, expected end of statement");
1622 clearFeatureBits(RISCV::FeatureRelax,
"relax");
1628 "unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or " 1636 bool Res = compressInst(CInst, Inst, getSTI(), S.
getContext());
1638 ++RISCVNumInstrsCompressed;
1642 void RISCVAsmParser::emitLoadImm(
Register DestReg, int64_t
Value,
1649 if (Inst.Opc == RISCV::LUI) {
1651 Out,
MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm));
1666 unsigned SecondOpcode,
SMLoc IDLoc,
1674 "pcrel_hi",
true,
false);
1681 const MCExpr *RefToLinkTmpLabel =
1688 .addExpr(RefToLinkTmpLabel));
1691 void RISCVAsmParser::emitLoadLocalAddress(
MCInst &Inst,
SMLoc IDLoc,
1702 RISCV::ADDI, IDLoc, Out);
1705 void RISCVAsmParser::emitLoadAddress(
MCInst &Inst,
SMLoc IDLoc,
1718 unsigned SecondOpcode;
1721 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1722 SecondOpcode = isRV64() ?
RISCV::LD : RISCV::LW;
1725 SecondOpcode = RISCV::ADDI;
1728 emitAuipcInstPair(DestReg, DestReg, Symbol, VKHi, SecondOpcode, IDLoc, Out);
1731 void RISCVAsmParser::emitLoadTLSIEAddress(
MCInst &Inst,
SMLoc IDLoc,
1741 unsigned SecondOpcode = isRV64() ?
RISCV::LD : RISCV::LW;
1743 SecondOpcode, IDLoc, Out);
1746 void RISCVAsmParser::emitLoadTLSGDAddress(
MCInst &Inst,
SMLoc IDLoc,
1757 RISCV::ADDI, IDLoc, Out);
1760 void RISCVAsmParser::emitLoadStoreSymbol(
MCInst &Inst,
unsigned Opcode,
1771 unsigned SymbolOpIdx = HasTmpReg ? 2 : 1;
1772 unsigned TmpRegOpIdx = HasTmpReg ? 1 : 0;
1776 Opcode, IDLoc, Out);
1779 bool RISCVAsmParser::checkPseudoAddTPRel(
MCInst &Inst,
1781 assert(Inst.
getOpcode() == RISCV::PseudoAddTPRel &&
"Invalid instruction");
1784 SMLoc ErrorLoc = ((RISCVOperand &)*Operands[3]).getStartLoc();
1785 return Error(ErrorLoc,
"the second input operand must be tp/x4 when using " 1786 "%tprel_add modifier");
1792 bool RISCVAsmParser::processInstruction(
MCInst &Inst,
SMLoc IDLoc,
1800 case RISCV::PseudoLI: {
1817 Imm = SignExtend64<32>(Imm);
1818 emitLoadImm(Reg, Imm, Out);
1821 case RISCV::PseudoLLA:
1822 emitLoadLocalAddress(Inst, IDLoc, Out);
1824 case RISCV::PseudoLA:
1825 emitLoadAddress(Inst, IDLoc, Out);
1827 case RISCV::PseudoLA_TLS_IE:
1828 emitLoadTLSIEAddress(Inst, IDLoc, Out);
1830 case RISCV::PseudoLA_TLS_GD:
1831 emitLoadTLSGDAddress(Inst, IDLoc, Out);
1833 case RISCV::PseudoLB:
1834 emitLoadStoreSymbol(Inst, RISCV::LB, IDLoc, Out,
false);
1836 case RISCV::PseudoLBU:
1837 emitLoadStoreSymbol(Inst, RISCV::LBU, IDLoc, Out,
false);
1839 case RISCV::PseudoLH:
1840 emitLoadStoreSymbol(Inst, RISCV::LH, IDLoc, Out,
false);
1842 case RISCV::PseudoLHU:
1843 emitLoadStoreSymbol(Inst, RISCV::LHU, IDLoc, Out,
false);
1845 case RISCV::PseudoLW:
1846 emitLoadStoreSymbol(Inst, RISCV::LW, IDLoc, Out,
false);
1848 case RISCV::PseudoLWU:
1849 emitLoadStoreSymbol(Inst, RISCV::LWU, IDLoc, Out,
false);
1851 case RISCV::PseudoLD:
1852 emitLoadStoreSymbol(Inst,
RISCV::LD, IDLoc, Out,
false);
1854 case RISCV::PseudoFLW:
1855 emitLoadStoreSymbol(Inst, RISCV::FLW, IDLoc, Out,
true);
1857 case RISCV::PseudoFLD:
1858 emitLoadStoreSymbol(Inst,
RISCV::FLD, IDLoc, Out,
true);
1860 case RISCV::PseudoSB:
1861 emitLoadStoreSymbol(Inst, RISCV::SB, IDLoc, Out,
true);
1863 case RISCV::PseudoSH:
1864 emitLoadStoreSymbol(Inst, RISCV::SH, IDLoc, Out,
true);
1866 case RISCV::PseudoSW:
1867 emitLoadStoreSymbol(Inst, RISCV::SW, IDLoc, Out,
true);
1869 case RISCV::PseudoSD:
1870 emitLoadStoreSymbol(Inst, RISCV::SD, IDLoc, Out,
true);
1872 case RISCV::PseudoFSW:
1873 emitLoadStoreSymbol(Inst, RISCV::FSW, IDLoc, Out,
true);
1875 case RISCV::PseudoFSD:
1876 emitLoadStoreSymbol(Inst, RISCV::FSD, IDLoc, Out,
true);
1878 case RISCV::PseudoAddTPRel:
1879 if (checkPseudoAddTPRel(Inst, Operands))
1884 emitToStreamer(Out, Inst);
static bool isReg(const MCInst &MI, unsigned OpNo)
constexpr bool isUInt< 32 >(uint64_t x)
Represents a range in source code.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
This class represents lattice values for constants.
bool isVariable() const
isVariable - Check if this is a variable symbol.
static unsigned MatchRegisterName(StringRef Name)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Generic assembler parser interface, for use by target specific assembly parsers.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static MCOperand createExpr(const MCExpr *Val)
MCTargetAsmParser - Generic interface to target specific assembly parsers.
void push_back(const T &Elt)
static RoundingMode stringToRoundingMode(StringRef Str)
constexpr size_t size() const
Target specific streamer interface.
bool isNot(TokenKind K) const
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
STATISTIC(NumFunctions, "Total number of functions")
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
static Register convertFPR64ToFPR32(Register Reg)
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
MCContext & getContext() const
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string...
static MCOperand createReg(unsigned Reg)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
const FeatureBitset & getFeatureBits() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Base class for the full range of assembler expressions which are needed for parsing.
Target independent representation for an assembler token.
Represent a reference to a symbol from inside an expression.
Target & getTheRISCV32Target()
mir Rename Register Operands
static bool isMem(const MachineInstr &MI, unsigned Op)
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand...
unsigned getReg() const
Returns the register number.
Context object for machine code objects.
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
RegisterMCAsmParser - Helper template for registering a target specific assembly parser, for use in the target machine initialization function.
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
LLVM_NODISCARD size_t size() const
size - Get the string size.
const MCExpr * getExpr() const
Instances of this class represent a single low-level machine instruction.
Analysis containing CSE Info
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
FeatureBitset ToggleFeature(uint64_t FB)
Toggle a feature and return the re-computed feature bits.
const char * getPointer() const
Streaming machine code generation interface.
MCInstBuilder & addReg(unsigned Reg)
Add a new register operand.
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
MCTargetStreamer * getTargetStreamer()
Container class for subtarget features.
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
This instruction implements an extending load to FP stack slots.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Target & getTheRISCV64Target()
Interface to description of machine instruction set.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Binary assembler expressions.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
static std::string RISCVMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS, unsigned VariantID=0)
MCStreamer & getStreamer()
constexpr bool isInt< 32 >(int64_t x)
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const MCOperand & getOperand(unsigned i) const
Promote Memory to Register
LLVM_NODISCARD T pop_back_val()
#define LLVM_EXTERNAL_VISIBILITY
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
const SysReg * lookupSysRegByName(StringRef)
Base class for user error types.
static SMLoc getFromPointer(const char *Ptr)
static VariantKind getVariantKindForName(StringRef name)
void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res)
LLVM_NODISCARD bool empty() const
Generic base class for all target subtargets.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Opcode getOpcode() const
Get the kind of this binary expression.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmParser()
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
LLVM Value Representation.
const SysReg * lookupSysRegByEncoding(uint16_t)
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
This class implements an extremely fast bulk output stream that can only output to a stream...
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
Represents a location in source code.
static const char * getSubtargetFeatureName(uint64_t Val)
unsigned getOpcode() const
Instances of this class represent operands of the MCInst class.
static MCOperand createImm(int64_t Val)
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Wrapper class representing virtual and physical registers.
static bool matchRegisterNameHelper(bool IsRV32E, Register &RegNo, StringRef Name)
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)