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 PersonalityPrefix = "L";
72 PersonalitySuffix = "$non_lazy_ptr";
73 NeedsIndirectEncoding = true;
74 InlineAsmStart = "# InlineAsm Start";
75 InlineAsmEnd = "# InlineAsm End";
76 SetDirective = "\t.set";
78 UsedDirective = "\t.no_dead_strip\t";
79 WeakRefDirective = "\t.weak_reference\t";
80 HiddenDirective = "\t.private_extern\t";
82 // In non-PIC modes, emit a special label before jump tables so that the
83 // linker can perform more accurate dead code stripping.
84 if (TM.getRelocationModel() != Reloc::PIC_) {
85 // Emit a local label that is preserved until the linker runs.
86 JumpTableSpecialLabelPrefix = "l";
89 SupportsDebugInformation = true;
91 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
92 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
93 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
94 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
95 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
96 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
97 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
98 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
99 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
100 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
101 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
103 // Exceptions handling
104 SupportsExceptionHandling = true;
105 AbsoluteEHSectionOffsets = false;
106 DwarfEHFrameSection =
107 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
108 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
111 case X86Subtarget::isELF:
112 ReadOnlySection = "\t.section\t.rodata";
113 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
114 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
115 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
116 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
117 PrivateGlobalPrefix = ".L";
118 WeakRefDirective = "\t.weak\t";
119 SetDirective = "\t.set\t";
122 // Set up DWARF directives
123 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
126 AbsoluteDebugSectionOffsets = true;
127 SupportsDebugInformation = true;
128 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
129 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
130 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
131 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
132 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
133 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
134 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
135 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
136 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
137 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
138 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
140 // Exceptions handling
141 if (!Subtarget->is64Bit())
142 SupportsExceptionHandling = true;
143 AbsoluteEHSectionOffsets = false;
144 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
145 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
148 case X86Subtarget::isCygwin:
149 case X86Subtarget::isMingw:
151 LCOMMDirective = "\t.lcomm\t";
152 COMMDirectiveTakesAlignment = false;
153 HasDotTypeDotSizeDirective = false;
154 StaticCtorsSection = "\t.section .ctors,\"aw\"";
155 StaticDtorsSection = "\t.section .dtors,\"aw\"";
156 HiddenDirective = NULL;
157 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
158 WeakRefDirective = "\t.weak\t";
159 SetDirective = "\t.set\t";
161 // Set up DWARF directives
162 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
163 AbsoluteDebugSectionOffsets = true;
164 AbsoluteEHSectionOffsets = false;
165 SupportsDebugInformation = true;
166 DwarfSectionOffsetDirective = "\t.secrel32\t";
167 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
168 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
169 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
170 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
171 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
172 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
173 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
174 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
175 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
176 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
177 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
180 case X86Subtarget::isWindows:
182 HasDotTypeDotSizeDirective = false;
188 if (Subtarget->isFlavorIntel()) {
192 PrivateGlobalPrefix = "$";
193 AlignDirective = "\talign\t";
194 ZeroDirective = "\tdb\t";
195 ZeroDirectiveSuffix = " dup(0)";
196 AsciiDirective = "\tdb\t";
198 Data8bitsDirective = "\tdb\t";
199 Data16bitsDirective = "\tdw\t";
200 Data32bitsDirective = "\tdd\t";
201 Data64bitsDirective = "\tdq\t";
202 HasDotTypeDotSizeDirective = false;
204 TextSection = "_text";
205 DataSection = "_data";
206 JumpTableDataSection = NULL;
207 SwitchToSectionDirective = "";
208 TextSectionStartSuffix = "\tsegment 'CODE'";
209 DataSectionStartSuffix = "\tsegment 'DATA'";
210 SectionEndDirectiveSuffix = "\tends\n";
213 AssemblerDialect = Subtarget->getAsmFlavor();
216 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
217 // FIXME: this should verify that we are targetting a 486 or better. If not,
218 // we will turn this bswap into something that will be lowered to logical ops
219 // instead of emitting the bswap asm. For now, we don't support 486 or lower
220 // so don't worry about this.
222 // Verify this is a simple bswap.
223 if (CI->getNumOperands() != 2 ||
224 CI->getType() != CI->getOperand(1)->getType() ||
225 !CI->getType()->isInteger())
228 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
229 if (!Ty || Ty->getBitWidth() % 16 != 0)
232 // Okay, we can do this xform, do so now.
233 const Type *Tys[] = { Ty };
234 Module *M = CI->getParent()->getParent()->getParent();
235 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
237 Value *Op = CI->getOperand(1);
238 Op = new CallInst(Int, Op, CI->getName(), CI);
240 CI->replaceAllUsesWith(Op);
241 CI->eraseFromParent();
246 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
247 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
248 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
250 std::string AsmStr = IA->getAsmString();
252 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
253 std::vector<std::string> AsmPieces;
254 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
256 switch (AsmPieces.size()) {
257 default: return false;
259 AsmStr = AsmPieces[0];
261 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
264 if (AsmPieces.size() == 2 &&
265 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
266 // No need to check constraints, nothing other than the equivalent of
267 // "=r,0" would be valid here.
268 return LowerToBSwap(CI);
272 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
273 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
274 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
275 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
276 std::vector<std::string> Words;
277 SplitString(AsmPieces[0], Words, " \t");
278 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
280 SplitString(AsmPieces[1], Words, " \t");
281 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
283 SplitString(AsmPieces[2], Words, " \t,");
284 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
285 Words[2] == "%edx") {
286 return LowerToBSwap(CI);