blob: 5f156451818d792c2ae1580a2010f76a52a1ee5c [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";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000067 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000068
69 NeedsSet = true;
70 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
71 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
72 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
73 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
74 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
75 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
76 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
77 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
78 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
79 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
80 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
81 break;
Reid Spencer02b85112006-10-30 22:32:30 +000082
83 case X86Subtarget::isELF:
84 // Set up DWARF directives
85 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
86 // bool HasLEB128; // Defaults to false.
87 // hasDotLoc - True if target asm supports .loc directives.
88 // bool HasDotLoc; // Defaults to false.
89 // HasDotFile - True if target asm supports .file directives.
90 // bool HasDotFile; // Defaults to false.
Chris Lattnera3bfdd42007-01-03 18:16:48 +000091 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +000092 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +000093 DwarfRequiresFrameSection = false;
94 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
95 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
96 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
97 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
98 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
99 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
100 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
101 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
102 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
103 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
104 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
105 break;
106
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000107 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000108 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000109 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000110 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000111 COMMDirectiveTakesAlignment = false;
112 HasDotTypeDotSizeDirective = false;
113 StaticCtorsSection = "\t.section .ctors,\"aw\"";
114 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000115 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000116
117 // Set up DWARF directives
118 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
119 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
120 DwarfRequiresFrameSection = false;
121 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
122 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
123 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
124 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
125 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
126 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
127 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
128 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
129 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
130 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
131 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
132 break;
133
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000134 break;
135 case X86Subtarget::isWindows:
136 GlobalPrefix = "_";
137 HasDotTypeDotSizeDirective = false;
138 break;
139 default: break;
140 }
141
142 if (Subtarget->isFlavorIntel()) {
143 GlobalPrefix = "_";
144 CommentString = ";";
145
146 PrivateGlobalPrefix = "$";
147 AlignDirective = "\talign\t";
148 ZeroDirective = "\tdb\t";
149 ZeroDirectiveSuffix = " dup(0)";
150 AsciiDirective = "\tdb\t";
151 AscizDirective = 0;
152 Data8bitsDirective = "\tdb\t";
153 Data16bitsDirective = "\tdw\t";
154 Data32bitsDirective = "\tdd\t";
155 Data64bitsDirective = "\tdq\t";
156 HasDotTypeDotSizeDirective = false;
157
158 TextSection = "_text";
159 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000160 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000161 SwitchToSectionDirective = "";
162 TextSectionStartSuffix = "\tsegment 'CODE'";
163 DataSectionStartSuffix = "\tsegment 'DATA'";
164 SectionEndDirectiveSuffix = "\tends\n";
165 }
166}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000167
Chris Lattner625bd052006-11-29 01:14:06 +0000168bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
169 // FIXME: this should verify that we are targetting a 486 or better. If not,
170 // we will turn this bswap into something that will be lowered to logical ops
171 // instead of emitting the bswap asm. For now, we don't support 486 or lower
172 // so don't worry about this.
173
174 // Verify this is a simple bswap.
175 if (CI->getNumOperands() != 2 ||
176 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000177 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000178 return false;
179
Reid Spencer47857812006-12-31 05:55:36 +0000180 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000181 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000182 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
183 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000184 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000186 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000187 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000188 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000189 IntName = "llvm.bswap.i64";
190 else
191 return false;
192 } else
193 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000194
195 // Okay, we can do this xform, do so now.
196 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000197 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000198
199 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000200 Op = new CallInst(Int, Op, CI->getName(), CI);
201
Chris Lattner625bd052006-11-29 01:14:06 +0000202 CI->replaceAllUsesWith(Op);
203 CI->eraseFromParent();
204 return true;
205}
206
207
208bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
209 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000210 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000211
212 std::string AsmStr = IA->getAsmString();
213
214 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
215 std::vector<std::string> AsmPieces;
216 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
217
218 switch (AsmPieces.size()) {
219 default: return false;
220 case 1:
221 AsmStr = AsmPieces[0];
222 AsmPieces.clear();
223 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
224
Chris Lattner5d521352006-11-29 01:48:01 +0000225 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000226 if (AsmPieces.size() == 2 &&
227 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
228 // No need to check constraints, nothing other than the equivalent of
229 // "=r,0" would be valid here.
230 return LowerToBSwap(CI);
231 }
232 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000233 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000234 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000235 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
236 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
237 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
238 std::vector<std::string> Words;
239 SplitString(AsmPieces[0], Words, " \t");
240 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
241 Words.clear();
242 SplitString(AsmPieces[1], Words, " \t");
243 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
244 Words.clear();
245 SplitString(AsmPieces[2], Words, " \t,");
246 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
247 Words[2] == "%edx") {
248 return LowerToBSwap(CI);
249 }
250 }
251 }
252 }
253 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000254 }
255 return false;
256}