1 //===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 SwitchToSectionDirective = "\t.section ";
63 COMMDirectiveTakesAlignment = false;
64 HasDotTypeDotSizeDirective = false;
65 if (TM.getRelocationModel() == Reloc::Static) {
66 StaticCtorsSection = ".constructor";
67 StaticDtorsSection = ".destructor";
69 StaticCtorsSection = ".mod_init_func";
70 StaticDtorsSection = ".mod_term_func";
72 if (Subtarget->is64Bit()) {
73 PersonalityPrefix = "";
74 PersonalitySuffix = "+4@GOTPCREL";
76 PersonalityPrefix = "L";
77 PersonalitySuffix = "$non_lazy_ptr";
79 NeedsIndirectEncoding = true;
80 InlineAsmStart = "# InlineAsm Start";
81 InlineAsmEnd = "# InlineAsm End";
82 SetDirective = "\t.set";
84 UsedDirective = "\t.no_dead_strip\t";
85 WeakDefDirective = "\t.weak_definition ";
86 WeakRefDirective = "\t.weak_reference ";
87 HiddenDirective = "\t.private_extern ";
89 // In non-PIC modes, emit a special label before jump tables so that the
90 // linker can perform more accurate dead code stripping.
91 if (TM.getRelocationModel() != Reloc::PIC_) {
92 // Emit a local label that is preserved until the linker runs.
93 JumpTableSpecialLabelPrefix = "l";
96 SupportsDebugInformation = true;
98 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
99 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
100 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
101 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
102 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
103 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
104 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
105 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
106 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
107 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
108 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
110 // Exceptions handling
111 SupportsExceptionHandling = true;
112 GlobalEHDirective = "\t.globl\t";
113 SupportsWeakOmittedEHFrame = false;
114 AbsoluteEHSectionOffsets = false;
115 if (Subtarget->is64Bit())
116 ShortenEHDataOn64Bit = true;
117 DwarfEHFrameSection =
118 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
119 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
122 case X86Subtarget::isELF:
123 ReadOnlySection = "\t.section\t.rodata";
124 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
125 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
126 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
127 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
128 PrivateGlobalPrefix = ".L";
129 WeakRefDirective = "\t.weak\t";
130 SetDirective = "\t.set\t";
133 // Set up DWARF directives
134 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
137 AbsoluteDebugSectionOffsets = true;
138 SupportsDebugInformation = true;
139 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
140 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
141 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
142 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
143 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
144 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
145 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
146 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
147 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
148 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
149 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
151 // Exceptions handling
152 if (!Subtarget->is64Bit())
153 SupportsExceptionHandling = true;
154 AbsoluteEHSectionOffsets = false;
155 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
156 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
159 case X86Subtarget::isCygwin:
160 case X86Subtarget::isMingw:
162 LCOMMDirective = "\t.lcomm\t";
163 COMMDirectiveTakesAlignment = false;
164 HasDotTypeDotSizeDirective = false;
165 StaticCtorsSection = "\t.section .ctors,\"aw\"";
166 StaticDtorsSection = "\t.section .dtors,\"aw\"";
167 HiddenDirective = NULL;
168 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
169 WeakRefDirective = "\t.weak\t";
170 SetDirective = "\t.set\t";
172 // Set up DWARF directives
173 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
174 AbsoluteDebugSectionOffsets = true;
175 AbsoluteEHSectionOffsets = false;
176 SupportsDebugInformation = true;
177 DwarfSectionOffsetDirective = "\t.secrel32\t";
178 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
179 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
180 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
181 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
182 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
183 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
184 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
185 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
186 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
187 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
188 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
191 case X86Subtarget::isWindows:
193 HasDotTypeDotSizeDirective = false;
199 if (Subtarget->isFlavorIntel()) {
203 PrivateGlobalPrefix = "$";
204 AlignDirective = "\talign\t";
205 ZeroDirective = "\tdb\t";
206 ZeroDirectiveSuffix = " dup(0)";
207 AsciiDirective = "\tdb\t";
209 Data8bitsDirective = "\tdb\t";
210 Data16bitsDirective = "\tdw\t";
211 Data32bitsDirective = "\tdd\t";
212 Data64bitsDirective = "\tdq\t";
213 HasDotTypeDotSizeDirective = false;
215 TextSection = "_text";
216 DataSection = "_data";
217 JumpTableDataSection = NULL;
218 SwitchToSectionDirective = "";
219 TextSectionStartSuffix = "\tsegment 'CODE'";
220 DataSectionStartSuffix = "\tsegment 'DATA'";
221 SectionEndDirectiveSuffix = "\tends\n";
224 AssemblerDialect = Subtarget->getAsmFlavor();
227 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
228 // FIXME: this should verify that we are targetting a 486 or better. If not,
229 // we will turn this bswap into something that will be lowered to logical ops
230 // instead of emitting the bswap asm. For now, we don't support 486 or lower
231 // so don't worry about this.
233 // Verify this is a simple bswap.
234 if (CI->getNumOperands() != 2 ||
235 CI->getType() != CI->getOperand(1)->getType() ||
236 !CI->getType()->isInteger())
239 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
240 if (!Ty || Ty->getBitWidth() % 16 != 0)
243 // Okay, we can do this xform, do so now.
244 const Type *Tys[] = { Ty };
245 Module *M = CI->getParent()->getParent()->getParent();
246 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
248 Value *Op = CI->getOperand(1);
249 Op = new CallInst(Int, Op, CI->getName(), CI);
251 CI->replaceAllUsesWith(Op);
252 CI->eraseFromParent();
257 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
258 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
259 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
261 std::string AsmStr = IA->getAsmString();
263 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
264 std::vector<std::string> AsmPieces;
265 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
267 switch (AsmPieces.size()) {
268 default: return false;
270 AsmStr = AsmPieces[0];
272 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
275 if (AsmPieces.size() == 2 &&
276 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
277 // No need to check constraints, nothing other than the equivalent of
278 // "=r,0" would be valid here.
279 return LowerToBSwap(CI);
283 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
284 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
285 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
286 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
287 std::vector<std::string> Words;
288 SplitString(AsmPieces[0], Words, " \t");
289 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
291 SplitString(AsmPieces[1], Words, " \t");
292 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
294 SplitString(AsmPieces[2], Words, " \t,");
295 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
296 Words[2] == "%edx") {
297 return LowerToBSwap(CI);