244b689a552e377cc8714733b70cd357497d0b60
[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_ = getOrCreateSection("\t.cstring", true,
32                                        SectionKind::MergeableCString);
33   FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
34                                                SectionKind::MergeableConst4);
35   EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
36                                                 SectionKind::MergeableConst8);
37   SixteenByteConstantSection = 
38     getOrCreateSection("\t.literal16\n", true, SectionKind::MergeableConst16);
39
40   ReadOnlySection = getOrCreateSection("\t.const", true, SectionKind::ReadOnly);
41
42   TextCoalSection =
43     getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
44                        false, SectionKind::Text);
45   ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
46                                             false, SectionKind::Text);
47   ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
48                                             false, SectionKind::Text);
49   ConstDataSection = getOrCreateSection("\t.const_data", true,
50                                         SectionKind::ReadOnlyWithRel);
51   DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
52                                        false, SectionKind::DataRel);
53     
54   
55   // Common settings for all Darwin targets.
56   // Syntax:
57   GlobalPrefix = "_";
58   PrivateGlobalPrefix = "L";
59   LinkerPrivateGlobalPrefix = "l";  // Marker for some ObjC metadata
60   NeedsSet = true;
61   NeedsIndirectEncoding = true;
62   AllowQuotesInName = true;
63   HasSingleParameterDotFile = false;
64
65   // In non-PIC modes, emit a special label before jump tables so that the
66   // linker can perform more accurate dead code stripping.  We do not check the
67   // relocation model here since it can be overridden later.
68   JumpTableSpecialLabelPrefix = "l";
69     
70   // Directives:
71   WeakDefDirective = "\t.weak_definition ";
72   WeakRefDirective = "\t.weak_reference ";
73   HiddenDirective = "\t.private_extern ";
74     
75   // Sections:
76   CStringSection = "\t.cstring";
77   JumpTableDataSection = "\t.const";
78   BSSSection = 0;
79
80   if (TM.getRelocationModel() == Reloc::Static) {
81     StaticCtorsSection = ".constructor";
82     StaticDtorsSection = ".destructor";
83   } else {
84     StaticCtorsSection = ".mod_init_func";
85     StaticDtorsSection = ".mod_term_func";
86   }
87     
88   // _foo.eh symbols are currently always exported so that the linker knows
89   // about them.  This may not strictly be necessary on 10.6 and later, but it
90   // doesn't hurt anything.
91   Is_EHSymbolPrivate = false;
92     
93   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
94   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
95   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
96   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
97   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
98   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
99   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
100   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
101   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
102   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
103   DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
104 }
105
106 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
107 /// the PrivateGlobalPrefix or the LinkerPrivateGlobalPrefix does not have the
108 /// directive emitted (this occurs in ObjC metadata).
109 bool DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
110                                                Mangler *Mang) const {
111   if (!GV) return false;
112   
113   // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
114   if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
115     // FIXME: ObjC metadata is currently emitted as internal symbols that have
116     // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
117     // this horrible hack can go away.
118     const std::string &Name = Mang->getMangledName(GV);
119     if (Name[0] == 'L' || Name[0] == 'l')
120       return false;
121   }
122
123   return true;
124 }
125
126 const Section*
127 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
128                                             SectionKind Kind) const {
129   assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
130   
131   if (Kind.isText())
132     return Kind.isWeak() ? TextCoalSection : TextSection;
133   
134   // If this is weak/linkonce, put this in a coalescable section, either in text
135   // or data depending on if it is writable.
136   if (Kind.isWeak()) {
137     if (Kind.isReadOnly())
138       return ConstTextCoalSection;
139     return DataCoalSection;
140   }
141   
142   // FIXME: Alignment check should be handled by section classifier.
143   if (Kind.isMergeableCString())
144     return MergeableStringSection(cast<GlobalVariable>(GV));
145   
146   if (Kind.isMergeableConst()) {
147     if (Kind.isMergeableConst4())
148       return FourByteConstantSection;
149     if (Kind.isMergeableConst8())
150       return EightByteConstantSection;
151     if (Kind.isMergeableConst16())
152       return SixteenByteConstantSection;
153     return ReadOnlySection;  // .const
154   }
155   
156   // FIXME: ROData -> const in -static mode that is relocatable but they happen
157   // by the static linker.  Why not mergeable?
158   if (Kind.isReadOnly())
159     return getReadOnlySection();
160
161   // If this is marked const, put it into a const section.  But if the dynamic
162   // linker needs to write to it, put it in the data segment.
163   if (Kind.isReadOnlyWithRel())
164     return ConstDataSection;
165   
166   // Otherwise, just drop the variable in the normal data section.
167   return DataSection;
168 }
169
170 const Section*
171 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
172   const TargetData *TD = TM.getTargetData();
173   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
174   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
175
176   unsigned Size = TD->getTypeAllocSize(Ty);
177   if (Size) {
178     unsigned Align = TD->getPreferredAlignment(GV);
179     if (Align <= 32)
180       return getCStringSection_();
181   }
182
183   return getReadOnlySection();
184 }
185
186 const Section *
187 DarwinTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const {
188   // If this constant requires a relocation, we have to put it in the data
189   // segment, not in the text segment.
190   if (Kind.isDataRel())
191     return ConstDataSection;
192   
193   if (Kind.isMergeableConst4())
194     return FourByteConstantSection;
195   if (Kind.isMergeableConst8())
196     return EightByteConstantSection;
197   if (Kind.isMergeableConst16())
198     return SixteenByteConstantSection;
199   return ReadOnlySection;  // .const
200 }
201