blob: 965e8d6b77a9e2d63a9fd014af83c9b626a765a6 [file] [log] [blame]
Jim Laskey8e8de8f2006-09-07 22:05:02 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey8e8de8f2006-09-07 22:05:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declarations of the X86TargetAsmInfo properties.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetAsmInfo.h"
15#include "X86TargetMachine.h"
16#include "X86Subtarget.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000017#include "llvm/DerivedTypes.h"
Chris Lattner625bd052006-11-29 01:14:06 +000018#include "llvm/InlineAsm.h"
19#include "llvm/Instructions.h"
Chris Lattner3e9f1d02007-04-01 20:49:36 +000020#include "llvm/Intrinsics.h"
Chris Lattner625bd052006-11-29 01:14:06 +000021#include "llvm/Module.h"
22#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000023#include "llvm/Support/Dwarf.h"
24
Jim Laskey8e8de8f2006-09-07 22:05:02 +000025using namespace llvm;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000026using namespace llvm::dwarf;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000027
Dan Gohmancfbb2f02008-03-25 21:45:14 +000028static const char *const x86_asm_table[] = {
29 "{si}", "S",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000030 "{di}", "D",
31 "{ax}", "a",
32 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000033 "{memory}", "memory",
34 "{flags}", "",
35 "{dirflag}", "",
36 "{fpsr}", "",
37 "{cc}", "cc",
38 0,0};
39
Jim Laskey8e8de8f2006-09-07 22:05:02 +000040X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
41 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000042 X86TM = &TM;
43
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000044 AsmTransCBE = x86_asm_table;
Anton Korobeynikov82100452008-07-09 13:20:27 +000045
Bill Wendlingcb900992007-01-16 09:29:17 +000046 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +000047}
Chris Lattnerafbfded2006-10-05 02:43:52 +000048
Chris Lattner625bd052006-11-29 01:14:06 +000049bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
50 // FIXME: this should verify that we are targetting a 486 or better. If not,
51 // we will turn this bswap into something that will be lowered to logical ops
52 // instead of emitting the bswap asm. For now, we don't support 486 or lower
53 // so don't worry about this.
Anton Korobeynikov82100452008-07-09 13:20:27 +000054
Chris Lattner625bd052006-11-29 01:14:06 +000055 // Verify this is a simple bswap.
56 if (CI->getNumOperands() != 2 ||
57 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +000058 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +000059 return false;
Anton Korobeynikov82100452008-07-09 13:20:27 +000060
Chris Lattner3e9f1d02007-04-01 20:49:36 +000061 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
62 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +000063 return false;
Anton Korobeynikov82100452008-07-09 13:20:27 +000064
Chris Lattner625bd052006-11-29 01:14:06 +000065 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +000066 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +000067 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +000068 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Anton Korobeynikov82100452008-07-09 13:20:27 +000069
Chris Lattner625bd052006-11-29 01:14:06 +000070 Value *Op = CI->getOperand(1);
Gabor Greif051a9502008-04-06 20:25:17 +000071 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Anton Korobeynikov82100452008-07-09 13:20:27 +000072
Chris Lattner625bd052006-11-29 01:14:06 +000073 CI->replaceAllUsesWith(Op);
74 CI->eraseFromParent();
75 return true;
76}
77
78
79bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
80 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +000081 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Anton Korobeynikov82100452008-07-09 13:20:27 +000082
Chris Lattner625bd052006-11-29 01:14:06 +000083 std::string AsmStr = IA->getAsmString();
Anton Korobeynikov82100452008-07-09 13:20:27 +000084
Chris Lattner625bd052006-11-29 01:14:06 +000085 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
86 std::vector<std::string> AsmPieces;
87 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
Anton Korobeynikov82100452008-07-09 13:20:27 +000088
Chris Lattner625bd052006-11-29 01:14:06 +000089 switch (AsmPieces.size()) {
Anton Korobeynikov82100452008-07-09 13:20:27 +000090 default: return false;
Chris Lattner625bd052006-11-29 01:14:06 +000091 case 1:
92 AsmStr = AsmPieces[0];
93 AsmPieces.clear();
94 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
Anton Korobeynikov82100452008-07-09 13:20:27 +000095
Chris Lattner5d521352006-11-29 01:48:01 +000096 // bswap $0
Anton Korobeynikov82100452008-07-09 13:20:27 +000097 if (AsmPieces.size() == 2 &&
Chris Lattner625bd052006-11-29 01:14:06 +000098 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
99 // No need to check constraints, nothing other than the equivalent of
100 // "=r,0" would be valid here.
101 return LowerToBSwap(CI);
102 }
103 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000104 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000105 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000106 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
107 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
108 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
109 std::vector<std::string> Words;
110 SplitString(AsmPieces[0], Words, " \t");
111 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
112 Words.clear();
113 SplitString(AsmPieces[1], Words, " \t");
114 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
115 Words.clear();
116 SplitString(AsmPieces[2], Words, " \t,");
117 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
118 Words[2] == "%edx") {
119 return LowerToBSwap(CI);
120 }
121 }
122 }
123 }
124 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000125 }
126 return false;
127}
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000128
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000129X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
130 X86TargetAsmInfo(TM) {
131 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
132
133 AlignmentIsInBytes = false;
134 TextAlignFillValue = 0x90;
135 GlobalPrefix = "_";
136 if (!is64Bit)
137 Data64bitsDirective = 0; // we can't emit a 64-bit unit
138 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
139 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
140 BSSSection = 0; // no BSS section.
141 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
142 ConstantPoolSection = "\t.const\n";
143 JumpTableDataSection = "\t.const\n";
144 CStringSection = "\t.cstring";
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000145 CStringSection_ = getUnnamedSection("\t.cstring",
146 SectionFlags::Mergeable | SectionFlags::Strings);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000147 FourByteConstantSection = "\t.literal4\n";
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000148 FourByteConstantSection_ = getUnnamedSection("\t.literal4\n",
149 SectionFlags::Mergeable);
150 EightByteConstantSection_ = getUnnamedSection("\t.literal8\n",
151 SectionFlags::Mergeable);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000152 // FIXME: Why don't always use this section?
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000153 if (is64Bit) {
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000154 SixteenByteConstantSection = "\t.literal16\n";
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000155 SixteenByteConstantSection_ = getUnnamedSection("\t.literal16\n",
156 SectionFlags::Mergeable);
157 }
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000158 ReadOnlySection = "\t.const\n";
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000159 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
Anton Korobeynikovc5a7e402008-07-09 13:30:02 +0000160 // FIXME: These should be named sections, really.
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000161 TextCoalSection =
162 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
163 SectionFlags::Code);
164 ConstDataCoalSection =
165 getUnnamedSection(".section __DATA,__const_coal,coalesced",
166 SectionFlags::None);
167 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
168 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
169 SectionFlags::Writeable);
170
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000171 LCOMMDirective = "\t.lcomm\t";
172 SwitchToSectionDirective = "\t.section ";
173 StringConstantPrefix = "\1LC";
174 COMMDirectiveTakesAlignment = false;
175 HasDotTypeDotSizeDirective = false;
176 if (TM.getRelocationModel() == Reloc::Static) {
177 StaticCtorsSection = ".constructor";
178 StaticDtorsSection = ".destructor";
179 } else {
180 StaticCtorsSection = ".mod_init_func";
181 StaticDtorsSection = ".mod_term_func";
182 }
183 if (is64Bit) {
184 PersonalityPrefix = "";
185 PersonalitySuffix = "+4@GOTPCREL";
186 } else {
187 PersonalityPrefix = "L";
188 PersonalitySuffix = "$non_lazy_ptr";
189 }
190 NeedsIndirectEncoding = true;
191 InlineAsmStart = "## InlineAsm Start";
192 InlineAsmEnd = "## InlineAsm End";
193 CommentString = "##";
194 SetDirective = "\t.set";
195 PCSymbol = ".";
196 UsedDirective = "\t.no_dead_strip\t";
197 WeakDefDirective = "\t.weak_definition ";
198 WeakRefDirective = "\t.weak_reference ";
199 HiddenDirective = "\t.private_extern ";
200 ProtectedDirective = "\t.globl\t";
201
202 // In non-PIC modes, emit a special label before jump tables so that the
203 // linker can perform more accurate dead code stripping.
204 if (TM.getRelocationModel() != Reloc::PIC_) {
205 // Emit a local label that is preserved until the linker runs.
206 JumpTableSpecialLabelPrefix = "l";
207 }
208
209 SupportsDebugInformation = true;
210 NeedsSet = true;
211 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
212 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
213 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
214 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
215 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
216 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
217 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
218 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
219 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
220 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
221 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
222
223 // Exceptions handling
224 SupportsExceptionHandling = true;
225 GlobalEHDirective = "\t.globl\t";
226 SupportsWeakOmittedEHFrame = false;
227 AbsoluteEHSectionOffsets = false;
228 DwarfEHFrameSection =
229 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
230 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
231}
232
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000233unsigned
234X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
235 bool Global) const {
236 if (Reason == DwarfEncoding::Functions && Global)
237 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
238 else if (Reason == DwarfEncoding::CodeLabels || !Global)
239 return DW_EH_PE_pcrel;
240 else
241 return DW_EH_PE_absptr;
242}
243
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000244const Section*
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000245X86DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
246 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000247 bool isWeak = GV->isWeakForLinker();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000248
249 switch (Kind) {
250 case SectionKind::Text:
251 if (isWeak)
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000252 return TextCoalSection;
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000253 else
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000254 return getTextSection_();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000255 case SectionKind::Data:
256 case SectionKind::ThreadData:
257 case SectionKind::BSS:
258 case SectionKind::ThreadBSS:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000259 if (cast<GlobalVariable>(GV)->isConstant())
260 return (isWeak ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000261 else
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000262 return (isWeak ? DataCoalSection : getDataSection_());
263 case SectionKind::ROData:
264 return (isWeak ? ConstDataCoalSection : getReadOnlySection_());
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000265 case SectionKind::RODataMergeStr:
Anton Korobeynikovf56c2f72008-07-09 19:06:02 +0000266 return (isWeak ?
267 ConstDataCoalSection :
268 MergeableStringSection(cast<GlobalVariable>(GV)));
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000269 case SectionKind::RODataMergeConst:
Anton Korobeynikovf56c2f72008-07-09 19:06:02 +0000270 return (isWeak ?
271 ConstDataCoalSection:
272 MergeableConstSection(cast<GlobalVariable>(GV)));
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000273 default:
274 assert(0 && "Unsuported section kind for global");
275 }
276
277 // FIXME: Do we have any extra special weird cases?
278}
279
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000280const Section*
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000281X86DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000282 const TargetData *TD = X86TM->getTargetData();
283 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
284 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000285
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000286 unsigned Size = TD->getABITypeSize(Type);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000287 if (Size) {
288 const TargetData *TD = X86TM->getTargetData();
289 unsigned Align = TD->getPreferredAlignment(GV);
290 if (Align <= 32)
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000291 return getCStringSection_();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000292 }
293
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000294 return getReadOnlySection_();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000295}
296
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000297const Section*
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000298X86DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000299 const TargetData *TD = X86TM->getTargetData();
300 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000301
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000302 unsigned Size = TD->getABITypeSize(C->getType());
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000303 if (Size == 4)
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000304 return FourByteConstantSection_;
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000305 else if (Size == 8)
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000306 return EightByteConstantSection_;
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000307 else if (Size == 16 && X86TM->getSubtarget<X86Subtarget>().is64Bit())
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000308 return SixteenByteConstantSection_;
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000309
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000310 return getReadOnlySection_();
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000311}
312
313std::string
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000314X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
315 SectionKind::Kind kind) const {
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000316 assert(0 && "Darwin does not use unique sections");
317 return "";
318}
319
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000320X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
321 X86TargetAsmInfo(TM) {
322 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
323
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000324 TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code);
325 DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
326 BSSSection_ = getUnnamedSection("\t.bss",
327 SectionFlags::Writeable | SectionFlags::BSS);
Anton Korobeynikova3af0bf2008-07-09 13:29:27 +0000328 ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None);
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000329 TLSDataSection_ = getNamedSection("\t.tdata",
330 SectionFlags::Writeable | SectionFlags::TLS);
331 TLSBSSSection_ = getNamedSection("\t.tbss",
332 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
333
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000334 ReadOnlySection = ".rodata";
Anton Korobeynikov2a889172008-07-09 13:25:46 +0000335 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
336 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
337 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000338 CStringSection = ".rodata.str";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000339 PrivateGlobalPrefix = ".L";
340 WeakRefDirective = "\t.weak\t";
341 SetDirective = "\t.set\t";
342 PCSymbol = ".";
343
344 // Set up DWARF directives
345 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
346
347 // Debug Information
348 AbsoluteDebugSectionOffsets = true;
349 SupportsDebugInformation = true;
350 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
351 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
352 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
353 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
354 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
355 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
356 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
357 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
358 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
359 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
360 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
361
362 // Exceptions handling
363 if (!is64Bit)
364 SupportsExceptionHandling = true;
365 AbsoluteEHSectionOffsets = false;
366 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
367 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
368
369 // On Linux we must declare when we can use a non-executable stack.
370 if (X86TM->getSubtarget<X86Subtarget>().isLinux())
371 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
372}
373
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000374unsigned
375X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
376 bool Global) const {
377 CodeModel::Model CM = X86TM->getCodeModel();
378 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
379
380 if (X86TM->getRelocationModel() == Reloc::PIC_) {
381 unsigned Format = 0;
382
383 if (!is64Bit)
384 // 32 bit targets always encode pointers as 4 bytes
385 Format = DW_EH_PE_sdata4;
386 else {
387 // 64 bit targets encode pointers in 4 bytes iff:
388 // - code model is small OR
389 // - code model is medium and we're emitting externally visible symbols
390 // or any code symbols
391 if (CM == CodeModel::Small ||
392 (CM == CodeModel::Medium && (Global ||
393 Reason != DwarfEncoding::Data)))
394 Format = DW_EH_PE_sdata4;
395 else
396 Format = DW_EH_PE_sdata8;
397 }
398
399 if (Global)
400 Format |= DW_EH_PE_indirect;
401
402 return (Format | DW_EH_PE_pcrel);
403 } else {
404 if (is64Bit &&
405 (CM == CodeModel::Small ||
406 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
407 return DW_EH_PE_udata4;
408 else
409 return DW_EH_PE_absptr;
410 }
411}
412
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000413const Section*
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000414X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000415 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000416
417 if (const Function *F = dyn_cast<Function>(GV)) {
418 switch (F->getLinkage()) {
419 default: assert(0 && "Unknown linkage type!");
420 case Function::InternalLinkage:
421 case Function::DLLExportLinkage:
422 case Function::ExternalLinkage:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000423 return getTextSection_();
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000424 case Function::WeakLinkage:
425 case Function::LinkOnceLinkage:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000426 std::string Name = UniqueSectionForGlobal(GV, Kind);
427 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
428 return getNamedSection(Name.c_str(), Flags);
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000429 }
430 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000431 if (GVar->isWeakForLinker()) {
432 std::string Name = UniqueSectionForGlobal(GVar, Kind);
433 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
434 return getNamedSection(Name.c_str(), Flags);
435 } else {
436 switch (Kind) {
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000437 case SectionKind::Data:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000438 return getDataSection_();
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000439 case SectionKind::BSS:
440 // ELF targets usually have BSS sections
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000441 return getBSSSection_();
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000442 case SectionKind::ROData:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000443 return getReadOnlySection_();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000444 case SectionKind::RODataMergeStr:
445 return MergeableStringSection(GVar);
446 case SectionKind::RODataMergeConst:
447 return MergeableConstSection(GVar);
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000448 case SectionKind::ThreadData:
449 // ELF targets usually support TLS stuff
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000450 return getTLSDataSection_();
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000451 case SectionKind::ThreadBSS:
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000452 return getTLSBSSSection_();
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000453 default:
454 assert(0 && "Unsuported section kind for global");
455 }
456 }
457 } else
458 assert(0 && "Unsupported global");
459}
460
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000461const Section*
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000462X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000463 const TargetData *TD = X86TM->getTargetData();
464 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
465 const Type *Type = C->getType();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000466
467 // FIXME: string here is temporary, until stuff will fully land in.
Anton Korobeynikov2a889172008-07-09 13:25:46 +0000468 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
469 // currently directly used by asmprinter.
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000470 unsigned Size = TD->getABITypeSize(Type);
471 if (Size == 4 || Size == 8 || Size == 16) {
472 std::string Name = ".rodata.cst" + utostr(Size);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000473
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000474 return getNamedSection(Name.c_str(),
475 SectionFlags::setEntitySize(SectionFlags::Mergeable,
476 Size));
477 }
478
479 return getReadOnlySection_();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000480}
481
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000482const Section*
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000483X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000484 const TargetData *TD = X86TM->getTargetData();
485 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
486 const ConstantArray *CVA = cast<ConstantArray>(C);
487 const Type *Type = CVA->getType()->getElementType();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000488
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000489 unsigned Size = TD->getABITypeSize(Type);
490 if (Size <= 16) {
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000491 // We also need alignment here
492 const TargetData *TD = X86TM->getTargetData();
493 unsigned Align = TD->getPreferredAlignment(GV);
494 if (Align < Size)
495 Align = Size;
496
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000497 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
498 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
499 SectionFlags::Strings,
500 Size);
501 return getNamedSection(Name.c_str(), Flags);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000502 }
503
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000504 return getReadOnlySection_();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000505}
506
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000507std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
508 std::string Flags = ",\"";
509
510 if (!(flags & SectionFlags::Debug))
511 Flags += 'a';
512 if (flags & SectionFlags::Code)
513 Flags += 'x';
514 if (flags & SectionFlags::Writeable)
515 Flags += 'w';
516 if (flags & SectionFlags::Mergeable)
517 Flags += 'M';
518 if (flags & SectionFlags::Strings)
519 Flags += 'S';
520 if (flags & SectionFlags::TLS)
521 Flags += 'T';
522
523 Flags += "\"";
524
525 // FIXME: There can be exceptions here
526 if (flags & SectionFlags::BSS)
527 Flags += ",@nobits";
528 else
529 Flags += ",@progbits";
530
Anton Korobeynikov6d829422008-07-09 13:22:17 +0000531 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
532 Flags += "," + utostr(entitySize);
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000533
534 return Flags;
535}
536
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000537X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
538 X86TargetAsmInfo(TM) {
539 GlobalPrefix = "_";
540 LCOMMDirective = "\t.lcomm\t";
541 COMMDirectiveTakesAlignment = false;
542 HasDotTypeDotSizeDirective = false;
543 StaticCtorsSection = "\t.section .ctors,\"aw\"";
544 StaticDtorsSection = "\t.section .dtors,\"aw\"";
545 HiddenDirective = NULL;
546 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
547 WeakRefDirective = "\t.weak\t";
548 SetDirective = "\t.set\t";
549
550 // Set up DWARF directives
551 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
552 AbsoluteDebugSectionOffsets = true;
553 AbsoluteEHSectionOffsets = false;
554 SupportsDebugInformation = true;
555 DwarfSectionOffsetDirective = "\t.secrel32\t";
556 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
557 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
558 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
559 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
560 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
561 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
562 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
563 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
564 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
565 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
566 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
567}
568
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000569unsigned
570X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
571 bool Global) const {
572 CodeModel::Model CM = X86TM->getCodeModel();
573 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000574
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000575 if (X86TM->getRelocationModel() == Reloc::PIC_) {
576 unsigned Format = 0;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000577
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000578 if (!is64Bit)
579 // 32 bit targets always encode pointers as 4 bytes
580 Format = DW_EH_PE_sdata4;
581 else {
582 // 64 bit targets encode pointers in 4 bytes iff:
583 // - code model is small OR
584 // - code model is medium and we're emitting externally visible symbols
585 // or any code symbols
586 if (CM == CodeModel::Small ||
587 (CM == CodeModel::Medium && (Global ||
588 Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000589 Format = DW_EH_PE_sdata4;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000590 else
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000591 Format = DW_EH_PE_sdata8;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000592 }
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000593
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000594 if (Global)
595 Format |= DW_EH_PE_indirect;
596
597 return (Format | DW_EH_PE_pcrel);
598 } else {
599 if (is64Bit &&
600 (CM == CodeModel::Small ||
601 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
602 return DW_EH_PE_udata4;
603 else
604 return DW_EH_PE_absptr;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000605 }
606}
607
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000608std::string
609X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
610 SectionKind::Kind kind) const {
611 switch (kind) {
612 case SectionKind::Text:
613 return ".text$linkonce" + GV->getName();
614 case SectionKind::Data:
615 case SectionKind::BSS:
616 case SectionKind::ThreadData:
617 case SectionKind::ThreadBSS:
618 return ".data$linkonce" + GV->getName();
619 case SectionKind::ROData:
620 case SectionKind::RODataMergeConst:
621 case SectionKind::RODataMergeStr:
622 return ".rdata$linkonce" + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000623 default:
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000624 assert(0 && "Unknown section kind");
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000625 }
626}
627
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000628std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
629 std::string Flags = ",\"";
630
631 if (flags & SectionFlags::Code)
632 Flags += 'x';
633 if (flags & SectionFlags::Writeable)
634 Flags += 'w';
635
636 Flags += "\"";
637
638 return Flags;
639}
640
641X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
642 X86TargetAsmInfo(TM) {
643 GlobalPrefix = "_";
644 CommentString = ";";
645
646 PrivateGlobalPrefix = "$";
647 AlignDirective = "\talign\t";
648 ZeroDirective = "\tdb\t";
649 ZeroDirectiveSuffix = " dup(0)";
650 AsciiDirective = "\tdb\t";
651 AscizDirective = 0;
652 Data8bitsDirective = "\tdb\t";
653 Data16bitsDirective = "\tdw\t";
654 Data32bitsDirective = "\tdd\t";
655 Data64bitsDirective = "\tdq\t";
656 HasDotTypeDotSizeDirective = false;
657
658 TextSection = "_text";
659 DataSection = "_data";
660 JumpTableDataSection = NULL;
661 SwitchToSectionDirective = "";
662 TextSectionStartSuffix = "\tsegment 'CODE'";
663 DataSectionStartSuffix = "\tsegment 'DATA'";
664 SectionEndDirectiveSuffix = "\tends\n";
665}