1 //===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11 #define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
12 
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"
19 
20 // Implemented in X86MCInstLower.cpp
21 namespace {
22   class X86MCInstLower;
23 }
24 
25 namespace llvm {
26 class MCStreamer;
27 class MCSymbol;
28 
29 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
30   const X86Subtarget *Subtarget;
31   StackMaps SM;
32   FaultMaps FM;
33   std::unique_ptr<MCCodeEmitter> CodeEmitter;
34   bool EmitFPOData = false;
35   bool NeedsRetpoline = false;
36 
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.
41   //
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 {
46   public:
startFunction(MachineFunction & MF)47     void startFunction(MachineFunction &MF) {
48       this->MF = &MF;
49     }
50     void count(MCInst &Inst, const MCSubtargetInfo &STI,
51                MCCodeEmitter *CodeEmitter);
52 
53     // Called to signal the start of a shadow of RequiredSize bytes.
reset(unsigned RequiredSize)54     void reset(unsigned RequiredSize) {
55       RequiredShadowSize = RequiredSize;
56       CurrentShadowSize = 0;
57       InShadow = true;
58     }
59 
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);
63   private:
64     const MachineFunction *MF;
65     bool InShadow = false;
66 
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;
73   };
74 
75   StackMapShadowTracker SMShadowTracker;
76 
77   // All instructions emitted by the X86AsmPrinter should use this helper
78   // method.
79   //
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);
89 
90   void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
91 
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);
100 
101   void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
102 
103   // Choose between emitting .seh_ directives and .cv_fpo_ directives.
104   void EmitSEHInstruction(const MachineInstr *MI);
105 
106 public:
107   X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
108 
getPassName()109   StringRef getPassName() const override {
110     return "X86 Assembly Printer";
111   }
112 
getSubtarget()113   const X86Subtarget &getSubtarget() const { return *Subtarget; }
114 
115   void EmitStartOfAsmFile(Module &M) override;
116 
117   void EmitEndOfAsmFile(Module &M) override;
118 
119   void EmitInstruction(const MachineInstr *MI) override;
120 
EmitBasicBlockEnd(const MachineBasicBlock & MBB)121   void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
122     AsmPrinter::EmitBasicBlockEnd(MBB);
123     SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
124   }
125 
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;
132 
doInitialization(Module & M)133   bool doInitialization(Module &M) override {
134     SMShadowTracker.reset(0);
135     SM.reset();
136     FM.reset();
137     return AsmPrinter::doInitialization(M);
138   }
139 
140   bool runOnMachineFunction(MachineFunction &F) override;
141   void EmitFunctionBodyStart() override;
142   void EmitFunctionBodyEnd() override;
143 };
144 
145 } // end namespace llvm
146 
147 #endif
148