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/MemoryObject.h"
24 #include "llvm/Support/TargetRegistry.h"
28 #define DEBUG_TYPE "mips-disassembler"
30 typedef MCDisassembler::DecodeStatus DecodeStatus;
34 /// MipsDisassemblerBase - a disasembler class for Mips.
35 class MipsDisassemblerBase : public MCDisassembler {
37 /// Constructor - Initializes the disassembler.
39 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
41 MCDisassembler(STI, Ctx),
42 IsN64(STI.getFeatureBits() & Mips::FeatureN64), isBigEndian(bigEndian) {}
44 virtual ~MipsDisassemblerBase() {}
46 bool isN64() const { return IsN64; }
54 /// MipsDisassembler - a disasembler class for Mips32.
55 class MipsDisassembler : public MipsDisassemblerBase {
58 /// Constructor - Initializes the disassembler.
60 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
62 MipsDisassemblerBase(STI, Ctx, bigEndian) {
63 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
66 bool isMips32r6() const {
67 return STI.getFeatureBits() & Mips::FeatureMips32r6;
70 /// getInstruction - See MCDisassembler.
71 DecodeStatus getInstruction(MCInst &instr,
73 const MemoryObject ®ion,
76 raw_ostream &cStream) const override;
80 /// Mips64Disassembler - a disasembler class for Mips64.
81 class Mips64Disassembler : public MipsDisassemblerBase {
83 /// Constructor - Initializes the disassembler.
85 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
87 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
89 /// getInstruction - See MCDisassembler.
90 DecodeStatus getInstruction(MCInst &instr,
92 const MemoryObject ®ion,
95 raw_ostream &cStream) const override;
98 } // end anonymous namespace
100 // Forward declare these because the autogenerated code will reference them.
101 // Definitions are further down.
102 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
105 const void *Decoder);
107 static DecodeStatus DecodeCPU16RegsRegisterClass(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 DecodeFGRH32RegisterClass(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 DecodeHWRegsRegisterClass(MCInst &Inst,
155 const void *Decoder);
157 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
160 const void *Decoder);
162 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
165 const void *Decoder);
167 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
170 const void *Decoder);
172 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
175 const void *Decoder);
177 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
180 const void *Decoder);
182 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
185 const void *Decoder);
187 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
190 const void *Decoder);
192 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
195 const void *Decoder);
197 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
200 const void *Decoder);
202 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
205 const void *Decoder);
207 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
210 const void *Decoder);
212 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
215 const void *Decoder);
217 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
220 const void *Decoder);
222 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
225 const void *Decoder);
227 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
228 // shifted left by 1 bit.
229 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
232 const void *Decoder);
234 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
235 // shifted left by 1 bit.
236 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
239 const void *Decoder);
241 static DecodeStatus DecodeMem(MCInst &Inst,
244 const void *Decoder);
246 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
247 uint64_t Address, const void *Decoder);
249 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
252 const void *Decoder);
254 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
257 const void *Decoder);
259 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
261 const void *Decoder);
263 static DecodeStatus DecodeSimm16(MCInst &Inst,
266 const void *Decoder);
268 // Decode the immediate field of an LSA instruction which
270 static DecodeStatus DecodeLSAImm(MCInst &Inst,
273 const void *Decoder);
275 static DecodeStatus DecodeInsSize(MCInst &Inst,
278 const void *Decoder);
280 static DecodeStatus DecodeExtSize(MCInst &Inst,
283 const void *Decoder);
285 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
286 uint64_t Address, const void *Decoder);
288 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
289 uint64_t Address, const void *Decoder);
291 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
293 template <typename InsnType>
294 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
295 const void *Decoder);
297 template <typename InsnType>
299 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
300 const void *Decoder);
302 template <typename InsnType>
304 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
305 const void *Decoder);
307 template <typename InsnType>
309 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
310 const void *Decoder);
312 template <typename InsnType>
314 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
315 const void *Decoder);
317 template <typename InsnType>
319 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
320 const void *Decoder);
322 template <typename InsnType>
324 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
325 const void *Decoder);
328 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
332 static MCDisassembler *createMipsDisassembler(
334 const MCSubtargetInfo &STI,
336 return new MipsDisassembler(STI, Ctx, true);
339 static MCDisassembler *createMipselDisassembler(
341 const MCSubtargetInfo &STI,
343 return new MipsDisassembler(STI, Ctx, false);
346 static MCDisassembler *createMips64Disassembler(
348 const MCSubtargetInfo &STI,
350 return new Mips64Disassembler(STI, Ctx, true);
353 static MCDisassembler *createMips64elDisassembler(
355 const MCSubtargetInfo &STI,
357 return new Mips64Disassembler(STI, Ctx, false);
360 extern "C" void LLVMInitializeMipsDisassembler() {
361 // Register the disassembler.
362 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
363 createMipsDisassembler);
364 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
365 createMipselDisassembler);
366 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
367 createMips64Disassembler);
368 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
369 createMips64elDisassembler);
372 #include "MipsGenDisassemblerTables.inc"
374 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
375 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
376 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
377 return *(RegInfo->getRegClass(RC).begin() + RegNo);
380 template <typename InsnType>
381 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
382 const void *Decoder) {
383 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
384 // The size of the n field depends on the element size
385 // The register class also depends on this.
386 InsnType tmp = fieldFromInstruction(insn, 17, 5);
388 DecodeFN RegDecoder = nullptr;
389 if ((tmp & 0x18) == 0x00) { // INSVE_B
391 RegDecoder = DecodeMSA128BRegisterClass;
392 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
394 RegDecoder = DecodeMSA128HRegisterClass;
395 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
397 RegDecoder = DecodeMSA128WRegisterClass;
398 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
400 RegDecoder = DecodeMSA128DRegisterClass;
402 llvm_unreachable("Invalid encoding");
404 assert(NSize != 0 && RegDecoder != nullptr);
407 tmp = fieldFromInstruction(insn, 6, 5);
408 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
409 return MCDisassembler::Fail;
411 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
412 return MCDisassembler::Fail;
414 tmp = fieldFromInstruction(insn, 16, NSize);
415 MI.addOperand(MCOperand::CreateImm(tmp));
417 tmp = fieldFromInstruction(insn, 11, 5);
418 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
419 return MCDisassembler::Fail;
421 MI.addOperand(MCOperand::CreateImm(0));
423 return MCDisassembler::Success;
426 template <typename InsnType>
427 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
429 const void *Decoder) {
430 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
431 // (otherwise we would have matched the ADDI instruction from the earlier
435 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
437 // BEQZALC if rs == 0 && rt != 0
438 // BEQC if rs < rt && rs != 0
440 InsnType Rs = fieldFromInstruction(insn, 21, 5);
441 InsnType Rt = fieldFromInstruction(insn, 16, 5);
442 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
446 MI.setOpcode(Mips::BOVC);
448 } else if (Rs != 0 && Rs < Rt) {
449 MI.setOpcode(Mips::BEQC);
452 MI.setOpcode(Mips::BEQZALC);
455 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
458 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
460 MI.addOperand(MCOperand::CreateImm(Imm));
462 return MCDisassembler::Success;
465 template <typename InsnType>
466 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
468 const void *Decoder) {
469 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
470 // (otherwise we would have matched the ADDI instruction from the earlier
474 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
476 // BNEZALC if rs == 0 && rt != 0
477 // BNEC if rs < rt && rs != 0
479 InsnType Rs = fieldFromInstruction(insn, 21, 5);
480 InsnType Rt = fieldFromInstruction(insn, 16, 5);
481 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
485 MI.setOpcode(Mips::BNVC);
487 } else if (Rs != 0 && Rs < Rt) {
488 MI.setOpcode(Mips::BNEC);
491 MI.setOpcode(Mips::BNEZALC);
494 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
497 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
499 MI.addOperand(MCOperand::CreateImm(Imm));
501 return MCDisassembler::Success;
504 template <typename InsnType>
505 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
507 const void *Decoder) {
508 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
509 // (otherwise we would have matched the BLEZL instruction from the earlier
513 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
514 // Invalid if rs == 0
515 // BLEZC if rs == 0 && rt != 0
516 // BGEZC if rs == rt && rt != 0
517 // BGEC if rs != rt && rs != 0 && rt != 0
519 InsnType Rs = fieldFromInstruction(insn, 21, 5);
520 InsnType Rt = fieldFromInstruction(insn, 16, 5);
521 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
525 return MCDisassembler::Fail;
527 MI.setOpcode(Mips::BLEZC);
529 MI.setOpcode(Mips::BGEZC);
532 MI.setOpcode(Mips::BGEC);
536 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
539 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
542 MI.addOperand(MCOperand::CreateImm(Imm));
544 return MCDisassembler::Success;
547 template <typename InsnType>
548 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
550 const void *Decoder) {
551 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
552 // (otherwise we would have matched the BGTZL instruction from the earlier
556 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
557 // Invalid if rs == 0
558 // BGTZC if rs == 0 && rt != 0
559 // BLTZC if rs == rt && rt != 0
560 // BLTC if rs != rt && rs != 0 && rt != 0
562 InsnType Rs = fieldFromInstruction(insn, 21, 5);
563 InsnType Rt = fieldFromInstruction(insn, 16, 5);
564 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
567 return MCDisassembler::Fail;
569 MI.setOpcode(Mips::BGTZC);
571 MI.setOpcode(Mips::BLTZC);
573 return MCDisassembler::Fail; // FIXME: BLTC is not implemented yet.
575 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
578 MI.addOperand(MCOperand::CreateImm(Imm));
580 return MCDisassembler::Success;
583 template <typename InsnType>
584 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
586 const void *Decoder) {
587 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
588 // (otherwise we would have matched the BGTZ instruction from the earlier
592 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
594 // BGTZALC if rs == 0 && rt != 0
595 // BLTZALC if rs != 0 && rs == rt
596 // BLTUC if rs != 0 && rs != rt
598 InsnType Rs = fieldFromInstruction(insn, 21, 5);
599 InsnType Rt = fieldFromInstruction(insn, 16, 5);
600 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
605 MI.setOpcode(Mips::BGTZ);
607 } else if (Rs == 0) {
608 MI.setOpcode(Mips::BGTZALC);
610 } else if (Rs == Rt) {
611 MI.setOpcode(Mips::BLTZALC);
614 return MCDisassembler::Fail; // BLTUC not implemented yet
617 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
621 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
624 MI.addOperand(MCOperand::CreateImm(Imm));
626 return MCDisassembler::Success;
629 template <typename InsnType>
630 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
632 const void *Decoder) {
633 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
634 // (otherwise we would have matched the BLEZL instruction from the earlier
638 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
639 // Invalid if rs == 0
640 // BLEZALC if rs == 0 && rt != 0
641 // BGEZALC if rs == rt && rt != 0
642 // BGEUC if rs != rt && rs != 0 && rt != 0
644 InsnType Rs = fieldFromInstruction(insn, 21, 5);
645 InsnType Rt = fieldFromInstruction(insn, 16, 5);
646 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
650 return MCDisassembler::Fail;
652 MI.setOpcode(Mips::BLEZALC);
654 MI.setOpcode(Mips::BGEZALC);
657 MI.setOpcode(Mips::BGEUC);
661 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
663 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
666 MI.addOperand(MCOperand::CreateImm(Imm));
668 return MCDisassembler::Success;
671 /// readInstruction - read four bytes from the MemoryObject
672 /// and return 32 bit word sorted according to the given endianess
673 static DecodeStatus readInstruction32(const MemoryObject ®ion,
681 // We want to read exactly 4 Bytes of data.
682 if (region.readBytes(address, 4, Bytes) == -1) {
684 return MCDisassembler::Fail;
688 // Encoded as a big-endian 32-bit word in the stream.
689 insn = (Bytes[3] << 0) |
695 // Encoded as a small-endian 32-bit word in the stream.
696 // Little-endian byte ordering:
697 // mips32r2: 4 | 3 | 2 | 1
698 // microMIPS: 2 | 1 | 4 | 3
700 insn = (Bytes[2] << 0) |
705 insn = (Bytes[0] << 0) |
712 return MCDisassembler::Success;
716 MipsDisassembler::getInstruction(MCInst &instr,
718 const MemoryObject &Region,
720 raw_ostream &vStream,
721 raw_ostream &cStream) const {
724 DecodeStatus Result = readInstruction32(Region, Address, Size,
725 Insn, isBigEndian, IsMicroMips);
726 if (Result == MCDisassembler::Fail)
727 return MCDisassembler::Fail;
730 // Calling the auto-generated decoder function.
731 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
733 if (Result != MCDisassembler::Fail) {
737 return MCDisassembler::Fail;
741 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
743 if (Result != MCDisassembler::Fail) {
749 // Calling the auto-generated decoder function.
750 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
752 if (Result != MCDisassembler::Fail) {
757 return MCDisassembler::Fail;
761 Mips64Disassembler::getInstruction(MCInst &instr,
763 const MemoryObject &Region,
765 raw_ostream &vStream,
766 raw_ostream &cStream) const {
769 DecodeStatus Result = readInstruction32(Region, Address, Size,
770 Insn, isBigEndian, false);
771 if (Result == MCDisassembler::Fail)
772 return MCDisassembler::Fail;
774 // Calling the auto-generated decoder function.
775 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
777 if (Result != MCDisassembler::Fail) {
781 // If we fail to decode in Mips64 decoder space we can try in Mips32
782 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
784 if (Result != MCDisassembler::Fail) {
789 return MCDisassembler::Fail;
792 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
795 const void *Decoder) {
797 return MCDisassembler::Fail;
801 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
804 const void *Decoder) {
807 return MCDisassembler::Fail;
809 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
810 Inst.addOperand(MCOperand::CreateReg(Reg));
811 return MCDisassembler::Success;
814 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
817 const void *Decoder) {
819 return MCDisassembler::Fail;
820 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
821 Inst.addOperand(MCOperand::CreateReg(Reg));
822 return MCDisassembler::Success;
825 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
828 const void *Decoder) {
829 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
830 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
832 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
835 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
838 const void *Decoder) {
839 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
842 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
845 const void *Decoder) {
847 return MCDisassembler::Fail;
849 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
850 Inst.addOperand(MCOperand::CreateReg(Reg));
851 return MCDisassembler::Success;
854 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
857 const void *Decoder) {
859 return MCDisassembler::Fail;
861 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
862 Inst.addOperand(MCOperand::CreateReg(Reg));
863 return MCDisassembler::Success;
866 static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
869 const void *Decoder) {
871 return MCDisassembler::Fail;
873 unsigned Reg = getReg(Decoder, Mips::FGRH32RegClassID, RegNo);
874 Inst.addOperand(MCOperand::CreateReg(Reg));
875 return MCDisassembler::Success;
878 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
881 const void *Decoder) {
883 return MCDisassembler::Fail;
884 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
885 Inst.addOperand(MCOperand::CreateReg(Reg));
886 return MCDisassembler::Success;
889 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
892 const void *Decoder) {
894 return MCDisassembler::Fail;
895 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
896 Inst.addOperand(MCOperand::CreateReg(Reg));
897 return MCDisassembler::Success;
900 static DecodeStatus DecodeMem(MCInst &Inst,
903 const void *Decoder) {
904 int Offset = SignExtend32<16>(Insn & 0xffff);
905 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
906 unsigned Base = fieldFromInstruction(Insn, 21, 5);
908 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
909 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
911 if(Inst.getOpcode() == Mips::SC){
912 Inst.addOperand(MCOperand::CreateReg(Reg));
915 Inst.addOperand(MCOperand::CreateReg(Reg));
916 Inst.addOperand(MCOperand::CreateReg(Base));
917 Inst.addOperand(MCOperand::CreateImm(Offset));
919 return MCDisassembler::Success;
922 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
923 uint64_t Address, const void *Decoder) {
924 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
925 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
926 unsigned Base = fieldFromInstruction(Insn, 11, 5);
928 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
929 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
931 Inst.addOperand(MCOperand::CreateReg(Reg));
932 Inst.addOperand(MCOperand::CreateReg(Base));
934 // The immediate field of an LD/ST instruction is scaled which means it must
935 // be multiplied (when decoding) by the size (in bytes) of the instructions'
941 switch(Inst.getOpcode())
944 assert (0 && "Unexpected instruction");
945 return MCDisassembler::Fail;
949 Inst.addOperand(MCOperand::CreateImm(Offset));
953 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
957 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
961 Inst.addOperand(MCOperand::CreateImm(Offset << 3));
965 return MCDisassembler::Success;
968 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
971 const void *Decoder) {
972 int Offset = SignExtend32<12>(Insn & 0x0fff);
973 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
974 unsigned Base = fieldFromInstruction(Insn, 16, 5);
976 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
977 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
979 if (Inst.getOpcode() == Mips::SC_MM)
980 Inst.addOperand(MCOperand::CreateReg(Reg));
982 Inst.addOperand(MCOperand::CreateReg(Reg));
983 Inst.addOperand(MCOperand::CreateReg(Base));
984 Inst.addOperand(MCOperand::CreateImm(Offset));
986 return MCDisassembler::Success;
989 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
992 const void *Decoder) {
993 int Offset = SignExtend32<16>(Insn & 0xffff);
994 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
995 unsigned Base = fieldFromInstruction(Insn, 16, 5);
997 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
998 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1000 Inst.addOperand(MCOperand::CreateReg(Reg));
1001 Inst.addOperand(MCOperand::CreateReg(Base));
1002 Inst.addOperand(MCOperand::CreateImm(Offset));
1004 return MCDisassembler::Success;
1007 static DecodeStatus DecodeFMem(MCInst &Inst,
1010 const void *Decoder) {
1011 int Offset = SignExtend32<16>(Insn & 0xffff);
1012 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1013 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1015 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1016 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1018 Inst.addOperand(MCOperand::CreateReg(Reg));
1019 Inst.addOperand(MCOperand::CreateReg(Base));
1020 Inst.addOperand(MCOperand::CreateImm(Offset));
1022 return MCDisassembler::Success;
1026 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1029 const void *Decoder) {
1030 // Currently only hardware register 29 is supported.
1032 return MCDisassembler::Fail;
1033 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1034 return MCDisassembler::Success;
1037 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1040 const void *Decoder) {
1041 if (RegNo > 30 || RegNo %2)
1042 return MCDisassembler::Fail;
1045 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1046 Inst.addOperand(MCOperand::CreateReg(Reg));
1047 return MCDisassembler::Success;
1050 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1053 const void *Decoder) {
1055 return MCDisassembler::Fail;
1057 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1058 Inst.addOperand(MCOperand::CreateReg(Reg));
1059 return MCDisassembler::Success;
1062 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1065 const void *Decoder) {
1067 return MCDisassembler::Fail;
1069 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1070 Inst.addOperand(MCOperand::CreateReg(Reg));
1071 return MCDisassembler::Success;
1074 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1077 const void *Decoder) {
1079 return MCDisassembler::Fail;
1081 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1082 Inst.addOperand(MCOperand::CreateReg(Reg));
1083 return MCDisassembler::Success;
1086 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1089 const void *Decoder) {
1091 return MCDisassembler::Fail;
1093 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1094 Inst.addOperand(MCOperand::CreateReg(Reg));
1095 return MCDisassembler::Success;
1098 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1101 const void *Decoder) {
1103 return MCDisassembler::Fail;
1105 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1106 Inst.addOperand(MCOperand::CreateReg(Reg));
1107 return MCDisassembler::Success;
1110 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1113 const void *Decoder) {
1115 return MCDisassembler::Fail;
1117 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1118 Inst.addOperand(MCOperand::CreateReg(Reg));
1119 return MCDisassembler::Success;
1122 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1125 const void *Decoder) {
1127 return MCDisassembler::Fail;
1129 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1130 Inst.addOperand(MCOperand::CreateReg(Reg));
1131 return MCDisassembler::Success;
1134 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1137 const void *Decoder) {
1139 return MCDisassembler::Fail;
1141 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1142 Inst.addOperand(MCOperand::CreateReg(Reg));
1143 return MCDisassembler::Success;
1146 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1149 const void *Decoder) {
1151 return MCDisassembler::Fail;
1153 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1154 Inst.addOperand(MCOperand::CreateReg(Reg));
1155 return MCDisassembler::Success;
1158 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1161 const void *Decoder) {
1162 int32_t BranchOffset = (SignExtend32<16>(Offset) << 2) + 4;
1163 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1164 return MCDisassembler::Success;
1167 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1170 const void *Decoder) {
1172 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1173 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1174 return MCDisassembler::Success;
1177 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1180 const void *Decoder) {
1181 int32_t BranchOffset = SignExtend32<21>(Offset) << 2;
1183 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1184 return MCDisassembler::Success;
1187 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1190 const void *Decoder) {
1191 int32_t BranchOffset = SignExtend32<26>(Offset) << 2;
1193 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1194 return MCDisassembler::Success;
1197 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1200 const void *Decoder) {
1201 int32_t BranchOffset = SignExtend32<16>(Offset) << 1;
1202 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1203 return MCDisassembler::Success;
1206 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1209 const void *Decoder) {
1210 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1211 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1212 return MCDisassembler::Success;
1215 static DecodeStatus DecodeSimm16(MCInst &Inst,
1218 const void *Decoder) {
1219 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1220 return MCDisassembler::Success;
1223 static DecodeStatus DecodeLSAImm(MCInst &Inst,
1226 const void *Decoder) {
1227 // We add one to the immediate field as it was encoded as 'imm - 1'.
1228 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1229 return MCDisassembler::Success;
1232 static DecodeStatus DecodeInsSize(MCInst &Inst,
1235 const void *Decoder) {
1236 // First we need to grab the pos(lsb) from MCInst.
1237 int Pos = Inst.getOperand(2).getImm();
1238 int Size = (int) Insn - Pos + 1;
1239 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1240 return MCDisassembler::Success;
1243 static DecodeStatus DecodeExtSize(MCInst &Inst,
1246 const void *Decoder) {
1247 int Size = (int) Insn + 1;
1248 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1249 return MCDisassembler::Success;
1252 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1253 uint64_t Address, const void *Decoder) {
1254 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) << 2));
1255 return MCDisassembler::Success;
1258 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1259 uint64_t Address, const void *Decoder) {
1260 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) << 3));
1261 return MCDisassembler::Success;