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 /// MipsDisassemblerBase - a disasembler class for Mips.
35 class MipsDisassemblerBase : public MCDisassembler {
37 /// Constructor - Initializes the disassembler.
39 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
41 MCDisassembler(STI, Ctx),
42 IsN64(STI.getFeatureBits() & Mips::FeatureN64), isBigEndian(bigEndian) {}
44 virtual ~MipsDisassemblerBase() {}
46 bool isN64() const { return IsN64; }
54 /// MipsDisassembler - a disasembler class for Mips32.
55 class MipsDisassembler : public MipsDisassemblerBase {
58 /// Constructor - Initializes the disassembler.
60 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
61 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
62 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
65 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
66 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
67 bool hasMips32r6() const {
68 return STI.getFeatureBits() & Mips::FeatureMips32r6;
71 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
73 bool hasCOP3() const {
74 // Only present in MIPS-I and MIPS-II
75 return !hasMips32() && !hasMips3();
78 /// getInstruction - See MCDisassembler.
79 DecodeStatus getInstruction(MCInst &instr,
81 const MemoryObject ®ion,
84 raw_ostream &cStream) const override;
88 /// Mips64Disassembler - a disasembler class for Mips64.
89 class Mips64Disassembler : public MipsDisassemblerBase {
91 /// Constructor - Initializes the disassembler.
93 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
95 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
97 /// getInstruction - See MCDisassembler.
98 DecodeStatus getInstruction(MCInst &instr,
100 const MemoryObject ®ion,
102 raw_ostream &vStream,
103 raw_ostream &cStream) const override;
106 } // end anonymous namespace
108 // Forward declare these because the autogenerated code will reference them.
109 // Definitions are further down.
110 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
113 const void *Decoder);
115 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
118 const void *Decoder);
120 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
123 const void *Decoder);
125 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
128 const void *Decoder);
130 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
133 const void *Decoder);
135 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
138 const void *Decoder);
140 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
143 const void *Decoder);
145 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
148 const void *Decoder);
150 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
153 const void *Decoder);
155 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
158 const void *Decoder);
160 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
162 const void *Decoder);
164 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
167 const void *Decoder);
169 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
172 const void *Decoder);
174 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
177 const void *Decoder);
179 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
182 const void *Decoder);
184 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
187 const void *Decoder);
189 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
192 const void *Decoder);
194 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
197 const void *Decoder);
199 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
202 const void *Decoder);
204 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
207 const void *Decoder);
209 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
212 const void *Decoder);
214 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
217 const void *Decoder);
219 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
222 const void *Decoder);
224 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
227 const void *Decoder);
229 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
232 const void *Decoder);
234 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
237 const void *Decoder);
239 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
240 // shifted left by 1 bit.
241 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
244 const void *Decoder);
246 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
247 // shifted left by 1 bit.
248 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
251 const void *Decoder);
253 static DecodeStatus DecodeMem(MCInst &Inst,
256 const void *Decoder);
258 static DecodeStatus DecodeCacheOp(MCInst &Inst,
261 const void *Decoder);
263 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
264 uint64_t Address, const void *Decoder);
266 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
269 const void *Decoder);
271 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
274 const void *Decoder);
276 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
278 const void *Decoder);
280 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
282 const void *Decoder);
284 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
286 const void *Decoder);
288 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
291 const void *Decoder);
293 static DecodeStatus DecodeSimm16(MCInst &Inst,
296 const void *Decoder);
298 // Decode the immediate field of an LSA instruction which
300 static DecodeStatus DecodeLSAImm(MCInst &Inst,
303 const void *Decoder);
305 static DecodeStatus DecodeInsSize(MCInst &Inst,
308 const void *Decoder);
310 static DecodeStatus DecodeExtSize(MCInst &Inst,
313 const void *Decoder);
315 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
316 uint64_t Address, const void *Decoder);
318 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
319 uint64_t Address, const void *Decoder);
321 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
323 template <typename InsnType>
324 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
325 const void *Decoder);
327 template <typename InsnType>
329 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
330 const void *Decoder);
332 template <typename InsnType>
334 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
335 const void *Decoder);
337 template <typename InsnType>
339 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
340 const void *Decoder);
342 template <typename InsnType>
344 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
345 const void *Decoder);
347 template <typename InsnType>
349 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
350 const void *Decoder);
352 template <typename InsnType>
354 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
355 const void *Decoder);
358 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
362 static MCDisassembler *createMipsDisassembler(
364 const MCSubtargetInfo &STI,
366 return new MipsDisassembler(STI, Ctx, true);
369 static MCDisassembler *createMipselDisassembler(
371 const MCSubtargetInfo &STI,
373 return new MipsDisassembler(STI, Ctx, false);
376 static MCDisassembler *createMips64Disassembler(
378 const MCSubtargetInfo &STI,
380 return new Mips64Disassembler(STI, Ctx, true);
383 static MCDisassembler *createMips64elDisassembler(
385 const MCSubtargetInfo &STI,
387 return new Mips64Disassembler(STI, Ctx, false);
390 extern "C" void LLVMInitializeMipsDisassembler() {
391 // Register the disassembler.
392 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
393 createMipsDisassembler);
394 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
395 createMipselDisassembler);
396 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
397 createMips64Disassembler);
398 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
399 createMips64elDisassembler);
402 #include "MipsGenDisassemblerTables.inc"
404 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
405 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
406 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
407 return *(RegInfo->getRegClass(RC).begin() + RegNo);
410 template <typename InsnType>
411 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder) {
413 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
414 // The size of the n field depends on the element size
415 // The register class also depends on this.
416 InsnType tmp = fieldFromInstruction(insn, 17, 5);
418 DecodeFN RegDecoder = nullptr;
419 if ((tmp & 0x18) == 0x00) { // INSVE_B
421 RegDecoder = DecodeMSA128BRegisterClass;
422 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
424 RegDecoder = DecodeMSA128HRegisterClass;
425 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
427 RegDecoder = DecodeMSA128WRegisterClass;
428 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
430 RegDecoder = DecodeMSA128DRegisterClass;
432 llvm_unreachable("Invalid encoding");
434 assert(NSize != 0 && RegDecoder != nullptr);
437 tmp = fieldFromInstruction(insn, 6, 5);
438 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
439 return MCDisassembler::Fail;
441 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
442 return MCDisassembler::Fail;
444 tmp = fieldFromInstruction(insn, 16, NSize);
445 MI.addOperand(MCOperand::CreateImm(tmp));
447 tmp = fieldFromInstruction(insn, 11, 5);
448 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
449 return MCDisassembler::Fail;
451 MI.addOperand(MCOperand::CreateImm(0));
453 return MCDisassembler::Success;
456 template <typename InsnType>
457 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
459 const void *Decoder) {
460 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
461 // (otherwise we would have matched the ADDI instruction from the earlier
465 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
467 // BEQZALC if rs == 0 && rt != 0
468 // BEQC if rs < rt && rs != 0
470 InsnType Rs = fieldFromInstruction(insn, 21, 5);
471 InsnType Rt = fieldFromInstruction(insn, 16, 5);
472 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
476 MI.setOpcode(Mips::BOVC);
478 } else if (Rs != 0 && Rs < Rt) {
479 MI.setOpcode(Mips::BEQC);
482 MI.setOpcode(Mips::BEQZALC);
485 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
488 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
490 MI.addOperand(MCOperand::CreateImm(Imm));
492 return MCDisassembler::Success;
495 template <typename InsnType>
496 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
498 const void *Decoder) {
499 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
500 // (otherwise we would have matched the ADDI instruction from the earlier
504 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
506 // BNEZALC if rs == 0 && rt != 0
507 // BNEC if rs < rt && rs != 0
509 InsnType Rs = fieldFromInstruction(insn, 21, 5);
510 InsnType Rt = fieldFromInstruction(insn, 16, 5);
511 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
515 MI.setOpcode(Mips::BNVC);
517 } else if (Rs != 0 && Rs < Rt) {
518 MI.setOpcode(Mips::BNEC);
521 MI.setOpcode(Mips::BNEZALC);
524 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
527 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
529 MI.addOperand(MCOperand::CreateImm(Imm));
531 return MCDisassembler::Success;
534 template <typename InsnType>
535 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
537 const void *Decoder) {
538 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
539 // (otherwise we would have matched the BLEZL instruction from the earlier
543 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
544 // Invalid if rs == 0
545 // BLEZC if rs == 0 && rt != 0
546 // BGEZC if rs == rt && rt != 0
547 // BGEC if rs != rt && rs != 0 && rt != 0
549 InsnType Rs = fieldFromInstruction(insn, 21, 5);
550 InsnType Rt = fieldFromInstruction(insn, 16, 5);
551 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
555 return MCDisassembler::Fail;
557 MI.setOpcode(Mips::BLEZC);
559 MI.setOpcode(Mips::BGEZC);
562 MI.setOpcode(Mips::BGEC);
566 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
569 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
572 MI.addOperand(MCOperand::CreateImm(Imm));
574 return MCDisassembler::Success;
577 template <typename InsnType>
578 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
580 const void *Decoder) {
581 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
582 // (otherwise we would have matched the BGTZL instruction from the earlier
586 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
587 // Invalid if rs == 0
588 // BGTZC if rs == 0 && rt != 0
589 // BLTZC if rs == rt && rt != 0
590 // BLTC if rs != rt && rs != 0 && rt != 0
594 InsnType Rs = fieldFromInstruction(insn, 21, 5);
595 InsnType Rt = fieldFromInstruction(insn, 16, 5);
596 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
599 return MCDisassembler::Fail;
601 MI.setOpcode(Mips::BGTZC);
603 MI.setOpcode(Mips::BLTZC);
605 MI.setOpcode(Mips::BLTC);
610 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
613 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
616 MI.addOperand(MCOperand::CreateImm(Imm));
618 return MCDisassembler::Success;
621 template <typename InsnType>
622 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
624 const void *Decoder) {
625 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
626 // (otherwise we would have matched the BGTZ instruction from the earlier
630 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
632 // BGTZALC if rs == 0 && rt != 0
633 // BLTZALC if rs != 0 && rs == rt
634 // BLTUC if rs != 0 && rs != rt
636 InsnType Rs = fieldFromInstruction(insn, 21, 5);
637 InsnType Rt = fieldFromInstruction(insn, 16, 5);
638 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
643 MI.setOpcode(Mips::BGTZ);
645 } else if (Rs == 0) {
646 MI.setOpcode(Mips::BGTZALC);
648 } else if (Rs == Rt) {
649 MI.setOpcode(Mips::BLTZALC);
652 MI.setOpcode(Mips::BLTUC);
658 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
662 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
665 MI.addOperand(MCOperand::CreateImm(Imm));
667 return MCDisassembler::Success;
670 template <typename InsnType>
671 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
673 const void *Decoder) {
674 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
675 // (otherwise we would have matched the BLEZL instruction from the earlier
679 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
680 // Invalid if rs == 0
681 // BLEZALC if rs == 0 && rt != 0
682 // BGEZALC if rs == rt && rt != 0
683 // BGEUC if rs != rt && rs != 0 && rt != 0
685 InsnType Rs = fieldFromInstruction(insn, 21, 5);
686 InsnType Rt = fieldFromInstruction(insn, 16, 5);
687 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
691 return MCDisassembler::Fail;
693 MI.setOpcode(Mips::BLEZALC);
695 MI.setOpcode(Mips::BGEZALC);
698 MI.setOpcode(Mips::BGEUC);
702 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
704 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
707 MI.addOperand(MCOperand::CreateImm(Imm));
709 return MCDisassembler::Success;
712 /// readInstruction - read four bytes from the MemoryObject
713 /// and return 32 bit word sorted according to the given endianess
714 static DecodeStatus readInstruction32(const MemoryObject ®ion,
722 // We want to read exactly 4 Bytes of data.
723 if (region.readBytes(address, 4, Bytes) == -1) {
725 return MCDisassembler::Fail;
729 // Encoded as a big-endian 32-bit word in the stream.
730 insn = (Bytes[3] << 0) |
736 // Encoded as a small-endian 32-bit word in the stream.
737 // Little-endian byte ordering:
738 // mips32r2: 4 | 3 | 2 | 1
739 // microMIPS: 2 | 1 | 4 | 3
741 insn = (Bytes[2] << 0) |
746 insn = (Bytes[0] << 0) |
753 return MCDisassembler::Success;
757 MipsDisassembler::getInstruction(MCInst &instr,
759 const MemoryObject &Region,
761 raw_ostream &vStream,
762 raw_ostream &cStream) const {
765 DecodeStatus Result = readInstruction32(Region, Address, Size,
766 Insn, isBigEndian, IsMicroMips);
767 if (Result == MCDisassembler::Fail)
768 return MCDisassembler::Fail;
771 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
772 // Calling the auto-generated decoder function.
773 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
775 if (Result != MCDisassembler::Fail) {
779 return MCDisassembler::Fail;
783 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
785 decodeInstruction(DecoderTableCOP3_32, instr, Insn, Address, this, STI);
786 if (Result != MCDisassembler::Fail) {
792 if (hasMips32r6() && isGP64()) {
793 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
794 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
796 if (Result != MCDisassembler::Fail) {
803 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
804 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
806 if (Result != MCDisassembler::Fail) {
812 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
813 // Calling the auto-generated decoder function.
814 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
816 if (Result != MCDisassembler::Fail) {
821 return MCDisassembler::Fail;
825 Mips64Disassembler::getInstruction(MCInst &instr,
827 const MemoryObject &Region,
829 raw_ostream &vStream,
830 raw_ostream &cStream) const {
833 DecodeStatus Result = readInstruction32(Region, Address, Size,
834 Insn, isBigEndian, false);
835 if (Result == MCDisassembler::Fail)
836 return MCDisassembler::Fail;
838 // Calling the auto-generated decoder function.
839 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
841 if (Result != MCDisassembler::Fail) {
845 // If we fail to decode in Mips64 decoder space we can try in Mips32
846 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
848 if (Result != MCDisassembler::Fail) {
853 return MCDisassembler::Fail;
856 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
859 const void *Decoder) {
861 return MCDisassembler::Fail;
865 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
868 const void *Decoder) {
871 return MCDisassembler::Fail;
873 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
874 Inst.addOperand(MCOperand::CreateReg(Reg));
875 return MCDisassembler::Success;
878 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
881 const void *Decoder) {
882 return MCDisassembler::Fail;
885 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
888 const void *Decoder) {
890 return MCDisassembler::Fail;
891 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
892 Inst.addOperand(MCOperand::CreateReg(Reg));
893 return MCDisassembler::Success;
896 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
899 const void *Decoder) {
900 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
901 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
903 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
906 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
909 const void *Decoder) {
910 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
913 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
916 const void *Decoder) {
918 return MCDisassembler::Fail;
920 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
921 Inst.addOperand(MCOperand::CreateReg(Reg));
922 return MCDisassembler::Success;
925 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
928 const void *Decoder) {
930 return MCDisassembler::Fail;
932 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
933 Inst.addOperand(MCOperand::CreateReg(Reg));
934 return MCDisassembler::Success;
937 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
940 const void *Decoder) {
942 return MCDisassembler::Fail;
943 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
944 Inst.addOperand(MCOperand::CreateReg(Reg));
945 return MCDisassembler::Success;
948 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
951 const void *Decoder) {
953 return MCDisassembler::Fail;
954 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
955 Inst.addOperand(MCOperand::CreateReg(Reg));
956 return MCDisassembler::Success;
959 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
961 const void *Decoder) {
963 return MCDisassembler::Fail;
965 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
966 Inst.addOperand(MCOperand::CreateReg(Reg));
967 return MCDisassembler::Success;
970 static DecodeStatus DecodeMem(MCInst &Inst,
973 const void *Decoder) {
974 int Offset = SignExtend32<16>(Insn & 0xffff);
975 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
976 unsigned Base = fieldFromInstruction(Insn, 21, 5);
978 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
979 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
981 if(Inst.getOpcode() == Mips::SC){
982 Inst.addOperand(MCOperand::CreateReg(Reg));
985 Inst.addOperand(MCOperand::CreateReg(Reg));
986 Inst.addOperand(MCOperand::CreateReg(Base));
987 Inst.addOperand(MCOperand::CreateImm(Offset));
989 return MCDisassembler::Success;
992 static DecodeStatus DecodeCacheOp(MCInst &Inst,
995 const void *Decoder) {
996 int Offset = SignExtend32<16>(Insn & 0xffff);
997 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
998 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1000 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1002 Inst.addOperand(MCOperand::CreateReg(Base));
1003 Inst.addOperand(MCOperand::CreateImm(Offset));
1004 Inst.addOperand(MCOperand::CreateImm(Hint));
1006 return MCDisassembler::Success;
1009 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1010 uint64_t Address, const void *Decoder) {
1011 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1012 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1013 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1015 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1016 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1018 Inst.addOperand(MCOperand::CreateReg(Reg));
1019 Inst.addOperand(MCOperand::CreateReg(Base));
1021 // The immediate field of an LD/ST instruction is scaled which means it must
1022 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1028 switch(Inst.getOpcode())
1031 assert (0 && "Unexpected instruction");
1032 return MCDisassembler::Fail;
1036 Inst.addOperand(MCOperand::CreateImm(Offset));
1040 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1044 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1048 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1052 return MCDisassembler::Success;
1055 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1058 const void *Decoder) {
1059 int Offset = SignExtend32<12>(Insn & 0x0fff);
1060 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1061 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1063 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1064 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1066 if (Inst.getOpcode() == Mips::SC_MM)
1067 Inst.addOperand(MCOperand::CreateReg(Reg));
1069 Inst.addOperand(MCOperand::CreateReg(Reg));
1070 Inst.addOperand(MCOperand::CreateReg(Base));
1071 Inst.addOperand(MCOperand::CreateImm(Offset));
1073 return MCDisassembler::Success;
1076 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1079 const void *Decoder) {
1080 int Offset = SignExtend32<16>(Insn & 0xffff);
1081 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1082 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1084 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1085 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1087 Inst.addOperand(MCOperand::CreateReg(Reg));
1088 Inst.addOperand(MCOperand::CreateReg(Base));
1089 Inst.addOperand(MCOperand::CreateImm(Offset));
1091 return MCDisassembler::Success;
1094 static DecodeStatus DecodeFMem(MCInst &Inst,
1097 const void *Decoder) {
1098 int Offset = SignExtend32<16>(Insn & 0xffff);
1099 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1100 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1102 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1103 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1105 Inst.addOperand(MCOperand::CreateReg(Reg));
1106 Inst.addOperand(MCOperand::CreateReg(Base));
1107 Inst.addOperand(MCOperand::CreateImm(Offset));
1109 return MCDisassembler::Success;
1112 static DecodeStatus DecodeFMem2(MCInst &Inst,
1115 const void *Decoder) {
1116 int Offset = SignExtend32<16>(Insn & 0xffff);
1117 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1118 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1120 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1121 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1123 Inst.addOperand(MCOperand::CreateReg(Reg));
1124 Inst.addOperand(MCOperand::CreateReg(Base));
1125 Inst.addOperand(MCOperand::CreateImm(Offset));
1127 return MCDisassembler::Success;
1130 static DecodeStatus DecodeFMem3(MCInst &Inst,
1133 const void *Decoder) {
1134 int Offset = SignExtend32<16>(Insn & 0xffff);
1135 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1136 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1138 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1139 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1141 Inst.addOperand(MCOperand::CreateReg(Reg));
1142 Inst.addOperand(MCOperand::CreateReg(Base));
1143 Inst.addOperand(MCOperand::CreateImm(Offset));
1145 return MCDisassembler::Success;
1148 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1151 const void *Decoder) {
1152 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1153 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1154 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1156 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1157 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1159 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1160 Inst.addOperand(MCOperand::CreateReg(Rt));
1163 Inst.addOperand(MCOperand::CreateReg(Rt));
1164 Inst.addOperand(MCOperand::CreateReg(Base));
1165 Inst.addOperand(MCOperand::CreateImm(Offset));
1167 return MCDisassembler::Success;
1170 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1173 const void *Decoder) {
1174 // Currently only hardware register 29 is supported.
1176 return MCDisassembler::Fail;
1177 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1178 return MCDisassembler::Success;
1181 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1184 const void *Decoder) {
1185 if (RegNo > 30 || RegNo %2)
1186 return MCDisassembler::Fail;
1189 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1190 Inst.addOperand(MCOperand::CreateReg(Reg));
1191 return MCDisassembler::Success;
1194 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1197 const void *Decoder) {
1199 return MCDisassembler::Fail;
1201 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1202 Inst.addOperand(MCOperand::CreateReg(Reg));
1203 return MCDisassembler::Success;
1206 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1209 const void *Decoder) {
1211 return MCDisassembler::Fail;
1213 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1214 Inst.addOperand(MCOperand::CreateReg(Reg));
1215 return MCDisassembler::Success;
1218 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1221 const void *Decoder) {
1223 return MCDisassembler::Fail;
1225 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1226 Inst.addOperand(MCOperand::CreateReg(Reg));
1227 return MCDisassembler::Success;
1230 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1233 const void *Decoder) {
1235 return MCDisassembler::Fail;
1237 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1238 Inst.addOperand(MCOperand::CreateReg(Reg));
1239 return MCDisassembler::Success;
1242 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1245 const void *Decoder) {
1247 return MCDisassembler::Fail;
1249 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1250 Inst.addOperand(MCOperand::CreateReg(Reg));
1251 return MCDisassembler::Success;
1254 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1257 const void *Decoder) {
1259 return MCDisassembler::Fail;
1261 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1262 Inst.addOperand(MCOperand::CreateReg(Reg));
1263 return MCDisassembler::Success;
1266 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1269 const void *Decoder) {
1271 return MCDisassembler::Fail;
1273 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1274 Inst.addOperand(MCOperand::CreateReg(Reg));
1275 return MCDisassembler::Success;
1278 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1281 const void *Decoder) {
1283 return MCDisassembler::Fail;
1285 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1286 Inst.addOperand(MCOperand::CreateReg(Reg));
1287 return MCDisassembler::Success;
1290 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1293 const void *Decoder) {
1295 return MCDisassembler::Fail;
1297 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1298 Inst.addOperand(MCOperand::CreateReg(Reg));
1299 return MCDisassembler::Success;
1302 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1305 const void *Decoder) {
1306 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1307 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1308 return MCDisassembler::Success;
1311 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1314 const void *Decoder) {
1316 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1317 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1318 return MCDisassembler::Success;
1321 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1324 const void *Decoder) {
1325 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1327 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1328 return MCDisassembler::Success;
1331 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1334 const void *Decoder) {
1335 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1337 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1338 return MCDisassembler::Success;
1341 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1344 const void *Decoder) {
1345 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1346 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1347 return MCDisassembler::Success;
1350 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1353 const void *Decoder) {
1354 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1355 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1356 return MCDisassembler::Success;
1359 static DecodeStatus DecodeSimm16(MCInst &Inst,
1362 const void *Decoder) {
1363 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1364 return MCDisassembler::Success;
1367 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1370 const void *Decoder) {
1371 // We add one to the immediate field as it was encoded as 'imm - 1'.
1372 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1373 return MCDisassembler::Success;
1376 static DecodeStatus DecodeInsSize(MCInst &Inst,
1379 const void *Decoder) {
1380 // First we need to grab the pos(lsb) from MCInst.
1381 int Pos = Inst.getOperand(2).getImm();
1382 int Size = (int) Insn - Pos + 1;
1383 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1384 return MCDisassembler::Success;
1387 static DecodeStatus DecodeExtSize(MCInst &Inst,
1390 const void *Decoder) {
1391 int Size = (int) Insn + 1;
1392 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1393 return MCDisassembler::Success;
1396 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1397 uint64_t Address, const void *Decoder) {
1398 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1399 return MCDisassembler::Success;
1402 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1403 uint64_t Address, const void *Decoder) {
1404 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1405 return MCDisassembler::Success;