blob: 48904ce2dee43c5abcfaf3dd723f747446b72d1a [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
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000112static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
113 unsigned RegNo,
114 uint64_t Address,
115 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000116
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000117static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
118 unsigned Insn,
119 uint64_t Address,
120 const void *Decoder);
121
Akira Hatanaka654655f2013-08-14 00:53:38 +0000122static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
123 unsigned RegNo,
124 uint64_t Address,
125 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000126
Akira Hatanaka71928e62012-04-17 18:03:21 +0000127static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
128 unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder);
131
132static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
133 unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
136
137static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
138 unsigned RegNo,
139 uint64_t Address,
140 const void *Decoder);
141
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000142static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
143 unsigned RegNo,
144 uint64_t Address,
145 const void *Decoder);
146
Daniel Sanders0fa60412014-06-12 13:39:06 +0000147static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
148 uint64_t Address,
149 const void *Decoder);
150
Akira Hatanaka71928e62012-04-17 18:03:21 +0000151static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
152 unsigned Insn,
153 uint64_t Address,
154 const void *Decoder);
155
156static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
157 unsigned RegNo,
158 uint64_t Address,
159 const void *Decoder);
160
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000161static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
162 unsigned RegNo,
163 uint64_t Address,
164 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000165
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000166static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
167 unsigned RegNo,
168 uint64_t Address,
169 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000170
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000171static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
172 unsigned RegNo,
173 uint64_t Address,
174 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000175
Jack Carter3eb663b2013-09-26 00:09:46 +0000176static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
177 unsigned RegNo,
178 uint64_t Address,
179 const void *Decoder);
180
Jack Carter5dc8ac92013-09-25 23:50:44 +0000181static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
182 unsigned RegNo,
183 uint64_t Address,
184 const void *Decoder);
185
186static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
187 unsigned RegNo,
188 uint64_t Address,
189 const void *Decoder);
190
191static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
192 unsigned RegNo,
193 uint64_t Address,
194 const void *Decoder);
195
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000196static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
197 unsigned RegNo,
198 uint64_t Address,
199 const void *Decoder);
200
Daniel Sanders2a83d682014-05-21 12:56:39 +0000201static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
202 unsigned RegNo,
203 uint64_t Address,
204 const void *Decoder);
205
Akira Hatanaka71928e62012-04-17 18:03:21 +0000206static DecodeStatus DecodeBranchTarget(MCInst &Inst,
207 unsigned Offset,
208 uint64_t Address,
209 const void *Decoder);
210
Akira Hatanaka71928e62012-04-17 18:03:21 +0000211static DecodeStatus DecodeJumpTarget(MCInst &Inst,
212 unsigned Insn,
213 uint64_t Address,
214 const void *Decoder);
215
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000216static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
217 unsigned Offset,
218 uint64_t Address,
219 const void *Decoder);
220
221static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
222 unsigned Offset,
223 uint64_t Address,
224 const void *Decoder);
225
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000226// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
227// shifted left by 1 bit.
228static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
229 unsigned Offset,
230 uint64_t Address,
231 const void *Decoder);
232
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000233// DecodeJumpTargetMM - Decode microMIPS jump target, which is
234// shifted left by 1 bit.
235static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
236 unsigned Insn,
237 uint64_t Address,
238 const void *Decoder);
239
Akira Hatanaka71928e62012-04-17 18:03:21 +0000240static DecodeStatus DecodeMem(MCInst &Inst,
241 unsigned Insn,
242 uint64_t Address,
243 const void *Decoder);
244
Daniel Sanders92db6b72014-10-01 08:26:55 +0000245static DecodeStatus DecodeCacheOp(MCInst &Inst,
246 unsigned Insn,
247 uint64_t Address,
248 const void *Decoder);
249
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000250static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
251 uint64_t Address, const void *Decoder);
252
Vladimir Medicdde3d582013-09-06 12:30:36 +0000253static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
254 unsigned Insn,
255 uint64_t Address,
256 const void *Decoder);
257
258static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
259 unsigned Insn,
260 uint64_t Address,
261 const void *Decoder);
262
Akira Hatanaka71928e62012-04-17 18:03:21 +0000263static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
264 uint64_t Address,
265 const void *Decoder);
266
Daniel Sanders92db6b72014-10-01 08:26:55 +0000267static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
268 uint64_t Address,
269 const void *Decoder);
270
271static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
272 uint64_t Address,
273 const void *Decoder);
274
Daniel Sanders6a803f62014-06-16 13:13:03 +0000275static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
276 unsigned Insn,
277 uint64_t Address,
278 const void *Decoder);
279
Akira Hatanaka71928e62012-04-17 18:03:21 +0000280static DecodeStatus DecodeSimm16(MCInst &Inst,
281 unsigned Insn,
282 uint64_t Address,
283 const void *Decoder);
284
Matheus Almeida779c5932013-11-18 12:32:49 +0000285// Decode the immediate field of an LSA instruction which
286// is off by one.
287static DecodeStatus DecodeLSAImm(MCInst &Inst,
288 unsigned Insn,
289 uint64_t Address,
290 const void *Decoder);
291
Akira Hatanaka71928e62012-04-17 18:03:21 +0000292static DecodeStatus DecodeInsSize(MCInst &Inst,
293 unsigned Insn,
294 uint64_t Address,
295 const void *Decoder);
296
297static DecodeStatus DecodeExtSize(MCInst &Inst,
298 unsigned Insn,
299 uint64_t Address,
300 const void *Decoder);
301
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000302static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
303 uint64_t Address, const void *Decoder);
304
Zoran Jovanovic28551422014-06-09 09:49:51 +0000305static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
306 uint64_t Address, const void *Decoder);
307
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000308/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
309/// handle.
310template <typename InsnType>
311static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
312 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000313
314template <typename InsnType>
315static DecodeStatus
316DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
317 const void *Decoder);
318
319template <typename InsnType>
320static DecodeStatus
321DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
322 const void *Decoder);
323
324template <typename InsnType>
325static DecodeStatus
326DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
327 const void *Decoder);
328
329template <typename InsnType>
330static DecodeStatus
331DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
332 const void *Decoder);
333
334template <typename InsnType>
335static DecodeStatus
336DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
337 const void *Decoder);
338
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000339template <typename InsnType>
340static DecodeStatus
341DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
342 const void *Decoder);
343
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000344static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
345 uint64_t Address,
346 const void *Decoder);
347
Akira Hatanaka71928e62012-04-17 18:03:21 +0000348namespace llvm {
349extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
350 TheMips64elTarget;
351}
352
353static MCDisassembler *createMipsDisassembler(
354 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000355 const MCSubtargetInfo &STI,
356 MCContext &Ctx) {
357 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000358}
359
360static MCDisassembler *createMipselDisassembler(
361 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000362 const MCSubtargetInfo &STI,
363 MCContext &Ctx) {
364 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000365}
366
367static MCDisassembler *createMips64Disassembler(
368 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000369 const MCSubtargetInfo &STI,
370 MCContext &Ctx) {
371 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000372}
373
374static MCDisassembler *createMips64elDisassembler(
375 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000376 const MCSubtargetInfo &STI,
377 MCContext &Ctx) {
378 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000379}
380
381extern "C" void LLVMInitializeMipsDisassembler() {
382 // Register the disassembler.
383 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
384 createMipsDisassembler);
385 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
386 createMipselDisassembler);
387 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
388 createMips64Disassembler);
389 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
390 createMips64elDisassembler);
391}
392
Akira Hatanaka71928e62012-04-17 18:03:21 +0000393#include "MipsGenDisassemblerTables.inc"
394
Daniel Sanders5c582b22014-05-22 11:23:21 +0000395static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
396 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
397 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
398 return *(RegInfo->getRegClass(RC).begin() + RegNo);
399}
400
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000401template <typename InsnType>
402static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
403 const void *Decoder) {
404 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
405 // The size of the n field depends on the element size
406 // The register class also depends on this.
407 InsnType tmp = fieldFromInstruction(insn, 17, 5);
408 unsigned NSize = 0;
409 DecodeFN RegDecoder = nullptr;
410 if ((tmp & 0x18) == 0x00) { // INSVE_B
411 NSize = 4;
412 RegDecoder = DecodeMSA128BRegisterClass;
413 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
414 NSize = 3;
415 RegDecoder = DecodeMSA128HRegisterClass;
416 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
417 NSize = 2;
418 RegDecoder = DecodeMSA128WRegisterClass;
419 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
420 NSize = 1;
421 RegDecoder = DecodeMSA128DRegisterClass;
422 } else
423 llvm_unreachable("Invalid encoding");
424
425 assert(NSize != 0 && RegDecoder != nullptr);
426
427 // $wd
428 tmp = fieldFromInstruction(insn, 6, 5);
429 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
430 return MCDisassembler::Fail;
431 // $wd_in
432 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
433 return MCDisassembler::Fail;
434 // $n
435 tmp = fieldFromInstruction(insn, 16, NSize);
436 MI.addOperand(MCOperand::CreateImm(tmp));
437 // $ws
438 tmp = fieldFromInstruction(insn, 11, 5);
439 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
440 return MCDisassembler::Fail;
441 // $n2
442 MI.addOperand(MCOperand::CreateImm(0));
443
444 return MCDisassembler::Success;
445}
446
Daniel Sanders5c582b22014-05-22 11:23:21 +0000447template <typename InsnType>
448static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
449 uint64_t Address,
450 const void *Decoder) {
451 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
452 // (otherwise we would have matched the ADDI instruction from the earlier
453 // ISA's instead).
454 //
455 // We have:
456 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
457 // BOVC if rs >= rt
458 // BEQZALC if rs == 0 && rt != 0
459 // BEQC if rs < rt && rs != 0
460
461 InsnType Rs = fieldFromInstruction(insn, 21, 5);
462 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000463 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000464 bool HasRs = false;
465
466 if (Rs >= Rt) {
467 MI.setOpcode(Mips::BOVC);
468 HasRs = true;
469 } else if (Rs != 0 && Rs < Rt) {
470 MI.setOpcode(Mips::BEQC);
471 HasRs = true;
472 } else
473 MI.setOpcode(Mips::BEQZALC);
474
475 if (HasRs)
476 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
477 Rs)));
478
479 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
480 Rt)));
481 MI.addOperand(MCOperand::CreateImm(Imm));
482
483 return MCDisassembler::Success;
484}
485
486template <typename InsnType>
487static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
488 uint64_t Address,
489 const void *Decoder) {
490 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
491 // (otherwise we would have matched the ADDI instruction from the earlier
492 // ISA's instead).
493 //
494 // We have:
495 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
496 // BNVC if rs >= rt
497 // BNEZALC if rs == 0 && rt != 0
498 // BNEC if rs < rt && rs != 0
499
500 InsnType Rs = fieldFromInstruction(insn, 21, 5);
501 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000502 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000503 bool HasRs = false;
504
505 if (Rs >= Rt) {
506 MI.setOpcode(Mips::BNVC);
507 HasRs = true;
508 } else if (Rs != 0 && Rs < Rt) {
509 MI.setOpcode(Mips::BNEC);
510 HasRs = true;
511 } else
512 MI.setOpcode(Mips::BNEZALC);
513
514 if (HasRs)
515 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
516 Rs)));
517
518 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
519 Rt)));
520 MI.addOperand(MCOperand::CreateImm(Imm));
521
522 return MCDisassembler::Success;
523}
524
525template <typename InsnType>
526static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
527 uint64_t Address,
528 const void *Decoder) {
529 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
530 // (otherwise we would have matched the BLEZL instruction from the earlier
531 // ISA's instead).
532 //
533 // We have:
534 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
535 // Invalid if rs == 0
536 // BLEZC if rs == 0 && rt != 0
537 // BGEZC if rs == rt && rt != 0
538 // BGEC if rs != rt && rs != 0 && rt != 0
539
540 InsnType Rs = fieldFromInstruction(insn, 21, 5);
541 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000542 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000543 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000544
545 if (Rt == 0)
546 return MCDisassembler::Fail;
547 else if (Rs == 0)
548 MI.setOpcode(Mips::BLEZC);
549 else if (Rs == Rt)
550 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000551 else {
552 HasRs = true;
553 MI.setOpcode(Mips::BGEC);
554 }
555
556 if (HasRs)
557 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
558 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000559
560 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
561 Rt)));
562
563 MI.addOperand(MCOperand::CreateImm(Imm));
564
565 return MCDisassembler::Success;
566}
567
568template <typename InsnType>
569static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
570 uint64_t Address,
571 const void *Decoder) {
572 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
573 // (otherwise we would have matched the BGTZL instruction from the earlier
574 // ISA's instead).
575 //
576 // We have:
577 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
578 // Invalid if rs == 0
579 // BGTZC if rs == 0 && rt != 0
580 // BLTZC if rs == rt && rt != 0
581 // BLTC if rs != rt && rs != 0 && rt != 0
582
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000583 bool HasRs = false;
584
Daniel Sanders5c582b22014-05-22 11:23:21 +0000585 InsnType Rs = fieldFromInstruction(insn, 21, 5);
586 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000587 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000588
589 if (Rt == 0)
590 return MCDisassembler::Fail;
591 else if (Rs == 0)
592 MI.setOpcode(Mips::BGTZC);
593 else if (Rs == Rt)
594 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000595 else {
596 MI.setOpcode(Mips::BLTC);
597 HasRs = true;
598 }
599
600 if (HasRs)
601 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
602 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000603
604 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
605 Rt)));
606
607 MI.addOperand(MCOperand::CreateImm(Imm));
608
609 return MCDisassembler::Success;
610}
611
612template <typename InsnType>
613static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
614 uint64_t Address,
615 const void *Decoder) {
616 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
617 // (otherwise we would have matched the BGTZ instruction from the earlier
618 // ISA's instead).
619 //
620 // We have:
621 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
622 // BGTZ if rt == 0
623 // BGTZALC if rs == 0 && rt != 0
624 // BLTZALC if rs != 0 && rs == rt
625 // BLTUC if rs != 0 && rs != rt
626
627 InsnType Rs = fieldFromInstruction(insn, 21, 5);
628 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000629 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000630 bool HasRs = false;
631 bool HasRt = false;
632
633 if (Rt == 0) {
634 MI.setOpcode(Mips::BGTZ);
635 HasRs = true;
636 } else if (Rs == 0) {
637 MI.setOpcode(Mips::BGTZALC);
638 HasRt = true;
639 } else if (Rs == Rt) {
640 MI.setOpcode(Mips::BLTZALC);
641 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000642 } else {
643 MI.setOpcode(Mips::BLTUC);
644 HasRs = true;
645 HasRt = true;
646 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000647
648 if (HasRs)
649 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
650 Rs)));
651
652 if (HasRt)
653 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
654 Rt)));
655
656 MI.addOperand(MCOperand::CreateImm(Imm));
657
658 return MCDisassembler::Success;
659}
660
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000661template <typename InsnType>
662static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
663 uint64_t Address,
664 const void *Decoder) {
665 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
666 // (otherwise we would have matched the BLEZL instruction from the earlier
667 // ISA's instead).
668 //
669 // We have:
670 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
671 // Invalid if rs == 0
672 // BLEZALC if rs == 0 && rt != 0
673 // BGEZALC if rs == rt && rt != 0
674 // BGEUC if rs != rt && rs != 0 && rt != 0
675
676 InsnType Rs = fieldFromInstruction(insn, 21, 5);
677 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000678 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000679 bool HasRs = false;
680
681 if (Rt == 0)
682 return MCDisassembler::Fail;
683 else if (Rs == 0)
684 MI.setOpcode(Mips::BLEZALC);
685 else if (Rs == Rt)
686 MI.setOpcode(Mips::BGEZALC);
687 else {
688 HasRs = true;
689 MI.setOpcode(Mips::BGEUC);
690 }
691
692 if (HasRs)
693 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
694 Rs)));
695 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
696 Rt)));
697
698 MI.addOperand(MCOperand::CreateImm(Imm));
699
700 return MCDisassembler::Success;
701}
702
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000703/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000704/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000705static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
706 uint64_t &Size, uint32_t &Insn,
707 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000708 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000709 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000710 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000711 return MCDisassembler::Fail;
712 }
713
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000714 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000715 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000716 Insn =
717 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
718 } else {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000719 // Encoded as a small-endian 32-bit word in the stream.
Vladimir Medicdde3d582013-09-06 12:30:36 +0000720 // Little-endian byte ordering:
721 // mips32r2: 4 | 3 | 2 | 1
722 // microMIPS: 2 | 1 | 4 | 3
723 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000724 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000725 (Bytes[1] << 24);
726 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000727 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000728 (Bytes[3] << 24);
729 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000730 }
731
732 return MCDisassembler::Success;
733}
734
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000735DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000736 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000737 uint64_t Address,
738 raw_ostream &VStream,
739 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000740 uint32_t Insn;
741
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000742 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000743 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, IsMicroMips);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000744 if (Result == MCDisassembler::Fail)
745 return MCDisassembler::Fail;
746
Vladimir Medicdde3d582013-09-06 12:30:36 +0000747 if (IsMicroMips) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000748 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000749 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000750 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000751 this, STI);
752 if (Result != MCDisassembler::Fail) {
753 Size = 4;
754 return Result;
755 }
756 return MCDisassembler::Fail;
757 }
758
Daniel Sandersc171f652014-06-13 13:15:59 +0000759 if (hasCOP3()) {
760 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
761 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000762 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000763 if (Result != MCDisassembler::Fail) {
764 Size = 4;
765 return Result;
766 }
767 }
768
769 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000770 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000771 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000772 Address, this, STI);
773 if (Result != MCDisassembler::Fail) {
774 Size = 4;
775 return Result;
776 }
777 }
778
Daniel Sandersc171f652014-06-13 13:15:59 +0000779 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000780 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000781 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000782 Address, this, STI);
783 if (Result != MCDisassembler::Fail) {
784 Size = 4;
785 return Result;
786 }
787 }
788
Daniel Sanders0fa60412014-06-12 13:39:06 +0000789 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000790 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000791 Result =
792 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000793 if (Result != MCDisassembler::Fail) {
794 Size = 4;
795 return Result;
796 }
797
798 return MCDisassembler::Fail;
799}
800
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000801DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000802 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000803 uint64_t Address,
804 raw_ostream &VStream,
805 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000806 uint32_t Insn;
807
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000808 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000809 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000810 if (Result == MCDisassembler::Fail)
811 return MCDisassembler::Fail;
812
813 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000814 Result =
815 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000816 if (Result != MCDisassembler::Fail) {
817 Size = 4;
818 return Result;
819 }
820 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000821 Result =
822 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000823 if (Result != MCDisassembler::Fail) {
824 Size = 4;
825 return Result;
826 }
827
828 return MCDisassembler::Fail;
829}
830
Reed Kotlerec8a5492013-02-14 03:05:25 +0000831static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
832 unsigned RegNo,
833 uint64_t Address,
834 const void *Decoder) {
835
836 return MCDisassembler::Fail;
837
838}
839
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000840static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
841 unsigned RegNo,
842 uint64_t Address,
843 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000844
845 if (RegNo > 31)
846 return MCDisassembler::Fail;
847
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000848 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000849 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000850 return MCDisassembler::Success;
851}
852
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000853static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
854 unsigned RegNo,
855 uint64_t Address,
856 const void *Decoder) {
857 return MCDisassembler::Fail;
858}
859
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000860static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
861 unsigned RegNo,
862 uint64_t Address,
863 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000864 if (RegNo > 31)
865 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000866 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000867 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000868 return MCDisassembler::Success;
869}
870
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000871static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
872 unsigned RegNo,
873 uint64_t Address,
874 const void *Decoder) {
875 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
876 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
877
878 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
879}
880
Akira Hatanaka654655f2013-08-14 00:53:38 +0000881static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
882 unsigned RegNo,
883 uint64_t Address,
884 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000885 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000886}
887
Akira Hatanaka71928e62012-04-17 18:03:21 +0000888static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
889 unsigned RegNo,
890 uint64_t Address,
891 const void *Decoder) {
892 if (RegNo > 31)
893 return MCDisassembler::Fail;
894
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000895 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
896 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000897 return MCDisassembler::Success;
898}
899
900static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
901 unsigned RegNo,
902 uint64_t Address,
903 const void *Decoder) {
904 if (RegNo > 31)
905 return MCDisassembler::Fail;
906
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000907 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
908 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000909 return MCDisassembler::Success;
910}
911
912static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
913 unsigned RegNo,
914 uint64_t Address,
915 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +0000916 if (RegNo > 31)
917 return MCDisassembler::Fail;
918 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
919 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000920 return MCDisassembler::Success;
921}
922
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000923static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
924 unsigned RegNo,
925 uint64_t Address,
926 const void *Decoder) {
927 if (RegNo > 7)
928 return MCDisassembler::Fail;
929 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
930 Inst.addOperand(MCOperand::CreateReg(Reg));
931 return MCDisassembler::Success;
932}
933
Daniel Sanders0fa60412014-06-12 13:39:06 +0000934static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
935 uint64_t Address,
936 const void *Decoder) {
937 if (RegNo > 31)
938 return MCDisassembler::Fail;
939
940 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
941 Inst.addOperand(MCOperand::CreateReg(Reg));
942 return MCDisassembler::Success;
943}
944
Akira Hatanaka71928e62012-04-17 18:03:21 +0000945static DecodeStatus DecodeMem(MCInst &Inst,
946 unsigned Insn,
947 uint64_t Address,
948 const void *Decoder) {
949 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +0000950 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
951 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000952
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000953 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
954 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000955
956 if(Inst.getOpcode() == Mips::SC){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000957 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000958 }
959
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000960 Inst.addOperand(MCOperand::CreateReg(Reg));
961 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000962 Inst.addOperand(MCOperand::CreateImm(Offset));
963
964 return MCDisassembler::Success;
965}
966
Daniel Sanders92db6b72014-10-01 08:26:55 +0000967static DecodeStatus DecodeCacheOp(MCInst &Inst,
968 unsigned Insn,
969 uint64_t Address,
970 const void *Decoder) {
971 int Offset = SignExtend32<16>(Insn & 0xffff);
972 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
973 unsigned Base = fieldFromInstruction(Insn, 21, 5);
974
975 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
976
977 Inst.addOperand(MCOperand::CreateReg(Base));
978 Inst.addOperand(MCOperand::CreateImm(Offset));
979 Inst.addOperand(MCOperand::CreateImm(Hint));
980
981 return MCDisassembler::Success;
982}
983
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000984static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
985 uint64_t Address, const void *Decoder) {
986 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
987 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
988 unsigned Base = fieldFromInstruction(Insn, 11, 5);
989
990 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
991 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
992
993 Inst.addOperand(MCOperand::CreateReg(Reg));
994 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +0000995
996 // The immediate field of an LD/ST instruction is scaled which means it must
997 // be multiplied (when decoding) by the size (in bytes) of the instructions'
998 // data format.
999 // .b - 1 byte
1000 // .h - 2 bytes
1001 // .w - 4 bytes
1002 // .d - 8 bytes
1003 switch(Inst.getOpcode())
1004 {
1005 default:
1006 assert (0 && "Unexpected instruction");
1007 return MCDisassembler::Fail;
1008 break;
1009 case Mips::LD_B:
1010 case Mips::ST_B:
1011 Inst.addOperand(MCOperand::CreateImm(Offset));
1012 break;
1013 case Mips::LD_H:
1014 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001015 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001016 break;
1017 case Mips::LD_W:
1018 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001019 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001020 break;
1021 case Mips::LD_D:
1022 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001023 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001024 break;
1025 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001026
1027 return MCDisassembler::Success;
1028}
1029
Vladimir Medicdde3d582013-09-06 12:30:36 +00001030static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1031 unsigned Insn,
1032 uint64_t Address,
1033 const void *Decoder) {
1034 int Offset = SignExtend32<12>(Insn & 0x0fff);
1035 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1036 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1037
1038 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1039 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1040
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001041 switch (Inst.getOpcode()) {
1042 case Mips::SWM32_MM:
1043 case Mips::LWM32_MM:
1044 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1045 == MCDisassembler::Fail)
1046 return MCDisassembler::Fail;
1047 Inst.addOperand(MCOperand::CreateReg(Base));
1048 Inst.addOperand(MCOperand::CreateImm(Offset));
1049 break;
1050 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001051 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001052 // fallthrough
1053 default:
1054 Inst.addOperand(MCOperand::CreateReg(Reg));
1055 Inst.addOperand(MCOperand::CreateReg(Base));
1056 Inst.addOperand(MCOperand::CreateImm(Offset));
1057 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001058
1059 return MCDisassembler::Success;
1060}
1061
1062static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1063 unsigned Insn,
1064 uint64_t Address,
1065 const void *Decoder) {
1066 int Offset = SignExtend32<16>(Insn & 0xffff);
1067 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1068 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1069
1070 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1071 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1072
1073 Inst.addOperand(MCOperand::CreateReg(Reg));
1074 Inst.addOperand(MCOperand::CreateReg(Base));
1075 Inst.addOperand(MCOperand::CreateImm(Offset));
1076
1077 return MCDisassembler::Success;
1078}
1079
Akira Hatanaka71928e62012-04-17 18:03:21 +00001080static DecodeStatus DecodeFMem(MCInst &Inst,
1081 unsigned Insn,
1082 uint64_t Address,
1083 const void *Decoder) {
1084 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001085 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1086 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001087
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001088 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001089 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001090
1091 Inst.addOperand(MCOperand::CreateReg(Reg));
1092 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001093 Inst.addOperand(MCOperand::CreateImm(Offset));
1094
1095 return MCDisassembler::Success;
1096}
1097
Daniel Sanders92db6b72014-10-01 08:26:55 +00001098static DecodeStatus DecodeFMem2(MCInst &Inst,
1099 unsigned Insn,
1100 uint64_t Address,
1101 const void *Decoder) {
1102 int Offset = SignExtend32<16>(Insn & 0xffff);
1103 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1104 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1105
1106 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1107 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1108
1109 Inst.addOperand(MCOperand::CreateReg(Reg));
1110 Inst.addOperand(MCOperand::CreateReg(Base));
1111 Inst.addOperand(MCOperand::CreateImm(Offset));
1112
1113 return MCDisassembler::Success;
1114}
1115
1116static DecodeStatus DecodeFMem3(MCInst &Inst,
1117 unsigned Insn,
1118 uint64_t Address,
1119 const void *Decoder) {
1120 int Offset = SignExtend32<16>(Insn & 0xffff);
1121 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1122 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1123
1124 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1125 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1126
1127 Inst.addOperand(MCOperand::CreateReg(Reg));
1128 Inst.addOperand(MCOperand::CreateReg(Base));
1129 Inst.addOperand(MCOperand::CreateImm(Offset));
1130
1131 return MCDisassembler::Success;
1132}
1133
Daniel Sanders6a803f62014-06-16 13:13:03 +00001134static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1135 unsigned Insn,
1136 uint64_t Address,
1137 const void *Decoder) {
1138 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1139 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1140 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1141
1142 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1143 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1144
1145 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1146 Inst.addOperand(MCOperand::CreateReg(Rt));
1147 }
1148
1149 Inst.addOperand(MCOperand::CreateReg(Rt));
1150 Inst.addOperand(MCOperand::CreateReg(Base));
1151 Inst.addOperand(MCOperand::CreateImm(Offset));
1152
1153 return MCDisassembler::Success;
1154}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001155
1156static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1157 unsigned RegNo,
1158 uint64_t Address,
1159 const void *Decoder) {
1160 // Currently only hardware register 29 is supported.
1161 if (RegNo != 29)
1162 return MCDisassembler::Fail;
1163 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1164 return MCDisassembler::Success;
1165}
1166
Akira Hatanaka71928e62012-04-17 18:03:21 +00001167static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1168 unsigned RegNo,
1169 uint64_t Address,
1170 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001171 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001172 return MCDisassembler::Fail;
1173
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001174 ;
1175 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1176 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001177 return MCDisassembler::Success;
1178}
1179
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001180static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1181 unsigned RegNo,
1182 uint64_t Address,
1183 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001184 if (RegNo >= 4)
1185 return MCDisassembler::Fail;
1186
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001187 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001188 Inst.addOperand(MCOperand::CreateReg(Reg));
1189 return MCDisassembler::Success;
1190}
1191
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001192static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1193 unsigned RegNo,
1194 uint64_t Address,
1195 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001196 if (RegNo >= 4)
1197 return MCDisassembler::Fail;
1198
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001199 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001200 Inst.addOperand(MCOperand::CreateReg(Reg));
1201 return MCDisassembler::Success;
1202}
1203
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001204static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1205 unsigned RegNo,
1206 uint64_t Address,
1207 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001208 if (RegNo >= 4)
1209 return MCDisassembler::Fail;
1210
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001211 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001212 Inst.addOperand(MCOperand::CreateReg(Reg));
1213 return MCDisassembler::Success;
1214}
1215
Jack Carter3eb663b2013-09-26 00:09:46 +00001216static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1217 unsigned RegNo,
1218 uint64_t Address,
1219 const void *Decoder) {
1220 if (RegNo > 31)
1221 return MCDisassembler::Fail;
1222
1223 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1224 Inst.addOperand(MCOperand::CreateReg(Reg));
1225 return MCDisassembler::Success;
1226}
1227
Jack Carter5dc8ac92013-09-25 23:50:44 +00001228static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1229 unsigned RegNo,
1230 uint64_t Address,
1231 const void *Decoder) {
1232 if (RegNo > 31)
1233 return MCDisassembler::Fail;
1234
1235 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1236 Inst.addOperand(MCOperand::CreateReg(Reg));
1237 return MCDisassembler::Success;
1238}
1239
1240static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1241 unsigned RegNo,
1242 uint64_t Address,
1243 const void *Decoder) {
1244 if (RegNo > 31)
1245 return MCDisassembler::Fail;
1246
1247 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1248 Inst.addOperand(MCOperand::CreateReg(Reg));
1249 return MCDisassembler::Success;
1250}
1251
1252static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1253 unsigned RegNo,
1254 uint64_t Address,
1255 const void *Decoder) {
1256 if (RegNo > 31)
1257 return MCDisassembler::Fail;
1258
1259 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1260 Inst.addOperand(MCOperand::CreateReg(Reg));
1261 return MCDisassembler::Success;
1262}
1263
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001264static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1265 unsigned RegNo,
1266 uint64_t Address,
1267 const void *Decoder) {
1268 if (RegNo > 7)
1269 return MCDisassembler::Fail;
1270
1271 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1272 Inst.addOperand(MCOperand::CreateReg(Reg));
1273 return MCDisassembler::Success;
1274}
1275
Daniel Sanders2a83d682014-05-21 12:56:39 +00001276static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1277 unsigned RegNo,
1278 uint64_t Address,
1279 const void *Decoder) {
1280 if (RegNo > 31)
1281 return MCDisassembler::Fail;
1282
1283 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1284 Inst.addOperand(MCOperand::CreateReg(Reg));
1285 return MCDisassembler::Success;
1286}
1287
Akira Hatanaka71928e62012-04-17 18:03:21 +00001288static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1289 unsigned Offset,
1290 uint64_t Address,
1291 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001292 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001293 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1294 return MCDisassembler::Success;
1295}
1296
Akira Hatanaka71928e62012-04-17 18:03:21 +00001297static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1298 unsigned Insn,
1299 uint64_t Address,
1300 const void *Decoder) {
1301
Jim Grosbachecaef492012-08-14 19:06:05 +00001302 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001303 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1304 return MCDisassembler::Success;
1305}
1306
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001307static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1308 unsigned Offset,
1309 uint64_t Address,
1310 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001311 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001312
1313 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1314 return MCDisassembler::Success;
1315}
1316
1317static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1318 unsigned Offset,
1319 uint64_t Address,
1320 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001321 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001322
1323 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1324 return MCDisassembler::Success;
1325}
1326
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001327static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1328 unsigned Offset,
1329 uint64_t Address,
1330 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001331 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001332 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1333 return MCDisassembler::Success;
1334}
1335
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001336static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1337 unsigned Insn,
1338 uint64_t Address,
1339 const void *Decoder) {
1340 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1341 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1342 return MCDisassembler::Success;
1343}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001344
1345static DecodeStatus DecodeSimm16(MCInst &Inst,
1346 unsigned Insn,
1347 uint64_t Address,
1348 const void *Decoder) {
1349 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1350 return MCDisassembler::Success;
1351}
1352
Matheus Almeida779c5932013-11-18 12:32:49 +00001353static DecodeStatus DecodeLSAImm(MCInst &Inst,
1354 unsigned Insn,
1355 uint64_t Address,
1356 const void *Decoder) {
1357 // We add one to the immediate field as it was encoded as 'imm - 1'.
1358 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1359 return MCDisassembler::Success;
1360}
1361
Akira Hatanaka71928e62012-04-17 18:03:21 +00001362static DecodeStatus DecodeInsSize(MCInst &Inst,
1363 unsigned Insn,
1364 uint64_t Address,
1365 const void *Decoder) {
1366 // First we need to grab the pos(lsb) from MCInst.
1367 int Pos = Inst.getOperand(2).getImm();
1368 int Size = (int) Insn - Pos + 1;
1369 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1370 return MCDisassembler::Success;
1371}
1372
1373static DecodeStatus DecodeExtSize(MCInst &Inst,
1374 unsigned Insn,
1375 uint64_t Address,
1376 const void *Decoder) {
1377 int Size = (int) Insn + 1;
1378 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1379 return MCDisassembler::Success;
1380}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001381
1382static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1383 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001384 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001385 return MCDisassembler::Success;
1386}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001387
1388static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1389 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001390 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001391 return MCDisassembler::Success;
1392}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001393
1394static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1395 unsigned Insn,
1396 uint64_t Address,
1397 const void *Decoder) {
1398 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1399 Mips::S6, Mips::FP};
1400 unsigned RegNum;
1401
1402 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1403 // Empty register lists are not allowed.
1404 if (RegLst == 0)
1405 return MCDisassembler::Fail;
1406
1407 RegNum = RegLst & 0xf;
1408 for (unsigned i = 0; i < RegNum; i++)
1409 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1410
1411 if (RegLst & 0x10)
1412 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1413
1414 return MCDisassembler::Success;
1415}