Long double patch 8 of N: make it partially work in
[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 was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source 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     InlineAsmStart = "# InlineAsm Start";
72     InlineAsmEnd = "# InlineAsm End";
73     SetDirective = "\t.set";
74     UsedDirective = "\t.no_dead_strip\t";
75     WeakRefDirective = "\t.weak_reference\t";
76     HiddenDirective = "\t.private_extern\t";
77     
78     // In non-PIC modes, emit a special label before jump tables so that the
79     // linker can perform more accurate dead code stripping.
80     if (TM.getRelocationModel() != Reloc::PIC_) {
81       // Emit a local label that is preserved until the linker runs.
82       JumpTableSpecialLabelPrefix = "l";
83     }
84
85     SupportsDebugInformation = true;
86     NeedsSet = true;
87     DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
88     DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
89     DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
90     DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
91     DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
92     DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
93     DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
94     DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
95     DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
96     DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
97     DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
98     break;
99
100   case X86Subtarget::isELF:
101     ReadOnlySection = "\t.section\t.rodata";
102     FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
103     EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
104     SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
105     CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
106     PrivateGlobalPrefix = ".L";
107     WeakRefDirective = "\t.weak\t";
108     SetDirective = "\t.set\t";
109     PCSymbol = ".";
110
111     // Set up DWARF directives
112     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
113
114     // Debug Information
115     AbsoluteDebugSectionOffsets = true;
116     SupportsDebugInformation = true;
117     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
118     DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
119     DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
120     DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
121     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
122     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
123     DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
124     DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
125     DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
126     DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
127     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
128
129     // Exceptions handling
130     if (!Subtarget->is64Bit())
131       SupportsExceptionHandling = true;
132     AbsoluteEHSectionOffsets = false;
133     DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
134     DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
135     break;
136
137   case X86Subtarget::isCygwin:
138   case X86Subtarget::isMingw:
139     GlobalPrefix = "_";
140     LCOMMDirective = "\t.lcomm\t";
141     COMMDirectiveTakesAlignment = false;
142     HasDotTypeDotSizeDirective = false;
143     StaticCtorsSection = "\t.section .ctors,\"aw\"";
144     StaticDtorsSection = "\t.section .dtors,\"aw\"";
145     HiddenDirective = NULL;
146     PrivateGlobalPrefix = "L";  // Prefix for private global symbols
147     WeakRefDirective = "\t.weak\t";
148     SetDirective = "\t.set\t";
149
150     // Set up DWARF directives
151     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
152     AbsoluteDebugSectionOffsets = true;
153     AbsoluteEHSectionOffsets = false;
154     SupportsDebugInformation = true;
155     DwarfSectionOffsetDirective = "\t.secrel32\t";
156     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
157     DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
158     DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
159     DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
160     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
161     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
162     DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
163     DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
164     DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
165     DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
166     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
167     break;
168
169   case X86Subtarget::isWindows:
170     GlobalPrefix = "_";
171     HasDotTypeDotSizeDirective = false;
172     break;
173
174   default: break;
175   }
176   
177   if (Subtarget->isFlavorIntel()) {
178     GlobalPrefix = "_";
179     CommentString = ";";
180   
181     PrivateGlobalPrefix = "$";
182     AlignDirective = "\talign\t";
183     ZeroDirective = "\tdb\t";
184     ZeroDirectiveSuffix = " dup(0)";
185     AsciiDirective = "\tdb\t";
186     AscizDirective = 0;
187     Data8bitsDirective = "\tdb\t";
188     Data16bitsDirective = "\tdw\t";
189     Data32bitsDirective = "\tdd\t";
190     Data64bitsDirective = "\tdq\t";
191     HasDotTypeDotSizeDirective = false;
192     
193     TextSection = "_text";
194     DataSection = "_data";
195     JumpTableDataSection = NULL;
196     SwitchToSectionDirective = "";
197     TextSectionStartSuffix = "\tsegment 'CODE'";
198     DataSectionStartSuffix = "\tsegment 'DATA'";
199     SectionEndDirectiveSuffix = "\tends\n";
200   }
201
202   AssemblerDialect = Subtarget->getAsmFlavor();
203 }
204
205 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
206   // FIXME: this should verify that we are targetting a 486 or better.  If not,
207   // we will turn this bswap into something that will be lowered to logical ops
208   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
209   // so don't worry about this.
210   
211   // Verify this is a simple bswap.
212   if (CI->getNumOperands() != 2 ||
213       CI->getType() != CI->getOperand(1)->getType() ||
214       !CI->getType()->isInteger())
215     return false;
216   
217   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
218   if (!Ty || Ty->getBitWidth() % 16 != 0)
219     return false;
220   
221   // Okay, we can do this xform, do so now.
222   const Type *Tys[] = { Ty };
223   Module *M = CI->getParent()->getParent()->getParent();
224   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
225   
226   Value *Op = CI->getOperand(1);
227   Op = new CallInst(Int, Op, CI->getName(), CI);
228   
229   CI->replaceAllUsesWith(Op);
230   CI->eraseFromParent();
231   return true;
232 }
233
234
235 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
236   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
237   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
238   
239   std::string AsmStr = IA->getAsmString();
240   
241   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
242   std::vector<std::string> AsmPieces;
243   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
244   
245   switch (AsmPieces.size()) {
246   default: return false;    
247   case 1:
248     AsmStr = AsmPieces[0];
249     AsmPieces.clear();
250     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
251     
252     // bswap $0
253     if (AsmPieces.size() == 2 && 
254         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
255       // No need to check constraints, nothing other than the equivalent of
256       // "=r,0" would be valid here.
257       return LowerToBSwap(CI);
258     }
259     break;
260   case 3:
261     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
262         Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
263         Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
264       // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
265       std::vector<std::string> Words;
266       SplitString(AsmPieces[0], Words, " \t");
267       if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
268         Words.clear();
269         SplitString(AsmPieces[1], Words, " \t");
270         if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
271           Words.clear();
272           SplitString(AsmPieces[2], Words, " \t,");
273           if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
274               Words[2] == "%edx") {
275             return LowerToBSwap(CI);
276           }
277         }
278       }
279     }
280     break;
281   }
282   return false;
283 }