Peachpie Open Source PHP to .NET Compiler
Written by Nikos Vaggalis   
Monday, 24 October 2016
Article Index
Peachpie Open Source PHP to .NET Compiler
Advantages of Roslyn

Fast forward to the future, at a time that we can fully experience   Roslyn's profound effects on compiler construction, by looking into the building of the Peachpie PHP compiler.

 

 

What does Jakub Míšek, a member of Peachpie's core developer team have to say about the advantages of using Roslyn?

  1. Massive performance improvement and built-in mechanism for handling dynamic objects. Crucial functionality for code emitting, parsing assemblies and the structure of the compiler itself that result in assemblies portability and the possibility of integrating Peachpie with tools available only for C# (code analysis, VS extensions).
  2.  Cross platform capability since Roslyn produces portable class libraries compatible with Mono and the .NET Core.Subsequently, since Mono and Xamarin can run on Windows, Linux, Android, Windows Phone, Windows 10 IoT Core, iOS or OSX, Peachpie can also work on  mobile devices too.
  3. Visual studio integration in debugging PHP code and other functionality including code colourization, syntax highlighting and IntelliSense using the extension PHP Tools for Visual Studio.

That's about Roslyn, what about the DLR, where does it fit into the picture?

DLR is still an important part or runtime, but Roslyn simplifies and improves the compiler implementation. The port of PHP on .NET uses the same mechanism as "dynamic" in C#, but in the future, classes will provide DLR interfaces for complete DLR compatibility.

So while not yet fully compatible with the DLR, it generates the use of callsites and implements the callsites' binders. Roslyn has helper classes called ILBuilder and MetadataWriter, which in addition to their small footprint and much greater performance in comparison to old .NET ILBuilder, it allows for a more low-level emission of IL.Thanks to their low level features, it makes possible for a more efficient use of the call site objects. 

In the future, we'll generate class declarations that implement the IDynamicMetaObjectProvider interface, so that when every other dynamic language as well as C#  use the 'dynamic' keyword they will treat PHP classes correctly, with PHP semantics. E.g. if I write

dynamic x = new SomePhpClass();

x.SomeNonExistingFunction();

it will properly call 'SomePhpClass::__call()'


On using C# to call PHP:


The command line utility produces a command line EXE or portable DLL, which can then be used in your C# project as a reference,so for example pchp /target:exe test.php will create a command line executable program, which in turn will start executing global code in first script provided – test.php.

What is more interesting, using the /t:library argument, we can create a dynamically linked library file (dll), which can be referenced by a C# (or any other .NET) project. In other words, we have already managed to achieve one-way interoperability, enabling the calling of PHP from C#.

 

peachprocess

 

This is demonstrated by compiling the following PHP program into a library, a .dll, using the aforementioned switch.:


< ?php echo "Computing Pi ... :", leibniz_pi(10000); class PhpNumberTest { /** * @param int $steps */ static function leibniz_pi($steps) { return leibniz_pi($steps); } } /** * @param int $steps */ function leibniz_pi($steps) { $pi = 4.0; $top = 4.0; $bot = 3.0; $minus = TRUE; for ($step = 0; $step < $steps; $step ++) { $pi += ( $minus ? -($top/$bot) : ($top/$bot) ); $minus = ( $minus ? FALSE : TRUE); $bot += 2; } return $pi; }

 

Checking the dll under  ILSpy reveals that the PHP function has turned strongly typed!:


 

It returns a double and gets a long instead of a generic value.This of course brings up the typing system debate; strong vs weak, static vs. dynamic. A refresher on the type systems, can be found in our  Type Systems Demystified series.

In essence what Jakub describes is that Roslyn facilitated the construction of a high end and versatile compiler in Peachpie in the quickest way possible, enhanced with capabilities never thought possible before:

PHP code compiled into MSIL, and .NET assemblies containing PHP objects? now it is possible..

But what could PHP gain as a fully qualified member of the .NET languages family?

In its turn the .NET platform through the DLR would grant stellar performance to PHP, since the PHP code is now a candidate of full static compilation. This would be a good approach to take into consideration in RPerl's proceedings in forking a statically compiled version of Perl 5. Check RPerl - Running Perl 5 Faster for more on that.

Then,

  • you can reference libraries directly from PHP in a C# project and enable bidirectional interoperability of PHP and C# the likes of overriding PHP objects in C# and vice versa.
  • Use of the .NET debugging features, and NuGet's distribution & updates.
  • Since the code is now managed there's no more memory leaks, plus PHP  can now benefit of method-level and assembly level security.     

Peachpie while currently only capable of supporting a subset of the PHP language, has already implemented the following feature list :

  •     Compiling global functions
  •     Compiling class functions (no overloading yet)
  •     Loops, goto statements, if statements
  •     Local variables, assignments, number comparison, numeric   operators
  •     Converting between number, boolean, long and double
  •     Echo of string, number and boolean
  •     Returning a value from a function
  •     Type analysis on numbers
  •     Working natively with 64 bit integers (long)
  •     Calling global PHP functions

    
On its to-do list we find Traits, Closures, Delegates, Lambda functions, Eval, PHP Generators, porting of PHP packages such as
Streams, Mail, Serialization, PDO, PCRE, SPL, Reflection and much more.
 
So there you have it, PHP and .NET hand in hand living in harmony, completing each other and building on each others advantages, all due to Roslyn's revolutionary vision and the ingenuity of the Peachpie team. It really gives new meaning to the age old quest of language interoperability.

 

pplogo

More Information

Peachpie

Peachpie on Github

Related Articles

Pro DLR 4.0 in .NET book review

IronJS - In Conversation with Fredrik Holmströmm

Where is Perl Heading? Interview with Jonathan Worthington

Niecza - Perl 6 Implemented in .NET

C# Guru - An Interview With Eric Lippert

RPerl - Running Perl 5 Faster

Type Systems Demystified

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter,subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin

 

{loadposition moreNEWS}

{loadposition moreNEWSlist}

 

{loadposition comment}

<ASIN:1430230681>

<ASIN:B004EWGKXQ>

 



Last Updated ( Monday, 24 October 2016 )