blob: 44d357640039e0cb039577c2b8db3856454bbf6e [file] [log] [blame]
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +00001//===- SparcDisassembler.cpp - Disassembler for Sparc -----------*- 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 Sparc Disassembler.
11//
12//===----------------------------------------------------------------------===//
13
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000014#include "Sparc.h"
15#include "SparcRegisterInfo.h"
16#include "SparcSubtarget.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000017#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000018#include "llvm/MC/MCFixedLenDisassembler.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000019#include "llvm/MC/MCInst.h"
Douglas Katzman9160e782015-04-29 20:30:57 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCAsmInfo.h"
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000022#include "llvm/Support/TargetRegistry.h"
23
24using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "sparc-disassembler"
27
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000028typedef MCDisassembler::DecodeStatus DecodeStatus;
29
30namespace {
31
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000032/// A disassembler class for Sparc.
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000033class SparcDisassembler : public MCDisassembler {
34public:
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000035 SparcDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
36 : MCDisassembler(STI, Ctx) {}
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000037 virtual ~SparcDisassembler() {}
38
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000039 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000040 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000041 raw_ostream &VStream,
42 raw_ostream &CStream) const override;
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000043};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000044}
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000045
46namespace llvm {
Douglas Katzman9160e782015-04-29 20:30:57 +000047extern Target TheSparcTarget, TheSparcV9Target, TheSparcelTarget;
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000048}
49
Douglas Katzman9160e782015-04-29 20:30:57 +000050static MCDisassembler *createSparcDisassembler(const Target &T,
51 const MCSubtargetInfo &STI,
52 MCContext &Ctx) {
Lang Hamesa1bc0f52014-04-15 04:40:56 +000053 return new SparcDisassembler(STI, Ctx);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000054}
55
56
57extern "C" void LLVMInitializeSparcDisassembler() {
58 // Register the disassembler.
59 TargetRegistry::RegisterMCDisassembler(TheSparcTarget,
60 createSparcDisassembler);
61 TargetRegistry::RegisterMCDisassembler(TheSparcV9Target,
62 createSparcDisassembler);
Douglas Katzman9160e782015-04-29 20:30:57 +000063 TargetRegistry::RegisterMCDisassembler(TheSparcelTarget,
64 createSparcDisassembler);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000065}
66
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +000067static const unsigned IntRegDecoderTable[] = {
68 SP::G0, SP::G1, SP::G2, SP::G3,
69 SP::G4, SP::G5, SP::G6, SP::G7,
70 SP::O0, SP::O1, SP::O2, SP::O3,
71 SP::O4, SP::O5, SP::O6, SP::O7,
72 SP::L0, SP::L1, SP::L2, SP::L3,
73 SP::L4, SP::L5, SP::L6, SP::L7,
74 SP::I0, SP::I1, SP::I2, SP::I3,
75 SP::I4, SP::I5, SP::I6, SP::I7 };
76
77static const unsigned FPRegDecoderTable[] = {
78 SP::F0, SP::F1, SP::F2, SP::F3,
79 SP::F4, SP::F5, SP::F6, SP::F7,
80 SP::F8, SP::F9, SP::F10, SP::F11,
81 SP::F12, SP::F13, SP::F14, SP::F15,
82 SP::F16, SP::F17, SP::F18, SP::F19,
83 SP::F20, SP::F21, SP::F22, SP::F23,
84 SP::F24, SP::F25, SP::F26, SP::F27,
85 SP::F28, SP::F29, SP::F30, SP::F31 };
86
87static const unsigned DFPRegDecoderTable[] = {
88 SP::D0, SP::D16, SP::D1, SP::D17,
89 SP::D2, SP::D18, SP::D3, SP::D19,
90 SP::D4, SP::D20, SP::D5, SP::D21,
91 SP::D6, SP::D22, SP::D7, SP::D23,
92 SP::D8, SP::D24, SP::D9, SP::D25,
93 SP::D10, SP::D26, SP::D11, SP::D27,
94 SP::D12, SP::D28, SP::D13, SP::D29,
95 SP::D14, SP::D30, SP::D15, SP::D31 };
96
97static const unsigned QFPRegDecoderTable[] = {
Venkatraman Govindaraju0b9debf2014-01-12 04:34:31 +000098 SP::Q0, SP::Q8, ~0U, ~0U,
99 SP::Q1, SP::Q9, ~0U, ~0U,
100 SP::Q2, SP::Q10, ~0U, ~0U,
101 SP::Q3, SP::Q11, ~0U, ~0U,
102 SP::Q4, SP::Q12, ~0U, ~0U,
103 SP::Q5, SP::Q13, ~0U, ~0U,
104 SP::Q6, SP::Q14, ~0U, ~0U,
105 SP::Q7, SP::Q15, ~0U, ~0U } ;
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000106
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000107static const unsigned FCCRegDecoderTable[] = {
108 SP::FCC0, SP::FCC1, SP::FCC2, SP::FCC3 };
109
James Y Knight807563d2015-05-18 16:29:48 +0000110static const unsigned ASRRegDecoderTable[] = {
111 SP::Y, SP::ASR1, SP::ASR2, SP::ASR3,
112 SP::ASR4, SP::ASR5, SP::ASR6, SP::ASR7,
113 SP::ASR8, SP::ASR9, SP::ASR10, SP::ASR11,
114 SP::ASR12, SP::ASR13, SP::ASR14, SP::ASR15,
115 SP::ASR16, SP::ASR17, SP::ASR18, SP::ASR19,
116 SP::ASR20, SP::ASR21, SP::ASR22, SP::ASR23,
117 SP::ASR24, SP::ASR25, SP::ASR26, SP::ASR27,
118 SP::ASR28, SP::ASR29, SP::ASR30, SP::ASR31};
119
Joerg Sonnenberger726e6242015-10-04 09:11:22 +0000120static const unsigned PRRegDecoderTable[] = {
121 SP::TPC, SP::TNPC, SP::TSTATE, SP::TT, SP::TICK, SP::TBA, SP::PSTATE,
122 SP::TL, SP::PIL, SP::CWP, SP::CANSAVE, SP::CANRESTORE, SP::CLEANWIN,
123 SP::OTHERWIN, SP::WSTATE
124};
125
James Y Knight3994be82015-08-10 19:11:39 +0000126static const uint16_t IntPairDecoderTable[] = {
127 SP::G0_G1, SP::G2_G3, SP::G4_G5, SP::G6_G7,
128 SP::O0_O1, SP::O2_O3, SP::O4_O5, SP::O6_O7,
129 SP::L0_L1, SP::L2_L3, SP::L4_L5, SP::L6_L7,
130 SP::I0_I1, SP::I2_I3, SP::I4_I5, SP::I6_I7,
131};
132
Chris Dewhurst053826a2016-02-27 12:49:59 +0000133static const unsigned CPRegDecoderTable[] = {
134 SP::C0, SP::C1, SP::C2, SP::C3,
135 SP::C4, SP::C5, SP::C6, SP::C7,
136 SP::C8, SP::C9, SP::C10, SP::C11,
137 SP::C12, SP::C13, SP::C14, SP::C15,
138 SP::C16, SP::C17, SP::C18, SP::C19,
139 SP::C20, SP::C21, SP::C22, SP::C23,
140 SP::C24, SP::C25, SP::C26, SP::C27,
141 SP::C28, SP::C29, SP::C30, SP::C31
142};
143
144
145static const uint16_t CPPairDecoderTable[] = {
146 SP::C0_C1, SP::C2_C3, SP::C4_C5, SP::C6_C7,
147 SP::C8_C9, SP::C10_C11, SP::C12_C13, SP::C14_C15,
148 SP::C16_C17, SP::C18_C19, SP::C20_C21, SP::C22_C23,
149 SP::C24_C25, SP::C26_C27, SP::C28_C29, SP::C30_C31
150};
151
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000152static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst,
153 unsigned RegNo,
154 uint64_t Address,
155 const void *Decoder) {
156 if (RegNo > 31)
157 return MCDisassembler::Fail;
158 unsigned Reg = IntRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000159 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000160 return MCDisassembler::Success;
161}
162
163static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst,
164 unsigned RegNo,
165 uint64_t Address,
166 const void *Decoder) {
167 if (RegNo > 31)
168 return MCDisassembler::Fail;
169 unsigned Reg = IntRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000170 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000171 return MCDisassembler::Success;
172}
173
174
175static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst,
176 unsigned RegNo,
177 uint64_t Address,
178 const void *Decoder) {
179 if (RegNo > 31)
180 return MCDisassembler::Fail;
181 unsigned Reg = FPRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000182 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000183 return MCDisassembler::Success;
184}
185
186
187static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst,
188 unsigned RegNo,
189 uint64_t Address,
190 const void *Decoder) {
191 if (RegNo > 31)
192 return MCDisassembler::Fail;
193 unsigned Reg = DFPRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000194 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000195 return MCDisassembler::Success;
196}
197
198
199static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst,
200 unsigned RegNo,
201 uint64_t Address,
202 const void *Decoder) {
203 if (RegNo > 31)
204 return MCDisassembler::Fail;
205
206 unsigned Reg = QFPRegDecoderTable[RegNo];
Venkatraman Govindaraju0b9debf2014-01-12 04:34:31 +0000207 if (Reg == ~0U)
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000208 return MCDisassembler::Fail;
Jim Grosbache9119e42015-05-13 18:37:00 +0000209 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000210 return MCDisassembler::Success;
211}
212
Chris Dewhurst053826a2016-02-27 12:49:59 +0000213static DecodeStatus DecodeCPRegsRegisterClass(MCInst &Inst,
214 unsigned RegNo,
215 uint64_t Address,
216 const void *Decoder) {
217 if (RegNo > 31)
218 return MCDisassembler::Fail;
219 unsigned Reg = CPRegDecoderTable[RegNo];
220 Inst.addOperand(MCOperand::createReg(Reg));
221 return MCDisassembler::Success;
222}
223
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000224static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
225 uint64_t Address,
226 const void *Decoder) {
227 if (RegNo > 3)
228 return MCDisassembler::Fail;
Jim Grosbache9119e42015-05-13 18:37:00 +0000229 Inst.addOperand(MCOperand::createReg(FCCRegDecoderTable[RegNo]));
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000230 return MCDisassembler::Success;
231}
232
James Y Knight807563d2015-05-18 16:29:48 +0000233static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
234 uint64_t Address,
235 const void *Decoder) {
236 if (RegNo > 31)
237 return MCDisassembler::Fail;
238 Inst.addOperand(MCOperand::createReg(ASRRegDecoderTable[RegNo]));
239 return MCDisassembler::Success;
240}
241
Joerg Sonnenberger726e6242015-10-04 09:11:22 +0000242static DecodeStatus DecodePRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
243 uint64_t Address,
244 const void *Decoder) {
245 if (RegNo >= array_lengthof(PRRegDecoderTable))
246 return MCDisassembler::Fail;
247 Inst.addOperand(MCOperand::createReg(PRRegDecoderTable[RegNo]));
248 return MCDisassembler::Success;
249}
250
James Y Knight3994be82015-08-10 19:11:39 +0000251static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo,
252 uint64_t Address, const void *Decoder) {
253 DecodeStatus S = MCDisassembler::Success;
254
255 if (RegNo > 31)
256 return MCDisassembler::Fail;
257
258 if ((RegNo & 1))
259 S = MCDisassembler::SoftFail;
260
261 unsigned RegisterPair = IntPairDecoderTable[RegNo/2];
262 Inst.addOperand(MCOperand::createReg(RegisterPair));
263 return S;
264}
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000265
Chris Dewhurst053826a2016-02-27 12:49:59 +0000266static DecodeStatus DecodeCPPairRegisterClass(MCInst &Inst, unsigned RegNo,
267 uint64_t Address, const void *Decoder) {
268 if (RegNo > 31)
269 return MCDisassembler::Fail;
270
271 unsigned RegisterPair = CPPairDecoderTable[RegNo/2];
272 Inst.addOperand(MCOperand::createReg(RegisterPair));
273 return MCDisassembler::Success;
274}
275
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000276static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
277 const void *Decoder);
James Y Knight3994be82015-08-10 19:11:39 +0000278static DecodeStatus DecodeLoadIntPair(MCInst &Inst, unsigned insn, uint64_t Address,
279 const void *Decoder);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000280static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
281 const void *Decoder);
282static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
283 const void *Decoder);
284static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
285 const void *Decoder);
Chris Dewhurst053826a2016-02-27 12:49:59 +0000286static DecodeStatus DecodeLoadCP(MCInst &Inst, unsigned insn, uint64_t Address,
287 const void *Decoder);
288static DecodeStatus DecodeLoadCPPair(MCInst &Inst, unsigned insn, uint64_t Address,
289 const void *Decoder);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000290static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
291 uint64_t Address, const void *Decoder);
James Y Knight3994be82015-08-10 19:11:39 +0000292static DecodeStatus DecodeStoreIntPair(MCInst &Inst, unsigned insn,
293 uint64_t Address, const void *Decoder);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000294static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn,
295 uint64_t Address, const void *Decoder);
296static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
297 uint64_t Address, const void *Decoder);
298static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
299 uint64_t Address, const void *Decoder);
Chris Dewhurst053826a2016-02-27 12:49:59 +0000300static DecodeStatus DecodeStoreCP(MCInst &Inst, unsigned insn,
301 uint64_t Address, const void *Decoder);
302static DecodeStatus DecodeStoreCPPair(MCInst &Inst, unsigned insn,
303 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000304static DecodeStatus DecodeCall(MCInst &Inst, unsigned insn,
305 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000306static DecodeStatus DecodeSIMM13(MCInst &Inst, unsigned insn,
307 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000308static DecodeStatus DecodeJMPL(MCInst &Inst, unsigned insn, uint64_t Address,
309 const void *Decoder);
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000310static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
311 const void *Decoder);
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000312static DecodeStatus DecodeSWAP(MCInst &Inst, unsigned insn, uint64_t Address,
313 const void *Decoder);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000314
315#include "SparcGenDisassemblerTables.inc"
316
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000317/// Read four bytes from the ArrayRef and return 32 bit word.
318static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
Douglas Katzman9160e782015-04-29 20:30:57 +0000319 uint64_t &Size, uint32_t &Insn,
320 bool IsLittleEndian) {
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000321 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000322 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000323 Size = 0;
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000324 return MCDisassembler::Fail;
325 }
326
Douglas Katzman9160e782015-04-29 20:30:57 +0000327 Insn = IsLittleEndian
328 ? (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
329 (Bytes[3] << 24)
330 : (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) |
331 (Bytes[0] << 24);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000332
333 return MCDisassembler::Success;
334}
335
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000336DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000337 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000338 uint64_t Address,
339 raw_ostream &VStream,
340 raw_ostream &CStream) const {
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000341 uint32_t Insn;
Douglas Katzman9160e782015-04-29 20:30:57 +0000342 bool isLittleEndian = getContext().getAsmInfo()->isLittleEndian();
343 DecodeStatus Result =
344 readInstruction32(Bytes, Address, Size, Insn, isLittleEndian);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000345 if (Result == MCDisassembler::Fail)
346 return MCDisassembler::Fail;
347
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000348 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000349 Result =
350 decodeInstruction(DecoderTableSparc32, Instr, Insn, Address, this, STI);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000351
352 if (Result != MCDisassembler::Fail) {
353 Size = 4;
354 return Result;
355 }
356
357 return MCDisassembler::Fail;
358}
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000359
360
361typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
362 const void *Decoder);
363
364static DecodeStatus DecodeMem(MCInst &MI, unsigned insn, uint64_t Address,
365 const void *Decoder,
366 bool isLoad, DecodeFunc DecodeRD) {
367 unsigned rd = fieldFromInstruction(insn, 25, 5);
368 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
369 bool isImm = fieldFromInstruction(insn, 13, 1);
James Y Knight24060be2015-05-18 16:35:04 +0000370 bool hasAsi = fieldFromInstruction(insn, 23, 1); // (in op3 field)
371 unsigned asi = fieldFromInstruction(insn, 5, 8);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000372 unsigned rs2 = 0;
373 unsigned simm13 = 0;
374 if (isImm)
375 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
376 else
377 rs2 = fieldFromInstruction(insn, 0, 5);
378
379 DecodeStatus status;
380 if (isLoad) {
381 status = DecodeRD(MI, rd, Address, Decoder);
382 if (status != MCDisassembler::Success)
383 return status;
384 }
385
386 // Decode rs1.
387 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
388 if (status != MCDisassembler::Success)
389 return status;
390
391 // Decode imm|rs2.
392 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000393 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000394 else {
395 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
396 if (status != MCDisassembler::Success)
397 return status;
398 }
399
James Y Knight24060be2015-05-18 16:35:04 +0000400 if (hasAsi)
401 MI.addOperand(MCOperand::createImm(asi));
402
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000403 if (!isLoad) {
404 status = DecodeRD(MI, rd, Address, Decoder);
405 if (status != MCDisassembler::Success)
406 return status;
407 }
408 return MCDisassembler::Success;
409}
410
411static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
412 const void *Decoder) {
413 return DecodeMem(Inst, insn, Address, Decoder, true,
414 DecodeIntRegsRegisterClass);
415}
416
James Y Knight3994be82015-08-10 19:11:39 +0000417static DecodeStatus DecodeLoadIntPair(MCInst &Inst, unsigned insn, uint64_t Address,
418 const void *Decoder) {
419 return DecodeMem(Inst, insn, Address, Decoder, true,
420 DecodeIntPairRegisterClass);
421}
422
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000423static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
424 const void *Decoder) {
425 return DecodeMem(Inst, insn, Address, Decoder, true,
426 DecodeFPRegsRegisterClass);
427}
428
429static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
430 const void *Decoder) {
431 return DecodeMem(Inst, insn, Address, Decoder, true,
432 DecodeDFPRegsRegisterClass);
433}
434
435static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
436 const void *Decoder) {
437 return DecodeMem(Inst, insn, Address, Decoder, true,
438 DecodeQFPRegsRegisterClass);
439}
440
Chris Dewhurst053826a2016-02-27 12:49:59 +0000441static DecodeStatus DecodeLoadCP(MCInst &Inst, unsigned insn, uint64_t Address,
442 const void *Decoder) {
443 return DecodeMem(Inst, insn, Address, Decoder, true,
444 DecodeCPRegsRegisterClass);
445}
446
447static DecodeStatus DecodeLoadCPPair(MCInst &Inst, unsigned insn, uint64_t Address,
448 const void *Decoder) {
449 return DecodeMem(Inst, insn, Address, Decoder, true,
450 DecodeCPPairRegisterClass);
451}
452
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000453static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
454 uint64_t Address, const void *Decoder) {
455 return DecodeMem(Inst, insn, Address, Decoder, false,
456 DecodeIntRegsRegisterClass);
457}
458
James Y Knight3994be82015-08-10 19:11:39 +0000459static DecodeStatus DecodeStoreIntPair(MCInst &Inst, unsigned insn,
460 uint64_t Address, const void *Decoder) {
461 return DecodeMem(Inst, insn, Address, Decoder, false,
462 DecodeIntPairRegisterClass);
463}
464
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000465static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn, uint64_t Address,
466 const void *Decoder) {
467 return DecodeMem(Inst, insn, Address, Decoder, false,
468 DecodeFPRegsRegisterClass);
469}
470
471static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
472 uint64_t Address, const void *Decoder) {
473 return DecodeMem(Inst, insn, Address, Decoder, false,
474 DecodeDFPRegsRegisterClass);
475}
476
477static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
478 uint64_t Address, const void *Decoder) {
479 return DecodeMem(Inst, insn, Address, Decoder, false,
480 DecodeQFPRegsRegisterClass);
481}
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000482
Chris Dewhurst053826a2016-02-27 12:49:59 +0000483static DecodeStatus DecodeStoreCP(MCInst &Inst, unsigned insn,
484 uint64_t Address, const void *Decoder) {
485 return DecodeMem(Inst, insn, Address, Decoder, false,
486 DecodeCPRegsRegisterClass);
487}
488
489static DecodeStatus DecodeStoreCPPair(MCInst &Inst, unsigned insn,
490 uint64_t Address, const void *Decoder) {
491 return DecodeMem(Inst, insn, Address, Decoder, false,
492 DecodeCPPairRegisterClass);
493}
494
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000495static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
496 uint64_t Address, uint64_t Offset,
497 uint64_t Width, MCInst &MI,
498 const void *Decoder) {
499 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
500 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
501 Offset, Width);
502}
503
504static DecodeStatus DecodeCall(MCInst &MI, unsigned insn,
505 uint64_t Address, const void *Decoder) {
506 unsigned tgt = fieldFromInstruction(insn, 0, 30);
507 tgt <<= 2;
508 if (!tryAddingSymbolicOperand(tgt+Address, false, Address,
509 0, 30, MI, Decoder))
Jim Grosbache9119e42015-05-13 18:37:00 +0000510 MI.addOperand(MCOperand::createImm(tgt));
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000511 return MCDisassembler::Success;
512}
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000513
514static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn,
515 uint64_t Address, const void *Decoder) {
516 unsigned tgt = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
Jim Grosbache9119e42015-05-13 18:37:00 +0000517 MI.addOperand(MCOperand::createImm(tgt));
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000518 return MCDisassembler::Success;
519}
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000520
521static DecodeStatus DecodeJMPL(MCInst &MI, unsigned insn, uint64_t Address,
522 const void *Decoder) {
523
524 unsigned rd = fieldFromInstruction(insn, 25, 5);
525 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
526 unsigned isImm = fieldFromInstruction(insn, 13, 1);
527 unsigned rs2 = 0;
528 unsigned simm13 = 0;
529 if (isImm)
530 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
531 else
532 rs2 = fieldFromInstruction(insn, 0, 5);
533
534 // Decode RD.
535 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
536 if (status != MCDisassembler::Success)
537 return status;
538
539 // Decode RS1.
540 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
541 if (status != MCDisassembler::Success)
542 return status;
543
544 // Decode RS1 | SIMM13.
545 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000546 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000547 else {
548 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
549 if (status != MCDisassembler::Success)
550 return status;
551 }
552 return MCDisassembler::Success;
553}
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000554
555static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
556 const void *Decoder) {
557
558 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
559 unsigned isImm = fieldFromInstruction(insn, 13, 1);
560 unsigned rs2 = 0;
561 unsigned simm13 = 0;
562 if (isImm)
563 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
564 else
565 rs2 = fieldFromInstruction(insn, 0, 5);
566
567 // Decode RS1.
568 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
569 if (status != MCDisassembler::Success)
570 return status;
571
572 // Decode RS2 | SIMM13.
573 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000574 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000575 else {
576 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
577 if (status != MCDisassembler::Success)
578 return status;
579 }
580 return MCDisassembler::Success;
581}
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000582
583static DecodeStatus DecodeSWAP(MCInst &MI, unsigned insn, uint64_t Address,
584 const void *Decoder) {
585
586 unsigned rd = fieldFromInstruction(insn, 25, 5);
587 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
588 unsigned isImm = fieldFromInstruction(insn, 13, 1);
James Y Knight24060be2015-05-18 16:35:04 +0000589 bool hasAsi = fieldFromInstruction(insn, 23, 1); // (in op3 field)
590 unsigned asi = fieldFromInstruction(insn, 5, 8);
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000591 unsigned rs2 = 0;
592 unsigned simm13 = 0;
593 if (isImm)
594 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
595 else
596 rs2 = fieldFromInstruction(insn, 0, 5);
597
598 // Decode RD.
599 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
600 if (status != MCDisassembler::Success)
601 return status;
602
603 // Decode RS1.
604 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
605 if (status != MCDisassembler::Success)
606 return status;
607
608 // Decode RS1 | SIMM13.
609 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000610 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000611 else {
612 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
613 if (status != MCDisassembler::Success)
614 return status;
615 }
James Y Knight24060be2015-05-18 16:35:04 +0000616
617 if (hasAsi)
618 MI.addOperand(MCOperand::createImm(asi));
619
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000620 return MCDisassembler::Success;
621}