|
| 1 | +// |
| 2 | +// wasm2asm console tool |
| 3 | +// |
| 4 | + |
| 5 | +#include "wasm2asm.h" |
| 6 | + |
| 7 | +using namespace cashew; |
| 8 | +using namespace wasm; |
| 9 | + |
| 10 | +namespace wasm { |
| 11 | +int debug = 0; |
| 12 | +} |
| 13 | + |
| 14 | +int main(int argc, char **argv) { |
| 15 | + debug = getenv("WASM2ASM_DEBUG") ? getenv("WASM2ASM_DEBUG")[0] - '0' : 0; |
| 16 | + |
| 17 | + char *infile = argv[1]; |
| 18 | + |
| 19 | + if (debug) std::cerr << "loading '" << infile << "'...\n"; |
| 20 | + FILE *f = fopen(argv[1], "r"); |
| 21 | + assert(f); |
| 22 | + fseek(f, 0, SEEK_END); |
| 23 | + int size = ftell(f); |
| 24 | + char *input = new char[size+1]; |
| 25 | + rewind(f); |
| 26 | + int num = fread(input, 1, size, f); |
| 27 | + // On Windows, ftell() gives the byte position (\r\n counts as two bytes), but when |
| 28 | + // reading, fread() returns the number of characters read (\r\n is read as one char \n, and counted as one), |
| 29 | + // so return value of fread can be less than size reported by ftell, and that is normal. |
| 30 | + assert((num > 0 || size == 0) && num <= size); |
| 31 | + fclose(f); |
| 32 | + input[num] = 0; |
| 33 | + |
| 34 | + SExpressionParser parser(input); |
| 35 | + Element& root = *parser.root; |
| 36 | + AllocatingModule wasm; |
| 37 | + SExpressionWasmBuilder builder(wasm, *root[0], [&]() { abort(); }); |
| 38 | + |
| 39 | + if (debug) std::cerr << "parsing...\n"; |
| 40 | + cashew::Parser<Ref, DotZeroValueBuilder> builder; |
| 41 | + Ref asmjs = builder.parseToplevel(input); |
| 42 | + |
| 43 | + if (debug) std::cerr << "asming...\n"; |
| 44 | + Wasm2AsmBuilder wasm2asm; |
| 45 | + Ref asmjs = wasm2asm.processWasm(wasm); |
| 46 | + |
| 47 | + if (debug) std::cerr << "printing...\n"; |
| 48 | + asmjs->stringify(std::cout); |
| 49 | + std::cout << '\n'; |
| 50 | + |
| 51 | + if (debug) std::cerr << "done.\n"; |
| 52 | +} |
| 53 | + |
0 commit comments