Typo
[oota-llvm.git] / lib / Target / X86 / X86TargetAsmInfo.cpp
1 //===-- X86TargetAsmInfo.cpp - X86 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 contains the declarations of the X86TargetAsmInfo properties.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86Subtarget.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Module.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Support/Dwarf.h"
24
25 using namespace llvm;
26 using namespace llvm::dwarf;
27
28 static const char *const x86_asm_table[] = {
29                                       "{si}", "S",
30                                       "{di}", "D",
31                                       "{ax}", "a",
32                                       "{cx}", "c",
33                                       "{memory}", "memory",
34                                       "{flags}", "",
35                                       "{dirflag}", "",
36                                       "{fpsr}", "",
37                                       "{cc}", "cc",
38                                       0,0};
39
40 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
41   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
42   X86TM = &TM;
43
44   AsmTransCBE = x86_asm_table;
45
46   AssemblerDialect = Subtarget->getAsmFlavor();
47 }
48
49 bool 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.
54
55   // Verify this is a simple bswap.
56   if (CI->getNumOperands() != 2 ||
57       CI->getType() != CI->getOperand(1)->getType() ||
58       !CI->getType()->isInteger())
59     return false;
60
61   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
62   if (!Ty || Ty->getBitWidth() % 16 != 0)
63     return false;
64
65   // Okay, we can do this xform, do so now.
66   const Type *Tys[] = { Ty };
67   Module *M = CI->getParent()->getParent()->getParent();
68   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
69
70   Value *Op = CI->getOperand(1);
71   Op = CallInst::Create(Int, Op, CI->getName(), CI);
72
73   CI->replaceAllUsesWith(Op);
74   CI->eraseFromParent();
75   return true;
76 }
77
78
79 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
80   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
81   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
82
83   std::string AsmStr = IA->getAsmString();
84
85   // 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?
88
89   switch (AsmPieces.size()) {
90   default: return false;
91   case 1:
92     AsmStr = AsmPieces[0];
93     AsmPieces.clear();
94     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
95
96     // bswap $0
97     if (AsmPieces.size() == 2 &&
98         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;
104   case 3:
105     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
106         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;
125   }
126   return false;
127 }
128
129 X86DarwinTargetAsmInfo::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
213 unsigned
214 X86DarwinTargetAsmInfo::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
224 std::string
225 X86DarwinTargetAsmInfo::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
268 std::string
269 X86DarwinTargetAsmInfo::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
283 std::string
284 X86DarwinTargetAsmInfo::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
298 std::string
299 X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
300                                                SectionKind::Kind kind) const {
301   assert(0 && "Darwin does not use unique sections");
302   return "";
303 }
304
305 unsigned
306 X86DarwinTargetAsmInfo::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);
326     if (Size > 16) {
327       // Too big for mergeable
328       Size = 0;
329       Flags &= ~SectionFlags::Mergeable;
330     }
331     Flags = SectionFlags::setEntitySize(Flags, Size);
332   }
333
334   return Flags;
335 }
336
337 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
338   X86TargetAsmInfo(TM) {
339   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
340
341   ReadOnlySection = ".rodata";
342   FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
343   EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
344   SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
345   CStringSection = ".rodata.str";
346   PrivateGlobalPrefix = ".L";
347   WeakRefDirective = "\t.weak\t";
348   SetDirective = "\t.set\t";
349   PCSymbol = ".";
350
351   // Set up DWARF directives
352   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
353
354   // Debug Information
355   AbsoluteDebugSectionOffsets = true;
356   SupportsDebugInformation = true;
357   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
358   DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
359   DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
360   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
361   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
362   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
363   DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
364   DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
365   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
366   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
367   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
368
369   // Exceptions handling
370   if (!is64Bit)
371     SupportsExceptionHandling = true;
372   AbsoluteEHSectionOffsets = false;
373   DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
374   DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
375
376   // On Linux we must declare when we can use a non-executable stack.
377   if (X86TM->getSubtarget<X86Subtarget>().isLinux())
378     NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
379 }
380
381 unsigned
382 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
383                                            bool Global) const {
384   CodeModel::Model CM = X86TM->getCodeModel();
385   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
386
387   if (X86TM->getRelocationModel() == Reloc::PIC_) {
388     unsigned Format = 0;
389
390     if (!is64Bit)
391       // 32 bit targets always encode pointers as 4 bytes
392       Format = DW_EH_PE_sdata4;
393     else {
394       // 64 bit targets encode pointers in 4 bytes iff:
395       // - code model is small OR
396       // - code model is medium and we're emitting externally visible symbols
397       //   or any code symbols
398       if (CM == CodeModel::Small ||
399           (CM == CodeModel::Medium && (Global ||
400                                        Reason != DwarfEncoding::Data)))
401         Format = DW_EH_PE_sdata4;
402       else
403         Format = DW_EH_PE_sdata8;
404     }
405
406     if (Global)
407       Format |= DW_EH_PE_indirect;
408
409     return (Format | DW_EH_PE_pcrel);
410   } else {
411     if (is64Bit &&
412         (CM == CodeModel::Small ||
413          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
414       return DW_EH_PE_udata4;
415     else
416       return DW_EH_PE_absptr;
417   }
418 }
419
420 std::string
421 X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
422   SectionKind::Kind kind = SectionKindForGlobal(GV);
423
424   if (const Function *F = dyn_cast<Function>(GV)) {
425     switch (F->getLinkage()) {
426      default: assert(0 && "Unknown linkage type!");
427      case Function::InternalLinkage:
428      case Function::DLLExportLinkage:
429      case Function::ExternalLinkage:
430       return getTextSection();
431      case Function::WeakLinkage:
432      case Function::LinkOnceLinkage:
433       return UniqueSectionForGlobal(F, kind);
434     }
435   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
436     if (GVar->hasCommonLinkage() ||
437         GVar->hasLinkOnceLinkage() ||
438         GVar->hasWeakLinkage())
439       return UniqueSectionForGlobal(GVar, kind);
440     else {
441       switch (kind) {
442        case SectionKind::Data:
443         return getDataSection();
444        case SectionKind::BSS:
445         // ELF targets usually have BSS sections
446         return getBSSSection();
447        case SectionKind::ROData:
448         return getReadOnlySection();
449        case SectionKind::RODataMergeStr:
450         return MergeableStringSection(GVar);
451        case SectionKind::RODataMergeConst:
452         return MergeableConstSection(GVar);
453        case SectionKind::ThreadData:
454         // ELF targets usually support TLS stuff
455         return getTLSDataSection();
456        case SectionKind::ThreadBSS:
457         return getTLSBSSSection();
458        default:
459         assert(0 && "Unsuported section kind for global");
460       }
461     }
462   } else
463     assert(0 && "Unsupported global");
464 }
465
466 std::string
467 X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
468   unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
469   unsigned Size = SectionFlags::getEntitySize(Flags);
470
471   // FIXME: string here is temporary, until stuff will fully land in.
472   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
473   // currently directly used by asmprinter.
474   if (Size == 4 || Size == 8 || Size == 16)
475     return ".rodata.cst" + utostr(Size);
476
477   return getReadOnlySection();
478 }
479
480 std::string
481 X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
482   unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
483   unsigned Size = SectionFlags::getEntitySize(Flags);
484
485   if (Size) {
486     // We also need alignment here
487     const TargetData *TD = X86TM->getTargetData();
488     unsigned Align = TD->getPreferredAlignment(GV);
489     if (Align < Size)
490       Align = Size;
491
492     return getCStringSection() + utostr(Size) + '.' + utostr(Align);
493   }
494
495   return getReadOnlySection();
496 }
497
498 unsigned
499 X86ELFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
500                                            const char* name) const {
501   unsigned Flags =
502     TargetAsmInfo::SectionFlagsForGlobal(GV,
503                                          GV->getSection().c_str());
504
505   // If there was decision to put stuff into mergeable section - calculate
506   // entity size
507   if (Flags & SectionFlags::Mergeable) {
508     const TargetData *TD = X86TM->getTargetData();
509     Constant *C = cast<GlobalVariable>(GV)->getInitializer();
510     const Type *Type;
511
512     if (Flags & SectionFlags::Strings) {
513       const ConstantArray *CVA = cast<ConstantArray>(C);
514       Type = CVA->getType()->getElementType();
515     } else
516       Type = C->getType();
517
518     unsigned Size = TD->getABITypeSize(Type);
519     if (Size > 16) {
520       // Too big for mergeable
521       Size = 0;
522       Flags &= ~SectionFlags::Mergeable;
523     }
524     Flags = SectionFlags::setEntitySize(Flags, Size);
525   }
526
527   // FIXME: This is hacky and will be removed when switching from std::string
528   // sections into 'general' ones
529
530   // Mark section as named, when needed (so, we we will need .section directive
531   // to switch into it).
532   if (Flags & (SectionFlags::Mergeable |
533                SectionFlags::TLS |
534                SectionFlags::Linkonce))
535     Flags |= SectionFlags::Named;
536
537   return Flags;
538 }
539
540
541 std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
542   std::string Flags = ",\"";
543
544   if (!(flags & SectionFlags::Debug))
545     Flags += 'a';
546   if (flags & SectionFlags::Code)
547     Flags += 'x';
548   if (flags & SectionFlags::Writeable)
549     Flags += 'w';
550   if (flags & SectionFlags::Mergeable)
551     Flags += 'M';
552   if (flags & SectionFlags::Strings)
553     Flags += 'S';
554   if (flags & SectionFlags::TLS)
555     Flags += 'T';
556
557   Flags += "\"";
558
559   // FIXME: There can be exceptions here
560   if (flags & SectionFlags::BSS)
561     Flags += ",@nobits";
562   else
563     Flags += ",@progbits";
564
565   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
566     Flags += "," + utostr(entitySize);
567
568   return Flags;
569 }
570
571 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
572   X86TargetAsmInfo(TM) {
573   GlobalPrefix = "_";
574   LCOMMDirective = "\t.lcomm\t";
575   COMMDirectiveTakesAlignment = false;
576   HasDotTypeDotSizeDirective = false;
577   StaticCtorsSection = "\t.section .ctors,\"aw\"";
578   StaticDtorsSection = "\t.section .dtors,\"aw\"";
579   HiddenDirective = NULL;
580   PrivateGlobalPrefix = "L";  // Prefix for private global symbols
581   WeakRefDirective = "\t.weak\t";
582   SetDirective = "\t.set\t";
583
584   // Set up DWARF directives
585   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
586   AbsoluteDebugSectionOffsets = true;
587   AbsoluteEHSectionOffsets = false;
588   SupportsDebugInformation = true;
589   DwarfSectionOffsetDirective = "\t.secrel32\t";
590   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
591   DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
592   DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
593   DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
594   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
595   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
596   DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
597   DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
598   DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
599   DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
600   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
601 }
602
603 unsigned
604 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
605                                             bool Global) const {
606   CodeModel::Model CM = X86TM->getCodeModel();
607   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
608
609   if (X86TM->getRelocationModel() == Reloc::PIC_) {
610     unsigned Format = 0;
611
612     if (!is64Bit)
613       // 32 bit targets always encode pointers as 4 bytes
614       Format = DW_EH_PE_sdata4;
615     else {
616       // 64 bit targets encode pointers in 4 bytes iff:
617       // - code model is small OR
618       // - code model is medium and we're emitting externally visible symbols
619       //   or any code symbols
620       if (CM == CodeModel::Small ||
621           (CM == CodeModel::Medium && (Global ||
622                                        Reason != DwarfEncoding::Data)))
623         Format = DW_EH_PE_sdata4;
624       else
625         Format = DW_EH_PE_sdata8;
626     }
627
628     if (Global)
629       Format |= DW_EH_PE_indirect;
630
631     return (Format | DW_EH_PE_pcrel);
632   } else {
633     if (is64Bit &&
634         (CM == CodeModel::Small ||
635          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
636       return DW_EH_PE_udata4;
637     else
638       return DW_EH_PE_absptr;
639   }
640 }
641
642 std::string
643 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
644                                              SectionKind::Kind kind) const {
645   switch (kind) {
646    case SectionKind::Text:
647     return ".text$linkonce" + GV->getName();
648    case SectionKind::Data:
649    case SectionKind::BSS:
650    case SectionKind::ThreadData:
651    case SectionKind::ThreadBSS:
652     return ".data$linkonce" + GV->getName();
653    case SectionKind::ROData:
654    case SectionKind::RODataMergeConst:
655    case SectionKind::RODataMergeStr:
656     return ".rdata$linkonce" + GV->getName();
657    default:
658     assert(0 && "Unknown section kind");
659   }
660 }
661
662 unsigned
663 X86COFFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
664                                             const char* name) const {
665   unsigned Flags =
666     TargetAsmInfo::SectionFlagsForGlobal(GV,
667                                          GV->getSection().c_str());
668
669   // Mark section as named, when needed (so, we we will need .section directive
670   // to switch into it).
671   if (Flags & (SectionFlags::Mergeable ||
672                SectionFlags::TLS ||
673                SectionFlags::Linkonce))
674     Flags |= SectionFlags::Named;
675
676   return Flags;
677 }
678
679 std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
680   std::string Flags = ",\"";
681
682   if (flags & SectionFlags::Code)
683     Flags += 'x';
684   if (flags & SectionFlags::Writeable)
685     Flags += 'w';
686
687   Flags += "\"";
688
689   return Flags;
690 }
691
692 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
693   X86TargetAsmInfo(TM) {
694   GlobalPrefix = "_";
695   CommentString = ";";
696
697   PrivateGlobalPrefix = "$";
698   AlignDirective = "\talign\t";
699   ZeroDirective = "\tdb\t";
700   ZeroDirectiveSuffix = " dup(0)";
701   AsciiDirective = "\tdb\t";
702   AscizDirective = 0;
703   Data8bitsDirective = "\tdb\t";
704   Data16bitsDirective = "\tdw\t";
705   Data32bitsDirective = "\tdd\t";
706   Data64bitsDirective = "\tdq\t";
707   HasDotTypeDotSizeDirective = false;
708
709   TextSection = "_text";
710   DataSection = "_data";
711   JumpTableDataSection = NULL;
712   SwitchToSectionDirective = "";
713   TextSectionStartSuffix = "\tsegment 'CODE'";
714   DataSectionStartSuffix = "\tsegment 'DATA'";
715   SectionEndDirectiveSuffix = "\tends\n";
716 }