Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 10 | #include "MachO.h" |
| 11 | #include "MachOWriter.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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" |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRelocation.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetAsmInfo.h" |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Mangler.h" |
| 23 | #include "llvm/Support/OutputBuffer.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 24 | #include <vector> |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // MachOCodeEmitter Implementation |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace llvm { |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 55 | // Upgrade the section alignment if required. |
| 56 | if (MOS->align < Align) MOS->align = Align; |
| 57 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 58 | MOS->emitAlignment(Align); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 59 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 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); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 109 | MOS->addRelocation(MR); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 110 | } |
| 111 | Relocations.clear(); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 145 | OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 146 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 147 | CPLocations.push_back(Sec->size()); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 148 | CPSections.push_back(Sec->Index); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 156 | MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i], |
| 157 | TM.getTargetData(), Sec); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 162 | /// record to the appropriate section. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 173 | OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 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; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 179 | JTLocations.push_back(Sec->size()); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 180 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 181 | MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi])); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 182 | MR.setResultPointer((void *)JTLocations[i]); |
| 183 | MR.setConstantVal(TextSecIndex); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 184 | Sec->addRelocation(MR); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 185 | SecDataOut.outaddr(0); |
| 186 | } |
| 187 | } |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // end namespace llvm |
| 191 | |