blob: 3a3cbadc365d3604f8d161cdd4789175a3415601 [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;
60 std::unique_ptr<MCCodeEmitter> CodeEmitter;
Lang Hames54326492014-07-25 02:29:19 +000061 bool InShadow;
62
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.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000068 unsigned RequiredShadowSize, CurrentShadowSize;
69 };
70
71 StackMapShadowTracker SMShadowTracker;
72
73 // All instructions emitted by the X86AsmPrinter should use this helper
74 // method.
75 //
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);
80
81 void InsertStackMapShadows(MachineFunction &MF);
82 void LowerSTACKMAP(const MachineInstr &MI);
83 void LowerPATCHPOINT(const MachineInstr &MI);
84
85 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
86
Bill Wendlingc5437ea2009-02-24 08:30:20 +000087 public:
David Blaikie94598322015-01-18 20:29:04 +000088 explicit X86AsmPrinter(TargetMachine &TM,
89 std::unique_ptr<MCStreamer> Streamer)
Eric Christopherd7dec662015-02-05 19:06:45 +000090 : AsmPrinter(TM, std::move(Streamer)), SM(*this), SMShadowTracker(TM) {}
Chris Lattnerb9740462005-07-01 22:44:09 +000091
Craig Topper24e685f2014-03-10 05:29:18 +000092 const char *getPassName() const override {
Eli Benderskyb0b13b22013-02-19 16:38:32 +000093 return "X86 Assembly / Object Emitter";
Chris Lattnerb9740462005-07-01 22:44:09 +000094 }
Chad Rosier24c19d22012-08-01 18:39:17 +000095
Chris Lattner31722082009-09-12 20:34:57 +000096 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +000097
Craig Topper24e685f2014-03-10 05:29:18 +000098 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +000099
Craig Topper24e685f2014-03-10 05:29:18 +0000100 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000101
Craig Topper24e685f2014-03-10 05:29:18 +0000102 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000103
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000104 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
105 SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
106 }
107
Craig Topper24e685f2014-03-10 05:29:18 +0000108 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
109 unsigned AsmVariant, const char *ExtraCode,
110 raw_ostream &OS) override;
111 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
112 unsigned AsmVariant, const char *ExtraCode,
113 raw_ostream &OS) override;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000114
David Majnemer8bce66b2014-07-14 22:57:27 +0000115 /// \brief Return the symbol for the specified constant pool entry.
116 MCSymbol *GetCPISymbol(unsigned CPID) const override;
117
Yaron Keren559b47d2014-09-17 09:25:36 +0000118 bool doInitialization(Module &M) override {
119 SMShadowTracker.reset(0);
120 SM.reset();
121 return AsmPrinter::doInitialization(M);
122 }
123
Craig Topper24e685f2014-03-10 05:29:18 +0000124 bool runOnMachineFunction(MachineFunction &F) override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000125};
126
Chris Lattnerb9740462005-07-01 22:44:09 +0000127} // end namespace llvm
128
129#endif