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