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 | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/RISCVAsmBackend.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 | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/RISCVTargetStreamer.h" |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 14 | #include "Utils/RISCVBaseInfo.h" |
Alex Bradbury | 22c091f | 2018-11-15 10:11:31 +0000 | [diff] [blame^] | 15 | #include "Utils/RISCVMatInt.h" |
Alex Bradbury | 4f7f0da | 2017-09-06 09:21:21 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/StringSwitch.h" |
Alex Bradbury | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAssembler.h" |
Alex Bradbury | 4f7f0da | 2017-09-06 09:21:21 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCInst.h" |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstBuilder.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 24 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 25 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCRegisterInfo.h" |
| 27 | #include "llvm/MC/MCStreamer.h" |
| 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Casting.h" |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetRegistry.h" |
| 32 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 33 | #include <limits> |
| 34 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Sameer AbuAsal | c1b0e66 | 2018-04-06 21:07:05 +0000 | [diff] [blame] | 37 | // Include the auto-generated portion of the compress emitter. |
| 38 | #define GEN_COMPRESS_INSTR |
| 39 | #include "RISCVGenCompressInstEmitter.inc" |
| 40 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | struct RISCVOperand; |
| 43 | |
| 44 | class RISCVAsmParser : public MCTargetAsmParser { |
| 45 | SMLoc getLoc() const { return getParser().getTok().getLoc(); } |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 46 | bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 47 | |
Alex Bradbury | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 48 | RISCVTargetStreamer &getTargetStreamer() { |
| 49 | MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer(); |
| 50 | return static_cast<RISCVTargetStreamer &>(TS); |
| 51 | } |
| 52 | |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 53 | unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, |
| 54 | unsigned Kind) override; |
| 55 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 56 | bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo, |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 57 | int64_t Lower, int64_t Upper, Twine Msg); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 58 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 59 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
| 60 | OperandVector &Operands, MCStreamer &Out, |
| 61 | uint64_t &ErrorInfo, |
| 62 | bool MatchingInlineAsm) override; |
| 63 | |
| 64 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; |
| 65 | |
| 66 | bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, |
| 67 | SMLoc NameLoc, OperandVector &Operands) override; |
| 68 | |
| 69 | bool ParseDirective(AsmToken DirectiveID) override; |
| 70 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 71 | // Helper to actually emit an instruction to the MCStreamer. Also, when |
| 72 | // possible, compression of the instruction is performed. |
| 73 | void emitToStreamer(MCStreamer &S, const MCInst &Inst); |
| 74 | |
| 75 | // Helper to emit a combination of LUI, ADDI(W), and SLLI instructions that |
| 76 | // synthesize the desired immedate value into the destination register. |
| 77 | void emitLoadImm(unsigned DestReg, int64_t Value, MCStreamer &Out); |
| 78 | |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 79 | // Helper to emit pseudo instruction "lla" used in PC-rel addressing. |
| 80 | void emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out); |
| 81 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 82 | /// Helper for processing MC instructions that have been successfully matched |
| 83 | /// by MatchAndEmitInstruction. Modifications to the emitted instructions, |
| 84 | /// like the expansion of pseudo instructions (e.g., "li"), can be performed |
| 85 | /// in this method. |
| 86 | bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out); |
| 87 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 88 | // Auto-generated instruction matching functions |
| 89 | #define GET_ASSEMBLER_HEADER |
| 90 | #include "RISCVGenAsmMatcher.inc" |
| 91 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 92 | OperandMatchResultTy parseCSRSystemRegister(OperandVector &Operands); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 93 | OperandMatchResultTy parseImmediate(OperandVector &Operands); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 94 | OperandMatchResultTy parseRegister(OperandVector &Operands, |
| 95 | bool AllowParens = false); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 96 | OperandMatchResultTy parseMemOpBaseReg(OperandVector &Operands); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 97 | OperandMatchResultTy parseOperandWithModifier(OperandVector &Operands); |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 98 | OperandMatchResultTy parseBareSymbol(OperandVector &Operands); |
Alex Bradbury | 226f3ef | 2018-09-20 08:10:35 +0000 | [diff] [blame] | 99 | OperandMatchResultTy parseJALOffset(OperandVector &Operands); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 100 | |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 101 | bool parseOperand(OperandVector &Operands, StringRef Mnemonic); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 102 | |
Alex Bradbury | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 103 | bool parseDirectiveOption(); |
| 104 | |
| 105 | void setFeatureBits(uint64_t Feature, StringRef FeatureString) { |
| 106 | if (!(getSTI().getFeatureBits()[Feature])) { |
| 107 | MCSubtargetInfo &STI = copySTI(); |
| 108 | setAvailableFeatures( |
| 109 | ComputeAvailableFeatures(STI.ToggleFeature(FeatureString))); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void clearFeatureBits(uint64_t Feature, StringRef FeatureString) { |
| 114 | if (getSTI().getFeatureBits()[Feature]) { |
| 115 | MCSubtargetInfo &STI = copySTI(); |
| 116 | setAvailableFeatures( |
| 117 | ComputeAvailableFeatures(STI.ToggleFeature(FeatureString))); |
| 118 | } |
| 119 | } |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 120 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 121 | public: |
| 122 | enum RISCVMatchResultTy { |
| 123 | Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY, |
| 124 | #define GET_OPERAND_DIAGNOSTIC_TYPES |
| 125 | #include "RISCVGenAsmMatcher.inc" |
| 126 | #undef GET_OPERAND_DIAGNOSTIC_TYPES |
| 127 | }; |
| 128 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 129 | static bool classifySymbolRef(const MCExpr *Expr, |
| 130 | RISCVMCExpr::VariantKind &Kind, |
| 131 | int64_t &Addend); |
| 132 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 133 | RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, |
| 134 | const MCInstrInfo &MII, const MCTargetOptions &Options) |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 135 | : MCTargetAsmParser(Options, STI, MII) { |
Alex Bradbury | cea6db0 | 2018-05-17 05:58:08 +0000 | [diff] [blame] | 136 | Parser.addAliasForDirective(".half", ".2byte"); |
| 137 | Parser.addAliasForDirective(".hword", ".2byte"); |
| 138 | Parser.addAliasForDirective(".word", ".4byte"); |
| 139 | Parser.addAliasForDirective(".dword", ".8byte"); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 140 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | /// RISCVOperand - Instances of this class represent a parsed machine |
| 145 | /// instruction |
| 146 | struct RISCVOperand : public MCParsedAsmOperand { |
| 147 | |
| 148 | enum KindTy { |
| 149 | Token, |
| 150 | Register, |
| 151 | Immediate, |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 152 | SystemRegister |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 153 | } Kind; |
| 154 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 155 | bool IsRV64; |
| 156 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 157 | struct RegOp { |
| 158 | unsigned RegNum; |
| 159 | }; |
| 160 | |
| 161 | struct ImmOp { |
| 162 | const MCExpr *Val; |
| 163 | }; |
| 164 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 165 | struct SysRegOp { |
| 166 | const char *Data; |
| 167 | unsigned Length; |
| 168 | unsigned Encoding; |
| 169 | // FIXME: Add the Encoding parsed fields as needed for checks, |
| 170 | // e.g.: read/write or user/supervisor/machine privileges. |
| 171 | }; |
| 172 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 173 | SMLoc StartLoc, EndLoc; |
| 174 | union { |
| 175 | StringRef Tok; |
| 176 | RegOp Reg; |
| 177 | ImmOp Imm; |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 178 | struct SysRegOp SysReg; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | RISCVOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {} |
| 182 | |
| 183 | public: |
| 184 | RISCVOperand(const RISCVOperand &o) : MCParsedAsmOperand() { |
| 185 | Kind = o.Kind; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 186 | IsRV64 = o.IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 187 | StartLoc = o.StartLoc; |
| 188 | EndLoc = o.EndLoc; |
| 189 | switch (Kind) { |
| 190 | case Register: |
| 191 | Reg = o.Reg; |
| 192 | break; |
| 193 | case Immediate: |
| 194 | Imm = o.Imm; |
| 195 | break; |
| 196 | case Token: |
| 197 | Tok = o.Tok; |
| 198 | break; |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 199 | case SystemRegister: |
| 200 | SysReg = o.SysReg; |
| 201 | break; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | bool isToken() const override { return Kind == Token; } |
| 206 | bool isReg() const override { return Kind == Register; } |
| 207 | bool isImm() const override { return Kind == Immediate; } |
| 208 | bool isMem() const override { return false; } |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 209 | bool isSystemRegister() const { return Kind == SystemRegister; } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 210 | |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 211 | static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm, |
| 212 | RISCVMCExpr::VariantKind &VK) { |
| 213 | if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 214 | VK = RE->getKind(); |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 215 | return RE->evaluateAsConstant(Imm); |
| 216 | } |
| 217 | |
| 218 | if (auto CE = dyn_cast<MCConstantExpr>(Expr)) { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 219 | VK = RISCVMCExpr::VK_RISCV_None; |
| 220 | Imm = CE->getValue(); |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 221 | return true; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 222 | } |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 223 | |
| 224 | return false; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 227 | // True if operand is a symbol with no modifiers, or a constant with no |
| 228 | // modifiers and isShiftedInt<N-1, 1>(Op). |
| 229 | template <int N> bool isBareSimmNLsb0() const { |
| 230 | int64_t Imm; |
| 231 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 232 | if (!isImm()) |
| 233 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 234 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 235 | bool IsValid; |
| 236 | if (!IsConstantImm) |
| 237 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 238 | else |
| 239 | IsValid = isShiftedInt<N - 1, 1>(Imm); |
| 240 | return IsValid && VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 243 | // Predicate methods for AsmOperands defined in RISCVInstrInfo.td |
| 244 | |
Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 245 | bool isBareSymbol() const { |
| 246 | int64_t Imm; |
| 247 | RISCVMCExpr::VariantKind VK; |
| 248 | // Must be of 'immediate' type but not a constant. |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 249 | if (!isImm() || evaluateConstantImm(getImm(), Imm, VK)) |
Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 250 | return false; |
| 251 | return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) && |
| 252 | VK == RISCVMCExpr::VK_RISCV_None; |
| 253 | } |
| 254 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 255 | bool isCSRSystemRegister() const { return isSystemRegister(); } |
| 256 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 257 | /// Return true if the operand is a valid for the fence instruction e.g. |
| 258 | /// ('iorw'). |
| 259 | bool isFenceArg() const { |
| 260 | if (!isImm()) |
| 261 | return false; |
| 262 | const MCExpr *Val = getImm(); |
| 263 | auto *SVal = dyn_cast<MCSymbolRefExpr>(Val); |
| 264 | if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None) |
| 265 | return false; |
| 266 | |
| 267 | StringRef Str = SVal->getSymbol().getName(); |
| 268 | // Letters must be unique, taken from 'iorw', and in ascending order. This |
| 269 | // holds as long as each individual character is one of 'iorw' and is |
| 270 | // greater than the previous character. |
| 271 | char Prev = '\0'; |
| 272 | for (char c : Str) { |
| 273 | if (c != 'i' && c != 'o' && c != 'r' && c != 'w') |
| 274 | return false; |
| 275 | if (c <= Prev) |
| 276 | return false; |
| 277 | Prev = c; |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 282 | /// Return true if the operand is a valid floating point rounding mode. |
| 283 | bool isFRMArg() const { |
| 284 | if (!isImm()) |
| 285 | return false; |
| 286 | const MCExpr *Val = getImm(); |
| 287 | auto *SVal = dyn_cast<MCSymbolRefExpr>(Val); |
| 288 | if (!SVal || SVal->getKind() != MCSymbolRefExpr::VK_None) |
| 289 | return false; |
| 290 | |
| 291 | StringRef Str = SVal->getSymbol().getName(); |
| 292 | |
| 293 | return RISCVFPRndMode::stringToRoundingMode(Str) != RISCVFPRndMode::Invalid; |
| 294 | } |
| 295 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 296 | bool isImmXLen() const { |
| 297 | int64_t Imm; |
| 298 | RISCVMCExpr::VariantKind VK; |
| 299 | if (!isImm()) |
| 300 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 301 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 302 | // Given only Imm, ensuring that the actually specified constant is either |
| 303 | // a signed or unsigned 64-bit number is unfortunately impossible. |
| 304 | bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm); |
| 305 | return IsConstantImm && IsInRange && VK == RISCVMCExpr::VK_RISCV_None; |
| 306 | } |
| 307 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 308 | bool isUImmLog2XLen() const { |
| 309 | int64_t Imm; |
| 310 | RISCVMCExpr::VariantKind VK; |
| 311 | if (!isImm()) |
| 312 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 313 | if (!evaluateConstantImm(getImm(), Imm, VK) || |
| 314 | VK != RISCVMCExpr::VK_RISCV_None) |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 315 | return false; |
| 316 | return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm); |
| 317 | } |
| 318 | |
Alex Bradbury | 0ad4c26 | 2017-12-15 10:20:51 +0000 | [diff] [blame] | 319 | bool isUImmLog2XLenNonZero() const { |
| 320 | int64_t Imm; |
| 321 | RISCVMCExpr::VariantKind VK; |
| 322 | if (!isImm()) |
| 323 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 324 | if (!evaluateConstantImm(getImm(), Imm, VK) || |
| 325 | VK != RISCVMCExpr::VK_RISCV_None) |
Alex Bradbury | 0ad4c26 | 2017-12-15 10:20:51 +0000 | [diff] [blame] | 326 | return false; |
| 327 | if (Imm == 0) |
| 328 | return false; |
| 329 | return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm); |
| 330 | } |
| 331 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 332 | bool isUImm5() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 333 | int64_t Imm; |
| 334 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 335 | if (!isImm()) |
| 336 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 337 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 338 | return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 341 | bool isUImm5NonZero() const { |
| 342 | int64_t Imm; |
| 343 | RISCVMCExpr::VariantKind VK; |
| 344 | if (!isImm()) |
| 345 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 346 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 347 | return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) && |
| 348 | VK == RISCVMCExpr::VK_RISCV_None; |
| 349 | } |
| 350 | |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 351 | bool isSImm6() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 352 | if (!isImm()) |
| 353 | return false; |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 354 | RISCVMCExpr::VariantKind VK; |
| 355 | int64_t Imm; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 356 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Ana Pazos | 065b088 | 2018-09-13 18:37:23 +0000 | [diff] [blame] | 357 | return IsConstantImm && isInt<6>(Imm) && |
| 358 | VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 361 | bool isSImm6NonZero() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 362 | if (!isImm()) |
| 363 | return false; |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 364 | RISCVMCExpr::VariantKind VK; |
| 365 | int64_t Imm; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 366 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Ana Pazos | 065b088 | 2018-09-13 18:37:23 +0000 | [diff] [blame] | 367 | return IsConstantImm && isInt<6>(Imm) && (Imm != 0) && |
| 368 | VK == RISCVMCExpr::VK_RISCV_None; |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 371 | bool isCLUIImm() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 372 | if (!isImm()) |
| 373 | return false; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 374 | int64_t Imm; |
| 375 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 376 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 377 | return IsConstantImm && (Imm != 0) && |
| 378 | (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) && |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 379 | VK == RISCVMCExpr::VK_RISCV_None; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 382 | bool isUImm7Lsb00() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 383 | if (!isImm()) |
| 384 | return false; |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 385 | int64_t Imm; |
| 386 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 387 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 388 | return IsConstantImm && isShiftedUInt<5, 2>(Imm) && |
| 389 | VK == RISCVMCExpr::VK_RISCV_None; |
| 390 | } |
| 391 | |
| 392 | bool isUImm8Lsb00() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 393 | if (!isImm()) |
| 394 | return false; |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 395 | int64_t Imm; |
| 396 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 397 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 398 | return IsConstantImm && isShiftedUInt<6, 2>(Imm) && |
| 399 | VK == RISCVMCExpr::VK_RISCV_None; |
| 400 | } |
| 401 | |
| 402 | bool isUImm8Lsb000() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 403 | if (!isImm()) |
| 404 | return false; |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 405 | int64_t Imm; |
| 406 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 407 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 408 | return IsConstantImm && isShiftedUInt<5, 3>(Imm) && |
| 409 | VK == RISCVMCExpr::VK_RISCV_None; |
| 410 | } |
| 411 | |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 412 | bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); } |
| 413 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 414 | bool isUImm9Lsb000() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 415 | if (!isImm()) |
| 416 | return false; |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 417 | int64_t Imm; |
| 418 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 419 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 420 | return IsConstantImm && isShiftedUInt<6, 3>(Imm) && |
| 421 | VK == RISCVMCExpr::VK_RISCV_None; |
| 422 | } |
| 423 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 424 | bool isUImm10Lsb00NonZero() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 425 | if (!isImm()) |
| 426 | return false; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 427 | int64_t Imm; |
| 428 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 429 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 430 | return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) && |
| 431 | VK == RISCVMCExpr::VK_RISCV_None; |
| 432 | } |
| 433 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 434 | bool isSImm12() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 435 | RISCVMCExpr::VariantKind VK; |
| 436 | int64_t Imm; |
| 437 | bool IsValid; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 438 | if (!isImm()) |
| 439 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 440 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 441 | if (!IsConstantImm) |
| 442 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 443 | else |
| 444 | IsValid = isInt<12>(Imm); |
Alex Bradbury | 7d0e18d | 2018-09-18 15:13:29 +0000 | [diff] [blame] | 445 | return IsValid && ((IsConstantImm && VK == RISCVMCExpr::VK_RISCV_None) || |
Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 446 | VK == RISCVMCExpr::VK_RISCV_LO || |
| 447 | VK == RISCVMCExpr::VK_RISCV_PCREL_LO); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 450 | bool isSImm12Lsb0() const { return isBareSimmNLsb0<12>(); } |
| 451 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 452 | bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 453 | |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 454 | bool isSImm10Lsb0000NonZero() const { |
Ana Pazos | ecc65ed | 2018-08-24 23:47:49 +0000 | [diff] [blame] | 455 | if (!isImm()) |
| 456 | return false; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 457 | int64_t Imm; |
| 458 | RISCVMCExpr::VariantKind VK; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 459 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 460 | return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) && |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 461 | VK == RISCVMCExpr::VK_RISCV_None; |
| 462 | } |
| 463 | |
Alex Bradbury | 74340f1 | 2018-09-18 15:08:35 +0000 | [diff] [blame] | 464 | bool isUImm20LUI() const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 465 | RISCVMCExpr::VariantKind VK; |
| 466 | int64_t Imm; |
| 467 | bool IsValid; |
Alex Bradbury | 3c941e7 | 2017-10-19 16:22:51 +0000 | [diff] [blame] | 468 | if (!isImm()) |
| 469 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 470 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 74340f1 | 2018-09-18 15:08:35 +0000 | [diff] [blame] | 471 | if (!IsConstantImm) { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 472 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
Alex Bradbury | 74340f1 | 2018-09-18 15:08:35 +0000 | [diff] [blame] | 473 | return IsValid && VK == RISCVMCExpr::VK_RISCV_HI; |
| 474 | } else { |
| 475 | return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None || |
| 476 | VK == RISCVMCExpr::VK_RISCV_HI); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | bool isUImm20AUIPC() const { |
| 481 | RISCVMCExpr::VariantKind VK; |
| 482 | int64_t Imm; |
| 483 | bool IsValid; |
| 484 | if (!isImm()) |
| 485 | return false; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 486 | bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); |
Alex Bradbury | 74340f1 | 2018-09-18 15:08:35 +0000 | [diff] [blame] | 487 | if (!IsConstantImm) { |
| 488 | IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm); |
| 489 | return IsValid && VK == RISCVMCExpr::VK_RISCV_PCREL_HI; |
| 490 | } else { |
| 491 | return isUInt<20>(Imm) && (VK == RISCVMCExpr::VK_RISCV_None || |
| 492 | VK == RISCVMCExpr::VK_RISCV_PCREL_HI); |
| 493 | } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Alex Bradbury | 226f3ef | 2018-09-20 08:10:35 +0000 | [diff] [blame] | 496 | bool isSImm21Lsb0JAL() const { return isBareSimmNLsb0<21>(); } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 497 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 498 | /// getStartLoc - Gets location of the first token of this operand |
| 499 | SMLoc getStartLoc() const override { return StartLoc; } |
| 500 | /// getEndLoc - Gets location of the last token of this operand |
| 501 | SMLoc getEndLoc() const override { return EndLoc; } |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 502 | /// True if this operand is for an RV64 instruction |
| 503 | bool isRV64() const { return IsRV64; } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 504 | |
| 505 | unsigned getReg() const override { |
| 506 | assert(Kind == Register && "Invalid type access!"); |
| 507 | return Reg.RegNum; |
| 508 | } |
| 509 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 510 | StringRef getSysReg() const { |
| 511 | assert(Kind == SystemRegister && "Invalid access!"); |
| 512 | return StringRef(SysReg.Data, SysReg.Length); |
| 513 | } |
| 514 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 515 | const MCExpr *getImm() const { |
| 516 | assert(Kind == Immediate && "Invalid type access!"); |
| 517 | return Imm.Val; |
| 518 | } |
| 519 | |
| 520 | StringRef getToken() const { |
| 521 | assert(Kind == Token && "Invalid type access!"); |
| 522 | return Tok; |
| 523 | } |
| 524 | |
| 525 | void print(raw_ostream &OS) const override { |
| 526 | switch (Kind) { |
| 527 | case Immediate: |
| 528 | OS << *getImm(); |
| 529 | break; |
| 530 | case Register: |
| 531 | OS << "<register x"; |
| 532 | OS << getReg() << ">"; |
| 533 | break; |
| 534 | case Token: |
| 535 | OS << "'" << getToken() << "'"; |
| 536 | break; |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 537 | case SystemRegister: |
| 538 | OS << "<sysreg: " << getSysReg() << '>'; |
| 539 | break; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 543 | static std::unique_ptr<RISCVOperand> createToken(StringRef Str, SMLoc S, |
| 544 | bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 545 | auto Op = make_unique<RISCVOperand>(Token); |
| 546 | Op->Tok = Str; |
| 547 | Op->StartLoc = S; |
| 548 | Op->EndLoc = S; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 549 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 550 | return Op; |
| 551 | } |
| 552 | |
| 553 | static std::unique_ptr<RISCVOperand> createReg(unsigned RegNo, SMLoc S, |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 554 | SMLoc E, bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 555 | auto Op = make_unique<RISCVOperand>(Register); |
| 556 | Op->Reg.RegNum = RegNo; |
| 557 | Op->StartLoc = S; |
| 558 | Op->EndLoc = E; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 559 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 560 | return Op; |
| 561 | } |
| 562 | |
| 563 | static std::unique_ptr<RISCVOperand> createImm(const MCExpr *Val, SMLoc S, |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 564 | SMLoc E, bool IsRV64) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 565 | auto Op = make_unique<RISCVOperand>(Immediate); |
| 566 | Op->Imm.Val = Val; |
| 567 | Op->StartLoc = S; |
| 568 | Op->EndLoc = E; |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 569 | Op->IsRV64 = IsRV64; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 570 | return Op; |
| 571 | } |
| 572 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 573 | static std::unique_ptr<RISCVOperand> |
| 574 | createSysReg(StringRef Str, SMLoc S, unsigned Encoding, bool IsRV64) { |
| 575 | auto Op = make_unique<RISCVOperand>(SystemRegister); |
| 576 | Op->SysReg.Data = Str.data(); |
| 577 | Op->SysReg.Length = Str.size(); |
| 578 | Op->SysReg.Encoding = Encoding; |
| 579 | Op->StartLoc = S; |
| 580 | Op->IsRV64 = IsRV64; |
| 581 | return Op; |
| 582 | } |
| 583 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 584 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
| 585 | assert(Expr && "Expr shouldn't be null!"); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 586 | int64_t Imm = 0; |
Alex Bradbury | 96ed75d | 2018-09-20 11:40:43 +0000 | [diff] [blame] | 587 | RISCVMCExpr::VariantKind VK; |
| 588 | bool IsConstant = evaluateConstantImm(Expr, Imm, VK); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 589 | |
| 590 | if (IsConstant) |
| 591 | Inst.addOperand(MCOperand::createImm(Imm)); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 592 | else |
| 593 | Inst.addOperand(MCOperand::createExpr(Expr)); |
| 594 | } |
| 595 | |
| 596 | // Used by the TableGen Code |
| 597 | void addRegOperands(MCInst &Inst, unsigned N) const { |
| 598 | assert(N == 1 && "Invalid number of operands!"); |
| 599 | Inst.addOperand(MCOperand::createReg(getReg())); |
| 600 | } |
| 601 | |
| 602 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 603 | assert(N == 1 && "Invalid number of operands!"); |
| 604 | addExpr(Inst, getImm()); |
| 605 | } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 606 | |
| 607 | void addFenceArgOperands(MCInst &Inst, unsigned N) const { |
| 608 | assert(N == 1 && "Invalid number of operands!"); |
| 609 | // isFenceArg has validated the operand, meaning this cast is safe |
| 610 | auto SE = cast<MCSymbolRefExpr>(getImm()); |
| 611 | |
| 612 | unsigned Imm = 0; |
| 613 | for (char c : SE->getSymbol().getName()) { |
| 614 | switch (c) { |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 615 | default: |
| 616 | llvm_unreachable("FenceArg must contain only [iorw]"); |
| 617 | case 'i': Imm |= RISCVFenceField::I; break; |
| 618 | case 'o': Imm |= RISCVFenceField::O; break; |
| 619 | case 'r': Imm |= RISCVFenceField::R; break; |
| 620 | case 'w': Imm |= RISCVFenceField::W; break; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | Inst.addOperand(MCOperand::createImm(Imm)); |
| 624 | } |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 625 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 626 | void addCSRSystemRegisterOperands(MCInst &Inst, unsigned N) const { |
| 627 | assert(N == 1 && "Invalid number of operands!"); |
| 628 | Inst.addOperand(MCOperand::createImm(SysReg.Encoding)); |
| 629 | } |
| 630 | |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 631 | // Returns the rounding mode represented by this RISCVOperand. Should only |
| 632 | // be called after checking isFRMArg. |
| 633 | RISCVFPRndMode::RoundingMode getRoundingMode() const { |
| 634 | // isFRMArg has validated the operand, meaning this cast is safe. |
| 635 | auto SE = cast<MCSymbolRefExpr>(getImm()); |
| 636 | RISCVFPRndMode::RoundingMode FRM = |
| 637 | RISCVFPRndMode::stringToRoundingMode(SE->getSymbol().getName()); |
| 638 | assert(FRM != RISCVFPRndMode::Invalid && "Invalid rounding mode"); |
| 639 | return FRM; |
| 640 | } |
| 641 | |
| 642 | void addFRMArgOperands(MCInst &Inst, unsigned N) const { |
| 643 | assert(N == 1 && "Invalid number of operands!"); |
| 644 | Inst.addOperand(MCOperand::createImm(getRoundingMode())); |
| 645 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 646 | }; |
| 647 | } // end anonymous namespace. |
| 648 | |
| 649 | #define GET_REGISTER_MATCHER |
| 650 | #define GET_MATCHER_IMPLEMENTATION |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 651 | #include "RISCVGenAsmMatcher.inc" |
| 652 | |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 653 | // Return the matching FPR64 register for the given FPR32. |
| 654 | // FIXME: Ideally this function could be removed in favour of using |
| 655 | // information from TableGen. |
| 656 | unsigned convertFPR32ToFPR64(unsigned Reg) { |
| 657 | switch (Reg) { |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 658 | default: |
| 659 | llvm_unreachable("Not a recognised FPR32 register"); |
| 660 | case RISCV::F0_32: return RISCV::F0_64; |
| 661 | case RISCV::F1_32: return RISCV::F1_64; |
| 662 | case RISCV::F2_32: return RISCV::F2_64; |
| 663 | case RISCV::F3_32: return RISCV::F3_64; |
| 664 | case RISCV::F4_32: return RISCV::F4_64; |
| 665 | case RISCV::F5_32: return RISCV::F5_64; |
| 666 | case RISCV::F6_32: return RISCV::F6_64; |
| 667 | case RISCV::F7_32: return RISCV::F7_64; |
| 668 | case RISCV::F8_32: return RISCV::F8_64; |
| 669 | case RISCV::F9_32: return RISCV::F9_64; |
| 670 | case RISCV::F10_32: return RISCV::F10_64; |
| 671 | case RISCV::F11_32: return RISCV::F11_64; |
| 672 | case RISCV::F12_32: return RISCV::F12_64; |
| 673 | case RISCV::F13_32: return RISCV::F13_64; |
| 674 | case RISCV::F14_32: return RISCV::F14_64; |
| 675 | case RISCV::F15_32: return RISCV::F15_64; |
| 676 | case RISCV::F16_32: return RISCV::F16_64; |
| 677 | case RISCV::F17_32: return RISCV::F17_64; |
| 678 | case RISCV::F18_32: return RISCV::F18_64; |
| 679 | case RISCV::F19_32: return RISCV::F19_64; |
| 680 | case RISCV::F20_32: return RISCV::F20_64; |
| 681 | case RISCV::F21_32: return RISCV::F21_64; |
| 682 | case RISCV::F22_32: return RISCV::F22_64; |
| 683 | case RISCV::F23_32: return RISCV::F23_64; |
| 684 | case RISCV::F24_32: return RISCV::F24_64; |
| 685 | case RISCV::F25_32: return RISCV::F25_64; |
| 686 | case RISCV::F26_32: return RISCV::F26_64; |
| 687 | case RISCV::F27_32: return RISCV::F27_64; |
| 688 | case RISCV::F28_32: return RISCV::F28_64; |
| 689 | case RISCV::F29_32: return RISCV::F29_64; |
| 690 | case RISCV::F30_32: return RISCV::F30_64; |
| 691 | case RISCV::F31_32: return RISCV::F31_64; |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | unsigned RISCVAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp, |
| 696 | unsigned Kind) { |
| 697 | RISCVOperand &Op = static_cast<RISCVOperand &>(AsmOp); |
| 698 | if (!Op.isReg()) |
| 699 | return Match_InvalidOperand; |
| 700 | |
| 701 | unsigned Reg = Op.getReg(); |
| 702 | bool IsRegFPR32 = |
| 703 | RISCVMCRegisterClasses[RISCV::FPR32RegClassID].contains(Reg); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 704 | bool IsRegFPR32C = |
| 705 | RISCVMCRegisterClasses[RISCV::FPR32CRegClassID].contains(Reg); |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 706 | |
| 707 | // 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] | 708 | // register from FPR32 to FPR64 or FPR32C to FPR64C if necessary. |
| 709 | if ((IsRegFPR32 && Kind == MCK_FPR64) || |
| 710 | (IsRegFPR32C && Kind == MCK_FPR64C)) { |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 711 | Op.Reg.RegNum = convertFPR32ToFPR64(Reg); |
| 712 | return Match_Success; |
| 713 | } |
| 714 | return Match_InvalidOperand; |
| 715 | } |
| 716 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 717 | bool RISCVAsmParser::generateImmOutOfRangeError( |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 718 | OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper, |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 719 | Twine Msg = "immediate must be an integer in the range") { |
| 720 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 721 | return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]"); |
| 722 | } |
| 723 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 724 | bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, |
| 725 | OperandVector &Operands, |
| 726 | MCStreamer &Out, |
| 727 | uint64_t &ErrorInfo, |
| 728 | bool MatchingInlineAsm) { |
| 729 | MCInst Inst; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 730 | |
Ana Pazos | 6b34051b | 2018-08-30 19:43:19 +0000 | [diff] [blame] | 731 | auto Result = |
| 732 | MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); |
| 733 | switch (Result) { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 734 | default: |
| 735 | break; |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 736 | case Match_Success: |
| 737 | return processInstruction(Inst, IDLoc, Out); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 738 | case Match_MissingFeature: |
| 739 | return Error(IDLoc, "instruction use requires an option to be enabled"); |
| 740 | case Match_MnemonicFail: |
| 741 | return Error(IDLoc, "unrecognized instruction mnemonic"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 742 | case Match_InvalidOperand: { |
| 743 | SMLoc ErrorLoc = IDLoc; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 744 | if (ErrorInfo != ~0U) { |
| 745 | if (ErrorInfo >= Operands.size()) |
| 746 | return Error(ErrorLoc, "too few operands for instruction"); |
| 747 | |
| 748 | ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 749 | if (ErrorLoc == SMLoc()) |
| 750 | ErrorLoc = IDLoc; |
| 751 | } |
| 752 | return Error(ErrorLoc, "invalid operand for instruction"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 753 | } |
Ana Pazos | 6b34051b | 2018-08-30 19:43:19 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Handle the case when the error message is of specific type |
| 757 | // other than the generic Match_InvalidOperand, and the |
| 758 | // corresponding operand is missing. |
| 759 | if (Result > FIRST_TARGET_MATCH_RESULT_TY) { |
| 760 | SMLoc ErrorLoc = IDLoc; |
| 761 | if (ErrorInfo != ~0U && ErrorInfo >= Operands.size()) |
| 762 | return Error(ErrorLoc, "too few operands for instruction"); |
| 763 | } |
| 764 | |
| 765 | switch(Result) { |
| 766 | default: |
| 767 | break; |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 768 | case Match_InvalidImmXLen: |
| 769 | if (isRV64()) { |
| 770 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 771 | return Error(ErrorLoc, "operand must be a constant 64-bit integer"); |
| 772 | } |
| 773 | return generateImmOutOfRangeError(Operands, ErrorInfo, |
| 774 | std::numeric_limits<int32_t>::min(), |
| 775 | std::numeric_limits<uint32_t>::max()); |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 776 | case Match_InvalidUImmLog2XLen: |
| 777 | if (isRV64()) |
| 778 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1); |
| 779 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1); |
Alex Bradbury | 0ad4c26 | 2017-12-15 10:20:51 +0000 | [diff] [blame] | 780 | case Match_InvalidUImmLog2XLenNonZero: |
| 781 | if (isRV64()) |
| 782 | return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6) - 1); |
| 783 | return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5) - 1); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 784 | case Match_InvalidUImm5: |
| 785 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1); |
Alex Bradbury | 581d6b0 | 2017-12-13 09:41:21 +0000 | [diff] [blame] | 786 | case Match_InvalidSImm6: |
| 787 | return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 5), |
| 788 | (1 << 5) - 1); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 789 | case Match_InvalidSImm6NonZero: |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 790 | return generateImmOutOfRangeError( |
| 791 | Operands, ErrorInfo, -(1 << 5), (1 << 5) - 1, |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 792 | "immediate must be non-zero in the range"); |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 793 | case Match_InvalidCLUIImm: |
| 794 | return generateImmOutOfRangeError( |
| 795 | Operands, ErrorInfo, 1, (1 << 5) - 1, |
| 796 | "immediate must be in [0xfffe0, 0xfffff] or"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 797 | case Match_InvalidUImm7Lsb00: |
| 798 | return generateImmOutOfRangeError( |
| 799 | Operands, ErrorInfo, 0, (1 << 7) - 4, |
| 800 | "immediate must be a multiple of 4 bytes in the range"); |
| 801 | case Match_InvalidUImm8Lsb00: |
| 802 | return generateImmOutOfRangeError( |
| 803 | Operands, ErrorInfo, 0, (1 << 8) - 4, |
| 804 | "immediate must be a multiple of 4 bytes in the range"); |
| 805 | case Match_InvalidUImm8Lsb000: |
| 806 | return generateImmOutOfRangeError( |
| 807 | Operands, ErrorInfo, 0, (1 << 8) - 8, |
| 808 | "immediate must be a multiple of 8 bytes in the range"); |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 809 | case Match_InvalidSImm9Lsb0: |
| 810 | return generateImmOutOfRangeError( |
| 811 | Operands, ErrorInfo, -(1 << 8), (1 << 8) - 2, |
| 812 | "immediate must be a multiple of 2 bytes in the range"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 813 | case Match_InvalidUImm9Lsb000: |
| 814 | return generateImmOutOfRangeError( |
| 815 | Operands, ErrorInfo, 0, (1 << 9) - 8, |
| 816 | "immediate must be a multiple of 8 bytes in the range"); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 817 | case Match_InvalidUImm10Lsb00NonZero: |
| 818 | return generateImmOutOfRangeError( |
| 819 | Operands, ErrorInfo, 4, (1 << 10) - 4, |
| 820 | "immediate must be a multiple of 4 bytes in the range"); |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 821 | case Match_InvalidSImm10Lsb0000NonZero: |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 822 | return generateImmOutOfRangeError( |
| 823 | Operands, ErrorInfo, -(1 << 9), (1 << 9) - 16, |
Shiva Chen | b22c1d2 | 2018-02-02 02:43:23 +0000 | [diff] [blame] | 824 | "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] | 825 | case Match_InvalidSImm12: |
Alex Bradbury | 7d0e18d | 2018-09-18 15:13:29 +0000 | [diff] [blame] | 826 | return generateImmOutOfRangeError( |
| 827 | Operands, ErrorInfo, -(1 << 11), (1 << 11) - 1, |
| 828 | "operand must be a symbol with %lo/%pcrel_lo modifier or an integer in " |
| 829 | "the range"); |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 830 | case Match_InvalidSImm12Lsb0: |
| 831 | return generateImmOutOfRangeError( |
| 832 | Operands, ErrorInfo, -(1 << 11), (1 << 11) - 2, |
| 833 | "immediate must be a multiple of 2 bytes in the range"); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 834 | case Match_InvalidSImm13Lsb0: |
| 835 | return generateImmOutOfRangeError( |
| 836 | Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2, |
| 837 | "immediate must be a multiple of 2 bytes in the range"); |
Alex Bradbury | 74340f1 | 2018-09-18 15:08:35 +0000 | [diff] [blame] | 838 | case Match_InvalidUImm20LUI: |
| 839 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 20) - 1, |
| 840 | "operand must be a symbol with %hi() " |
| 841 | "modifier or an integer in the range"); |
| 842 | case Match_InvalidUImm20AUIPC: |
| 843 | return generateImmOutOfRangeError( |
| 844 | Operands, ErrorInfo, 0, (1 << 20) - 1, |
| 845 | "operand must be a symbol with %pcrel_hi() modifier or an integer in " |
| 846 | "the range"); |
Alex Bradbury | 226f3ef | 2018-09-20 08:10:35 +0000 | [diff] [blame] | 847 | case Match_InvalidSImm21Lsb0JAL: |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 848 | return generateImmOutOfRangeError( |
| 849 | Operands, ErrorInfo, -(1 << 20), (1 << 20) - 2, |
| 850 | "immediate must be a multiple of 2 bytes in the range"); |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 851 | case Match_InvalidCSRSystemRegister: { |
| 852 | return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1, |
| 853 | "operand must be a valid system register " |
| 854 | "name or an integer in the range"); |
| 855 | } |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 856 | case Match_InvalidFenceArg: { |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 857 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 858 | return Error( |
| 859 | ErrorLoc, |
| 860 | "operand must be formed of letters selected in-order from 'iorw'"); |
| 861 | } |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 862 | case Match_InvalidFRMArg: { |
| 863 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 864 | return Error( |
| 865 | ErrorLoc, |
| 866 | "operand must be a valid floating point rounding mode mnemonic"); |
| 867 | } |
Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 868 | case Match_InvalidBareSymbol: { |
| 869 | SMLoc ErrorLoc = ((RISCVOperand &)*Operands[ErrorInfo]).getStartLoc(); |
| 870 | return Error(ErrorLoc, "operand must be a bare symbol name"); |
| 871 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | llvm_unreachable("Unknown match type detected!"); |
| 875 | } |
| 876 | |
| 877 | bool RISCVAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, |
| 878 | SMLoc &EndLoc) { |
| 879 | const AsmToken &Tok = getParser().getTok(); |
| 880 | StartLoc = Tok.getLoc(); |
| 881 | EndLoc = Tok.getEndLoc(); |
| 882 | RegNo = 0; |
| 883 | StringRef Name = getLexer().getTok().getIdentifier(); |
| 884 | |
| 885 | if (!MatchRegisterName(Name) || !MatchRegisterAltName(Name)) { |
| 886 | getParser().Lex(); // Eat identifier token. |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | return Error(StartLoc, "invalid register name"); |
| 891 | } |
| 892 | |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 893 | OperandMatchResultTy RISCVAsmParser::parseRegister(OperandVector &Operands, |
| 894 | bool AllowParens) { |
| 895 | SMLoc FirstS = getLoc(); |
| 896 | bool HadParens = false; |
| 897 | AsmToken Buf[2]; |
| 898 | |
| 899 | // If this a parenthesised register name is allowed, parse it atomically |
| 900 | if (AllowParens && getLexer().is(AsmToken::LParen)) { |
| 901 | size_t ReadCount = getLexer().peekTokens(Buf); |
| 902 | if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) { |
| 903 | HadParens = true; |
| 904 | getParser().Lex(); // Eat '(' |
| 905 | } |
| 906 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 907 | |
| 908 | switch (getLexer().getKind()) { |
| 909 | default: |
| 910 | return MatchOperand_NoMatch; |
| 911 | case AsmToken::Identifier: |
| 912 | StringRef Name = getLexer().getTok().getIdentifier(); |
| 913 | unsigned RegNo = MatchRegisterName(Name); |
| 914 | if (RegNo == 0) { |
| 915 | RegNo = MatchRegisterAltName(Name); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 916 | if (RegNo == 0) { |
| 917 | if (HadParens) |
| 918 | getLexer().UnLex(Buf[0]); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 919 | return MatchOperand_NoMatch; |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 920 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 921 | } |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 922 | if (HadParens) |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 923 | Operands.push_back(RISCVOperand::createToken("(", FirstS, isRV64())); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 924 | SMLoc S = getLoc(); |
| 925 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 926 | getLexer().Lex(); |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 927 | Operands.push_back(RISCVOperand::createReg(RegNo, S, E, isRV64())); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 928 | } |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 929 | |
| 930 | if (HadParens) { |
| 931 | getParser().Lex(); // Eat ')' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 932 | Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64())); |
Alex Bradbury | 8c345c5 | 2017-11-09 15:00:03 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 935 | return MatchOperand_Success; |
| 936 | } |
| 937 | |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 938 | OperandMatchResultTy |
| 939 | RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) { |
| 940 | SMLoc S = getLoc(); |
| 941 | const MCExpr *Res; |
| 942 | |
| 943 | switch (getLexer().getKind()) { |
| 944 | default: |
| 945 | return MatchOperand_NoMatch; |
| 946 | case AsmToken::LParen: |
| 947 | case AsmToken::Minus: |
| 948 | case AsmToken::Plus: |
| 949 | case AsmToken::Integer: |
| 950 | case AsmToken::String: { |
| 951 | if (getParser().parseExpression(Res)) |
| 952 | return MatchOperand_ParseFail; |
| 953 | |
| 954 | auto *CE = dyn_cast<MCConstantExpr>(Res); |
| 955 | if (CE) { |
| 956 | int64_t Imm = CE->getValue(); |
| 957 | if (isUInt<12>(Imm)) { |
| 958 | auto SysReg = RISCVSysReg::lookupSysRegByEncoding(Imm); |
| 959 | // Accept an immediate representing a named or un-named Sys Reg |
| 960 | // if the range is valid, regardless of the required features. |
| 961 | Operands.push_back(RISCVOperand::createSysReg( |
| 962 | SysReg ? SysReg->Name : "", S, Imm, isRV64())); |
| 963 | return MatchOperand_Success; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | Twine Msg = "immediate must be an integer in the range"; |
| 968 | Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]"); |
| 969 | return MatchOperand_ParseFail; |
| 970 | } |
| 971 | case AsmToken::Identifier: { |
| 972 | StringRef Identifier; |
| 973 | if (getParser().parseIdentifier(Identifier)) |
| 974 | return MatchOperand_ParseFail; |
| 975 | |
| 976 | auto SysReg = RISCVSysReg::lookupSysRegByName(Identifier); |
| 977 | // Accept a named Sys Reg if the required features are present. |
| 978 | if (SysReg) { |
| 979 | if (!SysReg->haveRequiredFeatures(getSTI().getFeatureBits())) { |
| 980 | Error(S, "system register use requires an option to be enabled"); |
| 981 | return MatchOperand_ParseFail; |
| 982 | } |
| 983 | Operands.push_back(RISCVOperand::createSysReg( |
| 984 | Identifier, S, SysReg->Encoding, isRV64())); |
| 985 | return MatchOperand_Success; |
| 986 | } |
| 987 | |
| 988 | Twine Msg = "operand must be a valid system register name " |
| 989 | "or an integer in the range"; |
| 990 | Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]"); |
| 991 | return MatchOperand_ParseFail; |
| 992 | } |
| 993 | case AsmToken::Percent: { |
| 994 | // Discard operand with modifier. |
| 995 | Twine Msg = "immediate must be an integer in the range"; |
| 996 | Error(S, Msg + " [" + Twine(0) + ", " + Twine((1 << 12) - 1) + "]"); |
| 997 | return MatchOperand_ParseFail; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | return MatchOperand_NoMatch; |
| 1002 | } |
| 1003 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1004 | OperandMatchResultTy RISCVAsmParser::parseImmediate(OperandVector &Operands) { |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1005 | SMLoc S = getLoc(); |
| 1006 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
| 1007 | const MCExpr *Res; |
| 1008 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1009 | switch (getLexer().getKind()) { |
| 1010 | default: |
| 1011 | return MatchOperand_NoMatch; |
| 1012 | case AsmToken::LParen: |
| 1013 | case AsmToken::Minus: |
| 1014 | case AsmToken::Plus: |
| 1015 | case AsmToken::Integer: |
| 1016 | case AsmToken::String: |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1017 | if (getParser().parseExpression(Res)) |
| 1018 | return MatchOperand_ParseFail; |
| 1019 | break; |
| 1020 | case AsmToken::Identifier: { |
| 1021 | StringRef Identifier; |
| 1022 | if (getParser().parseIdentifier(Identifier)) |
| 1023 | return MatchOperand_ParseFail; |
| 1024 | MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier); |
| 1025 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | } |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 1028 | case AsmToken::Percent: |
| 1029 | return parseOperandWithModifier(Operands); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1030 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1031 | |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 1032 | Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64())); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 1033 | return MatchOperand_Success; |
| 1034 | } |
| 1035 | |
| 1036 | OperandMatchResultTy |
| 1037 | RISCVAsmParser::parseOperandWithModifier(OperandVector &Operands) { |
| 1038 | SMLoc S = getLoc(); |
| 1039 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
| 1040 | |
| 1041 | if (getLexer().getKind() != AsmToken::Percent) { |
| 1042 | Error(getLoc(), "expected '%' for operand modifier"); |
| 1043 | return MatchOperand_ParseFail; |
| 1044 | } |
| 1045 | |
| 1046 | getParser().Lex(); // Eat '%' |
| 1047 | |
| 1048 | if (getLexer().getKind() != AsmToken::Identifier) { |
| 1049 | Error(getLoc(), "expected valid identifier for operand modifier"); |
| 1050 | return MatchOperand_ParseFail; |
| 1051 | } |
| 1052 | StringRef Identifier = getParser().getTok().getIdentifier(); |
| 1053 | RISCVMCExpr::VariantKind VK = RISCVMCExpr::getVariantKindForName(Identifier); |
| 1054 | if (VK == RISCVMCExpr::VK_RISCV_Invalid) { |
| 1055 | Error(getLoc(), "unrecognized operand modifier"); |
| 1056 | return MatchOperand_ParseFail; |
| 1057 | } |
| 1058 | |
| 1059 | getParser().Lex(); // Eat the identifier |
| 1060 | if (getLexer().getKind() != AsmToken::LParen) { |
| 1061 | Error(getLoc(), "expected '('"); |
| 1062 | return MatchOperand_ParseFail; |
| 1063 | } |
| 1064 | getParser().Lex(); // Eat '(' |
| 1065 | |
| 1066 | const MCExpr *SubExpr; |
| 1067 | if (getParser().parseParenExpression(SubExpr, E)) { |
| 1068 | return MatchOperand_ParseFail; |
| 1069 | } |
| 1070 | |
| 1071 | const MCExpr *ModExpr = RISCVMCExpr::create(SubExpr, VK, getContext()); |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 1072 | Operands.push_back(RISCVOperand::createImm(ModExpr, S, E, isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1073 | return MatchOperand_Success; |
| 1074 | } |
| 1075 | |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 1076 | OperandMatchResultTy RISCVAsmParser::parseBareSymbol(OperandVector &Operands) { |
| 1077 | SMLoc S = getLoc(); |
| 1078 | SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1); |
| 1079 | const MCExpr *Res; |
| 1080 | |
| 1081 | if (getLexer().getKind() != AsmToken::Identifier) |
| 1082 | return MatchOperand_NoMatch; |
| 1083 | |
| 1084 | StringRef Identifier; |
| 1085 | if (getParser().parseIdentifier(Identifier)) |
| 1086 | return MatchOperand_ParseFail; |
| 1087 | |
| 1088 | MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier); |
| 1089 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
| 1090 | Operands.push_back(RISCVOperand::createImm(Res, S, E, isRV64())); |
| 1091 | return MatchOperand_Success; |
| 1092 | } |
| 1093 | |
Alex Bradbury | 226f3ef | 2018-09-20 08:10:35 +0000 | [diff] [blame] | 1094 | OperandMatchResultTy RISCVAsmParser::parseJALOffset(OperandVector &Operands) { |
| 1095 | // Parsing jal operands is fiddly due to the `jal foo` and `jal ra, foo` |
| 1096 | // both being acceptable forms. When parsing `jal ra, foo` this function |
| 1097 | // will be called for the `ra` register operand in an attempt to match the |
| 1098 | // single-operand alias. parseJALOffset must fail for this case. It would |
| 1099 | // seem logical to try parse the operand using parseImmediate and return |
| 1100 | // NoMatch if the next token is a comma (meaning we must be parsing a jal in |
| 1101 | // the second form rather than the first). We can't do this as there's no |
| 1102 | // way of rewinding the lexer state. Instead, return NoMatch if this operand |
| 1103 | // is an identifier and is followed by a comma. |
| 1104 | if (getLexer().is(AsmToken::Identifier) && |
| 1105 | getLexer().peekTok().is(AsmToken::Comma)) |
| 1106 | return MatchOperand_NoMatch; |
| 1107 | |
| 1108 | return parseImmediate(Operands); |
| 1109 | } |
| 1110 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1111 | OperandMatchResultTy |
| 1112 | RISCVAsmParser::parseMemOpBaseReg(OperandVector &Operands) { |
| 1113 | if (getLexer().isNot(AsmToken::LParen)) { |
| 1114 | Error(getLoc(), "expected '('"); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1115 | return MatchOperand_ParseFail; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1116 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1117 | |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1118 | getParser().Lex(); // Eat '(' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 1119 | Operands.push_back(RISCVOperand::createToken("(", getLoc(), isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (parseRegister(Operands) != MatchOperand_Success) { |
| 1122 | Error(getLoc(), "expected register"); |
| 1123 | return MatchOperand_ParseFail; |
| 1124 | } |
| 1125 | |
| 1126 | if (getLexer().isNot(AsmToken::RParen)) { |
| 1127 | Error(getLoc(), "expected ')'"); |
| 1128 | return MatchOperand_ParseFail; |
| 1129 | } |
| 1130 | |
| 1131 | getParser().Lex(); // Eat ')' |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 1132 | Operands.push_back(RISCVOperand::createToken(")", getLoc(), isRV64())); |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1133 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1134 | return MatchOperand_Success; |
| 1135 | } |
| 1136 | |
Alex Bradbury | cd8688a | 2018-04-25 17:25:29 +0000 | [diff] [blame] | 1137 | /// Looks at a token type and creates the relevant operand from this |
| 1138 | /// information, adding to Operands. If operand was parsed, returns false, else |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 1139 | /// true. |
| 1140 | bool RISCVAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) { |
| 1141 | // Check if the current operand has a custom associated parser, if so, try to |
| 1142 | // custom parse the operand, or fallback to the general approach. |
| 1143 | OperandMatchResultTy Result = |
| 1144 | MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true); |
| 1145 | if (Result == MatchOperand_Success) |
| 1146 | return false; |
| 1147 | if (Result == MatchOperand_ParseFail) |
| 1148 | return true; |
| 1149 | |
| 1150 | // Attempt to parse token as a register. |
| 1151 | if (parseRegister(Operands, true) == MatchOperand_Success) |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | |
| 1154 | // Attempt to parse token as an immediate |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1155 | if (parseImmediate(Operands) == MatchOperand_Success) { |
| 1156 | // Parse memory base register if present |
| 1157 | if (getLexer().is(AsmToken::LParen)) |
| 1158 | return parseMemOpBaseReg(Operands) != MatchOperand_Success; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1159 | return false; |
Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 1160 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Finally we have exhausted all options and must declare defeat. |
| 1163 | Error(getLoc(), "unknown operand"); |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | bool RISCVAsmParser::ParseInstruction(ParseInstructionInfo &Info, |
| 1168 | StringRef Name, SMLoc NameLoc, |
| 1169 | OperandVector &Operands) { |
Alex Bradbury | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame] | 1170 | // Ensure that if the instruction occurs when relaxation is enabled, |
| 1171 | // relocations are forced for the file. Ideally this would be done when there |
| 1172 | // is enough information to reliably determine if the instruction itself may |
| 1173 | // cause relaxations. Unfortunately instruction processing stage occurs in the |
| 1174 | // same pass as relocation emission, so it's too late to set a 'sticky bit' |
| 1175 | // for the entire file. |
| 1176 | if (getSTI().getFeatureBits()[RISCV::FeatureRelax]) { |
| 1177 | auto *Assembler = getTargetStreamer().getStreamer().getAssemblerPtr(); |
| 1178 | if (Assembler != nullptr) { |
| 1179 | RISCVAsmBackend &MAB = |
| 1180 | static_cast<RISCVAsmBackend &>(Assembler->getBackend()); |
| 1181 | MAB.setForceRelocs(); |
| 1182 | } |
| 1183 | } |
| 1184 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1185 | // First operand is token for instruction |
Alex Bradbury | a6e6248 | 2017-12-07 10:53:48 +0000 | [diff] [blame] | 1186 | Operands.push_back(RISCVOperand::createToken(Name, NameLoc, isRV64())); |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1187 | |
| 1188 | // If there are no more operands, then finish |
| 1189 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 1190 | return false; |
| 1191 | |
| 1192 | // Parse first operand |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 1193 | if (parseOperand(Operands, Name)) |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1194 | return true; |
| 1195 | |
| 1196 | // Parse until end of statement, consuming commas between operands |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1197 | unsigned OperandIdx = 1; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1198 | while (getLexer().is(AsmToken::Comma)) { |
| 1199 | // Consume comma token |
| 1200 | getLexer().Lex(); |
| 1201 | |
| 1202 | // Parse next operand |
Alex Bradbury | 68f73c1 | 2018-09-18 15:18:16 +0000 | [diff] [blame] | 1203 | if (parseOperand(Operands, Name)) |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1204 | return true; |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1205 | |
| 1206 | ++OperandIdx; |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1210 | SMLoc Loc = getLexer().getLoc(); |
| 1211 | getParser().eatToEndOfStatement(); |
| 1212 | return Error(Loc, "unexpected token"); |
| 1213 | } |
| 1214 | |
| 1215 | getParser().Lex(); // Consume the EndOfStatement. |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 1219 | bool RISCVAsmParser::classifySymbolRef(const MCExpr *Expr, |
| 1220 | RISCVMCExpr::VariantKind &Kind, |
| 1221 | int64_t &Addend) { |
| 1222 | Kind = RISCVMCExpr::VK_RISCV_None; |
| 1223 | Addend = 0; |
| 1224 | |
| 1225 | if (const RISCVMCExpr *RE = dyn_cast<RISCVMCExpr>(Expr)) { |
| 1226 | Kind = RE->getKind(); |
| 1227 | Expr = RE->getSubExpr(); |
| 1228 | } |
| 1229 | |
| 1230 | // It's a simple symbol reference or constant with no addend. |
| 1231 | if (isa<MCConstantExpr>(Expr) || isa<MCSymbolRefExpr>(Expr)) |
| 1232 | return true; |
| 1233 | |
| 1234 | const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr); |
| 1235 | if (!BE) |
| 1236 | return false; |
| 1237 | |
| 1238 | if (!isa<MCSymbolRefExpr>(BE->getLHS())) |
| 1239 | return false; |
| 1240 | |
| 1241 | if (BE->getOpcode() != MCBinaryExpr::Add && |
| 1242 | BE->getOpcode() != MCBinaryExpr::Sub) |
| 1243 | return false; |
| 1244 | |
| 1245 | // We are able to support the subtraction of two symbol references |
| 1246 | if (BE->getOpcode() == MCBinaryExpr::Sub && |
| 1247 | isa<MCSymbolRefExpr>(BE->getRHS())) |
| 1248 | return true; |
| 1249 | |
Hiroshi Inoue | 9ff2380 | 2018-04-09 04:37:53 +0000 | [diff] [blame] | 1250 | // See if the addend is a constant, otherwise there's more going |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 1251 | // on here than we can deal with. |
| 1252 | auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS()); |
| 1253 | if (!AddendExpr) |
| 1254 | return false; |
| 1255 | |
| 1256 | Addend = AddendExpr->getValue(); |
| 1257 | if (BE->getOpcode() == MCBinaryExpr::Sub) |
| 1258 | Addend = -Addend; |
| 1259 | |
| 1260 | // It's some symbol reference + a constant addend |
| 1261 | return Kind != RISCVMCExpr::VK_RISCV_Invalid; |
| 1262 | } |
| 1263 | |
Alex Bradbury | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 1264 | bool RISCVAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 1265 | // This returns false if this function recognizes the directive |
| 1266 | // regardless of whether it is successfully handles or reports an |
| 1267 | // error. Otherwise it returns true to give the generic parser a |
| 1268 | // chance at recognizing it. |
| 1269 | StringRef IDVal = DirectiveID.getString(); |
| 1270 | |
| 1271 | if (IDVal == ".option") |
| 1272 | return parseDirectiveOption(); |
| 1273 | |
| 1274 | return true; |
| 1275 | } |
| 1276 | |
| 1277 | bool RISCVAsmParser::parseDirectiveOption() { |
| 1278 | MCAsmParser &Parser = getParser(); |
| 1279 | // Get the option token. |
| 1280 | AsmToken Tok = Parser.getTok(); |
| 1281 | // At the moment only identifiers are supported. |
| 1282 | if (Tok.isNot(AsmToken::Identifier)) |
| 1283 | return Error(Parser.getTok().getLoc(), |
| 1284 | "unexpected token, expected identifier"); |
| 1285 | |
| 1286 | StringRef Option = Tok.getIdentifier(); |
| 1287 | |
| 1288 | if (Option == "rvc") { |
| 1289 | getTargetStreamer().emitDirectiveOptionRVC(); |
| 1290 | |
| 1291 | Parser.Lex(); |
| 1292 | if (Parser.getTok().isNot(AsmToken::EndOfStatement)) |
| 1293 | return Error(Parser.getTok().getLoc(), |
| 1294 | "unexpected token, expected end of statement"); |
| 1295 | |
| 1296 | setFeatureBits(RISCV::FeatureStdExtC, "c"); |
| 1297 | return false; |
| 1298 | } |
| 1299 | |
| 1300 | if (Option == "norvc") { |
| 1301 | getTargetStreamer().emitDirectiveOptionNoRVC(); |
| 1302 | |
| 1303 | Parser.Lex(); |
| 1304 | if (Parser.getTok().isNot(AsmToken::EndOfStatement)) |
| 1305 | return Error(Parser.getTok().getLoc(), |
| 1306 | "unexpected token, expected end of statement"); |
| 1307 | |
| 1308 | clearFeatureBits(RISCV::FeatureStdExtC, "c"); |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
Alex Bradbury | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame] | 1312 | if (Option == "relax") { |
| 1313 | getTargetStreamer().emitDirectiveOptionRelax(); |
| 1314 | |
| 1315 | Parser.Lex(); |
| 1316 | if (Parser.getTok().isNot(AsmToken::EndOfStatement)) |
| 1317 | return Error(Parser.getTok().getLoc(), |
| 1318 | "unexpected token, expected end of statement"); |
| 1319 | |
| 1320 | setFeatureBits(RISCV::FeatureRelax, "relax"); |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
| 1324 | if (Option == "norelax") { |
| 1325 | getTargetStreamer().emitDirectiveOptionNoRelax(); |
| 1326 | |
| 1327 | Parser.Lex(); |
| 1328 | if (Parser.getTok().isNot(AsmToken::EndOfStatement)) |
| 1329 | return Error(Parser.getTok().getLoc(), |
| 1330 | "unexpected token, expected end of statement"); |
| 1331 | |
| 1332 | clearFeatureBits(RISCV::FeatureRelax, "relax"); |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
Alex Bradbury | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 1336 | // Unknown option. |
| 1337 | Warning(Parser.getTok().getLoc(), |
Alex Bradbury | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame] | 1338 | "unknown option, expected 'rvc', 'norvc', 'relax' or 'norelax'"); |
Alex Bradbury | bca0c3c | 2018-05-11 17:30:28 +0000 | [diff] [blame] | 1339 | Parser.eatToEndOfStatement(); |
| 1340 | return false; |
| 1341 | } |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1342 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1343 | void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) { |
| 1344 | MCInst CInst; |
| 1345 | bool Res = compressInst(CInst, Inst, getSTI(), S.getContext()); |
| 1346 | CInst.setLoc(Inst.getLoc()); |
| 1347 | S.EmitInstruction((Res ? CInst : Inst), getSTI()); |
| 1348 | } |
| 1349 | |
| 1350 | void RISCVAsmParser::emitLoadImm(unsigned DestReg, int64_t Value, |
| 1351 | MCStreamer &Out) { |
Alex Bradbury | 22c091f | 2018-11-15 10:11:31 +0000 | [diff] [blame^] | 1352 | RISCVMatInt::InstSeq Seq; |
| 1353 | RISCVMatInt::generateInstSeq(Value, isRV64(), Seq); |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1354 | |
Alex Bradbury | 22c091f | 2018-11-15 10:11:31 +0000 | [diff] [blame^] | 1355 | unsigned SrcReg = RISCV::X0; |
| 1356 | for (RISCVMatInt::Inst &Inst : Seq) { |
| 1357 | if (Inst.Opc == RISCV::LUI) { |
| 1358 | emitToStreamer( |
| 1359 | Out, MCInstBuilder(RISCV::LUI).addReg(DestReg).addImm(Inst.Imm)); |
| 1360 | } else { |
| 1361 | emitToStreamer( |
| 1362 | Out, MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm( |
| 1363 | Inst.Imm)); |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Alex Bradbury | 22c091f | 2018-11-15 10:11:31 +0000 | [diff] [blame^] | 1366 | // Only the first instruction has X0 as its source. |
| 1367 | SrcReg = DestReg; |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1368 | } |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1371 | void RISCVAsmParser::emitLoadLocalAddress(MCInst &Inst, SMLoc IDLoc, |
| 1372 | MCStreamer &Out) { |
| 1373 | // The local load address pseudo-instruction "lla" is used in PC-relative |
| 1374 | // addressing of symbols: |
| 1375 | // lla rdest, symbol |
| 1376 | // expands to |
| 1377 | // TmpLabel: AUIPC rdest, %pcrel_hi(symbol) |
| 1378 | // ADDI rdest, %pcrel_lo(TmpLabel) |
| 1379 | MCContext &Ctx = getContext(); |
| 1380 | |
| 1381 | MCSymbol *TmpLabel = Ctx.createTempSymbol( |
| 1382 | "pcrel_hi", /* AlwaysAddSuffix */ true, /* CanBeUnnamed */ false); |
| 1383 | Out.EmitLabel(TmpLabel); |
| 1384 | |
| 1385 | MCOperand DestReg = Inst.getOperand(0); |
| 1386 | const RISCVMCExpr *Symbol = RISCVMCExpr::create( |
| 1387 | Inst.getOperand(1).getExpr(), RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx); |
| 1388 | |
Roger Ferrer Ibanez | c8f4dbb | 2018-08-14 08:30:42 +0000 | [diff] [blame] | 1389 | emitToStreamer( |
| 1390 | Out, MCInstBuilder(RISCV::AUIPC).addOperand(DestReg).addExpr(Symbol)); |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1391 | |
| 1392 | const MCExpr *RefToLinkTmpLabel = |
| 1393 | RISCVMCExpr::create(MCSymbolRefExpr::create(TmpLabel, Ctx), |
| 1394 | RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx); |
| 1395 | |
Roger Ferrer Ibanez | c8f4dbb | 2018-08-14 08:30:42 +0000 | [diff] [blame] | 1396 | emitToStreamer(Out, MCInstBuilder(RISCV::ADDI) |
| 1397 | .addOperand(DestReg) |
| 1398 | .addOperand(DestReg) |
| 1399 | .addExpr(RefToLinkTmpLabel)); |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1402 | bool RISCVAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc, |
| 1403 | MCStreamer &Out) { |
| 1404 | Inst.setLoc(IDLoc); |
| 1405 | |
| 1406 | if (Inst.getOpcode() == RISCV::PseudoLI) { |
| 1407 | auto Reg = Inst.getOperand(0).getReg(); |
| 1408 | int64_t Imm = Inst.getOperand(1).getImm(); |
| 1409 | // On RV32 the immediate here can either be a signed or an unsigned |
| 1410 | // 32-bit number. Sign extension has to be performed to ensure that Imm |
| 1411 | // represents the expected signed 64-bit number. |
| 1412 | if (!isRV64()) |
| 1413 | Imm = SignExtend64<32>(Imm); |
| 1414 | emitLoadImm(Reg, Imm, Out); |
| 1415 | return false; |
Roger Ferrer Ibanez | 577a97e | 2018-08-09 07:08:20 +0000 | [diff] [blame] | 1416 | } else if (Inst.getOpcode() == RISCV::PseudoLLA) { |
| 1417 | emitLoadLocalAddress(Inst, IDLoc, Out); |
| 1418 | return false; |
Alex Bradbury | 6a4b544 | 2018-06-07 15:35:47 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | emitToStreamer(Out, Inst); |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
Alex Bradbury | 04f06d9 | 2017-08-08 14:43:36 +0000 | [diff] [blame] | 1425 | extern "C" void LLVMInitializeRISCVAsmParser() { |
| 1426 | RegisterMCAsmParser<RISCVAsmParser> X(getTheRISCV32Target()); |
| 1427 | RegisterMCAsmParser<RISCVAsmParser> Y(getTheRISCV64Target()); |
| 1428 | } |