summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tools/asm2wasm.cpp21
-rw-r--r--src/tools/wasm-opt.cpp31
-rw-r--r--src/wasm-io.h68
-rw-r--r--src/wasm/CMakeLists.txt1
-rw-r--r--src/wasm/wasm-io.cpp86
5 files changed, 194 insertions, 13 deletions
diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp
index 9ba6062ea..c880e7459 100644
--- a/src/tools/asm2wasm.cpp
+++ b/src/tools/asm2wasm.cpp
@@ -23,6 +23,7 @@
#include "support/file.h"
#include "wasm-builder.h"
#include "wasm-printing.h"
+#include "wasm-io.h"
#include "asm2wasm.h"
@@ -34,6 +35,9 @@ int main(int argc, const char *argv[]) {
bool runOptimizationPasses = false;
bool imprecise = false;
bool wasmOnly = false;
+ bool debugInfo = false;
+ std::string symbolMap;
+ bool emitBinary = true;
Options options("asm2wasm", "Translate asm.js files to .wast files");
options
@@ -80,6 +84,15 @@ int main(int argc, const char *argv[]) {
[&wasmOnly](Options *o, const std::string &) {
wasmOnly = true;
})
+ .add("--debuginfo", "-g", "Emit names section and debug info",
+ Options::Arguments::Zero,
+ [&](Options *o, const std::string &arguments) { debugInfo = true; })
+ .add("--symbolmap", "-s", "Emit a symbol map (indexes => names)",
+ Options::Arguments::One,
+ [&](Options *o, const std::string &argument) { symbolMap = argument; })
+ .add("--emit-text", "-S", "Emit text instead of binary for the output file",
+ Options::Arguments::Zero,
+ [&](Options *o, const std::string &argument) { emitBinary = false; })
.add_positional("INFILE", Options::Arguments::One,
[](Options *o, const std::string &argument) {
o->extra["infile"] = argument;
@@ -153,8 +166,12 @@ int main(int argc, const char *argv[]) {
}
if (options.debug) std::cerr << "printing..." << std::endl;
- Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
- WasmPrinter::printModule(&wasm, output.getStream());
+ ModuleWriter writer;
+ writer.setDebug(options.debug);
+ writer.setDebugInfo(debugInfo);
+ writer.setSymbolMap(symbolMap);
+ writer.setBinary(emitBinary);
+ writer.write(wasm, options.extra["output"]);
if (options.debug) std::cerr << "done." << std::endl;
}
diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp
index 1da038b8e..93d8f5baa 100644
--- a/src/tools/wasm-opt.cpp
+++ b/src/tools/wasm-opt.cpp
@@ -27,6 +27,7 @@
#include "wasm-printing.h"
#include "wasm-s-parser.h"
#include "wasm-validator.h"
+#include "wasm-io.h"
using namespace wasm;
@@ -39,6 +40,7 @@ int main(int argc, const char* argv[]) {
std::vector<std::string> passes;
bool runOptimizationPasses = false;
PassOptions passOptions;
+ bool emitBinary = true;
Options options("wasm-opt", "Optimize .wast files");
options
@@ -49,6 +51,9 @@ int main(int argc, const char* argv[]) {
Colors::disable();
})
#include "optimization-options.h"
+ .add("--emit-text", "-S", "Emit text instead of binary for the output file",
+ Options::Arguments::Zero,
+ [&](Options *o, const std::string &argument) { emitBinary = false; })
.add_positional("INFILE", Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["infile"] = argument;
@@ -71,15 +76,17 @@ int main(int argc, const char* argv[]) {
Module wasm;
- try {
- if (options.debug) std::cerr << "s-parsing..." << std::endl;
- SExpressionParser parser(const_cast<char*>(input.c_str()));
- Element& root = *parser.root;
- if (options.debug) std::cerr << "w-parsing..." << std::endl;
- SExpressionWasmBuilder builder(wasm, *root[0]);
- } catch (ParseException& p) {
- p.dump(std::cerr);
- Fatal() << "error in parsing input";
+ {
+ if (options.debug) std::cerr << "reading...\n";
+ ModuleReader reader;
+ reader.setDebug(options.debug);
+
+ try {
+ reader.read(options.extra["infile"], wasm);
+ } catch (ParseException& p) {
+ p.dump(std::cerr);
+ Fatal() << "error in parsing input";
+ }
}
if (!WasmValidator().validate(wasm)) {
@@ -103,7 +110,9 @@ int main(int argc, const char* argv[]) {
if (options.extra.count("output") > 0) {
if (options.debug) std::cerr << "writing..." << std::endl;
- Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
- WasmPrinter::printModule(&wasm, output.getStream());
+ ModuleWriter writer;
+ writer.setDebug(options.debug);
+ writer.setBinary(emitBinary);
+ writer.write(wasm, options.extra["output"]);
}
}
diff --git a/src/wasm-io.h b/src/wasm-io.h
new file mode 100644
index 000000000..7d7358f9c
--- /dev/null
+++ b/src/wasm-io.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2017 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Abstracts reading and writing, supporting both text and binary.
+//
+
+#ifndef wasm_wasm_io_h
+#define wasm_wasm_io_h
+
+#include "wasm.h"
+
+namespace wasm {
+
+class ModuleIO {
+protected:
+ bool debug = false;
+
+public:
+ void setDebug(bool debug_) { debug = debug_; }
+};
+
+class ModuleReader : public ModuleIO {
+public:
+ // read text
+ void readText(std::string filename, Module& wasm);
+ // read binary
+ void readBinary(std::string filename, Module& wasm);
+ // read text or binary, checking the contents for what it is
+ void read(std::string filename, Module& wasm);
+};
+
+class ModuleWriter : public ModuleIO {
+ bool binary = true;
+ bool debugInfo = false;
+ std::string symbolMap;
+
+public:
+ void setBinary(bool binary_) { binary = binary_; }
+ void setDebugInfo(bool debugInfo_) { debugInfo = debugInfo_; }
+ void setSymbolMap(std::string symbolMap_) { symbolMap = symbolMap_; }
+
+ // write text
+ void writeText(Module& wasm, std::string filename);
+ // write binary
+ void writeBinary(Module& wasm, std::string filename);
+ // write text or binary, defaulting to binary unless setBinary(false),
+ // and unless there is no output file (in which case we write text
+ // to stdout).
+ void write(Module& wasm, std::string filename);
+};
+
+}
+
+#endif // wasm_wasm_io_h
diff --git a/src/wasm/CMakeLists.txt b/src/wasm/CMakeLists.txt
index 65b626c25..e1b1aac33 100644
--- a/src/wasm/CMakeLists.txt
+++ b/src/wasm/CMakeLists.txt
@@ -1,6 +1,7 @@
SET(wasm_SOURCES
wasm.cpp
wasm-binary.cpp
+ wasm-io.cpp
wasm-s-parser.cpp
)
ADD_LIBRARY(wasm STATIC ${wasm_SOURCES})
diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp
new file mode 100644
index 000000000..2380f7447
--- /dev/null
+++ b/src/wasm/wasm-io.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Abstracts reading and writing, supporting both text and binary
+// depending on the suffix.
+//
+// When the suffix is unclear, writing defaults to text (this
+// allows odd suffixes, which we use in the test suite), while
+// reading will check the magic number and default to text if not
+// binary.
+//
+
+#include "wasm-io.h"
+#include "wasm-s-parser.h"
+#include "wasm-binary.h"
+#include "support/file.h"
+
+namespace wasm {
+
+void ModuleReader::readText(std::string filename, Module& wasm) {
+ if (debug) std::cerr << "reading text from " << filename << "\n";
+ auto input(read_file<std::string>(filename, Flags::Text, debug ? Flags::Debug : Flags::Release));
+ SExpressionParser parser(const_cast<char*>(input.c_str()));
+ Element& root = *parser.root;
+ SExpressionWasmBuilder builder(wasm, *root[0]);
+}
+
+void ModuleReader::readBinary(std::string filename, Module& wasm) {
+ if (debug) std::cerr << "reading binary from " << filename << "\n";
+ auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release));
+ WasmBinaryBuilder parser(wasm, input, debug);
+ parser.read();
+}
+
+void ModuleReader::read(std::string filename, Module& wasm) {
+ // see if this is a binary
+ auto contents = read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release);
+ if (contents.size() >= 4 && contents[0] == '\0' && contents[1] == 'a' && contents[2] == 's' && contents[3] == 'm') {
+ readBinary(filename, wasm);
+ } else {
+ // default to text
+ readText(filename, wasm);
+ }
+}
+
+void ModuleWriter::writeText(Module& wasm, std::string filename) {
+ if (debug) std::cerr << "writing text to " << filename << "\n";
+ Output output(filename, Flags::Text, debug ? Flags::Debug : Flags::Release);
+ WasmPrinter::printModule(&wasm, output.getStream());
+}
+
+void ModuleWriter::writeBinary(Module& wasm, std::string filename) {
+ if (debug) std::cerr << "writing binary to " << filename << "\n";
+ BufferWithRandomAccess buffer(debug);
+ WasmBinaryWriter writer(&wasm, buffer, debug);
+ writer.setDebugInfo(debugInfo);
+ if (symbolMap.size() > 0) writer.setSymbolMap(symbolMap);
+ writer.write();
+ Output output(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release);
+ buffer.writeTo(output);
+}
+
+void ModuleWriter::write(Module& wasm, std::string filename) {
+ if (binary && filename.size() > 0) {
+ writeBinary(wasm, filename);
+ } else {
+ writeText(wasm, filename);
+ }
+}
+
+}
+