blob: 1735220ae2c206dc078077edaa397916229291d2 [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;
62 StaticCtorsSection = ".mod_init_func";
63 StaticDtorsSection = ".mod_term_func";
64 InlineAsmStart = "# InlineAsm Start";
65 InlineAsmEnd = "# InlineAsm End";
66 SetDirective = "\t.set";
Chris Lattnera53115b2006-09-26 03:39:53 +000067 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000068 WeakRefDirective = "\t.weak_reference\t";
Chris Lattner43bbc5c2007-01-14 06:29:53 +000069 HiddenDirective = "\t.private_extern\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000070
71 NeedsSet = true;
72 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
73 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
74 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
75 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
76 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
77 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
78 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
79 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
80 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
81 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
82 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
83 break;
Reid Spencer02b85112006-10-30 22:32:30 +000084
85 case X86Subtarget::isELF:
86 // Set up DWARF directives
87 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
88 // bool HasLEB128; // Defaults to false.
89 // hasDotLoc - True if target asm supports .loc directives.
90 // bool HasDotLoc; // Defaults to false.
91 // HasDotFile - True if target asm supports .file directives.
92 // bool HasDotFile; // Defaults to false.
Chris Lattnera3bfdd42007-01-03 18:16:48 +000093 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +000094 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +000095 DwarfRequiresFrameSection = false;
96 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
97 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
98 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
99 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
100 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
101 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
102 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
103 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
104 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
105 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
106 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
107 break;
108
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000109 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000110 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000111 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000112 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000113 COMMDirectiveTakesAlignment = false;
114 HasDotTypeDotSizeDirective = false;
115 StaticCtorsSection = "\t.section .ctors,\"aw\"";
116 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000117 HiddenDirective = NULL;
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000118
119 // Set up DWARF directives
120 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
121 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
122 DwarfRequiresFrameSection = false;
123 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
124 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
125 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
126 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
127 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
128 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
129 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
130 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
131 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
132 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
133 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
134 break;
Bill Wendling11502862007-01-16 04:13:03 +0000135
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000136 case X86Subtarget::isWindows:
137 GlobalPrefix = "_";
138 HasDotTypeDotSizeDirective = false;
139 break;
Bill Wendling11502862007-01-16 04:13:03 +0000140
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000141 default: break;
142 }
143
144 if (Subtarget->isFlavorIntel()) {
145 GlobalPrefix = "_";
146 CommentString = ";";
147
148 PrivateGlobalPrefix = "$";
149 AlignDirective = "\talign\t";
150 ZeroDirective = "\tdb\t";
151 ZeroDirectiveSuffix = " dup(0)";
152 AsciiDirective = "\tdb\t";
153 AscizDirective = 0;
154 Data8bitsDirective = "\tdb\t";
155 Data16bitsDirective = "\tdw\t";
156 Data32bitsDirective = "\tdd\t";
157 Data64bitsDirective = "\tdq\t";
158 HasDotTypeDotSizeDirective = false;
159
160 TextSection = "_text";
161 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000162 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000163 SwitchToSectionDirective = "";
164 TextSectionStartSuffix = "\tsegment 'CODE'";
165 DataSectionStartSuffix = "\tsegment 'DATA'";
166 SectionEndDirectiveSuffix = "\tends\n";
167 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000168
169 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000170}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000171
Chris Lattner625bd052006-11-29 01:14:06 +0000172bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
173 // FIXME: this should verify that we are targetting a 486 or better. If not,
174 // we will turn this bswap into something that will be lowered to logical ops
175 // instead of emitting the bswap asm. For now, we don't support 486 or lower
176 // so don't worry about this.
177
178 // Verify this is a simple bswap.
179 if (CI->getNumOperands() != 2 ||
180 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000181 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000182 return false;
183
Reid Spencer47857812006-12-31 05:55:36 +0000184 const Type *Ty = CI->getType();
Chris Lattner625bd052006-11-29 01:14:06 +0000185 const char *IntName;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000186 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
187 unsigned BitWidth = ITy->getBitWidth();
Chris Lattner286bf462007-01-12 23:28:32 +0000188 if (BitWidth == 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000189 IntName = "llvm.bswap.i16";
Chris Lattner286bf462007-01-12 23:28:32 +0000190 else if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000191 IntName = "llvm.bswap.i32";
Chris Lattner286bf462007-01-12 23:28:32 +0000192 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000193 IntName = "llvm.bswap.i64";
194 else
195 return false;
196 } else
197 return false;
Chris Lattner625bd052006-11-29 01:14:06 +0000198
199 // Okay, we can do this xform, do so now.
200 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattner52a457c2007-01-07 07:24:32 +0000201 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
Chris Lattner625bd052006-11-29 01:14:06 +0000202
203 Value *Op = CI->getOperand(1);
Chris Lattner625bd052006-11-29 01:14:06 +0000204 Op = new CallInst(Int, Op, CI->getName(), CI);
205
Chris Lattner625bd052006-11-29 01:14:06 +0000206 CI->replaceAllUsesWith(Op);
207 CI->eraseFromParent();
208 return true;
209}
210
211
212bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
213 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000214 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000215
216 std::string AsmStr = IA->getAsmString();
217
218 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
219 std::vector<std::string> AsmPieces;
220 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
221
222 switch (AsmPieces.size()) {
223 default: return false;
224 case 1:
225 AsmStr = AsmPieces[0];
226 AsmPieces.clear();
227 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
228
Chris Lattner5d521352006-11-29 01:48:01 +0000229 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000230 if (AsmPieces.size() == 2 &&
231 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
232 // No need to check constraints, nothing other than the equivalent of
233 // "=r,0" would be valid here.
234 return LowerToBSwap(CI);
235 }
236 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000237 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000238 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000239 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
240 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
241 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
242 std::vector<std::string> Words;
243 SplitString(AsmPieces[0], Words, " \t");
244 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
245 Words.clear();
246 SplitString(AsmPieces[1], Words, " \t");
247 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
248 Words.clear();
249 SplitString(AsmPieces[2], Words, " \t,");
250 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
251 Words[2] == "%edx") {
252 return LowerToBSwap(CI);
253 }
254 }
255 }
256 }
257 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000258 }
259 return false;
260}