Skip to content

Commit 254e779

Browse files
committed
Return stack end from execute_internal()
1 parent 0869282 commit 254e779

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

lib/fizzy/execute.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,14 @@ void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, uint32_t
474474
stack.drop(stack_drop);
475475
}
476476

477-
inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance,
478-
OperandStack& stack, int depth)
477+
inline bool invoke_function(uint32_t func_idx, Instance& instance, OperandStack& stack, int depth)
479478
{
480-
const auto num_args = func_type.inputs.size();
481-
const auto num_outputs = func_type.outputs.size();
482-
assert(stack.size() >= num_args);
483-
484479
const auto ret = execute_internal(instance, func_idx, stack.rend(), depth + 1);
485480
// Bubble up traps
486481
if (ret == nullptr)
487482
return false;
488483

489-
stack.drop(num_args - num_outputs);
484+
stack.set_end(ret);
490485
return true;
491486
}
492487

@@ -512,7 +507,10 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i
512507
return nullptr;
513508

514509
if (res.has_value)
510+
{
515511
args[0] = res.value;
512+
return args + 1;
513+
}
516514
return args;
517515
}
518516

@@ -589,9 +587,8 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i
589587
case Instr::call:
590588
{
591589
const auto called_func_idx = read<uint32_t>(pc);
592-
const auto& called_func_type = instance.module->get_function_type(called_func_idx);
593590

594-
if (!invoke_function(called_func_type, called_func_idx, instance, stack, depth))
591+
if (!invoke_function(called_func_idx, instance, stack, depth))
595592
goto trap;
596593
break;
597594
}
@@ -617,8 +614,7 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i
617614
if (expected_type != actual_type)
618615
goto trap;
619616

620-
if (!invoke_function(
621-
actual_type, called_func.func_idx, *called_func.instance, stack, depth))
617+
if (!invoke_function(called_func.func_idx, *called_func.instance, stack, depth))
622618
goto trap;
623619
break;
624620
}
@@ -1539,7 +1535,10 @@ Value* execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, i
15391535
assert(stack.size() == instance.module->get_function_type(func_idx).outputs.size());
15401536

15411537
if (stack.size() != 0 && args != nullptr)
1538+
{
15421539
args[0] = stack.top();
1540+
return args + 1;
1541+
}
15431542

15441543
return args;
15451544

lib/fizzy/stack.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class OperandStack
138138
return *m_top;
139139
}
140140

141+
void set_end(Value* end) noexcept { m_top = end - 1; }
142+
141143
/// Returns the reference to the stack item on given position from the stack top.
142144
/// Requires index < size().
143145
Value& operator[](size_t index) noexcept

0 commit comments

Comments
 (0)