DE
games/alis / src / Alis / Compiled / Compiler.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled;use App\Alis\Alis;use App\Alis\Lang\Decoder;use App\Alis\Lang\Grammar;use App\Alis\Lang\Script;use App\Alis\ScriptData;/** * ALIS -> PHP: a loaded script becomes a PHP class (CompiledScript) whose run() executes it without reading its byte * code. The script's bytes are decoded once (Lang\Decoder, from its entry points); each instruction becomes a `case` * of the script's offset, written by its template (Templates/), or, when none fits yet, the VM's own handler run at * that address (readexec_opcode()): both share the VM's memory, registers and stacks, so a script can be compiled a * piece at a time and the game stays the same. Offsets nobody decoded (data jumped into, a stack return the decoder * did not see) go to the VM too (`default`). * * The PHP is made when the game loads the script (the scripts are the game's data: their PHP is never published). */final class Compiler{    /** @var array<string, CompiledScript|false> script key => its compiled class (false: not compiled) */    public static array $cache = [];    /** on / off (the HD edition and the sources editions use it; tools/run.php --compiled) */    public static bool $on = false;    /** a folder to keep the generated PHP in (debugging), '' = none */    public static string $dumpDir = '';    /**     * a folder of scripts in PHP to run instead of generating them (tools/compiled/readable.php writes them, a person     * or an AI edits them): <name>.php, used when its class is the one of these bytes (its name ends with their md5)     */    public static string $fromDir = '';    /** @var array<string, array<string, string>> the variables' names: script name (or "main") => offset ("0x62") => name */    public static array $names = [];    /** how many scripts were taken from $fromDir */    public static int $fromFiles = 0;    public static function for(ScriptData $d): ?CompiledScript    {        // by name and size, not by address: compacting the memory moves a loaded script (run() reads data_org each turn)        $key = $d->name . ':' . $d->sz;        if (!isset(self::$cache[$key])) {            try {                self::$cache[$key] = self::compile($d) ?? false;            } catch (\Throwable $e) {                error_log('ALIS compiler: ' . $d->name . ': ' . $e->getMessage());                self::$cache[$key] = false;            }        }        return self::$cache[$key] ?: null;    }    public static function compile(ScriptData $d): ?CompiledScript    {        if (Alis::$platform->version >= 60 || $d->sz < 0x18) {            return null;              // v6 has its own decoder (not supported yet)        }        $bytes = substr(Alis::$mem, $d->data_org, $d->sz);        if (!Script::isScript($bytes)) {            return null;        }        // the script's name as the tools write it: main, logosnd... (the VM keeps the file's: main.io, logosnd.IO)        $name = strtolower((string) preg_replace('/\.io$/i', '', basename($d->name)));        if (self::$fromDir !== '' && ($cs = self::fromFile($name, $bytes)) !== null) {            return $cs;        }        // the same program under another key (name spelled otherwise, other size of its resources, a new game in the        // same PHP): its class is already declared (a second eval of it is a fatal error that cannot be caught)        $full = __NAMESPACE__ . '\\Generated\\' . self::className($name, $bytes);        if (self::$dumpDir === '' && class_exists($full, false)) {            return new $full();        }        $php = self::source($name, $bytes, $class, self::$dumpDir !== '');        if (self::$dumpDir !== '') {            @mkdir(self::$dumpDir, 0777, true);            file_put_contents(self::$dumpDir . '/' . preg_replace('/\W+/', '_', $name) . '.php', "<?php\n" . $php);        }        $full = __NAMESPACE__ . '\\Generated\\' . $class;        if (!class_exists($full, false)) {            eval($php);        }        return new $full();    }    /** The script's class written in $fromDir, when it is the one of these bytes (else null: generated again). */    private static function fromFile(string $name, string $bytes): ?CompiledScript    {        $file = self::$fromDir . '/' . preg_replace('/\W+/', '_', $name) . '.php';        $class = self::className($name, $bytes);        if (!is_file($file)) {            return null;        }        if (!str_contains((string) file_get_contents($file), "class $class ")) {            error_log("ALIS compiler: $file is not for this version of the script ($class): generated again");            return null;        }        $full = __NAMESPACE__ . '\\Generated\\' . $class;        if (!class_exists($full, false)) {            require $file;        }        self::$fromFiles++;        return new $full();    }    /**     * The class of a script: its name and the md5 of its header and program (not of its resources: the VM converts     * some of them when it loads the script, the program is the same).     */    public static function className(string $name, string $bytes): string    {        $res = strlen($bytes) >= 0x12 ? (int) unpack('V', $bytes, 0x0e)[1] : strlen($bytes);        return 'S_' . preg_replace('/\W+/', '_', $name) . '_' . substr(md5(substr($bytes, 0, $res)), 0, 10);    }    /** "0x62", "-0x5c", "98" => the offset */    private static function off(string $k): int    {        $neg = str_starts_with($k, '-');        $k = ltrim($k, '-');        $v = str_starts_with(strtolower($k), '0x') ? (int) hexdec(substr($k, 2)) : (int) $k;        return $neg ? -$v : $v;    }    /** a variable's name as a PHP constant */    private static function constName(string $n): string    {        $c = strtoupper(trim((string) preg_replace('/[^A-Za-z0-9]+/', '_', $n), '_'));        return $c === '' || ctype_digit($c[0]) ? 'V_' . $c : $c;    }    /**     * The PHP of a script's class (without the opening tag); $class receives its name. $readable: each case says its     * instruction as ALIS source, and the named variables (self::$names) are constants of the class.     */    public static function source(string $name, string $bytes, ?string &$class = null, bool $readable = false): string    {        Templates::load();        $s = Script::fromBytes($name, $bytes);        $texts = $readable ? (new \App\Alis\Lang\Printer())->texts($s) : [];        // names: this script's variables (vram offsets) and the main script's (basemain offsets)        $isMain = $s->id === 0;        $own = [];        $main = [];        foreach (self::$names[$name] ?? [] as $off => $n) {            $own[self::off((string) $off)] = self::constName($n);        }        foreach (self::$names['main'] ?? [] as $off => $n) {            $main[self::off((string) $off)] = ($isMain ? '' : 'MAIN_') . self::constName($n);        }        if ($isMain) {            $own += $main;        }        $used = [];        $code = str_repeat("\0", 0x18) . $s->code;        $entries = array_values(array_filter([$s->main, $s->before, $s->after]));        [$ins] = (new Decoder($code, 0x18, strlen($code)))->program($entries);        ksort($ins);        $ctx = new Context($name, $bytes);        $class = self::className($name, $bytes);        $cases = [];        $native = $interpreted = 0;        foreach ($ins as $at => $i) {            $opName = Grammar::OPCODES[$i['op']][0] ?? '';            $body = null;            $why = isset(Templates::$op[$opName]) ? 'template returned null' : 'no template';            if (isset(Templates::$op[$opName])) {                try {                    $body = (Templates::$op[$opName])($i, $ctx);                } catch (NoTemplate $e) {                    $body = null;     // an element without a template yet: the VM runs this instruction                    $why = $e->getMessage();                }            }            if ($body === null) {                $interpreted++;                $body = '\App\Alis\readexec_opcode(); // VM: ' . $why;            } else {                $native++;                // as the VM after reading the instruction: opcode_pc at its start (vm_yield_retry() comes back to                // it), pc after its operands (the handlers that read pc see the same value); the body may jump                $body = sprintf('Alis::$opcode_pc = $s->pc;' . "\n" . '$s->pc = $o + 0x%x;', $i['end']) . "\n" . $body;            }            $body = self::tidy($body);            if ($own !== [] || $main !== []) {                $body = (string) preg_replace_callback('/(\$s->vram_org|Alis::\$script->vram_org|Alis::\$basemain) \+ (-?\d+)(?![\d.])/',                    static function (array $m) use ($own, $main, $isMain, &$used): string {                        $off = (int) $m[2];                        $c = $m[1] === 'Alis::$basemain' && !$isMain ? ($main[$off] ?? null) : ($own[$off] ?? null);                        if ($c === null) {                            return $m[0];                        }                        $used[$c] = $off;                        return $m[1] . ' + self::' . $c;                    }, $body);            }            $comment = $opName;            if (isset($texts[$at])) {                $comment = self::named(str_replace("\n", ' ', $texts[$at]), $name, $isMain);            }            $cases[] = sprintf("                case 0x%x: // %s\n                    %s\n                    break;", $at, $comment, str_replace("\n", "\n                    ", $body));        }        $casesPhp = implode("\n", $cases);        ksort($used);        $consts = '';        foreach ($used as $c => $off) {            $consts .= sprintf("    public const %s = %d;\n", $c, $off);        }        if ($consts !== '') {            $consts = "    // the named variables (offsets in the entity's variables; MAIN_: the main script's)\n" . $consts . "\n";        }        return <<<PHPnamespace App\\Alis\\Compiled\\Generated;use App\\Alis\\Alis;use App\\Alis\\ScriptLive;use App\\Alis\\VmYield;/** ALIS script "$name" compiled to PHP: $native instructions in PHP, $interpreted run by the VM at their address. */final class $class extends \\App\\Alis\\Compiled\\CompiledScript{$consts    public int \$native = $native;    public int \$interpreted = $interpreted;    public function run(ScriptLive \$s, int &\$budget): void    {        while (Alis::\$state && \$s->running) {            if (--\$budget <= 0) {                throw new VmYield();            }            \$o = \$s->data->data_org;            switch (\$s->pc - \$o) {$casesPhp                default:                    \\App\\Alis\\readexec_opcode();            }        }    }}PHP;    }    /** an ALIS source line with the named variables written by their names */    public static function named(string $text, string $script, bool $isMain = false): string    {        $own = self::$names[$script] ?? [];        $main = self::$names['main'] ?? [];        if ($isMain) {            $own += $main;        }        if ($own === [] && $main === []) {            return $text;        }        $find = static function (array $map, string $off): ?string {            $v = (int) $off;            foreach ($map as $k => $n) {                if (self::off((string) $k) === $v) {                    return $n;                }            }            return null;        };        return (string) preg_replace_callback('/\b([vtmh]t?[bwso])(?::loc)?\[(-?0x[0-9a-f]+)\]/', static function (array $m) use ($own, $main, $find): string {            $map = $m[1][0] === 'm' ? $main : ($m[1][0] === 'h' ? [] : $own);            return $find($map, str_starts_with($m[2], '-') ? '-' . hexdec(substr($m[2], 3)) : (string) hexdec(substr($m[2], 2))) ?? $m[0];        }, $text);    }    private const S16 = 'Alis::$varD7 = ((Alis::$varD7 & 0xffff) ^ 0x8000) - 0x8000;';    /**     * Lines that change nothing taken out (the same effects, fewer lines to read): D7 made a signed 16-bit value just     * after it was given one (a constant in range, a comparison's -1 / 0, an s8() / s16()), a pc set twice in a row.     */    public static function tidy(string $body): string    {        $lines = explode("\n", $body);        $out = [];        foreach ($lines as $line) {            $prev = $out === [] ? null : $out[count($out) - 1];            if ($line === self::S16 && $prev !== null && preg_match('/^Alis::\$varD7 = (-?\d+|\(.*\) \? -1 : 0|\\\\App\\\\Alis\\\\s(8|16)\((?:[^;]*)\));$/', $prev, $m)                && (!is_numeric($m[1]) || ((int) $m[1] >= -32768 && (int) $m[1] <= 32767)) && self::balanced($m[1])) {                continue;            }            if ($prev !== null && preg_match('/^\$s->pc = \$o \+ 0x[0-9a-f]+;$/', $prev) && preg_match('/^\$s->pc = \$o \+ 0x[0-9a-f]+;$/', $line)) {                $out[count($out) - 1] = $line;                continue;            }            $out[] = $line;        }        return implode("\n", $out);    }    /** an expression whose outer parentheses close at its end (s16(a) + s16(b) is not one call) */    private static function balanced(string $e): bool    {        if (!str_starts_with($e, '\\App\\Alis\\s')) {            return true;        }        $depth = 0;        $open = strpos($e, '(');        for ($k = $open, $n = strlen($e); $k < $n; $k++) {            $depth += $e[$k] === '(' ? 1 : ($e[$k] === ')' ? -1 : 0);            if ($depth === 0 && $k < $n - 1) {                return false;            }        }        return true;    }}