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
, bool bigEndian
)
61 : MipsDisassemblerBase(STI
, Ctx
, bigEndian
) {
62 IsMicroMips
= STI
.getFeatureBits() & Mips::FeatureMicroMips
;
65 bool hasMips3() const { return STI
.getFeatureBits() & Mips::FeatureMips3
; }
66 bool hasMips32() const { return STI
.getFeatureBits() & Mips::FeatureMips32
; }
67 bool hasMips32r6() const {
68 return STI
.getFeatureBits() & Mips::FeatureMips32r6
;
71 bool isGP64() const { return STI
.getFeatureBits() & Mips::FeatureGP64Bit
; }
73 bool hasCOP3() const {
74 // Only present in MIPS-I and MIPS-II
75 return !hasMips32() && !hasMips3();
78 /// getInstruction - See MCDisassembler.
79 DecodeStatus
getInstruction(MCInst
&instr
,
81 const MemoryObject
®ion
,
84 raw_ostream
&cStream
) const override
;
88 /// Mips64Disassembler - a disasembler class for Mips64.
89 class Mips64Disassembler
: public MipsDisassemblerBase
{
91 /// Constructor - Initializes the disassembler.
93 Mips64Disassembler(const MCSubtargetInfo
&STI
, MCContext
&Ctx
,
95 MipsDisassemblerBase(STI
, Ctx
, bigEndian
) {}
97 /// getInstruction - See MCDisassembler.
98 DecodeStatus
getInstruction(MCInst
&instr
,
100 const MemoryObject
®ion
,
102 raw_ostream
&vStream
,
103 raw_ostream
&cStream
) const override
;
106 } // end anonymous namespace
108 // Forward declare these because the autogenerated code will reference them.
109 // Definitions are further down.
110 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
113 const void *Decoder
);
115 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
118 const void *Decoder
);
120 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
123 const void *Decoder
);
125 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
128 const void *Decoder
);
130 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
133 const void *Decoder
);
135 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
138 const void *Decoder
);
140 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
143 const void *Decoder
);
145 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
148 const void *Decoder
);
150 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
153 const void *Decoder
);
155 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
157 const void *Decoder
);
159 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
162 const void *Decoder
);
164 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
167 const void *Decoder
);
169 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
172 const void *Decoder
);
174 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
177 const void *Decoder
);
179 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
182 const void *Decoder
);
184 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
187 const void *Decoder
);
189 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
192 const void *Decoder
);
194 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
197 const void *Decoder
);
199 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
202 const void *Decoder
);
204 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
207 const void *Decoder
);
209 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
212 const void *Decoder
);
214 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
217 const void *Decoder
);
219 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
222 const void *Decoder
);
224 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
227 const void *Decoder
);
229 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
232 const void *Decoder
);
234 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
235 // shifted left by 1 bit.
236 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
239 const void *Decoder
);
241 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
242 // shifted left by 1 bit.
243 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
246 const void *Decoder
);
248 static DecodeStatus
DecodeMem(MCInst
&Inst
,
251 const void *Decoder
);
253 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
,
256 const void *Decoder
);
258 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
259 uint64_t Address
, const void *Decoder
);
261 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
264 const void *Decoder
);
266 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
269 const void *Decoder
);
271 static DecodeStatus
DecodeFMem(MCInst
&Inst
, unsigned Insn
,
273 const void *Decoder
);
275 static DecodeStatus
DecodeFMem2(MCInst
&Inst
, unsigned Insn
,
277 const void *Decoder
);
279 static DecodeStatus
DecodeFMem3(MCInst
&Inst
, unsigned Insn
,
281 const void *Decoder
);
283 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
286 const void *Decoder
);
288 static DecodeStatus
DecodeSimm16(MCInst
&Inst
,
291 const void *Decoder
);
293 // Decode the immediate field of an LSA instruction which
295 static DecodeStatus
DecodeLSAImm(MCInst
&Inst
,
298 const void *Decoder
);
300 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
303 const void *Decoder
);
305 static DecodeStatus
DecodeExtSize(MCInst
&Inst
,
308 const void *Decoder
);
310 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
311 uint64_t Address
, const void *Decoder
);
313 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
314 uint64_t Address
, const void *Decoder
);
316 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
318 template <typename InsnType
>
319 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
320 const void *Decoder
);
322 template <typename InsnType
>
324 DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
325 const void *Decoder
);
327 template <typename InsnType
>
329 DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
330 const void *Decoder
);
332 template <typename InsnType
>
334 DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
335 const void *Decoder
);
337 template <typename InsnType
>
339 DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
340 const void *Decoder
);
342 template <typename InsnType
>
344 DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
345 const void *Decoder
);
347 template <typename InsnType
>
349 DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
350 const void *Decoder
);
353 extern Target TheMipselTarget
, TheMipsTarget
, TheMips64Target
,
357 static MCDisassembler
*createMipsDisassembler(
359 const MCSubtargetInfo
&STI
,
361 return new MipsDisassembler(STI
, Ctx
, true);
364 static MCDisassembler
*createMipselDisassembler(
366 const MCSubtargetInfo
&STI
,
368 return new MipsDisassembler(STI
, Ctx
, false);
371 static MCDisassembler
*createMips64Disassembler(
373 const MCSubtargetInfo
&STI
,
375 return new Mips64Disassembler(STI
, Ctx
, true);
378 static MCDisassembler
*createMips64elDisassembler(
380 const MCSubtargetInfo
&STI
,
382 return new Mips64Disassembler(STI
, Ctx
, false);
385 extern "C" void LLVMInitializeMipsDisassembler() {
386 // Register the disassembler.
387 TargetRegistry::RegisterMCDisassembler(TheMipsTarget
,
388 createMipsDisassembler
);
389 TargetRegistry::RegisterMCDisassembler(TheMipselTarget
,
390 createMipselDisassembler
);
391 TargetRegistry::RegisterMCDisassembler(TheMips64Target
,
392 createMips64Disassembler
);
393 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget
,
394 createMips64elDisassembler
);
397 #include "MipsGenDisassemblerTables.inc"
399 static unsigned getReg(const void *D
, unsigned RC
, unsigned RegNo
) {
400 const MipsDisassemblerBase
*Dis
= static_cast<const MipsDisassemblerBase
*>(D
);
401 const MCRegisterInfo
*RegInfo
= Dis
->getContext().getRegisterInfo();
402 return *(RegInfo
->getRegClass(RC
).begin() + RegNo
);
405 template <typename InsnType
>
406 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
407 const void *Decoder
) {
408 typedef DecodeStatus (*DecodeFN
)(MCInst
&, unsigned, uint64_t, const void *);
409 // The size of the n field depends on the element size
410 // The register class also depends on this.
411 InsnType tmp
= fieldFromInstruction(insn
, 17, 5);
413 DecodeFN RegDecoder
= nullptr;
414 if ((tmp
& 0x18) == 0x00) { // INSVE_B
416 RegDecoder
= DecodeMSA128BRegisterClass
;
417 } else if ((tmp
& 0x1c) == 0x10) { // INSVE_H
419 RegDecoder
= DecodeMSA128HRegisterClass
;
420 } else if ((tmp
& 0x1e) == 0x18) { // INSVE_W
422 RegDecoder
= DecodeMSA128WRegisterClass
;
423 } else if ((tmp
& 0x1f) == 0x1c) { // INSVE_D
425 RegDecoder
= DecodeMSA128DRegisterClass
;
427 llvm_unreachable("Invalid encoding");
429 assert(NSize
!= 0 && RegDecoder
!= nullptr);
432 tmp
= fieldFromInstruction(insn
, 6, 5);
433 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
434 return MCDisassembler::Fail
;
436 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
437 return MCDisassembler::Fail
;
439 tmp
= fieldFromInstruction(insn
, 16, NSize
);
440 MI
.addOperand(MCOperand::CreateImm(tmp
));
442 tmp
= fieldFromInstruction(insn
, 11, 5);
443 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
444 return MCDisassembler::Fail
;
446 MI
.addOperand(MCOperand::CreateImm(0));
448 return MCDisassembler::Success
;
451 template <typename InsnType
>
452 static DecodeStatus
DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
,
454 const void *Decoder
) {
455 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
456 // (otherwise we would have matched the ADDI instruction from the earlier
460 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
462 // BEQZALC if rs == 0 && rt != 0
463 // BEQC if rs < rt && rs != 0
465 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
466 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
467 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
471 MI
.setOpcode(Mips::BOVC
);
473 } else if (Rs
!= 0 && Rs
< Rt
) {
474 MI
.setOpcode(Mips::BEQC
);
477 MI
.setOpcode(Mips::BEQZALC
);
480 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
483 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
485 MI
.addOperand(MCOperand::CreateImm(Imm
));
487 return MCDisassembler::Success
;
490 template <typename InsnType
>
491 static DecodeStatus
DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
,
493 const void *Decoder
) {
494 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
495 // (otherwise we would have matched the ADDI instruction from the earlier
499 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
501 // BNEZALC if rs == 0 && rt != 0
502 // BNEC if rs < rt && rs != 0
504 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
505 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
506 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
510 MI
.setOpcode(Mips::BNVC
);
512 } else if (Rs
!= 0 && Rs
< Rt
) {
513 MI
.setOpcode(Mips::BNEC
);
516 MI
.setOpcode(Mips::BNEZALC
);
519 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
522 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
524 MI
.addOperand(MCOperand::CreateImm(Imm
));
526 return MCDisassembler::Success
;
529 template <typename InsnType
>
530 static DecodeStatus
DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
,
532 const void *Decoder
) {
533 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
534 // (otherwise we would have matched the BLEZL instruction from the earlier
538 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
539 // Invalid if rs == 0
540 // BLEZC if rs == 0 && rt != 0
541 // BGEZC if rs == rt && rt != 0
542 // BGEC if rs != rt && rs != 0 && rt != 0
544 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
545 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
546 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
550 return MCDisassembler::Fail
;
552 MI
.setOpcode(Mips::BLEZC
);
554 MI
.setOpcode(Mips::BGEZC
);
557 MI
.setOpcode(Mips::BGEC
);
561 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
564 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
567 MI
.addOperand(MCOperand::CreateImm(Imm
));
569 return MCDisassembler::Success
;
572 template <typename InsnType
>
573 static DecodeStatus
DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
,
575 const void *Decoder
) {
576 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
577 // (otherwise we would have matched the BGTZL instruction from the earlier
581 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
582 // Invalid if rs == 0
583 // BGTZC if rs == 0 && rt != 0
584 // BLTZC if rs == rt && rt != 0
585 // BLTC if rs != rt && rs != 0 && rt != 0
589 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
590 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
591 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
594 return MCDisassembler::Fail
;
596 MI
.setOpcode(Mips::BGTZC
);
598 MI
.setOpcode(Mips::BLTZC
);
600 MI
.setOpcode(Mips::BLTC
);
605 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
608 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
611 MI
.addOperand(MCOperand::CreateImm(Imm
));
613 return MCDisassembler::Success
;
616 template <typename InsnType
>
617 static DecodeStatus
DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
,
619 const void *Decoder
) {
620 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
621 // (otherwise we would have matched the BGTZ instruction from the earlier
625 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
627 // BGTZALC if rs == 0 && rt != 0
628 // BLTZALC if rs != 0 && rs == rt
629 // BLTUC if rs != 0 && rs != rt
631 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
632 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
633 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
638 MI
.setOpcode(Mips::BGTZ
);
640 } else if (Rs
== 0) {
641 MI
.setOpcode(Mips::BGTZALC
);
643 } else if (Rs
== Rt
) {
644 MI
.setOpcode(Mips::BLTZALC
);
647 MI
.setOpcode(Mips::BLTUC
);
653 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
657 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
660 MI
.addOperand(MCOperand::CreateImm(Imm
));
662 return MCDisassembler::Success
;
665 template <typename InsnType
>
666 static DecodeStatus
DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
,
668 const void *Decoder
) {
669 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
670 // (otherwise we would have matched the BLEZL instruction from the earlier
674 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
675 // Invalid if rs == 0
676 // BLEZALC if rs == 0 && rt != 0
677 // BGEZALC if rs == rt && rt != 0
678 // BGEUC if rs != rt && rs != 0 && rt != 0
680 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
681 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
682 InsnType Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4;
686 return MCDisassembler::Fail
;
688 MI
.setOpcode(Mips::BLEZALC
);
690 MI
.setOpcode(Mips::BGEZALC
);
693 MI
.setOpcode(Mips::BGEUC
);
697 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
699 MI
.addOperand(MCOperand::CreateReg(getReg(Decoder
, Mips::GPR32RegClassID
,
702 MI
.addOperand(MCOperand::CreateImm(Imm
));
704 return MCDisassembler::Success
;
707 /// readInstruction - read four bytes from the MemoryObject
708 /// and return 32 bit word sorted according to the given endianess
709 static DecodeStatus
readInstruction32(const MemoryObject
®ion
,
717 // We want to read exactly 4 Bytes of data.
718 if (region
.readBytes(address
, 4, Bytes
) == -1) {
720 return MCDisassembler::Fail
;
724 // Encoded as a big-endian 32-bit word in the stream.
725 insn
= (Bytes
[3] << 0) |
731 // Encoded as a small-endian 32-bit word in the stream.
732 // Little-endian byte ordering:
733 // mips32r2: 4 | 3 | 2 | 1
734 // microMIPS: 2 | 1 | 4 | 3
736 insn
= (Bytes
[2] << 0) |
741 insn
= (Bytes
[0] << 0) |
748 return MCDisassembler::Success
;
752 MipsDisassembler::getInstruction(MCInst
&instr
,
754 const MemoryObject
&Region
,
756 raw_ostream
&vStream
,
757 raw_ostream
&cStream
) const {
760 DecodeStatus Result
= readInstruction32(Region
, Address
, Size
,
761 Insn
, isBigEndian
, IsMicroMips
);
762 if (Result
== MCDisassembler::Fail
)
763 return MCDisassembler::Fail
;
766 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
767 // Calling the auto-generated decoder function.
768 Result
= decodeInstruction(DecoderTableMicroMips32
, instr
, Insn
, Address
,
770 if (Result
!= MCDisassembler::Fail
) {
774 return MCDisassembler::Fail
;
778 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
780 decodeInstruction(DecoderTableCOP3_32
, instr
, Insn
, Address
, this, STI
);
781 if (Result
!= MCDisassembler::Fail
) {
787 if (hasMips32r6() && isGP64()) {
788 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
789 Result
= decodeInstruction(DecoderTableMips32r6_64r6_GP6432
, instr
, Insn
,
791 if (Result
!= MCDisassembler::Fail
) {
798 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
799 Result
= decodeInstruction(DecoderTableMips32r6_64r632
, instr
, Insn
,
801 if (Result
!= MCDisassembler::Fail
) {
807 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
808 // Calling the auto-generated decoder function.
809 Result
= decodeInstruction(DecoderTableMips32
, instr
, Insn
, Address
,
811 if (Result
!= MCDisassembler::Fail
) {
816 return MCDisassembler::Fail
;
820 Mips64Disassembler::getInstruction(MCInst
&instr
,
822 const MemoryObject
&Region
,
824 raw_ostream
&vStream
,
825 raw_ostream
&cStream
) const {
828 DecodeStatus Result
= readInstruction32(Region
, Address
, Size
,
829 Insn
, isBigEndian
, false);
830 if (Result
== MCDisassembler::Fail
)
831 return MCDisassembler::Fail
;
833 // Calling the auto-generated decoder function.
834 Result
= decodeInstruction(DecoderTableMips6432
, instr
, Insn
, Address
,
836 if (Result
!= MCDisassembler::Fail
) {
840 // If we fail to decode in Mips64 decoder space we can try in Mips32
841 Result
= decodeInstruction(DecoderTableMips32
, instr
, Insn
, Address
,
843 if (Result
!= MCDisassembler::Fail
) {
848 return MCDisassembler::Fail
;
851 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
854 const void *Decoder
) {
856 return MCDisassembler::Fail
;
860 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
863 const void *Decoder
) {
866 return MCDisassembler::Fail
;
868 unsigned Reg
= getReg(Decoder
, Mips::GPR64RegClassID
, RegNo
);
869 Inst
.addOperand(MCOperand::CreateReg(Reg
));
870 return MCDisassembler::Success
;
873 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
876 const void *Decoder
) {
878 return MCDisassembler::Fail
;
879 unsigned Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, RegNo
);
880 Inst
.addOperand(MCOperand::CreateReg(Reg
));
881 return MCDisassembler::Success
;
884 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
887 const void *Decoder
) {
888 if (static_cast<const MipsDisassembler
*>(Decoder
)->isN64())
889 return DecodeGPR64RegisterClass(Inst
, RegNo
, Address
, Decoder
);
891 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
894 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
897 const void *Decoder
) {
898 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
901 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
904 const void *Decoder
) {
906 return MCDisassembler::Fail
;
908 unsigned Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, RegNo
);
909 Inst
.addOperand(MCOperand::CreateReg(Reg
));
910 return MCDisassembler::Success
;
913 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
916 const void *Decoder
) {
918 return MCDisassembler::Fail
;
920 unsigned Reg
= getReg(Decoder
, Mips::FGR32RegClassID
, RegNo
);
921 Inst
.addOperand(MCOperand::CreateReg(Reg
));
922 return MCDisassembler::Success
;
925 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
928 const void *Decoder
) {
930 return MCDisassembler::Fail
;
931 unsigned Reg
= getReg(Decoder
, Mips::CCRRegClassID
, RegNo
);
932 Inst
.addOperand(MCOperand::CreateReg(Reg
));
933 return MCDisassembler::Success
;
936 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
939 const void *Decoder
) {
941 return MCDisassembler::Fail
;
942 unsigned Reg
= getReg(Decoder
, Mips::FCCRegClassID
, RegNo
);
943 Inst
.addOperand(MCOperand::CreateReg(Reg
));
944 return MCDisassembler::Success
;
947 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
949 const void *Decoder
) {
951 return MCDisassembler::Fail
;
953 unsigned Reg
= getReg(Decoder
, Mips::FGRCCRegClassID
, RegNo
);
954 Inst
.addOperand(MCOperand::CreateReg(Reg
));
955 return MCDisassembler::Success
;
958 static DecodeStatus
DecodeMem(MCInst
&Inst
,
961 const void *Decoder
) {
962 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
963 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
964 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
966 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
967 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
969 if(Inst
.getOpcode() == Mips::SC
){
970 Inst
.addOperand(MCOperand::CreateReg(Reg
));
973 Inst
.addOperand(MCOperand::CreateReg(Reg
));
974 Inst
.addOperand(MCOperand::CreateReg(Base
));
975 Inst
.addOperand(MCOperand::CreateImm(Offset
));
977 return MCDisassembler::Success
;
980 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
,
983 const void *Decoder
) {
984 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
985 unsigned Hint
= fieldFromInstruction(Insn
, 16, 5);
986 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
988 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
990 Inst
.addOperand(MCOperand::CreateReg(Base
));
991 Inst
.addOperand(MCOperand::CreateImm(Offset
));
992 Inst
.addOperand(MCOperand::CreateImm(Hint
));
994 return MCDisassembler::Success
;
997 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
998 uint64_t Address
, const void *Decoder
) {
999 int Offset
= SignExtend32
<10>(fieldFromInstruction(Insn
, 16, 10));
1000 unsigned Reg
= fieldFromInstruction(Insn
, 6, 5);
1001 unsigned Base
= fieldFromInstruction(Insn
, 11, 5);
1003 Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, Reg
);
1004 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1006 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1007 Inst
.addOperand(MCOperand::CreateReg(Base
));
1009 // The immediate field of an LD/ST instruction is scaled which means it must
1010 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1016 switch(Inst
.getOpcode())
1019 assert (0 && "Unexpected instruction");
1020 return MCDisassembler::Fail
;
1024 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1028 Inst
.addOperand(MCOperand::CreateImm(Offset
* 2));
1032 Inst
.addOperand(MCOperand::CreateImm(Offset
* 4));
1036 Inst
.addOperand(MCOperand::CreateImm(Offset
* 8));
1040 return MCDisassembler::Success
;
1043 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
1046 const void *Decoder
) {
1047 int Offset
= SignExtend32
<12>(Insn
& 0x0fff);
1048 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1049 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1051 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1052 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1054 if (Inst
.getOpcode() == Mips::SC_MM
)
1055 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1057 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1058 Inst
.addOperand(MCOperand::CreateReg(Base
));
1059 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1061 return MCDisassembler::Success
;
1064 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
1067 const void *Decoder
) {
1068 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1069 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1070 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1072 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1073 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1075 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1076 Inst
.addOperand(MCOperand::CreateReg(Base
));
1077 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1079 return MCDisassembler::Success
;
1082 static DecodeStatus
DecodeFMem(MCInst
&Inst
,
1085 const void *Decoder
) {
1086 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1087 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1088 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1090 Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, Reg
);
1091 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1093 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1094 Inst
.addOperand(MCOperand::CreateReg(Base
));
1095 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1097 return MCDisassembler::Success
;
1100 static DecodeStatus
DecodeFMem2(MCInst
&Inst
,
1103 const void *Decoder
) {
1104 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1105 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1106 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1108 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
1109 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1111 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1112 Inst
.addOperand(MCOperand::CreateReg(Base
));
1113 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1115 return MCDisassembler::Success
;
1118 static DecodeStatus
DecodeFMem3(MCInst
&Inst
,
1121 const void *Decoder
) {
1122 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1123 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1124 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1126 Reg
= getReg(Decoder
, Mips::COP3RegClassID
, Reg
);
1127 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1129 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1130 Inst
.addOperand(MCOperand::CreateReg(Base
));
1131 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1133 return MCDisassembler::Success
;
1136 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
1139 const void *Decoder
) {
1140 int64_t Offset
= SignExtend64
<9>((Insn
>> 7) & 0x1ff);
1141 unsigned Rt
= fieldFromInstruction(Insn
, 16, 5);
1142 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1144 Rt
= getReg(Decoder
, Mips::GPR32RegClassID
, Rt
);
1145 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1147 if(Inst
.getOpcode() == Mips::SC_R6
|| Inst
.getOpcode() == Mips::SCD_R6
){
1148 Inst
.addOperand(MCOperand::CreateReg(Rt
));
1151 Inst
.addOperand(MCOperand::CreateReg(Rt
));
1152 Inst
.addOperand(MCOperand::CreateReg(Base
));
1153 Inst
.addOperand(MCOperand::CreateImm(Offset
));
1155 return MCDisassembler::Success
;
1158 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
1161 const void *Decoder
) {
1162 // Currently only hardware register 29 is supported.
1164 return MCDisassembler::Fail
;
1165 Inst
.addOperand(MCOperand::CreateReg(Mips::HWR29
));
1166 return MCDisassembler::Success
;
1169 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
1172 const void *Decoder
) {
1173 if (RegNo
> 30 || RegNo
%2)
1174 return MCDisassembler::Fail
;
1177 unsigned Reg
= getReg(Decoder
, Mips::AFGR64RegClassID
, RegNo
/2);
1178 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1179 return MCDisassembler::Success
;
1182 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
1185 const void *Decoder
) {
1187 return MCDisassembler::Fail
;
1189 unsigned Reg
= getReg(Decoder
, Mips::ACC64DSPRegClassID
, RegNo
);
1190 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1191 return MCDisassembler::Success
;
1194 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
1197 const void *Decoder
) {
1199 return MCDisassembler::Fail
;
1201 unsigned Reg
= getReg(Decoder
, Mips::HI32DSPRegClassID
, RegNo
);
1202 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1203 return MCDisassembler::Success
;
1206 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
1209 const void *Decoder
) {
1211 return MCDisassembler::Fail
;
1213 unsigned Reg
= getReg(Decoder
, Mips::LO32DSPRegClassID
, RegNo
);
1214 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1215 return MCDisassembler::Success
;
1218 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
1221 const void *Decoder
) {
1223 return MCDisassembler::Fail
;
1225 unsigned Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, RegNo
);
1226 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1227 return MCDisassembler::Success
;
1230 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
1233 const void *Decoder
) {
1235 return MCDisassembler::Fail
;
1237 unsigned Reg
= getReg(Decoder
, Mips::MSA128HRegClassID
, RegNo
);
1238 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1239 return MCDisassembler::Success
;
1242 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
1245 const void *Decoder
) {
1247 return MCDisassembler::Fail
;
1249 unsigned Reg
= getReg(Decoder
, Mips::MSA128WRegClassID
, RegNo
);
1250 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1251 return MCDisassembler::Success
;
1254 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
1257 const void *Decoder
) {
1259 return MCDisassembler::Fail
;
1261 unsigned Reg
= getReg(Decoder
, Mips::MSA128DRegClassID
, RegNo
);
1262 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1263 return MCDisassembler::Success
;
1266 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
1269 const void *Decoder
) {
1271 return MCDisassembler::Fail
;
1273 unsigned Reg
= getReg(Decoder
, Mips::MSACtrlRegClassID
, RegNo
);
1274 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1275 return MCDisassembler::Success
;
1278 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
1281 const void *Decoder
) {
1283 return MCDisassembler::Fail
;
1285 unsigned Reg
= getReg(Decoder
, Mips::COP2RegClassID
, RegNo
);
1286 Inst
.addOperand(MCOperand::CreateReg(Reg
));
1287 return MCDisassembler::Success
;
1290 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
1293 const void *Decoder
) {
1294 int32_t BranchOffset
= (SignExtend32
<16>(Offset
) * 4) + 4;
1295 Inst
.addOperand(MCOperand::CreateImm(BranchOffset
));
1296 return MCDisassembler::Success
;
1299 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
1302 const void *Decoder
) {
1304 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 2;
1305 Inst
.addOperand(MCOperand::CreateImm(JumpOffset
));
1306 return MCDisassembler::Success
;
1309 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
1312 const void *Decoder
) {
1313 int32_t BranchOffset
= SignExtend32
<21>(Offset
) * 4;
1315 Inst
.addOperand(MCOperand::CreateImm(BranchOffset
));
1316 return MCDisassembler::Success
;
1319 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
1322 const void *Decoder
) {
1323 int32_t BranchOffset
= SignExtend32
<26>(Offset
) * 4;
1325 Inst
.addOperand(MCOperand::CreateImm(BranchOffset
));
1326 return MCDisassembler::Success
;
1329 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
1332 const void *Decoder
) {
1333 int32_t BranchOffset
= SignExtend32
<16>(Offset
) * 2;
1334 Inst
.addOperand(MCOperand::CreateImm(BranchOffset
));
1335 return MCDisassembler::Success
;
1338 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
1341 const void *Decoder
) {
1342 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 1;
1343 Inst
.addOperand(MCOperand::CreateImm(JumpOffset
));
1344 return MCDisassembler::Success
;
1347 static DecodeStatus
DecodeSimm16(MCInst
&Inst
,
1350 const void *Decoder
) {
1351 Inst
.addOperand(MCOperand::CreateImm(SignExtend32
<16>(Insn
)));
1352 return MCDisassembler::Success
;
1355 static DecodeStatus
DecodeLSAImm(MCInst
&Inst
,
1358 const void *Decoder
) {
1359 // We add one to the immediate field as it was encoded as 'imm - 1'.
1360 Inst
.addOperand(MCOperand::CreateImm(Insn
+ 1));
1361 return MCDisassembler::Success
;
1364 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
1367 const void *Decoder
) {
1368 // First we need to grab the pos(lsb) from MCInst.
1369 int Pos
= Inst
.getOperand(2).getImm();
1370 int Size
= (int) Insn
- Pos
+ 1;
1371 Inst
.addOperand(MCOperand::CreateImm(SignExtend32
<16>(Size
)));
1372 return MCDisassembler::Success
;
1375 static DecodeStatus
DecodeExtSize(MCInst
&Inst
,
1378 const void *Decoder
) {
1379 int Size
= (int) Insn
+ 1;
1380 Inst
.addOperand(MCOperand::CreateImm(SignExtend32
<16>(Size
)));
1381 return MCDisassembler::Success
;
1384 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
1385 uint64_t Address
, const void *Decoder
) {
1386 Inst
.addOperand(MCOperand::CreateImm(SignExtend32
<19>(Insn
) * 4));
1387 return MCDisassembler::Success
;
1390 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
1391 uint64_t Address
, const void *Decoder
) {
1392 Inst
.addOperand(MCOperand::CreateImm(SignExtend32
<18>(Insn
) * 8));
1393 return MCDisassembler::Success
;