Translates WebAssembly modules to C

Overview

w2c2

Translates WebAssembly modules to C. Inspired by wabt's wasm2c.

Features

  • Implements the WebAssembly Core Specification 1.0
  • Passes 99.9% of the WebAssembly core semantics test suite
  • Written in C89 and generates C89
  • Support for older operating systems and architectures
  • Support for big-endian systems
  • Streaming compilation, low memory usage

Performance

  • Coremark 1.0: ~7% slower than native

Usage

make release
./w2c2 <module.wasm> > <module.c>

Examples

Coremark:

cd examples/coremark
./coremark

Testing

Requires Python 3 and wabt (for wast2json).

cd tests
make gen
make run-tests
Issues
  • Doesn't fully work on big-endian architectures

    Doesn't fully work on big-endian architectures

    I have a Casio CG50 calculator, which can be programmed in C(++). However, when I compile this AssemblScript code, convert it and compile it for the calculator I get all 0s back.

    export function fac2(n: i32): i32 {
      if (n == 0) {
        return 1;
      }
      return n * fac2(n - 1);
    }
    
    export function fac(n: i32): string {
      // Create an i32 to i32 map
      let map = new Map<i32, i32>();
      map.set(n, fac2(n));
      return `The factorial of ${n} is ${map.get(n)}\nMap: ${map}`;
    }
    

    I did make some modifications to the header file but I compiled the same thing on native Linux and it worked fine. An idea what I can try? The calculator uses a big-endian SuperH 4 CPU and I'm compiling with GCC. Any idea what I can try to debug it?

    opened by Heath123 12
  • Windows, WASI targets support

    Windows, WASI targets support

    Following-up the idea in https://github.com/turbolent/w2c2/issues/1#issuecomment-1019954628

    • Allow disabling pthreads
    • Initial Windows target support (no parallel mode + getopt impl)
    • Initial WASI target support
    • Skip creating empty files in parallel mode
    enhancement 
    opened by vshymanskyy 4
  • Improve WASI implementation

    Improve WASI implementation

    Implement enough of the WASI spec to get e.g. clang.wasm running

    Proc

    • [x] wasi_unstable.proc_exit

    Environ

    • [x] wasi_unstable.environ_sizes_get
    • [x] wasi_unstable.environ_get

    Args

    • [x] wasi_unstable.args_sizes_get
    • [x] wasi_unstable.args_get

    Clock

    • [x] wasi_unstable.clock_time_get

    Random

    • [x] wasi_unstable.random_get

    Poll

    • [ ] wasi_unstable.poll_oneoff

    FD

    • [x] wasi_unstable.fd_write
    • [x] wasi_unstable.fd_close
    • [x] wasi_unstable.fd_seek
    • [x] wasi_unstable.fd_fdstat_get
    • [x] wasi_unstable.fd_filestat_get
    • [x] wasi_unstable.fd_pread
    • [x] wasi_unstable.fd_read
    • [ ] wasi_unstable.fd_readdir
    • [ ] wasi_unstable.fd_fdstat_set_flags
    • [x] wasi_unstable.fd_prestat_get
    • [x] wasi_unstable.fd_prestat_dir_name

    Path

    • [x] wasi_unstable.path_open
    • [x] wasi_unstable.path_filestat_get
    • [x] wasi_unstable.path_rename
    • [x] wasi_unstable.path_unlink_file
    • [x] wasi_unstable.path_remove_directory
    • [x] wasi_unstable.path_create_directory
    • [x] wasi_unstable.path_readlink
    • [x] wasi_unstable.path_symlink
    enhancement 
    opened by turbolent 3
  • Reduce output size

    Reduce output size

    Reduce the size of the generated code by:

    • Omitting the parameter names in function declarations
    • Removing whitespace (e.g. indentation, space between operators)
    • Compressing stack variable declarations in function implementations

    For clang.wasm this optimization reduces the size from 1.3 GiB to 159MiB.

    enhancement 
    opened by turbolent 1
  • Add parallel compilation

    Add parallel compilation

    Generate code in parallel when the new option -j is given. Code is written into separate files, with a fixed number of functions per file, configurable with the new option -f.

    The new output path option -o can be specified to generate code into a single file when there is one job, and the option must be specified and be a directory when compiling in parallel.

    Parallel compilation generates a decls.h with all declarations, an inits.c file with the initialization code, and one or more '.c' files with the function implementations.

    enhancement 
    opened by turbolent 1
  • Add support for parsing DWARF and generating line directives

    Add support for parsing DWARF and generating line directives

    Implement support for DWARF in WebAssembly modules:

    • Parse custom sections with .debug_ prefix
    • Parse DWARF information using libdwarf:
      • Line information of all compilation units
      • Line information of all subprograms
    • Generate #line directives
    enhancement 
    opened by turbolent 0
  • Optimize data segments

    Optimize data segments

    Optimize the compilation of data segments by adding

    Make the data segment compilation configurable, by adding the command line option -d. The option help lists the available modes:

    • arrays: Writes each data segment as a C array (default)
    • gnu-ld: All data segments are embedded into a data section using GNU LD
    • sectcreate1: All data segments are embedded into a data section using the sectcreate option of modern Mach-O LD versions (recent macOS versions) and accessed using an asm reference
    • sectcreate2: All data segments are embedded into a data section using the sectcreate option of older Mach-O LD versions (older Mac OS X, OPENSTEP, NeXTSTEP, etc.) and accessed using Mach-O getsectdata
    enhancement 
    opened by turbolent 0
  • Segfault when compiling module

    Segfault when compiling module

    If you compile the AssemblyScript program:

    export declare function test(): void
    

    into WebAssembly (or use this one I already compiled):

    asc test.ts --binaryFile test.wasm
    

    Then into C:

    w2c2 -o test.c test.wasm
    

    you get a segfault:

    fish: Job 1, '~/w2c2/w2c2 -o test.c test.wasm' terminated by signal SIGSEGV (Address boundary error)
    

    Backtrace:

    (gdb) backtrace
    #0  wasmCWriteExports (file=0x5615678c32a0, module=0x5615678c3480, pretty=false, external=true) at c.c:3187
    #1  0x0000561565bb8338 in wasmCWriteModuleDeclarations (file=0x5615678c32a0, module=0x5615678c3480, parallel=false, pretty=false) at c.c:3626
    #2  0x0000561565bb8566 in wasmCWriteDeclarations (module=0x5615678c3480, singleFile=0x5615678c32a0, pretty=false) at c.c:3687
    #3  0x0000561565bb8960 in wasmCWriteModule (module=0x5615678c3480, options=...) at c.c:4100
    #4  0x0000561565bba9f2 in main (argc=4, argv=0x7ffcf43a12f8) at main.c:184
    (gdb) 
    

    Happens here: https://github.com/turbolent/w2c2/blob/main/c.c#L3187

    opened by Heath123 4
  • Comparison with `wasm2c` + some ideas

    Comparison with `wasm2c` + some ideas

    Just performed a quick test to understand how it compares to wasm2c. For this test I took a ~25 MB wasm (WASI) file, clang.wasm

    • w2c2 finishes in 5.1s, RAM usage: 193 Mb. Output: 1.3 GiB file (34.8 Mb zipped)
    • wasm2c finishes in 1m23s, RAM usage: 1432 Mb. Output: 301 MiB file (22 Mb zipped)

    Clearly, speed and RAM usage are really good :tada: :rocket: Wondering if file size can be reduced :scientist:

    This also brings another question. C compilers are not very efficient when working with huge C files, so monsters like clang.wasm will take ages to compile. This is mentioned in my wasm2native tool: https://github.com/vshymanskyy/wasm2native#todo One of the possible solutions is to split output into smaller chunks (which also enables parallel build).

    opened by vshymanskyy 30
