Eliminate SectionFlags, just embed a SectionKind into Section
[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 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/ErrorHandling.h"
25
26 using namespace llvm;
27 using namespace llvm::dwarf;
28
29 const char *const llvm::x86_asm_table[] = {
30   "{si}", "S",
31   "{di}", "D",
32   "{ax}", "a",
33   "{cx}", "c",
34   "{memory}", "memory",
35   "{flags}", "",
36   "{dirflag}", "",
37   "{fpsr}", "",
38   "{cc}", "cc",
39   0,0};
40
41 X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
42   X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
43   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
44   bool is64Bit = Subtarget->is64Bit();
45
46   AlignmentIsInBytes = false;
47   TextAlignFillValue = 0x90;
48
49
50   if (!is64Bit)
51     Data64bitsDirective = 0;       // we can't emit a 64-bit unit
52   ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
53   ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
54   if (TM.getRelocationModel() != Reloc::Static)
55     ConstantPoolSection = "\t.const_data";
56   else
57     ConstantPoolSection = "\t.const\n";
58   // FIXME: Why don't we always use this section?
59   if (is64Bit)
60     SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
61                                                  SectionKind::MergeableConst16);
62   LCOMMDirective = "\t.lcomm\t";
63
64   // Leopard and above support aligned common symbols.
65   COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
66   HasDotTypeDotSizeDirective = false;
67
68   if (is64Bit) {
69     PersonalityPrefix = "";
70     PersonalitySuffix = "+4@GOTPCREL";
71   } else {
72     PersonalityPrefix = "L";
73     PersonalitySuffix = "$non_lazy_ptr";
74   }
75
76   InlineAsmStart = "## InlineAsm Start";
77   InlineAsmEnd = "## InlineAsm End";
78   CommentString = "##";
79   SetDirective = "\t.set";
80   PCSymbol = ".";
81   UsedDirective = "\t.no_dead_strip\t";
82   ProtectedDirective = "\t.globl\t";
83
84   SupportsDebugInformation = true;
85   DwarfDebugInlineSection = ".section __DWARF,__debug_inlined,regular,debug";
86   DwarfUsesInlineInfoSection = true;
87
88   // Exceptions handling
89   SupportsExceptionHandling = true;
90   GlobalEHDirective = "\t.globl\t";
91   SupportsWeakOmittedEHFrame = false;
92   AbsoluteEHSectionOffsets = false;
93   DwarfEHFrameSection =
94   ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
95   DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
96 }
97
98 unsigned
99 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
100                                               bool Global) const {
101   if (Reason == DwarfEncoding::Functions && Global)
102     return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
103   else if (Reason == DwarfEncoding::CodeLabels || !Global)
104     return DW_EH_PE_pcrel;
105   else
106     return DW_EH_PE_absptr;
107 }
108
109 const char *
110 X86DarwinTargetAsmInfo::getEHGlobalPrefix() const
111 {
112   const X86Subtarget* Subtarget = &TM.getSubtarget<X86Subtarget>();
113   if (Subtarget->getDarwinVers() > 9)
114     return PrivateGlobalPrefix;
115   else
116     return "";
117 }
118
119 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
120   X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
121
122   CStringSection = ".rodata.str";
123   PrivateGlobalPrefix = ".L";
124   WeakRefDirective = "\t.weak\t";
125   SetDirective = "\t.set\t";
126   PCSymbol = ".";
127
128   // Set up DWARF directives
129   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
130
131   BSSSection_ = getUnnamedSection("\t.bss", SectionKind::BSS);
132
133   // Debug Information
134   AbsoluteDebugSectionOffsets = true;
135   SupportsDebugInformation = true;
136   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
137   DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
138   DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
139   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
140   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
141   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
142   DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
143   DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
144   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
145   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
146   DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
147
148   // Exceptions handling
149   SupportsExceptionHandling = true;
150   AbsoluteEHSectionOffsets = false;
151   DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
152   DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
153
154   // On Linux we must declare when we can use a non-executable stack.
155   if (TM.getSubtarget<X86Subtarget>().isLinux())
156     NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
157 }
158
159 unsigned
160 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
161                                            bool Global) const {
162   CodeModel::Model CM = TM.getCodeModel();
163   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
164
165   if (TM.getRelocationModel() == Reloc::PIC_) {
166     unsigned Format = 0;
167
168     if (!is64Bit)
169       // 32 bit targets always encode pointers as 4 bytes
170       Format = DW_EH_PE_sdata4;
171     else {
172       // 64 bit targets encode pointers in 4 bytes iff:
173       // - code model is small OR
174       // - code model is medium and we're emitting externally visible symbols
175       //   or any code symbols
176       if (CM == CodeModel::Small ||
177           (CM == CodeModel::Medium && (Global ||
178                                        Reason != DwarfEncoding::Data)))
179         Format = DW_EH_PE_sdata4;
180       else
181         Format = DW_EH_PE_sdata8;
182     }
183
184     if (Global)
185       Format |= DW_EH_PE_indirect;
186
187     return (Format | DW_EH_PE_pcrel);
188   } else {
189     if (is64Bit &&
190         (CM == CodeModel::Small ||
191          (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
192       return DW_EH_PE_udata4;
193     else
194       return DW_EH_PE_absptr;
195   }
196 }
197
198 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
199   X86GenericTargetAsmInfo(TM) {
200
201   GlobalPrefix = "_";
202   LCOMMDirective = "\t.lcomm\t";
203   COMMDirectiveTakesAlignment = false;
204   HasDotTypeDotSizeDirective = false;
205   HasSingleParameterDotFile = false;
206   StaticCtorsSection = "\t.section .ctors,\"aw\"";
207   StaticDtorsSection = "\t.section .dtors,\"aw\"";
208   HiddenDirective = NULL;
209   PrivateGlobalPrefix = "L";  // Prefix for private global symbols
210   WeakRefDirective = "\t.weak\t";
211   SetDirective = "\t.set\t";
212
213   // Set up DWARF directives
214   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
215   AbsoluteDebugSectionOffsets = true;
216   AbsoluteEHSectionOffsets = false;
217   SupportsDebugInformation = true;
218   DwarfSectionOffsetDirective = "\t.secrel32\t";
219   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
220   DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
221   DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
222   DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
223   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
224   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
225   DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
226   DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
227   DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
228   DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
229   DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
230 }
231
232 unsigned
233 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
234                                             bool Global) const {
235   CodeModel::Model CM = TM.getCodeModel();
236   bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
237
238   if (TM.getRelocationModel() == Reloc::PIC_) {
239     unsigned Format = 0;
240
241     if (!is64Bit)
242       // 32 bit targets always encode pointers as 4 bytes
243       Format = DW_EH_PE_sdata4;
244     else {
245       // 64 bit targets encode pointers in 4 bytes iff:
246       // - code model is small OR
247       // - code model is medium and we're emitting externally visible symbols
248       //   or any code symbols
249       if (CM == CodeModel::Small ||
250           (CM == CodeModel::Medium && (Global ||
251                                        Reason != DwarfEncoding::Data)))
252         Format = DW_EH_PE_sdata4;
253       else
254         Format = DW_EH_PE_sdata8;
255     }
256
257     if (Global)
258       Format |= DW_EH_PE_indirect;
259
260     return (Format | DW_EH_PE_pcrel);
261   }
262   
263   if (is64Bit &&
264       (CM == CodeModel::Small ||
265        (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
266     return DW_EH_PE_udata4;
267   return DW_EH_PE_absptr;
268 }
269
270 const char *X86COFFTargetAsmInfo::
271 getSectionPrefixForUniqueGlobal(SectionKind Kind) const {
272   if (Kind.isText())
273     return ".text$linkonce";
274   if (Kind.isWriteable())
275     return ".data$linkonce";
276   return ".rdata$linkonce";
277 }
278
279
280
281 void X86COFFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind,
282                                             SmallVectorImpl<char> &Str) const {
283   // FIXME: Inefficient.
284   std::string Res = ",\"";
285   if (Kind.isText())
286     Res += 'x';
287   if (Kind.isWriteable())
288     Res += 'w';
289   Res += "\"";
290
291   Str.append(Res.begin(), Res.end());
292 }
293
294 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
295   X86GenericTargetAsmInfo(TM) {
296   GlobalPrefix = "_";
297   CommentString = ";";
298
299   InlineAsmStart = "; InlineAsm Start";
300   InlineAsmEnd   = "; InlineAsm End";
301
302   PrivateGlobalPrefix = "$";
303   AlignDirective = "\tALIGN\t";
304   ZeroDirective = "\tdb\t";
305   ZeroDirectiveSuffix = " dup(0)";
306   AsciiDirective = "\tdb\t";
307   AscizDirective = 0;
308   Data8bitsDirective = "\tdb\t";
309   Data16bitsDirective = "\tdw\t";
310   Data32bitsDirective = "\tdd\t";
311   Data64bitsDirective = "\tdq\t";
312   HasDotTypeDotSizeDirective = false;
313   HasSingleParameterDotFile = false;
314
315   AlignmentIsInBytes = true;
316
317   TextSection = getUnnamedSection("_text", SectionKind::Text);
318   DataSection = getUnnamedSection("_data", SectionKind::DataRel);
319
320   JumpTableDataSection = NULL;
321   SwitchToSectionDirective = "";
322   TextSectionStartSuffix = "\tSEGMENT PARA 'CODE'";
323   DataSectionStartSuffix = "\tSEGMENT PARA 'DATA'";
324   SectionEndDirectiveSuffix = "\tends\n";
325 }
326
327 // Instantiate default implementation.
328 TEMPLATE_INSTANTIATION(class X86TargetAsmInfo<TargetAsmInfo>);