blob: 3a20a9362de1b586243871da41ea02c78afd7838 [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;
Chandler Carruthc58f2162018-01-22 22:05:25 +000035 bool NeedsRetpoline = false;
Andrew Trick153ebe62013-10-31 22:11:56 +000036
Lang Hamesf49bc3f2014-07-24 20:40:55 +000037 // This utility class tracks the length of a stackmap instruction's 'shadow'.
38 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
39 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
40 // the shadow) are met, while outputting a minimal number of NOPs for padding.
41 //
42 // To minimise the number of NOPs used, the shadow tracker counts the number
43 // of instruction bytes output since the last stackmap. Only if there are too
44 // few instruction bytes to cover the shadow are NOPs used for padding.
45 class StackMapShadowTracker {
46 public:
Sanjoy Das2effffd2016-04-19 18:48:16 +000047 void startFunction(MachineFunction &MF) {
48 this->MF = &MF;
49 }
Sanjoy Dasc0441c22016-04-19 05:24:47 +000050 void count(MCInst &Inst, const MCSubtargetInfo &STI,
51 MCCodeEmitter *CodeEmitter);
Lang Hames54326492014-07-25 02:29:19 +000052
53 // Called to signal the start of a shadow of RequiredSize bytes.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000054 void reset(unsigned RequiredSize) {
55 RequiredShadowSize = RequiredSize;
56 CurrentShadowSize = 0;
Lang Hames54326492014-07-25 02:29:19 +000057 InShadow = true;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000058 }
Lang Hames54326492014-07-25 02:29:19 +000059
60 // Called before every stackmap/patchpoint, and at the end of basic blocks,
61 // to emit any necessary padding-NOPs.
Lang Hamesf49bc3f2014-07-24 20:40:55 +000062 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
63 private:
Eric Christopherad1ef042015-02-20 08:01:55 +000064 const MachineFunction *MF;
Sanjoy Das2effffd2016-04-19 18:48:16 +000065 bool InShadow = false;
Lang Hames54326492014-07-25 02:29:19 +000066
67 // RequiredShadowSize holds the length of the shadow specified in the most
68 // recently encountered STACKMAP instruction.
69 // CurrentShadowSize counts the number of bytes encoded since the most
70 // recently encountered STACKMAP, stopping when that number is greater than
71 // or equal to RequiredShadowSize.
Sanjoy Das2effffd2016-04-19 18:48:16 +000072 unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
Lang Hamesf49bc3f2014-07-24 20:40:55 +000073 };
74
75 StackMapShadowTracker SMShadowTracker;
76
77 // All instructions emitted by the X86AsmPrinter should use this helper
78 // method.
79 //
80 // This helper function invokes the SMShadowTracker on each instruction before
81 // outputting it to the OutStream. This allows the shadow tracker to minimise
82 // the number of NOPs used for stackmap padding.
83 void EmitAndCountInstruction(MCInst &Inst);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000084 void LowerSTACKMAP(const MachineInstr &MI);
Lang Hames65613a62015-04-22 06:02:31 +000085 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Das2e0d29f2015-05-06 23:53:26 +000086 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Das2f63cbc2017-02-07 19:19:49 +000087 void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Sanjoy Dasc0441c22016-04-19 05:24:47 +000088 void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
Lang Hamesf49bc3f2014-07-24 20:40:55 +000089
90 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
91
Dean Michael Berris52735fc2016-07-14 04:06:33 +000092 // XRay-specific lowering for X86.
93 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
94 X86MCInstLower &MCIL);
95 void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
96 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
Dean Michael Berris9bcaed82017-05-08 05:45:21 +000097 void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
Keith Wyss3d868232018-04-17 21:30:29 +000098 void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI,
99 X86MCInstLower &MCIL);
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000100
Nirav Davea7c041d2017-01-31 17:00:27 +0000101 void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
102
Reid Kleckner9cdd4df2017-10-11 21:24:33 +0000103 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
104 void EmitSEHInstruction(const MachineInstr *MI);
105
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000106public:
Reid Klecknerec4ff242017-10-11 23:53:12 +0000107 X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
Chris Lattnerb9740462005-07-01 22:44:09 +0000108
Mehdi Amini117296c2016-10-01 02:56:57 +0000109 StringRef getPassName() const override {
Davide Italianoa22dddd2016-11-10 18:39:31 +0000110 return "X86 Assembly Printer";
Chris Lattnerb9740462005-07-01 22:44:09 +0000111 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000112
Chris Lattner31722082009-09-12 20:34:57 +0000113 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +0000114
Craig Topper24e685f2014-03-10 05:29:18 +0000115 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +0000116
Craig Topper24e685f2014-03-10 05:29:18 +0000117 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000118
Craig Topper24e685f2014-03-10 05:29:18 +0000119 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000120
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000121 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000122 AsmPrinter::EmitBasicBlockEnd(MBB);
Lang Hames9ff69c82015-04-24 19:11:51 +0000123 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000124 }
125
Craig Topper24e685f2014-03-10 05:29:18 +0000126 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
127 unsigned AsmVariant, const char *ExtraCode,
128 raw_ostream &OS) override;
129 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
130 unsigned AsmVariant, const char *ExtraCode,
131 raw_ostream &OS) override;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000132
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000133 /// Return the symbol for the specified constant pool entry.
David Majnemer8bce66b2014-07-14 22:57:27 +0000134 MCSymbol *GetCPISymbol(unsigned CPID) const override;
135
Yaron Keren559b47d2014-09-17 09:25:36 +0000136 bool doInitialization(Module &M) override {
137 SMShadowTracker.reset(0);
138 SM.reset();
Yichao Yua46eb8e2017-10-17 11:44:34 +0000139 FM.reset();
Yaron Keren559b47d2014-09-17 09:25:36 +0000140 return AsmPrinter::doInitialization(M);
141 }
142
Craig Topper24e685f2014-03-10 05:29:18 +0000143 bool runOnMachineFunction(MachineFunction &F) override;
Reid Kleckner9cdd4df2017-10-11 21:24:33 +0000144 void EmitFunctionBodyStart() override;
145 void EmitFunctionBodyEnd() override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000146};
147
Chris Lattnerb9740462005-07-01 22:44:09 +0000148} // end namespace llvm
149
150#endif