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 X86ASMPRINTER_H
11 #define 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);
47 void reset(unsigned RequiredSize) {
48 RequiredShadowSize = RequiredSize;
49 CurrentShadowSize = 0;
52 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
55 std::unique_ptr<MCCodeEmitter> CodeEmitter;
57 unsigned RequiredShadowSize, CurrentShadowSize;
60 StackMapShadowTracker SMShadowTracker;
62 // All instructions emitted by the X86AsmPrinter should use this helper
65 // This helper function invokes the SMShadowTracker on each instruction before
66 // outputting it to the OutStream. This allows the shadow tracker to minimise
67 // the number of NOPs used for stackmap padding.
68 void EmitAndCountInstruction(MCInst &Inst);
70 void InsertStackMapShadows(MachineFunction &MF);
71 void LowerSTACKMAP(const MachineInstr &MI);
72 void LowerPATCHPOINT(const MachineInstr &MI);
74 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
77 explicit X86AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
78 : AsmPrinter(TM, Streamer), SM(*this), SMShadowTracker(TM) {
79 Subtarget = &TM.getSubtarget<X86Subtarget>();
82 const char *getPassName() const override {
83 return "X86 Assembly / Object Emitter";
86 const X86Subtarget &getSubtarget() const { return *Subtarget; }
88 void EmitStartOfAsmFile(Module &M) override;
90 void EmitEndOfAsmFile(Module &M) override;
92 void EmitInstruction(const MachineInstr *MI) override;
94 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
95 SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
98 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
99 unsigned AsmVariant, const char *ExtraCode,
100 raw_ostream &OS) override;
101 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
102 unsigned AsmVariant, const char *ExtraCode,
103 raw_ostream &OS) override;
105 /// \brief Return the symbol for the specified constant pool entry.
106 MCSymbol *GetCPISymbol(unsigned CPID) const override;
108 bool runOnMachineFunction(MachineFunction &F) override;
111 } // end namespace llvm