1//===-- RISCVDisassembler.cpp - Disassembler for RISC-V -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the RISCVDisassembler class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MCTargetDesc/RISCVBaseInfo.h"
14#include "MCTargetDesc/RISCVMCTargetDesc.h"
15#include "TargetInfo/RISCVTargetInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDecoder.h"
18#include "llvm/MC/MCDecoderOps.h"
19#include "llvm/MC/MCDisassembler/MCDisassembler.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/TargetRegistry.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/Endian.h"
27
28using namespace llvm;
29using namespace llvm::MCD;
30
31#define DEBUG_TYPE "riscv-disassembler"
32
33typedef MCDisassembler::DecodeStatus DecodeStatus;
34
35namespace {
36class RISCVDisassembler : public MCDisassembler {
37 std::unique_ptr<MCInstrInfo const> const MCII;
38
39public:
40 RISCVDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
41 MCInstrInfo const *MCII)
42 : MCDisassembler(STI, Ctx), MCII(MCII) {}
43
44 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
45 ArrayRef<uint8_t> Bytes, uint64_t Address,
46 raw_ostream &CStream) const override;
47
48private:
49 DecodeStatus getInstruction48(MCInst &Instr, uint64_t &Size,
50 ArrayRef<uint8_t> Bytes, uint64_t Address,
51 raw_ostream &CStream) const;
52
53 DecodeStatus getInstruction32(MCInst &Instr, uint64_t &Size,
54 ArrayRef<uint8_t> Bytes, uint64_t Address,
55 raw_ostream &CStream) const;
56 DecodeStatus getInstruction16(MCInst &Instr, uint64_t &Size,
57 ArrayRef<uint8_t> Bytes, uint64_t Address,
58 raw_ostream &CStream) const;
59};
60} // end anonymous namespace
61
62static MCDisassembler *createRISCVDisassembler(const Target &T,
63 const MCSubtargetInfo &STI,
64 MCContext &Ctx) {
65 return new RISCVDisassembler(STI, Ctx, T.createMCInstrInfo());
66}
67
68extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
69LLVMInitializeRISCVDisassembler() {
70 // Register the disassembler for each target.
71 TargetRegistry::RegisterMCDisassembler(T&: getTheRISCV32Target(),
72 Fn: createRISCVDisassembler);
73 TargetRegistry::RegisterMCDisassembler(T&: getTheRISCV64Target(),
74 Fn: createRISCVDisassembler);
75 TargetRegistry::RegisterMCDisassembler(T&: getTheRISCV32beTarget(),
76 Fn: createRISCVDisassembler);
77 TargetRegistry::RegisterMCDisassembler(T&: getTheRISCV64beTarget(),
78 Fn: createRISCVDisassembler);
79}
80
81template <unsigned FirstReg, unsigned NumRegsInClass, unsigned RVELimit = 0>
82static DecodeStatus DecodeSimpleRegisterClass(MCInst &Inst, uint32_t RegNo,
83 uint64_t Address,
84 const MCDisassembler *Decoder) {
85 bool CheckRVE = RVELimit != 0 &&
86 Decoder->getSubtargetInfo().hasFeature(Feature: RISCV::FeatureStdExtE);
87
88 if (RegNo >= NumRegsInClass || (CheckRVE && RegNo >= RVELimit))
89 return MCDisassembler::Fail;
90
91 MCRegister Reg = FirstReg + RegNo;
92 Inst.addOperand(Op: MCOperand::createReg(Reg));
93 return MCDisassembler::Success;
94}
95
96constexpr auto DecodeGPRRegisterClass =
97 DecodeSimpleRegisterClass<RISCV::X0, 32, /*RVELimit=*/16>;
98
99static DecodeStatus DecodeGPRX1X5RegisterClass(MCInst &Inst, uint32_t RegNo,
100 uint64_t Address,
101 const MCDisassembler *Decoder) {
102 MCRegister Reg = RISCV::X0 + RegNo;
103 if (Reg != RISCV::X1 && Reg != RISCV::X5)
104 return MCDisassembler::Fail;
105
106 Inst.addOperand(Op: MCOperand::createReg(Reg));
107 return MCDisassembler::Success;
108}
109
110static DecodeStatus DecodeGPRX1RegisterClass(MCInst &Inst,
111 const MCDisassembler *Decoder) {
112 Inst.addOperand(Op: MCOperand::createReg(Reg: RISCV::X1));
113 return MCDisassembler::Success;
114}
115
116static DecodeStatus DecodeSPRegisterClass(MCInst &Inst,
117 const MCDisassembler *Decoder) {
118 Inst.addOperand(Op: MCOperand::createReg(Reg: RISCV::X2));
119 return MCDisassembler::Success;
120}
121
122static DecodeStatus DecodeSPRegisterClass(MCInst &Inst, uint64_t RegNo,
123 uint32_t Address,
124 const MCDisassembler *Decoder) {
125 assert(RegNo == 2);
126 Inst.addOperand(Op: MCOperand::createReg(Reg: RISCV::X2));
127 return MCDisassembler::Success;
128}
129
130static DecodeStatus DecodeGPRX5RegisterClass(MCInst &Inst,
131 const MCDisassembler *Decoder) {
132 Inst.addOperand(Op: MCOperand::createReg(Reg: RISCV::X5));
133 return MCDisassembler::Success;
134}
135
136template <auto DecodeFn, auto PredicateFn>
137static DecodeStatus DecodeFilteredRegisterClass(MCInst &Inst, uint32_t RegNo,
138 uint64_t Address,
139 const MCDisassembler *Decoder) {
140 if (!PredicateFn(RegNo))
141 return MCDisassembler::Fail;
142 return DecodeFn(Inst, RegNo, Address, Decoder);
143}
144
145constexpr bool PredNoX0(uint32_t RegNo) { return RegNo != 0; }
146constexpr bool PredNoX2(uint32_t RegNo) { return RegNo != 2; }
147constexpr bool PredNoX31(uint32_t RegNo) { return RegNo != 31; }
148
149constexpr auto DecodeGPRNoX0RegisterClass =
150 DecodeFilteredRegisterClass<DecodeGPRRegisterClass, PredNoX0>;
151constexpr auto DecodeGPRNoX2RegisterClass =
152 DecodeFilteredRegisterClass<DecodeGPRRegisterClass, PredNoX2>;
153constexpr auto DecodeGPRNoX31RegisterClass =
154 DecodeFilteredRegisterClass<DecodeGPRRegisterClass, PredNoX31>;
155
156static DecodeStatus DecodeGPRPairRegisterClass(MCInst &Inst, uint32_t RegNo,
157 uint64_t Address,
158 const MCDisassembler *Decoder) {
159 if (RegNo >= 32 || RegNo % 2)
160 return MCDisassembler::Fail;
161
162 const RISCVDisassembler *Dis =
163 static_cast<const RISCVDisassembler *>(Decoder);
164 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
165 MCRegister Reg = RI->getMatchingSuperReg(
166 Reg: RISCV::X0 + RegNo, SubIdx: RISCV::sub_gpr_even,
167 RC: &RISCVMCRegisterClasses[RISCV::GPRPairRegClassID]);
168 Inst.addOperand(Op: MCOperand::createReg(Reg));
169 return MCDisassembler::Success;
170}
171
172constexpr auto DecodeGPRPairNoX0RegisterClass =
173 DecodeFilteredRegisterClass<DecodeGPRPairRegisterClass, PredNoX0>;
174
175static DecodeStatus DecodeGPRPairCRegisterClass(MCInst &Inst, uint32_t RegNo,
176 uint64_t Address,
177 const MCDisassembler *Decoder) {
178 if (RegNo >= 8 || RegNo % 2)
179 return MCDisassembler::Fail;
180
181 const RISCVDisassembler *Dis =
182 static_cast<const RISCVDisassembler *>(Decoder);
183 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
184 MCRegister Reg = RI->getMatchingSuperReg(
185 Reg: RISCV::X8 + RegNo, SubIdx: RISCV::sub_gpr_even,
186 RC: &RISCVMCRegisterClasses[RISCV::GPRPairCRegClassID]);
187 Inst.addOperand(Op: MCOperand::createReg(Reg));
188 return MCDisassembler::Success;
189}
190
191static DecodeStatus DecodeSR07RegisterClass(MCInst &Inst, uint32_t RegNo,
192 uint64_t Address,
193 const void *Decoder) {
194 if (RegNo >= 8)
195 return MCDisassembler::Fail;
196
197 MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV::X8) : (RegNo - 2 + RISCV::X18);
198 Inst.addOperand(Op: MCOperand::createReg(Reg));
199 return MCDisassembler::Success;
200}
201
202template <unsigned RegisterClass, unsigned NumRegsInClass, unsigned LMul>
203static DecodeStatus DecodeVectorRegisterClass(MCInst &Inst, uint32_t RegNo,
204 uint64_t Address,
205 const MCDisassembler *Decoder) {
206 if (RegNo >= NumRegsInClass || RegNo % LMul)
207 return MCDisassembler::Fail;
208
209 const RISCVDisassembler *Dis =
210 static_cast<const RISCVDisassembler *>(Decoder);
211 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
212 MCRegister Reg =
213 RI->getMatchingSuperReg(Reg: RISCV::V0 + RegNo, SubIdx: RISCV::sub_vrm1_0,
214 RC: &RISCVMCRegisterClasses[RegisterClass]);
215
216 Inst.addOperand(Op: MCOperand::createReg(Reg));
217 return MCDisassembler::Success;
218}
219
220static DecodeStatus DecodeTRM2RegisterClass(MCInst &Inst, uint32_t RegNo,
221 uint64_t Address,
222 const MCDisassembler *Decoder) {
223 if (RegNo > 15 || RegNo % 2)
224 return MCDisassembler::Fail;
225
226 MCRegister Reg = RISCV::T0 + RegNo;
227 Inst.addOperand(Op: MCOperand::createReg(Reg));
228 return MCDisassembler::Success;
229}
230
231static DecodeStatus DecodeTRM4RegisterClass(MCInst &Inst, uint32_t RegNo,
232 uint64_t Address,
233 const MCDisassembler *Decoder) {
234 if (RegNo > 15 || RegNo % 4)
235 return MCDisassembler::Fail;
236
237 MCRegister Reg = RISCV::T0 + RegNo;
238 Inst.addOperand(Op: MCOperand::createReg(Reg));
239 return MCDisassembler::Success;
240}
241
242static DecodeStatus DecodeYBNDSWImm(MCInst &Inst, uint64_t Imm, int64_t Address,
243 const MCDisassembler *Decoder) {
244 assert(isUInt<9>(Imm) && "Invalid immediate");
245 uint64_t Result;
246 if (Imm == 0) {
247 // If imm[8:0] == 0, result is 4096.
248 Result = 4096;
249 } else if (Imm <= 255) {
250 // If imm[8] == 0 and imm[7:0] != 0, result is imm[7:0].
251 Result = Imm;
252 } else {
253 uint64_t Imm7To0 = Imm & 0xFF;
254 if (Imm7To0 <= 31) {
255 // If imm[8] == 1 and imm[7:5] == 0 (i.e. imm[7:0] <= 31), result is
256 // `256 | (imm[3:0] << 4) | (imm[4] << 3)`.
257 Result = 256 + ((Imm & 0xF) << 4) + (((Imm >> 4) & 1) << 3);
258 } else {
259 // Otherwise, result is imm[7:0] << 4.
260 Result = Imm7To0 << 4;
261 }
262 }
263 Inst.addOperand(Op: MCOperand::createImm(Val: Result));
264 return MCDisassembler::Success;
265}
266
267static DecodeStatus decodeVMaskReg(MCInst &Inst, uint32_t RegNo,
268 uint64_t Address,
269 const MCDisassembler *Decoder) {
270 if (RegNo >= 2)
271 return MCDisassembler::Fail;
272
273 MCRegister Reg = (RegNo == 0) ? RISCV::V0 : RISCV::NoRegister;
274
275 Inst.addOperand(Op: MCOperand::createReg(Reg));
276 return MCDisassembler::Success;
277}
278
279static DecodeStatus decodeImmThreeOperand(MCInst &Inst,
280 const MCDisassembler *Decoder) {
281 Inst.addOperand(Op: MCOperand::createImm(Val: 3));
282 return MCDisassembler::Success;
283}
284
285static DecodeStatus decodeImmFourOperand(MCInst &Inst,
286 const MCDisassembler *Decoder) {
287 Inst.addOperand(Op: MCOperand::createImm(Val: 4));
288 return MCDisassembler::Success;
289}
290
291template <unsigned N>
292static DecodeStatus decodeUImmOperand(MCInst &Inst, uint32_t Imm,
293 int64_t Address,
294 const MCDisassembler *Decoder) {
295 assert(isUInt<N>(Imm) && "Invalid immediate");
296 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
297 return MCDisassembler::Success;
298}
299
300template <unsigned Width, unsigned LowerBound>
301static DecodeStatus decodeUImmOperandGE(MCInst &Inst, uint32_t Imm,
302 int64_t Address,
303 const MCDisassembler *Decoder) {
304 assert(isUInt<Width>(Imm) && "Invalid immediate");
305
306 if (Imm < LowerBound)
307 return MCDisassembler::Fail;
308
309 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
310 return MCDisassembler::Success;
311}
312
313template <unsigned Width, unsigned LowerBound>
314static DecodeStatus decodeUImmPlus1OperandGE(MCInst &Inst, uint32_t Imm,
315 int64_t Address,
316 const MCDisassembler *Decoder) {
317 assert(isUInt<Width>(Imm) && "Invalid immediate");
318
319 if ((Imm + 1) < LowerBound)
320 return MCDisassembler::Fail;
321
322 Inst.addOperand(Op: MCOperand::createImm(Val: Imm + 1));
323 return MCDisassembler::Success;
324}
325
326static DecodeStatus decodeUImmSlistOperand(MCInst &Inst, uint32_t Imm,
327 int64_t Address,
328 const MCDisassembler *Decoder) {
329 assert(isUInt<3>(Imm) && "Invalid Slist immediate");
330 const uint8_t Slist[] = {0, 1, 2, 4, 8, 16, 15, 31};
331 Inst.addOperand(Op: MCOperand::createImm(Val: Slist[Imm]));
332 return MCDisassembler::Success;
333}
334
335static DecodeStatus decodeUImmLog2XLenOperand(MCInst &Inst, uint32_t Imm,
336 int64_t Address,
337 const MCDisassembler *Decoder) {
338 assert(isUInt<6>(Imm) && "Invalid immediate");
339
340 if (!Decoder->getSubtargetInfo().hasFeature(Feature: RISCV::Feature64Bit) &&
341 !isUInt<5>(x: Imm))
342 return MCDisassembler::Fail;
343
344 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
345 return MCDisassembler::Success;
346}
347
348static DecodeStatus decodeUImm7EqXLenOperand(MCInst &Inst, uint32_t Imm,
349 int64_t Address,
350 const MCDisassembler *Decoder) {
351 assert(isUInt<7>(Imm) && "Invalid immediate");
352
353 uint32_t ExpectedValue =
354 Decoder->getSubtargetInfo().hasFeature(Feature: RISCV::Feature64Bit) ? 64 : 32;
355 if (Imm != ExpectedValue)
356 return MCDisassembler::Fail;
357
358 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
359 return MCDisassembler::Success;
360}
361
362template <unsigned N>
363static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
364 int64_t Address,
365 const MCDisassembler *Decoder) {
366 if (Imm == 0)
367 return MCDisassembler::Fail;
368 return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
369}
370
371static DecodeStatus
372decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address,
373 const MCDisassembler *Decoder) {
374 if (Imm == 0)
375 return MCDisassembler::Fail;
376 return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
377}
378
379template <unsigned N>
380static DecodeStatus decodeUImmPlus1Operand(MCInst &Inst, uint32_t Imm,
381 int64_t Address,
382 const MCDisassembler *Decoder) {
383 assert(isUInt<N>(Imm) && "Invalid immediate");
384 Inst.addOperand(Op: MCOperand::createImm(Val: Imm + 1));
385 return MCDisassembler::Success;
386}
387
388static DecodeStatus decodeImmZibiOperand(MCInst &Inst, uint32_t Imm,
389 int64_t Address,
390 const MCDisassembler *Decoder) {
391 assert(isUInt<5>(Imm) && "Invalid immediate");
392 Inst.addOperand(Op: MCOperand::createImm(Val: Imm ? Imm : -1LL));
393 return MCDisassembler::Success;
394}
395
396template <unsigned N>
397static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm,
398 int64_t Address,
399 const MCDisassembler *Decoder) {
400 assert(isUInt<N>(Imm) && "Invalid immediate");
401 // Sign-extend the number in the bottom N bits of Imm
402 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend64<N>(Imm)));
403 return MCDisassembler::Success;
404}
405
406template <unsigned N>
407static DecodeStatus decodeSImmNonZeroOperand(MCInst &Inst, uint32_t Imm,
408 int64_t Address,
409 const MCDisassembler *Decoder) {
410 if (Imm == 0)
411 return MCDisassembler::Fail;
412 return decodeSImmOperand<N>(Inst, Imm, Address, Decoder);
413}
414
415template <unsigned T, unsigned N>
416static DecodeStatus decodeSImmOperandAndLslN(MCInst &Inst, uint32_t Imm,
417 int64_t Address,
418 const MCDisassembler *Decoder) {
419 assert(isUInt<T - N + 1>(Imm) && "Invalid immediate");
420 // Sign-extend the number in the bottom T bits of Imm after accounting for
421 // the fact that the T bit immediate is stored in T-N bits (the LSB is
422 // always zero)
423 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend64<T>(Imm << N)));
424 return MCDisassembler::Success;
425}
426
427static DecodeStatus decodeCLUIImmOperand(MCInst &Inst, uint32_t Imm,
428 int64_t Address,
429 const MCDisassembler *Decoder) {
430 assert(isUInt<6>(Imm) && "Invalid immediate");
431 if (Imm == 0)
432 return MCDisassembler::Fail;
433 Imm = SignExtend64<6>(x: Imm) & 0xfffff;
434 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
435 return MCDisassembler::Success;
436}
437
438static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address,
439 const MCDisassembler *Decoder) {
440 assert(isUInt<3>(Imm) && "Invalid immediate");
441 if (!llvm::RISCVFPRndMode::isValidRoundingMode(Mode: Imm))
442 return MCDisassembler::Fail;
443
444 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
445 return MCDisassembler::Success;
446}
447
448static DecodeStatus decodeZcmpRlist(MCInst &Inst, uint32_t Imm,
449 uint64_t Address,
450 const MCDisassembler *Decoder) {
451 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(Feature: RISCV::FeatureStdExtE);
452 if (Imm < RISCVZC::RA || (IsRVE && Imm >= RISCVZC::RA_S0_S2))
453 return MCDisassembler::Fail;
454 Inst.addOperand(Op: MCOperand::createImm(Val: Imm));
455 return MCDisassembler::Success;
456}
457
458static DecodeStatus decodeXqccmpRlistS0(MCInst &Inst, uint32_t Imm,
459 uint64_t Address,
460 const MCDisassembler *Decoder) {
461 if (Imm < RISCVZC::RA_S0)
462 return MCDisassembler::Fail;
463 return decodeZcmpRlist(Inst, Imm, Address, Decoder);
464}
465
466#include "RISCVGenDisassemblerTables.inc"
467
468namespace {
469
470struct DecoderListEntry {
471 const uint8_t *Table;
472 FeatureBitset ContainedFeatures;
473 const char *Desc;
474
475 bool haveContainedFeatures(const FeatureBitset &ActiveFeatures) const {
476 return ContainedFeatures.none() ||
477 (ContainedFeatures & ActiveFeatures).any();
478 }
479};
480
481} // end anonymous namespace
482
483static constexpr FeatureBitset XCVFeatureGroup = {
484 RISCV::FeatureVendorXCVbitmanip, RISCV::FeatureVendorXCVelw,
485 RISCV::FeatureVendorXCVmac, RISCV::FeatureVendorXCVmem,
486 RISCV::FeatureVendorXCValu, RISCV::FeatureVendorXCVsimd,
487 RISCV::FeatureVendorXCVbi};
488
489static constexpr FeatureBitset XqciFeatureGroup = {
490 RISCV::FeatureVendorXqcia, RISCV::FeatureVendorXqciac,
491 RISCV::FeatureVendorXqcibi, RISCV::FeatureVendorXqcibm,
492 RISCV::FeatureVendorXqcicli, RISCV::FeatureVendorXqcicm,
493 RISCV::FeatureVendorXqcics, RISCV::FeatureVendorXqcicsr,
494 RISCV::FeatureVendorXqciint, RISCV::FeatureVendorXqciio,
495 RISCV::FeatureVendorXqcilb, RISCV::FeatureVendorXqcili,
496 RISCV::FeatureVendorXqcilia, RISCV::FeatureVendorXqcilo,
497 RISCV::FeatureVendorXqcilsm, RISCV::FeatureVendorXqcisim,
498 RISCV::FeatureVendorXqcisls, RISCV::FeatureVendorXqcisync,
499};
500
501static constexpr FeatureBitset XSfVectorGroup = {
502 RISCV::FeatureVendorXSfvcp, RISCV::FeatureVendorXSfvqmaccdod,
503 RISCV::FeatureVendorXSfvqmaccqoq, RISCV::FeatureVendorXSfvfwmaccqqq,
504 RISCV::FeatureVendorXSfvfnrclipxfqf, RISCV::FeatureVendorXSfmmbase,
505 RISCV::FeatureVendorXSfvfexpa, RISCV::FeatureVendorXSfvfexpa64e,
506 RISCV::FeatureVendorXSfvfbfexp16e, RISCV::FeatureVendorXSfvfexp16e,
507 RISCV::FeatureVendorXSfvfexp32e};
508static constexpr FeatureBitset XSfSystemGroup = {
509 RISCV::FeatureVendorXSiFivecdiscarddlone,
510 RISCV::FeatureVendorXSiFivecflushdlone,
511};
512
513static constexpr FeatureBitset XMIPSGroup = {
514 RISCV::FeatureVendorXMIPSLSP,
515 RISCV::FeatureVendorXMIPSCMov,
516 RISCV::FeatureVendorXMIPSCBOP,
517 RISCV::FeatureVendorXMIPSEXECTL,
518};
519
520static constexpr FeatureBitset XTHeadGroup = {
521 RISCV::FeatureVendorXTHeadBa, RISCV::FeatureVendorXTHeadBb,
522 RISCV::FeatureVendorXTHeadBs, RISCV::FeatureVendorXTHeadCondMov,
523 RISCV::FeatureVendorXTHeadCmo, RISCV::FeatureVendorXTHeadFMemIdx,
524 RISCV::FeatureVendorXTHeadMac, RISCV::FeatureVendorXTHeadMemIdx,
525 RISCV::FeatureVendorXTHeadMemPair, RISCV::FeatureVendorXTHeadSync,
526 RISCV::FeatureVendorXTHeadVdot};
527
528static constexpr FeatureBitset XAndesGroup = {
529 RISCV::FeatureVendorXAndesPerf, RISCV::FeatureVendorXAndesBFHCvt,
530 RISCV::FeatureVendorXAndesVBFHCvt, RISCV::FeatureVendorXAndesVSIntH,
531 RISCV::FeatureVendorXAndesVSIntLoad, RISCV::FeatureVendorXAndesVPackFPH,
532 RISCV::FeatureVendorXAndesVDot};
533
534static constexpr FeatureBitset XSMTGroup = {RISCV::FeatureVendorXSMTVDot};
535
536static constexpr FeatureBitset XAIFGroup = {RISCV::FeatureVendorXAIFET};
537
538static constexpr DecoderListEntry DecoderList32[]{
539 // Vendor Extensions
540 {.Table: DecoderTableXCV32, .ContainedFeatures: XCVFeatureGroup, .Desc: "CORE-V extensions"},
541 {.Table: DecoderTableXqci32, .ContainedFeatures: XqciFeatureGroup, .Desc: "Qualcomm uC Extensions"},
542 {.Table: DecoderTableXVentana32,
543 .ContainedFeatures: {RISCV::FeatureVendorXVentanaCondOps},
544 .Desc: "XVentanaCondOps"},
545 {.Table: DecoderTableXTHead32, .ContainedFeatures: XTHeadGroup, .Desc: "T-Head extensions"},
546 {.Table: DecoderTableXSfvector32, .ContainedFeatures: XSfVectorGroup, .Desc: "SiFive vector extensions"},
547 {.Table: DecoderTableXSfsystem32, .ContainedFeatures: XSfSystemGroup, .Desc: "SiFive system extensions"},
548 {.Table: DecoderTableXSfcease32, .ContainedFeatures: {RISCV::FeatureVendorXSfcease}, .Desc: "SiFive sf.cease"},
549 {.Table: DecoderTableXMIPS32, .ContainedFeatures: XMIPSGroup, .Desc: "Mips extensions"},
550 {.Table: DecoderTableXAndes32, .ContainedFeatures: XAndesGroup, .Desc: "Andes extensions"},
551 {.Table: DecoderTableXSMT32, .ContainedFeatures: XSMTGroup, .Desc: "SpacemiT extensions"},
552 {.Table: DecoderTableXAIF32, .ContainedFeatures: XAIFGroup, .Desc: "AI Foundry extensions"},
553 // Standard Extensions
554 {.Table: DecoderTable32, .ContainedFeatures: {}, .Desc: "standard 32-bit instructions"},
555 {.Table: DecoderTableRV32Only32, .ContainedFeatures: {}, .Desc: "RV32-only standard 32-bit instructions"},
556 {.Table: DecoderTableZfinx32, .ContainedFeatures: {}, .Desc: "Zfinx (Float in Integer)"},
557 {.Table: DecoderTableZdinxRV32Only32, .ContainedFeatures: {}, .Desc: "RV32-only Zdinx (Double in Integer)"},
558};
559
560namespace {
561// Define bitwidths for various types used to instantiate the decoder.
562template <> constexpr uint32_t InsnBitWidth<uint16_t> = 16;
563template <> constexpr uint32_t InsnBitWidth<uint32_t> = 32;
564// Use uint64_t to represent 48 bit instructions.
565template <> constexpr uint32_t InsnBitWidth<uint64_t> = 48;
566} // namespace
567
568DecodeStatus RISCVDisassembler::getInstruction32(MCInst &MI, uint64_t &Size,
569 ArrayRef<uint8_t> Bytes,
570 uint64_t Address,
571 raw_ostream &CS) const {
572 if (Bytes.size() < 4) {
573 Size = 0;
574 return MCDisassembler::Fail;
575 }
576 Size = 4;
577
578 uint32_t Insn = support::endian::read32le(P: Bytes.data());
579
580 for (const DecoderListEntry &Entry : DecoderList32) {
581 if (!Entry.haveContainedFeatures(ActiveFeatures: STI.getFeatureBits()))
582 continue;
583
584 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
585 DecodeStatus Result =
586 decodeInstruction(DecodeTable: Entry.Table, MI, insn: Insn, Address, DisAsm: this, STI);
587 if (Result == MCDisassembler::Fail)
588 continue;
589
590 return Result;
591 }
592
593 return MCDisassembler::Fail;
594}
595
596static constexpr DecoderListEntry DecoderList16[]{
597 // Vendor Extensions
598 {.Table: DecoderTableXqci16, .ContainedFeatures: XqciFeatureGroup, .Desc: "Qualcomm uC 16-bit"},
599 {.Table: DecoderTableXqccmp16,
600 .ContainedFeatures: {RISCV::FeatureVendorXqccmp},
601 .Desc: "Xqccmp (Qualcomm 16-bit Push/Pop & Double Move Instructions)"},
602 {.Table: DecoderTableXqccmt16,
603 .ContainedFeatures: {RISCV::FeatureVendorXqccmt},
604 .Desc: "Xqccmt (Qualcomm 16-bit Table Jump Instructions)"},
605 {.Table: DecoderTableXwchc16, .ContainedFeatures: {RISCV::FeatureVendorXwchc}, .Desc: "WCH QingKe XW"},
606 // Standard Extensions
607 // DecoderTableZicfiss16 must be checked before DecoderTable16.
608 {.Table: DecoderTableZicfiss16, .ContainedFeatures: {}, .Desc: "Zicfiss (Shadow Stack 16-bit)"},
609 {.Table: DecoderTable16, .ContainedFeatures: {}, .Desc: "standard 16-bit instructions"},
610 {.Table: DecoderTableRV32Only16, .ContainedFeatures: {}, .Desc: "RV32-only 16-bit instructions"},
611 // Zc* instructions incompatible with Zcf or Zcd
612 {.Table: DecoderTableZcOverlap16,
613 .ContainedFeatures: {},
614 .Desc: "ZcOverlap (16-bit Instructions overlapping with Zcf/Zcd)"},
615};
616
617DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
618 ArrayRef<uint8_t> Bytes,
619 uint64_t Address,
620 raw_ostream &CS) const {
621 if (Bytes.size() < 2) {
622 Size = 0;
623 return MCDisassembler::Fail;
624 }
625 Size = 2;
626
627 uint16_t Insn = support::endian::read16le(P: Bytes.data());
628
629 for (const DecoderListEntry &Entry : DecoderList16) {
630 if (!Entry.haveContainedFeatures(ActiveFeatures: STI.getFeatureBits()))
631 continue;
632
633 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
634 DecodeStatus Result =
635 decodeInstruction(DecodeTable: Entry.Table, MI, insn: Insn, Address, DisAsm: this, STI);
636 if (Result != MCDisassembler::Fail)
637 return Result;
638 }
639
640 return MCDisassembler::Fail;
641}
642
643static constexpr DecoderListEntry DecoderList48[]{
644 {.Table: DecoderTableXqci48, .ContainedFeatures: XqciFeatureGroup, .Desc: "Qualcomm uC 48bit"},
645};
646
647DecodeStatus RISCVDisassembler::getInstruction48(MCInst &MI, uint64_t &Size,
648 ArrayRef<uint8_t> Bytes,
649 uint64_t Address,
650 raw_ostream &CS) const {
651 if (Bytes.size() < 6) {
652 Size = 0;
653 return MCDisassembler::Fail;
654 }
655 Size = 6;
656
657 uint64_t Insn = 0;
658 for (size_t i = Size; i-- != 0;)
659 Insn += (static_cast<uint64_t>(Bytes[i]) << 8 * i);
660
661 for (const DecoderListEntry &Entry : DecoderList48) {
662 if (!Entry.haveContainedFeatures(ActiveFeatures: STI.getFeatureBits()))
663 continue;
664
665 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
666 DecodeStatus Result =
667 decodeInstruction(DecodeTable: Entry.Table, MI, insn: Insn, Address, DisAsm: this, STI);
668 if (Result == MCDisassembler::Fail)
669 continue;
670
671 return Result;
672 }
673
674 return MCDisassembler::Fail;
675}
676
677DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
678 ArrayRef<uint8_t> Bytes,
679 uint64_t Address,
680 raw_ostream &CS) const {
681 CommentStream = &CS;
682 // It's a 16 bit instruction if bit 0 and 1 are not 0b11.
683 if ((Bytes[0] & 0b11) != 0b11)
684 return getInstruction16(MI, Size, Bytes, Address, CS);
685
686 // Try to decode as a 32-bit instruction first.
687 DecodeStatus Result = getInstruction32(MI, Size, Bytes, Address, CS);
688 if (Result != MCDisassembler::Fail)
689 return Result;
690
691 // If bits [4:2] are 0b111 this might be a 48-bit or larger instruction,
692 // otherwise assume it's an unknown 32-bit instruction.
693 if ((Bytes[0] & 0b1'1100) != 0b1'1100) {
694 Size = Bytes.size() >= 4 ? 4 : 0;
695 return MCDisassembler::Fail;
696 }
697
698 // 48-bit instructions are encoded as 0bxx011111.
699 if ((Bytes[0] & 0b11'1111) == 0b01'1111)
700 return getInstruction48(MI, Size, Bytes, Address, CS);
701
702 // 64-bit instructions are encoded as 0x0111111.
703 if ((Bytes[0] & 0b111'1111) == 0b011'1111) {
704 Size = Bytes.size() >= 8 ? 8 : 0;
705 return MCDisassembler::Fail;
706 }
707
708 // Remaining cases need to check a second byte.
709 if (Bytes.size() < 2) {
710 Size = 0;
711 return MCDisassembler::Fail;
712 }
713
714 // 80-bit through 176-bit instructions are encoded as 0bxnnnxxxx_x1111111.
715 // Where the number of bits is (80 + (nnn * 16)) for nnn != 0b111.
716 unsigned nnn = (Bytes[1] >> 4) & 0b111;
717 if (nnn != 0b111) {
718 Size = 10 + (nnn * 2);
719 if (Bytes.size() < Size)
720 Size = 0;
721 return MCDisassembler::Fail;
722 }
723
724 // Remaining encodings are reserved for > 176-bit instructions.
725 Size = 0;
726 return MCDisassembler::Fail;
727}
728