blob: 9dab45c6d4ad6ce9803b7fc69f1ecb282ef06e7c [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
Vladimir Medicdde3d582013-09-06 12:30:36 +0000302static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
303 unsigned Insn,
304 uint64_t Address,
305 const void *Decoder);
306
307static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
308 unsigned Insn,
309 uint64_t Address,
310 const void *Decoder);
311
Akira Hatanaka71928e62012-04-17 18:03:21 +0000312static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
313 uint64_t Address,
314 const void *Decoder);
315
Daniel Sanders92db6b72014-10-01 08:26:55 +0000316static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
317 uint64_t Address,
318 const void *Decoder);
319
320static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
321 uint64_t Address,
322 const void *Decoder);
323
Vladimir Medic435cf8a2015-01-21 10:47:36 +0000324static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
325 uint64_t Address,
326 const void *Decoder);
327
Daniel Sanders6a803f62014-06-16 13:13:03 +0000328static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
329 unsigned Insn,
330 uint64_t Address,
331 const void *Decoder);
332
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000333static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
334 unsigned Value,
335 uint64_t Address,
336 const void *Decoder);
337
338static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
339 unsigned Value,
340 uint64_t Address,
341 const void *Decoder);
342
343static DecodeStatus DecodeLiSimm7(MCInst &Inst,
344 unsigned Value,
345 uint64_t Address,
346 const void *Decoder);
347
348static DecodeStatus DecodeSimm4(MCInst &Inst,
349 unsigned Value,
350 uint64_t Address,
351 const void *Decoder);
352
Akira Hatanaka71928e62012-04-17 18:03:21 +0000353static DecodeStatus DecodeSimm16(MCInst &Inst,
354 unsigned Insn,
355 uint64_t Address,
356 const void *Decoder);
357
Matheus Almeida779c5932013-11-18 12:32:49 +0000358// Decode the immediate field of an LSA instruction which
359// is off by one.
360static DecodeStatus DecodeLSAImm(MCInst &Inst,
361 unsigned Insn,
362 uint64_t Address,
363 const void *Decoder);
364
Akira Hatanaka71928e62012-04-17 18:03:21 +0000365static DecodeStatus DecodeInsSize(MCInst &Inst,
366 unsigned Insn,
367 uint64_t Address,
368 const void *Decoder);
369
370static DecodeStatus DecodeExtSize(MCInst &Inst,
371 unsigned Insn,
372 uint64_t Address,
373 const void *Decoder);
374
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000375static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
376 uint64_t Address, const void *Decoder);
377
Zoran Jovanovic28551422014-06-09 09:49:51 +0000378static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
379 uint64_t Address, const void *Decoder);
380
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000381static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
382 uint64_t Address, const void *Decoder);
383
384static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
385 uint64_t Address, const void *Decoder);
386
387static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
388 uint64_t Address, const void *Decoder);
389
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000390static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
391 uint64_t Address, const void *Decoder);
392
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000393/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
394/// handle.
395template <typename InsnType>
396static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
397 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000398
399template <typename InsnType>
400static DecodeStatus
401DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
403
404template <typename InsnType>
405static DecodeStatus
406DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
408
409template <typename InsnType>
410static DecodeStatus
411DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
413
414template <typename InsnType>
415static DecodeStatus
416DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
418
419template <typename InsnType>
420static DecodeStatus
421DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
422 const void *Decoder);
423
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000424template <typename InsnType>
425static DecodeStatus
426DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
427 const void *Decoder);
428
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000429static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
430 uint64_t Address,
431 const void *Decoder);
432
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000433static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
434 uint64_t Address,
435 const void *Decoder);
436
Akira Hatanaka71928e62012-04-17 18:03:21 +0000437namespace llvm {
438extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
439 TheMips64elTarget;
440}
441
442static MCDisassembler *createMipsDisassembler(
443 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000444 const MCSubtargetInfo &STI,
445 MCContext &Ctx) {
446 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000447}
448
449static MCDisassembler *createMipselDisassembler(
450 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000451 const MCSubtargetInfo &STI,
452 MCContext &Ctx) {
453 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000454}
455
456static MCDisassembler *createMips64Disassembler(
457 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000458 const MCSubtargetInfo &STI,
459 MCContext &Ctx) {
460 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000461}
462
463static MCDisassembler *createMips64elDisassembler(
464 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000465 const MCSubtargetInfo &STI,
466 MCContext &Ctx) {
467 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000468}
469
470extern "C" void LLVMInitializeMipsDisassembler() {
471 // Register the disassembler.
472 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
473 createMipsDisassembler);
474 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
475 createMipselDisassembler);
476 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
477 createMips64Disassembler);
478 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
479 createMips64elDisassembler);
480}
481
Akira Hatanaka71928e62012-04-17 18:03:21 +0000482#include "MipsGenDisassemblerTables.inc"
483
Daniel Sanders5c582b22014-05-22 11:23:21 +0000484static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
485 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
486 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
487 return *(RegInfo->getRegClass(RC).begin() + RegNo);
488}
489
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000490template <typename InsnType>
491static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
492 const void *Decoder) {
493 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
494 // The size of the n field depends on the element size
495 // The register class also depends on this.
496 InsnType tmp = fieldFromInstruction(insn, 17, 5);
497 unsigned NSize = 0;
498 DecodeFN RegDecoder = nullptr;
499 if ((tmp & 0x18) == 0x00) { // INSVE_B
500 NSize = 4;
501 RegDecoder = DecodeMSA128BRegisterClass;
502 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
503 NSize = 3;
504 RegDecoder = DecodeMSA128HRegisterClass;
505 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
506 NSize = 2;
507 RegDecoder = DecodeMSA128WRegisterClass;
508 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
509 NSize = 1;
510 RegDecoder = DecodeMSA128DRegisterClass;
511 } else
512 llvm_unreachable("Invalid encoding");
513
514 assert(NSize != 0 && RegDecoder != nullptr);
515
516 // $wd
517 tmp = fieldFromInstruction(insn, 6, 5);
518 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
519 return MCDisassembler::Fail;
520 // $wd_in
521 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
522 return MCDisassembler::Fail;
523 // $n
524 tmp = fieldFromInstruction(insn, 16, NSize);
525 MI.addOperand(MCOperand::CreateImm(tmp));
526 // $ws
527 tmp = fieldFromInstruction(insn, 11, 5);
528 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
529 return MCDisassembler::Fail;
530 // $n2
531 MI.addOperand(MCOperand::CreateImm(0));
532
533 return MCDisassembler::Success;
534}
535
Daniel Sanders5c582b22014-05-22 11:23:21 +0000536template <typename InsnType>
537static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
538 uint64_t Address,
539 const void *Decoder) {
540 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
541 // (otherwise we would have matched the ADDI instruction from the earlier
542 // ISA's instead).
543 //
544 // We have:
545 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
546 // BOVC if rs >= rt
547 // BEQZALC if rs == 0 && rt != 0
548 // BEQC if rs < rt && rs != 0
549
550 InsnType Rs = fieldFromInstruction(insn, 21, 5);
551 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000552 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000553 bool HasRs = false;
554
555 if (Rs >= Rt) {
556 MI.setOpcode(Mips::BOVC);
557 HasRs = true;
558 } else if (Rs != 0 && Rs < Rt) {
559 MI.setOpcode(Mips::BEQC);
560 HasRs = true;
561 } else
562 MI.setOpcode(Mips::BEQZALC);
563
564 if (HasRs)
565 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
566 Rs)));
567
568 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
569 Rt)));
570 MI.addOperand(MCOperand::CreateImm(Imm));
571
572 return MCDisassembler::Success;
573}
574
575template <typename InsnType>
576static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
577 uint64_t Address,
578 const void *Decoder) {
579 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
580 // (otherwise we would have matched the ADDI instruction from the earlier
581 // ISA's instead).
582 //
583 // We have:
584 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
585 // BNVC if rs >= rt
586 // BNEZALC if rs == 0 && rt != 0
587 // BNEC if rs < rt && rs != 0
588
589 InsnType Rs = fieldFromInstruction(insn, 21, 5);
590 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000591 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000592 bool HasRs = false;
593
594 if (Rs >= Rt) {
595 MI.setOpcode(Mips::BNVC);
596 HasRs = true;
597 } else if (Rs != 0 && Rs < Rt) {
598 MI.setOpcode(Mips::BNEC);
599 HasRs = true;
600 } else
601 MI.setOpcode(Mips::BNEZALC);
602
603 if (HasRs)
604 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
605 Rs)));
606
607 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
608 Rt)));
609 MI.addOperand(MCOperand::CreateImm(Imm));
610
611 return MCDisassembler::Success;
612}
613
614template <typename InsnType>
615static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
616 uint64_t Address,
617 const void *Decoder) {
618 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
619 // (otherwise we would have matched the BLEZL instruction from the earlier
620 // ISA's instead).
621 //
622 // We have:
623 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
624 // Invalid if rs == 0
625 // BLEZC if rs == 0 && rt != 0
626 // BGEZC if rs == rt && rt != 0
627 // BGEC if rs != rt && rs != 0 && rt != 0
628
629 InsnType Rs = fieldFromInstruction(insn, 21, 5);
630 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000631 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000632 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000633
634 if (Rt == 0)
635 return MCDisassembler::Fail;
636 else if (Rs == 0)
637 MI.setOpcode(Mips::BLEZC);
638 else if (Rs == Rt)
639 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000640 else {
641 HasRs = true;
642 MI.setOpcode(Mips::BGEC);
643 }
644
645 if (HasRs)
646 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
647 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000648
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
650 Rt)));
651
652 MI.addOperand(MCOperand::CreateImm(Imm));
653
654 return MCDisassembler::Success;
655}
656
657template <typename InsnType>
658static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
659 uint64_t Address,
660 const void *Decoder) {
661 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
662 // (otherwise we would have matched the BGTZL instruction from the earlier
663 // ISA's instead).
664 //
665 // We have:
666 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
667 // Invalid if rs == 0
668 // BGTZC if rs == 0 && rt != 0
669 // BLTZC if rs == rt && rt != 0
670 // BLTC if rs != rt && rs != 0 && rt != 0
671
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000672 bool HasRs = false;
673
Daniel Sanders5c582b22014-05-22 11:23:21 +0000674 InsnType Rs = fieldFromInstruction(insn, 21, 5);
675 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000676 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000677
678 if (Rt == 0)
679 return MCDisassembler::Fail;
680 else if (Rs == 0)
681 MI.setOpcode(Mips::BGTZC);
682 else if (Rs == Rt)
683 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000684 else {
685 MI.setOpcode(Mips::BLTC);
686 HasRs = true;
687 }
688
689 if (HasRs)
690 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
691 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000692
693 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
694 Rt)));
695
696 MI.addOperand(MCOperand::CreateImm(Imm));
697
698 return MCDisassembler::Success;
699}
700
701template <typename InsnType>
702static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
703 uint64_t Address,
704 const void *Decoder) {
705 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
706 // (otherwise we would have matched the BGTZ instruction from the earlier
707 // ISA's instead).
708 //
709 // We have:
710 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
711 // BGTZ if rt == 0
712 // BGTZALC if rs == 0 && rt != 0
713 // BLTZALC if rs != 0 && rs == rt
714 // BLTUC if rs != 0 && rs != rt
715
716 InsnType Rs = fieldFromInstruction(insn, 21, 5);
717 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000718 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000719 bool HasRs = false;
720 bool HasRt = false;
721
722 if (Rt == 0) {
723 MI.setOpcode(Mips::BGTZ);
724 HasRs = true;
725 } else if (Rs == 0) {
726 MI.setOpcode(Mips::BGTZALC);
727 HasRt = true;
728 } else if (Rs == Rt) {
729 MI.setOpcode(Mips::BLTZALC);
730 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000731 } else {
732 MI.setOpcode(Mips::BLTUC);
733 HasRs = true;
734 HasRt = true;
735 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000736
737 if (HasRs)
738 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
739 Rs)));
740
741 if (HasRt)
742 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
743 Rt)));
744
745 MI.addOperand(MCOperand::CreateImm(Imm));
746
747 return MCDisassembler::Success;
748}
749
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000750template <typename InsnType>
751static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
752 uint64_t Address,
753 const void *Decoder) {
754 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
755 // (otherwise we would have matched the BLEZL instruction from the earlier
756 // ISA's instead).
757 //
758 // We have:
759 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
760 // Invalid if rs == 0
761 // BLEZALC if rs == 0 && rt != 0
762 // BGEZALC if rs == rt && rt != 0
763 // BGEUC if rs != rt && rs != 0 && rt != 0
764
765 InsnType Rs = fieldFromInstruction(insn, 21, 5);
766 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000767 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000768 bool HasRs = false;
769
770 if (Rt == 0)
771 return MCDisassembler::Fail;
772 else if (Rs == 0)
773 MI.setOpcode(Mips::BLEZALC);
774 else if (Rs == Rt)
775 MI.setOpcode(Mips::BGEZALC);
776 else {
777 HasRs = true;
778 MI.setOpcode(Mips::BGEUC);
779 }
780
781 if (HasRs)
782 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
783 Rs)));
784 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
785 Rt)));
786
787 MI.addOperand(MCOperand::CreateImm(Imm));
788
789 return MCDisassembler::Success;
790}
791
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000792/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
793/// according to the given endianess.
794static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
795 uint64_t &Size, uint32_t &Insn,
796 bool IsBigEndian) {
797 // We want to read exactly 2 Bytes of data.
798 if (Bytes.size() < 2) {
799 Size = 0;
800 return MCDisassembler::Fail;
801 }
802
803 if (IsBigEndian) {
804 Insn = (Bytes[0] << 8) | Bytes[1];
805 } else {
806 Insn = (Bytes[1] << 8) | Bytes[0];
807 }
808
809 return MCDisassembler::Success;
810}
811
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000812/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000813/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000814static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
815 uint64_t &Size, uint32_t &Insn,
816 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000817 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000818 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000819 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000820 return MCDisassembler::Fail;
821 }
822
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000823 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
824 // always precede the low 16 bits in the instruction stream (that is, they
825 // are placed at lower addresses in the instruction stream).
826 //
827 // microMIPS byte ordering:
828 // Big-endian: 0 | 1 | 2 | 3
829 // Little-endian: 1 | 0 | 3 | 2
830
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000831 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000832 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000833 Insn =
834 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
835 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000836 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000837 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000838 (Bytes[1] << 24);
839 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000840 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000841 (Bytes[3] << 24);
842 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000843 }
844
845 return MCDisassembler::Success;
846}
847
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000848DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000849 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000850 uint64_t Address,
851 raw_ostream &VStream,
852 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000853 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000854 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000855
Vladimir Medicdde3d582013-09-06 12:30:36 +0000856 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000857 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
858
859 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
860 // Calling the auto-generated decoder function.
861 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
862 this, STI);
863 if (Result != MCDisassembler::Fail) {
864 Size = 2;
865 return Result;
866 }
867
868 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
869 if (Result == MCDisassembler::Fail)
870 return MCDisassembler::Fail;
871
872 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000873 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000874 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000875 this, STI);
876 if (Result != MCDisassembler::Fail) {
877 Size = 4;
878 return Result;
879 }
880 return MCDisassembler::Fail;
881 }
882
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000883 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
884 if (Result == MCDisassembler::Fail)
885 return MCDisassembler::Fail;
886
Daniel Sandersc171f652014-06-13 13:15:59 +0000887 if (hasCOP3()) {
888 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
889 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000890 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000891 if (Result != MCDisassembler::Fail) {
892 Size = 4;
893 return Result;
894 }
895 }
896
897 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000898 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000899 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000900 Address, this, STI);
901 if (Result != MCDisassembler::Fail) {
902 Size = 4;
903 return Result;
904 }
905 }
906
Daniel Sandersc171f652014-06-13 13:15:59 +0000907 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000908 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000909 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000910 Address, this, STI);
911 if (Result != MCDisassembler::Fail) {
912 Size = 4;
913 return Result;
914 }
915 }
916
Daniel Sanders0fa60412014-06-12 13:39:06 +0000917 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000918 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000919 Result =
920 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000921 if (Result != MCDisassembler::Fail) {
922 Size = 4;
923 return Result;
924 }
925
926 return MCDisassembler::Fail;
927}
928
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000929DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000930 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000931 uint64_t Address,
932 raw_ostream &VStream,
933 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000934 uint32_t Insn;
935
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000936 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000937 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000938 if (Result == MCDisassembler::Fail)
939 return MCDisassembler::Fail;
940
941 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000942 Result =
943 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000944 if (Result != MCDisassembler::Fail) {
945 Size = 4;
946 return Result;
947 }
948 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000949 Result =
950 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000951 if (Result != MCDisassembler::Fail) {
952 Size = 4;
953 return Result;
954 }
955
956 return MCDisassembler::Fail;
957}
958
Reed Kotlerec8a5492013-02-14 03:05:25 +0000959static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
960 unsigned RegNo,
961 uint64_t Address,
962 const void *Decoder) {
963
964 return MCDisassembler::Fail;
965
966}
967
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000968static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
969 unsigned RegNo,
970 uint64_t Address,
971 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000972
973 if (RegNo > 31)
974 return MCDisassembler::Fail;
975
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000976 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000977 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000978 return MCDisassembler::Success;
979}
980
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000981static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
982 unsigned RegNo,
983 uint64_t Address,
984 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000985 if (RegNo > 7)
986 return MCDisassembler::Fail;
987 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
988 Inst.addOperand(MCOperand::CreateReg(Reg));
989 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000990}
991
Jozef Kolek1904fa22014-11-24 14:25:53 +0000992static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
993 unsigned RegNo,
994 uint64_t Address,
995 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000996 if (RegNo > 7)
997 return MCDisassembler::Fail;
998 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
999 Inst.addOperand(MCOperand::CreateReg(Reg));
1000 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +00001001}
1002
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001003static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
1004 unsigned RegNo,
1005 uint64_t Address,
1006 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +00001007 if (RegNo > 31)
1008 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001009 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001010 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001011 return MCDisassembler::Success;
1012}
1013
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001014static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1015 unsigned RegNo,
1016 uint64_t Address,
1017 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +00001018 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001019 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1020
1021 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1022}
1023
Akira Hatanaka654655f2013-08-14 00:53:38 +00001024static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1025 unsigned RegNo,
1026 uint64_t Address,
1027 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001028 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001029}
1030
Akira Hatanaka71928e62012-04-17 18:03:21 +00001031static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1032 unsigned RegNo,
1033 uint64_t Address,
1034 const void *Decoder) {
1035 if (RegNo > 31)
1036 return MCDisassembler::Fail;
1037
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001038 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1039 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001040 return MCDisassembler::Success;
1041}
1042
1043static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1044 unsigned RegNo,
1045 uint64_t Address,
1046 const void *Decoder) {
1047 if (RegNo > 31)
1048 return MCDisassembler::Fail;
1049
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001050 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1051 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001052 return MCDisassembler::Success;
1053}
1054
1055static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1056 unsigned RegNo,
1057 uint64_t Address,
1058 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001059 if (RegNo > 31)
1060 return MCDisassembler::Fail;
1061 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1062 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001063 return MCDisassembler::Success;
1064}
1065
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001066static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1067 unsigned RegNo,
1068 uint64_t Address,
1069 const void *Decoder) {
1070 if (RegNo > 7)
1071 return MCDisassembler::Fail;
1072 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1073 Inst.addOperand(MCOperand::CreateReg(Reg));
1074 return MCDisassembler::Success;
1075}
1076
Daniel Sanders0fa60412014-06-12 13:39:06 +00001077static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1078 uint64_t Address,
1079 const void *Decoder) {
1080 if (RegNo > 31)
1081 return MCDisassembler::Fail;
1082
1083 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1084 Inst.addOperand(MCOperand::CreateReg(Reg));
1085 return MCDisassembler::Success;
1086}
1087
Akira Hatanaka71928e62012-04-17 18:03:21 +00001088static DecodeStatus DecodeMem(MCInst &Inst,
1089 unsigned Insn,
1090 uint64_t Address,
1091 const void *Decoder) {
1092 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001093 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1094 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001095
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001096 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1097 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001098
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001099 if(Inst.getOpcode() == Mips::SC ||
1100 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001101 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001102 }
1103
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001104 Inst.addOperand(MCOperand::CreateReg(Reg));
1105 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001106 Inst.addOperand(MCOperand::CreateImm(Offset));
1107
1108 return MCDisassembler::Success;
1109}
1110
Daniel Sanders92db6b72014-10-01 08:26:55 +00001111static DecodeStatus DecodeCacheOp(MCInst &Inst,
1112 unsigned Insn,
1113 uint64_t Address,
1114 const void *Decoder) {
1115 int Offset = SignExtend32<16>(Insn & 0xffff);
1116 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1117 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1118
1119 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1120
1121 Inst.addOperand(MCOperand::CreateReg(Base));
1122 Inst.addOperand(MCOperand::CreateImm(Offset));
1123 Inst.addOperand(MCOperand::CreateImm(Hint));
1124
1125 return MCDisassembler::Success;
1126}
1127
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001128static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1129 unsigned Insn,
1130 uint64_t Address,
1131 const void *Decoder) {
1132 int Offset = SignExtend32<12>(Insn & 0xfff);
1133 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1134 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1135
1136 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1137
1138 Inst.addOperand(MCOperand::CreateReg(Base));
1139 Inst.addOperand(MCOperand::CreateImm(Offset));
1140 Inst.addOperand(MCOperand::CreateImm(Hint));
1141
1142 return MCDisassembler::Success;
1143}
1144
Vladimir Medicdf464ae2015-01-29 11:33:41 +00001145static DecodeStatus DecodeCacheOpR6(MCInst &Inst,
1146 unsigned Insn,
1147 uint64_t Address,
1148 const void *Decoder) {
1149 int Offset = fieldFromInstruction(Insn, 7, 9);
1150 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1151 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1152
1153 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1154
1155 Inst.addOperand(MCOperand::CreateReg(Base));
1156 Inst.addOperand(MCOperand::CreateImm(Offset));
1157 Inst.addOperand(MCOperand::CreateImm(Hint));
1158
1159 return MCDisassembler::Success;
1160}
1161
Daniel Sandersb4484d62014-11-27 17:28:10 +00001162static DecodeStatus DecodeSyncI(MCInst &Inst,
1163 unsigned Insn,
1164 uint64_t Address,
1165 const void *Decoder) {
1166 int Offset = SignExtend32<16>(Insn & 0xffff);
1167 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1168
1169 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1170
1171 Inst.addOperand(MCOperand::CreateReg(Base));
1172 Inst.addOperand(MCOperand::CreateImm(Offset));
1173
1174 return MCDisassembler::Success;
1175}
1176
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001177static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1178 uint64_t Address, const void *Decoder) {
1179 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1180 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1181 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1182
1183 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1184 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1185
1186 Inst.addOperand(MCOperand::CreateReg(Reg));
1187 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001188
1189 // The immediate field of an LD/ST instruction is scaled which means it must
1190 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1191 // data format.
1192 // .b - 1 byte
1193 // .h - 2 bytes
1194 // .w - 4 bytes
1195 // .d - 8 bytes
1196 switch(Inst.getOpcode())
1197 {
1198 default:
1199 assert (0 && "Unexpected instruction");
1200 return MCDisassembler::Fail;
1201 break;
1202 case Mips::LD_B:
1203 case Mips::ST_B:
1204 Inst.addOperand(MCOperand::CreateImm(Offset));
1205 break;
1206 case Mips::LD_H:
1207 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001208 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001209 break;
1210 case Mips::LD_W:
1211 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001212 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001213 break;
1214 case Mips::LD_D:
1215 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001216 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001217 break;
1218 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001219
1220 return MCDisassembler::Success;
1221}
1222
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001223static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1224 unsigned Insn,
1225 uint64_t Address,
1226 const void *Decoder) {
1227 unsigned Offset = Insn & 0xf;
1228 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1229 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1230
1231 switch (Inst.getOpcode()) {
1232 case Mips::LBU16_MM:
1233 case Mips::LHU16_MM:
1234 case Mips::LW16_MM:
1235 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1236 == MCDisassembler::Fail)
1237 return MCDisassembler::Fail;
1238 break;
1239 case Mips::SB16_MM:
1240 case Mips::SH16_MM:
1241 case Mips::SW16_MM:
1242 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1243 == MCDisassembler::Fail)
1244 return MCDisassembler::Fail;
1245 break;
1246 }
1247
1248 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1249 == MCDisassembler::Fail)
1250 return MCDisassembler::Fail;
1251
1252 switch (Inst.getOpcode()) {
1253 case Mips::LBU16_MM:
1254 if (Offset == 0xf)
1255 Inst.addOperand(MCOperand::CreateImm(-1));
1256 else
1257 Inst.addOperand(MCOperand::CreateImm(Offset));
1258 break;
1259 case Mips::SB16_MM:
1260 Inst.addOperand(MCOperand::CreateImm(Offset));
1261 break;
1262 case Mips::LHU16_MM:
1263 case Mips::SH16_MM:
1264 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1265 break;
1266 case Mips::LW16_MM:
1267 case Mips::SW16_MM:
1268 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1269 break;
1270 }
1271
1272 return MCDisassembler::Success;
1273}
1274
Jozef Kolek12c69822014-12-23 16:16:33 +00001275static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1276 unsigned Insn,
1277 uint64_t Address,
1278 const void *Decoder) {
1279 unsigned Offset = Insn & 0x1F;
1280 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1281
1282 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1283
1284 Inst.addOperand(MCOperand::CreateReg(Reg));
1285 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1286 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1287
1288 return MCDisassembler::Success;
1289}
1290
Jozef Koleke10a02e2015-01-28 17:27:26 +00001291static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1292 unsigned Insn,
1293 uint64_t Address,
1294 const void *Decoder) {
1295 unsigned Offset = Insn & 0x7F;
1296 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1297
1298 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1299
1300 Inst.addOperand(MCOperand::CreateReg(Reg));
1301 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1302 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1303
1304 return MCDisassembler::Success;
1305}
1306
Vladimir Medicdde3d582013-09-06 12:30:36 +00001307static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1308 unsigned Insn,
1309 uint64_t Address,
1310 const void *Decoder) {
1311 int Offset = SignExtend32<12>(Insn & 0x0fff);
1312 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1313 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1314
1315 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1316 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1317
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001318 switch (Inst.getOpcode()) {
1319 case Mips::SWM32_MM:
1320 case Mips::LWM32_MM:
1321 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1322 == MCDisassembler::Fail)
1323 return MCDisassembler::Fail;
1324 Inst.addOperand(MCOperand::CreateReg(Base));
1325 Inst.addOperand(MCOperand::CreateImm(Offset));
1326 break;
1327 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001328 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001329 // fallthrough
1330 default:
1331 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001332 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1333 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1334
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001335 Inst.addOperand(MCOperand::CreateReg(Base));
1336 Inst.addOperand(MCOperand::CreateImm(Offset));
1337 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001338
1339 return MCDisassembler::Success;
1340}
1341
1342static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1343 unsigned Insn,
1344 uint64_t Address,
1345 const void *Decoder) {
1346 int Offset = SignExtend32<16>(Insn & 0xffff);
1347 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1348 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1349
1350 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1351 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1352
1353 Inst.addOperand(MCOperand::CreateReg(Reg));
1354 Inst.addOperand(MCOperand::CreateReg(Base));
1355 Inst.addOperand(MCOperand::CreateImm(Offset));
1356
1357 return MCDisassembler::Success;
1358}
1359
Akira Hatanaka71928e62012-04-17 18:03:21 +00001360static DecodeStatus DecodeFMem(MCInst &Inst,
1361 unsigned Insn,
1362 uint64_t Address,
1363 const void *Decoder) {
1364 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001365 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1366 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001367
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001368 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001369 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001370
1371 Inst.addOperand(MCOperand::CreateReg(Reg));
1372 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001373 Inst.addOperand(MCOperand::CreateImm(Offset));
1374
1375 return MCDisassembler::Success;
1376}
1377
Daniel Sanders92db6b72014-10-01 08:26:55 +00001378static DecodeStatus DecodeFMem2(MCInst &Inst,
1379 unsigned Insn,
1380 uint64_t Address,
1381 const void *Decoder) {
1382 int Offset = SignExtend32<16>(Insn & 0xffff);
1383 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1384 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1385
1386 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1387 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1388
1389 Inst.addOperand(MCOperand::CreateReg(Reg));
1390 Inst.addOperand(MCOperand::CreateReg(Base));
1391 Inst.addOperand(MCOperand::CreateImm(Offset));
1392
1393 return MCDisassembler::Success;
1394}
1395
1396static DecodeStatus DecodeFMem3(MCInst &Inst,
1397 unsigned Insn,
1398 uint64_t Address,
1399 const void *Decoder) {
1400 int Offset = SignExtend32<16>(Insn & 0xffff);
1401 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1402 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1403
1404 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1405 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1406
1407 Inst.addOperand(MCOperand::CreateReg(Reg));
1408 Inst.addOperand(MCOperand::CreateReg(Base));
1409 Inst.addOperand(MCOperand::CreateImm(Offset));
1410
1411 return MCDisassembler::Success;
1412}
1413
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001414static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1415 unsigned Insn,
1416 uint64_t Address,
1417 const void *Decoder) {
1418 int Offset = SignExtend32<11>(Insn & 0x07ff);
1419 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1420 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1421
1422 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1423 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1424
1425 Inst.addOperand(MCOperand::CreateReg(Reg));
1426 Inst.addOperand(MCOperand::CreateReg(Base));
1427 Inst.addOperand(MCOperand::CreateImm(Offset));
1428
1429 return MCDisassembler::Success;
1430}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001431static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1432 unsigned Insn,
1433 uint64_t Address,
1434 const void *Decoder) {
1435 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1436 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1437 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1438
1439 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1440 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1441
1442 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1443 Inst.addOperand(MCOperand::CreateReg(Rt));
1444 }
1445
1446 Inst.addOperand(MCOperand::CreateReg(Rt));
1447 Inst.addOperand(MCOperand::CreateReg(Base));
1448 Inst.addOperand(MCOperand::CreateImm(Offset));
1449
1450 return MCDisassembler::Success;
1451}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001452
1453static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1454 unsigned RegNo,
1455 uint64_t Address,
1456 const void *Decoder) {
1457 // Currently only hardware register 29 is supported.
1458 if (RegNo != 29)
1459 return MCDisassembler::Fail;
1460 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1461 return MCDisassembler::Success;
1462}
1463
Akira Hatanaka71928e62012-04-17 18:03:21 +00001464static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1465 unsigned RegNo,
1466 uint64_t Address,
1467 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001468 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001469 return MCDisassembler::Fail;
1470
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001471 ;
1472 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1473 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001474 return MCDisassembler::Success;
1475}
1476
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001477static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1478 unsigned RegNo,
1479 uint64_t Address,
1480 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001481 if (RegNo >= 4)
1482 return MCDisassembler::Fail;
1483
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001484 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001485 Inst.addOperand(MCOperand::CreateReg(Reg));
1486 return MCDisassembler::Success;
1487}
1488
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001489static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1490 unsigned RegNo,
1491 uint64_t Address,
1492 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001493 if (RegNo >= 4)
1494 return MCDisassembler::Fail;
1495
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001496 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001497 Inst.addOperand(MCOperand::CreateReg(Reg));
1498 return MCDisassembler::Success;
1499}
1500
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001501static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1502 unsigned RegNo,
1503 uint64_t Address,
1504 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001505 if (RegNo >= 4)
1506 return MCDisassembler::Fail;
1507
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001508 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001509 Inst.addOperand(MCOperand::CreateReg(Reg));
1510 return MCDisassembler::Success;
1511}
1512
Jack Carter3eb663b2013-09-26 00:09:46 +00001513static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1514 unsigned RegNo,
1515 uint64_t Address,
1516 const void *Decoder) {
1517 if (RegNo > 31)
1518 return MCDisassembler::Fail;
1519
1520 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1521 Inst.addOperand(MCOperand::CreateReg(Reg));
1522 return MCDisassembler::Success;
1523}
1524
Jack Carter5dc8ac92013-09-25 23:50:44 +00001525static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1526 unsigned RegNo,
1527 uint64_t Address,
1528 const void *Decoder) {
1529 if (RegNo > 31)
1530 return MCDisassembler::Fail;
1531
1532 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1533 Inst.addOperand(MCOperand::CreateReg(Reg));
1534 return MCDisassembler::Success;
1535}
1536
1537static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1538 unsigned RegNo,
1539 uint64_t Address,
1540 const void *Decoder) {
1541 if (RegNo > 31)
1542 return MCDisassembler::Fail;
1543
1544 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1545 Inst.addOperand(MCOperand::CreateReg(Reg));
1546 return MCDisassembler::Success;
1547}
1548
1549static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1550 unsigned RegNo,
1551 uint64_t Address,
1552 const void *Decoder) {
1553 if (RegNo > 31)
1554 return MCDisassembler::Fail;
1555
1556 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1557 Inst.addOperand(MCOperand::CreateReg(Reg));
1558 return MCDisassembler::Success;
1559}
1560
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001561static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1562 unsigned RegNo,
1563 uint64_t Address,
1564 const void *Decoder) {
1565 if (RegNo > 7)
1566 return MCDisassembler::Fail;
1567
1568 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1569 Inst.addOperand(MCOperand::CreateReg(Reg));
1570 return MCDisassembler::Success;
1571}
1572
Daniel Sanders2a83d682014-05-21 12:56:39 +00001573static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1574 unsigned RegNo,
1575 uint64_t Address,
1576 const void *Decoder) {
1577 if (RegNo > 31)
1578 return MCDisassembler::Fail;
1579
1580 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1581 Inst.addOperand(MCOperand::CreateReg(Reg));
1582 return MCDisassembler::Success;
1583}
1584
Akira Hatanaka71928e62012-04-17 18:03:21 +00001585static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1586 unsigned Offset,
1587 uint64_t Address,
1588 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001589 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001590 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1591 return MCDisassembler::Success;
1592}
1593
Akira Hatanaka71928e62012-04-17 18:03:21 +00001594static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1595 unsigned Insn,
1596 uint64_t Address,
1597 const void *Decoder) {
1598
Jim Grosbachecaef492012-08-14 19:06:05 +00001599 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001600 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1601 return MCDisassembler::Success;
1602}
1603
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001604static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1605 unsigned Offset,
1606 uint64_t Address,
1607 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001608 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001609
1610 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1611 return MCDisassembler::Success;
1612}
1613
1614static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1615 unsigned Offset,
1616 uint64_t Address,
1617 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001618 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001619
1620 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1621 return MCDisassembler::Success;
1622}
1623
Jozef Kolek9761e962015-01-12 12:03:34 +00001624static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1625 unsigned Offset,
1626 uint64_t Address,
1627 const void *Decoder) {
1628 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1629 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1630 return MCDisassembler::Success;
1631}
1632
Jozef Kolek5cfebdd2015-01-21 12:39:30 +00001633static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1634 unsigned Offset,
1635 uint64_t Address,
1636 const void *Decoder) {
1637 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1638 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1639 return MCDisassembler::Success;
1640}
1641
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001642static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1643 unsigned Offset,
1644 uint64_t Address,
1645 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001646 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001647 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1648 return MCDisassembler::Success;
1649}
1650
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001651static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1652 unsigned Insn,
1653 uint64_t Address,
1654 const void *Decoder) {
1655 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1656 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1657 return MCDisassembler::Success;
1658}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001659
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001660static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1661 unsigned Value,
1662 uint64_t Address,
1663 const void *Decoder) {
1664 if (Value == 0)
1665 Inst.addOperand(MCOperand::CreateImm(1));
1666 else if (Value == 0x7)
1667 Inst.addOperand(MCOperand::CreateImm(-1));
1668 else
1669 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1670 return MCDisassembler::Success;
1671}
1672
1673static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1674 unsigned Value,
1675 uint64_t Address,
1676 const void *Decoder) {
1677 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1678 return MCDisassembler::Success;
1679}
1680
1681static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1682 unsigned Value,
1683 uint64_t Address,
1684 const void *Decoder) {
1685 if (Value == 0x7F)
1686 Inst.addOperand(MCOperand::CreateImm(-1));
1687 else
1688 Inst.addOperand(MCOperand::CreateImm(Value));
1689 return MCDisassembler::Success;
1690}
1691
1692static DecodeStatus DecodeSimm4(MCInst &Inst,
1693 unsigned Value,
1694 uint64_t Address,
1695 const void *Decoder) {
1696 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1697 return MCDisassembler::Success;
1698}
1699
Akira Hatanaka71928e62012-04-17 18:03:21 +00001700static DecodeStatus DecodeSimm16(MCInst &Inst,
1701 unsigned Insn,
1702 uint64_t Address,
1703 const void *Decoder) {
1704 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1705 return MCDisassembler::Success;
1706}
1707
Matheus Almeida779c5932013-11-18 12:32:49 +00001708static DecodeStatus DecodeLSAImm(MCInst &Inst,
1709 unsigned Insn,
1710 uint64_t Address,
1711 const void *Decoder) {
1712 // We add one to the immediate field as it was encoded as 'imm - 1'.
1713 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1714 return MCDisassembler::Success;
1715}
1716
Akira Hatanaka71928e62012-04-17 18:03:21 +00001717static DecodeStatus DecodeInsSize(MCInst &Inst,
1718 unsigned Insn,
1719 uint64_t Address,
1720 const void *Decoder) {
1721 // First we need to grab the pos(lsb) from MCInst.
1722 int Pos = Inst.getOperand(2).getImm();
1723 int Size = (int) Insn - Pos + 1;
1724 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1725 return MCDisassembler::Success;
1726}
1727
1728static DecodeStatus DecodeExtSize(MCInst &Inst,
1729 unsigned Insn,
1730 uint64_t Address,
1731 const void *Decoder) {
1732 int Size = (int) Insn + 1;
1733 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1734 return MCDisassembler::Success;
1735}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001736
1737static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1738 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001739 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001740 return MCDisassembler::Success;
1741}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001742
1743static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1744 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001745 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001746 return MCDisassembler::Success;
1747}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001748
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001749static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1750 uint64_t Address, const void *Decoder) {
1751 int32_t DecodedValue;
1752 switch (Insn) {
1753 case 0: DecodedValue = 256; break;
1754 case 1: DecodedValue = 257; break;
1755 case 510: DecodedValue = -258; break;
1756 case 511: DecodedValue = -257; break;
1757 default: DecodedValue = SignExtend32<9>(Insn); break;
1758 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001759 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001760 return MCDisassembler::Success;
1761}
1762
1763static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1764 uint64_t Address, const void *Decoder) {
1765 // Insn must be >= 0, since it is unsigned that condition is always true.
1766 assert(Insn < 16);
1767 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1768 255, 32768, 65535};
1769 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1770 return MCDisassembler::Success;
1771}
1772
1773static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1774 uint64_t Address, const void *Decoder) {
1775 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1776 return MCDisassembler::Success;
1777}
1778
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001779static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1780 unsigned Insn,
1781 uint64_t Address,
1782 const void *Decoder) {
1783 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1784 Mips::S6, Mips::FP};
1785 unsigned RegNum;
1786
1787 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1788 // Empty register lists are not allowed.
1789 if (RegLst == 0)
1790 return MCDisassembler::Fail;
1791
1792 RegNum = RegLst & 0xf;
1793 for (unsigned i = 0; i < RegNum; i++)
1794 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1795
1796 if (RegLst & 0x10)
1797 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1798
1799 return MCDisassembler::Success;
1800}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001801
1802static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1803 uint64_t Address,
1804 const void *Decoder) {
1805 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1806 unsigned RegNum;
1807
1808 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1809 // Empty register lists are not allowed.
1810 if (RegLst == 0)
1811 return MCDisassembler::Fail;
1812
1813 RegNum = RegLst & 0x3;
1814 for (unsigned i = 0; i < RegNum - 1; i++)
1815 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1816
1817 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1818
1819 return MCDisassembler::Success;
1820}
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001821
1822static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1823 uint64_t Address, const void *Decoder) {
1824 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1825 return MCDisassembler::Success;
1826}