6bfc326f20dd4f49ef47d64fbc469c2e93ed9f8e
[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 using namespace llvm;
24
25 static const char* x86_asm_table[] = {"{si}", "S",
26                                       "{di}", "D",
27                                       "{ax}", "a",
28                                       "{cx}", "c",
29                                       "{memory}", "memory",
30                                       "{flags}", "",
31                                       "{dirflag}", "",
32                                       "{fpsr}", "",
33                                       "{cc}", "cc",
34                                       0,0};
35
36 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
37   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
38   
39   // FIXME - Should be simplified.
40
41   AsmTransCBE = x86_asm_table;
42   
43   switch (Subtarget->TargetType) {
44   case X86Subtarget::isDarwin:
45     AlignmentIsInBytes = false;
46     GlobalPrefix = "_";
47     if (!Subtarget->is64Bit())
48       Data64bitsDirective = 0;       // we can't emit a 64-bit unit
49     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
50     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
51     BSSSection = 0;                       // no BSS section.
52     ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
53     ConstantPoolSection = "\t.const\n";
54     JumpTableDataSection = "\t.const\n";
55     CStringSection = "\t.cstring";
56     FourByteConstantSection = "\t.literal4\n";
57     EightByteConstantSection = "\t.literal8\n";
58     if (Subtarget->is64Bit())
59       SixteenByteConstantSection = "\t.literal16\n";
60     ReadOnlySection = "\t.const\n";
61     LCOMMDirective = "\t.lcomm\t";
62     COMMDirectiveTakesAlignment = false;
63     HasDotTypeDotSizeDirective = false;
64     if (TM.getRelocationModel() == Reloc::Static) {
65       StaticCtorsSection = ".constructor";
66       StaticDtorsSection = ".destructor";
67     } else {
68       StaticCtorsSection = ".mod_init_func";
69       StaticDtorsSection = ".mod_term_func";
70     }
71     PersonalityPrefix = "L";
72     PersonalitySuffix = "$non_lazy_ptr";
73     NeedsIndirectEncoding = true;
74     InlineAsmStart = "# InlineAsm Start";
75     InlineAsmEnd = "# InlineAsm End";
76     SetDirective = "\t.set";
77     PCSymbol = ".";
78     UsedDirective = "\t.no_dead_strip\t";
79     WeakDefDirective = "\t.weak_definition\t";
80     WeakRefDirective = "\t.weak_reference\t";
81     HiddenDirective = "\t.private_extern\t";
82     
83     // In non-PIC modes, emit a special label before jump tables so that the
84     // linker can perform more accurate dead code stripping.
85     if (TM.getRelocationModel() != Reloc::PIC_) {
86       // Emit a local label that is preserved until the linker runs.
87       JumpTableSpecialLabelPrefix = "l";
88     }
89
90     SupportsDebugInformation = true;
91     NeedsSet = true;
92     DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
93     DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
94     DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
95     DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
96     GlobalEHDirective = "\t.globl\t";
97     DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
98     DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
99     DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
100     DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
101     DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
102     DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
103     DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
104
105     // Exceptions handling
106     if (!Subtarget->is64Bit())
107       SupportsExceptionHandling = true;
108     AbsoluteEHSectionOffsets = false;
109     DwarfEHFrameSection =
110     ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
111     DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
112     break;
113
114   case X86Subtarget::isELF:
115     ReadOnlySection = "\t.section\t.rodata";
116     FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
117     EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
118     SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
119     CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
120     PrivateGlobalPrefix = ".L";
121     WeakRefDirective = "\t.weak\t";
122     SetDirective = "\t.set\t";
123     PCSymbol = ".";
124
125     // Set up DWARF directives
126     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
127
128     // Debug Information
129     AbsoluteDebugSectionOffsets = true;
130     SupportsDebugInformation = true;
131     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
132     DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
133     DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
134     DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
135     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
136     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
137     DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
138     DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
139     DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
140     DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
141     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
142
143     // Exceptions handling
144     if (!Subtarget->is64Bit())
145       SupportsExceptionHandling = true;
146     AbsoluteEHSectionOffsets = false;
147     DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
148     DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
149     break;
150
151   case X86Subtarget::isCygwin:
152   case X86Subtarget::isMingw:
153     GlobalPrefix = "_";
154     LCOMMDirective = "\t.lcomm\t";
155     COMMDirectiveTakesAlignment = false;
156     HasDotTypeDotSizeDirective = false;
157     StaticCtorsSection = "\t.section .ctors,\"aw\"";
158     StaticDtorsSection = "\t.section .dtors,\"aw\"";
159     HiddenDirective = NULL;
160     PrivateGlobalPrefix = "L";  // Prefix for private global symbols
161     WeakRefDirective = "\t.weak\t";
162     SetDirective = "\t.set\t";
163
164     // Set up DWARF directives
165     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
166     AbsoluteDebugSectionOffsets = true;
167     AbsoluteEHSectionOffsets = false;
168     SupportsDebugInformation = true;
169     DwarfSectionOffsetDirective = "\t.secrel32\t";
170     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
171     DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
172     DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
173     DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
174     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
175     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
176     DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
177     DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
178     DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
179     DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
180     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
181     break;
182
183   case X86Subtarget::isWindows:
184     GlobalPrefix = "_";
185     HasDotTypeDotSizeDirective = false;
186     break;
187
188   default: break;
189   }
190   
191   if (Subtarget->isFlavorIntel()) {
192     GlobalPrefix = "_";
193     CommentString = ";";
194   
195     PrivateGlobalPrefix = "$";
196     AlignDirective = "\talign\t";
197     ZeroDirective = "\tdb\t";
198     ZeroDirectiveSuffix = " dup(0)";
199     AsciiDirective = "\tdb\t";
200     AscizDirective = 0;
201     Data8bitsDirective = "\tdb\t";
202     Data16bitsDirective = "\tdw\t";
203     Data32bitsDirective = "\tdd\t";
204     Data64bitsDirective = "\tdq\t";
205     HasDotTypeDotSizeDirective = false;
206     
207     TextSection = "_text";
208     DataSection = "_data";
209     JumpTableDataSection = NULL;
210     SwitchToSectionDirective = "";
211     TextSectionStartSuffix = "\tsegment 'CODE'";
212     DataSectionStartSuffix = "\tsegment 'DATA'";
213     SectionEndDirectiveSuffix = "\tends\n";
214   }
215
216   AssemblerDialect = Subtarget->getAsmFlavor();
217 }
218
219 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
220   // FIXME: this should verify that we are targetting a 486 or better.  If not,
221   // we will turn this bswap into something that will be lowered to logical ops
222   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
223   // so don't worry about this.
224   
225   // Verify this is a simple bswap.
226   if (CI->getNumOperands() != 2 ||
227       CI->getType() != CI->getOperand(1)->getType() ||
228       !CI->getType()->isInteger())
229     return false;
230   
231   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
232   if (!Ty || Ty->getBitWidth() % 16 != 0)
233     return false;
234   
235   // Okay, we can do this xform, do so now.
236   const Type *Tys[] = { Ty };
237   Module *M = CI->getParent()->getParent()->getParent();
238   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
239   
240   Value *Op = CI->getOperand(1);
241   Op = new CallInst(Int, Op, CI->getName(), CI);
242   
243   CI->replaceAllUsesWith(Op);
244   CI->eraseFromParent();
245   return true;
246 }
247
248
249 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
250   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
251   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
252   
253   std::string AsmStr = IA->getAsmString();
254   
255   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
256   std::vector<std::string> AsmPieces;
257   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
258   
259   switch (AsmPieces.size()) {
260   default: return false;    
261   case 1:
262     AsmStr = AsmPieces[0];
263     AsmPieces.clear();
264     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
265     
266     // bswap $0
267     if (AsmPieces.size() == 2 && 
268         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
269       // No need to check constraints, nothing other than the equivalent of
270       // "=r,0" would be valid here.
271       return LowerToBSwap(CI);
272     }
273     break;
274   case 3:
275     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
276         Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
277         Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
278       // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
279       std::vector<std::string> Words;
280       SplitString(AsmPieces[0], Words, " \t");
281       if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
282         Words.clear();
283         SplitString(AsmPieces[1], Words, " \t");
284         if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
285           Words.clear();
286           SplitString(AsmPieces[2], Words, " \t,");
287           if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
288               Words[2] == "%edx") {
289             return LowerToBSwap(CI);
290           }
291         }
292       }
293     }
294     break;
295   }
296   return false;
297 }