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)
32 BSSSection = "\t.bss";
37 SmallRODataSection = 0;
40 ZeroFillDirective = 0;
41 NonexecutableStackDirective = 0;
48 FirstOperandColumn = 0;
51 PrivateGlobalPrefix = ".";
52 LinkerPrivateGlobalPrefix = "";
53 JumpTableSpecialLabelPrefix = 0;
54 GlobalVarAddrPrefix = "";
55 GlobalVarAddrSuffix = "";
56 FunctionAddrPrefix = "";
57 FunctionAddrSuffix = "";
58 PersonalityPrefix = "";
59 PersonalitySuffix = "";
60 NeedsIndirectEncoding = false;
61 InlineAsmStart = "#APP";
62 InlineAsmEnd = "#NO_APP";
64 AllowQuotesInName = false;
65 ZeroDirective = "\t.zero\t";
66 ZeroDirectiveSuffix = 0;
67 AsciiDirective = "\t.ascii\t";
68 AscizDirective = "\t.asciz\t";
69 Data8bitsDirective = "\t.byte\t";
70 Data16bitsDirective = "\t.short\t";
71 Data32bitsDirective = "\t.long\t";
72 Data64bitsDirective = "\t.quad\t";
73 AlignDirective = "\t.align\t";
74 AlignmentIsInBytes = true;
75 TextAlignFillValue = 0;
76 SwitchToSectionDirective = "\t.section\t";
77 TextSectionStartSuffix = "";
78 DataSectionStartSuffix = "";
79 SectionEndDirectiveSuffix = 0;
80 ConstantPoolSection = "\t.section .rodata";
81 JumpTableDataSection = "\t.section .rodata";
82 JumpTableDirective = 0;
85 // FIXME: Flags are ELFish - replace with normal section stuff.
86 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
87 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
88 GlobalDirective = "\t.globl\t";
91 COMMDirective = "\t.comm\t";
92 COMMDirectiveTakesAlignment = true;
93 HasDotTypeDotSizeDirective = true;
94 HasSingleParameterDotFile = true;
98 // FIXME: These are ELFish - move to ELFTAI.
99 HiddenDirective = "\t.hidden\t";
100 ProtectedDirective = "\t.protected\t";
101 AbsoluteDebugSectionOffsets = false;
102 AbsoluteEHSectionOffsets = false;
104 HasDotLocAndDotFile = false;
105 SupportsDebugInformation = false;
106 SupportsExceptionHandling = false;
107 DwarfRequiresFrameSection = true;
108 DwarfUsesInlineInfoSection = false;
109 Is_EHSymbolPrivate = true;
110 GlobalEHDirective = 0;
111 SupportsWeakOmittedEHFrame = true;
112 DwarfSectionOffsetDirective = 0;
113 DwarfAbbrevSection = ".debug_abbrev";
114 DwarfInfoSection = ".debug_info";
115 DwarfLineSection = ".debug_line";
116 DwarfFrameSection = ".debug_frame";
117 DwarfPubNamesSection = ".debug_pubnames";
118 DwarfPubTypesSection = ".debug_pubtypes";
119 DwarfDebugInlineSection = ".debug_inlined";
120 DwarfStrSection = ".debug_str";
121 DwarfLocSection = ".debug_loc";
122 DwarfARangesSection = ".debug_aranges";
123 DwarfRangesSection = ".debug_ranges";
124 DwarfMacroInfoSection = ".debug_macinfo";
125 DwarfEHFrameSection = ".eh_frame";
126 DwarfExceptionSection = ".gcc_except_table";
128 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
129 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
132 TargetAsmInfo::~TargetAsmInfo() {
135 /// Measure the specified inline asm to determine an approximation of its
137 /// Comments (which run till the next SeparatorChar or newline) do not
138 /// count as an instruction.
139 /// Any other non-whitespace text is considered an instruction, with
140 /// multiple instructions separated by SeparatorChar or newlines.
141 /// Variable-length instructions are not handled here; this function
142 /// may be overloaded in the target code to do that.
143 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
144 // Count the number of instructions in the asm.
145 bool atInsnStart = true;
147 for (; *Str; ++Str) {
148 if (*Str == '\n' || *Str == SeparatorChar)
150 if (atInsnStart && !isspace(*Str)) {
151 Length += MaxInstLength;
154 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
161 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
163 return dwarf::DW_EH_PE_absptr;
166 static bool isSuitableForBSS(const GlobalVariable *GV) {
167 if (!GV->hasInitializer())
170 // Leave constant zeros in readonly constant sections, so they can be shared
171 Constant *C = GV->getInitializer();
172 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
175 static bool isConstantString(const Constant *C) {
176 // First check: is we have constant array of i8 terminated with zero
177 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
178 // Check, if initializer is a null-terminated string
179 if (CVA && CVA->isCString())
182 // Another possibility: [1 x i8] zeroinitializer
183 if (isa<ConstantAggregateZero>(C)) {
184 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) {
185 return (Ty->getElementType() == Type::Int8Ty &&
186 Ty->getNumElements() == 1);
194 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
195 // Early exit - functions should be always in text sections.
196 if (isa<Function>(GV))
197 return SectionKind::Text;
199 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
200 bool isThreadLocal = GVar->isThreadLocal();
201 assert(GVar && "Invalid global value for section selection");
203 if (isSuitableForBSS(GVar)) {
204 // Variable can be easily put to BSS section.
205 return isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS;
206 } else if (GVar->isConstant() && !isThreadLocal) {
207 // Now we know, that variable has initializer and it is constant. We need to
208 // check its initializer to decide, which section to output it into. Also
209 // note, there is no thread-local r/o section.
210 Constant *C = GVar->getInitializer();
211 if (C->getRelocationInfo() != 0) {
212 // Decide whether it is still possible to put symbol into r/o section.
213 if (TM.getRelocationModel() != Reloc::Static)
214 return SectionKind::Data;
216 return SectionKind::ROData;
218 // Check, if initializer is a null-terminated string
219 if (isConstantString(C))
220 return SectionKind::RODataMergeStr;
222 return SectionKind::RODataMergeConst;
226 // Variable either is not constant or thread-local - output to data section.
227 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
231 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
232 const char* Name) const {
233 unsigned Flags = SectionFlags::None;
235 // Decode flags from global itself.
237 SectionKind::Kind Kind = SectionKindForGlobal(GV);
239 case SectionKind::Text:
240 Flags |= SectionFlags::Code;
242 case SectionKind::ThreadData:
243 case SectionKind::ThreadBSS:
244 Flags |= SectionFlags::TLS;
246 case SectionKind::Data:
247 case SectionKind::DataRel:
248 case SectionKind::DataRelLocal:
249 case SectionKind::DataRelRO:
250 case SectionKind::DataRelROLocal:
251 case SectionKind::BSS:
252 Flags |= SectionFlags::Writeable;
254 case SectionKind::ROData:
255 case SectionKind::RODataMergeStr:
256 case SectionKind::RODataMergeConst:
257 // No additional flags here
259 case SectionKind::SmallData:
260 case SectionKind::SmallBSS:
261 Flags |= SectionFlags::Writeable;
263 case SectionKind::SmallROData:
264 Flags |= SectionFlags::Small;
267 llvm_unreachable("Unexpected section kind!");
270 if (GV->isWeakForLinker())
271 Flags |= SectionFlags::Linkonce;
274 // Add flags from sections, if any.
276 Flags |= SectionFlags::Named;
278 // Some lame default implementation based on some magic section names.
279 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
280 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
281 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
282 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
283 Flags |= SectionFlags::BSS;
284 else if (strcmp(Name, ".tdata") == 0 ||
285 strncmp(Name, ".tdata.", 7) == 0 ||
286 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
287 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
288 Flags |= SectionFlags::TLS;
289 else if (strcmp(Name, ".tbss") == 0 ||
290 strncmp(Name, ".tbss.", 6) == 0 ||
291 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
292 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
293 Flags |= SectionFlags::BSS | SectionFlags::TLS;
299 const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
300 // Select section name
301 if (GV->hasSection()) {
302 // Honour section already set, if any.
303 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
304 return getNamedSection(GV->getSection().c_str(), Flags);
307 // Use default section depending on the 'type' of global
308 return SelectSectionForGlobal(GV);
311 // Lame default implementation. Calculate the section name for global.
313 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
314 SectionKind::Kind Kind = SectionKindForGlobal(GV);
316 if (GV->isWeakForLinker()) {
317 std::string Name = UniqueSectionForGlobal(GV, Kind);
318 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
319 return getNamedSection(Name.c_str(), Flags);
321 if (Kind == SectionKind::Text)
322 return getTextSection();
323 else if (isBSS(Kind) && getBSSSection_())
324 return getBSSSection_();
325 else if (getReadOnlySection() && SectionKind::isReadOnly(Kind))
326 return getReadOnlySection();
329 return getDataSection();
332 /// getSectionForMergableConstant - Given a mergable constant with the
333 /// specified size and relocation information, return a section that it
334 /// should be placed in.
336 TargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
337 unsigned ReloInfo) const {
338 // FIXME: Support data.rel stuff someday
339 // Lame default implementation. Calculate the section name for machine const.
340 return getDataSection();
347 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
348 SectionKind::Kind Kind) const {
350 case SectionKind::Text:
351 return ".gnu.linkonce.t." + GV->getName();
352 case SectionKind::Data:
353 return ".gnu.linkonce.d." + GV->getName();
354 case SectionKind::DataRel:
355 return ".gnu.linkonce.d.rel" + GV->getName();
356 case SectionKind::DataRelLocal:
357 return ".gnu.linkonce.d.rel.local" + GV->getName();
358 case SectionKind::DataRelRO:
359 return ".gnu.linkonce.d.rel.ro" + GV->getName();
360 case SectionKind::DataRelROLocal:
361 return ".gnu.linkonce.d.rel.ro.local" + GV->getName();
362 case SectionKind::SmallData:
363 return ".gnu.linkonce.s." + GV->getName();
364 case SectionKind::BSS:
365 return ".gnu.linkonce.b." + GV->getName();
366 case SectionKind::SmallBSS:
367 return ".gnu.linkonce.sb." + GV->getName();
368 case SectionKind::ROData:
369 case SectionKind::RODataMergeConst:
370 case SectionKind::RODataMergeStr:
371 return ".gnu.linkonce.r." + GV->getName();
372 case SectionKind::SmallROData:
373 return ".gnu.linkonce.s2." + GV->getName();
374 case SectionKind::ThreadData:
375 return ".gnu.linkonce.td." + GV->getName();
376 case SectionKind::ThreadBSS:
377 return ".gnu.linkonce.tb." + GV->getName();
379 llvm_unreachable("Unknown section kind");
384 const Section *TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
385 bool Override) const {
386 Section &S = Sections[Name];
388 // This is newly-created section, set it up properly.
389 if (S.Flags == SectionFlags::Invalid || Override) {
390 S.Flags = Flags | SectionFlags::Named;
398 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
399 bool Override) const {
400 Section& S = Sections[Directive];
402 // This is newly-created section, set it up properly.
403 if (S.Flags == SectionFlags::Invalid || Override) {
404 S.Flags = Flags & ~SectionFlags::Named;
412 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
413 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
415 // We didn't print these flags yet, print and save them to map. This reduces
416 // amount of heap trashing due to std::string construction / concatenation.
417 if (I == FlagsStrings.end())
418 I = FlagsStrings.insert(std::make_pair(Flags,
419 printSectionFlags(Flags))).first;
424 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
428 Size += sizeof(int8_t);
433 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
435 int Sign = Value >> (8 * sizeof(Value) - 1);
439 unsigned Byte = Value & 0x7f;
441 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
442 Size += sizeof(int8_t);