JA
games/alis / tools / lang / check.php · commit 9c722cf
<?php// Checks the grammar against a real run: for every instruction of var/<run>/trace.txt (tools/run.php ... traceCycles),// the decoder must find the same length (the VM's pc after it), and the flow decoding must reach it.//   bin/jumphp php games/alis tools/lang/check.php run-tracerequire __DIR__ . '/../../vendor/autoload.php';use App\Alis\Lang\Decoder;use App\Alis\Lang\Grammar;$run = $argv[1] ?? 'run-trace';$bad = [];$missing = [];$seen = [];$dec = [];$prog = [];foreach (file(__DIR__ . "/../../var/$run/trace.txt") as $line) {    $f = explode(' ', trim($line));    if (count($f) < 6) continue;    [, $script, , $name, $at, $after] = $f;    $at = hexdec($at);    $after = hexdec($after);    $key = "$script $at";    if (isset($seen[$key])) continue;    $seen[$key] = true;    $id = strtolower(preg_replace('/\.io$/i', '', $script));    if (!isset($dec[$id])) {        $b = file_get_contents(__DIR__ . "/../../var/scripts/$id.bin");        $h = unpack('vid/vcode/Cu1/Cu2/Vafter/Vbefore/Vres', $b);        $dec[$id] = new Decoder($b, 0x18, $h['res']);        $entries = [$h['code'] + 2];        if ($h['before']) $entries[] = 10 + $h['before'];        if ($h['after']) $entries[] = 6 + $h['after'];        $prog[$id] = $dec[$id]->program($entries)[0];    }    if (in_array($name, ['csoundon', 'csoundoff', 'cmusicon', 'cmusicoff', 'cdelfilm', 'copenfilm'], true)) continue;    if (!isset($prog[$id][$at])) {        $missing[$name][] = sprintf('%s:%x', $id, $at);    }    try {        $d = $dec[$id];        $i = $d->instruction($at);    } catch (\RuntimeException $e) {        $bad[$name][] = sprintf('%s:%x %s', $id, $at, $e->getMessage());        continue;    }    $iname = Grammar::OPCODES[$i['op']][0];    if ($iname !== $name && !($iname === 'cnul2' && $name === 'cnul')) {        $bad[$name][] = sprintf('%s:%x decoded as %s', $id, $at, $iname);        continue;    }    if ($after !== $i['end'] && !in_array($after, $d->jumpTargets(), true)) {        $bad[$name][] = sprintf('%s:%x end %x, the VM went to %x', $id, $at, $i['end'], $after);    }}foreach ($bad as $n => $l) {    printf("BAD %-10s %4d  %s\n", $n, count($l), implode('  ', array_slice($l, 0, 4)));}foreach ($missing as $n => $l) {    printf("UNREACHED %-10s %4d  %s\n", $n, count($l), implode(' ', array_slice($l, 0, 6)));}printf("%d distinct instructions run, %d bad, %d not reached by the flow\n", count($seen), array_sum(array_map('count', $bad)), array_sum(array_map('count', $missing)));