blob: 20e0f195fcf0c4c4900f7f40bd783ae41a91f727 [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)
100 // bool HasLEB128; // Defaults to false.
101 // hasDotLoc - True if target asm supports .loc directives.
102 // bool HasDotLoc; // Defaults to false.
103 // HasDotFile - True if target asm supports .file directives.
104 // bool HasDotFile; // Defaults to false.
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000105 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000106 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +0000107 DwarfRequiresFrameSection = false;
108 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
109 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
110 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
111 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
112 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
113 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
114 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
115 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
116 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
117 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
118 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
119 break;
120
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000121 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000122 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000123 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000124 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000125 COMMDirectiveTakesAlignment = false;
126 HasDotTypeDotSizeDirective = false;
127 StaticCtorsSection = "\t.section .ctors,\"aw\"";
128 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000129 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000130
131 // Set up DWARF directives
132 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
133 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
134 DwarfRequiresFrameSection = false;
135 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
136 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
137 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
138 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
139 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
140 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
141 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
142 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
143 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
144 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
145 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
146 break;
Bill Wendling11502862007-01-16 04:13:03 +0000147
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000148 case X86Subtarget::isWindows:
149 GlobalPrefix = "_";
150 HasDotTypeDotSizeDirective = false;
151 break;
Bill Wendling11502862007-01-16 04:13:03 +0000152
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000153 default: break;
154 }
155
156 if (Subtarget->isFlavorIntel()) {
157 GlobalPrefix = "_";
158 CommentString = ";";
159
160 PrivateGlobalPrefix = "$";
161 AlignDirective = "\talign\t";
162 ZeroDirective = "\tdb\t";
163 ZeroDirectiveSuffix = " dup(0)";
164 AsciiDirective = "\tdb\t";
165 AscizDirective = 0;
166 Data8bitsDirective = "\tdb\t";
167 Data16bitsDirective = "\tdw\t";
168 Data32bitsDirective = "\tdd\t";
169 Data64bitsDirective = "\tdq\t";
170 HasDotTypeDotSizeDirective = false;
171
172 TextSection = "_text";
173 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000174 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000175 SwitchToSectionDirective = "";
176 TextSectionStartSuffix = "\tsegment 'CODE'";
177 DataSectionStartSuffix = "\tsegment 'DATA'";
178 SectionEndDirectiveSuffix = "\tends\n";
179 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000180
181 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000182}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000183
Chris Lattner625bd052006-11-29 01:14:06 +0000184bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
185 // FIXME: this should verify that we are targetting a 486 or better. If not,
186 // we will turn this bswap into something that will be lowered to logical ops
187 // instead of emitting the bswap asm. For now, we don't support 486 or lower
188 // so don't worry about this.
189
190 // Verify this is a simple bswap.
191 if (CI->getNumOperands() != 2 ||
192 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000193 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000194 return false;
195
Reid Spencer47857812006-12-31 05:55:36 +0000196 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000197 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000198 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
199 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000200 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000201 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000202 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000203 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000204 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000205 IntName = "llvm.bswap.i64";
206 else
207 return false;
208 } else
209 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000210
211 // Okay, we can do this xform, do so now.
212 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000213 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000214
215 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000216 Op = new CallInst(Int, Op, CI->getName(), CI);
217
Chris Lattner625bd052006-11-29 01:14:06 +0000218 CI->replaceAllUsesWith(Op);
219 CI->eraseFromParent();
220 return true;
221}
222
223
224bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
225 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000226 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000227
228 std::string AsmStr = IA->getAsmString();
229
230 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
231 std::vector<std::string> AsmPieces;
232 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
233
234 switch (AsmPieces.size()) {
235 default: return false;
236 case 1:
237 AsmStr = AsmPieces[0];
238 AsmPieces.clear();
239 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
240
Chris Lattner5d521352006-11-29 01:48:01 +0000241 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000242 if (AsmPieces.size() == 2 &&
243 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
244 // No need to check constraints, nothing other than the equivalent of
245 // "=r,0" would be valid here.
246 return LowerToBSwap(CI);
247 }
248 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000249 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000250 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000251 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
252 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
253 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
254 std::vector<std::string> Words;
255 SplitString(AsmPieces[0], Words, " \t");
256 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
257 Words.clear();
258 SplitString(AsmPieces[1], Words, " \t");
259 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
260 Words.clear();
261 SplitString(AsmPieces[2], Words, " \t,");
262 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
263 Words[2] == "%edx") {
264 return LowerToBSwap(CI);
265 }
266 }
267 }
268 }
269 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000270 }
271 return false;
272}