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/MachineFunction.h"
17#include "llvm/CodeGen/StackMaps.h"
18#include "llvm/MC/MCInstBuilder.h"
19#include "llvm/Support/Compiler.h"
20
21namespace llvm {
22class MCStreamer;
23class MachineInstr;
24class Module;
25class raw_ostream;
26
27class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
28public:
29 static char ID;
30
31private:
32 MCSymbol *PPA2Sym;
33
34 SystemZTargetStreamer *getTargetStreamer() {
35 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
36 assert(TS && "do not have a target streamer");
37 return static_cast<SystemZTargetStreamer *>(TS);
38 }
39
40 /// Call type information for XPLINK.
41 enum class CallType {
42 BASR76 = 0, // b'x000' == BASR r7,r6
43 BRAS7 = 1, // b'x001' == BRAS r7,ep
44 RESVD_2 = 2, // b'x010'
45 BRASL7 = 3, // b'x011' == BRASL r7,ep
46 RESVD_4 = 4, // b'x100'
47 RESVD_5 = 5, // b'x101'
48 BALR1415 = 6, // b'x110' == BALR r14,r15
49 BASR33 = 7, // b'x111' == BASR r3,r3
50 };
51
52 // The Associated Data Area (ADA) contains descriptors which help locating
53 // external symbols. For each symbol and type, the displacement into the ADA
54 // is stored.
55 class AssociatedDataAreaTable {
56 public:
57 using DisplacementTable =
58 MapVector<std::pair<const MCSymbol *, unsigned>, uint32_t>;
59
60 private:
61 const uint64_t PointerSize;
62
63 /// The mapping of name/slot type pairs to displacements.
64 DisplacementTable Displacements;
65
66 /// The next available displacement value. Incremented when new entries into
67 /// the ADA are created.
68 uint32_t NextDisplacement = 0;
69
70 public:
71 AssociatedDataAreaTable(uint64_t PointerSize) : PointerSize(PointerSize) {}
72
73 /// @brief Add a function descriptor to the ADA.
74 /// @param MI Pointer to an ADA_ENTRY instruction.
75 /// @return The displacement of the descriptor into the ADA.
76 uint32_t insert(const MachineOperand MO);
77
78 /// @brief Get the displacement into associated data area (ADA) for a name.
79 /// If no displacement is already associated with the name, assign one and
80 /// return it.
81 /// @param Sym The symbol for which the displacement should be returned.
82 /// @param SlotKind The ADA type.
83 /// @return The displacement of the descriptor into the ADA.
84 uint32_t insert(const MCSymbol *Sym, unsigned SlotKind);
85
86 /// Get the table of GOFF displacements. This is 'const' since it should
87 /// never be modified by anything except the APIs on this class.
88 const DisplacementTable &getTable() const { return Displacements; }
89
90 uint32_t getNextDisplacement() const { return NextDisplacement; }
91 };
92
93 AssociatedDataAreaTable ADATable;
94
95 // Record a list of GlobalAlias associated with a GlobalObject.
96 // This is used for z/OS's extra-label-at-definition aliasing strategy.
97 // This is similar to what is done for AIX.
98 DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>>
99 GOAliasMap;
100
101 struct PPA1Info {
102 StringRef Name;
103 MCSymbol *FnEnd = nullptr; // Symbol marking function end.
104 MCSymbol *PPA1 = nullptr; // Symbol marking PPA1 begin.
105 MCSymbol *EPMarker = nullptr; // Symbol marking entry point.
106 MCSymbol *PersonalityRoutine = nullptr;
107 MCSymbol *GCCEH = nullptr;
108 int64_t OffsetFPR = 0;
109 int64_t OffsetVR = 0;
110 uint64_t CallFrameSize = 0;
111 unsigned SizeOfFnParams = 0;
112 uint32_t FrameAndFPROffset;
113 uint32_t FrameAndVROffset;
114 uint16_t SavedGPRMask = 0;
115 uint16_t SavedFPRMask = 0;
116 uint8_t SavedVRMask = 0;
117 uint8_t FrameReg = 0;
118 uint8_t AllocaReg = 0;
119 bool IsVarArg = false;
120 bool HasStackProtector = false;
121 };
122 SmallVector<PPA1Info, 0> DeferredPPA1;
123
124 void calculatePPA1();
125 void emitPPA1(PPA1Info &Info);
126 void emitPPA2(Module &M);
127 void emitADASection();
128 void emitIDRLSection(Module &M);
129
130public:
131 SystemZAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
132 : AsmPrinter(TM, std::move(Streamer), ID), PPA2Sym(nullptr),
133 ADATable(TM.getPointerSize(AS: 0)) {}
134
135 // Override AsmPrinter.
136 StringRef getPassName() const override { return "SystemZ Assembly Printer"; }
137 void emitInstruction(const MachineInstr *MI) override;
138 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
139 void emitXXStructorList(const DataLayout &DL, const Constant *List,
140 bool IsCtor) override;
141 void emitEndOfAsmFile(Module &M) override;
142 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
143 const char *ExtraCode, raw_ostream &OS) override;
144 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
145 const char *ExtraCode, raw_ostream &OS) override;
146
147 bool runOnMachineFunction(MachineFunction &MF) override {
148 AsmPrinter::runOnMachineFunction(MF);
149
150 // Emit the XRay table for this function.
151 emitXRayTable();
152
153 return false;
154 }
155
156 bool doInitialization(Module &M) override;
157 void emitFunctionEntryLabel() override;
158 void emitFunctionBodyEnd() override;
159 void emitStartOfAsmFile(Module &M) override;
160 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
161 const MCExpr *lowerConstant(const Constant *CV,
162 const Constant *BaseCV = nullptr,
163 uint64_t Offset = 0) override;
164
165private:
166 void emitCallInformation(CallType CT);
167 void LowerFENTRY_CALL(const MachineInstr &MI, SystemZMCInstLower &MCIL);
168 void LowerSTACKMAP(const MachineInstr &MI);
169 void LowerPATCHPOINT(const MachineInstr &MI, SystemZMCInstLower &Lower);
170 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
171 SystemZMCInstLower &Lower);
172 void LowerPATCHABLE_RET(const MachineInstr &MI, SystemZMCInstLower &Lower);
173 void lowerLOAD_TLS_BLOCK_ADDR(const MachineInstr &MI,
174 SystemZMCInstLower &Lower);
175 void lowerLOAD_GLOBAL_STACKGUARD_ADDR(const MachineInstr &MI,
176 SystemZMCInstLower &Lower);
177 void emitAttributes(Module &M);
178};
179} // end namespace llvm
180
181#endif
182