blob: 7e70789ac82c31714598a67f402a2f220f7c24d5 [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"
David Blaikie6a2b1242017-10-24 21:29:14 +000017#include "llvm/MC/MCCodeEmitter.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000019
Lang Hamesf49bc3f2014-07-24 20:40:55 +000020// Implemented in X86MCInstLower.cpp
21namespace {
22 class X86MCInstLower;
23}
24
Chris Lattnerb9740462005-07-01 22:44:09 +000025namespace llvm {
Chris Lattner9f40bc22009-06-24 05:46:28 +000026class MCStreamer;
Saleem Abdulrasool75e68cb2014-05-04 00:03:41 +000027class MCSymbol;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000028
Duncan Sands6c5e4352010-05-11 20:16:09 +000029class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000030 const X86Subtarget *Subtarget;
Andrew Trick153ebe62013-10-31 22:11:56 +000031 StackMaps SM;
Sanjoy Dasc63244d2015-06-15 18:44:08 +000032 FaultMaps FM;
Sanjoy Dasc0441c22016-04-19 05:24:47 +000033 std::unique_ptr<MCCodeEmitter> CodeEmitter;
Reid Kleckner9cdd4df2017-10-11 21:24:33 +000034 bool EmitFPOData = false;
Andrew Trick153ebe62013-10-31 22:11:56 +000035
Lang Hamesf49bc3f2014-07-24 20:40:55 +000036 // This utility class tracks the length of a stackmap instruction's 'shadow'.
37 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
38 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
39 // the shadow) are met, while outputting a minimal number of NOPs for padding.
40 //
41 // To minimise the number of NOPs used, the shadow tracker counts the number
42 // of instruction bytes output since the last stackmap. Only if there are too
43 // few instruction bytes to cover the shadow are NOPs used for padding.
44 class StackMapShadowTracker {
45 public:
Sanjoy Das2effffd2016-04-19 18:48:16 +000046 void startFunction(MachineFunction &MF) {
47 this->MF = &MF;
48 }
Sanjoy Dasc0441c22016-04-19 05:24:47 +000049 void count(MCInst &Inst, const MCSubtargetInfo &STI,
50 MCCodeEmitter *CodeEmitter);
Lang Hames54326492014-07-25 02:29:19 +000051
52 // Called to signal the start of a shadow of RequiredSize bytes.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000053 void reset(unsigned RequiredSize) {
54 RequiredShadowSize = RequiredSize;
55 CurrentShadowSize = 0;
Lang Hames54326492014-07-25 02:29:19 +000056 InShadow = true;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000057 }
Lang Hames54326492014-07-25 02:29:19 +000058
59 // Called before every stackmap/patchpoint, and at the end of basic blocks,
60 // to emit any necessary padding-NOPs.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000061 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
62 private:
Eric Christopherad1ef042015-02-20 08:01:55 +000063 const MachineFunction *MF;
Sanjoy Das2effffd2016-04-19 18:48:16 +000064 bool InShadow = false;
Lang Hames54326492014-07-25 02:29:19 +000065
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.
Sanjoy Das2effffd2016-04-19 18:48:16 +000071 unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000072 };
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);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000083 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);
Sanjoy Das2f63cbc2017-02-07 19:19:49 +000086 void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Dasc0441c22016-04-19 05:24:47 +000087 void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000088
89 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
90
Dean Michael Berris52735fc2016-07-14 04:06:33 +000091 // XRay-specific lowering for X86.
92 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
93 X86MCInstLower &MCIL);
94 void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
95 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
Dean Michael Berris9bcaed82017-05-08 05:45:21 +000096 void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
Dean Michael Berris52735fc2016-07-14 04:06:33 +000097
Nirav Davea7c041d2017-01-31 17:00:27 +000098 void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
99
Reid Kleckner9cdd4df2017-10-11 21:24:33 +0000100 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
101 void EmitSEHInstruction(const MachineInstr *MI);
102
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000103public:
Reid Klecknerec4ff242017-10-11 23:53:12 +0000104 X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
Chris Lattnerb9740462005-07-01 22:44:09 +0000105
Mehdi Amini117296c2016-10-01 02:56:57 +0000106 StringRef getPassName() const override {
Davide Italianoa22dddd2016-11-10 18:39:31 +0000107 return "X86 Assembly Printer";
Chris Lattnerb9740462005-07-01 22:44:09 +0000108 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000109
Chris Lattner31722082009-09-12 20:34:57 +0000110 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +0000111
Craig Topper24e685f2014-03-10 05:29:18 +0000112 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +0000113
Craig Topper24e685f2014-03-10 05:29:18 +0000114 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000115
Craig Topper24e685f2014-03-10 05:29:18 +0000116 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000117
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000118 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000119 AsmPrinter::EmitBasicBlockEnd(MBB);
Lang Hames9ff69c82015-04-24 19:11:51 +0000120 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000121 }
122
Craig Topper24e685f2014-03-10 05:29:18 +0000123 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
124 unsigned AsmVariant, const char *ExtraCode,
125 raw_ostream &OS) override;
126 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
127 unsigned AsmVariant, const char *ExtraCode,
128 raw_ostream &OS) override;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000129
David Majnemer8bce66b2014-07-14 22:57:27 +0000130 /// \brief Return the symbol for the specified constant pool entry.
131 MCSymbol *GetCPISymbol(unsigned CPID) const override;
132
Yaron Keren559b47d2014-09-17 09:25:36 +0000133 bool doInitialization(Module &M) override {
134 SMShadowTracker.reset(0);
135 SM.reset();
Yichao Yua46eb8e2017-10-17 11:44:34 +0000136 FM.reset();
Yaron Keren559b47d2014-09-17 09:25:36 +0000137 return AsmPrinter::doInitialization(M);
138 }
139
Craig Topper24e685f2014-03-10 05:29:18 +0000140 bool runOnMachineFunction(MachineFunction &F) override;
Reid Kleckner9cdd4df2017-10-11 21:24:33 +0000141 void EmitFunctionBodyStart() override;
142 void EmitFunctionBodyEnd() override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000143};
144
Chris Lattnerb9740462005-07-01 22:44:09 +0000145} // end namespace llvm
146
147#endif