blob: 244b689a552e377cc8714733b70cd357497d0b60 [file] [log] [blame]
Anton Korobeynikov745e8642008-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"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Dale Johannesend2e51af2008-09-09 22:29:13 +000021#include "llvm/Support/Mangler.h"
Anton Korobeynikov745e8642008-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 Gohman8f092252008-11-03 18:22:42 +000028DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM)
29 : TargetAsmInfo(TM) {
Anton Korobeynikov745e8642008-07-19 13:14:46 +000030
Chris Lattnere3466942009-07-27 06:17:14 +000031 CStringSection_ = getOrCreateSection("\t.cstring", true,
32 SectionKind::MergeableCString);
33 FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
34 SectionKind::MergeableConst4);
35 EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
36 SectionKind::MergeableConst8);
Chris Lattnerde3a1072009-07-27 15:44:04 +000037 SixteenByteConstantSection =
38 getOrCreateSection("\t.literal16\n", true, SectionKind::MergeableConst16);
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000039
Chris Lattnere3466942009-07-27 06:17:14 +000040 ReadOnlySection = getOrCreateSection("\t.const", true, SectionKind::ReadOnly);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000041
Anton Korobeynikov745e8642008-07-19 13:14:46 +000042 TextCoalSection =
Chris Lattnere3466942009-07-27 06:17:14 +000043 getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
44 false, SectionKind::Text);
45 ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
46 false, SectionKind::Text);
47 ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
48 false, SectionKind::Text);
49 ConstDataSection = getOrCreateSection("\t.const_data", true,
50 SectionKind::ReadOnlyWithRel);
51 DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
52 false, SectionKind::DataRel);
Chris Lattner4e0f25b2009-06-19 00:08:39 +000053
54
55 // Common settings for all Darwin targets.
56 // Syntax:
57 GlobalPrefix = "_";
58 PrivateGlobalPrefix = "L";
Chris Lattner90f8b702009-07-21 17:30:51 +000059 LinkerPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
Chris Lattner4e0f25b2009-06-19 00:08:39 +000060 NeedsSet = true;
61 NeedsIndirectEncoding = true;
62 AllowQuotesInName = true;
63 HasSingleParameterDotFile = false;
64
65 // In non-PIC modes, emit a special label before jump tables so that the
66 // linker can perform more accurate dead code stripping. We do not check the
67 // relocation model here since it can be overridden later.
68 JumpTableSpecialLabelPrefix = "l";
69
70 // Directives:
71 WeakDefDirective = "\t.weak_definition ";
72 WeakRefDirective = "\t.weak_reference ";
73 HiddenDirective = "\t.private_extern ";
74
75 // Sections:
76 CStringSection = "\t.cstring";
Chris Lattner30c4a3b2009-07-26 01:24:18 +000077 JumpTableDataSection = "\t.const";
Chris Lattner4e0f25b2009-06-19 00:08:39 +000078 BSSSection = 0;
79
80 if (TM.getRelocationModel() == Reloc::Static) {
81 StaticCtorsSection = ".constructor";
82 StaticDtorsSection = ".destructor";
83 } else {
84 StaticCtorsSection = ".mod_init_func";
85 StaticDtorsSection = ".mod_term_func";
86 }
87
Chris Lattnere2cf37b2009-07-17 20:46:40 +000088 // _foo.eh symbols are currently always exported so that the linker knows
89 // about them. This may not strictly be necessary on 10.6 and later, but it
90 // doesn't hurt anything.
91 Is_EHSymbolPrivate = false;
92
Chris Lattner4e0f25b2009-06-19 00:08:39 +000093 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
94 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
95 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
96 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
97 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
98 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
99 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
100 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
101 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
102 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
103 DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000104}
105
Dale Johannesend2e51af2008-09-09 22:29:13 +0000106/// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
Chris Lattner90f8b702009-07-21 17:30:51 +0000107/// the PrivateGlobalPrefix or the LinkerPrivateGlobalPrefix does not have the
Dale Johannesend2e51af2008-09-09 22:29:13 +0000108/// directive emitted (this occurs in ObjC metadata).
Bill Wendlingecc2da82009-07-20 21:38:26 +0000109bool DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
110 Mangler *Mang) const {
111 if (!GV) return false;
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000112
Chris Lattner90f8b702009-07-21 17:30:51 +0000113 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
Bill Wendlingecc2da82009-07-20 21:38:26 +0000114 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
Chris Lattner449e3792009-07-21 22:32:55 +0000115 // FIXME: ObjC metadata is currently emitted as internal symbols that have
116 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
117 // this horrible hack can go away.
Bill Wendlingecc2da82009-07-20 21:38:26 +0000118 const std::string &Name = Mang->getMangledName(GV);
Chris Lattner449e3792009-07-21 22:32:55 +0000119 if (Name[0] == 'L' || Name[0] == 'l')
Bill Wendlingecc2da82009-07-20 21:38:26 +0000120 return false;
121 }
122
Dale Johannesend2e51af2008-09-09 22:29:13 +0000123 return true;
124}
125
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000126const Section*
Chris Lattnerf20f2502009-07-24 18:42:53 +0000127DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattner460d51e2009-07-25 23:21:55 +0000128 SectionKind Kind) const {
Chris Lattnere346e182009-07-26 05:44:20 +0000129 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Chris Lattnere3577da2009-07-26 00:51:36 +0000130
Chris Lattnere346e182009-07-26 05:44:20 +0000131 if (Kind.isText())
Chris Lattner2b421da2009-07-26 07:07:01 +0000132 return Kind.isWeak() ? TextCoalSection : TextSection;
Chris Lattnere3577da2009-07-26 00:51:36 +0000133
134 // If this is weak/linkonce, put this in a coalescable section, either in text
135 // or data depending on if it is writable.
Chris Lattner2b421da2009-07-26 07:07:01 +0000136 if (Kind.isWeak()) {
Chris Lattnere3577da2009-07-26 00:51:36 +0000137 if (Kind.isReadOnly())
138 return ConstTextCoalSection;
139 return DataCoalSection;
140 }
141
142 // FIXME: Alignment check should be handled by section classifier.
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000143 if (Kind.isMergeableCString())
Chris Lattnere3577da2009-07-26 00:51:36 +0000144 return MergeableStringSection(cast<GlobalVariable>(GV));
145
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000146 if (Kind.isMergeableConst()) {
147 if (Kind.isMergeableConst4())
Chris Lattner8ca52092009-07-26 06:11:33 +0000148 return FourByteConstantSection;
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000149 if (Kind.isMergeableConst8())
Chris Lattner8ca52092009-07-26 06:11:33 +0000150 return EightByteConstantSection;
Chris Lattnerde3a1072009-07-27 15:44:04 +0000151 if (Kind.isMergeableConst16())
Chris Lattner8ca52092009-07-26 06:11:33 +0000152 return SixteenByteConstantSection;
153 return ReadOnlySection; // .const
Chris Lattnerf1581562009-07-22 00:47:11 +0000154 }
Chris Lattner30c4a3b2009-07-26 01:24:18 +0000155
156 // FIXME: ROData -> const in -static mode that is relocatable but they happen
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000157 // by the static linker. Why not mergeable?
Chris Lattner30c4a3b2009-07-26 01:24:18 +0000158 if (Kind.isReadOnly())
159 return getReadOnlySection();
Chris Lattnere346e182009-07-26 05:44:20 +0000160
161 // If this is marked const, put it into a const section. But if the dynamic
162 // linker needs to write to it, put it in the data segment.
163 if (Kind.isReadOnlyWithRel())
164 return ConstDataSection;
Chris Lattner30c4a3b2009-07-26 01:24:18 +0000165
166 // Otherwise, just drop the variable in the normal data section.
167 return DataSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000168}
169
170const Section*
171DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000172 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000173 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000174 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000175
Duncan Sands777d2302009-05-09 07:06:46 +0000176 unsigned Size = TD->getTypeAllocSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000177 if (Size) {
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000178 unsigned Align = TD->getPreferredAlignment(GV);
179 if (Align <= 32)
180 return getCStringSection_();
181 }
182
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000183 return getReadOnlySection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000184}
185
Chris Lattner298414e2009-07-22 00:28:43 +0000186const Section *
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000187DarwinTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const {
Chris Lattner298414e2009-07-22 00:28:43 +0000188 // If this constant requires a relocation, we have to put it in the data
189 // segment, not in the text segment.
Chris Lattner5c2f7892009-07-26 06:26:55 +0000190 if (Kind.isDataRel())
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000191 return ConstDataSection;
Chris Lattner298414e2009-07-22 00:28:43 +0000192
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000193 if (Kind.isMergeableConst4())
Chris Lattner298414e2009-07-22 00:28:43 +0000194 return FourByteConstantSection;
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000195 if (Kind.isMergeableConst8())
Chris Lattner298414e2009-07-22 00:28:43 +0000196 return EightByteConstantSection;
Chris Lattnerde3a1072009-07-27 15:44:04 +0000197 if (Kind.isMergeableConst16())
Chris Lattner5c2f7892009-07-26 06:26:55 +0000198 return SixteenByteConstantSection;
Chris Lattner298414e2009-07-22 00:28:43 +0000199 return ReadOnlySection; // .const
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000200}
201