Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Zig’s wasm2c adapted for WAHE

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 EXPORT attribute.

  • 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 memory pointer is exported so it can be accessed by the WAHE host.

  • Exported variables such as __heap_base or __data_end are 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.h is their implementation for the translated C file.

  • Functions are named, whereas we’d originally only see static void f46(void) now we see static 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 .map file. For example: static double f43_get_time_hr(void) { // source pos 77de which matches the .map file 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.

Compilation and usage

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.