lua-users home
lua-l archive

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


On Tuesday 13, Mark Feldman wrote:
> One of the technologies I've been working on which I've talked about
> here before is a modified version of the Lua compiler that spits out C
> code instead of compiled Lua. The concept is simple enough: you convert
> all your Lua functions to C functions and substitute each op code with
> the equivalent C instructions from the VMs inner loop. In return you get
> much faster execution by eliminating the VM loop mechanics and taking
> advantage of the C compilers optimizer. You have to pull a few tricks
> along the way to detect and handle things like tail recursion and
> coroutines, but overall it works perfectly and integrates seamlessly
> without having to modifying the Lua code base in any way (it moves all
> your code to the program segment too, which can be handy on very low-end
> devices).

My llvm-lua project did the same thing, but generated LLVM IR code and had 
LLVM compile that to machine code.

Just last night I finished most of the work of porting my llvm-lua project to 
a new [1] 'slua' project that directly generates C code instead of 
translating Lua bytecodes into LLVM IR code.  I got a few example programs to 
compile & run last night.  It only supports compiling Lua scripts to a 
standalone binary right now.  I still need to add command line options to the 
compiler for setting optimization level, turning on debug support, and 
compiling to a Lua module (.so).  This project has no dependence on LLVM and 
the C code that it generates can be compiled with any C compiler.

I don't have a Mac or iPhone, so I can't really help with using this project 
for iPhone development, but it shouldn't be to hard to get working.  I 
started this port a few days ago when the whole "Goodbye Lua on iPhone" 
thread got started, since I had been thinking of trying this port for some 
time.  I don't know if this will get around the new iPhone SDK legal 
restrictions, but it might be helpfull to people anyways.

Attached is an example of how the following Lua code looks as C code:
print("hello")

To compile the attached C code you will need most of the .c files from the 
slua project.  Also note that this project doesn't have JIT support, some of 
the variable names still use jit_* prefixes like they where in the llvm-lua 
project.  I will be changing them later.


1. http://github.com/Neopallium/slua

-- 
Robert G. Jakabosky
#include "lua_vm_ops.h"
#include "load_jit_proto.h"
#include <stdint.h>

static int __tmp_hello_lua_0_0(lua_State * L) {
	LClosure * cl0;
	TValue * k1;
	uint32_t retval2;
	uint32_t retval3;
block_entry:
	cl0 = vm_get_current_closure(L);
	k1 = vm_get_current_constants(cl0);
	vm_OP_GETGLOBAL(L, k1, cl0, 0, 0);
	vm_OP_LOADK(L, k1, 1, 1);
	retval2 = vm_OP_CALL(L, 0, 2, 1);
	retval3 = vm_OP_RETURN(L, 0, 1);
	return retval3;
}

static constant_type constants0[] = {
	{ 3,5, { .str = "print"} },
	{ 3,5, { .str = "hello"} },
};
static uint32_t proto_code1[] = {
	5,
	16449,
	16793628,
	8388638,
};
static uint32_t proto_lineinfo2[] = {
	1,
	1,
	1,
	1,
};
 jit_proto jit_proto_init = {
	"@/tmp/hello.lua",
	__tmp_hello_lua_0_0,
	0,
	0,
	0,
	0,
	2,
	2,
	2,
	0,
	0,
	0,
	4,
	4,
	constants0,
	NULL,
	NULL,
	NULL,
	proto_code1,
	proto_lineinfo2,
};