blob: acc13ba1070091ee26cbe0eba4648bed85c70f92 [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
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000028static const char* x86_asm_table[] = {"{si}", "S",
29 "{di}", "D",
30 "{ax}", "a",
31 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000032 "{memory}", "memory",
33 "{flags}", "",
34 "{dirflag}", "",
35 "{fpsr}", "",
36 "{cc}", "cc",
37 0,0};
38
Jim Laskey8e8de8f2006-09-07 22:05:02 +000039X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
40 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000041 X86TM = &TM;
42
Chris Lattnerafbfded2006-10-05 02:43:52 +000043 // FIXME - Should be simplified.
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000044
45 AsmTransCBE = x86_asm_table;
46
Jim Laskey8e8de8f2006-09-07 22:05:02 +000047 switch (Subtarget->TargetType) {
48 case X86Subtarget::isDarwin:
49 AlignmentIsInBytes = false;
Evan Chengfb8075d2008-02-28 00:43:03 +000050 TextAlignFillValue = 0x90;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000051 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000052 if (!Subtarget->is64Bit())
53 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000054 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
55 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner42ca4502007-01-17 17:43:33 +000056 BSSSection = 0; // no BSS section.
57 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Jim Laskey8e8de8f2006-09-07 22:05:02 +000058 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000059 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000060 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000061 FourByteConstantSection = "\t.literal4\n";
62 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000063 if (Subtarget->is64Bit())
64 SixteenByteConstantSection = "\t.literal16\n";
Evan Chengf0b5d562007-03-08 01:07:07 +000065 ReadOnlySection = "\t.const\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000066 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen4af34942008-01-15 23:24:56 +000067 SwitchToSectionDirective = "\t.section ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000068 COMMDirectiveTakesAlignment = false;
69 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000070 if (TM.getRelocationModel() == Reloc::Static) {
71 StaticCtorsSection = ".constructor";
72 StaticDtorsSection = ".destructor";
73 } else {
74 StaticCtorsSection = ".mod_init_func";
75 StaticDtorsSection = ".mod_term_func";
76 }
Dale Johannesen4af34942008-01-15 23:24:56 +000077 if (Subtarget->is64Bit()) {
78 PersonalityPrefix = "";
79 PersonalitySuffix = "+4@GOTPCREL";
80 } else {
81 PersonalityPrefix = "L";
82 PersonalitySuffix = "$non_lazy_ptr";
83 }
Bill Wendlingef4a6612007-09-11 17:20:55 +000084 NeedsIndirectEncoding = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000085 InlineAsmStart = "# InlineAsm Start";
86 InlineAsmEnd = "# InlineAsm End";
87 SetDirective = "\t.set";
Bill Wendlingadbda022007-08-22 18:44:05 +000088 PCSymbol = ".";
Chris Lattnera53115b2006-09-26 03:39:53 +000089 UsedDirective = "\t.no_dead_strip\t";
Dale Johannesen038129d2008-01-10 02:03:30 +000090 WeakDefDirective = "\t.weak_definition ";
91 WeakRefDirective = "\t.weak_reference ";
Dale Johannesenc7406ae2008-01-11 00:54:37 +000092 HiddenDirective = "\t.private_extern ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000093
Chris Lattnerb266ccd2007-01-18 01:15:58 +000094 // In non-PIC modes, emit a special label before jump tables so that the
95 // linker can perform more accurate dead code stripping.
96 if (TM.getRelocationModel() != Reloc::PIC_) {
97 // Emit a local label that is preserved until the linker runs.
98 JumpTableSpecialLabelPrefix = "l";
99 }
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000100
101 SupportsDebugInformation = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000102 NeedsSet = true;
103 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
104 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
105 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
106 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
107 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
108 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
109 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
110 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
111 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
112 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
113 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000114
115 // Exceptions handling
Dale Johannesen4af34942008-01-15 23:24:56 +0000116 SupportsExceptionHandling = true;
Dale Johannesen038129d2008-01-10 02:03:30 +0000117 GlobalEHDirective = "\t.globl\t";
118 SupportsWeakOmittedEHFrame = false;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000119 AbsoluteEHSectionOffsets = false;
Dale Johannesen4af34942008-01-15 23:24:56 +0000120 if (Subtarget->is64Bit())
121 ShortenEHDataOn64Bit = true;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000122 DwarfEHFrameSection =
123 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
124 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000125 break;
Reid Spencer02b85112006-10-30 22:32:30 +0000126
127 case X86Subtarget::isELF:
Dan Gohman6445f612007-06-27 15:09:47 +0000128 ReadOnlySection = "\t.section\t.rodata";
Dan Gohman6f858e22007-05-03 16:38:57 +0000129 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
130 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000131 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Dan Gohman6f858e22007-05-03 16:38:57 +0000132 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000133 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000134 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000135 SetDirective = "\t.set\t";
Anton Korobeynikovedb4a6b2007-05-01 10:19:31 +0000136 PCSymbol = ".";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000137
138 // Set up DWARF directives
139 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000140
141 // Debug Information
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000142 AbsoluteDebugSectionOffsets = true;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000143 SupportsDebugInformation = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000144 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
145 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
146 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
147 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
148 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
149 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
150 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
151 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
152 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
153 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
154 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000155
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000156 // Exceptions handling
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000157 if (!Subtarget->is64Bit())
158 SupportsExceptionHandling = true;
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000159 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov71144d82007-05-01 10:16:06 +0000160 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
161 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
Reid Spencer02b85112006-10-30 22:32:30 +0000162 break;
163
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000164 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000165 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000166 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000167 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000168 COMMDirectiveTakesAlignment = false;
169 HasDotTypeDotSizeDirective = false;
170 StaticCtorsSection = "\t.section .ctors,\"aw\"";
171 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000172 HiddenDirective = NULL;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000173 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
174 WeakRefDirective = "\t.weak\t";
175 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000176
177 // Set up DWARF directives
178 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000179 AbsoluteDebugSectionOffsets = true;
180 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000181 SupportsDebugInformation = true;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000182 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000183 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
184 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
185 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
186 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
187 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
188 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
189 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
190 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
191 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
192 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
193 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
194 break;
Bill Wendling11502862007-01-16 04:13:03 +0000195
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000196 case X86Subtarget::isWindows:
197 GlobalPrefix = "_";
198 HasDotTypeDotSizeDirective = false;
199 break;
Bill Wendling11502862007-01-16 04:13:03 +0000200
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000201 default: break;
202 }
203
204 if (Subtarget->isFlavorIntel()) {
205 GlobalPrefix = "_";
206 CommentString = ";";
207
208 PrivateGlobalPrefix = "$";
209 AlignDirective = "\talign\t";
210 ZeroDirective = "\tdb\t";
211 ZeroDirectiveSuffix = " dup(0)";
212 AsciiDirective = "\tdb\t";
213 AscizDirective = 0;
214 Data8bitsDirective = "\tdb\t";
215 Data16bitsDirective = "\tdw\t";
216 Data32bitsDirective = "\tdd\t";
217 Data64bitsDirective = "\tdq\t";
218 HasDotTypeDotSizeDirective = false;
219
220 TextSection = "_text";
221 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000222 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000223 SwitchToSectionDirective = "";
224 TextSectionStartSuffix = "\tsegment 'CODE'";
225 DataSectionStartSuffix = "\tsegment 'DATA'";
226 SectionEndDirectiveSuffix = "\tends\n";
227 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000228
229 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000230}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000231
Chris Lattner625bd052006-11-29 01:14:06 +0000232bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
233 // FIXME: this should verify that we are targetting a 486 or better. If not,
234 // we will turn this bswap into something that will be lowered to logical ops
235 // instead of emitting the bswap asm. For now, we don't support 486 or lower
236 // so don't worry about this.
237
238 // Verify this is a simple bswap.
239 if (CI->getNumOperands() != 2 ||
240 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000241 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000242 return false;
243
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000244 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
245 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000246 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000247
Chris Lattner625bd052006-11-29 01:14:06 +0000248 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +0000249 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000250 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +0000251 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Chris Lattner625bd052006-11-29 01:14:06 +0000252
253 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000254 Op = new CallInst(Int, Op, CI->getName(), CI);
255
Chris Lattner625bd052006-11-29 01:14:06 +0000256 CI->replaceAllUsesWith(Op);
257 CI->eraseFromParent();
258 return true;
259}
260
261
262bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
263 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000264 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000265
266 std::string AsmStr = IA->getAsmString();
267
268 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
269 std::vector<std::string> AsmPieces;
270 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
271
272 switch (AsmPieces.size()) {
273 default: return false;
274 case 1:
275 AsmStr = AsmPieces[0];
276 AsmPieces.clear();
277 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
278
Chris Lattner5d521352006-11-29 01:48:01 +0000279 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000280 if (AsmPieces.size() == 2 &&
281 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
282 // No need to check constraints, nothing other than the equivalent of
283 // "=r,0" would be valid here.
284 return LowerToBSwap(CI);
285 }
286 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000287 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000288 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000289 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
290 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
291 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
292 std::vector<std::string> Words;
293 SplitString(AsmPieces[0], Words, " \t");
294 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
295 Words.clear();
296 SplitString(AsmPieces[1], Words, " \t");
297 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
298 Words.clear();
299 SplitString(AsmPieces[2], Words, " \t,");
300 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
301 Words[2] == "%edx") {
302 return LowerToBSwap(CI);
303 }
304 }
305 }
306 }
307 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000308 }
309 return false;
310}
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000311
312/// PreferredEHDataFormat - This hook allows the target to select data
313/// format used for encoding pointers in exception handling data. Reason is
314/// 0 for data, 1 for code labels, 2 for function pointers. Global is true
315/// if the symbol can be relocated.
316unsigned X86TargetAsmInfo::PreferredEHDataFormat(unsigned Reason,
317 bool Global) const {
318 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
319
320 switch (Subtarget->TargetType) {
321 case X86Subtarget::isDarwin:
322 if (Reason == 2 && Global)
323 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
324 else if (Reason == 1 || !Global)
325 return DW_EH_PE_pcrel;
326 else
327 return DW_EH_PE_absptr;
328
329 case X86Subtarget::isELF:
330 case X86Subtarget::isCygwin:
331 case X86Subtarget::isMingw: {
332 CodeModel::Model CM = X86TM->getCodeModel();
333
334 if (X86TM->getRelocationModel() == Reloc::PIC_) {
335 unsigned Format = 0;
336
337 if (!Subtarget->is64Bit())
338 // 32 bit targets always encode pointers as 4 bytes
339 Format = DW_EH_PE_sdata4;
340 else {
341 // 64 bit targets encode pointers in 4 bytes iff:
342 // - code model is small OR
343 // - code model is medium and we're emitting externally visible symbols or
344 // any code symbols
345 if (CM == CodeModel::Small ||
346 (CM == CodeModel::Medium && (Global || Reason)))
347 Format = DW_EH_PE_sdata4;
348 else
349 Format = DW_EH_PE_sdata8;
350 }
351
352 if (Global)
353 Format |= DW_EH_PE_indirect;
354
355 return (Format | DW_EH_PE_pcrel);
356 } else {
357 if (Subtarget->is64Bit() &&
358 (CM == CodeModel::Small ||
359 (CM == CodeModel::Medium && Reason)))
360 return DW_EH_PE_udata4;
361 else
362 return DW_EH_PE_absptr;
363 }
364 }
365
366 default:
367 return TargetAsmInfo::PreferredEHDataFormat(Reason, Global);
368 }
369}
370