summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tools/wasm-emscripten-finalize.cpp16
-rw-r--r--src/wasm-binary.h4
-rw-r--r--src/wasm-io.h7
-rw-r--r--src/wasm/wasm-binary.cpp12
-rw-r--r--src/wasm/wasm-io.cpp1
5 files changed, 36 insertions, 4 deletions
diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp
index fc55a8d83..8e37d60f4 100644
--- a/src/tools/wasm-emscripten-finalize.cpp
+++ b/src/tools/wasm-emscripten-finalize.cpp
@@ -203,9 +203,21 @@ int main(int argc, const char* argv[]) {
Fatal() << "Need to specify an infile\n";
}
+ // We will write the modified wasm if the user asked us to, either by
+ // specifying an output file or requesting text output (which goes to stdout
+ // by default).
+ auto writeOutput = outfile.size() > 0 || !emitBinary;
+
Module wasm;
ModuleReader reader;
reader.setDWARF(DWARF);
+ if (!writeOutput) {
+ // If we are not writing the output then all we are doing is simple parsing
+ // of metadata from global parts of the wasm such as imports and exports. In
+ // that case, it is unnecessary to parse function contents which are the
+ // great bulk of the work, and we can skip all that.
+ reader.setSkipFunctionBodies(true);
+ }
try {
reader.read(infile, wasm, inputSourceMapFilename);
} catch (ParseException& p) {
@@ -303,9 +315,7 @@ int main(int argc, const char* argv[]) {
BYN_TRACE_WITH_TYPE("emscripten-dump", "Module after:\n");
BYN_DEBUG_WITH_TYPE("emscripten-dump", std::cerr << wasm << '\n');
- // Write the modified wasm if the user asked us to, either by specifying an
- // output file, or requesting text output (which goes to stdout by default).
- if (outfile.size() > 0 || !emitBinary) {
+ if (writeOutput) {
Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
ModuleWriter writer;
writer.setDebugInfo(debugInfo);
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 326c37bf0..453bcb9af 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1307,6 +1307,7 @@ class WasmBinaryBuilder {
std::istream* sourceMap;
std::pair<uint32_t, Function::DebugLocation> nextDebugLocation;
bool DWARF = false;
+ bool skipFunctionBodies = false;
size_t pos = 0;
Index startIndex = -1;
@@ -1324,6 +1325,9 @@ public:
nextDebugLocation(0, {0, 0, 0}), debugLocation() {}
void setDWARF(bool value) { DWARF = value; }
+ void setSkipFunctionBodies(bool skipFunctionBodies_) {
+ skipFunctionBodies = skipFunctionBodies_;
+ }
void read();
void readUserSection(size_t payloadLen);
diff --git a/src/wasm-io.h b/src/wasm-io.h
index fbc8947c6..04fe90b51 100644
--- a/src/wasm-io.h
+++ b/src/wasm-io.h
@@ -35,6 +35,11 @@ public:
void setProfile(IRProfile profile_) { profile = profile_; }
+ // TODO: add support for this in the text format as well
+ void setSkipFunctionBodies(bool skipFunctionBodies_) {
+ skipFunctionBodies = skipFunctionBodies_;
+ }
+
// read text
void readText(std::string filename, Module& wasm);
// read binary
@@ -53,6 +58,8 @@ private:
IRProfile profile = IRProfile::Normal;
+ bool skipFunctionBodies = false;
+
void readStdin(Module& wasm, std::string sourceMapFilename);
void readBinaryData(std::vector<char>& input,
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 1dac17e93..52aa4a6e0 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -2137,7 +2137,17 @@ void WasmBinaryBuilder::readFunctions() {
assert(controlFlowStack.empty());
assert(letStack.empty());
assert(depth == 0);
- func->body = getBlockOrSingleton(func->sig.results);
+ if (!skipFunctionBodies) {
+ func->body = getBlockOrSingleton(func->sig.results);
+ } else {
+ // When skipping the function body we need to put something valid in
+ // their place so we validate. An unreachable is always acceptable
+ // there.
+ func->body = Builder(wasm).makeUnreachable();
+
+ // Skip reading the contents.
+ pos = endOfFunction;
+ }
assert(depth == 0);
assert(breakStack.empty());
assert(breakTargetNames.empty());
diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp
index 2fb4af838..f2c5af6db 100644
--- a/src/wasm/wasm-io.cpp
+++ b/src/wasm/wasm-io.cpp
@@ -51,6 +51,7 @@ void ModuleReader::readBinaryData(std::vector<char>& input,
std::unique_ptr<std::ifstream> sourceMapStream;
WasmBinaryBuilder parser(wasm, input);
parser.setDWARF(DWARF);
+ parser.setSkipFunctionBodies(skipFunctionBodies);
if (sourceMapFilename.size()) {
sourceMapStream = make_unique<std::ifstream>();
sourceMapStream->open(sourceMapFilename);