Files

90 lines
3.0 KiB
Bash
Executable File

#!/bin/sh
# M4 unsigned runtime gate: compile unsigned.c at -O0, link with GSPA/GSPLNK,
# run in MAME, and check every signed/unsigned predicate marker. The linked
# assembly must use the real condition mnemonics for each function.
set -eu
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
WORK=${M4_UNSIGNED_WORK:-$ROOT/build/m4-unsigned}
fail() {
echo "m4 unsigned: FAIL: $*" >&2
exit 1
}
M4_WORK="$WORK" "$ROOT/scripts/m4/run.sh" || fail "M4 unsigned run failed"
ASM=$WORK/m4.asm
[ -f "$ASM" ] || fail "linked assembly not found: $ASM"
# Stop at the next function label (a column-0 label that is not an LBB block
# label), so a following local function cannot leak into this slice.
body() { # body FILE FUNC
awk -v want="_$2:" '
$1 == want { infn = 1; next }
infn {
if ($0 ~ /^[A-Za-z_][A-Za-z0-9_]*:/ && $0 !~ /^LBB/) exit
print
}
' "$1"
}
TMP=$(mktemp -d "${TMPDIR:-/tmp}/m4-unsigned.XXXXXX")
trap 'rm -rf "$TMP"' EXIT HUP INT TERM
EXPECTED="M4: le=1 ule=0 sum_le=55 ge=1 uge=0 gt=0 ugt=1 ne=1 lt=0 ult=1"
grep -Fqx "$EXPECTED" "$WORK/mame.log" \
|| { grep -E '^M4: ' "$WORK/mame.log" >&2 || true; fail "runtime markers did not match: $EXPECTED"; }
echo " ok runtime markers: $EXPECTED"
for fn in le ule sum_le ge uge gt ugt ne lt ult; do
body "$ASM" "$fn" > "$TMP/$fn.s"
[ -s "$TMP/$fn.s" ] || fail "linked assembly has no _$fn body"
done
check_branch() { # check_branch FUNC JA<cc> WHAT
# M5's JR relaxation selects the one-word relative form for an in-range
# branch, keeping the condition code. Accept either spelling but require
# exactly the same <cc> suffix.
_cond=${2#JA}
[ "$_cond" != "$2" ] \
|| fail "check_branch expects a JA<cc> mnemonic, got $2"
if grep -Eq "^[[:space:]]*J[AR]${_cond}[[:space:]]" "$TMP/$1.s"; then
echo " ok _$1 uses JA/JR$_cond ($3)"
return 0
fi
sed 's/^/ /' "$TMP/$1.s" >&2
fail "_$1 does not use JA/JR$_cond ($3)"
}
reject_branch() { # reject_branch FUNC ERE WHAT
# The ERE names the absolute form; allow the relative prefix as well. The
# condition itself is unchanged, so a wrong signed/unsigned form still
# fails.
_re=$(printf '%s\n' "$2" | sed 's/^JA/J[AR]/')
if grep -Eq "^[[:space:]]*${_re}[[:space:]]" "$TMP/$1.s"; then
sed 's/^/ /' "$TMP/$1.s" >&2
fail "_$1 uses the wrong condition $2 ($3)"
fi
}
check_branch le JALE "signed <="
check_branch ule JALS "unsigned <="
check_branch sum_le JAGT "signed i > n loop exit"
check_branch ge JAGE "signed >="
check_branch uge JAHS "unsigned >="
check_branch gt JAGT "signed >"
check_branch ugt JAHI "unsigned >"
check_branch ne JANE "!="
check_branch lt JALT "signed <"
check_branch ult JALO "unsigned <"
# The unsigned predicates must not silently select a signed branch form.
reject_branch ule 'JA(LT|LE|GT|GE)' "unsigned <="
reject_branch uge 'JA(LT|LE|GT|GE)' "unsigned >="
reject_branch ugt 'JA(LT|LE|GT|GE)' "unsigned >"
reject_branch ult 'JA(LT|LE|GT|GE)' "unsigned <"
reject_branch ne 'JAEQ' "!="
echo "m4 unsigned: PASS"