Translates WebAssembly modules to portable C. Inspired by wabt's wasm2c.
Working towards WebAssembly as the Elusive Universal Binary:
↗ different
source code → WebAssembly → C89 → OSes,
(C++, Rust, ...) ↘ CPUs
names
custom section.debug_line
custom section.
Requires libdwarf to be installed. See instructions below.If your system is supported by at least CMake 2.8.12, prefer using CMake to detect features. On systems without CMake you can also use Make.
cd w2c2
cmake -B build
cmake --build build
cd wasi
cmake -B build
cmake --build build
For example, to compile module.wasm
to module.c
(and module.h
):
./w2c2 module.wasm module.c
w2c2 is able to compile a module into separate C files. This is recommended when compiling large modules and on hosts with limited resources.
For example, to compile module.wasm
(and module.h
), into multiple files with 100 functions each:
./w2c2 -f 100 module.wasm module.c
When w2c2 was built with threading support, it is able to compile a module in parallel. By default, w2c2 spawns as many worker threads as CPU cores are available.
To manually specify the number of worker threads, pass the number using the -t
flag.
For example, to compile using 2 threads:
./w2c2 -t 2 module.wasm module.c
Coremark:
cd examples/coremark
make
./coremark
Requires Python 3 and wabt (for wast2json
).
cd tests
make gen
make run-tests
args_get
args_sizes_get
clock_res_get
clock_time_get
environ_get
environ_sizes_get
fd_advise
fd_allocate
fd_close
fd_datasync
fd_fdstat_get
fd_fdstat_set_flags
fd_fdstat_set_rights
fd_filestat_get
fd_filestat_set_size
fd_filestat_set_times
fd_pread
fd_prestat_get
fd_prestat_dir_name
fd_pwrite
fd_read
fd_readdir
fd_renumber
fd_seek
fd_sync
fd_tell
fd_write
path_create_directory
path_filestat_get
path_filestat_set_times
path_link
path_open
path_readlink
path_remove_directory
path_rename
path_symlink
path_unlink_file
poll_oneoff
proc_exit
random_get
sched_yield
sock_recv
sock_send
sock_shutdown
thread-spawn
(from the threads proposal)To build a debug release, pass BUILD=debug
to make
.
To enable sanitizers, list them in the SANITIZERS
variable passed to make
, e.g. make BUILD=debug SANITIZERS="base clang address thread"
.
base
enables the Undefined Behavior Sanitizerclang
enables Clang-specific sanitizersthread
enables the Thread Sanitizeraddress
enables the Address Sanitizerlibdwarf-dev
libdwarf
(not dwarf
!)-DDWARF_OLD=1
to CMake. Version 20200114 is known to work.Since version 0.1.1, libdwarf ships with a pkg-config file, which CMake should be able to detect automatically.
If libdwarf cannot be automatically found by CMake, you get the following message:
-- Checking for module 'libdwarf'
-- No package 'libdwarf' found
In that case you can still provide the necessary information manually by passing a variation of the following options:
-DDWARF_FOUND=1 -DDWARF_LIBRARIES=-ldwarf -DDWARF_LIBRARY_DIRS=/usr/lib -DDWARF_INCLUDE_DIRS=/usr/include/libdwarf