diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm-as.cpp | 29 | ||||
-rw-r--r-- | src/tools/wasm-dis.cpp | 16 | ||||
-rw-r--r-- | src/tools/wasm-opt.cpp | 18 | ||||
-rw-r--r-- | src/wasm-io.h | 7 | ||||
-rw-r--r-- | src/wasm/wasm-io.cpp | 20 |
5 files changed, 51 insertions, 39 deletions
diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index b01bae0e3..46885dec0 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -21,8 +21,9 @@ #include "support/colors.h" #include "support/command-line.h" #include "support/file.h" -#include "wasm-binary.h" +#include "wasm-io.h" #include "wasm-s-parser.h" +#include "wasm-validator.h" #include "tool-utils.h" @@ -99,26 +100,18 @@ int main(int argc, const char *argv[]) { } } - if (options.debug) std::cerr << "binarification..." << std::endl; - BufferWithRandomAccess buffer(options.debug); - WasmBinaryWriter writer(&wasm, buffer, options.debug); - // if debug info is used, then we want to emit the names section - writer.setNamesSection(debugInfo); - std::unique_ptr<std::ofstream> sourceMapStream = nullptr; + if (options.debug) std::cerr << "writing..." << std::endl; + ModuleWriter writer; + writer.setBinary(true); + writer.setDebugInfo(debugInfo); if (sourceMapFilename.size()) { - sourceMapStream = make_unique<std::ofstream>(); - sourceMapStream->open(sourceMapFilename); - writer.setSourceMap(sourceMapStream.get(), sourceMapUrl); + writer.setSourceMapFilename(sourceMapFilename); + writer.setSourceMapUrl(sourceMapUrl); } - if (symbolMap.size() > 0) writer.setSymbolMap(symbolMap); - writer.write(); - - if (options.debug) std::cerr << "writing to output..." << std::endl; - Output output(options.extra["output"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release); - buffer.writeTo(output); - if (sourceMapStream) { - sourceMapStream->close(); + if (symbolMap.size() > 0) { + writer.setSymbolMap(symbolMap); } + writer.write(wasm, options.extra["output"]); if (options.debug) std::cerr << "Done." << std::endl; } diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index da1a56b6b..3ff6819a5 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -21,7 +21,7 @@ #include "support/colors.h" #include "support/command-line.h" #include "support/file.h" -#include "wasm-binary.h" +#include "wasm-io.h" #include "wasm-printing.h" using namespace cashew; @@ -45,22 +45,10 @@ int main(int argc, const char *argv[]) { }); options.parse(argc, argv); - auto input(read_file<std::vector<char>>(options.extra["infile"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release)); - if (options.debug) std::cerr << "parsing binary..." << std::endl; Module wasm; try { - std::unique_ptr<std::ifstream> sourceMapStream; - WasmBinaryBuilder parser(wasm, input, options.debug); - if (sourceMapFilename.size()) { - sourceMapStream = make_unique<std::ifstream>(); - sourceMapStream->open(sourceMapFilename); - parser.setDebugLocations(sourceMapStream.get()); - } - parser.read(); - if (sourceMapStream) { - sourceMapStream->close(); - } + ModuleReader().readBinary(options.extra["infile"], wasm, sourceMapFilename); } catch (ParseException& p) { p.dump(std::cerr); std::cerr << '\n'; diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 7d7d44b2b..43496b34e 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -72,6 +72,9 @@ int main(int argc, const char* argv[]) { bool fuzzPasses = false; std::string emitJSWrapper; std::string emitSpecWrapper; + std::string inputSourceMapFilename; + std::string outputSourceMapFilename; + std::string outputSourceMapUrl; OptimizationOptions options("wasm-opt", "Read, write, and optimize files"); options @@ -111,6 +114,15 @@ int main(int argc, const char* argv[]) { .add("--emit-spec-wrapper", "-esw", "Emit a wasm spec interpreter wrapper file that can run the wasm with some test values, useful for fuzzing", Options::Arguments::One, [&](Options *o, const std::string& arguments) { emitSpecWrapper = arguments; }) + .add("--input-source-map", "-ism", "Consume source map from the specified file", + Options::Arguments::One, + [&inputSourceMapFilename](Options *o, const std::string& argument) { inputSourceMapFilename = argument; }) + .add("--output-source-map", "-osm", "Emit source map to the specified file", + Options::Arguments::One, + [&outputSourceMapFilename](Options *o, const std::string& argument) { outputSourceMapFilename = argument; }) + .add("--output-source-map-url", "-osu", "Emit specified string as source map URL", + Options::Arguments::One, + [&outputSourceMapUrl](Options *o, const std::string& argument) { outputSourceMapUrl = argument; }) .add_positional("INFILE", Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["infile"] = argument; @@ -128,7 +140,7 @@ int main(int argc, const char* argv[]) { ModuleReader reader; reader.setDebug(options.debug); try { - reader.read(options.extra["infile"], wasm); + reader.read(options.extra["infile"], wasm, inputSourceMapFilename); } catch (ParseException& p) { p.dump(std::cerr); std::cerr << '\n'; @@ -256,6 +268,10 @@ int main(int argc, const char* argv[]) { writer.setDebug(options.debug); writer.setBinary(emitBinary); writer.setDebugInfo(debugInfo); + if (outputSourceMapFilename.size()) { + writer.setSourceMapFilename(outputSourceMapFilename); + writer.setSourceMapUrl(outputSourceMapUrl); + } writer.write(*curr, options.extra["output"]); if (extraFuzzCommand.size() > 0) { diff --git a/src/wasm-io.h b/src/wasm-io.h index cbb8f36c8..2dfdbae6c 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -40,10 +40,11 @@ public: // read text void readText(std::string filename, Module& wasm); // read binary - void readBinary(std::string filename, Module& wasm); + void readBinary(std::string filename, Module& wasm, + std::string sourceMapFilename=""); // read text or binary, checking the contents for what it is - void read(std::string filename, Module& wasm); - + void read(std::string filename, Module& wasm, + std::string sourceMapFilename=""); // check whether a file is a wasm binary bool isBinaryFile(std::string filename); }; diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index d7ba4cf25..95ffe6e89 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -38,11 +38,21 @@ void ModuleReader::readText(std::string filename, Module& wasm) { SExpressionWasmBuilder builder(wasm, *root[0]); } -void ModuleReader::readBinary(std::string filename, Module& wasm) { +void ModuleReader::readBinary(std::string filename, Module& wasm, + std::string sourceMapFilename) { if (debug) std::cerr << "reading binary from " << filename << "\n"; auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release)); + std::unique_ptr<std::ifstream> sourceMapStream; WasmBinaryBuilder parser(wasm, input, debug); + if (sourceMapFilename.size()) { + sourceMapStream = make_unique<std::ifstream>(); + sourceMapStream->open(sourceMapFilename); + parser.setDebugLocations(sourceMapStream.get()); + } parser.read(); + if (sourceMapStream) { + sourceMapStream->close(); + } } bool ModuleReader::isBinaryFile(std::string filename) { @@ -55,11 +65,15 @@ bool ModuleReader::isBinaryFile(std::string filename) { return buffer[0] == '\0' && buffer[1] == 'a' && buffer[2] == 's' && buffer[3] == 'm'; } -void ModuleReader::read(std::string filename, Module& wasm) { +void ModuleReader::read(std::string filename, Module& wasm, + std::string sourceMapFilename) { if (isBinaryFile(filename)) { - readBinary(filename, wasm); + readBinary(filename, wasm, sourceMapFilename); } else { // default to text + if (sourceMapFilename.size()) { + std::cerr << "Binaryen ModuleReader::read() - source map filename provided, but file appears to not be binary\n"; + } readText(filename, wasm); } } |