1 //===-- DarwinTargetAsmInfo.cpp - Darwin asm properties ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target asm properties related what form asm statements
11 // should take in general on Darwin-based targets
13 //===----------------------------------------------------------------------===//
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"
27 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine
&TM
)
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
);
37 // Note: 16-byte constant section is subtarget specific and should be provided
39 SixteenByteConstantSection
= 0;
41 ReadOnlySection
= getUnnamedSection("\t.const\n", SectionFlags::None
);
44 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
46 ConstTextCoalSection
= getNamedSection("\t__TEXT,__const_coal,coalesced",
48 ConstDataCoalSection
= getNamedSection("\t__DATA,__const_coal,coalesced",
50 ConstDataSection
= getUnnamedSection(".const_data", SectionFlags::None
);
51 DataCoalSection
= getNamedSection("\t__DATA,__datacoal_nt,coalesced",
52 SectionFlags::Writeable
);
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).
60 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue
* GV
,
61 Mangler
*Mang
) const {
64 if (GV
->hasLocalLinkage() && !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())))
76 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue
*GV
) const {
77 SectionKind::Kind Kind
= SectionKindForGlobal(GV
);
78 bool isWeak
= GV
->isWeakForLinker();
79 bool isNonStatic
= TM
.getRelocationModel() != Reloc::Static
;
82 case SectionKind::Text
:
84 return TextCoalSection
;
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
);
94 return (isWeak
? DataCoalSection
: DataSection
);
95 case SectionKind::ROData
:
96 return (isWeak
? ConstDataCoalSection
:
97 (isNonStatic
? ConstDataSection
: getReadOnlySection()));
98 case SectionKind::RODataMergeStr
:
100 ConstTextCoalSection
:
101 MergeableStringSection(cast
<GlobalVariable
>(GV
)));
102 case SectionKind::RODataMergeConst
:
104 ConstDataCoalSection
:
105 MergeableConstSection(cast
<GlobalVariable
>(GV
)));
107 assert(0 && "Unsuported section kind for global");
110 // FIXME: Do we have any extra special weird cases?
115 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable
*GV
) const {
116 const TargetData
*TD
= TM
.getTargetData();
117 Constant
*C
= cast
<GlobalVariable
>(GV
)->getInitializer();
118 const Type
*Ty
= cast
<ArrayType
>(C
->getType())->getElementType();
120 unsigned Size
= TD
->getTypeAllocSize(Ty
);
122 unsigned Align
= TD
->getPreferredAlignment(GV
);
124 return getCStringSection_();
127 return getReadOnlySection();
131 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable
*GV
) const {
132 Constant
*C
= GV
->getInitializer();
134 return MergeableConstSection(C
->getType());
137 inline const Section
*
138 DarwinTargetAsmInfo::MergeableConstSection(const Type
*Ty
) const {
139 const TargetData
*TD
= TM
.getTargetData();
141 unsigned Size
= TD
->getTypeAllocSize(Ty
);
143 return FourByteConstantSection
;
145 return EightByteConstantSection
;
146 else if (Size
== 16 && SixteenByteConstantSection
)
147 return SixteenByteConstantSection
;
149 return getReadOnlySection();
153 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type
*Ty
) const {
154 const Section
* S
= MergeableConstSection(Ty
);
156 // Handle weird special case, when compiling PIC stuff.
157 if (S
== getReadOnlySection() &&
158 TM
.getRelocationModel() != Reloc::Static
)
159 return ConstDataSection
;
165 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue
* GV
,
166 SectionKind::Kind kind
) const {
167 assert(0 && "Darwin does not use unique sections");