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