Fixed some bugs in register stack pass.
[llvm/zpu.git] / lib / Target / TargetLoweringObjectFile.cpp
blob0e67fe5d9877be59ef67df74e44bb328bfdaacef
1 //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
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 //
10 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Target/TargetLoweringObjectFile.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Target/Mangler.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Support/Dwarf.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/SmallString.h"
32 using namespace llvm;
34 //===----------------------------------------------------------------------===//
35 // Generic Code
36 //===----------------------------------------------------------------------===//
38 TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
39 TextSection = 0;
40 DataSection = 0;
41 BSSSection = 0;
42 ReadOnlySection = 0;
43 StaticCtorSection = 0;
44 StaticDtorSection = 0;
45 LSDASection = 0;
46 EHFrameSection = 0;
48 CommDirectiveSupportsAlignment = true;
49 DwarfAbbrevSection = 0;
50 DwarfInfoSection = 0;
51 DwarfLineSection = 0;
52 DwarfFrameSection = 0;
53 DwarfPubNamesSection = 0;
54 DwarfPubTypesSection = 0;
55 DwarfDebugInlineSection = 0;
56 DwarfStrSection = 0;
57 DwarfLocSection = 0;
58 DwarfARangesSection = 0;
59 DwarfRangesSection = 0;
60 DwarfMacroInfoSection = 0;
62 IsFunctionEHSymbolGlobal = false;
63 IsFunctionEHFrameSymbolPrivate = true;
64 SupportsWeakOmittedEHFrame = true;
67 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
70 static bool isSuitableForBSS(const GlobalVariable *GV) {
71 Constant *C = GV->getInitializer();
73 // Must have zero initializer.
74 if (!C->isNullValue())
75 return false;
77 // Leave constant zeros in readonly constant sections, so they can be shared.
78 if (GV->isConstant())
79 return false;
81 // If the global has an explicit section specified, don't put it in BSS.
82 if (!GV->getSection().empty())
83 return false;
85 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
86 if (NoZerosInBSS)
87 return false;
89 // Otherwise, put it in BSS!
90 return true;
93 /// IsNullTerminatedString - Return true if the specified constant (which is
94 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
95 /// nul value and contains no other nuls in it.
96 static bool IsNullTerminatedString(const Constant *C) {
97 const ArrayType *ATy = cast<ArrayType>(C->getType());
99 // First check: is we have constant array of i8 terminated with zero
100 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
101 if (ATy->getNumElements() == 0) return false;
103 ConstantInt *Null =
104 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
105 if (Null == 0 || !Null->isZero())
106 return false; // Not null terminated.
108 // Verify that the null doesn't occur anywhere else in the string.
109 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
110 // Reject constantexpr elements etc.
111 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
112 CVA->getOperand(i) == Null)
113 return false;
114 return true;
117 // Another possibility: [1 x i8] zeroinitializer
118 if (isa<ConstantAggregateZero>(C))
119 return ATy->getNumElements() == 1;
121 return false;
124 /// getKindForGlobal - This is a top-level target-independent classifier for
125 /// a global variable. Given an global variable and information from TM, it
126 /// classifies the global in a variety of ways that make various target
127 /// implementations simpler. The target implementation is free to ignore this
128 /// extra info of course.
129 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
130 const TargetMachine &TM){
131 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
132 "Can only be used for global definitions");
134 Reloc::Model ReloModel = TM.getRelocationModel();
136 // Early exit - functions should be always in text sections.
137 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
138 if (GVar == 0)
139 return SectionKind::getText();
141 // Handle thread-local data first.
142 if (GVar->isThreadLocal()) {
143 if (isSuitableForBSS(GVar))
144 return SectionKind::getThreadBSS();
145 return SectionKind::getThreadData();
148 // Variables with common linkage always get classified as common.
149 if (GVar->hasCommonLinkage())
150 return SectionKind::getCommon();
152 // Variable can be easily put to BSS section.
153 if (isSuitableForBSS(GVar)) {
154 if (GVar->hasLocalLinkage())
155 return SectionKind::getBSSLocal();
156 else if (GVar->hasExternalLinkage())
157 return SectionKind::getBSSExtern();
158 return SectionKind::getBSS();
161 Constant *C = GVar->getInitializer();
163 // If the global is marked constant, we can put it into a mergable section,
164 // a mergable string section, or general .data if it contains relocations.
165 if (GVar->isConstant()) {
166 // If the initializer for the global contains something that requires a
167 // relocation, then we may have to drop this into a wriable data section
168 // even though it is marked const.
169 switch (C->getRelocationInfo()) {
170 default: assert(0 && "unknown relocation info kind");
171 case Constant::NoRelocation:
172 // If initializer is a null-terminated string, put it in a "cstring"
173 // section of the right width.
174 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
175 if (const IntegerType *ITy =
176 dyn_cast<IntegerType>(ATy->getElementType())) {
177 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
178 ITy->getBitWidth() == 32) &&
179 IsNullTerminatedString(C)) {
180 if (ITy->getBitWidth() == 8)
181 return SectionKind::getMergeable1ByteCString();
182 if (ITy->getBitWidth() == 16)
183 return SectionKind::getMergeable2ByteCString();
185 assert(ITy->getBitWidth() == 32 && "Unknown width");
186 return SectionKind::getMergeable4ByteCString();
191 // Otherwise, just drop it into a mergable constant section. If we have
192 // a section for this size, use it, otherwise use the arbitrary sized
193 // mergable section.
194 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
195 case 4: return SectionKind::getMergeableConst4();
196 case 8: return SectionKind::getMergeableConst8();
197 case 16: return SectionKind::getMergeableConst16();
198 default: return SectionKind::getMergeableConst();
201 case Constant::LocalRelocation:
202 // In static relocation model, the linker will resolve all addresses, so
203 // the relocation entries will actually be constants by the time the app
204 // starts up. However, we can't put this into a mergable section, because
205 // the linker doesn't take relocations into consideration when it tries to
206 // merge entries in the section.
207 if (ReloModel == Reloc::Static)
208 return SectionKind::getReadOnly();
210 // Otherwise, the dynamic linker needs to fix it up, put it in the
211 // writable data.rel.local section.
212 return SectionKind::getReadOnlyWithRelLocal();
214 case Constant::GlobalRelocations:
215 // In static relocation model, the linker will resolve all addresses, so
216 // the relocation entries will actually be constants by the time the app
217 // starts up. However, we can't put this into a mergable section, because
218 // the linker doesn't take relocations into consideration when it tries to
219 // merge entries in the section.
220 if (ReloModel == Reloc::Static)
221 return SectionKind::getReadOnly();
223 // Otherwise, the dynamic linker needs to fix it up, put it in the
224 // writable data.rel section.
225 return SectionKind::getReadOnlyWithRel();
229 // Okay, this isn't a constant. If the initializer for the global is going
230 // to require a runtime relocation by the dynamic linker, put it into a more
231 // specific section to improve startup time of the app. This coalesces these
232 // globals together onto fewer pages, improving the locality of the dynamic
233 // linker.
234 if (ReloModel == Reloc::Static)
235 return SectionKind::getDataNoRel();
237 switch (C->getRelocationInfo()) {
238 default: assert(0 && "unknown relocation info kind");
239 case Constant::NoRelocation:
240 return SectionKind::getDataNoRel();
241 case Constant::LocalRelocation:
242 return SectionKind::getDataRelLocal();
243 case Constant::GlobalRelocations:
244 return SectionKind::getDataRel();
248 /// SectionForGlobal - This method computes the appropriate section to emit
249 /// the specified global variable or function definition. This should not
250 /// be passed external (or available externally) globals.
251 const MCSection *TargetLoweringObjectFile::
252 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
253 const TargetMachine &TM) const {
254 // Select section name.
255 if (GV->hasSection())
256 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
259 // Use default section depending on the 'type' of global
260 return SelectSectionForGlobal(GV, Kind, Mang, TM);
264 // Lame default implementation. Calculate the section name for global.
265 const MCSection *
266 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
267 SectionKind Kind,
268 Mangler *Mang,
269 const TargetMachine &TM) const{
270 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
272 if (Kind.isText())
273 return getTextSection();
275 if (Kind.isBSS() && BSSSection != 0)
276 return BSSSection;
278 if (Kind.isReadOnly() && ReadOnlySection != 0)
279 return ReadOnlySection;
281 return getDataSection();
284 /// getSectionForConstant - Given a mergable constant with the
285 /// specified size and relocation information, return a section that it
286 /// should be placed in.
287 const MCSection *
288 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
289 if (Kind.isReadOnly() && ReadOnlySection != 0)
290 return ReadOnlySection;
292 return DataSection;
295 /// getExprForDwarfGlobalReference - Return an MCExpr to use for a
296 /// reference to the specified global variable from exception
297 /// handling information.
298 const MCExpr *TargetLoweringObjectFile::
299 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
300 MachineModuleInfo *MMI, unsigned Encoding,
301 MCStreamer &Streamer) const {
302 const MCSymbol *Sym = Mang->getSymbol(GV);
303 return getExprForDwarfReference(Sym, Mang, MMI, Encoding, Streamer);
306 const MCExpr *TargetLoweringObjectFile::
307 getExprForDwarfReference(const MCSymbol *Sym, Mangler *Mang,
308 MachineModuleInfo *MMI, unsigned Encoding,
309 MCStreamer &Streamer) const {
310 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
312 switch (Encoding & 0xF0) {
313 default:
314 report_fatal_error("We do not support this DWARF encoding yet!");
315 case dwarf::DW_EH_PE_absptr:
316 // Do nothing special
317 return Res;
318 case dwarf::DW_EH_PE_pcrel: {
319 // Emit a label to the streamer for the current position. This gives us
320 // .-foo addressing.
321 MCSymbol *PCSym = getContext().CreateTempSymbol();
322 Streamer.EmitLabel(PCSym);
323 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
324 return MCBinaryExpr::CreateSub(Res, PC, getContext());
329 unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
330 return dwarf::DW_EH_PE_absptr;
333 unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
334 return dwarf::DW_EH_PE_absptr;
337 unsigned TargetLoweringObjectFile::getFDEEncoding() const {
338 return dwarf::DW_EH_PE_absptr;
341 unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
342 return dwarf::DW_EH_PE_absptr;