LLVM API Documentation

MipsDisassembler.cpp
Go to the documentation of this file.
00001 //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file is part of the Mips Disassembler.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Mips.h"
00015 #include "MipsRegisterInfo.h"
00016 #include "MipsSubtarget.h"
00017 #include "llvm/MC/MCContext.h"
00018 #include "llvm/MC/MCDisassembler.h"
00019 #include "llvm/MC/MCFixedLenDisassembler.h"
00020 #include "llvm/MC/MCInst.h"
00021 #include "llvm/MC/MCSubtargetInfo.h"
00022 #include "llvm/Support/MathExtras.h"
00023 #include "llvm/Support/MemoryObject.h"
00024 #include "llvm/Support/TargetRegistry.h"
00025 
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "mips-disassembler"
00029 
00030 typedef MCDisassembler::DecodeStatus DecodeStatus;
00031 
00032 namespace {
00033 
00034 /// MipsDisassemblerBase - a disasembler class for Mips.
00035 class MipsDisassemblerBase : public MCDisassembler {
00036 public:
00037   /// Constructor     - Initializes the disassembler.
00038   ///
00039   MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
00040                        bool bigEndian) :
00041     MCDisassembler(STI, Ctx),
00042     IsN64(STI.getFeatureBits() & Mips::FeatureN64), isBigEndian(bigEndian) {}
00043 
00044   virtual ~MipsDisassemblerBase() {}
00045 
00046   bool isN64() const { return IsN64; }
00047 
00048 private:
00049   bool IsN64;
00050 protected:
00051   bool isBigEndian;
00052 };
00053 
00054 /// MipsDisassembler - a disasembler class for Mips32.
00055 class MipsDisassembler : public MipsDisassemblerBase {
00056   bool IsMicroMips;
00057 public:
00058   /// Constructor     - Initializes the disassembler.
00059   ///
00060   MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
00061       : MipsDisassemblerBase(STI, Ctx, bigEndian) {
00062     IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
00063   }
00064 
00065   bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
00066   bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
00067   bool hasMips32r6() const {
00068     return STI.getFeatureBits() & Mips::FeatureMips32r6;
00069   }
00070 
00071   bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
00072 
00073   bool hasCOP3() const {
00074     // Only present in MIPS-I and MIPS-II
00075     return !hasMips32() && !hasMips3();
00076   }
00077 
00078   /// getInstruction - See MCDisassembler.
00079   DecodeStatus getInstruction(MCInst &instr,
00080                               uint64_t &size,
00081                               const MemoryObject &region,
00082                               uint64_t address,
00083                               raw_ostream &vStream,
00084                               raw_ostream &cStream) const override;
00085 };
00086 
00087 
00088 /// Mips64Disassembler - a disasembler class for Mips64.
00089 class Mips64Disassembler : public MipsDisassemblerBase {
00090 public:
00091   /// Constructor     - Initializes the disassembler.
00092   ///
00093   Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
00094                      bool bigEndian) :
00095     MipsDisassemblerBase(STI, Ctx, bigEndian) {}
00096 
00097   /// getInstruction - See MCDisassembler.
00098   DecodeStatus getInstruction(MCInst &instr,
00099                               uint64_t &size,
00100                               const MemoryObject &region,
00101                               uint64_t address,
00102                               raw_ostream &vStream,
00103                               raw_ostream &cStream) const override;
00104 };
00105 
00106 } // end anonymous namespace
00107 
00108 // Forward declare these because the autogenerated code will reference them.
00109 // Definitions are further down.
00110 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
00111                                              unsigned RegNo,
00112                                              uint64_t Address,
00113                                              const void *Decoder);
00114 
00115 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
00116                                                  unsigned RegNo,
00117                                                  uint64_t Address,
00118                                                  const void *Decoder);
00119 
00120 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
00121                                              unsigned RegNo,
00122                                              uint64_t Address,
00123                                              const void *Decoder);
00124 
00125 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
00126                                            unsigned Insn,
00127                                            uint64_t Address,
00128                                            const void *Decoder);
00129 
00130 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
00131                                             unsigned RegNo,
00132                                             uint64_t Address,
00133                                             const void *Decoder);
00134 
00135 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
00136                                              unsigned RegNo,
00137                                              uint64_t Address,
00138                                              const void *Decoder);
00139 
00140 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
00141                                              unsigned RegNo,
00142                                              uint64_t Address,
00143                                              const void *Decoder);
00144 
00145 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
00146                                            unsigned RegNo,
00147                                            uint64_t Address,
00148                                            const void *Decoder);
00149 
00150 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
00151                                            unsigned RegNo,
00152                                            uint64_t Address,
00153                                            const void *Decoder);
00154 
00155 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
00156                                              uint64_t Address,
00157                                              const void *Decoder);
00158 
00159 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
00160                                               unsigned Insn,
00161                                               uint64_t Address,
00162                                               const void *Decoder);
00163 
00164 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
00165                                               unsigned RegNo,
00166                                               uint64_t Address,
00167                                               const void *Decoder);
00168 
00169 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
00170                                                 unsigned RegNo,
00171                                                 uint64_t Address,
00172                                                 const void *Decoder);
00173 
00174 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
00175                                                unsigned RegNo,
00176                                                uint64_t Address,
00177                                                const void *Decoder);
00178 
00179 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
00180                                                unsigned RegNo,
00181                                                uint64_t Address,
00182                                                const void *Decoder);
00183 
00184 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
00185                                                unsigned RegNo,
00186                                                uint64_t Address,
00187                                                const void *Decoder);
00188 
00189 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
00190                                                unsigned RegNo,
00191                                                uint64_t Address,
00192                                                const void *Decoder);
00193 
00194 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
00195                                                unsigned RegNo,
00196                                                uint64_t Address,
00197                                                const void *Decoder);
00198 
00199 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
00200                                                unsigned RegNo,
00201                                                uint64_t Address,
00202                                                const void *Decoder);
00203 
00204 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
00205                                                unsigned RegNo,
00206                                                uint64_t Address,
00207                                                const void *Decoder);
00208 
00209 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
00210                                             unsigned RegNo,
00211                                             uint64_t Address,
00212                                             const void *Decoder);
00213 
00214 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
00215                                        unsigned Offset,
00216                                        uint64_t Address,
00217                                        const void *Decoder);
00218 
00219 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
00220                                      unsigned Insn,
00221                                      uint64_t Address,
00222                                      const void *Decoder);
00223 
00224 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
00225                                          unsigned Offset,
00226                                          uint64_t Address,
00227                                          const void *Decoder);
00228 
00229 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
00230                                          unsigned Offset,
00231                                          uint64_t Address,
00232                                          const void *Decoder);
00233 
00234 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
00235 // shifted left by 1 bit.
00236 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
00237                                          unsigned Offset,
00238                                          uint64_t Address,
00239                                          const void *Decoder);
00240 
00241 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
00242 // shifted left by 1 bit.
00243 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
00244                                        unsigned Insn,
00245                                        uint64_t Address,
00246                                        const void *Decoder);
00247 
00248 static DecodeStatus DecodeMem(MCInst &Inst,
00249                               unsigned Insn,
00250                               uint64_t Address,
00251                               const void *Decoder);
00252 
00253 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
00254                                     uint64_t Address, const void *Decoder);
00255 
00256 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
00257                                      unsigned Insn,
00258                                      uint64_t Address,
00259                                      const void *Decoder);
00260 
00261 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
00262                                      unsigned Insn,
00263                                      uint64_t Address,
00264                                      const void *Decoder);
00265 
00266 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
00267                                uint64_t Address,
00268                                const void *Decoder);
00269 
00270 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
00271                                        unsigned Insn,
00272                                        uint64_t Address,
00273                                        const void *Decoder);
00274 
00275 static DecodeStatus DecodeSimm16(MCInst &Inst,
00276                                  unsigned Insn,
00277                                  uint64_t Address,
00278                                  const void *Decoder);
00279 
00280 // Decode the immediate field of an LSA instruction which
00281 // is off by one.
00282 static DecodeStatus DecodeLSAImm(MCInst &Inst,
00283                                  unsigned Insn,
00284                                  uint64_t Address,
00285                                  const void *Decoder);
00286 
00287 static DecodeStatus DecodeInsSize(MCInst &Inst,
00288                                   unsigned Insn,
00289                                   uint64_t Address,
00290                                   const void *Decoder);
00291 
00292 static DecodeStatus DecodeExtSize(MCInst &Inst,
00293                                   unsigned Insn,
00294                                   uint64_t Address,
00295                                   const void *Decoder);
00296 
00297 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
00298                                      uint64_t Address, const void *Decoder);
00299 
00300 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
00301                                      uint64_t Address, const void *Decoder);
00302 
00303 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
00304 /// handle.
00305 template <typename InsnType>
00306 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
00307                                    const void *Decoder);
00308 
00309 template <typename InsnType>
00310 static DecodeStatus
00311 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00312                       const void *Decoder);
00313 
00314 template <typename InsnType>
00315 static DecodeStatus
00316 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00317                        const void *Decoder);
00318 
00319 template <typename InsnType>
00320 static DecodeStatus
00321 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00322                        const void *Decoder);
00323 
00324 template <typename InsnType>
00325 static DecodeStatus
00326 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00327                        const void *Decoder);
00328 
00329 template <typename InsnType>
00330 static DecodeStatus
00331 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00332                       const void *Decoder);
00333 
00334 template <typename InsnType>
00335 static DecodeStatus
00336 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
00337                        const void *Decoder);
00338 
00339 namespace llvm {
00340 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
00341               TheMips64elTarget;
00342 }
00343 
00344 static MCDisassembler *createMipsDisassembler(
00345                        const Target &T,
00346                        const MCSubtargetInfo &STI,
00347                        MCContext &Ctx) {
00348   return new MipsDisassembler(STI, Ctx, true);
00349 }
00350 
00351 static MCDisassembler *createMipselDisassembler(
00352                        const Target &T,
00353                        const MCSubtargetInfo &STI,
00354                        MCContext &Ctx) {
00355   return new MipsDisassembler(STI, Ctx, false);
00356 }
00357 
00358 static MCDisassembler *createMips64Disassembler(
00359                        const Target &T,
00360                        const MCSubtargetInfo &STI,
00361                        MCContext &Ctx) {
00362   return new Mips64Disassembler(STI, Ctx, true);
00363 }
00364 
00365 static MCDisassembler *createMips64elDisassembler(
00366                        const Target &T,
00367                        const MCSubtargetInfo &STI,
00368                        MCContext &Ctx) {
00369   return new Mips64Disassembler(STI, Ctx, false);
00370 }
00371 
00372 extern "C" void LLVMInitializeMipsDisassembler() {
00373   // Register the disassembler.
00374   TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
00375                                          createMipsDisassembler);
00376   TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
00377                                          createMipselDisassembler);
00378   TargetRegistry::RegisterMCDisassembler(TheMips64Target,
00379                                          createMips64Disassembler);
00380   TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
00381                                          createMips64elDisassembler);
00382 }
00383 
00384 #include "MipsGenDisassemblerTables.inc"
00385 
00386 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
00387   const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
00388   const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
00389   return *(RegInfo->getRegClass(RC).begin() + RegNo);
00390 }
00391 
00392 template <typename InsnType>
00393 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
00394                                    const void *Decoder) {
00395   typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
00396   // The size of the n field depends on the element size
00397   // The register class also depends on this.
00398   InsnType tmp = fieldFromInstruction(insn, 17, 5);
00399   unsigned NSize = 0;
00400   DecodeFN RegDecoder = nullptr;
00401   if ((tmp & 0x18) == 0x00) { // INSVE_B
00402     NSize = 4;
00403     RegDecoder = DecodeMSA128BRegisterClass;
00404   } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
00405     NSize = 3;
00406     RegDecoder = DecodeMSA128HRegisterClass;
00407   } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
00408     NSize = 2;
00409     RegDecoder = DecodeMSA128WRegisterClass;
00410   } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
00411     NSize = 1;
00412     RegDecoder = DecodeMSA128DRegisterClass;
00413   } else
00414     llvm_unreachable("Invalid encoding");
00415 
00416   assert(NSize != 0 && RegDecoder != nullptr);
00417 
00418   // $wd
00419   tmp = fieldFromInstruction(insn, 6, 5);
00420   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
00421     return MCDisassembler::Fail;
00422   // $wd_in
00423   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
00424     return MCDisassembler::Fail;
00425   // $n
00426   tmp = fieldFromInstruction(insn, 16, NSize);
00427   MI.addOperand(MCOperand::CreateImm(tmp));
00428   // $ws
00429   tmp = fieldFromInstruction(insn, 11, 5);
00430   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
00431     return MCDisassembler::Fail;
00432   // $n2
00433   MI.addOperand(MCOperand::CreateImm(0));
00434 
00435   return MCDisassembler::Success;
00436 }
00437 
00438 template <typename InsnType>
00439 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
00440                                           uint64_t Address,
00441                                           const void *Decoder) {
00442   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00443   // (otherwise we would have matched the ADDI instruction from the earlier
00444   // ISA's instead).
00445   //
00446   // We have:
00447   //    0b001000 sssss ttttt iiiiiiiiiiiiiiii
00448   //      BOVC if rs >= rt
00449   //      BEQZALC if rs == 0 && rt != 0
00450   //      BEQC if rs < rt && rs != 0
00451 
00452   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00453   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00454   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00455   bool HasRs = false;
00456 
00457   if (Rs >= Rt) {
00458     MI.setOpcode(Mips::BOVC);
00459     HasRs = true;
00460   } else if (Rs != 0 && Rs < Rt) {
00461     MI.setOpcode(Mips::BEQC);
00462     HasRs = true;
00463   } else
00464     MI.setOpcode(Mips::BEQZALC);
00465 
00466   if (HasRs)
00467     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00468                                        Rs)));
00469 
00470   MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00471                                      Rt)));
00472   MI.addOperand(MCOperand::CreateImm(Imm));
00473 
00474   return MCDisassembler::Success;
00475 }
00476 
00477 template <typename InsnType>
00478 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
00479                                            uint64_t Address,
00480                                            const void *Decoder) {
00481   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00482   // (otherwise we would have matched the ADDI instruction from the earlier
00483   // ISA's instead).
00484   //
00485   // We have:
00486   //    0b011000 sssss ttttt iiiiiiiiiiiiiiii
00487   //      BNVC if rs >= rt
00488   //      BNEZALC if rs == 0 && rt != 0
00489   //      BNEC if rs < rt && rs != 0
00490 
00491   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00492   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00493   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00494   bool HasRs = false;
00495 
00496   if (Rs >= Rt) {
00497     MI.setOpcode(Mips::BNVC);
00498     HasRs = true;
00499   } else if (Rs != 0 && Rs < Rt) {
00500     MI.setOpcode(Mips::BNEC);
00501     HasRs = true;
00502   } else
00503     MI.setOpcode(Mips::BNEZALC);
00504 
00505   if (HasRs)
00506     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00507                                        Rs)));
00508 
00509   MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00510                                      Rt)));
00511   MI.addOperand(MCOperand::CreateImm(Imm));
00512 
00513   return MCDisassembler::Success;
00514 }
00515 
00516 template <typename InsnType>
00517 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
00518                                            uint64_t Address,
00519                                            const void *Decoder) {
00520   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00521   // (otherwise we would have matched the BLEZL instruction from the earlier
00522   // ISA's instead).
00523   //
00524   // We have:
00525   //    0b010110 sssss ttttt iiiiiiiiiiiiiiii
00526   //      Invalid if rs == 0
00527   //      BLEZC   if rs == 0  && rt != 0
00528   //      BGEZC   if rs == rt && rt != 0
00529   //      BGEC    if rs != rt && rs != 0  && rt != 0
00530 
00531   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00532   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00533   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00534   bool HasRs = false;
00535 
00536   if (Rt == 0)
00537     return MCDisassembler::Fail;
00538   else if (Rs == 0)
00539     MI.setOpcode(Mips::BLEZC);
00540   else if (Rs == Rt)
00541     MI.setOpcode(Mips::BGEZC);
00542   else {
00543     HasRs = true;
00544     MI.setOpcode(Mips::BGEC);
00545   }
00546 
00547   if (HasRs)
00548     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00549                                        Rs)));
00550 
00551   MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00552                                      Rt)));
00553 
00554   MI.addOperand(MCOperand::CreateImm(Imm));
00555 
00556   return MCDisassembler::Success;
00557 }
00558 
00559 template <typename InsnType>
00560 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
00561                                            uint64_t Address,
00562                                            const void *Decoder) {
00563   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00564   // (otherwise we would have matched the BGTZL instruction from the earlier
00565   // ISA's instead).
00566   //
00567   // We have:
00568   //    0b010111 sssss ttttt iiiiiiiiiiiiiiii
00569   //      Invalid if rs == 0
00570   //      BGTZC   if rs == 0  && rt != 0
00571   //      BLTZC   if rs == rt && rt != 0
00572   //      BLTC    if rs != rt && rs != 0  && rt != 0
00573 
00574   bool HasRs = false;
00575 
00576   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00577   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00578   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00579 
00580   if (Rt == 0)
00581     return MCDisassembler::Fail;
00582   else if (Rs == 0)
00583     MI.setOpcode(Mips::BGTZC);
00584   else if (Rs == Rt)
00585     MI.setOpcode(Mips::BLTZC);
00586   else {
00587     MI.setOpcode(Mips::BLTC);
00588     HasRs = true;
00589   }
00590 
00591   if (HasRs)
00592     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00593                                               Rs)));
00594 
00595   MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00596                                      Rt)));
00597 
00598   MI.addOperand(MCOperand::CreateImm(Imm));
00599 
00600   return MCDisassembler::Success;
00601 }
00602 
00603 template <typename InsnType>
00604 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
00605                                           uint64_t Address,
00606                                           const void *Decoder) {
00607   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00608   // (otherwise we would have matched the BGTZ instruction from the earlier
00609   // ISA's instead).
00610   //
00611   // We have:
00612   //    0b000111 sssss ttttt iiiiiiiiiiiiiiii
00613   //      BGTZ    if rt == 0
00614   //      BGTZALC if rs == 0 && rt != 0
00615   //      BLTZALC if rs != 0 && rs == rt
00616   //      BLTUC   if rs != 0 && rs != rt
00617 
00618   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00619   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00620   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00621   bool HasRs = false;
00622   bool HasRt = false;
00623 
00624   if (Rt == 0) {
00625     MI.setOpcode(Mips::BGTZ);
00626     HasRs = true;
00627   } else if (Rs == 0) {
00628     MI.setOpcode(Mips::BGTZALC);
00629     HasRt = true;
00630   } else if (Rs == Rt) {
00631     MI.setOpcode(Mips::BLTZALC);
00632     HasRs = true;
00633   } else {
00634     MI.setOpcode(Mips::BLTUC);
00635     HasRs = true;
00636     HasRt = true;
00637   }
00638 
00639   if (HasRs)
00640     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00641                                        Rs)));
00642 
00643   if (HasRt)
00644     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00645                                        Rt)));
00646 
00647   MI.addOperand(MCOperand::CreateImm(Imm));
00648 
00649   return MCDisassembler::Success;
00650 }
00651 
00652 template <typename InsnType>
00653 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
00654                                            uint64_t Address,
00655                                            const void *Decoder) {
00656   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
00657   // (otherwise we would have matched the BLEZL instruction from the earlier
00658   // ISA's instead).
00659   //
00660   // We have:
00661   //    0b000110 sssss ttttt iiiiiiiiiiiiiiii
00662   //      Invalid   if rs == 0
00663   //      BLEZALC   if rs == 0  && rt != 0
00664   //      BGEZALC   if rs == rt && rt != 0
00665   //      BGEUC     if rs != rt && rs != 0  && rt != 0
00666 
00667   InsnType Rs = fieldFromInstruction(insn, 21, 5);
00668   InsnType Rt = fieldFromInstruction(insn, 16, 5);
00669   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
00670   bool HasRs = false;
00671 
00672   if (Rt == 0)
00673     return MCDisassembler::Fail;
00674   else if (Rs == 0)
00675     MI.setOpcode(Mips::BLEZALC);
00676   else if (Rs == Rt)
00677     MI.setOpcode(Mips::BGEZALC);
00678   else {
00679     HasRs = true;
00680     MI.setOpcode(Mips::BGEUC);
00681   }
00682 
00683   if (HasRs)
00684     MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00685                                        Rs)));
00686   MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
00687                                      Rt)));
00688 
00689   MI.addOperand(MCOperand::CreateImm(Imm));
00690 
00691   return MCDisassembler::Success;
00692 }
00693 
00694   /// readInstruction - read four bytes from the MemoryObject
00695   /// and return 32 bit word sorted according to the given endianess
00696 static DecodeStatus readInstruction32(const MemoryObject &region,
00697                                       uint64_t address,
00698                                       uint64_t &size,
00699                                       uint32_t &insn,
00700                                       bool isBigEndian,
00701                                       bool IsMicroMips) {
00702   uint8_t Bytes[4];
00703 
00704   // We want to read exactly 4 Bytes of data.
00705   if (region.readBytes(address, 4, Bytes) == -1) {
00706     size = 0;
00707     return MCDisassembler::Fail;
00708   }
00709 
00710   if (isBigEndian) {
00711     // Encoded as a big-endian 32-bit word in the stream.
00712     insn = (Bytes[3] <<  0) |
00713            (Bytes[2] <<  8) |
00714            (Bytes[1] << 16) |
00715            (Bytes[0] << 24);
00716   }
00717   else {
00718     // Encoded as a small-endian 32-bit word in the stream.
00719     // Little-endian byte ordering:
00720     //   mips32r2:   4 | 3 | 2 | 1
00721     //   microMIPS:  2 | 1 | 4 | 3
00722     if (IsMicroMips) {
00723       insn = (Bytes[2] <<  0) |
00724              (Bytes[3] <<  8) |
00725              (Bytes[0] << 16) |
00726              (Bytes[1] << 24);
00727     } else {
00728       insn = (Bytes[0] <<  0) |
00729              (Bytes[1] <<  8) |
00730              (Bytes[2] << 16) |
00731              (Bytes[3] << 24);
00732     }
00733   }
00734 
00735   return MCDisassembler::Success;
00736 }
00737 
00738 DecodeStatus
00739 MipsDisassembler::getInstruction(MCInst &instr,
00740                                  uint64_t &Size,
00741                                  const MemoryObject &Region,
00742                                  uint64_t Address,
00743                                  raw_ostream &vStream,
00744                                  raw_ostream &cStream) const {
00745   uint32_t Insn;
00746 
00747   DecodeStatus Result = readInstruction32(Region, Address, Size,
00748                                           Insn, isBigEndian, IsMicroMips);
00749   if (Result == MCDisassembler::Fail)
00750     return MCDisassembler::Fail;
00751 
00752   if (IsMicroMips) {
00753     DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
00754     // Calling the auto-generated decoder function.
00755     Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
00756                                this, STI);
00757     if (Result != MCDisassembler::Fail) {
00758       Size = 4;
00759       return Result;
00760     }
00761     return MCDisassembler::Fail;
00762   }
00763 
00764   if (hasCOP3()) {
00765     DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
00766     Result =
00767         decodeInstruction(DecoderTableCOP3_32, instr, Insn, Address, this, STI);
00768     if (Result != MCDisassembler::Fail) {
00769       Size = 4;
00770       return Result;
00771     }
00772   }
00773 
00774   if (hasMips32r6() && isGP64()) {
00775     DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
00776     Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
00777                                Address, this, STI);
00778     if (Result != MCDisassembler::Fail) {
00779       Size = 4;
00780       return Result;
00781     }
00782   }
00783 
00784   if (hasMips32r6()) {
00785     DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
00786     Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
00787                                Address, this, STI);
00788     if (Result != MCDisassembler::Fail) {
00789       Size = 4;
00790       return Result;
00791     }
00792   }
00793 
00794   DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
00795   // Calling the auto-generated decoder function.
00796   Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
00797                              this, STI);
00798   if (Result != MCDisassembler::Fail) {
00799     Size = 4;
00800     return Result;
00801   }
00802 
00803   return MCDisassembler::Fail;
00804 }
00805 
00806 DecodeStatus
00807 Mips64Disassembler::getInstruction(MCInst &instr,
00808                                    uint64_t &Size,
00809                                    const MemoryObject &Region,
00810                                    uint64_t Address,
00811                                    raw_ostream &vStream,
00812                                    raw_ostream &cStream) const {
00813   uint32_t Insn;
00814 
00815   DecodeStatus Result = readInstruction32(Region, Address, Size,
00816                                           Insn, isBigEndian, false);
00817   if (Result == MCDisassembler::Fail)
00818     return MCDisassembler::Fail;
00819 
00820   // Calling the auto-generated decoder function.
00821   Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
00822                              this, STI);
00823   if (Result != MCDisassembler::Fail) {
00824     Size = 4;
00825     return Result;
00826   }
00827   // If we fail to decode in Mips64 decoder space we can try in Mips32
00828   Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
00829                              this, STI);
00830   if (Result != MCDisassembler::Fail) {
00831     Size = 4;
00832     return Result;
00833   }
00834 
00835   return MCDisassembler::Fail;
00836 }
00837 
00838 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
00839                                                  unsigned RegNo,
00840                                                  uint64_t Address,
00841                                                  const void *Decoder) {
00842 
00843   return MCDisassembler::Fail;
00844 
00845 }
00846 
00847 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
00848                                              unsigned RegNo,
00849                                              uint64_t Address,
00850                                              const void *Decoder) {
00851 
00852   if (RegNo > 31)
00853     return MCDisassembler::Fail;
00854 
00855   unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
00856   Inst.addOperand(MCOperand::CreateReg(Reg));
00857   return MCDisassembler::Success;
00858 }
00859 
00860 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
00861                                              unsigned RegNo,
00862                                              uint64_t Address,
00863                                              const void *Decoder) {
00864   if (RegNo > 31)
00865     return MCDisassembler::Fail;
00866   unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
00867   Inst.addOperand(MCOperand::CreateReg(Reg));
00868   return MCDisassembler::Success;
00869 }
00870 
00871 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
00872                                            unsigned RegNo,
00873                                            uint64_t Address,
00874                                            const void *Decoder) {
00875   if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
00876     return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
00877 
00878   return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
00879 }
00880 
00881 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
00882                                             unsigned RegNo,
00883                                             uint64_t Address,
00884                                             const void *Decoder) {
00885   return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
00886 }
00887 
00888 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
00889                                              unsigned RegNo,
00890                                              uint64_t Address,
00891                                              const void *Decoder) {
00892   if (RegNo > 31)
00893     return MCDisassembler::Fail;
00894 
00895   unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
00896   Inst.addOperand(MCOperand::CreateReg(Reg));
00897   return MCDisassembler::Success;
00898 }
00899 
00900 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
00901                                              unsigned RegNo,
00902                                              uint64_t Address,
00903                                              const void *Decoder) {
00904   if (RegNo > 31)
00905     return MCDisassembler::Fail;
00906 
00907   unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
00908   Inst.addOperand(MCOperand::CreateReg(Reg));
00909   return MCDisassembler::Success;
00910 }
00911 
00912 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
00913                                            unsigned RegNo,
00914                                            uint64_t Address,
00915                                            const void *Decoder) {
00916   if (RegNo > 31)
00917     return MCDisassembler::Fail;
00918   unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
00919   Inst.addOperand(MCOperand::CreateReg(Reg));
00920   return MCDisassembler::Success;
00921 }
00922 
00923 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
00924                                            unsigned RegNo,
00925                                            uint64_t Address,
00926                                            const void *Decoder) {
00927   if (RegNo > 7)
00928     return MCDisassembler::Fail;
00929   unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
00930   Inst.addOperand(MCOperand::CreateReg(Reg));
00931   return MCDisassembler::Success;
00932 }
00933 
00934 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
00935                                              uint64_t Address,
00936                                              const void *Decoder) {
00937   if (RegNo > 31)
00938     return MCDisassembler::Fail;
00939 
00940   unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
00941   Inst.addOperand(MCOperand::CreateReg(Reg));
00942   return MCDisassembler::Success;
00943 }
00944 
00945 static DecodeStatus DecodeMem(MCInst &Inst,
00946                               unsigned Insn,
00947                               uint64_t Address,
00948                               const void *Decoder) {
00949   int Offset = SignExtend32<16>(Insn & 0xffff);
00950   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
00951   unsigned Base = fieldFromInstruction(Insn, 21, 5);
00952 
00953   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
00954   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
00955 
00956   if(Inst.getOpcode() == Mips::SC){
00957     Inst.addOperand(MCOperand::CreateReg(Reg));
00958   }
00959 
00960   Inst.addOperand(MCOperand::CreateReg(Reg));
00961   Inst.addOperand(MCOperand::CreateReg(Base));
00962   Inst.addOperand(MCOperand::CreateImm(Offset));
00963 
00964   return MCDisassembler::Success;
00965 }
00966 
00967 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
00968                                     uint64_t Address, const void *Decoder) {
00969   int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
00970   unsigned Reg = fieldFromInstruction(Insn, 6, 5);
00971   unsigned Base = fieldFromInstruction(Insn, 11, 5);
00972 
00973   Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
00974   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
00975 
00976   Inst.addOperand(MCOperand::CreateReg(Reg));
00977   Inst.addOperand(MCOperand::CreateReg(Base));
00978 
00979   // The immediate field of an LD/ST instruction is scaled which means it must
00980   // be multiplied (when decoding) by the size (in bytes) of the instructions'
00981   // data format.
00982   // .b - 1 byte
00983   // .h - 2 bytes
00984   // .w - 4 bytes
00985   // .d - 8 bytes
00986   switch(Inst.getOpcode())
00987   {
00988   default:
00989     assert (0 && "Unexpected instruction");
00990     return MCDisassembler::Fail;
00991     break;
00992   case Mips::LD_B:
00993   case Mips::ST_B:
00994     Inst.addOperand(MCOperand::CreateImm(Offset));
00995     break;
00996   case Mips::LD_H:
00997   case Mips::ST_H:
00998     Inst.addOperand(MCOperand::CreateImm(Offset * 2));
00999     break;
01000   case Mips::LD_W:
01001   case Mips::ST_W:
01002     Inst.addOperand(MCOperand::CreateImm(Offset * 4));
01003     break;
01004   case Mips::LD_D:
01005   case Mips::ST_D:
01006     Inst.addOperand(MCOperand::CreateImm(Offset * 8));
01007     break;
01008   }
01009 
01010   return MCDisassembler::Success;
01011 }
01012 
01013 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
01014                                      unsigned Insn,
01015                                      uint64_t Address,
01016                                      const void *Decoder) {
01017   int Offset = SignExtend32<12>(Insn & 0x0fff);
01018   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
01019   unsigned Base = fieldFromInstruction(Insn, 16, 5);
01020 
01021   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
01022   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
01023 
01024   if (Inst.getOpcode() == Mips::SC_MM)
01025     Inst.addOperand(MCOperand::CreateReg(Reg));
01026 
01027   Inst.addOperand(MCOperand::CreateReg(Reg));
01028   Inst.addOperand(MCOperand::CreateReg(Base));
01029   Inst.addOperand(MCOperand::CreateImm(Offset));
01030 
01031   return MCDisassembler::Success;
01032 }
01033 
01034 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
01035                                      unsigned Insn,
01036                                      uint64_t Address,
01037                                      const void *Decoder) {
01038   int Offset = SignExtend32<16>(Insn & 0xffff);
01039   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
01040   unsigned Base = fieldFromInstruction(Insn, 16, 5);
01041 
01042   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
01043   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
01044 
01045   Inst.addOperand(MCOperand::CreateReg(Reg));
01046   Inst.addOperand(MCOperand::CreateReg(Base));
01047   Inst.addOperand(MCOperand::CreateImm(Offset));
01048 
01049   return MCDisassembler::Success;
01050 }
01051 
01052 static DecodeStatus DecodeFMem(MCInst &Inst,
01053                                unsigned Insn,
01054                                uint64_t Address,
01055                                const void *Decoder) {
01056   int Offset = SignExtend32<16>(Insn & 0xffff);
01057   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
01058   unsigned Base = fieldFromInstruction(Insn, 21, 5);
01059 
01060   Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
01061   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
01062 
01063   Inst.addOperand(MCOperand::CreateReg(Reg));
01064   Inst.addOperand(MCOperand::CreateReg(Base));
01065   Inst.addOperand(MCOperand::CreateImm(Offset));
01066 
01067   return MCDisassembler::Success;
01068 }
01069 
01070 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
01071                                        unsigned Insn,
01072                                        uint64_t Address,
01073                                        const void *Decoder) {
01074   int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
01075   unsigned Rt = fieldFromInstruction(Insn, 16, 5);
01076   unsigned Base = fieldFromInstruction(Insn, 21, 5);
01077 
01078   Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
01079   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
01080 
01081   if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
01082     Inst.addOperand(MCOperand::CreateReg(Rt));
01083   }
01084 
01085   Inst.addOperand(MCOperand::CreateReg(Rt));
01086   Inst.addOperand(MCOperand::CreateReg(Base));
01087   Inst.addOperand(MCOperand::CreateImm(Offset));
01088 
01089   return MCDisassembler::Success;
01090 }
01091 
01092 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
01093                                               unsigned RegNo,
01094                                               uint64_t Address,
01095                                               const void *Decoder) {
01096   // Currently only hardware register 29 is supported.
01097   if (RegNo != 29)
01098     return  MCDisassembler::Fail;
01099   Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
01100   return MCDisassembler::Success;
01101 }
01102 
01103 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
01104                                               unsigned RegNo,
01105                                               uint64_t Address,
01106                                               const void *Decoder) {
01107   if (RegNo > 30 || RegNo %2)
01108     return MCDisassembler::Fail;
01109 
01110   ;
01111   unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
01112   Inst.addOperand(MCOperand::CreateReg(Reg));
01113   return MCDisassembler::Success;
01114 }
01115 
01116 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
01117                                                 unsigned RegNo,
01118                                                 uint64_t Address,
01119                                                 const void *Decoder) {
01120   if (RegNo >= 4)
01121     return MCDisassembler::Fail;
01122 
01123   unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
01124   Inst.addOperand(MCOperand::CreateReg(Reg));
01125   return MCDisassembler::Success;
01126 }
01127 
01128 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
01129                                                unsigned RegNo,
01130                                                uint64_t Address,
01131                                                const void *Decoder) {
01132   if (RegNo >= 4)
01133     return MCDisassembler::Fail;
01134 
01135   unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
01136   Inst.addOperand(MCOperand::CreateReg(Reg));
01137   return MCDisassembler::Success;
01138 }
01139 
01140 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
01141                                                unsigned RegNo,
01142                                                uint64_t Address,
01143                                                const void *Decoder) {
01144   if (RegNo >= 4)
01145     return MCDisassembler::Fail;
01146 
01147   unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
01148   Inst.addOperand(MCOperand::CreateReg(Reg));
01149   return MCDisassembler::Success;
01150 }
01151 
01152 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
01153                                                unsigned RegNo,
01154                                                uint64_t Address,
01155                                                const void *Decoder) {
01156   if (RegNo > 31)
01157     return MCDisassembler::Fail;
01158 
01159   unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
01160   Inst.addOperand(MCOperand::CreateReg(Reg));
01161   return MCDisassembler::Success;
01162 }
01163 
01164 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
01165                                                unsigned RegNo,
01166                                                uint64_t Address,
01167                                                const void *Decoder) {
01168   if (RegNo > 31)
01169     return MCDisassembler::Fail;
01170 
01171   unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
01172   Inst.addOperand(MCOperand::CreateReg(Reg));
01173   return MCDisassembler::Success;
01174 }
01175 
01176 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
01177                                                unsigned RegNo,
01178                                                uint64_t Address,
01179                                                const void *Decoder) {
01180   if (RegNo > 31)
01181     return MCDisassembler::Fail;
01182 
01183   unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
01184   Inst.addOperand(MCOperand::CreateReg(Reg));
01185   return MCDisassembler::Success;
01186 }
01187 
01188 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
01189                                                unsigned RegNo,
01190                                                uint64_t Address,
01191                                                const void *Decoder) {
01192   if (RegNo > 31)
01193     return MCDisassembler::Fail;
01194 
01195   unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
01196   Inst.addOperand(MCOperand::CreateReg(Reg));
01197   return MCDisassembler::Success;
01198 }
01199 
01200 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
01201                                                unsigned RegNo,
01202                                                uint64_t Address,
01203                                                const void *Decoder) {
01204   if (RegNo > 7)
01205     return MCDisassembler::Fail;
01206 
01207   unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
01208   Inst.addOperand(MCOperand::CreateReg(Reg));
01209   return MCDisassembler::Success;
01210 }
01211 
01212 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
01213                                             unsigned RegNo,
01214                                             uint64_t Address,
01215                                             const void *Decoder) {
01216   if (RegNo > 31)
01217     return MCDisassembler::Fail;
01218 
01219   unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
01220   Inst.addOperand(MCOperand::CreateReg(Reg));
01221   return MCDisassembler::Success;
01222 }
01223 
01224 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
01225                                        unsigned Offset,
01226                                        uint64_t Address,
01227                                        const void *Decoder) {
01228   int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
01229   Inst.addOperand(MCOperand::CreateImm(BranchOffset));
01230   return MCDisassembler::Success;
01231 }
01232 
01233 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
01234                                      unsigned Insn,
01235                                      uint64_t Address,
01236                                      const void *Decoder) {
01237 
01238   unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
01239   Inst.addOperand(MCOperand::CreateImm(JumpOffset));
01240   return MCDisassembler::Success;
01241 }
01242 
01243 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
01244                                          unsigned Offset,
01245                                          uint64_t Address,
01246                                          const void *Decoder) {
01247   int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
01248 
01249   Inst.addOperand(MCOperand::CreateImm(BranchOffset));
01250   return MCDisassembler::Success;
01251 }
01252 
01253 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
01254                                          unsigned Offset,
01255                                          uint64_t Address,
01256                                          const void *Decoder) {
01257   int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
01258 
01259   Inst.addOperand(MCOperand::CreateImm(BranchOffset));
01260   return MCDisassembler::Success;
01261 }
01262 
01263 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
01264                                          unsigned Offset,
01265                                          uint64_t Address,
01266                                          const void *Decoder) {
01267   int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
01268   Inst.addOperand(MCOperand::CreateImm(BranchOffset));
01269   return MCDisassembler::Success;
01270 }
01271 
01272 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
01273                                        unsigned Insn,
01274                                        uint64_t Address,
01275                                        const void *Decoder) {
01276   unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
01277   Inst.addOperand(MCOperand::CreateImm(JumpOffset));
01278   return MCDisassembler::Success;
01279 }
01280 
01281 static DecodeStatus DecodeSimm16(MCInst &Inst,
01282                                  unsigned Insn,
01283                                  uint64_t Address,
01284                                  const void *Decoder) {
01285   Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
01286   return MCDisassembler::Success;
01287 }
01288 
01289 static DecodeStatus DecodeLSAImm(MCInst &Inst,
01290                                  unsigned Insn,
01291                                  uint64_t Address,
01292                                  const void *Decoder) {
01293   // We add one to the immediate field as it was encoded as 'imm - 1'.
01294   Inst.addOperand(MCOperand::CreateImm(Insn + 1));
01295   return MCDisassembler::Success;
01296 }
01297 
01298 static DecodeStatus DecodeInsSize(MCInst &Inst,
01299                                   unsigned Insn,
01300                                   uint64_t Address,
01301                                   const void *Decoder) {
01302   // First we need to grab the pos(lsb) from MCInst.
01303   int Pos = Inst.getOperand(2).getImm();
01304   int Size = (int) Insn - Pos + 1;
01305   Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
01306   return MCDisassembler::Success;
01307 }
01308 
01309 static DecodeStatus DecodeExtSize(MCInst &Inst,
01310                                   unsigned Insn,
01311                                   uint64_t Address,
01312                                   const void *Decoder) {
01313   int Size = (int) Insn  + 1;
01314   Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
01315   return MCDisassembler::Success;
01316 }
01317 
01318 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
01319                                      uint64_t Address, const void *Decoder) {
01320   Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
01321   return MCDisassembler::Success;
01322 }
01323 
01324 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
01325                                      uint64_t Address, const void *Decoder) {
01326   Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
01327   return MCDisassembler::Success;
01328 }