From a645da8fd22cea7789f07d4e0b88fdf38f9f5035 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 27 Apr 2018 14:50:13 -0700 Subject: improve --extract-function (#1517) Remove more of the unwanted stuff, and leave just an export to the function we are extracting. Then optimizations can do an effective cleanup. --- src/passes/ExtractFunction.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/passes/ExtractFunction.cpp b/src/passes/ExtractFunction.cpp index b91d2c406..7259f399d 100644 --- a/src/passes/ExtractFunction.cpp +++ b/src/passes/ExtractFunction.cpp @@ -26,19 +26,37 @@ namespace wasm { struct ExtractFunction : public Pass { void run(PassRunner* runner, Module* module) override { - auto* leave = getenv("BYN_LEAVE"); + auto* leave = getenv("BINARYEN_EXTRACT"); if (!leave) { - std::cerr << "usage: set BYN_LEAVE in the env\n"; + std::cerr << "usage: set BINARYEN_EXTRACT in the env\n"; abort(); } Name LEAVE(leave); - std::cerr << "keeping " << LEAVE << "\n"; + std::cerr << "extracting " << LEAVE << "\n"; + bool found = false; for (auto& func : module->functions) { if (func->name != LEAVE) { - // wipe out the body + // wipe out all the contents + func->vars.clear(); func->body = module->allocator.alloc(); + } else { + found = true; } } + if (!found) { + std::cerr << "could not find the function to extract\n"; + abort(); + } + // clear data + module->memory.segments.clear(); + module->table.segments.clear(); + // leave just an export for the thing we want + module->exports.clear(); + auto* export_ = new Export; + export_->name = LEAVE; + export_->value = LEAVE; + export_->kind = ExternalKind::Function; + module->addExport(export_); } }; -- cgit v1.2.3