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 ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
48 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
49 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
50 SectionFlags::Writeable);
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).
58 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
59 Mangler *Mang) const {
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())))
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);
80 case SectionKind::Text:
82 return TextCoalSection;
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);
92 return (isWeak ? DataCoalSection : DataSection);
93 case SectionKind::ROData:
94 return (isWeak ? ConstDataCoalSection :
95 (isNonStatic ? ConstDataSection : getReadOnlySection()));
96 case SectionKind::RODataMergeStr:
98 ConstDataCoalSection :
99 MergeableStringSection(cast<GlobalVariable>(GV)));
100 case SectionKind::RODataMergeConst:
102 ConstDataCoalSection:
103 MergeableConstSection(cast<GlobalVariable>(GV)));
105 assert(0 && "Unsuported section kind for global");
108 // FIXME: Do we have any extra special weird cases?
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();
117 unsigned Size = TD->getABITypeSize(Type);
119 const TargetData *TD = DTM->getTargetData();
120 unsigned Align = TD->getPreferredAlignment(GV);
122 return getCStringSection_();
125 return getReadOnlySection();
129 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
130 Constant *C = GV->getInitializer();
132 return MergeableConstSection(C->getType());
135 inline const Section*
136 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
137 const TargetData *TD = DTM->getTargetData();
139 unsigned Size = TD->getABITypeSize(Ty);
141 return FourByteConstantSection;
143 return EightByteConstantSection;
144 else if (Size == 16 && SixteenByteConstantSection)
145 return SixteenByteConstantSection;
147 return getReadOnlySection();
151 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
152 const Section* S = MergeableConstSection(Ty);
154 // Handle weird special case, when compiling PIC stuff.
155 if (S == getReadOnlySection() &&
156 DTM->getRelocationModel() != Reloc::Static)
157 return ConstDataSection;
163 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
164 SectionKind::Kind kind) const {
165 assert(0 && "Darwin does not use unique sections");