]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/X86/X86AsmPrinter.h
Upgrade to OpenSSH 7.8p1.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / X86 / X86AsmPrinter.h
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:
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.
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
99   void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
100
101   // Choose between emitting .seh_ directives and .cv_fpo_ directives.
102   void EmitSEHInstruction(const MachineInstr *MI);
103
104 public:
105   X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
106
107   StringRef getPassName() const override {
108     return "X86 Assembly Printer";
109   }
110
111   const X86Subtarget &getSubtarget() const { return *Subtarget; }
112
113   void EmitStartOfAsmFile(Module &M) override;
114
115   void EmitEndOfAsmFile(Module &M) override;
116
117   void EmitInstruction(const MachineInstr *MI) override;
118
119   void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
120     AsmPrinter::EmitBasicBlockEnd(MBB);
121     SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
122   }
123
124   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
125                        unsigned AsmVariant, const char *ExtraCode,
126                        raw_ostream &OS) override;
127   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
128                              unsigned AsmVariant, const char *ExtraCode,
129                              raw_ostream &OS) override;
130
131   /// \brief Return the symbol for the specified constant pool entry.
132   MCSymbol *GetCPISymbol(unsigned CPID) const override;
133
134   bool doInitialization(Module &M) override {
135     SMShadowTracker.reset(0);
136     SM.reset();
137     FM.reset();
138     return AsmPrinter::doInitialization(M);
139   }
140
141   bool runOnMachineFunction(MachineFunction &F) override;
142   void EmitFunctionBodyStart() override;
143   void EmitFunctionBodyEnd() override;
144 };
145
146 } // end namespace llvm
147
148 #endif