1 //===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- 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 #ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11 #define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
13 #include "X86Subtarget.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/FaultMaps.h"
16 #include "llvm/CodeGen/StackMaps.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/Target/TargetMachine.h"
20 // Implemented in X86MCInstLower.cpp
29 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter
: public AsmPrinter
{
30 const X86Subtarget
*Subtarget
;
33 std::unique_ptr
<MCCodeEmitter
> CodeEmitter
;
34 bool EmitFPOData
= false;
35 bool NeedsRetpoline
= false;
37 // This utility class tracks the length of a stackmap instruction's 'shadow'.
38 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
39 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
40 // the shadow) are met, while outputting a minimal number of NOPs for padding.
42 // To minimise the number of NOPs used, the shadow tracker counts the number
43 // of instruction bytes output since the last stackmap. Only if there are too
44 // few instruction bytes to cover the shadow are NOPs used for padding.
45 class StackMapShadowTracker
{
47 void startFunction(MachineFunction
&MF
) {
50 void count(MCInst
&Inst
, const MCSubtargetInfo
&STI
,
51 MCCodeEmitter
*CodeEmitter
);
53 // Called to signal the start of a shadow of RequiredSize bytes.
54 void reset(unsigned RequiredSize
) {
55 RequiredShadowSize
= RequiredSize
;
56 CurrentShadowSize
= 0;
60 // Called before every stackmap/patchpoint, and at the end of basic blocks,
61 // to emit any necessary padding-NOPs.
62 void emitShadowPadding(MCStreamer
&OutStreamer
, const MCSubtargetInfo
&STI
);
64 const MachineFunction
*MF
;
65 bool InShadow
= false;
67 // RequiredShadowSize holds the length of the shadow specified in the most
68 // recently encountered STACKMAP instruction.
69 // CurrentShadowSize counts the number of bytes encoded since the most
70 // recently encountered STACKMAP, stopping when that number is greater than
71 // or equal to RequiredShadowSize.
72 unsigned RequiredShadowSize
= 0, CurrentShadowSize
= 0;
75 StackMapShadowTracker SMShadowTracker
;
77 // All instructions emitted by the X86AsmPrinter should use this helper
80 // This helper function invokes the SMShadowTracker on each instruction before
81 // outputting it to the OutStream. This allows the shadow tracker to minimise
82 // the number of NOPs used for stackmap padding.
83 void EmitAndCountInstruction(MCInst
&Inst
);
84 void LowerSTACKMAP(const MachineInstr
&MI
);
85 void LowerPATCHPOINT(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
86 void LowerSTATEPOINT(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
87 void LowerFAULTING_OP(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
88 void LowerPATCHABLE_OP(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
90 void LowerTlsAddr(X86MCInstLower
&MCInstLowering
, const MachineInstr
&MI
);
92 // XRay-specific lowering for X86.
93 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr
&MI
,
94 X86MCInstLower
&MCIL
);
95 void LowerPATCHABLE_RET(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
96 void LowerPATCHABLE_TAIL_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
97 void LowerPATCHABLE_EVENT_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
98 void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr
&MI
,
99 X86MCInstLower
&MCIL
);
101 void LowerFENTRY_CALL(const MachineInstr
&MI
, X86MCInstLower
&MCIL
);
103 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
104 void EmitSEHInstruction(const MachineInstr
*MI
);
107 X86AsmPrinter(TargetMachine
&TM
, std::unique_ptr
<MCStreamer
> Streamer
);
109 StringRef
getPassName() const override
{
110 return "X86 Assembly Printer";
113 const X86Subtarget
&getSubtarget() const { return *Subtarget
; }
115 void EmitStartOfAsmFile(Module
&M
) override
;
117 void EmitEndOfAsmFile(Module
&M
) override
;
119 void EmitInstruction(const MachineInstr
*MI
) override
;
121 void EmitBasicBlockEnd(const MachineBasicBlock
&MBB
) override
{
122 AsmPrinter::EmitBasicBlockEnd(MBB
);
123 SMShadowTracker
.emitShadowPadding(*OutStreamer
, getSubtargetInfo());
126 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
127 unsigned AsmVariant
, const char *ExtraCode
,
128 raw_ostream
&OS
) override
;
129 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
130 unsigned AsmVariant
, const char *ExtraCode
,
131 raw_ostream
&OS
) override
;
133 bool doInitialization(Module
&M
) override
{
134 SMShadowTracker
.reset(0);
137 return AsmPrinter::doInitialization(M
);
140 bool runOnMachineFunction(MachineFunction
&F
) override
;
141 void EmitFunctionBodyStart() override
;
142 void EmitFunctionBodyEnd() override
;
145 } // end namespace llvm