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 void TargetAsmInfo::fillDefaultValues() {
29 BSSSection = "\t.bss";
34 SmallRODataSection = 0;
37 ZeroFillDirective = 0;
38 NonexecutableStackDirective = 0;
45 PrivateGlobalPrefix = ".";
46 LessPrivateGlobalPrefix = "";
47 JumpTableSpecialLabelPrefix = 0;
48 GlobalVarAddrPrefix = "";
49 GlobalVarAddrSuffix = "";
50 FunctionAddrPrefix = "";
51 FunctionAddrSuffix = "";
52 PersonalityPrefix = "";
53 PersonalitySuffix = "";
54 NeedsIndirectEncoding = false;
55 InlineAsmStart = "#APP";
56 InlineAsmEnd = "#NO_APP";
58 StringConstantPrefix = ".str";
59 ZeroDirective = "\t.zero\t";
60 ZeroDirectiveSuffix = 0;
61 AsciiDirective = "\t.ascii\t";
62 AscizDirective = "\t.asciz\t";
63 Data8bitsDirective = "\t.byte\t";
64 Data16bitsDirective = "\t.short\t";
65 Data32bitsDirective = "\t.long\t";
66 Data64bitsDirective = "\t.quad\t";
67 AlignDirective = "\t.align\t";
68 AlignmentIsInBytes = true;
69 TextAlignFillValue = 0;
70 SwitchToSectionDirective = "\t.section\t";
71 TextSectionStartSuffix = "";
72 DataSectionStartSuffix = "";
73 SectionEndDirectiveSuffix = 0;
74 ConstantPoolSection = "\t.section .rodata";
75 JumpTableDataSection = "\t.section .rodata";
76 JumpTableDirective = 0;
79 // FIXME: Flags are ELFish - replace with normal section stuff.
80 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
81 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
82 GlobalDirective = "\t.globl\t";
85 COMMDirective = "\t.comm\t";
86 COMMDirectiveTakesAlignment = true;
87 HasDotTypeDotSizeDirective = true;
88 HasSingleParameterDotFile = true;
92 // FIXME: These are ELFish - move to ELFTAI.
93 HiddenDirective = "\t.hidden\t";
94 ProtectedDirective = "\t.protected\t";
95 AbsoluteDebugSectionOffsets = false;
96 AbsoluteEHSectionOffsets = false;
98 HasDotLocAndDotFile = false;
99 SupportsDebugInformation = false;
100 SupportsExceptionHandling = false;
101 DwarfRequiresFrameSection = true;
102 NonLocalEHFrameLabel = false;
103 GlobalEHDirective = 0;
104 SupportsWeakOmittedEHFrame = true;
105 DwarfSectionOffsetDirective = 0;
106 DwarfAbbrevSection = ".debug_abbrev";
107 DwarfInfoSection = ".debug_info";
108 DwarfLineSection = ".debug_line";
109 DwarfFrameSection = ".debug_frame";
110 DwarfPubNamesSection = ".debug_pubnames";
111 DwarfPubTypesSection = ".debug_pubtypes";
112 DwarfStrSection = ".debug_str";
113 DwarfLocSection = ".debug_loc";
114 DwarfARangesSection = ".debug_aranges";
115 DwarfRangesSection = ".debug_ranges";
116 DwarfMacInfoSection = ".debug_macinfo";
117 DwarfEHFrameSection = ".eh_frame";
118 DwarfExceptionSection = ".gcc_except_table";
120 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
121 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
124 TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm)
129 TargetAsmInfo::~TargetAsmInfo() {
132 /// Measure the specified inline asm to determine an approximation of its
134 /// Comments (which run till the next SeparatorChar or newline) do not
135 /// count as an instruction.
136 /// Any other non-whitespace text is considered an instruction, with
137 /// multiple instructions separated by SeparatorChar or newlines.
138 /// Variable-length instructions are not handled here; this function
139 /// may be overloaded in the target code to do that.
140 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
141 // Count the number of instructions in the asm.
142 bool atInsnStart = true;
144 for (; *Str; ++Str) {
145 if (*Str == '\n' || *Str == SeparatorChar)
147 if (atInsnStart && !isspace(*Str)) {
148 Length += MaxInstLength;
151 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
158 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
160 return dwarf::DW_EH_PE_absptr;
163 static bool isSuitableForBSS(const GlobalVariable *GV) {
164 if (!GV->hasInitializer())
167 // Leave constant zeros in readonly constant sections, so they can be shared
168 Constant *C = GV->getInitializer();
169 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
173 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
174 // Early exit - functions should be always in text sections.
175 if (isa<Function>(GV))
176 return SectionKind::Text;
178 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
179 bool isThreadLocal = GVar->isThreadLocal();
180 assert(GVar && "Invalid global value for section selection");
182 if (isSuitableForBSS(GVar)) {
183 // Variable can be easily put to BSS section.
184 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
185 } else if (GVar->isConstant() && !isThreadLocal) {
186 // Now we know, that varible has initializer and it is constant. We need to
187 // check its initializer to decide, which section to output it into. Also
188 // note, there is no thread-local r/o section.
189 Constant *C = GVar->getInitializer();
190 if (C->ContainsRelocations())
191 return SectionKind::ROData;
193 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
194 // Check, if initializer is a null-terminated string
195 if (CVA && CVA->isCString())
196 return SectionKind::RODataMergeStr;
198 return SectionKind::RODataMergeConst;
202 // Variable is not constant or thread-local - emit to generic data section.
203 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
207 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
208 const char* Name) const {
209 unsigned Flags = SectionFlags::None;
211 // Decode flags from global itself.
213 SectionKind::Kind Kind = SectionKindForGlobal(GV);
215 case SectionKind::Text:
216 Flags |= SectionFlags::Code;
218 case SectionKind::ThreadData:
219 case SectionKind::ThreadBSS:
220 Flags |= SectionFlags::TLS;
222 case SectionKind::Data:
223 case SectionKind::BSS:
224 Flags |= SectionFlags::Writeable;
226 case SectionKind::ROData:
227 case SectionKind::RODataMergeStr:
228 case SectionKind::RODataMergeConst:
229 // No additional flags here
231 case SectionKind::SmallData:
232 case SectionKind::SmallBSS:
233 Flags |= SectionFlags::Writeable;
235 case SectionKind::SmallROData:
236 Flags |= SectionFlags::Small;
239 assert(0 && "Unexpected section kind!");
242 if (GV->mayBeOverridden())
243 Flags |= SectionFlags::Linkonce;
246 // Add flags from sections, if any.
248 Flags |= SectionFlags::Named;
250 // Some lame default implementation based on some magic section names.
251 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
252 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
253 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
254 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
255 Flags |= SectionFlags::BSS;
256 else if (strcmp(Name, ".tdata") == 0 ||
257 strncmp(Name, ".tdata.", 7) == 0 ||
258 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
259 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
260 Flags |= SectionFlags::TLS;
261 else if (strcmp(Name, ".tbss") == 0 ||
262 strncmp(Name, ".tbss.", 6) == 0 ||
263 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
264 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
265 Flags |= SectionFlags::BSS | SectionFlags::TLS;
272 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
274 // Select section name
275 if (GV->hasSection()) {
276 // Honour section already set, if any
277 unsigned Flags = SectionFlagsForGlobal(GV,
278 GV->getSection().c_str());
279 S = getNamedSection(GV->getSection().c_str(), Flags);
281 // Use default section depending on the 'type' of global
282 S = SelectSectionForGlobal(GV);
288 // Lame default implementation. Calculate the section name for global.
290 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
291 SectionKind::Kind Kind = SectionKindForGlobal(GV);
293 if (GV->mayBeOverridden()) {
294 std::string Name = UniqueSectionForGlobal(GV, Kind);
295 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
296 return getNamedSection(Name.c_str(), Flags);
298 if (Kind == SectionKind::Text)
299 return getTextSection();
300 else if (isBSS(Kind) && getBSSSection_())
301 return getBSSSection_();
302 else if (getReadOnlySection() && SectionKind::isReadOnly(Kind))
303 return getReadOnlySection();
306 return getDataSection();
309 // Lame default implementation. Calculate the section name for machine const.
311 TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
312 // FIXME: Support data.rel stuff someday
313 return getDataSection();
317 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
318 SectionKind::Kind Kind) const {
320 case SectionKind::Text:
321 return ".gnu.linkonce.t." + GV->getName();
322 case SectionKind::Data:
323 return ".gnu.linkonce.d." + GV->getName();
324 case SectionKind::SmallData:
325 return ".gnu.linkonce.s." + GV->getName();
326 case SectionKind::BSS:
327 return ".gnu.linkonce.b." + GV->getName();
328 case SectionKind::SmallBSS:
329 return ".gnu.linkonce.sb." + GV->getName();
330 case SectionKind::ROData:
331 case SectionKind::RODataMergeConst:
332 case SectionKind::RODataMergeStr:
333 return ".gnu.linkonce.r." + GV->getName();
334 case SectionKind::SmallROData:
335 return ".gnu.linkonce.s2." + GV->getName();
336 case SectionKind::ThreadData:
337 return ".gnu.linkonce.td." + GV->getName();
338 case SectionKind::ThreadBSS:
339 return ".gnu.linkonce.tb." + GV->getName();
341 assert(0 && "Unknown section kind");
347 TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
348 bool Override) const {
349 Section& S = Sections[Name];
351 // This is newly-created section, set it up properly.
352 if (S.Flags == SectionFlags::Invalid || Override) {
353 S.Flags = Flags | SectionFlags::Named;
361 TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
362 bool Override) const {
363 Section& S = Sections[Directive];
365 // This is newly-created section, set it up properly.
366 if (S.Flags == SectionFlags::Invalid || Override) {
367 S.Flags = Flags & ~SectionFlags::Named;
375 TargetAsmInfo::getSectionFlags(unsigned Flags) const {
376 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
378 // We didn't print these flags yet, print and save them to map. This reduces
379 // amount of heap trashing due to std::string construction / concatenation.
380 if (I == FlagsStrings.end())
381 I = FlagsStrings.insert(std::make_pair(Flags,
382 printSectionFlags(Flags))).first;
387 unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
391 Size += sizeof(int8_t);
396 unsigned TargetAsmInfo::getSLEB128Size(int Value) {
398 int Sign = Value >> (8 * sizeof(Value) - 1);
402 unsigned Byte = Value & 0x7f;
404 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
405 Size += sizeof(int8_t);