/src/WasmEdge/include/runtime/instance/function.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/runtime/instance/function.h - Function Instance definition ==// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the function instance definition in store manager. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/instruction.h" |
17 | | #include "common/symbol.h" |
18 | | #include "runtime/hostfunc.h" |
19 | | #include "runtime/instance/composite.h" |
20 | | |
21 | | #include <memory> |
22 | | #include <numeric> |
23 | | #include <string> |
24 | | #include <vector> |
25 | | |
26 | | namespace WasmEdge { |
27 | | namespace Runtime { |
28 | | namespace Instance { |
29 | | |
30 | | class ModuleInstance; |
31 | | |
32 | | class FunctionInstance : public CompositeBase { |
33 | | public: |
34 | | using CompiledFunction = void; |
35 | | |
36 | | FunctionInstance() = delete; |
37 | | /// Move constructor. |
38 | | FunctionInstance(FunctionInstance &&Inst) noexcept |
39 | | : CompositeBase(Inst.ModInst, Inst.TypeIdx), FuncType(Inst.FuncType), |
40 | 0 | Data(std::move(Inst.Data)) { |
41 | 0 | assuming(ModInst); |
42 | 0 | } |
43 | | /// Constructor for native function. |
44 | | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
45 | | const AST::FunctionType &Type, |
46 | | Span<const std::pair<uint32_t, ValType>> Locs, |
47 | | AST::InstrView Expr) noexcept |
48 | 0 | : CompositeBase(Mod, TIdx), FuncType(Type), |
49 | 0 | Data(std::in_place_type_t<WasmFunction>(), Locs, Expr) { |
50 | 0 | assuming(ModInst); |
51 | 0 | } |
52 | | /// Constructor for compiled function. |
53 | | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
54 | | const AST::FunctionType &Type, |
55 | | Symbol<CompiledFunction> S) noexcept |
56 | 0 | : CompositeBase(Mod, TIdx), FuncType(Type), |
57 | 0 | Data(std::in_place_type_t<Symbol<CompiledFunction>>(), std::move(S)) { |
58 | 0 | assuming(ModInst); |
59 | 0 | } |
60 | | /// Constructors for host function. |
61 | | FunctionInstance(const ModuleInstance *Mod, const uint32_t TIdx, |
62 | | std::unique_ptr<HostFunctionBase> &&Func) noexcept |
63 | 0 | : CompositeBase(Mod, TIdx), FuncType(Func->getFuncType()), |
64 | 0 | Data(std::in_place_type_t<std::unique_ptr<HostFunctionBase>>(), |
65 | 0 | std::move(Func)) { |
66 | 0 | assuming(ModInst); |
67 | 0 | } |
68 | | FunctionInstance(std::unique_ptr<HostFunctionBase> &&Func) noexcept |
69 | 0 | : CompositeBase(), FuncType(Func->getFuncType()), |
70 | 0 | Data(std::in_place_type_t<std::unique_ptr<HostFunctionBase>>(), |
71 | 0 | std::move(Func)) {} |
72 | | |
73 | | /// Getter of checking is native wasm function. |
74 | 0 | bool isWasmFunction() const noexcept { |
75 | 0 | return std::holds_alternative<WasmFunction>(Data); |
76 | 0 | } |
77 | | |
78 | | /// Getter of checking is compiled function. |
79 | 0 | bool isCompiledFunction() const noexcept { |
80 | 0 | return std::holds_alternative<Symbol<CompiledFunction>>(Data); |
81 | 0 | } |
82 | | |
83 | | /// Getter of checking is host function. |
84 | 0 | bool isHostFunction() const noexcept { |
85 | 0 | return std::holds_alternative<std::unique_ptr<HostFunctionBase>>(Data); |
86 | 0 | } |
87 | | |
88 | | /// Getter of function type. |
89 | 0 | const AST::FunctionType &getFuncType() const noexcept { return FuncType; } |
90 | | |
91 | | /// Getter of function local variables. |
92 | 0 | Span<const std::pair<uint32_t, ValType>> getLocals() const noexcept { |
93 | 0 | return std::get_if<WasmFunction>(&Data)->Locals; |
94 | 0 | } |
95 | | |
96 | | /// Getter of function local number. |
97 | 0 | uint32_t getLocalNum() const noexcept { |
98 | 0 | return std::get_if<WasmFunction>(&Data)->LocalNum; |
99 | 0 | } |
100 | | |
101 | | /// Getter of function body instrs. |
102 | 0 | AST::InstrView getInstrs() const noexcept { |
103 | 0 | if (std::holds_alternative<WasmFunction>(Data)) { |
104 | 0 | return std::get<WasmFunction>(Data).Instrs; |
105 | 0 | } else { |
106 | 0 | return {}; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | /// Getter of symbol |
111 | 0 | auto &getSymbol() const noexcept { |
112 | 0 | return *std::get_if<Symbol<CompiledFunction>>(&Data); |
113 | 0 | } |
114 | | |
115 | | /// Getter of host function. |
116 | 0 | HostFunctionBase &getHostFunc() const noexcept { |
117 | 0 | return *std::get_if<std::unique_ptr<HostFunctionBase>>(&Data)->get(); |
118 | 0 | } |
119 | | |
120 | | private: |
121 | | struct WasmFunction { |
122 | | const std::vector<std::pair<uint32_t, ValType>> Locals; |
123 | | const uint32_t LocalNum; |
124 | | AST::InstrVec Instrs; |
125 | | WasmFunction(Span<const std::pair<uint32_t, ValType>> Locs, |
126 | | AST::InstrView Expr) noexcept |
127 | 0 | : Locals(Locs.begin(), Locs.end()), |
128 | | LocalNum( |
129 | 0 | std::accumulate(Locals.begin(), Locals.end(), UINT32_C(0), |
130 | 0 | [](uint32_t N, const auto &Pair) -> uint32_t { |
131 | 0 | return N + Pair.first; |
132 | 0 | })) { |
133 | | // FIXME: Modify the capacity to prevent from connection of 2 vectors. |
134 | 0 | Instrs.reserve(Expr.size() + 1); |
135 | 0 | Instrs.assign(Expr.begin(), Expr.end()); |
136 | 0 | } |
137 | | }; |
138 | | |
139 | | /// \name Data of function instance. |
140 | | /// @{ |
141 | | |
142 | | const AST::FunctionType &FuncType; |
143 | | std::variant<WasmFunction, Symbol<CompiledFunction>, |
144 | | std::unique_ptr<HostFunctionBase>> |
145 | | Data; |
146 | | /// @} |
147 | | }; |
148 | | |
149 | | } // namespace Instance |
150 | | } // namespace Runtime |
151 | | } // namespace WasmEdge |