1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to X86 machine code.
12 //===----------------------------------------------------------------------===//
14 #include "X86AsmPrinter.h"
15 #include "MCTargetDesc/X86ATTInstPrinter.h"
16 #include "MCTargetDesc/X86BaseInfo.h"
17 #include "MCTargetDesc/X86TargetStreamer.h"
18 #include "TargetInfo/X86TargetInfo.h"
19 #include "X86InstrInfo.h"
20 #include "X86MachineFunctionInfo.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
25 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/MC/MCCodeEmitter.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCExpr.h"
34 #include "llvm/MC/MCSectionCOFF.h"
35 #include "llvm/MC/MCSectionELF.h"
36 #include "llvm/MC/MCSectionMachO.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/MachineValueType.h"
42 #include "llvm/Support/TargetRegistry.h"
45 X86AsmPrinter::X86AsmPrinter(TargetMachine
&TM
,
46 std::unique_ptr
<MCStreamer
> Streamer
)
47 : AsmPrinter(TM
, std::move(Streamer
)), SM(*this), FM(*this) {}
49 //===----------------------------------------------------------------------===//
50 // Primitive Helper Functions.
51 //===----------------------------------------------------------------------===//
53 /// runOnMachineFunction - Emit the function body.
55 bool X86AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
56 Subtarget
= &MF
.getSubtarget
<X86Subtarget
>();
58 SMShadowTracker
.startFunction(MF
);
59 CodeEmitter
.reset(TM
.getTarget().createMCCodeEmitter(
60 *Subtarget
->getInstrInfo(), *Subtarget
->getRegisterInfo(),
64 Subtarget
->isTargetWin32() && MF
.getMMI().getModule()->getCodeViewFlag();
66 SetupMachineFunction(MF
);
68 if (Subtarget
->isTargetCOFF()) {
69 bool Local
= MF
.getFunction().hasLocalLinkage();
70 OutStreamer
->BeginCOFFSymbolDef(CurrentFnSym
);
71 OutStreamer
->EmitCOFFSymbolStorageClass(
72 Local
? COFF::IMAGE_SYM_CLASS_STATIC
: COFF::IMAGE_SYM_CLASS_EXTERNAL
);
73 OutStreamer
->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
74 << COFF::SCT_COMPLEX_TYPE_SHIFT
);
75 OutStreamer
->EndCOFFSymbolDef();
78 // Emit the rest of the function body.
81 // Emit the XRay table for this function.
86 // We didn't modify anything.
90 void X86AsmPrinter::EmitFunctionBodyStart() {
93 static_cast<X86TargetStreamer
*>(OutStreamer
->getTargetStreamer()))
96 MF
->getInfo
<X86MachineFunctionInfo
>()->getArgumentStackSize());
100 void X86AsmPrinter::EmitFunctionBodyEnd() {
103 static_cast<X86TargetStreamer
*>(OutStreamer
->getTargetStreamer()))
104 XTS
->emitFPOEndProc();
108 /// PrintSymbolOperand - Print a raw symbol reference operand. This handles
109 /// jump tables, constant pools, global address and external symbols, all of
110 /// which print to a label with various suffixes for relocation types etc.
111 void X86AsmPrinter::PrintSymbolOperand(const MachineOperand
&MO
,
113 switch (MO
.getType()) {
114 default: llvm_unreachable("unknown symbol type!");
115 case MachineOperand::MO_ConstantPoolIndex
:
116 GetCPISymbol(MO
.getIndex())->print(O
, MAI
);
117 printOffset(MO
.getOffset(), O
);
119 case MachineOperand::MO_GlobalAddress
: {
120 const GlobalValue
*GV
= MO
.getGlobal();
123 if (MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
124 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
)
125 GVSym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr");
127 GVSym
= getSymbol(GV
);
129 // Handle dllimport linkage.
130 if (MO
.getTargetFlags() == X86II::MO_DLLIMPORT
)
131 GVSym
= OutContext
.getOrCreateSymbol(Twine("__imp_") + GVSym
->getName());
132 else if (MO
.getTargetFlags() == X86II::MO_COFFSTUB
)
134 OutContext
.getOrCreateSymbol(Twine(".refptr.") + GVSym
->getName());
136 if (MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
137 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
) {
138 MCSymbol
*Sym
= getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr");
139 MachineModuleInfoImpl::StubValueTy
&StubSym
=
140 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>().getGVStubEntry(Sym
);
141 if (!StubSym
.getPointer())
142 StubSym
= MachineModuleInfoImpl::StubValueTy(getSymbol(GV
),
143 !GV
->hasInternalLinkage());
146 // If the name begins with a dollar-sign, enclose it in parens. We do this
147 // to avoid having it look like an integer immediate to the assembler.
148 if (GVSym
->getName()[0] != '$')
149 GVSym
->print(O
, MAI
);
152 GVSym
->print(O
, MAI
);
155 printOffset(MO
.getOffset(), O
);
160 switch (MO
.getTargetFlags()) {
162 llvm_unreachable("Unknown target flag on GV operand");
163 case X86II::MO_NO_FLAG
: // No flag.
165 case X86II::MO_DARWIN_NONLAZY
:
166 case X86II::MO_DLLIMPORT
:
167 case X86II::MO_COFFSTUB
:
168 // These affect the name of the symbol, not any suffix.
170 case X86II::MO_GOT_ABSOLUTE_ADDRESS
:
172 MF
->getPICBaseSymbol()->print(O
, MAI
);
175 case X86II::MO_PIC_BASE_OFFSET
:
176 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
178 MF
->getPICBaseSymbol()->print(O
, MAI
);
180 case X86II::MO_TLSGD
: O
<< "@TLSGD"; break;
181 case X86II::MO_TLSLD
: O
<< "@TLSLD"; break;
182 case X86II::MO_TLSLDM
: O
<< "@TLSLDM"; break;
183 case X86II::MO_GOTTPOFF
: O
<< "@GOTTPOFF"; break;
184 case X86II::MO_INDNTPOFF
: O
<< "@INDNTPOFF"; break;
185 case X86II::MO_TPOFF
: O
<< "@TPOFF"; break;
186 case X86II::MO_DTPOFF
: O
<< "@DTPOFF"; break;
187 case X86II::MO_NTPOFF
: O
<< "@NTPOFF"; break;
188 case X86II::MO_GOTNTPOFF
: O
<< "@GOTNTPOFF"; break;
189 case X86II::MO_GOTPCREL
: O
<< "@GOTPCREL"; break;
190 case X86II::MO_GOT
: O
<< "@GOT"; break;
191 case X86II::MO_GOTOFF
: O
<< "@GOTOFF"; break;
192 case X86II::MO_PLT
: O
<< "@PLT"; break;
193 case X86II::MO_TLVP
: O
<< "@TLVP"; break;
194 case X86II::MO_TLVP_PIC_BASE
:
196 MF
->getPICBaseSymbol()->print(O
, MAI
);
198 case X86II::MO_SECREL
: O
<< "@SECREL32"; break;
202 void X86AsmPrinter::PrintOperand(const MachineInstr
*MI
, unsigned OpNo
,
204 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
205 const bool IsATT
= MI
->getInlineAsmDialect() == InlineAsm::AD_ATT
;
206 switch (MO
.getType()) {
207 default: llvm_unreachable("unknown operand type!");
208 case MachineOperand::MO_Register
: {
211 O
<< X86ATTInstPrinter::getRegisterName(MO
.getReg());
215 case MachineOperand::MO_Immediate
:
221 case MachineOperand::MO_GlobalAddress
: {
224 PrintSymbolOperand(MO
, O
);
227 case MachineOperand::MO_BlockAddress
: {
228 MCSymbol
*Sym
= GetBlockAddressSymbol(MO
.getBlockAddress());
235 /// PrintModifiedOperand - Print subregisters based on supplied modifier,
236 /// deferring to PrintOperand() if no modifier was supplied or if operand is not
238 void X86AsmPrinter::PrintModifiedOperand(const MachineInstr
*MI
, unsigned OpNo
,
239 raw_ostream
&O
, const char *Modifier
) {
240 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
241 if (!Modifier
|| MO
.getType() != MachineOperand::MO_Register
)
242 return PrintOperand(MI
, OpNo
, O
);
243 if (MI
->getInlineAsmDialect() == InlineAsm::AD_ATT
)
245 Register Reg
= MO
.getReg();
246 if (strncmp(Modifier
, "subreg", strlen("subreg")) == 0) {
247 unsigned Size
= (strcmp(Modifier
+6,"64") == 0) ? 64 :
248 (strcmp(Modifier
+6,"32") == 0) ? 32 :
249 (strcmp(Modifier
+6,"16") == 0) ? 16 : 8;
250 Reg
= getX86SubSuperRegister(Reg
, Size
);
252 O
<< X86ATTInstPrinter::getRegisterName(Reg
);
255 /// PrintPCRelImm - This is used to print an immediate value that ends up
256 /// being encoded as a pc-relative value. These print slightly differently, for
257 /// example, a $ is not emitted.
258 void X86AsmPrinter::PrintPCRelImm(const MachineInstr
*MI
, unsigned OpNo
,
260 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
261 switch (MO
.getType()) {
262 default: llvm_unreachable("Unknown pcrel immediate operand");
263 case MachineOperand::MO_Register
:
264 // pc-relativeness was handled when computing the value in the reg.
265 PrintOperand(MI
, OpNo
, O
);
267 case MachineOperand::MO_Immediate
:
270 case MachineOperand::MO_GlobalAddress
:
271 PrintSymbolOperand(MO
, O
);
276 void X86AsmPrinter::PrintLeaMemReference(const MachineInstr
*MI
, unsigned OpNo
,
277 raw_ostream
&O
, const char *Modifier
) {
278 const MachineOperand
&BaseReg
= MI
->getOperand(OpNo
+ X86::AddrBaseReg
);
279 const MachineOperand
&IndexReg
= MI
->getOperand(OpNo
+ X86::AddrIndexReg
);
280 const MachineOperand
&DispSpec
= MI
->getOperand(OpNo
+ X86::AddrDisp
);
282 // If we really don't want to print out (rip), don't.
283 bool HasBaseReg
= BaseReg
.getReg() != 0;
284 if (HasBaseReg
&& Modifier
&& !strcmp(Modifier
, "no-rip") &&
285 BaseReg
.getReg() == X86::RIP
)
288 // HasParenPart - True if we will print out the () part of the mem ref.
289 bool HasParenPart
= IndexReg
.getReg() || HasBaseReg
;
291 switch (DispSpec
.getType()) {
293 llvm_unreachable("unknown operand type!");
294 case MachineOperand::MO_Immediate
: {
295 int DispVal
= DispSpec
.getImm();
296 if (DispVal
|| !HasParenPart
)
300 case MachineOperand::MO_GlobalAddress
:
301 case MachineOperand::MO_ConstantPoolIndex
:
302 PrintSymbolOperand(DispSpec
, O
);
306 if (Modifier
&& strcmp(Modifier
, "H") == 0)
310 assert(IndexReg
.getReg() != X86::ESP
&&
311 "X86 doesn't allow scaling by ESP");
315 PrintModifiedOperand(MI
, OpNo
+ X86::AddrBaseReg
, O
, Modifier
);
317 if (IndexReg
.getReg()) {
319 PrintModifiedOperand(MI
, OpNo
+ X86::AddrIndexReg
, O
, Modifier
);
320 unsigned ScaleVal
= MI
->getOperand(OpNo
+ X86::AddrScaleAmt
).getImm();
322 O
<< ',' << ScaleVal
;
328 void X86AsmPrinter::PrintMemReference(const MachineInstr
*MI
, unsigned OpNo
,
329 raw_ostream
&O
, const char *Modifier
) {
330 assert(isMem(*MI
, OpNo
) && "Invalid memory reference!");
331 const MachineOperand
&Segment
= MI
->getOperand(OpNo
+ X86::AddrSegmentReg
);
332 if (Segment
.getReg()) {
333 PrintModifiedOperand(MI
, OpNo
+ X86::AddrSegmentReg
, O
, Modifier
);
336 PrintLeaMemReference(MI
, OpNo
, O
, Modifier
);
339 void X86AsmPrinter::PrintIntelMemReference(const MachineInstr
*MI
,
340 unsigned OpNo
, raw_ostream
&O
) {
341 const MachineOperand
&BaseReg
= MI
->getOperand(OpNo
+ X86::AddrBaseReg
);
342 unsigned ScaleVal
= MI
->getOperand(OpNo
+ X86::AddrScaleAmt
).getImm();
343 const MachineOperand
&IndexReg
= MI
->getOperand(OpNo
+ X86::AddrIndexReg
);
344 const MachineOperand
&DispSpec
= MI
->getOperand(OpNo
+ X86::AddrDisp
);
345 const MachineOperand
&SegReg
= MI
->getOperand(OpNo
+ X86::AddrSegmentReg
);
347 // If this has a segment register, print it.
348 if (SegReg
.getReg()) {
349 PrintOperand(MI
, OpNo
+ X86::AddrSegmentReg
, O
);
355 bool NeedPlus
= false;
356 if (BaseReg
.getReg()) {
357 PrintOperand(MI
, OpNo
+ X86::AddrBaseReg
, O
);
361 if (IndexReg
.getReg()) {
362 if (NeedPlus
) O
<< " + ";
364 O
<< ScaleVal
<< '*';
365 PrintOperand(MI
, OpNo
+ X86::AddrIndexReg
, O
);
369 if (!DispSpec
.isImm()) {
370 if (NeedPlus
) O
<< " + ";
371 PrintOperand(MI
, OpNo
+ X86::AddrDisp
, O
);
373 int64_t DispVal
= DispSpec
.getImm();
374 if (DispVal
|| (!IndexReg
.getReg() && !BaseReg
.getReg())) {
389 static bool printAsmMRegister(X86AsmPrinter
&P
, const MachineOperand
&MO
,
390 char Mode
, raw_ostream
&O
) {
391 Register Reg
= MO
.getReg();
392 bool EmitPercent
= true;
394 if (!X86::GR8RegClass
.contains(Reg
) &&
395 !X86::GR16RegClass
.contains(Reg
) &&
396 !X86::GR32RegClass
.contains(Reg
) &&
397 !X86::GR64RegClass
.contains(Reg
))
401 default: return true; // Unknown mode.
402 case 'b': // Print QImode register
403 Reg
= getX86SubSuperRegister(Reg
, 8);
405 case 'h': // Print QImode high register
406 Reg
= getX86SubSuperRegister(Reg
, 8, true);
408 case 'w': // Print HImode register
409 Reg
= getX86SubSuperRegister(Reg
, 16);
411 case 'k': // Print SImode register
412 Reg
= getX86SubSuperRegister(Reg
, 32);
418 // Print 64-bit register names if 64-bit integer registers are available.
419 // Otherwise, print 32-bit register names.
420 Reg
= getX86SubSuperRegister(Reg
, P
.getSubtarget().is64Bit() ? 64 : 32);
427 O
<< X86ATTInstPrinter::getRegisterName(Reg
);
431 /// PrintAsmOperand - Print out an operand for an inline asm expression.
433 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
434 const char *ExtraCode
, raw_ostream
&O
) {
435 // Does this asm operand have a single letter operand modifier?
436 if (ExtraCode
&& ExtraCode
[0]) {
437 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
439 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
441 switch (ExtraCode
[0]) {
443 // See if this is a generic print operand
444 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, ExtraCode
, O
);
445 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
446 switch (MO
.getType()) {
449 case MachineOperand::MO_Immediate
:
452 case MachineOperand::MO_ConstantPoolIndex
:
453 case MachineOperand::MO_JumpTableIndex
:
454 case MachineOperand::MO_ExternalSymbol
:
455 llvm_unreachable("unexpected operand type!");
456 case MachineOperand::MO_GlobalAddress
:
457 PrintSymbolOperand(MO
, O
);
458 if (Subtarget
->isPICStyleRIPRel())
461 case MachineOperand::MO_Register
:
463 PrintOperand(MI
, OpNo
, O
);
468 case 'c': // Don't print "$" before a global var name or constant.
469 switch (MO
.getType()) {
471 PrintOperand(MI
, OpNo
, O
);
473 case MachineOperand::MO_Immediate
:
476 case MachineOperand::MO_ConstantPoolIndex
:
477 case MachineOperand::MO_JumpTableIndex
:
478 case MachineOperand::MO_ExternalSymbol
:
479 llvm_unreachable("unexpected operand type!");
480 case MachineOperand::MO_GlobalAddress
:
481 PrintSymbolOperand(MO
, O
);
486 case 'A': // Print '*' before a register (it must be a register)
489 PrintOperand(MI
, OpNo
, O
);
494 case 'b': // Print QImode register
495 case 'h': // Print QImode high register
496 case 'w': // Print HImode register
497 case 'k': // Print SImode register
498 case 'q': // Print DImode register
499 case 'V': // Print native register without '%'
501 return printAsmMRegister(*this, MO
, ExtraCode
[0], O
);
502 PrintOperand(MI
, OpNo
, O
);
505 case 'P': // This is the operand of a call, treat specially.
506 PrintPCRelImm(MI
, OpNo
, O
);
509 case 'n': // Negate the immediate or print a '-' before the operand.
510 // Note: this is a temporary solution. It should be handled target
511 // independently as part of the 'MC' work.
520 PrintOperand(MI
, OpNo
, O
);
524 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
525 const char *ExtraCode
,
527 if (MI
->getInlineAsmDialect() == InlineAsm::AD_Intel
) {
528 PrintIntelMemReference(MI
, OpNo
, O
);
532 if (ExtraCode
&& ExtraCode
[0]) {
533 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
535 switch (ExtraCode
[0]) {
536 default: return true; // Unknown modifier.
537 case 'b': // Print QImode register
538 case 'h': // Print QImode high register
539 case 'w': // Print HImode register
540 case 'k': // Print SImode register
541 case 'q': // Print SImode register
542 // These only apply to registers, ignore on mem.
545 PrintMemReference(MI
, OpNo
, O
, "H");
547 case 'P': // Don't print @PLT, but do print as memory.
548 PrintMemReference(MI
, OpNo
, O
, "no-rip");
552 PrintMemReference(MI
, OpNo
, O
, nullptr);
556 void X86AsmPrinter::EmitStartOfAsmFile(Module
&M
) {
557 const Triple
&TT
= TM
.getTargetTriple();
559 if (TT
.isOSBinFormatELF()) {
560 // Assemble feature flags that may require creation of a note section.
561 unsigned FeatureFlagsAnd
= 0;
562 if (M
.getModuleFlag("cf-protection-branch"))
563 FeatureFlagsAnd
|= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT
;
564 if (M
.getModuleFlag("cf-protection-return"))
565 FeatureFlagsAnd
|= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
567 if (FeatureFlagsAnd
) {
568 // Emit a .note.gnu.property section with the flags.
569 if (!TT
.isArch32Bit() && !TT
.isArch64Bit())
570 llvm_unreachable("CFProtection used on invalid architecture!");
571 MCSection
*Cur
= OutStreamer
->getCurrentSectionOnly();
572 MCSection
*Nt
= MMI
->getContext().getELFSection(
573 ".note.gnu.property", ELF::SHT_NOTE
, ELF::SHF_ALLOC
);
574 OutStreamer
->SwitchSection(Nt
);
576 // Emitting note header.
577 int WordSize
= TT
.isArch64Bit() ? 8 : 4;
578 EmitAlignment(WordSize
== 4 ? Align(4) : Align(8));
579 OutStreamer
->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
580 OutStreamer
->EmitIntValue(8 + WordSize
, 4 /*size*/); // Elf_Prop size
581 OutStreamer
->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0
, 4 /*size*/);
582 OutStreamer
->EmitBytes(StringRef("GNU", 4)); // note name
584 // Emitting an Elf_Prop for the CET properties.
585 OutStreamer
->EmitIntValue(ELF::GNU_PROPERTY_X86_FEATURE_1_AND
, 4);
586 OutStreamer
->EmitIntValue(4, 4); // data size
587 OutStreamer
->EmitIntValue(FeatureFlagsAnd
, 4); // data
588 EmitAlignment(WordSize
== 4 ? Align(4) : Align(8)); // padding
590 OutStreamer
->endSection(Nt
);
591 OutStreamer
->SwitchSection(Cur
);
595 if (TT
.isOSBinFormatMachO())
596 OutStreamer
->SwitchSection(getObjFileLowering().getTextSection());
598 if (TT
.isOSBinFormatCOFF()) {
599 // Emit an absolute @feat.00 symbol. This appears to be some kind of
600 // compiler features bitfield read by link.exe.
601 MCSymbol
*S
= MMI
->getContext().getOrCreateSymbol(StringRef("@feat.00"));
602 OutStreamer
->BeginCOFFSymbolDef(S
);
603 OutStreamer
->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC
);
604 OutStreamer
->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL
);
605 OutStreamer
->EndCOFFSymbolDef();
606 int64_t Feat00Flags
= 0;
608 if (TT
.getArch() == Triple::x86
) {
609 // According to the PE-COFF spec, the LSB of this value marks the object
610 // for "registered SEH". This means that all SEH handler entry points
611 // must be registered in .sxdata. Use of any unregistered handlers will
612 // cause the process to terminate immediately. LLVM does not know how to
613 // register any SEH handlers, so its object files should be safe.
617 if (M
.getModuleFlag("cfguardtable"))
618 Feat00Flags
|= 0x800; // Object is CFG-aware.
620 OutStreamer
->EmitSymbolAttribute(S
, MCSA_Global
);
621 OutStreamer
->EmitAssignment(
622 S
, MCConstantExpr::create(Feat00Flags
, MMI
->getContext()));
624 OutStreamer
->EmitSyntaxDirective();
626 // If this is not inline asm and we're in 16-bit
627 // mode prefix assembly with .code16.
628 bool is16
= TT
.getEnvironment() == Triple::CODE16
;
629 if (M
.getModuleInlineAsm().empty() && is16
)
630 OutStreamer
->EmitAssemblerFlag(MCAF_Code16
);
634 emitNonLazySymbolPointer(MCStreamer
&OutStreamer
, MCSymbol
*StubLabel
,
635 MachineModuleInfoImpl::StubValueTy
&MCSym
) {
637 OutStreamer
.EmitLabel(StubLabel
);
638 // .indirect_symbol _foo
639 OutStreamer
.EmitSymbolAttribute(MCSym
.getPointer(), MCSA_IndirectSymbol
);
642 // External to current translation unit.
643 OutStreamer
.EmitIntValue(0, 4/*size*/);
645 // Internal to current translation unit.
647 // When we place the LSDA into the TEXT section, the type info
648 // pointers need to be indirect and pc-rel. We accomplish this by
649 // using NLPs; however, sometimes the types are local to the file.
650 // We need to fill in the value for the NLP in those cases.
651 OutStreamer
.EmitValue(
652 MCSymbolRefExpr::create(MCSym
.getPointer(), OutStreamer
.getContext()),
656 static void emitNonLazyStubs(MachineModuleInfo
*MMI
, MCStreamer
&OutStreamer
) {
658 MachineModuleInfoMachO
&MMIMacho
=
659 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
661 // Output stubs for dynamically-linked functions.
662 MachineModuleInfoMachO::SymbolListTy Stubs
;
664 // Output stubs for external and common global variables.
665 Stubs
= MMIMacho
.GetGVStubList();
666 if (!Stubs
.empty()) {
667 OutStreamer
.SwitchSection(MMI
->getContext().getMachOSection(
668 "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS
,
669 SectionKind::getMetadata()));
671 for (auto &Stub
: Stubs
)
672 emitNonLazySymbolPointer(OutStreamer
, Stub
.first
, Stub
.second
);
675 OutStreamer
.AddBlankLine();
679 void X86AsmPrinter::EmitEndOfAsmFile(Module
&M
) {
680 const Triple
&TT
= TM
.getTargetTriple();
682 if (TT
.isOSBinFormatMachO()) {
683 // Mach-O uses non-lazy symbol stubs to encode per-TU information into
684 // global table for symbol lookup.
685 emitNonLazyStubs(MMI
, *OutStreamer
);
687 // Emit stack and fault map information.
689 FM
.serializeToFaultMapSection();
691 // This flag tells the linker that no global symbols contain code that fall
692 // through to other global symbols (e.g. an implementation of multiple entry
693 // points). If this doesn't occur, the linker can safely perform dead code
694 // stripping. Since LLVM never generates code that does this, it is always
696 OutStreamer
->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols
);
697 } else if (TT
.isOSBinFormatCOFF()) {
698 if (MMI
->usesMSVCFloatingPoint()) {
699 // In Windows' libcmt.lib, there is a file which is linked in only if the
700 // symbol _fltused is referenced. Linking this in causes some
703 // 1. For x86-32, it will set the x87 rounding mode to 53-bit instead of
704 // 64-bit mantissas at program start.
706 // 2. It links in support routines for floating-point in scanf and printf.
708 // MSVC emits an undefined reference to _fltused when there are any
709 // floating point operations in the program (including calls). A program
710 // that only has: `scanf("%f", &global_float);` may fail to trigger this,
711 // but oh well...that's a documented issue.
712 StringRef SymbolName
=
713 (TT
.getArch() == Triple::x86
) ? "__fltused" : "_fltused";
714 MCSymbol
*S
= MMI
->getContext().getOrCreateSymbol(SymbolName
);
715 OutStreamer
->EmitSymbolAttribute(S
, MCSA_Global
);
719 } else if (TT
.isOSBinFormatELF()) {
721 FM
.serializeToFaultMapSection();
725 //===----------------------------------------------------------------------===//
726 // Target Registry Stuff
727 //===----------------------------------------------------------------------===//
729 // Force static initialization.
730 extern "C" void LLVMInitializeX86AsmPrinter() {
731 RegisterAsmPrinter
<X86AsmPrinter
> X(getTheX86_32Target());
732 RegisterAsmPrinter
<X86AsmPrinter
> Y(getTheX86_64Target());