Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1 | //===-- RISCVAsmParser.cpp - Parse RISCV assembly to MCInst instructions --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/RISCVBaseInfo.h" |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/RISCVMCExpr.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 12 | #include "MCTargetDesc/RISCVMCTargetDesc.h" |
Alex Bradbury | 4f7f0da | 2017-09-06 09:21:21 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
| 14 | #include "llvm/ADT/StringSwitch.h" |
| 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCInst.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 19 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 20 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCRegisterInfo.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
| 23 | #include "llvm/MC/MCSubtargetInfo.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Casting.h" |
| 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | struct RISCVOperand; |
| 31 | |
| 32 | class RISCVAsmParser : public MCTargetAsmParser { |
| 33 | SMLoc getLoc() const { return getParser().getTok().getLoc(); } |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 34 | bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 35 | |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 36 | unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, |
| 37 | unsigned Kind) override; |
| 38 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 39 | bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo, |
| 40 | int Lower, int Upper, Twine Msg); |
| 41 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 42 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
| 43 | OperandVector &Operands, MCStreamer &Out, |
| 44 | uint64_t &ErrorInfo, |
| 45 | bool MatchingInlineAsm) override; |
| 46 | |
| 47 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; |
| 48 | |
| 49 | bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, |
| 50 | SMLoc NameLoc, OperandVector &Operands) override; |
| 51 | |
| 52 | bool ParseDirective(AsmToken DirectiveID) override; |
| 53 | |
| 54 | // Auto-generated instruction matching functions |
| 55 | #define GET_ASSEMBLER_HEADER |
| 56 | #include "RISCVGenAsmMatcher.inc" |
| 57 | |
| 58 | OperandMatchResultTy parseImmediate(OperandVector &Operands); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 59 | OperandMatchResultTy parseRegister(OperandVector &Operands, |
| 60 | bool AllowParens = false); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 61 | OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 62 | OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 63 | |
| 64 | bool parseOperand(OperandVector &Operands); |
| 65 | |
| 66 | public: |
| 67 | enum RISCVMatchResultTy { |
| 68 | Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY, |
| 69 | #define GET_OPERAND_DIAGNOSTIC_TYPES |
| 70 | #include "RISCVGenAsmMatcher.inc" |
| 71 | #undef GET_OPERAND_DIAGNOSTIC_TYPES |
| 72 | }; |
| 73 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 74 | static bool classifySymbolRef(const MCExpr *Expr, |
| 75 | RISCVMCExpr::VariantKind &Kind, |
| 76 | int64_t &Addend); |
| 77 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 78 | RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, |
| 79 | const MCInstrInfo &MII, const MCTargetOptions &Options) |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 80 | : MCTargetAsmParser(Options, STI, MII) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 81 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | /// RISCVOperand - Instances of this class represent a parsed machine |
| 86 | /// instruction |
| 87 | struct RISCVOperand : public MCParsedAsmOperand { |
| 88 | |
| 89 | enum KindTy { |
| 90 | Token, |
| 91 | Register, |
| 92 | Immediate, |
| 93 | } Kind; |
| 94 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 95 | bool IsRV64; |
| 96 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 97 | struct RegOp { |
| 98 | unsigned RegNum; |
| 99 | }; |
| 100 | |
| 101 | struct ImmOp { |
| 102 | const MCExpr *Val; |
| 103 | }; |
| 104 | |
| 105 | SMLoc StartLoc, EndLoc; |
| 106 | union { |
| 107 | StringRef Tok; |
| 108 | RegOp Reg; |
| 109 | ImmOp Imm; |
| 110 | }; |
| 111 | |
| 112 | RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {} |
| 113 | |
| 114 | public: |
| 115 | RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() { |
| 116 | Kind = o.Kind; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 117 | IsRV64 = o.IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 118 | StartLoc = o.StartLoc; |
| 119 | EndLoc = o.EndLoc; |
| 120 | switch (Kind) { |
| 121 | case Register: |
| 122 | Reg = o.Reg; |
| 123 | break; |
| 124 | case Immediate: |
| 125 | Imm = o.Imm; |
| 126 | break; |
| 127 | case Token: |
| 128 | Tok = o.Tok; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool isToken() const override { return Kind == Token; } |
| 134 | bool isReg() const override { return Kind == Register; } |
| 135 | bool isImm() const override { return Kind == Immediate; } |
| 136 | bool isMem() const override { return false; } |
| 137 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 138 | bool evaluateConstantImm(int64_t &Imm, RISCVMCExpr::VariantKind &VK) const { |
| 139 | const MCExpr *Val = getImm(); |
| 140 | bool Ret = false; |
| 141 | if (auto *RE = dyn_cast<RISCVMCExpr>(Val)) { |
| 142 | Ret = RE->evaluateAsConstant(Imm); |
| 143 | VK = RE->getKind(); |
| 144 | } else if (auto CE = dyn_cast<MCConstantExpr>(Val)) { |
| 145 | Ret = true; |
| 146 | VK = RISCVMCExpr::VK_RISCV_None; |
| 147 | Imm = CE->getValue(); |
| 148 | } |
| 149 | return Ret; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 152 | // True if operand is a symbol with no modifiers, or a constant with no |
| 153 | // modifiers and isShiftedInt<N-1, 1>(Op). |
| 154 | template <int N> bool isBareSimmNLsb0() const { |
| 155 | int64_t Imm; |
| 156 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 157 | if (!isImm()) |
| 158 | return false; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 159 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 160 | bool IsValid; |
| 161 | if (!IsConstantImm) |
| 162 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 163 | else |
| 164 | IsValid = isShiftedInt<N - 1, 1>(Imm); |
| 165 | return IsValid && VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 168 | // Predicate methods for AsmOperands defined in RISCVInstrInfo.td |
| 169 | |
| 170 | /// Return true if the operand is a valid for the fence instruction e.g. |
| 171 | /// ('iorw'). |
| 172 | bool isFenceArg() const { |
| 173 | if (!isImm()) |
| 174 | return false; |
| 175 | const MCExpr *Val = getImm(); |
| 176 | auto *SVal = dyn_cast<MCSymbolRefExpr>(Val); |
| 177 | if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None) |
| 178 | return false; |
| 179 | |
| 180 | StringRef Str = SVal->getSymbol().getName(); |
| 181 | // Letters must be unique, taken from 'iorw', and in ascending order. This |
| 182 | // holds as long as each individual character is one of 'iorw' and is |
| 183 | // greater than the previous character. |
| 184 | char Prev = '\0'; |
| 185 | for (char c : Str) { |
| 186 | if (c != 'i' && c != 'o' && c != 'r' && c != 'w') |
| 187 | return false; |
| 188 | if (c <= Prev) |
| 189 | return false; |
| 190 | Prev = c; |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 195 | /// Return true if the operand is a valid floating point rounding mode. |
| 196 | bool isFRMArg() const { |
| 197 | if (!isImm()) |
| 198 | return false; |
| 199 | const MCExpr *Val = getImm(); |
| 200 | auto *SVal = dyn_cast<MCSymbolRefExpr>(Val); |
| 201 | if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None) |
| 202 | return false; |
| 203 | |
| 204 | StringRef Str = SVal->getSymbol().getName(); |
| 205 | |
| 206 | return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid; |
| 207 | } |
| 208 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 209 | bool isUImmLog2XLen() const { |
| 210 | int64_t Imm; |
| 211 | RISCVMCExpr::VariantKind VK; |
| 212 | if (!isImm()) |
| 213 | return false; |
| 214 | if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None) |
| 215 | return false; |
| 216 | return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm); |
| 217 | } |
| 218 | |
Alex Bradbury | 0ad4c26 | 2017-12-15 10:20:51 +0000 | [diff] [blame] | 219 | bool isUImmLog2XLenNonZero() const { |
| 220 | int64_t Imm; |
| 221 | RISCVMCExpr::VariantKind VK; |
| 222 | if (!isImm()) |
| 223 | return false; |
| 224 | if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None) |
| 225 | return false; |
| 226 | if (Imm == 0) |
| 227 | return false; |
| 228 | return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm); |
| 229 | } |
| 230 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 231 | bool isUImm5() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 232 | int64_t Imm; |
| 233 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 234 | if (!isImm()) |
| 235 | return false; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 236 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 237 | return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 240 | bool isUImm5NonZero() const { |
| 241 | int64_t Imm; |
| 242 | RISCVMCExpr::VariantKind VK; |
| 243 | if (!isImm()) |
| 244 | return false; |
| 245 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 246 | return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) && |
| 247 | VK == RISCVMCExpr::VK_RISCV_None; |
| 248 | } |
| 249 | |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 250 | bool isSImm6() const { |
| 251 | RISCVMCExpr::VariantKind VK; |
| 252 | int64_t Imm; |
| 253 | bool IsValid; |
| 254 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 255 | if (!IsConstantImm) |
| 256 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 257 | else |
| 258 | IsValid = isInt<6>(Imm); |
| 259 | return IsValid && |
| 260 | (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO); |
| 261 | } |
| 262 | |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 263 | bool isSImm6NonZero() const { |
| 264 | RISCVMCExpr::VariantKind VK; |
| 265 | int64_t Imm; |
| 266 | bool IsValid; |
| 267 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 268 | if (!IsConstantImm) |
| 269 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 270 | else |
| 271 | IsValid = ((Imm != 0) && isInt<6>(Imm)); |
| 272 | return IsValid && |
| 273 | (VK == RISCVMCExpr::VK_RISCV_None || VK == RISCVMCExpr::VK_RISCV_LO); |
| 274 | } |
| 275 | |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 276 | bool isCLUIImm() const { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 277 | int64_t Imm; |
| 278 | RISCVMCExpr::VariantKind VK; |
| 279 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 280 | return IsConstantImm && (Imm != 0) && |
| 281 | (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) && |
| 282 | VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 285 | bool isUImm7Lsb00() const { |
| 286 | int64_t Imm; |
| 287 | RISCVMCExpr::VariantKind VK; |
| 288 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 289 | return IsConstantImm && isShiftedUInt<5, 2>(Imm) && |
| 290 | VK == RISCVMCExpr::VK_RISCV_None; |
| 291 | } |
| 292 | |
| 293 | bool isUImm8Lsb00() const { |
| 294 | int64_t Imm; |
| 295 | RISCVMCExpr::VariantKind VK; |
| 296 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 297 | return IsConstantImm && isShiftedUInt<6, 2>(Imm) && |
| 298 | VK == RISCVMCExpr::VK_RISCV_None; |
| 299 | } |
| 300 | |
| 301 | bool isUImm8Lsb000() const { |
| 302 | int64_t Imm; |
| 303 | RISCVMCExpr::VariantKind VK; |
| 304 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 305 | return IsConstantImm && isShiftedUInt<5, 3>(Imm) && |
| 306 | VK == RISCVMCExpr::VK_RISCV_None; |
| 307 | } |
| 308 | |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 309 | bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); } |
| 310 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 311 | bool isUImm9Lsb000() const { |
| 312 | int64_t Imm; |
| 313 | RISCVMCExpr::VariantKind VK; |
| 314 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 315 | return IsConstantImm && isShiftedUInt<6, 3>(Imm) && |
| 316 | VK == RISCVMCExpr::VK_RISCV_None; |
| 317 | } |
| 318 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 319 | bool isUImm10Lsb00NonZero() const { |
| 320 | int64_t Imm; |
| 321 | RISCVMCExpr::VariantKind VK; |
| 322 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 323 | return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) && |
| 324 | VK == RISCVMCExpr::VK_RISCV_None; |
| 325 | } |
| 326 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 327 | bool isSImm12() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 328 | RISCVMCExpr::VariantKind VK; |
| 329 | int64_t Imm; |
| 330 | bool IsValid; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 331 | if (!isImm()) |
| 332 | return false; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 333 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 334 | if (!IsConstantImm) |
| 335 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 336 | else |
| 337 | IsValid = isInt<12>(Imm); |
Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 338 | return IsValid && (VK == RISCVMCExpr::VK_RISCV_None || |
| 339 | VK == RISCVMCExpr::VK_RISCV_LO || |
| 340 | VK == RISCVMCExpr::VK_RISCV_PCREL_LO); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 343 | bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); } |
| 344 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 345 | bool isUImm12() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 346 | int64_t Imm; |
| 347 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 348 | if (!isImm()) |
| 349 | return false; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 350 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 351 | return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 354 | bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 355 | |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 356 | bool isSImm10Lsb0000NonZero() const { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 357 | int64_t Imm; |
| 358 | RISCVMCExpr::VariantKind VK; |
| 359 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 360 | return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) && |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 361 | VK == RISCVMCExpr::VK_RISCV_None; |
| 362 | } |
| 363 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 364 | bool isUImm20() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 365 | RISCVMCExpr::VariantKind VK; |
| 366 | int64_t Imm; |
| 367 | bool IsValid; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 368 | if (!isImm()) |
| 369 | return false; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 370 | bool IsConstantImm = evaluateConstantImm(Imm, VK); |
| 371 | if (!IsConstantImm) |
| 372 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 373 | else |
| 374 | IsValid = isUInt<20>(Imm); |
| 375 | return IsValid && (VK == RISCVMCExpr::VK_RISCV_None || |
| 376 | VK == RISCVMCExpr::VK_RISCV_HI || |
| 377 | VK == RISCVMCExpr::VK_RISCV_PCREL_HI); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 380 | bool isSImm21Lsb0() const { return isBareSimmNLsb0<21>(); } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 381 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 382 | /// getStartLoc - Gets location of the first token of this operand |
| 383 | SMLoc getStartLoc() const override { return StartLoc; } |
| 384 | /// getEndLoc - Gets location of the last token of this operand |
| 385 | SMLoc getEndLoc() const override { return EndLoc; } |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 386 | /// True if this operand is for an RV64 instruction |
| 387 | bool isRV64() const { return IsRV64; } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 388 | |
| 389 | unsigned getReg() const override { |
| 390 | assert(Kind == Register && "Invalid type access!"); |
| 391 | return Reg.RegNum; |
| 392 | } |
| 393 | |
| 394 | const MCExpr *getImm() const { |
| 395 | assert(Kind == Immediate && "Invalid type access!"); |
| 396 | return Imm.Val; |
| 397 | } |
| 398 | |
| 399 | StringRef getToken() const { |
| 400 | assert(Kind == Token && "Invalid type access!"); |
| 401 | return Tok; |
| 402 | } |
| 403 | |
| 404 | void print(raw_ostream &OS) const override { |
| 405 | switch (Kind) { |
| 406 | case Immediate: |
| 407 | OS << *getImm(); |
| 408 | break; |
| 409 | case Register: |
| 410 | OS << "<register x"; |
| 411 | OS << getReg() << ">"; |
| 412 | break; |
| 413 | case Token: |
| 414 | OS << "'" << getToken() << "'"; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 419 | static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S, |
| 420 | bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 421 | auto Op = make_unique<RISCVOperand>(Token); |
| 422 | Op->Tok = Str; |
| 423 | Op->StartLoc = S; |
| 424 | Op->EndLoc = S; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 425 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 426 | return Op; |
| 427 | } |
| 428 | |
| 429 | static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S, |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 430 | SMLoc E, bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 431 | auto Op = make_unique<RISCVOperand>(Register); |
| 432 | Op->Reg.RegNum = RegNo; |
| 433 | Op->StartLoc = S; |
| 434 | Op->EndLoc = E; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 435 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 436 | return Op; |
| 437 | } |
| 438 | |
| 439 | static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S, |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 440 | SMLoc E, bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 441 | auto Op = make_unique<RISCVOperand>(Immediate); |
| 442 | Op->Imm.Val = Val; |
| 443 | Op->StartLoc = S; |
| 444 | Op->EndLoc = E; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 445 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 446 | return Op; |
| 447 | } |
| 448 | |
| 449 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 450 | assert(Expr && "Expr shouldn't be null!"); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 451 | int64_t Imm = 0; |
| 452 | bool IsConstant = false; |
| 453 | if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) { |
| 454 | IsConstant = RE->evaluateAsConstant(Imm); |
| 455 | } else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) { |
| 456 | IsConstant = true; |
| 457 | Imm = CE->getValue(); |
| 458 | } |
| 459 | |
| 460 | if (IsConstant) |
| 461 | Inst.addOperand(MCOperand::createImm(Imm)); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 462 | else |
| 463 | Inst.addOperand(MCOperand::createExpr(Expr)); |
| 464 | } |
| 465 | |
| 466 | // Used by the TableGen Code |
| 467 | void addRegOperands(MCInst &Inst, unsigned N) const { |
| 468 | assert(N == 1 && "Invalid number of operands!"); |
| 469 | Inst.addOperand(MCOperand::createReg(getReg())); |
| 470 | } |
| 471 | |
| 472 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 473 | assert(N == 1 && "Invalid number of operands!"); |
| 474 | addExpr(Inst, getImm()); |
| 475 | } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 476 | |
| 477 | void addFenceArgOperands(MCInst &Inst, unsigned N) const { |
| 478 | assert(N == 1 && "Invalid number of operands!"); |
| 479 | // isFenceArg has validated the operand, meaning this cast is safe |
| 480 | auto SE = cast<MCSymbolRefExpr>(getImm()); |
| 481 | |
| 482 | unsigned Imm = 0; |
| 483 | for (char c : SE->getSymbol().getName()) { |
| 484 | switch (c) { |
| 485 | default: llvm_unreachable("FenceArg must contain only [iorw]"); |
| 486 | case 'i': Imm |= RISCVFenceField::I; break; |
| 487 | case 'o': Imm |= RISCVFenceField::O; break; |
| 488 | case 'r': Imm |= RISCVFenceField::R; break; |
| 489 | case 'w': Imm |= RISCVFenceField::W; break; |
| 490 | } |
| 491 | } |
| 492 | Inst.addOperand(MCOperand::createImm(Imm)); |
| 493 | } |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 494 | |
| 495 | // Returns the rounding mode represented by this RISCVOperand. Should only |
| 496 | // be called after checking isFRMArg. |
| 497 | RISCVFPRndMode::RoundingMode getRoundingMode() const { |
| 498 | // isFRMArg has validated the operand, meaning this cast is safe. |
| 499 | auto SE = cast<MCSymbolRefExpr>(getImm()); |
| 500 | RISCVFPRndMode::RoundingMode FRM = |
| 501 | RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName()); |
| 502 | assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode"); |
| 503 | return FRM; |
| 504 | } |
| 505 | |
| 506 | void addFRMArgOperands(MCInst &Inst, unsigned N) const { |
| 507 | assert(N == 1 && "Invalid number of operands!"); |
| 508 | Inst.addOperand(MCOperand::createImm(getRoundingMode())); |
| 509 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 510 | }; |
| 511 | } // end anonymous namespace. |
| 512 | |
| 513 | #define GET_REGISTER_MATCHER |
| 514 | #define GET_MATCHER_IMPLEMENTATION |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 515 | #include "RISCVGenAsmMatcher.inc" |
| 516 | |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 517 | // Return the matching FPR64 register for the given FPR32. |
| 518 | // FIXME: Ideally this function could be removed in favour of using |
| 519 | // information from TableGen. |
| 520 | unsigned convertFPR32ToFPR64(unsigned Reg) { |
| 521 | switch (Reg) { |
| 522 | default: |
| 523 | llvm_unreachable("Not a recognised FPR32 register"); |
| 524 | case RISCV::F0_32: return RISCV::F0_64; |
| 525 | case RISCV::F1_32: return RISCV::F1_64; |
| 526 | case RISCV::F2_32: return RISCV::F2_64; |
| 527 | case RISCV::F3_32: return RISCV::F3_64; |
| 528 | case RISCV::F4_32: return RISCV::F4_64; |
| 529 | case RISCV::F5_32: return RISCV::F5_64; |
| 530 | case RISCV::F6_32: return RISCV::F6_64; |
| 531 | case RISCV::F7_32: return RISCV::F7_64; |
| 532 | case RISCV::F8_32: return RISCV::F8_64; |
| 533 | case RISCV::F9_32: return RISCV::F9_64; |
| 534 | case RISCV::F10_32: return RISCV::F10_64; |
| 535 | case RISCV::F11_32: return RISCV::F11_64; |
| 536 | case RISCV::F12_32: return RISCV::F12_64; |
| 537 | case RISCV::F13_32: return RISCV::F13_64; |
| 538 | case RISCV::F14_32: return RISCV::F14_64; |
| 539 | case RISCV::F15_32: return RISCV::F15_64; |
| 540 | case RISCV::F16_32: return RISCV::F16_64; |
| 541 | case RISCV::F17_32: return RISCV::F17_64; |
| 542 | case RISCV::F18_32: return RISCV::F18_64; |
| 543 | case RISCV::F19_32: return RISCV::F19_64; |
| 544 | case RISCV::F20_32: return RISCV::F20_64; |
| 545 | case RISCV::F21_32: return RISCV::F21_64; |
| 546 | case RISCV::F22_32: return RISCV::F22_64; |
| 547 | case RISCV::F23_32: return RISCV::F23_64; |
| 548 | case RISCV::F24_32: return RISCV::F24_64; |
| 549 | case RISCV::F25_32: return RISCV::F25_64; |
| 550 | case RISCV::F26_32: return RISCV::F26_64; |
| 551 | case RISCV::F27_32: return RISCV::F27_64; |
| 552 | case RISCV::F28_32: return RISCV::F28_64; |
| 553 | case RISCV::F29_32: return RISCV::F29_64; |
| 554 | case RISCV::F30_32: return RISCV::F30_64; |
| 555 | case RISCV::F31_32: return RISCV::F31_64; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp, |
| 560 | unsigned Kind) { |
| 561 | RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp); |
| 562 | if (!Op.isReg()) |
| 563 | return Match_InvalidOperand; |
| 564 | |
| 565 | unsigned Reg = Op.getReg(); |
| 566 | bool IsRegFPR32 = |
| 567 | RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 568 | bool IsRegFPR32C = |
| 569 | RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg); |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 570 | |
| 571 | // As the parser couldn't differentiate an FPR32 from an FPR64, coerce the |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 572 | // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary. |
| 573 | if ((IsRegFPR32 && Kind == MCK_FPR64) || |
| 574 | (IsRegFPR32C && Kind == MCK_FPR64C)) { |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 575 | Op.Reg.RegNum = convertFPR32ToFPR64(Reg); |
| 576 | return Match_Success; |
| 577 | } |
| 578 | return Match_InvalidOperand; |
| 579 | } |
| 580 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 581 | bool RISCVAsmParser::generateImmOutOfRangeError( |
| 582 | OperandVector &Operands, uint64_t ErrorInfo, int Lower, int Upper, |
| 583 | Twine Msg = "immediate must be an integer in the range") { |
| 584 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 585 | return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]"); |
| 586 | } |
| 587 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 588 | bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
| 589 | OperandVector &Operands, |
| 590 | MCStreamer &Out, |
| 591 | uint64_t &ErrorInfo, |
| 592 | bool MatchingInlineAsm) { |
| 593 | MCInst Inst; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 594 | |
| 595 | switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) { |
| 596 | default: |
| 597 | break; |
| 598 | case Match_Success: |
| 599 | Inst.setLoc(IDLoc); |
| 600 | Out.EmitInstruction(Inst, getSTI()); |
| 601 | return false; |
| 602 | case Match_MissingFeature: |
| 603 | return Error(IDLoc, "instruction use requires an option to be enabled"); |
| 604 | case Match_MnemonicFail: |
| 605 | return Error(IDLoc, "unrecognized instruction mnemonic"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 606 | case Match_InvalidOperand: { |
| 607 | SMLoc ErrorLoc = IDLoc; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 608 | if (ErrorInfo != ~0U) { |
| 609 | if (ErrorInfo >= Operands.size()) |
| 610 | return Error(ErrorLoc, "too few operands for instruction"); |
| 611 | |
| 612 | ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 613 | if (ErrorLoc == SMLoc()) |
| 614 | ErrorLoc = IDLoc; |
| 615 | } |
| 616 | return Error(ErrorLoc, "invalid operand for instruction"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 617 | } |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 618 | case Match_InvalidUImmLog2XLen: |
| 619 | if (isRV64()) |
| 620 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1); |
| 621 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1); |
Alex Bradbury | 0ad4c26 | 2017-12-15 10:20:51 +0000 | [diff] [blame] | 622 | case Match_InvalidUImmLog2XLenNonZero: |
| 623 | if (isRV64()) |
| 624 | return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1); |
| 625 | return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 626 | case Match_InvalidUImm5: |
| 627 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1); |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 628 | case Match_InvalidSImm6: |
| 629 | return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5), |
| 630 | (1 << 5) - 1); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 631 | case Match_InvalidSImm6NonZero: |
| 632 | return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5), |
| 633 | (1 << 5) - 1, |
| 634 | "immediate must be non-zero in the range"); |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 635 | case Match_InvalidCLUIImm: |
| 636 | return generateImmOutOfRangeError( |
| 637 | Operands, ErrorInfo, 1, (1 << 5) - 1, |
| 638 | "immediate must be in [0xfffe0, 0xfffff] or"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 639 | case Match_InvalidUImm7Lsb00: |
| 640 | return generateImmOutOfRangeError( |
| 641 | Operands, ErrorInfo, 0, (1 << 7) - 4, |
| 642 | "immediate must be a multiple of 4 bytes in the range"); |
| 643 | case Match_InvalidUImm8Lsb00: |
| 644 | return generateImmOutOfRangeError( |
| 645 | Operands, ErrorInfo, 0, (1 << 8) - 4, |
| 646 | "immediate must be a multiple of 4 bytes in the range"); |
| 647 | case Match_InvalidUImm8Lsb000: |
| 648 | return generateImmOutOfRangeError( |
| 649 | Operands, ErrorInfo, 0, (1 << 8) - 8, |
| 650 | "immediate must be a multiple of 8 bytes in the range"); |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 651 | case Match_InvalidSImm9Lsb0: |
| 652 | return generateImmOutOfRangeError( |
| 653 | Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2, |
| 654 | "immediate must be a multiple of 2 bytes in the range"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 655 | case Match_InvalidUImm9Lsb000: |
| 656 | return generateImmOutOfRangeError( |
| 657 | Operands, ErrorInfo, 0, (1 << 9) - 8, |
| 658 | "immediate must be a multiple of 8 bytes in the range"); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 659 | case Match_InvalidUImm10Lsb00NonZero: |
| 660 | return generateImmOutOfRangeError( |
| 661 | Operands, ErrorInfo, 4, (1 << 10) - 4, |
| 662 | "immediate must be a multiple of 4 bytes in the range"); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 663 | case Match_InvalidSImm10Lsb0000NonZero: |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 664 | return generateImmOutOfRangeError( |
| 665 | Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16, |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 666 | "immediate must be a multiple of 16 bytes and non-zero in the range"); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 667 | case Match_InvalidSImm12: |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 668 | return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 11), |
| 669 | (1 << 11) - 1); |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 670 | case Match_InvalidSImm12Lsb0: |
| 671 | return generateImmOutOfRangeError( |
| 672 | Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2, |
| 673 | "immediate must be a multiple of 2 bytes in the range"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 674 | case Match_InvalidUImm12: |
| 675 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1); |
| 676 | case Match_InvalidSImm13Lsb0: |
| 677 | return generateImmOutOfRangeError( |
| 678 | Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2, |
| 679 | "immediate must be a multiple of 2 bytes in the range"); |
| 680 | case Match_InvalidUImm20: |
| 681 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1); |
| 682 | case Match_InvalidSImm21Lsb0: |
| 683 | return generateImmOutOfRangeError( |
| 684 | Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2, |
| 685 | "immediate must be a multiple of 2 bytes in the range"); |
| 686 | case Match_InvalidFenceArg: { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 687 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 688 | return Error( |
| 689 | ErrorLoc, |
| 690 | "operand must be formed of letters selected in-order from 'iorw'"); |
| 691 | } |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 692 | case Match_InvalidFRMArg: { |
| 693 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 694 | return Error( |
| 695 | ErrorLoc, |
| 696 | "operand must be a valid floating point rounding mode mnemonic"); |
| 697 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | llvm_unreachable("Unknown match type detected!"); |
| 701 | } |
| 702 | |
| 703 | bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, |
| 704 | SMLoc &EndLoc) { |
| 705 | const AsmToken &Tok = getParser().getTok(); |
| 706 | StartLoc = Tok.getLoc(); |
| 707 | EndLoc = Tok.getEndLoc(); |
| 708 | RegNo = 0; |
| 709 | StringRef Name = getLexer().getTok().getIdentifier(); |
| 710 | |
| 711 | if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) { |
| 712 | getParser().Lex(); // Eat identifier token. |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | return Error(StartLoc, "invalid register name"); |
| 717 | } |
| 718 | |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 719 | OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands, |
| 720 | bool AllowParens) { |
| 721 | SMLoc FirstS = getLoc(); |
| 722 | bool HadParens = false; |
| 723 | AsmToken Buf[2]; |
| 724 | |
| 725 | // If this a parenthesised register name is allowed, parse it atomically |
| 726 | if (AllowParens && getLexer().is(AsmToken::LParen)) { |
| 727 | size_t ReadCount = getLexer().peekTokens(Buf); |
| 728 | if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) { |
| 729 | HadParens = true; |
| 730 | getParser().Lex(); // Eat '(' |
| 731 | } |
| 732 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 733 | |
| 734 | switch (getLexer().getKind()) { |
| 735 | default: |
| 736 | return MatchOperand_NoMatch; |
| 737 | case AsmToken::Identifier: |
| 738 | StringRef Name = getLexer().getTok().getIdentifier(); |
| 739 | unsigned RegNo = MatchRegisterName(Name); |
| 740 | if (RegNo == 0) { |
| 741 | RegNo = MatchRegisterAltName(Name); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 742 | if (RegNo == 0) { |
| 743 | if (HadParens) |
| 744 | getLexer().UnLex(Buf[0]); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 745 | return MatchOperand_NoMatch; |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 746 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 747 | } |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 748 | if (HadParens) |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 749 | Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64())); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 750 | SMLoc S = getLoc(); |
| 751 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 752 | getLexer().Lex(); |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 753 | Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64())); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 754 | } |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 755 | |
| 756 | if (HadParens) { |
| 757 | getParser().Lex(); // Eat ')' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 758 | Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64())); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 761 | return MatchOperand_Success; |
| 762 | } |
| 763 | |
| 764 | OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) { |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 765 | SMLoc S = getLoc(); |
| 766 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
| 767 | const MCExpr *Res; |
| 768 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 769 | switch (getLexer().getKind()) { |
| 770 | default: |
| 771 | return MatchOperand_NoMatch; |
| 772 | case AsmToken::LParen: |
| 773 | case AsmToken::Minus: |
| 774 | case AsmToken::Plus: |
| 775 | case AsmToken::Integer: |
| 776 | case AsmToken::String: |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 777 | if (getParser().parseExpression(Res)) |
| 778 | return MatchOperand_ParseFail; |
| 779 | break; |
| 780 | case AsmToken::Identifier: { |
| 781 | StringRef Identifier; |
| 782 | if (getParser().parseIdentifier(Identifier)) |
| 783 | return MatchOperand_ParseFail; |
| 784 | MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier); |
| 785 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 786 | break; |
| 787 | } |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 788 | case AsmToken::Percent: |
| 789 | return parseOperandWithModifier(Operands); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 790 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 791 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 792 | Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64())); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 793 | return MatchOperand_Success; |
| 794 | } |
| 795 | |
| 796 | OperandMatchResultTy |
| 797 | RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) { |
| 798 | SMLoc S = getLoc(); |
| 799 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
| 800 | |
| 801 | if (getLexer().getKind() != AsmToken::Percent) { |
| 802 | Error(getLoc(), "expected '%' for operand modifier"); |
| 803 | return MatchOperand_ParseFail; |
| 804 | } |
| 805 | |
| 806 | getParser().Lex(); // Eat '%' |
| 807 | |
| 808 | if (getLexer().getKind() != AsmToken::Identifier) { |
| 809 | Error(getLoc(), "expected valid identifier for operand modifier"); |
| 810 | return MatchOperand_ParseFail; |
| 811 | } |
| 812 | StringRef Identifier = getParser().getTok().getIdentifier(); |
| 813 | RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier); |
| 814 | if (VK == RISCVMCExpr::VK_RISCV_Invalid) { |
| 815 | Error(getLoc(), "unrecognized operand modifier"); |
| 816 | return MatchOperand_ParseFail; |
| 817 | } |
| 818 | |
| 819 | getParser().Lex(); // Eat the identifier |
| 820 | if (getLexer().getKind() != AsmToken::LParen) { |
| 821 | Error(getLoc(), "expected '('"); |
| 822 | return MatchOperand_ParseFail; |
| 823 | } |
| 824 | getParser().Lex(); // Eat '(' |
| 825 | |
| 826 | const MCExpr *SubExpr; |
| 827 | if (getParser().parseParenExpression(SubExpr, E)) { |
| 828 | return MatchOperand_ParseFail; |
| 829 | } |
| 830 | |
| 831 | const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext()); |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 832 | Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 833 | return MatchOperand_Success; |
| 834 | } |
| 835 | |
| 836 | OperandMatchResultTy |
| 837 | RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) { |
| 838 | if (getLexer().isNot(AsmToken::LParen)) { |
| 839 | Error(getLoc(), "expected '('"); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 840 | return MatchOperand_ParseFail; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 841 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 842 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 843 | getParser().Lex(); // Eat '(' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 844 | Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 845 | |
| 846 | if (parseRegister(Operands) != MatchOperand_Success) { |
| 847 | Error(getLoc(), "expected register"); |
| 848 | return MatchOperand_ParseFail; |
| 849 | } |
| 850 | |
| 851 | if (getLexer().isNot(AsmToken::RParen)) { |
| 852 | Error(getLoc(), "expected ')'"); |
| 853 | return MatchOperand_ParseFail; |
| 854 | } |
| 855 | |
| 856 | getParser().Lex(); // Eat ')' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 857 | Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 858 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 859 | return MatchOperand_Success; |
| 860 | } |
| 861 | |
| 862 | /// Looks at a token type and creates the relevant operand |
| 863 | /// from this information, adding to Operands. |
| 864 | /// If operand was parsed, returns false, else true. |
| 865 | bool RISCVAsmParser::parseOperand(OperandVector &Operands) { |
| 866 | // Attempt to parse token as register |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 867 | if (parseRegister(Operands, true) == MatchOperand_Success) |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 868 | return false; |
| 869 | |
| 870 | // Attempt to parse token as an immediate |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 871 | if (parseImmediate(Operands) == MatchOperand_Success) { |
| 872 | // Parse memory base register if present |
| 873 | if (getLexer().is(AsmToken::LParen)) |
| 874 | return parseMemOpBaseReg(Operands) != MatchOperand_Success; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 875 | return false; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 876 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 877 | |
| 878 | // Finally we have exhausted all options and must declare defeat. |
| 879 | Error(getLoc(), "unknown operand"); |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info, |
| 884 | StringRef Name, SMLoc NameLoc, |
| 885 | OperandVector &Operands) { |
| 886 | // First operand is token for instruction |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 887 | Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64())); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 888 | |
| 889 | // If there are no more operands, then finish |
| 890 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 891 | return false; |
| 892 | |
| 893 | // Parse first operand |
| 894 | if (parseOperand(Operands)) |
| 895 | return true; |
| 896 | |
| 897 | // Parse until end of statement, consuming commas between operands |
| 898 | while (getLexer().is(AsmToken::Comma)) { |
| 899 | // Consume comma token |
| 900 | getLexer().Lex(); |
| 901 | |
| 902 | // Parse next operand |
| 903 | if (parseOperand(Operands)) |
| 904 | return true; |
| 905 | } |
| 906 | |
| 907 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 908 | SMLoc Loc = getLexer().getLoc(); |
| 909 | getParser().eatToEndOfStatement(); |
| 910 | return Error(Loc, "unexpected token"); |
| 911 | } |
| 912 | |
| 913 | getParser().Lex(); // Consume the EndOfStatement. |
| 914 | return false; |
| 915 | } |
| 916 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 917 | bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr, |
| 918 | RISCVMCExpr::VariantKind &Kind, |
| 919 | int64_t &Addend) { |
| 920 | Kind = RISCVMCExpr::VK_RISCV_None; |
| 921 | Addend = 0; |
| 922 | |
| 923 | if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) { |
| 924 | Kind = RE->getKind(); |
| 925 | Expr = RE->getSubExpr(); |
| 926 | } |
| 927 | |
| 928 | // It's a simple symbol reference or constant with no addend. |
| 929 | if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr)) |
| 930 | return true; |
| 931 | |
| 932 | const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr); |
| 933 | if (!BE) |
| 934 | return false; |
| 935 | |
| 936 | if (!isa<MCSymbolRefExpr>(BE->getLHS())) |
| 937 | return false; |
| 938 | |
| 939 | if (BE->getOpcode() != MCBinaryExpr::Add && |
| 940 | BE->getOpcode() != MCBinaryExpr::Sub) |
| 941 | return false; |
| 942 | |
| 943 | // We are able to support the subtraction of two symbol references |
| 944 | if (BE->getOpcode() == MCBinaryExpr::Sub && |
| 945 | isa<MCSymbolRefExpr>(BE->getRHS())) |
| 946 | return true; |
| 947 | |
| 948 | // See if the addend is is a constant, otherwise there's more going |
| 949 | // on here than we can deal with. |
| 950 | auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS()); |
| 951 | if (!AddendExpr) |
| 952 | return false; |
| 953 | |
| 954 | Addend = AddendExpr->getValue(); |
| 955 | if (BE->getOpcode() == MCBinaryExpr::Sub) |
| 956 | Addend = -Addend; |
| 957 | |
| 958 | // It's some symbol reference + a constant addend |
| 959 | return Kind != RISCVMCExpr::VK_RISCV_Invalid; |
| 960 | } |
| 961 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 962 | bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) { return true; } |
| 963 | |
| 964 | extern "C" void LLVMInitializeRISCVAsmParser() { |
| 965 | RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target()); |
| 966 | RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target()); |
| 967 | } |