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