#!/usr/bin/env bash
# runFileCheckTests.sh - run W65816 backend regression tests.
#
# Walks src/llvm/test/CodeGen/W65816/*.ll and for each:
#   - reads RUN: lines from the test header (lit-compatible syntax)
#   - executes them with %s -> the test path
#   - any non-zero exit fails the run.
#
# Why not lit: the in-tree llvm-mos build is configured with
# LLVM_INCLUDE_TESTS=OFF (saves ~5 min from incremental rebuilds and
# ~2 GB of test artifacts).  These regression tests are codegen-shape
# pins, not full lit-harness sweeps; FileCheck alone covers our needs.
#
# Usage:
#   scripts/runFileCheckTests.sh                # run all
#   scripts/runFileCheckTests.sh foo.ll bar.ll  # run named (relative to dir)

set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TEST_DIR="$PROJECT_ROOT/src/llvm/test/CodeGen/W65816"
LLC="$PROJECT_ROOT/tools/llvm-mos-build/bin/llc"
FILECHECK="$PROJECT_ROOT/tools/llvm-mos-build/bin/FileCheck"
NOT="$PROJECT_ROOT/tools/llvm-mos-build/bin/not"

[ -x "$LLC" ]       || { echo "missing $LLC" >&2; exit 2; }
[ -x "$FILECHECK" ] || { echo "missing $FILECHECK; build with 'ninja FileCheck not'" >&2; exit 2; }

if [ $# -gt 0 ]; then
    files=()
    for f in "$@"; do
        files+=("$TEST_DIR/$f")
    done
else
    mapfile -t files < <(find "$TEST_DIR" -maxdepth 1 -name '*.ll' | sort)
fi

pass=0
fail=0
failed=()
for f in "${files[@]}"; do
    [ -f "$f" ] || { echo "skip missing: $f"; continue; }
    name="$(basename "$f")"

    runs=$(grep -E '^[[:space:]]*;[[:space:]]*RUN:' "$f" | sed -E 's/^[[:space:]]*;[[:space:]]*RUN:[[:space:]]*//')
    if [ -z "$runs" ]; then
        echo "SKIP $name (no RUN: line)"
        continue
    fi

    ok=1
    while IFS= read -r line; do
        [ -z "$line" ] && continue
        cmd=${line//%s/$f}
        cmd=${cmd//llc/$LLC}
        cmd=${cmd//FileCheck/$FILECHECK}
        cmd=${cmd//not /$NOT }
        out=$(bash -c "$cmd" 2>&1) || {
            ok=0
            echo "FAIL $name"
            echo "  cmd: $cmd"
            echo "$out" | sed 's/^/  | /'
            break
        }
    done <<< "$runs"

    if [ $ok -eq 1 ]; then
        echo "PASS $name"
        pass=$((pass + 1))
    else
        fail=$((fail + 1))
        failed+=("$name")
    fi
done

echo
echo "==== W65816 FileCheck: $pass pass, $fail fail ===="
if [ $fail -gt 0 ]; then
    printf '  - %s\n' "${failed[@]}"
    exit 1
fi
