blob: b5779b575999a60dbd00e42bcd11bf569077238f [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;
Sanjoy Dasc0441c22016-04-19 05:24:47 +000032 std::unique_ptr<MCCodeEmitter> CodeEmitter;
Andrew Trick153ebe62013-10-31 22:11:56 +000033
Lang Hamesf49bc3f2014-07-24 20:40:55 +000034 // This utility class tracks the length of a stackmap instruction's 'shadow'.
35 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
36 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
37 // 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 number
40 // of instruction bytes output since the last stackmap. Only if there are too
41 // few instruction bytes to cover the shadow are NOPs used for padding.
42 class StackMapShadowTracker {
43 public:
Sanjoy Das2effffd2016-04-19 18:48:16 +000044 void startFunction(MachineFunction &MF) {
45 this->MF = &MF;
46 }
Sanjoy Dasc0441c22016-04-19 05:24:47 +000047 void count(MCInst &Inst, const MCSubtargetInfo &STI,
48 MCCodeEmitter *CodeEmitter);
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:
Eric Christopherad1ef042015-02-20 08:01:55 +000061 const MachineFunction *MF;
Sanjoy Das2effffd2016-04-19 18:48:16 +000062 bool InShadow = false;
Lang Hames54326492014-07-25 02:29:19 +000063
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.
Sanjoy Das2effffd2016-04-19 18:48:16 +000069 unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000070 };
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);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000081 void LowerSTACKMAP(const MachineInstr &MI);
Lang Hames65613a62015-04-22 06:02:31 +000082 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Das2e0d29f2015-05-06 23:53:26 +000083 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000084 void LowerFAULTING_LOAD_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Dasc0441c22016-04-19 05:24:47 +000085 void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000086
87 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
88
Dean Michael Berris52735fc2016-07-14 04:06:33 +000089 // XRay-specific lowering for X86.
90 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
91 X86MCInstLower &MCIL);
92 void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
93 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
94
95 // Helper function that emits the XRay sleds we've collected for a particular
96 // function.
97 void EmitXRayTable();
98
Dean Michael Berris52735fc2016-07-14 04:06:33 +000099public:
100 explicit X86AsmPrinter(TargetMachine &TM,
101 std::unique_ptr<MCStreamer> Streamer)
102 : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {}
Chris Lattnerb9740462005-07-01 22:44:09 +0000103
Mehdi Amini117296c2016-10-01 02:56:57 +0000104 StringRef getPassName() const override {
Eli Benderskyb0b13b22013-02-19 16:38:32 +0000105 return "X86 Assembly / Object Emitter";
Chris Lattnerb9740462005-07-01 22:44:09 +0000106 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000107
Chris Lattner31722082009-09-12 20:34:57 +0000108 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +0000109
Craig Topper24e685f2014-03-10 05:29:18 +0000110 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +0000111
Craig Topper24e685f2014-03-10 05:29:18 +0000112 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000113
Craig Topper24e685f2014-03-10 05:29:18 +0000114 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000115
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000116 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
Lang Hames9ff69c82015-04-24 19:11:51 +0000117 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000118 }
119
Craig Topper24e685f2014-03-10 05:29:18 +0000120 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
121 unsigned AsmVariant, const char *ExtraCode,
122 raw_ostream &OS) override;
123 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
124 unsigned AsmVariant, const char *ExtraCode,
125 raw_ostream &OS) override;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000126
David Majnemer8bce66b2014-07-14 22:57:27 +0000127 /// \brief Return the symbol for the specified constant pool entry.
128 MCSymbol *GetCPISymbol(unsigned CPID) const override;
129
Yaron Keren559b47d2014-09-17 09:25:36 +0000130 bool doInitialization(Module &M) override {
131 SMShadowTracker.reset(0);
132 SM.reset();
133 return AsmPrinter::doInitialization(M);
134 }
135
Craig Topper24e685f2014-03-10 05:29:18 +0000136 bool runOnMachineFunction(MachineFunction &F) override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000137};
138
Chris Lattnerb9740462005-07-01 22:44:09 +0000139} // end namespace llvm
140
141#endif