blob: 600971986a3ceb43bc0e5c2e548b6891ee8ead04 [file] [log] [blame]
Akira Hatanaka71928e62012-04-17 18:03:21 +00001//===- 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"
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000015#include "MipsRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "MipsSubtarget.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000017#include "llvm/MC/MCContext.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000018#include "llvm/MC/MCDisassembler.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000019#include "llvm/MC/MCFixedLenDisassembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/Support/MathExtras.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000023#include "llvm/Support/TargetRegistry.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000024
Akira Hatanaka71928e62012-04-17 18:03:21 +000025using namespace llvm;
26
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "mips-disassembler"
28
Akira Hatanaka71928e62012-04-17 18:03:21 +000029typedef MCDisassembler::DecodeStatus DecodeStatus;
30
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000031namespace {
32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000033/// A disasembler class for Mips.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000034class MipsDisassemblerBase : public MCDisassembler {
Akira Hatanaka71928e62012-04-17 18:03:21 +000035public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000036 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000037 bool IsBigEndian)
38 : MCDisassembler(STI, Ctx),
Vladimir Medice8860932014-12-16 15:29:12 +000039 IsGP64Bit(STI.getFeatureBits() & Mips::FeatureGP64Bit),
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000040 IsBigEndian(IsBigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000041
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000042 virtual ~MipsDisassemblerBase() {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000043
Vladimir Medice8860932014-12-16 15:29:12 +000044 bool isGP64Bit() const { return IsGP64Bit; }
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000045
Akira Hatanaka71928e62012-04-17 18:03:21 +000046private:
Vladimir Medice8860932014-12-16 15:29:12 +000047 bool IsGP64Bit;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000048protected:
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000049 bool IsBigEndian;
Akira Hatanaka71928e62012-04-17 18:03:21 +000050};
51
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000052/// A disasembler class for Mips32.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000053class MipsDisassembler : public MipsDisassemblerBase {
Vladimir Medicdde3d582013-09-06 12:30:36 +000054 bool IsMicroMips;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000055public:
Daniel Sandersc171f652014-06-13 13:15:59 +000056 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
57 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
58 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
59 }
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000060
Daniel Sandersc171f652014-06-13 13:15:59 +000061 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
62 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
63 bool hasMips32r6() const {
Daniel Sanders5c582b22014-05-22 11:23:21 +000064 return STI.getFeatureBits() & Mips::FeatureMips32r6;
65 }
66
Daniel Sanders0fa60412014-06-12 13:39:06 +000067 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
68
Daniel Sandersc171f652014-06-13 13:15:59 +000069 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
72 }
73
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000074 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000075 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000076 raw_ostream &VStream,
77 raw_ostream &CStream) const override;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000078};
79
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000080/// A disasembler class for Mips64.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000081class Mips64Disassembler : public MipsDisassemblerBase {
Akira Hatanaka71928e62012-04-17 18:03:21 +000082public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000083 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000084 bool bigEndian) :
Lang Hamesa1bc0f52014-04-15 04:40:56 +000085 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000086
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000087 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000088 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000089 raw_ostream &VStream,
90 raw_ostream &CStream) const override;
Akira Hatanaka71928e62012-04-17 18:03:21 +000091};
92
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000093} // end anonymous namespace
94
Akira Hatanaka71928e62012-04-17 18:03:21 +000095// Forward declare these because the autogenerated code will reference them.
96// Definitions are further down.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000097static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
98 unsigned RegNo,
99 uint64_t Address,
100 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000101
Reed Kotlerec8a5492013-02-14 03:05:25 +0000102static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
103 unsigned RegNo,
104 uint64_t Address,
105 const void *Decoder);
106
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000107static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
108 unsigned RegNo,
109 uint64_t Address,
110 const void *Decoder);
111
Jozef Kolek1904fa22014-11-24 14:25:53 +0000112static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
113 unsigned RegNo,
114 uint64_t Address,
115 const void *Decoder);
116
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000117static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
118 unsigned RegNo,
119 uint64_t Address,
120 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000121
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000122static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
123 unsigned Insn,
124 uint64_t Address,
125 const void *Decoder);
126
Akira Hatanaka654655f2013-08-14 00:53:38 +0000127static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
128 unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000131
Akira Hatanaka71928e62012-04-17 18:03:21 +0000132static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
133 unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
136
137static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
138 unsigned RegNo,
139 uint64_t Address,
140 const void *Decoder);
141
142static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
143 unsigned RegNo,
144 uint64_t Address,
145 const void *Decoder);
146
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000147static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
148 unsigned RegNo,
149 uint64_t Address,
150 const void *Decoder);
151
Daniel Sanders0fa60412014-06-12 13:39:06 +0000152static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
153 uint64_t Address,
154 const void *Decoder);
155
Akira Hatanaka71928e62012-04-17 18:03:21 +0000156static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
157 unsigned Insn,
158 uint64_t Address,
159 const void *Decoder);
160
161static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
162 unsigned RegNo,
163 uint64_t Address,
164 const void *Decoder);
165
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000166static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
167 unsigned RegNo,
168 uint64_t Address,
169 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000170
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000171static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
172 unsigned RegNo,
173 uint64_t Address,
174 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000175
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000176static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
177 unsigned RegNo,
178 uint64_t Address,
179 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000180
Jack Carter3eb663b2013-09-26 00:09:46 +0000181static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
182 unsigned RegNo,
183 uint64_t Address,
184 const void *Decoder);
185
Jack Carter5dc8ac92013-09-25 23:50:44 +0000186static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
187 unsigned RegNo,
188 uint64_t Address,
189 const void *Decoder);
190
191static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
192 unsigned RegNo,
193 uint64_t Address,
194 const void *Decoder);
195
196static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
197 unsigned RegNo,
198 uint64_t Address,
199 const void *Decoder);
200
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000201static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
202 unsigned RegNo,
203 uint64_t Address,
204 const void *Decoder);
205
Daniel Sanders2a83d682014-05-21 12:56:39 +0000206static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
207 unsigned RegNo,
208 uint64_t Address,
209 const void *Decoder);
210
Akira Hatanaka71928e62012-04-17 18:03:21 +0000211static DecodeStatus DecodeBranchTarget(MCInst &Inst,
212 unsigned Offset,
213 uint64_t Address,
214 const void *Decoder);
215
Akira Hatanaka71928e62012-04-17 18:03:21 +0000216static DecodeStatus DecodeJumpTarget(MCInst &Inst,
217 unsigned Insn,
218 uint64_t Address,
219 const void *Decoder);
220
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000221static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
222 unsigned Offset,
223 uint64_t Address,
224 const void *Decoder);
225
226static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
227 unsigned Offset,
228 uint64_t Address,
229 const void *Decoder);
230
Jozef Kolek9761e962015-01-12 12:03:34 +0000231// DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
232// shifted left by 1 bit.
233static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
234 unsigned Offset,
235 uint64_t Address,
236 const void *Decoder);
237
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000238// DecodeBranchTarget10MM - Decode microMIPS branch offset, which is
239// shifted left by 1 bit.
240static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
241 unsigned Offset,
242 uint64_t Address,
243 const void *Decoder);
244
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000245// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
246// shifted left by 1 bit.
247static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
248 unsigned Offset,
249 uint64_t Address,
250 const void *Decoder);
251
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000252// DecodeJumpTargetMM - Decode microMIPS jump target, which is
253// shifted left by 1 bit.
254static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
255 unsigned Insn,
256 uint64_t Address,
257 const void *Decoder);
258
Akira Hatanaka71928e62012-04-17 18:03:21 +0000259static DecodeStatus DecodeMem(MCInst &Inst,
260 unsigned Insn,
261 uint64_t Address,
262 const void *Decoder);
263
Daniel Sanders92db6b72014-10-01 08:26:55 +0000264static DecodeStatus DecodeCacheOp(MCInst &Inst,
265 unsigned Insn,
266 uint64_t Address,
267 const void *Decoder);
268
Jozef Kolekab6d1cc2014-12-23 19:55:34 +0000269static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
270 unsigned Insn,
271 uint64_t Address,
272 const void *Decoder);
273
Daniel Sandersb4484d62014-11-27 17:28:10 +0000274static DecodeStatus DecodeSyncI(MCInst &Inst,
275 unsigned Insn,
276 uint64_t Address,
277 const void *Decoder);
278
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000279static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
280 uint64_t Address, const void *Decoder);
281
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000282static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
283 unsigned Insn,
284 uint64_t Address,
285 const void *Decoder);
286
Jozef Kolek12c69822014-12-23 16:16:33 +0000287static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
288 unsigned Insn,
289 uint64_t Address,
290 const void *Decoder);
291
Vladimir Medicdde3d582013-09-06 12:30:36 +0000292static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
293 unsigned Insn,
294 uint64_t Address,
295 const void *Decoder);
296
297static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
298 unsigned Insn,
299 uint64_t Address,
300 const void *Decoder);
301
Akira Hatanaka71928e62012-04-17 18:03:21 +0000302static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
303 uint64_t Address,
304 const void *Decoder);
305
Daniel Sanders92db6b72014-10-01 08:26:55 +0000306static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
307 uint64_t Address,
308 const void *Decoder);
309
310static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
311 uint64_t Address,
312 const void *Decoder);
313
Vladimir Medic435cf8a2015-01-21 10:47:36 +0000314static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
315 uint64_t Address,
316 const void *Decoder);
317
Daniel Sanders6a803f62014-06-16 13:13:03 +0000318static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
319 unsigned Insn,
320 uint64_t Address,
321 const void *Decoder);
322
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000323static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
324 unsigned Value,
325 uint64_t Address,
326 const void *Decoder);
327
328static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
329 unsigned Value,
330 uint64_t Address,
331 const void *Decoder);
332
333static DecodeStatus DecodeLiSimm7(MCInst &Inst,
334 unsigned Value,
335 uint64_t Address,
336 const void *Decoder);
337
338static DecodeStatus DecodeSimm4(MCInst &Inst,
339 unsigned Value,
340 uint64_t Address,
341 const void *Decoder);
342
Akira Hatanaka71928e62012-04-17 18:03:21 +0000343static DecodeStatus DecodeSimm16(MCInst &Inst,
344 unsigned Insn,
345 uint64_t Address,
346 const void *Decoder);
347
Matheus Almeida779c5932013-11-18 12:32:49 +0000348// Decode the immediate field of an LSA instruction which
349// is off by one.
350static DecodeStatus DecodeLSAImm(MCInst &Inst,
351 unsigned Insn,
352 uint64_t Address,
353 const void *Decoder);
354
Akira Hatanaka71928e62012-04-17 18:03:21 +0000355static DecodeStatus DecodeInsSize(MCInst &Inst,
356 unsigned Insn,
357 uint64_t Address,
358 const void *Decoder);
359
360static DecodeStatus DecodeExtSize(MCInst &Inst,
361 unsigned Insn,
362 uint64_t Address,
363 const void *Decoder);
364
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000365static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
366 uint64_t Address, const void *Decoder);
367
Zoran Jovanovic28551422014-06-09 09:49:51 +0000368static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
369 uint64_t Address, const void *Decoder);
370
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000371static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
372 uint64_t Address, const void *Decoder);
373
374static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
375 uint64_t Address, const void *Decoder);
376
377static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
378 uint64_t Address, const void *Decoder);
379
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000380static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
381 uint64_t Address, const void *Decoder);
382
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000383/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
384/// handle.
385template <typename InsnType>
386static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
387 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000388
389template <typename InsnType>
390static DecodeStatus
391DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
392 const void *Decoder);
393
394template <typename InsnType>
395static DecodeStatus
396DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
397 const void *Decoder);
398
399template <typename InsnType>
400static DecodeStatus
401DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
403
404template <typename InsnType>
405static DecodeStatus
406DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
408
409template <typename InsnType>
410static DecodeStatus
411DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
413
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000414template <typename InsnType>
415static DecodeStatus
416DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
418
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000419static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
420 uint64_t Address,
421 const void *Decoder);
422
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000423static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
424 uint64_t Address,
425 const void *Decoder);
426
Akira Hatanaka71928e62012-04-17 18:03:21 +0000427namespace llvm {
428extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
429 TheMips64elTarget;
430}
431
432static MCDisassembler *createMipsDisassembler(
433 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000434 const MCSubtargetInfo &STI,
435 MCContext &Ctx) {
436 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000437}
438
439static MCDisassembler *createMipselDisassembler(
440 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000441 const MCSubtargetInfo &STI,
442 MCContext &Ctx) {
443 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000444}
445
446static MCDisassembler *createMips64Disassembler(
447 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000448 const MCSubtargetInfo &STI,
449 MCContext &Ctx) {
450 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000451}
452
453static MCDisassembler *createMips64elDisassembler(
454 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000455 const MCSubtargetInfo &STI,
456 MCContext &Ctx) {
457 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000458}
459
460extern "C" void LLVMInitializeMipsDisassembler() {
461 // Register the disassembler.
462 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
463 createMipsDisassembler);
464 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
465 createMipselDisassembler);
466 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
467 createMips64Disassembler);
468 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
469 createMips64elDisassembler);
470}
471
Akira Hatanaka71928e62012-04-17 18:03:21 +0000472#include "MipsGenDisassemblerTables.inc"
473
Daniel Sanders5c582b22014-05-22 11:23:21 +0000474static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
475 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
476 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
477 return *(RegInfo->getRegClass(RC).begin() + RegNo);
478}
479
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000480template <typename InsnType>
481static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
482 const void *Decoder) {
483 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
484 // The size of the n field depends on the element size
485 // The register class also depends on this.
486 InsnType tmp = fieldFromInstruction(insn, 17, 5);
487 unsigned NSize = 0;
488 DecodeFN RegDecoder = nullptr;
489 if ((tmp & 0x18) == 0x00) { // INSVE_B
490 NSize = 4;
491 RegDecoder = DecodeMSA128BRegisterClass;
492 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
493 NSize = 3;
494 RegDecoder = DecodeMSA128HRegisterClass;
495 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
496 NSize = 2;
497 RegDecoder = DecodeMSA128WRegisterClass;
498 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
499 NSize = 1;
500 RegDecoder = DecodeMSA128DRegisterClass;
501 } else
502 llvm_unreachable("Invalid encoding");
503
504 assert(NSize != 0 && RegDecoder != nullptr);
505
506 // $wd
507 tmp = fieldFromInstruction(insn, 6, 5);
508 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
509 return MCDisassembler::Fail;
510 // $wd_in
511 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
512 return MCDisassembler::Fail;
513 // $n
514 tmp = fieldFromInstruction(insn, 16, NSize);
515 MI.addOperand(MCOperand::CreateImm(tmp));
516 // $ws
517 tmp = fieldFromInstruction(insn, 11, 5);
518 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
519 return MCDisassembler::Fail;
520 // $n2
521 MI.addOperand(MCOperand::CreateImm(0));
522
523 return MCDisassembler::Success;
524}
525
Daniel Sanders5c582b22014-05-22 11:23:21 +0000526template <typename InsnType>
527static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
528 uint64_t Address,
529 const void *Decoder) {
530 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
531 // (otherwise we would have matched the ADDI instruction from the earlier
532 // ISA's instead).
533 //
534 // We have:
535 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
536 // BOVC if rs >= rt
537 // BEQZALC if rs == 0 && rt != 0
538 // BEQC if rs < rt && rs != 0
539
540 InsnType Rs = fieldFromInstruction(insn, 21, 5);
541 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000542 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000543 bool HasRs = false;
544
545 if (Rs >= Rt) {
546 MI.setOpcode(Mips::BOVC);
547 HasRs = true;
548 } else if (Rs != 0 && Rs < Rt) {
549 MI.setOpcode(Mips::BEQC);
550 HasRs = true;
551 } else
552 MI.setOpcode(Mips::BEQZALC);
553
554 if (HasRs)
555 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
556 Rs)));
557
558 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
559 Rt)));
560 MI.addOperand(MCOperand::CreateImm(Imm));
561
562 return MCDisassembler::Success;
563}
564
565template <typename InsnType>
566static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
567 uint64_t Address,
568 const void *Decoder) {
569 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
570 // (otherwise we would have matched the ADDI instruction from the earlier
571 // ISA's instead).
572 //
573 // We have:
574 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
575 // BNVC if rs >= rt
576 // BNEZALC if rs == 0 && rt != 0
577 // BNEC if rs < rt && rs != 0
578
579 InsnType Rs = fieldFromInstruction(insn, 21, 5);
580 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000581 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000582 bool HasRs = false;
583
584 if (Rs >= Rt) {
585 MI.setOpcode(Mips::BNVC);
586 HasRs = true;
587 } else if (Rs != 0 && Rs < Rt) {
588 MI.setOpcode(Mips::BNEC);
589 HasRs = true;
590 } else
591 MI.setOpcode(Mips::BNEZALC);
592
593 if (HasRs)
594 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
595 Rs)));
596
597 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
598 Rt)));
599 MI.addOperand(MCOperand::CreateImm(Imm));
600
601 return MCDisassembler::Success;
602}
603
604template <typename InsnType>
605static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
606 uint64_t Address,
607 const void *Decoder) {
608 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
609 // (otherwise we would have matched the BLEZL instruction from the earlier
610 // ISA's instead).
611 //
612 // We have:
613 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
614 // Invalid if rs == 0
615 // BLEZC if rs == 0 && rt != 0
616 // BGEZC if rs == rt && rt != 0
617 // BGEC if rs != rt && rs != 0 && rt != 0
618
619 InsnType Rs = fieldFromInstruction(insn, 21, 5);
620 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000621 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000622 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000623
624 if (Rt == 0)
625 return MCDisassembler::Fail;
626 else if (Rs == 0)
627 MI.setOpcode(Mips::BLEZC);
628 else if (Rs == Rt)
629 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000630 else {
631 HasRs = true;
632 MI.setOpcode(Mips::BGEC);
633 }
634
635 if (HasRs)
636 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
637 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000638
639 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
640 Rt)));
641
642 MI.addOperand(MCOperand::CreateImm(Imm));
643
644 return MCDisassembler::Success;
645}
646
647template <typename InsnType>
648static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
649 uint64_t Address,
650 const void *Decoder) {
651 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
652 // (otherwise we would have matched the BGTZL instruction from the earlier
653 // ISA's instead).
654 //
655 // We have:
656 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
657 // Invalid if rs == 0
658 // BGTZC if rs == 0 && rt != 0
659 // BLTZC if rs == rt && rt != 0
660 // BLTC if rs != rt && rs != 0 && rt != 0
661
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000662 bool HasRs = false;
663
Daniel Sanders5c582b22014-05-22 11:23:21 +0000664 InsnType Rs = fieldFromInstruction(insn, 21, 5);
665 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000666 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000667
668 if (Rt == 0)
669 return MCDisassembler::Fail;
670 else if (Rs == 0)
671 MI.setOpcode(Mips::BGTZC);
672 else if (Rs == Rt)
673 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000674 else {
675 MI.setOpcode(Mips::BLTC);
676 HasRs = true;
677 }
678
679 if (HasRs)
680 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
681 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000682
683 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
684 Rt)));
685
686 MI.addOperand(MCOperand::CreateImm(Imm));
687
688 return MCDisassembler::Success;
689}
690
691template <typename InsnType>
692static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
693 uint64_t Address,
694 const void *Decoder) {
695 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
696 // (otherwise we would have matched the BGTZ instruction from the earlier
697 // ISA's instead).
698 //
699 // We have:
700 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
701 // BGTZ if rt == 0
702 // BGTZALC if rs == 0 && rt != 0
703 // BLTZALC if rs != 0 && rs == rt
704 // BLTUC if rs != 0 && rs != rt
705
706 InsnType Rs = fieldFromInstruction(insn, 21, 5);
707 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000708 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000709 bool HasRs = false;
710 bool HasRt = false;
711
712 if (Rt == 0) {
713 MI.setOpcode(Mips::BGTZ);
714 HasRs = true;
715 } else if (Rs == 0) {
716 MI.setOpcode(Mips::BGTZALC);
717 HasRt = true;
718 } else if (Rs == Rt) {
719 MI.setOpcode(Mips::BLTZALC);
720 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000721 } else {
722 MI.setOpcode(Mips::BLTUC);
723 HasRs = true;
724 HasRt = true;
725 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000726
727 if (HasRs)
728 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
729 Rs)));
730
731 if (HasRt)
732 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
733 Rt)));
734
735 MI.addOperand(MCOperand::CreateImm(Imm));
736
737 return MCDisassembler::Success;
738}
739
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000740template <typename InsnType>
741static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
742 uint64_t Address,
743 const void *Decoder) {
744 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
745 // (otherwise we would have matched the BLEZL instruction from the earlier
746 // ISA's instead).
747 //
748 // We have:
749 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
750 // Invalid if rs == 0
751 // BLEZALC if rs == 0 && rt != 0
752 // BGEZALC if rs == rt && rt != 0
753 // BGEUC if rs != rt && rs != 0 && rt != 0
754
755 InsnType Rs = fieldFromInstruction(insn, 21, 5);
756 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000757 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000758 bool HasRs = false;
759
760 if (Rt == 0)
761 return MCDisassembler::Fail;
762 else if (Rs == 0)
763 MI.setOpcode(Mips::BLEZALC);
764 else if (Rs == Rt)
765 MI.setOpcode(Mips::BGEZALC);
766 else {
767 HasRs = true;
768 MI.setOpcode(Mips::BGEUC);
769 }
770
771 if (HasRs)
772 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
773 Rs)));
774 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
775 Rt)));
776
777 MI.addOperand(MCOperand::CreateImm(Imm));
778
779 return MCDisassembler::Success;
780}
781
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000782/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
783/// according to the given endianess.
784static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
785 uint64_t &Size, uint32_t &Insn,
786 bool IsBigEndian) {
787 // We want to read exactly 2 Bytes of data.
788 if (Bytes.size() < 2) {
789 Size = 0;
790 return MCDisassembler::Fail;
791 }
792
793 if (IsBigEndian) {
794 Insn = (Bytes[0] << 8) | Bytes[1];
795 } else {
796 Insn = (Bytes[1] << 8) | Bytes[0];
797 }
798
799 return MCDisassembler::Success;
800}
801
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000802/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000803/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000804static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
805 uint64_t &Size, uint32_t &Insn,
806 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000807 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000808 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000809 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000810 return MCDisassembler::Fail;
811 }
812
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000813 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
814 // always precede the low 16 bits in the instruction stream (that is, they
815 // are placed at lower addresses in the instruction stream).
816 //
817 // microMIPS byte ordering:
818 // Big-endian: 0 | 1 | 2 | 3
819 // Little-endian: 1 | 0 | 3 | 2
820
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000821 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000822 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000823 Insn =
824 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
825 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000826 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000827 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000828 (Bytes[1] << 24);
829 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000830 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000831 (Bytes[3] << 24);
832 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000833 }
834
835 return MCDisassembler::Success;
836}
837
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000838DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000839 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000840 uint64_t Address,
841 raw_ostream &VStream,
842 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000843 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000844 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000845
Vladimir Medicdde3d582013-09-06 12:30:36 +0000846 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000847 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
848
849 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
850 // Calling the auto-generated decoder function.
851 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
852 this, STI);
853 if (Result != MCDisassembler::Fail) {
854 Size = 2;
855 return Result;
856 }
857
858 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
859 if (Result == MCDisassembler::Fail)
860 return MCDisassembler::Fail;
861
862 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000863 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000864 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000865 this, STI);
866 if (Result != MCDisassembler::Fail) {
867 Size = 4;
868 return Result;
869 }
870 return MCDisassembler::Fail;
871 }
872
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000873 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
874 if (Result == MCDisassembler::Fail)
875 return MCDisassembler::Fail;
876
Daniel Sandersc171f652014-06-13 13:15:59 +0000877 if (hasCOP3()) {
878 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
879 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000880 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000881 if (Result != MCDisassembler::Fail) {
882 Size = 4;
883 return Result;
884 }
885 }
886
887 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000888 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000889 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000890 Address, this, STI);
891 if (Result != MCDisassembler::Fail) {
892 Size = 4;
893 return Result;
894 }
895 }
896
Daniel Sandersc171f652014-06-13 13:15:59 +0000897 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000898 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000899 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000900 Address, this, STI);
901 if (Result != MCDisassembler::Fail) {
902 Size = 4;
903 return Result;
904 }
905 }
906
Daniel Sanders0fa60412014-06-12 13:39:06 +0000907 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000908 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000909 Result =
910 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000911 if (Result != MCDisassembler::Fail) {
912 Size = 4;
913 return Result;
914 }
915
916 return MCDisassembler::Fail;
917}
918
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000919DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000920 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000921 uint64_t Address,
922 raw_ostream &VStream,
923 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000924 uint32_t Insn;
925
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000926 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000927 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000928 if (Result == MCDisassembler::Fail)
929 return MCDisassembler::Fail;
930
931 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000932 Result =
933 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000934 if (Result != MCDisassembler::Fail) {
935 Size = 4;
936 return Result;
937 }
938 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000939 Result =
940 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000941 if (Result != MCDisassembler::Fail) {
942 Size = 4;
943 return Result;
944 }
945
946 return MCDisassembler::Fail;
947}
948
Reed Kotlerec8a5492013-02-14 03:05:25 +0000949static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
950 unsigned RegNo,
951 uint64_t Address,
952 const void *Decoder) {
953
954 return MCDisassembler::Fail;
955
956}
957
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000958static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
959 unsigned RegNo,
960 uint64_t Address,
961 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000962
963 if (RegNo > 31)
964 return MCDisassembler::Fail;
965
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000966 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000967 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000968 return MCDisassembler::Success;
969}
970
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000971static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
972 unsigned RegNo,
973 uint64_t Address,
974 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000975 if (RegNo > 7)
976 return MCDisassembler::Fail;
977 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
978 Inst.addOperand(MCOperand::CreateReg(Reg));
979 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000980}
981
Jozef Kolek1904fa22014-11-24 14:25:53 +0000982static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
983 unsigned RegNo,
984 uint64_t Address,
985 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000986 if (RegNo > 7)
987 return MCDisassembler::Fail;
988 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
989 Inst.addOperand(MCOperand::CreateReg(Reg));
990 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000991}
992
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000993static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
994 unsigned RegNo,
995 uint64_t Address,
996 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000997 if (RegNo > 31)
998 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000999 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001000 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001001 return MCDisassembler::Success;
1002}
1003
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001004static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1005 unsigned RegNo,
1006 uint64_t Address,
1007 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +00001008 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001009 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1010
1011 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1012}
1013
Akira Hatanaka654655f2013-08-14 00:53:38 +00001014static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1015 unsigned RegNo,
1016 uint64_t Address,
1017 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001018 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001019}
1020
Akira Hatanaka71928e62012-04-17 18:03:21 +00001021static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1022 unsigned RegNo,
1023 uint64_t Address,
1024 const void *Decoder) {
1025 if (RegNo > 31)
1026 return MCDisassembler::Fail;
1027
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001028 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1029 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001030 return MCDisassembler::Success;
1031}
1032
1033static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1034 unsigned RegNo,
1035 uint64_t Address,
1036 const void *Decoder) {
1037 if (RegNo > 31)
1038 return MCDisassembler::Fail;
1039
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001040 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1041 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001042 return MCDisassembler::Success;
1043}
1044
1045static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1046 unsigned RegNo,
1047 uint64_t Address,
1048 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001049 if (RegNo > 31)
1050 return MCDisassembler::Fail;
1051 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1052 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001053 return MCDisassembler::Success;
1054}
1055
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001056static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1057 unsigned RegNo,
1058 uint64_t Address,
1059 const void *Decoder) {
1060 if (RegNo > 7)
1061 return MCDisassembler::Fail;
1062 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1063 Inst.addOperand(MCOperand::CreateReg(Reg));
1064 return MCDisassembler::Success;
1065}
1066
Daniel Sanders0fa60412014-06-12 13:39:06 +00001067static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1068 uint64_t Address,
1069 const void *Decoder) {
1070 if (RegNo > 31)
1071 return MCDisassembler::Fail;
1072
1073 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1074 Inst.addOperand(MCOperand::CreateReg(Reg));
1075 return MCDisassembler::Success;
1076}
1077
Akira Hatanaka71928e62012-04-17 18:03:21 +00001078static DecodeStatus DecodeMem(MCInst &Inst,
1079 unsigned Insn,
1080 uint64_t Address,
1081 const void *Decoder) {
1082 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001083 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1084 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001085
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001086 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1087 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001088
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001089 if(Inst.getOpcode() == Mips::SC ||
1090 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001091 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001092 }
1093
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001094 Inst.addOperand(MCOperand::CreateReg(Reg));
1095 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001096 Inst.addOperand(MCOperand::CreateImm(Offset));
1097
1098 return MCDisassembler::Success;
1099}
1100
Daniel Sanders92db6b72014-10-01 08:26:55 +00001101static DecodeStatus DecodeCacheOp(MCInst &Inst,
1102 unsigned Insn,
1103 uint64_t Address,
1104 const void *Decoder) {
1105 int Offset = SignExtend32<16>(Insn & 0xffff);
1106 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1107 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1108
1109 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1110
1111 Inst.addOperand(MCOperand::CreateReg(Base));
1112 Inst.addOperand(MCOperand::CreateImm(Offset));
1113 Inst.addOperand(MCOperand::CreateImm(Hint));
1114
1115 return MCDisassembler::Success;
1116}
1117
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001118static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1119 unsigned Insn,
1120 uint64_t Address,
1121 const void *Decoder) {
1122 int Offset = SignExtend32<12>(Insn & 0xfff);
1123 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1124 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1125
1126 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1127
1128 Inst.addOperand(MCOperand::CreateReg(Base));
1129 Inst.addOperand(MCOperand::CreateImm(Offset));
1130 Inst.addOperand(MCOperand::CreateImm(Hint));
1131
1132 return MCDisassembler::Success;
1133}
1134
Daniel Sandersb4484d62014-11-27 17:28:10 +00001135static DecodeStatus DecodeSyncI(MCInst &Inst,
1136 unsigned Insn,
1137 uint64_t Address,
1138 const void *Decoder) {
1139 int Offset = SignExtend32<16>(Insn & 0xffff);
1140 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1141
1142 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1143
1144 Inst.addOperand(MCOperand::CreateReg(Base));
1145 Inst.addOperand(MCOperand::CreateImm(Offset));
1146
1147 return MCDisassembler::Success;
1148}
1149
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001150static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1151 uint64_t Address, const void *Decoder) {
1152 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1153 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1154 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1155
1156 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1157 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1158
1159 Inst.addOperand(MCOperand::CreateReg(Reg));
1160 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001161
1162 // The immediate field of an LD/ST instruction is scaled which means it must
1163 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1164 // data format.
1165 // .b - 1 byte
1166 // .h - 2 bytes
1167 // .w - 4 bytes
1168 // .d - 8 bytes
1169 switch(Inst.getOpcode())
1170 {
1171 default:
1172 assert (0 && "Unexpected instruction");
1173 return MCDisassembler::Fail;
1174 break;
1175 case Mips::LD_B:
1176 case Mips::ST_B:
1177 Inst.addOperand(MCOperand::CreateImm(Offset));
1178 break;
1179 case Mips::LD_H:
1180 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001181 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001182 break;
1183 case Mips::LD_W:
1184 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001185 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001186 break;
1187 case Mips::LD_D:
1188 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001189 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001190 break;
1191 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001192
1193 return MCDisassembler::Success;
1194}
1195
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001196static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1197 unsigned Insn,
1198 uint64_t Address,
1199 const void *Decoder) {
1200 unsigned Offset = Insn & 0xf;
1201 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1202 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1203
1204 switch (Inst.getOpcode()) {
1205 case Mips::LBU16_MM:
1206 case Mips::LHU16_MM:
1207 case Mips::LW16_MM:
1208 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1209 == MCDisassembler::Fail)
1210 return MCDisassembler::Fail;
1211 break;
1212 case Mips::SB16_MM:
1213 case Mips::SH16_MM:
1214 case Mips::SW16_MM:
1215 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1216 == MCDisassembler::Fail)
1217 return MCDisassembler::Fail;
1218 break;
1219 }
1220
1221 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1222 == MCDisassembler::Fail)
1223 return MCDisassembler::Fail;
1224
1225 switch (Inst.getOpcode()) {
1226 case Mips::LBU16_MM:
1227 if (Offset == 0xf)
1228 Inst.addOperand(MCOperand::CreateImm(-1));
1229 else
1230 Inst.addOperand(MCOperand::CreateImm(Offset));
1231 break;
1232 case Mips::SB16_MM:
1233 Inst.addOperand(MCOperand::CreateImm(Offset));
1234 break;
1235 case Mips::LHU16_MM:
1236 case Mips::SH16_MM:
1237 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1238 break;
1239 case Mips::LW16_MM:
1240 case Mips::SW16_MM:
1241 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1242 break;
1243 }
1244
1245 return MCDisassembler::Success;
1246}
1247
Jozef Kolek12c69822014-12-23 16:16:33 +00001248static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1249 unsigned Insn,
1250 uint64_t Address,
1251 const void *Decoder) {
1252 unsigned Offset = Insn & 0x1F;
1253 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1254
1255 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1256
1257 Inst.addOperand(MCOperand::CreateReg(Reg));
1258 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1259 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1260
1261 return MCDisassembler::Success;
1262}
1263
Vladimir Medicdde3d582013-09-06 12:30:36 +00001264static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1265 unsigned Insn,
1266 uint64_t Address,
1267 const void *Decoder) {
1268 int Offset = SignExtend32<12>(Insn & 0x0fff);
1269 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1270 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1271
1272 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1273 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1274
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001275 switch (Inst.getOpcode()) {
1276 case Mips::SWM32_MM:
1277 case Mips::LWM32_MM:
1278 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1279 == MCDisassembler::Fail)
1280 return MCDisassembler::Fail;
1281 Inst.addOperand(MCOperand::CreateReg(Base));
1282 Inst.addOperand(MCOperand::CreateImm(Offset));
1283 break;
1284 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001285 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001286 // fallthrough
1287 default:
1288 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001289 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1290 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1291
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001292 Inst.addOperand(MCOperand::CreateReg(Base));
1293 Inst.addOperand(MCOperand::CreateImm(Offset));
1294 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001295
1296 return MCDisassembler::Success;
1297}
1298
1299static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1300 unsigned Insn,
1301 uint64_t Address,
1302 const void *Decoder) {
1303 int Offset = SignExtend32<16>(Insn & 0xffff);
1304 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1305 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1306
1307 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1308 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1309
1310 Inst.addOperand(MCOperand::CreateReg(Reg));
1311 Inst.addOperand(MCOperand::CreateReg(Base));
1312 Inst.addOperand(MCOperand::CreateImm(Offset));
1313
1314 return MCDisassembler::Success;
1315}
1316
Akira Hatanaka71928e62012-04-17 18:03:21 +00001317static DecodeStatus DecodeFMem(MCInst &Inst,
1318 unsigned Insn,
1319 uint64_t Address,
1320 const void *Decoder) {
1321 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001322 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1323 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001324
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001325 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001326 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001327
1328 Inst.addOperand(MCOperand::CreateReg(Reg));
1329 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001330 Inst.addOperand(MCOperand::CreateImm(Offset));
1331
1332 return MCDisassembler::Success;
1333}
1334
Daniel Sanders92db6b72014-10-01 08:26:55 +00001335static DecodeStatus DecodeFMem2(MCInst &Inst,
1336 unsigned Insn,
1337 uint64_t Address,
1338 const void *Decoder) {
1339 int Offset = SignExtend32<16>(Insn & 0xffff);
1340 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1341 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1342
1343 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1344 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1345
1346 Inst.addOperand(MCOperand::CreateReg(Reg));
1347 Inst.addOperand(MCOperand::CreateReg(Base));
1348 Inst.addOperand(MCOperand::CreateImm(Offset));
1349
1350 return MCDisassembler::Success;
1351}
1352
1353static DecodeStatus DecodeFMem3(MCInst &Inst,
1354 unsigned Insn,
1355 uint64_t Address,
1356 const void *Decoder) {
1357 int Offset = SignExtend32<16>(Insn & 0xffff);
1358 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1359 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1360
1361 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1362 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1363
1364 Inst.addOperand(MCOperand::CreateReg(Reg));
1365 Inst.addOperand(MCOperand::CreateReg(Base));
1366 Inst.addOperand(MCOperand::CreateImm(Offset));
1367
1368 return MCDisassembler::Success;
1369}
1370
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001371static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1372 unsigned Insn,
1373 uint64_t Address,
1374 const void *Decoder) {
1375 int Offset = SignExtend32<11>(Insn & 0x07ff);
1376 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1377 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1378
1379 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1380 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1381
1382 Inst.addOperand(MCOperand::CreateReg(Reg));
1383 Inst.addOperand(MCOperand::CreateReg(Base));
1384 Inst.addOperand(MCOperand::CreateImm(Offset));
1385
1386 return MCDisassembler::Success;
1387}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001388static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1389 unsigned Insn,
1390 uint64_t Address,
1391 const void *Decoder) {
1392 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1393 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1394 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1395
1396 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1397 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1398
1399 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1400 Inst.addOperand(MCOperand::CreateReg(Rt));
1401 }
1402
1403 Inst.addOperand(MCOperand::CreateReg(Rt));
1404 Inst.addOperand(MCOperand::CreateReg(Base));
1405 Inst.addOperand(MCOperand::CreateImm(Offset));
1406
1407 return MCDisassembler::Success;
1408}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001409
1410static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1411 unsigned RegNo,
1412 uint64_t Address,
1413 const void *Decoder) {
1414 // Currently only hardware register 29 is supported.
1415 if (RegNo != 29)
1416 return MCDisassembler::Fail;
1417 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1418 return MCDisassembler::Success;
1419}
1420
Akira Hatanaka71928e62012-04-17 18:03:21 +00001421static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1422 unsigned RegNo,
1423 uint64_t Address,
1424 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001425 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001426 return MCDisassembler::Fail;
1427
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001428 ;
1429 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1430 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001431 return MCDisassembler::Success;
1432}
1433
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001434static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1435 unsigned RegNo,
1436 uint64_t Address,
1437 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001438 if (RegNo >= 4)
1439 return MCDisassembler::Fail;
1440
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001441 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001442 Inst.addOperand(MCOperand::CreateReg(Reg));
1443 return MCDisassembler::Success;
1444}
1445
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001446static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1447 unsigned RegNo,
1448 uint64_t Address,
1449 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001450 if (RegNo >= 4)
1451 return MCDisassembler::Fail;
1452
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001453 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001454 Inst.addOperand(MCOperand::CreateReg(Reg));
1455 return MCDisassembler::Success;
1456}
1457
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001458static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1459 unsigned RegNo,
1460 uint64_t Address,
1461 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001462 if (RegNo >= 4)
1463 return MCDisassembler::Fail;
1464
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001465 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001466 Inst.addOperand(MCOperand::CreateReg(Reg));
1467 return MCDisassembler::Success;
1468}
1469
Jack Carter3eb663b2013-09-26 00:09:46 +00001470static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1471 unsigned RegNo,
1472 uint64_t Address,
1473 const void *Decoder) {
1474 if (RegNo > 31)
1475 return MCDisassembler::Fail;
1476
1477 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1478 Inst.addOperand(MCOperand::CreateReg(Reg));
1479 return MCDisassembler::Success;
1480}
1481
Jack Carter5dc8ac92013-09-25 23:50:44 +00001482static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1483 unsigned RegNo,
1484 uint64_t Address,
1485 const void *Decoder) {
1486 if (RegNo > 31)
1487 return MCDisassembler::Fail;
1488
1489 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1490 Inst.addOperand(MCOperand::CreateReg(Reg));
1491 return MCDisassembler::Success;
1492}
1493
1494static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1495 unsigned RegNo,
1496 uint64_t Address,
1497 const void *Decoder) {
1498 if (RegNo > 31)
1499 return MCDisassembler::Fail;
1500
1501 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1502 Inst.addOperand(MCOperand::CreateReg(Reg));
1503 return MCDisassembler::Success;
1504}
1505
1506static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1507 unsigned RegNo,
1508 uint64_t Address,
1509 const void *Decoder) {
1510 if (RegNo > 31)
1511 return MCDisassembler::Fail;
1512
1513 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1514 Inst.addOperand(MCOperand::CreateReg(Reg));
1515 return MCDisassembler::Success;
1516}
1517
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001518static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1519 unsigned RegNo,
1520 uint64_t Address,
1521 const void *Decoder) {
1522 if (RegNo > 7)
1523 return MCDisassembler::Fail;
1524
1525 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1526 Inst.addOperand(MCOperand::CreateReg(Reg));
1527 return MCDisassembler::Success;
1528}
1529
Daniel Sanders2a83d682014-05-21 12:56:39 +00001530static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1531 unsigned RegNo,
1532 uint64_t Address,
1533 const void *Decoder) {
1534 if (RegNo > 31)
1535 return MCDisassembler::Fail;
1536
1537 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1538 Inst.addOperand(MCOperand::CreateReg(Reg));
1539 return MCDisassembler::Success;
1540}
1541
Akira Hatanaka71928e62012-04-17 18:03:21 +00001542static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1543 unsigned Offset,
1544 uint64_t Address,
1545 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001546 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001547 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1548 return MCDisassembler::Success;
1549}
1550
Akira Hatanaka71928e62012-04-17 18:03:21 +00001551static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1552 unsigned Insn,
1553 uint64_t Address,
1554 const void *Decoder) {
1555
Jim Grosbachecaef492012-08-14 19:06:05 +00001556 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001557 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1558 return MCDisassembler::Success;
1559}
1560
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001561static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1562 unsigned Offset,
1563 uint64_t Address,
1564 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001565 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001566
1567 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1568 return MCDisassembler::Success;
1569}
1570
1571static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1572 unsigned Offset,
1573 uint64_t Address,
1574 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001575 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001576
1577 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1578 return MCDisassembler::Success;
1579}
1580
Jozef Kolek9761e962015-01-12 12:03:34 +00001581static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1582 unsigned Offset,
1583 uint64_t Address,
1584 const void *Decoder) {
1585 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1586 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1587 return MCDisassembler::Success;
1588}
1589
Jozef Kolek5cfebdd2015-01-21 12:39:30 +00001590static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1591 unsigned Offset,
1592 uint64_t Address,
1593 const void *Decoder) {
1594 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1595 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1596 return MCDisassembler::Success;
1597}
1598
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001599static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1600 unsigned Offset,
1601 uint64_t Address,
1602 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001603 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001604 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1605 return MCDisassembler::Success;
1606}
1607
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001608static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1609 unsigned Insn,
1610 uint64_t Address,
1611 const void *Decoder) {
1612 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1613 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1614 return MCDisassembler::Success;
1615}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001616
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001617static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1618 unsigned Value,
1619 uint64_t Address,
1620 const void *Decoder) {
1621 if (Value == 0)
1622 Inst.addOperand(MCOperand::CreateImm(1));
1623 else if (Value == 0x7)
1624 Inst.addOperand(MCOperand::CreateImm(-1));
1625 else
1626 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1627 return MCDisassembler::Success;
1628}
1629
1630static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1631 unsigned Value,
1632 uint64_t Address,
1633 const void *Decoder) {
1634 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1635 return MCDisassembler::Success;
1636}
1637
1638static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1639 unsigned Value,
1640 uint64_t Address,
1641 const void *Decoder) {
1642 if (Value == 0x7F)
1643 Inst.addOperand(MCOperand::CreateImm(-1));
1644 else
1645 Inst.addOperand(MCOperand::CreateImm(Value));
1646 return MCDisassembler::Success;
1647}
1648
1649static DecodeStatus DecodeSimm4(MCInst &Inst,
1650 unsigned Value,
1651 uint64_t Address,
1652 const void *Decoder) {
1653 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1654 return MCDisassembler::Success;
1655}
1656
Akira Hatanaka71928e62012-04-17 18:03:21 +00001657static DecodeStatus DecodeSimm16(MCInst &Inst,
1658 unsigned Insn,
1659 uint64_t Address,
1660 const void *Decoder) {
1661 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1662 return MCDisassembler::Success;
1663}
1664
Matheus Almeida779c5932013-11-18 12:32:49 +00001665static DecodeStatus DecodeLSAImm(MCInst &Inst,
1666 unsigned Insn,
1667 uint64_t Address,
1668 const void *Decoder) {
1669 // We add one to the immediate field as it was encoded as 'imm - 1'.
1670 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1671 return MCDisassembler::Success;
1672}
1673
Akira Hatanaka71928e62012-04-17 18:03:21 +00001674static DecodeStatus DecodeInsSize(MCInst &Inst,
1675 unsigned Insn,
1676 uint64_t Address,
1677 const void *Decoder) {
1678 // First we need to grab the pos(lsb) from MCInst.
1679 int Pos = Inst.getOperand(2).getImm();
1680 int Size = (int) Insn - Pos + 1;
1681 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1682 return MCDisassembler::Success;
1683}
1684
1685static DecodeStatus DecodeExtSize(MCInst &Inst,
1686 unsigned Insn,
1687 uint64_t Address,
1688 const void *Decoder) {
1689 int Size = (int) Insn + 1;
1690 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1691 return MCDisassembler::Success;
1692}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001693
1694static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1695 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001696 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001697 return MCDisassembler::Success;
1698}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001699
1700static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1701 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001702 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001703 return MCDisassembler::Success;
1704}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001705
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001706static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1707 uint64_t Address, const void *Decoder) {
1708 int32_t DecodedValue;
1709 switch (Insn) {
1710 case 0: DecodedValue = 256; break;
1711 case 1: DecodedValue = 257; break;
1712 case 510: DecodedValue = -258; break;
1713 case 511: DecodedValue = -257; break;
1714 default: DecodedValue = SignExtend32<9>(Insn); break;
1715 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001716 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001717 return MCDisassembler::Success;
1718}
1719
1720static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1721 uint64_t Address, const void *Decoder) {
1722 // Insn must be >= 0, since it is unsigned that condition is always true.
1723 assert(Insn < 16);
1724 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1725 255, 32768, 65535};
1726 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1727 return MCDisassembler::Success;
1728}
1729
1730static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1731 uint64_t Address, const void *Decoder) {
1732 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1733 return MCDisassembler::Success;
1734}
1735
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001736static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1737 unsigned Insn,
1738 uint64_t Address,
1739 const void *Decoder) {
1740 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1741 Mips::S6, Mips::FP};
1742 unsigned RegNum;
1743
1744 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1745 // Empty register lists are not allowed.
1746 if (RegLst == 0)
1747 return MCDisassembler::Fail;
1748
1749 RegNum = RegLst & 0xf;
1750 for (unsigned i = 0; i < RegNum; i++)
1751 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1752
1753 if (RegLst & 0x10)
1754 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1755
1756 return MCDisassembler::Success;
1757}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001758
1759static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1760 uint64_t Address,
1761 const void *Decoder) {
1762 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1763 unsigned RegNum;
1764
1765 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1766 // Empty register lists are not allowed.
1767 if (RegLst == 0)
1768 return MCDisassembler::Fail;
1769
1770 RegNum = RegLst & 0x3;
1771 for (unsigned i = 0; i < RegNum - 1; i++)
1772 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1773
1774 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1775
1776 return MCDisassembler::Success;
1777}
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001778
1779static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1780 uint64_t Address, const void *Decoder) {
1781 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1782 return MCDisassembler::Success;
1783}