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 class MipsDisassembler : public MCDisassembler {
37 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
38 : MCDisassembler(STI, Ctx),
39 IsMicroMips(STI.getFeatureBits() & Mips::FeatureMicroMips),
40 IsBigEndian(IsBigEndian) {}
42 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
43 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
44 bool hasMips32r6() const {
45 return STI.getFeatureBits() & Mips::FeatureMips32r6;
48 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
50 bool hasCOP3() const {
51 // Only present in MIPS-I and MIPS-II
52 return !hasMips32() && !hasMips3();
55 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
56 ArrayRef<uint8_t> Bytes, uint64_t Address,
58 raw_ostream &CStream) const override;
61 } // end anonymous namespace
63 // Forward declare these because the autogenerated code will reference them.
64 // Definitions are further down.
65 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
70 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
75 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
80 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
85 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
90 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
95 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
100 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
103 const void *Decoder);
105 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
108 const void *Decoder);
110 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
113 const void *Decoder);
115 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
118 const void *Decoder);
120 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
123 const void *Decoder);
125 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
127 const void *Decoder);
129 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
132 const void *Decoder);
134 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
137 const void *Decoder);
139 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
142 const void *Decoder);
144 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
147 const void *Decoder);
149 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
152 const void *Decoder);
154 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
157 const void *Decoder);
159 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
162 const void *Decoder);
164 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
167 const void *Decoder);
169 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
172 const void *Decoder);
174 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
177 const void *Decoder);
179 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
182 const void *Decoder);
184 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
187 const void *Decoder);
189 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
192 const void *Decoder);
194 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
197 const void *Decoder);
199 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
202 const void *Decoder);
204 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
205 // shifted left by 1 bit.
206 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
209 const void *Decoder);
211 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
212 // shifted left by 1 bit.
213 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
216 const void *Decoder);
218 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
219 // shifted left by 1 bit.
220 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
223 const void *Decoder);
225 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
226 // shifted left by 1 bit.
227 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
230 const void *Decoder);
232 static DecodeStatus DecodeMem(MCInst &Inst,
235 const void *Decoder);
237 static DecodeStatus DecodeCacheOp(MCInst &Inst,
240 const void *Decoder);
242 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
245 const void *Decoder);
247 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
250 const void *Decoder);
252 static DecodeStatus DecodeSyncI(MCInst &Inst,
255 const void *Decoder);
257 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
258 uint64_t Address, const void *Decoder);
260 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
263 const void *Decoder);
265 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
268 const void *Decoder);
270 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
273 const void *Decoder);
275 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
278 const void *Decoder);
280 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
283 const void *Decoder);
285 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
288 const void *Decoder);
290 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
292 const void *Decoder);
294 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
296 const void *Decoder);
298 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
300 const void *Decoder);
302 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
304 const void *Decoder);
306 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
309 const void *Decoder);
311 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
314 const void *Decoder);
316 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
319 const void *Decoder);
321 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
324 const void *Decoder);
326 static DecodeStatus DecodeSimm4(MCInst &Inst,
329 const void *Decoder);
331 static DecodeStatus DecodeSimm16(MCInst &Inst,
334 const void *Decoder);
336 // Decode the immediate field of an LSA instruction which
338 static DecodeStatus DecodeLSAImm(MCInst &Inst,
341 const void *Decoder);
343 static DecodeStatus DecodeInsSize(MCInst &Inst,
346 const void *Decoder);
348 static DecodeStatus DecodeExtSize(MCInst &Inst,
351 const void *Decoder);
353 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
354 uint64_t Address, const void *Decoder);
356 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
357 uint64_t Address, const void *Decoder);
359 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
360 uint64_t Address, const void *Decoder);
362 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
363 uint64_t Address, const void *Decoder);
365 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
366 uint64_t Address, const void *Decoder);
368 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
369 uint64_t Address, const void *Decoder);
371 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
373 template <typename InsnType>
374 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
375 const void *Decoder);
377 template <typename InsnType>
379 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
380 const void *Decoder);
382 template <typename InsnType>
384 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
385 const void *Decoder);
387 template <typename InsnType>
389 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
390 const void *Decoder);
392 template <typename InsnType>
394 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
395 const void *Decoder);
397 template <typename InsnType>
399 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
400 const void *Decoder);
402 template <typename InsnType>
404 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
405 const void *Decoder);
407 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
409 const void *Decoder);
411 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
413 const void *Decoder);
415 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
417 const void *Decoder);
420 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
424 static MCDisassembler *createMipsDisassembler(
426 const MCSubtargetInfo &STI,
428 return new MipsDisassembler(STI, Ctx, true);
431 static MCDisassembler *createMipselDisassembler(
433 const MCSubtargetInfo &STI,
435 return new MipsDisassembler(STI, Ctx, false);
438 extern "C" void LLVMInitializeMipsDisassembler() {
439 // Register the disassembler.
440 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
441 createMipsDisassembler);
442 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
443 createMipselDisassembler);
444 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
445 createMipsDisassembler);
446 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
447 createMipselDisassembler);
450 #include "MipsGenDisassemblerTables.inc"
452 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
453 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
454 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
455 return *(RegInfo->getRegClass(RC).begin() + RegNo);
458 template <typename InsnType>
459 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
460 const void *Decoder) {
461 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
462 // The size of the n field depends on the element size
463 // The register class also depends on this.
464 InsnType tmp = fieldFromInstruction(insn, 17, 5);
466 DecodeFN RegDecoder = nullptr;
467 if ((tmp & 0x18) == 0x00) { // INSVE_B
469 RegDecoder = DecodeMSA128BRegisterClass;
470 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
472 RegDecoder = DecodeMSA128HRegisterClass;
473 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
475 RegDecoder = DecodeMSA128WRegisterClass;
476 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
478 RegDecoder = DecodeMSA128DRegisterClass;
480 llvm_unreachable("Invalid encoding");
482 assert(NSize != 0 && RegDecoder != nullptr);
485 tmp = fieldFromInstruction(insn, 6, 5);
486 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
487 return MCDisassembler::Fail;
489 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
490 return MCDisassembler::Fail;
492 tmp = fieldFromInstruction(insn, 16, NSize);
493 MI.addOperand(MCOperand::CreateImm(tmp));
495 tmp = fieldFromInstruction(insn, 11, 5);
496 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
497 return MCDisassembler::Fail;
499 MI.addOperand(MCOperand::CreateImm(0));
501 return MCDisassembler::Success;
504 template <typename InsnType>
505 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
507 const void *Decoder) {
508 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
509 // (otherwise we would have matched the ADDI instruction from the earlier
513 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
515 // BEQZALC if rs == 0 && rt != 0
516 // BEQC if rs < rt && rs != 0
518 InsnType Rs = fieldFromInstruction(insn, 21, 5);
519 InsnType Rt = fieldFromInstruction(insn, 16, 5);
520 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
524 MI.setOpcode(Mips::BOVC);
526 } else if (Rs != 0 && Rs < Rt) {
527 MI.setOpcode(Mips::BEQC);
530 MI.setOpcode(Mips::BEQZALC);
533 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
536 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
538 MI.addOperand(MCOperand::CreateImm(Imm));
540 return MCDisassembler::Success;
543 template <typename InsnType>
544 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
546 const void *Decoder) {
547 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
548 // (otherwise we would have matched the ADDI instruction from the earlier
552 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
554 // BNEZALC if rs == 0 && rt != 0
555 // BNEC if rs < rt && rs != 0
557 InsnType Rs = fieldFromInstruction(insn, 21, 5);
558 InsnType Rt = fieldFromInstruction(insn, 16, 5);
559 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
563 MI.setOpcode(Mips::BNVC);
565 } else if (Rs != 0 && Rs < Rt) {
566 MI.setOpcode(Mips::BNEC);
569 MI.setOpcode(Mips::BNEZALC);
572 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
575 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
577 MI.addOperand(MCOperand::CreateImm(Imm));
579 return MCDisassembler::Success;
582 template <typename InsnType>
583 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
585 const void *Decoder) {
586 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
587 // (otherwise we would have matched the BLEZL instruction from the earlier
591 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
592 // Invalid if rs == 0
593 // BLEZC if rs == 0 && rt != 0
594 // BGEZC if rs == rt && rt != 0
595 // BGEC if rs != rt && rs != 0 && rt != 0
597 InsnType Rs = fieldFromInstruction(insn, 21, 5);
598 InsnType Rt = fieldFromInstruction(insn, 16, 5);
599 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
603 return MCDisassembler::Fail;
605 MI.setOpcode(Mips::BLEZC);
607 MI.setOpcode(Mips::BGEZC);
610 MI.setOpcode(Mips::BGEC);
614 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
617 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
620 MI.addOperand(MCOperand::CreateImm(Imm));
622 return MCDisassembler::Success;
625 template <typename InsnType>
626 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
628 const void *Decoder) {
629 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
630 // (otherwise we would have matched the BGTZL instruction from the earlier
634 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
635 // Invalid if rs == 0
636 // BGTZC if rs == 0 && rt != 0
637 // BLTZC if rs == rt && rt != 0
638 // BLTC if rs != rt && rs != 0 && rt != 0
642 InsnType Rs = fieldFromInstruction(insn, 21, 5);
643 InsnType Rt = fieldFromInstruction(insn, 16, 5);
644 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
647 return MCDisassembler::Fail;
649 MI.setOpcode(Mips::BGTZC);
651 MI.setOpcode(Mips::BLTZC);
653 MI.setOpcode(Mips::BLTC);
658 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
661 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
664 MI.addOperand(MCOperand::CreateImm(Imm));
666 return MCDisassembler::Success;
669 template <typename InsnType>
670 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
672 const void *Decoder) {
673 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
674 // (otherwise we would have matched the BGTZ instruction from the earlier
678 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
680 // BGTZALC if rs == 0 && rt != 0
681 // BLTZALC if rs != 0 && rs == rt
682 // BLTUC if rs != 0 && rs != rt
684 InsnType Rs = fieldFromInstruction(insn, 21, 5);
685 InsnType Rt = fieldFromInstruction(insn, 16, 5);
686 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
691 MI.setOpcode(Mips::BGTZ);
693 } else if (Rs == 0) {
694 MI.setOpcode(Mips::BGTZALC);
696 } else if (Rs == Rt) {
697 MI.setOpcode(Mips::BLTZALC);
700 MI.setOpcode(Mips::BLTUC);
706 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
710 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
713 MI.addOperand(MCOperand::CreateImm(Imm));
715 return MCDisassembler::Success;
718 template <typename InsnType>
719 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
721 const void *Decoder) {
722 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
723 // (otherwise we would have matched the BLEZL instruction from the earlier
727 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
728 // Invalid if rs == 0
729 // BLEZALC if rs == 0 && rt != 0
730 // BGEZALC if rs == rt && rt != 0
731 // BGEUC if rs != rt && rs != 0 && rt != 0
733 InsnType Rs = fieldFromInstruction(insn, 21, 5);
734 InsnType Rt = fieldFromInstruction(insn, 16, 5);
735 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
739 return MCDisassembler::Fail;
741 MI.setOpcode(Mips::BLEZALC);
743 MI.setOpcode(Mips::BGEZALC);
746 MI.setOpcode(Mips::BGEUC);
750 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
752 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
755 MI.addOperand(MCOperand::CreateImm(Imm));
757 return MCDisassembler::Success;
760 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
761 /// according to the given endianess.
762 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
763 uint64_t &Size, uint32_t &Insn,
765 // We want to read exactly 2 Bytes of data.
766 if (Bytes.size() < 2) {
768 return MCDisassembler::Fail;
772 Insn = (Bytes[0] << 8) | Bytes[1];
774 Insn = (Bytes[1] << 8) | Bytes[0];
777 return MCDisassembler::Success;
780 /// Read four bytes from the ArrayRef and return 32 bit word sorted
781 /// according to the given endianess
782 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
783 uint64_t &Size, uint32_t &Insn,
784 bool IsBigEndian, bool IsMicroMips) {
785 // We want to read exactly 4 Bytes of data.
786 if (Bytes.size() < 4) {
788 return MCDisassembler::Fail;
791 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
792 // always precede the low 16 bits in the instruction stream (that is, they
793 // are placed at lower addresses in the instruction stream).
795 // microMIPS byte ordering:
796 // Big-endian: 0 | 1 | 2 | 3
797 // Little-endian: 1 | 0 | 3 | 2
800 // Encoded as a big-endian 32-bit word in the stream.
802 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
805 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
808 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
813 return MCDisassembler::Success;
816 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
817 ArrayRef<uint8_t> Bytes,
819 raw_ostream &VStream,
820 raw_ostream &CStream) const {
825 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
827 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
828 // Calling the auto-generated decoder function.
829 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
831 if (Result != MCDisassembler::Fail) {
836 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
837 if (Result == MCDisassembler::Fail)
838 return MCDisassembler::Fail;
840 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
841 // Calling the auto-generated decoder function.
842 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
844 if (Result != MCDisassembler::Fail) {
848 return MCDisassembler::Fail;
851 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
852 if (Result == MCDisassembler::Fail)
853 return MCDisassembler::Fail;
856 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
858 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
859 if (Result != MCDisassembler::Fail) {
865 if (hasMips32r6() && isGP64()) {
866 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
867 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
869 if (Result != MCDisassembler::Fail) {
876 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
877 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
879 if (Result != MCDisassembler::Fail) {
886 DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
887 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
889 if (Result != MCDisassembler::Fail) {
895 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
896 // Calling the auto-generated decoder function.
898 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
899 if (Result != MCDisassembler::Fail) {
904 return MCDisassembler::Fail;
907 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
910 const void *Decoder) {
912 return MCDisassembler::Fail;
916 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
919 const void *Decoder) {
922 return MCDisassembler::Fail;
924 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
925 Inst.addOperand(MCOperand::CreateReg(Reg));
926 return MCDisassembler::Success;
929 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
932 const void *Decoder) {
934 return MCDisassembler::Fail;
935 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
936 Inst.addOperand(MCOperand::CreateReg(Reg));
937 return MCDisassembler::Success;
940 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
943 const void *Decoder) {
945 return MCDisassembler::Fail;
946 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
947 Inst.addOperand(MCOperand::CreateReg(Reg));
948 return MCDisassembler::Success;
951 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
954 const void *Decoder) {
956 return MCDisassembler::Fail;
957 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
958 Inst.addOperand(MCOperand::CreateReg(Reg));
959 return MCDisassembler::Success;
962 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
965 const void *Decoder) {
967 return MCDisassembler::Fail;
968 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
969 Inst.addOperand(MCOperand::CreateReg(Reg));
970 return MCDisassembler::Success;
973 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
976 const void *Decoder) {
977 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
978 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
980 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
983 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
986 const void *Decoder) {
987 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
990 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
993 const void *Decoder) {
995 return MCDisassembler::Fail;
997 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
998 Inst.addOperand(MCOperand::CreateReg(Reg));
999 return MCDisassembler::Success;
1002 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1005 const void *Decoder) {
1007 return MCDisassembler::Fail;
1009 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1010 Inst.addOperand(MCOperand::CreateReg(Reg));
1011 return MCDisassembler::Success;
1014 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1017 const void *Decoder) {
1019 return MCDisassembler::Fail;
1020 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1021 Inst.addOperand(MCOperand::CreateReg(Reg));
1022 return MCDisassembler::Success;
1025 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1028 const void *Decoder) {
1030 return MCDisassembler::Fail;
1031 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1032 Inst.addOperand(MCOperand::CreateReg(Reg));
1033 return MCDisassembler::Success;
1036 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1038 const void *Decoder) {
1040 return MCDisassembler::Fail;
1042 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1043 Inst.addOperand(MCOperand::CreateReg(Reg));
1044 return MCDisassembler::Success;
1047 static DecodeStatus DecodeMem(MCInst &Inst,
1050 const void *Decoder) {
1051 int Offset = SignExtend32<16>(Insn & 0xffff);
1052 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1053 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1055 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1056 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1058 if(Inst.getOpcode() == Mips::SC ||
1059 Inst.getOpcode() == Mips::SCD){
1060 Inst.addOperand(MCOperand::CreateReg(Reg));
1063 Inst.addOperand(MCOperand::CreateReg(Reg));
1064 Inst.addOperand(MCOperand::CreateReg(Base));
1065 Inst.addOperand(MCOperand::CreateImm(Offset));
1067 return MCDisassembler::Success;
1070 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1073 const void *Decoder) {
1074 int Offset = SignExtend32<16>(Insn & 0xffff);
1075 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1076 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1078 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1080 Inst.addOperand(MCOperand::CreateReg(Base));
1081 Inst.addOperand(MCOperand::CreateImm(Offset));
1082 Inst.addOperand(MCOperand::CreateImm(Hint));
1084 return MCDisassembler::Success;
1087 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1090 const void *Decoder) {
1091 int Offset = SignExtend32<12>(Insn & 0xfff);
1092 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1093 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1095 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1097 Inst.addOperand(MCOperand::CreateReg(Base));
1098 Inst.addOperand(MCOperand::CreateImm(Offset));
1099 Inst.addOperand(MCOperand::CreateImm(Hint));
1101 return MCDisassembler::Success;
1104 static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
1107 const void *Decoder) {
1108 int Offset = fieldFromInstruction(Insn, 7, 9);
1109 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1110 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1112 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1114 Inst.addOperand(MCOperand::CreateReg(Base));
1115 Inst.addOperand(MCOperand::CreateImm(Offset));
1116 Inst.addOperand(MCOperand::CreateImm(Hint));
1118 return MCDisassembler::Success;
1121 static DecodeStatus DecodeSyncI(MCInst &Inst,
1124 const void *Decoder) {
1125 int Offset = SignExtend32<16>(Insn & 0xffff);
1126 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1128 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1130 Inst.addOperand(MCOperand::CreateReg(Base));
1131 Inst.addOperand(MCOperand::CreateImm(Offset));
1133 return MCDisassembler::Success;
1136 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1137 uint64_t Address, const void *Decoder) {
1138 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1139 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1140 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1142 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1143 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1145 Inst.addOperand(MCOperand::CreateReg(Reg));
1146 Inst.addOperand(MCOperand::CreateReg(Base));
1148 // The immediate field of an LD/ST instruction is scaled which means it must
1149 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1155 switch(Inst.getOpcode())
1158 assert (0 && "Unexpected instruction");
1159 return MCDisassembler::Fail;
1163 Inst.addOperand(MCOperand::CreateImm(Offset));
1167 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1171 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1175 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1179 return MCDisassembler::Success;
1182 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1185 const void *Decoder) {
1186 unsigned Offset = Insn & 0xf;
1187 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1188 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1190 switch (Inst.getOpcode()) {
1191 case Mips::LBU16_MM:
1192 case Mips::LHU16_MM:
1194 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1195 == MCDisassembler::Fail)
1196 return MCDisassembler::Fail;
1201 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1202 == MCDisassembler::Fail)
1203 return MCDisassembler::Fail;
1207 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1208 == MCDisassembler::Fail)
1209 return MCDisassembler::Fail;
1211 switch (Inst.getOpcode()) {
1212 case Mips::LBU16_MM:
1214 Inst.addOperand(MCOperand::CreateImm(-1));
1216 Inst.addOperand(MCOperand::CreateImm(Offset));
1219 Inst.addOperand(MCOperand::CreateImm(Offset));
1221 case Mips::LHU16_MM:
1223 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1227 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1231 return MCDisassembler::Success;
1234 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1237 const void *Decoder) {
1238 unsigned Offset = Insn & 0x1F;
1239 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1241 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1243 Inst.addOperand(MCOperand::CreateReg(Reg));
1244 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1245 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1247 return MCDisassembler::Success;
1250 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1253 const void *Decoder) {
1254 unsigned Offset = Insn & 0x7F;
1255 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1257 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1259 Inst.addOperand(MCOperand::CreateReg(Reg));
1260 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1261 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1263 return MCDisassembler::Success;
1266 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1269 const void *Decoder) {
1270 int Offset = SignExtend32<4>(Insn & 0xf);
1272 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1273 == MCDisassembler::Fail)
1274 return MCDisassembler::Fail;
1276 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1277 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1279 return MCDisassembler::Success;
1282 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1285 const void *Decoder) {
1286 int Offset = SignExtend32<12>(Insn & 0x0fff);
1287 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1288 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1290 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1291 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1293 switch (Inst.getOpcode()) {
1294 case Mips::SWM32_MM:
1295 case Mips::LWM32_MM:
1296 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1297 == MCDisassembler::Fail)
1298 return MCDisassembler::Fail;
1299 Inst.addOperand(MCOperand::CreateReg(Base));
1300 Inst.addOperand(MCOperand::CreateImm(Offset));
1303 Inst.addOperand(MCOperand::CreateReg(Reg));
1306 Inst.addOperand(MCOperand::CreateReg(Reg));
1307 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1308 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1310 Inst.addOperand(MCOperand::CreateReg(Base));
1311 Inst.addOperand(MCOperand::CreateImm(Offset));
1314 return MCDisassembler::Success;
1317 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1320 const void *Decoder) {
1321 int Offset = SignExtend32<16>(Insn & 0xffff);
1322 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1323 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1325 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1326 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1328 Inst.addOperand(MCOperand::CreateReg(Reg));
1329 Inst.addOperand(MCOperand::CreateReg(Base));
1330 Inst.addOperand(MCOperand::CreateImm(Offset));
1332 return MCDisassembler::Success;
1335 static DecodeStatus DecodeFMem(MCInst &Inst,
1338 const void *Decoder) {
1339 int Offset = SignExtend32<16>(Insn & 0xffff);
1340 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1341 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1343 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1344 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1346 Inst.addOperand(MCOperand::CreateReg(Reg));
1347 Inst.addOperand(MCOperand::CreateReg(Base));
1348 Inst.addOperand(MCOperand::CreateImm(Offset));
1350 return MCDisassembler::Success;
1353 static DecodeStatus DecodeFMem2(MCInst &Inst,
1356 const void *Decoder) {
1357 int Offset = SignExtend32<16>(Insn & 0xffff);
1358 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1359 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1361 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1362 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1364 Inst.addOperand(MCOperand::CreateReg(Reg));
1365 Inst.addOperand(MCOperand::CreateReg(Base));
1366 Inst.addOperand(MCOperand::CreateImm(Offset));
1368 return MCDisassembler::Success;
1371 static DecodeStatus DecodeFMem3(MCInst &Inst,
1374 const void *Decoder) {
1375 int Offset = SignExtend32<16>(Insn & 0xffff);
1376 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1377 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1379 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1380 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1382 Inst.addOperand(MCOperand::CreateReg(Reg));
1383 Inst.addOperand(MCOperand::CreateReg(Base));
1384 Inst.addOperand(MCOperand::CreateImm(Offset));
1386 return MCDisassembler::Success;
1389 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1392 const void *Decoder) {
1393 int Offset = SignExtend32<11>(Insn & 0x07ff);
1394 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1395 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1397 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1398 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1400 Inst.addOperand(MCOperand::CreateReg(Reg));
1401 Inst.addOperand(MCOperand::CreateReg(Base));
1402 Inst.addOperand(MCOperand::CreateImm(Offset));
1404 return MCDisassembler::Success;
1406 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1409 const void *Decoder) {
1410 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1411 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1412 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1414 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1415 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1417 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1418 Inst.addOperand(MCOperand::CreateReg(Rt));
1421 Inst.addOperand(MCOperand::CreateReg(Rt));
1422 Inst.addOperand(MCOperand::CreateReg(Base));
1423 Inst.addOperand(MCOperand::CreateImm(Offset));
1425 return MCDisassembler::Success;
1428 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1431 const void *Decoder) {
1432 // Currently only hardware register 29 is supported.
1434 return MCDisassembler::Fail;
1435 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1436 return MCDisassembler::Success;
1439 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1442 const void *Decoder) {
1443 if (RegNo > 30 || RegNo %2)
1444 return MCDisassembler::Fail;
1447 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1448 Inst.addOperand(MCOperand::CreateReg(Reg));
1449 return MCDisassembler::Success;
1452 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1455 const void *Decoder) {
1457 return MCDisassembler::Fail;
1459 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1460 Inst.addOperand(MCOperand::CreateReg(Reg));
1461 return MCDisassembler::Success;
1464 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1467 const void *Decoder) {
1469 return MCDisassembler::Fail;
1471 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1472 Inst.addOperand(MCOperand::CreateReg(Reg));
1473 return MCDisassembler::Success;
1476 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1479 const void *Decoder) {
1481 return MCDisassembler::Fail;
1483 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1484 Inst.addOperand(MCOperand::CreateReg(Reg));
1485 return MCDisassembler::Success;
1488 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1491 const void *Decoder) {
1493 return MCDisassembler::Fail;
1495 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1496 Inst.addOperand(MCOperand::CreateReg(Reg));
1497 return MCDisassembler::Success;
1500 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1503 const void *Decoder) {
1505 return MCDisassembler::Fail;
1507 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1508 Inst.addOperand(MCOperand::CreateReg(Reg));
1509 return MCDisassembler::Success;
1512 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1515 const void *Decoder) {
1517 return MCDisassembler::Fail;
1519 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1520 Inst.addOperand(MCOperand::CreateReg(Reg));
1521 return MCDisassembler::Success;
1524 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1527 const void *Decoder) {
1529 return MCDisassembler::Fail;
1531 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1532 Inst.addOperand(MCOperand::CreateReg(Reg));
1533 return MCDisassembler::Success;
1536 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1539 const void *Decoder) {
1541 return MCDisassembler::Fail;
1543 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1544 Inst.addOperand(MCOperand::CreateReg(Reg));
1545 return MCDisassembler::Success;
1548 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1551 const void *Decoder) {
1553 return MCDisassembler::Fail;
1555 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1556 Inst.addOperand(MCOperand::CreateReg(Reg));
1557 return MCDisassembler::Success;
1560 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1563 const void *Decoder) {
1564 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1565 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1566 return MCDisassembler::Success;
1569 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1572 const void *Decoder) {
1574 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1575 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1576 return MCDisassembler::Success;
1579 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1582 const void *Decoder) {
1583 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1585 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1586 return MCDisassembler::Success;
1589 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1592 const void *Decoder) {
1593 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1595 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1596 return MCDisassembler::Success;
1599 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1602 const void *Decoder) {
1603 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1604 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1605 return MCDisassembler::Success;
1608 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1611 const void *Decoder) {
1612 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1613 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1614 return MCDisassembler::Success;
1617 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1620 const void *Decoder) {
1621 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1622 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1623 return MCDisassembler::Success;
1626 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1629 const void *Decoder) {
1630 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1631 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1632 return MCDisassembler::Success;
1635 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1638 const void *Decoder) {
1640 Inst.addOperand(MCOperand::CreateImm(1));
1641 else if (Value == 0x7)
1642 Inst.addOperand(MCOperand::CreateImm(-1));
1644 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1645 return MCDisassembler::Success;
1648 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1651 const void *Decoder) {
1652 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1653 return MCDisassembler::Success;
1656 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1659 const void *Decoder) {
1661 Inst.addOperand(MCOperand::CreateImm(-1));
1663 Inst.addOperand(MCOperand::CreateImm(Value));
1664 return MCDisassembler::Success;
1667 static DecodeStatus DecodeSimm4(MCInst &Inst,
1670 const void *Decoder) {
1671 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1672 return MCDisassembler::Success;
1675 static DecodeStatus DecodeSimm16(MCInst &Inst,
1678 const void *Decoder) {
1679 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1680 return MCDisassembler::Success;
1683 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1686 const void *Decoder) {
1687 // We add one to the immediate field as it was encoded as 'imm - 1'.
1688 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1689 return MCDisassembler::Success;
1692 static DecodeStatus DecodeInsSize(MCInst &Inst,
1695 const void *Decoder) {
1696 // First we need to grab the pos(lsb) from MCInst.
1697 int Pos = Inst.getOperand(2).getImm();
1698 int Size = (int) Insn - Pos + 1;
1699 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1700 return MCDisassembler::Success;
1703 static DecodeStatus DecodeExtSize(MCInst &Inst,
1706 const void *Decoder) {
1707 int Size = (int) Insn + 1;
1708 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1709 return MCDisassembler::Success;
1712 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1713 uint64_t Address, const void *Decoder) {
1714 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1715 return MCDisassembler::Success;
1718 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1719 uint64_t Address, const void *Decoder) {
1720 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1721 return MCDisassembler::Success;
1724 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1725 uint64_t Address, const void *Decoder) {
1726 int32_t DecodedValue;
1728 case 0: DecodedValue = 256; break;
1729 case 1: DecodedValue = 257; break;
1730 case 510: DecodedValue = -258; break;
1731 case 511: DecodedValue = -257; break;
1732 default: DecodedValue = SignExtend32<9>(Insn); break;
1734 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
1735 return MCDisassembler::Success;
1738 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1739 uint64_t Address, const void *Decoder) {
1740 // Insn must be >= 0, since it is unsigned that condition is always true.
1742 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1744 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1745 return MCDisassembler::Success;
1748 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1749 uint64_t Address, const void *Decoder) {
1750 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1751 return MCDisassembler::Success;
1754 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1757 const void *Decoder) {
1758 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1759 Mips::S6, Mips::FP};
1762 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1763 // Empty register lists are not allowed.
1765 return MCDisassembler::Fail;
1767 RegNum = RegLst & 0xf;
1768 for (unsigned i = 0; i < RegNum; i++)
1769 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1772 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1774 return MCDisassembler::Success;
1777 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1779 const void *Decoder) {
1780 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1781 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1782 unsigned RegNum = RegLst & 0x3;
1784 for (unsigned i = 0; i <= RegNum; i++)
1785 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1787 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1789 return MCDisassembler::Success;
1792 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
1793 uint64_t Address, const void *Decoder) {
1795 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
1799 return MCDisassembler::Fail;
1801 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1802 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1805 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1806 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1809 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1810 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1813 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1814 Inst.addOperand(MCOperand::CreateReg(Mips::S5));
1817 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1818 Inst.addOperand(MCOperand::CreateReg(Mips::S6));
1821 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1822 Inst.addOperand(MCOperand::CreateReg(Mips::A1));
1825 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1826 Inst.addOperand(MCOperand::CreateReg(Mips::A2));
1829 Inst.addOperand(MCOperand::CreateReg(Mips::A0));
1830 Inst.addOperand(MCOperand::CreateReg(Mips::A3));
1834 return MCDisassembler::Success;
1837 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1838 uint64_t Address, const void *Decoder) {
1839 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1840 return MCDisassembler::Success;