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"
24 #include "llvm/Support/ErrorHandling.h"
27 using namespace llvm::dwarf;
29 const char *const llvm::x86_asm_table[] = {
41 X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
42 X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
43 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
44 bool is64Bit = Subtarget->is64Bit();
46 AlignmentIsInBytes = false;
47 TextAlignFillValue = 0x90;
51 Data64bitsDirective = 0; // we can't emit a 64-bit unit
52 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
53 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
54 if (TM.getRelocationModel() != Reloc::Static)
55 ConstantPoolSection = "\t.const_data";
57 ConstantPoolSection = "\t.const\n";
58 // FIXME: Why don't we always use this section?
60 SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
61 SectionFlags::Mergeable);
62 LCOMMDirective = "\t.lcomm\t";
64 // Leopard and above support aligned common symbols.
65 COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
66 HasDotTypeDotSizeDirective = false;
67 NonLocalEHFrameLabel = true;
70 PersonalityPrefix = "";
71 PersonalitySuffix = "+4@GOTPCREL";
73 PersonalityPrefix = "L";
74 PersonalitySuffix = "$non_lazy_ptr";
77 InlineAsmStart = "## InlineAsm Start";
78 InlineAsmEnd = "## InlineAsm End";
80 SetDirective = "\t.set";
82 UsedDirective = "\t.no_dead_strip\t";
83 ProtectedDirective = "\t.globl\t";
85 SupportsDebugInformation = true;
86 DwarfDebugInlineSection = ".section __DWARF,__debug_inlined,regular,debug";
87 DwarfUsesInlineInfoSection = true;
89 // Exceptions handling
90 SupportsExceptionHandling = true;
91 GlobalEHDirective = "\t.globl\t";
92 SupportsWeakOmittedEHFrame = false;
93 AbsoluteEHSectionOffsets = false;
95 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
97 // Leopard and earlier put exception tables in __DATA. Greater than Leopard
98 // put them in __TEXT.
99 if (Subtarget->getDarwinVers() > 9)
100 DwarfExceptionSection = ".section __TEXT,__gcc_except_tab";
102 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
106 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
108 if (Reason == DwarfEncoding::Functions && Global)
109 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
110 else if (Reason == DwarfEncoding::CodeLabels || !Global)
111 return DW_EH_PE_pcrel;
113 return DW_EH_PE_absptr;
117 X86DarwinTargetAsmInfo::getEHGlobalPrefix() const
119 const X86Subtarget* Subtarget = &TM.getSubtarget<X86Subtarget>();
120 if (Subtarget->getDarwinVers() > 9)
121 return PrivateGlobalPrefix;
126 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
127 X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
129 CStringSection = ".rodata.str";
130 PrivateGlobalPrefix = ".L";
131 WeakRefDirective = "\t.weak\t";
132 SetDirective = "\t.set\t";
135 // Set up DWARF directives
136 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
139 AbsoluteDebugSectionOffsets = true;
140 SupportsDebugInformation = true;
141 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
142 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
143 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
144 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
145 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
146 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
147 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
148 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
149 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
150 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
151 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
153 // Exceptions handling
154 SupportsExceptionHandling = true;
155 AbsoluteEHSectionOffsets = false;
156 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
157 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
159 // On Linux we must declare when we can use a non-executable stack.
160 if (TM.getSubtarget<X86Subtarget>().isLinux())
161 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
165 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
167 CodeModel::Model CM = TM.getCodeModel();
168 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
170 if (TM.getRelocationModel() == Reloc::PIC_) {
174 // 32 bit targets always encode pointers as 4 bytes
175 Format = DW_EH_PE_sdata4;
177 // 64 bit targets encode pointers in 4 bytes iff:
178 // - code model is small OR
179 // - code model is medium and we're emitting externally visible symbols
180 // or any code symbols
181 if (CM == CodeModel::Small ||
182 (CM == CodeModel::Medium && (Global ||
183 Reason != DwarfEncoding::Data)))
184 Format = DW_EH_PE_sdata4;
186 Format = DW_EH_PE_sdata8;
190 Format |= DW_EH_PE_indirect;
192 return (Format | DW_EH_PE_pcrel);
195 (CM == CodeModel::Small ||
196 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
197 return DW_EH_PE_udata4;
199 return DW_EH_PE_absptr;
203 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
204 X86GenericTargetAsmInfo(TM) {
207 LCOMMDirective = "\t.lcomm\t";
208 COMMDirectiveTakesAlignment = false;
209 HasDotTypeDotSizeDirective = false;
210 HasSingleParameterDotFile = false;
211 StaticCtorsSection = "\t.section .ctors,\"aw\"";
212 StaticDtorsSection = "\t.section .dtors,\"aw\"";
213 HiddenDirective = NULL;
214 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
215 WeakRefDirective = "\t.weak\t";
216 SetDirective = "\t.set\t";
218 // Set up DWARF directives
219 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
220 AbsoluteDebugSectionOffsets = true;
221 AbsoluteEHSectionOffsets = false;
222 SupportsDebugInformation = true;
223 DwarfSectionOffsetDirective = "\t.secrel32\t";
224 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
225 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
226 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
227 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
228 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
229 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
230 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
231 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
232 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
233 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
234 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
238 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
240 CodeModel::Model CM = TM.getCodeModel();
241 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
243 if (TM.getRelocationModel() == Reloc::PIC_) {
247 // 32 bit targets always encode pointers as 4 bytes
248 Format = DW_EH_PE_sdata4;
250 // 64 bit targets encode pointers in 4 bytes iff:
251 // - code model is small OR
252 // - code model is medium and we're emitting externally visible symbols
253 // or any code symbols
254 if (CM == CodeModel::Small ||
255 (CM == CodeModel::Medium && (Global ||
256 Reason != DwarfEncoding::Data)))
257 Format = DW_EH_PE_sdata4;
259 Format = DW_EH_PE_sdata8;
263 Format |= DW_EH_PE_indirect;
265 return (Format | DW_EH_PE_pcrel);
268 (CM == CodeModel::Small ||
269 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
270 return DW_EH_PE_udata4;
272 return DW_EH_PE_absptr;
277 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
278 SectionKind::Kind kind) const {
280 case SectionKind::Text:
281 return ".text$linkonce" + GV->getName();
282 case SectionKind::Data:
283 case SectionKind::BSS:
284 case SectionKind::ThreadData:
285 case SectionKind::ThreadBSS:
286 return ".data$linkonce" + GV->getName();
287 case SectionKind::ROData:
288 case SectionKind::RODataMergeConst:
289 case SectionKind::RODataMergeStr:
290 return ".rdata$linkonce" + GV->getName();
292 LLVM_UNREACHABLE("Unknown section kind");
297 std::string X86COFFTargetAsmInfo::printSectionFlags(unsigned flags) const {
298 std::string Flags = ",\"";
300 if (flags & SectionFlags::Code)
302 if (flags & SectionFlags::Writeable)
310 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
311 X86GenericTargetAsmInfo(TM) {
315 InlineAsmStart = "; InlineAsm Start";
316 InlineAsmEnd = "; InlineAsm End";
318 PrivateGlobalPrefix = "$";
319 AlignDirective = "\tALIGN\t";
320 ZeroDirective = "\tdb\t";
321 ZeroDirectiveSuffix = " dup(0)";
322 AsciiDirective = "\tdb\t";
324 Data8bitsDirective = "\tdb\t";
325 Data16bitsDirective = "\tdw\t";
326 Data32bitsDirective = "\tdd\t";
327 Data64bitsDirective = "\tdq\t";
328 HasDotTypeDotSizeDirective = false;
329 HasSingleParameterDotFile = false;
331 AlignmentIsInBytes = true;
333 TextSection = getUnnamedSection("_text", SectionFlags::Code);
334 DataSection = getUnnamedSection("_data", SectionFlags::Writeable);
336 JumpTableDataSection = NULL;
337 SwitchToSectionDirective = "";
338 TextSectionStartSuffix = "\tSEGMENT PARA 'CODE'";
339 DataSectionStartSuffix = "\tSEGMENT PARA 'DATA'";
340 SectionEndDirectiveSuffix = "\tends\n";
343 template <class BaseTAI>
344 bool X86TargetAsmInfo<BaseTAI>::LowerToBSwap(CallInst *CI) const {
345 // FIXME: this should verify that we are targetting a 486 or better. If not,
346 // we will turn this bswap into something that will be lowered to logical ops
347 // instead of emitting the bswap asm. For now, we don't support 486 or lower
348 // so don't worry about this.
350 // Verify this is a simple bswap.
351 if (CI->getNumOperands() != 2 ||
352 CI->getType() != CI->getOperand(1)->getType() ||
353 !CI->getType()->isInteger())
356 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
357 if (!Ty || Ty->getBitWidth() % 16 != 0)
360 // Okay, we can do this xform, do so now.
361 const Type *Tys[] = { Ty };
362 Module *M = CI->getParent()->getParent()->getParent();
363 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
365 Value *Op = CI->getOperand(1);
366 Op = CallInst::Create(Int, Op, CI->getName(), CI);
368 CI->replaceAllUsesWith(Op);
369 CI->eraseFromParent();
373 template <class BaseTAI>
374 bool X86TargetAsmInfo<BaseTAI>::ExpandInlineAsm(CallInst *CI) const {
375 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
376 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
378 std::string AsmStr = IA->getAsmString();
380 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
381 std::vector<std::string> AsmPieces;
382 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
384 switch (AsmPieces.size()) {
385 default: return false;
387 AsmStr = AsmPieces[0];
389 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
392 if (AsmPieces.size() == 2 &&
393 (AsmPieces[0] == "bswap" ||
394 AsmPieces[0] == "bswapq" ||
395 AsmPieces[0] == "bswapl") &&
396 (AsmPieces[1] == "$0" ||
397 AsmPieces[1] == "${0:q}")) {
398 // No need to check constraints, nothing other than the equivalent of
399 // "=r,0" would be valid here.
400 return LowerToBSwap(CI);
402 // rorw $$8, ${0:w} --> llvm.bswap.i16
403 if (CI->getType() == Type::Int16Ty &&
404 AsmPieces.size() == 3 &&
405 AsmPieces[0] == "rorw" &&
406 AsmPieces[1] == "$$8," &&
407 AsmPieces[2] == "${0:w}" &&
408 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
409 return LowerToBSwap(CI);
413 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
414 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
415 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
416 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
417 std::vector<std::string> Words;
418 SplitString(AsmPieces[0], Words, " \t");
419 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
421 SplitString(AsmPieces[1], Words, " \t");
422 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
424 SplitString(AsmPieces[2], Words, " \t,");
425 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
426 Words[2] == "%edx") {
427 return LowerToBSwap(CI);
437 // Instantiate default implementation.
438 TEMPLATE_INSTANTIATION(class X86TargetAsmInfo<TargetAsmInfo>);