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/StackMaps.h"
16 #include "llvm/Target/TargetMachine.h"
17 
18 // Implemented in X86MCInstLower.cpp
19 namespace {
20   class X86MCInstLower;
21 }
22 
23 namespace llvm {
24 class MCStreamer;
25 class MCSymbol;
26 
27 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
28   const X86Subtarget *Subtarget;
29   StackMaps SM;
30 
31   void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
32 
33   // This utility class tracks the length of a stackmap instruction's 'shadow'.
34   // It is used by the X86AsmPrinter to ensure that the stackmap shadow
35   // invariants (i.e. no other stackmaps, patchpoints, or control flow within
36   // the shadow) are met, while outputting a minimal number of NOPs for padding.
37   //
38   // To minimise the number of NOPs used, the shadow tracker counts the number
39   // of instruction bytes output since the last stackmap. Only if there are too
40   // few instruction bytes to cover the shadow are NOPs used for padding.
41   class StackMapShadowTracker {
42   public:
43     StackMapShadowTracker(TargetMachine &TM);
44     ~StackMapShadowTracker();
45     void startFunction(MachineFunction &MF);
46     void count(MCInst &Inst, const MCSubtargetInfo &STI);
47 
48     // Called to signal the start of a shadow of RequiredSize bytes.
reset(unsigned RequiredSize)49     void reset(unsigned RequiredSize) {
50       RequiredShadowSize = RequiredSize;
51       CurrentShadowSize = 0;
52       InShadow = true;
53     }
54 
55     // Called before every stackmap/patchpoint, and at the end of basic blocks,
56     // to emit any necessary padding-NOPs.
57     void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
58   private:
59     TargetMachine &TM;
60     std::unique_ptr<MCCodeEmitter> CodeEmitter;
61     bool InShadow;
62 
63     // RequiredShadowSize holds the length of the shadow specified in the most
64     // recently encountered STACKMAP instruction.
65     // CurrentShadowSize counts the number of bytes encoded since the most
66     // recently encountered STACKMAP, stopping when that number is greater than
67     // or equal to RequiredShadowSize.
68     unsigned RequiredShadowSize, CurrentShadowSize;
69   };
70 
71   StackMapShadowTracker SMShadowTracker;
72 
73   // All instructions emitted by the X86AsmPrinter should use this helper
74   // method.
75   //
76   // This helper function invokes the SMShadowTracker on each instruction before
77   // outputting it to the OutStream. This allows the shadow tracker to minimise
78   // the number of NOPs used for stackmap padding.
79   void EmitAndCountInstruction(MCInst &Inst);
80 
81   void InsertStackMapShadows(MachineFunction &MF);
82   void LowerSTACKMAP(const MachineInstr &MI);
83   void LowerPATCHPOINT(const MachineInstr &MI);
84 
85   void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
86 
87  public:
X86AsmPrinter(TargetMachine & TM,MCStreamer & Streamer)88   explicit X86AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
89     : AsmPrinter(TM, Streamer), SM(*this), SMShadowTracker(TM) {
90     Subtarget = &TM.getSubtarget<X86Subtarget>();
91   }
92 
getPassName()93   const char *getPassName() const override {
94     return "X86 Assembly / Object Emitter";
95   }
96 
getSubtarget()97   const X86Subtarget &getSubtarget() const { return *Subtarget; }
98 
99   void EmitStartOfAsmFile(Module &M) override;
100 
101   void EmitEndOfAsmFile(Module &M) override;
102 
103   void EmitInstruction(const MachineInstr *MI) override;
104 
EmitBasicBlockEnd(const MachineBasicBlock & MBB)105   void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
106     SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
107   }
108 
109   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
110                        unsigned AsmVariant, const char *ExtraCode,
111                        raw_ostream &OS) override;
112   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
113                              unsigned AsmVariant, const char *ExtraCode,
114                              raw_ostream &OS) override;
115 
116   /// \brief Return the symbol for the specified constant pool entry.
117   MCSymbol *GetCPISymbol(unsigned CPID) const override;
118 
doInitialization(Module & M)119   bool doInitialization(Module &M) override {
120     SMShadowTracker.reset(0);
121     SM.reset();
122     return AsmPrinter::doInitialization(M);
123   }
124 
125   bool runOnMachineFunction(MachineFunction &F) override;
126 };
127 
128 } // end namespace llvm
129 
130 #endif
131