blob: acba21169c9cee5bbde32ed95d94081dbe10db81 [file] [log] [blame]
Eli Benderskyb0b13b22013-02-19 16:38:32 +00001//===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//
Chris Lattnerb9740462005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb9740462005-07-01 22:44:09 +00007//
8//===----------------------------------------------------------------------===//
Chris Lattnerb9740462005-07-01 22:44:09 +00009
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11#define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
Chris Lattnerb9740462005-07-01 22:44:09 +000012
Craig Topperc6d4efa2014-03-19 06:53:25 +000013#include "X86Subtarget.h"
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000014#include "llvm/CodeGen/AsmPrinter.h"
Sanjoy Dasc63244d2015-06-15 18:44:08 +000015#include "llvm/CodeGen/FaultMaps.h"
Andrew Trick153ebe62013-10-31 22:11:56 +000016#include "llvm/CodeGen/StackMaps.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000017#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000018
Lang Hamesf49bc3f2014-07-24 20:40:55 +000019// Implemented in X86MCInstLower.cpp
20namespace {
21 class X86MCInstLower;
22}
23
Chris Lattnerb9740462005-07-01 22:44:09 +000024namespace llvm {
Chris Lattner9f40bc22009-06-24 05:46:28 +000025class MCStreamer;
Saleem Abdulrasool75e68cb2014-05-04 00:03:41 +000026class MCSymbol;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000027
Duncan Sands6c5e4352010-05-11 20:16:09 +000028class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000029 const X86Subtarget *Subtarget;
Andrew Trick153ebe62013-10-31 22:11:56 +000030 StackMaps SM;
Sanjoy Dasc63244d2015-06-15 18:44:08 +000031 FaultMaps FM;
Andrew Trick153ebe62013-10-31 22:11:56 +000032
Saleem Abdulrasool75e68cb2014-05-04 00:03:41 +000033 void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
34
Lang Hamesf49bc3f2014-07-24 20:40:55 +000035 // 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.
39 //
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 {
44 public:
45 StackMapShadowTracker(TargetMachine &TM);
46 ~StackMapShadowTracker();
47 void startFunction(MachineFunction &MF);
48 void count(MCInst &Inst, const MCSubtargetInfo &STI);
Lang Hames54326492014-07-25 02:29:19 +000049
50 // Called to signal the start of a shadow of RequiredSize bytes.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000051 void reset(unsigned RequiredSize) {
52 RequiredShadowSize = RequiredSize;
53 CurrentShadowSize = 0;
Lang Hames54326492014-07-25 02:29:19 +000054 InShadow = true;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000055 }
Lang Hames54326492014-07-25 02:29:19 +000056
57 // Called before every stackmap/patchpoint, and at the end of basic blocks,
58 // to emit any necessary padding-NOPs.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000059 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
60 private:
61 TargetMachine &TM;
Eric Christopherad1ef042015-02-20 08:01:55 +000062 const MachineFunction *MF;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000063 std::unique_ptr<MCCodeEmitter> CodeEmitter;
Lang Hames54326492014-07-25 02:29:19 +000064 bool InShadow;
65
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.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000071 unsigned RequiredShadowSize, CurrentShadowSize;
72 };
73
74 StackMapShadowTracker SMShadowTracker;
75
76 // All instructions emitted by the X86AsmPrinter should use this helper
77 // method.
78 //
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);
83
84 void InsertStackMapShadows(MachineFunction &MF);
85 void LowerSTACKMAP(const MachineInstr &MI);
Lang Hames65613a62015-04-22 06:02:31 +000086 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Das2e0d29f2015-05-06 23:53:26 +000087 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000088 void LowerFAULTING_LOAD_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000089
90 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
91
Bill Wendlingc5437ea2009-02-24 08:30:20 +000092 public:
David Blaikie94598322015-01-18 20:29:04 +000093 explicit X86AsmPrinter(TargetMachine &TM,
94 std::unique_ptr<MCStreamer> Streamer)
Sanjoy Dasc63244d2015-06-15 18:44:08 +000095 : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this),
96 SMShadowTracker(TM) {}
Chris Lattnerb9740462005-07-01 22:44:09 +000097
Craig Topper24e685f2014-03-10 05:29:18 +000098 const char *getPassName() const override {
Eli Benderskyb0b13b22013-02-19 16:38:32 +000099 return "X86 Assembly / Object Emitter";
Chris Lattnerb9740462005-07-01 22:44:09 +0000100 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000101
Chris Lattner31722082009-09-12 20:34:57 +0000102 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +0000103
Craig Topper24e685f2014-03-10 05:29:18 +0000104 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +0000105
Craig Topper24e685f2014-03-10 05:29:18 +0000106 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000107
Craig Topper24e685f2014-03-10 05:29:18 +0000108 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000109
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000110 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
Lang Hames9ff69c82015-04-24 19:11:51 +0000111 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000112 }
113
Craig Topper24e685f2014-03-10 05:29:18 +0000114 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;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000120
David Majnemer8bce66b2014-07-14 22:57:27 +0000121 /// \brief Return the symbol for the specified constant pool entry.
122 MCSymbol *GetCPISymbol(unsigned CPID) const override;
123
Yaron Keren559b47d2014-09-17 09:25:36 +0000124 bool doInitialization(Module &M) override {
125 SMShadowTracker.reset(0);
126 SM.reset();
127 return AsmPrinter::doInitialization(M);
128 }
129
Craig Topper24e685f2014-03-10 05:29:18 +0000130 bool runOnMachineFunction(MachineFunction &F) override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000131};
132
Chris Lattnerb9740462005-07-01 22:44:09 +0000133} // end namespace llvm
134
135#endif