News for hackers. Stuff that matters.
Re:C compiler for Web Assembly (c4wa) by comex on Friday June 27, 2025 @10:53PM (#44402028)
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....

Re:C compiler for Web Assembly (c4wa) by soegaard on Saturday June 28, 2025 @06:43AM (#44403711)
I think, the author's goal was to produce wasm-code that had the same structure as the original C. Thus it makes sense to turn off the advanced optimizations.
Re:C compiler for Web Assembly (c4wa) by lioeters on Friday June 27, 2025 @11:22PM (#44402104)
> 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.

Re:C compiler for Web Assembly (c4wa) by apitman on Friday June 27, 2025 @11:34PM (#44402139)
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.

Re:C compiler for Web Assembly (c4wa) by pyrolistical on Saturday June 28, 2025 @12:15AM (#44402265)
Zig can also compile to free standing wasm
Re:C compiler for Web Assembly (c4wa) by jeden on Saturday June 28, 2025 @03:42AM (#44402918)
any example? I search wasm without js.
Re:C compiler for Web Assembly (c4wa) by whitehexagon on Saturday June 28, 2025 @05:01AM (#44403250)
I noticed this question yesterday, which includes the changed build options for freestanding.

https://ziggit.dev/t/wasm-index-out-of-bounds-only-in-debug-...

Re:C compiler for Web Assembly (c4wa) by pyrolistical on Saturday June 28, 2025 @04:27PM (#44407906)
Re:C compiler for Web Assembly (c4wa) by Anduia on Saturday June 28, 2025 @01:43AM (#44402513)
Last commit on Jan 29, 2022
Re:C compiler for Web Assembly (c4wa) by s-macke on Saturday June 28, 2025 @02:42AM (#44402726)
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

Re:C compiler for Web Assembly (c4wa) by ncruces on Saturday June 28, 2025 @06:11AM (#44403560)
And if you need parts of libc, you can use something like the below, for something more minimal than musl: https://github.com/Photosounder/MinQND-libc

Personally, although I do use wasi-sdk (clang) and the wasm32-unknown-wasi triple, the result can be basically free-standing, depending on what flags you use.

As a sibling post said, optimization matters, and using wasm-ctor-eval and wasm-opt also helps. But it's useful to keep full C semantics.

Re:C compiler for Web Assembly (c4wa) by s-macke on Saturday June 28, 2025 @06:51AM (#44403742)
Thanks. This is awesome. For the emulator I wrote my own 200 line libc implementation with a five line malloc routine [0]. Nice to see another implementation, which does a little bit more.

[0] https://github.com/s-macke/FSHistory/tree/master/src/wasm_li...

Re:C compiler for Web Assembly (c4wa) by apitman on Saturday June 28, 2025 @09:47AM (#44404669)
> Personally, although I do use wasi-sdk (clang) and the wasm32-unknown-wasi triple, the result can be basically free-standing, depending on what flags you use.

This is what I've been doing for my little non-WASI runtime. For my minimal programs I find only a few wasi exports are required, which don't appear to actually be used and I just stub them out. Would love to figure out how to get rid of the code depending on them entirely but it's lower priority.

Re:C compiler for Web Assembly (c4wa) by ncruces on Sunday June 29, 2025 @07:34AM (#44412263)
If it's possible that it's stuff that's being used at initialization time, wasm-ctor-eval might help.

Also, you probably want to build a reactor (-mexec-model=reactor) if that's not what you're already doing. A command will use WASI to read command-line arguments and environment.

But it's hard to tell without knowing the specific imports you're trying to get rid of.

Re:C compiler for Web Assembly (c4wa) by apitman on Saturday June 28, 2025 @10:10AM (#44404812)
That emulator is awesome. Especially cool about the easter egg.

I've seen a few of these WASM emulators and fantasy consoles, but they all seem to be focused on games. Are you aware of any that are designed to run normal GUI apps?

Re:C compiler for Web Assembly (c4wa) by s-macke on Sunday June 29, 2025 @06:01AM (#44411761)
Maybe WebVM is something you are looking for [0].

[0] https://github.com/leaningtech/webvm

Re:C compiler for Web Assembly (c4wa) by 90s_dev on Saturday June 28, 2025 @06:47PM (#44408805)
I tried for a few days to embed clang as a lib just to get it to compile to wasm and got so close and I might pick that project up again because I think it would be a really exciting and fun app.
Re:C compiler for Web Assembly (c4wa) by teo_zero on Saturday June 28, 2025 @02:53AM (#44402779)
> 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!

Re:C compiler for Web Assembly (c4wa) by b0a04gl on Saturday June 28, 2025 @06:39AM (#44403697)
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
Re:C compiler for Web Assembly (c4wa) by keysdev on Saturday June 28, 2025 @10:52AM (#44405104)
> 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