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
40 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
42 // FIXME: These should be named sections, really.
44 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
46 ConstDataCoalSection =
47 getUnnamedSection(".section __DATA,__const_coal,coalesced",
49 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
50 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
51 SectionFlags::Writeable);
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).
59 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
60 Mangler *Mang) const {
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())))
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);
81 case SectionKind::Text:
83 return TextCoalSection;
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);
93 return (isWeak ? DataCoalSection : DataSection);
94 case SectionKind::ROData:
95 return (isWeak ? ConstDataCoalSection :
96 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
97 case SectionKind::RODataMergeStr:
99 ConstDataCoalSection :
100 MergeableStringSection(cast<GlobalVariable>(GV)));
101 case SectionKind::RODataMergeConst:
103 ConstDataCoalSection:
104 MergeableConstSection(cast<GlobalVariable>(GV)));
106 assert(0 && "Unsuported section kind for global");
109 // FIXME: Do we have any extra special weird cases?
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();
118 unsigned Size = TD->getABITypeSize(Type);
120 const TargetData *TD = DTM->getTargetData();
121 unsigned Align = TD->getPreferredAlignment(GV);
123 return getCStringSection_();
126 return getReadOnlySection_();
130 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
131 Constant *C = GV->getInitializer();
133 return MergeableConstSection(C->getType());
136 inline const Section*
137 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
138 const TargetData *TD = DTM->getTargetData();
140 unsigned Size = TD->getABITypeSize(Ty);
142 return FourByteConstantSection_;
144 return EightByteConstantSection_;
145 else if (Size == 16 && SixteenByteConstantSection_)
146 return SixteenByteConstantSection_;
148 return getReadOnlySection_();
152 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
153 const Section* S = MergeableConstSection(Ty);
155 // Handle weird special case, when compiling PIC stuff.
156 if (S == getReadOnlySection_() &&
157 DTM->getRelocationModel() != Reloc::Static)
158 return ConstDataSection;
164 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
165 SectionKind::Kind kind) const {
166 assert(0 && "Darwin does not use unique sections");