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