c4wa – C compiler for Web Assembly

by 90s_devon 6/24/25, 3:52 PMwith 17 comments
by lioeterson 6/28/25, 3:22 AM

> c4wa needs Java 11 or above

It sounded good until this part. Would have been nice if it were written in the subset of C that it supports, so it could compile the compiler to Wasm.

by comexon 6/28/25, 2:53 AM

In the comparison with Emscripten using wasm-decompile [1], the author appears to have forgotten to turn on optimization. Yes, if you run emcc with no -O option then you will get extremely bad generated code quality, similar to most C compilers. Add -O and you get nice and tight code similar to what c4wa outputs.

[1] https://github.com/kign/c4wa/blob/master/etc/doc/comparison....

by s-mackeon 6/28/25, 6:42 AM

clang can compile into wasm pretty well via the

--target=wasm32

option. It creates small binaries. My 16-Bit x86 emulator with BIOS and DOS emulation is under 100kB [0].

[0] https://github.com/s-macke/FSHistory

by keysdevon 6/28/25, 2:52 PM

> It's really, really difficult to recognize out original logic here. Among other things, whereas our original C code only has 2 local variables, this version has 29, not counting module-level globals.

Isnt that quite common now with machine generated code. Look at nim clientside js, I dear any js dev to try to navigate that.

The point is should we just trust the machine generated as it is and hope that the compiler/bundler just did a correct job, or just write clean and simple code

by Anduiaon 6/28/25, 5:43 AM

Last commit on Jan 29, 2022

by teo_zeroon 6/28/25, 6:53 AM

> here are some of the most commonly used features of C language NOT supported by c4wa. > [...] Almost all new features introduced in C99

At least, it doesn't require K&R syntax for functions!

by pyrolisticalon 6/28/25, 4:15 AM

Zig can also compile to free standing wasm

by apitmanon 6/28/25, 3:34 AM

This is actually pretty compelling to me. I think the more support for freestanding wasm modules the better.

I'm working on a custom wasm app runtime and I don't want to have to implement the entire API surface of Emscripten or WASI. The new component model is even more complex. I wish there was more tooling available for using C/Rust stdlib functions for things like reading files or opening a socket, but being able to define your own API to handle the actually operations in the host/module interface.

by b0a04glon 6/28/25, 10:39 AM

one wierd connection i realised later now, > volatile in c was for blocking reorders, forcing real mem reads, esp mmio or sync operations. wasm runs singlethreaded, no fences unless you go atomic. so compiler keeps volatile loads yeah, but wasm runtime ain't bound to order. it runs but doesn't mean anything. semantics gone. still compiles