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 static const char* x86_asm_table[] = {"{si}", "S",
35 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
36 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
38 // FIXME - Should be simplified.
40 AsmTransCBE = x86_asm_table;
42 switch (Subtarget->TargetType) {
43 case X86Subtarget::isDarwin:
44 AlignmentIsInBytes = false;
46 if (!Subtarget->is64Bit())
47 Data64bitsDirective = 0; // we can't emit a 64-bit unit
48 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
49 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
50 ConstantPoolSection = "\t.const\n";
51 JumpTableDataSection = "\t.const\n";
52 CStringSection = "\t.cstring";
53 FourByteConstantSection = "\t.literal4\n";
54 EightByteConstantSection = "\t.literal8\n";
55 if (Subtarget->is64Bit())
56 SixteenByteConstantSection = "\t.literal16\n";
57 LCOMMDirective = "\t.lcomm\t";
58 COMMDirectiveTakesAlignment = false;
59 HasDotTypeDotSizeDirective = false;
60 StaticCtorsSection = ".mod_init_func";
61 StaticDtorsSection = ".mod_term_func";
62 InlineAsmStart = "# InlineAsm Start";
63 InlineAsmEnd = "# InlineAsm End";
64 SetDirective = "\t.set";
65 UsedDirective = "\t.no_dead_strip\t";
66 WeakRefDirective = "\t.weak_reference\t";
67 HiddenDirective = "\t.private_extern\t";
70 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
71 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
72 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
73 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
74 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
75 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
76 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
77 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
78 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
79 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
80 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
83 case X86Subtarget::isELF:
84 // Set up DWARF directives
85 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
86 // bool HasLEB128; // Defaults to false.
87 // hasDotLoc - True if target asm supports .loc directives.
88 // bool HasDotLoc; // Defaults to false.
89 // HasDotFile - True if target asm supports .file directives.
90 // bool HasDotFile; // Defaults to false.
91 PrivateGlobalPrefix = ".L";
92 WeakRefDirective = "\t.weak\t";
93 DwarfRequiresFrameSection = false;
94 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
95 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
96 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
97 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
98 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
99 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
100 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
101 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
102 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
103 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
104 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
107 case X86Subtarget::isCygwin:
108 case X86Subtarget::isMingw:
110 LCOMMDirective = "\t.lcomm\t";
111 COMMDirectiveTakesAlignment = false;
112 HasDotTypeDotSizeDirective = false;
113 StaticCtorsSection = "\t.section .ctors,\"aw\"";
114 StaticDtorsSection = "\t.section .dtors,\"aw\"";
115 HiddenDirective = NULL;
117 // Set up DWARF directives
118 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
119 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
120 DwarfRequiresFrameSection = false;
121 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
122 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
123 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
124 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
125 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
126 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
127 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
128 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
129 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
130 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
131 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
134 case X86Subtarget::isWindows:
136 HasDotTypeDotSizeDirective = false;
142 if (Subtarget->isFlavorIntel()) {
146 PrivateGlobalPrefix = "$";
147 AlignDirective = "\talign\t";
148 ZeroDirective = "\tdb\t";
149 ZeroDirectiveSuffix = " dup(0)";
150 AsciiDirective = "\tdb\t";
152 Data8bitsDirective = "\tdb\t";
153 Data16bitsDirective = "\tdw\t";
154 Data32bitsDirective = "\tdd\t";
155 Data64bitsDirective = "\tdq\t";
156 HasDotTypeDotSizeDirective = false;
158 TextSection = "_text";
159 DataSection = "_data";
160 JumpTableDataSection = NULL;
161 SwitchToSectionDirective = "";
162 TextSectionStartSuffix = "\tsegment 'CODE'";
163 DataSectionStartSuffix = "\tsegment 'DATA'";
164 SectionEndDirectiveSuffix = "\tends\n";
167 AssemblerDialect = Subtarget->getAsmFlavor();
170 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
171 // FIXME: this should verify that we are targetting a 486 or better. If not,
172 // we will turn this bswap into something that will be lowered to logical ops
173 // instead of emitting the bswap asm. For now, we don't support 486 or lower
174 // so don't worry about this.
176 // Verify this is a simple bswap.
177 if (CI->getNumOperands() != 2 ||
178 CI->getType() != CI->getOperand(1)->getType() ||
179 !CI->getType()->isInteger())
182 const Type *Ty = CI->getType();
184 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
185 unsigned BitWidth = ITy->getBitWidth();
187 IntName = "llvm.bswap.i16";
188 else if (BitWidth == 32)
189 IntName = "llvm.bswap.i32";
190 else if (BitWidth == 64)
191 IntName = "llvm.bswap.i64";
197 // Okay, we can do this xform, do so now.
198 Module *M = CI->getParent()->getParent()->getParent();
199 Constant *Int = M->getOrInsertFunction(IntName, Ty, Ty, (Type*)0);
201 Value *Op = CI->getOperand(1);
202 Op = new CallInst(Int, Op, CI->getName(), CI);
204 CI->replaceAllUsesWith(Op);
205 CI->eraseFromParent();
210 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
211 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
212 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
214 std::string AsmStr = IA->getAsmString();
216 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
217 std::vector<std::string> AsmPieces;
218 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
220 switch (AsmPieces.size()) {
221 default: return false;
223 AsmStr = AsmPieces[0];
225 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
228 if (AsmPieces.size() == 2 &&
229 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
230 // No need to check constraints, nothing other than the equivalent of
231 // "=r,0" would be valid here.
232 return LowerToBSwap(CI);
236 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
237 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
238 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
239 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
240 std::vector<std::string> Words;
241 SplitString(AsmPieces[0], Words, " \t");
242 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
244 SplitString(AsmPieces[1], Words, " \t");
245 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
247 SplitString(AsmPieces[2], Words, " \t,");
248 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
249 Words[2] == "%edx") {
250 return LowerToBSwap(CI);