phplang 0.1.0

PHP as a fusevm frontend: a lexer/parser and compiler to fusevm::Chunk on a PhpHost object heap, with no bespoke VM or JIT — the first compiled standalone PHP runtime
Documentation
██████╗ ██╗  ██╗██████╗ ██╗      █████╗ ███╗   ██╗ ██████╗
██╔══██╗██║  ██║██╔══██╗██║     ██╔══██╗████╗  ██║██╔════╝
██████╔╝███████║██████╔╝██║     ███████║██╔██╗ ██║██║  ███╗
██╔═══╝ ██╔══██║██╔═══╝ ██║     ██╔══██║██║╚██╗██║██║   ██║
██║     ██║  ██║██║     ███████╗██║  ██║██║ ╚████║╚██████╔╝
╚═╝     ╚═╝  ╚═╝╚═╝     ╚══════╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝

Rust Docs Built on status license

[PHP, COMPILED TO BYTECODE — ON A SHARED CRANELIFT JIT]

"Zend compiles PHP to its own opcodes and walks them. phplang lowers PHP to a shared machine that other languages already run on, and lets a tracing JIT compile the hot loops."

phplang is PHP as a fusevm frontend — a lexer/parser and compiler that lowers PHP to fusevm::Chunk bytecode running on fusevm's bytecode VM + tracing Cranelift JIT, over a PhpHost object heap. There is no bespoke interpreter loop: phplang is a pure front end; execution and codegen live in fusevm — the same engine behind zshrs, strykelang, awkrs, pythonrs, and rubylang.

It is, to our knowledge, the first compiled standalone PHP runtime. The binary is php.

Read the Docs · Engineering Report · fusevm


Table of Contents


[0x00] OVERVIEW

phplang keeps PHP the language and throws away Zend's execution model. It lexes and parses PHP (two-mode: inline-HTML passthrough plus <?php … ?> code), lowers the AST to fusevm bytecode, and runs it on the shared bytecode VM with a tracing Cranelift JIT. Arithmetic lowers to native ops so the JIT can trace hot loops; PHP-specific behavior — loose comparison, string↔number coercion, array semantics, the standard library — is served by the PhpHost object heap.

It carries no VM or JIT of its own. Bug fixes and JIT improvements in fusevm land once and benefit every hosted frontend at the same time.

[0x01] PIPELINE

source ──▶ lexer ──▶ parser ──▶ compiler ──▶ fusevm::Chunk ──▶ fusevm VM + JIT
              │         │           │                                  │
         two-mode   PHP AST    lower to bytecode              callbacks into PhpHost
        (HTML/PHP)            (native ops + CallBuiltin)      (builtins + numeric hook)
  • Scalars (int, float, bool, string, null) ride through the VM as native fusevm::Values.
  • Arrays are heap objects in PhpHost; they travel as Value::Obj(u32) handles into that heap.
  • Arithmetic + - * lowers to native fusevm ops so the JIT can trace hot loops; a strict numeric hook supplies PHP coercion when an operand is a string/array/null or an i64 op overflows. / % **, concatenation, comparisons, and everything PHP-specific lower to CallBuiltin handlers.

[0x02] USAGE

php script.php              # run a file
php -r 'echo 1 + 1;'        # run a one-liner (no <?php tag needed)
php -a                      # interactive REPL (persistent state per line)
php --dump-bytecode f.php   # print the lowered fusevm bytecode

A man/php.1 man page and runnable examples/*.php ship with the crate.

[0x03] SUPPORTED TODAY

A working core, grown outward from the sibling frontends. Implemented and tested end-to-end (see tests/basic.rs):

  • <?php … ?> tags with inline-HTML passthrough; <?= short echo; #, //, and /* */ comments.
  • Scalars, single- and double-quoted strings with $var interpolation, escapes.
  • Variables, arithmetic (+ - * / % **), string concat (.), compound assignment (+= .= …), pre/post ++/--.
  • Loose/strict comparison (== != === !== < > <= >=, PHP-8 string↔number ordering), short-circuit && || and or, ternary ?: (incl. the elvis short form), null-coalesce ??, !, and (int)/(float)/(string)/(bool) casts.
  • Indexed, associative, and appended ($a[] =) arrays; index read/write.
  • if / elseif / else, while, do … while, for, foreach ($a as [$k =>] $v), switch (with fall-through), break, continue, return; match expressions.
  • User functions with positional parameters and recursion.
  • A ~90-function standard library across strings (str_*, substr, trim, sprintf, number_format, htmlspecialchars, chr/ord, …), arrays (array_map/filter/reduce/merge/slice/keys/values, sort family, in_array, array_sum, range, …), math (abs/floor/ceil/round/sqrt, trig, intdiv, fmod), type/util (is_*, gettype, json_encode, var_dump, print_r, var_export).

[0x04] NOT YET (LATER WAVES)

Classes, interfaces, traits, namespaces, closures/arrow functions, references, try/catch, generators, superglobals, default/variadic/typed parameters, and deep ($a[b][c]) lvalues. A few current deviations, documented in-code: array semantics are reference-based rather than PHP's copy-on-write; array_pop/shift are omitted pending a host by-reference delete; match with no arm and no default yields null instead of throwing \UnhandledMatchError; loose comparison follows a simplified model. Persistent bytecode caching, AOT (--build), LSP, and DAP — present in the sibling frontends — are not wired yet.

[0x05] PARITY FUZZER

parity-fuzz is a differential fuzzer: it generates seed-deterministic PHP snippets, runs each through both the reference php and phplang, and reports every case where stdout or success/failure diverges. It is a development tool — it needs a reference php on PATH, so CI never runs it.

cargo build --bin parity-fuzz
./target/debug/parity-fuzz --count 5000        # fuzz 5000 cases
./target/debug/parity-fuzz --once --seed 1234  # replay one case, show both sides

Generators are biased toward where a PHP frontend is likely to disagree with the reference: float formatting, integer division/modulo signs, ** precedence, loose-vs-strict comparison, sort ordering, sprintf/number_format, and string↔number coercion. Divergences are delta-debugged to a minimal reproducer and grouped by signature; a full report lands in target/parity-fuzz/divergences.txt.

[0x06] BUILD

cargo build
cargo test

phplang is a standalone crate (an explicit empty [workspace] stops cargo walking up to the meta parent). fusevm is pulled from crates.io with the jit feature.

[0x07] DOCUMENTATION

[0xFF] LICENSE

MIT — free and open source. See LICENSE.