c55c4caa07ee997f22fcb80dc8a13c8e0e608ef3
[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\n";
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     AbsoluteDebugSectionOffsets = true;
114     AbsoluteEHSectionOffsets = false;
115     SupportsDebugInformation = true;
116     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
117     DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
118     DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
119     DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
120     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
121     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
122     DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
123     DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
124     DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
125     DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
126     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
127     
128     SupportsExceptionHandling = true;
129     DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
130     DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
131     break;
132
133   case X86Subtarget::isCygwin:
134   case X86Subtarget::isMingw:
135     GlobalPrefix = "_";
136     LCOMMDirective = "\t.lcomm\t";
137     COMMDirectiveTakesAlignment = false;
138     HasDotTypeDotSizeDirective = false;
139     StaticCtorsSection = "\t.section .ctors,\"aw\"";
140     StaticDtorsSection = "\t.section .dtors,\"aw\"";
141     HiddenDirective = NULL;
142     PrivateGlobalPrefix = "L";  // Prefix for private global symbols
143     WeakRefDirective = "\t.weak\t";
144     SetDirective = "\t.set\t";
145
146     // Set up DWARF directives
147     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
148     AbsoluteDebugSectionOffsets = true;
149     AbsoluteEHSectionOffsets = false;
150     SupportsDebugInformation = true;
151     DwarfSectionOffsetDirective = "\t.secrel32\t";
152     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
153     DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
154     DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
155     DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
156     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
157     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
158     DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
159     DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
160     DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
161     DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
162     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
163     break;
164
165   case X86Subtarget::isWindows:
166     GlobalPrefix = "_";
167     HasDotTypeDotSizeDirective = false;
168     break;
169
170   default: break;
171   }
172   
173   if (Subtarget->isFlavorIntel()) {
174     GlobalPrefix = "_";
175     CommentString = ";";
176   
177     PrivateGlobalPrefix = "$";
178     AlignDirective = "\talign\t";
179     ZeroDirective = "\tdb\t";
180     ZeroDirectiveSuffix = " dup(0)";
181     AsciiDirective = "\tdb\t";
182     AscizDirective = 0;
183     Data8bitsDirective = "\tdb\t";
184     Data16bitsDirective = "\tdw\t";
185     Data32bitsDirective = "\tdd\t";
186     Data64bitsDirective = "\tdq\t";
187     HasDotTypeDotSizeDirective = false;
188     
189     TextSection = "_text";
190     DataSection = "_data";
191     JumpTableDataSection = NULL;
192     SwitchToSectionDirective = "";
193     TextSectionStartSuffix = "\tsegment 'CODE'";
194     DataSectionStartSuffix = "\tsegment 'DATA'";
195     SectionEndDirectiveSuffix = "\tends\n";
196   }
197
198   AssemblerDialect = Subtarget->getAsmFlavor();
199 }
200
201 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
202   // FIXME: this should verify that we are targetting a 486 or better.  If not,
203   // we will turn this bswap into something that will be lowered to logical ops
204   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
205   // so don't worry about this.
206   
207   // Verify this is a simple bswap.
208   if (CI->getNumOperands() != 2 ||
209       CI->getType() != CI->getOperand(1)->getType() ||
210       !CI->getType()->isInteger())
211     return false;
212   
213   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
214   if (!Ty || Ty->getBitWidth() % 16 != 0)
215     return false;
216   
217   // Okay, we can do this xform, do so now.
218   const Type *Tys[] = { Ty, Ty };
219   Module *M = CI->getParent()->getParent()->getParent();
220   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
221   
222   Value *Op = CI->getOperand(1);
223   Op = new CallInst(Int, Op, CI->getName(), CI);
224   
225   CI->replaceAllUsesWith(Op);
226   CI->eraseFromParent();
227   return true;
228 }
229
230
231 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
232   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
233   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
234   
235   std::string AsmStr = IA->getAsmString();
236   
237   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
238   std::vector<std::string> AsmPieces;
239   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
240   
241   switch (AsmPieces.size()) {
242   default: return false;    
243   case 1:
244     AsmStr = AsmPieces[0];
245     AsmPieces.clear();
246     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
247     
248     // bswap $0
249     if (AsmPieces.size() == 2 && 
250         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
251       // No need to check constraints, nothing other than the equivalent of
252       // "=r,0" would be valid here.
253       return LowerToBSwap(CI);
254     }
255     break;
256   case 3:
257     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
258         Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
259         Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
260       // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
261       std::vector<std::string> Words;
262       SplitString(AsmPieces[0], Words, " \t");
263       if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
264         Words.clear();
265         SplitString(AsmPieces[1], Words, " \t");
266         if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
267           Words.clear();
268           SplitString(AsmPieces[2], Words, " \t,");
269           if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
270               Words[2] == "%edx") {
271             return LowerToBSwap(CI);
272           }
273         }
274       }
275     }
276     break;
277   }
278   return false;
279 }