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" |
David Greene | 43c76fb | 2009-08-19 22:08:26 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRelocation.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mangler.h" |
| 25 | #include "llvm/Support/OutputBuffer.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 26 | #include <vector> |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // MachOCodeEmitter Implementation |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | namespace llvm { |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 33 | |
| 34 | MachOCodeEmitter::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 Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 38 | MAI = TM.getMCAsmInfo(); |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 41 | /// startFunction - This callback is invoked when a new machine function is |
| 42 | /// about to be emitted. |
| 43 | |
| 44 | void 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 Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 57 | // Upgrade the section alignment if required. |
| 58 | if (MOS->align < Align) MOS->align = Align; |
| 59 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 60 | MOS->emitAlignment(Align); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 61 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 62 | // Create symbol for function entry |
| 63 | const GlobalValue *FuncV = MF.getFunction(); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 64 | MachOSym FnSym(FuncV, MOW.Mang->getMangledName(FuncV), MOS->Index, MAI); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 65 | FnSym.n_value = getCurrentPCOffset(); |
| 66 | |
| 67 | // add it to the symtab. |
| 68 | MOW.SymbolTable.push_back(FnSym); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// finishFunction - This callback is invoked after the function is completely |
| 72 | /// finished. |
| 73 | |
| 74 | bool MachOCodeEmitter::finishFunction(MachineFunction &MF) { |
| 75 | |
| 76 | // Get the Mach-O Section that this function belongs in. |
| 77 | MachOSection *MOS = MOW.getTextSection(); |
| 78 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 79 | // 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 Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 109 | llvm_unreachable("Unhandled relocation type"); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 110 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 111 | MOS->addRelocation(MR); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 112 | } |
| 113 | Relocations.clear(); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 114 | |
| 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. |
| 127 | void 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 Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 147 | OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 148 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 149 | CPLocations.push_back(Sec->size()); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 150 | CPSections.push_back(Sec->Index); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 151 | |
| 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 Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame] | 158 | MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i], |
| 159 | TM.getTargetData(), Sec); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 164 | /// record to the appropriate section. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 165 | void 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 Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 175 | OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 176 | |
| 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 Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 181 | JTLocations.push_back(Sec->size()); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 182 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 183 | MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi])); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 184 | MR.setResultPointer((void *)JTLocations[i]); |
| 185 | MR.setConstantVal(TextSecIndex); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 186 | Sec->addRelocation(MR); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 187 | SecDataOut.outaddr(0); |
| 188 | } |
| 189 | } |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // end namespace llvm |
| 193 | |