Compile Rust to Linux ELF with a PHP‑Based Compiler – No LLVM Needed
An open‑source PHP‑written Rust compiler can generate native x86‑64 Linux ELF binaries without LLVM, assemblers, or linkers, offering ownership, borrowing, type checking, generics, traits, closures, and more; the guide covers installation via PHP and WSL, usage commands, supported features, and current limitations.
Introduction
This project implements a Rust compiler entirely in PHP. The compiler generates native x86‑64 Linux ELF executables without relying on LLVM, an external assembler, or a linker. It supports core Rust language features such as ownership checking, borrowing, type checking, move semantics, generics, traits, closures, and iterators, making it usable on environments where only PHP is available.
Installation
Install PHP first. On Windows 11 the latest version can be installed via winget: winget install PHP.PHP.8.4 Because the generated binaries target Linux, Windows users should run them inside the Windows Subsystem for Linux (WSL). Install Ubuntu with: wsl --install After the installation finishes, restart the computer and complete the initial Ubuntu setup.
Usage
Compile a .rs source file with the PHP script: php rustc.php main.rs -o main Run the resulting binary inside WSL: wsl ./main To view the program’s exit code:
wsl ./main; echo $?Quick test
Example HelloWorld.rs:
fn main() {
println!("开源技术小栈!");
}Compile and run:
php rustc.php HelloWorld.rs -o HelloWorld
wsl ./HelloWorldThe executable prints:
开源技术小栈!Supported features
Types
i32, bool, u8, u16, u32, u64, u128,
usize String(heap‑allocated, move semantics) &str and string slices with indexing (e.g., s[0])
References &T and &mut T with borrow checking
Structs with named fields, field access, and method calls
Enums with tuple‑variant data and match support
Unit type () (usable in expressions, return types, generics)
Generics on functions, structs, and impl blocks
Built‑in Option<T> and Result<T, E> with Some/None and
Ok/ErrControl flow
if/ else (usable as expressions) while, loop, break, continue Range‑based
for x in start..end matchwith enum branches and wildcard
_ returnFunctions & closures
Free functions with multiple parameters and explicit return types const fn (accepted but treated as regular functions) impl blocks supporting self, &self, and &mut self Trait definitions and impl Trait for Type Default trait method implementations
Closures capturing values by value, e.g.,
|x: i32| x + captured_varOwnership & borrowing
Move semantics for non‑ Copy types
Immutable and mutable borrow checking
Automatic Copy derivation for i32, bool, &T, and structs/enums whose fields are all copyable
Modules & syntax
File‑based module declaration: mod name; Visibility control with pub Path imports using use Attributes #[...] and #![...] (parsed but largely ignored)
Output
println!("{}", expr)– currently only a single {} placeholder is supported exit(code) – explicit program termination with status code
Testing
Run the full test suite with: php tests/run.php Test cases reside in tests/cases/ and are organized into sub‑directories such as fundamentals/valid, fundamentals/invalid, modules, and programs. Each .rs file may start with comments indicating the expected outcome, for example:
// exit: 42
// stdout: hello
// error: Use of moved valueUnimplemented high‑priority features
Compound assignment operators ( +=, -=, *=, /=)
Tuple types and destructuring
Heap‑allocated Vec<T> Floating‑point types f32 and
f64 constand static items
The ? operator (syntax not yet implemented despite Result support)
Closures as function parameters, e.g., fn apply(f: impl Fn(i32) -> i32) Parameter‑less closures ( || expr)
Complex pattern matching beyond single‑level enum variants
Enhanced println! formatting (multiple placeholders)
Lifetime annotations (current borrow checker is a simplified version)
Additional signed integer types ( i8, i16, i64, i128)
Repository
Source code: https://github.com/mrconter1/rustc-php
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