Owner
Bastian Müller
Bastian Müller
exp2swift translator which translates STEP schema files described in ISO 10303-11 STEP EXPRESS language to the Swift programing language.

STEPswiftcode/ exp2swift exp2swift translator which translates STEP schema files described in ISO 10303-11 STEP EXPRESS language to the Swift programi

Tsutomu Yoshida 1 Jan 3, 2022
Run statically-compiled WebAssembly apps on any embedded platform

embedded-wasm-apps Run native, statically-compiled apps on any platform, using WebAssembly. Examples include AssemblyScript, Rust, C/C++, TinyGo, Zig,

Wasm3 Labs 99 Jun 22, 2022
WebAssembly from Scratch: From FizzBuzz to DooM.

WebAssembly from Scratch: From FizzBuzz to DooM Exploring WebAssembly from scratch from a backend-person-point-of-view. A story in four acts. Welcome

Cornelius Diekmann 1.4k Jun 23, 2022
Snake in C++ for WebAssembly

Snake-wasm Snake in C++ for WebAssembly This is a primitive greedy snake game demo in C++(ish) to test WebAssembly Getting Started Install emscripten

null 1 Jan 4, 2022
Minimal C# WebAssembly WebGL Example

Minimal C# WebAssembly WebGL Example a minimal example of C# wasm WebGL app based on dotnet/runtimelab/NativeAOT-LLVM WebAssembly This example is port

