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() :
36 SmallRODataSection(0),
40 NonexecutableStackDirective(0),
47 PrivateGlobalPrefix("."),
48 LessPrivateGlobalPrefix(""),
49 JumpTableSpecialLabelPrefix(0),
50 GlobalVarAddrPrefix(""),
51 GlobalVarAddrSuffix(""),
52 FunctionAddrPrefix(""),
53 FunctionAddrSuffix(""),
54 PersonalityPrefix(""),
55 PersonalitySuffix(""),
56 NeedsIndirectEncoding(false),
57 InlineAsmStart("#APP"),
58 InlineAsmEnd("#NO_APP"),
60 StringConstantPrefix(".str"),
61 ZeroDirective("\t.zero\t"),
62 ZeroDirectiveSuffix(0),
63 AsciiDirective("\t.ascii\t"),
64 AscizDirective("\t.asciz\t"),
65 Data8bitsDirective("\t.byte\t"),
66 Data16bitsDirective("\t.short\t"),
67 Data32bitsDirective("\t.long\t"),
68 Data64bitsDirective("\t.quad\t"),
69 AlignDirective("\t.align\t"),
70 AlignmentIsInBytes(true),
71 TextAlignFillValue(0),
72 SwitchToSectionDirective("\t.section\t"),
73 TextSectionStartSuffix(""),
74 DataSectionStartSuffix(""),
75 SectionEndDirectiveSuffix(0),
76 ConstantPoolSection("\t.section .rodata"),
77 JumpTableDataSection("\t.section .rodata"),
78 JumpTableDirective(0),
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),
92 HiddenDirective("\t.hidden\t"),
93 ProtectedDirective("\t.protected\t"),
94 AbsoluteDebugSectionOffsets(false),
95 AbsoluteEHSectionOffsets(false),
97 HasDotLocAndDotFile(false),
98 SupportsDebugInformation(false),
99 SupportsExceptionHandling(false),
100 DwarfRequiresFrameSection(true),
101 GlobalEHDirective(0),
102 SupportsWeakOmittedEHFrame(true),
103 DwarfSectionOffsetDirective(0),
104 DwarfAbbrevSection(".debug_abbrev"),
105 DwarfInfoSection(".debug_info"),
106 DwarfLineSection(".debug_line"),
107 DwarfFrameSection(".debug_frame"),
108 DwarfPubNamesSection(".debug_pubnames"),
109 DwarfPubTypesSection(".debug_pubtypes"),
110 DwarfStrSection(".debug_str"),
111 DwarfLocSection(".debug_loc"),
112 DwarfARangesSection(".debug_aranges"),
113 DwarfRangesSection(".debug_ranges"),
114 DwarfMacInfoSection(".debug_macinfo"),
115 DwarfEHFrameSection(".eh_frame"),
116 DwarfExceptionSection(".gcc_except_table"),
118 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
119 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
122 TargetAsmInfo::~TargetAsmInfo() {
125 /// Measure the specified inline asm to determine an approximation of its
127 /// Comments (which run till the next SeparatorChar or newline) do not
128 /// count as an instruction.
129 /// Any other non-whitespace text is considered an instruction, with
130 /// multiple instructions separated by SeparatorChar or newlines.
131 /// Variable-length instructions are not handled here; this function
132 /// may be overloaded in the target code to do that.
133 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
134 // Count the number of instructions in the asm.
135 bool atInsnStart = true;
137 for (; *Str; ++Str) {
138 if (*Str == '\n' || *Str == SeparatorChar)
140 if (atInsnStart && !isspace(*Str)) {
141 Length += MaxInstLength;
144 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
151 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
153 return dwarf::DW_EH_PE_absptr;
156 static bool isSuitableForBSS(const GlobalVariable *GV) {
157 if (!GV->hasInitializer())
160 // Leave constant zeros in readonly constant sections, so they can be shared
161 Constant *C = GV->getInitializer();
162 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
166 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
167 // Early exit - functions should be always in text sections.
168 if (isa<Function>(GV))
169 return SectionKind::Text;
171 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
172 bool isThreadLocal = GVar->isThreadLocal();
173 assert(GVar && "Invalid global value for section selection");
175 if (isSuitableForBSS(GVar)) {
176 // Variable can be easily put to BSS section.
177 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
178 } else if (GVar->isConstant() && !isThreadLocal) {
179 // Now we know, that varible has initializer and it is constant. We need to
180 // check its initializer to decide, which section to output it into. Also
181 // note, there is no thread-local r/o section.
182 Constant *C = GVar->getInitializer();
183 if (C->ContainsRelocations())
184 return SectionKind::ROData;
186 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
187 // Check, if initializer is a null-terminated string
188 if (CVA && CVA->isCString())
189 return SectionKind::RODataMergeStr;
191 return SectionKind::RODataMergeConst;
195 // Variable is not constant or thread-local - emit to generic data section.
196 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
200 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
201 const char* Name) const {
202 unsigned Flags = SectionFlags::None;
204 // Decode flags from global itself.
206 SectionKind::Kind Kind = SectionKindForGlobal(GV);
208 case SectionKind::Text:
209 Flags |= SectionFlags::Code;
211 case SectionKind::ThreadData:
212 case SectionKind::ThreadBSS:
213 Flags |= SectionFlags::TLS;
215 case SectionKind::Data:
216 case SectionKind::BSS:
217 Flags |= SectionFlags::Writeable;
219 case SectionKind::ROData:
220 case SectionKind::RODataMergeStr:
221 case SectionKind::RODataMergeConst:
222 // No additional flags here
224 case SectionKind::SmallData:
225 case SectionKind::SmallBSS:
226 Flags |= SectionFlags::Writeable;
228 case SectionKind::SmallROData:
229 Flags |= SectionFlags::Small;
232 assert(0 && "Unexpected section kind!");
235 if (GV->isWeakForLinker())
236 Flags |= SectionFlags::Linkonce;
239 // Add flags from sections, if any.
241 Flags |= SectionFlags::Named;
243 // Some lame default implementation based on some magic section names.
244 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
245 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
246 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
247 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
248 Flags |= SectionFlags::BSS;
249 else if (strcmp(Name, ".tdata") == 0 ||
250 strncmp(Name, ".tdata.", 7) == 0 ||
251 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
252 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
253 Flags |= SectionFlags::TLS;
254 else if (strcmp(Name, ".tbss") == 0 ||
255 strncmp(Name, ".tbss.", 6) == 0 ||
256 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
257 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
258 Flags |= SectionFlags::BSS | SectionFlags::TLS;
265 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
267 // Select section name
268 if (GV->hasSection()) {
269 // Honour section already set, if any
270 unsigned Flags = SectionFlagsForGlobal(GV,
271 GV->getSection().c_str());
272 S = getNamedSection(GV->getSection().c_str(), Flags);
274 // Use default section depending on the 'type' of global
275 S = SelectSectionForGlobal(GV);
281 // Lame default implementation. Calculate the section name for global.
283 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
284 SectionKind::Kind Kind = SectionKindForGlobal(GV);
286 if (GV->isWeakForLinker()) {
287 std::string Name = UniqueSectionForGlobal(GV, Kind);
288 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
289 return getNamedSection(Name.c_str(), Flags);
291 if (Kind == SectionKind::Text)
292 return getTextSection();
293 else if (isBSS(Kind) && getBSSSection_())
294 return getBSSSection_();
295 else if (getReadOnlySection() && SectionKind::isReadOnly(Kind))
296 return getReadOnlySection();
299 return getDataSection();
302 // Lame default implementation. Calculate the section name for machine const.
304 TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
305 // FIXME: Support data.rel stuff someday
306 return getDataSection();
310 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
311 SectionKind::Kind Kind) const {
313 case SectionKind::Text:
314 return ".gnu.linkonce.t." + GV->getName();
315 case SectionKind::Data:
316 return ".gnu.linkonce.d." + GV->getName();
317 case SectionKind::SmallData:
318 return ".gnu.linkonce.s." + GV->getName();
319 case SectionKind::BSS:
320 return ".gnu.linkonce.b." + GV->getName();
321 case SectionKind::SmallBSS:
322 return ".gnu.linkonce.sb." + GV->getName();
323 case SectionKind::ROData:
324 case SectionKind::RODataMergeConst:
325 case SectionKind::RODataMergeStr:
326 return ".gnu.linkonce.r." + GV->getName();
327 case SectionKind::SmallROData:
328 return ".gnu.linkonce.s2." + GV->getName();
329 case SectionKind::ThreadData:
330 return ".gnu.linkonce.td." + GV->getName();
331 case SectionKind::ThreadBSS:
332 return ".gnu.linkonce.tb." + GV->getName();
334 assert(0 && "Unknown section kind");
339 TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
340 bool Override) const {
341 Section& S = Sections[Name];
343 // This is newly-created section, set it up properly.
344 if (S.Flags == SectionFlags::Invalid || Override) {
345 S.Flags = Flags | SectionFlags::Named;
353 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
354 bool Override) const {
355 Section& S = Sections[Directive];
357 // This is newly-created section, set it up properly.
358 if (S.Flags == SectionFlags::Invalid || Override) {
359 S.Flags = Flags & ~SectionFlags::Named;
367 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
368 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
370 // We didn't print these flags yet, print and save them to map. This reduces
371 // amount of heap trashing due to std::string construction / concatenation.
372 if (I == FlagsStrings.end())
373 I = FlagsStrings.insert(std::make_pair(Flags,
374 printSectionFlags(Flags))).first;
379 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
383 Size += sizeof(int8_t);
388 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
390 int Sign = Value >> (8 * sizeof(Value) - 1);
394 unsigned Byte = Value & 0x7f;
396 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
397 Size += sizeof(int8_t);