blob: 4b9913c01e63714a79a4e6ba9cd18184d39fb1ac [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
Chris Lattner5159bbaf2009-09-20 07:41:30 +000010#ifndef X86ASMPRINTER_H
11#define 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);
47 void reset(unsigned RequiredSize) {
48 RequiredShadowSize = RequiredSize;
49 CurrentShadowSize = 0;
50 Count = true;
51 }
52 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
53 private:
54 TargetMachine &TM;
55 std::unique_ptr<MCCodeEmitter> CodeEmitter;
56 bool Count;
57 unsigned RequiredShadowSize, CurrentShadowSize;
58 };
59
60 StackMapShadowTracker SMShadowTracker;
61
62 // All instructions emitted by the X86AsmPrinter should use this helper
63 // method.
64 //
65 // This helper function invokes the SMShadowTracker on each instruction before
66 // outputting it to the OutStream. This allows the shadow tracker to minimise
67 // the number of NOPs used for stackmap padding.
68 void EmitAndCountInstruction(MCInst &Inst);
69
70 void InsertStackMapShadows(MachineFunction &MF);
71 void LowerSTACKMAP(const MachineInstr &MI);
72 void LowerPATCHPOINT(const MachineInstr &MI);
73
74 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
75
Bill Wendlingc5437ea2009-02-24 08:30:20 +000076 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000077 explicit X86AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
Lang Hamesf49bc3f2014-07-24 20:40:55 +000078 : AsmPrinter(TM, Streamer), SM(*this), SMShadowTracker(TM) {
Anton Korobeynikov016d1d02008-06-28 11:08:27 +000079 Subtarget = &TM.getSubtarget<X86Subtarget>();
80 }
Chris Lattnerb9740462005-07-01 22:44:09 +000081
Craig Topper24e685f2014-03-10 05:29:18 +000082 const char *getPassName() const override {
Eli Benderskyb0b13b22013-02-19 16:38:32 +000083 return "X86 Assembly / Object Emitter";
Chris Lattnerb9740462005-07-01 22:44:09 +000084 }
Chad Rosier24c19d22012-08-01 18:39:17 +000085
Chris Lattner31722082009-09-12 20:34:57 +000086 const X86Subtarget &getSubtarget() const { return *Subtarget; }
Chris Lattnerb9740462005-07-01 22:44:09 +000087
Craig Topper24e685f2014-03-10 05:29:18 +000088 void EmitStartOfAsmFile(Module &M) override;
Chris Lattner81fe45d2010-03-13 02:10:00 +000089
Craig Topper24e685f2014-03-10 05:29:18 +000090 void EmitEndOfAsmFile(Module &M) override;
Chad Rosier24c19d22012-08-01 18:39:17 +000091
Craig Topper24e685f2014-03-10 05:29:18 +000092 void EmitInstruction(const MachineInstr *MI) override;
Chad Rosier24c19d22012-08-01 18:39:17 +000093
Lang Hamesf49bc3f2014-07-24 20:40:55 +000094 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
95 SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
96 }
97
Craig Topper24e685f2014-03-10 05:29:18 +000098 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
99 unsigned AsmVariant, const char *ExtraCode,
100 raw_ostream &OS) override;
101 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
102 unsigned AsmVariant, const char *ExtraCode,
103 raw_ostream &OS) override;
Anton Korobeynikov016d1d02008-06-28 11:08:27 +0000104
David Majnemer8bce66b2014-07-14 22:57:27 +0000105 /// \brief Return the symbol for the specified constant pool entry.
106 MCSymbol *GetCPISymbol(unsigned CPID) const override;
107
Craig Topper24e685f2014-03-10 05:29:18 +0000108 bool runOnMachineFunction(MachineFunction &F) override;
Chris Lattnerb9740462005-07-01 22:44:09 +0000109};
110
Chris Lattnerb9740462005-07-01 22:44:09 +0000111} // end namespace llvm
112
113#endif