1 //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the Mips Disassembler.
12 //===----------------------------------------------------------------------===//
15 #include "MipsRegisterInfo.h"
16 #include "MipsSubtarget.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler.h"
19 #include "llvm/MC/MCFixedLenDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/TargetRegistry.h"
27 #define DEBUG_TYPE "mips-disassembler"
29 typedef MCDisassembler::DecodeStatus DecodeStatus;
33 /// A disasembler class for Mips.
34 class MipsDisassemblerBase : public MCDisassembler {
36 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
38 : MCDisassembler(STI, Ctx),
39 IsN64(STI.getFeatureBits() & Mips::FeatureN64),
40 IsBigEndian(IsBigEndian) {}
42 virtual ~MipsDisassemblerBase() {}
44 bool isN64() const { return IsN64; }
52 /// A disasembler class for Mips32.
53 class MipsDisassembler : public MipsDisassemblerBase {
56 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
57 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
58 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
61 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
62 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
63 bool hasMips32r6() const {
64 return STI.getFeatureBits() & Mips::FeatureMips32r6;
67 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
69 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
74 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
75 ArrayRef<uint8_t> Bytes, uint64_t Address,
77 raw_ostream &CStream) const override;
80 /// A disasembler class for Mips64.
81 class Mips64Disassembler : public MipsDisassemblerBase {
83 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
85 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
87 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
88 ArrayRef<uint8_t> Bytes, uint64_t Address,
90 raw_ostream &CStream) const override;
93 } // end anonymous namespace
95 // Forward declare these because the autogenerated code will reference them.
96 // Definitions are further down.
97 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
100 const void *Decoder);
102 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
105 const void *Decoder);
107 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
110 const void *Decoder);
112 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
115 const void *Decoder);
117 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
120 const void *Decoder);
122 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
125 const void *Decoder);
127 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
130 const void *Decoder);
132 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
135 const void *Decoder);
137 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
140 const void *Decoder);
142 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
145 const void *Decoder);
147 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
149 const void *Decoder);
151 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
154 const void *Decoder);
156 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
159 const void *Decoder);
161 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
164 const void *Decoder);
166 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
169 const void *Decoder);
171 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
174 const void *Decoder);
176 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
184 const void *Decoder);
186 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
189 const void *Decoder);
191 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
194 const void *Decoder);
196 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
199 const void *Decoder);
201 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
204 const void *Decoder);
206 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
209 const void *Decoder);
211 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
214 const void *Decoder);
216 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
219 const void *Decoder);
221 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
224 const void *Decoder);
226 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
227 // shifted left by 1 bit.
228 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
231 const void *Decoder);
233 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
234 // shifted left by 1 bit.
235 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
238 const void *Decoder);
240 static DecodeStatus DecodeMem(MCInst &Inst,
243 const void *Decoder);
245 static DecodeStatus DecodeCacheOp(MCInst &Inst,
248 const void *Decoder);
250 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
251 uint64_t Address, const void *Decoder);
253 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
256 const void *Decoder);
258 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
261 const void *Decoder);
263 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
265 const void *Decoder);
267 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
269 const void *Decoder);
271 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
273 const void *Decoder);
275 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
278 const void *Decoder);
280 static DecodeStatus DecodeSimm16(MCInst &Inst,
283 const void *Decoder);
285 // Decode the immediate field of an LSA instruction which
287 static DecodeStatus DecodeLSAImm(MCInst &Inst,
290 const void *Decoder);
292 static DecodeStatus DecodeInsSize(MCInst &Inst,
295 const void *Decoder);
297 static DecodeStatus DecodeExtSize(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
303 uint64_t Address, const void *Decoder);
305 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
306 uint64_t Address, const void *Decoder);
308 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
310 template <typename InsnType>
311 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
312 const void *Decoder);
314 template <typename InsnType>
316 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
317 const void *Decoder);
319 template <typename InsnType>
321 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
322 const void *Decoder);
324 template <typename InsnType>
326 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
327 const void *Decoder);
329 template <typename InsnType>
331 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
332 const void *Decoder);
334 template <typename InsnType>
336 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
337 const void *Decoder);
339 template <typename InsnType>
341 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
342 const void *Decoder);
344 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
346 const void *Decoder);
349 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
353 static MCDisassembler *createMipsDisassembler(
355 const MCSubtargetInfo &STI,
357 return new MipsDisassembler(STI, Ctx, true);
360 static MCDisassembler *createMipselDisassembler(
362 const MCSubtargetInfo &STI,
364 return new MipsDisassembler(STI, Ctx, false);
367 static MCDisassembler *createMips64Disassembler(
369 const MCSubtargetInfo &STI,
371 return new Mips64Disassembler(STI, Ctx, true);
374 static MCDisassembler *createMips64elDisassembler(
376 const MCSubtargetInfo &STI,
378 return new Mips64Disassembler(STI, Ctx, false);
381 extern "C" void LLVMInitializeMipsDisassembler() {
382 // Register the disassembler.
383 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
384 createMipsDisassembler);
385 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
386 createMipselDisassembler);
387 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
388 createMips64Disassembler);
389 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
390 createMips64elDisassembler);
393 #include "MipsGenDisassemblerTables.inc"
395 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
396 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
397 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
398 return *(RegInfo->getRegClass(RC).begin() + RegNo);
401 template <typename InsnType>
402 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
403 const void *Decoder) {
404 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
405 // The size of the n field depends on the element size
406 // The register class also depends on this.
407 InsnType tmp = fieldFromInstruction(insn, 17, 5);
409 DecodeFN RegDecoder = nullptr;
410 if ((tmp & 0x18) == 0x00) { // INSVE_B
412 RegDecoder = DecodeMSA128BRegisterClass;
413 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
415 RegDecoder = DecodeMSA128HRegisterClass;
416 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
418 RegDecoder = DecodeMSA128WRegisterClass;
419 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
421 RegDecoder = DecodeMSA128DRegisterClass;
423 llvm_unreachable("Invalid encoding");
425 assert(NSize != 0 && RegDecoder != nullptr);
428 tmp = fieldFromInstruction(insn, 6, 5);
429 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
430 return MCDisassembler::Fail;
432 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
433 return MCDisassembler::Fail;
435 tmp = fieldFromInstruction(insn, 16, NSize);
436 MI.addOperand(MCOperand::CreateImm(tmp));
438 tmp = fieldFromInstruction(insn, 11, 5);
439 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
440 return MCDisassembler::Fail;
442 MI.addOperand(MCOperand::CreateImm(0));
444 return MCDisassembler::Success;
447 template <typename InsnType>
448 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
450 const void *Decoder) {
451 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
452 // (otherwise we would have matched the ADDI instruction from the earlier
456 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
458 // BEQZALC if rs == 0 && rt != 0
459 // BEQC if rs < rt && rs != 0
461 InsnType Rs = fieldFromInstruction(insn, 21, 5);
462 InsnType Rt = fieldFromInstruction(insn, 16, 5);
463 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
467 MI.setOpcode(Mips::BOVC);
469 } else if (Rs != 0 && Rs < Rt) {
470 MI.setOpcode(Mips::BEQC);
473 MI.setOpcode(Mips::BEQZALC);
476 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
479 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
481 MI.addOperand(MCOperand::CreateImm(Imm));
483 return MCDisassembler::Success;
486 template <typename InsnType>
487 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
489 const void *Decoder) {
490 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
491 // (otherwise we would have matched the ADDI instruction from the earlier
495 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
497 // BNEZALC if rs == 0 && rt != 0
498 // BNEC if rs < rt && rs != 0
500 InsnType Rs = fieldFromInstruction(insn, 21, 5);
501 InsnType Rt = fieldFromInstruction(insn, 16, 5);
502 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
506 MI.setOpcode(Mips::BNVC);
508 } else if (Rs != 0 && Rs < Rt) {
509 MI.setOpcode(Mips::BNEC);
512 MI.setOpcode(Mips::BNEZALC);
515 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
518 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
520 MI.addOperand(MCOperand::CreateImm(Imm));
522 return MCDisassembler::Success;
525 template <typename InsnType>
526 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
528 const void *Decoder) {
529 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
530 // (otherwise we would have matched the BLEZL instruction from the earlier
534 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
535 // Invalid if rs == 0
536 // BLEZC if rs == 0 && rt != 0
537 // BGEZC if rs == rt && rt != 0
538 // BGEC if rs != rt && rs != 0 && rt != 0
540 InsnType Rs = fieldFromInstruction(insn, 21, 5);
541 InsnType Rt = fieldFromInstruction(insn, 16, 5);
542 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
546 return MCDisassembler::Fail;
548 MI.setOpcode(Mips::BLEZC);
550 MI.setOpcode(Mips::BGEZC);
553 MI.setOpcode(Mips::BGEC);
557 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
560 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
563 MI.addOperand(MCOperand::CreateImm(Imm));
565 return MCDisassembler::Success;
568 template <typename InsnType>
569 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
571 const void *Decoder) {
572 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
573 // (otherwise we would have matched the BGTZL instruction from the earlier
577 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
578 // Invalid if rs == 0
579 // BGTZC if rs == 0 && rt != 0
580 // BLTZC if rs == rt && rt != 0
581 // BLTC if rs != rt && rs != 0 && rt != 0
585 InsnType Rs = fieldFromInstruction(insn, 21, 5);
586 InsnType Rt = fieldFromInstruction(insn, 16, 5);
587 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
590 return MCDisassembler::Fail;
592 MI.setOpcode(Mips::BGTZC);
594 MI.setOpcode(Mips::BLTZC);
596 MI.setOpcode(Mips::BLTC);
601 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
604 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
607 MI.addOperand(MCOperand::CreateImm(Imm));
609 return MCDisassembler::Success;
612 template <typename InsnType>
613 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
615 const void *Decoder) {
616 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
617 // (otherwise we would have matched the BGTZ instruction from the earlier
621 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
623 // BGTZALC if rs == 0 && rt != 0
624 // BLTZALC if rs != 0 && rs == rt
625 // BLTUC if rs != 0 && rs != rt
627 InsnType Rs = fieldFromInstruction(insn, 21, 5);
628 InsnType Rt = fieldFromInstruction(insn, 16, 5);
629 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
634 MI.setOpcode(Mips::BGTZ);
636 } else if (Rs == 0) {
637 MI.setOpcode(Mips::BGTZALC);
639 } else if (Rs == Rt) {
640 MI.setOpcode(Mips::BLTZALC);
643 MI.setOpcode(Mips::BLTUC);
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
653 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
656 MI.addOperand(MCOperand::CreateImm(Imm));
658 return MCDisassembler::Success;
661 template <typename InsnType>
662 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
664 const void *Decoder) {
665 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
666 // (otherwise we would have matched the BLEZL instruction from the earlier
670 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
671 // Invalid if rs == 0
672 // BLEZALC if rs == 0 && rt != 0
673 // BGEZALC if rs == rt && rt != 0
674 // BGEUC if rs != rt && rs != 0 && rt != 0
676 InsnType Rs = fieldFromInstruction(insn, 21, 5);
677 InsnType Rt = fieldFromInstruction(insn, 16, 5);
678 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
682 return MCDisassembler::Fail;
684 MI.setOpcode(Mips::BLEZALC);
686 MI.setOpcode(Mips::BGEZALC);
689 MI.setOpcode(Mips::BGEUC);
693 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
695 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
698 MI.addOperand(MCOperand::CreateImm(Imm));
700 return MCDisassembler::Success;
703 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
704 /// according to the given endianess.
705 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
706 uint64_t &Size, uint32_t &Insn,
708 // We want to read exactly 2 Bytes of data.
709 if (Bytes.size() < 2) {
711 return MCDisassembler::Fail;
715 Insn = (Bytes[0] << 8) | Bytes[1];
717 Insn = (Bytes[1] << 8) | Bytes[0];
720 return MCDisassembler::Success;
723 /// Read four bytes from the ArrayRef and return 32 bit word sorted
724 /// according to the given endianess
725 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
726 uint64_t &Size, uint32_t &Insn,
727 bool IsBigEndian, bool IsMicroMips) {
728 // We want to read exactly 4 Bytes of data.
729 if (Bytes.size() < 4) {
731 return MCDisassembler::Fail;
734 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
735 // always precede the low 16 bits in the instruction stream (that is, they
736 // are placed at lower addresses in the instruction stream).
738 // microMIPS byte ordering:
739 // Big-endian: 0 | 1 | 2 | 3
740 // Little-endian: 1 | 0 | 3 | 2
743 // Encoded as a big-endian 32-bit word in the stream.
745 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
748 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
751 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
756 return MCDisassembler::Success;
759 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
760 ArrayRef<uint8_t> Bytes,
762 raw_ostream &VStream,
763 raw_ostream &CStream) const {
768 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
770 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
771 // Calling the auto-generated decoder function.
772 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
774 if (Result != MCDisassembler::Fail) {
779 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
780 if (Result == MCDisassembler::Fail)
781 return MCDisassembler::Fail;
783 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
784 // Calling the auto-generated decoder function.
785 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
787 if (Result != MCDisassembler::Fail) {
791 return MCDisassembler::Fail;
794 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
795 if (Result == MCDisassembler::Fail)
796 return MCDisassembler::Fail;
799 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
801 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
802 if (Result != MCDisassembler::Fail) {
808 if (hasMips32r6() && isGP64()) {
809 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
810 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
812 if (Result != MCDisassembler::Fail) {
819 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
820 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
822 if (Result != MCDisassembler::Fail) {
828 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
829 // Calling the auto-generated decoder function.
831 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
832 if (Result != MCDisassembler::Fail) {
837 return MCDisassembler::Fail;
840 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
841 ArrayRef<uint8_t> Bytes,
843 raw_ostream &VStream,
844 raw_ostream &CStream) const {
847 DecodeStatus Result =
848 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
849 if (Result == MCDisassembler::Fail)
850 return MCDisassembler::Fail;
852 // Calling the auto-generated decoder function.
854 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
855 if (Result != MCDisassembler::Fail) {
859 // If we fail to decode in Mips64 decoder space we can try in Mips32
861 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
862 if (Result != MCDisassembler::Fail) {
867 return MCDisassembler::Fail;
870 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
873 const void *Decoder) {
875 return MCDisassembler::Fail;
879 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
882 const void *Decoder) {
885 return MCDisassembler::Fail;
887 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
888 Inst.addOperand(MCOperand::CreateReg(Reg));
889 return MCDisassembler::Success;
892 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
895 const void *Decoder) {
897 return MCDisassembler::Fail;
898 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
899 Inst.addOperand(MCOperand::CreateReg(Reg));
900 return MCDisassembler::Success;
903 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
906 const void *Decoder) {
908 return MCDisassembler::Fail;
909 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
910 Inst.addOperand(MCOperand::CreateReg(Reg));
911 return MCDisassembler::Success;
914 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
917 const void *Decoder) {
918 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
919 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
921 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
924 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
927 const void *Decoder) {
928 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
931 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
934 const void *Decoder) {
936 return MCDisassembler::Fail;
938 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
939 Inst.addOperand(MCOperand::CreateReg(Reg));
940 return MCDisassembler::Success;
943 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
946 const void *Decoder) {
948 return MCDisassembler::Fail;
950 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
951 Inst.addOperand(MCOperand::CreateReg(Reg));
952 return MCDisassembler::Success;
955 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
958 const void *Decoder) {
960 return MCDisassembler::Fail;
961 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
962 Inst.addOperand(MCOperand::CreateReg(Reg));
963 return MCDisassembler::Success;
966 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
969 const void *Decoder) {
971 return MCDisassembler::Fail;
972 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
973 Inst.addOperand(MCOperand::CreateReg(Reg));
974 return MCDisassembler::Success;
977 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
979 const void *Decoder) {
981 return MCDisassembler::Fail;
983 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
984 Inst.addOperand(MCOperand::CreateReg(Reg));
985 return MCDisassembler::Success;
988 static DecodeStatus DecodeMem(MCInst &Inst,
991 const void *Decoder) {
992 int Offset = SignExtend32<16>(Insn & 0xffff);
993 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
994 unsigned Base = fieldFromInstruction(Insn, 21, 5);
996 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
997 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
999 if(Inst.getOpcode() == Mips::SC){
1000 Inst.addOperand(MCOperand::CreateReg(Reg));
1003 Inst.addOperand(MCOperand::CreateReg(Reg));
1004 Inst.addOperand(MCOperand::CreateReg(Base));
1005 Inst.addOperand(MCOperand::CreateImm(Offset));
1007 return MCDisassembler::Success;
1010 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1013 const void *Decoder) {
1014 int Offset = SignExtend32<16>(Insn & 0xffff);
1015 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1016 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1018 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1020 Inst.addOperand(MCOperand::CreateReg(Base));
1021 Inst.addOperand(MCOperand::CreateImm(Offset));
1022 Inst.addOperand(MCOperand::CreateImm(Hint));
1024 return MCDisassembler::Success;
1027 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1028 uint64_t Address, const void *Decoder) {
1029 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1030 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1031 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1033 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1034 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1036 Inst.addOperand(MCOperand::CreateReg(Reg));
1037 Inst.addOperand(MCOperand::CreateReg(Base));
1039 // The immediate field of an LD/ST instruction is scaled which means it must
1040 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1046 switch(Inst.getOpcode())
1049 assert (0 && "Unexpected instruction");
1050 return MCDisassembler::Fail;
1054 Inst.addOperand(MCOperand::CreateImm(Offset));
1058 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1062 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1066 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1070 return MCDisassembler::Success;
1073 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1076 const void *Decoder) {
1077 int Offset = SignExtend32<12>(Insn & 0x0fff);
1078 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1079 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1081 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1082 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1084 switch (Inst.getOpcode()) {
1085 case Mips::SWM32_MM:
1086 case Mips::LWM32_MM:
1087 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1088 == MCDisassembler::Fail)
1089 return MCDisassembler::Fail;
1090 Inst.addOperand(MCOperand::CreateReg(Base));
1091 Inst.addOperand(MCOperand::CreateImm(Offset));
1094 Inst.addOperand(MCOperand::CreateReg(Reg));
1097 Inst.addOperand(MCOperand::CreateReg(Reg));
1098 Inst.addOperand(MCOperand::CreateReg(Base));
1099 Inst.addOperand(MCOperand::CreateImm(Offset));
1102 return MCDisassembler::Success;
1105 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1108 const void *Decoder) {
1109 int Offset = SignExtend32<16>(Insn & 0xffff);
1110 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1111 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1113 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1114 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1116 Inst.addOperand(MCOperand::CreateReg(Reg));
1117 Inst.addOperand(MCOperand::CreateReg(Base));
1118 Inst.addOperand(MCOperand::CreateImm(Offset));
1120 return MCDisassembler::Success;
1123 static DecodeStatus DecodeFMem(MCInst &Inst,
1126 const void *Decoder) {
1127 int Offset = SignExtend32<16>(Insn & 0xffff);
1128 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1129 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1131 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1132 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1134 Inst.addOperand(MCOperand::CreateReg(Reg));
1135 Inst.addOperand(MCOperand::CreateReg(Base));
1136 Inst.addOperand(MCOperand::CreateImm(Offset));
1138 return MCDisassembler::Success;
1141 static DecodeStatus DecodeFMem2(MCInst &Inst,
1144 const void *Decoder) {
1145 int Offset = SignExtend32<16>(Insn & 0xffff);
1146 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1147 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1149 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1150 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1152 Inst.addOperand(MCOperand::CreateReg(Reg));
1153 Inst.addOperand(MCOperand::CreateReg(Base));
1154 Inst.addOperand(MCOperand::CreateImm(Offset));
1156 return MCDisassembler::Success;
1159 static DecodeStatus DecodeFMem3(MCInst &Inst,
1162 const void *Decoder) {
1163 int Offset = SignExtend32<16>(Insn & 0xffff);
1164 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1165 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1167 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1168 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1170 Inst.addOperand(MCOperand::CreateReg(Reg));
1171 Inst.addOperand(MCOperand::CreateReg(Base));
1172 Inst.addOperand(MCOperand::CreateImm(Offset));
1174 return MCDisassembler::Success;
1177 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1180 const void *Decoder) {
1181 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1182 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1183 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1185 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1186 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1188 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1189 Inst.addOperand(MCOperand::CreateReg(Rt));
1192 Inst.addOperand(MCOperand::CreateReg(Rt));
1193 Inst.addOperand(MCOperand::CreateReg(Base));
1194 Inst.addOperand(MCOperand::CreateImm(Offset));
1196 return MCDisassembler::Success;
1199 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1202 const void *Decoder) {
1203 // Currently only hardware register 29 is supported.
1205 return MCDisassembler::Fail;
1206 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1207 return MCDisassembler::Success;
1210 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1213 const void *Decoder) {
1214 if (RegNo > 30 || RegNo %2)
1215 return MCDisassembler::Fail;
1218 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1219 Inst.addOperand(MCOperand::CreateReg(Reg));
1220 return MCDisassembler::Success;
1223 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1226 const void *Decoder) {
1228 return MCDisassembler::Fail;
1230 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1231 Inst.addOperand(MCOperand::CreateReg(Reg));
1232 return MCDisassembler::Success;
1235 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1238 const void *Decoder) {
1240 return MCDisassembler::Fail;
1242 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1243 Inst.addOperand(MCOperand::CreateReg(Reg));
1244 return MCDisassembler::Success;
1247 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1250 const void *Decoder) {
1252 return MCDisassembler::Fail;
1254 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1255 Inst.addOperand(MCOperand::CreateReg(Reg));
1256 return MCDisassembler::Success;
1259 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1262 const void *Decoder) {
1264 return MCDisassembler::Fail;
1266 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1267 Inst.addOperand(MCOperand::CreateReg(Reg));
1268 return MCDisassembler::Success;
1271 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1274 const void *Decoder) {
1276 return MCDisassembler::Fail;
1278 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1279 Inst.addOperand(MCOperand::CreateReg(Reg));
1280 return MCDisassembler::Success;
1283 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1286 const void *Decoder) {
1288 return MCDisassembler::Fail;
1290 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1291 Inst.addOperand(MCOperand::CreateReg(Reg));
1292 return MCDisassembler::Success;
1295 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1298 const void *Decoder) {
1300 return MCDisassembler::Fail;
1302 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1303 Inst.addOperand(MCOperand::CreateReg(Reg));
1304 return MCDisassembler::Success;
1307 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1310 const void *Decoder) {
1312 return MCDisassembler::Fail;
1314 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1315 Inst.addOperand(MCOperand::CreateReg(Reg));
1316 return MCDisassembler::Success;
1319 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1322 const void *Decoder) {
1324 return MCDisassembler::Fail;
1326 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1327 Inst.addOperand(MCOperand::CreateReg(Reg));
1328 return MCDisassembler::Success;
1331 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1334 const void *Decoder) {
1335 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1336 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1337 return MCDisassembler::Success;
1340 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1343 const void *Decoder) {
1345 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1346 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1347 return MCDisassembler::Success;
1350 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1353 const void *Decoder) {
1354 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1356 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1357 return MCDisassembler::Success;
1360 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1363 const void *Decoder) {
1364 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1366 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1367 return MCDisassembler::Success;
1370 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1373 const void *Decoder) {
1374 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1375 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1376 return MCDisassembler::Success;
1379 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1382 const void *Decoder) {
1383 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1384 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1385 return MCDisassembler::Success;
1388 static DecodeStatus DecodeSimm16(MCInst &Inst,
1391 const void *Decoder) {
1392 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1393 return MCDisassembler::Success;
1396 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1399 const void *Decoder) {
1400 // We add one to the immediate field as it was encoded as 'imm - 1'.
1401 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1402 return MCDisassembler::Success;
1405 static DecodeStatus DecodeInsSize(MCInst &Inst,
1408 const void *Decoder) {
1409 // First we need to grab the pos(lsb) from MCInst.
1410 int Pos = Inst.getOperand(2).getImm();
1411 int Size = (int) Insn - Pos + 1;
1412 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1413 return MCDisassembler::Success;
1416 static DecodeStatus DecodeExtSize(MCInst &Inst,
1419 const void *Decoder) {
1420 int Size = (int) Insn + 1;
1421 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1422 return MCDisassembler::Success;
1425 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1426 uint64_t Address, const void *Decoder) {
1427 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1428 return MCDisassembler::Success;
1431 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1432 uint64_t Address, const void *Decoder) {
1433 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1434 return MCDisassembler::Success;
1437 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1440 const void *Decoder) {
1441 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1442 Mips::S6, Mips::FP};
1445 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1446 // Empty register lists are not allowed.
1448 return MCDisassembler::Fail;
1450 RegNum = RegLst & 0xf;
1451 for (unsigned i = 0; i < RegNum; i++)
1452 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1455 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1457 return MCDisassembler::Success;