6967ebe4860b5b08a6c134e35c6953159218719f
[oota-llvm.git] / lib / Target / TargetAsmInfo.cpp
1 //===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines target asm properties related what form asm statements
11 // should take.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
23 #include <cctype>
24 #include <cstring>
25
26 using namespace llvm;
27
28 TargetAsmInfo::TargetAsmInfo() :
29   TextSection("\t.text"),
30   DataSection("\t.data"),
31   BSSSection("\t.bss"),
32   TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
33   TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
34   ZeroFillDirective(0),
35   NonexecutableStackDirective(0),
36   NeedsSet(false),
37   MaxInstLength(4),
38   PCSymbol("$"),
39   SeparatorChar(';'),
40   CommentString("#"),
41   GlobalPrefix(""),
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"),
53   AssemblerDialect(0),
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),
73   CStringSection(0),
74   StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
75   StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
76   FourByteConstantSection(0),
77   EightByteConstantSection(0),
78   SixteenByteConstantSection(0),
79   ReadOnlySection(0),
80   GlobalDirective("\t.globl\t"),
81   SetDirective(0),
82   LCOMMDirective(0),
83   COMMDirective("\t.comm\t"),
84   COMMDirectiveTakesAlignment(true),
85   HasDotTypeDotSizeDirective(true),
86   UsedDirective(0),
87   WeakRefDirective(0),
88   WeakDefDirective(0),
89   HiddenDirective("\t.hidden\t"),
90   ProtectedDirective("\t.protected\t"),
91   AbsoluteDebugSectionOffsets(false),
92   AbsoluteEHSectionOffsets(false),
93   HasLEB128(false),
94   HasDotLocAndDotFile(false),
95   SupportsDebugInformation(false),
96   SupportsExceptionHandling(false),
97   DwarfRequiresFrameSection(true),
98   GlobalEHDirective(0),
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"),
114   AsmTransCBE(0) {
115 }
116
117 TargetAsmInfo::~TargetAsmInfo() {
118 }
119
120 /// Measure the specified inline asm to determine an approximation of its
121 /// length.
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;
131   unsigned Length = 0;
132   for (; *Str; ++Str) {
133     if (*Str == '\n' || *Str == SeparatorChar)
134       atInsnStart = true;
135     if (atInsnStart && !isspace(*Str)) {
136       Length += MaxInstLength;
137       atInsnStart = false;
138     }
139     if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
140       atInsnStart = false;
141   }
142
143   return Length;
144 }
145
146 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
147                                               bool Global) const {
148   return dwarf::DW_EH_PE_absptr;
149 }
150
151 static bool isSuitableForBSS(const GlobalVariable *GV) {
152   if (!GV->hasInitializer())
153     return true;
154
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);
158 }
159
160 SectionKind::Kind
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;
165
166   const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
167   bool isThreadLocal = GVar->isThreadLocal();
168   assert(GVar && "Invalid global value for section selection");
169
170   SectionKind::Kind kind;
171   if (isSuitableForBSS(GVar)) {
172     // Variable can be easily put to BSS section.
173     return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
174   } else if (GVar->isConstant() && !isThreadLocal) {
175     // Now we know, that varible has initializer and it is constant. We need to
176     // check its initializer to decide, which section to output it into. Also
177     // note, there is no thread-local r/o section.
178     Constant *C = GVar->getInitializer();
179     if (C->ContainsRelocations())
180       kind = SectionKind::ROData;
181     else {
182       const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
183       // Check, if initializer is a null-terminated string
184       if (CVA && CVA->isCString())
185         kind = SectionKind::RODataMergeStr;
186       else
187         kind = SectionKind::RODataMergeConst;
188     }
189   }
190
191   // Variable is not constant or thread-local - emit to generic data section.
192   return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
193 }
194
195 unsigned
196 TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
197                                      const char* name) const {
198   unsigned flags = SectionFlags::None;
199
200   // Decode flags from global itself.
201   if (GV) {
202     SectionKind::Kind kind = SectionKindForGlobal(GV);
203     switch (kind) {
204      case SectionKind::Text:
205       flags |= SectionFlags::Code;
206       break;
207      case SectionKind::ThreadData:
208       flags |= SectionFlags::TLS;
209       // FALLS THROUGH
210      case SectionKind::Data:
211       flags |= SectionFlags::Writeable;
212       break;
213      case SectionKind::ThreadBSS:
214       flags |= SectionFlags::TLS;
215       // FALLS THROUGH
216      case SectionKind::BSS:
217       flags |= SectionFlags::BSS;
218       break;
219      case SectionKind::ROData:
220       // No additional flags here
221       break;
222      case SectionKind::RODataMergeStr:
223       flags |= SectionFlags::Strings;
224       // FALLS THROUGH
225      case SectionKind::RODataMergeConst:
226       flags |= SectionFlags::Mergeable;
227       break;
228      default:
229       assert(0 && "Unexpected section kind!");
230     }
231
232     if (GV->hasLinkOnceLinkage() ||
233         GV->hasWeakLinkage() ||
234         GV->hasCommonLinkage())
235       flags |= SectionFlags::Linkonce;
236   }
237
238   // Add flags from sections, if any.
239   if (name) {
240     // Some lame default implementation
241     if (strcmp(name, ".bss") == 0 ||
242         strncmp(name, ".bss.", 5) == 0 ||
243         strncmp(name, ".llvm.linkonce.b.", 17) == 0)
244       flags |= SectionFlags::BSS;
245     else if (strcmp(name, ".tdata") == 0 ||
246              strncmp(name, ".tdata.", 7) == 0 ||
247              strncmp(name, ".llvm.linkonce.td.", 18) == 0)
248       flags |= SectionFlags::TLS;
249     else if (strcmp(name, ".tbss") == 0 ||
250              strncmp(name, ".tbss.", 6) == 0 ||
251              strncmp(name, ".llvm.linkonce.tb.", 18) == 0)
252       flags |= SectionFlags::BSS | SectionFlags::TLS;
253   }
254
255   return flags;
256 }
257
258 std::string
259 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
260   SectionKind::Kind kind = SectionKindForGlobal(GV);
261
262   if (kind == SectionKind::Text)
263     return getTextSection();
264   else if (kind == SectionKind::BSS && getBSSSection())
265     return getBSSSection();
266
267   return getDataSection();
268 }
269
270 std::string
271 TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
272                                       SectionKind::Kind kind) const {
273   switch (kind) {
274    case SectionKind::Text:
275     return ".llvm.linkonce.t." + GV->getName();
276    case SectionKind::Data:
277     return ".llvm.linkonce.d." + GV->getName();
278    case SectionKind::BSS:
279     return ".llvm.linkonce.b." + GV->getName();
280    case SectionKind::ROData:
281    case SectionKind::RODataMergeConst:
282    case SectionKind::RODataMergeStr:
283     return ".llvm.linkonce.r." + GV->getName();
284    case SectionKind::ThreadData:
285     return ".llvm.linkonce.td." + GV->getName();
286    case SectionKind::ThreadBSS:
287     return ".llvm.linkonce.tb." + GV->getName();
288    default:
289     assert(0 && "Unknown section kind");
290   }
291 }