blob: 62ad7cfc2542c5e25f6432522b53be1ba558f928 [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
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000255static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
256 uint64_t Address, const void *Decoder);
257
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000258static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
259 unsigned Insn,
260 uint64_t Address,
261 const void *Decoder);
262
Vladimir Medicdde3d582013-09-06 12:30:36 +0000263static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
264 unsigned Insn,
265 uint64_t Address,
266 const void *Decoder);
267
268static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
269 unsigned Insn,
270 uint64_t Address,
271 const void *Decoder);
272
Akira Hatanaka71928e62012-04-17 18:03:21 +0000273static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
274 uint64_t Address,
275 const void *Decoder);
276
Daniel Sanders92db6b72014-10-01 08:26:55 +0000277static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
278 uint64_t Address,
279 const void *Decoder);
280
281static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
282 uint64_t Address,
283 const void *Decoder);
284
Daniel Sanders6a803f62014-06-16 13:13:03 +0000285static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
286 unsigned Insn,
287 uint64_t Address,
288 const void *Decoder);
289
Akira Hatanaka71928e62012-04-17 18:03:21 +0000290static DecodeStatus DecodeSimm16(MCInst &Inst,
291 unsigned Insn,
292 uint64_t Address,
293 const void *Decoder);
294
Matheus Almeida779c5932013-11-18 12:32:49 +0000295// Decode the immediate field of an LSA instruction which
296// is off by one.
297static DecodeStatus DecodeLSAImm(MCInst &Inst,
298 unsigned Insn,
299 uint64_t Address,
300 const void *Decoder);
301
Akira Hatanaka71928e62012-04-17 18:03:21 +0000302static DecodeStatus DecodeInsSize(MCInst &Inst,
303 unsigned Insn,
304 uint64_t Address,
305 const void *Decoder);
306
307static DecodeStatus DecodeExtSize(MCInst &Inst,
308 unsigned Insn,
309 uint64_t Address,
310 const void *Decoder);
311
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000312static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
313 uint64_t Address, const void *Decoder);
314
Zoran Jovanovic28551422014-06-09 09:49:51 +0000315static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
316 uint64_t Address, const void *Decoder);
317
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000318/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
319/// handle.
320template <typename InsnType>
321static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
322 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000323
324template <typename InsnType>
325static DecodeStatus
326DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
327 const void *Decoder);
328
329template <typename InsnType>
330static DecodeStatus
331DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
332 const void *Decoder);
333
334template <typename InsnType>
335static DecodeStatus
336DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
337 const void *Decoder);
338
339template <typename InsnType>
340static DecodeStatus
341DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
342 const void *Decoder);
343
344template <typename InsnType>
345static DecodeStatus
346DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
347 const void *Decoder);
348
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000349template <typename InsnType>
350static DecodeStatus
351DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
352 const void *Decoder);
353
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000354static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
355 uint64_t Address,
356 const void *Decoder);
357
Akira Hatanaka71928e62012-04-17 18:03:21 +0000358namespace llvm {
359extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
360 TheMips64elTarget;
361}
362
363static MCDisassembler *createMipsDisassembler(
364 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000365 const MCSubtargetInfo &STI,
366 MCContext &Ctx) {
367 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000368}
369
370static MCDisassembler *createMipselDisassembler(
371 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000372 const MCSubtargetInfo &STI,
373 MCContext &Ctx) {
374 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000375}
376
377static MCDisassembler *createMips64Disassembler(
378 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000379 const MCSubtargetInfo &STI,
380 MCContext &Ctx) {
381 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000382}
383
384static MCDisassembler *createMips64elDisassembler(
385 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000386 const MCSubtargetInfo &STI,
387 MCContext &Ctx) {
388 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000389}
390
391extern "C" void LLVMInitializeMipsDisassembler() {
392 // Register the disassembler.
393 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
394 createMipsDisassembler);
395 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
396 createMipselDisassembler);
397 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
398 createMips64Disassembler);
399 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
400 createMips64elDisassembler);
401}
402
Akira Hatanaka71928e62012-04-17 18:03:21 +0000403#include "MipsGenDisassemblerTables.inc"
404
Daniel Sanders5c582b22014-05-22 11:23:21 +0000405static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
406 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
407 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
408 return *(RegInfo->getRegClass(RC).begin() + RegNo);
409}
410
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000411template <typename InsnType>
412static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
413 const void *Decoder) {
414 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
415 // The size of the n field depends on the element size
416 // The register class also depends on this.
417 InsnType tmp = fieldFromInstruction(insn, 17, 5);
418 unsigned NSize = 0;
419 DecodeFN RegDecoder = nullptr;
420 if ((tmp & 0x18) == 0x00) { // INSVE_B
421 NSize = 4;
422 RegDecoder = DecodeMSA128BRegisterClass;
423 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
424 NSize = 3;
425 RegDecoder = DecodeMSA128HRegisterClass;
426 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
427 NSize = 2;
428 RegDecoder = DecodeMSA128WRegisterClass;
429 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
430 NSize = 1;
431 RegDecoder = DecodeMSA128DRegisterClass;
432 } else
433 llvm_unreachable("Invalid encoding");
434
435 assert(NSize != 0 && RegDecoder != nullptr);
436
437 // $wd
438 tmp = fieldFromInstruction(insn, 6, 5);
439 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
440 return MCDisassembler::Fail;
441 // $wd_in
442 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
443 return MCDisassembler::Fail;
444 // $n
445 tmp = fieldFromInstruction(insn, 16, NSize);
446 MI.addOperand(MCOperand::CreateImm(tmp));
447 // $ws
448 tmp = fieldFromInstruction(insn, 11, 5);
449 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
450 return MCDisassembler::Fail;
451 // $n2
452 MI.addOperand(MCOperand::CreateImm(0));
453
454 return MCDisassembler::Success;
455}
456
Daniel Sanders5c582b22014-05-22 11:23:21 +0000457template <typename InsnType>
458static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
459 uint64_t Address,
460 const void *Decoder) {
461 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
462 // (otherwise we would have matched the ADDI instruction from the earlier
463 // ISA's instead).
464 //
465 // We have:
466 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
467 // BOVC if rs >= rt
468 // BEQZALC if rs == 0 && rt != 0
469 // BEQC if rs < rt && rs != 0
470
471 InsnType Rs = fieldFromInstruction(insn, 21, 5);
472 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000473 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000474 bool HasRs = false;
475
476 if (Rs >= Rt) {
477 MI.setOpcode(Mips::BOVC);
478 HasRs = true;
479 } else if (Rs != 0 && Rs < Rt) {
480 MI.setOpcode(Mips::BEQC);
481 HasRs = true;
482 } else
483 MI.setOpcode(Mips::BEQZALC);
484
485 if (HasRs)
486 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
487 Rs)));
488
489 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
490 Rt)));
491 MI.addOperand(MCOperand::CreateImm(Imm));
492
493 return MCDisassembler::Success;
494}
495
496template <typename InsnType>
497static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
498 uint64_t Address,
499 const void *Decoder) {
500 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
501 // (otherwise we would have matched the ADDI instruction from the earlier
502 // ISA's instead).
503 //
504 // We have:
505 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
506 // BNVC if rs >= rt
507 // BNEZALC if rs == 0 && rt != 0
508 // BNEC if rs < rt && rs != 0
509
510 InsnType Rs = fieldFromInstruction(insn, 21, 5);
511 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000512 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000513 bool HasRs = false;
514
515 if (Rs >= Rt) {
516 MI.setOpcode(Mips::BNVC);
517 HasRs = true;
518 } else if (Rs != 0 && Rs < Rt) {
519 MI.setOpcode(Mips::BNEC);
520 HasRs = true;
521 } else
522 MI.setOpcode(Mips::BNEZALC);
523
524 if (HasRs)
525 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
526 Rs)));
527
528 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
529 Rt)));
530 MI.addOperand(MCOperand::CreateImm(Imm));
531
532 return MCDisassembler::Success;
533}
534
535template <typename InsnType>
536static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
537 uint64_t Address,
538 const void *Decoder) {
539 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
540 // (otherwise we would have matched the BLEZL instruction from the earlier
541 // ISA's instead).
542 //
543 // We have:
544 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
545 // Invalid if rs == 0
546 // BLEZC if rs == 0 && rt != 0
547 // BGEZC if rs == rt && rt != 0
548 // BGEC if rs != rt && rs != 0 && rt != 0
549
550 InsnType Rs = fieldFromInstruction(insn, 21, 5);
551 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000552 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000553 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000554
555 if (Rt == 0)
556 return MCDisassembler::Fail;
557 else if (Rs == 0)
558 MI.setOpcode(Mips::BLEZC);
559 else if (Rs == Rt)
560 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000561 else {
562 HasRs = true;
563 MI.setOpcode(Mips::BGEC);
564 }
565
566 if (HasRs)
567 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
568 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000569
570 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
571 Rt)));
572
573 MI.addOperand(MCOperand::CreateImm(Imm));
574
575 return MCDisassembler::Success;
576}
577
578template <typename InsnType>
579static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
580 uint64_t Address,
581 const void *Decoder) {
582 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
583 // (otherwise we would have matched the BGTZL instruction from the earlier
584 // ISA's instead).
585 //
586 // We have:
587 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
588 // Invalid if rs == 0
589 // BGTZC if rs == 0 && rt != 0
590 // BLTZC if rs == rt && rt != 0
591 // BLTC if rs != rt && rs != 0 && rt != 0
592
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000593 bool HasRs = false;
594
Daniel Sanders5c582b22014-05-22 11:23:21 +0000595 InsnType Rs = fieldFromInstruction(insn, 21, 5);
596 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000597 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000598
599 if (Rt == 0)
600 return MCDisassembler::Fail;
601 else if (Rs == 0)
602 MI.setOpcode(Mips::BGTZC);
603 else if (Rs == Rt)
604 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000605 else {
606 MI.setOpcode(Mips::BLTC);
607 HasRs = true;
608 }
609
610 if (HasRs)
611 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
612 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000613
614 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
615 Rt)));
616
617 MI.addOperand(MCOperand::CreateImm(Imm));
618
619 return MCDisassembler::Success;
620}
621
622template <typename InsnType>
623static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
624 uint64_t Address,
625 const void *Decoder) {
626 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
627 // (otherwise we would have matched the BGTZ instruction from the earlier
628 // ISA's instead).
629 //
630 // We have:
631 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
632 // BGTZ if rt == 0
633 // BGTZALC if rs == 0 && rt != 0
634 // BLTZALC if rs != 0 && rs == rt
635 // BLTUC if rs != 0 && rs != rt
636
637 InsnType Rs = fieldFromInstruction(insn, 21, 5);
638 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000639 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000640 bool HasRs = false;
641 bool HasRt = false;
642
643 if (Rt == 0) {
644 MI.setOpcode(Mips::BGTZ);
645 HasRs = true;
646 } else if (Rs == 0) {
647 MI.setOpcode(Mips::BGTZALC);
648 HasRt = true;
649 } else if (Rs == Rt) {
650 MI.setOpcode(Mips::BLTZALC);
651 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000652 } else {
653 MI.setOpcode(Mips::BLTUC);
654 HasRs = true;
655 HasRt = true;
656 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000657
658 if (HasRs)
659 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
660 Rs)));
661
662 if (HasRt)
663 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
664 Rt)));
665
666 MI.addOperand(MCOperand::CreateImm(Imm));
667
668 return MCDisassembler::Success;
669}
670
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000671template <typename InsnType>
672static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
673 uint64_t Address,
674 const void *Decoder) {
675 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
676 // (otherwise we would have matched the BLEZL instruction from the earlier
677 // ISA's instead).
678 //
679 // We have:
680 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
681 // Invalid if rs == 0
682 // BLEZALC if rs == 0 && rt != 0
683 // BGEZALC if rs == rt && rt != 0
684 // BGEUC if rs != rt && rs != 0 && rt != 0
685
686 InsnType Rs = fieldFromInstruction(insn, 21, 5);
687 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000688 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000689 bool HasRs = false;
690
691 if (Rt == 0)
692 return MCDisassembler::Fail;
693 else if (Rs == 0)
694 MI.setOpcode(Mips::BLEZALC);
695 else if (Rs == Rt)
696 MI.setOpcode(Mips::BGEZALC);
697 else {
698 HasRs = true;
699 MI.setOpcode(Mips::BGEUC);
700 }
701
702 if (HasRs)
703 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
704 Rs)));
705 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
706 Rt)));
707
708 MI.addOperand(MCOperand::CreateImm(Imm));
709
710 return MCDisassembler::Success;
711}
712
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000713/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
714/// according to the given endianess.
715static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
716 uint64_t &Size, uint32_t &Insn,
717 bool IsBigEndian) {
718 // We want to read exactly 2 Bytes of data.
719 if (Bytes.size() < 2) {
720 Size = 0;
721 return MCDisassembler::Fail;
722 }
723
724 if (IsBigEndian) {
725 Insn = (Bytes[0] << 8) | Bytes[1];
726 } else {
727 Insn = (Bytes[1] << 8) | Bytes[0];
728 }
729
730 return MCDisassembler::Success;
731}
732
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000733/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000734/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000735static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
736 uint64_t &Size, uint32_t &Insn,
737 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000738 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000739 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000740 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000741 return MCDisassembler::Fail;
742 }
743
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000744 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
745 // always precede the low 16 bits in the instruction stream (that is, they
746 // are placed at lower addresses in the instruction stream).
747 //
748 // microMIPS byte ordering:
749 // Big-endian: 0 | 1 | 2 | 3
750 // Little-endian: 1 | 0 | 3 | 2
751
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000752 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000753 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000754 Insn =
755 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
756 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000757 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000758 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000759 (Bytes[1] << 24);
760 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000761 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000762 (Bytes[3] << 24);
763 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000764 }
765
766 return MCDisassembler::Success;
767}
768
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000769DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000770 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000771 uint64_t Address,
772 raw_ostream &VStream,
773 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000774 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000775 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000776
Vladimir Medicdde3d582013-09-06 12:30:36 +0000777 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000778 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
779
780 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
781 // Calling the auto-generated decoder function.
782 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
783 this, STI);
784 if (Result != MCDisassembler::Fail) {
785 Size = 2;
786 return Result;
787 }
788
789 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
790 if (Result == MCDisassembler::Fail)
791 return MCDisassembler::Fail;
792
793 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000794 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000795 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000796 this, STI);
797 if (Result != MCDisassembler::Fail) {
798 Size = 4;
799 return Result;
800 }
801 return MCDisassembler::Fail;
802 }
803
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000804 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
805 if (Result == MCDisassembler::Fail)
806 return MCDisassembler::Fail;
807
Daniel Sandersc171f652014-06-13 13:15:59 +0000808 if (hasCOP3()) {
809 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
810 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000811 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000812 if (Result != MCDisassembler::Fail) {
813 Size = 4;
814 return Result;
815 }
816 }
817
818 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000819 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000820 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000821 Address, this, STI);
822 if (Result != MCDisassembler::Fail) {
823 Size = 4;
824 return Result;
825 }
826 }
827
Daniel Sandersc171f652014-06-13 13:15:59 +0000828 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000829 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000830 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000831 Address, this, STI);
832 if (Result != MCDisassembler::Fail) {
833 Size = 4;
834 return Result;
835 }
836 }
837
Daniel Sanders0fa60412014-06-12 13:39:06 +0000838 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000839 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000840 Result =
841 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000842 if (Result != MCDisassembler::Fail) {
843 Size = 4;
844 return Result;
845 }
846
847 return MCDisassembler::Fail;
848}
849
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000850DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000851 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000852 uint64_t Address,
853 raw_ostream &VStream,
854 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000855 uint32_t Insn;
856
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000857 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000858 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000859 if (Result == MCDisassembler::Fail)
860 return MCDisassembler::Fail;
861
862 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000863 Result =
864 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000865 if (Result != MCDisassembler::Fail) {
866 Size = 4;
867 return Result;
868 }
869 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000870 Result =
871 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000872 if (Result != MCDisassembler::Fail) {
873 Size = 4;
874 return Result;
875 }
876
877 return MCDisassembler::Fail;
878}
879
Reed Kotlerec8a5492013-02-14 03:05:25 +0000880static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
881 unsigned RegNo,
882 uint64_t Address,
883 const void *Decoder) {
884
885 return MCDisassembler::Fail;
886
887}
888
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000889static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
890 unsigned RegNo,
891 uint64_t Address,
892 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000893
894 if (RegNo > 31)
895 return MCDisassembler::Fail;
896
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000897 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000898 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000899 return MCDisassembler::Success;
900}
901
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000902static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
903 unsigned RegNo,
904 uint64_t Address,
905 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000906 if (RegNo > 7)
907 return MCDisassembler::Fail;
908 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
909 Inst.addOperand(MCOperand::CreateReg(Reg));
910 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000911}
912
Jozef Kolek1904fa22014-11-24 14:25:53 +0000913static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
914 unsigned RegNo,
915 uint64_t Address,
916 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000917 if (RegNo > 7)
918 return MCDisassembler::Fail;
919 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
920 Inst.addOperand(MCOperand::CreateReg(Reg));
921 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000922}
923
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000924static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
925 unsigned RegNo,
926 uint64_t Address,
927 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000928 if (RegNo > 31)
929 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000930 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000931 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000932 return MCDisassembler::Success;
933}
934
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000935static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
936 unsigned RegNo,
937 uint64_t Address,
938 const void *Decoder) {
939 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
940 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
941
942 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
943}
944
Akira Hatanaka654655f2013-08-14 00:53:38 +0000945static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
946 unsigned RegNo,
947 uint64_t Address,
948 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000949 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000950}
951
Akira Hatanaka71928e62012-04-17 18:03:21 +0000952static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
953 unsigned RegNo,
954 uint64_t Address,
955 const void *Decoder) {
956 if (RegNo > 31)
957 return MCDisassembler::Fail;
958
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000959 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
960 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000961 return MCDisassembler::Success;
962}
963
964static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
965 unsigned RegNo,
966 uint64_t Address,
967 const void *Decoder) {
968 if (RegNo > 31)
969 return MCDisassembler::Fail;
970
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000971 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
972 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000973 return MCDisassembler::Success;
974}
975
976static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
977 unsigned RegNo,
978 uint64_t Address,
979 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +0000980 if (RegNo > 31)
981 return MCDisassembler::Fail;
982 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
983 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000984 return MCDisassembler::Success;
985}
986
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000987static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
988 unsigned RegNo,
989 uint64_t Address,
990 const void *Decoder) {
991 if (RegNo > 7)
992 return MCDisassembler::Fail;
993 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
994 Inst.addOperand(MCOperand::CreateReg(Reg));
995 return MCDisassembler::Success;
996}
997
Daniel Sanders0fa60412014-06-12 13:39:06 +0000998static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
999 uint64_t Address,
1000 const void *Decoder) {
1001 if (RegNo > 31)
1002 return MCDisassembler::Fail;
1003
1004 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1005 Inst.addOperand(MCOperand::CreateReg(Reg));
1006 return MCDisassembler::Success;
1007}
1008
Akira Hatanaka71928e62012-04-17 18:03:21 +00001009static DecodeStatus DecodeMem(MCInst &Inst,
1010 unsigned Insn,
1011 uint64_t Address,
1012 const void *Decoder) {
1013 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001014 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1015 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001016
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001017 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1018 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001019
1020 if(Inst.getOpcode() == Mips::SC){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001021 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001022 }
1023
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001024 Inst.addOperand(MCOperand::CreateReg(Reg));
1025 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001026 Inst.addOperand(MCOperand::CreateImm(Offset));
1027
1028 return MCDisassembler::Success;
1029}
1030
Daniel Sanders92db6b72014-10-01 08:26:55 +00001031static DecodeStatus DecodeCacheOp(MCInst &Inst,
1032 unsigned Insn,
1033 uint64_t Address,
1034 const void *Decoder) {
1035 int Offset = SignExtend32<16>(Insn & 0xffff);
1036 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1037 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1038
1039 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1040
1041 Inst.addOperand(MCOperand::CreateReg(Base));
1042 Inst.addOperand(MCOperand::CreateImm(Offset));
1043 Inst.addOperand(MCOperand::CreateImm(Hint));
1044
1045 return MCDisassembler::Success;
1046}
1047
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001048static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1049 uint64_t Address, const void *Decoder) {
1050 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1051 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1052 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1053
1054 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1055 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1056
1057 Inst.addOperand(MCOperand::CreateReg(Reg));
1058 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001059
1060 // The immediate field of an LD/ST instruction is scaled which means it must
1061 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1062 // data format.
1063 // .b - 1 byte
1064 // .h - 2 bytes
1065 // .w - 4 bytes
1066 // .d - 8 bytes
1067 switch(Inst.getOpcode())
1068 {
1069 default:
1070 assert (0 && "Unexpected instruction");
1071 return MCDisassembler::Fail;
1072 break;
1073 case Mips::LD_B:
1074 case Mips::ST_B:
1075 Inst.addOperand(MCOperand::CreateImm(Offset));
1076 break;
1077 case Mips::LD_H:
1078 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001079 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001080 break;
1081 case Mips::LD_W:
1082 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001083 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001084 break;
1085 case Mips::LD_D:
1086 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001087 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001088 break;
1089 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001090
1091 return MCDisassembler::Success;
1092}
1093
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001094static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1095 unsigned Insn,
1096 uint64_t Address,
1097 const void *Decoder) {
1098 unsigned Offset = Insn & 0xf;
1099 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1100 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1101
1102 switch (Inst.getOpcode()) {
1103 case Mips::LBU16_MM:
1104 case Mips::LHU16_MM:
1105 case Mips::LW16_MM:
1106 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1107 == MCDisassembler::Fail)
1108 return MCDisassembler::Fail;
1109 break;
1110 case Mips::SB16_MM:
1111 case Mips::SH16_MM:
1112 case Mips::SW16_MM:
1113 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1114 == MCDisassembler::Fail)
1115 return MCDisassembler::Fail;
1116 break;
1117 }
1118
1119 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1120 == MCDisassembler::Fail)
1121 return MCDisassembler::Fail;
1122
1123 switch (Inst.getOpcode()) {
1124 case Mips::LBU16_MM:
1125 if (Offset == 0xf)
1126 Inst.addOperand(MCOperand::CreateImm(-1));
1127 else
1128 Inst.addOperand(MCOperand::CreateImm(Offset));
1129 break;
1130 case Mips::SB16_MM:
1131 Inst.addOperand(MCOperand::CreateImm(Offset));
1132 break;
1133 case Mips::LHU16_MM:
1134 case Mips::SH16_MM:
1135 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1136 break;
1137 case Mips::LW16_MM:
1138 case Mips::SW16_MM:
1139 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1140 break;
1141 }
1142
1143 return MCDisassembler::Success;
1144}
1145
Vladimir Medicdde3d582013-09-06 12:30:36 +00001146static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1147 unsigned Insn,
1148 uint64_t Address,
1149 const void *Decoder) {
1150 int Offset = SignExtend32<12>(Insn & 0x0fff);
1151 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1152 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1153
1154 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1155 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1156
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001157 switch (Inst.getOpcode()) {
1158 case Mips::SWM32_MM:
1159 case Mips::LWM32_MM:
1160 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1161 == MCDisassembler::Fail)
1162 return MCDisassembler::Fail;
1163 Inst.addOperand(MCOperand::CreateReg(Base));
1164 Inst.addOperand(MCOperand::CreateImm(Offset));
1165 break;
1166 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001167 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001168 // fallthrough
1169 default:
1170 Inst.addOperand(MCOperand::CreateReg(Reg));
1171 Inst.addOperand(MCOperand::CreateReg(Base));
1172 Inst.addOperand(MCOperand::CreateImm(Offset));
1173 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001174
1175 return MCDisassembler::Success;
1176}
1177
1178static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1179 unsigned Insn,
1180 uint64_t Address,
1181 const void *Decoder) {
1182 int Offset = SignExtend32<16>(Insn & 0xffff);
1183 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1184 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1185
1186 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1187 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1188
1189 Inst.addOperand(MCOperand::CreateReg(Reg));
1190 Inst.addOperand(MCOperand::CreateReg(Base));
1191 Inst.addOperand(MCOperand::CreateImm(Offset));
1192
1193 return MCDisassembler::Success;
1194}
1195
Akira Hatanaka71928e62012-04-17 18:03:21 +00001196static DecodeStatus DecodeFMem(MCInst &Inst,
1197 unsigned Insn,
1198 uint64_t Address,
1199 const void *Decoder) {
1200 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001201 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1202 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001203
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001204 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001205 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001206
1207 Inst.addOperand(MCOperand::CreateReg(Reg));
1208 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001209 Inst.addOperand(MCOperand::CreateImm(Offset));
1210
1211 return MCDisassembler::Success;
1212}
1213
Daniel Sanders92db6b72014-10-01 08:26:55 +00001214static DecodeStatus DecodeFMem2(MCInst &Inst,
1215 unsigned Insn,
1216 uint64_t Address,
1217 const void *Decoder) {
1218 int Offset = SignExtend32<16>(Insn & 0xffff);
1219 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1220 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1221
1222 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1223 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1224
1225 Inst.addOperand(MCOperand::CreateReg(Reg));
1226 Inst.addOperand(MCOperand::CreateReg(Base));
1227 Inst.addOperand(MCOperand::CreateImm(Offset));
1228
1229 return MCDisassembler::Success;
1230}
1231
1232static DecodeStatus DecodeFMem3(MCInst &Inst,
1233 unsigned Insn,
1234 uint64_t Address,
1235 const void *Decoder) {
1236 int Offset = SignExtend32<16>(Insn & 0xffff);
1237 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1238 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1239
1240 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1241 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1242
1243 Inst.addOperand(MCOperand::CreateReg(Reg));
1244 Inst.addOperand(MCOperand::CreateReg(Base));
1245 Inst.addOperand(MCOperand::CreateImm(Offset));
1246
1247 return MCDisassembler::Success;
1248}
1249
Daniel Sanders6a803f62014-06-16 13:13:03 +00001250static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1251 unsigned Insn,
1252 uint64_t Address,
1253 const void *Decoder) {
1254 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1255 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1256 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1257
1258 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1259 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1260
1261 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1262 Inst.addOperand(MCOperand::CreateReg(Rt));
1263 }
1264
1265 Inst.addOperand(MCOperand::CreateReg(Rt));
1266 Inst.addOperand(MCOperand::CreateReg(Base));
1267 Inst.addOperand(MCOperand::CreateImm(Offset));
1268
1269 return MCDisassembler::Success;
1270}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001271
1272static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1273 unsigned RegNo,
1274 uint64_t Address,
1275 const void *Decoder) {
1276 // Currently only hardware register 29 is supported.
1277 if (RegNo != 29)
1278 return MCDisassembler::Fail;
1279 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1280 return MCDisassembler::Success;
1281}
1282
Akira Hatanaka71928e62012-04-17 18:03:21 +00001283static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1284 unsigned RegNo,
1285 uint64_t Address,
1286 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001287 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001288 return MCDisassembler::Fail;
1289
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001290 ;
1291 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1292 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001293 return MCDisassembler::Success;
1294}
1295
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001296static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1297 unsigned RegNo,
1298 uint64_t Address,
1299 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001300 if (RegNo >= 4)
1301 return MCDisassembler::Fail;
1302
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001303 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001304 Inst.addOperand(MCOperand::CreateReg(Reg));
1305 return MCDisassembler::Success;
1306}
1307
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001308static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1309 unsigned RegNo,
1310 uint64_t Address,
1311 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001312 if (RegNo >= 4)
1313 return MCDisassembler::Fail;
1314
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001315 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001316 Inst.addOperand(MCOperand::CreateReg(Reg));
1317 return MCDisassembler::Success;
1318}
1319
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001320static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1321 unsigned RegNo,
1322 uint64_t Address,
1323 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001324 if (RegNo >= 4)
1325 return MCDisassembler::Fail;
1326
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001327 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001328 Inst.addOperand(MCOperand::CreateReg(Reg));
1329 return MCDisassembler::Success;
1330}
1331
Jack Carter3eb663b2013-09-26 00:09:46 +00001332static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1333 unsigned RegNo,
1334 uint64_t Address,
1335 const void *Decoder) {
1336 if (RegNo > 31)
1337 return MCDisassembler::Fail;
1338
1339 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1340 Inst.addOperand(MCOperand::CreateReg(Reg));
1341 return MCDisassembler::Success;
1342}
1343
Jack Carter5dc8ac92013-09-25 23:50:44 +00001344static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1345 unsigned RegNo,
1346 uint64_t Address,
1347 const void *Decoder) {
1348 if (RegNo > 31)
1349 return MCDisassembler::Fail;
1350
1351 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1352 Inst.addOperand(MCOperand::CreateReg(Reg));
1353 return MCDisassembler::Success;
1354}
1355
1356static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1357 unsigned RegNo,
1358 uint64_t Address,
1359 const void *Decoder) {
1360 if (RegNo > 31)
1361 return MCDisassembler::Fail;
1362
1363 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1364 Inst.addOperand(MCOperand::CreateReg(Reg));
1365 return MCDisassembler::Success;
1366}
1367
1368static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1369 unsigned RegNo,
1370 uint64_t Address,
1371 const void *Decoder) {
1372 if (RegNo > 31)
1373 return MCDisassembler::Fail;
1374
1375 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1376 Inst.addOperand(MCOperand::CreateReg(Reg));
1377 return MCDisassembler::Success;
1378}
1379
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001380static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1381 unsigned RegNo,
1382 uint64_t Address,
1383 const void *Decoder) {
1384 if (RegNo > 7)
1385 return MCDisassembler::Fail;
1386
1387 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1388 Inst.addOperand(MCOperand::CreateReg(Reg));
1389 return MCDisassembler::Success;
1390}
1391
Daniel Sanders2a83d682014-05-21 12:56:39 +00001392static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1393 unsigned RegNo,
1394 uint64_t Address,
1395 const void *Decoder) {
1396 if (RegNo > 31)
1397 return MCDisassembler::Fail;
1398
1399 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1400 Inst.addOperand(MCOperand::CreateReg(Reg));
1401 return MCDisassembler::Success;
1402}
1403
Akira Hatanaka71928e62012-04-17 18:03:21 +00001404static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1405 unsigned Offset,
1406 uint64_t Address,
1407 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001408 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001409 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1410 return MCDisassembler::Success;
1411}
1412
Akira Hatanaka71928e62012-04-17 18:03:21 +00001413static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1414 unsigned Insn,
1415 uint64_t Address,
1416 const void *Decoder) {
1417
Jim Grosbachecaef492012-08-14 19:06:05 +00001418 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001419 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1420 return MCDisassembler::Success;
1421}
1422
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001423static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1424 unsigned Offset,
1425 uint64_t Address,
1426 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001427 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001428
1429 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1430 return MCDisassembler::Success;
1431}
1432
1433static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1434 unsigned Offset,
1435 uint64_t Address,
1436 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001437 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001438
1439 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1440 return MCDisassembler::Success;
1441}
1442
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001443static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1444 unsigned Offset,
1445 uint64_t Address,
1446 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001447 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001448 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1449 return MCDisassembler::Success;
1450}
1451
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001452static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1453 unsigned Insn,
1454 uint64_t Address,
1455 const void *Decoder) {
1456 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1457 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1458 return MCDisassembler::Success;
1459}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001460
1461static DecodeStatus DecodeSimm16(MCInst &Inst,
1462 unsigned Insn,
1463 uint64_t Address,
1464 const void *Decoder) {
1465 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1466 return MCDisassembler::Success;
1467}
1468
Matheus Almeida779c5932013-11-18 12:32:49 +00001469static DecodeStatus DecodeLSAImm(MCInst &Inst,
1470 unsigned Insn,
1471 uint64_t Address,
1472 const void *Decoder) {
1473 // We add one to the immediate field as it was encoded as 'imm - 1'.
1474 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1475 return MCDisassembler::Success;
1476}
1477
Akira Hatanaka71928e62012-04-17 18:03:21 +00001478static DecodeStatus DecodeInsSize(MCInst &Inst,
1479 unsigned Insn,
1480 uint64_t Address,
1481 const void *Decoder) {
1482 // First we need to grab the pos(lsb) from MCInst.
1483 int Pos = Inst.getOperand(2).getImm();
1484 int Size = (int) Insn - Pos + 1;
1485 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1486 return MCDisassembler::Success;
1487}
1488
1489static DecodeStatus DecodeExtSize(MCInst &Inst,
1490 unsigned Insn,
1491 uint64_t Address,
1492 const void *Decoder) {
1493 int Size = (int) Insn + 1;
1494 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1495 return MCDisassembler::Success;
1496}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001497
1498static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1499 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001500 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001501 return MCDisassembler::Success;
1502}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001503
1504static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1505 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001506 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001507 return MCDisassembler::Success;
1508}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001509
1510static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1511 unsigned Insn,
1512 uint64_t Address,
1513 const void *Decoder) {
1514 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1515 Mips::S6, Mips::FP};
1516 unsigned RegNum;
1517
1518 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1519 // Empty register lists are not allowed.
1520 if (RegLst == 0)
1521 return MCDisassembler::Fail;
1522
1523 RegNum = RegLst & 0xf;
1524 for (unsigned i = 0; i < RegNum; i++)
1525 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1526
1527 if (RegLst & 0x10)
1528 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1529
1530 return MCDisassembler::Success;
1531}