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