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/TargetOptions.h"
23 #include "llvm/Support/Dwarf.h"
29 void TargetAsmInfo::fillDefaultValues() {
30 BSSSection = "\t.bss";
35 SmallRODataSection = 0;
38 ZeroFillDirective = 0;
39 NonexecutableStackDirective = 0;
46 PrivateGlobalPrefix = ".";
47 LessPrivateGlobalPrefix = "";
48 JumpTableSpecialLabelPrefix = 0;
49 GlobalVarAddrPrefix = "";
50 GlobalVarAddrSuffix = "";
51 FunctionAddrPrefix = "";
52 FunctionAddrSuffix = "";
53 PersonalityPrefix = "";
54 PersonalitySuffix = "";
55 NeedsIndirectEncoding = false;
56 InlineAsmStart = "#APP";
57 InlineAsmEnd = "#NO_APP";
59 StringConstantPrefix = ".str";
60 ZeroDirective = "\t.zero\t";
61 ZeroDirectiveSuffix = 0;
62 AsciiDirective = "\t.ascii\t";
63 AscizDirective = "\t.asciz\t";
64 Data8bitsDirective = "\t.byte\t";
65 Data16bitsDirective = "\t.short\t";
66 Data32bitsDirective = "\t.long\t";
67 Data64bitsDirective = "\t.quad\t";
68 AlignDirective = "\t.align\t";
69 AlignmentIsInBytes = true;
70 TextAlignFillValue = 0;
71 SwitchToSectionDirective = "\t.section\t";
72 TextSectionStartSuffix = "";
73 DataSectionStartSuffix = "";
74 SectionEndDirectiveSuffix = 0;
75 ConstantPoolSection = "\t.section .rodata";
76 JumpTableDataSection = "\t.section .rodata";
77 JumpTableDirective = 0;
80 // FIXME: Flags are ELFish - replace with normal section stuff.
81 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
82 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
83 GlobalDirective = "\t.globl\t";
86 COMMDirective = "\t.comm\t";
87 COMMDirectiveTakesAlignment = true;
88 HasDotTypeDotSizeDirective = true;
89 HasSingleParameterDotFile = true;
93 // FIXME: These are ELFish - move to ELFTAI.
94 HiddenDirective = "\t.hidden\t";
95 ProtectedDirective = "\t.protected\t";
96 AbsoluteDebugSectionOffsets = false;
97 AbsoluteEHSectionOffsets = false;
99 HasDotLocAndDotFile = false;
100 SupportsDebugInformation = false;
101 SupportsExceptionHandling = false;
102 DwarfRequiresFrameSection = true;
103 SupportsMacInfoSection = true;
104 NonLocalEHFrameLabel = false;
105 GlobalEHDirective = 0;
106 SupportsWeakOmittedEHFrame = true;
107 DwarfSectionOffsetDirective = 0;
108 DwarfAbbrevSection = ".debug_abbrev";
109 DwarfInfoSection = ".debug_info";
110 DwarfLineSection = ".debug_line";
111 DwarfFrameSection = ".debug_frame";
112 DwarfPubNamesSection = ".debug_pubnames";
113 DwarfPubTypesSection = ".debug_pubtypes";
114 DwarfStrSection = ".debug_str";
115 DwarfLocSection = ".debug_loc";
116 DwarfARangesSection = ".debug_aranges";
117 DwarfRangesSection = ".debug_ranges";
118 DwarfMacInfoSection = ".debug_macinfo";
119 DwarfEHFrameSection = ".eh_frame";
120 DwarfExceptionSection = ".gcc_except_table";
122 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
123 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
126 TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm)
131 TargetAsmInfo::~TargetAsmInfo() {
134 /// Measure the specified inline asm to determine an approximation of its
136 /// Comments (which run till the next SeparatorChar or newline) do not
137 /// count as an instruction.
138 /// Any other non-whitespace text is considered an instruction, with
139 /// multiple instructions separated by SeparatorChar or newlines.
140 /// Variable-length instructions are not handled here; this function
141 /// may be overloaded in the target code to do that.
142 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
143 // Count the number of instructions in the asm.
144 bool atInsnStart = true;
146 for (; *Str; ++Str) {
147 if (*Str == '\n' || *Str == SeparatorChar)
149 if (atInsnStart && !isspace(*Str)) {
150 Length += MaxInstLength;
153 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
160 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
162 return dwarf::DW_EH_PE_absptr;
165 static bool isSuitableForBSS(const GlobalVariable *GV) {
166 if (!GV->hasInitializer())
169 // Leave constant zeros in readonly constant sections, so they can be shared
170 Constant *C = GV->getInitializer();
171 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
174 static bool isConstantString(const Constant *C) {
175 // First check: is we have constant array of i8 terminated with zero
176 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
177 // Check, if initializer is a null-terminated string
178 if (CVA && CVA->isCString())
181 // Another possibility: [1 x i8] zeroinitializer
182 if (isa<ConstantAggregateZero>(C)) {
183 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) {
184 return (Ty->getElementType() == Type::Int8Ty &&
185 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 varible 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->ContainsRelocations())
212 return SectionKind::ROData;
214 // Check, if initializer is a null-terminated string
215 if (isConstantString(C))
216 return SectionKind::RODataMergeStr;
218 return SectionKind::RODataMergeConst;
222 // Variable is not constant or thread-local - emit to generic data section.
223 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
227 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
228 const char* Name) const {
229 unsigned Flags = SectionFlags::None;
231 // Decode flags from global itself.
233 SectionKind::Kind Kind = SectionKindForGlobal(GV);
235 case SectionKind::Text:
236 Flags |= SectionFlags::Code;
238 case SectionKind::ThreadData:
239 case SectionKind::ThreadBSS:
240 Flags |= SectionFlags::TLS;
242 case SectionKind::Data:
243 case SectionKind::BSS:
244 Flags |= SectionFlags::Writeable;
246 case SectionKind::ROData:
247 case SectionKind::RODataMergeStr:
248 case SectionKind::RODataMergeConst:
249 // No additional flags here
251 case SectionKind::SmallData:
252 case SectionKind::SmallBSS:
253 Flags |= SectionFlags::Writeable;
255 case SectionKind::SmallROData:
256 Flags |= SectionFlags::Small;
259 assert(0 && "Unexpected section kind!");
262 if (GV->mayBeOverridden())
263 Flags |= SectionFlags::Linkonce;
266 // Add flags from sections, if any.
268 Flags |= SectionFlags::Named;
270 // Some lame default implementation based on some magic section names.
271 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
272 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
273 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
274 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
275 Flags |= SectionFlags::BSS;
276 else if (strcmp(Name, ".tdata") == 0 ||
277 strncmp(Name, ".tdata.", 7) == 0 ||
278 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
279 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
280 Flags |= SectionFlags::TLS;
281 else if (strcmp(Name, ".tbss") == 0 ||
282 strncmp(Name, ".tbss.", 6) == 0 ||
283 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
284 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
285 Flags |= SectionFlags::BSS | SectionFlags::TLS;
292 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
294 // Select section name
295 if (GV->hasSection()) {
296 // Honour section already set, if any
297 unsigned Flags = SectionFlagsForGlobal(GV,
298 GV->getSection().c_str());
299 S = getNamedSection(GV->getSection().c_str(), Flags);
301 // Use default section depending on the 'type' of global
302 S = SelectSectionForGlobal(GV);
308 // Lame default implementation. Calculate the section name for global.
310 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
311 SectionKind::Kind Kind = SectionKindForGlobal(GV);
313 if (GV->mayBeOverridden()) {
314 std::string Name = UniqueSectionForGlobal(GV, Kind);
315 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
316 return getNamedSection(Name.c_str(), Flags);
318 if (Kind == SectionKind::Text)
319 return getTextSection();
320 else if (isBSS(Kind) && getBSSSection_())
321 return getBSSSection_();
322 else if (getReadOnlySection() && SectionKind::isReadOnly(Kind))
323 return getReadOnlySection();
326 return getDataSection();
329 // Lame default implementation. Calculate the section name for machine const.
331 TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
332 // FIXME: Support data.rel stuff someday
333 return getDataSection();
337 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
338 SectionKind::Kind Kind) const {
340 case SectionKind::Text:
341 return ".gnu.linkonce.t." + GV->getName();
342 case SectionKind::Data:
343 return ".gnu.linkonce.d." + GV->getName();
344 case SectionKind::SmallData:
345 return ".gnu.linkonce.s." + GV->getName();
346 case SectionKind::BSS:
347 return ".gnu.linkonce.b." + GV->getName();
348 case SectionKind::SmallBSS:
349 return ".gnu.linkonce.sb." + GV->getName();
350 case SectionKind::ROData:
351 case SectionKind::RODataMergeConst:
352 case SectionKind::RODataMergeStr:
353 return ".gnu.linkonce.r." + GV->getName();
354 case SectionKind::SmallROData:
355 return ".gnu.linkonce.s2." + GV->getName();
356 case SectionKind::ThreadData:
357 return ".gnu.linkonce.td." + GV->getName();
358 case SectionKind::ThreadBSS:
359 return ".gnu.linkonce.tb." + GV->getName();
361 assert(0 && "Unknown section kind");
367 TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
368 bool Override) const {
369 Section& S = Sections[Name];
371 // This is newly-created section, set it up properly.
372 if (S.Flags == SectionFlags::Invalid || Override) {
373 S.Flags = Flags | SectionFlags::Named;
381 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
382 bool Override) const {
383 Section& S = Sections[Directive];
385 // This is newly-created section, set it up properly.
386 if (S.Flags == SectionFlags::Invalid || Override) {
387 S.Flags = Flags & ~SectionFlags::Named;
395 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
396 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
398 // We didn't print these flags yet, print and save them to map. This reduces
399 // amount of heap trashing due to std::string construction / concatenation.
400 if (I == FlagsStrings.end())
401 I = FlagsStrings.insert(std::make_pair(Flags,
402 printSectionFlags(Flags))).first;
407 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
411 Size += sizeof(int8_t);
416 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
418 int Sign = Value >> (8 * sizeof(Value) - 1);
422 unsigned Byte = Value & 0x7f;
424 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
425 Size += sizeof(int8_t);