Files
NeoandClaude Opus 5.5 7ba5f78ac6 Open Transport, MacTCP and Shared Library Manager support for 68k and PowerPC (#38)
Adds the Open Transport and MacTCP name resolver headers and calls on
both CPUs, a Shared Library Manager client on 68k, native Open
Transport on 68k through ASLM with a MacTCP fallback, a fetch sample,
a QEMU network test script and docs.

Signed-off-by: Neo-Desktop <321592+Neo-Desktop@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRU3QJgPHoQrF9EQ8YZ
2026-11-28 12:37:26 -08:00

138 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Write docs/coverage.md: how much of the Toolbox the headers declare.
python3 tools/coverage.py [--rom ROMFILE]
PowerPC coverage is measured against the symbols InterfaceLib exports
(data/interfacelib-exports.txt; --rom regenerates it from a Mac OS ROM
image). 68k coverage counts the calls that have 68k code, and how many
of those tools/check-traps.py confirms against the ROM glue.
"""
import json
import os
import re
import subprocess
import sys
from collections import defaultdict
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
EXPORTS = os.path.join(ROOT, "data", "interfacelib-exports.txt")
# Name prefixes that identify a manager, for grouping what is missing.
GROUPS = [
("AppleTalk", r"^(AFP|ASP|ATP|ATEvent|ATPre|DDP|LAP|MPP|NBP|XPP|ADSP|P[A-Z][a-z]+(Skt|Name|Request|Response|PH|DDP|LAP|NBP|ATalk|AppleTalk|TCB|CB|Self)|Build(BDS|DDP|LAP)|GetZoneList|GetMyZone|GetLocalZones|GetBridgeAddress|GetNodeAddress|IsATPOpen|IsMPPOpen|OpenXPP|MPPOpen|ATPLoad|ATPUnload|CustomNBP|StandardNBP|NBP|Kill|ataManager)"),
("Comm Toolbox", r"^(CM|FT|TM|CRM|CTB|InitCM|InitFT|InitTM|InitCRM|InitCTB)"),
("Ethernet", r"^E(AddMulti|AttachPH|DelMulti|DetachPH|GetInfo|RdCancel|Read|SetGeneral|Write)$"),
("File system internals (UT*)", r"^UT[A-Z]"),
("File system plug-ins", r"^(InstallFS|RemoveFS|SetFSInfo|GetFSInfo|InformFSM|InformFFS)$"),
("Mapped files", r"(MappedFile|FileView|MapFileView)"),
("AVL trees", r"^AVL"),
("SCSI Manager 4.3", r"^SCSI"),
("Low memory", r"^LM[GS]et"),
("Text services", r"(TextService|TSM)"),
("Folder Manager", r"^(Folder|FindFolderExtended|FSFindFolderExtended)"),
("Exceptions and extensions", r"(Exception|Extension)"),
]
def declared_names():
names = set()
inc = os.path.join(ROOT, "include", "mv")
for f in os.listdir(inc):
t = re.sub(r"MAC_SINCE(?:_GESTALT)?\([^()]*\) |MAC_COLOR_QD ", "", open(os.path.join(inc, f)).read())
names.update(re.findall(r"\b([A-Za-z_]\w*)\s*\(", t))
names.update(re.findall(r"^#define (\w+)", t, re.M))
names.update(re.findall(r"\b(\w+)\s*(?:=[^,;]*)?[,;]", t))
return names
def has_68k_code():
inc = os.path.join(ROOT, "include", "mv")
out = set()
for f in os.listdir(inc):
t = re.sub(r"MAC_SINCE(?:_GESTALT)?\([^()]*\) |MAC_COLOR_QD ", "", open(os.path.join(inc, f)).read())
out.update(re.findall(r"\b(\w+)\([^;{\n]*\)\s*M68K_INLINE\(", t))
out.update(re.findall(r"__always_inline__\)\) [^(]*?\b(\w+)\(", t))
out.update(re.findall(r"^#define (\w+)\(", t, re.M))
return out
def main():
if "--rom" in sys.argv:
sys.path.insert(0, HERE)
import pefglue
import trapinfo
blob = open(sys.argv[sys.argv.index("--rom") + 1], "rb").read()
p = trapinfo.find_interfacelib(blob)
if not isinstance(p, pefglue.Pef):
p = pefglue.Pef(p)
with open(EXPORTS, "w") as f:
f.write("# Symbols exported by InterfaceLib in the Mac OS ROM, "
"listed by tools/coverage.py --rom.\n")
f.write("\n".join(sorted(p.exports)) + "\n")
exports = [l.strip() for l in open(EXPORTS) if l.strip() and not l.startswith("#")]
have = declared_names()
missing = [n for n in exports if n not in have]
groups = defaultdict(list)
for n in missing:
for g, rx in GROUPS:
if re.search(rx, n):
groups[g].append(n)
break
else:
groups["Other"].append(n)
glue = [l.strip() for l in open(os.path.join(ROOT, "include", "needs-glue.txt"))
if l.strip() and not l.startswith("#")]
code = has_68k_code()
info = json.load(open(os.path.join(ROOT, "data", "trapinfo.json")))
r = subprocess.run([sys.executable, os.path.join(HERE, "check-traps.py")],
capture_output=True, text=True)
summary = r.stderr.strip().splitlines()[-1].replace("check-traps: ", "")
# The C library supplies these on 68k: Open Transport (ot68k.c and
# otnative.c), the MacTCP name resolver (dnr.c) and the ASLM client
# (aslm.c).
libc_glue = set()
for f in ("OpenTransport.h", "OpenTransportProviders.h", "AddressXlation.h",
"LibraryManager.h"):
libc_glue |= set(re.findall(r"\b(\w+)\(", open(os.path.join(ROOT, "include", "mv", f)).read()))
in_libc = [n for n in glue if n in libc_glue]
funcs = set(re.findall(r"\b(\w+)\(", "".join(
re.sub(r"MAC_SINCE(?:_GESTALT)?\([^()]*\) |MAC_COLOR_QD ", "", open(os.path.join(ROOT, "include", "mv", f)).read())
for f in os.listdir(os.path.join(ROOT, "include", "mv")))))
pct = 100 * (len(exports) - len(missing)) / len(exports)
out = ["# Coverage", "",
"Generated by `tools/coverage.py`. Do not edit.", "",
"## PowerPC", "",
f"The ROM's InterfaceLib exports {len(exports)} symbols. The headers "
f"declare {len(exports) - len(missing)} of them ({pct:.0f}%). The "
"headers also declare Mac OS 9 calls that the ROM's InterfaceLib "
"predates, such as the HFS Plus API.", "",
"## 68k", "",
f"- {len(glue)} declared calls have no 68k trap and need glue code "
"on 68k (listed in `include/needs-glue.txt`). The C library "
f"supplies {len(in_libc)} of them: Open Transport, the MacTCP "
"name resolver and the Shared Library Manager client. Most of the others are Mac OS 9 "
"calls that never ran on 68k: the HFS Plus API, Navigation "
"Services, Driver Services and the Name Registry.",
f"- The ROM's PowerPC glue describes {len(info)} 68k calls. Checked "
f"against it: {summary}.", "",
"## Not declared yet", "",
"InterfaceLib exports these, and the headers do not declare them:", ""]
for g in [g for g, _ in GROUPS] + ["Other"]:
if groups.get(g):
out.append(f"### {g} ({len(groups[g])})")
out.append("")
out.append(", ".join(f"`{n}`" for n in sorted(groups[g])))
out.append("")
os.makedirs(os.path.join(ROOT, "docs"), exist_ok=True)
open(os.path.join(ROOT, "docs", "coverage.md"), "w").write("\n".join(out))
print(f"coverage: {len(exports) - len(missing)}/{len(exports)} InterfaceLib "
f"symbols declared, {len(missing)} missing", file=sys.stderr)
if __name__ == "__main__":
main()