blob: e518fc75c14825f894d89af4ffcf8fd0bf1b0beb [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"
17#include "llvm/MC/MCDisassembler.h"
18#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
James Y Knight3994be82015-08-10 19:11:39 +0000120static const uint16_t IntPairDecoderTable[] = {
121 SP::G0_G1, SP::G2_G3, SP::G4_G5, SP::G6_G7,
122 SP::O0_O1, SP::O2_O3, SP::O4_O5, SP::O6_O7,
123 SP::L0_L1, SP::L2_L3, SP::L4_L5, SP::L6_L7,
124 SP::I0_I1, SP::I2_I3, SP::I4_I5, SP::I6_I7,
125};
126
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000127static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst,
128 unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder) {
131 if (RegNo > 31)
132 return MCDisassembler::Fail;
133 unsigned Reg = IntRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000134 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000135 return MCDisassembler::Success;
136}
137
138static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst,
139 unsigned RegNo,
140 uint64_t Address,
141 const void *Decoder) {
142 if (RegNo > 31)
143 return MCDisassembler::Fail;
144 unsigned Reg = IntRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000145 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000146 return MCDisassembler::Success;
147}
148
149
150static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst,
151 unsigned RegNo,
152 uint64_t Address,
153 const void *Decoder) {
154 if (RegNo > 31)
155 return MCDisassembler::Fail;
156 unsigned Reg = FPRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000157 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000158 return MCDisassembler::Success;
159}
160
161
162static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst,
163 unsigned RegNo,
164 uint64_t Address,
165 const void *Decoder) {
166 if (RegNo > 31)
167 return MCDisassembler::Fail;
168 unsigned Reg = DFPRegDecoderTable[RegNo];
Jim Grosbache9119e42015-05-13 18:37:00 +0000169 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000170 return MCDisassembler::Success;
171}
172
173
174static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst,
175 unsigned RegNo,
176 uint64_t Address,
177 const void *Decoder) {
178 if (RegNo > 31)
179 return MCDisassembler::Fail;
180
181 unsigned Reg = QFPRegDecoderTable[RegNo];
Venkatraman Govindaraju0b9debf2014-01-12 04:34:31 +0000182 if (Reg == ~0U)
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000183 return MCDisassembler::Fail;
Jim Grosbache9119e42015-05-13 18:37:00 +0000184 Inst.addOperand(MCOperand::createReg(Reg));
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000185 return MCDisassembler::Success;
186}
187
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000188static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
189 uint64_t Address,
190 const void *Decoder) {
191 if (RegNo > 3)
192 return MCDisassembler::Fail;
Jim Grosbache9119e42015-05-13 18:37:00 +0000193 Inst.addOperand(MCOperand::createReg(FCCRegDecoderTable[RegNo]));
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000194 return MCDisassembler::Success;
195}
196
James Y Knight807563d2015-05-18 16:29:48 +0000197static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
198 uint64_t Address,
199 const void *Decoder) {
200 if (RegNo > 31)
201 return MCDisassembler::Fail;
202 Inst.addOperand(MCOperand::createReg(ASRRegDecoderTable[RegNo]));
203 return MCDisassembler::Success;
204}
205
James Y Knight3994be82015-08-10 19:11:39 +0000206static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo,
207 uint64_t Address, const void *Decoder) {
208 DecodeStatus S = MCDisassembler::Success;
209
210 if (RegNo > 31)
211 return MCDisassembler::Fail;
212
213 if ((RegNo & 1))
214 S = MCDisassembler::SoftFail;
215
216 unsigned RegisterPair = IntPairDecoderTable[RegNo/2];
217 Inst.addOperand(MCOperand::createReg(RegisterPair));
218 return S;
219}
Venkatraman Govindaraju81aae572014-03-02 03:39:39 +0000220
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000221static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
222 const void *Decoder);
James Y Knight3994be82015-08-10 19:11:39 +0000223static DecodeStatus DecodeLoadIntPair(MCInst &Inst, unsigned insn, uint64_t Address,
224 const void *Decoder);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000225static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
226 const void *Decoder);
227static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
228 const void *Decoder);
229static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
230 const void *Decoder);
231static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
232 uint64_t Address, const void *Decoder);
James Y Knight3994be82015-08-10 19:11:39 +0000233static DecodeStatus DecodeStoreIntPair(MCInst &Inst, unsigned insn,
234 uint64_t Address, const void *Decoder);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000235static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn,
236 uint64_t Address, const void *Decoder);
237static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
238 uint64_t Address, const void *Decoder);
239static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
240 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000241static DecodeStatus DecodeCall(MCInst &Inst, unsigned insn,
242 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000243static DecodeStatus DecodeSIMM13(MCInst &Inst, unsigned insn,
244 uint64_t Address, const void *Decoder);
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000245static DecodeStatus DecodeJMPL(MCInst &Inst, unsigned insn, uint64_t Address,
246 const void *Decoder);
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000247static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
248 const void *Decoder);
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000249static DecodeStatus DecodeSWAP(MCInst &Inst, unsigned insn, uint64_t Address,
250 const void *Decoder);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000251
252#include "SparcGenDisassemblerTables.inc"
253
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000254/// Read four bytes from the ArrayRef and return 32 bit word.
255static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address,
Douglas Katzman9160e782015-04-29 20:30:57 +0000256 uint64_t &Size, uint32_t &Insn,
257 bool IsLittleEndian) {
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000258 // We want to read exactly 4 Bytes of data.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000259 if (Bytes.size() < 4) {
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000260 Size = 0;
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000261 return MCDisassembler::Fail;
262 }
263
Douglas Katzman9160e782015-04-29 20:30:57 +0000264 Insn = IsLittleEndian
265 ? (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
266 (Bytes[3] << 24)
267 : (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) |
268 (Bytes[0] << 24);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000269
270 return MCDisassembler::Success;
271}
272
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000273DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000274 ArrayRef<uint8_t> Bytes,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000275 uint64_t Address,
276 raw_ostream &VStream,
277 raw_ostream &CStream) const {
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000278 uint32_t Insn;
Douglas Katzman9160e782015-04-29 20:30:57 +0000279 bool isLittleEndian = getContext().getAsmInfo()->isLittleEndian();
280 DecodeStatus Result =
281 readInstruction32(Bytes, Address, Size, Insn, isLittleEndian);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000282 if (Result == MCDisassembler::Fail)
283 return MCDisassembler::Fail;
284
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000285 // Calling the auto-generated decoder function.
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000286 Result =
287 decodeInstruction(DecoderTableSparc32, Instr, Insn, Address, this, STI);
Venkatraman Govindarajudfcccc72014-01-06 08:08:58 +0000288
289 if (Result != MCDisassembler::Fail) {
290 Size = 4;
291 return Result;
292 }
293
294 return MCDisassembler::Fail;
295}
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000296
297
298typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
299 const void *Decoder);
300
301static DecodeStatus DecodeMem(MCInst &MI, unsigned insn, uint64_t Address,
302 const void *Decoder,
303 bool isLoad, DecodeFunc DecodeRD) {
304 unsigned rd = fieldFromInstruction(insn, 25, 5);
305 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
306 bool isImm = fieldFromInstruction(insn, 13, 1);
James Y Knight24060be2015-05-18 16:35:04 +0000307 bool hasAsi = fieldFromInstruction(insn, 23, 1); // (in op3 field)
308 unsigned asi = fieldFromInstruction(insn, 5, 8);
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000309 unsigned rs2 = 0;
310 unsigned simm13 = 0;
311 if (isImm)
312 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
313 else
314 rs2 = fieldFromInstruction(insn, 0, 5);
315
316 DecodeStatus status;
317 if (isLoad) {
318 status = DecodeRD(MI, rd, Address, Decoder);
319 if (status != MCDisassembler::Success)
320 return status;
321 }
322
323 // Decode rs1.
324 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
325 if (status != MCDisassembler::Success)
326 return status;
327
328 // Decode imm|rs2.
329 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000330 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000331 else {
332 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
333 if (status != MCDisassembler::Success)
334 return status;
335 }
336
James Y Knight24060be2015-05-18 16:35:04 +0000337 if (hasAsi)
338 MI.addOperand(MCOperand::createImm(asi));
339
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000340 if (!isLoad) {
341 status = DecodeRD(MI, rd, Address, Decoder);
342 if (status != MCDisassembler::Success)
343 return status;
344 }
345 return MCDisassembler::Success;
346}
347
348static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
349 const void *Decoder) {
350 return DecodeMem(Inst, insn, Address, Decoder, true,
351 DecodeIntRegsRegisterClass);
352}
353
James Y Knight3994be82015-08-10 19:11:39 +0000354static DecodeStatus DecodeLoadIntPair(MCInst &Inst, unsigned insn, uint64_t Address,
355 const void *Decoder) {
356 return DecodeMem(Inst, insn, Address, Decoder, true,
357 DecodeIntPairRegisterClass);
358}
359
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000360static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
361 const void *Decoder) {
362 return DecodeMem(Inst, insn, Address, Decoder, true,
363 DecodeFPRegsRegisterClass);
364}
365
366static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
367 const void *Decoder) {
368 return DecodeMem(Inst, insn, Address, Decoder, true,
369 DecodeDFPRegsRegisterClass);
370}
371
372static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
373 const void *Decoder) {
374 return DecodeMem(Inst, insn, Address, Decoder, true,
375 DecodeQFPRegsRegisterClass);
376}
377
378static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
379 uint64_t Address, const void *Decoder) {
380 return DecodeMem(Inst, insn, Address, Decoder, false,
381 DecodeIntRegsRegisterClass);
382}
383
James Y Knight3994be82015-08-10 19:11:39 +0000384static DecodeStatus DecodeStoreIntPair(MCInst &Inst, unsigned insn,
385 uint64_t Address, const void *Decoder) {
386 return DecodeMem(Inst, insn, Address, Decoder, false,
387 DecodeIntPairRegisterClass);
388}
389
Venkatraman Govindarajufb548212014-03-01 07:46:33 +0000390static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn, uint64_t Address,
391 const void *Decoder) {
392 return DecodeMem(Inst, insn, Address, Decoder, false,
393 DecodeFPRegsRegisterClass);
394}
395
396static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
397 uint64_t Address, const void *Decoder) {
398 return DecodeMem(Inst, insn, Address, Decoder, false,
399 DecodeDFPRegsRegisterClass);
400}
401
402static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
403 uint64_t Address, const void *Decoder) {
404 return DecodeMem(Inst, insn, Address, Decoder, false,
405 DecodeQFPRegsRegisterClass);
406}
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000407
408static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
409 uint64_t Address, uint64_t Offset,
410 uint64_t Width, MCInst &MI,
411 const void *Decoder) {
412 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
413 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
414 Offset, Width);
415}
416
417static DecodeStatus DecodeCall(MCInst &MI, unsigned insn,
418 uint64_t Address, const void *Decoder) {
419 unsigned tgt = fieldFromInstruction(insn, 0, 30);
420 tgt <<= 2;
421 if (!tryAddingSymbolicOperand(tgt+Address, false, Address,
422 0, 30, MI, Decoder))
Jim Grosbache9119e42015-05-13 18:37:00 +0000423 MI.addOperand(MCOperand::createImm(tgt));
Venkatraman Govindaraju78df2de2014-03-01 08:30:58 +0000424 return MCDisassembler::Success;
425}
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000426
427static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn,
428 uint64_t Address, const void *Decoder) {
429 unsigned tgt = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
Jim Grosbache9119e42015-05-13 18:37:00 +0000430 MI.addOperand(MCOperand::createImm(tgt));
Venkatraman Govindaraju484ca1a2014-03-01 09:11:57 +0000431 return MCDisassembler::Success;
432}
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000433
434static DecodeStatus DecodeJMPL(MCInst &MI, unsigned insn, uint64_t Address,
435 const void *Decoder) {
436
437 unsigned rd = fieldFromInstruction(insn, 25, 5);
438 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
439 unsigned isImm = fieldFromInstruction(insn, 13, 1);
440 unsigned rs2 = 0;
441 unsigned simm13 = 0;
442 if (isImm)
443 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
444 else
445 rs2 = fieldFromInstruction(insn, 0, 5);
446
447 // Decode RD.
448 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
449 if (status != MCDisassembler::Success)
450 return status;
451
452 // Decode RS1.
453 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
454 if (status != MCDisassembler::Success)
455 return status;
456
457 // Decode RS1 | SIMM13.
458 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000459 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindaraju4fa2ab22014-03-02 21:17:44 +0000460 else {
461 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
462 if (status != MCDisassembler::Success)
463 return status;
464 }
465 return MCDisassembler::Success;
466}
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000467
468static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
469 const void *Decoder) {
470
471 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
472 unsigned isImm = fieldFromInstruction(insn, 13, 1);
473 unsigned rs2 = 0;
474 unsigned simm13 = 0;
475 if (isImm)
476 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
477 else
478 rs2 = fieldFromInstruction(insn, 0, 5);
479
480 // Decode RS1.
481 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
482 if (status != MCDisassembler::Success)
483 return status;
484
485 // Decode RS2 | SIMM13.
486 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000487 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindaraju07d3af22014-03-02 22:55:53 +0000488 else {
489 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
490 if (status != MCDisassembler::Success)
491 return status;
492 }
493 return MCDisassembler::Success;
494}
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000495
496static DecodeStatus DecodeSWAP(MCInst &MI, unsigned insn, uint64_t Address,
497 const void *Decoder) {
498
499 unsigned rd = fieldFromInstruction(insn, 25, 5);
500 unsigned rs1 = fieldFromInstruction(insn, 14, 5);
501 unsigned isImm = fieldFromInstruction(insn, 13, 1);
James Y Knight24060be2015-05-18 16:35:04 +0000502 bool hasAsi = fieldFromInstruction(insn, 23, 1); // (in op3 field)
503 unsigned asi = fieldFromInstruction(insn, 5, 8);
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000504 unsigned rs2 = 0;
505 unsigned simm13 = 0;
506 if (isImm)
507 simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
508 else
509 rs2 = fieldFromInstruction(insn, 0, 5);
510
511 // Decode RD.
512 DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
513 if (status != MCDisassembler::Success)
514 return status;
515
516 // Decode RS1.
517 status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
518 if (status != MCDisassembler::Success)
519 return status;
520
521 // Decode RS1 | SIMM13.
522 if (isImm)
Jim Grosbache9119e42015-05-13 18:37:00 +0000523 MI.addOperand(MCOperand::createImm(simm13));
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000524 else {
525 status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
526 if (status != MCDisassembler::Success)
527 return status;
528 }
James Y Knight24060be2015-05-18 16:35:04 +0000529
530 if (hasAsi)
531 MI.addOperand(MCOperand::createImm(asi));
532
Venkatraman Govindarajuf7031322014-03-09 23:32:07 +0000533 return MCDisassembler::Success;
534}