diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/Print.cpp | 6 | ||||
-rw-r--r-- | src/support/name.h | 7 | ||||
-rw-r--r-- | src/tools/wasm-merge.cpp | 34 |
3 files changed, 34 insertions, 13 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 2f7588b37..cfdfc09dd 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -741,20 +741,18 @@ struct PrintSExpression : public Visitor<PrintSExpression> { printTableHeader(curr); o << maybeNewLine; } - if (curr->segments.empty()) return; - doIndent(o, indent); for (auto& segment : curr->segments) { // Don't print empty segments if (segment.data.empty()) continue; + doIndent(o, indent); printOpening(o, "elem ", true); visit(segment.offset); for (auto name : segment.data) { o << ' '; printName(name); } - o << ')'; + o << ")\n"; } - o << maybeNewLine; } void printMemoryHeader(Memory* curr) { printOpening(o, "memory") << ' '; diff --git a/src/support/name.h b/src/support/name.h index b77397555..ae01db787 100644 --- a/src/support/name.h +++ b/src/support/name.h @@ -40,8 +40,11 @@ struct Name : public cashew::IString { Name(const std::string& str) : cashew::IString(str.c_str(), false) {} friend std::ostream& operator<<(std::ostream& o, Name name) { - assert(name.str); - return o << '$' << name.str; // reference interpreter requires we prefix all names + if (name.str) { + return o << '$' << name.str; // reference interpreter requires we prefix all names + } else { + return o << "(null Name)"; + } } static Name fromInt(size_t i) { diff --git a/src/tools/wasm-merge.cpp b/src/tools/wasm-merge.cpp index 7a713675f..7fa5af137 100644 --- a/src/tools/wasm-merge.cpp +++ b/src/tools/wasm-merge.cpp @@ -123,13 +123,28 @@ struct Mergeable { memoryBaseGlobals.insert(name); }); if (memoryBaseGlobals.size() == 0) { - Fatal() << "no memory base was imported"; + // add one + auto* import = new Import; + import->name = MEMORY_BASE; + import->module = ENV; + import->base = MEMORY_BASE; + import->kind = ExternalKind::Global; + import->globalType = i32; + wasm.addImport(import); + memoryBaseGlobals.insert(import->name); } findImportsByBase(wasm, TABLE_BASE, [&](Name name) { tableBaseGlobals.insert(name); }); if (tableBaseGlobals.size() == 0) { - Fatal() << "no table base was imported"; + auto* import = new Import; + import->name = TABLE_BASE; + import->module = ENV; + import->base = TABLE_BASE; + import->kind = ExternalKind::Global; + import->globalType = i32; + wasm.addImport(import); + tableBaseGlobals.insert(import->name); } } @@ -169,7 +184,8 @@ struct Mergeable { // ensure a relocatable segment exists, of the proper size, including // the dylink bump applied into it, standardized into the form of // not using a dylink section and instead having enough zeros at - // the end. this makes linking much simpler. + // the end. this makes linking much simpler.ta + // there may be other non-relocatable segments too. template<typename T, typename U, typename Segment> void standardizeSegment(Module& wasm, T& what, Index size, U zero, Name globalName) { Segment* relocatable = nullptr; @@ -194,9 +210,10 @@ struct Mergeable { ensureSize(what, relocatable->data.size()); } - // copies a relocatable segment from the input to the output + // copies a relocatable segment from the input to the output, and + // copies the non-relocatable ones as well template<typename T, typename V> - void copySegment(T& output, T& input, V updater) { + void copySegments(T& output, T& input, V updater) { for (auto& inputSegment : input.segments) { Expression* inputOffset = inputSegment.offset; if (inputOffset->is<GetGlobal>()) { @@ -213,6 +230,9 @@ struct Mergeable { } } WASM_UNREACHABLE(); // we must find a relocatable one in the output, as we standardized + } else { + // this is a non-relocatable one. just copy it. + output.segments.push_back(inputSegment); } } } @@ -405,8 +425,8 @@ struct InputMergeable : public ExpressionStackWalker<InputMergeable, Visitor<Inp } // memory&table: we place the new memory segments at a higher position. after the existing ones. - copySegment(outputMergeable.wasm.memory, wasm.memory, [](char x) -> char { return x; }); - copySegment(outputMergeable.wasm.table, wasm.table, [&](Name x) -> Name { return fNames[x]; }); + copySegments(outputMergeable.wasm.memory, wasm.memory, [](char x) -> char { return x; }); + copySegments(outputMergeable.wasm.table, wasm.table, [&](Name x) -> Name { return fNames[x]; }); // update the new contents about to be merged in walkModule(&wasm); |