]>
git.proxmox.com Git - rustc.git/blob - src/binaryen/src/tools/wasm-dis.cpp
2 * Copyright 2016 WebAssembly Community Group participants
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // wasm2asm console tool
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"
27 using namespace cashew
;
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
;
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
;
46 options
.parse(argc
, argv
);
48 auto input(read_file
<std::vector
<char>>(options
.extra
["infile"], Flags::Binary
, options
.debug
? Flags::Debug
: Flags::Release
));
50 if (options
.debug
) std::cerr
<< "parsing binary..." << std::endl
;
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());
61 if (sourceMapStream
) {
62 sourceMapStream
->close();
64 } catch (ParseException
& p
) {
66 Fatal() << "error in parsing wasm binary";
67 } catch (MapParseException
& p
) {
69 Fatal() << "error in parsing wasm source mapping";
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());
77 if (options
.debug
) std::cerr
<< "Done." << std::endl
;