Hacker News new | ask | show | jobs
by DrNosferatu 559 days ago
Why not use WASM to bootstrap your compiler?
2 comments

Unlike any real machine (and most virtual ones), WASM is impossible to target with a simple single-pass C compiler. The reducible control flow requirement means you have to build a full control-flow graph and do graph algorithms to it before you can start emitting code. So IMO it makes for a bad starting point.
If you want to have "fun", generate code in a WASM-representation of Hoare's WHILE language, i.e. a program that only consists of a single outer while loop (in WASM a "loop" block), and conditionally decide for every single instruction in the loop whether it should be executed during a given pass.

I think that could be done with a single-pass C compiler. In a very trivial (and terrible) case, you could keep a running "line_counter" variable that every statement in the loop is predicated with, which either gets incremented at the end of the loop, or set to an arbitrary value for representing branches.

It would of course be a horrible (but very amusing) mess, and rather enforces your actual point.

Reminds me of what I understand from the „Usagi Electric“ Bendix rotating drum -memory-computer execution :)
Elaborate, why use WASM?
Like Zig did - then you could bootstrap in whatever environment that can interpret WASM.

PS: Why all the downvotes? Just another perspective.