1 //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the Mips Disassembler.
12 //===----------------------------------------------------------------------===//
15 #include "MipsRegisterInfo.h"
16 #include "MipsSubtarget.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler.h"
19 #include "llvm/MC/MCFixedLenDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/MemoryObject.h"
24 #include "llvm/Support/TargetRegistry.h"
28 #define DEBUG_TYPE "mips-disassembler"
30 typedef MCDisassembler::DecodeStatus DecodeStatus;
34 /// A disasembler class for Mips.
35 class MipsDisassemblerBase : public MCDisassembler {
37 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
39 : MCDisassembler(STI, Ctx),
40 IsN64(STI.getFeatureBits() & Mips::FeatureN64),
41 IsBigEndian(IsBigEndian) {}
43 virtual ~MipsDisassemblerBase() {}
45 bool isN64() const { return IsN64; }
53 /// A disasembler class for Mips32.
54 class MipsDisassembler : public MipsDisassemblerBase {
57 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
58 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
59 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
62 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
63 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
64 bool hasMips32r6() const {
65 return STI.getFeatureBits() & Mips::FeatureMips32r6;
68 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
70 bool hasCOP3() const {
71 // Only present in MIPS-I and MIPS-II
72 return !hasMips32() && !hasMips3();
75 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
76 const MemoryObject &Region, uint64_t Address,
78 raw_ostream &CStream) const override;
81 /// A disasembler class for Mips64.
82 class Mips64Disassembler : public MipsDisassemblerBase {
84 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
86 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
88 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
89 const MemoryObject &Region, uint64_t Address,
91 raw_ostream &CStream) const override;
94 } // end anonymous namespace
96 // Forward declare these because the autogenerated code will reference them.
97 // Definitions are further down.
98 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
101 const void *Decoder);
103 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
106 const void *Decoder);
108 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
111 const void *Decoder);
113 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
116 const void *Decoder);
118 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
121 const void *Decoder);
123 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
126 const void *Decoder);
128 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
131 const void *Decoder);
133 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
136 const void *Decoder);
138 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
141 const void *Decoder);
143 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
146 const void *Decoder);
148 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
150 const void *Decoder);
152 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
155 const void *Decoder);
157 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
160 const void *Decoder);
162 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
165 const void *Decoder);
167 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
170 const void *Decoder);
172 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
175 const void *Decoder);
177 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
180 const void *Decoder);
182 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
185 const void *Decoder);
187 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
190 const void *Decoder);
192 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
195 const void *Decoder);
197 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
200 const void *Decoder);
202 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
205 const void *Decoder);
207 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
210 const void *Decoder);
212 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
215 const void *Decoder);
217 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
220 const void *Decoder);
222 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
225 const void *Decoder);
227 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
228 // shifted left by 1 bit.
229 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
232 const void *Decoder);
234 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
235 // shifted left by 1 bit.
236 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
239 const void *Decoder);
241 static DecodeStatus DecodeMem(MCInst &Inst,
244 const void *Decoder);
246 static DecodeStatus DecodeCacheOp(MCInst &Inst,
249 const void *Decoder);
251 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
252 uint64_t Address, const void *Decoder);
254 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
257 const void *Decoder);
259 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
262 const void *Decoder);
264 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
266 const void *Decoder);
268 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
270 const void *Decoder);
272 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
274 const void *Decoder);
276 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
279 const void *Decoder);
281 static DecodeStatus DecodeSimm16(MCInst &Inst,
284 const void *Decoder);
286 // Decode the immediate field of an LSA instruction which
288 static DecodeStatus DecodeLSAImm(MCInst &Inst,
291 const void *Decoder);
293 static DecodeStatus DecodeInsSize(MCInst &Inst,
296 const void *Decoder);
298 static DecodeStatus DecodeExtSize(MCInst &Inst,
301 const void *Decoder);
303 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
304 uint64_t Address, const void *Decoder);
306 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
307 uint64_t Address, const void *Decoder);
309 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
311 template <typename InsnType>
312 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
313 const void *Decoder);
315 template <typename InsnType>
317 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
318 const void *Decoder);
320 template <typename InsnType>
322 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
323 const void *Decoder);
325 template <typename InsnType>
327 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
328 const void *Decoder);
330 template <typename InsnType>
332 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
333 const void *Decoder);
335 template <typename InsnType>
337 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
338 const void *Decoder);
340 template <typename InsnType>
342 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
343 const void *Decoder);
346 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
350 static MCDisassembler *createMipsDisassembler(
352 const MCSubtargetInfo &STI,
354 return new MipsDisassembler(STI, Ctx, true);
357 static MCDisassembler *createMipselDisassembler(
359 const MCSubtargetInfo &STI,
361 return new MipsDisassembler(STI, Ctx, false);
364 static MCDisassembler *createMips64Disassembler(
366 const MCSubtargetInfo &STI,
368 return new Mips64Disassembler(STI, Ctx, true);
371 static MCDisassembler *createMips64elDisassembler(
373 const MCSubtargetInfo &STI,
375 return new Mips64Disassembler(STI, Ctx, false);
378 extern "C" void LLVMInitializeMipsDisassembler() {
379 // Register the disassembler.
380 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
381 createMipsDisassembler);
382 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
383 createMipselDisassembler);
384 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
385 createMips64Disassembler);
386 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
387 createMips64elDisassembler);
390 #include "MipsGenDisassemblerTables.inc"
392 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
393 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
394 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
395 return *(RegInfo->getRegClass(RC).begin() + RegNo);
398 template <typename InsnType>
399 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
400 const void *Decoder) {
401 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
402 // The size of the n field depends on the element size
403 // The register class also depends on this.
404 InsnType tmp = fieldFromInstruction(insn, 17, 5);
406 DecodeFN RegDecoder = nullptr;
407 if ((tmp & 0x18) == 0x00) { // INSVE_B
409 RegDecoder = DecodeMSA128BRegisterClass;
410 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
412 RegDecoder = DecodeMSA128HRegisterClass;
413 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
415 RegDecoder = DecodeMSA128WRegisterClass;
416 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
418 RegDecoder = DecodeMSA128DRegisterClass;
420 llvm_unreachable("Invalid encoding");
422 assert(NSize != 0 && RegDecoder != nullptr);
425 tmp = fieldFromInstruction(insn, 6, 5);
426 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
427 return MCDisassembler::Fail;
429 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
430 return MCDisassembler::Fail;
432 tmp = fieldFromInstruction(insn, 16, NSize);
433 MI.addOperand(MCOperand::CreateImm(tmp));
435 tmp = fieldFromInstruction(insn, 11, 5);
436 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
437 return MCDisassembler::Fail;
439 MI.addOperand(MCOperand::CreateImm(0));
441 return MCDisassembler::Success;
444 template <typename InsnType>
445 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
447 const void *Decoder) {
448 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
449 // (otherwise we would have matched the ADDI instruction from the earlier
453 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
455 // BEQZALC if rs == 0 && rt != 0
456 // BEQC if rs < rt && rs != 0
458 InsnType Rs = fieldFromInstruction(insn, 21, 5);
459 InsnType Rt = fieldFromInstruction(insn, 16, 5);
460 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
464 MI.setOpcode(Mips::BOVC);
466 } else if (Rs != 0 && Rs < Rt) {
467 MI.setOpcode(Mips::BEQC);
470 MI.setOpcode(Mips::BEQZALC);
473 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
476 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
478 MI.addOperand(MCOperand::CreateImm(Imm));
480 return MCDisassembler::Success;
483 template <typename InsnType>
484 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
486 const void *Decoder) {
487 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
488 // (otherwise we would have matched the ADDI instruction from the earlier
492 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
494 // BNEZALC if rs == 0 && rt != 0
495 // BNEC if rs < rt && rs != 0
497 InsnType Rs = fieldFromInstruction(insn, 21, 5);
498 InsnType Rt = fieldFromInstruction(insn, 16, 5);
499 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
503 MI.setOpcode(Mips::BNVC);
505 } else if (Rs != 0 && Rs < Rt) {
506 MI.setOpcode(Mips::BNEC);
509 MI.setOpcode(Mips::BNEZALC);
512 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
515 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
517 MI.addOperand(MCOperand::CreateImm(Imm));
519 return MCDisassembler::Success;
522 template <typename InsnType>
523 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
525 const void *Decoder) {
526 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
527 // (otherwise we would have matched the BLEZL instruction from the earlier
531 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
532 // Invalid if rs == 0
533 // BLEZC if rs == 0 && rt != 0
534 // BGEZC if rs == rt && rt != 0
535 // BGEC if rs != rt && rs != 0 && rt != 0
537 InsnType Rs = fieldFromInstruction(insn, 21, 5);
538 InsnType Rt = fieldFromInstruction(insn, 16, 5);
539 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
543 return MCDisassembler::Fail;
545 MI.setOpcode(Mips::BLEZC);
547 MI.setOpcode(Mips::BGEZC);
550 MI.setOpcode(Mips::BGEC);
554 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
557 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
560 MI.addOperand(MCOperand::CreateImm(Imm));
562 return MCDisassembler::Success;
565 template <typename InsnType>
566 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
568 const void *Decoder) {
569 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
570 // (otherwise we would have matched the BGTZL instruction from the earlier
574 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
575 // Invalid if rs == 0
576 // BGTZC if rs == 0 && rt != 0
577 // BLTZC if rs == rt && rt != 0
578 // BLTC if rs != rt && rs != 0 && rt != 0
582 InsnType Rs = fieldFromInstruction(insn, 21, 5);
583 InsnType Rt = fieldFromInstruction(insn, 16, 5);
584 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
587 return MCDisassembler::Fail;
589 MI.setOpcode(Mips::BGTZC);
591 MI.setOpcode(Mips::BLTZC);
593 MI.setOpcode(Mips::BLTC);
598 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
601 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
604 MI.addOperand(MCOperand::CreateImm(Imm));
606 return MCDisassembler::Success;
609 template <typename InsnType>
610 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
612 const void *Decoder) {
613 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
614 // (otherwise we would have matched the BGTZ instruction from the earlier
618 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
620 // BGTZALC if rs == 0 && rt != 0
621 // BLTZALC if rs != 0 && rs == rt
622 // BLTUC if rs != 0 && rs != rt
624 InsnType Rs = fieldFromInstruction(insn, 21, 5);
625 InsnType Rt = fieldFromInstruction(insn, 16, 5);
626 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
631 MI.setOpcode(Mips::BGTZ);
633 } else if (Rs == 0) {
634 MI.setOpcode(Mips::BGTZALC);
636 } else if (Rs == Rt) {
637 MI.setOpcode(Mips::BLTZALC);
640 MI.setOpcode(Mips::BLTUC);
646 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
650 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
653 MI.addOperand(MCOperand::CreateImm(Imm));
655 return MCDisassembler::Success;
658 template <typename InsnType>
659 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
661 const void *Decoder) {
662 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
663 // (otherwise we would have matched the BLEZL instruction from the earlier
667 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
668 // Invalid if rs == 0
669 // BLEZALC if rs == 0 && rt != 0
670 // BGEZALC if rs == rt && rt != 0
671 // BGEUC if rs != rt && rs != 0 && rt != 0
673 InsnType Rs = fieldFromInstruction(insn, 21, 5);
674 InsnType Rt = fieldFromInstruction(insn, 16, 5);
675 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
679 return MCDisassembler::Fail;
681 MI.setOpcode(Mips::BLEZALC);
683 MI.setOpcode(Mips::BGEZALC);
686 MI.setOpcode(Mips::BGEUC);
690 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
692 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
695 MI.addOperand(MCOperand::CreateImm(Imm));
697 return MCDisassembler::Success;
700 /// Read four bytes from the MemoryObject and return 32 bit word sorted
701 /// according to the given endianess
702 static DecodeStatus readInstruction32(const MemoryObject &Region,
703 uint64_t Address, uint64_t &Size,
704 uint32_t &Insn, bool IsBigEndian,
708 // We want to read exactly 4 Bytes of data.
709 if (Region.readBytes(Address, 4, Bytes) == -1) {
711 return MCDisassembler::Fail;
715 // Encoded as a big-endian 32-bit word in the stream.
717 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
719 // Encoded as a small-endian 32-bit word in the stream.
720 // Little-endian byte ordering:
721 // mips32r2: 4 | 3 | 2 | 1
722 // microMIPS: 2 | 1 | 4 | 3
724 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
727 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
732 return MCDisassembler::Success;
735 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
736 const MemoryObject &Region,
738 raw_ostream &VStream,
739 raw_ostream &CStream) const {
742 DecodeStatus Result =
743 readInstruction32(Region, Address, Size, Insn, IsBigEndian, IsMicroMips);
744 if (Result == MCDisassembler::Fail)
745 return MCDisassembler::Fail;
748 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
749 // Calling the auto-generated decoder function.
750 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
752 if (Result != MCDisassembler::Fail) {
756 return MCDisassembler::Fail;
760 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
762 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
763 if (Result != MCDisassembler::Fail) {
769 if (hasMips32r6() && isGP64()) {
770 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
771 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
773 if (Result != MCDisassembler::Fail) {
780 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
781 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
783 if (Result != MCDisassembler::Fail) {
789 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
790 // Calling the auto-generated decoder function.
792 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
793 if (Result != MCDisassembler::Fail) {
798 return MCDisassembler::Fail;
801 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
802 const MemoryObject &Region,
804 raw_ostream &VStream,
805 raw_ostream &CStream) const {
808 DecodeStatus Result =
809 readInstruction32(Region, Address, Size, Insn, IsBigEndian, false);
810 if (Result == MCDisassembler::Fail)
811 return MCDisassembler::Fail;
813 // Calling the auto-generated decoder function.
815 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
816 if (Result != MCDisassembler::Fail) {
820 // If we fail to decode in Mips64 decoder space we can try in Mips32
822 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
823 if (Result != MCDisassembler::Fail) {
828 return MCDisassembler::Fail;
831 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
834 const void *Decoder) {
836 return MCDisassembler::Fail;
840 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
843 const void *Decoder) {
846 return MCDisassembler::Fail;
848 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
849 Inst.addOperand(MCOperand::CreateReg(Reg));
850 return MCDisassembler::Success;
853 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
856 const void *Decoder) {
857 return MCDisassembler::Fail;
860 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
863 const void *Decoder) {
865 return MCDisassembler::Fail;
866 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
867 Inst.addOperand(MCOperand::CreateReg(Reg));
868 return MCDisassembler::Success;
871 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
874 const void *Decoder) {
875 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
876 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
878 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
881 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
884 const void *Decoder) {
885 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
888 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
891 const void *Decoder) {
893 return MCDisassembler::Fail;
895 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
896 Inst.addOperand(MCOperand::CreateReg(Reg));
897 return MCDisassembler::Success;
900 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
903 const void *Decoder) {
905 return MCDisassembler::Fail;
907 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
908 Inst.addOperand(MCOperand::CreateReg(Reg));
909 return MCDisassembler::Success;
912 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
915 const void *Decoder) {
917 return MCDisassembler::Fail;
918 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
919 Inst.addOperand(MCOperand::CreateReg(Reg));
920 return MCDisassembler::Success;
923 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
926 const void *Decoder) {
928 return MCDisassembler::Fail;
929 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
930 Inst.addOperand(MCOperand::CreateReg(Reg));
931 return MCDisassembler::Success;
934 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
936 const void *Decoder) {
938 return MCDisassembler::Fail;
940 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
941 Inst.addOperand(MCOperand::CreateReg(Reg));
942 return MCDisassembler::Success;
945 static DecodeStatus DecodeMem(MCInst &Inst,
948 const void *Decoder) {
949 int Offset = SignExtend32<16>(Insn & 0xffff);
950 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
951 unsigned Base = fieldFromInstruction(Insn, 21, 5);
953 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
954 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
956 if(Inst.getOpcode() == Mips::SC){
957 Inst.addOperand(MCOperand::CreateReg(Reg));
960 Inst.addOperand(MCOperand::CreateReg(Reg));
961 Inst.addOperand(MCOperand::CreateReg(Base));
962 Inst.addOperand(MCOperand::CreateImm(Offset));
964 return MCDisassembler::Success;
967 static DecodeStatus DecodeCacheOp(MCInst &Inst,
970 const void *Decoder) {
971 int Offset = SignExtend32<16>(Insn & 0xffff);
972 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
973 unsigned Base = fieldFromInstruction(Insn, 21, 5);
975 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
977 Inst.addOperand(MCOperand::CreateReg(Base));
978 Inst.addOperand(MCOperand::CreateImm(Offset));
979 Inst.addOperand(MCOperand::CreateImm(Hint));
981 return MCDisassembler::Success;
984 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
985 uint64_t Address, const void *Decoder) {
986 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
987 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
988 unsigned Base = fieldFromInstruction(Insn, 11, 5);
990 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
991 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
993 Inst.addOperand(MCOperand::CreateReg(Reg));
994 Inst.addOperand(MCOperand::CreateReg(Base));
996 // The immediate field of an LD/ST instruction is scaled which means it must
997 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1003 switch(Inst.getOpcode())
1006 assert (0 && "Unexpected instruction");
1007 return MCDisassembler::Fail;
1011 Inst.addOperand(MCOperand::CreateImm(Offset));
1015 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1019 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1023 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1027 return MCDisassembler::Success;
1030 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1033 const void *Decoder) {
1034 int Offset = SignExtend32<12>(Insn & 0x0fff);
1035 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1036 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1038 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1039 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1041 if (Inst.getOpcode() == Mips::SC_MM)
1042 Inst.addOperand(MCOperand::CreateReg(Reg));
1044 Inst.addOperand(MCOperand::CreateReg(Reg));
1045 Inst.addOperand(MCOperand::CreateReg(Base));
1046 Inst.addOperand(MCOperand::CreateImm(Offset));
1048 return MCDisassembler::Success;
1051 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1054 const void *Decoder) {
1055 int Offset = SignExtend32<16>(Insn & 0xffff);
1056 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1057 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1059 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1060 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1062 Inst.addOperand(MCOperand::CreateReg(Reg));
1063 Inst.addOperand(MCOperand::CreateReg(Base));
1064 Inst.addOperand(MCOperand::CreateImm(Offset));
1066 return MCDisassembler::Success;
1069 static DecodeStatus DecodeFMem(MCInst &Inst,
1072 const void *Decoder) {
1073 int Offset = SignExtend32<16>(Insn & 0xffff);
1074 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1075 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1077 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1078 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1080 Inst.addOperand(MCOperand::CreateReg(Reg));
1081 Inst.addOperand(MCOperand::CreateReg(Base));
1082 Inst.addOperand(MCOperand::CreateImm(Offset));
1084 return MCDisassembler::Success;
1087 static DecodeStatus DecodeFMem2(MCInst &Inst,
1090 const void *Decoder) {
1091 int Offset = SignExtend32<16>(Insn & 0xffff);
1092 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1093 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1095 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1096 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1098 Inst.addOperand(MCOperand::CreateReg(Reg));
1099 Inst.addOperand(MCOperand::CreateReg(Base));
1100 Inst.addOperand(MCOperand::CreateImm(Offset));
1102 return MCDisassembler::Success;
1105 static DecodeStatus DecodeFMem3(MCInst &Inst,
1108 const void *Decoder) {
1109 int Offset = SignExtend32<16>(Insn & 0xffff);
1110 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1111 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1113 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1114 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1116 Inst.addOperand(MCOperand::CreateReg(Reg));
1117 Inst.addOperand(MCOperand::CreateReg(Base));
1118 Inst.addOperand(MCOperand::CreateImm(Offset));
1120 return MCDisassembler::Success;
1123 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1126 const void *Decoder) {
1127 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1128 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1129 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1131 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1132 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1134 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1135 Inst.addOperand(MCOperand::CreateReg(Rt));
1138 Inst.addOperand(MCOperand::CreateReg(Rt));
1139 Inst.addOperand(MCOperand::CreateReg(Base));
1140 Inst.addOperand(MCOperand::CreateImm(Offset));
1142 return MCDisassembler::Success;
1145 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1148 const void *Decoder) {
1149 // Currently only hardware register 29 is supported.
1151 return MCDisassembler::Fail;
1152 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1153 return MCDisassembler::Success;
1156 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1159 const void *Decoder) {
1160 if (RegNo > 30 || RegNo %2)
1161 return MCDisassembler::Fail;
1164 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1165 Inst.addOperand(MCOperand::CreateReg(Reg));
1166 return MCDisassembler::Success;
1169 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1172 const void *Decoder) {
1174 return MCDisassembler::Fail;
1176 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1177 Inst.addOperand(MCOperand::CreateReg(Reg));
1178 return MCDisassembler::Success;
1181 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1184 const void *Decoder) {
1186 return MCDisassembler::Fail;
1188 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1189 Inst.addOperand(MCOperand::CreateReg(Reg));
1190 return MCDisassembler::Success;
1193 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1196 const void *Decoder) {
1198 return MCDisassembler::Fail;
1200 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1201 Inst.addOperand(MCOperand::CreateReg(Reg));
1202 return MCDisassembler::Success;
1205 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1208 const void *Decoder) {
1210 return MCDisassembler::Fail;
1212 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1213 Inst.addOperand(MCOperand::CreateReg(Reg));
1214 return MCDisassembler::Success;
1217 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1220 const void *Decoder) {
1222 return MCDisassembler::Fail;
1224 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1225 Inst.addOperand(MCOperand::CreateReg(Reg));
1226 return MCDisassembler::Success;
1229 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1232 const void *Decoder) {
1234 return MCDisassembler::Fail;
1236 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1237 Inst.addOperand(MCOperand::CreateReg(Reg));
1238 return MCDisassembler::Success;
1241 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1244 const void *Decoder) {
1246 return MCDisassembler::Fail;
1248 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1249 Inst.addOperand(MCOperand::CreateReg(Reg));
1250 return MCDisassembler::Success;
1253 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1256 const void *Decoder) {
1258 return MCDisassembler::Fail;
1260 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1261 Inst.addOperand(MCOperand::CreateReg(Reg));
1262 return MCDisassembler::Success;
1265 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1268 const void *Decoder) {
1270 return MCDisassembler::Fail;
1272 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1273 Inst.addOperand(MCOperand::CreateReg(Reg));
1274 return MCDisassembler::Success;
1277 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1280 const void *Decoder) {
1281 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1282 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1283 return MCDisassembler::Success;
1286 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1289 const void *Decoder) {
1291 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1292 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1293 return MCDisassembler::Success;
1296 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1299 const void *Decoder) {
1300 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1302 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1303 return MCDisassembler::Success;
1306 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1309 const void *Decoder) {
1310 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1312 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1313 return MCDisassembler::Success;
1316 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1319 const void *Decoder) {
1320 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1321 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1322 return MCDisassembler::Success;
1325 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1328 const void *Decoder) {
1329 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1330 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1331 return MCDisassembler::Success;
1334 static DecodeStatus DecodeSimm16(MCInst &Inst,
1337 const void *Decoder) {
1338 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1339 return MCDisassembler::Success;
1342 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1345 const void *Decoder) {
1346 // We add one to the immediate field as it was encoded as 'imm - 1'.
1347 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1348 return MCDisassembler::Success;
1351 static DecodeStatus DecodeInsSize(MCInst &Inst,
1354 const void *Decoder) {
1355 // First we need to grab the pos(lsb) from MCInst.
1356 int Pos = Inst.getOperand(2).getImm();
1357 int Size = (int) Insn - Pos + 1;
1358 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1359 return MCDisassembler::Success;
1362 static DecodeStatus DecodeExtSize(MCInst &Inst,
1365 const void *Decoder) {
1366 int Size = (int) Insn + 1;
1367 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1368 return MCDisassembler::Success;
1371 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1372 uint64_t Address, const void *Decoder) {
1373 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1374 return MCDisassembler::Success;
1377 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1378 uint64_t Address, const void *Decoder) {
1379 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1380 return MCDisassembler::Success;