1 //==-- WebAssemblyTargetStreamer.h - WebAssembly Target Streamer -*- 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 /// \file
10 /// This file declares WebAssembly-specific target streamer classes.
11 /// These are for implementing support for target-specific assembly directives.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYTARGETSTREAMER_H
16 #define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYTARGETSTREAMER_H
17 
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Support/MachineValueType.h"
21 
22 namespace llvm {
23 
24 class MCSymbolWasm;
25 
26 /// WebAssembly-specific streamer interface, to implement support
27 /// WebAssembly-specific assembly directives.
28 class WebAssemblyTargetStreamer : public MCTargetStreamer {
29 public:
30   explicit WebAssemblyTargetStreamer(MCStreamer &S);
31 
32   /// .local
33   virtual void emitLocal(ArrayRef<wasm::ValType> Types) = 0;
34   /// .endfunc
35   virtual void emitEndFunc() = 0;
36   /// .functype
37   virtual void emitFunctionType(const MCSymbolWasm *Sym) = 0;
38   /// .indidx
39   virtual void emitIndIdx(const MCExpr *Value) = 0;
40   /// .globaltype
41   virtual void emitGlobalType(const MCSymbolWasm *Sym) = 0;
42   /// .eventtype
43   virtual void emitEventType(const MCSymbolWasm *Sym) = 0;
44   /// .import_module
45   virtual void emitImportModule(const MCSymbolWasm *Sym,
46                                 StringRef ImportModule) = 0;
47   /// .import_name
48   virtual void emitImportName(const MCSymbolWasm *Sym,
49                               StringRef ImportName) = 0;
50   /// .export_name
51   virtual void emitExportName(const MCSymbolWasm *Sym,
52                               StringRef ExportName) = 0;
53 
54 protected:
55   void emitValueType(wasm::ValType Type);
56 };
57 
58 /// This part is for ascii assembly output
59 class WebAssemblyTargetAsmStreamer final : public WebAssemblyTargetStreamer {
60   formatted_raw_ostream &OS;
61 
62 public:
63   WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
64 
65   void emitLocal(ArrayRef<wasm::ValType> Types) override;
66   void emitEndFunc() override;
67   void emitFunctionType(const MCSymbolWasm *Sym) override;
68   void emitIndIdx(const MCExpr *Value) override;
69   void emitGlobalType(const MCSymbolWasm *Sym) override;
70   void emitEventType(const MCSymbolWasm *Sym) override;
71   void emitImportModule(const MCSymbolWasm *Sym, StringRef ImportModule) override;
72   void emitImportName(const MCSymbolWasm *Sym, StringRef ImportName) override;
73   void emitExportName(const MCSymbolWasm *Sym, StringRef ExportName) override;
74 };
75 
76 /// This part is for Wasm object output
77 class WebAssemblyTargetWasmStreamer final : public WebAssemblyTargetStreamer {
78 public:
79   explicit WebAssemblyTargetWasmStreamer(MCStreamer &S);
80 
81   void emitLocal(ArrayRef<wasm::ValType> Types) override;
82   void emitEndFunc() override;
83   void emitFunctionType(const MCSymbolWasm *Sym) override {}
84   void emitIndIdx(const MCExpr *Value) override;
85   void emitGlobalType(const MCSymbolWasm *Sym) override {}
86   void emitEventType(const MCSymbolWasm *Sym) override {}
87   void emitImportModule(const MCSymbolWasm *Sym,
88                         StringRef ImportModule) override {}
89   void emitImportName(const MCSymbolWasm *Sym,
90                       StringRef ImportName) override {}
91   void emitExportName(const MCSymbolWasm *Sym,
92                       StringRef ExportName) override {}
93 };
94 
95 /// This part is for null output
96 class WebAssemblyTargetNullStreamer final : public WebAssemblyTargetStreamer {
97 public:
98   explicit WebAssemblyTargetNullStreamer(MCStreamer &S)
99       : WebAssemblyTargetStreamer(S) {}
100 
101   void emitLocal(ArrayRef<wasm::ValType>) override {}
102   void emitEndFunc() override {}
103   void emitFunctionType(const MCSymbolWasm *) override {}
104   void emitIndIdx(const MCExpr *) override {}
105   void emitGlobalType(const MCSymbolWasm *) override {}
106   void emitEventType(const MCSymbolWasm *) override {}
107   void emitImportModule(const MCSymbolWasm *, StringRef) override {}
108   void emitImportName(const MCSymbolWasm *, StringRef) override {}
109   void emitExportName(const MCSymbolWasm *, StringRef) override {}
110 };
111 
112 } // end namespace llvm
113 
114 #endif
115