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