blob: 908166f0a7b3fdbef589e2477957dc256e4d1ca3 [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/MemoryObject.h"
24#include "llvm/Support/TargetRegistry.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000025
Akira Hatanaka71928e62012-04-17 18:03:21 +000026using namespace llvm;
27
Chandler Carruthe96dd892014-04-21 22:55:11 +000028#define DEBUG_TYPE "mips-disassembler"
29
Akira Hatanaka71928e62012-04-17 18:03:21 +000030typedef MCDisassembler::DecodeStatus DecodeStatus;
31
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000032namespace {
33
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000034/// MipsDisassemblerBase - a disasembler class for Mips.
35class MipsDisassemblerBase : public MCDisassembler {
Akira Hatanaka71928e62012-04-17 18:03:21 +000036public:
37 /// Constructor - Initializes the disassembler.
38 ///
Lang Hamesa1bc0f52014-04-15 04:40:56 +000039 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000040 bool bigEndian) :
Lang Hamesa1bc0f52014-04-15 04:40:56 +000041 MCDisassembler(STI, Ctx),
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000042 IsN64(STI.getFeatureBits() & Mips::FeatureN64), isBigEndian(bigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000043
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000044 virtual ~MipsDisassemblerBase() {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000045
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000046 bool isN64() const { return IsN64; }
47
Akira Hatanaka71928e62012-04-17 18:03:21 +000048private:
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000049 bool IsN64;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000050protected:
Akira Hatanaka71928e62012-04-17 18:03:21 +000051 bool isBigEndian;
52};
53
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000054/// MipsDisassembler - a disasembler class for Mips32.
55class MipsDisassembler : public MipsDisassemblerBase {
Vladimir Medicdde3d582013-09-06 12:30:36 +000056 bool IsMicroMips;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000057public:
58 /// Constructor - Initializes the disassembler.
59 ///
Daniel Sandersc171f652014-06-13 13:15:59 +000060 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
61 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
62 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
63 }
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000064
Daniel Sandersc171f652014-06-13 13:15:59 +000065 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
66 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
67 bool hasMips32r6() const {
Daniel Sanders5c582b22014-05-22 11:23:21 +000068 return STI.getFeatureBits() & Mips::FeatureMips32r6;
69 }
70
Daniel Sanders0fa60412014-06-12 13:39:06 +000071 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
72
Daniel Sandersc171f652014-06-13 13:15:59 +000073 bool hasCOP3() const {
74 // Only present in MIPS-I and MIPS-II
75 return !hasMips32() && !hasMips3();
76 }
77
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000078 /// getInstruction - See MCDisassembler.
Craig Topper56c590a2014-04-29 07:58:02 +000079 DecodeStatus getInstruction(MCInst &instr,
80 uint64_t &size,
81 const MemoryObject &region,
82 uint64_t address,
83 raw_ostream &vStream,
84 raw_ostream &cStream) const override;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000085};
86
Akira Hatanaka71928e62012-04-17 18:03:21 +000087
88/// Mips64Disassembler - a disasembler class for Mips64.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000089class Mips64Disassembler : public MipsDisassemblerBase {
Akira Hatanaka71928e62012-04-17 18:03:21 +000090public:
91 /// Constructor - Initializes the disassembler.
92 ///
Lang Hamesa1bc0f52014-04-15 04:40:56 +000093 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000094 bool bigEndian) :
Lang Hamesa1bc0f52014-04-15 04:40:56 +000095 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000096
97 /// getInstruction - See MCDisassembler.
Craig Topper56c590a2014-04-29 07:58:02 +000098 DecodeStatus getInstruction(MCInst &instr,
99 uint64_t &size,
100 const MemoryObject &region,
101 uint64_t address,
102 raw_ostream &vStream,
103 raw_ostream &cStream) const override;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000104};
105
Benjamin Kramercb3e98c2012-05-01 14:34:24 +0000106} // end anonymous namespace
107
Akira Hatanaka71928e62012-04-17 18:03:21 +0000108// Forward declare these because the autogenerated code will reference them.
109// Definitions are further down.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000110static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
111 unsigned RegNo,
112 uint64_t Address,
113 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000114
Reed Kotlerec8a5492013-02-14 03:05:25 +0000115static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
116 unsigned RegNo,
117 uint64_t Address,
118 const void *Decoder);
119
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000120static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
121 unsigned RegNo,
122 uint64_t Address,
123 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000124
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000125static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
126 unsigned Insn,
127 uint64_t Address,
128 const void *Decoder);
129
Akira Hatanaka654655f2013-08-14 00:53:38 +0000130static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
131 unsigned RegNo,
132 uint64_t Address,
133 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000134
Akira Hatanaka71928e62012-04-17 18:03:21 +0000135static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
136 unsigned RegNo,
137 uint64_t Address,
138 const void *Decoder);
139
140static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
141 unsigned RegNo,
142 uint64_t Address,
143 const void *Decoder);
144
Akira Hatanaka14e31a22013-08-20 22:58:56 +0000145static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
146 unsigned RegNo,
147 uint64_t Address,
148 const void *Decoder);
149
Akira Hatanaka71928e62012-04-17 18:03:21 +0000150static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
151 unsigned RegNo,
152 uint64_t Address,
153 const void *Decoder);
154
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000155static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
156 unsigned RegNo,
157 uint64_t Address,
158 const void *Decoder);
159
Daniel Sanders0fa60412014-06-12 13:39:06 +0000160static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
161 uint64_t Address,
162 const void *Decoder);
163
Akira Hatanaka71928e62012-04-17 18:03:21 +0000164static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
165 unsigned Insn,
166 uint64_t Address,
167 const void *Decoder);
168
169static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
170 unsigned RegNo,
171 uint64_t Address,
172 const void *Decoder);
173
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000174static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
175 unsigned RegNo,
176 uint64_t Address,
177 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000178
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000179static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
180 unsigned RegNo,
181 uint64_t Address,
182 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000183
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000184static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
185 unsigned RegNo,
186 uint64_t Address,
187 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000188
Jack Carter3eb663b2013-09-26 00:09:46 +0000189static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
190 unsigned RegNo,
191 uint64_t Address,
192 const void *Decoder);
193
Jack Carter5dc8ac92013-09-25 23:50:44 +0000194static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
195 unsigned RegNo,
196 uint64_t Address,
197 const void *Decoder);
198
199static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
200 unsigned RegNo,
201 uint64_t Address,
202 const void *Decoder);
203
204static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
205 unsigned RegNo,
206 uint64_t Address,
207 const void *Decoder);
208
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000209static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
210 unsigned RegNo,
211 uint64_t Address,
212 const void *Decoder);
213
Daniel Sanders2a83d682014-05-21 12:56:39 +0000214static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
215 unsigned RegNo,
216 uint64_t Address,
217 const void *Decoder);
218
Akira Hatanaka71928e62012-04-17 18:03:21 +0000219static DecodeStatus DecodeBranchTarget(MCInst &Inst,
220 unsigned Offset,
221 uint64_t Address,
222 const void *Decoder);
223
Akira Hatanaka71928e62012-04-17 18:03:21 +0000224static DecodeStatus DecodeJumpTarget(MCInst &Inst,
225 unsigned Insn,
226 uint64_t Address,
227 const void *Decoder);
228
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000229static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
230 unsigned Offset,
231 uint64_t Address,
232 const void *Decoder);
233
234static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
235 unsigned Offset,
236 uint64_t Address,
237 const void *Decoder);
238
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000239// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
240// shifted left by 1 bit.
241static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
242 unsigned Offset,
243 uint64_t Address,
244 const void *Decoder);
245
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000246// DecodeJumpTargetMM - Decode microMIPS jump target, which is
247// shifted left by 1 bit.
248static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
249 unsigned Insn,
250 uint64_t Address,
251 const void *Decoder);
252
Akira Hatanaka71928e62012-04-17 18:03:21 +0000253static DecodeStatus DecodeMem(MCInst &Inst,
254 unsigned Insn,
255 uint64_t Address,
256 const void *Decoder);
257
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000258static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
259 uint64_t Address, const void *Decoder);
260
Vladimir Medicdde3d582013-09-06 12:30:36 +0000261static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
262 unsigned Insn,
263 uint64_t Address,
264 const void *Decoder);
265
266static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
267 unsigned Insn,
268 uint64_t Address,
269 const void *Decoder);
270
Akira Hatanaka71928e62012-04-17 18:03:21 +0000271static DecodeStatus DecodeFMem(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
Akira Hatanaka71928e62012-04-17 18:03:21 +0000344namespace llvm {
345extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
346 TheMips64elTarget;
347}
348
349static MCDisassembler *createMipsDisassembler(
350 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000351 const MCSubtargetInfo &STI,
352 MCContext &Ctx) {
353 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000354}
355
356static MCDisassembler *createMipselDisassembler(
357 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000358 const MCSubtargetInfo &STI,
359 MCContext &Ctx) {
360 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000361}
362
363static MCDisassembler *createMips64Disassembler(
364 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000365 const MCSubtargetInfo &STI,
366 MCContext &Ctx) {
367 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000368}
369
370static MCDisassembler *createMips64elDisassembler(
371 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000372 const MCSubtargetInfo &STI,
373 MCContext &Ctx) {
374 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000375}
376
377extern "C" void LLVMInitializeMipsDisassembler() {
378 // Register the disassembler.
379 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
380 createMipsDisassembler);
381 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
382 createMipselDisassembler);
383 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
384 createMips64Disassembler);
385 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
386 createMips64elDisassembler);
387}
388
Akira Hatanaka71928e62012-04-17 18:03:21 +0000389#include "MipsGenDisassemblerTables.inc"
390
Daniel Sanders5c582b22014-05-22 11:23:21 +0000391static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
392 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
393 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
394 return *(RegInfo->getRegClass(RC).begin() + RegNo);
395}
396
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000397template <typename InsnType>
398static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
399 const void *Decoder) {
400 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
401 // The size of the n field depends on the element size
402 // The register class also depends on this.
403 InsnType tmp = fieldFromInstruction(insn, 17, 5);
404 unsigned NSize = 0;
405 DecodeFN RegDecoder = nullptr;
406 if ((tmp & 0x18) == 0x00) { // INSVE_B
407 NSize = 4;
408 RegDecoder = DecodeMSA128BRegisterClass;
409 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
410 NSize = 3;
411 RegDecoder = DecodeMSA128HRegisterClass;
412 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
413 NSize = 2;
414 RegDecoder = DecodeMSA128WRegisterClass;
415 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
416 NSize = 1;
417 RegDecoder = DecodeMSA128DRegisterClass;
418 } else
419 llvm_unreachable("Invalid encoding");
420
421 assert(NSize != 0 && RegDecoder != nullptr);
422
423 // $wd
424 tmp = fieldFromInstruction(insn, 6, 5);
425 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
426 return MCDisassembler::Fail;
427 // $wd_in
428 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
429 return MCDisassembler::Fail;
430 // $n
431 tmp = fieldFromInstruction(insn, 16, NSize);
432 MI.addOperand(MCOperand::CreateImm(tmp));
433 // $ws
434 tmp = fieldFromInstruction(insn, 11, 5);
435 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
436 return MCDisassembler::Fail;
437 // $n2
438 MI.addOperand(MCOperand::CreateImm(0));
439
440 return MCDisassembler::Success;
441}
442
Daniel Sanders5c582b22014-05-22 11:23:21 +0000443template <typename InsnType>
444static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
445 uint64_t Address,
446 const void *Decoder) {
447 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
448 // (otherwise we would have matched the ADDI instruction from the earlier
449 // ISA's instead).
450 //
451 // We have:
452 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
453 // BOVC if rs >= rt
454 // BEQZALC if rs == 0 && rt != 0
455 // BEQC if rs < rt && rs != 0
456
457 InsnType Rs = fieldFromInstruction(insn, 21, 5);
458 InsnType Rt = fieldFromInstruction(insn, 16, 5);
459 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
460 bool HasRs = false;
461
462 if (Rs >= Rt) {
463 MI.setOpcode(Mips::BOVC);
464 HasRs = true;
465 } else if (Rs != 0 && Rs < Rt) {
466 MI.setOpcode(Mips::BEQC);
467 HasRs = true;
468 } else
469 MI.setOpcode(Mips::BEQZALC);
470
471 if (HasRs)
472 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
473 Rs)));
474
475 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
476 Rt)));
477 MI.addOperand(MCOperand::CreateImm(Imm));
478
479 return MCDisassembler::Success;
480}
481
482template <typename InsnType>
483static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
484 uint64_t Address,
485 const void *Decoder) {
486 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
487 // (otherwise we would have matched the ADDI instruction from the earlier
488 // ISA's instead).
489 //
490 // We have:
491 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
492 // BNVC if rs >= rt
493 // BNEZALC if rs == 0 && rt != 0
494 // BNEC if rs < rt && rs != 0
495
496 InsnType Rs = fieldFromInstruction(insn, 21, 5);
497 InsnType Rt = fieldFromInstruction(insn, 16, 5);
498 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
499 bool HasRs = false;
500
501 if (Rs >= Rt) {
502 MI.setOpcode(Mips::BNVC);
503 HasRs = true;
504 } else if (Rs != 0 && Rs < Rt) {
505 MI.setOpcode(Mips::BNEC);
506 HasRs = true;
507 } else
508 MI.setOpcode(Mips::BNEZALC);
509
510 if (HasRs)
511 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
512 Rs)));
513
514 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
515 Rt)));
516 MI.addOperand(MCOperand::CreateImm(Imm));
517
518 return MCDisassembler::Success;
519}
520
521template <typename InsnType>
522static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
523 uint64_t Address,
524 const void *Decoder) {
525 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
526 // (otherwise we would have matched the BLEZL instruction from the earlier
527 // ISA's instead).
528 //
529 // We have:
530 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
531 // Invalid if rs == 0
532 // BLEZC if rs == 0 && rt != 0
533 // BGEZC if rs == rt && rt != 0
534 // BGEC if rs != rt && rs != 0 && rt != 0
535
536 InsnType Rs = fieldFromInstruction(insn, 21, 5);
537 InsnType Rt = fieldFromInstruction(insn, 16, 5);
538 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000539 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000540
541 if (Rt == 0)
542 return MCDisassembler::Fail;
543 else if (Rs == 0)
544 MI.setOpcode(Mips::BLEZC);
545 else if (Rs == Rt)
546 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000547 else {
548 HasRs = true;
549 MI.setOpcode(Mips::BGEC);
550 }
551
552 if (HasRs)
553 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
554 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000555
556 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
557 Rt)));
558
559 MI.addOperand(MCOperand::CreateImm(Imm));
560
561 return MCDisassembler::Success;
562}
563
564template <typename InsnType>
565static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
566 uint64_t Address,
567 const void *Decoder) {
568 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
569 // (otherwise we would have matched the BGTZL instruction from the earlier
570 // ISA's instead).
571 //
572 // We have:
573 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
574 // Invalid if rs == 0
575 // BGTZC if rs == 0 && rt != 0
576 // BLTZC if rs == rt && rt != 0
577 // BLTC if rs != rt && rs != 0 && rt != 0
578
579 InsnType Rs = fieldFromInstruction(insn, 21, 5);
580 InsnType Rt = fieldFromInstruction(insn, 16, 5);
581 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
582
583 if (Rt == 0)
584 return MCDisassembler::Fail;
585 else if (Rs == 0)
586 MI.setOpcode(Mips::BGTZC);
587 else if (Rs == Rt)
588 MI.setOpcode(Mips::BLTZC);
589 else
590 return MCDisassembler::Fail; // FIXME: BLTC is not implemented yet.
591
592 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
593 Rt)));
594
595 MI.addOperand(MCOperand::CreateImm(Imm));
596
597 return MCDisassembler::Success;
598}
599
600template <typename InsnType>
601static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
602 uint64_t Address,
603 const void *Decoder) {
604 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
605 // (otherwise we would have matched the BGTZ instruction from the earlier
606 // ISA's instead).
607 //
608 // We have:
609 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
610 // BGTZ if rt == 0
611 // BGTZALC if rs == 0 && rt != 0
612 // BLTZALC if rs != 0 && rs == rt
613 // BLTUC if rs != 0 && rs != rt
614
615 InsnType Rs = fieldFromInstruction(insn, 21, 5);
616 InsnType Rt = fieldFromInstruction(insn, 16, 5);
617 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
618 bool HasRs = false;
619 bool HasRt = false;
620
621 if (Rt == 0) {
622 MI.setOpcode(Mips::BGTZ);
623 HasRs = true;
624 } else if (Rs == 0) {
625 MI.setOpcode(Mips::BGTZALC);
626 HasRt = true;
627 } else if (Rs == Rt) {
628 MI.setOpcode(Mips::BLTZALC);
629 HasRs = true;
630 } else
631 return MCDisassembler::Fail; // BLTUC not implemented yet
632
633 if (HasRs)
634 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
635 Rs)));
636
637 if (HasRt)
638 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
639 Rt)));
640
641 MI.addOperand(MCOperand::CreateImm(Imm));
642
643 return MCDisassembler::Success;
644}
645
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000646template <typename InsnType>
647static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
648 uint64_t Address,
649 const void *Decoder) {
650 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
651 // (otherwise we would have matched the BLEZL instruction from the earlier
652 // ISA's instead).
653 //
654 // We have:
655 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
656 // Invalid if rs == 0
657 // BLEZALC if rs == 0 && rt != 0
658 // BGEZALC if rs == rt && rt != 0
659 // BGEUC if rs != rt && rs != 0 && rt != 0
660
661 InsnType Rs = fieldFromInstruction(insn, 21, 5);
662 InsnType Rt = fieldFromInstruction(insn, 16, 5);
663 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) << 2;
664 bool HasRs = false;
665
666 if (Rt == 0)
667 return MCDisassembler::Fail;
668 else if (Rs == 0)
669 MI.setOpcode(Mips::BLEZALC);
670 else if (Rs == Rt)
671 MI.setOpcode(Mips::BGEZALC);
672 else {
673 HasRs = true;
674 MI.setOpcode(Mips::BGEUC);
675 }
676
677 if (HasRs)
678 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
679 Rs)));
680 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
681 Rt)));
682
683 MI.addOperand(MCOperand::CreateImm(Imm));
684
685 return MCDisassembler::Success;
686}
687
Akira Hatanaka71928e62012-04-17 18:03:21 +0000688 /// readInstruction - read four bytes from the MemoryObject
689 /// and return 32 bit word sorted according to the given endianess
690static DecodeStatus readInstruction32(const MemoryObject &region,
691 uint64_t address,
692 uint64_t &size,
693 uint32_t &insn,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000694 bool isBigEndian,
695 bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000696 uint8_t Bytes[4];
697
698 // We want to read exactly 4 Bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000699 if (region.readBytes(address, 4, Bytes) == -1) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000700 size = 0;
701 return MCDisassembler::Fail;
702 }
703
704 if (isBigEndian) {
705 // Encoded as a big-endian 32-bit word in the stream.
706 insn = (Bytes[3] << 0) |
707 (Bytes[2] << 8) |
708 (Bytes[1] << 16) |
709 (Bytes[0] << 24);
710 }
711 else {
712 // Encoded as a small-endian 32-bit word in the stream.
Vladimir Medicdde3d582013-09-06 12:30:36 +0000713 // Little-endian byte ordering:
714 // mips32r2: 4 | 3 | 2 | 1
715 // microMIPS: 2 | 1 | 4 | 3
716 if (IsMicroMips) {
717 insn = (Bytes[2] << 0) |
718 (Bytes[3] << 8) |
719 (Bytes[0] << 16) |
720 (Bytes[1] << 24);
721 } else {
722 insn = (Bytes[0] << 0) |
723 (Bytes[1] << 8) |
724 (Bytes[2] << 16) |
725 (Bytes[3] << 24);
726 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000727 }
728
729 return MCDisassembler::Success;
730}
731
732DecodeStatus
733MipsDisassembler::getInstruction(MCInst &instr,
734 uint64_t &Size,
735 const MemoryObject &Region,
736 uint64_t Address,
737 raw_ostream &vStream,
738 raw_ostream &cStream) const {
739 uint32_t Insn;
740
741 DecodeStatus Result = readInstruction32(Region, Address, Size,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000742 Insn, isBigEndian, IsMicroMips);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000743 if (Result == MCDisassembler::Fail)
744 return MCDisassembler::Fail;
745
Vladimir Medicdde3d582013-09-06 12:30:36 +0000746 if (IsMicroMips) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000747 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000748 // Calling the auto-generated decoder function.
749 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
750 this, STI);
751 if (Result != MCDisassembler::Fail) {
752 Size = 4;
753 return Result;
754 }
755 return MCDisassembler::Fail;
756 }
757
Daniel Sandersc171f652014-06-13 13:15:59 +0000758 if (hasCOP3()) {
759 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
760 Result =
761 decodeInstruction(DecoderTableCOP3_32, instr, Insn, Address, this, STI);
762 if (Result != MCDisassembler::Fail) {
763 Size = 4;
764 return Result;
765 }
766 }
767
768 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000769 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
770 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
771 Address, this, STI);
772 if (Result != MCDisassembler::Fail) {
773 Size = 4;
774 return Result;
775 }
776 }
777
Daniel Sandersc171f652014-06-13 13:15:59 +0000778 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000779 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Daniel Sanders5c582b22014-05-22 11:23:21 +0000780 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
781 Address, this, STI);
782 if (Result != MCDisassembler::Fail) {
783 Size = 4;
784 return Result;
785 }
786 }
787
Daniel Sanders0fa60412014-06-12 13:39:06 +0000788 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000789 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000790 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
791 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000792 if (Result != MCDisassembler::Fail) {
793 Size = 4;
794 return Result;
795 }
796
797 return MCDisassembler::Fail;
798}
799
800DecodeStatus
801Mips64Disassembler::getInstruction(MCInst &instr,
802 uint64_t &Size,
803 const MemoryObject &Region,
804 uint64_t Address,
805 raw_ostream &vStream,
806 raw_ostream &cStream) const {
807 uint32_t Insn;
808
809 DecodeStatus Result = readInstruction32(Region, Address, Size,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000810 Insn, isBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000811 if (Result == MCDisassembler::Fail)
812 return MCDisassembler::Fail;
813
814 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000815 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
816 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000817 if (Result != MCDisassembler::Fail) {
818 Size = 4;
819 return Result;
820 }
821 // If we fail to decode in Mips64 decoder space we can try in Mips32
Jim Grosbachecaef492012-08-14 19:06:05 +0000822 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
823 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000824 if (Result != MCDisassembler::Fail) {
825 Size = 4;
826 return Result;
827 }
828
829 return MCDisassembler::Fail;
830}
831
Reed Kotlerec8a5492013-02-14 03:05:25 +0000832static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
833 unsigned RegNo,
834 uint64_t Address,
835 const void *Decoder) {
836
837 return MCDisassembler::Fail;
838
839}
840
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000841static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
842 unsigned RegNo,
843 uint64_t Address,
844 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000845
846 if (RegNo > 31)
847 return MCDisassembler::Fail;
848
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000849 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000850 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000851 return MCDisassembler::Success;
852}
853
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000854static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
855 unsigned RegNo,
856 uint64_t Address,
857 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000858 if (RegNo > 31)
859 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000860 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000861 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000862 return MCDisassembler::Success;
863}
864
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000865static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
866 unsigned RegNo,
867 uint64_t Address,
868 const void *Decoder) {
869 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
870 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
871
872 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
873}
874
Akira Hatanaka654655f2013-08-14 00:53:38 +0000875static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
876 unsigned RegNo,
877 uint64_t Address,
878 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000879 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000880}
881
Akira Hatanaka71928e62012-04-17 18:03:21 +0000882static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
883 unsigned RegNo,
884 uint64_t Address,
885 const void *Decoder) {
886 if (RegNo > 31)
887 return MCDisassembler::Fail;
888
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000889 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
890 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000891 return MCDisassembler::Success;
892}
893
894static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
895 unsigned RegNo,
896 uint64_t Address,
897 const void *Decoder) {
898 if (RegNo > 31)
899 return MCDisassembler::Fail;
900
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000901 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
902 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000903 return MCDisassembler::Success;
904}
905
Akira Hatanaka14e31a22013-08-20 22:58:56 +0000906static DecodeStatus DecodeFGRH32RegisterClass(MCInst &Inst,
907 unsigned RegNo,
908 uint64_t Address,
909 const void *Decoder) {
910 if (RegNo > 31)
911 return MCDisassembler::Fail;
912
913 unsigned Reg = getReg(Decoder, Mips::FGRH32RegClassID, RegNo);
914 Inst.addOperand(MCOperand::CreateReg(Reg));
915 return MCDisassembler::Success;
916}
917
Akira Hatanaka71928e62012-04-17 18:03:21 +0000918static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
919 unsigned RegNo,
920 uint64_t Address,
921 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +0000922 if (RegNo > 31)
923 return MCDisassembler::Fail;
924 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
925 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000926 return MCDisassembler::Success;
927}
928
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000929static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
930 unsigned RegNo,
931 uint64_t Address,
932 const void *Decoder) {
933 if (RegNo > 7)
934 return MCDisassembler::Fail;
935 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
936 Inst.addOperand(MCOperand::CreateReg(Reg));
937 return MCDisassembler::Success;
938}
939
Daniel Sanders0fa60412014-06-12 13:39:06 +0000940static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
941 uint64_t Address,
942 const void *Decoder) {
943 if (RegNo > 31)
944 return MCDisassembler::Fail;
945
946 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
947 Inst.addOperand(MCOperand::CreateReg(Reg));
948 return MCDisassembler::Success;
949}
950
Akira Hatanaka71928e62012-04-17 18:03:21 +0000951static DecodeStatus DecodeMem(MCInst &Inst,
952 unsigned Insn,
953 uint64_t Address,
954 const void *Decoder) {
955 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +0000956 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
957 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000958
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000959 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
960 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000961
962 if(Inst.getOpcode() == Mips::SC){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000963 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000964 }
965
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000966 Inst.addOperand(MCOperand::CreateReg(Reg));
967 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000968 Inst.addOperand(MCOperand::CreateImm(Offset));
969
970 return MCDisassembler::Success;
971}
972
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000973static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
974 uint64_t Address, const void *Decoder) {
975 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
976 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
977 unsigned Base = fieldFromInstruction(Insn, 11, 5);
978
979 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
980 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
981
982 Inst.addOperand(MCOperand::CreateReg(Reg));
983 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +0000984
985 // The immediate field of an LD/ST instruction is scaled which means it must
986 // be multiplied (when decoding) by the size (in bytes) of the instructions'
987 // data format.
988 // .b - 1 byte
989 // .h - 2 bytes
990 // .w - 4 bytes
991 // .d - 8 bytes
992 switch(Inst.getOpcode())
993 {
994 default:
995 assert (0 && "Unexpected instruction");
996 return MCDisassembler::Fail;
997 break;
998 case Mips::LD_B:
999 case Mips::ST_B:
1000 Inst.addOperand(MCOperand::CreateImm(Offset));
1001 break;
1002 case Mips::LD_H:
1003 case Mips::ST_H:
1004 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1005 break;
1006 case Mips::LD_W:
1007 case Mips::ST_W:
1008 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1009 break;
1010 case Mips::LD_D:
1011 case Mips::ST_D:
1012 Inst.addOperand(MCOperand::CreateImm(Offset << 3));
1013 break;
1014 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001015
1016 return MCDisassembler::Success;
1017}
1018
Vladimir Medicdde3d582013-09-06 12:30:36 +00001019static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1020 unsigned Insn,
1021 uint64_t Address,
1022 const void *Decoder) {
1023 int Offset = SignExtend32<12>(Insn & 0x0fff);
1024 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1025 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1026
1027 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1028 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1029
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001030 if (Inst.getOpcode() == Mips::SC_MM)
1031 Inst.addOperand(MCOperand::CreateReg(Reg));
1032
Vladimir Medicdde3d582013-09-06 12:30:36 +00001033 Inst.addOperand(MCOperand::CreateReg(Reg));
1034 Inst.addOperand(MCOperand::CreateReg(Base));
1035 Inst.addOperand(MCOperand::CreateImm(Offset));
1036
1037 return MCDisassembler::Success;
1038}
1039
1040static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1041 unsigned Insn,
1042 uint64_t Address,
1043 const void *Decoder) {
1044 int Offset = SignExtend32<16>(Insn & 0xffff);
1045 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1046 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1047
1048 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1049 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1050
1051 Inst.addOperand(MCOperand::CreateReg(Reg));
1052 Inst.addOperand(MCOperand::CreateReg(Base));
1053 Inst.addOperand(MCOperand::CreateImm(Offset));
1054
1055 return MCDisassembler::Success;
1056}
1057
Akira Hatanaka71928e62012-04-17 18:03:21 +00001058static DecodeStatus DecodeFMem(MCInst &Inst,
1059 unsigned Insn,
1060 uint64_t Address,
1061 const void *Decoder) {
1062 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001063 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1064 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001065
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001066 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001067 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001068
1069 Inst.addOperand(MCOperand::CreateReg(Reg));
1070 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001071 Inst.addOperand(MCOperand::CreateImm(Offset));
1072
1073 return MCDisassembler::Success;
1074}
1075
Daniel Sanders6a803f62014-06-16 13:13:03 +00001076static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1077 unsigned Insn,
1078 uint64_t Address,
1079 const void *Decoder) {
1080 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1081 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1082 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1083
1084 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1085 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1086
1087 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1088 Inst.addOperand(MCOperand::CreateReg(Rt));
1089 }
1090
1091 Inst.addOperand(MCOperand::CreateReg(Rt));
1092 Inst.addOperand(MCOperand::CreateReg(Base));
1093 Inst.addOperand(MCOperand::CreateImm(Offset));
1094
1095 return MCDisassembler::Success;
1096}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001097
1098static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1099 unsigned RegNo,
1100 uint64_t Address,
1101 const void *Decoder) {
1102 // Currently only hardware register 29 is supported.
1103 if (RegNo != 29)
1104 return MCDisassembler::Fail;
1105 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1106 return MCDisassembler::Success;
1107}
1108
Akira Hatanaka71928e62012-04-17 18:03:21 +00001109static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1110 unsigned RegNo,
1111 uint64_t Address,
1112 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001113 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001114 return MCDisassembler::Fail;
1115
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001116 ;
1117 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1118 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001119 return MCDisassembler::Success;
1120}
1121
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001122static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1123 unsigned RegNo,
1124 uint64_t Address,
1125 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001126 if (RegNo >= 4)
1127 return MCDisassembler::Fail;
1128
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001129 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001130 Inst.addOperand(MCOperand::CreateReg(Reg));
1131 return MCDisassembler::Success;
1132}
1133
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001134static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1135 unsigned RegNo,
1136 uint64_t Address,
1137 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001138 if (RegNo >= 4)
1139 return MCDisassembler::Fail;
1140
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001141 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001142 Inst.addOperand(MCOperand::CreateReg(Reg));
1143 return MCDisassembler::Success;
1144}
1145
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001146static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1147 unsigned RegNo,
1148 uint64_t Address,
1149 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001150 if (RegNo >= 4)
1151 return MCDisassembler::Fail;
1152
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001153 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001154 Inst.addOperand(MCOperand::CreateReg(Reg));
1155 return MCDisassembler::Success;
1156}
1157
Jack Carter3eb663b2013-09-26 00:09:46 +00001158static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1159 unsigned RegNo,
1160 uint64_t Address,
1161 const void *Decoder) {
1162 if (RegNo > 31)
1163 return MCDisassembler::Fail;
1164
1165 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1166 Inst.addOperand(MCOperand::CreateReg(Reg));
1167 return MCDisassembler::Success;
1168}
1169
Jack Carter5dc8ac92013-09-25 23:50:44 +00001170static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1171 unsigned RegNo,
1172 uint64_t Address,
1173 const void *Decoder) {
1174 if (RegNo > 31)
1175 return MCDisassembler::Fail;
1176
1177 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1178 Inst.addOperand(MCOperand::CreateReg(Reg));
1179 return MCDisassembler::Success;
1180}
1181
1182static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1183 unsigned RegNo,
1184 uint64_t Address,
1185 const void *Decoder) {
1186 if (RegNo > 31)
1187 return MCDisassembler::Fail;
1188
1189 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1190 Inst.addOperand(MCOperand::CreateReg(Reg));
1191 return MCDisassembler::Success;
1192}
1193
1194static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1195 unsigned RegNo,
1196 uint64_t Address,
1197 const void *Decoder) {
1198 if (RegNo > 31)
1199 return MCDisassembler::Fail;
1200
1201 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1202 Inst.addOperand(MCOperand::CreateReg(Reg));
1203 return MCDisassembler::Success;
1204}
1205
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001206static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1207 unsigned RegNo,
1208 uint64_t Address,
1209 const void *Decoder) {
1210 if (RegNo > 7)
1211 return MCDisassembler::Fail;
1212
1213 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1214 Inst.addOperand(MCOperand::CreateReg(Reg));
1215 return MCDisassembler::Success;
1216}
1217
Daniel Sanders2a83d682014-05-21 12:56:39 +00001218static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1219 unsigned RegNo,
1220 uint64_t Address,
1221 const void *Decoder) {
1222 if (RegNo > 31)
1223 return MCDisassembler::Fail;
1224
1225 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1226 Inst.addOperand(MCOperand::CreateReg(Reg));
1227 return MCDisassembler::Success;
1228}
1229
Akira Hatanaka71928e62012-04-17 18:03:21 +00001230static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1231 unsigned Offset,
1232 uint64_t Address,
1233 const void *Decoder) {
Daniel Sanders5c582b22014-05-22 11:23:21 +00001234 int32_t BranchOffset = (SignExtend32<16>(Offset) << 2) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001235 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1236 return MCDisassembler::Success;
1237}
1238
Akira Hatanaka71928e62012-04-17 18:03:21 +00001239static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1240 unsigned Insn,
1241 uint64_t Address,
1242 const void *Decoder) {
1243
Jim Grosbachecaef492012-08-14 19:06:05 +00001244 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001245 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1246 return MCDisassembler::Success;
1247}
1248
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001249static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1250 unsigned Offset,
1251 uint64_t Address,
1252 const void *Decoder) {
1253 int32_t BranchOffset = SignExtend32<21>(Offset) << 2;
1254
1255 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1256 return MCDisassembler::Success;
1257}
1258
1259static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1260 unsigned Offset,
1261 uint64_t Address,
1262 const void *Decoder) {
1263 int32_t BranchOffset = SignExtend32<26>(Offset) << 2;
1264
1265 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1266 return MCDisassembler::Success;
1267}
1268
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001269static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1270 unsigned Offset,
1271 uint64_t Address,
1272 const void *Decoder) {
Daniel Sanders5c582b22014-05-22 11:23:21 +00001273 int32_t BranchOffset = SignExtend32<16>(Offset) << 1;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001274 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1275 return MCDisassembler::Success;
1276}
1277
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001278static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1279 unsigned Insn,
1280 uint64_t Address,
1281 const void *Decoder) {
1282 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1283 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1284 return MCDisassembler::Success;
1285}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001286
1287static DecodeStatus DecodeSimm16(MCInst &Inst,
1288 unsigned Insn,
1289 uint64_t Address,
1290 const void *Decoder) {
1291 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1292 return MCDisassembler::Success;
1293}
1294
Matheus Almeida779c5932013-11-18 12:32:49 +00001295static DecodeStatus DecodeLSAImm(MCInst &Inst,
1296 unsigned Insn,
1297 uint64_t Address,
1298 const void *Decoder) {
1299 // We add one to the immediate field as it was encoded as 'imm - 1'.
1300 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1301 return MCDisassembler::Success;
1302}
1303
Akira Hatanaka71928e62012-04-17 18:03:21 +00001304static DecodeStatus DecodeInsSize(MCInst &Inst,
1305 unsigned Insn,
1306 uint64_t Address,
1307 const void *Decoder) {
1308 // First we need to grab the pos(lsb) from MCInst.
1309 int Pos = Inst.getOperand(2).getImm();
1310 int Size = (int) Insn - Pos + 1;
1311 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1312 return MCDisassembler::Success;
1313}
1314
1315static DecodeStatus DecodeExtSize(MCInst &Inst,
1316 unsigned Insn,
1317 uint64_t Address,
1318 const void *Decoder) {
1319 int Size = (int) Insn + 1;
1320 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1321 return MCDisassembler::Success;
1322}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001323
1324static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1325 uint64_t Address, const void *Decoder) {
1326 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) << 2));
1327 return MCDisassembler::Success;
1328}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001329
1330static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1331 uint64_t Address, const void *Decoder) {
1332 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) << 3));
1333 return MCDisassembler::Success;
1334}