diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-09-19 15:50:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-19 15:50:30 -0700 |
commit | fe88b47749115009da0447e340cbdc86edf30984 (patch) | |
tree | 7dfd9aba7086c8aa6dff4877ac1ee3b9d78bc5ce /src/passes/Metrics.cpp | |
parent | a53356ab155a7d8c2f334dc9a3c1432bacbc78fe (diff) | |
download | binaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.gz binaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.bz2 binaryen-fe88b47749115009da0447e340cbdc86edf30984.zip |
Unify imported and non-imported things (#1678)
Fixes #1649
This moves us to a single object for functions, which can be imported or nor, and likewise for globals (as a result, GetGlobals do not need to check if the global is imported or not, etc.). All imported things now inherit from Importable, which has the module and base of the import, and if they are set then it is an import.
For convenient iteration, there are a few helpers like
ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) {
.. use global ..
});
as often iteration only cares about imported or defined (non-imported) things.
Diffstat (limited to 'src/passes/Metrics.cpp')
-rw-r--r-- | src/passes/Metrics.cpp | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/passes/Metrics.cpp b/src/passes/Metrics.cpp index 81706042b..5176f8762 100644 --- a/src/passes/Metrics.cpp +++ b/src/passes/Metrics.cpp @@ -46,25 +46,26 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor< } void doWalkModule(Module* module) { + ImportInfo imports(*module); + // global things for (auto& curr : module->functionTypes) { visitFunctionType(curr.get()); } - for (auto& curr : module->imports) { - visitImport(curr.get()); - } for (auto& curr : module->exports) { visitExport(curr.get()); } - for (auto& curr : module->globals) { - walkGlobal(curr.get()); - } + ModuleUtils::iterDefinedGlobals(*module, [&](Global* curr) { + walkGlobal(curr); + }); walkTable(&module->table); walkMemory(&module->memory); + // add imports + counts["[imports]"] = imports.getNumImports(); // add functions - counts["[funcs]"] = module->functions.size(); + counts["[funcs]"] = imports.getNumDefinedFunctions(); // add memory and table if (module->memory.exists) { Index size = 0; @@ -89,14 +90,14 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor< WasmBinaryWriter writer(module, buffer); writer.write(); // print for each function - for (Index i = 0; i < module->functions.size(); i++) { - auto* func = module->functions[i].get(); + Index binaryIndex = 0; + ModuleUtils::iterDefinedFunctions(*module, [&](Function* func) { counts.clear(); walkFunction(func); counts["[vars]"] = func->getNumVars(); - counts["[binary-bytes]"] = writer.tableOfContents.functionBodies[i].size; + counts["[binary-bytes]"] = writer.tableOfContents.functionBodies[binaryIndex++].size; printCounts(std::string("func: ") + func->name.str); - } + }); // print for each export how much code size is due to it, i.e., // how much the module could shrink without it. auto sizeAfterGlobalCleanup = [](Module* module) { @@ -138,10 +139,10 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor< } else { // add function info size_t vars = 0; - for (auto& func : module->functions) { - walkFunction(func.get()); + ModuleUtils::iterDefinedFunctions(*module, [&](Function* func) { + walkFunction(func); vars += func->getNumVars(); - } + }); counts["[vars]"] = vars; // print printCounts("total"); |