blob: 15943ba42156fb9230b7277f8e03fe2a69b71957 [file] [log] [blame]
Alex Bradbury8ab4a962017-09-17 14:36:28 +00001//===-- RISCVDisassembler.cpp - Disassembler for RISCV --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Bradbury8ab4a962017-09-17 14:36:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the RISCVDisassembler class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MCTargetDesc/RISCVMCTargetDesc.h"
Richard Trieu51fc56d2019-05-15 00:24:15 +000014#include "TargetInfo/RISCVTargetInfo.h"
Ana Pazos9d6c5532018-10-04 21:50:54 +000015#include "Utils/RISCVBaseInfo.h"
Luis Marquesfa06e952019-08-16 14:27:50 +000016#include "llvm/CodeGen/Register.h"
Alex Bradbury8ab4a962017-09-17 14:36:28 +000017#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
26using namespace llvm;
27
28#define DEBUG_TYPE "riscv-disassembler"
29
30typedef MCDisassembler::DecodeStatus DecodeStatus;
31
32namespace {
33class RISCVDisassembler : public MCDisassembler {
34
35public:
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
46static MCDisassembler *createRISCVDisassembler(const Target &T,
47 const MCSubtargetInfo &STI,
48 MCContext &Ctx) {
49 return new RISCVDisassembler(STI, Ctx);
50}
51
Tom Stellard4b0b2612019-06-11 03:21:13 +000052extern "C" void LLVMInitializeRISCVDisassembler() {
Alex Bradbury8ab4a962017-09-17 14:36:28 +000053 // Register the disassembler for each target.
54 TargetRegistry::RegisterMCDisassembler(getTheRISCV32Target(),
55 createRISCVDisassembler);
56 TargetRegistry::RegisterMCDisassembler(getTheRISCV64Target(),
57 createRISCVDisassembler);
58}
59
Alex Bradbury8ab4a962017-09-17 14:36:28 +000060static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, uint64_t RegNo,
61 uint64_t Address,
62 const void *Decoder) {
Alex Bradburydab1f6f2019-03-22 11:21:40 +000063 const FeatureBitset &FeatureBits =
64 static_cast<const MCDisassembler *>(Decoder)
65 ->getSubtargetInfo()
66 .getFeatureBits();
67 bool IsRV32E = FeatureBits[RISCV::FeatureRV32E];
68
Luis Marquesaae97bf2019-09-27 15:49:10 +000069 if (RegNo >= 32 || (IsRV32E && RegNo >= 16))
Alex Bradbury60714f92017-12-13 09:32:55 +000070 return MCDisassembler::Fail;
Alex Bradbury8ab4a962017-09-17 14:36:28 +000071
Luis Marquesaae97bf2019-09-27 15:49:10 +000072 Register Reg = RISCV::X0 + RegNo;
Alex Bradbury60714f92017-12-13 09:32:55 +000073 Inst.addOperand(MCOperand::createReg(Reg));
74 return MCDisassembler::Success;
Alex Bradbury8ab4a962017-09-17 14:36:28 +000075}
76
Alex Bradbury0d6cf902017-12-07 10:26:05 +000077static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, uint64_t RegNo,
78 uint64_t Address,
79 const void *Decoder) {
Luis Marquesaae97bf2019-09-27 15:49:10 +000080 if (RegNo >= 32)
Alex Bradbury0d6cf902017-12-07 10:26:05 +000081 return MCDisassembler::Fail;
82
Luis Marquesaae97bf2019-09-27 15:49:10 +000083 Register Reg = RISCV::F0_F + RegNo;
Alex Bradbury0d6cf902017-12-07 10:26:05 +000084 Inst.addOperand(MCOperand::createReg(Reg));
85 return MCDisassembler::Success;
86}
87
Alex Bradbury60714f92017-12-13 09:32:55 +000088static DecodeStatus DecodeFPR32CRegisterClass(MCInst &Inst, uint64_t RegNo,
89 uint64_t Address,
90 const void *Decoder) {
Luis Marquesaae97bf2019-09-27 15:49:10 +000091 if (RegNo >= 8) {
Alex Bradbury60714f92017-12-13 09:32:55 +000092 return MCDisassembler::Fail;
93 }
Luis Marquesaae97bf2019-09-27 15:49:10 +000094 Register Reg = RISCV::F8_F + RegNo;
Alex Bradbury60714f92017-12-13 09:32:55 +000095 Inst.addOperand(MCOperand::createReg(Reg));
96 return MCDisassembler::Success;
97}
98
Alex Bradbury7bc2a952017-12-07 10:46:23 +000099static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, uint64_t RegNo,
100 uint64_t Address,
101 const void *Decoder) {
Luis Marquesaae97bf2019-09-27 15:49:10 +0000102 if (RegNo >= 32)
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000103 return MCDisassembler::Fail;
104
Luis Marquesaae97bf2019-09-27 15:49:10 +0000105 Register Reg = RISCV::F0_D + RegNo;
Alex Bradbury7bc2a952017-12-07 10:46:23 +0000106 Inst.addOperand(MCOperand::createReg(Reg));
107 return MCDisassembler::Success;
108}
109
Alex Bradbury60714f92017-12-13 09:32:55 +0000110static DecodeStatus DecodeFPR64CRegisterClass(MCInst &Inst, uint64_t RegNo,
111 uint64_t Address,
112 const void *Decoder) {
Luis Marquesaae97bf2019-09-27 15:49:10 +0000113 if (RegNo >= 8) {
Alex Bradbury60714f92017-12-13 09:32:55 +0000114 return MCDisassembler::Fail;
115 }
Luis Marquesaae97bf2019-09-27 15:49:10 +0000116 Register Reg = RISCV::F8_D + RegNo;
Alex Bradbury60714f92017-12-13 09:32:55 +0000117 Inst.addOperand(MCOperand::createReg(Reg));
118 return MCDisassembler::Success;
119}
120
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000121static DecodeStatus DecodeGPRNoX0RegisterClass(MCInst &Inst, uint64_t RegNo,
122 uint64_t Address,
123 const void *Decoder) {
Alex Bradbury60714f92017-12-13 09:32:55 +0000124 if (RegNo == 0) {
125 return MCDisassembler::Fail;
126 }
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000127
Alex Bradbury60714f92017-12-13 09:32:55 +0000128 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
129}
130
131static 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 Bradbury9f6aec42017-12-07 12:50:32 +0000139}
140
141static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
142 uint64_t Address,
143 const void *Decoder) {
Luis Marquesaae97bf2019-09-27 15:49:10 +0000144 if (RegNo >= 8)
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000145 return MCDisassembler::Fail;
146
Luis Marquesaae97bf2019-09-27 15:49:10 +0000147 Register Reg = RISCV::X8 + RegNo;
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000148 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.
154static void addImplySP(MCInst &Inst, int64_t Address, const void *Decoder) {
Alex Bradbury19c93142017-12-13 09:57:25 +0000155 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 Bradbury60714f92017-12-13 09:32:55 +0000162 DecodeGPRRegisterClass(Inst, 2, Address, Decoder);
163 }
Alex Bradbury19c93142017-12-13 09:57:25 +0000164 if (Inst.getOpcode() == RISCV::C_ADDI16SP) {
Alex Bradbury60714f92017-12-13 09:32:55 +0000165 DecodeGPRRegisterClass(Inst, 2, Address, Decoder);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000166 DecodeGPRRegisterClass(Inst, 2, Address, Decoder);
167 }
168}
169
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000170template <unsigned N>
171static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
172 int64_t Address, const void *Decoder) {
173 assert(isUInt<N>(Imm) && "Invalid immediate");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000174 addImplySP(Inst, Address, Decoder);
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000175 Inst.addOperand(MCOperand::createImm(Imm));
176 return MCDisassembler::Success;
177}
178
179template <unsigned N>
Ana Pazosb0799dd2018-09-13 18:21:19 +0000180static 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
188template <unsigned N>
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000189static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
190 int64_t Address, const void *Decoder) {
191 assert(isUInt<N>(Imm) && "Invalid immediate");
Alex Bradbury60714f92017-12-13 09:32:55 +0000192 addImplySP(Inst, Address, Decoder);
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000193 // 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
198template <unsigned N>
Ana Pazosb0799dd2018-09-13 18:21:19 +0000199static 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
207template <unsigned N>
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000208static 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 Chen7c172422018-02-22 15:02:28 +0000219static 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 Pazosb2ed11a2018-09-07 18:43:43 +0000230static 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 Marquesc3bf3d12019-08-21 14:00:58 +0000241static DecodeStatus decodeRVCInstrSImm(MCInst &Inst, unsigned Insn,
242 uint64_t Address, const void *Decoder);
243
244static DecodeStatus decodeRVCInstrRdSImm(MCInst &Inst, unsigned Insn,
245 uint64_t Address, const void *Decoder);
246
247static DecodeStatus decodeRVCInstrRdRs1UImm(MCInst &Inst, unsigned Insn,
248 uint64_t Address,
249 const void *Decoder);
250
251static DecodeStatus decodeRVCInstrRdRs2(MCInst &Inst, unsigned Insn,
252 uint64_t Address, const void *Decoder);
253
254static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst &Inst, unsigned Insn,
255 uint64_t Address,
256 const void *Decoder);
257
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000258#include "RISCVGenDisassemblerTables.inc"
259
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000260static 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 Marques4f488b52019-08-21 21:11:37 +0000264 DecodeStatus Result = decodeSImmOperand<6>(Inst, SImm6, Address, Decoder);
265 (void)Result;
266 assert(Result == MCDisassembler::Success && "Invalid immediate");
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000267 return MCDisassembler::Success;
268}
269
270static 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 Marques4f488b52019-08-21 21:11:37 +0000276 DecodeStatus Result = decodeSImmOperand<6>(Inst, SImm6, Address, Decoder);
277 (void)Result;
278 assert(Result == MCDisassembler::Success && "Invalid immediate");
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000279 return MCDisassembler::Success;
280}
281
282static 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 Marques4f488b52019-08-21 21:11:37 +0000289 DecodeStatus Result = decodeUImmOperand<6>(Inst, UImm6, Address, Decoder);
290 (void)Result;
291 assert(Result == MCDisassembler::Success && "Invalid immediate");
Luis Marquesc3bf3d12019-08-21 14:00:58 +0000292 return MCDisassembler::Success;
293}
294
295static 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
304static 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 Bradbury8ab4a962017-09-17 14:36:28 +0000315DecodeStatus 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 Bradbury9f6aec42017-12-07 12:50:32 +0000320 // 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 Pazosb97d1892018-09-07 18:23:19 +0000327 if (Bytes.size() < 4) {
328 Size = 0;
329 return MCDisassembler::Fail;
330 }
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000331 Insn = support::endian::read32le(Bytes.data());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000332 LLVM_DEBUG(dbgs() << "Trying RISCV32 table :\n");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000333 Result = decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
334 Size = 4;
335 } else {
Ana Pazosb97d1892018-09-07 18:23:19 +0000336 if (Bytes.size() < 2) {
337 Size = 0;
338 return MCDisassembler::Fail;
339 }
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000340 Insn = support::endian::read16le(Bytes.data());
Alex Bradbury60714f92017-12-13 09:32:55 +0000341
342 if (!STI.getFeatureBits()[RISCV::Feature64Bit]) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000343 LLVM_DEBUG(
344 dbgs() << "Trying RISCV32Only_16 table (16-bit Instruction):\n");
Alex Bradbury60714f92017-12-13 09:32:55 +0000345 // 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 Zaghend34e60c2018-05-14 12:53:11 +0000354 LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n");
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000355 // Calling the auto-generated decoder function.
356 Result = decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI);
357 Size = 2;
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000358 }
359
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000360 return Result;
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000361}