blob: 3beeb1752bf5b65bd0371c8b2344457457a23177 [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"
Andrew Trick153ebe62013-10-31 22:11:56 +000015#include "llvm/CodeGen/StackMaps.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000017
Lang Hamesf49bc3f2014-07-24 20:40:55 +000018// Implemented in X86MCInstLower.cpp
19namespace {
20 class X86MCInstLower;
21}
22
Chris Lattnerb9740462005-07-01 22:44:09 +000023namespace llvm {
Chris Lattner9f40bc22009-06-24 05:46:28 +000024class MCStreamer;
Saleem Abdulrasool75e68cb2014-05-04 00:03:41 +000025class MCSymbol;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000026
Duncan Sands6c5e4352010-05-11 20:16:09 +000027class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000028 const X86Subtarget *Subtarget;
Andrew Trick153ebe62013-10-31 22:11:56 +000029 StackMaps SM;
30
Saleem Abdulrasool75e68cb2014-05-04 00:03:41 +000031 void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
32
Lang Hamesf49bc3f2014-07-24 20:40:55 +000033 // 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.
37 //
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 {
42 public:
43 StackMapShadowTracker(TargetMachine &TM);
44 ~StackMapShadowTracker();
45 void startFunction(MachineFunction &MF);
46 void count(MCInst &Inst, const MCSubtargetInfo &STI);
Lang Hames54326492014-07-25 02:29:19 +000047
48 // Called to signal the start of a shadow of RequiredSize bytes.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000049 void reset(unsigned RequiredSize) {
50 RequiredShadowSize = RequiredSize;
51 CurrentShadowSize = 0;
Lang Hames54326492014-07-25 02:29:19 +000052 InShadow = true;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000053 }
Lang Hames54326492014-07-25 02:29:19 +000054
55 // Called before every stackmap/patchpoint, and at the end of basic blocks,
56 // to emit any necessary padding-NOPs.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000057 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
58 private:
59 TargetMachine &TM;
Eric Christopherad1ef042015-02-20 08:01:55 +000060 const MachineFunction *MF;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000061 std::unique_ptr<MCCodeEmitter> CodeEmitter;
Lang Hames54326492014-07-25 02:29:19 +000062 bool InShadow;
63
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.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000069 unsigned RequiredShadowSize, CurrentShadowSize;
70 };
71
72 StackMapShadowTracker SMShadowTracker;
73
74 // All instructions emitted by the X86AsmPrinter should use this helper
75 // method.
76 //
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);
81
82 void InsertStackMapShadows(MachineFunction &MF);
83 void LowerSTACKMAP(const MachineInstr &MI);
Lang Hames65613a62015-04-22 06:02:31 +000084 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Das2e0d29f2015-05-06 23:53:26 +000085 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000086
87 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
88
Bill Wendlingc5437ea2009-02-24 08:30:20 +000089 public:
David Blaikie94598322015-01-18 20:29:04 +000090 explicit X86AsmPrinter(TargetMachine &TM,
91 std::unique_ptr<MCStreamer> Streamer)
Eric Christopherd7dec662015-02-05 19:06:45 +000092 : AsmPrinter(TM, std::move(Streamer)), SM(*this), SMShadowTracker(TM) {}
Chris Lattnerb9740462005-07-01 22:44:09 +000093
Craig Topper24e685f2014-03-10 05:29:18 +000094 const char *getPassName() const override {
Eli Benderskyb0b13b22013-02-19 16:38:32 +000095 return "X86 Assembly / Object Emitter";
Chris Lattnerb9740462005-07-01 22:44:09 +000096 }
Chad Rosier24c19d22012-08-01 18:39:17 +000097
Chris Lattner31722082009-09-12 20:34:57 +000098 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +000099
Craig Topper24e685f2014-03-10 05:29:18 +0000100 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +0000101
Craig Topper24e685f2014-03-10 05:29:18 +0000102 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000103
Craig Topper24e685f2014-03-10 05:29:18 +0000104 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000105
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000106 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
Lang Hames9ff69c82015-04-24 19:11:51 +0000107 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000108 }
109
Craig Topper24e685f2014-03-10 05:29:18 +0000110 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;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000116
David Majnemer8bce66b2014-07-14 22:57:27 +0000117 /// \brief Return the symbol for the specified constant pool entry.
118 MCSymbol *GetCPISymbol(unsigned CPID) const override;
119
Yaron Keren559b47d2014-09-17 09:25:36 +0000120 bool doInitialization(Module &M) override {
121 SMShadowTracker.reset(0);
122 SM.reset();
123 return AsmPrinter::doInitialization(M);
124 }
125
Craig Topper24e685f2014-03-10 05:29:18 +0000126 bool runOnMachineFunction(MachineFunction &F) override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000127};
128
Chris Lattnerb9740462005-07-01 22:44:09 +0000129} // end namespace llvm
130
131#endif