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 std::unique_ptr<MCCodeEmitter> CodeEmitter;
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;
71 StackMapShadowTracker SMShadowTracker;
73 // All instructions emitted by the X86AsmPrinter should use this helper
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);
81 void InsertStackMapShadows(MachineFunction &MF);
82 void LowerSTACKMAP(const MachineInstr &MI);
83 void LowerPATCHPOINT(const MachineInstr &MI);
85 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
88 explicit X86AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
89 : AsmPrinter(TM, Streamer), SM(*this), SMShadowTracker(TM) {
90 Subtarget = &TM.getSubtarget<X86Subtarget>();
93 const char *getPassName() const override {
94 return "X86 Assembly / Object Emitter";
97 const X86Subtarget &getSubtarget() const { return *Subtarget; }
99 void EmitStartOfAsmFile(Module &M) override;
101 void EmitEndOfAsmFile(Module &M) override;
103 void EmitInstruction(const MachineInstr *MI) override;
105 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
106 SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
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;
116 /// \brief Return the symbol for the specified constant pool entry.
117 MCSymbol *GetCPISymbol(unsigned CPID) const override;
119 bool runOnMachineFunction(MachineFunction &F) override;
122 } // end namespace llvm