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 AsmTransCBE = x86_asm_table;
46 AssemblerDialect = Subtarget->getAsmFlavor();
49 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
50 // FIXME: this should verify that we are targetting a 486 or better. If not,
51 // we will turn this bswap into something that will be lowered to logical ops
52 // instead of emitting the bswap asm. For now, we don't support 486 or lower
53 // so don't worry about this.
55 // Verify this is a simple bswap.
56 if (CI->getNumOperands() != 2 ||
57 CI->getType() != CI->getOperand(1)->getType() ||
58 !CI->getType()->isInteger())
61 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
62 if (!Ty || Ty->getBitWidth() % 16 != 0)
65 // Okay, we can do this xform, do so now.
66 const Type *Tys[] = { Ty };
67 Module *M = CI->getParent()->getParent()->getParent();
68 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
70 Value *Op = CI->getOperand(1);
71 Op = CallInst::Create(Int, Op, CI->getName(), CI);
73 CI->replaceAllUsesWith(Op);
74 CI->eraseFromParent();
79 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
80 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
81 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
83 std::string AsmStr = IA->getAsmString();
85 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
86 std::vector<std::string> AsmPieces;
87 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
89 switch (AsmPieces.size()) {
90 default: return false;
92 AsmStr = AsmPieces[0];
94 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
97 if (AsmPieces.size() == 2 &&
98 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
99 // No need to check constraints, nothing other than the equivalent of
100 // "=r,0" would be valid here.
101 return LowerToBSwap(CI);
105 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
106 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
107 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
108 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
109 std::vector<std::string> Words;
110 SplitString(AsmPieces[0], Words, " \t");
111 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
113 SplitString(AsmPieces[1], Words, " \t");
114 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
116 SplitString(AsmPieces[2], Words, " \t,");
117 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
118 Words[2] == "%edx") {
119 return LowerToBSwap(CI);
129 X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
130 X86TargetAsmInfo(TM) {
131 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
133 AlignmentIsInBytes = false;
134 TextAlignFillValue = 0x90;
137 Data64bitsDirective = 0; // we can't emit a 64-bit unit
138 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
139 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
140 BSSSection = 0; // no BSS section.
141 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
142 ConstantPoolSection = "\t.const\n";
143 JumpTableDataSection = "\t.const\n";
144 CStringSection = "\t.cstring";
145 FourByteConstantSection = "\t.literal4\n";
146 EightByteConstantSection = "\t.literal8\n";
147 // FIXME: Why don't always use this section?
149 SixteenByteConstantSection = "\t.literal16\n";
150 ReadOnlySection = "\t.const\n";
151 LCOMMDirective = "\t.lcomm\t";
152 SwitchToSectionDirective = "\t.section ";
153 StringConstantPrefix = "\1LC";
154 COMMDirectiveTakesAlignment = false;
155 HasDotTypeDotSizeDirective = false;
156 if (TM.getRelocationModel() == Reloc::Static) {
157 StaticCtorsSection = ".constructor";
158 StaticDtorsSection = ".destructor";
160 StaticCtorsSection = ".mod_init_func";
161 StaticDtorsSection = ".mod_term_func";
164 PersonalityPrefix = "";
165 PersonalitySuffix = "+4@GOTPCREL";
167 PersonalityPrefix = "L";
168 PersonalitySuffix = "$non_lazy_ptr";
170 NeedsIndirectEncoding = true;
171 InlineAsmStart = "## InlineAsm Start";
172 InlineAsmEnd = "## InlineAsm End";
173 CommentString = "##";
174 SetDirective = "\t.set";
176 UsedDirective = "\t.no_dead_strip\t";
177 WeakDefDirective = "\t.weak_definition ";
178 WeakRefDirective = "\t.weak_reference ";
179 HiddenDirective = "\t.private_extern ";
180 ProtectedDirective = "\t.globl\t";
182 // In non-PIC modes, emit a special label before jump tables so that the
183 // linker can perform more accurate dead code stripping.
184 if (TM.getRelocationModel() != Reloc::PIC_) {
185 // Emit a local label that is preserved until the linker runs.
186 JumpTableSpecialLabelPrefix = "l";
189 SupportsDebugInformation = true;
191 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
192 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
193 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
194 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
195 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
196 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
197 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
198 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
199 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
200 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
201 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
203 // Exceptions handling
204 SupportsExceptionHandling = true;
205 GlobalEHDirective = "\t.globl\t";
206 SupportsWeakOmittedEHFrame = false;
207 AbsoluteEHSectionOffsets = false;
208 DwarfEHFrameSection =
209 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
210 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
214 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
216 if (Reason == DwarfEncoding::Functions && Global)
217 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
218 else if (Reason == DwarfEncoding::CodeLabels || !Global)
219 return DW_EH_PE_pcrel;
221 return DW_EH_PE_absptr;
225 X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
226 SectionKind::Kind kind) const {
227 if (kind == SectionKind::Text)
228 return "__TEXT,__textcoal_nt,coalesced,pure_instructions";
230 return "__DATA,__datacoal_nt,coalesced";
233 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
234 X86TargetAsmInfo(TM) {
235 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
237 ReadOnlySection = "\t.section\t.rodata";
238 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
239 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
240 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
241 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
242 PrivateGlobalPrefix = ".L";
243 WeakRefDirective = "\t.weak\t";
244 SetDirective = "\t.set\t";
247 // Set up DWARF directives
248 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
251 AbsoluteDebugSectionOffsets = true;
252 SupportsDebugInformation = true;
253 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
254 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
255 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
256 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
257 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
258 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
259 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
260 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
261 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
262 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
263 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
265 // Exceptions handling
267 SupportsExceptionHandling = true;
268 AbsoluteEHSectionOffsets = false;
269 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
270 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
272 // On Linux we must declare when we can use a non-executable stack.
273 if (X86TM->getSubtarget<X86Subtarget>().isLinux())
274 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
278 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
280 CodeModel::Model CM = X86TM->getCodeModel();
281 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
283 if (X86TM->getRelocationModel() == Reloc::PIC_) {
287 // 32 bit targets always encode pointers as 4 bytes
288 Format = DW_EH_PE_sdata4;
290 // 64 bit targets encode pointers in 4 bytes iff:
291 // - code model is small OR
292 // - code model is medium and we're emitting externally visible symbols
293 // or any code symbols
294 if (CM == CodeModel::Small ||
295 (CM == CodeModel::Medium && (Global ||
296 Reason != DwarfEncoding::Data)))
297 Format = DW_EH_PE_sdata4;
299 Format = DW_EH_PE_sdata8;
303 Format |= DW_EH_PE_indirect;
305 return (Format | DW_EH_PE_pcrel);
308 (CM == CodeModel::Small ||
309 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
310 return DW_EH_PE_udata4;
312 return DW_EH_PE_absptr;
316 std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
317 std::string Flags = ",\"";
319 if (!(flags & SectionFlags::Debug))
321 if (flags & SectionFlags::Code)
323 if (flags & SectionFlags::Writeable)
325 if (flags & SectionFlags::Mergeable)
327 if (flags & SectionFlags::Strings)
329 if (flags & SectionFlags::TLS)
334 // FIXME: There can be exceptions here
335 if (flags & SectionFlags::BSS)
338 Flags += ",@progbits";
340 // FIXME: entity size for mergeable sections
345 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
346 X86TargetAsmInfo(TM) {
348 LCOMMDirective = "\t.lcomm\t";
349 COMMDirectiveTakesAlignment = false;
350 HasDotTypeDotSizeDirective = false;
351 StaticCtorsSection = "\t.section .ctors,\"aw\"";
352 StaticDtorsSection = "\t.section .dtors,\"aw\"";
353 HiddenDirective = NULL;
354 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
355 WeakRefDirective = "\t.weak\t";
356 SetDirective = "\t.set\t";
358 // Set up DWARF directives
359 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
360 AbsoluteDebugSectionOffsets = true;
361 AbsoluteEHSectionOffsets = false;
362 SupportsDebugInformation = true;
363 DwarfSectionOffsetDirective = "\t.secrel32\t";
364 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
365 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
366 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
367 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
368 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
369 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
370 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
371 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
372 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
373 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
374 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
378 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
380 CodeModel::Model CM = X86TM->getCodeModel();
381 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
383 if (X86TM->getRelocationModel() == Reloc::PIC_) {
387 // 32 bit targets always encode pointers as 4 bytes
388 Format = DW_EH_PE_sdata4;
390 // 64 bit targets encode pointers in 4 bytes iff:
391 // - code model is small OR
392 // - code model is medium and we're emitting externally visible symbols
393 // or any code symbols
394 if (CM == CodeModel::Small ||
395 (CM == CodeModel::Medium && (Global ||
396 Reason != DwarfEncoding::Data)))
397 Format = DW_EH_PE_sdata4;
399 Format = DW_EH_PE_sdata8;
403 Format |= DW_EH_PE_indirect;
405 return (Format | DW_EH_PE_pcrel);
408 (CM == CodeModel::Small ||
409 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
410 return DW_EH_PE_udata4;
412 return DW_EH_PE_absptr;
417 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
418 SectionKind::Kind kind) const {
420 case SectionKind::Text:
421 return ".text$linkonce" + GV->getName();
422 case SectionKind::Data:
423 case SectionKind::BSS:
424 case SectionKind::ThreadData:
425 case SectionKind::ThreadBSS:
426 return ".data$linkonce" + GV->getName();
427 case SectionKind::ROData:
428 case SectionKind::RODataMergeConst:
429 case SectionKind::RODataMergeStr:
430 return ".rdata$linkonce" + GV->getName();
432 assert(0 && "Unknown section kind");
436 std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
437 std::string Flags = ",\"";
439 if (flags & SectionFlags::Code)
441 if (flags & SectionFlags::Writeable)
449 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
450 X86TargetAsmInfo(TM) {
454 PrivateGlobalPrefix = "$";
455 AlignDirective = "\talign\t";
456 ZeroDirective = "\tdb\t";
457 ZeroDirectiveSuffix = " dup(0)";
458 AsciiDirective = "\tdb\t";
460 Data8bitsDirective = "\tdb\t";
461 Data16bitsDirective = "\tdw\t";
462 Data32bitsDirective = "\tdd\t";
463 Data64bitsDirective = "\tdq\t";
464 HasDotTypeDotSizeDirective = false;
466 TextSection = "_text";
467 DataSection = "_data";
468 JumpTableDataSection = NULL;
469 SwitchToSectionDirective = "";
470 TextSectionStartSuffix = "\tsegment 'CODE'";
471 DataSectionStartSuffix = "\tsegment 'DATA'";
472 SectionEndDirectiveSuffix = "\tends\n";
475 std::string X86TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
476 SectionKind::Kind kind = SectionKindForGlobal(GV);
477 unsigned flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
480 // FIXME: Should we use some hashing based on section name and just check
482 // FIXME: It seems, that Darwin uses much more sections.
484 // Select section name
485 if (GV->hasSection()) {
486 // Honour section already set, if any
487 Name = GV->getSection();
489 // Use default section depending on the 'type' of global
490 if (const Function *F = dyn_cast<Function>(GV)) {
491 switch (F->getLinkage()) {
492 default: assert(0 && "Unknown linkage type!");
493 case Function::InternalLinkage:
494 case Function::DLLExportLinkage:
495 case Function::ExternalLinkage:
498 case Function::WeakLinkage:
499 case Function::LinkOnceLinkage:
500 Name = UniqueSectionForGlobal(F, kind);
503 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
504 if (GVar->hasCommonLinkage() ||
505 GVar->hasLinkOnceLinkage() ||
506 GVar->hasWeakLinkage())
507 Name = UniqueSectionForGlobal(GVar, kind);
510 case SectionKind::Data:
513 case SectionKind::BSS:
514 Name = (BSSSection ? BSSSection : DataSection);
516 case SectionKind::ROData:
517 case SectionKind::RODataMergeStr:
518 case SectionKind::RODataMergeConst:
522 case SectionKind::ThreadData:
523 Name = (TLSDataSection ? TLSDataSection : DataSection);
525 case SectionKind::ThreadBSS:
526 Name = (TLSBSSSection ? TLSBSSSection : DataSection);
528 assert(0 && "Unsuported section kind for global");
532 assert(0 && "Unsupported global");
535 Name += PrintSectionFlags(flags);