71f317abc26411d7c87eea1f91e96263a5de2fbd
[oota-llvm.git] / lib / Target / PowerPC / MCTargetDesc / PPCMCCodeEmitter.cpp
1 //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
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 implements the PPCMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mccodeemitter"
15 #include "MCTargetDesc/PPCMCTargetDesc.h"
16 #include "MCTargetDesc/PPCBaseInfo.h"
17 #include "MCTargetDesc/PPCFixupKinds.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
27
28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
29
30 namespace {
31 class PPCMCCodeEmitter : public MCCodeEmitter {
32   PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33   void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34
35   const MCSubtargetInfo &STI;
36   Triple TT;
37
38 public:
39   PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
40                    MCContext &ctx)
41     : STI(sti), TT(STI.getTargetTriple()) {
42   }
43   
44   ~PPCMCCodeEmitter() {}
45
46   bool is64BitMode() const {
47     return (STI.getFeatureBits() & PPC::Feature64Bit) != 0;
48   }
49
50   bool isSVR4ABI() const {
51     return TT.isMacOSX() == 0;
52   }
53
54   unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
55                                SmallVectorImpl<MCFixup> &Fixups) const;
56   unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
57                              SmallVectorImpl<MCFixup> &Fixups) const;
58   unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
59                            SmallVectorImpl<MCFixup> &Fixups) const;
60   unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
61                            SmallVectorImpl<MCFixup> &Fixups) const;
62   unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
63                             SmallVectorImpl<MCFixup> &Fixups) const;
64   unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
65                              SmallVectorImpl<MCFixup> &Fixups) const;
66   unsigned getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
67                                 SmallVectorImpl<MCFixup> &Fixups) const;
68   unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
69                              SmallVectorImpl<MCFixup> &Fixups) const;
70   unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
71                                SmallVectorImpl<MCFixup> &Fixups) const;
72
73   /// getMachineOpValue - Return binary encoding of operand. If the machine
74   /// operand requires relocation, record the relocation and return zero.
75   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76                              SmallVectorImpl<MCFixup> &Fixups) const;
77   
78   // getBinaryCodeForInstr - TableGen'erated function for getting the
79   // binary encoding for an instruction.
80   uint64_t getBinaryCodeForInstr(const MCInst &MI,
81                                  SmallVectorImpl<MCFixup> &Fixups) const;
82   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
83                          SmallVectorImpl<MCFixup> &Fixups) const {
84     uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
85
86     // BL8_NOP_ELF, BLA8_NOP_ELF, etc., all have a size of 8 because of the
87     // following 'nop'.
88     unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
89     unsigned Opcode = MI.getOpcode();
90     if (Opcode == PPC::BL8_NOP_ELF || Opcode == PPC::BLA8_NOP_ELF ||
91         Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD)
92       Size = 8;
93     
94     // Output the constant in big endian byte order.
95     int ShiftValue = (Size * 8) - 8;
96     for (unsigned i = 0; i != Size; ++i) {
97       OS << (char)(Bits >> ShiftValue);
98       Bits <<= 8;
99     }
100     
101     ++MCNumEmitted;  // Keep track of the # of mi's emitted.
102   }
103   
104 };
105   
106 } // end anonymous namespace
107   
108 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
109                                             const MCRegisterInfo &MRI,
110                                             const MCSubtargetInfo &STI,
111                                             MCContext &Ctx) {
112   return new PPCMCCodeEmitter(MCII, STI, Ctx);
113 }
114
115 unsigned PPCMCCodeEmitter::
116 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
117                     SmallVectorImpl<MCFixup> &Fixups) const {
118   const MCOperand &MO = MI.getOperand(OpNo);
119   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
120   
121   // Add a fixup for the branch target.
122   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
123                                    (MCFixupKind)PPC::fixup_ppc_br24));
124
125   // For special TLS calls, add another fixup for the symbol.  Apparently
126   // BL8_NOP_ELF, BL8_NOP_ELF_TLSGD, and BL8_NOP_ELF_TLSLD are sufficiently
127   // similar that TblGen will not generate a separate case for the latter
128   // two, so this is the only way to get the extra fixup generated.
129   unsigned Opcode = MI.getOpcode();
130   if (Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD) {
131     const MCOperand &MO2 = MI.getOperand(OpNo+1);
132     Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
133                                      (MCFixupKind)PPC::fixup_ppc_nofixup));
134   }
135   return 0;
136 }
137
138 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
139                                      SmallVectorImpl<MCFixup> &Fixups) const {
140   const MCOperand &MO = MI.getOperand(OpNo);
141   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
142
143   // Add a fixup for the branch target.
144   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
145                                    (MCFixupKind)PPC::fixup_ppc_brcond14));
146   return 0;
147 }
148
149 unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
150                                        SmallVectorImpl<MCFixup> &Fixups) const {
151   const MCOperand &MO = MI.getOperand(OpNo);
152   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
153   
154   // Add a fixup for the branch target.
155   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
156                                    (MCFixupKind)PPC::fixup_ppc_ha16));
157   return 0;
158 }
159
160 unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
161                                        SmallVectorImpl<MCFixup> &Fixups) const {
162   const MCOperand &MO = MI.getOperand(OpNo);
163   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
164   
165   // Add a fixup for the branch target.
166   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
167                                    (MCFixupKind)PPC::fixup_ppc_lo16));
168   return 0;
169 }
170
171 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
172                                             SmallVectorImpl<MCFixup> &Fixups) const {
173   // Encode (imm, reg) as a memri, which has the low 16-bits as the
174   // displacement and the next 5 bits as the register #.
175   assert(MI.getOperand(OpNo+1).isReg());
176   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
177   
178   const MCOperand &MO = MI.getOperand(OpNo);
179   if (MO.isImm())
180     return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
181   
182   // Add a fixup for the displacement field.
183   if (isSVR4ABI() && is64BitMode())
184     Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
185                                      (MCFixupKind)PPC::fixup_ppc_toc16));
186   else
187     Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
188                                      (MCFixupKind)PPC::fixup_ppc_lo16));
189   return RegBits;
190 }
191
192
193 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
194                                        SmallVectorImpl<MCFixup> &Fixups) const {
195   // Encode (imm, reg) as a memrix, which has the low 14-bits as the
196   // displacement and the next 5 bits as the register #.
197   assert(MI.getOperand(OpNo+1).isReg());
198   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
199   
200   const MCOperand &MO = MI.getOperand(OpNo);
201   if (MO.isImm())
202     return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
203   
204   // Add a fixup for the branch target.
205   if (isSVR4ABI() && is64BitMode())
206     Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
207                                      (MCFixupKind)PPC::fixup_ppc_toc16_ds));
208   else
209     Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
210                                      (MCFixupKind)PPC::fixup_ppc_lo14));
211   return RegBits;
212 }
213
214
215 unsigned PPCMCCodeEmitter::getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
216                                        SmallVectorImpl<MCFixup> &Fixups) const {
217   const MCOperand &MO = MI.getOperand(OpNo);
218   
219   // Add a fixup for the GOT displacement to the TLS block offset.
220   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
221                                    (MCFixupKind)PPC::fixup_ppc_toc16_ds));
222   return 0;
223 }
224
225
226 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
227                                        SmallVectorImpl<MCFixup> &Fixups) const {
228   const MCOperand &MO = MI.getOperand(OpNo);
229   if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
230   
231   // Add a fixup for the TLS register, which simply provides a relocation
232   // hint to the linker that this statement is part of a relocation sequence.
233   // Return the thread-pointer register's encoding.
234   Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
235                                    (MCFixupKind)PPC::fixup_ppc_tlsreg));
236   return getPPCRegisterNumbering(PPC::X13);
237 }
238
239 unsigned PPCMCCodeEmitter::
240 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
241                     SmallVectorImpl<MCFixup> &Fixups) const {
242   const MCOperand &MO = MI.getOperand(OpNo);
243   assert((MI.getOpcode() == PPC::MTCRF || 
244           MI.getOpcode() == PPC::MFOCRF ||
245           MI.getOpcode() == PPC::MTCRF8) &&
246          (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
247   return 0x80 >> getPPCRegisterNumbering(MO.getReg());
248 }
249
250
251 unsigned PPCMCCodeEmitter::
252 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
253                   SmallVectorImpl<MCFixup> &Fixups) const {
254   if (MO.isReg()) {
255     // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
256     // The GPR operand should come through here though.
257     assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
258            MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
259     return getPPCRegisterNumbering(MO.getReg());
260   }
261   
262   assert(MO.isImm() &&
263          "Relocation required in an instruction that we cannot encode!");
264   return MO.getImm();
265 }
266
267
268 #include "PPCGenMCCodeEmitter.inc"