From 986808ceae48d72a8d0d7cda7bdcb76f98d04266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 16 Nov 2020 17:36:45 +0100 Subject: [PATCH] Return stack end from execute_internal() --- lib/fizzy/execute.cpp | 21 ++++++++++----------- lib/fizzy/stack.hpp | 2 ++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index c72222334a..2d390fabfd 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -474,19 +474,14 @@ void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, uint32_t stack.drop(stack_drop); } -inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance, - OperandStack& stack, int depth) +inline bool invoke_function(uint32_t func_idx, Instance& instance, OperandStack& stack, int depth) { - const auto num_args = func_type.inputs.size(); - const auto num_outputs = func_type.outputs.size(); - assert(stack.size() >= num_args); - const auto ret = execute_internal(instance, func_idx, stack.rend(), depth + 1); // Bubble up traps if (ret == nullptr) return false; - stack.drop(num_args - num_outputs); + stack.set_end(ret); return true; } @@ -512,7 +507,10 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i return nullptr; if (res.has_value) + { args[0] = res.value; + return args + 1; + } return args; } @@ -589,9 +587,8 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i case Instr::call: { const auto called_func_idx = read(pc); - const auto& called_func_type = instance.module->get_function_type(called_func_idx); - if (!invoke_function(called_func_type, called_func_idx, instance, stack, depth)) + if (!invoke_function(called_func_idx, instance, stack, depth)) goto trap; break; } @@ -617,8 +614,7 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i if (expected_type != actual_type) goto trap; - if (!invoke_function( - actual_type, called_func.func_idx, *called_func.instance, stack, depth)) + if (!invoke_function(called_func.func_idx, *called_func.instance, stack, depth)) goto trap; break; } @@ -1539,7 +1535,10 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i assert(stack.size() == instance.module->get_function_type(func_idx).outputs.size()); if (stack.size() != 0 && args != nullptr) + { args[0] = stack.top(); + return args + 1; + } return args; diff --git a/lib/fizzy/stack.hpp b/lib/fizzy/stack.hpp index 5e0b918f8b..19747dbee3 100644 --- a/lib/fizzy/stack.hpp +++ b/lib/fizzy/stack.hpp @@ -138,6 +138,8 @@ class OperandStack return *m_top; } + void set_end(Value* end) noexcept { m_top = end - 1; } + /// Returns the reference to the stack item on given position from the stack top. /// Requires index < size(). Value& operator[](size_t index) noexcept