EN
games/alis / src / Alis / Compiled / Templates / Flow.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Compiled\Context;use App\Alis\Compiled\Templates;/** Jumps and branches (cjmp*, cbz*, cbnz*, cbeq*, cbne*): the targets known at compile time. */final class Flow{    public static function register(): void    {        foreach ([8, 16, 24] as $bits) {            Templates::$op["cjmp$bits"] = static fn (array $i, Context $c): string => Context::go(self::target($i));            Templates::$op["cbz$bits"] = static fn (array $i, Context $c): string => self::branch('\App\Alis\bztest() == 0', $i);            Templates::$op["cbnz$bits"] = static fn (array $i, Context $c): string => self::branch('\App\Alis\bztest() != 0', $i);            Templates::$op["cbeq$bits"] = static fn (array $i, Context $c): string => self::branch('\App\Alis\beqtest() != 0', $i);            Templates::$op["cbne$bits"] = static fn (array $i, Context $c): string => self::branch('\App\Alis\beqtest() == 0', $i);        }    }    private static function target(array $i): int    {        foreach ($i['ops'] as $op) {            if ($op[0] === 'j') {                return $op[2];            }        }        throw new \LogicException('no jump operand');    }    private static function branch(string $cond, array $i): string    {        return sprintf('$s->pc = $o + (%s ? 0x%x : 0x%x);', $cond, self::target($i), $i['end']);    }}