1 //===- MipsDisassembler.cpp - Disassembler for Mips -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is part of the Mips Disassembler.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/MipsMCTargetDesc.h"
15 #include "TargetInfo/MipsTargetInfo.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19 #include "llvm/MC/MCFixedLenDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "mips-disassembler"
36 using DecodeStatus = MCDisassembler::DecodeStatus;
40 class MipsDisassembler : public MCDisassembler {
45 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
46 : MCDisassembler(STI, Ctx),
47 IsMicroMips(STI.getFeatureBits()[Mips::FeatureMicroMips]),
48 IsBigEndian(IsBigEndian) {}
50 bool hasMips2() const { return STI.getFeatureBits()[Mips::FeatureMips2]; }
51 bool hasMips3() const { return STI.getFeatureBits()[Mips::FeatureMips3]; }
52 bool hasMips32() const { return STI.getFeatureBits()[Mips::FeatureMips32]; }
54 bool hasMips32r6() const {
55 return STI.getFeatureBits()[Mips::FeatureMips32r6];
58 bool isFP64() const { return STI.getFeatureBits()[Mips::FeatureFP64Bit]; }
60 bool isGP64() const { return STI.getFeatureBits()[Mips::FeatureGP64Bit]; }
62 bool isPTR64() const { return STI.getFeatureBits()[Mips::FeaturePTR64Bit]; }
64 bool hasCnMips() const { return STI.getFeatureBits()[Mips::FeatureCnMips]; }
66 bool hasCOP3() const {
67 // Only present in MIPS-I and MIPS-II
68 return !hasMips32() && !hasMips3();
71 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
72 ArrayRef<uint8_t> Bytes, uint64_t Address,
74 raw_ostream &CStream) const override;
77 } // end anonymous namespace
79 // Forward declare these because the autogenerated code will reference them.
80 // Definitions are further down.
81 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
86 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
91 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
96 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
101 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
104 const void *Decoder);
106 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
109 const void *Decoder);
111 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
114 const void *Decoder);
116 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
119 const void *Decoder);
121 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
124 const void *Decoder);
126 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
129 const void *Decoder);
131 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
134 const void *Decoder);
136 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
139 const void *Decoder);
141 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
143 const void *Decoder);
145 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
148 const void *Decoder);
150 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
153 const void *Decoder);
155 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
158 const void *Decoder);
160 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
163 const void *Decoder);
165 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
168 const void *Decoder);
170 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
173 const void *Decoder);
175 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
178 const void *Decoder);
180 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
183 const void *Decoder);
185 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
188 const void *Decoder);
190 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
193 const void *Decoder);
195 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
198 const void *Decoder);
200 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
203 const void *Decoder);
205 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
208 const void *Decoder);
210 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
213 const void *Decoder);
215 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
218 const void *Decoder);
220 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
223 const void *Decoder);
225 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
228 const void *Decoder);
230 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
233 const void *Decoder);
235 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
236 // shifted left by 1 bit.
237 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
240 const void *Decoder);
242 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
243 // shifted left by 1 bit.
244 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
247 const void *Decoder);
249 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
250 // shifted left by 1 bit.
251 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
254 const void *Decoder);
256 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
257 // shifted left by 1 bit.
258 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
261 const void *Decoder);
263 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
264 // shifted left by 1 bit.
265 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
268 const void *Decoder);
270 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);
544 static MCDisassembler *createMipsDisassembler(
546 const MCSubtargetInfo &STI,
548 return new MipsDisassembler(STI, Ctx, true);
551 static MCDisassembler *createMipselDisassembler(
553 const MCSubtargetInfo &STI,
555 return new MipsDisassembler(STI, Ctx, false);
558 extern "C" void LLVMInitializeMipsDisassembler() {
559 // Register the disassembler.
560 TargetRegistry::RegisterMCDisassembler(getTheMipsTarget(),
561 createMipsDisassembler);
562 TargetRegistry::RegisterMCDisassembler(getTheMipselTarget(),
563 createMipselDisassembler);
564 TargetRegistry::RegisterMCDisassembler(getTheMips64Target(),
565 createMipsDisassembler);
566 TargetRegistry::RegisterMCDisassembler(getTheMips64elTarget(),
567 createMipselDisassembler);
570 #include "MipsGenDisassemblerTables.inc"
572 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
573 const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
574 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
575 return *(RegInfo->getRegClass(RC).begin() + RegNo);
578 template <typename InsnType>
579 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
580 const void *Decoder) {
581 using DecodeFN = DecodeStatus (*)(MCInst &, unsigned, uint64_t, const void *);
583 // The size of the n field depends on the element size
584 // The register class also depends on this.
585 InsnType tmp = fieldFromInstruction(insn, 17, 5);
587 DecodeFN RegDecoder = nullptr;
588 if ((tmp & 0x18) == 0x00) { // INSVE_B
590 RegDecoder = DecodeMSA128BRegisterClass;
591 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
593 RegDecoder = DecodeMSA128HRegisterClass;
594 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
596 RegDecoder = DecodeMSA128WRegisterClass;
597 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
599 RegDecoder = DecodeMSA128DRegisterClass;
601 llvm_unreachable("Invalid encoding");
603 assert(NSize != 0 && RegDecoder != nullptr);
606 tmp = fieldFromInstruction(insn, 6, 5);
607 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
608 return MCDisassembler::Fail;
610 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
611 return MCDisassembler::Fail;
613 tmp = fieldFromInstruction(insn, 16, NSize);
614 MI.addOperand(MCOperand::createImm(tmp));
616 tmp = fieldFromInstruction(insn, 11, 5);
617 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
618 return MCDisassembler::Fail;
620 MI.addOperand(MCOperand::createImm(0));
622 return MCDisassembler::Success;
625 template <typename InsnType>
626 static DecodeStatus DecodeDAHIDATIMMR6(MCInst &MI, InsnType insn, uint64_t Address,
627 const void *Decoder) {
628 InsnType Rs = fieldFromInstruction(insn, 16, 5);
629 InsnType Imm = fieldFromInstruction(insn, 0, 16);
630 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
632 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
634 MI.addOperand(MCOperand::createImm(Imm));
636 return MCDisassembler::Success;
639 template <typename InsnType>
640 static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
641 const void *Decoder) {
642 InsnType Rs = fieldFromInstruction(insn, 21, 5);
643 InsnType Imm = fieldFromInstruction(insn, 0, 16);
644 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
646 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID,
648 MI.addOperand(MCOperand::createImm(Imm));
650 return MCDisassembler::Success;
653 template <typename InsnType>
654 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
656 const void *Decoder) {
657 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
658 // (otherwise we would have matched the ADDI instruction from the earlier
662 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
664 // BEQZALC if rs == 0 && rt != 0
665 // BEQC if rs < rt && rs != 0
667 InsnType Rs = fieldFromInstruction(insn, 21, 5);
668 InsnType Rt = fieldFromInstruction(insn, 16, 5);
669 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
673 MI.setOpcode(Mips::BOVC);
675 } else if (Rs != 0 && Rs < Rt) {
676 MI.setOpcode(Mips::BEQC);
679 MI.setOpcode(Mips::BEQZALC);
682 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
685 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
687 MI.addOperand(MCOperand::createImm(Imm));
689 return MCDisassembler::Success;
692 template <typename InsnType>
693 static DecodeStatus DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn,
695 const void *Decoder) {
696 InsnType Rt = fieldFromInstruction(insn, 21, 5);
697 InsnType Rs = fieldFromInstruction(insn, 16, 5);
701 MI.setOpcode(Mips::BOVC_MMR6);
702 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
704 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
706 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
707 } else if (Rs != 0 && Rs < Rt) {
708 MI.setOpcode(Mips::BEQC_MMR6);
709 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
711 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
713 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
715 MI.setOpcode(Mips::BEQZALC_MMR6);
716 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
718 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
721 MI.addOperand(MCOperand::createImm(Imm));
723 return MCDisassembler::Success;
726 template <typename InsnType>
727 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
729 const void *Decoder) {
730 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
731 // (otherwise we would have matched the ADDI instruction from the earlier
735 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
737 // BNEZALC if rs == 0 && rt != 0
738 // BNEC if rs < rt && rs != 0
740 InsnType Rs = fieldFromInstruction(insn, 21, 5);
741 InsnType Rt = fieldFromInstruction(insn, 16, 5);
742 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
746 MI.setOpcode(Mips::BNVC);
748 } else if (Rs != 0 && Rs < Rt) {
749 MI.setOpcode(Mips::BNEC);
752 MI.setOpcode(Mips::BNEZALC);
755 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
758 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
760 MI.addOperand(MCOperand::createImm(Imm));
762 return MCDisassembler::Success;
765 template <typename InsnType>
766 static DecodeStatus DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn,
768 const void *Decoder) {
769 InsnType Rt = fieldFromInstruction(insn, 21, 5);
770 InsnType Rs = fieldFromInstruction(insn, 16, 5);
774 MI.setOpcode(Mips::BNVC_MMR6);
775 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
777 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
779 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
780 } else if (Rs != 0 && Rs < Rt) {
781 MI.setOpcode(Mips::BNEC_MMR6);
782 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
784 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
786 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
788 MI.setOpcode(Mips::BNEZALC_MMR6);
789 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
791 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
794 MI.addOperand(MCOperand::createImm(Imm));
796 return MCDisassembler::Success;
799 template <typename InsnType>
800 static DecodeStatus DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn,
802 const void *Decoder) {
804 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
805 // Invalid if rt == 0
806 // BGTZC_MMR6 if rs == 0 && rt != 0
807 // BLTZC_MMR6 if rs == rt && rt != 0
808 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
810 InsnType Rt = fieldFromInstruction(insn, 21, 5);
811 InsnType Rs = fieldFromInstruction(insn, 16, 5);
812 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
816 return MCDisassembler::Fail;
818 MI.setOpcode(Mips::BGTZC_MMR6);
820 MI.setOpcode(Mips::BLTZC_MMR6);
822 MI.setOpcode(Mips::BLTC_MMR6);
827 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
830 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
833 MI.addOperand(MCOperand::createImm(Imm));
835 return MCDisassembler::Success;
838 template <typename InsnType>
839 static DecodeStatus DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn,
841 const void *Decoder) {
843 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
844 // Invalid if rt == 0
845 // BLEZC_MMR6 if rs == 0 && rt != 0
846 // BGEZC_MMR6 if rs == rt && rt != 0
847 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
849 InsnType Rt = fieldFromInstruction(insn, 21, 5);
850 InsnType Rs = fieldFromInstruction(insn, 16, 5);
851 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
855 return MCDisassembler::Fail;
857 MI.setOpcode(Mips::BLEZC_MMR6);
859 MI.setOpcode(Mips::BGEZC_MMR6);
862 MI.setOpcode(Mips::BGEC_MMR6);
866 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
869 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
872 MI.addOperand(MCOperand::createImm(Imm));
874 return MCDisassembler::Success;
877 template <typename InsnType>
878 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
880 const void *Decoder) {
881 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
882 // (otherwise we would have matched the BLEZL instruction from the earlier
886 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
887 // Invalid if rs == 0
888 // BLEZC if rs == 0 && rt != 0
889 // BGEZC if rs == rt && rt != 0
890 // BGEC if rs != rt && rs != 0 && rt != 0
892 InsnType Rs = fieldFromInstruction(insn, 21, 5);
893 InsnType Rt = fieldFromInstruction(insn, 16, 5);
894 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
898 return MCDisassembler::Fail;
900 MI.setOpcode(Mips::BLEZC);
902 MI.setOpcode(Mips::BGEZC);
905 MI.setOpcode(Mips::BGEC);
909 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
912 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
915 MI.addOperand(MCOperand::createImm(Imm));
917 return MCDisassembler::Success;
920 template <typename InsnType>
921 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
923 const void *Decoder) {
924 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
925 // (otherwise we would have matched the BGTZL instruction from the earlier
929 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
930 // Invalid if rs == 0
931 // BGTZC if rs == 0 && rt != 0
932 // BLTZC if rs == rt && rt != 0
933 // BLTC if rs != rt && rs != 0 && rt != 0
937 InsnType Rs = fieldFromInstruction(insn, 21, 5);
938 InsnType Rt = fieldFromInstruction(insn, 16, 5);
939 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
942 return MCDisassembler::Fail;
944 MI.setOpcode(Mips::BGTZC);
946 MI.setOpcode(Mips::BLTZC);
948 MI.setOpcode(Mips::BLTC);
953 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
956 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
959 MI.addOperand(MCOperand::createImm(Imm));
961 return MCDisassembler::Success;
964 template <typename InsnType>
965 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
967 const void *Decoder) {
968 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
969 // (otherwise we would have matched the BGTZ instruction from the earlier
973 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
975 // BGTZALC if rs == 0 && rt != 0
976 // BLTZALC if rs != 0 && rs == rt
977 // BLTUC if rs != 0 && rs != rt
979 InsnType Rs = fieldFromInstruction(insn, 21, 5);
980 InsnType Rt = fieldFromInstruction(insn, 16, 5);
981 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
986 MI.setOpcode(Mips::BGTZ);
988 } else if (Rs == 0) {
989 MI.setOpcode(Mips::BGTZALC);
991 } else if (Rs == Rt) {
992 MI.setOpcode(Mips::BLTZALC);
995 MI.setOpcode(Mips::BLTUC);
1001 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1005 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1008 MI.addOperand(MCOperand::createImm(Imm));
1010 return MCDisassembler::Success;
1013 template <typename InsnType>
1014 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
1016 const void *Decoder) {
1017 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
1018 // (otherwise we would have matched the BLEZL instruction from the earlier
1022 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
1023 // Invalid if rs == 0
1024 // BLEZALC if rs == 0 && rt != 0
1025 // BGEZALC if rs == rt && rt != 0
1026 // BGEUC if rs != rt && rs != 0 && rt != 0
1028 InsnType Rs = fieldFromInstruction(insn, 21, 5);
1029 InsnType Rt = fieldFromInstruction(insn, 16, 5);
1030 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1034 return MCDisassembler::Fail;
1036 MI.setOpcode(Mips::BLEZALC);
1038 MI.setOpcode(Mips::BGEZALC);
1041 MI.setOpcode(Mips::BGEUC);
1045 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1047 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1050 MI.addOperand(MCOperand::createImm(Imm));
1052 return MCDisassembler::Success;
1055 // Override the generated disassembler to produce DEXT all the time. This is
1056 // for feature / behaviour parity with binutils.
1057 template <typename InsnType>
1058 static DecodeStatus DecodeDEXT(MCInst &MI, InsnType Insn, uint64_t Address,
1059 const void *Decoder) {
1060 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1061 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1065 switch (MI.getOpcode()) {
1072 Size = Msbd + 1 + 32;
1079 llvm_unreachable("Unknown DEXT instruction!");
1082 MI.setOpcode(Mips::DEXT);
1084 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1085 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1087 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1088 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1089 MI.addOperand(MCOperand::createImm(Pos));
1090 MI.addOperand(MCOperand::createImm(Size));
1092 return MCDisassembler::Success;
1095 // Override the generated disassembler to produce DINS all the time. This is
1096 // for feature / behaviour parity with binutils.
1097 template <typename InsnType>
1098 static DecodeStatus DecodeDINS(MCInst &MI, InsnType Insn, uint64_t Address,
1099 const void *Decoder) {
1100 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
1101 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
1105 switch (MI.getOpcode()) {
1108 Size = Msbd + 1 - Pos;
1112 Size = Msbd + 33 - Pos;
1116 // mbsd = pos + size - 33
1117 // mbsd - pos + 33 = size
1118 Size = Msbd + 33 - Pos;
1121 llvm_unreachable("Unknown DINS instruction!");
1124 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1125 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1127 MI.setOpcode(Mips::DINS);
1128 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rt)));
1129 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR64RegClassID, Rs)));
1130 MI.addOperand(MCOperand::createImm(Pos));
1131 MI.addOperand(MCOperand::createImm(Size));
1133 return MCDisassembler::Success;
1136 // Auto-generated decoder wouldn't add the third operand for CRC32*.
1137 template <typename InsnType>
1138 static DecodeStatus DecodeCRC(MCInst &MI, InsnType Insn, uint64_t Address,
1139 const void *Decoder) {
1140 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
1141 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
1142 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1144 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1146 MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
1148 return MCDisassembler::Success;
1151 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1152 /// according to the given endianness.
1153 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
1154 uint64_t &Size, uint32_t &Insn,
1156 // We want to read exactly 2 Bytes of data.
1157 if (Bytes.size() < 2) {
1159 return MCDisassembler::Fail;
1163 Insn = (Bytes[0] << 8) | Bytes[1];
1165 Insn = (Bytes[1] << 8) | Bytes[0];
1168 return MCDisassembler::Success;
1171 /// Read four bytes from the ArrayRef and return 32 bit word sorted
1172 /// according to the given endianness.
1173 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
1174 uint64_t &Size, uint32_t &Insn,
1175 bool IsBigEndian, bool IsMicroMips) {
1176 // We want to read exactly 4 Bytes of data.
1177 if (Bytes.size() < 4) {
1179 return MCDisassembler::Fail;
1182 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1183 // always precede the low 16 bits in the instruction stream (that is, they
1184 // are placed at lower addresses in the instruction stream).
1186 // microMIPS byte ordering:
1187 // Big-endian: 0 | 1 | 2 | 3
1188 // Little-endian: 1 | 0 | 3 | 2
1191 // Encoded as a big-endian 32-bit word in the stream.
1193 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
1196 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
1199 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
1204 return MCDisassembler::Success;
1207 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1208 ArrayRef<uint8_t> Bytes,
1210 raw_ostream &VStream,
1211 raw_ostream &CStream) const {
1213 DecodeStatus Result;
1217 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
1218 if (Result == MCDisassembler::Fail)
1219 return MCDisassembler::Fail;
1221 if (hasMips32r6()) {
1223 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1224 // Calling the auto-generated decoder function for microMIPS32R6
1225 // 16-bit instructions.
1226 Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
1227 Address, this, STI);
1228 if (Result != MCDisassembler::Fail) {
1234 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1235 // Calling the auto-generated decoder function for microMIPS 16-bit
1237 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
1239 if (Result != MCDisassembler::Fail) {
1244 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
1245 if (Result == MCDisassembler::Fail)
1246 return MCDisassembler::Fail;
1248 if (hasMips32r6()) {
1250 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1251 // Calling the auto-generated decoder function.
1252 Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
1254 if (Result != MCDisassembler::Fail) {
1260 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1261 // Calling the auto-generated decoder function.
1262 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
1264 if (Result != MCDisassembler::Fail) {
1270 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1271 Result = decodeInstruction(DecoderTableMicroMipsFP6432, Instr, Insn,
1272 Address, this, STI);
1273 if (Result != MCDisassembler::Fail) {
1279 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1280 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1281 // could form a valid instruction. The two bytes we rejected as an
1282 // instruction could have actually beeen an inline constant pool that is
1283 // unconditionally branched over.
1285 return MCDisassembler::Fail;
1288 // Attempt to read the instruction so that we can attempt to decode it. If
1289 // the buffer is not 4 bytes long, let the higher level logic figure out
1290 // what to do with a size of zero and MCDisassembler::Fail.
1291 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
1292 if (Result == MCDisassembler::Fail)
1293 return MCDisassembler::Fail;
1295 // The only instruction size for standard encoded MIPS.
1299 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1301 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
1302 if (Result != MCDisassembler::Fail)
1306 if (hasMips32r6() && isGP64()) {
1308 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1309 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
1310 Address, this, STI);
1311 if (Result != MCDisassembler::Fail)
1315 if (hasMips32r6() && isPTR64()) {
1317 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1318 Result = decodeInstruction(DecoderTableMips32r6_64r6_PTR6432, Instr, Insn,
1319 Address, this, STI);
1320 if (Result != MCDisassembler::Fail)
1324 if (hasMips32r6()) {
1325 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1326 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
1327 Address, this, STI);
1328 if (Result != MCDisassembler::Fail)
1332 if (hasMips2() && isPTR64()) {
1334 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1335 Result = decodeInstruction(DecoderTableMips32_64_PTR6432, Instr, Insn,
1336 Address, this, STI);
1337 if (Result != MCDisassembler::Fail)
1342 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1343 Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
1344 Address, this, STI);
1345 if (Result != MCDisassembler::Fail)
1350 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1351 Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
1352 Address, this, STI);
1353 if (Result != MCDisassembler::Fail)
1359 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1360 Result = decodeInstruction(DecoderTableMipsFP6432, Instr, Insn,
1361 Address, this, STI);
1362 if (Result != MCDisassembler::Fail)
1366 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1367 // Calling the auto-generated decoder function.
1369 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
1370 if (Result != MCDisassembler::Fail)
1373 return MCDisassembler::Fail;
1376 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
1379 const void *Decoder) {
1380 return MCDisassembler::Fail;
1383 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1386 const void *Decoder) {
1388 return MCDisassembler::Fail;
1390 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1391 Inst.addOperand(MCOperand::createReg(Reg));
1392 return MCDisassembler::Success;
1395 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1398 const void *Decoder) {
1400 return MCDisassembler::Fail;
1401 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1402 Inst.addOperand(MCOperand::createReg(Reg));
1403 return MCDisassembler::Success;
1406 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1409 const void *Decoder) {
1411 return MCDisassembler::Fail;
1412 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1413 Inst.addOperand(MCOperand::createReg(Reg));
1414 return MCDisassembler::Success;
1417 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1420 const void *Decoder) {
1422 return MCDisassembler::Fail;
1423 unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1424 Inst.addOperand(MCOperand::createReg(Reg));
1425 return MCDisassembler::Success;
1428 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1431 const void *Decoder) {
1433 return MCDisassembler::Fail;
1434 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1435 Inst.addOperand(MCOperand::createReg(Reg));
1436 return MCDisassembler::Success;
1439 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1442 const void *Decoder) {
1443 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1444 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1446 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1449 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1452 const void *Decoder) {
1453 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1456 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1459 const void *Decoder) {
1461 return MCDisassembler::Fail;
1463 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1464 Inst.addOperand(MCOperand::createReg(Reg));
1465 return MCDisassembler::Success;
1468 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1471 const void *Decoder) {
1473 return MCDisassembler::Fail;
1475 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1476 Inst.addOperand(MCOperand::createReg(Reg));
1477 return MCDisassembler::Success;
1480 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1483 const void *Decoder) {
1485 return MCDisassembler::Fail;
1486 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1487 Inst.addOperand(MCOperand::createReg(Reg));
1488 return MCDisassembler::Success;
1491 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1494 const void *Decoder) {
1496 return MCDisassembler::Fail;
1497 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1498 Inst.addOperand(MCOperand::createReg(Reg));
1499 return MCDisassembler::Success;
1502 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1504 const void *Decoder) {
1506 return MCDisassembler::Fail;
1508 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1509 Inst.addOperand(MCOperand::createReg(Reg));
1510 return MCDisassembler::Success;
1513 static DecodeStatus DecodeMem(MCInst &Inst,
1516 const void *Decoder) {
1517 int Offset = SignExtend32<16>(Insn & 0xffff);
1518 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1519 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1521 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1522 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1524 if (Inst.getOpcode() == Mips::SC ||
1525 Inst.getOpcode() == Mips::SCD)
1526 Inst.addOperand(MCOperand::createReg(Reg));
1528 Inst.addOperand(MCOperand::createReg(Reg));
1529 Inst.addOperand(MCOperand::createReg(Base));
1530 Inst.addOperand(MCOperand::createImm(Offset));
1532 return MCDisassembler::Success;
1535 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1538 const void *Decoder) {
1539 int Offset = SignExtend32<9>(Insn >> 7);
1540 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1541 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1543 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1544 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1546 if (Inst.getOpcode() == Mips::SCE)
1547 Inst.addOperand(MCOperand::createReg(Reg));
1549 Inst.addOperand(MCOperand::createReg(Reg));
1550 Inst.addOperand(MCOperand::createReg(Base));
1551 Inst.addOperand(MCOperand::createImm(Offset));
1553 return MCDisassembler::Success;
1556 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1559 const void *Decoder) {
1560 int Offset = SignExtend32<16>(Insn & 0xffff);
1561 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1562 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1564 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1565 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1567 Inst.addOperand(MCOperand::createReg(Reg));
1568 Inst.addOperand(MCOperand::createReg(Base));
1569 Inst.addOperand(MCOperand::createImm(Offset));
1571 return MCDisassembler::Success;
1574 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1577 const void *Decoder) {
1578 int Offset = SignExtend32<16>(Insn & 0xffff);
1579 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1580 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1582 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1584 Inst.addOperand(MCOperand::createReg(Base));
1585 Inst.addOperand(MCOperand::createImm(Offset));
1586 Inst.addOperand(MCOperand::createImm(Hint));
1588 return MCDisassembler::Success;
1591 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1594 const void *Decoder) {
1595 int Offset = SignExtend32<12>(Insn & 0xfff);
1596 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1597 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1599 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1601 Inst.addOperand(MCOperand::createReg(Base));
1602 Inst.addOperand(MCOperand::createImm(Offset));
1603 Inst.addOperand(MCOperand::createImm(Hint));
1605 return MCDisassembler::Success;
1608 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1611 const void *Decoder) {
1612 int Offset = SignExtend32<9>(Insn & 0x1ff);
1613 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1614 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1616 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1618 Inst.addOperand(MCOperand::createReg(Base));
1619 Inst.addOperand(MCOperand::createImm(Offset));
1620 Inst.addOperand(MCOperand::createImm(Hint));
1622 return MCDisassembler::Success;
1625 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1628 const void *Decoder) {
1629 int Offset = SignExtend32<9>(Insn >> 7);
1630 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1631 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1633 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1635 Inst.addOperand(MCOperand::createReg(Base));
1636 Inst.addOperand(MCOperand::createImm(Offset));
1637 Inst.addOperand(MCOperand::createImm(Hint));
1639 return MCDisassembler::Success;
1642 static DecodeStatus DecodeSyncI(MCInst &Inst,
1645 const void *Decoder) {
1646 int Offset = SignExtend32<16>(Insn & 0xffff);
1647 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1649 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1651 Inst.addOperand(MCOperand::createReg(Base));
1652 Inst.addOperand(MCOperand::createImm(Offset));
1654 return MCDisassembler::Success;
1657 static DecodeStatus DecodeSyncI_MM(MCInst &Inst, unsigned Insn,
1658 uint64_t Address, const void *Decoder) {
1659 int Offset = SignExtend32<16>(Insn & 0xffff);
1660 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1662 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1664 Inst.addOperand(MCOperand::createReg(Base));
1665 Inst.addOperand(MCOperand::createImm(Offset));
1667 return MCDisassembler::Success;
1670 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1673 const void *Decoder) {
1674 int Immediate = SignExtend32<16>(Insn & 0xffff);
1675 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1677 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1679 Inst.addOperand(MCOperand::createReg(Base));
1680 Inst.addOperand(MCOperand::createImm(Immediate));
1682 return MCDisassembler::Success;
1685 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1686 uint64_t Address, const void *Decoder) {
1687 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1688 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1689 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1691 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1692 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1694 Inst.addOperand(MCOperand::createReg(Reg));
1695 Inst.addOperand(MCOperand::createReg(Base));
1697 // The immediate field of an LD/ST instruction is scaled which means it must
1698 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1704 switch(Inst.getOpcode())
1707 assert(false && "Unexpected instruction");
1708 return MCDisassembler::Fail;
1712 Inst.addOperand(MCOperand::createImm(Offset));
1716 Inst.addOperand(MCOperand::createImm(Offset * 2));
1720 Inst.addOperand(MCOperand::createImm(Offset * 4));
1724 Inst.addOperand(MCOperand::createImm(Offset * 8));
1728 return MCDisassembler::Success;
1731 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1734 const void *Decoder) {
1735 unsigned Offset = Insn & 0xf;
1736 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1737 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1739 switch (Inst.getOpcode()) {
1740 case Mips::LBU16_MM:
1741 case Mips::LHU16_MM:
1743 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1744 == MCDisassembler::Fail)
1745 return MCDisassembler::Fail;
1748 case Mips::SB16_MMR6:
1750 case Mips::SH16_MMR6:
1752 case Mips::SW16_MMR6:
1753 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1754 == MCDisassembler::Fail)
1755 return MCDisassembler::Fail;
1759 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1760 == MCDisassembler::Fail)
1761 return MCDisassembler::Fail;
1763 switch (Inst.getOpcode()) {
1764 case Mips::LBU16_MM:
1766 Inst.addOperand(MCOperand::createImm(-1));
1768 Inst.addOperand(MCOperand::createImm(Offset));
1771 case Mips::SB16_MMR6:
1772 Inst.addOperand(MCOperand::createImm(Offset));
1774 case Mips::LHU16_MM:
1776 case Mips::SH16_MMR6:
1777 Inst.addOperand(MCOperand::createImm(Offset << 1));
1781 case Mips::SW16_MMR6:
1782 Inst.addOperand(MCOperand::createImm(Offset << 2));
1786 return MCDisassembler::Success;
1789 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1792 const void *Decoder) {
1793 unsigned Offset = Insn & 0x1F;
1794 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1796 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1798 Inst.addOperand(MCOperand::createReg(Reg));
1799 Inst.addOperand(MCOperand::createReg(Mips::SP));
1800 Inst.addOperand(MCOperand::createImm(Offset << 2));
1802 return MCDisassembler::Success;
1805 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1808 const void *Decoder) {
1809 unsigned Offset = Insn & 0x7F;
1810 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1812 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1814 Inst.addOperand(MCOperand::createReg(Reg));
1815 Inst.addOperand(MCOperand::createReg(Mips::GP));
1816 Inst.addOperand(MCOperand::createImm(Offset << 2));
1818 return MCDisassembler::Success;
1821 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1824 const void *Decoder) {
1826 switch (Inst.getOpcode()) {
1827 case Mips::LWM16_MMR6:
1828 case Mips::SWM16_MMR6:
1829 Offset = fieldFromInstruction(Insn, 4, 4);
1832 Offset = SignExtend32<4>(Insn & 0xf);
1836 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1837 == MCDisassembler::Fail)
1838 return MCDisassembler::Fail;
1840 Inst.addOperand(MCOperand::createReg(Mips::SP));
1841 Inst.addOperand(MCOperand::createImm(Offset << 2));
1843 return MCDisassembler::Success;
1846 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1849 const void *Decoder) {
1850 int Offset = SignExtend32<9>(Insn & 0x1ff);
1851 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1852 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1854 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1855 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1857 if (Inst.getOpcode() == Mips::SCE_MM || Inst.getOpcode() == Mips::SC_MMR6)
1858 Inst.addOperand(MCOperand::createReg(Reg));
1860 Inst.addOperand(MCOperand::createReg(Reg));
1861 Inst.addOperand(MCOperand::createReg(Base));
1862 Inst.addOperand(MCOperand::createImm(Offset));
1864 return MCDisassembler::Success;
1867 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1870 const void *Decoder) {
1871 int Offset = SignExtend32<12>(Insn & 0x0fff);
1872 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1873 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1875 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1876 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1878 switch (Inst.getOpcode()) {
1879 case Mips::SWM32_MM:
1880 case Mips::LWM32_MM:
1881 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1882 == MCDisassembler::Fail)
1883 return MCDisassembler::Fail;
1884 Inst.addOperand(MCOperand::createReg(Base));
1885 Inst.addOperand(MCOperand::createImm(Offset));
1888 Inst.addOperand(MCOperand::createReg(Reg));
1891 Inst.addOperand(MCOperand::createReg(Reg));
1892 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1893 Inst.addOperand(MCOperand::createReg(Reg+1));
1895 Inst.addOperand(MCOperand::createReg(Base));
1896 Inst.addOperand(MCOperand::createImm(Offset));
1899 return MCDisassembler::Success;
1902 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1905 const void *Decoder) {
1906 int Offset = SignExtend32<16>(Insn & 0xffff);
1907 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1908 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1910 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1911 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1913 Inst.addOperand(MCOperand::createReg(Reg));
1914 Inst.addOperand(MCOperand::createReg(Base));
1915 Inst.addOperand(MCOperand::createImm(Offset));
1917 return MCDisassembler::Success;
1920 static DecodeStatus DecodeFMem(MCInst &Inst,
1923 const void *Decoder) {
1924 int Offset = SignExtend32<16>(Insn & 0xffff);
1925 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1926 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1928 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1929 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1931 Inst.addOperand(MCOperand::createReg(Reg));
1932 Inst.addOperand(MCOperand::createReg(Base));
1933 Inst.addOperand(MCOperand::createImm(Offset));
1935 return MCDisassembler::Success;
1938 static DecodeStatus DecodeFMemMMR2(MCInst &Inst, unsigned Insn,
1939 uint64_t Address, const void *Decoder) {
1940 // This function is the same as DecodeFMem but with the Reg and Base fields
1941 // swapped according to microMIPS spec.
1942 int Offset = SignExtend32<16>(Insn & 0xffff);
1943 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1944 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1946 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1947 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1949 Inst.addOperand(MCOperand::createReg(Reg));
1950 Inst.addOperand(MCOperand::createReg(Base));
1951 Inst.addOperand(MCOperand::createImm(Offset));
1953 return MCDisassembler::Success;
1956 static DecodeStatus DecodeFMem2(MCInst &Inst,
1959 const void *Decoder) {
1960 int Offset = SignExtend32<16>(Insn & 0xffff);
1961 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1962 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1964 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1965 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1967 Inst.addOperand(MCOperand::createReg(Reg));
1968 Inst.addOperand(MCOperand::createReg(Base));
1969 Inst.addOperand(MCOperand::createImm(Offset));
1971 return MCDisassembler::Success;
1974 static DecodeStatus DecodeFMem3(MCInst &Inst,
1977 const void *Decoder) {
1978 int Offset = SignExtend32<16>(Insn & 0xffff);
1979 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1980 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1982 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1983 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1985 Inst.addOperand(MCOperand::createReg(Reg));
1986 Inst.addOperand(MCOperand::createReg(Base));
1987 Inst.addOperand(MCOperand::createImm(Offset));
1989 return MCDisassembler::Success;
1992 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1995 const void *Decoder) {
1996 int Offset = SignExtend32<11>(Insn & 0x07ff);
1997 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1998 unsigned Base = fieldFromInstruction(Insn, 11, 5);
2000 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2001 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2003 Inst.addOperand(MCOperand::createReg(Reg));
2004 Inst.addOperand(MCOperand::createReg(Base));
2005 Inst.addOperand(MCOperand::createImm(Offset));
2007 return MCDisassembler::Success;
2010 static DecodeStatus DecodeFMemCop2MMR6(MCInst &Inst, unsigned Insn,
2011 uint64_t Address, const void *Decoder) {
2012 int Offset = SignExtend32<11>(Insn & 0x07ff);
2013 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
2014 unsigned Base = fieldFromInstruction(Insn, 16, 5);
2016 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
2017 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2019 Inst.addOperand(MCOperand::createReg(Reg));
2020 Inst.addOperand(MCOperand::createReg(Base));
2021 Inst.addOperand(MCOperand::createImm(Offset));
2023 return MCDisassembler::Success;
2026 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
2029 const void *Decoder) {
2030 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
2031 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
2032 unsigned Base = fieldFromInstruction(Insn, 21, 5);
2034 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
2035 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
2037 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
2038 Inst.addOperand(MCOperand::createReg(Rt));
2041 Inst.addOperand(MCOperand::createReg(Rt));
2042 Inst.addOperand(MCOperand::createReg(Base));
2043 Inst.addOperand(MCOperand::createImm(Offset));
2045 return MCDisassembler::Success;
2048 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
2051 const void *Decoder) {
2052 // Currently only hardware register 29 is supported.
2054 return MCDisassembler::Fail;
2055 Inst.addOperand(MCOperand::createReg(Mips::HWR29));
2056 return MCDisassembler::Success;
2059 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
2062 const void *Decoder) {
2063 if (RegNo > 30 || RegNo %2)
2064 return MCDisassembler::Fail;
2066 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
2067 Inst.addOperand(MCOperand::createReg(Reg));
2068 return MCDisassembler::Success;
2071 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
2074 const void *Decoder) {
2076 return MCDisassembler::Fail;
2078 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
2079 Inst.addOperand(MCOperand::createReg(Reg));
2080 return MCDisassembler::Success;
2083 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
2086 const void *Decoder) {
2088 return MCDisassembler::Fail;
2090 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
2091 Inst.addOperand(MCOperand::createReg(Reg));
2092 return MCDisassembler::Success;
2095 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
2098 const void *Decoder) {
2100 return MCDisassembler::Fail;
2102 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
2103 Inst.addOperand(MCOperand::createReg(Reg));
2104 return MCDisassembler::Success;
2107 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
2110 const void *Decoder) {
2112 return MCDisassembler::Fail;
2114 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
2115 Inst.addOperand(MCOperand::createReg(Reg));
2116 return MCDisassembler::Success;
2119 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
2122 const void *Decoder) {
2124 return MCDisassembler::Fail;
2126 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
2127 Inst.addOperand(MCOperand::createReg(Reg));
2128 return MCDisassembler::Success;
2131 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
2134 const void *Decoder) {
2136 return MCDisassembler::Fail;
2138 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
2139 Inst.addOperand(MCOperand::createReg(Reg));
2140 return MCDisassembler::Success;
2143 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
2146 const void *Decoder) {
2148 return MCDisassembler::Fail;
2150 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
2151 Inst.addOperand(MCOperand::createReg(Reg));
2152 return MCDisassembler::Success;
2155 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
2158 const void *Decoder) {
2160 return MCDisassembler::Fail;
2162 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
2163 Inst.addOperand(MCOperand::createReg(Reg));
2164 return MCDisassembler::Success;
2167 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
2170 const void *Decoder) {
2172 return MCDisassembler::Fail;
2174 unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
2175 Inst.addOperand(MCOperand::createReg(Reg));
2176 return MCDisassembler::Success;
2179 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
2182 const void *Decoder) {
2184 return MCDisassembler::Fail;
2186 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
2187 Inst.addOperand(MCOperand::createReg(Reg));
2188 return MCDisassembler::Success;
2191 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
2194 const void *Decoder) {
2195 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
2196 Inst.addOperand(MCOperand::createImm(BranchOffset));
2197 return MCDisassembler::Success;
2200 static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst,
2203 const void *Decoder) {
2204 int32_t BranchOffset = (SignExtend32<16>(Offset) * 2);
2205 Inst.addOperand(MCOperand::createImm(BranchOffset));
2206 return MCDisassembler::Success;
2209 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
2212 const void *Decoder) {
2213 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
2214 Inst.addOperand(MCOperand::createImm(JumpOffset));
2215 return MCDisassembler::Success;
2218 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
2221 const void *Decoder) {
2222 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2224 Inst.addOperand(MCOperand::createImm(BranchOffset));
2225 return MCDisassembler::Success;
2228 static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst,
2231 const void *Decoder) {
2232 int32_t BranchOffset = SignExtend32<21>(Offset) * 4 + 4;
2234 Inst.addOperand(MCOperand::createImm(BranchOffset));
2235 return MCDisassembler::Success;
2238 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
2241 const void *Decoder) {
2242 int32_t BranchOffset = SignExtend32<26>(Offset) * 4 + 4;
2244 Inst.addOperand(MCOperand::createImm(BranchOffset));
2245 return MCDisassembler::Success;
2248 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
2251 const void *Decoder) {
2252 int32_t BranchOffset = SignExtend32<8>(Offset << 1);
2253 Inst.addOperand(MCOperand::createImm(BranchOffset));
2254 return MCDisassembler::Success;
2257 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
2260 const void *Decoder) {
2261 int32_t BranchOffset = SignExtend32<11>(Offset << 1);
2262 Inst.addOperand(MCOperand::createImm(BranchOffset));
2263 return MCDisassembler::Success;
2266 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
2269 const void *Decoder) {
2270 int32_t BranchOffset = SignExtend32<16>(Offset) * 2 + 4;
2271 Inst.addOperand(MCOperand::createImm(BranchOffset));
2272 return MCDisassembler::Success;
2275 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
2278 const void *Decoder) {
2279 int32_t BranchOffset = SignExtend32<27>(Offset << 1);
2281 Inst.addOperand(MCOperand::createImm(BranchOffset));
2282 return MCDisassembler::Success;
2285 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
2288 const void *Decoder) {
2289 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
2290 Inst.addOperand(MCOperand::createImm(JumpOffset));
2291 return MCDisassembler::Success;
2294 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
2297 const void *Decoder) {
2299 Inst.addOperand(MCOperand::createImm(1));
2300 else if (Value == 0x7)
2301 Inst.addOperand(MCOperand::createImm(-1));
2303 Inst.addOperand(MCOperand::createImm(Value << 2));
2304 return MCDisassembler::Success;
2307 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
2310 const void *Decoder) {
2312 Inst.addOperand(MCOperand::createImm(-1));
2314 Inst.addOperand(MCOperand::createImm(Value));
2315 return MCDisassembler::Success;
2318 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
2321 const void *Decoder) {
2322 Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
2323 return MCDisassembler::Success;
2326 template <unsigned Bits, int Offset, int Scale>
2327 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2329 const void *Decoder) {
2330 Value &= ((1 << Bits) - 1);
2332 Inst.addOperand(MCOperand::createImm(Value + Offset));
2333 return MCDisassembler::Success;
2336 template <unsigned Bits, int Offset, int ScaleBy>
2337 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
2339 const void *Decoder) {
2340 int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
2341 Inst.addOperand(MCOperand::createImm(Imm + Offset));
2342 return MCDisassembler::Success;
2345 static DecodeStatus DecodeInsSize(MCInst &Inst,
2348 const void *Decoder) {
2349 // First we need to grab the pos(lsb) from MCInst.
2350 // This function only handles the 32 bit variants of ins, as dins
2351 // variants are handled differently.
2352 int Pos = Inst.getOperand(2).getImm();
2353 int Size = (int) Insn - Pos + 1;
2354 Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
2355 return MCDisassembler::Success;
2358 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
2359 uint64_t Address, const void *Decoder) {
2360 Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
2361 return MCDisassembler::Success;
2364 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
2365 uint64_t Address, const void *Decoder) {
2366 Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
2367 return MCDisassembler::Success;
2370 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
2371 uint64_t Address, const void *Decoder) {
2372 int32_t DecodedValue;
2374 case 0: DecodedValue = 256; break;
2375 case 1: DecodedValue = 257; break;
2376 case 510: DecodedValue = -258; break;
2377 case 511: DecodedValue = -257; break;
2378 default: DecodedValue = SignExtend32<9>(Insn); break;
2380 Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
2381 return MCDisassembler::Success;
2384 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
2385 uint64_t Address, const void *Decoder) {
2386 // Insn must be >= 0, since it is unsigned that condition is always true.
2388 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
2390 Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
2391 return MCDisassembler::Success;
2394 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2397 const void *Decoder) {
2398 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2399 Mips::S6, Mips::S7, Mips::FP};
2402 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2404 // Empty register lists are not allowed.
2406 return MCDisassembler::Fail;
2408 RegNum = RegLst & 0xf;
2410 // RegLst values 10-15, and 26-31 are reserved.
2412 return MCDisassembler::Fail;
2414 for (unsigned i = 0; i < RegNum; i++)
2415 Inst.addOperand(MCOperand::createReg(Regs[i]));
2418 Inst.addOperand(MCOperand::createReg(Mips::RA));
2420 return MCDisassembler::Success;
2423 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2425 const void *Decoder) {
2426 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2428 switch(Inst.getOpcode()) {
2430 RegLst = fieldFromInstruction(Insn, 4, 2);
2432 case Mips::LWM16_MMR6:
2433 case Mips::SWM16_MMR6:
2434 RegLst = fieldFromInstruction(Insn, 8, 2);
2437 unsigned RegNum = RegLst & 0x3;
2439 for (unsigned i = 0; i <= RegNum; i++)
2440 Inst.addOperand(MCOperand::createReg(Regs[i]));
2442 Inst.addOperand(MCOperand::createReg(Mips::RA));
2444 return MCDisassembler::Success;
2447 static DecodeStatus DecodeMovePOperands(MCInst &Inst, unsigned Insn,
2449 const void *Decoder) {
2450 unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2451 if (DecodeMovePRegPair(Inst, RegPair, Address, Decoder) ==
2452 MCDisassembler::Fail)
2453 return MCDisassembler::Fail;
2456 if (static_cast<const MipsDisassembler*>(Decoder)->hasMips32r6())
2457 RegRs = fieldFromInstruction(Insn, 0, 2) |
2458 (fieldFromInstruction(Insn, 3, 1) << 2);
2460 RegRs = fieldFromInstruction(Insn, 1, 3);
2461 if (DecodeGPRMM16MovePRegisterClass(Inst, RegRs, Address, Decoder) ==
2462 MCDisassembler::Fail)
2463 return MCDisassembler::Fail;
2465 unsigned RegRt = fieldFromInstruction(Insn, 4, 3);
2466 if (DecodeGPRMM16MovePRegisterClass(Inst, RegRt, Address, Decoder) ==
2467 MCDisassembler::Fail)
2468 return MCDisassembler::Fail;
2470 return MCDisassembler::Success;
2473 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned RegPair,
2474 uint64_t Address, const void *Decoder) {
2477 return MCDisassembler::Fail;
2479 Inst.addOperand(MCOperand::createReg(Mips::A1));
2480 Inst.addOperand(MCOperand::createReg(Mips::A2));
2483 Inst.addOperand(MCOperand::createReg(Mips::A1));
2484 Inst.addOperand(MCOperand::createReg(Mips::A3));
2487 Inst.addOperand(MCOperand::createReg(Mips::A2));
2488 Inst.addOperand(MCOperand::createReg(Mips::A3));
2491 Inst.addOperand(MCOperand::createReg(Mips::A0));
2492 Inst.addOperand(MCOperand::createReg(Mips::S5));
2495 Inst.addOperand(MCOperand::createReg(Mips::A0));
2496 Inst.addOperand(MCOperand::createReg(Mips::S6));
2499 Inst.addOperand(MCOperand::createReg(Mips::A0));
2500 Inst.addOperand(MCOperand::createReg(Mips::A1));
2503 Inst.addOperand(MCOperand::createReg(Mips::A0));
2504 Inst.addOperand(MCOperand::createReg(Mips::A2));
2507 Inst.addOperand(MCOperand::createReg(Mips::A0));
2508 Inst.addOperand(MCOperand::createReg(Mips::A3));
2512 return MCDisassembler::Success;
2515 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2516 uint64_t Address, const void *Decoder) {
2517 Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2518 return MCDisassembler::Success;
2521 template <typename InsnType>
2522 static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
2524 const void *Decoder) {
2526 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
2527 // Invalid if rt == 0
2528 // BGTZALC_MMR6 if rs == 0 && rt != 0
2529 // BLTZALC_MMR6 if rs != 0 && rs == rt
2530 // BLTUC_MMR6 if rs != 0 && rs != rt
2532 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2533 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2539 return MCDisassembler::Fail;
2541 MI.setOpcode(Mips::BGTZALC_MMR6);
2543 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2545 else if (Rs == Rt) {
2546 MI.setOpcode(Mips::BLTZALC_MMR6);
2548 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2551 MI.setOpcode(Mips::BLTUC_MMR6);
2554 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2559 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2563 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2565 MI.addOperand(MCOperand::createImm(Imm));
2567 return MCDisassembler::Success;
2570 template <typename InsnType>
2571 static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
2573 const void *Decoder) {
2575 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
2576 // Invalid if rt == 0
2577 // BLEZALC_MMR6 if rs == 0 && rt != 0
2578 // BGEZALC_MMR6 if rs == rt && rt != 0
2579 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
2581 InsnType Rt = fieldFromInstruction(insn, 21, 5);
2582 InsnType Rs = fieldFromInstruction(insn, 16, 5);
2587 return MCDisassembler::Fail;
2589 MI.setOpcode(Mips::BLEZALC_MMR6);
2590 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2592 else if (Rs == Rt) {
2593 MI.setOpcode(Mips::BGEZALC_MMR6);
2594 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
2598 MI.setOpcode(Mips::BGEUC_MMR6);
2599 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
2604 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2606 MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2608 MI.addOperand(MCOperand::createImm(Imm));
2610 return MCDisassembler::Success;