blob: 98fc1ebba1559f20eff4818fe2d2e7516d9773c3 [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
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000373/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
374/// handle.
375template <typename InsnType>
376static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
377 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000378
379template <typename InsnType>
380static DecodeStatus
381DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
382 const void *Decoder);
383
384template <typename InsnType>
385static DecodeStatus
386DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
387 const void *Decoder);
388
389template <typename InsnType>
390static DecodeStatus
391DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
392 const void *Decoder);
393
394template <typename InsnType>
395static DecodeStatus
396DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
397 const void *Decoder);
398
399template <typename InsnType>
400static DecodeStatus
401DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
402 const void *Decoder);
403
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000404template <typename InsnType>
405static DecodeStatus
406DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder);
408
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000409static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
410 uint64_t Address,
411 const void *Decoder);
412
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000413static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
414 uint64_t Address,
415 const void *Decoder);
416
Akira Hatanaka71928e62012-04-17 18:03:21 +0000417namespace llvm {
418extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
419 TheMips64elTarget;
420}
421
422static MCDisassembler *createMipsDisassembler(
423 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000424 const MCSubtargetInfo &STI,
425 MCContext &Ctx) {
426 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000427}
428
429static MCDisassembler *createMipselDisassembler(
430 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000431 const MCSubtargetInfo &STI,
432 MCContext &Ctx) {
433 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000434}
435
436static MCDisassembler *createMips64Disassembler(
437 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000438 const MCSubtargetInfo &STI,
439 MCContext &Ctx) {
440 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000441}
442
443static MCDisassembler *createMips64elDisassembler(
444 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000445 const MCSubtargetInfo &STI,
446 MCContext &Ctx) {
447 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000448}
449
450extern "C" void LLVMInitializeMipsDisassembler() {
451 // Register the disassembler.
452 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
453 createMipsDisassembler);
454 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
455 createMipselDisassembler);
456 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
457 createMips64Disassembler);
458 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
459 createMips64elDisassembler);
460}
461
Akira Hatanaka71928e62012-04-17 18:03:21 +0000462#include "MipsGenDisassemblerTables.inc"
463
Daniel Sanders5c582b22014-05-22 11:23:21 +0000464static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
465 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
466 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
467 return *(RegInfo->getRegClass(RC).begin() + RegNo);
468}
469
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000470template <typename InsnType>
471static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
472 const void *Decoder) {
473 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
474 // The size of the n field depends on the element size
475 // The register class also depends on this.
476 InsnType tmp = fieldFromInstruction(insn, 17, 5);
477 unsigned NSize = 0;
478 DecodeFN RegDecoder = nullptr;
479 if ((tmp & 0x18) == 0x00) { // INSVE_B
480 NSize = 4;
481 RegDecoder = DecodeMSA128BRegisterClass;
482 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
483 NSize = 3;
484 RegDecoder = DecodeMSA128HRegisterClass;
485 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
486 NSize = 2;
487 RegDecoder = DecodeMSA128WRegisterClass;
488 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
489 NSize = 1;
490 RegDecoder = DecodeMSA128DRegisterClass;
491 } else
492 llvm_unreachable("Invalid encoding");
493
494 assert(NSize != 0 && RegDecoder != nullptr);
495
496 // $wd
497 tmp = fieldFromInstruction(insn, 6, 5);
498 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
499 return MCDisassembler::Fail;
500 // $wd_in
501 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
502 return MCDisassembler::Fail;
503 // $n
504 tmp = fieldFromInstruction(insn, 16, NSize);
505 MI.addOperand(MCOperand::CreateImm(tmp));
506 // $ws
507 tmp = fieldFromInstruction(insn, 11, 5);
508 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
509 return MCDisassembler::Fail;
510 // $n2
511 MI.addOperand(MCOperand::CreateImm(0));
512
513 return MCDisassembler::Success;
514}
515
Daniel Sanders5c582b22014-05-22 11:23:21 +0000516template <typename InsnType>
517static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
518 uint64_t Address,
519 const void *Decoder) {
520 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
521 // (otherwise we would have matched the ADDI instruction from the earlier
522 // ISA's instead).
523 //
524 // We have:
525 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
526 // BOVC if rs >= rt
527 // BEQZALC if rs == 0 && rt != 0
528 // BEQC if rs < rt && rs != 0
529
530 InsnType Rs = fieldFromInstruction(insn, 21, 5);
531 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000532 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000533 bool HasRs = false;
534
535 if (Rs >= Rt) {
536 MI.setOpcode(Mips::BOVC);
537 HasRs = true;
538 } else if (Rs != 0 && Rs < Rt) {
539 MI.setOpcode(Mips::BEQC);
540 HasRs = true;
541 } else
542 MI.setOpcode(Mips::BEQZALC);
543
544 if (HasRs)
545 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
546 Rs)));
547
548 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
549 Rt)));
550 MI.addOperand(MCOperand::CreateImm(Imm));
551
552 return MCDisassembler::Success;
553}
554
555template <typename InsnType>
556static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
557 uint64_t Address,
558 const void *Decoder) {
559 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
560 // (otherwise we would have matched the ADDI instruction from the earlier
561 // ISA's instead).
562 //
563 // We have:
564 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
565 // BNVC if rs >= rt
566 // BNEZALC if rs == 0 && rt != 0
567 // BNEC if rs < rt && rs != 0
568
569 InsnType Rs = fieldFromInstruction(insn, 21, 5);
570 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000571 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000572 bool HasRs = false;
573
574 if (Rs >= Rt) {
575 MI.setOpcode(Mips::BNVC);
576 HasRs = true;
577 } else if (Rs != 0 && Rs < Rt) {
578 MI.setOpcode(Mips::BNEC);
579 HasRs = true;
580 } else
581 MI.setOpcode(Mips::BNEZALC);
582
583 if (HasRs)
584 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
585 Rs)));
586
587 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
588 Rt)));
589 MI.addOperand(MCOperand::CreateImm(Imm));
590
591 return MCDisassembler::Success;
592}
593
594template <typename InsnType>
595static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
596 uint64_t Address,
597 const void *Decoder) {
598 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
599 // (otherwise we would have matched the BLEZL instruction from the earlier
600 // ISA's instead).
601 //
602 // We have:
603 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
604 // Invalid if rs == 0
605 // BLEZC if rs == 0 && rt != 0
606 // BGEZC if rs == rt && rt != 0
607 // BGEC if rs != rt && rs != 0 && rt != 0
608
609 InsnType Rs = fieldFromInstruction(insn, 21, 5);
610 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000611 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000612 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000613
614 if (Rt == 0)
615 return MCDisassembler::Fail;
616 else if (Rs == 0)
617 MI.setOpcode(Mips::BLEZC);
618 else if (Rs == Rt)
619 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000620 else {
621 HasRs = true;
622 MI.setOpcode(Mips::BGEC);
623 }
624
625 if (HasRs)
626 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
627 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000628
629 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
630 Rt)));
631
632 MI.addOperand(MCOperand::CreateImm(Imm));
633
634 return MCDisassembler::Success;
635}
636
637template <typename InsnType>
638static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
639 uint64_t Address,
640 const void *Decoder) {
641 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
642 // (otherwise we would have matched the BGTZL instruction from the earlier
643 // ISA's instead).
644 //
645 // We have:
646 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
647 // Invalid if rs == 0
648 // BGTZC if rs == 0 && rt != 0
649 // BLTZC if rs == rt && rt != 0
650 // BLTC if rs != rt && rs != 0 && rt != 0
651
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000652 bool HasRs = false;
653
Daniel Sanders5c582b22014-05-22 11:23:21 +0000654 InsnType Rs = fieldFromInstruction(insn, 21, 5);
655 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000656 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000657
658 if (Rt == 0)
659 return MCDisassembler::Fail;
660 else if (Rs == 0)
661 MI.setOpcode(Mips::BGTZC);
662 else if (Rs == Rt)
663 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000664 else {
665 MI.setOpcode(Mips::BLTC);
666 HasRs = true;
667 }
668
669 if (HasRs)
670 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
671 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000672
673 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
674 Rt)));
675
676 MI.addOperand(MCOperand::CreateImm(Imm));
677
678 return MCDisassembler::Success;
679}
680
681template <typename InsnType>
682static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
683 uint64_t Address,
684 const void *Decoder) {
685 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
686 // (otherwise we would have matched the BGTZ instruction from the earlier
687 // ISA's instead).
688 //
689 // We have:
690 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
691 // BGTZ if rt == 0
692 // BGTZALC if rs == 0 && rt != 0
693 // BLTZALC if rs != 0 && rs == rt
694 // BLTUC if rs != 0 && rs != rt
695
696 InsnType Rs = fieldFromInstruction(insn, 21, 5);
697 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000698 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000699 bool HasRs = false;
700 bool HasRt = false;
701
702 if (Rt == 0) {
703 MI.setOpcode(Mips::BGTZ);
704 HasRs = true;
705 } else if (Rs == 0) {
706 MI.setOpcode(Mips::BGTZALC);
707 HasRt = true;
708 } else if (Rs == Rt) {
709 MI.setOpcode(Mips::BLTZALC);
710 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000711 } else {
712 MI.setOpcode(Mips::BLTUC);
713 HasRs = true;
714 HasRt = true;
715 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000716
717 if (HasRs)
718 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
719 Rs)));
720
721 if (HasRt)
722 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
723 Rt)));
724
725 MI.addOperand(MCOperand::CreateImm(Imm));
726
727 return MCDisassembler::Success;
728}
729
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000730template <typename InsnType>
731static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
732 uint64_t Address,
733 const void *Decoder) {
734 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
735 // (otherwise we would have matched the BLEZL instruction from the earlier
736 // ISA's instead).
737 //
738 // We have:
739 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
740 // Invalid if rs == 0
741 // BLEZALC if rs == 0 && rt != 0
742 // BGEZALC if rs == rt && rt != 0
743 // BGEUC if rs != rt && rs != 0 && rt != 0
744
745 InsnType Rs = fieldFromInstruction(insn, 21, 5);
746 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000747 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000748 bool HasRs = false;
749
750 if (Rt == 0)
751 return MCDisassembler::Fail;
752 else if (Rs == 0)
753 MI.setOpcode(Mips::BLEZALC);
754 else if (Rs == Rt)
755 MI.setOpcode(Mips::BGEZALC);
756 else {
757 HasRs = true;
758 MI.setOpcode(Mips::BGEUC);
759 }
760
761 if (HasRs)
762 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
763 Rs)));
764 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
765 Rt)));
766
767 MI.addOperand(MCOperand::CreateImm(Imm));
768
769 return MCDisassembler::Success;
770}
771
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000772/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
773/// according to the given endianess.
774static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
775 uint64_t &Size, uint32_t &Insn,
776 bool IsBigEndian) {
777 // We want to read exactly 2 Bytes of data.
778 if (Bytes.size() < 2) {
779 Size = 0;
780 return MCDisassembler::Fail;
781 }
782
783 if (IsBigEndian) {
784 Insn = (Bytes[0] << 8) | Bytes[1];
785 } else {
786 Insn = (Bytes[1] << 8) | Bytes[0];
787 }
788
789 return MCDisassembler::Success;
790}
791
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000792/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000793/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000794static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
795 uint64_t &Size, uint32_t &Insn,
796 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000797 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000798 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000799 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000800 return MCDisassembler::Fail;
801 }
802
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000803 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
804 // always precede the low 16 bits in the instruction stream (that is, they
805 // are placed at lower addresses in the instruction stream).
806 //
807 // microMIPS byte ordering:
808 // Big-endian: 0 | 1 | 2 | 3
809 // Little-endian: 1 | 0 | 3 | 2
810
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000811 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000812 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000813 Insn =
814 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
815 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000816 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000817 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000818 (Bytes[1] << 24);
819 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000820 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000821 (Bytes[3] << 24);
822 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000823 }
824
825 return MCDisassembler::Success;
826}
827
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000828DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000829 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000830 uint64_t Address,
831 raw_ostream &VStream,
832 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000833 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000834 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000835
Vladimir Medicdde3d582013-09-06 12:30:36 +0000836 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000837 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
838
839 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
840 // Calling the auto-generated decoder function.
841 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
842 this, STI);
843 if (Result != MCDisassembler::Fail) {
844 Size = 2;
845 return Result;
846 }
847
848 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
849 if (Result == MCDisassembler::Fail)
850 return MCDisassembler::Fail;
851
852 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000853 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000854 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000855 this, STI);
856 if (Result != MCDisassembler::Fail) {
857 Size = 4;
858 return Result;
859 }
860 return MCDisassembler::Fail;
861 }
862
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000863 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
864 if (Result == MCDisassembler::Fail)
865 return MCDisassembler::Fail;
866
Daniel Sandersc171f652014-06-13 13:15:59 +0000867 if (hasCOP3()) {
868 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
869 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000870 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000871 if (Result != MCDisassembler::Fail) {
872 Size = 4;
873 return Result;
874 }
875 }
876
877 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000878 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000879 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000880 Address, this, STI);
881 if (Result != MCDisassembler::Fail) {
882 Size = 4;
883 return Result;
884 }
885 }
886
Daniel Sandersc171f652014-06-13 13:15:59 +0000887 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000888 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000889 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000890 Address, this, STI);
891 if (Result != MCDisassembler::Fail) {
892 Size = 4;
893 return Result;
894 }
895 }
896
Daniel Sanders0fa60412014-06-12 13:39:06 +0000897 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000898 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000899 Result =
900 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000901 if (Result != MCDisassembler::Fail) {
902 Size = 4;
903 return Result;
904 }
905
906 return MCDisassembler::Fail;
907}
908
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000909DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000910 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000911 uint64_t Address,
912 raw_ostream &VStream,
913 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000914 uint32_t Insn;
915
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000916 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000917 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000918 if (Result == MCDisassembler::Fail)
919 return MCDisassembler::Fail;
920
921 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000922 Result =
923 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000924 if (Result != MCDisassembler::Fail) {
925 Size = 4;
926 return Result;
927 }
928 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000929 Result =
930 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000931 if (Result != MCDisassembler::Fail) {
932 Size = 4;
933 return Result;
934 }
935
936 return MCDisassembler::Fail;
937}
938
Reed Kotlerec8a5492013-02-14 03:05:25 +0000939static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
940 unsigned RegNo,
941 uint64_t Address,
942 const void *Decoder) {
943
944 return MCDisassembler::Fail;
945
946}
947
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000948static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
949 unsigned RegNo,
950 uint64_t Address,
951 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000952
953 if (RegNo > 31)
954 return MCDisassembler::Fail;
955
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000956 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000957 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000958 return MCDisassembler::Success;
959}
960
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000961static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
962 unsigned RegNo,
963 uint64_t Address,
964 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000965 if (RegNo > 7)
966 return MCDisassembler::Fail;
967 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
968 Inst.addOperand(MCOperand::CreateReg(Reg));
969 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000970}
971
Jozef Kolek1904fa22014-11-24 14:25:53 +0000972static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
973 unsigned RegNo,
974 uint64_t Address,
975 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000976 if (RegNo > 7)
977 return MCDisassembler::Fail;
978 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
979 Inst.addOperand(MCOperand::CreateReg(Reg));
980 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000981}
982
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000983static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
984 unsigned RegNo,
985 uint64_t Address,
986 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000987 if (RegNo > 31)
988 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000989 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000990 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000991 return MCDisassembler::Success;
992}
993
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000994static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
995 unsigned RegNo,
996 uint64_t Address,
997 const void *Decoder) {
Vladimir Medice8860932014-12-16 15:29:12 +0000998 if (static_cast<const MipsDisassembler *>(Decoder)->isGP64Bit())
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000999 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
1000
1001 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
1002}
1003
Akira Hatanaka654655f2013-08-14 00:53:38 +00001004static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
1005 unsigned RegNo,
1006 uint64_t Address,
1007 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001008 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001009}
1010
Akira Hatanaka71928e62012-04-17 18:03:21 +00001011static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
1012 unsigned RegNo,
1013 uint64_t Address,
1014 const void *Decoder) {
1015 if (RegNo > 31)
1016 return MCDisassembler::Fail;
1017
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001018 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
1019 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001020 return MCDisassembler::Success;
1021}
1022
1023static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1024 unsigned RegNo,
1025 uint64_t Address,
1026 const void *Decoder) {
1027 if (RegNo > 31)
1028 return MCDisassembler::Fail;
1029
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001030 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1031 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001032 return MCDisassembler::Success;
1033}
1034
1035static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1036 unsigned RegNo,
1037 uint64_t Address,
1038 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001039 if (RegNo > 31)
1040 return MCDisassembler::Fail;
1041 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1042 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001043 return MCDisassembler::Success;
1044}
1045
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001046static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1047 unsigned RegNo,
1048 uint64_t Address,
1049 const void *Decoder) {
1050 if (RegNo > 7)
1051 return MCDisassembler::Fail;
1052 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1053 Inst.addOperand(MCOperand::CreateReg(Reg));
1054 return MCDisassembler::Success;
1055}
1056
Daniel Sanders0fa60412014-06-12 13:39:06 +00001057static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1058 uint64_t Address,
1059 const void *Decoder) {
1060 if (RegNo > 31)
1061 return MCDisassembler::Fail;
1062
1063 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1064 Inst.addOperand(MCOperand::CreateReg(Reg));
1065 return MCDisassembler::Success;
1066}
1067
Akira Hatanaka71928e62012-04-17 18:03:21 +00001068static DecodeStatus DecodeMem(MCInst &Inst,
1069 unsigned Insn,
1070 uint64_t Address,
1071 const void *Decoder) {
1072 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001073 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1074 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001075
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001076 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1077 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001078
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001079 if(Inst.getOpcode() == Mips::SC ||
1080 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001081 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001082 }
1083
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001084 Inst.addOperand(MCOperand::CreateReg(Reg));
1085 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001086 Inst.addOperand(MCOperand::CreateImm(Offset));
1087
1088 return MCDisassembler::Success;
1089}
1090
Daniel Sanders92db6b72014-10-01 08:26:55 +00001091static DecodeStatus DecodeCacheOp(MCInst &Inst,
1092 unsigned Insn,
1093 uint64_t Address,
1094 const void *Decoder) {
1095 int Offset = SignExtend32<16>(Insn & 0xffff);
1096 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1097 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1098
1099 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1100
1101 Inst.addOperand(MCOperand::CreateReg(Base));
1102 Inst.addOperand(MCOperand::CreateImm(Offset));
1103 Inst.addOperand(MCOperand::CreateImm(Hint));
1104
1105 return MCDisassembler::Success;
1106}
1107
Jozef Kolekab6d1cc2014-12-23 19:55:34 +00001108static DecodeStatus DecodeCacheOpMM(MCInst &Inst,
1109 unsigned Insn,
1110 uint64_t Address,
1111 const void *Decoder) {
1112 int Offset = SignExtend32<12>(Insn & 0xfff);
1113 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1114 unsigned Hint = fieldFromInstruction(Insn, 21, 5);
1115
1116 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1117
1118 Inst.addOperand(MCOperand::CreateReg(Base));
1119 Inst.addOperand(MCOperand::CreateImm(Offset));
1120 Inst.addOperand(MCOperand::CreateImm(Hint));
1121
1122 return MCDisassembler::Success;
1123}
1124
Daniel Sandersb4484d62014-11-27 17:28:10 +00001125static DecodeStatus DecodeSyncI(MCInst &Inst,
1126 unsigned Insn,
1127 uint64_t Address,
1128 const void *Decoder) {
1129 int Offset = SignExtend32<16>(Insn & 0xffff);
1130 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1131
1132 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1133
1134 Inst.addOperand(MCOperand::CreateReg(Base));
1135 Inst.addOperand(MCOperand::CreateImm(Offset));
1136
1137 return MCDisassembler::Success;
1138}
1139
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001140static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1141 uint64_t Address, const void *Decoder) {
1142 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1143 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1144 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1145
1146 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1147 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1148
1149 Inst.addOperand(MCOperand::CreateReg(Reg));
1150 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001151
1152 // The immediate field of an LD/ST instruction is scaled which means it must
1153 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1154 // data format.
1155 // .b - 1 byte
1156 // .h - 2 bytes
1157 // .w - 4 bytes
1158 // .d - 8 bytes
1159 switch(Inst.getOpcode())
1160 {
1161 default:
1162 assert (0 && "Unexpected instruction");
1163 return MCDisassembler::Fail;
1164 break;
1165 case Mips::LD_B:
1166 case Mips::ST_B:
1167 Inst.addOperand(MCOperand::CreateImm(Offset));
1168 break;
1169 case Mips::LD_H:
1170 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001171 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001172 break;
1173 case Mips::LD_W:
1174 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001175 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001176 break;
1177 case Mips::LD_D:
1178 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001179 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001180 break;
1181 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001182
1183 return MCDisassembler::Success;
1184}
1185
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001186static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1187 unsigned Insn,
1188 uint64_t Address,
1189 const void *Decoder) {
1190 unsigned Offset = Insn & 0xf;
1191 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1192 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1193
1194 switch (Inst.getOpcode()) {
1195 case Mips::LBU16_MM:
1196 case Mips::LHU16_MM:
1197 case Mips::LW16_MM:
1198 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1199 == MCDisassembler::Fail)
1200 return MCDisassembler::Fail;
1201 break;
1202 case Mips::SB16_MM:
1203 case Mips::SH16_MM:
1204 case Mips::SW16_MM:
1205 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1206 == MCDisassembler::Fail)
1207 return MCDisassembler::Fail;
1208 break;
1209 }
1210
1211 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1212 == MCDisassembler::Fail)
1213 return MCDisassembler::Fail;
1214
1215 switch (Inst.getOpcode()) {
1216 case Mips::LBU16_MM:
1217 if (Offset == 0xf)
1218 Inst.addOperand(MCOperand::CreateImm(-1));
1219 else
1220 Inst.addOperand(MCOperand::CreateImm(Offset));
1221 break;
1222 case Mips::SB16_MM:
1223 Inst.addOperand(MCOperand::CreateImm(Offset));
1224 break;
1225 case Mips::LHU16_MM:
1226 case Mips::SH16_MM:
1227 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1228 break;
1229 case Mips::LW16_MM:
1230 case Mips::SW16_MM:
1231 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1232 break;
1233 }
1234
1235 return MCDisassembler::Success;
1236}
1237
Jozef Kolek12c69822014-12-23 16:16:33 +00001238static DecodeStatus DecodeMemMMSPImm5Lsl2(MCInst &Inst,
1239 unsigned Insn,
1240 uint64_t Address,
1241 const void *Decoder) {
1242 unsigned Offset = Insn & 0x1F;
1243 unsigned Reg = fieldFromInstruction(Insn, 5, 5);
1244
1245 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1246
1247 Inst.addOperand(MCOperand::CreateReg(Reg));
1248 Inst.addOperand(MCOperand::CreateReg(Mips::SP));
1249 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1250
1251 return MCDisassembler::Success;
1252}
1253
Vladimir Medicdde3d582013-09-06 12:30:36 +00001254static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1255 unsigned Insn,
1256 uint64_t Address,
1257 const void *Decoder) {
1258 int Offset = SignExtend32<12>(Insn & 0x0fff);
1259 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1260 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1261
1262 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1263 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1264
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001265 switch (Inst.getOpcode()) {
1266 case Mips::SWM32_MM:
1267 case Mips::LWM32_MM:
1268 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1269 == MCDisassembler::Fail)
1270 return MCDisassembler::Fail;
1271 Inst.addOperand(MCOperand::CreateReg(Base));
1272 Inst.addOperand(MCOperand::CreateImm(Offset));
1273 break;
1274 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001275 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001276 // fallthrough
1277 default:
1278 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001279 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1280 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1281
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001282 Inst.addOperand(MCOperand::CreateReg(Base));
1283 Inst.addOperand(MCOperand::CreateImm(Offset));
1284 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001285
1286 return MCDisassembler::Success;
1287}
1288
1289static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1290 unsigned Insn,
1291 uint64_t Address,
1292 const void *Decoder) {
1293 int Offset = SignExtend32<16>(Insn & 0xffff);
1294 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1295 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1296
1297 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1298 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1299
1300 Inst.addOperand(MCOperand::CreateReg(Reg));
1301 Inst.addOperand(MCOperand::CreateReg(Base));
1302 Inst.addOperand(MCOperand::CreateImm(Offset));
1303
1304 return MCDisassembler::Success;
1305}
1306
Akira Hatanaka71928e62012-04-17 18:03:21 +00001307static DecodeStatus DecodeFMem(MCInst &Inst,
1308 unsigned Insn,
1309 uint64_t Address,
1310 const void *Decoder) {
1311 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001312 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1313 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001314
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001315 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001316 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001317
1318 Inst.addOperand(MCOperand::CreateReg(Reg));
1319 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001320 Inst.addOperand(MCOperand::CreateImm(Offset));
1321
1322 return MCDisassembler::Success;
1323}
1324
Daniel Sanders92db6b72014-10-01 08:26:55 +00001325static DecodeStatus DecodeFMem2(MCInst &Inst,
1326 unsigned Insn,
1327 uint64_t Address,
1328 const void *Decoder) {
1329 int Offset = SignExtend32<16>(Insn & 0xffff);
1330 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1331 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1332
1333 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1334 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1335
1336 Inst.addOperand(MCOperand::CreateReg(Reg));
1337 Inst.addOperand(MCOperand::CreateReg(Base));
1338 Inst.addOperand(MCOperand::CreateImm(Offset));
1339
1340 return MCDisassembler::Success;
1341}
1342
1343static DecodeStatus DecodeFMem3(MCInst &Inst,
1344 unsigned Insn,
1345 uint64_t Address,
1346 const void *Decoder) {
1347 int Offset = SignExtend32<16>(Insn & 0xffff);
1348 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1349 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1350
1351 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1352 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1353
1354 Inst.addOperand(MCOperand::CreateReg(Reg));
1355 Inst.addOperand(MCOperand::CreateReg(Base));
1356 Inst.addOperand(MCOperand::CreateImm(Offset));
1357
1358 return MCDisassembler::Success;
1359}
1360
Vladimir Medic435cf8a2015-01-21 10:47:36 +00001361static DecodeStatus DecodeFMemCop2R6(MCInst &Inst,
1362 unsigned Insn,
1363 uint64_t Address,
1364 const void *Decoder) {
1365 int Offset = SignExtend32<11>(Insn & 0x07ff);
1366 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1367 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1368
1369 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1370 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1371
1372 Inst.addOperand(MCOperand::CreateReg(Reg));
1373 Inst.addOperand(MCOperand::CreateReg(Base));
1374 Inst.addOperand(MCOperand::CreateImm(Offset));
1375
1376 return MCDisassembler::Success;
1377}
Daniel Sanders6a803f62014-06-16 13:13:03 +00001378static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1379 unsigned Insn,
1380 uint64_t Address,
1381 const void *Decoder) {
1382 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1383 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1384 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1385
1386 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1387 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1388
1389 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1390 Inst.addOperand(MCOperand::CreateReg(Rt));
1391 }
1392
1393 Inst.addOperand(MCOperand::CreateReg(Rt));
1394 Inst.addOperand(MCOperand::CreateReg(Base));
1395 Inst.addOperand(MCOperand::CreateImm(Offset));
1396
1397 return MCDisassembler::Success;
1398}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001399
1400static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1401 unsigned RegNo,
1402 uint64_t Address,
1403 const void *Decoder) {
1404 // Currently only hardware register 29 is supported.
1405 if (RegNo != 29)
1406 return MCDisassembler::Fail;
1407 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1408 return MCDisassembler::Success;
1409}
1410
Akira Hatanaka71928e62012-04-17 18:03:21 +00001411static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1412 unsigned RegNo,
1413 uint64_t Address,
1414 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001415 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001416 return MCDisassembler::Fail;
1417
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001418 ;
1419 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1420 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001421 return MCDisassembler::Success;
1422}
1423
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001424static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1425 unsigned RegNo,
1426 uint64_t Address,
1427 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001428 if (RegNo >= 4)
1429 return MCDisassembler::Fail;
1430
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001431 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001432 Inst.addOperand(MCOperand::CreateReg(Reg));
1433 return MCDisassembler::Success;
1434}
1435
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001436static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1437 unsigned RegNo,
1438 uint64_t Address,
1439 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001440 if (RegNo >= 4)
1441 return MCDisassembler::Fail;
1442
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001443 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001444 Inst.addOperand(MCOperand::CreateReg(Reg));
1445 return MCDisassembler::Success;
1446}
1447
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001448static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1449 unsigned RegNo,
1450 uint64_t Address,
1451 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001452 if (RegNo >= 4)
1453 return MCDisassembler::Fail;
1454
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001455 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001456 Inst.addOperand(MCOperand::CreateReg(Reg));
1457 return MCDisassembler::Success;
1458}
1459
Jack Carter3eb663b2013-09-26 00:09:46 +00001460static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1461 unsigned RegNo,
1462 uint64_t Address,
1463 const void *Decoder) {
1464 if (RegNo > 31)
1465 return MCDisassembler::Fail;
1466
1467 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1468 Inst.addOperand(MCOperand::CreateReg(Reg));
1469 return MCDisassembler::Success;
1470}
1471
Jack Carter5dc8ac92013-09-25 23:50:44 +00001472static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1473 unsigned RegNo,
1474 uint64_t Address,
1475 const void *Decoder) {
1476 if (RegNo > 31)
1477 return MCDisassembler::Fail;
1478
1479 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1480 Inst.addOperand(MCOperand::CreateReg(Reg));
1481 return MCDisassembler::Success;
1482}
1483
1484static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1485 unsigned RegNo,
1486 uint64_t Address,
1487 const void *Decoder) {
1488 if (RegNo > 31)
1489 return MCDisassembler::Fail;
1490
1491 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1492 Inst.addOperand(MCOperand::CreateReg(Reg));
1493 return MCDisassembler::Success;
1494}
1495
1496static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1497 unsigned RegNo,
1498 uint64_t Address,
1499 const void *Decoder) {
1500 if (RegNo > 31)
1501 return MCDisassembler::Fail;
1502
1503 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1504 Inst.addOperand(MCOperand::CreateReg(Reg));
1505 return MCDisassembler::Success;
1506}
1507
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001508static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1509 unsigned RegNo,
1510 uint64_t Address,
1511 const void *Decoder) {
1512 if (RegNo > 7)
1513 return MCDisassembler::Fail;
1514
1515 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1516 Inst.addOperand(MCOperand::CreateReg(Reg));
1517 return MCDisassembler::Success;
1518}
1519
Daniel Sanders2a83d682014-05-21 12:56:39 +00001520static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1521 unsigned RegNo,
1522 uint64_t Address,
1523 const void *Decoder) {
1524 if (RegNo > 31)
1525 return MCDisassembler::Fail;
1526
1527 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1528 Inst.addOperand(MCOperand::CreateReg(Reg));
1529 return MCDisassembler::Success;
1530}
1531
Akira Hatanaka71928e62012-04-17 18:03:21 +00001532static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1533 unsigned Offset,
1534 uint64_t Address,
1535 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001536 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001537 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1538 return MCDisassembler::Success;
1539}
1540
Akira Hatanaka71928e62012-04-17 18:03:21 +00001541static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1542 unsigned Insn,
1543 uint64_t Address,
1544 const void *Decoder) {
1545
Jim Grosbachecaef492012-08-14 19:06:05 +00001546 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001547 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1548 return MCDisassembler::Success;
1549}
1550
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001551static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1552 unsigned Offset,
1553 uint64_t Address,
1554 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001555 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001556
1557 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1558 return MCDisassembler::Success;
1559}
1560
1561static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1562 unsigned Offset,
1563 uint64_t Address,
1564 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001565 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001566
1567 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1568 return MCDisassembler::Success;
1569}
1570
Jozef Kolek9761e962015-01-12 12:03:34 +00001571static DecodeStatus DecodeBranchTarget7MM(MCInst &Inst,
1572 unsigned Offset,
1573 uint64_t Address,
1574 const void *Decoder) {
1575 int32_t BranchOffset = SignExtend32<7>(Offset) << 1;
1576 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1577 return MCDisassembler::Success;
1578}
1579
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001580static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1581 unsigned Offset,
1582 uint64_t Address,
1583 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001584 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001585 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1586 return MCDisassembler::Success;
1587}
1588
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001589static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1590 unsigned Insn,
1591 uint64_t Address,
1592 const void *Decoder) {
1593 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1594 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1595 return MCDisassembler::Success;
1596}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001597
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001598static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1599 unsigned Value,
1600 uint64_t Address,
1601 const void *Decoder) {
1602 if (Value == 0)
1603 Inst.addOperand(MCOperand::CreateImm(1));
1604 else if (Value == 0x7)
1605 Inst.addOperand(MCOperand::CreateImm(-1));
1606 else
1607 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1608 return MCDisassembler::Success;
1609}
1610
1611static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1612 unsigned Value,
1613 uint64_t Address,
1614 const void *Decoder) {
1615 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1616 return MCDisassembler::Success;
1617}
1618
1619static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1620 unsigned Value,
1621 uint64_t Address,
1622 const void *Decoder) {
1623 if (Value == 0x7F)
1624 Inst.addOperand(MCOperand::CreateImm(-1));
1625 else
1626 Inst.addOperand(MCOperand::CreateImm(Value));
1627 return MCDisassembler::Success;
1628}
1629
1630static DecodeStatus DecodeSimm4(MCInst &Inst,
1631 unsigned Value,
1632 uint64_t Address,
1633 const void *Decoder) {
1634 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1635 return MCDisassembler::Success;
1636}
1637
Akira Hatanaka71928e62012-04-17 18:03:21 +00001638static DecodeStatus DecodeSimm16(MCInst &Inst,
1639 unsigned Insn,
1640 uint64_t Address,
1641 const void *Decoder) {
1642 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1643 return MCDisassembler::Success;
1644}
1645
Matheus Almeida779c5932013-11-18 12:32:49 +00001646static DecodeStatus DecodeLSAImm(MCInst &Inst,
1647 unsigned Insn,
1648 uint64_t Address,
1649 const void *Decoder) {
1650 // We add one to the immediate field as it was encoded as 'imm - 1'.
1651 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1652 return MCDisassembler::Success;
1653}
1654
Akira Hatanaka71928e62012-04-17 18:03:21 +00001655static DecodeStatus DecodeInsSize(MCInst &Inst,
1656 unsigned Insn,
1657 uint64_t Address,
1658 const void *Decoder) {
1659 // First we need to grab the pos(lsb) from MCInst.
1660 int Pos = Inst.getOperand(2).getImm();
1661 int Size = (int) Insn - Pos + 1;
1662 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1663 return MCDisassembler::Success;
1664}
1665
1666static DecodeStatus DecodeExtSize(MCInst &Inst,
1667 unsigned Insn,
1668 uint64_t Address,
1669 const void *Decoder) {
1670 int Size = (int) Insn + 1;
1671 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1672 return MCDisassembler::Success;
1673}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001674
1675static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1676 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001677 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001678 return MCDisassembler::Success;
1679}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001680
1681static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1682 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001683 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001684 return MCDisassembler::Success;
1685}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001686
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001687static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1688 uint64_t Address, const void *Decoder) {
1689 int32_t DecodedValue;
1690 switch (Insn) {
1691 case 0: DecodedValue = 256; break;
1692 case 1: DecodedValue = 257; break;
1693 case 510: DecodedValue = -258; break;
1694 case 511: DecodedValue = -257; break;
1695 default: DecodedValue = SignExtend32<9>(Insn); break;
1696 }
Alexey Samsonov2c559742014-12-23 04:15:53 +00001697 Inst.addOperand(MCOperand::CreateImm(DecodedValue * 4));
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001698 return MCDisassembler::Success;
1699}
1700
1701static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1702 uint64_t Address, const void *Decoder) {
1703 // Insn must be >= 0, since it is unsigned that condition is always true.
1704 assert(Insn < 16);
1705 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1706 255, 32768, 65535};
1707 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1708 return MCDisassembler::Success;
1709}
1710
1711static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1712 uint64_t Address, const void *Decoder) {
1713 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1714 return MCDisassembler::Success;
1715}
1716
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001717static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1718 unsigned Insn,
1719 uint64_t Address,
1720 const void *Decoder) {
1721 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1722 Mips::S6, Mips::FP};
1723 unsigned RegNum;
1724
1725 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1726 // Empty register lists are not allowed.
1727 if (RegLst == 0)
1728 return MCDisassembler::Fail;
1729
1730 RegNum = RegLst & 0xf;
1731 for (unsigned i = 0; i < RegNum; i++)
1732 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1733
1734 if (RegLst & 0x10)
1735 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1736
1737 return MCDisassembler::Success;
1738}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001739
1740static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1741 uint64_t Address,
1742 const void *Decoder) {
1743 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1744 unsigned RegNum;
1745
1746 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1747 // Empty register lists are not allowed.
1748 if (RegLst == 0)
1749 return MCDisassembler::Fail;
1750
1751 RegNum = RegLst & 0x3;
1752 for (unsigned i = 0; i < RegNum - 1; i++)
1753 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1754
1755 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1756
1757 return MCDisassembler::Success;
1758}