lua-users home
lua-l archive

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

For the past few weeks I've been working on a C compiler for Lua. It's now in a sufficient state to run non-trivial programs and benchmarks, although it's by no means finished.

(This will move to a real URL soon.)

Clue is based on the Sparse C compiler frontend. It generates Lua 5.1.3 bytecode by emitting Lua source and running it through a modified version of luac to patch in goto instructions (thanks to Luiz de Figueiredo for the patch to make this work). Currently most ANSI C C89 programs and some C99 programs will compile, although there are big holes in the coverage (varargs, switch, for example).

There's a libc, but I've only implemented enough to make the sample benchmark programs work.

The code it generates isn't particularly great because sparse is completely undocumented and I had to learn to work it as I went along; the register allocator and code generator needs throwing away and rewriting, for example. This makes the benchmarks rather poor. But at least at this point they can only improve.

And, well, it turns out that there's quite a lot of scope for improvement...

Whetstone benchmark (gcc):              820
                    (clue with LuaJIT):   7.2
                    (clue with Lua):      3.6

I've also tested with some of the Computer Language Benchmark Game benchmarks for C, some of which turn out to have been written by our very own Mike Pall; the results vary considerably from benchmark to benchmark:

gcc:       0.055           1.000
lua:       8.687         158.449
luajit:    3.669          66.917
gcc:       0.340           1.000
lua:      25.372          74.570
luajit:    3.619          10.637
gcc:       0.264           1.000
lua:       1.127           4.263
luajit:    0.472           1.784
gcc:       0.007           1.000
lua:       4.304         613.131
luajit:    2.254         321.163
gcc:       0.461           1.000
lua:      42.145          91.393
luajit:    9.565          20.742
gcc:       0.013           1.000
lua:       5.375         411.633
luajit:    2.162         165.576
gcc:       0.360           1.000
lua:      57.219         158.732
luajit:   29.389          81.529

(The first column is absolute time in seconds; the second column indicates the speed relative to the time gcc took. Yes, I'm aware that come of the benchmarks took such a short amount of time that the results will be hopelessly inaccurate.)

I suspect that a major reason for the poor performance is the pointer emulation code. Currently every C pointer dereference takes at least two array lookups and a function call. If the code generator gets rewritten, this should be fairly easily fixable.

It's all currently deeply experimental, buggy, half-finished and potentially useless, but I hope it's at least interesting...

David Given