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 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
233 // shifted left by 1 bit.
234 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
237 const void *Decoder);
239 static DecodeStatus DecodeMem(MCInst &Inst,
242 const void *Decoder);
244 static DecodeStatus DecodeMemEVA(MCInst &Inst,
247 const void *Decoder);
249 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
252 const void *Decoder);
254 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
257 const void *Decoder);
259 static DecodeStatus DecodeCacheOp(MCInst &Inst,
262 const void *Decoder);
264 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
267 const void *Decoder);
269 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
272 const void *Decoder);
274 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
277 const void *Decoder);
279 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
282 const void *Decoder);
284 static DecodeStatus DecodeSyncI(MCInst &Inst,
287 const void *Decoder);
289 static DecodeStatus DecodeSynciR6(MCInst &Inst,
292 const void *Decoder);
294 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
295 uint64_t Address, const void *Decoder);
297 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
305 const void *Decoder);
307 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
310 const void *Decoder);
312 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
315 const void *Decoder);
317 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
320 const void *Decoder);
322 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
325 const void *Decoder);
327 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
330 const void *Decoder);
332 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
334 const void *Decoder);
336 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
338 const void *Decoder);
340 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
342 const void *Decoder);
344 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
346 const void *Decoder);
348 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
351 const void *Decoder);
353 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
356 const void *Decoder);
358 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
361 const void *Decoder);
363 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
366 const void *Decoder);
368 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
371 const void *Decoder);
373 static DecodeStatus DecodeSimm4(MCInst &Inst,
376 const void *Decoder);
378 static DecodeStatus DecodeSimm16(MCInst &Inst,
381 const void *Decoder);
383 template <unsigned Bits, int Offset>
384 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
385 uint64_t Address, const void *Decoder);
387 static DecodeStatus DecodeInsSize(MCInst &Inst,
390 const void *Decoder);
392 static DecodeStatus DecodeExtSize(MCInst &Inst,
395 const void *Decoder);
397 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
398 uint64_t Address, const void *Decoder);
400 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
401 uint64_t Address, const void *Decoder);
403 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
404 uint64_t Address, const void *Decoder);
406 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
407 uint64_t Address, const void *Decoder);
409 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
410 uint64_t Address, const void *Decoder);
412 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
413 uint64_t Address, const void *Decoder);
415 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
417 template <typename InsnType>
418 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
419 const void *Decoder);
421 template <typename InsnType>
423 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
424 const void *Decoder);
426 template <typename InsnType>
428 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
429 const void *Decoder);
431 template <typename InsnType>
433 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
434 const void *Decoder);
436 template <typename InsnType>
438 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
439 const void *Decoder);
441 template <typename InsnType>
443 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
444 const void *Decoder);
446 template <typename InsnType>
448 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
449 const void *Decoder);
451 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
453 const void *Decoder);
455 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
457 const void *Decoder);
459 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
461 const void *Decoder);
464 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
468 static MCDisassembler *createMipsDisassembler(
470 const MCSubtargetInfo &STI,
472 return new MipsDisassembler(STI, Ctx, true);
475 static MCDisassembler *createMipselDisassembler(
477 const MCSubtargetInfo &STI,
479 return new MipsDisassembler(STI, Ctx, false);
482 extern "C" void LLVMInitializeMipsDisassembler() {
483 // Register the disassembler.
484 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
485 createMipsDisassembler);
486 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
487 createMipselDisassembler);
488 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
489 createMipsDisassembler);
490 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
491 createMipselDisassembler);
494 #include "MipsGenDisassemblerTables.inc"
496 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
497 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
498 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
499 return *(RegInfo->getRegClass(RC).begin() + RegNo);
502 template <typename InsnType>
503 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
504 const void *Decoder) {
505 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
506 // The size of the n field depends on the element size
507 // The register class also depends on this.
508 InsnType tmp = fieldFromInstruction(insn, 17, 5);
510 DecodeFN RegDecoder = nullptr;
511 if ((tmp & 0x18) == 0x00) { // INSVE_B
513 RegDecoder = DecodeMSA128BRegisterClass;
514 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
516 RegDecoder = DecodeMSA128HRegisterClass;
517 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
519 RegDecoder = DecodeMSA128WRegisterClass;
520 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
522 RegDecoder = DecodeMSA128DRegisterClass;
524 llvm_unreachable("Invalid encoding");
526 assert(NSize != 0 && RegDecoder != nullptr);
529 tmp = fieldFromInstruction(insn, 6, 5);
530 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
531 return MCDisassembler::Fail;
533 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
534 return MCDisassembler::Fail;
536 tmp = fieldFromInstruction(insn, 16, NSize);
537 MI.addOperand(MCOperand::createImm(tmp));
539 tmp = fieldFromInstruction(insn, 11, 5);
540 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
541 return MCDisassembler::Fail;
543 MI.addOperand(MCOperand::createImm(0));
545 return MCDisassembler::Success;
548 template <typename InsnType>
549 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
551 const void *Decoder) {
552 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
553 // (otherwise we would have matched the ADDI instruction from the earlier
557 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
559 // BEQZALC if rs == 0 && rt != 0
560 // BEQC if rs < rt && rs != 0
562 InsnType Rs = fieldFromInstruction(insn, 21, 5);
563 InsnType Rt = fieldFromInstruction(insn, 16, 5);
564 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
568 MI.setOpcode(Mips::BOVC);
570 } else if (Rs != 0 && Rs < Rt) {
571 MI.setOpcode(Mips::BEQC);
574 MI.setOpcode(Mips::BEQZALC);
577 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
580 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
582 MI.addOperand(MCOperand::createImm(Imm));
584 return MCDisassembler::Success;
587 template <typename InsnType>
588 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
590 const void *Decoder) {
591 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
592 // (otherwise we would have matched the ADDI instruction from the earlier
596 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
598 // BNEZALC if rs == 0 && rt != 0
599 // BNEC if rs < rt && rs != 0
601 InsnType Rs = fieldFromInstruction(insn, 21, 5);
602 InsnType Rt = fieldFromInstruction(insn, 16, 5);
603 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
607 MI.setOpcode(Mips::BNVC);
609 } else if (Rs != 0 && Rs < Rt) {
610 MI.setOpcode(Mips::BNEC);
613 MI.setOpcode(Mips::BNEZALC);
616 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
619 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
621 MI.addOperand(MCOperand::createImm(Imm));
623 return MCDisassembler::Success;
626 template <typename InsnType>
627 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
629 const void *Decoder) {
630 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
631 // (otherwise we would have matched the BLEZL instruction from the earlier
635 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
636 // Invalid if rs == 0
637 // BLEZC if rs == 0 && rt != 0
638 // BGEZC if rs == rt && rt != 0
639 // BGEC if rs != rt && rs != 0 && rt != 0
641 InsnType Rs = fieldFromInstruction(insn, 21, 5);
642 InsnType Rt = fieldFromInstruction(insn, 16, 5);
643 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
647 return MCDisassembler::Fail;
649 MI.setOpcode(Mips::BLEZC);
651 MI.setOpcode(Mips::BGEZC);
654 MI.setOpcode(Mips::BGEC);
658 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
661 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
664 MI.addOperand(MCOperand::createImm(Imm));
666 return MCDisassembler::Success;
669 template <typename InsnType>
670 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
672 const void *Decoder) {
673 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
674 // (otherwise we would have matched the BGTZL instruction from the earlier
678 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
679 // Invalid if rs == 0
680 // BGTZC if rs == 0 && rt != 0
681 // BLTZC if rs == rt && rt != 0
682 // BLTC if rs != rt && rs != 0 && rt != 0
686 InsnType Rs = fieldFromInstruction(insn, 21, 5);
687 InsnType Rt = fieldFromInstruction(insn, 16, 5);
688 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
691 return MCDisassembler::Fail;
693 MI.setOpcode(Mips::BGTZC);
695 MI.setOpcode(Mips::BLTZC);
697 MI.setOpcode(Mips::BLTC);
702 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
705 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
708 MI.addOperand(MCOperand::createImm(Imm));
710 return MCDisassembler::Success;
713 template <typename InsnType>
714 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
716 const void *Decoder) {
717 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
718 // (otherwise we would have matched the BGTZ instruction from the earlier
722 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
724 // BGTZALC if rs == 0 && rt != 0
725 // BLTZALC if rs != 0 && rs == rt
726 // BLTUC if rs != 0 && rs != rt
728 InsnType Rs = fieldFromInstruction(insn, 21, 5);
729 InsnType Rt = fieldFromInstruction(insn, 16, 5);
730 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
735 MI.setOpcode(Mips::BGTZ);
737 } else if (Rs == 0) {
738 MI.setOpcode(Mips::BGTZALC);
740 } else if (Rs == Rt) {
741 MI.setOpcode(Mips::BLTZALC);
744 MI.setOpcode(Mips::BLTUC);
750 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
754 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
757 MI.addOperand(MCOperand::createImm(Imm));
759 return MCDisassembler::Success;
762 template <typename InsnType>
763 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
765 const void *Decoder) {
766 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
767 // (otherwise we would have matched the BLEZL instruction from the earlier
771 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
772 // Invalid if rs == 0
773 // BLEZALC if rs == 0 && rt != 0
774 // BGEZALC if rs == rt && rt != 0
775 // BGEUC if rs != rt && rs != 0 && rt != 0
777 InsnType Rs = fieldFromInstruction(insn, 21, 5);
778 InsnType Rt = fieldFromInstruction(insn, 16, 5);
779 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
783 return MCDisassembler::Fail;
785 MI.setOpcode(Mips::BLEZALC);
787 MI.setOpcode(Mips::BGEZALC);
790 MI.setOpcode(Mips::BGEUC);
794 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
796 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
799 MI.addOperand(MCOperand::createImm(Imm));
801 return MCDisassembler::Success;
804 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
805 /// according to the given endianess.
806 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
807 uint64_t &Size, uint32_t &Insn,
809 // We want to read exactly 2 Bytes of data.
810 if (Bytes.size() < 2) {
812 return MCDisassembler::Fail;
816 Insn = (Bytes[0] << 8) | Bytes[1];
818 Insn = (Bytes[1] << 8) | Bytes[0];
821 return MCDisassembler::Success;
824 /// Read four bytes from the ArrayRef and return 32 bit word sorted
825 /// according to the given endianess
826 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
827 uint64_t &Size, uint32_t &Insn,
828 bool IsBigEndian, bool IsMicroMips) {
829 // We want to read exactly 4 Bytes of data.
830 if (Bytes.size() < 4) {
832 return MCDisassembler::Fail;
835 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
836 // always precede the low 16 bits in the instruction stream (that is, they
837 // are placed at lower addresses in the instruction stream).
839 // microMIPS byte ordering:
840 // Big-endian: 0 | 1 | 2 | 3
841 // Little-endian: 1 | 0 | 3 | 2
844 // Encoded as a big-endian 32-bit word in the stream.
846 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
849 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
852 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
857 return MCDisassembler::Success;
860 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
861 ArrayRef<uint8_t> Bytes,
863 raw_ostream &VStream,
864 raw_ostream &CStream) const {
869 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
870 if (Result == MCDisassembler::Fail)
871 return MCDisassembler::Fail;
874 DEBUG(dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
875 // Calling the auto-generated decoder function for microMIPS32R6
876 // (and microMIPS64R6) 16-bit instructions.
877 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
879 if (Result != MCDisassembler::Fail) {
885 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
886 // Calling the auto-generated decoder function for microMIPS 16-bit
888 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
890 if (Result != MCDisassembler::Fail) {
895 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
896 if (Result == MCDisassembler::Fail)
897 return MCDisassembler::Fail;
900 DEBUG(dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
901 // Calling the auto-generated decoder function.
902 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
904 if (Result != MCDisassembler::Fail) {
910 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
911 // Calling the auto-generated decoder function.
912 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
914 if (Result != MCDisassembler::Fail) {
918 // This is an invalid instruction. Let the disassembler move forward by the
919 // minimum instruction size.
921 return MCDisassembler::Fail;
924 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
925 if (Result == MCDisassembler::Fail) {
927 return MCDisassembler::Fail;
931 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
933 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
934 if (Result != MCDisassembler::Fail) {
940 if (hasMips32r6() && isGP64()) {
941 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
942 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
944 if (Result != MCDisassembler::Fail) {
951 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
952 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
954 if (Result != MCDisassembler::Fail) {
961 DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
962 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
964 if (Result != MCDisassembler::Fail) {
971 DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
972 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
974 if (Result != MCDisassembler::Fail) {
980 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
981 // Calling the auto-generated decoder function.
983 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
984 if (Result != MCDisassembler::Fail) {
990 return MCDisassembler::Fail;
993 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
996 const void *Decoder) {
998 return MCDisassembler::Fail;
1002 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1005 const void *Decoder) {
1008 return MCDisassembler::Fail;
1010 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1011 Inst.addOperand(MCOperand::createReg(Reg));
1012 return MCDisassembler::Success;
1015 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1018 const void *Decoder) {
1020 return MCDisassembler::Fail;
1021 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1022 Inst.addOperand(MCOperand::createReg(Reg));
1023 return MCDisassembler::Success;
1026 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1029 const void *Decoder) {
1031 return MCDisassembler::Fail;
1032 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1033 Inst.addOperand(MCOperand::createReg(Reg));
1034 return MCDisassembler::Success;
1037 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1040 const void *Decoder) {
1042 return MCDisassembler::Fail;
1043 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1044 Inst.addOperand(MCOperand::createReg(Reg));
1045 return MCDisassembler::Success;
1048 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1051 const void *Decoder) {
1053 return MCDisassembler::Fail;
1054 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1055 Inst.addOperand(MCOperand::createReg(Reg));
1056 return MCDisassembler::Success;
1059 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1062 const void *Decoder) {
1063 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1064 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1066 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1069 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1072 const void *Decoder) {
1073 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1076 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1079 const void *Decoder) {
1081 return MCDisassembler::Fail;
1083 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1084 Inst.addOperand(MCOperand::createReg(Reg));
1085 return MCDisassembler::Success;
1088 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1091 const void *Decoder) {
1093 return MCDisassembler::Fail;
1095 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1096 Inst.addOperand(MCOperand::createReg(Reg));
1097 return MCDisassembler::Success;
1100 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1103 const void *Decoder) {
1105 return MCDisassembler::Fail;
1106 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1107 Inst.addOperand(MCOperand::createReg(Reg));
1108 return MCDisassembler::Success;
1111 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1114 const void *Decoder) {
1116 return MCDisassembler::Fail;
1117 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1118 Inst.addOperand(MCOperand::createReg(Reg));
1119 return MCDisassembler::Success;
1122 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1124 const void *Decoder) {
1126 return MCDisassembler::Fail;
1128 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1129 Inst.addOperand(MCOperand::createReg(Reg));
1130 return MCDisassembler::Success;
1133 static DecodeStatus DecodeMem(MCInst &Inst,
1136 const void *Decoder) {
1137 int Offset = SignExtend32<16>(Insn & 0xffff);
1138 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1139 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1141 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1142 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1144 if (Inst.getOpcode() == Mips::SC ||
1145 Inst.getOpcode() == Mips::SCD)
1146 Inst.addOperand(MCOperand::createReg(Reg));
1148 Inst.addOperand(MCOperand::createReg(Reg));
1149 Inst.addOperand(MCOperand::createReg(Base));
1150 Inst.addOperand(MCOperand::createImm(Offset));
1152 return MCDisassembler::Success;
1155 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1158 const void *Decoder) {
1159 int Offset = SignExtend32<9>(Insn >> 7);
1160 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1161 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1163 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1164 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1166 if (Inst.getOpcode() == Mips::SCE)
1167 Inst.addOperand(MCOperand::createReg(Reg));
1169 Inst.addOperand(MCOperand::createReg(Reg));
1170 Inst.addOperand(MCOperand::createReg(Base));
1171 Inst.addOperand(MCOperand::createImm(Offset));
1173 return MCDisassembler::Success;
1176 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
1179 const void *Decoder) {
1180 int Offset = SignExtend32<9>(Insn & 0x1ff);
1181 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1182 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1184 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1185 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1187 Inst.addOperand(MCOperand::createReg(Reg));
1188 Inst.addOperand(MCOperand::createReg(Base));
1189 Inst.addOperand(MCOperand::createImm(Offset));
1191 return MCDisassembler::Success;
1194 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1197 const void *Decoder) {
1198 int Offset = SignExtend32<16>(Insn & 0xffff);
1199 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1200 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1202 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1203 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1205 Inst.addOperand(MCOperand::createReg(Reg));
1206 Inst.addOperand(MCOperand::createReg(Base));
1207 Inst.addOperand(MCOperand::createImm(Offset));
1209 return MCDisassembler::Success;
1212 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1215 const void *Decoder) {
1216 int Offset = SignExtend32<16>(Insn & 0xffff);
1217 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1218 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1220 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1222 Inst.addOperand(MCOperand::createReg(Base));
1223 Inst.addOperand(MCOperand::createImm(Offset));
1224 Inst.addOperand(MCOperand::createImm(Hint));
1226 return MCDisassembler::Success;
1229 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1232 const void *Decoder) {
1233 int Offset = SignExtend32<12>(Insn & 0xfff);
1234 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1235 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1237 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1239 Inst.addOperand(MCOperand::createReg(Base));
1240 Inst.addOperand(MCOperand::createImm(Offset));
1241 Inst.addOperand(MCOperand::createImm(Hint));
1243 return MCDisassembler::Success;
1246 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1249 const void *Decoder) {
1250 int Offset = SignExtend32<9>(Insn & 0x1ff);
1251 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1252 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1254 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1256 Inst.addOperand(MCOperand::createReg(Base));
1257 Inst.addOperand(MCOperand::createImm(Offset));
1258 Inst.addOperand(MCOperand::createImm(Hint));
1260 return MCDisassembler::Success;
1263 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1266 const void *Decoder) {
1267 int Offset = SignExtend32<9>(Insn >> 7);
1268 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1269 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1271 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1273 Inst.addOperand(MCOperand::createReg(Base));
1274 Inst.addOperand(MCOperand::createImm(Offset));
1275 Inst.addOperand(MCOperand::createImm(Hint));
1277 return MCDisassembler::Success;
1280 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
1283 const void *Decoder) {
1284 int Offset = SignExtend32<9>(Insn & 0x1ff);
1285 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1286 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1288 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1289 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1291 Inst.addOperand(MCOperand::createReg(Reg));
1292 Inst.addOperand(MCOperand::createReg(Base));
1293 Inst.addOperand(MCOperand::createImm(Offset));
1295 return MCDisassembler::Success;
1298 static DecodeStatus DecodeSyncI(MCInst &Inst,
1301 const void *Decoder) {
1302 int Offset = SignExtend32<16>(Insn & 0xffff);
1303 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1305 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1307 Inst.addOperand(MCOperand::createReg(Base));
1308 Inst.addOperand(MCOperand::createImm(Offset));
1310 return MCDisassembler::Success;
1313 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1316 const void *Decoder) {
1317 int Immediate = SignExtend32<16>(Insn & 0xffff);
1318 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1320 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1322 Inst.addOperand(MCOperand::createReg(Base));
1323 Inst.addOperand(MCOperand::createImm(Immediate));
1325 return MCDisassembler::Success;
1328 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1329 uint64_t Address, const void *Decoder) {
1330 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1331 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1332 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1334 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1335 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1337 Inst.addOperand(MCOperand::createReg(Reg));
1338 Inst.addOperand(MCOperand::createReg(Base));
1340 // The immediate field of an LD/ST instruction is scaled which means it must
1341 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1347 switch(Inst.getOpcode())
1350 assert (0 && "Unexpected instruction");
1351 return MCDisassembler::Fail;
1355 Inst.addOperand(MCOperand::createImm(Offset));
1359 Inst.addOperand(MCOperand::createImm(Offset * 2));
1363 Inst.addOperand(MCOperand::createImm(Offset * 4));
1367 Inst.addOperand(MCOperand::createImm(Offset * 8));
1371 return MCDisassembler::Success;
1374 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1377 const void *Decoder) {
1378 unsigned Offset = Insn & 0xf;
1379 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1380 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1382 switch (Inst.getOpcode()) {
1383 case Mips::LBU16_MM:
1384 case Mips::LHU16_MM:
1386 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1387 == MCDisassembler::Fail)
1388 return MCDisassembler::Fail;
1391 case Mips::SB16_MMR6:
1393 case Mips::SH16_MMR6:
1395 case Mips::SW16_MMR6:
1396 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1397 == MCDisassembler::Fail)
1398 return MCDisassembler::Fail;
1402 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1403 == MCDisassembler::Fail)
1404 return MCDisassembler::Fail;
1406 switch (Inst.getOpcode()) {
1407 case Mips::LBU16_MM:
1409 Inst.addOperand(MCOperand::createImm(-1));
1411 Inst.addOperand(MCOperand::createImm(Offset));
1414 case Mips::SB16_MMR6:
1415 Inst.addOperand(MCOperand::createImm(Offset));
1417 case Mips::LHU16_MM:
1419 case Mips::SH16_MMR6:
1420 Inst.addOperand(MCOperand::createImm(Offset << 1));
1424 case Mips::SW16_MMR6:
1425 Inst.addOperand(MCOperand::createImm(Offset << 2));
1429 return MCDisassembler::Success;
1432 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1435 const void *Decoder) {
1436 unsigned Offset = Insn & 0x1F;
1437 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1439 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1441 Inst.addOperand(MCOperand::createReg(Reg));
1442 Inst.addOperand(MCOperand::createReg(Mips::SP));
1443 Inst.addOperand(MCOperand::createImm(Offset << 2));
1445 return MCDisassembler::Success;
1448 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1451 const void *Decoder) {
1452 unsigned Offset = Insn & 0x7F;
1453 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1455 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1457 Inst.addOperand(MCOperand::createReg(Reg));
1458 Inst.addOperand(MCOperand::createReg(Mips::GP));
1459 Inst.addOperand(MCOperand::createImm(Offset << 2));
1461 return MCDisassembler::Success;
1464 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1467 const void *Decoder) {
1469 switch (Inst.getOpcode()) {
1470 case Mips::LWM16_MMR6:
1471 case Mips::SWM16_MMR6:
1472 Offset = fieldFromInstruction(Insn, 4, 4);
1475 Offset = SignExtend32<4>(Insn & 0xf);
1479 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1480 == MCDisassembler::Fail)
1481 return MCDisassembler::Fail;
1483 Inst.addOperand(MCOperand::createReg(Mips::SP));
1484 Inst.addOperand(MCOperand::createImm(Offset << 2));
1486 return MCDisassembler::Success;
1489 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1492 const void *Decoder) {
1493 int Offset = SignExtend32<9>(Insn & 0x1ff);
1494 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1495 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1497 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1498 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1500 if (Inst.getOpcode() == Mips::SCE_MM)
1501 Inst.addOperand(MCOperand::createReg(Reg));
1503 Inst.addOperand(MCOperand::createReg(Reg));
1504 Inst.addOperand(MCOperand::createReg(Base));
1505 Inst.addOperand(MCOperand::createImm(Offset));
1507 return MCDisassembler::Success;
1510 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1513 const void *Decoder) {
1514 int Offset = SignExtend32<12>(Insn & 0x0fff);
1515 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1516 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1518 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1519 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1521 switch (Inst.getOpcode()) {
1522 case Mips::SWM32_MM:
1523 case Mips::LWM32_MM:
1524 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1525 == MCDisassembler::Fail)
1526 return MCDisassembler::Fail;
1527 Inst.addOperand(MCOperand::createReg(Base));
1528 Inst.addOperand(MCOperand::createImm(Offset));
1531 Inst.addOperand(MCOperand::createReg(Reg));
1534 Inst.addOperand(MCOperand::createReg(Reg));
1535 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1536 Inst.addOperand(MCOperand::createReg(Reg+1));
1538 Inst.addOperand(MCOperand::createReg(Base));
1539 Inst.addOperand(MCOperand::createImm(Offset));
1542 return MCDisassembler::Success;
1545 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1548 const void *Decoder) {
1549 int Offset = SignExtend32<16>(Insn & 0xffff);
1550 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1551 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1553 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1554 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1556 Inst.addOperand(MCOperand::createReg(Reg));
1557 Inst.addOperand(MCOperand::createReg(Base));
1558 Inst.addOperand(MCOperand::createImm(Offset));
1560 return MCDisassembler::Success;
1563 static DecodeStatus DecodeFMem(MCInst &Inst,
1566 const void *Decoder) {
1567 int Offset = SignExtend32<16>(Insn & 0xffff);
1568 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1569 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1571 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1572 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1574 Inst.addOperand(MCOperand::createReg(Reg));
1575 Inst.addOperand(MCOperand::createReg(Base));
1576 Inst.addOperand(MCOperand::createImm(Offset));
1578 return MCDisassembler::Success;
1581 static DecodeStatus DecodeFMem2(MCInst &Inst,
1584 const void *Decoder) {
1585 int Offset = SignExtend32<16>(Insn & 0xffff);
1586 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1587 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1589 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1590 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1592 Inst.addOperand(MCOperand::createReg(Reg));
1593 Inst.addOperand(MCOperand::createReg(Base));
1594 Inst.addOperand(MCOperand::createImm(Offset));
1596 return MCDisassembler::Success;
1599 static DecodeStatus DecodeFMem3(MCInst &Inst,
1602 const void *Decoder) {
1603 int Offset = SignExtend32<16>(Insn & 0xffff);
1604 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1605 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1607 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1608 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1610 Inst.addOperand(MCOperand::createReg(Reg));
1611 Inst.addOperand(MCOperand::createReg(Base));
1612 Inst.addOperand(MCOperand::createImm(Offset));
1614 return MCDisassembler::Success;
1617 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1620 const void *Decoder) {
1621 int Offset = SignExtend32<11>(Insn & 0x07ff);
1622 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1623 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1625 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1626 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1628 Inst.addOperand(MCOperand::createReg(Reg));
1629 Inst.addOperand(MCOperand::createReg(Base));
1630 Inst.addOperand(MCOperand::createImm(Offset));
1632 return MCDisassembler::Success;
1634 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1637 const void *Decoder) {
1638 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1639 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1640 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1642 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1643 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1645 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1646 Inst.addOperand(MCOperand::createReg(Rt));
1649 Inst.addOperand(MCOperand::createReg(Rt));
1650 Inst.addOperand(MCOperand::createReg(Base));
1651 Inst.addOperand(MCOperand::createImm(Offset));
1653 return MCDisassembler::Success;
1656 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1659 const void *Decoder) {
1660 // Currently only hardware register 29 is supported.
1662 return MCDisassembler::Fail;
1663 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
1664 return MCDisassembler::Success;
1667 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1670 const void *Decoder) {
1671 if (RegNo > 30 || RegNo %2)
1672 return MCDisassembler::Fail;
1675 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1676 Inst.addOperand(MCOperand::createReg(Reg));
1677 return MCDisassembler::Success;
1680 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1683 const void *Decoder) {
1685 return MCDisassembler::Fail;
1687 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1688 Inst.addOperand(MCOperand::createReg(Reg));
1689 return MCDisassembler::Success;
1692 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1695 const void *Decoder) {
1697 return MCDisassembler::Fail;
1699 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1700 Inst.addOperand(MCOperand::createReg(Reg));
1701 return MCDisassembler::Success;
1704 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1707 const void *Decoder) {
1709 return MCDisassembler::Fail;
1711 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1712 Inst.addOperand(MCOperand::createReg(Reg));
1713 return MCDisassembler::Success;
1716 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1719 const void *Decoder) {
1721 return MCDisassembler::Fail;
1723 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1724 Inst.addOperand(MCOperand::createReg(Reg));
1725 return MCDisassembler::Success;
1728 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1731 const void *Decoder) {
1733 return MCDisassembler::Fail;
1735 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1736 Inst.addOperand(MCOperand::createReg(Reg));
1737 return MCDisassembler::Success;
1740 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1743 const void *Decoder) {
1745 return MCDisassembler::Fail;
1747 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1748 Inst.addOperand(MCOperand::createReg(Reg));
1749 return MCDisassembler::Success;
1752 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1755 const void *Decoder) {
1757 return MCDisassembler::Fail;
1759 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1760 Inst.addOperand(MCOperand::createReg(Reg));
1761 return MCDisassembler::Success;
1764 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1767 const void *Decoder) {
1769 return MCDisassembler::Fail;
1771 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1772 Inst.addOperand(MCOperand::createReg(Reg));
1773 return MCDisassembler::Success;
1776 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
1779 const void *Decoder) {
1781 return MCDisassembler::Fail;
1783 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
1784 Inst.addOperand(MCOperand::createReg(Reg));
1785 return MCDisassembler::Success;
1788 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1791 const void *Decoder) {
1793 return MCDisassembler::Fail;
1795 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1796 Inst.addOperand(MCOperand::createReg(Reg));
1797 return MCDisassembler::Success;
1800 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1803 const void *Decoder) {
1804 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1805 Inst.addOperand(MCOperand::createImm(BranchOffset));
1806 return MCDisassembler::Success;
1809 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1812 const void *Decoder) {
1814 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1815 Inst.addOperand(MCOperand::createImm(JumpOffset));
1816 return MCDisassembler::Success;
1819 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1822 const void *Decoder) {
1823 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1825 Inst.addOperand(MCOperand::createImm(BranchOffset));
1826 return MCDisassembler::Success;
1829 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1832 const void *Decoder) {
1833 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1835 Inst.addOperand(MCOperand::createImm(BranchOffset));
1836 return MCDisassembler::Success;
1839 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1842 const void *Decoder) {
1843 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1844 Inst.addOperand(MCOperand::createImm(BranchOffset));
1845 return MCDisassembler::Success;
1848 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1851 const void *Decoder) {
1852 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1853 Inst.addOperand(MCOperand::createImm(BranchOffset));
1854 return MCDisassembler::Success;
1857 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1860 const void *Decoder) {
1861 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1862 Inst.addOperand(MCOperand::createImm(BranchOffset));
1863 return MCDisassembler::Success;
1866 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1869 const void *Decoder) {
1870 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1871 Inst.addOperand(MCOperand::createImm(JumpOffset));
1872 return MCDisassembler::Success;
1875 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1878 const void *Decoder) {
1880 Inst.addOperand(MCOperand::createImm(1));
1881 else if (Value == 0x7)
1882 Inst.addOperand(MCOperand::createImm(-1));
1884 Inst.addOperand(MCOperand::createImm(Value << 2));
1885 return MCDisassembler::Success;
1888 static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1891 const void *Decoder) {
1892 Inst.addOperand(MCOperand::createImm(Value << 2));
1893 return MCDisassembler::Success;
1896 static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1899 const void *Decoder) {
1901 Inst.addOperand(MCOperand::createImm(-1));
1903 Inst.addOperand(MCOperand::createImm(Value));
1904 return MCDisassembler::Success;
1907 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
1910 const void *Decoder) {
1911 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
1912 return MCDisassembler::Success;
1915 static DecodeStatus DecodeSimm4(MCInst &Inst,
1918 const void *Decoder) {
1919 Inst.addOperand(MCOperand::createImm(SignExtend32<4>(Value)));
1920 return MCDisassembler::Success;
1923 static DecodeStatus DecodeSimm16(MCInst &Inst,
1926 const void *Decoder) {
1927 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Insn)));
1928 return MCDisassembler::Success;
1931 template <unsigned Bits, int Offset>
1932 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
1934 const void *Decoder) {
1935 Value &= ((1 << Bits) - 1);
1936 Inst.addOperand(MCOperand::createImm(Value + Offset));
1937 return MCDisassembler::Success;
1940 static DecodeStatus DecodeInsSize(MCInst &Inst,
1943 const void *Decoder) {
1944 // First we need to grab the pos(lsb) from MCInst.
1945 int Pos = Inst.getOperand(2).getImm();
1946 int Size = (int) Insn - Pos + 1;
1947 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
1948 return MCDisassembler::Success;
1951 static DecodeStatus DecodeExtSize(MCInst &Inst,
1954 const void *Decoder) {
1955 int Size = (int) Insn + 1;
1956 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
1957 return MCDisassembler::Success;
1960 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1961 uint64_t Address, const void *Decoder) {
1962 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
1963 return MCDisassembler::Success;
1966 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1967 uint64_t Address, const void *Decoder) {
1968 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
1969 return MCDisassembler::Success;
1972 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1973 uint64_t Address, const void *Decoder) {
1974 int32_t DecodedValue;
1976 case 0: DecodedValue = 256; break;
1977 case 1: DecodedValue = 257; break;
1978 case 510: DecodedValue = -258; break;
1979 case 511: DecodedValue = -257; break;
1980 default: DecodedValue = SignExtend32<9>(Insn); break;
1982 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
1983 return MCDisassembler::Success;
1986 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1987 uint64_t Address, const void *Decoder) {
1988 // Insn must be >= 0, since it is unsigned that condition is always true.
1990 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1992 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
1993 return MCDisassembler::Success;
1996 static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1997 uint64_t Address, const void *Decoder) {
1998 Inst.addOperand(MCOperand::createImm(Insn << 2));
1999 return MCDisassembler::Success;
2002 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2005 const void *Decoder) {
2006 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2007 Mips::S6, Mips::S7, Mips::FP};
2010 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2012 // Empty register lists are not allowed.
2014 return MCDisassembler::Fail;
2016 RegNum = RegLst & 0xf;
2018 // RegLst values 10-15, and 26-31 are reserved.
2020 return MCDisassembler::Fail;
2022 for (unsigned i = 0; i < RegNum; i++)
2023 Inst.addOperand(MCOperand::createReg(Regs[i]));
2026 Inst.addOperand(MCOperand::createReg(Mips::RA));
2028 return MCDisassembler::Success;
2031 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2033 const void *Decoder) {
2034 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2036 switch(Inst.getOpcode()) {
2038 RegLst = fieldFromInstruction(Insn, 4, 2);
2040 case Mips::LWM16_MMR6:
2041 case Mips::SWM16_MMR6:
2042 RegLst = fieldFromInstruction(Insn, 8, 2);
2045 unsigned RegNum = RegLst & 0x3;
2047 for (unsigned i = 0; i <= RegNum; i++)
2048 Inst.addOperand(MCOperand::createReg(Regs[i]));
2050 Inst.addOperand(MCOperand::createReg(Mips::RA));
2052 return MCDisassembler::Success;
2055 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
2056 uint64_t Address, const void *Decoder) {
2058 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2062 return MCDisassembler::Fail;
2064 Inst.addOperand(MCOperand::createReg(Mips::A1));
2065 Inst.addOperand(MCOperand::createReg(Mips::A2));
2068 Inst.addOperand(MCOperand::createReg(Mips::A1));
2069 Inst.addOperand(MCOperand::createReg(Mips::A3));
2072 Inst.addOperand(MCOperand::createReg(Mips::A2));
2073 Inst.addOperand(MCOperand::createReg(Mips::A3));
2076 Inst.addOperand(MCOperand::createReg(Mips::A0));
2077 Inst.addOperand(MCOperand::createReg(Mips::S5));
2080 Inst.addOperand(MCOperand::createReg(Mips::A0));
2081 Inst.addOperand(MCOperand::createReg(Mips::S6));
2084 Inst.addOperand(MCOperand::createReg(Mips::A0));
2085 Inst.addOperand(MCOperand::createReg(Mips::A1));
2088 Inst.addOperand(MCOperand::createReg(Mips::A0));
2089 Inst.addOperand(MCOperand::createReg(Mips::A2));
2092 Inst.addOperand(MCOperand::createReg(Mips::A0));
2093 Inst.addOperand(MCOperand::createReg(Mips::A3));
2097 return MCDisassembler::Success;
2100 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2101 uint64_t Address, const void *Decoder) {
2102 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2103 return MCDisassembler::Success;