Path: blob/main/contrib/llvm-project/llvm/lib/Target/X86/X86AsmPrinter.h
35269 views
//===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H9#define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H1011#include "llvm/CodeGen/AsmPrinter.h"12#include "llvm/CodeGen/FaultMaps.h"13#include "llvm/CodeGen/StackMaps.h"1415// Implemented in X86MCInstLower.cpp16namespace {17class X86MCInstLower;18}1920namespace llvm {21class MCCodeEmitter;22class MCStreamer;23class X86Subtarget;24class TargetMachine;2526class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {27const X86Subtarget *Subtarget = nullptr;28FaultMaps FM;29std::unique_ptr<MCCodeEmitter> CodeEmitter;30bool EmitFPOData = false;31bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;32bool IndCSPrefix = false;3334// This utility class tracks the length of a stackmap instruction's 'shadow'.35// It is used by the X86AsmPrinter to ensure that the stackmap shadow36// invariants (i.e. no other stackmaps, patchpoints, or control flow within37// the shadow) are met, while outputting a minimal number of NOPs for padding.38//39// To minimise the number of NOPs used, the shadow tracker counts the number40// of instruction bytes output since the last stackmap. Only if there are too41// few instruction bytes to cover the shadow are NOPs used for padding.42class StackMapShadowTracker {43public:44void startFunction(MachineFunction &MF) {45this->MF = &MF;46}47void count(MCInst &Inst, const MCSubtargetInfo &STI,48MCCodeEmitter *CodeEmitter);4950// Called to signal the start of a shadow of RequiredSize bytes.51void reset(unsigned RequiredSize) {52RequiredShadowSize = RequiredSize;53CurrentShadowSize = 0;54InShadow = true;55}5657// Called before every stackmap/patchpoint, and at the end of basic blocks,58// to emit any necessary padding-NOPs.59void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);60private:61const MachineFunction *MF = nullptr;62bool InShadow = false;6364// RequiredShadowSize holds the length of the shadow specified in the most65// recently encountered STACKMAP instruction.66// CurrentShadowSize counts the number of bytes encoded since the most67// recently encountered STACKMAP, stopping when that number is greater than68// or equal to RequiredShadowSize.69unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;70};7172StackMapShadowTracker SMShadowTracker;7374// All instructions emitted by the X86AsmPrinter should use this helper75// method.76//77// This helper function invokes the SMShadowTracker on each instruction before78// outputting it to the OutStream. This allows the shadow tracker to minimise79// the number of NOPs used for stackmap padding.80void EmitAndCountInstruction(MCInst &Inst);81void LowerSTACKMAP(const MachineInstr &MI);82void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);83void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);84void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);85void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);8687void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);8889// XRay-specific lowering for X86.90void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,91X86MCInstLower &MCIL);92void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);93void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);94void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);95void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI,96X86MCInstLower &MCIL);9798void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);99100// KCFI specific lowering for X86.101uint32_t MaskKCFIType(uint32_t Value);102void EmitKCFITypePadding(const MachineFunction &MF, bool HasType = true);103void LowerKCFI_CHECK(const MachineInstr &MI);104105// Address sanitizer specific lowering for X86.106void LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI);107108// Choose between emitting .seh_ directives and .cv_fpo_ directives.109void EmitSEHInstruction(const MachineInstr *MI);110111void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;112void PrintOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);113void PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo,114raw_ostream &O, const char *Modifier);115void PrintPCRelImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);116void PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo,117raw_ostream &O, const char *Modifier);118void PrintMemReference(const MachineInstr *MI, unsigned OpNo, raw_ostream &O,119const char *Modifier);120void PrintIntelMemReference(const MachineInstr *MI, unsigned OpNo,121raw_ostream &O, const char *Modifier);122const MCSubtargetInfo *getIFuncMCSubtargetInfo() const override;123void emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI,124MCSymbol *LazyPointer) override;125void emitMachOIFuncStubHelperBody(Module &M, const GlobalIFunc &GI,126MCSymbol *LazyPointer) override;127128public:129X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);130131StringRef getPassName() const override {132return "X86 Assembly Printer";133}134135const X86Subtarget &getSubtarget() const { return *Subtarget; }136137void emitStartOfAsmFile(Module &M) override;138139void emitEndOfAsmFile(Module &M) override;140141void emitInstruction(const MachineInstr *MI) override;142143void emitBasicBlockEnd(const MachineBasicBlock &MBB) override;144145bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,146const char *ExtraCode, raw_ostream &O) override;147bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,148const char *ExtraCode, raw_ostream &O) override;149150bool doInitialization(Module &M) override {151SMShadowTracker.reset(0);152SM.reset();153FM.reset();154return AsmPrinter::doInitialization(M);155}156157bool runOnMachineFunction(MachineFunction &MF) override;158void emitFunctionBodyStart() override;159void emitFunctionBodyEnd() override;160void emitKCFITypeId(const MachineFunction &MF) override;161162bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const override {163return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;164}165};166167} // end namespace llvm168169#endif170171172