1 //===- MipsDisassembler.cpp - Disassembler for Mips -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is part of the Mips Disassembler.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/MipsMCTargetDesc.h"
15 #include "TargetInfo/MipsTargetInfo.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19 #include "llvm/MC/MCFixedLenDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "mips-disassembler"
36 using DecodeStatus
= MCDisassembler::DecodeStatus
;
40 class MipsDisassembler
: public MCDisassembler
{
45 MipsDisassembler(const MCSubtargetInfo
&STI
, MCContext
&Ctx
, bool IsBigEndian
)
46 : MCDisassembler(STI
, Ctx
),
47 IsMicroMips(STI
.getFeatureBits()[Mips::FeatureMicroMips
]),
48 IsBigEndian(IsBigEndian
) {}
50 bool hasMips2() const { return STI
.getFeatureBits()[Mips::FeatureMips2
]; }
51 bool hasMips3() const { return STI
.getFeatureBits()[Mips::FeatureMips3
]; }
52 bool hasMips32() const { return STI
.getFeatureBits()[Mips::FeatureMips32
]; }
54 bool hasMips32r6() const {
55 return STI
.getFeatureBits()[Mips::FeatureMips32r6
];
58 bool isFP64() const { return STI
.getFeatureBits()[Mips::FeatureFP64Bit
]; }
60 bool isGP64() const { return STI
.getFeatureBits()[Mips::FeatureGP64Bit
]; }
62 bool isPTR64() const { return STI
.getFeatureBits()[Mips::FeaturePTR64Bit
]; }
64 bool hasCnMips() const { return STI
.getFeatureBits()[Mips::FeatureCnMips
]; }
66 bool hasCOP3() const {
67 // Only present in MIPS-I and MIPS-II
68 return !hasMips32() && !hasMips3();
71 DecodeStatus
getInstruction(MCInst
&Instr
, uint64_t &Size
,
72 ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
74 raw_ostream
&CStream
) const override
;
77 } // end anonymous namespace
79 // Forward declare these because the autogenerated code will reference them.
80 // Definitions are further down.
81 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
86 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
91 static DecodeStatus
DecodeGPRMM16RegisterClass(MCInst
&Inst
,
96 static DecodeStatus
DecodeGPRMM16ZeroRegisterClass(MCInst
&Inst
,
101 static DecodeStatus
DecodeGPRMM16MovePRegisterClass(MCInst
&Inst
,
104 const void *Decoder
);
106 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
109 const void *Decoder
);
111 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
114 const void *Decoder
);
116 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
119 const void *Decoder
);
121 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
124 const void *Decoder
);
126 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
129 const void *Decoder
);
131 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
134 const void *Decoder
);
136 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
139 const void *Decoder
);
141 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
143 const void *Decoder
);
145 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
148 const void *Decoder
);
150 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
153 const void *Decoder
);
155 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
158 const void *Decoder
);
160 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
163 const void *Decoder
);
165 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
168 const void *Decoder
);
170 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
173 const void *Decoder
);
175 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
178 const void *Decoder
);
180 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
183 const void *Decoder
);
185 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
188 const void *Decoder
);
190 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
193 const void *Decoder
);
195 static DecodeStatus
DecodeCOP0RegisterClass(MCInst
&Inst
,
198 const void *Decoder
);
200 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
203 const void *Decoder
);
205 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
208 const void *Decoder
);
210 static DecodeStatus
DecodeBranchTarget1SImm16(MCInst
&Inst
,
213 const void *Decoder
);
215 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
218 const void *Decoder
);
220 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
223 const void *Decoder
);
225 static DecodeStatus
DecodeBranchTarget21MM(MCInst
&Inst
,
228 const void *Decoder
);
230 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
233 const void *Decoder
);
235 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
236 // shifted left by 1 bit.
237 static DecodeStatus
DecodeBranchTarget7MM(MCInst
&Inst
,
240 const void *Decoder
);
242 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
243 // shifted left by 1 bit.
244 static DecodeStatus
DecodeBranchTarget10MM(MCInst
&Inst
,
247 const void *Decoder
);
249 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
250 // shifted left by 1 bit.
251 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
254 const void *Decoder
);
256 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
257 // shifted left by 1 bit.
258 static DecodeStatus
DecodeBranchTarget26MM(MCInst
&Inst
,
261 const void *Decoder
);
263 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
264 // shifted left by 1 bit.
265 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
268 const void *Decoder
);
270 // DecodeJumpTargetXMM - Decode microMIPS jump and link exchange target,
271 // which is shifted left by 2 bit.
272 static DecodeStatus
DecodeJumpTargetXMM(MCInst
&Inst
,
275 const void *Decoder
);
277 static DecodeStatus
DecodeMem(MCInst
&Inst
,
280 const void *Decoder
);
282 static DecodeStatus
DecodeMemEVA(MCInst
&Inst
,
285 const void *Decoder
);
287 static DecodeStatus
DecodeLoadByte15(MCInst
&Inst
,
290 const void *Decoder
);
292 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
293 const void *Decoder
);
295 static DecodeStatus
DecodeCacheeOp_CacheOpR6(MCInst
&Inst
,
298 const void *Decoder
);
300 static DecodeStatus
DecodeCacheOpMM(MCInst
&Inst
,
303 const void *Decoder
);
305 static DecodeStatus
DecodePrefeOpMM(MCInst
&Inst
,
308 const void *Decoder
);
310 static DecodeStatus
DecodeSyncI(MCInst
&Inst
,
313 const void *Decoder
);
315 static DecodeStatus
DecodeSyncI_MM(MCInst
&Inst
,
318 const void *Decoder
);
320 static DecodeStatus
DecodeSynciR6(MCInst
&Inst
,
323 const void *Decoder
);
325 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
326 uint64_t Address
, const void *Decoder
);
328 static DecodeStatus
DecodeMemMMImm4(MCInst
&Inst
,
331 const void *Decoder
);
333 static DecodeStatus
DecodeMemMMSPImm5Lsl2(MCInst
&Inst
,
336 const void *Decoder
);
338 static DecodeStatus
DecodeMemMMGPImm7Lsl2(MCInst
&Inst
,
341 const void *Decoder
);
343 static DecodeStatus
DecodeMemMMReglistImm4Lsl2(MCInst
&Inst
,
346 const void *Decoder
);
348 static DecodeStatus
DecodeMemMMImm9(MCInst
&Inst
,
351 const void *Decoder
);
353 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
356 const void *Decoder
);
358 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
361 const void *Decoder
);
363 static DecodeStatus
DecodeFMem(MCInst
&Inst
, unsigned Insn
,
365 const void *Decoder
);
367 static DecodeStatus
DecodeFMemMMR2(MCInst
&Inst
, unsigned Insn
,
369 const void *Decoder
);
371 static DecodeStatus
DecodeFMem2(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
372 const void *Decoder
);
374 static DecodeStatus
DecodeFMem3(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
375 const void *Decoder
);
377 static DecodeStatus
DecodeFMemCop2R6(MCInst
&Inst
, unsigned Insn
,
378 uint64_t Address
, const void *Decoder
);
380 static DecodeStatus
DecodeFMemCop2MMR6(MCInst
&Inst
, unsigned Insn
,
382 const void *Decoder
);
384 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
387 const void *Decoder
);
389 static DecodeStatus
DecodeAddiur2Simm7(MCInst
&Inst
,
392 const void *Decoder
);
394 static DecodeStatus
DecodeLi16Imm(MCInst
&Inst
,
397 const void *Decoder
);
399 static DecodeStatus
DecodePOOL16BEncodedField(MCInst
&Inst
,
402 const void *Decoder
);
404 template <unsigned Bits
, int Offset
, int Scale
>
405 static DecodeStatus
DecodeUImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
407 const void *Decoder
);
409 template <unsigned Bits
, int Offset
>
410 static DecodeStatus
DecodeUImmWithOffset(MCInst
&Inst
, unsigned Value
,
412 const void *Decoder
) {
413 return DecodeUImmWithOffsetAndScale
<Bits
, Offset
, 1>(Inst
, Value
, Address
,
417 template <unsigned Bits
, int Offset
= 0, int ScaleBy
= 1>
418 static DecodeStatus
DecodeSImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
420 const void *Decoder
);
422 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
425 const void *Decoder
);
427 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
428 uint64_t Address
, const void *Decoder
);
430 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
431 uint64_t Address
, const void *Decoder
);
433 static DecodeStatus
DecodeSimm9SP(MCInst
&Inst
, unsigned Insn
,
434 uint64_t Address
, const void *Decoder
);
436 static DecodeStatus
DecodeANDI16Imm(MCInst
&Inst
, unsigned Insn
,
437 uint64_t Address
, const void *Decoder
);
439 static DecodeStatus
DecodeSimm23Lsl2(MCInst
&Inst
, unsigned Insn
,
440 uint64_t Address
, const void *Decoder
);
442 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
444 template <typename InsnType
>
445 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
446 const void *Decoder
);
448 template <typename InsnType
>
449 static DecodeStatus
DecodeDAHIDATIMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
450 const void *Decoder
);
452 template <typename InsnType
>
453 static DecodeStatus
DecodeDAHIDATI(MCInst
&MI
, InsnType insn
, uint64_t Address
,
454 const void *Decoder
);
456 template <typename InsnType
>
457 static DecodeStatus
DecodeDAHIDATIMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
458 const void *Decoder
);
460 template <typename InsnType
>
461 static DecodeStatus
DecodeDAHIDATI(MCInst
&MI
, InsnType insn
, uint64_t Address
,
462 const void *Decoder
);
464 template <typename InsnType
>
466 DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
467 const void *Decoder
);
469 template <typename InsnType
>
471 DecodePOP35GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
472 const void *Decoder
);
474 template <typename InsnType
>
476 DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
477 const void *Decoder
);
479 template <typename InsnType
>
481 DecodePOP37GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
482 const void *Decoder
);
484 template <typename InsnType
>
486 DecodePOP65GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
487 const void *Decoder
);
489 template <typename InsnType
>
491 DecodePOP75GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
492 const void *Decoder
);
494 template <typename InsnType
>
496 DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
497 const void *Decoder
);
499 template <typename InsnType
>
501 DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
502 const void *Decoder
);
504 template <typename InsnType
>
506 DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
507 const void *Decoder
);
509 template <typename InsnType
>
511 DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
512 const void *Decoder
);
514 template <typename InsnType
>
516 DecodeBgtzGroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
517 const void *Decoder
);
519 template <typename InsnType
>
521 DecodeBlezGroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
522 const void *Decoder
);
524 template <typename InsnType
>
525 static DecodeStatus
DecodeDINS(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
526 const void *Decoder
);
528 template <typename InsnType
>
529 static DecodeStatus
DecodeDEXT(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
530 const void *Decoder
);
532 template <typename InsnType
>
533 static DecodeStatus
DecodeCRC(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
534 const void *Decoder
);
536 static DecodeStatus
DecodeRegListOperand(MCInst
&Inst
, unsigned Insn
,
538 const void *Decoder
);
540 static DecodeStatus
DecodeRegListOperand16(MCInst
&Inst
, unsigned Insn
,
542 const void *Decoder
);
544 static DecodeStatus
DecodeMovePRegPair(MCInst
&Inst
, unsigned RegPair
,
546 const void *Decoder
);
548 static DecodeStatus
DecodeMovePOperands(MCInst
&Inst
, unsigned Insn
,
549 uint64_t Address
, const void *Decoder
);
551 static MCDisassembler
*createMipsDisassembler(
553 const MCSubtargetInfo
&STI
,
555 return new MipsDisassembler(STI
, Ctx
, true);
558 static MCDisassembler
*createMipselDisassembler(
560 const MCSubtargetInfo
&STI
,
562 return new MipsDisassembler(STI
, Ctx
, false);
565 extern "C" void LLVMInitializeMipsDisassembler() {
566 // Register the disassembler.
567 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
568 createMipsDisassembler
);
569 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
570 createMipselDisassembler
);
571 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
572 createMipsDisassembler
);
573 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
574 createMipselDisassembler
);
577 #include "MipsGenDisassemblerTables.inc"
579 static unsigned getReg(const void *D
, unsigned RC
, unsigned RegNo
) {
580 const MipsDisassembler
*Dis
= static_cast<const MipsDisassembler
*>(D
);
581 const MCRegisterInfo
*RegInfo
= Dis
->getContext().getRegisterInfo();
582 return *(RegInfo
->getRegClass(RC
).begin() + RegNo
);
585 template <typename InsnType
>
586 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
587 const void *Decoder
) {
588 using DecodeFN
= DecodeStatus (*)(MCInst
&, unsigned, uint64_t, const void *);
590 // The size of the n field depends on the element size
591 // The register class also depends on this.
592 InsnType tmp
= fieldFromInstruction(insn
, 17, 5);
594 DecodeFN RegDecoder
= nullptr;
595 if ((tmp
& 0x18) == 0x00) { // INSVE_B
597 RegDecoder
= DecodeMSA128BRegisterClass
;
598 } else if ((tmp
& 0x1c) == 0x10) { // INSVE_H
600 RegDecoder
= DecodeMSA128HRegisterClass
;
601 } else if ((tmp
& 0x1e) == 0x18) { // INSVE_W
603 RegDecoder
= DecodeMSA128WRegisterClass
;
604 } else if ((tmp
& 0x1f) == 0x1c) { // INSVE_D
606 RegDecoder
= DecodeMSA128DRegisterClass
;
608 llvm_unreachable("Invalid encoding");
610 assert(NSize
!= 0 && RegDecoder
!= nullptr);
613 tmp
= fieldFromInstruction(insn
, 6, 5);
614 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
615 return MCDisassembler::Fail
;
617 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
618 return MCDisassembler::Fail
;
620 tmp
= fieldFromInstruction(insn
, 16, NSize
);
621 MI
.addOperand(MCOperand::createImm(tmp
));
623 tmp
= fieldFromInstruction(insn
, 11, 5);
624 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
625 return MCDisassembler::Fail
;
627 MI
.addOperand(MCOperand::createImm(0));
629 return MCDisassembler::Success
;
632 template <typename InsnType
>
633 static DecodeStatus
DecodeDAHIDATIMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
634 const void *Decoder
) {
635 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
636 InsnType Imm
= fieldFromInstruction(insn
, 0, 16);
637 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
639 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
641 MI
.addOperand(MCOperand::createImm(Imm
));
643 return MCDisassembler::Success
;
646 template <typename InsnType
>
647 static DecodeStatus
DecodeDAHIDATI(MCInst
&MI
, InsnType insn
, uint64_t Address
,
648 const void *Decoder
) {
649 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
650 InsnType Imm
= fieldFromInstruction(insn
, 0, 16);
651 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
653 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
655 MI
.addOperand(MCOperand::createImm(Imm
));
657 return MCDisassembler::Success
;
660 template <typename InsnType
>
661 static DecodeStatus
DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
,
663 const void *Decoder
) {
664 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
665 // (otherwise we would have matched the ADDI instruction from the earlier
669 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
671 // BEQZALC if rs == 0 && rt != 0
672 // BEQC if rs < rt && rs != 0
674 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
675 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
676 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
680 MI
.setOpcode(Mips::BOVC
);
682 } else if (Rs
!= 0 && Rs
< Rt
) {
683 MI
.setOpcode(Mips::BEQC
);
686 MI
.setOpcode(Mips::BEQZALC
);
689 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
692 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
694 MI
.addOperand(MCOperand::createImm(Imm
));
696 return MCDisassembler::Success
;
699 template <typename InsnType
>
700 static DecodeStatus
DecodePOP35GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
702 const void *Decoder
) {
703 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
704 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
708 MI
.setOpcode(Mips::BOVC_MMR6
);
709 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
711 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
713 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
714 } else if (Rs
!= 0 && Rs
< Rt
) {
715 MI
.setOpcode(Mips::BEQC_MMR6
);
716 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
718 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
720 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
722 MI
.setOpcode(Mips::BEQZALC_MMR6
);
723 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
725 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
728 MI
.addOperand(MCOperand::createImm(Imm
));
730 return MCDisassembler::Success
;
733 template <typename InsnType
>
734 static DecodeStatus
DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
,
736 const void *Decoder
) {
737 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
738 // (otherwise we would have matched the ADDI instruction from the earlier
742 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
744 // BNEZALC if rs == 0 && rt != 0
745 // BNEC if rs < rt && rs != 0
747 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
748 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
749 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
753 MI
.setOpcode(Mips::BNVC
);
755 } else if (Rs
!= 0 && Rs
< Rt
) {
756 MI
.setOpcode(Mips::BNEC
);
759 MI
.setOpcode(Mips::BNEZALC
);
762 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
765 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
767 MI
.addOperand(MCOperand::createImm(Imm
));
769 return MCDisassembler::Success
;
772 template <typename InsnType
>
773 static DecodeStatus
DecodePOP37GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
775 const void *Decoder
) {
776 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
777 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
781 MI
.setOpcode(Mips::BNVC_MMR6
);
782 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
784 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
786 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
787 } else if (Rs
!= 0 && Rs
< Rt
) {
788 MI
.setOpcode(Mips::BNEC_MMR6
);
789 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
791 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
793 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
795 MI
.setOpcode(Mips::BNEZALC_MMR6
);
796 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
798 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
801 MI
.addOperand(MCOperand::createImm(Imm
));
803 return MCDisassembler::Success
;
806 template <typename InsnType
>
807 static DecodeStatus
DecodePOP65GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
809 const void *Decoder
) {
811 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
812 // Invalid if rt == 0
813 // BGTZC_MMR6 if rs == 0 && rt != 0
814 // BLTZC_MMR6 if rs == rt && rt != 0
815 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
817 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
818 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
819 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
823 return MCDisassembler::Fail
;
825 MI
.setOpcode(Mips::BGTZC_MMR6
);
827 MI
.setOpcode(Mips::BLTZC_MMR6
);
829 MI
.setOpcode(Mips::BLTC_MMR6
);
834 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
837 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
840 MI
.addOperand(MCOperand::createImm(Imm
));
842 return MCDisassembler::Success
;
845 template <typename InsnType
>
846 static DecodeStatus
DecodePOP75GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
848 const void *Decoder
) {
850 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
851 // Invalid if rt == 0
852 // BLEZC_MMR6 if rs == 0 && rt != 0
853 // BGEZC_MMR6 if rs == rt && rt != 0
854 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
856 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
857 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
858 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
862 return MCDisassembler::Fail
;
864 MI
.setOpcode(Mips::BLEZC_MMR6
);
866 MI
.setOpcode(Mips::BGEZC_MMR6
);
869 MI
.setOpcode(Mips::BGEC_MMR6
);
873 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
876 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
879 MI
.addOperand(MCOperand::createImm(Imm
));
881 return MCDisassembler::Success
;
884 template <typename InsnType
>
885 static DecodeStatus
DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
,
887 const void *Decoder
) {
888 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
889 // (otherwise we would have matched the BLEZL instruction from the earlier
893 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
894 // Invalid if rs == 0
895 // BLEZC if rs == 0 && rt != 0
896 // BGEZC if rs == rt && rt != 0
897 // BGEC if rs != rt && rs != 0 && rt != 0
899 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
900 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
901 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
905 return MCDisassembler::Fail
;
907 MI
.setOpcode(Mips::BLEZC
);
909 MI
.setOpcode(Mips::BGEZC
);
912 MI
.setOpcode(Mips::BGEC
);
916 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
919 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
922 MI
.addOperand(MCOperand::createImm(Imm
));
924 return MCDisassembler::Success
;
927 template <typename InsnType
>
928 static DecodeStatus
DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
,
930 const void *Decoder
) {
931 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
932 // (otherwise we would have matched the BGTZL instruction from the earlier
936 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
937 // Invalid if rs == 0
938 // BGTZC if rs == 0 && rt != 0
939 // BLTZC if rs == rt && rt != 0
940 // BLTC if rs != rt && rs != 0 && rt != 0
944 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
945 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
946 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
949 return MCDisassembler::Fail
;
951 MI
.setOpcode(Mips::BGTZC
);
953 MI
.setOpcode(Mips::BLTZC
);
955 MI
.setOpcode(Mips::BLTC
);
960 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
963 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
966 MI
.addOperand(MCOperand::createImm(Imm
));
968 return MCDisassembler::Success
;
971 template <typename InsnType
>
972 static DecodeStatus
DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
,
974 const void *Decoder
) {
975 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
976 // (otherwise we would have matched the BGTZ instruction from the earlier
980 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
982 // BGTZALC if rs == 0 && rt != 0
983 // BLTZALC if rs != 0 && rs == rt
984 // BLTUC if rs != 0 && rs != rt
986 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
987 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
988 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
993 MI
.setOpcode(Mips::BGTZ
);
995 } else if (Rs
== 0) {
996 MI
.setOpcode(Mips::BGTZALC
);
998 } else if (Rs
== Rt
) {
999 MI
.setOpcode(Mips::BLTZALC
);
1002 MI
.setOpcode(Mips::BLTUC
);
1008 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1012 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1015 MI
.addOperand(MCOperand::createImm(Imm
));
1017 return MCDisassembler::Success
;
1020 template <typename InsnType
>
1021 static DecodeStatus
DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
,
1023 const void *Decoder
) {
1024 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1025 // (otherwise we would have matched the BLEZL instruction from the earlier
1029 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1030 // Invalid if rs == 0
1031 // BLEZALC if rs == 0 && rt != 0
1032 // BGEZALC if rs == rt && rt != 0
1033 // BGEUC if rs != rt && rs != 0 && rt != 0
1035 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
1036 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
1037 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
1041 return MCDisassembler::Fail
;
1043 MI
.setOpcode(Mips::BLEZALC
);
1045 MI
.setOpcode(Mips::BGEZALC
);
1048 MI
.setOpcode(Mips::BGEUC
);
1052 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1054 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1057 MI
.addOperand(MCOperand::createImm(Imm
));
1059 return MCDisassembler::Success
;
1062 // Override the generated disassembler to produce DEXT all the time. This is
1063 // for feature / behaviour parity with binutils.
1064 template <typename InsnType
>
1065 static DecodeStatus
DecodeDEXT(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1066 const void *Decoder
) {
1067 unsigned Msbd
= fieldFromInstruction(Insn
, 11, 5);
1068 unsigned Lsb
= fieldFromInstruction(Insn
, 6, 5);
1072 switch (MI
.getOpcode()) {
1079 Size
= Msbd
+ 1 + 32;
1086 llvm_unreachable("Unknown DEXT instruction!");
1089 MI
.setOpcode(Mips::DEXT
);
1091 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1092 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1094 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rt
)));
1095 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rs
)));
1096 MI
.addOperand(MCOperand::createImm(Pos
));
1097 MI
.addOperand(MCOperand::createImm(Size
));
1099 return MCDisassembler::Success
;
1102 // Override the generated disassembler to produce DINS all the time. This is
1103 // for feature / behaviour parity with binutils.
1104 template <typename InsnType
>
1105 static DecodeStatus
DecodeDINS(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1106 const void *Decoder
) {
1107 unsigned Msbd
= fieldFromInstruction(Insn
, 11, 5);
1108 unsigned Lsb
= fieldFromInstruction(Insn
, 6, 5);
1112 switch (MI
.getOpcode()) {
1115 Size
= Msbd
+ 1 - Pos
;
1119 Size
= Msbd
+ 33 - Pos
;
1123 // mbsd = pos + size - 33
1124 // mbsd - pos + 33 = size
1125 Size
= Msbd
+ 33 - Pos
;
1128 llvm_unreachable("Unknown DINS instruction!");
1131 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1132 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1134 MI
.setOpcode(Mips::DINS
);
1135 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rt
)));
1136 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rs
)));
1137 MI
.addOperand(MCOperand::createImm(Pos
));
1138 MI
.addOperand(MCOperand::createImm(Size
));
1140 return MCDisassembler::Success
;
1143 // Auto-generated decoder wouldn't add the third operand for CRC32*.
1144 template <typename InsnType
>
1145 static DecodeStatus
DecodeCRC(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1146 const void *Decoder
) {
1147 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1148 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1149 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1151 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1153 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1155 return MCDisassembler::Success
;
1158 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1159 /// according to the given endianness.
1160 static DecodeStatus
readInstruction16(ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1161 uint64_t &Size
, uint32_t &Insn
,
1163 // We want to read exactly 2 Bytes of data.
1164 if (Bytes
.size() < 2) {
1166 return MCDisassembler::Fail
;
1170 Insn
= (Bytes
[0] << 8) | Bytes
[1];
1172 Insn
= (Bytes
[1] << 8) | Bytes
[0];
1175 return MCDisassembler::Success
;
1178 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1179 /// according to the given endianness.
1180 static DecodeStatus
readInstruction32(ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1181 uint64_t &Size
, uint32_t &Insn
,
1182 bool IsBigEndian
, bool IsMicroMips
) {
1183 // We want to read exactly 4 Bytes of data.
1184 if (Bytes
.size() < 4) {
1186 return MCDisassembler::Fail
;
1189 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1190 // always precede the low 16 bits in the instruction stream (that is, they
1191 // are placed at lower addresses in the instruction stream).
1193 // microMIPS byte ordering:
1194 // Big-endian: 0 | 1 | 2 | 3
1195 // Little-endian: 1 | 0 | 3 | 2
1198 // Encoded as a big-endian 32-bit word in the stream.
1200 (Bytes
[3] << 0) | (Bytes
[2] << 8) | (Bytes
[1] << 16) | (Bytes
[0] << 24);
1203 Insn
= (Bytes
[2] << 0) | (Bytes
[3] << 8) | (Bytes
[0] << 16) |
1206 Insn
= (Bytes
[0] << 0) | (Bytes
[1] << 8) | (Bytes
[2] << 16) |
1211 return MCDisassembler::Success
;
1214 DecodeStatus
MipsDisassembler::getInstruction(MCInst
&Instr
, uint64_t &Size
,
1215 ArrayRef
<uint8_t> Bytes
,
1217 raw_ostream
&VStream
,
1218 raw_ostream
&CStream
) const {
1220 DecodeStatus Result
;
1224 Result
= readInstruction16(Bytes
, Address
, Size
, Insn
, IsBigEndian
);
1225 if (Result
== MCDisassembler::Fail
)
1226 return MCDisassembler::Fail
;
1228 if (hasMips32r6()) {
1230 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1231 // Calling the auto-generated decoder function for microMIPS32R6
1232 // 16-bit instructions.
1233 Result
= decodeInstruction(DecoderTableMicroMipsR616
, Instr
, Insn
,
1234 Address
, this, STI
);
1235 if (Result
!= MCDisassembler::Fail
) {
1241 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1242 // Calling the auto-generated decoder function for microMIPS 16-bit
1244 Result
= decodeInstruction(DecoderTableMicroMips16
, Instr
, Insn
, Address
,
1246 if (Result
!= MCDisassembler::Fail
) {
1251 Result
= readInstruction32(Bytes
, Address
, Size
, Insn
, IsBigEndian
, true);
1252 if (Result
== MCDisassembler::Fail
)
1253 return MCDisassembler::Fail
;
1255 if (hasMips32r6()) {
1257 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1258 // Calling the auto-generated decoder function.
1259 Result
= decodeInstruction(DecoderTableMicroMipsR632
, Instr
, Insn
, Address
,
1261 if (Result
!= MCDisassembler::Fail
) {
1267 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1268 // Calling the auto-generated decoder function.
1269 Result
= decodeInstruction(DecoderTableMicroMips32
, Instr
, Insn
, Address
,
1271 if (Result
!= MCDisassembler::Fail
) {
1277 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1278 Result
= decodeInstruction(DecoderTableMicroMipsFP6432
, Instr
, Insn
,
1279 Address
, this, STI
);
1280 if (Result
!= MCDisassembler::Fail
) {
1286 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1287 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1288 // could form a valid instruction. The two bytes we rejected as an
1289 // instruction could have actually beeen an inline constant pool that is
1290 // unconditionally branched over.
1292 return MCDisassembler::Fail
;
1295 // Attempt to read the instruction so that we can attempt to decode it. If
1296 // the buffer is not 4 bytes long, let the higher level logic figure out
1297 // what to do with a size of zero and MCDisassembler::Fail.
1298 Result
= readInstruction32(Bytes
, Address
, Size
, Insn
, IsBigEndian
, false);
1299 if (Result
== MCDisassembler::Fail
)
1300 return MCDisassembler::Fail
;
1302 // The only instruction size for standard encoded MIPS.
1306 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1308 decodeInstruction(DecoderTableCOP3_32
, Instr
, Insn
, Address
, this, STI
);
1309 if (Result
!= MCDisassembler::Fail
)
1313 if (hasMips32r6() && isGP64()) {
1315 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1316 Result
= decodeInstruction(DecoderTableMips32r6_64r6_GP6432
, Instr
, Insn
,
1317 Address
, this, STI
);
1318 if (Result
!= MCDisassembler::Fail
)
1322 if (hasMips32r6() && isPTR64()) {
1324 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1325 Result
= decodeInstruction(DecoderTableMips32r6_64r6_PTR6432
, Instr
, Insn
,
1326 Address
, this, STI
);
1327 if (Result
!= MCDisassembler::Fail
)
1331 if (hasMips32r6()) {
1332 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1333 Result
= decodeInstruction(DecoderTableMips32r6_64r632
, Instr
, Insn
,
1334 Address
, this, STI
);
1335 if (Result
!= MCDisassembler::Fail
)
1339 if (hasMips2() && isPTR64()) {
1341 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1342 Result
= decodeInstruction(DecoderTableMips32_64_PTR6432
, Instr
, Insn
,
1343 Address
, this, STI
);
1344 if (Result
!= MCDisassembler::Fail
)
1349 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1350 Result
= decodeInstruction(DecoderTableCnMips32
, Instr
, Insn
,
1351 Address
, this, STI
);
1352 if (Result
!= MCDisassembler::Fail
)
1357 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1358 Result
= decodeInstruction(DecoderTableMips6432
, Instr
, Insn
,
1359 Address
, this, STI
);
1360 if (Result
!= MCDisassembler::Fail
)
1366 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1367 Result
= decodeInstruction(DecoderTableMipsFP6432
, Instr
, Insn
,
1368 Address
, this, STI
);
1369 if (Result
!= MCDisassembler::Fail
)
1373 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1374 // Calling the auto-generated decoder function.
1376 decodeInstruction(DecoderTableMips32
, Instr
, Insn
, Address
, this, STI
);
1377 if (Result
!= MCDisassembler::Fail
)
1380 return MCDisassembler::Fail
;
1383 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
1386 const void *Decoder
) {
1387 return MCDisassembler::Fail
;
1390 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
1393 const void *Decoder
) {
1395 return MCDisassembler::Fail
;
1397 unsigned Reg
= getReg(Decoder
, Mips::GPR64RegClassID
, RegNo
);
1398 Inst
.addOperand(MCOperand::createReg(Reg
));
1399 return MCDisassembler::Success
;
1402 static DecodeStatus
DecodeGPRMM16RegisterClass(MCInst
&Inst
,
1405 const void *Decoder
) {
1407 return MCDisassembler::Fail
;
1408 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16RegClassID
, RegNo
);
1409 Inst
.addOperand(MCOperand::createReg(Reg
));
1410 return MCDisassembler::Success
;
1413 static DecodeStatus
DecodeGPRMM16ZeroRegisterClass(MCInst
&Inst
,
1416 const void *Decoder
) {
1418 return MCDisassembler::Fail
;
1419 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16ZeroRegClassID
, RegNo
);
1420 Inst
.addOperand(MCOperand::createReg(Reg
));
1421 return MCDisassembler::Success
;
1424 static DecodeStatus
DecodeGPRMM16MovePRegisterClass(MCInst
&Inst
,
1427 const void *Decoder
) {
1429 return MCDisassembler::Fail
;
1430 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16MovePRegClassID
, RegNo
);
1431 Inst
.addOperand(MCOperand::createReg(Reg
));
1432 return MCDisassembler::Success
;
1435 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
1438 const void *Decoder
) {
1440 return MCDisassembler::Fail
;
1441 unsigned Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, RegNo
);
1442 Inst
.addOperand(MCOperand::createReg(Reg
));
1443 return MCDisassembler::Success
;
1446 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
1449 const void *Decoder
) {
1450 if (static_cast<const MipsDisassembler
*>(Decoder
)->isGP64())
1451 return DecodeGPR64RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1453 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1456 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
1459 const void *Decoder
) {
1460 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1463 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
1466 const void *Decoder
) {
1468 return MCDisassembler::Fail
;
1470 unsigned Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, RegNo
);
1471 Inst
.addOperand(MCOperand::createReg(Reg
));
1472 return MCDisassembler::Success
;
1475 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
1478 const void *Decoder
) {
1480 return MCDisassembler::Fail
;
1482 unsigned Reg
= getReg(Decoder
, Mips::FGR32RegClassID
, RegNo
);
1483 Inst
.addOperand(MCOperand::createReg(Reg
));
1484 return MCDisassembler::Success
;
1487 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
1490 const void *Decoder
) {
1492 return MCDisassembler::Fail
;
1493 unsigned Reg
= getReg(Decoder
, Mips::CCRRegClassID
, RegNo
);
1494 Inst
.addOperand(MCOperand::createReg(Reg
));
1495 return MCDisassembler::Success
;
1498 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
1501 const void *Decoder
) {
1503 return MCDisassembler::Fail
;
1504 unsigned Reg
= getReg(Decoder
, Mips::FCCRegClassID
, RegNo
);
1505 Inst
.addOperand(MCOperand::createReg(Reg
));
1506 return MCDisassembler::Success
;
1509 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
1511 const void *Decoder
) {
1513 return MCDisassembler::Fail
;
1515 unsigned Reg
= getReg(Decoder
, Mips::FGRCCRegClassID
, RegNo
);
1516 Inst
.addOperand(MCOperand::createReg(Reg
));
1517 return MCDisassembler::Success
;
1520 static DecodeStatus
DecodeMem(MCInst
&Inst
,
1523 const void *Decoder
) {
1524 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1525 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1526 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1528 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1529 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1531 if (Inst
.getOpcode() == Mips::SC
||
1532 Inst
.getOpcode() == Mips::SCD
)
1533 Inst
.addOperand(MCOperand::createReg(Reg
));
1535 Inst
.addOperand(MCOperand::createReg(Reg
));
1536 Inst
.addOperand(MCOperand::createReg(Base
));
1537 Inst
.addOperand(MCOperand::createImm(Offset
));
1539 return MCDisassembler::Success
;
1542 static DecodeStatus
DecodeMemEVA(MCInst
&Inst
,
1545 const void *Decoder
) {
1546 int Offset
= SignExtend32
<9>(Insn
>> 7);
1547 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1548 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1550 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1551 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1553 if (Inst
.getOpcode() == Mips::SCE
)
1554 Inst
.addOperand(MCOperand::createReg(Reg
));
1556 Inst
.addOperand(MCOperand::createReg(Reg
));
1557 Inst
.addOperand(MCOperand::createReg(Base
));
1558 Inst
.addOperand(MCOperand::createImm(Offset
));
1560 return MCDisassembler::Success
;
1563 static DecodeStatus
DecodeLoadByte15(MCInst
&Inst
,
1566 const void *Decoder
) {
1567 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1568 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1569 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1571 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1572 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1574 Inst
.addOperand(MCOperand::createReg(Reg
));
1575 Inst
.addOperand(MCOperand::createReg(Base
));
1576 Inst
.addOperand(MCOperand::createImm(Offset
));
1578 return MCDisassembler::Success
;
1581 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
,
1584 const void *Decoder
) {
1585 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1586 unsigned Hint
= fieldFromInstruction(Insn
, 16, 5);
1587 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1589 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1591 Inst
.addOperand(MCOperand::createReg(Base
));
1592 Inst
.addOperand(MCOperand::createImm(Offset
));
1593 Inst
.addOperand(MCOperand::createImm(Hint
));
1595 return MCDisassembler::Success
;
1598 static DecodeStatus
DecodeCacheOpMM(MCInst
&Inst
,
1601 const void *Decoder
) {
1602 int Offset
= SignExtend32
<12>(Insn
& 0xfff);
1603 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1604 unsigned Hint
= fieldFromInstruction(Insn
, 21, 5);
1606 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1608 Inst
.addOperand(MCOperand::createReg(Base
));
1609 Inst
.addOperand(MCOperand::createImm(Offset
));
1610 Inst
.addOperand(MCOperand::createImm(Hint
));
1612 return MCDisassembler::Success
;
1615 static DecodeStatus
DecodePrefeOpMM(MCInst
&Inst
,
1618 const void *Decoder
) {
1619 int Offset
= SignExtend32
<9>(Insn
& 0x1ff);
1620 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1621 unsigned Hint
= fieldFromInstruction(Insn
, 21, 5);
1623 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1625 Inst
.addOperand(MCOperand::createReg(Base
));
1626 Inst
.addOperand(MCOperand::createImm(Offset
));
1627 Inst
.addOperand(MCOperand::createImm(Hint
));
1629 return MCDisassembler::Success
;
1632 static DecodeStatus
DecodeCacheeOp_CacheOpR6(MCInst
&Inst
,
1635 const void *Decoder
) {
1636 int Offset
= SignExtend32
<9>(Insn
>> 7);
1637 unsigned Hint
= fieldFromInstruction(Insn
, 16, 5);
1638 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1640 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1642 Inst
.addOperand(MCOperand::createReg(Base
));
1643 Inst
.addOperand(MCOperand::createImm(Offset
));
1644 Inst
.addOperand(MCOperand::createImm(Hint
));
1646 return MCDisassembler::Success
;
1649 static DecodeStatus
DecodeSyncI(MCInst
&Inst
,
1652 const void *Decoder
) {
1653 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1654 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1656 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1658 Inst
.addOperand(MCOperand::createReg(Base
));
1659 Inst
.addOperand(MCOperand::createImm(Offset
));
1661 return MCDisassembler::Success
;
1664 static DecodeStatus
DecodeSyncI_MM(MCInst
&Inst
, unsigned Insn
,
1665 uint64_t Address
, const void *Decoder
) {
1666 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1667 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1669 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1671 Inst
.addOperand(MCOperand::createReg(Base
));
1672 Inst
.addOperand(MCOperand::createImm(Offset
));
1674 return MCDisassembler::Success
;
1677 static DecodeStatus
DecodeSynciR6(MCInst
&Inst
,
1680 const void *Decoder
) {
1681 int Immediate
= SignExtend32
<16>(Insn
& 0xffff);
1682 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1684 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1686 Inst
.addOperand(MCOperand::createReg(Base
));
1687 Inst
.addOperand(MCOperand::createImm(Immediate
));
1689 return MCDisassembler::Success
;
1692 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
1693 uint64_t Address
, const void *Decoder
) {
1694 int Offset
= SignExtend32
<10>(fieldFromInstruction(Insn
, 16, 10));
1695 unsigned Reg
= fieldFromInstruction(Insn
, 6, 5);
1696 unsigned Base
= fieldFromInstruction(Insn
, 11, 5);
1698 Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, Reg
);
1699 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1701 Inst
.addOperand(MCOperand::createReg(Reg
));
1702 Inst
.addOperand(MCOperand::createReg(Base
));
1704 // The immediate field of an LD/ST instruction is scaled which means it must
1705 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1711 switch(Inst
.getOpcode())
1714 assert(false && "Unexpected instruction");
1715 return MCDisassembler::Fail
;
1719 Inst
.addOperand(MCOperand::createImm(Offset
));
1723 Inst
.addOperand(MCOperand::createImm(Offset
* 2));
1727 Inst
.addOperand(MCOperand::createImm(Offset
* 4));
1731 Inst
.addOperand(MCOperand::createImm(Offset
* 8));
1735 return MCDisassembler::Success
;
1738 static DecodeStatus
DecodeMemMMImm4(MCInst
&Inst
,
1741 const void *Decoder
) {
1742 unsigned Offset
= Insn
& 0xf;
1743 unsigned Reg
= fieldFromInstruction(Insn
, 7, 3);
1744 unsigned Base
= fieldFromInstruction(Insn
, 4, 3);
1746 switch (Inst
.getOpcode()) {
1747 case Mips::LBU16_MM
:
1748 case Mips::LHU16_MM
:
1750 if (DecodeGPRMM16RegisterClass(Inst
, Reg
, Address
, Decoder
)
1751 == MCDisassembler::Fail
)
1752 return MCDisassembler::Fail
;
1755 case Mips::SB16_MMR6
:
1757 case Mips::SH16_MMR6
:
1759 case Mips::SW16_MMR6
:
1760 if (DecodeGPRMM16ZeroRegisterClass(Inst
, Reg
, Address
, Decoder
)
1761 == MCDisassembler::Fail
)
1762 return MCDisassembler::Fail
;
1766 if (DecodeGPRMM16RegisterClass(Inst
, Base
, Address
, Decoder
)
1767 == MCDisassembler::Fail
)
1768 return MCDisassembler::Fail
;
1770 switch (Inst
.getOpcode()) {
1771 case Mips::LBU16_MM
:
1773 Inst
.addOperand(MCOperand::createImm(-1));
1775 Inst
.addOperand(MCOperand::createImm(Offset
));
1778 case Mips::SB16_MMR6
:
1779 Inst
.addOperand(MCOperand::createImm(Offset
));
1781 case Mips::LHU16_MM
:
1783 case Mips::SH16_MMR6
:
1784 Inst
.addOperand(MCOperand::createImm(Offset
<< 1));
1788 case Mips::SW16_MMR6
:
1789 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1793 return MCDisassembler::Success
;
1796 static DecodeStatus
DecodeMemMMSPImm5Lsl2(MCInst
&Inst
,
1799 const void *Decoder
) {
1800 unsigned Offset
= Insn
& 0x1F;
1801 unsigned Reg
= fieldFromInstruction(Insn
, 5, 5);
1803 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1805 Inst
.addOperand(MCOperand::createReg(Reg
));
1806 Inst
.addOperand(MCOperand::createReg(Mips::SP
));
1807 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1809 return MCDisassembler::Success
;
1812 static DecodeStatus
DecodeMemMMGPImm7Lsl2(MCInst
&Inst
,
1815 const void *Decoder
) {
1816 unsigned Offset
= Insn
& 0x7F;
1817 unsigned Reg
= fieldFromInstruction(Insn
, 7, 3);
1819 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1821 Inst
.addOperand(MCOperand::createReg(Reg
));
1822 Inst
.addOperand(MCOperand::createReg(Mips::GP
));
1823 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1825 return MCDisassembler::Success
;
1828 static DecodeStatus
DecodeMemMMReglistImm4Lsl2(MCInst
&Inst
,
1831 const void *Decoder
) {
1833 switch (Inst
.getOpcode()) {
1834 case Mips::LWM16_MMR6
:
1835 case Mips::SWM16_MMR6
:
1836 Offset
= fieldFromInstruction(Insn
, 4, 4);
1839 Offset
= SignExtend32
<4>(Insn
& 0xf);
1843 if (DecodeRegListOperand16(Inst
, Insn
, Address
, Decoder
)
1844 == MCDisassembler::Fail
)
1845 return MCDisassembler::Fail
;
1847 Inst
.addOperand(MCOperand::createReg(Mips::SP
));
1848 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1850 return MCDisassembler::Success
;
1853 static DecodeStatus
DecodeMemMMImm9(MCInst
&Inst
,
1856 const void *Decoder
) {
1857 int Offset
= SignExtend32
<9>(Insn
& 0x1ff);
1858 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1859 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1861 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1862 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1864 if (Inst
.getOpcode() == Mips::SCE_MM
|| Inst
.getOpcode() == Mips::SC_MMR6
)
1865 Inst
.addOperand(MCOperand::createReg(Reg
));
1867 Inst
.addOperand(MCOperand::createReg(Reg
));
1868 Inst
.addOperand(MCOperand::createReg(Base
));
1869 Inst
.addOperand(MCOperand::createImm(Offset
));
1871 return MCDisassembler::Success
;
1874 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
1877 const void *Decoder
) {
1878 int Offset
= SignExtend32
<12>(Insn
& 0x0fff);
1879 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1880 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1882 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1883 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1885 switch (Inst
.getOpcode()) {
1886 case Mips::SWM32_MM
:
1887 case Mips::LWM32_MM
:
1888 if (DecodeRegListOperand(Inst
, Insn
, Address
, Decoder
)
1889 == MCDisassembler::Fail
)
1890 return MCDisassembler::Fail
;
1891 Inst
.addOperand(MCOperand::createReg(Base
));
1892 Inst
.addOperand(MCOperand::createImm(Offset
));
1895 Inst
.addOperand(MCOperand::createReg(Reg
));
1898 Inst
.addOperand(MCOperand::createReg(Reg
));
1899 if (Inst
.getOpcode() == Mips::LWP_MM
|| Inst
.getOpcode() == Mips::SWP_MM
)
1900 Inst
.addOperand(MCOperand::createReg(Reg
+1));
1902 Inst
.addOperand(MCOperand::createReg(Base
));
1903 Inst
.addOperand(MCOperand::createImm(Offset
));
1906 return MCDisassembler::Success
;
1909 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
1912 const void *Decoder
) {
1913 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1914 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1915 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1917 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1918 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1920 Inst
.addOperand(MCOperand::createReg(Reg
));
1921 Inst
.addOperand(MCOperand::createReg(Base
));
1922 Inst
.addOperand(MCOperand::createImm(Offset
));
1924 return MCDisassembler::Success
;
1927 static DecodeStatus
DecodeFMem(MCInst
&Inst
,
1930 const void *Decoder
) {
1931 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1932 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1933 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1935 Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, Reg
);
1936 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1938 Inst
.addOperand(MCOperand::createReg(Reg
));
1939 Inst
.addOperand(MCOperand::createReg(Base
));
1940 Inst
.addOperand(MCOperand::createImm(Offset
));
1942 return MCDisassembler::Success
;
1945 static DecodeStatus
DecodeFMemMMR2(MCInst
&Inst
, unsigned Insn
,
1946 uint64_t Address
, const void *Decoder
) {
1947 // This function is the same as DecodeFMem but with the Reg and Base fields
1948 // swapped according to microMIPS spec.
1949 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1950 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1951 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1953 Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, Reg
);
1954 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1956 Inst
.addOperand(MCOperand::createReg(Reg
));
1957 Inst
.addOperand(MCOperand::createReg(Base
));
1958 Inst
.addOperand(MCOperand::createImm(Offset
));
1960 return MCDisassembler::Success
;
1963 static DecodeStatus
DecodeFMem2(MCInst
&Inst
,
1966 const void *Decoder
) {
1967 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1968 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1969 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1971 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
1972 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1974 Inst
.addOperand(MCOperand::createReg(Reg
));
1975 Inst
.addOperand(MCOperand::createReg(Base
));
1976 Inst
.addOperand(MCOperand::createImm(Offset
));
1978 return MCDisassembler::Success
;
1981 static DecodeStatus
DecodeFMem3(MCInst
&Inst
,
1984 const void *Decoder
) {
1985 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1986 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1987 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1989 Reg
= getReg(Decoder
, Mips::COP3RegClassID
, Reg
);
1990 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1992 Inst
.addOperand(MCOperand::createReg(Reg
));
1993 Inst
.addOperand(MCOperand::createReg(Base
));
1994 Inst
.addOperand(MCOperand::createImm(Offset
));
1996 return MCDisassembler::Success
;
1999 static DecodeStatus
DecodeFMemCop2R6(MCInst
&Inst
,
2002 const void *Decoder
) {
2003 int Offset
= SignExtend32
<11>(Insn
& 0x07ff);
2004 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
2005 unsigned Base
= fieldFromInstruction(Insn
, 11, 5);
2007 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
2008 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2010 Inst
.addOperand(MCOperand::createReg(Reg
));
2011 Inst
.addOperand(MCOperand::createReg(Base
));
2012 Inst
.addOperand(MCOperand::createImm(Offset
));
2014 return MCDisassembler::Success
;
2017 static DecodeStatus
DecodeFMemCop2MMR6(MCInst
&Inst
, unsigned Insn
,
2018 uint64_t Address
, const void *Decoder
) {
2019 int Offset
= SignExtend32
<11>(Insn
& 0x07ff);
2020 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
2021 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
2023 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
2024 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2026 Inst
.addOperand(MCOperand::createReg(Reg
));
2027 Inst
.addOperand(MCOperand::createReg(Base
));
2028 Inst
.addOperand(MCOperand::createImm(Offset
));
2030 return MCDisassembler::Success
;
2033 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
2036 const void *Decoder
) {
2037 int64_t Offset
= SignExtend64
<9>((Insn
>> 7) & 0x1ff);
2038 unsigned Rt
= fieldFromInstruction(Insn
, 16, 5);
2039 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
2041 Rt
= getReg(Decoder
, Mips::GPR32RegClassID
, Rt
);
2042 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2044 if(Inst
.getOpcode() == Mips::SC_R6
|| Inst
.getOpcode() == Mips::SCD_R6
){
2045 Inst
.addOperand(MCOperand::createReg(Rt
));
2048 Inst
.addOperand(MCOperand::createReg(Rt
));
2049 Inst
.addOperand(MCOperand::createReg(Base
));
2050 Inst
.addOperand(MCOperand::createImm(Offset
));
2052 return MCDisassembler::Success
;
2055 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
2058 const void *Decoder
) {
2059 // Currently only hardware register 29 is supported.
2061 return MCDisassembler::Fail
;
2062 Inst
.addOperand(MCOperand::createReg(Mips::HWR29
));
2063 return MCDisassembler::Success
;
2066 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
2069 const void *Decoder
) {
2070 if (RegNo
> 30 || RegNo
%2)
2071 return MCDisassembler::Fail
;
2073 unsigned Reg
= getReg(Decoder
, Mips::AFGR64RegClassID
, RegNo
/2);
2074 Inst
.addOperand(MCOperand::createReg(Reg
));
2075 return MCDisassembler::Success
;
2078 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
2081 const void *Decoder
) {
2083 return MCDisassembler::Fail
;
2085 unsigned Reg
= getReg(Decoder
, Mips::ACC64DSPRegClassID
, RegNo
);
2086 Inst
.addOperand(MCOperand::createReg(Reg
));
2087 return MCDisassembler::Success
;
2090 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
2093 const void *Decoder
) {
2095 return MCDisassembler::Fail
;
2097 unsigned Reg
= getReg(Decoder
, Mips::HI32DSPRegClassID
, RegNo
);
2098 Inst
.addOperand(MCOperand::createReg(Reg
));
2099 return MCDisassembler::Success
;
2102 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
2105 const void *Decoder
) {
2107 return MCDisassembler::Fail
;
2109 unsigned Reg
= getReg(Decoder
, Mips::LO32DSPRegClassID
, RegNo
);
2110 Inst
.addOperand(MCOperand::createReg(Reg
));
2111 return MCDisassembler::Success
;
2114 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
2117 const void *Decoder
) {
2119 return MCDisassembler::Fail
;
2121 unsigned Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, RegNo
);
2122 Inst
.addOperand(MCOperand::createReg(Reg
));
2123 return MCDisassembler::Success
;
2126 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
2129 const void *Decoder
) {
2131 return MCDisassembler::Fail
;
2133 unsigned Reg
= getReg(Decoder
, Mips::MSA128HRegClassID
, RegNo
);
2134 Inst
.addOperand(MCOperand::createReg(Reg
));
2135 return MCDisassembler::Success
;
2138 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
2141 const void *Decoder
) {
2143 return MCDisassembler::Fail
;
2145 unsigned Reg
= getReg(Decoder
, Mips::MSA128WRegClassID
, RegNo
);
2146 Inst
.addOperand(MCOperand::createReg(Reg
));
2147 return MCDisassembler::Success
;
2150 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
2153 const void *Decoder
) {
2155 return MCDisassembler::Fail
;
2157 unsigned Reg
= getReg(Decoder
, Mips::MSA128DRegClassID
, RegNo
);
2158 Inst
.addOperand(MCOperand::createReg(Reg
));
2159 return MCDisassembler::Success
;
2162 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
2165 const void *Decoder
) {
2167 return MCDisassembler::Fail
;
2169 unsigned Reg
= getReg(Decoder
, Mips::MSACtrlRegClassID
, RegNo
);
2170 Inst
.addOperand(MCOperand::createReg(Reg
));
2171 return MCDisassembler::Success
;
2174 static DecodeStatus
DecodeCOP0RegisterClass(MCInst
&Inst
,
2177 const void *Decoder
) {
2179 return MCDisassembler::Fail
;
2181 unsigned Reg
= getReg(Decoder
, Mips::COP0RegClassID
, RegNo
);
2182 Inst
.addOperand(MCOperand::createReg(Reg
));
2183 return MCDisassembler::Success
;
2186 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
2189 const void *Decoder
) {
2191 return MCDisassembler::Fail
;
2193 unsigned Reg
= getReg(Decoder
, Mips::COP2RegClassID
, RegNo
);
2194 Inst
.addOperand(MCOperand::createReg(Reg
));
2195 return MCDisassembler::Success
;
2198 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
2201 const void *Decoder
) {
2202 int32_t BranchOffset
= (SignExtend32
<16>(Offset
) * 4) + 4;
2203 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2204 return MCDisassembler::Success
;
2207 static DecodeStatus
DecodeBranchTarget1SImm16(MCInst
&Inst
,
2210 const void *Decoder
) {
2211 int32_t BranchOffset
= (SignExtend32
<16>(Offset
) * 2);
2212 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2213 return MCDisassembler::Success
;
2216 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
2219 const void *Decoder
) {
2220 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 2;
2221 Inst
.addOperand(MCOperand::createImm(JumpOffset
));
2222 return MCDisassembler::Success
;
2225 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
2228 const void *Decoder
) {
2229 int32_t BranchOffset
= SignExtend32
<21>(Offset
) * 4 + 4;
2231 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2232 return MCDisassembler::Success
;
2235 static DecodeStatus
DecodeBranchTarget21MM(MCInst
&Inst
,
2238 const void *Decoder
) {
2239 int32_t BranchOffset
= SignExtend32
<21>(Offset
) * 4 + 4;
2241 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2242 return MCDisassembler::Success
;
2245 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
2248 const void *Decoder
) {
2249 int32_t BranchOffset
= SignExtend32
<26>(Offset
) * 4 + 4;
2251 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2252 return MCDisassembler::Success
;
2255 static DecodeStatus
DecodeBranchTarget7MM(MCInst
&Inst
,
2258 const void *Decoder
) {
2259 int32_t BranchOffset
= SignExtend32
<8>(Offset
<< 1);
2260 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2261 return MCDisassembler::Success
;
2264 static DecodeStatus
DecodeBranchTarget10MM(MCInst
&Inst
,
2267 const void *Decoder
) {
2268 int32_t BranchOffset
= SignExtend32
<11>(Offset
<< 1);
2269 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2270 return MCDisassembler::Success
;
2273 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
2276 const void *Decoder
) {
2277 int32_t BranchOffset
= SignExtend32
<16>(Offset
) * 2 + 4;
2278 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2279 return MCDisassembler::Success
;
2282 static DecodeStatus
DecodeBranchTarget26MM(MCInst
&Inst
,
2285 const void *Decoder
) {
2286 int32_t BranchOffset
= SignExtend32
<27>(Offset
<< 1);
2288 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2289 return MCDisassembler::Success
;
2292 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
2295 const void *Decoder
) {
2296 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 1;
2297 Inst
.addOperand(MCOperand::createImm(JumpOffset
));
2298 return MCDisassembler::Success
;
2301 static DecodeStatus
DecodeJumpTargetXMM(MCInst
&Inst
,
2304 const void *Decoder
) {
2305 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 2;
2306 Inst
.addOperand(MCOperand::createImm(JumpOffset
));
2307 return MCDisassembler::Success
;
2310 static DecodeStatus
DecodeAddiur2Simm7(MCInst
&Inst
,
2313 const void *Decoder
) {
2315 Inst
.addOperand(MCOperand::createImm(1));
2316 else if (Value
== 0x7)
2317 Inst
.addOperand(MCOperand::createImm(-1));
2319 Inst
.addOperand(MCOperand::createImm(Value
<< 2));
2320 return MCDisassembler::Success
;
2323 static DecodeStatus
DecodeLi16Imm(MCInst
&Inst
,
2326 const void *Decoder
) {
2328 Inst
.addOperand(MCOperand::createImm(-1));
2330 Inst
.addOperand(MCOperand::createImm(Value
));
2331 return MCDisassembler::Success
;
2334 static DecodeStatus
DecodePOOL16BEncodedField(MCInst
&Inst
,
2337 const void *Decoder
) {
2338 Inst
.addOperand(MCOperand::createImm(Value
== 0x0 ? 8 : Value
));
2339 return MCDisassembler::Success
;
2342 template <unsigned Bits
, int Offset
, int Scale
>
2343 static DecodeStatus
DecodeUImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
2345 const void *Decoder
) {
2346 Value
&= ((1 << Bits
) - 1);
2348 Inst
.addOperand(MCOperand::createImm(Value
+ Offset
));
2349 return MCDisassembler::Success
;
2352 template <unsigned Bits
, int Offset
, int ScaleBy
>
2353 static DecodeStatus
DecodeSImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
2355 const void *Decoder
) {
2356 int32_t Imm
= SignExtend32
<Bits
>(Value
) * ScaleBy
;
2357 Inst
.addOperand(MCOperand::createImm(Imm
+ Offset
));
2358 return MCDisassembler::Success
;
2361 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
2364 const void *Decoder
) {
2365 // First we need to grab the pos(lsb) from MCInst.
2366 // This function only handles the 32 bit variants of ins, as dins
2367 // variants are handled differently.
2368 int Pos
= Inst
.getOperand(2).getImm();
2369 int Size
= (int) Insn
- Pos
+ 1;
2370 Inst
.addOperand(MCOperand::createImm(SignExtend32
<16>(Size
)));
2371 return MCDisassembler::Success
;
2374 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
2375 uint64_t Address
, const void *Decoder
) {
2376 Inst
.addOperand(MCOperand::createImm(SignExtend32
<19>(Insn
) * 4));
2377 return MCDisassembler::Success
;
2380 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
2381 uint64_t Address
, const void *Decoder
) {
2382 Inst
.addOperand(MCOperand::createImm(SignExtend32
<18>(Insn
) * 8));
2383 return MCDisassembler::Success
;
2386 static DecodeStatus
DecodeSimm9SP(MCInst
&Inst
, unsigned Insn
,
2387 uint64_t Address
, const void *Decoder
) {
2388 int32_t DecodedValue
;
2390 case 0: DecodedValue
= 256; break;
2391 case 1: DecodedValue
= 257; break;
2392 case 510: DecodedValue
= -258; break;
2393 case 511: DecodedValue
= -257; break;
2394 default: DecodedValue
= SignExtend32
<9>(Insn
); break;
2396 Inst
.addOperand(MCOperand::createImm(DecodedValue
* 4));
2397 return MCDisassembler::Success
;
2400 static DecodeStatus
DecodeANDI16Imm(MCInst
&Inst
, unsigned Insn
,
2401 uint64_t Address
, const void *Decoder
) {
2402 // Insn must be >= 0, since it is unsigned that condition is always true.
2404 int32_t DecodedValues
[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2406 Inst
.addOperand(MCOperand::createImm(DecodedValues
[Insn
]));
2407 return MCDisassembler::Success
;
2410 static DecodeStatus
DecodeRegListOperand(MCInst
&Inst
,
2413 const void *Decoder
) {
2414 unsigned Regs
[] = {Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
, Mips::S4
, Mips::S5
,
2415 Mips::S6
, Mips::S7
, Mips::FP
};
2418 unsigned RegLst
= fieldFromInstruction(Insn
, 21, 5);
2420 // Empty register lists are not allowed.
2422 return MCDisassembler::Fail
;
2424 RegNum
= RegLst
& 0xf;
2426 // RegLst values 10-15, and 26-31 are reserved.
2428 return MCDisassembler::Fail
;
2430 for (unsigned i
= 0; i
< RegNum
; i
++)
2431 Inst
.addOperand(MCOperand::createReg(Regs
[i
]));
2434 Inst
.addOperand(MCOperand::createReg(Mips::RA
));
2436 return MCDisassembler::Success
;
2439 static DecodeStatus
DecodeRegListOperand16(MCInst
&Inst
, unsigned Insn
,
2441 const void *Decoder
) {
2442 unsigned Regs
[] = {Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
};
2444 switch(Inst
.getOpcode()) {
2446 RegLst
= fieldFromInstruction(Insn
, 4, 2);
2448 case Mips::LWM16_MMR6
:
2449 case Mips::SWM16_MMR6
:
2450 RegLst
= fieldFromInstruction(Insn
, 8, 2);
2453 unsigned RegNum
= RegLst
& 0x3;
2455 for (unsigned i
= 0; i
<= RegNum
; i
++)
2456 Inst
.addOperand(MCOperand::createReg(Regs
[i
]));
2458 Inst
.addOperand(MCOperand::createReg(Mips::RA
));
2460 return MCDisassembler::Success
;
2463 static DecodeStatus
DecodeMovePOperands(MCInst
&Inst
, unsigned Insn
,
2465 const void *Decoder
) {
2466 unsigned RegPair
= fieldFromInstruction(Insn
, 7, 3);
2467 if (DecodeMovePRegPair(Inst
, RegPair
, Address
, Decoder
) ==
2468 MCDisassembler::Fail
)
2469 return MCDisassembler::Fail
;
2472 if (static_cast<const MipsDisassembler
*>(Decoder
)->hasMips32r6())
2473 RegRs
= fieldFromInstruction(Insn
, 0, 2) |
2474 (fieldFromInstruction(Insn
, 3, 1) << 2);
2476 RegRs
= fieldFromInstruction(Insn
, 1, 3);
2477 if (DecodeGPRMM16MovePRegisterClass(Inst
, RegRs
, Address
, Decoder
) ==
2478 MCDisassembler::Fail
)
2479 return MCDisassembler::Fail
;
2481 unsigned RegRt
= fieldFromInstruction(Insn
, 4, 3);
2482 if (DecodeGPRMM16MovePRegisterClass(Inst
, RegRt
, Address
, Decoder
) ==
2483 MCDisassembler::Fail
)
2484 return MCDisassembler::Fail
;
2486 return MCDisassembler::Success
;
2489 static DecodeStatus
DecodeMovePRegPair(MCInst
&Inst
, unsigned RegPair
,
2490 uint64_t Address
, const void *Decoder
) {
2493 return MCDisassembler::Fail
;
2495 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2496 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2499 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2500 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2503 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2504 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2507 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2508 Inst
.addOperand(MCOperand::createReg(Mips::S5
));
2511 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2512 Inst
.addOperand(MCOperand::createReg(Mips::S6
));
2515 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2516 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2519 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2520 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2523 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2524 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2528 return MCDisassembler::Success
;
2531 static DecodeStatus
DecodeSimm23Lsl2(MCInst
&Inst
, unsigned Insn
,
2532 uint64_t Address
, const void *Decoder
) {
2533 Inst
.addOperand(MCOperand::createImm(SignExtend32
<25>(Insn
<< 2)));
2534 return MCDisassembler::Success
;
2537 template <typename InsnType
>
2538 static DecodeStatus
DecodeBgtzGroupBranchMMR6(MCInst
&MI
, InsnType insn
,
2540 const void *Decoder
) {
2542 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2543 // Invalid if rt == 0
2544 // BGTZALC_MMR6 if rs == 0 && rt != 0
2545 // BLTZALC_MMR6 if rs != 0 && rs == rt
2546 // BLTUC_MMR6 if rs != 0 && rs != rt
2548 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
2549 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
2555 return MCDisassembler::Fail
;
2557 MI
.setOpcode(Mips::BGTZALC_MMR6
);
2559 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2561 else if (Rs
== Rt
) {
2562 MI
.setOpcode(Mips::BLTZALC_MMR6
);
2564 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2567 MI
.setOpcode(Mips::BLTUC_MMR6
);
2570 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
2575 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rs
)));
2579 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rt
)));
2581 MI
.addOperand(MCOperand::createImm(Imm
));
2583 return MCDisassembler::Success
;
2586 template <typename InsnType
>
2587 static DecodeStatus
DecodeBlezGroupBranchMMR6(MCInst
&MI
, InsnType insn
,
2589 const void *Decoder
) {
2591 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2592 // Invalid if rt == 0
2593 // BLEZALC_MMR6 if rs == 0 && rt != 0
2594 // BGEZALC_MMR6 if rs == rt && rt != 0
2595 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2597 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
2598 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
2603 return MCDisassembler::Fail
;
2605 MI
.setOpcode(Mips::BLEZALC_MMR6
);
2606 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2608 else if (Rs
== Rt
) {
2609 MI
.setOpcode(Mips::BGEZALC_MMR6
);
2610 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2614 MI
.setOpcode(Mips::BGEUC_MMR6
);
2615 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
2620 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rs
)));
2622 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rt
)));
2624 MI
.addOperand(MCOperand::createImm(Imm
));
2626 return MCDisassembler::Success
;