Files
zig/stage0
Motiejus bcf6dcdf71 stage0: add analyzeMemoizedStateC during module loading + fix CG builtin ns
Three fixes toward closing the 64-entry IP gap for return_integer.zig:

1. Call analyzeMemoizedStateC() after the full module chain is loaded.
   This creates CallingConvention, Signedness, AddressSpace, and other
   builtin type entries that the Zig compiler creates during its
   analyzeMemoizedState(.main) call chain.

2. Fix ensureNavValUpToDate CG builtin namespace collision: when
   std/builtin.zig and the CG builtin module share the same namespace
   index, check has_zir to distinguish them. Without this, builtins
   like CallingConvention (which have ZIR in std/builtin.zig) were
   incorrectly routed to the CG builtin resolution path and returned
   IP_INDEX_NONE.

3. Limit memoized state resolution to the first 6 direct std.builtin
   declarations (Signedness through SourceLocation). Skip Type and its
   21 children (indices 15-35) — the C sema's resolveTypeFullyC is too
   aggressive for these complex nested types.

Gap reduced from 64 to 3 entries. Remaining gap is from module chain
entry ordering (C sema creates struct types and ptr_navs in batches,
reference interleaves them as pairs).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-01 19:24:42 +00:00
..
2026-02-20 12:33:48 +02:00
2026-02-26 00:45:27 +02:00
2026-02-17 10:56:11 +00:00

About

zig0 aspires to be an interpreter of zig 0.15.2 written in C.

Except for the lexer (written by hand by yours truly), it's been written by an LLM.

The goal of stage0 is to be able to implement enough zig to be able to build zig1.wasm. For that we need:

  1. Lexer: DONE, written by hand by yours truly in late 2024.
  2. Parser: DONE, written mostly by an LLM.
  3. AstGen: DONE, written fully by an LLM.
  4. Sema: in progress.

Testing

Quick test:

zig build fmt-zig0 test-zig0

Static analysis (takes a while, run separately):

zig build lint-zig0

More elaborate (tries all compilers + static analysis + ReleaseSafe):

zig build all-zig0 -Doptimize=ReleaseSafe

Most elaborate, takes >10m:

zig build all-zig0 -Doptimize=ReleaseSafe -Dvalgrind |& grep -v Warning

Debugging tips

Test runs infinitely? Build the test program executable:

$ zig build test-zig0 -Dzig0-no-exec

And then run it, capturing the stack trace:

gdb -batch \
    -ex "python import threading; threading.Timer(1.0, lambda: gdb.post_event(lambda: gdb.execute('interrupt'))).start()" \
    -ex run \
    -ex "bt full" \
    -ex quit \
    zig-out/bin/test

You are welcome to replace -ex "bt full" with anything other of interest.

Float Handling

Float literals are parsed with strtold() (C11 standard, portable). On x86-64 Linux, long double is 80-bit extended precision (63 fraction bits).

When a float doesn't round-trip through f64, it's emitted as f128 (ZIR float128 instruction). The 80-bit extended value is converted to IEEE 754 binary128 encoding by bit manipulation — both formats share the same 15-bit exponent with bias 16383. The top 63 of binary128's 112 fraction bits come from the 80-bit value; the bottom 49 are zero-padded.

This means float128 literals lose ~49 bits of precision compared to the upstream Zig implementation (which uses native f128). This is acceptable because stage0 is a bootstrap tool — the real Zig compiler re-parses all source with full f128 precision in later stages. The test comparison mask in astgen_test.zig skips float128 payloads to account for this.

Previous approach used __float128/strtof128 (GCC/glibc extensions) for full precision, but these are not portable to TCC and other C11 compilers.