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