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/Intrinsics.h"
21 #include "llvm/Module.h"
22 #include "llvm/ADT/StringExtras.h"
25 static const char* x86_asm_table[] = {"{si}", "S",
36 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
37 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
39 // FIXME - Should be simplified.
41 AsmTransCBE = x86_asm_table;
43 switch (Subtarget->TargetType) {
44 case X86Subtarget::isDarwin:
45 AlignmentIsInBytes = false;
47 if (!Subtarget->is64Bit())
48 Data64bitsDirective = 0; // we can't emit a 64-bit unit
49 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
50 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
51 BSSSection = 0; // no BSS section.
52 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
53 ConstantPoolSection = "\t.const\n";
54 JumpTableDataSection = "\t.const\n";
55 CStringSection = "\t.cstring";
56 FourByteConstantSection = "\t.literal4\n";
57 EightByteConstantSection = "\t.literal8\n";
58 if (Subtarget->is64Bit())
59 SixteenByteConstantSection = "\t.literal16\n";
60 ReadOnlySection = "\t.const\n";
61 LCOMMDirective = "\t.lcomm\t";
62 COMMDirectiveTakesAlignment = false;
63 HasDotTypeDotSizeDirective = false;
64 if (TM.getRelocationModel() == Reloc::Static) {
65 StaticCtorsSection = ".constructor";
66 StaticDtorsSection = ".destructor";
68 StaticCtorsSection = ".mod_init_func";
69 StaticDtorsSection = ".mod_term_func";
71 InlineAsmStart = "# InlineAsm Start";
72 InlineAsmEnd = "# InlineAsm End";
73 SetDirective = "\t.set";
74 UsedDirective = "\t.no_dead_strip\t";
75 WeakRefDirective = "\t.weak_reference\t";
76 HiddenDirective = "\t.private_extern\t";
78 // In non-PIC modes, emit a special label before jump tables so that the
79 // linker can perform more accurate dead code stripping.
80 if (TM.getRelocationModel() != Reloc::PIC_) {
81 // Emit a local label that is preserved until the linker runs.
82 JumpTableSpecialLabelPrefix = "l";
86 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
87 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
88 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
89 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
90 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
91 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
92 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
93 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
94 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
95 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
96 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
99 case X86Subtarget::isELF:
100 // Set up DWARF directives
101 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
102 AbsoluteSectionOffsets = true;
103 // bool HasLEB128; // Defaults to false.
104 // hasDotLoc - True if target asm supports .loc directives.
105 // bool HasDotLoc; // Defaults to false.
106 // HasDotFile - True if target asm supports .file directives.
107 // bool HasDotFile; // Defaults to false.
108 ReadOnlySection = "\t.section\t.rodata\n";
109 PrivateGlobalPrefix = ".L";
110 WeakRefDirective = "\t.weak\t";
111 SetDirective = "\t.set\t";
112 DwarfRequiresFrameSection = false;
113 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
114 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
115 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
116 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
117 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
118 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
119 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
120 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
121 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
122 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
123 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
125 SupportsExceptionHandling = true;
126 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
127 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
130 case X86Subtarget::isCygwin:
131 case X86Subtarget::isMingw:
133 LCOMMDirective = "\t.lcomm\t";
134 COMMDirectiveTakesAlignment = false;
135 HasDotTypeDotSizeDirective = false;
136 StaticCtorsSection = "\t.section .ctors,\"aw\"";
137 StaticDtorsSection = "\t.section .dtors,\"aw\"";
138 HiddenDirective = NULL;
140 // Set up DWARF directives
141 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
142 AbsoluteSectionOffsets = true;
143 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
144 WeakRefDirective = "\t.weak\t";
145 SetDirective = "\t.set\t";
146 DwarfRequiresFrameSection = false;
147 DwarfSectionOffsetDirective = "\t.secrel32\t";
148 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
149 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
150 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
151 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
152 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
153 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
154 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
155 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
156 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
157 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
158 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
161 case X86Subtarget::isWindows:
163 HasDotTypeDotSizeDirective = false;
169 if (Subtarget->isFlavorIntel()) {
173 PrivateGlobalPrefix = "$";
174 AlignDirective = "\talign\t";
175 ZeroDirective = "\tdb\t";
176 ZeroDirectiveSuffix = " dup(0)";
177 AsciiDirective = "\tdb\t";
179 Data8bitsDirective = "\tdb\t";
180 Data16bitsDirective = "\tdw\t";
181 Data32bitsDirective = "\tdd\t";
182 Data64bitsDirective = "\tdq\t";
183 HasDotTypeDotSizeDirective = false;
185 TextSection = "_text";
186 DataSection = "_data";
187 JumpTableDataSection = NULL;
188 SwitchToSectionDirective = "";
189 TextSectionStartSuffix = "\tsegment 'CODE'";
190 DataSectionStartSuffix = "\tsegment 'DATA'";
191 SectionEndDirectiveSuffix = "\tends\n";
194 AssemblerDialect = Subtarget->getAsmFlavor();
197 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
198 // FIXME: this should verify that we are targetting a 486 or better. If not,
199 // we will turn this bswap into something that will be lowered to logical ops
200 // instead of emitting the bswap asm. For now, we don't support 486 or lower
201 // so don't worry about this.
203 // Verify this is a simple bswap.
204 if (CI->getNumOperands() != 2 ||
205 CI->getType() != CI->getOperand(1)->getType() ||
206 !CI->getType()->isInteger())
209 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
210 if (!Ty || Ty->getBitWidth() % 16 != 0)
213 // Okay, we can do this xform, do so now.
214 const Type *Tys[] = { Ty, Ty };
215 Module *M = CI->getParent()->getParent()->getParent();
216 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
218 Value *Op = CI->getOperand(1);
219 Op = new CallInst(Int, Op, CI->getName(), CI);
221 CI->replaceAllUsesWith(Op);
222 CI->eraseFromParent();
227 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
228 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
229 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
231 std::string AsmStr = IA->getAsmString();
233 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
234 std::vector<std::string> AsmPieces;
235 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
237 switch (AsmPieces.size()) {
238 default: return false;
240 AsmStr = AsmPieces[0];
242 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
245 if (AsmPieces.size() == 2 &&
246 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
247 // No need to check constraints, nothing other than the equivalent of
248 // "=r,0" would be valid here.
249 return LowerToBSwap(CI);
253 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
254 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
255 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
256 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
257 std::vector<std::string> Words;
258 SplitString(AsmPieces[0], Words, " \t");
259 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
261 SplitString(AsmPieces[1], Words, " \t");
262 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
264 SplitString(AsmPieces[2], Words, " \t,");
265 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
266 Words[2] == "%edx") {
267 return LowerToBSwap(CI);