8caff91e6029095473da5c61a2946aa93365ccfe
[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   DebugInfoRequireFrameMoveInfo(true),
115   AsmTransCBE(0) {
116 }
117
118 TargetAsmInfo::~TargetAsmInfo() {
119 }
120
121 /// Measure the specified inline asm to determine an approximation of its
122 /// length.
123 /// Comments (which run till the next SeparatorChar or newline) do not
124 /// count as an instruction.
125 /// Any other non-whitespace text is considered an instruction, with
126 /// multiple instructions separated by SeparatorChar or newlines.
127 /// Variable-length instructions are not handled here; this function
128 /// may be overloaded in the target code to do that.
129 unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
130   // Count the number of instructions in the asm.
131   bool atInsnStart = true;
132   unsigned Length = 0;
133   for (; *Str; ++Str) {
134     if (*Str == '\n' || *Str == SeparatorChar)
135       atInsnStart = true;
136     if (atInsnStart && !isspace(*Str)) {
137       Length += MaxInstLength;
138       atInsnStart = false;
139     }
140     if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
141       atInsnStart = false;
142   }
143
144   return Length;
145 }
146
147 unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
148                                               bool Global) const {
149   return dwarf::DW_EH_PE_absptr;
150 }
151
152 static bool isSuitableForBSS(const GlobalVariable *GV) {
153   if (!GV->hasInitializer())
154     return true;
155
156   // Leave constant zeros in readonly constant sections, so they can be shared
157   Constant *C = GV->getInitializer();
158   return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
159 }
160
161 SectionKind::Kind
162 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
163   // Early exit - functions should be always in text sections.
164   if (isa<Function>(GV))
165     return SectionKind::Text;
166
167   const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
168   bool isThreadLocal = GVar->isThreadLocal();
169   assert(GVar && "Invalid global value for section selection");
170
171   SectionKind::Kind kind;
172   if (isSuitableForBSS(GVar)) {
173     // Variable can be easily put to BSS section.
174     return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
175   } else if (GVar->isConstant() && !isThreadLocal) {
176     // Now we know, that varible has initializer and it is constant. We need to
177     // check its initializer to decide, which section to output it into. Also
178     // note, there is no thread-local r/o section.
179     Constant *C = GVar->getInitializer();
180     if (C->ContainsRelocations())
181       kind = SectionKind::ROData;
182     else {
183       const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
184       // Check, if initializer is a null-terminated string
185       if (CVA && CVA->isCString())
186         kind = SectionKind::RODataMergeStr;
187       else
188         kind = SectionKind::RODataMergeConst;
189     }
190   }
191
192   // Variable is not constant or thread-local - emit to generic data section.
193   return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
194 }