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 DecodeGPR32RegisterClass(MCInst &Inst,
120 const void *Decoder);
122 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
125 const void *Decoder);
127 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
130 const void *Decoder);
132 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
135 const void *Decoder);
137 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
140 const void *Decoder);
142 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
145 const void *Decoder);
147 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
150 const void *Decoder);
152 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
154 const void *Decoder);
156 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
159 const void *Decoder);
161 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
164 const void *Decoder);
166 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
169 const void *Decoder);
171 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
174 const void *Decoder);
176 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
184 const void *Decoder);
186 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
189 const void *Decoder);
191 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
194 const void *Decoder);
196 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
199 const void *Decoder);
201 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
204 const void *Decoder);
206 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
209 const void *Decoder);
211 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
214 const void *Decoder);
216 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
219 const void *Decoder);
221 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
224 const void *Decoder);
226 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
229 const void *Decoder);
231 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
232 // shifted left by 1 bit.
233 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
236 const void *Decoder);
238 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
239 // shifted left by 1 bit.
240 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
243 const void *Decoder);
245 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
246 // shifted left by 1 bit.
247 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
250 const void *Decoder);
252 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
253 // shifted left by 1 bit.
254 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
257 const void *Decoder);
259 static DecodeStatus DecodeMem(MCInst &Inst,
262 const void *Decoder);
264 static DecodeStatus DecodeCacheOp(MCInst &Inst,
267 const void *Decoder);
269 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
272 const void *Decoder);
274 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
277 const void *Decoder);
279 static DecodeStatus DecodeSyncI(MCInst &Inst,
282 const void *Decoder);
284 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
285 uint64_t Address, const void *Decoder);
287 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
290 const void *Decoder);
292 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
295 const void *Decoder);
297 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
305 const void *Decoder);
307 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
310 const void *Decoder);
312 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
315 const void *Decoder);
317 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
319 const void *Decoder);
321 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
323 const void *Decoder);
325 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
327 const void *Decoder);
329 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
331 const void *Decoder);
333 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
336 const void *Decoder);
338 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
341 const void *Decoder);
343 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
346 const void *Decoder);
348 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
351 const void *Decoder);
353 static DecodeStatus DecodeSimm4(MCInst &Inst,
356 const void *Decoder);
358 static DecodeStatus DecodeSimm16(MCInst &Inst,
361 const void *Decoder);
363 // Decode the immediate field of an LSA instruction which
365 static DecodeStatus DecodeLSAImm(MCInst &Inst,
368 const void *Decoder);
370 static DecodeStatus DecodeInsSize(MCInst &Inst,
373 const void *Decoder);
375 static DecodeStatus DecodeExtSize(MCInst &Inst,
378 const void *Decoder);
380 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
381 uint64_t Address, const void *Decoder);
383 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
384 uint64_t Address, const void *Decoder);
386 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
387 uint64_t Address, const void *Decoder);
389 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
390 uint64_t Address, const void *Decoder);
392 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
393 uint64_t Address, const void *Decoder);
395 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
396 uint64_t Address, const void *Decoder);
398 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
400 template <typename InsnType>
401 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
404 template <typename InsnType>
406 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
409 template <typename InsnType>
411 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
414 template <typename InsnType>
416 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
419 template <typename InsnType>
421 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
422 const void *Decoder);
424 template <typename InsnType>
426 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
427 const void *Decoder);
429 template <typename InsnType>
431 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
432 const void *Decoder);
434 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
436 const void *Decoder);
438 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
440 const void *Decoder);
443 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
447 static MCDisassembler *createMipsDisassembler(
449 const MCSubtargetInfo &STI,
451 return new MipsDisassembler(STI, Ctx, true);
454 static MCDisassembler *createMipselDisassembler(
456 const MCSubtargetInfo &STI,
458 return new MipsDisassembler(STI, Ctx, false);
461 static MCDisassembler *createMips64Disassembler(
463 const MCSubtargetInfo &STI,
465 return new Mips64Disassembler(STI, Ctx, true);
468 static MCDisassembler *createMips64elDisassembler(
470 const MCSubtargetInfo &STI,
472 return new Mips64Disassembler(STI, Ctx, false);
475 extern "C" void LLVMInitializeMipsDisassembler() {
476 // Register the disassembler.
477 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
478 createMipsDisassembler);
479 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
480 createMipselDisassembler);
481 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
482 createMips64Disassembler);
483 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
484 createMips64elDisassembler);
487 #include "MipsGenDisassemblerTables.inc"
489 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
490 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
491 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
492 return *(RegInfo->getRegClass(RC).begin() + RegNo);
495 template <typename InsnType>
496 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
497 const void *Decoder) {
498 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
499 // The size of the n field depends on the element size
500 // The register class also depends on this.
501 InsnType tmp = fieldFromInstruction(insn, 17, 5);
503 DecodeFN RegDecoder = nullptr;
504 if ((tmp & 0x18) == 0x00) { // INSVE_B
506 RegDecoder = DecodeMSA128BRegisterClass;
507 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
509 RegDecoder = DecodeMSA128HRegisterClass;
510 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
512 RegDecoder = DecodeMSA128WRegisterClass;
513 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
515 RegDecoder = DecodeMSA128DRegisterClass;
517 llvm_unreachable("Invalid encoding");
519 assert(NSize != 0 && RegDecoder != nullptr);
522 tmp = fieldFromInstruction(insn, 6, 5);
523 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
524 return MCDisassembler::Fail;
526 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
527 return MCDisassembler::Fail;
529 tmp = fieldFromInstruction(insn, 16, NSize);
530 MI.addOperand(MCOperand::CreateImm(tmp));
532 tmp = fieldFromInstruction(insn, 11, 5);
533 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
534 return MCDisassembler::Fail;
536 MI.addOperand(MCOperand::CreateImm(0));
538 return MCDisassembler::Success;
541 template <typename InsnType>
542 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
544 const void *Decoder) {
545 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
546 // (otherwise we would have matched the ADDI instruction from the earlier
550 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
552 // BEQZALC if rs == 0 && rt != 0
553 // BEQC if rs < rt && rs != 0
555 InsnType Rs = fieldFromInstruction(insn, 21, 5);
556 InsnType Rt = fieldFromInstruction(insn, 16, 5);
557 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
561 MI.setOpcode(Mips::BOVC);
563 } else if (Rs != 0 && Rs < Rt) {
564 MI.setOpcode(Mips::BEQC);
567 MI.setOpcode(Mips::BEQZALC);
570 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
573 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
575 MI.addOperand(MCOperand::CreateImm(Imm));
577 return MCDisassembler::Success;
580 template <typename InsnType>
581 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
583 const void *Decoder) {
584 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
585 // (otherwise we would have matched the ADDI instruction from the earlier
589 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
591 // BNEZALC if rs == 0 && rt != 0
592 // BNEC if rs < rt && rs != 0
594 InsnType Rs = fieldFromInstruction(insn, 21, 5);
595 InsnType Rt = fieldFromInstruction(insn, 16, 5);
596 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
600 MI.setOpcode(Mips::BNVC);
602 } else if (Rs != 0 && Rs < Rt) {
603 MI.setOpcode(Mips::BNEC);
606 MI.setOpcode(Mips::BNEZALC);
609 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
612 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
614 MI.addOperand(MCOperand::CreateImm(Imm));
616 return MCDisassembler::Success;
619 template <typename InsnType>
620 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
622 const void *Decoder) {
623 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
624 // (otherwise we would have matched the BLEZL instruction from the earlier
628 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
629 // Invalid if rs == 0
630 // BLEZC if rs == 0 && rt != 0
631 // BGEZC if rs == rt && rt != 0
632 // BGEC if rs != rt && rs != 0 && rt != 0
634 InsnType Rs = fieldFromInstruction(insn, 21, 5);
635 InsnType Rt = fieldFromInstruction(insn, 16, 5);
636 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
640 return MCDisassembler::Fail;
642 MI.setOpcode(Mips::BLEZC);
644 MI.setOpcode(Mips::BGEZC);
647 MI.setOpcode(Mips::BGEC);
651 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
654 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
657 MI.addOperand(MCOperand::CreateImm(Imm));
659 return MCDisassembler::Success;
662 template <typename InsnType>
663 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
665 const void *Decoder) {
666 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
667 // (otherwise we would have matched the BGTZL instruction from the earlier
671 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
672 // Invalid if rs == 0
673 // BGTZC if rs == 0 && rt != 0
674 // BLTZC if rs == rt && rt != 0
675 // BLTC if rs != rt && rs != 0 && rt != 0
679 InsnType Rs = fieldFromInstruction(insn, 21, 5);
680 InsnType Rt = fieldFromInstruction(insn, 16, 5);
681 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
684 return MCDisassembler::Fail;
686 MI.setOpcode(Mips::BGTZC);
688 MI.setOpcode(Mips::BLTZC);
690 MI.setOpcode(Mips::BLTC);
695 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
698 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
701 MI.addOperand(MCOperand::CreateImm(Imm));
703 return MCDisassembler::Success;
706 template <typename InsnType>
707 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
709 const void *Decoder) {
710 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
711 // (otherwise we would have matched the BGTZ instruction from the earlier
715 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
717 // BGTZALC if rs == 0 && rt != 0
718 // BLTZALC if rs != 0 && rs == rt
719 // BLTUC if rs != 0 && rs != rt
721 InsnType Rs = fieldFromInstruction(insn, 21, 5);
722 InsnType Rt = fieldFromInstruction(insn, 16, 5);
723 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
728 MI.setOpcode(Mips::BGTZ);
730 } else if (Rs == 0) {
731 MI.setOpcode(Mips::BGTZALC);
733 } else if (Rs == Rt) {
734 MI.setOpcode(Mips::BLTZALC);
737 MI.setOpcode(Mips::BLTUC);
743 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
747 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
750 MI.addOperand(MCOperand::CreateImm(Imm));
752 return MCDisassembler::Success;
755 template <typename InsnType>
756 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
758 const void *Decoder) {
759 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
760 // (otherwise we would have matched the BLEZL instruction from the earlier
764 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
765 // Invalid if rs == 0
766 // BLEZALC if rs == 0 && rt != 0
767 // BGEZALC if rs == rt && rt != 0
768 // BGEUC if rs != rt && rs != 0 && rt != 0
770 InsnType Rs = fieldFromInstruction(insn, 21, 5);
771 InsnType Rt = fieldFromInstruction(insn, 16, 5);
772 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
776 return MCDisassembler::Fail;
778 MI.setOpcode(Mips::BLEZALC);
780 MI.setOpcode(Mips::BGEZALC);
783 MI.setOpcode(Mips::BGEUC);
787 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
789 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
792 MI.addOperand(MCOperand::CreateImm(Imm));
794 return MCDisassembler::Success;
797 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
798 /// according to the given endianess.
799 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
800 uint64_t &Size, uint32_t &Insn,
802 // We want to read exactly 2 Bytes of data.
803 if (Bytes.size() < 2) {
805 return MCDisassembler::Fail;
809 Insn = (Bytes[0] << 8) | Bytes[1];
811 Insn = (Bytes[1] << 8) | Bytes[0];
814 return MCDisassembler::Success;
817 /// Read four bytes from the ArrayRef and return 32 bit word sorted
818 /// according to the given endianess
819 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
820 uint64_t &Size, uint32_t &Insn,
821 bool IsBigEndian, bool IsMicroMips) {
822 // We want to read exactly 4 Bytes of data.
823 if (Bytes.size() < 4) {
825 return MCDisassembler::Fail;
828 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
829 // always precede the low 16 bits in the instruction stream (that is, they
830 // are placed at lower addresses in the instruction stream).
832 // microMIPS byte ordering:
833 // Big-endian: 0 | 1 | 2 | 3
834 // Little-endian: 1 | 0 | 3 | 2
837 // Encoded as a big-endian 32-bit word in the stream.
839 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
842 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
845 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
850 return MCDisassembler::Success;
853 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
854 ArrayRef<uint8_t> Bytes,
856 raw_ostream &VStream,
857 raw_ostream &CStream) const {
862 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
864 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
865 // Calling the auto-generated decoder function.
866 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
868 if (Result != MCDisassembler::Fail) {
873 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
874 if (Result == MCDisassembler::Fail)
875 return MCDisassembler::Fail;
877 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
878 // Calling the auto-generated decoder function.
879 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
881 if (Result != MCDisassembler::Fail) {
885 return MCDisassembler::Fail;
888 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
889 if (Result == MCDisassembler::Fail)
890 return MCDisassembler::Fail;
893 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
895 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
896 if (Result != MCDisassembler::Fail) {
902 if (hasMips32r6() && isGP64()) {
903 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
904 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
906 if (Result != MCDisassembler::Fail) {
913 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
914 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
916 if (Result != MCDisassembler::Fail) {
922 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
923 // Calling the auto-generated decoder function.
925 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
926 if (Result != MCDisassembler::Fail) {
931 return MCDisassembler::Fail;
934 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
935 ArrayRef<uint8_t> Bytes,
937 raw_ostream &VStream,
938 raw_ostream &CStream) const {
941 DecodeStatus Result =
942 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
943 if (Result == MCDisassembler::Fail)
944 return MCDisassembler::Fail;
946 // Calling the auto-generated decoder function.
948 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
949 if (Result != MCDisassembler::Fail) {
953 // If we fail to decode in Mips64 decoder space we can try in Mips32
955 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
956 if (Result != MCDisassembler::Fail) {
961 return MCDisassembler::Fail;
964 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
967 const void *Decoder) {
969 return MCDisassembler::Fail;
973 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
976 const void *Decoder) {
979 return MCDisassembler::Fail;
981 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
982 Inst.addOperand(MCOperand::CreateReg(Reg));
983 return MCDisassembler::Success;
986 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
989 const void *Decoder) {
991 return MCDisassembler::Fail;
992 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
993 Inst.addOperand(MCOperand::CreateReg(Reg));
994 return MCDisassembler::Success;
997 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1000 const void *Decoder) {
1002 return MCDisassembler::Fail;
1003 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1004 Inst.addOperand(MCOperand::CreateReg(Reg));
1005 return MCDisassembler::Success;
1008 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1011 const void *Decoder) {
1013 return MCDisassembler::Fail;
1014 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1015 Inst.addOperand(MCOperand::CreateReg(Reg));
1016 return MCDisassembler::Success;
1019 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1022 const void *Decoder) {
1023 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
1024 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1026 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1029 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1032 const void *Decoder) {
1033 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1036 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1039 const void *Decoder) {
1041 return MCDisassembler::Fail;
1043 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1044 Inst.addOperand(MCOperand::CreateReg(Reg));
1045 return MCDisassembler::Success;
1048 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1051 const void *Decoder) {
1053 return MCDisassembler::Fail;
1055 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1056 Inst.addOperand(MCOperand::CreateReg(Reg));
1057 return MCDisassembler::Success;
1060 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1063 const void *Decoder) {
1065 return MCDisassembler::Fail;
1066 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1067 Inst.addOperand(MCOperand::CreateReg(Reg));
1068 return MCDisassembler::Success;
1071 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1074 const void *Decoder) {
1076 return MCDisassembler::Fail;
1077 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1078 Inst.addOperand(MCOperand::CreateReg(Reg));
1079 return MCDisassembler::Success;
1082 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1084 const void *Decoder) {
1086 return MCDisassembler::Fail;
1088 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1089 Inst.addOperand(MCOperand::CreateReg(Reg));
1090 return MCDisassembler::Success;
1093 static DecodeStatus DecodeMem(MCInst &Inst,
1096 const void *Decoder) {
1097 int Offset = SignExtend32<16>(Insn & 0xffff);
1098 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1099 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1101 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1102 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1104 if(Inst.getOpcode() == Mips::SC ||
1105 Inst.getOpcode() == Mips::SCD){
1106 Inst.addOperand(MCOperand::CreateReg(Reg));
1109 Inst.addOperand(MCOperand::CreateReg(Reg));
1110 Inst.addOperand(MCOperand::CreateReg(Base));
1111 Inst.addOperand(MCOperand::CreateImm(Offset));
1113 return MCDisassembler::Success;
1116 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1119 const void *Decoder) {
1120 int Offset = SignExtend32<16>(Insn & 0xffff);
1121 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1122 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1124 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1126 Inst.addOperand(MCOperand::CreateReg(Base));
1127 Inst.addOperand(MCOperand::CreateImm(Offset));
1128 Inst.addOperand(MCOperand::CreateImm(Hint));
1130 return MCDisassembler::Success;
1133 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1136 const void *Decoder) {
1137 int Offset = SignExtend32<12>(Insn & 0xfff);
1138 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1139 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1141 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1143 Inst.addOperand(MCOperand::CreateReg(Base));
1144 Inst.addOperand(MCOperand::CreateImm(Offset));
1145 Inst.addOperand(MCOperand::CreateImm(Hint));
1147 return MCDisassembler::Success;
1150 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
1153 const void *Decoder) {
1154 int Offset = fieldFromInstruction(Insn, 7, 9);
1155 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1156 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1158 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1160 Inst.addOperand(MCOperand::CreateReg(Base));
1161 Inst.addOperand(MCOperand::CreateImm(Offset));
1162 Inst.addOperand(MCOperand::CreateImm(Hint));
1164 return MCDisassembler::Success;
1167 static DecodeStatus DecodeSyncI(MCInst &Inst,
1170 const void *Decoder) {
1171 int Offset = SignExtend32<16>(Insn & 0xffff);
1172 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1174 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1176 Inst.addOperand(MCOperand::CreateReg(Base));
1177 Inst.addOperand(MCOperand::CreateImm(Offset));
1179 return MCDisassembler::Success;
1182 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1183 uint64_t Address, const void *Decoder) {
1184 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1185 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1186 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1188 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1189 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1191 Inst.addOperand(MCOperand::CreateReg(Reg));
1192 Inst.addOperand(MCOperand::CreateReg(Base));
1194 // The immediate field of an LD/ST instruction is scaled which means it must
1195 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1201 switch(Inst.getOpcode())
1204 assert (0 && "Unexpected instruction");
1205 return MCDisassembler::Fail;
1209 Inst.addOperand(MCOperand::CreateImm(Offset));
1213 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1217 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1221 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1225 return MCDisassembler::Success;
1228 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1231 const void *Decoder) {
1232 unsigned Offset = Insn & 0xf;
1233 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1234 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1236 switch (Inst.getOpcode()) {
1237 case Mips::LBU16_MM:
1238 case Mips::LHU16_MM:
1240 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1241 == MCDisassembler::Fail)
1242 return MCDisassembler::Fail;
1247 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1248 == MCDisassembler::Fail)
1249 return MCDisassembler::Fail;
1253 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1254 == MCDisassembler::Fail)
1255 return MCDisassembler::Fail;
1257 switch (Inst.getOpcode()) {
1258 case Mips::LBU16_MM:
1260 Inst.addOperand(MCOperand::CreateImm(-1));
1262 Inst.addOperand(MCOperand::CreateImm(Offset));
1265 Inst.addOperand(MCOperand::CreateImm(Offset));
1267 case Mips::LHU16_MM:
1269 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1273 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1277 return MCDisassembler::Success;
1280 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1283 const void *Decoder) {
1284 unsigned Offset = Insn & 0x1F;
1285 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1287 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1289 Inst.addOperand(MCOperand::CreateReg(Reg));
1290 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1291 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1293 return MCDisassembler::Success;
1296 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1299 const void *Decoder) {
1300 unsigned Offset = Insn & 0x7F;
1301 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1303 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1305 Inst.addOperand(MCOperand::CreateReg(Reg));
1306 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1307 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1309 return MCDisassembler::Success;
1312 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1315 const void *Decoder) {
1316 int Offset = SignExtend32<4>(Insn & 0xf);
1318 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1319 == MCDisassembler::Fail)
1320 return MCDisassembler::Fail;
1322 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1323 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1325 return MCDisassembler::Success;
1328 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1331 const void *Decoder) {
1332 int Offset = SignExtend32<12>(Insn & 0x0fff);
1333 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1334 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1336 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1337 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1339 switch (Inst.getOpcode()) {
1340 case Mips::SWM32_MM:
1341 case Mips::LWM32_MM:
1342 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1343 == MCDisassembler::Fail)
1344 return MCDisassembler::Fail;
1345 Inst.addOperand(MCOperand::CreateReg(Base));
1346 Inst.addOperand(MCOperand::CreateImm(Offset));
1349 Inst.addOperand(MCOperand::CreateReg(Reg));
1352 Inst.addOperand(MCOperand::CreateReg(Reg));
1353 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1354 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1356 Inst.addOperand(MCOperand::CreateReg(Base));
1357 Inst.addOperand(MCOperand::CreateImm(Offset));
1360 return MCDisassembler::Success;
1363 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1366 const void *Decoder) {
1367 int Offset = SignExtend32<16>(Insn & 0xffff);
1368 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1369 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1371 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1372 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1374 Inst.addOperand(MCOperand::CreateReg(Reg));
1375 Inst.addOperand(MCOperand::CreateReg(Base));
1376 Inst.addOperand(MCOperand::CreateImm(Offset));
1378 return MCDisassembler::Success;
1381 static DecodeStatus DecodeFMem(MCInst &Inst,
1384 const void *Decoder) {
1385 int Offset = SignExtend32<16>(Insn & 0xffff);
1386 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1387 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1389 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1390 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1392 Inst.addOperand(MCOperand::CreateReg(Reg));
1393 Inst.addOperand(MCOperand::CreateReg(Base));
1394 Inst.addOperand(MCOperand::CreateImm(Offset));
1396 return MCDisassembler::Success;
1399 static DecodeStatus DecodeFMem2(MCInst &Inst,
1402 const void *Decoder) {
1403 int Offset = SignExtend32<16>(Insn & 0xffff);
1404 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1405 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1407 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1408 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1410 Inst.addOperand(MCOperand::CreateReg(Reg));
1411 Inst.addOperand(MCOperand::CreateReg(Base));
1412 Inst.addOperand(MCOperand::CreateImm(Offset));
1414 return MCDisassembler::Success;
1417 static DecodeStatus DecodeFMem3(MCInst &Inst,
1420 const void *Decoder) {
1421 int Offset = SignExtend32<16>(Insn & 0xffff);
1422 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1423 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1425 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1426 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1428 Inst.addOperand(MCOperand::CreateReg(Reg));
1429 Inst.addOperand(MCOperand::CreateReg(Base));
1430 Inst.addOperand(MCOperand::CreateImm(Offset));
1432 return MCDisassembler::Success;
1435 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1438 const void *Decoder) {
1439 int Offset = SignExtend32<11>(Insn & 0x07ff);
1440 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1441 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1443 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1444 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1446 Inst.addOperand(MCOperand::CreateReg(Reg));
1447 Inst.addOperand(MCOperand::CreateReg(Base));
1448 Inst.addOperand(MCOperand::CreateImm(Offset));
1450 return MCDisassembler::Success;
1452 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1455 const void *Decoder) {
1456 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1457 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1458 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1460 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1461 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1463 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1464 Inst.addOperand(MCOperand::CreateReg(Rt));
1467 Inst.addOperand(MCOperand::CreateReg(Rt));
1468 Inst.addOperand(MCOperand::CreateReg(Base));
1469 Inst.addOperand(MCOperand::CreateImm(Offset));
1471 return MCDisassembler::Success;
1474 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1477 const void *Decoder) {
1478 // Currently only hardware register 29 is supported.
1480 return MCDisassembler::Fail;
1481 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1482 return MCDisassembler::Success;
1485 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1488 const void *Decoder) {
1489 if (RegNo > 30 || RegNo %2)
1490 return MCDisassembler::Fail;
1493 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1494 Inst.addOperand(MCOperand::CreateReg(Reg));
1495 return MCDisassembler::Success;
1498 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1501 const void *Decoder) {
1503 return MCDisassembler::Fail;
1505 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1506 Inst.addOperand(MCOperand::CreateReg(Reg));
1507 return MCDisassembler::Success;
1510 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1513 const void *Decoder) {
1515 return MCDisassembler::Fail;
1517 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1518 Inst.addOperand(MCOperand::CreateReg(Reg));
1519 return MCDisassembler::Success;
1522 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1525 const void *Decoder) {
1527 return MCDisassembler::Fail;
1529 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1530 Inst.addOperand(MCOperand::CreateReg(Reg));
1531 return MCDisassembler::Success;
1534 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1537 const void *Decoder) {
1539 return MCDisassembler::Fail;
1541 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1542 Inst.addOperand(MCOperand::CreateReg(Reg));
1543 return MCDisassembler::Success;
1546 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1549 const void *Decoder) {
1551 return MCDisassembler::Fail;
1553 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1554 Inst.addOperand(MCOperand::CreateReg(Reg));
1555 return MCDisassembler::Success;
1558 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1561 const void *Decoder) {
1563 return MCDisassembler::Fail;
1565 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1566 Inst.addOperand(MCOperand::CreateReg(Reg));
1567 return MCDisassembler::Success;
1570 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1573 const void *Decoder) {
1575 return MCDisassembler::Fail;
1577 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1578 Inst.addOperand(MCOperand::CreateReg(Reg));
1579 return MCDisassembler::Success;
1582 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1585 const void *Decoder) {
1587 return MCDisassembler::Fail;
1589 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1590 Inst.addOperand(MCOperand::CreateReg(Reg));
1591 return MCDisassembler::Success;
1594 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1597 const void *Decoder) {
1599 return MCDisassembler::Fail;
1601 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1602 Inst.addOperand(MCOperand::CreateReg(Reg));
1603 return MCDisassembler::Success;
1606 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1609 const void *Decoder) {
1610 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1611 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1612 return MCDisassembler::Success;
1615 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1618 const void *Decoder) {
1620 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1621 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1622 return MCDisassembler::Success;
1625 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1628 const void *Decoder) {
1629 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1631 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1632 return MCDisassembler::Success;
1635 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1638 const void *Decoder) {
1639 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1641 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1642 return MCDisassembler::Success;
1645 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1648 const void *Decoder) {
1649 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1650 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1651 return MCDisassembler::Success;
1654 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1657 const void *Decoder) {
1658 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1659 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1660 return MCDisassembler::Success;
1663 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1666 const void *Decoder) {
1667 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1668 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1669 return MCDisassembler::Success;
1672 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1675 const void *Decoder) {
1676 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1677 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1678 return MCDisassembler::Success;
1681 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1684 const void *Decoder) {
1686 Inst.addOperand(MCOperand::CreateImm(1));
1687 else if (Value == 0x7)
1688 Inst.addOperand(MCOperand::CreateImm(-1));
1690 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1691 return MCDisassembler::Success;
1694 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1697 const void *Decoder) {
1698 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1699 return MCDisassembler::Success;
1702 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1705 const void *Decoder) {
1707 Inst.addOperand(MCOperand::CreateImm(-1));
1709 Inst.addOperand(MCOperand::CreateImm(Value));
1710 return MCDisassembler::Success;
1713 static DecodeStatus DecodeSimm4(MCInst &Inst,
1716 const void *Decoder) {
1717 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1718 return MCDisassembler::Success;
1721 static DecodeStatus DecodeSimm16(MCInst &Inst,
1724 const void *Decoder) {
1725 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1726 return MCDisassembler::Success;
1729 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1732 const void *Decoder) {
1733 // We add one to the immediate field as it was encoded as 'imm - 1'.
1734 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1735 return MCDisassembler::Success;
1738 static DecodeStatus DecodeInsSize(MCInst &Inst,
1741 const void *Decoder) {
1742 // First we need to grab the pos(lsb) from MCInst.
1743 int Pos = Inst.getOperand(2).getImm();
1744 int Size = (int) Insn - Pos + 1;
1745 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1746 return MCDisassembler::Success;
1749 static DecodeStatus DecodeExtSize(MCInst &Inst,
1752 const void *Decoder) {
1753 int Size = (int) Insn + 1;
1754 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1755 return MCDisassembler::Success;
1758 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1759 uint64_t Address, const void *Decoder) {
1760 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1761 return MCDisassembler::Success;
1764 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1765 uint64_t Address, const void *Decoder) {
1766 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1767 return MCDisassembler::Success;
1770 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1771 uint64_t Address, const void *Decoder) {
1772 int32_t DecodedValue;
1774 case 0: DecodedValue = 256; break;
1775 case 1: DecodedValue = 257; break;
1776 case 510: DecodedValue = -258; break;
1777 case 511: DecodedValue = -257; break;
1778 default: DecodedValue = SignExtend32<9>(Insn); break;
1780 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
1781 return MCDisassembler::Success;
1784 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1785 uint64_t Address, const void *Decoder) {
1786 // Insn must be >= 0, since it is unsigned that condition is always true.
1788 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1790 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1791 return MCDisassembler::Success;
1794 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1795 uint64_t Address, const void *Decoder) {
1796 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1797 return MCDisassembler::Success;
1800 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1803 const void *Decoder) {
1804 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1805 Mips::S6, Mips::FP};
1808 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1809 // Empty register lists are not allowed.
1811 return MCDisassembler::Fail;
1813 RegNum = RegLst & 0xf;
1814 for (unsigned i = 0; i < RegNum; i++)
1815 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1818 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1820 return MCDisassembler::Success;
1823 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1825 const void *Decoder) {
1826 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1827 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1828 unsigned RegNum = RegLst & 0x3;
1830 for (unsigned i = 0; i <= RegNum; i++)
1831 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1833 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1835 return MCDisassembler::Success;
1838 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1839 uint64_t Address, const void *Decoder) {
1840 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1841 return MCDisassembler::Success;