xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric // WebAssemblyAsmPrinter.h - WebAssembly implementation of AsmPrinter-*- C++ -*-
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
100b57cec5SDimitry Andric #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYASMPRINTER_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
130b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
160b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric namespace llvm {
190b57cec5SDimitry Andric class WebAssemblyTargetStreamer;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter {
220b57cec5SDimitry Andric   const WebAssemblySubtarget *Subtarget;
230b57cec5SDimitry Andric   const MachineRegisterInfo *MRI;
240b57cec5SDimitry Andric   WebAssemblyFunctionInfo *MFI;
250b57cec5SDimitry Andric   // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter?
260b57cec5SDimitry Andric   std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
27*5ffd83dbSDimitry Andric   std::vector<std::unique_ptr<std::string>> Names;
28*5ffd83dbSDimitry Andric 
29*5ffd83dbSDimitry Andric   StringRef storeName(StringRef Name) {
30*5ffd83dbSDimitry Andric     std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
31*5ffd83dbSDimitry Andric     Names.push_back(std::move(N));
32*5ffd83dbSDimitry Andric     return *Names.back();
33*5ffd83dbSDimitry Andric   }
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric public:
360b57cec5SDimitry Andric   explicit WebAssemblyAsmPrinter(TargetMachine &TM,
370b57cec5SDimitry Andric                                  std::unique_ptr<MCStreamer> Streamer)
380b57cec5SDimitry Andric       : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), MRI(nullptr),
390b57cec5SDimitry Andric         MFI(nullptr) {}
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   StringRef getPassName() const override {
420b57cec5SDimitry Andric     return "WebAssembly Assembly Printer";
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; }
460b57cec5SDimitry Andric   void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
470b57cec5SDimitry Andric     Signatures.push_back(std::move(Sig));
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   //===------------------------------------------------------------------===//
510b57cec5SDimitry Andric   // MachineFunctionPass Implementation.
520b57cec5SDimitry Andric   //===------------------------------------------------------------------===//
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
550b57cec5SDimitry Andric     Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
560b57cec5SDimitry Andric     MRI = &MF.getRegInfo();
570b57cec5SDimitry Andric     MFI = MF.getInfo<WebAssemblyFunctionInfo>();
580b57cec5SDimitry Andric     return AsmPrinter::runOnMachineFunction(MF);
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   //===------------------------------------------------------------------===//
620b57cec5SDimitry Andric   // AsmPrinter Implementation.
630b57cec5SDimitry Andric   //===------------------------------------------------------------------===//
640b57cec5SDimitry Andric 
65*5ffd83dbSDimitry Andric   void emitEndOfAsmFile(Module &M) override;
660b57cec5SDimitry Andric   void EmitProducerInfo(Module &M);
670b57cec5SDimitry Andric   void EmitTargetFeatures(Module &M);
68*5ffd83dbSDimitry Andric   void emitJumpTableInfo() override;
69*5ffd83dbSDimitry Andric   void emitConstantPool() override;
70*5ffd83dbSDimitry Andric   void emitFunctionBodyStart() override;
71*5ffd83dbSDimitry Andric   void emitInstruction(const MachineInstr *MI) override;
720b57cec5SDimitry Andric   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
730b57cec5SDimitry Andric                        const char *ExtraCode, raw_ostream &OS) override;
740b57cec5SDimitry Andric   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
750b57cec5SDimitry Andric                              const char *ExtraCode, raw_ostream &OS) override;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   MVT getRegType(unsigned RegNo) const;
780b57cec5SDimitry Andric   std::string regToString(const MachineOperand &MO);
790b57cec5SDimitry Andric   WebAssemblyTargetStreamer *getTargetStreamer();
800b57cec5SDimitry Andric };
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric } // end namespace llvm
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric #endif
85