blob: 29998e92f2ad3aade7920a28cef28339cc7003b4 [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
Jozef Kolekab6d1cc2014-12-23 19:55:34 +0000269static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
270 unsigned Insn,
271 uint64_t Address,
272 const void *Decoder);
273
Daniel Sandersb4484d62014-11-27 17:28:10 +0000274static DecodeStatus DecodeSyncI(MCInst &Inst,
275 unsigned Insn,
276 uint64_t Address,
277 const void *Decoder);
278
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000279static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
280 uint64_t Address, const void *Decoder);
281
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000282static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
283 unsigned Insn,
284 uint64_t Address,
285 const void *Decoder);
286
Jozef Kolek12c69822014-12-23 16:16:33 +0000287static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
288 unsigned Insn,
289 uint64_t Address,
290 const void *Decoder);
291
Jozef Koleke10a02e2015-01-28 17:27:26 +0000292static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
293 unsigned Insn,
294 uint64_t Address,
295 const void *Decoder);
296
Vladimir Medicdde3d582013-09-06 12:30:36 +0000297static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
298 unsigned Insn,
299 uint64_t Address,
300 const void *Decoder);
301
302static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
303 unsigned Insn,
304 uint64_t Address,
305 const void *Decoder);
306
Akira Hatanaka71928e62012-04-17 18:03:21 +0000307static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
308 uint64_t Address,
309 const void *Decoder);
310
Daniel Sanders92db6b72014-10-01 08:26:55 +0000311static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
312 uint64_t Address,
313 const void *Decoder);
314
315static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
316 uint64_t Address,
317 const void *Decoder);
318
Vladimir Medic435cf8a2015-01-21 10:47:36 +0000319static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
320 uint64_t Address,
321 const void *Decoder);
322
Daniel Sanders6a803f62014-06-16 13:13:03 +0000323static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
324 unsigned Insn,
325 uint64_t Address,
326 const void *Decoder);
327
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000328static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
329 unsigned Value,
330 uint64_t Address,
331 const void *Decoder);
332
333static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
334 unsigned Value,
335 uint64_t Address,
336 const void *Decoder);
337
338static DecodeStatus DecodeLiSimm7(MCInst &Inst,
339 unsigned Value,
340 uint64_t Address,
341 const void *Decoder);
342
343static DecodeStatus DecodeSimm4(MCInst &Inst,
344 unsigned Value,
345 uint64_t Address,
346 const void *Decoder);
347
Akira Hatanaka71928e62012-04-17 18:03:21 +0000348static DecodeStatus DecodeSimm16(MCInst &Inst,
349 unsigned Insn,
350 uint64_t Address,
351 const void *Decoder);
352
Matheus Almeida779c5932013-11-18 12:32:49 +0000353// Decode the immediate field of an LSA instruction which
354// is off by one.
355static DecodeStatus DecodeLSAImm(MCInst &Inst,
356 unsigned Insn,
357 uint64_t Address,
358 const void *Decoder);
359
Akira Hatanaka71928e62012-04-17 18:03:21 +0000360static DecodeStatus DecodeInsSize(MCInst &Inst,
361 unsigned Insn,
362 uint64_t Address,
363 const void *Decoder);
364
365static DecodeStatus DecodeExtSize(MCInst &Inst,
366 unsigned Insn,
367 uint64_t Address,
368 const void *Decoder);
369
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000370static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
371 uint64_t Address, const void *Decoder);
372
Zoran Jovanovic28551422014-06-09 09:49:51 +0000373static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
374 uint64_t Address, const void *Decoder);
375
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000376static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
377 uint64_t Address, const void *Decoder);
378
379static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
380 uint64_t Address, const void *Decoder);
381
382static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
383 uint64_t Address, const void *Decoder);
384
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000385static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
386 uint64_t Address, const void *Decoder);
387
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000388/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
389/// handle.
390template <typename InsnType>
391static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
392 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000393
394template <typename InsnType>
395static DecodeStatus
396DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
397 const void *Decoder);
398
399template <typename InsnType>
400static DecodeStatus
401DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
403
404template <typename InsnType>
405static DecodeStatus
406DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
408
409template <typename InsnType>
410static DecodeStatus
411DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
412 const void *Decoder);
413
414template <typename InsnType>
415static DecodeStatus
416DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
417 const void *Decoder);
418
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000419template <typename InsnType>
420static DecodeStatus
421DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
422 const void *Decoder);
423
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000424static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
425 uint64_t Address,
426 const void *Decoder);
427
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000428static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
429 uint64_t Address,
430 const void *Decoder);
431
Akira Hatanaka71928e62012-04-17 18:03:21 +0000432namespace llvm {
433extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
434 TheMips64elTarget;
435}
436
437static MCDisassembler *createMipsDisassembler(
438 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000439 const MCSubtargetInfo &STI,
440 MCContext &Ctx) {
441 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000442}
443
444static MCDisassembler *createMipselDisassembler(
445 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000446 const MCSubtargetInfo &STI,
447 MCContext &Ctx) {
448 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000449}
450
451static MCDisassembler *createMips64Disassembler(
452 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000453 const MCSubtargetInfo &STI,
454 MCContext &Ctx) {
455 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000456}
457
458static MCDisassembler *createMips64elDisassembler(
459 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000460 const MCSubtargetInfo &STI,
461 MCContext &Ctx) {
462 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000463}
464
465extern "C" void LLVMInitializeMipsDisassembler() {
466 // Register the disassembler.
467 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
468 createMipsDisassembler);
469 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
470 createMipselDisassembler);
471 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
472 createMips64Disassembler);
473 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
474 createMips64elDisassembler);
475}
476
Akira Hatanaka71928e62012-04-17 18:03:21 +0000477#include "MipsGenDisassemblerTables.inc"
478
Daniel Sanders5c582b22014-05-22 11:23:21 +0000479static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
480 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
481 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
482 return *(RegInfo->getRegClass(RC).begin() + RegNo);
483}
484
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000485template <typename InsnType>
486static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
487 const void *Decoder) {
488 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
489 // The size of the n field depends on the element size
490 // The register class also depends on this.
491 InsnType tmp = fieldFromInstruction(insn, 17, 5);
492 unsigned NSize = 0;
493 DecodeFN RegDecoder = nullptr;
494 if ((tmp & 0x18) == 0x00) { // INSVE_B
495 NSize = 4;
496 RegDecoder = DecodeMSA128BRegisterClass;
497 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
498 NSize = 3;
499 RegDecoder = DecodeMSA128HRegisterClass;
500 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
501 NSize = 2;
502 RegDecoder = DecodeMSA128WRegisterClass;
503 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
504 NSize = 1;
505 RegDecoder = DecodeMSA128DRegisterClass;
506 } else
507 llvm_unreachable("Invalid encoding");
508
509 assert(NSize != 0 && RegDecoder != nullptr);
510
511 // $wd
512 tmp = fieldFromInstruction(insn, 6, 5);
513 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
514 return MCDisassembler::Fail;
515 // $wd_in
516 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
517 return MCDisassembler::Fail;
518 // $n
519 tmp = fieldFromInstruction(insn, 16, NSize);
520 MI.addOperand(MCOperand::CreateImm(tmp));
521 // $ws
522 tmp = fieldFromInstruction(insn, 11, 5);
523 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
524 return MCDisassembler::Fail;
525 // $n2
526 MI.addOperand(MCOperand::CreateImm(0));
527
528 return MCDisassembler::Success;
529}
530
Daniel Sanders5c582b22014-05-22 11:23:21 +0000531template <typename InsnType>
532static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
533 uint64_t Address,
534 const void *Decoder) {
535 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
536 // (otherwise we would have matched the ADDI instruction from the earlier
537 // ISA's instead).
538 //
539 // We have:
540 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
541 // BOVC if rs >= rt
542 // BEQZALC if rs == 0 && rt != 0
543 // BEQC if rs < rt && rs != 0
544
545 InsnType Rs = fieldFromInstruction(insn, 21, 5);
546 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000547 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000548 bool HasRs = false;
549
550 if (Rs >= Rt) {
551 MI.setOpcode(Mips::BOVC);
552 HasRs = true;
553 } else if (Rs != 0 && Rs < Rt) {
554 MI.setOpcode(Mips::BEQC);
555 HasRs = true;
556 } else
557 MI.setOpcode(Mips::BEQZALC);
558
559 if (HasRs)
560 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
561 Rs)));
562
563 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
564 Rt)));
565 MI.addOperand(MCOperand::CreateImm(Imm));
566
567 return MCDisassembler::Success;
568}
569
570template <typename InsnType>
571static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
572 uint64_t Address,
573 const void *Decoder) {
574 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
575 // (otherwise we would have matched the ADDI instruction from the earlier
576 // ISA's instead).
577 //
578 // We have:
579 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
580 // BNVC if rs >= rt
581 // BNEZALC if rs == 0 && rt != 0
582 // BNEC if rs < rt && rs != 0
583
584 InsnType Rs = fieldFromInstruction(insn, 21, 5);
585 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000586 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000587 bool HasRs = false;
588
589 if (Rs >= Rt) {
590 MI.setOpcode(Mips::BNVC);
591 HasRs = true;
592 } else if (Rs != 0 && Rs < Rt) {
593 MI.setOpcode(Mips::BNEC);
594 HasRs = true;
595 } else
596 MI.setOpcode(Mips::BNEZALC);
597
598 if (HasRs)
599 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
600 Rs)));
601
602 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
603 Rt)));
604 MI.addOperand(MCOperand::CreateImm(Imm));
605
606 return MCDisassembler::Success;
607}
608
609template <typename InsnType>
610static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
611 uint64_t Address,
612 const void *Decoder) {
613 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
614 // (otherwise we would have matched the BLEZL instruction from the earlier
615 // ISA's instead).
616 //
617 // We have:
618 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
619 // Invalid if rs == 0
620 // BLEZC if rs == 0 && rt != 0
621 // BGEZC if rs == rt && rt != 0
622 // BGEC if rs != rt && rs != 0 && rt != 0
623
624 InsnType Rs = fieldFromInstruction(insn, 21, 5);
625 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000626 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000627 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000628
629 if (Rt == 0)
630 return MCDisassembler::Fail;
631 else if (Rs == 0)
632 MI.setOpcode(Mips::BLEZC);
633 else if (Rs == Rt)
634 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000635 else {
636 HasRs = true;
637 MI.setOpcode(Mips::BGEC);
638 }
639
640 if (HasRs)
641 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
642 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000643
644 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
645 Rt)));
646
647 MI.addOperand(MCOperand::CreateImm(Imm));
648
649 return MCDisassembler::Success;
650}
651
652template <typename InsnType>
653static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
654 uint64_t Address,
655 const void *Decoder) {
656 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
657 // (otherwise we would have matched the BGTZL instruction from the earlier
658 // ISA's instead).
659 //
660 // We have:
661 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
662 // Invalid if rs == 0
663 // BGTZC if rs == 0 && rt != 0
664 // BLTZC if rs == rt && rt != 0
665 // BLTC if rs != rt && rs != 0 && rt != 0
666
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000667 bool HasRs = false;
668
Daniel Sanders5c582b22014-05-22 11:23:21 +0000669 InsnType Rs = fieldFromInstruction(insn, 21, 5);
670 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000671 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000672
673 if (Rt == 0)
674 return MCDisassembler::Fail;
675 else if (Rs == 0)
676 MI.setOpcode(Mips::BGTZC);
677 else if (Rs == Rt)
678 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000679 else {
680 MI.setOpcode(Mips::BLTC);
681 HasRs = true;
682 }
683
684 if (HasRs)
685 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
686 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000687
688 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
689 Rt)));
690
691 MI.addOperand(MCOperand::CreateImm(Imm));
692
693 return MCDisassembler::Success;
694}
695
696template <typename InsnType>
697static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
698 uint64_t Address,
699 const void *Decoder) {
700 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
701 // (otherwise we would have matched the BGTZ instruction from the earlier
702 // ISA's instead).
703 //
704 // We have:
705 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
706 // BGTZ if rt == 0
707 // BGTZALC if rs == 0 && rt != 0
708 // BLTZALC if rs != 0 && rs == rt
709 // BLTUC if rs != 0 && rs != rt
710
711 InsnType Rs = fieldFromInstruction(insn, 21, 5);
712 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000713 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000714 bool HasRs = false;
715 bool HasRt = false;
716
717 if (Rt == 0) {
718 MI.setOpcode(Mips::BGTZ);
719 HasRs = true;
720 } else if (Rs == 0) {
721 MI.setOpcode(Mips::BGTZALC);
722 HasRt = true;
723 } else if (Rs == Rt) {
724 MI.setOpcode(Mips::BLTZALC);
725 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000726 } else {
727 MI.setOpcode(Mips::BLTUC);
728 HasRs = true;
729 HasRt = true;
730 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000731
732 if (HasRs)
733 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
734 Rs)));
735
736 if (HasRt)
737 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
738 Rt)));
739
740 MI.addOperand(MCOperand::CreateImm(Imm));
741
742 return MCDisassembler::Success;
743}
744
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000745template <typename InsnType>
746static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
747 uint64_t Address,
748 const void *Decoder) {
749 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
750 // (otherwise we would have matched the BLEZL instruction from the earlier
751 // ISA's instead).
752 //
753 // We have:
754 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
755 // Invalid if rs == 0
756 // BLEZALC if rs == 0 && rt != 0
757 // BGEZALC if rs == rt && rt != 0
758 // BGEUC if rs != rt && rs != 0 && rt != 0
759
760 InsnType Rs = fieldFromInstruction(insn, 21, 5);
761 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000762 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000763 bool HasRs = false;
764
765 if (Rt == 0)
766 return MCDisassembler::Fail;
767 else if (Rs == 0)
768 MI.setOpcode(Mips::BLEZALC);
769 else if (Rs == Rt)
770 MI.setOpcode(Mips::BGEZALC);
771 else {
772 HasRs = true;
773 MI.setOpcode(Mips::BGEUC);
774 }
775
776 if (HasRs)
777 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
778 Rs)));
779 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
780 Rt)));
781
782 MI.addOperand(MCOperand::CreateImm(Imm));
783
784 return MCDisassembler::Success;
785}
786
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000787/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
788/// according to the given endianess.
789static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
790 uint64_t &Size, uint32_t &Insn,
791 bool IsBigEndian) {
792 // We want to read exactly 2 Bytes of data.
793 if (Bytes.size() < 2) {
794 Size = 0;
795 return MCDisassembler::Fail;
796 }
797
798 if (IsBigEndian) {
799 Insn = (Bytes[0] << 8) | Bytes[1];
800 } else {
801 Insn = (Bytes[1] << 8) | Bytes[0];
802 }
803
804 return MCDisassembler::Success;
805}
806
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000807/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000808/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000809static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
810 uint64_t &Size, uint32_t &Insn,
811 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000812 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000813 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000814 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000815 return MCDisassembler::Fail;
816 }
817
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000818 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
819 // always precede the low 16 bits in the instruction stream (that is, they
820 // are placed at lower addresses in the instruction stream).
821 //
822 // microMIPS byte ordering:
823 // Big-endian: 0 | 1 | 2 | 3
824 // Little-endian: 1 | 0 | 3 | 2
825
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000826 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000827 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000828 Insn =
829 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
830 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000831 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000832 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000833 (Bytes[1] << 24);
834 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000835 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000836 (Bytes[3] << 24);
837 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000838 }
839
840 return MCDisassembler::Success;
841}
842
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000843DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000844 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000845 uint64_t Address,
846 raw_ostream &VStream,
847 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000848 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000849 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000850
Vladimir Medicdde3d582013-09-06 12:30:36 +0000851 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000852 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
853
854 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
855 // Calling the auto-generated decoder function.
856 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
857 this, STI);
858 if (Result != MCDisassembler::Fail) {
859 Size = 2;
860 return Result;
861 }
862
863 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
864 if (Result == MCDisassembler::Fail)
865 return MCDisassembler::Fail;
866
867 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000868 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000869 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000870 this, STI);
871 if (Result != MCDisassembler::Fail) {
872 Size = 4;
873 return Result;
874 }
875 return MCDisassembler::Fail;
876 }
877
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000878 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
879 if (Result == MCDisassembler::Fail)
880 return MCDisassembler::Fail;
881
Daniel Sandersc171f652014-06-13 13:15:59 +0000882 if (hasCOP3()) {
883 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
884 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000885 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000886 if (Result != MCDisassembler::Fail) {
887 Size = 4;
888 return Result;
889 }
890 }
891
892 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000893 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000894 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000895 Address, this, STI);
896 if (Result != MCDisassembler::Fail) {
897 Size = 4;
898 return Result;
899 }
900 }
901
Daniel Sandersc171f652014-06-13 13:15:59 +0000902 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000903 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000904 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000905 Address, this, STI);
906 if (Result != MCDisassembler::Fail) {
907 Size = 4;
908 return Result;
909 }
910 }
911
Daniel Sanders0fa60412014-06-12 13:39:06 +0000912 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000913 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000914 Result =
915 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000916 if (Result != MCDisassembler::Fail) {
917 Size = 4;
918 return Result;
919 }
920
921 return MCDisassembler::Fail;
922}
923
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000924DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000925 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000926 uint64_t Address,
927 raw_ostream &VStream,
928 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000929 uint32_t Insn;
930
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000931 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000932 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000933 if (Result == MCDisassembler::Fail)
934 return MCDisassembler::Fail;
935
936 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000937 Result =
938 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000939 if (Result != MCDisassembler::Fail) {
940 Size = 4;
941 return Result;
942 }
943 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000944 Result =
945 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000946 if (Result != MCDisassembler::Fail) {
947 Size = 4;
948 return Result;
949 }
950
951 return MCDisassembler::Fail;
952}
953
Reed Kotlerec8a5492013-02-14 03:05:25 +0000954static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
955 unsigned RegNo,
956 uint64_t Address,
957 const void *Decoder) {
958
959 return MCDisassembler::Fail;
960
961}
962
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000963static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
964 unsigned RegNo,
965 uint64_t Address,
966 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000967
968 if (RegNo > 31)
969 return MCDisassembler::Fail;
970
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000971 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000972 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000973 return MCDisassembler::Success;
974}
975
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000976static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
977 unsigned RegNo,
978 uint64_t Address,
979 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000980 if (RegNo > 7)
981 return MCDisassembler::Fail;
982 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
983 Inst.addOperand(MCOperand::CreateReg(Reg));
984 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000985}
986
Jozef Kolek1904fa22014-11-24 14:25:53 +0000987static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
988 unsigned RegNo,
989 uint64_t Address,
990 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000991 if (RegNo > 7)
992 return MCDisassembler::Fail;
993 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
994 Inst.addOperand(MCOperand::CreateReg(Reg));
995 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000996}
997
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000998static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
999 unsigned RegNo,
1000 uint64_t Address,
1001 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +00001002 if (RegNo > 31)
1003 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001004 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001005 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001006 return MCDisassembler::Success;
1007}
1008
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001009static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
1010 unsigned RegNo,
1011 uint64_t Address,
1012 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +00001013 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001014 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1015
1016 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1017}
1018
Akira Hatanaka654655f2013-08-14 00:53:38 +00001019static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1020 unsigned RegNo,
1021 uint64_t Address,
1022 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001023 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001024}
1025
Akira Hatanaka71928e62012-04-17 18:03:21 +00001026static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1027 unsigned RegNo,
1028 uint64_t Address,
1029 const void *Decoder) {
1030 if (RegNo > 31)
1031 return MCDisassembler::Fail;
1032
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001033 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1034 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001035 return MCDisassembler::Success;
1036}
1037
1038static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1039 unsigned RegNo,
1040 uint64_t Address,
1041 const void *Decoder) {
1042 if (RegNo > 31)
1043 return MCDisassembler::Fail;
1044
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001045 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1046 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001047 return MCDisassembler::Success;
1048}
1049
1050static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1051 unsigned RegNo,
1052 uint64_t Address,
1053 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001054 if (RegNo > 31)
1055 return MCDisassembler::Fail;
1056 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1057 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001058 return MCDisassembler::Success;
1059}
1060
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001061static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1062 unsigned RegNo,
1063 uint64_t Address,
1064 const void *Decoder) {
1065 if (RegNo > 7)
1066 return MCDisassembler::Fail;
1067 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1068 Inst.addOperand(MCOperand::CreateReg(Reg));
1069 return MCDisassembler::Success;
1070}
1071
Daniel Sanders0fa60412014-06-12 13:39:06 +00001072static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1073 uint64_t Address,
1074 const void *Decoder) {
1075 if (RegNo > 31)
1076 return MCDisassembler::Fail;
1077
1078 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1079 Inst.addOperand(MCOperand::CreateReg(Reg));
1080 return MCDisassembler::Success;
1081}
1082
Akira Hatanaka71928e62012-04-17 18:03:21 +00001083static DecodeStatus DecodeMem(MCInst &Inst,
1084 unsigned Insn,
1085 uint64_t Address,
1086 const void *Decoder) {
1087 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001088 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1089 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001090
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001091 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1092 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001093
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001094 if(Inst.getOpcode() == Mips::SC ||
1095 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001096 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001097 }
1098
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001099 Inst.addOperand(MCOperand::CreateReg(Reg));
1100 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001101 Inst.addOperand(MCOperand::CreateImm(Offset));
1102
1103 return MCDisassembler::Success;
1104}
1105
Daniel Sanders92db6b72014-10-01 08:26:55 +00001106static DecodeStatus DecodeCacheOp(MCInst &Inst,
1107 unsigned Insn,
1108 uint64_t Address,
1109 const void *Decoder) {
1110 int Offset = SignExtend32<16>(Insn & 0xffff);
1111 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1112 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1113
1114 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1115
1116 Inst.addOperand(MCOperand::CreateReg(Base));
1117 Inst.addOperand(MCOperand::CreateImm(Offset));
1118 Inst.addOperand(MCOperand::CreateImm(Hint));
1119
1120 return MCDisassembler::Success;
1121}
1122
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001123static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1124 unsigned Insn,
1125 uint64_t Address,
1126 const void *Decoder) {
1127 int Offset = SignExtend32<12>(Insn & 0xfff);
1128 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1129 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1130
1131 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1132
1133 Inst.addOperand(MCOperand::CreateReg(Base));
1134 Inst.addOperand(MCOperand::CreateImm(Offset));
1135 Inst.addOperand(MCOperand::CreateImm(Hint));
1136
1137 return MCDisassembler::Success;
1138}
1139
Daniel Sandersb4484d62014-11-27 17:28:10 +00001140static DecodeStatus DecodeSyncI(MCInst &Inst,
1141 unsigned Insn,
1142 uint64_t Address,
1143 const void *Decoder) {
1144 int Offset = SignExtend32<16>(Insn & 0xffff);
1145 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1146
1147 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1148
1149 Inst.addOperand(MCOperand::CreateReg(Base));
1150 Inst.addOperand(MCOperand::CreateImm(Offset));
1151
1152 return MCDisassembler::Success;
1153}
1154
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001155static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1156 uint64_t Address, const void *Decoder) {
1157 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1158 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1159 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1160
1161 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1162 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1163
1164 Inst.addOperand(MCOperand::CreateReg(Reg));
1165 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001166
1167 // The immediate field of an LD/ST instruction is scaled which means it must
1168 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1169 // data format.
1170 // .b - 1 byte
1171 // .h - 2 bytes
1172 // .w - 4 bytes
1173 // .d - 8 bytes
1174 switch(Inst.getOpcode())
1175 {
1176 default:
1177 assert (0 && "Unexpected instruction");
1178 return MCDisassembler::Fail;
1179 break;
1180 case Mips::LD_B:
1181 case Mips::ST_B:
1182 Inst.addOperand(MCOperand::CreateImm(Offset));
1183 break;
1184 case Mips::LD_H:
1185 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001186 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001187 break;
1188 case Mips::LD_W:
1189 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001190 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001191 break;
1192 case Mips::LD_D:
1193 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001194 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001195 break;
1196 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001197
1198 return MCDisassembler::Success;
1199}
1200
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001201static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1202 unsigned Insn,
1203 uint64_t Address,
1204 const void *Decoder) {
1205 unsigned Offset = Insn & 0xf;
1206 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1207 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1208
1209 switch (Inst.getOpcode()) {
1210 case Mips::LBU16_MM:
1211 case Mips::LHU16_MM:
1212 case Mips::LW16_MM:
1213 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1214 == MCDisassembler::Fail)
1215 return MCDisassembler::Fail;
1216 break;
1217 case Mips::SB16_MM:
1218 case Mips::SH16_MM:
1219 case Mips::SW16_MM:
1220 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1221 == MCDisassembler::Fail)
1222 return MCDisassembler::Fail;
1223 break;
1224 }
1225
1226 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1227 == MCDisassembler::Fail)
1228 return MCDisassembler::Fail;
1229
1230 switch (Inst.getOpcode()) {
1231 case Mips::LBU16_MM:
1232 if (Offset == 0xf)
1233 Inst.addOperand(MCOperand::CreateImm(-1));
1234 else
1235 Inst.addOperand(MCOperand::CreateImm(Offset));
1236 break;
1237 case Mips::SB16_MM:
1238 Inst.addOperand(MCOperand::CreateImm(Offset));
1239 break;
1240 case Mips::LHU16_MM:
1241 case Mips::SH16_MM:
1242 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1243 break;
1244 case Mips::LW16_MM:
1245 case Mips::SW16_MM:
1246 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1247 break;
1248 }
1249
1250 return MCDisassembler::Success;
1251}
1252
Jozef Kolek12c69822014-12-23 16:16:33 +00001253static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1254 unsigned Insn,
1255 uint64_t Address,
1256 const void *Decoder) {
1257 unsigned Offset = Insn & 0x1F;
1258 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1259
1260 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1261
1262 Inst.addOperand(MCOperand::CreateReg(Reg));
1263 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1264 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1265
1266 return MCDisassembler::Success;
1267}
1268
Jozef Koleke10a02e2015-01-28 17:27:26 +00001269static DecodeStatus DecodeMemMMGPImm7Lsl2(MCInst &Inst,
1270 unsigned Insn,
1271 uint64_t Address,
1272 const void *Decoder) {
1273 unsigned Offset = Insn & 0x7F;
1274 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1275
1276 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1277
1278 Inst.addOperand(MCOperand::CreateReg(Reg));
1279 Inst.addOperand(MCOperand::CreateReg(Mips::GP));
1280 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1281
1282 return MCDisassembler::Success;
1283}
1284
Vladimir Medicdde3d582013-09-06 12:30:36 +00001285static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1286 unsigned Insn,
1287 uint64_t Address,
1288 const void *Decoder) {
1289 int Offset = SignExtend32<12>(Insn & 0x0fff);
1290 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1291 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1292
1293 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1294 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1295
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001296 switch (Inst.getOpcode()) {
1297 case Mips::SWM32_MM:
1298 case Mips::LWM32_MM:
1299 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1300 == MCDisassembler::Fail)
1301 return MCDisassembler::Fail;
1302 Inst.addOperand(MCOperand::CreateReg(Base));
1303 Inst.addOperand(MCOperand::CreateImm(Offset));
1304 break;
1305 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001306 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001307 // fallthrough
1308 default:
1309 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001310 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1311 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1312
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001313 Inst.addOperand(MCOperand::CreateReg(Base));
1314 Inst.addOperand(MCOperand::CreateImm(Offset));
1315 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001316
1317 return MCDisassembler::Success;
1318}
1319
1320static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1321 unsigned Insn,
1322 uint64_t Address,
1323 const void *Decoder) {
1324 int Offset = SignExtend32<16>(Insn & 0xffff);
1325 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1326 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1327
1328 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1329 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1330
1331 Inst.addOperand(MCOperand::CreateReg(Reg));
1332 Inst.addOperand(MCOperand::CreateReg(Base));
1333 Inst.addOperand(MCOperand::CreateImm(Offset));
1334
1335 return MCDisassembler::Success;
1336}
1337
Akira Hatanaka71928e62012-04-17 18:03:21 +00001338static DecodeStatus DecodeFMem(MCInst &Inst,
1339 unsigned Insn,
1340 uint64_t Address,
1341 const void *Decoder) {
1342 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001343 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1344 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001345
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001346 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001347 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001348
1349 Inst.addOperand(MCOperand::CreateReg(Reg));
1350 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001351 Inst.addOperand(MCOperand::CreateImm(Offset));
1352
1353 return MCDisassembler::Success;
1354}
1355
Daniel Sanders92db6b72014-10-01 08:26:55 +00001356static DecodeStatus DecodeFMem2(MCInst &Inst,
1357 unsigned Insn,
1358 uint64_t Address,
1359 const void *Decoder) {
1360 int Offset = SignExtend32<16>(Insn & 0xffff);
1361 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1362 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1363
1364 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1365 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1366
1367 Inst.addOperand(MCOperand::CreateReg(Reg));
1368 Inst.addOperand(MCOperand::CreateReg(Base));
1369 Inst.addOperand(MCOperand::CreateImm(Offset));
1370
1371 return MCDisassembler::Success;
1372}
1373
1374static DecodeStatus DecodeFMem3(MCInst &Inst,
1375 unsigned Insn,
1376 uint64_t Address,
1377 const void *Decoder) {
1378 int Offset = SignExtend32<16>(Insn & 0xffff);
1379 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1380 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1381
1382 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1383 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1384
1385 Inst.addOperand(MCOperand::CreateReg(Reg));
1386 Inst.addOperand(MCOperand::CreateReg(Base));
1387 Inst.addOperand(MCOperand::CreateImm(Offset));
1388
1389 return MCDisassembler::Success;
1390}
1391
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001392static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1393 unsigned Insn,
1394 uint64_t Address,
1395 const void *Decoder) {
1396 int Offset = SignExtend32<11>(Insn & 0x07ff);
1397 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1398 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1399
1400 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1401 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1402
1403 Inst.addOperand(MCOperand::CreateReg(Reg));
1404 Inst.addOperand(MCOperand::CreateReg(Base));
1405 Inst.addOperand(MCOperand::CreateImm(Offset));
1406
1407 return MCDisassembler::Success;
1408}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001409static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1410 unsigned Insn,
1411 uint64_t Address,
1412 const void *Decoder) {
1413 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1414 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1415 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1416
1417 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1418 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1419
1420 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1421 Inst.addOperand(MCOperand::CreateReg(Rt));
1422 }
1423
1424 Inst.addOperand(MCOperand::CreateReg(Rt));
1425 Inst.addOperand(MCOperand::CreateReg(Base));
1426 Inst.addOperand(MCOperand::CreateImm(Offset));
1427
1428 return MCDisassembler::Success;
1429}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001430
1431static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1432 unsigned RegNo,
1433 uint64_t Address,
1434 const void *Decoder) {
1435 // Currently only hardware register 29 is supported.
1436 if (RegNo != 29)
1437 return MCDisassembler::Fail;
1438 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1439 return MCDisassembler::Success;
1440}
1441
Akira Hatanaka71928e62012-04-17 18:03:21 +00001442static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1443 unsigned RegNo,
1444 uint64_t Address,
1445 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001446 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001447 return MCDisassembler::Fail;
1448
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001449 ;
1450 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1451 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001452 return MCDisassembler::Success;
1453}
1454
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001455static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1456 unsigned RegNo,
1457 uint64_t Address,
1458 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001459 if (RegNo >= 4)
1460 return MCDisassembler::Fail;
1461
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001462 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001463 Inst.addOperand(MCOperand::CreateReg(Reg));
1464 return MCDisassembler::Success;
1465}
1466
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001467static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1468 unsigned RegNo,
1469 uint64_t Address,
1470 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001471 if (RegNo >= 4)
1472 return MCDisassembler::Fail;
1473
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001474 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001475 Inst.addOperand(MCOperand::CreateReg(Reg));
1476 return MCDisassembler::Success;
1477}
1478
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001479static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1480 unsigned RegNo,
1481 uint64_t Address,
1482 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001483 if (RegNo >= 4)
1484 return MCDisassembler::Fail;
1485
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001486 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001487 Inst.addOperand(MCOperand::CreateReg(Reg));
1488 return MCDisassembler::Success;
1489}
1490
Jack Carter3eb663b2013-09-26 00:09:46 +00001491static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1492 unsigned RegNo,
1493 uint64_t Address,
1494 const void *Decoder) {
1495 if (RegNo > 31)
1496 return MCDisassembler::Fail;
1497
1498 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1499 Inst.addOperand(MCOperand::CreateReg(Reg));
1500 return MCDisassembler::Success;
1501}
1502
Jack Carter5dc8ac92013-09-25 23:50:44 +00001503static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1504 unsigned RegNo,
1505 uint64_t Address,
1506 const void *Decoder) {
1507 if (RegNo > 31)
1508 return MCDisassembler::Fail;
1509
1510 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1511 Inst.addOperand(MCOperand::CreateReg(Reg));
1512 return MCDisassembler::Success;
1513}
1514
1515static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1516 unsigned RegNo,
1517 uint64_t Address,
1518 const void *Decoder) {
1519 if (RegNo > 31)
1520 return MCDisassembler::Fail;
1521
1522 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1523 Inst.addOperand(MCOperand::CreateReg(Reg));
1524 return MCDisassembler::Success;
1525}
1526
1527static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1528 unsigned RegNo,
1529 uint64_t Address,
1530 const void *Decoder) {
1531 if (RegNo > 31)
1532 return MCDisassembler::Fail;
1533
1534 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1535 Inst.addOperand(MCOperand::CreateReg(Reg));
1536 return MCDisassembler::Success;
1537}
1538
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001539static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1540 unsigned RegNo,
1541 uint64_t Address,
1542 const void *Decoder) {
1543 if (RegNo > 7)
1544 return MCDisassembler::Fail;
1545
1546 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1547 Inst.addOperand(MCOperand::CreateReg(Reg));
1548 return MCDisassembler::Success;
1549}
1550
Daniel Sanders2a83d682014-05-21 12:56:39 +00001551static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1552 unsigned RegNo,
1553 uint64_t Address,
1554 const void *Decoder) {
1555 if (RegNo > 31)
1556 return MCDisassembler::Fail;
1557
1558 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1559 Inst.addOperand(MCOperand::CreateReg(Reg));
1560 return MCDisassembler::Success;
1561}
1562
Akira Hatanaka71928e62012-04-17 18:03:21 +00001563static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1564 unsigned Offset,
1565 uint64_t Address,
1566 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001567 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001568 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1569 return MCDisassembler::Success;
1570}
1571
Akira Hatanaka71928e62012-04-17 18:03:21 +00001572static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1573 unsigned Insn,
1574 uint64_t Address,
1575 const void *Decoder) {
1576
Jim Grosbachecaef492012-08-14 19:06:05 +00001577 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001578 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1579 return MCDisassembler::Success;
1580}
1581
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001582static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1583 unsigned Offset,
1584 uint64_t Address,
1585 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001586 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001587
1588 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1589 return MCDisassembler::Success;
1590}
1591
1592static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1593 unsigned Offset,
1594 uint64_t Address,
1595 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001596 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001597
1598 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1599 return MCDisassembler::Success;
1600}
1601
Jozef Kolek9761e962015-01-12 12:03:34 +00001602static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1603 unsigned Offset,
1604 uint64_t Address,
1605 const void *Decoder) {
1606 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1607 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1608 return MCDisassembler::Success;
1609}
1610
Jozef Kolek5cfebdd2015-01-21 12:39:30 +00001611static DecodeStatus DecodeBranchTarget10MM(MCInst &Inst,
1612 unsigned Offset,
1613 uint64_t Address,
1614 const void *Decoder) {
1615 int32_t BranchOffset = SignExtend32<10>(Offset) << 1;
1616 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1617 return MCDisassembler::Success;
1618}
1619
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001620static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1621 unsigned Offset,
1622 uint64_t Address,
1623 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001624 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001625 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1626 return MCDisassembler::Success;
1627}
1628
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001629static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1630 unsigned Insn,
1631 uint64_t Address,
1632 const void *Decoder) {
1633 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1634 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1635 return MCDisassembler::Success;
1636}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001637
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001638static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1639 unsigned Value,
1640 uint64_t Address,
1641 const void *Decoder) {
1642 if (Value == 0)
1643 Inst.addOperand(MCOperand::CreateImm(1));
1644 else if (Value == 0x7)
1645 Inst.addOperand(MCOperand::CreateImm(-1));
1646 else
1647 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1648 return MCDisassembler::Success;
1649}
1650
1651static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1652 unsigned Value,
1653 uint64_t Address,
1654 const void *Decoder) {
1655 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1656 return MCDisassembler::Success;
1657}
1658
1659static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1660 unsigned Value,
1661 uint64_t Address,
1662 const void *Decoder) {
1663 if (Value == 0x7F)
1664 Inst.addOperand(MCOperand::CreateImm(-1));
1665 else
1666 Inst.addOperand(MCOperand::CreateImm(Value));
1667 return MCDisassembler::Success;
1668}
1669
1670static DecodeStatus DecodeSimm4(MCInst &Inst,
1671 unsigned Value,
1672 uint64_t Address,
1673 const void *Decoder) {
1674 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1675 return MCDisassembler::Success;
1676}
1677
Akira Hatanaka71928e62012-04-17 18:03:21 +00001678static DecodeStatus DecodeSimm16(MCInst &Inst,
1679 unsigned Insn,
1680 uint64_t Address,
1681 const void *Decoder) {
1682 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1683 return MCDisassembler::Success;
1684}
1685
Matheus Almeida779c5932013-11-18 12:32:49 +00001686static DecodeStatus DecodeLSAImm(MCInst &Inst,
1687 unsigned Insn,
1688 uint64_t Address,
1689 const void *Decoder) {
1690 // We add one to the immediate field as it was encoded as 'imm - 1'.
1691 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1692 return MCDisassembler::Success;
1693}
1694
Akira Hatanaka71928e62012-04-17 18:03:21 +00001695static DecodeStatus DecodeInsSize(MCInst &Inst,
1696 unsigned Insn,
1697 uint64_t Address,
1698 const void *Decoder) {
1699 // First we need to grab the pos(lsb) from MCInst.
1700 int Pos = Inst.getOperand(2).getImm();
1701 int Size = (int) Insn - Pos + 1;
1702 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1703 return MCDisassembler::Success;
1704}
1705
1706static DecodeStatus DecodeExtSize(MCInst &Inst,
1707 unsigned Insn,
1708 uint64_t Address,
1709 const void *Decoder) {
1710 int Size = (int) Insn + 1;
1711 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1712 return MCDisassembler::Success;
1713}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001714
1715static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1716 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001717 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001718 return MCDisassembler::Success;
1719}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001720
1721static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1722 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001723 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001724 return MCDisassembler::Success;
1725}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001726
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001727static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1728 uint64_t Address, const void *Decoder) {
1729 int32_t DecodedValue;
1730 switch (Insn) {
1731 case 0: DecodedValue = 256; break;
1732 case 1: DecodedValue = 257; break;
1733 case 510: DecodedValue = -258; break;
1734 case 511: DecodedValue = -257; break;
1735 default: DecodedValue = SignExtend32<9>(Insn); break;
1736 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001737 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001738 return MCDisassembler::Success;
1739}
1740
1741static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1742 uint64_t Address, const void *Decoder) {
1743 // Insn must be >= 0, since it is unsigned that condition is always true.
1744 assert(Insn < 16);
1745 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1746 255, 32768, 65535};
1747 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1748 return MCDisassembler::Success;
1749}
1750
1751static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1752 uint64_t Address, const void *Decoder) {
1753 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1754 return MCDisassembler::Success;
1755}
1756
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001757static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1758 unsigned Insn,
1759 uint64_t Address,
1760 const void *Decoder) {
1761 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1762 Mips::S6, Mips::FP};
1763 unsigned RegNum;
1764
1765 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1766 // Empty register lists are not allowed.
1767 if (RegLst == 0)
1768 return MCDisassembler::Fail;
1769
1770 RegNum = RegLst & 0xf;
1771 for (unsigned i = 0; i < RegNum; i++)
1772 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1773
1774 if (RegLst & 0x10)
1775 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1776
1777 return MCDisassembler::Success;
1778}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001779
1780static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1781 uint64_t Address,
1782 const void *Decoder) {
1783 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1784 unsigned RegNum;
1785
1786 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1787 // Empty register lists are not allowed.
1788 if (RegLst == 0)
1789 return MCDisassembler::Fail;
1790
1791 RegNum = RegLst & 0x3;
1792 for (unsigned i = 0; i < RegNum - 1; i++)
1793 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1794
1795 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1796
1797 return MCDisassembler::Success;
1798}
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001799
1800static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1801 uint64_t Address, const void *Decoder) {
1802 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1803 return MCDisassembler::Success;
1804}