blob: 267f2d41c1adda7c742eb73062a08f042b3dbc75 [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
Alexei Starovoitov4ea2f602015-01-23 21:00:08 +000033/// A disassembler 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
Alexei Starovoitov4ea2f602015-01-23 21:00:08 +000052/// A disassembler 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
Alexei Starovoitov4ea2f602015-01-23 21:00:08 +000080/// A disassembler 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
Vladimir Medicdf464ae2015-01-29 11:33:41 +0000269static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
270 unsigned Insn,
271 uint64_t Address,
272 const void *Decoder);
273
Jozef Kolekab6d1cc2014-12-23 19:55:34 +0000274static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
275 unsigned Insn,
276 uint64_t Address,
277 const void *Decoder);
278
Daniel Sandersb4484d62014-11-27 17:28:10 +0000279static DecodeStatus DecodeSyncI(MCInst &Inst,
280 unsigned Insn,
281 uint64_t Address,
282 const void *Decoder);
283
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000284static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
285 uint64_t Address, const void *Decoder);
286
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000287static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
288 unsigned Insn,
289 uint64_t Address,
290 const void *Decoder);
291
Jozef Kolek12c69822014-12-23 16:16:33 +0000292static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
293 unsigned Insn,
294 uint64_t Address,
295 const void *Decoder);
296
Jozef Koleke10a02e2015-01-28 17:27:26 +0000297static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
298 unsigned Insn,
299 uint64_t Address,
300 const void *Decoder);
301
Jozef Kolekd68d424a2015-02-10 12:41:13 +0000302static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
303 unsigned Insn,
304 uint64_t Address,
305 const void *Decoder);
306
Vladimir Medicdde3d582013-09-06 12:30:36 +0000307static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
308 unsigned Insn,
309 uint64_t Address,
310 const void *Decoder);
311
312static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
313 unsigned Insn,
314 uint64_t Address,
315 const void *Decoder);
316
Akira Hatanaka71928e62012-04-17 18:03:21 +0000317static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
318 uint64_t Address,
319 const void *Decoder);
320
Daniel Sanders92db6b72014-10-01 08:26:55 +0000321static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
322 uint64_t Address,
323 const void *Decoder);
324
325static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
326 uint64_t Address,
327 const void *Decoder);
328
Vladimir Medic435cf8a2015-01-21 10:47:36 +0000329static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
330 uint64_t Address,
331 const void *Decoder);
332
Daniel Sanders6a803f62014-06-16 13:13:03 +0000333static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
334 unsigned Insn,
335 uint64_t Address,
336 const void *Decoder);
337
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000338static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
339 unsigned Value,
340 uint64_t Address,
341 const void *Decoder);
342
343static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
344 unsigned Value,
345 uint64_t Address,
346 const void *Decoder);
347
348static DecodeStatus DecodeLiSimm7(MCInst &Inst,
349 unsigned Value,
350 uint64_t Address,
351 const void *Decoder);
352
353static DecodeStatus DecodeSimm4(MCInst &Inst,
354 unsigned Value,
355 uint64_t Address,
356 const void *Decoder);
357
Akira Hatanaka71928e62012-04-17 18:03:21 +0000358static DecodeStatus DecodeSimm16(MCInst &Inst,
359 unsigned Insn,
360 uint64_t Address,
361 const void *Decoder);
362
Matheus Almeida779c5932013-11-18 12:32:49 +0000363// Decode the immediate field of an LSA instruction which
364// is off by one.
365static DecodeStatus DecodeLSAImm(MCInst &Inst,
366 unsigned Insn,
367 uint64_t Address,
368 const void *Decoder);
369
Akira Hatanaka71928e62012-04-17 18:03:21 +0000370static DecodeStatus DecodeInsSize(MCInst &Inst,
371 unsigned Insn,
372 uint64_t Address,
373 const void *Decoder);
374
375static DecodeStatus DecodeExtSize(MCInst &Inst,
376 unsigned Insn,
377 uint64_t Address,
378 const void *Decoder);
379
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000380static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
381 uint64_t Address, const void *Decoder);
382
Zoran Jovanovic28551422014-06-09 09:49:51 +0000383static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
384 uint64_t Address, const void *Decoder);
385
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000386static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
387 uint64_t Address, const void *Decoder);
388
389static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
390 uint64_t Address, const void *Decoder);
391
392static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
393 uint64_t Address, const void *Decoder);
394
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000395static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
396 uint64_t Address, const void *Decoder);
397
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000398/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
399/// handle.
400template <typename InsnType>
401static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000403
404template <typename InsnType>
405static DecodeStatus
406DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
408
409template <typename InsnType>
410static DecodeStatus
411DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
413
414template <typename InsnType>
415static DecodeStatus
416DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
418
419template <typename InsnType>
420static DecodeStatus
421DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
422 const void *Decoder);
423
424template <typename InsnType>
425static DecodeStatus
426DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
427 const void *Decoder);
428
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000429template <typename InsnType>
430static DecodeStatus
431DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
432 const void *Decoder);
433
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000434static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
435 uint64_t Address,
436 const void *Decoder);
437
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000438static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
439 uint64_t Address,
440 const void *Decoder);
441
Akira Hatanaka71928e62012-04-17 18:03:21 +0000442namespace llvm {
443extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
444 TheMips64elTarget;
445}
446
447static MCDisassembler *createMipsDisassembler(
448 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000449 const MCSubtargetInfo &STI,
450 MCContext &Ctx) {
451 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000452}
453
454static MCDisassembler *createMipselDisassembler(
455 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000456 const MCSubtargetInfo &STI,
457 MCContext &Ctx) {
458 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000459}
460
461static MCDisassembler *createMips64Disassembler(
462 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000463 const MCSubtargetInfo &STI,
464 MCContext &Ctx) {
465 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000466}
467
468static MCDisassembler *createMips64elDisassembler(
469 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000470 const MCSubtargetInfo &STI,
471 MCContext &Ctx) {
472 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000473}
474
475extern "C" void LLVMInitializeMipsDisassembler() {
476 // Register the disassembler.
477 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
478 createMipsDisassembler);
479 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
480 createMipselDisassembler);
481 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
482 createMips64Disassembler);
483 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
484 createMips64elDisassembler);
485}
486
Akira Hatanaka71928e62012-04-17 18:03:21 +0000487#include "MipsGenDisassemblerTables.inc"
488
Daniel Sanders5c582b22014-05-22 11:23:21 +0000489static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
490 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
491 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
492 return *(RegInfo->getRegClass(RC).begin() + RegNo);
493}
494
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000495template <typename InsnType>
496static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
497 const void *Decoder) {
498 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
499 // The size of the n field depends on the element size
500 // The register class also depends on this.
501 InsnType tmp = fieldFromInstruction(insn, 17, 5);
502 unsigned NSize = 0;
503 DecodeFN RegDecoder = nullptr;
504 if ((tmp & 0x18) == 0x00) { // INSVE_B
505 NSize = 4;
506 RegDecoder = DecodeMSA128BRegisterClass;
507 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
508 NSize = 3;
509 RegDecoder = DecodeMSA128HRegisterClass;
510 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
511 NSize = 2;
512 RegDecoder = DecodeMSA128WRegisterClass;
513 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
514 NSize = 1;
515 RegDecoder = DecodeMSA128DRegisterClass;
516 } else
517 llvm_unreachable("Invalid encoding");
518
519 assert(NSize != 0 && RegDecoder != nullptr);
520
521 // $wd
522 tmp = fieldFromInstruction(insn, 6, 5);
523 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
524 return MCDisassembler::Fail;
525 // $wd_in
526 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
527 return MCDisassembler::Fail;
528 // $n
529 tmp = fieldFromInstruction(insn, 16, NSize);
530 MI.addOperand(MCOperand::CreateImm(tmp));
531 // $ws
532 tmp = fieldFromInstruction(insn, 11, 5);
533 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
534 return MCDisassembler::Fail;
535 // $n2
536 MI.addOperand(MCOperand::CreateImm(0));
537
538 return MCDisassembler::Success;
539}
540
Daniel Sanders5c582b22014-05-22 11:23:21 +0000541template <typename InsnType>
542static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
543 uint64_t Address,
544 const void *Decoder) {
545 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
546 // (otherwise we would have matched the ADDI instruction from the earlier
547 // ISA's instead).
548 //
549 // We have:
550 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
551 // BOVC if rs >= rt
552 // BEQZALC if rs == 0 && rt != 0
553 // BEQC if rs < rt && rs != 0
554
555 InsnType Rs = fieldFromInstruction(insn, 21, 5);
556 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000557 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000558 bool HasRs = false;
559
560 if (Rs >= Rt) {
561 MI.setOpcode(Mips::BOVC);
562 HasRs = true;
563 } else if (Rs != 0 && Rs < Rt) {
564 MI.setOpcode(Mips::BEQC);
565 HasRs = true;
566 } else
567 MI.setOpcode(Mips::BEQZALC);
568
569 if (HasRs)
570 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
571 Rs)));
572
573 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
574 Rt)));
575 MI.addOperand(MCOperand::CreateImm(Imm));
576
577 return MCDisassembler::Success;
578}
579
580template <typename InsnType>
581static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
582 uint64_t Address,
583 const void *Decoder) {
584 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
585 // (otherwise we would have matched the ADDI instruction from the earlier
586 // ISA's instead).
587 //
588 // We have:
589 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
590 // BNVC if rs >= rt
591 // BNEZALC if rs == 0 && rt != 0
592 // BNEC if rs < rt && rs != 0
593
594 InsnType Rs = fieldFromInstruction(insn, 21, 5);
595 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000596 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000597 bool HasRs = false;
598
599 if (Rs >= Rt) {
600 MI.setOpcode(Mips::BNVC);
601 HasRs = true;
602 } else if (Rs != 0 && Rs < Rt) {
603 MI.setOpcode(Mips::BNEC);
604 HasRs = true;
605 } else
606 MI.setOpcode(Mips::BNEZALC);
607
608 if (HasRs)
609 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
610 Rs)));
611
612 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
613 Rt)));
614 MI.addOperand(MCOperand::CreateImm(Imm));
615
616 return MCDisassembler::Success;
617}
618
619template <typename InsnType>
620static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
621 uint64_t Address,
622 const void *Decoder) {
623 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
624 // (otherwise we would have matched the BLEZL instruction from the earlier
625 // ISA's instead).
626 //
627 // We have:
628 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
629 // Invalid if rs == 0
630 // BLEZC if rs == 0 && rt != 0
631 // BGEZC if rs == rt && rt != 0
632 // BGEC if rs != rt && rs != 0 && rt != 0
633
634 InsnType Rs = fieldFromInstruction(insn, 21, 5);
635 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000636 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000637 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000638
639 if (Rt == 0)
640 return MCDisassembler::Fail;
641 else if (Rs == 0)
642 MI.setOpcode(Mips::BLEZC);
643 else if (Rs == Rt)
644 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000645 else {
646 HasRs = true;
647 MI.setOpcode(Mips::BGEC);
648 }
649
650 if (HasRs)
651 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
652 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000653
654 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
655 Rt)));
656
657 MI.addOperand(MCOperand::CreateImm(Imm));
658
659 return MCDisassembler::Success;
660}
661
662template <typename InsnType>
663static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
664 uint64_t Address,
665 const void *Decoder) {
666 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
667 // (otherwise we would have matched the BGTZL instruction from the earlier
668 // ISA's instead).
669 //
670 // We have:
671 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
672 // Invalid if rs == 0
673 // BGTZC if rs == 0 && rt != 0
674 // BLTZC if rs == rt && rt != 0
675 // BLTC if rs != rt && rs != 0 && rt != 0
676
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000677 bool HasRs = false;
678
Daniel Sanders5c582b22014-05-22 11:23:21 +0000679 InsnType Rs = fieldFromInstruction(insn, 21, 5);
680 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000681 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000682
683 if (Rt == 0)
684 return MCDisassembler::Fail;
685 else if (Rs == 0)
686 MI.setOpcode(Mips::BGTZC);
687 else if (Rs == Rt)
688 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000689 else {
690 MI.setOpcode(Mips::BLTC);
691 HasRs = true;
692 }
693
694 if (HasRs)
695 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
696 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000697
698 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
699 Rt)));
700
701 MI.addOperand(MCOperand::CreateImm(Imm));
702
703 return MCDisassembler::Success;
704}
705
706template <typename InsnType>
707static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
708 uint64_t Address,
709 const void *Decoder) {
710 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
711 // (otherwise we would have matched the BGTZ instruction from the earlier
712 // ISA's instead).
713 //
714 // We have:
715 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
716 // BGTZ if rt == 0
717 // BGTZALC if rs == 0 && rt != 0
718 // BLTZALC if rs != 0 && rs == rt
719 // BLTUC if rs != 0 && rs != rt
720
721 InsnType Rs = fieldFromInstruction(insn, 21, 5);
722 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000723 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000724 bool HasRs = false;
725 bool HasRt = false;
726
727 if (Rt == 0) {
728 MI.setOpcode(Mips::BGTZ);
729 HasRs = true;
730 } else if (Rs == 0) {
731 MI.setOpcode(Mips::BGTZALC);
732 HasRt = true;
733 } else if (Rs == Rt) {
734 MI.setOpcode(Mips::BLTZALC);
735 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000736 } else {
737 MI.setOpcode(Mips::BLTUC);
738 HasRs = true;
739 HasRt = true;
740 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000741
742 if (HasRs)
743 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
744 Rs)));
745
746 if (HasRt)
747 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
748 Rt)));
749
750 MI.addOperand(MCOperand::CreateImm(Imm));
751
752 return MCDisassembler::Success;
753}
754
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000755template <typename InsnType>
756static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
757 uint64_t Address,
758 const void *Decoder) {
759 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
760 // (otherwise we would have matched the BLEZL instruction from the earlier
761 // ISA's instead).
762 //
763 // We have:
764 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
765 // Invalid if rs == 0
766 // BLEZALC if rs == 0 && rt != 0
767 // BGEZALC if rs == rt && rt != 0
768 // BGEUC if rs != rt && rs != 0 && rt != 0
769
770 InsnType Rs = fieldFromInstruction(insn, 21, 5);
771 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000772 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000773 bool HasRs = false;
774
775 if (Rt == 0)
776 return MCDisassembler::Fail;
777 else if (Rs == 0)
778 MI.setOpcode(Mips::BLEZALC);
779 else if (Rs == Rt)
780 MI.setOpcode(Mips::BGEZALC);
781 else {
782 HasRs = true;
783 MI.setOpcode(Mips::BGEUC);
784 }
785
786 if (HasRs)
787 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
788 Rs)));
789 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
790 Rt)));
791
792 MI.addOperand(MCOperand::CreateImm(Imm));
793
794 return MCDisassembler::Success;
795}
796
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000797/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
798/// according to the given endianess.
799static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
800 uint64_t &Size, uint32_t &Insn,
801 bool IsBigEndian) {
802 // We want to read exactly 2 Bytes of data.
803 if (Bytes.size() < 2) {
804 Size = 0;
805 return MCDisassembler::Fail;
806 }
807
808 if (IsBigEndian) {
809 Insn = (Bytes[0] << 8) | Bytes[1];
810 } else {
811 Insn = (Bytes[1] << 8) | Bytes[0];
812 }
813
814 return MCDisassembler::Success;
815}
816
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000817/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000818/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000819static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
820 uint64_t &Size, uint32_t &Insn,
821 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000822 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000823 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000824 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000825 return MCDisassembler::Fail;
826 }
827
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000828 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
829 // always precede the low 16 bits in the instruction stream (that is, they
830 // are placed at lower addresses in the instruction stream).
831 //
832 // microMIPS byte ordering:
833 // Big-endian: 0 | 1 | 2 | 3
834 // Little-endian: 1 | 0 | 3 | 2
835
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000836 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000837 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000838 Insn =
839 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
840 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000841 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000842 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000843 (Bytes[1] << 24);
844 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000845 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000846 (Bytes[3] << 24);
847 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000848 }
849
850 return MCDisassembler::Success;
851}
852
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000853DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000854 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000855 uint64_t Address,
856 raw_ostream &VStream,
857 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000858 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000859 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000860
Vladimir Medicdde3d582013-09-06 12:30:36 +0000861 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000862 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
863
864 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
865 // Calling the auto-generated decoder function.
866 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
867 this, STI);
868 if (Result != MCDisassembler::Fail) {
869 Size = 2;
870 return Result;
871 }
872
873 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
874 if (Result == MCDisassembler::Fail)
875 return MCDisassembler::Fail;
876
877 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000878 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000879 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000880 this, STI);
881 if (Result != MCDisassembler::Fail) {
882 Size = 4;
883 return Result;
884 }
885 return MCDisassembler::Fail;
886 }
887
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000888 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
889 if (Result == MCDisassembler::Fail)
890 return MCDisassembler::Fail;
891
Daniel Sandersc171f652014-06-13 13:15:59 +0000892 if (hasCOP3()) {
893 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
894 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000895 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000896 if (Result != MCDisassembler::Fail) {
897 Size = 4;
898 return Result;
899 }
900 }
901
902 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000903 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000904 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000905 Address, this, STI);
906 if (Result != MCDisassembler::Fail) {
907 Size = 4;
908 return Result;
909 }
910 }
911
Daniel Sandersc171f652014-06-13 13:15:59 +0000912 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000913 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000914 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000915 Address, this, STI);
916 if (Result != MCDisassembler::Fail) {
917 Size = 4;
918 return Result;
919 }
920 }
921
Daniel Sanders0fa60412014-06-12 13:39:06 +0000922 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000923 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000924 Result =
925 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000926 if (Result != MCDisassembler::Fail) {
927 Size = 4;
928 return Result;
929 }
930
931 return MCDisassembler::Fail;
932}
933
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000934DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000935 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000936 uint64_t Address,
937 raw_ostream &VStream,
938 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000939 uint32_t Insn;
940
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000941 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000942 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000943 if (Result == MCDisassembler::Fail)
944 return MCDisassembler::Fail;
945
946 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000947 Result =
948 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000949 if (Result != MCDisassembler::Fail) {
950 Size = 4;
951 return Result;
952 }
953 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000954 Result =
955 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000956 if (Result != MCDisassembler::Fail) {
957 Size = 4;
958 return Result;
959 }
960
961 return MCDisassembler::Fail;
962}
963
Reed Kotlerec8a5492013-02-14 03:05:25 +0000964static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
965 unsigned RegNo,
966 uint64_t Address,
967 const void *Decoder) {
968
969 return MCDisassembler::Fail;
970
971}
972
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000973static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
974 unsigned RegNo,
975 uint64_t Address,
976 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000977
978 if (RegNo > 31)
979 return MCDisassembler::Fail;
980
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000981 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000982 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000983 return MCDisassembler::Success;
984}
985
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000986static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
987 unsigned RegNo,
988 uint64_t Address,
989 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000990 if (RegNo > 7)
991 return MCDisassembler::Fail;
992 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
993 Inst.addOperand(MCOperand::CreateReg(Reg));
994 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000995}
996
Jozef Kolek1904fa22014-11-24 14:25:53 +0000997static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
998 unsigned RegNo,
999 uint64_t Address,
1000 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001001 if (RegNo > 7)
1002 return MCDisassembler::Fail;
1003 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
1004 Inst.addOperand(MCOperand::CreateReg(Reg));
1005 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +00001006}
1007
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001008static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1009 unsigned RegNo,
1010 uint64_t Address,
1011 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +00001012 if (RegNo > 31)
1013 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001014 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001015 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001016 return MCDisassembler::Success;
1017}
1018
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001019static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1020 unsigned RegNo,
1021 uint64_t Address,
1022 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +00001023 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001024 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1025
1026 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1027}
1028
Akira Hatanaka654655f2013-08-14 00:53:38 +00001029static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1030 unsigned RegNo,
1031 uint64_t Address,
1032 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001033 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001034}
1035
Akira Hatanaka71928e62012-04-17 18:03:21 +00001036static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1037 unsigned RegNo,
1038 uint64_t Address,
1039 const void *Decoder) {
1040 if (RegNo > 31)
1041 return MCDisassembler::Fail;
1042
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001043 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1044 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001045 return MCDisassembler::Success;
1046}
1047
1048static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1049 unsigned RegNo,
1050 uint64_t Address,
1051 const void *Decoder) {
1052 if (RegNo > 31)
1053 return MCDisassembler::Fail;
1054
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001055 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1056 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001057 return MCDisassembler::Success;
1058}
1059
1060static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1061 unsigned RegNo,
1062 uint64_t Address,
1063 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001064 if (RegNo > 31)
1065 return MCDisassembler::Fail;
1066 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1067 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001068 return MCDisassembler::Success;
1069}
1070
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001071static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1072 unsigned RegNo,
1073 uint64_t Address,
1074 const void *Decoder) {
1075 if (RegNo > 7)
1076 return MCDisassembler::Fail;
1077 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1078 Inst.addOperand(MCOperand::CreateReg(Reg));
1079 return MCDisassembler::Success;
1080}
1081
Daniel Sanders0fa60412014-06-12 13:39:06 +00001082static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1083 uint64_t Address,
1084 const void *Decoder) {
1085 if (RegNo > 31)
1086 return MCDisassembler::Fail;
1087
1088 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1089 Inst.addOperand(MCOperand::CreateReg(Reg));
1090 return MCDisassembler::Success;
1091}
1092
Akira Hatanaka71928e62012-04-17 18:03:21 +00001093static DecodeStatus DecodeMem(MCInst &Inst,
1094 unsigned Insn,
1095 uint64_t Address,
1096 const void *Decoder) {
1097 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001098 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1099 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001100
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001101 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1102 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001103
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001104 if(Inst.getOpcode() == Mips::SC ||
1105 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001106 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001107 }
1108
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001109 Inst.addOperand(MCOperand::CreateReg(Reg));
1110 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001111 Inst.addOperand(MCOperand::CreateImm(Offset));
1112
1113 return MCDisassembler::Success;
1114}
1115
Daniel Sanders92db6b72014-10-01 08:26:55 +00001116static DecodeStatus DecodeCacheOp(MCInst &Inst,
1117 unsigned Insn,
1118 uint64_t Address,
1119 const void *Decoder) {
1120 int Offset = SignExtend32<16>(Insn & 0xffff);
1121 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1122 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1123
1124 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1125
1126 Inst.addOperand(MCOperand::CreateReg(Base));
1127 Inst.addOperand(MCOperand::CreateImm(Offset));
1128 Inst.addOperand(MCOperand::CreateImm(Hint));
1129
1130 return MCDisassembler::Success;
1131}
1132
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001133static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1134 unsigned Insn,
1135 uint64_t Address,
1136 const void *Decoder) {
1137 int Offset = SignExtend32<12>(Insn & 0xfff);
1138 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1139 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1140
1141 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1142
1143 Inst.addOperand(MCOperand::CreateReg(Base));
1144 Inst.addOperand(MCOperand::CreateImm(Offset));
1145 Inst.addOperand(MCOperand::CreateImm(Hint));
1146
1147 return MCDisassembler::Success;
1148}
1149
Vladimir Medicdf464ae2015-01-29 11:33:41 +00001150static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
1151 unsigned Insn,
1152 uint64_t Address,
1153 const void *Decoder) {
1154 int Offset = fieldFromInstruction(Insn, 7, 9);
1155 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1156 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1157
1158 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1159
1160 Inst.addOperand(MCOperand::CreateReg(Base));
1161 Inst.addOperand(MCOperand::CreateImm(Offset));
1162 Inst.addOperand(MCOperand::CreateImm(Hint));
1163
1164 return MCDisassembler::Success;
1165}
1166
Daniel Sandersb4484d62014-11-27 17:28:10 +00001167static DecodeStatus DecodeSyncI(MCInst &Inst,
1168 unsigned Insn,
1169 uint64_t Address,
1170 const void *Decoder) {
1171 int Offset = SignExtend32<16>(Insn & 0xffff);
1172 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1173
1174 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1175
1176 Inst.addOperand(MCOperand::CreateReg(Base));
1177 Inst.addOperand(MCOperand::CreateImm(Offset));
1178
1179 return MCDisassembler::Success;
1180}
1181
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001182static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1183 uint64_t Address, const void *Decoder) {
1184 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1185 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1186 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1187
1188 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1189 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1190
1191 Inst.addOperand(MCOperand::CreateReg(Reg));
1192 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001193
1194 // The immediate field of an LD/ST instruction is scaled which means it must
1195 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1196 // data format.
1197 // .b - 1 byte
1198 // .h - 2 bytes
1199 // .w - 4 bytes
1200 // .d - 8 bytes
1201 switch(Inst.getOpcode())
1202 {
1203 default:
1204 assert (0 && "Unexpected instruction");
1205 return MCDisassembler::Fail;
1206 break;
1207 case Mips::LD_B:
1208 case Mips::ST_B:
1209 Inst.addOperand(MCOperand::CreateImm(Offset));
1210 break;
1211 case Mips::LD_H:
1212 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001213 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001214 break;
1215 case Mips::LD_W:
1216 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001217 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001218 break;
1219 case Mips::LD_D:
1220 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001221 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001222 break;
1223 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001224
1225 return MCDisassembler::Success;
1226}
1227
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001228static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1229 unsigned Insn,
1230 uint64_t Address,
1231 const void *Decoder) {
1232 unsigned Offset = Insn & 0xf;
1233 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1234 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1235
1236 switch (Inst.getOpcode()) {
1237 case Mips::LBU16_MM:
1238 case Mips::LHU16_MM:
1239 case Mips::LW16_MM:
1240 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1241 == MCDisassembler::Fail)
1242 return MCDisassembler::Fail;
1243 break;
1244 case Mips::SB16_MM:
1245 case Mips::SH16_MM:
1246 case Mips::SW16_MM:
1247 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1248 == MCDisassembler::Fail)
1249 return MCDisassembler::Fail;
1250 break;
1251 }
1252
1253 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1254 == MCDisassembler::Fail)
1255 return MCDisassembler::Fail;
1256
1257 switch (Inst.getOpcode()) {
1258 case Mips::LBU16_MM:
1259 if (Offset == 0xf)
1260 Inst.addOperand(MCOperand::CreateImm(-1));
1261 else
1262 Inst.addOperand(MCOperand::CreateImm(Offset));
1263 break;
1264 case Mips::SB16_MM:
1265 Inst.addOperand(MCOperand::CreateImm(Offset));
1266 break;
1267 case Mips::LHU16_MM:
1268 case Mips::SH16_MM:
1269 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1270 break;
1271 case Mips::LW16_MM:
1272 case Mips::SW16_MM:
1273 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1274 break;
1275 }
1276
1277 return MCDisassembler::Success;
1278}
1279
Jozef Kolek12c69822014-12-23 16:16:33 +00001280static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1281 unsigned Insn,
1282 uint64_t Address,
1283 const void *Decoder) {
1284 unsigned Offset = Insn & 0x1F;
1285 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1286
1287 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1288
1289 Inst.addOperand(MCOperand::CreateReg(Reg));
1290 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1291 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1292
1293 return MCDisassembler::Success;
1294}
1295
Jozef Koleke10a02e2015-01-28 17:27:26 +00001296static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1297 unsigned Insn,
1298 uint64_t Address,
1299 const void *Decoder) {
1300 unsigned Offset = Insn & 0x7F;
1301 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1302
1303 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1304
1305 Inst.addOperand(MCOperand::CreateReg(Reg));
1306 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1307 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1308
1309 return MCDisassembler::Success;
1310}
1311
Jozef Kolekd68d424a2015-02-10 12:41:13 +00001312static DecodeStatus DecodeMemMMReglistImm4Lsl2(MCInst &Inst,
1313 unsigned Insn,
1314 uint64_t Address,
1315 const void *Decoder) {
1316 int Offset = SignExtend32<4>(Insn & 0xf);
1317
1318 if (DecodeRegListOperand16(Inst, Insn, Address, Decoder)
1319 == MCDisassembler::Fail)
1320 return MCDisassembler::Fail;
1321
1322 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1323 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1324
1325 return MCDisassembler::Success;
1326}
1327
Vladimir Medicdde3d582013-09-06 12:30:36 +00001328static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1329 unsigned Insn,
1330 uint64_t Address,
1331 const void *Decoder) {
1332 int Offset = SignExtend32<12>(Insn & 0x0fff);
1333 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1334 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1335
1336 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1337 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1338
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001339 switch (Inst.getOpcode()) {
1340 case Mips::SWM32_MM:
1341 case Mips::LWM32_MM:
1342 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1343 == MCDisassembler::Fail)
1344 return MCDisassembler::Fail;
1345 Inst.addOperand(MCOperand::CreateReg(Base));
1346 Inst.addOperand(MCOperand::CreateImm(Offset));
1347 break;
1348 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001349 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001350 // fallthrough
1351 default:
1352 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001353 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1354 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1355
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001356 Inst.addOperand(MCOperand::CreateReg(Base));
1357 Inst.addOperand(MCOperand::CreateImm(Offset));
1358 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001359
1360 return MCDisassembler::Success;
1361}
1362
1363static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1364 unsigned Insn,
1365 uint64_t Address,
1366 const void *Decoder) {
1367 int Offset = SignExtend32<16>(Insn & 0xffff);
1368 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1369 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1370
1371 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1372 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1373
1374 Inst.addOperand(MCOperand::CreateReg(Reg));
1375 Inst.addOperand(MCOperand::CreateReg(Base));
1376 Inst.addOperand(MCOperand::CreateImm(Offset));
1377
1378 return MCDisassembler::Success;
1379}
1380
Akira Hatanaka71928e62012-04-17 18:03:21 +00001381static DecodeStatus DecodeFMem(MCInst &Inst,
1382 unsigned Insn,
1383 uint64_t Address,
1384 const void *Decoder) {
1385 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001386 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1387 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001388
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001389 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001390 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001391
1392 Inst.addOperand(MCOperand::CreateReg(Reg));
1393 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001394 Inst.addOperand(MCOperand::CreateImm(Offset));
1395
1396 return MCDisassembler::Success;
1397}
1398
Daniel Sanders92db6b72014-10-01 08:26:55 +00001399static DecodeStatus DecodeFMem2(MCInst &Inst,
1400 unsigned Insn,
1401 uint64_t Address,
1402 const void *Decoder) {
1403 int Offset = SignExtend32<16>(Insn & 0xffff);
1404 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1405 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1406
1407 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1408 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1409
1410 Inst.addOperand(MCOperand::CreateReg(Reg));
1411 Inst.addOperand(MCOperand::CreateReg(Base));
1412 Inst.addOperand(MCOperand::CreateImm(Offset));
1413
1414 return MCDisassembler::Success;
1415}
1416
1417static DecodeStatus DecodeFMem3(MCInst &Inst,
1418 unsigned Insn,
1419 uint64_t Address,
1420 const void *Decoder) {
1421 int Offset = SignExtend32<16>(Insn & 0xffff);
1422 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1423 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1424
1425 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1426 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1427
1428 Inst.addOperand(MCOperand::CreateReg(Reg));
1429 Inst.addOperand(MCOperand::CreateReg(Base));
1430 Inst.addOperand(MCOperand::CreateImm(Offset));
1431
1432 return MCDisassembler::Success;
1433}
1434
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001435static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1436 unsigned Insn,
1437 uint64_t Address,
1438 const void *Decoder) {
1439 int Offset = SignExtend32<11>(Insn & 0x07ff);
1440 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1441 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1442
1443 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1444 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1445
1446 Inst.addOperand(MCOperand::CreateReg(Reg));
1447 Inst.addOperand(MCOperand::CreateReg(Base));
1448 Inst.addOperand(MCOperand::CreateImm(Offset));
1449
1450 return MCDisassembler::Success;
1451}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001452static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1453 unsigned Insn,
1454 uint64_t Address,
1455 const void *Decoder) {
1456 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1457 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1458 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1459
1460 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1461 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1462
1463 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1464 Inst.addOperand(MCOperand::CreateReg(Rt));
1465 }
1466
1467 Inst.addOperand(MCOperand::CreateReg(Rt));
1468 Inst.addOperand(MCOperand::CreateReg(Base));
1469 Inst.addOperand(MCOperand::CreateImm(Offset));
1470
1471 return MCDisassembler::Success;
1472}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001473
1474static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1475 unsigned RegNo,
1476 uint64_t Address,
1477 const void *Decoder) {
1478 // Currently only hardware register 29 is supported.
1479 if (RegNo != 29)
1480 return MCDisassembler::Fail;
1481 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1482 return MCDisassembler::Success;
1483}
1484
Akira Hatanaka71928e62012-04-17 18:03:21 +00001485static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1486 unsigned RegNo,
1487 uint64_t Address,
1488 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001489 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001490 return MCDisassembler::Fail;
1491
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001492 ;
1493 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1494 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001495 return MCDisassembler::Success;
1496}
1497
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001498static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1499 unsigned RegNo,
1500 uint64_t Address,
1501 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001502 if (RegNo >= 4)
1503 return MCDisassembler::Fail;
1504
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001505 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001506 Inst.addOperand(MCOperand::CreateReg(Reg));
1507 return MCDisassembler::Success;
1508}
1509
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001510static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1511 unsigned RegNo,
1512 uint64_t Address,
1513 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001514 if (RegNo >= 4)
1515 return MCDisassembler::Fail;
1516
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001517 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001518 Inst.addOperand(MCOperand::CreateReg(Reg));
1519 return MCDisassembler::Success;
1520}
1521
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001522static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1523 unsigned RegNo,
1524 uint64_t Address,
1525 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001526 if (RegNo >= 4)
1527 return MCDisassembler::Fail;
1528
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001529 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001530 Inst.addOperand(MCOperand::CreateReg(Reg));
1531 return MCDisassembler::Success;
1532}
1533
Jack Carter3eb663b2013-09-26 00:09:46 +00001534static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1535 unsigned RegNo,
1536 uint64_t Address,
1537 const void *Decoder) {
1538 if (RegNo > 31)
1539 return MCDisassembler::Fail;
1540
1541 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1542 Inst.addOperand(MCOperand::CreateReg(Reg));
1543 return MCDisassembler::Success;
1544}
1545
Jack Carter5dc8ac92013-09-25 23:50:44 +00001546static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1547 unsigned RegNo,
1548 uint64_t Address,
1549 const void *Decoder) {
1550 if (RegNo > 31)
1551 return MCDisassembler::Fail;
1552
1553 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1554 Inst.addOperand(MCOperand::CreateReg(Reg));
1555 return MCDisassembler::Success;
1556}
1557
1558static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1559 unsigned RegNo,
1560 uint64_t Address,
1561 const void *Decoder) {
1562 if (RegNo > 31)
1563 return MCDisassembler::Fail;
1564
1565 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1566 Inst.addOperand(MCOperand::CreateReg(Reg));
1567 return MCDisassembler::Success;
1568}
1569
1570static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1571 unsigned RegNo,
1572 uint64_t Address,
1573 const void *Decoder) {
1574 if (RegNo > 31)
1575 return MCDisassembler::Fail;
1576
1577 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1578 Inst.addOperand(MCOperand::CreateReg(Reg));
1579 return MCDisassembler::Success;
1580}
1581
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001582static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1583 unsigned RegNo,
1584 uint64_t Address,
1585 const void *Decoder) {
1586 if (RegNo > 7)
1587 return MCDisassembler::Fail;
1588
1589 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1590 Inst.addOperand(MCOperand::CreateReg(Reg));
1591 return MCDisassembler::Success;
1592}
1593
Daniel Sanders2a83d682014-05-21 12:56:39 +00001594static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1595 unsigned RegNo,
1596 uint64_t Address,
1597 const void *Decoder) {
1598 if (RegNo > 31)
1599 return MCDisassembler::Fail;
1600
1601 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1602 Inst.addOperand(MCOperand::CreateReg(Reg));
1603 return MCDisassembler::Success;
1604}
1605
Akira Hatanaka71928e62012-04-17 18:03:21 +00001606static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1607 unsigned Offset,
1608 uint64_t Address,
1609 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001610 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001611 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1612 return MCDisassembler::Success;
1613}
1614
Akira Hatanaka71928e62012-04-17 18:03:21 +00001615static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1616 unsigned Insn,
1617 uint64_t Address,
1618 const void *Decoder) {
1619
Jim Grosbachecaef492012-08-14 19:06:05 +00001620 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001621 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1622 return MCDisassembler::Success;
1623}
1624
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001625static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1626 unsigned Offset,
1627 uint64_t Address,
1628 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001629 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001630
1631 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1632 return MCDisassembler::Success;
1633}
1634
1635static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1636 unsigned Offset,
1637 uint64_t Address,
1638 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001639 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001640
1641 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1642 return MCDisassembler::Success;
1643}
1644
Jozef Kolek9761e962015-01-12 12:03:34 +00001645static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1646 unsigned Offset,
1647 uint64_t Address,
1648 const void *Decoder) {
1649 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1650 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1651 return MCDisassembler::Success;
1652}
1653
Jozef Kolek5cfebdd2015-01-21 12:39:30 +00001654static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1655 unsigned Offset,
1656 uint64_t Address,
1657 const void *Decoder) {
1658 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1659 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1660 return MCDisassembler::Success;
1661}
1662
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001663static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1664 unsigned Offset,
1665 uint64_t Address,
1666 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001667 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001668 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1669 return MCDisassembler::Success;
1670}
1671
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001672static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1673 unsigned Insn,
1674 uint64_t Address,
1675 const void *Decoder) {
1676 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1677 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1678 return MCDisassembler::Success;
1679}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001680
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001681static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1682 unsigned Value,
1683 uint64_t Address,
1684 const void *Decoder) {
1685 if (Value == 0)
1686 Inst.addOperand(MCOperand::CreateImm(1));
1687 else if (Value == 0x7)
1688 Inst.addOperand(MCOperand::CreateImm(-1));
1689 else
1690 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1691 return MCDisassembler::Success;
1692}
1693
1694static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1695 unsigned Value,
1696 uint64_t Address,
1697 const void *Decoder) {
1698 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1699 return MCDisassembler::Success;
1700}
1701
1702static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1703 unsigned Value,
1704 uint64_t Address,
1705 const void *Decoder) {
1706 if (Value == 0x7F)
1707 Inst.addOperand(MCOperand::CreateImm(-1));
1708 else
1709 Inst.addOperand(MCOperand::CreateImm(Value));
1710 return MCDisassembler::Success;
1711}
1712
1713static DecodeStatus DecodeSimm4(MCInst &Inst,
1714 unsigned Value,
1715 uint64_t Address,
1716 const void *Decoder) {
1717 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1718 return MCDisassembler::Success;
1719}
1720
Akira Hatanaka71928e62012-04-17 18:03:21 +00001721static DecodeStatus DecodeSimm16(MCInst &Inst,
1722 unsigned Insn,
1723 uint64_t Address,
1724 const void *Decoder) {
1725 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1726 return MCDisassembler::Success;
1727}
1728
Matheus Almeida779c5932013-11-18 12:32:49 +00001729static DecodeStatus DecodeLSAImm(MCInst &Inst,
1730 unsigned Insn,
1731 uint64_t Address,
1732 const void *Decoder) {
1733 // We add one to the immediate field as it was encoded as 'imm - 1'.
1734 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1735 return MCDisassembler::Success;
1736}
1737
Akira Hatanaka71928e62012-04-17 18:03:21 +00001738static DecodeStatus DecodeInsSize(MCInst &Inst,
1739 unsigned Insn,
1740 uint64_t Address,
1741 const void *Decoder) {
1742 // First we need to grab the pos(lsb) from MCInst.
1743 int Pos = Inst.getOperand(2).getImm();
1744 int Size = (int) Insn - Pos + 1;
1745 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1746 return MCDisassembler::Success;
1747}
1748
1749static DecodeStatus DecodeExtSize(MCInst &Inst,
1750 unsigned Insn,
1751 uint64_t Address,
1752 const void *Decoder) {
1753 int Size = (int) Insn + 1;
1754 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1755 return MCDisassembler::Success;
1756}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001757
1758static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1759 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001760 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001761 return MCDisassembler::Success;
1762}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001763
1764static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1765 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001766 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001767 return MCDisassembler::Success;
1768}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001769
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001770static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1771 uint64_t Address, const void *Decoder) {
1772 int32_t DecodedValue;
1773 switch (Insn) {
1774 case 0: DecodedValue = 256; break;
1775 case 1: DecodedValue = 257; break;
1776 case 510: DecodedValue = -258; break;
1777 case 511: DecodedValue = -257; break;
1778 default: DecodedValue = SignExtend32<9>(Insn); break;
1779 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001780 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001781 return MCDisassembler::Success;
1782}
1783
1784static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1785 uint64_t Address, const void *Decoder) {
1786 // Insn must be >= 0, since it is unsigned that condition is always true.
1787 assert(Insn < 16);
1788 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1789 255, 32768, 65535};
1790 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1791 return MCDisassembler::Success;
1792}
1793
1794static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1795 uint64_t Address, const void *Decoder) {
1796 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1797 return MCDisassembler::Success;
1798}
1799
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001800static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1801 unsigned Insn,
1802 uint64_t Address,
1803 const void *Decoder) {
1804 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1805 Mips::S6, Mips::FP};
1806 unsigned RegNum;
1807
1808 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1809 // Empty register lists are not allowed.
1810 if (RegLst == 0)
1811 return MCDisassembler::Fail;
1812
1813 RegNum = RegLst & 0xf;
1814 for (unsigned i = 0; i < RegNum; i++)
1815 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1816
1817 if (RegLst & 0x10)
1818 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1819
1820 return MCDisassembler::Success;
1821}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001822
1823static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1824 uint64_t Address,
1825 const void *Decoder) {
1826 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001827 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
Jozef Kolekd68d424a2015-02-10 12:41:13 +00001828 unsigned RegNum = RegLst & 0x3;
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001829
Jozef Kolekd68d424a2015-02-10 12:41:13 +00001830 for (unsigned i = 0; i <= RegNum; i++)
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001831 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1832
1833 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1834
1835 return MCDisassembler::Success;
1836}
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001837
1838static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1839 uint64_t Address, const void *Decoder) {
1840 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1841 return MCDisassembler::Success;
1842}