[Sparc] Add return/rett instruction to Sparc backend.
[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 static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
213                                  const void *Decoder);
214
215 #include "SparcGenDisassemblerTables.inc"
216
217 /// readInstruction - read four bytes from the MemoryObject
218 /// and return 32 bit word.
219 static DecodeStatus readInstruction32(const MemoryObject &region,
220                                       uint64_t address,
221                                       uint64_t &size,
222                                       uint32_t &insn) {
223   uint8_t Bytes[4];
224
225   // We want to read exactly 4 Bytes of data.
226   if (region.readBytes(address, 4, Bytes) == -1) {
227     size = 0;
228     return MCDisassembler::Fail;
229   }
230
231   // Encoded as a big-endian 32-bit word in the stream.
232   insn = (Bytes[3] <<  0) |
233     (Bytes[2] <<  8) |
234     (Bytes[1] << 16) |
235     (Bytes[0] << 24);
236
237   return MCDisassembler::Success;
238 }
239
240
241 DecodeStatus
242 SparcDisassembler::getInstruction(MCInst &instr,
243                                  uint64_t &Size,
244                                  const MemoryObject &Region,
245                                  uint64_t Address,
246                                  raw_ostream &vStream,
247                                  raw_ostream &cStream) const {
248   uint32_t Insn;
249
250   DecodeStatus Result = readInstruction32(Region, Address, Size, Insn);
251   if (Result == MCDisassembler::Fail)
252     return MCDisassembler::Fail;
253
254
255   // Calling the auto-generated decoder function.
256   Result = decodeInstruction(DecoderTableSparc32, instr, Insn, Address,
257                              this, STI);
258
259   if (Result != MCDisassembler::Fail) {
260     Size = 4;
261     return Result;
262   }
263
264   return MCDisassembler::Fail;
265 }
266
267
268 typedef DecodeStatus (*DecodeFunc)(MCInst &MI, unsigned insn, uint64_t Address,
269                                    const void *Decoder);
270
271 static DecodeStatus DecodeMem(MCInst &MI, unsigned insn, uint64_t Address,
272                               const void *Decoder,
273                               bool isLoad, DecodeFunc DecodeRD) {
274   unsigned rd = fieldFromInstruction(insn, 25, 5);
275   unsigned rs1 = fieldFromInstruction(insn, 14, 5);
276   bool isImm = fieldFromInstruction(insn, 13, 1);
277   unsigned rs2 = 0;
278   unsigned simm13 = 0;
279   if (isImm)
280     simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
281   else
282     rs2 = fieldFromInstruction(insn, 0, 5);
283
284   DecodeStatus status;
285   if (isLoad) {
286     status = DecodeRD(MI, rd, Address, Decoder);
287     if (status != MCDisassembler::Success)
288       return status;
289   }
290
291   // Decode rs1.
292   status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
293   if (status != MCDisassembler::Success)
294     return status;
295
296   // Decode imm|rs2.
297   if (isImm)
298     MI.addOperand(MCOperand::CreateImm(simm13));
299   else {
300     status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
301     if (status != MCDisassembler::Success)
302       return status;
303   }
304
305   if (!isLoad) {
306     status = DecodeRD(MI, rd, Address, Decoder);
307     if (status != MCDisassembler::Success)
308       return status;
309   }
310   return MCDisassembler::Success;
311 }
312
313 static DecodeStatus DecodeLoadInt(MCInst &Inst, unsigned insn, uint64_t Address,
314                                   const void *Decoder) {
315   return DecodeMem(Inst, insn, Address, Decoder, true,
316                    DecodeIntRegsRegisterClass);
317 }
318
319 static DecodeStatus DecodeLoadFP(MCInst &Inst, unsigned insn, uint64_t Address,
320                                  const void *Decoder) {
321   return DecodeMem(Inst, insn, Address, Decoder, true,
322                    DecodeFPRegsRegisterClass);
323 }
324
325 static DecodeStatus DecodeLoadDFP(MCInst &Inst, unsigned insn, uint64_t Address,
326                                   const void *Decoder) {
327   return DecodeMem(Inst, insn, Address, Decoder, true,
328                    DecodeDFPRegsRegisterClass);
329 }
330
331 static DecodeStatus DecodeLoadQFP(MCInst &Inst, unsigned insn, uint64_t Address,
332                                   const void *Decoder) {
333   return DecodeMem(Inst, insn, Address, Decoder, true,
334                    DecodeQFPRegsRegisterClass);
335 }
336
337 static DecodeStatus DecodeStoreInt(MCInst &Inst, unsigned insn,
338                                    uint64_t Address, const void *Decoder) {
339   return DecodeMem(Inst, insn, Address, Decoder, false,
340                    DecodeIntRegsRegisterClass);
341 }
342
343 static DecodeStatus DecodeStoreFP(MCInst &Inst, unsigned insn, uint64_t Address,
344                                   const void *Decoder) {
345   return DecodeMem(Inst, insn, Address, Decoder, false,
346                    DecodeFPRegsRegisterClass);
347 }
348
349 static DecodeStatus DecodeStoreDFP(MCInst &Inst, unsigned insn,
350                                    uint64_t Address, const void *Decoder) {
351   return DecodeMem(Inst, insn, Address, Decoder, false,
352                    DecodeDFPRegsRegisterClass);
353 }
354
355 static DecodeStatus DecodeStoreQFP(MCInst &Inst, unsigned insn,
356                                    uint64_t Address, const void *Decoder) {
357   return DecodeMem(Inst, insn, Address, Decoder, false,
358                    DecodeQFPRegsRegisterClass);
359 }
360
361 static bool tryAddingSymbolicOperand(int64_t Value,  bool isBranch,
362                                      uint64_t Address, uint64_t Offset,
363                                      uint64_t Width, MCInst &MI,
364                                      const void *Decoder) {
365   const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
366   return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
367                                        Offset, Width);
368 }
369
370 static DecodeStatus DecodeCall(MCInst &MI, unsigned insn,
371                                uint64_t Address, const void *Decoder) {
372   unsigned tgt = fieldFromInstruction(insn, 0, 30);
373   tgt <<= 2;
374   if (!tryAddingSymbolicOperand(tgt+Address, false, Address,
375                                 0, 30, MI, Decoder))
376     MI.addOperand(MCOperand::CreateImm(tgt));
377   return MCDisassembler::Success;
378 }
379
380 static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn,
381                                  uint64_t Address, const void *Decoder) {
382   unsigned tgt = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
383   MI.addOperand(MCOperand::CreateImm(tgt));
384   return MCDisassembler::Success;
385 }
386
387 static DecodeStatus DecodeJMPL(MCInst &MI, unsigned insn, uint64_t Address,
388                                const void *Decoder) {
389
390   unsigned rd = fieldFromInstruction(insn, 25, 5);
391   unsigned rs1 = fieldFromInstruction(insn, 14, 5);
392   unsigned isImm = fieldFromInstruction(insn, 13, 1);
393   unsigned rs2 = 0;
394   unsigned simm13 = 0;
395   if (isImm)
396     simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
397   else
398     rs2 = fieldFromInstruction(insn, 0, 5);
399
400   // Decode RD.
401   DecodeStatus status = DecodeIntRegsRegisterClass(MI, rd, Address, Decoder);
402   if (status != MCDisassembler::Success)
403     return status;
404
405   // Decode RS1.
406   status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
407   if (status != MCDisassembler::Success)
408     return status;
409
410   // Decode RS1 | SIMM13.
411   if (isImm)
412     MI.addOperand(MCOperand::CreateImm(simm13));
413   else {
414     status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
415     if (status != MCDisassembler::Success)
416       return status;
417   }
418   return MCDisassembler::Success;
419 }
420
421 static DecodeStatus DecodeReturn(MCInst &MI, unsigned insn, uint64_t Address,
422                                  const void *Decoder) {
423
424   unsigned rs1 = fieldFromInstruction(insn, 14, 5);
425   unsigned isImm = fieldFromInstruction(insn, 13, 1);
426   unsigned rs2 = 0;
427   unsigned simm13 = 0;
428   if (isImm)
429     simm13 = SignExtend32<13>(fieldFromInstruction(insn, 0, 13));
430   else
431     rs2 = fieldFromInstruction(insn, 0, 5);
432
433   // Decode RS1.
434   DecodeStatus status = DecodeIntRegsRegisterClass(MI, rs1, Address, Decoder);
435   if (status != MCDisassembler::Success)
436     return status;
437
438   // Decode RS2 | SIMM13.
439   if (isImm)
440     MI.addOperand(MCOperand::CreateImm(simm13));
441   else {
442     status = DecodeIntRegsRegisterClass(MI, rs2, Address, Decoder);
443     if (status != MCDisassembler::Success)
444       return status;
445   }
446   return MCDisassembler::Success;
447 }