1 //===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//
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 #ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
10 #define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
12 #include "X86Subtarget.h"
13 #include "llvm/CodeGen/AsmPrinter.h"
14 #include "llvm/CodeGen/FaultMaps.h"
15 #include "llvm/CodeGen/StackMaps.h"
16 #include "llvm/MC/MCCodeEmitter.h"
17 #include "llvm/Target/TargetMachine.h"
19 // Implemented in X86MCInstLower.cpp
28 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter
: public AsmPrinter
{
29 const X86Subtarget
*Subtarget
;
32 std::unique_ptr
<MCCodeEmitter
> CodeEmitter
;
33 bool EmitFPOData
= false;
34 bool NeedsRetpoline
= false;
36 // This utility class tracks the length of a stackmap instruction's 'shadow'.
37 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
38 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
39 // the shadow) are met, while outputting a minimal number of NOPs for padding.
41 // To minimise the number of NOPs used, the shadow tracker counts the number
42 // of instruction bytes output since the last stackmap. Only if there are too
43 // few instruction bytes to cover the shadow are NOPs used for padding.
44 class StackMapShadowTracker
{
46 void startFunction(MachineFunction
&MF
) {
49 void count(MCInst
&Inst
, const MCSubtargetInfo
&STI
,
50 MCCodeEmitter
*CodeEmitter
);
52 // Called to signal the start of a shadow of RequiredSize bytes.
53 void reset(unsigned RequiredSize
) {
54 RequiredShadowSize
= RequiredSize
;
55 CurrentShadowSize
= 0;
59 // Called before every stackmap/patchpoint, and at the end of basic blocks,
60 // to emit any necessary padding-NOPs.
61 void emitShadowPadding(MCStreamer
&OutStreamer
, const MCSubtargetInfo
&STI
);
63 const MachineFunction
*MF
;
64 bool InShadow
= false;
66 // RequiredShadowSize holds the length of the shadow specified in the most
67 // recently encountered STACKMAP instruction.
68 // CurrentShadowSize counts the number of bytes encoded since the most
69 // recently encountered STACKMAP, stopping when that number is greater than
70 // or equal to RequiredShadowSize.
71 unsigned RequiredShadowSize
= 0, CurrentShadowSize
= 0;
74 StackMapShadowTracker SMShadowTracker
;
76 // All instructions emitted by the X86AsmPrinter should use this helper
79 // This helper function invokes the SMShadowTracker on each instruction before
80 // outputting it to the OutStream. This allows the shadow tracker to minimise
81 // the number of NOPs used for stackmap padding.
82 void EmitAndCountInstruction(MCInst
&Inst
);
83 void LowerSTACKMAP(const MachineInstr
&MI
);
84 void LowerPATCHPOINT(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
85 void LowerSTATEPOINT(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
86 void LowerFAULTING_OP(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
87 void LowerPATCHABLE_OP(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
89 void LowerTlsAddr(X86MCInstLower
&MCInstLowering
, const MachineInstr
&MI
);
91 // XRay-specific lowering for X86.
92 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr
&MI
,
93 X86MCInstLower
&MCIL
);
94 void LowerPATCHABLE_RET(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
95 void LowerPATCHABLE_TAIL_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
96 void LowerPATCHABLE_EVENT_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
97 void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr
&MI
,
98 X86MCInstLower
&MCIL
);
100 void LowerFENTRY_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
102 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
103 void EmitSEHInstruction(const MachineInstr
*MI
);
105 void PrintSymbolOperand(const MachineOperand
&MO
, raw_ostream
&O
) override
;
106 void PrintOperand(const MachineInstr
*MI
, unsigned OpNo
, raw_ostream
&O
);
107 void PrintModifiedOperand(const MachineInstr
*MI
, unsigned OpNo
,
108 raw_ostream
&O
, const char *Modifier
);
109 void PrintPCRelImm(const MachineInstr
*MI
, unsigned OpNo
, raw_ostream
&O
);
110 void PrintLeaMemReference(const MachineInstr
*MI
, unsigned OpNo
,
111 raw_ostream
&O
, const char *Modifier
);
112 void PrintMemReference(const MachineInstr
*MI
, unsigned OpNo
, raw_ostream
&O
,
113 const char *Modifier
);
114 void PrintIntelMemReference(const MachineInstr
*MI
, unsigned OpNo
,
118 X86AsmPrinter(TargetMachine
&TM
, std::unique_ptr
<MCStreamer
> Streamer
);
120 StringRef
getPassName() const override
{
121 return "X86 Assembly Printer";
124 const X86Subtarget
&getSubtarget() const { return *Subtarget
; }
126 void EmitStartOfAsmFile(Module
&M
) override
;
128 void EmitEndOfAsmFile(Module
&M
) override
;
130 void EmitInstruction(const MachineInstr
*MI
) override
;
132 void EmitBasicBlockEnd(const MachineBasicBlock
&MBB
) override
{
133 AsmPrinter::EmitBasicBlockEnd(MBB
);
134 SMShadowTracker
.emitShadowPadding(*OutStreamer
, getSubtargetInfo());
137 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
138 const char *ExtraCode
, raw_ostream
&OS
) override
;
139 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
140 const char *ExtraCode
, raw_ostream
&OS
) override
;
142 bool doInitialization(Module
&M
) override
{
143 SMShadowTracker
.reset(0);
146 return AsmPrinter::doInitialization(M
);
149 bool runOnMachineFunction(MachineFunction
&F
) override
;
150 void EmitFunctionBodyStart() override
;
151 void EmitFunctionBodyEnd() override
;
154 } // end namespace llvm