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/GlobalVariable.h"
17 #include "llvm/Function.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Support/Dwarf.h"
28 TargetAsmInfo::TargetAsmInfo() :
29 TextSection("\t.text"),
30 DataSection("\t.data"),
32 TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
33 TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
35 NonexecutableStackDirective(0),
42 PrivateGlobalPrefix("."),
43 JumpTableSpecialLabelPrefix(0),
44 GlobalVarAddrPrefix(""),
45 GlobalVarAddrSuffix(""),
46 FunctionAddrPrefix(""),
47 FunctionAddrSuffix(""),
48 PersonalityPrefix(""),
49 PersonalitySuffix(""),
50 NeedsIndirectEncoding(false),
51 InlineAsmStart("#APP"),
52 InlineAsmEnd("#NO_APP"),
54 StringConstantPrefix(".str"),
55 ZeroDirective("\t.zero\t"),
56 ZeroDirectiveSuffix(0),
57 AsciiDirective("\t.ascii\t"),
58 AscizDirective("\t.asciz\t"),
59 Data8bitsDirective("\t.byte\t"),
60 Data16bitsDirective("\t.short\t"),
61 Data32bitsDirective("\t.long\t"),
62 Data64bitsDirective("\t.quad\t"),
63 AlignDirective("\t.align\t"),
64 AlignmentIsInBytes(true),
65 TextAlignFillValue(0),
66 SwitchToSectionDirective("\t.section\t"),
67 TextSectionStartSuffix(""),
68 DataSectionStartSuffix(""),
69 SectionEndDirectiveSuffix(0),
70 ConstantPoolSection("\t.section .rodata"),
71 JumpTableDataSection("\t.section .rodata"),
72 JumpTableDirective(0),
74 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
75 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
76 FourByteConstantSection(0),
77 EightByteConstantSection(0),
78 SixteenByteConstantSection(0),
80 GlobalDirective("\t.globl\t"),
83 COMMDirective("\t.comm\t"),
84 COMMDirectiveTakesAlignment(true),
85 HasDotTypeDotSizeDirective(true),
89 HiddenDirective("\t.hidden\t"),
90 ProtectedDirective("\t.protected\t"),
91 AbsoluteDebugSectionOffsets(false),
92 AbsoluteEHSectionOffsets(false),
94 HasDotLocAndDotFile(false),
95 SupportsDebugInformation(false),
96 SupportsExceptionHandling(false),
97 DwarfRequiresFrameSection(true),
99 SupportsWeakOmittedEHFrame(true),
100 DwarfSectionOffsetDirective(0),
101 DwarfAbbrevSection(".debug_abbrev"),
102 DwarfInfoSection(".debug_info"),
103 DwarfLineSection(".debug_line"),
104 DwarfFrameSection(".debug_frame"),
105 DwarfPubNamesSection(".debug_pubnames"),
106 DwarfPubTypesSection(".debug_pubtypes"),
107 DwarfStrSection(".debug_str"),
108 DwarfLocSection(".debug_loc"),
109 DwarfARangesSection(".debug_aranges"),
110 DwarfRangesSection(".debug_ranges"),
111 DwarfMacInfoSection(".debug_macinfo"),
112 DwarfEHFrameSection(".eh_frame"),
113 DwarfExceptionSection(".gcc_except_table"),
117 TargetAsmInfo::~TargetAsmInfo() {
120 /// Measure the specified inline asm to determine an approximation of its
122 /// Comments (which run till the next SeparatorChar or newline) do not
123 /// count as an instruction.
124 /// Any other non-whitespace text is considered an instruction, with
125 /// multiple instructions separated by SeparatorChar or newlines.
126 /// Variable-length instructions are not handled here; this function
127 /// may be overloaded in the target code to do that.
128 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
129 // Count the number of instructions in the asm.
130 bool atInsnStart = true;
132 for (; *Str; ++Str) {
133 if (*Str == '\n' || *Str == SeparatorChar)
135 if (atInsnStart && !isspace(*Str)) {
136 Length += MaxInstLength;
139 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
146 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
148 return dwarf::DW_EH_PE_absptr;
151 static bool isSuitableForBSS(const GlobalVariable *GV) {
152 if (!GV->hasInitializer())
155 // Leave constant zeros in readonly constant sections, so they can be shared
156 Constant *C = GV->getInitializer();
157 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
161 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
162 // Early exit - functions should be always in text sections.
163 if (isa<Function>(GV))
164 return SectionKind::Text;
166 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
167 bool isThreadLocal = GVar->isThreadLocal();
168 assert(GVar && "Invalid global value for section selection");
170 if (isSuitableForBSS(GVar)) {
171 // Variable can be easily put to BSS section.
172 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
173 } else if (GVar->isConstant() && !isThreadLocal) {
174 // Now we know, that varible has initializer and it is constant. We need to
175 // check its initializer to decide, which section to output it into. Also
176 // note, there is no thread-local r/o section.
177 Constant *C = GVar->getInitializer();
178 if (C->ContainsRelocations())
179 return SectionKind::ROData;
181 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
182 // Check, if initializer is a null-terminated string
183 if (CVA && CVA->isCString())
184 return SectionKind::RODataMergeStr;
186 return SectionKind::RODataMergeConst;
190 // Variable is not constant or thread-local - emit to generic data section.
191 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
195 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
196 const char* Name) const {
197 unsigned Flags = SectionFlags::None;
199 // Decode flags from global itself.
201 SectionKind::Kind Kind = SectionKindForGlobal(GV);
203 case SectionKind::Text:
204 Flags |= SectionFlags::Code;
206 case SectionKind::ThreadData:
207 Flags |= SectionFlags::TLS;
209 case SectionKind::Data:
210 Flags |= SectionFlags::Writeable;
212 case SectionKind::ThreadBSS:
213 Flags |= SectionFlags::TLS;
215 case SectionKind::BSS:
216 Flags |= SectionFlags::BSS;
218 case SectionKind::ROData:
219 // No additional flags here
221 case SectionKind::RODataMergeStr:
222 Flags |= SectionFlags::Strings;
224 case SectionKind::RODataMergeConst:
225 Flags |= SectionFlags::Mergeable;
228 assert(0 && "Unexpected section kind!");
231 if (GV->isWeakForLinker())
232 Flags |= SectionFlags::Linkonce;
235 // Add flags from sections, if any.
237 Flags |= SectionFlags::Named;
239 // Some lame default implementation based on some magic section names.
240 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
241 strncmp(Name, ".llvm.linkonce.b.", 17) == 0)
242 Flags |= SectionFlags::BSS;
243 else if (strcmp(Name, ".tdata") == 0 ||
244 strncmp(Name, ".tdata.", 7) == 0 ||
245 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
246 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
247 Flags |= SectionFlags::TLS;
248 else if (strcmp(Name, ".tbss") == 0 ||
249 strncmp(Name, ".tbss.", 6) == 0 ||
250 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
251 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
252 Flags |= SectionFlags::BSS | SectionFlags::TLS;
259 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
260 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
264 // FIXME: Should we use some hashing based on section name and just check
267 // Select section name
268 if (GV->hasSection()) {
269 // Honour section already set, if any
270 Name = GV->getSection();
272 // Use default section depending on the 'type' of global
273 Name = SelectSectionForGlobal(GV);
276 // If section is named we need to switch into it via special '.section'
277 // directive and also append funky flags. Otherwise - section name is just
278 // some magic assembler directive.
279 if (Flags & SectionFlags::Named)
280 Name = getSwitchToSectionDirective() + Name + PrintSectionFlags(Flags);
285 // Lame default implementation. Calculate the section name for global.
287 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
288 SectionKind::Kind Kind = SectionKindForGlobal(GV);
290 if (GV->isWeakForLinker())
291 return UniqueSectionForGlobal(GV, Kind);
293 if (Kind == SectionKind::Text)
294 return getTextSection();
295 else if (Kind == SectionKind::BSS && getBSSSection())
296 return getBSSSection();
297 else if (getReadOnlySection() &&
298 (Kind == SectionKind::ROData ||
299 Kind == SectionKind::RODataMergeConst ||
300 Kind == SectionKind::RODataMergeStr))
301 return getReadOnlySection();
304 return getDataSection();
308 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
309 SectionKind::Kind Kind) const {
311 case SectionKind::Text:
312 return ".gnu.linkonce.t." + GV->getName();
313 case SectionKind::Data:
314 return ".gnu.linkonce.d." + GV->getName();
315 case SectionKind::BSS:
316 return ".gnu.linkonce.b." + GV->getName();
317 case SectionKind::ROData:
318 case SectionKind::RODataMergeConst:
319 case SectionKind::RODataMergeStr:
320 return ".gnu.linkonce.r." + GV->getName();
321 case SectionKind::ThreadData:
322 return ".gnu.linkonce.td." + GV->getName();
323 case SectionKind::ThreadBSS:
324 return ".gnu.linkonce.tb." + GV->getName();
326 assert(0 && "Unknown section kind");