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 disasembler 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 disasembler 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 disasembler 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 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
232 // shifted left by 1 bit.
233 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
236 const void *Decoder);
238 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
239 // shifted left by 1 bit.
240 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
243 const void *Decoder);
245 static DecodeStatus DecodeMem(MCInst &Inst,
248 const void *Decoder);
250 static DecodeStatus DecodeCacheOp(MCInst &Inst,
253 const void *Decoder);
255 static DecodeStatus DecodeSyncI(MCInst &Inst,
258 const void *Decoder);
260 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
261 uint64_t Address, const void *Decoder);
263 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
266 const void *Decoder);
268 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
271 const void *Decoder);
273 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
276 const void *Decoder);
278 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
280 const void *Decoder);
282 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
284 const void *Decoder);
286 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
288 const void *Decoder);
290 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
293 const void *Decoder);
295 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
298 const void *Decoder);
300 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
303 const void *Decoder);
305 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
308 const void *Decoder);
310 static DecodeStatus DecodeSimm4(MCInst &Inst,
313 const void *Decoder);
315 static DecodeStatus DecodeSimm16(MCInst &Inst,
318 const void *Decoder);
320 // Decode the immediate field of an LSA instruction which
322 static DecodeStatus DecodeLSAImm(MCInst &Inst,
325 const void *Decoder);
327 static DecodeStatus DecodeInsSize(MCInst &Inst,
330 const void *Decoder);
332 static DecodeStatus DecodeExtSize(MCInst &Inst,
335 const void *Decoder);
337 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
338 uint64_t Address, const void *Decoder);
340 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
341 uint64_t Address, const void *Decoder);
343 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
344 uint64_t Address, const void *Decoder);
346 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
347 uint64_t Address, const void *Decoder);
349 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
350 uint64_t Address, const void *Decoder);
352 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
354 template <typename InsnType>
355 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
356 const void *Decoder);
358 template <typename InsnType>
360 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
361 const void *Decoder);
363 template <typename InsnType>
365 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
366 const void *Decoder);
368 template <typename InsnType>
370 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
371 const void *Decoder);
373 template <typename InsnType>
375 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
376 const void *Decoder);
378 template <typename InsnType>
380 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
381 const void *Decoder);
383 template <typename InsnType>
385 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
386 const void *Decoder);
388 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
390 const void *Decoder);
392 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
394 const void *Decoder);
397 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
401 static MCDisassembler *createMipsDisassembler(
403 const MCSubtargetInfo &STI,
405 return new MipsDisassembler(STI, Ctx, true);
408 static MCDisassembler *createMipselDisassembler(
410 const MCSubtargetInfo &STI,
412 return new MipsDisassembler(STI, Ctx, false);
415 static MCDisassembler *createMips64Disassembler(
417 const MCSubtargetInfo &STI,
419 return new Mips64Disassembler(STI, Ctx, true);
422 static MCDisassembler *createMips64elDisassembler(
424 const MCSubtargetInfo &STI,
426 return new Mips64Disassembler(STI, Ctx, false);
429 extern "C" void LLVMInitializeMipsDisassembler() {
430 // Register the disassembler.
431 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
432 createMipsDisassembler);
433 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
434 createMipselDisassembler);
435 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
436 createMips64Disassembler);
437 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
438 createMips64elDisassembler);
441 #include "MipsGenDisassemblerTables.inc"
443 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
444 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
445 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
446 return *(RegInfo->getRegClass(RC).begin() + RegNo);
449 template <typename InsnType>
450 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
451 const void *Decoder) {
452 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
453 // The size of the n field depends on the element size
454 // The register class also depends on this.
455 InsnType tmp = fieldFromInstruction(insn, 17, 5);
457 DecodeFN RegDecoder = nullptr;
458 if ((tmp & 0x18) == 0x00) { // INSVE_B
460 RegDecoder = DecodeMSA128BRegisterClass;
461 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
463 RegDecoder = DecodeMSA128HRegisterClass;
464 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
466 RegDecoder = DecodeMSA128WRegisterClass;
467 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
469 RegDecoder = DecodeMSA128DRegisterClass;
471 llvm_unreachable("Invalid encoding");
473 assert(NSize != 0 && RegDecoder != nullptr);
476 tmp = fieldFromInstruction(insn, 6, 5);
477 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
478 return MCDisassembler::Fail;
480 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
481 return MCDisassembler::Fail;
483 tmp = fieldFromInstruction(insn, 16, NSize);
484 MI.addOperand(MCOperand::CreateImm(tmp));
486 tmp = fieldFromInstruction(insn, 11, 5);
487 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
488 return MCDisassembler::Fail;
490 MI.addOperand(MCOperand::CreateImm(0));
492 return MCDisassembler::Success;
495 template <typename InsnType>
496 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
498 const void *Decoder) {
499 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
500 // (otherwise we would have matched the ADDI instruction from the earlier
504 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
506 // BEQZALC if rs == 0 && rt != 0
507 // BEQC if rs < rt && rs != 0
509 InsnType Rs = fieldFromInstruction(insn, 21, 5);
510 InsnType Rt = fieldFromInstruction(insn, 16, 5);
511 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
515 MI.setOpcode(Mips::BOVC);
517 } else if (Rs != 0 && Rs < Rt) {
518 MI.setOpcode(Mips::BEQC);
521 MI.setOpcode(Mips::BEQZALC);
524 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
527 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
529 MI.addOperand(MCOperand::CreateImm(Imm));
531 return MCDisassembler::Success;
534 template <typename InsnType>
535 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
537 const void *Decoder) {
538 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
539 // (otherwise we would have matched the ADDI instruction from the earlier
543 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
545 // BNEZALC if rs == 0 && rt != 0
546 // BNEC if rs < rt && rs != 0
548 InsnType Rs = fieldFromInstruction(insn, 21, 5);
549 InsnType Rt = fieldFromInstruction(insn, 16, 5);
550 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
554 MI.setOpcode(Mips::BNVC);
556 } else if (Rs != 0 && Rs < Rt) {
557 MI.setOpcode(Mips::BNEC);
560 MI.setOpcode(Mips::BNEZALC);
563 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
566 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
568 MI.addOperand(MCOperand::CreateImm(Imm));
570 return MCDisassembler::Success;
573 template <typename InsnType>
574 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
576 const void *Decoder) {
577 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
578 // (otherwise we would have matched the BLEZL instruction from the earlier
582 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
583 // Invalid if rs == 0
584 // BLEZC if rs == 0 && rt != 0
585 // BGEZC if rs == rt && rt != 0
586 // BGEC if rs != rt && rs != 0 && rt != 0
588 InsnType Rs = fieldFromInstruction(insn, 21, 5);
589 InsnType Rt = fieldFromInstruction(insn, 16, 5);
590 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
594 return MCDisassembler::Fail;
596 MI.setOpcode(Mips::BLEZC);
598 MI.setOpcode(Mips::BGEZC);
601 MI.setOpcode(Mips::BGEC);
605 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
608 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
611 MI.addOperand(MCOperand::CreateImm(Imm));
613 return MCDisassembler::Success;
616 template <typename InsnType>
617 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
619 const void *Decoder) {
620 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
621 // (otherwise we would have matched the BGTZL instruction from the earlier
625 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
626 // Invalid if rs == 0
627 // BGTZC if rs == 0 && rt != 0
628 // BLTZC if rs == rt && rt != 0
629 // BLTC if rs != rt && rs != 0 && rt != 0
633 InsnType Rs = fieldFromInstruction(insn, 21, 5);
634 InsnType Rt = fieldFromInstruction(insn, 16, 5);
635 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
638 return MCDisassembler::Fail;
640 MI.setOpcode(Mips::BGTZC);
642 MI.setOpcode(Mips::BLTZC);
644 MI.setOpcode(Mips::BLTC);
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
652 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
655 MI.addOperand(MCOperand::CreateImm(Imm));
657 return MCDisassembler::Success;
660 template <typename InsnType>
661 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
663 const void *Decoder) {
664 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
665 // (otherwise we would have matched the BGTZ instruction from the earlier
669 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
671 // BGTZALC if rs == 0 && rt != 0
672 // BLTZALC if rs != 0 && rs == rt
673 // BLTUC if rs != 0 && rs != rt
675 InsnType Rs = fieldFromInstruction(insn, 21, 5);
676 InsnType Rt = fieldFromInstruction(insn, 16, 5);
677 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
682 MI.setOpcode(Mips::BGTZ);
684 } else if (Rs == 0) {
685 MI.setOpcode(Mips::BGTZALC);
687 } else if (Rs == Rt) {
688 MI.setOpcode(Mips::BLTZALC);
691 MI.setOpcode(Mips::BLTUC);
697 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
701 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
704 MI.addOperand(MCOperand::CreateImm(Imm));
706 return MCDisassembler::Success;
709 template <typename InsnType>
710 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
712 const void *Decoder) {
713 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
714 // (otherwise we would have matched the BLEZL instruction from the earlier
718 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
719 // Invalid if rs == 0
720 // BLEZALC if rs == 0 && rt != 0
721 // BGEZALC if rs == rt && rt != 0
722 // BGEUC if rs != rt && rs != 0 && rt != 0
724 InsnType Rs = fieldFromInstruction(insn, 21, 5);
725 InsnType Rt = fieldFromInstruction(insn, 16, 5);
726 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
730 return MCDisassembler::Fail;
732 MI.setOpcode(Mips::BLEZALC);
734 MI.setOpcode(Mips::BGEZALC);
737 MI.setOpcode(Mips::BGEUC);
741 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
743 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
746 MI.addOperand(MCOperand::CreateImm(Imm));
748 return MCDisassembler::Success;
751 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
752 /// according to the given endianess.
753 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
754 uint64_t &Size, uint32_t &Insn,
756 // We want to read exactly 2 Bytes of data.
757 if (Bytes.size() < 2) {
759 return MCDisassembler::Fail;
763 Insn = (Bytes[0] << 8) | Bytes[1];
765 Insn = (Bytes[1] << 8) | Bytes[0];
768 return MCDisassembler::Success;
771 /// Read four bytes from the ArrayRef and return 32 bit word sorted
772 /// according to the given endianess
773 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
774 uint64_t &Size, uint32_t &Insn,
775 bool IsBigEndian, bool IsMicroMips) {
776 // We want to read exactly 4 Bytes of data.
777 if (Bytes.size() < 4) {
779 return MCDisassembler::Fail;
782 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
783 // always precede the low 16 bits in the instruction stream (that is, they
784 // are placed at lower addresses in the instruction stream).
786 // microMIPS byte ordering:
787 // Big-endian: 0 | 1 | 2 | 3
788 // Little-endian: 1 | 0 | 3 | 2
791 // Encoded as a big-endian 32-bit word in the stream.
793 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
796 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
799 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
804 return MCDisassembler::Success;
807 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
808 ArrayRef<uint8_t> Bytes,
810 raw_ostream &VStream,
811 raw_ostream &CStream) const {
816 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
818 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
819 // Calling the auto-generated decoder function.
820 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
822 if (Result != MCDisassembler::Fail) {
827 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
828 if (Result == MCDisassembler::Fail)
829 return MCDisassembler::Fail;
831 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
832 // Calling the auto-generated decoder function.
833 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
835 if (Result != MCDisassembler::Fail) {
839 return MCDisassembler::Fail;
842 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
843 if (Result == MCDisassembler::Fail)
844 return MCDisassembler::Fail;
847 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
849 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
850 if (Result != MCDisassembler::Fail) {
856 if (hasMips32r6() && isGP64()) {
857 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
858 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
860 if (Result != MCDisassembler::Fail) {
867 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
868 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
870 if (Result != MCDisassembler::Fail) {
876 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
877 // Calling the auto-generated decoder function.
879 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
880 if (Result != MCDisassembler::Fail) {
885 return MCDisassembler::Fail;
888 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
889 ArrayRef<uint8_t> Bytes,
891 raw_ostream &VStream,
892 raw_ostream &CStream) const {
895 DecodeStatus Result =
896 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
897 if (Result == MCDisassembler::Fail)
898 return MCDisassembler::Fail;
900 // Calling the auto-generated decoder function.
902 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
903 if (Result != MCDisassembler::Fail) {
907 // If we fail to decode in Mips64 decoder space we can try in Mips32
909 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
910 if (Result != MCDisassembler::Fail) {
915 return MCDisassembler::Fail;
918 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
921 const void *Decoder) {
923 return MCDisassembler::Fail;
927 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
930 const void *Decoder) {
933 return MCDisassembler::Fail;
935 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
936 Inst.addOperand(MCOperand::CreateReg(Reg));
937 return MCDisassembler::Success;
940 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
943 const void *Decoder) {
945 return MCDisassembler::Fail;
946 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
947 Inst.addOperand(MCOperand::CreateReg(Reg));
948 return MCDisassembler::Success;
951 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
954 const void *Decoder) {
956 return MCDisassembler::Fail;
957 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, 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)->isGP64Bit())
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 DecodeSyncI(MCInst &Inst,
1090 const void *Decoder) {
1091 int Offset = SignExtend32<16>(Insn & 0xffff);
1092 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1094 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1096 Inst.addOperand(MCOperand::CreateReg(Base));
1097 Inst.addOperand(MCOperand::CreateImm(Offset));
1099 return MCDisassembler::Success;
1102 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1103 uint64_t Address, const void *Decoder) {
1104 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1105 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1106 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1108 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1109 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1111 Inst.addOperand(MCOperand::CreateReg(Reg));
1112 Inst.addOperand(MCOperand::CreateReg(Base));
1114 // The immediate field of an LD/ST instruction is scaled which means it must
1115 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1121 switch(Inst.getOpcode())
1124 assert (0 && "Unexpected instruction");
1125 return MCDisassembler::Fail;
1129 Inst.addOperand(MCOperand::CreateImm(Offset));
1133 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1137 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1141 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1145 return MCDisassembler::Success;
1148 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1151 const void *Decoder) {
1152 unsigned Offset = Insn & 0xf;
1153 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1154 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1156 switch (Inst.getOpcode()) {
1157 case Mips::LBU16_MM:
1158 case Mips::LHU16_MM:
1160 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1161 == MCDisassembler::Fail)
1162 return MCDisassembler::Fail;
1167 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1168 == MCDisassembler::Fail)
1169 return MCDisassembler::Fail;
1173 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1174 == MCDisassembler::Fail)
1175 return MCDisassembler::Fail;
1177 switch (Inst.getOpcode()) {
1178 case Mips::LBU16_MM:
1180 Inst.addOperand(MCOperand::CreateImm(-1));
1182 Inst.addOperand(MCOperand::CreateImm(Offset));
1185 Inst.addOperand(MCOperand::CreateImm(Offset));
1187 case Mips::LHU16_MM:
1189 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1193 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1197 return MCDisassembler::Success;
1200 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1203 const void *Decoder) {
1204 int Offset = SignExtend32<12>(Insn & 0x0fff);
1205 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1206 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1208 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1209 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1211 switch (Inst.getOpcode()) {
1212 case Mips::SWM32_MM:
1213 case Mips::LWM32_MM:
1214 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1215 == MCDisassembler::Fail)
1216 return MCDisassembler::Fail;
1217 Inst.addOperand(MCOperand::CreateReg(Base));
1218 Inst.addOperand(MCOperand::CreateImm(Offset));
1221 Inst.addOperand(MCOperand::CreateReg(Reg));
1224 Inst.addOperand(MCOperand::CreateReg(Reg));
1225 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1226 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1228 Inst.addOperand(MCOperand::CreateReg(Base));
1229 Inst.addOperand(MCOperand::CreateImm(Offset));
1232 return MCDisassembler::Success;
1235 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1238 const void *Decoder) {
1239 int Offset = SignExtend32<16>(Insn & 0xffff);
1240 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1241 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1243 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1244 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1246 Inst.addOperand(MCOperand::CreateReg(Reg));
1247 Inst.addOperand(MCOperand::CreateReg(Base));
1248 Inst.addOperand(MCOperand::CreateImm(Offset));
1250 return MCDisassembler::Success;
1253 static DecodeStatus DecodeFMem(MCInst &Inst,
1256 const void *Decoder) {
1257 int Offset = SignExtend32<16>(Insn & 0xffff);
1258 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1259 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1261 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1262 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1264 Inst.addOperand(MCOperand::CreateReg(Reg));
1265 Inst.addOperand(MCOperand::CreateReg(Base));
1266 Inst.addOperand(MCOperand::CreateImm(Offset));
1268 return MCDisassembler::Success;
1271 static DecodeStatus DecodeFMem2(MCInst &Inst,
1274 const void *Decoder) {
1275 int Offset = SignExtend32<16>(Insn & 0xffff);
1276 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1277 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1279 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1280 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1282 Inst.addOperand(MCOperand::CreateReg(Reg));
1283 Inst.addOperand(MCOperand::CreateReg(Base));
1284 Inst.addOperand(MCOperand::CreateImm(Offset));
1286 return MCDisassembler::Success;
1289 static DecodeStatus DecodeFMem3(MCInst &Inst,
1292 const void *Decoder) {
1293 int Offset = SignExtend32<16>(Insn & 0xffff);
1294 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1295 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1297 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1298 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1300 Inst.addOperand(MCOperand::CreateReg(Reg));
1301 Inst.addOperand(MCOperand::CreateReg(Base));
1302 Inst.addOperand(MCOperand::CreateImm(Offset));
1304 return MCDisassembler::Success;
1307 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1310 const void *Decoder) {
1311 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1312 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1313 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1315 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1316 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1318 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1319 Inst.addOperand(MCOperand::CreateReg(Rt));
1322 Inst.addOperand(MCOperand::CreateReg(Rt));
1323 Inst.addOperand(MCOperand::CreateReg(Base));
1324 Inst.addOperand(MCOperand::CreateImm(Offset));
1326 return MCDisassembler::Success;
1329 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1332 const void *Decoder) {
1333 // Currently only hardware register 29 is supported.
1335 return MCDisassembler::Fail;
1336 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1337 return MCDisassembler::Success;
1340 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1343 const void *Decoder) {
1344 if (RegNo > 30 || RegNo %2)
1345 return MCDisassembler::Fail;
1348 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1349 Inst.addOperand(MCOperand::CreateReg(Reg));
1350 return MCDisassembler::Success;
1353 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1356 const void *Decoder) {
1358 return MCDisassembler::Fail;
1360 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1361 Inst.addOperand(MCOperand::CreateReg(Reg));
1362 return MCDisassembler::Success;
1365 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1368 const void *Decoder) {
1370 return MCDisassembler::Fail;
1372 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1373 Inst.addOperand(MCOperand::CreateReg(Reg));
1374 return MCDisassembler::Success;
1377 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1380 const void *Decoder) {
1382 return MCDisassembler::Fail;
1384 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1385 Inst.addOperand(MCOperand::CreateReg(Reg));
1386 return MCDisassembler::Success;
1389 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1392 const void *Decoder) {
1394 return MCDisassembler::Fail;
1396 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1397 Inst.addOperand(MCOperand::CreateReg(Reg));
1398 return MCDisassembler::Success;
1401 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1404 const void *Decoder) {
1406 return MCDisassembler::Fail;
1408 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1409 Inst.addOperand(MCOperand::CreateReg(Reg));
1410 return MCDisassembler::Success;
1413 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1416 const void *Decoder) {
1418 return MCDisassembler::Fail;
1420 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1421 Inst.addOperand(MCOperand::CreateReg(Reg));
1422 return MCDisassembler::Success;
1425 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1428 const void *Decoder) {
1430 return MCDisassembler::Fail;
1432 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1433 Inst.addOperand(MCOperand::CreateReg(Reg));
1434 return MCDisassembler::Success;
1437 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1440 const void *Decoder) {
1442 return MCDisassembler::Fail;
1444 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1445 Inst.addOperand(MCOperand::CreateReg(Reg));
1446 return MCDisassembler::Success;
1449 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1452 const void *Decoder) {
1454 return MCDisassembler::Fail;
1456 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1457 Inst.addOperand(MCOperand::CreateReg(Reg));
1458 return MCDisassembler::Success;
1461 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1464 const void *Decoder) {
1465 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1466 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1467 return MCDisassembler::Success;
1470 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1473 const void *Decoder) {
1475 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1476 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1477 return MCDisassembler::Success;
1480 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1483 const void *Decoder) {
1484 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1486 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1487 return MCDisassembler::Success;
1490 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1493 const void *Decoder) {
1494 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1496 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1497 return MCDisassembler::Success;
1500 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1503 const void *Decoder) {
1504 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1505 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1506 return MCDisassembler::Success;
1509 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1512 const void *Decoder) {
1513 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1514 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1515 return MCDisassembler::Success;
1518 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1521 const void *Decoder) {
1523 Inst.addOperand(MCOperand::CreateImm(1));
1524 else if (Value == 0x7)
1525 Inst.addOperand(MCOperand::CreateImm(-1));
1527 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1528 return MCDisassembler::Success;
1531 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1534 const void *Decoder) {
1535 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1536 return MCDisassembler::Success;
1539 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1542 const void *Decoder) {
1544 Inst.addOperand(MCOperand::CreateImm(-1));
1546 Inst.addOperand(MCOperand::CreateImm(Value));
1547 return MCDisassembler::Success;
1550 static DecodeStatus DecodeSimm4(MCInst &Inst,
1553 const void *Decoder) {
1554 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1555 return MCDisassembler::Success;
1558 static DecodeStatus DecodeSimm16(MCInst &Inst,
1561 const void *Decoder) {
1562 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1563 return MCDisassembler::Success;
1566 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1569 const void *Decoder) {
1570 // We add one to the immediate field as it was encoded as 'imm - 1'.
1571 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1572 return MCDisassembler::Success;
1575 static DecodeStatus DecodeInsSize(MCInst &Inst,
1578 const void *Decoder) {
1579 // First we need to grab the pos(lsb) from MCInst.
1580 int Pos = Inst.getOperand(2).getImm();
1581 int Size = (int) Insn - Pos + 1;
1582 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1583 return MCDisassembler::Success;
1586 static DecodeStatus DecodeExtSize(MCInst &Inst,
1589 const void *Decoder) {
1590 int Size = (int) Insn + 1;
1591 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1592 return MCDisassembler::Success;
1595 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1596 uint64_t Address, const void *Decoder) {
1597 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1598 return MCDisassembler::Success;
1601 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1602 uint64_t Address, const void *Decoder) {
1603 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1604 return MCDisassembler::Success;
1607 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1608 uint64_t Address, const void *Decoder) {
1609 int32_t DecodedValue;
1611 case 0: DecodedValue = 256; break;
1612 case 1: DecodedValue = 257; break;
1613 case 510: DecodedValue = -258; break;
1614 case 511: DecodedValue = -257; break;
1615 default: DecodedValue = SignExtend32<9>(Insn); break;
1617 Inst.addOperand(MCOperand::CreateImm(DecodedValue << 2));
1618 return MCDisassembler::Success;
1621 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1622 uint64_t Address, const void *Decoder) {
1623 // Insn must be >= 0, since it is unsigned that condition is always true.
1625 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1627 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1628 return MCDisassembler::Success;
1631 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1632 uint64_t Address, const void *Decoder) {
1633 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1634 return MCDisassembler::Success;
1637 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1640 const void *Decoder) {
1641 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1642 Mips::S6, Mips::FP};
1645 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1646 // Empty register lists are not allowed.
1648 return MCDisassembler::Fail;
1650 RegNum = RegLst & 0xf;
1651 for (unsigned i = 0; i < RegNum; i++)
1652 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1655 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1657 return MCDisassembler::Success;
1660 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1662 const void *Decoder) {
1663 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1666 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1667 // Empty register lists are not allowed.
1669 return MCDisassembler::Fail;
1671 RegNum = RegLst & 0x3;
1672 for (unsigned i = 0; i < RegNum - 1; i++)
1673 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1675 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1677 return MCDisassembler::Success;