[Sparc] Add support for decoding jmpl/retl/ret instruction.
[oota-llvm.git] / lib / Target / Sparc / Disassembler / SparcDisassembler.cpp
1 //===- 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
14 #define DEBUG_TYPE "sparc-disassembler"
15
16 #include "Sparc.h"
17 #include "SparcRegisterInfo.h"
18 #include "SparcSubtarget.h"
19 #include "llvm/MC/MCDisassembler.h"
20 #include "llvm/MC/MCFixedLenDisassembler.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
23
24 using namespace llvm;
25
26 typedef MCDisassembler::DecodeStatus DecodeStatus;
27
28 namespace {
29
30 /// SparcDisassembler - a disassembler class for Sparc.
31 class SparcDisassembler : public MCDisassembler {
32 public:
33   /// Constructor     - Initializes the disassembler.
34   ///
35   SparcDisassembler(const MCSubtargetInfo &STI, const MCRegisterInfo *Info) :
36     MCDisassembler(STI), RegInfo(Info)
37   {}
38   virtual ~SparcDisassembler() {}
39
40   const MCRegisterInfo *getRegInfo() const { return RegInfo.get(); }
41
42   /// getInstruction - See MCDisassembler.
43   virtual DecodeStatus getInstruction(MCInst &instr,
44                                       uint64_t &size,
45                                       const MemoryObject &region,
46                                       uint64_t address,
47                                       raw_ostream &vStream,
48                                       raw_ostream &cStream) const;
49 private:
50   OwningPtr<const MCRegisterInfo> RegInfo;
51 };
52
53 }
54
55 namespace llvm {
56   extern Target TheSparcTarget, TheSparcV9Target;
57 }
58
59 static MCDisassembler *createSparcDisassembler(
60                        const Target &T,
61                        const MCSubtargetInfo &STI) {
62   return new SparcDisassembler(STI, T.createMCRegInfo(""));
63 }
64
65
66 extern "C" void LLVMInitializeSparcDisassembler() {
67   // Register the disassembler.
68   TargetRegistry::RegisterMCDisassembler(TheSparcTarget,
69                                          createSparcDisassembler);
70   TargetRegistry::RegisterMCDisassembler(TheSparcV9Target,
71                                          createSparcDisassembler);
72 }
73
74
75
76 static const unsigned IntRegDecoderTable[] = {
77   SP::G0,  SP::G1,  SP::G2,  SP::G3,
78   SP::G4,  SP::G5,  SP::G6,  SP::G7,
79   SP::O0,  SP::O1,  SP::O2,  SP::O3,
80   SP::O4,  SP::O5,  SP::O6,  SP::O7,
81   SP::L0,  SP::L1,  SP::L2,  SP::L3,
82   SP::L4,  SP::L5,  SP::L6,  SP::L7,
83   SP::I0,  SP::I1,  SP::I2,  SP::I3,
84   SP::I4,  SP::I5,  SP::I6,  SP::I7 };
85
86 static const unsigned FPRegDecoderTable[] = {
87   SP::F0,   SP::F1,   SP::F2,   SP::F3,
88   SP::F4,   SP::F5,   SP::F6,   SP::F7,
89   SP::F8,   SP::F9,   SP::F10,  SP::F11,
90   SP::F12,  SP::F13,  SP::F14,  SP::F15,
91   SP::F16,  SP::F17,  SP::F18,  SP::F19,
92   SP::F20,  SP::F21,  SP::F22,  SP::F23,
93   SP::F24,  SP::F25,  SP::F26,  SP::F27,
94   SP::F28,  SP::F29,  SP::F30,  SP::F31 };
95
96 static const unsigned DFPRegDecoderTable[] = {
97   SP::D0,   SP::D16,  SP::D1,   SP::D17,
98   SP::D2,   SP::D18,  SP::D3,   SP::D19,
99   SP::D4,   SP::D20,  SP::D5,   SP::D21,
100   SP::D6,   SP::D22,  SP::D7,   SP::D23,
101   SP::D8,   SP::D24,  SP::D9,   SP::D25,
102   SP::D10,  SP::D26,  SP::D11,  SP::D27,
103   SP::D12,  SP::D28,  SP::D13,  SP::D29,
104   SP::D14,  SP::D30,  SP::D15,  SP::D31 };
105
106 static const unsigned QFPRegDecoderTable[] = {
107   SP::Q0,  SP::Q8,   ~0U,  ~0U,
108   SP::Q1,  SP::Q9,   ~0U,  ~0U,
109   SP::Q2,  SP::Q10,  ~0U,  ~0U,
110   SP::Q3,  SP::Q11,  ~0U,  ~0U,
111   SP::Q4,  SP::Q12,  ~0U,  ~0U,
112   SP::Q5,  SP::Q13,  ~0U,  ~0U,
113   SP::Q6,  SP::Q14,  ~0U,  ~0U,
114   SP::Q7,  SP::Q15,  ~0U,  ~0U } ;
115
116 static const unsigned FCCRegDecoderTable[] = {
117   SP::FCC0, SP::FCC1, SP::FCC2, SP::FCC3 };
118
119 static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst,
120                                                unsigned RegNo,
121                                                uint64_t Address,
122                                                const void *Decoder) {
123   if (RegNo > 31)
124     return MCDisassembler::Fail;
125   unsigned Reg = IntRegDecoderTable[RegNo];
126   Inst.addOperand(MCOperand::CreateReg(Reg));
127   return MCDisassembler::Success;
128 }
129
130 static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst,
131                                                unsigned RegNo,
132                                                uint64_t Address,
133                                                const void *Decoder) {
134   if (RegNo > 31)
135     return MCDisassembler::Fail;
136   unsigned Reg = IntRegDecoderTable[RegNo];
137   Inst.addOperand(MCOperand::CreateReg(Reg));
138   return MCDisassembler::Success;
139 }
140
141
142 static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst,
143                                               unsigned RegNo,
144                                               uint64_t Address,
145                                               const void *Decoder) {
146   if (RegNo > 31)
147     return MCDisassembler::Fail;
148   unsigned Reg = FPRegDecoderTable[RegNo];
149   Inst.addOperand(MCOperand::CreateReg(Reg));
150   return MCDisassembler::Success;
151 }
152
153
154 static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst,
155                                                unsigned RegNo,
156                                                uint64_t Address,
157                                                const void *Decoder) {
158   if (RegNo > 31)
159     return MCDisassembler::Fail;
160   unsigned Reg = DFPRegDecoderTable[RegNo];
161   Inst.addOperand(MCOperand::CreateReg(Reg));
162   return MCDisassembler::Success;
163 }
164
165
166 static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst,
167                                                unsigned RegNo,
168                                                uint64_t Address,
169                                                const void *Decoder) {
170   if (RegNo > 31)
171     return MCDisassembler::Fail;
172
173   unsigned Reg = QFPRegDecoderTable[RegNo];
174   if (Reg == ~0U)
175     return MCDisassembler::Fail;
176   Inst.addOperand(MCOperand::CreateReg(Reg));
177   return MCDisassembler::Success;
178 }
179
180 static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
181                                                uint64_t Address,
182                                                const void *Decoder) {
183   if (RegNo > 3)
184     return MCDisassembler::Fail;
185   Inst.addOperand(MCOperand::CreateReg(FCCRegDecoderTable[RegNo]));
186   return MCDisassembler::Success;
187 }
188
189
190 static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
191                                   const void *Decoder);
192 static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
193                                  const void *Decoder);
194 static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
195                                   const void *Decoder);
196 static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
197                                   const void *Decoder);
198 static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
199                                    uint64_t Address, const void *Decoder);
200 static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn,
201                                   uint64_t Address, const void *Decoder);
202 static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
203                                    uint64_t Address, const void *Decoder);
204 static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
205                                    uint64_t Address, const void *Decoder);
206 static DecodeStatus DecodeCall(MCInst &Inst, unsigned insn,
207                                uint64_t Address, const void *Decoder);
208 static DecodeStatus DecodeSIMM13(MCInst &Inst, unsigned insn,
209                                  uint64_t Address, const void *Decoder);
210 static DecodeStatus DecodeJMPL(MCInst &Inst, unsigned insn, uint64_t Address,
211                                const void *Decoder);
212
213 #include "SparcGenDisassemblerTables.inc"
214
215 /// readInstruction - read four bytes from the MemoryObject
216 /// and return 32 bit word.
217 static DecodeStatus readInstruction32(const MemoryObject &region,
218                                       uint64_t address,
219                                       uint64_t &size,
220                                       uint32_t &insn) {
221   uint8_t Bytes[4];
222
223   // We want to read exactly 4 Bytes of data.
224   if (region.readBytes(address, 4, Bytes) == -1) {
225     size = 0;
226     return MCDisassembler::Fail;
227   }
228
229   // Encoded as a big-endian 32-bit word in the stream.
230   insn = (Bytes[3] <<  0) |
231     (Bytes[2] <<  8) |
232     (Bytes[1] << 16) |
233     (Bytes[0] << 24);
234
235   return MCDisassembler::Success;
236 }
237
238
239 DecodeStatus
240 SparcDisassembler::getInstruction(MCInst &instr,
241                                  uint64_t &Size,
242                                  const MemoryObject &Region,
243                                  uint64_t Address,
244                                  raw_ostream &vStream,
245                                  raw_ostream &cStream) const {
246   uint32_t Insn;
247
248   DecodeStatus Result = readInstruction32(Region, Address, Size, Insn);
249   if (Result == MCDisassembler::Fail)
250     return MCDisassembler::Fail;
251
252
253   // Calling the auto-generated decoder function.
254   Result = decodeInstruction(DecoderTableSparc32, instr, Insn, Address,
255                              this, STI);
256
257   if (Result != MCDisassembler::Fail) {
258     Size = 4;
259     return Result;
260   }
261
262   return MCDisassembler::Fail;
263 }
264
265
266 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
267                                    const void *Decoder);
268
269 static DecodeStatus DecodeMem(MCInst &MI, unsigned insn, uint64_t Address,
270                               const void *Decoder,
271                               bool isLoad, DecodeFunc DecodeRD) {
272   unsigned rd = fieldFromInstruction(insn, 25, 5);
273   unsigned rs1 = fieldFromInstruction(insn, 14, 5);
274   bool isImm = fieldFromInstruction(insn, 13, 1);
275   unsigned rs2 = 0;
276   unsigned simm13 = 0;
277   if (isImm)
278     simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
279   else
280     rs2 = fieldFromInstruction(insn, 0, 5);
281
282   DecodeStatus status;
283   if (isLoad) {
284     status = DecodeRD(MI, rd, Address, Decoder);
285     if (status != MCDisassembler::Success)
286       return status;
287   }
288
289   // Decode rs1.
290   status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
291   if (status != MCDisassembler::Success)
292     return status;
293
294   // Decode imm|rs2.
295   if (isImm)
296     MI.addOperand(MCOperand::CreateImm(simm13));
297   else {
298     status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
299     if (status != MCDisassembler::Success)
300       return status;
301   }
302
303   if (!isLoad) {
304     status = DecodeRD(MI, rd, Address, Decoder);
305     if (status != MCDisassembler::Success)
306       return status;
307   }
308   return MCDisassembler::Success;
309 }
310
311 static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
312                                   const void *Decoder) {
313   return DecodeMem(Inst, insn, Address, Decoder, true,
314                    DecodeIntRegsRegisterClass);
315 }
316
317 static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
318                                  const void *Decoder) {
319   return DecodeMem(Inst, insn, Address, Decoder, true,
320                    DecodeFPRegsRegisterClass);
321 }
322
323 static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
324                                   const void *Decoder) {
325   return DecodeMem(Inst, insn, Address, Decoder, true,
326                    DecodeDFPRegsRegisterClass);
327 }
328
329 static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
330                                   const void *Decoder) {
331   return DecodeMem(Inst, insn, Address, Decoder, true,
332                    DecodeQFPRegsRegisterClass);
333 }
334
335 static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
336                                    uint64_t Address, const void *Decoder) {
337   return DecodeMem(Inst, insn, Address, Decoder, false,
338                    DecodeIntRegsRegisterClass);
339 }
340
341 static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn, uint64_t Address,
342                                   const void *Decoder) {
343   return DecodeMem(Inst, insn, Address, Decoder, false,
344                    DecodeFPRegsRegisterClass);
345 }
346
347 static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
348                                    uint64_t Address, const void *Decoder) {
349   return DecodeMem(Inst, insn, Address, Decoder, false,
350                    DecodeDFPRegsRegisterClass);
351 }
352
353 static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
354                                    uint64_t Address, const void *Decoder) {
355   return DecodeMem(Inst, insn, Address, Decoder, false,
356                    DecodeQFPRegsRegisterClass);
357 }
358
359 static bool tryAddingSymbolicOperand(int64_t Value,  bool isBranch,
360                                      uint64_t Address, uint64_t Offset,
361                                      uint64_t Width, MCInst &MI,
362                                      const void *Decoder) {
363   const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
364   return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
365                                        Offset, Width);
366 }
367
368 static DecodeStatus DecodeCall(MCInst &MI, unsigned insn,
369                                uint64_t Address, const void *Decoder) {
370   unsigned tgt = fieldFromInstruction(insn, 0, 30);
371   tgt <<= 2;
372   if (!tryAddingSymbolicOperand(tgt+Address, false, Address,
373                                 0, 30, MI, Decoder))
374     MI.addOperand(MCOperand::CreateImm(tgt));
375   return MCDisassembler::Success;
376 }
377
378 static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn,
379                                  uint64_t Address, const void *Decoder) {
380   unsigned tgt = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
381   MI.addOperand(MCOperand::CreateImm(tgt));
382   return MCDisassembler::Success;
383 }
384
385 static DecodeStatus DecodeJMPL(MCInst &MI, unsigned insn, uint64_t Address,
386                                const void *Decoder) {
387
388   unsigned rd = fieldFromInstruction(insn, 25, 5);
389   unsigned rs1 = fieldFromInstruction(insn, 14, 5);
390   unsigned isImm = fieldFromInstruction(insn, 13, 1);
391   unsigned rs2 = 0;
392   unsigned simm13 = 0;
393   if (isImm)
394     simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
395   else
396     rs2 = fieldFromInstruction(insn, 0, 5);
397
398   // Decode RD.
399   DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
400   if (status != MCDisassembler::Success)
401     return status;
402
403   // Decode RS1.
404   status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
405   if (status != MCDisassembler::Success)
406     return status;
407
408   // Decode RS1 | SIMM13.
409   if (isImm)
410     MI.addOperand(MCOperand::CreateImm(simm13));
411   else {
412     status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
413     if (status != MCDisassembler::Success)
414       return status;
415   }
416   return MCDisassembler::Success;
417 }