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"
23 #include "llvm/Support/Dwarf.h"
26 using namespace llvm::dwarf;
28 static const char *const x86_asm_table[] = {
40 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
41 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
44 // FIXME - Should be simplified.
46 AsmTransCBE = x86_asm_table;
48 switch (Subtarget->TargetType) {
49 case X86Subtarget::isDarwin:
50 AlignmentIsInBytes = false;
51 TextAlignFillValue = 0x90;
53 if (!Subtarget->is64Bit())
54 Data64bitsDirective = 0; // we can't emit a 64-bit unit
55 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
56 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
57 BSSSection = 0; // no BSS section.
58 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
59 ConstantPoolSection = "\t.const\n";
60 JumpTableDataSection = "\t.const\n";
61 CStringSection = "\t.cstring";
62 FourByteConstantSection = "\t.literal4\n";
63 EightByteConstantSection = "\t.literal8\n";
64 if (Subtarget->is64Bit())
65 SixteenByteConstantSection = "\t.literal16\n";
66 ReadOnlySection = "\t.const\n";
67 LCOMMDirective = "\t.lcomm\t";
68 SwitchToSectionDirective = "\t.section ";
69 COMMDirectiveTakesAlignment = false;
70 HasDotTypeDotSizeDirective = false;
71 if (TM.getRelocationModel() == Reloc::Static) {
72 StaticCtorsSection = ".constructor";
73 StaticDtorsSection = ".destructor";
75 StaticCtorsSection = ".mod_init_func";
76 StaticDtorsSection = ".mod_term_func";
78 if (Subtarget->is64Bit()) {
79 PersonalityPrefix = "";
80 PersonalitySuffix = "+4@GOTPCREL";
82 PersonalityPrefix = "L";
83 PersonalitySuffix = "$non_lazy_ptr";
85 NeedsIndirectEncoding = true;
86 InlineAsmStart = "## InlineAsm Start";
87 InlineAsmEnd = "## InlineAsm End";
89 SetDirective = "\t.set";
91 UsedDirective = "\t.no_dead_strip\t";
92 WeakDefDirective = "\t.weak_definition ";
93 WeakRefDirective = "\t.weak_reference ";
94 HiddenDirective = "\t.private_extern ";
95 ProtectedDirective = "\t.globl\t";
97 // In non-PIC modes, emit a special label before jump tables so that the
98 // linker can perform more accurate dead code stripping.
99 if (TM.getRelocationModel() != Reloc::PIC_) {
100 // Emit a local label that is preserved until the linker runs.
101 JumpTableSpecialLabelPrefix = "l";
104 SupportsDebugInformation = true;
106 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
107 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
108 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
109 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
110 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
111 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
112 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
113 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
114 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
115 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
116 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
118 // Exceptions handling
119 SupportsExceptionHandling = true;
120 GlobalEHDirective = "\t.globl\t";
121 SupportsWeakOmittedEHFrame = false;
122 AbsoluteEHSectionOffsets = false;
123 if (Subtarget->is64Bit())
124 ShortenEHDataOn64Bit = true;
125 DwarfEHFrameSection =
126 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
127 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
130 case X86Subtarget::isELF:
131 ReadOnlySection = "\t.section\t.rodata";
132 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
133 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
134 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
135 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
136 PrivateGlobalPrefix = ".L";
137 WeakRefDirective = "\t.weak\t";
138 SetDirective = "\t.set\t";
141 // Set up DWARF directives
142 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
145 AbsoluteDebugSectionOffsets = true;
146 SupportsDebugInformation = true;
147 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
148 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
149 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
150 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
151 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
152 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
153 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
154 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
155 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
156 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
157 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
159 // Exceptions handling
160 if (!Subtarget->is64Bit())
161 SupportsExceptionHandling = true;
162 AbsoluteEHSectionOffsets = false;
163 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
164 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
167 case X86Subtarget::isCygwin:
168 case X86Subtarget::isMingw:
170 LCOMMDirective = "\t.lcomm\t";
171 COMMDirectiveTakesAlignment = false;
172 HasDotTypeDotSizeDirective = false;
173 StaticCtorsSection = "\t.section .ctors,\"aw\"";
174 StaticDtorsSection = "\t.section .dtors,\"aw\"";
175 HiddenDirective = NULL;
176 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
177 WeakRefDirective = "\t.weak\t";
178 SetDirective = "\t.set\t";
180 // Set up DWARF directives
181 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
182 AbsoluteDebugSectionOffsets = true;
183 AbsoluteEHSectionOffsets = false;
184 SupportsDebugInformation = true;
185 DwarfSectionOffsetDirective = "\t.secrel32\t";
186 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
187 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
188 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
189 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
190 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
191 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
192 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
193 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
194 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
195 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
196 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
199 case X86Subtarget::isWindows:
201 HasDotTypeDotSizeDirective = false;
207 if (Subtarget->isFlavorIntel()) {
211 PrivateGlobalPrefix = "$";
212 AlignDirective = "\talign\t";
213 ZeroDirective = "\tdb\t";
214 ZeroDirectiveSuffix = " dup(0)";
215 AsciiDirective = "\tdb\t";
217 Data8bitsDirective = "\tdb\t";
218 Data16bitsDirective = "\tdw\t";
219 Data32bitsDirective = "\tdd\t";
220 Data64bitsDirective = "\tdq\t";
221 HasDotTypeDotSizeDirective = false;
223 TextSection = "_text";
224 DataSection = "_data";
225 JumpTableDataSection = NULL;
226 SwitchToSectionDirective = "";
227 TextSectionStartSuffix = "\tsegment 'CODE'";
228 DataSectionStartSuffix = "\tsegment 'DATA'";
229 SectionEndDirectiveSuffix = "\tends\n";
232 // On Linux we must declare when we can use a non-executable stack.
233 if (Subtarget->isLinux())
234 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
236 AssemblerDialect = Subtarget->getAsmFlavor();
239 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
240 // FIXME: this should verify that we are targetting a 486 or better. If not,
241 // we will turn this bswap into something that will be lowered to logical ops
242 // instead of emitting the bswap asm. For now, we don't support 486 or lower
243 // so don't worry about this.
245 // Verify this is a simple bswap.
246 if (CI->getNumOperands() != 2 ||
247 CI->getType() != CI->getOperand(1)->getType() ||
248 !CI->getType()->isInteger())
251 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
252 if (!Ty || Ty->getBitWidth() % 16 != 0)
255 // Okay, we can do this xform, do so now.
256 const Type *Tys[] = { Ty };
257 Module *M = CI->getParent()->getParent()->getParent();
258 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
260 Value *Op = CI->getOperand(1);
261 Op = CallInst::Create(Int, Op, CI->getName(), CI);
263 CI->replaceAllUsesWith(Op);
264 CI->eraseFromParent();
269 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
270 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
271 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
273 std::string AsmStr = IA->getAsmString();
275 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
276 std::vector<std::string> AsmPieces;
277 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
279 switch (AsmPieces.size()) {
280 default: return false;
282 AsmStr = AsmPieces[0];
284 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
287 if (AsmPieces.size() == 2 &&
288 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
289 // No need to check constraints, nothing other than the equivalent of
290 // "=r,0" would be valid here.
291 return LowerToBSwap(CI);
295 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
296 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
297 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
298 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
299 std::vector<std::string> Words;
300 SplitString(AsmPieces[0], Words, " \t");
301 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
303 SplitString(AsmPieces[1], Words, " \t");
304 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
306 SplitString(AsmPieces[2], Words, " \t,");
307 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
308 Words[2] == "%edx") {
309 return LowerToBSwap(CI);
319 /// PreferredEHDataFormat - This hook allows the target to select data
320 /// format used for encoding pointers in exception handling data. Reason is
321 /// 0 for data, 1 for code labels, 2 for function pointers. Global is true
322 /// if the symbol can be relocated.
323 unsigned X86TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
325 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
327 switch (Subtarget->TargetType) {
328 case X86Subtarget::isDarwin:
329 if (Reason == DwarfEncoding::Functions && Global)
330 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
331 else if (Reason == DwarfEncoding::CodeLabels || !Global)
332 return DW_EH_PE_pcrel;
334 return DW_EH_PE_absptr;
336 case X86Subtarget::isELF:
337 case X86Subtarget::isCygwin:
338 case X86Subtarget::isMingw: {
339 CodeModel::Model CM = X86TM->getCodeModel();
341 if (X86TM->getRelocationModel() == Reloc::PIC_) {
344 if (!Subtarget->is64Bit())
345 // 32 bit targets always encode pointers as 4 bytes
346 Format = DW_EH_PE_sdata4;
348 // 64 bit targets encode pointers in 4 bytes iff:
349 // - code model is small OR
350 // - code model is medium and we're emitting externally visible symbols or
352 if (CM == CodeModel::Small ||
353 (CM == CodeModel::Medium && (Global ||
354 Reason != DwarfEncoding::Data)))
355 Format = DW_EH_PE_sdata4;
357 Format = DW_EH_PE_sdata8;
361 Format |= DW_EH_PE_indirect;
363 return (Format | DW_EH_PE_pcrel);
365 if (Subtarget->is64Bit() &&
366 (CM == CodeModel::Small ||
367 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
368 return DW_EH_PE_udata4;
370 return DW_EH_PE_absptr;
375 return TargetAsmInfo::PreferredEHDataFormat(Reason, Global);