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