Saturday, December 3, 2011

PHP Compiler Part One: Translating PHP

A PHP Compiler. Hmmm.. That seems new doesn't it? Apparently not. It seemed like a radically-different-world-changing idea when it first struck me one morning. But later that day I learnt tht I was beaten to it by Facebook.... and a whole lot of other people! Anyway, this seemed like an interesting project.

PHP is a scripting language. Scripts are compiled into Opcodes and run on a virtual machine called the Zend Engine. It's a bit like the JVM for Java, except here, scripts have to be compiled each time before execution. In Java, code is compiled once into .class files which are then run on the JVM. The basic idea behind this project is to make PHP faster by compiling it. Machine code is supposed to execute faster than any interpreted language right?

Obviously I was going to need some help with this. Google's always been a good friend of mine at times like this. What I was looking for was something to translate PHP code to C/C++ so that I could use gcc/g++ to compile it to machine code. These are some of the projects I came across.

php2cpp - "The PHP to C++ Translation tool". Downloaded the source, compiled it and translated a tiny script. php2cpp uses a set of translation rules (these) to perform statement-by-statement translation of PHP code to semantically equivalent C++ code. That's all well and good. But then what about PHP's inbuilt functions?? Surely C++ does not have equivalent functions for Every Single function in PHP. Or what about code that makes use of extensions??? I didn't bother finding out as I wasn't able to get anything to actually compile after translation with php2cpp.

Then there's Roadsend. According to the description on Roadsend.com, you can use it to make web applications with FastCGI or even use it with PHP-GTK to make offline applications (with an embedded web-server). Hmmm, desktop application development with PHP. Weird, but if you are interested, check out Roadsend and WinBinder.

Facebooks HipHop - You should probably read this. Definitely better than anything I can write about it here.

PHC - This is what I found next. PHC has been in development since 2005. It supports the entire standard library of functions in PHP. The author claims in his blog that he was sort of a pioneer in this field (and I believe him. He's got a PhD in "compiler optimizations, static analysis and scripting languages") but back then he wasn't able to convince anyone to "give a shit", not even Facebook. And that was before they came up with HipHop. But now that Facebook's on-board, everyone's interested again in the field! Beware, the idiots on the Internet will have you believe that Facebook invented the concept. Now you know the truth.

BinaryPHP is what I eventually ended up using (mainly because it worked on the first try). I did not test it extensively but from my experience, I can tell you that it supports a few, not all, of PHP's standard library functions. It even supports the MySQL functions. I'll admit, I ended up not testing anything else once I got this working. May be some other time. This one works after all.

So ok. I can translate PHP code to C++ code and even compile it. Now I needed it to work under Apache, my web-server. More on that in Part 2.

2 comments: