Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 1 | //===-- RISCVDisassembler.cpp - Disassembler for RISCV --------------------===// |
| 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 | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the RISCVDisassembler class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "MCTargetDesc/RISCVMCTargetDesc.h" |
Richard Trieu | 51fc56d | 2019-05-15 00:24:15 +0000 | [diff] [blame] | 14 | #include "TargetInfo/RISCVTargetInfo.h" |
Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 15 | #include "Utils/RISCVBaseInfo.h" |
Luis Marques | fa06e95 | 2019-08-16 14:27:50 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Register.h" |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 19 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include "llvm/MC/MCRegisterInfo.h" |
| 22 | #include "llvm/MC/MCSubtargetInfo.h" |
| 23 | #include "llvm/Support/Endian.h" |
| 24 | #include "llvm/Support/TargetRegistry.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | #define DEBUG_TYPE "riscv-disassembler" |
| 29 | |
| 30 | typedef MCDisassembler::DecodeStatus DecodeStatus; |
| 31 | |
| 32 | namespace { |
| 33 | class RISCVDisassembler : public MCDisassembler { |
| 34 | |
| 35 | public: |
| 36 | RISCVDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) |
| 37 | : MCDisassembler(STI, Ctx) {} |
| 38 | |
| 39 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
| 40 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 41 | raw_ostream &VStream, |
| 42 | raw_ostream &CStream) const override; |
| 43 | }; |
| 44 | } // end anonymous namespace |
| 45 | |
| 46 | static MCDisassembler *createRISCVDisassembler(const Target &T, |
| 47 | const MCSubtargetInfo &STI, |
| 48 | MCContext &Ctx) { |
| 49 | return new RISCVDisassembler(STI, Ctx); |
| 50 | } |
| 51 | |
Tom Stellard | 4b0b261 | 2019-06-11 03:21:13 +0000 | [diff] [blame] | 52 | extern "C" void LLVMInitializeRISCVDisassembler() { |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 53 | // Register the disassembler for each target. |
| 54 | TargetRegistry::RegisterMCDisassembler(getTheRISCV32Target(), |
| 55 | createRISCVDisassembler); |
| 56 | TargetRegistry::RegisterMCDisassembler(getTheRISCV64Target(), |
| 57 | createRISCVDisassembler); |
| 58 | } |
| 59 | |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 60 | static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, uint64_t RegNo, |
| 61 | uint64_t Address, |
| 62 | const void *Decoder) { |
Alex Bradbury | dab1f6f | 2019-03-22 11:21:40 +0000 | [diff] [blame] | 63 | const FeatureBitset &FeatureBits = |
| 64 | static_cast<const MCDisassembler *>(Decoder) |
| 65 | ->getSubtargetInfo() |
| 66 | .getFeatureBits(); |
| 67 | bool IsRV32E = FeatureBits[RISCV::FeatureRV32E]; |
| 68 | |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 69 | if (RegNo >= 32 || (IsRV32E && RegNo >= 16)) |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 70 | return MCDisassembler::Fail; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 71 | |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 72 | Register Reg = RISCV::X0 + RegNo; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 73 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 74 | return MCDisassembler::Success; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 77 | static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, uint64_t RegNo, |
| 78 | uint64_t Address, |
| 79 | const void *Decoder) { |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 80 | if (RegNo >= 32) |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 81 | return MCDisassembler::Fail; |
| 82 | |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 83 | Register Reg = RISCV::F0_F + RegNo; |
Alex Bradbury | 0d6cf90 | 2017-12-07 10:26:05 +0000 | [diff] [blame] | 84 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 85 | return MCDisassembler::Success; |
| 86 | } |
| 87 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 88 | static DecodeStatus DecodeFPR32CRegisterClass(MCInst &Inst, uint64_t RegNo, |
| 89 | uint64_t Address, |
| 90 | const void *Decoder) { |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 91 | if (RegNo >= 8) { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 92 | return MCDisassembler::Fail; |
| 93 | } |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 94 | Register Reg = RISCV::F8_F + RegNo; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 95 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 96 | return MCDisassembler::Success; |
| 97 | } |
| 98 | |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 99 | static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, uint64_t RegNo, |
| 100 | uint64_t Address, |
| 101 | const void *Decoder) { |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 102 | if (RegNo >= 32) |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 103 | return MCDisassembler::Fail; |
| 104 | |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 105 | Register Reg = RISCV::F0_D + RegNo; |
Alex Bradbury | 7bc2a95 | 2017-12-07 10:46:23 +0000 | [diff] [blame] | 106 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 107 | return MCDisassembler::Success; |
| 108 | } |
| 109 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 110 | static DecodeStatus DecodeFPR64CRegisterClass(MCInst &Inst, uint64_t RegNo, |
| 111 | uint64_t Address, |
| 112 | const void *Decoder) { |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 113 | if (RegNo >= 8) { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 114 | return MCDisassembler::Fail; |
| 115 | } |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 116 | Register Reg = RISCV::F8_D + RegNo; |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 117 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 118 | return MCDisassembler::Success; |
| 119 | } |
| 120 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 121 | static DecodeStatus DecodeGPRNoX0RegisterClass(MCInst &Inst, uint64_t RegNo, |
| 122 | uint64_t Address, |
| 123 | const void *Decoder) { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 124 | if (RegNo == 0) { |
| 125 | return MCDisassembler::Fail; |
| 126 | } |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 127 | |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 128 | return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder); |
| 129 | } |
| 130 | |
| 131 | static DecodeStatus DecodeGPRNoX0X2RegisterClass(MCInst &Inst, uint64_t RegNo, |
| 132 | uint64_t Address, |
| 133 | const void *Decoder) { |
| 134 | if (RegNo == 2) { |
| 135 | return MCDisassembler::Fail; |
| 136 | } |
| 137 | |
| 138 | return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo, |
| 142 | uint64_t Address, |
| 143 | const void *Decoder) { |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 144 | if (RegNo >= 8) |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 145 | return MCDisassembler::Fail; |
| 146 | |
Luis Marques | aae97bf | 2019-09-27 15:49:10 +0000 | [diff] [blame] | 147 | Register Reg = RISCV::X8 + RegNo; |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 148 | Inst.addOperand(MCOperand::createReg(Reg)); |
| 149 | return MCDisassembler::Success; |
| 150 | } |
| 151 | |
| 152 | // Add implied SP operand for instructions *SP compressed instructions. The SP |
| 153 | // operand isn't explicitly encoded in the instruction. |
| 154 | static void addImplySP(MCInst &Inst, int64_t Address, const void *Decoder) { |
Alex Bradbury | 19c9314 | 2017-12-13 09:57:25 +0000 | [diff] [blame] | 155 | if (Inst.getOpcode() == RISCV::C_LWSP || Inst.getOpcode() == RISCV::C_SWSP || |
| 156 | Inst.getOpcode() == RISCV::C_LDSP || Inst.getOpcode() == RISCV::C_SDSP || |
| 157 | Inst.getOpcode() == RISCV::C_FLWSP || |
| 158 | Inst.getOpcode() == RISCV::C_FSWSP || |
| 159 | Inst.getOpcode() == RISCV::C_FLDSP || |
| 160 | Inst.getOpcode() == RISCV::C_FSDSP || |
| 161 | Inst.getOpcode() == RISCV::C_ADDI4SPN) { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 162 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
| 163 | } |
Alex Bradbury | 19c9314 | 2017-12-13 09:57:25 +0000 | [diff] [blame] | 164 | if (Inst.getOpcode() == RISCV::C_ADDI16SP) { |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 165 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 166 | DecodeGPRRegisterClass(Inst, 2, Address, Decoder); |
| 167 | } |
| 168 | } |
| 169 | |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 170 | template <unsigned N> |
| 171 | static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm, |
| 172 | int64_t Address, const void *Decoder) { |
| 173 | assert(isUInt<N>(Imm) && "Invalid immediate"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 174 | addImplySP(Inst, Address, Decoder); |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 175 | Inst.addOperand(MCOperand::createImm(Imm)); |
| 176 | return MCDisassembler::Success; |
| 177 | } |
| 178 | |
| 179 | template <unsigned N> |
Ana Pazos | b0799dd | 2018-09-13 18:21:19 +0000 | [diff] [blame] | 180 | static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint64_t Imm, |
| 181 | int64_t Address, |
| 182 | const void *Decoder) { |
| 183 | if (Imm == 0) |
| 184 | return MCDisassembler::Fail; |
| 185 | return decodeUImmOperand<N>(Inst, Imm, Address, Decoder); |
| 186 | } |
| 187 | |
| 188 | template <unsigned N> |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 189 | static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm, |
| 190 | int64_t Address, const void *Decoder) { |
| 191 | assert(isUInt<N>(Imm) && "Invalid immediate"); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 192 | addImplySP(Inst, Address, Decoder); |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 193 | // Sign-extend the number in the bottom N bits of Imm |
| 194 | Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm))); |
| 195 | return MCDisassembler::Success; |
| 196 | } |
| 197 | |
| 198 | template <unsigned N> |
Ana Pazos | b0799dd | 2018-09-13 18:21:19 +0000 | [diff] [blame] | 199 | static DecodeStatus decodeSImmNonZeroOperand(MCInst &Inst, uint64_t Imm, |
| 200 | int64_t Address, |
| 201 | const void *Decoder) { |
| 202 | if (Imm == 0) |
| 203 | return MCDisassembler::Fail; |
| 204 | return decodeSImmOperand<N>(Inst, Imm, Address, Decoder); |
| 205 | } |
| 206 | |
| 207 | template <unsigned N> |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 208 | static DecodeStatus decodeSImmOperandAndLsl1(MCInst &Inst, uint64_t Imm, |
| 209 | int64_t Address, |
| 210 | const void *Decoder) { |
| 211 | assert(isUInt<N>(Imm) && "Invalid immediate"); |
| 212 | // Sign-extend the number in the bottom N bits of Imm after accounting for |
| 213 | // the fact that the N bit immediate is stored in N-1 bits (the LSB is |
| 214 | // always zero) |
| 215 | Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm << 1))); |
| 216 | return MCDisassembler::Success; |
| 217 | } |
| 218 | |
Shiva Chen | 7c17242 | 2018-02-22 15:02:28 +0000 | [diff] [blame] | 219 | static DecodeStatus decodeCLUIImmOperand(MCInst &Inst, uint64_t Imm, |
| 220 | int64_t Address, |
| 221 | const void *Decoder) { |
| 222 | assert(isUInt<6>(Imm) && "Invalid immediate"); |
| 223 | if (Imm > 31) { |
| 224 | Imm = (SignExtend64<6>(Imm) & 0xfffff); |
| 225 | } |
| 226 | Inst.addOperand(MCOperand::createImm(Imm)); |
| 227 | return MCDisassembler::Success; |
| 228 | } |
| 229 | |
Ana Pazos | b2ed11a | 2018-09-07 18:43:43 +0000 | [diff] [blame] | 230 | static DecodeStatus decodeFRMArg(MCInst &Inst, uint64_t Imm, |
| 231 | int64_t Address, |
| 232 | const void *Decoder) { |
| 233 | assert(isUInt<3>(Imm) && "Invalid immediate"); |
| 234 | if (!llvm::RISCVFPRndMode::isValidRoundingMode(Imm)) |
| 235 | return MCDisassembler::Fail; |
| 236 | |
| 237 | Inst.addOperand(MCOperand::createImm(Imm)); |
| 238 | return MCDisassembler::Success; |
| 239 | } |
| 240 | |
Luis Marques | c3bf3d1 | 2019-08-21 14:00:58 +0000 | [diff] [blame] | 241 | static DecodeStatus decodeRVCInstrSImm(MCInst &Inst, unsigned Insn, |
| 242 | uint64_t Address, const void *Decoder); |
| 243 | |
| 244 | static DecodeStatus decodeRVCInstrRdSImm(MCInst &Inst, unsigned Insn, |
| 245 | uint64_t Address, const void *Decoder); |
| 246 | |
| 247 | static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst &Inst, unsigned Insn, |
| 248 | uint64_t Address, |
| 249 | const void *Decoder); |
| 250 | |
| 251 | static DecodeStatus decodeRVCInstrRdRs2(MCInst &Inst, unsigned Insn, |
| 252 | uint64_t Address, const void *Decoder); |
| 253 | |
| 254 | static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst &Inst, unsigned Insn, |
| 255 | uint64_t Address, |
| 256 | const void *Decoder); |
| 257 | |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 258 | #include "RISCVGenDisassemblerTables.inc" |
| 259 | |
Luis Marques | c3bf3d1 | 2019-08-21 14:00:58 +0000 | [diff] [blame] | 260 | static DecodeStatus decodeRVCInstrSImm(MCInst &Inst, unsigned Insn, |
| 261 | uint64_t Address, const void *Decoder) { |
| 262 | uint64_t SImm6 = |
| 263 | fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5); |
Luis Marques | 4f488b5 | 2019-08-21 21:11:37 +0000 | [diff] [blame] | 264 | DecodeStatus Result = decodeSImmOperand<6>(Inst, SImm6, Address, Decoder); |
| 265 | (void)Result; |
| 266 | assert(Result == MCDisassembler::Success && "Invalid immediate"); |
Luis Marques | c3bf3d1 | 2019-08-21 14:00:58 +0000 | [diff] [blame] | 267 | return MCDisassembler::Success; |
| 268 | } |
| 269 | |
| 270 | static DecodeStatus decodeRVCInstrRdSImm(MCInst &Inst, unsigned Insn, |
| 271 | uint64_t Address, |
| 272 | const void *Decoder) { |
| 273 | DecodeGPRRegisterClass(Inst, 0, Address, Decoder); |
| 274 | uint64_t SImm6 = |
| 275 | fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5); |
Luis Marques | 4f488b5 | 2019-08-21 21:11:37 +0000 | [diff] [blame] | 276 | DecodeStatus Result = decodeSImmOperand<6>(Inst, SImm6, Address, Decoder); |
| 277 | (void)Result; |
| 278 | assert(Result == MCDisassembler::Success && "Invalid immediate"); |
Luis Marques | c3bf3d1 | 2019-08-21 14:00:58 +0000 | [diff] [blame] | 279 | return MCDisassembler::Success; |
| 280 | } |
| 281 | |
| 282 | static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst &Inst, unsigned Insn, |
| 283 | uint64_t Address, |
| 284 | const void *Decoder) { |
| 285 | DecodeGPRRegisterClass(Inst, 0, Address, Decoder); |
| 286 | Inst.addOperand(Inst.getOperand(0)); |
| 287 | uint64_t UImm6 = |
| 288 | fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5); |
Luis Marques | 4f488b5 | 2019-08-21 21:11:37 +0000 | [diff] [blame] | 289 | DecodeStatus Result = decodeUImmOperand<6>(Inst, UImm6, Address, Decoder); |
| 290 | (void)Result; |
| 291 | assert(Result == MCDisassembler::Success && "Invalid immediate"); |
Luis Marques | c3bf3d1 | 2019-08-21 14:00:58 +0000 | [diff] [blame] | 292 | return MCDisassembler::Success; |
| 293 | } |
| 294 | |
| 295 | static DecodeStatus decodeRVCInstrRdRs2(MCInst &Inst, unsigned Insn, |
| 296 | uint64_t Address, const void *Decoder) { |
| 297 | unsigned Rd = fieldFromInstruction(Insn, 7, 5); |
| 298 | unsigned Rs2 = fieldFromInstruction(Insn, 2, 5); |
| 299 | DecodeGPRRegisterClass(Inst, Rd, Address, Decoder); |
| 300 | DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder); |
| 301 | return MCDisassembler::Success; |
| 302 | } |
| 303 | |
| 304 | static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst &Inst, unsigned Insn, |
| 305 | uint64_t Address, |
| 306 | const void *Decoder) { |
| 307 | unsigned Rd = fieldFromInstruction(Insn, 7, 5); |
| 308 | unsigned Rs2 = fieldFromInstruction(Insn, 2, 5); |
| 309 | DecodeGPRRegisterClass(Inst, Rd, Address, Decoder); |
| 310 | Inst.addOperand(Inst.getOperand(0)); |
| 311 | DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder); |
| 312 | return MCDisassembler::Success; |
| 313 | } |
| 314 | |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 315 | DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
| 316 | ArrayRef<uint8_t> Bytes, |
| 317 | uint64_t Address, |
| 318 | raw_ostream &OS, |
| 319 | raw_ostream &CS) const { |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 320 | // TODO: This will need modification when supporting instruction set |
| 321 | // extensions with instructions > 32-bits (up to 176 bits wide). |
| 322 | uint32_t Insn; |
| 323 | DecodeStatus Result; |
| 324 | |
| 325 | // It's a 32 bit instruction if bit 0 and 1 are 1. |
| 326 | if ((Bytes[0] & 0x3) == 0x3) { |
Ana Pazos | b97d189 | 2018-09-07 18:23:19 +0000 | [diff] [blame] | 327 | if (Bytes.size() < 4) { |
| 328 | Size = 0; |
| 329 | return MCDisassembler::Fail; |
| 330 | } |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 331 | Insn = support::endian::read32le(Bytes.data()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 332 | LLVM_DEBUG(dbgs() << "Trying RISCV32 table :\n"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 333 | Result = decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI); |
| 334 | Size = 4; |
| 335 | } else { |
Ana Pazos | b97d189 | 2018-09-07 18:23:19 +0000 | [diff] [blame] | 336 | if (Bytes.size() < 2) { |
| 337 | Size = 0; |
| 338 | return MCDisassembler::Fail; |
| 339 | } |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 340 | Insn = support::endian::read16le(Bytes.data()); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 341 | |
| 342 | if (!STI.getFeatureBits()[RISCV::Feature64Bit]) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 343 | LLVM_DEBUG( |
| 344 | dbgs() << "Trying RISCV32Only_16 table (16-bit Instruction):\n"); |
Alex Bradbury | 60714f9 | 2017-12-13 09:32:55 +0000 | [diff] [blame] | 345 | // Calling the auto-generated decoder function. |
| 346 | Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Insn, Address, |
| 347 | this, STI); |
| 348 | if (Result != MCDisassembler::Fail) { |
| 349 | Size = 2; |
| 350 | return Result; |
| 351 | } |
| 352 | } |
| 353 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 354 | LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n"); |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 355 | // Calling the auto-generated decoder function. |
| 356 | Result = decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI); |
| 357 | Size = 2; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 360 | return Result; |
Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 361 | } |