summaryrefslogtreecommitdiff
path: root/src/tools/s2wasm.cpp
diff options
context:
space:
mode:
authorJacob Gravelle <jgravelle@google.com>2018-03-08 13:47:00 -0800
committerGitHub <noreply@github.com>2018-03-08 13:47:00 -0800
commit073d3707cb5980a76161105e421034e85ac0f8ce (patch)
tree5dd18cf5d947d07e97301e59a84d86c6cc52cba4 /src/tools/s2wasm.cpp
parentbe9243419302aca1ff088004546205f1c252f9c5 (diff)
downloadbinaryen-073d3707cb5980a76161105e421034e85ac0f8ce.tar.gz
binaryen-073d3707cb5980a76161105e421034e85ac0f8ce.tar.bz2
binaryen-073d3707cb5980a76161105e421034e85ac0f8ce.zip
Let s2wasm emit binary output (#1465)
* Emit invokeFuncs list as metadata * Refactor s2wasm to use ModuleWriter * Fix wasm-emscripten-finalize metadata output for binary output * Add a flag to emit binary from s2wasm NOTE: I chose to emit text by default, and binary behind a flag. This mismatches with asm2wasm (and the expectations of users of a "2wasm" tool), but doesn't break any existing users of s2wasm. If s2wasm is deprecated in favor of lld, this will be the least disruptive change, and we won't have to live with awkward defaults for too long. * Emit source maps in the binary output of s2wasm * Only emit binary with an outfile specified
Diffstat (limited to 'src/tools/s2wasm.cpp')
-rw-r--r--src/tools/s2wasm.cpp77
1 files changed, 68 insertions, 9 deletions
diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp
index a2c4e4a38..dc6b1f38f 100644
--- a/src/tools/s2wasm.cpp
+++ b/src/tools/s2wasm.cpp
@@ -26,6 +26,7 @@
#include "support/file.h"
#include "s2wasm.h"
#include "wasm-emscripten.h"
+#include "wasm-io.h"
#include "wasm-linker.h"
#include "wasm-printing.h"
#include "wasm-validator.h"
@@ -38,7 +39,12 @@ int main(int argc, const char *argv[]) {
bool generateEmscriptenGlue = false;
bool allowMemoryGrowth = false;
bool importMemory = false;
+ bool emitBinary = false;
+ bool debugInfo = false;
std::string startFunction;
+ std::string sourceMapFilename;
+ std::string sourceMapUrl;
+ std::string symbolMap;
std::vector<std::string> archiveLibraries;
TrapMode trapMode = TrapMode::Allow;
unsigned numReservedFunctionPointers = 0;
@@ -61,7 +67,7 @@ int main(int argc, const char *argv[]) {
[&startFunction](Options *, const std::string& argument) {
startFunction = argument.size() ? argument : "main";
})
- .add("--global-base", "-g", "Where to start to place globals",
+ .add("--global-base", "", "Where to start to place globals",
Options::Arguments::One,
[](Options *o, const std::string& argument) {
o->extra["global-base"] = argument;
@@ -130,12 +136,47 @@ int main(int argc, const char *argv[]) {
const std::string &argument) {
numReservedFunctionPointers = std::stoi(argument);
})
+ .add("--emit-binary", "",
+ "Emit binary instead of text for the output file",
+ Options::Arguments::Zero,
+ [&emitBinary](Options *, const std::string &) {
+ emitBinary = true;
+ })
+ .add("--debuginfo", "-g",
+ "Emit names section in wasm binary (or full debuginfo in wast)",
+ Options::Arguments::Zero,
+ [&debugInfo](Options *, const std::string &) {
+ debugInfo = true;
+ })
+ .add("--source-map", "-sm",
+ "Emit source map (if using binary output) to the specified file",
+ Options::Arguments::One,
+ [&sourceMapFilename](Options *, const std::string& argument) {
+ sourceMapFilename = argument;
+ })
+ .add("--source-map-url", "-su",
+ "Use specified string as source map URL",
+ Options::Arguments::One,
+ [&sourceMapUrl](Options *, const std::string& argument) {
+ sourceMapUrl = argument;
+ })
+ .add("--symbolmap", "-s",
+ "Emit a symbol map (indexes => names)",
+ Options::Arguments::One,
+ [&symbolMap](Options *, const std::string& argument) {
+ symbolMap = argument;
+ })
.add_positional("INFILE", Options::Arguments::One,
[](Options *o, const std::string& argument) {
o->extra["infile"] = argument;
});
options.parse(argc, argv);
+ if (options.extra["output"].size() == 0) {
+ // when no output file is specified, we emit text to stdout
+ emitBinary = false;
+ }
+
if (allowMemoryGrowth && !generateEmscriptenGlue) {
Fatal() << "Error: adding memory growth code without Emscripten glue. "
"This doesn't do anything.\n";
@@ -187,13 +228,13 @@ int main(int argc, const char *argv[]) {
linker.layout();
std::string metadata;
+ Module& wasm = linker.getOutput().wasm;
if (generateEmscriptenGlue) {
- Module& wasm = linker.getOutput().wasm;
if (options.debug) {
std::cerr << "Emscripten gluing..." << std::endl;
WasmPrinter::printModule(&wasm, std::cerr);
}
- metadata = ";; METADATA: " + emscriptenGlue(
+ metadata = emscriptenGlue(
wasm,
allowMemoryGrowth,
linker.getStackPointerAddress(),
@@ -204,18 +245,36 @@ int main(int argc, const char *argv[]) {
if (options.extra["validate"] != "none") {
if (options.debug) std::cerr << "Validating..." << std::endl;
- Module* output = &linker.getOutput().wasm;
- if (!wasm::WasmValidator().validate(*output,
+ if (!wasm::WasmValidator().validate(wasm,
WasmValidator::Globally | (options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
- WasmPrinter::printModule(output);
+ WasmPrinter::printModule(&wasm);
Fatal() << "Error: linked module is not valid.\n";
}
}
if (options.debug) std::cerr << "Printing..." << std::endl;
- Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
- WasmPrinter::printModule(&linker.getOutput().wasm, output.getStream());
- output << metadata;
+ auto outputDebugFlag = options.debug ? Flags::Debug : Flags::Release;
+ auto outputBinaryFlag = emitBinary ? Flags::Binary : Flags::Text;
+ Output output(options.extra["output"], outputBinaryFlag, outputDebugFlag);
+
+ ModuleWriter writer;
+ writer.setDebug(options.debug);
+ writer.setDebugInfo(debugInfo);
+ writer.setSymbolMap(symbolMap);
+ writer.setBinary(emitBinary);
+ if (emitBinary) {
+ writer.setSourceMapFilename(sourceMapFilename);
+ writer.setSourceMapUrl(sourceMapUrl);
+ }
+ writer.write(wasm, output);
+
+ if (generateEmscriptenGlue) {
+ if (emitBinary) {
+ std::cout << metadata;
+ } else {
+ output << ";; METADATA: " << metadata;
+ }
+ }
if (options.debug) std::cerr << "Done." << std::endl;
return 0;