Zou Wei 8 Jun 13, 2022
An Arduino library which allows you to communicate seamlessly with the full range of u-blox GNSS modules

u-blox makes some incredible GNSS receivers covering everything from low-cost, highly configurable modules such as the SAM-M8Q all the way up to the surveyor grade ZED-F9P with precision of the diameter of a dime.

SparkFun Electronics 112 Jun 22, 2022
ESP32 firmware to read and control EMS and Heatronic compatible equipment such as boilers, thermostats, solar modules, and heat pumps

EMS-ESP is an open-source firmware for the Espressif ESP8266 and ESP32 microcontroller that communicates with EMS (Energy Management System) based equipment from manufacturers like Bosch, Buderus, Nefit, Junkers, Worcester and Sieger.

EMS-ESP 152 Jun 16, 2022
Doom port to the Ikea Tradfri RGB1923R5 and any device using Silicon labs EFR32MG21 based modules

MG21DOOM Doom port to the Ikea Tradfri RGB1923R5 lamp and any device using Silicon labs EFR32MG21 based modules. Coded by Nicola Wrachien. WARNING Do

null 20 Jun 26, 2022
Create VCV Rack modules from gen~ exports

gen-rack Export gen~ patches to VCV Rack modules. How to use First, you'll want to grab a copy of this repository (clone/download/etc). Then take a lo

Isabel 66 May 17, 2022
An open source Zstack3 gateway powered by ESP8266 and CC2652 modules

OpenZ3Gateway An open source Zstack3 gateway powered by ESP8266 and CC2652P modules. One costs less than 60 CNY in China. This repository includes Alt

Chen Yijun 27 May 9, 2022
A template for creating new MOLA modules

mola-foo Write me! Provided MOLA modules: Foo, type Foo. Build and install Refer to the root MOLA repository. Docs and examples See this package page

The MOLA SLAM framework 7 Oct 23, 2021
New linux driver and tools for RME HDSPe sound cards and extension modules

snd-hdspe New linux kernel ALSA driver for RME HDSPe MADI / AES / RayDAT / AIO and AIO Pro sound cards and extension modules. In addition to the funct

Philippe Bekaert 20 Jun 29, 2022
A sample project for building Zygisk modules

Developing Zygisk Modules This repository hosts a template zygisk module for developers to start developing Zygisk modules. Before developing Zygisk m

John Wu 187 Jun 24, 2022
Template library and blog that explain how JSI modules are built from scratch in React Native

react-native-jsi-template This is an example library that explains how anyone can build jsi modules from scratch in React Native. This code is written

Ammar Ahmed 104 Jun 18, 2022
CMake-based build system for node.js native modules

Node CMake A CMake-based build system for Node.js native modules, for CMake >= v3.1. Newly rewritten for 2.0! New Features Drop-in execution compatibi

Colin Taylor 74 Jun 6, 2022
CMake modules for common applications related to computer graphics

cgcmake CMake modules for common applications related to computer graphics Sample Usage Maya Project |-- CMakeLists.txt |-- cgcmake |-- modules

Chad Vernon 112 Jun 23, 2022
Generic embedded C driver to work with Sensirion's SEN5x environmental sensor modules via I2C

Sensirion Embedded I2C SEN5x Driver This is a generic embedded driver for the Sensirion SEN5x sensor modules. It enables developers to communicate wit

Sensirion AG 3 May 13, 2022
The SPIR-V Tools project provides an API and commands for processing SPIR-V modules.

SPIR-V Tools Overview The SPIR-V Tools project provides an API and commands for processing SPIR-V modules. The project includes an assembler, binary m

The Khronos Group 756 Jun 28, 2022
jai programming language starter kit (minimal modules)

jai starter kit modules jai programming language starter kit (minimal modules) Modules List sk_Window : minimal window creation and input module. sk_B

null 4 Jan 20, 2022