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/InlineAsm.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/StringExtras.h"
23 static const char* x86_asm_table[] = {"{si}", "S",
34 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
35 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
37 // FIXME - Should be simplified.
39 AsmTransCBE = x86_asm_table;
41 switch (Subtarget->TargetType) {
42 case X86Subtarget::isDarwin:
43 AlignmentIsInBytes = false;
45 if (!Subtarget->is64Bit())
46 Data64bitsDirective = 0; // we can't emit a 64-bit unit
47 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
48 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
49 ConstantPoolSection = "\t.const\n";
50 JumpTableDataSection = "\t.const\n";
51 CStringSection = "\t.cstring";
52 FourByteConstantSection = "\t.literal4\n";
53 EightByteConstantSection = "\t.literal8\n";
54 if (Subtarget->is64Bit())
55 SixteenByteConstantSection = "\t.literal16\n";
56 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";
64 UsedDirective = "\t.no_dead_strip\t";
65 WeakRefDirective = "\t.weak_reference\t";
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";
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 = ".L";
90 WeakRefDirective = "\t.weak\t";
91 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";
105 case X86Subtarget::isCygwin:
106 case X86Subtarget::isMingw:
108 LCOMMDirective = "\t.lcomm\t";
109 COMMDirectiveTakesAlignment = false;
110 HasDotTypeDotSizeDirective = false;
111 StaticCtorsSection = "\t.section .ctors,\"aw\"";
112 StaticDtorsSection = "\t.section .dtors,\"aw\"";
114 // Set up DWARF directives
115 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
116 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
117 DwarfRequiresFrameSection = false;
118 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
119 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
120 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
121 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
122 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
123 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
124 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
125 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
126 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
127 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
128 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
132 case X86Subtarget::isWindows:
134 HasDotTypeDotSizeDirective = false;
139 if (Subtarget->isFlavorIntel()) {
143 PrivateGlobalPrefix = "$";
144 AlignDirective = "\talign\t";
145 ZeroDirective = "\tdb\t";
146 ZeroDirectiveSuffix = " dup(0)";
147 AsciiDirective = "\tdb\t";
149 Data8bitsDirective = "\tdb\t";
150 Data16bitsDirective = "\tdw\t";
151 Data32bitsDirective = "\tdd\t";
152 Data64bitsDirective = "\tdq\t";
153 HasDotTypeDotSizeDirective = false;
155 TextSection = "_text";
156 DataSection = "_data";
157 JumpTableDataSection = NULL;
158 SwitchToSectionDirective = "";
159 TextSectionStartSuffix = "\tsegment 'CODE'";
160 DataSectionStartSuffix = "\tsegment 'DATA'";
161 SectionEndDirectiveSuffix = "\tends\n";
165 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
166 // FIXME: this should verify that we are targetting a 486 or better. If not,
167 // we will turn this bswap into something that will be lowered to logical ops
168 // instead of emitting the bswap asm. For now, we don't support 486 or lower
169 // so don't worry about this.
171 // Verify this is a simple bswap.
172 if (CI->getNumOperands() != 2 ||
173 CI->getType() != CI->getOperand(1)->getType() ||
174 !CI->getType()->isInteger())
177 const Type *Ty = CI->getType();
179 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
180 unsigned BitWidth = ITy->getBitWidth();
181 if (BitWidth > 8 && BitWidth <= 16)
182 IntName = "llvm.bswap.i16";
183 else if (BitWidth > 24 && BitWidth <= 32)
184 IntName = "llvm.bswap.i32";
185 else if (BitWidth > 56 && BitWidth <= 64)
186 IntName = "llvm.bswap.i64";
192 // Okay, we can do this xform, do so now.
193 Module *M = CI->getParent()->getParent()->getParent();
194 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
196 Value *Op = CI->getOperand(1);
197 Op = new CallInst(Int, Op, CI->getName(), CI);
199 CI->replaceAllUsesWith(Op);
200 CI->eraseFromParent();
205 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
206 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
207 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
209 std::string AsmStr = IA->getAsmString();
211 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
212 std::vector<std::string> AsmPieces;
213 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
215 switch (AsmPieces.size()) {
216 default: return false;
218 AsmStr = AsmPieces[0];
220 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
223 if (AsmPieces.size() == 2 &&
224 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
225 // No need to check constraints, nothing other than the equivalent of
226 // "=r,0" would be valid here.
227 return LowerToBSwap(CI);
231 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
232 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
233 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
234 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
235 std::vector<std::string> Words;
236 SplitString(AsmPieces[0], Words, " \t");
237 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
239 SplitString(AsmPieces[1], Words, " \t");
240 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
242 SplitString(AsmPieces[2], Words, " \t,");
243 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
244 Words[2] == "%edx") {
245 return LowerToBSwap(CI);