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() :
37 SmallRODataSection(0),
38 TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
40 TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
43 NonexecutableStackDirective(0),
50 PrivateGlobalPrefix("."),
51 LessPrivateGlobalPrefix(""),
52 JumpTableSpecialLabelPrefix(0),
53 GlobalVarAddrPrefix(""),
54 GlobalVarAddrSuffix(""),
55 FunctionAddrPrefix(""),
56 FunctionAddrSuffix(""),
57 PersonalityPrefix(""),
58 PersonalitySuffix(""),
59 NeedsIndirectEncoding(false),
60 InlineAsmStart("#APP"),
61 InlineAsmEnd("#NO_APP"),
63 StringConstantPrefix(".str"),
64 ZeroDirective("\t.zero\t"),
65 ZeroDirectiveSuffix(0),
66 AsciiDirective("\t.ascii\t"),
67 AscizDirective("\t.asciz\t"),
68 Data8bitsDirective("\t.byte\t"),
69 Data16bitsDirective("\t.short\t"),
70 Data32bitsDirective("\t.long\t"),
71 Data64bitsDirective("\t.quad\t"),
72 AlignDirective("\t.align\t"),
73 AlignmentIsInBytes(true),
74 TextAlignFillValue(0),
75 SwitchToSectionDirective("\t.section\t"),
76 TextSectionStartSuffix(""),
77 DataSectionStartSuffix(""),
78 SectionEndDirectiveSuffix(0),
79 ConstantPoolSection("\t.section .rodata"),
80 JumpTableDataSection("\t.section .rodata"),
81 JumpTableDirective(0),
84 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
85 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
86 FourByteConstantSection(0),
87 FourByteConstantSection_(0),
88 EightByteConstantSection(0),
89 EightByteConstantSection_(0),
90 SixteenByteConstantSection(0),
91 SixteenByteConstantSection_(0),
92 GlobalDirective("\t.globl\t"),
95 COMMDirective("\t.comm\t"),
96 COMMDirectiveTakesAlignment(true),
97 HasDotTypeDotSizeDirective(true),
101 HiddenDirective("\t.hidden\t"),
102 ProtectedDirective("\t.protected\t"),
103 AbsoluteDebugSectionOffsets(false),
104 AbsoluteEHSectionOffsets(false),
106 HasDotLocAndDotFile(false),
107 SupportsDebugInformation(false),
108 SupportsExceptionHandling(false),
109 DwarfRequiresFrameSection(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 DwarfStrSection(".debug_str"),
120 DwarfLocSection(".debug_loc"),
121 DwarfARangesSection(".debug_aranges"),
122 DwarfRangesSection(".debug_ranges"),
123 DwarfMacInfoSection(".debug_macinfo"),
124 DwarfEHFrameSection(".eh_frame"),
125 DwarfExceptionSection(".gcc_except_table"),
127 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
128 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
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);
175 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
176 // Early exit - functions should be always in text sections.
177 if (isa<Function>(GV))
178 return SectionKind::Text;
180 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
181 bool isThreadLocal = GVar->isThreadLocal();
182 assert(GVar && "Invalid global value for section selection");
184 if (isSuitableForBSS(GVar)) {
185 // Variable can be easily put to BSS section.
186 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
187 } else if (GVar->isConstant() && !isThreadLocal) {
188 // Now we know, that varible has initializer and it is constant. We need to
189 // check its initializer to decide, which section to output it into. Also
190 // note, there is no thread-local r/o section.
191 Constant *C = GVar->getInitializer();
192 if (C->ContainsRelocations())
193 return SectionKind::ROData;
195 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
196 // Check, if initializer is a null-terminated string
197 if (CVA && CVA->isCString())
198 return SectionKind::RODataMergeStr;
200 return SectionKind::RODataMergeConst;
204 // Variable is not constant or thread-local - emit to generic data section.
205 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
209 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
210 const char* Name) const {
211 unsigned Flags = SectionFlags::None;
213 // Decode flags from global itself.
215 SectionKind::Kind Kind = SectionKindForGlobal(GV);
217 case SectionKind::Text:
218 Flags |= SectionFlags::Code;
220 case SectionKind::ThreadData:
221 case SectionKind::ThreadBSS:
222 Flags |= SectionFlags::TLS;
224 case SectionKind::Data:
225 case SectionKind::BSS:
226 Flags |= SectionFlags::Writeable;
228 case SectionKind::ROData:
229 case SectionKind::RODataMergeStr:
230 case SectionKind::RODataMergeConst:
231 // No additional flags here
233 case SectionKind::SmallData:
234 case SectionKind::SmallBSS:
235 Flags |= SectionFlags::Writeable;
237 case SectionKind::SmallROData:
238 Flags |= SectionFlags::Small;
241 assert(0 && "Unexpected section kind!");
244 if (GV->isWeakForLinker())
245 Flags |= SectionFlags::Linkonce;
248 // Add flags from sections, if any.
250 Flags |= SectionFlags::Named;
252 // Some lame default implementation based on some magic section names.
253 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
254 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
255 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
256 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
257 Flags |= SectionFlags::BSS;
258 else if (strcmp(Name, ".tdata") == 0 ||
259 strncmp(Name, ".tdata.", 7) == 0 ||
260 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
261 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
262 Flags |= SectionFlags::TLS;
263 else if (strcmp(Name, ".tbss") == 0 ||
264 strncmp(Name, ".tbss.", 6) == 0 ||
265 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
266 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
267 Flags |= SectionFlags::BSS | SectionFlags::TLS;
274 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
276 // Select section name
277 if (GV->hasSection()) {
278 // Honour section already set, if any
279 unsigned Flags = SectionFlagsForGlobal(GV,
280 GV->getSection().c_str());
281 S = getNamedSection(GV->getSection().c_str(), Flags);
283 // Use default section depending on the 'type' of global
284 S = SelectSectionForGlobal(GV);
290 // Lame default implementation. Calculate the section name for global.
292 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
293 SectionKind::Kind Kind = SectionKindForGlobal(GV);
295 if (GV->isWeakForLinker()) {
296 std::string Name = UniqueSectionForGlobal(GV, Kind);
297 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
298 return getNamedSection(Name.c_str(), Flags);
300 if (Kind == SectionKind::Text)
301 return getTextSection();
302 else if (isBSS(Kind) && getBSSSection_())
303 return getBSSSection_();
304 else if (getReadOnlySection_() && SectionKind::isReadOnly(Kind))
305 return getReadOnlySection_();
308 return getDataSection();
311 // Lame default implementation. Calculate the section name for machine const.
313 TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
314 // FIXME: Support data.rel stuff someday
315 return getDataSection();
319 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
320 SectionKind::Kind Kind) const {
322 case SectionKind::Text:
323 return ".gnu.linkonce.t." + GV->getName();
324 case SectionKind::Data:
325 return ".gnu.linkonce.d." + GV->getName();
326 case SectionKind::SmallData:
327 return ".gnu.linkonce.s." + GV->getName();
328 case SectionKind::BSS:
329 return ".gnu.linkonce.b." + GV->getName();
330 case SectionKind::SmallBSS:
331 return ".gnu.linkonce.sb." + GV->getName();
332 case SectionKind::ROData:
333 case SectionKind::RODataMergeConst:
334 case SectionKind::RODataMergeStr:
335 return ".gnu.linkonce.r." + GV->getName();
336 case SectionKind::SmallROData:
337 return ".gnu.linkonce.s2." + GV->getName();
338 case SectionKind::ThreadData:
339 return ".gnu.linkonce.td." + GV->getName();
340 case SectionKind::ThreadBSS:
341 return ".gnu.linkonce.tb." + GV->getName();
343 assert(0 && "Unknown section kind");
348 TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
349 bool Override) const {
350 Section& S = Sections[Name];
352 // This is newly-created section, set it up properly.
353 if (S.Flags == SectionFlags::Invalid || Override) {
354 S.Flags = Flags | SectionFlags::Named;
362 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
363 bool Override) const {
364 Section& S = Sections[Directive];
366 // This is newly-created section, set it up properly.
367 if (S.Flags == SectionFlags::Invalid || Override) {
368 S.Flags = Flags & ~SectionFlags::Named;
376 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
377 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
379 // We didn't print these flags yet, print and save them to map. This reduces
380 // amount of heap trashing due to std::string construction / concatenation.
381 if (I == FlagsStrings.end())
382 I = FlagsStrings.insert(std::make_pair(Flags,
383 printSectionFlags(Flags))).first;
388 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
392 Size += sizeof(int8_t);
397 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
399 int Sign = Value >> (8 * sizeof(Value) - 1);
403 unsigned Byte = Value & 0x7f;
405 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
406 Size += sizeof(int8_t);