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 DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
115 const void *Decoder);
117 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
120 const void *Decoder);
122 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
125 const void *Decoder);
127 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
130 const void *Decoder);
132 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
135 const void *Decoder);
137 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
140 const void *Decoder);
142 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
145 const void *Decoder);
147 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
150 const void *Decoder);
152 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
154 const void *Decoder);
156 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
159 const void *Decoder);
161 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
164 const void *Decoder);
166 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
169 const void *Decoder);
171 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
174 const void *Decoder);
176 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
179 const void *Decoder);
181 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
184 const void *Decoder);
186 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
189 const void *Decoder);
191 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
194 const void *Decoder);
196 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
199 const void *Decoder);
201 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
204 const void *Decoder);
206 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
209 const void *Decoder);
211 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
214 const void *Decoder);
216 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
219 const void *Decoder);
221 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
224 const void *Decoder);
226 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
229 const void *Decoder);
231 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
232 // shifted left by 1 bit.
233 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
236 const void *Decoder);
238 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
239 // shifted left by 1 bit.
240 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
243 const void *Decoder);
245 static DecodeStatus DecodeMem(MCInst &Inst,
248 const void *Decoder);
250 static DecodeStatus DecodeCacheOp(MCInst &Inst,
253 const void *Decoder);
255 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
256 uint64_t Address, const void *Decoder);
258 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
261 const void *Decoder);
263 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
266 const void *Decoder);
268 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
270 const void *Decoder);
272 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
274 const void *Decoder);
276 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
278 const void *Decoder);
280 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
283 const void *Decoder);
285 static DecodeStatus DecodeSimm16(MCInst &Inst,
288 const void *Decoder);
290 // Decode the immediate field of an LSA instruction which
292 static DecodeStatus DecodeLSAImm(MCInst &Inst,
295 const void *Decoder);
297 static DecodeStatus DecodeInsSize(MCInst &Inst,
300 const void *Decoder);
302 static DecodeStatus DecodeExtSize(MCInst &Inst,
305 const void *Decoder);
307 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
308 uint64_t Address, const void *Decoder);
310 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
311 uint64_t Address, const void *Decoder);
313 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
315 template <typename InsnType>
316 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
317 const void *Decoder);
319 template <typename InsnType>
321 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
322 const void *Decoder);
324 template <typename InsnType>
326 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
327 const void *Decoder);
329 template <typename InsnType>
331 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
332 const void *Decoder);
334 template <typename InsnType>
336 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
337 const void *Decoder);
339 template <typename InsnType>
341 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
342 const void *Decoder);
344 template <typename InsnType>
346 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
347 const void *Decoder);
349 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
351 const void *Decoder);
354 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
358 static MCDisassembler *createMipsDisassembler(
360 const MCSubtargetInfo &STI,
362 return new MipsDisassembler(STI, Ctx, true);
365 static MCDisassembler *createMipselDisassembler(
367 const MCSubtargetInfo &STI,
369 return new MipsDisassembler(STI, Ctx, false);
372 static MCDisassembler *createMips64Disassembler(
374 const MCSubtargetInfo &STI,
376 return new Mips64Disassembler(STI, Ctx, true);
379 static MCDisassembler *createMips64elDisassembler(
381 const MCSubtargetInfo &STI,
383 return new Mips64Disassembler(STI, Ctx, false);
386 extern "C" void LLVMInitializeMipsDisassembler() {
387 // Register the disassembler.
388 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
389 createMipsDisassembler);
390 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
391 createMipselDisassembler);
392 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
393 createMips64Disassembler);
394 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
395 createMips64elDisassembler);
398 #include "MipsGenDisassemblerTables.inc"
400 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
401 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
402 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
403 return *(RegInfo->getRegClass(RC).begin() + RegNo);
406 template <typename InsnType>
407 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
408 const void *Decoder) {
409 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
410 // The size of the n field depends on the element size
411 // The register class also depends on this.
412 InsnType tmp = fieldFromInstruction(insn, 17, 5);
414 DecodeFN RegDecoder = nullptr;
415 if ((tmp & 0x18) == 0x00) { // INSVE_B
417 RegDecoder = DecodeMSA128BRegisterClass;
418 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
420 RegDecoder = DecodeMSA128HRegisterClass;
421 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
423 RegDecoder = DecodeMSA128WRegisterClass;
424 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
426 RegDecoder = DecodeMSA128DRegisterClass;
428 llvm_unreachable("Invalid encoding");
430 assert(NSize != 0 && RegDecoder != nullptr);
433 tmp = fieldFromInstruction(insn, 6, 5);
434 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
435 return MCDisassembler::Fail;
437 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
438 return MCDisassembler::Fail;
440 tmp = fieldFromInstruction(insn, 16, NSize);
441 MI.addOperand(MCOperand::CreateImm(tmp));
443 tmp = fieldFromInstruction(insn, 11, 5);
444 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
445 return MCDisassembler::Fail;
447 MI.addOperand(MCOperand::CreateImm(0));
449 return MCDisassembler::Success;
452 template <typename InsnType>
453 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
455 const void *Decoder) {
456 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
457 // (otherwise we would have matched the ADDI instruction from the earlier
461 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
463 // BEQZALC if rs == 0 && rt != 0
464 // BEQC if rs < rt && rs != 0
466 InsnType Rs = fieldFromInstruction(insn, 21, 5);
467 InsnType Rt = fieldFromInstruction(insn, 16, 5);
468 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
472 MI.setOpcode(Mips::BOVC);
474 } else if (Rs != 0 && Rs < Rt) {
475 MI.setOpcode(Mips::BEQC);
478 MI.setOpcode(Mips::BEQZALC);
481 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
484 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
486 MI.addOperand(MCOperand::CreateImm(Imm));
488 return MCDisassembler::Success;
491 template <typename InsnType>
492 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
494 const void *Decoder) {
495 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
496 // (otherwise we would have matched the ADDI instruction from the earlier
500 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
502 // BNEZALC if rs == 0 && rt != 0
503 // BNEC if rs < rt && rs != 0
505 InsnType Rs = fieldFromInstruction(insn, 21, 5);
506 InsnType Rt = fieldFromInstruction(insn, 16, 5);
507 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
511 MI.setOpcode(Mips::BNVC);
513 } else if (Rs != 0 && Rs < Rt) {
514 MI.setOpcode(Mips::BNEC);
517 MI.setOpcode(Mips::BNEZALC);
520 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
523 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
525 MI.addOperand(MCOperand::CreateImm(Imm));
527 return MCDisassembler::Success;
530 template <typename InsnType>
531 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
533 const void *Decoder) {
534 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
535 // (otherwise we would have matched the BLEZL instruction from the earlier
539 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
540 // Invalid if rs == 0
541 // BLEZC if rs == 0 && rt != 0
542 // BGEZC if rs == rt && rt != 0
543 // BGEC if rs != rt && rs != 0 && rt != 0
545 InsnType Rs = fieldFromInstruction(insn, 21, 5);
546 InsnType Rt = fieldFromInstruction(insn, 16, 5);
547 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
551 return MCDisassembler::Fail;
553 MI.setOpcode(Mips::BLEZC);
555 MI.setOpcode(Mips::BGEZC);
558 MI.setOpcode(Mips::BGEC);
562 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
565 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
568 MI.addOperand(MCOperand::CreateImm(Imm));
570 return MCDisassembler::Success;
573 template <typename InsnType>
574 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
576 const void *Decoder) {
577 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
578 // (otherwise we would have matched the BGTZL instruction from the earlier
582 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
583 // Invalid if rs == 0
584 // BGTZC if rs == 0 && rt != 0
585 // BLTZC if rs == rt && rt != 0
586 // BLTC if rs != rt && rs != 0 && rt != 0
590 InsnType Rs = fieldFromInstruction(insn, 21, 5);
591 InsnType Rt = fieldFromInstruction(insn, 16, 5);
592 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
595 return MCDisassembler::Fail;
597 MI.setOpcode(Mips::BGTZC);
599 MI.setOpcode(Mips::BLTZC);
601 MI.setOpcode(Mips::BLTC);
606 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
609 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
612 MI.addOperand(MCOperand::CreateImm(Imm));
614 return MCDisassembler::Success;
617 template <typename InsnType>
618 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
620 const void *Decoder) {
621 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
622 // (otherwise we would have matched the BGTZ instruction from the earlier
626 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
628 // BGTZALC if rs == 0 && rt != 0
629 // BLTZALC if rs != 0 && rs == rt
630 // BLTUC if rs != 0 && rs != rt
632 InsnType Rs = fieldFromInstruction(insn, 21, 5);
633 InsnType Rt = fieldFromInstruction(insn, 16, 5);
634 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
639 MI.setOpcode(Mips::BGTZ);
641 } else if (Rs == 0) {
642 MI.setOpcode(Mips::BGTZALC);
644 } else if (Rs == Rt) {
645 MI.setOpcode(Mips::BLTZALC);
648 MI.setOpcode(Mips::BLTUC);
654 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
658 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
661 MI.addOperand(MCOperand::CreateImm(Imm));
663 return MCDisassembler::Success;
666 template <typename InsnType>
667 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
669 const void *Decoder) {
670 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
671 // (otherwise we would have matched the BLEZL instruction from the earlier
675 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
676 // Invalid if rs == 0
677 // BLEZALC if rs == 0 && rt != 0
678 // BGEZALC if rs == rt && rt != 0
679 // BGEUC if rs != rt && rs != 0 && rt != 0
681 InsnType Rs = fieldFromInstruction(insn, 21, 5);
682 InsnType Rt = fieldFromInstruction(insn, 16, 5);
683 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
687 return MCDisassembler::Fail;
689 MI.setOpcode(Mips::BLEZALC);
691 MI.setOpcode(Mips::BGEZALC);
694 MI.setOpcode(Mips::BGEUC);
698 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
700 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
703 MI.addOperand(MCOperand::CreateImm(Imm));
705 return MCDisassembler::Success;
708 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
709 /// according to the given endianess.
710 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
711 uint64_t &Size, uint32_t &Insn,
713 // We want to read exactly 2 Bytes of data.
714 if (Bytes.size() < 2) {
716 return MCDisassembler::Fail;
720 Insn = (Bytes[0] << 8) | Bytes[1];
722 Insn = (Bytes[1] << 8) | Bytes[0];
725 return MCDisassembler::Success;
728 /// Read four bytes from the ArrayRef and return 32 bit word sorted
729 /// according to the given endianess
730 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
731 uint64_t &Size, uint32_t &Insn,
732 bool IsBigEndian, bool IsMicroMips) {
733 // We want to read exactly 4 Bytes of data.
734 if (Bytes.size() < 4) {
736 return MCDisassembler::Fail;
739 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
740 // always precede the low 16 bits in the instruction stream (that is, they
741 // are placed at lower addresses in the instruction stream).
743 // microMIPS byte ordering:
744 // Big-endian: 0 | 1 | 2 | 3
745 // Little-endian: 1 | 0 | 3 | 2
748 // Encoded as a big-endian 32-bit word in the stream.
750 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
753 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
756 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
761 return MCDisassembler::Success;
764 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
765 ArrayRef<uint8_t> Bytes,
767 raw_ostream &VStream,
768 raw_ostream &CStream) const {
773 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
775 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
776 // Calling the auto-generated decoder function.
777 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
779 if (Result != MCDisassembler::Fail) {
784 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
785 if (Result == MCDisassembler::Fail)
786 return MCDisassembler::Fail;
788 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
789 // Calling the auto-generated decoder function.
790 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
792 if (Result != MCDisassembler::Fail) {
796 return MCDisassembler::Fail;
799 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
800 if (Result == MCDisassembler::Fail)
801 return MCDisassembler::Fail;
804 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
806 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
807 if (Result != MCDisassembler::Fail) {
813 if (hasMips32r6() && isGP64()) {
814 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
815 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
817 if (Result != MCDisassembler::Fail) {
824 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
825 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
827 if (Result != MCDisassembler::Fail) {
833 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
834 // Calling the auto-generated decoder function.
836 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
837 if (Result != MCDisassembler::Fail) {
842 return MCDisassembler::Fail;
845 DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
846 ArrayRef<uint8_t> Bytes,
848 raw_ostream &VStream,
849 raw_ostream &CStream) const {
852 DecodeStatus Result =
853 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
854 if (Result == MCDisassembler::Fail)
855 return MCDisassembler::Fail;
857 // Calling the auto-generated decoder function.
859 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
860 if (Result != MCDisassembler::Fail) {
864 // If we fail to decode in Mips64 decoder space we can try in Mips32
866 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
867 if (Result != MCDisassembler::Fail) {
872 return MCDisassembler::Fail;
875 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
878 const void *Decoder) {
880 return MCDisassembler::Fail;
884 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
887 const void *Decoder) {
890 return MCDisassembler::Fail;
892 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
893 Inst.addOperand(MCOperand::CreateReg(Reg));
894 return MCDisassembler::Success;
897 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
900 const void *Decoder) {
902 return MCDisassembler::Fail;
903 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
904 Inst.addOperand(MCOperand::CreateReg(Reg));
905 return MCDisassembler::Success;
908 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
911 const void *Decoder) {
912 return MCDisassembler::Fail;
915 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
918 const void *Decoder) {
920 return MCDisassembler::Fail;
921 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
922 Inst.addOperand(MCOperand::CreateReg(Reg));
923 return MCDisassembler::Success;
926 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
929 const void *Decoder) {
930 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
931 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
933 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
936 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
939 const void *Decoder) {
940 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
943 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
946 const void *Decoder) {
948 return MCDisassembler::Fail;
950 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
951 Inst.addOperand(MCOperand::CreateReg(Reg));
952 return MCDisassembler::Success;
955 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
958 const void *Decoder) {
960 return MCDisassembler::Fail;
962 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
963 Inst.addOperand(MCOperand::CreateReg(Reg));
964 return MCDisassembler::Success;
967 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
970 const void *Decoder) {
972 return MCDisassembler::Fail;
973 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
974 Inst.addOperand(MCOperand::CreateReg(Reg));
975 return MCDisassembler::Success;
978 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
981 const void *Decoder) {
983 return MCDisassembler::Fail;
984 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
985 Inst.addOperand(MCOperand::CreateReg(Reg));
986 return MCDisassembler::Success;
989 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
991 const void *Decoder) {
993 return MCDisassembler::Fail;
995 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
996 Inst.addOperand(MCOperand::CreateReg(Reg));
997 return MCDisassembler::Success;
1000 static DecodeStatus DecodeMem(MCInst &Inst,
1003 const void *Decoder) {
1004 int Offset = SignExtend32<16>(Insn & 0xffff);
1005 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1006 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1008 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1009 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1011 if(Inst.getOpcode() == Mips::SC){
1012 Inst.addOperand(MCOperand::CreateReg(Reg));
1015 Inst.addOperand(MCOperand::CreateReg(Reg));
1016 Inst.addOperand(MCOperand::CreateReg(Base));
1017 Inst.addOperand(MCOperand::CreateImm(Offset));
1019 return MCDisassembler::Success;
1022 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1025 const void *Decoder) {
1026 int Offset = SignExtend32<16>(Insn & 0xffff);
1027 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1028 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1030 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1032 Inst.addOperand(MCOperand::CreateReg(Base));
1033 Inst.addOperand(MCOperand::CreateImm(Offset));
1034 Inst.addOperand(MCOperand::CreateImm(Hint));
1036 return MCDisassembler::Success;
1039 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1040 uint64_t Address, const void *Decoder) {
1041 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1042 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1043 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1045 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1046 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1048 Inst.addOperand(MCOperand::CreateReg(Reg));
1049 Inst.addOperand(MCOperand::CreateReg(Base));
1051 // The immediate field of an LD/ST instruction is scaled which means it must
1052 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1058 switch(Inst.getOpcode())
1061 assert (0 && "Unexpected instruction");
1062 return MCDisassembler::Fail;
1066 Inst.addOperand(MCOperand::CreateImm(Offset));
1070 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
1074 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
1078 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
1082 return MCDisassembler::Success;
1085 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1088 const void *Decoder) {
1089 int Offset = SignExtend32<12>(Insn & 0x0fff);
1090 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1091 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1093 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1094 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1096 switch (Inst.getOpcode()) {
1097 case Mips::SWM32_MM:
1098 case Mips::LWM32_MM:
1099 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1100 == MCDisassembler::Fail)
1101 return MCDisassembler::Fail;
1102 Inst.addOperand(MCOperand::CreateReg(Base));
1103 Inst.addOperand(MCOperand::CreateImm(Offset));
1106 Inst.addOperand(MCOperand::CreateReg(Reg));
1109 Inst.addOperand(MCOperand::CreateReg(Reg));
1110 Inst.addOperand(MCOperand::CreateReg(Base));
1111 Inst.addOperand(MCOperand::CreateImm(Offset));
1114 return MCDisassembler::Success;
1117 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1120 const void *Decoder) {
1121 int Offset = SignExtend32<16>(Insn & 0xffff);
1122 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1123 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1125 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1126 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1128 Inst.addOperand(MCOperand::CreateReg(Reg));
1129 Inst.addOperand(MCOperand::CreateReg(Base));
1130 Inst.addOperand(MCOperand::CreateImm(Offset));
1132 return MCDisassembler::Success;
1135 static DecodeStatus DecodeFMem(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::FGR64RegClassID, Reg);
1144 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1146 Inst.addOperand(MCOperand::CreateReg(Reg));
1147 Inst.addOperand(MCOperand::CreateReg(Base));
1148 Inst.addOperand(MCOperand::CreateImm(Offset));
1150 return MCDisassembler::Success;
1153 static DecodeStatus DecodeFMem2(MCInst &Inst,
1156 const void *Decoder) {
1157 int Offset = SignExtend32<16>(Insn & 0xffff);
1158 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1159 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1161 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1162 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1164 Inst.addOperand(MCOperand::CreateReg(Reg));
1165 Inst.addOperand(MCOperand::CreateReg(Base));
1166 Inst.addOperand(MCOperand::CreateImm(Offset));
1168 return MCDisassembler::Success;
1171 static DecodeStatus DecodeFMem3(MCInst &Inst,
1174 const void *Decoder) {
1175 int Offset = SignExtend32<16>(Insn & 0xffff);
1176 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1177 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1179 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1180 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1182 Inst.addOperand(MCOperand::CreateReg(Reg));
1183 Inst.addOperand(MCOperand::CreateReg(Base));
1184 Inst.addOperand(MCOperand::CreateImm(Offset));
1186 return MCDisassembler::Success;
1189 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1192 const void *Decoder) {
1193 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1194 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1195 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1197 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1198 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1200 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1201 Inst.addOperand(MCOperand::CreateReg(Rt));
1204 Inst.addOperand(MCOperand::CreateReg(Rt));
1205 Inst.addOperand(MCOperand::CreateReg(Base));
1206 Inst.addOperand(MCOperand::CreateImm(Offset));
1208 return MCDisassembler::Success;
1211 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1214 const void *Decoder) {
1215 // Currently only hardware register 29 is supported.
1217 return MCDisassembler::Fail;
1218 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1219 return MCDisassembler::Success;
1222 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1225 const void *Decoder) {
1226 if (RegNo > 30 || RegNo %2)
1227 return MCDisassembler::Fail;
1230 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1231 Inst.addOperand(MCOperand::CreateReg(Reg));
1232 return MCDisassembler::Success;
1235 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1238 const void *Decoder) {
1240 return MCDisassembler::Fail;
1242 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1243 Inst.addOperand(MCOperand::CreateReg(Reg));
1244 return MCDisassembler::Success;
1247 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1250 const void *Decoder) {
1252 return MCDisassembler::Fail;
1254 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1255 Inst.addOperand(MCOperand::CreateReg(Reg));
1256 return MCDisassembler::Success;
1259 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1262 const void *Decoder) {
1264 return MCDisassembler::Fail;
1266 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1267 Inst.addOperand(MCOperand::CreateReg(Reg));
1268 return MCDisassembler::Success;
1271 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1274 const void *Decoder) {
1276 return MCDisassembler::Fail;
1278 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1279 Inst.addOperand(MCOperand::CreateReg(Reg));
1280 return MCDisassembler::Success;
1283 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1286 const void *Decoder) {
1288 return MCDisassembler::Fail;
1290 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1291 Inst.addOperand(MCOperand::CreateReg(Reg));
1292 return MCDisassembler::Success;
1295 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1298 const void *Decoder) {
1300 return MCDisassembler::Fail;
1302 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1303 Inst.addOperand(MCOperand::CreateReg(Reg));
1304 return MCDisassembler::Success;
1307 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1310 const void *Decoder) {
1312 return MCDisassembler::Fail;
1314 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1315 Inst.addOperand(MCOperand::CreateReg(Reg));
1316 return MCDisassembler::Success;
1319 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1322 const void *Decoder) {
1324 return MCDisassembler::Fail;
1326 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1327 Inst.addOperand(MCOperand::CreateReg(Reg));
1328 return MCDisassembler::Success;
1331 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1334 const void *Decoder) {
1336 return MCDisassembler::Fail;
1338 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1339 Inst.addOperand(MCOperand::CreateReg(Reg));
1340 return MCDisassembler::Success;
1343 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1346 const void *Decoder) {
1347 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1348 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1349 return MCDisassembler::Success;
1352 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1355 const void *Decoder) {
1357 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1358 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1359 return MCDisassembler::Success;
1362 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1365 const void *Decoder) {
1366 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1368 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1369 return MCDisassembler::Success;
1372 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1375 const void *Decoder) {
1376 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1378 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1379 return MCDisassembler::Success;
1382 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1385 const void *Decoder) {
1386 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1387 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1388 return MCDisassembler::Success;
1391 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1394 const void *Decoder) {
1395 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1396 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1397 return MCDisassembler::Success;
1400 static DecodeStatus DecodeSimm16(MCInst &Inst,
1403 const void *Decoder) {
1404 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1405 return MCDisassembler::Success;
1408 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1411 const void *Decoder) {
1412 // We add one to the immediate field as it was encoded as 'imm - 1'.
1413 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1414 return MCDisassembler::Success;
1417 static DecodeStatus DecodeInsSize(MCInst &Inst,
1420 const void *Decoder) {
1421 // First we need to grab the pos(lsb) from MCInst.
1422 int Pos = Inst.getOperand(2).getImm();
1423 int Size = (int) Insn - Pos + 1;
1424 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1425 return MCDisassembler::Success;
1428 static DecodeStatus DecodeExtSize(MCInst &Inst,
1431 const void *Decoder) {
1432 int Size = (int) Insn + 1;
1433 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1434 return MCDisassembler::Success;
1437 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1438 uint64_t Address, const void *Decoder) {
1439 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
1440 return MCDisassembler::Success;
1443 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1444 uint64_t Address, const void *Decoder) {
1445 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
1446 return MCDisassembler::Success;
1449 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1452 const void *Decoder) {
1453 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1454 Mips::S6, Mips::FP};
1457 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1458 // Empty register lists are not allowed.
1460 return MCDisassembler::Fail;
1462 RegNum = RegLst & 0xf;
1463 for (unsigned i = 0; i < RegNum; i++)
1464 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1467 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1469 return MCDisassembler::Success;