Essential insights from Hacker News discussions

c4wa – C compiler for Web Assembly

Here's a summary of the themes from the Hacker News discussion:

WebAssembly Code Quality and Optimization

A significant point of discussion revolves around the quality of WebAssembly (Wasm) code generated by different tools, and the role of compiler optimizations. The initial comparison of c4wa with Emscripten is debated, with users pointing out that optimization flags are crucial for achieving competitive code size and performance.

  • comex argues that the comparison likely neglected optimization: "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."
  • soegaard suggests that for tools aiming to preserve the original C code structure, disabling advanced optimizations might be an intended design choice: "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."
  • ncruces also emphasizes the importance of optimization for size and performance, mentioning tools like wasm-ctor-eval and wasm-opt.

Support for Freestanding WebAssembly Modules

Several users expressed interest in and support for tools and approaches that enable WebAssembly to run in a "freestanding" manner, independent of large runtimes like Emscripten or the full WASI specification. This is particularly relevant for custom Wasm runtimes and specific application scenarios where a minimal footprint is desired.

  • apitman is a strong advocate for this: "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ِ to be able to define your own API to handle the actually operations in the host/module interface."
  • Later, apitman reiterates this point when discussing WASI SDK: "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."
  • pyrolistical mentions Zig as another option for this use case: "Zig can also compile to free standing wasm".

Alternative Compilers and Toolchains for WebAssembly

The discussion highlights various C/C++ compilers and languages that can target WebAssembly, offering alternatives to Emscripten and showcasing different approaches to Wasm compilation and runtime integration.

  • s-macke notes the capabilities of Clang: "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]."
  • ncruces suggests minimal libc implementations for smaller Wasm binaries when targeting WASI, indicating flexibility within the WASI ecosystem: "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."
  • pyrolistical provides an example of Zig for freestanding Wasm with a link to a canvas project: "Zig can also compile to free standing wasm" and later provides the example: "https://github.com/Pyrolistical/zig-wasm-canvas".

c4wa Specifics and Limitations

Comments are also directed at c4wa itself, touching on its dependencies, feature support, and the readability of its generated code.

  • The requirement for Java 11 is seen as a drawback by some for self-compilation: lioeters states: "> 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."
  • The limited support for modern C standards (like C99) is noted as a restriction: teo_zero points out: "> 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!"
  • The impact of volatile semantics in Wasm is questioned due to the single-threaded nature of the runtime: b0a04gl muses: "> 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"
  • The readability of generated code, in general, is a concern: keysdev comments on the large number of local variables in generated code: "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"

WebAssembly Emulators and GUI Applications

A tangential but interesting thread arises about the use of WebAssembly emulators, with a desire for them to run more than just games, specifically normal GUI applications.

  • apitman asks about GUI applications: "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?" This question is prompted by appreciating an emulator example shared earlier.