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/TargetRegistry.h"
27 #define DEBUG_TYPE "mips-disassembler"
29 typedef MCDisassembler::DecodeStatus DecodeStatus;
33 /// A disassembler class for Mips.
34 class MipsDisassemblerBase : public MCDisassembler {
36 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
38 : MCDisassembler(STI, Ctx),
39 IsGP64Bit(STI.getFeatureBits() & Mips::FeatureGP64Bit),
40 IsBigEndian(IsBigEndian) {}
42 virtual ~MipsDisassemblerBase() {}
44 bool isGP64Bit() const { return IsGP64Bit; }
52 /// A disassembler class for Mips32.
53 class MipsDisassembler : public MipsDisassemblerBase {
56 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
57 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
58 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
61 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
62 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
63 bool hasMips32r6() const {
64 return STI.getFeatureBits() & Mips::FeatureMips32r6;
67 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
69 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
74 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
75 ArrayRef<uint8_t> Bytes, uint64_t Address,
77 raw_ostream &CStream) const override;
80 /// A disassembler class for Mips64.
81 class Mips64Disassembler : public MipsDisassemblerBase {
83 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
85 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
87 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
88 ArrayRef<uint8_t> Bytes, uint64_t Address,
90 raw_ostream &CStream) const override;
93 } // end anonymous namespace
95 // Forward declare these because the autogenerated code will reference them.
96 // Definitions are further down.
97 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
100 const void *Decoder);
102 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
105 const void *Decoder);
107 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
110 const void *Decoder);
112 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
115 const void *Decoder);
117 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
120 const void *Decoder);
122 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
125 const void *Decoder);
127 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
130 const void *Decoder);
132 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
135 const void *Decoder);
137 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
140 const void *Decoder);
142 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
145 const void *Decoder);
147 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
150 const void *Decoder);
152 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
155 const void *Decoder);
157 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
159 const void *Decoder);
161 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
164 const void *Decoder);
166 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
169 const void *Decoder);
171 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
174 const void *Decoder);
176 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
184 const void *Decoder);
186 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
189 const void *Decoder);
191 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
194 const void *Decoder);
196 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
199 const void *Decoder);
201 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
204 const void *Decoder);
206 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
209 const void *Decoder);
211 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
214 const void *Decoder);
216 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
219 const void *Decoder);
221 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
224 const void *Decoder);
226 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
229 const void *Decoder);
231 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
234 const void *Decoder);
236 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
237 // shifted left by 1 bit.
238 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
241 const void *Decoder);
243 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
244 // shifted left by 1 bit.
245 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
248 const void *Decoder);
250 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
251 // shifted left by 1 bit.
252 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
255 const void *Decoder);
257 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
258 // shifted left by 1 bit.
259 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
262 const void *Decoder);
264 static DecodeStatus DecodeMem(MCInst &Inst,
267 const void *Decoder);
269 static DecodeStatus DecodeCacheOp(MCInst &Inst,
272 const void *Decoder);
274 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
277 const void *Decoder);
279 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
282 const void *Decoder);
284 static DecodeStatus DecodeSyncI(MCInst &Inst,
287 const void *Decoder);
289 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
290 uint64_t Address, const void *Decoder);
292 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
295 const void *Decoder);
297 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
305 const void *Decoder);
307 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
310 const void *Decoder);
312 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
315 const void *Decoder);
317 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
320 const void *Decoder);
322 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
324 const void *Decoder);
326 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
328 const void *Decoder);
330 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
332 const void *Decoder);
334 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
336 const void *Decoder);
338 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
341 const void *Decoder);
343 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
346 const void *Decoder);
348 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
351 const void *Decoder);
353 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
356 const void *Decoder);
358 static DecodeStatus DecodeSimm4(MCInst &Inst,
361 const void *Decoder);
363 static DecodeStatus DecodeSimm16(MCInst &Inst,
366 const void *Decoder);
368 // Decode the immediate field of an LSA instruction which
370 static DecodeStatus DecodeLSAImm(MCInst &Inst,
373 const void *Decoder);
375 static DecodeStatus DecodeInsSize(MCInst &Inst,
378 const void *Decoder);
380 static DecodeStatus DecodeExtSize(MCInst &Inst,
383 const void *Decoder);
385 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
386 uint64_t Address, const void *Decoder);
388 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
389 uint64_t Address, const void *Decoder);
391 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
392 uint64_t Address, const void *Decoder);
394 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
395 uint64_t Address, const void *Decoder);
397 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
398 uint64_t Address, const void *Decoder);
400 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
401 uint64_t Address, const void *Decoder);
403 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
405 template <typename InsnType>
406 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
409 template <typename InsnType>
411 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
414 template <typename InsnType>
416 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
419 template <typename InsnType>
421 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
422 const void *Decoder);
424 template <typename InsnType>
426 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
427 const void *Decoder);
429 template <typename InsnType>
431 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
432 const void *Decoder);
434 template <typename InsnType>
436 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
437 const void *Decoder);
439 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
441 const void *Decoder);
443 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
445 const void *Decoder);
447 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
449 const void *Decoder);
452 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
456 static MCDisassembler *createMipsDisassembler(
458 const MCSubtargetInfo &STI,
460 return new MipsDisassembler(STI, Ctx, true);
463 static MCDisassembler *createMipselDisassembler(
465 const MCSubtargetInfo &STI,
467 return new MipsDisassembler(STI, Ctx, false);
470 static MCDisassembler *createMips64Disassembler(
472 const MCSubtargetInfo &STI,
474 return new Mips64Disassembler(STI, Ctx, true);
477 static MCDisassembler *createMips64elDisassembler(
479 const MCSubtargetInfo &STI,
481 return new Mips64Disassembler(STI, Ctx, false);
484 extern "C" void LLVMInitializeMipsDisassembler() {
485 // Register the disassembler.
486 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
487 createMipsDisassembler);
488 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
489 createMipselDisassembler);
490 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
491 createMips64Disassembler);
492 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
493 createMips64elDisassembler);
496 #include "MipsGenDisassemblerTables.inc"
498 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
499 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
500 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
501 return *(RegInfo->getRegClass(RC).begin() + RegNo);
504 template <typename InsnType>
505 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
506 const void *Decoder) {
507 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
508 // The size of the n field depends on the element size
509 // The register class also depends on this.
510 InsnType tmp = fieldFromInstruction(insn, 17, 5);
512 DecodeFN RegDecoder = nullptr;
513 if ((tmp & 0x18) == 0x00) { // INSVE_B
515 RegDecoder = DecodeMSA128BRegisterClass;
516 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
518 RegDecoder = DecodeMSA128HRegisterClass;
519 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
521 RegDecoder = DecodeMSA128WRegisterClass;
522 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
524 RegDecoder = DecodeMSA128DRegisterClass;
526 llvm_unreachable("Invalid encoding");
528 assert(NSize != 0 && RegDecoder != nullptr);
531 tmp = fieldFromInstruction(insn, 6, 5);
532 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
533 return MCDisassembler::Fail;
535 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
536 return MCDisassembler::Fail;
538 tmp = fieldFromInstruction(insn, 16, NSize);
539 MI.addOperand(MCOperand::CreateImm(tmp));
541 tmp = fieldFromInstruction(insn, 11, 5);
542 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
543 return MCDisassembler::Fail;
545 MI.addOperand(MCOperand::CreateImm(0));
547 return MCDisassembler::Success;
550 template <typename InsnType>
551 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
553 const void *Decoder) {
554 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
555 // (otherwise we would have matched the ADDI instruction from the earlier
559 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
561 // BEQZALC if rs == 0 && rt != 0
562 // BEQC if rs < rt && rs != 0
564 InsnType Rs = fieldFromInstruction(insn, 21, 5);
565 InsnType Rt = fieldFromInstruction(insn, 16, 5);
566 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
570 MI.setOpcode(Mips::BOVC);
572 } else if (Rs != 0 && Rs < Rt) {
573 MI.setOpcode(Mips::BEQC);
576 MI.setOpcode(Mips::BEQZALC);
579 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
582 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 DecodeDaddiGroupBranch(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 ADDI instruction from the earlier
598 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
600 // BNEZALC if rs == 0 && rt != 0
601 // BNEC if rs < rt && rs != 0
603 InsnType Rs = fieldFromInstruction(insn, 21, 5);
604 InsnType Rt = fieldFromInstruction(insn, 16, 5);
605 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
609 MI.setOpcode(Mips::BNVC);
611 } else if (Rs != 0 && Rs < Rt) {
612 MI.setOpcode(Mips::BNEC);
615 MI.setOpcode(Mips::BNEZALC);
618 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
621 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
623 MI.addOperand(MCOperand::CreateImm(Imm));
625 return MCDisassembler::Success;
628 template <typename InsnType>
629 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
631 const void *Decoder) {
632 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
633 // (otherwise we would have matched the BLEZL instruction from the earlier
637 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
638 // Invalid if rs == 0
639 // BLEZC if rs == 0 && rt != 0
640 // BGEZC if rs == rt && rt != 0
641 // BGEC if rs != rt && rs != 0 && rt != 0
643 InsnType Rs = fieldFromInstruction(insn, 21, 5);
644 InsnType Rt = fieldFromInstruction(insn, 16, 5);
645 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
649 return MCDisassembler::Fail;
651 MI.setOpcode(Mips::BLEZC);
653 MI.setOpcode(Mips::BGEZC);
656 MI.setOpcode(Mips::BGEC);
660 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
663 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
666 MI.addOperand(MCOperand::CreateImm(Imm));
668 return MCDisassembler::Success;
671 template <typename InsnType>
672 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
674 const void *Decoder) {
675 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
676 // (otherwise we would have matched the BGTZL instruction from the earlier
680 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
681 // Invalid if rs == 0
682 // BGTZC if rs == 0 && rt != 0
683 // BLTZC if rs == rt && rt != 0
684 // BLTC if rs != rt && rs != 0 && rt != 0
688 InsnType Rs = fieldFromInstruction(insn, 21, 5);
689 InsnType Rt = fieldFromInstruction(insn, 16, 5);
690 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
693 return MCDisassembler::Fail;
695 MI.setOpcode(Mips::BGTZC);
697 MI.setOpcode(Mips::BLTZC);
699 MI.setOpcode(Mips::BLTC);
704 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
707 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
710 MI.addOperand(MCOperand::CreateImm(Imm));
712 return MCDisassembler::Success;
715 template <typename InsnType>
716 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
718 const void *Decoder) {
719 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
720 // (otherwise we would have matched the BGTZ instruction from the earlier
724 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
726 // BGTZALC if rs == 0 && rt != 0
727 // BLTZALC if rs != 0 && rs == rt
728 // BLTUC if rs != 0 && rs != rt
730 InsnType Rs = fieldFromInstruction(insn, 21, 5);
731 InsnType Rt = fieldFromInstruction(insn, 16, 5);
732 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
737 MI.setOpcode(Mips::BGTZ);
739 } else if (Rs == 0) {
740 MI.setOpcode(Mips::BGTZALC);
742 } else if (Rs == Rt) {
743 MI.setOpcode(Mips::BLTZALC);
746 MI.setOpcode(Mips::BLTUC);
752 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
756 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
759 MI.addOperand(MCOperand::CreateImm(Imm));
761 return MCDisassembler::Success;
764 template <typename InsnType>
765 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
767 const void *Decoder) {
768 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
769 // (otherwise we would have matched the BLEZL instruction from the earlier
773 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
774 // Invalid if rs == 0
775 // BLEZALC if rs == 0 && rt != 0
776 // BGEZALC if rs == rt && rt != 0
777 // BGEUC if rs != rt && rs != 0 && rt != 0
779 InsnType Rs = fieldFromInstruction(insn, 21, 5);
780 InsnType Rt = fieldFromInstruction(insn, 16, 5);
781 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
785 return MCDisassembler::Fail;
787 MI.setOpcode(Mips::BLEZALC);
789 MI.setOpcode(Mips::BGEZALC);
792 MI.setOpcode(Mips::BGEUC);
796 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
798 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
801 MI.addOperand(MCOperand::CreateImm(Imm));
803 return MCDisassembler::Success;
806 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
807 /// according to the given endianess.
808 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
809 uint64_t &Size, uint32_t &Insn,
811 // We want to read exactly 2 Bytes of data.
812 if (Bytes.size() < 2) {
814 return MCDisassembler::Fail;
818 Insn = (Bytes[0] << 8) | Bytes[1];
820 Insn = (Bytes[1] << 8) | Bytes[0];
823 return MCDisassembler::Success;
826 /// Read four bytes from the ArrayRef and return 32 bit word sorted
827 /// according to the given endianess
828 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
829 uint64_t &Size, uint32_t &Insn,
830 bool IsBigEndian, bool IsMicroMips) {
831 // We want to read exactly 4 Bytes of data.
832 if (Bytes.size() < 4) {
834 return MCDisassembler::Fail;
837 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
838 // always precede the low 16 bits in the instruction stream (that is, they
839 // are placed at lower addresses in the instruction stream).
841 // microMIPS byte ordering:
842 // Big-endian: 0 | 1 | 2 | 3
843 // Little-endian: 1 | 0 | 3 | 2
846 // Encoded as a big-endian 32-bit word in the stream.
848 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
851 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
854 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
859 return MCDisassembler::Success;
862 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
863 ArrayRef<uint8_t> Bytes,
865 raw_ostream &VStream,
866 raw_ostream &CStream) const {
871 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
873 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
874 // Calling the auto-generated decoder function.
875 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
877 if (Result != MCDisassembler::Fail) {
882 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
883 if (Result == MCDisassembler::Fail)
884 return MCDisassembler::Fail;
886 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
887 // Calling the auto-generated decoder function.
888 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
890 if (Result != MCDisassembler::Fail) {
894 return MCDisassembler::Fail;
897 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
898 if (Result == MCDisassembler::Fail)
899 return MCDisassembler::Fail;
902 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
904 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
905 if (Result != MCDisassembler::Fail) {
911 if (hasMips32r6() && isGP64()) {
912 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
913 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
915 if (Result != MCDisassembler::Fail) {
922 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
923 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
925 if (Result != MCDisassembler::Fail) {
931 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
932 // Calling the auto-generated decoder function.
934 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
935 if (Result != MCDisassembler::Fail) {
940 return MCDisassembler::Fail;
943 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
944 ArrayRef<uint8_t> Bytes,
946 raw_ostream &VStream,
947 raw_ostream &CStream) const {
950 DecodeStatus Result =
951 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
952 if (Result == MCDisassembler::Fail)
953 return MCDisassembler::Fail;
955 // Calling the auto-generated decoder function.
957 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
958 if (Result != MCDisassembler::Fail) {
962 // If we fail to decode in Mips64 decoder space we can try in Mips32
964 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
965 if (Result != MCDisassembler::Fail) {
970 return MCDisassembler::Fail;
973 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
976 const void *Decoder) {
978 return MCDisassembler::Fail;
982 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
985 const void *Decoder) {
988 return MCDisassembler::Fail;
990 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
991 Inst.addOperand(MCOperand::CreateReg(Reg));
992 return MCDisassembler::Success;
995 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
998 const void *Decoder) {
1000 return MCDisassembler::Fail;
1001 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1002 Inst.addOperand(MCOperand::CreateReg(Reg));
1003 return MCDisassembler::Success;
1006 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1009 const void *Decoder) {
1011 return MCDisassembler::Fail;
1012 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1013 Inst.addOperand(MCOperand::CreateReg(Reg));
1014 return MCDisassembler::Success;
1017 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1020 const void *Decoder) {
1022 return MCDisassembler::Fail;
1023 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1024 Inst.addOperand(MCOperand::CreateReg(Reg));
1025 return MCDisassembler::Success;
1028 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1031 const void *Decoder) {
1033 return MCDisassembler::Fail;
1034 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1035 Inst.addOperand(MCOperand::CreateReg(Reg));
1036 return MCDisassembler::Success;
1039 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1042 const void *Decoder) {
1043 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
1044 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1046 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1049 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1052 const void *Decoder) {
1053 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1056 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1059 const void *Decoder) {
1061 return MCDisassembler::Fail;
1063 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1064 Inst.addOperand(MCOperand::CreateReg(Reg));
1065 return MCDisassembler::Success;
1068 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1071 const void *Decoder) {
1073 return MCDisassembler::Fail;
1075 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1076 Inst.addOperand(MCOperand::CreateReg(Reg));
1077 return MCDisassembler::Success;
1080 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1083 const void *Decoder) {
1085 return MCDisassembler::Fail;
1086 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1087 Inst.addOperand(MCOperand::CreateReg(Reg));
1088 return MCDisassembler::Success;
1091 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1094 const void *Decoder) {
1096 return MCDisassembler::Fail;
1097 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1098 Inst.addOperand(MCOperand::CreateReg(Reg));
1099 return MCDisassembler::Success;
1102 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1104 const void *Decoder) {
1106 return MCDisassembler::Fail;
1108 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1109 Inst.addOperand(MCOperand::CreateReg(Reg));
1110 return MCDisassembler::Success;
1113 static DecodeStatus DecodeMem(MCInst &Inst,
1116 const void *Decoder) {
1117 int Offset = SignExtend32<16>(Insn & 0xffff);
1118 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1119 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1121 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1122 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1124 if(Inst.getOpcode() == Mips::SC ||
1125 Inst.getOpcode() == Mips::SCD){
1126 Inst.addOperand(MCOperand::CreateReg(Reg));
1129 Inst.addOperand(MCOperand::CreateReg(Reg));
1130 Inst.addOperand(MCOperand::CreateReg(Base));
1131 Inst.addOperand(MCOperand::CreateImm(Offset));
1133 return MCDisassembler::Success;
1136 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1139 const void *Decoder) {
1140 int Offset = SignExtend32<16>(Insn & 0xffff);
1141 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1142 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1144 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1146 Inst.addOperand(MCOperand::CreateReg(Base));
1147 Inst.addOperand(MCOperand::CreateImm(Offset));
1148 Inst.addOperand(MCOperand::CreateImm(Hint));
1150 return MCDisassembler::Success;
1153 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1156 const void *Decoder) {
1157 int Offset = SignExtend32<12>(Insn & 0xfff);
1158 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1159 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1161 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1163 Inst.addOperand(MCOperand::CreateReg(Base));
1164 Inst.addOperand(MCOperand::CreateImm(Offset));
1165 Inst.addOperand(MCOperand::CreateImm(Hint));
1167 return MCDisassembler::Success;
1170 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
1173 const void *Decoder) {
1174 int Offset = fieldFromInstruction(Insn, 7, 9);
1175 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1176 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1178 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1180 Inst.addOperand(MCOperand::CreateReg(Base));
1181 Inst.addOperand(MCOperand::CreateImm(Offset));
1182 Inst.addOperand(MCOperand::CreateImm(Hint));
1184 return MCDisassembler::Success;
1187 static DecodeStatus DecodeSyncI(MCInst &Inst,
1190 const void *Decoder) {
1191 int Offset = SignExtend32<16>(Insn & 0xffff);
1192 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1194 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1196 Inst.addOperand(MCOperand::CreateReg(Base));
1197 Inst.addOperand(MCOperand::CreateImm(Offset));
1199 return MCDisassembler::Success;
1202 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1203 uint64_t Address, const void *Decoder) {
1204 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1205 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1206 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1208 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1209 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1211 Inst.addOperand(MCOperand::CreateReg(Reg));
1212 Inst.addOperand(MCOperand::CreateReg(Base));
1214 // The immediate field of an LD/ST instruction is scaled which means it must
1215 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1221 switch(Inst.getOpcode())
1224 assert (0 && "Unexpected instruction");
1225 return MCDisassembler::Fail;
1229 Inst.addOperand(MCOperand::CreateImm(Offset));
1233 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1237 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1241 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1245 return MCDisassembler::Success;
1248 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1251 const void *Decoder) {
1252 unsigned Offset = Insn & 0xf;
1253 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1254 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1256 switch (Inst.getOpcode()) {
1257 case Mips::LBU16_MM:
1258 case Mips::LHU16_MM:
1260 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1261 == MCDisassembler::Fail)
1262 return MCDisassembler::Fail;
1267 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1268 == MCDisassembler::Fail)
1269 return MCDisassembler::Fail;
1273 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1274 == MCDisassembler::Fail)
1275 return MCDisassembler::Fail;
1277 switch (Inst.getOpcode()) {
1278 case Mips::LBU16_MM:
1280 Inst.addOperand(MCOperand::CreateImm(-1));
1282 Inst.addOperand(MCOperand::CreateImm(Offset));
1285 Inst.addOperand(MCOperand::CreateImm(Offset));
1287 case Mips::LHU16_MM:
1289 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1293 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1297 return MCDisassembler::Success;
1300 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1303 const void *Decoder) {
1304 unsigned Offset = Insn & 0x1F;
1305 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1307 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1309 Inst.addOperand(MCOperand::CreateReg(Reg));
1310 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1311 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1313 return MCDisassembler::Success;
1316 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1319 const void *Decoder) {
1320 unsigned Offset = Insn & 0x7F;
1321 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1323 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1325 Inst.addOperand(MCOperand::CreateReg(Reg));
1326 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1327 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1329 return MCDisassembler::Success;
1332 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1335 const void *Decoder) {
1336 int Offset = SignExtend32<4>(Insn & 0xf);
1338 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1339 == MCDisassembler::Fail)
1340 return MCDisassembler::Fail;
1342 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1343 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1345 return MCDisassembler::Success;
1348 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1351 const void *Decoder) {
1352 int Offset = SignExtend32<12>(Insn & 0x0fff);
1353 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1354 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1356 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1357 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1359 switch (Inst.getOpcode()) {
1360 case Mips::SWM32_MM:
1361 case Mips::LWM32_MM:
1362 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1363 == MCDisassembler::Fail)
1364 return MCDisassembler::Fail;
1365 Inst.addOperand(MCOperand::CreateReg(Base));
1366 Inst.addOperand(MCOperand::CreateImm(Offset));
1369 Inst.addOperand(MCOperand::CreateReg(Reg));
1372 Inst.addOperand(MCOperand::CreateReg(Reg));
1373 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1374 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1376 Inst.addOperand(MCOperand::CreateReg(Base));
1377 Inst.addOperand(MCOperand::CreateImm(Offset));
1380 return MCDisassembler::Success;
1383 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1386 const void *Decoder) {
1387 int Offset = SignExtend32<16>(Insn & 0xffff);
1388 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1389 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1391 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1392 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1394 Inst.addOperand(MCOperand::CreateReg(Reg));
1395 Inst.addOperand(MCOperand::CreateReg(Base));
1396 Inst.addOperand(MCOperand::CreateImm(Offset));
1398 return MCDisassembler::Success;
1401 static DecodeStatus DecodeFMem(MCInst &Inst,
1404 const void *Decoder) {
1405 int Offset = SignExtend32<16>(Insn & 0xffff);
1406 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1407 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1409 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1410 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1412 Inst.addOperand(MCOperand::CreateReg(Reg));
1413 Inst.addOperand(MCOperand::CreateReg(Base));
1414 Inst.addOperand(MCOperand::CreateImm(Offset));
1416 return MCDisassembler::Success;
1419 static DecodeStatus DecodeFMem2(MCInst &Inst,
1422 const void *Decoder) {
1423 int Offset = SignExtend32<16>(Insn & 0xffff);
1424 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1425 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1427 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1428 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1430 Inst.addOperand(MCOperand::CreateReg(Reg));
1431 Inst.addOperand(MCOperand::CreateReg(Base));
1432 Inst.addOperand(MCOperand::CreateImm(Offset));
1434 return MCDisassembler::Success;
1437 static DecodeStatus DecodeFMem3(MCInst &Inst,
1440 const void *Decoder) {
1441 int Offset = SignExtend32<16>(Insn & 0xffff);
1442 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1443 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1445 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1446 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1448 Inst.addOperand(MCOperand::CreateReg(Reg));
1449 Inst.addOperand(MCOperand::CreateReg(Base));
1450 Inst.addOperand(MCOperand::CreateImm(Offset));
1452 return MCDisassembler::Success;
1455 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1458 const void *Decoder) {
1459 int Offset = SignExtend32<11>(Insn & 0x07ff);
1460 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1461 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1463 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1464 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1466 Inst.addOperand(MCOperand::CreateReg(Reg));
1467 Inst.addOperand(MCOperand::CreateReg(Base));
1468 Inst.addOperand(MCOperand::CreateImm(Offset));
1470 return MCDisassembler::Success;
1472 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1475 const void *Decoder) {
1476 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1477 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1478 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1480 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1481 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1483 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1484 Inst.addOperand(MCOperand::CreateReg(Rt));
1487 Inst.addOperand(MCOperand::CreateReg(Rt));
1488 Inst.addOperand(MCOperand::CreateReg(Base));
1489 Inst.addOperand(MCOperand::CreateImm(Offset));
1491 return MCDisassembler::Success;
1494 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1497 const void *Decoder) {
1498 // Currently only hardware register 29 is supported.
1500 return MCDisassembler::Fail;
1501 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1502 return MCDisassembler::Success;
1505 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1508 const void *Decoder) {
1509 if (RegNo > 30 || RegNo %2)
1510 return MCDisassembler::Fail;
1513 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1514 Inst.addOperand(MCOperand::CreateReg(Reg));
1515 return MCDisassembler::Success;
1518 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1521 const void *Decoder) {
1523 return MCDisassembler::Fail;
1525 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1526 Inst.addOperand(MCOperand::CreateReg(Reg));
1527 return MCDisassembler::Success;
1530 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1533 const void *Decoder) {
1535 return MCDisassembler::Fail;
1537 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1538 Inst.addOperand(MCOperand::CreateReg(Reg));
1539 return MCDisassembler::Success;
1542 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1545 const void *Decoder) {
1547 return MCDisassembler::Fail;
1549 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1550 Inst.addOperand(MCOperand::CreateReg(Reg));
1551 return MCDisassembler::Success;
1554 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1557 const void *Decoder) {
1559 return MCDisassembler::Fail;
1561 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1562 Inst.addOperand(MCOperand::CreateReg(Reg));
1563 return MCDisassembler::Success;
1566 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1569 const void *Decoder) {
1571 return MCDisassembler::Fail;
1573 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1574 Inst.addOperand(MCOperand::CreateReg(Reg));
1575 return MCDisassembler::Success;
1578 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1581 const void *Decoder) {
1583 return MCDisassembler::Fail;
1585 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1586 Inst.addOperand(MCOperand::CreateReg(Reg));
1587 return MCDisassembler::Success;
1590 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1593 const void *Decoder) {
1595 return MCDisassembler::Fail;
1597 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1598 Inst.addOperand(MCOperand::CreateReg(Reg));
1599 return MCDisassembler::Success;
1602 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1605 const void *Decoder) {
1607 return MCDisassembler::Fail;
1609 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1610 Inst.addOperand(MCOperand::CreateReg(Reg));
1611 return MCDisassembler::Success;
1614 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1617 const void *Decoder) {
1619 return MCDisassembler::Fail;
1621 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1622 Inst.addOperand(MCOperand::CreateReg(Reg));
1623 return MCDisassembler::Success;
1626 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1629 const void *Decoder) {
1630 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1631 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1632 return MCDisassembler::Success;
1635 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1638 const void *Decoder) {
1640 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1641 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1642 return MCDisassembler::Success;
1645 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1648 const void *Decoder) {
1649 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1651 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1652 return MCDisassembler::Success;
1655 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1658 const void *Decoder) {
1659 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1661 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1662 return MCDisassembler::Success;
1665 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1668 const void *Decoder) {
1669 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1670 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1671 return MCDisassembler::Success;
1674 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1677 const void *Decoder) {
1678 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1679 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1680 return MCDisassembler::Success;
1683 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1686 const void *Decoder) {
1687 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1688 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1689 return MCDisassembler::Success;
1692 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1695 const void *Decoder) {
1696 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1697 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1698 return MCDisassembler::Success;
1701 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1704 const void *Decoder) {
1706 Inst.addOperand(MCOperand::CreateImm(1));
1707 else if (Value == 0x7)
1708 Inst.addOperand(MCOperand::CreateImm(-1));
1710 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1711 return MCDisassembler::Success;
1714 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1717 const void *Decoder) {
1718 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1719 return MCDisassembler::Success;
1722 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1725 const void *Decoder) {
1727 Inst.addOperand(MCOperand::CreateImm(-1));
1729 Inst.addOperand(MCOperand::CreateImm(Value));
1730 return MCDisassembler::Success;
1733 static DecodeStatus DecodeSimm4(MCInst &Inst,
1736 const void *Decoder) {
1737 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1738 return MCDisassembler::Success;
1741 static DecodeStatus DecodeSimm16(MCInst &Inst,
1744 const void *Decoder) {
1745 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1746 return MCDisassembler::Success;
1749 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1752 const void *Decoder) {
1753 // We add one to the immediate field as it was encoded as 'imm - 1'.
1754 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1755 return MCDisassembler::Success;
1758 static DecodeStatus DecodeInsSize(MCInst &Inst,
1761 const void *Decoder) {
1762 // First we need to grab the pos(lsb) from MCInst.
1763 int Pos = Inst.getOperand(2).getImm();
1764 int Size = (int) Insn - Pos + 1;
1765 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1766 return MCDisassembler::Success;
1769 static DecodeStatus DecodeExtSize(MCInst &Inst,
1772 const void *Decoder) {
1773 int Size = (int) Insn + 1;
1774 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1775 return MCDisassembler::Success;
1778 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1779 uint64_t Address, const void *Decoder) {
1780 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1781 return MCDisassembler::Success;
1784 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1785 uint64_t Address, const void *Decoder) {
1786 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1787 return MCDisassembler::Success;
1790 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1791 uint64_t Address, const void *Decoder) {
1792 int32_t DecodedValue;
1794 case 0: DecodedValue = 256; break;
1795 case 1: DecodedValue = 257; break;
1796 case 510: DecodedValue = -258; break;
1797 case 511: DecodedValue = -257; break;
1798 default: DecodedValue = SignExtend32<9>(Insn); break;
1800 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
1801 return MCDisassembler::Success;
1804 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1805 uint64_t Address, const void *Decoder) {
1806 // Insn must be >= 0, since it is unsigned that condition is always true.
1808 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1810 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1811 return MCDisassembler::Success;
1814 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1815 uint64_t Address, const void *Decoder) {
1816 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1817 return MCDisassembler::Success;
1820 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1823 const void *Decoder) {
1824 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1825 Mips::S6, Mips::FP};
1828 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1829 // Empty register lists are not allowed.
1831 return MCDisassembler::Fail;
1833 RegNum = RegLst & 0xf;
1834 for (unsigned i = 0; i < RegNum; i++)
1835 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1838 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1840 return MCDisassembler::Success;
1843 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1845 const void *Decoder) {
1846 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1847 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1848 unsigned RegNum = RegLst & 0x3;
1850 for (unsigned i = 0; i <= RegNum; i++)
1851 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1853 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1855 return MCDisassembler::Success;
1858 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
1859 uint64_t Address, const void *Decoder) {
1861 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
1865 return MCDisassembler::Fail;
1867 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1868 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1871 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1872 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1875 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1876 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1879 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1880 Inst.addOperand(MCOperand::CreateReg(Mips::S5));
1883 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1884 Inst.addOperand(MCOperand::CreateReg(Mips::S6));
1887 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1888 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1891 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1892 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1895 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1896 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1900 return MCDisassembler::Success;
1903 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1904 uint64_t Address, const void *Decoder) {
1905 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1906 return MCDisassembler::Success;