summaryrefslogtreecommitdiff
path: root/src/tools/wasm-as.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/wasm-as.cpp')
-rw-r--r--src/tools/wasm-as.cpp115
1 files changed, 73 insertions, 42 deletions
diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp
index 20e51e31f..253a93b3b 100644
--- a/src/tools/wasm-as.cpp
+++ b/src/tools/wasm-as.cpp
@@ -30,61 +30,87 @@
using namespace cashew;
using namespace wasm;
-int main(int argc, const char *argv[]) {
+int main(int argc, const char* argv[]) {
bool debugInfo = false;
std::string symbolMap;
std::string sourceMapFilename;
std::string sourceMapUrl;
- ToolOptions options("wasm-as", "Assemble a .wast (WebAssembly text format) into a .wasm (WebAssembly binary format)");
+ ToolOptions options("wasm-as",
+ "Assemble a .wast (WebAssembly text format) into a .wasm "
+ "(WebAssembly binary format)");
options.extra["validate"] = "wasm";
options
- .add("--output", "-o", "Output file (stdout if not specified)",
- Options::Arguments::One,
- [](Options *o, const std::string& argument) {
- o->extra["output"] = argument;
- Colors::disable();
- })
- .add("--validate", "-v", "Control validation of the output module",
- Options::Arguments::One,
- [](Options *o, const std::string& argument) {
- if (argument != "web" && argument != "none" && argument != "wasm") {
- std::cerr << "Valid arguments for --validate flag are 'wasm', 'web', and 'none'.\n";
- exit(1);
- }
- o->extra["validate"] = argument;
- })
- .add("--debuginfo", "-g", "Emit names section and debug info",
- Options::Arguments::Zero,
- [&](Options *o, const std::string& arguments) { debugInfo = true; })
- .add("--source-map", "-sm", "Emit source map to the specified file",
- Options::Arguments::One,
- [&sourceMapFilename](Options *o, const std::string& argument) { sourceMapFilename = argument; })
- .add("--source-map-url", "-su", "Use specified string as source map URL",
- Options::Arguments::One,
- [&sourceMapUrl](Options *o, const std::string& argument) { sourceMapUrl = argument; })
- .add("--symbolmap", "-s", "Emit a symbol map (indexes => names)",
- Options::Arguments::One,
- [&](Options *o, const std::string& argument) { symbolMap = argument; })
- .add_positional("INFILE", Options::Arguments::One,
- [](Options *o, const std::string& argument) {
- o->extra["infile"] = argument;
- });
+ .add("--output",
+ "-o",
+ "Output file (stdout if not specified)",
+ Options::Arguments::One,
+ [](Options* o, const std::string& argument) {
+ o->extra["output"] = argument;
+ Colors::disable();
+ })
+ .add("--validate",
+ "-v",
+ "Control validation of the output module",
+ Options::Arguments::One,
+ [](Options* o, const std::string& argument) {
+ if (argument != "web" && argument != "none" && argument != "wasm") {
+ std::cerr << "Valid arguments for --validate flag are 'wasm', "
+ "'web', and 'none'.\n";
+ exit(1);
+ }
+ o->extra["validate"] = argument;
+ })
+ .add("--debuginfo",
+ "-g",
+ "Emit names section and debug info",
+ Options::Arguments::Zero,
+ [&](Options* o, const std::string& arguments) { debugInfo = true; })
+ .add("--source-map",
+ "-sm",
+ "Emit source map to the specified file",
+ Options::Arguments::One,
+ [&sourceMapFilename](Options* o, const std::string& argument) {
+ sourceMapFilename = argument;
+ })
+ .add("--source-map-url",
+ "-su",
+ "Use specified string as source map URL",
+ Options::Arguments::One,
+ [&sourceMapUrl](Options* o, const std::string& argument) {
+ sourceMapUrl = argument;
+ })
+ .add("--symbolmap",
+ "-s",
+ "Emit a symbol map (indexes => names)",
+ Options::Arguments::One,
+ [&](Options* o, 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);
// default output is infile with changed suffix
if (options.extra.find("output") == options.extra.end()) {
- options.extra["output"] = removeSpecificSuffix(options.extra["infile"], ".wast") + ".wasm";
+ options.extra["output"] =
+ removeSpecificSuffix(options.extra["infile"], ".wast") + ".wasm";
}
- auto input(read_file<std::string>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
+ auto input(
+ read_file<std::string>(options.extra["infile"],
+ Flags::Text,
+ options.debug ? Flags::Debug : Flags::Release));
Module wasm;
try {
- if (options.debug) std::cerr << "s-parsing..." << std::endl;
+ 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;
+ if (options.debug)
+ std::cerr << "w-parsing..." << std::endl;
SExpressionWasmBuilder builder(wasm, *root[0]);
} catch (ParseException& p) {
p.dump(std::cerr);
@@ -94,15 +120,19 @@ int main(int argc, const char *argv[]) {
options.applyFeatures(wasm);
if (options.extra["validate"] != "none") {
- if (options.debug) std::cerr << "Validating..." << std::endl;
- if (!wasm::WasmValidator().validate(wasm,
- WasmValidator::Globally | (options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
+ if (options.debug)
+ std::cerr << "Validating..." << std::endl;
+ if (!wasm::WasmValidator().validate(
+ wasm,
+ WasmValidator::Globally |
+ (options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
WasmPrinter::printModule(&wasm);
Fatal() << "Error: input module is not valid.\n";
}
}
- if (options.debug) std::cerr << "writing..." << std::endl;
+ if (options.debug)
+ std::cerr << "writing..." << std::endl;
ModuleWriter writer;
writer.setBinary(true);
writer.setDebugInfo(debugInfo);
@@ -115,5 +145,6 @@ int main(int argc, const char *argv[]) {
}
writer.write(wasm, options.extra["output"]);
- if (options.debug) std::cerr << "Done." << std::endl;
+ if (options.debug)
+ std::cerr << "Done." << std::endl;
}