]> git.proxmox.com Git - rustc.git/blob - src/binaryen/src/tools/wasm-dis.cpp
New upstream version 1.23.0+dfsg1
[rustc.git] / src / binaryen / src / tools / wasm-dis.cpp
1 /*
2 * Copyright 2016 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //
18 // wasm2asm console tool
19 //
20
21 #include "support/colors.h"
22 #include "support/command-line.h"
23 #include "support/file.h"
24 #include "wasm-binary.h"
25 #include "wasm-printing.h"
26
27 using namespace cashew;
28 using namespace wasm;
29
30 int main(int argc, const char *argv[]) {
31 std::string sourceMapFilename;
32 Options options("wasm-dis", "Un-assemble a .wasm (WebAssembly binary format) into a .wast (WebAssembly text format)");
33 options.add("--output", "-o", "Output file (stdout if not specified)",
34 Options::Arguments::One,
35 [](Options *o, const std::string &argument) {
36 o->extra["output"] = argument;
37 Colors::disable();
38 })
39 .add("--source-map", "-sm", "Consume source map from the specified file to add location information",
40 Options::Arguments::One,
41 [&sourceMapFilename](Options *o, const std::string &argument) { sourceMapFilename = argument; })
42 .add_positional("INFILE", Options::Arguments::One,
43 [](Options *o, const std::string &argument) {
44 o->extra["infile"] = argument;
45 });
46 options.parse(argc, argv);
47
48 auto input(read_file<std::vector<char>>(options.extra["infile"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release));
49
50 if (options.debug) std::cerr << "parsing binary..." << std::endl;
51 Module wasm;
52 try {
53 std::unique_ptr<std::ifstream> sourceMapStream;
54 WasmBinaryBuilder parser(wasm, input, options.debug);
55 if (sourceMapFilename.size()) {
56 sourceMapStream = make_unique<std::ifstream>();
57 sourceMapStream->open(sourceMapFilename);
58 parser.setDebugLocations(sourceMapStream.get());
59 }
60 parser.read();
61 if (sourceMapStream) {
62 sourceMapStream->close();
63 }
64 } catch (ParseException& p) {
65 p.dump(std::cerr);
66 Fatal() << "error in parsing wasm binary";
67 } catch (MapParseException& p) {
68 p.dump(std::cerr);
69 Fatal() << "error in parsing wasm source mapping";
70 }
71
72 if (options.debug) std::cerr << "Printing..." << std::endl;
73 Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
74 WasmPrinter::printModule(&wasm, output.getStream());
75 output << '\n';
76
77 if (options.debug) std::cerr << "Done." << std::endl;
78 }