blob: a00d616ca5fee78bd39fb0e7c5dd8fe1c704e111 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
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"
23using namespace llvm;
24
25static const char* x86_asm_table[] = {"{si}", "S",
26 "{di}", "D",
27 "{ax}", "a",
28 "{cx}", "c",
29 "{memory}", "memory",
30 "{flags}", "",
31 "{dirflag}", "",
32 "{fpsr}", "",
33 "{cc}", "cc",
34 0,0};
35
36X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
37 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
38
39 // FIXME - Should be simplified.
40
41 AsmTransCBE = x86_asm_table;
42
43 switch (Subtarget->TargetType) {
44 case X86Subtarget::isDarwin:
45 AlignmentIsInBytes = false;
46 GlobalPrefix = "_";
47 if (!Subtarget->is64Bit())
48 Data64bitsDirective = 0; // we can't emit a 64-bit unit
49 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
50 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
51 BSSSection = 0; // no BSS section.
52 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
53 ConstantPoolSection = "\t.const\n";
54 JumpTableDataSection = "\t.const\n";
55 CStringSection = "\t.cstring";
56 FourByteConstantSection = "\t.literal4\n";
57 EightByteConstantSection = "\t.literal8\n";
58 if (Subtarget->is64Bit())
59 SixteenByteConstantSection = "\t.literal16\n";
60 ReadOnlySection = "\t.const\n";
61 LCOMMDirective = "\t.lcomm\t";
62 COMMDirectiveTakesAlignment = false;
63 HasDotTypeDotSizeDirective = false;
64 if (TM.getRelocationModel() == Reloc::Static) {
65 StaticCtorsSection = ".constructor";
66 StaticDtorsSection = ".destructor";
67 } else {
68 StaticCtorsSection = ".mod_init_func";
69 StaticDtorsSection = ".mod_term_func";
70 }
71 InlineAsmStart = "# InlineAsm Start";
72 InlineAsmEnd = "# InlineAsm End";
73 SetDirective = "\t.set";
74 UsedDirective = "\t.no_dead_strip\t";
75 WeakRefDirective = "\t.weak_reference\t";
76 HiddenDirective = "\t.private_extern\t";
77
78 // 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
85 SupportsDebugInformation = true;
86 NeedsSet = true;
87 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
88 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
89 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
90 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
91 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
92 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
93 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
94 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
95 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
96 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
97 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov9fd3fe92007-08-21 00:31:30 +000098
99 // Exceptions handling
100 if (!Subtarget->is64Bit())
101 SupportsExceptionHandling = true;
102 AbsoluteEHSectionOffsets = false;
103 DwarfEHFrameSection =
104 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
105 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 break;
107
108 case X86Subtarget::isELF:
109 ReadOnlySection = "\t.section\t.rodata";
110 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
111 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
112 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
113 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
114 PrivateGlobalPrefix = ".L";
115 WeakRefDirective = "\t.weak\t";
116 SetDirective = "\t.set\t";
117 PCSymbol = ".";
118
119 // Set up DWARF directives
120 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000121
122 // Debug Information
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 AbsoluteDebugSectionOffsets = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 SupportsDebugInformation = true;
125 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
126 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
127 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
128 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
129 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
130 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
131 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
132 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
133 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
134 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
135 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
136
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000137 // Exceptions handling
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 if (!Subtarget->is64Bit())
139 SupportsExceptionHandling = true;
Anton Korobeynikovc6449f12007-07-25 00:06:28 +0000140 AbsoluteEHSectionOffsets = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
142 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
143 break;
144
145 case X86Subtarget::isCygwin:
146 case X86Subtarget::isMingw:
147 GlobalPrefix = "_";
148 LCOMMDirective = "\t.lcomm\t";
149 COMMDirectiveTakesAlignment = false;
150 HasDotTypeDotSizeDirective = false;
151 StaticCtorsSection = "\t.section .ctors,\"aw\"";
152 StaticDtorsSection = "\t.section .dtors,\"aw\"";
153 HiddenDirective = NULL;
154 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
155 WeakRefDirective = "\t.weak\t";
156 SetDirective = "\t.set\t";
157
158 // Set up DWARF directives
159 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
160 AbsoluteDebugSectionOffsets = true;
161 AbsoluteEHSectionOffsets = false;
162 SupportsDebugInformation = true;
163 DwarfSectionOffsetDirective = "\t.secrel32\t";
164 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
165 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
166 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
167 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
168 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
169 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
170 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
171 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
172 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
173 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
174 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
175 break;
176
177 case X86Subtarget::isWindows:
178 GlobalPrefix = "_";
179 HasDotTypeDotSizeDirective = false;
180 break;
181
182 default: break;
183 }
184
185 if (Subtarget->isFlavorIntel()) {
186 GlobalPrefix = "_";
187 CommentString = ";";
188
189 PrivateGlobalPrefix = "$";
190 AlignDirective = "\talign\t";
191 ZeroDirective = "\tdb\t";
192 ZeroDirectiveSuffix = " dup(0)";
193 AsciiDirective = "\tdb\t";
194 AscizDirective = 0;
195 Data8bitsDirective = "\tdb\t";
196 Data16bitsDirective = "\tdw\t";
197 Data32bitsDirective = "\tdd\t";
198 Data64bitsDirective = "\tdq\t";
199 HasDotTypeDotSizeDirective = false;
200
201 TextSection = "_text";
202 DataSection = "_data";
203 JumpTableDataSection = NULL;
204 SwitchToSectionDirective = "";
205 TextSectionStartSuffix = "\tsegment 'CODE'";
206 DataSectionStartSuffix = "\tsegment 'DATA'";
207 SectionEndDirectiveSuffix = "\tends\n";
208 }
209
210 AssemblerDialect = Subtarget->getAsmFlavor();
211}
212
213bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
214 // FIXME: this should verify that we are targetting a 486 or better. If not,
215 // we will turn this bswap into something that will be lowered to logical ops
216 // instead of emitting the bswap asm. For now, we don't support 486 or lower
217 // so don't worry about this.
218
219 // Verify this is a simple bswap.
220 if (CI->getNumOperands() != 2 ||
221 CI->getType() != CI->getOperand(1)->getType() ||
222 !CI->getType()->isInteger())
223 return false;
224
225 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
226 if (!Ty || Ty->getBitWidth() % 16 != 0)
227 return false;
228
229 // Okay, we can do this xform, do so now.
Chandler Carrutha228e392007-08-04 01:51:18 +0000230 const Type *Tys[] = { Ty };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carrutha228e392007-08-04 01:51:18 +0000232 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
234 Value *Op = CI->getOperand(1);
235 Op = new CallInst(Int, Op, CI->getName(), CI);
236
237 CI->replaceAllUsesWith(Op);
238 CI->eraseFromParent();
239 return true;
240}
241
242
243bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
244 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
245 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
246
247 std::string AsmStr = IA->getAsmString();
248
249 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
250 std::vector<std::string> AsmPieces;
251 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
252
253 switch (AsmPieces.size()) {
254 default: return false;
255 case 1:
256 AsmStr = AsmPieces[0];
257 AsmPieces.clear();
258 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
259
260 // bswap $0
261 if (AsmPieces.size() == 2 &&
262 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
263 // No need to check constraints, nothing other than the equivalent of
264 // "=r,0" would be valid here.
265 return LowerToBSwap(CI);
266 }
267 break;
268 case 3:
269 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
270 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
271 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
272 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
273 std::vector<std::string> Words;
274 SplitString(AsmPieces[0], Words, " \t");
275 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
276 Words.clear();
277 SplitString(AsmPieces[1], Words, " \t");
278 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
279 Words.clear();
280 SplitString(AsmPieces[2], Words, " \t,");
281 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
282 Words[2] == "%edx") {
283 return LowerToBSwap(CI);
284 }
285 }
286 }
287 }
288 break;
289 }
290 return false;
291}