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";
96 DwarfExceptionSection
= ".section __DATA,__gcc_except_tab";
100 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason
,
102 if (Reason
== DwarfEncoding::Functions
&& Global
)
103 return (DW_EH_PE_pcrel
| DW_EH_PE_indirect
| DW_EH_PE_sdata4
);
104 else if (Reason
== DwarfEncoding::CodeLabels
|| !Global
)
105 return DW_EH_PE_pcrel
;
107 return DW_EH_PE_absptr
;
111 X86DarwinTargetAsmInfo::getEHGlobalPrefix() const
113 const X86Subtarget
* Subtarget
= &TM
.getSubtarget
<X86Subtarget
>();
114 if (Subtarget
->getDarwinVers() > 9)
115 return PrivateGlobalPrefix
;
120 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine
&TM
):
121 X86TargetAsmInfo
<ELFTargetAsmInfo
>(TM
) {
123 CStringSection
= ".rodata.str";
124 PrivateGlobalPrefix
= ".L";
125 WeakRefDirective
= "\t.weak\t";
126 SetDirective
= "\t.set\t";
129 // Set up DWARF directives
130 HasLEB128
= true; // Target asm supports leb128 directives (little-endian)
133 AbsoluteDebugSectionOffsets
= true;
134 SupportsDebugInformation
= true;
135 DwarfAbbrevSection
= "\t.section\t.debug_abbrev,\"\",@progbits";
136 DwarfInfoSection
= "\t.section\t.debug_info,\"\",@progbits";
137 DwarfLineSection
= "\t.section\t.debug_line,\"\",@progbits";
138 DwarfFrameSection
= "\t.section\t.debug_frame,\"\",@progbits";
139 DwarfPubNamesSection
="\t.section\t.debug_pubnames,\"\",@progbits";
140 DwarfPubTypesSection
="\t.section\t.debug_pubtypes,\"\",@progbits";
141 DwarfStrSection
= "\t.section\t.debug_str,\"\",@progbits";
142 DwarfLocSection
= "\t.section\t.debug_loc,\"\",@progbits";
143 DwarfARangesSection
= "\t.section\t.debug_aranges,\"\",@progbits";
144 DwarfRangesSection
= "\t.section\t.debug_ranges,\"\",@progbits";
145 DwarfMacroInfoSection
= "\t.section\t.debug_macinfo,\"\",@progbits";
147 // Exceptions handling
148 SupportsExceptionHandling
= true;
149 AbsoluteEHSectionOffsets
= false;
150 DwarfEHFrameSection
= "\t.section\t.eh_frame,\"aw\",@progbits";
151 DwarfExceptionSection
= "\t.section\t.gcc_except_table,\"a\",@progbits";
153 // On Linux we must declare when we can use a non-executable stack.
154 if (TM
.getSubtarget
<X86Subtarget
>().isLinux())
155 NonexecutableStackDirective
= "\t.section\t.note.GNU-stack,\"\",@progbits";
159 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason
,
161 CodeModel::Model CM
= TM
.getCodeModel();
162 bool is64Bit
= TM
.getSubtarget
<X86Subtarget
>().is64Bit();
164 if (TM
.getRelocationModel() == Reloc::PIC_
) {
168 // 32 bit targets always encode pointers as 4 bytes
169 Format
= DW_EH_PE_sdata4
;
171 // 64 bit targets encode pointers in 4 bytes iff:
172 // - code model is small OR
173 // - code model is medium and we're emitting externally visible symbols
174 // or any code symbols
175 if (CM
== CodeModel::Small
||
176 (CM
== CodeModel::Medium
&& (Global
||
177 Reason
!= DwarfEncoding::Data
)))
178 Format
= DW_EH_PE_sdata4
;
180 Format
= DW_EH_PE_sdata8
;
184 Format
|= DW_EH_PE_indirect
;
186 return (Format
| DW_EH_PE_pcrel
);
189 (CM
== CodeModel::Small
||
190 (CM
== CodeModel::Medium
&& Reason
!= DwarfEncoding::Data
)))
191 return DW_EH_PE_udata4
;
193 return DW_EH_PE_absptr
;
197 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine
&TM
):
198 X86GenericTargetAsmInfo(TM
) {
201 LCOMMDirective
= "\t.lcomm\t";
202 COMMDirectiveTakesAlignment
= false;
203 HasDotTypeDotSizeDirective
= false;
204 HasSingleParameterDotFile
= false;
205 StaticCtorsSection
= "\t.section .ctors,\"aw\"";
206 StaticDtorsSection
= "\t.section .dtors,\"aw\"";
207 HiddenDirective
= NULL
;
208 PrivateGlobalPrefix
= "L"; // Prefix for private global symbols
209 WeakRefDirective
= "\t.weak\t";
210 SetDirective
= "\t.set\t";
212 // Set up DWARF directives
213 HasLEB128
= true; // Target asm supports leb128 directives (little-endian)
214 AbsoluteDebugSectionOffsets
= true;
215 AbsoluteEHSectionOffsets
= false;
216 SupportsDebugInformation
= true;
217 DwarfSectionOffsetDirective
= "\t.secrel32\t";
218 DwarfAbbrevSection
= "\t.section\t.debug_abbrev,\"dr\"";
219 DwarfInfoSection
= "\t.section\t.debug_info,\"dr\"";
220 DwarfLineSection
= "\t.section\t.debug_line,\"dr\"";
221 DwarfFrameSection
= "\t.section\t.debug_frame,\"dr\"";
222 DwarfPubNamesSection
="\t.section\t.debug_pubnames,\"dr\"";
223 DwarfPubTypesSection
="\t.section\t.debug_pubtypes,\"dr\"";
224 DwarfStrSection
= "\t.section\t.debug_str,\"dr\"";
225 DwarfLocSection
= "\t.section\t.debug_loc,\"dr\"";
226 DwarfARangesSection
= "\t.section\t.debug_aranges,\"dr\"";
227 DwarfRangesSection
= "\t.section\t.debug_ranges,\"dr\"";
228 DwarfMacroInfoSection
= "\t.section\t.debug_macinfo,\"dr\"";
232 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason
,
234 CodeModel::Model CM
= TM
.getCodeModel();
235 bool is64Bit
= TM
.getSubtarget
<X86Subtarget
>().is64Bit();
237 if (TM
.getRelocationModel() == Reloc::PIC_
) {
241 // 32 bit targets always encode pointers as 4 bytes
242 Format
= DW_EH_PE_sdata4
;
244 // 64 bit targets encode pointers in 4 bytes iff:
245 // - code model is small OR
246 // - code model is medium and we're emitting externally visible symbols
247 // or any code symbols
248 if (CM
== CodeModel::Small
||
249 (CM
== CodeModel::Medium
&& (Global
||
250 Reason
!= DwarfEncoding::Data
)))
251 Format
= DW_EH_PE_sdata4
;
253 Format
= DW_EH_PE_sdata8
;
257 Format
|= DW_EH_PE_indirect
;
259 return (Format
| DW_EH_PE_pcrel
);
262 (CM
== CodeModel::Small
||
263 (CM
== CodeModel::Medium
&& Reason
!= DwarfEncoding::Data
)))
264 return DW_EH_PE_udata4
;
266 return DW_EH_PE_absptr
;
271 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue
* GV
,
272 SectionKind::Kind kind
) const {
274 case SectionKind::Text
:
275 return ".text$linkonce" + GV
->getName();
276 case SectionKind::Data
:
277 case SectionKind::BSS
:
278 case SectionKind::ThreadData
:
279 case SectionKind::ThreadBSS
:
280 return ".data$linkonce" + GV
->getName();
281 case SectionKind::ROData
:
282 case SectionKind::RODataMergeConst
:
283 case SectionKind::RODataMergeStr
:
284 return ".rdata$linkonce" + GV
->getName();
286 llvm_unreachable("Unknown section kind");
291 std::string
X86COFFTargetAsmInfo::printSectionFlags(unsigned flags
) const {
292 std::string Flags
= ",\"";
294 if (flags
& SectionFlags::Code
)
296 if (flags
& SectionFlags::Writeable
)
304 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine
&TM
):
305 X86GenericTargetAsmInfo(TM
) {
309 InlineAsmStart
= "; InlineAsm Start";
310 InlineAsmEnd
= "; InlineAsm End";
312 PrivateGlobalPrefix
= "$";
313 AlignDirective
= "\tALIGN\t";
314 ZeroDirective
= "\tdb\t";
315 ZeroDirectiveSuffix
= " dup(0)";
316 AsciiDirective
= "\tdb\t";
318 Data8bitsDirective
= "\tdb\t";
319 Data16bitsDirective
= "\tdw\t";
320 Data32bitsDirective
= "\tdd\t";
321 Data64bitsDirective
= "\tdq\t";
322 HasDotTypeDotSizeDirective
= false;
323 HasSingleParameterDotFile
= false;
325 AlignmentIsInBytes
= true;
327 TextSection
= getUnnamedSection("_text", SectionFlags::Code
);
328 DataSection
= getUnnamedSection("_data", SectionFlags::Writeable
);
330 JumpTableDataSection
= NULL
;
331 SwitchToSectionDirective
= "";
332 TextSectionStartSuffix
= "\tSEGMENT PARA 'CODE'";
333 DataSectionStartSuffix
= "\tSEGMENT PARA 'DATA'";
334 SectionEndDirectiveSuffix
= "\tends\n";
337 template <class BaseTAI
>
338 bool X86TargetAsmInfo
<BaseTAI
>::LowerToBSwap(CallInst
*CI
) const {
339 // FIXME: this should verify that we are targetting a 486 or better. If not,
340 // we will turn this bswap into something that will be lowered to logical ops
341 // instead of emitting the bswap asm. For now, we don't support 486 or lower
342 // so don't worry about this.
344 // Verify this is a simple bswap.
345 if (CI
->getNumOperands() != 2 ||
346 CI
->getType() != CI
->getOperand(1)->getType() ||
347 !CI
->getType()->isInteger())
350 const IntegerType
*Ty
= dyn_cast
<IntegerType
>(CI
->getType());
351 if (!Ty
|| Ty
->getBitWidth() % 16 != 0)
354 // Okay, we can do this xform, do so now.
355 const Type
*Tys
[] = { Ty
};
356 Module
*M
= CI
->getParent()->getParent()->getParent();
357 Constant
*Int
= Intrinsic::getDeclaration(M
, Intrinsic::bswap
, Tys
, 1);
359 Value
*Op
= CI
->getOperand(1);
360 Op
= CallInst::Create(Int
, Op
, CI
->getName(), CI
);
362 CI
->replaceAllUsesWith(Op
);
363 CI
->eraseFromParent();
367 template <class BaseTAI
>
368 bool X86TargetAsmInfo
<BaseTAI
>::ExpandInlineAsm(CallInst
*CI
) const {
369 InlineAsm
*IA
= cast
<InlineAsm
>(CI
->getCalledValue());
370 std::vector
<InlineAsm::ConstraintInfo
> Constraints
= IA
->ParseConstraints();
372 std::string AsmStr
= IA
->getAsmString();
374 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
375 std::vector
<std::string
> AsmPieces
;
376 SplitString(AsmStr
, AsmPieces
, "\n"); // ; as separator?
378 switch (AsmPieces
.size()) {
379 default: return false;
381 AsmStr
= AsmPieces
[0];
383 SplitString(AsmStr
, AsmPieces
, " \t"); // Split with whitespace.
386 if (AsmPieces
.size() == 2 &&
387 (AsmPieces
[0] == "bswap" ||
388 AsmPieces
[0] == "bswapq" ||
389 AsmPieces
[0] == "bswapl") &&
390 (AsmPieces
[1] == "$0" ||
391 AsmPieces
[1] == "${0:q}")) {
392 // No need to check constraints, nothing other than the equivalent of
393 // "=r,0" would be valid here.
394 return LowerToBSwap(CI
);
396 // rorw $$8, ${0:w} --> llvm.bswap.i16
397 if (CI
->getType() == Type::Int16Ty
&&
398 AsmPieces
.size() == 3 &&
399 AsmPieces
[0] == "rorw" &&
400 AsmPieces
[1] == "$$8," &&
401 AsmPieces
[2] == "${0:w}" &&
402 IA
->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
403 return LowerToBSwap(CI
);
407 if (CI
->getType() == Type::Int64Ty
&& Constraints
.size() >= 2 &&
408 Constraints
[0].Codes
.size() == 1 && Constraints
[0].Codes
[0] == "A" &&
409 Constraints
[1].Codes
.size() == 1 && Constraints
[1].Codes
[0] == "0") {
410 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
411 std::vector
<std::string
> Words
;
412 SplitString(AsmPieces
[0], Words
, " \t");
413 if (Words
.size() == 2 && Words
[0] == "bswap" && Words
[1] == "%eax") {
415 SplitString(AsmPieces
[1], Words
, " \t");
416 if (Words
.size() == 2 && Words
[0] == "bswap" && Words
[1] == "%edx") {
418 SplitString(AsmPieces
[2], Words
, " \t,");
419 if (Words
.size() == 3 && Words
[0] == "xchgl" && Words
[1] == "%eax" &&
420 Words
[2] == "%edx") {
421 return LowerToBSwap(CI
);
431 // Instantiate default implementation.
432 TEMPLATE_INSTANTIATION(class X86TargetAsmInfo
<TargetAsmInfo
>);