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);
541 static DecodeStatus DecodeMovePOperands(MCInst &Inst, unsigned Insn,
542 uint64_t Address, const void *Decoder);
546 Target &getTheMipselTarget();
547 Target &getTheMipsTarget();
548 Target &getTheMips64Target();
549 Target &getTheMips64elTarget();
551 } // end namespace llvm
553 static MCDisassembler *createMipsDisassembler(
555 const MCSubtargetInfo &STI,
557 return new MipsDisassembler(STI, Ctx, true);
560 static MCDisassembler *createMipselDisassembler(
562 const MCSubtargetInfo &STI,
564 return new MipsDisassembler(STI, Ctx, false);
567 extern "C" void LLVMInitializeMipsDisassembler() {
568 // Register the disassembler.
569 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
570 createMipsDisassembler);
571 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
572 createMipselDisassembler);
573 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
574 createMipsDisassembler);
575 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
576 createMipselDisassembler);
579 #include "MipsGenDisassemblerTables.inc"
581 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
582 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
583 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
584 return *(RegInfo->getRegClass(RC).begin() + RegNo);
587 template <typename InsnType>
588 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
589 const void *Decoder) {
590 using DecodeFN = DecodeStatus (*)(MCInst &, unsigned, uint64_t, const void *);
592 // The size of the n field depends on the element size
593 // The register class also depends on this.
594 InsnType tmp = fieldFromInstruction(insn, 17, 5);
596 DecodeFN RegDecoder = nullptr;
597 if ((tmp & 0x18) == 0x00) { // INSVE_B
599 RegDecoder = DecodeMSA128BRegisterClass;
600 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
602 RegDecoder = DecodeMSA128HRegisterClass;
603 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
605 RegDecoder = DecodeMSA128WRegisterClass;
606 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
608 RegDecoder = DecodeMSA128DRegisterClass;
610 llvm_unreachable("Invalid encoding");
612 assert(NSize != 0 && RegDecoder != nullptr);
615 tmp = fieldFromInstruction(insn, 6, 5);
616 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
617 return MCDisassembler::Fail;
619 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
620 return MCDisassembler::Fail;
622 tmp = fieldFromInstruction(insn, 16, NSize);
623 MI.addOperand(MCOperand::createImm(tmp));
625 tmp = fieldFromInstruction(insn, 11, 5);
626 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
627 return MCDisassembler::Fail;
629 MI.addOperand(MCOperand::createImm(0));
631 return MCDisassembler::Success;
634 template <typename InsnType>
635 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
636 const void *Decoder) {
637 InsnType Rs = fieldFromInstruction(insn, 16, 5);
638 InsnType Imm = fieldFromInstruction(insn, 0, 16);
639 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
641 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
643 MI.addOperand(MCOperand::createImm(Imm));
645 return MCDisassembler::Success;
648 template <typename InsnType>
649 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
650 const void *Decoder) {
651 InsnType Rs = fieldFromInstruction(insn, 21, 5);
652 InsnType Imm = fieldFromInstruction(insn, 0, 16);
653 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
655 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
657 MI.addOperand(MCOperand::createImm(Imm));
659 return MCDisassembler::Success;
662 template <typename InsnType>
663 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
665 const void *Decoder) {
666 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
667 // (otherwise we would have matched the ADDI instruction from the earlier
671 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
673 // BEQZALC if rs == 0 && rt != 0
674 // BEQC if rs < rt && rs != 0
676 InsnType Rs = fieldFromInstruction(insn, 21, 5);
677 InsnType Rt = fieldFromInstruction(insn, 16, 5);
678 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
682 MI.setOpcode(Mips::BOVC);
684 } else if (Rs != 0 && Rs < Rt) {
685 MI.setOpcode(Mips::BEQC);
688 MI.setOpcode(Mips::BEQZALC);
691 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
694 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
696 MI.addOperand(MCOperand::createImm(Imm));
698 return MCDisassembler::Success;
701 template <typename InsnType>
702 static DecodeStatus DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn,
704 const void *Decoder) {
705 InsnType Rt = fieldFromInstruction(insn, 21, 5);
706 InsnType Rs = fieldFromInstruction(insn, 16, 5);
710 MI.setOpcode(Mips::BOVC_MMR6);
711 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
713 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
715 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
716 } else if (Rs != 0 && Rs < Rt) {
717 MI.setOpcode(Mips::BEQC_MMR6);
718 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
720 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
722 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
724 MI.setOpcode(Mips::BEQZALC_MMR6);
725 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
727 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
730 MI.addOperand(MCOperand::createImm(Imm));
732 return MCDisassembler::Success;
735 template <typename InsnType>
736 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
738 const void *Decoder) {
739 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
740 // (otherwise we would have matched the ADDI instruction from the earlier
744 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
746 // BNEZALC if rs == 0 && rt != 0
747 // BNEC if rs < rt && rs != 0
749 InsnType Rs = fieldFromInstruction(insn, 21, 5);
750 InsnType Rt = fieldFromInstruction(insn, 16, 5);
751 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
755 MI.setOpcode(Mips::BNVC);
757 } else if (Rs != 0 && Rs < Rt) {
758 MI.setOpcode(Mips::BNEC);
761 MI.setOpcode(Mips::BNEZALC);
764 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
767 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
769 MI.addOperand(MCOperand::createImm(Imm));
771 return MCDisassembler::Success;
774 template <typename InsnType>
775 static DecodeStatus DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn,
777 const void *Decoder) {
778 InsnType Rt = fieldFromInstruction(insn, 21, 5);
779 InsnType Rs = fieldFromInstruction(insn, 16, 5);
783 MI.setOpcode(Mips::BNVC_MMR6);
784 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
786 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
788 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
789 } else if (Rs != 0 && Rs < Rt) {
790 MI.setOpcode(Mips::BNEC_MMR6);
791 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
793 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
795 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
797 MI.setOpcode(Mips::BNEZALC_MMR6);
798 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
800 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
803 MI.addOperand(MCOperand::createImm(Imm));
805 return MCDisassembler::Success;
808 template <typename InsnType>
809 static DecodeStatus DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn,
811 const void *Decoder) {
813 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
814 // Invalid if rt == 0
815 // BGTZC_MMR6 if rs == 0 && rt != 0
816 // BLTZC_MMR6 if rs == rt && rt != 0
817 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
819 InsnType Rt = fieldFromInstruction(insn, 21, 5);
820 InsnType Rs = fieldFromInstruction(insn, 16, 5);
821 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
825 return MCDisassembler::Fail;
827 MI.setOpcode(Mips::BGTZC_MMR6);
829 MI.setOpcode(Mips::BLTZC_MMR6);
831 MI.setOpcode(Mips::BLTC_MMR6);
836 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
839 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
842 MI.addOperand(MCOperand::createImm(Imm));
844 return MCDisassembler::Success;
847 template <typename InsnType>
848 static DecodeStatus DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn,
850 const void *Decoder) {
852 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
853 // Invalid if rt == 0
854 // BLEZC_MMR6 if rs == 0 && rt != 0
855 // BGEZC_MMR6 if rs == rt && rt != 0
856 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
858 InsnType Rt = fieldFromInstruction(insn, 21, 5);
859 InsnType Rs = fieldFromInstruction(insn, 16, 5);
860 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
864 return MCDisassembler::Fail;
866 MI.setOpcode(Mips::BLEZC_MMR6);
868 MI.setOpcode(Mips::BGEZC_MMR6);
871 MI.setOpcode(Mips::BGEC_MMR6);
875 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
878 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
881 MI.addOperand(MCOperand::createImm(Imm));
883 return MCDisassembler::Success;
886 template <typename InsnType>
887 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
889 const void *Decoder) {
890 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
891 // (otherwise we would have matched the BLEZL instruction from the earlier
895 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
896 // Invalid if rs == 0
897 // BLEZC if rs == 0 && rt != 0
898 // BGEZC if rs == rt && rt != 0
899 // BGEC if rs != rt && rs != 0 && rt != 0
901 InsnType Rs = fieldFromInstruction(insn, 21, 5);
902 InsnType Rt = fieldFromInstruction(insn, 16, 5);
903 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
907 return MCDisassembler::Fail;
909 MI.setOpcode(Mips::BLEZC);
911 MI.setOpcode(Mips::BGEZC);
914 MI.setOpcode(Mips::BGEC);
918 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
921 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
924 MI.addOperand(MCOperand::createImm(Imm));
926 return MCDisassembler::Success;
929 template <typename InsnType>
930 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
932 const void *Decoder) {
933 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
934 // (otherwise we would have matched the BGTZL instruction from the earlier
938 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
939 // Invalid if rs == 0
940 // BGTZC if rs == 0 && rt != 0
941 // BLTZC if rs == rt && rt != 0
942 // BLTC if rs != rt && rs != 0 && rt != 0
946 InsnType Rs = fieldFromInstruction(insn, 21, 5);
947 InsnType Rt = fieldFromInstruction(insn, 16, 5);
948 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
951 return MCDisassembler::Fail;
953 MI.setOpcode(Mips::BGTZC);
955 MI.setOpcode(Mips::BLTZC);
957 MI.setOpcode(Mips::BLTC);
962 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
965 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
968 MI.addOperand(MCOperand::createImm(Imm));
970 return MCDisassembler::Success;
973 template <typename InsnType>
974 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
976 const void *Decoder) {
977 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
978 // (otherwise we would have matched the BGTZ instruction from the earlier
982 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
984 // BGTZALC if rs == 0 && rt != 0
985 // BLTZALC if rs != 0 && rs == rt
986 // BLTUC if rs != 0 && rs != rt
988 InsnType Rs = fieldFromInstruction(insn, 21, 5);
989 InsnType Rt = fieldFromInstruction(insn, 16, 5);
990 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
995 MI.setOpcode(Mips::BGTZ);
997 } else if (Rs == 0) {
998 MI.setOpcode(Mips::BGTZALC);
1000 } else if (Rs == Rt) {
1001 MI.setOpcode(Mips::BLTZALC);
1004 MI.setOpcode(Mips::BLTUC);
1010 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1014 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1017 MI.addOperand(MCOperand::createImm(Imm));
1019 return MCDisassembler::Success;
1022 template <typename InsnType>
1023 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
1025 const void *Decoder) {
1026 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1027 // (otherwise we would have matched the BLEZL instruction from the earlier
1031 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1032 // Invalid if rs == 0
1033 // BLEZALC if rs == 0 && rt != 0
1034 // BGEZALC if rs == rt && rt != 0
1035 // BGEUC if rs != rt && rs != 0 && rt != 0
1037 InsnType Rs = fieldFromInstruction(insn, 21, 5);
1038 InsnType Rt = fieldFromInstruction(insn, 16, 5);
1039 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1043 return MCDisassembler::Fail;
1045 MI.setOpcode(Mips::BLEZALC);
1047 MI.setOpcode(Mips::BGEZALC);
1050 MI.setOpcode(Mips::BGEUC);
1054 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1056 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1059 MI.addOperand(MCOperand::createImm(Imm));
1061 return MCDisassembler::Success;
1064 // Override the generated disassembler to produce DEXT all the time. This is
1065 // for feature / behaviour parity with binutils.
1066 template <typename InsnType>
1067 static DecodeStatus DecodeDEXT(MCInst &MI, InsnType Insn, uint64_t Address,
1068 const void *Decoder) {
1069 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1070 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1074 switch (MI.getOpcode()) {
1081 Size = Msbd + 1 + 32;
1088 llvm_unreachable("Unknown DEXT instruction!");
1091 MI.setOpcode(Mips::DEXT);
1093 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1094 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1096 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1097 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1098 MI.addOperand(MCOperand::createImm(Pos));
1099 MI.addOperand(MCOperand::createImm(Size));
1101 return MCDisassembler::Success;
1104 // Override the generated disassembler to produce DINS all the time. This is
1105 // for feature / behaviour parity with binutils.
1106 template <typename InsnType>
1107 static DecodeStatus DecodeDINS(MCInst &MI, InsnType Insn, uint64_t Address,
1108 const void *Decoder) {
1109 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1110 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1114 switch (MI.getOpcode()) {
1117 Size = Msbd + 1 - Pos;
1121 Size = Msbd + 33 - Pos;
1125 // mbsd = pos + size - 33
1126 // mbsd - pos + 33 = size
1127 Size = Msbd + 33 - Pos;
1130 llvm_unreachable("Unknown DINS instruction!");
1133 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1134 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1136 MI.setOpcode(Mips::DINS);
1137 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1138 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1139 MI.addOperand(MCOperand::createImm(Pos));
1140 MI.addOperand(MCOperand::createImm(Size));
1142 return MCDisassembler::Success;
1145 // Auto-generated decoder wouldn't add the third operand for CRC32*.
1146 template <typename InsnType>
1147 static DecodeStatus DecodeCRC(MCInst &MI, InsnType Insn, uint64_t Address,
1148 const void *Decoder) {
1149 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1150 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1151 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1153 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1155 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1157 return MCDisassembler::Success;
1160 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1161 /// according to the given endianness.
1162 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
1163 uint64_t &Size, uint32_t &Insn,
1165 // We want to read exactly 2 Bytes of data.
1166 if (Bytes.size() < 2) {
1168 return MCDisassembler::Fail;
1172 Insn = (Bytes[0] << 8) | Bytes[1];
1174 Insn = (Bytes[1] << 8) | Bytes[0];
1177 return MCDisassembler::Success;
1180 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1181 /// according to the given endianness.
1182 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
1183 uint64_t &Size, uint32_t &Insn,
1184 bool IsBigEndian, bool IsMicroMips) {
1185 // We want to read exactly 4 Bytes of data.
1186 if (Bytes.size() < 4) {
1188 return MCDisassembler::Fail;
1191 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1192 // always precede the low 16 bits in the instruction stream (that is, they
1193 // are placed at lower addresses in the instruction stream).
1195 // microMIPS byte ordering:
1196 // Big-endian: 0 | 1 | 2 | 3
1197 // Little-endian: 1 | 0 | 3 | 2
1200 // Encoded as a big-endian 32-bit word in the stream.
1202 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
1205 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
1208 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
1213 return MCDisassembler::Success;
1216 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1217 ArrayRef<uint8_t> Bytes,
1219 raw_ostream &VStream,
1220 raw_ostream &CStream) const {
1222 DecodeStatus Result;
1226 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
1227 if (Result == MCDisassembler::Fail)
1228 return MCDisassembler::Fail;
1230 if (hasMips32r6()) {
1232 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1233 // Calling the auto-generated decoder function for microMIPS32R6
1234 // 16-bit instructions.
1235 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
1236 Address, this, STI);
1237 if (Result != MCDisassembler::Fail) {
1243 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1244 // Calling the auto-generated decoder function for microMIPS 16-bit
1246 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
1248 if (Result != MCDisassembler::Fail) {
1253 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
1254 if (Result == MCDisassembler::Fail)
1255 return MCDisassembler::Fail;
1257 if (hasMips32r6()) {
1259 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1260 // Calling the auto-generated decoder function.
1261 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
1263 if (Result != MCDisassembler::Fail) {
1269 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1270 // Calling the auto-generated decoder function.
1271 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
1273 if (Result != MCDisassembler::Fail) {
1279 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1280 Result = decodeInstruction(DecoderTableMicroMipsFP6432, Instr, Insn,
1281 Address, this, STI);
1282 if (Result != MCDisassembler::Fail) {
1288 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1289 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1290 // could form a valid instruction. The two bytes we rejected as an
1291 // instruction could have actually beeen an inline constant pool that is
1292 // unconditionally branched over.
1294 return MCDisassembler::Fail;
1297 // Attempt to read the instruction so that we can attempt to decode it. If
1298 // the buffer is not 4 bytes long, let the higher level logic figure out
1299 // what to do with a size of zero and MCDisassembler::Fail.
1300 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
1301 if (Result == MCDisassembler::Fail)
1302 return MCDisassembler::Fail;
1304 // The only instruction size for standard encoded MIPS.
1308 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1310 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
1311 if (Result != MCDisassembler::Fail)
1315 if (hasMips32r6() && isGP64()) {
1317 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1318 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
1319 Address, this, STI);
1320 if (Result != MCDisassembler::Fail)
1324 if (hasMips32r6() && isPTR64()) {
1326 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1327 Result = decodeInstruction(DecoderTableMips32r6_64r6_PTR6432, Instr, Insn,
1328 Address, this, STI);
1329 if (Result != MCDisassembler::Fail)
1333 if (hasMips32r6()) {
1334 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1335 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
1336 Address, this, STI);
1337 if (Result != MCDisassembler::Fail)
1341 if (hasMips2() && isPTR64()) {
1343 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1344 Result = decodeInstruction(DecoderTableMips32_64_PTR6432, Instr, Insn,
1345 Address, this, STI);
1346 if (Result != MCDisassembler::Fail)
1351 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1352 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
1353 Address, this, STI);
1354 if (Result != MCDisassembler::Fail)
1359 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1360 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
1361 Address, this, STI);
1362 if (Result != MCDisassembler::Fail)
1368 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1369 Result = decodeInstruction(DecoderTableMipsFP6432, Instr, Insn,
1370 Address, this, STI);
1371 if (Result != MCDisassembler::Fail)
1375 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1376 // Calling the auto-generated decoder function.
1378 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
1379 if (Result != MCDisassembler::Fail)
1382 return MCDisassembler::Fail;
1385 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
1388 const void *Decoder) {
1389 return MCDisassembler::Fail;
1392 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1395 const void *Decoder) {
1397 return MCDisassembler::Fail;
1399 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1400 Inst.addOperand(MCOperand::createReg(Reg));
1401 return MCDisassembler::Success;
1404 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1407 const void *Decoder) {
1409 return MCDisassembler::Fail;
1410 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1411 Inst.addOperand(MCOperand::createReg(Reg));
1412 return MCDisassembler::Success;
1415 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1418 const void *Decoder) {
1420 return MCDisassembler::Fail;
1421 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1422 Inst.addOperand(MCOperand::createReg(Reg));
1423 return MCDisassembler::Success;
1426 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1429 const void *Decoder) {
1431 return MCDisassembler::Fail;
1432 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1433 Inst.addOperand(MCOperand::createReg(Reg));
1434 return MCDisassembler::Success;
1437 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1440 const void *Decoder) {
1442 return MCDisassembler::Fail;
1443 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1444 Inst.addOperand(MCOperand::createReg(Reg));
1445 return MCDisassembler::Success;
1448 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1451 const void *Decoder) {
1452 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1453 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1455 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1458 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1461 const void *Decoder) {
1462 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1465 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1468 const void *Decoder) {
1470 return MCDisassembler::Fail;
1472 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1473 Inst.addOperand(MCOperand::createReg(Reg));
1474 return MCDisassembler::Success;
1477 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1480 const void *Decoder) {
1482 return MCDisassembler::Fail;
1484 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1485 Inst.addOperand(MCOperand::createReg(Reg));
1486 return MCDisassembler::Success;
1489 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1492 const void *Decoder) {
1494 return MCDisassembler::Fail;
1495 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1496 Inst.addOperand(MCOperand::createReg(Reg));
1497 return MCDisassembler::Success;
1500 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1503 const void *Decoder) {
1505 return MCDisassembler::Fail;
1506 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1507 Inst.addOperand(MCOperand::createReg(Reg));
1508 return MCDisassembler::Success;
1511 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1513 const void *Decoder) {
1515 return MCDisassembler::Fail;
1517 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1518 Inst.addOperand(MCOperand::createReg(Reg));
1519 return MCDisassembler::Success;
1522 static DecodeStatus DecodeMem(MCInst &Inst,
1525 const void *Decoder) {
1526 int Offset = SignExtend32<16>(Insn & 0xffff);
1527 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1528 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1530 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1531 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1533 if (Inst.getOpcode() == Mips::SC ||
1534 Inst.getOpcode() == Mips::SCD)
1535 Inst.addOperand(MCOperand::createReg(Reg));
1537 Inst.addOperand(MCOperand::createReg(Reg));
1538 Inst.addOperand(MCOperand::createReg(Base));
1539 Inst.addOperand(MCOperand::createImm(Offset));
1541 return MCDisassembler::Success;
1544 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1547 const void *Decoder) {
1548 int Offset = SignExtend32<9>(Insn >> 7);
1549 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1550 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1552 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1553 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1555 if (Inst.getOpcode() == Mips::SCE)
1556 Inst.addOperand(MCOperand::createReg(Reg));
1558 Inst.addOperand(MCOperand::createReg(Reg));
1559 Inst.addOperand(MCOperand::createReg(Base));
1560 Inst.addOperand(MCOperand::createImm(Offset));
1562 return MCDisassembler::Success;
1565 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1568 const void *Decoder) {
1569 int Offset = SignExtend32<16>(Insn & 0xffff);
1570 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1571 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1573 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1574 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1576 Inst.addOperand(MCOperand::createReg(Reg));
1577 Inst.addOperand(MCOperand::createReg(Base));
1578 Inst.addOperand(MCOperand::createImm(Offset));
1580 return MCDisassembler::Success;
1583 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1586 const void *Decoder) {
1587 int Offset = SignExtend32<16>(Insn & 0xffff);
1588 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1589 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1591 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1593 Inst.addOperand(MCOperand::createReg(Base));
1594 Inst.addOperand(MCOperand::createImm(Offset));
1595 Inst.addOperand(MCOperand::createImm(Hint));
1597 return MCDisassembler::Success;
1600 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1603 const void *Decoder) {
1604 int Offset = SignExtend32<12>(Insn & 0xfff);
1605 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1606 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1608 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1610 Inst.addOperand(MCOperand::createReg(Base));
1611 Inst.addOperand(MCOperand::createImm(Offset));
1612 Inst.addOperand(MCOperand::createImm(Hint));
1614 return MCDisassembler::Success;
1617 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1620 const void *Decoder) {
1621 int Offset = SignExtend32<9>(Insn & 0x1ff);
1622 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1623 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1625 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1627 Inst.addOperand(MCOperand::createReg(Base));
1628 Inst.addOperand(MCOperand::createImm(Offset));
1629 Inst.addOperand(MCOperand::createImm(Hint));
1631 return MCDisassembler::Success;
1634 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1637 const void *Decoder) {
1638 int Offset = SignExtend32<9>(Insn >> 7);
1639 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1640 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1642 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1644 Inst.addOperand(MCOperand::createReg(Base));
1645 Inst.addOperand(MCOperand::createImm(Offset));
1646 Inst.addOperand(MCOperand::createImm(Hint));
1648 return MCDisassembler::Success;
1651 static DecodeStatus DecodeSyncI(MCInst &Inst,
1654 const void *Decoder) {
1655 int Offset = SignExtend32<16>(Insn & 0xffff);
1656 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1658 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1660 Inst.addOperand(MCOperand::createReg(Base));
1661 Inst.addOperand(MCOperand::createImm(Offset));
1663 return MCDisassembler::Success;
1666 static DecodeStatus DecodeSyncI_MM(MCInst &Inst, unsigned Insn,
1667 uint64_t Address, const void *Decoder) {
1668 int Offset = SignExtend32<16>(Insn & 0xffff);
1669 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1671 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1673 Inst.addOperand(MCOperand::createReg(Base));
1674 Inst.addOperand(MCOperand::createImm(Offset));
1676 return MCDisassembler::Success;
1679 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1682 const void *Decoder) {
1683 int Immediate = SignExtend32<16>(Insn & 0xffff);
1684 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1686 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1688 Inst.addOperand(MCOperand::createReg(Base));
1689 Inst.addOperand(MCOperand::createImm(Immediate));
1691 return MCDisassembler::Success;
1694 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1695 uint64_t Address, const void *Decoder) {
1696 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1697 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1698 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1700 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1701 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1703 Inst.addOperand(MCOperand::createReg(Reg));
1704 Inst.addOperand(MCOperand::createReg(Base));
1706 // The immediate field of an LD/ST instruction is scaled which means it must
1707 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1713 switch(Inst.getOpcode())
1716 assert(false && "Unexpected instruction");
1717 return MCDisassembler::Fail;
1721 Inst.addOperand(MCOperand::createImm(Offset));
1725 Inst.addOperand(MCOperand::createImm(Offset * 2));
1729 Inst.addOperand(MCOperand::createImm(Offset * 4));
1733 Inst.addOperand(MCOperand::createImm(Offset * 8));
1737 return MCDisassembler::Success;
1740 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1743 const void *Decoder) {
1744 unsigned Offset = Insn & 0xf;
1745 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1746 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1748 switch (Inst.getOpcode()) {
1749 case Mips::LBU16_MM:
1750 case Mips::LHU16_MM:
1752 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1753 == MCDisassembler::Fail)
1754 return MCDisassembler::Fail;
1757 case Mips::SB16_MMR6:
1759 case Mips::SH16_MMR6:
1761 case Mips::SW16_MMR6:
1762 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1763 == MCDisassembler::Fail)
1764 return MCDisassembler::Fail;
1768 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1769 == MCDisassembler::Fail)
1770 return MCDisassembler::Fail;
1772 switch (Inst.getOpcode()) {
1773 case Mips::LBU16_MM:
1775 Inst.addOperand(MCOperand::createImm(-1));
1777 Inst.addOperand(MCOperand::createImm(Offset));
1780 case Mips::SB16_MMR6:
1781 Inst.addOperand(MCOperand::createImm(Offset));
1783 case Mips::LHU16_MM:
1785 case Mips::SH16_MMR6:
1786 Inst.addOperand(MCOperand::createImm(Offset << 1));
1790 case Mips::SW16_MMR6:
1791 Inst.addOperand(MCOperand::createImm(Offset << 2));
1795 return MCDisassembler::Success;
1798 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1801 const void *Decoder) {
1802 unsigned Offset = Insn & 0x1F;
1803 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1805 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1807 Inst.addOperand(MCOperand::createReg(Reg));
1808 Inst.addOperand(MCOperand::createReg(Mips::SP));
1809 Inst.addOperand(MCOperand::createImm(Offset << 2));
1811 return MCDisassembler::Success;
1814 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1817 const void *Decoder) {
1818 unsigned Offset = Insn & 0x7F;
1819 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1821 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1823 Inst.addOperand(MCOperand::createReg(Reg));
1824 Inst.addOperand(MCOperand::createReg(Mips::GP));
1825 Inst.addOperand(MCOperand::createImm(Offset << 2));
1827 return MCDisassembler::Success;
1830 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1833 const void *Decoder) {
1835 switch (Inst.getOpcode()) {
1836 case Mips::LWM16_MMR6:
1837 case Mips::SWM16_MMR6:
1838 Offset = fieldFromInstruction(Insn, 4, 4);
1841 Offset = SignExtend32<4>(Insn & 0xf);
1845 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1846 == MCDisassembler::Fail)
1847 return MCDisassembler::Fail;
1849 Inst.addOperand(MCOperand::createReg(Mips::SP));
1850 Inst.addOperand(MCOperand::createImm(Offset << 2));
1852 return MCDisassembler::Success;
1855 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1858 const void *Decoder) {
1859 int Offset = SignExtend32<9>(Insn & 0x1ff);
1860 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1861 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1863 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1864 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1866 if (Inst.getOpcode() == Mips::SCE_MM || Inst.getOpcode() == Mips::SC_MMR6)
1867 Inst.addOperand(MCOperand::createReg(Reg));
1869 Inst.addOperand(MCOperand::createReg(Reg));
1870 Inst.addOperand(MCOperand::createReg(Base));
1871 Inst.addOperand(MCOperand::createImm(Offset));
1873 return MCDisassembler::Success;
1876 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1879 const void *Decoder) {
1880 int Offset = SignExtend32<12>(Insn & 0x0fff);
1881 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1882 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1884 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1885 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1887 switch (Inst.getOpcode()) {
1888 case Mips::SWM32_MM:
1889 case Mips::LWM32_MM:
1890 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1891 == MCDisassembler::Fail)
1892 return MCDisassembler::Fail;
1893 Inst.addOperand(MCOperand::createReg(Base));
1894 Inst.addOperand(MCOperand::createImm(Offset));
1897 Inst.addOperand(MCOperand::createReg(Reg));
1900 Inst.addOperand(MCOperand::createReg(Reg));
1901 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1902 Inst.addOperand(MCOperand::createReg(Reg+1));
1904 Inst.addOperand(MCOperand::createReg(Base));
1905 Inst.addOperand(MCOperand::createImm(Offset));
1908 return MCDisassembler::Success;
1911 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1914 const void *Decoder) {
1915 int Offset = SignExtend32<16>(Insn & 0xffff);
1916 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1917 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1919 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1920 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1922 Inst.addOperand(MCOperand::createReg(Reg));
1923 Inst.addOperand(MCOperand::createReg(Base));
1924 Inst.addOperand(MCOperand::createImm(Offset));
1926 return MCDisassembler::Success;
1929 static DecodeStatus DecodeFMem(MCInst &Inst,
1932 const void *Decoder) {
1933 int Offset = SignExtend32<16>(Insn & 0xffff);
1934 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1935 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1937 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1938 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1940 Inst.addOperand(MCOperand::createReg(Reg));
1941 Inst.addOperand(MCOperand::createReg(Base));
1942 Inst.addOperand(MCOperand::createImm(Offset));
1944 return MCDisassembler::Success;
1947 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
1948 uint64_t Address, const void *Decoder) {
1949 // This function is the same as DecodeFMem but with the Reg and Base fields
1950 // swapped according to microMIPS spec.
1951 int Offset = SignExtend32<16>(Insn & 0xffff);
1952 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1953 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1955 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1956 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1958 Inst.addOperand(MCOperand::createReg(Reg));
1959 Inst.addOperand(MCOperand::createReg(Base));
1960 Inst.addOperand(MCOperand::createImm(Offset));
1962 return MCDisassembler::Success;
1965 static DecodeStatus DecodeFMem2(MCInst &Inst,
1968 const void *Decoder) {
1969 int Offset = SignExtend32<16>(Insn & 0xffff);
1970 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1971 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1973 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1974 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1976 Inst.addOperand(MCOperand::createReg(Reg));
1977 Inst.addOperand(MCOperand::createReg(Base));
1978 Inst.addOperand(MCOperand::createImm(Offset));
1980 return MCDisassembler::Success;
1983 static DecodeStatus DecodeFMem3(MCInst &Inst,
1986 const void *Decoder) {
1987 int Offset = SignExtend32<16>(Insn & 0xffff);
1988 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1989 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1991 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1992 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1994 Inst.addOperand(MCOperand::createReg(Reg));
1995 Inst.addOperand(MCOperand::createReg(Base));
1996 Inst.addOperand(MCOperand::createImm(Offset));
1998 return MCDisassembler::Success;
2001 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
2004 const void *Decoder) {
2005 int Offset = SignExtend32<11>(Insn & 0x07ff);
2006 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
2007 unsigned Base = fieldFromInstruction(Insn, 11, 5);
2009 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2010 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2012 Inst.addOperand(MCOperand::createReg(Reg));
2013 Inst.addOperand(MCOperand::createReg(Base));
2014 Inst.addOperand(MCOperand::createImm(Offset));
2016 return MCDisassembler::Success;
2019 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
2020 uint64_t Address, const void *Decoder) {
2021 int Offset = SignExtend32<11>(Insn & 0x07ff);
2022 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
2023 unsigned Base = fieldFromInstruction(Insn, 16, 5);
2025 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2026 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2028 Inst.addOperand(MCOperand::createReg(Reg));
2029 Inst.addOperand(MCOperand::createReg(Base));
2030 Inst.addOperand(MCOperand::createImm(Offset));
2032 return MCDisassembler::Success;
2035 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
2038 const void *Decoder) {
2039 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
2040 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
2041 unsigned Base = fieldFromInstruction(Insn, 21, 5);
2043 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
2044 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2046 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
2047 Inst.addOperand(MCOperand::createReg(Rt));
2050 Inst.addOperand(MCOperand::createReg(Rt));
2051 Inst.addOperand(MCOperand::createReg(Base));
2052 Inst.addOperand(MCOperand::createImm(Offset));
2054 return MCDisassembler::Success;
2057 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
2060 const void *Decoder) {
2061 // Currently only hardware register 29 is supported.
2063 return MCDisassembler::Fail;
2064 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
2065 return MCDisassembler::Success;
2068 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
2071 const void *Decoder) {
2072 if (RegNo > 30 || RegNo %2)
2073 return MCDisassembler::Fail;
2075 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
2076 Inst.addOperand(MCOperand::createReg(Reg));
2077 return MCDisassembler::Success;
2080 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
2083 const void *Decoder) {
2085 return MCDisassembler::Fail;
2087 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
2088 Inst.addOperand(MCOperand::createReg(Reg));
2089 return MCDisassembler::Success;
2092 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
2095 const void *Decoder) {
2097 return MCDisassembler::Fail;
2099 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
2100 Inst.addOperand(MCOperand::createReg(Reg));
2101 return MCDisassembler::Success;
2104 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
2107 const void *Decoder) {
2109 return MCDisassembler::Fail;
2111 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
2112 Inst.addOperand(MCOperand::createReg(Reg));
2113 return MCDisassembler::Success;
2116 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
2119 const void *Decoder) {
2121 return MCDisassembler::Fail;
2123 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
2124 Inst.addOperand(MCOperand::createReg(Reg));
2125 return MCDisassembler::Success;
2128 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
2131 const void *Decoder) {
2133 return MCDisassembler::Fail;
2135 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
2136 Inst.addOperand(MCOperand::createReg(Reg));
2137 return MCDisassembler::Success;
2140 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
2143 const void *Decoder) {
2145 return MCDisassembler::Fail;
2147 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
2148 Inst.addOperand(MCOperand::createReg(Reg));
2149 return MCDisassembler::Success;
2152 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
2155 const void *Decoder) {
2157 return MCDisassembler::Fail;
2159 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
2160 Inst.addOperand(MCOperand::createReg(Reg));
2161 return MCDisassembler::Success;
2164 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
2167 const void *Decoder) {
2169 return MCDisassembler::Fail;
2171 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
2172 Inst.addOperand(MCOperand::createReg(Reg));
2173 return MCDisassembler::Success;
2176 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
2179 const void *Decoder) {
2181 return MCDisassembler::Fail;
2183 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
2184 Inst.addOperand(MCOperand::createReg(Reg));
2185 return MCDisassembler::Success;
2188 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
2191 const void *Decoder) {
2193 return MCDisassembler::Fail;
2195 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
2196 Inst.addOperand(MCOperand::createReg(Reg));
2197 return MCDisassembler::Success;
2200 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
2203 const void *Decoder) {
2204 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
2205 Inst.addOperand(MCOperand::createImm(BranchOffset));
2206 return MCDisassembler::Success;
2209 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
2212 const void *Decoder) {
2213 int32_t BranchOffset = (SignExtend32<16>(Offset) * 2);
2214 Inst.addOperand(MCOperand::createImm(BranchOffset));
2215 return MCDisassembler::Success;
2218 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
2221 const void *Decoder) {
2222 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
2223 Inst.addOperand(MCOperand::createImm(JumpOffset));
2224 return MCDisassembler::Success;
2227 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
2230 const void *Decoder) {
2231 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2233 Inst.addOperand(MCOperand::createImm(BranchOffset));
2234 return MCDisassembler::Success;
2237 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
2240 const void *Decoder) {
2241 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2243 Inst.addOperand(MCOperand::createImm(BranchOffset));
2244 return MCDisassembler::Success;
2247 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
2250 const void *Decoder) {
2251 int32_t BranchOffset = SignExtend32<26>(Offset) * 4 + 4;
2253 Inst.addOperand(MCOperand::createImm(BranchOffset));
2254 return MCDisassembler::Success;
2257 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
2260 const void *Decoder) {
2261 int32_t BranchOffset = SignExtend32<8>(Offset << 1);
2262 Inst.addOperand(MCOperand::createImm(BranchOffset));
2263 return MCDisassembler::Success;
2266 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
2269 const void *Decoder) {
2270 int32_t BranchOffset = SignExtend32<11>(Offset << 1);
2271 Inst.addOperand(MCOperand::createImm(BranchOffset));
2272 return MCDisassembler::Success;
2275 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
2278 const void *Decoder) {
2279 int32_t BranchOffset = SignExtend32<16>(Offset) * 2 + 4;
2280 Inst.addOperand(MCOperand::createImm(BranchOffset));
2281 return MCDisassembler::Success;
2284 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
2287 const void *Decoder) {
2288 int32_t BranchOffset = SignExtend32<27>(Offset << 1);
2290 Inst.addOperand(MCOperand::createImm(BranchOffset));
2291 return MCDisassembler::Success;
2294 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
2297 const void *Decoder) {
2298 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
2299 Inst.addOperand(MCOperand::createImm(JumpOffset));
2300 return MCDisassembler::Success;
2303 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
2306 const void *Decoder) {
2308 Inst.addOperand(MCOperand::createImm(1));
2309 else if (Value == 0x7)
2310 Inst.addOperand(MCOperand::createImm(-1));
2312 Inst.addOperand(MCOperand::createImm(Value << 2));
2313 return MCDisassembler::Success;
2316 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
2319 const void *Decoder) {
2321 Inst.addOperand(MCOperand::createImm(-1));
2323 Inst.addOperand(MCOperand::createImm(Value));
2324 return MCDisassembler::Success;
2327 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
2330 const void *Decoder) {
2331 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
2332 return MCDisassembler::Success;
2335 template <unsigned Bits, int Offset, int Scale>
2336 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2338 const void *Decoder) {
2339 Value &= ((1 << Bits) - 1);
2341 Inst.addOperand(MCOperand::createImm(Value + Offset));
2342 return MCDisassembler::Success;
2345 template <unsigned Bits, int Offset, int ScaleBy>
2346 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2348 const void *Decoder) {
2349 int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
2350 Inst.addOperand(MCOperand::createImm(Imm + Offset));
2351 return MCDisassembler::Success;
2354 static DecodeStatus DecodeInsSize(MCInst &Inst,
2357 const void *Decoder) {
2358 // First we need to grab the pos(lsb) from MCInst.
2359 // This function only handles the 32 bit variants of ins, as dins
2360 // variants are handled differently.
2361 int Pos = Inst.getOperand(2).getImm();
2362 int Size = (int) Insn - Pos + 1;
2363 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
2364 return MCDisassembler::Success;
2367 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
2368 uint64_t Address, const void *Decoder) {
2369 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
2370 return MCDisassembler::Success;
2373 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
2374 uint64_t Address, const void *Decoder) {
2375 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
2376 return MCDisassembler::Success;
2379 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
2380 uint64_t Address, const void *Decoder) {
2381 int32_t DecodedValue;
2383 case 0: DecodedValue = 256; break;
2384 case 1: DecodedValue = 257; break;
2385 case 510: DecodedValue = -258; break;
2386 case 511: DecodedValue = -257; break;
2387 default: DecodedValue = SignExtend32<9>(Insn); break;
2389 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
2390 return MCDisassembler::Success;
2393 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
2394 uint64_t Address, const void *Decoder) {
2395 // Insn must be >= 0, since it is unsigned that condition is always true.
2397 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2399 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
2400 return MCDisassembler::Success;
2403 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2406 const void *Decoder) {
2407 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2408 Mips::S6, Mips::S7, Mips::FP};
2411 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2413 // Empty register lists are not allowed.
2415 return MCDisassembler::Fail;
2417 RegNum = RegLst & 0xf;
2419 // RegLst values 10-15, and 26-31 are reserved.
2421 return MCDisassembler::Fail;
2423 for (unsigned i = 0; i < RegNum; i++)
2424 Inst.addOperand(MCOperand::createReg(Regs[i]));
2427 Inst.addOperand(MCOperand::createReg(Mips::RA));
2429 return MCDisassembler::Success;
2432 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2434 const void *Decoder) {
2435 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2437 switch(Inst.getOpcode()) {
2439 RegLst = fieldFromInstruction(Insn, 4, 2);
2441 case Mips::LWM16_MMR6:
2442 case Mips::SWM16_MMR6:
2443 RegLst = fieldFromInstruction(Insn, 8, 2);
2446 unsigned RegNum = RegLst & 0x3;
2448 for (unsigned i = 0; i <= RegNum; i++)
2449 Inst.addOperand(MCOperand::createReg(Regs[i]));
2451 Inst.addOperand(MCOperand::createReg(Mips::RA));
2453 return MCDisassembler::Success;
2456 static DecodeStatus DecodeMovePOperands(MCInst &Inst, unsigned Insn,
2458 const void *Decoder) {
2459 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2460 if (DecodeMovePRegPair(Inst, RegPair, Address, Decoder) ==
2461 MCDisassembler::Fail)
2462 return MCDisassembler::Fail;
2465 if (static_cast<const MipsDisassembler*>(Decoder)->hasMips32r6())
2466 RegRs = fieldFromInstruction(Insn, 0, 2) |
2467 (fieldFromInstruction(Insn, 3, 1) << 2);
2469 RegRs = fieldFromInstruction(Insn, 1, 3);
2470 if (DecodeGPRMM16MovePRegisterClass(Inst, RegRs, Address, Decoder) ==
2471 MCDisassembler::Fail)
2472 return MCDisassembler::Fail;
2474 unsigned RegRt = fieldFromInstruction(Insn, 4, 3);
2475 if (DecodeGPRMM16MovePRegisterClass(Inst, RegRt, Address, Decoder) ==
2476 MCDisassembler::Fail)
2477 return MCDisassembler::Fail;
2479 return MCDisassembler::Success;
2482 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned RegPair,
2483 uint64_t Address, const void *Decoder) {
2486 return MCDisassembler::Fail;
2488 Inst.addOperand(MCOperand::createReg(Mips::A1));
2489 Inst.addOperand(MCOperand::createReg(Mips::A2));
2492 Inst.addOperand(MCOperand::createReg(Mips::A1));
2493 Inst.addOperand(MCOperand::createReg(Mips::A3));
2496 Inst.addOperand(MCOperand::createReg(Mips::A2));
2497 Inst.addOperand(MCOperand::createReg(Mips::A3));
2500 Inst.addOperand(MCOperand::createReg(Mips::A0));
2501 Inst.addOperand(MCOperand::createReg(Mips::S5));
2504 Inst.addOperand(MCOperand::createReg(Mips::A0));
2505 Inst.addOperand(MCOperand::createReg(Mips::S6));
2508 Inst.addOperand(MCOperand::createReg(Mips::A0));
2509 Inst.addOperand(MCOperand::createReg(Mips::A1));
2512 Inst.addOperand(MCOperand::createReg(Mips::A0));
2513 Inst.addOperand(MCOperand::createReg(Mips::A2));
2516 Inst.addOperand(MCOperand::createReg(Mips::A0));
2517 Inst.addOperand(MCOperand::createReg(Mips::A3));
2521 return MCDisassembler::Success;
2524 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2525 uint64_t Address, const void *Decoder) {
2526 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2527 return MCDisassembler::Success;
2530 template <typename InsnType>
2531 static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
2533 const void *Decoder) {
2535 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2536 // Invalid if rt == 0
2537 // BGTZALC_MMR6 if rs == 0 && rt != 0
2538 // BLTZALC_MMR6 if rs != 0 && rs == rt
2539 // BLTUC_MMR6 if rs != 0 && rs != rt
2541 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2542 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2548 return MCDisassembler::Fail;
2550 MI.setOpcode(Mips::BGTZALC_MMR6);
2552 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2554 else if (Rs == Rt) {
2555 MI.setOpcode(Mips::BLTZALC_MMR6);
2557 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2560 MI.setOpcode(Mips::BLTUC_MMR6);
2563 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2568 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2572 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2574 MI.addOperand(MCOperand::createImm(Imm));
2576 return MCDisassembler::Success;
2579 template <typename InsnType>
2580 static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
2582 const void *Decoder) {
2584 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2585 // Invalid if rt == 0
2586 // BLEZALC_MMR6 if rs == 0 && rt != 0
2587 // BGEZALC_MMR6 if rs == rt && rt != 0
2588 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2590 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2591 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2596 return MCDisassembler::Fail;
2598 MI.setOpcode(Mips::BLEZALC_MMR6);
2599 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2601 else if (Rs == Rt) {
2602 MI.setOpcode(Mips::BGEZALC_MMR6);
2603 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2607 MI.setOpcode(Mips::BGEUC_MMR6);
2608 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2613 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2615 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2617 MI.addOperand(MCOperand::createImm(Imm));
2619 return MCDisassembler::Success;