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 const char *const llvm::x86_asm_table[] = {
40 template <class BaseTAI>
41 bool X86TargetAsmInfo<BaseTAI>::LowerToBSwap(CallInst *CI) const {
42 // FIXME: this should verify that we are targetting a 486 or better. If not,
43 // we will turn this bswap into something that will be lowered to logical ops
44 // instead of emitting the bswap asm. For now, we don't support 486 or lower
45 // so don't worry about this.
47 // Verify this is a simple bswap.
48 if (CI->getNumOperands() != 2 ||
49 CI->getType() != CI->getOperand(1)->getType() ||
50 !CI->getType()->isInteger())
53 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
54 if (!Ty || Ty->getBitWidth() % 16 != 0)
57 // Okay, we can do this xform, do so now.
58 const Type *Tys[] = { Ty };
59 Module *M = CI->getParent()->getParent()->getParent();
60 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
62 Value *Op = CI->getOperand(1);
63 Op = CallInst::Create(Int, Op, CI->getName(), CI);
65 CI->replaceAllUsesWith(Op);
66 CI->eraseFromParent();
70 template <class BaseTAI>
71 bool X86TargetAsmInfo<BaseTAI>::ExpandInlineAsm(CallInst *CI) const {
72 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
73 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
75 std::string AsmStr = IA->getAsmString();
77 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
78 std::vector<std::string> AsmPieces;
79 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
81 switch (AsmPieces.size()) {
82 default: return false;
84 AsmStr = AsmPieces[0];
86 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
89 if (AsmPieces.size() == 2 &&
90 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
91 // No need to check constraints, nothing other than the equivalent of
92 // "=r,0" would be valid here.
93 return LowerToBSwap(CI);
97 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
98 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
99 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
100 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
101 std::vector<std::string> Words;
102 SplitString(AsmPieces[0], Words, " \t");
103 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
105 SplitString(AsmPieces[1], Words, " \t");
106 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
108 SplitString(AsmPieces[2], Words, " \t,");
109 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
110 Words[2] == "%edx") {
111 return LowerToBSwap(CI);
121 X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM):
122 X86TargetAsmInfo<DarwinTargetAsmInfo>(TM) {
123 const X86Subtarget* Subtarget = &DTM->getSubtarget<X86Subtarget>();
124 bool is64Bit = Subtarget->is64Bit();
126 AlignmentIsInBytes = false;
127 TextAlignFillValue = 0x90;
130 Data64bitsDirective = 0; // we can't emit a 64-bit unit
131 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
132 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
133 LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata
134 BSSSection = 0; // no BSS section.
135 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
136 if (DTM->getRelocationModel() != Reloc::Static)
137 ConstantPoolSection = "\t.const_data";
139 ConstantPoolSection = "\t.const\n";
140 JumpTableDataSection = "\t.const\n";
141 CStringSection = "\t.cstring";
142 // FIXME: Why don't always use this section?
144 SixteenByteConstantSection = getUnnamedSection("\t.literal16\n",
145 SectionFlags::Mergeable);
147 LCOMMDirective = "\t.lcomm\t";
148 SwitchToSectionDirective = "\t.section ";
149 StringConstantPrefix = "\1LC";
150 // Leopard and above support aligned common symbols.
151 COMMDirectiveTakesAlignment = (Subtarget->getDarwinVers() >= 9);
152 HasDotTypeDotSizeDirective = false;
153 if (TM.getRelocationModel() == Reloc::Static) {
154 StaticCtorsSection = ".constructor";
155 StaticDtorsSection = ".destructor";
157 StaticCtorsSection = ".mod_init_func";
158 StaticDtorsSection = ".mod_term_func";
161 PersonalityPrefix = "";
162 PersonalitySuffix = "+4@GOTPCREL";
164 PersonalityPrefix = "L";
165 PersonalitySuffix = "$non_lazy_ptr";
167 NeedsIndirectEncoding = true;
168 InlineAsmStart = "## InlineAsm Start";
169 InlineAsmEnd = "## InlineAsm End";
170 CommentString = "##";
171 SetDirective = "\t.set";
173 UsedDirective = "\t.no_dead_strip\t";
174 WeakDefDirective = "\t.weak_definition ";
175 WeakRefDirective = "\t.weak_reference ";
176 HiddenDirective = "\t.private_extern ";
177 ProtectedDirective = "\t.globl\t";
179 // In non-PIC modes, emit a special label before jump tables so that the
180 // linker can perform more accurate dead code stripping.
181 if (TM.getRelocationModel() != Reloc::PIC_) {
182 // Emit a local label that is preserved until the linker runs.
183 JumpTableSpecialLabelPrefix = "l";
186 SupportsDebugInformation = true;
188 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
189 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
190 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
191 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
192 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
193 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
194 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
195 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
196 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
197 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
198 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
200 // Exceptions handling
201 SupportsExceptionHandling = true;
202 GlobalEHDirective = "\t.globl\t";
203 SupportsWeakOmittedEHFrame = false;
204 AbsoluteEHSectionOffsets = false;
205 DwarfEHFrameSection =
206 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
207 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
211 X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
213 if (Reason == DwarfEncoding::Functions && Global)
214 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
215 else if (Reason == DwarfEncoding::CodeLabels || !Global)
216 return DW_EH_PE_pcrel;
218 return DW_EH_PE_absptr;
221 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
222 X86TargetAsmInfo<ELFTargetAsmInfo>(TM) {
224 CStringSection = ".rodata.str";
225 PrivateGlobalPrefix = ".L";
226 WeakRefDirective = "\t.weak\t";
227 SetDirective = "\t.set\t";
230 // Set up DWARF directives
231 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
234 AbsoluteDebugSectionOffsets = true;
235 SupportsDebugInformation = true;
236 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
237 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
238 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
239 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
240 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
241 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
242 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
243 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
244 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
245 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
246 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
248 // Exceptions handling
249 SupportsExceptionHandling = true;
250 AbsoluteEHSectionOffsets = false;
251 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
252 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
254 // On Linux we must declare when we can use a non-executable stack.
255 if (ETM->getSubtarget<X86Subtarget>().isLinux())
256 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
260 X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
262 CodeModel::Model CM = ETM->getCodeModel();
263 bool is64Bit = ETM->getSubtarget<X86Subtarget>().is64Bit();
265 if (ETM->getRelocationModel() == Reloc::PIC_) {
269 // 32 bit targets always encode pointers as 4 bytes
270 Format = DW_EH_PE_sdata4;
272 // 64 bit targets encode pointers in 4 bytes iff:
273 // - code model is small OR
274 // - code model is medium and we're emitting externally visible symbols
275 // or any code symbols
276 if (CM == CodeModel::Small ||
277 (CM == CodeModel::Medium && (Global ||
278 Reason != DwarfEncoding::Data)))
279 Format = DW_EH_PE_sdata4;
281 Format = DW_EH_PE_sdata8;
285 Format |= DW_EH_PE_indirect;
287 return (Format | DW_EH_PE_pcrel);
290 (CM == CodeModel::Small ||
291 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
292 return DW_EH_PE_udata4;
294 return DW_EH_PE_absptr;
298 X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM):
299 X86GenericTargetAsmInfo(TM) {
303 LCOMMDirective = "\t.lcomm\t";
304 COMMDirectiveTakesAlignment = false;
305 HasDotTypeDotSizeDirective = false;
306 StaticCtorsSection = "\t.section .ctors,\"aw\"";
307 StaticDtorsSection = "\t.section .dtors,\"aw\"";
308 HiddenDirective = NULL;
309 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
310 WeakRefDirective = "\t.weak\t";
311 SetDirective = "\t.set\t";
313 // Set up DWARF directives
314 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
315 AbsoluteDebugSectionOffsets = true;
316 AbsoluteEHSectionOffsets = false;
317 SupportsDebugInformation = true;
318 DwarfSectionOffsetDirective = "\t.secrel32\t";
319 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
320 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
321 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
322 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
323 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
324 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
325 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
326 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
327 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
328 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
329 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
333 X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
335 CodeModel::Model CM = X86TM->getCodeModel();
336 bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
338 if (X86TM->getRelocationModel() == Reloc::PIC_) {
342 // 32 bit targets always encode pointers as 4 bytes
343 Format = DW_EH_PE_sdata4;
345 // 64 bit targets encode pointers in 4 bytes iff:
346 // - code model is small OR
347 // - code model is medium and we're emitting externally visible symbols
348 // or any code symbols
349 if (CM == CodeModel::Small ||
350 (CM == CodeModel::Medium && (Global ||
351 Reason != DwarfEncoding::Data)))
352 Format = DW_EH_PE_sdata4;
354 Format = DW_EH_PE_sdata8;
358 Format |= DW_EH_PE_indirect;
360 return (Format | DW_EH_PE_pcrel);
363 (CM == CodeModel::Small ||
364 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
365 return DW_EH_PE_udata4;
367 return DW_EH_PE_absptr;
372 X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
373 SectionKind::Kind kind) const {
375 case SectionKind::Text:
376 return ".text$linkonce" + GV->getName();
377 case SectionKind::Data:
378 case SectionKind::BSS:
379 case SectionKind::ThreadData:
380 case SectionKind::ThreadBSS:
381 return ".data$linkonce" + GV->getName();
382 case SectionKind::ROData:
383 case SectionKind::RODataMergeConst:
384 case SectionKind::RODataMergeStr:
385 return ".rdata$linkonce" + GV->getName();
387 assert(0 && "Unknown section kind");
391 std::string X86COFFTargetAsmInfo::printSectionFlags(unsigned flags) const {
392 std::string Flags = ",\"";
394 if (flags & SectionFlags::Code)
396 if (flags & SectionFlags::Writeable)
404 X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM):
405 X86GenericTargetAsmInfo(TM) {
409 PrivateGlobalPrefix = "$";
410 AlignDirective = "\talign\t";
411 ZeroDirective = "\tdb\t";
412 ZeroDirectiveSuffix = " dup(0)";
413 AsciiDirective = "\tdb\t";
415 Data8bitsDirective = "\tdb\t";
416 Data16bitsDirective = "\tdw\t";
417 Data32bitsDirective = "\tdd\t";
418 Data64bitsDirective = "\tdq\t";
419 HasDotTypeDotSizeDirective = false;
421 TextSection = getUnnamedSection("_text", SectionFlags::Code);
422 DataSection = getUnnamedSection("_data", SectionFlags::Writeable);
424 JumpTableDataSection = NULL;
425 SwitchToSectionDirective = "";
426 TextSectionStartSuffix = "\tsegment 'CODE'";
427 DataSectionStartSuffix = "\tsegment 'DATA'";
428 SectionEndDirectiveSuffix = "\tends\n";