blob: 802d04a6b3c23355fd5abe3fa5d754bdf9a85e37 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000023#include "llvm/Support/Dwarf.h"
Edwin Török675d5622009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000025
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000027using namespace llvm::dwarf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000029const 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};
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Anton Korobeynikovf700e682008-07-09 13:20:48 +000041X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000042 X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
Bill Wendling0ddb3862009-07-13 18:48:39 +000043 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikov16876eb2008-08-08 18:25:52 +000044 bool is64Bit = Subtarget->is64Bit();
Anton Korobeynikovf700e682008-07-09 13:20:48 +000045
46 AlignmentIsInBytes = false;
47 TextAlignFillValue = 0x90;
Daniel Dunbar1e13b972009-07-24 08:24:36 +000048
49
Anton Korobeynikovf700e682008-07-09 13:20:48 +000050 if (!is64Bit)
51 Data64bitsDirective = 0; // we can't emit a 64-bit unit
52 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Anton Korobeynikovf700e682008-07-09 13:20:48 +000053 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Dan Gohmana004d7b2008-11-03 18:22:42 +000054 if (TM.getRelocationModel() != Reloc::Static)
Anton Korobeynikova7463ee2008-07-09 21:54:26 +000055 ConstantPoolSection = "\t.const_data";
56 else
57 ConstantPoolSection = "\t.const\n";
Chris Lattnerca072172009-06-19 00:08:39 +000058 // FIXME: Why don't we always use this section?
59 if (is64Bit)
Anton Korobeynikovbce78512008-09-24 22:18:54 +000060 SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
61 SectionFlags::Mergeable);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000062 LCOMMDirective = "\t.lcomm\t";
Bill Wendling0ddb3862009-07-13 18:48:39 +000063
Anton Korobeynikov16876eb2008-08-08 18:25:52 +000064 // Leopard and above support aligned common symbols.
65 COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000066 HasDotTypeDotSizeDirective = false;
Daniel Dunbar1e13b972009-07-24 08:24:36 +000067
Anton Korobeynikovf700e682008-07-09 13:20:48 +000068 if (is64Bit) {
69 PersonalityPrefix = "";
70 PersonalitySuffix = "+4@GOTPCREL";
71 } else {
72 PersonalityPrefix = "L";
73 PersonalitySuffix = "$non_lazy_ptr";
74 }
Bill Wendling0ddb3862009-07-13 18:48:39 +000075
Anton Korobeynikovf700e682008-07-09 13:20:48 +000076 InlineAsmStart = "## InlineAsm Start";
77 InlineAsmEnd = "## InlineAsm End";
78 CommentString = "##";
79 SetDirective = "\t.set";
80 PCSymbol = ".";
81 UsedDirective = "\t.no_dead_strip\t";
Anton Korobeynikovf700e682008-07-09 13:20:48 +000082 ProtectedDirective = "\t.globl\t";
83
Anton Korobeynikovf700e682008-07-09 13:20:48 +000084 SupportsDebugInformation = true;
Devang Patel88bf96e2009-04-13 17:02:03 +000085 DwarfDebugInlineSection = ".section __DWARF,__debug_inlined,regular,debug";
86 DwarfUsesInlineInfoSection = true;
Anton Korobeynikovf700e682008-07-09 13:20:48 +000087
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";
Bill Wendling4e39d3e2009-07-13 20:27:41 +000095 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Anton Korobeynikovf700e682008-07-09 13:20:48 +000096}
97
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +000098unsigned
99X86DarwinTargetAsmInfo::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
Rafael Espindola1a931842008-12-19 10:55:56 +0000109const char *
110X86DarwinTargetAsmInfo::getEHGlobalPrefix() const
111{
112 const X86Subtarget* Subtarget = &TM.getSubtarget<X86Subtarget>();
113 if (Subtarget->getDarwinVers() > 9)
114 return PrivateGlobalPrefix;
115 else
116 return "";
117}
118
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000119X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000120 X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
Anton Korobeynikovc33a1ff2008-07-09 13:28:49 +0000121
Anton Korobeynikov03db6772008-07-09 13:25:26 +0000122 CStringSection = ".rodata.str";
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000123 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
Chris Lattnerc51585b2009-07-26 19:23:28 +0000131 BSSSection_ = getUnnamedSection("\t.bss",
132 SectionFlags::Writable | SectionFlags::BSS);
133
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000134 // Debug Information
135 AbsoluteDebugSectionOffsets = true;
136 SupportsDebugInformation = true;
137 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
138 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
139 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
140 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
141 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
142 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
143 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
144 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
145 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
146 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
Chris Lattnerecbf73b2009-06-18 23:31:37 +0000147 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000148
149 // Exceptions handling
Anton Korobeynikov15f3a842008-09-08 21:13:08 +0000150 SupportsExceptionHandling = true;
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000151 AbsoluteEHSectionOffsets = false;
152 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
153 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
154
155 // On Linux we must declare when we can use a non-executable stack.
Dan Gohmana004d7b2008-11-03 18:22:42 +0000156 if (TM.getSubtarget<X86Subtarget>().isLinux())
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000157 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
158}
159
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000160unsigned
161X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
162 bool Global) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000163 CodeModel::Model CM = TM.getCodeModel();
164 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000165
Dan Gohmana004d7b2008-11-03 18:22:42 +0000166 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000167 unsigned Format = 0;
168
169 if (!is64Bit)
170 // 32 bit targets always encode pointers as 4 bytes
171 Format = DW_EH_PE_sdata4;
172 else {
173 // 64 bit targets encode pointers in 4 bytes iff:
174 // - code model is small OR
175 // - code model is medium and we're emitting externally visible symbols
176 // or any code symbols
177 if (CM == CodeModel::Small ||
178 (CM == CodeModel::Medium && (Global ||
179 Reason != DwarfEncoding::Data)))
180 Format = DW_EH_PE_sdata4;
181 else
182 Format = DW_EH_PE_sdata8;
183 }
184
185 if (Global)
186 Format |= DW_EH_PE_indirect;
187
188 return (Format | DW_EH_PE_pcrel);
189 } else {
190 if (is64Bit &&
191 (CM == CodeModel::Small ||
192 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
193 return DW_EH_PE_udata4;
194 else
195 return DW_EH_PE_absptr;
196 }
197}
198
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000199X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000200 X86GenericTargetAsmInfo(TM) {
asl631323b2008-07-19 13:15:21 +0000201
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000202 GlobalPrefix = "_";
203 LCOMMDirective = "\t.lcomm\t";
204 COMMDirectiveTakesAlignment = false;
205 HasDotTypeDotSizeDirective = false;
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000206 HasSingleParameterDotFile = false;
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000207 StaticCtorsSection = "\t.section .ctors,\"aw\"";
208 StaticDtorsSection = "\t.section .dtors,\"aw\"";
209 HiddenDirective = NULL;
210 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
211 WeakRefDirective = "\t.weak\t";
212 SetDirective = "\t.set\t";
213
214 // Set up DWARF directives
215 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
216 AbsoluteDebugSectionOffsets = true;
217 AbsoluteEHSectionOffsets = false;
218 SupportsDebugInformation = true;
219 DwarfSectionOffsetDirective = "\t.secrel32\t";
220 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
221 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
222 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
223 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
224 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
225 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
226 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
227 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
228 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
229 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
Chris Lattnerecbf73b2009-06-18 23:31:37 +0000230 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
Anton Korobeynikovf700e682008-07-09 13:20:48 +0000231}
232
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000233unsigned
234X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
235 bool Global) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000236 CodeModel::Model CM = TM.getCodeModel();
237 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000238
Dan Gohmana004d7b2008-11-03 18:22:42 +0000239 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000240 unsigned Format = 0;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000241
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000242 if (!is64Bit)
243 // 32 bit targets always encode pointers as 4 bytes
244 Format = DW_EH_PE_sdata4;
245 else {
246 // 64 bit targets encode pointers in 4 bytes iff:
247 // - code model is small OR
248 // - code model is medium and we're emitting externally visible symbols
249 // or any code symbols
250 if (CM == CodeModel::Small ||
251 (CM == CodeModel::Medium && (Global ||
252 Reason != DwarfEncoding::Data)))
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000253 Format = DW_EH_PE_sdata4;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000254 else
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000255 Format = DW_EH_PE_sdata8;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000256 }
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000257
Anton Korobeynikovcd2a4e52008-07-09 13:21:08 +0000258 if (Global)
259 Format |= DW_EH_PE_indirect;
260
261 return (Format | DW_EH_PE_pcrel);
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000262 }
Chris Lattnerb8245582009-07-24 04:49:34 +0000263
264 if (is64Bit &&
265 (CM == CodeModel::Small ||
266 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
267 return DW_EH_PE_udata4;
268 return DW_EH_PE_absptr;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000269}
270
Chris Lattnerb8245582009-07-24 04:49:34 +0000271const char *X86COFFTargetAsmInfo::
Chris Lattnercc195212009-07-25 23:21:55 +0000272getSectionPrefixForUniqueGlobal(SectionKind Kind) const {
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000273 if (Kind.isText())
Chris Lattnercc195212009-07-25 23:21:55 +0000274 return ".text$linkonce";
Chris Lattner1c379242009-07-26 06:48:26 +0000275 if (Kind.isWriteable())
Chris Lattnercc195212009-07-25 23:21:55 +0000276 return ".data$linkonce";
277 return ".rdata$linkonce";
Anton Korobeynikov3819e2d2008-07-09 13:19:38 +0000278}
279
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000280
Chris Lattner94d8a762009-07-26 07:33:58 +0000281void X86COFFTargetAsmInfo::getSectionFlags(unsigned Flags,
282 SmallVectorImpl<char> &Str) const {
283 // FIXME: Inefficient.
284 std::string Res = ",\"";
285 if (Flags & SectionFlags::Code)
286 Res += 'x';
287 if (Flags & SectionFlags::Writable)
288 Res += 'w';
289 Res += "\"";
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000290
Chris Lattner94d8a762009-07-26 07:33:58 +0000291 Str.append(Res.begin(), Res.end());
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000292}
293
294X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000295 X86GenericTargetAsmInfo(TM) {
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000296 GlobalPrefix = "_";
297 CommentString = ";";
298
Eli Friedman378ea832009-06-19 04:48:38 +0000299 InlineAsmStart = "; InlineAsm Start";
300 InlineAsmEnd = "; InlineAsm End";
301
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000302 PrivateGlobalPrefix = "$";
Eli Friedman378ea832009-06-19 04:48:38 +0000303 AlignDirective = "\tALIGN\t";
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000304 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;
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000313 HasSingleParameterDotFile = false;
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000314
Eli Friedman378ea832009-06-19 04:48:38 +0000315 AlignmentIsInBytes = true;
316
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000317 TextSection = getUnnamedSection("_text", SectionFlags::Code);
Chris Lattner4c530ee2009-07-25 18:57:34 +0000318 DataSection = getUnnamedSection("_data", SectionFlags::Writable);
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000319
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000320 JumpTableDataSection = NULL;
321 SwitchToSectionDirective = "";
Eli Friedman378ea832009-06-19 04:48:38 +0000322 TextSectionStartSuffix = "\tSEGMENT PARA 'CODE'";
323 DataSectionStartSuffix = "\tSEGMENT PARA 'DATA'";
Anton Korobeynikov49881c52008-07-09 13:21:49 +0000324 SectionEndDirectiveSuffix = "\tends\n";
325}
Anton Korobeynikov45d1b622008-10-05 08:53:29 +0000326
Anton Korobeynikov45d1b622008-10-05 08:53:29 +0000327// Instantiate default implementation.
328TEMPLATE_INSTANTIATION(class X86TargetAsmInfo<TargetAsmInfo>);