Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3/*
4 * definitions from https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
5 */
6
7#include <sysio/vm/allocator.hpp>
8#include <sysio/vm/guarded_ptr.hpp>
9#include <sysio/vm/opcodes.hpp>
10#include <sysio/vm/vector.hpp>
11
12#include <algorithm>
13#include <cstdint>
14#include <cstring>
15#include <limits>
16#include <string_view>
17#include <vector>
18
19namespace sysio { namespace vm {
20 enum types { i32 = 0x7f, i64 = 0x7e, f32 = 0x7d, f64 = 0x7c, anyfunc = 0x70, func = 0x60, pseudo = 0x40, ret_void };
21
22 enum external_kind { Function = 0, Table = 1, Memory = 2, Global = 3 };
23
24 typedef uint8_t value_type;
25 typedef uint8_t block_type;
26 typedef uint8_t elem_type;
27
28 template <typename T>
29 using guarded_vector = managed_vector<T, growable_allocator>;
30
35
37 bool flags;
39 uint32_t maximum = 0;
40 };
41
42 struct func_type {
43 value_type form; // value for the func type constructor
47 };
48
49 inline bool operator==(const func_type& lhs, const func_type& rhs) {
50 return lhs.form == rhs.form &&
51 lhs.param_types.size() == rhs.param_types.size() &&
52 std::equal(lhs.param_types.raw(), lhs.param_types.raw() + lhs.param_types.size(), rhs.param_types.raw()) &&
53 lhs.return_count == rhs.return_count &&
54 (lhs.return_count || lhs.return_type == rhs.return_type);
55 }
56
57 union expr_value {
62 };
63
64 struct init_expr {
67 };
68
69 struct global_type {
71 bool mutability;
72 };
73
79
85
89
97
104
110
116
117 struct local_entry {
120 };
121
123 native_value() = default;
124 constexpr native_value(uint32_t arg) : i32(arg) {}
125 constexpr native_value(uint64_t arg) : i64(arg) {}
126 constexpr native_value(float arg) : f32(arg) {}
127 constexpr native_value(double arg) : f64(arg) {}
130 float f32;
131 double f64;
132 };
133
137 opcode* code;
138 std::size_t jit_code_offset;
139 };
140
146
147 using wasm_code = std::vector<uint8_t>;
149 typedef std::uint32_t wasm_ptr_t;
150 typedef std::uint32_t wasm_size_t;
151
152 struct name_assoc {
153 std::uint32_t idx;
155 };
157 std::uint32_t idx;
159 };
165
166 struct module {
167 growable_allocator allocator = { constants::initial_module_size };
168 uint32_t start = std::numeric_limits<uint32_t>::max();
179
180 // Custom sections:
181 name_section* names = nullptr;
182
183 // not part of the spec for WASM
188 // If non-null, indicates that the parser encountered an error
189 // that would prevent successful instantiation. Must refer
190 // to memory with static storage duration.
191 const char * error = nullptr;
192
193 void finalize() {
195 allocator.finalize();
196 }
198 uint32_t number_of_imports = 0;
199 for (uint32_t i = 0; i < imports.size(); i++) {
200 if (imports[i].kind == external_kind::Function)
201 number_of_imports++;
202 }
203 return number_of_imports;
204 }
205 inline uint32_t get_functions_size() const { return functions.size(); }
207 inline opcode* get_function_pc( uint32_t fidx ) const {
208 SYS_VM_ASSERT( fidx >= get_imported_functions_size(), wasm_interpreter_exception, "trying to get the PC of an imported function" );
209 return code[fidx-get_imported_functions_size()].code;
210 }
211
212 inline auto& get_opcode(uint32_t pc) const {
213 return ((opcode*)&code[0].code[0])[pc];
214 }
215
217 SYS_VM_ASSERT(index >= get_imported_functions_size(), wasm_interpreter_exception, "imported functions do not have locals");
218 return code[index - get_imported_functions_size()].locals.size();
219 }
220
221 auto& get_function_type(uint32_t index) const {
222 if (index < get_imported_functions_size())
223 return types[imports[index].type.func_t];
225 }
226
227 uint32_t get_exported_function(const std::string_view str) {
228 uint32_t index = std::numeric_limits<uint32_t>::max();
229 for (uint32_t i = 0; i < exports.size(); i++) {
230 if (exports[i].kind == external_kind::Function && exports[i].field_str.size() == str.size() &&
231 memcmp((const char*)str.data(), (const char*)exports[i].field_str.raw(), exports[i].field_str.size()) ==
232 0) {
233 index = exports[i].index;
234 break;
235 }
236 }
237 return index;
238 }
239
241 type_aliases.resize(types.size());
242 for (uint32_t i = 0; i < types.size(); ++i) {
243 uint32_t j = 0;
244 for (; j < i; ++j) {
245 if (types[j] == types[i]) {
246 break;
247 }
248 }
249 type_aliases[i] = j;
250 }
251
252 uint32_t imported_functions_size = get_imported_functions_size();
253 fast_functions.resize(functions.size() + imported_functions_size);
254 for (uint32_t i = 0; i < imported_functions_size; ++i) {
255 fast_functions[i] = type_aliases[imports[i].type.func_t];
256 }
257 for (uint32_t i = 0; i < functions.size(); ++i) {
258 fast_functions[i + imported_functions_size] = type_aliases[functions[i]];
259 }
260 }
261 };
262}} // namespace sysio::vm
bip::allocator< T, pinnable_mapped_file::segment_manager > allocator
Definition chainbase.hpp:56
uint8_t value_type
Definition types.hpp:24
uint8_t block_type
Definition types.hpp:25
managed_vector< T, growable_allocator > guarded_vector
Definition types.hpp:29
external_kind
Definition types.hpp:22
@ Function
Definition types.hpp:22
std::vector< uint8_t > wasm_code
Definition types.hpp:147
bool operator==(const host_function &lhs, const func_type &rhs)
std::uint32_t wasm_ptr_t
Definition types.hpp:149
uint8_t elem_type
Definition types.hpp:26
std::uint32_t wasm_size_t
Definition types.hpp:150
@ ret_void
Definition types.hpp:20
@ anyfunc
Definition types.hpp:20
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
signed char int8_t
Definition stdint.h:121
guarded_vector< uint8_t > data
Definition types.hpp:144
guarded_vector< uint32_t > elems
Definition types.hpp:114
Definition types.hpp:105
external_kind kind
Definition types.hpp:107
guarded_vector< uint8_t > field_str
Definition types.hpp:106
uint32_t index
Definition types.hpp:108
uint8_t return_count
Definition types.hpp:45
value_type form
Definition types.hpp:43
guarded_vector< value_type > param_types
Definition types.hpp:44
value_type return_type
Definition types.hpp:46
guarded_vector< local_entry > locals
Definition types.hpp:136
std::size_t jit_code_offset
Definition types.hpp:138
value_type content_type
Definition types.hpp:70
Definition types.hpp:98
guarded_vector< uint8_t > module_str
Definition types.hpp:99
import_type type
Definition types.hpp:102
guarded_vector< uint8_t > field_str
Definition types.hpp:100
external_kind kind
Definition types.hpp:101
guarded_vector< name_assoc > namemap
Definition types.hpp:158
expr_value value
Definition types.hpp:66
Definition types.hpp:117
uint32_t count
Definition types.hpp:118
value_type type
Definition types.hpp:119
resizable_limits limits
Definition types.hpp:87
uint32_t get_exported_function(const std::string_view str)
Definition types.hpp:227
guarded_vector< uint32_t > functions
Definition types.hpp:171
guarded_vector< uint32_t > type_aliases
Definition types.hpp:185
uint32_t start
Definition types.hpp:168
guarded_vector< import_entry > imports
Definition types.hpp:170
guarded_vector< uint32_t > fast_functions
Definition types.hpp:186
guarded_vector< global_variable > globals
Definition types.hpp:174
uint32_t get_function_locals_size(uint32_t index) const
Definition types.hpp:216
guarded_vector< elem_segment > elements
Definition types.hpp:176
uint32_t get_imported_functions_size() const
Definition types.hpp:197
guarded_vector< uint32_t > import_functions
Definition types.hpp:184
guarded_vector< export_entry > exports
Definition types.hpp:175
guarded_vector< table_type > tables
Definition types.hpp:172
uint32_t get_functions_size() const
Definition types.hpp:205
guarded_vector< memory_type > memories
Definition types.hpp:173
auto & get_opcode(uint32_t pc) const
Definition types.hpp:212
name_section * names
Definition types.hpp:181
auto & get_function_type(uint32_t index) const
Definition types.hpp:221
opcode * get_function_pc(uint32_t fidx) const
Definition types.hpp:207
void normalize_types()
Definition types.hpp:240
growable_allocator allocator
Definition types.hpp:167
uint32_t get_functions_total() const
Definition types.hpp:206
uint64_t maximum_stack
Definition types.hpp:187
guarded_vector< uint8_t > name
Definition types.hpp:154
std::uint32_t idx
Definition types.hpp:153
guarded_vector< indirect_name_assoc > * local_names
Definition types.hpp:163
guarded_vector< name_assoc > * function_names
Definition types.hpp:162
guarded_vector< uint8_t > * module_name
Definition types.hpp:161
resizable_limits limits
Definition types.hpp:82
guarded_vector< uint32_t > table
Definition types.hpp:83
elem_type element_type
Definition types.hpp:81
#define SYS_VM_ASSERT(expr, exc_type, msg)
Definition exceptions.hpp:8
global_type global_t
Definition types.hpp:95
table_type table_t
Definition types.hpp:93
memory_type mem_t
Definition types.hpp:94
constexpr native_value(float arg)
Definition types.hpp:126
constexpr native_value(uint32_t arg)
Definition types.hpp:124
constexpr native_value(double arg)
Definition types.hpp:127
constexpr native_value(uint64_t arg)
Definition types.hpp:125
uint16_t j