blob: fccbf657057c9619174622b7c9013910483568fa [file] [log] [blame]
Bruno Cardoso Lopesa321dcd2009-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
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000010#include "MachO.h"
11#include "MachOWriter.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000012#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"
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000018#include "llvm/CodeGen/MachineRelocation.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000019#include "llvm/Target/TargetAsmInfo.h"
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000020#include "llvm/Target/TargetData.h"
21#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000022#include "llvm/Support/Mangler.h"
23#include "llvm/Support/OutputBuffer.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000024#include <vector>
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000025
26//===----------------------------------------------------------------------===//
27// MachOCodeEmitter Implementation
28//===----------------------------------------------------------------------===//
29
30namespace llvm {
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000031
32MachOCodeEmitter::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
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000039/// startFunction - This callback is invoked when a new machine function is
40/// about to be emitted.
41
42void 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
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000055 // Upgrade the section alignment if required.
56 if (MOS->align < Align) MOS->align = Align;
57
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000058 MOS->emitAlignment(Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000059
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000060 // 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);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000067}
68
69/// finishFunction - This callback is invoked after the function is completely
70/// finished.
71
72bool MachOCodeEmitter::finishFunction(MachineFunction &MF) {
73
74 // Get the Mach-O Section that this function belongs in.
75 MachOSection *MOS = MOW.getTextSection();
76
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000077 // 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 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000109 MOS->addRelocation(MR);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000110 }
111 Relocations.clear();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000112
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.
125void 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);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000145 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000146
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000147 CPLocations.push_back(Sec->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000148 CPSections.push_back(Sec->Index);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000149
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
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000156 MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i],
157 TM.getTargetData(), Sec);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000158 }
159}
160
161/// emitJumpTables - Emit all the jump tables for a given jump table info
162/// record to the appropriate section.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000163void 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;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000173 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000174
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;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000179 JTLocations.push_back(Sec->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000180 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000181 MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi]));
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000182 MR.setResultPointer((void *)JTLocations[i]);
183 MR.setConstantVal(TextSecIndex);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000184 Sec->addRelocation(MR);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000185 SecDataOut.outaddr(0);
186 }
187 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000188}
189
190} // end namespace llvm
191