Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / Target / TargetLoweringObjectFile.cpp
blob9f87c071643063c4ff78249c23fc51df0f24fe19
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 CompactUnwindSection = 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 IsFunctionEHFrameSymbolPrivate = true;
63 SupportsWeakOmittedEHFrame = true;
66 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
69 static bool isSuitableForBSS(const GlobalVariable *GV) {
70 const Constant *C = GV->getInitializer();
72 // Must have zero initializer.
73 if (!C->isNullValue())
74 return false;
76 // Leave constant zeros in readonly constant sections, so they can be shared.
77 if (GV->isConstant())
78 return false;
80 // If the global has an explicit section specified, don't put it in BSS.
81 if (!GV->getSection().empty())
82 return false;
84 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
85 if (NoZerosInBSS)
86 return false;
88 // Otherwise, put it in BSS!
89 return true;
92 /// IsNullTerminatedString - Return true if the specified constant (which is
93 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
94 /// nul value and contains no other nuls in it.
95 static bool IsNullTerminatedString(const Constant *C) {
96 const ArrayType *ATy = cast<ArrayType>(C->getType());
98 // First check: is we have constant array of i8 terminated with zero
99 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
100 if (ATy->getNumElements() == 0) return false;
102 ConstantInt *Null =
103 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
104 if (Null == 0 || !Null->isZero())
105 return false; // Not null terminated.
107 // Verify that the null doesn't occur anywhere else in the string.
108 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
109 // Reject constantexpr elements etc.
110 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
111 CVA->getOperand(i) == Null)
112 return false;
113 return true;
116 // Another possibility: [1 x i8] zeroinitializer
117 if (isa<ConstantAggregateZero>(C))
118 return ATy->getNumElements() == 1;
120 return false;
123 MCSymbol *TargetLoweringObjectFile::
124 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
125 MachineModuleInfo *MMI) const {
126 return Mang->getSymbol(GV);
129 void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
130 const TargetMachine &TM,
131 const MCSymbol *Sym) const {
135 /// getKindForGlobal - This is a top-level target-independent classifier for
136 /// a global variable. Given an global variable and information from TM, it
137 /// classifies the global in a variety of ways that make various target
138 /// implementations simpler. The target implementation is free to ignore this
139 /// extra info of course.
140 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
141 const TargetMachine &TM){
142 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
143 "Can only be used for global definitions");
145 Reloc::Model ReloModel = TM.getRelocationModel();
147 // Early exit - functions should be always in text sections.
148 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
149 if (GVar == 0)
150 return SectionKind::getText();
152 // Handle thread-local data first.
153 if (GVar->isThreadLocal()) {
154 if (isSuitableForBSS(GVar))
155 return SectionKind::getThreadBSS();
156 return SectionKind::getThreadData();
159 // Variables with common linkage always get classified as common.
160 if (GVar->hasCommonLinkage())
161 return SectionKind::getCommon();
163 // Variable can be easily put to BSS section.
164 if (isSuitableForBSS(GVar)) {
165 if (GVar->hasLocalLinkage())
166 return SectionKind::getBSSLocal();
167 else if (GVar->hasExternalLinkage())
168 return SectionKind::getBSSExtern();
169 return SectionKind::getBSS();
172 const Constant *C = GVar->getInitializer();
174 // If the global is marked constant, we can put it into a mergable section,
175 // a mergable string section, or general .data if it contains relocations.
176 if (GVar->isConstant()) {
177 // If the initializer for the global contains something that requires a
178 // relocation, then we may have to drop this into a wriable data section
179 // even though it is marked const.
180 switch (C->getRelocationInfo()) {
181 default: assert(0 && "unknown relocation info kind");
182 case Constant::NoRelocation:
183 // If the global is required to have a unique address, it can't be put
184 // into a mergable section: just drop it into the general read-only
185 // section instead.
186 if (!GVar->hasUnnamedAddr())
187 return SectionKind::getReadOnly();
189 // If initializer is a null-terminated string, put it in a "cstring"
190 // section of the right width.
191 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
192 if (const IntegerType *ITy =
193 dyn_cast<IntegerType>(ATy->getElementType())) {
194 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
195 ITy->getBitWidth() == 32) &&
196 IsNullTerminatedString(C)) {
197 if (ITy->getBitWidth() == 8)
198 return SectionKind::getMergeable1ByteCString();
199 if (ITy->getBitWidth() == 16)
200 return SectionKind::getMergeable2ByteCString();
202 assert(ITy->getBitWidth() == 32 && "Unknown width");
203 return SectionKind::getMergeable4ByteCString();
208 // Otherwise, just drop it into a mergable constant section. If we have
209 // a section for this size, use it, otherwise use the arbitrary sized
210 // mergable section.
211 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
212 case 4: return SectionKind::getMergeableConst4();
213 case 8: return SectionKind::getMergeableConst8();
214 case 16: return SectionKind::getMergeableConst16();
215 default: return SectionKind::getMergeableConst();
218 case Constant::LocalRelocation:
219 // In static relocation model, the linker will resolve all addresses, so
220 // the relocation entries will actually be constants by the time the app
221 // starts up. However, we can't put this into a mergable section, because
222 // the linker doesn't take relocations into consideration when it tries to
223 // merge entries in the section.
224 if (ReloModel == Reloc::Static)
225 return SectionKind::getReadOnly();
227 // Otherwise, the dynamic linker needs to fix it up, put it in the
228 // writable data.rel.local section.
229 return SectionKind::getReadOnlyWithRelLocal();
231 case Constant::GlobalRelocations:
232 // In static relocation model, the linker will resolve all addresses, so
233 // the relocation entries will actually be constants by the time the app
234 // starts up. However, we can't put this into a mergable section, because
235 // the linker doesn't take relocations into consideration when it tries to
236 // merge entries in the section.
237 if (ReloModel == Reloc::Static)
238 return SectionKind::getReadOnly();
240 // Otherwise, the dynamic linker needs to fix it up, put it in the
241 // writable data.rel section.
242 return SectionKind::getReadOnlyWithRel();
246 // Okay, this isn't a constant. If the initializer for the global is going
247 // to require a runtime relocation by the dynamic linker, put it into a more
248 // specific section to improve startup time of the app. This coalesces these
249 // globals together onto fewer pages, improving the locality of the dynamic
250 // linker.
251 if (ReloModel == Reloc::Static)
252 return SectionKind::getDataNoRel();
254 switch (C->getRelocationInfo()) {
255 default: assert(0 && "unknown relocation info kind");
256 case Constant::NoRelocation:
257 return SectionKind::getDataNoRel();
258 case Constant::LocalRelocation:
259 return SectionKind::getDataRelLocal();
260 case Constant::GlobalRelocations:
261 return SectionKind::getDataRel();
265 /// SectionForGlobal - This method computes the appropriate section to emit
266 /// the specified global variable or function definition. This should not
267 /// be passed external (or available externally) globals.
268 const MCSection *TargetLoweringObjectFile::
269 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
270 const TargetMachine &TM) const {
271 // Select section name.
272 if (GV->hasSection())
273 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
276 // Use default section depending on the 'type' of global
277 return SelectSectionForGlobal(GV, Kind, Mang, TM);
281 // Lame default implementation. Calculate the section name for global.
282 const MCSection *
283 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
284 SectionKind Kind,
285 Mangler *Mang,
286 const TargetMachine &TM) const{
287 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
289 if (Kind.isText())
290 return getTextSection();
292 if (Kind.isBSS() && BSSSection != 0)
293 return BSSSection;
295 if (Kind.isReadOnly() && ReadOnlySection != 0)
296 return ReadOnlySection;
298 return getDataSection();
301 /// getSectionForConstant - Given a mergable constant with the
302 /// specified size and relocation information, return a section that it
303 /// should be placed in.
304 const MCSection *
305 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
306 if (Kind.isReadOnly() && ReadOnlySection != 0)
307 return ReadOnlySection;
309 return DataSection;
312 /// getExprForDwarfGlobalReference - Return an MCExpr to use for a
313 /// reference to the specified global variable from exception
314 /// handling information.
315 const MCExpr *TargetLoweringObjectFile::
316 getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
317 MachineModuleInfo *MMI, unsigned Encoding,
318 MCStreamer &Streamer) const {
319 const MCSymbol *Sym = Mang->getSymbol(GV);
320 return getExprForDwarfReference(Sym, Encoding, Streamer);
323 const MCExpr *TargetLoweringObjectFile::
324 getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding,
325 MCStreamer &Streamer) const {
326 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
328 switch (Encoding & 0x70) {
329 default:
330 report_fatal_error("We do not support this DWARF encoding yet!");
331 case dwarf::DW_EH_PE_absptr:
332 // Do nothing special
333 return Res;
334 case dwarf::DW_EH_PE_pcrel: {
335 // Emit a label to the streamer for the current position. This gives us
336 // .-foo addressing.
337 MCSymbol *PCSym = getContext().CreateTempSymbol();
338 Streamer.EmitLabel(PCSym);
339 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
340 return MCBinaryExpr::CreateSub(Res, PC, getContext());
345 unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
346 return dwarf::DW_EH_PE_absptr;
349 unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
350 return dwarf::DW_EH_PE_absptr;
353 unsigned TargetLoweringObjectFile::getFDEEncoding(bool CFI) const {
354 return dwarf::DW_EH_PE_absptr;
357 unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
358 return dwarf::DW_EH_PE_absptr;