#!/usr/bin/env python3 """Generates expectations.h from expectations.json. expectations.json is produced from a hardware run of nxdk_low_level_nv2a_tests by extract_expectations.py. See "Updating the exceptional value expectations" in README.md for the full process. Usage: generate_expectations.py [expectations.json] [expectations.h] Both paths default to the files next to this script. """ from __future__ import annotations import argparse import json import re from pathlib import Path from typing import Any _HERE = Path(__file__).resolve().parent _COMPONENTS = {None: 0, "x": 0, "y": 1, "z": 2, "w": 3} _VEC4_SIZE = 4 _ARL_REGISTER = re.compile(r"^c\[(\d+)\]$") _ARL_OUT_OF_RANGE = "out of range (reads 0)" Record = dict[str, Any] Operand = str | list[str] | None def _c_string(value: str) -> str: if not value.isascii(): msg = f"Non-ASCII string: {value!r}" raise ValueError(msg) return json.dumps(value) def _vec4(value: Operand) -> str: if value is None: bits = ["0x00000000"] * _VEC4_SIZE elif isinstance(value, list): if len(value) != _VEC4_SIZE: msg = f"Expected a vec4: {value!r}" raise ValueError(msg) bits = value else: bits = [value] * _VEC4_SIZE return "{" + ", ".join(f"0x{int(b, 16):08X}u" for b in bits) + "}" def _hw(record: Record) -> tuple[str, str]: hw: str = record["hw"] if hw.startswith("0x"): return f"0x{int(hw, 16):08X}u", "kNotAddress" if hw == _ARL_OUT_OF_RANGE: return "0x00000000u", "kAddressOutOfRange" match = _ARL_REGISTER.match(hw) if not match: msg = f"Unrecognized hw value: {hw!r}" raise ValueError(msg) return "0x00000000u", match.group(1) def _record(record: Record) -> str: inputs: dict[str, Operand] = record["inputs"] a = inputs.get("a", inputs.get("P")) hw, address = _hw(record) fields = [ _c_string(record["group"]), _c_string(record["op"]), _c_string(record["case"]), str(_COMPONENTS[record.get("component")]), _vec4(a), _vec4(inputs.get("b")), _vec4(inputs.get("c")), hw, address, ] return " {" + ", ".join(fields) + "}," def _source_comment(source: dict[str, Any]) -> list[str]: revision = source["git_revision"] if source["git_dirty"]: revision += " (with uncommitted changes)" return [ "// Measured on hardware by nxdk_low_level_nv2a_tests", f"// revision {revision}.", ] def generate(src: Path, dst: Path) -> None: with src.open(encoding="ascii") as infile: data = json.load(infile) records: list[Record] = data["records"] lines = [ "// Generated by generate_expectations.py from expectations.json.", "// DO NOT EDIT.", *_source_comment(data["source"]), "", "#ifndef NV2A_VSH_CPU_TEST_EXCEPTIONAL_VALUES_EXPECTATIONS_H_", "#define NV2A_VSH_CPU_TEST_EXCEPTIONAL_VALUES_EXPECTATIONS_H_", "", "#include ", "", "namespace exceptional_values {", "", "// Value of Expectation::hw_address for records whose hw value is a bit", "// pattern rather than an ARL address.", "static constexpr int32_t kNotAddress = -2;", "// Value of Expectation::hw_address for ARL records whose address reads", "// outside of the constant file.", "static constexpr int32_t kAddressOutOfRange = -1;", "", "struct Expectation {", " const char* group;", " const char* op;", " const char* case_name;", " uint32_t component;", " // Scalar inputs are broadcast to all four components.", " uint32_t a[4];", " uint32_t b[4];", " uint32_t c[4];", " uint32_t hw;", " // Index of the constant register addressed via A0 for ARL records.", " int32_t hw_address;", "};", "", "// clang-format off", "static const Expectation kExpectations[] = {", ] lines.extend(_record(r) for r in records) lines.extend( [ "};", "// clang-format on", "", "} // namespace exceptional_values", "", "#endif // NV2A_VSH_CPU_TEST_EXCEPTIONAL_VALUES_EXPECTATIONS_H_", "", ] ) with dst.open("w", encoding="ascii", newline="\n") as outfile: outfile.write("\n".join(lines)) def main() -> None: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("src", nargs="?", type=Path, default=_HERE / "expectations.json") parser.add_argument("dst", nargs="?", type=Path, default=_HERE / "expectations.h") args = parser.parse_args() generate(args.src, args.dst) if __name__ == "__main__": main()