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