Finch/tests/boot.sh
Nexoniarz 04d5486477 boot: structs by value (System V), addr(fn) callbacks -- the compiler builds itself
C structs travel as C passes them: up to 16 bytes in INTEGER/SSE registers per eightbyte,
bigger ones byval and through sret, out-of-registers structs on the stack. addr(fn) makes a
C-callable thunk that rebuilds the arguments. With those, the self-hosted compiler can
compile boot/cimport.fch, which calls libclang with CXCursor and CXString by value and gives
it callbacks -- so it compiles itself.

boot.sh: stage 2 and stage 3 are identical (57904 lines of IR); 22 passed, 0 failed,
0 skipped. Only x86-64 System V so far; Windows and ARM64 conventions are still in C++.
2026-10-25 19:47:20 +01:00

53 lines
2 KiB
Shell
Executable file

#!/usr/bin/env bash
# The self-hosted compiler (boot/) must
# 1. build itself: stage1 (built by build/finch) -> stage2 -> stage3, with identical IR
# 2. give the same output as build/finch on every test in tests/run it supports
set -u
cd "$(dirname "$0")/.."
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
build/finch build boot/main.fch -o "$tmp/boot1" || exit 1
# Stage 2: the compiler built by itself. Until it can read the C headers its own source imports
# (libclang's), this stage can't be made, and the tests below run on stage 1 instead.
boot=$tmp/boot1
if "$tmp/boot1" build boot/main.fch -o "$tmp/boot2" > "$tmp/stage2.err" 2>&1; then
"$tmp/boot1" ir boot/main.fch > "$tmp/stage2.ll"
"$tmp/boot2" ir boot/main.fch > "$tmp/stage3.ll"
if cmp -s "$tmp/stage2.ll" "$tmp/stage3.ll"; then
echo "bootstrap: stage 2 and stage 3 are identical ($(wc -l < "$tmp/stage3.ll") lines of IR)"
boot=$tmp/boot2
else
echo "bootstrap: FAILED, stage 2 and stage 3 differ"
exit 1
fi
else
echo "bootstrap: not yet -- stage 1 can't build itself: $(head -1 "$tmp/stage2.err")"
fi
pass=0; failed=0; skipped=0
for f in tests/run/*.fch; do
grep -q "^fn main" "$f" || continue
# the bootstrap compiler doesn't do C structs by value
# (checked in the test and in the modules it imports)
files="$f"
for m in $(sed -n 's/^import \([a-z_]*\)$/\1/p' "$f"); do files="$files tests/run/$m.fch"; done
input="${f%.fch}.in"
[ -f "$input" ] || input=/dev/null
if ! "$boot" build "$f" -o "$tmp/prog" > "$tmp/err" 2>&1; then
failed=$((failed + 1))
echo "FAIL (compile) $f"
head -3 "$tmp/err"
continue
fi
got=$("$tmp/prog" < "$input" 2>&1)
if [ "$got" == "$(cat "${f%.fch}.out")" ]; then
pass=$((pass + 1))
else
failed=$((failed + 1))
echo "FAIL (output) $f"
diff <(echo "$got") "${f%.fch}.out" | head -10
fi
done
echo "self-hosted compiler: $pass passed, $failed failed, $skipped skipped (features it doesn't have)"
[ $failed -eq 0 ]