blob: d29347d6213c77a9ac44ca1897c3b1e6f92866b3 [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),
39 IsN64(STI.getFeatureBits() & Mips::FeatureN64),
40 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
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000044 bool isN64() const { return IsN64; }
45
Akira Hatanaka71928e62012-04-17 18:03:21 +000046private:
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000047 bool IsN64;
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
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000231// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
232// shifted left by 1 bit.
233static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
234 unsigned Offset,
235 uint64_t Address,
236 const void *Decoder);
237
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000238// DecodeJumpTargetMM - Decode microMIPS jump target, which is
239// shifted left by 1 bit.
240static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
241 unsigned Insn,
242 uint64_t Address,
243 const void *Decoder);
244
Akira Hatanaka71928e62012-04-17 18:03:21 +0000245static DecodeStatus DecodeMem(MCInst &Inst,
246 unsigned Insn,
247 uint64_t Address,
248 const void *Decoder);
249
Daniel Sanders92db6b72014-10-01 08:26:55 +0000250static DecodeStatus DecodeCacheOp(MCInst &Inst,
251 unsigned Insn,
252 uint64_t Address,
253 const void *Decoder);
254
Daniel Sandersb4484d62014-11-27 17:28:10 +0000255static DecodeStatus DecodeSyncI(MCInst &Inst,
256 unsigned Insn,
257 uint64_t Address,
258 const void *Decoder);
259
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000260static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
261 uint64_t Address, const void *Decoder);
262
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000263static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
264 unsigned Insn,
265 uint64_t Address,
266 const void *Decoder);
267
Vladimir Medicdde3d582013-09-06 12:30:36 +0000268static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
269 unsigned Insn,
270 uint64_t Address,
271 const void *Decoder);
272
273static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
274 unsigned Insn,
275 uint64_t Address,
276 const void *Decoder);
277
Akira Hatanaka71928e62012-04-17 18:03:21 +0000278static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
279 uint64_t Address,
280 const void *Decoder);
281
Daniel Sanders92db6b72014-10-01 08:26:55 +0000282static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
283 uint64_t Address,
284 const void *Decoder);
285
286static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
287 uint64_t Address,
288 const void *Decoder);
289
Daniel Sanders6a803f62014-06-16 13:13:03 +0000290static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
291 unsigned Insn,
292 uint64_t Address,
293 const void *Decoder);
294
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000295static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
296 unsigned Value,
297 uint64_t Address,
298 const void *Decoder);
299
300static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
301 unsigned Value,
302 uint64_t Address,
303 const void *Decoder);
304
305static DecodeStatus DecodeLiSimm7(MCInst &Inst,
306 unsigned Value,
307 uint64_t Address,
308 const void *Decoder);
309
310static DecodeStatus DecodeSimm4(MCInst &Inst,
311 unsigned Value,
312 uint64_t Address,
313 const void *Decoder);
314
Akira Hatanaka71928e62012-04-17 18:03:21 +0000315static DecodeStatus DecodeSimm16(MCInst &Inst,
316 unsigned Insn,
317 uint64_t Address,
318 const void *Decoder);
319
Matheus Almeida779c5932013-11-18 12:32:49 +0000320// Decode the immediate field of an LSA instruction which
321// is off by one.
322static DecodeStatus DecodeLSAImm(MCInst &Inst,
323 unsigned Insn,
324 uint64_t Address,
325 const void *Decoder);
326
Akira Hatanaka71928e62012-04-17 18:03:21 +0000327static DecodeStatus DecodeInsSize(MCInst &Inst,
328 unsigned Insn,
329 uint64_t Address,
330 const void *Decoder);
331
332static DecodeStatus DecodeExtSize(MCInst &Inst,
333 unsigned Insn,
334 uint64_t Address,
335 const void *Decoder);
336
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000337static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
338 uint64_t Address, const void *Decoder);
339
Zoran Jovanovic28551422014-06-09 09:49:51 +0000340static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
341 uint64_t Address, const void *Decoder);
342
Vladimir Medicb682ddf2014-12-01 11:12:04 +0000343static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
344 uint64_t Address, const void *Decoder);
345
346static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
347 uint64_t Address, const void *Decoder);
348
349static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
350 uint64_t Address, const void *Decoder);
351
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000352/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
353/// handle.
354template <typename InsnType>
355static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
356 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000357
358template <typename InsnType>
359static DecodeStatus
360DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
361 const void *Decoder);
362
363template <typename InsnType>
364static DecodeStatus
365DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
366 const void *Decoder);
367
368template <typename InsnType>
369static DecodeStatus
370DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
371 const void *Decoder);
372
373template <typename InsnType>
374static DecodeStatus
375DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
376 const void *Decoder);
377
378template <typename InsnType>
379static DecodeStatus
380DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
381 const void *Decoder);
382
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000383template <typename InsnType>
384static DecodeStatus
385DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
386 const void *Decoder);
387
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000388static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
389 uint64_t Address,
390 const void *Decoder);
391
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000392static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
393 uint64_t Address,
394 const void *Decoder);
395
Akira Hatanaka71928e62012-04-17 18:03:21 +0000396namespace llvm {
397extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
398 TheMips64elTarget;
399}
400
401static MCDisassembler *createMipsDisassembler(
402 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000403 const MCSubtargetInfo &STI,
404 MCContext &Ctx) {
405 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000406}
407
408static MCDisassembler *createMipselDisassembler(
409 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000410 const MCSubtargetInfo &STI,
411 MCContext &Ctx) {
412 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000413}
414
415static MCDisassembler *createMips64Disassembler(
416 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000417 const MCSubtargetInfo &STI,
418 MCContext &Ctx) {
419 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000420}
421
422static MCDisassembler *createMips64elDisassembler(
423 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000424 const MCSubtargetInfo &STI,
425 MCContext &Ctx) {
426 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000427}
428
429extern "C" void LLVMInitializeMipsDisassembler() {
430 // Register the disassembler.
431 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
432 createMipsDisassembler);
433 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
434 createMipselDisassembler);
435 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
436 createMips64Disassembler);
437 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
438 createMips64elDisassembler);
439}
440
Akira Hatanaka71928e62012-04-17 18:03:21 +0000441#include "MipsGenDisassemblerTables.inc"
442
Daniel Sanders5c582b22014-05-22 11:23:21 +0000443static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
444 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
445 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
446 return *(RegInfo->getRegClass(RC).begin() + RegNo);
447}
448
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000449template <typename InsnType>
450static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
451 const void *Decoder) {
452 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
453 // The size of the n field depends on the element size
454 // The register class also depends on this.
455 InsnType tmp = fieldFromInstruction(insn, 17, 5);
456 unsigned NSize = 0;
457 DecodeFN RegDecoder = nullptr;
458 if ((tmp & 0x18) == 0x00) { // INSVE_B
459 NSize = 4;
460 RegDecoder = DecodeMSA128BRegisterClass;
461 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
462 NSize = 3;
463 RegDecoder = DecodeMSA128HRegisterClass;
464 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
465 NSize = 2;
466 RegDecoder = DecodeMSA128WRegisterClass;
467 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
468 NSize = 1;
469 RegDecoder = DecodeMSA128DRegisterClass;
470 } else
471 llvm_unreachable("Invalid encoding");
472
473 assert(NSize != 0 && RegDecoder != nullptr);
474
475 // $wd
476 tmp = fieldFromInstruction(insn, 6, 5);
477 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
478 return MCDisassembler::Fail;
479 // $wd_in
480 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
481 return MCDisassembler::Fail;
482 // $n
483 tmp = fieldFromInstruction(insn, 16, NSize);
484 MI.addOperand(MCOperand::CreateImm(tmp));
485 // $ws
486 tmp = fieldFromInstruction(insn, 11, 5);
487 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
488 return MCDisassembler::Fail;
489 // $n2
490 MI.addOperand(MCOperand::CreateImm(0));
491
492 return MCDisassembler::Success;
493}
494
Daniel Sanders5c582b22014-05-22 11:23:21 +0000495template <typename InsnType>
496static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
497 uint64_t Address,
498 const void *Decoder) {
499 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
500 // (otherwise we would have matched the ADDI instruction from the earlier
501 // ISA's instead).
502 //
503 // We have:
504 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
505 // BOVC if rs >= rt
506 // BEQZALC if rs == 0 && rt != 0
507 // BEQC if rs < rt && rs != 0
508
509 InsnType Rs = fieldFromInstruction(insn, 21, 5);
510 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000511 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000512 bool HasRs = false;
513
514 if (Rs >= Rt) {
515 MI.setOpcode(Mips::BOVC);
516 HasRs = true;
517 } else if (Rs != 0 && Rs < Rt) {
518 MI.setOpcode(Mips::BEQC);
519 HasRs = true;
520 } else
521 MI.setOpcode(Mips::BEQZALC);
522
523 if (HasRs)
524 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
525 Rs)));
526
527 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
528 Rt)));
529 MI.addOperand(MCOperand::CreateImm(Imm));
530
531 return MCDisassembler::Success;
532}
533
534template <typename InsnType>
535static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
536 uint64_t Address,
537 const void *Decoder) {
538 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
539 // (otherwise we would have matched the ADDI instruction from the earlier
540 // ISA's instead).
541 //
542 // We have:
543 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
544 // BNVC if rs >= rt
545 // BNEZALC if rs == 0 && rt != 0
546 // BNEC if rs < rt && rs != 0
547
548 InsnType Rs = fieldFromInstruction(insn, 21, 5);
549 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000550 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000551 bool HasRs = false;
552
553 if (Rs >= Rt) {
554 MI.setOpcode(Mips::BNVC);
555 HasRs = true;
556 } else if (Rs != 0 && Rs < Rt) {
557 MI.setOpcode(Mips::BNEC);
558 HasRs = true;
559 } else
560 MI.setOpcode(Mips::BNEZALC);
561
562 if (HasRs)
563 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
564 Rs)));
565
566 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
567 Rt)));
568 MI.addOperand(MCOperand::CreateImm(Imm));
569
570 return MCDisassembler::Success;
571}
572
573template <typename InsnType>
574static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
575 uint64_t Address,
576 const void *Decoder) {
577 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
578 // (otherwise we would have matched the BLEZL instruction from the earlier
579 // ISA's instead).
580 //
581 // We have:
582 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
583 // Invalid if rs == 0
584 // BLEZC if rs == 0 && rt != 0
585 // BGEZC if rs == rt && rt != 0
586 // BGEC if rs != rt && rs != 0 && rt != 0
587
588 InsnType Rs = fieldFromInstruction(insn, 21, 5);
589 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000590 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000591 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000592
593 if (Rt == 0)
594 return MCDisassembler::Fail;
595 else if (Rs == 0)
596 MI.setOpcode(Mips::BLEZC);
597 else if (Rs == Rt)
598 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000599 else {
600 HasRs = true;
601 MI.setOpcode(Mips::BGEC);
602 }
603
604 if (HasRs)
605 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
606 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000607
608 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
609 Rt)));
610
611 MI.addOperand(MCOperand::CreateImm(Imm));
612
613 return MCDisassembler::Success;
614}
615
616template <typename InsnType>
617static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
618 uint64_t Address,
619 const void *Decoder) {
620 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
621 // (otherwise we would have matched the BGTZL instruction from the earlier
622 // ISA's instead).
623 //
624 // We have:
625 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
626 // Invalid if rs == 0
627 // BGTZC if rs == 0 && rt != 0
628 // BLTZC if rs == rt && rt != 0
629 // BLTC if rs != rt && rs != 0 && rt != 0
630
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000631 bool HasRs = false;
632
Daniel Sanders5c582b22014-05-22 11:23:21 +0000633 InsnType Rs = fieldFromInstruction(insn, 21, 5);
634 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000635 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000636
637 if (Rt == 0)
638 return MCDisassembler::Fail;
639 else if (Rs == 0)
640 MI.setOpcode(Mips::BGTZC);
641 else if (Rs == Rt)
642 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000643 else {
644 MI.setOpcode(Mips::BLTC);
645 HasRs = true;
646 }
647
648 if (HasRs)
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
650 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000651
652 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
653 Rt)));
654
655 MI.addOperand(MCOperand::CreateImm(Imm));
656
657 return MCDisassembler::Success;
658}
659
660template <typename InsnType>
661static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
662 uint64_t Address,
663 const void *Decoder) {
664 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
665 // (otherwise we would have matched the BGTZ instruction from the earlier
666 // ISA's instead).
667 //
668 // We have:
669 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
670 // BGTZ if rt == 0
671 // BGTZALC if rs == 0 && rt != 0
672 // BLTZALC if rs != 0 && rs == rt
673 // BLTUC if rs != 0 && rs != rt
674
675 InsnType Rs = fieldFromInstruction(insn, 21, 5);
676 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000677 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000678 bool HasRs = false;
679 bool HasRt = false;
680
681 if (Rt == 0) {
682 MI.setOpcode(Mips::BGTZ);
683 HasRs = true;
684 } else if (Rs == 0) {
685 MI.setOpcode(Mips::BGTZALC);
686 HasRt = true;
687 } else if (Rs == Rt) {
688 MI.setOpcode(Mips::BLTZALC);
689 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000690 } else {
691 MI.setOpcode(Mips::BLTUC);
692 HasRs = true;
693 HasRt = true;
694 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000695
696 if (HasRs)
697 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
698 Rs)));
699
700 if (HasRt)
701 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
702 Rt)));
703
704 MI.addOperand(MCOperand::CreateImm(Imm));
705
706 return MCDisassembler::Success;
707}
708
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000709template <typename InsnType>
710static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
711 uint64_t Address,
712 const void *Decoder) {
713 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
714 // (otherwise we would have matched the BLEZL instruction from the earlier
715 // ISA's instead).
716 //
717 // We have:
718 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
719 // Invalid if rs == 0
720 // BLEZALC if rs == 0 && rt != 0
721 // BGEZALC if rs == rt && rt != 0
722 // BGEUC if rs != rt && rs != 0 && rt != 0
723
724 InsnType Rs = fieldFromInstruction(insn, 21, 5);
725 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000726 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000727 bool HasRs = false;
728
729 if (Rt == 0)
730 return MCDisassembler::Fail;
731 else if (Rs == 0)
732 MI.setOpcode(Mips::BLEZALC);
733 else if (Rs == Rt)
734 MI.setOpcode(Mips::BGEZALC);
735 else {
736 HasRs = true;
737 MI.setOpcode(Mips::BGEUC);
738 }
739
740 if (HasRs)
741 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
742 Rs)));
743 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
744 Rt)));
745
746 MI.addOperand(MCOperand::CreateImm(Imm));
747
748 return MCDisassembler::Success;
749}
750
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000751/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
752/// according to the given endianess.
753static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
754 uint64_t &Size, uint32_t &Insn,
755 bool IsBigEndian) {
756 // We want to read exactly 2 Bytes of data.
757 if (Bytes.size() < 2) {
758 Size = 0;
759 return MCDisassembler::Fail;
760 }
761
762 if (IsBigEndian) {
763 Insn = (Bytes[0] << 8) | Bytes[1];
764 } else {
765 Insn = (Bytes[1] << 8) | Bytes[0];
766 }
767
768 return MCDisassembler::Success;
769}
770
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000771/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000772/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000773static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
774 uint64_t &Size, uint32_t &Insn,
775 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000776 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000777 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000778 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000779 return MCDisassembler::Fail;
780 }
781
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000782 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
783 // always precede the low 16 bits in the instruction stream (that is, they
784 // are placed at lower addresses in the instruction stream).
785 //
786 // microMIPS byte ordering:
787 // Big-endian: 0 | 1 | 2 | 3
788 // Little-endian: 1 | 0 | 3 | 2
789
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000790 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000791 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000792 Insn =
793 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
794 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000795 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000796 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000797 (Bytes[1] << 24);
798 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000799 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000800 (Bytes[3] << 24);
801 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000802 }
803
804 return MCDisassembler::Success;
805}
806
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000807DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000808 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000809 uint64_t Address,
810 raw_ostream &VStream,
811 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000812 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000813 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000814
Vladimir Medicdde3d582013-09-06 12:30:36 +0000815 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000816 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
817
818 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
819 // Calling the auto-generated decoder function.
820 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
821 this, STI);
822 if (Result != MCDisassembler::Fail) {
823 Size = 2;
824 return Result;
825 }
826
827 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
828 if (Result == MCDisassembler::Fail)
829 return MCDisassembler::Fail;
830
831 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000832 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000833 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000834 this, STI);
835 if (Result != MCDisassembler::Fail) {
836 Size = 4;
837 return Result;
838 }
839 return MCDisassembler::Fail;
840 }
841
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000842 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
843 if (Result == MCDisassembler::Fail)
844 return MCDisassembler::Fail;
845
Daniel Sandersc171f652014-06-13 13:15:59 +0000846 if (hasCOP3()) {
847 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
848 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000849 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000850 if (Result != MCDisassembler::Fail) {
851 Size = 4;
852 return Result;
853 }
854 }
855
856 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000857 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000858 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000859 Address, this, STI);
860 if (Result != MCDisassembler::Fail) {
861 Size = 4;
862 return Result;
863 }
864 }
865
Daniel Sandersc171f652014-06-13 13:15:59 +0000866 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000867 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000868 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000869 Address, this, STI);
870 if (Result != MCDisassembler::Fail) {
871 Size = 4;
872 return Result;
873 }
874 }
875
Daniel Sanders0fa60412014-06-12 13:39:06 +0000876 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000877 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000878 Result =
879 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000880 if (Result != MCDisassembler::Fail) {
881 Size = 4;
882 return Result;
883 }
884
885 return MCDisassembler::Fail;
886}
887
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000888DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000889 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000890 uint64_t Address,
891 raw_ostream &VStream,
892 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000893 uint32_t Insn;
894
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000895 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000896 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000897 if (Result == MCDisassembler::Fail)
898 return MCDisassembler::Fail;
899
900 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000901 Result =
902 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000903 if (Result != MCDisassembler::Fail) {
904 Size = 4;
905 return Result;
906 }
907 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000908 Result =
909 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000910 if (Result != MCDisassembler::Fail) {
911 Size = 4;
912 return Result;
913 }
914
915 return MCDisassembler::Fail;
916}
917
Reed Kotlerec8a5492013-02-14 03:05:25 +0000918static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
919 unsigned RegNo,
920 uint64_t Address,
921 const void *Decoder) {
922
923 return MCDisassembler::Fail;
924
925}
926
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000927static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
928 unsigned RegNo,
929 uint64_t Address,
930 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000931
932 if (RegNo > 31)
933 return MCDisassembler::Fail;
934
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000935 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000936 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000937 return MCDisassembler::Success;
938}
939
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000940static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
941 unsigned RegNo,
942 uint64_t Address,
943 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000944 if (RegNo > 7)
945 return MCDisassembler::Fail;
946 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
947 Inst.addOperand(MCOperand::CreateReg(Reg));
948 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000949}
950
Jozef Kolek1904fa22014-11-24 14:25:53 +0000951static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
952 unsigned RegNo,
953 uint64_t Address,
954 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000955 if (RegNo > 7)
956 return MCDisassembler::Fail;
957 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
958 Inst.addOperand(MCOperand::CreateReg(Reg));
959 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000960}
961
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000962static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
963 unsigned RegNo,
964 uint64_t Address,
965 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000966 if (RegNo > 31)
967 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000968 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000969 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000970 return MCDisassembler::Success;
971}
972
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000973static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
974 unsigned RegNo,
975 uint64_t Address,
976 const void *Decoder) {
977 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
978 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
979
980 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
981}
982
Akira Hatanaka654655f2013-08-14 00:53:38 +0000983static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
984 unsigned RegNo,
985 uint64_t Address,
986 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000987 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000988}
989
Akira Hatanaka71928e62012-04-17 18:03:21 +0000990static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
991 unsigned RegNo,
992 uint64_t Address,
993 const void *Decoder) {
994 if (RegNo > 31)
995 return MCDisassembler::Fail;
996
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000997 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
998 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000999 return MCDisassembler::Success;
1000}
1001
1002static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
1003 unsigned RegNo,
1004 uint64_t Address,
1005 const void *Decoder) {
1006 if (RegNo > 31)
1007 return MCDisassembler::Fail;
1008
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001009 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
1010 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001011 return MCDisassembler::Success;
1012}
1013
1014static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1015 unsigned RegNo,
1016 uint64_t Address,
1017 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001018 if (RegNo > 31)
1019 return MCDisassembler::Fail;
1020 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1021 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001022 return MCDisassembler::Success;
1023}
1024
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001025static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1026 unsigned RegNo,
1027 uint64_t Address,
1028 const void *Decoder) {
1029 if (RegNo > 7)
1030 return MCDisassembler::Fail;
1031 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1032 Inst.addOperand(MCOperand::CreateReg(Reg));
1033 return MCDisassembler::Success;
1034}
1035
Daniel Sanders0fa60412014-06-12 13:39:06 +00001036static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1037 uint64_t Address,
1038 const void *Decoder) {
1039 if (RegNo > 31)
1040 return MCDisassembler::Fail;
1041
1042 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1043 Inst.addOperand(MCOperand::CreateReg(Reg));
1044 return MCDisassembler::Success;
1045}
1046
Akira Hatanaka71928e62012-04-17 18:03:21 +00001047static DecodeStatus DecodeMem(MCInst &Inst,
1048 unsigned Insn,
1049 uint64_t Address,
1050 const void *Decoder) {
1051 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001052 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1053 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001054
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001055 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1056 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001057
Vladimir Medicd7ecf492014-12-15 16:19:34 +00001058 if(Inst.getOpcode() == Mips::SC ||
1059 Inst.getOpcode() == Mips::SCD){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001060 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001061 }
1062
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001063 Inst.addOperand(MCOperand::CreateReg(Reg));
1064 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001065 Inst.addOperand(MCOperand::CreateImm(Offset));
1066
1067 return MCDisassembler::Success;
1068}
1069
Daniel Sanders92db6b72014-10-01 08:26:55 +00001070static DecodeStatus DecodeCacheOp(MCInst &Inst,
1071 unsigned Insn,
1072 uint64_t Address,
1073 const void *Decoder) {
1074 int Offset = SignExtend32<16>(Insn & 0xffff);
1075 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1076 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1077
1078 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1079
1080 Inst.addOperand(MCOperand::CreateReg(Base));
1081 Inst.addOperand(MCOperand::CreateImm(Offset));
1082 Inst.addOperand(MCOperand::CreateImm(Hint));
1083
1084 return MCDisassembler::Success;
1085}
1086
Daniel Sandersb4484d62014-11-27 17:28:10 +00001087static DecodeStatus DecodeSyncI(MCInst &Inst,
1088 unsigned Insn,
1089 uint64_t Address,
1090 const void *Decoder) {
1091 int Offset = SignExtend32<16>(Insn & 0xffff);
1092 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1093
1094 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1095
1096 Inst.addOperand(MCOperand::CreateReg(Base));
1097 Inst.addOperand(MCOperand::CreateImm(Offset));
1098
1099 return MCDisassembler::Success;
1100}
1101
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001102static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1103 uint64_t Address, const void *Decoder) {
1104 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1105 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1106 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1107
1108 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1109 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1110
1111 Inst.addOperand(MCOperand::CreateReg(Reg));
1112 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001113
1114 // The immediate field of an LD/ST instruction is scaled which means it must
1115 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1116 // data format.
1117 // .b - 1 byte
1118 // .h - 2 bytes
1119 // .w - 4 bytes
1120 // .d - 8 bytes
1121 switch(Inst.getOpcode())
1122 {
1123 default:
1124 assert (0 && "Unexpected instruction");
1125 return MCDisassembler::Fail;
1126 break;
1127 case Mips::LD_B:
1128 case Mips::ST_B:
1129 Inst.addOperand(MCOperand::CreateImm(Offset));
1130 break;
1131 case Mips::LD_H:
1132 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001133 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001134 break;
1135 case Mips::LD_W:
1136 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001137 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001138 break;
1139 case Mips::LD_D:
1140 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001141 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001142 break;
1143 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001144
1145 return MCDisassembler::Success;
1146}
1147
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001148static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1149 unsigned Insn,
1150 uint64_t Address,
1151 const void *Decoder) {
1152 unsigned Offset = Insn & 0xf;
1153 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1154 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1155
1156 switch (Inst.getOpcode()) {
1157 case Mips::LBU16_MM:
1158 case Mips::LHU16_MM:
1159 case Mips::LW16_MM:
1160 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1161 == MCDisassembler::Fail)
1162 return MCDisassembler::Fail;
1163 break;
1164 case Mips::SB16_MM:
1165 case Mips::SH16_MM:
1166 case Mips::SW16_MM:
1167 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1168 == MCDisassembler::Fail)
1169 return MCDisassembler::Fail;
1170 break;
1171 }
1172
1173 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1174 == MCDisassembler::Fail)
1175 return MCDisassembler::Fail;
1176
1177 switch (Inst.getOpcode()) {
1178 case Mips::LBU16_MM:
1179 if (Offset == 0xf)
1180 Inst.addOperand(MCOperand::CreateImm(-1));
1181 else
1182 Inst.addOperand(MCOperand::CreateImm(Offset));
1183 break;
1184 case Mips::SB16_MM:
1185 Inst.addOperand(MCOperand::CreateImm(Offset));
1186 break;
1187 case Mips::LHU16_MM:
1188 case Mips::SH16_MM:
1189 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1190 break;
1191 case Mips::LW16_MM:
1192 case Mips::SW16_MM:
1193 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1194 break;
1195 }
1196
1197 return MCDisassembler::Success;
1198}
1199
Vladimir Medicdde3d582013-09-06 12:30:36 +00001200static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1201 unsigned Insn,
1202 uint64_t Address,
1203 const void *Decoder) {
1204 int Offset = SignExtend32<12>(Insn & 0x0fff);
1205 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1206 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1207
1208 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1209 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1210
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001211 switch (Inst.getOpcode()) {
1212 case Mips::SWM32_MM:
1213 case Mips::LWM32_MM:
1214 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1215 == MCDisassembler::Fail)
1216 return MCDisassembler::Fail;
1217 Inst.addOperand(MCOperand::CreateReg(Base));
1218 Inst.addOperand(MCOperand::CreateImm(Offset));
1219 break;
1220 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001221 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001222 // fallthrough
1223 default:
1224 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001225 if (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM)
1226 Inst.addOperand(MCOperand::CreateReg(Reg+1));
1227
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001228 Inst.addOperand(MCOperand::CreateReg(Base));
1229 Inst.addOperand(MCOperand::CreateImm(Offset));
1230 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001231
1232 return MCDisassembler::Success;
1233}
1234
1235static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1236 unsigned Insn,
1237 uint64_t Address,
1238 const void *Decoder) {
1239 int Offset = SignExtend32<16>(Insn & 0xffff);
1240 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1241 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1242
1243 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1244 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1245
1246 Inst.addOperand(MCOperand::CreateReg(Reg));
1247 Inst.addOperand(MCOperand::CreateReg(Base));
1248 Inst.addOperand(MCOperand::CreateImm(Offset));
1249
1250 return MCDisassembler::Success;
1251}
1252
Akira Hatanaka71928e62012-04-17 18:03:21 +00001253static DecodeStatus DecodeFMem(MCInst &Inst,
1254 unsigned Insn,
1255 uint64_t Address,
1256 const void *Decoder) {
1257 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001258 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1259 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001260
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001261 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001262 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001263
1264 Inst.addOperand(MCOperand::CreateReg(Reg));
1265 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001266 Inst.addOperand(MCOperand::CreateImm(Offset));
1267
1268 return MCDisassembler::Success;
1269}
1270
Daniel Sanders92db6b72014-10-01 08:26:55 +00001271static DecodeStatus DecodeFMem2(MCInst &Inst,
1272 unsigned Insn,
1273 uint64_t Address,
1274 const void *Decoder) {
1275 int Offset = SignExtend32<16>(Insn & 0xffff);
1276 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1277 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1278
1279 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1280 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1281
1282 Inst.addOperand(MCOperand::CreateReg(Reg));
1283 Inst.addOperand(MCOperand::CreateReg(Base));
1284 Inst.addOperand(MCOperand::CreateImm(Offset));
1285
1286 return MCDisassembler::Success;
1287}
1288
1289static DecodeStatus DecodeFMem3(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, 16, 5);
1295 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1296
1297 Reg = getReg(Decoder, Mips::COP3RegClassID, 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
Daniel Sanders6a803f62014-06-16 13:13:03 +00001307static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1308 unsigned Insn,
1309 uint64_t Address,
1310 const void *Decoder) {
1311 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1312 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1313 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1314
1315 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1316 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1317
1318 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1319 Inst.addOperand(MCOperand::CreateReg(Rt));
1320 }
1321
1322 Inst.addOperand(MCOperand::CreateReg(Rt));
1323 Inst.addOperand(MCOperand::CreateReg(Base));
1324 Inst.addOperand(MCOperand::CreateImm(Offset));
1325
1326 return MCDisassembler::Success;
1327}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001328
1329static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1330 unsigned RegNo,
1331 uint64_t Address,
1332 const void *Decoder) {
1333 // Currently only hardware register 29 is supported.
1334 if (RegNo != 29)
1335 return MCDisassembler::Fail;
1336 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1337 return MCDisassembler::Success;
1338}
1339
Akira Hatanaka71928e62012-04-17 18:03:21 +00001340static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1341 unsigned RegNo,
1342 uint64_t Address,
1343 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001344 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001345 return MCDisassembler::Fail;
1346
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001347 ;
1348 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1349 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001350 return MCDisassembler::Success;
1351}
1352
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001353static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1354 unsigned RegNo,
1355 uint64_t Address,
1356 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001357 if (RegNo >= 4)
1358 return MCDisassembler::Fail;
1359
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001360 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001361 Inst.addOperand(MCOperand::CreateReg(Reg));
1362 return MCDisassembler::Success;
1363}
1364
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001365static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1366 unsigned RegNo,
1367 uint64_t Address,
1368 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001369 if (RegNo >= 4)
1370 return MCDisassembler::Fail;
1371
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001372 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001373 Inst.addOperand(MCOperand::CreateReg(Reg));
1374 return MCDisassembler::Success;
1375}
1376
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001377static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1378 unsigned RegNo,
1379 uint64_t Address,
1380 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001381 if (RegNo >= 4)
1382 return MCDisassembler::Fail;
1383
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001384 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001385 Inst.addOperand(MCOperand::CreateReg(Reg));
1386 return MCDisassembler::Success;
1387}
1388
Jack Carter3eb663b2013-09-26 00:09:46 +00001389static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1390 unsigned RegNo,
1391 uint64_t Address,
1392 const void *Decoder) {
1393 if (RegNo > 31)
1394 return MCDisassembler::Fail;
1395
1396 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1397 Inst.addOperand(MCOperand::CreateReg(Reg));
1398 return MCDisassembler::Success;
1399}
1400
Jack Carter5dc8ac92013-09-25 23:50:44 +00001401static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1402 unsigned RegNo,
1403 uint64_t Address,
1404 const void *Decoder) {
1405 if (RegNo > 31)
1406 return MCDisassembler::Fail;
1407
1408 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1409 Inst.addOperand(MCOperand::CreateReg(Reg));
1410 return MCDisassembler::Success;
1411}
1412
1413static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1414 unsigned RegNo,
1415 uint64_t Address,
1416 const void *Decoder) {
1417 if (RegNo > 31)
1418 return MCDisassembler::Fail;
1419
1420 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1421 Inst.addOperand(MCOperand::CreateReg(Reg));
1422 return MCDisassembler::Success;
1423}
1424
1425static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1426 unsigned RegNo,
1427 uint64_t Address,
1428 const void *Decoder) {
1429 if (RegNo > 31)
1430 return MCDisassembler::Fail;
1431
1432 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1433 Inst.addOperand(MCOperand::CreateReg(Reg));
1434 return MCDisassembler::Success;
1435}
1436
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001437static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1438 unsigned RegNo,
1439 uint64_t Address,
1440 const void *Decoder) {
1441 if (RegNo > 7)
1442 return MCDisassembler::Fail;
1443
1444 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1445 Inst.addOperand(MCOperand::CreateReg(Reg));
1446 return MCDisassembler::Success;
1447}
1448
Daniel Sanders2a83d682014-05-21 12:56:39 +00001449static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1450 unsigned RegNo,
1451 uint64_t Address,
1452 const void *Decoder) {
1453 if (RegNo > 31)
1454 return MCDisassembler::Fail;
1455
1456 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1457 Inst.addOperand(MCOperand::CreateReg(Reg));
1458 return MCDisassembler::Success;
1459}
1460
Akira Hatanaka71928e62012-04-17 18:03:21 +00001461static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1462 unsigned Offset,
1463 uint64_t Address,
1464 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001465 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001466 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1467 return MCDisassembler::Success;
1468}
1469
Akira Hatanaka71928e62012-04-17 18:03:21 +00001470static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1471 unsigned Insn,
1472 uint64_t Address,
1473 const void *Decoder) {
1474
Jim Grosbachecaef492012-08-14 19:06:05 +00001475 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001476 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1477 return MCDisassembler::Success;
1478}
1479
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001480static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1481 unsigned Offset,
1482 uint64_t Address,
1483 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001484 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001485
1486 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1487 return MCDisassembler::Success;
1488}
1489
1490static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1491 unsigned Offset,
1492 uint64_t Address,
1493 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001494 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001495
1496 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1497 return MCDisassembler::Success;
1498}
1499
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001500static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1501 unsigned Offset,
1502 uint64_t Address,
1503 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001504 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001505 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1506 return MCDisassembler::Success;
1507}
1508
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001509static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1510 unsigned Insn,
1511 uint64_t Address,
1512 const void *Decoder) {
1513 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1514 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1515 return MCDisassembler::Success;
1516}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001517
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001518static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1519 unsigned Value,
1520 uint64_t Address,
1521 const void *Decoder) {
1522 if (Value == 0)
1523 Inst.addOperand(MCOperand::CreateImm(1));
1524 else if (Value == 0x7)
1525 Inst.addOperand(MCOperand::CreateImm(-1));
1526 else
1527 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1528 return MCDisassembler::Success;
1529}
1530
1531static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1532 unsigned Value,
1533 uint64_t Address,
1534 const void *Decoder) {
1535 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1536 return MCDisassembler::Success;
1537}
1538
1539static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1540 unsigned Value,
1541 uint64_t Address,
1542 const void *Decoder) {
1543 if (Value == 0x7F)
1544 Inst.addOperand(MCOperand::CreateImm(-1));
1545 else
1546 Inst.addOperand(MCOperand::CreateImm(Value));
1547 return MCDisassembler::Success;
1548}
1549
1550static DecodeStatus DecodeSimm4(MCInst &Inst,
1551 unsigned Value,
1552 uint64_t Address,
1553 const void *Decoder) {
1554 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1555 return MCDisassembler::Success;
1556}
1557
Akira Hatanaka71928e62012-04-17 18:03:21 +00001558static DecodeStatus DecodeSimm16(MCInst &Inst,
1559 unsigned Insn,
1560 uint64_t Address,
1561 const void *Decoder) {
1562 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1563 return MCDisassembler::Success;
1564}
1565
Matheus Almeida779c5932013-11-18 12:32:49 +00001566static DecodeStatus DecodeLSAImm(MCInst &Inst,
1567 unsigned Insn,
1568 uint64_t Address,
1569 const void *Decoder) {
1570 // We add one to the immediate field as it was encoded as 'imm - 1'.
1571 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1572 return MCDisassembler::Success;
1573}
1574
Akira Hatanaka71928e62012-04-17 18:03:21 +00001575static DecodeStatus DecodeInsSize(MCInst &Inst,
1576 unsigned Insn,
1577 uint64_t Address,
1578 const void *Decoder) {
1579 // First we need to grab the pos(lsb) from MCInst.
1580 int Pos = Inst.getOperand(2).getImm();
1581 int Size = (int) Insn - Pos + 1;
1582 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1583 return MCDisassembler::Success;
1584}
1585
1586static DecodeStatus DecodeExtSize(MCInst &Inst,
1587 unsigned Insn,
1588 uint64_t Address,
1589 const void *Decoder) {
1590 int Size = (int) Insn + 1;
1591 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1592 return MCDisassembler::Success;
1593}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001594
1595static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1596 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001597 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001598 return MCDisassembler::Success;
1599}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001600
1601static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1602 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001603 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001604 return MCDisassembler::Success;
1605}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001606
Vladimir Medicb682ddf2014-12-01 11:12:04 +00001607static DecodeStatus DecodeSimm9SP(MCInst &Inst, unsigned Insn,
1608 uint64_t Address, const void *Decoder) {
1609 int32_t DecodedValue;
1610 switch (Insn) {
1611 case 0: DecodedValue = 256; break;
1612 case 1: DecodedValue = 257; break;
1613 case 510: DecodedValue = -258; break;
1614 case 511: DecodedValue = -257; break;
1615 default: DecodedValue = SignExtend32<9>(Insn); break;
1616 }
1617 Inst.addOperand(MCOperand::CreateImm(DecodedValue << 2));
1618 return MCDisassembler::Success;
1619}
1620
1621static DecodeStatus DecodeANDI16Imm(MCInst &Inst, unsigned Insn,
1622 uint64_t Address, const void *Decoder) {
1623 // Insn must be >= 0, since it is unsigned that condition is always true.
1624 assert(Insn < 16);
1625 int32_t DecodedValues[] = {128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64,
1626 255, 32768, 65535};
1627 Inst.addOperand(MCOperand::CreateImm(DecodedValues[Insn]));
1628 return MCDisassembler::Success;
1629}
1630
1631static DecodeStatus DecodeUImm5lsl2(MCInst &Inst, unsigned Insn,
1632 uint64_t Address, const void *Decoder) {
1633 Inst.addOperand(MCOperand::CreateImm(Insn << 2));
1634 return MCDisassembler::Success;
1635}
1636
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001637static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1638 unsigned Insn,
1639 uint64_t Address,
1640 const void *Decoder) {
1641 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1642 Mips::S6, Mips::FP};
1643 unsigned RegNum;
1644
1645 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1646 // Empty register lists are not allowed.
1647 if (RegLst == 0)
1648 return MCDisassembler::Fail;
1649
1650 RegNum = RegLst & 0xf;
1651 for (unsigned i = 0; i < RegNum; i++)
1652 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1653
1654 if (RegLst & 0x10)
1655 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1656
1657 return MCDisassembler::Success;
1658}
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001659
1660static DecodeStatus DecodeRegListOperand16(MCInst &Inst, unsigned Insn,
1661 uint64_t Address,
1662 const void *Decoder) {
1663 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3};
1664 unsigned RegNum;
1665
1666 unsigned RegLst = fieldFromInstruction(Insn, 4, 2);
1667 // Empty register lists are not allowed.
1668 if (RegLst == 0)
1669 return MCDisassembler::Fail;
1670
1671 RegNum = RegLst & 0x3;
1672 for (unsigned i = 0; i < RegNum - 1; i++)
1673 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1674
1675 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1676
1677 return MCDisassembler::Success;
1678}