32d918efec1a06413ae5033a9e4b4d81a378272e
[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/InlineAsm.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/StringExtras.h"
21 using namespace llvm;
22
23 static const char* x86_asm_table[] = {"{si}", "S",
24                                       "{di}", "D",
25                                       "{ax}", "a",
26                                       "{cx}", "c",
27                                       "{memory}", "memory",
28                                       "{flags}", "",
29                                       "{dirflag}", "",
30                                       "{fpsr}", "",
31                                       "{cc}", "cc",
32                                       0,0};
33
34 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
35   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
36   
37   // FIXME - Should be simplified.
38
39   AsmTransCBE = x86_asm_table;
40   
41   switch (Subtarget->TargetType) {
42   case X86Subtarget::isDarwin:
43     AlignmentIsInBytes = false;
44     GlobalPrefix = "_";
45     if (!Subtarget->is64Bit())
46       Data64bitsDirective = 0;       // we can't emit a 64-bit unit
47     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
48     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
49     ConstantPoolSection = "\t.const\n";
50     JumpTableDataSection = "\t.const\n";
51     CStringSection = "\t.cstring";
52     FourByteConstantSection = "\t.literal4\n";
53     EightByteConstantSection = "\t.literal8\n";
54     if (Subtarget->is64Bit())
55       SixteenByteConstantSection = "\t.literal16\n";
56     LCOMMDirective = "\t.lcomm\t";
57     COMMDirectiveTakesAlignment = false;
58     HasDotTypeDotSizeDirective = false;
59     StaticCtorsSection = ".mod_init_func";
60     StaticDtorsSection = ".mod_term_func";
61     InlineAsmStart = "# InlineAsm Start";
62     InlineAsmEnd = "# InlineAsm End";
63     SetDirective = "\t.set";
64     UsedDirective = "\t.no_dead_strip\t";
65     
66     NeedsSet = true;
67     DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
68     DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
69     DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
70     DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
71     DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
72     DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
73     DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
74     DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
75     DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
76     DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
77     DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
78     break;
79
80   case X86Subtarget::isELF:
81     // Set up DWARF directives
82     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
83     // bool HasLEB128; // Defaults to false.
84     // hasDotLoc - True if target asm supports .loc directives.
85     // bool HasDotLoc; // Defaults to false.
86     // HasDotFile - True if target asm supports .file directives.
87     // bool HasDotFile; // Defaults to false.
88     PrivateGlobalPrefix = ".";  // Prefix for private global symbols
89     DwarfRequiresFrameSection = false;
90     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
91     DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
92     DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
93     DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
94     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
95     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
96     DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
97     DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
98     DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
99     DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
100     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
101     break;
102
103   case X86Subtarget::isCygwin:
104     GlobalPrefix = "_";
105     COMMDirectiveTakesAlignment = false;
106     HasDotTypeDotSizeDirective = false;
107     StaticCtorsSection = "\t.section .ctors,\"aw\"";
108     StaticDtorsSection = "\t.section .dtors,\"aw\"";
109
110     // Set up DWARF directives
111     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
112     PrivateGlobalPrefix = "L";  // Prefix for private global symbols
113     DwarfRequiresFrameSection = false;
114     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
115     DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
116     DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
117     DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
118     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
119     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
120     DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
121     DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
122     DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
123     DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
124     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
125     break;
126     
127     break;
128   case X86Subtarget::isWindows:
129     GlobalPrefix = "_";
130     HasDotTypeDotSizeDirective = false;
131     break;
132   default: break;
133   }
134   
135   if (Subtarget->isFlavorIntel()) {
136     GlobalPrefix = "_";
137     CommentString = ";";
138   
139     PrivateGlobalPrefix = "$";
140     AlignDirective = "\talign\t";
141     ZeroDirective = "\tdb\t";
142     ZeroDirectiveSuffix = " dup(0)";
143     AsciiDirective = "\tdb\t";
144     AscizDirective = 0;
145     Data8bitsDirective = "\tdb\t";
146     Data16bitsDirective = "\tdw\t";
147     Data32bitsDirective = "\tdd\t";
148     Data64bitsDirective = "\tdq\t";
149     HasDotTypeDotSizeDirective = false;
150     
151     TextSection = "_text";
152     DataSection = "_data";
153     SwitchToSectionDirective = "";
154     TextSectionStartSuffix = "\tsegment 'CODE'";
155     DataSectionStartSuffix = "\tsegment 'DATA'";
156     SectionEndDirectiveSuffix = "\tends\n";
157   }
158 }
159
160 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
161   // FIXME: this should verify that we are targetting a 486 or better.  If not,
162   // we will turn this bswap into something that will be lowered to logical ops
163   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
164   // so don't worry about this.
165   
166   // Verify this is a simple bswap.
167   if (CI->getNumOperands() != 2 ||
168       CI->getType() != CI->getOperand(1)->getType() ||
169       !CI->getType()->isInteger())
170     return false;
171   
172   const Type *Ty = CI->getType()->getUnsignedVersion();
173   const char *IntName;
174   switch (Ty->getTypeID()) {
175   default: return false;
176   case Type::UShortTyID: IntName = "llvm.bswap.i16"; break;
177   case Type::UIntTyID:   IntName = "llvm.bswap.i32"; break;
178   case Type::ULongTyID:  IntName = "llvm.bswap.i64"; break;
179   }
180
181   // Okay, we can do this xform, do so now.
182   Module *M = CI->getParent()->getParent()->getParent();
183   Function *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
184   
185   Value *Op = CI->getOperand(1);
186   if (CI->getOperand(1)->getType() != Ty)
187     Op = new BitCastInst(Op, Ty, Op->getName(), CI);
188   
189   Op = new CallInst(Int, Op, CI->getName(), CI);
190   
191   if (Op->getType() != CI->getType())
192     Op = new BitCastInst(Op, CI->getType(), Op->getName(), CI);
193   
194   CI->replaceAllUsesWith(Op);
195   CI->eraseFromParent();
196   return true;
197 }
198
199
200 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
201   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
202   //std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
203   
204   std::string AsmStr = IA->getAsmString();
205   
206   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
207   std::vector<std::string> AsmPieces;
208   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
209   
210   switch (AsmPieces.size()) {
211   default: return false;    
212   case 1:
213     AsmStr = AsmPieces[0];
214     AsmPieces.clear();
215     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
216     
217     if (AsmPieces.size() == 2 && 
218         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
219       // No need to check constraints, nothing other than the equivalent of
220       // "=r,0" would be valid here.
221       return LowerToBSwap(CI);
222     }
223     break;
224   }
225   return false;
226 }