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,
62 MipsDisassemblerBase(STI, Ctx, bigEndian) {
63 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
66 bool isMips32r6() const {
67 return STI.getFeatureBits() & Mips::FeatureMips32r6;
70 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
72 /// getInstruction - See MCDisassembler.
73 DecodeStatus getInstruction(MCInst &instr,
75 const MemoryObject ®ion,
78 raw_ostream &cStream) const override;
82 /// Mips64Disassembler - a disasembler class for Mips64.
83 class Mips64Disassembler : public MipsDisassemblerBase {
85 /// Constructor - Initializes the disassembler.
87 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
89 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
91 /// getInstruction - See MCDisassembler.
92 DecodeStatus getInstruction(MCInst &instr,
94 const MemoryObject ®ion,
97 raw_ostream &cStream) const override;
100 } // end anonymous namespace
102 // Forward declare these because the autogenerated code will reference them.
103 // Definitions are further down.
104 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
107 const void *Decoder);
109 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
112 const void *Decoder);
114 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
117 const void *Decoder);
119 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
122 const void *Decoder);
124 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
127 const void *Decoder);
129 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
132 const void *Decoder);
134 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
137 const void *Decoder);
139 static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
142 const void *Decoder);
144 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
147 const void *Decoder);
149 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
152 const void *Decoder);
154 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
156 const void *Decoder);
158 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
161 const void *Decoder);
163 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
166 const void *Decoder);
168 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
171 const void *Decoder);
173 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
176 const void *Decoder);
178 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
181 const void *Decoder);
183 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
186 const void *Decoder);
188 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
191 const void *Decoder);
193 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
196 const void *Decoder);
198 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
201 const void *Decoder);
203 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
206 const void *Decoder);
208 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
211 const void *Decoder);
213 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
216 const void *Decoder);
218 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
221 const void *Decoder);
223 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
226 const void *Decoder);
228 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
231 const void *Decoder);
233 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
234 // shifted left by 1 bit.
235 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
238 const void *Decoder);
240 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
241 // shifted left by 1 bit.
242 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
245 const void *Decoder);
247 static DecodeStatus DecodeMem(MCInst &Inst,
250 const void *Decoder);
252 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
253 uint64_t Address, const void *Decoder);
255 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
258 const void *Decoder);
260 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
263 const void *Decoder);
265 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
267 const void *Decoder);
269 static DecodeStatus DecodeSimm16(MCInst &Inst,
272 const void *Decoder);
274 // Decode the immediate field of an LSA instruction which
276 static DecodeStatus DecodeLSAImm(MCInst &Inst,
279 const void *Decoder);
281 static DecodeStatus DecodeInsSize(MCInst &Inst,
284 const void *Decoder);
286 static DecodeStatus DecodeExtSize(MCInst &Inst,
289 const void *Decoder);
291 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
292 uint64_t Address, const void *Decoder);
294 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
295 uint64_t Address, const void *Decoder);
297 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
299 template <typename InsnType>
300 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
301 const void *Decoder);
303 template <typename InsnType>
305 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
306 const void *Decoder);
308 template <typename InsnType>
310 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
311 const void *Decoder);
313 template <typename InsnType>
315 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
316 const void *Decoder);
318 template <typename InsnType>
320 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
321 const void *Decoder);
323 template <typename InsnType>
325 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
326 const void *Decoder);
328 template <typename InsnType>
330 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
331 const void *Decoder);
334 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
338 static MCDisassembler *createMipsDisassembler(
340 const MCSubtargetInfo &STI,
342 return new MipsDisassembler(STI, Ctx, true);
345 static MCDisassembler *createMipselDisassembler(
347 const MCSubtargetInfo &STI,
349 return new MipsDisassembler(STI, Ctx, false);
352 static MCDisassembler *createMips64Disassembler(
354 const MCSubtargetInfo &STI,
356 return new Mips64Disassembler(STI, Ctx, true);
359 static MCDisassembler *createMips64elDisassembler(
361 const MCSubtargetInfo &STI,
363 return new Mips64Disassembler(STI, Ctx, false);
366 extern "C" void LLVMInitializeMipsDisassembler() {
367 // Register the disassembler.
368 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
369 createMipsDisassembler);
370 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
371 createMipselDisassembler);
372 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
373 createMips64Disassembler);
374 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
375 createMips64elDisassembler);
378 #include "MipsGenDisassemblerTables.inc"
380 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
381 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
382 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
383 return *(RegInfo->getRegClass(RC).begin() + RegNo);
386 template <typename InsnType>
387 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
388 const void *Decoder) {
389 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
390 // The size of the n field depends on the element size
391 // The register class also depends on this.
392 InsnType tmp = fieldFromInstruction(insn, 17, 5);
394 DecodeFN RegDecoder = nullptr;
395 if ((tmp & 0x18) == 0x00) { // INSVE_B
397 RegDecoder = DecodeMSA128BRegisterClass;
398 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
400 RegDecoder = DecodeMSA128HRegisterClass;
401 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
403 RegDecoder = DecodeMSA128WRegisterClass;
404 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
406 RegDecoder = DecodeMSA128DRegisterClass;
408 llvm_unreachable("Invalid encoding");
410 assert(NSize != 0 && RegDecoder != nullptr);
413 tmp = fieldFromInstruction(insn, 6, 5);
414 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
415 return MCDisassembler::Fail;
417 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
418 return MCDisassembler::Fail;
420 tmp = fieldFromInstruction(insn, 16, NSize);
421 MI.addOperand(MCOperand::CreateImm(tmp));
423 tmp = fieldFromInstruction(insn, 11, 5);
424 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
425 return MCDisassembler::Fail;
427 MI.addOperand(MCOperand::CreateImm(0));
429 return MCDisassembler::Success;
432 template <typename InsnType>
433 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
435 const void *Decoder) {
436 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
437 // (otherwise we would have matched the ADDI instruction from the earlier
441 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
443 // BEQZALC if rs == 0 && rt != 0
444 // BEQC if rs < rt && rs != 0
446 InsnType Rs = fieldFromInstruction(insn, 21, 5);
447 InsnType Rt = fieldFromInstruction(insn, 16, 5);
448 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
452 MI.setOpcode(Mips::BOVC);
454 } else if (Rs != 0 && Rs < Rt) {
455 MI.setOpcode(Mips::BEQC);
458 MI.setOpcode(Mips::BEQZALC);
461 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
464 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
466 MI.addOperand(MCOperand::CreateImm(Imm));
468 return MCDisassembler::Success;
471 template <typename InsnType>
472 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
474 const void *Decoder) {
475 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
476 // (otherwise we would have matched the ADDI instruction from the earlier
480 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
482 // BNEZALC if rs == 0 && rt != 0
483 // BNEC if rs < rt && rs != 0
485 InsnType Rs = fieldFromInstruction(insn, 21, 5);
486 InsnType Rt = fieldFromInstruction(insn, 16, 5);
487 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
491 MI.setOpcode(Mips::BNVC);
493 } else if (Rs != 0 && Rs < Rt) {
494 MI.setOpcode(Mips::BNEC);
497 MI.setOpcode(Mips::BNEZALC);
500 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
503 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
505 MI.addOperand(MCOperand::CreateImm(Imm));
507 return MCDisassembler::Success;
510 template <typename InsnType>
511 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
513 const void *Decoder) {
514 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
515 // (otherwise we would have matched the BLEZL instruction from the earlier
519 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
520 // Invalid if rs == 0
521 // BLEZC if rs == 0 && rt != 0
522 // BGEZC if rs == rt && rt != 0
523 // BGEC if rs != rt && rs != 0 && rt != 0
525 InsnType Rs = fieldFromInstruction(insn, 21, 5);
526 InsnType Rt = fieldFromInstruction(insn, 16, 5);
527 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
531 return MCDisassembler::Fail;
533 MI.setOpcode(Mips::BLEZC);
535 MI.setOpcode(Mips::BGEZC);
538 MI.setOpcode(Mips::BGEC);
542 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
545 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
548 MI.addOperand(MCOperand::CreateImm(Imm));
550 return MCDisassembler::Success;
553 template <typename InsnType>
554 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
556 const void *Decoder) {
557 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
558 // (otherwise we would have matched the BGTZL instruction from the earlier
562 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
563 // Invalid if rs == 0
564 // BGTZC if rs == 0 && rt != 0
565 // BLTZC if rs == rt && rt != 0
566 // BLTC if rs != rt && rs != 0 && rt != 0
568 InsnType Rs = fieldFromInstruction(insn, 21, 5);
569 InsnType Rt = fieldFromInstruction(insn, 16, 5);
570 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
573 return MCDisassembler::Fail;
575 MI.setOpcode(Mips::BGTZC);
577 MI.setOpcode(Mips::BLTZC);
579 return MCDisassembler::Fail; // FIXME: BLTC is not implemented yet.
581 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
584 MI.addOperand(MCOperand::CreateImm(Imm));
586 return MCDisassembler::Success;
589 template <typename InsnType>
590 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
592 const void *Decoder) {
593 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
594 // (otherwise we would have matched the BGTZ instruction from the earlier
598 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
600 // BGTZALC if rs == 0 && rt != 0
601 // BLTZALC if rs != 0 && rs == rt
602 // BLTUC if rs != 0 && rs != rt
604 InsnType Rs = fieldFromInstruction(insn, 21, 5);
605 InsnType Rt = fieldFromInstruction(insn, 16, 5);
606 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
611 MI.setOpcode(Mips::BGTZ);
613 } else if (Rs == 0) {
614 MI.setOpcode(Mips::BGTZALC);
616 } else if (Rs == Rt) {
617 MI.setOpcode(Mips::BLTZALC);
620 return MCDisassembler::Fail; // BLTUC not implemented yet
623 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
627 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
630 MI.addOperand(MCOperand::CreateImm(Imm));
632 return MCDisassembler::Success;
635 template <typename InsnType>
636 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
638 const void *Decoder) {
639 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
640 // (otherwise we would have matched the BLEZL instruction from the earlier
644 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
645 // Invalid if rs == 0
646 // BLEZALC if rs == 0 && rt != 0
647 // BGEZALC if rs == rt && rt != 0
648 // BGEUC if rs != rt && rs != 0 && rt != 0
650 InsnType Rs = fieldFromInstruction(insn, 21, 5);
651 InsnType Rt = fieldFromInstruction(insn, 16, 5);
652 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
656 return MCDisassembler::Fail;
658 MI.setOpcode(Mips::BLEZALC);
660 MI.setOpcode(Mips::BGEZALC);
663 MI.setOpcode(Mips::BGEUC);
667 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
669 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
672 MI.addOperand(MCOperand::CreateImm(Imm));
674 return MCDisassembler::Success;
677 /// readInstruction - read four bytes from the MemoryObject
678 /// and return 32 bit word sorted according to the given endianess
679 static DecodeStatus readInstruction32(const MemoryObject ®ion,
687 // We want to read exactly 4 Bytes of data.
688 if (region.readBytes(address, 4, Bytes) == -1) {
690 return MCDisassembler::Fail;
694 // Encoded as a big-endian 32-bit word in the stream.
695 insn = (Bytes[3] << 0) |
701 // Encoded as a small-endian 32-bit word in the stream.
702 // Little-endian byte ordering:
703 // mips32r2: 4 | 3 | 2 | 1
704 // microMIPS: 2 | 1 | 4 | 3
706 insn = (Bytes[2] << 0) |
711 insn = (Bytes[0] << 0) |
718 return MCDisassembler::Success;
722 MipsDisassembler::getInstruction(MCInst &instr,
724 const MemoryObject &Region,
726 raw_ostream &vStream,
727 raw_ostream &cStream) const {
730 DecodeStatus Result = readInstruction32(Region, Address, Size,
731 Insn, isBigEndian, IsMicroMips);
732 if (Result == MCDisassembler::Fail)
733 return MCDisassembler::Fail;
736 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
737 // Calling the auto-generated decoder function.
738 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
740 if (Result != MCDisassembler::Fail) {
744 return MCDisassembler::Fail;
747 if (isMips32r6() && isGP64()) {
748 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
749 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
751 if (Result != MCDisassembler::Fail) {
758 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
759 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
761 if (Result != MCDisassembler::Fail) {
767 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
768 // Calling the auto-generated decoder function.
769 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
771 if (Result != MCDisassembler::Fail) {
776 return MCDisassembler::Fail;
780 Mips64Disassembler::getInstruction(MCInst &instr,
782 const MemoryObject &Region,
784 raw_ostream &vStream,
785 raw_ostream &cStream) const {
788 DecodeStatus Result = readInstruction32(Region, Address, Size,
789 Insn, isBigEndian, false);
790 if (Result == MCDisassembler::Fail)
791 return MCDisassembler::Fail;
793 // Calling the auto-generated decoder function.
794 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
796 if (Result != MCDisassembler::Fail) {
800 // If we fail to decode in Mips64 decoder space we can try in Mips32
801 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
803 if (Result != MCDisassembler::Fail) {
808 return MCDisassembler::Fail;
811 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
814 const void *Decoder) {
816 return MCDisassembler::Fail;
820 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
823 const void *Decoder) {
826 return MCDisassembler::Fail;
828 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
829 Inst.addOperand(MCOperand::CreateReg(Reg));
830 return MCDisassembler::Success;
833 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
836 const void *Decoder) {
838 return MCDisassembler::Fail;
839 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
840 Inst.addOperand(MCOperand::CreateReg(Reg));
841 return MCDisassembler::Success;
844 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
847 const void *Decoder) {
848 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
849 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
851 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
854 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
857 const void *Decoder) {
858 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
861 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
864 const void *Decoder) {
866 return MCDisassembler::Fail;
868 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
869 Inst.addOperand(MCOperand::CreateReg(Reg));
870 return MCDisassembler::Success;
873 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
876 const void *Decoder) {
878 return MCDisassembler::Fail;
880 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
881 Inst.addOperand(MCOperand::CreateReg(Reg));
882 return MCDisassembler::Success;
885 static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
888 const void *Decoder) {
890 return MCDisassembler::Fail;
892 unsigned Reg = getReg(Decoder, Mips::FGRH32RegClassID, RegNo);
893 Inst.addOperand(MCOperand::CreateReg(Reg));
894 return MCDisassembler::Success;
897 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
900 const void *Decoder) {
902 return MCDisassembler::Fail;
903 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
904 Inst.addOperand(MCOperand::CreateReg(Reg));
905 return MCDisassembler::Success;
908 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
911 const void *Decoder) {
913 return MCDisassembler::Fail;
914 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
915 Inst.addOperand(MCOperand::CreateReg(Reg));
916 return MCDisassembler::Success;
919 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
921 const void *Decoder) {
923 return MCDisassembler::Fail;
925 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
926 Inst.addOperand(MCOperand::CreateReg(Reg));
927 return MCDisassembler::Success;
930 static DecodeStatus DecodeMem(MCInst &Inst,
933 const void *Decoder) {
934 int Offset = SignExtend32<16>(Insn & 0xffff);
935 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
936 unsigned Base = fieldFromInstruction(Insn, 21, 5);
938 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
939 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
941 if(Inst.getOpcode() == Mips::SC){
942 Inst.addOperand(MCOperand::CreateReg(Reg));
945 Inst.addOperand(MCOperand::CreateReg(Reg));
946 Inst.addOperand(MCOperand::CreateReg(Base));
947 Inst.addOperand(MCOperand::CreateImm(Offset));
949 return MCDisassembler::Success;
952 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
953 uint64_t Address, const void *Decoder) {
954 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
955 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
956 unsigned Base = fieldFromInstruction(Insn, 11, 5);
958 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
959 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
961 Inst.addOperand(MCOperand::CreateReg(Reg));
962 Inst.addOperand(MCOperand::CreateReg(Base));
964 // The immediate field of an LD/ST instruction is scaled which means it must
965 // be multiplied (when decoding) by the size (in bytes) of the instructions'
971 switch(Inst.getOpcode())
974 assert (0 && "Unexpected instruction");
975 return MCDisassembler::Fail;
979 Inst.addOperand(MCOperand::CreateImm(Offset));
983 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
987 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
991 Inst.addOperand(MCOperand::CreateImm(Offset << 3));
995 return MCDisassembler::Success;
998 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1001 const void *Decoder) {
1002 int Offset = SignExtend32<12>(Insn & 0x0fff);
1003 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1004 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1006 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1007 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1009 if (Inst.getOpcode() == Mips::SC_MM)
1010 Inst.addOperand(MCOperand::CreateReg(Reg));
1012 Inst.addOperand(MCOperand::CreateReg(Reg));
1013 Inst.addOperand(MCOperand::CreateReg(Base));
1014 Inst.addOperand(MCOperand::CreateImm(Offset));
1016 return MCDisassembler::Success;
1019 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1022 const void *Decoder) {
1023 int Offset = SignExtend32<16>(Insn & 0xffff);
1024 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1025 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1027 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1028 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1030 Inst.addOperand(MCOperand::CreateReg(Reg));
1031 Inst.addOperand(MCOperand::CreateReg(Base));
1032 Inst.addOperand(MCOperand::CreateImm(Offset));
1034 return MCDisassembler::Success;
1037 static DecodeStatus DecodeFMem(MCInst &Inst,
1040 const void *Decoder) {
1041 int Offset = SignExtend32<16>(Insn & 0xffff);
1042 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1043 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1045 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1046 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1048 Inst.addOperand(MCOperand::CreateReg(Reg));
1049 Inst.addOperand(MCOperand::CreateReg(Base));
1050 Inst.addOperand(MCOperand::CreateImm(Offset));
1052 return MCDisassembler::Success;
1056 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1059 const void *Decoder) {
1060 // Currently only hardware register 29 is supported.
1062 return MCDisassembler::Fail;
1063 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1064 return MCDisassembler::Success;
1067 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1070 const void *Decoder) {
1071 if (RegNo > 30 || RegNo %2)
1072 return MCDisassembler::Fail;
1075 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1076 Inst.addOperand(MCOperand::CreateReg(Reg));
1077 return MCDisassembler::Success;
1080 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1083 const void *Decoder) {
1085 return MCDisassembler::Fail;
1087 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1088 Inst.addOperand(MCOperand::CreateReg(Reg));
1089 return MCDisassembler::Success;
1092 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1095 const void *Decoder) {
1097 return MCDisassembler::Fail;
1099 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1100 Inst.addOperand(MCOperand::CreateReg(Reg));
1101 return MCDisassembler::Success;
1104 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1107 const void *Decoder) {
1109 return MCDisassembler::Fail;
1111 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1112 Inst.addOperand(MCOperand::CreateReg(Reg));
1113 return MCDisassembler::Success;
1116 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1119 const void *Decoder) {
1121 return MCDisassembler::Fail;
1123 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1124 Inst.addOperand(MCOperand::CreateReg(Reg));
1125 return MCDisassembler::Success;
1128 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1131 const void *Decoder) {
1133 return MCDisassembler::Fail;
1135 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1136 Inst.addOperand(MCOperand::CreateReg(Reg));
1137 return MCDisassembler::Success;
1140 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1143 const void *Decoder) {
1145 return MCDisassembler::Fail;
1147 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1148 Inst.addOperand(MCOperand::CreateReg(Reg));
1149 return MCDisassembler::Success;
1152 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1155 const void *Decoder) {
1157 return MCDisassembler::Fail;
1159 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1160 Inst.addOperand(MCOperand::CreateReg(Reg));
1161 return MCDisassembler::Success;
1164 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1167 const void *Decoder) {
1169 return MCDisassembler::Fail;
1171 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1172 Inst.addOperand(MCOperand::CreateReg(Reg));
1173 return MCDisassembler::Success;
1176 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1179 const void *Decoder) {
1181 return MCDisassembler::Fail;
1183 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1184 Inst.addOperand(MCOperand::CreateReg(Reg));
1185 return MCDisassembler::Success;
1188 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1191 const void *Decoder) {
1192 int32_t BranchOffset = (SignExtend32<16>(Offset) << 2) + 4;
1193 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1194 return MCDisassembler::Success;
1197 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1200 const void *Decoder) {
1202 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1203 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1204 return MCDisassembler::Success;
1207 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1210 const void *Decoder) {
1211 int32_t BranchOffset = SignExtend32<21>(Offset) << 2;
1213 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1214 return MCDisassembler::Success;
1217 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1220 const void *Decoder) {
1221 int32_t BranchOffset = SignExtend32<26>(Offset) << 2;
1223 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1224 return MCDisassembler::Success;
1227 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1230 const void *Decoder) {
1231 int32_t BranchOffset = SignExtend32<16>(Offset) << 1;
1232 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1233 return MCDisassembler::Success;
1236 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1239 const void *Decoder) {
1240 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1241 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1242 return MCDisassembler::Success;
1245 static DecodeStatus DecodeSimm16(MCInst &Inst,
1248 const void *Decoder) {
1249 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1250 return MCDisassembler::Success;
1253 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1256 const void *Decoder) {
1257 // We add one to the immediate field as it was encoded as 'imm - 1'.
1258 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1259 return MCDisassembler::Success;
1262 static DecodeStatus DecodeInsSize(MCInst &Inst,
1265 const void *Decoder) {
1266 // First we need to grab the pos(lsb) from MCInst.
1267 int Pos = Inst.getOperand(2).getImm();
1268 int Size = (int) Insn - Pos + 1;
1269 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1270 return MCDisassembler::Success;
1273 static DecodeStatus DecodeExtSize(MCInst &Inst,
1276 const void *Decoder) {
1277 int Size = (int) Insn + 1;
1278 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1279 return MCDisassembler::Success;
1282 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1283 uint64_t Address, const void *Decoder) {
1284 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) << 2));
1285 return MCDisassembler::Success;
1288 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1289 uint64_t Address, const void *Decoder) {
1290 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) << 3));
1291 return MCDisassembler::Success;