XMM6-XMM15 are callee-saved on Win64. Patch by Nicolas Capens!
[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.
39
40   ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
41
42   // FIXME: These should be named sections, really.
43   TextCoalSection =
44   getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
45                     SectionFlags::Code);
46   ConstDataCoalSection =
47     getUnnamedSection(".section __DATA,__const_coal,coalesced",
48                       SectionFlags::None);
49   ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
50   DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
51                                       SectionFlags::Writeable);
52 }
53
54 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
55 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
56 /// directive emitted (this occurs in ObjC metadata).
57
58 bool
59 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
60                                           Mangler *Mang) const {
61   if (GV==0)
62     return false;
63   if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
64       ((strlen(getPrivateGlobalPrefix()) != 0 &&
65         Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
66           getPrivateGlobalPrefix()) ||
67        (strlen(getLessPrivateGlobalPrefix()) != 0 &&
68         Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
69           getLessPrivateGlobalPrefix())))
70     return false;
71   return true;
72 }
73
74 const Section*
75 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
76   SectionKind::Kind Kind = SectionKindForGlobal(GV);
77   bool isWeak = GV->isWeakForLinker();
78   bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
79
80   switch (Kind) {
81    case SectionKind::Text:
82     if (isWeak)
83       return TextCoalSection;
84     else
85       return getTextSection_();
86    case SectionKind::Data:
87    case SectionKind::ThreadData:
88    case SectionKind::BSS:
89    case SectionKind::ThreadBSS:
90     if (cast<GlobalVariable>(GV)->isConstant())
91       return (isWeak ? ConstDataCoalSection : ConstDataSection);
92     else
93       return (isWeak ? DataCoalSection : getDataSection_());
94    case SectionKind::ROData:
95     return (isWeak ? ConstDataCoalSection :
96             (isNonStatic ? ConstDataSection : getReadOnlySection_()));
97    case SectionKind::RODataMergeStr:
98     return (isWeak ?
99             ConstDataCoalSection :
100             MergeableStringSection(cast<GlobalVariable>(GV)));
101    case SectionKind::RODataMergeConst:
102     return (isWeak ?
103             ConstDataCoalSection:
104             MergeableConstSection(cast<GlobalVariable>(GV)));
105    default:
106     assert(0 && "Unsuported section kind for global");
107   }
108
109   // FIXME: Do we have any extra special weird cases?
110 }
111
112 const Section*
113 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
114   const TargetData *TD = DTM->getTargetData();
115   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
116   const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
117
118   unsigned Size = TD->getABITypeSize(Type);
119   if (Size) {
120     const TargetData *TD = DTM->getTargetData();
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 = cast<GlobalVariable>(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 = DTM->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       DTM->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 }