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