Skip to content

Commit

Permalink
wasm-emscripten-finalize: add namedGlobals to output metadata (#1979)
Browse files Browse the repository at this point in the history
This key is used by emscripten when building with MAIN_MODULE in order
to export global variables from the main module to the side modules.
  • Loading branch information
sbc100 authored Apr 4, 2019
1 parent cac81ba commit 2129cef
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ full changeset diff at the end of each section.
Current Trunk
-------------

- Add `namedGlobals` to metadata output of wasm-emscripten-finalize
- Add support for llvm PIC code.
- Add --side-module option to wasm-emscripten-finalize.

Expand Down
11 changes: 5 additions & 6 deletions src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ int main(int argc, const char *argv[]) {
WasmPrinter::printModule(&wasm, std::cerr);
}

for (const UserSection& section : wasm.userSections) {
if (section.name == BinaryConsts::UserSections::Dylink) {
isSideModule = true;
}
}

uint32_t dataSize = 0;

if (!isSideModule) {
Expand Down Expand Up @@ -187,6 +181,11 @@ int main(int argc, const char *argv[]) {
generator.generateStackInitialization(initialStackPointer);
// For side modules these gets called via __post_instantiate
if (Function* F = generator.generateAssignGOTEntriesFunction()) {
auto* ex = new Export();
ex->value = F->name;
ex->name = F->name;
ex->kind = ExternalKind::Function;
wasm.addExport(ex);
initializerFunctions.push_back(F->name);
}
if (auto* e = wasm.getExportOrNull(WASM_CALL_CTORS)) {
Expand Down
18 changes: 16 additions & 2 deletions src/wasm/wasm-emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ void EmscriptenGlueGenerator::generatePostInstantiateFunction() {
ex->name = POST_INSTANTIATE;
ex->kind = ExternalKind::Function;
wasm.addExport(ex);
wasm.updateMaps();
}

Function* EmscriptenGlueGenerator::generateMemoryGrowthFunction() {
Expand Down Expand Up @@ -897,9 +896,24 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
meta << " \"exports\": [";
commaFirst = true;
for (const auto& ex : wasm.exports) {
meta << nextElement() << '"' << ex->name.str << '"';
if (ex->kind == ExternalKind::Function) {
meta << nextElement() << '"' << ex->name.str << '"';
}
}
meta << "\n ],\n";

meta << " \"namedGlobals\": {";
commaFirst = true;
for (const auto& ex : wasm.exports) {
if (ex->kind == ExternalKind::Global) {
const Global* g = wasm.getGlobal(ex->value);
assert(g->type == i32);
Const* init = g->init->cast<Const>();
uint32_t addr = init->value.geti32();
meta << nextElement() << '"' << ex->name.str << "\" : \"" << addr << '"';
}
}
meta << "\n },\n";
}

meta << " \"invokeFuncs\": [";
Expand Down
7 changes: 4 additions & 3 deletions test/lld/duplicate_imports.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"main",
"__heap_base",
"__data_end",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66128",
"__data_end" : "581"
},
"invokeFuncs": [
"invoke_ffd"
]
Expand Down
7 changes: 4 additions & 3 deletions test/lld/em_asm.wast.mem.out
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66192",
"__data_end" : "652"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/em_asm.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66192",
"__data_end" : "652"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/em_asm_O0.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"main",
"__heap_base",
"__data_end",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66192",
"__data_end" : "652"
},
"invokeFuncs": [
]
}
Expand Down
4 changes: 3 additions & 1 deletion test/lld/em_asm_table.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@
"_dynCall_iiii"
],
"exports": [
"__data_end",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory",
"dynCall_vii",
"dynCall_iiii"
],
"namedGlobals": {
"__data_end" : "1048"
},
"invokeFuncs": [
]
}
Expand Down
6 changes: 4 additions & 2 deletions test/lld/em_js_O0.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@
"___growWasmMemory"
],
"exports": [
"__heap_base",
"__data_end",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "5250112",
"__data_end" : "7232"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/hello_world.wast.mem.out
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66128",
"__data_end" : "581"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/hello_world.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66128",
"__data_end" : "581"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/init.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66112",
"__data_end" : "576"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/recursive.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@
"___growWasmMemory"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory"
],
"namedGlobals": {
"__heap_base" : "66128",
"__data_end" : "587"
},
"invokeFuncs": [
]
}
Expand Down
7 changes: 4 additions & 3 deletions test/lld/reserved_func_ptr.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,18 @@
"_dynCall_viii"
],
"exports": [
"memory",
"__wasm_call_ctors",
"__heap_base",
"__data_end",
"main",
"stackSave",
"stackAlloc",
"stackRestore",
"__growWasmMemory",
"dynCall_viii"
],
"namedGlobals": {
"__heap_base" : "66112",
"__data_end" : "568"
},
"invokeFuncs": [
]
}
Expand Down
2 changes: 2 additions & 0 deletions test/lld/shared.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
"print_message",
"__post_instantiate"
],
"namedGlobals": {
},
"invokeFuncs": [
]
}
Expand Down

0 comments on commit 2129cef

Please sign in to comment.