LLVM API Documentation
00001 //===-- MachOEmitter.cpp - Target-independent Mach-O Emitter code --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "MachO.h" 00011 #include "MachOWriter.h" 00012 #include "MachOCodeEmitter.h" 00013 #include "llvm/Constants.h" 00014 #include "llvm/DerivedTypes.h" 00015 #include "llvm/Function.h" 00016 #include "llvm/CodeGen/MachineConstantPool.h" 00017 #include "llvm/CodeGen/MachineFunction.h" 00018 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00019 #include "llvm/CodeGen/MachineRelocation.h" 00020 #include "llvm/Target/TargetAsmInfo.h" 00021 #include "llvm/Target/TargetData.h" 00022 #include "llvm/Target/TargetMachine.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/Mangler.h" 00025 #include "llvm/Support/OutputBuffer.h" 00026 #include <vector> 00027 00028 //===----------------------------------------------------------------------===// 00029 // MachOCodeEmitter Implementation 00030 //===----------------------------------------------------------------------===// 00031 00032 namespace llvm { 00033 00034 MachOCodeEmitter::MachOCodeEmitter(MachOWriter &mow, MachOSection &mos) : 00035 ObjectCodeEmitter(&mos), MOW(mow), TM(MOW.TM) { 00036 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64; 00037 isLittleEndian = TM.getTargetData()->isLittleEndian(); 00038 TAI = TM.getTargetAsmInfo(); 00039 } 00040 00041 /// startFunction - This callback is invoked when a new machine function is 00042 /// about to be emitted. 00043 00044 void MachOCodeEmitter::startFunction(MachineFunction &MF) { 00045 const TargetData *TD = TM.getTargetData(); 00046 const Function *F = MF.getFunction(); 00047 00048 // Align the output buffer to the appropriate alignment, power of 2. 00049 unsigned FnAlign = F->getAlignment(); 00050 unsigned TDAlign = TD->getPrefTypeAlignment(F->getType()); 00051 unsigned Align = Log2_32(std::max(FnAlign, TDAlign)); 00052 assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); 00053 00054 // Get the Mach-O Section that this function belongs in. 00055 MachOSection *MOS = MOW.getTextSection(); 00056 00057 // Upgrade the section alignment if required. 00058 if (MOS->align < Align) MOS->align = Align; 00059 00060 MOS->emitAlignment(Align); 00061 00062 // Create symbol for function entry 00063 const GlobalValue *FuncV = MF.getFunction(); 00064 MachOSym FnSym(FuncV, MOW.Mang->getMangledName(FuncV), MOS->Index, TAI); 00065 FnSym.n_value = getCurrentPCOffset(); 00066 00067 // add it to the symtab. 00068 MOW.SymbolTable.push_back(FnSym); 00069 } 00070 00071 /// finishFunction - This callback is invoked after the function is completely 00072 /// finished. 00073 00074 bool MachOCodeEmitter::finishFunction(MachineFunction &MF) { 00075 00076 // Get the Mach-O Section that this function belongs in. 00077 MachOSection *MOS = MOW.getTextSection(); 00078 00079 // Emit constant pool to appropriate section(s) 00080 emitConstantPool(MF.getConstantPool()); 00081 00082 // Emit jump tables to appropriate section 00083 emitJumpTables(MF.getJumpTableInfo()); 00084 00085 // If we have emitted any relocations to function-specific objects such as 00086 // basic blocks, constant pools entries, or jump tables, record their 00087 // addresses now so that we can rewrite them with the correct addresses 00088 // later. 00089 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { 00090 MachineRelocation &MR = Relocations[i]; 00091 intptr_t Addr; 00092 00093 if (MR.isBasicBlock()) { 00094 Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); 00095 MR.setConstantVal(MOS->Index); 00096 MR.setResultPointer((void*)Addr); 00097 } else if (MR.isJumpTableIndex()) { 00098 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex()); 00099 MR.setConstantVal(MOW.getJumpTableSection()->Index); 00100 MR.setResultPointer((void*)Addr); 00101 } else if (MR.isConstantPoolIndex()) { 00102 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex()); 00103 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]); 00104 MR.setResultPointer((void*)Addr); 00105 } else if (MR.isGlobalValue()) { 00106 // FIXME: This should be a set or something that uniques 00107 MOW.PendingGlobals.push_back(MR.getGlobalValue()); 00108 } else { 00109 llvm_unreachable("Unhandled relocation type"); 00110 } 00111 MOS->addRelocation(MR); 00112 } 00113 Relocations.clear(); 00114 00115 // Clear per-function data structures. 00116 CPLocations.clear(); 00117 CPSections.clear(); 00118 JTLocations.clear(); 00119 MBBLocations.clear(); 00120 00121 return false; 00122 } 00123 00124 /// emitConstantPool - For each constant pool entry, figure out which section 00125 /// the constant should live in, allocate space for it, and emit it to the 00126 /// Section data buffer. 00127 void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) { 00128 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); 00129 if (CP.empty()) return; 00130 00131 // FIXME: handle PIC codegen 00132 assert(TM.getRelocationModel() != Reloc::PIC_ && 00133 "PIC codegen not yet handled for mach-o jump tables!"); 00134 00135 // Although there is no strict necessity that I am aware of, we will do what 00136 // gcc for OS X does and put each constant pool entry in a section of constant 00137 // objects of a certain size. That means that float constants go in the 00138 // literal4 section, and double objects go in literal8, etc. 00139 // 00140 // FIXME: revisit this decision if we ever do the "stick everything into one 00141 // "giant object for PIC" optimization. 00142 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 00143 const Type *Ty = CP[i].getType(); 00144 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty); 00145 00146 MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal); 00147 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); 00148 00149 CPLocations.push_back(Sec->size()); 00150 CPSections.push_back(Sec->Index); 00151 00152 // Allocate space in the section for the global. 00153 // FIXME: need alignment? 00154 // FIXME: share between here and AddSymbolToSection? 00155 for (unsigned j = 0; j < Size; ++j) 00156 SecDataOut.outbyte(0); 00157 00158 MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i], 00159 TM.getTargetData(), Sec); 00160 } 00161 } 00162 00163 /// emitJumpTables - Emit all the jump tables for a given jump table info 00164 /// record to the appropriate section. 00165 void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) { 00166 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 00167 if (JT.empty()) return; 00168 00169 // FIXME: handle PIC codegen 00170 assert(TM.getRelocationModel() != Reloc::PIC_ && 00171 "PIC codegen not yet handled for mach-o jump tables!"); 00172 00173 MachOSection *Sec = MOW.getJumpTableSection(); 00174 unsigned TextSecIndex = MOW.getTextSection()->Index; 00175 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); 00176 00177 for (unsigned i = 0, e = JT.size(); i != e; ++i) { 00178 // For each jump table, record its offset from the start of the section, 00179 // reserve space for the relocations to the MBBs, and add the relocations. 00180 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; 00181 JTLocations.push_back(Sec->size()); 00182 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { 00183 MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi])); 00184 MR.setResultPointer((void *)JTLocations[i]); 00185 MR.setConstantVal(TextSecIndex); 00186 Sec->addRelocation(MR); 00187 SecDataOut.outaddr(0); 00188 } 00189 } 00190 } 00191 00192 } // end namespace llvm 00193