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.
Line numbers outside C89's portable range of 1 through 32767 are omitted from the generated #line directives.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
w2c2 builds a static translator library by default.
Use -DSHARED_LIB=ON with CMake,
or SHARED_LIB=1 with Make,
for a shared library on POSIX systems.
Windows supports static libraries only.
cmake -S w2c2 -B build -DCMAKE_INSTALL_PREFIX=/your/prefix
cmake --build build
ctest --test-dir build --output-on-failure
cmake --install build
CMake installs the library, public headers, CLI, and a relocatable package. Consumers can use:
find_package(w2c2 CONFIG REQUIRED)
target_link_libraries(my_translator PRIVATE w2c2::w2c2_lib)
Pass -DCMAKE_PREFIX_PATH=/your/prefix when configuring the consumer.
The package supplies the library's thread and libdwarf dependencies when enabled.
With Make:
make -C w2c2
make -C w2c2 test
make -C w2c2 install PREFIX=/your/prefix
cc -I/your/prefix/include/w2c2 examples/embed/main.c /your/prefix/lib/libw2c2.a -lm -pthread -o embed
Make installation supports DESTDIR for staging.
Add -ldwarf when the library was built with libdwarf support.
The CMake package is installed by CMake only.
Run make clean before changing build features or switching library types.
Coremark:
cd examples/coremark
make
./coremark
Requires Python 3 and wabt (for wast2json).
cd tests
make gen
make run-tests
args_getargs_sizes_getclock_res_getclock_time_getenviron_getenviron_sizes_getfd_advisefd_allocatefd_closefd_datasyncfd_fdstat_getfd_fdstat_set_flagsfd_fdstat_set_rightsfd_filestat_getfd_filestat_set_sizefd_filestat_set_timesfd_preadfd_prestat_getfd_prestat_dir_namefd_pwritefd_readfd_readdirfd_renumberfd_seekfd_syncfd_tellfd_writepath_create_directorypath_filestat_getpath_filestat_set_timespath_linkpath_openpath_readlinkpath_remove_directorypath_renamepath_symlinkpath_unlink_filepoll_oneoffproc_exitrandom_getsched_yieldsock_recvsock_sendsock_shutdownthread-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 SanitizerOn Linux, try installing a package named like libdwarf-dev
On macOS, you can use Homebrew and install libdwarf (not dwarf!)
w2c2 currently defaults to using the libdwarf API of >=v0.4.2. v0.6.0 has been tested to work successfully too.
If using a version <0.4.2, try passing -DDWARF_OLD=1 to CMake.
Version 20201020 or later is required.
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
(top 24 of 63)
1,943 followers · starred Dec 2021
1,793 followers · starred Feb 2023
1,853 followers · starred Apr 2023
1,026 followers · starred Dec 2021
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.
Line numbers outside C89's portable range of 1 through 32767 are omitted from the generated #line directives.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
w2c2 builds a static translator library by default.
Use -DSHARED_LIB=ON with CMake,
or SHARED_LIB=1 with Make,
for a shared library on POSIX systems.
Windows supports static libraries only.
cmake -S w2c2 -B build -DCMAKE_INSTALL_PREFIX=/your/prefix
cmake --build build
ctest --test-dir build --output-on-failure
cmake --install build
CMake installs the library, public headers, CLI, and a relocatable package. Consumers can use:
find_package(w2c2 CONFIG REQUIRED)
target_link_libraries(my_translator PRIVATE w2c2::w2c2_lib)
Pass -DCMAKE_PREFIX_PATH=/your/prefix when configuring the consumer.
The package supplies the library's thread and libdwarf dependencies when enabled.
With Make:
make -C w2c2
make -C w2c2 test
make -C w2c2 install PREFIX=/your/prefix
cc -I/your/prefix/include/w2c2 examples/embed/main.c /your/prefix/lib/libw2c2.a -lm -pthread -o embed
Make installation supports DESTDIR for staging.
Add -ldwarf when the library was built with libdwarf support.
The CMake package is installed by CMake only.
Run make clean before changing build features or switching library types.
Coremark:
cd examples/coremark
make
./coremark
Requires Python 3 and wabt (for wast2json).
cd tests
make gen
make run-tests
args_getargs_sizes_getclock_res_getclock_time_getenviron_getenviron_sizes_getfd_advisefd_allocatefd_closefd_datasyncfd_fdstat_getfd_fdstat_set_flagsfd_fdstat_set_rightsfd_filestat_getfd_filestat_set_sizefd_filestat_set_timesfd_preadfd_prestat_getfd_prestat_dir_namefd_pwritefd_readfd_readdirfd_renumberfd_seekfd_syncfd_tellfd_writepath_create_directorypath_filestat_getpath_filestat_set_timespath_linkpath_openpath_readlinkpath_remove_directorypath_renamepath_symlinkpath_unlink_filepoll_oneoffproc_exitrandom_getsched_yieldsock_recvsock_sendsock_shutdownthread-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 SanitizerOn Linux, try installing a package named like libdwarf-dev
On macOS, you can use Homebrew and install libdwarf (not dwarf!)
w2c2 currently defaults to using the libdwarf API of >=v0.4.2. v0.6.0 has been tested to work successfully too.
If using a version <0.4.2, try passing -DDWARF_OLD=1 to CMake.
Version 20201020 or later is required.
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
(top 24 of 63)
1,943 followers · starred Dec 2021
1,793 followers · starred Feb 2023
1,853 followers · starred Apr 2023
1,026 followers · starred Dec 2021