1// WebAssemblyAsmPrinter.h - WebAssembly implementation of AsmPrinter-*- 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_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
10#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
11
12#include "WebAssemblyMachineFunctionInfo.h"
13#include "WebAssemblySubtarget.h"
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
16#include "llvm/IR/Analysis.h"
17#include "llvm/IR/PassManager.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/Target/TargetMachine.h"
20
21namespace llvm {
22class WebAssemblyTargetStreamer;
23
24class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter {
25public:
26 static char ID;
27
28private:
29 const WebAssemblySubtarget *Subtarget;
30 const MachineRegisterInfo *MRI;
31 WebAssemblyFunctionInfo *MFI;
32 bool signaturesEmitted = false;
33
34public:
35 explicit WebAssemblyAsmPrinter(TargetMachine &TM,
36 std::unique_ptr<MCStreamer> Streamer)
37 : AsmPrinter(TM, std::move(Streamer), ID), Subtarget(nullptr),
38 MRI(nullptr), MFI(nullptr) {}
39
40 StringRef getPassName() const override {
41 return "WebAssembly Assembly Printer";
42 }
43
44 const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; }
45
46 //===------------------------------------------------------------------===//
47 // MachineFunctionPass Implementation.
48 //===------------------------------------------------------------------===//
49
50 bool runOnMachineFunction(MachineFunction &MF) override {
51 Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
52 MRI = &MF.getRegInfo();
53 MFI = MF.getInfo<WebAssemblyFunctionInfo>();
54 return AsmPrinter::runOnMachineFunction(MF);
55 }
56
57 //===------------------------------------------------------------------===//
58 // AsmPrinter Implementation.
59 //===------------------------------------------------------------------===//
60
61 void emitEndOfAsmFile(Module &M) override;
62 void EmitProducerInfo(Module &M);
63 void EmitTargetFeatures(Module &M);
64 void EmitFunctionAttributes(Module &M);
65 void emitSymbolType(const MCSymbolWasm *Sym);
66 void emitGlobalVariable(const GlobalVariable *GV) override;
67 void emitJumpTableInfo() override;
68 void emitConstantPool() override;
69 void emitFunctionBodyStart() override;
70 void emitInstruction(const MachineInstr *MI) override;
71 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 const char *ExtraCode, raw_ostream &OS) override;
73 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
74 const char *ExtraCode, raw_ostream &OS) override;
75
76 MVT getRegType(unsigned RegNo) const;
77 std::string regToString(const MachineOperand &MO);
78 WebAssemblyTargetStreamer *getTargetStreamer();
79 MCSymbolWasm *getMCSymbolForFunction(const Function *F,
80 wasm::WasmSignature *Sig,
81 bool &InvokeDetected);
82 MCSymbol *getOrCreateWasmSymbol(StringRef Name);
83 void emitDecls(const Module &M);
84};
85
86class WebAssemblyAsmPrinterBeginPass
87 : public RequiredPassInfoMixin<WebAssemblyAsmPrinterBeginPass> {
88public:
89 PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
90};
91
92class WebAssemblyAsmPrinterPass
93 : public RequiredPassInfoMixin<WebAssemblyAsmPrinterPass> {
94public:
95 PreservedAnalyses run(MachineFunction &MF,
96 MachineFunctionAnalysisManager &MFAM);
97};
98
99class WebAssemblyAsmPrinterEndPass
100 : public RequiredPassInfoMixin<WebAssemblyAsmPrinterEndPass> {
101public:
102 PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
103};
104
105} // end namespace llvm
106
107#endif
108