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
18namespace llvm {
19class WebAssemblyTargetStreamer;
20
21class 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
36public:
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 EmitFunctionAttributes(Module &M);
70  void emitSymbolType(const MCSymbolWasm *Sym);
71  void emitGlobalVariable(const GlobalVariable *GV) override;
72  void emitJumpTableInfo() override;
73  void emitConstantPool() override;
74  void emitFunctionBodyStart() override;
75  void emitInstruction(const MachineInstr *MI) override;
76  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
77                       const char *ExtraCode, raw_ostream &OS) override;
78  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
79                             const char *ExtraCode, raw_ostream &OS) override;
80
81  MVT getRegType(unsigned RegNo) const;
82  std::string regToString(const MachineOperand &MO);
83  WebAssemblyTargetStreamer *getTargetStreamer();
84  MCSymbolWasm *getMCSymbolForFunction(const Function *F, bool EnableEmEH,
85                                       wasm::WasmSignature *Sig,
86                                       bool &InvokeDetected);
87  MCSymbol *getOrCreateWasmSymbol(StringRef Name);
88  void emitDecls(const Module &M);
89};
90
91} // end namespace llvm
92
93#endif
94