blob: 74977dfa5bf4e92b45d37dc785e5b29402e4f766 [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";
Evan Chengf0b5d562007-03-08 01:07:07 +000059 ReadOnlySection = "\t.const\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000060 LCOMMDirective = "\t.lcomm\t";
61 COMMDirectiveTakesAlignment = false;
62 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000063 if (TM.getRelocationModel() == Reloc::Static) {
64 StaticCtorsSection = ".constructor";
65 StaticDtorsSection = ".destructor";
66 } else {
67 StaticCtorsSection = ".mod_init_func";
68 StaticDtorsSection = ".mod_term_func";
69 }
Jim Laskey8e8de8f2006-09-07 22:05:02 +000070 InlineAsmStart = "# InlineAsm Start";
71 InlineAsmEnd = "# InlineAsm End";
72 SetDirective = "\t.set";
Chris Lattnera53115b2006-09-26 03:39:53 +000073 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000074 WeakRefDirective = "\t.weak_reference\t";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000075 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000076
Chris Lattnerb266ccd2007-01-18 01:15:58 +000077 // In non-PIC modes, emit a special label before jump tables so that the
78 // linker can perform more accurate dead code stripping.
79 if (TM.getRelocationModel() != Reloc::PIC_) {
80 // Emit a local label that is preserved until the linker runs.
81 JumpTableSpecialLabelPrefix = "l";
82 }
83
Jim Laskey8e8de8f2006-09-07 22:05:02 +000084 NeedsSet = true;
85 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
86 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
87 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
88 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
89 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
90 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
91 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
92 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
93 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
94 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
95 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
96 break;
Reid Spencer02b85112006-10-30 22:32:30 +000097
98 case X86Subtarget::isELF:
99 // Set up DWARF directives
100 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000101 AbsoluteSectionOffsets = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000102 // bool HasLEB128; // Defaults to false.
103 // hasDotLoc - True if target asm supports .loc directives.
104 // bool HasDotLoc; // Defaults to false.
105 // HasDotFile - True if target asm supports .file directives.
106 // bool HasDotFile; // Defaults to false.
Evan Chengf0b5d562007-03-08 01:07:07 +0000107 ReadOnlySection = "\t.section\t.rodata\n";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000108 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000109 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +0000110 DwarfRequiresFrameSection = false;
111 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
112 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
113 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
114 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
115 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
116 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
117 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
118 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
119 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
120 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
121 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
122 break;
123
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000124 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000125 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000126 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000127 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000128 COMMDirectiveTakesAlignment = false;
129 HasDotTypeDotSizeDirective = false;
130 StaticCtorsSection = "\t.section .ctors,\"aw\"";
131 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000132 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000133
134 // Set up DWARF directives
135 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000136 AbsoluteSectionOffsets = true;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000137 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
Anton Korobeynikov66413092007-02-23 01:58:50 +0000138 WeakRefDirective = "\t.weak\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000139 DwarfRequiresFrameSection = false;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000140 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000141 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
142 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
143 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
144 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
145 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
146 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
147 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
148 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
149 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
150 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
151 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
152 break;
Bill Wendling11502862007-01-16 04:13:03 +0000153
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000154 case X86Subtarget::isWindows:
155 GlobalPrefix = "_";
156 HasDotTypeDotSizeDirective = false;
157 break;
Bill Wendling11502862007-01-16 04:13:03 +0000158
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000159 default: break;
160 }
161
162 if (Subtarget->isFlavorIntel()) {
163 GlobalPrefix = "_";
164 CommentString = ";";
165
166 PrivateGlobalPrefix = "$";
167 AlignDirective = "\talign\t";
168 ZeroDirective = "\tdb\t";
169 ZeroDirectiveSuffix = " dup(0)";
170 AsciiDirective = "\tdb\t";
171 AscizDirective = 0;
172 Data8bitsDirective = "\tdb\t";
173 Data16bitsDirective = "\tdw\t";
174 Data32bitsDirective = "\tdd\t";
175 Data64bitsDirective = "\tdq\t";
176 HasDotTypeDotSizeDirective = false;
177
178 TextSection = "_text";
179 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000180 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000181 SwitchToSectionDirective = "";
182 TextSectionStartSuffix = "\tsegment 'CODE'";
183 DataSectionStartSuffix = "\tsegment 'DATA'";
184 SectionEndDirectiveSuffix = "\tends\n";
185 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000186
187 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000188}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000189
Chris Lattner625bd052006-11-29 01:14:06 +0000190bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
191 // FIXME: this should verify that we are targetting a 486 or better. If not,
192 // we will turn this bswap into something that will be lowered to logical ops
193 // instead of emitting the bswap asm. For now, we don't support 486 or lower
194 // so don't worry about this.
195
196 // Verify this is a simple bswap.
197 if (CI->getNumOperands() != 2 ||
198 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000199 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000200 return false;
201
Reid Spencer47857812006-12-31 05:55:36 +0000202 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000203 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000204 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
205 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000206 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000207 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000208 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000209 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000210 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000211 IntName = "llvm.bswap.i64";
212 else
213 return false;
214 } else
215 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000216
217 // Okay, we can do this xform, do so now.
218 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000219 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000220
221 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000222 Op = new CallInst(Int, Op, CI->getName(), CI);
223
Chris Lattner625bd052006-11-29 01:14:06 +0000224 CI->replaceAllUsesWith(Op);
225 CI->eraseFromParent();
226 return true;
227}
228
229
230bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
231 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000232 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000233
234 std::string AsmStr = IA->getAsmString();
235
236 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
237 std::vector<std::string> AsmPieces;
238 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
239
240 switch (AsmPieces.size()) {
241 default: return false;
242 case 1:
243 AsmStr = AsmPieces[0];
244 AsmPieces.clear();
245 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
246
Chris Lattner5d521352006-11-29 01:48:01 +0000247 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000248 if (AsmPieces.size() == 2 &&
249 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
250 // No need to check constraints, nothing other than the equivalent of
251 // "=r,0" would be valid here.
252 return LowerToBSwap(CI);
253 }
254 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000255 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000256 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000257 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
258 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
259 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
260 std::vector<std::string> Words;
261 SplitString(AsmPieces[0], Words, " \t");
262 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
263 Words.clear();
264 SplitString(AsmPieces[1], Words, " \t");
265 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
266 Words.clear();
267 SplitString(AsmPieces[2], Words, " \t,");
268 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
269 Words[2] == "%edx") {
270 return LowerToBSwap(CI);
271 }
272 }
273 }
274 }
275 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000276 }
277 return false;
278}