xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
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/MC/MCStreamer.h"
16 #include "llvm/Target/TargetMachine.h"
17 
18 namespace llvm {
19 class WebAssemblyTargetStreamer;
20 
21 class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter {
22   const WebAssemblySubtarget *Subtarget;
23   const MachineRegisterInfo *MRI;
24   WebAssemblyFunctionInfo *MFI;
25   // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter?
26   std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
27   std::vector<std::unique_ptr<std::string>> Names;
28 
29   StringRef storeName(StringRef Name) {
30     std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
31     Names.push_back(std::move(N));
32     return *Names.back();
33   }
34 
35 public:
36   explicit WebAssemblyAsmPrinter(TargetMachine &TM,
37                                  std::unique_ptr<MCStreamer> Streamer)
38       : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), MRI(nullptr),
39         MFI(nullptr) {}
40 
41   StringRef getPassName() const override {
42     return "WebAssembly Assembly Printer";
43   }
44 
45   const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; }
46   void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
47     Signatures.push_back(std::move(Sig));
48   }
49 
50   //===------------------------------------------------------------------===//
51   // MachineFunctionPass Implementation.
52   //===------------------------------------------------------------------===//
53 
54   bool runOnMachineFunction(MachineFunction &MF) override {
55     Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
56     MRI = &MF.getRegInfo();
57     MFI = MF.getInfo<WebAssemblyFunctionInfo>();
58     return AsmPrinter::runOnMachineFunction(MF);
59   }
60 
61   //===------------------------------------------------------------------===//
62   // AsmPrinter Implementation.
63   //===------------------------------------------------------------------===//
64 
65   void emitEndOfAsmFile(Module &M) override;
66   void EmitProducerInfo(Module &M);
67   void EmitTargetFeatures(Module &M);
68   void emitJumpTableInfo() override;
69   void emitConstantPool() override;
70   void emitFunctionBodyStart() override;
71   void emitInstruction(const MachineInstr *MI) override;
72   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73                        const char *ExtraCode, raw_ostream &OS) override;
74   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75                              const char *ExtraCode, raw_ostream &OS) override;
76 
77   MVT getRegType(unsigned RegNo) const;
78   std::string regToString(const MachineOperand &MO);
79   WebAssemblyTargetStreamer *getTargetStreamer();
80   MCSymbolWasm *getMCSymbolForFunction(const Function *F, bool EnableEmEH,
81                                        wasm::WasmSignature *Sig,
82                                        bool &InvokeDetected);
83 };
84 
85 } // end namespace llvm
86 
87 #endif
88