Files

257 lines
10 KiB
Bash
Executable File

#!/bin/sh
# M7.1 `.data` initializer gate: C globals through both the GSPA text path and
# the native TI-COFF object path, with MAME reading the field values and the
# exact raw object bytes.
#
# scripts/tests/m7/data.sh
#
# The C driver m7data.c defines:
# - a 64-bit scalar (0x1122334455667788, LSW first in memory);
# - the 1987 bitfield struct s (5 bytes: 8D ED CD AB 09);
# - a packed record <{ i8, i32, i64 }> (13 bytes);
# - an array of packed records (3 x 6 bytes);
# - a union whose active member is the 6-byte struct (8-byte allocation);
# - a pointer global, a +3-byte pointer addend, and a pointer field inside a
# packed record.
# i64arr.c adds an i64 array (Clang's ConstantArray<i64> form).
# oddptr.c adds the native-only packed { char, long long * } whose pointer is
# at byte offset 1; the raw GSPA path cannot represent it, so this script also
# asserts that the raw compile fails with the specific diagnostic.
#
# The script runs the driver set through scripts/m4/run.sh:
# 1. the default GSPA assembly text path (m7data.c + i64arr.c);
# 2. M4_NATIVE=1, i.e. llc -filetype=obj + GSPLNK (plus oddptr.c).
# Both link/load with the 1987 GSPA/GSPLNK tools, the dwt loader and MAME.
# data-check.lua checks the marker words and raw byte image (including the
# native-only odd-offset pointer). The unlinked objects are also parsed
# directly to pin the `.data` sizes, literal relocation addresses/type and the
# pointer addend before the linker sees them.
#
# Overridable: LLVM_BIN TMS_CLANG TMS_OPT TMS_BITADDR TMS_LLC MAME DOSBOX
# DOSBOX_CONF SDK_IMG M7_DATA_WORK DATA_OPT LOADER_AT LOADER_END
# MAME_TIMEOUT DOSBOX_TIMEOUT
set -eu
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
DIR=$ROOT/scripts/tests/m7
WORK=${M7_DATA_WORK:-$ROOT/build/m7-data}
OPT=${DATA_OPT:--O0}
LLVM_BIN=${LLVM_BIN:-$ROOT/build/llvm-clean-build/bin}
TMS_LLC=${TMS_LLC:-$LLVM_BIN/llc}
TMS_CLANG=${TMS_CLANG:-$LLVM_BIN/clang}
TMS_OPT=${TMS_OPT:-$LLVM_BIN/opt}
TMS_BITADDR=${TMS_BITADDR:-$ROOT/tools/tms-bitaddr/tms-bitaddr}
fail() {
echo "m7 data: FAIL: $*" >&2
exit 1
}
rm -rf "$WORK"
mkdir -p "$WORK"
run_one() { # run_one TAG NATIVE ENTRY SOURCES ODD_NATIVE
tag=$1
native=$2
entry=$3
sources=$4
odd_native=$5
echo "== [$tag] compile/link/run the C drivers ($OPT)"
M7_DATA_NATIVE="$odd_native" \
M4_WORK="$WORK/$tag" \
M4_SDK="$WORK/sdk-$tag" \
M4_COFF="$WORK/data-$tag.coff" \
M4_SOURCES="$sources" \
M4_ENTRY="$entry" \
M4_CHECK_LUA="$DIR/data-check.lua" \
M4_CLANG_OPT="$OPT" \
M4_LLC_OPT=-O0 \
M4_NATIVE="$native" \
M4_NATIVE_SKIP_ASM="$native" \
"$ROOT/scripts/m4/run.sh" > "$WORK/$tag.log" 2>&1 \
|| { tail -80 "$WORK/$tag.log" >&2; \
fail "$tag: compile/link/MAME harness failed"; }
grep -q '^M4 MAME: PASS' "$WORK/$tag/mame.log" \
|| { tail -50 "$WORK/$tag/mame.log" >&2; \
fail "$tag: M4 harness marker check failed"; }
grep -q '^M7 DATA MAME: PASS' "$WORK/$tag/mame.log" \
|| { grep -E '^M7 data' "$WORK/$tag/mame.log" >&2 || true; \
tail -50 "$WORK/$tag/mame.log" >&2; \
fail "$tag: field values or raw bytes did not match in MAME"; }
grep -E '^M7 data' "$WORK/$tag/mame.log" | sed 's/^/ /'
}
run_one asm 0 "$DIR/data-entry.asm" \
"$DIR/m7data.c $DIR/i64arr.c" 0
run_one native 1 "$DIR/data-native-entry.asm" \
"$DIR/m7data.c $DIR/i64arr.c $DIR/oddptr.c" 1
echo "== the raw GSPA path must reject the odd-offset pointer"
export TMS_CLANG TMS_OPT TMS_BITADDR
"$ROOT/scripts/tms-clang" "$OPT" -o "$WORK/oddptr.ll" "$DIR/oddptr.c" \
|| fail "tms-clang failed for oddptr.c"
if "$TMS_LLC" -march=tms34010 "$OPT" -filetype=asm "$WORK/oddptr.ll" \
-o "$WORK/oddptr.s" > "$WORK/oddptr-raw.log" 2>&1; then
fail "the raw GSPA path accepted the odd-offset pointer"
fi
grep -Fq "a pointer field at an odd .data byte offset cannot be represented in GSPA assembly" \
"$WORK/oddptr-raw.log" \
|| { cat "$WORK/oddptr-raw.log" >&2; \
fail "the raw path did not emit the specific odd-pointer diagnostic"; }
echo " $(grep -m1 'pointer field at an odd' "$WORK/oddptr-raw.log")"
echo "== object-mode wide integers from the CodeGen fixture"
# The asm arm of the i24/i40/i48/i56 and [2 x i24] tests lives in the LLVM lit
# test. Compile the same module to TI-COFF here so the object emit arm is
# byte-checked too (the object writer needs a .text anchor, which the fixture
# provides).
WIDE_LL=${WIDE_LL:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m7-data-initializers.ll}
[ -f "$WIDE_LL" ] || fail "missing wide-integer fixture $WIDE_LL"
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj "$WIDE_LL" \
-o "$WORK/wide.o" > "$WORK/wide.log" 2>&1 \
|| { cat "$WORK/wide.log" >&2; fail "object-mode wide-integer compile failed"; }
echo "== parse the native objects' .data before linking"
python3 - "$WORK/native/asm/m7data.o" \
"$WORK/native/asm/i64arr.o" \
"$WORK/native/asm/oddptr.o" \
"$WORK/wide.o" <<'PY' \
|| fail "native .data bytes/symbols/relocations do not match"
import struct
import sys
def parse(path):
data = open(path, "rb").read()
machine, nscns, _time, symptr, nsyms, opthdr, _flags = \
struct.unpack_from("<HHIIIHH", data, 0)
if machine != 0x0090:
sys.exit(f"{path}: machine {machine:#06x}, want 0x0090")
base = 20 + opthdr
sections = {}
for i in range(nscns):
off = base + i * 40
name = data[off:off + 8].split(b"\0", 1)[0].decode("latin1")
size, ptr, relptr, _lnptr = struct.unpack_from(
"<IIII", data, off + 16)
nrel, _nln = struct.unpack_from("<HH", data, off + 32)
sections[name] = (size, ptr, relptr, nrel)
symbols = {}
i = 0
while i < nsyms:
off = symptr + i * 18
name = data[off:off + 8]
if name[:4] == b"\0\0\0\0":
soff = struct.unpack_from("<I", data, off + 4)[0]
end = data.find(b"\0", symptr + nsyms * 18 + soff)
nm = data[symptr + nsyms * 18 + soff:end].decode("latin1")
else:
nm = name.rstrip(b"\0").decode("latin1")
value = struct.unpack_from("<I", data, off + 8)[0]
cls = data[off + 16]
naux = data[off + 17]
if cls != 3:
symbols[nm] = value
i += 1 + naux
if ".data" not in sections:
sys.exit(f"{path}: no .data section")
data_size, data_ptr, rel_ptr, nrel = sections[".data"]
raw = data[data_ptr:data_ptr + data_size // 8]
relocs = []
for i in range(nrel):
off = rel_ptr + i * 10
addr, _sym, typ = struct.unpack_from("<IIH", data, off)
if typ != 0x11:
sys.exit(f"{path}: relocation type {typ:#06x}, want 0x11")
relocs.append(addr)
return raw, data_size, symbols, relocs
def at(symbols, name, raw, hexbytes, field_off=0, path=""):
if name not in symbols:
sys.exit(f"{path}: missing symbol {name}")
off = symbols[name] // 8 + field_off
got = raw[off:off + len(hexbytes) // 2]
if got.hex() != hexbytes:
sys.exit(f"{path}: {name} bytes {got.hex()}, want {hexbytes}")
base_path, i64_path, odd_path, wide_path = sys.argv[1:5]
# The original .data set: exactly 72 bytes and the three literal relocation
# addresses for _pp, _cp and _pt.p.
raw, size, symbols, relocs = parse(base_path)
if size != 72 * 8:
sys.exit(f"{base_path}: .data size {size} bits, want 72 bytes (576)")
if sorted(relocs) != [0x1B0, 0x1D0, 0x210]:
sys.exit(f"{base_path}: relocations {[hex(r) for r in relocs]}, "
f"want 0x1b0/0x1d0/0x210")
want = {
"_x": "8877665544332211",
"_s": "8dedcdab09",
"_p": "1155443322ddccbbaa99887766",
"_arr": "443322116655aa998877ccbb00ffeedd2211",
"_u": "78563412bc9a0000",
}
for name, hexbytes in want.items():
at(symbols, name, raw, hexbytes, path=base_path)
# Pointer fields: zero in the object plus an R-RELLONG at those addresses.
for name, field_off in (("_pp", 0), ("_cp", 0), ("_pt", 4)):
off = symbols[name] // 8 + field_off
got = raw[off:off + 4]
if name == "_cp":
if got != bytes([24, 0, 0, 0]):
sys.exit(f"{base_path}: _cp addend {got.hex()}, want 18000000")
elif got != b"\0\0\0\0":
sys.exit(f"{base_path}: {name} field {got.hex()}, want zero + reloc")
# i64 array: 2 * 8 bytes, no relocations.
raw, size, symbols, relocs = parse(i64_path)
if size != 16 * 8:
sys.exit(f"{i64_path}: .data size {size} bits, want 16 bytes (128)")
if relocs:
sys.exit(f"{i64_path}: unexpected relocations {[hex(r) for r in relocs]}")
at(symbols, "_y", raw, "887766554433221100ffeeddccbbaa99", path=i64_path)
# Odd-offset pointer: one 6-byte object with the pointer at byte 1 and a
# relocation at bit 8.
raw, size, symbols, relocs = parse(odd_path)
if size != 6 * 8:
sys.exit(f"{odd_path}: .data size {size} bits, want 6 bytes (48)")
if relocs != [8]:
sys.exit(f"{odd_path}: relocations {[hex(r) for r in relocs]}, "
f"want exactly [0x8] (the pointer at byte 1)")
at(symbols, "_odd", raw, "5a0000000000", path=odd_path)
if symbols["_odd"] != 0:
sys.exit(f"{odd_path}: _odd is at bit {symbols['_odd']}, want 0")
# Object-mode wide integers from m7-data-initializers.ll: exact value bytes
# and allocation padding, in TI-COFF rather than asm text.
raw, _size, symbols, _relocs = parse(wide_path)
wide_want = {
"_wide3": "56341200", # i24 + alloc pad
"_wide5": "9a7856341200", # i40 + alloc pad
"_wide6": "bc9a78563412", # i48, no pad
"_wide7": "debc9a7856341200", # i56 + alloc pad
"_wide3arr": "5634120021436500", # [2 x i24], stride 4
"_i64arr": "887766554433221100ffeeddccbbaa99",
}
for name, hexbytes in wide_want.items():
at(symbols, name, raw, hexbytes, path=wide_path)
print("m7-data-object: own .data=72 bytes relocs=0x1b0/0x1d0/0x210; "
"i64arr .data=16 bytes; oddptr .data=6 bytes reloc=0x8; "
"wide i24/i40/i48/i56 + [2 x i24] bytes exact")
PY
echo "m7 data: PASS (i64 LSW-first in asm and object mode, i64 array, i24/i40/"
echo " i48/i56 and [2 x i24] bytes in asm and object mode, packed "
echo " records, bitfields, union, pointer relocations, and the "
echo " native-only odd-offset pointer in TI-COFF/MAME)"