blob: 7ab3967c0d1bf2bd0c006d223ce927ac402a78ee [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
31 CStringSection_ = getUnnamedSection("\t.cstring",
32 SectionFlags::Mergeable | SectionFlags::Strings);
Anton Korobeynikov64818732008-09-24 22:18:54 +000033 FourByteConstantSection = getUnnamedSection("\t.literal4\n",
34 SectionFlags::Mergeable);
35 EightByteConstantSection = getUnnamedSection("\t.literal8\n",
Anton Korobeynikov745e8642008-07-19 13:14:46 +000036 SectionFlags::Mergeable);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000037
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000038 // Note: 16-byte constant section is subtarget specific and should be provided
Anton Korobeynikov64818732008-09-24 22:18:54 +000039 // there, if needed.
40 SixteenByteConstantSection = 0;
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000041
Anton Korobeynikov00181a32008-09-24 22:20:27 +000042 ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000043
Anton Korobeynikov745e8642008-07-19 13:14:46 +000044 TextCoalSection =
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000045 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Anton Korobeynikov745e8642008-07-19 13:14:46 +000046 SectionFlags::Code);
Dale Johannesen585457e2008-10-08 21:49:47 +000047 ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
48 SectionFlags::None);
Anton Korobeynikov00181a32008-09-24 22:20:27 +000049 ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000050 SectionFlags::None);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000051 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000052 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
53 SectionFlags::Writeable);
Chris Lattner4e0f25b2009-06-19 00:08:39 +000054
55
56 // Common settings for all Darwin targets.
57 // Syntax:
58 GlobalPrefix = "_";
59 PrivateGlobalPrefix = "L";
60 LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
Chris Lattner4e0f25b2009-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
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";
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000100}
101
Dale Johannesend2e51af2008-09-09 22:29:13 +0000102/// 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).
Dale Johannesend2e51af2008-09-09 22:29:13 +0000105bool
106DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
107 Mangler *Mang) const {
108 if (GV==0)
109 return false;
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000110
111 /// FIXME: WHAT IS THIS?
112
Rafael Espindolabb46f522009-01-15 20:18:42 +0000113 if (GV->hasLocalLinkage() && !isa<Function>(GV) &&
Dale Johannesend2e51af2008-09-09 22:29:13 +0000114 ((strlen(getPrivateGlobalPrefix()) != 0 &&
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000115 Mang->getMangledName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
Dale Johannesend2e51af2008-09-09 22:29:13 +0000116 getPrivateGlobalPrefix()) ||
117 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000118 Mang->getMangledName(GV).substr(0,
119 strlen(getLessPrivateGlobalPrefix())) ==
Dale Johannesend2e51af2008-09-09 22:29:13 +0000120 getLessPrivateGlobalPrefix())))
121 return false;
122 return true;
123}
124
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000125const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +0000126DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000127 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Duncan Sands667d4b82009-03-07 15:45:40 +0000128 bool isWeak = GV->isWeakForLinker();
Dan Gohman8f092252008-11-03 18:22:42 +0000129 bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000130
131 switch (Kind) {
132 case SectionKind::Text:
Evan Cheng42ccc212008-08-08 17:56:50 +0000133 if (isWeak)
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000134 return TextCoalSection;
135 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +0000136 return TextSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000137 case SectionKind::Data:
138 case SectionKind::ThreadData:
139 case SectionKind::BSS:
140 case SectionKind::ThreadBSS:
141 if (cast<GlobalVariable>(GV)->isConstant())
Evan Cheng42ccc212008-08-08 17:56:50 +0000142 return (isWeak ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000143 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +0000144 return (isWeak ? DataCoalSection : DataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000145 case SectionKind::ROData:
Evan Cheng42ccc212008-08-08 17:56:50 +0000146 return (isWeak ? ConstDataCoalSection :
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000147 (isNonStatic ? ConstDataSection : getReadOnlySection()));
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000148 case SectionKind::RODataMergeStr:
Evan Cheng42ccc212008-08-08 17:56:50 +0000149 return (isWeak ?
Dale Johannesen585457e2008-10-08 21:49:47 +0000150 ConstTextCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000151 MergeableStringSection(cast<GlobalVariable>(GV)));
152 case SectionKind::RODataMergeConst:
Evan Cheng42ccc212008-08-08 17:56:50 +0000153 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000154 ConstDataCoalSection:
155 MergeableConstSection(cast<GlobalVariable>(GV)));
156 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000157 llvm_unreachable("Unsuported section kind for global");
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000158 }
159
160 // FIXME: Do we have any extra special weird cases?
Devang Patel8a84e442009-01-05 17:31:22 +0000161 return NULL;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000162}
163
164const Section*
165DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000166 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000167 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000168 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000169
Duncan Sands777d2302009-05-09 07:06:46 +0000170 unsigned Size = TD->getTypeAllocSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000171 if (Size) {
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000172 unsigned Align = TD->getPreferredAlignment(GV);
173 if (Align <= 32)
174 return getCStringSection_();
175 }
176
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000177 return getReadOnlySection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000178}
179
180const Section*
181DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +0000182 Constant *C = GV->getInitializer();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000183
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000184 return MergeableConstSection(C->getType());
185}
186
187inline const Section*
188DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000189 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000190
Duncan Sands777d2302009-05-09 07:06:46 +0000191 unsigned Size = TD->getTypeAllocSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000192 if (Size == 4)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000193 return FourByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000194 else if (Size == 8)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000195 return EightByteConstantSection;
196 else if (Size == 16 && SixteenByteConstantSection)
197 return SixteenByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000198
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000199 return getReadOnlySection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000200}
201
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000202const Section*
203DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
204 const Section* S = MergeableConstSection(Ty);
205
206 // Handle weird special case, when compiling PIC stuff.
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000207 if (S == getReadOnlySection() &&
Dan Gohman8f092252008-11-03 18:22:42 +0000208 TM.getRelocationModel() != Reloc::Static)
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000209 return ConstDataSection;
210
211 return S;
212}
213
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000214std::string
215DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
216 SectionKind::Kind kind) const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000217 llvm_unreachable("Darwin does not use unique sections");
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000218 return "";
219}