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