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