From ca4e5c1cca9f0e6db5f564e6786a356bcdffb956 Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Mon, 21 Sep 2020 11:53:13 +0200 Subject: put function call wasm_compile test in separate bench --- tests/wasm_compile_function_call/Makefile | 1 + tests/wasm_compile_function_call/instructions.wat | 13 +++++++++++++ tests/wasm_compile_function_call/test.v | 1 + .../wasm_compile_function_call/words_to_verify.mem | 3 +++ tests/wasm_compile_simple_module/instructions.wat | 12 +++++------- .../wasm_compile_simple_module/words_to_verify.mem | 2 +- tools/assemble.c | 10 ++++------ tools/translate.c | 21 +++++++++++++++++++++ 8 files changed, 49 insertions(+), 14 deletions(-) create mode 120000 tests/wasm_compile_function_call/Makefile create mode 100644 tests/wasm_compile_function_call/instructions.wat create mode 120000 tests/wasm_compile_function_call/test.v create mode 100644 tests/wasm_compile_function_call/words_to_verify.mem diff --git a/tests/wasm_compile_function_call/Makefile b/tests/wasm_compile_function_call/Makefile new file mode 120000 index 0000000..e451c8b --- /dev/null +++ b/tests/wasm_compile_function_call/Makefile @@ -0,0 +1 @@ +../wasm_compile_simple_module/Makefile \ No newline at end of file diff --git a/tests/wasm_compile_function_call/instructions.wat b/tests/wasm_compile_function_call/instructions.wat new file mode 100644 index 0000000..ad37037 --- /dev/null +++ b/tests/wasm_compile_function_call/instructions.wat @@ -0,0 +1,13 @@ +(module + (memory 0 2) + (func $main + i32.const 0x17 ;; dynamic offset for memory store instruction + i32.const 0x32 ;; value to substract from + i32.const 0x14 ;; value to substract + call $sub + i32.store offset=0x25 align=2) ;; we store the difference at 0x23C + (func $sub (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.sub) + (export "main" (func $main))) diff --git a/tests/wasm_compile_function_call/test.v b/tests/wasm_compile_function_call/test.v new file mode 120000 index 0000000..f0235d8 --- /dev/null +++ b/tests/wasm_compile_function_call/test.v @@ -0,0 +1 @@ +../wasm_compile_simple_module/test.v \ No newline at end of file diff --git a/tests/wasm_compile_function_call/words_to_verify.mem b/tests/wasm_compile_function_call/words_to_verify.mem new file mode 100644 index 0000000..bd24e95 --- /dev/null +++ b/tests/wasm_compile_function_call/words_to_verify.mem @@ -0,0 +1,3 @@ +// address value + 0FFFFC 23 + 23C 1E // Address is 0x200 + 0x25 + 0x17 diff --git a/tests/wasm_compile_simple_module/instructions.wat b/tests/wasm_compile_simple_module/instructions.wat index 7579a46..bdf8a16 100644 --- a/tests/wasm_compile_simple_module/instructions.wat +++ b/tests/wasm_compile_simple_module/instructions.wat @@ -1,9 +1,7 @@ (module (memory 0 2) - (func $sub (param $lhs i32) (param $rhs i32);; (result i32) - i32.const 0x17 - local.get $lhs - local.get $rhs - i32.sub - i32.store offset=0x25 align=2) - (export "main" (func $sub))) + (func $main + i32.const 0x1D ;; dynamic offset for memory store instruction + i32.const 0x1E ;; value to store + i32.store offset=0x1F align=2) ;; we store the difference at 0x23C + (export "main" (func $main))) diff --git a/tests/wasm_compile_simple_module/words_to_verify.mem b/tests/wasm_compile_simple_module/words_to_verify.mem index bd24e95..d6a16c1 100644 --- a/tests/wasm_compile_simple_module/words_to_verify.mem +++ b/tests/wasm_compile_simple_module/words_to_verify.mem @@ -1,3 +1,3 @@ // address value 0FFFFC 23 - 23C 1E // Address is 0x200 + 0x25 + 0x17 + 23C 1E // Address is 0x200 + 0x1F + 0x1D diff --git a/tools/assemble.c b/tools/assemble.c index 07416fd..11bb7c9 100644 --- a/tools/assemble.c +++ b/tools/assemble.c @@ -163,14 +163,12 @@ int assemble(uint32_t memory_size, uint16_t memory[memory_size], goto fail; } - // For now we're passing 2 numbers to main(): 0x32 and 0x14. - // This is just a temporary way to check if our Wasm function - // actually does anything. In the end - main() will be - // a function, that doesn't require any arguments. + /* + * We're first writing some value at STACK_FRAME_BACKUP_ADDR to be able + * to check, if the functions we call restore frame address properly + */ if (i_const(im(0x23), &startup) || i_store(im(STACK_FRAME_BACKUP_ADDR), &startup) || - i_const(im(0x32), &startup) || - i_const(im(0x14), &startup) || i_call (ptr(&main_function->translated_body), &startup) || i_halt ( &startup)) goto fail; diff --git a/tools/translate.c b/tools/translate.c index ee6ea99..4361d6f 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -3,6 +3,7 @@ /* WebAssembly opcodes */ #define WASM_END 0x0B +#define WASM_CALL 0x10 #define WASM_LOCAL_GET 0x20 #define WASM_I32_LOAD 0x28 #define WASM_I32_LOAD8_S 0x2C @@ -62,6 +63,26 @@ int translate(FILE *handle, struct function *function, struct module *module) if (i_sub(&expr)) goto fail; + matched = 1; + } else if (wasm_opcode == WASM_CALL) { + uint32_t funcidx; + struct instruction **target; + + if (leb_u32(handle, &funcidx)) { + PRERR(MSG_BAD_NUM); + goto fail; + } + + if (funcidx >= module->functions_count) { + PRERR(MSG_BAD_IDX("funcidx")); + goto fail; + } + + target = &module->functions[funcidx].translated_body; + + if (i_call(ptr(target), &expr)) + goto fail; + matched = 1; } else if (wasm_opcode <= WASM_I64_STORE32 && wasm_opcode >= WASM_I32_LOAD) { -- cgit v1.2.3