diff options
author | Alon Zakai <alonzakai@gmail.com> | 2017-01-05 15:33:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-05 15:33:55 -0800 |
commit | 90e06072f321507e61120bed891642f191c63547 (patch) | |
tree | 2b792535710be22156c0f4e8c523dca54ec07d09 /src | |
parent | 4935b96a60fc2d45dd5229acb9c784f278c37b78 (diff) | |
parent | 892e6524c147c8ff21e68556efee799a9c67f63c (diff) | |
download | binaryen-90e06072f321507e61120bed891642f191c63547.tar.gz binaryen-90e06072f321507e61120bed891642f191c63547.tar.bz2 binaryen-90e06072f321507e61120bed891642f191c63547.zip |
Merge pull request #863 from WebAssembly/linking-fix
Linking cleanups
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 33 | ||||
-rw-r--r-- | src/passes/Print.cpp | 8 |
2 files changed, 23 insertions, 18 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 6ed13b79a..fe9bd0f3f 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -649,19 +649,24 @@ void Asm2WasmBuilder::processAsm(Ref ast) { type = WasmType::f64; } if (type != WasmType::none) { - // we need imported globals to be mutable, but wasm doesn't support that yet, so we must - // import an immutable and create a mutable global initialized to its value - import->name = Name(std::string(import->name.str) + "$asm2wasm$import"); + // this is a global import->kind = ExternalKind::Global; import->globalType = type; mappedGlobals.emplace(name, type); - { - auto global = new Global(); - global->name = name; - global->type = type; - global->init = builder.makeGetGlobal(import->name, type); - global->mutable_ = true; - wasm.addGlobal(global); + // tableBase and memoryBase are used as segment/element offsets, and must be constant; + // otherwise, an asm.js import of a constant is mutable, e.g. STACKTOP + if (name != "tableBase" && name != "memoryBase") { + // we need imported globals to be mutable, but wasm doesn't support that yet, so we must + // import an immutable and create a mutable global initialized to its value + import->name = Name(std::string(import->name.str) + "$asm2wasm$import"); + { + auto global = new Global(); + global->name = name; + global->type = type; + global->init = builder.makeGetGlobal(import->name, type); + global->mutable_ = true; + wasm.addGlobal(global); + } } } else { import->kind = ExternalKind::Function; @@ -1059,8 +1064,8 @@ void Asm2WasmBuilder::processAsm(Ref ast) { wasm.table.exists = true; wasm.table.imported = true; - // Import memory offset - { + // Import memory offset, if not already there + if (!wasm.checkImport("memoryBase") && !wasm.checkGlobal("memoryBase")) { auto* import = new Import; import->name = Name("memoryBase"); import->module = Name("env"); @@ -1070,8 +1075,8 @@ void Asm2WasmBuilder::processAsm(Ref ast) { wasm.addImport(import); } - // Import table offset - { + // Import table offset, if not already there + if (!wasm.checkImport("tableBase") && !wasm.checkGlobal("tableBase")) { auto* import = new Import; import->name = Name("tableBase"); import->module = Name("env"); diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index c6fca0256..3c847809f 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -698,15 +698,15 @@ struct PrintSExpression : public Visitor<PrintSExpression> { visitImport(child.get()); o << maybeNewLine; } - if (curr->table.exists) { - visitTable(&curr->table); // Prints its own newlines - } - visitMemory(&curr->memory); for (auto& child : curr->globals) { doIndent(o, indent); visitGlobal(child.get()); o << maybeNewLine; } + if (curr->table.exists) { + visitTable(&curr->table); // Prints its own newlines + } + visitMemory(&curr->memory); for (auto& child : curr->exports) { doIndent(o, indent); visitExport(child.get()); |