Oh wow, really chuffed to see a blog post I wrote 4 years ago back on the front page of HN once again. It was a real rollercoaster, spending ages developing a talk, that I wasn't able to give due to a flight cancellation - then turning it into a blog post that lots of people seemed to like.<p>Hard work pays off in the end :-)
<p><pre><code> (block
(loop
[loop condition]
i32.eqz
[nested statements]
br_if 1
br 0)
)
</code></pre>
That's... not really the while loop, is it? That should've been something like this IMO<p><pre><code> (block
(loop
[loop condition]
i32.eqz
br_if 1
[nested statements]
br 0)
)
</code></pre>
Honestly, WASM's "br"/"br_if" semantics is quite wacky: for a block "br"/"br_if" mean "break", and for a loop they mean "continue". I understand that they didn't want to have two separate opcodes which would only make sense in certain contexts but could we at least have gotten two mnemonics for them? Having "break/break_if" for targeting a block and "continue/continue_if" for targeting a loop would reduce some confusion and I don't think there are any difficulties in either assembling or disaseembling that.<p>I've seen only about three toy-ish WASM interpreters and they all translate those unorthodox control flow instructions into something like goto's during the parsing stage. Why did the WASM authors did it this way, I don't know: it's just as well possible to give types to traditional asm-like GOTOs and labels.
If you're interested in this, there's also <a href="https://wasmgroundup.com/" rel="nofollow noreferrer">https://wasmgroundup.com/</a> currently in early access.<p>I've worked through the first couple of chapters and found it pretty well structured and helpful.
Discussed at the time:<p><i>Build Your Own WebAssembly Compiler</i> - <a href="https://news.ycombinator.com/item?id=21889614">https://news.ycombinator.com/item?id=21889614</a> - Dec 2019 (19 comments)
[2019] Author builds a compiler that _targets_ WebAssembly, not a compiler that _takes in_ WebAssembly. Interesting but I was hoping for the other side, a minimal AOT or JIT wasm --> machine code compiler.