Skip to content

Commit da871de

Browse files
authored
Print wasm2asm parsing errors (#1251)
1 parent 2e51491 commit da871de

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD_REQUIRED ON)
244244
INSTALL(TARGETS asm2wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
245245

246246
SET(wasm2asm_SOURCES
247-
src/wasm2asm-main.cpp
247+
src/tools/wasm2asm.cpp
248248
)
249249
ADD_EXECUTABLE(wasm2asm
250250
${wasm2asm_SOURCES})

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Binaryen's internal IR is designed to be
3535
There are a few differences between Binaryen IR and the WebAssembly language:
3636

3737
* Tree structure
38-
* 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.
38+
* 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.
3939
* 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.
4040
* Types and unreachable code
4141
* 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).
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,31 @@ int main(int argc, const char *argv[]) {
5757
auto input(
5858
read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
5959

60-
if (options.debug) std::cerr << "s-parsing..." << std::endl;
61-
SExpressionParser parser(input.data());
62-
Element &root = *parser.root;
63-
64-
if (options.debug) std::cerr << "w-parsing..." << std::endl;
60+
Element* root;
6561
Module wasm;
66-
SExpressionWasmBuilder builder(wasm, *root[0]);
62+
Ref asmjs;
63+
64+
try {
65+
if (options.debug) std::cerr << "s-parsing..." << std::endl;
66+
SExpressionParser parser(input.data());
67+
root = parser.root;
68+
69+
if (options.debug) std::cerr << "w-parsing..." << std::endl;
70+
SExpressionWasmBuilder builder(wasm, *(*root)[0]);
6771

68-
if (options.debug) std::cerr << "asming..." << std::endl;
69-
Wasm2AsmBuilder wasm2asm(builderFlags);
70-
Ref asmjs = wasm2asm.processWasm(&wasm);
72+
if (options.debug) std::cerr << "asming..." << std::endl;
73+
Wasm2AsmBuilder wasm2asm(builderFlags);
74+
asmjs = wasm2asm.processWasm(&wasm);
7175

72-
if (options.extra["asserts"] == "1") {
73-
if (options.debug) std::cerr << "asserting..." << std::endl;
74-
flattenAppend(asmjs, wasm2asm.processAsserts(root, builder));
76+
if (options.extra["asserts"] == "1") {
77+
if (options.debug) std::cerr << "asserting..." << std::endl;
78+
flattenAppend(asmjs, wasm2asm.processAsserts(*root, builder));
79+
}
80+
} catch (ParseException& p) {
81+
p.dump(std::cerr);
82+
Fatal() << "error in parsing input";
83+
} catch (std::bad_alloc& b) {
84+
Fatal() << "error in building module, std::bad_alloc (possibly invalid request for silly amounts of memory)";
7585
}
7686

7787
if (options.debug) {

0 commit comments

Comments
 (0)