blob: 6be9aa0285bfbfa01568ccea302ce469926ce351 [file] [log] [blame]
aslf80e6082008-07-19 13:14:46 +00001//===-- DarwinTargetAsmInfo.cpp - Darwin asm properties ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines target asm properties related what form asm statements
11// should take in general on Darwin-based targets
12//
13//===----------------------------------------------------------------------===//
14
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"
Edwin Török675d5622009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Dale Johannesen60567622008-09-09 22:29:13 +000021#include "llvm/Support/Mangler.h"
aslf80e6082008-07-19 13:14:46 +000022#include "llvm/Target/DarwinTargetAsmInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetData.h"
25
26using namespace llvm;
27
Dan Gohmana004d7b2008-11-03 18:22:42 +000028DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM)
29 : TargetAsmInfo(TM) {
aslf80e6082008-07-19 13:14:46 +000030
31 CStringSection_ = getUnnamedSection("\t.cstring",
Chris Lattner680c6f62009-07-22 00:28:43 +000032 SectionFlags::Mergeable |SectionFlags::Strings);
Anton Korobeynikovbce78512008-09-24 22:18:54 +000033 FourByteConstantSection = getUnnamedSection("\t.literal4\n",
34 SectionFlags::Mergeable);
35 EightByteConstantSection = getUnnamedSection("\t.literal8\n",
aslf80e6082008-07-19 13:14:46 +000036 SectionFlags::Mergeable);
asldedcd842008-07-19 13:16:11 +000037
aslcf0f6c22008-07-19 13:15:46 +000038 // Note: 16-byte constant section is subtarget specific and should be provided
Anton Korobeynikovbce78512008-09-24 22:18:54 +000039 // there, if needed.
40 SixteenByteConstantSection = 0;
aslcf0f6c22008-07-19 13:15:46 +000041
Anton Korobeynikov4d433222008-09-24 22:20:27 +000042 ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
aslf80e6082008-07-19 13:14:46 +000043
aslf80e6082008-07-19 13:14:46 +000044 TextCoalSection =
Anton Korobeynikov8abb7582008-09-24 22:19:13 +000045 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
aslf80e6082008-07-19 13:14:46 +000046 SectionFlags::Code);
Dale Johannesen65851472008-10-08 21:49:47 +000047 ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
48 SectionFlags::None);
Anton Korobeynikov4d433222008-09-24 22:20:27 +000049 ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
Anton Korobeynikov8abb7582008-09-24 22:19:13 +000050 SectionFlags::None);
aslf80e6082008-07-19 13:14:46 +000051 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
Anton Korobeynikov8abb7582008-09-24 22:19:13 +000052 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
Chris Lattner4c530ee2009-07-25 18:57:34 +000053 SectionFlags::Writable);
Chris Lattnerca072172009-06-19 00:08:39 +000054
55
56 // Common settings for all Darwin targets.
57 // Syntax:
58 GlobalPrefix = "_";
59 PrivateGlobalPrefix = "L";
Chris Lattnerc889b3b2009-07-21 17:30:51 +000060 LinkerPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
Chris Lattnerca072172009-06-19 00:08:39 +000061 NeedsSet = true;
62 NeedsIndirectEncoding = true;
63 AllowQuotesInName = true;
64 HasSingleParameterDotFile = false;
65
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";
70
71 // Directives:
72 WeakDefDirective = "\t.weak_definition ";
73 WeakRefDirective = "\t.weak_reference ";
74 HiddenDirective = "\t.private_extern ";
75
76 // Sections:
77 CStringSection = "\t.cstring";
78 JumpTableDataSection = "\t.const\n";
79 BSSSection = 0;
80
81 if (TM.getRelocationModel() == Reloc::Static) {
82 StaticCtorsSection = ".constructor";
83 StaticDtorsSection = ".destructor";
84 } else {
85 StaticCtorsSection = ".mod_init_func";
86 StaticDtorsSection = ".mod_term_func";
87 }
88
Chris Lattner5c3475e2009-07-17 20:46:40 +000089 // _foo.eh symbols are currently always exported so that the linker knows
90 // about them. This may not strictly be necessary on 10.6 and later, but it
91 // doesn't hurt anything.
92 Is_EHSymbolPrivate = false;
93
Chris Lattnerca072172009-06-19 00:08:39 +000094 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
95 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
96 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
97 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
98 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
99 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
100 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
101 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
102 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
103 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
104 DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
aslf80e6082008-07-19 13:14:46 +0000105}
106
Dale Johannesen60567622008-09-09 22:29:13 +0000107/// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
Chris Lattnerc889b3b2009-07-21 17:30:51 +0000108/// the PrivateGlobalPrefix or the LinkerPrivateGlobalPrefix does not have the
Dale Johannesen60567622008-09-09 22:29:13 +0000109/// directive emitted (this occurs in ObjC metadata).
Bill Wendlingfbefc072009-07-20 21:38:26 +0000110bool DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
111 Mangler *Mang) const {
112 if (!GV) return false;
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000113
Chris Lattnerc889b3b2009-07-21 17:30:51 +0000114 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Bill Wendlingfbefc072009-07-20 21:38:26 +0000115 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
Chris Lattner303a75c2009-07-21 22:32:55 +0000116 // FIXME: ObjC metadata is currently emitted as internal symbols that have
117 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
118 // this horrible hack can go away.
Bill Wendlingfbefc072009-07-20 21:38:26 +0000119 const std::string &Name = Mang->getMangledName(GV);
Chris Lattner303a75c2009-07-21 22:32:55 +0000120 if (Name[0] == 'L' || Name[0] == 'l')
Bill Wendlingfbefc072009-07-20 21:38:26 +0000121 return false;
122 }
123
Dale Johannesen60567622008-09-09 22:29:13 +0000124 return true;
125}
126
aslf80e6082008-07-19 13:14:46 +0000127const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000128DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +0000129 SectionKind Kind) const {
Chris Lattnerd49fec92009-07-26 00:51:36 +0000130 assert(!Kind.isTLS() && "Darwin doesn't support TLS");
131
Chris Lattner4c530ee2009-07-25 18:57:34 +0000132 // FIXME: Use sectionflags:linkonce instead of isWeakForLinker() here.
Duncan Sands19d161f2009-03-07 15:45:40 +0000133 bool isWeak = GV->isWeakForLinker();
Dan Gohmana004d7b2008-11-03 18:22:42 +0000134 bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
aslf80e6082008-07-19 13:14:46 +0000135
Chris Lattnerd49fec92009-07-26 00:51:36 +0000136 if (Kind.isCode())
137 return isWeak ? TextCoalSection : TextSection;
138
139 // If this is weak/linkonce, put this in a coalescable section, either in text
140 // or data depending on if it is writable.
141 if (isWeak) {
142 if (Kind.isReadOnly())
143 return ConstTextCoalSection;
144 return DataCoalSection;
145 }
146
147 // FIXME: Alignment check should be handled by section classifier.
148 if (Kind.isMergableString())
149 return MergeableStringSection(cast<GlobalVariable>(GV));
150
Chris Lattnercc195212009-07-25 23:21:55 +0000151 switch (Kind.getKind()) {
Chris Lattner81466bc2009-07-22 00:47:11 +0000152 case SectionKind::Data:
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000153 case SectionKind::DataRelLocal:
154 case SectionKind::DataRel:
Chris Lattner81466bc2009-07-22 00:47:11 +0000155 case SectionKind::BSS:
aslf80e6082008-07-19 13:14:46 +0000156 if (cast<GlobalVariable>(GV)->isConstant())
Chris Lattnerd49fec92009-07-26 00:51:36 +0000157 return ConstDataSection;
158 return DataSection;
Chris Lattner88025bd2009-07-24 05:10:25 +0000159
Chris Lattner81466bc2009-07-22 00:47:11 +0000160 case SectionKind::ROData:
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000161 case SectionKind::DataRelRO:
162 case SectionKind::DataRelROLocal:
Chris Lattnerd49fec92009-07-26 00:51:36 +0000163 return isNonStatic ? ConstDataSection : getReadOnlySection();
Chris Lattner81466bc2009-07-22 00:47:11 +0000164 case SectionKind::RODataMergeConst: {
Chris Lattner81466bc2009-07-22 00:47:11 +0000165 const Type *Ty = cast<GlobalVariable>(GV)->getInitializer()->getType();
166 const TargetData *TD = TM.getTargetData();
167 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
168 }
169 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000170 llvm_unreachable("Unsuported section kind for global");
aslf80e6082008-07-19 13:14:46 +0000171 }
172
173 // FIXME: Do we have any extra special weird cases?
Devang Patelf3707e82009-01-05 17:31:22 +0000174 return NULL;
aslf80e6082008-07-19 13:14:46 +0000175}
176
177const Section*
178DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000179 const TargetData *TD = TM.getTargetData();
aslf80e6082008-07-19 13:14:46 +0000180 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000181 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
aslf80e6082008-07-19 13:14:46 +0000182
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000183 unsigned Size = TD->getTypeAllocSize(Ty);
aslf80e6082008-07-19 13:14:46 +0000184 if (Size) {
aslf80e6082008-07-19 13:14:46 +0000185 unsigned Align = TD->getPreferredAlignment(GV);
186 if (Align <= 32)
187 return getCStringSection_();
188 }
189
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000190 return getReadOnlySection();
aslf80e6082008-07-19 13:14:46 +0000191}
192
Chris Lattner680c6f62009-07-22 00:28:43 +0000193const Section *
194DarwinTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
195 unsigned ReloInfo) const {
196 // If this constant requires a relocation, we have to put it in the data
197 // segment, not in the text segment.
198 if (ReloInfo != 0)
Anton Korobeynikovfee95e02008-08-07 09:51:02 +0000199 return ConstDataSection;
Chris Lattner680c6f62009-07-22 00:28:43 +0000200
201 switch (Size) {
202 default: break;
203 case 4:
204 return FourByteConstantSection;
205 case 8:
206 return EightByteConstantSection;
207 case 16:
208 if (SixteenByteConstantSection)
209 return SixteenByteConstantSection;
210 break;
211 }
212
213 return ReadOnlySection; // .const
Anton Korobeynikovfee95e02008-08-07 09:51:02 +0000214}
215