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 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 // Record a list of GlobalAlias associated with a GlobalObject.
94 // This is used for z/OS's extra-label-at-definition aliasing strategy.
95 // This is similar to what is done for AIX.
96 DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>>
97 GOAliasMap;
98
99 void calculatePPA1();
100 void emitPPA2(Module &M);
101 void emitADASection();
102 void emitIDRLSection(Module &M);
103
104public:
105 SystemZAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
106 : AsmPrinter(TM, std::move(Streamer), ID),
107 ADATable(TM.getPointerSize(AS: 0)) {}
108
109 // Override AsmPrinter.
110 StringRef getPassName() const override { return "SystemZ Assembly Printer"; }
111 void emitInstruction(const MachineInstr *MI) override;
112 void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
113 void emitXXStructorList(const DataLayout &DL, const Constant *List,
114 bool IsCtor) override;
115 void emitEndOfAsmFile(Module &M) override;
116 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
117 const char *ExtraCode, raw_ostream &OS) override;
118 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
119 const char *ExtraCode, raw_ostream &OS) override;
120
121 bool runOnMachineFunction(MachineFunction &MF) override {
122 AsmPrinter::runOnMachineFunction(MF);
123
124 // Emit the XRay table for this function.
125 emitXRayTable();
126
127 return false;
128 }
129
130 bool doInitialization(Module &M) override;
131 void emitFunctionEntryLabel() override;
132 void emitFunctionBodyEnd() override;
133 void emitStartOfAsmFile(Module &M) override;
134 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
135 const MCExpr *lowerConstant(const Constant *CV,
136 const Constant *BaseCV = nullptr,
137 uint64_t Offset = 0) override;
138
139private:
140 void emitCallInformation(CallType CT);
141 void LowerFENTRY_CALL(const MachineInstr &MI, SystemZMCInstLower &MCIL);
142 void LowerSTACKMAP(const MachineInstr &MI);
143 void LowerPATCHPOINT(const MachineInstr &MI, SystemZMCInstLower &Lower);
144 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
145 SystemZMCInstLower &Lower);
146 void LowerPATCHABLE_RET(const MachineInstr &MI, SystemZMCInstLower &Lower);
147 void lowerLOAD_TLS_BLOCK_ADDR(const MachineInstr &MI,
148 SystemZMCInstLower &Lower);
149 void lowerLOAD_GLOBAL_STACKGUARD_ADDR(const MachineInstr &MI,
150 SystemZMCInstLower &Lower);
151 void emitAttributes(Module &M);
152};
153} // end namespace llvm
154
155#endif
156