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 "InstPrinter/X86ATTInstPrinter.h"
16 #include "MCTargetDesc/X86BaseInfo.h"
17 #include "MCTargetDesc/X86TargetStreamer.h"
18 #include "X86InstrInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "llvm/BinaryFormat/COFF.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/MC/MCCodeEmitter.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCSectionCOFF.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCSectionMachO.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/MachineValueType.h"
40 #include "llvm/Support/TargetRegistry.h"
43 X86AsmPrinter::X86AsmPrinter(TargetMachine
&TM
,
44 std::unique_ptr
<MCStreamer
> Streamer
)
45 : AsmPrinter(TM
, std::move(Streamer
)), SM(*this), FM(*this) {}
47 //===----------------------------------------------------------------------===//
48 // Primitive Helper Functions.
49 //===----------------------------------------------------------------------===//
51 /// runOnMachineFunction - Emit the function body.
53 bool X86AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
54 Subtarget
= &MF
.getSubtarget
<X86Subtarget
>();
56 SMShadowTracker
.startFunction(MF
);
57 CodeEmitter
.reset(TM
.getTarget().createMCCodeEmitter(
58 *Subtarget
->getInstrInfo(), *Subtarget
->getRegisterInfo(),
62 Subtarget
->isTargetWin32() && MF
.getMMI().getModule()->getCodeViewFlag();
64 SetupMachineFunction(MF
);
66 if (Subtarget
->isTargetCOFF()) {
67 bool Local
= MF
.getFunction().hasLocalLinkage();
68 OutStreamer
->BeginCOFFSymbolDef(CurrentFnSym
);
69 OutStreamer
->EmitCOFFSymbolStorageClass(
70 Local
? COFF::IMAGE_SYM_CLASS_STATIC
: COFF::IMAGE_SYM_CLASS_EXTERNAL
);
71 OutStreamer
->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
72 << COFF::SCT_COMPLEX_TYPE_SHIFT
);
73 OutStreamer
->EndCOFFSymbolDef();
76 // Emit the rest of the function body.
79 // Emit the XRay table for this function.
84 // We didn't modify anything.
88 void X86AsmPrinter::EmitFunctionBodyStart() {
91 static_cast<X86TargetStreamer
*>(OutStreamer
->getTargetStreamer()))
94 MF
->getInfo
<X86MachineFunctionInfo
>()->getArgumentStackSize());
98 void X86AsmPrinter::EmitFunctionBodyEnd() {
101 static_cast<X86TargetStreamer
*>(OutStreamer
->getTargetStreamer()))
102 XTS
->emitFPOEndProc();
106 /// printSymbolOperand - Print a raw symbol reference operand. This handles
107 /// jump tables, constant pools, global address and external symbols, all of
108 /// which print to a label with various suffixes for relocation types etc.
109 static void printSymbolOperand(X86AsmPrinter
&P
, const MachineOperand
&MO
,
111 switch (MO
.getType()) {
112 default: llvm_unreachable("unknown symbol type!");
113 case MachineOperand::MO_ConstantPoolIndex
:
114 P
.GetCPISymbol(MO
.getIndex())->print(O
, P
.MAI
);
115 P
.printOffset(MO
.getOffset(), O
);
117 case MachineOperand::MO_GlobalAddress
: {
118 const GlobalValue
*GV
= MO
.getGlobal();
121 if (MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
122 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
)
123 GVSym
= P
.getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr");
125 GVSym
= P
.getSymbol(GV
);
127 // Handle dllimport linkage.
128 if (MO
.getTargetFlags() == X86II::MO_DLLIMPORT
)
130 P
.OutContext
.getOrCreateSymbol(Twine("__imp_") + GVSym
->getName());
131 else if (MO
.getTargetFlags() == X86II::MO_COFFSTUB
)
133 P
.OutContext
.getOrCreateSymbol(Twine(".refptr.") + GVSym
->getName());
135 if (MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
136 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
) {
137 MCSymbol
*Sym
= P
.getSymbolWithGlobalValueBase(GV
, "$non_lazy_ptr");
138 MachineModuleInfoImpl::StubValueTy
&StubSym
=
139 P
.MMI
->getObjFileInfo
<MachineModuleInfoMachO
>().getGVStubEntry(Sym
);
140 if (!StubSym
.getPointer())
141 StubSym
= MachineModuleInfoImpl::
142 StubValueTy(P
.getSymbol(GV
), !GV
->hasInternalLinkage());
145 // If the name begins with a dollar-sign, enclose it in parens. We do this
146 // to avoid having it look like an integer immediate to the assembler.
147 if (GVSym
->getName()[0] != '$')
148 GVSym
->print(O
, P
.MAI
);
151 GVSym
->print(O
, P
.MAI
);
154 P
.printOffset(MO
.getOffset(), O
);
159 switch (MO
.getTargetFlags()) {
161 llvm_unreachable("Unknown target flag on GV operand");
162 case X86II::MO_NO_FLAG
: // No flag.
164 case X86II::MO_DARWIN_NONLAZY
:
165 case X86II::MO_DLLIMPORT
:
166 case X86II::MO_COFFSTUB
:
167 // These affect the name of the symbol, not any suffix.
169 case X86II::MO_GOT_ABSOLUTE_ADDRESS
:
171 P
.MF
->getPICBaseSymbol()->print(O
, P
.MAI
);
174 case X86II::MO_PIC_BASE_OFFSET
:
175 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
177 P
.MF
->getPICBaseSymbol()->print(O
, P
.MAI
);
179 case X86II::MO_TLSGD
: O
<< "@TLSGD"; break;
180 case X86II::MO_TLSLD
: O
<< "@TLSLD"; break;
181 case X86II::MO_TLSLDM
: O
<< "@TLSLDM"; break;
182 case X86II::MO_GOTTPOFF
: O
<< "@GOTTPOFF"; break;
183 case X86II::MO_INDNTPOFF
: O
<< "@INDNTPOFF"; break;
184 case X86II::MO_TPOFF
: O
<< "@TPOFF"; break;
185 case X86II::MO_DTPOFF
: O
<< "@DTPOFF"; break;
186 case X86II::MO_NTPOFF
: O
<< "@NTPOFF"; break;
187 case X86II::MO_GOTNTPOFF
: O
<< "@GOTNTPOFF"; break;
188 case X86II::MO_GOTPCREL
: O
<< "@GOTPCREL"; break;
189 case X86II::MO_GOT
: O
<< "@GOT"; break;
190 case X86II::MO_GOTOFF
: O
<< "@GOTOFF"; break;
191 case X86II::MO_PLT
: O
<< "@PLT"; break;
192 case X86II::MO_TLVP
: O
<< "@TLVP"; break;
193 case X86II::MO_TLVP_PIC_BASE
:
195 P
.MF
->getPICBaseSymbol()->print(O
, P
.MAI
);
197 case X86II::MO_SECREL
: O
<< "@SECREL32"; break;
201 static void printOperand(X86AsmPrinter
&P
, const MachineInstr
*MI
,
202 unsigned OpNo
, raw_ostream
&O
,
203 const char *Modifier
= nullptr, unsigned AsmVariant
= 0);
205 /// printPCRelImm - This is used to print an immediate value that ends up
206 /// being encoded as a pc-relative value. These print slightly differently, for
207 /// example, a $ is not emitted.
208 static void printPCRelImm(X86AsmPrinter
&P
, const MachineInstr
*MI
,
209 unsigned OpNo
, raw_ostream
&O
) {
210 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
211 switch (MO
.getType()) {
212 default: llvm_unreachable("Unknown pcrel immediate operand");
213 case MachineOperand::MO_Register
:
214 // pc-relativeness was handled when computing the value in the reg.
215 printOperand(P
, MI
, OpNo
, O
);
217 case MachineOperand::MO_Immediate
:
220 case MachineOperand::MO_GlobalAddress
:
221 printSymbolOperand(P
, MO
, O
);
226 static void printOperand(X86AsmPrinter
&P
, const MachineInstr
*MI
,
227 unsigned OpNo
, raw_ostream
&O
, const char *Modifier
,
228 unsigned AsmVariant
) {
229 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
230 switch (MO
.getType()) {
231 default: llvm_unreachable("unknown operand type!");
232 case MachineOperand::MO_Register
: {
233 // FIXME: Enumerating AsmVariant, so we can remove magic number.
234 if (AsmVariant
== 0) O
<< '%';
235 unsigned Reg
= MO
.getReg();
236 if (Modifier
&& strncmp(Modifier
, "subreg", strlen("subreg")) == 0) {
237 unsigned Size
= (strcmp(Modifier
+6,"64") == 0) ? 64 :
238 (strcmp(Modifier
+6,"32") == 0) ? 32 :
239 (strcmp(Modifier
+6,"16") == 0) ? 16 : 8;
240 Reg
= getX86SubSuperRegister(Reg
, Size
);
242 O
<< X86ATTInstPrinter::getRegisterName(Reg
);
246 case MachineOperand::MO_Immediate
:
247 if (AsmVariant
== 0) O
<< '$';
251 case MachineOperand::MO_GlobalAddress
: {
252 if (AsmVariant
== 0) O
<< '$';
253 printSymbolOperand(P
, MO
, O
);
256 case MachineOperand::MO_BlockAddress
: {
257 MCSymbol
*Sym
= P
.GetBlockAddressSymbol(MO
.getBlockAddress());
258 Sym
->print(O
, P
.MAI
);
264 static void printLeaMemReference(X86AsmPrinter
&P
, const MachineInstr
*MI
,
265 unsigned Op
, raw_ostream
&O
,
266 const char *Modifier
= nullptr) {
267 const MachineOperand
&BaseReg
= MI
->getOperand(Op
+X86::AddrBaseReg
);
268 const MachineOperand
&IndexReg
= MI
->getOperand(Op
+X86::AddrIndexReg
);
269 const MachineOperand
&DispSpec
= MI
->getOperand(Op
+X86::AddrDisp
);
271 // If we really don't want to print out (rip), don't.
272 bool HasBaseReg
= BaseReg
.getReg() != 0;
273 if (HasBaseReg
&& Modifier
&& !strcmp(Modifier
, "no-rip") &&
274 BaseReg
.getReg() == X86::RIP
)
277 // HasParenPart - True if we will print out the () part of the mem ref.
278 bool HasParenPart
= IndexReg
.getReg() || HasBaseReg
;
280 switch (DispSpec
.getType()) {
282 llvm_unreachable("unknown operand type!");
283 case MachineOperand::MO_Immediate
: {
284 int DispVal
= DispSpec
.getImm();
285 if (DispVal
|| !HasParenPart
)
289 case MachineOperand::MO_GlobalAddress
:
290 case MachineOperand::MO_ConstantPoolIndex
:
291 printSymbolOperand(P
, DispSpec
, O
);
294 if (Modifier
&& strcmp(Modifier
, "H") == 0)
298 assert(IndexReg
.getReg() != X86::ESP
&&
299 "X86 doesn't allow scaling by ESP");
303 printOperand(P
, MI
, Op
+X86::AddrBaseReg
, O
, Modifier
);
305 if (IndexReg
.getReg()) {
307 printOperand(P
, MI
, Op
+X86::AddrIndexReg
, O
, Modifier
);
308 unsigned ScaleVal
= MI
->getOperand(Op
+X86::AddrScaleAmt
).getImm();
310 O
<< ',' << ScaleVal
;
316 static void printMemReference(X86AsmPrinter
&P
, const MachineInstr
*MI
,
317 unsigned Op
, raw_ostream
&O
,
318 const char *Modifier
= nullptr) {
319 assert(isMem(*MI
, Op
) && "Invalid memory reference!");
320 const MachineOperand
&Segment
= MI
->getOperand(Op
+X86::AddrSegmentReg
);
321 if (Segment
.getReg()) {
322 printOperand(P
, MI
, Op
+X86::AddrSegmentReg
, O
, Modifier
);
325 printLeaMemReference(P
, MI
, Op
, O
, Modifier
);
328 static void printIntelMemReference(X86AsmPrinter
&P
, const MachineInstr
*MI
,
329 unsigned Op
, raw_ostream
&O
,
330 const char *Modifier
= nullptr,
331 unsigned AsmVariant
= 1) {
332 const MachineOperand
&BaseReg
= MI
->getOperand(Op
+X86::AddrBaseReg
);
333 unsigned ScaleVal
= MI
->getOperand(Op
+X86::AddrScaleAmt
).getImm();
334 const MachineOperand
&IndexReg
= MI
->getOperand(Op
+X86::AddrIndexReg
);
335 const MachineOperand
&DispSpec
= MI
->getOperand(Op
+X86::AddrDisp
);
336 const MachineOperand
&SegReg
= MI
->getOperand(Op
+X86::AddrSegmentReg
);
338 // If this has a segment register, print it.
339 if (SegReg
.getReg()) {
340 printOperand(P
, MI
, Op
+X86::AddrSegmentReg
, O
, Modifier
, AsmVariant
);
346 bool NeedPlus
= false;
347 if (BaseReg
.getReg()) {
348 printOperand(P
, MI
, Op
+X86::AddrBaseReg
, O
, Modifier
, AsmVariant
);
352 if (IndexReg
.getReg()) {
353 if (NeedPlus
) O
<< " + ";
355 O
<< ScaleVal
<< '*';
356 printOperand(P
, MI
, Op
+X86::AddrIndexReg
, O
, Modifier
, AsmVariant
);
360 if (!DispSpec
.isImm()) {
361 if (NeedPlus
) O
<< " + ";
362 printOperand(P
, MI
, Op
+X86::AddrDisp
, O
, Modifier
, AsmVariant
);
364 int64_t DispVal
= DispSpec
.getImm();
365 if (DispVal
|| (!IndexReg
.getReg() && !BaseReg
.getReg())) {
380 static bool printAsmMRegister(X86AsmPrinter
&P
, const MachineOperand
&MO
,
381 char Mode
, raw_ostream
&O
) {
382 unsigned Reg
= MO
.getReg();
383 bool EmitPercent
= true;
385 if (!X86::GR8RegClass
.contains(Reg
) &&
386 !X86::GR16RegClass
.contains(Reg
) &&
387 !X86::GR32RegClass
.contains(Reg
) &&
388 !X86::GR64RegClass
.contains(Reg
))
392 default: return true; // Unknown mode.
393 case 'b': // Print QImode register
394 Reg
= getX86SubSuperRegister(Reg
, 8);
396 case 'h': // Print QImode high register
397 Reg
= getX86SubSuperRegister(Reg
, 8, true);
399 case 'w': // Print HImode register
400 Reg
= getX86SubSuperRegister(Reg
, 16);
402 case 'k': // Print SImode register
403 Reg
= getX86SubSuperRegister(Reg
, 32);
409 // Print 64-bit register names if 64-bit integer registers are available.
410 // Otherwise, print 32-bit register names.
411 Reg
= getX86SubSuperRegister(Reg
, P
.getSubtarget().is64Bit() ? 64 : 32);
418 O
<< X86ATTInstPrinter::getRegisterName(Reg
);
422 /// PrintAsmOperand - Print out an operand for an inline asm expression.
424 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
426 const char *ExtraCode
, raw_ostream
&O
) {
427 // Does this asm operand have a single letter operand modifier?
428 if (ExtraCode
&& ExtraCode
[0]) {
429 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
431 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
433 switch (ExtraCode
[0]) {
435 // See if this is a generic print operand
436 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, AsmVariant
, ExtraCode
, O
);
437 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
438 switch (MO
.getType()) {
441 case MachineOperand::MO_Immediate
:
444 case MachineOperand::MO_ConstantPoolIndex
:
445 case MachineOperand::MO_JumpTableIndex
:
446 case MachineOperand::MO_ExternalSymbol
:
447 llvm_unreachable("unexpected operand type!");
448 case MachineOperand::MO_GlobalAddress
:
449 printSymbolOperand(*this, MO
, O
);
450 if (Subtarget
->isPICStyleRIPRel())
453 case MachineOperand::MO_Register
:
455 printOperand(*this, MI
, OpNo
, O
);
460 case 'c': // Don't print "$" before a global var name or constant.
461 switch (MO
.getType()) {
463 printOperand(*this, MI
, OpNo
, O
);
465 case MachineOperand::MO_Immediate
:
468 case MachineOperand::MO_ConstantPoolIndex
:
469 case MachineOperand::MO_JumpTableIndex
:
470 case MachineOperand::MO_ExternalSymbol
:
471 llvm_unreachable("unexpected operand type!");
472 case MachineOperand::MO_GlobalAddress
:
473 printSymbolOperand(*this, MO
, O
);
478 case 'A': // Print '*' before a register (it must be a register)
481 printOperand(*this, MI
, OpNo
, O
);
486 case 'b': // Print QImode register
487 case 'h': // Print QImode high register
488 case 'w': // Print HImode register
489 case 'k': // Print SImode register
490 case 'q': // Print DImode register
491 case 'V': // Print native register without '%'
493 return printAsmMRegister(*this, MO
, ExtraCode
[0], O
);
494 printOperand(*this, MI
, OpNo
, O
);
497 case 'P': // This is the operand of a call, treat specially.
498 printPCRelImm(*this, MI
, OpNo
, O
);
501 case 'n': // Negate the immediate or print a '-' before the operand.
502 // Note: this is a temporary solution. It should be handled target
503 // independently as part of the 'MC' work.
512 printOperand(*this, MI
, OpNo
, O
, /*Modifier*/ nullptr, AsmVariant
);
516 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
517 unsigned OpNo
, unsigned AsmVariant
,
518 const char *ExtraCode
,
521 printIntelMemReference(*this, MI
, OpNo
, O
);
525 if (ExtraCode
&& ExtraCode
[0]) {
526 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
528 switch (ExtraCode
[0]) {
529 default: return true; // Unknown modifier.
530 case 'b': // Print QImode register
531 case 'h': // Print QImode high register
532 case 'w': // Print HImode register
533 case 'k': // Print SImode register
534 case 'q': // Print SImode register
535 // These only apply to registers, ignore on mem.
538 printMemReference(*this, MI
, OpNo
, O
, "H");
540 case 'P': // Don't print @PLT, but do print as memory.
541 printMemReference(*this, MI
, OpNo
, O
, "no-rip");
545 printMemReference(*this, MI
, OpNo
, O
);
549 void X86AsmPrinter::EmitStartOfAsmFile(Module
&M
) {
550 const Triple
&TT
= TM
.getTargetTriple();
552 if (TT
.isOSBinFormatELF()) {
553 // Assemble feature flags that may require creation of a note section.
554 unsigned FeatureFlagsAnd
= 0;
555 if (M
.getModuleFlag("cf-protection-branch"))
556 FeatureFlagsAnd
|= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT
;
557 if (M
.getModuleFlag("cf-protection-return"))
558 FeatureFlagsAnd
|= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
560 if (FeatureFlagsAnd
) {
561 // Emit a .note.gnu.property section with the flags.
562 if (!TT
.isArch32Bit() && !TT
.isArch64Bit())
563 llvm_unreachable("CFProtection used on invalid architecture!");
564 MCSection
*Cur
= OutStreamer
->getCurrentSectionOnly();
565 MCSection
*Nt
= MMI
->getContext().getELFSection(
566 ".note.gnu.property", ELF::SHT_NOTE
, ELF::SHF_ALLOC
);
567 OutStreamer
->SwitchSection(Nt
);
569 // Emitting note header.
570 int WordSize
= TT
.isArch64Bit() ? 8 : 4;
571 EmitAlignment(WordSize
== 4 ? 2 : 3);
572 OutStreamer
->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
573 OutStreamer
->EmitIntValue(8 + WordSize
, 4 /*size*/); // Elf_Prop size
574 OutStreamer
->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0
, 4 /*size*/);
575 OutStreamer
->EmitBytes(StringRef("GNU", 4)); // note name
577 // Emitting an Elf_Prop for the CET properties.
578 OutStreamer
->EmitIntValue(ELF::GNU_PROPERTY_X86_FEATURE_1_AND
, 4);
579 OutStreamer
->EmitIntValue(4, 4); // data size
580 OutStreamer
->EmitIntValue(FeatureFlagsAnd
, 4); // data
581 EmitAlignment(WordSize
== 4 ? 2 : 3); // padding
583 OutStreamer
->endSection(Nt
);
584 OutStreamer
->SwitchSection(Cur
);
588 if (TT
.isOSBinFormatMachO())
589 OutStreamer
->SwitchSection(getObjFileLowering().getTextSection());
591 if (TT
.isOSBinFormatCOFF()) {
592 // Emit an absolute @feat.00 symbol. This appears to be some kind of
593 // compiler features bitfield read by link.exe.
594 MCSymbol
*S
= MMI
->getContext().getOrCreateSymbol(StringRef("@feat.00"));
595 OutStreamer
->BeginCOFFSymbolDef(S
);
596 OutStreamer
->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC
);
597 OutStreamer
->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL
);
598 OutStreamer
->EndCOFFSymbolDef();
599 int64_t Feat00Flags
= 0;
601 if (TT
.getArch() == Triple::x86
) {
602 // According to the PE-COFF spec, the LSB of this value marks the object
603 // for "registered SEH". This means that all SEH handler entry points
604 // must be registered in .sxdata. Use of any unregistered handlers will
605 // cause the process to terminate immediately. LLVM does not know how to
606 // register any SEH handlers, so its object files should be safe.
610 if (M
.getModuleFlag("cfguardtable"))
611 Feat00Flags
|= 0x800; // Object is CFG-aware.
613 OutStreamer
->EmitSymbolAttribute(S
, MCSA_Global
);
614 OutStreamer
->EmitAssignment(
615 S
, MCConstantExpr::create(Feat00Flags
, MMI
->getContext()));
617 OutStreamer
->EmitSyntaxDirective();
619 // If this is not inline asm and we're in 16-bit
620 // mode prefix assembly with .code16.
621 bool is16
= TT
.getEnvironment() == Triple::CODE16
;
622 if (M
.getModuleInlineAsm().empty() && is16
)
623 OutStreamer
->EmitAssemblerFlag(MCAF_Code16
);
627 emitNonLazySymbolPointer(MCStreamer
&OutStreamer
, MCSymbol
*StubLabel
,
628 MachineModuleInfoImpl::StubValueTy
&MCSym
) {
630 OutStreamer
.EmitLabel(StubLabel
);
631 // .indirect_symbol _foo
632 OutStreamer
.EmitSymbolAttribute(MCSym
.getPointer(), MCSA_IndirectSymbol
);
635 // External to current translation unit.
636 OutStreamer
.EmitIntValue(0, 4/*size*/);
638 // Internal to current translation unit.
640 // When we place the LSDA into the TEXT section, the type info
641 // pointers need to be indirect and pc-rel. We accomplish this by
642 // using NLPs; however, sometimes the types are local to the file.
643 // We need to fill in the value for the NLP in those cases.
644 OutStreamer
.EmitValue(
645 MCSymbolRefExpr::create(MCSym
.getPointer(), OutStreamer
.getContext()),
649 static void emitNonLazyStubs(MachineModuleInfo
*MMI
, MCStreamer
&OutStreamer
) {
651 MachineModuleInfoMachO
&MMIMacho
=
652 MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
654 // Output stubs for dynamically-linked functions.
655 MachineModuleInfoMachO::SymbolListTy Stubs
;
657 // Output stubs for external and common global variables.
658 Stubs
= MMIMacho
.GetGVStubList();
659 if (!Stubs
.empty()) {
660 OutStreamer
.SwitchSection(MMI
->getContext().getMachOSection(
661 "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS
,
662 SectionKind::getMetadata()));
664 for (auto &Stub
: Stubs
)
665 emitNonLazySymbolPointer(OutStreamer
, Stub
.first
, Stub
.second
);
668 OutStreamer
.AddBlankLine();
672 void X86AsmPrinter::EmitEndOfAsmFile(Module
&M
) {
673 const Triple
&TT
= TM
.getTargetTriple();
675 if (TT
.isOSBinFormatMachO()) {
676 // Mach-O uses non-lazy symbol stubs to encode per-TU information into
677 // global table for symbol lookup.
678 emitNonLazyStubs(MMI
, *OutStreamer
);
680 // Emit stack and fault map information.
682 FM
.serializeToFaultMapSection();
684 // This flag tells the linker that no global symbols contain code that fall
685 // through to other global symbols (e.g. an implementation of multiple entry
686 // points). If this doesn't occur, the linker can safely perform dead code
687 // stripping. Since LLVM never generates code that does this, it is always
689 OutStreamer
->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols
);
690 } else if (TT
.isOSBinFormatCOFF()) {
691 if (MMI
->usesMSVCFloatingPoint()) {
692 // In Windows' libcmt.lib, there is a file which is linked in only if the
693 // symbol _fltused is referenced. Linking this in causes some
696 // 1. For x86-32, it will set the x87 rounding mode to 53-bit instead of
697 // 64-bit mantissas at program start.
699 // 2. It links in support routines for floating-point in scanf and printf.
701 // MSVC emits an undefined reference to _fltused when there are any
702 // floating point operations in the program (including calls). A program
703 // that only has: `scanf("%f", &global_float);` may fail to trigger this,
704 // but oh well...that's a documented issue.
705 StringRef SymbolName
=
706 (TT
.getArch() == Triple::x86
) ? "__fltused" : "_fltused";
707 MCSymbol
*S
= MMI
->getContext().getOrCreateSymbol(SymbolName
);
708 OutStreamer
->EmitSymbolAttribute(S
, MCSA_Global
);
712 } else if (TT
.isOSBinFormatELF()) {
714 FM
.serializeToFaultMapSection();
718 //===----------------------------------------------------------------------===//
719 // Target Registry Stuff
720 //===----------------------------------------------------------------------===//
722 // Force static initialization.
723 extern "C" void LLVMInitializeX86AsmPrinter() {
724 RegisterAsmPrinter
<X86AsmPrinter
> X(getTheX86_32Target());
725 RegisterAsmPrinter
<X86AsmPrinter
> Y(getTheX86_64Target());