diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm-interp.cc | 2 | ||||
-rw-r--r-- | src/tools/wast2json.cc | 130 | ||||
-rw-r--r-- | src/tools/wat2wasm.cc (renamed from src/tools/wast2wasm.cc) | 52 |
3 files changed, 148 insertions, 36 deletions
diff --git a/src/tools/wasm-interp.cc b/src/tools/wasm-interp.cc index aba0e9f3..5c108405 100644 --- a/src/tools/wasm-interp.cc +++ b/src/tools/wasm-interp.cc @@ -476,7 +476,7 @@ struct Action { }; // An extremely simple JSON parser that only knows how to parse the expected -// format from wast2wasm. +// format from wat2wasm. class SpecJSONParser { public: SpecJSONParser() : thread_(&env_, s_thread_options) {} diff --git a/src/tools/wast2json.cc b/src/tools/wast2json.cc new file mode 100644 index 00000000..7fb75c72 --- /dev/null +++ b/src/tools/wast2json.cc @@ -0,0 +1,130 @@ +/* + * Copyright 2016 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. + */ + +#include <cassert> +#include <cstdarg> +#include <cstdint> +#include <cstdlib> +#include <cstdio> +#include <string> + +#include "config.h" + +#include "src/binary-writer.h" +#include "src/binary-writer-spec.h" +#include "src/common.h" +#include "src/error-handler.h" +#include "src/feature.h" +#include "src/ir.h" +#include "src/option-parser.h" +#include "src/resolve-names.h" +#include "src/stream.h" +#include "src/validator.h" +#include "src/wast-parser.h" + +using namespace wabt; + +static const char* s_infile; +static const char* s_outfile; +static int s_verbose; +static WriteBinaryOptions s_write_binary_options; +static bool s_validate = true; +static bool s_debug_parsing; +static Features s_features; + +static std::unique_ptr<FileStream> s_log_stream; + +static const char s_description[] = +R"( read a file in the wasm spec test format, check it for errors, and + convert it to a JSON file and associated wasm binary files. + +examples: + # parse spec-test.wast, and write files to spec-test.json. Modules are + # written to spec-test.0.wasm, spec-test.1.wasm, etc. + $ wast2json spec-test.wast -o spec-test.json +)"; + +static void ParseOptions(int argc, char* argv[]) { + OptionParser parser("wast2json", s_description); + + parser.AddOption('v', "verbose", "Use multiple times for more info", []() { + s_verbose++; + s_log_stream = FileStream::CreateStdout(); + }); + parser.AddHelpOption(); + parser.AddOption("debug-parser", "Turn on debugging the parser of wast files", + []() { s_debug_parsing = true; }); + s_features.AddOptions(&parser); + parser.AddOption('o', "output", "FILE", "output wasm binary file", + [](const char* argument) { s_outfile = argument; }); + parser.AddOption( + 'r', "relocatable", + "Create a relocatable wasm binary (suitable for linking with wasm-link)", + []() { s_write_binary_options.relocatable = true; }); + parser.AddOption( + "no-canonicalize-leb128s", + "Write all LEB128 sizes as 5-bytes instead of their minimal size", + []() { s_write_binary_options.canonicalize_lebs = false; }); + parser.AddOption("debug-names", + "Write debug names to the generated binary file", + []() { s_write_binary_options.write_debug_names = true; }); + parser.AddOption("no-check", "Don't check for invalid modules", + []() { s_validate = false; }); + parser.AddArgument("filename", OptionParser::ArgumentCount::One, + [](const char* argument) { s_infile = argument; }); + + parser.Parse(argc, argv); +} + +int ProgramMain(int argc, char** argv) { + InitStdio(); + + ParseOptions(argc, argv); + + std::unique_ptr<WastLexer> lexer = WastLexer::CreateFileLexer(s_infile); + if (!lexer) + WABT_FATAL("unable to read file: %s\n", s_infile); + + ErrorHandlerFile error_handler(Location::Type::Text); + std::unique_ptr<Script> script; + WastParseOptions parse_wast_options(s_features); + Result result = + ParseWast(lexer.get(), &script, &error_handler, &parse_wast_options); + + if (Succeeded(result)) { + result = ResolveNamesScript(lexer.get(), script.get(), &error_handler); + + if (Succeeded(result) && s_validate) + result = ValidateScript(lexer.get(), script.get(), &error_handler); + + if (Succeeded(result)) { + WriteBinarySpecOptions write_binary_spec_options; + write_binary_spec_options.log_stream = s_log_stream.get(); + write_binary_spec_options.json_filename = s_outfile; + write_binary_spec_options.write_binary_options = s_write_binary_options; + result = WriteBinarySpecScript(script.get(), s_infile, + &write_binary_spec_options); + } + } + + return result != Result::Ok; +} + +int main(int argc, char** argv) { + WABT_TRY + return ProgramMain(argc, argv); + WABT_CATCH_BAD_ALLOC_AND_EXIT +} diff --git a/src/tools/wast2wasm.cc b/src/tools/wat2wasm.cc index bed3437f..20e27908 100644 --- a/src/tools/wast2wasm.cc +++ b/src/tools/wat2wasm.cc @@ -24,7 +24,6 @@ #include "config.h" #include "src/binary-writer.h" -#include "src/binary-writer-spec.h" #include "src/common.h" #include "src/error-handler.h" #include "src/feature.h" @@ -42,7 +41,6 @@ static const char* s_outfile; static bool s_dump_module; static int s_verbose; static WriteBinaryOptions s_write_binary_options; -static bool s_spec; static bool s_validate = true; static bool s_debug_parsing; static Features s_features; @@ -50,34 +48,30 @@ static Features s_features; static std::unique_ptr<FileStream> s_log_stream; static const char s_description[] = -R"( read a file in the wasm s-expression format, check it for errors, and +R"( read a file in the wasm text format, check it for errors, and convert it to the wasm binary format. examples: - # parse and typecheck test.wast - $ wast2wasm test.wast + # parse and typecheck test.wat + $ wat2wasm test.wat - # parse test.wast and write to binary file test.wasm - $ wast2wasm test.wast -o test.wasm + # parse test.wat and write to binary file test.wasm + $ wat2wasm test.wat -o test.wasm # parse spec-test.wast, and write verbose output to stdout (including # the meaning of every byte) - $ wast2wasm spec-test.wast -v - - # parse spec-test.wast, and write files to spec-test.json. Modules are - # written to spec-test.0.wasm, spec-test.1.wasm, etc. - $ wast2wasm spec-test.wast --spec -o spec-test.json + $ wat2wasm spec-test.wast -v )"; static void ParseOptions(int argc, char* argv[]) { - OptionParser parser("wast2wasm", s_description); + OptionParser parser("wat2wasm", s_description); parser.AddOption('v', "verbose", "Use multiple times for more info", []() { s_verbose++; s_log_stream = FileStream::CreateStdout(); }); parser.AddHelpOption(); - parser.AddOption("debug-parser", "Turn on debugging the parser of wast files", + parser.AddOption("debug-parser", "Turn on debugging the parser of wat files", []() { s_debug_parsing = true; }); parser.AddOption('d', "dump-module", "Print a hexdump of the module to stdout", @@ -90,10 +84,6 @@ static void ParseOptions(int argc, char* argv[]) { "Create a relocatable wasm binary (suitable for linking with wasm-link)", []() { s_write_binary_options.relocatable = true; }); parser.AddOption( - "spec", - "Parse a file with multiple modules and assertions, like the spec tests", - []() { s_spec = true; }); - parser.AddOption( "no-canonicalize-leb128s", "Write all LEB128 sizes as 5-bytes instead of their minimal size", []() { s_write_binary_options.canonicalize_lebs = false; }); @@ -133,6 +123,7 @@ int ProgramMain(int argc, char** argv) { WABT_FATAL("unable to read file: %s\n", s_infile); ErrorHandlerFile error_handler(Location::Type::Text); + // TODO(binji): Parse Module instead of Script. std::unique_ptr<Script> script; WastParseOptions parse_wast_options(s_features); Result result = @@ -145,25 +136,16 @@ int ProgramMain(int argc, char** argv) { result = ValidateScript(lexer.get(), script.get(), &error_handler); if (Succeeded(result)) { - if (s_spec) { - WriteBinarySpecOptions write_binary_spec_options; - write_binary_spec_options.log_stream = s_log_stream.get(); - write_binary_spec_options.json_filename = s_outfile; - write_binary_spec_options.write_binary_options = s_write_binary_options; - result = WriteBinarySpecScript(script.get(), s_infile, - &write_binary_spec_options); + MemoryStream stream(s_log_stream.get()); + const Module* module = script->GetFirstModule(); + if (module) { + result = WriteBinaryModule(&stream, module, &s_write_binary_options); } else { - MemoryStream stream(s_log_stream.get()); - const Module* module = script->GetFirstModule(); - if (module) { - result = WriteBinaryModule(&stream, module, &s_write_binary_options); - } else { - WABT_FATAL("no module found\n"); - } - - if (Succeeded(result)) - WriteBufferToFile(s_outfile, stream.output_buffer()); + WABT_FATAL("no module found\n"); } + + if (Succeeded(result)) + WriteBufferToFile(s_outfile, stream.output_buffer()); } } |