blob: 7e90cd9b3e7e3065df9cdf662f7539f363a692f1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
17#include "llvm/DerivedTypes.h"
18#include "llvm/InlineAsm.h"
19#include "llvm/Instructions.h"
20#include "llvm/Intrinsics.h"
21#include "llvm/Module.h"
22#include "llvm/ADT/StringExtras.h"
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000023#include "llvm/Support/Dwarf.h"
24
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025using namespace llvm;
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000026using namespace llvm::dwarf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
Dan Gohman12300e12008-03-25 21:45:14 +000028static const char *const x86_asm_table[] = {
29 "{si}", "S",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 "{di}", "D",
31 "{ax}", "a",
32 "{cx}", "c",
33 "{memory}", "memory",
34 "{flags}", "",
35 "{dirflag}", "",
36 "{fpsr}", "",
37 "{cc}", "cc",
38 0,0};
39
40X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
41 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000042 X86TM = &TM;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 // FIXME - Should be simplified.
45
46 AsmTransCBE = x86_asm_table;
47
48 switch (Subtarget->TargetType) {
49 case X86Subtarget::isDarwin:
50 AlignmentIsInBytes = false;
Evan Cheng45c1edb2008-02-28 00:43:03 +000051 TextAlignFillValue = 0x90;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 GlobalPrefix = "_";
53 if (!Subtarget->is64Bit())
54 Data64bitsDirective = 0; // we can't emit a 64-bit unit
55 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
56 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
57 BSSSection = 0; // no BSS section.
58 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
59 ConstantPoolSection = "\t.const\n";
60 JumpTableDataSection = "\t.const\n";
61 CStringSection = "\t.cstring";
62 FourByteConstantSection = "\t.literal4\n";
63 EightByteConstantSection = "\t.literal8\n";
64 if (Subtarget->is64Bit())
65 SixteenByteConstantSection = "\t.literal16\n";
66 ReadOnlySection = "\t.const\n";
67 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen4670be42008-01-15 23:24:56 +000068 SwitchToSectionDirective = "\t.section ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 COMMDirectiveTakesAlignment = false;
70 HasDotTypeDotSizeDirective = false;
71 if (TM.getRelocationModel() == Reloc::Static) {
72 StaticCtorsSection = ".constructor";
73 StaticDtorsSection = ".destructor";
74 } else {
75 StaticCtorsSection = ".mod_init_func";
76 StaticDtorsSection = ".mod_term_func";
77 }
Dale Johannesen4670be42008-01-15 23:24:56 +000078 if (Subtarget->is64Bit()) {
79 PersonalityPrefix = "";
80 PersonalitySuffix = "+4@GOTPCREL";
81 } else {
82 PersonalityPrefix = "L";
83 PersonalitySuffix = "$non_lazy_ptr";
84 }
Bill Wendling2d369922007-09-11 17:20:55 +000085 NeedsIndirectEncoding = true;
Dale Johannesen4bf49712008-03-25 23:29:30 +000086 InlineAsmStart = "## InlineAsm Start";
87 InlineAsmEnd = "## InlineAsm End";
88 CommentString = "##";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 SetDirective = "\t.set";
Bill Wendlingfc6c82e2007-08-22 18:44:05 +000090 PCSymbol = ".";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 UsedDirective = "\t.no_dead_strip\t";
Dale Johannesenf09b5992008-01-10 02:03:30 +000092 WeakDefDirective = "\t.weak_definition ";
93 WeakRefDirective = "\t.weak_reference ";
Dale Johannesen3c788322008-01-11 00:54:37 +000094 HiddenDirective = "\t.private_extern ";
Bill Wendling9474c412008-03-18 23:38:12 +000095 ProtectedDirective = "\t.globl\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // In non-PIC modes, emit a special label before jump tables so that the
98 // linker can perform more accurate dead code stripping.
99 if (TM.getRelocationModel() != Reloc::PIC_) {
100 // Emit a local label that is preserved until the linker runs.
101 JumpTableSpecialLabelPrefix = "l";
102 }
103
104 SupportsDebugInformation = true;
105 NeedsSet = true;
106 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
107 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
108 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
109 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
110 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
111 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
112 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
113 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
114 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
115 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
116 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov9fd3fe92007-08-21 00:31:30 +0000117
118 // Exceptions handling
Dale Johannesen4670be42008-01-15 23:24:56 +0000119 SupportsExceptionHandling = true;
Dale Johannesenf09b5992008-01-10 02:03:30 +0000120 GlobalEHDirective = "\t.globl\t";
121 SupportsWeakOmittedEHFrame = false;
Anton Korobeynikov9fd3fe92007-08-21 00:31:30 +0000122 AbsoluteEHSectionOffsets = false;
123 DwarfEHFrameSection =
124 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
125 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 break;
127
128 case X86Subtarget::isELF:
129 ReadOnlySection = "\t.section\t.rodata";
130 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
131 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
132 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
133 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
134 PrivateGlobalPrefix = ".L";
135 WeakRefDirective = "\t.weak\t";
136 SetDirective = "\t.set\t";
137 PCSymbol = ".";
138
139 // Set up DWARF directives
140 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000141
142 // Debug Information
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 AbsoluteDebugSectionOffsets = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 SupportsDebugInformation = true;
145 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
146 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
147 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
148 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
149 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
150 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
151 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
152 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
153 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
154 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
155 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
156
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000157 // Exceptions handling
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 if (!Subtarget->is64Bit())
159 SupportsExceptionHandling = true;
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000160 AbsoluteEHSectionOffsets = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
162 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
163 break;
164
165 case X86Subtarget::isCygwin:
166 case X86Subtarget::isMingw:
167 GlobalPrefix = "_";
168 LCOMMDirective = "\t.lcomm\t";
169 COMMDirectiveTakesAlignment = false;
170 HasDotTypeDotSizeDirective = false;
171 StaticCtorsSection = "\t.section .ctors,\"aw\"";
172 StaticDtorsSection = "\t.section .dtors,\"aw\"";
173 HiddenDirective = NULL;
174 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
175 WeakRefDirective = "\t.weak\t";
176 SetDirective = "\t.set\t";
177
178 // Set up DWARF directives
179 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
180 AbsoluteDebugSectionOffsets = true;
181 AbsoluteEHSectionOffsets = false;
182 SupportsDebugInformation = true;
183 DwarfSectionOffsetDirective = "\t.secrel32\t";
184 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
185 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
186 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
187 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
188 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
189 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
190 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
191 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
192 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
193 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
194 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
195 break;
196
197 case X86Subtarget::isWindows:
198 GlobalPrefix = "_";
199 HasDotTypeDotSizeDirective = false;
200 break;
201
202 default: break;
203 }
204
205 if (Subtarget->isFlavorIntel()) {
206 GlobalPrefix = "_";
207 CommentString = ";";
208
209 PrivateGlobalPrefix = "$";
210 AlignDirective = "\talign\t";
211 ZeroDirective = "\tdb\t";
212 ZeroDirectiveSuffix = " dup(0)";
213 AsciiDirective = "\tdb\t";
214 AscizDirective = 0;
215 Data8bitsDirective = "\tdb\t";
216 Data16bitsDirective = "\tdw\t";
217 Data32bitsDirective = "\tdd\t";
218 Data64bitsDirective = "\tdq\t";
219 HasDotTypeDotSizeDirective = false;
220
221 TextSection = "_text";
222 DataSection = "_data";
223 JumpTableDataSection = NULL;
224 SwitchToSectionDirective = "";
225 TextSectionStartSuffix = "\tsegment 'CODE'";
226 DataSectionStartSuffix = "\tsegment 'DATA'";
227 SectionEndDirectiveSuffix = "\tends\n";
228 }
229
Dan Gohmana65530a2008-05-05 00:28:39 +0000230 // On Linux we must declare when we can use a non-executable stack.
Dan Gohman2593e2b2008-05-05 16:11:31 +0000231 if (Subtarget->isLinux())
Dan Gohmana65530a2008-05-05 00:28:39 +0000232 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
233
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 AssemblerDialect = Subtarget->getAsmFlavor();
235}
236
237bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
238 // FIXME: this should verify that we are targetting a 486 or better. If not,
239 // we will turn this bswap into something that will be lowered to logical ops
240 // instead of emitting the bswap asm. For now, we don't support 486 or lower
241 // so don't worry about this.
242
243 // Verify this is a simple bswap.
244 if (CI->getNumOperands() != 2 ||
245 CI->getType() != CI->getOperand(1)->getType() ||
246 !CI->getType()->isInteger())
247 return false;
248
249 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
250 if (!Ty || Ty->getBitWidth() % 16 != 0)
251 return false;
252
253 // Okay, we can do this xform, do so now.
Chandler Carrutha228e392007-08-04 01:51:18 +0000254 const Type *Tys[] = { Ty };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carrutha228e392007-08-04 01:51:18 +0000256 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257
258 Value *Op = CI->getOperand(1);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000259 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260
261 CI->replaceAllUsesWith(Op);
262 CI->eraseFromParent();
263 return true;
264}
265
266
267bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
268 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
269 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
270
271 std::string AsmStr = IA->getAsmString();
272
273 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
274 std::vector<std::string> AsmPieces;
275 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
276
277 switch (AsmPieces.size()) {
278 default: return false;
279 case 1:
280 AsmStr = AsmPieces[0];
281 AsmPieces.clear();
282 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
283
284 // bswap $0
285 if (AsmPieces.size() == 2 &&
286 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
287 // No need to check constraints, nothing other than the equivalent of
288 // "=r,0" would be valid here.
289 return LowerToBSwap(CI);
290 }
291 break;
292 case 3:
293 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
294 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
295 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
296 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
297 std::vector<std::string> Words;
298 SplitString(AsmPieces[0], Words, " \t");
299 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
300 Words.clear();
301 SplitString(AsmPieces[1], Words, " \t");
302 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
303 Words.clear();
304 SplitString(AsmPieces[2], Words, " \t,");
305 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
306 Words[2] == "%edx") {
307 return LowerToBSwap(CI);
308 }
309 }
310 }
311 }
312 break;
313 }
314 return false;
315}
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000316
317/// PreferredEHDataFormat - This hook allows the target to select data
318/// format used for encoding pointers in exception handling data. Reason is
319/// 0 for data, 1 for code labels, 2 for function pointers. Global is true
320/// if the symbol can be relocated.
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000321unsigned X86TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000322 bool Global) const {
323 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
324
325 switch (Subtarget->TargetType) {
326 case X86Subtarget::isDarwin:
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000327 if (Reason == DwarfEncoding::Functions && Global)
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000328 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000329 else if (Reason == DwarfEncoding::CodeLabels || !Global)
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000330 return DW_EH_PE_pcrel;
331 else
332 return DW_EH_PE_absptr;
333
334 case X86Subtarget::isELF:
335 case X86Subtarget::isCygwin:
336 case X86Subtarget::isMingw: {
337 CodeModel::Model CM = X86TM->getCodeModel();
338
339 if (X86TM->getRelocationModel() == Reloc::PIC_) {
340 unsigned Format = 0;
341
342 if (!Subtarget->is64Bit())
343 // 32 bit targets always encode pointers as 4 bytes
344 Format = DW_EH_PE_sdata4;
345 else {
346 // 64 bit targets encode pointers in 4 bytes iff:
347 // - code model is small OR
348 // - code model is medium and we're emitting externally visible symbols or
349 // any code symbols
350 if (CM == CodeModel::Small ||
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000351 (CM == CodeModel::Medium && (Global ||
352 Reason != DwarfEncoding::Data)))
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000353 Format = DW_EH_PE_sdata4;
354 else
355 Format = DW_EH_PE_sdata8;
356 }
357
358 if (Global)
359 Format |= DW_EH_PE_indirect;
360
361 return (Format | DW_EH_PE_pcrel);
362 } else {
363 if (Subtarget->is64Bit() &&
364 (CM == CodeModel::Small ||
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000365 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000366 return DW_EH_PE_udata4;
367 else
368 return DW_EH_PE_absptr;
369 }
370 }
371
372 default:
373 return TargetAsmInfo::PreferredEHDataFormat(Reason, Global);
374 }
375}
376