/src/WasmEdge/include/runtime/instance/function.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
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 | | /// Check whether this is a native wasm function. |
74 | 0 | bool isWasmFunction() const noexcept { |
75 | 0 | return std::holds_alternative<WasmFunction>(Data); |
76 | 0 | } |
77 | | |
78 | | /// Check whether this is a compiled function. |
79 | 0 | bool isCompiledFunction() const noexcept { |
80 | 0 | return std::holds_alternative<Symbol<CompiledFunction>>(Data); |
81 | 0 | } |
82 | | |
83 | | /// Check whether this is a host function. |
84 | 0 | bool isHostFunction() const noexcept { |
85 | 0 | return std::holds_alternative<std::unique_ptr<HostFunctionBase>>(Data); |
86 | 0 | } |
87 | | |
88 | | /// Getter for function type. |
89 | 0 | const AST::FunctionType &getFuncType() const noexcept { return FuncType; } |
90 | | |
91 | | /// Getter for 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 for function local number. |
97 | 0 | uint32_t getLocalNum() const noexcept { |
98 | 0 | return std::get_if<WasmFunction>(&Data)->LocalNum; |
99 | 0 | } |
100 | | |
101 | | /// Getter for 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 for symbol. |
111 | 0 | auto &getSymbol() const noexcept { |
112 | 0 | return *std::get_if<Symbol<CompiledFunction>>(&Data); |
113 | 0 | } |
114 | | |
115 | | /// Getter for host function. |
116 | 0 | HostFunctionBase &getHostFunc() const noexcept { |
117 | 0 | return *std::get_if<std::unique_ptr<HostFunctionBase>>(&Data)->get(); |
118 | 0 | } |
119 | | |
120 | | /// Upgrade from WasmFunction to CompiledFunction. |
121 | | /// Must be called under synchronization that |
122 | | /// prevents concurrent access to this function's body. |
123 | 0 | bool unsafeUpgradeToCompiled(Symbol<CompiledFunction> Sym) noexcept { |
124 | 0 | if (!isWasmFunction()) { |
125 | 0 | return false; |
126 | 0 | } |
127 | 0 | Data = std::move(Sym); |
128 | 0 | return true; |
129 | 0 | } |
130 | | |
131 | | private: |
132 | | struct WasmFunction { |
133 | | const std::vector<std::pair<uint32_t, ValType>> Locals; |
134 | | const uint32_t LocalNum; |
135 | | AST::InstrVec Instrs; |
136 | | WasmFunction(Span<const std::pair<uint32_t, ValType>> Locs, |
137 | | AST::InstrView Expr) noexcept |
138 | 0 | : Locals(Locs.begin(), Locs.end()), |
139 | | LocalNum( |
140 | 0 | std::accumulate(Locals.begin(), Locals.end(), UINT32_C(0), |
141 | 0 | [](uint32_t N, const auto &Pair) -> uint32_t { |
142 | 0 | return N + Pair.first; |
143 | 0 | })) { |
144 | | // FIXME: Modify the capacity to prevent connecting 2 vectors. |
145 | 0 | Instrs.reserve(Expr.size() + 1); |
146 | 0 | Instrs.assign(Expr.begin(), Expr.end()); |
147 | 0 | } |
148 | | }; |
149 | | |
150 | | /// \name Data of function instance. |
151 | | /// @{ |
152 | | |
153 | | const AST::FunctionType &FuncType; |
154 | | std::variant<WasmFunction, Symbol<CompiledFunction>, |
155 | | std::unique_ptr<HostFunctionBase>> |
156 | | Data; |
157 | | /// @} |
158 | | }; |
159 | | |
160 | | } // namespace Instance |
161 | | } // namespace Runtime |
162 | | } // namespace WasmEdge |