blob: c2ac9f2ff4cd686f7708058029b204d4befa9c4b [file] [log] [blame]
Jim Laskey8e8de8f2006-09-07 22:05:02 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey8e8de8f2006-09-07 22:05:02 +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"
Reid Spencer7aa8a452007-01-12 23:22:14 +000017#include "llvm/DerivedTypes.h"
Chris Lattner625bd052006-11-29 01:14:06 +000018#include "llvm/InlineAsm.h"
19#include "llvm/Instructions.h"
Chris Lattner3e9f1d02007-04-01 20:49:36 +000020#include "llvm/Intrinsics.h"
Chris Lattner625bd052006-11-29 01:14:06 +000021#include "llvm/Module.h"
22#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000023#include "llvm/Support/Dwarf.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000025
Jim Laskey8e8de8f2006-09-07 22:05:02 +000026using namespace llvm;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000027using namespace llvm::dwarf;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000028
Anton Korobeynikov32b952a2008-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};
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000040
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000041X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000042 X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
Bill Wendlingb6be1392009-07-13 18:48:39 +000043 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikov16b7f512008-08-08 18:25:52 +000044 bool is64Bit = Subtarget->is64Bit();
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000045
46 AlignmentIsInBytes = false;
47 TextAlignFillValue = 0x90;
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000048
49
Anton Korobeynikov4468b7a2008-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 Korobeynikov4468b7a2008-07-09 13:20:48 +000053 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Dan Gohman8f092252008-11-03 18:22:42 +000054 if (TM.getRelocationModel() != Reloc::Static)
Anton Korobeynikov7705ea32008-07-09 21:54:26 +000055 ConstantPoolSection = "\t.const_data";
56 else
57 ConstantPoolSection = "\t.const\n";
Chris Lattner4e0f25b2009-06-19 00:08:39 +000058 // FIXME: Why don't we always use this section?
59 if (is64Bit)
Chris Lattnere3466942009-07-27 06:17:14 +000060 SixteenByteConstantSection =
61 getOrCreateSection("\t.literal16\n", true, SectionKind::MergeableConst16);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000062 LCOMMDirective = "\t.lcomm\t";
Bill Wendlingb6be1392009-07-13 18:48:39 +000063
Anton Korobeynikov16b7f512008-08-08 18:25:52 +000064 // Leopard and above support aligned common symbols.
65 COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000066 HasDotTypeDotSizeDirective = false;
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000067
Anton Korobeynikov4468b7a2008-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 Wendlingb6be1392009-07-13 18:48:39 +000075
Anton Korobeynikov4468b7a2008-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 Korobeynikov4468b7a2008-07-09 13:20:48 +000082 ProtectedDirective = "\t.globl\t";
83
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000084 SupportsDebugInformation = true;
Devang Patel0f7fef32009-04-13 17:02:03 +000085 DwarfDebugInlineSection = ".section __DWARF,__debug_inlined,regular,debug";
86 DwarfUsesInlineInfoSection = true;
Anton Korobeynikov4468b7a2008-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 Wendlingdb425492009-07-13 20:27:41 +000095 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000096}
97
Anton Korobeynikovb9e58ef2008-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);
Chris Lattnere3466942009-07-27 06:17:14 +0000103 if (Reason == DwarfEncoding::CodeLabels || !Global)
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000104 return DW_EH_PE_pcrel;
Chris Lattnere3466942009-07-27 06:17:14 +0000105 return DW_EH_PE_absptr;
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000106}
107
Rafael Espindola2f6fea92008-12-19 10:55:56 +0000108const char *
Chris Lattnere3466942009-07-27 06:17:14 +0000109X86DarwinTargetAsmInfo::getEHGlobalPrefix() const {
Rafael Espindola2f6fea92008-12-19 10:55:56 +0000110 const X86Subtarget* Subtarget = &TM.getSubtarget<X86Subtarget>();
111 if (Subtarget->getDarwinVers() > 9)
112 return PrivateGlobalPrefix;
Chris Lattnere3466942009-07-27 06:17:14 +0000113 return "";
Rafael Espindola2f6fea92008-12-19 10:55:56 +0000114}
115
Chris Lattnere3466942009-07-27 06:17:14 +0000116X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM) :
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000117 X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000118
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000119 CStringSection = ".rodata.str";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000120 PrivateGlobalPrefix = ".L";
121 WeakRefDirective = "\t.weak\t";
122 SetDirective = "\t.set\t";
123 PCSymbol = ".";
124
125 // Set up DWARF directives
126 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
127
Chris Lattnere3466942009-07-27 06:17:14 +0000128 BSSSection_ = getOrCreateSection("\t.bss", true, SectionKind::BSS);
Chris Lattner0fcf4dc2009-07-26 19:23:28 +0000129
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000130 // Debug Information
131 AbsoluteDebugSectionOffsets = true;
132 SupportsDebugInformation = true;
133 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
134 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
135 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
136 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
137 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
138 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
139 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
140 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
141 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
142 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
Chris Lattnerb839c3f2009-06-18 23:31:37 +0000143 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000144
145 // Exceptions handling
Anton Korobeynikovc4da15a2008-09-08 21:13:08 +0000146 SupportsExceptionHandling = true;
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000147 AbsoluteEHSectionOffsets = false;
148 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
149 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
150
151 // On Linux we must declare when we can use a non-executable stack.
Dan Gohman8f092252008-11-03 18:22:42 +0000152 if (TM.getSubtarget<X86Subtarget>().isLinux())
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000153 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
154}
155
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000156unsigned
157X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
158 bool Global) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000159 CodeModel::Model CM = TM.getCodeModel();
160 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000161
Dan Gohman8f092252008-11-03 18:22:42 +0000162 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000163 unsigned Format = 0;
164
165 if (!is64Bit)
166 // 32 bit targets always encode pointers as 4 bytes
167 Format = DW_EH_PE_sdata4;
168 else {
169 // 64 bit targets encode pointers in 4 bytes iff:
170 // - code model is small OR
171 // - code model is medium and we're emitting externally visible symbols
172 // or any code symbols
173 if (CM == CodeModel::Small ||
174 (CM == CodeModel::Medium && (Global ||
175 Reason != DwarfEncoding::Data)))
176 Format = DW_EH_PE_sdata4;
177 else
178 Format = DW_EH_PE_sdata8;
179 }
180
181 if (Global)
182 Format |= DW_EH_PE_indirect;
183
184 return (Format | DW_EH_PE_pcrel);
185 } else {
186 if (is64Bit &&
187 (CM == CodeModel::Small ||
188 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
189 return DW_EH_PE_udata4;
190 else
191 return DW_EH_PE_absptr;
192 }
193}
194
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000195X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000196 X86GenericTargetAsmInfo(TM) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000197
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000198 GlobalPrefix = "_";
199 LCOMMDirective = "\t.lcomm\t";
200 COMMDirectiveTakesAlignment = false;
201 HasDotTypeDotSizeDirective = false;
Rafael Espindola952b8392008-12-03 11:01:37 +0000202 HasSingleParameterDotFile = false;
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000203 StaticCtorsSection = "\t.section .ctors,\"aw\"";
204 StaticDtorsSection = "\t.section .dtors,\"aw\"";
205 HiddenDirective = NULL;
206 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
207 WeakRefDirective = "\t.weak\t";
208 SetDirective = "\t.set\t";
209
210 // Set up DWARF directives
211 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
212 AbsoluteDebugSectionOffsets = true;
213 AbsoluteEHSectionOffsets = false;
214 SupportsDebugInformation = true;
215 DwarfSectionOffsetDirective = "\t.secrel32\t";
216 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
217 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
218 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
219 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
220 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
221 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
222 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
223 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
224 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
225 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
Chris Lattnerb839c3f2009-06-18 23:31:37 +0000226 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000227}
228
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000229unsigned
230X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
231 bool Global) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000232 CodeModel::Model CM = TM.getCodeModel();
233 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000234
Dan Gohman8f092252008-11-03 18:22:42 +0000235 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000236 unsigned Format = 0;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000237
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000238 if (!is64Bit)
239 // 32 bit targets always encode pointers as 4 bytes
240 Format = DW_EH_PE_sdata4;
241 else {
242 // 64 bit targets encode pointers in 4 bytes iff:
243 // - code model is small OR
244 // - code model is medium and we're emitting externally visible symbols
245 // or any code symbols
246 if (CM == CodeModel::Small ||
247 (CM == CodeModel::Medium && (Global ||
248 Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000249 Format = DW_EH_PE_sdata4;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000250 else
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000251 Format = DW_EH_PE_sdata8;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000252 }
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000253
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000254 if (Global)
255 Format |= DW_EH_PE_indirect;
256
257 return (Format | DW_EH_PE_pcrel);
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000258 }
Chris Lattner55acc682009-07-24 04:49:34 +0000259
260 if (is64Bit &&
261 (CM == CodeModel::Small ||
262 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
263 return DW_EH_PE_udata4;
264 return DW_EH_PE_absptr;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000265}
266
Chris Lattner55acc682009-07-24 04:49:34 +0000267const char *X86COFFTargetAsmInfo::
Chris Lattner460d51e2009-07-25 23:21:55 +0000268getSectionPrefixForUniqueGlobal(SectionKind Kind) const {
Chris Lattnere346e182009-07-26 05:44:20 +0000269 if (Kind.isText())
Chris Lattner460d51e2009-07-25 23:21:55 +0000270 return ".text$linkonce";
Chris Lattner2ceb60a2009-07-26 06:48:26 +0000271 if (Kind.isWriteable())
Chris Lattner460d51e2009-07-25 23:21:55 +0000272 return ".data$linkonce";
273 return ".rdata$linkonce";
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000274}
275
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000276
Chris Lattner5fe575f2009-07-27 05:32:16 +0000277
278void X86COFFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind,
279 SmallVectorImpl<char> &Str) const {
Chris Lattnerf40761d2009-07-26 07:33:58 +0000280 // FIXME: Inefficient.
281 std::string Res = ",\"";
Chris Lattner5fe575f2009-07-27 05:32:16 +0000282 if (Kind.isText())
Chris Lattnerf40761d2009-07-26 07:33:58 +0000283 Res += 'x';
Chris Lattner5fe575f2009-07-27 05:32:16 +0000284 if (Kind.isWriteable())
Chris Lattnerf40761d2009-07-26 07:33:58 +0000285 Res += 'w';
286 Res += "\"";
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000287
Chris Lattnerf40761d2009-07-26 07:33:58 +0000288 Str.append(Res.begin(), Res.end());
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000289}
290
291X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000292 X86GenericTargetAsmInfo(TM) {
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000293 GlobalPrefix = "_";
294 CommentString = ";";
295
Eli Friedmanaace4b12009-06-19 04:48:38 +0000296 InlineAsmStart = "; InlineAsm Start";
297 InlineAsmEnd = "; InlineAsm End";
298
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000299 PrivateGlobalPrefix = "$";
Eli Friedmanaace4b12009-06-19 04:48:38 +0000300 AlignDirective = "\tALIGN\t";
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000301 ZeroDirective = "\tdb\t";
302 ZeroDirectiveSuffix = " dup(0)";
303 AsciiDirective = "\tdb\t";
304 AscizDirective = 0;
305 Data8bitsDirective = "\tdb\t";
306 Data16bitsDirective = "\tdw\t";
307 Data32bitsDirective = "\tdd\t";
308 Data64bitsDirective = "\tdq\t";
309 HasDotTypeDotSizeDirective = false;
Rafael Espindola952b8392008-12-03 11:01:37 +0000310 HasSingleParameterDotFile = false;
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000311
Eli Friedmanaace4b12009-06-19 04:48:38 +0000312 AlignmentIsInBytes = true;
313
Chris Lattnere3466942009-07-27 06:17:14 +0000314 TextSection = getOrCreateSection("_text", true, SectionKind::Text);
315 DataSection = getOrCreateSection("_data", true, SectionKind::DataRel);
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000316
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000317 JumpTableDataSection = NULL;
318 SwitchToSectionDirective = "";
Eli Friedmanaace4b12009-06-19 04:48:38 +0000319 TextSectionStartSuffix = "\tSEGMENT PARA 'CODE'";
320 DataSectionStartSuffix = "\tSEGMENT PARA 'DATA'";
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000321 SectionEndDirectiveSuffix = "\tends\n";
322}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000323
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000324// Instantiate default implementation.
325TEMPLATE_INSTANTIATION(class X86TargetAsmInfo<TargetAsmInfo>);