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