blob: 0fdc0eefbae3bf86286661814c7478f9f5487f77 [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";
145 FourByteConstantSection = "\t.literal4\n";
146 EightByteConstantSection = "\t.literal8\n";
147 // FIXME: Why don't always use this section?
148 if (is64Bit)
149 SixteenByteConstantSection = "\t.literal16\n";
150 ReadOnlySection = "\t.const\n";
151 LCOMMDirective = "\t.lcomm\t";
152 SwitchToSectionDirective = "\t.section ";
153 StringConstantPrefix = "\1LC";
154 COMMDirectiveTakesAlignment = false;
155 HasDotTypeDotSizeDirective = false;
156 if (TM.getRelocationModel() == Reloc::Static) {
157 StaticCtorsSection = ".constructor";
158 StaticDtorsSection = ".destructor";
159 } else {
160 StaticCtorsSection = ".mod_init_func";
161 StaticDtorsSection = ".mod_term_func";
162 }
163 if (is64Bit) {
164 PersonalityPrefix = "";
165 PersonalitySuffix = "+4@GOTPCREL";
166 } else {
167 PersonalityPrefix = "L";
168 PersonalitySuffix = "$non_lazy_ptr";
169 }
170 NeedsIndirectEncoding = true;
171 InlineAsmStart = "## InlineAsm Start";
172 InlineAsmEnd = "## InlineAsm End";
173 CommentString = "##";
174 SetDirective = "\t.set";
175 PCSymbol = ".";
176 UsedDirective = "\t.no_dead_strip\t";
177 WeakDefDirective = "\t.weak_definition ";
178 WeakRefDirective = "\t.weak_reference ";
179 HiddenDirective = "\t.private_extern ";
180 ProtectedDirective = "\t.globl\t";
181
182 // In non-PIC modes, emit a special label before jump tables so that the
183 // linker can perform more accurate dead code stripping.
184 if (TM.getRelocationModel() != Reloc::PIC_) {
185 // Emit a local label that is preserved until the linker runs.
186 JumpTableSpecialLabelPrefix = "l";
187 }
188
189 SupportsDebugInformation = true;
190 NeedsSet = true;
191 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
192 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
193 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
194 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
195 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
196 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
197 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
198 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
199 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
200 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
201 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
202
203 // Exceptions handling
204 SupportsExceptionHandling = true;
205 GlobalEHDirective = "\t.globl\t";
206 SupportsWeakOmittedEHFrame = false;
207 AbsoluteEHSectionOffsets = false;
208 DwarfEHFrameSection =
209 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
210 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
211}
212
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000213unsigned
214X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
215 bool Global) const {
216 if (Reason == DwarfEncoding::Functions && Global)
217 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
218 else if (Reason == DwarfEncoding::CodeLabels || !Global)
219 return DW_EH_PE_pcrel;
220 else
221 return DW_EH_PE_absptr;
222}
223
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000224std::string
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000225X86DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
226 SectionKind::Kind Kind = SectionKindForGlobal(GV);
227 bool isWeak = GV->hasWeakLinkage() ||
228 GV->hasCommonLinkage() ||
229 GV->hasLinkOnceLinkage();
230
231 switch (Kind) {
232 case SectionKind::Text:
233 if (isWeak)
234 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
235 else
236 return getTextSection();
237 case SectionKind::Data:
238 case SectionKind::ThreadData:
239 case SectionKind::BSS:
240 case SectionKind::ThreadBSS:
241 if (cast<GlobalVariable>(GV)->isConstant()) {
242 if (isWeak)
243 return ".section __DATA,__const_coal,coalesced";
244 else
245 return ".const_data";
246 } else {
247 if (isWeak)
248 return ".section __DATA,__datacoal_nt,coalesced";
249 else
250 return getDataSection();
251 }
252 case SectionKind::ROData:
253 if (isWeak)
254 return ".section __DATA,__const_coal,coalesced";
255 else
256 return getReadOnlySection();
257 case SectionKind::RODataMergeStr:
258 return MergeableStringSection(cast<GlobalVariable>(GV));
259 case SectionKind::RODataMergeConst:
260 return MergeableConstSection(cast<GlobalVariable>(GV));
261 default:
262 assert(0 && "Unsuported section kind for global");
263 }
264
265 // FIXME: Do we have any extra special weird cases?
266}
267
268std::string
269X86DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
270 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
271 unsigned Size = SectionFlags::getEntitySize(Flags);
272
273 if (Size) {
274 const TargetData *TD = X86TM->getTargetData();
275 unsigned Align = TD->getPreferredAlignment(GV);
276 if (Align <= 32)
277 return getCStringSection();
278 }
279
280 return getReadOnlySection();
281}
282
283std::string
284X86DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
285 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
286 unsigned Size = SectionFlags::getEntitySize(Flags);
287
288 if (Size == 4)
289 return FourByteConstantSection;
290 else if (Size == 8)
291 return EightByteConstantSection;
292 else if (Size == 16 && X86TM->getSubtarget<X86Subtarget>().is64Bit())
293 return SixteenByteConstantSection;
294
295 return getReadOnlySection();
296}
297
298std::string
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000299X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
300 SectionKind::Kind kind) const {
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000301 assert(0 && "Darwin does not use unique sections");
302 return "";
303}
304
305unsigned
306X86DarwinTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
307 const char* name) const {
308 unsigned Flags =
309 TargetAsmInfo::SectionFlagsForGlobal(GV,
310 GV->getSection().c_str());
311
312 // If there was decision to put stuff into mergeable section - calculate
313 // entity size
314 if (Flags & SectionFlags::Mergeable) {
315 const TargetData *TD = X86TM->getTargetData();
316 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
317 const Type *Type;
318
319 if (Flags & SectionFlags::Strings) {
320 const ConstantArray *CVA = cast<ConstantArray>(C);
321 Type = CVA->getType()->getElementType();
322 } else
323 Type = C->getType();
324
325 unsigned Size = TD->getABITypeSize(Type);
Anton Korobeynikov7e7fc822008-07-09 13:26:52 +0000326 if (Size > 16 ||
327 !(Flags & SectionFlags::Strings ||
328 (Size == 4 || Size == 8 || Size == 16))) {
329 // Not suitable for mergeable
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000330 Size = 0;
Anton Korobeynikov87fc6b42008-07-09 13:26:05 +0000331 Flags &= ~SectionFlags::Mergeable;
332 }
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000333 Flags = SectionFlags::setEntitySize(Flags, Size);
334 }
335
336 return Flags;
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000337}
338
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000339X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
340 X86TargetAsmInfo(TM) {
341 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
342
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000343 ReadOnlySection = ".rodata";
Anton Korobeynikov2a889172008-07-09 13:25:46 +0000344 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
345 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
346 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000347 CStringSection = ".rodata.str";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000348 PrivateGlobalPrefix = ".L";
349 WeakRefDirective = "\t.weak\t";
350 SetDirective = "\t.set\t";
351 PCSymbol = ".";
352
353 // Set up DWARF directives
354 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
355
356 // Debug Information
357 AbsoluteDebugSectionOffsets = true;
358 SupportsDebugInformation = true;
359 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
360 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
361 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
362 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
363 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
364 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
365 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
366 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
367 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
368 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
369 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
370
371 // Exceptions handling
372 if (!is64Bit)
373 SupportsExceptionHandling = true;
374 AbsoluteEHSectionOffsets = false;
375 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
376 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
377
378 // On Linux we must declare when we can use a non-executable stack.
379 if (X86TM->getSubtarget<X86Subtarget>().isLinux())
380 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
381}
382
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000383unsigned
384X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
385 bool Global) const {
386 CodeModel::Model CM = X86TM->getCodeModel();
387 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
388
389 if (X86TM->getRelocationModel() == Reloc::PIC_) {
390 unsigned Format = 0;
391
392 if (!is64Bit)
393 // 32 bit targets always encode pointers as 4 bytes
394 Format = DW_EH_PE_sdata4;
395 else {
396 // 64 bit targets encode pointers in 4 bytes iff:
397 // - code model is small OR
398 // - code model is medium and we're emitting externally visible symbols
399 // or any code symbols
400 if (CM == CodeModel::Small ||
401 (CM == CodeModel::Medium && (Global ||
402 Reason != DwarfEncoding::Data)))
403 Format = DW_EH_PE_sdata4;
404 else
405 Format = DW_EH_PE_sdata8;
406 }
407
408 if (Global)
409 Format |= DW_EH_PE_indirect;
410
411 return (Format | DW_EH_PE_pcrel);
412 } else {
413 if (is64Bit &&
414 (CM == CodeModel::Small ||
415 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
416 return DW_EH_PE_udata4;
417 else
418 return DW_EH_PE_absptr;
419 }
420}
421
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000422std::string
423X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
424 SectionKind::Kind kind = SectionKindForGlobal(GV);
425
426 if (const Function *F = dyn_cast<Function>(GV)) {
427 switch (F->getLinkage()) {
428 default: assert(0 && "Unknown linkage type!");
429 case Function::InternalLinkage:
430 case Function::DLLExportLinkage:
431 case Function::ExternalLinkage:
432 return getTextSection();
433 case Function::WeakLinkage:
434 case Function::LinkOnceLinkage:
435 return UniqueSectionForGlobal(F, kind);
436 }
437 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
438 if (GVar->hasCommonLinkage() ||
439 GVar->hasLinkOnceLinkage() ||
440 GVar->hasWeakLinkage())
441 return UniqueSectionForGlobal(GVar, kind);
442 else {
443 switch (kind) {
444 case SectionKind::Data:
445 return getDataSection();
446 case SectionKind::BSS:
447 // ELF targets usually have BSS sections
448 return getBSSSection();
449 case SectionKind::ROData:
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000450 return getReadOnlySection();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000451 case SectionKind::RODataMergeStr:
452 return MergeableStringSection(GVar);
453 case SectionKind::RODataMergeConst:
454 return MergeableConstSection(GVar);
Anton Korobeynikov4260ad32008-07-09 13:23:08 +0000455 case SectionKind::ThreadData:
456 // ELF targets usually support TLS stuff
457 return getTLSDataSection();
458 case SectionKind::ThreadBSS:
459 return getTLSBSSSection();
460 default:
461 assert(0 && "Unsuported section kind for global");
462 }
463 }
464 } else
465 assert(0 && "Unsupported global");
466}
467
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000468std::string
469X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
470 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
471 unsigned Size = SectionFlags::getEntitySize(Flags);
472
473 // FIXME: string here is temporary, until stuff will fully land in.
Anton Korobeynikov2a889172008-07-09 13:25:46 +0000474 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
475 // currently directly used by asmprinter.
476 if (Size == 4 || Size == 8 || Size == 16)
477 return ".rodata.cst" + utostr(Size);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000478
479 return getReadOnlySection();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000480}
481
482std::string
483X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
484 unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
485 unsigned Size = SectionFlags::getEntitySize(Flags);
486
487 if (Size) {
488 // We also need alignment here
489 const TargetData *TD = X86TM->getTargetData();
490 unsigned Align = TD->getPreferredAlignment(GV);
491 if (Align < Size)
492 Align = Size;
493
Anton Korobeynikov1290ecf2008-07-09 13:26:24 +0000494 return getCStringSection() + utostr(Size) + '.' + utostr(Align);
Anton Korobeynikov4039e682008-07-09 13:23:57 +0000495 }
496
497 return getReadOnlySection();
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000498}
499
500unsigned
501X86ELFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
502 const char* name) const {
503 unsigned Flags =
504 TargetAsmInfo::SectionFlagsForGlobal(GV,
505 GV->getSection().c_str());
506
507 // If there was decision to put stuff into mergeable section - calculate
508 // entity size
509 if (Flags & SectionFlags::Mergeable) {
510 const TargetData *TD = X86TM->getTargetData();
511 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
512 const Type *Type;
513
514 if (Flags & SectionFlags::Strings) {
515 const ConstantArray *CVA = cast<ConstantArray>(C);
516 Type = CVA->getType()->getElementType();
517 } else
518 Type = C->getType();
519
520 unsigned Size = TD->getABITypeSize(Type);
Anton Korobeynikov7e7fc822008-07-09 13:26:52 +0000521 // FIXME: size check here ugly, and will hopefully have gone, when we will
522 // have sane interface for attaching flags to sections.
523 if (Size > 16 ||
524 !(Flags & SectionFlags::Strings ||
525 (Size == 4 || Size == 8 || Size == 16))) {
526 // Not suitable for mergeable
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000527 Size = 0;
Anton Korobeynikov87fc6b42008-07-09 13:26:05 +0000528 Flags &= ~SectionFlags::Mergeable;
529 }
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000530 Flags = SectionFlags::setEntitySize(Flags, Size);
531 }
532
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000533 // FIXME: This is hacky and will be removed when switching from std::string
534 // sections into 'general' ones
535
536 // Mark section as named, when needed (so, we we will need .section directive
537 // to switch into it).
Anton Korobeynikov7e7fc822008-07-09 13:26:52 +0000538 unsigned TypeFlags = Flags & SectionFlags::TypeFlags;
539 if (!TypeFlags /* Read-only section */ ||
540 (TypeFlags & (SectionFlags::Mergeable |
541 SectionFlags::TLS |
542 SectionFlags::Linkonce)))
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000543 Flags |= SectionFlags::Named;
544
Anton Korobeynikov01b0e242008-07-09 13:23:37 +0000545 return Flags;
546}
547
548
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000549std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
550 std::string Flags = ",\"";
551
552 if (!(flags & SectionFlags::Debug))
553 Flags += 'a';
554 if (flags & SectionFlags::Code)
555 Flags += 'x';
556 if (flags & SectionFlags::Writeable)
557 Flags += 'w';
558 if (flags & SectionFlags::Mergeable)
559 Flags += 'M';
560 if (flags & SectionFlags::Strings)
561 Flags += 'S';
562 if (flags & SectionFlags::TLS)
563 Flags += 'T';
564
565 Flags += "\"";
566
567 // FIXME: There can be exceptions here
568 if (flags & SectionFlags::BSS)
569 Flags += ",@nobits";
570 else
571 Flags += ",@progbits";
572
Anton Korobeynikov6d829422008-07-09 13:22:17 +0000573 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
574 Flags += "," + utostr(entitySize);
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000575
576 return Flags;
577}
578
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000579X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
580 X86TargetAsmInfo(TM) {
581 GlobalPrefix = "_";
582 LCOMMDirective = "\t.lcomm\t";
583 COMMDirectiveTakesAlignment = false;
584 HasDotTypeDotSizeDirective = false;
585 StaticCtorsSection = "\t.section .ctors,\"aw\"";
586 StaticDtorsSection = "\t.section .dtors,\"aw\"";
587 HiddenDirective = NULL;
588 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
589 WeakRefDirective = "\t.weak\t";
590 SetDirective = "\t.set\t";
591
592 // Set up DWARF directives
593 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
594 AbsoluteDebugSectionOffsets = true;
595 AbsoluteEHSectionOffsets = false;
596 SupportsDebugInformation = true;
597 DwarfSectionOffsetDirective = "\t.secrel32\t";
598 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
599 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
600 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
601 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
602 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
603 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
604 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
605 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
606 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
607 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
608 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
609}
610
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000611unsigned
612X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
613 bool Global) const {
614 CodeModel::Model CM = X86TM->getCodeModel();
615 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000616
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000617 if (X86TM->getRelocationModel() == Reloc::PIC_) {
618 unsigned Format = 0;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000619
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000620 if (!is64Bit)
621 // 32 bit targets always encode pointers as 4 bytes
622 Format = DW_EH_PE_sdata4;
623 else {
624 // 64 bit targets encode pointers in 4 bytes iff:
625 // - code model is small OR
626 // - code model is medium and we're emitting externally visible symbols
627 // or any code symbols
628 if (CM == CodeModel::Small ||
629 (CM == CodeModel::Medium && (Global ||
630 Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000631 Format = DW_EH_PE_sdata4;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000632 else
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000633 Format = DW_EH_PE_sdata8;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000634 }
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000635
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000636 if (Global)
637 Format |= DW_EH_PE_indirect;
638
639 return (Format | DW_EH_PE_pcrel);
640 } else {
641 if (is64Bit &&
642 (CM == CodeModel::Small ||
643 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
644 return DW_EH_PE_udata4;
645 else
646 return DW_EH_PE_absptr;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000647 }
648}
649
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000650std::string
651X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
652 SectionKind::Kind kind) const {
653 switch (kind) {
654 case SectionKind::Text:
655 return ".text$linkonce" + GV->getName();
656 case SectionKind::Data:
657 case SectionKind::BSS:
658 case SectionKind::ThreadData:
659 case SectionKind::ThreadBSS:
660 return ".data$linkonce" + GV->getName();
661 case SectionKind::ROData:
662 case SectionKind::RODataMergeConst:
663 case SectionKind::RODataMergeStr:
664 return ".rdata$linkonce" + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000665 default:
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000666 assert(0 && "Unknown section kind");
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000667 }
668}
669
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000670unsigned
671X86COFFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
672 const char* name) const {
673 unsigned Flags =
674 TargetAsmInfo::SectionFlagsForGlobal(GV,
675 GV->getSection().c_str());
676
677 // Mark section as named, when needed (so, we we will need .section directive
678 // to switch into it).
Anton Korobeynikov7e7fc822008-07-09 13:26:52 +0000679 unsigned TypeFlags = Flags & SectionFlags::TypeFlags;
680 if (!TypeFlags /* Read-only section */ ||
681 (TypeFlags & (SectionFlags::Mergeable |
682 SectionFlags::TLS |
683 SectionFlags::Linkonce)))
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000684 Flags |= SectionFlags::Named;
685
686 return Flags;
687}
688
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000689std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
690 std::string Flags = ",\"";
691
692 if (flags & SectionFlags::Code)
693 Flags += 'x';
694 if (flags & SectionFlags::Writeable)
695 Flags += 'w';
696
697 Flags += "\"";
698
699 return Flags;
700}
701
702X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
703 X86TargetAsmInfo(TM) {
704 GlobalPrefix = "_";
705 CommentString = ";";
706
707 PrivateGlobalPrefix = "$";
708 AlignDirective = "\talign\t";
709 ZeroDirective = "\tdb\t";
710 ZeroDirectiveSuffix = " dup(0)";
711 AsciiDirective = "\tdb\t";
712 AscizDirective = 0;
713 Data8bitsDirective = "\tdb\t";
714 Data16bitsDirective = "\tdw\t";
715 Data32bitsDirective = "\tdd\t";
716 Data64bitsDirective = "\tdq\t";
717 HasDotTypeDotSizeDirective = false;
718
719 TextSection = "_text";
720 DataSection = "_data";
721 JumpTableDataSection = NULL;
722 SwitchToSectionDirective = "";
723 TextSectionStartSuffix = "\tsegment 'CODE'";
724 DataSectionStartSuffix = "\tsegment 'DATA'";
725 SectionEndDirectiveSuffix = "\tends\n";
726}