1//===-- SystemZAsmPrinter.h - SystemZ LLVM assembly printer ----*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZASMPRINTER_H
10#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZASMPRINTER_H
11
12#include "MCTargetDesc/SystemZTargetStreamer.h"
13#include "SystemZMCInstLower.h"
14#include "SystemZTargetMachine.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/StackMaps.h"
17#include "llvm/MC/MCInstBuilder.h"
18#include "llvm/Support/Compiler.h"
19
20namespace llvm {
21class MCStreamer;
22class MachineInstr;
23class Module;
24class raw_ostream;
25
26class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
27public:
28 static char ID;
29
30private:
31 MCSymbol *CurrentFnPPA1Sym; // PPA1 Symbol.
32 MCSymbol *CurrentFnEPMarkerSym; // Entry Point Marker.
33 MCSymbol *PPA2Sym;
34
35 SystemZTargetStreamer *getTargetStreamer() {
36 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
37 assert(TS && "do not have a target streamer");
38 return static_cast<SystemZTargetStreamer *>(TS);
39 }
40
41 /// Call type information for XPLINK.
42 enum class CallType {
43 BASR76 = 0, // b'x000' == BASR r7,r6
44 BRAS7 = 1, // b'x001' == BRAS r7,ep
45 RESVD_2 = 2, // b'x010'
46 BRASL7 = 3, // b'x011' == BRASL r7,ep
47 RESVD_4 = 4, // b'x100'
48 RESVD_5 = 5, // b'x101'
49 BALR1415 = 6, // b'x110' == BALR r14,r15
50 BASR33 = 7, // b'x111' == BASR r3,r3
51 };
52
53 // The Associated Data Area (ADA) contains descriptors which help locating
54 // external symbols. For each symbol and type, the displacement into the ADA
55 // is stored.
56 class AssociatedDataAreaTable {
57 public:
58 using DisplacementTable =
59 MapVector<std::pair<const MCSymbol *, unsigned>, uint32_t>;
60
61 private:
62 const uint64_t PointerSize;
63
64 /// The mapping of name/slot type pairs to displacements.
65 DisplacementTable Displacements;
66
67 /// The next available displacement value. Incremented when new entries into
68 /// the ADA are created.
69 uint32_t NextDisplacement = 0;
70
71 public:
72 AssociatedDataAreaTable(uint64_t PointerSize) : PointerSize(PointerSize) {}
73
74 /// @brief Add a function descriptor to the ADA.
75 /// @param MI Pointer to an ADA_ENTRY instruction.
76 /// @return The displacement of the descriptor into the ADA.
77 uint32_t insert(const MachineOperand MO);
78
79 /// @brief Get the displacement into associated data area (ADA) for a name.
80 /// If no displacement is already associated with the name, assign one and
81 /// return it.
82 /// @param Sym The symbol for which the displacement should be returned.
83 /// @param SlotKind The ADA type.
84 /// @return The displacement of the descriptor into the ADA.
85 uint32_t insert(const MCSymbol *Sym, unsigned SlotKind);
86
87 /// Get the table of GOFF displacements. This is 'const' since it should
88 /// never be modified by anything except the APIs on this class.
89 const DisplacementTable &getTable() const { return Displacements; }
90
91 uint32_t getNextDisplacement() const { return NextDisplacement; }
92 };
93
94 AssociatedDataAreaTable ADATable;
95
96 void emitPPA1(MCSymbol *FnEndSym);
97 void emitPPA2(Module &M);
98 void emitADASection();
99 void emitIDRLSection(Module &M);
100
101public:
102 SystemZAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
103 : AsmPrinter(TM, std::move(Streamer), ID), CurrentFnPPA1Sym(nullptr),
104 CurrentFnEPMarkerSym(nullptr), PPA2Sym(nullptr),
105 ADATable(TM.getPointerSize(AS: 0)) {}
106
107 // Override AsmPrinter.
108 StringRef getPassName() const override { return "SystemZ Assembly Printer"; }
109 void emitInstruction(const MachineInstr *MI) override;
110 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
111 void emitEndOfAsmFile(Module &M) override;
112 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
113 const char *ExtraCode, raw_ostream &OS) override;
114 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
115 const char *ExtraCode, raw_ostream &OS) override;
116
117 bool runOnMachineFunction(MachineFunction &MF) override {
118 AsmPrinter::runOnMachineFunction(MF);
119
120 // Emit the XRay table for this function.
121 emitXRayTable();
122
123 return false;
124 }
125
126 bool doInitialization(Module &M) override {
127 SM.reset();
128 return AsmPrinter::doInitialization(M);
129 }
130 void emitFunctionEntryLabel() override;
131 void emitFunctionBodyEnd() override;
132 void emitStartOfAsmFile(Module &M) override;
133
134private:
135 void emitCallInformation(CallType CT);
136 void LowerFENTRY_CALL(const MachineInstr &MI, SystemZMCInstLower &MCIL);
137 void LowerSTACKMAP(const MachineInstr &MI);
138 void LowerPATCHPOINT(const MachineInstr &MI, SystemZMCInstLower &Lower);
139 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
140 SystemZMCInstLower &Lower);
141 void LowerPATCHABLE_RET(const MachineInstr &MI, SystemZMCInstLower &Lower);
142 void emitAttributes(Module &M);
143};
144} // end namespace llvm
145
146#endif
147