blob: fd8af4f69a7f3e670c8d3459d75798e0388de64f [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";
Chris Lattnera53115b2006-09-26 03:39:53 +000074 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000075 WeakRefDirective = "\t.weak_reference\t";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000076 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000077
Chris Lattnerb266ccd2007-01-18 01:15:58 +000078 // In non-PIC modes, emit a special label before jump tables so that the
79 // linker can perform more accurate dead code stripping.
80 if (TM.getRelocationModel() != Reloc::PIC_) {
81 // Emit a local label that is preserved until the linker runs.
82 JumpTableSpecialLabelPrefix = "l";
83 }
84
Jim Laskey8e8de8f2006-09-07 22:05:02 +000085 NeedsSet = true;
86 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
87 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
88 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
89 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
90 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
91 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
92 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
93 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
94 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
95 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
96 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
97 break;
Reid Spencer02b85112006-10-30 22:32:30 +000098
99 case X86Subtarget::isELF:
100 // Set up DWARF directives
101 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000102 AbsoluteSectionOffsets = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000103 // bool HasLEB128; // Defaults to false.
104 // hasDotLoc - True if target asm supports .loc directives.
105 // bool HasDotLoc; // Defaults to false.
106 // HasDotFile - True if target asm supports .file directives.
107 // bool HasDotFile; // Defaults to false.
Evan Chengf0b5d562007-03-08 01:07:07 +0000108 ReadOnlySection = "\t.section\t.rodata\n";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000109 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000110 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000111 SetDirective = "\t.set\t";
Reid Spencer02b85112006-10-30 22:32:30 +0000112 DwarfRequiresFrameSection = false;
113 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
114 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
115 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
116 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
117 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
118 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
119 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
120 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
121 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
122 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
123 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
124 break;
125
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000126 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000127 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000128 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000129 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000130 COMMDirectiveTakesAlignment = false;
131 HasDotTypeDotSizeDirective = false;
132 StaticCtorsSection = "\t.section .ctors,\"aw\"";
133 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000134 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000135
136 // Set up DWARF directives
137 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000138 AbsoluteSectionOffsets = true;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000139 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
Anton Korobeynikov66413092007-02-23 01:58:50 +0000140 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000141 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000142 DwarfRequiresFrameSection = false;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000143 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000144 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
145 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
146 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
147 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
148 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
149 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
150 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
151 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
152 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
153 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
154 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
155 break;
Bill Wendling11502862007-01-16 04:13:03 +0000156
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000157 case X86Subtarget::isWindows:
158 GlobalPrefix = "_";
159 HasDotTypeDotSizeDirective = false;
160 break;
Bill Wendling11502862007-01-16 04:13:03 +0000161
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000162 default: break;
163 }
164
165 if (Subtarget->isFlavorIntel()) {
166 GlobalPrefix = "_";
167 CommentString = ";";
168
169 PrivateGlobalPrefix = "$";
170 AlignDirective = "\talign\t";
171 ZeroDirective = "\tdb\t";
172 ZeroDirectiveSuffix = " dup(0)";
173 AsciiDirective = "\tdb\t";
174 AscizDirective = 0;
175 Data8bitsDirective = "\tdb\t";
176 Data16bitsDirective = "\tdw\t";
177 Data32bitsDirective = "\tdd\t";
178 Data64bitsDirective = "\tdq\t";
179 HasDotTypeDotSizeDirective = false;
180
181 TextSection = "_text";
182 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000183 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000184 SwitchToSectionDirective = "";
185 TextSectionStartSuffix = "\tsegment 'CODE'";
186 DataSectionStartSuffix = "\tsegment 'DATA'";
187 SectionEndDirectiveSuffix = "\tends\n";
188 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000189
190 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000191}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000192
Chris Lattner625bd052006-11-29 01:14:06 +0000193bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
194 // FIXME: this should verify that we are targetting a 486 or better. If not,
195 // we will turn this bswap into something that will be lowered to logical ops
196 // instead of emitting the bswap asm. For now, we don't support 486 or lower
197 // so don't worry about this.
198
199 // Verify this is a simple bswap.
200 if (CI->getNumOperands() != 2 ||
201 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000202 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000203 return false;
204
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000205 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
206 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000207 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000208
Chris Lattner625bd052006-11-29 01:14:06 +0000209 // Okay, we can do this xform, do so now.
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000210 const Type *Tys[] = { Ty, Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000211 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000212 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
Chris Lattner625bd052006-11-29 01:14:06 +0000213
214 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000215 Op = new CallInst(Int, Op, CI->getName(), CI);
216
Chris Lattner625bd052006-11-29 01:14:06 +0000217 CI->replaceAllUsesWith(Op);
218 CI->eraseFromParent();
219 return true;
220}
221
222
223bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
224 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000225 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000226
227 std::string AsmStr = IA->getAsmString();
228
229 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
230 std::vector<std::string> AsmPieces;
231 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
232
233 switch (AsmPieces.size()) {
234 default: return false;
235 case 1:
236 AsmStr = AsmPieces[0];
237 AsmPieces.clear();
238 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
239
Chris Lattner5d521352006-11-29 01:48:01 +0000240 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000241 if (AsmPieces.size() == 2 &&
242 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
243 // No need to check constraints, nothing other than the equivalent of
244 // "=r,0" would be valid here.
245 return LowerToBSwap(CI);
246 }
247 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000248 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000249 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000250 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
251 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
252 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
253 std::vector<std::string> Words;
254 SplitString(AsmPieces[0], Words, " \t");
255 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
256 Words.clear();
257 SplitString(AsmPieces[1], Words, " \t");
258 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
259 Words.clear();
260 SplitString(AsmPieces[2], Words, " \t,");
261 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
262 Words[2] == "%edx") {
263 return LowerToBSwap(CI);
264 }
265 }
266 }
267 }
268 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000269 }
270 return false;
271}