diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ast/module-utils.h | 59 | ||||
-rw-r--r-- | src/passes/Print.cpp | 11 | ||||
-rw-r--r-- | src/wasm-binary.h | 4 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 32 |
4 files changed, 76 insertions, 30 deletions
diff --git a/src/ast/module-utils.h b/src/ast/module-utils.h new file mode 100644 index 000000000..11a5a3530 --- /dev/null +++ b/src/ast/module-utils.h @@ -0,0 +1,59 @@ +/* + * Copyright 2017 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef wasm_ast_module_h +#define wasm_ast_module_h + +#include "wasm.h" + +namespace wasm { + +namespace ModuleUtils { + +// Computes the indexes in a wasm binary, i.e., with function imports +// and function implementations sharing a single index space, etc. +struct BinaryIndexes { + std::unordered_map<Name, Index> functionIndexes; + std::unordered_map<Name, Index> globalIndexes; + + BinaryIndexes(Module& wasm) { + for (Index i = 0; i < wasm.imports.size(); i++) { + auto& import = wasm.imports[i]; + if (import->kind == ExternalKind::Function) { + auto index = functionIndexes.size(); + functionIndexes[import->name] = index; + } else if (import->kind == ExternalKind::Global) { + auto index = globalIndexes.size(); + globalIndexes[import->name] = index; + } + } + for (Index i = 0; i < wasm.functions.size(); i++) { + auto index = functionIndexes.size(); + functionIndexes[wasm.functions[i]->name] = index; + } + for (Index i = 0; i < wasm.globals.size(); i++) { + auto index = globalIndexes.size(); + globalIndexes[wasm.globals[i]->name] = index; + } + } +}; + +} // namespace ModuleUtils + +} // namespace wasm + +#endif // wasm_ast_module_h + diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 6d40034fc..beae693d9 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -22,6 +22,7 @@ #include <wasm-printing.h> #include <pass.h> #include <pretty_printing.h> +#include <ast/module-utils.h> namespace wasm { @@ -47,6 +48,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> { Function* currFunction = nullptr; Function::DebugLocation lastPrintedLocation; + std::unordered_map<Name, Index> functionIndexes; + PrintSExpression(std::ostream& o) : o(o) { setMinify(false); if (!full) full = isFullForced(); @@ -686,6 +689,14 @@ struct PrintSExpression : public Visitor<PrintSExpression> { lastPrintedLocation = { 0, 0, 0 }; printOpening(o, "func ", true); printName(curr->name); + if (currModule && !minify) { + // emit the function index in a comment + if (functionIndexes.empty()) { + ModuleUtils::BinaryIndexes indexes(*currModule); + functionIndexes = std::move(indexes.functionIndexes); + } + o << " (; " << functionIndexes[curr->name] << " ;)"; + } if (curr->type.is()) { o << maybeSpace << "(type " << curr->type << ')'; } diff --git a/src/wasm-binary.h b/src/wasm-binary.h index bf1cb14d6..bcf6cf45f 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -696,8 +696,8 @@ public: void writeExports(); void writeDataSegments(); - std::map<Name, Index> mappedFunctions; // name of the Function => index. first imports, then internals - std::map<Name, uint32_t> mappedGlobals; // name of the Global => index. first imported globals, then internal globals + std::unordered_map<Name, Index> mappedFunctions; // name of the Function => index. first imports, then internals + std::unordered_map<Name, uint32_t> mappedGlobals; // name of the Global => index. first imported globals, then internal globals uint32_t getFunctionIndex(Name name); uint32_t getGlobalIndex(Name name); diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 6c79a9032..bd0684d28 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -20,6 +20,7 @@ #include "support/bits.h" #include "wasm-binary.h" #include "ast/branch-utils.h" +#include "ast/module-utils.h" namespace wasm { @@ -31,6 +32,9 @@ void WasmBinaryWriter::prepare() { } // TODO: depending on upstream flux https://github.com/WebAssembly/spec/pull/301 might want this: assert(!func->type.isNull()); } + ModuleUtils::BinaryIndexes indexes(*wasm); + mappedFunctions = std::move(indexes.functionIndexes); + mappedGlobals = std::move(indexes.globalIndexes); } void WasmBinaryWriter::write() { @@ -343,39 +347,11 @@ void WasmBinaryWriter::writeDataSegments() { } uint32_t WasmBinaryWriter::getFunctionIndex(Name name) { - if (!mappedFunctions.size()) { - // Create name => index mapping. - for (auto& import : wasm->imports) { - if (import->kind != ExternalKind::Function) continue; - assert(mappedFunctions.count(import->name) == 0); - auto index = mappedFunctions.size(); - mappedFunctions[import->name] = index; - } - for (size_t i = 0; i < wasm->functions.size(); i++) { - assert(mappedFunctions.count(wasm->functions[i]->name) == 0); - auto index = mappedFunctions.size(); - mappedFunctions[wasm->functions[i]->name] = index; - } - } assert(mappedFunctions.count(name)); return mappedFunctions[name]; } uint32_t WasmBinaryWriter::getGlobalIndex(Name name) { - if (!mappedGlobals.size()) { - // Create name => index mapping. - for (auto& import : wasm->imports) { - if (import->kind != ExternalKind::Global) continue; - assert(mappedGlobals.count(import->name) == 0); - auto index = mappedGlobals.size(); - mappedGlobals[import->name] = index; - } - for (size_t i = 0; i < wasm->globals.size(); i++) { - assert(mappedGlobals.count(wasm->globals[i]->name) == 0); - auto index = mappedGlobals.size(); - mappedGlobals[wasm->globals[i]->name] = index; - } - } assert(mappedGlobals.count(name)); return mappedGlobals[name]; } |