diff options
Diffstat (limited to 'wasm2c')
-rw-r--r-- | wasm2c/examples/fac/fac.c | 109 | ||||
-rw-r--r-- | wasm2c/wasm-rt.h | 4 |
2 files changed, 102 insertions, 11 deletions
diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index 303790f2..24d4e177 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -1,6 +1,7 @@ /* Automatically generated by wasm2c */ #include <assert.h> #include <math.h> +#include <stddef.h> #include <string.h> #if defined(_MSC_VER) #include <intrin.h> @@ -456,9 +457,91 @@ static float wasm_sqrtf(float x) { return sqrtf(x); } +static inline void memory_fill(wasm_rt_memory_t* mem, u32 d, u32 val, u32 n) { + RANGE_CHECK(mem, d, n); + memset(mem->data + d, val, n); +} + +static inline void memory_copy(wasm_rt_memory_t* dest, + const wasm_rt_memory_t* src, + u32 dest_addr, + u32 src_addr, + u32 n) { + RANGE_CHECK(dest, dest_addr, n); + RANGE_CHECK(src, src_addr, n); + memmove(dest->data + dest_addr, src->data + src_addr, n); +} + +static inline void memory_init(wasm_rt_memory_t* dest, + const u8* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + LOAD_DATA((*dest), dest_addr, src + src_addr, n); +} + +typedef struct { + uint32_t func_type_index; + wasm_rt_funcref_t func; + size_t module_offset; +} wasm_elem_segment_expr_t; + +static inline void table_init(wasm_rt_table_t* dest, + const wasm_elem_segment_expr_t* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n, + void* module_instance, + const u32* func_types) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + for (u32 i = 0; i < n; i++) { + const wasm_elem_segment_expr_t* src_expr = &src[src_addr + i]; + dest->data[dest_addr + i] = + (wasm_rt_elem_t){func_types[src_expr->func_type_index], src_expr->func, + (char*)module_instance + src_expr->module_offset}; + } +} + +static inline void table_copy(wasm_rt_table_t* dest, + const wasm_rt_table_t* src, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + if (UNLIKELY(src_addr + (uint64_t)n > src->size)) + TRAP(OOB); + + if (n == 0) { + return; + } + + if (dest->data + dest_addr == src->data + src_addr) { + return; + } + + if (dest->data + dest_addr < src->data + src_addr) { + for (u32 i = 0; i < n; i++) { + dest->data[dest_addr + i] = src->data[src_addr + i]; + } + } else { + for (u32 i = n; i > 0; i--) { + dest->data[dest_addr + i - 1] = src->data[src_addr + i - 1]; + } + } +} + static bool s_module_initialized = false; static u32 func_types[1]; + static void init_func_types(void) { func_types[0] = wasm_rt_register_func_type(1, 1, WASM_RT_I32, WASM_RT_I32); } @@ -468,6 +551,21 @@ static void init_tags(void) { static u32 w2c_fac(Z_fac_instance_t*, u32); +static void init_globals(Z_fac_instance_t* instance) { +} + +static void init_memory(Z_fac_instance_t* instance) { +} + +static void init_data_instances(Z_fac_instance_t *instance) { +} + +static void init_table(Z_fac_instance_t* instance) { +} + +static void init_elem_instances(Z_fac_instance_t *instance) { +} + static u32 w2c_fac(Z_fac_instance_t* instance, u32 w2c_p0) { FUNC_PROLOGUE; u32 w2c_i0, w2c_i1, w2c_i2; @@ -488,15 +586,6 @@ static u32 w2c_fac(Z_fac_instance_t* instance, u32 w2c_p0) { return w2c_i0; } -static void init_globals(Z_fac_instance_t* instance) { -} -static void init_memory(Z_fac_instance_t* instance) { -} - -static void init_table(Z_fac_instance_t* instance) { - uint32_t offset; -} - /* export: 'fac' */ u32 Z_facZ_fac(Z_fac_instance_t* instance, u32 w2c_p0) { return w2c_fac(instance, w2c_p0); @@ -515,6 +604,8 @@ void Z_fac_instantiate(Z_fac_instance_t* instance) { init_globals(instance); init_memory(instance); init_table(instance); + init_data_instances(instance); + init_elem_instances(instance); } void Z_fac_free(Z_fac_instance_t* instance) { diff --git a/wasm2c/wasm-rt.h b/wasm2c/wasm-rt.h index 7b8ec1f4..c27905fc 100644 --- a/wasm2c/wasm-rt.h +++ b/wasm2c/wasm-rt.h @@ -109,8 +109,8 @@ extern uint32_t wasm_rt_call_stack_depth; /** Reason a trap occurred. Provide this to `wasm_rt_trap`. */ typedef enum { - WASM_RT_TRAP_NONE, /** No error. */ - WASM_RT_TRAP_OOB, /** Out-of-bounds access in linear memory. */ + WASM_RT_TRAP_NONE, /** No error. */ + WASM_RT_TRAP_OOB, /** Out-of-bounds access in linear memory or a table. */ WASM_RT_TRAP_INT_OVERFLOW, /** Integer overflow on divide or truncation. */ WASM_RT_TRAP_DIV_BY_ZERO, /** Integer divide by zero. */ WASM_RT_TRAP_INVALID_CONVERSION, /** Conversion from NaN to integer. */ |