1 //===- MipsDisassembler.cpp - Disassembler for Mips -----------------------===//
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 "llvm/ADT/ArrayRef.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
18 #include "llvm/MC/MCFixedLenDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/TargetRegistry.h"
33 #define DEBUG_TYPE "mips-disassembler"
35 typedef MCDisassembler::DecodeStatus DecodeStatus;
39 class MipsDisassembler : public MCDisassembler {
44 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
45 : MCDisassembler(STI, Ctx),
46 IsMicroMips(STI.getFeatureBits()[Mips::FeatureMicroMips]),
47 IsBigEndian(IsBigEndian) {}
49 bool hasMips2() const { return STI.getFeatureBits()[Mips::FeatureMips2]; }
50 bool hasMips3() const { return STI.getFeatureBits()[Mips::FeatureMips3]; }
51 bool hasMips32() const { return STI.getFeatureBits()[Mips::FeatureMips32]; }
53 bool hasMips32r6() const {
54 return STI.getFeatureBits()[Mips::FeatureMips32r6];
57 bool isFP64() const { return STI.getFeatureBits()[Mips::FeatureFP64Bit]; }
59 bool isGP64() const { return STI.getFeatureBits()[Mips::FeatureGP64Bit]; }
61 bool isPTR64() const { return STI.getFeatureBits()[Mips::FeaturePTR64Bit]; }
63 bool hasCnMips() const { return STI.getFeatureBits()[Mips::FeatureCnMips]; }
65 bool hasCOP3() const {
66 // Only present in MIPS-I and MIPS-II
67 return !hasMips32() && !hasMips3();
70 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
71 ArrayRef<uint8_t> Bytes, uint64_t Address,
73 raw_ostream &CStream) const override;
76 } // end anonymous namespace
78 // Forward declare these because the autogenerated code will reference them.
79 // Definitions are further down.
80 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
85 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
90 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
95 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
100 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
103 const void *Decoder);
105 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
108 const void *Decoder);
110 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
113 const void *Decoder);
115 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
118 const void *Decoder);
120 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
123 const void *Decoder);
125 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
128 const void *Decoder);
130 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
133 const void *Decoder);
135 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
138 const void *Decoder);
140 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
142 const void *Decoder);
144 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
147 const void *Decoder);
149 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
152 const void *Decoder);
154 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
157 const void *Decoder);
159 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
162 const void *Decoder);
164 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
167 const void *Decoder);
169 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
172 const void *Decoder);
174 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
177 const void *Decoder);
179 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
182 const void *Decoder);
184 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
187 const void *Decoder);
189 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
192 const void *Decoder);
194 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
197 const void *Decoder);
199 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
202 const void *Decoder);
204 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
207 const void *Decoder);
209 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
212 const void *Decoder);
214 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
217 const void *Decoder);
219 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
222 const void *Decoder);
224 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
227 const void *Decoder);
229 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
232 const void *Decoder);
234 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
235 // shifted left by 1 bit.
236 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
239 const void *Decoder);
241 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
242 // shifted left by 1 bit.
243 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
246 const void *Decoder);
248 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
249 // shifted left by 1 bit.
250 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
253 const void *Decoder);
255 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
256 // shifted left by 1 bit.
257 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
260 const void *Decoder);
262 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
263 // shifted left by 1 bit.
264 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
267 const void *Decoder);
269 static DecodeStatus DecodeMem(MCInst &Inst,
272 const void *Decoder);
274 static DecodeStatus DecodeMemEVA(MCInst &Inst,
277 const void *Decoder);
279 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
282 const void *Decoder);
284 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
287 const void *Decoder);
289 static DecodeStatus DecodeCacheOp(MCInst &Inst,
292 const void *Decoder);
294 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
297 const void *Decoder);
299 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
302 const void *Decoder);
304 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
307 const void *Decoder);
309 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
312 const void *Decoder);
314 static DecodeStatus DecodeSyncI(MCInst &Inst,
317 const void *Decoder);
319 static DecodeStatus DecodeSynciR6(MCInst &Inst,
322 const void *Decoder);
324 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
325 uint64_t Address, const void *Decoder);
327 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
330 const void *Decoder);
332 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
335 const void *Decoder);
337 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
340 const void *Decoder);
342 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
345 const void *Decoder);
347 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
350 const void *Decoder);
352 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
355 const void *Decoder);
357 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
360 const void *Decoder);
362 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
364 const void *Decoder);
366 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
368 const void *Decoder);
370 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
372 const void *Decoder);
374 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
376 const void *Decoder);
378 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
380 const void *Decoder);
382 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
384 const void *Decoder);
386 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
389 const void *Decoder);
391 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
394 const void *Decoder);
396 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
399 const void *Decoder);
401 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
404 const void *Decoder);
406 template <unsigned Bits, int Offset, int Scale>
407 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
409 const void *Decoder);
411 template <unsigned Bits, int Offset>
412 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
414 const void *Decoder) {
415 return DecodeUImmWithOffsetAndScale<Bits, Offset, 1>(Inst, Value, Address,
419 template <unsigned Bits, int Offset = 0, int ScaleBy = 1>
420 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
422 const void *Decoder);
424 static DecodeStatus DecodeInsSize(MCInst &Inst,
427 const void *Decoder);
429 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
430 uint64_t Address, const void *Decoder);
432 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
433 uint64_t Address, const void *Decoder);
435 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
436 uint64_t Address, const void *Decoder);
438 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
439 uint64_t Address, const void *Decoder);
441 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
442 uint64_t Address, const void *Decoder);
444 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
446 template <typename InsnType>
447 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
448 const void *Decoder);
450 template <typename InsnType>
451 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
452 const void *Decoder);
454 template <typename InsnType>
455 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
456 const void *Decoder);
458 template <typename InsnType>
459 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
460 const void *Decoder);
462 template <typename InsnType>
463 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
464 const void *Decoder);
466 template <typename InsnType>
468 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
469 const void *Decoder);
471 template <typename InsnType>
473 DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
474 const void *Decoder);
476 template <typename InsnType>
478 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
479 const void *Decoder);
481 template <typename InsnType>
483 DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
484 const void *Decoder);
486 template <typename InsnType>
488 DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
489 const void *Decoder);
491 template <typename InsnType>
493 DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
494 const void *Decoder);
496 template <typename InsnType>
498 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
499 const void *Decoder);
501 template <typename InsnType>
503 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
504 const void *Decoder);
506 template <typename InsnType>
508 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
509 const void *Decoder);
511 template <typename InsnType>
513 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
514 const void *Decoder);
516 template <typename InsnType>
518 DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
519 const void *Decoder);
521 template <typename InsnType>
523 DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
524 const void *Decoder);
526 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
528 const void *Decoder);
530 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
532 const void *Decoder);
534 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
536 const void *Decoder);
540 Target &getTheMipselTarget();
541 Target &getTheMipsTarget();
542 Target &getTheMips64Target();
543 Target &getTheMips64elTarget();
545 } // end namespace llvm
547 static MCDisassembler *createMipsDisassembler(
549 const MCSubtargetInfo &STI,
551 return new MipsDisassembler(STI, Ctx, true);
554 static MCDisassembler *createMipselDisassembler(
556 const MCSubtargetInfo &STI,
558 return new MipsDisassembler(STI, Ctx, false);
561 extern "C" void LLVMInitializeMipsDisassembler() {
562 // Register the disassembler.
563 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
564 createMipsDisassembler);
565 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
566 createMipselDisassembler);
567 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
568 createMipsDisassembler);
569 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
570 createMipselDisassembler);
573 #include "MipsGenDisassemblerTables.inc"
575 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
576 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
577 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
578 return *(RegInfo->getRegClass(RC).begin() + RegNo);
581 template <typename InsnType>
582 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
583 const void *Decoder) {
584 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
585 // The size of the n field depends on the element size
586 // The register class also depends on this.
587 InsnType tmp = fieldFromInstruction(insn, 17, 5);
589 DecodeFN RegDecoder = nullptr;
590 if ((tmp & 0x18) == 0x00) { // INSVE_B
592 RegDecoder = DecodeMSA128BRegisterClass;
593 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
595 RegDecoder = DecodeMSA128HRegisterClass;
596 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
598 RegDecoder = DecodeMSA128WRegisterClass;
599 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
601 RegDecoder = DecodeMSA128DRegisterClass;
603 llvm_unreachable("Invalid encoding");
605 assert(NSize != 0 && RegDecoder != nullptr);
608 tmp = fieldFromInstruction(insn, 6, 5);
609 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
610 return MCDisassembler::Fail;
612 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
613 return MCDisassembler::Fail;
615 tmp = fieldFromInstruction(insn, 16, NSize);
616 MI.addOperand(MCOperand::createImm(tmp));
618 tmp = fieldFromInstruction(insn, 11, 5);
619 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
620 return MCDisassembler::Fail;
622 MI.addOperand(MCOperand::createImm(0));
624 return MCDisassembler::Success;
627 template <typename InsnType>
628 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
629 const void *Decoder) {
630 InsnType Rs = fieldFromInstruction(insn, 16, 5);
631 InsnType Imm = fieldFromInstruction(insn, 0, 16);
632 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
634 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
636 MI.addOperand(MCOperand::createImm(Imm));
638 return MCDisassembler::Success;
641 template <typename InsnType>
642 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
643 const void *Decoder) {
644 InsnType Rs = fieldFromInstruction(insn, 21, 5);
645 InsnType Imm = fieldFromInstruction(insn, 0, 16);
646 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
648 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
650 MI.addOperand(MCOperand::createImm(Imm));
652 return MCDisassembler::Success;
655 template <typename InsnType>
656 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
658 const void *Decoder) {
659 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
660 // (otherwise we would have matched the ADDI instruction from the earlier
664 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
666 // BEQZALC if rs == 0 && rt != 0
667 // BEQC if rs < rt && rs != 0
669 InsnType Rs = fieldFromInstruction(insn, 21, 5);
670 InsnType Rt = fieldFromInstruction(insn, 16, 5);
671 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
675 MI.setOpcode(Mips::BOVC);
677 } else if (Rs != 0 && Rs < Rt) {
678 MI.setOpcode(Mips::BEQC);
681 MI.setOpcode(Mips::BEQZALC);
684 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
687 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
689 MI.addOperand(MCOperand::createImm(Imm));
691 return MCDisassembler::Success;
694 template <typename InsnType>
695 static DecodeStatus DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn,
697 const void *Decoder) {
698 InsnType Rt = fieldFromInstruction(insn, 21, 5);
699 InsnType Rs = fieldFromInstruction(insn, 16, 5);
703 MI.setOpcode(Mips::BOVC_MMR6);
704 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
706 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
708 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
709 } else if (Rs != 0 && Rs < Rt) {
710 MI.setOpcode(Mips::BEQC_MMR6);
711 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
713 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
715 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
717 MI.setOpcode(Mips::BEQZALC_MMR6);
718 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
720 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
723 MI.addOperand(MCOperand::createImm(Imm));
725 return MCDisassembler::Success;
728 template <typename InsnType>
729 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
731 const void *Decoder) {
732 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
733 // (otherwise we would have matched the ADDI instruction from the earlier
737 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
739 // BNEZALC if rs == 0 && rt != 0
740 // BNEC if rs < rt && rs != 0
742 InsnType Rs = fieldFromInstruction(insn, 21, 5);
743 InsnType Rt = fieldFromInstruction(insn, 16, 5);
744 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
748 MI.setOpcode(Mips::BNVC);
750 } else if (Rs != 0 && Rs < Rt) {
751 MI.setOpcode(Mips::BNEC);
754 MI.setOpcode(Mips::BNEZALC);
757 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
760 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
762 MI.addOperand(MCOperand::createImm(Imm));
764 return MCDisassembler::Success;
767 template <typename InsnType>
768 static DecodeStatus DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn,
770 const void *Decoder) {
771 InsnType Rt = fieldFromInstruction(insn, 21, 5);
772 InsnType Rs = fieldFromInstruction(insn, 16, 5);
776 MI.setOpcode(Mips::BNVC_MMR6);
777 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
779 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
781 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
782 } else if (Rs != 0 && Rs < Rt) {
783 MI.setOpcode(Mips::BNEC_MMR6);
784 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
786 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
788 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
790 MI.setOpcode(Mips::BNEZALC_MMR6);
791 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
793 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
796 MI.addOperand(MCOperand::createImm(Imm));
798 return MCDisassembler::Success;
801 template <typename InsnType>
802 static DecodeStatus DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn,
804 const void *Decoder) {
806 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
807 // Invalid if rt == 0
808 // BGTZC_MMR6 if rs == 0 && rt != 0
809 // BLTZC_MMR6 if rs == rt && rt != 0
810 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
812 InsnType Rt = fieldFromInstruction(insn, 21, 5);
813 InsnType Rs = fieldFromInstruction(insn, 16, 5);
814 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
818 return MCDisassembler::Fail;
820 MI.setOpcode(Mips::BGTZC_MMR6);
822 MI.setOpcode(Mips::BLTZC_MMR6);
824 MI.setOpcode(Mips::BLTC_MMR6);
829 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
832 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
835 MI.addOperand(MCOperand::createImm(Imm));
837 return MCDisassembler::Success;
840 template <typename InsnType>
841 static DecodeStatus DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn,
843 const void *Decoder) {
845 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
846 // Invalid if rt == 0
847 // BLEZC_MMR6 if rs == 0 && rt != 0
848 // BGEZC_MMR6 if rs == rt && rt != 0
849 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
851 InsnType Rt = fieldFromInstruction(insn, 21, 5);
852 InsnType Rs = fieldFromInstruction(insn, 16, 5);
853 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
857 return MCDisassembler::Fail;
859 MI.setOpcode(Mips::BLEZC_MMR6);
861 MI.setOpcode(Mips::BGEZC_MMR6);
864 MI.setOpcode(Mips::BGEC_MMR6);
868 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
871 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
874 MI.addOperand(MCOperand::createImm(Imm));
876 return MCDisassembler::Success;
879 template <typename InsnType>
880 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
882 const void *Decoder) {
883 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
884 // (otherwise we would have matched the BLEZL instruction from the earlier
888 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
889 // Invalid if rs == 0
890 // BLEZC if rs == 0 && rt != 0
891 // BGEZC if rs == rt && rt != 0
892 // BGEC if rs != rt && rs != 0 && rt != 0
894 InsnType Rs = fieldFromInstruction(insn, 21, 5);
895 InsnType Rt = fieldFromInstruction(insn, 16, 5);
896 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
900 return MCDisassembler::Fail;
902 MI.setOpcode(Mips::BLEZC);
904 MI.setOpcode(Mips::BGEZC);
907 MI.setOpcode(Mips::BGEC);
911 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
914 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
917 MI.addOperand(MCOperand::createImm(Imm));
919 return MCDisassembler::Success;
922 template <typename InsnType>
923 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
925 const void *Decoder) {
926 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
927 // (otherwise we would have matched the BGTZL instruction from the earlier
931 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
932 // Invalid if rs == 0
933 // BGTZC if rs == 0 && rt != 0
934 // BLTZC if rs == rt && rt != 0
935 // BLTC if rs != rt && rs != 0 && rt != 0
939 InsnType Rs = fieldFromInstruction(insn, 21, 5);
940 InsnType Rt = fieldFromInstruction(insn, 16, 5);
941 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
944 return MCDisassembler::Fail;
946 MI.setOpcode(Mips::BGTZC);
948 MI.setOpcode(Mips::BLTZC);
950 MI.setOpcode(Mips::BLTC);
955 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
958 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
961 MI.addOperand(MCOperand::createImm(Imm));
963 return MCDisassembler::Success;
966 template <typename InsnType>
967 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
969 const void *Decoder) {
970 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
971 // (otherwise we would have matched the BGTZ instruction from the earlier
975 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
977 // BGTZALC if rs == 0 && rt != 0
978 // BLTZALC if rs != 0 && rs == rt
979 // BLTUC if rs != 0 && rs != rt
981 InsnType Rs = fieldFromInstruction(insn, 21, 5);
982 InsnType Rt = fieldFromInstruction(insn, 16, 5);
983 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
988 MI.setOpcode(Mips::BGTZ);
990 } else if (Rs == 0) {
991 MI.setOpcode(Mips::BGTZALC);
993 } else if (Rs == Rt) {
994 MI.setOpcode(Mips::BLTZALC);
997 MI.setOpcode(Mips::BLTUC);
1003 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1007 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1010 MI.addOperand(MCOperand::createImm(Imm));
1012 return MCDisassembler::Success;
1015 template <typename InsnType>
1016 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
1018 const void *Decoder) {
1019 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1020 // (otherwise we would have matched the BLEZL instruction from the earlier
1024 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1025 // Invalid if rs == 0
1026 // BLEZALC if rs == 0 && rt != 0
1027 // BGEZALC if rs == rt && rt != 0
1028 // BGEUC if rs != rt && rs != 0 && rt != 0
1030 InsnType Rs = fieldFromInstruction(insn, 21, 5);
1031 InsnType Rt = fieldFromInstruction(insn, 16, 5);
1032 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1036 return MCDisassembler::Fail;
1038 MI.setOpcode(Mips::BLEZALC);
1040 MI.setOpcode(Mips::BGEZALC);
1043 MI.setOpcode(Mips::BGEUC);
1047 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1049 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1052 MI.addOperand(MCOperand::createImm(Imm));
1054 return MCDisassembler::Success;
1057 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1058 /// according to the given endianness.
1059 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
1060 uint64_t &Size, uint32_t &Insn,
1062 // We want to read exactly 2 Bytes of data.
1063 if (Bytes.size() < 2) {
1065 return MCDisassembler::Fail;
1069 Insn = (Bytes[0] << 8) | Bytes[1];
1071 Insn = (Bytes[1] << 8) | Bytes[0];
1074 return MCDisassembler::Success;
1077 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1078 /// according to the given endianness.
1079 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
1080 uint64_t &Size, uint32_t &Insn,
1081 bool IsBigEndian, bool IsMicroMips) {
1082 // We want to read exactly 4 Bytes of data.
1083 if (Bytes.size() < 4) {
1085 return MCDisassembler::Fail;
1088 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1089 // always precede the low 16 bits in the instruction stream (that is, they
1090 // are placed at lower addresses in the instruction stream).
1092 // microMIPS byte ordering:
1093 // Big-endian: 0 | 1 | 2 | 3
1094 // Little-endian: 1 | 0 | 3 | 2
1097 // Encoded as a big-endian 32-bit word in the stream.
1099 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
1102 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
1105 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
1110 return MCDisassembler::Success;
1113 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1114 ArrayRef<uint8_t> Bytes,
1116 raw_ostream &VStream,
1117 raw_ostream &CStream) const {
1119 DecodeStatus Result;
1123 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
1124 if (Result == MCDisassembler::Fail)
1125 return MCDisassembler::Fail;
1127 if (hasMips32r6()) {
1128 DEBUG(dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1129 // Calling the auto-generated decoder function for microMIPS32R6
1130 // (and microMIPS64R6) 16-bit instructions.
1131 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
1132 Address, this, STI);
1133 if (Result != MCDisassembler::Fail) {
1139 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1140 // Calling the auto-generated decoder function for microMIPS 16-bit
1142 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
1144 if (Result != MCDisassembler::Fail) {
1149 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
1150 if (Result == MCDisassembler::Fail)
1151 return MCDisassembler::Fail;
1153 if (hasMips32r6()) {
1154 DEBUG(dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1155 // Calling the auto-generated decoder function.
1156 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
1158 if (Result != MCDisassembler::Fail) {
1164 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1165 // Calling the auto-generated decoder function.
1166 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
1168 if (Result != MCDisassembler::Fail) {
1173 if (hasMips32r6() && isFP64()) {
1174 DEBUG(dbgs() << "Trying MicroMips32r6FP64 table (32-bit opcodes):\n");
1175 Result = decodeInstruction(DecoderTableMicroMips32r6FP6432, Instr, Insn,
1176 Address, this, STI);
1177 if (Result != MCDisassembler::Fail) {
1183 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1184 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1185 // could form a valid instruction. The two bytes we rejected as an
1186 // instruction could have actually beeen an inline constant pool that is
1187 // unconditionally branched over.
1189 return MCDisassembler::Fail;
1192 // Attempt to read the instruction so that we can attempt to decode it. If
1193 // the buffer is not 4 bytes long, let the higher level logic figure out
1194 // what to do with a size of zero and MCDisassembler::Fail.
1195 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
1196 if (Result == MCDisassembler::Fail)
1197 return MCDisassembler::Fail;
1199 // The only instruction size for standard encoded MIPS.
1203 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1205 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
1206 if (Result != MCDisassembler::Fail)
1210 if (hasMips32r6() && isGP64()) {
1211 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1212 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
1213 Address, this, STI);
1214 if (Result != MCDisassembler::Fail)
1218 if (hasMips32r6() && isPTR64()) {
1219 DEBUG(dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1220 Result = decodeInstruction(DecoderTableMips32r6_64r6_PTR6432, Instr, Insn,
1221 Address, this, STI);
1222 if (Result != MCDisassembler::Fail)
1226 if (hasMips32r6()) {
1227 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1228 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
1229 Address, this, STI);
1230 if (Result != MCDisassembler::Fail)
1234 if (hasMips2() && isPTR64()) {
1235 DEBUG(dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1236 Result = decodeInstruction(DecoderTableMips32_64_PTR6432, Instr, Insn,
1237 Address, this, STI);
1238 if (Result != MCDisassembler::Fail)
1243 DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1244 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
1245 Address, this, STI);
1246 if (Result != MCDisassembler::Fail)
1251 DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1252 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
1253 Address, this, STI);
1254 if (Result != MCDisassembler::Fail)
1258 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1259 // Calling the auto-generated decoder function.
1261 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
1262 if (Result != MCDisassembler::Fail)
1265 return MCDisassembler::Fail;
1268 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
1271 const void *Decoder) {
1272 return MCDisassembler::Fail;
1275 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1278 const void *Decoder) {
1280 return MCDisassembler::Fail;
1282 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1283 Inst.addOperand(MCOperand::createReg(Reg));
1284 return MCDisassembler::Success;
1287 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1290 const void *Decoder) {
1292 return MCDisassembler::Fail;
1293 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1294 Inst.addOperand(MCOperand::createReg(Reg));
1295 return MCDisassembler::Success;
1298 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1301 const void *Decoder) {
1303 return MCDisassembler::Fail;
1304 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1305 Inst.addOperand(MCOperand::createReg(Reg));
1306 return MCDisassembler::Success;
1309 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1312 const void *Decoder) {
1314 return MCDisassembler::Fail;
1315 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1316 Inst.addOperand(MCOperand::createReg(Reg));
1317 return MCDisassembler::Success;
1320 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1323 const void *Decoder) {
1325 return MCDisassembler::Fail;
1326 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1327 Inst.addOperand(MCOperand::createReg(Reg));
1328 return MCDisassembler::Success;
1331 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1334 const void *Decoder) {
1335 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1336 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1338 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1341 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1344 const void *Decoder) {
1345 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1348 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1351 const void *Decoder) {
1353 return MCDisassembler::Fail;
1355 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1356 Inst.addOperand(MCOperand::createReg(Reg));
1357 return MCDisassembler::Success;
1360 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1363 const void *Decoder) {
1365 return MCDisassembler::Fail;
1367 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1368 Inst.addOperand(MCOperand::createReg(Reg));
1369 return MCDisassembler::Success;
1372 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1375 const void *Decoder) {
1377 return MCDisassembler::Fail;
1378 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1379 Inst.addOperand(MCOperand::createReg(Reg));
1380 return MCDisassembler::Success;
1383 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1386 const void *Decoder) {
1388 return MCDisassembler::Fail;
1389 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1390 Inst.addOperand(MCOperand::createReg(Reg));
1391 return MCDisassembler::Success;
1394 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1396 const void *Decoder) {
1398 return MCDisassembler::Fail;
1400 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1401 Inst.addOperand(MCOperand::createReg(Reg));
1402 return MCDisassembler::Success;
1405 static DecodeStatus DecodeMem(MCInst &Inst,
1408 const void *Decoder) {
1409 int Offset = SignExtend32<16>(Insn & 0xffff);
1410 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1411 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1413 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1414 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1416 if (Inst.getOpcode() == Mips::SC ||
1417 Inst.getOpcode() == Mips::SCD)
1418 Inst.addOperand(MCOperand::createReg(Reg));
1420 Inst.addOperand(MCOperand::createReg(Reg));
1421 Inst.addOperand(MCOperand::createReg(Base));
1422 Inst.addOperand(MCOperand::createImm(Offset));
1424 return MCDisassembler::Success;
1427 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1430 const void *Decoder) {
1431 int Offset = SignExtend32<9>(Insn >> 7);
1432 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1433 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1435 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1436 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1438 if (Inst.getOpcode() == Mips::SCE)
1439 Inst.addOperand(MCOperand::createReg(Reg));
1441 Inst.addOperand(MCOperand::createReg(Reg));
1442 Inst.addOperand(MCOperand::createReg(Base));
1443 Inst.addOperand(MCOperand::createImm(Offset));
1445 return MCDisassembler::Success;
1448 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
1451 const void *Decoder) {
1452 int Offset = SignExtend32<9>(Insn & 0x1ff);
1453 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1454 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1456 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1457 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1459 Inst.addOperand(MCOperand::createReg(Reg));
1460 Inst.addOperand(MCOperand::createReg(Base));
1461 Inst.addOperand(MCOperand::createImm(Offset));
1463 return MCDisassembler::Success;
1466 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1469 const void *Decoder) {
1470 int Offset = SignExtend32<16>(Insn & 0xffff);
1471 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1472 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1474 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1475 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1477 Inst.addOperand(MCOperand::createReg(Reg));
1478 Inst.addOperand(MCOperand::createReg(Base));
1479 Inst.addOperand(MCOperand::createImm(Offset));
1481 return MCDisassembler::Success;
1484 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1487 const void *Decoder) {
1488 int Offset = SignExtend32<16>(Insn & 0xffff);
1489 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1490 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1492 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1494 Inst.addOperand(MCOperand::createReg(Base));
1495 Inst.addOperand(MCOperand::createImm(Offset));
1496 Inst.addOperand(MCOperand::createImm(Hint));
1498 return MCDisassembler::Success;
1501 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1504 const void *Decoder) {
1505 int Offset = SignExtend32<12>(Insn & 0xfff);
1506 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1507 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1509 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1511 Inst.addOperand(MCOperand::createReg(Base));
1512 Inst.addOperand(MCOperand::createImm(Offset));
1513 Inst.addOperand(MCOperand::createImm(Hint));
1515 return MCDisassembler::Success;
1518 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1521 const void *Decoder) {
1522 int Offset = SignExtend32<9>(Insn & 0x1ff);
1523 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1524 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1526 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1528 Inst.addOperand(MCOperand::createReg(Base));
1529 Inst.addOperand(MCOperand::createImm(Offset));
1530 Inst.addOperand(MCOperand::createImm(Hint));
1532 return MCDisassembler::Success;
1535 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1538 const void *Decoder) {
1539 int Offset = SignExtend32<9>(Insn >> 7);
1540 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1541 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1543 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1545 Inst.addOperand(MCOperand::createReg(Base));
1546 Inst.addOperand(MCOperand::createImm(Offset));
1547 Inst.addOperand(MCOperand::createImm(Hint));
1549 return MCDisassembler::Success;
1552 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
1555 const void *Decoder) {
1556 int Offset = SignExtend32<9>(Insn & 0x1ff);
1557 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1558 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1560 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1561 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1563 Inst.addOperand(MCOperand::createReg(Reg));
1564 Inst.addOperand(MCOperand::createReg(Base));
1565 Inst.addOperand(MCOperand::createImm(Offset));
1567 return MCDisassembler::Success;
1570 static DecodeStatus DecodeSyncI(MCInst &Inst,
1573 const void *Decoder) {
1574 int Offset = SignExtend32<16>(Insn & 0xffff);
1575 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1577 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1579 Inst.addOperand(MCOperand::createReg(Base));
1580 Inst.addOperand(MCOperand::createImm(Offset));
1582 return MCDisassembler::Success;
1585 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1588 const void *Decoder) {
1589 int Immediate = SignExtend32<16>(Insn & 0xffff);
1590 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1592 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1594 Inst.addOperand(MCOperand::createReg(Base));
1595 Inst.addOperand(MCOperand::createImm(Immediate));
1597 return MCDisassembler::Success;
1600 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1601 uint64_t Address, const void *Decoder) {
1602 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1603 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1604 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1606 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1607 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1609 Inst.addOperand(MCOperand::createReg(Reg));
1610 Inst.addOperand(MCOperand::createReg(Base));
1612 // The immediate field of an LD/ST instruction is scaled which means it must
1613 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1619 switch(Inst.getOpcode())
1622 assert(false && "Unexpected instruction");
1623 return MCDisassembler::Fail;
1627 Inst.addOperand(MCOperand::createImm(Offset));
1631 Inst.addOperand(MCOperand::createImm(Offset * 2));
1635 Inst.addOperand(MCOperand::createImm(Offset * 4));
1639 Inst.addOperand(MCOperand::createImm(Offset * 8));
1643 return MCDisassembler::Success;
1646 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1649 const void *Decoder) {
1650 unsigned Offset = Insn & 0xf;
1651 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1652 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1654 switch (Inst.getOpcode()) {
1655 case Mips::LBU16_MM:
1656 case Mips::LHU16_MM:
1658 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1659 == MCDisassembler::Fail)
1660 return MCDisassembler::Fail;
1663 case Mips::SB16_MMR6:
1665 case Mips::SH16_MMR6:
1667 case Mips::SW16_MMR6:
1668 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1669 == MCDisassembler::Fail)
1670 return MCDisassembler::Fail;
1674 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1675 == MCDisassembler::Fail)
1676 return MCDisassembler::Fail;
1678 switch (Inst.getOpcode()) {
1679 case Mips::LBU16_MM:
1681 Inst.addOperand(MCOperand::createImm(-1));
1683 Inst.addOperand(MCOperand::createImm(Offset));
1686 case Mips::SB16_MMR6:
1687 Inst.addOperand(MCOperand::createImm(Offset));
1689 case Mips::LHU16_MM:
1691 case Mips::SH16_MMR6:
1692 Inst.addOperand(MCOperand::createImm(Offset << 1));
1696 case Mips::SW16_MMR6:
1697 Inst.addOperand(MCOperand::createImm(Offset << 2));
1701 return MCDisassembler::Success;
1704 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1707 const void *Decoder) {
1708 unsigned Offset = Insn & 0x1F;
1709 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1711 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1713 Inst.addOperand(MCOperand::createReg(Reg));
1714 Inst.addOperand(MCOperand::createReg(Mips::SP));
1715 Inst.addOperand(MCOperand::createImm(Offset << 2));
1717 return MCDisassembler::Success;
1720 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1723 const void *Decoder) {
1724 unsigned Offset = Insn & 0x7F;
1725 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1727 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1729 Inst.addOperand(MCOperand::createReg(Reg));
1730 Inst.addOperand(MCOperand::createReg(Mips::GP));
1731 Inst.addOperand(MCOperand::createImm(Offset << 2));
1733 return MCDisassembler::Success;
1736 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1739 const void *Decoder) {
1741 switch (Inst.getOpcode()) {
1742 case Mips::LWM16_MMR6:
1743 case Mips::SWM16_MMR6:
1744 Offset = fieldFromInstruction(Insn, 4, 4);
1747 Offset = SignExtend32<4>(Insn & 0xf);
1751 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1752 == MCDisassembler::Fail)
1753 return MCDisassembler::Fail;
1755 Inst.addOperand(MCOperand::createReg(Mips::SP));
1756 Inst.addOperand(MCOperand::createImm(Offset << 2));
1758 return MCDisassembler::Success;
1761 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1764 const void *Decoder) {
1765 int Offset = SignExtend32<9>(Insn & 0x1ff);
1766 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1767 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1769 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1770 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1772 if (Inst.getOpcode() == Mips::SCE_MM)
1773 Inst.addOperand(MCOperand::createReg(Reg));
1775 Inst.addOperand(MCOperand::createReg(Reg));
1776 Inst.addOperand(MCOperand::createReg(Base));
1777 Inst.addOperand(MCOperand::createImm(Offset));
1779 return MCDisassembler::Success;
1782 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1785 const void *Decoder) {
1786 int Offset = SignExtend32<12>(Insn & 0x0fff);
1787 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1788 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1790 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1791 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1793 switch (Inst.getOpcode()) {
1794 case Mips::SWM32_MM:
1795 case Mips::LWM32_MM:
1796 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1797 == MCDisassembler::Fail)
1798 return MCDisassembler::Fail;
1799 Inst.addOperand(MCOperand::createReg(Base));
1800 Inst.addOperand(MCOperand::createImm(Offset));
1803 Inst.addOperand(MCOperand::createReg(Reg));
1806 Inst.addOperand(MCOperand::createReg(Reg));
1807 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM ||
1808 Inst.getOpcode() == Mips::LWP_MMR6 || Inst.getOpcode() == Mips::SWP_MMR6)
1809 Inst.addOperand(MCOperand::createReg(Reg+1));
1811 Inst.addOperand(MCOperand::createReg(Base));
1812 Inst.addOperand(MCOperand::createImm(Offset));
1815 return MCDisassembler::Success;
1818 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1821 const void *Decoder) {
1822 int Offset = SignExtend32<16>(Insn & 0xffff);
1823 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1824 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1826 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1827 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1829 Inst.addOperand(MCOperand::createReg(Reg));
1830 Inst.addOperand(MCOperand::createReg(Base));
1831 Inst.addOperand(MCOperand::createImm(Offset));
1833 return MCDisassembler::Success;
1836 static DecodeStatus DecodeFMem(MCInst &Inst,
1839 const void *Decoder) {
1840 int Offset = SignExtend32<16>(Insn & 0xffff);
1841 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1842 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1844 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1845 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1847 Inst.addOperand(MCOperand::createReg(Reg));
1848 Inst.addOperand(MCOperand::createReg(Base));
1849 Inst.addOperand(MCOperand::createImm(Offset));
1851 return MCDisassembler::Success;
1854 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
1855 uint64_t Address, const void *Decoder) {
1856 // This function is the same as DecodeFMem but with the Reg and Base fields
1857 // swapped according to microMIPS spec.
1858 int Offset = SignExtend32<16>(Insn & 0xffff);
1859 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1860 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1862 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1863 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1865 Inst.addOperand(MCOperand::createReg(Reg));
1866 Inst.addOperand(MCOperand::createReg(Base));
1867 Inst.addOperand(MCOperand::createImm(Offset));
1869 return MCDisassembler::Success;
1872 static DecodeStatus DecodeFMem2(MCInst &Inst,
1875 const void *Decoder) {
1876 int Offset = SignExtend32<16>(Insn & 0xffff);
1877 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1878 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1880 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1881 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1883 Inst.addOperand(MCOperand::createReg(Reg));
1884 Inst.addOperand(MCOperand::createReg(Base));
1885 Inst.addOperand(MCOperand::createImm(Offset));
1887 return MCDisassembler::Success;
1890 static DecodeStatus DecodeFMem3(MCInst &Inst,
1893 const void *Decoder) {
1894 int Offset = SignExtend32<16>(Insn & 0xffff);
1895 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1896 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1898 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1899 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1901 Inst.addOperand(MCOperand::createReg(Reg));
1902 Inst.addOperand(MCOperand::createReg(Base));
1903 Inst.addOperand(MCOperand::createImm(Offset));
1905 return MCDisassembler::Success;
1908 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1911 const void *Decoder) {
1912 int Offset = SignExtend32<11>(Insn & 0x07ff);
1913 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1914 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1916 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1917 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1919 Inst.addOperand(MCOperand::createReg(Reg));
1920 Inst.addOperand(MCOperand::createReg(Base));
1921 Inst.addOperand(MCOperand::createImm(Offset));
1923 return MCDisassembler::Success;
1926 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
1927 uint64_t Address, const void *Decoder) {
1928 int Offset = SignExtend32<11>(Insn & 0x07ff);
1929 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1930 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1932 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1933 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1935 Inst.addOperand(MCOperand::createReg(Reg));
1936 Inst.addOperand(MCOperand::createReg(Base));
1937 Inst.addOperand(MCOperand::createImm(Offset));
1939 return MCDisassembler::Success;
1942 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1945 const void *Decoder) {
1946 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1947 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1948 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1950 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1951 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1953 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1954 Inst.addOperand(MCOperand::createReg(Rt));
1957 Inst.addOperand(MCOperand::createReg(Rt));
1958 Inst.addOperand(MCOperand::createReg(Base));
1959 Inst.addOperand(MCOperand::createImm(Offset));
1961 return MCDisassembler::Success;
1964 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1967 const void *Decoder) {
1968 // Currently only hardware register 29 is supported.
1970 return MCDisassembler::Fail;
1971 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
1972 return MCDisassembler::Success;
1975 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1978 const void *Decoder) {
1979 if (RegNo > 30 || RegNo %2)
1980 return MCDisassembler::Fail;
1982 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1983 Inst.addOperand(MCOperand::createReg(Reg));
1984 return MCDisassembler::Success;
1987 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1990 const void *Decoder) {
1992 return MCDisassembler::Fail;
1994 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1995 Inst.addOperand(MCOperand::createReg(Reg));
1996 return MCDisassembler::Success;
1999 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
2002 const void *Decoder) {
2004 return MCDisassembler::Fail;
2006 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
2007 Inst.addOperand(MCOperand::createReg(Reg));
2008 return MCDisassembler::Success;
2011 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
2014 const void *Decoder) {
2016 return MCDisassembler::Fail;
2018 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
2019 Inst.addOperand(MCOperand::createReg(Reg));
2020 return MCDisassembler::Success;
2023 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
2026 const void *Decoder) {
2028 return MCDisassembler::Fail;
2030 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
2031 Inst.addOperand(MCOperand::createReg(Reg));
2032 return MCDisassembler::Success;
2035 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
2038 const void *Decoder) {
2040 return MCDisassembler::Fail;
2042 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
2043 Inst.addOperand(MCOperand::createReg(Reg));
2044 return MCDisassembler::Success;
2047 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
2050 const void *Decoder) {
2052 return MCDisassembler::Fail;
2054 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
2055 Inst.addOperand(MCOperand::createReg(Reg));
2056 return MCDisassembler::Success;
2059 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
2062 const void *Decoder) {
2064 return MCDisassembler::Fail;
2066 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
2067 Inst.addOperand(MCOperand::createReg(Reg));
2068 return MCDisassembler::Success;
2071 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
2074 const void *Decoder) {
2076 return MCDisassembler::Fail;
2078 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
2079 Inst.addOperand(MCOperand::createReg(Reg));
2080 return MCDisassembler::Success;
2083 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
2086 const void *Decoder) {
2088 return MCDisassembler::Fail;
2090 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
2091 Inst.addOperand(MCOperand::createReg(Reg));
2092 return MCDisassembler::Success;
2095 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
2098 const void *Decoder) {
2100 return MCDisassembler::Fail;
2102 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
2103 Inst.addOperand(MCOperand::createReg(Reg));
2104 return MCDisassembler::Success;
2107 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
2110 const void *Decoder) {
2111 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
2112 Inst.addOperand(MCOperand::createImm(BranchOffset));
2113 return MCDisassembler::Success;
2116 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
2119 const void *Decoder) {
2120 int32_t BranchOffset = (SignExtend32<16>(Offset) * 2);
2121 Inst.addOperand(MCOperand::createImm(BranchOffset));
2122 return MCDisassembler::Success;
2125 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
2128 const void *Decoder) {
2129 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
2130 Inst.addOperand(MCOperand::createImm(JumpOffset));
2131 return MCDisassembler::Success;
2134 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
2137 const void *Decoder) {
2138 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2140 Inst.addOperand(MCOperand::createImm(BranchOffset));
2141 return MCDisassembler::Success;
2144 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
2147 const void *Decoder) {
2148 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2150 Inst.addOperand(MCOperand::createImm(BranchOffset));
2151 return MCDisassembler::Success;
2154 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
2157 const void *Decoder) {
2158 int32_t BranchOffset = SignExtend32<26>(Offset) * 4 + 4;
2160 Inst.addOperand(MCOperand::createImm(BranchOffset));
2161 return MCDisassembler::Success;
2164 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
2167 const void *Decoder) {
2168 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
2169 Inst.addOperand(MCOperand::createImm(BranchOffset));
2170 return MCDisassembler::Success;
2173 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
2176 const void *Decoder) {
2177 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
2178 Inst.addOperand(MCOperand::createImm(BranchOffset));
2179 return MCDisassembler::Success;
2182 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
2185 const void *Decoder) {
2186 int32_t BranchOffset = SignExtend32<16>(Offset) * 2 + 4;
2187 Inst.addOperand(MCOperand::createImm(BranchOffset));
2188 return MCDisassembler::Success;
2191 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
2194 const void *Decoder) {
2195 int32_t BranchOffset = SignExtend32<26>(Offset) << 1;
2197 Inst.addOperand(MCOperand::createImm(BranchOffset));
2198 return MCDisassembler::Success;
2201 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
2204 const void *Decoder) {
2205 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
2206 Inst.addOperand(MCOperand::createImm(JumpOffset));
2207 return MCDisassembler::Success;
2210 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
2213 const void *Decoder) {
2215 Inst.addOperand(MCOperand::createImm(1));
2216 else if (Value == 0x7)
2217 Inst.addOperand(MCOperand::createImm(-1));
2219 Inst.addOperand(MCOperand::createImm(Value << 2));
2220 return MCDisassembler::Success;
2223 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
2226 const void *Decoder) {
2228 Inst.addOperand(MCOperand::createImm(-1));
2230 Inst.addOperand(MCOperand::createImm(Value));
2231 return MCDisassembler::Success;
2234 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
2237 const void *Decoder) {
2238 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
2239 return MCDisassembler::Success;
2242 template <unsigned Bits, int Offset, int Scale>
2243 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2245 const void *Decoder) {
2246 Value &= ((1 << Bits) - 1);
2248 Inst.addOperand(MCOperand::createImm(Value + Offset));
2249 return MCDisassembler::Success;
2252 template <unsigned Bits, int Offset, int ScaleBy>
2253 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2255 const void *Decoder) {
2256 int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
2257 Inst.addOperand(MCOperand::createImm(Imm + Offset));
2258 return MCDisassembler::Success;
2261 static DecodeStatus DecodeInsSize(MCInst &Inst,
2264 const void *Decoder) {
2265 // First we need to grab the pos(lsb) from MCInst.
2266 int Pos = Inst.getOperand(2).getImm();
2267 if (Inst.getOpcode() == Mips::DINSU)
2270 if (Inst.getOpcode() == Mips::DINSM ||
2271 Inst.getOpcode() == Mips::DINSU)
2272 Size = (int) Insn - Pos + 33;
2274 Size = (int) Insn - Pos + 1;
2275 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
2276 return MCDisassembler::Success;
2279 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
2280 uint64_t Address, const void *Decoder) {
2281 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
2282 return MCDisassembler::Success;
2285 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
2286 uint64_t Address, const void *Decoder) {
2287 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
2288 return MCDisassembler::Success;
2291 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
2292 uint64_t Address, const void *Decoder) {
2293 int32_t DecodedValue;
2295 case 0: DecodedValue = 256; break;
2296 case 1: DecodedValue = 257; break;
2297 case 510: DecodedValue = -258; break;
2298 case 511: DecodedValue = -257; break;
2299 default: DecodedValue = SignExtend32<9>(Insn); break;
2301 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
2302 return MCDisassembler::Success;
2305 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
2306 uint64_t Address, const void *Decoder) {
2307 // Insn must be >= 0, since it is unsigned that condition is always true.
2309 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2311 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
2312 return MCDisassembler::Success;
2315 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2318 const void *Decoder) {
2319 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2320 Mips::S6, Mips::S7, Mips::FP};
2323 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2325 // Empty register lists are not allowed.
2327 return MCDisassembler::Fail;
2329 RegNum = RegLst & 0xf;
2331 // RegLst values 10-15, and 26-31 are reserved.
2333 return MCDisassembler::Fail;
2335 for (unsigned i = 0; i < RegNum; i++)
2336 Inst.addOperand(MCOperand::createReg(Regs[i]));
2339 Inst.addOperand(MCOperand::createReg(Mips::RA));
2341 return MCDisassembler::Success;
2344 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2346 const void *Decoder) {
2347 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2349 switch(Inst.getOpcode()) {
2351 RegLst = fieldFromInstruction(Insn, 4, 2);
2353 case Mips::LWM16_MMR6:
2354 case Mips::SWM16_MMR6:
2355 RegLst = fieldFromInstruction(Insn, 8, 2);
2358 unsigned RegNum = RegLst & 0x3;
2360 for (unsigned i = 0; i <= RegNum; i++)
2361 Inst.addOperand(MCOperand::createReg(Regs[i]));
2363 Inst.addOperand(MCOperand::createReg(Mips::RA));
2365 return MCDisassembler::Success;
2368 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
2369 uint64_t Address, const void *Decoder) {
2370 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2374 return MCDisassembler::Fail;
2376 Inst.addOperand(MCOperand::createReg(Mips::A1));
2377 Inst.addOperand(MCOperand::createReg(Mips::A2));
2380 Inst.addOperand(MCOperand::createReg(Mips::A1));
2381 Inst.addOperand(MCOperand::createReg(Mips::A3));
2384 Inst.addOperand(MCOperand::createReg(Mips::A2));
2385 Inst.addOperand(MCOperand::createReg(Mips::A3));
2388 Inst.addOperand(MCOperand::createReg(Mips::A0));
2389 Inst.addOperand(MCOperand::createReg(Mips::S5));
2392 Inst.addOperand(MCOperand::createReg(Mips::A0));
2393 Inst.addOperand(MCOperand::createReg(Mips::S6));
2396 Inst.addOperand(MCOperand::createReg(Mips::A0));
2397 Inst.addOperand(MCOperand::createReg(Mips::A1));
2400 Inst.addOperand(MCOperand::createReg(Mips::A0));
2401 Inst.addOperand(MCOperand::createReg(Mips::A2));
2404 Inst.addOperand(MCOperand::createReg(Mips::A0));
2405 Inst.addOperand(MCOperand::createReg(Mips::A3));
2409 return MCDisassembler::Success;
2412 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2413 uint64_t Address, const void *Decoder) {
2414 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2415 return MCDisassembler::Success;
2418 template <typename InsnType>
2419 static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
2421 const void *Decoder) {
2423 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2424 // Invalid if rt == 0
2425 // BGTZALC_MMR6 if rs == 0 && rt != 0
2426 // BLTZALC_MMR6 if rs != 0 && rs == rt
2427 // BLTUC_MMR6 if rs != 0 && rs != rt
2429 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2430 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2436 return MCDisassembler::Fail;
2438 MI.setOpcode(Mips::BGTZALC_MMR6);
2440 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2442 else if (Rs == Rt) {
2443 MI.setOpcode(Mips::BLTZALC_MMR6);
2445 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2448 MI.setOpcode(Mips::BLTUC_MMR6);
2451 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2456 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2460 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2462 MI.addOperand(MCOperand::createImm(Imm));
2464 return MCDisassembler::Success;
2467 template <typename InsnType>
2468 static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
2470 const void *Decoder) {
2472 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2473 // Invalid if rt == 0
2474 // BLEZALC_MMR6 if rs == 0 && rt != 0
2475 // BGEZALC_MMR6 if rs == rt && rt != 0
2476 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2478 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2479 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2484 return MCDisassembler::Fail;
2486 MI.setOpcode(Mips::BLEZALC_MMR6);
2487 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2489 else if (Rs == Rt) {
2490 MI.setOpcode(Mips::BGEZALC_MMR6);
2491 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2495 MI.setOpcode(Mips::BGEUC_MMR6);
2496 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2501 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2503 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2505 MI.addOperand(MCOperand::createImm(Imm));
2507 return MCDisassembler::Success;