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