Files
zig/stage0
Motiejus f4635b5ac0 sema: add wrapping ops, bool_not, float/int casts, bit counting, byte_swap
New ZIR handlers:
- addwrap/subwrap/mulwrap → AIR add_wrap/sub_wrap/mul_wrap
- negate_wrap → AIR sub_wrap(0, operand)
- bool_not → AIR not(bool, operand)
- float_cast → AIR fpext or fptrunc (width comparison)
- int_from_float, float_from_int → AIR ty_op casts
- clz, ctz, pop_count → AIR ty_op with computed result type
- byte_swap → AIR ty_op(operand_ty, operand)

Infrastructure improvements:
- semaCoerce: handle runtime int→int coercion via AIR intcast
- ret_node: use semaCoerce instead of coerceIntRef for full coercion
- Return type resolution: handle 2-instruction ptr_type bodies
- func_fancy: properly track ret_ty_body_len and ret_ty_ref_pos
- semaTypeOf: add wrapping ops, cmp, load, not, float/int casts, bit ops
- airDataRefSlots: fix LOAD/NOT classification (ty_op not un_op),
  add wrap ops, float/int casts, bit count ops
- zirAsShiftOperand: pass block for proper coercion support

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 14:31:45 +00:00
..
2026-02-20 12:33:48 +02:00
2026-02-18 22:49:36 +02:00
fmt
2026-02-20 10:30:47 +02:00
2026-02-17 10:56:11 +00:00
2026-02-19 20:58:30 +00:00
2026-02-14 00:03:26 +02:00

About

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

This is written with help from LLM:

  • Lexer:
    • Datastructures 100% human.
    • Helper functions 100% human.
    • Lexing functions 50/50 human/bot.
  • Parser:
    • Datastructures 100% human.
    • Helper functions 50/50.
    • Parser functions 5/95 human/bot.
  • AstGen: TBD.

Testing

Quick test:

./zig-out/bin/zig build fmt-zig0 test-zig0

Full test and static analysis with all supported compilers and valgrind (run before commit, takes a while):

./zig-out/bin/zig build all-zig0 -Dvalgrind

Debugging tips

Test runs infinitely? Build the test program executable:

$ ./zig-out/bin/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.

Rebuilding zig

If you need to rebuild zig-out/bin/zig for some reason, here's how:

~/code/zig-bootstrap/out-0.15.2/zig-x86_64-linux-musl-x86_64_v3/zig build \
    --zig-lib-dir lib/ -Dtarget=x86_64-linux-musl -Dcpu=x86_64_v3 \
    -Dstatic-llvm --search-prefix \
    $HOME/code/zig-bootstrap/out-0.15.2/x86_64-linux-musl-x86_64_v3/ \
    -Ddebug-extensions=true -Dlog=false -Doptimize=ReleaseSafe