Friday, July 8, 2016

Ravi: A JIT for Lua 5.3

Recently I was looking for a JIT compiled implementation of Lua 5.3 to work with for a project. Then I found Ravi - a JIT implementation for Lua 5.3 extended with optional static typing for local variables. Ravi is available on Windows, Mac, Linux, and pretty much any other system with cmake, a C compiler, and libgccjit or LLVM. It can also be run without a JIT.

The static typing is pretty simple. Here is an example:

local a
local b: interger

Currently, variables can be statically typed as intergers, numbers, tables, interger arrays, and number arrays. Arrays are not compatable with tables because they can only be indexed by intergers. Example for declaring an array:

local c: interger[]

Arrays can be initialized similarly to tables though:

c={}

But, they are not compatable with tables:

local d: table ={}
c=d --error

The method of creating arrays mentioned before creaates dynamically sized arrays, but fixed size arrays can be declared with table.intarray and table.numarray.

JIT compilation may be either done in manual or automatic mode. In manual mode, the code must run ravi.compile on the function. As an optional second argument to ravi.compile, you may specify compiler options. In automatic mode, ravi will automatically JIT any function greater than a certain size (defaults to 150 instructions) or that has been run more than a certain number of times (defaults to 50 calls).

Ravi also has a binding for LLVM - so you can implement a JIT in Ravi. This binding can access C functions. Ravi can run normal Lua source code without a problem - but not bytecode. Ravi has a slightly different bytecode in order to accommodate additional instructions, but has a limit of 125 registers.

Ravi is by Dibyendu Majumdar and can be found here on GitHub.

No comments:

Post a Comment