From 2a677ec1fb0a1213535200ba3c84a4d74adb761c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 30 Jan 2020 12:22:53 +0000 Subject: [PATCH] Refactor instance to avoid keeping a copy of Module --- lib/fizzy/execute.cpp | 41 ++++++++++++++++++----------------------- lib/fizzy/execute.hpp | 5 ++++- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 9a7bdc81f..d214c793d 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -348,12 +348,8 @@ Instance instantiate(const Module& module, std::vector importe assert(module.elementsec.empty() || !module.tablesec.empty()); for (const auto& element : module.elementsec) { - uint64_t offset; - if (element.offset.kind == ConstantExpression::Kind::Constant) - offset = element.offset.value.constant; - else - throw std::runtime_error( - "element initialization by imported global is not supported yet"); + const uint64_t offset = + eval_constant_expression(element.offset, imported_globals, module.globalsec, globals); // Overwrite table[offset..] with element.init assert((offset + element.init.size()) <= table.size()); @@ -371,9 +367,9 @@ Instance instantiate(const Module& module, std::vector importe std::memcpy(memory.data() + offset, data.init.data(), data.init.size()); } - Instance instance = {module, std::move(memory), memory_max, std::move(table), + Instance instance = {module.codesec, std::move(memory), memory_max, std::move(table), std::move(globals), std::move(imported_functions), std::move(imported_function_types), - std::move(imported_globals)}; + std::move(imported_globals), module.typesec, module.funcsec, module.globalsec}; // Run start function if present if (module.startfunc) @@ -391,9 +387,9 @@ execution_result execute(Instance& instance, FuncIdx func_idx, std::vector locals = std::move(args); locals.resize(locals.size() + code.local_count); @@ -479,15 +475,14 @@ execution_result execute(Instance& instance, FuncIdx func_idx, std::vector(immediates); - assert(called_func_idx < - instance.imported_functions.size() + instance.module.funcsec.size()); + assert(called_func_idx < instance.imported_functions.size() + instance.funcsec.size()); const auto type_idx = called_func_idx < instance.imported_functions.size() ? instance.imported_function_types[called_func_idx] : - instance.module.funcsec[called_func_idx - instance.imported_functions.size()]; - assert(type_idx < instance.module.typesec.size()); + instance.funcsec[called_func_idx - instance.imported_functions.size()]; + assert(type_idx < instance.typesec.size()); - const auto num_inputs = instance.module.typesec[type_idx].inputs.size(); + const auto num_inputs = instance.typesec[type_idx].inputs.size(); assert(stack.size() >= num_inputs); std::vector call_args( stack.rbegin(), stack.rbegin() + static_cast(num_inputs)); @@ -501,7 +496,7 @@ execution_result execute(Instance& instance, FuncIdx func_idx, std::vector codesec; bytes memory; size_t memory_max_pages = 0; std::vector table; @@ -36,6 +36,9 @@ struct Instance std::vector imported_functions; std::vector imported_function_types; std::vector imported_globals; + std::vector typesec; + std::vector funcsec; + std::vector globalsec; }; // Instantiate a module.