4 ms·
> We provide a minimal WASI interpreter implementation that is built from C source, and then used to translate the Zig self-hosted compiler source code into C c
by shp0ngle 4y ago
> We provide a minimal WASI interpreter implementation that is built from C source, and then used to translate the Zig self-hosted compiler source code into C code. The C code is then compiled and linked, again by the system C compiler, into a stage2 binary. The stage2 binary can then be used repeatedly with zig build to build from source from that point on.
Nope, no matter how many times I read this, I’m still lost.
But then I never needed to care about VMs, compilers and bootstraps.
- Arnavion 4y agoRead the "To summarize:" section a little after the part you quoted.
- gavinray 4y agoOkay so we start from some C source code, and we build an interpreter for WASM + WASI from that (WASM that has access to system calls) C source -> WASM interpreter w/ system access Now we can take the Zig self-hosted compiler (the one in .zig), which has been compiled to .wasm/.wasi files. Since we have an interpreter for those now, we can do this: Zig compiler as .wasi instead of .exe --> WASM interpreter --> Zig's "translate-to-c" function, for the .zig file sources of the Zig compiler E.G. $ run-webassembly "zig-compiler-as-wasm.wasi" --translate-c <source code to zig compiler> At this point, we have the Zig compiler as .c files. Now you can use GCC/clang or whatnot, to build a regular binary for the compiler Output of Zig's "translate-to-c" from previous step --> GCC/clang --> Zig compiler but NOT AS WASM, as a regular binary
- MrBuddyCasino 4y agohow do you compile the Zig self-hosted compiler to wasm?
- TUSF 4y agoDefen explained explains this well here: https://news.ycombinator.com/item?id=33914969 https://news.ycombinator.com/item?id=33914969
- shp0ngle 4y agoAh. OK. I still don't understand why did Zig need to write their own WASM interpreter in C. There is no already existing interpreter of WASM? Also was that WASM interpreter written in C, or in Zig and compiled to C? Wait it might be covered in the article. I will read once more, slowly...
- TUSF 4y agoThey wanted you to only have to use what's in the git repo itself, plus whatever C compiler is on your system. Thus, a basic WASM→C converter was written, and kept minimal to save on space, because it's only meant to work on this one binary.
- 95014_refugee 4y agoI'm in the same boat. This seems like an enormous amount of work to avoid archiving compiler binaries for a baseline architecture, and supporting cross-compilation.
- TUSF 4y agoWeird take. This wasn't gone to avoid supporting cross-compilation—Zig can already cross-compile, even without the LLVM backend. This was to avoid having to provide a binary for every individual OS and Architecture combination that Zig supports. Using a VM that is agnostic to the OS or architecture it's running on means that you only need to provide a single binary, and in this case WASM+WASI was chosen.