lua-users home
lua-l archive

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


Jason P wrote:


I don't know if anyone else would be interested, but I would like to see a dynamic JIT style compiler for LUA opcodes to machine code. It would be callable inside the LUA via a 'JIT_Compile(<function>)' interface perhaps? This is one method I have thought about implementing:

Look through/Learn the LUA internal Opcode C Source.
Bundle TinyCC with LUA.
Write a routine to parse LUA opcodes into one C source program, wrapped as a LUA function.
Pass that program to TinyCC to compile dynamically.
Get the function symbol back from TinyCC and register it with LUA.


I have also considered (offline) compilation of Lua byte codes to
C-code. In addition to the metamethod problem which LHF mentioned
I have been worried about the added bouncing between C and Lua
language environments and its cost.

My first try on the code generation would basically unroll
the loop in luaV_execute for each instruction (each instruction
would be a constant value for this unrolled program), and let
the C optimizer do its work. Obviously dropping the
unused branches from the main opcode switch would clean the resulting
mess a lot.

This might give noticeable performance benefits, because currently
on my profiling the expensive part of the interpreter is that
the code in luaV_execute has lots of  branches which are
impossible to guess correctly by the processor. With
a compile time constant opcode the compiler should get rid of these
totally.

Btw. TinyCC seems to be under LGPL license so I would not touch it
for my current projects.


		Eero