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 getInstruction48(MCInst &Instr, uint64_t &Size,
50 raw_ostream &CStream) const;
51
52 DecodeStatus getInstruction32(MCInst &Instr, uint64_t &Size,
54 raw_ostream &CStream) const;
55 DecodeStatus getInstruction16(MCInst &Instr, uint64_t &Size,
57 raw_ostream &CStream) const;
58};
59} // end anonymous namespace
60
62 const MCSubtargetInfo &STI,
63 MCContext &Ctx) {
64 return new RISCVDisassembler(STI, Ctx, T.createMCInstrInfo());
65}
66
68 // Register the disassembler for each target.
73}
74
76 uint64_t Address,
77 const MCDisassembler *Decoder) {
78 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
79
80 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
82
83 MCRegister Reg = RISCV::X0 + RegNo;
86}
87
89 uint64_t Address,
90 const MCDisassembler *Decoder) {
91 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
92
93 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
95
96 MCRegister Reg = RISCV::X0_H + RegNo;
99}
100
102 uint64_t Address,
103 const MCDisassembler *Decoder) {
104 bool IsRVE = Decoder->getSubtargetInfo().hasFeature(RISCV::FeatureStdExtE);
105
106 if (RegNo >= 32 || (IsRVE && RegNo >= 16))
108
109 MCRegister Reg = RISCV::X0_W + RegNo;
112}
113
115 uint64_t Address,
116 const MCDisassembler *Decoder) {
117 MCRegister Reg = RISCV::X0 + RegNo;
118 if (Reg != RISCV::X1 && Reg != RISCV::X5)
120
123}
124
126 uint64_t Address,
127 const MCDisassembler *Decoder) {
128 if (RegNo >= 32)
130
131 MCRegister Reg = RISCV::F0_H + RegNo;
134}
135
137 uint64_t Address,
138 const MCDisassembler *Decoder) {
139 if (RegNo >= 32)
141
142 MCRegister Reg = RISCV::F0_F + RegNo;
145}
146
148 uint64_t Address,
149 const MCDisassembler *Decoder) {
150 if (RegNo >= 8) {
152 }
153 MCRegister Reg = RISCV::F8_F + RegNo;
156}
157
159 uint64_t Address,
160 const MCDisassembler *Decoder) {
161 if (RegNo >= 32)
163
164 MCRegister Reg = RISCV::F0_D + RegNo;
167}
168
170 uint64_t Address,
171 const MCDisassembler *Decoder) {
172 if (RegNo >= 8) {
174 }
175 MCRegister Reg = RISCV::F8_D + RegNo;
178}
179
181 uint64_t Address,
182 const MCDisassembler *Decoder) {
183 if (RegNo == 0) {
185 }
186
187 return DecodeGPRRegisterClass(Inst, RegNo, Address, Decoder);
188}
189
190static DecodeStatus
192 const MCDisassembler *Decoder) {
193 if (RegNo == 2) {
195 }
196
197 return DecodeGPRNoX0RegisterClass(Inst, RegNo, Address, Decoder);
198}
199
201 uint64_t Address,
202 const MCDisassembler *Decoder) {
203 if (RegNo >= 8)
205
206 MCRegister Reg = RISCV::X8 + RegNo;
209}
210
212 uint64_t Address,
213 const MCDisassembler *Decoder) {
214 if (RegNo >= 32 || RegNo % 2)
216
217 const RISCVDisassembler *Dis =
218 static_cast<const RISCVDisassembler *>(Decoder);
219 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
221 RISCV::X0 + RegNo, RISCV::sub_gpr_even,
222 &RISCVMCRegisterClasses[RISCV::GPRPairRegClassID]);
225}
226
228 uint64_t Address,
229 const void *Decoder) {
230 if (RegNo >= 8)
232
233 MCRegister Reg = (RegNo < 2) ? (RegNo + RISCV::X8) : (RegNo - 2 + RISCV::X18);
236}
237
239 uint64_t Address,
240 const MCDisassembler *Decoder) {
241 if (RegNo >= 32)
243
244 MCRegister Reg = RISCV::V0 + RegNo;
247}
248
250 uint64_t Address,
251 const MCDisassembler *Decoder) {
252 if (RegNo >= 32 || RegNo % 2)
254
255 const RISCVDisassembler *Dis =
256 static_cast<const RISCVDisassembler *>(Decoder);
257 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
258 MCRegister Reg =
259 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
260 &RISCVMCRegisterClasses[RISCV::VRM2RegClassID]);
261
264}
265
267 uint64_t Address,
268 const MCDisassembler *Decoder) {
269 if (RegNo >= 32 || RegNo % 4)
271
272 const RISCVDisassembler *Dis =
273 static_cast<const RISCVDisassembler *>(Decoder);
274 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
275 MCRegister Reg =
276 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
277 &RISCVMCRegisterClasses[RISCV::VRM4RegClassID]);
278
281}
282
284 uint64_t Address,
285 const MCDisassembler *Decoder) {
286 if (RegNo >= 32 || RegNo % 8)
288
289 const RISCVDisassembler *Dis =
290 static_cast<const RISCVDisassembler *>(Decoder);
291 const MCRegisterInfo *RI = Dis->getContext().getRegisterInfo();
292 MCRegister Reg =
293 RI->getMatchingSuperReg(RISCV::V0 + RegNo, RISCV::sub_vrm1_0,
294 &RISCVMCRegisterClasses[RISCV::VRM8RegClassID]);
295
298}
299
301 uint64_t Address,
302 const MCDisassembler *Decoder) {
303 if (RegNo)
305
306 Inst.addOperand(MCOperand::createReg(RISCV::V0));
308}
309
311 uint64_t Address,
312 const MCDisassembler *Decoder) {
313 if (RegNo >= 2)
315
316 MCRegister Reg = (RegNo == 0) ? RISCV::V0 : RISCV::NoRegister;
317
320}
321
322template <unsigned N>
324 int64_t Address,
325 const MCDisassembler *Decoder) {
326 assert(isUInt<N>(Imm) && "Invalid immediate");
329}
330
332 int64_t Address,
333 const MCDisassembler *Decoder) {
334 assert(isUInt<6>(Imm) && "Invalid immediate");
335
336 if (!Decoder->getSubtargetInfo().hasFeature(RISCV::Feature64Bit) &&
337 !isUInt<5>(Imm))
339
342}
343
344template <unsigned N>
346 int64_t Address,
347 const MCDisassembler *Decoder) {
348 if (Imm == 0)
350 return decodeUImmOperand<N>(Inst, Imm, Address, Decoder);
351}
352
353static DecodeStatus
355 const MCDisassembler *Decoder) {
356 if (Imm == 0)
358 return decodeUImmLog2XLenOperand(Inst, Imm, Address, Decoder);
359}
360
361template <unsigned N>
363 int64_t Address,
364 const MCDisassembler *Decoder) {
365 assert(isUInt<N>(Imm) && "Invalid immediate");
366 // Sign-extend the number in the bottom N bits of Imm
367 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
369}
370
371template <unsigned N>
373 int64_t Address,
374 const MCDisassembler *Decoder) {
375 if (Imm == 0)
377 return decodeSImmOperand<N>(Inst, Imm, Address, Decoder);
378}
379
380template <unsigned N>
382 int64_t Address,
383 const MCDisassembler *Decoder) {
384 assert(isUInt<N>(Imm) && "Invalid immediate");
385 // Sign-extend the number in the bottom N bits of Imm after accounting for
386 // the fact that the N bit immediate is stored in N-1 bits (the LSB is
387 // always zero)
388 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm << 1)));
390}
391
393 int64_t Address,
394 const MCDisassembler *Decoder) {
395 assert(isUInt<6>(Imm) && "Invalid immediate");
396 if (Imm > 31) {
397 Imm = (SignExtend64<6>(Imm) & 0xfffff);
398 }
401}
402
403static DecodeStatus decodeFRMArg(MCInst &Inst, uint32_t Imm, int64_t Address,
404 const MCDisassembler *Decoder) {
405 assert(isUInt<3>(Imm) && "Invalid immediate");
408
411}
412
413static DecodeStatus decodeRTZArg(MCInst &Inst, uint32_t Imm, int64_t Address,
414 const MCDisassembler *Decoder) {
415 assert(isUInt<3>(Imm) && "Invalid immediate");
416 if (Imm != RISCVFPRndMode::RTZ)
418
421}
422
424 uint64_t Address,
425 const MCDisassembler *Decoder);
426
428 uint64_t Address,
429 const MCDisassembler *Decoder);
430
432 uint64_t Address,
433 const MCDisassembler *Decoder);
434
436 uint64_t Address,
437 const MCDisassembler *Decoder);
438
440 uint64_t Address,
441 const MCDisassembler *Decoder);
442
444 uint64_t Address,
445 const MCDisassembler *Decoder);
446
448 uint64_t Address, const void *Decoder);
449
451 const MCDisassembler *Decoder);
452
454 uint64_t Address, const void *Decoder);
455
457 uint64_t Address,
458 const MCDisassembler *Decoder);
459
460#include "RISCVGenDisassemblerTables.inc"
461
463 uint64_t Address,
464 const MCDisassembler *Decoder) {
465 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
466 [[maybe_unused]] DecodeStatus Result =
467 DecodeGPRNoX0RegisterClass(Inst, Rd, Address, Decoder);
468 assert(Result == MCDisassembler::Success && "Invalid register");
469 Inst.addOperand(Inst.getOperand(0));
472}
473
475 uint64_t Address,
476 const MCDisassembler *Decoder) {
477 uint32_t Rs1 = fieldFromInstruction(Insn, 7, 5);
478 [[maybe_unused]] DecodeStatus Result =
479 DecodeGPRX1X5RegisterClass(Inst, Rs1, Address, Decoder);
480 assert(Result == MCDisassembler::Success && "Invalid register");
482}
483
485 uint64_t Address,
486 const MCDisassembler *Decoder) {
487 Inst.addOperand(MCOperand::createReg(RISCV::X0));
488 uint32_t SImm6 =
489 fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5);
490 [[maybe_unused]] DecodeStatus Result =
491 decodeSImmOperand<6>(Inst, SImm6, Address, Decoder);
492 assert(Result == MCDisassembler::Success && "Invalid immediate");
494}
495
497 uint64_t Address,
498 const MCDisassembler *Decoder) {
499 Inst.addOperand(MCOperand::createReg(RISCV::X0));
500 Inst.addOperand(Inst.getOperand(0));
501 uint32_t UImm6 =
502 fieldFromInstruction(Insn, 12, 1) << 5 | fieldFromInstruction(Insn, 2, 5);
503 [[maybe_unused]] DecodeStatus Result =
504 decodeUImmOperand<6>(Inst, UImm6, Address, Decoder);
505 assert(Result == MCDisassembler::Success && "Invalid immediate");
507}
508
510 uint64_t Address,
511 const MCDisassembler *Decoder) {
512 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
513 uint32_t Rs2 = fieldFromInstruction(Insn, 2, 5);
514 DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
515 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
517}
518
520 uint64_t Address,
521 const MCDisassembler *Decoder) {
522 uint32_t Rd = fieldFromInstruction(Insn, 7, 5);
523 uint32_t Rs2 = fieldFromInstruction(Insn, 2, 5);
524 DecodeGPRRegisterClass(Inst, Rd, Address, Decoder);
525 Inst.addOperand(Inst.getOperand(0));
526 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
528}
529
531 uint64_t Address,
532 const MCDisassembler *Decoder) {
533 uint32_t Rd1 = fieldFromInstruction(Insn, 7, 5);
534 uint32_t Rs1 = fieldFromInstruction(Insn, 15, 5);
535 uint32_t Rd2 = fieldFromInstruction(Insn, 20, 5);
536 uint32_t UImm2 = fieldFromInstruction(Insn, 25, 2);
537 DecodeGPRRegisterClass(Inst, Rd1, Address, Decoder);
538 DecodeGPRRegisterClass(Inst, Rd2, Address, Decoder);
539 DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
540 [[maybe_unused]] DecodeStatus Result =
541 decodeUImmOperand<2>(Inst, UImm2, Address, Decoder);
542 assert(Result == MCDisassembler::Success && "Invalid immediate");
543
544 // Disassemble the final operand which is implicit.
545 unsigned Opcode = Inst.getOpcode();
546 bool IsWordOp = (Opcode == RISCV::TH_LWD || Opcode == RISCV::TH_LWUD ||
547 Opcode == RISCV::TH_SWD);
548 if (IsWordOp)
550 else
552
554}
555
557 uint64_t Address, const void *Decoder) {
558 if (Imm <= 3)
562}
563
565 const MCDisassembler *Decoder) {
566 uint32_t Rs1 = fieldFromInstruction(Insn, 0, 5);
567 uint32_t Rs2 = fieldFromInstruction(Insn, 5, 5);
568 DecodeGPRRegisterClass(Inst, Rs1, Address, Decoder);
569 DecodeGPRRegisterClass(Inst, Rs2, Address, Decoder);
571}
572
574 uint64_t Address, const void *Decoder) {
577}
578
579// Add implied SP operand for C.*SP compressed instructions. The SP operand
580// isn't explicitly encoded in the instruction.
581void RISCVDisassembler::addSPOperands(MCInst &MI) const {
582 const MCInstrDesc &MCID = MCII->get(MI.getOpcode());
583 for (unsigned i = 0; i < MCID.getNumOperands(); i++)
584 if (MCID.operands()[i].RegClass == RISCV::SPRegClassID)
585 MI.insert(MI.begin() + i, MCOperand::createReg(RISCV::X2));
586}
587
588#define TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, \
589 DESC, ADDITIONAL_OPERATION) \
590 do { \
591 if (FEATURE_CHECKS) { \
592 LLVM_DEBUG(dbgs() << "Trying " DESC ":\n"); \
593 DecodeStatus Result = \
594 decodeInstruction(DECODER_TABLE, MI, Insn, Address, this, STI); \
595 if (Result != MCDisassembler::Fail) { \
596 ADDITIONAL_OPERATION; \
597 return Result; \
598 } \
599 } \
600 } while (false)
601#define TRY_TO_DECODE_AND_ADD_SP(FEATURE_CHECKS, DECODER_TABLE, DESC) \
602 TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
603 addSPOperands(MI))
604#define TRY_TO_DECODE(FEATURE_CHECKS, DECODER_TABLE, DESC) \
605 TRY_TO_DECODE_WITH_ADDITIONAL_OPERATION(FEATURE_CHECKS, DECODER_TABLE, DESC, \
606 (void)nullptr)
607#define TRY_TO_DECODE_FEATURE(FEATURE, DECODER_TABLE, DESC) \
608 TRY_TO_DECODE(STI.hasFeature(FEATURE), DECODER_TABLE, DESC)
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;
617 }
618 Size = 4;
619
621
622 TRY_TO_DECODE(STI.hasFeature(RISCV::FeatureStdExtZdinx) &&
623 !STI.hasFeature(RISCV::Feature64Bit),
624 DecoderTableRV32Zdinx32,
625 "RV32Zdinx table (Double in Integer and rv32)");
626 TRY_TO_DECODE(STI.hasFeature(RISCV::FeatureStdExtZacas) &&
627 !STI.hasFeature(RISCV::Feature64Bit),
628 DecoderTableRV32Zacas32,
629 "RV32Zacas table (Compare-And-Swap and rv32)");
630 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZfinx, DecoderTableRVZfinx32,
631 "RVZfinx table (Float in Integer)");
632 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXVentanaCondOps,
633 DecoderTableXVentana32, "Ventana custom opcode table");
634 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBa, DecoderTableXTHeadBa32,
635 "XTHeadBa custom opcode table");
636 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBb, DecoderTableXTHeadBb32,
637 "XTHeadBb custom opcode table");
638 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadBs, DecoderTableXTHeadBs32,
639 "XTHeadBs custom opcode table");
640 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCondMov,
641 DecoderTableXTHeadCondMov32,
642 "XTHeadCondMov custom opcode table");
643 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadCmo, DecoderTableXTHeadCmo32,
644 "XTHeadCmo custom opcode table");
645 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadFMemIdx,
646 DecoderTableXTHeadFMemIdx32,
647 "XTHeadFMemIdx custom opcode table");
648 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMac, DecoderTableXTHeadMac32,
649 "XTHeadMac custom opcode table");
650 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemIdx,
651 DecoderTableXTHeadMemIdx32,
652 "XTHeadMemIdx custom opcode table");
653 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadMemPair,
654 DecoderTableXTHeadMemPair32,
655 "XTHeadMemPair custom opcode table");
656 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadSync,
657 DecoderTableXTHeadSync32,
658 "XTHeadSync custom opcode table");
659 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXTHeadVdot,
660 DecoderTableXTHeadVdot32,
661 "XTHeadVdot custom opcode table");
662 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSfvcp, DecoderTableXSfvcp32,
663 "SiFive VCIX custom opcode table");
665 RISCV::FeatureVendorXSfvqmaccdod, DecoderTableXSfvqmaccdod32,
666 "SiFive Matrix Multiplication (2x8 and 8x2) Instruction opcode table");
668 RISCV::FeatureVendorXSfvqmaccqoq, DecoderTableXSfvqmaccqoq32,
669 "SiFive Matrix Multiplication (4x8 and 8x4) Instruction opcode table");
671 RISCV::FeatureVendorXSfvfwmaccqqq, DecoderTableXSfvfwmaccqqq32,
672 "SiFive Matrix Multiplication Instruction opcode table");
674 RISCV::FeatureVendorXSfvfnrclipxfqf, DecoderTableXSfvfnrclipxfqf32,
675 "SiFive FP32-to-int8 Ranged Clip Instructions opcode table");
676 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSiFivecdiscarddlone,
677 DecoderTableXSiFivecdiscarddlone32,
678 "SiFive sf.cdiscard.d.l1 custom opcode table");
679 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSiFivecflushdlone,
680 DecoderTableXSiFivecflushdlone32,
681 "SiFive sf.cflush.d.l1 custom opcode table");
682 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXSfcease, DecoderTableXSfcease32,
683 "SiFive sf.cease custom opcode table");
684 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVbitmanip,
685 DecoderTableXCVbitmanip32,
686 "CORE-V Bit Manipulation custom opcode table");
687 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVelw, DecoderTableXCVelw32,
688 "CORE-V Event load custom opcode table");
689 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVmac, DecoderTableXCVmac32,
690 "CORE-V MAC custom opcode table");
691 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVmem, DecoderTableXCVmem32,
692 "CORE-V MEM custom opcode table");
693 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCValu, DecoderTableXCValu32,
694 "CORE-V ALU custom opcode table");
695 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVsimd, DecoderTableXCVsimd32,
696 "CORE-V SIMD extensions custom opcode table");
697 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXCVbi, DecoderTableXCVbi32,
698 "CORE-V Immediate Branching custom opcode table");
699 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcicsr, DecoderTableXqcicsr32,
700 "Qualcomm uC CSR custom opcode table");
701 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcisls, DecoderTableXqcisls32,
702 "Qualcomm uC Scaled Load Store custom opcode table");
703 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcia, DecoderTableXqcia32,
704 "Qualcomm uC Arithmetic custom opcode table");
705 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcics, DecoderTableXqcics32,
706 "Qualcomm uC Conditional Select custom opcode table");
707 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcilsm, DecoderTableXqcilsm32,
708 "Qualcomm uC Load Store Multiple custom opcode table");
710 RISCV::FeatureVendorXqciac, DecoderTableXqciac32,
711 "Qualcomm uC Load-Store Address Calculation custom opcode table");
713 RISCV::FeatureVendorXqcicli, DecoderTableXqcicli32,
714 "Qualcomm uC Conditional Load Immediate custom opcode table");
715 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqcicm, DecoderTableXqcicm32,
716 "Qualcomm uC Conditional Move custom opcode table");
717 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqciint, DecoderTableXqciint32,
718 "Qualcomm uC Interrupts custom opcode table");
719 TRY_TO_DECODE(true, DecoderTable32, "RISCV32 table");
720
722}
723
724DecodeStatus RISCVDisassembler::getInstruction16(MCInst &MI, uint64_t &Size,
725 ArrayRef<uint8_t> Bytes,
726 uint64_t Address,
727 raw_ostream &CS) const {
728 if (Bytes.size() < 2) {
729 Size = 0;
731 }
732 Size = 2;
733
735 TRY_TO_DECODE_AND_ADD_SP(!STI.hasFeature(RISCV::Feature64Bit),
736 DecoderTableRISCV32Only_16,
737 "RISCV32Only_16 table (16-bit Instruction)");
738 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZicfiss, DecoderTableZicfiss16,
739 "RVZicfiss table (Shadow Stack)");
740 TRY_TO_DECODE_FEATURE(RISCV::FeatureStdExtZcmt, DecoderTableRVZcmt16,
741 "Zcmt table (16-bit Table Jump Instructions)");
743 RISCV::FeatureStdExtZcmp, DecoderTableRVZcmp16,
744 "Zcmp table (16-bit Push/Pop & Double Move Instructions)");
746 RISCV::FeatureVendorXqciac, DecoderTableXqciac16,
747 "Qualcomm uC Load-Store Address Calculation custom 16bit opcode table");
749 RISCV::FeatureVendorXqcicm, DecoderTableXqcicm16,
750 "Qualcomm uC Conditional Move custom 16bit opcode table");
751 TRY_TO_DECODE_FEATURE(RISCV::FeatureVendorXqciint, DecoderTableXqciint16,
752 "Qualcomm uC Interrupts custom 16bit opcode table");
753 TRY_TO_DECODE_AND_ADD_SP(STI.hasFeature(RISCV::FeatureVendorXwchc),
754 DecoderTableXwchc16,
755 "WCH QingKe XW custom opcode table");
756 TRY_TO_DECODE_AND_ADD_SP(true, DecoderTable16,
757 "RISCV_C table (16-bit Instruction)");
758
760}
761
762DecodeStatus RISCVDisassembler::getInstruction48(MCInst &MI, uint64_t &Size,
763 ArrayRef<uint8_t> Bytes,
764 uint64_t Address,
765 raw_ostream &CS) const {
766 if (Bytes.size() < 6) {
767 Size = 0;
769 }
770 Size = 6;
771
772 uint64_t Insn = 0;
773 for (size_t i = Size; i-- != 0;) {
774 Insn += (static_cast<uint64_t>(Bytes[i]) << 8 * i);
775 }
777 RISCV::FeatureVendorXqcilo, DecoderTableXqcilo48,
778 "Qualcomm uC Large Offset Load Store custom 48bit opcode table");
779
781}
782
783DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
784 ArrayRef<uint8_t> Bytes,
785 uint64_t Address,
786 raw_ostream &CS) const {
787 // It's a 16 bit instruction if bit 0 and 1 are not 0b11.
788 if ((Bytes[0] & 0b11) != 0b11)
789 return getInstruction16(MI, Size, Bytes, Address, CS);
790
791 // It's a 32 bit instruction if bit 1:0 are 0b11(checked above) and bits 4:2
792 // are not 0b111.
793 if ((Bytes[0] & 0b1'1100) != 0b1'1100)
794 return getInstruction32(MI, Size, Bytes, Address, CS);
795
796 // 48-bit instructions are encoded as 0bxx011111.
797 if ((Bytes[0] & 0b11'1111) == 0b01'1111) {
798 return getInstruction48(MI, Size, Bytes, Address, CS);
799 }
800
801 // 64-bit instructions are encoded as 0x0111111.
802 if ((Bytes[0] & 0b111'1111) == 0b011'1111) {
803 Size = Bytes.size() >= 8 ? 8 : 0;
805 }
806
807 // Remaining cases need to check a second byte.
808 if (Bytes.size() < 2) {
809 Size = 0;
811 }
812
813 // 80-bit through 176-bit instructions are encoded as 0bxnnnxxxx_x1111111.
814 // Where the number of bits is (80 + (nnn * 16)) for nnn != 0b111.
815 unsigned nnn = (Bytes[1] >> 4) & 0b111;
816 if (nnn != 0b111) {
817 Size = 10 + (nnn * 2);
818 if (Bytes.size() < Size)
819 Size = 0;
821 }
822
823 // Remaining encodings are reserved for > 176-bit instructions.
824 Size = 0;
826}
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 DecodeVMV0RegisterClass(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)
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.