interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / tests / examples.sh

262 lines · 11628 bytes Raw Blame History
1#!/bin/sh
2# tests/examples.sh OMC_A64 OMC_X86 [QEMU_A64]
3#
4# Compiles and runs every worked example of the Omega harness (harness/examples/*.om and
5# harness/skills/*/examples/*.om), and this repository's compiler regressions (tests/regress/*.om),
6# on two targets, against the same NAME.expect files that harness/examples/gate.sh reads, with the
7# same meaning:
8# compile N required; the compiler's exit status
9# stderr LINE required when N is not 0; the compiler's whole stderr is exactly LINE
10# exit N required when N is 0; the program's exit status
11# input dir optional; the compiler reads a directory instead of NAME.om (error 1)
12# output full optional; the compiler writes to /dev/full (error 2)
13# Optional fixtures: NAME.stdout (exact stdout bytes, absent means empty), NAME.stdin (piped in),
14# NAME.args (one argument per line), NAME.env (one KEY=VALUE per line). Every program runs with
15# `env -i` plus NAME.env, in its own empty scratch directory, under a 20 s timeout, and must write
16# nothing to stderr. A compile that is not expected to succeed is never run.
17#
18# Unlike gate.sh this runner pins no compiler hash: the compilers are the ones the build grew from
19# the seed, and the build checks their hashes itself.
20#
21# Targets:
22# a64 OMC_A64 (an AArch64 omc) compiles the unit as written; the AArch64 program runs.
23# On an AArch64 host both run directly; elsewhere both run under QEMU_A64
24# (default qemu-aarch64-static). Without it every a64 case is NOT_RUN.
25# x86 OMC_X86 (an x86-64 omc) compiles `target x86linux`, a newline, then the unit; the
26# x86-64 program runs. On an x86-64 host both run natively; elsewhere both run under
27# qemu-x86_64(-static) when present, otherwise every x86 case is NOT_RUN.
28# Line numbers: the prepended target line makes the x86 compiler report every source
29# line one higher, so each `omc: line M: error K` line of an x86 compile is compared as
30# `omc: line M-1: error K` (line 0, no line read, stays 0). A unit that selects its own
31# mode at byte 0 (`target `, `ir`, `check`, `next`, `om2 fmt`) cannot take a second
32# selector, so the x86-hosted compiler reads it unchanged and no line shift applies; if
33# such a unit is expected to compile, its output is not an x86 program and the run is
34# NOT_RUN. An `input dir` case also reads the directory unchanged.
35#
36# tests/examples.known, optional: lines `TARGET NAME reason` (NAME as printed, e.g.
37# examples/hello) for confirmed divergences. A listed case that fails prints KNOWN; one that
38# passes prints FIXED and fails the run, so the list is pruned; a line naming no case fails too.
39#
40# EXAMPLES_ONLY=a64 or EXAMPLES_ONLY=x86 runs one target. EXAMPLES_ROOT=DIR reads DIR/examples/*.om
41# and DIR/skills/*/examples/*.om instead of the harness and tests/regress (default harness/; tests/omc
42# holds the examples of compiler words that are not in the imported harness). Exit 0 when nothing FAILs,
43# 1 on any FAIL or FIXED, 2 on a usage error.
44set -u
45
46usage() { echo "usage: sh $0 OMC_A64 OMC_X86 [QEMU_A64]" >&2; exit 2; }
47[ "$#" -ge 2 ] && [ "$#" -le 3 ] || usage
48
49abspath() {
50 [ -f "$1" ] || { echo "examples: no such file: $1" >&2; exit 2; }
51 printf '%s/%s\n' "$(cd "$(dirname "$1")" && pwd)" "$(basename "$1")"
52}
53A64=$(abspath "$1")
54X86=$(abspath "$2")
55QA=${3:-qemu-aarch64-static}
56ONLY=${EXAMPLES_ONLY:-}
57case $ONLY in ''|a64|x86) ;; *) usage ;; esac
58
59here=$(cd "$(dirname "$0")" && pwd)
60root=${EXAMPLES_ROOT:-$(dirname "$here")/harness}
61root=$(cd "$root" 2>/dev/null && pwd) || { echo "examples: no directory ${EXAMPLES_ROOT:-harness}" >&2; exit 2; }
62# The compiler regressions run with the harness only, not with another EXAMPLES_ROOT.
63if [ -z "${EXAMPLES_ROOT:-}" ]; then regress=$here/regress; else regress=$here/regress.none; fi
64known=$here/examples.known
65[ -d "$root/examples" ] || { echo "examples: no $root/examples" >&2; exit 2; }
66
67host=$(uname -m)
68# arun / xrun: the emulator that runs that target's binaries on this host ('' = run directly);
69# anot / xnot: why that target cannot run here ('' = it can).
70arun= anot= xrun= xnot=
71case $host in
72 aarch64|arm64) ;;
73 *) arun=$(command -v "$QA" 2>/dev/null) || { arun=; anot="no $QA on this $host host"; } ;;
74esac
75case $host in
76 x86_64|amd64) ;;
77 *) xrun=$(command -v qemu-x86_64-static 2>/dev/null || command -v qemu-x86_64 2>/dev/null) ||
78 { xrun=; xnot="no qemu-x86_64 on this $host host"; } ;;
79esac
80
81work=$(mktemp -d)
82trap 'rm -rf "$work"' EXIT HUP INT TERM
83
84a_pass=0 a_fail=0 a_not=0 a_known=0
85x_pass=0 x_fail=0 x_not=0 x_known=0
86
87# report TGT NAME VERDICT REASON [raw]: apply tests/examples.known (unless raw), print one line, count it
88report() {
89 tgt=$1 nm=$2 verdict=$3 why=$4
90 listed=0
91 if [ "${5:-}" != raw ] && [ -f "$known" ] && awk -v t="$tgt" -v n="$nm" '$1 == t && $2 == n { f = 1 } END { exit !f }' "$known"; then
92 listed=1
93 echo "$tgt $nm" >> "$work/known-hit"
94 fi
95 if [ "$listed" = 1 ] && [ "$verdict" = FAIL ]; then
96 verdict=KNOWN
97 elif [ "$listed" = 1 ] && [ "$verdict" = PASS ]; then
98 verdict=FIXED why="passes now; remove it from tests/examples.known"
99 fi
100 if [ -n "$why" ]; then printf '%s %s %s: %s\n' "$verdict" "$tgt" "$nm" "$why"; else printf '%s %s %s\n' "$verdict" "$tgt" "$nm"; fi
101 case $tgt.$verdict in
102 a64.PASS) a_pass=$((a_pass + 1)) ;;
103 a64.NOT_RUN) a_not=$((a_not + 1)) ;;
104 a64.KNOWN) a_known=$((a_known + 1)) ;;
105 a64.*) a_fail=$((a_fail + 1)) ;;
106 x86.PASS) x_pass=$((x_pass + 1)) ;;
107 x86.NOT_RUN) x_not=$((x_not + 1)) ;;
108 x86.KNOWN) x_known=$((x_known + 1)) ;;
109 x86.*) x_fail=$((x_fail + 1)) ;;
110 esac
111}
112
113# with EMU CMD...: run CMD, under the emulator EMU when it is not empty
114with() {
115 e=$1; shift
116 if [ -n "$e" ]; then "$e" "$@"; else "$@"; fi
117}
118
119# one TGT SRC: compile and run one example for one target, then report it
120one() {
121 tgt=$1 src=$2
122 base=${src%.om}
123 nm=${base#"$root"/}
124 nm=${nm#"$here"/}
125 if [ "$tgt" = a64 ]; then omc=$A64 emu=$arun not=$anot; else omc=$X86 emu=$xrun not=$xnot; fi
126 if [ -n "$not" ]; then report "$tgt" "$nm" NOT_RUN "$not"; return; fi
127 if [ ! -f "$base.expect" ]; then report "$tgt" "$nm" FAIL "no $nm.expect"; return; fi
128 want_compile= want_stderr= want_exit= input=file output=file
129 while IFS= read -r line || [ -n "$line" ]; do
130 case $line in
131 "compile "*) want_compile=${line#compile } ;;
132 "stderr "*) want_stderr=${line#stderr } ;;
133 "exit "*) want_exit=${line#exit } ;;
134 "input dir") input=dir ;;
135 "output full") output=full ;;
136 "") ;;
137 *) want_compile=invalid ;;
138 esac
139 done < "$base.expect"
140 case $want_compile in
141 ''|invalid|*[!0-9]*) report "$tgt" "$nm" FAIL "malformed $nm.expect"; return ;;
142 esac
143 if [ "$want_compile" != 0 ] && [ -z "$want_stderr" ]; then
144 report "$tgt" "$nm" FAIL "$nm.expect has no stderr line"; return
145 fi
146 if [ "$want_compile" = 0 ]; then
147 case $want_exit in
148 ''|*[!0-9]*) report "$tgt" "$nm" FAIL "$nm.expect has no exit line"; return ;;
149 esac
150 fi
151
152 d=$work/$tgt/$(printf '%s' "$nm" | tr / _)
153 mkdir -p "$d/run"
154 in=$src
155 shift_lines=0
156 if [ "$input" = dir ]; then
157 in=$d/run
158 elif [ "$tgt" = x86 ]; then
159 if head -c 7 "$src" | grep -q '^target ' ||
160 head -n 1 "$src" | grep -Eq '^(ir|check|next|om2 fmt)([ ]|$)'; then
161 if [ "$want_compile" = 0 ]; then
162 report "$tgt" "$nm" NOT_RUN "the unit selects its own mode at byte 0, so its program is not x86"
163 return
164 fi
165 else
166 { printf 'target x86linux\n'; cat "$src"; } > "$d/unit.om"
167 in=$d/unit.om
168 shift_lines=1
169 fi
170 fi
171 out=$d/p
172 [ "$output" = full ] && out=/dev/full
173 with "$emu" "$omc" < "$in" > "$out" 2> "$d/cerr.raw"
174 rc=$?
175 if [ "$shift_lines" = 1 ]; then
176 awk '/^omc: line [0-9]+: error [0-9]+$/ { n = $3 + 0; if (n > 0) n = n - 1; print "omc: line " n ": error " $5; next } { print }' \
177 "$d/cerr.raw" > "$d/cerr"
178 else
179 cp "$d/cerr.raw" "$d/cerr"
180 fi
181 if [ "$rc" != "$want_compile" ]; then
182 report "$tgt" "$nm" FAIL "compile exit $rc, expected $want_compile: $(head -c 200 "$d/cerr")"
183 return
184 fi
185 if [ "$want_compile" != 0 ]; then
186 if printf '%s\n' "$want_stderr" | cmp -s - "$d/cerr"; then
187 report "$tgt" "$nm" PASS ""
188 else
189 report "$tgt" "$nm" FAIL "compiler stderr '$(head -c 200 "$d/cerr")', expected '$want_stderr'"
190 fi
191 return
192 fi
193 if [ -s "$d/cerr" ]; then
194 report "$tgt" "$nm" FAIL "compiler stderr not empty: $(head -c 200 "$d/cerr")"
195 return
196 fi
197
198 chmod +x "$d/p"
199 (
200 cd "$d/run" || exit 125
201 set --
202 if [ -f "$base.env" ]; then
203 while IFS= read -r kv || [ -n "$kv" ]; do set -- "$@" "$kv"; done < "$base.env"
204 fi
205 [ -n "$emu" ] && set -- "$@" "$emu"
206 set -- "$@" "$d/p"
207 if [ -f "$base.args" ]; then
208 while IFS= read -r a || [ -n "$a" ]; do set -- "$@" "$a"; done < "$base.args"
209 fi
210 if [ -f "$base.stdin" ]; then
211 cat "$base.stdin" | timeout 20 env -i "$@" > "$d/out" 2> "$d/rerr"
212 else
213 timeout 20 env -i "$@" < /dev/null > "$d/out" 2> "$d/rerr"
214 fi
215 )
216 rc=$?
217 want_out=/dev/null
218 [ -f "$base.stdout" ] && want_out=$base.stdout
219 if [ "$rc" != "$want_exit" ]; then
220 report "$tgt" "$nm" FAIL "exit $rc, expected $want_exit"
221 elif ! cmp -s "$want_out" "$d/out"; then
222 report "$tgt" "$nm" FAIL "stdout differs from $(basename "$want_out"): $(od -An -c "$d/out" | tr -s ' ' | tr -d '\n' | cut -c1-200)"
223 elif [ -s "$d/rerr" ]; then
224 report "$tgt" "$nm" FAIL "program stderr not empty: $(head -c 200 "$d/rerr")"
225 else
226 report "$tgt" "$nm" PASS ""
227 fi
228}
229
230: > "$work/known-hit"
231for tgt in a64 x86; do
232 [ -z "$ONLY" ] || [ "$ONLY" = "$tgt" ] || continue
233 # An expectation without its program is a silent loss of coverage, so it fails.
234 for f in "$root"/examples/*.expect "$root"/skills/*/examples/*.expect "$regress"/*.expect; do
235 [ -e "$f" ] || continue
236 [ -e "${f%.expect}.om" ] || report "$tgt" "$(printf '%s' "${f%.expect}" | sed "s|^$root/||; s|^$here/||")" FAIL "expect file without its .om"
237 done
238 for src in "$root"/examples/*.om "$root"/skills/*/examples/*.om "$regress"/*.om; do
239 [ -e "$src" ] || continue
240 one "$tgt" "$src"
241 done
242done
243
244# A known-divergence line that named no case is stale.
245if [ -f "$known" ]; then
246 while read -r t n rest || [ -n "$t" ]; do
247 case $t in ''|'#'*) continue ;; esac
248 [ -z "$ONLY" ] || [ "$ONLY" = "$t" ] || continue
249 if ! grep -qx "$t $n" "$work/known-hit"; then
250 report "$t" "$n" FAIL "tests/examples.known names a case that does not exist" raw
251 fi
252 done < "$known"
253fi
254
255total=$((a_pass + a_fail + a_not + a_known + x_pass + x_fail + x_not + x_known))
256if [ "$total" = 0 ]; then echo "EXAMPLES none found under $root"; exit 1; fi
257if [ -f "$known" ]; then
258 echo "EXAMPLES a64 pass=$a_pass fail=$a_fail notrun=$a_not known=$a_known x86 pass=$x_pass fail=$x_fail notrun=$x_not known=$x_known"
259else
260 echo "EXAMPLES a64 pass=$a_pass fail=$a_fail notrun=$a_not x86 pass=$x_pass fail=$x_fail notrun=$x_not"
261fi
262[ "$a_fail" = 0 ] && [ "$x_fail" = 0 ]