fccbf657057c9619174622b7c9013910483568fa
[oota-llvm.git] / lib / CodeGen / MachOCodeEmitter.cpp
1 //===-- MachOEmitter.cpp - Target-independent Mach-O Emitter 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 #include "MachO.h"
11 #include "MachOWriter.h"
12 #include "MachOCodeEmitter.h"
13 #include "llvm/Constants.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/MachineConstantPool.h"
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/CodeGen/MachineRelocation.h"
19 #include "llvm/Target/TargetAsmInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/Mangler.h"
23 #include "llvm/Support/OutputBuffer.h"
24 #include <vector>
25
26 //===----------------------------------------------------------------------===//
27 //                       MachOCodeEmitter Implementation
28 //===----------------------------------------------------------------------===//
29
30 namespace llvm {
31
32 MachOCodeEmitter::MachOCodeEmitter(MachOWriter &mow, MachOSection &mos) :
33       ObjectCodeEmitter(&mos), MOW(mow), TM(MOW.TM) {
34   is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
35   isLittleEndian = TM.getTargetData()->isLittleEndian();
36   TAI = TM.getTargetAsmInfo();
37 }
38
39 /// startFunction - This callback is invoked when a new machine function is
40 /// about to be emitted.
41
42 void MachOCodeEmitter::startFunction(MachineFunction &MF) {
43   const TargetData *TD = TM.getTargetData();
44   const Function *F = MF.getFunction();
45
46   // Align the output buffer to the appropriate alignment, power of 2.
47   unsigned FnAlign = F->getAlignment();
48   unsigned TDAlign = TD->getPrefTypeAlignment(F->getType());
49   unsigned Align = Log2_32(std::max(FnAlign, TDAlign));
50   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
51
52   // Get the Mach-O Section that this function belongs in.
53   MachOSection *MOS = MOW.getTextSection();
54   
55   // Upgrade the section alignment if required.
56   if (MOS->align < Align) MOS->align = Align;
57
58   MOS->emitAlignment(Align);
59
60   // Create symbol for function entry
61   const GlobalValue *FuncV = MF.getFunction();
62   MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, TAI);
63   FnSym.n_value = getCurrentPCOffset();
64
65   // add it to the symtab.
66   MOW.SymbolTable.push_back(FnSym);
67 }
68
69 /// finishFunction - This callback is invoked after the function is completely
70 /// finished.
71
72 bool MachOCodeEmitter::finishFunction(MachineFunction &MF) {
73     
74   // Get the Mach-O Section that this function belongs in.
75   MachOSection *MOS = MOW.getTextSection();
76
77   // Emit constant pool to appropriate section(s)
78   emitConstantPool(MF.getConstantPool());
79
80   // Emit jump tables to appropriate section
81   emitJumpTables(MF.getJumpTableInfo());
82   
83   // If we have emitted any relocations to function-specific objects such as 
84   // basic blocks, constant pools entries, or jump tables, record their
85   // addresses now so that we can rewrite them with the correct addresses
86   // later.
87   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
88     MachineRelocation &MR = Relocations[i];
89     intptr_t Addr;
90
91     if (MR.isBasicBlock()) {
92       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
93       MR.setConstantVal(MOS->Index);
94       MR.setResultPointer((void*)Addr);
95     } else if (MR.isJumpTableIndex()) {
96       Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
97       MR.setConstantVal(MOW.getJumpTableSection()->Index);
98       MR.setResultPointer((void*)Addr);
99     } else if (MR.isConstantPoolIndex()) {
100       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
101       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
102       MR.setResultPointer((void*)Addr);
103     } else if (MR.isGlobalValue()) {
104       // FIXME: This should be a set or something that uniques
105       MOW.PendingGlobals.push_back(MR.getGlobalValue());
106     } else {
107       assert(0 && "Unhandled relocation type");
108     }
109     MOS->addRelocation(MR);
110   }
111   Relocations.clear();
112
113   // Clear per-function data structures.
114   CPLocations.clear();
115   CPSections.clear();
116   JTLocations.clear();
117   MBBLocations.clear();
118
119   return false;
120 }
121
122 /// emitConstantPool - For each constant pool entry, figure out which section
123 /// the constant should live in, allocate space for it, and emit it to the 
124 /// Section data buffer.
125 void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
126   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
127   if (CP.empty()) return;
128
129   // FIXME: handle PIC codegen
130   assert(TM.getRelocationModel() != Reloc::PIC_ &&
131          "PIC codegen not yet handled for mach-o jump tables!");
132
133   // Although there is no strict necessity that I am aware of, we will do what
134   // gcc for OS X does and put each constant pool entry in a section of constant
135   // objects of a certain size.  That means that float constants go in the
136   // literal4 section, and double objects go in literal8, etc.
137   //
138   // FIXME: revisit this decision if we ever do the "stick everything into one
139   // "giant object for PIC" optimization.
140   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
141     const Type *Ty = CP[i].getType();
142     unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
143
144     MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal);
145     OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
146
147     CPLocations.push_back(Sec->size());
148     CPSections.push_back(Sec->Index);
149
150     // Allocate space in the section for the global.
151     // FIXME: need alignment?
152     // FIXME: share between here and AddSymbolToSection?
153     for (unsigned j = 0; j < Size; ++j)
154       SecDataOut.outbyte(0);
155
156     MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i],
157                          TM.getTargetData(), Sec);
158   }
159 }
160
161 /// emitJumpTables - Emit all the jump tables for a given jump table info
162 /// record to the appropriate section.
163 void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
164   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
165   if (JT.empty()) return;
166
167   // FIXME: handle PIC codegen
168   assert(TM.getRelocationModel() != Reloc::PIC_ &&
169          "PIC codegen not yet handled for mach-o jump tables!");
170
171   MachOSection *Sec = MOW.getJumpTableSection();
172   unsigned TextSecIndex = MOW.getTextSection()->Index;
173   OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
174
175   for (unsigned i = 0, e = JT.size(); i != e; ++i) {
176     // For each jump table, record its offset from the start of the section,
177     // reserve space for the relocations to the MBBs, and add the relocations.
178     const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
179     JTLocations.push_back(Sec->size());
180     for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
181       MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi]));
182       MR.setResultPointer((void *)JTLocations[i]);
183       MR.setConstantVal(TextSecIndex);
184       Sec->addRelocation(MR);
185       SecDataOut.outaddr(0);
186     }
187   }
188 }
189
190 } // end namespace llvm
191