blob: 7371161b96d450f4ef67bed34ed7291bf6cd9db6 [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
50 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000051 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000052 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000053 FourByteConstantSection = "\t.literal4\n";
54 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000055 if (Subtarget->is64Bit())
56 SixteenByteConstantSection = "\t.literal16\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000057 LCOMMDirective = "\t.lcomm\t";
58 COMMDirectiveTakesAlignment = false;
59 HasDotTypeDotSizeDirective = false;
60 StaticCtorsSection = ".mod_init_func";
61 StaticDtorsSection = ".mod_term_func";
62 InlineAsmStart = "# InlineAsm Start";
63 InlineAsmEnd = "# InlineAsm End";
64 SetDirective = "\t.set";
Chris Lattnera53115b2006-09-26 03:39:53 +000065 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000066 WeakRefDirective = "\t.weak_reference\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000067
68 NeedsSet = true;
69 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
70 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
71 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
72 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
73 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
74 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
75 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
76 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
77 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
78 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
79 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
80 break;
Reid Spencer02b85112006-10-30 22:32:30 +000081
82 case X86Subtarget::isELF:
83 // Set up DWARF directives
84 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
85 // bool HasLEB128; // Defaults to false.
86 // hasDotLoc - True if target asm supports .loc directives.
87 // bool HasDotLoc; // Defaults to false.
88 // HasDotFile - True if target asm supports .file directives.
89 // bool HasDotFile; // Defaults to false.
Chris Lattnera3bfdd42007-01-03 18:16:48 +000090 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +000091 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +000092 DwarfRequiresFrameSection = false;
93 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
94 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
95 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
96 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
97 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
98 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
99 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
100 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
101 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
102 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
103 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
104 break;
105
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000106 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000107 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000108 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000109 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000110 COMMDirectiveTakesAlignment = false;
111 HasDotTypeDotSizeDirective = false;
112 StaticCtorsSection = "\t.section .ctors,\"aw\"";
113 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000114
115 // Set up DWARF directives
116 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
117 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
118 DwarfRequiresFrameSection = false;
119 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
120 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
121 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
122 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
123 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
124 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
125 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
126 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
127 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
128 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
129 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
130 break;
131
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000132 break;
133 case X86Subtarget::isWindows:
134 GlobalPrefix = "_";
135 HasDotTypeDotSizeDirective = false;
136 break;
137 default: break;
138 }
139
140 if (Subtarget->isFlavorIntel()) {
141 GlobalPrefix = "_";
142 CommentString = ";";
143
144 PrivateGlobalPrefix = "$";
145 AlignDirective = "\talign\t";
146 ZeroDirective = "\tdb\t";
147 ZeroDirectiveSuffix = " dup(0)";
148 AsciiDirective = "\tdb\t";
149 AscizDirective = 0;
150 Data8bitsDirective = "\tdb\t";
151 Data16bitsDirective = "\tdw\t";
152 Data32bitsDirective = "\tdd\t";
153 Data64bitsDirective = "\tdq\t";
154 HasDotTypeDotSizeDirective = false;
155
156 TextSection = "_text";
157 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000158 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000159 SwitchToSectionDirective = "";
160 TextSectionStartSuffix = "\tsegment 'CODE'";
161 DataSectionStartSuffix = "\tsegment 'DATA'";
162 SectionEndDirectiveSuffix = "\tends\n";
163 }
164}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000165
Chris Lattner625bd052006-11-29 01:14:06 +0000166bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
167 // FIXME: this should verify that we are targetting a 486 or better. If not,
168 // we will turn this bswap into something that will be lowered to logical ops
169 // instead of emitting the bswap asm. For now, we don't support 486 or lower
170 // so don't worry about this.
171
172 // Verify this is a simple bswap.
173 if (CI->getNumOperands() != 2 ||
174 CI->getType() != CI->getOperand(1)->getType() ||
175 !CI->getType()->isInteger())
176 return false;
177
Reid Spencer47857812006-12-31 05:55:36 +0000178 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000179 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000180 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
181 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000182 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000183 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000184 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000186 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000187 IntName = "llvm.bswap.i64";
188 else
189 return false;
190 } else
191 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000192
193 // Okay, we can do this xform, do so now.
194 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000195 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000196
197 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000198 Op = new CallInst(Int, Op, CI->getName(), CI);
199
Chris Lattner625bd052006-11-29 01:14:06 +0000200 CI->replaceAllUsesWith(Op);
201 CI->eraseFromParent();
202 return true;
203}
204
205
206bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
207 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000208 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000209
210 std::string AsmStr = IA->getAsmString();
211
212 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
213 std::vector<std::string> AsmPieces;
214 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
215
216 switch (AsmPieces.size()) {
217 default: return false;
218 case 1:
219 AsmStr = AsmPieces[0];
220 AsmPieces.clear();
221 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
222
Chris Lattner5d521352006-11-29 01:48:01 +0000223 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000224 if (AsmPieces.size() == 2 &&
225 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
226 // No need to check constraints, nothing other than the equivalent of
227 // "=r,0" would be valid here.
228 return LowerToBSwap(CI);
229 }
230 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000231 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000232 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000233 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
234 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
235 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
236 std::vector<std::string> Words;
237 SplitString(AsmPieces[0], Words, " \t");
238 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
239 Words.clear();
240 SplitString(AsmPieces[1], Words, " \t");
241 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
242 Words.clear();
243 SplitString(AsmPieces[2], Words, " \t,");
244 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
245 Words[2] == "%edx") {
246 return LowerToBSwap(CI);
247 }
248 }
249 }
250 }
251 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000252 }
253 return false;
254}