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