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"),
31 DataSection("\t.data"),
39 SmallRODataSection(0),
40 TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
42 TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
45 NonexecutableStackDirective(0),
52 PrivateGlobalPrefix("."),
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 StringConstantPrefix(".str"),
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 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
86 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
87 FourByteConstantSection(0),
88 FourByteConstantSection_(0),
89 EightByteConstantSection(0),
90 EightByteConstantSection_(0),
91 SixteenByteConstantSection(0),
92 SixteenByteConstantSection_(0),
93 GlobalDirective("\t.globl\t"),
96 COMMDirective("\t.comm\t"),
97 COMMDirectiveTakesAlignment(true),
98 HasDotTypeDotSizeDirective(true),
102 HiddenDirective("\t.hidden\t"),
103 ProtectedDirective("\t.protected\t"),
104 AbsoluteDebugSectionOffsets(false),
105 AbsoluteEHSectionOffsets(false),
107 HasDotLocAndDotFile(false),
108 SupportsDebugInformation(false),
109 SupportsExceptionHandling(false),
110 DwarfRequiresFrameSection(true),
111 GlobalEHDirective(0),
112 SupportsWeakOmittedEHFrame(true),
113 DwarfSectionOffsetDirective(0),
114 DwarfAbbrevSection(".debug_abbrev"),
115 DwarfInfoSection(".debug_info"),
116 DwarfLineSection(".debug_line"),
117 DwarfFrameSection(".debug_frame"),
118 DwarfPubNamesSection(".debug_pubnames"),
119 DwarfPubTypesSection(".debug_pubtypes"),
120 DwarfStrSection(".debug_str"),
121 DwarfLocSection(".debug_loc"),
122 DwarfARangesSection(".debug_aranges"),
123 DwarfRangesSection(".debug_ranges"),
124 DwarfMacInfoSection(".debug_macinfo"),
125 DwarfEHFrameSection(".eh_frame"),
126 DwarfExceptionSection(".gcc_except_table"),
128 TextSection_ = getUnnamedSection(TextSection);
129 DataSection_ = getUnnamedSection(DataSection);
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);
176 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
177 // Early exit - functions should be always in text sections.
178 if (isa<Function>(GV))
179 return SectionKind::Text;
181 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
182 bool isThreadLocal = GVar->isThreadLocal();
183 assert(GVar && "Invalid global value for section selection");
185 if (isSuitableForBSS(GVar)) {
186 // Variable can be easily put to BSS section.
187 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
188 } else if (GVar->isConstant() && !isThreadLocal) {
189 // Now we know, that varible has initializer and it is constant. We need to
190 // check its initializer to decide, which section to output it into. Also
191 // note, there is no thread-local r/o section.
192 Constant *C = GVar->getInitializer();
193 if (C->ContainsRelocations())
194 return SectionKind::ROData;
196 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
197 // Check, if initializer is a null-terminated string
198 if (CVA && CVA->isCString())
199 return SectionKind::RODataMergeStr;
201 return SectionKind::RODataMergeConst;
205 // Variable is not constant or thread-local - emit to generic data section.
206 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
210 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
211 const char* Name) const {
212 unsigned Flags = SectionFlags::None;
214 // Decode flags from global itself.
216 SectionKind::Kind Kind = SectionKindForGlobal(GV);
218 case SectionKind::Text:
219 Flags |= SectionFlags::Code;
221 case SectionKind::ThreadData:
222 case SectionKind::ThreadBSS:
223 Flags |= SectionFlags::TLS;
225 case SectionKind::Data:
226 case SectionKind::BSS:
227 Flags |= SectionFlags::Writeable;
229 case SectionKind::ROData:
230 case SectionKind::RODataMergeStr:
231 case SectionKind::RODataMergeConst:
232 // No additional flags here
234 case SectionKind::SmallData:
235 case SectionKind::SmallBSS:
236 Flags |= SectionFlags::Writeable;
238 case SectionKind::SmallROData:
239 Flags |= SectionFlags::Small;
242 assert(0 && "Unexpected section kind!");
245 if (GV->isWeakForLinker())
246 Flags |= SectionFlags::Linkonce;
249 // Add flags from sections, if any.
251 Flags |= SectionFlags::Named;
253 // Some lame default implementation based on some magic section names.
254 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
255 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
256 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
257 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
258 Flags |= SectionFlags::BSS;
259 else if (strcmp(Name, ".tdata") == 0 ||
260 strncmp(Name, ".tdata.", 7) == 0 ||
261 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
262 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
263 Flags |= SectionFlags::TLS;
264 else if (strcmp(Name, ".tbss") == 0 ||
265 strncmp(Name, ".tbss.", 6) == 0 ||
266 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
267 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
268 Flags |= SectionFlags::BSS | SectionFlags::TLS;
275 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
277 // Select section name
278 if (GV->hasSection()) {
279 // Honour section already set, if any
280 unsigned Flags = SectionFlagsForGlobal(GV,
281 GV->getSection().c_str());
282 S = getNamedSection(GV->getSection().c_str(), Flags);
284 // Use default section depending on the 'type' of global
285 S = SelectSectionForGlobal(GV);
291 // If section is named we need to switch into it via special '.section'
292 // directive and also append funky flags. Otherwise - section name is just
293 // some magic assembler directive.
294 return getSwitchToSectionDirective() + S->Name + getSectionFlags(S->Flags);
297 // Lame default implementation. Calculate the section name for global.
299 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
300 SectionKind::Kind Kind = SectionKindForGlobal(GV);
302 if (GV->isWeakForLinker()) {
303 std::string Name = UniqueSectionForGlobal(GV, Kind);
304 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
305 return getNamedSection(Name.c_str(), Flags);
307 if (Kind == SectionKind::Text)
308 return getTextSection_();
309 else if (isBSS(Kind) && getBSSSection_())
310 return getBSSSection_();
311 else if (getReadOnlySection_() && SectionKind::isReadOnly(Kind))
312 return getReadOnlySection_();
315 return getDataSection_();
318 // Lame default implementation. Calculate the section name for machine const.
320 TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
321 // FIXME: Support data.rel stuff someday
322 return getDataSection_();
326 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
327 SectionKind::Kind Kind) const {
329 case SectionKind::Text:
330 return ".gnu.linkonce.t." + GV->getName();
331 case SectionKind::Data:
332 return ".gnu.linkonce.d." + GV->getName();
333 case SectionKind::SmallData:
334 return ".gnu.linkonce.s." + GV->getName();
335 case SectionKind::BSS:
336 return ".gnu.linkonce.b." + GV->getName();
337 case SectionKind::SmallBSS:
338 return ".gnu.linkonce.sb." + GV->getName();
339 case SectionKind::ROData:
340 case SectionKind::RODataMergeConst:
341 case SectionKind::RODataMergeStr:
342 return ".gnu.linkonce.r." + GV->getName();
343 case SectionKind::SmallROData:
344 return ".gnu.linkonce.s2." + GV->getName();
345 case SectionKind::ThreadData:
346 return ".gnu.linkonce.td." + GV->getName();
347 case SectionKind::ThreadBSS:
348 return ".gnu.linkonce.tb." + GV->getName();
350 assert(0 && "Unknown section kind");
355 TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
356 bool Override) const {
357 Section& S = Sections[Name];
359 // This is newly-created section, set it up properly.
360 if (S.Flags == SectionFlags::Invalid || Override) {
361 S.Flags = Flags | SectionFlags::Named;
369 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
370 bool Override) const {
371 Section& S = Sections[Directive];
373 // This is newly-created section, set it up properly.
374 if (S.Flags == SectionFlags::Invalid || Override) {
375 S.Flags = Flags & ~SectionFlags::Named;
383 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
384 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
386 // We didn't print these flags yet, print and save them to map. This reduces
387 // amount of heap trashing due to std::string construction / concatenation.
388 if (I == FlagsStrings.end())
389 I = FlagsStrings.insert(std::make_pair(Flags,
390 printSectionFlags(Flags))).first;
395 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
399 Size += sizeof(int8_t);
404 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
406 int Sign = Value >> (8 * sizeof(Value) - 1);
410 unsigned Byte = Value & 0x7f;
412 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
413 Size += sizeof(int8_t);