

Making a JIT interpreter with LuaJIT - Rexxar
http://rjpower.org/wordpress/making-a-jit-with-luajit/

======
daeken
Very cool article.

> Still, it’s somewhat surprising how easy it was to generate our ‘JIT’
> interpreter — the code isn’t much bigger then the original version. Perhaps
> with some more scaffolding/helper libraries, this could be a viable way to
> create fast interpreters for new languages?

This is one of the many things that Terra excels at and is intended for:
[http://terralang.org/](http://terralang.org/) I'm personally using it for
dynarecing code in emulators now; works great.

------
oh_teh_meows
So in short, this is basically a specialized transpiler (transpiling your
bytecodes into high level Lua code) + a call to eval to evaluate the
transpiled code using LuaJIT's JIT facility.

This seems like a fairly useful technique, provided the given bytecode file
isn't terribly big (since you're uh...converting it into string
representation), and that the bytecode isn't some run-once-throw-away code,
otherwise you spend more time converting it into Lua code and JIT-ing it.

Having said that, I wonder if it'd be even less of a hassle by skipping the
bytecode part. Just transpile the source written in your custom language
straight into Lua code, and have that code JIT'ed instead. Many high level
structures/information would be preserved, allowing JIT to do an even better
job perhaps?

~~~
rjpower9000
Yes; this could potentially be slow. If for example, you had an application
that just had straight line code (no loops), and you were running it from the
command line a million times you would end up hurting from this approach.
Generating LuaJIT bytecode instead of source code would (I think) make this
less of an issue.

In this case, the bytecode is what you start with :)

Going straight from a parse tree to Lua could work, but locks you into the
possibly slow code-generation approach. Having an intermediate level (high
level bytecode, or even an interpreter over the AST) would be more flexible --
you could choose when to switch over the compiled version.

