
A PHP Compiler, a.k.a. the FFI Rabbit Hole - ingve
https://blog.ircmaxell.com/2019/04/compilers-ffi.html
======
fernly
Ironically about the only acronym he does NOT try to explain is FFI, the
reason for the project.

> A foreign function interface (FFI) is a mechanism by which a program written
> in one programming language can call routines or make use of services
> written in another.

[https://en.wikipedia.org/wiki/Foreign_function_interface](https://en.wikipedia.org/wiki/Foreign_function_interface)

------
maxxxxx
When you think about how dynamic PHP is wouldn't you pretty much end up with
the source code and a PHP Interpreter baked together into a binary?

~~~
tyingq
From his GitHub page:

 _" Future Work - Right now, this only supports an EXTREMELY limited subset of
PHP. There is no support for dynamic anything. Arrays aren't supported.
Neither Object properties nor methods are supported. And the only builtin
functions that are supported are var_dump and strlen."_

[https://github.com/ircmaxell/php-
compiler/blob/master/README...](https://github.com/ircmaxell/php-
compiler/blob/master/README.md)

He basically has avoided that road thus far :)

I think you're right though. Things like eval() and ${$deref-me-at-runtime}
aren't going to be easily compiled.

~~~
thaumaturgy
There are many who would argue that removing those features from PHP would be
a massive improvement.

~~~
gchamonlive
Replacing php entirely would be a massive improvement. I only had sorrows with
it, the kind of language that is painful to write all the way through.

~~~
dpacmittal
PHP 7 is so much better and PHP 8 is on track to become even better.

~~~
gchamonlive
Can't really agree. 7.3 differs so much from 7.0 that migrating is just not
possible without careful code rewrite. How is a version good when it is
incompatible inside minor version releases?

I know little about version 8 though, but as long as they are not abandoning
backwards compatibility it won't make up for the language flaws

------
thrusong
Looks really cool, but it's a little above my skill level. Does this mean you
could protect your PHP source code if you wanted to compile and then
distribute it?

~~~
debatem1
Please don't do this. It's both ineffective and pisses off those of us who try
to produce good bug reports when your obfuscated software misbehaves.

~~~
munk-a
This is just a general issue with third party code - C/C++ libraries have
always suffered from this weakness. Still, I've worked in places where if we
were unable to distribute our library closed source the business wing wouldn't
let us distribute it at all - everyone is welcome to their thoughts on copy-
left software, but a thing that exists wouldn't have existed if we could only
release it open-source (and yes, we even explained how licensing worked to the
higher ups)

~~~
debatem1
I'm confused.

This is a compiler. The difference between distributing the output of this and
a compiled C or C++ library is basically that this emits machine code that no
sane C or C++ compiler would emit today. This does not prevent me from
figuring out how the parts you want kept secret work, it only spreads bugs out
over more code.

My objection is twofold: first, on practical grounds, this makes producing
good bug reports harder. That's because there's more code to prove isn't the
true source of the problematic behavior.

The second is aesthetic. If your goal is to keep your secrets, this does not
accomplish it.

~~~
Piskvorrr
No, such behavior does not make technical sense, any more than bits can be
coloured. Yet...
[https://ansuz.sooke.bc.ca/entry/23](https://ansuz.sooke.bc.ca/entry/23)

------
simplyinfinity
Another cool (and more functional) compiler is Peachpie. You can compile
WordPress and run it on. Net vm. From the reports it's even few times faster
than "native" php :)

[https://www.peachpie.io](https://www.peachpie.io)

------
sedeki
I appreciate the clear explainations of AOT, VM, etc. Someone tried to explain
AOT to me a while back, but I didn’t understand anything.

------
ufo
Does anyone know how this compares to PHC? It was an ahead-of-time compiler
for PHP.

[https://github.com/pbiggar/phc](https://github.com/pbiggar/phc)

~~~
pbiggar
phc is dead, so there's that :)

Apart from that, it seems to be following a similar path, but is a little bit
earlier down it. Could probably look at the code we generated and copy some of
the solutions.

PHC was also designed to support eval/dynamic features by embedding the PHP
interpreter and optimizing around it. Not sure that this implementation is
going down that path, which has upsides and downsides, the upside being that
eval can be handled. Would be interested to see what he ends up doing.

~~~
Twirrim
How heavily does eval get used in PHP?

~~~
pbiggar
More than you'd think. I did some analysis (in 2008 mind you, long time ago,
PHP4/PHP5). I think I found that half the packages on sourceforge
(sourceforge!) had a use of a dynamic construct.

When I looked into it, it wasn't so much eval as dynamic includes, often used
for localization:

    
    
        $lang = "en";
        include("l18n/$lang.php");

------
coderintherye
This is amazing work and great job documenting! I had no idea about the
Foreign Function Interface coming to PHP 7.4, that's exciting.

------
xfer
Wonder why meta-compilation is not more popular? There are already some
frameworks available like RPython for quite some time, Truffle is relatively
new.

------
dingdingdang
Very impressive work, will comment again if I manage a test compile of some
minor project! :)

------
jtd514
[https://en.wikipedia.org/wiki/HipHop_for_PHP](https://en.wikipedia.org/wiki/HipHop_for_PHP)

~~~
simcop2387
HipHop turned into HHVM, which as of Feb no longer supports PHP going forward.
[https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html](https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html)

