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