LLVM 20.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"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/Support/Endian.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "riscv-disassembler"
29
31
32namespace {
33class RISCVDisassembler : public MCDisassembler {
34 std::unique_ptr<MCInstrInfo const> const MCII;
35
36public:
37 RISCVDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
38 MCInstrInfo const *MCII)
39 : MCDisassembler(STI, Ctx), MCII(MCII) {}
40
43 raw_ostream &CStream) const override;
44
45private:
46 void addSPOperands(MCInst &MI) const;
47
48 DecodeStatus getInstruction32(MCInst &Instr, uint64_t &Size,
50 raw_ostream &CStream) const;
51 DecodeStatus getInstruction16(MCInst &Instr, uint64_t &Size,
53 raw_ostream &CStream) const;
54};
55} // end anonymous namespace
56
58 const MCSubtargetInfo &STI,
59 MCContext &Ctx) {
60 return new RISCVDisassembler(STI, Ctx, T.createMCInstrInfo());
61}
62
64 // Register the disassembler for each target.
69}
70
72 uint64_t Address,
73 const MCDisassembler *Decoder) {
74 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
75
76 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
78
79 MCRegister Reg = RISCV::X0 + RegNo;
82}
83
85 uint64_t Address,
86 const MCDisassembler *Decoder) {
87 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
88
89 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
91
92 MCRegister Reg = RISCV::X0_H + RegNo;
95}
96
98 uint64_t Address,
99 const MCDisassembler *Decoder) {
100 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
101
102 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
104
105 MCRegister Reg = RISCV::X0_W + RegNo;
108}
109
111 uint64_t Address,
112 const MCDisassembler *Decoder) {
113 MCRegister Reg = RISCV::X0 + RegNo;
114 if (Reg != RISCV::X1 && Reg != RISCV::X5)
116
119}
120
122 uint64_t Address,
123 const MCDisassembler *Decoder) {
124 if (RegNo >= 32)
126
127 MCRegister Reg = RISCV::F0_H + RegNo;
130}
131
133 uint64_t Address,
134 const MCDisassembler *Decoder) {
135 if (RegNo >= 32)
137
138 MCRegister Reg = RISCV::F0_F + RegNo;
141}
142
144 uint64_t Address,
145 const MCDisassembler *Decoder) {
146 if (RegNo >= 8) {
148 }
149 MCRegister Reg = RISCV::F8_F + RegNo;
152}
153
155 uint64_t Address,
156 const MCDisassembler *Decoder) {
157 if (RegNo >= 32)
159
160 MCRegister Reg = RISCV::F0_D + RegNo;
163}
164
166 uint64_t Address,
167 const MCDisassembler *Decoder) {
168 if (RegNo >= 8) {
170 }
171 MCRegister Reg = RISCV::F8_D + RegNo;
174}
175
177 uint64_t Address,
178 const MCDisassembler *Decoder) {
179 if (RegNo == 0) {
181 }
182
183 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
184}
185
186static DecodeStatus
188 const MCDisassembler *Decoder) {
189 if (RegNo == 2) {
191 }
192
193 return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder);
194}
195
197 uint64_t Address,
198 const MCDisassembler *Decoder) {
199 if (RegNo >= 8)
201
202 MCRegister Reg = RISCV::X8 + RegNo;
205}
206
208 uint64_t Address,
209 const MCDisassembler *Decoder) {
210 if (RegNo >= 32 || RegNo % 2)
212
213 const RISCVDisassembler *Dis =
214 static_cast<const RISCVDisassembler *>(Decoder);
215 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
217 RISCV::X0 + RegNo, RISCV::sub_gpr_even,
218 &RISCVMCRegisterClasses[RISCV::GPRPairRegClassID]);
221}
222
224 uint64_t Address,
225 const void *Decoder) {
226 if (RegNo >= 8)
228
229 MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV::X8) : (RegNo - 2 + RISCV::X18);
232}
233
235 uint64_t Address,
236 const MCDisassembler *Decoder) {
237 if (RegNo >= 32)
239
240 MCRegister Reg = RISCV::V0 + RegNo;
243}
244
246 uint64_t Address,
247 const MCDisassembler *Decoder) {
248 if (RegNo >= 32 || RegNo % 2)
250
251 const RISCVDisassembler *Dis =
252 static_cast<const RISCVDisassembler *>(Decoder);
253 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
254 MCRegister Reg =
255 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
256 &RISCVMCRegisterClasses[RISCV::VRM2RegClassID]);
257
260}
261
263 uint64_t Address,
264 const MCDisassembler *Decoder) {
265 if (RegNo >= 32 || RegNo % 4)
267
268 const RISCVDisassembler *Dis =
269 static_cast<const RISCVDisassembler *>(Decoder);
270 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
271 MCRegister Reg =
272 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
273 &RISCVMCRegisterClasses[RISCV::VRM4RegClassID]);
274
277}
278
280 uint64_t Address,
281 const MCDisassembler *Decoder) {
282 if (RegNo >= 32 || RegNo % 8)
284
285 const RISCVDisassembler *Dis =
286 static_cast<const RISCVDisassembler *>(Decoder);
287 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
288 MCRegister Reg =
289 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
290 &RISCVMCRegisterClasses[RISCV::VRM8RegClassID]);
291
294}
295
297 uint64_t Address,
298 const MCDisassembler *Decoder) {
299 if (RegNo >= 2)
301
302 MCRegister Reg = (RegNo == 0) ? RISCV::V0 : RISCV::NoRegister;
303
306}
307
308template <unsigned N>
310 int64_t Address,
311 const MCDisassembler *Decoder) {
312 assert(isUInt<N>(Imm) && "Invalid immediate");
315}
316
318 int64_t Address,
319 const MCDisassembler *Decoder) {
320 assert(isUInt<6>(Imm) && "Invalid immediate");
321
322 if (!Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) &&
323 !isUInt<5>(Imm))
325
328}
329
330template <unsigned N>
332 int64_t Address,
333 const MCDisassembler *Decoder) {
334 if (Imm == 0)
336 return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
337}
338
339static DecodeStatus
341 const MCDisassembler *Decoder) {
342 if (Imm == 0)
344 return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
345}
346
347template <unsigned N>
349 int64_t Address,
350 const MCDisassembler *Decoder) {
351 assert(isUInt<N>(Imm) && "Invalid immediate");
352 // Sign-extend the number in the bottom N bits of Imm
353 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
355}
356
357template <unsigned N>
359 int64_t Address,
360 const MCDisassembler *Decoder) {
361 if (Imm == 0)
363 return decodeSImmOperand<N>(Inst, Imm, Address, Decoder);
364}
365
366template <unsigned N>
368 int64_t Address,
369 const MCDisassembler *Decoder) {
370 assert(isUInt<N>(Imm) && "Invalid immediate");
371 // Sign-extend the number in the bottom N bits of Imm after accounting for
372 // the fact that the N bit immediate is stored in N-1 bits (the LSB is
373 // always zero)
374 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm << 1)));
376}
377
379 int64_t Address,
380 const MCDisassembler *Decoder) {
381 assert(isUInt<6>(Imm) && "Invalid immediate");
382 if (Imm > 31) {
383 Imm = (SignExtend64<6>(Imm) & 0xfffff);
384 }
387}
388
389static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address,
390 const MCDisassembler *Decoder) {
391 assert(isUInt<3>(Imm) && "Invalid immediate");
394
397}
398
399static DecodeStatus decodeRTZArg(MCInst &Inst, uint32_t Imm, int64_t Address,
400 const MCDisassembler *Decoder) {
401 assert(isUInt<3>(Imm) && "Invalid immediate");
402 if (Imm != RISCVFPRndMode::RTZ)
404
407}
408
410 uint64_t Address,
411 const MCDisassembler *Decoder);
412
414 uint64_t Address,
415 const MCDisassembler *Decoder);
416
418 uint64_t Address,
419 const MCDisassembler *Decoder);
420
422 uint64_t Address,
423 const MCDisassembler *Decoder);
424
426 uint64_t Address,
427 const MCDisassembler *Decoder);
428
430 uint64_t Address,
431 const MCDisassembler *Decoder);
432
434 uint64_t Address, const void *Decoder);
435
437 const MCDisassembler *Decoder);
438
440 uint64_t Address, const void *Decoder);
441
443 uint64_t Address,
444 const MCDisassembler *Decoder);
445
446#include "RISCVGenDisassemblerTables.inc"
447
449 uint64_t Address,
450 const MCDisassembler *Decoder) {
451 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
452 [[maybe_unused]] DecodeStatus Result =
453 DecodeGPRNoX0RegisterClass(Inst, Rd, Address, Decoder);
454 assert(Result == MCDisassembler::Success && "Invalid register");
455 Inst.addOperand(Inst.getOperand(0));
458}
459
461 uint64_t Address,
462 const MCDisassembler *Decoder) {
463 uint32_t Rs1 = fieldFromInstruction(Insn, 7, 5);
464 [[maybe_unused]] DecodeStatus Result =
465 DecodeGPRX1X5RegisterClass(Inst, Rs1, Address, Decoder);
466 assert(Result == MCDisassembler::Success && "Invalid register");
468}
469
471 uint64_t Address,
472 const MCDisassembler *Decoder) {
473 Inst.addOperand(MCOperand::createReg(RISCV::X0));
474 uint32_t SImm6 =
475 fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5);
476 [[maybe_unused]] DecodeStatus Result =
477 decodeSImmOperand<6>(Inst, SImm6, Address, Decoder);
478 assert(Result == MCDisassembler::Success && "Invalid immediate");
480}
481
483 uint64_t Address,
484 const MCDisassembler *Decoder) {
485 Inst.addOperand(MCOperand::createReg(RISCV::X0));
486 Inst.addOperand(Inst.getOperand(0));
487 uint32_t UImm6 =
488 fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5);
489 [[maybe_unused]] DecodeStatus Result =
490 decodeUImmOperand<6>(Inst, UImm6, Address, Decoder);
491 assert(Result == MCDisassembler::Success && "Invalid immediate");
493}
494
496 uint64_t Address,
497 const MCDisassembler *Decoder) {
498 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
499 uint32_t Rs2 = fieldFromInstruction(Insn, 2, 5);
500 DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
501 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
503}
504
506 uint64_t Address,
507 const MCDisassembler *Decoder) {
508 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
509 uint32_t Rs2 = fieldFromInstruction(Insn, 2, 5);
510 DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
511 Inst.addOperand(Inst.getOperand(0));
512 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
514}
515
517 uint64_t Address,
518 const MCDisassembler *Decoder) {
519 uint32_t Rd1 = fieldFromInstruction(Insn, 7, 5);
520 uint32_t Rs1 = fieldFromInstruction(Insn, 15, 5);
521 uint32_t Rd2 = fieldFromInstruction(Insn, 20, 5);
522 uint32_t UImm2 = fieldFromInstruction(Insn, 25, 2);
523 DecodeGPRRegisterClass(Inst, Rd1, Address, Decoder);
524 DecodeGPRRegisterClass(Inst, Rd2, Address, Decoder);
525 DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
526 [[maybe_unused]] DecodeStatus Result =
527 decodeUImmOperand<2>(Inst, UImm2, Address, Decoder);
528 assert(Result == MCDisassembler::Success && "Invalid immediate");
529
530 // Disassemble the final operand which is implicit.
531 unsigned Opcode = Inst.getOpcode();
532 bool IsWordOp = (Opcode == RISCV::TH_LWD || Opcode == RISCV::TH_LWUD ||
533 Opcode == RISCV::TH_SWD);
534 if (IsWordOp)
536 else
538
540}
541
543 uint64_t Address, const void *Decoder) {
544 if (Imm <= 3)
548}
549
551 const MCDisassembler *Decoder) {
552 uint32_t Rs1 = fieldFromInstruction(Insn, 0, 5);
553 uint32_t Rs2 = fieldFromInstruction(Insn, 5, 5);
554 DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
555 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
557}
558
560 uint64_t Address, const void *Decoder) {
563}
564
565// Add implied SP operand for C.*SP compressed instructions. The SP operand
566// isn't explicitly encoded in the instruction.
567void RISCVDisassembler::addSPOperands(MCInst &MI) const {
568 const MCInstrDesc &MCID = MCII->get(MI.getOpcode());
569 for (unsigned i = 0; i < MCID.getNumOperands(); i++)
570 if (MCID.operands()[i].RegClass == RISCV::SPRegClassID)
571 MI.insert(MI.begin() + i, MCOperand::createReg(RISCV::X2));
572}
573
574#define TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, \
575 DESC, ADDITIONAL_OPERATION) \
576 do { \
577 if (FEATURE_CHECKS) { \
578 LLVM_DEBUG(dbgs() << "Trying " DESC ":\n"); \
579 DecodeStatus Result = \
580 decodeInstruction(DECODER_TABLE, MI, Insn, Address, this, STI); \
581 if (Result != MCDisassembler::Fail) { \
582 ADDITIONAL_OPERATION; \
583 return Result; \
584 } \
585 } \
586 } while (false)
587#define TRY_TO_DECODE_AND_ADD_SP(FEATURE_CHECKS, DECODER_TABLE, DESC) \
588 TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
589 addSPOperands(MI))
590#define TRY_TO_DECODE(FEATURE_CHECKS, DECODER_TABLE, DESC) \
591 TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
592 (void)nullptr)
593#define TRY_TO_DECODE_FEATURE(FEATURE, DECODER_TABLE, DESC) \
594 TRY_TO_DECODE(STI.hasFeature(FEATURE), DECODER_TABLE, DESC)
595
596DecodeStatus RISCVDisassembler::getInstruction32(MCInst &MI, uint64_t &Size,
597 ArrayRef<uint8_t> Bytes,
598 uint64_t Address,
599 raw_ostream &CS) const {
600 if (Bytes.size() < 4) {
601 Size = 0;
603 }
604 Size = 4;
605
607
608 TRY_TO_DECODE(STI.hasFeature(RISCV::FeatureStdExtZdinx) &&
609 !STI.hasFeature(RISCV::Feature64Bit),
610 DecoderTableRV32Zdinx32,
611 "RV32Zdinx table (Double in Integer and rv32)");
612 TRY_TO_DECODE(STI.hasFeature(RISCV::FeatureStdExtZacas) &&
613 !STI.hasFeature(RISCV::Feature64Bit),
614 DecoderTableRV32Zacas32,
615 "RV32Zacas table (Compare-And-Swap and rv32)");
616 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZfinx, DecoderTableRVZfinx32,
617 "RVZfinx table (Float in Integer)");
618 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXVentanaCondOps,
619 DecoderTableXVentana32, "Ventana custom opcode table");
620 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBa, DecoderTableXTHeadBa32,
621 "XTHeadBa custom opcode table");
622 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBb, DecoderTableXTHeadBb32,
623 "XTHeadBb custom opcode table");
624 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBs, DecoderTableXTHeadBs32,
625 "XTHeadBs custom opcode table");
626 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCondMov,
627 DecoderTableXTHeadCondMov32,
628 "XTHeadCondMov custom opcode table");
629 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCmo, DecoderTableXTHeadCmo32,
630 "XTHeadCmo custom opcode table");
631 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadFMemIdx,
632 DecoderTableXTHeadFMemIdx32,
633 "XTHeadFMemIdx custom opcode table");
634 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMac, DecoderTableXTHeadMac32,
635 "XTHeadMac custom opcode table");
636 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemIdx,
637 DecoderTableXTHeadMemIdx32,
638 "XTHeadMemIdx custom opcode table");
639 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemPair,
640 DecoderTableXTHeadMemPair32,
641 "XTHeadMemPair custom opcode table");
642 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadSync,
643 DecoderTableXTHeadSync32,
644 "XTHeadSync custom opcode table");
645 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadVdot,
646 DecoderTableXTHeadVdot32,
647 "XTHeadVdot custom opcode table");
648 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSfvcp, DecoderTableXSfvcp32,
649 "SiFive VCIX custom opcode table");
651 RISCV::FeatureVendorXSfvqmaccdod, DecoderTableXSfvqmaccdod32,
652 "SiFive Matrix Multiplication (2x8 and 8x2) Instruction opcode table");
654 RISCV::FeatureVendorXSfvqmaccqoq, DecoderTableXSfvqmaccqoq32,
655 "SiFive Matrix Multiplication (4x8 and 8x4) Instruction opcode table");
657 RISCV::FeatureVendorXSfvfwmaccqqq, DecoderTableXSfvfwmaccqqq32,
658 "SiFive Matrix Multiplication Instruction opcode table");
660 RISCV::FeatureVendorXSfvfnrclipxfqf, DecoderTableXSfvfnrclipxfqf32,
661 "SiFive FP32-to-int8 Ranged Clip Instructions opcode table");
662 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSiFivecdiscarddlone,
663 DecoderTableXSiFivecdiscarddlone32,
664 "SiFive sf.cdiscard.d.l1 custom opcode table");
665 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSiFivecflushdlone,
666 DecoderTableXSiFivecflushdlone32,
667 "SiFive sf.cflush.d.l1 custom opcode table");
668 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSfcease, DecoderTableXSfcease32,
669 "SiFive sf.cease custom opcode table");
670 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVbitmanip,
671 DecoderTableXCVbitmanip32,
672 "CORE-V Bit Manipulation custom opcode table");
673 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVelw, DecoderTableXCVelw32,
674 "CORE-V Event load custom opcode table");
675 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVmac, DecoderTableXCVmac32,
676 "CORE-V MAC custom opcode table");
677 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVmem, DecoderTableXCVmem32,
678 "CORE-V MEM custom opcode table");
679 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCValu, DecoderTableXCValu32,
680 "CORE-V ALU custom opcode table");
681 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVsimd, DecoderTableXCVsimd32,
682 "CORE-V SIMD extensions custom opcode table");
683 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVbi, DecoderTableXCVbi32,
684 "CORE-V Immediate Branching custom opcode table");
685 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcicsr, DecoderTableXqcicsr32,
686 "Qualcomm uC CSR custom opcode table");
687 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcisls, DecoderTableXqcisls32,
688 "Qualcomm uC Scaled Load Store custom opcode table");
689 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcia, DecoderTableXqcia32,
690 "Qualcomm uC Arithmetic custom opcode table");
691 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcics, DecoderTableXqcics32,
692 "Qualcomm uC Conditional Select custom opcode table");
693 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcilsm, DecoderTableXqcilsm32,
694 "Qualcomm uC Load Store Multiple custom opcode table");
695 TRY_TO_DECODE(true, DecoderTable32, "RISCV32 table");
696
698}
699
700DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
701 ArrayRef<uint8_t> Bytes,
702 uint64_t Address,
703 raw_ostream &CS) const {
704 if (Bytes.size() < 2) {
705 Size = 0;
707 }
708 Size = 2;
709
711 TRY_TO_DECODE_AND_ADD_SP(!STI.hasFeature(RISCV::Feature64Bit),
712 DecoderTableRISCV32Only_16,
713 "RISCV32Only_16 table (16-bit Instruction)");
714 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZicfiss, DecoderTableZicfiss16,
715 "RVZicfiss table (Shadow Stack)");
716 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZcmt, DecoderTableRVZcmt16,
717 "Zcmt table (16-bit Table Jump Instructions)");
719 RISCV::FeatureStdExtZcmp, DecoderTableRVZcmp16,
720 "Zcmp table (16-bit Push/Pop & Double Move Instructions)");
721 TRY_TO_DECODE_AND_ADD_SP(STI.hasFeature(RISCV::FeatureVendorXwchc),
722 DecoderTableXwchc16,
723 "WCH QingKe XW custom opcode table");
724 TRY_TO_DECODE_AND_ADD_SP(true, DecoderTable16,
725 "RISCV_C table (16-bit Instruction)");
726
728}
729
730DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
731 ArrayRef<uint8_t> Bytes,
732 uint64_t Address,
733 raw_ostream &CS) const {
734 // It's a 16 bit instruction if bit 0 and 1 are not 0b11.
735 if ((Bytes[0] & 0b11) != 0b11)
736 return getInstruction16(MI, Size, Bytes, Address, CS);
737
738 // It's a 32 bit instruction if bit 1:0 are 0b11(checked above) and bits 4:2
739 // are not 0b111.
740 if ((Bytes[0] & 0b1'1100) != 0b1'1100)
741 return getInstruction32(MI, Size, Bytes, Address, CS);
742
743 // 48-bit instructions are encoded as 0bxx011111.
744 if ((Bytes[0] & 0b11'1111) == 0b01'1111) {
745 Size = Bytes.size() >= 6 ? 6 : 0;
747 }
748
749 // 64-bit instructions are encoded as 0x0111111.
750 if ((Bytes[0] & 0b111'1111) == 0b011'1111) {
751 Size = Bytes.size() >= 8 ? 8 : 0;
753 }
754
755 // Remaining cases need to check a second byte.
756 if (Bytes.size() < 2) {
757 Size = 0;
759 }
760
761 // 80-bit through 176-bit instructions are encoded as 0bxnnnxxxx_x1111111.
762 // Where the number of bits is (80 + (nnn * 16)) for nnn != 0b111.
763 unsigned nnn = (Bytes[1] >> 4) & 0b111;
764 if (nnn != 0b111) {
765 Size = 10 + (nnn * 2);
766 if (Bytes.size() < Size)
767 Size = 0;
769 }
770
771 // Remaining encodings are reserved for > 176-bit instructions.
772 Size = 0;
774}
SmallVector< AArch64_IMM::ImmInsnModel, 4 > Insn
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
uint64_t Size
IRTranslator LLVM IR MI
#define TRY_TO_DECODE(FEATURE_CHECKS, DECODER_TABLE, DESC)
static DecodeStatus decodeRegReg(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeZcmpRlist(MCInst &Inst, uint32_t Imm, uint64_t Address, const void *Decoder)
static DecodeStatus decodeRVCInstrRdRs1Rs2(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmLog2XLenOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
MCDisassembler::DecodeStatus DecodeStatus
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRX1X5RegisterClass(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 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 decodeRVCInstrRdRs1ImmZero(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRF16RegisterClass(MCInst &Inst, uint32_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
#define TRY_TO_DECODE_FEATURE(FEATURE, DECODER_TABLE, DESC)
static DecodeStatus decodeSImmOperandAndLsl1(MCInst &Inst, uint32_t Imm, int64_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 decodeCLUIImmOperand(MCInst &Inst, uint32_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeCSSPushPopchk(MCInst &Inst, uint32_t Insn, 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 decodeRVCInstrRdSImm(MCInst &Inst, uint32_t Insn, uint64_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 decodeZcmpSpimm(MCInst &Inst, uint32_t Imm, uint64_t Address, const void *Decoder)
static DecodeStatus DecodeGPRNoX0X2RegisterClass(MCInst &Inst, uint64_t RegNo, uint32_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeRVCInstrRdRs2(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeSImmNonZeroOperand(MCInst &Inst, uint32_t Imm, int64_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)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVDisassembler()
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 DecodeStatus decodeRVCInstrRdRs1UImm(MCInst &Inst, uint32_t Insn, uint64_t Address, const MCDisassembler *Decoder)
#define TRY_TO_DECODE_AND_ADD_SP(FEATURE_CHECKS, DECODER_TABLE, DESC)
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:168
const T * data() const
Definition: ArrayRef.h:165
Context object for machine code objects.
Definition: MCContext.h:83
Superclass for all disassemblers.
const MCSubtargetInfo & getSubtargetInfo() const
DecodeStatus
Ternary decode status.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
unsigned getOpcode() const
Definition: MCInst.h:199
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:207
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:237
ArrayRef< MCOperandInfo > operands() const
Definition: MCInstrDesc.h:239
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:142
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:52
static bool isValidRoundingMode(unsigned Mode)
uint16_t read16le(const void *P)
Definition: Endian.h:422
uint32_t read32le(const void *P)
Definition: Endian.h:425
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheRISCV32Target()
Target & getTheRISCV64Target()
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.