1804adf6a4c1bcf90f7b4e15b2c3757b63d745bf
[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", 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("\t.const_data", SectionFlags::None);
52   DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
53                                     SectionFlags::Writable);
54     
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   // FIXME: Use sectionflags:linkonce instead of isWeakForLinker() here.
133   bool isWeak = GV->isWeakForLinker();
134
135   if (Kind.isText())
136     return isWeak ? TextCoalSection : TextSection;
137   
138   // If this is weak/linkonce, put this in a coalescable section, either in text
139   // or data depending on if it is writable.
140   if (isWeak) {
141     if (Kind.isReadOnly())
142       return ConstTextCoalSection;
143     return DataCoalSection;
144   }
145   
146   // FIXME: Alignment check should be handled by section classifier.
147   if (Kind.isMergableCString())
148     return MergeableStringSection(cast<GlobalVariable>(GV));
149   
150   if (Kind.isMergableConst()) {
151     if (Kind.isMergableConst4())
152       return FourByteConstantSection;
153     if (Kind.isMergableConst8())
154       return EightByteConstantSection;
155     if (Kind.isMergableConst16() && SixteenByteConstantSection)
156       return SixteenByteConstantSection;
157     return ReadOnlySection;  // .const
158   }
159   
160   // FIXME: ROData -> const in -static mode that is relocatable but they happen
161   // by the static linker.  Why not mergable?
162   if (Kind.isReadOnly())
163     return getReadOnlySection();
164
165   // If this is marked const, put it into a const section.  But if the dynamic
166   // linker needs to write to it, put it in the data segment.
167   if (Kind.isReadOnlyWithRel())
168     return ConstDataSection;
169   
170   // Otherwise, just drop the variable in the normal data section.
171   return DataSection;
172 }
173
174 const Section*
175 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
176   const TargetData *TD = TM.getTargetData();
177   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
178   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
179
180   unsigned Size = TD->getTypeAllocSize(Ty);
181   if (Size) {
182     unsigned Align = TD->getPreferredAlignment(GV);
183     if (Align <= 32)
184       return getCStringSection_();
185   }
186
187   return getReadOnlySection();
188 }
189
190 const Section *
191 DarwinTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
192                                                    unsigned ReloInfo) const {
193   // If this constant requires a relocation, we have to put it in the data
194   // segment, not in the text segment.
195   if (ReloInfo != 0)
196     return ConstDataSection;
197   
198   switch (Size) {
199   default: break;
200   case 4:
201     return FourByteConstantSection;
202   case 8:
203     return EightByteConstantSection;
204   case 16:
205     if (SixteenByteConstantSection)
206       return SixteenByteConstantSection;
207     break;
208   }
209   
210   return ReadOnlySection;  // .const
211 }
212