blob: 58a857b662a4e94979bfe73d9fc5c81b12c21be5 [file] [log] [blame]
Akira Hatanaka71928e62012-04-17 18:03:21 +00001//===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is part of the Mips Disassembler.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Mips.h"
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000015#include "MipsRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "MipsSubtarget.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000017#include "llvm/MC/MCContext.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000018#include "llvm/MC/MCDisassembler.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000019#include "llvm/MC/MCFixedLenDisassembler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22#include "llvm/Support/MathExtras.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000023#include "llvm/Support/TargetRegistry.h"
Akira Hatanaka71928e62012-04-17 18:03:21 +000024
Akira Hatanaka71928e62012-04-17 18:03:21 +000025using namespace llvm;
26
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "mips-disassembler"
28
Akira Hatanaka71928e62012-04-17 18:03:21 +000029typedef MCDisassembler::DecodeStatus DecodeStatus;
30
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000031namespace {
32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000033/// A disasembler class for Mips.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000034class MipsDisassemblerBase : public MCDisassembler {
Akira Hatanaka71928e62012-04-17 18:03:21 +000035public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000036 MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000037 bool IsBigEndian)
38 : MCDisassembler(STI, Ctx),
39 IsN64(STI.getFeatureBits() & Mips::FeatureN64),
40 IsBigEndian(IsBigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000041
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000042 virtual ~MipsDisassemblerBase() {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000043
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000044 bool isN64() const { return IsN64; }
45
Akira Hatanaka71928e62012-04-17 18:03:21 +000046private:
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +000047 bool IsN64;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000048protected:
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000049 bool IsBigEndian;
Akira Hatanaka71928e62012-04-17 18:03:21 +000050};
51
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000052/// A disasembler class for Mips32.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000053class MipsDisassembler : public MipsDisassemblerBase {
Vladimir Medicdde3d582013-09-06 12:30:36 +000054 bool IsMicroMips;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000055public:
Daniel Sandersc171f652014-06-13 13:15:59 +000056 MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian)
57 : MipsDisassemblerBase(STI, Ctx, bigEndian) {
58 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
59 }
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000060
Daniel Sandersc171f652014-06-13 13:15:59 +000061 bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
62 bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; }
63 bool hasMips32r6() const {
Daniel Sanders5c582b22014-05-22 11:23:21 +000064 return STI.getFeatureBits() & Mips::FeatureMips32r6;
65 }
66
Daniel Sanders0fa60412014-06-12 13:39:06 +000067 bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
68
Daniel Sandersc171f652014-06-13 13:15:59 +000069 bool hasCOP3() const {
70 // Only present in MIPS-I and MIPS-II
71 return !hasMips32() && !hasMips3();
72 }
73
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000074 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000075 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000076 raw_ostream &VStream,
77 raw_ostream &CStream) const override;
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000078};
79
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000080/// A disasembler class for Mips64.
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000081class Mips64Disassembler : public MipsDisassemblerBase {
Akira Hatanaka71928e62012-04-17 18:03:21 +000082public:
Lang Hamesa1bc0f52014-04-15 04:40:56 +000083 Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
Akira Hatanaka9bf2b562012-07-09 18:46:47 +000084 bool bigEndian) :
Lang Hamesa1bc0f52014-04-15 04:40:56 +000085 MipsDisassemblerBase(STI, Ctx, bigEndian) {}
Akira Hatanaka71928e62012-04-17 18:03:21 +000086
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000087 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000088 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000089 raw_ostream &VStream,
90 raw_ostream &CStream) const override;
Akira Hatanaka71928e62012-04-17 18:03:21 +000091};
92
Benjamin Kramercb3e98c2012-05-01 14:34:24 +000093} // end anonymous namespace
94
Akira Hatanaka71928e62012-04-17 18:03:21 +000095// Forward declare these because the autogenerated code will reference them.
96// Definitions are further down.
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000097static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
98 unsigned RegNo,
99 uint64_t Address,
100 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000101
Reed Kotlerec8a5492013-02-14 03:05:25 +0000102static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
103 unsigned RegNo,
104 uint64_t Address,
105 const void *Decoder);
106
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000107static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
108 unsigned RegNo,
109 uint64_t Address,
110 const void *Decoder);
111
Jozef Kolek1904fa22014-11-24 14:25:53 +0000112static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
113 unsigned RegNo,
114 uint64_t Address,
115 const void *Decoder);
116
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000117static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
118 unsigned RegNo,
119 uint64_t Address,
120 const void *Decoder);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000121
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000122static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
123 unsigned Insn,
124 uint64_t Address,
125 const void *Decoder);
126
Akira Hatanaka654655f2013-08-14 00:53:38 +0000127static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
128 unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000131
Akira Hatanaka71928e62012-04-17 18:03:21 +0000132static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
133 unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
136
137static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
138 unsigned RegNo,
139 uint64_t Address,
140 const void *Decoder);
141
142static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
143 unsigned RegNo,
144 uint64_t Address,
145 const void *Decoder);
146
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +0000147static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
148 unsigned RegNo,
149 uint64_t Address,
150 const void *Decoder);
151
Daniel Sanders0fa60412014-06-12 13:39:06 +0000152static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
153 uint64_t Address,
154 const void *Decoder);
155
Akira Hatanaka71928e62012-04-17 18:03:21 +0000156static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
157 unsigned Insn,
158 uint64_t Address,
159 const void *Decoder);
160
161static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
162 unsigned RegNo,
163 uint64_t Address,
164 const void *Decoder);
165
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +0000166static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
167 unsigned RegNo,
168 uint64_t Address,
169 const void *Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000170
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000171static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
172 unsigned RegNo,
173 uint64_t Address,
174 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000175
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000176static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
177 unsigned RegNo,
178 uint64_t Address,
179 const void *Decoder);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +0000180
Jack Carter3eb663b2013-09-26 00:09:46 +0000181static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
182 unsigned RegNo,
183 uint64_t Address,
184 const void *Decoder);
185
Jack Carter5dc8ac92013-09-25 23:50:44 +0000186static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
187 unsigned RegNo,
188 uint64_t Address,
189 const void *Decoder);
190
191static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
192 unsigned RegNo,
193 uint64_t Address,
194 const void *Decoder);
195
196static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
197 unsigned RegNo,
198 uint64_t Address,
199 const void *Decoder);
200
Matheus Almeidaa591fdc2013-10-21 12:26:50 +0000201static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
202 unsigned RegNo,
203 uint64_t Address,
204 const void *Decoder);
205
Daniel Sanders2a83d682014-05-21 12:56:39 +0000206static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
207 unsigned RegNo,
208 uint64_t Address,
209 const void *Decoder);
210
Akira Hatanaka71928e62012-04-17 18:03:21 +0000211static DecodeStatus DecodeBranchTarget(MCInst &Inst,
212 unsigned Offset,
213 uint64_t Address,
214 const void *Decoder);
215
Akira Hatanaka71928e62012-04-17 18:03:21 +0000216static DecodeStatus DecodeJumpTarget(MCInst &Inst,
217 unsigned Insn,
218 uint64_t Address,
219 const void *Decoder);
220
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000221static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
222 unsigned Offset,
223 uint64_t Address,
224 const void *Decoder);
225
226static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
227 unsigned Offset,
228 uint64_t Address,
229 const void *Decoder);
230
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000231// DecodeBranchTargetMM - Decode microMIPS branch offset, which is
232// shifted left by 1 bit.
233static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
234 unsigned Offset,
235 uint64_t Address,
236 const void *Decoder);
237
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000238// DecodeJumpTargetMM - Decode microMIPS jump target, which is
239// shifted left by 1 bit.
240static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
241 unsigned Insn,
242 uint64_t Address,
243 const void *Decoder);
244
Akira Hatanaka71928e62012-04-17 18:03:21 +0000245static DecodeStatus DecodeMem(MCInst &Inst,
246 unsigned Insn,
247 uint64_t Address,
248 const void *Decoder);
249
Daniel Sanders92db6b72014-10-01 08:26:55 +0000250static DecodeStatus DecodeCacheOp(MCInst &Inst,
251 unsigned Insn,
252 uint64_t Address,
253 const void *Decoder);
254
Daniel Sandersb4484d62014-11-27 17:28:10 +0000255static DecodeStatus DecodeSyncI(MCInst &Inst,
256 unsigned Insn,
257 uint64_t Address,
258 const void *Decoder);
259
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +0000260static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
261 uint64_t Address, const void *Decoder);
262
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000263static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
264 unsigned Insn,
265 uint64_t Address,
266 const void *Decoder);
267
Vladimir Medicdde3d582013-09-06 12:30:36 +0000268static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
269 unsigned Insn,
270 uint64_t Address,
271 const void *Decoder);
272
273static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
274 unsigned Insn,
275 uint64_t Address,
276 const void *Decoder);
277
Akira Hatanaka71928e62012-04-17 18:03:21 +0000278static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn,
279 uint64_t Address,
280 const void *Decoder);
281
Daniel Sanders92db6b72014-10-01 08:26:55 +0000282static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn,
283 uint64_t Address,
284 const void *Decoder);
285
286static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn,
287 uint64_t Address,
288 const void *Decoder);
289
Daniel Sanders6a803f62014-06-16 13:13:03 +0000290static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
291 unsigned Insn,
292 uint64_t Address,
293 const void *Decoder);
294
Jozef Kolekaa2b9272014-11-27 14:41:44 +0000295static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
296 unsigned Value,
297 uint64_t Address,
298 const void *Decoder);
299
300static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
301 unsigned Value,
302 uint64_t Address,
303 const void *Decoder);
304
305static DecodeStatus DecodeLiSimm7(MCInst &Inst,
306 unsigned Value,
307 uint64_t Address,
308 const void *Decoder);
309
310static DecodeStatus DecodeSimm4(MCInst &Inst,
311 unsigned Value,
312 uint64_t Address,
313 const void *Decoder);
314
Akira Hatanaka71928e62012-04-17 18:03:21 +0000315static DecodeStatus DecodeSimm16(MCInst &Inst,
316 unsigned Insn,
317 uint64_t Address,
318 const void *Decoder);
319
Matheus Almeida779c5932013-11-18 12:32:49 +0000320// Decode the immediate field of an LSA instruction which
321// is off by one.
322static DecodeStatus DecodeLSAImm(MCInst &Inst,
323 unsigned Insn,
324 uint64_t Address,
325 const void *Decoder);
326
Akira Hatanaka71928e62012-04-17 18:03:21 +0000327static DecodeStatus DecodeInsSize(MCInst &Inst,
328 unsigned Insn,
329 uint64_t Address,
330 const void *Decoder);
331
332static DecodeStatus DecodeExtSize(MCInst &Inst,
333 unsigned Insn,
334 uint64_t Address,
335 const void *Decoder);
336
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000337static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
338 uint64_t Address, const void *Decoder);
339
Zoran Jovanovic28551422014-06-09 09:49:51 +0000340static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
341 uint64_t Address, const void *Decoder);
342
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000343/// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't
344/// handle.
345template <typename InsnType>
346static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
347 const void *Decoder);
Daniel Sanders5c582b22014-05-22 11:23:21 +0000348
349template <typename InsnType>
350static DecodeStatus
351DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
352 const void *Decoder);
353
354template <typename InsnType>
355static DecodeStatus
356DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
357 const void *Decoder);
358
359template <typename InsnType>
360static DecodeStatus
361DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
362 const void *Decoder);
363
364template <typename InsnType>
365static DecodeStatus
366DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
367 const void *Decoder);
368
369template <typename InsnType>
370static DecodeStatus
371DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
372 const void *Decoder);
373
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000374template <typename InsnType>
375static DecodeStatus
376DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address,
377 const void *Decoder);
378
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000379static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn,
380 uint64_t Address,
381 const void *Decoder);
382
Akira Hatanaka71928e62012-04-17 18:03:21 +0000383namespace llvm {
384extern Target TheMipselTarget, TheMipsTarget, TheMips64Target,
385 TheMips64elTarget;
386}
387
388static MCDisassembler *createMipsDisassembler(
389 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000390 const MCSubtargetInfo &STI,
391 MCContext &Ctx) {
392 return new MipsDisassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000393}
394
395static MCDisassembler *createMipselDisassembler(
396 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000397 const MCSubtargetInfo &STI,
398 MCContext &Ctx) {
399 return new MipsDisassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000400}
401
402static MCDisassembler *createMips64Disassembler(
403 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000404 const MCSubtargetInfo &STI,
405 MCContext &Ctx) {
406 return new Mips64Disassembler(STI, Ctx, true);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000407}
408
409static MCDisassembler *createMips64elDisassembler(
410 const Target &T,
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000411 const MCSubtargetInfo &STI,
412 MCContext &Ctx) {
413 return new Mips64Disassembler(STI, Ctx, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000414}
415
416extern "C" void LLVMInitializeMipsDisassembler() {
417 // Register the disassembler.
418 TargetRegistry::RegisterMCDisassembler(TheMipsTarget,
419 createMipsDisassembler);
420 TargetRegistry::RegisterMCDisassembler(TheMipselTarget,
421 createMipselDisassembler);
422 TargetRegistry::RegisterMCDisassembler(TheMips64Target,
423 createMips64Disassembler);
424 TargetRegistry::RegisterMCDisassembler(TheMips64elTarget,
425 createMips64elDisassembler);
426}
427
Akira Hatanaka71928e62012-04-17 18:03:21 +0000428#include "MipsGenDisassemblerTables.inc"
429
Daniel Sanders5c582b22014-05-22 11:23:21 +0000430static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
431 const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D);
432 const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
433 return *(RegInfo->getRegClass(RC).begin() + RegNo);
434}
435
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000436template <typename InsnType>
437static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address,
438 const void *Decoder) {
439 typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *);
440 // The size of the n field depends on the element size
441 // The register class also depends on this.
442 InsnType tmp = fieldFromInstruction(insn, 17, 5);
443 unsigned NSize = 0;
444 DecodeFN RegDecoder = nullptr;
445 if ((tmp & 0x18) == 0x00) { // INSVE_B
446 NSize = 4;
447 RegDecoder = DecodeMSA128BRegisterClass;
448 } else if ((tmp & 0x1c) == 0x10) { // INSVE_H
449 NSize = 3;
450 RegDecoder = DecodeMSA128HRegisterClass;
451 } else if ((tmp & 0x1e) == 0x18) { // INSVE_W
452 NSize = 2;
453 RegDecoder = DecodeMSA128WRegisterClass;
454 } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D
455 NSize = 1;
456 RegDecoder = DecodeMSA128DRegisterClass;
457 } else
458 llvm_unreachable("Invalid encoding");
459
460 assert(NSize != 0 && RegDecoder != nullptr);
461
462 // $wd
463 tmp = fieldFromInstruction(insn, 6, 5);
464 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
465 return MCDisassembler::Fail;
466 // $wd_in
467 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
468 return MCDisassembler::Fail;
469 // $n
470 tmp = fieldFromInstruction(insn, 16, NSize);
471 MI.addOperand(MCOperand::CreateImm(tmp));
472 // $ws
473 tmp = fieldFromInstruction(insn, 11, 5);
474 if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail)
475 return MCDisassembler::Fail;
476 // $n2
477 MI.addOperand(MCOperand::CreateImm(0));
478
479 return MCDisassembler::Success;
480}
481
Daniel Sanders5c582b22014-05-22 11:23:21 +0000482template <typename InsnType>
483static DecodeStatus DecodeAddiGroupBranch(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 // 0b001000 sssss ttttt iiiiiiiiiiiiiiii
492 // BOVC if rs >= rt
493 // BEQZALC if rs == 0 && rt != 0
494 // BEQC if rs < rt && rs != 0
495
496 InsnType Rs = fieldFromInstruction(insn, 21, 5);
497 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000498 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000499 bool HasRs = false;
500
501 if (Rs >= Rt) {
502 MI.setOpcode(Mips::BOVC);
503 HasRs = true;
504 } else if (Rs != 0 && Rs < Rt) {
505 MI.setOpcode(Mips::BEQC);
506 HasRs = true;
507 } else
508 MI.setOpcode(Mips::BEQZALC);
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 DecodeDaddiGroupBranch(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 ADDI instruction from the earlier
527 // ISA's instead).
528 //
529 // We have:
530 // 0b011000 sssss ttttt iiiiiiiiiiiiiiii
531 // BNVC if rs >= rt
532 // BNEZALC if rs == 0 && rt != 0
533 // BNEC if rs < rt && rs != 0
534
535 InsnType Rs = fieldFromInstruction(insn, 21, 5);
536 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000537 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000538 bool HasRs = false;
539
540 if (Rs >= Rt) {
541 MI.setOpcode(Mips::BNVC);
542 HasRs = true;
543 } else if (Rs != 0 && Rs < Rt) {
544 MI.setOpcode(Mips::BNEC);
545 HasRs = true;
546 } else
547 MI.setOpcode(Mips::BNEZALC);
548
549 if (HasRs)
550 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
551 Rs)));
552
553 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
554 Rt)));
555 MI.addOperand(MCOperand::CreateImm(Imm));
556
557 return MCDisassembler::Success;
558}
559
560template <typename InsnType>
561static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn,
562 uint64_t Address,
563 const void *Decoder) {
564 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
565 // (otherwise we would have matched the BLEZL instruction from the earlier
566 // ISA's instead).
567 //
568 // We have:
569 // 0b010110 sssss ttttt iiiiiiiiiiiiiiii
570 // Invalid if rs == 0
571 // BLEZC if rs == 0 && rt != 0
572 // BGEZC if rs == rt && rt != 0
573 // BGEC if rs != rt && rs != 0 && rt != 0
574
575 InsnType Rs = fieldFromInstruction(insn, 21, 5);
576 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000577 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000578 bool HasRs = false;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000579
580 if (Rt == 0)
581 return MCDisassembler::Fail;
582 else if (Rs == 0)
583 MI.setOpcode(Mips::BLEZC);
584 else if (Rs == Rt)
585 MI.setOpcode(Mips::BGEZC);
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000586 else {
587 HasRs = true;
588 MI.setOpcode(Mips::BGEC);
589 }
590
591 if (HasRs)
592 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
593 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000594
595 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
596 Rt)));
597
598 MI.addOperand(MCOperand::CreateImm(Imm));
599
600 return MCDisassembler::Success;
601}
602
603template <typename InsnType>
604static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn,
605 uint64_t Address,
606 const void *Decoder) {
607 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
608 // (otherwise we would have matched the BGTZL instruction from the earlier
609 // ISA's instead).
610 //
611 // We have:
612 // 0b010111 sssss ttttt iiiiiiiiiiiiiiii
613 // Invalid if rs == 0
614 // BGTZC if rs == 0 && rt != 0
615 // BLTZC if rs == rt && rt != 0
616 // BLTC if rs != rt && rs != 0 && rt != 0
617
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000618 bool HasRs = false;
619
Daniel Sanders5c582b22014-05-22 11:23:21 +0000620 InsnType Rs = fieldFromInstruction(insn, 21, 5);
621 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000622 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000623
624 if (Rt == 0)
625 return MCDisassembler::Fail;
626 else if (Rs == 0)
627 MI.setOpcode(Mips::BGTZC);
628 else if (Rs == Rt)
629 MI.setOpcode(Mips::BLTZC);
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000630 else {
631 MI.setOpcode(Mips::BLTC);
632 HasRs = true;
633 }
634
635 if (HasRs)
636 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
637 Rs)));
Daniel Sanders5c582b22014-05-22 11:23:21 +0000638
639 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
640 Rt)));
641
642 MI.addOperand(MCOperand::CreateImm(Imm));
643
644 return MCDisassembler::Success;
645}
646
647template <typename InsnType>
648static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn,
649 uint64_t Address,
650 const void *Decoder) {
651 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
652 // (otherwise we would have matched the BGTZ instruction from the earlier
653 // ISA's instead).
654 //
655 // We have:
656 // 0b000111 sssss ttttt iiiiiiiiiiiiiiii
657 // BGTZ if rt == 0
658 // BGTZALC if rs == 0 && rt != 0
659 // BLTZALC if rs != 0 && rs == rt
660 // BLTUC if rs != 0 && rs != rt
661
662 InsnType Rs = fieldFromInstruction(insn, 21, 5);
663 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000664 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Daniel Sanders5c582b22014-05-22 11:23:21 +0000665 bool HasRs = false;
666 bool HasRt = false;
667
668 if (Rt == 0) {
669 MI.setOpcode(Mips::BGTZ);
670 HasRs = true;
671 } else if (Rs == 0) {
672 MI.setOpcode(Mips::BGTZALC);
673 HasRt = true;
674 } else if (Rs == Rt) {
675 MI.setOpcode(Mips::BLTZALC);
676 HasRs = true;
Zoran Jovanovic5c14b062014-06-18 14:36:00 +0000677 } else {
678 MI.setOpcode(Mips::BLTUC);
679 HasRs = true;
680 HasRt = true;
681 }
Daniel Sanders5c582b22014-05-22 11:23:21 +0000682
683 if (HasRs)
684 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
685 Rs)));
686
687 if (HasRt)
688 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
689 Rt)));
690
691 MI.addOperand(MCOperand::CreateImm(Imm));
692
693 return MCDisassembler::Success;
694}
695
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000696template <typename InsnType>
697static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn,
698 uint64_t Address,
699 const void *Decoder) {
700 // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled
701 // (otherwise we would have matched the BLEZL instruction from the earlier
702 // ISA's instead).
703 //
704 // We have:
705 // 0b000110 sssss ttttt iiiiiiiiiiiiiiii
706 // Invalid if rs == 0
707 // BLEZALC if rs == 0 && rt != 0
708 // BGEZALC if rs == rt && rt != 0
709 // BGEUC if rs != rt && rs != 0 && rt != 0
710
711 InsnType Rs = fieldFromInstruction(insn, 21, 5);
712 InsnType Rt = fieldFromInstruction(insn, 16, 5);
Alexey Samsonovd37bab62014-09-02 17:49:16 +0000713 InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4;
Zoran Jovanovic28a0ca02014-06-12 11:47:44 +0000714 bool HasRs = false;
715
716 if (Rt == 0)
717 return MCDisassembler::Fail;
718 else if (Rs == 0)
719 MI.setOpcode(Mips::BLEZALC);
720 else if (Rs == Rt)
721 MI.setOpcode(Mips::BGEZALC);
722 else {
723 HasRs = true;
724 MI.setOpcode(Mips::BGEUC);
725 }
726
727 if (HasRs)
728 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
729 Rs)));
730 MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID,
731 Rt)));
732
733 MI.addOperand(MCOperand::CreateImm(Imm));
734
735 return MCDisassembler::Success;
736}
737
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000738/// Read two bytes from the ArrayRef and return 16 bit halfword sorted
739/// according to the given endianess.
740static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address,
741 uint64_t &Size, uint32_t &Insn,
742 bool IsBigEndian) {
743 // We want to read exactly 2 Bytes of data.
744 if (Bytes.size() < 2) {
745 Size = 0;
746 return MCDisassembler::Fail;
747 }
748
749 if (IsBigEndian) {
750 Insn = (Bytes[0] << 8) | Bytes[1];
751 } else {
752 Insn = (Bytes[1] << 8) | Bytes[0];
753 }
754
755 return MCDisassembler::Success;
756}
757
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000758/// Read four bytes from the ArrayRef and return 32 bit word sorted
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000759/// according to the given endianess
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000760static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
761 uint64_t &Size, uint32_t &Insn,
762 bool IsBigEndian, bool IsMicroMips) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000763 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000764 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000765 Size = 0;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000766 return MCDisassembler::Fail;
767 }
768
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000769 // High 16 bits of a 32-bit microMIPS instruction (where the opcode is)
770 // always precede the low 16 bits in the instruction stream (that is, they
771 // are placed at lower addresses in the instruction stream).
772 //
773 // microMIPS byte ordering:
774 // Big-endian: 0 | 1 | 2 | 3
775 // Little-endian: 1 | 0 | 3 | 2
776
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000777 if (IsBigEndian) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000778 // Encoded as a big-endian 32-bit word in the stream.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000779 Insn =
780 (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24);
781 } else {
Vladimir Medicdde3d582013-09-06 12:30:36 +0000782 if (IsMicroMips) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000783 Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000784 (Bytes[1] << 24);
785 } else {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000786 Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
Vladimir Medicdde3d582013-09-06 12:30:36 +0000787 (Bytes[3] << 24);
788 }
Akira Hatanaka71928e62012-04-17 18:03:21 +0000789 }
790
791 return MCDisassembler::Success;
792}
793
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000794DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000795 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000796 uint64_t Address,
797 raw_ostream &VStream,
798 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000799 uint32_t Insn;
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000800 DecodeStatus Result;
Akira Hatanaka71928e62012-04-17 18:03:21 +0000801
Vladimir Medicdde3d582013-09-06 12:30:36 +0000802 if (IsMicroMips) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000803 Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian);
804
805 DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n");
806 // Calling the auto-generated decoder function.
807 Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address,
808 this, STI);
809 if (Result != MCDisassembler::Fail) {
810 Size = 2;
811 return Result;
812 }
813
814 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true);
815 if (Result == MCDisassembler::Fail)
816 return MCDisassembler::Fail;
817
818 DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n");
Vladimir Medicdde3d582013-09-06 12:30:36 +0000819 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000820 Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address,
Vladimir Medicdde3d582013-09-06 12:30:36 +0000821 this, STI);
822 if (Result != MCDisassembler::Fail) {
823 Size = 4;
824 return Result;
825 }
826 return MCDisassembler::Fail;
827 }
828
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000829 Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
830 if (Result == MCDisassembler::Fail)
831 return MCDisassembler::Fail;
832
Daniel Sandersc171f652014-06-13 13:15:59 +0000833 if (hasCOP3()) {
834 DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n");
835 Result =
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000836 decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI);
Daniel Sandersc171f652014-06-13 13:15:59 +0000837 if (Result != MCDisassembler::Fail) {
838 Size = 4;
839 return Result;
840 }
841 }
842
843 if (hasMips32r6() && isGP64()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000844 DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000845 Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn,
Daniel Sanders0fa60412014-06-12 13:39:06 +0000846 Address, this, STI);
847 if (Result != MCDisassembler::Fail) {
848 Size = 4;
849 return Result;
850 }
851 }
852
Daniel Sandersc171f652014-06-13 13:15:59 +0000853 if (hasMips32r6()) {
Daniel Sanders0fa60412014-06-12 13:39:06 +0000854 DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n");
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000855 Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn,
Daniel Sanders5c582b22014-05-22 11:23:21 +0000856 Address, this, STI);
857 if (Result != MCDisassembler::Fail) {
858 Size = 4;
859 return Result;
860 }
861 }
862
Daniel Sanders0fa60412014-06-12 13:39:06 +0000863 DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n");
Akira Hatanaka71928e62012-04-17 18:03:21 +0000864 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000865 Result =
866 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000867 if (Result != MCDisassembler::Fail) {
868 Size = 4;
869 return Result;
870 }
871
872 return MCDisassembler::Fail;
873}
874
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000875DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000876 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000877 uint64_t Address,
878 raw_ostream &VStream,
879 raw_ostream &CStream) const {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000880 uint32_t Insn;
881
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000882 DecodeStatus Result =
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000883 readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000884 if (Result == MCDisassembler::Fail)
885 return MCDisassembler::Fail;
886
887 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000888 Result =
889 decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000890 if (Result != MCDisassembler::Fail) {
891 Size = 4;
892 return Result;
893 }
894 // If we fail to decode in Mips64 decoder space we can try in Mips32
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000895 Result =
896 decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI);
Akira Hatanaka71928e62012-04-17 18:03:21 +0000897 if (Result != MCDisassembler::Fail) {
898 Size = 4;
899 return Result;
900 }
901
902 return MCDisassembler::Fail;
903}
904
Reed Kotlerec8a5492013-02-14 03:05:25 +0000905static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst,
906 unsigned RegNo,
907 uint64_t Address,
908 const void *Decoder) {
909
910 return MCDisassembler::Fail;
911
912}
913
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000914static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst,
915 unsigned RegNo,
916 uint64_t Address,
917 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000918
919 if (RegNo > 31)
920 return MCDisassembler::Fail;
921
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000922 unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000923 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000924 return MCDisassembler::Success;
925}
926
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000927static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst,
928 unsigned RegNo,
929 uint64_t Address,
930 const void *Decoder) {
Jozef Kolekea22c4c2014-11-24 13:29:59 +0000931 if (RegNo > 7)
932 return MCDisassembler::Fail;
933 unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo);
934 Inst.addOperand(MCOperand::CreateReg(Reg));
935 return MCDisassembler::Success;
Zoran Jovanovicb0852e52014-10-21 08:23:11 +0000936}
937
Jozef Kolek1904fa22014-11-24 14:25:53 +0000938static DecodeStatus DecodeGPRMM16ZeroRegisterClass(MCInst &Inst,
939 unsigned RegNo,
940 uint64_t Address,
941 const void *Decoder) {
Jozef Kolek315e7ec2014-11-26 18:56:38 +0000942 if (RegNo > 7)
943 return MCDisassembler::Fail;
944 unsigned Reg = getReg(Decoder, Mips::GPRMM16ZeroRegClassID, RegNo);
945 Inst.addOperand(MCOperand::CreateReg(Reg));
946 return MCDisassembler::Success;
Jozef Kolek1904fa22014-11-24 14:25:53 +0000947}
948
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000949static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst,
950 unsigned RegNo,
951 uint64_t Address,
952 const void *Decoder) {
Akira Hatanaka71928e62012-04-17 18:03:21 +0000953 if (RegNo > 31)
954 return MCDisassembler::Fail;
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000955 unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000956 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000957 return MCDisassembler::Success;
958}
959
Akira Hatanaka9bfa2e22013-08-28 00:55:15 +0000960static DecodeStatus DecodePtrRegisterClass(MCInst &Inst,
961 unsigned RegNo,
962 uint64_t Address,
963 const void *Decoder) {
964 if (static_cast<const MipsDisassembler *>(Decoder)->isN64())
965 return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder);
966
967 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
968}
969
Akira Hatanaka654655f2013-08-14 00:53:38 +0000970static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst,
971 unsigned RegNo,
972 uint64_t Address,
973 const void *Decoder) {
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000974 return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +0000975}
976
Akira Hatanaka71928e62012-04-17 18:03:21 +0000977static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst,
978 unsigned RegNo,
979 uint64_t Address,
980 const void *Decoder) {
981 if (RegNo > 31)
982 return MCDisassembler::Fail;
983
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000984 unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo);
985 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000986 return MCDisassembler::Success;
987}
988
989static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst,
990 unsigned RegNo,
991 uint64_t Address,
992 const void *Decoder) {
993 if (RegNo > 31)
994 return MCDisassembler::Fail;
995
Akira Hatanaka9bf2b562012-07-09 18:46:47 +0000996 unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo);
997 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +0000998 return MCDisassembler::Success;
999}
1000
1001static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst,
1002 unsigned RegNo,
1003 uint64_t Address,
1004 const void *Decoder) {
Chad Rosier253777f2013-06-26 22:23:32 +00001005 if (RegNo > 31)
1006 return MCDisassembler::Fail;
1007 unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo);
1008 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001009 return MCDisassembler::Success;
1010}
1011
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001012static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst,
1013 unsigned RegNo,
1014 uint64_t Address,
1015 const void *Decoder) {
1016 if (RegNo > 7)
1017 return MCDisassembler::Fail;
1018 unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo);
1019 Inst.addOperand(MCOperand::CreateReg(Reg));
1020 return MCDisassembler::Success;
1021}
1022
Daniel Sanders0fa60412014-06-12 13:39:06 +00001023static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo,
1024 uint64_t Address,
1025 const void *Decoder) {
1026 if (RegNo > 31)
1027 return MCDisassembler::Fail;
1028
1029 unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo);
1030 Inst.addOperand(MCOperand::CreateReg(Reg));
1031 return MCDisassembler::Success;
1032}
1033
Akira Hatanaka71928e62012-04-17 18:03:21 +00001034static DecodeStatus DecodeMem(MCInst &Inst,
1035 unsigned Insn,
1036 uint64_t Address,
1037 const void *Decoder) {
1038 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001039 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1040 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001041
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001042 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1043 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001044
1045 if(Inst.getOpcode() == Mips::SC){
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001046 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001047 }
1048
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001049 Inst.addOperand(MCOperand::CreateReg(Reg));
1050 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001051 Inst.addOperand(MCOperand::CreateImm(Offset));
1052
1053 return MCDisassembler::Success;
1054}
1055
Daniel Sanders92db6b72014-10-01 08:26:55 +00001056static DecodeStatus DecodeCacheOp(MCInst &Inst,
1057 unsigned Insn,
1058 uint64_t Address,
1059 const void *Decoder) {
1060 int Offset = SignExtend32<16>(Insn & 0xffff);
1061 unsigned Hint = fieldFromInstruction(Insn, 16, 5);
1062 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1063
1064 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1065
1066 Inst.addOperand(MCOperand::CreateReg(Base));
1067 Inst.addOperand(MCOperand::CreateImm(Offset));
1068 Inst.addOperand(MCOperand::CreateImm(Hint));
1069
1070 return MCDisassembler::Success;
1071}
1072
Daniel Sandersb4484d62014-11-27 17:28:10 +00001073static DecodeStatus DecodeSyncI(MCInst &Inst,
1074 unsigned Insn,
1075 uint64_t Address,
1076 const void *Decoder) {
1077 int Offset = SignExtend32<16>(Insn & 0xffff);
1078 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1079
1080 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1081
1082 Inst.addOperand(MCOperand::CreateReg(Base));
1083 Inst.addOperand(MCOperand::CreateImm(Offset));
1084
1085 return MCDisassembler::Success;
1086}
1087
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001088static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn,
1089 uint64_t Address, const void *Decoder) {
1090 int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10));
1091 unsigned Reg = fieldFromInstruction(Insn, 6, 5);
1092 unsigned Base = fieldFromInstruction(Insn, 11, 5);
1093
1094 Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg);
1095 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1096
1097 Inst.addOperand(MCOperand::CreateReg(Reg));
1098 Inst.addOperand(MCOperand::CreateReg(Base));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001099
1100 // The immediate field of an LD/ST instruction is scaled which means it must
1101 // be multiplied (when decoding) by the size (in bytes) of the instructions'
1102 // data format.
1103 // .b - 1 byte
1104 // .h - 2 bytes
1105 // .w - 4 bytes
1106 // .d - 8 bytes
1107 switch(Inst.getOpcode())
1108 {
1109 default:
1110 assert (0 && "Unexpected instruction");
1111 return MCDisassembler::Fail;
1112 break;
1113 case Mips::LD_B:
1114 case Mips::ST_B:
1115 Inst.addOperand(MCOperand::CreateImm(Offset));
1116 break;
1117 case Mips::LD_H:
1118 case Mips::ST_H:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001119 Inst.addOperand(MCOperand::CreateImm(Offset * 2));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001120 break;
1121 case Mips::LD_W:
1122 case Mips::ST_W:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001123 Inst.addOperand(MCOperand::CreateImm(Offset * 4));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001124 break;
1125 case Mips::LD_D:
1126 case Mips::ST_D:
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001127 Inst.addOperand(MCOperand::CreateImm(Offset * 8));
Matheus Almeida6b59c442013-12-05 11:06:22 +00001128 break;
1129 }
Matheus Almeidafe0bf9f2013-10-21 13:07:13 +00001130
1131 return MCDisassembler::Success;
1132}
1133
Jozef Kolek315e7ec2014-11-26 18:56:38 +00001134static DecodeStatus DecodeMemMMImm4(MCInst &Inst,
1135 unsigned Insn,
1136 uint64_t Address,
1137 const void *Decoder) {
1138 unsigned Offset = Insn & 0xf;
1139 unsigned Reg = fieldFromInstruction(Insn, 7, 3);
1140 unsigned Base = fieldFromInstruction(Insn, 4, 3);
1141
1142 switch (Inst.getOpcode()) {
1143 case Mips::LBU16_MM:
1144 case Mips::LHU16_MM:
1145 case Mips::LW16_MM:
1146 if (DecodeGPRMM16RegisterClass(Inst, Reg, Address, Decoder)
1147 == MCDisassembler::Fail)
1148 return MCDisassembler::Fail;
1149 break;
1150 case Mips::SB16_MM:
1151 case Mips::SH16_MM:
1152 case Mips::SW16_MM:
1153 if (DecodeGPRMM16ZeroRegisterClass(Inst, Reg, Address, Decoder)
1154 == MCDisassembler::Fail)
1155 return MCDisassembler::Fail;
1156 break;
1157 }
1158
1159 if (DecodeGPRMM16RegisterClass(Inst, Base, Address, Decoder)
1160 == MCDisassembler::Fail)
1161 return MCDisassembler::Fail;
1162
1163 switch (Inst.getOpcode()) {
1164 case Mips::LBU16_MM:
1165 if (Offset == 0xf)
1166 Inst.addOperand(MCOperand::CreateImm(-1));
1167 else
1168 Inst.addOperand(MCOperand::CreateImm(Offset));
1169 break;
1170 case Mips::SB16_MM:
1171 Inst.addOperand(MCOperand::CreateImm(Offset));
1172 break;
1173 case Mips::LHU16_MM:
1174 case Mips::SH16_MM:
1175 Inst.addOperand(MCOperand::CreateImm(Offset << 1));
1176 break;
1177 case Mips::LW16_MM:
1178 case Mips::SW16_MM:
1179 Inst.addOperand(MCOperand::CreateImm(Offset << 2));
1180 break;
1181 }
1182
1183 return MCDisassembler::Success;
1184}
1185
Vladimir Medicdde3d582013-09-06 12:30:36 +00001186static DecodeStatus DecodeMemMMImm12(MCInst &Inst,
1187 unsigned Insn,
1188 uint64_t Address,
1189 const void *Decoder) {
1190 int Offset = SignExtend32<12>(Insn & 0x0fff);
1191 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1192 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1193
1194 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1195 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1196
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001197 switch (Inst.getOpcode()) {
1198 case Mips::SWM32_MM:
1199 case Mips::LWM32_MM:
1200 if (DecodeRegListOperand(Inst, Insn, Address, Decoder)
1201 == MCDisassembler::Fail)
1202 return MCDisassembler::Fail;
1203 Inst.addOperand(MCOperand::CreateReg(Base));
1204 Inst.addOperand(MCOperand::CreateImm(Offset));
1205 break;
1206 case Mips::SC_MM:
Zoran Jovanovic285cc282014-02-28 18:22:56 +00001207 Inst.addOperand(MCOperand::CreateReg(Reg));
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001208 // fallthrough
1209 default:
1210 Inst.addOperand(MCOperand::CreateReg(Reg));
1211 Inst.addOperand(MCOperand::CreateReg(Base));
1212 Inst.addOperand(MCOperand::CreateImm(Offset));
1213 }
Vladimir Medicdde3d582013-09-06 12:30:36 +00001214
1215 return MCDisassembler::Success;
1216}
1217
1218static DecodeStatus DecodeMemMMImm16(MCInst &Inst,
1219 unsigned Insn,
1220 uint64_t Address,
1221 const void *Decoder) {
1222 int Offset = SignExtend32<16>(Insn & 0xffff);
1223 unsigned Reg = fieldFromInstruction(Insn, 21, 5);
1224 unsigned Base = fieldFromInstruction(Insn, 16, 5);
1225
1226 Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg);
1227 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1228
1229 Inst.addOperand(MCOperand::CreateReg(Reg));
1230 Inst.addOperand(MCOperand::CreateReg(Base));
1231 Inst.addOperand(MCOperand::CreateImm(Offset));
1232
1233 return MCDisassembler::Success;
1234}
1235
Akira Hatanaka71928e62012-04-17 18:03:21 +00001236static DecodeStatus DecodeFMem(MCInst &Inst,
1237 unsigned Insn,
1238 uint64_t Address,
1239 const void *Decoder) {
1240 int Offset = SignExtend32<16>(Insn & 0xffff);
Jim Grosbachecaef492012-08-14 19:06:05 +00001241 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1242 unsigned Base = fieldFromInstruction(Insn, 21, 5);
Akira Hatanaka71928e62012-04-17 18:03:21 +00001243
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001244 Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00001245 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001246
1247 Inst.addOperand(MCOperand::CreateReg(Reg));
1248 Inst.addOperand(MCOperand::CreateReg(Base));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001249 Inst.addOperand(MCOperand::CreateImm(Offset));
1250
1251 return MCDisassembler::Success;
1252}
1253
Daniel Sanders92db6b72014-10-01 08:26:55 +00001254static DecodeStatus DecodeFMem2(MCInst &Inst,
1255 unsigned Insn,
1256 uint64_t Address,
1257 const void *Decoder) {
1258 int Offset = SignExtend32<16>(Insn & 0xffff);
1259 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1260 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1261
1262 Reg = getReg(Decoder, Mips::COP2RegClassID, Reg);
1263 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1264
1265 Inst.addOperand(MCOperand::CreateReg(Reg));
1266 Inst.addOperand(MCOperand::CreateReg(Base));
1267 Inst.addOperand(MCOperand::CreateImm(Offset));
1268
1269 return MCDisassembler::Success;
1270}
1271
1272static DecodeStatus DecodeFMem3(MCInst &Inst,
1273 unsigned Insn,
1274 uint64_t Address,
1275 const void *Decoder) {
1276 int Offset = SignExtend32<16>(Insn & 0xffff);
1277 unsigned Reg = fieldFromInstruction(Insn, 16, 5);
1278 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1279
1280 Reg = getReg(Decoder, Mips::COP3RegClassID, Reg);
1281 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1282
1283 Inst.addOperand(MCOperand::CreateReg(Reg));
1284 Inst.addOperand(MCOperand::CreateReg(Base));
1285 Inst.addOperand(MCOperand::CreateImm(Offset));
1286
1287 return MCDisassembler::Success;
1288}
1289
Daniel Sanders6a803f62014-06-16 13:13:03 +00001290static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst,
1291 unsigned Insn,
1292 uint64_t Address,
1293 const void *Decoder) {
1294 int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff);
1295 unsigned Rt = fieldFromInstruction(Insn, 16, 5);
1296 unsigned Base = fieldFromInstruction(Insn, 21, 5);
1297
1298 Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt);
1299 Base = getReg(Decoder, Mips::GPR32RegClassID, Base);
1300
1301 if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){
1302 Inst.addOperand(MCOperand::CreateReg(Rt));
1303 }
1304
1305 Inst.addOperand(MCOperand::CreateReg(Rt));
1306 Inst.addOperand(MCOperand::CreateReg(Base));
1307 Inst.addOperand(MCOperand::CreateImm(Offset));
1308
1309 return MCDisassembler::Success;
1310}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001311
1312static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst,
1313 unsigned RegNo,
1314 uint64_t Address,
1315 const void *Decoder) {
1316 // Currently only hardware register 29 is supported.
1317 if (RegNo != 29)
1318 return MCDisassembler::Fail;
1319 Inst.addOperand(MCOperand::CreateReg(Mips::HWR29));
1320 return MCDisassembler::Success;
1321}
1322
Akira Hatanaka71928e62012-04-17 18:03:21 +00001323static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst,
1324 unsigned RegNo,
1325 uint64_t Address,
1326 const void *Decoder) {
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001327 if (RegNo > 30 || RegNo %2)
Akira Hatanaka71928e62012-04-17 18:03:21 +00001328 return MCDisassembler::Fail;
1329
Akira Hatanaka9bf2b562012-07-09 18:46:47 +00001330 ;
1331 unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2);
1332 Inst.addOperand(MCOperand::CreateReg(Reg));
Akira Hatanaka71928e62012-04-17 18:03:21 +00001333 return MCDisassembler::Success;
1334}
1335
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001336static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst,
1337 unsigned RegNo,
1338 uint64_t Address,
1339 const void *Decoder) {
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001340 if (RegNo >= 4)
1341 return MCDisassembler::Fail;
1342
Akira Hatanaka00fcf2e2013-08-08 21:54:26 +00001343 unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo);
Akira Hatanakaecabd1a2012-09-27 02:01:10 +00001344 Inst.addOperand(MCOperand::CreateReg(Reg));
1345 return MCDisassembler::Success;
1346}
1347
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001348static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst,
1349 unsigned RegNo,
1350 uint64_t Address,
1351 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001352 if (RegNo >= 4)
1353 return MCDisassembler::Fail;
1354
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001355 unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001356 Inst.addOperand(MCOperand::CreateReg(Reg));
1357 return MCDisassembler::Success;
1358}
1359
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001360static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst,
1361 unsigned RegNo,
1362 uint64_t Address,
1363 const void *Decoder) {
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001364 if (RegNo >= 4)
1365 return MCDisassembler::Fail;
1366
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00001367 unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo);
Akira Hatanaka59bfaf72013-04-18 00:52:44 +00001368 Inst.addOperand(MCOperand::CreateReg(Reg));
1369 return MCDisassembler::Success;
1370}
1371
Jack Carter3eb663b2013-09-26 00:09:46 +00001372static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst,
1373 unsigned RegNo,
1374 uint64_t Address,
1375 const void *Decoder) {
1376 if (RegNo > 31)
1377 return MCDisassembler::Fail;
1378
1379 unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo);
1380 Inst.addOperand(MCOperand::CreateReg(Reg));
1381 return MCDisassembler::Success;
1382}
1383
Jack Carter5dc8ac92013-09-25 23:50:44 +00001384static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst,
1385 unsigned RegNo,
1386 uint64_t Address,
1387 const void *Decoder) {
1388 if (RegNo > 31)
1389 return MCDisassembler::Fail;
1390
1391 unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo);
1392 Inst.addOperand(MCOperand::CreateReg(Reg));
1393 return MCDisassembler::Success;
1394}
1395
1396static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst,
1397 unsigned RegNo,
1398 uint64_t Address,
1399 const void *Decoder) {
1400 if (RegNo > 31)
1401 return MCDisassembler::Fail;
1402
1403 unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo);
1404 Inst.addOperand(MCOperand::CreateReg(Reg));
1405 return MCDisassembler::Success;
1406}
1407
1408static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst,
1409 unsigned RegNo,
1410 uint64_t Address,
1411 const void *Decoder) {
1412 if (RegNo > 31)
1413 return MCDisassembler::Fail;
1414
1415 unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo);
1416 Inst.addOperand(MCOperand::CreateReg(Reg));
1417 return MCDisassembler::Success;
1418}
1419
Matheus Almeidaa591fdc2013-10-21 12:26:50 +00001420static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst,
1421 unsigned RegNo,
1422 uint64_t Address,
1423 const void *Decoder) {
1424 if (RegNo > 7)
1425 return MCDisassembler::Fail;
1426
1427 unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo);
1428 Inst.addOperand(MCOperand::CreateReg(Reg));
1429 return MCDisassembler::Success;
1430}
1431
Daniel Sanders2a83d682014-05-21 12:56:39 +00001432static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst,
1433 unsigned RegNo,
1434 uint64_t Address,
1435 const void *Decoder) {
1436 if (RegNo > 31)
1437 return MCDisassembler::Fail;
1438
1439 unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo);
1440 Inst.addOperand(MCOperand::CreateReg(Reg));
1441 return MCDisassembler::Success;
1442}
1443
Akira Hatanaka71928e62012-04-17 18:03:21 +00001444static DecodeStatus DecodeBranchTarget(MCInst &Inst,
1445 unsigned Offset,
1446 uint64_t Address,
1447 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001448 int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001449 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1450 return MCDisassembler::Success;
1451}
1452
Akira Hatanaka71928e62012-04-17 18:03:21 +00001453static DecodeStatus DecodeJumpTarget(MCInst &Inst,
1454 unsigned Insn,
1455 uint64_t Address,
1456 const void *Decoder) {
1457
Jim Grosbachecaef492012-08-14 19:06:05 +00001458 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2;
Akira Hatanaka71928e62012-04-17 18:03:21 +00001459 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1460 return MCDisassembler::Success;
1461}
1462
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001463static DecodeStatus DecodeBranchTarget21(MCInst &Inst,
1464 unsigned Offset,
1465 uint64_t Address,
1466 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001467 int32_t BranchOffset = SignExtend32<21>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001468
1469 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1470 return MCDisassembler::Success;
1471}
1472
1473static DecodeStatus DecodeBranchTarget26(MCInst &Inst,
1474 unsigned Offset,
1475 uint64_t Address,
1476 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001477 int32_t BranchOffset = SignExtend32<26>(Offset) * 4;
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +00001478
1479 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1480 return MCDisassembler::Success;
1481}
1482
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001483static DecodeStatus DecodeBranchTargetMM(MCInst &Inst,
1484 unsigned Offset,
1485 uint64_t Address,
1486 const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001487 int32_t BranchOffset = SignExtend32<16>(Offset) * 2;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +00001488 Inst.addOperand(MCOperand::CreateImm(BranchOffset));
1489 return MCDisassembler::Success;
1490}
1491
Zoran Jovanovic507e0842013-10-29 16:38:59 +00001492static DecodeStatus DecodeJumpTargetMM(MCInst &Inst,
1493 unsigned Insn,
1494 uint64_t Address,
1495 const void *Decoder) {
1496 unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1;
1497 Inst.addOperand(MCOperand::CreateImm(JumpOffset));
1498 return MCDisassembler::Success;
1499}
Akira Hatanaka71928e62012-04-17 18:03:21 +00001500
Jozef Kolekaa2b9272014-11-27 14:41:44 +00001501static DecodeStatus DecodeAddiur2Simm7(MCInst &Inst,
1502 unsigned Value,
1503 uint64_t Address,
1504 const void *Decoder) {
1505 if (Value == 0)
1506 Inst.addOperand(MCOperand::CreateImm(1));
1507 else if (Value == 0x7)
1508 Inst.addOperand(MCOperand::CreateImm(-1));
1509 else
1510 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1511 return MCDisassembler::Success;
1512}
1513
1514static DecodeStatus DecodeUImm6Lsl2(MCInst &Inst,
1515 unsigned Value,
1516 uint64_t Address,
1517 const void *Decoder) {
1518 Inst.addOperand(MCOperand::CreateImm(Value << 2));
1519 return MCDisassembler::Success;
1520}
1521
1522static DecodeStatus DecodeLiSimm7(MCInst &Inst,
1523 unsigned Value,
1524 uint64_t Address,
1525 const void *Decoder) {
1526 if (Value == 0x7F)
1527 Inst.addOperand(MCOperand::CreateImm(-1));
1528 else
1529 Inst.addOperand(MCOperand::CreateImm(Value));
1530 return MCDisassembler::Success;
1531}
1532
1533static DecodeStatus DecodeSimm4(MCInst &Inst,
1534 unsigned Value,
1535 uint64_t Address,
1536 const void *Decoder) {
1537 Inst.addOperand(MCOperand::CreateImm(SignExtend32<4>(Value)));
1538 return MCDisassembler::Success;
1539}
1540
Akira Hatanaka71928e62012-04-17 18:03:21 +00001541static DecodeStatus DecodeSimm16(MCInst &Inst,
1542 unsigned Insn,
1543 uint64_t Address,
1544 const void *Decoder) {
1545 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn)));
1546 return MCDisassembler::Success;
1547}
1548
Matheus Almeida779c5932013-11-18 12:32:49 +00001549static DecodeStatus DecodeLSAImm(MCInst &Inst,
1550 unsigned Insn,
1551 uint64_t Address,
1552 const void *Decoder) {
1553 // We add one to the immediate field as it was encoded as 'imm - 1'.
1554 Inst.addOperand(MCOperand::CreateImm(Insn + 1));
1555 return MCDisassembler::Success;
1556}
1557
Akira Hatanaka71928e62012-04-17 18:03:21 +00001558static DecodeStatus DecodeInsSize(MCInst &Inst,
1559 unsigned Insn,
1560 uint64_t Address,
1561 const void *Decoder) {
1562 // First we need to grab the pos(lsb) from MCInst.
1563 int Pos = Inst.getOperand(2).getImm();
1564 int Size = (int) Insn - Pos + 1;
1565 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1566 return MCDisassembler::Success;
1567}
1568
1569static DecodeStatus DecodeExtSize(MCInst &Inst,
1570 unsigned Insn,
1571 uint64_t Address,
1572 const void *Decoder) {
1573 int Size = (int) Insn + 1;
1574 Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size)));
1575 return MCDisassembler::Success;
1576}
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001577
1578static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn,
1579 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001580 Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4));
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001581 return MCDisassembler::Success;
1582}
Zoran Jovanovic28551422014-06-09 09:49:51 +00001583
1584static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn,
1585 uint64_t Address, const void *Decoder) {
Alexey Samsonovd37bab62014-09-02 17:49:16 +00001586 Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8));
Zoran Jovanovic28551422014-06-09 09:49:51 +00001587 return MCDisassembler::Success;
1588}
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001589
1590static DecodeStatus DecodeRegListOperand(MCInst &Inst,
1591 unsigned Insn,
1592 uint64_t Address,
1593 const void *Decoder) {
1594 unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
1595 Mips::S6, Mips::FP};
1596 unsigned RegNum;
1597
1598 unsigned RegLst = fieldFromInstruction(Insn, 21, 5);
1599 // Empty register lists are not allowed.
1600 if (RegLst == 0)
1601 return MCDisassembler::Fail;
1602
1603 RegNum = RegLst & 0xf;
1604 for (unsigned i = 0; i < RegNum; i++)
1605 Inst.addOperand(MCOperand::CreateReg(Regs[i]));
1606
1607 if (RegLst & 0x10)
1608 Inst.addOperand(MCOperand::CreateReg(Mips::RA));
1609
1610 return MCDisassembler::Success;
1611}