blob: 13184772cdb45177e81c0f6800e6d382976a1e2f [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"
David Greene43c76fb2009-08-19 22:08:26 +000017#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000018#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000019#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetMachine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000024#include "llvm/Support/Mangler.h"
25#include "llvm/Support/OutputBuffer.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000026#include <vector>
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000027
28//===----------------------------------------------------------------------===//
29// MachOCodeEmitter Implementation
30//===----------------------------------------------------------------------===//
31
32namespace llvm {
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000033
34MachOCodeEmitter::MachOCodeEmitter(MachOWriter &mow, MachOSection &mos) :
35 ObjectCodeEmitter(&mos), MOW(mow), TM(MOW.TM) {
36 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
37 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattner33adcfb2009-08-22 21:43:10 +000038 MAI = TM.getMCAsmInfo();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000039}
40
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000041/// startFunction - This callback is invoked when a new machine function is
42/// about to be emitted.
43
44void MachOCodeEmitter::startFunction(MachineFunction &MF) {
45 const TargetData *TD = TM.getTargetData();
46 const Function *F = MF.getFunction();
47
48 // Align the output buffer to the appropriate alignment, power of 2.
49 unsigned FnAlign = F->getAlignment();
50 unsigned TDAlign = TD->getPrefTypeAlignment(F->getType());
51 unsigned Align = Log2_32(std::max(FnAlign, TDAlign));
52 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
53
54 // Get the Mach-O Section that this function belongs in.
55 MachOSection *MOS = MOW.getTextSection();
56
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000057 // Upgrade the section alignment if required.
58 if (MOS->align < Align) MOS->align = Align;
59
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000060 MOS->emitAlignment(Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000061
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000062 // Create symbol for function entry
63 const GlobalValue *FuncV = MF.getFunction();
Chris Lattner33adcfb2009-08-22 21:43:10 +000064 MachOSym FnSym(FuncV, MOW.Mang->getMangledName(FuncV), MOS->Index, MAI);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000065 FnSym.n_value = getCurrentPCOffset();
66
67 // add it to the symtab.
68 MOW.SymbolTable.push_back(FnSym);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000069}
70
71/// finishFunction - This callback is invoked after the function is completely
72/// finished.
73
74bool MachOCodeEmitter::finishFunction(MachineFunction &MF) {
75
76 // Get the Mach-O Section that this function belongs in.
77 MachOSection *MOS = MOW.getTextSection();
78
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000079 // Emit constant pool to appropriate section(s)
80 emitConstantPool(MF.getConstantPool());
81
82 // Emit jump tables to appropriate section
83 emitJumpTables(MF.getJumpTableInfo());
84
85 // If we have emitted any relocations to function-specific objects such as
86 // basic blocks, constant pools entries, or jump tables, record their
87 // addresses now so that we can rewrite them with the correct addresses
88 // later.
89 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
90 MachineRelocation &MR = Relocations[i];
91 intptr_t Addr;
92
93 if (MR.isBasicBlock()) {
94 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
95 MR.setConstantVal(MOS->Index);
96 MR.setResultPointer((void*)Addr);
97 } else if (MR.isJumpTableIndex()) {
98 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
99 MR.setConstantVal(MOW.getJumpTableSection()->Index);
100 MR.setResultPointer((void*)Addr);
101 } else if (MR.isConstantPoolIndex()) {
102 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
103 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
104 MR.setResultPointer((void*)Addr);
105 } else if (MR.isGlobalValue()) {
106 // FIXME: This should be a set or something that uniques
107 MOW.PendingGlobals.push_back(MR.getGlobalValue());
108 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000109 llvm_unreachable("Unhandled relocation type");
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000110 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000111 MOS->addRelocation(MR);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000112 }
113 Relocations.clear();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000114
115 // Clear per-function data structures.
116 CPLocations.clear();
117 CPSections.clear();
118 JTLocations.clear();
119 MBBLocations.clear();
120
121 return false;
122}
123
124/// emitConstantPool - For each constant pool entry, figure out which section
125/// the constant should live in, allocate space for it, and emit it to the
126/// Section data buffer.
127void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
128 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
129 if (CP.empty()) return;
130
131 // FIXME: handle PIC codegen
132 assert(TM.getRelocationModel() != Reloc::PIC_ &&
133 "PIC codegen not yet handled for mach-o jump tables!");
134
135 // Although there is no strict necessity that I am aware of, we will do what
136 // gcc for OS X does and put each constant pool entry in a section of constant
137 // objects of a certain size. That means that float constants go in the
138 // literal4 section, and double objects go in literal8, etc.
139 //
140 // FIXME: revisit this decision if we ever do the "stick everything into one
141 // "giant object for PIC" optimization.
142 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
143 const Type *Ty = CP[i].getType();
144 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
145
146 MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000147 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000148
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000149 CPLocations.push_back(Sec->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000150 CPSections.push_back(Sec->Index);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000151
152 // Allocate space in the section for the global.
153 // FIXME: need alignment?
154 // FIXME: share between here and AddSymbolToSection?
155 for (unsigned j = 0; j < Size; ++j)
156 SecDataOut.outbyte(0);
157
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000158 MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i],
159 TM.getTargetData(), Sec);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000160 }
161}
162
163/// emitJumpTables - Emit all the jump tables for a given jump table info
164/// record to the appropriate section.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000165void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
166 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
167 if (JT.empty()) return;
168
169 // FIXME: handle PIC codegen
170 assert(TM.getRelocationModel() != Reloc::PIC_ &&
171 "PIC codegen not yet handled for mach-o jump tables!");
172
173 MachOSection *Sec = MOW.getJumpTableSection();
174 unsigned TextSecIndex = MOW.getTextSection()->Index;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000175 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000176
177 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
178 // For each jump table, record its offset from the start of the section,
179 // reserve space for the relocations to the MBBs, and add the relocations.
180 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000181 JTLocations.push_back(Sec->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000182 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000183 MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi]));
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000184 MR.setResultPointer((void *)JTLocations[i]);
185 MR.setConstantVal(TextSecIndex);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000186 Sec->addRelocation(MR);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000187 SecDataOut.outaddr(0);
188 }
189 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000190}
191
192} // end namespace llvm
193