turbolent/w2c2 logo

turbolent/w2c2

Free

Translates WebAssembly modules to portable C

FreeFree tier
Type
Open Source

About turbolent/w2c2

w2c2 is an open-source tool that translates WebAssembly modules into portable C code, inspired by wabt's wasm2c. It implements the WebAssembly Core Specification 1.0 along with several extensions (threads, bulk memory, sign-extension, non-trapping float-to-int conversions) and passes 99.9% of the WebAssembly core semantics test suite. The tool is written in (mostly) C89 and generates (mostly) C89 output, supporting a wide range of operating systems (Mac OS X, Mac OS 9, Haiku, DOS, Windows XP, etc.), architectures (x86, ARM, PowerPC, SPARC, PA-RISC), compilers (old GCC, MSVC, CodeWarrior), and big-endian systems. Features include streaming/single-pass compilation with low memory usage, separate compilation into multiple files, parallel compilation when built with threading support, and generation of debug information (function names and source line mapping) via libdwarf. w2c2 also includes a WASI implementation capable of running clang and Python. Performance benchmarks show Coremark 1.0 runs about 7% slower than native.

Key Features

Implements WebAssembly Core Specification 1.0
Supports extensions: threads/atomics, bulk memory, sign-extension, non-trapping float-to-int conversions
Passes 99.9% of the WebAssembly core semantics test suite
Written in (mostly) C89 and generates (mostly) C89 output
Support for many operating systems (Mac OS X, Mac OS 9, Haiku, DOS, Windows XP, etc.)
Support for many architectures (x86, ARM, PowerPC, SPARC, PA-RISC) and big-endian systems
Support for many compilers (old GCC, MSVC, CodeWarrior)
Streaming/single-pass compilation with low memory usage
Separate compilation into multiple files (using -f flag) and parallel compilation (using -t flag)
Generates debug information: function names and source line mapping via libdwarf

Pros & Cons

Pros
  • High compliance with WebAssembly specification (99.9% test suite pass rate)
  • Generates portable C89 code that runs on a vast array of operating systems, architectures, and compilers
  • Streaming/single-pass compilation minimizes memory usage, ideal for large modules or limited resources
  • Support for separate and parallel compilation to speed up builds and handle large modules
  • Includes a built-in WASI implementation capable of running real-world applications like clang and Python
  • Performance is close to native (Coremark 1.0: 7% slower), making it suitable for production use
  • Can generate debug information for easier troubleshooting
  • Open source and free to use
Cons
  • Does not pass 100% of the WebAssembly core semantics test suite (99.9% pass rate, minor deviations)
  • Debug info generation requires libdwarf, an external library
  • Only generates C89 output, which may not leverage modern C features or compiler optimizations
  • Performance is slightly slower than native (~7% on Coremark), though still competitive
  • Some common C extensions are used in output; may not compile on extremely strict C89 compilers

Best For

Embedding WebAssembly modules in C/C++ projects across various operating systems and architecturesRunning WebAssembly on legacy or resource-constrained systems (e.g., DOS, Mac OS 9) that lack a WebAssembly runtimeConverting WebAssembly to C89 for cross-platform portabilityUtilizing WebAssembly as a universal binary format for multiple CPU architectures and operating systemsDebugging WebAssembly modules by generating source line mappings and function namesRunning WASI applications (e.g., clang, Python) on non-WASI platforms via generated C code

FAQ

What is w2c2?
w2c2 is an open-source tool that translates WebAssembly modules into portable C code, enabling WebAssembly to run on virtually any platform that has a C compiler.
How does w2c2 compare to wabt's wasm2c?
w2c2 is inspired by wabt's wasm2c but aims for broader platform support, including legacy operating systems and architectures, and achieves 99.9% spec compliance.
What operating systems and architectures does w2c2 support?
It supports many operating systems (Mac OS X, Mac OS 9, Haiku, Rhapsody, DOS, Windows XP, etc.) and architectures (x86, ARM, PowerPC, SPARC, PA-RISC, etc.), including big-endian systems.
Can w2c2 generate debug information?
Yes, it can generate function names (if provided in the names custom section) and source line mapping (if DWARF line info is provided in the .debug_line custom section). Debug support requires libdwarf to be installed.
What is the performance of w2c2 compared to native?
On the Coremark 1.0 benchmark, w2c2-generated code is about 7% slower than native compilation.
Does w2c2 support multiple modules and instances?
Yes, w2c2 provides support for multiple modules and instances, and can assist with incremental compilation by separating static and dynamic functions.