Get rid of ReadOnlySection duplicate
[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/Mangler.h"
21 #include "llvm/Target/DarwinTargetAsmInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetData.h"
24
25 using namespace llvm;
26
27 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) {
28   DTM = &TM;
29
30   CStringSection_ = getUnnamedSection("\t.cstring",
31                                 SectionFlags::Mergeable | SectionFlags::Strings);
32   FourByteConstantSection = getUnnamedSection("\t.literal4\n",
33                                               SectionFlags::Mergeable);
34   EightByteConstantSection = getUnnamedSection("\t.literal8\n",
35                                                SectionFlags::Mergeable);
36
37   // Note: 16-byte constant section is subtarget specific and should be provided
38   // there, if needed.
39   SixteenByteConstantSection = 0;
40
41   ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
42
43   TextCoalSection =
44     getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
45                     SectionFlags::Code);
46   ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
47                                          SectionFlags::None);
48   ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
49   DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
50                                     SectionFlags::Writeable);
51 }
52
53 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
54 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
55 /// directive emitted (this occurs in ObjC metadata).
56
57 bool
58 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
59                                           Mangler *Mang) const {
60   if (GV==0)
61     return false;
62   if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
63       ((strlen(getPrivateGlobalPrefix()) != 0 &&
64         Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
65           getPrivateGlobalPrefix()) ||
66        (strlen(getLessPrivateGlobalPrefix()) != 0 &&
67         Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
68           getLessPrivateGlobalPrefix())))
69     return false;
70   return true;
71 }
72
73 const Section*
74 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
75   SectionKind::Kind Kind = SectionKindForGlobal(GV);
76   bool isWeak = GV->isWeakForLinker();
77   bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
78
79   switch (Kind) {
80    case SectionKind::Text:
81     if (isWeak)
82       return TextCoalSection;
83     else
84       return TextSection;
85    case SectionKind::Data:
86    case SectionKind::ThreadData:
87    case SectionKind::BSS:
88    case SectionKind::ThreadBSS:
89     if (cast<GlobalVariable>(GV)->isConstant())
90       return (isWeak ? ConstDataCoalSection : ConstDataSection);
91     else
92       return (isWeak ? DataCoalSection : DataSection);
93    case SectionKind::ROData:
94     return (isWeak ? ConstDataCoalSection :
95             (isNonStatic ? ConstDataSection : getReadOnlySection()));
96    case SectionKind::RODataMergeStr:
97     return (isWeak ?
98             ConstDataCoalSection :
99             MergeableStringSection(cast<GlobalVariable>(GV)));
100    case SectionKind::RODataMergeConst:
101     return (isWeak ?
102             ConstDataCoalSection:
103             MergeableConstSection(cast<GlobalVariable>(GV)));
104    default:
105     assert(0 && "Unsuported section kind for global");
106   }
107
108   // FIXME: Do we have any extra special weird cases?
109 }
110
111 const Section*
112 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
113   const TargetData *TD = DTM->getTargetData();
114   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
115   const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
116
117   unsigned Size = TD->getABITypeSize(Type);
118   if (Size) {
119     const TargetData *TD = DTM->getTargetData();
120     unsigned Align = TD->getPreferredAlignment(GV);
121     if (Align <= 32)
122       return getCStringSection_();
123   }
124
125   return getReadOnlySection();
126 }
127
128 const Section*
129 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
130   Constant *C = GV->getInitializer();
131
132   return MergeableConstSection(C->getType());
133 }
134
135 inline const Section*
136 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
137   const TargetData *TD = DTM->getTargetData();
138
139   unsigned Size = TD->getABITypeSize(Ty);
140   if (Size == 4)
141     return FourByteConstantSection;
142   else if (Size == 8)
143     return EightByteConstantSection;
144   else if (Size == 16 && SixteenByteConstantSection)
145     return SixteenByteConstantSection;
146
147   return getReadOnlySection();
148 }
149
150 const Section*
151 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
152   const Section* S = MergeableConstSection(Ty);
153
154   // Handle weird special case, when compiling PIC stuff.
155   if (S == getReadOnlySection() &&
156       DTM->getRelocationModel() != Reloc::Static)
157     return ConstDataSection;
158
159   return S;
160 }
161
162 std::string
163 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
164                                                SectionKind::Kind kind) const {
165   assert(0 && "Darwin does not use unique sections");
166   return "";
167 }