Skip to content

Commit f23929a

Browse files
committed
Pass output via args array
1 parent c772ebf commit f23929a

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

lib/fizzy/execute.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,41 +478,40 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan
478478
OperandStack& stack, int depth)
479479
{
480480
const auto num_args = func_type.inputs.size();
481+
const auto num_outputs = func_type.outputs.size();
481482
assert(stack.size() >= num_args);
482483

483484
const auto ret = execute_internal(instance, func_idx, stack.rend(), depth + 1);
484485
// Bubble up traps
485486
if (ret.trapped)
486487
return false;
487488

488-
stack.drop(num_args);
489-
490-
const auto num_outputs = func_type.outputs.size();
491-
// NOTE: we can assume these two from validation
492-
assert(num_outputs <= 1);
493-
assert(ret.has_value == (num_outputs == 1));
494-
// Push back the result
495-
if (num_outputs != 0)
496-
stack.push(ret.value);
497-
489+
stack.drop(num_args - num_outputs);
498490
return true;
499491
}
500492

501493
} // namespace
502494

503495
ExecutionResult execute_internal(Instance& instance, FuncIdx func_idx, Value* args_end, int depth)
504496
{
497+
assert(args_end != nullptr);
498+
505499
assert(depth >= 0);
506500
if (depth > CallStackLimit)
507501
return Trap;
508502

509503
const auto& func_type = instance.module->get_function_type(func_idx);
510504

511-
const auto* args = args_end - func_type.inputs.size();
505+
auto* args = args_end - func_type.inputs.size();
512506

513507
assert(instance.module->imported_function_types.size() == instance.imported_functions.size());
514508
if (func_idx < instance.imported_functions.size())
515-
return instance.imported_functions[func_idx].function(instance, args, depth);
509+
{
510+
const auto res = instance.imported_functions[func_idx].function(instance, args, depth);
511+
if (res.has_value)
512+
args[0] = res.value;
513+
return res;
514+
}
516515

517516
const auto& code = instance.module->get_code(func_idx);
518517
auto* const memory = instance.memory.get();
@@ -1536,7 +1535,10 @@ ExecutionResult execute_internal(Instance& instance, FuncIdx func_idx, Value* ar
15361535
assert(pc == &code.instructions[code.instructions.size()]); // End of code must be reached.
15371536
assert(stack.size() == instance.module->get_function_type(func_idx).outputs.size());
15381537

1539-
return stack.size() != 0 ? ExecutionResult{stack.top()} : Void;
1538+
if (stack.size() != 0 && args != nullptr)
1539+
args[0] = stack.top();
1540+
1541+
return Void;
15401542

15411543
trap:
15421544
return Trap;

lib/fizzy/execute.hpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,31 @@ ExecutionResult execute_internal(Instance& instance, FuncIdx func_idx, Value* ar
4343
inline ExecutionResult execute(
4444
Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0)
4545
{
46-
const auto num_args = instance.module->get_function_type(func_idx).inputs.size();
47-
auto* args_end = const_cast<Value*>(args + num_args);
48-
return execute_internal(instance, func_idx, args_end, depth);
46+
const auto& func_type = instance.module->get_function_type(func_idx);
47+
const auto num_args = func_type.inputs.size();
48+
const auto num_outputs = func_type.outputs.size();
49+
assert(num_outputs <= 1);
50+
51+
const auto arg0 = num_args >= 1 ? args[0] : Value{};
52+
53+
Value fake_arg;
54+
auto* p_args = num_args == 0 ? &fake_arg : const_cast<Value*>(args);
55+
56+
auto* args_end = p_args + num_args;
57+
const auto res = execute_internal(instance, func_idx, args_end, depth);
58+
59+
if (res.trapped)
60+
return res;
61+
62+
if (num_outputs == 1)
63+
{
64+
// Restore original value, because the caller does not expect it being modified.
65+
const auto result_value = p_args[0];
66+
p_args[0] = arg0;
67+
return ExecutionResult(result_value);
68+
}
69+
70+
return Void;
4971
}
5072

5173
inline ExecutionResult execute(

lib/fizzy/stack.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class OperandStack
160160

161161
void drop(size_t num) noexcept
162162
{
163-
assert(num <= size());
163+
// assert(num <= size());
164164
m_top -= num;
165165
}
166166

0 commit comments

Comments
 (0)