diff options
Diffstat (limited to 'test')
-rwxr-xr-x | test/run-spec-wasm2c.py | 2 | ||||
-rw-r--r-- | test/spec-wasm2c-prefix.c | 9 | ||||
-rw-r--r-- | test/wasm2c/add.txt | 61 | ||||
-rw-r--r-- | test/wasm2c/hello.txt | 89 | ||||
-rw-r--r-- | test/wasm2c/minimal.txt | 45 |
5 files changed, 131 insertions, 75 deletions
diff --git a/test/run-spec-wasm2c.py b/test/run-spec-wasm2c.py index 0714e249..e9a93228 100755 --- a/test/run-spec-wasm2c.py +++ b/test/run-spec-wasm2c.py @@ -253,7 +253,6 @@ class CWriter(object): def _WriteModuleCommand(self, command): self.module_idx += 1 - self.out_file.write('%s_init_module();\n' % self.GetModulePrefix()) self._WriteModuleInitCall(command, False) def _WriteModuleInstances(self): @@ -269,7 +268,6 @@ class CWriter(object): def _WriteAssertUninstantiableCommand(self, command): self.module_idx += 1 - self.out_file.write('%s_init_module();\n' % self.GetModulePrefix()) self._WriteModuleInitCall(command, True) def _WriteActionCommand(self, command): diff --git a/test/spec-wasm2c-prefix.c b/test/spec-wasm2c-prefix.c index b9ba0934..c3c6e500 100644 --- a/test/spec-wasm2c-prefix.c +++ b/test/spec-wasm2c-prefix.c @@ -194,10 +194,15 @@ static bool is_equal_wasm_rt_externref_t(wasm_rt_externref_t x, return x == y; } +static inline bool is_equal_wasm_rt_func_type_t(const wasm_rt_func_type_t a, + const wasm_rt_func_type_t b) { + return (a == b) || (a && b && !memcmp(a, b, 32)); +} + static bool is_equal_wasm_rt_funcref_t(wasm_rt_funcref_t x, wasm_rt_funcref_t y) { - return (x.func_type == y.func_type) && (x.func == y.func) && - (x.module_instance == y.module_instance); + return is_equal_wasm_rt_func_type_t(x.func_type, y.func_type) && + (x.func == y.func) && (x.module_instance == y.module_instance); } wasm_rt_externref_t spectest_make_externref(uintptr_t x) { diff --git a/test/wasm2c/add.txt b/test/wasm2c/add.txt index 6f005f6d..f9207db3 100644 --- a/test/wasm2c/add.txt +++ b/test/wasm2c/add.txt @@ -35,9 +35,9 @@ typedef struct Z_test_instance_t { char dummy_member; } Z_test_instance_t; -void Z_test_init_module(void); void Z_test_instantiate(Z_test_instance_t*); void Z_test_free(Z_test_instance_t*); +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...); /* export: 'add' */ u32 Z_testZ_add(Z_test_instance_t*, u32, u32); @@ -50,6 +50,7 @@ u32 Z_testZ_add(Z_test_instance_t*, u32, u32); /* Automatically generated by wasm2c */ #include <assert.h> #include <math.h> +#include <stdarg.h> #include <stddef.h> #include <string.h> #if defined(_MSC_VER) @@ -78,10 +79,15 @@ u32 Z_testZ_add(Z_test_instance_t*, u32, u32); #define UNREACHABLE TRAP(UNREACHABLE) -#define CALL_INDIRECT(table, t, ft, x, ...) \ - (LIKELY((x) < table.size && table.data[x].func && \ - table.data[x].func_type == func_types[ft]) || \ - TRAP(CALL_INDIRECT), \ +static inline bool func_types_eq(const wasm_rt_func_type_t a, + const wasm_rt_func_type_t b) { + return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); +} + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (LIKELY((x) < table.size && table.data[x].func && \ + func_types_eq(ft, table.data[x].func_type)) || \ + TRAP(CALL_INDIRECT), \ ((t)table.data[x].func)(__VA_ARGS__)) #ifdef SUPPORT_MEMORY64 @@ -543,7 +549,7 @@ static inline void memory_init(wasm_rt_memory_t* dest, } typedef struct { - uint32_t func_type_index; + wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; size_t module_offset; } wasm_elem_segment_expr_t; @@ -554,17 +560,16 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, u32 dest_addr, u32 src_addr, u32 n, - void* module_instance, - const u32* func_types) { + void* module_instance) { 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_funcref_t){ - func_types[src_expr->func_type_index], src_expr->func, - (char*)module_instance + src_expr->module_offset}; + dest->data[dest_addr + i] = + (wasm_rt_funcref_t){src_expr->type, src_expr->func, + (char*)module_instance + src_expr->module_offset}; } } @@ -635,13 +640,13 @@ DEFINE_TABLE_SET(externref) DEFINE_TABLE_FILL(funcref) DEFINE_TABLE_FILL(externref) -static bool s_module_initialized = false; - -static u32 func_types[1]; +#if defined(__GNUC__) || defined(__clang__) +#define FUNC_TYPE_T(x) static const char* const x +#else +#define FUNC_TYPE_T(x) static const char x[] +#endif -static void init_func_types(void) { - func_types[0] = wasm_rt_register_func_type(2, 1, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32); -} +FUNC_TYPE_T(w2c_t0) = "\x92\xfb\x6a\xdf\x49\x07\x0a\x83\xbe\x08\x02\x68\xcd\xf6\x95\x27\x4a\xc2\xf3\xe5\xe4\x7d\x29\x49\xe8\xed\x42\x92\x6a\x9d\xda\xf0"; static u32 w2c_add(Z_test_instance_t*, u32, u32); @@ -660,17 +665,25 @@ u32 Z_testZ_add(Z_test_instance_t* instance, u32 w2c_p0, u32 w2c_p1) { return w2c_add(instance, w2c_p0, w2c_p1); } -void Z_test_init_module(void) { - assert(wasm_rt_is_initialized()); - s_module_initialized = true; - init_func_types(); -} - void Z_test_instantiate(Z_test_instance_t* instance) { assert(wasm_rt_is_initialized()); - assert(s_module_initialized); } void Z_test_free(Z_test_instance_t* instance) { } + +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...) { + va_list args; + + if (param_count == 2 && result_count == 1) { + va_start(args, result_count); + if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32) { + va_end(args); + return w2c_t0; + } + va_end(args); + } + + return NULL; +} ;;; STDOUT ;;) diff --git a/test/wasm2c/hello.txt b/test/wasm2c/hello.txt index 0e3f0f26..91a55482 100644 --- a/test/wasm2c/hello.txt +++ b/test/wasm2c/hello.txt @@ -57,9 +57,9 @@ typedef struct Z_test_instance_t { wasm_rt_funcref_table_t w2c_T0; } Z_test_instance_t; -void Z_test_init_module(void); void Z_test_instantiate(Z_test_instance_t*, struct Z_wasi_snapshot_preview1_instance_t*); void Z_test_free(Z_test_instance_t*); +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...); /* import: 'wasi_snapshot_preview1' 'fd_write' */ u32 Z_wasi_snapshot_preview1Z_fd_write(struct Z_wasi_snapshot_preview1_instance_t*, u32, u32, u32, u32); @@ -80,6 +80,7 @@ void Z_testZ__start(Z_test_instance_t*); /* Automatically generated by wasm2c */ #include <assert.h> #include <math.h> +#include <stdarg.h> #include <stddef.h> #include <string.h> #if defined(_MSC_VER) @@ -108,10 +109,15 @@ void Z_testZ__start(Z_test_instance_t*); #define UNREACHABLE TRAP(UNREACHABLE) -#define CALL_INDIRECT(table, t, ft, x, ...) \ - (LIKELY((x) < table.size && table.data[x].func && \ - table.data[x].func_type == func_types[ft]) || \ - TRAP(CALL_INDIRECT), \ +static inline bool func_types_eq(const wasm_rt_func_type_t a, + const wasm_rt_func_type_t b) { + return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); +} + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (LIKELY((x) < table.size && table.data[x].func && \ + func_types_eq(ft, table.data[x].func_type)) || \ + TRAP(CALL_INDIRECT), \ ((t)table.data[x].func)(__VA_ARGS__)) #ifdef SUPPORT_MEMORY64 @@ -573,7 +579,7 @@ static inline void memory_init(wasm_rt_memory_t* dest, } typedef struct { - uint32_t func_type_index; + wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; size_t module_offset; } wasm_elem_segment_expr_t; @@ -584,17 +590,16 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, u32 dest_addr, u32 src_addr, u32 n, - void* module_instance, - const u32* func_types) { + void* module_instance) { 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_funcref_t){ - func_types[src_expr->func_type_index], src_expr->func, - (char*)module_instance + src_expr->module_offset}; + dest->data[dest_addr + i] = + (wasm_rt_funcref_t){src_expr->type, src_expr->func, + (char*)module_instance + src_expr->module_offset}; } } @@ -665,15 +670,15 @@ DEFINE_TABLE_SET(externref) DEFINE_TABLE_FILL(funcref) DEFINE_TABLE_FILL(externref) -static bool s_module_initialized = false; - -static u32 func_types[3]; +#if defined(__GNUC__) || defined(__clang__) +#define FUNC_TYPE_T(x) static const char* const x +#else +#define FUNC_TYPE_T(x) static const char x[] +#endif -static void init_func_types(void) { - func_types[0] = wasm_rt_register_func_type(4, 1, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32); - func_types[1] = wasm_rt_register_func_type(1, 0, WASM_RT_I32); - func_types[2] = wasm_rt_register_func_type(0, 0); -} +FUNC_TYPE_T(w2c_t0) = "\xf6\x98\x1b\xc6\x10\xda\xb7\xb2\x63\x37\xcd\xdc\x72\xca\xe9\x50\x00\x13\xba\x10\x6c\xde\x87\x27\x10\xf8\x86\x2f\xe3\xdb\x94\xe4"; +FUNC_TYPE_T(w2c_t1) = "\x89\x3a\x3d\x2c\x8f\x4d\x7f\x6d\x6c\x9d\x62\x67\x29\xaf\x3d\x44\x39\x8e\xc3\xf3\xe8\x51\xc1\x99\xb9\xdd\x9f\xd5\x3d\x1f\xd3\xe4"; +FUNC_TYPE_T(w2c_t2) = "\x36\xa9\xe7\xf1\xc9\x5b\x82\xff\xb9\x97\x43\xe0\xc5\xc4\xce\x95\xd8\x3c\x9a\x43\x0a\xac\x59\xf8\x4e\xf3\xcb\xfa\xb6\x14\x50\x68"; static void w2c__start(Z_test_instance_t*); @@ -690,12 +695,12 @@ static void init_memories(Z_test_instance_t* instance) { static void init_data_instances(Z_test_instance_t *instance) { } static const wasm_elem_segment_expr_t elem_segment_exprs_w2c_e0[] = { - {0, (wasm_rt_function_ptr_t)Z_wasi_snapshot_preview1Z_fd_write, offsetof(Z_test_instance_t, Z_wasi_snapshot_preview1_instance)}, + {w2c_t0, (wasm_rt_function_ptr_t)Z_wasi_snapshot_preview1Z_fd_write, offsetof(Z_test_instance_t, Z_wasi_snapshot_preview1_instance)}, }; static void init_tables(Z_test_instance_t* instance) { wasm_rt_allocate_funcref_table(&instance->w2c_T0, 1, 1); - funcref_table_init(&instance->w2c_T0, elem_segment_exprs_w2c_e0, 1, 0u, 0, 1, instance, func_types); + funcref_table_init(&instance->w2c_T0, elem_segment_exprs_w2c_e0, 1, 0u, 0, 1, instance); } static void init_elem_instances(Z_test_instance_t *instance) { @@ -715,7 +720,7 @@ static void w2c__start(Z_test_instance_t* instance) { w2c_i2 = 1u; w2c_i3 = 0u; w2c_i4 = 0u; - w2c_i0 = CALL_INDIRECT(instance->w2c_T0, u32 (*)(void*, u32, u32, u32, u32), 0, w2c_i4, instance->w2c_T0.data[w2c_i4].module_instance, w2c_i0, w2c_i1, w2c_i2, w2c_i3); + w2c_i0 = CALL_INDIRECT(instance->w2c_T0, u32 (*)(void*, u32, u32, u32, u32), w2c_t0, w2c_i4, instance->w2c_T0.data[w2c_i4].module_instance, w2c_i0, w2c_i1, w2c_i2, w2c_i3); (*Z_wasi_snapshot_preview1Z_proc_exit)(instance->Z_wasi_snapshot_preview1_instance, w2c_i0); FUNC_EPILOGUE; } @@ -734,15 +739,8 @@ static void init_instance_import(Z_test_instance_t* instance, struct Z_wasi_snap instance->Z_wasi_snapshot_preview1_instance = Z_wasi_snapshot_preview1_instance; } -void Z_test_init_module(void) { - assert(wasm_rt_is_initialized()); - s_module_initialized = true; - init_func_types(); -} - void Z_test_instantiate(Z_test_instance_t* instance, struct Z_wasi_snapshot_preview1_instance_t* Z_wasi_snapshot_preview1_instance) { assert(wasm_rt_is_initialized()); - assert(s_module_initialized); init_instance_import(instance, Z_wasi_snapshot_preview1_instance); init_memories(instance); init_tables(instance); @@ -754,4 +752,37 @@ void Z_test_free(Z_test_instance_t* instance) { wasm_rt_free_funcref_table(&instance->w2c_T0); wasm_rt_free_memory(&instance->w2c_memory); } + +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...) { + va_list args; + + if (param_count == 4 && result_count == 1) { + va_start(args, result_count); + if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32) { + va_end(args); + return w2c_t0; + } + va_end(args); + } + + if (param_count == 1 && result_count == 0) { + va_start(args, result_count); + if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32) { + va_end(args); + return w2c_t1; + } + va_end(args); + } + + if (param_count == 0 && result_count == 0) { + va_start(args, result_count); + if (true) { + va_end(args); + return w2c_t2; + } + va_end(args); + } + + return NULL; +} ;;; STDOUT ;;) diff --git a/test/wasm2c/minimal.txt b/test/wasm2c/minimal.txt index 857dc879..6a706d40 100644 --- a/test/wasm2c/minimal.txt +++ b/test/wasm2c/minimal.txt @@ -32,9 +32,9 @@ typedef struct Z_test_instance_t { char dummy_member; } Z_test_instance_t; -void Z_test_init_module(void); void Z_test_instantiate(Z_test_instance_t*); void Z_test_free(Z_test_instance_t*); +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...); #ifdef __cplusplus } @@ -44,6 +44,7 @@ void Z_test_free(Z_test_instance_t*); /* Automatically generated by wasm2c */ #include <assert.h> #include <math.h> +#include <stdarg.h> #include <stddef.h> #include <string.h> #if defined(_MSC_VER) @@ -72,10 +73,15 @@ void Z_test_free(Z_test_instance_t*); #define UNREACHABLE TRAP(UNREACHABLE) -#define CALL_INDIRECT(table, t, ft, x, ...) \ - (LIKELY((x) < table.size && table.data[x].func && \ - table.data[x].func_type == func_types[ft]) || \ - TRAP(CALL_INDIRECT), \ +static inline bool func_types_eq(const wasm_rt_func_type_t a, + const wasm_rt_func_type_t b) { + return (a == b) || LIKELY(a && b && !memcmp(a, b, 32)); +} + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (LIKELY((x) < table.size && table.data[x].func && \ + func_types_eq(ft, table.data[x].func_type)) || \ + TRAP(CALL_INDIRECT), \ ((t)table.data[x].func)(__VA_ARGS__)) #ifdef SUPPORT_MEMORY64 @@ -537,7 +543,7 @@ static inline void memory_init(wasm_rt_memory_t* dest, } typedef struct { - uint32_t func_type_index; + wasm_rt_func_type_t type; wasm_rt_function_ptr_t func; size_t module_offset; } wasm_elem_segment_expr_t; @@ -548,17 +554,16 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, u32 dest_addr, u32 src_addr, u32 n, - void* module_instance, - const u32* func_types) { + void* module_instance) { 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_funcref_t){ - func_types[src_expr->func_type_index], src_expr->func, - (char*)module_instance + src_expr->module_offset}; + dest->data[dest_addr + i] = + (wasm_rt_funcref_t){src_expr->type, src_expr->func, + (char*)module_instance + src_expr->module_offset}; } } @@ -629,18 +634,22 @@ DEFINE_TABLE_SET(externref) DEFINE_TABLE_FILL(funcref) DEFINE_TABLE_FILL(externref) -static bool s_module_initialized = false; - -void Z_test_init_module(void) { - assert(wasm_rt_is_initialized()); - s_module_initialized = true; -} +#if defined(__GNUC__) || defined(__clang__) +#define FUNC_TYPE_T(x) static const char* const x +#else +#define FUNC_TYPE_T(x) static const char x[] +#endif void Z_test_instantiate(Z_test_instance_t* instance) { assert(wasm_rt_is_initialized()); - assert(s_module_initialized); } void Z_test_free(Z_test_instance_t* instance) { } + +wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...) { + va_list args; + + return NULL; +} ;;; STDOUT ;;) |