Skip to content

Commit 0d2c26a

Browse files
committed
start on wasm2asm tool
1 parent be0ddfa commit 0d2c26a

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ echo "building binaryen shell"
22
g++ -O2 -std=c++11 src/binaryen-shell.cpp src/pass.cpp src/passes/*.cpp -o bin/binaryen-shell -Isrc/ -msse2 -mfpmath=sse # use sse for math, avoid x87, necessarily for proper float rounding on 32-bit
33
echo "building asm2wasm"
44
g++ -O2 -std=c++11 src/asm2wasm-main.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp -o bin/asm2wasm
5+
#echo "building wasm2asm"
6+
#g++ -O2 -std=c++11 src/wasm2asm-main.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp -o bin/wasm2asm
57
echo "building interpreter/js"
68
em++ -std=c++11 src/wasm-js.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp -o bin/wasm.js -s MODULARIZE=1 -s 'EXPORT_NAME="WasmJS"' --memory-init-file 0 -Oz -s ALLOW_MEMORY_GROWTH=1 -profiling -s DEMANGLE_SUPPORT=1 #-DWASM_JS_DEBUG -DWASM_INTERPRETER_DEBUG=2
79
cat src/js/post.js >> bin/wasm.js

src/wasm2asm-main.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)