lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

A few years ago, I did a project for my Bachelor in CS which consisted of writing a Lua compiler for Parrot, the new Perl 6 virtual machine (www.parrotcode.org). The initial project more or less failed, as not much code could be compiled. However, later on, I restarted the project, and that worked pretty good, although many library functions weren't implemented on Parrot yet. (but for instance, the "life" program, in the Lua examples directory could be compiled and run!).

However, the code generated wasn't too good, the above mentioned life program ran many times slower. So, I thought of another way to run Lua code on Parrot: translating bytecodes. A few months ago, I started this idea, and the current version is 0.5.3. With this version, all Lua 5.0 instructions are implemented, and it can support other Lua instruction sets as well. As soon as all the documentation for Lua 5.1 is ready (especially the No Frills Intro to Lua 5 VM Instructions by Kein-Hong Man), I can start to implement the 4 new Lua 5.1 instructions as well. The bytecode translator will automatically detect the version of the bytecode, and load the correct instruction set. So, once Lua 6.0 is released (probably with a different bytecode layout and maybe some new instructions) it should be easy to add support for that version as well.

The translator is pretty simple, it will read in a compiled Lua chunk, and translate each instruction to the Parrot equivalent (Parrot Intermediate Representation, or PIR) instruction(s). As Lua instructions do a lot of assumptions on what's in the registers, sometimes there are a number of "move" instructions, in other words, copying data from one register to another, just for the purpose of an instruction that expects the data at that specific target register. Parrot does not do these kinds of assumptions, and I expect some optimizations could be done over here. I didn't start on this part of the translator, though.

If you are interested, you can find this project at http://members.home.nl/joeijoei/lua2pir

Please note that it is version 0.5.3, and many Lua libs are not implemented for Parrot. Also, you would need Parrot to be able to use it, but without it, you would get a pretty good idea of what Parrot code looks like. As Parrot will be the next engine for Perl, in a couple of years, Parrot will be available on many platforms by default, running Lua code can then be run on the same platforms as Perl. Finally, it's important to realize that the translated bytecodes are still somewhat slow, as Lua is very lightweight, and Parrot has many built-in features which Lua does not have.

Kind regards,
Klaas-Jan Stol