From c5619fe4efed8dfec534c8fa887e36890f6b5a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 16:53:52 +0200 Subject: [PATCH 1/6] Update PC only when needed --- lib/fizzy/execute.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index c3a5e63af..255a9f9d1 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -540,7 +540,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, while (true) { - const auto instruction = *pc++; + const auto instruction = *pc; switch (instruction) { case Instr::unreachable: @@ -552,7 +552,10 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, case Instr::if_: { if (stack.pop().as() != 0) + { immediates += 2 * sizeof(uint32_t); // Skip the immediates for else instruction. + break; + } else { const auto target_pc = read(immediates); @@ -560,8 +563,8 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, pc = code.instructions.data() + target_pc; immediates = code.immediates.data() + target_imm; + continue; } - break; } case Instr::else_: { @@ -573,12 +576,12 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, pc = code.instructions.data() + target_pc; immediates = code.immediates.data() + target_imm; - break; + continue; } case Instr::end: { // End execution if it's a final end instruction. - if (pc == &code.instructions[code.instructions.size()]) + if (pc == &code.instructions[code.instructions.size() - 1]) goto end; break; } @@ -596,7 +599,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, } branch(code, stack, pc, immediates, arity); - break; + continue; } case Instr::br_table: { @@ -611,7 +614,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, immediates += label_idx_offset; branch(code, stack, pc, immediates, arity); - break; + continue; } case Instr::call: { @@ -1557,10 +1560,12 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, default: FIZZY_UNREACHABLE(); } + + ++pc; } end: - assert(pc == &code.instructions[code.instructions.size()]); // End of code must be reached. + assert(pc == &code.instructions[code.instructions.size() - 1]); // End of code must be reached. assert(stack.size() == instance.module->get_function_type(func_idx).outputs.size()); return stack.size() != 0 ? ExecutionResult{stack.top()} : Void; From 0210b87d6499203d0ebcb7dcd243da07214fddb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 16:57:57 +0200 Subject: [PATCH 2/6] Skip block and loop instructions --- lib/fizzy/execute.cpp | 2 -- lib/fizzy/parser_expr.cpp | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 255a9f9d1..7436e50cf 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -546,8 +546,6 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, case Instr::unreachable: goto trap; case Instr::nop: - case Instr::block: - case Instr::loop: break; case Instr::if_: { diff --git a/lib/fizzy/parser_expr.cpp b/lib/fizzy/parser_expr.cpp index 28bc47a00..5e6bb9408 100644 --- a/lib/fizzy/parser_expr.cpp +++ b/lib/fizzy/parser_expr.cpp @@ -466,7 +466,7 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f // Push label with immediates offset after arity. control_stack.emplace(Instr::block, block_type, static_cast(operand_stack.size()), code.instructions.size(), code.immediates.size()); - break; + continue; } case Instr::loop: @@ -476,7 +476,7 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f control_stack.emplace(Instr::loop, loop_type, static_cast(operand_stack.size()), code.instructions.size(), code.immediates.size()); - break; + continue; } case Instr::if_: From 39de85b1eef9d0f081dfa633bfaab334ab280fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 17:02:18 +0200 Subject: [PATCH 3/6] Skip nop instructions --- lib/fizzy/execute.cpp | 4 +--- lib/fizzy/parser_expr.cpp | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 7436e50cf..eebdd4b0e 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -545,8 +545,6 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, { case Instr::unreachable: goto trap; - case Instr::nop: - break; case Instr::if_: { if (stack.pop().as() != 0) @@ -585,7 +583,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, } case Instr::br: case Instr::br_if: - case Instr::return_: + case Instr::return_: // TODO: Replace return with br { const auto arity = read(immediates); diff --git a/lib/fizzy/parser_expr.cpp b/lib/fizzy/parser_expr.cpp index 5e6bb9408..a4dfbd115 100644 --- a/lib/fizzy/parser_expr.cpp +++ b/lib/fizzy/parser_expr.cpp @@ -333,6 +333,8 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f } case Instr::nop: + continue; + case Instr::i32_eqz: case Instr::i32_eq: case Instr::i32_ne: From d12bd378d25cf9906629b3d5a5939504ad7a478d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 19:24:34 +0200 Subject: [PATCH 4/6] Do not omit end instructions --- lib/fizzy/parser_expr.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/fizzy/parser_expr.cpp b/lib/fizzy/parser_expr.cpp index a4dfbd115..6ba0b447d 100644 --- a/lib/fizzy/parser_expr.cpp +++ b/lib/fizzy/parser_expr.cpp @@ -540,9 +540,7 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f // In case it's an outermost implicit function block, // we want br to jump to the final end of the function. // Otherwise jump to the next instruction after block's end. - const auto target_pc = control_stack.size() == 1 ? - static_cast(code.instructions.size()) : - static_cast(code.instructions.size() + 1); + const auto target_pc = static_cast(code.instructions.size()); const auto target_imm = static_cast(code.immediates.size()); if (frame.instruction == Instr::if_ || frame.instruction == Instr::else_) From 630272edbc658b6bcadf6f1f783ede25b7c92d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 19:24:53 +0200 Subject: [PATCH 5/6] Skip end instructions --- lib/fizzy/parser_expr.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/fizzy/parser_expr.cpp b/lib/fizzy/parser_expr.cpp index 6ba0b447d..a658f1373 100644 --- a/lib/fizzy/parser_expr.cpp +++ b/lib/fizzy/parser_expr.cpp @@ -569,10 +569,14 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f control_stack.pop(); // Pop the current frame. if (control_stack.empty()) + { continue_parsing = false; - else if (frame_type.has_value()) + break; + } + + if (frame_type.has_value()) push_operand(operand_stack, *frame_type); - break; + continue; } case Instr::br: From 8f311f18195d80dff27131047f71b3ef32f96557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 20 Oct 2020 19:25:59 +0200 Subject: [PATCH 6/6] Simplify end implementation --- lib/fizzy/execute.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index eebdd4b0e..12e01a89e 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -576,10 +576,8 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, } case Instr::end: { - // End execution if it's a final end instruction. - if (pc == &code.instructions[code.instructions.size() - 1]) - goto end; - break; + assert(pc == &code.instructions[code.instructions.size() - 1]); + goto end; } case Instr::br: case Instr::br_if: