Indentation change.
[llvm/avr.git] / lib / CodeGen / ELFCodeEmitter.cpp
blob38cf826fbe3f227fcbb7c4af7bfdff795b83f251
1 //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
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 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "elfce"
12 #include "ELF.h"
13 #include "ELFWriter.h"
14 #include "ELFCodeEmitter.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/BinaryObject.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/CodeGen/MachineRelocation.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetELFWriterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
30 //===----------------------------------------------------------------------===//
31 // ELFCodeEmitter Implementation
32 //===----------------------------------------------------------------------===//
34 namespace llvm {
36 /// startFunction - This callback is invoked when a new machine function is
37 /// about to be emitted.
38 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
39 DEBUG(errs() << "processing function: "
40 << MF.getFunction()->getName() << "\n");
42 // Get the ELF Section that this function belongs in.
43 ES = &EW.getTextSection(MF.getFunction());
45 // Set the desired binary object to be used by the code emitters
46 setBinaryObject(ES);
48 // Get the function alignment in bytes
49 unsigned Align = (1 << MF.getAlignment());
51 // The function must start on its required alignment
52 ES->emitAlignment(Align);
54 // Update the section alignment if needed.
55 ES->Align = std::max(ES->Align, Align);
57 // Record the function start offset
58 FnStartOff = ES->getCurrentPCOffset();
60 // Emit constant pool and jump tables to their appropriate sections.
61 // They need to be emitted before the function because in some targets
62 // the later may reference JT or CP entry address.
63 emitConstantPool(MF.getConstantPool());
64 emitJumpTables(MF.getJumpTableInfo());
67 /// finishFunction - This callback is invoked after the function is completely
68 /// finished.
69 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
70 // Add a symbol to represent the function.
71 const Function *F = MF.getFunction();
72 ELFSym *FnSym = ELFSym::getGV(F, EW.getGlobalELFBinding(F), ELFSym::STT_FUNC,
73 EW.getGlobalELFVisibility(F));
74 FnSym->SectionIdx = ES->SectionIdx;
75 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
76 EW.AddPendingGlobalSymbol(F, true);
78 // Offset from start of Section
79 FnSym->Value = FnStartOff;
81 if (!F->hasPrivateLinkage())
82 EW.SymbolList.push_back(FnSym);
84 // Patch up Jump Table Section relocations to use the real MBBs offsets
85 // now that the MBB label offsets inside the function are known.
86 if (!MF.getJumpTableInfo()->isEmpty()) {
87 ELFSection &JTSection = EW.getJumpTableSection();
88 for (std::vector<MachineRelocation>::iterator MRI = JTRelocations.begin(),
89 MRE = JTRelocations.end(); MRI != MRE; ++MRI) {
90 MachineRelocation &MR = *MRI;
91 unsigned MBBOffset = getMachineBasicBlockAddress(MR.getBasicBlock());
92 MR.setResultPointer((void*)MBBOffset);
93 MR.setConstantVal(ES->SectionIdx);
94 JTSection.addRelocation(MR);
98 // If we have emitted any relocations to function-specific objects such as
99 // basic blocks, constant pools entries, or jump tables, record their
100 // addresses now so that we can rewrite them with the correct addresses later
101 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
102 MachineRelocation &MR = Relocations[i];
103 intptr_t Addr;
104 if (MR.isGlobalValue()) {
105 EW.AddPendingGlobalSymbol(MR.getGlobalValue());
106 } else if (MR.isExternalSymbol()) {
107 EW.AddPendingExternalSymbol(MR.getExternalSymbol());
108 } else if (MR.isBasicBlock()) {
109 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
110 MR.setConstantVal(ES->SectionIdx);
111 MR.setResultPointer((void*)Addr);
112 } else if (MR.isConstantPoolIndex()) {
113 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
114 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
115 MR.setResultPointer((void*)Addr);
116 } else if (MR.isJumpTableIndex()) {
117 ELFSection &JTSection = EW.getJumpTableSection();
118 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
119 MR.setConstantVal(JTSection.SectionIdx);
120 MR.setResultPointer((void*)Addr);
121 } else {
122 llvm_unreachable("Unhandled relocation type");
124 ES->addRelocation(MR);
127 // Clear per-function data structures.
128 JTRelocations.clear();
129 Relocations.clear();
130 CPLocations.clear();
131 CPSections.clear();
132 JTLocations.clear();
133 MBBLocations.clear();
134 return false;
137 /// emitConstantPool - For each constant pool entry, figure out which section
138 /// the constant should live in and emit the constant
139 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
140 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
141 if (CP.empty()) return;
143 // TODO: handle PIC codegen
144 assert(TM.getRelocationModel() != Reloc::PIC_ &&
145 "PIC codegen not yet handled for elf constant pools!");
147 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
148 MachineConstantPoolEntry CPE = CP[i];
150 // Record the constant pool location and the section index
151 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
152 CPLocations.push_back(CstPool.size());
153 CPSections.push_back(CstPool.SectionIdx);
155 if (CPE.isMachineConstantPoolEntry())
156 assert("CPE.isMachineConstantPoolEntry not supported yet");
158 // Emit the constant to constant pool section
159 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
163 /// emitJumpTables - Emit all the jump tables for a given jump table info
164 /// record to the appropriate section.
165 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
166 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
167 if (JT.empty()) return;
169 // FIXME: handle PIC codegen
170 assert(TM.getRelocationModel() != Reloc::PIC_ &&
171 "PIC codegen not yet handled for elf jump tables!");
173 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
174 unsigned EntrySize = MJTI->getEntrySize();
176 // Get the ELF Section to emit the jump table
177 ELFSection &JTSection = EW.getJumpTableSection();
179 // For each JT, record its offset from the start of the section
180 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
181 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
183 // Record JT 'i' offset in the JT section
184 JTLocations.push_back(JTSection.size());
186 // Each MBB entry in the Jump table section has a relocation entry
187 // against the current text section.
188 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
189 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
190 MachineRelocation MR =
191 MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
193 // Add the relocation to the Jump Table section
194 JTRelocations.push_back(MR);
196 // Output placeholder for MBB in the JT section
197 for (unsigned s=0; s < EntrySize; ++s)
198 JTSection.emitByte(0);
203 } // end namespace llvm