Add FIXME for future checking.
[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   // FIXME: It seems, this should be .const_data if relocation model is not
143   // static.
144   ConstantPoolSection = "\t.const\n";
145   JumpTableDataSection = "\t.const\n";
146   CStringSection = "\t.cstring";
147   CStringSection_ = getUnnamedSection("\t.cstring",
148                                SectionFlags::Mergeable | SectionFlags::Strings);
149   FourByteConstantSection = "\t.literal4\n";
150   FourByteConstantSection_ = getUnnamedSection("\t.literal4\n",
151                                                SectionFlags::Mergeable);
152   EightByteConstantSection = "\t.literal8\n";
153   EightByteConstantSection_ = getUnnamedSection("\t.literal8\n",
154                                                 SectionFlags::Mergeable);
155   // FIXME: Why don't always use this section?
156   if (is64Bit) {
157     SixteenByteConstantSection = "\t.literal16\n";
158     SixteenByteConstantSection_ = getUnnamedSection("\t.literal16\n",
159                                                     SectionFlags::Mergeable);
160   }
161   ReadOnlySection = "\t.const\n";
162   ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
163   // FIXME: These should be named sections, really.
164   TextCoalSection =
165   getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
166                     SectionFlags::Code);
167   ConstDataCoalSection =
168     getUnnamedSection(".section __DATA,__const_coal,coalesced",
169                       SectionFlags::None);
170   ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
171   DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
172                                       SectionFlags::Writeable);
173
174   LCOMMDirective = "\t.lcomm\t";
175   SwitchToSectionDirective = "\t.section ";
176   StringConstantPrefix = "\1LC";
177   COMMDirectiveTakesAlignment = false;
178   HasDotTypeDotSizeDirective = false;
179   if (TM.getRelocationModel() == Reloc::Static) {
180     StaticCtorsSection = ".constructor";
181     StaticDtorsSection = ".destructor";
182   } else {
183     StaticCtorsSection = ".mod_init_func";
184     StaticDtorsSection = ".mod_term_func";
185   }
186   if (is64Bit) {
187     PersonalityPrefix = "";
188     PersonalitySuffix = "+4@GOTPCREL";
189   } else {
190     PersonalityPrefix = "L";
191     PersonalitySuffix = "$non_lazy_ptr";
192   }
193   NeedsIndirectEncoding = true;
194   InlineAsmStart = "## InlineAsm Start";
195   InlineAsmEnd = "## InlineAsm End";
196   CommentString = "##";
197   SetDirective = "\t.set";
198   PCSymbol = ".";
199   UsedDirective = "\t.no_dead_strip\t";
200   WeakDefDirective = "\t.weak_definition ";
201   WeakRefDirective = "\t.weak_reference ";
202   HiddenDirective = "\t.private_extern ";
203   ProtectedDirective = "\t.globl\t";
204
205   // In non-PIC modes, emit a special label before jump tables so that the
206   // linker can perform more accurate dead code stripping.
207   if (TM.getRelocationModel() != Reloc::PIC_) {
208     // Emit a local label that is preserved until the linker runs.
209     JumpTableSpecialLabelPrefix = "l";
210   }
211
212   SupportsDebugInformation = true;
213   NeedsSet = true;
214   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
215   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
216   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
217   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
218   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
219   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
220   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
221   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
222   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
223   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
224   DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
225
226   // Exceptions handling
227   SupportsExceptionHandling = true;
228   GlobalEHDirective = "\t.globl\t";
229   SupportsWeakOmittedEHFrame = false;
230   AbsoluteEHSectionOffsets = false;
231   DwarfEHFrameSection =
232   ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
233   DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
234 }
235
236 unsigned
237 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
238                                               bool Global) const {
239   if (Reason == DwarfEncoding::Functions && Global)
240     return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
241   else if (Reason == DwarfEncoding::CodeLabels || !Global)
242     return DW_EH_PE_pcrel;
243   else
244     return DW_EH_PE_absptr;
245 }
246
247 const Section*
248 X86DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
249   SectionKind::Kind Kind = SectionKindForGlobal(GV);
250   bool isWeak = GV->isWeakForLinker();
251   bool isNonStatic = (X86TM->getRelocationModel() != Reloc::Static);
252
253   switch (Kind) {
254    case SectionKind::Text:
255     if (isWeak)
256       return TextCoalSection;
257     else
258       return getTextSection_();
259    case SectionKind::Data:
260    case SectionKind::ThreadData:
261    case SectionKind::BSS:
262    case SectionKind::ThreadBSS:
263     if (cast<GlobalVariable>(GV)->isConstant())
264       return (isWeak ? ConstDataCoalSection : ConstDataSection);
265     else
266       return (isWeak ? DataCoalSection : getDataSection_());
267    case SectionKind::ROData:
268     return (isWeak ? ConstDataCoalSection :
269             (isNonStatic ? ConstDataSection : getReadOnlySection_()));
270    case SectionKind::RODataMergeStr:
271     return (isWeak ?
272             ConstDataCoalSection :
273             MergeableStringSection(cast<GlobalVariable>(GV)));
274    case SectionKind::RODataMergeConst:
275     return (isWeak ?
276             ConstDataCoalSection:
277             MergeableConstSection(cast<GlobalVariable>(GV)));
278    default:
279     assert(0 && "Unsuported section kind for global");
280   }
281
282   // FIXME: Do we have any extra special weird cases?
283 }
284
285 const Section*
286 X86DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
287   const TargetData *TD = X86TM->getTargetData();
288   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
289   const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
290
291   unsigned Size = TD->getABITypeSize(Type);
292   if (Size) {
293     const TargetData *TD = X86TM->getTargetData();
294     unsigned Align = TD->getPreferredAlignment(GV);
295     if (Align <= 32)
296       return getCStringSection_();
297   }
298
299   return getReadOnlySection_();
300 }
301
302 const Section*
303 X86DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
304   const TargetData *TD = X86TM->getTargetData();
305   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
306
307   unsigned Size = TD->getABITypeSize(C->getType());
308   if (Size == 4)
309     return FourByteConstantSection_;
310   else if (Size == 8)
311     return EightByteConstantSection_;
312   else if (Size == 16 && X86TM->getSubtarget<X86Subtarget>().is64Bit())
313     return SixteenByteConstantSection_;
314
315   return getReadOnlySection_();
316 }
317
318 std::string
319 X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
320                                                SectionKind::Kind kind) const {
321   assert(0 && "Darwin does not use unique sections");
322   return "";
323 }
324
325 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
326   X86TargetAsmInfo(TM) {
327   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
328
329   TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code);
330   DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
331   BSSSection_  = getUnnamedSection("\t.bss",
332                                    SectionFlags::Writeable | SectionFlags::BSS);
333   ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None);
334   TLSDataSection_ = getNamedSection("\t.tdata",
335                                     SectionFlags::Writeable | SectionFlags::TLS);
336   TLSBSSSection_ = getNamedSection("\t.tbss",
337                 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
338
339   ReadOnlySection = ".rodata";
340   FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
341   EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
342   SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
343   CStringSection = ".rodata.str";
344   PrivateGlobalPrefix = ".L";
345   WeakRefDirective = "\t.weak\t";
346   SetDirective = "\t.set\t";
347   PCSymbol = ".";
348
349   // Set up DWARF directives
350   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
351
352   // Debug Information
353   AbsoluteDebugSectionOffsets = true;
354   SupportsDebugInformation = true;
355   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
356   DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
357   DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
358   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
359   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
360   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
361   DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
362   DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
363   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
364   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
365   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
366
367   // Exceptions handling
368   if (!is64Bit)
369     SupportsExceptionHandling = true;
370   AbsoluteEHSectionOffsets = false;
371   DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
372   DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
373
374   // On Linux we must declare when we can use a non-executable stack.
375   if (X86TM->getSubtarget<X86Subtarget>().isLinux())
376     NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
377 }
378
379 unsigned
380 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
381                                            bool Global) const {
382   CodeModel::Model CM = X86TM->getCodeModel();
383   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
384
385   if (X86TM->getRelocationModel() == Reloc::PIC_) {
386     unsigned Format = 0;
387
388     if (!is64Bit)
389       // 32 bit targets always encode pointers as 4 bytes
390       Format = DW_EH_PE_sdata4;
391     else {
392       // 64 bit targets encode pointers in 4 bytes iff:
393       // - code model is small OR
394       // - code model is medium and we're emitting externally visible symbols
395       //   or any code symbols
396       if (CM == CodeModel::Small ||
397           (CM == CodeModel::Medium && (Global ||
398                                        Reason != DwarfEncoding::Data)))
399         Format = DW_EH_PE_sdata4;
400       else
401         Format = DW_EH_PE_sdata8;
402     }
403
404     if (Global)
405       Format |= DW_EH_PE_indirect;
406
407     return (Format | DW_EH_PE_pcrel);
408   } else {
409     if (is64Bit &&
410         (CM == CodeModel::Small ||
411          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
412       return DW_EH_PE_udata4;
413     else
414       return DW_EH_PE_absptr;
415   }
416 }
417
418 const Section*
419 X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
420   SectionKind::Kind Kind = SectionKindForGlobal(GV);
421
422   if (const Function *F = dyn_cast<Function>(GV)) {
423     switch (F->getLinkage()) {
424      default: assert(0 && "Unknown linkage type!");
425      case Function::InternalLinkage:
426      case Function::DLLExportLinkage:
427      case Function::ExternalLinkage:
428       return getTextSection_();
429      case Function::WeakLinkage:
430      case Function::LinkOnceLinkage:
431       std::string Name = UniqueSectionForGlobal(GV, Kind);
432       unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
433       return getNamedSection(Name.c_str(), Flags);
434     }
435   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
436     if (GVar->isWeakForLinker()) {
437       std::string Name = UniqueSectionForGlobal(GVar, Kind);
438       unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
439       return getNamedSection(Name.c_str(), Flags);
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 const Section*
467 X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
468   const TargetData *TD = X86TM->getTargetData();
469   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
470   const Type *Type = C->getType();
471
472   // FIXME: string here is temporary, until stuff will fully land in.
473   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
474   // currently directly used by asmprinter.
475   unsigned Size = TD->getABITypeSize(Type);
476   if (Size == 4 || Size == 8 || Size == 16) {
477     std::string Name =  ".rodata.cst" + utostr(Size);
478
479     return getNamedSection(Name.c_str(),
480                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
481                                                        Size));
482   }
483
484   return getReadOnlySection_();
485 }
486
487 const Section*
488 X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
489   const TargetData *TD = X86TM->getTargetData();
490   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
491   const ConstantArray *CVA = cast<ConstantArray>(C);
492   const Type *Type = CVA->getType()->getElementType();
493
494   unsigned Size = TD->getABITypeSize(Type);
495   if (Size <= 16) {
496     // We also need alignment here
497     const TargetData *TD = X86TM->getTargetData();
498     unsigned Align = TD->getPreferredAlignment(GV);
499     if (Align < Size)
500       Align = Size;
501
502     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
503     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
504                                                  SectionFlags::Strings,
505                                                  Size);
506     return getNamedSection(Name.c_str(), Flags);
507   }
508
509   return getReadOnlySection_();
510 }
511
512 std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
513   std::string Flags = ",\"";
514
515   if (!(flags & SectionFlags::Debug))
516     Flags += 'a';
517   if (flags & SectionFlags::Code)
518     Flags += 'x';
519   if (flags & SectionFlags::Writeable)
520     Flags += 'w';
521   if (flags & SectionFlags::Mergeable)
522     Flags += 'M';
523   if (flags & SectionFlags::Strings)
524     Flags += 'S';
525   if (flags & SectionFlags::TLS)
526     Flags += 'T';
527
528   Flags += "\"";
529
530   // FIXME: There can be exceptions here
531   if (flags & SectionFlags::BSS)
532     Flags += ",@nobits";
533   else
534     Flags += ",@progbits";
535
536   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
537     Flags += "," + utostr(entitySize);
538
539   return Flags;
540 }
541
542 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
543   X86TargetAsmInfo(TM) {
544   GlobalPrefix = "_";
545   LCOMMDirective = "\t.lcomm\t";
546   COMMDirectiveTakesAlignment = false;
547   HasDotTypeDotSizeDirective = false;
548   StaticCtorsSection = "\t.section .ctors,\"aw\"";
549   StaticDtorsSection = "\t.section .dtors,\"aw\"";
550   HiddenDirective = NULL;
551   PrivateGlobalPrefix = "L";  // Prefix for private global symbols
552   WeakRefDirective = "\t.weak\t";
553   SetDirective = "\t.set\t";
554
555   // Set up DWARF directives
556   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
557   AbsoluteDebugSectionOffsets = true;
558   AbsoluteEHSectionOffsets = false;
559   SupportsDebugInformation = true;
560   DwarfSectionOffsetDirective = "\t.secrel32\t";
561   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
562   DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
563   DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
564   DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
565   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
566   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
567   DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
568   DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
569   DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
570   DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
571   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
572 }
573
574 unsigned
575 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
576                                             bool Global) const {
577   CodeModel::Model CM = X86TM->getCodeModel();
578   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
579
580   if (X86TM->getRelocationModel() == Reloc::PIC_) {
581     unsigned Format = 0;
582
583     if (!is64Bit)
584       // 32 bit targets always encode pointers as 4 bytes
585       Format = DW_EH_PE_sdata4;
586     else {
587       // 64 bit targets encode pointers in 4 bytes iff:
588       // - code model is small OR
589       // - code model is medium and we're emitting externally visible symbols
590       //   or any code symbols
591       if (CM == CodeModel::Small ||
592           (CM == CodeModel::Medium && (Global ||
593                                        Reason != DwarfEncoding::Data)))
594         Format = DW_EH_PE_sdata4;
595       else
596         Format = DW_EH_PE_sdata8;
597     }
598
599     if (Global)
600       Format |= DW_EH_PE_indirect;
601
602     return (Format | DW_EH_PE_pcrel);
603   } else {
604     if (is64Bit &&
605         (CM == CodeModel::Small ||
606          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
607       return DW_EH_PE_udata4;
608     else
609       return DW_EH_PE_absptr;
610   }
611 }
612
613 std::string
614 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
615                                              SectionKind::Kind kind) const {
616   switch (kind) {
617    case SectionKind::Text:
618     return ".text$linkonce" + GV->getName();
619    case SectionKind::Data:
620    case SectionKind::BSS:
621    case SectionKind::ThreadData:
622    case SectionKind::ThreadBSS:
623     return ".data$linkonce" + GV->getName();
624    case SectionKind::ROData:
625    case SectionKind::RODataMergeConst:
626    case SectionKind::RODataMergeStr:
627     return ".rdata$linkonce" + GV->getName();
628    default:
629     assert(0 && "Unknown section kind");
630   }
631 }
632
633 std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
634   std::string Flags = ",\"";
635
636   if (flags & SectionFlags::Code)
637     Flags += 'x';
638   if (flags & SectionFlags::Writeable)
639     Flags += 'w';
640
641   Flags += "\"";
642
643   return Flags;
644 }
645
646 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
647   X86TargetAsmInfo(TM) {
648   GlobalPrefix = "_";
649   CommentString = ";";
650
651   PrivateGlobalPrefix = "$";
652   AlignDirective = "\talign\t";
653   ZeroDirective = "\tdb\t";
654   ZeroDirectiveSuffix = " dup(0)";
655   AsciiDirective = "\tdb\t";
656   AscizDirective = 0;
657   Data8bitsDirective = "\tdb\t";
658   Data16bitsDirective = "\tdw\t";
659   Data32bitsDirective = "\tdd\t";
660   Data64bitsDirective = "\tdq\t";
661   HasDotTypeDotSizeDirective = false;
662
663   TextSection = "_text";
664   DataSection = "_data";
665   JumpTableDataSection = NULL;
666   SwitchToSectionDirective = "";
667   TextSectionStartSuffix = "\tsegment 'CODE'";
668   DataSectionStartSuffix = "\tsegment 'DATA'";
669   SectionEndDirectiveSuffix = "\tends\n";
670 }