Fix a regression from 76124. Thumb1 instructions default to S bit being true.
[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   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\n";
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 LessPrivateGlobalPrefix does not have the
109 /// directive emitted (this occurs in ObjC metadata).
110 bool
111 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
112                                           Mangler *Mang) const {
113   if (GV==0)
114     return false;
115   
116   /// FIXME: WHAT IS THIS?
117   
118   if (GV->hasLocalLinkage() && !isa<Function>(GV) &&
119       ((strlen(getPrivateGlobalPrefix()) != 0 &&
120         Mang->getMangledName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
121           getPrivateGlobalPrefix()) ||
122        (strlen(getLessPrivateGlobalPrefix()) != 0 &&
123         Mang->getMangledName(GV).substr(0,
124                                         strlen(getLessPrivateGlobalPrefix())) ==
125           getLessPrivateGlobalPrefix())))
126     return false;
127   return true;
128 }
129
130 const Section*
131 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
132   SectionKind::Kind Kind = SectionKindForGlobal(GV);
133   bool isWeak = GV->isWeakForLinker();
134   bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
135
136   switch (Kind) {
137    case SectionKind::Text:
138     if (isWeak)
139       return TextCoalSection;
140     else
141       return TextSection;
142    case SectionKind::Data:
143    case SectionKind::ThreadData:
144    case SectionKind::BSS:
145    case SectionKind::ThreadBSS:
146     if (cast<GlobalVariable>(GV)->isConstant())
147       return (isWeak ? ConstDataCoalSection : ConstDataSection);
148     else
149       return (isWeak ? DataCoalSection : DataSection);
150    case SectionKind::ROData:
151     return (isWeak ? ConstDataCoalSection :
152             (isNonStatic ? ConstDataSection : getReadOnlySection()));
153    case SectionKind::RODataMergeStr:
154     return (isWeak ?
155             ConstTextCoalSection :
156             MergeableStringSection(cast<GlobalVariable>(GV)));
157    case SectionKind::RODataMergeConst:
158     return (isWeak ?
159             ConstDataCoalSection:
160             MergeableConstSection(cast<GlobalVariable>(GV)));
161    default:
162     llvm_unreachable("Unsuported section kind for global");
163   }
164
165   // FIXME: Do we have any extra special weird cases?
166   return NULL;
167 }
168
169 const Section*
170 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
171   const TargetData *TD = TM.getTargetData();
172   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
173   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
174
175   unsigned Size = TD->getTypeAllocSize(Ty);
176   if (Size) {
177     unsigned Align = TD->getPreferredAlignment(GV);
178     if (Align <= 32)
179       return getCStringSection_();
180   }
181
182   return getReadOnlySection();
183 }
184
185 const Section*
186 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
187   Constant *C = GV->getInitializer();
188
189   return MergeableConstSection(C->getType());
190 }
191
192 inline const Section*
193 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
194   const TargetData *TD = TM.getTargetData();
195
196   unsigned Size = TD->getTypeAllocSize(Ty);
197   if (Size == 4)
198     return FourByteConstantSection;
199   else if (Size == 8)
200     return EightByteConstantSection;
201   else if (Size == 16 && SixteenByteConstantSection)
202     return SixteenByteConstantSection;
203
204   return getReadOnlySection();
205 }
206
207 const Section*
208 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
209   const Section* S = MergeableConstSection(Ty);
210
211   // Handle weird special case, when compiling PIC stuff.
212   if (S == getReadOnlySection() &&
213       TM.getRelocationModel() != Reloc::Static)
214     return ConstDataSection;
215
216   return S;
217 }
218
219 std::string
220 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
221                                                SectionKind::Kind kind) const {
222   llvm_unreachable("Darwin does not use unique sections");
223   return "";
224 }