1 //==-- WebAssemblyTargetStreamer.h - WebAssembly Target Streamer -*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file declares WebAssembly-specific target streamer classes.
12 /// These are for implementing support for target-specific assembly directives.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYTARGETSTREAMER_H
17 #define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYTARGETSTREAMER_H
18 
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/Support/MachineValueType.h"
22 
23 namespace llvm {
24 
25 class MCWasmStreamer;
26 class MCSymbolWasm;
27 
28 /// WebAssembly-specific streamer interface, to implement support
29 /// WebAssembly-specific assembly directives.
30 class WebAssemblyTargetStreamer : public MCTargetStreamer {
31 public:
32   explicit WebAssemblyTargetStreamer(MCStreamer &S);
33 
34   /// .local
35   virtual void emitLocal(ArrayRef<wasm::ValType> Types) = 0;
36   /// .endfunc
37   virtual void emitEndFunc() = 0;
38   /// .functype
39   virtual void emitFunctionType(const MCSymbolWasm *Sym) = 0;
40   /// .indidx
41   virtual void emitIndIdx(const MCExpr *Value) = 0;
42   /// .globaltype
43   virtual void emitGlobalType(const MCSymbolWasm *Sym) = 0;
44   /// .eventtype
45   virtual void emitEventType(const MCSymbolWasm *Sym) = 0;
46   /// .import_module
47   virtual void emitImportModule(const MCSymbolWasm *Sym,
48                                 StringRef ModuleName) = 0;
49 
50 protected:
51   void emitValueType(wasm::ValType Type);
52 };
53 
54 /// This part is for ascii assembly output
55 class WebAssemblyTargetAsmStreamer final : public WebAssemblyTargetStreamer {
56   formatted_raw_ostream &OS;
57   void emitSignature(const wasm::WasmSignature *Sig);
58   void emitParamList(const wasm::WasmSignature *Sig);
59   void emitReturnList(const wasm::WasmSignature *Sig);
60 
61 public:
62   WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
63 
64   void emitLocal(ArrayRef<wasm::ValType> Types) override;
65   void emitEndFunc() override;
66   void emitFunctionType(const MCSymbolWasm *Sym) override;
67   void emitIndIdx(const MCExpr *Value) override;
68   void emitGlobalType(const MCSymbolWasm *Sym) override;
69   void emitEventType(const MCSymbolWasm *Sym) override;
70   void emitImportModule(const MCSymbolWasm *Sym, StringRef ModuleName) override;
71 };
72 
73 /// This part is for Wasm object output
74 class WebAssemblyTargetWasmStreamer final : public WebAssemblyTargetStreamer {
75 public:
76   explicit WebAssemblyTargetWasmStreamer(MCStreamer &S);
77 
78   void emitLocal(ArrayRef<wasm::ValType> Types) override;
79   void emitEndFunc() override;
80   void emitFunctionType(const MCSymbolWasm *Sym) override {}
81   void emitIndIdx(const MCExpr *Value) override;
82   void emitGlobalType(const MCSymbolWasm *Sym) override {}
83   void emitEventType(const MCSymbolWasm *Sym) override {}
84   void emitImportModule(const MCSymbolWasm *Sym,
85                         StringRef ModuleName) override {}
86 };
87 
88 /// This part is for null output
89 class WebAssemblyTargetNullStreamer final : public WebAssemblyTargetStreamer {
90 public:
91   explicit WebAssemblyTargetNullStreamer(MCStreamer &S)
92       : WebAssemblyTargetStreamer(S) {}
93 
94   void emitLocal(ArrayRef<wasm::ValType>) override {}
95   void emitEndFunc() override {}
96   void emitFunctionType(const MCSymbolWasm *) override {}
97   void emitIndIdx(const MCExpr *) override {}
98   void emitGlobalType(const MCSymbolWasm *) override {}
99   void emitEventType(const MCSymbolWasm *) override {}
100   void emitImportModule(const MCSymbolWasm *, StringRef) override {}
101 };
102 
103 } // end namespace llvm
104 
105 #endif
106