turbolent / w2c2

Translates WebAssembly modules to portable C
MIT License
708 stars 37 forks source link
c c89 compiler wasi wasm wasm2c webassembly

w2c2

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

Features

Performance

Compilation

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.

w2c2

cd w2c2
cmake -B build
cmake --build build

wasi

cd wasi
cmake -B build
cmake --build build

Usage

For example, to compile module.wasm to module.c (and module.h):

./w2c2 module.wasm module.c

Separate Compilation

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

Parallel Compilation

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

Examples

Coremark:

cd examples/coremark
make
./coremark

Testing

Requires Python 3 and wabt (for wast2json).

cd tests
make gen
make run-tests

WASI Status

Development

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".

Installing libdwarf (required for source line mapping)