HOME

TheInfoList



OR:

LuaJIT is a tracing just in time compiler for the
Lua Lua or LUA may refer to: Science and technology * Lua (programming language) * Latvia University of Agriculture * Last universal ancestor, in evolution Ethnicity and language * Lua people, of Laos * Lawa people, of Thailand sometimes referred t ...
programming language.


History

The LuaJIT project was started in 2005 by developer Mike Pall, released under the MIT open source license. The second major release of the compiler, 2.0.0, featured major performance increases. The latest release, 2.0.5 is released in 2017. Since then, the project is not currently maintained by developers other than contributors.


Notable users

* CERN, for their Methodical Accelerator Design 'next-generation' software for describing and simulating particle accelerators * OpenResty, a fork of
nginx Nginx (pronounced "engine x" ) is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software ...
with Lua scripting * Kong, a web API gateway *
Cloudflare Cloudflare, Inc. is an American content delivery network and DDoS mitigation company, founded in 2009. It primarily acts as a reverse proxy between a website's visitor and the Cloudflare customer's hosting provider. Its headquarters are in San ...
, who use LuaJIT in their
web application firewall A web application firewall (WAF) is a specific form of application firewall that filters, monitors, and blocks HTTP traffic to and from a web service. By inspecting HTTP traffic, it can prevent attacks exploiting a web application's known vu ...
service


Performance

LuaJIT is often the fastest Lua runtime. LuaJIT has also been named the fastest implementation of a dynamic programming language. LuaJIT is sometimes hailed as competitive to the performance of C++. LuaJIT includes a
Foreign Function Interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Naming The term comes from the specification for Common Lisp, which explicit ...
compatible with C data structures. Its use is encouraged for numerical computation.


Tracing

LuaJIT is a tracing just-in-time compiler. LuaJIT chooses loops and function calls as trace anchors to begin recording possible hot paths. Function calls will require twice as many invocations to begin recording as a loop. Once LuaJIT begins recording, all control flow, including jumps and calls, are inlined to form a linear trace. All executed bytecode instructions are stored and incrementally converted into LuaJIT's Static single-assignment
Intermediate representation An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation. A "good ...
. LuaJIT's trace compiler is often capable of
inlining In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compilation, without ch ...
and removing dispatches from object orientation,
operators Operator may refer to: Mathematics * A symbol indicating a mathematical operation * Logical operator or logical connective in mathematical logic * Operator (mathematics), mapping that acts on elements of a space to produce elements of another sp ...
, and type modifications.


Internal representation

LuaJIT uses two types of internal representation. A stack-based bytecode is used for the
Interpreter (computing) In computer science, an interpreter is a computer program that directly execution (computers), executes instructions written in a Programming language, programming or scripting language, without requiring them previously to have been Compiler, co ...
, and a static-single assignment form is used for the just-in-time compiler. The interpreter bytecode is frequently patched by the JIT compiler, often to begin executing a compiled trace or to mark a segment of bytecode for causing too many trace aborts. -- Loop with if-statement local x = 0 for i=1,1e4 do x = x + 11 if i%10

0 then -- if-statement x = x + 22 end x = x + 33 end
---- TRACE 1 start Ex.lua:5 ---- TRACE 1 IR 0001 int SLOAD #2 CI 0002 > num SLOAD #1 T 0003 num ADD 0002 +11 0004 int MOD 0001 +10 0005 > int NE 0004 +0 0006 + num ADD 0003 +33 0007 + int ADD 0001 +1 0008 > int LE 0007 +10000 0009 ------ LOOP ------------ 0010 num ADD 0006 +11 0011 int MOD 0007 +10 0012 > int NE 0011 +0 0013 + num ADD 0010 +33 0014 + int ADD 0007 +1 0015 > int LE 0014 +10000 0016 int PHI 0007 0014 0017 num PHI 0006 0013 ---- TRACE 1 stop -> loop ---- TRACE 2 start 1/4 Ex.lua:8 ---- TRACE 2 IR 0001 num SLOAD #1 PI 0002 int SLOAD #2 PI 0003 num ADD 0001 +22 0004 num ADD 0003 +33 0005 int ADD 0002 +1 0006 > int LE 0005 +10000 0007 num CONV 0005 num.int ---- TRACE 2 stop -> 1


Extensions

LuaJIT adds several extensions to its base implementation, Lua 5.1, most of which do not break compatibility. * "BitOp" for binary operations on unsigned 32-bit integers (these operations are also compiled by the just-in-time compiler) * "CoCo", which allows the VM to be fully resumable across all contexts * A
foreign function interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Naming The term comes from the specification for Common Lisp, which explicit ...
* Portable bytecode (regardless of architecture, word size, or endianness, not version)


DynASM

DynASM is a lightweight
preprocessor In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which is often used by s ...
for C which was created for LuaJIT 1.0.0 to make developing the just-in-time compiler easier. DynASM replaces assembly code in C files with runtime writes to a 'code buffer', such that a developer may generate and then evoke code at runtime from a C program. DynASM was phased out in LuaJIT 2.0.0 after a complete rewrite of the assembler, but remains in use by the LuaJIT contributors as a better assembly syntax for the LuaJIT interpreter. DynASM includes a bare-bones C
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
which is used at compile time for logic the preprocessor generates. The actual preprocessor is written in Lua.


Example

, .type L, lua_State, esi // L. , .type BASE, TValue, ebx // L->base. , .type TOP, TValue, edi // L->top. , .type CI, CallInfo, ecx // L->ci. , .type LCL, LClosure, eax // L->ci->func->value. , .type UPVAL, UpVal , .macro copyslot, D, S, R1, R2, R3 , mov R1, S.value; mov R2, S.value.na mov R3, S.tt , mov D.value, R1; mov D.value.na R2; mov D.tt, R3 , .endmacro , .macro copyslot, D, S; copyslot D, S, ecx, edx, eax; .endmacro , .macro getLCL, reg , , if (!J->pt->is_vararg) else , .endmacro , .macro getLCL; getLCL eax; .endmacro .. static void jit_op_getupval(jit_State *J, int dest, int uvidx) { , getLCL , mov UPVAL:ecx, LCL->upvals vidx , mov TOP, UPVAL:ecx->v , copyslot BASE est TOP }


References

Compiler construction