1//===-- SystemZXPLINKAsmPrinter.h - SystemZ XPLINK asm 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// This file declares the SystemZXPLINKAsmPrinter class, which owns all
10// z/OS XPLINK-specific asm printer behaviour: ADA table management, PPA1/PPA2,
11// IDRL, entry-point markers, and XPLINK call/return instruction lowering.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZXPLINKASMPRINTER_H
16#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZXPLINKASMPRINTER_H
17
18#include "MCTargetDesc/SystemZTargetStreamer.h"
19#include "SystemZAsmPrinter.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22
23namespace llvm {
24class MCStreamer;
25class MCSymbolGOFF;
26class MachineFunction;
27class MachineInstr;
28class MachineOperand;
29class Module;
30class raw_ostream;
31
32class LLVM_LIBRARY_VISIBILITY SystemZXPLINKAsmPrinter
33 : public SystemZAsmPrinter {
34
35 /// Call type information for XPLINK.
36 enum class CallType {
37 BASR76 = 0, // b'x000' == BASR r7,r6
38 BRAS7 = 1, // b'x001' == BRAS r7,ep
39 RESVD_2 = 2, // b'x010'
40 BRASL7 = 3, // b'x011' == BRASL r7,ep
41 RESVD_4 = 4, // b'x100'
42 RESVD_5 = 5, // b'x101'
43 BALR1415 = 6, // b'x110' == BALR r14,r15
44 BASR33 = 7, // b'x111' == BASR r3,r3
45 };
46
47 // The Associated Data Area (ADA) contains descriptors which help locating
48 // external symbols. For each symbol and type, the displacement into the ADA
49 // is stored.
50 class AssociatedDataAreaTable {
51 public:
52 using DisplacementTable =
53 MapVector<std::pair<const MCSymbol *, unsigned>, uint32_t>;
54
55 private:
56 const uint64_t PointerSize;
57
58 /// The mapping of name/slot type pairs to displacements.
59 DisplacementTable Displacements;
60
61 /// The next available displacement value. Incremented when new entries into
62 /// the ADA are created.
63 uint32_t NextDisplacement = 0;
64
65 public:
66 AssociatedDataAreaTable(uint64_t PointerSize) : PointerSize(PointerSize) {}
67
68 /// @brief Add a function descriptor to the ADA.
69 /// @param MF The function containing the ADA_ENTRY instruction.
70 /// @param MO The operand describing the descriptor symbol.
71 /// @return The displacement of the descriptor into the ADA.
72 uint32_t insert(const MachineFunction &MF, const MachineOperand &MO);
73
74 /// @brief Get the displacement into associated data area (ADA) for a name.
75 /// If no displacement is already associated with the name, assign one and
76 /// return it.
77 /// @param Sym The symbol for which the displacement should be returned.
78 /// @param SlotKind The ADA type.
79 /// @return The displacement of the descriptor into the ADA.
80 uint32_t insert(const MCSymbol *Sym, unsigned SlotKind);
81
82 /// Get the table of GOFF displacements. This is 'const' since it should
83 /// never be modified by anything except the APIs on this class.
84 const DisplacementTable &getTable() const { return Displacements; }
85
86 uint32_t getNextDisplacement() const { return NextDisplacement; }
87 };
88
89 AssociatedDataAreaTable ADATable;
90
91 // Record a list of GlobalAlias associated with a GlobalObject.
92 // This is used for z/OS's extra-label-at-definition aliasing strategy.
93 // This is similar to what is done for AIX.
94 DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>>
95 GOAliasMap;
96
97 void calculatePPA1();
98 void emitPPA2(Module &M);
99 void emitADASection();
100 void emitIDRLSection(Module &M);
101 void emitCallInformation(CallType CT);
102
103 SystemZTargetzOSStreamer *getTargetStreamer() {
104 MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
105 assert(TS && "do not have a target streamer");
106 return static_cast<SystemZTargetzOSStreamer *>(TS);
107 }
108
109public:
110 SystemZXPLINKAsmPrinter(TargetMachine &TM,
111 std::unique_ptr<MCStreamer> Streamer);
112
113 // Override AsmPrinter.
114 void emitInstruction(const MachineInstr *MI) override;
115 void emitXXStructorList(const DataLayout &DL, const Constant *List,
116 bool IsCtor) override;
117 void emitEndOfAsmFile(Module &M) override;
118 bool doInitialization(Module &M) override;
119 void emitFunctionEntryLabel() override;
120 void emitFunctionBodyEnd() override;
121 void emitStartOfAsmFile(Module &M) override;
122 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
123 const MCExpr *lowerConstant(const Constant *CV,
124 const Constant *BaseCV = nullptr,
125 uint64_t Offset = 0) override;
126};
127
128} // end namespace llvm
129
130#endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZXPLINKASMPRINTER_H
131