Files

424 lines
17 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 "== M9 vector initializers in the raw and object paths"
# Integer and floating vector constants are emitted as their exact
# little-endian element bytes (the f32 elements are target GSP words, not
# IEEE). The asm arm is pinned here as text and the object arm below as
# bytes; llvm/test/CodeGen/TMS34010/m9-vector-data.ll covers the same module.
VECTOR_LL=${VECTOR_LL:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m9-vector-data.ll}
[ -f "$VECTOR_LL" ] || fail "missing vector fixture $VECTOR_LL"
"$TMS_LLC" -march=tms34010 -O0 -filetype=asm "$VECTOR_LL" \
-o "$WORK/vector.s" > "$WORK/vector.log" 2>&1 \
|| { cat "$WORK/vector.log" >&2; fail "raw vector compile failed"; }
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj "$VECTOR_LL" \
-o "$WORK/vector.o" > "$WORK/vector-obj.log" 2>&1 \
|| { cat "$WORK/vector-obj.log" >&2; fail "object vector compile failed"; }
"$TMS_LLC" -march=tms34010 -O2 -filetype=obj "$VECTOR_LL" \
-o "$WORK/vector-o2.o" > "$WORK/vector-obj-o2.log" 2>&1 \
|| { cat "$WORK/vector-obj-o2.log" >&2; \
fail "object vector -O2 compile failed"; }
# A padded zero vector (<3 x i32>: 12 store / 16 alloc) is supported because
# the emitter writes store bytes and the callers/trailer add the allocation
# padding. Its record member offset and .data size are byte-checked below.
PADDED_LL=${PADDED_LL:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m9-vector-data-padded-zero.ll}
[ -f "$PADDED_LL" ] || fail "missing padded-zero vector fixture $PADDED_LL"
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj "$PADDED_LL" \
-o "$WORK/vector-padded-zero.o" > "$WORK/vector-padded-zero.log" 2>&1 \
|| { cat "$WORK/vector-padded-zero.log" >&2; \
fail "object padded-zero vector compile failed"; }
python3 - "$WORK/vector.s" <<'PY' \
|| fail "raw vector .data directives do not match"
import sys
lines = [line.strip() for line in open(sys.argv[1])]
def block(label):
try:
start = lines.index(label + ":")
except ValueError:
sys.exit(f"raw vector asm: missing {label}:")
out = []
for line in lines[start + 1:]:
if not line:
continue
if (line.endswith(":") or line.startswith(".globl") or
line == ".even" or line.startswith(".data") or
line.startswith(".text")):
break
out.append(line)
return out
want = {
# <2 x i32>: two little-endian .long values.
"_v2i32": [".long 287454020", ".long 3735928559"],
# <4 x i16>: four 16-bit elements, in order.
"_v4i16": [".word 01234h", ".word 0FFFFh", ".word 05566h", ".word 0ABCDh"],
# <2 x float>: GSP 1.0 = 40400000h, GSP 2.5 = 40D00000h.
"_v2f32": [".long 40400000h", ".long 40D00000h"],
# Vector aggregate zero: exactly the 8-byte allocation.
"_vzero": [".byte 0, 0, 0, 0, 0, 0, 0, 0"],
# Array of vector: element order, then a zero vector element.
"_vaggarr": [".word 00001h", ".word 00002h", ".byte 0, 0, 0, 0"],
# Record member vector at its DataLayout offset (3 pad bytes first).
"_vrec": [".byte 011h", ".byte 0, 0, 0", ".word 01234h",
".word 0ABCDh", ".long 195948557"],
# <8 x i8>: one .byte per element in order.
"_v8i8": [".byte 001h", ".byte 002h", ".byte 003h", ".byte 004h",
".byte 005h", ".byte 006h", ".byte 007h", ".byte 008h"],
# <2 x i64>: LSW-first words per element.
"_v2i64": [".long 55667788h", ".long 11223344h",
".long 0DDEEFF00h", ".long 99AABBCCh"],
# <2 x f64>: GSP doubles, LSW-first (1.5 / 2.5).
"_v2f64": [".long 00000000h", ".long 400C0000h",
".long 00000000h", ".long 401A0000h"],
}
for label, want_lines in want.items():
got = block(label)
if got != want_lines:
sys.exit(f"raw vector asm: {label} {got}, want {want_lines}")
print("m7-data-raw-vector: <2 x i32>/<4 x i16>/<2 x float>/<8 x i8>/"
"<2 x i64>/<2 x f64>/zero/nested bytes exact")
PY
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" \
"$WORK/vector.o" \
"$WORK/vector-o2.o" \
"$WORK/vector-padded-zero.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, vector_path, vector_o2_path, \
padded_path = sys.argv[1:8]
# 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)
# M9 vector initializers in TI-COFF: every element is its exact little-endian
# bytes, the f32/f64 elements are GSP words, and the vector zero is the full
# allocation. Symbol values are section-relative bit addresses. Both the
# alloc==store fixture and the padded-zero fixture are gated, including the
# total .data size and the inter-symbol offsets (so extra bytes emitted
# between symbols cannot hide).
def check_vector(path):
raw, size, symbols, relocs = parse(path)
if size != 96 * 8:
sys.exit(f"{path}: .data size {size} bits, want 96 bytes (768)")
if relocs:
sys.exit(f"{path}: unexpected relocations "
f"{[hex(r) for r in relocs]}")
offsets = {
"_v2i32": 0, "_v4i16": 8, "_v2f32": 16, "_vzero": 24,
"_vaggarr": 32, "_vrec": 40, "_v8i8": 56, "_v2i64": 64,
"_v2f64": 80,
}
for name, off in offsets.items():
if name not in symbols:
sys.exit(f"{path}: missing symbol {name}")
if symbols[name] // 8 != off:
sys.exit(f"{path}: {name} at byte {symbols[name] // 8}, "
f"want {off}")
vector_want = {
"_v2i32": "44332211efbeadde",
"_v4i16": "3412ffff6655cdab",
"_v2f32": "000040400000d040",
"_vzero": "0000000000000000",
"_vaggarr": "0100020000000000",
"_vrec": "110000003412cdab0df0ad0b",
"_v8i8": "0102030405060708",
"_v2i64": "887766554433221100ffeeddccbbaa99",
"_v2f64": "0000000000000c400000000000001a40",
}
for name, hexbytes in vector_want.items():
at(symbols, name, raw, hexbytes, path=path)
return raw
vector_o0 = check_vector(vector_path)
vector_o2 = check_vector(vector_o2_path)
if vector_o0 != vector_o2:
sys.exit("vector object bytes differ between -O0 and -O2")
# Padded aggregate-zero vectors: <3 x i32> is 12 store / 16 alloc and the
# record is 20 store / 32 alloc. Offsets prove the following member lands at
# its DataLayout offset (record i32 at byte 16) and the total proves the
# emitter does not over-write or double-pad.
raw, size, symbols, relocs = parse(padded_path)
if size != 116 * 8:
sys.exit(f"{padded_path}: .data size {size} bits, want 116 bytes (928)")
if relocs:
sys.exit(f"{padded_path}: unexpected relocations "
f"{[hex(r) for r in relocs]}")
padded_offsets = {
"_z3i32": 0, "_z3i32_next": 16, "_z3rec": 32,
"_z3rec_next": 64, "_z3arr": 80, "_z3arr_next": 112,
}
for name, off in padded_offsets.items():
if name not in symbols:
sys.exit(f"{padded_path}: missing symbol {name}")
if symbols[name] // 8 != off:
sys.exit(f"{padded_path}: {name} at byte {symbols[name] // 8}, "
f"want {off}")
padded_want = {
"_z3i32": "00" * 16,
"_z3rec": "00" * 16 + "07000000" + "00" * 12,
"_z3arr": "00" * 32,
}
for name, hexbytes in padded_want.items():
at(symbols, name, raw, hexbytes, path=padded_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; "
"vector .data=96 bytes at -O0 and -O2, symbol offsets and "
"<2 x i32>/<4 x i16>/<2 x f32>/<8 x i8>/<2 x i64>/<2 x f64>/zero/"
"nested bytes exact; padded zero .data=116 bytes with record member at "
"byte 16")
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, the "
echo " native-only odd-offset pointer in TI-COFF/MAME, and M9 "
echo " integer/fp vector .data bytes in asm and object mode)"