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