From c75dcab5988f5c0b5e0629015f384e5aa1440690 Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Mon, 21 Sep 2020 13:43:29 +0200 Subject: enable translation of few arithmetic operations (testbench included) --- tests/wasm_compile_arithm/Makefile | 1 + tests/wasm_compile_arithm/instructions.wat | 16 ++++++++++++++++ tests/wasm_compile_arithm/test.v | 1 + tests/wasm_compile_arithm/words_to_verify.mem | 7 +++++++ tools/stack_machine_instruction.h | 3 +++ tools/translate.c | 25 ++++++++++++++++++++++++- 6 files changed, 52 insertions(+), 1 deletion(-) create mode 120000 tests/wasm_compile_arithm/Makefile create mode 100644 tests/wasm_compile_arithm/instructions.wat create mode 120000 tests/wasm_compile_arithm/test.v create mode 100644 tests/wasm_compile_arithm/words_to_verify.mem diff --git a/tests/wasm_compile_arithm/Makefile b/tests/wasm_compile_arithm/Makefile new file mode 120000 index 0000000..e451c8b --- /dev/null +++ b/tests/wasm_compile_arithm/Makefile @@ -0,0 +1 @@ +../wasm_compile_simple_module/Makefile \ No newline at end of file diff --git a/tests/wasm_compile_arithm/instructions.wat b/tests/wasm_compile_arithm/instructions.wat new file mode 100644 index 0000000..a43a348 --- /dev/null +++ b/tests/wasm_compile_arithm/instructions.wat @@ -0,0 +1,16 @@ +(module + (memory 0 2) + (func $main + ;; write 0xFFEFE021 at MEMORY_BOTTOM_ADDR + 0x0 + (i32.store offset=0x0 align=2 (i32.const 0x0) + (i32.sub (i32.const 0x42143) (i32.const 0x144122))) + ;; write 0x16D3B7 at MEMORY_BOTTOM_ADDR + 0x4 + (i32.store offset=0x0 align=2 (i32.const 0x4) + (i32.add (i32.const 0x7109D) (i32.const 0xFC31A))) + ;; write 0xED8 at MEMORY_BOTTOM_ADDR + 0x8 + (i32.store offset=0x0 align=2 (i32.const 0x8) + (i32.div_u (i32.const 0x193E324F) (i32.const 0x1B342))) + ;; write 0x3C773E7C at MEMORY_BOTTOM_ADDR + 0xC + (i32.store offset=0x0 align=2 (i32.const 0xC) + (i32.mul (i32.const 0x38F2C) (i32.const 0x10FD)))) + (export "main" (func $main))) diff --git a/tests/wasm_compile_arithm/test.v b/tests/wasm_compile_arithm/test.v new file mode 120000 index 0000000..f0235d8 --- /dev/null +++ b/tests/wasm_compile_arithm/test.v @@ -0,0 +1 @@ +../wasm_compile_simple_module/test.v \ No newline at end of file diff --git a/tests/wasm_compile_arithm/words_to_verify.mem b/tests/wasm_compile_arithm/words_to_verify.mem new file mode 100644 index 0000000..d3f3c4e --- /dev/null +++ b/tests/wasm_compile_arithm/words_to_verify.mem @@ -0,0 +1,7 @@ +// address value + 0FFFFC 23 + + 200 FFEFE021 + 204 16D3B7 + 208 ED8 + 20C 3C773E7C diff --git a/tools/stack_machine_instruction.h b/tools/stack_machine_instruction.h index 5fd20fc..7b22c83 100644 --- a/tools/stack_machine_instruction.h +++ b/tools/stack_machine_instruction.h @@ -139,7 +139,10 @@ Y(tee, 0x1000) /* 0001_0000_0000_0000 */ Y(get_frame, 0x1001) /* 0001_0000_0000_0001 */ X(const, 0x5000) /* 0101_0000_0xxx_xxxx */ X(call, 0x5080) /* 0101_0000_1xxx_xxxx */ +Y(add, 0x3000) /* 0011_0000_0000_0000 */ Y(sub, 0x3001) /* 0011_0000_0000_0001 */ +Y(div, 0x3002) /* 0011_0000_0000_0010 */ +Y(mul, 0x3003) /* 0011_0000_0000_0011 */ Y(drop, 0x3004) /* 0011_0000_0000_0100 */ Y(ret, 0x3080) /* 0011_0000_1000_0000 */ Y(halt, 0x0000) /* 0000_0000_0000_0000 */ diff --git a/tools/translate.c b/tools/translate.c index 4361d6f..bf13bd1 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -4,7 +4,9 @@ /* 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 #define WASM_I32_LOAD8_U 0x2D @@ -14,8 +16,14 @@ #define WASM_I32_STORE8 0x3A #define WASM_I32_STORE16 0x3B #define WASM_I64_STORE32 0x3E + #define WASM_I32_CONST 0x41 + +#define WASM_I32_ADD 0x6A #define WASM_I32_SUB 0x6B +#define WASM_I32_MUL 0x6C +#define WASM_I32_DIV_U 0x6E + int translate(FILE *handle, struct function *function, struct module *module) { @@ -59,10 +67,25 @@ int translate(FILE *handle, struct function *function, struct module *module) // TODO: make a function for each instruction type, // call them through some table... - if (wasm_opcode == WASM_I32_SUB) { + if (wasm_opcode == WASM_I32_ADD) { + if (i_add(&expr)) + goto fail; + + matched = 1; + } else if (wasm_opcode == WASM_I32_SUB) { if (i_sub(&expr)) goto fail; + matched = 1; + } else if (wasm_opcode == WASM_I32_MUL) { + if (i_mul(&expr)) + goto fail; + + matched = 1; + } else if (wasm_opcode == WASM_I32_DIV_U) { + if (i_div(&expr)) + goto fail; + matched = 1; } else if (wasm_opcode == WASM_CALL) { uint32_t funcidx; -- cgit v1.2.3