blob: abb05260cbac6e0243158ada3d1ba0495b744868 [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"
24
Jim Laskey8e8de8f2006-09-07 22:05:02 +000025using namespace llvm;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000026using namespace llvm::dwarf;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000027
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000028const char *const llvm::x86_asm_table[] = {
29 "{si}", "S",
30 "{di}", "D",
31 "{ax}", "a",
32 "{cx}", "c",
33 "{memory}", "memory",
34 "{flags}", "",
35 "{dirflag}", "",
36 "{fpsr}", "",
37 "{cc}", "cc",
38 0,0};
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000039
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000040X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000041 X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000042 const X86Subtarget* Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikov16b7f512008-08-08 18:25:52 +000043 bool is64Bit = Subtarget->is64Bit();
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000044
45 AlignmentIsInBytes = false;
46 TextAlignFillValue = 0x90;
47 GlobalPrefix = "_";
48 if (!is64Bit)
49 Data64bitsDirective = 0; // we can't emit a 64-bit unit
50 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
51 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Dale Johannesenb2dfb892008-09-09 01:21:22 +000052 LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000053 BSSSection = 0; // no BSS section.
54 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Dan Gohman8f092252008-11-03 18:22:42 +000055 if (TM.getRelocationModel() != Reloc::Static)
Anton Korobeynikov7705ea32008-07-09 21:54:26 +000056 ConstantPoolSection = "\t.const_data";
57 else
58 ConstantPoolSection = "\t.const\n";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000059 JumpTableDataSection = "\t.const\n";
60 CStringSection = "\t.cstring";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000061 // FIXME: Why don't always use this section?
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +000062 if (is64Bit) {
Anton Korobeynikov64818732008-09-24 22:18:54 +000063 SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
64 SectionFlags::Mergeable);
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +000065 }
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000066 LCOMMDirective = "\t.lcomm\t";
67 SwitchToSectionDirective = "\t.section ";
68 StringConstantPrefix = "\1LC";
Anton Korobeynikov16b7f512008-08-08 18:25:52 +000069 // Leopard and above support aligned common symbols.
70 COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000071 HasDotTypeDotSizeDirective = false;
72 if (TM.getRelocationModel() == Reloc::Static) {
73 StaticCtorsSection = ".constructor";
74 StaticDtorsSection = ".destructor";
75 } else {
76 StaticCtorsSection = ".mod_init_func";
77 StaticDtorsSection = ".mod_term_func";
78 }
79 if (is64Bit) {
80 PersonalityPrefix = "";
81 PersonalitySuffix = "+4@GOTPCREL";
82 } else {
83 PersonalityPrefix = "L";
84 PersonalitySuffix = "$non_lazy_ptr";
85 }
86 NeedsIndirectEncoding = true;
87 InlineAsmStart = "## InlineAsm Start";
88 InlineAsmEnd = "## InlineAsm End";
89 CommentString = "##";
90 SetDirective = "\t.set";
91 PCSymbol = ".";
92 UsedDirective = "\t.no_dead_strip\t";
93 WeakDefDirective = "\t.weak_definition ";
94 WeakRefDirective = "\t.weak_reference ";
95 HiddenDirective = "\t.private_extern ";
96 ProtectedDirective = "\t.globl\t";
97
98 // In non-PIC modes, emit a special label before jump tables so that the
99 // linker can perform more accurate dead code stripping.
100 if (TM.getRelocationModel() != Reloc::PIC_) {
101 // Emit a local label that is preserved until the linker runs.
102 JumpTableSpecialLabelPrefix = "l";
103 }
104
105 SupportsDebugInformation = true;
106 NeedsSet = true;
107 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
108 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
109 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
110 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
111 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
112 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
113 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
114 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
115 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
116 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
117 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
118
119 // Exceptions handling
120 SupportsExceptionHandling = true;
121 GlobalEHDirective = "\t.globl\t";
122 SupportsWeakOmittedEHFrame = false;
123 AbsoluteEHSectionOffsets = false;
124 DwarfEHFrameSection =
125 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
126 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
127}
128
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000129unsigned
130X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
131 bool Global) const {
132 if (Reason == DwarfEncoding::Functions && Global)
133 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
134 else if (Reason == DwarfEncoding::CodeLabels || !Global)
135 return DW_EH_PE_pcrel;
136 else
137 return DW_EH_PE_absptr;
138}
139
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000140X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000141 X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000142
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000143 CStringSection = ".rodata.str";
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000144 PrivateGlobalPrefix = ".L";
145 WeakRefDirective = "\t.weak\t";
146 SetDirective = "\t.set\t";
147 PCSymbol = ".";
148
149 // Set up DWARF directives
150 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
151
152 // Debug Information
153 AbsoluteDebugSectionOffsets = true;
154 SupportsDebugInformation = true;
155 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
156 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
157 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
158 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
159 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
160 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
161 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
162 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
163 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
164 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
165 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
166
167 // Exceptions handling
Anton Korobeynikovc4da15a2008-09-08 21:13:08 +0000168 SupportsExceptionHandling = true;
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000169 AbsoluteEHSectionOffsets = false;
170 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
171 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
172
173 // On Linux we must declare when we can use a non-executable stack.
Dan Gohman8f092252008-11-03 18:22:42 +0000174 if (TM.getSubtarget<X86Subtarget>().isLinux())
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000175 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
176}
177
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000178unsigned
179X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
180 bool Global) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000181 CodeModel::Model CM = TM.getCodeModel();
182 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000183
Dan Gohman8f092252008-11-03 18:22:42 +0000184 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000185 unsigned Format = 0;
186
187 if (!is64Bit)
188 // 32 bit targets always encode pointers as 4 bytes
189 Format = DW_EH_PE_sdata4;
190 else {
191 // 64 bit targets encode pointers in 4 bytes iff:
192 // - code model is small OR
193 // - code model is medium and we're emitting externally visible symbols
194 // or any code symbols
195 if (CM == CodeModel::Small ||
196 (CM == CodeModel::Medium && (Global ||
197 Reason != DwarfEncoding::Data)))
198 Format = DW_EH_PE_sdata4;
199 else
200 Format = DW_EH_PE_sdata8;
201 }
202
203 if (Global)
204 Format |= DW_EH_PE_indirect;
205
206 return (Format | DW_EH_PE_pcrel);
207 } else {
208 if (is64Bit &&
209 (CM == CodeModel::Small ||
210 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
211 return DW_EH_PE_udata4;
212 else
213 return DW_EH_PE_absptr;
214 }
215}
216
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000217X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000218 X86GenericTargetAsmInfo(TM) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000219
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +0000220 GlobalPrefix = "_";
221 LCOMMDirective = "\t.lcomm\t";
222 COMMDirectiveTakesAlignment = false;
223 HasDotTypeDotSizeDirective = false;
224 StaticCtorsSection = "\t.section .ctors,\"aw\"";
225 StaticDtorsSection = "\t.section .dtors,\"aw\"";
226 HiddenDirective = NULL;
227 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
228 WeakRefDirective = "\t.weak\t";
229 SetDirective = "\t.set\t";
230
231 // Set up DWARF directives
232 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
233 AbsoluteDebugSectionOffsets = true;
234 AbsoluteEHSectionOffsets = false;
235 SupportsDebugInformation = true;
236 DwarfSectionOffsetDirective = "\t.secrel32\t";
237 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
238 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
239 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
240 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
241 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
242 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
243 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
244 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
245 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
246 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
247 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
248}
249
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000250unsigned
251X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
252 bool Global) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000253 CodeModel::Model CM = TM.getCodeModel();
254 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000255
Dan Gohman8f092252008-11-03 18:22:42 +0000256 if (TM.getRelocationModel() == Reloc::PIC_) {
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000257 unsigned Format = 0;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000258
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000259 if (!is64Bit)
260 // 32 bit targets always encode pointers as 4 bytes
261 Format = DW_EH_PE_sdata4;
262 else {
263 // 64 bit targets encode pointers in 4 bytes iff:
264 // - code model is small OR
265 // - code model is medium and we're emitting externally visible symbols
266 // or any code symbols
267 if (CM == CodeModel::Small ||
268 (CM == CodeModel::Medium && (Global ||
269 Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000270 Format = DW_EH_PE_sdata4;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000271 else
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000272 Format = DW_EH_PE_sdata8;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000273 }
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000274
Anton Korobeynikovb9e58ef2008-07-09 13:21:08 +0000275 if (Global)
276 Format |= DW_EH_PE_indirect;
277
278 return (Format | DW_EH_PE_pcrel);
279 } else {
280 if (is64Bit &&
281 (CM == CodeModel::Small ||
282 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
283 return DW_EH_PE_udata4;
284 else
285 return DW_EH_PE_absptr;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000286 }
287}
288
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000289std::string
290X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
291 SectionKind::Kind kind) const {
292 switch (kind) {
293 case SectionKind::Text:
294 return ".text$linkonce" + GV->getName();
295 case SectionKind::Data:
296 case SectionKind::BSS:
297 case SectionKind::ThreadData:
298 case SectionKind::ThreadBSS:
299 return ".data$linkonce" + GV->getName();
300 case SectionKind::ROData:
301 case SectionKind::RODataMergeConst:
302 case SectionKind::RODataMergeStr:
303 return ".rdata$linkonce" + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000304 default:
Anton Korobeynikovb9a02fc2008-07-09 13:21:29 +0000305 assert(0 && "Unknown section kind");
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000306 }
307}
308
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000309std::string X86COFFTargetAsmInfo::printSectionFlags(unsigned flags) const {
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000310 std::string Flags = ",\"";
311
312 if (flags & SectionFlags::Code)
313 Flags += 'x';
314 if (flags & SectionFlags::Writeable)
315 Flags += 'w';
316
317 Flags += "\"";
318
319 return Flags;
320}
321
322X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000323 X86GenericTargetAsmInfo(TM) {
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000324 GlobalPrefix = "_";
325 CommentString = ";";
326
327 PrivateGlobalPrefix = "$";
328 AlignDirective = "\talign\t";
329 ZeroDirective = "\tdb\t";
330 ZeroDirectiveSuffix = " dup(0)";
331 AsciiDirective = "\tdb\t";
332 AscizDirective = 0;
333 Data8bitsDirective = "\tdb\t";
334 Data16bitsDirective = "\tdw\t";
335 Data32bitsDirective = "\tdd\t";
336 Data64bitsDirective = "\tdq\t";
337 HasDotTypeDotSizeDirective = false;
338
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000339 TextSection = getUnnamedSection("_text", SectionFlags::Code);
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000340 DataSection = getUnnamedSection("_data", SectionFlags::Writeable);
341
Anton Korobeynikovf447e3d2008-07-09 13:21:49 +0000342 JumpTableDataSection = NULL;
343 SwitchToSectionDirective = "";
344 TextSectionStartSuffix = "\tsegment 'CODE'";
345 DataSectionStartSuffix = "\tsegment 'DATA'";
346 SectionEndDirectiveSuffix = "\tends\n";
347}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000348
349template <class BaseTAI>
350bool X86TargetAsmInfo<BaseTAI>::LowerToBSwap(CallInst *CI) const {
351 // FIXME: this should verify that we are targetting a 486 or better. If not,
352 // we will turn this bswap into something that will be lowered to logical ops
353 // instead of emitting the bswap asm. For now, we don't support 486 or lower
354 // so don't worry about this.
355
356 // Verify this is a simple bswap.
357 if (CI->getNumOperands() != 2 ||
358 CI->getType() != CI->getOperand(1)->getType() ||
359 !CI->getType()->isInteger())
360 return false;
361
362 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
363 if (!Ty || Ty->getBitWidth() % 16 != 0)
364 return false;
365
366 // Okay, we can do this xform, do so now.
367 const Type *Tys[] = { Ty };
368 Module *M = CI->getParent()->getParent()->getParent();
369 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
370
371 Value *Op = CI->getOperand(1);
372 Op = CallInst::Create(Int, Op, CI->getName(), CI);
373
374 CI->replaceAllUsesWith(Op);
375 CI->eraseFromParent();
376 return true;
377}
378
379template <class BaseTAI>
380bool X86TargetAsmInfo<BaseTAI>::ExpandInlineAsm(CallInst *CI) const {
381 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
382 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
383
384 std::string AsmStr = IA->getAsmString();
385
386 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
387 std::vector<std::string> AsmPieces;
388 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
389
390 switch (AsmPieces.size()) {
391 default: return false;
392 case 1:
393 AsmStr = AsmPieces[0];
394 AsmPieces.clear();
395 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
396
397 // bswap $0
398 if (AsmPieces.size() == 2 &&
399 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
400 // No need to check constraints, nothing other than the equivalent of
401 // "=r,0" would be valid here.
402 return LowerToBSwap(CI);
403 }
404 break;
405 case 3:
406 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
407 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
408 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
409 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
410 std::vector<std::string> Words;
411 SplitString(AsmPieces[0], Words, " \t");
412 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
413 Words.clear();
414 SplitString(AsmPieces[1], Words, " \t");
415 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
416 Words.clear();
417 SplitString(AsmPieces[2], Words, " \t,");
418 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
419 Words[2] == "%edx") {
420 return LowerToBSwap(CI);
421 }
422 }
423 }
424 }
425 break;
426 }
427 return false;
428}
429
430// Instantiate default implementation.
431TEMPLATE_INSTANTIATION(class X86TargetAsmInfo<TargetAsmInfo>);