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   bool signaturesEmitted = false;
29 
30   StringRef storeName(StringRef Name) {
31     std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
32     Names.push_back(std::move(N));
33     return *Names.back();
34   }
35 
36 public:
37   explicit WebAssemblyAsmPrinter(TargetMachine &TM,
38                                  std::unique_ptr<MCStreamer> Streamer)
39       : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), MRI(nullptr),
40         MFI(nullptr) {}
41 
42   StringRef getPassName() const override {
43     return "WebAssembly Assembly Printer";
44   }
45 
46   const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; }
47   void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
48     Signatures.push_back(std::move(Sig));
49   }
50 
51   //===------------------------------------------------------------------===//
52   // MachineFunctionPass Implementation.
53   //===------------------------------------------------------------------===//
54 
55   bool runOnMachineFunction(MachineFunction &MF) override {
56     Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
57     MRI = &MF.getRegInfo();
58     MFI = MF.getInfo<WebAssemblyFunctionInfo>();
59     return AsmPrinter::runOnMachineFunction(MF);
60   }
61 
62   //===------------------------------------------------------------------===//
63   // AsmPrinter Implementation.
64   //===------------------------------------------------------------------===//
65 
66   void emitEndOfAsmFile(Module &M) override;
67   void EmitProducerInfo(Module &M);
68   void EmitTargetFeatures(Module &M);
69   void emitSymbolType(const MCSymbolWasm *Sym);
70   void emitGlobalVariable(const GlobalVariable *GV) override;
71   void emitJumpTableInfo() override;
72   void emitConstantPool() override;
73   void emitFunctionBodyStart() override;
74   void emitInstruction(const MachineInstr *MI) override;
75   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
76                        const char *ExtraCode, raw_ostream &OS) override;
77   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
78                              const char *ExtraCode, raw_ostream &OS) override;
79 
80   MVT getRegType(unsigned RegNo) const;
81   std::string regToString(const MachineOperand &MO);
82   WebAssemblyTargetStreamer *getTargetStreamer();
83   MCSymbolWasm *getMCSymbolForFunction(const Function *F, bool EnableEmEH,
84                                        wasm::WasmSignature *Sig,
85                                        bool &InvokeDetected);
86   MCSymbol *getOrCreateWasmSymbol(StringRef Name);
87   void emitDecls(const Module &M);
88 };
89 
90 } // end namespace llvm
91 
92 #endif
93