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/StackMaps.h"
16 #include "llvm/Target/TargetMachine.h"
18 // Implemented in X86MCInstLower.cpp
27 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
28 const X86Subtarget *Subtarget;
31 void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
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.
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 {
43 StackMapShadowTracker(TargetMachine &TM);
44 ~StackMapShadowTracker();
45 void startFunction(MachineFunction &MF);
46 void count(MCInst &Inst, const MCSubtargetInfo &STI);
48 // Called to signal the start of a shadow of RequiredSize bytes.
49 void reset(unsigned RequiredSize) {
50 RequiredShadowSize = RequiredSize;
51 CurrentShadowSize = 0;
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);
60 const MachineFunction *MF;
61 std::unique_ptr<MCCodeEmitter> CodeEmitter;
64 // RequiredShadowSize holds the length of the shadow specified in the most
65 // recently encountered STACKMAP instruction.
66 // CurrentShadowSize counts the number of bytes encoded since the most
67 // recently encountered STACKMAP, stopping when that number is greater than
68 // or equal to RequiredShadowSize.
69 unsigned RequiredShadowSize, CurrentShadowSize;
72 StackMapShadowTracker SMShadowTracker;
74 // All instructions emitted by the X86AsmPrinter should use this helper
77 // This helper function invokes the SMShadowTracker on each instruction before
78 // outputting it to the OutStream. This allows the shadow tracker to minimise
79 // the number of NOPs used for stackmap padding.
80 void EmitAndCountInstruction(MCInst &Inst);
82 void InsertStackMapShadows(MachineFunction &MF);
83 void LowerSTACKMAP(const MachineInstr &MI);
84 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
85 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
87 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
90 explicit X86AsmPrinter(TargetMachine &TM,
91 std::unique_ptr<MCStreamer> Streamer)
92 : AsmPrinter(TM, std::move(Streamer)), SM(*this), SMShadowTracker(TM) {}
94 const char *getPassName() const override {
95 return "X86 Assembly / Object Emitter";
98 const X86Subtarget &getSubtarget() const { return *Subtarget; }
100 void EmitStartOfAsmFile(Module &M) override;
102 void EmitEndOfAsmFile(Module &M) override;
104 void EmitInstruction(const MachineInstr *MI) override;
106 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
107 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
110 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
111 unsigned AsmVariant, const char *ExtraCode,
112 raw_ostream &OS) override;
113 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
114 unsigned AsmVariant, const char *ExtraCode,
115 raw_ostream &OS) override;
117 /// \brief Return the symbol for the specified constant pool entry.
118 MCSymbol *GetCPISymbol(unsigned CPID) const override;
120 bool doInitialization(Module &M) override {
121 SMShadowTracker.reset(0);
123 return AsmPrinter::doInitialization(M);
126 bool runOnMachineFunction(MachineFunction &F) override;
129 } // end namespace llvm