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