blob: ed9f1ac313278fefdb6c3a0f2da9b96541f687e6 [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"
Chris Lattner625bd052006-11-29 01:14:06 +000017#include "llvm/InlineAsm.h"
18#include "llvm/Instructions.h"
19#include "llvm/Module.h"
20#include "llvm/ADT/StringExtras.h"
Jim Laskey8e8de8f2006-09-07 22:05:02 +000021using namespace llvm;
22
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000023static const char* x86_asm_table[] = {"{si}", "S",
24 "{di}", "D",
25 "{ax}", "a",
26 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000027 "{memory}", "memory",
28 "{flags}", "",
29 "{dirflag}", "",
30 "{fpsr}", "",
31 "{cc}", "cc",
32 0,0};
33
Jim Laskey8e8de8f2006-09-07 22:05:02 +000034X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
35 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
36
Chris Lattnerafbfded2006-10-05 02:43:52 +000037 // FIXME - Should be simplified.
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000038
39 AsmTransCBE = x86_asm_table;
40
Jim Laskey8e8de8f2006-09-07 22:05:02 +000041 switch (Subtarget->TargetType) {
42 case X86Subtarget::isDarwin:
43 AlignmentIsInBytes = false;
44 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000045 if (!Subtarget->is64Bit())
46 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000047 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
48 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
49 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000050 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000051 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000052 FourByteConstantSection = "\t.literal4\n";
53 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000054 if (Subtarget->is64Bit())
55 SixteenByteConstantSection = "\t.literal16\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000056 LCOMMDirective = "\t.lcomm\t";
57 COMMDirectiveTakesAlignment = false;
58 HasDotTypeDotSizeDirective = false;
59 StaticCtorsSection = ".mod_init_func";
60 StaticDtorsSection = ".mod_term_func";
61 InlineAsmStart = "# InlineAsm Start";
62 InlineAsmEnd = "# InlineAsm End";
63 SetDirective = "\t.set";
Chris Lattnera53115b2006-09-26 03:39:53 +000064 UsedDirective = "\t.no_dead_strip\t";
Evan Cheng8752ce62006-12-01 20:47:11 +000065 WeakRefDirective = "\t.weak_reference\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000066
67 NeedsSet = true;
68 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
69 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
70 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
71 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
72 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
73 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
74 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
75 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
76 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
77 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
78 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
79 break;
Reid Spencer02b85112006-10-30 22:32:30 +000080
81 case X86Subtarget::isELF:
82 // Set up DWARF directives
83 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
84 // bool HasLEB128; // Defaults to false.
85 // hasDotLoc - True if target asm supports .loc directives.
86 // bool HasDotLoc; // Defaults to false.
87 // HasDotFile - True if target asm supports .file directives.
88 // bool HasDotFile; // Defaults to false.
89 PrivateGlobalPrefix = "."; // Prefix for private global symbols
Evan Cheng8752ce62006-12-01 20:47:11 +000090 WeakRefDirective = "\t.weak\t";
Reid Spencer02b85112006-10-30 22:32:30 +000091 DwarfRequiresFrameSection = false;
92 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
93 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
94 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
95 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
96 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
97 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
98 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
99 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
100 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
101 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
102 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
103 break;
104
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000105 case X86Subtarget::isCygwin:
106 GlobalPrefix = "_";
107 COMMDirectiveTakesAlignment = false;
108 HasDotTypeDotSizeDirective = false;
109 StaticCtorsSection = "\t.section .ctors,\"aw\"";
110 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000111
112 // Set up DWARF directives
113 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
114 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
115 DwarfRequiresFrameSection = false;
116 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
117 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
118 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
119 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
120 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
121 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
122 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
123 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
124 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
125 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
126 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
127 break;
128
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000129 break;
130 case X86Subtarget::isWindows:
131 GlobalPrefix = "_";
132 HasDotTypeDotSizeDirective = false;
133 break;
134 default: break;
135 }
136
137 if (Subtarget->isFlavorIntel()) {
138 GlobalPrefix = "_";
139 CommentString = ";";
140
141 PrivateGlobalPrefix = "$";
142 AlignDirective = "\talign\t";
143 ZeroDirective = "\tdb\t";
144 ZeroDirectiveSuffix = " dup(0)";
145 AsciiDirective = "\tdb\t";
146 AscizDirective = 0;
147 Data8bitsDirective = "\tdb\t";
148 Data16bitsDirective = "\tdw\t";
149 Data32bitsDirective = "\tdd\t";
150 Data64bitsDirective = "\tdq\t";
151 HasDotTypeDotSizeDirective = false;
152
153 TextSection = "_text";
154 DataSection = "_data";
155 SwitchToSectionDirective = "";
156 TextSectionStartSuffix = "\tsegment 'CODE'";
157 DataSectionStartSuffix = "\tsegment 'DATA'";
158 SectionEndDirectiveSuffix = "\tends\n";
159 }
160}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000161
Chris Lattner625bd052006-11-29 01:14:06 +0000162bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
163 // FIXME: this should verify that we are targetting a 486 or better. If not,
164 // we will turn this bswap into something that will be lowered to logical ops
165 // instead of emitting the bswap asm. For now, we don't support 486 or lower
166 // so don't worry about this.
167
168 // Verify this is a simple bswap.
169 if (CI->getNumOperands() != 2 ||
170 CI->getType() != CI->getOperand(1)->getType() ||
171 !CI->getType()->isInteger())
172 return false;
173
174 const Type *Ty = CI->getType()->getUnsignedVersion();
175 const char *IntName;
176 switch (Ty->getTypeID()) {
177 default: return false;
178 case Type::UShortTyID: IntName = "llvm.bswap.i16"; break;
179 case Type::UIntTyID: IntName = "llvm.bswap.i32"; break;
180 case Type::ULongTyID: IntName = "llvm.bswap.i64"; break;
181 }
182
183 // Okay, we can do this xform, do so now.
184 Module *M = CI->getParent()->getParent()->getParent();
185 Function *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
186
187 Value *Op = CI->getOperand(1);
188 if (CI->getOperand(1)->getType() != Ty)
189 Op = new BitCastInst(Op, Ty, Op->getName(), CI);
190
191 Op = new CallInst(Int, Op, CI->getName(), CI);
192
193 if (Op->getType() != CI->getType())
194 Op = new BitCastInst(Op, CI->getType(), Op->getName(), CI);
195
196 CI->replaceAllUsesWith(Op);
197 CI->eraseFromParent();
198 return true;
199}
200
201
202bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
203 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000204 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000205
206 std::string AsmStr = IA->getAsmString();
207
208 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
209 std::vector<std::string> AsmPieces;
210 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
211
212 switch (AsmPieces.size()) {
213 default: return false;
214 case 1:
215 AsmStr = AsmPieces[0];
216 AsmPieces.clear();
217 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
218
Chris Lattner5d521352006-11-29 01:48:01 +0000219 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000220 if (AsmPieces.size() == 2 &&
221 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
222 // No need to check constraints, nothing other than the equivalent of
223 // "=r,0" would be valid here.
224 return LowerToBSwap(CI);
225 }
226 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000227 case 3:
228 if (CI->getType() == Type::ULongTy && Constraints.size() >= 2 &&
229 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
230 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
231 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
232 std::vector<std::string> Words;
233 SplitString(AsmPieces[0], Words, " \t");
234 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
235 Words.clear();
236 SplitString(AsmPieces[1], Words, " \t");
237 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
238 Words.clear();
239 SplitString(AsmPieces[2], Words, " \t,");
240 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
241 Words[2] == "%edx") {
242 return LowerToBSwap(CI);
243 }
244 }
245 }
246 }
247 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000248 }
249 return false;
250}