1 //===- MipsDisassembler.cpp - Disassembler for Mips -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is part of the Mips Disassembler.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/MipsMCTargetDesc.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 static DecodeStatus DecodeMem(MCInst &Inst,
273 const void *Decoder);
275 static DecodeStatus DecodeMemEVA(MCInst &Inst,
278 const void *Decoder);
280 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
283 const void *Decoder);
285 static DecodeStatus DecodeCacheOp(MCInst &Inst, unsigned Insn, uint64_t Address,
286 const void *Decoder);
288 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
291 const void *Decoder);
293 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
296 const void *Decoder);
298 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
301 const void *Decoder);
303 static DecodeStatus DecodeSyncI(MCInst &Inst,
306 const void *Decoder);
308 static DecodeStatus DecodeSyncI_MM(MCInst &Inst,
311 const void *Decoder);
313 static DecodeStatus DecodeSynciR6(MCInst &Inst,
316 const void *Decoder);
318 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
319 uint64_t Address, const void *Decoder);
321 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
324 const void *Decoder);
326 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
329 const void *Decoder);
331 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
334 const void *Decoder);
336 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
339 const void *Decoder);
341 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
344 const void *Decoder);
346 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
349 const void *Decoder);
351 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
354 const void *Decoder);
356 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
358 const void *Decoder);
360 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
362 const void *Decoder);
364 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn, uint64_t Address,
365 const void *Decoder);
367 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn, uint64_t Address,
368 const void *Decoder);
370 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
371 uint64_t Address, const void *Decoder);
373 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
375 const void *Decoder);
377 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
380 const void *Decoder);
382 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
385 const void *Decoder);
387 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
390 const void *Decoder);
392 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
395 const void *Decoder);
397 template <unsigned Bits, int Offset, int Scale>
398 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
400 const void *Decoder);
402 template <unsigned Bits, int Offset>
403 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
405 const void *Decoder) {
406 return DecodeUImmWithOffsetAndScale<Bits, Offset, 1>(Inst, Value, Address,
410 template <unsigned Bits, int Offset = 0, int ScaleBy = 1>
411 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
413 const void *Decoder);
415 static DecodeStatus DecodeInsSize(MCInst &Inst,
418 const void *Decoder);
420 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
421 uint64_t Address, const void *Decoder);
423 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
424 uint64_t Address, const void *Decoder);
426 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
427 uint64_t Address, const void *Decoder);
429 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
430 uint64_t Address, const void *Decoder);
432 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
433 uint64_t Address, const void *Decoder);
435 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
437 template <typename InsnType>
438 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
439 const void *Decoder);
441 template <typename InsnType>
442 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
443 const void *Decoder);
445 template <typename InsnType>
446 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
447 const void *Decoder);
449 template <typename InsnType>
450 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
451 const void *Decoder);
453 template <typename InsnType>
454 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
455 const void *Decoder);
457 template <typename InsnType>
459 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
460 const void *Decoder);
462 template <typename InsnType>
464 DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
465 const void *Decoder);
467 template <typename InsnType>
469 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
470 const void *Decoder);
472 template <typename InsnType>
474 DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
475 const void *Decoder);
477 template <typename InsnType>
479 DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
480 const void *Decoder);
482 template <typename InsnType>
484 DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
485 const void *Decoder);
487 template <typename InsnType>
489 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
490 const void *Decoder);
492 template <typename InsnType>
494 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
495 const void *Decoder);
497 template <typename InsnType>
499 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
500 const void *Decoder);
502 template <typename InsnType>
504 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
505 const void *Decoder);
507 template <typename InsnType>
509 DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
510 const void *Decoder);
512 template <typename InsnType>
514 DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
515 const void *Decoder);
517 template <typename InsnType>
518 static DecodeStatus DecodeDINS(MCInst &MI, InsnType Insn, uint64_t Address,
519 const void *Decoder);
521 template <typename InsnType>
522 static DecodeStatus DecodeDEXT(MCInst &MI, InsnType Insn, uint64_t Address,
523 const void *Decoder);
525 template <typename InsnType>
526 static DecodeStatus DecodeCRC(MCInst &MI, InsnType Insn, uint64_t Address,
527 const void *Decoder);
529 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
531 const void *Decoder);
533 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
535 const void *Decoder);
537 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned RegPair,
539 const void *Decoder);
543 Target &getTheMipselTarget();
544 Target &getTheMipsTarget();
545 Target &getTheMips64Target();
546 Target &getTheMips64elTarget();
548 } // end namespace llvm
550 static MCDisassembler *createMipsDisassembler(
552 const MCSubtargetInfo &STI,
554 return new MipsDisassembler(STI, Ctx, true);
557 static MCDisassembler *createMipselDisassembler(
559 const MCSubtargetInfo &STI,
561 return new MipsDisassembler(STI, Ctx, false);
564 extern "C" void LLVMInitializeMipsDisassembler() {
565 // Register the disassembler.
566 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
567 createMipsDisassembler);
568 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
569 createMipselDisassembler);
570 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
571 createMipsDisassembler);
572 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
573 createMipselDisassembler);
576 #include "MipsGenDisassemblerTables.inc"
578 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
579 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
580 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
581 return *(RegInfo->getRegClass(RC).begin() + RegNo);
584 template <typename InsnType>
585 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
586 const void *Decoder) {
587 using DecodeFN = DecodeStatus (*)(MCInst &, unsigned, uint64_t, const void *);
589 // The size of the n field depends on the element size
590 // The register class also depends on this.
591 InsnType tmp = fieldFromInstruction(insn, 17, 5);
593 DecodeFN RegDecoder = nullptr;
594 if ((tmp & 0x18) == 0x00) { // INSVE_B
596 RegDecoder = DecodeMSA128BRegisterClass;
597 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
599 RegDecoder = DecodeMSA128HRegisterClass;
600 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
602 RegDecoder = DecodeMSA128WRegisterClass;
603 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
605 RegDecoder = DecodeMSA128DRegisterClass;
607 llvm_unreachable("Invalid encoding");
609 assert(NSize != 0 && RegDecoder != nullptr);
612 tmp = fieldFromInstruction(insn, 6, 5);
613 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
614 return MCDisassembler::Fail;
616 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
617 return MCDisassembler::Fail;
619 tmp = fieldFromInstruction(insn, 16, NSize);
620 MI.addOperand(MCOperand::createImm(tmp));
622 tmp = fieldFromInstruction(insn, 11, 5);
623 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
624 return MCDisassembler::Fail;
626 MI.addOperand(MCOperand::createImm(0));
628 return MCDisassembler::Success;
631 template <typename InsnType>
632 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
633 const void *Decoder) {
634 InsnType Rs = fieldFromInstruction(insn, 16, 5);
635 InsnType Imm = fieldFromInstruction(insn, 0, 16);
636 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
638 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
640 MI.addOperand(MCOperand::createImm(Imm));
642 return MCDisassembler::Success;
645 template <typename InsnType>
646 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
647 const void *Decoder) {
648 InsnType Rs = fieldFromInstruction(insn, 21, 5);
649 InsnType Imm = fieldFromInstruction(insn, 0, 16);
650 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
652 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
654 MI.addOperand(MCOperand::createImm(Imm));
656 return MCDisassembler::Success;
659 template <typename InsnType>
660 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
662 const void *Decoder) {
663 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
664 // (otherwise we would have matched the ADDI instruction from the earlier
668 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
670 // BEQZALC if rs == 0 && rt != 0
671 // BEQC if rs < rt && rs != 0
673 InsnType Rs = fieldFromInstruction(insn, 21, 5);
674 InsnType Rt = fieldFromInstruction(insn, 16, 5);
675 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
679 MI.setOpcode(Mips::BOVC);
681 } else if (Rs != 0 && Rs < Rt) {
682 MI.setOpcode(Mips::BEQC);
685 MI.setOpcode(Mips::BEQZALC);
688 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
691 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
693 MI.addOperand(MCOperand::createImm(Imm));
695 return MCDisassembler::Success;
698 template <typename InsnType>
699 static DecodeStatus DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn,
701 const void *Decoder) {
702 InsnType Rt = fieldFromInstruction(insn, 21, 5);
703 InsnType Rs = fieldFromInstruction(insn, 16, 5);
707 MI.setOpcode(Mips::BOVC_MMR6);
708 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
710 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
712 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
713 } else if (Rs != 0 && Rs < Rt) {
714 MI.setOpcode(Mips::BEQC_MMR6);
715 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
717 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
719 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
721 MI.setOpcode(Mips::BEQZALC_MMR6);
722 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
724 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
727 MI.addOperand(MCOperand::createImm(Imm));
729 return MCDisassembler::Success;
732 template <typename InsnType>
733 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
735 const void *Decoder) {
736 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
737 // (otherwise we would have matched the ADDI instruction from the earlier
741 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
743 // BNEZALC if rs == 0 && rt != 0
744 // BNEC if rs < rt && rs != 0
746 InsnType Rs = fieldFromInstruction(insn, 21, 5);
747 InsnType Rt = fieldFromInstruction(insn, 16, 5);
748 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
752 MI.setOpcode(Mips::BNVC);
754 } else if (Rs != 0 && Rs < Rt) {
755 MI.setOpcode(Mips::BNEC);
758 MI.setOpcode(Mips::BNEZALC);
761 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
764 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
766 MI.addOperand(MCOperand::createImm(Imm));
768 return MCDisassembler::Success;
771 template <typename InsnType>
772 static DecodeStatus DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn,
774 const void *Decoder) {
775 InsnType Rt = fieldFromInstruction(insn, 21, 5);
776 InsnType Rs = fieldFromInstruction(insn, 16, 5);
780 MI.setOpcode(Mips::BNVC_MMR6);
781 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
783 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
785 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
786 } else if (Rs != 0 && Rs < Rt) {
787 MI.setOpcode(Mips::BNEC_MMR6);
788 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
790 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
792 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
794 MI.setOpcode(Mips::BNEZALC_MMR6);
795 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
797 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
800 MI.addOperand(MCOperand::createImm(Imm));
802 return MCDisassembler::Success;
805 template <typename InsnType>
806 static DecodeStatus DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn,
808 const void *Decoder) {
810 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
811 // Invalid if rt == 0
812 // BGTZC_MMR6 if rs == 0 && rt != 0
813 // BLTZC_MMR6 if rs == rt && rt != 0
814 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
816 InsnType Rt = fieldFromInstruction(insn, 21, 5);
817 InsnType Rs = fieldFromInstruction(insn, 16, 5);
818 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
822 return MCDisassembler::Fail;
824 MI.setOpcode(Mips::BGTZC_MMR6);
826 MI.setOpcode(Mips::BLTZC_MMR6);
828 MI.setOpcode(Mips::BLTC_MMR6);
833 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
836 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
839 MI.addOperand(MCOperand::createImm(Imm));
841 return MCDisassembler::Success;
844 template <typename InsnType>
845 static DecodeStatus DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn,
847 const void *Decoder) {
849 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
850 // Invalid if rt == 0
851 // BLEZC_MMR6 if rs == 0 && rt != 0
852 // BGEZC_MMR6 if rs == rt && rt != 0
853 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
855 InsnType Rt = fieldFromInstruction(insn, 21, 5);
856 InsnType Rs = fieldFromInstruction(insn, 16, 5);
857 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
861 return MCDisassembler::Fail;
863 MI.setOpcode(Mips::BLEZC_MMR6);
865 MI.setOpcode(Mips::BGEZC_MMR6);
868 MI.setOpcode(Mips::BGEC_MMR6);
872 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
875 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
878 MI.addOperand(MCOperand::createImm(Imm));
880 return MCDisassembler::Success;
883 template <typename InsnType>
884 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
886 const void *Decoder) {
887 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
888 // (otherwise we would have matched the BLEZL instruction from the earlier
892 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
893 // Invalid if rs == 0
894 // BLEZC if rs == 0 && rt != 0
895 // BGEZC if rs == rt && rt != 0
896 // BGEC if rs != rt && rs != 0 && rt != 0
898 InsnType Rs = fieldFromInstruction(insn, 21, 5);
899 InsnType Rt = fieldFromInstruction(insn, 16, 5);
900 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
904 return MCDisassembler::Fail;
906 MI.setOpcode(Mips::BLEZC);
908 MI.setOpcode(Mips::BGEZC);
911 MI.setOpcode(Mips::BGEC);
915 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
918 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
921 MI.addOperand(MCOperand::createImm(Imm));
923 return MCDisassembler::Success;
926 template <typename InsnType>
927 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
929 const void *Decoder) {
930 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
931 // (otherwise we would have matched the BGTZL instruction from the earlier
935 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
936 // Invalid if rs == 0
937 // BGTZC if rs == 0 && rt != 0
938 // BLTZC if rs == rt && rt != 0
939 // BLTC if rs != rt && rs != 0 && rt != 0
943 InsnType Rs = fieldFromInstruction(insn, 21, 5);
944 InsnType Rt = fieldFromInstruction(insn, 16, 5);
945 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
948 return MCDisassembler::Fail;
950 MI.setOpcode(Mips::BGTZC);
952 MI.setOpcode(Mips::BLTZC);
954 MI.setOpcode(Mips::BLTC);
959 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
962 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
965 MI.addOperand(MCOperand::createImm(Imm));
967 return MCDisassembler::Success;
970 template <typename InsnType>
971 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
973 const void *Decoder) {
974 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
975 // (otherwise we would have matched the BGTZ instruction from the earlier
979 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
981 // BGTZALC if rs == 0 && rt != 0
982 // BLTZALC if rs != 0 && rs == rt
983 // BLTUC if rs != 0 && rs != rt
985 InsnType Rs = fieldFromInstruction(insn, 21, 5);
986 InsnType Rt = fieldFromInstruction(insn, 16, 5);
987 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
992 MI.setOpcode(Mips::BGTZ);
994 } else if (Rs == 0) {
995 MI.setOpcode(Mips::BGTZALC);
997 } else if (Rs == Rt) {
998 MI.setOpcode(Mips::BLTZALC);
1001 MI.setOpcode(Mips::BLTUC);
1007 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1011 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1014 MI.addOperand(MCOperand::createImm(Imm));
1016 return MCDisassembler::Success;
1019 template <typename InsnType>
1020 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
1022 const void *Decoder) {
1023 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1024 // (otherwise we would have matched the BLEZL instruction from the earlier
1028 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1029 // Invalid if rs == 0
1030 // BLEZALC if rs == 0 && rt != 0
1031 // BGEZALC if rs == rt && rt != 0
1032 // BGEUC if rs != rt && rs != 0 && rt != 0
1034 InsnType Rs = fieldFromInstruction(insn, 21, 5);
1035 InsnType Rt = fieldFromInstruction(insn, 16, 5);
1036 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1040 return MCDisassembler::Fail;
1042 MI.setOpcode(Mips::BLEZALC);
1044 MI.setOpcode(Mips::BGEZALC);
1047 MI.setOpcode(Mips::BGEUC);
1051 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1053 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1056 MI.addOperand(MCOperand::createImm(Imm));
1058 return MCDisassembler::Success;
1061 // Override the generated disassembler to produce DEXT all the time. This is
1062 // for feature / behaviour parity with binutils.
1063 template <typename InsnType>
1064 static DecodeStatus DecodeDEXT(MCInst &MI, InsnType Insn, uint64_t Address,
1065 const void *Decoder) {
1066 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1067 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1071 switch (MI.getOpcode()) {
1078 Size = Msbd + 1 + 32;
1085 llvm_unreachable("Unknown DEXT instruction!");
1088 MI.setOpcode(Mips::DEXT);
1090 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1091 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1093 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1094 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1095 MI.addOperand(MCOperand::createImm(Pos));
1096 MI.addOperand(MCOperand::createImm(Size));
1098 return MCDisassembler::Success;
1101 // Override the generated disassembler to produce DINS all the time. This is
1102 // for feature / behaviour parity with binutils.
1103 template <typename InsnType>
1104 static DecodeStatus DecodeDINS(MCInst &MI, InsnType Insn, uint64_t Address,
1105 const void *Decoder) {
1106 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1107 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1111 switch (MI.getOpcode()) {
1114 Size = Msbd + 1 - Pos;
1118 Size = Msbd + 33 - Pos;
1122 // mbsd = pos + size - 33
1123 // mbsd - pos + 33 = size
1124 Size = Msbd + 33 - Pos;
1127 llvm_unreachable("Unknown DINS instruction!");
1130 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1131 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1133 MI.setOpcode(Mips::DINS);
1134 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1135 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1136 MI.addOperand(MCOperand::createImm(Pos));
1137 MI.addOperand(MCOperand::createImm(Size));
1139 return MCDisassembler::Success;
1142 // Auto-generated decoder wouldn't add the third operand for CRC32*.
1143 template <typename InsnType>
1144 static DecodeStatus DecodeCRC(MCInst &MI, InsnType Insn, uint64_t Address,
1145 const void *Decoder) {
1146 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1147 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1148 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1150 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1152 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1154 return MCDisassembler::Success;
1157 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1158 /// according to the given endianness.
1159 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
1160 uint64_t &Size, uint32_t &Insn,
1162 // We want to read exactly 2 Bytes of data.
1163 if (Bytes.size() < 2) {
1165 return MCDisassembler::Fail;
1169 Insn = (Bytes[0] << 8) | Bytes[1];
1171 Insn = (Bytes[1] << 8) | Bytes[0];
1174 return MCDisassembler::Success;
1177 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1178 /// according to the given endianness.
1179 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
1180 uint64_t &Size, uint32_t &Insn,
1181 bool IsBigEndian, bool IsMicroMips) {
1182 // We want to read exactly 4 Bytes of data.
1183 if (Bytes.size() < 4) {
1185 return MCDisassembler::Fail;
1188 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1189 // always precede the low 16 bits in the instruction stream (that is, they
1190 // are placed at lower addresses in the instruction stream).
1192 // microMIPS byte ordering:
1193 // Big-endian: 0 | 1 | 2 | 3
1194 // Little-endian: 1 | 0 | 3 | 2
1197 // Encoded as a big-endian 32-bit word in the stream.
1199 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
1202 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
1205 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
1210 return MCDisassembler::Success;
1213 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1214 ArrayRef<uint8_t> Bytes,
1216 raw_ostream &VStream,
1217 raw_ostream &CStream) const {
1219 DecodeStatus Result;
1223 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
1224 if (Result == MCDisassembler::Fail)
1225 return MCDisassembler::Fail;
1227 if (hasMips32r6()) {
1229 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1230 // Calling the auto-generated decoder function for microMIPS32R6
1231 // 16-bit instructions.
1232 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
1233 Address, this, STI);
1234 if (Result != MCDisassembler::Fail) {
1240 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1241 // Calling the auto-generated decoder function for microMIPS 16-bit
1243 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
1245 if (Result != MCDisassembler::Fail) {
1250 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
1251 if (Result == MCDisassembler::Fail)
1252 return MCDisassembler::Fail;
1254 if (hasMips32r6()) {
1256 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1257 // Calling the auto-generated decoder function.
1258 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
1260 if (Result != MCDisassembler::Fail) {
1266 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1267 // Calling the auto-generated decoder function.
1268 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
1270 if (Result != MCDisassembler::Fail) {
1276 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1277 Result = decodeInstruction(DecoderTableMicroMipsFP6432, Instr, Insn,
1278 Address, this, STI);
1279 if (Result != MCDisassembler::Fail) {
1285 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1286 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1287 // could form a valid instruction. The two bytes we rejected as an
1288 // instruction could have actually beeen an inline constant pool that is
1289 // unconditionally branched over.
1291 return MCDisassembler::Fail;
1294 // Attempt to read the instruction so that we can attempt to decode it. If
1295 // the buffer is not 4 bytes long, let the higher level logic figure out
1296 // what to do with a size of zero and MCDisassembler::Fail.
1297 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
1298 if (Result == MCDisassembler::Fail)
1299 return MCDisassembler::Fail;
1301 // The only instruction size for standard encoded MIPS.
1305 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1307 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
1308 if (Result != MCDisassembler::Fail)
1312 if (hasMips32r6() && isGP64()) {
1314 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1315 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
1316 Address, this, STI);
1317 if (Result != MCDisassembler::Fail)
1321 if (hasMips32r6() && isPTR64()) {
1323 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1324 Result = decodeInstruction(DecoderTableMips32r6_64r6_PTR6432, Instr, Insn,
1325 Address, this, STI);
1326 if (Result != MCDisassembler::Fail)
1330 if (hasMips32r6()) {
1331 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1332 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
1333 Address, this, STI);
1334 if (Result != MCDisassembler::Fail)
1338 if (hasMips2() && isPTR64()) {
1340 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1341 Result = decodeInstruction(DecoderTableMips32_64_PTR6432, Instr, Insn,
1342 Address, this, STI);
1343 if (Result != MCDisassembler::Fail)
1348 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1349 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
1350 Address, this, STI);
1351 if (Result != MCDisassembler::Fail)
1356 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1357 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
1358 Address, this, STI);
1359 if (Result != MCDisassembler::Fail)
1365 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1366 Result = decodeInstruction(DecoderTableMipsFP6432, Instr, Insn,
1367 Address, this, STI);
1368 if (Result != MCDisassembler::Fail)
1372 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1373 // Calling the auto-generated decoder function.
1375 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
1376 if (Result != MCDisassembler::Fail)
1379 return MCDisassembler::Fail;
1382 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
1385 const void *Decoder) {
1386 return MCDisassembler::Fail;
1389 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1392 const void *Decoder) {
1394 return MCDisassembler::Fail;
1396 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1397 Inst.addOperand(MCOperand::createReg(Reg));
1398 return MCDisassembler::Success;
1401 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1404 const void *Decoder) {
1406 return MCDisassembler::Fail;
1407 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1408 Inst.addOperand(MCOperand::createReg(Reg));
1409 return MCDisassembler::Success;
1412 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1415 const void *Decoder) {
1417 return MCDisassembler::Fail;
1418 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1419 Inst.addOperand(MCOperand::createReg(Reg));
1420 return MCDisassembler::Success;
1423 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1426 const void *Decoder) {
1428 return MCDisassembler::Fail;
1429 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1430 Inst.addOperand(MCOperand::createReg(Reg));
1431 return MCDisassembler::Success;
1434 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1437 const void *Decoder) {
1439 return MCDisassembler::Fail;
1440 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1441 Inst.addOperand(MCOperand::createReg(Reg));
1442 return MCDisassembler::Success;
1445 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1448 const void *Decoder) {
1449 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1450 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1452 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1455 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1458 const void *Decoder) {
1459 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1462 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1465 const void *Decoder) {
1467 return MCDisassembler::Fail;
1469 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1470 Inst.addOperand(MCOperand::createReg(Reg));
1471 return MCDisassembler::Success;
1474 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1477 const void *Decoder) {
1479 return MCDisassembler::Fail;
1481 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1482 Inst.addOperand(MCOperand::createReg(Reg));
1483 return MCDisassembler::Success;
1486 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1489 const void *Decoder) {
1491 return MCDisassembler::Fail;
1492 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1493 Inst.addOperand(MCOperand::createReg(Reg));
1494 return MCDisassembler::Success;
1497 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1500 const void *Decoder) {
1502 return MCDisassembler::Fail;
1503 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1504 Inst.addOperand(MCOperand::createReg(Reg));
1505 return MCDisassembler::Success;
1508 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1510 const void *Decoder) {
1512 return MCDisassembler::Fail;
1514 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1515 Inst.addOperand(MCOperand::createReg(Reg));
1516 return MCDisassembler::Success;
1519 static DecodeStatus DecodeMem(MCInst &Inst,
1522 const void *Decoder) {
1523 int Offset = SignExtend32<16>(Insn & 0xffff);
1524 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1525 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1527 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1528 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1530 if (Inst.getOpcode() == Mips::SC ||
1531 Inst.getOpcode() == Mips::SCD)
1532 Inst.addOperand(MCOperand::createReg(Reg));
1534 Inst.addOperand(MCOperand::createReg(Reg));
1535 Inst.addOperand(MCOperand::createReg(Base));
1536 Inst.addOperand(MCOperand::createImm(Offset));
1538 return MCDisassembler::Success;
1541 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1544 const void *Decoder) {
1545 int Offset = SignExtend32<9>(Insn >> 7);
1546 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1547 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1549 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1550 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1552 if (Inst.getOpcode() == Mips::SCE)
1553 Inst.addOperand(MCOperand::createReg(Reg));
1555 Inst.addOperand(MCOperand::createReg(Reg));
1556 Inst.addOperand(MCOperand::createReg(Base));
1557 Inst.addOperand(MCOperand::createImm(Offset));
1559 return MCDisassembler::Success;
1562 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1565 const void *Decoder) {
1566 int Offset = SignExtend32<16>(Insn & 0xffff);
1567 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1568 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1570 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1571 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1573 Inst.addOperand(MCOperand::createReg(Reg));
1574 Inst.addOperand(MCOperand::createReg(Base));
1575 Inst.addOperand(MCOperand::createImm(Offset));
1577 return MCDisassembler::Success;
1580 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1583 const void *Decoder) {
1584 int Offset = SignExtend32<16>(Insn & 0xffff);
1585 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1586 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1588 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1590 Inst.addOperand(MCOperand::createReg(Base));
1591 Inst.addOperand(MCOperand::createImm(Offset));
1592 Inst.addOperand(MCOperand::createImm(Hint));
1594 return MCDisassembler::Success;
1597 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1600 const void *Decoder) {
1601 int Offset = SignExtend32<12>(Insn & 0xfff);
1602 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1603 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1605 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1607 Inst.addOperand(MCOperand::createReg(Base));
1608 Inst.addOperand(MCOperand::createImm(Offset));
1609 Inst.addOperand(MCOperand::createImm(Hint));
1611 return MCDisassembler::Success;
1614 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1617 const void *Decoder) {
1618 int Offset = SignExtend32<9>(Insn & 0x1ff);
1619 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1620 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1622 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1624 Inst.addOperand(MCOperand::createReg(Base));
1625 Inst.addOperand(MCOperand::createImm(Offset));
1626 Inst.addOperand(MCOperand::createImm(Hint));
1628 return MCDisassembler::Success;
1631 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1634 const void *Decoder) {
1635 int Offset = SignExtend32<9>(Insn >> 7);
1636 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1637 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1639 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1641 Inst.addOperand(MCOperand::createReg(Base));
1642 Inst.addOperand(MCOperand::createImm(Offset));
1643 Inst.addOperand(MCOperand::createImm(Hint));
1645 return MCDisassembler::Success;
1648 static DecodeStatus DecodeSyncI(MCInst &Inst,
1651 const void *Decoder) {
1652 int Offset = SignExtend32<16>(Insn & 0xffff);
1653 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1655 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1657 Inst.addOperand(MCOperand::createReg(Base));
1658 Inst.addOperand(MCOperand::createImm(Offset));
1660 return MCDisassembler::Success;
1663 static DecodeStatus DecodeSyncI_MM(MCInst &Inst, unsigned Insn,
1664 uint64_t Address, const void *Decoder) {
1665 int Offset = SignExtend32<16>(Insn & 0xffff);
1666 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1668 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1670 Inst.addOperand(MCOperand::createReg(Base));
1671 Inst.addOperand(MCOperand::createImm(Offset));
1673 return MCDisassembler::Success;
1676 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1679 const void *Decoder) {
1680 int Immediate = SignExtend32<16>(Insn & 0xffff);
1681 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1683 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1685 Inst.addOperand(MCOperand::createReg(Base));
1686 Inst.addOperand(MCOperand::createImm(Immediate));
1688 return MCDisassembler::Success;
1691 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1692 uint64_t Address, const void *Decoder) {
1693 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1694 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1695 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1697 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1698 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1700 Inst.addOperand(MCOperand::createReg(Reg));
1701 Inst.addOperand(MCOperand::createReg(Base));
1703 // The immediate field of an LD/ST instruction is scaled which means it must
1704 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1710 switch(Inst.getOpcode())
1713 assert(false && "Unexpected instruction");
1714 return MCDisassembler::Fail;
1718 Inst.addOperand(MCOperand::createImm(Offset));
1722 Inst.addOperand(MCOperand::createImm(Offset * 2));
1726 Inst.addOperand(MCOperand::createImm(Offset * 4));
1730 Inst.addOperand(MCOperand::createImm(Offset * 8));
1734 return MCDisassembler::Success;
1737 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1740 const void *Decoder) {
1741 unsigned Offset = Insn & 0xf;
1742 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1743 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1745 switch (Inst.getOpcode()) {
1746 case Mips::LBU16_MM:
1747 case Mips::LHU16_MM:
1749 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1750 == MCDisassembler::Fail)
1751 return MCDisassembler::Fail;
1754 case Mips::SB16_MMR6:
1756 case Mips::SH16_MMR6:
1758 case Mips::SW16_MMR6:
1759 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1760 == MCDisassembler::Fail)
1761 return MCDisassembler::Fail;
1765 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1766 == MCDisassembler::Fail)
1767 return MCDisassembler::Fail;
1769 switch (Inst.getOpcode()) {
1770 case Mips::LBU16_MM:
1772 Inst.addOperand(MCOperand::createImm(-1));
1774 Inst.addOperand(MCOperand::createImm(Offset));
1777 case Mips::SB16_MMR6:
1778 Inst.addOperand(MCOperand::createImm(Offset));
1780 case Mips::LHU16_MM:
1782 case Mips::SH16_MMR6:
1783 Inst.addOperand(MCOperand::createImm(Offset << 1));
1787 case Mips::SW16_MMR6:
1788 Inst.addOperand(MCOperand::createImm(Offset << 2));
1792 return MCDisassembler::Success;
1795 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1798 const void *Decoder) {
1799 unsigned Offset = Insn & 0x1F;
1800 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1802 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1804 Inst.addOperand(MCOperand::createReg(Reg));
1805 Inst.addOperand(MCOperand::createReg(Mips::SP));
1806 Inst.addOperand(MCOperand::createImm(Offset << 2));
1808 return MCDisassembler::Success;
1811 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1814 const void *Decoder) {
1815 unsigned Offset = Insn & 0x7F;
1816 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1818 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1820 Inst.addOperand(MCOperand::createReg(Reg));
1821 Inst.addOperand(MCOperand::createReg(Mips::GP));
1822 Inst.addOperand(MCOperand::createImm(Offset << 2));
1824 return MCDisassembler::Success;
1827 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1830 const void *Decoder) {
1832 switch (Inst.getOpcode()) {
1833 case Mips::LWM16_MMR6:
1834 case Mips::SWM16_MMR6:
1835 Offset = fieldFromInstruction(Insn, 4, 4);
1838 Offset = SignExtend32<4>(Insn & 0xf);
1842 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1843 == MCDisassembler::Fail)
1844 return MCDisassembler::Fail;
1846 Inst.addOperand(MCOperand::createReg(Mips::SP));
1847 Inst.addOperand(MCOperand::createImm(Offset << 2));
1849 return MCDisassembler::Success;
1852 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1855 const void *Decoder) {
1856 int Offset = SignExtend32<9>(Insn & 0x1ff);
1857 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1858 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1860 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1861 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1863 if (Inst.getOpcode() == Mips::SCE_MM || Inst.getOpcode() == Mips::SC_MMR6)
1864 Inst.addOperand(MCOperand::createReg(Reg));
1866 Inst.addOperand(MCOperand::createReg(Reg));
1867 Inst.addOperand(MCOperand::createReg(Base));
1868 Inst.addOperand(MCOperand::createImm(Offset));
1870 return MCDisassembler::Success;
1873 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1876 const void *Decoder) {
1877 int Offset = SignExtend32<12>(Insn & 0x0fff);
1878 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1879 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1881 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1882 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1884 switch (Inst.getOpcode()) {
1885 case Mips::SWM32_MM:
1886 case Mips::LWM32_MM:
1887 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1888 == MCDisassembler::Fail)
1889 return MCDisassembler::Fail;
1890 Inst.addOperand(MCOperand::createReg(Base));
1891 Inst.addOperand(MCOperand::createImm(Offset));
1894 Inst.addOperand(MCOperand::createReg(Reg));
1897 Inst.addOperand(MCOperand::createReg(Reg));
1898 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1899 Inst.addOperand(MCOperand::createReg(Reg+1));
1901 Inst.addOperand(MCOperand::createReg(Base));
1902 Inst.addOperand(MCOperand::createImm(Offset));
1905 return MCDisassembler::Success;
1908 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1911 const void *Decoder) {
1912 int Offset = SignExtend32<16>(Insn & 0xffff);
1913 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1914 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1916 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1917 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1919 Inst.addOperand(MCOperand::createReg(Reg));
1920 Inst.addOperand(MCOperand::createReg(Base));
1921 Inst.addOperand(MCOperand::createImm(Offset));
1923 return MCDisassembler::Success;
1926 static DecodeStatus DecodeFMem(MCInst &Inst,
1929 const void *Decoder) {
1930 int Offset = SignExtend32<16>(Insn & 0xffff);
1931 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1932 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1934 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1935 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1937 Inst.addOperand(MCOperand::createReg(Reg));
1938 Inst.addOperand(MCOperand::createReg(Base));
1939 Inst.addOperand(MCOperand::createImm(Offset));
1941 return MCDisassembler::Success;
1944 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
1945 uint64_t Address, const void *Decoder) {
1946 // This function is the same as DecodeFMem but with the Reg and Base fields
1947 // swapped according to microMIPS spec.
1948 int Offset = SignExtend32<16>(Insn & 0xffff);
1949 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1950 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1952 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1953 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1955 Inst.addOperand(MCOperand::createReg(Reg));
1956 Inst.addOperand(MCOperand::createReg(Base));
1957 Inst.addOperand(MCOperand::createImm(Offset));
1959 return MCDisassembler::Success;
1962 static DecodeStatus DecodeFMem2(MCInst &Inst,
1965 const void *Decoder) {
1966 int Offset = SignExtend32<16>(Insn & 0xffff);
1967 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1968 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1970 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1971 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1973 Inst.addOperand(MCOperand::createReg(Reg));
1974 Inst.addOperand(MCOperand::createReg(Base));
1975 Inst.addOperand(MCOperand::createImm(Offset));
1977 return MCDisassembler::Success;
1980 static DecodeStatus DecodeFMem3(MCInst &Inst,
1983 const void *Decoder) {
1984 int Offset = SignExtend32<16>(Insn & 0xffff);
1985 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1986 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1988 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1989 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1991 Inst.addOperand(MCOperand::createReg(Reg));
1992 Inst.addOperand(MCOperand::createReg(Base));
1993 Inst.addOperand(MCOperand::createImm(Offset));
1995 return MCDisassembler::Success;
1998 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
2001 const void *Decoder) {
2002 int Offset = SignExtend32<11>(Insn & 0x07ff);
2003 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
2004 unsigned Base = fieldFromInstruction(Insn, 11, 5);
2006 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2007 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2009 Inst.addOperand(MCOperand::createReg(Reg));
2010 Inst.addOperand(MCOperand::createReg(Base));
2011 Inst.addOperand(MCOperand::createImm(Offset));
2013 return MCDisassembler::Success;
2016 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
2017 uint64_t Address, const void *Decoder) {
2018 int Offset = SignExtend32<11>(Insn & 0x07ff);
2019 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
2020 unsigned Base = fieldFromInstruction(Insn, 16, 5);
2022 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2023 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2025 Inst.addOperand(MCOperand::createReg(Reg));
2026 Inst.addOperand(MCOperand::createReg(Base));
2027 Inst.addOperand(MCOperand::createImm(Offset));
2029 return MCDisassembler::Success;
2032 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
2035 const void *Decoder) {
2036 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
2037 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
2038 unsigned Base = fieldFromInstruction(Insn, 21, 5);
2040 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
2041 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2043 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
2044 Inst.addOperand(MCOperand::createReg(Rt));
2047 Inst.addOperand(MCOperand::createReg(Rt));
2048 Inst.addOperand(MCOperand::createReg(Base));
2049 Inst.addOperand(MCOperand::createImm(Offset));
2051 return MCDisassembler::Success;
2054 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
2057 const void *Decoder) {
2058 // Currently only hardware register 29 is supported.
2060 return MCDisassembler::Fail;
2061 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
2062 return MCDisassembler::Success;
2065 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
2068 const void *Decoder) {
2069 if (RegNo > 30 || RegNo %2)
2070 return MCDisassembler::Fail;
2072 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
2073 Inst.addOperand(MCOperand::createReg(Reg));
2074 return MCDisassembler::Success;
2077 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
2080 const void *Decoder) {
2082 return MCDisassembler::Fail;
2084 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
2085 Inst.addOperand(MCOperand::createReg(Reg));
2086 return MCDisassembler::Success;
2089 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
2092 const void *Decoder) {
2094 return MCDisassembler::Fail;
2096 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
2097 Inst.addOperand(MCOperand::createReg(Reg));
2098 return MCDisassembler::Success;
2101 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
2104 const void *Decoder) {
2106 return MCDisassembler::Fail;
2108 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
2109 Inst.addOperand(MCOperand::createReg(Reg));
2110 return MCDisassembler::Success;
2113 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
2116 const void *Decoder) {
2118 return MCDisassembler::Fail;
2120 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
2121 Inst.addOperand(MCOperand::createReg(Reg));
2122 return MCDisassembler::Success;
2125 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
2128 const void *Decoder) {
2130 return MCDisassembler::Fail;
2132 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
2133 Inst.addOperand(MCOperand::createReg(Reg));
2134 return MCDisassembler::Success;
2137 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
2140 const void *Decoder) {
2142 return MCDisassembler::Fail;
2144 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
2145 Inst.addOperand(MCOperand::createReg(Reg));
2146 return MCDisassembler::Success;
2149 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
2152 const void *Decoder) {
2154 return MCDisassembler::Fail;
2156 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
2157 Inst.addOperand(MCOperand::createReg(Reg));
2158 return MCDisassembler::Success;
2161 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
2164 const void *Decoder) {
2166 return MCDisassembler::Fail;
2168 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
2169 Inst.addOperand(MCOperand::createReg(Reg));
2170 return MCDisassembler::Success;
2173 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
2176 const void *Decoder) {
2178 return MCDisassembler::Fail;
2180 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
2181 Inst.addOperand(MCOperand::createReg(Reg));
2182 return MCDisassembler::Success;
2185 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
2188 const void *Decoder) {
2190 return MCDisassembler::Fail;
2192 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
2193 Inst.addOperand(MCOperand::createReg(Reg));
2194 return MCDisassembler::Success;
2197 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
2200 const void *Decoder) {
2201 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
2202 Inst.addOperand(MCOperand::createImm(BranchOffset));
2203 return MCDisassembler::Success;
2206 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
2209 const void *Decoder) {
2210 int32_t BranchOffset = (SignExtend32<16>(Offset) * 2);
2211 Inst.addOperand(MCOperand::createImm(BranchOffset));
2212 return MCDisassembler::Success;
2215 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
2218 const void *Decoder) {
2219 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
2220 Inst.addOperand(MCOperand::createImm(JumpOffset));
2221 return MCDisassembler::Success;
2224 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
2227 const void *Decoder) {
2228 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2230 Inst.addOperand(MCOperand::createImm(BranchOffset));
2231 return MCDisassembler::Success;
2234 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
2237 const void *Decoder) {
2238 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2240 Inst.addOperand(MCOperand::createImm(BranchOffset));
2241 return MCDisassembler::Success;
2244 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
2247 const void *Decoder) {
2248 int32_t BranchOffset = SignExtend32<26>(Offset) * 4 + 4;
2250 Inst.addOperand(MCOperand::createImm(BranchOffset));
2251 return MCDisassembler::Success;
2254 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
2257 const void *Decoder) {
2258 int32_t BranchOffset = SignExtend32<8>(Offset << 1);
2259 Inst.addOperand(MCOperand::createImm(BranchOffset));
2260 return MCDisassembler::Success;
2263 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
2266 const void *Decoder) {
2267 int32_t BranchOffset = SignExtend32<11>(Offset << 1);
2268 Inst.addOperand(MCOperand::createImm(BranchOffset));
2269 return MCDisassembler::Success;
2272 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
2275 const void *Decoder) {
2276 int32_t BranchOffset = SignExtend32<16>(Offset) * 2 + 4;
2277 Inst.addOperand(MCOperand::createImm(BranchOffset));
2278 return MCDisassembler::Success;
2281 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
2284 const void *Decoder) {
2285 int32_t BranchOffset = SignExtend32<27>(Offset << 1);
2287 Inst.addOperand(MCOperand::createImm(BranchOffset));
2288 return MCDisassembler::Success;
2291 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
2294 const void *Decoder) {
2295 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
2296 Inst.addOperand(MCOperand::createImm(JumpOffset));
2297 return MCDisassembler::Success;
2300 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
2303 const void *Decoder) {
2305 Inst.addOperand(MCOperand::createImm(1));
2306 else if (Value == 0x7)
2307 Inst.addOperand(MCOperand::createImm(-1));
2309 Inst.addOperand(MCOperand::createImm(Value << 2));
2310 return MCDisassembler::Success;
2313 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
2316 const void *Decoder) {
2318 Inst.addOperand(MCOperand::createImm(-1));
2320 Inst.addOperand(MCOperand::createImm(Value));
2321 return MCDisassembler::Success;
2324 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
2327 const void *Decoder) {
2328 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
2329 return MCDisassembler::Success;
2332 template <unsigned Bits, int Offset, int Scale>
2333 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2335 const void *Decoder) {
2336 Value &= ((1 << Bits) - 1);
2338 Inst.addOperand(MCOperand::createImm(Value + Offset));
2339 return MCDisassembler::Success;
2342 template <unsigned Bits, int Offset, int ScaleBy>
2343 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2345 const void *Decoder) {
2346 int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
2347 Inst.addOperand(MCOperand::createImm(Imm + Offset));
2348 return MCDisassembler::Success;
2351 static DecodeStatus DecodeInsSize(MCInst &Inst,
2354 const void *Decoder) {
2355 // First we need to grab the pos(lsb) from MCInst.
2356 // This function only handles the 32 bit variants of ins, as dins
2357 // variants are handled differently.
2358 int Pos = Inst.getOperand(2).getImm();
2359 int Size = (int) Insn - Pos + 1;
2360 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
2361 return MCDisassembler::Success;
2364 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
2365 uint64_t Address, const void *Decoder) {
2366 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
2367 return MCDisassembler::Success;
2370 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
2371 uint64_t Address, const void *Decoder) {
2372 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
2373 return MCDisassembler::Success;
2376 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
2377 uint64_t Address, const void *Decoder) {
2378 int32_t DecodedValue;
2380 case 0: DecodedValue = 256; break;
2381 case 1: DecodedValue = 257; break;
2382 case 510: DecodedValue = -258; break;
2383 case 511: DecodedValue = -257; break;
2384 default: DecodedValue = SignExtend32<9>(Insn); break;
2386 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
2387 return MCDisassembler::Success;
2390 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
2391 uint64_t Address, const void *Decoder) {
2392 // Insn must be >= 0, since it is unsigned that condition is always true.
2394 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2396 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
2397 return MCDisassembler::Success;
2400 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2403 const void *Decoder) {
2404 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2405 Mips::S6, Mips::S7, Mips::FP};
2408 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2410 // Empty register lists are not allowed.
2412 return MCDisassembler::Fail;
2414 RegNum = RegLst & 0xf;
2416 // RegLst values 10-15, and 26-31 are reserved.
2418 return MCDisassembler::Fail;
2420 for (unsigned i = 0; i < RegNum; i++)
2421 Inst.addOperand(MCOperand::createReg(Regs[i]));
2424 Inst.addOperand(MCOperand::createReg(Mips::RA));
2426 return MCDisassembler::Success;
2429 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2431 const void *Decoder) {
2432 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2434 switch(Inst.getOpcode()) {
2436 RegLst = fieldFromInstruction(Insn, 4, 2);
2438 case Mips::LWM16_MMR6:
2439 case Mips::SWM16_MMR6:
2440 RegLst = fieldFromInstruction(Insn, 8, 2);
2443 unsigned RegNum = RegLst & 0x3;
2445 for (unsigned i = 0; i <= RegNum; i++)
2446 Inst.addOperand(MCOperand::createReg(Regs[i]));
2448 Inst.addOperand(MCOperand::createReg(Mips::RA));
2450 return MCDisassembler::Success;
2453 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned RegPair,
2454 uint64_t Address, const void *Decoder) {
2457 return MCDisassembler::Fail;
2459 Inst.addOperand(MCOperand::createReg(Mips::A1));
2460 Inst.addOperand(MCOperand::createReg(Mips::A2));
2463 Inst.addOperand(MCOperand::createReg(Mips::A1));
2464 Inst.addOperand(MCOperand::createReg(Mips::A3));
2467 Inst.addOperand(MCOperand::createReg(Mips::A2));
2468 Inst.addOperand(MCOperand::createReg(Mips::A3));
2471 Inst.addOperand(MCOperand::createReg(Mips::A0));
2472 Inst.addOperand(MCOperand::createReg(Mips::S5));
2475 Inst.addOperand(MCOperand::createReg(Mips::A0));
2476 Inst.addOperand(MCOperand::createReg(Mips::S6));
2479 Inst.addOperand(MCOperand::createReg(Mips::A0));
2480 Inst.addOperand(MCOperand::createReg(Mips::A1));
2483 Inst.addOperand(MCOperand::createReg(Mips::A0));
2484 Inst.addOperand(MCOperand::createReg(Mips::A2));
2487 Inst.addOperand(MCOperand::createReg(Mips::A0));
2488 Inst.addOperand(MCOperand::createReg(Mips::A3));
2492 return MCDisassembler::Success;
2495 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2496 uint64_t Address, const void *Decoder) {
2497 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2498 return MCDisassembler::Success;
2501 template <typename InsnType>
2502 static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
2504 const void *Decoder) {
2506 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2507 // Invalid if rt == 0
2508 // BGTZALC_MMR6 if rs == 0 && rt != 0
2509 // BLTZALC_MMR6 if rs != 0 && rs == rt
2510 // BLTUC_MMR6 if rs != 0 && rs != rt
2512 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2513 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2519 return MCDisassembler::Fail;
2521 MI.setOpcode(Mips::BGTZALC_MMR6);
2523 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2525 else if (Rs == Rt) {
2526 MI.setOpcode(Mips::BLTZALC_MMR6);
2528 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2531 MI.setOpcode(Mips::BLTUC_MMR6);
2534 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2539 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2543 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2545 MI.addOperand(MCOperand::createImm(Imm));
2547 return MCDisassembler::Success;
2550 template <typename InsnType>
2551 static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
2553 const void *Decoder) {
2555 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2556 // Invalid if rt == 0
2557 // BLEZALC_MMR6 if rs == 0 && rt != 0
2558 // BGEZALC_MMR6 if rs == rt && rt != 0
2559 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2561 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2562 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2567 return MCDisassembler::Fail;
2569 MI.setOpcode(Mips::BLEZALC_MMR6);
2570 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2572 else if (Rs == Rt) {
2573 MI.setOpcode(Mips::BGEZALC_MMR6);
2574 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2578 MI.setOpcode(Mips::BGEUC_MMR6);
2579 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2584 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2586 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2588 MI.addOperand(MCOperand::createImm(Imm));
2590 return MCDisassembler::Success;