1 //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the Mips Disassembler.
12 //===----------------------------------------------------------------------===//
15 #include "MipsRegisterInfo.h"
16 #include "MipsSubtarget.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler.h"
19 #include "llvm/MC/MCFixedLenDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/TargetRegistry.h"
27 #define DEBUG_TYPE "mips-disassembler"
29 typedef MCDisassembler::DecodeStatus DecodeStatus;
33 class MipsDisassembler : public MCDisassembler {
37 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
38 : MCDisassembler(STI, Ctx),
39 IsMicroMips(STI.getFeatureBits()[Mips::FeatureMicroMips]),
40 IsBigEndian(IsBigEndian) {}
42 bool hasMips3() const { return STI.getFeatureBits()[Mips::FeatureMips3]; }
43 bool hasMips32() const { return STI.getFeatureBits()[Mips::FeatureMips32]; }
44 bool hasMips32r6() const {
45 return STI.getFeatureBits()[Mips::FeatureMips32r6];
48 bool isGP64() const { return STI.getFeatureBits()[Mips::FeatureGP64Bit]; }
50 bool hasCnMips() const { return STI.getFeatureBits()[Mips::FeatureCnMips]; }
52 bool hasCOP3() const {
53 // Only present in MIPS-I and MIPS-II
54 return !hasMips32() && !hasMips3();
57 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
58 ArrayRef<uint8_t> Bytes, uint64_t Address,
60 raw_ostream &CStream) const override;
63 } // end anonymous namespace
65 // Forward declare these because the autogenerated code will reference them.
66 // Definitions are further down.
67 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
72 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
77 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
82 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
87 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
92 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
97 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
100 const void *Decoder);
102 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
105 const void *Decoder);
107 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
110 const void *Decoder);
112 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
115 const void *Decoder);
117 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
120 const void *Decoder);
122 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
125 const void *Decoder);
127 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
129 const void *Decoder);
131 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
134 const void *Decoder);
136 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
139 const void *Decoder);
141 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
144 const void *Decoder);
146 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
149 const void *Decoder);
151 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
154 const void *Decoder);
156 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
159 const void *Decoder);
161 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
164 const void *Decoder);
166 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
169 const void *Decoder);
171 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
174 const void *Decoder);
176 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
184 const void *Decoder);
186 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
189 const void *Decoder);
191 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
194 const void *Decoder);
196 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
199 const void *Decoder);
201 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
204 const void *Decoder);
206 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
209 const void *Decoder);
211 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
212 // shifted left by 1 bit.
213 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
216 const void *Decoder);
218 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
219 // shifted left by 1 bit.
220 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
223 const void *Decoder);
225 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
226 // shifted left by 1 bit.
227 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
230 const void *Decoder);
232 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
233 // shifted left by 1 bit.
234 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
237 const void *Decoder);
239 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
240 // shifted left by 1 bit.
241 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
244 const void *Decoder);
246 static DecodeStatus DecodeMem(MCInst &Inst,
249 const void *Decoder);
251 static DecodeStatus DecodeMemEVA(MCInst &Inst,
254 const void *Decoder);
256 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
259 const void *Decoder);
261 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
264 const void *Decoder);
266 static DecodeStatus DecodeCacheOp(MCInst &Inst,
269 const void *Decoder);
271 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
274 const void *Decoder);
276 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
279 const void *Decoder);
281 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
284 const void *Decoder);
286 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
289 const void *Decoder);
291 static DecodeStatus DecodeSyncI(MCInst &Inst,
294 const void *Decoder);
296 static DecodeStatus DecodeSynciR6(MCInst &Inst,
299 const void *Decoder);
301 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
302 uint64_t Address, const void *Decoder);
304 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
307 const void *Decoder);
309 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
312 const void *Decoder);
314 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
317 const void *Decoder);
319 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
322 const void *Decoder);
324 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
327 const void *Decoder);
329 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
332 const void *Decoder);
334 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
337 const void *Decoder);
339 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
341 const void *Decoder);
343 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
345 const void *Decoder);
347 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
349 const void *Decoder);
351 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
353 const void *Decoder);
355 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
358 const void *Decoder);
360 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
363 const void *Decoder);
365 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
368 const void *Decoder);
370 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
373 const void *Decoder);
375 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
378 const void *Decoder);
380 static DecodeStatus DecodeSimm4(MCInst &Inst,
383 const void *Decoder);
385 static DecodeStatus DecodeSimm16(MCInst &Inst,
388 const void *Decoder);
390 template <unsigned Bits, int Offset>
391 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
392 uint64_t Address, const void *Decoder);
394 static DecodeStatus DecodeInsSize(MCInst &Inst,
397 const void *Decoder);
399 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
400 uint64_t Address, const void *Decoder);
402 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
403 uint64_t Address, const void *Decoder);
405 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
406 uint64_t Address, const void *Decoder);
408 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
409 uint64_t Address, const void *Decoder);
411 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
412 uint64_t Address, const void *Decoder);
414 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
415 uint64_t Address, const void *Decoder);
417 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
419 template <typename InsnType>
420 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
421 const void *Decoder);
423 template <typename InsnType>
425 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
426 const void *Decoder);
428 template <typename InsnType>
430 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
431 const void *Decoder);
433 template <typename InsnType>
435 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
436 const void *Decoder);
438 template <typename InsnType>
440 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
441 const void *Decoder);
443 template <typename InsnType>
445 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
446 const void *Decoder);
448 template <typename InsnType>
450 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
451 const void *Decoder);
453 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
455 const void *Decoder);
457 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
459 const void *Decoder);
461 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
463 const void *Decoder);
466 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
470 static MCDisassembler *createMipsDisassembler(
472 const MCSubtargetInfo &STI,
474 return new MipsDisassembler(STI, Ctx, true);
477 static MCDisassembler *createMipselDisassembler(
479 const MCSubtargetInfo &STI,
481 return new MipsDisassembler(STI, Ctx, false);
484 extern "C" void LLVMInitializeMipsDisassembler() {
485 // Register the disassembler.
486 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
487 createMipsDisassembler);
488 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
489 createMipselDisassembler);
490 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
491 createMipsDisassembler);
492 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
493 createMipselDisassembler);
496 #include "MipsGenDisassemblerTables.inc"
498 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
499 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
500 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
501 return *(RegInfo->getRegClass(RC).begin() + RegNo);
504 template <typename InsnType>
505 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
506 const void *Decoder) {
507 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
508 // The size of the n field depends on the element size
509 // The register class also depends on this.
510 InsnType tmp = fieldFromInstruction(insn, 17, 5);
512 DecodeFN RegDecoder = nullptr;
513 if ((tmp & 0x18) == 0x00) { // INSVE_B
515 RegDecoder = DecodeMSA128BRegisterClass;
516 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
518 RegDecoder = DecodeMSA128HRegisterClass;
519 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
521 RegDecoder = DecodeMSA128WRegisterClass;
522 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
524 RegDecoder = DecodeMSA128DRegisterClass;
526 llvm_unreachable("Invalid encoding");
528 assert(NSize != 0 && RegDecoder != nullptr);
531 tmp = fieldFromInstruction(insn, 6, 5);
532 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
533 return MCDisassembler::Fail;
535 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
536 return MCDisassembler::Fail;
538 tmp = fieldFromInstruction(insn, 16, NSize);
539 MI.addOperand(MCOperand::createImm(tmp));
541 tmp = fieldFromInstruction(insn, 11, 5);
542 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
543 return MCDisassembler::Fail;
545 MI.addOperand(MCOperand::createImm(0));
547 return MCDisassembler::Success;
550 template <typename InsnType>
551 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
553 const void *Decoder) {
554 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
555 // (otherwise we would have matched the ADDI instruction from the earlier
559 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
561 // BEQZALC if rs == 0 && rt != 0
562 // BEQC if rs < rt && rs != 0
564 InsnType Rs = fieldFromInstruction(insn, 21, 5);
565 InsnType Rt = fieldFromInstruction(insn, 16, 5);
566 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
570 MI.setOpcode(Mips::BOVC);
572 } else if (Rs != 0 && Rs < Rt) {
573 MI.setOpcode(Mips::BEQC);
576 MI.setOpcode(Mips::BEQZALC);
579 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
582 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
584 MI.addOperand(MCOperand::createImm(Imm));
586 return MCDisassembler::Success;
589 template <typename InsnType>
590 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
592 const void *Decoder) {
593 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
594 // (otherwise we would have matched the ADDI instruction from the earlier
598 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
600 // BNEZALC if rs == 0 && rt != 0
601 // BNEC if rs < rt && rs != 0
603 InsnType Rs = fieldFromInstruction(insn, 21, 5);
604 InsnType Rt = fieldFromInstruction(insn, 16, 5);
605 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
609 MI.setOpcode(Mips::BNVC);
611 } else if (Rs != 0 && Rs < Rt) {
612 MI.setOpcode(Mips::BNEC);
615 MI.setOpcode(Mips::BNEZALC);
618 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
621 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
623 MI.addOperand(MCOperand::createImm(Imm));
625 return MCDisassembler::Success;
628 template <typename InsnType>
629 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
631 const void *Decoder) {
632 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
633 // (otherwise we would have matched the BLEZL instruction from the earlier
637 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
638 // Invalid if rs == 0
639 // BLEZC if rs == 0 && rt != 0
640 // BGEZC if rs == rt && rt != 0
641 // BGEC if rs != rt && rs != 0 && rt != 0
643 InsnType Rs = fieldFromInstruction(insn, 21, 5);
644 InsnType Rt = fieldFromInstruction(insn, 16, 5);
645 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
649 return MCDisassembler::Fail;
651 MI.setOpcode(Mips::BLEZC);
653 MI.setOpcode(Mips::BGEZC);
656 MI.setOpcode(Mips::BGEC);
660 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
663 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
666 MI.addOperand(MCOperand::createImm(Imm));
668 return MCDisassembler::Success;
671 template <typename InsnType>
672 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
674 const void *Decoder) {
675 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
676 // (otherwise we would have matched the BGTZL instruction from the earlier
680 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
681 // Invalid if rs == 0
682 // BGTZC if rs == 0 && rt != 0
683 // BLTZC if rs == rt && rt != 0
684 // BLTC if rs != rt && rs != 0 && rt != 0
688 InsnType Rs = fieldFromInstruction(insn, 21, 5);
689 InsnType Rt = fieldFromInstruction(insn, 16, 5);
690 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
693 return MCDisassembler::Fail;
695 MI.setOpcode(Mips::BGTZC);
697 MI.setOpcode(Mips::BLTZC);
699 MI.setOpcode(Mips::BLTC);
704 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
707 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
710 MI.addOperand(MCOperand::createImm(Imm));
712 return MCDisassembler::Success;
715 template <typename InsnType>
716 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
718 const void *Decoder) {
719 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
720 // (otherwise we would have matched the BGTZ instruction from the earlier
724 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
726 // BGTZALC if rs == 0 && rt != 0
727 // BLTZALC if rs != 0 && rs == rt
728 // BLTUC if rs != 0 && rs != rt
730 InsnType Rs = fieldFromInstruction(insn, 21, 5);
731 InsnType Rt = fieldFromInstruction(insn, 16, 5);
732 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
737 MI.setOpcode(Mips::BGTZ);
739 } else if (Rs == 0) {
740 MI.setOpcode(Mips::BGTZALC);
742 } else if (Rs == Rt) {
743 MI.setOpcode(Mips::BLTZALC);
746 MI.setOpcode(Mips::BLTUC);
752 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
756 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
759 MI.addOperand(MCOperand::createImm(Imm));
761 return MCDisassembler::Success;
764 template <typename InsnType>
765 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
767 const void *Decoder) {
768 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
769 // (otherwise we would have matched the BLEZL instruction from the earlier
773 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
774 // Invalid if rs == 0
775 // BLEZALC if rs == 0 && rt != 0
776 // BGEZALC if rs == rt && rt != 0
777 // BGEUC if rs != rt && rs != 0 && rt != 0
779 InsnType Rs = fieldFromInstruction(insn, 21, 5);
780 InsnType Rt = fieldFromInstruction(insn, 16, 5);
781 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
785 return MCDisassembler::Fail;
787 MI.setOpcode(Mips::BLEZALC);
789 MI.setOpcode(Mips::BGEZALC);
792 MI.setOpcode(Mips::BGEUC);
796 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
798 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
801 MI.addOperand(MCOperand::createImm(Imm));
803 return MCDisassembler::Success;
806 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
807 /// according to the given endianess.
808 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
809 uint64_t &Size, uint32_t &Insn,
811 // We want to read exactly 2 Bytes of data.
812 if (Bytes.size() < 2) {
814 return MCDisassembler::Fail;
818 Insn = (Bytes[0] << 8) | Bytes[1];
820 Insn = (Bytes[1] << 8) | Bytes[0];
823 return MCDisassembler::Success;
826 /// Read four bytes from the ArrayRef and return 32 bit word sorted
827 /// according to the given endianess
828 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
829 uint64_t &Size, uint32_t &Insn,
830 bool IsBigEndian, bool IsMicroMips) {
831 // We want to read exactly 4 Bytes of data.
832 if (Bytes.size() < 4) {
834 return MCDisassembler::Fail;
837 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
838 // always precede the low 16 bits in the instruction stream (that is, they
839 // are placed at lower addresses in the instruction stream).
841 // microMIPS byte ordering:
842 // Big-endian: 0 | 1 | 2 | 3
843 // Little-endian: 1 | 0 | 3 | 2
846 // Encoded as a big-endian 32-bit word in the stream.
848 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
851 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
854 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
859 return MCDisassembler::Success;
862 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
863 ArrayRef<uint8_t> Bytes,
865 raw_ostream &VStream,
866 raw_ostream &CStream) const {
871 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
872 if (Result == MCDisassembler::Fail)
873 return MCDisassembler::Fail;
876 DEBUG(dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
877 // Calling the auto-generated decoder function for microMIPS32R6
878 // (and microMIPS64R6) 16-bit instructions.
879 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
881 if (Result != MCDisassembler::Fail) {
887 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
888 // Calling the auto-generated decoder function for microMIPS 16-bit
890 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
892 if (Result != MCDisassembler::Fail) {
897 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
898 if (Result == MCDisassembler::Fail)
899 return MCDisassembler::Fail;
902 DEBUG(dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
903 // Calling the auto-generated decoder function.
904 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
906 if (Result != MCDisassembler::Fail) {
912 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
913 // Calling the auto-generated decoder function.
914 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
916 if (Result != MCDisassembler::Fail) {
920 // This is an invalid instruction. Let the disassembler move forward by the
921 // minimum instruction size.
923 return MCDisassembler::Fail;
926 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
927 if (Result == MCDisassembler::Fail) {
929 return MCDisassembler::Fail;
933 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
935 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
936 if (Result != MCDisassembler::Fail) {
942 if (hasMips32r6() && isGP64()) {
943 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
944 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
946 if (Result != MCDisassembler::Fail) {
953 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
954 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
956 if (Result != MCDisassembler::Fail) {
963 DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
964 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
966 if (Result != MCDisassembler::Fail) {
973 DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
974 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
976 if (Result != MCDisassembler::Fail) {
982 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
983 // Calling the auto-generated decoder function.
985 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
986 if (Result != MCDisassembler::Fail) {
992 return MCDisassembler::Fail;
995 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
998 const void *Decoder) {
1000 return MCDisassembler::Fail;
1004 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1007 const void *Decoder) {
1010 return MCDisassembler::Fail;
1012 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1013 Inst.addOperand(MCOperand::createReg(Reg));
1014 return MCDisassembler::Success;
1017 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1020 const void *Decoder) {
1022 return MCDisassembler::Fail;
1023 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1024 Inst.addOperand(MCOperand::createReg(Reg));
1025 return MCDisassembler::Success;
1028 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1031 const void *Decoder) {
1033 return MCDisassembler::Fail;
1034 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1035 Inst.addOperand(MCOperand::createReg(Reg));
1036 return MCDisassembler::Success;
1039 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1042 const void *Decoder) {
1044 return MCDisassembler::Fail;
1045 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1046 Inst.addOperand(MCOperand::createReg(Reg));
1047 return MCDisassembler::Success;
1050 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1053 const void *Decoder) {
1055 return MCDisassembler::Fail;
1056 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1057 Inst.addOperand(MCOperand::createReg(Reg));
1058 return MCDisassembler::Success;
1061 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1064 const void *Decoder) {
1065 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1066 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1068 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1071 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1074 const void *Decoder) {
1075 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1078 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1081 const void *Decoder) {
1083 return MCDisassembler::Fail;
1085 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1086 Inst.addOperand(MCOperand::createReg(Reg));
1087 return MCDisassembler::Success;
1090 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1093 const void *Decoder) {
1095 return MCDisassembler::Fail;
1097 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1098 Inst.addOperand(MCOperand::createReg(Reg));
1099 return MCDisassembler::Success;
1102 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1105 const void *Decoder) {
1107 return MCDisassembler::Fail;
1108 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1109 Inst.addOperand(MCOperand::createReg(Reg));
1110 return MCDisassembler::Success;
1113 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1116 const void *Decoder) {
1118 return MCDisassembler::Fail;
1119 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1120 Inst.addOperand(MCOperand::createReg(Reg));
1121 return MCDisassembler::Success;
1124 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1126 const void *Decoder) {
1128 return MCDisassembler::Fail;
1130 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1131 Inst.addOperand(MCOperand::createReg(Reg));
1132 return MCDisassembler::Success;
1135 static DecodeStatus DecodeMem(MCInst &Inst,
1138 const void *Decoder) {
1139 int Offset = SignExtend32<16>(Insn & 0xffff);
1140 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1141 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1143 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1144 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1146 if (Inst.getOpcode() == Mips::SC ||
1147 Inst.getOpcode() == Mips::SCD)
1148 Inst.addOperand(MCOperand::createReg(Reg));
1150 Inst.addOperand(MCOperand::createReg(Reg));
1151 Inst.addOperand(MCOperand::createReg(Base));
1152 Inst.addOperand(MCOperand::createImm(Offset));
1154 return MCDisassembler::Success;
1157 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1160 const void *Decoder) {
1161 int Offset = SignExtend32<9>(Insn >> 7);
1162 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1163 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1165 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1166 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1168 if (Inst.getOpcode() == Mips::SCE)
1169 Inst.addOperand(MCOperand::createReg(Reg));
1171 Inst.addOperand(MCOperand::createReg(Reg));
1172 Inst.addOperand(MCOperand::createReg(Base));
1173 Inst.addOperand(MCOperand::createImm(Offset));
1175 return MCDisassembler::Success;
1178 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
1181 const void *Decoder) {
1182 int Offset = SignExtend32<9>(Insn & 0x1ff);
1183 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1184 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1186 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1187 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1189 Inst.addOperand(MCOperand::createReg(Reg));
1190 Inst.addOperand(MCOperand::createReg(Base));
1191 Inst.addOperand(MCOperand::createImm(Offset));
1193 return MCDisassembler::Success;
1196 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1199 const void *Decoder) {
1200 int Offset = SignExtend32<16>(Insn & 0xffff);
1201 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1202 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1204 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1205 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1207 Inst.addOperand(MCOperand::createReg(Reg));
1208 Inst.addOperand(MCOperand::createReg(Base));
1209 Inst.addOperand(MCOperand::createImm(Offset));
1211 return MCDisassembler::Success;
1214 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1217 const void *Decoder) {
1218 int Offset = SignExtend32<16>(Insn & 0xffff);
1219 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1220 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1222 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1224 Inst.addOperand(MCOperand::createReg(Base));
1225 Inst.addOperand(MCOperand::createImm(Offset));
1226 Inst.addOperand(MCOperand::createImm(Hint));
1228 return MCDisassembler::Success;
1231 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1234 const void *Decoder) {
1235 int Offset = SignExtend32<12>(Insn & 0xfff);
1236 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1237 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1239 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1241 Inst.addOperand(MCOperand::createReg(Base));
1242 Inst.addOperand(MCOperand::createImm(Offset));
1243 Inst.addOperand(MCOperand::createImm(Hint));
1245 return MCDisassembler::Success;
1248 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1251 const void *Decoder) {
1252 int Offset = SignExtend32<9>(Insn & 0x1ff);
1253 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1254 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1256 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1258 Inst.addOperand(MCOperand::createReg(Base));
1259 Inst.addOperand(MCOperand::createImm(Offset));
1260 Inst.addOperand(MCOperand::createImm(Hint));
1262 return MCDisassembler::Success;
1265 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1268 const void *Decoder) {
1269 int Offset = SignExtend32<9>(Insn >> 7);
1270 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1271 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1273 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1275 Inst.addOperand(MCOperand::createReg(Base));
1276 Inst.addOperand(MCOperand::createImm(Offset));
1277 Inst.addOperand(MCOperand::createImm(Hint));
1279 return MCDisassembler::Success;
1282 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
1285 const void *Decoder) {
1286 int Offset = SignExtend32<9>(Insn & 0x1ff);
1287 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1288 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1290 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1291 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1293 Inst.addOperand(MCOperand::createReg(Reg));
1294 Inst.addOperand(MCOperand::createReg(Base));
1295 Inst.addOperand(MCOperand::createImm(Offset));
1297 return MCDisassembler::Success;
1300 static DecodeStatus DecodeSyncI(MCInst &Inst,
1303 const void *Decoder) {
1304 int Offset = SignExtend32<16>(Insn & 0xffff);
1305 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1307 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1309 Inst.addOperand(MCOperand::createReg(Base));
1310 Inst.addOperand(MCOperand::createImm(Offset));
1312 return MCDisassembler::Success;
1315 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1318 const void *Decoder) {
1319 int Immediate = SignExtend32<16>(Insn & 0xffff);
1320 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1322 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1324 Inst.addOperand(MCOperand::createReg(Base));
1325 Inst.addOperand(MCOperand::createImm(Immediate));
1327 return MCDisassembler::Success;
1330 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1331 uint64_t Address, const void *Decoder) {
1332 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1333 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1334 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1336 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1337 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1339 Inst.addOperand(MCOperand::createReg(Reg));
1340 Inst.addOperand(MCOperand::createReg(Base));
1342 // The immediate field of an LD/ST instruction is scaled which means it must
1343 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1349 switch(Inst.getOpcode())
1352 assert (0 && "Unexpected instruction");
1353 return MCDisassembler::Fail;
1357 Inst.addOperand(MCOperand::createImm(Offset));
1361 Inst.addOperand(MCOperand::createImm(Offset * 2));
1365 Inst.addOperand(MCOperand::createImm(Offset * 4));
1369 Inst.addOperand(MCOperand::createImm(Offset * 8));
1373 return MCDisassembler::Success;
1376 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1379 const void *Decoder) {
1380 unsigned Offset = Insn & 0xf;
1381 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1382 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1384 switch (Inst.getOpcode()) {
1385 case Mips::LBU16_MM:
1386 case Mips::LHU16_MM:
1388 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1389 == MCDisassembler::Fail)
1390 return MCDisassembler::Fail;
1393 case Mips::SB16_MMR6:
1395 case Mips::SH16_MMR6:
1397 case Mips::SW16_MMR6:
1398 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1399 == MCDisassembler::Fail)
1400 return MCDisassembler::Fail;
1404 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1405 == MCDisassembler::Fail)
1406 return MCDisassembler::Fail;
1408 switch (Inst.getOpcode()) {
1409 case Mips::LBU16_MM:
1411 Inst.addOperand(MCOperand::createImm(-1));
1413 Inst.addOperand(MCOperand::createImm(Offset));
1416 case Mips::SB16_MMR6:
1417 Inst.addOperand(MCOperand::createImm(Offset));
1419 case Mips::LHU16_MM:
1421 case Mips::SH16_MMR6:
1422 Inst.addOperand(MCOperand::createImm(Offset << 1));
1426 case Mips::SW16_MMR6:
1427 Inst.addOperand(MCOperand::createImm(Offset << 2));
1431 return MCDisassembler::Success;
1434 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1437 const void *Decoder) {
1438 unsigned Offset = Insn & 0x1F;
1439 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1441 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1443 Inst.addOperand(MCOperand::createReg(Reg));
1444 Inst.addOperand(MCOperand::createReg(Mips::SP));
1445 Inst.addOperand(MCOperand::createImm(Offset << 2));
1447 return MCDisassembler::Success;
1450 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1453 const void *Decoder) {
1454 unsigned Offset = Insn & 0x7F;
1455 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1457 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1459 Inst.addOperand(MCOperand::createReg(Reg));
1460 Inst.addOperand(MCOperand::createReg(Mips::GP));
1461 Inst.addOperand(MCOperand::createImm(Offset << 2));
1463 return MCDisassembler::Success;
1466 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1469 const void *Decoder) {
1471 switch (Inst.getOpcode()) {
1472 case Mips::LWM16_MMR6:
1473 case Mips::SWM16_MMR6:
1474 Offset = fieldFromInstruction(Insn, 4, 4);
1477 Offset = SignExtend32<4>(Insn & 0xf);
1481 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1482 == MCDisassembler::Fail)
1483 return MCDisassembler::Fail;
1485 Inst.addOperand(MCOperand::createReg(Mips::SP));
1486 Inst.addOperand(MCOperand::createImm(Offset << 2));
1488 return MCDisassembler::Success;
1491 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1494 const void *Decoder) {
1495 int Offset = SignExtend32<9>(Insn & 0x1ff);
1496 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1497 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1499 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1500 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1502 if (Inst.getOpcode() == Mips::SCE_MM)
1503 Inst.addOperand(MCOperand::createReg(Reg));
1505 Inst.addOperand(MCOperand::createReg(Reg));
1506 Inst.addOperand(MCOperand::createReg(Base));
1507 Inst.addOperand(MCOperand::createImm(Offset));
1509 return MCDisassembler::Success;
1512 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1515 const void *Decoder) {
1516 int Offset = SignExtend32<12>(Insn & 0x0fff);
1517 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1518 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1520 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1521 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1523 switch (Inst.getOpcode()) {
1524 case Mips::SWM32_MM:
1525 case Mips::LWM32_MM:
1526 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1527 == MCDisassembler::Fail)
1528 return MCDisassembler::Fail;
1529 Inst.addOperand(MCOperand::createReg(Base));
1530 Inst.addOperand(MCOperand::createImm(Offset));
1533 Inst.addOperand(MCOperand::createReg(Reg));
1536 Inst.addOperand(MCOperand::createReg(Reg));
1537 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1538 Inst.addOperand(MCOperand::createReg(Reg+1));
1540 Inst.addOperand(MCOperand::createReg(Base));
1541 Inst.addOperand(MCOperand::createImm(Offset));
1544 return MCDisassembler::Success;
1547 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1550 const void *Decoder) {
1551 int Offset = SignExtend32<16>(Insn & 0xffff);
1552 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1553 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1555 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1556 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1558 Inst.addOperand(MCOperand::createReg(Reg));
1559 Inst.addOperand(MCOperand::createReg(Base));
1560 Inst.addOperand(MCOperand::createImm(Offset));
1562 return MCDisassembler::Success;
1565 static DecodeStatus DecodeFMem(MCInst &Inst,
1568 const void *Decoder) {
1569 int Offset = SignExtend32<16>(Insn & 0xffff);
1570 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1571 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1573 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1574 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1576 Inst.addOperand(MCOperand::createReg(Reg));
1577 Inst.addOperand(MCOperand::createReg(Base));
1578 Inst.addOperand(MCOperand::createImm(Offset));
1580 return MCDisassembler::Success;
1583 static DecodeStatus DecodeFMem2(MCInst &Inst,
1586 const void *Decoder) {
1587 int Offset = SignExtend32<16>(Insn & 0xffff);
1588 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1589 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1591 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1592 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1594 Inst.addOperand(MCOperand::createReg(Reg));
1595 Inst.addOperand(MCOperand::createReg(Base));
1596 Inst.addOperand(MCOperand::createImm(Offset));
1598 return MCDisassembler::Success;
1601 static DecodeStatus DecodeFMem3(MCInst &Inst,
1604 const void *Decoder) {
1605 int Offset = SignExtend32<16>(Insn & 0xffff);
1606 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1607 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1609 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1610 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1612 Inst.addOperand(MCOperand::createReg(Reg));
1613 Inst.addOperand(MCOperand::createReg(Base));
1614 Inst.addOperand(MCOperand::createImm(Offset));
1616 return MCDisassembler::Success;
1619 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1622 const void *Decoder) {
1623 int Offset = SignExtend32<11>(Insn & 0x07ff);
1624 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1625 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1627 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1628 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1630 Inst.addOperand(MCOperand::createReg(Reg));
1631 Inst.addOperand(MCOperand::createReg(Base));
1632 Inst.addOperand(MCOperand::createImm(Offset));
1634 return MCDisassembler::Success;
1636 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1639 const void *Decoder) {
1640 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1641 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1642 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1644 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1645 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1647 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1648 Inst.addOperand(MCOperand::createReg(Rt));
1651 Inst.addOperand(MCOperand::createReg(Rt));
1652 Inst.addOperand(MCOperand::createReg(Base));
1653 Inst.addOperand(MCOperand::createImm(Offset));
1655 return MCDisassembler::Success;
1658 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1661 const void *Decoder) {
1662 // Currently only hardware register 29 is supported.
1664 return MCDisassembler::Fail;
1665 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
1666 return MCDisassembler::Success;
1669 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1672 const void *Decoder) {
1673 if (RegNo > 30 || RegNo %2)
1674 return MCDisassembler::Fail;
1677 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1678 Inst.addOperand(MCOperand::createReg(Reg));
1679 return MCDisassembler::Success;
1682 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1685 const void *Decoder) {
1687 return MCDisassembler::Fail;
1689 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1690 Inst.addOperand(MCOperand::createReg(Reg));
1691 return MCDisassembler::Success;
1694 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1697 const void *Decoder) {
1699 return MCDisassembler::Fail;
1701 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1702 Inst.addOperand(MCOperand::createReg(Reg));
1703 return MCDisassembler::Success;
1706 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1709 const void *Decoder) {
1711 return MCDisassembler::Fail;
1713 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1714 Inst.addOperand(MCOperand::createReg(Reg));
1715 return MCDisassembler::Success;
1718 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1721 const void *Decoder) {
1723 return MCDisassembler::Fail;
1725 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1726 Inst.addOperand(MCOperand::createReg(Reg));
1727 return MCDisassembler::Success;
1730 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1733 const void *Decoder) {
1735 return MCDisassembler::Fail;
1737 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1738 Inst.addOperand(MCOperand::createReg(Reg));
1739 return MCDisassembler::Success;
1742 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1745 const void *Decoder) {
1747 return MCDisassembler::Fail;
1749 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1750 Inst.addOperand(MCOperand::createReg(Reg));
1751 return MCDisassembler::Success;
1754 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1757 const void *Decoder) {
1759 return MCDisassembler::Fail;
1761 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1762 Inst.addOperand(MCOperand::createReg(Reg));
1763 return MCDisassembler::Success;
1766 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1769 const void *Decoder) {
1771 return MCDisassembler::Fail;
1773 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1774 Inst.addOperand(MCOperand::createReg(Reg));
1775 return MCDisassembler::Success;
1778 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
1781 const void *Decoder) {
1783 return MCDisassembler::Fail;
1785 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
1786 Inst.addOperand(MCOperand::createReg(Reg));
1787 return MCDisassembler::Success;
1790 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1793 const void *Decoder) {
1795 return MCDisassembler::Fail;
1797 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1798 Inst.addOperand(MCOperand::createReg(Reg));
1799 return MCDisassembler::Success;
1802 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1805 const void *Decoder) {
1806 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1807 Inst.addOperand(MCOperand::createImm(BranchOffset));
1808 return MCDisassembler::Success;
1811 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1814 const void *Decoder) {
1816 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1817 Inst.addOperand(MCOperand::createImm(JumpOffset));
1818 return MCDisassembler::Success;
1821 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1824 const void *Decoder) {
1825 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1827 Inst.addOperand(MCOperand::createImm(BranchOffset));
1828 return MCDisassembler::Success;
1831 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1834 const void *Decoder) {
1835 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1837 Inst.addOperand(MCOperand::createImm(BranchOffset));
1838 return MCDisassembler::Success;
1841 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1844 const void *Decoder) {
1845 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1846 Inst.addOperand(MCOperand::createImm(BranchOffset));
1847 return MCDisassembler::Success;
1850 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1853 const void *Decoder) {
1854 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1855 Inst.addOperand(MCOperand::createImm(BranchOffset));
1856 return MCDisassembler::Success;
1859 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1862 const void *Decoder) {
1863 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1864 Inst.addOperand(MCOperand::createImm(BranchOffset));
1865 return MCDisassembler::Success;
1868 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
1871 const void *Decoder) {
1872 int32_t BranchOffset = SignExtend32<26>(Offset) << 1;
1874 Inst.addOperand(MCOperand::createImm(BranchOffset));
1875 return MCDisassembler::Success;
1878 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1881 const void *Decoder) {
1882 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1883 Inst.addOperand(MCOperand::createImm(JumpOffset));
1884 return MCDisassembler::Success;
1887 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1890 const void *Decoder) {
1892 Inst.addOperand(MCOperand::createImm(1));
1893 else if (Value == 0x7)
1894 Inst.addOperand(MCOperand::createImm(-1));
1896 Inst.addOperand(MCOperand::createImm(Value << 2));
1897 return MCDisassembler::Success;
1900 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1903 const void *Decoder) {
1904 Inst.addOperand(MCOperand::createImm(Value << 2));
1905 return MCDisassembler::Success;
1908 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1911 const void *Decoder) {
1913 Inst.addOperand(MCOperand::createImm(-1));
1915 Inst.addOperand(MCOperand::createImm(Value));
1916 return MCDisassembler::Success;
1919 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
1922 const void *Decoder) {
1923 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
1924 return MCDisassembler::Success;
1927 static DecodeStatus DecodeSimm4(MCInst &Inst,
1930 const void *Decoder) {
1931 Inst.addOperand(MCOperand::createImm(SignExtend32<4>(Value)));
1932 return MCDisassembler::Success;
1935 static DecodeStatus DecodeSimm16(MCInst &Inst,
1938 const void *Decoder) {
1939 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Insn)));
1940 return MCDisassembler::Success;
1943 template <unsigned Bits, int Offset>
1944 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
1946 const void *Decoder) {
1947 Value &= ((1 << Bits) - 1);
1948 Inst.addOperand(MCOperand::createImm(Value + Offset));
1949 return MCDisassembler::Success;
1952 static DecodeStatus DecodeInsSize(MCInst &Inst,
1955 const void *Decoder) {
1956 // First we need to grab the pos(lsb) from MCInst.
1957 int Pos = Inst.getOperand(2).getImm();
1958 int Size = (int) Insn - Pos + 1;
1959 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
1960 return MCDisassembler::Success;
1963 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1964 uint64_t Address, const void *Decoder) {
1965 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
1966 return MCDisassembler::Success;
1969 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1970 uint64_t Address, const void *Decoder) {
1971 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
1972 return MCDisassembler::Success;
1975 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1976 uint64_t Address, const void *Decoder) {
1977 int32_t DecodedValue;
1979 case 0: DecodedValue = 256; break;
1980 case 1: DecodedValue = 257; break;
1981 case 510: DecodedValue = -258; break;
1982 case 511: DecodedValue = -257; break;
1983 default: DecodedValue = SignExtend32<9>(Insn); break;
1985 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
1986 return MCDisassembler::Success;
1989 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1990 uint64_t Address, const void *Decoder) {
1991 // Insn must be >= 0, since it is unsigned that condition is always true.
1993 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1995 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
1996 return MCDisassembler::Success;
1999 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
2000 uint64_t Address, const void *Decoder) {
2001 Inst.addOperand(MCOperand::createImm(Insn << 2));
2002 return MCDisassembler::Success;
2005 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2008 const void *Decoder) {
2009 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2010 Mips::S6, Mips::S7, Mips::FP};
2013 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2015 // Empty register lists are not allowed.
2017 return MCDisassembler::Fail;
2019 RegNum = RegLst & 0xf;
2021 // RegLst values 10-15, and 26-31 are reserved.
2023 return MCDisassembler::Fail;
2025 for (unsigned i = 0; i < RegNum; i++)
2026 Inst.addOperand(MCOperand::createReg(Regs[i]));
2029 Inst.addOperand(MCOperand::createReg(Mips::RA));
2031 return MCDisassembler::Success;
2034 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2036 const void *Decoder) {
2037 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2039 switch(Inst.getOpcode()) {
2041 RegLst = fieldFromInstruction(Insn, 4, 2);
2043 case Mips::LWM16_MMR6:
2044 case Mips::SWM16_MMR6:
2045 RegLst = fieldFromInstruction(Insn, 8, 2);
2048 unsigned RegNum = RegLst & 0x3;
2050 for (unsigned i = 0; i <= RegNum; i++)
2051 Inst.addOperand(MCOperand::createReg(Regs[i]));
2053 Inst.addOperand(MCOperand::createReg(Mips::RA));
2055 return MCDisassembler::Success;
2058 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
2059 uint64_t Address, const void *Decoder) {
2061 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2065 return MCDisassembler::Fail;
2067 Inst.addOperand(MCOperand::createReg(Mips::A1));
2068 Inst.addOperand(MCOperand::createReg(Mips::A2));
2071 Inst.addOperand(MCOperand::createReg(Mips::A1));
2072 Inst.addOperand(MCOperand::createReg(Mips::A3));
2075 Inst.addOperand(MCOperand::createReg(Mips::A2));
2076 Inst.addOperand(MCOperand::createReg(Mips::A3));
2079 Inst.addOperand(MCOperand::createReg(Mips::A0));
2080 Inst.addOperand(MCOperand::createReg(Mips::S5));
2083 Inst.addOperand(MCOperand::createReg(Mips::A0));
2084 Inst.addOperand(MCOperand::createReg(Mips::S6));
2087 Inst.addOperand(MCOperand::createReg(Mips::A0));
2088 Inst.addOperand(MCOperand::createReg(Mips::A1));
2091 Inst.addOperand(MCOperand::createReg(Mips::A0));
2092 Inst.addOperand(MCOperand::createReg(Mips::A2));
2095 Inst.addOperand(MCOperand::createReg(Mips::A0));
2096 Inst.addOperand(MCOperand::createReg(Mips::A3));
2100 return MCDisassembler::Success;
2103 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2104 uint64_t Address, const void *Decoder) {
2105 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2106 return MCDisassembler::Success;