blob: 501e066c910f33f408b9f620eb7ce8530e5a3b38 [file] [log] [blame]
Akira Hatanaka71928e62012-04-17 18:03:21 +00001//===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is part of the Mips Disassembler.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Mips.h"
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000015#include "MipsRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "MipsSubtarget.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000017#include "llvm/MC/MCContext.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000018#include "llvm/MC/MCDisassembler.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000019#include "llvm/MC/MCFixedLenDisassembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/Support/MathExtras.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000023#include "llvm/Support/TargetRegistry.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000024
Akira Hatanaka71928e62012-04-17 18:03:21 +000025using namespace llvm;
26
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "mips-disassembler"
28
Akira Hatanaka71928e62012-04-17 18:03:21 +000029typedef MCDisassembler::DecodeStatus DecodeStatus;
30
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000031namespace {
32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000033/// A disasembler class for Mips.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000034class MipsDisassemblerBase : public MCDisassembler {
Akira Hatanaka71928e62012-04-17 18:03:21 +000035public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000036 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000037 bool IsBigEndian)
38 : MCDisassembler(STI, Ctx),
Vladimir Medice8860932014-12-16 15:29:12 +000039 IsGP64Bit(STI.getFeatureBits() & Mips::FeatureGP64Bit),
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000040 IsBigEndian(IsBigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000041
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000042 virtual ~MipsDisassemblerBase() {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000043
Vladimir Medice8860932014-12-16 15:29:12 +000044 bool isGP64Bit() const { return IsGP64Bit; }
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000045
Akira Hatanaka71928e62012-04-17 18:03:21 +000046private:
Vladimir Medice8860932014-12-16 15:29:12 +000047 bool IsGP64Bit;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000048protected:
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000049 bool IsBigEndian;
Akira Hatanaka71928e62012-04-17 18:03:21 +000050};
51
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000052/// A disasembler class for Mips32.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000053class MipsDisassembler : public MipsDisassemblerBase {
Vladimir Medicdde3d582013-09-06 12:30:36 +000054 bool IsMicroMips;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000055public:
Daniel Sandersc171f652014-06-13 13:15:59 +000056 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
57 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
58 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
59 }
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000060
Daniel Sandersc171f652014-06-13 13:15:59 +000061 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
62 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
63 bool hasMips32r6() const {
Daniel Sanders5c582b22014-05-22 11:23:21 +000064 return STI.getFeatureBits() & Mips::FeatureMips32r6;
65 }
66
Daniel Sanders0fa60412014-06-12 13:39:06 +000067 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
68
Daniel Sandersc171f652014-06-13 13:15:59 +000069 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
72 }
73
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000074 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000075 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000076 raw_ostream &VStream,
77 raw_ostream &CStream) const override;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000078};
79
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000080/// A disasembler class for Mips64.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000081class Mips64Disassembler : public MipsDisassemblerBase {
Akira Hatanaka71928e62012-04-17 18:03:21 +000082public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000083 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000084 bool bigEndian) :
Lang Hamesa1bc0f52014-04-15 04:40:56 +000085 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000086
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000087 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000088 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000089 raw_ostream &VStream,
90 raw_ostream &CStream) const override;
Akira Hatanaka71928e62012-04-17 18:03:21 +000091};
92
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000093} // end anonymous namespace
94
Akira Hatanaka71928e62012-04-17 18:03:21 +000095// Forward declare these because the autogenerated code will reference them.
96// Definitions are further down.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000097static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
98 unsigned RegNo,
99 uint64_t Address,
100 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000101
Reed Kotlerec8a5492013-02-14 03:05:25 +0000102static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
103 unsigned RegNo,
104 uint64_t Address,
105 const void *Decoder);
106
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000107static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
108 unsigned RegNo,
109 uint64_t Address,
110 const void *Decoder);
111
Jozef Kolek1904fa22014-11-24 14:25:53 +0000112static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
113 unsigned RegNo,
114 uint64_t Address,
115 const void *Decoder);
116
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000117static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
118 unsigned RegNo,
119 uint64_t Address,
120 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000121
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000122static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
123 unsigned Insn,
124 uint64_t Address,
125 const void *Decoder);
126
Akira Hatanaka654655f2013-08-14 00:53:38 +0000127static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
128 unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000131
Akira Hatanaka71928e62012-04-17 18:03:21 +0000132static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
133 unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
136
137static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
138 unsigned RegNo,
139 uint64_t Address,
140 const void *Decoder);
141
142static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
143 unsigned RegNo,
144 uint64_t Address,
145 const void *Decoder);
146
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000147static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
148 unsigned RegNo,
149 uint64_t Address,
150 const void *Decoder);
151
Daniel Sanders0fa60412014-06-12 13:39:06 +0000152static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
153 uint64_t Address,
154 const void *Decoder);
155
Akira Hatanaka71928e62012-04-17 18:03:21 +0000156static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
157 unsigned Insn,
158 uint64_t Address,
159 const void *Decoder);
160
161static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
162 unsigned RegNo,
163 uint64_t Address,
164 const void *Decoder);
165
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000166static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
167 unsigned RegNo,
168 uint64_t Address,
169 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000170
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000171static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
172 unsigned RegNo,
173 uint64_t Address,
174 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000175
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000176static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
177 unsigned RegNo,
178 uint64_t Address,
179 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000180
Jack Carter3eb663b2013-09-26 00:09:46 +0000181static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
182 unsigned RegNo,
183 uint64_t Address,
184 const void *Decoder);
185
Jack Carter5dc8ac92013-09-25 23:50:44 +0000186static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
187 unsigned RegNo,
188 uint64_t Address,
189 const void *Decoder);
190
191static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
192 unsigned RegNo,
193 uint64_t Address,
194 const void *Decoder);
195
196static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
197 unsigned RegNo,
198 uint64_t Address,
199 const void *Decoder);
200
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000201static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
202 unsigned RegNo,
203 uint64_t Address,
204 const void *Decoder);
205
Daniel Sanders2a83d682014-05-21 12:56:39 +0000206static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
207 unsigned RegNo,
208 uint64_t Address,
209 const void *Decoder);
210
Akira Hatanaka71928e62012-04-17 18:03:21 +0000211static DecodeStatus DecodeBranchTarget(MCInst &Inst,
212 unsigned Offset,
213 uint64_t Address,
214 const void *Decoder);
215
Akira Hatanaka71928e62012-04-17 18:03:21 +0000216static DecodeStatus DecodeJumpTarget(MCInst &Inst,
217 unsigned Insn,
218 uint64_t Address,
219 const void *Decoder);
220
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000221static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
222 unsigned Offset,
223 uint64_t Address,
224 const void *Decoder);
225
226static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
227 unsigned Offset,
228 uint64_t Address,
229 const void *Decoder);
230
Jozef Kolek9761e962015-01-12 12:03:34 +0000231// DecodeBranchTarget7MM - Decode microMIPS branch offset, which is
232// shifted left by 1 bit.
233static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
234 unsigned Offset,
235 uint64_t Address,
236 const void *Decoder);
237
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000238// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
239// shifted left by 1 bit.
240static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
241 unsigned Offset,
242 uint64_t Address,
243 const void *Decoder);
244
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000245// DecodeJumpTargetMM - Decode microMIPS jump target, which is
246// shifted left by 1 bit.
247static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
248 unsigned Insn,
249 uint64_t Address,
250 const void *Decoder);
251
Akira Hatanaka71928e62012-04-17 18:03:21 +0000252static DecodeStatus DecodeMem(MCInst &Inst,
253 unsigned Insn,
254 uint64_t Address,
255 const void *Decoder);
256
Daniel Sanders92db6b72014-10-01 08:26:55 +0000257static DecodeStatus DecodeCacheOp(MCInst &Inst,
258 unsigned Insn,
259 uint64_t Address,
260 const void *Decoder);
261
Jozef Kolekab6d1cc2014-12-23 19:55:34 +0000262static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
263 unsigned Insn,
264 uint64_t Address,
265 const void *Decoder);
266
Daniel Sandersb4484d62014-11-27 17:28:10 +0000267static DecodeStatus DecodeSyncI(MCInst &Inst,
268 unsigned Insn,
269 uint64_t Address,
270 const void *Decoder);
271
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000272static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
273 uint64_t Address, const void *Decoder);
274
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000275static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
276 unsigned Insn,
277 uint64_t Address,
278 const void *Decoder);
279
Jozef Kolek12c69822014-12-23 16:16:33 +0000280static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
281 unsigned Insn,
282 uint64_t Address,
283 const void *Decoder);
284
Vladimir Medicdde3d582013-09-06 12:30:36 +0000285static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
286 unsigned Insn,
287 uint64_t Address,
288 const void *Decoder);
289
290static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
291 unsigned Insn,
292 uint64_t Address,
293 const void *Decoder);
294
Akira Hatanaka71928e62012-04-17 18:03:21 +0000295static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
296 uint64_t Address,
297 const void *Decoder);
298
Daniel Sanders92db6b72014-10-01 08:26:55 +0000299static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
300 uint64_t Address,
301 const void *Decoder);
302
303static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
304 uint64_t Address,
305 const void *Decoder);
306
Vladimir Medic435cf8a2015-01-21 10:47:36 +0000307static DecodeStatus DecodeFMemCop2R6(MCInst &Inst, unsigned Insn,
308 uint64_t Address,
309 const void *Decoder);
310
Daniel Sanders6a803f62014-06-16 13:13:03 +0000311static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
312 unsigned Insn,
313 uint64_t Address,
314 const void *Decoder);
315
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000316static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
317 unsigned Value,
318 uint64_t Address,
319 const void *Decoder);
320
321static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
322 unsigned Value,
323 uint64_t Address,
324 const void *Decoder);
325
326static DecodeStatus DecodeLiSimm7(MCInst &Inst,
327 unsigned Value,
328 uint64_t Address,
329 const void *Decoder);
330
331static DecodeStatus DecodeSimm4(MCInst &Inst,
332 unsigned Value,
333 uint64_t Address,
334 const void *Decoder);
335
Akira Hatanaka71928e62012-04-17 18:03:21 +0000336static DecodeStatus DecodeSimm16(MCInst &Inst,
337 unsigned Insn,
338 uint64_t Address,
339 const void *Decoder);
340
Matheus Almeida779c5932013-11-18 12:32:49 +0000341// Decode the immediate field of an LSA instruction which
342// is off by one.
343static DecodeStatus DecodeLSAImm(MCInst &Inst,
344 unsigned Insn,
345 uint64_t Address,
346 const void *Decoder);
347
Akira Hatanaka71928e62012-04-17 18:03:21 +0000348static DecodeStatus DecodeInsSize(MCInst &Inst,
349 unsigned Insn,
350 uint64_t Address,
351 const void *Decoder);
352
353static DecodeStatus DecodeExtSize(MCInst &Inst,
354 unsigned Insn,
355 uint64_t Address,
356 const void *Decoder);
357
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000358static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
359 uint64_t Address, const void *Decoder);
360
Zoran Jovanovic28551422014-06-09 09:49:51 +0000361static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
362 uint64_t Address, const void *Decoder);
363
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000364static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
365 uint64_t Address, const void *Decoder);
366
367static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
368 uint64_t Address, const void *Decoder);
369
370static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
371 uint64_t Address, const void *Decoder);
372
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000373static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
374 uint64_t Address, const void *Decoder);
375
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000376/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
377/// handle.
378template <typename InsnType>
379static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
380 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000381
382template <typename InsnType>
383static DecodeStatus
384DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
385 const void *Decoder);
386
387template <typename InsnType>
388static DecodeStatus
389DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
390 const void *Decoder);
391
392template <typename InsnType>
393static DecodeStatus
394DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
395 const void *Decoder);
396
397template <typename InsnType>
398static DecodeStatus
399DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
400 const void *Decoder);
401
402template <typename InsnType>
403static DecodeStatus
404DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
405 const void *Decoder);
406
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000407template <typename InsnType>
408static DecodeStatus
409DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
410 const void *Decoder);
411
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000412static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
413 uint64_t Address,
414 const void *Decoder);
415
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000416static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
417 uint64_t Address,
418 const void *Decoder);
419
Akira Hatanaka71928e62012-04-17 18:03:21 +0000420namespace llvm {
421extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
422 TheMips64elTarget;
423}
424
425static MCDisassembler *createMipsDisassembler(
426 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000427 const MCSubtargetInfo &STI,
428 MCContext &Ctx) {
429 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000430}
431
432static MCDisassembler *createMipselDisassembler(
433 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000434 const MCSubtargetInfo &STI,
435 MCContext &Ctx) {
436 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000437}
438
439static MCDisassembler *createMips64Disassembler(
440 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000441 const MCSubtargetInfo &STI,
442 MCContext &Ctx) {
443 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000444}
445
446static MCDisassembler *createMips64elDisassembler(
447 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000448 const MCSubtargetInfo &STI,
449 MCContext &Ctx) {
450 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000451}
452
453extern "C" void LLVMInitializeMipsDisassembler() {
454 // Register the disassembler.
455 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
456 createMipsDisassembler);
457 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
458 createMipselDisassembler);
459 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
460 createMips64Disassembler);
461 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
462 createMips64elDisassembler);
463}
464
Akira Hatanaka71928e62012-04-17 18:03:21 +0000465#include "MipsGenDisassemblerTables.inc"
466
Daniel Sanders5c582b22014-05-22 11:23:21 +0000467static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
468 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
469 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
470 return *(RegInfo->getRegClass(RC).begin() + RegNo);
471}
472
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000473template <typename InsnType>
474static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
475 const void *Decoder) {
476 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
477 // The size of the n field depends on the element size
478 // The register class also depends on this.
479 InsnType tmp = fieldFromInstruction(insn, 17, 5);
480 unsigned NSize = 0;
481 DecodeFN RegDecoder = nullptr;
482 if ((tmp & 0x18) == 0x00) { // INSVE_B
483 NSize = 4;
484 RegDecoder = DecodeMSA128BRegisterClass;
485 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
486 NSize = 3;
487 RegDecoder = DecodeMSA128HRegisterClass;
488 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
489 NSize = 2;
490 RegDecoder = DecodeMSA128WRegisterClass;
491 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
492 NSize = 1;
493 RegDecoder = DecodeMSA128DRegisterClass;
494 } else
495 llvm_unreachable("Invalid encoding");
496
497 assert(NSize != 0 && RegDecoder != nullptr);
498
499 // $wd
500 tmp = fieldFromInstruction(insn, 6, 5);
501 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
502 return MCDisassembler::Fail;
503 // $wd_in
504 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
505 return MCDisassembler::Fail;
506 // $n
507 tmp = fieldFromInstruction(insn, 16, NSize);
508 MI.addOperand(MCOperand::CreateImm(tmp));
509 // $ws
510 tmp = fieldFromInstruction(insn, 11, 5);
511 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
512 return MCDisassembler::Fail;
513 // $n2
514 MI.addOperand(MCOperand::CreateImm(0));
515
516 return MCDisassembler::Success;
517}
518
Daniel Sanders5c582b22014-05-22 11:23:21 +0000519template <typename InsnType>
520static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
521 uint64_t Address,
522 const void *Decoder) {
523 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
524 // (otherwise we would have matched the ADDI instruction from the earlier
525 // ISA's instead).
526 //
527 // We have:
528 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
529 // BOVC if rs >= rt
530 // BEQZALC if rs == 0 && rt != 0
531 // BEQC if rs < rt && rs != 0
532
533 InsnType Rs = fieldFromInstruction(insn, 21, 5);
534 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000535 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000536 bool HasRs = false;
537
538 if (Rs >= Rt) {
539 MI.setOpcode(Mips::BOVC);
540 HasRs = true;
541 } else if (Rs != 0 && Rs < Rt) {
542 MI.setOpcode(Mips::BEQC);
543 HasRs = true;
544 } else
545 MI.setOpcode(Mips::BEQZALC);
546
547 if (HasRs)
548 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
549 Rs)));
550
551 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
552 Rt)));
553 MI.addOperand(MCOperand::CreateImm(Imm));
554
555 return MCDisassembler::Success;
556}
557
558template <typename InsnType>
559static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
560 uint64_t Address,
561 const void *Decoder) {
562 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
563 // (otherwise we would have matched the ADDI instruction from the earlier
564 // ISA's instead).
565 //
566 // We have:
567 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
568 // BNVC if rs >= rt
569 // BNEZALC if rs == 0 && rt != 0
570 // BNEC if rs < rt && rs != 0
571
572 InsnType Rs = fieldFromInstruction(insn, 21, 5);
573 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000574 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000575 bool HasRs = false;
576
577 if (Rs >= Rt) {
578 MI.setOpcode(Mips::BNVC);
579 HasRs = true;
580 } else if (Rs != 0 && Rs < Rt) {
581 MI.setOpcode(Mips::BNEC);
582 HasRs = true;
583 } else
584 MI.setOpcode(Mips::BNEZALC);
585
586 if (HasRs)
587 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
588 Rs)));
589
590 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
591 Rt)));
592 MI.addOperand(MCOperand::CreateImm(Imm));
593
594 return MCDisassembler::Success;
595}
596
597template <typename InsnType>
598static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
599 uint64_t Address,
600 const void *Decoder) {
601 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
602 // (otherwise we would have matched the BLEZL instruction from the earlier
603 // ISA's instead).
604 //
605 // We have:
606 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
607 // Invalid if rs == 0
608 // BLEZC if rs == 0 && rt != 0
609 // BGEZC if rs == rt && rt != 0
610 // BGEC if rs != rt && rs != 0 && rt != 0
611
612 InsnType Rs = fieldFromInstruction(insn, 21, 5);
613 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000614 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000615 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000616
617 if (Rt == 0)
618 return MCDisassembler::Fail;
619 else if (Rs == 0)
620 MI.setOpcode(Mips::BLEZC);
621 else if (Rs == Rt)
622 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000623 else {
624 HasRs = true;
625 MI.setOpcode(Mips::BGEC);
626 }
627
628 if (HasRs)
629 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
630 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000631
632 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
633 Rt)));
634
635 MI.addOperand(MCOperand::CreateImm(Imm));
636
637 return MCDisassembler::Success;
638}
639
640template <typename InsnType>
641static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
642 uint64_t Address,
643 const void *Decoder) {
644 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
645 // (otherwise we would have matched the BGTZL instruction from the earlier
646 // ISA's instead).
647 //
648 // We have:
649 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
650 // Invalid if rs == 0
651 // BGTZC if rs == 0 && rt != 0
652 // BLTZC if rs == rt && rt != 0
653 // BLTC if rs != rt && rs != 0 && rt != 0
654
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000655 bool HasRs = false;
656
Daniel Sanders5c582b22014-05-22 11:23:21 +0000657 InsnType Rs = fieldFromInstruction(insn, 21, 5);
658 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000659 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000660
661 if (Rt == 0)
662 return MCDisassembler::Fail;
663 else if (Rs == 0)
664 MI.setOpcode(Mips::BGTZC);
665 else if (Rs == Rt)
666 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000667 else {
668 MI.setOpcode(Mips::BLTC);
669 HasRs = true;
670 }
671
672 if (HasRs)
673 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
674 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000675
676 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
677 Rt)));
678
679 MI.addOperand(MCOperand::CreateImm(Imm));
680
681 return MCDisassembler::Success;
682}
683
684template <typename InsnType>
685static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
686 uint64_t Address,
687 const void *Decoder) {
688 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
689 // (otherwise we would have matched the BGTZ instruction from the earlier
690 // ISA's instead).
691 //
692 // We have:
693 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
694 // BGTZ if rt == 0
695 // BGTZALC if rs == 0 && rt != 0
696 // BLTZALC if rs != 0 && rs == rt
697 // BLTUC if rs != 0 && rs != rt
698
699 InsnType Rs = fieldFromInstruction(insn, 21, 5);
700 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000701 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000702 bool HasRs = false;
703 bool HasRt = false;
704
705 if (Rt == 0) {
706 MI.setOpcode(Mips::BGTZ);
707 HasRs = true;
708 } else if (Rs == 0) {
709 MI.setOpcode(Mips::BGTZALC);
710 HasRt = true;
711 } else if (Rs == Rt) {
712 MI.setOpcode(Mips::BLTZALC);
713 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000714 } else {
715 MI.setOpcode(Mips::BLTUC);
716 HasRs = true;
717 HasRt = true;
718 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000719
720 if (HasRs)
721 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
722 Rs)));
723
724 if (HasRt)
725 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
726 Rt)));
727
728 MI.addOperand(MCOperand::CreateImm(Imm));
729
730 return MCDisassembler::Success;
731}
732
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000733template <typename InsnType>
734static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
735 uint64_t Address,
736 const void *Decoder) {
737 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
738 // (otherwise we would have matched the BLEZL instruction from the earlier
739 // ISA's instead).
740 //
741 // We have:
742 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
743 // Invalid if rs == 0
744 // BLEZALC if rs == 0 && rt != 0
745 // BGEZALC if rs == rt && rt != 0
746 // BGEUC if rs != rt && rs != 0 && rt != 0
747
748 InsnType Rs = fieldFromInstruction(insn, 21, 5);
749 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000750 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000751 bool HasRs = false;
752
753 if (Rt == 0)
754 return MCDisassembler::Fail;
755 else if (Rs == 0)
756 MI.setOpcode(Mips::BLEZALC);
757 else if (Rs == Rt)
758 MI.setOpcode(Mips::BGEZALC);
759 else {
760 HasRs = true;
761 MI.setOpcode(Mips::BGEUC);
762 }
763
764 if (HasRs)
765 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
766 Rs)));
767 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
768 Rt)));
769
770 MI.addOperand(MCOperand::CreateImm(Imm));
771
772 return MCDisassembler::Success;
773}
774
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000775/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
776/// according to the given endianess.
777static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
778 uint64_t &Size, uint32_t &Insn,
779 bool IsBigEndian) {
780 // We want to read exactly 2 Bytes of data.
781 if (Bytes.size() < 2) {
782 Size = 0;
783 return MCDisassembler::Fail;
784 }
785
786 if (IsBigEndian) {
787 Insn = (Bytes[0] << 8) | Bytes[1];
788 } else {
789 Insn = (Bytes[1] << 8) | Bytes[0];
790 }
791
792 return MCDisassembler::Success;
793}
794
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000795/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000796/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000797static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
798 uint64_t &Size, uint32_t &Insn,
799 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000800 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000801 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000802 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000803 return MCDisassembler::Fail;
804 }
805
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000806 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
807 // always precede the low 16 bits in the instruction stream (that is, they
808 // are placed at lower addresses in the instruction stream).
809 //
810 // microMIPS byte ordering:
811 // Big-endian: 0 | 1 | 2 | 3
812 // Little-endian: 1 | 0 | 3 | 2
813
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000814 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000815 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000816 Insn =
817 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
818 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000819 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000820 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000821 (Bytes[1] << 24);
822 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000823 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000824 (Bytes[3] << 24);
825 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000826 }
827
828 return MCDisassembler::Success;
829}
830
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000831DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000832 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000833 uint64_t Address,
834 raw_ostream &VStream,
835 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000836 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000837 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000838
Vladimir Medicdde3d582013-09-06 12:30:36 +0000839 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000840 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
841
842 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
843 // Calling the auto-generated decoder function.
844 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
845 this, STI);
846 if (Result != MCDisassembler::Fail) {
847 Size = 2;
848 return Result;
849 }
850
851 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
852 if (Result == MCDisassembler::Fail)
853 return MCDisassembler::Fail;
854
855 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000856 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000857 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000858 this, STI);
859 if (Result != MCDisassembler::Fail) {
860 Size = 4;
861 return Result;
862 }
863 return MCDisassembler::Fail;
864 }
865
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000866 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
867 if (Result == MCDisassembler::Fail)
868 return MCDisassembler::Fail;
869
Daniel Sandersc171f652014-06-13 13:15:59 +0000870 if (hasCOP3()) {
871 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
872 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000873 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000874 if (Result != MCDisassembler::Fail) {
875 Size = 4;
876 return Result;
877 }
878 }
879
880 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000881 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000882 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000883 Address, this, STI);
884 if (Result != MCDisassembler::Fail) {
885 Size = 4;
886 return Result;
887 }
888 }
889
Daniel Sandersc171f652014-06-13 13:15:59 +0000890 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000891 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000892 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000893 Address, this, STI);
894 if (Result != MCDisassembler::Fail) {
895 Size = 4;
896 return Result;
897 }
898 }
899
Daniel Sanders0fa60412014-06-12 13:39:06 +0000900 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000901 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000902 Result =
903 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000904 if (Result != MCDisassembler::Fail) {
905 Size = 4;
906 return Result;
907 }
908
909 return MCDisassembler::Fail;
910}
911
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000912DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000913 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000914 uint64_t Address,
915 raw_ostream &VStream,
916 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000917 uint32_t Insn;
918
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000919 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000920 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000921 if (Result == MCDisassembler::Fail)
922 return MCDisassembler::Fail;
923
924 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000925 Result =
926 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000927 if (Result != MCDisassembler::Fail) {
928 Size = 4;
929 return Result;
930 }
931 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000932 Result =
933 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000934 if (Result != MCDisassembler::Fail) {
935 Size = 4;
936 return Result;
937 }
938
939 return MCDisassembler::Fail;
940}
941
Reed Kotlerec8a5492013-02-14 03:05:25 +0000942static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
943 unsigned RegNo,
944 uint64_t Address,
945 const void *Decoder) {
946
947 return MCDisassembler::Fail;
948
949}
950
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000951static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
952 unsigned RegNo,
953 uint64_t Address,
954 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000955
956 if (RegNo > 31)
957 return MCDisassembler::Fail;
958
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000959 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000960 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000961 return MCDisassembler::Success;
962}
963
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000964static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
965 unsigned RegNo,
966 uint64_t Address,
967 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000968 if (RegNo > 7)
969 return MCDisassembler::Fail;
970 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
971 Inst.addOperand(MCOperand::CreateReg(Reg));
972 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000973}
974
Jozef Kolek1904fa22014-11-24 14:25:53 +0000975static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
976 unsigned RegNo,
977 uint64_t Address,
978 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000979 if (RegNo > 7)
980 return MCDisassembler::Fail;
981 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
982 Inst.addOperand(MCOperand::CreateReg(Reg));
983 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000984}
985
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000986static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
987 unsigned RegNo,
988 uint64_t Address,
989 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000990 if (RegNo > 31)
991 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000992 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000993 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000994 return MCDisassembler::Success;
995}
996
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000997static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
998 unsigned RegNo,
999 uint64_t Address,
1000 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +00001001 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +00001002 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1003
1004 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1005}
1006
Akira Hatanaka654655f2013-08-14 00:53:38 +00001007static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1008 unsigned RegNo,
1009 uint64_t Address,
1010 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001011 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001012}
1013
Akira Hatanaka71928e62012-04-17 18:03:21 +00001014static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1015 unsigned RegNo,
1016 uint64_t Address,
1017 const void *Decoder) {
1018 if (RegNo > 31)
1019 return MCDisassembler::Fail;
1020
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001021 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1022 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001023 return MCDisassembler::Success;
1024}
1025
1026static DecodeStatus DecodeFGR32RegisterClass(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::FGR32RegClassID, RegNo);
1034 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001035 return MCDisassembler::Success;
1036}
1037
1038static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1039 unsigned RegNo,
1040 uint64_t Address,
1041 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001042 if (RegNo > 31)
1043 return MCDisassembler::Fail;
1044 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1045 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001046 return MCDisassembler::Success;
1047}
1048
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001049static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1050 unsigned RegNo,
1051 uint64_t Address,
1052 const void *Decoder) {
1053 if (RegNo > 7)
1054 return MCDisassembler::Fail;
1055 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1056 Inst.addOperand(MCOperand::CreateReg(Reg));
1057 return MCDisassembler::Success;
1058}
1059
Daniel Sanders0fa60412014-06-12 13:39:06 +00001060static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1061 uint64_t Address,
1062 const void *Decoder) {
1063 if (RegNo > 31)
1064 return MCDisassembler::Fail;
1065
1066 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1067 Inst.addOperand(MCOperand::CreateReg(Reg));
1068 return MCDisassembler::Success;
1069}
1070
Akira Hatanaka71928e62012-04-17 18:03:21 +00001071static DecodeStatus DecodeMem(MCInst &Inst,
1072 unsigned Insn,
1073 uint64_t Address,
1074 const void *Decoder) {
1075 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001076 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1077 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001078
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001079 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1080 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001081
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001082 if(Inst.getOpcode() == Mips::SC ||
1083 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001084 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001085 }
1086
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001087 Inst.addOperand(MCOperand::CreateReg(Reg));
1088 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001089 Inst.addOperand(MCOperand::CreateImm(Offset));
1090
1091 return MCDisassembler::Success;
1092}
1093
Daniel Sanders92db6b72014-10-01 08:26:55 +00001094static DecodeStatus DecodeCacheOp(MCInst &Inst,
1095 unsigned Insn,
1096 uint64_t Address,
1097 const void *Decoder) {
1098 int Offset = SignExtend32<16>(Insn & 0xffff);
1099 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1100 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1101
1102 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1103
1104 Inst.addOperand(MCOperand::CreateReg(Base));
1105 Inst.addOperand(MCOperand::CreateImm(Offset));
1106 Inst.addOperand(MCOperand::CreateImm(Hint));
1107
1108 return MCDisassembler::Success;
1109}
1110
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001111static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1112 unsigned Insn,
1113 uint64_t Address,
1114 const void *Decoder) {
1115 int Offset = SignExtend32<12>(Insn & 0xfff);
1116 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1117 unsigned Hint = 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
Daniel Sandersb4484d62014-11-27 17:28:10 +00001128static DecodeStatus DecodeSyncI(MCInst &Inst,
1129 unsigned Insn,
1130 uint64_t Address,
1131 const void *Decoder) {
1132 int Offset = SignExtend32<16>(Insn & 0xffff);
1133 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1134
1135 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1136
1137 Inst.addOperand(MCOperand::CreateReg(Base));
1138 Inst.addOperand(MCOperand::CreateImm(Offset));
1139
1140 return MCDisassembler::Success;
1141}
1142
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001143static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1144 uint64_t Address, const void *Decoder) {
1145 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1146 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1147 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1148
1149 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1150 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1151
1152 Inst.addOperand(MCOperand::CreateReg(Reg));
1153 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001154
1155 // The immediate field of an LD/ST instruction is scaled which means it must
1156 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1157 // data format.
1158 // .b - 1 byte
1159 // .h - 2 bytes
1160 // .w - 4 bytes
1161 // .d - 8 bytes
1162 switch(Inst.getOpcode())
1163 {
1164 default:
1165 assert (0 && "Unexpected instruction");
1166 return MCDisassembler::Fail;
1167 break;
1168 case Mips::LD_B:
1169 case Mips::ST_B:
1170 Inst.addOperand(MCOperand::CreateImm(Offset));
1171 break;
1172 case Mips::LD_H:
1173 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001174 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001175 break;
1176 case Mips::LD_W:
1177 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001178 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001179 break;
1180 case Mips::LD_D:
1181 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001182 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001183 break;
1184 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001185
1186 return MCDisassembler::Success;
1187}
1188
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001189static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1190 unsigned Insn,
1191 uint64_t Address,
1192 const void *Decoder) {
1193 unsigned Offset = Insn & 0xf;
1194 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1195 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1196
1197 switch (Inst.getOpcode()) {
1198 case Mips::LBU16_MM:
1199 case Mips::LHU16_MM:
1200 case Mips::LW16_MM:
1201 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1202 == MCDisassembler::Fail)
1203 return MCDisassembler::Fail;
1204 break;
1205 case Mips::SB16_MM:
1206 case Mips::SH16_MM:
1207 case Mips::SW16_MM:
1208 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1209 == MCDisassembler::Fail)
1210 return MCDisassembler::Fail;
1211 break;
1212 }
1213
1214 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1215 == MCDisassembler::Fail)
1216 return MCDisassembler::Fail;
1217
1218 switch (Inst.getOpcode()) {
1219 case Mips::LBU16_MM:
1220 if (Offset == 0xf)
1221 Inst.addOperand(MCOperand::CreateImm(-1));
1222 else
1223 Inst.addOperand(MCOperand::CreateImm(Offset));
1224 break;
1225 case Mips::SB16_MM:
1226 Inst.addOperand(MCOperand::CreateImm(Offset));
1227 break;
1228 case Mips::LHU16_MM:
1229 case Mips::SH16_MM:
1230 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1231 break;
1232 case Mips::LW16_MM:
1233 case Mips::SW16_MM:
1234 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1235 break;
1236 }
1237
1238 return MCDisassembler::Success;
1239}
1240
Jozef Kolek12c69822014-12-23 16:16:33 +00001241static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1242 unsigned Insn,
1243 uint64_t Address,
1244 const void *Decoder) {
1245 unsigned Offset = Insn & 0x1F;
1246 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1247
1248 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1249
1250 Inst.addOperand(MCOperand::CreateReg(Reg));
1251 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1252 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1253
1254 return MCDisassembler::Success;
1255}
1256
Vladimir Medicdde3d582013-09-06 12:30:36 +00001257static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1258 unsigned Insn,
1259 uint64_t Address,
1260 const void *Decoder) {
1261 int Offset = SignExtend32<12>(Insn & 0x0fff);
1262 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1263 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1264
1265 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1266 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1267
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001268 switch (Inst.getOpcode()) {
1269 case Mips::SWM32_MM:
1270 case Mips::LWM32_MM:
1271 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1272 == MCDisassembler::Fail)
1273 return MCDisassembler::Fail;
1274 Inst.addOperand(MCOperand::CreateReg(Base));
1275 Inst.addOperand(MCOperand::CreateImm(Offset));
1276 break;
1277 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001278 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001279 // fallthrough
1280 default:
1281 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001282 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1283 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1284
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001285 Inst.addOperand(MCOperand::CreateReg(Base));
1286 Inst.addOperand(MCOperand::CreateImm(Offset));
1287 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001288
1289 return MCDisassembler::Success;
1290}
1291
1292static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1293 unsigned Insn,
1294 uint64_t Address,
1295 const void *Decoder) {
1296 int Offset = SignExtend32<16>(Insn & 0xffff);
1297 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1298 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1299
1300 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1301 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1302
1303 Inst.addOperand(MCOperand::CreateReg(Reg));
1304 Inst.addOperand(MCOperand::CreateReg(Base));
1305 Inst.addOperand(MCOperand::CreateImm(Offset));
1306
1307 return MCDisassembler::Success;
1308}
1309
Akira Hatanaka71928e62012-04-17 18:03:21 +00001310static DecodeStatus DecodeFMem(MCInst &Inst,
1311 unsigned Insn,
1312 uint64_t Address,
1313 const void *Decoder) {
1314 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001315 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1316 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001317
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001318 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001319 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001320
1321 Inst.addOperand(MCOperand::CreateReg(Reg));
1322 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001323 Inst.addOperand(MCOperand::CreateImm(Offset));
1324
1325 return MCDisassembler::Success;
1326}
1327
Daniel Sanders92db6b72014-10-01 08:26:55 +00001328static DecodeStatus DecodeFMem2(MCInst &Inst,
1329 unsigned Insn,
1330 uint64_t Address,
1331 const void *Decoder) {
1332 int Offset = SignExtend32<16>(Insn & 0xffff);
1333 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1334 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1335
1336 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1337 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1338
1339 Inst.addOperand(MCOperand::CreateReg(Reg));
1340 Inst.addOperand(MCOperand::CreateReg(Base));
1341 Inst.addOperand(MCOperand::CreateImm(Offset));
1342
1343 return MCDisassembler::Success;
1344}
1345
1346static DecodeStatus DecodeFMem3(MCInst &Inst,
1347 unsigned Insn,
1348 uint64_t Address,
1349 const void *Decoder) {
1350 int Offset = SignExtend32<16>(Insn & 0xffff);
1351 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1352 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1353
1354 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1355 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1356
1357 Inst.addOperand(MCOperand::CreateReg(Reg));
1358 Inst.addOperand(MCOperand::CreateReg(Base));
1359 Inst.addOperand(MCOperand::CreateImm(Offset));
1360
1361 return MCDisassembler::Success;
1362}
1363
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001364static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1365 unsigned Insn,
1366 uint64_t Address,
1367 const void *Decoder) {
1368 int Offset = SignExtend32<11>(Insn & 0x07ff);
1369 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1370 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1371
1372 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1373 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1374
1375 Inst.addOperand(MCOperand::CreateReg(Reg));
1376 Inst.addOperand(MCOperand::CreateReg(Base));
1377 Inst.addOperand(MCOperand::CreateImm(Offset));
1378
1379 return MCDisassembler::Success;
1380}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001381static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1382 unsigned Insn,
1383 uint64_t Address,
1384 const void *Decoder) {
1385 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1386 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1387 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1388
1389 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1390 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1391
1392 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1393 Inst.addOperand(MCOperand::CreateReg(Rt));
1394 }
1395
1396 Inst.addOperand(MCOperand::CreateReg(Rt));
1397 Inst.addOperand(MCOperand::CreateReg(Base));
1398 Inst.addOperand(MCOperand::CreateImm(Offset));
1399
1400 return MCDisassembler::Success;
1401}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001402
1403static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1404 unsigned RegNo,
1405 uint64_t Address,
1406 const void *Decoder) {
1407 // Currently only hardware register 29 is supported.
1408 if (RegNo != 29)
1409 return MCDisassembler::Fail;
1410 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1411 return MCDisassembler::Success;
1412}
1413
Akira Hatanaka71928e62012-04-17 18:03:21 +00001414static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1415 unsigned RegNo,
1416 uint64_t Address,
1417 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001418 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001419 return MCDisassembler::Fail;
1420
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001421 ;
1422 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1423 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001424 return MCDisassembler::Success;
1425}
1426
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001427static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1428 unsigned RegNo,
1429 uint64_t Address,
1430 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001431 if (RegNo >= 4)
1432 return MCDisassembler::Fail;
1433
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001434 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001435 Inst.addOperand(MCOperand::CreateReg(Reg));
1436 return MCDisassembler::Success;
1437}
1438
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001439static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1440 unsigned RegNo,
1441 uint64_t Address,
1442 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001443 if (RegNo >= 4)
1444 return MCDisassembler::Fail;
1445
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001446 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001447 Inst.addOperand(MCOperand::CreateReg(Reg));
1448 return MCDisassembler::Success;
1449}
1450
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001451static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1452 unsigned RegNo,
1453 uint64_t Address,
1454 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001455 if (RegNo >= 4)
1456 return MCDisassembler::Fail;
1457
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001458 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001459 Inst.addOperand(MCOperand::CreateReg(Reg));
1460 return MCDisassembler::Success;
1461}
1462
Jack Carter3eb663b2013-09-26 00:09:46 +00001463static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1464 unsigned RegNo,
1465 uint64_t Address,
1466 const void *Decoder) {
1467 if (RegNo > 31)
1468 return MCDisassembler::Fail;
1469
1470 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1471 Inst.addOperand(MCOperand::CreateReg(Reg));
1472 return MCDisassembler::Success;
1473}
1474
Jack Carter5dc8ac92013-09-25 23:50:44 +00001475static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1476 unsigned RegNo,
1477 uint64_t Address,
1478 const void *Decoder) {
1479 if (RegNo > 31)
1480 return MCDisassembler::Fail;
1481
1482 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1483 Inst.addOperand(MCOperand::CreateReg(Reg));
1484 return MCDisassembler::Success;
1485}
1486
1487static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1488 unsigned RegNo,
1489 uint64_t Address,
1490 const void *Decoder) {
1491 if (RegNo > 31)
1492 return MCDisassembler::Fail;
1493
1494 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1495 Inst.addOperand(MCOperand::CreateReg(Reg));
1496 return MCDisassembler::Success;
1497}
1498
1499static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1500 unsigned RegNo,
1501 uint64_t Address,
1502 const void *Decoder) {
1503 if (RegNo > 31)
1504 return MCDisassembler::Fail;
1505
1506 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1507 Inst.addOperand(MCOperand::CreateReg(Reg));
1508 return MCDisassembler::Success;
1509}
1510
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001511static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1512 unsigned RegNo,
1513 uint64_t Address,
1514 const void *Decoder) {
1515 if (RegNo > 7)
1516 return MCDisassembler::Fail;
1517
1518 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1519 Inst.addOperand(MCOperand::CreateReg(Reg));
1520 return MCDisassembler::Success;
1521}
1522
Daniel Sanders2a83d682014-05-21 12:56:39 +00001523static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1524 unsigned RegNo,
1525 uint64_t Address,
1526 const void *Decoder) {
1527 if (RegNo > 31)
1528 return MCDisassembler::Fail;
1529
1530 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1531 Inst.addOperand(MCOperand::CreateReg(Reg));
1532 return MCDisassembler::Success;
1533}
1534
Akira Hatanaka71928e62012-04-17 18:03:21 +00001535static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1536 unsigned Offset,
1537 uint64_t Address,
1538 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001539 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001540 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1541 return MCDisassembler::Success;
1542}
1543
Akira Hatanaka71928e62012-04-17 18:03:21 +00001544static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1545 unsigned Insn,
1546 uint64_t Address,
1547 const void *Decoder) {
1548
Jim Grosbachecaef492012-08-14 19:06:05 +00001549 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001550 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1551 return MCDisassembler::Success;
1552}
1553
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001554static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1555 unsigned Offset,
1556 uint64_t Address,
1557 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001558 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001559
1560 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1561 return MCDisassembler::Success;
1562}
1563
1564static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1565 unsigned Offset,
1566 uint64_t Address,
1567 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001568 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001569
1570 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1571 return MCDisassembler::Success;
1572}
1573
Jozef Kolek9761e962015-01-12 12:03:34 +00001574static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1575 unsigned Offset,
1576 uint64_t Address,
1577 const void *Decoder) {
1578 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1579 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1580 return MCDisassembler::Success;
1581}
1582
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001583static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1584 unsigned Offset,
1585 uint64_t Address,
1586 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001587 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001588 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1589 return MCDisassembler::Success;
1590}
1591
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001592static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1593 unsigned Insn,
1594 uint64_t Address,
1595 const void *Decoder) {
1596 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1597 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1598 return MCDisassembler::Success;
1599}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001600
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001601static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1602 unsigned Value,
1603 uint64_t Address,
1604 const void *Decoder) {
1605 if (Value == 0)
1606 Inst.addOperand(MCOperand::CreateImm(1));
1607 else if (Value == 0x7)
1608 Inst.addOperand(MCOperand::CreateImm(-1));
1609 else
1610 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1611 return MCDisassembler::Success;
1612}
1613
1614static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1615 unsigned Value,
1616 uint64_t Address,
1617 const void *Decoder) {
1618 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1619 return MCDisassembler::Success;
1620}
1621
1622static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1623 unsigned Value,
1624 uint64_t Address,
1625 const void *Decoder) {
1626 if (Value == 0x7F)
1627 Inst.addOperand(MCOperand::CreateImm(-1));
1628 else
1629 Inst.addOperand(MCOperand::CreateImm(Value));
1630 return MCDisassembler::Success;
1631}
1632
1633static DecodeStatus DecodeSimm4(MCInst &Inst,
1634 unsigned Value,
1635 uint64_t Address,
1636 const void *Decoder) {
1637 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1638 return MCDisassembler::Success;
1639}
1640
Akira Hatanaka71928e62012-04-17 18:03:21 +00001641static DecodeStatus DecodeSimm16(MCInst &Inst,
1642 unsigned Insn,
1643 uint64_t Address,
1644 const void *Decoder) {
1645 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1646 return MCDisassembler::Success;
1647}
1648
Matheus Almeida779c5932013-11-18 12:32:49 +00001649static DecodeStatus DecodeLSAImm(MCInst &Inst,
1650 unsigned Insn,
1651 uint64_t Address,
1652 const void *Decoder) {
1653 // We add one to the immediate field as it was encoded as 'imm - 1'.
1654 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1655 return MCDisassembler::Success;
1656}
1657
Akira Hatanaka71928e62012-04-17 18:03:21 +00001658static DecodeStatus DecodeInsSize(MCInst &Inst,
1659 unsigned Insn,
1660 uint64_t Address,
1661 const void *Decoder) {
1662 // First we need to grab the pos(lsb) from MCInst.
1663 int Pos = Inst.getOperand(2).getImm();
1664 int Size = (int) Insn - Pos + 1;
1665 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1666 return MCDisassembler::Success;
1667}
1668
1669static DecodeStatus DecodeExtSize(MCInst &Inst,
1670 unsigned Insn,
1671 uint64_t Address,
1672 const void *Decoder) {
1673 int Size = (int) Insn + 1;
1674 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1675 return MCDisassembler::Success;
1676}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001677
1678static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1679 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001680 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001681 return MCDisassembler::Success;
1682}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001683
1684static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1685 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001686 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001687 return MCDisassembler::Success;
1688}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001689
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001690static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1691 uint64_t Address, const void *Decoder) {
1692 int32_t DecodedValue;
1693 switch (Insn) {
1694 case 0: DecodedValue = 256; break;
1695 case 1: DecodedValue = 257; break;
1696 case 510: DecodedValue = -258; break;
1697 case 511: DecodedValue = -257; break;
1698 default: DecodedValue = SignExtend32<9>(Insn); break;
1699 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001700 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001701 return MCDisassembler::Success;
1702}
1703
1704static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1705 uint64_t Address, const void *Decoder) {
1706 // Insn must be >= 0, since it is unsigned that condition is always true.
1707 assert(Insn < 16);
1708 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1709 255, 32768, 65535};
1710 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1711 return MCDisassembler::Success;
1712}
1713
1714static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1715 uint64_t Address, const void *Decoder) {
1716 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1717 return MCDisassembler::Success;
1718}
1719
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001720static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1721 unsigned Insn,
1722 uint64_t Address,
1723 const void *Decoder) {
1724 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1725 Mips::S6, Mips::FP};
1726 unsigned RegNum;
1727
1728 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1729 // Empty register lists are not allowed.
1730 if (RegLst == 0)
1731 return MCDisassembler::Fail;
1732
1733 RegNum = RegLst & 0xf;
1734 for (unsigned i = 0; i < RegNum; i++)
1735 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1736
1737 if (RegLst & 0x10)
1738 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1739
1740 return MCDisassembler::Success;
1741}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001742
1743static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1744 uint64_t Address,
1745 const void *Decoder) {
1746 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1747 unsigned RegNum;
1748
1749 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1750 // Empty register lists are not allowed.
1751 if (RegLst == 0)
1752 return MCDisassembler::Fail;
1753
1754 RegNum = RegLst & 0x3;
1755 for (unsigned i = 0; i < RegNum - 1; i++)
1756 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1757
1758 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1759
1760 return MCDisassembler::Success;
1761}
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001762
1763static DecodeStatus DecodeSimm23Lsl2(MCInst &Inst, unsigned Insn,
1764 uint64_t Address, const void *Decoder) {
1765 Inst.addOperand(MCOperand::CreateImm(SignExtend32<23>(Insn) << 2));
1766 return MCDisassembler::Success;
1767}