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