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/TargetMachine.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/ErrorHandling.h"
30 TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm)
33 BSSSection = "\t.bss";
38 ZeroFillDirective = 0;
39 NonexecutableStackDirective = 0;
46 FirstOperandColumn = 0;
49 PrivateGlobalPrefix = ".";
50 LinkerPrivateGlobalPrefix = "";
51 JumpTableSpecialLabelPrefix = 0;
52 GlobalVarAddrPrefix = "";
53 GlobalVarAddrSuffix = "";
54 FunctionAddrPrefix = "";
55 FunctionAddrSuffix = "";
56 PersonalityPrefix = "";
57 PersonalitySuffix = "";
58 NeedsIndirectEncoding = false;
59 InlineAsmStart = "#APP";
60 InlineAsmEnd = "#NO_APP";
62 AllowQuotesInName = false;
63 ZeroDirective = "\t.zero\t";
64 ZeroDirectiveSuffix = 0;
65 AsciiDirective = "\t.ascii\t";
66 AscizDirective = "\t.asciz\t";
67 Data8bitsDirective = "\t.byte\t";
68 Data16bitsDirective = "\t.short\t";
69 Data32bitsDirective = "\t.long\t";
70 Data64bitsDirective = "\t.quad\t";
71 AlignDirective = "\t.align\t";
72 AlignmentIsInBytes = true;
73 TextAlignFillValue = 0;
74 SwitchToSectionDirective = "\t.section\t";
75 TextSectionStartSuffix = "";
76 DataSectionStartSuffix = "";
77 SectionEndDirectiveSuffix = 0;
78 ConstantPoolSection = "\t.section .rodata";
79 JumpTableDataSection = "\t.section .rodata";
80 JumpTableDirective = 0;
83 // FIXME: Flags are ELFish - replace with normal section stuff.
84 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
85 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
86 GlobalDirective = "\t.globl\t";
89 COMMDirective = "\t.comm\t";
90 COMMDirectiveTakesAlignment = true;
91 HasDotTypeDotSizeDirective = true;
92 HasSingleParameterDotFile = true;
96 // FIXME: These are ELFish - move to ELFTAI.
97 HiddenDirective = "\t.hidden\t";
98 ProtectedDirective = "\t.protected\t";
99 AbsoluteDebugSectionOffsets = false;
100 AbsoluteEHSectionOffsets = false;
102 HasDotLocAndDotFile = false;
103 SupportsDebugInformation = false;
104 SupportsExceptionHandling = false;
105 DwarfRequiresFrameSection = true;
106 DwarfUsesInlineInfoSection = false;
107 Is_EHSymbolPrivate = true;
108 GlobalEHDirective = 0;
109 SupportsWeakOmittedEHFrame = true;
110 DwarfSectionOffsetDirective = 0;
111 DwarfAbbrevSection = ".debug_abbrev";
112 DwarfInfoSection = ".debug_info";
113 DwarfLineSection = ".debug_line";
114 DwarfFrameSection = ".debug_frame";
115 DwarfPubNamesSection = ".debug_pubnames";
116 DwarfPubTypesSection = ".debug_pubtypes";
117 DwarfDebugInlineSection = ".debug_inlined";
118 DwarfStrSection = ".debug_str";
119 DwarfLocSection = ".debug_loc";
120 DwarfARangesSection = ".debug_aranges";
121 DwarfRangesSection = ".debug_ranges";
122 DwarfMacroInfoSection = ".debug_macinfo";
123 DwarfEHFrameSection = ".eh_frame";
124 DwarfExceptionSection = ".gcc_except_table";
126 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
127 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
130 TargetAsmInfo::~TargetAsmInfo() {
133 /// Measure the specified inline asm to determine an approximation of its
135 /// Comments (which run till the next SeparatorChar or newline) do not
136 /// count as an instruction.
137 /// Any other non-whitespace text is considered an instruction, with
138 /// multiple instructions separated by SeparatorChar or newlines.
139 /// Variable-length instructions are not handled here; this function
140 /// may be overloaded in the target code to do that.
141 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
142 // Count the number of instructions in the asm.
143 bool atInsnStart = true;
145 for (; *Str; ++Str) {
146 if (*Str == '\n' || *Str == SeparatorChar)
148 if (atInsnStart && !isspace(*Str)) {
149 Length += MaxInstLength;
152 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
159 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
161 return dwarf::DW_EH_PE_absptr;
164 static bool isSuitableForBSS(const GlobalVariable *GV) {
165 if (!GV->hasInitializer())
168 // Leave constant zeros in readonly constant sections, so they can be shared
169 Constant *C = GV->getInitializer();
170 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
173 static bool isConstantString(const Constant *C) {
174 // First check: is we have constant array of i8 terminated with zero
175 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
176 // Check, if initializer is a null-terminated string
177 if (CVA && CVA->isCString())
180 // Another possibility: [1 x i8] zeroinitializer
181 if (isa<ConstantAggregateZero>(C)) {
182 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) {
183 return (Ty->getElementType() == Type::Int8Ty &&
184 Ty->getNumElements() == 1);
191 static unsigned SectionFlagsForGlobal(const GlobalValue *GV,
192 SectionKind::Kind Kind) {
193 unsigned Flags = SectionFlags::None;
195 // Decode flags from global itself.
197 case SectionKind::Text:
198 Flags |= SectionFlags::Code;
200 case SectionKind::ThreadData:
201 case SectionKind::ThreadBSS:
202 Flags |= SectionFlags::TLS;
204 case SectionKind::Data:
205 case SectionKind::DataRel:
206 case SectionKind::DataRelLocal:
207 case SectionKind::DataRelRO:
208 case SectionKind::DataRelROLocal:
209 case SectionKind::BSS:
210 Flags |= SectionFlags::Writeable;
212 case SectionKind::ROData:
213 case SectionKind::RODataMergeStr:
214 case SectionKind::RODataMergeConst:
215 // No additional flags here
218 llvm_unreachable("Unexpected section kind!");
221 if (GV->isWeakForLinker())
222 Flags |= SectionFlags::Linkonce;
228 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
229 // Early exit - functions should be always in text sections.
230 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
232 return SectionKind::Text;
234 bool isThreadLocal = GVar->isThreadLocal();
236 // Variable can be easily put to BSS section.
237 if (isSuitableForBSS(GVar))
238 return isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS;
240 // If this is thread-local, put it in the general "thread_data" section.
242 return SectionKind::ThreadData;
244 Constant *C = GVar->getInitializer();
246 // If the global is marked constant, we can put it into a mergable section,
247 // a mergable string section, or general .data if it contains relocations.
248 if (GVar->isConstant()) {
249 // If the initializer for the global contains something that requires a
250 // relocation, then we may have to drop this into a wriable data section
251 // even though it is marked const.
252 switch (C->getRelocationInfo()) {
253 default: llvm_unreachable("unknown relocation info kind");
254 case Constant::NoRelocation:
255 // If initializer is a null-terminated string, put it in a "cstring"
256 // section if the target has it.
257 if (isConstantString(C))
258 return SectionKind::RODataMergeStr;
260 // Otherwise, just drop it into a mergable constant section.
261 return SectionKind::RODataMergeConst;
263 case Constant::LocalRelocation:
264 // In static relocation model, the linker will resolve all addresses, so
265 // the relocation entries will actually be constants by the time the app
267 if (TM.getRelocationModel() == Reloc::Static)
268 return SectionKind::ROData;
270 // Otherwise, the dynamic linker needs to fix it up, put it in the
271 // writable data.rel.local section.
272 return SectionKind::DataRelROLocal;
274 case Constant::GlobalRelocations:
275 // In static relocation model, the linker will resolve all addresses, so
276 // the relocation entries will actually be constants by the time the app
278 if (TM.getRelocationModel() == Reloc::Static)
279 return SectionKind::ROData;
281 // Otherwise, the dynamic linker needs to fix it up, put it in the
282 // writable data.rel section.
283 return SectionKind::DataRelRO;
287 // Okay, this isn't a constant. If the initializer for the global is going
288 // to require a runtime relocation by the dynamic linker, put it into a more
289 // specific section to improve startup time of the app. This coalesces these
290 // globals together onto fewer pages, improving the locality of the dynamic
292 if (TM.getRelocationModel() == Reloc::Static)
293 return SectionKind::Data;
295 switch (C->getRelocationInfo()) {
296 default: llvm_unreachable("unknown relocation info kind");
297 case Constant::NoRelocation: return SectionKind::Data;
298 case Constant::LocalRelocation: return SectionKind::DataRelLocal;
299 case Constant::GlobalRelocations: return SectionKind::DataRel;
304 const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
305 SectionKind::Kind Kind = SectionKindForGlobal(GV);
307 // Select section name.
308 if (GV->hasSection()) {
310 // If the target has special section hacks for specifically named globals,
312 if (const Section *TS = getSpecialCasedSectionGlobals(GV, Kind))
315 // Honour section already set, if any.
316 unsigned Flags = SectionFlagsForGlobal(GV, Kind);
318 // This is an explicitly named section.
319 Flags |= SectionFlags::Named;
321 // If the target has magic semantics for certain section names, make sure to
322 // pick up the flags. This allows the user to write things with attribute
323 // section and still get the appropriate section flags printed.
324 Flags |= getFlagsForNamedSection(GV->getSection().c_str());
326 return getNamedSection(GV->getSection().c_str(), Flags);
329 // If this global is linkonce/weak and the target handles this by emitting it
330 // into a 'uniqued' section name, create and return the section now.
331 if (GV->isWeakForLinker()) {
332 if (const char *Prefix = getSectionPrefixForUniqueGlobal(Kind)) {
333 unsigned Flags = SectionFlagsForGlobal(GV, Kind);
335 // FIXME: Use mangler interface (PR4584).
336 std::string Name = Prefix+GV->getNameStr();
337 return getNamedSection(Name.c_str(), Flags);
341 // Use default section depending on the 'type' of global
342 return SelectSectionForGlobal(GV, Kind);
345 // Lame default implementation. Calculate the section name for global.
347 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
348 SectionKind::Kind Kind) const {
349 if (Kind == SectionKind::Text)
350 return getTextSection();
353 if (const Section *S = getBSSSection_())
356 if (SectionKind::isReadOnly(Kind))
357 if (const Section *S = getReadOnlySection())
360 return getDataSection();
363 /// getSectionForMergableConstant - Given a mergable constant with the
364 /// specified size and relocation information, return a section that it
365 /// should be placed in.
367 TargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
368 unsigned ReloInfo) const {
369 // FIXME: Support data.rel stuff someday
370 // Lame default implementation. Calculate the section name for machine const.
371 return getDataSection();
378 TargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind::Kind Kind) const {
380 default: llvm_unreachable("Unknown section kind");
381 case SectionKind::Text: return ".gnu.linkonce.t.";
382 case SectionKind::Data: return ".gnu.linkonce.d.";
383 case SectionKind::DataRel: return ".gnu.linkonce.d.rel.";
384 case SectionKind::DataRelLocal: return ".gnu.linkonce.d.rel.local.";
385 case SectionKind::DataRelRO: return ".gnu.linkonce.d.rel.ro.";
386 case SectionKind::DataRelROLocal: return ".gnu.linkonce.d.rel.ro.local.";
387 case SectionKind::BSS: return ".gnu.linkonce.b.";
388 case SectionKind::ROData:
389 case SectionKind::RODataMergeConst:
390 case SectionKind::RODataMergeStr: return ".gnu.linkonce.r.";
391 case SectionKind::ThreadData: return ".gnu.linkonce.td.";
392 case SectionKind::ThreadBSS: return ".gnu.linkonce.tb.";
396 const Section *TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
397 bool Override) const {
398 Section &S = Sections[Name];
400 // This is newly-created section, set it up properly.
401 if (S.Flags == SectionFlags::Invalid || Override) {
402 S.Flags = Flags | SectionFlags::Named;
410 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
411 bool Override) const {
412 Section& S = Sections[Directive];
414 // This is newly-created section, set it up properly.
415 if (S.Flags == SectionFlags::Invalid || Override) {
416 S.Flags = Flags & ~SectionFlags::Named;
424 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
425 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
427 // We didn't print these flags yet, print and save them to map. This reduces
428 // amount of heap trashing due to std::string construction / concatenation.
429 if (I == FlagsStrings.end())
430 I = FlagsStrings.insert(std::make_pair(Flags,
431 printSectionFlags(Flags))).first;
436 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
440 Size += sizeof(int8_t);
445 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
447 int Sign = Value >> (8 * sizeof(Value) - 1);
451 unsigned Byte = Value & 0x7f;
453 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
454 Size += sizeof(int8_t);