diff options
author | Derek Schuff <dschuff@chromium.org> | 2016-10-14 10:27:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-14 10:27:36 -0700 |
commit | 58f7d6cd477701d7fb4c87ffa9e795ddc6423abb (patch) | |
tree | 9bda4f141e0ffffea5e3556377ca6d463ea1834a /src | |
parent | 87c3aab6500f2a3a3ca8cecfaf65cc14e407a0cd (diff) | |
download | binaryen-58f7d6cd477701d7fb4c87ffa9e795ddc6423abb.tar.gz binaryen-58f7d6cd477701d7fb4c87ffa9e795ddc6423abb.tar.bz2 binaryen-58f7d6cd477701d7fb4c87ffa9e795ddc6423abb.zip |
Import memory instead of defining/exporting it when using emscripten glue (#777)
The emscripten JS module code creates the memory using the native wasm
APIs, and imports that into the wasm module.
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/s2wasm.cpp | 3 | ||||
-rw-r--r-- | src/wasm-linker.cpp | 21 | ||||
-rw-r--r-- | src/wasm-linker.h | 27 |
3 files changed, 33 insertions, 18 deletions
diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp index 8c36a0d58..ef5d276d3 100644 --- a/src/tools/s2wasm.cpp +++ b/src/tools/s2wasm.cpp @@ -132,7 +132,8 @@ int main(int argc, const char *argv[]) { if (options.debug) std::cerr << "Global base " << globalBase << '\n'; Linker linker(globalBase, stackAllocation, initialMem, maxMem, - ignoreUnknownSymbols, startFunction, options.debug); + generateEmscriptenGlue, ignoreUnknownSymbols, startFunction, + options.debug); S2WasmBuilder mainbuilder(input.c_str(), options.debug); linker.linkObject(mainbuilder); diff --git a/src/wasm-linker.cpp b/src/wasm-linker.cpp index 014dfe6a9..56ec03851 100644 --- a/src/wasm-linker.cpp +++ b/src/wasm-linker.cpp @@ -107,14 +107,25 @@ void Linker::layout() { } else { out.wasm.memory.initial = initialMem / Memory::kPageSize; } + out.wasm.memory.exists = true; if (userMaxMemory) out.wasm.memory.max = userMaxMemory / Memory::kPageSize; - auto memoryExport = make_unique<Export>(); - memoryExport->name = MEMORY; - memoryExport->value = Name::fromInt(0); - memoryExport->kind = ExternalKind::Memory; - out.wasm.addExport(memoryExport.release()); + if (importMemory) { + auto memoryImport = make_unique<Import>(); + memoryImport->name = MEMORY; + memoryImport->module = ENV; + memoryImport->base = MEMORY; + memoryImport->kind = ExternalKind::Memory; + out.wasm.memory.imported = true; + out.wasm.addImport(memoryImport.release()); + } else { + auto memoryExport = make_unique<Export>(); + memoryExport->name = MEMORY; + memoryExport->value = Name::fromInt(0); + memoryExport->kind = ExternalKind::Memory; + out.wasm.addExport(memoryExport.release()); + } // XXX For now, export all functions marked .globl. for (Name name : out.globls) exportFunction(name, false); diff --git a/src/wasm-linker.h b/src/wasm-linker.h index 4a0e1e0b9..3660b3038 100644 --- a/src/wasm-linker.h +++ b/src/wasm-linker.h @@ -193,18 +193,18 @@ class LinkerObject { // applying the relocations, resulting in an executable wasm module. class Linker { public: - Linker(Address globalBase, Address stackAllocation, - Address userInitialMemory, Address userMaxMemory, - bool ignoreUnknownSymbols, Name startFunction, - bool debug) : - ignoreUnknownSymbols(ignoreUnknownSymbols), - startFunction(startFunction), - globalBase(globalBase), - nextStatic(globalBase), - userInitialMemory(userInitialMemory), - userMaxMemory(userMaxMemory), - stackAllocation(stackAllocation), - debug(debug) { + Linker(Address globalBase, Address stackAllocation, Address userInitialMemory, + Address userMaxMemory, bool importMemory, bool ignoreUnknownSymbols, + Name startFunction, bool debug) + : ignoreUnknownSymbols(ignoreUnknownSymbols), + startFunction(startFunction), + globalBase(globalBase), + nextStatic(globalBase), + userInitialMemory(userInitialMemory), + userMaxMemory(userMaxMemory), + importMemory(importMemory), + stackAllocation(stackAllocation), + debug(debug) { if (userMaxMemory && userMaxMemory < userInitialMemory) { Fatal() << "Specified max memory " << userMaxMemory << " is < specified initial memory " << userInitialMemory; @@ -217,6 +217,7 @@ class Linker { Fatal() << "Specified initial memory " << userInitialMemory << " is not a multiple of 64k"; } + // Don't allow anything to be allocated at address 0 if (globalBase == 0) nextStatic = 1; @@ -312,6 +313,8 @@ class Linker { Address userInitialMemory; // Initial memory size (in bytes) specified by the user. Address userMaxMemory; // Max memory size (in bytes) specified by the user. //(after linking, this is rounded and set on the wasm object in pages) + bool importMemory; // Whether the memory should be imported instead of + // defined. Address stackAllocation; bool debug; |