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