1 //===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target asm properties related what form asm statements
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/Function.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Target/TargetAsmInfo.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/ErrorHandling.h"
31 TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm) : TM(tm) {
32 BSSSection = "\t.bss";
37 ZeroFillDirective = 0;
38 NonexecutableStackDirective = 0;
45 FirstOperandColumn = 0;
48 PrivateGlobalPrefix = ".";
49 LinkerPrivateGlobalPrefix = "";
50 JumpTableSpecialLabelPrefix = 0;
51 GlobalVarAddrPrefix = "";
52 GlobalVarAddrSuffix = "";
53 FunctionAddrPrefix = "";
54 FunctionAddrSuffix = "";
55 PersonalityPrefix = "";
56 PersonalitySuffix = "";
57 NeedsIndirectEncoding = false;
58 InlineAsmStart = "#APP";
59 InlineAsmEnd = "#NO_APP";
61 AllowQuotesInName = false;
62 ZeroDirective = "\t.zero\t";
63 ZeroDirectiveSuffix = 0;
64 AsciiDirective = "\t.ascii\t";
65 AscizDirective = "\t.asciz\t";
66 Data8bitsDirective = "\t.byte\t";
67 Data16bitsDirective = "\t.short\t";
68 Data32bitsDirective = "\t.long\t";
69 Data64bitsDirective = "\t.quad\t";
70 AlignDirective = "\t.align\t";
71 AlignmentIsInBytes = true;
72 TextAlignFillValue = 0;
73 SwitchToSectionDirective = "\t.section\t";
74 TextSectionStartSuffix = "";
75 DataSectionStartSuffix = "";
76 SectionEndDirectiveSuffix = 0;
77 ConstantPoolSection = "\t.section .rodata";
78 JumpTableDataSection = "\t.section .rodata";
79 JumpTableDirective = 0;
82 // FIXME: Flags are ELFish - replace with normal section stuff.
83 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
84 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
85 GlobalDirective = "\t.globl\t";
88 COMMDirective = "\t.comm\t";
89 COMMDirectiveTakesAlignment = true;
90 HasDotTypeDotSizeDirective = true;
91 HasSingleParameterDotFile = true;
95 // FIXME: These are ELFish - move to ELFTAI.
96 HiddenDirective = "\t.hidden\t";
97 ProtectedDirective = "\t.protected\t";
98 AbsoluteDebugSectionOffsets = false;
99 AbsoluteEHSectionOffsets = false;
101 HasDotLocAndDotFile = false;
102 SupportsDebugInformation = false;
103 SupportsExceptionHandling = false;
104 DwarfRequiresFrameSection = true;
105 DwarfUsesInlineInfoSection = false;
106 Is_EHSymbolPrivate = true;
107 GlobalEHDirective = 0;
108 SupportsWeakOmittedEHFrame = true;
109 DwarfSectionOffsetDirective = 0;
110 DwarfAbbrevSection = ".debug_abbrev";
111 DwarfInfoSection = ".debug_info";
112 DwarfLineSection = ".debug_line";
113 DwarfFrameSection = ".debug_frame";
114 DwarfPubNamesSection = ".debug_pubnames";
115 DwarfPubTypesSection = ".debug_pubtypes";
116 DwarfDebugInlineSection = ".debug_inlined";
117 DwarfStrSection = ".debug_str";
118 DwarfLocSection = ".debug_loc";
119 DwarfARangesSection = ".debug_aranges";
120 DwarfRangesSection = ".debug_ranges";
121 DwarfMacroInfoSection = ".debug_macinfo";
122 DwarfEHFrameSection = ".eh_frame";
123 DwarfExceptionSection = ".gcc_except_table";
127 TargetAsmInfo::~TargetAsmInfo() {
130 /// Measure the specified inline asm to determine an approximation of its
132 /// Comments (which run till the next SeparatorChar or newline) do not
133 /// count as an instruction.
134 /// Any other non-whitespace text is considered an instruction, with
135 /// multiple instructions separated by SeparatorChar or newlines.
136 /// Variable-length instructions are not handled here; this function
137 /// may be overloaded in the target code to do that.
138 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
139 // Count the number of instructions in the asm.
140 bool atInsnStart = true;
142 for (; *Str; ++Str) {
143 if (*Str == '\n' || *Str == SeparatorChar)
145 if (atInsnStart && !isspace(*Str)) {
146 Length += MaxInstLength;
149 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
156 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
158 return dwarf::DW_EH_PE_absptr;
161 static bool isSuitableForBSS(const GlobalVariable *GV) {
162 Constant *C = GV->getInitializer();
164 // Must have zero initializer.
165 if (!C->isNullValue())
168 // Leave constant zeros in readonly constant sections, so they can be shared.
169 if (GV->isConstant())
172 // If the global has an explicit section specified, don't put it in BSS.
173 if (!GV->getSection().empty())
176 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
180 // Otherwise, put it in BSS!
184 static bool isConstantString(const Constant *C) {
185 // First check: is we have constant array of i8 terminated with zero
186 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
187 // Check, if initializer is a null-terminated string
188 if (CVA && CVA->isCString())
191 // Another possibility: [1 x i8] zeroinitializer
192 if (isa<ConstantAggregateZero>(C))
193 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
194 return (Ty->getElementType() == Type::Int8Ty &&
195 Ty->getNumElements() == 1);
200 static SectionKind::Kind SectionKindForGlobal(const GlobalValue *GV,
201 const TargetMachine &TM) {
202 Reloc::Model ReloModel = TM.getRelocationModel();
204 // Early exit - functions should be always in text sections.
205 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
207 return SectionKind::Text;
210 // Handle thread-local data first.
211 if (GVar->isThreadLocal()) {
212 if (isSuitableForBSS(GVar))
213 return SectionKind::ThreadBSS;
214 return SectionKind::ThreadData;
217 // Variable can be easily put to BSS section.
218 if (isSuitableForBSS(GVar))
219 return SectionKind::BSS;
221 Constant *C = GVar->getInitializer();
223 // If the global is marked constant, we can put it into a mergable section,
224 // a mergable string section, or general .data if it contains relocations.
225 if (GVar->isConstant()) {
226 // If the initializer for the global contains something that requires a
227 // relocation, then we may have to drop this into a wriable data section
228 // even though it is marked const.
229 switch (C->getRelocationInfo()) {
230 default: llvm_unreachable("unknown relocation info kind");
231 case Constant::NoRelocation:
232 // If initializer is a null-terminated string, put it in a "cstring"
233 // section if the target has it.
234 if (isConstantString(C))
235 return SectionKind::MergeableCString;
237 // Otherwise, just drop it into a mergable constant section. If we have
238 // a section for this size, use it, otherwise use the arbitrary sized
240 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
241 case 4: return SectionKind::MergeableConst4;
242 case 8: return SectionKind::MergeableConst8;
243 case 16: return SectionKind::MergeableConst16;
244 default: return SectionKind::MergeableConst;
247 case Constant::LocalRelocation:
248 // In static relocation model, the linker will resolve all addresses, so
249 // the relocation entries will actually be constants by the time the app
251 if (ReloModel == Reloc::Static)
252 return SectionKind::ReadOnly;
254 // Otherwise, the dynamic linker needs to fix it up, put it in the
255 // writable data.rel.local section.
256 return SectionKind::ReadOnlyWithRelLocal;
258 case Constant::GlobalRelocations:
259 // In static relocation model, the linker will resolve all addresses, so
260 // the relocation entries will actually be constants by the time the app
262 if (ReloModel == Reloc::Static)
263 return SectionKind::ReadOnly;
265 // Otherwise, the dynamic linker needs to fix it up, put it in the
266 // writable data.rel section.
267 return SectionKind::ReadOnlyWithRel;
271 // Okay, this isn't a constant. If the initializer for the global is going
272 // to require a runtime relocation by the dynamic linker, put it into a more
273 // specific section to improve startup time of the app. This coalesces these
274 // globals together onto fewer pages, improving the locality of the dynamic
276 if (ReloModel == Reloc::Static)
277 return SectionKind::DataNoRel;
279 switch (C->getRelocationInfo()) {
280 default: llvm_unreachable("unknown relocation info kind");
281 case Constant::NoRelocation:
282 return SectionKind::DataNoRel;
283 case Constant::LocalRelocation:
284 return SectionKind::DataRelLocal;
285 case Constant::GlobalRelocations:
286 return SectionKind::DataRel;
290 /// SectionForGlobal - This method computes the appropriate section to emit
291 /// the specified global variable or function definition. This should not
292 /// be passed external (or available externally) globals.
293 const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
294 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
295 "Can only be used for global definitions");
297 SectionKind::Kind GVKind = SectionKindForGlobal(GV, TM);
299 SectionKind Kind = SectionKind::get(GVKind, GV->isWeakForLinker(),
303 // Select section name.
304 if (GV->hasSection()) {
305 // If the target has special section hacks for specifically named globals,
307 if (const Section *TS = getSpecialCasedSectionGlobals(GV, Kind))
310 // If the target has magic semantics for certain section names, make sure to
311 // pick up the flags. This allows the user to write things with attribute
312 // section and still get the appropriate section flags printed.
313 GVKind = getKindForNamedSection(GV->getSection().c_str(), GVKind);
315 return getOrCreateSection(GV->getSection().c_str(), false, GVKind);
318 // If this global is linkonce/weak and the target handles this by emitting it
319 // into a 'uniqued' section name, create and return the section now.
321 if (const char *Prefix = getSectionPrefixForUniqueGlobal(Kind)) {
322 // FIXME: Use mangler interface (PR4584).
323 std::string Name = Prefix+GV->getNameStr();
324 return getOrCreateSection(Name.c_str(), false, GVKind);
328 // Use default section depending on the 'type' of global
329 return SelectSectionForGlobal(GV, Kind);
332 // Lame default implementation. Calculate the section name for global.
334 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
335 SectionKind Kind) const {
336 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
339 return getTextSection();
342 if (const Section *S = getBSSSection_())
345 if (Kind.isReadOnly())
346 if (const Section *S = getReadOnlySection())
349 return getDataSection();
352 /// getSectionForMergableConstant - Given a mergable constant with the
353 /// specified size and relocation information, return a section that it
354 /// should be placed in.
356 TargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const {
357 if (Kind.isReadOnly())
358 if (const Section *S = getReadOnlySection())
361 return getDataSection();
365 const Section *TargetAsmInfo::getOrCreateSection(const char *Name,
367 SectionKind::Kind Kind) const {
368 Section &S = Sections[Name];
370 // This is newly-created section, set it up properly.
371 if (S.Name.empty()) {
372 S.Kind = SectionKind::get(Kind, false /*weak*/, !isDirective);
379 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
383 Size += sizeof(int8_t);
388 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
390 int Sign = Value >> (8 * sizeof(Value) - 1);
394 unsigned Byte = Value & 0x7f;
396 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
397 Size += sizeof(int8_t);