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 DecodeGPR32RegisterClass(MCInst &Inst,
123 const void *Decoder);
125 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
128 const void *Decoder);
130 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
133 const void *Decoder);
135 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
138 const void *Decoder);
140 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
143 const void *Decoder);
145 static DecodeStatus DecodeFGRH32RegisterClass(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 DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
259 uint64_t Address, const void *Decoder);
261 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
264 const void *Decoder);
266 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
269 const void *Decoder);
271 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
273 const void *Decoder);
275 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
278 const void *Decoder);
280 static DecodeStatus DecodeSimm16(MCInst &Inst,
283 const void *Decoder);
285 // Decode the immediate field of an LSA instruction which
287 static DecodeStatus DecodeLSAImm(MCInst &Inst,
290 const void *Decoder);
292 static DecodeStatus DecodeInsSize(MCInst &Inst,
295 const void *Decoder);
297 static DecodeStatus DecodeExtSize(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
303 uint64_t Address, const void *Decoder);
305 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
306 uint64_t Address, const void *Decoder);
308 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
310 template <typename InsnType>
311 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
312 const void *Decoder);
314 template <typename InsnType>
316 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
317 const void *Decoder);
319 template <typename InsnType>
321 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
322 const void *Decoder);
324 template <typename InsnType>
326 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
327 const void *Decoder);
329 template <typename InsnType>
331 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
332 const void *Decoder);
334 template <typename InsnType>
336 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
337 const void *Decoder);
339 template <typename InsnType>
341 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
342 const void *Decoder);
345 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
349 static MCDisassembler *createMipsDisassembler(
351 const MCSubtargetInfo &STI,
353 return new MipsDisassembler(STI, Ctx, true);
356 static MCDisassembler *createMipselDisassembler(
358 const MCSubtargetInfo &STI,
360 return new MipsDisassembler(STI, Ctx, false);
363 static MCDisassembler *createMips64Disassembler(
365 const MCSubtargetInfo &STI,
367 return new Mips64Disassembler(STI, Ctx, true);
370 static MCDisassembler *createMips64elDisassembler(
372 const MCSubtargetInfo &STI,
374 return new Mips64Disassembler(STI, Ctx, false);
377 extern "C" void LLVMInitializeMipsDisassembler() {
378 // Register the disassembler.
379 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
380 createMipsDisassembler);
381 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
382 createMipselDisassembler);
383 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
384 createMips64Disassembler);
385 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
386 createMips64elDisassembler);
389 #include "MipsGenDisassemblerTables.inc"
391 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
392 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
393 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
394 return *(RegInfo->getRegClass(RC).begin() + RegNo);
397 template <typename InsnType>
398 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
399 const void *Decoder) {
400 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
401 // The size of the n field depends on the element size
402 // The register class also depends on this.
403 InsnType tmp = fieldFromInstruction(insn, 17, 5);
405 DecodeFN RegDecoder = nullptr;
406 if ((tmp & 0x18) == 0x00) { // INSVE_B
408 RegDecoder = DecodeMSA128BRegisterClass;
409 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
411 RegDecoder = DecodeMSA128HRegisterClass;
412 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
414 RegDecoder = DecodeMSA128WRegisterClass;
415 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
417 RegDecoder = DecodeMSA128DRegisterClass;
419 llvm_unreachable("Invalid encoding");
421 assert(NSize != 0 && RegDecoder != nullptr);
424 tmp = fieldFromInstruction(insn, 6, 5);
425 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
426 return MCDisassembler::Fail;
428 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
429 return MCDisassembler::Fail;
431 tmp = fieldFromInstruction(insn, 16, NSize);
432 MI.addOperand(MCOperand::CreateImm(tmp));
434 tmp = fieldFromInstruction(insn, 11, 5);
435 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
436 return MCDisassembler::Fail;
438 MI.addOperand(MCOperand::CreateImm(0));
440 return MCDisassembler::Success;
443 template <typename InsnType>
444 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
446 const void *Decoder) {
447 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
448 // (otherwise we would have matched the ADDI instruction from the earlier
452 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
454 // BEQZALC if rs == 0 && rt != 0
455 // BEQC if rs < rt && rs != 0
457 InsnType Rs = fieldFromInstruction(insn, 21, 5);
458 InsnType Rt = fieldFromInstruction(insn, 16, 5);
459 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
463 MI.setOpcode(Mips::BOVC);
465 } else if (Rs != 0 && Rs < Rt) {
466 MI.setOpcode(Mips::BEQC);
469 MI.setOpcode(Mips::BEQZALC);
472 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
475 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
477 MI.addOperand(MCOperand::CreateImm(Imm));
479 return MCDisassembler::Success;
482 template <typename InsnType>
483 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
485 const void *Decoder) {
486 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
487 // (otherwise we would have matched the ADDI instruction from the earlier
491 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
493 // BNEZALC if rs == 0 && rt != 0
494 // BNEC if rs < rt && rs != 0
496 InsnType Rs = fieldFromInstruction(insn, 21, 5);
497 InsnType Rt = fieldFromInstruction(insn, 16, 5);
498 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
502 MI.setOpcode(Mips::BNVC);
504 } else if (Rs != 0 && Rs < Rt) {
505 MI.setOpcode(Mips::BNEC);
508 MI.setOpcode(Mips::BNEZALC);
511 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
514 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
516 MI.addOperand(MCOperand::CreateImm(Imm));
518 return MCDisassembler::Success;
521 template <typename InsnType>
522 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
524 const void *Decoder) {
525 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
526 // (otherwise we would have matched the BLEZL instruction from the earlier
530 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
531 // Invalid if rs == 0
532 // BLEZC if rs == 0 && rt != 0
533 // BGEZC if rs == rt && rt != 0
534 // BGEC if rs != rt && rs != 0 && rt != 0
536 InsnType Rs = fieldFromInstruction(insn, 21, 5);
537 InsnType Rt = fieldFromInstruction(insn, 16, 5);
538 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
542 return MCDisassembler::Fail;
544 MI.setOpcode(Mips::BLEZC);
546 MI.setOpcode(Mips::BGEZC);
549 MI.setOpcode(Mips::BGEC);
553 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
556 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
559 MI.addOperand(MCOperand::CreateImm(Imm));
561 return MCDisassembler::Success;
564 template <typename InsnType>
565 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
567 const void *Decoder) {
568 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
569 // (otherwise we would have matched the BGTZL instruction from the earlier
573 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
574 // Invalid if rs == 0
575 // BGTZC if rs == 0 && rt != 0
576 // BLTZC if rs == rt && rt != 0
577 // BLTC if rs != rt && rs != 0 && rt != 0
581 InsnType Rs = fieldFromInstruction(insn, 21, 5);
582 InsnType Rt = fieldFromInstruction(insn, 16, 5);
583 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
586 return MCDisassembler::Fail;
588 MI.setOpcode(Mips::BGTZC);
590 MI.setOpcode(Mips::BLTZC);
592 MI.setOpcode(Mips::BLTC);
597 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
600 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
603 MI.addOperand(MCOperand::CreateImm(Imm));
605 return MCDisassembler::Success;
608 template <typename InsnType>
609 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
611 const void *Decoder) {
612 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
613 // (otherwise we would have matched the BGTZ instruction from the earlier
617 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
619 // BGTZALC if rs == 0 && rt != 0
620 // BLTZALC if rs != 0 && rs == rt
621 // BLTUC if rs != 0 && rs != rt
623 InsnType Rs = fieldFromInstruction(insn, 21, 5);
624 InsnType Rt = fieldFromInstruction(insn, 16, 5);
625 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
630 MI.setOpcode(Mips::BGTZ);
632 } else if (Rs == 0) {
633 MI.setOpcode(Mips::BGTZALC);
635 } else if (Rs == Rt) {
636 MI.setOpcode(Mips::BLTZALC);
639 MI.setOpcode(Mips::BLTUC);
645 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
652 MI.addOperand(MCOperand::CreateImm(Imm));
654 return MCDisassembler::Success;
657 template <typename InsnType>
658 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
660 const void *Decoder) {
661 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
662 // (otherwise we would have matched the BLEZL instruction from the earlier
666 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
667 // Invalid if rs == 0
668 // BLEZALC if rs == 0 && rt != 0
669 // BGEZALC if rs == rt && rt != 0
670 // BGEUC if rs != rt && rs != 0 && rt != 0
672 InsnType Rs = fieldFromInstruction(insn, 21, 5);
673 InsnType Rt = fieldFromInstruction(insn, 16, 5);
674 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
678 return MCDisassembler::Fail;
680 MI.setOpcode(Mips::BLEZALC);
682 MI.setOpcode(Mips::BGEZALC);
685 MI.setOpcode(Mips::BGEUC);
689 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
691 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
694 MI.addOperand(MCOperand::CreateImm(Imm));
696 return MCDisassembler::Success;
699 /// readInstruction - read four bytes from the MemoryObject
700 /// and return 32 bit word sorted according to the given endianess
701 static DecodeStatus readInstruction32(const MemoryObject ®ion,
709 // We want to read exactly 4 Bytes of data.
710 if (region.readBytes(address, 4, Bytes) == -1) {
712 return MCDisassembler::Fail;
716 // Encoded as a big-endian 32-bit word in the stream.
717 insn = (Bytes[3] << 0) |
723 // Encoded as a small-endian 32-bit word in the stream.
724 // Little-endian byte ordering:
725 // mips32r2: 4 | 3 | 2 | 1
726 // microMIPS: 2 | 1 | 4 | 3
728 insn = (Bytes[2] << 0) |
733 insn = (Bytes[0] << 0) |
740 return MCDisassembler::Success;
744 MipsDisassembler::getInstruction(MCInst &instr,
746 const MemoryObject &Region,
748 raw_ostream &vStream,
749 raw_ostream &cStream) const {
752 DecodeStatus Result = readInstruction32(Region, Address, Size,
753 Insn, isBigEndian, IsMicroMips);
754 if (Result == MCDisassembler::Fail)
755 return MCDisassembler::Fail;
758 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
759 // Calling the auto-generated decoder function.
760 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
762 if (Result != MCDisassembler::Fail) {
766 return MCDisassembler::Fail;
770 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
772 decodeInstruction(DecoderTableCOP3_32, instr, Insn, Address, this, STI);
773 if (Result != MCDisassembler::Fail) {
779 if (hasMips32r6() && isGP64()) {
780 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
781 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
783 if (Result != MCDisassembler::Fail) {
790 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
791 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
793 if (Result != MCDisassembler::Fail) {
799 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
800 // Calling the auto-generated decoder function.
801 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
803 if (Result != MCDisassembler::Fail) {
808 return MCDisassembler::Fail;
812 Mips64Disassembler::getInstruction(MCInst &instr,
814 const MemoryObject &Region,
816 raw_ostream &vStream,
817 raw_ostream &cStream) const {
820 DecodeStatus Result = readInstruction32(Region, Address, Size,
821 Insn, isBigEndian, false);
822 if (Result == MCDisassembler::Fail)
823 return MCDisassembler::Fail;
825 // Calling the auto-generated decoder function.
826 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
828 if (Result != MCDisassembler::Fail) {
832 // If we fail to decode in Mips64 decoder space we can try in Mips32
833 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
835 if (Result != MCDisassembler::Fail) {
840 return MCDisassembler::Fail;
843 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
846 const void *Decoder) {
848 return MCDisassembler::Fail;
852 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
855 const void *Decoder) {
858 return MCDisassembler::Fail;
860 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
861 Inst.addOperand(MCOperand::CreateReg(Reg));
862 return MCDisassembler::Success;
865 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
868 const void *Decoder) {
870 return MCDisassembler::Fail;
871 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
872 Inst.addOperand(MCOperand::CreateReg(Reg));
873 return MCDisassembler::Success;
876 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
879 const void *Decoder) {
880 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
881 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
883 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
886 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
889 const void *Decoder) {
890 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
893 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
896 const void *Decoder) {
898 return MCDisassembler::Fail;
900 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
901 Inst.addOperand(MCOperand::CreateReg(Reg));
902 return MCDisassembler::Success;
905 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
908 const void *Decoder) {
910 return MCDisassembler::Fail;
912 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
913 Inst.addOperand(MCOperand::CreateReg(Reg));
914 return MCDisassembler::Success;
917 static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
920 const void *Decoder) {
922 return MCDisassembler::Fail;
924 unsigned Reg = getReg(Decoder, Mips::FGRH32RegClassID, RegNo);
925 Inst.addOperand(MCOperand::CreateReg(Reg));
926 return MCDisassembler::Success;
929 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
932 const void *Decoder) {
934 return MCDisassembler::Fail;
935 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
936 Inst.addOperand(MCOperand::CreateReg(Reg));
937 return MCDisassembler::Success;
940 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
943 const void *Decoder) {
945 return MCDisassembler::Fail;
946 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
947 Inst.addOperand(MCOperand::CreateReg(Reg));
948 return MCDisassembler::Success;
951 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
953 const void *Decoder) {
955 return MCDisassembler::Fail;
957 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
958 Inst.addOperand(MCOperand::CreateReg(Reg));
959 return MCDisassembler::Success;
962 static DecodeStatus DecodeMem(MCInst &Inst,
965 const void *Decoder) {
966 int Offset = SignExtend32<16>(Insn & 0xffff);
967 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
968 unsigned Base = fieldFromInstruction(Insn, 21, 5);
970 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
971 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
973 if(Inst.getOpcode() == Mips::SC){
974 Inst.addOperand(MCOperand::CreateReg(Reg));
977 Inst.addOperand(MCOperand::CreateReg(Reg));
978 Inst.addOperand(MCOperand::CreateReg(Base));
979 Inst.addOperand(MCOperand::CreateImm(Offset));
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 << 1));
1019 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1023 Inst.addOperand(MCOperand::CreateImm(Offset << 3));
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 DecodeSpecial3LlSc(MCInst &Inst,
1090 const void *Decoder) {
1091 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1092 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1093 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1095 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1096 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1098 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1099 Inst.addOperand(MCOperand::CreateReg(Rt));
1102 Inst.addOperand(MCOperand::CreateReg(Rt));
1103 Inst.addOperand(MCOperand::CreateReg(Base));
1104 Inst.addOperand(MCOperand::CreateImm(Offset));
1106 return MCDisassembler::Success;
1109 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1112 const void *Decoder) {
1113 // Currently only hardware register 29 is supported.
1115 return MCDisassembler::Fail;
1116 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1117 return MCDisassembler::Success;
1120 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1123 const void *Decoder) {
1124 if (RegNo > 30 || RegNo %2)
1125 return MCDisassembler::Fail;
1128 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1129 Inst.addOperand(MCOperand::CreateReg(Reg));
1130 return MCDisassembler::Success;
1133 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1136 const void *Decoder) {
1138 return MCDisassembler::Fail;
1140 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1141 Inst.addOperand(MCOperand::CreateReg(Reg));
1142 return MCDisassembler::Success;
1145 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1148 const void *Decoder) {
1150 return MCDisassembler::Fail;
1152 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1153 Inst.addOperand(MCOperand::CreateReg(Reg));
1154 return MCDisassembler::Success;
1157 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1160 const void *Decoder) {
1162 return MCDisassembler::Fail;
1164 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1165 Inst.addOperand(MCOperand::CreateReg(Reg));
1166 return MCDisassembler::Success;
1169 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1172 const void *Decoder) {
1174 return MCDisassembler::Fail;
1176 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1177 Inst.addOperand(MCOperand::CreateReg(Reg));
1178 return MCDisassembler::Success;
1181 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1184 const void *Decoder) {
1186 return MCDisassembler::Fail;
1188 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1189 Inst.addOperand(MCOperand::CreateReg(Reg));
1190 return MCDisassembler::Success;
1193 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1196 const void *Decoder) {
1198 return MCDisassembler::Fail;
1200 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1201 Inst.addOperand(MCOperand::CreateReg(Reg));
1202 return MCDisassembler::Success;
1205 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1208 const void *Decoder) {
1210 return MCDisassembler::Fail;
1212 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1213 Inst.addOperand(MCOperand::CreateReg(Reg));
1214 return MCDisassembler::Success;
1217 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1220 const void *Decoder) {
1222 return MCDisassembler::Fail;
1224 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1225 Inst.addOperand(MCOperand::CreateReg(Reg));
1226 return MCDisassembler::Success;
1229 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1232 const void *Decoder) {
1234 return MCDisassembler::Fail;
1236 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1237 Inst.addOperand(MCOperand::CreateReg(Reg));
1238 return MCDisassembler::Success;
1241 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1244 const void *Decoder) {
1245 int32_t BranchOffset = (SignExtend32<16>(Offset) << 2) + 4;
1246 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1247 return MCDisassembler::Success;
1250 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1253 const void *Decoder) {
1255 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1256 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1257 return MCDisassembler::Success;
1260 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1263 const void *Decoder) {
1264 int32_t BranchOffset = SignExtend32<21>(Offset) << 2;
1266 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1267 return MCDisassembler::Success;
1270 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1273 const void *Decoder) {
1274 int32_t BranchOffset = SignExtend32<26>(Offset) << 2;
1276 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1277 return MCDisassembler::Success;
1280 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1283 const void *Decoder) {
1284 int32_t BranchOffset = SignExtend32<16>(Offset) << 1;
1285 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1286 return MCDisassembler::Success;
1289 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1292 const void *Decoder) {
1293 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1294 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1295 return MCDisassembler::Success;
1298 static DecodeStatus DecodeSimm16(MCInst &Inst,
1301 const void *Decoder) {
1302 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1303 return MCDisassembler::Success;
1306 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1309 const void *Decoder) {
1310 // We add one to the immediate field as it was encoded as 'imm - 1'.
1311 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1312 return MCDisassembler::Success;
1315 static DecodeStatus DecodeInsSize(MCInst &Inst,
1318 const void *Decoder) {
1319 // First we need to grab the pos(lsb) from MCInst.
1320 int Pos = Inst.getOperand(2).getImm();
1321 int Size = (int) Insn - Pos + 1;
1322 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1323 return MCDisassembler::Success;
1326 static DecodeStatus DecodeExtSize(MCInst &Inst,
1329 const void *Decoder) {
1330 int Size = (int) Insn + 1;
1331 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1332 return MCDisassembler::Success;
1335 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1336 uint64_t Address, const void *Decoder) {
1337 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) << 2));
1338 return MCDisassembler::Success;
1341 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1342 uint64_t Address, const void *Decoder) {
1343 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) << 3));
1344 return MCDisassembler::Success;