1 //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is part of the Mips Disassembler.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "Mips.h"
15 #include "MipsRegisterInfo.h"
16 #include "MipsSubtarget.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/MCSubtargetInfo.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/TargetRegistry.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "mips-disassembler"
28 
29 typedef MCDisassembler::DecodeStatus DecodeStatus;
30 
31 namespace {
32 
33 class MipsDisassembler : public MCDisassembler {
34   bool IsMicroMips;
35   bool IsBigEndian;
36 public:
37   MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
38       : MCDisassembler(STI, Ctx),
39         IsMicroMips(STI.getFeatureBits()[Mips::FeatureMicroMips]),
40         IsBigEndian(IsBigEndian) {}
41 
42   bool hasMips3() const { return STI.getFeatureBits()[Mips::FeatureMips3]; }
43   bool hasMips32() const { return STI.getFeatureBits()[Mips::FeatureMips32]; }
44   bool hasMips32r6() const {
45     return STI.getFeatureBits()[Mips::FeatureMips32r6];
46   }
47   bool isFP64() const { return STI.getFeatureBits()[Mips::FeatureFP64Bit]; }
48 
49   bool isGP64() const { return STI.getFeatureBits()[Mips::FeatureGP64Bit]; }
50 
51   bool hasCnMips() const { return STI.getFeatureBits()[Mips::FeatureCnMips]; }
52 
53   bool hasCOP3() const {
54     // Only present in MIPS-I and MIPS-II
55     return !hasMips32() && !hasMips3();
56   }
57 
58   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
59                               ArrayRef<uint8_t> Bytes, uint64_t Address,
60                               raw_ostream &VStream,
61                               raw_ostream &CStream) const override;
62 };
63 
64 } // end anonymous namespace
65 
66 // Forward declare these because the autogenerated code will reference them.
67 // Definitions are further down.
68 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
69                                              unsigned RegNo,
70                                              uint64_t Address,
71                                              const void *Decoder);
72 
73 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
74                                                  unsigned RegNo,
75                                                  uint64_t Address,
76                                                  const void *Decoder);
77 
78 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
79                                                unsigned RegNo,
80                                                uint64_t Address,
81                                                const void *Decoder);
82 
83 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
84                                                    unsigned RegNo,
85                                                    uint64_t Address,
86                                                    const void *Decoder);
87 
88 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
89                                                     unsigned RegNo,
90                                                     uint64_t Address,
91                                                     const void *Decoder);
92 
93 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
94                                              unsigned RegNo,
95                                              uint64_t Address,
96                                              const void *Decoder);
97 
98 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
99                                            unsigned Insn,
100                                            uint64_t Address,
101                                            const void *Decoder);
102 
103 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
104                                             unsigned RegNo,
105                                             uint64_t Address,
106                                             const void *Decoder);
107 
108 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
109                                              unsigned RegNo,
110                                              uint64_t Address,
111                                              const void *Decoder);
112 
113 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
114                                              unsigned RegNo,
115                                              uint64_t Address,
116                                              const void *Decoder);
117 
118 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
119                                            unsigned RegNo,
120                                            uint64_t Address,
121                                            const void *Decoder);
122 
123 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
124                                            unsigned RegNo,
125                                            uint64_t Address,
126                                            const void *Decoder);
127 
128 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
129                                              uint64_t Address,
130                                              const void *Decoder);
131 
132 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
133                                               unsigned Insn,
134                                               uint64_t Address,
135                                               const void *Decoder);
136 
137 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
138                                               unsigned RegNo,
139                                               uint64_t Address,
140                                               const void *Decoder);
141 
142 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
143                                                 unsigned RegNo,
144                                                 uint64_t Address,
145                                                 const void *Decoder);
146 
147 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
148                                                unsigned RegNo,
149                                                uint64_t Address,
150                                                const void *Decoder);
151 
152 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
153                                                unsigned RegNo,
154                                                uint64_t Address,
155                                                const void *Decoder);
156 
157 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
158                                                unsigned RegNo,
159                                                uint64_t Address,
160                                                const void *Decoder);
161 
162 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
163                                                unsigned RegNo,
164                                                uint64_t Address,
165                                                const void *Decoder);
166 
167 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
168                                                unsigned RegNo,
169                                                uint64_t Address,
170                                                const void *Decoder);
171 
172 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
173                                                unsigned RegNo,
174                                                uint64_t Address,
175                                                const void *Decoder);
176 
177 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
178                                                unsigned RegNo,
179                                                uint64_t Address,
180                                                const void *Decoder);
181 
182 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
183                                             unsigned RegNo,
184                                             uint64_t Address,
185                                             const void *Decoder);
186 
187 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
188                                             unsigned RegNo,
189                                             uint64_t Address,
190                                             const void *Decoder);
191 
192 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
193                                        unsigned Offset,
194                                        uint64_t Address,
195                                        const void *Decoder);
196 
197 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
198                                      unsigned Insn,
199                                      uint64_t Address,
200                                      const void *Decoder);
201 
202 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
203                                          unsigned Offset,
204                                          uint64_t Address,
205                                          const void *Decoder);
206 
207 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
208                                          unsigned Offset,
209                                          uint64_t Address,
210                                          const void *Decoder);
211 
212 // DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
213 // shifted left by 1 bit.
214 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
215                                           unsigned Offset,
216                                           uint64_t Address,
217                                           const void *Decoder);
218 
219 // DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
220 // shifted left by 1 bit.
221 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
222                                            unsigned Offset,
223                                            uint64_t Address,
224                                            const void *Decoder);
225 
226 // DecodeBranchTargetMM - Decode microMIPS branch offset, which is
227 // shifted left by 1 bit.
228 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
229                                          unsigned Offset,
230                                          uint64_t Address,
231                                          const void *Decoder);
232 
233 // DecodeBranchTarget26MM - Decode microMIPS branch offset, which is
234 // shifted left by 1 bit.
235 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
236                                            unsigned Offset,
237                                            uint64_t Address,
238                                            const void *Decoder);
239 
240 // DecodeJumpTargetMM - Decode microMIPS jump target, which is
241 // shifted left by 1 bit.
242 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
243                                        unsigned Insn,
244                                        uint64_t Address,
245                                        const void *Decoder);
246 
247 static DecodeStatus DecodeMem(MCInst &Inst,
248                               unsigned Insn,
249                               uint64_t Address,
250                               const void *Decoder);
251 
252 static DecodeStatus DecodeMemEVA(MCInst &Inst,
253                                  unsigned Insn,
254                                  uint64_t Address,
255                                  const void *Decoder);
256 
257 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
258                                     unsigned Insn,
259                                     uint64_t Address,
260                                     const void *Decoder);
261 
262 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
263                                      unsigned Insn,
264                                      uint64_t Address,
265                                      const void *Decoder);
266 
267 static DecodeStatus DecodeCacheOp(MCInst &Inst,
268                               unsigned Insn,
269                               uint64_t Address,
270                               const void *Decoder);
271 
272 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
273                                              unsigned Insn,
274                                              uint64_t Address,
275                                              const void *Decoder);
276 
277 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
278                                     unsigned Insn,
279                                     uint64_t Address,
280                                     const void *Decoder);
281 
282 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
283                                        unsigned Insn,
284                                        uint64_t Address,
285                                        const void *Decoder);
286 
287 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
288                                     unsigned Insn,
289                                     uint64_t Address,
290                                     const void *Decoder);
291 
292 static DecodeStatus DecodeSyncI(MCInst &Inst,
293                                 unsigned Insn,
294                                 uint64_t Address,
295                                 const void *Decoder);
296 
297 static DecodeStatus DecodeSynciR6(MCInst &Inst,
298                                   unsigned Insn,
299                                   uint64_t Address,
300                                   const void *Decoder);
301 
302 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
303                                     uint64_t Address, const void *Decoder);
304 
305 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
306                                     unsigned Insn,
307                                     uint64_t Address,
308                                     const void *Decoder);
309 
310 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
311                                           unsigned Insn,
312                                           uint64_t Address,
313                                           const void *Decoder);
314 
315 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
316                                           unsigned Insn,
317                                           uint64_t Address,
318                                           const void *Decoder);
319 
320 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
321                                                unsigned Insn,
322                                                uint64_t Address,
323                                                const void *Decoder);
324 
325 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
326                                     unsigned Insn,
327                                     uint64_t Address,
328                                     const void *Decoder);
329 
330 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
331                                      unsigned Insn,
332                                      uint64_t Address,
333                                      const void *Decoder);
334 
335 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
336                                      unsigned Insn,
337                                      uint64_t Address,
338                                      const void *Decoder);
339 
340 static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
341                                uint64_t Address,
342                                const void *Decoder);
343 
344 static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
345                                uint64_t Address,
346                                const void *Decoder);
347 
348 static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
349                                uint64_t Address,
350                                const void *Decoder);
351 
352 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
353                                uint64_t Address,
354                                const void *Decoder);
355 
356 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
357                                        unsigned Insn,
358                                        uint64_t Address,
359                                        const void *Decoder);
360 
361 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
362                                        unsigned Value,
363                                        uint64_t Address,
364                                        const void *Decoder);
365 
366 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
367                                   unsigned Value,
368                                   uint64_t Address,
369                                   const void *Decoder);
370 
371 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
372                                               unsigned Value,
373                                               uint64_t Address,
374                                               const void *Decoder);
375 
376 template <unsigned Bits, int Offset, int Scale>
377 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
378                                                  uint64_t Address,
379                                                  const void *Decoder);
380 
381 template <unsigned Bits, int Offset>
382 static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
383                                          uint64_t Address,
384                                          const void *Decoder) {
385   return DecodeUImmWithOffsetAndScale<Bits, Offset, 1>(Inst, Value, Address,
386                                                        Decoder);
387 }
388 
389 template <unsigned Bits, int Offset = 0, int ScaleBy = 1>
390 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
391                                                  uint64_t Address,
392                                                  const void *Decoder);
393 
394 static DecodeStatus DecodeInsSize(MCInst &Inst,
395                                   unsigned Insn,
396                                   uint64_t Address,
397                                   const void *Decoder);
398 
399 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
400                                      uint64_t Address, const void *Decoder);
401 
402 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
403                                      uint64_t Address, const void *Decoder);
404 
405 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
406                                   uint64_t Address, const void *Decoder);
407 
408 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
409                                     uint64_t Address, const void *Decoder);
410 
411 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
412                                      uint64_t Address, const void *Decoder);
413 
414 /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
415 /// handle.
416 template <typename InsnType>
417 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
418                                    const void *Decoder);
419 
420 template <typename InsnType>
421 static DecodeStatus
422 DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
423                       const void *Decoder);
424 
425 template <typename InsnType>
426 static DecodeStatus
427 DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
428                        const void *Decoder);
429 
430 template <typename InsnType>
431 static DecodeStatus
432 DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
433                        const void *Decoder);
434 
435 template <typename InsnType>
436 static DecodeStatus
437 DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
438                        const void *Decoder);
439 
440 template <typename InsnType>
441 static DecodeStatus
442 DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
443                       const void *Decoder);
444 
445 template <typename InsnType>
446 static DecodeStatus
447 DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
448                        const void *Decoder);
449 
450 template <typename InsnType>
451 static DecodeStatus
452 DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
453                           const void *Decoder);
454 
455 template <typename InsnType>
456 static DecodeStatus
457 DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn, uint64_t Address,
458                           const void *Decoder);
459 
460 static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
461                                          uint64_t Address,
462                                          const void *Decoder);
463 
464 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
465                                            uint64_t Address,
466                                            const void *Decoder);
467 
468 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
469                                        uint64_t Address,
470                                        const void *Decoder);
471 
472 namespace llvm {
473 extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
474               TheMips64elTarget;
475 }
476 
477 static MCDisassembler *createMipsDisassembler(
478                        const Target &T,
479                        const MCSubtargetInfo &STI,
480                        MCContext &Ctx) {
481   return new MipsDisassembler(STI, Ctx, true);
482 }
483 
484 static MCDisassembler *createMipselDisassembler(
485                        const Target &T,
486                        const MCSubtargetInfo &STI,
487                        MCContext &Ctx) {
488   return new MipsDisassembler(STI, Ctx, false);
489 }
490 
491 extern "C" void LLVMInitializeMipsDisassembler() {
492   // Register the disassembler.
493   TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
494                                          createMipsDisassembler);
495   TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
496                                          createMipselDisassembler);
497   TargetRegistry::RegisterMCDisassembler(TheMips64Target,
498                                          createMipsDisassembler);
499   TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
500                                          createMipselDisassembler);
501 }
502 
503 #include "MipsGenDisassemblerTables.inc"
504 
505 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
506   const MipsDisassembler *Dis = static_cast<const MipsDisassembler*>(D);
507   const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
508   return *(RegInfo->getRegClass(RC).begin() + RegNo);
509 }
510 
511 template <typename InsnType>
512 static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
513                                    const void *Decoder) {
514   typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
515   // The size of the n field depends on the element size
516   // The register class also depends on this.
517   InsnType tmp = fieldFromInstruction(insn, 17, 5);
518   unsigned NSize = 0;
519   DecodeFN RegDecoder = nullptr;
520   if ((tmp & 0x18) == 0x00) { // INSVE_B
521     NSize = 4;
522     RegDecoder = DecodeMSA128BRegisterClass;
523   } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
524     NSize = 3;
525     RegDecoder = DecodeMSA128HRegisterClass;
526   } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
527     NSize = 2;
528     RegDecoder = DecodeMSA128WRegisterClass;
529   } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
530     NSize = 1;
531     RegDecoder = DecodeMSA128DRegisterClass;
532   } else
533     llvm_unreachable("Invalid encoding");
534 
535   assert(NSize != 0 && RegDecoder != nullptr);
536 
537   // $wd
538   tmp = fieldFromInstruction(insn, 6, 5);
539   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
540     return MCDisassembler::Fail;
541   // $wd_in
542   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
543     return MCDisassembler::Fail;
544   // $n
545   tmp = fieldFromInstruction(insn, 16, NSize);
546   MI.addOperand(MCOperand::createImm(tmp));
547   // $ws
548   tmp = fieldFromInstruction(insn, 11, 5);
549   if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
550     return MCDisassembler::Fail;
551   // $n2
552   MI.addOperand(MCOperand::createImm(0));
553 
554   return MCDisassembler::Success;
555 }
556 
557 template <typename InsnType>
558 static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
559                                           uint64_t Address,
560                                           const void *Decoder) {
561   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
562   // (otherwise we would have matched the ADDI instruction from the earlier
563   // ISA's instead).
564   //
565   // We have:
566   //    0b001000 sssss ttttt iiiiiiiiiiiiiiii
567   //      BOVC if rs >= rt
568   //      BEQZALC if rs == 0 && rt != 0
569   //      BEQC if rs < rt && rs != 0
570 
571   InsnType Rs = fieldFromInstruction(insn, 21, 5);
572   InsnType Rt = fieldFromInstruction(insn, 16, 5);
573   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
574   bool HasRs = false;
575 
576   if (Rs >= Rt) {
577     MI.setOpcode(Mips::BOVC);
578     HasRs = true;
579   } else if (Rs != 0 && Rs < Rt) {
580     MI.setOpcode(Mips::BEQC);
581     HasRs = true;
582   } else
583     MI.setOpcode(Mips::BEQZALC);
584 
585   if (HasRs)
586     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
587                                        Rs)));
588 
589   MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
590                                      Rt)));
591   MI.addOperand(MCOperand::createImm(Imm));
592 
593   return MCDisassembler::Success;
594 }
595 
596 template <typename InsnType>
597 static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
598                                            uint64_t Address,
599                                            const void *Decoder) {
600   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
601   // (otherwise we would have matched the ADDI instruction from the earlier
602   // ISA's instead).
603   //
604   // We have:
605   //    0b011000 sssss ttttt iiiiiiiiiiiiiiii
606   //      BNVC if rs >= rt
607   //      BNEZALC if rs == 0 && rt != 0
608   //      BNEC if rs < rt && rs != 0
609 
610   InsnType Rs = fieldFromInstruction(insn, 21, 5);
611   InsnType Rt = fieldFromInstruction(insn, 16, 5);
612   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
613   bool HasRs = false;
614 
615   if (Rs >= Rt) {
616     MI.setOpcode(Mips::BNVC);
617     HasRs = true;
618   } else if (Rs != 0 && Rs < Rt) {
619     MI.setOpcode(Mips::BNEC);
620     HasRs = true;
621   } else
622     MI.setOpcode(Mips::BNEZALC);
623 
624   if (HasRs)
625     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
626                                        Rs)));
627 
628   MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
629                                      Rt)));
630   MI.addOperand(MCOperand::createImm(Imm));
631 
632   return MCDisassembler::Success;
633 }
634 
635 template <typename InsnType>
636 static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
637                                            uint64_t Address,
638                                            const void *Decoder) {
639   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
640   // (otherwise we would have matched the BLEZL instruction from the earlier
641   // ISA's instead).
642   //
643   // We have:
644   //    0b010110 sssss ttttt iiiiiiiiiiiiiiii
645   //      Invalid if rs == 0
646   //      BLEZC   if rs == 0  && rt != 0
647   //      BGEZC   if rs == rt && rt != 0
648   //      BGEC    if rs != rt && rs != 0  && rt != 0
649 
650   InsnType Rs = fieldFromInstruction(insn, 21, 5);
651   InsnType Rt = fieldFromInstruction(insn, 16, 5);
652   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
653   bool HasRs = false;
654 
655   if (Rt == 0)
656     return MCDisassembler::Fail;
657   else if (Rs == 0)
658     MI.setOpcode(Mips::BLEZC);
659   else if (Rs == Rt)
660     MI.setOpcode(Mips::BGEZC);
661   else {
662     HasRs = true;
663     MI.setOpcode(Mips::BGEC);
664   }
665 
666   if (HasRs)
667     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
668                                        Rs)));
669 
670   MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
671                                      Rt)));
672 
673   MI.addOperand(MCOperand::createImm(Imm));
674 
675   return MCDisassembler::Success;
676 }
677 
678 template <typename InsnType>
679 static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
680                                            uint64_t Address,
681                                            const void *Decoder) {
682   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
683   // (otherwise we would have matched the BGTZL instruction from the earlier
684   // ISA's instead).
685   //
686   // We have:
687   //    0b010111 sssss ttttt iiiiiiiiiiiiiiii
688   //      Invalid if rs == 0
689   //      BGTZC   if rs == 0  && rt != 0
690   //      BLTZC   if rs == rt && rt != 0
691   //      BLTC    if rs != rt && rs != 0  && rt != 0
692 
693   bool HasRs = false;
694 
695   InsnType Rs = fieldFromInstruction(insn, 21, 5);
696   InsnType Rt = fieldFromInstruction(insn, 16, 5);
697   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
698 
699   if (Rt == 0)
700     return MCDisassembler::Fail;
701   else if (Rs == 0)
702     MI.setOpcode(Mips::BGTZC);
703   else if (Rs == Rt)
704     MI.setOpcode(Mips::BLTZC);
705   else {
706     MI.setOpcode(Mips::BLTC);
707     HasRs = true;
708   }
709 
710   if (HasRs)
711     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
712                                               Rs)));
713 
714   MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
715                                      Rt)));
716 
717   MI.addOperand(MCOperand::createImm(Imm));
718 
719   return MCDisassembler::Success;
720 }
721 
722 template <typename InsnType>
723 static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
724                                           uint64_t Address,
725                                           const void *Decoder) {
726   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
727   // (otherwise we would have matched the BGTZ instruction from the earlier
728   // ISA's instead).
729   //
730   // We have:
731   //    0b000111 sssss ttttt iiiiiiiiiiiiiiii
732   //      BGTZ    if rt == 0
733   //      BGTZALC if rs == 0 && rt != 0
734   //      BLTZALC if rs != 0 && rs == rt
735   //      BLTUC   if rs != 0 && rs != rt
736 
737   InsnType Rs = fieldFromInstruction(insn, 21, 5);
738   InsnType Rt = fieldFromInstruction(insn, 16, 5);
739   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
740   bool HasRs = false;
741   bool HasRt = false;
742 
743   if (Rt == 0) {
744     MI.setOpcode(Mips::BGTZ);
745     HasRs = true;
746   } else if (Rs == 0) {
747     MI.setOpcode(Mips::BGTZALC);
748     HasRt = true;
749   } else if (Rs == Rt) {
750     MI.setOpcode(Mips::BLTZALC);
751     HasRs = true;
752   } else {
753     MI.setOpcode(Mips::BLTUC);
754     HasRs = true;
755     HasRt = true;
756   }
757 
758   if (HasRs)
759     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
760                                        Rs)));
761 
762   if (HasRt)
763     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
764                                        Rt)));
765 
766   MI.addOperand(MCOperand::createImm(Imm));
767 
768   return MCDisassembler::Success;
769 }
770 
771 template <typename InsnType>
772 static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
773                                            uint64_t Address,
774                                            const void *Decoder) {
775   // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
776   // (otherwise we would have matched the BLEZL instruction from the earlier
777   // ISA's instead).
778   //
779   // We have:
780   //    0b000110 sssss ttttt iiiiiiiiiiiiiiii
781   //      Invalid   if rs == 0
782   //      BLEZALC   if rs == 0  && rt != 0
783   //      BGEZALC   if rs == rt && rt != 0
784   //      BGEUC     if rs != rt && rs != 0  && rt != 0
785 
786   InsnType Rs = fieldFromInstruction(insn, 21, 5);
787   InsnType Rt = fieldFromInstruction(insn, 16, 5);
788   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
789   bool HasRs = false;
790 
791   if (Rt == 0)
792     return MCDisassembler::Fail;
793   else if (Rs == 0)
794     MI.setOpcode(Mips::BLEZALC);
795   else if (Rs == Rt)
796     MI.setOpcode(Mips::BGEZALC);
797   else {
798     HasRs = true;
799     MI.setOpcode(Mips::BGEUC);
800   }
801 
802   if (HasRs)
803     MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
804                                        Rs)));
805   MI.addOperand(MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID,
806                                      Rt)));
807 
808   MI.addOperand(MCOperand::createImm(Imm));
809 
810   return MCDisassembler::Success;
811 }
812 
813 /// Read two bytes from the ArrayRef and return 16 bit halfword sorted
814 /// according to the given endianess.
815 static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
816                                       uint64_t &Size, uint32_t &Insn,
817                                       bool IsBigEndian) {
818   // We want to read exactly 2 Bytes of data.
819   if (Bytes.size() < 2) {
820     Size = 0;
821     return MCDisassembler::Fail;
822   }
823 
824   if (IsBigEndian) {
825     Insn = (Bytes[0] << 8) | Bytes[1];
826   } else {
827     Insn = (Bytes[1] << 8) | Bytes[0];
828   }
829 
830   return MCDisassembler::Success;
831 }
832 
833 /// Read four bytes from the ArrayRef and return 32 bit word sorted
834 /// according to the given endianess
835 static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
836                                       uint64_t &Size, uint32_t &Insn,
837                                       bool IsBigEndian, bool IsMicroMips) {
838   // We want to read exactly 4 Bytes of data.
839   if (Bytes.size() < 4) {
840     Size = 0;
841     return MCDisassembler::Fail;
842   }
843 
844   // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
845   // always precede the low 16 bits in the instruction stream (that is, they
846   // are placed at lower addresses in the instruction stream).
847   //
848   // microMIPS byte ordering:
849   //   Big-endian:    0 | 1 | 2 | 3
850   //   Little-endian: 1 | 0 | 3 | 2
851 
852   if (IsBigEndian) {
853     // Encoded as a big-endian 32-bit word in the stream.
854     Insn =
855         (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
856   } else {
857     if (IsMicroMips) {
858       Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
859              (Bytes[1] << 24);
860     } else {
861       Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
862              (Bytes[3] << 24);
863     }
864   }
865 
866   return MCDisassembler::Success;
867 }
868 
869 DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
870                                               ArrayRef<uint8_t> Bytes,
871                                               uint64_t Address,
872                                               raw_ostream &VStream,
873                                               raw_ostream &CStream) const {
874   uint32_t Insn;
875   DecodeStatus Result;
876 
877   if (IsMicroMips) {
878     Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
879     if (Result == MCDisassembler::Fail)
880       return MCDisassembler::Fail;
881 
882     if (hasMips32r6()) {
883       DEBUG(dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
884       // Calling the auto-generated decoder function for microMIPS32R6
885       // (and microMIPS64R6) 16-bit instructions.
886       Result = decodeInstruction(DecoderTableMicroMipsR616, Instr, Insn,
887                                  Address, this, STI);
888       if (Result != MCDisassembler::Fail) {
889         Size = 2;
890         return Result;
891       }
892     }
893 
894     DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
895     // Calling the auto-generated decoder function for microMIPS 16-bit
896     // instructions.
897     Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
898                                this, STI);
899     if (Result != MCDisassembler::Fail) {
900       Size = 2;
901       return Result;
902     }
903 
904     Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
905     if (Result == MCDisassembler::Fail)
906       return MCDisassembler::Fail;
907 
908     if (hasMips32r6()) {
909       DEBUG(dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
910       // Calling the auto-generated decoder function.
911       Result = decodeInstruction(DecoderTableMicroMipsR632, Instr, Insn, Address,
912                                  this, STI);
913       if (Result != MCDisassembler::Fail) {
914         Size = 4;
915         return Result;
916       }
917     }
918 
919     DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
920     // Calling the auto-generated decoder function.
921     Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
922                                this, STI);
923     if (Result != MCDisassembler::Fail) {
924       Size = 4;
925       return Result;
926     }
927 
928     if (hasMips32r6() && isFP64()) {
929       DEBUG(dbgs() << "Trying MicroMips32r6FP64 table (32-bit opcodes):\n");
930       Result = decodeInstruction(DecoderTableMicroMips32r6FP6432, Instr, Insn,
931                                  Address, this, STI);
932       if (Result != MCDisassembler::Fail) {
933         Size = 4;
934         return Result;
935       }
936     }
937 
938     // This is an invalid instruction. Let the disassembler move forward by the
939     // minimum instruction size.
940     Size = 2;
941     return MCDisassembler::Fail;
942   }
943 
944   Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
945   if (Result == MCDisassembler::Fail) {
946     Size = 4;
947     return MCDisassembler::Fail;
948   }
949 
950   if (hasCOP3()) {
951     DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
952     Result =
953         decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
954     if (Result != MCDisassembler::Fail) {
955       Size = 4;
956       return Result;
957     }
958   }
959 
960   if (hasMips32r6() && isGP64()) {
961     DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
962     Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
963                                Address, this, STI);
964     if (Result != MCDisassembler::Fail) {
965       Size = 4;
966       return Result;
967     }
968   }
969 
970   if (hasMips32r6()) {
971     DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
972     Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
973                                Address, this, STI);
974     if (Result != MCDisassembler::Fail) {
975       Size = 4;
976       return Result;
977     }
978   }
979 
980   if (hasCnMips()) {
981     DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
982     Result = decodeInstruction(DecoderTableCnMips32, Instr, Insn,
983                                Address, this, STI);
984     if (Result != MCDisassembler::Fail) {
985       Size = 4;
986       return Result;
987     }
988   }
989 
990   if (isGP64()) {
991     DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
992     Result = decodeInstruction(DecoderTableMips6432, Instr, Insn,
993                                Address, this, STI);
994     if (Result != MCDisassembler::Fail) {
995       Size = 4;
996       return Result;
997     }
998   }
999 
1000   DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1001   // Calling the auto-generated decoder function.
1002   Result =
1003       decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
1004   if (Result != MCDisassembler::Fail) {
1005     Size = 4;
1006     return Result;
1007   }
1008 
1009   Size = 4;
1010   return MCDisassembler::Fail;
1011 }
1012 
1013 static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
1014                                                  unsigned RegNo,
1015                                                  uint64_t Address,
1016                                                  const void *Decoder) {
1017 
1018   return MCDisassembler::Fail;
1019 
1020 }
1021 
1022 static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
1023                                              unsigned RegNo,
1024                                              uint64_t Address,
1025                                              const void *Decoder) {
1026 
1027   if (RegNo > 31)
1028     return MCDisassembler::Fail;
1029 
1030   unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
1031   Inst.addOperand(MCOperand::createReg(Reg));
1032   return MCDisassembler::Success;
1033 }
1034 
1035 static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
1036                                                unsigned RegNo,
1037                                                uint64_t Address,
1038                                                const void *Decoder) {
1039   if (RegNo > 7)
1040     return MCDisassembler::Fail;
1041   unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
1042   Inst.addOperand(MCOperand::createReg(Reg));
1043   return MCDisassembler::Success;
1044 }
1045 
1046 static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
1047                                                    unsigned RegNo,
1048                                                    uint64_t Address,
1049                                                    const void *Decoder) {
1050   if (RegNo > 7)
1051     return MCDisassembler::Fail;
1052   unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1053   Inst.addOperand(MCOperand::createReg(Reg));
1054   return MCDisassembler::Success;
1055 }
1056 
1057 static DecodeStatus DecodeGPRMM16MovePRegisterClass(MCInst &Inst,
1058                                                     unsigned RegNo,
1059                                                     uint64_t Address,
1060                                                     const void *Decoder) {
1061   if (RegNo > 7)
1062     return MCDisassembler::Fail;
1063   unsigned Reg = getReg(Decoder, Mips::GPRMM16MovePRegClassID, RegNo);
1064   Inst.addOperand(MCOperand::createReg(Reg));
1065   return MCDisassembler::Success;
1066 }
1067 
1068 static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1069                                              unsigned RegNo,
1070                                              uint64_t Address,
1071                                              const void *Decoder) {
1072   if (RegNo > 31)
1073     return MCDisassembler::Fail;
1074   unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
1075   Inst.addOperand(MCOperand::createReg(Reg));
1076   return MCDisassembler::Success;
1077 }
1078 
1079 static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1080                                            unsigned RegNo,
1081                                            uint64_t Address,
1082                                            const void *Decoder) {
1083   if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1084     return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1085 
1086   return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1087 }
1088 
1089 static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1090                                             unsigned RegNo,
1091                                             uint64_t Address,
1092                                             const void *Decoder) {
1093   return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1094 }
1095 
1096 static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1097                                              unsigned RegNo,
1098                                              uint64_t Address,
1099                                              const void *Decoder) {
1100   if (RegNo > 31)
1101     return MCDisassembler::Fail;
1102 
1103   unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1104   Inst.addOperand(MCOperand::createReg(Reg));
1105   return MCDisassembler::Success;
1106 }
1107 
1108 static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1109                                              unsigned RegNo,
1110                                              uint64_t Address,
1111                                              const void *Decoder) {
1112   if (RegNo > 31)
1113     return MCDisassembler::Fail;
1114 
1115   unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1116   Inst.addOperand(MCOperand::createReg(Reg));
1117   return MCDisassembler::Success;
1118 }
1119 
1120 static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1121                                            unsigned RegNo,
1122                                            uint64_t Address,
1123                                            const void *Decoder) {
1124   if (RegNo > 31)
1125     return MCDisassembler::Fail;
1126   unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1127   Inst.addOperand(MCOperand::createReg(Reg));
1128   return MCDisassembler::Success;
1129 }
1130 
1131 static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1132                                            unsigned RegNo,
1133                                            uint64_t Address,
1134                                            const void *Decoder) {
1135   if (RegNo > 7)
1136     return MCDisassembler::Fail;
1137   unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1138   Inst.addOperand(MCOperand::createReg(Reg));
1139   return MCDisassembler::Success;
1140 }
1141 
1142 static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1143                                              uint64_t Address,
1144                                              const void *Decoder) {
1145   if (RegNo > 31)
1146     return MCDisassembler::Fail;
1147 
1148   unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1149   Inst.addOperand(MCOperand::createReg(Reg));
1150   return MCDisassembler::Success;
1151 }
1152 
1153 static DecodeStatus DecodeMem(MCInst &Inst,
1154                               unsigned Insn,
1155                               uint64_t Address,
1156                               const void *Decoder) {
1157   int Offset = SignExtend32<16>(Insn & 0xffff);
1158   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1159   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1160 
1161   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1162   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1163 
1164   if (Inst.getOpcode() == Mips::SC ||
1165       Inst.getOpcode() == Mips::SCD)
1166     Inst.addOperand(MCOperand::createReg(Reg));
1167 
1168   Inst.addOperand(MCOperand::createReg(Reg));
1169   Inst.addOperand(MCOperand::createReg(Base));
1170   Inst.addOperand(MCOperand::createImm(Offset));
1171 
1172   return MCDisassembler::Success;
1173 }
1174 
1175 static DecodeStatus DecodeMemEVA(MCInst &Inst,
1176                                  unsigned Insn,
1177                                  uint64_t Address,
1178                                  const void *Decoder) {
1179   int Offset = SignExtend32<9>(Insn >> 7);
1180   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1181   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1182 
1183   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1184   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1185 
1186    if (Inst.getOpcode() == Mips::SCE)
1187      Inst.addOperand(MCOperand::createReg(Reg));
1188 
1189   Inst.addOperand(MCOperand::createReg(Reg));
1190   Inst.addOperand(MCOperand::createReg(Base));
1191   Inst.addOperand(MCOperand::createImm(Offset));
1192 
1193   return MCDisassembler::Success;
1194 }
1195 
1196 static DecodeStatus DecodeLoadByte9(MCInst &Inst,
1197                                     unsigned Insn,
1198                                     uint64_t Address,
1199                                     const void *Decoder) {
1200   int Offset = SignExtend32<9>(Insn & 0x1ff);
1201   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1202   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1203 
1204   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1205   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1206 
1207   Inst.addOperand(MCOperand::createReg(Reg));
1208   Inst.addOperand(MCOperand::createReg(Base));
1209   Inst.addOperand(MCOperand::createImm(Offset));
1210 
1211   return MCDisassembler::Success;
1212 }
1213 
1214 static DecodeStatus DecodeLoadByte15(MCInst &Inst,
1215                                      unsigned Insn,
1216                                      uint64_t Address,
1217                                      const void *Decoder) {
1218   int Offset = SignExtend32<16>(Insn & 0xffff);
1219   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1220   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1221 
1222   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1223   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1224 
1225   Inst.addOperand(MCOperand::createReg(Reg));
1226   Inst.addOperand(MCOperand::createReg(Base));
1227   Inst.addOperand(MCOperand::createImm(Offset));
1228 
1229   return MCDisassembler::Success;
1230 }
1231 
1232 static DecodeStatus DecodeCacheOp(MCInst &Inst,
1233                               unsigned Insn,
1234                               uint64_t Address,
1235                               const void *Decoder) {
1236   int Offset = SignExtend32<16>(Insn & 0xffff);
1237   unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1238   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1239 
1240   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1241 
1242   Inst.addOperand(MCOperand::createReg(Base));
1243   Inst.addOperand(MCOperand::createImm(Offset));
1244   Inst.addOperand(MCOperand::createImm(Hint));
1245 
1246   return MCDisassembler::Success;
1247 }
1248 
1249 static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1250                                     unsigned Insn,
1251                                     uint64_t Address,
1252                                     const void *Decoder) {
1253   int Offset = SignExtend32<12>(Insn & 0xfff);
1254   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1255   unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1256 
1257   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1258 
1259   Inst.addOperand(MCOperand::createReg(Base));
1260   Inst.addOperand(MCOperand::createImm(Offset));
1261   Inst.addOperand(MCOperand::createImm(Hint));
1262 
1263   return MCDisassembler::Success;
1264 }
1265 
1266 static DecodeStatus DecodePrefeOpMM(MCInst &Inst,
1267                                     unsigned Insn,
1268                                     uint64_t Address,
1269                                     const void *Decoder) {
1270   int Offset = SignExtend32<9>(Insn & 0x1ff);
1271   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1272   unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1273 
1274   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1275 
1276   Inst.addOperand(MCOperand::createReg(Base));
1277   Inst.addOperand(MCOperand::createImm(Offset));
1278   Inst.addOperand(MCOperand::createImm(Hint));
1279 
1280   return MCDisassembler::Success;
1281 }
1282 
1283 static DecodeStatus DecodeCacheeOp_CacheOpR6(MCInst &Inst,
1284                                              unsigned Insn,
1285                                              uint64_t Address,
1286                                              const void *Decoder) {
1287   int Offset = SignExtend32<9>(Insn >> 7);
1288   unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1289   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1290 
1291   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1292 
1293   Inst.addOperand(MCOperand::createReg(Base));
1294   Inst.addOperand(MCOperand::createImm(Offset));
1295   Inst.addOperand(MCOperand::createImm(Hint));
1296 
1297   return MCDisassembler::Success;
1298 }
1299 
1300 static DecodeStatus DecodeStoreEvaOpMM(MCInst &Inst,
1301                                        unsigned Insn,
1302                                        uint64_t Address,
1303                                        const void *Decoder) {
1304   int Offset = SignExtend32<9>(Insn & 0x1ff);
1305   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1306   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1307 
1308   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1309   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1310 
1311   Inst.addOperand(MCOperand::createReg(Reg));
1312   Inst.addOperand(MCOperand::createReg(Base));
1313   Inst.addOperand(MCOperand::createImm(Offset));
1314 
1315   return MCDisassembler::Success;
1316 }
1317 
1318 static DecodeStatus DecodeSyncI(MCInst &Inst,
1319                               unsigned Insn,
1320                               uint64_t Address,
1321                               const void *Decoder) {
1322   int Offset = SignExtend32<16>(Insn & 0xffff);
1323   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1324 
1325   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1326 
1327   Inst.addOperand(MCOperand::createReg(Base));
1328   Inst.addOperand(MCOperand::createImm(Offset));
1329 
1330   return MCDisassembler::Success;
1331 }
1332 
1333 static DecodeStatus DecodeSynciR6(MCInst &Inst,
1334                                   unsigned Insn,
1335                                   uint64_t Address,
1336                                   const void *Decoder) {
1337   int Immediate = SignExtend32<16>(Insn & 0xffff);
1338   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1339 
1340   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1341 
1342   Inst.addOperand(MCOperand::createReg(Base));
1343   Inst.addOperand(MCOperand::createImm(Immediate));
1344 
1345   return MCDisassembler::Success;
1346 }
1347 
1348 static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1349                                     uint64_t Address, const void *Decoder) {
1350   int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1351   unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1352   unsigned Base = fieldFromInstruction(Insn, 11, 5);
1353 
1354   Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1355   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1356 
1357   Inst.addOperand(MCOperand::createReg(Reg));
1358   Inst.addOperand(MCOperand::createReg(Base));
1359 
1360   // The immediate field of an LD/ST instruction is scaled which means it must
1361   // be multiplied (when decoding) by the size (in bytes) of the instructions'
1362   // data format.
1363   // .b - 1 byte
1364   // .h - 2 bytes
1365   // .w - 4 bytes
1366   // .d - 8 bytes
1367   switch(Inst.getOpcode())
1368   {
1369   default:
1370     assert (0 && "Unexpected instruction");
1371     return MCDisassembler::Fail;
1372     break;
1373   case Mips::LD_B:
1374   case Mips::ST_B:
1375     Inst.addOperand(MCOperand::createImm(Offset));
1376     break;
1377   case Mips::LD_H:
1378   case Mips::ST_H:
1379     Inst.addOperand(MCOperand::createImm(Offset * 2));
1380     break;
1381   case Mips::LD_W:
1382   case Mips::ST_W:
1383     Inst.addOperand(MCOperand::createImm(Offset * 4));
1384     break;
1385   case Mips::LD_D:
1386   case Mips::ST_D:
1387     Inst.addOperand(MCOperand::createImm(Offset * 8));
1388     break;
1389   }
1390 
1391   return MCDisassembler::Success;
1392 }
1393 
1394 static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1395                                     unsigned Insn,
1396                                     uint64_t Address,
1397                                     const void *Decoder) {
1398   unsigned Offset = Insn & 0xf;
1399   unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1400   unsigned Base = fieldFromInstruction(Insn, 4, 3);
1401 
1402   switch (Inst.getOpcode()) {
1403     case Mips::LBU16_MM:
1404     case Mips::LHU16_MM:
1405     case Mips::LW16_MM:
1406       if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1407             == MCDisassembler::Fail)
1408         return MCDisassembler::Fail;
1409       break;
1410     case Mips::SB16_MM:
1411     case Mips::SB16_MMR6:
1412     case Mips::SH16_MM:
1413     case Mips::SH16_MMR6:
1414     case Mips::SW16_MM:
1415     case Mips::SW16_MMR6:
1416       if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1417             == MCDisassembler::Fail)
1418         return MCDisassembler::Fail;
1419       break;
1420   }
1421 
1422   if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1423         == MCDisassembler::Fail)
1424     return MCDisassembler::Fail;
1425 
1426   switch (Inst.getOpcode()) {
1427     case Mips::LBU16_MM:
1428       if (Offset == 0xf)
1429         Inst.addOperand(MCOperand::createImm(-1));
1430       else
1431         Inst.addOperand(MCOperand::createImm(Offset));
1432       break;
1433     case Mips::SB16_MM:
1434     case Mips::SB16_MMR6:
1435       Inst.addOperand(MCOperand::createImm(Offset));
1436       break;
1437     case Mips::LHU16_MM:
1438     case Mips::SH16_MM:
1439     case Mips::SH16_MMR6:
1440       Inst.addOperand(MCOperand::createImm(Offset << 1));
1441       break;
1442     case Mips::LW16_MM:
1443     case Mips::SW16_MM:
1444     case Mips::SW16_MMR6:
1445       Inst.addOperand(MCOperand::createImm(Offset << 2));
1446       break;
1447   }
1448 
1449   return MCDisassembler::Success;
1450 }
1451 
1452 static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1453                                           unsigned Insn,
1454                                           uint64_t Address,
1455                                           const void *Decoder) {
1456   unsigned Offset = Insn & 0x1F;
1457   unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1458 
1459   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1460 
1461   Inst.addOperand(MCOperand::createReg(Reg));
1462   Inst.addOperand(MCOperand::createReg(Mips::SP));
1463   Inst.addOperand(MCOperand::createImm(Offset << 2));
1464 
1465   return MCDisassembler::Success;
1466 }
1467 
1468 static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1469                                           unsigned Insn,
1470                                           uint64_t Address,
1471                                           const void *Decoder) {
1472   unsigned Offset = Insn & 0x7F;
1473   unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1474 
1475   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1476 
1477   Inst.addOperand(MCOperand::createReg(Reg));
1478   Inst.addOperand(MCOperand::createReg(Mips::GP));
1479   Inst.addOperand(MCOperand::createImm(Offset << 2));
1480 
1481   return MCDisassembler::Success;
1482 }
1483 
1484 static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1485                                                unsigned Insn,
1486                                                uint64_t Address,
1487                                                const void *Decoder) {
1488   int Offset;
1489   switch (Inst.getOpcode()) {
1490   case Mips::LWM16_MMR6:
1491   case Mips::SWM16_MMR6:
1492     Offset = fieldFromInstruction(Insn, 4, 4);
1493     break;
1494   default:
1495     Offset = SignExtend32<4>(Insn & 0xf);
1496     break;
1497   }
1498 
1499   if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1500       == MCDisassembler::Fail)
1501     return MCDisassembler::Fail;
1502 
1503   Inst.addOperand(MCOperand::createReg(Mips::SP));
1504   Inst.addOperand(MCOperand::createImm(Offset << 2));
1505 
1506   return MCDisassembler::Success;
1507 }
1508 
1509 static DecodeStatus DecodeMemMMImm9(MCInst &Inst,
1510                                     unsigned Insn,
1511                                     uint64_t Address,
1512                                     const void *Decoder) {
1513   int Offset = SignExtend32<9>(Insn & 0x1ff);
1514   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1515   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1516 
1517   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1518   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1519 
1520   if (Inst.getOpcode() == Mips::SCE_MM)
1521     Inst.addOperand(MCOperand::createReg(Reg));
1522 
1523   Inst.addOperand(MCOperand::createReg(Reg));
1524   Inst.addOperand(MCOperand::createReg(Base));
1525   Inst.addOperand(MCOperand::createImm(Offset));
1526 
1527   return MCDisassembler::Success;
1528 }
1529 
1530 static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1531                                      unsigned Insn,
1532                                      uint64_t Address,
1533                                      const void *Decoder) {
1534   int Offset = SignExtend32<12>(Insn & 0x0fff);
1535   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1536   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1537 
1538   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1539   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1540 
1541   switch (Inst.getOpcode()) {
1542   case Mips::SWM32_MM:
1543   case Mips::LWM32_MM:
1544     if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1545         == MCDisassembler::Fail)
1546       return MCDisassembler::Fail;
1547     Inst.addOperand(MCOperand::createReg(Base));
1548     Inst.addOperand(MCOperand::createImm(Offset));
1549     break;
1550   case Mips::SC_MM:
1551     Inst.addOperand(MCOperand::createReg(Reg));
1552     // fallthrough
1553   default:
1554     Inst.addOperand(MCOperand::createReg(Reg));
1555     if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM ||
1556         Inst.getOpcode() == Mips::LWP_MMR6 || Inst.getOpcode() == Mips::SWP_MMR6)
1557       Inst.addOperand(MCOperand::createReg(Reg+1));
1558 
1559     Inst.addOperand(MCOperand::createReg(Base));
1560     Inst.addOperand(MCOperand::createImm(Offset));
1561   }
1562 
1563   return MCDisassembler::Success;
1564 }
1565 
1566 static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1567                                      unsigned Insn,
1568                                      uint64_t Address,
1569                                      const void *Decoder) {
1570   int Offset = SignExtend32<16>(Insn & 0xffff);
1571   unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1572   unsigned Base = fieldFromInstruction(Insn, 16, 5);
1573 
1574   Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1575   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1576 
1577   Inst.addOperand(MCOperand::createReg(Reg));
1578   Inst.addOperand(MCOperand::createReg(Base));
1579   Inst.addOperand(MCOperand::createImm(Offset));
1580 
1581   return MCDisassembler::Success;
1582 }
1583 
1584 static DecodeStatus DecodeFMem(MCInst &Inst,
1585                                unsigned Insn,
1586                                uint64_t Address,
1587                                const void *Decoder) {
1588   int Offset = SignExtend32<16>(Insn & 0xffff);
1589   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1590   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1591 
1592   Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
1593   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1594 
1595   Inst.addOperand(MCOperand::createReg(Reg));
1596   Inst.addOperand(MCOperand::createReg(Base));
1597   Inst.addOperand(MCOperand::createImm(Offset));
1598 
1599   return MCDisassembler::Success;
1600 }
1601 
1602 static DecodeStatus DecodeFMem2(MCInst &Inst,
1603                                unsigned Insn,
1604                                uint64_t Address,
1605                                const void *Decoder) {
1606   int Offset = SignExtend32<16>(Insn & 0xffff);
1607   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1608   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1609 
1610   Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1611   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1612 
1613   Inst.addOperand(MCOperand::createReg(Reg));
1614   Inst.addOperand(MCOperand::createReg(Base));
1615   Inst.addOperand(MCOperand::createImm(Offset));
1616 
1617   return MCDisassembler::Success;
1618 }
1619 
1620 static DecodeStatus DecodeFMem3(MCInst &Inst,
1621                                unsigned Insn,
1622                                uint64_t Address,
1623                                const void *Decoder) {
1624   int Offset = SignExtend32<16>(Insn & 0xffff);
1625   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1626   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1627 
1628   Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1629   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1630 
1631   Inst.addOperand(MCOperand::createReg(Reg));
1632   Inst.addOperand(MCOperand::createReg(Base));
1633   Inst.addOperand(MCOperand::createImm(Offset));
1634 
1635   return MCDisassembler::Success;
1636 }
1637 
1638 static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1639                                     unsigned Insn,
1640                                     uint64_t Address,
1641                                     const void *Decoder) {
1642   int Offset = SignExtend32<11>(Insn & 0x07ff);
1643   unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1644   unsigned Base = fieldFromInstruction(Insn, 11, 5);
1645 
1646   Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1647   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1648 
1649   Inst.addOperand(MCOperand::createReg(Reg));
1650   Inst.addOperand(MCOperand::createReg(Base));
1651   Inst.addOperand(MCOperand::createImm(Offset));
1652 
1653   return MCDisassembler::Success;
1654 }
1655 static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1656                                        unsigned Insn,
1657                                        uint64_t Address,
1658                                        const void *Decoder) {
1659   int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1660   unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1661   unsigned Base = fieldFromInstruction(Insn, 21, 5);
1662 
1663   Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1664   Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1665 
1666   if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1667     Inst.addOperand(MCOperand::createReg(Rt));
1668   }
1669 
1670   Inst.addOperand(MCOperand::createReg(Rt));
1671   Inst.addOperand(MCOperand::createReg(Base));
1672   Inst.addOperand(MCOperand::createImm(Offset));
1673 
1674   return MCDisassembler::Success;
1675 }
1676 
1677 static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1678                                               unsigned RegNo,
1679                                               uint64_t Address,
1680                                               const void *Decoder) {
1681   // Currently only hardware register 29 is supported.
1682   if (RegNo != 29)
1683     return  MCDisassembler::Fail;
1684   Inst.addOperand(MCOperand::createReg(Mips::HWR29));
1685   return MCDisassembler::Success;
1686 }
1687 
1688 static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1689                                               unsigned RegNo,
1690                                               uint64_t Address,
1691                                               const void *Decoder) {
1692   if (RegNo > 30 || RegNo %2)
1693     return MCDisassembler::Fail;
1694 
1695   ;
1696   unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1697   Inst.addOperand(MCOperand::createReg(Reg));
1698   return MCDisassembler::Success;
1699 }
1700 
1701 static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1702                                                 unsigned RegNo,
1703                                                 uint64_t Address,
1704                                                 const void *Decoder) {
1705   if (RegNo >= 4)
1706     return MCDisassembler::Fail;
1707 
1708   unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
1709   Inst.addOperand(MCOperand::createReg(Reg));
1710   return MCDisassembler::Success;
1711 }
1712 
1713 static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1714                                                unsigned RegNo,
1715                                                uint64_t Address,
1716                                                const void *Decoder) {
1717   if (RegNo >= 4)
1718     return MCDisassembler::Fail;
1719 
1720   unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
1721   Inst.addOperand(MCOperand::createReg(Reg));
1722   return MCDisassembler::Success;
1723 }
1724 
1725 static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1726                                                unsigned RegNo,
1727                                                uint64_t Address,
1728                                                const void *Decoder) {
1729   if (RegNo >= 4)
1730     return MCDisassembler::Fail;
1731 
1732   unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
1733   Inst.addOperand(MCOperand::createReg(Reg));
1734   return MCDisassembler::Success;
1735 }
1736 
1737 static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1738                                                unsigned RegNo,
1739                                                uint64_t Address,
1740                                                const void *Decoder) {
1741   if (RegNo > 31)
1742     return MCDisassembler::Fail;
1743 
1744   unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1745   Inst.addOperand(MCOperand::createReg(Reg));
1746   return MCDisassembler::Success;
1747 }
1748 
1749 static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1750                                                unsigned RegNo,
1751                                                uint64_t Address,
1752                                                const void *Decoder) {
1753   if (RegNo > 31)
1754     return MCDisassembler::Fail;
1755 
1756   unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1757   Inst.addOperand(MCOperand::createReg(Reg));
1758   return MCDisassembler::Success;
1759 }
1760 
1761 static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1762                                                unsigned RegNo,
1763                                                uint64_t Address,
1764                                                const void *Decoder) {
1765   if (RegNo > 31)
1766     return MCDisassembler::Fail;
1767 
1768   unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1769   Inst.addOperand(MCOperand::createReg(Reg));
1770   return MCDisassembler::Success;
1771 }
1772 
1773 static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1774                                                unsigned RegNo,
1775                                                uint64_t Address,
1776                                                const void *Decoder) {
1777   if (RegNo > 31)
1778     return MCDisassembler::Fail;
1779 
1780   unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1781   Inst.addOperand(MCOperand::createReg(Reg));
1782   return MCDisassembler::Success;
1783 }
1784 
1785 static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1786                                                unsigned RegNo,
1787                                                uint64_t Address,
1788                                                const void *Decoder) {
1789   if (RegNo > 7)
1790     return MCDisassembler::Fail;
1791 
1792   unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1793   Inst.addOperand(MCOperand::createReg(Reg));
1794   return MCDisassembler::Success;
1795 }
1796 
1797 static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst,
1798                                             unsigned RegNo,
1799                                             uint64_t Address,
1800                                             const void *Decoder) {
1801   if (RegNo > 31)
1802     return MCDisassembler::Fail;
1803 
1804   unsigned Reg = getReg(Decoder, Mips::COP0RegClassID, RegNo);
1805   Inst.addOperand(MCOperand::createReg(Reg));
1806   return MCDisassembler::Success;
1807 }
1808 
1809 static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1810                                             unsigned RegNo,
1811                                             uint64_t Address,
1812                                             const void *Decoder) {
1813   if (RegNo > 31)
1814     return MCDisassembler::Fail;
1815 
1816   unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1817   Inst.addOperand(MCOperand::createReg(Reg));
1818   return MCDisassembler::Success;
1819 }
1820 
1821 static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1822                                        unsigned Offset,
1823                                        uint64_t Address,
1824                                        const void *Decoder) {
1825   int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
1826   Inst.addOperand(MCOperand::createImm(BranchOffset));
1827   return MCDisassembler::Success;
1828 }
1829 
1830 static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1831                                      unsigned Insn,
1832                                      uint64_t Address,
1833                                      const void *Decoder) {
1834 
1835   unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
1836   Inst.addOperand(MCOperand::createImm(JumpOffset));
1837   return MCDisassembler::Success;
1838 }
1839 
1840 static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1841                                          unsigned Offset,
1842                                          uint64_t Address,
1843                                          const void *Decoder) {
1844   int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
1845 
1846   Inst.addOperand(MCOperand::createImm(BranchOffset));
1847   return MCDisassembler::Success;
1848 }
1849 
1850 static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1851                                          unsigned Offset,
1852                                          uint64_t Address,
1853                                          const void *Decoder) {
1854   int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
1855 
1856   Inst.addOperand(MCOperand::createImm(BranchOffset));
1857   return MCDisassembler::Success;
1858 }
1859 
1860 static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1861                                           unsigned Offset,
1862                                           uint64_t Address,
1863                                           const void *Decoder) {
1864   int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1865   Inst.addOperand(MCOperand::createImm(BranchOffset));
1866   return MCDisassembler::Success;
1867 }
1868 
1869 static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1870                                            unsigned Offset,
1871                                            uint64_t Address,
1872                                            const void *Decoder) {
1873   int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1874   Inst.addOperand(MCOperand::createImm(BranchOffset));
1875   return MCDisassembler::Success;
1876 }
1877 
1878 static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1879                                          unsigned Offset,
1880                                          uint64_t Address,
1881                                          const void *Decoder) {
1882   int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
1883   Inst.addOperand(MCOperand::createImm(BranchOffset));
1884   return MCDisassembler::Success;
1885 }
1886 
1887 static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst,
1888   unsigned Offset,
1889   uint64_t Address,
1890   const void *Decoder) {
1891   int32_t BranchOffset = SignExtend32<26>(Offset) << 1;
1892 
1893   Inst.addOperand(MCOperand::createImm(BranchOffset));
1894   return MCDisassembler::Success;
1895 }
1896 
1897 static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1898                                        unsigned Insn,
1899                                        uint64_t Address,
1900                                        const void *Decoder) {
1901   unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1902   Inst.addOperand(MCOperand::createImm(JumpOffset));
1903   return MCDisassembler::Success;
1904 }
1905 
1906 static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1907                                        unsigned Value,
1908                                        uint64_t Address,
1909                                        const void *Decoder) {
1910   if (Value == 0)
1911     Inst.addOperand(MCOperand::createImm(1));
1912   else if (Value == 0x7)
1913     Inst.addOperand(MCOperand::createImm(-1));
1914   else
1915     Inst.addOperand(MCOperand::createImm(Value << 2));
1916   return MCDisassembler::Success;
1917 }
1918 
1919 static DecodeStatus DecodeLi16Imm(MCInst &Inst,
1920                                   unsigned Value,
1921                                   uint64_t Address,
1922                                   const void *Decoder) {
1923   if (Value == 0x7F)
1924     Inst.addOperand(MCOperand::createImm(-1));
1925   else
1926     Inst.addOperand(MCOperand::createImm(Value));
1927   return MCDisassembler::Success;
1928 }
1929 
1930 static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst,
1931                                               unsigned Value,
1932                                               uint64_t Address,
1933                                               const void *Decoder) {
1934   Inst.addOperand(MCOperand::createImm(Value == 0x0 ? 8 : Value));
1935   return MCDisassembler::Success;
1936 }
1937 
1938 template <unsigned Bits, int Offset, int Scale>
1939 static DecodeStatus DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
1940                                                  uint64_t Address,
1941                                                  const void *Decoder) {
1942   Value &= ((1 << Bits) - 1);
1943   Value *= Scale;
1944   Inst.addOperand(MCOperand::createImm(Value + Offset));
1945   return MCDisassembler::Success;
1946 }
1947 
1948 template <unsigned Bits, int Offset, int ScaleBy>
1949 static DecodeStatus DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value,
1950                                                  uint64_t Address,
1951                                                  const void *Decoder) {
1952   int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
1953   Inst.addOperand(MCOperand::createImm(Imm + Offset));
1954   return MCDisassembler::Success;
1955 }
1956 
1957 static DecodeStatus DecodeInsSize(MCInst &Inst,
1958                                   unsigned Insn,
1959                                   uint64_t Address,
1960                                   const void *Decoder) {
1961   // First we need to grab the pos(lsb) from MCInst.
1962   int Pos = Inst.getOperand(2).getImm();
1963   int Size = (int) Insn - Pos + 1;
1964   Inst.addOperand(MCOperand::createImm(SignExtend32<16>(Size)));
1965   return MCDisassembler::Success;
1966 }
1967 
1968 static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1969                                      uint64_t Address, const void *Decoder) {
1970   Inst.addOperand(MCOperand::createImm(SignExtend32<19>(Insn) * 4));
1971   return MCDisassembler::Success;
1972 }
1973 
1974 static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1975                                      uint64_t Address, const void *Decoder) {
1976   Inst.addOperand(MCOperand::createImm(SignExtend32<18>(Insn) * 8));
1977   return MCDisassembler::Success;
1978 }
1979 
1980 static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1981                                   uint64_t Address, const void *Decoder) {
1982   int32_t DecodedValue;
1983   switch (Insn) {
1984   case 0: DecodedValue = 256; break;
1985   case 1: DecodedValue = 257; break;
1986   case 510: DecodedValue = -258; break;
1987   case 511: DecodedValue = -257; break;
1988   default: DecodedValue = SignExtend32<9>(Insn); break;
1989   }
1990   Inst.addOperand(MCOperand::createImm(DecodedValue * 4));
1991   return MCDisassembler::Success;
1992 }
1993 
1994 static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1995                                     uint64_t Address, const void *Decoder) {
1996   // Insn must be >= 0, since it is unsigned that condition is always true.
1997   assert(Insn < 16);
1998   int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1999                              255, 32768, 65535};
2000   Inst.addOperand(MCOperand::createImm(DecodedValues[Insn]));
2001   return MCDisassembler::Success;
2002 }
2003 
2004 static DecodeStatus DecodeRegListOperand(MCInst &Inst,
2005                                          unsigned Insn,
2006                                          uint64_t Address,
2007                                          const void *Decoder) {
2008   unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
2009                      Mips::S6, Mips::S7, Mips::FP};
2010   unsigned RegNum;
2011 
2012   unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
2013 
2014   // Empty register lists are not allowed.
2015   if (RegLst == 0)
2016     return MCDisassembler::Fail;
2017 
2018   RegNum = RegLst & 0xf;
2019 
2020   // RegLst values 10-15, and 26-31 are reserved.
2021   if (RegNum > 9)
2022     return MCDisassembler::Fail;
2023 
2024   for (unsigned i = 0; i < RegNum; i++)
2025     Inst.addOperand(MCOperand::createReg(Regs[i]));
2026 
2027   if (RegLst & 0x10)
2028     Inst.addOperand(MCOperand::createReg(Mips::RA));
2029 
2030   return MCDisassembler::Success;
2031 }
2032 
2033 static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
2034                                            uint64_t Address,
2035                                            const void *Decoder) {
2036   unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
2037   unsigned RegLst;
2038   switch(Inst.getOpcode()) {
2039   default:
2040     RegLst = fieldFromInstruction(Insn, 4, 2);
2041     break;
2042   case Mips::LWM16_MMR6:
2043   case Mips::SWM16_MMR6:
2044     RegLst = fieldFromInstruction(Insn, 8, 2);
2045     break;
2046   }
2047   unsigned RegNum = RegLst & 0x3;
2048 
2049   for (unsigned i = 0; i <= RegNum; i++)
2050     Inst.addOperand(MCOperand::createReg(Regs[i]));
2051 
2052   Inst.addOperand(MCOperand::createReg(Mips::RA));
2053 
2054   return MCDisassembler::Success;
2055 }
2056 
2057 static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned Insn,
2058                                        uint64_t Address, const void *Decoder) {
2059 
2060   unsigned RegPair = fieldFromInstruction(Insn, 7, 3);
2061 
2062   switch (RegPair) {
2063   default:
2064     return MCDisassembler::Fail;
2065   case 0:
2066     Inst.addOperand(MCOperand::createReg(Mips::A1));
2067     Inst.addOperand(MCOperand::createReg(Mips::A2));
2068     break;
2069   case 1:
2070     Inst.addOperand(MCOperand::createReg(Mips::A1));
2071     Inst.addOperand(MCOperand::createReg(Mips::A3));
2072     break;
2073   case 2:
2074     Inst.addOperand(MCOperand::createReg(Mips::A2));
2075     Inst.addOperand(MCOperand::createReg(Mips::A3));
2076     break;
2077   case 3:
2078     Inst.addOperand(MCOperand::createReg(Mips::A0));
2079     Inst.addOperand(MCOperand::createReg(Mips::S5));
2080     break;
2081   case 4:
2082     Inst.addOperand(MCOperand::createReg(Mips::A0));
2083     Inst.addOperand(MCOperand::createReg(Mips::S6));
2084     break;
2085   case 5:
2086     Inst.addOperand(MCOperand::createReg(Mips::A0));
2087     Inst.addOperand(MCOperand::createReg(Mips::A1));
2088     break;
2089   case 6:
2090     Inst.addOperand(MCOperand::createReg(Mips::A0));
2091     Inst.addOperand(MCOperand::createReg(Mips::A2));
2092     break;
2093   case 7:
2094     Inst.addOperand(MCOperand::createReg(Mips::A0));
2095     Inst.addOperand(MCOperand::createReg(Mips::A3));
2096     break;
2097   }
2098 
2099   return MCDisassembler::Success;
2100 }
2101 
2102 static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
2103                                      uint64_t Address, const void *Decoder) {
2104   Inst.addOperand(MCOperand::createImm(SignExtend32<25>(Insn << 2)));
2105   return MCDisassembler::Success;
2106 }
2107 
2108 template <typename InsnType>
2109 static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
2110   uint64_t Address,
2111   const void *Decoder) {
2112   // We have:
2113   //    0b000111 ttttt sssss iiiiiiiiiiiiiiii
2114   //      Invalid      if rt == 0
2115   //      BGTZALC_MMR6 if rs == 0 && rt != 0
2116   //      BLTZALC_MMR6 if rs != 0 && rs == rt
2117   //      BLTUC_MMR6   if rs != 0 && rs != rt
2118 
2119   InsnType Rt = fieldFromInstruction(insn, 21, 5);
2120   InsnType Rs = fieldFromInstruction(insn, 16, 5);
2121   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2;
2122   bool HasRs = false;
2123   bool HasRt = false;
2124 
2125   if (Rt == 0)
2126     return MCDisassembler::Fail;
2127   else if (Rs == 0) {
2128     MI.setOpcode(Mips::BGTZALC_MMR6);
2129     HasRt = true;
2130   }
2131   else if (Rs == Rt) {
2132     MI.setOpcode(Mips::BLTZALC_MMR6);
2133     HasRs = true;
2134   }
2135   else {
2136     MI.setOpcode(Mips::BLTUC_MMR6);
2137     HasRs = true;
2138     HasRt = true;
2139   }
2140 
2141   if (HasRs)
2142     MI.addOperand(
2143     MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2144 
2145   if (HasRt)
2146     MI.addOperand(
2147     MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2148 
2149   MI.addOperand(MCOperand::createImm(Imm));
2150 
2151   return MCDisassembler::Success;
2152 }
2153 
2154 template <typename InsnType>
2155 static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
2156   uint64_t Address,
2157   const void *Decoder) {
2158   // We have:
2159   //    0b000110 ttttt sssss iiiiiiiiiiiiiiii
2160   //      Invalid        if rs == 0
2161   //      BLEZALC_MMR6   if rs == 0  && rt != 0
2162   //      BGEZALC_MMR6   if rs == rt && rt != 0
2163   //      BGEUC_MMR6     if rs != rt && rs != 0  && rt != 0
2164 
2165   InsnType Rt = fieldFromInstruction(insn, 21, 5);
2166   InsnType Rs = fieldFromInstruction(insn, 16, 5);
2167   InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2;
2168   bool HasRs = false;
2169 
2170   if (Rt == 0)
2171     return MCDisassembler::Fail;
2172   else if (Rs == 0)
2173     MI.setOpcode(Mips::BLEZALC_MMR6);
2174   else if (Rs == Rt)
2175     MI.setOpcode(Mips::BGEZALC_MMR6);
2176   else {
2177     HasRs = true;
2178     MI.setOpcode(Mips::BGEUC_MMR6);
2179   }
2180 
2181   if (HasRs)
2182     MI.addOperand(
2183     MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rs)));
2184   MI.addOperand(
2185     MCOperand::createReg(getReg(Decoder, Mips::GPR32RegClassID, Rt)));
2186 
2187   MI.addOperand(MCOperand::createImm(Imm));
2188 
2189   return MCDisassembler::Success;
2190 }
2191