PT
games/alis / src / Alis / Compiled / Context.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled;/** * What a template may need while it writes an instruction's PHP: the expression elements (opernames), store and add * targets written by their own templates, in the same order and with the same effects on the VM's registers (D6, * D7, the string accumulators) as the VM's readexec_* functions (fn/alis.php). An element without a template throws * NoTemplate: the whole instruction is then left to the VM. */final class Context{    public function __construct(        /** the script's name (comments) */        public string $script,        /** the unpacked script's bytes (offset 0 = its header) */        public string $bytes,    ) {    }    /** PHP statement: go on at offset $target of the script (the next loop turn dispatches it) */    public static function go(int $target): string    {        return sprintf('$s->pc = $o + 0x%x;', $target);    }    /** readexec_opername(): the element, then D7 as a signed 16-bit value (versions below 60) */    public function opername(array $item): string    {        $t = Templates::$oper[$item['c']] ?? throw new NoTemplate(sprintf('opername 0x%02x', $item['c']));        return $t($item, $this) . "\n" . 'Alis::$varD7 = ((Alis::$varD7 & 0xffff) ^ 0x8000) - 0x8000;';    }    /** readexec_opername_saveD7(): D6 = D7, then the element */    public function opernameSaveD7(array $item): string    {        return 'Alis::$varD6 = Alis::$varD7;' . "\n" . $this->opername($item);    }    /** readexec_opername_saveD6(): the element's value goes to D6, D7 keeps its value (as a signed 16-bit one) */    public function opernameSaveD6(array $item): string    {        return '$t' . ($n = ++$this->tmp) . ' = \App\Alis\s16(Alis::$varD7);' . "\n" . $this->opernameSaveD7($item) . "\n"            . 'Alis::$varD6 = Alis::$varD7;' . "\n" . "Alis::\$varD7 = \$t$n;";    }    /** readexec_opername_swap(): the string accumulators sd7 / sd6 swapped, then the element */    public function opernameSwap(array $item): string    {        return '[Alis::$sd7, Alis::$sd6] = [Alis::$sd6, Alis::$sd7];' . "\n" . $this->opername($item);    }    /** readexec_storename() */    public function storename(array $item): string    {        $t = Templates::$store[$item['c']] ?? throw new NoTemplate(sprintf('storename 0x%02x', $item['c']));        return $t($item, $this);    }    /** readexec_addname() */    public function addname(array $item): string    {        $t = Templates::$add[$item['c']] ?? throw new NoTemplate(sprintf('addname 0x%02x', $item['c']));        return $t($item, $this);    }    /** readexec_addname_swap(): sd7 / oldsd7 swapped, then the add target */    public function addnameSwap(array $item): string    {        return '[Alis::$sd7, Alis::$oldsd7] = [Alis::$oldsd7, Alis::$sd7];' . "\n" . $this->addname($item);    }    /**     * A PHP string literal of $bytes on one line (the compiler indents every line of a body: a raw new line inside a     * literal would change the string): printable ASCII as is, every other byte as \xNN.     */    public static function str(string $bytes): string    {        $out = '"';        foreach (str_split($bytes) as $ch) {            $o = ord($ch);            $out .= match (true) {                $ch === '"', $ch === '\\', $ch === '$' => '\\' . $ch,                $o >= 0x20 && $o < 0x7f => $ch,                default => sprintf('\\x%02x', $o),            };        }        return $out . '"';    }    /** a fresh temporary variable number for a template (unique in the script's PHP) */    public int $tmp = 0;}