flan/spike/backend/run.sh
Joseph Ferano c73f05b052 Disassembly on request, and it is a debugging aid rather than evidence
SPIKE_DISASM=1 objdumps the exact buffers that ran. Kept behind a flag and
kept out of the pass/fail path: a disassembly that reads correctly beside a
function answering the wrong number is the normal outcome of hand-encoding.
2026-09-13 09:15:50 +07:00

49 lines
1.8 KiB
Bash

#!/usr/bin/env bash
# The spike, end to end: the real frontend produces a Tast, x86.ml turns it
# into bytes, the bytes go into an mmap, and the mmap gets called.
#
# Driven by hand with ocamlfind and clang against the flan.cmxa dune already
# builds, exactly as spike/embed does and for the same reason: nothing under
# spike/ is wired into the build, so there is no dune file here and `dune test`
# cannot see any of it.
set -u
here=$(cd "$(dirname "$0")" && pwd)
root=$(cd "$here/../.." && pwd)
cd "$root" || exit 1
dune build --root . lib/flan.cmxa 2>&1 | head -20
out=$(mktemp -d); trap 'rm -rf "$out"' EXIT
# The C stubs. -O2 on purpose: spike_probe_align's aligned load only becomes a
# real movaps with optimisation on, and an alignment bug that only shows up in
# a release build is the one this is looking for.
clang -O2 -c -I"$(ocamlopt -where)" "$here/jit_stubs.c" -o "$out/jit_stubs.o" || exit 1
ocamlfind ocamlopt -thread -package unix,threads.posix -linkpkg \
-I "$root/_build/default/lib/.flan.objs/byte" \
-I "$root/_build/default/lib/.flan.objs/native" \
-I "$out" -I "$here" \
-o "$out/spike" \
"$root/_build/default/lib/flan.cmxa" \
-cclib -rdynamic -ccopt -L"$root/_build/default/lib" \
"$out/jit_stubs.o" \
"$here/x86.ml" "$here/driver.ml" 2>&1 | head -40
test -x "$out/spike" || { echo "build failed"; exit 1; }
SPIKE_DUMP=$out "$out/spike" "$here/probe.flan"
rc=$?
# Disassembly on request. objdump over the raw buffer, which is what to reach
# for when a function answers the wrong number -- not what proves it answers
# the right one.
if [ "${SPIKE_DISASM:-}" = 1 ]; then
for f in "$out"/*.bin; do
echo; echo "== $(basename "$f" .bin)"
objdump -D -b binary -m i386:x86-64 -M intel "$f" | tail -n +7
done
fi
echo "exit: $rc"
exit $rc