xref: /freebsd/contrib/llvm-project/llvm/lib/Target/X86/X86AsmPrinter.h (revision da759cfa320d5076b075d15ff3f00ab3ba5634fd)
1 //===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
10 #define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11 
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"
18 
19 // Implemented in X86MCInstLower.cpp
20 namespace {
21   class X86MCInstLower;
22 }
23 
24 namespace llvm {
25 class MCStreamer;
26 class MCSymbol;
27 
28 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
29   const X86Subtarget *Subtarget = nullptr;
30   StackMaps SM;
31   FaultMaps FM;
32   std::unique_ptr<MCCodeEmitter> CodeEmitter;
33   bool EmitFPOData = false;
34   bool NeedsRetpoline = false;
35 
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.
40   //
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 {
45   public:
46     void startFunction(MachineFunction &MF) {
47       this->MF = &MF;
48     }
49     void count(MCInst &Inst, const MCSubtargetInfo &STI,
50                MCCodeEmitter *CodeEmitter);
51 
52     // Called to signal the start of a shadow of RequiredSize bytes.
53     void reset(unsigned RequiredSize) {
54       RequiredShadowSize = RequiredSize;
55       CurrentShadowSize = 0;
56       InShadow = true;
57     }
58 
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);
62   private:
63     const MachineFunction *MF = nullptr;
64     bool InShadow = false;
65 
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;
72   };
73 
74   StackMapShadowTracker SMShadowTracker;
75 
76   // All instructions emitted by the X86AsmPrinter should use this helper
77   // method.
78   //
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);
88 
89   void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
90 
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);
99 
100   void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
101 
102   // Choose between emitting .seh_ directives and .cv_fpo_ directives.
103   void EmitSEHInstruction(const MachineInstr *MI);
104 
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,
115                               raw_ostream &O, const char *Modifier);
116 
117 public:
118   X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
119 
120   StringRef getPassName() const override {
121     return "X86 Assembly Printer";
122   }
123 
124   const X86Subtarget &getSubtarget() const { return *Subtarget; }
125 
126   void EmitStartOfAsmFile(Module &M) override;
127 
128   void EmitEndOfAsmFile(Module &M) override;
129 
130   void EmitInstruction(const MachineInstr *MI) override;
131 
132   void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
133     AsmPrinter::EmitBasicBlockEnd(MBB);
134     SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
135   }
136 
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;
141 
142   bool doInitialization(Module &M) override {
143     SMShadowTracker.reset(0);
144     SM.reset();
145     FM.reset();
146     return AsmPrinter::doInitialization(M);
147   }
148 
149   bool runOnMachineFunction(MachineFunction &F) override;
150   void EmitFunctionBodyStart() override;
151   void EmitFunctionBodyEnd() override;
152 };
153 
154 } // end namespace llvm
155 
156 #endif
157