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/ErrorHandling.h"
21 #include "llvm/Support/Mangler.h"
22 #include "llvm/Target/DarwinTargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetData.h"
28 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM)
31 CStringSection_ = getUnnamedSection("\t.cstring",
32 SectionFlags::Mergeable | SectionFlags::Strings);
33 FourByteConstantSection = getUnnamedSection("\t.literal4\n",
34 SectionFlags::Mergeable);
35 EightByteConstantSection = getUnnamedSection("\t.literal8\n",
36 SectionFlags::Mergeable);
38 // Note: 16-byte constant section is subtarget specific and should be provided
40 SixteenByteConstantSection = 0;
42 ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
45 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
47 ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
49 ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
51 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
52 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
53 SectionFlags::Writeable);
56 // Common settings for all Darwin targets.
59 PrivateGlobalPrefix = "L";
60 LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
61 StringConstantPrefix = "\1LC";
63 NeedsIndirectEncoding = true;
64 AllowQuotesInName = true;
65 HasSingleParameterDotFile = false;
67 // In non-PIC modes, emit a special label before jump tables so that the
68 // linker can perform more accurate dead code stripping. We do not check the
69 // relocation model here since it can be overridden later.
70 JumpTableSpecialLabelPrefix = "l";
73 WeakDefDirective = "\t.weak_definition ";
74 WeakRefDirective = "\t.weak_reference ";
75 HiddenDirective = "\t.private_extern ";
78 CStringSection = "\t.cstring";
79 JumpTableDataSection = "\t.const\n";
82 if (TM.getRelocationModel() == Reloc::Static) {
83 StaticCtorsSection = ".constructor";
84 StaticDtorsSection = ".destructor";
86 StaticCtorsSection = ".mod_init_func";
87 StaticDtorsSection = ".mod_term_func";
90 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
91 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
92 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
93 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
94 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
95 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
96 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
97 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
98 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
99 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
100 DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
103 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
104 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
105 /// directive emitted (this occurs in ObjC metadata).
108 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
109 Mangler *Mang) const {
112 if (GV->hasLocalLinkage() && !isa<Function>(GV) &&
113 ((strlen(getPrivateGlobalPrefix()) != 0 &&
114 Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
115 getPrivateGlobalPrefix()) ||
116 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
117 Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
118 getLessPrivateGlobalPrefix())))
124 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
125 SectionKind::Kind Kind = SectionKindForGlobal(GV);
126 bool isWeak = GV->isWeakForLinker();
127 bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
130 case SectionKind::Text:
132 return TextCoalSection;
135 case SectionKind::Data:
136 case SectionKind::ThreadData:
137 case SectionKind::BSS:
138 case SectionKind::ThreadBSS:
139 if (cast<GlobalVariable>(GV)->isConstant())
140 return (isWeak ? ConstDataCoalSection : ConstDataSection);
142 return (isWeak ? DataCoalSection : DataSection);
143 case SectionKind::ROData:
144 return (isWeak ? ConstDataCoalSection :
145 (isNonStatic ? ConstDataSection : getReadOnlySection()));
146 case SectionKind::RODataMergeStr:
148 ConstTextCoalSection :
149 MergeableStringSection(cast<GlobalVariable>(GV)));
150 case SectionKind::RODataMergeConst:
152 ConstDataCoalSection:
153 MergeableConstSection(cast<GlobalVariable>(GV)));
155 LLVM_UNREACHABLE("Unsuported section kind for global");
158 // FIXME: Do we have any extra special weird cases?
163 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
164 const TargetData *TD = TM.getTargetData();
165 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
166 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
168 unsigned Size = TD->getTypeAllocSize(Ty);
170 unsigned Align = TD->getPreferredAlignment(GV);
172 return getCStringSection_();
175 return getReadOnlySection();
179 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
180 Constant *C = GV->getInitializer();
182 return MergeableConstSection(C->getType());
185 inline const Section*
186 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
187 const TargetData *TD = TM.getTargetData();
189 unsigned Size = TD->getTypeAllocSize(Ty);
191 return FourByteConstantSection;
193 return EightByteConstantSection;
194 else if (Size == 16 && SixteenByteConstantSection)
195 return SixteenByteConstantSection;
197 return getReadOnlySection();
201 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
202 const Section* S = MergeableConstSection(Ty);
204 // Handle weird special case, when compiling PIC stuff.
205 if (S == getReadOnlySection() &&
206 TM.getRelocationModel() != Reloc::Static)
207 return ConstDataSection;
213 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
214 SectionKind::Kind kind) const {
215 LLVM_UNREACHABLE("Darwin does not use unique sections");