|
18 | 18 | // wasm2asm console tool |
19 | 19 | // |
20 | 20 |
|
21 | | -#include "wasm2asm.h" |
| 21 | + |
| 22 | +#include "support/colors.h" |
| 23 | +#include "support/command-line.h" |
| 24 | +#include "support/file.h" |
22 | 25 | #include "wasm-s-parser.h" |
| 26 | +#include "wasm2asm.h" |
23 | 27 |
|
24 | 28 | using namespace cashew; |
25 | 29 | using namespace wasm; |
26 | 30 |
|
27 | | -namespace wasm { |
28 | | -int debug = 0; |
29 | | -} |
30 | | - |
31 | | -int main(int argc, char **argv) { |
32 | | - debug = getenv("WASM2ASM_DEBUG") ? getenv("WASM2ASM_DEBUG")[0] - '0' : 0; |
33 | | - |
34 | | - char *infile = argv[1]; |
| 31 | +int main(int argc, const char *argv[]) { |
| 32 | + Options options("wasm2asm", "Transform .wast files to asm.js"); |
| 33 | + options |
| 34 | + .add("--output", "-o", "Output file (stdout if not specified)", |
| 35 | + Options::Arguments::One, |
| 36 | + [](Options *o, const std::string &argument) { |
| 37 | + o->extra["output"] = argument; |
| 38 | + Colors::disable(); |
| 39 | + }) |
| 40 | + .add_positional("INFILE", Options::Arguments::One, |
| 41 | + [](Options *o, const std::string &argument) { |
| 42 | + o->extra["infile"] = argument; |
| 43 | + }); |
| 44 | + options.parse(argc, argv); |
35 | 45 |
|
36 | | - if (debug) std::cerr << "loading '" << infile << "'...\n"; |
37 | | - FILE *f = fopen(argv[1], "r"); |
38 | | - assert(f); |
39 | | - fseek(f, 0, SEEK_END); |
40 | | - int size = ftell(f); |
41 | | - char *input = new char[size+1]; |
42 | | - rewind(f); |
43 | | - int num = fread(input, 1, size, f); |
44 | | - // On Windows, ftell() gives the byte position (\r\n counts as two bytes), but when |
45 | | - // reading, fread() returns the number of characters read (\r\n is read as one char \n, and counted as one), |
46 | | - // so return value of fread can be less than size reported by ftell, and that is normal. |
47 | | - assert((num > 0 || size == 0) && num <= size); |
48 | | - fclose(f); |
49 | | - input[num] = 0; |
| 46 | + auto input( |
| 47 | + read_file<std::vector<char>>(options.extra["infile"], options.debug)); |
50 | 48 |
|
51 | | - if (debug) std::cerr << "s-parsing...\n"; |
52 | | - SExpressionParser parser(input); |
53 | | - Element& root = *parser.root; |
| 49 | + if (options.debug) std::cerr << "s-parsing..." << std::endl; |
| 50 | + SExpressionParser parser(input.data()); |
| 51 | + Element &root = *parser.root; |
54 | 52 |
|
55 | | - if (debug) std::cerr << "w-parsing...\n"; |
| 53 | + if (options.debug) std::cerr << "w-parsing..." << std::endl; |
56 | 54 | AllocatingModule wasm; |
57 | 55 | SExpressionWasmBuilder builder(wasm, *root[0], [&]() { abort(); }); |
58 | 56 |
|
59 | | - if (debug) std::cerr << "asming...\n"; |
60 | | - Wasm2AsmBuilder wasm2asm; |
| 57 | + if (options.debug) std::cerr << "asming..." << std::endl; |
| 58 | + Wasm2AsmBuilder wasm2asm(options.debug); |
61 | 59 | Ref asmjs = wasm2asm.processWasm(&wasm); |
62 | 60 |
|
63 | | - if (debug) { |
64 | | - std::cerr << "a-printing...\n"; |
| 61 | + if (options.debug) { |
| 62 | + std::cerr << "a-printing..." << std::endl; |
65 | 63 | asmjs->stringify(std::cout, true); |
66 | 64 | std::cout << '\n'; |
67 | 65 | } |
68 | 66 |
|
69 | | - if (debug) std::cerr << "j-printing...\n"; |
| 67 | + if (options.debug) std::cerr << "j-printing..." << std::endl; |
70 | 68 | JSPrinter jser(true, true, asmjs); |
71 | 69 | jser.printAst(); |
72 | | - std::cout << jser.buffer << "\n"; |
| 70 | + Output output(options.extra["output"], options.debug); |
| 71 | + output << jser.buffer << std::endl; |
73 | 72 |
|
74 | | - if (debug) std::cerr << "done.\n"; |
| 73 | + if (options.debug) std::cerr << "done." << std::endl; |
75 | 74 | } |
0 commit comments