blob: c66862aec6292380a92f4695cb9a8442032a47fc [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"
20#include "llvm/Module.h"
21#include "llvm/ADT/StringExtras.h"
Jim Laskey8e8de8f2006-09-07 22:05:02 +000022using namespace llvm;
23
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000024static const char* x86_asm_table[] = {"{si}", "S",
25 "{di}", "D",
26 "{ax}", "a",
27 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000028 "{memory}", "memory",
29 "{flags}", "",
30 "{dirflag}", "",
31 "{fpsr}", "",
32 "{cc}", "cc",
33 0,0};
34
Jim Laskey8e8de8f2006-09-07 22:05:02 +000035X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
36 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
37
Chris Lattnerafbfded2006-10-05 02:43:52 +000038 // FIXME - Should be simplified.
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000039
40 AsmTransCBE = x86_asm_table;
41
Jim Laskey8e8de8f2006-09-07 22:05:02 +000042 switch (Subtarget->TargetType) {
43 case X86Subtarget::isDarwin:
44 AlignmentIsInBytes = false;
45 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000046 if (!Subtarget->is64Bit())
47 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000048 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
49 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner42ca4502007-01-17 17:43:33 +000050 BSSSection = 0; // no BSS section.
51 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Jim Laskey8e8de8f2006-09-07 22:05:02 +000052 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000053 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000054 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000055 FourByteConstantSection = "\t.literal4\n";
56 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000057 if (Subtarget->is64Bit())
58 SixteenByteConstantSection = "\t.literal16\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000059 LCOMMDirective = "\t.lcomm\t";
60 COMMDirectiveTakesAlignment = false;
61 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000062 if (TM.getRelocationModel() == Reloc::Static) {
63 StaticCtorsSection = ".constructor";
64 StaticDtorsSection = ".destructor";
65 } else {
66 StaticCtorsSection = ".mod_init_func";
67 StaticDtorsSection = ".mod_term_func";
68 }
Jim Laskey8e8de8f2006-09-07 22:05:02 +000069 InlineAsmStart = "# InlineAsm Start";
70 InlineAsmEnd = "# InlineAsm End";
71 SetDirective = "\t.set";
Chris Lattnera53115b2006-09-26 03:39:53 +000072 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000073 WeakRefDirective = "\t.weak_reference\t";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000074 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000075
Chris Lattnerb266ccd2007-01-18 01:15:58 +000076 // In non-PIC modes, emit a special label before jump tables so that the
77 // linker can perform more accurate dead code stripping.
78 if (TM.getRelocationModel() != Reloc::PIC_) {
79 // Emit a local label that is preserved until the linker runs.
80 JumpTableSpecialLabelPrefix = "l";
81 }
82
Jim Laskey8e8de8f2006-09-07 22:05:02 +000083 NeedsSet = true;
84 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
85 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
86 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
87 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
88 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
89 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
90 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
91 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
92 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
93 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
94 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
95 break;
Reid Spencer02b85112006-10-30 22:32:30 +000096
97 case X86Subtarget::isELF:
98 // Set up DWARF directives
99 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000100 AbsoluteSectionOffsets = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000101 // bool HasLEB128; // Defaults to false.
102 // hasDotLoc - True if target asm supports .loc directives.
103 // bool HasDotLoc; // Defaults to false.
104 // HasDotFile - True if target asm supports .file directives.
105 // bool HasDotFile; // Defaults to false.
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000106 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000107 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +0000108 DwarfRequiresFrameSection = false;
109 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
110 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
111 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
112 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
113 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
114 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
115 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
116 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
117 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
118 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
119 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
120 break;
121
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000122 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000123 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000124 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000125 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000126 COMMDirectiveTakesAlignment = false;
127 HasDotTypeDotSizeDirective = false;
128 StaticCtorsSection = "\t.section .ctors,\"aw\"";
129 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000130 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000131
132 // Set up DWARF directives
133 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000134 AbsoluteSectionOffsets = true;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000135 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
Anton Korobeynikov66413092007-02-23 01:58:50 +0000136 WeakRefDirective = "\t.weak\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000137 DwarfRequiresFrameSection = false;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000138 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000139 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
140 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
141 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
142 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
143 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
144 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
145 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
146 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
147 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
148 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
149 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
150 break;
Bill Wendling11502862007-01-16 04:13:03 +0000151
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000152 case X86Subtarget::isWindows:
153 GlobalPrefix = "_";
154 HasDotTypeDotSizeDirective = false;
155 break;
Bill Wendling11502862007-01-16 04:13:03 +0000156
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000157 default: break;
158 }
159
160 if (Subtarget->isFlavorIntel()) {
161 GlobalPrefix = "_";
162 CommentString = ";";
163
164 PrivateGlobalPrefix = "$";
165 AlignDirective = "\talign\t";
166 ZeroDirective = "\tdb\t";
167 ZeroDirectiveSuffix = " dup(0)";
168 AsciiDirective = "\tdb\t";
169 AscizDirective = 0;
170 Data8bitsDirective = "\tdb\t";
171 Data16bitsDirective = "\tdw\t";
172 Data32bitsDirective = "\tdd\t";
173 Data64bitsDirective = "\tdq\t";
174 HasDotTypeDotSizeDirective = false;
175
176 TextSection = "_text";
177 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000178 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000179 SwitchToSectionDirective = "";
180 TextSectionStartSuffix = "\tsegment 'CODE'";
181 DataSectionStartSuffix = "\tsegment 'DATA'";
182 SectionEndDirectiveSuffix = "\tends\n";
183 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000184
185 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000186}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000187
Chris Lattner625bd052006-11-29 01:14:06 +0000188bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
189 // FIXME: this should verify that we are targetting a 486 or better. If not,
190 // we will turn this bswap into something that will be lowered to logical ops
191 // instead of emitting the bswap asm. For now, we don't support 486 or lower
192 // so don't worry about this.
193
194 // Verify this is a simple bswap.
195 if (CI->getNumOperands() != 2 ||
196 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000197 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000198 return false;
199
Reid Spencer47857812006-12-31 05:55:36 +0000200 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000201 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000202 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
203 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000204 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000205 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000206 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000207 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000208 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000209 IntName = "llvm.bswap.i64";
210 else
211 return false;
212 } else
213 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000214
215 // Okay, we can do this xform, do so now.
216 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000217 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000218
219 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000220 Op = new CallInst(Int, Op, CI->getName(), CI);
221
Chris Lattner625bd052006-11-29 01:14:06 +0000222 CI->replaceAllUsesWith(Op);
223 CI->eraseFromParent();
224 return true;
225}
226
227
228bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
229 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000230 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000231
232 std::string AsmStr = IA->getAsmString();
233
234 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
235 std::vector<std::string> AsmPieces;
236 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
237
238 switch (AsmPieces.size()) {
239 default: return false;
240 case 1:
241 AsmStr = AsmPieces[0];
242 AsmPieces.clear();
243 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
244
Chris Lattner5d521352006-11-29 01:48:01 +0000245 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000246 if (AsmPieces.size() == 2 &&
247 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
248 // No need to check constraints, nothing other than the equivalent of
249 // "=r,0" would be valid here.
250 return LowerToBSwap(CI);
251 }
252 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000253 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000254 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000255 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
256 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
257 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
258 std::vector<std::string> Words;
259 SplitString(AsmPieces[0], Words, " \t");
260 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
261 Words.clear();
262 SplitString(AsmPieces[1], Words, " \t");
263 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
264 Words.clear();
265 SplitString(AsmPieces[2], Words, " \t,");
266 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
267 Words[2] == "%edx") {
268 return LowerToBSwap(CI);
269 }
270 }
271 }
272 }
273 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000274 }
275 return false;
276}