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/Target/TargetMachine.h"
19 // Implemented in X86MCInstLower.cpp
28 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
29 const X86Subtarget *Subtarget;
33 void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
35 // This utility class tracks the length of a stackmap instruction's 'shadow'.
36 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
37 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
38 // the shadow) are met, while outputting a minimal number of NOPs for padding.
40 // To minimise the number of NOPs used, the shadow tracker counts the number
41 // of instruction bytes output since the last stackmap. Only if there are too
42 // few instruction bytes to cover the shadow are NOPs used for padding.
43 class StackMapShadowTracker {
45 StackMapShadowTracker(TargetMachine &TM);
46 ~StackMapShadowTracker();
47 void startFunction(MachineFunction &MF);
48 void count(MCInst &Inst, const MCSubtargetInfo &STI);
50 // Called to signal the start of a shadow of RequiredSize bytes.
51 void reset(unsigned RequiredSize) {
52 RequiredShadowSize = RequiredSize;
53 CurrentShadowSize = 0;
57 // Called before every stackmap/patchpoint, and at the end of basic blocks,
58 // to emit any necessary padding-NOPs.
59 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
62 const MachineFunction *MF;
63 std::unique_ptr<MCCodeEmitter> CodeEmitter;
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, CurrentShadowSize;
74 StackMapShadowTracker SMShadowTracker;
76 // All instructions emitted by the X86AsmPrinter should use this helper
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);
84 void InsertStackMapShadows(MachineFunction &MF);
85 void LowerSTACKMAP(const MachineInstr &MI);
86 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
87 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
88 void LowerFAULTING_LOAD_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
90 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
93 explicit X86AsmPrinter(TargetMachine &TM,
94 std::unique_ptr<MCStreamer> Streamer)
95 : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this),
96 SMShadowTracker(TM) {}
98 const char *getPassName() const override {
99 return "X86 Assembly / Object Emitter";
102 const X86Subtarget &getSubtarget() const { return *Subtarget; }
104 void EmitStartOfAsmFile(Module &M) override;
106 void EmitEndOfAsmFile(Module &M) override;
108 void EmitInstruction(const MachineInstr *MI) override;
110 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
111 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
114 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
115 unsigned AsmVariant, const char *ExtraCode,
116 raw_ostream &OS) override;
117 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
118 unsigned AsmVariant, const char *ExtraCode,
119 raw_ostream &OS) override;
121 /// \brief Return the symbol for the specified constant pool entry.
122 MCSymbol *GetCPISymbol(unsigned CPID) const override;
124 bool doInitialization(Module &M) override {
125 SMShadowTracker.reset(0);
127 return AsmPrinter::doInitialization(M);
130 bool runOnMachineFunction(MachineFunction &F) override;
133 } // end namespace llvm