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