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