1//===- MipsDisassembler.cpp - Disassembler for Mips -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is part of the Mips Disassembler.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MCTargetDesc/MipsMCTargetDesc.h"
14#include "TargetInfo/MipsTargetInfo.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDecoder.h"
18#include "llvm/MC/MCDecoderOps.h"
19#include "llvm/MC/MCDisassembler/MCDisassembler.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/MC/TargetRegistry.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/raw_ostream.h"
29#include <cassert>
30#include <cstdint>
31
32using namespace llvm;
33using namespace llvm::MCD;
34
35#define DEBUG_TYPE "mips-disassembler"
36
37using DecodeStatus = MCDisassembler::DecodeStatus;
38
39namespace {
40
41class MipsDisassembler : public MCDisassembler {
42 bool IsMicroMips;
43 bool IsBigEndian;
44
45public:
46 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool IsBigEndian)
47 : MCDisassembler(STI, Ctx),
48 IsMicroMips(STI.hasFeature(Feature: Mips::FeatureMicroMips)),
49 IsBigEndian(IsBigEndian) {}
50
51 bool hasMips2() const { return STI.hasFeature(Feature: Mips::FeatureMips2); }
52 bool hasMips3() const { return STI.hasFeature(Feature: Mips::FeatureMips3); }
53 bool hasMips32() const { return STI.hasFeature(Feature: Mips::FeatureMips32); }
54
55 bool hasMips32r6() const {
56 return STI.hasFeature(Feature: Mips::FeatureMips32r6);
57 }
58
59 bool isFP64() const { return STI.hasFeature(Feature: Mips::FeatureFP64Bit); }
60
61 bool isGP64() const { return STI.hasFeature(Feature: Mips::FeatureGP64Bit); }
62
63 bool isPTR64() const { return STI.hasFeature(Feature: Mips::FeaturePTR64Bit); }
64
65 bool hasCnMips() const { return STI.hasFeature(Feature: Mips::FeatureCnMips); }
66
67 bool hasCnMipsP() const { return STI.hasFeature(Feature: Mips::FeatureCnMipsP); }
68
69 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
72 }
73
74 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
75 ArrayRef<uint8_t> Bytes, uint64_t Address,
76 raw_ostream &CStream) const override;
77};
78
79} // end anonymous namespace
80
81static MCDisassembler *createMipsDisassembler(const Target &T,
82 const MCSubtargetInfo &STI,
83 MCContext &Ctx) {
84 return new MipsDisassembler(STI, Ctx, true);
85}
86
87static MCDisassembler *createMipselDisassembler(const Target &T,
88 const MCSubtargetInfo &STI,
89 MCContext &Ctx) {
90 return new MipsDisassembler(STI, Ctx, false);
91}
92
93extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
94LLVMInitializeMipsDisassembler() {
95 // Register the disassembler.
96 TargetRegistry::RegisterMCDisassembler(T&: getTheMipsTarget(),
97 Fn: createMipsDisassembler);
98 TargetRegistry::RegisterMCDisassembler(T&: getTheMipselTarget(),
99 Fn: createMipselDisassembler);
100 TargetRegistry::RegisterMCDisassembler(T&: getTheMips64Target(),
101 Fn: createMipsDisassembler);
102 TargetRegistry::RegisterMCDisassembler(T&: getTheMips64elTarget(),
103 Fn: createMipselDisassembler);
104}
105
106static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo) {
107 const MCRegisterInfo *RegInfo = D->getContext().getRegisterInfo();
108 return RegInfo->getRegClass(i: RC).getRegister(i: RegNo);
109}
110static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst, unsigned RegNo,
111 uint64_t Address,
112 const MCDisassembler *Decoder) {
113 // Currently only hardware register 29 is supported.
114 if (RegNo != 29)
115 return MCDisassembler::Fail;
116 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::HWR29));
117 return MCDisassembler::Success;
118}
119
120static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst, unsigned RegNo,
121 uint64_t Address,
122 const MCDisassembler *Decoder) {
123 if (RegNo > 30 || RegNo % 2)
124 return MCDisassembler::Fail;
125
126 MCRegister Reg = getReg(D: Decoder, RC: Mips::AFGR64RegClassID, RegNo: RegNo / 2);
127 Inst.addOperand(Op: MCOperand::createReg(Reg));
128 return MCDisassembler::Success;
129}
130
131static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst, unsigned RegNo,
132 uint64_t Address,
133 const MCDisassembler *Decoder) {
134 if (RegNo >= 4)
135 return MCDisassembler::Fail;
136
137 MCRegister Reg = getReg(D: Decoder, RC: Mips::ACC64DSPRegClassID, RegNo);
138 Inst.addOperand(Op: MCOperand::createReg(Reg));
139 return MCDisassembler::Success;
140}
141
142static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst, unsigned RegNo,
143 uint64_t Address,
144 const MCDisassembler *Decoder) {
145 if (RegNo >= 4)
146 return MCDisassembler::Fail;
147
148 MCRegister Reg = getReg(D: Decoder, RC: Mips::HI32DSPRegClassID, RegNo);
149 Inst.addOperand(Op: MCOperand::createReg(Reg));
150 return MCDisassembler::Success;
151}
152
153static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst, unsigned RegNo,
154 uint64_t Address,
155 const MCDisassembler *Decoder) {
156 if (RegNo >= 4)
157 return MCDisassembler::Fail;
158
159 MCRegister Reg = getReg(D: Decoder, RC: Mips::LO32DSPRegClassID, RegNo);
160 Inst.addOperand(Op: MCOperand::createReg(Reg));
161 return MCDisassembler::Success;
162}
163
164static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst, unsigned RegNo,
165 uint64_t Address,
166 const MCDisassembler *Decoder) {
167 if (RegNo > 31)
168 return MCDisassembler::Fail;
169
170 MCRegister Reg = getReg(D: Decoder, RC: Mips::MSA128BRegClassID, RegNo);
171 Inst.addOperand(Op: MCOperand::createReg(Reg));
172 return MCDisassembler::Success;
173}
174
175static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst, unsigned RegNo,
176 uint64_t Address,
177 const MCDisassembler *Decoder) {
178 if (RegNo > 31)
179 return MCDisassembler::Fail;
180
181 MCRegister Reg = getReg(D: Decoder, RC: Mips::MSA128HRegClassID, RegNo);
182 Inst.addOperand(Op: MCOperand::createReg(Reg));
183 return MCDisassembler::Success;
184}
185
186static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst, unsigned RegNo,
187 uint64_t Address,
188 const MCDisassembler *Decoder) {
189 if (RegNo > 31)
190 return MCDisassembler::Fail;
191
192 MCRegister Reg = getReg(D: Decoder, RC: Mips::MSA128WRegClassID, RegNo);
193 Inst.addOperand(Op: MCOperand::createReg(Reg));
194 return MCDisassembler::Success;
195}
196
197static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst, unsigned RegNo,
198 uint64_t Address,
199 const MCDisassembler *Decoder) {
200 if (RegNo > 31)
201 return MCDisassembler::Fail;
202
203 MCRegister Reg = getReg(D: Decoder, RC: Mips::MSA128DRegClassID, RegNo);
204 Inst.addOperand(Op: MCOperand::createReg(Reg));
205 return MCDisassembler::Success;
206}
207
208static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst, unsigned RegNo,
209 uint64_t Address,
210 const MCDisassembler *Decoder) {
211 if (RegNo > 7)
212 return MCDisassembler::Fail;
213
214 MCRegister Reg = getReg(D: Decoder, RC: Mips::MSACtrlRegClassID, RegNo);
215 Inst.addOperand(Op: MCOperand::createReg(Reg));
216 return MCDisassembler::Success;
217}
218
219static DecodeStatus DecodeCOP0RegisterClass(MCInst &Inst, unsigned RegNo,
220 uint64_t Address,
221 const MCDisassembler *Decoder) {
222 if (MIPS_MC::getCOP0RegNum(Encoding: RegNo) >= 32 || MIPS_MC::getCOP0Sel(Encoding: RegNo) >= 8)
223 return MCDisassembler::Fail;
224
225 MCRegister Reg =
226 MIPS_MC::getCOP0Register(MRI: *Decoder->getContext().getRegisterInfo(), Encoding: RegNo);
227 Inst.addOperand(Op: MCOperand::createReg(Reg));
228 return MCDisassembler::Success;
229}
230
231static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst, unsigned RegNo,
232 uint64_t Address,
233 const MCDisassembler *Decoder) {
234 if (RegNo > 31)
235 return MCDisassembler::Fail;
236
237 MCRegister Reg = getReg(D: Decoder, RC: Mips::COP2RegClassID, RegNo);
238 Inst.addOperand(Op: MCOperand::createReg(Reg));
239 return MCDisassembler::Success;
240}
241
242static DecodeStatus DecodeCOP3RegisterClass(MCInst &Inst, unsigned RegNo,
243 uint64_t Address,
244 const MCDisassembler *Decoder) {
245 if (RegNo > 31)
246 return MCDisassembler::Fail;
247 Inst.addOperand(
248 Op: MCOperand::createReg(Reg: getReg(D: Decoder, RC: Mips::COP3RegClassID, RegNo)));
249 return MCDisassembler::Success;
250}
251
252static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned RegListEncoding,
253 uint64_t Address,
254 const MCDisassembler *Decoder) {
255 unsigned Regs[] = {Mips::R16, Mips::R17, Mips::R18, Mips::R19, Mips::R20,
256 Mips::R21, Mips::R22, Mips::R23, Mips::FP};
257 unsigned RegNum;
258
259 unsigned RegLst = RegListEncoding;
260
261 // Empty register lists are not allowed.
262 if (RegLst == 0)
263 return MCDisassembler::Fail;
264
265 RegNum = RegLst & 0xf;
266
267 // RegLst values 10-15, and 26-31 are reserved.
268 if (RegNum > 9)
269 return MCDisassembler::Fail;
270
271 for (unsigned i = 0; i < RegNum; i++)
272 Inst.addOperand(Op: MCOperand::createReg(Reg: Regs[i]));
273
274 if (RegLst & 0x10)
275 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::RA));
276
277 return MCDisassembler::Success;
278}
279
280static DecodeStatus DecodeRegListOperand16(MCInst &Inst,
281 unsigned RegListEncoding,
282 uint64_t Address,
283 const MCDisassembler *Decoder) {
284 unsigned Regs[] = {Mips::R16, Mips::R17, Mips::R18, Mips::R19};
285 unsigned RegLst = RegListEncoding;
286 unsigned RegNum = RegLst & 0x3;
287
288 for (unsigned i = 0; i <= RegNum; i++)
289 Inst.addOperand(Op: MCOperand::createReg(Reg: Regs[i]));
290
291 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::RA));
292
293 return MCDisassembler::Success;
294}
295
296template <typename InsnType>
297static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
298 const MCDisassembler *Decoder) {
299 using DecodeFN =
300 DecodeStatus (*)(MCInst &, unsigned, uint64_t, const MCDisassembler *);
301
302 // The size of the n field depends on the element size
303 // The register class also depends on this.
304 InsnType tmp = fieldFromInstruction(insn, 17, 5);
305 unsigned NSize = 0;
306 DecodeFN RegDecoder = nullptr;
307 if ((tmp & 0x18) == 0x00) { // INSVE_B
308 NSize = 4;
309 RegDecoder = DecodeMSA128BRegisterClass;
310 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
311 NSize = 3;
312 RegDecoder = DecodeMSA128HRegisterClass;
313 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
314 NSize = 2;
315 RegDecoder = DecodeMSA128WRegisterClass;
316 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
317 NSize = 1;
318 RegDecoder = DecodeMSA128DRegisterClass;
319 } else
320 llvm_unreachable("Invalid encoding");
321
322 assert(NSize != 0 && RegDecoder != nullptr);
323
324 // $wd
325 tmp = fieldFromInstruction(insn, 6, 5);
326 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
327 return MCDisassembler::Fail;
328 // $wd_in
329 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
330 return MCDisassembler::Fail;
331 // $n
332 tmp = fieldFromInstruction(insn, 16, NSize);
333 MI.addOperand(Op: MCOperand::createImm(Val: tmp));
334 // $ws
335 tmp = fieldFromInstruction(insn, 11, 5);
336 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
337 return MCDisassembler::Fail;
338 // $n2
339 MI.addOperand(Op: MCOperand::createImm(Val: 0));
340
341 return MCDisassembler::Success;
342}
343
344template <typename InsnType>
345static DecodeStatus DecodeDAHIDATI(MCInst &MI, InsnType insn, uint64_t Address,
346 const MCDisassembler *Decoder) {
347 InsnType Rs = fieldFromInstruction(insn, 21, 5);
348 InsnType Imm = fieldFromInstruction(insn, 0, 16);
349 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID,
350 Rs)));
351 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID,
352 Rs)));
353 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
354
355 return MCDisassembler::Success;
356}
357
358template <typename InsnType>
359static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
360 uint64_t Address,
361 const MCDisassembler *Decoder) {
362 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
363 // (otherwise we would have matched the ADDI instruction from the earlier
364 // ISA's instead).
365 //
366 // We have:
367 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
368 // BOVC if rs >= rt
369 // BEQZALC if rs == 0 && rt != 0
370 // BEQC if rs < rt && rs != 0
371
372 InsnType Rs = fieldFromInstruction(insn, 21, 5);
373 InsnType Rt = fieldFromInstruction(insn, 16, 5);
374 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
375 bool HasRs = false;
376
377 if (Rs >= Rt) {
378 MI.setOpcode(Mips::BOVC);
379 HasRs = true;
380 } else if (Rs != 0 && Rs < Rt) {
381 MI.setOpcode(Mips::BEQC);
382 HasRs = true;
383 } else
384 MI.setOpcode(Mips::BEQZALC);
385
386 if (HasRs)
387 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
388 Rs)));
389
390 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
391 Rt)));
392 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
393
394 return MCDisassembler::Success;
395}
396
397template <typename InsnType>
398static DecodeStatus DecodePOP35GroupBranchMMR6(MCInst &MI, InsnType insn,
399 uint64_t Address,
400 const MCDisassembler *Decoder) {
401 InsnType Rt = fieldFromInstruction(insn, 21, 5);
402 InsnType Rs = fieldFromInstruction(insn, 16, 5);
403 int64_t Imm = 0;
404
405 if (Rs >= Rt) {
406 MI.setOpcode(Mips::BOVC_MMR6);
407 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
408 Rt)));
409 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
410 Rs)));
411 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
412 } else if (Rs != 0 && Rs < Rt) {
413 MI.setOpcode(Mips::BEQC_MMR6);
414 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
415 Rs)));
416 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
417 Rt)));
418 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
419 } else {
420 MI.setOpcode(Mips::BEQZALC_MMR6);
421 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
422 Rt)));
423 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
424 }
425
426 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
427
428 return MCDisassembler::Success;
429}
430
431template <typename InsnType>
432static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
433 uint64_t Address,
434 const MCDisassembler *Decoder) {
435 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
436 // (otherwise we would have matched the ADDI instruction from the earlier
437 // ISA's instead).
438 //
439 // We have:
440 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
441 // BNVC if rs >= rt
442 // BNEZALC if rs == 0 && rt != 0
443 // BNEC if rs < rt && rs != 0
444
445 InsnType Rs = fieldFromInstruction(insn, 21, 5);
446 InsnType Rt = fieldFromInstruction(insn, 16, 5);
447 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
448 bool HasRs = false;
449
450 if (Rs >= Rt) {
451 MI.setOpcode(Mips::BNVC);
452 HasRs = true;
453 } else if (Rs != 0 && Rs < Rt) {
454 MI.setOpcode(Mips::BNEC);
455 HasRs = true;
456 } else
457 MI.setOpcode(Mips::BNEZALC);
458
459 if (HasRs)
460 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
461 Rs)));
462
463 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
464 Rt)));
465 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
466
467 return MCDisassembler::Success;
468}
469
470template <typename InsnType>
471static DecodeStatus DecodePOP37GroupBranchMMR6(MCInst &MI, InsnType insn,
472 uint64_t Address,
473 const MCDisassembler *Decoder) {
474 InsnType Rt = fieldFromInstruction(insn, 21, 5);
475 InsnType Rs = fieldFromInstruction(insn, 16, 5);
476 int64_t Imm = 0;
477
478 if (Rs >= Rt) {
479 MI.setOpcode(Mips::BNVC_MMR6);
480 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
481 Rt)));
482 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
483 Rs)));
484 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
485 } else if (Rs != 0 && Rs < Rt) {
486 MI.setOpcode(Mips::BNEC_MMR6);
487 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
488 Rs)));
489 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
490 Rt)));
491 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
492 } else {
493 MI.setOpcode(Mips::BNEZALC_MMR6);
494 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
495 Rt)));
496 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
497 }
498
499 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
500
501 return MCDisassembler::Success;
502}
503
504template <typename InsnType>
505static DecodeStatus DecodePOP65GroupBranchMMR6(MCInst &MI, InsnType insn,
506 uint64_t Address,
507 const MCDisassembler *Decoder) {
508 // We have:
509 // 0b110101 ttttt sssss iiiiiiiiiiiiiiii
510 // Invalid if rt == 0
511 // BGTZC_MMR6 if rs == 0 && rt != 0
512 // BLTZC_MMR6 if rs == rt && rt != 0
513 // BLTC_MMR6 if rs != rt && rs != 0 && rt != 0
514
515 InsnType Rt = fieldFromInstruction(insn, 21, 5);
516 InsnType Rs = fieldFromInstruction(insn, 16, 5);
517 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
518 bool HasRs = false;
519
520 if (Rt == 0)
521 return MCDisassembler::Fail;
522 else if (Rs == 0)
523 MI.setOpcode(Mips::BGTZC_MMR6);
524 else if (Rs == Rt)
525 MI.setOpcode(Mips::BLTZC_MMR6);
526 else {
527 MI.setOpcode(Mips::BLTC_MMR6);
528 HasRs = true;
529 }
530
531 if (HasRs)
532 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
533 Rs)));
534
535 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
536 Rt)));
537
538 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
539
540 return MCDisassembler::Success;
541}
542
543template <typename InsnType>
544static DecodeStatus DecodePOP75GroupBranchMMR6(MCInst &MI, InsnType insn,
545 uint64_t Address,
546 const MCDisassembler *Decoder) {
547 // We have:
548 // 0b111101 ttttt sssss iiiiiiiiiiiiiiii
549 // Invalid if rt == 0
550 // BLEZC_MMR6 if rs == 0 && rt != 0
551 // BGEZC_MMR6 if rs == rt && rt != 0
552 // BGEC_MMR6 if rs != rt && rs != 0 && rt != 0
553
554 InsnType Rt = fieldFromInstruction(insn, 21, 5);
555 InsnType Rs = fieldFromInstruction(insn, 16, 5);
556 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
557 bool HasRs = false;
558
559 if (Rt == 0)
560 return MCDisassembler::Fail;
561 else if (Rs == 0)
562 MI.setOpcode(Mips::BLEZC_MMR6);
563 else if (Rs == Rt)
564 MI.setOpcode(Mips::BGEZC_MMR6);
565 else {
566 HasRs = true;
567 MI.setOpcode(Mips::BGEC_MMR6);
568 }
569
570 if (HasRs)
571 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
572 Rs)));
573
574 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
575 Rt)));
576
577 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
578
579 return MCDisassembler::Success;
580}
581
582template <typename InsnType>
583static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
584 uint64_t Address,
585 const MCDisassembler *Decoder) {
586 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
587 // (otherwise we would have matched the BLEZL instruction from the earlier
588 // ISA's instead).
589 //
590 // We have:
591 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
592 // Invalid if rs == 0
593 // BLEZC if rs == 0 && rt != 0
594 // BGEZC if rs == rt && rt != 0
595 // BGEC if rs != rt && rs != 0 && rt != 0
596
597 InsnType Rs = fieldFromInstruction(insn, 21, 5);
598 InsnType Rt = fieldFromInstruction(insn, 16, 5);
599 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
600 bool HasRs = false;
601
602 if (Rt == 0)
603 return MCDisassembler::Fail;
604 else if (Rs == 0)
605 MI.setOpcode(Mips::BLEZC);
606 else if (Rs == Rt)
607 MI.setOpcode(Mips::BGEZC);
608 else {
609 HasRs = true;
610 MI.setOpcode(Mips::BGEC);
611 }
612
613 if (HasRs)
614 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
615 Rs)));
616
617 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
618 Rt)));
619
620 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
621
622 return MCDisassembler::Success;
623}
624
625template <typename InsnType>
626static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
627 uint64_t Address,
628 const MCDisassembler *Decoder) {
629 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
630 // (otherwise we would have matched the BGTZL instruction from the earlier
631 // ISA's instead).
632 //
633 // We have:
634 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
635 // Invalid if rs == 0
636 // BGTZC if rs == 0 && rt != 0
637 // BLTZC if rs == rt && rt != 0
638 // BLTC if rs != rt && rs != 0 && rt != 0
639
640 bool HasRs = false;
641
642 InsnType Rs = fieldFromInstruction(insn, 21, 5);
643 InsnType Rt = fieldFromInstruction(insn, 16, 5);
644 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
645
646 if (Rt == 0)
647 return MCDisassembler::Fail;
648 else if (Rs == 0)
649 MI.setOpcode(Mips::BGTZC);
650 else if (Rs == Rt)
651 MI.setOpcode(Mips::BLTZC);
652 else {
653 MI.setOpcode(Mips::BLTC);
654 HasRs = true;
655 }
656
657 if (HasRs)
658 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
659 Rs)));
660
661 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
662 Rt)));
663
664 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
665
666 return MCDisassembler::Success;
667}
668
669template <typename InsnType>
670static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
671 uint64_t Address,
672 const MCDisassembler *Decoder) {
673 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
674 // (otherwise we would have matched the BGTZ instruction from the earlier
675 // ISA's instead).
676 //
677 // We have:
678 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
679 // BGTZ if rt == 0
680 // BGTZALC if rs == 0 && rt != 0
681 // BLTZALC if rs != 0 && rs == rt
682 // BLTUC if rs != 0 && rs != rt
683
684 InsnType Rs = fieldFromInstruction(insn, 21, 5);
685 InsnType Rt = fieldFromInstruction(insn, 16, 5);
686 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
687 bool HasRs = false;
688 bool HasRt = false;
689
690 if (Rt == 0) {
691 MI.setOpcode(Mips::BGTZ);
692 HasRs = true;
693 } else if (Rs == 0) {
694 MI.setOpcode(Mips::BGTZALC);
695 HasRt = true;
696 } else if (Rs == Rt) {
697 MI.setOpcode(Mips::BLTZALC);
698 HasRs = true;
699 } else {
700 MI.setOpcode(Mips::BLTUC);
701 HasRs = true;
702 HasRt = true;
703 }
704
705 if (HasRs)
706 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
707 Rs)));
708
709 if (HasRt)
710 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
711 Rt)));
712
713 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
714
715 return MCDisassembler::Success;
716}
717
718template <typename InsnType>
719static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
720 uint64_t Address,
721 const MCDisassembler *Decoder) {
722 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
723 // (otherwise we would have matched the BLEZL instruction from the earlier
724 // ISA's instead).
725 //
726 // We have:
727 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
728 // Invalid if rs == 0
729 // BLEZALC if rs == 0 && rt != 0
730 // BGEZALC if rs == rt && rt != 0
731 // BGEUC if rs != rt && rs != 0 && rt != 0
732
733 InsnType Rs = fieldFromInstruction(insn, 21, 5);
734 InsnType Rt = fieldFromInstruction(insn, 16, 5);
735 int64_t Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
736 bool HasRs = false;
737
738 if (Rt == 0)
739 return MCDisassembler::Fail;
740 else if (Rs == 0)
741 MI.setOpcode(Mips::BLEZALC);
742 else if (Rs == Rt)
743 MI.setOpcode(Mips::BGEZALC);
744 else {
745 HasRs = true;
746 MI.setOpcode(Mips::BGEUC);
747 }
748
749 if (HasRs)
750 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
751 Rs)));
752 MI.addOperand(Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID,
753 Rt)));
754
755 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
756
757 return MCDisassembler::Success;
758}
759
760// Override the generated disassembler to produce DEXT all the time. This is
761// for feature / behaviour parity with binutils.
762template <typename InsnType>
763static DecodeStatus DecodeDEXT(MCInst &MI, InsnType Insn, uint64_t Address,
764 const MCDisassembler *Decoder) {
765 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
766 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
767 unsigned Size = 0;
768 unsigned Pos = 0;
769
770 switch (MI.getOpcode()) {
771 case Mips::DEXT:
772 Pos = Lsb;
773 Size = Msbd + 1;
774 break;
775 case Mips::DEXTM:
776 Pos = Lsb;
777 Size = Msbd + 1 + 32;
778 break;
779 case Mips::DEXTU:
780 Pos = Lsb + 32;
781 Size = Msbd + 1;
782 break;
783 default:
784 llvm_unreachable("Unknown DEXT instruction!");
785 }
786
787 MI.setOpcode(Mips::DEXT);
788
789 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
790 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
791
792 MI.addOperand(
793 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID, Rt)));
794 MI.addOperand(
795 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID, Rs)));
796 MI.addOperand(Op: MCOperand::createImm(Val: Pos));
797 MI.addOperand(Op: MCOperand::createImm(Val: Size));
798
799 return MCDisassembler::Success;
800}
801
802// Override the generated disassembler to produce DINS all the time. This is
803// for feature / behaviour parity with binutils.
804template <typename InsnType>
805static DecodeStatus DecodeDINS(MCInst &MI, InsnType Insn, uint64_t Address,
806 const MCDisassembler *Decoder) {
807 unsigned Msbd = fieldFromInstruction(Insn, 11, 5);
808 unsigned Lsb = fieldFromInstruction(Insn, 6, 5);
809 unsigned Size = 0;
810 unsigned Pos = 0;
811
812 switch (MI.getOpcode()) {
813 case Mips::DINS:
814 Pos = Lsb;
815 Size = Msbd + 1 - Pos;
816 break;
817 case Mips::DINSM:
818 Pos = Lsb;
819 Size = Msbd + 33 - Pos;
820 break;
821 case Mips::DINSU:
822 Pos = Lsb + 32;
823 // mbsd = pos + size - 33
824 // mbsd - pos + 33 = size
825 Size = Msbd + 33 - Pos;
826 break;
827 default:
828 llvm_unreachable("Unknown DINS instruction!");
829 }
830
831 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
832 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
833
834 MI.setOpcode(Mips::DINS);
835 MI.addOperand(
836 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID, Rt)));
837 MI.addOperand(
838 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR64RegClassID, Rs)));
839 MI.addOperand(Op: MCOperand::createImm(Val: Pos));
840 MI.addOperand(Op: MCOperand::createImm(Val: Size));
841
842 return MCDisassembler::Success;
843}
844
845// Auto-generated decoder wouldn't add the third operand for CRC32*.
846template <typename InsnType>
847static DecodeStatus DecodeCRC(MCInst &MI, InsnType Insn, uint64_t Address,
848 const MCDisassembler *Decoder) {
849 InsnType Rs = fieldFromInstruction(Insn, 21, 5);
850 InsnType Rt = fieldFromInstruction(Insn, 16, 5);
851 MI.addOperand(
852 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rt)));
853 MI.addOperand(
854 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rs)));
855 MI.addOperand(
856 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rt)));
857 return MCDisassembler::Success;
858}
859
860static DecodeStatus
861DecodeCPU16RegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
862 const MCDisassembler *Decoder) {
863 return MCDisassembler::Fail;
864}
865
866static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst, unsigned RegNo,
867 uint64_t Address,
868 const MCDisassembler *Decoder) {
869 if (RegNo > 31)
870 return MCDisassembler::Fail;
871
872 MCRegister Reg = getReg(D: Decoder, RC: Mips::GPR64RegClassID, RegNo);
873 Inst.addOperand(Op: MCOperand::createReg(Reg));
874 return MCDisassembler::Success;
875}
876
877static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst, unsigned RegNo,
878 uint64_t Address,
879 const MCDisassembler *Decoder) {
880 if (RegNo > 7)
881 return MCDisassembler::Fail;
882 MCRegister Reg = getReg(D: Decoder, RC: Mips::GPRMM16RegClassID, RegNo);
883 Inst.addOperand(Op: MCOperand::createReg(Reg));
884 return MCDisassembler::Success;
885}
886
887static DecodeStatus
888DecodeGPRMM16ZeroRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
889 const MCDisassembler *Decoder) {
890 if (RegNo > 7)
891 return MCDisassembler::Fail;
892 MCRegister Reg = getReg(D: Decoder, RC: Mips::GPRMM16ZeroRegClassID, RegNo);
893 Inst.addOperand(Op: MCOperand::createReg(Reg));
894 return MCDisassembler::Success;
895}
896
897static DecodeStatus
898DecodeGPRMM16MovePRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
899 const MCDisassembler *Decoder) {
900 if (RegNo > 7)
901 return MCDisassembler::Fail;
902 MCRegister Reg = getReg(D: Decoder, RC: Mips::GPRMM16MovePRegClassID, RegNo);
903 Inst.addOperand(Op: MCOperand::createReg(Reg));
904 return MCDisassembler::Success;
905}
906
907// Tablegen emits references to these unimplemented functions due to usage of
908// RegClassByHwMode - it does not detect that the RegClassByHwMode decoders are
909// unused, which in turn use these register class decoders.
910static DecodeStatus DecodeGP32RegisterClass(MCInst &Inst, unsigned RegNo,
911 uint64_t Address,
912 const MCDisassembler *Decoder) {
913 llvm_unreachable("this is unused");
914}
915
916static DecodeStatus DecodeGP64RegisterClass(MCInst &Inst, unsigned RegNo,
917 uint64_t Address,
918 const MCDisassembler *Decoder) {
919 llvm_unreachable("this is unused");
920}
921
922static DecodeStatus DecodeSP32RegisterClass(MCInst &Inst, unsigned RegNo,
923 uint64_t Address,
924 const MCDisassembler *Decoder) {
925 llvm_unreachable("this is unused");
926}
927
928static DecodeStatus DecodeSP64RegisterClass(MCInst &Inst, unsigned RegNo,
929 uint64_t Address,
930 const MCDisassembler *Decoder) {
931 llvm_unreachable("this is unused");
932}
933
934static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo,
935 uint64_t Address,
936 const MCDisassembler *Decoder) {
937 if (RegNo > 31)
938 return MCDisassembler::Fail;
939 MCRegister Reg = getReg(D: Decoder, RC: Mips::GPR32RegClassID, RegNo);
940 Inst.addOperand(Op: MCOperand::createReg(Reg));
941 return MCDisassembler::Success;
942}
943
944static DecodeStatus DecodeGPRNMRARegisterClass(MCInst &Inst, unsigned RegNo,
945 uint64_t Address,
946 const MCDisassembler *Decoder) {
947 if (RegNo != 1)
948 return MCDisassembler::Fail;
949 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::RA));
950 return MCDisassembler::Success;
951}
952
953static DecodeStatus DecodeGPRNM3RegisterClass(MCInst &Inst, unsigned RegNo,
954 uint64_t Address,
955 const MCDisassembler *Decoder) {
956 if (RegNo > 7)
957 return MCDisassembler::Fail;
958 RegNo |= ((RegNo & 0x4) ^ 0x4) << 2;
959 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
960}
961
962static DecodeStatus DecodeGPRNM3ZRegisterClass(MCInst &Inst, unsigned RegNo,
963 uint64_t Address,
964 const MCDisassembler *Decoder) {
965 if (RegNo > 7)
966 return MCDisassembler::Fail;
967 if (RegNo != 0)
968 RegNo |= ((RegNo & 0x4) ^ 0x4) << 2;
969 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
970}
971
972static DecodeStatus DecodeGPRNM4RegisterClass(MCInst &Inst, unsigned RegNo,
973 uint64_t Address,
974 const MCDisassembler *Decoder) {
975 RegNo &= ~0x8;
976 RegNo += (RegNo < 4 ? 8 : 0);
977 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
978}
979
980static DecodeStatus DecodeGPRNM4ZRegisterClass(MCInst &Inst, unsigned RegNo,
981 uint64_t Address,
982 const MCDisassembler *Decoder) {
983 RegNo &= ~0x8;
984 if (RegNo == 3)
985 RegNo = 0;
986 else
987 RegNo += (RegNo < 3 ? 8 : 0);
988 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
989}
990
991static DecodeStatus
992DecodeGPR32NONZERORegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
993 const MCDisassembler *Decoder) {
994 if (RegNo == 0)
995 return MCDisassembler::Fail;
996 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
997}
998
999static DecodeStatus DecodeGPRNM2R1RegisterClass(MCInst &Inst, unsigned RegNo,
1000 uint64_t Address,
1001 const MCDisassembler *Decoder) {
1002 if (RegNo > 3)
1003 return MCDisassembler::Fail;
1004 RegNo += 4;
1005 MCRegister Reg1 = getReg(D: Decoder, RC: Mips::GPR32RegClassID, RegNo);
1006 MCRegister Reg2 = getReg(D: Decoder, RC: Mips::GPR32RegClassID, RegNo: RegNo + 1);
1007 Inst.addOperand(Op: MCOperand::createReg(Reg: Reg1));
1008 Inst.addOperand(Op: MCOperand::createReg(Reg: Reg2));
1009 return MCDisassembler::Success;
1010}
1011
1012static DecodeStatus DecodeGPRNM1R1RegisterClass(MCInst &Inst, unsigned RegNo,
1013 uint64_t Address,
1014 const MCDisassembler *Decoder) {
1015 if (RegNo != 0 && RegNo != 1)
1016 return MCDisassembler::Fail;
1017 RegNo += 4;
1018 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1019}
1020
1021static DecodeStatus DecodePtrRegisterClass(MCInst &Inst, unsigned RegNo,
1022 uint64_t Address,
1023 const MCDisassembler *Decoder) {
1024 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64())
1025 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1026
1027 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1028}
1029
1030template <unsigned RegNo>
1031static DecodeStatus DecodeFixedPtrRegister(MCInst &Inst,
1032 const MCDisassembler *Decoder) {
1033 return DecodePtrRegisterClass(Inst, RegNo, Address: 0, Decoder);
1034}
1035
1036static DecodeStatus DecodeLBU16Offset(MCInst &Inst, unsigned Value,
1037 uint64_t Address,
1038 const MCDisassembler *Decoder) {
1039 Inst.addOperand(Op: MCOperand::createImm(Val: Value == 15 ? -1 : int64_t(Value)));
1040 return MCDisassembler::Success;
1041}
1042
1043static DecodeStatus DecodeGPR32Pair(MCInst &Inst, unsigned RegNo,
1044 uint64_t Address,
1045 const MCDisassembler *Decoder) {
1046 if (RegNo >= 31)
1047 return MCDisassembler::Fail;
1048 DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1049 return DecodeGPR32RegisterClass(Inst, RegNo: RegNo + 1, Address, Decoder);
1050}
1051
1052static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst, unsigned RegNo,
1053 uint64_t Address,
1054 const MCDisassembler *Decoder) {
1055 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1056}
1057
1058static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst, unsigned RegNo,
1059 uint64_t Address,
1060 const MCDisassembler *Decoder) {
1061 if (RegNo > 31)
1062 return MCDisassembler::Fail;
1063
1064 MCRegister Reg = getReg(D: Decoder, RC: Mips::FGR64RegClassID, RegNo);
1065 Inst.addOperand(Op: MCOperand::createReg(Reg));
1066 return MCDisassembler::Success;
1067}
1068
1069static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst, unsigned RegNo,
1070 uint64_t Address,
1071 const MCDisassembler *Decoder) {
1072 if (RegNo > 31)
1073 return MCDisassembler::Fail;
1074
1075 MCRegister Reg = getReg(D: Decoder, RC: Mips::FGR32RegClassID, RegNo);
1076 Inst.addOperand(Op: MCOperand::createReg(Reg));
1077 return MCDisassembler::Success;
1078}
1079
1080static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst, unsigned RegNo,
1081 uint64_t Address,
1082 const MCDisassembler *Decoder) {
1083 if (RegNo > 31)
1084 return MCDisassembler::Fail;
1085 MCRegister Reg = getReg(D: Decoder, RC: Mips::CCRRegClassID, RegNo);
1086 Inst.addOperand(Op: MCOperand::createReg(Reg));
1087 return MCDisassembler::Success;
1088}
1089
1090static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst, unsigned RegNo,
1091 uint64_t Address,
1092 const MCDisassembler *Decoder) {
1093 if (RegNo > 7)
1094 return MCDisassembler::Fail;
1095 MCRegister Reg = getReg(D: Decoder, RC: Mips::FCCRegClassID, RegNo);
1096 Inst.addOperand(Op: MCOperand::createReg(Reg));
1097 return MCDisassembler::Success;
1098}
1099
1100static DecodeStatus DecodeBranchTarget(MCInst &Inst, unsigned Offset,
1101 uint64_t Address,
1102 const MCDisassembler *Decoder) {
1103 int32_t BranchOffset = (SignExtend32<16>(X: Offset) * 4) + 4;
1104 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1105 return MCDisassembler::Success;
1106}
1107
1108static DecodeStatus DecodeBranchTarget1SImm16(MCInst &Inst, unsigned Offset,
1109 uint64_t Address,
1110 const MCDisassembler *Decoder) {
1111 int32_t BranchOffset = (SignExtend32<16>(X: Offset) * 2);
1112 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1113 return MCDisassembler::Success;
1114}
1115
1116static DecodeStatus DecodeJumpTarget(MCInst &Inst, unsigned Insn,
1117 uint64_t Address,
1118 const MCDisassembler *Decoder) {
1119 unsigned JumpOffset = fieldFromInstruction(Insn, StartBit: 0, NumBits: 26) << 2;
1120 Inst.addOperand(Op: MCOperand::createImm(Val: JumpOffset));
1121 return MCDisassembler::Success;
1122}
1123
1124static DecodeStatus DecodeBranchTarget21(MCInst &Inst, unsigned Offset,
1125 uint64_t Address,
1126 const MCDisassembler *Decoder) {
1127 int32_t BranchOffset = SignExtend32<21>(X: Offset) * 4 + 4;
1128
1129 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1130 return MCDisassembler::Success;
1131}
1132
1133static DecodeStatus DecodeBranchTarget21MM(MCInst &Inst, unsigned Offset,
1134 uint64_t Address,
1135 const MCDisassembler *Decoder) {
1136 int32_t BranchOffset = SignExtend32<21>(X: Offset) * 4 + 4;
1137
1138 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1139 return MCDisassembler::Success;
1140}
1141
1142static DecodeStatus DecodeBranchTarget26(MCInst &Inst, unsigned Offset,
1143 uint64_t Address,
1144 const MCDisassembler *Decoder) {
1145 int32_t BranchOffset = SignExtend32<26>(X: Offset) * 4 + 4;
1146
1147 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1148 return MCDisassembler::Success;
1149}
1150
1151static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst, unsigned Offset,
1152 uint64_t Address,
1153 const MCDisassembler *Decoder) {
1154 int32_t BranchOffset = SignExtend32<8>(X: Offset << 1);
1155 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1156 return MCDisassembler::Success;
1157}
1158
1159static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst, unsigned Offset,
1160 uint64_t Address,
1161 const MCDisassembler *Decoder) {
1162 int32_t BranchOffset = SignExtend32<11>(X: Offset << 1);
1163 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1164 return MCDisassembler::Success;
1165}
1166
1167static DecodeStatus DecodeBranchTargetMM(MCInst &Inst, unsigned Offset,
1168 uint64_t Address,
1169 const MCDisassembler *Decoder) {
1170 int32_t BranchOffset = SignExtend32<16>(X: Offset) * 2 + 4;
1171 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1172 return MCDisassembler::Success;
1173}
1174
1175static DecodeStatus DecodeBranchTarget26MM(MCInst &Inst, unsigned Offset,
1176 uint64_t Address,
1177 const MCDisassembler *Decoder) {
1178 int32_t BranchOffset = SignExtend32<27>(X: Offset << 1);
1179
1180 Inst.addOperand(Op: MCOperand::createImm(Val: BranchOffset));
1181 return MCDisassembler::Success;
1182}
1183
1184static DecodeStatus DecodeJumpTargetMM(MCInst &Inst, unsigned Insn,
1185 uint64_t Address,
1186 const MCDisassembler *Decoder) {
1187 unsigned JumpOffset = fieldFromInstruction(Insn, StartBit: 0, NumBits: 26) << 1;
1188 Inst.addOperand(Op: MCOperand::createImm(Val: JumpOffset));
1189 return MCDisassembler::Success;
1190}
1191
1192static DecodeStatus DecodeJumpTargetXMM(MCInst &Inst, unsigned Insn,
1193 uint64_t Address,
1194 const MCDisassembler *Decoder) {
1195 unsigned JumpOffset = fieldFromInstruction(Insn, StartBit: 0, NumBits: 26) << 2;
1196 Inst.addOperand(Op: MCOperand::createImm(Val: JumpOffset));
1197 return MCDisassembler::Success;
1198}
1199
1200static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst, unsigned Value,
1201 uint64_t Address,
1202 const MCDisassembler *Decoder) {
1203 if (Value == 0)
1204 Inst.addOperand(Op: MCOperand::createImm(Val: 1));
1205 else if (Value == 0x7)
1206 Inst.addOperand(Op: MCOperand::createImm(Val: -1));
1207 else
1208 Inst.addOperand(Op: MCOperand::createImm(Val: Value << 2));
1209 return MCDisassembler::Success;
1210}
1211
1212static DecodeStatus DecodeLi16Imm(MCInst &Inst, unsigned Value,
1213 uint64_t Address,
1214 const MCDisassembler *Decoder) {
1215 if (Value == 0x7F)
1216 Inst.addOperand(Op: MCOperand::createImm(Val: -1));
1217 else
1218 Inst.addOperand(Op: MCOperand::createImm(Val: Value));
1219 return MCDisassembler::Success;
1220}
1221
1222static DecodeStatus DecodePOOL16BEncodedField(MCInst &Inst, unsigned Value,
1223 uint64_t Address,
1224 const MCDisassembler *Decoder) {
1225 Inst.addOperand(Op: MCOperand::createImm(Val: Value == 0x0 ? 8 : Value));
1226 return MCDisassembler::Success;
1227}
1228
1229template <unsigned Bits, int Offset, int Scale>
1230static DecodeStatus
1231DecodeUImmWithOffsetAndScale(MCInst &Inst, unsigned Value, uint64_t Address,
1232 const MCDisassembler *Decoder) {
1233 Value &= ((1 << Bits) - 1);
1234 Value *= Scale;
1235 Inst.addOperand(Op: MCOperand::createImm(Val: Value + Offset));
1236 return MCDisassembler::Success;
1237}
1238
1239template <unsigned Bits, int Offset = 0, int ScaleBy = 1>
1240static DecodeStatus
1241DecodeSImmWithOffsetAndScale(MCInst &Inst, unsigned Value, uint64_t Address,
1242 const MCDisassembler *Decoder) {
1243 int32_t Imm = SignExtend32<Bits>(Value) * ScaleBy;
1244 Inst.addOperand(Op: MCOperand::createImm(Val: Imm + Offset));
1245 return MCDisassembler::Success;
1246}
1247
1248template <unsigned Bits, int Offset>
1249static DecodeStatus DecodeUImmWithOffset(MCInst &Inst, unsigned Value,
1250 uint64_t Address,
1251 const MCDisassembler *Decoder) {
1252 return DecodeUImmWithOffsetAndScale<Bits, Offset, 1>(Inst, Value, Address,
1253 Decoder);
1254}
1255
1256static DecodeStatus DecodeInsSize(MCInst &Inst, unsigned Insn, uint64_t Address,
1257 const MCDisassembler *Decoder) {
1258 // First we need to grab the pos(lsb) from MCInst.
1259 // This function only handles the 32 bit variants of ins, as dins
1260 // variants are handled differently.
1261 int Pos = Inst.getOperand(i: 2).getImm();
1262 int Size = (int) Insn - Pos + 1;
1263 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend32<16>(X: Size)));
1264 return MCDisassembler::Success;
1265}
1266
1267static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1268 uint64_t Address,
1269 const MCDisassembler *Decoder) {
1270 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend32<19>(X: Insn) * 4));
1271 return MCDisassembler::Success;
1272}
1273
1274static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1275 uint64_t Address,
1276 const MCDisassembler *Decoder) {
1277 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend32<18>(X: Insn) * 8));
1278 return MCDisassembler::Success;
1279}
1280
1281static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn, uint64_t Address,
1282 const MCDisassembler *Decoder) {
1283 int32_t DecodedValue;
1284 switch (Insn) {
1285 case 0: DecodedValue = 256; break;
1286 case 1: DecodedValue = 257; break;
1287 case 510: DecodedValue = -258; break;
1288 case 511: DecodedValue = -257; break;
1289 default: DecodedValue = SignExtend32<9>(X: Insn); break;
1290 }
1291 Inst.addOperand(Op: MCOperand::createImm(Val: DecodedValue * 4));
1292 return MCDisassembler::Success;
1293}
1294
1295static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1296 uint64_t Address,
1297 const MCDisassembler *Decoder) {
1298 // Insn must be >= 0, since it is unsigned that condition is always true.
1299 assert(Insn < 16);
1300 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15,
1301 16, 31, 32, 63, 64, 255, 32768, 65535};
1302 Inst.addOperand(Op: MCOperand::createImm(Val: DecodedValues[Insn]));
1303 return MCDisassembler::Success;
1304}
1305
1306static DecodeStatus DecodeMovePRegPair(MCInst &Inst, unsigned RegPair,
1307 uint64_t Address,
1308 const MCDisassembler *Decoder) {
1309 switch (RegPair) {
1310 default:
1311 return MCDisassembler::Fail;
1312 case 0:
1313 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R5));
1314 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R6));
1315 break;
1316 case 1:
1317 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R5));
1318 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R7));
1319 break;
1320 case 2:
1321 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R6));
1322 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R7));
1323 break;
1324 case 3:
1325 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R4));
1326 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R21));
1327 break;
1328 case 4:
1329 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R4));
1330 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R22));
1331 break;
1332 case 5:
1333 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R4));
1334 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R5));
1335 break;
1336 case 6:
1337 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R4));
1338 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R6));
1339 break;
1340 case 7:
1341 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R4));
1342 Inst.addOperand(Op: MCOperand::createReg(Reg: Mips::R7));
1343 break;
1344 }
1345
1346 return MCDisassembler::Success;
1347}
1348
1349static DecodeStatus DecodeMovePOperands(MCInst &Inst, unsigned Insn,
1350 uint64_t Address,
1351 const MCDisassembler *Decoder) {
1352 unsigned RegPair = fieldFromInstruction(Insn, StartBit: 7, NumBits: 3);
1353 if (DecodeMovePRegPair(Inst, RegPair, Address, Decoder) ==
1354 MCDisassembler::Fail)
1355 return MCDisassembler::Fail;
1356
1357 unsigned RegRs;
1358 if (static_cast<const MipsDisassembler *>(Decoder)->hasMips32r6())
1359 RegRs = fieldFromInstruction(Insn, StartBit: 0, NumBits: 2) |
1360 (fieldFromInstruction(Insn, StartBit: 3, NumBits: 1) << 2);
1361 else
1362 RegRs = fieldFromInstruction(Insn, StartBit: 1, NumBits: 3);
1363 if (DecodeGPRMM16MovePRegisterClass(Inst, RegNo: RegRs, Address, Decoder) ==
1364 MCDisassembler::Fail)
1365 return MCDisassembler::Fail;
1366
1367 unsigned RegRt = fieldFromInstruction(Insn, StartBit: 4, NumBits: 3);
1368 if (DecodeGPRMM16MovePRegisterClass(Inst, RegNo: RegRt, Address, Decoder) ==
1369 MCDisassembler::Fail)
1370 return MCDisassembler::Fail;
1371
1372 return MCDisassembler::Success;
1373}
1374
1375static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1376 uint64_t Address,
1377 const MCDisassembler *Decoder) {
1378 Inst.addOperand(Op: MCOperand::createImm(Val: SignExtend32<25>(X: Insn << 2)));
1379 return MCDisassembler::Success;
1380}
1381
1382template <typename InsnType>
1383static DecodeStatus DecodeBgtzGroupBranchMMR6(MCInst &MI, InsnType insn,
1384 uint64_t Address,
1385 const MCDisassembler *Decoder) {
1386 // We have:
1387 // 0b000111 ttttt sssss iiiiiiiiiiiiiiii
1388 // Invalid if rt == 0
1389 // BGTZALC_MMR6 if rs == 0 && rt != 0
1390 // BLTZALC_MMR6 if rs != 0 && rs == rt
1391 // BLTUC_MMR6 if rs != 0 && rs != rt
1392
1393 InsnType Rt = fieldFromInstruction(insn, 21, 5);
1394 InsnType Rs = fieldFromInstruction(insn, 16, 5);
1395 InsnType Imm = 0;
1396 bool HasRs = false;
1397 bool HasRt = false;
1398
1399 if (Rt == 0)
1400 return MCDisassembler::Fail;
1401 else if (Rs == 0) {
1402 MI.setOpcode(Mips::BGTZALC_MMR6);
1403 HasRt = true;
1404 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
1405 }
1406 else if (Rs == Rt) {
1407 MI.setOpcode(Mips::BLTZALC_MMR6);
1408 HasRs = true;
1409 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
1410 }
1411 else {
1412 MI.setOpcode(Mips::BLTUC_MMR6);
1413 HasRs = true;
1414 HasRt = true;
1415 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1416 }
1417
1418 if (HasRs)
1419 MI.addOperand(
1420 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rs)));
1421
1422 if (HasRt)
1423 MI.addOperand(
1424 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rt)));
1425
1426 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
1427
1428 return MCDisassembler::Success;
1429}
1430
1431template <typename InsnType>
1432static DecodeStatus DecodeBlezGroupBranchMMR6(MCInst &MI, InsnType insn,
1433 uint64_t Address,
1434 const MCDisassembler *Decoder) {
1435 // We have:
1436 // 0b000110 ttttt sssss iiiiiiiiiiiiiiii
1437 // Invalid if rt == 0
1438 // BLEZALC_MMR6 if rs == 0 && rt != 0
1439 // BGEZALC_MMR6 if rs == rt && rt != 0
1440 // BGEUC_MMR6 if rs != rt && rs != 0 && rt != 0
1441
1442 InsnType Rt = fieldFromInstruction(insn, 21, 5);
1443 InsnType Rs = fieldFromInstruction(insn, 16, 5);
1444 InsnType Imm = 0;
1445 bool HasRs = false;
1446
1447 if (Rt == 0)
1448 return MCDisassembler::Fail;
1449 else if (Rs == 0) {
1450 MI.setOpcode(Mips::BLEZALC_MMR6);
1451 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
1452 }
1453 else if (Rs == Rt) {
1454 MI.setOpcode(Mips::BGEZALC_MMR6);
1455 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 2 + 4;
1456 }
1457 else {
1458 HasRs = true;
1459 MI.setOpcode(Mips::BGEUC_MMR6);
1460 Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4 + 4;
1461 }
1462
1463 if (HasRs)
1464 MI.addOperand(
1465 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rs)));
1466 MI.addOperand(
1467 Op: MCOperand::createReg(Reg: getReg(Decoder, Mips::GPR32RegClassID, Rt)));
1468
1469 MI.addOperand(Op: MCOperand::createImm(Val: Imm));
1470
1471 return MCDisassembler::Success;
1472}
1473
1474// This instruction does not have a working decoder, and needs to be
1475// fixed. This "fixme" function was introduced to keep the backend compiling,
1476// while making changes to tablegen code.
1477static DecodeStatus DecodeFIXMEInstruction(MCInst &Inst, unsigned Insn,
1478 uint64_t Address,
1479 const MCDisassembler *Decoder) {
1480 return MCDisassembler::Fail;
1481}
1482
1483#include "MipsGenDisassemblerTables.inc"
1484
1485/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
1486/// according to the given endianness.
1487static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
1488 uint64_t &Size, uint32_t &Insn,
1489 bool IsBigEndian) {
1490 // We want to read exactly 2 Bytes of data.
1491 if (Bytes.size() < 2) {
1492 Size = 0;
1493 return MCDisassembler::Fail;
1494 }
1495
1496 if (IsBigEndian) {
1497 Insn = (Bytes[0] << 8) | Bytes[1];
1498 } else {
1499 Insn = (Bytes[1] << 8) | Bytes[0];
1500 }
1501
1502 return MCDisassembler::Success;
1503}
1504
1505/// Read four bytes from the ArrayRef and return 32 bit word sorted
1506/// according to the given endianness.
1507static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
1508 uint64_t &Size, uint32_t &Insn,
1509 bool IsBigEndian, bool IsMicroMips) {
1510 // We want to read exactly 4 Bytes of data.
1511 if (Bytes.size() < 4) {
1512 Size = 0;
1513 return MCDisassembler::Fail;
1514 }
1515
1516 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
1517 // always precede the low 16 bits in the instruction stream (that is, they
1518 // are placed at lower addresses in the instruction stream).
1519 //
1520 // microMIPS byte ordering:
1521 // Big-endian: 0 | 1 | 2 | 3
1522 // Little-endian: 1 | 0 | 3 | 2
1523
1524 if (IsBigEndian) {
1525 // Encoded as a big-endian 32-bit word in the stream.
1526 Insn =
1527 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
1528 } else {
1529 if (IsMicroMips) {
1530 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
1531 (Bytes[1] << 24);
1532 } else {
1533 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
1534 (Bytes[3] << 24);
1535 }
1536 }
1537
1538 return MCDisassembler::Success;
1539}
1540
1541DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
1542 ArrayRef<uint8_t> Bytes,
1543 uint64_t Address,
1544 raw_ostream &CStream) const {
1545 uint32_t Insn;
1546 DecodeStatus Result;
1547 Size = 0;
1548
1549 if (IsMicroMips) {
1550 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
1551 if (Result == MCDisassembler::Fail)
1552 return MCDisassembler::Fail;
1553
1554 if (hasMips32r6()) {
1555 LLVM_DEBUG(
1556 dbgs() << "Trying MicroMipsR616 table (16-bit instructions):\n");
1557 // Calling the auto-generated decoder function for microMIPS32R6
1558 // 16-bit instructions.
1559 Result = decodeInstruction(DecodeTable: DecoderTableMicroMipsR616, MI&: Instr, insn: Insn,
1560 Address, DisAsm: this, STI);
1561 if (Result != MCDisassembler::Fail) {
1562 Size = 2;
1563 return Result;
1564 }
1565 }
1566
1567 LLVM_DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
1568 // Calling the auto-generated decoder function for microMIPS 16-bit
1569 // instructions.
1570 Result = decodeInstruction(DecodeTable: DecoderTableMicroMips16, MI&: Instr, insn: Insn, Address,
1571 DisAsm: this, STI);
1572 if (Result != MCDisassembler::Fail) {
1573 Size = 2;
1574 return Result;
1575 }
1576
1577 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, IsMicroMips: true);
1578 if (Result == MCDisassembler::Fail)
1579 return MCDisassembler::Fail;
1580
1581 if (hasMips32r6()) {
1582 LLVM_DEBUG(
1583 dbgs() << "Trying MicroMips32r632 table (32-bit instructions):\n");
1584 // Calling the auto-generated decoder function.
1585 Result = decodeInstruction(DecodeTable: DecoderTableMicroMipsR632, MI&: Instr, insn: Insn,
1586 Address, DisAsm: this, STI);
1587 if (Result != MCDisassembler::Fail) {
1588 Size = 4;
1589 return Result;
1590 }
1591 }
1592
1593 LLVM_DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
1594 // Calling the auto-generated decoder function.
1595 Result = decodeInstruction(DecodeTable: DecoderTableMicroMips32, MI&: Instr, insn: Insn, Address,
1596 DisAsm: this, STI);
1597 if (Result != MCDisassembler::Fail) {
1598 Size = 4;
1599 return Result;
1600 }
1601
1602 if (isFP64()) {
1603 LLVM_DEBUG(dbgs() << "Trying MicroMipsFP64 table (32-bit opcodes):\n");
1604 Result = decodeInstruction(DecodeTable: DecoderTableMicroMipsFP6432, MI&: Instr, insn: Insn,
1605 Address, DisAsm: this, STI);
1606 if (Result != MCDisassembler::Fail) {
1607 Size = 4;
1608 return Result;
1609 }
1610 }
1611
1612 // This is an invalid instruction. Claim that the Size is 2 bytes. Since
1613 // microMIPS instructions have a minimum alignment of 2, the next 2 bytes
1614 // could form a valid instruction. The two bytes we rejected as an
1615 // instruction could have actually beeen an inline constant pool that is
1616 // unconditionally branched over.
1617 Size = 2;
1618 return MCDisassembler::Fail;
1619 }
1620
1621 // Attempt to read the instruction so that we can attempt to decode it. If
1622 // the buffer is not 4 bytes long, let the higher level logic figure out
1623 // what to do with a size of zero and MCDisassembler::Fail.
1624 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, IsMicroMips: false);
1625 if (Result == MCDisassembler::Fail)
1626 return MCDisassembler::Fail;
1627
1628 // The only instruction size for standard encoded MIPS.
1629 Size = 4;
1630
1631 if (hasCOP3()) {
1632 LLVM_DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
1633 Result =
1634 decodeInstruction(DecodeTable: DecoderTableCOP3_32, MI&: Instr, insn: Insn, Address, DisAsm: this, STI);
1635 if (Result != MCDisassembler::Fail)
1636 return Result;
1637 }
1638
1639 if (hasMips32r6() && isGP64()) {
1640 LLVM_DEBUG(
1641 dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
1642 Result = decodeInstruction(DecodeTable: DecoderTableMips32r6_64r6_GP6432, MI&: Instr, insn: Insn,
1643 Address, DisAsm: this, STI);
1644 if (Result != MCDisassembler::Fail)
1645 return Result;
1646 }
1647
1648 if (hasMips32r6() && isPTR64()) {
1649 LLVM_DEBUG(
1650 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1651 Result = decodeInstruction(DecodeTable: DecoderTableMips32r6_64r6_PTR6432, MI&: Instr, insn: Insn,
1652 Address, DisAsm: this, STI);
1653 if (Result != MCDisassembler::Fail)
1654 return Result;
1655 }
1656
1657 if (hasMips32r6()) {
1658 LLVM_DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
1659 Result = decodeInstruction(DecodeTable: DecoderTableMips32r6_64r632, MI&: Instr, insn: Insn,
1660 Address, DisAsm: this, STI);
1661 if (Result != MCDisassembler::Fail)
1662 return Result;
1663 }
1664
1665 if (hasMips2() && isPTR64()) {
1666 LLVM_DEBUG(
1667 dbgs() << "Trying Mips32r6_64r6 (PTR64) table (32-bit opcodes):\n");
1668 Result = decodeInstruction(DecodeTable: DecoderTableMips32_64_PTR6432, MI&: Instr, insn: Insn,
1669 Address, DisAsm: this, STI);
1670 if (Result != MCDisassembler::Fail)
1671 return Result;
1672 }
1673
1674 if (hasCnMips()) {
1675 LLVM_DEBUG(dbgs() << "Trying CnMips table (32-bit opcodes):\n");
1676 Result = decodeInstruction(DecodeTable: DecoderTableCnMips32, MI&: Instr, insn: Insn, Address, DisAsm: this,
1677 STI);
1678 if (Result != MCDisassembler::Fail)
1679 return Result;
1680 }
1681
1682 if (hasCnMipsP()) {
1683 LLVM_DEBUG(dbgs() << "Trying CnMipsP table (32-bit opcodes):\n");
1684 Result = decodeInstruction(DecodeTable: DecoderTableCnMipsP32, MI&: Instr, insn: Insn, Address,
1685 DisAsm: this, STI);
1686 if (Result != MCDisassembler::Fail)
1687 return Result;
1688 }
1689
1690 if (isGP64()) {
1691 LLVM_DEBUG(dbgs() << "Trying Mips64 (GPR64) table (32-bit opcodes):\n");
1692 Result = decodeInstruction(DecodeTable: DecoderTableMips6432, MI&: Instr, insn: Insn, Address, DisAsm: this,
1693 STI);
1694 if (Result != MCDisassembler::Fail)
1695 return Result;
1696 }
1697
1698 if (isFP64()) {
1699 LLVM_DEBUG(
1700 dbgs() << "Trying MipsFP64 (64 bit FPU) table (32-bit opcodes):\n");
1701 Result = decodeInstruction(DecodeTable: DecoderTableMipsFP6432, MI&: Instr, insn: Insn, Address,
1702 DisAsm: this, STI);
1703 if (Result != MCDisassembler::Fail)
1704 return Result;
1705 }
1706
1707 LLVM_DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
1708 // Calling the auto-generated decoder function.
1709 Result =
1710 decodeInstruction(DecodeTable: DecoderTableMips32, MI&: Instr, insn: Insn, Address, DisAsm: this, STI);
1711 if (Result != MCDisassembler::Fail)
1712 return Result;
1713
1714 return MCDisassembler::Fail;
1715}
1716