This is Zig’s wasm2c adapted to WAHE WebAssembly modules so they can be turned into native modules. All of my edits are marked with WAHE edit in the code.
Modifications:
-
Added an
EXPORTattribute. -
Added a way to call WAHE host’s
wahe_run_command(). -
Currently implemented some WASI functions as dummy functions, more work is needed there.
-
The
memorypointer is exported so it can be accessed by the WAHE host. -
Exported variables such as
__heap_baseor__data_endare currently ignored whereas they caused the original wasm2c to stop everything. -
Exported functions keep their original name so they can be found by the host.
-
SIMD opcodes are translated and the
wasm_simd_replacement.his their implementation for the translated C file. -
Functions are named, whereas we’d originally only see
static void f46(void)now we seestatic void f46_init_mouse(void). -
Functions have a comment added that shows its original position in the .wasm file which helps with debugging because each function can be identified using the original module’s
.mapfile. For example:static double f43_get_time_hr(void) { // source pos 77dewhich matches the.mapfile entry that reads- 77da 7b get_time_hr. -
Every line of code is followed by a comment showing where it ended in the source binary, for instance:
l149 = simd_f64x2_sub(l149, l150); // ends at 19b7. -
The section skipping starts by rewinding the file pointer so that sections can be read out of order.
-
Memory64 support.
-
Improved error reporting.
As with the original you can simply compile it by doing clang wasm2c.c -o wasm2c.exe and then use it by doing wasm2c $1.wasm $1_trans.c.
Then you can compile the transpiled file into a native module for instance by doing this: clang $1_trans.c -o $1_trans.dll -shared -Os -march=native.