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 "llvm/ADT/ArrayRef.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
18 #include "llvm/MC/MCFixedLenDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "mips-disassembler"
35 using DecodeStatus
= MCDisassembler::DecodeStatus
;
39 class MipsDisassembler
: public MCDisassembler
{
44 MipsDisassembler(const MCSubtargetInfo
&STI
, MCContext
&Ctx
, bool IsBigEndian
)
45 : MCDisassembler(STI
, Ctx
),
46 IsMicroMips(STI
.getFeatureBits()[Mips::FeatureMicroMips
]),
47 IsBigEndian(IsBigEndian
) {}
49 bool hasMips2() const { return STI
.getFeatureBits()[Mips::FeatureMips2
]; }
50 bool hasMips3() const { return STI
.getFeatureBits()[Mips::FeatureMips3
]; }
51 bool hasMips32() const { return STI
.getFeatureBits()[Mips::FeatureMips32
]; }
53 bool hasMips32r6() const {
54 return STI
.getFeatureBits()[Mips::FeatureMips32r6
];
57 bool isFP64() const { return STI
.getFeatureBits()[Mips::FeatureFP64Bit
]; }
59 bool isGP64() const { return STI
.getFeatureBits()[Mips::FeatureGP64Bit
]; }
61 bool isPTR64() const { return STI
.getFeatureBits()[Mips::FeaturePTR64Bit
]; }
63 bool hasCnMips() const { return STI
.getFeatureBits()[Mips::FeatureCnMips
]; }
65 bool hasCOP3() const {
66 // Only present in MIPS-I and MIPS-II
67 return !hasMips32() && !hasMips3();
70 DecodeStatus
getInstruction(MCInst
&Instr
, uint64_t &Size
,
71 ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
73 raw_ostream
&CStream
) const override
;
76 } // end anonymous namespace
78 // Forward declare these because the autogenerated code will reference them.
79 // Definitions are further down.
80 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
85 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
90 static DecodeStatus
DecodeGPRMM16RegisterClass(MCInst
&Inst
,
95 static DecodeStatus
DecodeGPRMM16ZeroRegisterClass(MCInst
&Inst
,
100 static DecodeStatus
DecodeGPRMM16MovePRegisterClass(MCInst
&Inst
,
103 const void *Decoder
);
105 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
108 const void *Decoder
);
110 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
113 const void *Decoder
);
115 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
118 const void *Decoder
);
120 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
123 const void *Decoder
);
125 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
128 const void *Decoder
);
130 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
133 const void *Decoder
);
135 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
138 const void *Decoder
);
140 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
142 const void *Decoder
);
144 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
147 const void *Decoder
);
149 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
152 const void *Decoder
);
154 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
157 const void *Decoder
);
159 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
162 const void *Decoder
);
164 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
167 const void *Decoder
);
169 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
172 const void *Decoder
);
174 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
177 const void *Decoder
);
179 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
182 const void *Decoder
);
184 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
187 const void *Decoder
);
189 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
192 const void *Decoder
);
194 static DecodeStatus
DecodeCOP0RegisterClass(MCInst
&Inst
,
197 const void *Decoder
);
199 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
202 const void *Decoder
);
204 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
207 const void *Decoder
);
209 static DecodeStatus
DecodeBranchTarget1SImm16(MCInst
&Inst
,
212 const void *Decoder
);
214 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
217 const void *Decoder
);
219 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
222 const void *Decoder
);
224 static DecodeStatus
DecodeBranchTarget21MM(MCInst
&Inst
,
227 const void *Decoder
);
229 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
232 const void *Decoder
);
234 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
235 // shifted left by 1 bit.
236 static DecodeStatus
DecodeBranchTarget7MM(MCInst
&Inst
,
239 const void *Decoder
);
241 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
242 // shifted left by 1 bit.
243 static DecodeStatus
DecodeBranchTarget10MM(MCInst
&Inst
,
246 const void *Decoder
);
248 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
249 // shifted left by 1 bit.
250 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
253 const void *Decoder
);
255 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
256 // shifted left by 1 bit.
257 static DecodeStatus
DecodeBranchTarget26MM(MCInst
&Inst
,
260 const void *Decoder
);
262 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
263 // shifted left by 1 bit.
264 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
267 const void *Decoder
);
269 static DecodeStatus
DecodeMem(MCInst
&Inst
,
272 const void *Decoder
);
274 static DecodeStatus
DecodeMemEVA(MCInst
&Inst
,
277 const void *Decoder
);
279 static DecodeStatus
DecodeLoadByte15(MCInst
&Inst
,
282 const void *Decoder
);
284 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
285 const void *Decoder
);
287 static DecodeStatus
DecodeCacheeOp_CacheOpR6(MCInst
&Inst
,
290 const void *Decoder
);
292 static DecodeStatus
DecodeCacheOpMM(MCInst
&Inst
,
295 const void *Decoder
);
297 static DecodeStatus
DecodePrefeOpMM(MCInst
&Inst
,
300 const void *Decoder
);
302 static DecodeStatus
DecodeSyncI(MCInst
&Inst
,
305 const void *Decoder
);
307 static DecodeStatus
DecodeSyncI_MM(MCInst
&Inst
,
310 const void *Decoder
);
312 static DecodeStatus
DecodeSynciR6(MCInst
&Inst
,
315 const void *Decoder
);
317 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
318 uint64_t Address
, const void *Decoder
);
320 static DecodeStatus
DecodeMemMMImm4(MCInst
&Inst
,
323 const void *Decoder
);
325 static DecodeStatus
DecodeMemMMSPImm5Lsl2(MCInst
&Inst
,
328 const void *Decoder
);
330 static DecodeStatus
DecodeMemMMGPImm7Lsl2(MCInst
&Inst
,
333 const void *Decoder
);
335 static DecodeStatus
DecodeMemMMReglistImm4Lsl2(MCInst
&Inst
,
338 const void *Decoder
);
340 static DecodeStatus
DecodeMemMMImm9(MCInst
&Inst
,
343 const void *Decoder
);
345 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
348 const void *Decoder
);
350 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
353 const void *Decoder
);
355 static DecodeStatus
DecodeFMem(MCInst
&Inst
, unsigned Insn
,
357 const void *Decoder
);
359 static DecodeStatus
DecodeFMemMMR2(MCInst
&Inst
, unsigned Insn
,
361 const void *Decoder
);
363 static DecodeStatus
DecodeFMem2(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
364 const void *Decoder
);
366 static DecodeStatus
DecodeFMem3(MCInst
&Inst
, unsigned Insn
, uint64_t Address
,
367 const void *Decoder
);
369 static DecodeStatus
DecodeFMemCop2R6(MCInst
&Inst
, unsigned Insn
,
370 uint64_t Address
, const void *Decoder
);
372 static DecodeStatus
DecodeFMemCop2MMR6(MCInst
&Inst
, unsigned Insn
,
374 const void *Decoder
);
376 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
379 const void *Decoder
);
381 static DecodeStatus
DecodeAddiur2Simm7(MCInst
&Inst
,
384 const void *Decoder
);
386 static DecodeStatus
DecodeLi16Imm(MCInst
&Inst
,
389 const void *Decoder
);
391 static DecodeStatus
DecodePOOL16BEncodedField(MCInst
&Inst
,
394 const void *Decoder
);
396 template <unsigned Bits
, int Offset
, int Scale
>
397 static DecodeStatus
DecodeUImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
399 const void *Decoder
);
401 template <unsigned Bits
, int Offset
>
402 static DecodeStatus
DecodeUImmWithOffset(MCInst
&Inst
, unsigned Value
,
404 const void *Decoder
) {
405 return DecodeUImmWithOffsetAndScale
<Bits
, Offset
, 1>(Inst
, Value
, Address
,
409 template <unsigned Bits
, int Offset
= 0, int ScaleBy
= 1>
410 static DecodeStatus
DecodeSImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
412 const void *Decoder
);
414 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
417 const void *Decoder
);
419 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
420 uint64_t Address
, const void *Decoder
);
422 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
423 uint64_t Address
, const void *Decoder
);
425 static DecodeStatus
DecodeSimm9SP(MCInst
&Inst
, unsigned Insn
,
426 uint64_t Address
, const void *Decoder
);
428 static DecodeStatus
DecodeANDI16Imm(MCInst
&Inst
, unsigned Insn
,
429 uint64_t Address
, const void *Decoder
);
431 static DecodeStatus
DecodeSimm23Lsl2(MCInst
&Inst
, unsigned Insn
,
432 uint64_t Address
, const void *Decoder
);
434 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
436 template <typename InsnType
>
437 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
438 const void *Decoder
);
440 template <typename InsnType
>
441 static DecodeStatus
DecodeDAHIDATIMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
442 const void *Decoder
);
444 template <typename InsnType
>
445 static DecodeStatus
DecodeDAHIDATI(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
>
458 DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
459 const void *Decoder
);
461 template <typename InsnType
>
463 DecodePOP35GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
464 const void *Decoder
);
466 template <typename InsnType
>
468 DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
469 const void *Decoder
);
471 template <typename InsnType
>
473 DecodePOP37GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
474 const void *Decoder
);
476 template <typename InsnType
>
478 DecodePOP65GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
479 const void *Decoder
);
481 template <typename InsnType
>
483 DecodePOP75GroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
484 const void *Decoder
);
486 template <typename InsnType
>
488 DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
489 const void *Decoder
);
491 template <typename InsnType
>
493 DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
494 const void *Decoder
);
496 template <typename InsnType
>
498 DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
499 const void *Decoder
);
501 template <typename InsnType
>
503 DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
, uint64_t Address
,
504 const void *Decoder
);
506 template <typename InsnType
>
508 DecodeBgtzGroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
509 const void *Decoder
);
511 template <typename InsnType
>
513 DecodeBlezGroupBranchMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
514 const void *Decoder
);
516 template <typename InsnType
>
517 static DecodeStatus
DecodeDINS(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
518 const void *Decoder
);
520 template <typename InsnType
>
521 static DecodeStatus
DecodeDEXT(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
522 const void *Decoder
);
524 template <typename InsnType
>
525 static DecodeStatus
DecodeCRC(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
526 const void *Decoder
);
528 static DecodeStatus
DecodeRegListOperand(MCInst
&Inst
, unsigned Insn
,
530 const void *Decoder
);
532 static DecodeStatus
DecodeRegListOperand16(MCInst
&Inst
, unsigned Insn
,
534 const void *Decoder
);
536 static DecodeStatus
DecodeMovePRegPair(MCInst
&Inst
, unsigned RegPair
,
538 const void *Decoder
);
540 static DecodeStatus
DecodeMovePOperands(MCInst
&Inst
, unsigned Insn
,
541 uint64_t Address
, const void *Decoder
);
545 Target
&getTheMipselTarget();
546 Target
&getTheMipsTarget();
547 Target
&getTheMips64Target();
548 Target
&getTheMips64elTarget();
550 } // end namespace llvm
552 static MCDisassembler
*createMipsDisassembler(
554 const MCSubtargetInfo
&STI
,
556 return new MipsDisassembler(STI
, Ctx
, true);
559 static MCDisassembler
*createMipselDisassembler(
561 const MCSubtargetInfo
&STI
,
563 return new MipsDisassembler(STI
, Ctx
, false);
566 extern "C" void LLVMInitializeMipsDisassembler() {
567 // Register the disassembler.
568 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
569 createMipsDisassembler
);
570 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
571 createMipselDisassembler
);
572 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
573 createMipsDisassembler
);
574 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
575 createMipselDisassembler
);
578 #include "MipsGenDisassemblerTables.inc"
580 static unsigned getReg(const void *D
, unsigned RC
, unsigned RegNo
) {
581 const MipsDisassembler
*Dis
= static_cast<const MipsDisassembler
*>(D
);
582 const MCRegisterInfo
*RegInfo
= Dis
->getContext().getRegisterInfo();
583 return *(RegInfo
->getRegClass(RC
).begin() + RegNo
);
586 template <typename InsnType
>
587 static DecodeStatus
DecodeINSVE_DF(MCInst
&MI
, InsnType insn
, uint64_t Address
,
588 const void *Decoder
) {
589 using DecodeFN
= DecodeStatus (*)(MCInst
&, unsigned, uint64_t, const void *);
591 // The size of the n field depends on the element size
592 // The register class also depends on this.
593 InsnType tmp
= fieldFromInstruction(insn
, 17, 5);
595 DecodeFN RegDecoder
= nullptr;
596 if ((tmp
& 0x18) == 0x00) { // INSVE_B
598 RegDecoder
= DecodeMSA128BRegisterClass
;
599 } else if ((tmp
& 0x1c) == 0x10) { // INSVE_H
601 RegDecoder
= DecodeMSA128HRegisterClass
;
602 } else if ((tmp
& 0x1e) == 0x18) { // INSVE_W
604 RegDecoder
= DecodeMSA128WRegisterClass
;
605 } else if ((tmp
& 0x1f) == 0x1c) { // INSVE_D
607 RegDecoder
= DecodeMSA128DRegisterClass
;
609 llvm_unreachable("Invalid encoding");
611 assert(NSize
!= 0 && RegDecoder
!= nullptr);
614 tmp
= fieldFromInstruction(insn
, 6, 5);
615 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
616 return MCDisassembler::Fail
;
618 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
619 return MCDisassembler::Fail
;
621 tmp
= fieldFromInstruction(insn
, 16, NSize
);
622 MI
.addOperand(MCOperand::createImm(tmp
));
624 tmp
= fieldFromInstruction(insn
, 11, 5);
625 if (RegDecoder(MI
, tmp
, Address
, Decoder
) == MCDisassembler::Fail
)
626 return MCDisassembler::Fail
;
628 MI
.addOperand(MCOperand::createImm(0));
630 return MCDisassembler::Success
;
633 template <typename InsnType
>
634 static DecodeStatus
DecodeDAHIDATIMMR6(MCInst
&MI
, InsnType insn
, uint64_t Address
,
635 const void *Decoder
) {
636 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
637 InsnType Imm
= fieldFromInstruction(insn
, 0, 16);
638 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
640 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
642 MI
.addOperand(MCOperand::createImm(Imm
));
644 return MCDisassembler::Success
;
647 template <typename InsnType
>
648 static DecodeStatus
DecodeDAHIDATI(MCInst
&MI
, InsnType insn
, uint64_t Address
,
649 const void *Decoder
) {
650 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
651 InsnType Imm
= fieldFromInstruction(insn
, 0, 16);
652 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
654 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
,
656 MI
.addOperand(MCOperand::createImm(Imm
));
658 return MCDisassembler::Success
;
661 template <typename InsnType
>
662 static DecodeStatus
DecodeAddiGroupBranch(MCInst
&MI
, InsnType insn
,
664 const void *Decoder
) {
665 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
666 // (otherwise we would have matched the ADDI instruction from the earlier
670 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
672 // BEQZALC if rs == 0 && rt != 0
673 // BEQC if rs < rt && rs != 0
675 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
676 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
677 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
681 MI
.setOpcode(Mips::BOVC
);
683 } else if (Rs
!= 0 && Rs
< Rt
) {
684 MI
.setOpcode(Mips::BEQC
);
687 MI
.setOpcode(Mips::BEQZALC
);
690 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
693 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
695 MI
.addOperand(MCOperand::createImm(Imm
));
697 return MCDisassembler::Success
;
700 template <typename InsnType
>
701 static DecodeStatus
DecodePOP35GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
703 const void *Decoder
) {
704 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
705 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
709 MI
.setOpcode(Mips::BOVC_MMR6
);
710 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
712 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
714 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
715 } else if (Rs
!= 0 && Rs
< Rt
) {
716 MI
.setOpcode(Mips::BEQC_MMR6
);
717 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
719 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
721 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
723 MI
.setOpcode(Mips::BEQZALC_MMR6
);
724 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
726 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
729 MI
.addOperand(MCOperand::createImm(Imm
));
731 return MCDisassembler::Success
;
734 template <typename InsnType
>
735 static DecodeStatus
DecodeDaddiGroupBranch(MCInst
&MI
, InsnType insn
,
737 const void *Decoder
) {
738 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
739 // (otherwise we would have matched the ADDI instruction from the earlier
743 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
745 // BNEZALC if rs == 0 && rt != 0
746 // BNEC if rs < rt && rs != 0
748 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
749 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
750 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
754 MI
.setOpcode(Mips::BNVC
);
756 } else if (Rs
!= 0 && Rs
< Rt
) {
757 MI
.setOpcode(Mips::BNEC
);
760 MI
.setOpcode(Mips::BNEZALC
);
763 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
766 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
768 MI
.addOperand(MCOperand::createImm(Imm
));
770 return MCDisassembler::Success
;
773 template <typename InsnType
>
774 static DecodeStatus
DecodePOP37GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
776 const void *Decoder
) {
777 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
778 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
782 MI
.setOpcode(Mips::BNVC_MMR6
);
783 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
785 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
787 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
788 } else if (Rs
!= 0 && Rs
< Rt
) {
789 MI
.setOpcode(Mips::BNEC_MMR6
);
790 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
792 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
794 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
796 MI
.setOpcode(Mips::BNEZALC_MMR6
);
797 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
799 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
802 MI
.addOperand(MCOperand::createImm(Imm
));
804 return MCDisassembler::Success
;
807 template <typename InsnType
>
808 static DecodeStatus
DecodePOP65GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
810 const void *Decoder
) {
812 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
813 // Invalid if rt == 0
814 // BGTZC_MMR6 if rs == 0 && rt != 0
815 // BLTZC_MMR6 if rs == rt && rt != 0
816 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
818 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
819 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
820 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
824 return MCDisassembler::Fail
;
826 MI
.setOpcode(Mips::BGTZC_MMR6
);
828 MI
.setOpcode(Mips::BLTZC_MMR6
);
830 MI
.setOpcode(Mips::BLTC_MMR6
);
835 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
838 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
841 MI
.addOperand(MCOperand::createImm(Imm
));
843 return MCDisassembler::Success
;
846 template <typename InsnType
>
847 static DecodeStatus
DecodePOP75GroupBranchMMR6(MCInst
&MI
, InsnType insn
,
849 const void *Decoder
) {
851 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
852 // Invalid if rt == 0
853 // BLEZC_MMR6 if rs == 0 && rt != 0
854 // BGEZC_MMR6 if rs == rt && rt != 0
855 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
857 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
858 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
859 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
863 return MCDisassembler::Fail
;
865 MI
.setOpcode(Mips::BLEZC_MMR6
);
867 MI
.setOpcode(Mips::BGEZC_MMR6
);
870 MI
.setOpcode(Mips::BGEC_MMR6
);
874 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
877 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
880 MI
.addOperand(MCOperand::createImm(Imm
));
882 return MCDisassembler::Success
;
885 template <typename InsnType
>
886 static DecodeStatus
DecodeBlezlGroupBranch(MCInst
&MI
, InsnType insn
,
888 const void *Decoder
) {
889 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
890 // (otherwise we would have matched the BLEZL instruction from the earlier
894 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
895 // Invalid if rs == 0
896 // BLEZC if rs == 0 && rt != 0
897 // BGEZC if rs == rt && rt != 0
898 // BGEC if rs != rt && rs != 0 && rt != 0
900 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
901 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
902 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
906 return MCDisassembler::Fail
;
908 MI
.setOpcode(Mips::BLEZC
);
910 MI
.setOpcode(Mips::BGEZC
);
913 MI
.setOpcode(Mips::BGEC
);
917 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
920 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
923 MI
.addOperand(MCOperand::createImm(Imm
));
925 return MCDisassembler::Success
;
928 template <typename InsnType
>
929 static DecodeStatus
DecodeBgtzlGroupBranch(MCInst
&MI
, InsnType insn
,
931 const void *Decoder
) {
932 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
933 // (otherwise we would have matched the BGTZL instruction from the earlier
937 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
938 // Invalid if rs == 0
939 // BGTZC if rs == 0 && rt != 0
940 // BLTZC if rs == rt && rt != 0
941 // BLTC if rs != rt && rs != 0 && rt != 0
945 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
946 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
947 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
950 return MCDisassembler::Fail
;
952 MI
.setOpcode(Mips::BGTZC
);
954 MI
.setOpcode(Mips::BLTZC
);
956 MI
.setOpcode(Mips::BLTC
);
961 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
964 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
967 MI
.addOperand(MCOperand::createImm(Imm
));
969 return MCDisassembler::Success
;
972 template <typename InsnType
>
973 static DecodeStatus
DecodeBgtzGroupBranch(MCInst
&MI
, InsnType insn
,
975 const void *Decoder
) {
976 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
977 // (otherwise we would have matched the BGTZ instruction from the earlier
981 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
983 // BGTZALC if rs == 0 && rt != 0
984 // BLTZALC if rs != 0 && rs == rt
985 // BLTUC if rs != 0 && rs != rt
987 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
988 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
989 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
994 MI
.setOpcode(Mips::BGTZ
);
996 } else if (Rs
== 0) {
997 MI
.setOpcode(Mips::BGTZALC
);
999 } else if (Rs
== Rt
) {
1000 MI
.setOpcode(Mips::BLTZALC
);
1003 MI
.setOpcode(Mips::BLTUC
);
1009 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1013 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1016 MI
.addOperand(MCOperand::createImm(Imm
));
1018 return MCDisassembler::Success
;
1021 template <typename InsnType
>
1022 static DecodeStatus
DecodeBlezGroupBranch(MCInst
&MI
, InsnType insn
,
1024 const void *Decoder
) {
1025 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1026 // (otherwise we would have matched the BLEZL instruction from the earlier
1030 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1031 // Invalid if rs == 0
1032 // BLEZALC if rs == 0 && rt != 0
1033 // BGEZALC if rs == rt && rt != 0
1034 // BGEUC if rs != rt && rs != 0 && rt != 0
1036 InsnType Rs
= fieldFromInstruction(insn
, 21, 5);
1037 InsnType Rt
= fieldFromInstruction(insn
, 16, 5);
1038 int64_t Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
1042 return MCDisassembler::Fail
;
1044 MI
.setOpcode(Mips::BLEZALC
);
1046 MI
.setOpcode(Mips::BGEZALC
);
1049 MI
.setOpcode(Mips::BGEUC
);
1053 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1055 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1058 MI
.addOperand(MCOperand::createImm(Imm
));
1060 return MCDisassembler::Success
;
1063 // Override the generated disassembler to produce DEXT all the time. This is
1064 // for feature / behaviour parity with binutils.
1065 template <typename InsnType
>
1066 static DecodeStatus
DecodeDEXT(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1067 const void *Decoder
) {
1068 unsigned Msbd
= fieldFromInstruction(Insn
, 11, 5);
1069 unsigned Lsb
= fieldFromInstruction(Insn
, 6, 5);
1073 switch (MI
.getOpcode()) {
1080 Size
= Msbd
+ 1 + 32;
1087 llvm_unreachable("Unknown DEXT instruction!");
1090 MI
.setOpcode(Mips::DEXT
);
1092 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1093 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1095 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rt
)));
1096 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rs
)));
1097 MI
.addOperand(MCOperand::createImm(Pos
));
1098 MI
.addOperand(MCOperand::createImm(Size
));
1100 return MCDisassembler::Success
;
1103 // Override the generated disassembler to produce DINS all the time. This is
1104 // for feature / behaviour parity with binutils.
1105 template <typename InsnType
>
1106 static DecodeStatus
DecodeDINS(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1107 const void *Decoder
) {
1108 unsigned Msbd
= fieldFromInstruction(Insn
, 11, 5);
1109 unsigned Lsb
= fieldFromInstruction(Insn
, 6, 5);
1113 switch (MI
.getOpcode()) {
1116 Size
= Msbd
+ 1 - Pos
;
1120 Size
= Msbd
+ 33 - Pos
;
1124 // mbsd = pos + size - 33
1125 // mbsd - pos + 33 = size
1126 Size
= Msbd
+ 33 - Pos
;
1129 llvm_unreachable("Unknown DINS instruction!");
1132 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1133 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1135 MI
.setOpcode(Mips::DINS
);
1136 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rt
)));
1137 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR64RegClassID
, Rs
)));
1138 MI
.addOperand(MCOperand::createImm(Pos
));
1139 MI
.addOperand(MCOperand::createImm(Size
));
1141 return MCDisassembler::Success
;
1144 // Auto-generated decoder wouldn't add the third operand for CRC32*.
1145 template <typename InsnType
>
1146 static DecodeStatus
DecodeCRC(MCInst
&MI
, InsnType Insn
, uint64_t Address
,
1147 const void *Decoder
) {
1148 InsnType Rs
= fieldFromInstruction(Insn
, 21, 5);
1149 InsnType Rt
= fieldFromInstruction(Insn
, 16, 5);
1150 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1152 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1154 MI
.addOperand(MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
,
1156 return MCDisassembler::Success
;
1159 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1160 /// according to the given endianness.
1161 static DecodeStatus
readInstruction16(ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1162 uint64_t &Size
, uint32_t &Insn
,
1164 // We want to read exactly 2 Bytes of data.
1165 if (Bytes
.size() < 2) {
1167 return MCDisassembler::Fail
;
1171 Insn
= (Bytes
[0] << 8) | Bytes
[1];
1173 Insn
= (Bytes
[1] << 8) | Bytes
[0];
1176 return MCDisassembler::Success
;
1179 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1180 /// according to the given endianness.
1181 static DecodeStatus
readInstruction32(ArrayRef
<uint8_t> Bytes
, uint64_t Address
,
1182 uint64_t &Size
, uint32_t &Insn
,
1183 bool IsBigEndian
, bool IsMicroMips
) {
1184 // We want to read exactly 4 Bytes of data.
1185 if (Bytes
.size() < 4) {
1187 return MCDisassembler::Fail
;
1190 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1191 // always precede the low 16 bits in the instruction stream (that is, they
1192 // are placed at lower addresses in the instruction stream).
1194 // microMIPS byte ordering:
1195 // Big-endian: 0 | 1 | 2 | 3
1196 // Little-endian: 1 | 0 | 3 | 2
1199 // Encoded as a big-endian 32-bit word in the stream.
1201 (Bytes
[3] << 0) | (Bytes
[2] << 8) | (Bytes
[1] << 16) | (Bytes
[0] << 24);
1204 Insn
= (Bytes
[2] << 0) | (Bytes
[3] << 8) | (Bytes
[0] << 16) |
1207 Insn
= (Bytes
[0] << 0) | (Bytes
[1] << 8) | (Bytes
[2] << 16) |
1212 return MCDisassembler::Success
;
1215 DecodeStatus
MipsDisassembler::getInstruction(MCInst
&Instr
, uint64_t &Size
,
1216 ArrayRef
<uint8_t> Bytes
,
1218 raw_ostream
&VStream
,
1219 raw_ostream
&CStream
) const {
1221 DecodeStatus Result
;
1225 Result
= readInstruction16(Bytes
, Address
, Size
, Insn
, IsBigEndian
);
1226 if (Result
== MCDisassembler::Fail
)
1227 return MCDisassembler::Fail
;
1229 if (hasMips32r6()) {
1231 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1232 // Calling the auto-generated decoder function for microMIPS32R6
1233 // 16-bit instructions.
1234 Result
= decodeInstruction(DecoderTableMicroMipsR616
, Instr
, Insn
,
1235 Address
, this, STI
);
1236 if (Result
!= MCDisassembler::Fail
) {
1242 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1243 // Calling the auto-generated decoder function for microMIPS 16-bit
1245 Result
= decodeInstruction(DecoderTableMicroMips16
, Instr
, Insn
, Address
,
1247 if (Result
!= MCDisassembler::Fail
) {
1252 Result
= readInstruction32(Bytes
, Address
, Size
, Insn
, IsBigEndian
, true);
1253 if (Result
== MCDisassembler::Fail
)
1254 return MCDisassembler::Fail
;
1256 if (hasMips32r6()) {
1258 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1259 // Calling the auto-generated decoder function.
1260 Result
= decodeInstruction(DecoderTableMicroMipsR632
, Instr
, Insn
, Address
,
1262 if (Result
!= MCDisassembler::Fail
) {
1268 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1269 // Calling the auto-generated decoder function.
1270 Result
= decodeInstruction(DecoderTableMicroMips32
, Instr
, Insn
, Address
,
1272 if (Result
!= MCDisassembler::Fail
) {
1278 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1279 Result
= decodeInstruction(DecoderTableMicroMipsFP6432
, Instr
, Insn
,
1280 Address
, this, STI
);
1281 if (Result
!= MCDisassembler::Fail
) {
1287 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1288 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1289 // could form a valid instruction. The two bytes we rejected as an
1290 // instruction could have actually beeen an inline constant pool that is
1291 // unconditionally branched over.
1293 return MCDisassembler::Fail
;
1296 // Attempt to read the instruction so that we can attempt to decode it. If
1297 // the buffer is not 4 bytes long, let the higher level logic figure out
1298 // what to do with a size of zero and MCDisassembler::Fail.
1299 Result
= readInstruction32(Bytes
, Address
, Size
, Insn
, IsBigEndian
, false);
1300 if (Result
== MCDisassembler::Fail
)
1301 return MCDisassembler::Fail
;
1303 // The only instruction size for standard encoded MIPS.
1307 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1309 decodeInstruction(DecoderTableCOP3_32
, Instr
, Insn
, Address
, this, STI
);
1310 if (Result
!= MCDisassembler::Fail
)
1314 if (hasMips32r6() && isGP64()) {
1316 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1317 Result
= decodeInstruction(DecoderTableMips32r6_64r6_GP6432
, Instr
, Insn
,
1318 Address
, this, STI
);
1319 if (Result
!= MCDisassembler::Fail
)
1323 if (hasMips32r6() && isPTR64()) {
1325 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1326 Result
= decodeInstruction(DecoderTableMips32r6_64r6_PTR6432
, Instr
, Insn
,
1327 Address
, this, STI
);
1328 if (Result
!= MCDisassembler::Fail
)
1332 if (hasMips32r6()) {
1333 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1334 Result
= decodeInstruction(DecoderTableMips32r6_64r632
, Instr
, Insn
,
1335 Address
, this, STI
);
1336 if (Result
!= MCDisassembler::Fail
)
1340 if (hasMips2() && isPTR64()) {
1342 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1343 Result
= decodeInstruction(DecoderTableMips32_64_PTR6432
, Instr
, Insn
,
1344 Address
, this, STI
);
1345 if (Result
!= MCDisassembler::Fail
)
1350 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1351 Result
= decodeInstruction(DecoderTableCnMips32
, Instr
, Insn
,
1352 Address
, this, STI
);
1353 if (Result
!= MCDisassembler::Fail
)
1358 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1359 Result
= decodeInstruction(DecoderTableMips6432
, Instr
, Insn
,
1360 Address
, this, STI
);
1361 if (Result
!= MCDisassembler::Fail
)
1367 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1368 Result
= decodeInstruction(DecoderTableMipsFP6432
, Instr
, Insn
,
1369 Address
, this, STI
);
1370 if (Result
!= MCDisassembler::Fail
)
1374 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1375 // Calling the auto-generated decoder function.
1377 decodeInstruction(DecoderTableMips32
, Instr
, Insn
, Address
, this, STI
);
1378 if (Result
!= MCDisassembler::Fail
)
1381 return MCDisassembler::Fail
;
1384 static DecodeStatus
DecodeCPU16RegsRegisterClass(MCInst
&Inst
,
1387 const void *Decoder
) {
1388 return MCDisassembler::Fail
;
1391 static DecodeStatus
DecodeGPR64RegisterClass(MCInst
&Inst
,
1394 const void *Decoder
) {
1396 return MCDisassembler::Fail
;
1398 unsigned Reg
= getReg(Decoder
, Mips::GPR64RegClassID
, RegNo
);
1399 Inst
.addOperand(MCOperand::createReg(Reg
));
1400 return MCDisassembler::Success
;
1403 static DecodeStatus
DecodeGPRMM16RegisterClass(MCInst
&Inst
,
1406 const void *Decoder
) {
1408 return MCDisassembler::Fail
;
1409 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16RegClassID
, RegNo
);
1410 Inst
.addOperand(MCOperand::createReg(Reg
));
1411 return MCDisassembler::Success
;
1414 static DecodeStatus
DecodeGPRMM16ZeroRegisterClass(MCInst
&Inst
,
1417 const void *Decoder
) {
1419 return MCDisassembler::Fail
;
1420 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16ZeroRegClassID
, RegNo
);
1421 Inst
.addOperand(MCOperand::createReg(Reg
));
1422 return MCDisassembler::Success
;
1425 static DecodeStatus
DecodeGPRMM16MovePRegisterClass(MCInst
&Inst
,
1428 const void *Decoder
) {
1430 return MCDisassembler::Fail
;
1431 unsigned Reg
= getReg(Decoder
, Mips::GPRMM16MovePRegClassID
, RegNo
);
1432 Inst
.addOperand(MCOperand::createReg(Reg
));
1433 return MCDisassembler::Success
;
1436 static DecodeStatus
DecodeGPR32RegisterClass(MCInst
&Inst
,
1439 const void *Decoder
) {
1441 return MCDisassembler::Fail
;
1442 unsigned Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, RegNo
);
1443 Inst
.addOperand(MCOperand::createReg(Reg
));
1444 return MCDisassembler::Success
;
1447 static DecodeStatus
DecodePtrRegisterClass(MCInst
&Inst
,
1450 const void *Decoder
) {
1451 if (static_cast<const MipsDisassembler
*>(Decoder
)->isGP64())
1452 return DecodeGPR64RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1454 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1457 static DecodeStatus
DecodeDSPRRegisterClass(MCInst
&Inst
,
1460 const void *Decoder
) {
1461 return DecodeGPR32RegisterClass(Inst
, RegNo
, Address
, Decoder
);
1464 static DecodeStatus
DecodeFGR64RegisterClass(MCInst
&Inst
,
1467 const void *Decoder
) {
1469 return MCDisassembler::Fail
;
1471 unsigned Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, RegNo
);
1472 Inst
.addOperand(MCOperand::createReg(Reg
));
1473 return MCDisassembler::Success
;
1476 static DecodeStatus
DecodeFGR32RegisterClass(MCInst
&Inst
,
1479 const void *Decoder
) {
1481 return MCDisassembler::Fail
;
1483 unsigned Reg
= getReg(Decoder
, Mips::FGR32RegClassID
, RegNo
);
1484 Inst
.addOperand(MCOperand::createReg(Reg
));
1485 return MCDisassembler::Success
;
1488 static DecodeStatus
DecodeCCRRegisterClass(MCInst
&Inst
,
1491 const void *Decoder
) {
1493 return MCDisassembler::Fail
;
1494 unsigned Reg
= getReg(Decoder
, Mips::CCRRegClassID
, RegNo
);
1495 Inst
.addOperand(MCOperand::createReg(Reg
));
1496 return MCDisassembler::Success
;
1499 static DecodeStatus
DecodeFCCRegisterClass(MCInst
&Inst
,
1502 const void *Decoder
) {
1504 return MCDisassembler::Fail
;
1505 unsigned Reg
= getReg(Decoder
, Mips::FCCRegClassID
, RegNo
);
1506 Inst
.addOperand(MCOperand::createReg(Reg
));
1507 return MCDisassembler::Success
;
1510 static DecodeStatus
DecodeFGRCCRegisterClass(MCInst
&Inst
, unsigned RegNo
,
1512 const void *Decoder
) {
1514 return MCDisassembler::Fail
;
1516 unsigned Reg
= getReg(Decoder
, Mips::FGRCCRegClassID
, RegNo
);
1517 Inst
.addOperand(MCOperand::createReg(Reg
));
1518 return MCDisassembler::Success
;
1521 static DecodeStatus
DecodeMem(MCInst
&Inst
,
1524 const void *Decoder
) {
1525 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1526 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1527 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1529 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1530 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1532 if (Inst
.getOpcode() == Mips::SC
||
1533 Inst
.getOpcode() == Mips::SCD
)
1534 Inst
.addOperand(MCOperand::createReg(Reg
));
1536 Inst
.addOperand(MCOperand::createReg(Reg
));
1537 Inst
.addOperand(MCOperand::createReg(Base
));
1538 Inst
.addOperand(MCOperand::createImm(Offset
));
1540 return MCDisassembler::Success
;
1543 static DecodeStatus
DecodeMemEVA(MCInst
&Inst
,
1546 const void *Decoder
) {
1547 int Offset
= SignExtend32
<9>(Insn
>> 7);
1548 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1549 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1551 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1552 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1554 if (Inst
.getOpcode() == Mips::SCE
)
1555 Inst
.addOperand(MCOperand::createReg(Reg
));
1557 Inst
.addOperand(MCOperand::createReg(Reg
));
1558 Inst
.addOperand(MCOperand::createReg(Base
));
1559 Inst
.addOperand(MCOperand::createImm(Offset
));
1561 return MCDisassembler::Success
;
1564 static DecodeStatus
DecodeLoadByte15(MCInst
&Inst
,
1567 const void *Decoder
) {
1568 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1569 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1570 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1572 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1573 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1575 Inst
.addOperand(MCOperand::createReg(Reg
));
1576 Inst
.addOperand(MCOperand::createReg(Base
));
1577 Inst
.addOperand(MCOperand::createImm(Offset
));
1579 return MCDisassembler::Success
;
1582 static DecodeStatus
DecodeCacheOp(MCInst
&Inst
,
1585 const void *Decoder
) {
1586 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1587 unsigned Hint
= fieldFromInstruction(Insn
, 16, 5);
1588 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1590 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1592 Inst
.addOperand(MCOperand::createReg(Base
));
1593 Inst
.addOperand(MCOperand::createImm(Offset
));
1594 Inst
.addOperand(MCOperand::createImm(Hint
));
1596 return MCDisassembler::Success
;
1599 static DecodeStatus
DecodeCacheOpMM(MCInst
&Inst
,
1602 const void *Decoder
) {
1603 int Offset
= SignExtend32
<12>(Insn
& 0xfff);
1604 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1605 unsigned Hint
= fieldFromInstruction(Insn
, 21, 5);
1607 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1609 Inst
.addOperand(MCOperand::createReg(Base
));
1610 Inst
.addOperand(MCOperand::createImm(Offset
));
1611 Inst
.addOperand(MCOperand::createImm(Hint
));
1613 return MCDisassembler::Success
;
1616 static DecodeStatus
DecodePrefeOpMM(MCInst
&Inst
,
1619 const void *Decoder
) {
1620 int Offset
= SignExtend32
<9>(Insn
& 0x1ff);
1621 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1622 unsigned Hint
= fieldFromInstruction(Insn
, 21, 5);
1624 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1626 Inst
.addOperand(MCOperand::createReg(Base
));
1627 Inst
.addOperand(MCOperand::createImm(Offset
));
1628 Inst
.addOperand(MCOperand::createImm(Hint
));
1630 return MCDisassembler::Success
;
1633 static DecodeStatus
DecodeCacheeOp_CacheOpR6(MCInst
&Inst
,
1636 const void *Decoder
) {
1637 int Offset
= SignExtend32
<9>(Insn
>> 7);
1638 unsigned Hint
= fieldFromInstruction(Insn
, 16, 5);
1639 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1641 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1643 Inst
.addOperand(MCOperand::createReg(Base
));
1644 Inst
.addOperand(MCOperand::createImm(Offset
));
1645 Inst
.addOperand(MCOperand::createImm(Hint
));
1647 return MCDisassembler::Success
;
1650 static DecodeStatus
DecodeSyncI(MCInst
&Inst
,
1653 const void *Decoder
) {
1654 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1655 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1657 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1659 Inst
.addOperand(MCOperand::createReg(Base
));
1660 Inst
.addOperand(MCOperand::createImm(Offset
));
1662 return MCDisassembler::Success
;
1665 static DecodeStatus
DecodeSyncI_MM(MCInst
&Inst
, unsigned Insn
,
1666 uint64_t Address
, const void *Decoder
) {
1667 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1668 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1670 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1672 Inst
.addOperand(MCOperand::createReg(Base
));
1673 Inst
.addOperand(MCOperand::createImm(Offset
));
1675 return MCDisassembler::Success
;
1678 static DecodeStatus
DecodeSynciR6(MCInst
&Inst
,
1681 const void *Decoder
) {
1682 int Immediate
= SignExtend32
<16>(Insn
& 0xffff);
1683 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1685 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1687 Inst
.addOperand(MCOperand::createReg(Base
));
1688 Inst
.addOperand(MCOperand::createImm(Immediate
));
1690 return MCDisassembler::Success
;
1693 static DecodeStatus
DecodeMSA128Mem(MCInst
&Inst
, unsigned Insn
,
1694 uint64_t Address
, const void *Decoder
) {
1695 int Offset
= SignExtend32
<10>(fieldFromInstruction(Insn
, 16, 10));
1696 unsigned Reg
= fieldFromInstruction(Insn
, 6, 5);
1697 unsigned Base
= fieldFromInstruction(Insn
, 11, 5);
1699 Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, Reg
);
1700 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1702 Inst
.addOperand(MCOperand::createReg(Reg
));
1703 Inst
.addOperand(MCOperand::createReg(Base
));
1705 // The immediate field of an LD/ST instruction is scaled which means it must
1706 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1712 switch(Inst
.getOpcode())
1715 assert(false && "Unexpected instruction");
1716 return MCDisassembler::Fail
;
1720 Inst
.addOperand(MCOperand::createImm(Offset
));
1724 Inst
.addOperand(MCOperand::createImm(Offset
* 2));
1728 Inst
.addOperand(MCOperand::createImm(Offset
* 4));
1732 Inst
.addOperand(MCOperand::createImm(Offset
* 8));
1736 return MCDisassembler::Success
;
1739 static DecodeStatus
DecodeMemMMImm4(MCInst
&Inst
,
1742 const void *Decoder
) {
1743 unsigned Offset
= Insn
& 0xf;
1744 unsigned Reg
= fieldFromInstruction(Insn
, 7, 3);
1745 unsigned Base
= fieldFromInstruction(Insn
, 4, 3);
1747 switch (Inst
.getOpcode()) {
1748 case Mips::LBU16_MM
:
1749 case Mips::LHU16_MM
:
1751 if (DecodeGPRMM16RegisterClass(Inst
, Reg
, Address
, Decoder
)
1752 == MCDisassembler::Fail
)
1753 return MCDisassembler::Fail
;
1756 case Mips::SB16_MMR6
:
1758 case Mips::SH16_MMR6
:
1760 case Mips::SW16_MMR6
:
1761 if (DecodeGPRMM16ZeroRegisterClass(Inst
, Reg
, Address
, Decoder
)
1762 == MCDisassembler::Fail
)
1763 return MCDisassembler::Fail
;
1767 if (DecodeGPRMM16RegisterClass(Inst
, Base
, Address
, Decoder
)
1768 == MCDisassembler::Fail
)
1769 return MCDisassembler::Fail
;
1771 switch (Inst
.getOpcode()) {
1772 case Mips::LBU16_MM
:
1774 Inst
.addOperand(MCOperand::createImm(-1));
1776 Inst
.addOperand(MCOperand::createImm(Offset
));
1779 case Mips::SB16_MMR6
:
1780 Inst
.addOperand(MCOperand::createImm(Offset
));
1782 case Mips::LHU16_MM
:
1784 case Mips::SH16_MMR6
:
1785 Inst
.addOperand(MCOperand::createImm(Offset
<< 1));
1789 case Mips::SW16_MMR6
:
1790 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1794 return MCDisassembler::Success
;
1797 static DecodeStatus
DecodeMemMMSPImm5Lsl2(MCInst
&Inst
,
1800 const void *Decoder
) {
1801 unsigned Offset
= Insn
& 0x1F;
1802 unsigned Reg
= fieldFromInstruction(Insn
, 5, 5);
1804 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1806 Inst
.addOperand(MCOperand::createReg(Reg
));
1807 Inst
.addOperand(MCOperand::createReg(Mips::SP
));
1808 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1810 return MCDisassembler::Success
;
1813 static DecodeStatus
DecodeMemMMGPImm7Lsl2(MCInst
&Inst
,
1816 const void *Decoder
) {
1817 unsigned Offset
= Insn
& 0x7F;
1818 unsigned Reg
= fieldFromInstruction(Insn
, 7, 3);
1820 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1822 Inst
.addOperand(MCOperand::createReg(Reg
));
1823 Inst
.addOperand(MCOperand::createReg(Mips::GP
));
1824 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1826 return MCDisassembler::Success
;
1829 static DecodeStatus
DecodeMemMMReglistImm4Lsl2(MCInst
&Inst
,
1832 const void *Decoder
) {
1834 switch (Inst
.getOpcode()) {
1835 case Mips::LWM16_MMR6
:
1836 case Mips::SWM16_MMR6
:
1837 Offset
= fieldFromInstruction(Insn
, 4, 4);
1840 Offset
= SignExtend32
<4>(Insn
& 0xf);
1844 if (DecodeRegListOperand16(Inst
, Insn
, Address
, Decoder
)
1845 == MCDisassembler::Fail
)
1846 return MCDisassembler::Fail
;
1848 Inst
.addOperand(MCOperand::createReg(Mips::SP
));
1849 Inst
.addOperand(MCOperand::createImm(Offset
<< 2));
1851 return MCDisassembler::Success
;
1854 static DecodeStatus
DecodeMemMMImm9(MCInst
&Inst
,
1857 const void *Decoder
) {
1858 int Offset
= SignExtend32
<9>(Insn
& 0x1ff);
1859 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1860 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1862 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1863 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1865 if (Inst
.getOpcode() == Mips::SCE_MM
|| Inst
.getOpcode() == Mips::SC_MMR6
)
1866 Inst
.addOperand(MCOperand::createReg(Reg
));
1868 Inst
.addOperand(MCOperand::createReg(Reg
));
1869 Inst
.addOperand(MCOperand::createReg(Base
));
1870 Inst
.addOperand(MCOperand::createImm(Offset
));
1872 return MCDisassembler::Success
;
1875 static DecodeStatus
DecodeMemMMImm12(MCInst
&Inst
,
1878 const void *Decoder
) {
1879 int Offset
= SignExtend32
<12>(Insn
& 0x0fff);
1880 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1881 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1883 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1884 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1886 switch (Inst
.getOpcode()) {
1887 case Mips::SWM32_MM
:
1888 case Mips::LWM32_MM
:
1889 if (DecodeRegListOperand(Inst
, Insn
, Address
, Decoder
)
1890 == MCDisassembler::Fail
)
1891 return MCDisassembler::Fail
;
1892 Inst
.addOperand(MCOperand::createReg(Base
));
1893 Inst
.addOperand(MCOperand::createImm(Offset
));
1896 Inst
.addOperand(MCOperand::createReg(Reg
));
1899 Inst
.addOperand(MCOperand::createReg(Reg
));
1900 if (Inst
.getOpcode() == Mips::LWP_MM
|| Inst
.getOpcode() == Mips::SWP_MM
)
1901 Inst
.addOperand(MCOperand::createReg(Reg
+1));
1903 Inst
.addOperand(MCOperand::createReg(Base
));
1904 Inst
.addOperand(MCOperand::createImm(Offset
));
1907 return MCDisassembler::Success
;
1910 static DecodeStatus
DecodeMemMMImm16(MCInst
&Inst
,
1913 const void *Decoder
) {
1914 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1915 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1916 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1918 Reg
= getReg(Decoder
, Mips::GPR32RegClassID
, Reg
);
1919 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1921 Inst
.addOperand(MCOperand::createReg(Reg
));
1922 Inst
.addOperand(MCOperand::createReg(Base
));
1923 Inst
.addOperand(MCOperand::createImm(Offset
));
1925 return MCDisassembler::Success
;
1928 static DecodeStatus
DecodeFMem(MCInst
&Inst
,
1931 const void *Decoder
) {
1932 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1933 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1934 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1936 Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, Reg
);
1937 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1939 Inst
.addOperand(MCOperand::createReg(Reg
));
1940 Inst
.addOperand(MCOperand::createReg(Base
));
1941 Inst
.addOperand(MCOperand::createImm(Offset
));
1943 return MCDisassembler::Success
;
1946 static DecodeStatus
DecodeFMemMMR2(MCInst
&Inst
, unsigned Insn
,
1947 uint64_t Address
, const void *Decoder
) {
1948 // This function is the same as DecodeFMem but with the Reg and Base fields
1949 // swapped according to microMIPS spec.
1950 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1951 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
1952 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
1954 Reg
= getReg(Decoder
, Mips::FGR64RegClassID
, Reg
);
1955 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1957 Inst
.addOperand(MCOperand::createReg(Reg
));
1958 Inst
.addOperand(MCOperand::createReg(Base
));
1959 Inst
.addOperand(MCOperand::createImm(Offset
));
1961 return MCDisassembler::Success
;
1964 static DecodeStatus
DecodeFMem2(MCInst
&Inst
,
1967 const void *Decoder
) {
1968 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1969 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1970 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1972 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
1973 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1975 Inst
.addOperand(MCOperand::createReg(Reg
));
1976 Inst
.addOperand(MCOperand::createReg(Base
));
1977 Inst
.addOperand(MCOperand::createImm(Offset
));
1979 return MCDisassembler::Success
;
1982 static DecodeStatus
DecodeFMem3(MCInst
&Inst
,
1985 const void *Decoder
) {
1986 int Offset
= SignExtend32
<16>(Insn
& 0xffff);
1987 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
1988 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
1990 Reg
= getReg(Decoder
, Mips::COP3RegClassID
, Reg
);
1991 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
1993 Inst
.addOperand(MCOperand::createReg(Reg
));
1994 Inst
.addOperand(MCOperand::createReg(Base
));
1995 Inst
.addOperand(MCOperand::createImm(Offset
));
1997 return MCDisassembler::Success
;
2000 static DecodeStatus
DecodeFMemCop2R6(MCInst
&Inst
,
2003 const void *Decoder
) {
2004 int Offset
= SignExtend32
<11>(Insn
& 0x07ff);
2005 unsigned Reg
= fieldFromInstruction(Insn
, 16, 5);
2006 unsigned Base
= fieldFromInstruction(Insn
, 11, 5);
2008 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
2009 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2011 Inst
.addOperand(MCOperand::createReg(Reg
));
2012 Inst
.addOperand(MCOperand::createReg(Base
));
2013 Inst
.addOperand(MCOperand::createImm(Offset
));
2015 return MCDisassembler::Success
;
2018 static DecodeStatus
DecodeFMemCop2MMR6(MCInst
&Inst
, unsigned Insn
,
2019 uint64_t Address
, const void *Decoder
) {
2020 int Offset
= SignExtend32
<11>(Insn
& 0x07ff);
2021 unsigned Reg
= fieldFromInstruction(Insn
, 21, 5);
2022 unsigned Base
= fieldFromInstruction(Insn
, 16, 5);
2024 Reg
= getReg(Decoder
, Mips::COP2RegClassID
, Reg
);
2025 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2027 Inst
.addOperand(MCOperand::createReg(Reg
));
2028 Inst
.addOperand(MCOperand::createReg(Base
));
2029 Inst
.addOperand(MCOperand::createImm(Offset
));
2031 return MCDisassembler::Success
;
2034 static DecodeStatus
DecodeSpecial3LlSc(MCInst
&Inst
,
2037 const void *Decoder
) {
2038 int64_t Offset
= SignExtend64
<9>((Insn
>> 7) & 0x1ff);
2039 unsigned Rt
= fieldFromInstruction(Insn
, 16, 5);
2040 unsigned Base
= fieldFromInstruction(Insn
, 21, 5);
2042 Rt
= getReg(Decoder
, Mips::GPR32RegClassID
, Rt
);
2043 Base
= getReg(Decoder
, Mips::GPR32RegClassID
, Base
);
2045 if(Inst
.getOpcode() == Mips::SC_R6
|| Inst
.getOpcode() == Mips::SCD_R6
){
2046 Inst
.addOperand(MCOperand::createReg(Rt
));
2049 Inst
.addOperand(MCOperand::createReg(Rt
));
2050 Inst
.addOperand(MCOperand::createReg(Base
));
2051 Inst
.addOperand(MCOperand::createImm(Offset
));
2053 return MCDisassembler::Success
;
2056 static DecodeStatus
DecodeHWRegsRegisterClass(MCInst
&Inst
,
2059 const void *Decoder
) {
2060 // Currently only hardware register 29 is supported.
2062 return MCDisassembler::Fail
;
2063 Inst
.addOperand(MCOperand::createReg(Mips::HWR29
));
2064 return MCDisassembler::Success
;
2067 static DecodeStatus
DecodeAFGR64RegisterClass(MCInst
&Inst
,
2070 const void *Decoder
) {
2071 if (RegNo
> 30 || RegNo
%2)
2072 return MCDisassembler::Fail
;
2074 unsigned Reg
= getReg(Decoder
, Mips::AFGR64RegClassID
, RegNo
/2);
2075 Inst
.addOperand(MCOperand::createReg(Reg
));
2076 return MCDisassembler::Success
;
2079 static DecodeStatus
DecodeACC64DSPRegisterClass(MCInst
&Inst
,
2082 const void *Decoder
) {
2084 return MCDisassembler::Fail
;
2086 unsigned Reg
= getReg(Decoder
, Mips::ACC64DSPRegClassID
, RegNo
);
2087 Inst
.addOperand(MCOperand::createReg(Reg
));
2088 return MCDisassembler::Success
;
2091 static DecodeStatus
DecodeHI32DSPRegisterClass(MCInst
&Inst
,
2094 const void *Decoder
) {
2096 return MCDisassembler::Fail
;
2098 unsigned Reg
= getReg(Decoder
, Mips::HI32DSPRegClassID
, RegNo
);
2099 Inst
.addOperand(MCOperand::createReg(Reg
));
2100 return MCDisassembler::Success
;
2103 static DecodeStatus
DecodeLO32DSPRegisterClass(MCInst
&Inst
,
2106 const void *Decoder
) {
2108 return MCDisassembler::Fail
;
2110 unsigned Reg
= getReg(Decoder
, Mips::LO32DSPRegClassID
, RegNo
);
2111 Inst
.addOperand(MCOperand::createReg(Reg
));
2112 return MCDisassembler::Success
;
2115 static DecodeStatus
DecodeMSA128BRegisterClass(MCInst
&Inst
,
2118 const void *Decoder
) {
2120 return MCDisassembler::Fail
;
2122 unsigned Reg
= getReg(Decoder
, Mips::MSA128BRegClassID
, RegNo
);
2123 Inst
.addOperand(MCOperand::createReg(Reg
));
2124 return MCDisassembler::Success
;
2127 static DecodeStatus
DecodeMSA128HRegisterClass(MCInst
&Inst
,
2130 const void *Decoder
) {
2132 return MCDisassembler::Fail
;
2134 unsigned Reg
= getReg(Decoder
, Mips::MSA128HRegClassID
, RegNo
);
2135 Inst
.addOperand(MCOperand::createReg(Reg
));
2136 return MCDisassembler::Success
;
2139 static DecodeStatus
DecodeMSA128WRegisterClass(MCInst
&Inst
,
2142 const void *Decoder
) {
2144 return MCDisassembler::Fail
;
2146 unsigned Reg
= getReg(Decoder
, Mips::MSA128WRegClassID
, RegNo
);
2147 Inst
.addOperand(MCOperand::createReg(Reg
));
2148 return MCDisassembler::Success
;
2151 static DecodeStatus
DecodeMSA128DRegisterClass(MCInst
&Inst
,
2154 const void *Decoder
) {
2156 return MCDisassembler::Fail
;
2158 unsigned Reg
= getReg(Decoder
, Mips::MSA128DRegClassID
, RegNo
);
2159 Inst
.addOperand(MCOperand::createReg(Reg
));
2160 return MCDisassembler::Success
;
2163 static DecodeStatus
DecodeMSACtrlRegisterClass(MCInst
&Inst
,
2166 const void *Decoder
) {
2168 return MCDisassembler::Fail
;
2170 unsigned Reg
= getReg(Decoder
, Mips::MSACtrlRegClassID
, RegNo
);
2171 Inst
.addOperand(MCOperand::createReg(Reg
));
2172 return MCDisassembler::Success
;
2175 static DecodeStatus
DecodeCOP0RegisterClass(MCInst
&Inst
,
2178 const void *Decoder
) {
2180 return MCDisassembler::Fail
;
2182 unsigned Reg
= getReg(Decoder
, Mips::COP0RegClassID
, RegNo
);
2183 Inst
.addOperand(MCOperand::createReg(Reg
));
2184 return MCDisassembler::Success
;
2187 static DecodeStatus
DecodeCOP2RegisterClass(MCInst
&Inst
,
2190 const void *Decoder
) {
2192 return MCDisassembler::Fail
;
2194 unsigned Reg
= getReg(Decoder
, Mips::COP2RegClassID
, RegNo
);
2195 Inst
.addOperand(MCOperand::createReg(Reg
));
2196 return MCDisassembler::Success
;
2199 static DecodeStatus
DecodeBranchTarget(MCInst
&Inst
,
2202 const void *Decoder
) {
2203 int32_t BranchOffset
= (SignExtend32
<16>(Offset
) * 4) + 4;
2204 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2205 return MCDisassembler::Success
;
2208 static DecodeStatus
DecodeBranchTarget1SImm16(MCInst
&Inst
,
2211 const void *Decoder
) {
2212 int32_t BranchOffset
= (SignExtend32
<16>(Offset
) * 2);
2213 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2214 return MCDisassembler::Success
;
2217 static DecodeStatus
DecodeJumpTarget(MCInst
&Inst
,
2220 const void *Decoder
) {
2221 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 2;
2222 Inst
.addOperand(MCOperand::createImm(JumpOffset
));
2223 return MCDisassembler::Success
;
2226 static DecodeStatus
DecodeBranchTarget21(MCInst
&Inst
,
2229 const void *Decoder
) {
2230 int32_t BranchOffset
= SignExtend32
<21>(Offset
) * 4 + 4;
2232 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2233 return MCDisassembler::Success
;
2236 static DecodeStatus
DecodeBranchTarget21MM(MCInst
&Inst
,
2239 const void *Decoder
) {
2240 int32_t BranchOffset
= SignExtend32
<21>(Offset
) * 4 + 4;
2242 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2243 return MCDisassembler::Success
;
2246 static DecodeStatus
DecodeBranchTarget26(MCInst
&Inst
,
2249 const void *Decoder
) {
2250 int32_t BranchOffset
= SignExtend32
<26>(Offset
) * 4 + 4;
2252 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2253 return MCDisassembler::Success
;
2256 static DecodeStatus
DecodeBranchTarget7MM(MCInst
&Inst
,
2259 const void *Decoder
) {
2260 int32_t BranchOffset
= SignExtend32
<8>(Offset
<< 1);
2261 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2262 return MCDisassembler::Success
;
2265 static DecodeStatus
DecodeBranchTarget10MM(MCInst
&Inst
,
2268 const void *Decoder
) {
2269 int32_t BranchOffset
= SignExtend32
<11>(Offset
<< 1);
2270 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2271 return MCDisassembler::Success
;
2274 static DecodeStatus
DecodeBranchTargetMM(MCInst
&Inst
,
2277 const void *Decoder
) {
2278 int32_t BranchOffset
= SignExtend32
<16>(Offset
) * 2 + 4;
2279 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2280 return MCDisassembler::Success
;
2283 static DecodeStatus
DecodeBranchTarget26MM(MCInst
&Inst
,
2286 const void *Decoder
) {
2287 int32_t BranchOffset
= SignExtend32
<27>(Offset
<< 1);
2289 Inst
.addOperand(MCOperand::createImm(BranchOffset
));
2290 return MCDisassembler::Success
;
2293 static DecodeStatus
DecodeJumpTargetMM(MCInst
&Inst
,
2296 const void *Decoder
) {
2297 unsigned JumpOffset
= fieldFromInstruction(Insn
, 0, 26) << 1;
2298 Inst
.addOperand(MCOperand::createImm(JumpOffset
));
2299 return MCDisassembler::Success
;
2302 static DecodeStatus
DecodeAddiur2Simm7(MCInst
&Inst
,
2305 const void *Decoder
) {
2307 Inst
.addOperand(MCOperand::createImm(1));
2308 else if (Value
== 0x7)
2309 Inst
.addOperand(MCOperand::createImm(-1));
2311 Inst
.addOperand(MCOperand::createImm(Value
<< 2));
2312 return MCDisassembler::Success
;
2315 static DecodeStatus
DecodeLi16Imm(MCInst
&Inst
,
2318 const void *Decoder
) {
2320 Inst
.addOperand(MCOperand::createImm(-1));
2322 Inst
.addOperand(MCOperand::createImm(Value
));
2323 return MCDisassembler::Success
;
2326 static DecodeStatus
DecodePOOL16BEncodedField(MCInst
&Inst
,
2329 const void *Decoder
) {
2330 Inst
.addOperand(MCOperand::createImm(Value
== 0x0 ? 8 : Value
));
2331 return MCDisassembler::Success
;
2334 template <unsigned Bits
, int Offset
, int Scale
>
2335 static DecodeStatus
DecodeUImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
2337 const void *Decoder
) {
2338 Value
&= ((1 << Bits
) - 1);
2340 Inst
.addOperand(MCOperand::createImm(Value
+ Offset
));
2341 return MCDisassembler::Success
;
2344 template <unsigned Bits
, int Offset
, int ScaleBy
>
2345 static DecodeStatus
DecodeSImmWithOffsetAndScale(MCInst
&Inst
, unsigned Value
,
2347 const void *Decoder
) {
2348 int32_t Imm
= SignExtend32
<Bits
>(Value
) * ScaleBy
;
2349 Inst
.addOperand(MCOperand::createImm(Imm
+ Offset
));
2350 return MCDisassembler::Success
;
2353 static DecodeStatus
DecodeInsSize(MCInst
&Inst
,
2356 const void *Decoder
) {
2357 // First we need to grab the pos(lsb) from MCInst.
2358 // This function only handles the 32 bit variants of ins, as dins
2359 // variants are handled differently.
2360 int Pos
= Inst
.getOperand(2).getImm();
2361 int Size
= (int) Insn
- Pos
+ 1;
2362 Inst
.addOperand(MCOperand::createImm(SignExtend32
<16>(Size
)));
2363 return MCDisassembler::Success
;
2366 static DecodeStatus
DecodeSimm19Lsl2(MCInst
&Inst
, unsigned Insn
,
2367 uint64_t Address
, const void *Decoder
) {
2368 Inst
.addOperand(MCOperand::createImm(SignExtend32
<19>(Insn
) * 4));
2369 return MCDisassembler::Success
;
2372 static DecodeStatus
DecodeSimm18Lsl3(MCInst
&Inst
, unsigned Insn
,
2373 uint64_t Address
, const void *Decoder
) {
2374 Inst
.addOperand(MCOperand::createImm(SignExtend32
<18>(Insn
) * 8));
2375 return MCDisassembler::Success
;
2378 static DecodeStatus
DecodeSimm9SP(MCInst
&Inst
, unsigned Insn
,
2379 uint64_t Address
, const void *Decoder
) {
2380 int32_t DecodedValue
;
2382 case 0: DecodedValue
= 256; break;
2383 case 1: DecodedValue
= 257; break;
2384 case 510: DecodedValue
= -258; break;
2385 case 511: DecodedValue
= -257; break;
2386 default: DecodedValue
= SignExtend32
<9>(Insn
); break;
2388 Inst
.addOperand(MCOperand::createImm(DecodedValue
* 4));
2389 return MCDisassembler::Success
;
2392 static DecodeStatus
DecodeANDI16Imm(MCInst
&Inst
, unsigned Insn
,
2393 uint64_t Address
, const void *Decoder
) {
2394 // Insn must be >= 0, since it is unsigned that condition is always true.
2396 int32_t DecodedValues
[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2398 Inst
.addOperand(MCOperand::createImm(DecodedValues
[Insn
]));
2399 return MCDisassembler::Success
;
2402 static DecodeStatus
DecodeRegListOperand(MCInst
&Inst
,
2405 const void *Decoder
) {
2406 unsigned Regs
[] = {Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
, Mips::S4
, Mips::S5
,
2407 Mips::S6
, Mips::S7
, Mips::FP
};
2410 unsigned RegLst
= fieldFromInstruction(Insn
, 21, 5);
2412 // Empty register lists are not allowed.
2414 return MCDisassembler::Fail
;
2416 RegNum
= RegLst
& 0xf;
2418 // RegLst values 10-15, and 26-31 are reserved.
2420 return MCDisassembler::Fail
;
2422 for (unsigned i
= 0; i
< RegNum
; i
++)
2423 Inst
.addOperand(MCOperand::createReg(Regs
[i
]));
2426 Inst
.addOperand(MCOperand::createReg(Mips::RA
));
2428 return MCDisassembler::Success
;
2431 static DecodeStatus
DecodeRegListOperand16(MCInst
&Inst
, unsigned Insn
,
2433 const void *Decoder
) {
2434 unsigned Regs
[] = {Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
};
2436 switch(Inst
.getOpcode()) {
2438 RegLst
= fieldFromInstruction(Insn
, 4, 2);
2440 case Mips::LWM16_MMR6
:
2441 case Mips::SWM16_MMR6
:
2442 RegLst
= fieldFromInstruction(Insn
, 8, 2);
2445 unsigned RegNum
= RegLst
& 0x3;
2447 for (unsigned i
= 0; i
<= RegNum
; i
++)
2448 Inst
.addOperand(MCOperand::createReg(Regs
[i
]));
2450 Inst
.addOperand(MCOperand::createReg(Mips::RA
));
2452 return MCDisassembler::Success
;
2455 static DecodeStatus
DecodeMovePOperands(MCInst
&Inst
, unsigned Insn
,
2457 const void *Decoder
) {
2458 unsigned RegPair
= fieldFromInstruction(Insn
, 7, 3);
2459 if (DecodeMovePRegPair(Inst
, RegPair
, Address
, Decoder
) ==
2460 MCDisassembler::Fail
)
2461 return MCDisassembler::Fail
;
2464 if (static_cast<const MipsDisassembler
*>(Decoder
)->hasMips32r6())
2465 RegRs
= fieldFromInstruction(Insn
, 0, 2) |
2466 (fieldFromInstruction(Insn
, 3, 1) << 2);
2468 RegRs
= fieldFromInstruction(Insn
, 1, 3);
2469 if (DecodeGPRMM16MovePRegisterClass(Inst
, RegRs
, Address
, Decoder
) ==
2470 MCDisassembler::Fail
)
2471 return MCDisassembler::Fail
;
2473 unsigned RegRt
= fieldFromInstruction(Insn
, 4, 3);
2474 if (DecodeGPRMM16MovePRegisterClass(Inst
, RegRt
, Address
, Decoder
) ==
2475 MCDisassembler::Fail
)
2476 return MCDisassembler::Fail
;
2478 return MCDisassembler::Success
;
2481 static DecodeStatus
DecodeMovePRegPair(MCInst
&Inst
, unsigned RegPair
,
2482 uint64_t Address
, const void *Decoder
) {
2485 return MCDisassembler::Fail
;
2487 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2488 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2491 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2492 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2495 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2496 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2499 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2500 Inst
.addOperand(MCOperand::createReg(Mips::S5
));
2503 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2504 Inst
.addOperand(MCOperand::createReg(Mips::S6
));
2507 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2508 Inst
.addOperand(MCOperand::createReg(Mips::A1
));
2511 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2512 Inst
.addOperand(MCOperand::createReg(Mips::A2
));
2515 Inst
.addOperand(MCOperand::createReg(Mips::A0
));
2516 Inst
.addOperand(MCOperand::createReg(Mips::A3
));
2520 return MCDisassembler::Success
;
2523 static DecodeStatus
DecodeSimm23Lsl2(MCInst
&Inst
, unsigned Insn
,
2524 uint64_t Address
, const void *Decoder
) {
2525 Inst
.addOperand(MCOperand::createImm(SignExtend32
<25>(Insn
<< 2)));
2526 return MCDisassembler::Success
;
2529 template <typename InsnType
>
2530 static DecodeStatus
DecodeBgtzGroupBranchMMR6(MCInst
&MI
, InsnType insn
,
2532 const void *Decoder
) {
2534 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2535 // Invalid if rt == 0
2536 // BGTZALC_MMR6 if rs == 0 && rt != 0
2537 // BLTZALC_MMR6 if rs != 0 && rs == rt
2538 // BLTUC_MMR6 if rs != 0 && rs != rt
2540 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
2541 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
2547 return MCDisassembler::Fail
;
2549 MI
.setOpcode(Mips::BGTZALC_MMR6
);
2551 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2553 else if (Rs
== Rt
) {
2554 MI
.setOpcode(Mips::BLTZALC_MMR6
);
2556 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2559 MI
.setOpcode(Mips::BLTUC_MMR6
);
2562 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
2567 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rs
)));
2571 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rt
)));
2573 MI
.addOperand(MCOperand::createImm(Imm
));
2575 return MCDisassembler::Success
;
2578 template <typename InsnType
>
2579 static DecodeStatus
DecodeBlezGroupBranchMMR6(MCInst
&MI
, InsnType insn
,
2581 const void *Decoder
) {
2583 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2584 // Invalid if rt == 0
2585 // BLEZALC_MMR6 if rs == 0 && rt != 0
2586 // BGEZALC_MMR6 if rs == rt && rt != 0
2587 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2589 InsnType Rt
= fieldFromInstruction(insn
, 21, 5);
2590 InsnType Rs
= fieldFromInstruction(insn
, 16, 5);
2595 return MCDisassembler::Fail
;
2597 MI
.setOpcode(Mips::BLEZALC_MMR6
);
2598 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2600 else if (Rs
== Rt
) {
2601 MI
.setOpcode(Mips::BGEZALC_MMR6
);
2602 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 2 + 4;
2606 MI
.setOpcode(Mips::BGEUC_MMR6
);
2607 Imm
= SignExtend64(fieldFromInstruction(insn
, 0, 16), 16) * 4 + 4;
2612 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rs
)));
2614 MCOperand::createReg(getReg(Decoder
, Mips::GPR32RegClassID
, Rt
)));
2616 MI
.addOperand(MCOperand::createImm(Imm
));
2618 return MCDisassembler::Success
;