blob: 293836a06e8dcf1e2cf5f4faf10b065b30558be1 [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"
Jim Laskey8e8de8f2006-09-07 22:05:02 +000023using namespace llvm;
24
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000025static const char* x86_asm_table[] = {"{si}", "S",
26 "{di}", "D",
27 "{ax}", "a",
28 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000029 "{memory}", "memory",
30 "{flags}", "",
31 "{dirflag}", "",
32 "{fpsr}", "",
33 "{cc}", "cc",
34 0,0};
35
Jim Laskey8e8de8f2006-09-07 22:05:02 +000036X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
37 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
38
Chris Lattnerafbfded2006-10-05 02:43:52 +000039 // FIXME - Should be simplified.
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000040
41 AsmTransCBE = x86_asm_table;
42
Jim Laskey8e8de8f2006-09-07 22:05:02 +000043 switch (Subtarget->TargetType) {
44 case X86Subtarget::isDarwin:
45 AlignmentIsInBytes = false;
46 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000047 if (!Subtarget->is64Bit())
48 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000049 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
50 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner42ca4502007-01-17 17:43:33 +000051 BSSSection = 0; // no BSS section.
52 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Jim Laskey8e8de8f2006-09-07 22:05:02 +000053 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000054 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000055 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000056 FourByteConstantSection = "\t.literal4\n";
57 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000058 if (Subtarget->is64Bit())
59 SixteenByteConstantSection = "\t.literal16\n";
Evan Chengf0b5d562007-03-08 01:07:07 +000060 ReadOnlySection = "\t.const\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000061 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen4af34942008-01-15 23:24:56 +000062 SwitchToSectionDirective = "\t.section ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000063 COMMDirectiveTakesAlignment = false;
64 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000065 if (TM.getRelocationModel() == Reloc::Static) {
66 StaticCtorsSection = ".constructor";
67 StaticDtorsSection = ".destructor";
68 } else {
69 StaticCtorsSection = ".mod_init_func";
70 StaticDtorsSection = ".mod_term_func";
71 }
Dale Johannesen4af34942008-01-15 23:24:56 +000072 if (Subtarget->is64Bit()) {
73 PersonalityPrefix = "";
74 PersonalitySuffix = "+4@GOTPCREL";
75 } else {
76 PersonalityPrefix = "L";
77 PersonalitySuffix = "$non_lazy_ptr";
78 }
Bill Wendlingef4a6612007-09-11 17:20:55 +000079 NeedsIndirectEncoding = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000080 InlineAsmStart = "# InlineAsm Start";
81 InlineAsmEnd = "# InlineAsm End";
82 SetDirective = "\t.set";
Bill Wendlingadbda022007-08-22 18:44:05 +000083 PCSymbol = ".";
Chris Lattnera53115b2006-09-26 03:39:53 +000084 UsedDirective = "\t.no_dead_strip\t";
Dale Johannesen038129d2008-01-10 02:03:30 +000085 WeakDefDirective = "\t.weak_definition ";
86 WeakRefDirective = "\t.weak_reference ";
Dale Johannesenc7406ae2008-01-11 00:54:37 +000087 HiddenDirective = "\t.private_extern ";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000088
Chris Lattnerb266ccd2007-01-18 01:15:58 +000089 // In non-PIC modes, emit a special label before jump tables so that the
90 // linker can perform more accurate dead code stripping.
91 if (TM.getRelocationModel() != Reloc::PIC_) {
92 // Emit a local label that is preserved until the linker runs.
93 JumpTableSpecialLabelPrefix = "l";
94 }
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +000095
96 SupportsDebugInformation = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000097 NeedsSet = true;
98 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
99 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
100 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
101 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
102 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
103 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
104 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
105 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
106 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
107 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
108 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000109
110 // Exceptions handling
Dale Johannesen4af34942008-01-15 23:24:56 +0000111 SupportsExceptionHandling = true;
Dale Johannesen038129d2008-01-10 02:03:30 +0000112 GlobalEHDirective = "\t.globl\t";
113 SupportsWeakOmittedEHFrame = false;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000114 AbsoluteEHSectionOffsets = false;
Dale Johannesen4af34942008-01-15 23:24:56 +0000115 if (Subtarget->is64Bit())
116 ShortenEHDataOn64Bit = true;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000117 DwarfEHFrameSection =
118 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
119 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000120 break;
Reid Spencer02b85112006-10-30 22:32:30 +0000121
122 case X86Subtarget::isELF:
Dan Gohman6445f612007-06-27 15:09:47 +0000123 ReadOnlySection = "\t.section\t.rodata";
Dan Gohman6f858e22007-05-03 16:38:57 +0000124 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
125 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000126 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Dan Gohman6f858e22007-05-03 16:38:57 +0000127 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000128 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000129 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000130 SetDirective = "\t.set\t";
Anton Korobeynikovedb4a6b2007-05-01 10:19:31 +0000131 PCSymbol = ".";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000132
133 // Set up DWARF directives
134 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000135
136 // Debug Information
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000137 AbsoluteDebugSectionOffsets = true;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000138 SupportsDebugInformation = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000139 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
140 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
141 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
142 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
143 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
144 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
145 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
146 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
147 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
148 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
149 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000150
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000151 // Exceptions handling
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000152 if (!Subtarget->is64Bit())
153 SupportsExceptionHandling = true;
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000154 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov71144d82007-05-01 10:16:06 +0000155 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
156 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
Reid Spencer02b85112006-10-30 22:32:30 +0000157 break;
158
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000159 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000160 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000161 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000162 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000163 COMMDirectiveTakesAlignment = false;
164 HasDotTypeDotSizeDirective = false;
165 StaticCtorsSection = "\t.section .ctors,\"aw\"";
166 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000167 HiddenDirective = NULL;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000168 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
169 WeakRefDirective = "\t.weak\t";
170 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000171
172 // Set up DWARF directives
173 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000174 AbsoluteDebugSectionOffsets = true;
175 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000176 SupportsDebugInformation = true;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000177 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000178 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
179 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
180 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
181 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
182 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
183 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
184 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
185 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
186 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
187 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
188 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
189 break;
Bill Wendling11502862007-01-16 04:13:03 +0000190
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000191 case X86Subtarget::isWindows:
192 GlobalPrefix = "_";
193 HasDotTypeDotSizeDirective = false;
194 break;
Bill Wendling11502862007-01-16 04:13:03 +0000195
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000196 default: break;
197 }
198
199 if (Subtarget->isFlavorIntel()) {
200 GlobalPrefix = "_";
201 CommentString = ";";
202
203 PrivateGlobalPrefix = "$";
204 AlignDirective = "\talign\t";
205 ZeroDirective = "\tdb\t";
206 ZeroDirectiveSuffix = " dup(0)";
207 AsciiDirective = "\tdb\t";
208 AscizDirective = 0;
209 Data8bitsDirective = "\tdb\t";
210 Data16bitsDirective = "\tdw\t";
211 Data32bitsDirective = "\tdd\t";
212 Data64bitsDirective = "\tdq\t";
213 HasDotTypeDotSizeDirective = false;
214
215 TextSection = "_text";
216 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000217 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000218 SwitchToSectionDirective = "";
219 TextSectionStartSuffix = "\tsegment 'CODE'";
220 DataSectionStartSuffix = "\tsegment 'DATA'";
221 SectionEndDirectiveSuffix = "\tends\n";
222 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000223
224 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000225}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000226
Chris Lattner625bd052006-11-29 01:14:06 +0000227bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
228 // FIXME: this should verify that we are targetting a 486 or better. If not,
229 // we will turn this bswap into something that will be lowered to logical ops
230 // instead of emitting the bswap asm. For now, we don't support 486 or lower
231 // so don't worry about this.
232
233 // Verify this is a simple bswap.
234 if (CI->getNumOperands() != 2 ||
235 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000236 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000237 return false;
238
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000239 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
240 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000241 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000242
Chris Lattner625bd052006-11-29 01:14:06 +0000243 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +0000244 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000245 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +0000246 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Chris Lattner625bd052006-11-29 01:14:06 +0000247
248 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000249 Op = new CallInst(Int, Op, CI->getName(), CI);
250
Chris Lattner625bd052006-11-29 01:14:06 +0000251 CI->replaceAllUsesWith(Op);
252 CI->eraseFromParent();
253 return true;
254}
255
256
257bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
258 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000259 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000260
261 std::string AsmStr = IA->getAsmString();
262
263 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
264 std::vector<std::string> AsmPieces;
265 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
266
267 switch (AsmPieces.size()) {
268 default: return false;
269 case 1:
270 AsmStr = AsmPieces[0];
271 AsmPieces.clear();
272 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
273
Chris Lattner5d521352006-11-29 01:48:01 +0000274 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000275 if (AsmPieces.size() == 2 &&
276 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
277 // No need to check constraints, nothing other than the equivalent of
278 // "=r,0" would be valid here.
279 return LowerToBSwap(CI);
280 }
281 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000282 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000283 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000284 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
285 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
286 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
287 std::vector<std::string> Words;
288 SplitString(AsmPieces[0], Words, " \t");
289 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
290 Words.clear();
291 SplitString(AsmPieces[1], Words, " \t");
292 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
293 Words.clear();
294 SplitString(AsmPieces[2], Words, " \t,");
295 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
296 Words[2] == "%edx") {
297 return LowerToBSwap(CI);
298 }
299 }
300 }
301 }
302 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000303 }
304 return false;
305}