1 //===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations of the X86TargetAsmInfo properties.
12 //===----------------------------------------------------------------------===//
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/Module.h"
21 #include "llvm/ADT/StringExtras.h"
24 // ASM variant to use.
30 static const char* x86_asm_table[] = {"{si}", "S",
41 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
42 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
44 // FIXME - Should be simplified.
46 AsmTransCBE = x86_asm_table;
47 AssemblerDialect = X86_ATT;
49 switch (Subtarget->TargetType) {
50 case X86Subtarget::isDarwin:
51 AlignmentIsInBytes = false;
53 if (!Subtarget->is64Bit())
54 Data64bitsDirective = 0; // we can't emit a 64-bit unit
55 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
56 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
57 ConstantPoolSection = "\t.const\n";
58 JumpTableDataSection = "\t.const\n";
59 CStringSection = "\t.cstring";
60 FourByteConstantSection = "\t.literal4\n";
61 EightByteConstantSection = "\t.literal8\n";
62 if (Subtarget->is64Bit())
63 SixteenByteConstantSection = "\t.literal16\n";
64 LCOMMDirective = "\t.lcomm\t";
65 COMMDirectiveTakesAlignment = false;
66 HasDotTypeDotSizeDirective = false;
67 StaticCtorsSection = ".mod_init_func";
68 StaticDtorsSection = ".mod_term_func";
69 InlineAsmStart = "# InlineAsm Start";
70 InlineAsmEnd = "# InlineAsm End";
71 SetDirective = "\t.set";
72 UsedDirective = "\t.no_dead_strip\t";
73 WeakRefDirective = "\t.weak_reference\t";
74 HiddenDirective = "\t.private_extern\t";
77 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
78 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
79 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
80 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
81 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
82 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
83 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
84 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
85 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
86 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
87 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
90 case X86Subtarget::isELF:
91 // Set up DWARF directives
92 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
93 // bool HasLEB128; // Defaults to false.
94 // hasDotLoc - True if target asm supports .loc directives.
95 // bool HasDotLoc; // Defaults to false.
96 // HasDotFile - True if target asm supports .file directives.
97 // bool HasDotFile; // Defaults to false.
98 PrivateGlobalPrefix = ".L";
99 WeakRefDirective = "\t.weak\t";
100 DwarfRequiresFrameSection = false;
101 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
102 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
103 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
104 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
105 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
106 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
107 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
108 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
109 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
110 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
111 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
114 case X86Subtarget::isCygwin:
115 case X86Subtarget::isMingw:
117 LCOMMDirective = "\t.lcomm\t";
118 COMMDirectiveTakesAlignment = false;
119 HasDotTypeDotSizeDirective = false;
120 StaticCtorsSection = "\t.section .ctors,\"aw\"";
121 StaticDtorsSection = "\t.section .dtors,\"aw\"";
122 HiddenDirective = NULL;
124 // Set up DWARF directives
125 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
126 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
127 DwarfRequiresFrameSection = false;
128 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
129 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
130 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
131 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
132 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
133 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
134 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
135 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
136 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
137 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
138 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
142 case X86Subtarget::isWindows:
144 HasDotTypeDotSizeDirective = false;
149 if (Subtarget->isFlavorIntel()) {
153 PrivateGlobalPrefix = "$";
154 AlignDirective = "\talign\t";
155 ZeroDirective = "\tdb\t";
156 ZeroDirectiveSuffix = " dup(0)";
157 AsciiDirective = "\tdb\t";
159 Data8bitsDirective = "\tdb\t";
160 Data16bitsDirective = "\tdw\t";
161 Data32bitsDirective = "\tdd\t";
162 Data64bitsDirective = "\tdq\t";
163 HasDotTypeDotSizeDirective = false;
165 TextSection = "_text";
166 DataSection = "_data";
167 JumpTableDataSection = NULL;
168 SwitchToSectionDirective = "";
169 TextSectionStartSuffix = "\tsegment 'CODE'";
170 DataSectionStartSuffix = "\tsegment 'DATA'";
171 SectionEndDirectiveSuffix = "\tends\n";
175 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
176 // FIXME: this should verify that we are targetting a 486 or better. If not,
177 // we will turn this bswap into something that will be lowered to logical ops
178 // instead of emitting the bswap asm. For now, we don't support 486 or lower
179 // so don't worry about this.
181 // Verify this is a simple bswap.
182 if (CI->getNumOperands() != 2 ||
183 CI->getType() != CI->getOperand(1)->getType() ||
184 !CI->getType()->isInteger())
187 const Type *Ty = CI->getType();
189 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
190 unsigned BitWidth = ITy->getBitWidth();
192 IntName = "llvm.bswap.i16";
193 else if (BitWidth == 32)
194 IntName = "llvm.bswap.i32";
195 else if (BitWidth == 64)
196 IntName = "llvm.bswap.i64";
202 // Okay, we can do this xform, do so now.
203 Module *M = CI->getParent()->getParent()->getParent();
204 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
206 Value *Op = CI->getOperand(1);
207 Op = new CallInst(Int, Op, CI->getName(), CI);
209 CI->replaceAllUsesWith(Op);
210 CI->eraseFromParent();
215 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
216 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
217 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
219 std::string AsmStr = IA->getAsmString();
221 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
222 std::vector<std::string> AsmPieces;
223 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
225 switch (AsmPieces.size()) {
226 default: return false;
228 AsmStr = AsmPieces[0];
230 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
233 if (AsmPieces.size() == 2 &&
234 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
235 // No need to check constraints, nothing other than the equivalent of
236 // "=r,0" would be valid here.
237 return LowerToBSwap(CI);
241 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
242 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
243 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
244 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
245 std::vector<std::string> Words;
246 SplitString(AsmPieces[0], Words, " \t");
247 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
249 SplitString(AsmPieces[1], Words, " \t");
250 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
252 SplitString(AsmPieces[2], Words, " \t,");
253 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
254 Words[2] == "%edx") {
255 return LowerToBSwap(CI);