blob: 53fb7a10ad0dd2b986c428c9dd49971495383b2c [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
145static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
146 unsigned RegNo,
147 uint64_t Address,
148 const void *Decoder);
149
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000150static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
151 unsigned RegNo,
152 uint64_t Address,
153 const void *Decoder);
154
Daniel Sanders0fa60412014-06-12 13:39:06 +0000155static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
156 uint64_t Address,
157 const void *Decoder);
158
Akira Hatanaka71928e62012-04-17 18:03:21 +0000159static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
160 unsigned Insn,
161 uint64_t Address,
162 const void *Decoder);
163
164static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
165 unsigned RegNo,
166 uint64_t Address,
167 const void *Decoder);
168
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000169static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
170 unsigned RegNo,
171 uint64_t Address,
172 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000173
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000174static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
175 unsigned RegNo,
176 uint64_t Address,
177 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000178
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000179static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
180 unsigned RegNo,
181 uint64_t Address,
182 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000183
Jack Carter3eb663b2013-09-26 00:09:46 +0000184static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
185 unsigned RegNo,
186 uint64_t Address,
187 const void *Decoder);
188
Jack Carter5dc8ac92013-09-25 23:50:44 +0000189static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
190 unsigned RegNo,
191 uint64_t Address,
192 const void *Decoder);
193
194static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
195 unsigned RegNo,
196 uint64_t Address,
197 const void *Decoder);
198
199static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
200 unsigned RegNo,
201 uint64_t Address,
202 const void *Decoder);
203
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000204static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
205 unsigned RegNo,
206 uint64_t Address,
207 const void *Decoder);
208
Daniel Sanders2a83d682014-05-21 12:56:39 +0000209static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
210 unsigned RegNo,
211 uint64_t Address,
212 const void *Decoder);
213
Akira Hatanaka71928e62012-04-17 18:03:21 +0000214static DecodeStatus DecodeBranchTarget(MCInst &Inst,
215 unsigned Offset,
216 uint64_t Address,
217 const void *Decoder);
218
Akira Hatanaka71928e62012-04-17 18:03:21 +0000219static DecodeStatus DecodeJumpTarget(MCInst &Inst,
220 unsigned Insn,
221 uint64_t Address,
222 const void *Decoder);
223
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000224static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
225 unsigned Offset,
226 uint64_t Address,
227 const void *Decoder);
228
229static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
230 unsigned Offset,
231 uint64_t Address,
232 const void *Decoder);
233
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000234// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
235// shifted left by 1 bit.
236static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
237 unsigned Offset,
238 uint64_t Address,
239 const void *Decoder);
240
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000241// DecodeJumpTargetMM - Decode microMIPS jump target, which is
242// shifted left by 1 bit.
243static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
244 unsigned Insn,
245 uint64_t Address,
246 const void *Decoder);
247
Akira Hatanaka71928e62012-04-17 18:03:21 +0000248static DecodeStatus DecodeMem(MCInst &Inst,
249 unsigned Insn,
250 uint64_t Address,
251 const void *Decoder);
252
Daniel Sanders92db6b72014-10-01 08:26:55 +0000253static DecodeStatus DecodeCacheOp(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 Sanders92db6b72014-10-01 08:26:55 +0000275static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
276 uint64_t Address,
277 const void *Decoder);
278
279static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
280 uint64_t Address,
281 const void *Decoder);
282
Daniel Sanders6a803f62014-06-16 13:13:03 +0000283static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
284 unsigned Insn,
285 uint64_t Address,
286 const void *Decoder);
287
Akira Hatanaka71928e62012-04-17 18:03:21 +0000288static DecodeStatus DecodeSimm16(MCInst &Inst,
289 unsigned Insn,
290 uint64_t Address,
291 const void *Decoder);
292
Matheus Almeida779c5932013-11-18 12:32:49 +0000293// Decode the immediate field of an LSA instruction which
294// is off by one.
295static DecodeStatus DecodeLSAImm(MCInst &Inst,
296 unsigned Insn,
297 uint64_t Address,
298 const void *Decoder);
299
Akira Hatanaka71928e62012-04-17 18:03:21 +0000300static DecodeStatus DecodeInsSize(MCInst &Inst,
301 unsigned Insn,
302 uint64_t Address,
303 const void *Decoder);
304
305static DecodeStatus DecodeExtSize(MCInst &Inst,
306 unsigned Insn,
307 uint64_t Address,
308 const void *Decoder);
309
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000310static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
311 uint64_t Address, const void *Decoder);
312
Zoran Jovanovic28551422014-06-09 09:49:51 +0000313static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
314 uint64_t Address, const void *Decoder);
315
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000316/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
317/// handle.
318template <typename InsnType>
319static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
320 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000321
322template <typename InsnType>
323static DecodeStatus
324DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
325 const void *Decoder);
326
327template <typename InsnType>
328static DecodeStatus
329DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
330 const void *Decoder);
331
332template <typename InsnType>
333static DecodeStatus
334DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
335 const void *Decoder);
336
337template <typename InsnType>
338static DecodeStatus
339DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
340 const void *Decoder);
341
342template <typename InsnType>
343static DecodeStatus
344DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
345 const void *Decoder);
346
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000347template <typename InsnType>
348static DecodeStatus
349DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
350 const void *Decoder);
351
Akira Hatanaka71928e62012-04-17 18:03:21 +0000352namespace llvm {
353extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
354 TheMips64elTarget;
355}
356
357static MCDisassembler *createMipsDisassembler(
358 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000359 const MCSubtargetInfo &STI,
360 MCContext &Ctx) {
361 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000362}
363
364static MCDisassembler *createMipselDisassembler(
365 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000366 const MCSubtargetInfo &STI,
367 MCContext &Ctx) {
368 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000369}
370
371static MCDisassembler *createMips64Disassembler(
372 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000373 const MCSubtargetInfo &STI,
374 MCContext &Ctx) {
375 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000376}
377
378static MCDisassembler *createMips64elDisassembler(
379 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000380 const MCSubtargetInfo &STI,
381 MCContext &Ctx) {
382 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000383}
384
385extern "C" void LLVMInitializeMipsDisassembler() {
386 // Register the disassembler.
387 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
388 createMipsDisassembler);
389 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
390 createMipselDisassembler);
391 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
392 createMips64Disassembler);
393 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
394 createMips64elDisassembler);
395}
396
Akira Hatanaka71928e62012-04-17 18:03:21 +0000397#include "MipsGenDisassemblerTables.inc"
398
Daniel Sanders5c582b22014-05-22 11:23:21 +0000399static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
400 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
401 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
402 return *(RegInfo->getRegClass(RC).begin() + RegNo);
403}
404
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000405template <typename InsnType>
406static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
407 const void *Decoder) {
408 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
409 // The size of the n field depends on the element size
410 // The register class also depends on this.
411 InsnType tmp = fieldFromInstruction(insn, 17, 5);
412 unsigned NSize = 0;
413 DecodeFN RegDecoder = nullptr;
414 if ((tmp & 0x18) == 0x00) { // INSVE_B
415 NSize = 4;
416 RegDecoder = DecodeMSA128BRegisterClass;
417 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
418 NSize = 3;
419 RegDecoder = DecodeMSA128HRegisterClass;
420 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
421 NSize = 2;
422 RegDecoder = DecodeMSA128WRegisterClass;
423 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
424 NSize = 1;
425 RegDecoder = DecodeMSA128DRegisterClass;
426 } else
427 llvm_unreachable("Invalid encoding");
428
429 assert(NSize != 0 && RegDecoder != nullptr);
430
431 // $wd
432 tmp = fieldFromInstruction(insn, 6, 5);
433 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
434 return MCDisassembler::Fail;
435 // $wd_in
436 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
437 return MCDisassembler::Fail;
438 // $n
439 tmp = fieldFromInstruction(insn, 16, NSize);
440 MI.addOperand(MCOperand::CreateImm(tmp));
441 // $ws
442 tmp = fieldFromInstruction(insn, 11, 5);
443 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
444 return MCDisassembler::Fail;
445 // $n2
446 MI.addOperand(MCOperand::CreateImm(0));
447
448 return MCDisassembler::Success;
449}
450
Daniel Sanders5c582b22014-05-22 11:23:21 +0000451template <typename InsnType>
452static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn,
453 uint64_t Address,
454 const void *Decoder) {
455 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
456 // (otherwise we would have matched the ADDI instruction from the earlier
457 // ISA's instead).
458 //
459 // We have:
460 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
461 // BOVC if rs >= rt
462 // BEQZALC if rs == 0 && rt != 0
463 // BEQC if rs < rt && rs != 0
464
465 InsnType Rs = fieldFromInstruction(insn, 21, 5);
466 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000467 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000468 bool HasRs = false;
469
470 if (Rs >= Rt) {
471 MI.setOpcode(Mips::BOVC);
472 HasRs = true;
473 } else if (Rs != 0 && Rs < Rt) {
474 MI.setOpcode(Mips::BEQC);
475 HasRs = true;
476 } else
477 MI.setOpcode(Mips::BEQZALC);
478
479 if (HasRs)
480 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
481 Rs)));
482
483 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
484 Rt)));
485 MI.addOperand(MCOperand::CreateImm(Imm));
486
487 return MCDisassembler::Success;
488}
489
490template <typename InsnType>
491static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn,
492 uint64_t Address,
493 const void *Decoder) {
494 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
495 // (otherwise we would have matched the ADDI instruction from the earlier
496 // ISA's instead).
497 //
498 // We have:
499 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
500 // BNVC if rs >= rt
501 // BNEZALC if rs == 0 && rt != 0
502 // BNEC if rs < rt && rs != 0
503
504 InsnType Rs = fieldFromInstruction(insn, 21, 5);
505 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000506 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000507 bool HasRs = false;
508
509 if (Rs >= Rt) {
510 MI.setOpcode(Mips::BNVC);
511 HasRs = true;
512 } else if (Rs != 0 && Rs < Rt) {
513 MI.setOpcode(Mips::BNEC);
514 HasRs = true;
515 } else
516 MI.setOpcode(Mips::BNEZALC);
517
518 if (HasRs)
519 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
520 Rs)));
521
522 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
523 Rt)));
524 MI.addOperand(MCOperand::CreateImm(Imm));
525
526 return MCDisassembler::Success;
527}
528
529template <typename InsnType>
530static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
531 uint64_t Address,
532 const void *Decoder) {
533 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
534 // (otherwise we would have matched the BLEZL instruction from the earlier
535 // ISA's instead).
536 //
537 // We have:
538 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
539 // Invalid if rs == 0
540 // BLEZC if rs == 0 && rt != 0
541 // BGEZC if rs == rt && rt != 0
542 // BGEC if rs != rt && rs != 0 && rt != 0
543
544 InsnType Rs = fieldFromInstruction(insn, 21, 5);
545 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000546 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000547 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000548
549 if (Rt == 0)
550 return MCDisassembler::Fail;
551 else if (Rs == 0)
552 MI.setOpcode(Mips::BLEZC);
553 else if (Rs == Rt)
554 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000555 else {
556 HasRs = true;
557 MI.setOpcode(Mips::BGEC);
558 }
559
560 if (HasRs)
561 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
562 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000563
564 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
565 Rt)));
566
567 MI.addOperand(MCOperand::CreateImm(Imm));
568
569 return MCDisassembler::Success;
570}
571
572template <typename InsnType>
573static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
574 uint64_t Address,
575 const void *Decoder) {
576 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
577 // (otherwise we would have matched the BGTZL instruction from the earlier
578 // ISA's instead).
579 //
580 // We have:
581 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
582 // Invalid if rs == 0
583 // BGTZC if rs == 0 && rt != 0
584 // BLTZC if rs == rt && rt != 0
585 // BLTC if rs != rt && rs != 0 && rt != 0
586
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000587 bool HasRs = false;
588
Daniel Sanders5c582b22014-05-22 11:23:21 +0000589 InsnType Rs = fieldFromInstruction(insn, 21, 5);
590 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000591 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000592
593 if (Rt == 0)
594 return MCDisassembler::Fail;
595 else if (Rs == 0)
596 MI.setOpcode(Mips::BGTZC);
597 else if (Rs == Rt)
598 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000599 else {
600 MI.setOpcode(Mips::BLTC);
601 HasRs = true;
602 }
603
604 if (HasRs)
605 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
606 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000607
608 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
609 Rt)));
610
611 MI.addOperand(MCOperand::CreateImm(Imm));
612
613 return MCDisassembler::Success;
614}
615
616template <typename InsnType>
617static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
618 uint64_t Address,
619 const void *Decoder) {
620 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
621 // (otherwise we would have matched the BGTZ instruction from the earlier
622 // ISA's instead).
623 //
624 // We have:
625 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
626 // BGTZ if rt == 0
627 // BGTZALC if rs == 0 && rt != 0
628 // BLTZALC if rs != 0 && rs == rt
629 // BLTUC if rs != 0 && rs != rt
630
631 InsnType Rs = fieldFromInstruction(insn, 21, 5);
632 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000633 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000634 bool HasRs = false;
635 bool HasRt = false;
636
637 if (Rt == 0) {
638 MI.setOpcode(Mips::BGTZ);
639 HasRs = true;
640 } else if (Rs == 0) {
641 MI.setOpcode(Mips::BGTZALC);
642 HasRt = true;
643 } else if (Rs == Rt) {
644 MI.setOpcode(Mips::BLTZALC);
645 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000646 } else {
647 MI.setOpcode(Mips::BLTUC);
648 HasRs = true;
649 HasRt = true;
650 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000651
652 if (HasRs)
653 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
654 Rs)));
655
656 if (HasRt)
657 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
658 Rt)));
659
660 MI.addOperand(MCOperand::CreateImm(Imm));
661
662 return MCDisassembler::Success;
663}
664
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000665template <typename InsnType>
666static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
667 uint64_t Address,
668 const void *Decoder) {
669 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
670 // (otherwise we would have matched the BLEZL instruction from the earlier
671 // ISA's instead).
672 //
673 // We have:
674 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
675 // Invalid if rs == 0
676 // BLEZALC if rs == 0 && rt != 0
677 // BGEZALC if rs == rt && rt != 0
678 // BGEUC if rs != rt && rs != 0 && rt != 0
679
680 InsnType Rs = fieldFromInstruction(insn, 21, 5);
681 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000682 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000683 bool HasRs = false;
684
685 if (Rt == 0)
686 return MCDisassembler::Fail;
687 else if (Rs == 0)
688 MI.setOpcode(Mips::BLEZALC);
689 else if (Rs == Rt)
690 MI.setOpcode(Mips::BGEZALC);
691 else {
692 HasRs = true;
693 MI.setOpcode(Mips::BGEUC);
694 }
695
696 if (HasRs)
697 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
698 Rs)));
699 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
700 Rt)));
701
702 MI.addOperand(MCOperand::CreateImm(Imm));
703
704 return MCDisassembler::Success;
705}
706
Akira Hatanaka71928e62012-04-17 18:03:21 +0000707 /// readInstruction - read four bytes from the MemoryObject
708 /// and return 32 bit word sorted according to the given endianess
709static DecodeStatus readInstruction32(const MemoryObject &region,
710 uint64_t address,
711 uint64_t &size,
712 uint32_t &insn,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000713 bool isBigEndian,
714 bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000715 uint8_t Bytes[4];
716
717 // We want to read exactly 4 Bytes of data.
Benjamin Kramer534d3a42013-05-24 10:54:58 +0000718 if (region.readBytes(address, 4, Bytes) == -1) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000719 size = 0;
720 return MCDisassembler::Fail;
721 }
722
723 if (isBigEndian) {
724 // Encoded as a big-endian 32-bit word in the stream.
725 insn = (Bytes[3] << 0) |
726 (Bytes[2] << 8) |
727 (Bytes[1] << 16) |
728 (Bytes[0] << 24);
729 }
730 else {
731 // Encoded as a small-endian 32-bit word in the stream.
Vladimir Medicdde3d582013-09-06 12:30:36 +0000732 // Little-endian byte ordering:
733 // mips32r2: 4 | 3 | 2 | 1
734 // microMIPS: 2 | 1 | 4 | 3
735 if (IsMicroMips) {
736 insn = (Bytes[2] << 0) |
737 (Bytes[3] << 8) |
738 (Bytes[0] << 16) |
739 (Bytes[1] << 24);
740 } else {
741 insn = (Bytes[0] << 0) |
742 (Bytes[1] << 8) |
743 (Bytes[2] << 16) |
744 (Bytes[3] << 24);
745 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000746 }
747
748 return MCDisassembler::Success;
749}
750
751DecodeStatus
752MipsDisassembler::getInstruction(MCInst &instr,
753 uint64_t &Size,
754 const MemoryObject &Region,
755 uint64_t Address,
756 raw_ostream &vStream,
757 raw_ostream &cStream) const {
758 uint32_t Insn;
759
760 DecodeStatus Result = readInstruction32(Region, Address, Size,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000761 Insn, isBigEndian, IsMicroMips);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000762 if (Result == MCDisassembler::Fail)
763 return MCDisassembler::Fail;
764
Vladimir Medicdde3d582013-09-06 12:30:36 +0000765 if (IsMicroMips) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000766 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit opcodes):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000767 // Calling the auto-generated decoder function.
768 Result = decodeInstruction(DecoderTableMicroMips32, instr, Insn, Address,
769 this, STI);
770 if (Result != MCDisassembler::Fail) {
771 Size = 4;
772 return Result;
773 }
774 return MCDisassembler::Fail;
775 }
776
Daniel Sandersc171f652014-06-13 13:15:59 +0000777 if (hasCOP3()) {
778 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
779 Result =
780 decodeInstruction(DecoderTableCOP3_32, instr, Insn, Address, this, STI);
781 if (Result != MCDisassembler::Fail) {
782 Size = 4;
783 return Result;
784 }
785 }
786
787 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000788 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
789 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, instr, Insn,
790 Address, this, STI);
791 if (Result != MCDisassembler::Fail) {
792 Size = 4;
793 return Result;
794 }
795 }
796
Daniel Sandersc171f652014-06-13 13:15:59 +0000797 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000798 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Daniel Sanders5c582b22014-05-22 11:23:21 +0000799 Result = decodeInstruction(DecoderTableMips32r6_64r632, instr, Insn,
800 Address, this, STI);
801 if (Result != MCDisassembler::Fail) {
802 Size = 4;
803 return Result;
804 }
805 }
806
Daniel Sanders0fa60412014-06-12 13:39:06 +0000807 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000808 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000809 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
810 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000811 if (Result != MCDisassembler::Fail) {
812 Size = 4;
813 return Result;
814 }
815
816 return MCDisassembler::Fail;
817}
818
819DecodeStatus
820Mips64Disassembler::getInstruction(MCInst &instr,
821 uint64_t &Size,
822 const MemoryObject &Region,
823 uint64_t Address,
824 raw_ostream &vStream,
825 raw_ostream &cStream) const {
826 uint32_t Insn;
827
828 DecodeStatus Result = readInstruction32(Region, Address, Size,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000829 Insn, isBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000830 if (Result == MCDisassembler::Fail)
831 return MCDisassembler::Fail;
832
833 // Calling the auto-generated decoder function.
Jim Grosbachecaef492012-08-14 19:06:05 +0000834 Result = decodeInstruction(DecoderTableMips6432, instr, Insn, Address,
835 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000836 if (Result != MCDisassembler::Fail) {
837 Size = 4;
838 return Result;
839 }
840 // If we fail to decode in Mips64 decoder space we can try in Mips32
Jim Grosbachecaef492012-08-14 19:06:05 +0000841 Result = decodeInstruction(DecoderTableMips32, instr, Insn, Address,
842 this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000843 if (Result != MCDisassembler::Fail) {
844 Size = 4;
845 return Result;
846 }
847
848 return MCDisassembler::Fail;
849}
850
Reed Kotlerec8a5492013-02-14 03:05:25 +0000851static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
852 unsigned RegNo,
853 uint64_t Address,
854 const void *Decoder) {
855
856 return MCDisassembler::Fail;
857
858}
859
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000860static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
861 unsigned RegNo,
862 uint64_t Address,
863 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000864
865 if (RegNo > 31)
866 return MCDisassembler::Fail;
867
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000868 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000869 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000870 return MCDisassembler::Success;
871}
872
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000873static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
874 unsigned RegNo,
875 uint64_t Address,
876 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000877 if (RegNo > 31)
878 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000879 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000880 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000881 return MCDisassembler::Success;
882}
883
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000884static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
885 unsigned RegNo,
886 uint64_t Address,
887 const void *Decoder) {
888 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
889 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
890
891 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
892}
893
Akira Hatanaka654655f2013-08-14 00:53:38 +0000894static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
895 unsigned RegNo,
896 uint64_t Address,
897 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000898 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000899}
900
Akira Hatanaka71928e62012-04-17 18:03:21 +0000901static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
902 unsigned RegNo,
903 uint64_t Address,
904 const void *Decoder) {
905 if (RegNo > 31)
906 return MCDisassembler::Fail;
907
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000908 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
909 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000910 return MCDisassembler::Success;
911}
912
913static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
914 unsigned RegNo,
915 uint64_t Address,
916 const void *Decoder) {
917 if (RegNo > 31)
918 return MCDisassembler::Fail;
919
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000920 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
921 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000922 return MCDisassembler::Success;
923}
924
925static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
926 unsigned RegNo,
927 uint64_t Address,
928 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +0000929 if (RegNo > 31)
930 return MCDisassembler::Fail;
931 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
932 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000933 return MCDisassembler::Success;
934}
935
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000936static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
937 unsigned RegNo,
938 uint64_t Address,
939 const void *Decoder) {
940 if (RegNo > 7)
941 return MCDisassembler::Fail;
942 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
943 Inst.addOperand(MCOperand::CreateReg(Reg));
944 return MCDisassembler::Success;
945}
946
Daniel Sanders0fa60412014-06-12 13:39:06 +0000947static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
948 uint64_t Address,
949 const void *Decoder) {
950 if (RegNo > 31)
951 return MCDisassembler::Fail;
952
953 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
954 Inst.addOperand(MCOperand::CreateReg(Reg));
955 return MCDisassembler::Success;
956}
957
Akira Hatanaka71928e62012-04-17 18:03:21 +0000958static DecodeStatus DecodeMem(MCInst &Inst,
959 unsigned Insn,
960 uint64_t Address,
961 const void *Decoder) {
962 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +0000963 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
964 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000965
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000966 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
967 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000968
969 if(Inst.getOpcode() == Mips::SC){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000970 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000971 }
972
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000973 Inst.addOperand(MCOperand::CreateReg(Reg));
974 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000975 Inst.addOperand(MCOperand::CreateImm(Offset));
976
977 return MCDisassembler::Success;
978}
979
Daniel Sanders92db6b72014-10-01 08:26:55 +0000980static DecodeStatus DecodeCacheOp(MCInst &Inst,
981 unsigned Insn,
982 uint64_t Address,
983 const void *Decoder) {
984 int Offset = SignExtend32<16>(Insn & 0xffff);
985 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
986 unsigned Base = fieldFromInstruction(Insn, 21, 5);
987
988 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
989
990 Inst.addOperand(MCOperand::CreateReg(Base));
991 Inst.addOperand(MCOperand::CreateImm(Offset));
992 Inst.addOperand(MCOperand::CreateImm(Hint));
993
994 return MCDisassembler::Success;
995}
996
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000997static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
998 uint64_t Address, const void *Decoder) {
999 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1000 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1001 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1002
1003 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1004 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1005
1006 Inst.addOperand(MCOperand::CreateReg(Reg));
1007 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001008
1009 // The immediate field of an LD/ST instruction is scaled which means it must
1010 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1011 // data format.
1012 // .b - 1 byte
1013 // .h - 2 bytes
1014 // .w - 4 bytes
1015 // .d - 8 bytes
1016 switch(Inst.getOpcode())
1017 {
1018 default:
1019 assert (0 && "Unexpected instruction");
1020 return MCDisassembler::Fail;
1021 break;
1022 case Mips::LD_B:
1023 case Mips::ST_B:
1024 Inst.addOperand(MCOperand::CreateImm(Offset));
1025 break;
1026 case Mips::LD_H:
1027 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001028 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001029 break;
1030 case Mips::LD_W:
1031 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001032 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001033 break;
1034 case Mips::LD_D:
1035 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001036 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001037 break;
1038 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001039
1040 return MCDisassembler::Success;
1041}
1042
Vladimir Medicdde3d582013-09-06 12:30:36 +00001043static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1044 unsigned Insn,
1045 uint64_t Address,
1046 const void *Decoder) {
1047 int Offset = SignExtend32<12>(Insn & 0x0fff);
1048 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1049 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1050
1051 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1052 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1053
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001054 if (Inst.getOpcode() == Mips::SC_MM)
1055 Inst.addOperand(MCOperand::CreateReg(Reg));
1056
Vladimir Medicdde3d582013-09-06 12:30:36 +00001057 Inst.addOperand(MCOperand::CreateReg(Reg));
1058 Inst.addOperand(MCOperand::CreateReg(Base));
1059 Inst.addOperand(MCOperand::CreateImm(Offset));
1060
1061 return MCDisassembler::Success;
1062}
1063
1064static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1065 unsigned Insn,
1066 uint64_t Address,
1067 const void *Decoder) {
1068 int Offset = SignExtend32<16>(Insn & 0xffff);
1069 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1070 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1071
1072 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1073 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1074
1075 Inst.addOperand(MCOperand::CreateReg(Reg));
1076 Inst.addOperand(MCOperand::CreateReg(Base));
1077 Inst.addOperand(MCOperand::CreateImm(Offset));
1078
1079 return MCDisassembler::Success;
1080}
1081
Akira Hatanaka71928e62012-04-17 18:03:21 +00001082static DecodeStatus DecodeFMem(MCInst &Inst,
1083 unsigned Insn,
1084 uint64_t Address,
1085 const void *Decoder) {
1086 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001087 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1088 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001089
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001090 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001091 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001092
1093 Inst.addOperand(MCOperand::CreateReg(Reg));
1094 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001095 Inst.addOperand(MCOperand::CreateImm(Offset));
1096
1097 return MCDisassembler::Success;
1098}
1099
Daniel Sanders92db6b72014-10-01 08:26:55 +00001100static DecodeStatus DecodeFMem2(MCInst &Inst,
1101 unsigned Insn,
1102 uint64_t Address,
1103 const void *Decoder) {
1104 int Offset = SignExtend32<16>(Insn & 0xffff);
1105 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1106 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1107
1108 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1109 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1110
1111 Inst.addOperand(MCOperand::CreateReg(Reg));
1112 Inst.addOperand(MCOperand::CreateReg(Base));
1113 Inst.addOperand(MCOperand::CreateImm(Offset));
1114
1115 return MCDisassembler::Success;
1116}
1117
1118static DecodeStatus DecodeFMem3(MCInst &Inst,
1119 unsigned Insn,
1120 uint64_t Address,
1121 const void *Decoder) {
1122 int Offset = SignExtend32<16>(Insn & 0xffff);
1123 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1124 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1125
1126 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1127 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1128
1129 Inst.addOperand(MCOperand::CreateReg(Reg));
1130 Inst.addOperand(MCOperand::CreateReg(Base));
1131 Inst.addOperand(MCOperand::CreateImm(Offset));
1132
1133 return MCDisassembler::Success;
1134}
1135
Daniel Sanders6a803f62014-06-16 13:13:03 +00001136static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1137 unsigned Insn,
1138 uint64_t Address,
1139 const void *Decoder) {
1140 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1141 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1142 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1143
1144 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1145 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1146
1147 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1148 Inst.addOperand(MCOperand::CreateReg(Rt));
1149 }
1150
1151 Inst.addOperand(MCOperand::CreateReg(Rt));
1152 Inst.addOperand(MCOperand::CreateReg(Base));
1153 Inst.addOperand(MCOperand::CreateImm(Offset));
1154
1155 return MCDisassembler::Success;
1156}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001157
1158static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1159 unsigned RegNo,
1160 uint64_t Address,
1161 const void *Decoder) {
1162 // Currently only hardware register 29 is supported.
1163 if (RegNo != 29)
1164 return MCDisassembler::Fail;
1165 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1166 return MCDisassembler::Success;
1167}
1168
Akira Hatanaka71928e62012-04-17 18:03:21 +00001169static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1170 unsigned RegNo,
1171 uint64_t Address,
1172 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001173 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001174 return MCDisassembler::Fail;
1175
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001176 ;
1177 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1178 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001179 return MCDisassembler::Success;
1180}
1181
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001182static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1183 unsigned RegNo,
1184 uint64_t Address,
1185 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001186 if (RegNo >= 4)
1187 return MCDisassembler::Fail;
1188
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001189 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001190 Inst.addOperand(MCOperand::CreateReg(Reg));
1191 return MCDisassembler::Success;
1192}
1193
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001194static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1195 unsigned RegNo,
1196 uint64_t Address,
1197 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001198 if (RegNo >= 4)
1199 return MCDisassembler::Fail;
1200
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001201 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001202 Inst.addOperand(MCOperand::CreateReg(Reg));
1203 return MCDisassembler::Success;
1204}
1205
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001206static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1207 unsigned RegNo,
1208 uint64_t Address,
1209 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001210 if (RegNo >= 4)
1211 return MCDisassembler::Fail;
1212
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001213 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001214 Inst.addOperand(MCOperand::CreateReg(Reg));
1215 return MCDisassembler::Success;
1216}
1217
Jack Carter3eb663b2013-09-26 00:09:46 +00001218static DecodeStatus DecodeMSA128BRegisterClass(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::MSA128BRegClassID, RegNo);
1226 Inst.addOperand(MCOperand::CreateReg(Reg));
1227 return MCDisassembler::Success;
1228}
1229
Jack Carter5dc8ac92013-09-25 23:50:44 +00001230static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1231 unsigned RegNo,
1232 uint64_t Address,
1233 const void *Decoder) {
1234 if (RegNo > 31)
1235 return MCDisassembler::Fail;
1236
1237 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1238 Inst.addOperand(MCOperand::CreateReg(Reg));
1239 return MCDisassembler::Success;
1240}
1241
1242static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1243 unsigned RegNo,
1244 uint64_t Address,
1245 const void *Decoder) {
1246 if (RegNo > 31)
1247 return MCDisassembler::Fail;
1248
1249 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1250 Inst.addOperand(MCOperand::CreateReg(Reg));
1251 return MCDisassembler::Success;
1252}
1253
1254static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1255 unsigned RegNo,
1256 uint64_t Address,
1257 const void *Decoder) {
1258 if (RegNo > 31)
1259 return MCDisassembler::Fail;
1260
1261 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1262 Inst.addOperand(MCOperand::CreateReg(Reg));
1263 return MCDisassembler::Success;
1264}
1265
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001266static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1267 unsigned RegNo,
1268 uint64_t Address,
1269 const void *Decoder) {
1270 if (RegNo > 7)
1271 return MCDisassembler::Fail;
1272
1273 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1274 Inst.addOperand(MCOperand::CreateReg(Reg));
1275 return MCDisassembler::Success;
1276}
1277
Daniel Sanders2a83d682014-05-21 12:56:39 +00001278static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1279 unsigned RegNo,
1280 uint64_t Address,
1281 const void *Decoder) {
1282 if (RegNo > 31)
1283 return MCDisassembler::Fail;
1284
1285 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1286 Inst.addOperand(MCOperand::CreateReg(Reg));
1287 return MCDisassembler::Success;
1288}
1289
Akira Hatanaka71928e62012-04-17 18:03:21 +00001290static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1291 unsigned Offset,
1292 uint64_t Address,
1293 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001294 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001295 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1296 return MCDisassembler::Success;
1297}
1298
Akira Hatanaka71928e62012-04-17 18:03:21 +00001299static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1300 unsigned Insn,
1301 uint64_t Address,
1302 const void *Decoder) {
1303
Jim Grosbachecaef492012-08-14 19:06:05 +00001304 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001305 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1306 return MCDisassembler::Success;
1307}
1308
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001309static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1310 unsigned Offset,
1311 uint64_t Address,
1312 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001313 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001314
1315 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1316 return MCDisassembler::Success;
1317}
1318
1319static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1320 unsigned Offset,
1321 uint64_t Address,
1322 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001323 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001324
1325 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1326 return MCDisassembler::Success;
1327}
1328
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001329static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1330 unsigned Offset,
1331 uint64_t Address,
1332 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001333 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001334 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1335 return MCDisassembler::Success;
1336}
1337
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001338static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1339 unsigned Insn,
1340 uint64_t Address,
1341 const void *Decoder) {
1342 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1343 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1344 return MCDisassembler::Success;
1345}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001346
1347static DecodeStatus DecodeSimm16(MCInst &Inst,
1348 unsigned Insn,
1349 uint64_t Address,
1350 const void *Decoder) {
1351 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1352 return MCDisassembler::Success;
1353}
1354
Matheus Almeida779c5932013-11-18 12:32:49 +00001355static DecodeStatus DecodeLSAImm(MCInst &Inst,
1356 unsigned Insn,
1357 uint64_t Address,
1358 const void *Decoder) {
1359 // We add one to the immediate field as it was encoded as 'imm - 1'.
1360 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1361 return MCDisassembler::Success;
1362}
1363
Akira Hatanaka71928e62012-04-17 18:03:21 +00001364static DecodeStatus DecodeInsSize(MCInst &Inst,
1365 unsigned Insn,
1366 uint64_t Address,
1367 const void *Decoder) {
1368 // First we need to grab the pos(lsb) from MCInst.
1369 int Pos = Inst.getOperand(2).getImm();
1370 int Size = (int) Insn - Pos + 1;
1371 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1372 return MCDisassembler::Success;
1373}
1374
1375static DecodeStatus DecodeExtSize(MCInst &Inst,
1376 unsigned Insn,
1377 uint64_t Address,
1378 const void *Decoder) {
1379 int Size = (int) Insn + 1;
1380 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1381 return MCDisassembler::Success;
1382}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001383
1384static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1385 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001386 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001387 return MCDisassembler::Success;
1388}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001389
1390static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1391 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001392 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001393 return MCDisassembler::Success;
1394}