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 // Common settings for all Darwin targets.
58 PrivateGlobalPrefix = "L";
59 LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
60 StringConstantPrefix = "\1LC";
62 NeedsIndirectEncoding = true;
63 AllowQuotesInName = true;
64 HasSingleParameterDotFile = false;
66 // In non-PIC modes, emit a special label before jump tables so that the
67 // linker can perform more accurate dead code stripping. We do not check the
68 // relocation model here since it can be overridden later.
69 JumpTableSpecialLabelPrefix = "l";
72 WeakDefDirective = "\t.weak_definition ";
73 WeakRefDirective = "\t.weak_reference ";
74 HiddenDirective = "\t.private_extern ";
77 CStringSection = "\t.cstring";
78 JumpTableDataSection = "\t.const\n";
81 if (TM.getRelocationModel() == Reloc::Static) {
82 StaticCtorsSection = ".constructor";
83 StaticDtorsSection = ".destructor";
85 StaticCtorsSection = ".mod_init_func";
86 StaticDtorsSection = ".mod_term_func";
89 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
90 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
91 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
92 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
93 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
94 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
95 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
96 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
97 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
98 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
99 DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
102 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
103 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
104 /// directive emitted (this occurs in ObjC metadata).
107 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
108 Mangler *Mang) const {
111 if (GV->hasLocalLinkage() && !isa<Function>(GV) &&
112 ((strlen(getPrivateGlobalPrefix()) != 0 &&
113 Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
114 getPrivateGlobalPrefix()) ||
115 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
116 Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
117 getLessPrivateGlobalPrefix())))
123 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
124 SectionKind::Kind Kind = SectionKindForGlobal(GV);
125 bool isWeak = GV->isWeakForLinker();
126 bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
129 case SectionKind::Text:
131 return TextCoalSection;
134 case SectionKind::Data:
135 case SectionKind::ThreadData:
136 case SectionKind::BSS:
137 case SectionKind::ThreadBSS:
138 if (cast<GlobalVariable>(GV)->isConstant())
139 return (isWeak ? ConstDataCoalSection : ConstDataSection);
141 return (isWeak ? DataCoalSection : DataSection);
142 case SectionKind::ROData:
143 return (isWeak ? ConstDataCoalSection :
144 (isNonStatic ? ConstDataSection : getReadOnlySection()));
145 case SectionKind::RODataMergeStr:
147 ConstTextCoalSection :
148 MergeableStringSection(cast<GlobalVariable>(GV)));
149 case SectionKind::RODataMergeConst:
151 ConstDataCoalSection:
152 MergeableConstSection(cast<GlobalVariable>(GV)));
154 assert(0 && "Unsuported section kind for global");
157 // FIXME: Do we have any extra special weird cases?
162 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
163 const TargetData *TD = TM.getTargetData();
164 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
165 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
167 unsigned Size = TD->getTypeAllocSize(Ty);
169 unsigned Align = TD->getPreferredAlignment(GV);
171 return getCStringSection_();
174 return getReadOnlySection();
178 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
179 Constant *C = GV->getInitializer();
181 return MergeableConstSection(C->getType());
184 inline const Section*
185 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
186 const TargetData *TD = TM.getTargetData();
188 unsigned Size = TD->getTypeAllocSize(Ty);
190 return FourByteConstantSection;
192 return EightByteConstantSection;
193 else if (Size == 16 && SixteenByteConstantSection)
194 return SixteenByteConstantSection;
196 return getReadOnlySection();
200 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
201 const Section* S = MergeableConstSection(Ty);
203 // Handle weird special case, when compiling PIC stuff.
204 if (S == getReadOnlySection() &&
205 TM.getRelocationModel() != Reloc::Static)
206 return ConstDataSection;
212 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
213 SectionKind::Kind kind) const {
214 assert(0 && "Darwin does not use unique sections");