blob: 07a1a197f3ad492e0c94c4816d6bda28c2c6e2b4 [file] [log] [blame]
Jim Laskey8e8de8f2006-09-07 22:05:02 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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";
62 COMMDirectiveTakesAlignment = false;
63 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000064 if (TM.getRelocationModel() == Reloc::Static) {
65 StaticCtorsSection = ".constructor";
66 StaticDtorsSection = ".destructor";
67 } else {
68 StaticCtorsSection = ".mod_init_func";
69 StaticDtorsSection = ".mod_term_func";
70 }
Jim Laskey8e8de8f2006-09-07 22:05:02 +000071 InlineAsmStart = "# InlineAsm Start";
72 InlineAsmEnd = "# InlineAsm End";
73 SetDirective = "\t.set";
Bill Wendlingadbda022007-08-22 18:44:05 +000074 PCSymbol = ".";
Chris Lattnera53115b2006-09-26 03:39:53 +000075 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000076 WeakRefDirective = "\t.weak_reference\t";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000077 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000078
Chris Lattnerb266ccd2007-01-18 01:15:58 +000079 // In non-PIC modes, emit a special label before jump tables so that the
80 // linker can perform more accurate dead code stripping.
81 if (TM.getRelocationModel() != Reloc::PIC_) {
82 // Emit a local label that is preserved until the linker runs.
83 JumpTableSpecialLabelPrefix = "l";
84 }
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +000085
86 SupportsDebugInformation = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000087 NeedsSet = true;
88 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
89 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
90 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
91 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
92 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
93 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
94 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
95 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
96 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
97 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
98 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov095546c2007-08-21 00:31:30 +000099
100 // Exceptions handling
101 if (!Subtarget->is64Bit())
102 SupportsExceptionHandling = true;
103 AbsoluteEHSectionOffsets = false;
104 DwarfEHFrameSection =
105 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
106 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000107 break;
Reid Spencer02b85112006-10-30 22:32:30 +0000108
109 case X86Subtarget::isELF:
Dan Gohman6445f612007-06-27 15:09:47 +0000110 ReadOnlySection = "\t.section\t.rodata";
Dan Gohman6f858e22007-05-03 16:38:57 +0000111 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
112 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000113 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Dan Gohman6f858e22007-05-03 16:38:57 +0000114 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000115 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000116 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000117 SetDirective = "\t.set\t";
Anton Korobeynikovedb4a6b2007-05-01 10:19:31 +0000118 PCSymbol = ".";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000119
120 // Set up DWARF directives
121 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000122
123 // Debug Information
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000124 AbsoluteDebugSectionOffsets = true;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000125 SupportsDebugInformation = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000126 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
127 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
128 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
129 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
130 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
131 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
132 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
133 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
134 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
135 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
136 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000137
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000138 // Exceptions handling
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000139 if (!Subtarget->is64Bit())
140 SupportsExceptionHandling = true;
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000141 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov71144d82007-05-01 10:16:06 +0000142 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
143 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
Reid Spencer02b85112006-10-30 22:32:30 +0000144 break;
145
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000146 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000147 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000148 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000149 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000150 COMMDirectiveTakesAlignment = false;
151 HasDotTypeDotSizeDirective = false;
152 StaticCtorsSection = "\t.section .ctors,\"aw\"";
153 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000154 HiddenDirective = NULL;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000155 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
156 WeakRefDirective = "\t.weak\t";
157 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000158
159 // Set up DWARF directives
160 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000161 AbsoluteDebugSectionOffsets = true;
162 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000163 SupportsDebugInformation = true;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000164 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000165 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
166 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
167 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
168 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
169 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
170 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
171 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
172 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
173 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
174 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
175 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
176 break;
Bill Wendling11502862007-01-16 04:13:03 +0000177
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000178 case X86Subtarget::isWindows:
179 GlobalPrefix = "_";
180 HasDotTypeDotSizeDirective = false;
181 break;
Bill Wendling11502862007-01-16 04:13:03 +0000182
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000183 default: break;
184 }
185
186 if (Subtarget->isFlavorIntel()) {
187 GlobalPrefix = "_";
188 CommentString = ";";
189
190 PrivateGlobalPrefix = "$";
191 AlignDirective = "\talign\t";
192 ZeroDirective = "\tdb\t";
193 ZeroDirectiveSuffix = " dup(0)";
194 AsciiDirective = "\tdb\t";
195 AscizDirective = 0;
196 Data8bitsDirective = "\tdb\t";
197 Data16bitsDirective = "\tdw\t";
198 Data32bitsDirective = "\tdd\t";
199 Data64bitsDirective = "\tdq\t";
200 HasDotTypeDotSizeDirective = false;
201
202 TextSection = "_text";
203 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000204 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000205 SwitchToSectionDirective = "";
206 TextSectionStartSuffix = "\tsegment 'CODE'";
207 DataSectionStartSuffix = "\tsegment 'DATA'";
208 SectionEndDirectiveSuffix = "\tends\n";
209 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000210
211 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000212}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000213
Chris Lattner625bd052006-11-29 01:14:06 +0000214bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
215 // FIXME: this should verify that we are targetting a 486 or better. If not,
216 // we will turn this bswap into something that will be lowered to logical ops
217 // instead of emitting the bswap asm. For now, we don't support 486 or lower
218 // so don't worry about this.
219
220 // Verify this is a simple bswap.
221 if (CI->getNumOperands() != 2 ||
222 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000223 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000224 return false;
225
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000226 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
227 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000228 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000229
Chris Lattner625bd052006-11-29 01:14:06 +0000230 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +0000231 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000232 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +0000233 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Chris Lattner625bd052006-11-29 01:14:06 +0000234
235 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000236 Op = new CallInst(Int, Op, CI->getName(), CI);
237
Chris Lattner625bd052006-11-29 01:14:06 +0000238 CI->replaceAllUsesWith(Op);
239 CI->eraseFromParent();
240 return true;
241}
242
243
244bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
245 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000246 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000247
248 std::string AsmStr = IA->getAsmString();
249
250 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
251 std::vector<std::string> AsmPieces;
252 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
253
254 switch (AsmPieces.size()) {
255 default: return false;
256 case 1:
257 AsmStr = AsmPieces[0];
258 AsmPieces.clear();
259 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
260
Chris Lattner5d521352006-11-29 01:48:01 +0000261 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000262 if (AsmPieces.size() == 2 &&
263 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
264 // No need to check constraints, nothing other than the equivalent of
265 // "=r,0" would be valid here.
266 return LowerToBSwap(CI);
267 }
268 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000269 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000270 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000271 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
272 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
273 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
274 std::vector<std::string> Words;
275 SplitString(AsmPieces[0], Words, " \t");
276 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
277 Words.clear();
278 SplitString(AsmPieces[1], Words, " \t");
279 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
280 Words.clear();
281 SplitString(AsmPieces[2], Words, " \t,");
282 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
283 Words[2] == "%edx") {
284 return LowerToBSwap(CI);
285 }
286 }
287 }
288 }
289 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000290 }
291 return false;
292}