blob: 42e63ecba3495c7cd75e0da3d2620adc61d129df [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;
50 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000051 if (!Subtarget->is64Bit())
52 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000053 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
54 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner42ca4502007-01-17 17:43:33 +000055 BSSSection = 0; // no BSS section.
56 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Jim Laskey8e8de8f2006-09-07 22:05:02 +000057 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000058 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000059 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000060 FourByteConstantSection = "\t.literal4\n";
61 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000062 if (Subtarget->is64Bit())
63 SixteenByteConstantSection = "\t.literal16\n";
Evan Chengf0b5d562007-03-08 01:07:07 +000064 ReadOnlySection = "\t.const\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000065 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen4af34942008-01-15 23:24:56 +000066 SwitchToSectionDirective = "\t.section ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000067 COMMDirectiveTakesAlignment = false;
68 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000069 if (TM.getRelocationModel() == Reloc::Static) {
70 StaticCtorsSection = ".constructor";
71 StaticDtorsSection = ".destructor";
72 } else {
73 StaticCtorsSection = ".mod_init_func";
74 StaticDtorsSection = ".mod_term_func";
75 }
Dale Johannesen4af34942008-01-15 23:24:56 +000076 if (Subtarget->is64Bit()) {
77 PersonalityPrefix = "";
78 PersonalitySuffix = "+4@GOTPCREL";
79 } else {
80 PersonalityPrefix = "L";
81 PersonalitySuffix = "$non_lazy_ptr";
82 }
Bill Wendlingef4a6612007-09-11 17:20:55 +000083 NeedsIndirectEncoding = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000084 InlineAsmStart = "# InlineAsm Start";
85 InlineAsmEnd = "# InlineAsm End";
86 SetDirective = "\t.set";
Bill Wendlingadbda022007-08-22 18:44:05 +000087 PCSymbol = ".";
Chris Lattnera53115b2006-09-26 03:39:53 +000088 UsedDirective = "\t.no_dead_strip\t";
Dale Johannesen038129d2008-01-10 02:03:30 +000089 WeakDefDirective = "\t.weak_definition ";
90 WeakRefDirective = "\t.weak_reference ";
Dale Johannesenc7406ae2008-01-11 00:54:37 +000091 HiddenDirective = "\t.private_extern ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000092
Chris Lattnerb266ccd2007-01-18 01:15:58 +000093 // In non-PIC modes, emit a special label before jump tables so that the
94 // linker can perform more accurate dead code stripping.
95 if (TM.getRelocationModel() != Reloc::PIC_) {
96 // Emit a local label that is preserved until the linker runs.
97 JumpTableSpecialLabelPrefix = "l";
98 }
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +000099
100 SupportsDebugInformation = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000101 NeedsSet = true;
102 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
103 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
104 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
105 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
106 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
107 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
108 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
109 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
110 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
111 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
112 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000113
114 // Exceptions handling
Dale Johannesen4af34942008-01-15 23:24:56 +0000115 SupportsExceptionHandling = true;
Dale Johannesen038129d2008-01-10 02:03:30 +0000116 GlobalEHDirective = "\t.globl\t";
117 SupportsWeakOmittedEHFrame = false;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000118 AbsoluteEHSectionOffsets = false;
Dale Johannesen4af34942008-01-15 23:24:56 +0000119 if (Subtarget->is64Bit())
120 ShortenEHDataOn64Bit = true;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000121 DwarfEHFrameSection =
122 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
123 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000124 break;
Reid Spencer02b85112006-10-30 22:32:30 +0000125
126 case X86Subtarget::isELF:
Dan Gohman6445f612007-06-27 15:09:47 +0000127 ReadOnlySection = "\t.section\t.rodata";
Dan Gohman6f858e22007-05-03 16:38:57 +0000128 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
129 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000130 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Dan Gohman6f858e22007-05-03 16:38:57 +0000131 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000132 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000133 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000134 SetDirective = "\t.set\t";
Anton Korobeynikovedb4a6b2007-05-01 10:19:31 +0000135 PCSymbol = ".";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000136
137 // Set up DWARF directives
138 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000139
140 // Debug Information
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000141 AbsoluteDebugSectionOffsets = true;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000142 SupportsDebugInformation = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000143 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
144 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
145 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
146 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
147 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
148 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
149 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
150 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
151 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
152 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
153 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000154
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000155 // Exceptions handling
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000156 if (!Subtarget->is64Bit())
157 SupportsExceptionHandling = true;
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000158 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov71144d82007-05-01 10:16:06 +0000159 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
160 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
Reid Spencer02b85112006-10-30 22:32:30 +0000161 break;
162
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000163 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000164 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000165 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000166 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000167 COMMDirectiveTakesAlignment = false;
168 HasDotTypeDotSizeDirective = false;
169 StaticCtorsSection = "\t.section .ctors,\"aw\"";
170 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000171 HiddenDirective = NULL;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000172 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
173 WeakRefDirective = "\t.weak\t";
174 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000175
176 // Set up DWARF directives
177 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000178 AbsoluteDebugSectionOffsets = true;
179 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000180 SupportsDebugInformation = true;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000181 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000182 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
183 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
184 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
185 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
186 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
187 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
188 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
189 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
190 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
191 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
192 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
193 break;
Bill Wendling11502862007-01-16 04:13:03 +0000194
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000195 case X86Subtarget::isWindows:
196 GlobalPrefix = "_";
197 HasDotTypeDotSizeDirective = false;
198 break;
Bill Wendling11502862007-01-16 04:13:03 +0000199
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000200 default: break;
201 }
202
203 if (Subtarget->isFlavorIntel()) {
204 GlobalPrefix = "_";
205 CommentString = ";";
206
207 PrivateGlobalPrefix = "$";
208 AlignDirective = "\talign\t";
209 ZeroDirective = "\tdb\t";
210 ZeroDirectiveSuffix = " dup(0)";
211 AsciiDirective = "\tdb\t";
212 AscizDirective = 0;
213 Data8bitsDirective = "\tdb\t";
214 Data16bitsDirective = "\tdw\t";
215 Data32bitsDirective = "\tdd\t";
216 Data64bitsDirective = "\tdq\t";
217 HasDotTypeDotSizeDirective = false;
218
219 TextSection = "_text";
220 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000221 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000222 SwitchToSectionDirective = "";
223 TextSectionStartSuffix = "\tsegment 'CODE'";
224 DataSectionStartSuffix = "\tsegment 'DATA'";
225 SectionEndDirectiveSuffix = "\tends\n";
226 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000227
228 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000229}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000230
Chris Lattner625bd052006-11-29 01:14:06 +0000231bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
232 // FIXME: this should verify that we are targetting a 486 or better. If not,
233 // we will turn this bswap into something that will be lowered to logical ops
234 // instead of emitting the bswap asm. For now, we don't support 486 or lower
235 // so don't worry about this.
236
237 // Verify this is a simple bswap.
238 if (CI->getNumOperands() != 2 ||
239 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000240 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000241 return false;
242
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000243 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
244 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000245 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000246
Chris Lattner625bd052006-11-29 01:14:06 +0000247 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +0000248 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000249 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +0000250 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Chris Lattner625bd052006-11-29 01:14:06 +0000251
252 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000253 Op = new CallInst(Int, Op, CI->getName(), CI);
254
Chris Lattner625bd052006-11-29 01:14:06 +0000255 CI->replaceAllUsesWith(Op);
256 CI->eraseFromParent();
257 return true;
258}
259
260
261bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
262 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000263 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000264
265 std::string AsmStr = IA->getAsmString();
266
267 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
268 std::vector<std::string> AsmPieces;
269 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
270
271 switch (AsmPieces.size()) {
272 default: return false;
273 case 1:
274 AsmStr = AsmPieces[0];
275 AsmPieces.clear();
276 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
277
Chris Lattner5d521352006-11-29 01:48:01 +0000278 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000279 if (AsmPieces.size() == 2 &&
280 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
281 // No need to check constraints, nothing other than the equivalent of
282 // "=r,0" would be valid here.
283 return LowerToBSwap(CI);
284 }
285 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000286 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000287 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000288 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
289 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
290 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
291 std::vector<std::string> Words;
292 SplitString(AsmPieces[0], Words, " \t");
293 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
294 Words.clear();
295 SplitString(AsmPieces[1], Words, " \t");
296 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
297 Words.clear();
298 SplitString(AsmPieces[2], Words, " \t,");
299 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
300 Words[2] == "%edx") {
301 return LowerToBSwap(CI);
302 }
303 }
304 }
305 }
306 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000307 }
308 return false;
309}
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000310
311/// PreferredEHDataFormat - This hook allows the target to select data
312/// format used for encoding pointers in exception handling data. Reason is
313/// 0 for data, 1 for code labels, 2 for function pointers. Global is true
314/// if the symbol can be relocated.
315unsigned X86TargetAsmInfo::PreferredEHDataFormat(unsigned Reason,
316 bool Global) const {
317 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
318
319 switch (Subtarget->TargetType) {
320 case X86Subtarget::isDarwin:
321 if (Reason == 2 && Global)
322 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
323 else if (Reason == 1 || !Global)
324 return DW_EH_PE_pcrel;
325 else
326 return DW_EH_PE_absptr;
327
328 case X86Subtarget::isELF:
329 case X86Subtarget::isCygwin:
330 case X86Subtarget::isMingw: {
331 CodeModel::Model CM = X86TM->getCodeModel();
332
333 if (X86TM->getRelocationModel() == Reloc::PIC_) {
334 unsigned Format = 0;
335
336 if (!Subtarget->is64Bit())
337 // 32 bit targets always encode pointers as 4 bytes
338 Format = DW_EH_PE_sdata4;
339 else {
340 // 64 bit targets encode pointers in 4 bytes iff:
341 // - code model is small OR
342 // - code model is medium and we're emitting externally visible symbols or
343 // any code symbols
344 if (CM == CodeModel::Small ||
345 (CM == CodeModel::Medium && (Global || Reason)))
346 Format = DW_EH_PE_sdata4;
347 else
348 Format = DW_EH_PE_sdata8;
349 }
350
351 if (Global)
352 Format |= DW_EH_PE_indirect;
353
354 return (Format | DW_EH_PE_pcrel);
355 } else {
356 if (Subtarget->is64Bit() &&
357 (CM == CodeModel::Small ||
358 (CM == CodeModel::Medium && Reason)))
359 return DW_EH_PE_udata4;
360 else
361 return DW_EH_PE_absptr;
362 }
363 }
364
365 default:
366 return TargetAsmInfo::PreferredEHDataFormat(Reason, Global);
367 }
368}
369