LLVM 22.0.0git
RISCVDisassembler.cpp
Go to the documentation of this file.
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
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDecoder.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/Support/Endian.h"
27
28using namespace llvm;
29using namespace llvm::MCD;
30
31#define DEBUG_TYPE "riscv-disassembler"
32
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
63 const MCSubtargetInfo &STI,
64 MCContext &Ctx) {
65 return new RISCVDisassembler(STI, Ctx, T.createMCInstrInfo());
66}
67
80
82 uint64_t Address,
83 const MCDisassembler *Decoder) {
84 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
85
86 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
88
89 MCRegister Reg = RISCV::X0 + RegNo;
92}
93
95 uint64_t Address,
96 const MCDisassembler *Decoder) {
97 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
98
99 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
101
102 MCRegister Reg = RISCV::X0_H + RegNo;
105}
106
108 uint64_t Address,
109 const MCDisassembler *Decoder) {
110 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
111
112 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
114
115 MCRegister Reg = RISCV::X0_W + RegNo;
118}
119
121 uint64_t Address,
122 const MCDisassembler *Decoder) {
123 MCRegister Reg = RISCV::X0 + RegNo;
124 if (Reg != RISCV::X1 && Reg != RISCV::X5)
126
129}
130
132 uint64_t Address,
133 const MCDisassembler *Decoder) {
134 if (RegNo >= 32)
136
137 MCRegister Reg = RISCV::F0_H + RegNo;
140}
141
143 uint64_t Address,
144 const MCDisassembler *Decoder) {
145 if (RegNo >= 32)
147
148 MCRegister Reg = RISCV::F0_F + RegNo;
151}
152
154 uint64_t Address,
155 const MCDisassembler *Decoder) {
156 if (RegNo >= 8) {
158 }
159 MCRegister Reg = RISCV::F8_F + RegNo;
162}
163
165 uint64_t Address,
166 const MCDisassembler *Decoder) {
167 if (RegNo >= 32)
169
170 MCRegister Reg = RISCV::F0_D + RegNo;
173}
174
176 uint64_t Address,
177 const MCDisassembler *Decoder) {
178 if (RegNo >= 8) {
180 }
181 MCRegister Reg = RISCV::F8_D + RegNo;
184}
185
187 uint64_t Address,
188 const MCDisassembler *Decoder) {
189 if (RegNo >= 32)
191
192 MCRegister Reg = RISCV::F0_Q + RegNo;
195}
196
198 const MCDisassembler *Decoder) {
199 Inst.addOperand(MCOperand::createReg(RISCV::X2));
201}
202
204 uint64_t Address,
205 const MCDisassembler *Decoder) {
206 if (RegNo == 0)
208
209 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
210}
211
213 uint32_t Address,
214 const MCDisassembler *Decoder) {
215 if (RegNo == 2)
217
218 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
219}
220
222 uint64_t Address,
223 const MCDisassembler *Decoder) {
224 if (RegNo == 31) {
226 }
227
228 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
229}
230
232 uint64_t Address,
233 const MCDisassembler *Decoder) {
234 if (RegNo >= 8)
236
237 MCRegister Reg = RISCV::X8 + RegNo;
240}
241
243 uint64_t Address,
244 const MCDisassembler *Decoder) {
245 if (RegNo >= 32 || RegNo % 2)
247
248 const RISCVDisassembler *Dis =
249 static_cast<const RISCVDisassembler *>(Decoder);
250 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
252 RISCV::X0 + RegNo, RISCV::sub_gpr_even,
253 &RISCVMCRegisterClasses[RISCV::GPRPairRegClassID]);
256}
257
259 uint64_t Address,
260 const MCDisassembler *Decoder) {
261 if (RegNo >= 8 || RegNo % 2)
263
264 const RISCVDisassembler *Dis =
265 static_cast<const RISCVDisassembler *>(Decoder);
266 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
268 RISCV::X8 + RegNo, RISCV::sub_gpr_even,
269 &RISCVMCRegisterClasses[RISCV::GPRPairCRegClassID]);
272}
273
275 uint64_t Address,
276 const void *Decoder) {
277 if (RegNo >= 8)
279
280 MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV::X8) : (RegNo - 2 + RISCV::X18);
283}
284
286 uint64_t Address,
287 const MCDisassembler *Decoder) {
288 if (RegNo >= 32)
290
291 MCRegister Reg = RISCV::V0 + RegNo;
294}
295
297 uint64_t Address,
298 const MCDisassembler *Decoder) {
299 if (RegNo >= 32 || RegNo % 2)
301
302 const RISCVDisassembler *Dis =
303 static_cast<const RISCVDisassembler *>(Decoder);
304 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
306 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
307 &RISCVMCRegisterClasses[RISCV::VRM2RegClassID]);
308
311}
312
314 uint64_t Address,
315 const MCDisassembler *Decoder) {
316 if (RegNo >= 32 || RegNo % 4)
318
319 const RISCVDisassembler *Dis =
320 static_cast<const RISCVDisassembler *>(Decoder);
321 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
323 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
324 &RISCVMCRegisterClasses[RISCV::VRM4RegClassID]);
325
328}
329
331 uint64_t Address,
332 const MCDisassembler *Decoder) {
333 if (RegNo >= 32 || RegNo % 8)
335
336 const RISCVDisassembler *Dis =
337 static_cast<const RISCVDisassembler *>(Decoder);
338 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
340 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
341 &RISCVMCRegisterClasses[RISCV::VRM8RegClassID]);
342
345}
346
348 uint64_t Address,
349 const MCDisassembler *Decoder) {
350 if (RegNo)
352
353 Inst.addOperand(MCOperand::createReg(RISCV::V0));
355}
356
358 uint64_t Address,
359 const MCDisassembler *Decoder) {
360 if (RegNo > 15)
362
363 MCRegister Reg = RISCV::T0 + RegNo;
366}
367
369 uint64_t Address,
370 const MCDisassembler *Decoder) {
371 if (RegNo > 15 || RegNo % 2)
373
374 MCRegister Reg = RISCV::T0 + RegNo;
377}
378
380 uint64_t Address,
381 const MCDisassembler *Decoder) {
382 if (RegNo > 15 || RegNo % 4)
384
385 MCRegister Reg = RISCV::T0 + RegNo;
388}
389
391 uint64_t Address,
392 const MCDisassembler *Decoder) {
393 if (RegNo >= 2)
395
396 MCRegister Reg = (RegNo == 0) ? RISCV::V0 : RISCV::NoRegister;
397
400}
401
402template <unsigned N>
404 int64_t Address,
405 const MCDisassembler *Decoder) {
406 assert(isUInt<N>(Imm) && "Invalid immediate");
409}
410
411template <unsigned Width, unsigned LowerBound>
413 int64_t Address,
414 const MCDisassembler *Decoder) {
415 assert(isUInt<Width>(Imm) && "Invalid immediate");
416
417 if (Imm < LowerBound)
419
422}
423
424template <unsigned Width, unsigned LowerBound>
426 int64_t Address,
427 const MCDisassembler *Decoder) {
428 assert(isUInt<Width>(Imm) && "Invalid immediate");
429
430 if ((Imm + 1) < LowerBound)
432
433 Inst.addOperand(MCOperand::createImm(Imm + 1));
435}
436
438 int64_t Address,
439 const MCDisassembler *Decoder) {
440 assert(isUInt<3>(Imm) && "Invalid Slist immediate");
441 const uint8_t Slist[] = {0, 1, 2, 4, 8, 16, 15, 31};
442 Inst.addOperand(MCOperand::createImm(Slist[Imm]));
444}
445
447 int64_t Address,
448 const MCDisassembler *Decoder) {
449 assert(isUInt<6>(Imm) && "Invalid immediate");
450
451 if (!Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) &&
452 !isUInt<5>(Imm))
454
457}
458
459template <unsigned N>
461 int64_t Address,
462 const MCDisassembler *Decoder) {
463 if (Imm == 0)
465 return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
466}
467
468static DecodeStatus
470 const MCDisassembler *Decoder) {
471 if (Imm == 0)
473 return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
474}
475
476template <unsigned N>
478 int64_t Address,
479 const MCDisassembler *Decoder) {
480 assert(isUInt<N>(Imm) && "Invalid immediate");
481 Inst.addOperand(MCOperand::createImm(Imm + 1));
483}
484
485template <unsigned N>
487 int64_t Address,
488 const MCDisassembler *Decoder) {
489 assert(isUInt<N>(Imm) && "Invalid immediate");
490 // Sign-extend the number in the bottom N bits of Imm
493}
494
495template <unsigned N>
497 int64_t Address,
498 const MCDisassembler *Decoder) {
499 if (Imm == 0)
501 return decodeSImmOperand<N>(Inst, Imm, Address, Decoder);
502}
503
504template <unsigned T, unsigned N>
506 int64_t Address,
507 const MCDisassembler *Decoder) {
508 assert(isUInt<T - N + 1>(Imm) && "Invalid immediate");
509 // Sign-extend the number in the bottom T bits of Imm after accounting for
510 // the fact that the T bit immediate is stored in T-N bits (the LSB is
511 // always zero)
514}
515
517 int64_t Address,
518 const MCDisassembler *Decoder) {
519 assert(isUInt<6>(Imm) && "Invalid immediate");
520 if (Imm == 0)
522 Imm = SignExtend64<6>(Imm) & 0xfffff;
525}
526
527static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address,
528 const MCDisassembler *Decoder) {
529 assert(isUInt<3>(Imm) && "Invalid immediate");
532
535}
536
537static DecodeStatus decodeRTZArg(MCInst &Inst, uint32_t Imm, int64_t Address,
538 const MCDisassembler *Decoder) {
539 assert(isUInt<3>(Imm) && "Invalid immediate");
540 if (Imm != RISCVFPRndMode::RTZ)
542
545}
546
548 uint64_t Address,
549 const MCDisassembler *Decoder) {
550 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
551 if (Imm < RISCVZC::RA || (IsRVE && Imm >= RISCVZC::RA_S0_S2))
555}
556
558 uint64_t Address,
559 const MCDisassembler *Decoder) {
560 if (Imm < RISCVZC::RA_S0)
562 return decodeZcmpRlist(Inst, Imm, Address, Decoder);
563}
564
566 uint64_t Address,
567 const MCDisassembler *Decoder) {
568 uint32_t Rs1 = fieldFromInstruction(Insn, 7, 5);
569 [[maybe_unused]] DecodeStatus Result =
570 DecodeGPRX1X5RegisterClass(Inst, Rs1, Address, Decoder);
571 assert(Result == MCDisassembler::Success && "Invalid register");
573}
574
576 uint64_t Address,
577 const MCDisassembler *Decoder) {
579 uint32_t Rd1 = fieldFromInstruction(Insn, 7, 5);
580 uint32_t Rs1 = fieldFromInstruction(Insn, 15, 5);
581 uint32_t Rd2 = fieldFromInstruction(Insn, 20, 5);
582 uint32_t UImm2 = fieldFromInstruction(Insn, 25, 2);
583 if (!Check(S, DecodeGPRRegisterClass(Inst, Rd1, Address, Decoder)))
585 if (!Check(S, DecodeGPRRegisterClass(Inst, Rd2, Address, Decoder)))
587 if (!Check(S, DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder)))
589 [[maybe_unused]] DecodeStatus Result =
590 decodeUImmOperand<2>(Inst, UImm2, Address, Decoder);
591 assert(Result == MCDisassembler::Success && "Invalid immediate");
592
593 // Disassemble the final operand which is implicit.
594 unsigned Opcode = Inst.getOpcode();
595 bool IsWordOp = (Opcode == RISCV::TH_LWD || Opcode == RISCV::TH_LWUD ||
596 Opcode == RISCV::TH_SWD);
597 if (IsWordOp)
599 else
601
602 return S;
603}
604
605#include "RISCVGenDisassemblerTables.inc"
606
607namespace {
608
609struct DecoderListEntry {
610 const uint8_t *Table;
611 FeatureBitset ContainedFeatures;
612 const char *Desc;
613
614 bool haveContainedFeatures(const FeatureBitset &ActiveFeatures) const {
615 return ContainedFeatures.none() ||
616 (ContainedFeatures & ActiveFeatures).any();
617 }
618};
619
620} // end anonymous namespace
621
622static constexpr FeatureBitset XCVFeatureGroup = {
623 RISCV::FeatureVendorXCVbitmanip, RISCV::FeatureVendorXCVelw,
624 RISCV::FeatureVendorXCVmac, RISCV::FeatureVendorXCVmem,
625 RISCV::FeatureVendorXCValu, RISCV::FeatureVendorXCVsimd,
626 RISCV::FeatureVendorXCVbi};
627
629 RISCV::FeatureVendorXRivosVisni,
630 RISCV::FeatureVendorXRivosVizip,
631};
632
633static constexpr FeatureBitset XqciFeatureGroup = {
634 RISCV::FeatureVendorXqcia, RISCV::FeatureVendorXqciac,
635 RISCV::FeatureVendorXqcibi, RISCV::FeatureVendorXqcibm,
636 RISCV::FeatureVendorXqcicli, RISCV::FeatureVendorXqcicm,
637 RISCV::FeatureVendorXqcics, RISCV::FeatureVendorXqcicsr,
638 RISCV::FeatureVendorXqciint, RISCV::FeatureVendorXqciio,
639 RISCV::FeatureVendorXqcilb, RISCV::FeatureVendorXqcili,
640 RISCV::FeatureVendorXqcilia, RISCV::FeatureVendorXqcilo,
641 RISCV::FeatureVendorXqcilsm, RISCV::FeatureVendorXqcisim,
642 RISCV::FeatureVendorXqcisls, RISCV::FeatureVendorXqcisync,
643};
644
645static constexpr FeatureBitset XSfVectorGroup = {
646 RISCV::FeatureVendorXSfvcp, RISCV::FeatureVendorXSfvqmaccdod,
647 RISCV::FeatureVendorXSfvqmaccqoq, RISCV::FeatureVendorXSfvfwmaccqqq,
648 RISCV::FeatureVendorXSfvfnrclipxfqf, RISCV::FeatureVendorXSfmmbase};
649static constexpr FeatureBitset XSfSystemGroup = {
650 RISCV::FeatureVendorXSiFivecdiscarddlone,
651 RISCV::FeatureVendorXSiFivecflushdlone,
652};
653
654static constexpr FeatureBitset XMIPSGroup = {
655 RISCV::FeatureVendorXMIPSLSP,
656 RISCV::FeatureVendorXMIPSCMov,
657 RISCV::FeatureVendorXMIPSCBOP,
658 RISCV::FeatureVendorXMIPSEXECTL,
659};
660
661static constexpr FeatureBitset XTHeadGroup = {
662 RISCV::FeatureVendorXTHeadBa, RISCV::FeatureVendorXTHeadBb,
663 RISCV::FeatureVendorXTHeadBs, RISCV::FeatureVendorXTHeadCondMov,
664 RISCV::FeatureVendorXTHeadCmo, RISCV::FeatureVendorXTHeadFMemIdx,
665 RISCV::FeatureVendorXTHeadMac, RISCV::FeatureVendorXTHeadMemIdx,
666 RISCV::FeatureVendorXTHeadMemPair, RISCV::FeatureVendorXTHeadSync,
667 RISCV::FeatureVendorXTHeadVdot};
668
669static constexpr FeatureBitset XAndesGroup = {
670 RISCV::FeatureVendorXAndesPerf, RISCV::FeatureVendorXAndesBFHCvt,
671 RISCV::FeatureVendorXAndesVBFHCvt,
672 RISCV::FeatureVendorXAndesVSIntLoad, RISCV::FeatureVendorXAndesVPackFPH,
673 RISCV::FeatureVendorXAndesVDot};
674
675static constexpr FeatureBitset XSMTGroup = {RISCV::FeatureVendorXSMTVDot};
676
677static constexpr DecoderListEntry DecoderList32[]{
678 // Vendor Extensions
679 {DecoderTableXCV32, XCVFeatureGroup, "CORE-V extensions"},
680 {DecoderTableXRivos32, XRivosFeatureGroup, "Rivos"},
681 {DecoderTableXqci32, XqciFeatureGroup, "Qualcomm uC Extensions"},
682 {DecoderTableXVentana32,
683 {RISCV::FeatureVendorXVentanaCondOps},
684 "XVentanaCondOps"},
685 {DecoderTableXTHead32, XTHeadGroup, "T-Head extensions"},
686 {DecoderTableXSfvector32, XSfVectorGroup, "SiFive vector extensions"},
687 {DecoderTableXSfsystem32, XSfSystemGroup, "SiFive system extensions"},
688 {DecoderTableXSfcease32, {RISCV::FeatureVendorXSfcease}, "SiFive sf.cease"},
689 {DecoderTableXMIPS32, XMIPSGroup, "Mips extensions"},
690 {DecoderTableXAndes32, XAndesGroup, "Andes extensions"},
691 {DecoderTableXSMT32, XSMTGroup, "SpacemiT extensions"},
692 // Standard Extensions
693 {DecoderTable32, {}, "standard 32-bit instructions"},
694 {DecoderTableRV32Only32, {}, "RV32-only standard 32-bit instructions"},
695 {DecoderTableZfinx32, {}, "Zfinx (Float in Integer)"},
696 {DecoderTableZdinxRV32Only32, {}, "RV32-only Zdinx (Double in Integer)"},
697};
698
699namespace {
700// Define bitwidths for various types used to instantiate the decoder.
701template <> constexpr uint32_t InsnBitWidth<uint16_t> = 16;
702template <> constexpr uint32_t InsnBitWidth<uint32_t> = 32;
703// Use uint64_t to represent 48 bit instructions.
704template <> constexpr uint32_t InsnBitWidth<uint64_t> = 48;
705} // namespace
706
707DecodeStatus RISCVDisassembler::getInstruction32(MCInst &MI, uint64_t &Size,
708 ArrayRef<uint8_t> Bytes,
709 uint64_t Address,
710 raw_ostream &CS) const {
711 if (Bytes.size() < 4) {
712 Size = 0;
714 }
715 Size = 4;
716
717 uint32_t Insn = support::endian::read32le(Bytes.data());
718
719 for (const DecoderListEntry &Entry : DecoderList32) {
720 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
721 continue;
722
723 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
725 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
726 if (Result == MCDisassembler::Fail)
727 continue;
728
729 return Result;
730 }
731
733}
734
735static constexpr DecoderListEntry DecoderList16[]{
736 // Vendor Extensions
737 {DecoderTableXqci16, XqciFeatureGroup, "Qualcomm uC 16-bit"},
738 {DecoderTableXqccmp16,
739 {RISCV::FeatureVendorXqccmp},
740 "Xqccmp (Qualcomm 16-bit Push/Pop & Double Move Instructions)"},
741 {DecoderTableXwchc16, {RISCV::FeatureVendorXwchc}, "WCH QingKe XW"},
742 // Standard Extensions
743 // DecoderTableZicfiss16 must be checked before DecoderTable16.
744 {DecoderTableZicfiss16, {}, "Zicfiss (Shadow Stack 16-bit)"},
745 {DecoderTable16, {}, "standard 16-bit instructions"},
746 {DecoderTableRV32Only16, {}, "RV32-only 16-bit instructions"},
747 // Zc* instructions incompatible with Zcf or Zcd
748 {DecoderTableZcOverlap16,
749 {},
750 "ZcOverlap (16-bit Instructions overlapping with Zcf/Zcd)"},
751};
752
753DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
754 ArrayRef<uint8_t> Bytes,
755 uint64_t Address,
756 raw_ostream &CS) const {
757 if (Bytes.size() < 2) {
758 Size = 0;
760 }
761 Size = 2;
762
763 uint16_t Insn = support::endian::read16le(Bytes.data());
764
765 for (const DecoderListEntry &Entry : DecoderList16) {
766 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
767 continue;
768
769 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
771 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
772 if (Result != MCDisassembler::Fail)
773 return Result;
774 }
775
777}
778
779static constexpr DecoderListEntry DecoderList48[]{
780 {DecoderTableXqci48, XqciFeatureGroup, "Qualcomm uC 48bit"},
781};
782
783DecodeStatus RISCVDisassembler::getInstruction48(MCInst &MI, uint64_t &Size,
784 ArrayRef<uint8_t> Bytes,
785 uint64_t Address,
786 raw_ostream &CS) const {
787 if (Bytes.size() < 6) {
788 Size = 0;
790 }
791 Size = 6;
792
793 uint64_t Insn = 0;
794 for (size_t i = Size; i-- != 0;)
795 Insn += (static_cast<uint64_t>(Bytes[i]) << 8 * i);
796
797 for (const DecoderListEntry &Entry : DecoderList48) {
798 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
799 continue;
800
801 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
803 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
804 if (Result == MCDisassembler::Fail)
805 continue;
806
807 return Result;
808 }
809
811}
812
813DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
814 ArrayRef<uint8_t> Bytes,
815 uint64_t Address,
816 raw_ostream &CS) const {
817 CommentStream = &CS;
818 // It's a 16 bit instruction if bit 0 and 1 are not 0b11.
819 if ((Bytes[0] & 0b11) != 0b11)
820 return getInstruction16(MI, Size, Bytes, Address, CS);
821
822 // It's a 32 bit instruction if bit 1:0 are 0b11(checked above) and bits 4:2
823 // are not 0b111.
824 if ((Bytes[0] & 0b1'1100) != 0b1'1100)
825 return getInstruction32(MI, Size, Bytes, Address, CS);
826
827 // 48-bit instructions are encoded as 0bxx011111.
828 if ((Bytes[0] & 0b11'1111) == 0b01'1111) {
829 return getInstruction48(MI, Size, Bytes, Address, CS);
830 }
831
832 // 64-bit instructions are encoded as 0x0111111.
833 if ((Bytes[0] & 0b111'1111) == 0b011'1111) {
834 Size = Bytes.size() >= 8 ? 8 : 0;
836 }
837
838 // Remaining cases need to check a second byte.
839 if (Bytes.size() < 2) {
840 Size = 0;
842 }
843
844 // 80-bit through 176-bit instructions are encoded as 0bxnnnxxxx_x1111111.
845 // Where the number of bits is (80 + (nnn * 16)) for nnn != 0b111.
846 unsigned nnn = (Bytes[1] >> 4) & 0b111;
847 if (nnn != 0b111) {
848 Size = 10 + (nnn * 2);
849 if (Bytes.size() < Size)
850 Size = 0;
852 }
853
854 // Remaining encodings are reserved for > 176-bit instructions.
855 Size = 0;
857}
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define Check(C,...)
IRTranslator LLVM IR MI
Register Reg
#define T
static constexpr FeatureBitset XqciFeatureGroup
static DecodeStatus decodeUImmSlistOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XSMTGroup
static DecodeStatus decodeUImmLog2XLenOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XCVFeatureGroup
static constexpr DecoderListEntry DecoderList48[]
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr DecoderListEntry DecoderList16[]
static DecodeStatus DecodeGPRX1X5RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRNoX31RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeCSSPushPopchk(MCInst &Inst, uint16_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeRTZArg(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createRISCVDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus DecodeGPRF16RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeTRM2RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR128RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeSImmOperandAndLslN(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XSfVectorGroup
static constexpr DecoderListEntry DecoderList32[]
static DecodeStatus decodeXqccmpRlistS0(MCInst &Inst, uint32_t Imm, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR32CRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeXTHeadMemPair(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmPlus1Operand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeCLUIImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeZcmpRlist(MCInst &Inst, uint32_t Imm, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVRM8RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVRM4RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRNoX2RegisterClass(MCInst &Inst, uint64_t RegNo, uint32_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVRRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVRM2RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRPairRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeSPRegisterClass(MCInst &Inst, const MCDisassembler *Decoder)
static constexpr FeatureBitset XMIPSGroup
static DecodeStatus decodeSImmNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVMV0RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeTRRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFPR64CRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRF32RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XAndesGroup
static DecodeStatus DecodeTRM4RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRNoX0RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeSR07RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const void *Decoder)
static constexpr FeatureBitset XTHeadGroup
static constexpr FeatureBitset XRivosFeatureGroup
static DecodeStatus decodeUImmPlus1OperandGE(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVDisassembler()
static DecodeStatus DecodeFPR16RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeVMaskReg(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XSfSystemGroup
static DecodeStatus decodeUImmOperandGE(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRPairCRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
#define LLVM_DEBUG(...)
Definition Debug.h:119
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
const T * data() const
Definition ArrayRef.h:144
Container class for subtarget features.
Context object for machine code objects.
Definition MCContext.h:83
Superclass for all disassemblers.
const MCSubtargetInfo & getSubtargetInfo() const
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
void addOperand(const MCOperand Op)
Definition MCInst.h:215
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, const MCRegisterClass *RC) const
Return a super-register of the specified register Reg so its sub-register of index SubIdx is Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
Target - Wrapper for Target specific information.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
constexpr bool any(E Val)
@ Entry
Definition COFF.h:862
std::enable_if_t< std::is_integral_v< IntType >, IntType > fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits)
Definition MCDecoder.h:37
static bool isValidRoundingMode(unsigned Mode)
uint16_t read16le(const void *P)
Definition Endian.h:426
uint32_t read32le(const void *P)
Definition Endian.h:429
This is an optimization pass for GlobalISel generic memory operations.
Target & getTheRISCV32Target()
Target & getTheRISCV64beTarget()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
Target & getTheRISCV64Target()
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:577
Target & getTheRISCV32beTarget()
#define N
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.