Reapply my previous asmprinter changes now with more testing and two
[oota-llvm.git] / lib / Target / DarwinTargetAsmInfo.cpp
1 //===-- DarwinTargetAsmInfo.cpp - Darwin asm properties ---------*- C++ -*-===//
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 in general on Darwin-based targets
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Mangler.h"
22 #include "llvm/Target/DarwinTargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetData.h"
25
26 using namespace llvm;
27
28 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) 
29   : TargetAsmInfo(TM) {
30
31   CStringSection_ = getUnnamedSection("\t.cstring",
32                                 SectionFlags::Mergeable | SectionFlags::Strings);
33   FourByteConstantSection = getUnnamedSection("\t.literal4\n",
34                                               SectionFlags::Mergeable);
35   EightByteConstantSection = getUnnamedSection("\t.literal8\n",
36                                                SectionFlags::Mergeable);
37
38   // Note: 16-byte constant section is subtarget specific and should be provided
39   // there, if needed.
40   SixteenByteConstantSection = 0;
41
42   ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
43
44   TextCoalSection =
45     getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
46                     SectionFlags::Code);
47   ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
48                                          SectionFlags::None);
49   ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
50                                          SectionFlags::None);
51   ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
52   DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
53                                     SectionFlags::Writeable);
54     
55   
56   // Common settings for all Darwin targets.
57   // Syntax:
58   GlobalPrefix = "_";
59   PrivateGlobalPrefix = "L";
60   LessPrivateGlobalPrefix = "l";  // Marker for some ObjC metadata
61   StringConstantPrefix = "\1LC";
62   NeedsSet = true;
63   NeedsIndirectEncoding = true;
64   AllowQuotesInName = true;
65   HasSingleParameterDotFile = false;
66
67   // In non-PIC modes, emit a special label before jump tables so that the
68   // linker can perform more accurate dead code stripping.  We do not check the
69   // relocation model here since it can be overridden later.
70   JumpTableSpecialLabelPrefix = "l";
71     
72   // Directives:
73   WeakDefDirective = "\t.weak_definition ";
74   WeakRefDirective = "\t.weak_reference ";
75   HiddenDirective = "\t.private_extern ";
76     
77   // Sections:
78   CStringSection = "\t.cstring";
79   JumpTableDataSection = "\t.const\n";
80   BSSSection = 0;
81
82   if (TM.getRelocationModel() == Reloc::Static) {
83     StaticCtorsSection = ".constructor";
84     StaticDtorsSection = ".destructor";
85   } else {
86     StaticCtorsSection = ".mod_init_func";
87     StaticDtorsSection = ".mod_term_func";
88   }
89     
90   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
91   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
92   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
93   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
94   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
95   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
96   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
97   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
98   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
99   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
100   DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
101 }
102
103 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
104 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
105 /// directive emitted (this occurs in ObjC metadata).
106 bool
107 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
108                                           Mangler *Mang) const {
109   if (GV==0)
110     return false;
111   
112   /// FIXME: WHAT IS THIS?
113   
114   if (GV->hasLocalLinkage() && !isa<Function>(GV) &&
115       ((strlen(getPrivateGlobalPrefix()) != 0 &&
116         Mang->getMangledName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
117           getPrivateGlobalPrefix()) ||
118        (strlen(getLessPrivateGlobalPrefix()) != 0 &&
119         Mang->getMangledName(GV).substr(0,
120                                         strlen(getLessPrivateGlobalPrefix())) ==
121           getLessPrivateGlobalPrefix())))
122     return false;
123   return true;
124 }
125
126 const Section*
127 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
128   SectionKind::Kind Kind = SectionKindForGlobal(GV);
129   bool isWeak = GV->isWeakForLinker();
130   bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
131
132   switch (Kind) {
133    case SectionKind::Text:
134     if (isWeak)
135       return TextCoalSection;
136     else
137       return TextSection;
138    case SectionKind::Data:
139    case SectionKind::ThreadData:
140    case SectionKind::BSS:
141    case SectionKind::ThreadBSS:
142     if (cast<GlobalVariable>(GV)->isConstant())
143       return (isWeak ? ConstDataCoalSection : ConstDataSection);
144     else
145       return (isWeak ? DataCoalSection : DataSection);
146    case SectionKind::ROData:
147     return (isWeak ? ConstDataCoalSection :
148             (isNonStatic ? ConstDataSection : getReadOnlySection()));
149    case SectionKind::RODataMergeStr:
150     return (isWeak ?
151             ConstTextCoalSection :
152             MergeableStringSection(cast<GlobalVariable>(GV)));
153    case SectionKind::RODataMergeConst:
154     return (isWeak ?
155             ConstDataCoalSection:
156             MergeableConstSection(cast<GlobalVariable>(GV)));
157    default:
158     llvm_unreachable("Unsuported section kind for global");
159   }
160
161   // FIXME: Do we have any extra special weird cases?
162   return NULL;
163 }
164
165 const Section*
166 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
167   const TargetData *TD = TM.getTargetData();
168   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
169   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
170
171   unsigned Size = TD->getTypeAllocSize(Ty);
172   if (Size) {
173     unsigned Align = TD->getPreferredAlignment(GV);
174     if (Align <= 32)
175       return getCStringSection_();
176   }
177
178   return getReadOnlySection();
179 }
180
181 const Section*
182 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
183   Constant *C = GV->getInitializer();
184
185   return MergeableConstSection(C->getType());
186 }
187
188 inline const Section*
189 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
190   const TargetData *TD = TM.getTargetData();
191
192   unsigned Size = TD->getTypeAllocSize(Ty);
193   if (Size == 4)
194     return FourByteConstantSection;
195   else if (Size == 8)
196     return EightByteConstantSection;
197   else if (Size == 16 && SixteenByteConstantSection)
198     return SixteenByteConstantSection;
199
200   return getReadOnlySection();
201 }
202
203 const Section*
204 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
205   const Section* S = MergeableConstSection(Ty);
206
207   // Handle weird special case, when compiling PIC stuff.
208   if (S == getReadOnlySection() &&
209       TM.getRelocationModel() != Reloc::Static)
210     return ConstDataSection;
211
212   return S;
213 }
214
215 std::string
216 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
217                                                SectionKind::Kind kind) const {
218   llvm_unreachable("Darwin does not use unique sections");
219   return "";
220 }