LLVM 24.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
68static MCSymbolizer *
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, std::move(RelInfo));
78}
79
100
101template <unsigned FirstReg, unsigned NumRegsInClass, unsigned RVELimit = 0>
103 uint64_t Address,
104 const MCDisassembler *Decoder) {
105 bool CheckRVE = RVELimit != 0 &&
106 Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
107
108 if (RegNo >= NumRegsInClass || (CheckRVE && RegNo >= RVELimit))
110
111 MCRegister Reg = FirstReg + RegNo;
114}
115
116template <unsigned PhysReg, unsigned Encoding>
118 uint64_t Address,
119 const MCDisassembler *Decoder) {
120 assert(RegNo == Encoding);
121 Inst.addOperand(MCOperand::createReg(PhysReg));
123}
124
125template <unsigned PhysReg, unsigned Encoding>
131
133 DecodeSimpleRegisterClass<RISCV::X0, 32, /*RVELimit=*/16>;
134
135template <auto DecodeFn, auto PredicateFn>
137 uint64_t Address,
138 const MCDisassembler *Decoder) {
139 if (!PredicateFn(RegNo))
141 return DecodeFn(Inst, RegNo, Address, Decoder);
142}
143
144constexpr bool PredNoX0(uint32_t RegNo) { return RegNo != 0; }
145constexpr bool PredNoX2(uint32_t RegNo) { return RegNo != 2; }
146constexpr bool PredNoX31(uint32_t RegNo) { return RegNo != 31; }
147constexpr bool PredX1OrX5(uint32_t RegNo) { return RegNo == 1 || RegNo == 5; }
148
157
159 uint64_t Address,
160 const MCDisassembler *Decoder) {
161 const DecodeStatus Result =
162 DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
163 if (Result != MCDisassembler::Success)
164 return Result;
165 const MCRegister Rd = Inst.getOperand(0).getReg();
166 const MCRegister Rs1 = Inst.getOperand(Inst.getNumOperands() - 1).getReg();
167 if (Rd == Rs1)
169 return Result;
170}
171
173 uint64_t Address,
174 const MCDisassembler *Decoder) {
175 const DecodeStatus Result =
176 DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
177 if (Result != MCDisassembler::Success)
178 return Result;
179 const MCRegister Rd1 = Inst.getOperand(0).getReg();
180 const MCRegister Rd2 = Inst.getOperand(1).getReg();
181 const MCRegister Rs1 = Inst.getOperand(Inst.getNumOperands() - 1).getReg();
182 if (Rs1 == Rd1 || Rs1 == Rd2 || Rd1 == Rd2)
184 return Result;
185}
186
188 uint64_t Address,
189 const MCDisassembler *Decoder) {
190 if (RegNo >= 32 || RegNo % 2)
192
193 const RISCVDisassembler *Dis =
194 static_cast<const RISCVDisassembler *>(Decoder);
195 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
197 RISCV::X0 + RegNo, RISCV::sub_gpr_even,
198 &getRISCVMCRegisterClass(RISCV::GPRPairRegClassID));
201}
202
205
207 uint64_t Address,
208 const MCDisassembler *Decoder) {
209 if (RegNo >= 8 || RegNo % 2)
211
212 return DecodeGPRPairRegisterClass(Inst, RegNo + 8, Address, Decoder);
213}
214
216 uint64_t Address,
217 const void *Decoder) {
218 if (RegNo >= 8)
220
221 MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV::X8) : (RegNo - 2 + RISCV::X18);
224}
225
226template <unsigned RegisterClass, unsigned NumRegsInClass, unsigned LMul>
228 uint64_t Address,
229 const MCDisassembler *Decoder) {
230 if (RegNo >= NumRegsInClass || RegNo % LMul)
232
233 const RISCVDisassembler *Dis =
234 static_cast<const RISCVDisassembler *>(Decoder);
235 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
237 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
238 &getRISCVMCRegisterClass(RegisterClass));
239
242}
243
245 uint64_t Address,
246 const MCDisassembler *Decoder) {
247 if (RegNo > 15 || RegNo % 2)
249
250 MCRegister Reg = RISCV::T0 + RegNo;
253}
254
256 uint64_t Address,
257 const MCDisassembler *Decoder) {
258 if (RegNo > 15 || RegNo % 4)
260
261 MCRegister Reg = RISCV::T0 + RegNo;
264}
265
266static DecodeStatus DecodeYBNDSWImm(MCInst &Inst, uint64_t Imm, int64_t Address,
267 const MCDisassembler *Decoder) {
268 assert(isUInt<9>(Imm) && "Invalid immediate");
269 uint64_t Result;
270 if (Imm == 0) {
271 // If imm[8:0] == 0, result is 4096.
272 Result = 4096;
273 } else if (Imm <= 255) {
274 // If imm[8] == 0 and imm[7:0] != 0, result is imm[7:0].
275 Result = Imm;
276 } else {
277 uint64_t Imm7To0 = Imm & 0xFF;
278 if (Imm7To0 <= 31) {
279 // If imm[8] == 1 and imm[7:5] == 0 (i.e. imm[7:0] <= 31), result is
280 // `256 | (imm[3:0] << 4) | (imm[4] << 3)`.
281 Result = 256 + ((Imm & 0xF) << 4) + (((Imm >> 4) & 1) << 3);
282 } else {
283 // Otherwise, result is imm[7:0] << 4.
284 Result = Imm7To0 << 4;
285 }
286 }
287 Inst.addOperand(MCOperand::createImm(Result));
289}
290
292 uint64_t Address,
293 const MCDisassembler *Decoder) {
294 if (RegNo >= 2)
296
297 MCRegister Reg = (RegNo == 0) ? RISCV::V0 : RISCV::NoRegister;
298
301}
302
303template <int64_t Imm>
309
310template <unsigned N, unsigned LowerBound = 0>
312 int64_t Address,
313 const MCDisassembler *Decoder) {
314 assert(isUInt<N>(Imm) && "Invalid immediate");
315
316 if (Imm < LowerBound)
318
321}
322
324 int64_t Address,
325 const MCDisassembler *Decoder) {
326 assert(isUInt<3>(Imm) && "Invalid Slist immediate");
327 const uint8_t Slist[] = {0, 1, 2, 4, 8, 16, 15, 31};
328 Inst.addOperand(MCOperand::createImm(Slist[Imm]));
330}
331
333 int64_t Address,
334 const MCDisassembler *Decoder) {
335 assert(isUInt<6>(Imm) && "Invalid immediate");
336
337 if (!Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) &&
338 !isUInt<5>(Imm))
340
343}
344
346 int64_t Address,
347 const MCDisassembler *Decoder) {
348 assert(isUInt<7>(Imm) && "Invalid immediate");
349
350 uint32_t ExpectedValue =
351 Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) ? 64 : 32;
352 if (Imm != ExpectedValue)
354
357}
358
359template <unsigned N>
361 int64_t Address,
362 const MCDisassembler *Decoder) {
363 if (Imm == 0)
365 return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
366}
367
368static DecodeStatus
370 const MCDisassembler *Decoder) {
371 if (Imm == 0)
373 return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
374}
375
376template <unsigned N, unsigned LowerBound = 1>
378 int64_t Address,
379 const MCDisassembler *Decoder) {
380 assert(isUInt<N>(Imm) && "Invalid immediate");
381
382 if ((Imm + 1) < LowerBound)
384
387}
388
390 int64_t Address,
391 const MCDisassembler *Decoder) {
392 assert(isUInt<5>(Imm) && "Invalid immediate");
393 Inst.addOperand(MCOperand::createImm(Imm ? Imm : -1LL));
395}
396
397template <unsigned N>
399 int64_t Address,
400 const MCDisassembler *Decoder) {
401 assert(isUInt<N>(Imm) && "Invalid immediate");
402 // Sign-extend the number in the bottom N bits of Imm
405}
406
408 int64_t Address,
409 const MCDisassembler *Decoder) {
410 assert(isUInt<12>(Imm) && "Invalid immediate");
411 const int64_t Value = SignExtend64<12>(Imm);
412 if (!Decoder->tryAddingSymbolicOperand(Inst, Value, Address,
413 /*IsBranch=*/false,
414 /*Offset=*/0, /*OpSize=*/4,
415 /*InstSize=*/4))
418}
419
421 int64_t Address,
422 const MCDisassembler *Decoder) {
423 assert(isUInt<20>(Imm) && "Invalid immediate");
424 const int64_t Value = SignExtend64<32>(Imm << 12);
425 if (!Decoder->tryAddingSymbolicOperand(Inst, Value, Address,
426 /*IsBranch=*/false,
427 /*Offset=*/0, /*OpSize=*/4,
428 /*InstSize=*/4))
431}
432
433template <unsigned N>
435 int64_t Address,
436 const MCDisassembler *Decoder) {
437 if (Imm == 0)
439 return decodeSImmOperand<N>(Inst, Imm, Address, Decoder);
440}
441
442template <unsigned T, unsigned N>
444 int64_t Address,
445 const MCDisassembler *Decoder) {
446 assert(isUInt<T - N + 1>(Imm) && "Invalid immediate");
447 // Sign-extend the number in the bottom T bits of Imm after accounting for
448 // the fact that the T bit immediate is stored in T-N bits (the LSB is
449 // always zero)
452}
453
455 int64_t Address,
456 const MCDisassembler *Decoder) {
457 assert(isUInt<6>(Imm) && "Invalid immediate");
458 if (Imm == 0)
460 Imm = SignExtend64<6>(Imm) & 0xfffff;
463}
464
465static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address,
466 const MCDisassembler *Decoder) {
467 assert(isUInt<3>(Imm) && "Invalid immediate");
470
473}
474
476 int64_t Address,
477 const MCDisassembler *Decoder) {
478 const int64_t Is3 = Inst.getOperand(Inst.getNumOperands() - 1).getImm();
479 if (Is3 + Imm >= 32)
481 return decodeUImmOperand<5>(Inst, Imm, Address, Decoder);
482}
483
485 uint64_t Address,
486 const MCDisassembler *Decoder) {
487 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
488 if (Imm < RISCVZC::RA || (IsRVE && Imm >= RISCVZC::RA_S0_S2))
492}
493
495 uint64_t Address,
496 const MCDisassembler *Decoder) {
497 if (Imm < RISCVZC::RA_S0)
499 return decodeZcmpRlist(Inst, Imm, Address, Decoder);
500}
501
502#include "RISCVGenDisassemblerTables.inc"
503
504namespace {
505
506struct DecoderListEntry {
507 const uint8_t *Table;
508 FeatureBitset ContainedFeatures;
509 const char *Desc;
510
511 bool haveContainedFeatures(const FeatureBitset &ActiveFeatures) const {
512 return ContainedFeatures.none() ||
513 (ContainedFeatures & ActiveFeatures).any();
514 }
515};
516
517} // end anonymous namespace
518
519static constexpr FeatureBitset XCVFeatureGroup = {
520 RISCV::FeatureVendorXCVbitmanip, RISCV::FeatureVendorXCVelw,
521 RISCV::FeatureVendorXCVmac, RISCV::FeatureVendorXCVmem,
522 RISCV::FeatureVendorXCValu, RISCV::FeatureVendorXCVsimd,
523 RISCV::FeatureVendorXCVbi};
524
525static constexpr FeatureBitset XqciFeatureGroup = {
526 RISCV::FeatureVendorXqcia, RISCV::FeatureVendorXqciac,
527 RISCV::FeatureVendorXqcibi, RISCV::FeatureVendorXqcibm,
528 RISCV::FeatureVendorXqcicli, RISCV::FeatureVendorXqcicm,
529 RISCV::FeatureVendorXqcics, RISCV::FeatureVendorXqcicsr,
530 RISCV::FeatureVendorXqciint, RISCV::FeatureVendorXqciio,
531 RISCV::FeatureVendorXqcilb, RISCV::FeatureVendorXqcili,
532 RISCV::FeatureVendorXqcilia, RISCV::FeatureVendorXqcilo,
533 RISCV::FeatureVendorXqcilsm, RISCV::FeatureVendorXqcisim,
534 RISCV::FeatureVendorXqcisls, RISCV::FeatureVendorXqcisync,
535};
536
537static constexpr FeatureBitset XSfVectorGroup = {
538 RISCV::FeatureVendorXSfvcp, RISCV::FeatureVendorXSfvqmaccdod,
539 RISCV::FeatureVendorXSfvqmaccqoq, RISCV::FeatureVendorXSfvfwmaccqqq,
540 RISCV::FeatureVendorXSfvfnrclipxfqf, RISCV::FeatureVendorXSfmmbase,
541 RISCV::FeatureVendorXSfvfexpa, RISCV::FeatureVendorXSfvfexpa64e,
542 RISCV::FeatureVendorXSfvfbfexp16e, RISCV::FeatureVendorXSfvfexp16e,
543 RISCV::FeatureVendorXSfvfexp32e};
544static constexpr FeatureBitset XSfSystemGroup = {
545 RISCV::FeatureVendorXSiFivecdiscarddlone,
546 RISCV::FeatureVendorXSiFivecflushdlone,
547};
548
549static constexpr FeatureBitset XMIPSGroup = {
550 RISCV::FeatureVendorXMIPSLSP,
551 RISCV::FeatureVendorXMIPSCMov,
552 RISCV::FeatureVendorXMIPSCBOP,
553 RISCV::FeatureVendorXMIPSEXECTL,
554};
555
556static constexpr FeatureBitset XTHeadGroup = {
557 RISCV::FeatureVendorXTHeadBa, RISCV::FeatureVendorXTHeadBb,
558 RISCV::FeatureVendorXTHeadBs, RISCV::FeatureVendorXTHeadCondMov,
559 RISCV::FeatureVendorXTHeadCmo, RISCV::FeatureVendorXTHeadFMemIdx,
560 RISCV::FeatureVendorXTHeadMac, RISCV::FeatureVendorXTHeadMemIdx,
561 RISCV::FeatureVendorXTHeadMemPair, RISCV::FeatureVendorXTHeadSync,
562 RISCV::FeatureVendorXTHeadVdot};
563
564static constexpr FeatureBitset XAndesGroup = {
565 RISCV::FeatureVendorXAndesPerf, RISCV::FeatureVendorXAndesBFHCvt,
566 RISCV::FeatureVendorXAndesVBFHCvt, RISCV::FeatureVendorXAndesVSIntH,
567 RISCV::FeatureVendorXAndesVSIntLoad, RISCV::FeatureVendorXAndesVPackFPH,
568 RISCV::FeatureVendorXAndesVDot};
569
570static constexpr FeatureBitset XSMTGroup = {RISCV::FeatureVendorXSMTVDot,
571 RISCV::FeatureVendorXSMTVDotII};
572
573static constexpr FeatureBitset XAIFGroup = {RISCV::FeatureVendorXAIFET};
574
575static constexpr DecoderListEntry DecoderList32[]{
576 // Vendor Extensions
577 {DecoderTableXCV32, XCVFeatureGroup, "CORE-V extensions"},
578 {DecoderTableXqci32, XqciFeatureGroup, "Qualcomm uC Extensions"},
579 {DecoderTableXTHead32, XTHeadGroup, "T-Head extensions"},
580 {DecoderTableXSfvector32, XSfVectorGroup, "SiFive vector extensions"},
581 {DecoderTableXSfsystem32, XSfSystemGroup, "SiFive system extensions"},
582 {DecoderTableXSfcease32, {RISCV::FeatureVendorXSfcease}, "SiFive sf.cease"},
583 {DecoderTableXMIPS32, XMIPSGroup, "Mips extensions"},
584 {DecoderTableXAndes32, XAndesGroup, "Andes extensions"},
585 {DecoderTableXSMT32, XSMTGroup, "SpacemiT extensions"},
586 {DecoderTableXAIF32, XAIFGroup, "AI Foundry extensions"},
587 // Standard Extensions
588 {DecoderTable32, {}, "standard 32-bit instructions"},
589 {DecoderTableRV32Only32, {}, "RV32-only standard 32-bit instructions"},
590 {DecoderTableZfinx32, {}, "Zfinx (Float in Integer)"},
591 {DecoderTableZdinxRV32Only32, {}, "RV32-only Zdinx (Double in Integer)"},
592};
593
594namespace {
595// Define bitwidths for various types used to instantiate the decoder.
596template <> constexpr uint32_t InsnBitWidth<uint16_t> = 16;
597template <> constexpr uint32_t InsnBitWidth<uint32_t> = 32;
598// Use uint64_t to represent 48 bit instructions.
599template <> constexpr uint32_t InsnBitWidth<uint64_t> = 48;
600} // namespace
601
602DecodeStatus RISCVDisassembler::getInstruction32(MCInst &MI, uint64_t &Size,
603 ArrayRef<uint8_t> Bytes,
604 uint64_t Address,
605 raw_ostream &CS) const {
606 if (Bytes.size() < 4) {
607 Size = 0;
609 }
610 Size = 4;
611
612 uint32_t Insn = support::endian::read32le(Bytes.data());
613
614 for (const DecoderListEntry &Entry : DecoderList32) {
615 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
616 continue;
617
618 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
620 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
621 if (Result == MCDisassembler::Fail)
622 continue;
623
624 return Result;
625 }
626
628}
629
630static constexpr DecoderListEntry DecoderList16[]{
631 // Vendor Extensions
632 {DecoderTableXqci16, XqciFeatureGroup, "Qualcomm uC 16-bit"},
633 {DecoderTableXqccmp16,
634 {RISCV::FeatureVendorXqccmp},
635 "Xqccmp (Qualcomm 16-bit Push/Pop & Double Move Instructions)"},
636 {DecoderTableXqccmi16,
637 {RISCV::FeatureVendorXqccmi},
638 "Xqccmi (Qualcomm 16-bit Instruction Lookup Table)"},
639 {DecoderTableXqccmt16,
640 {RISCV::FeatureVendorXqccmt},
641 "Xqccmt (Qualcomm 16-bit Table Jump Instructions)"},
642 {DecoderTableXwchc16, {RISCV::FeatureVendorXwchc}, "WCH QingKe XW"},
643 // Standard Extensions
644 {DecoderTable16, {}, "standard 16-bit instructions"},
645 {DecoderTableRV32Only16, {}, "RV32-only 16-bit instructions"},
646 // Zc* instructions incompatible with Zcf or Zcd
647 {DecoderTableZcOverlap16,
648 {},
649 "ZcOverlap (16-bit Instructions overlapping with Zcf/Zcd)"},
650};
651
652DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
653 ArrayRef<uint8_t> Bytes,
655 raw_ostream &CS) const {
656 if (Bytes.size() < 2) {
657 Size = 0;
659 }
660 Size = 2;
661
662 uint16_t Insn = support::endian::read16le(Bytes.data());
663
664 for (const DecoderListEntry &Entry : DecoderList16) {
665 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
666 continue;
667
668 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
670 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
671 if (Result != MCDisassembler::Fail)
672 return Result;
673 }
674
676}
677
678static constexpr DecoderListEntry DecoderList48[]{
679 {DecoderTableXqci48, XqciFeatureGroup, "Qualcomm uC 48bit"},
680};
681
682DecodeStatus RISCVDisassembler::getInstruction48(MCInst &MI, uint64_t &Size,
683 ArrayRef<uint8_t> Bytes,
685 raw_ostream &CS) const {
686 if (Bytes.size() < 6) {
687 Size = 0;
689 }
690 Size = 6;
691
692 uint64_t Insn = 0;
693 for (size_t i = Size; i-- != 0;)
694 Insn += (static_cast<uint64_t>(Bytes[i]) << 8 * i);
695
696 for (const DecoderListEntry &Entry : DecoderList48) {
697 if (!Entry.haveContainedFeatures(STI.getFeatureBits()))
698 continue;
699
700 LLVM_DEBUG(dbgs() << "Trying " << Entry.Desc << " table:\n");
702 decodeInstruction(Entry.Table, MI, Insn, Address, this, STI);
703 if (Result == MCDisassembler::Fail)
704 continue;
705
706 return Result;
707 }
708
710}
711
712DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
713 ArrayRef<uint8_t> Bytes,
715 raw_ostream &CS) const {
716 CommentStream = &CS;
717 // It's a 16 bit instruction if bit 0 and 1 are not 0b11.
718 if ((Bytes[0] & 0b11) != 0b11)
719 return getInstruction16(MI, Size, Bytes, Address, CS);
720
721 // Try to decode as a 32-bit instruction first.
722 DecodeStatus Result = getInstruction32(MI, Size, Bytes, Address, CS);
723 if (Result != MCDisassembler::Fail)
724 return Result;
725
726 // If bits [4:2] are 0b111 this might be a 48-bit or larger instruction,
727 // otherwise assume it's an unknown 32-bit instruction.
728 if ((Bytes[0] & 0b1'1100) != 0b1'1100) {
729 Size = Bytes.size() >= 4 ? 4 : 0;
731 }
732
733 // 48-bit instructions are encoded as 0bxx011111.
734 if ((Bytes[0] & 0b11'1111) == 0b01'1111)
735 return getInstruction48(MI, Size, Bytes, Address, CS);
736
737 // 64-bit instructions are encoded as 0x0111111.
738 if ((Bytes[0] & 0b111'1111) == 0b011'1111) {
739 Size = Bytes.size() >= 8 ? 8 : 0;
741 }
742
743 // Remaining cases need to check a second byte.
744 if (Bytes.size() < 2) {
745 Size = 0;
747 }
748
749 // 80-bit through 176-bit instructions are encoded as 0bxnnnxxxx_x1111111.
750 // Where the number of bits is (80 + (nnn * 16)) for nnn != 0b111.
751 unsigned nnn = (Bytes[1] >> 4) & 0b111;
752 if (nnn != 0b111) {
753 Size = 10 + (nnn * 2);
754 if (Bytes.size() < Size)
755 Size = 0;
757 }
758
759 // Remaining encodings are reserved for > 176-bit instructions.
760 Size = 0;
762}
MCDisassembler::DecodeStatus DecodeStatus
static DecodeStatus DecodeSimpleRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
unsigned uint64_t
unsigned const MCDisassembler * Decoder
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
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 XAIFGroup
static DecodeStatus decodeFixedImmOperand(MCInst &Inst, const MCDisassembler *Decoder)
constexpr auto DecodeGPRNoX31RegisterClass
static DecodeStatus decodeUImm7EqXLenOperand(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)
constexpr auto DecodeGPRPairNoX0RegisterClass
constexpr bool PredNoX2(uint32_t RegNo)
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[]
constexpr auto DecodeGPRX1X5RegisterClass
static DecodeStatus decodeCVInsertIs2Operand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmLog2XLenNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createRISCVDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus DecodeTRM2RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVectorRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeTHMemPairRs1Operand(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[]
constexpr bool PredNoX0(uint32_t RegNo)
static DecodeStatus decodeXqccmpRlistS0(MCInst &Inst, uint32_t Imm, uint64_t Address, const MCDisassembler *Decoder)
static MCSymbolizer * createRISCVMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
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 decodeUImmPlus1Operand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImm20Operand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
constexpr bool PredX1OrX5(uint32_t RegNo)
static DecodeStatus DecodeGPRPairRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeSImm12LoOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
constexpr auto DecodeGPRNoX2RegisterClass
static DecodeStatus decodeImmZibiOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XMIPSGroup
constexpr auto DecodeGPRRegisterClass
static DecodeStatus decodeSImmNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFilteredRegisterClass(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 DecodeYBNDSWImm(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
constexpr bool PredNoX31(uint32_t RegNo)
static DecodeStatus decodeTHMemIdxRs1Operand(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 constexpr FeatureBitset XTHeadGroup
static DecodeStatus DecodeSimpleRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeSingleRegister(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRS07RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const void *Decoder)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVDisassembler()
static DecodeStatus decodeVMaskReg(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
constexpr auto DecodeGPRNoX0RegisterClass
static DecodeStatus decodeUImmNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static constexpr FeatureBitset XSfSystemGroup
static DecodeStatus DecodeGPRPairCRegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
Container class for subtarget features.
Context object for machine code objects.
Definition MCContext.h:83
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getNumOperands() const
Definition MCInst.h:212
void addOperand(const MCOperand Op)
Definition MCInst.h:215
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
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:41
Generic base class for all target subtargets.
Symbolize and annotate disassembled instructions.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
The type for the symbol lookup function.
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t OpSize, uint64_t InstSize, int TagType, void *TagBuf)
The type for the operand information call back function.
constexpr bool any(E Val)
@ Entry
Definition COFF.h:862
static bool isValidRoundingMode(unsigned Mode)
uint16_t read16le(const void *P)
Definition Endian.h:409
uint32_t read32le(const void *P)
Definition Endian.h:412
This is an optimization pass for GlobalISel generic memory operations.
Target & getTheRISCV32Target()
Target & getTheRISCV64beTarget()
LLVM_ABI MCSymbolizer * createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
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:567
Target & getTheRISCV32beTarget()
#define N
static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn)
RegisterMCSymbolizer - Register an MCSymbolizer implementation for the given target.
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.