Skip to content

Commit

Permalink
Print wasm2asm parsing errors (WebAssembly#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken authored Oct 27, 2017
1 parent 2e51491 commit da871de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS asm2wasm DESTINATION ${CMAKE_INSTALL_BINDIR})

SET(wasm2asm_SOURCES
src/wasm2asm-main.cpp
src/tools/wasm2asm.cpp
)
ADD_EXECUTABLE(wasm2asm
${wasm2asm_SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Binaryen's internal IR is designed to be
There are a few differences between Binaryen IR and the WebAssembly language:

* Tree structure
* Binaryen IR [is an tree](https://github.com/WebAssembly/binaryen/issues/663), i.e., it has hierarchical structure, for convenience of optimization. This differs from the WebAssembly binary format which is a stack machine.
* Binaryen IR [is a tree](https://github.com/WebAssembly/binaryen/issues/663), i.e., it has hierarchical structure, for convenience of optimization. This differs from the WebAssembly binary format which is a stack machine.
* Consequently Binaryen's text format allows only s-expressions. WebAssembly's official text format is primarily a linear instruction list (with s-expression extensions). Binaryen can't read the linear style, but it can read a wasm text file if it contains only s-expressions.
* Types and unreachable code
* WebAssembly limits block/if/loop types to none and the concrete value types (i32, i64, f32, f64). Binaryen IR has an unreachable type, and it allows block/if/loop to take it, allowing [local transforms that don't need to know the global context](https://github.com/WebAssembly/binaryen/issues/903).
Expand Down
34 changes: 22 additions & 12 deletions src/wasm2asm-main.cpp → src/tools/wasm2asm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,31 @@ int main(int argc, const char *argv[]) {
auto input(
read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));

if (options.debug) std::cerr << "s-parsing..." << std::endl;
SExpressionParser parser(input.data());
Element &root = *parser.root;

if (options.debug) std::cerr << "w-parsing..." << std::endl;
Element* root;
Module wasm;
SExpressionWasmBuilder builder(wasm, *root[0]);
Ref asmjs;

try {
if (options.debug) std::cerr << "s-parsing..." << std::endl;
SExpressionParser parser(input.data());
root = parser.root;

if (options.debug) std::cerr << "w-parsing..." << std::endl;
SExpressionWasmBuilder builder(wasm, *(*root)[0]);

if (options.debug) std::cerr << "asming..." << std::endl;
Wasm2AsmBuilder wasm2asm(builderFlags);
Ref asmjs = wasm2asm.processWasm(&wasm);
if (options.debug) std::cerr << "asming..." << std::endl;
Wasm2AsmBuilder wasm2asm(builderFlags);
asmjs = wasm2asm.processWasm(&wasm);

if (options.extra["asserts"] == "1") {
if (options.debug) std::cerr << "asserting..." << std::endl;
flattenAppend(asmjs, wasm2asm.processAsserts(root, builder));
if (options.extra["asserts"] == "1") {
if (options.debug) std::cerr << "asserting..." << std::endl;
flattenAppend(asmjs, wasm2asm.processAsserts(*root, builder));
}
} catch (ParseException& p) {
p.dump(std::cerr);
Fatal() << "error in parsing input";
} catch (std::bad_alloc& b) {
Fatal() << "error in building module, std::bad_alloc (possibly invalid request for silly amounts of memory)";
}

if (options.debug) {
Expand Down

0 comments on commit da871de

Please sign in to comment.