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