Use crazy template-based inheritance instead of virtual one.
[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 const char *const llvm::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 template <class BaseTAI>
41 bool X86TargetAsmInfo<BaseTAI>::LowerToBSwap(CallInst *CI) const {
42   // FIXME: this should verify that we are targetting a 486 or better.  If not,
43   // we will turn this bswap into something that will be lowered to logical ops
44   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
45   // so don't worry about this.
46
47   // Verify this is a simple bswap.
48   if (CI->getNumOperands() != 2 ||
49       CI->getType() != CI->getOperand(1)->getType() ||
50       !CI->getType()->isInteger())
51     return false;
52
53   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
54   if (!Ty || Ty->getBitWidth() % 16 != 0)
55     return false;
56
57   // Okay, we can do this xform, do so now.
58   const Type *Tys[] = { Ty };
59   Module *M = CI->getParent()->getParent()->getParent();
60   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
61
62   Value *Op = CI->getOperand(1);
63   Op = CallInst::Create(Int, Op, CI->getName(), CI);
64
65   CI->replaceAllUsesWith(Op);
66   CI->eraseFromParent();
67   return true;
68 }
69
70 template <class BaseTAI>
71 bool X86TargetAsmInfo<BaseTAI>::ExpandInlineAsm(CallInst *CI) const {
72   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
73   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
74
75   std::string AsmStr = IA->getAsmString();
76
77   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
78   std::vector<std::string> AsmPieces;
79   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
80
81   switch (AsmPieces.size()) {
82   default: return false;
83   case 1:
84     AsmStr = AsmPieces[0];
85     AsmPieces.clear();
86     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
87
88     // bswap $0
89     if (AsmPieces.size() == 2 &&
90         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
91       // No need to check constraints, nothing other than the equivalent of
92       // "=r,0" would be valid here.
93       return LowerToBSwap(CI);
94     }
95     break;
96   case 3:
97     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
98         Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
99         Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
100       // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
101       std::vector<std::string> Words;
102       SplitString(AsmPieces[0], Words, " \t");
103       if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
104         Words.clear();
105         SplitString(AsmPieces[1], Words, " \t");
106         if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
107           Words.clear();
108           SplitString(AsmPieces[2], Words, " \t,");
109           if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
110               Words[2] == "%edx") {
111             return LowerToBSwap(CI);
112           }
113         }
114       }
115     }
116     break;
117   }
118   return false;
119 }
120
121 X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
122   X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
123   const X86Subtarget* Subtarget = &DTM->getSubtarget<X86Subtarget>();
124   bool is64Bit = Subtarget->is64Bit();
125
126   AlignmentIsInBytes = false;
127   TextAlignFillValue = 0x90;
128   GlobalPrefix = "_";
129   if (!is64Bit)
130     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
131   ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
132   PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
133   LessPrivateGlobalPrefix = "l";  // Marker for some ObjC metadata
134   BSSSection = 0;                       // no BSS section.
135   ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
136   if (DTM->getRelocationModel() != Reloc::Static)
137     ConstantPoolSection = "\t.const_data";
138   else
139     ConstantPoolSection = "\t.const\n";
140   JumpTableDataSection = "\t.const\n";
141   CStringSection = "\t.cstring";
142   // FIXME: Why don't always use this section?
143   if (is64Bit) {
144     SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
145                                                    SectionFlags::Mergeable);
146   }
147   LCOMMDirective = "\t.lcomm\t";
148   SwitchToSectionDirective = "\t.section ";
149   StringConstantPrefix = "\1LC";
150   // Leopard and above support aligned common symbols.
151   COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
152   HasDotTypeDotSizeDirective = false;
153   if (TM.getRelocationModel() == Reloc::Static) {
154     StaticCtorsSection = ".constructor";
155     StaticDtorsSection = ".destructor";
156   } else {
157     StaticCtorsSection = ".mod_init_func";
158     StaticDtorsSection = ".mod_term_func";
159   }
160   if (is64Bit) {
161     PersonalityPrefix = "";
162     PersonalitySuffix = "+4@GOTPCREL";
163   } else {
164     PersonalityPrefix = "L";
165     PersonalitySuffix = "$non_lazy_ptr";
166   }
167   NeedsIndirectEncoding = true;
168   InlineAsmStart = "## InlineAsm Start";
169   InlineAsmEnd = "## InlineAsm End";
170   CommentString = "##";
171   SetDirective = "\t.set";
172   PCSymbol = ".";
173   UsedDirective = "\t.no_dead_strip\t";
174   WeakDefDirective = "\t.weak_definition ";
175   WeakRefDirective = "\t.weak_reference ";
176   HiddenDirective = "\t.private_extern ";
177   ProtectedDirective = "\t.globl\t";
178
179   // In non-PIC modes, emit a special label before jump tables so that the
180   // linker can perform more accurate dead code stripping.
181   if (TM.getRelocationModel() != Reloc::PIC_) {
182     // Emit a local label that is preserved until the linker runs.
183     JumpTableSpecialLabelPrefix = "l";
184   }
185
186   SupportsDebugInformation = true;
187   NeedsSet = true;
188   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
189   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
190   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
191   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
192   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
193   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
194   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
195   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
196   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
197   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
198   DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
199
200   // Exceptions handling
201   SupportsExceptionHandling = true;
202   GlobalEHDirective = "\t.globl\t";
203   SupportsWeakOmittedEHFrame = false;
204   AbsoluteEHSectionOffsets = false;
205   DwarfEHFrameSection =
206   ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
207   DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
208 }
209
210 unsigned
211 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
212                                               bool Global) const {
213   if (Reason == DwarfEncoding::Functions && Global)
214     return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
215   else if (Reason == DwarfEncoding::CodeLabels || !Global)
216     return DW_EH_PE_pcrel;
217   else
218     return DW_EH_PE_absptr;
219 }
220
221 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
222   X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
223
224   CStringSection = ".rodata.str";
225   PrivateGlobalPrefix = ".L";
226   WeakRefDirective = "\t.weak\t";
227   SetDirective = "\t.set\t";
228   PCSymbol = ".";
229
230   // Set up DWARF directives
231   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
232
233   // Debug Information
234   AbsoluteDebugSectionOffsets = true;
235   SupportsDebugInformation = true;
236   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
237   DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
238   DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
239   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
240   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
241   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
242   DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
243   DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
244   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
245   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
246   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
247
248   // Exceptions handling
249   SupportsExceptionHandling = true;
250   AbsoluteEHSectionOffsets = false;
251   DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
252   DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
253
254   // On Linux we must declare when we can use a non-executable stack.
255   if (ETM->getSubtarget<X86Subtarget>().isLinux())
256     NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
257 }
258
259 unsigned
260 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
261                                            bool Global) const {
262   CodeModel::Model CM = ETM->getCodeModel();
263   bool is64Bit = ETM->getSubtarget<X86Subtarget>().is64Bit();
264
265   if (ETM->getRelocationModel() == Reloc::PIC_) {
266     unsigned Format = 0;
267
268     if (!is64Bit)
269       // 32 bit targets always encode pointers as 4 bytes
270       Format = DW_EH_PE_sdata4;
271     else {
272       // 64 bit targets encode pointers in 4 bytes iff:
273       // - code model is small OR
274       // - code model is medium and we're emitting externally visible symbols
275       //   or any code symbols
276       if (CM == CodeModel::Small ||
277           (CM == CodeModel::Medium && (Global ||
278                                        Reason != DwarfEncoding::Data)))
279         Format = DW_EH_PE_sdata4;
280       else
281         Format = DW_EH_PE_sdata8;
282     }
283
284     if (Global)
285       Format |= DW_EH_PE_indirect;
286
287     return (Format | DW_EH_PE_pcrel);
288   } else {
289     if (is64Bit &&
290         (CM == CodeModel::Small ||
291          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
292       return DW_EH_PE_udata4;
293     else
294       return DW_EH_PE_absptr;
295   }
296 }
297
298 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
299   X86GenericTargetAsmInfo(TM) {
300   X86TM = &TM;
301
302   GlobalPrefix = "_";
303   LCOMMDirective = "\t.lcomm\t";
304   COMMDirectiveTakesAlignment = false;
305   HasDotTypeDotSizeDirective = false;
306   StaticCtorsSection = "\t.section .ctors,\"aw\"";
307   StaticDtorsSection = "\t.section .dtors,\"aw\"";
308   HiddenDirective = NULL;
309   PrivateGlobalPrefix = "L";  // Prefix for private global symbols
310   WeakRefDirective = "\t.weak\t";
311   SetDirective = "\t.set\t";
312
313   // Set up DWARF directives
314   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
315   AbsoluteDebugSectionOffsets = true;
316   AbsoluteEHSectionOffsets = false;
317   SupportsDebugInformation = true;
318   DwarfSectionOffsetDirective = "\t.secrel32\t";
319   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
320   DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
321   DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
322   DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
323   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
324   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
325   DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
326   DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
327   DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
328   DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
329   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
330 }
331
332 unsigned
333 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
334                                             bool Global) const {
335   CodeModel::Model CM = X86TM->getCodeModel();
336   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
337
338   if (X86TM->getRelocationModel() == Reloc::PIC_) {
339     unsigned Format = 0;
340
341     if (!is64Bit)
342       // 32 bit targets always encode pointers as 4 bytes
343       Format = DW_EH_PE_sdata4;
344     else {
345       // 64 bit targets encode pointers in 4 bytes iff:
346       // - code model is small OR
347       // - code model is medium and we're emitting externally visible symbols
348       //   or any code symbols
349       if (CM == CodeModel::Small ||
350           (CM == CodeModel::Medium && (Global ||
351                                        Reason != DwarfEncoding::Data)))
352         Format = DW_EH_PE_sdata4;
353       else
354         Format = DW_EH_PE_sdata8;
355     }
356
357     if (Global)
358       Format |= DW_EH_PE_indirect;
359
360     return (Format | DW_EH_PE_pcrel);
361   } else {
362     if (is64Bit &&
363         (CM == CodeModel::Small ||
364          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
365       return DW_EH_PE_udata4;
366     else
367       return DW_EH_PE_absptr;
368   }
369 }
370
371 std::string
372 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
373                                              SectionKind::Kind kind) const {
374   switch (kind) {
375    case SectionKind::Text:
376     return ".text$linkonce" + GV->getName();
377    case SectionKind::Data:
378    case SectionKind::BSS:
379    case SectionKind::ThreadData:
380    case SectionKind::ThreadBSS:
381     return ".data$linkonce" + GV->getName();
382    case SectionKind::ROData:
383    case SectionKind::RODataMergeConst:
384    case SectionKind::RODataMergeStr:
385     return ".rdata$linkonce" + GV->getName();
386    default:
387     assert(0 && "Unknown section kind");
388   }
389 }
390
391 std::string X86COFFTargetAsmInfo::printSectionFlags(unsigned flags) const {
392   std::string Flags = ",\"";
393
394   if (flags & SectionFlags::Code)
395     Flags += 'x';
396   if (flags & SectionFlags::Writeable)
397     Flags += 'w';
398
399   Flags += "\"";
400
401   return Flags;
402 }
403
404 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
405   X86GenericTargetAsmInfo(TM) {
406   GlobalPrefix = "_";
407   CommentString = ";";
408
409   PrivateGlobalPrefix = "$";
410   AlignDirective = "\talign\t";
411   ZeroDirective = "\tdb\t";
412   ZeroDirectiveSuffix = " dup(0)";
413   AsciiDirective = "\tdb\t";
414   AscizDirective = 0;
415   Data8bitsDirective = "\tdb\t";
416   Data16bitsDirective = "\tdw\t";
417   Data32bitsDirective = "\tdd\t";
418   Data64bitsDirective = "\tdq\t";
419   HasDotTypeDotSizeDirective = false;
420
421   TextSection = getUnnamedSection("_text", SectionFlags::Code);
422   DataSection = getUnnamedSection("_data", SectionFlags::Writeable);
423
424   JumpTableDataSection = NULL;
425   SwitchToSectionDirective = "";
426   TextSectionStartSuffix = "\tsegment 'CODE'";
427   DataSectionStartSuffix = "\tsegment 'DATA'";
428   SectionEndDirectiveSuffix = "\tends\n";
429 }