Lua’s authors do not document the precompiled format; it is defined by the source of
each release. This page follows ldump.c, lundump.c,
lopcodes.h, ldebug.c and lvm.c of Lua 5.0.3, from
lua.org (see sources). A chunk is written with the number sizes of
the machine that wrote it, which the header records; the loader reverses the byte order
when it has to, but refuses other sizes.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: escape and Lua. |
4 | 1 | version | 0x50: major × 16 + minor. |
5 | 1 | endianness | 1 little-endian, 0 big-endian: the first byte of an int holding 1. There is no format byte in 5.0. |
6 | 1 | sizeof(int) | Bytes in an int on the writing machine. |
7 | 1 | sizeof(size_t) | Bytes in a size_t. |
8 | 1 | sizeof(Instruction) | Bytes in an instruction. Instruction is unsigned long (llimits.h): 4 on 32-bit systems and 64-bit Windows, 8 on other 64-bit systems. |
9 | 1 | SIZE_OP | Width of the opcode field in bits; 6. |
10 | 1 | SIZE_A | Width of field A; 8. |
11 | 1 | SIZE_B | Width of field B; 9. |
12 | 1 | SIZE_C | Width of field C; 9. |
13 | 1 | sizeof(lua_Number) | Bytes in a number; 8 for the default double. |
14 | sizeof(lua_Number) | test number | (lua_Number)3.14159265358979323846E7, in the writer’s number format. |
With 8-byte numbers the header is 22 bytes. The main function follows directly. The
header has no flag that says whether lua_Number is a floating-point or an
integer type. A reader can tell from the test number: a floating-point build writes
31,415,926.5358979… (31,415,926 in single precision), an integer build writes the
integer 31,415,926. The loader converts both the number it read and its own to
long and compares only those, so differences in the fraction are
ignored. Lua 5.0 alpha and beta write another test
number.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| int | sizeof(int) bytes, in the header’s byte order. Every int that the loader reads as a count or a field must be 0 or more. |
| size_t | sizeof(size_t) bytes. |
| number | sizeof(lua_Number) bytes, the machine representation (an IEEE 754 double by default). |
| string | A size_t length that counts the terminating zero, then that many bytes including the zero. A length of 0 means no string. The string may hold zero bytes; the loader drops the last byte without looking at it. |
| list of T | An int count, then that many T. |
Function prototype
| Field | Type | Meaning |
|---|---|---|
| source | string | Source name. The writer stores no string when it equals the enclosing function’s source, and the loader then takes the parent’s. |
| lineDefined | int | Line where the function starts; 0 for the main function. There is no end line. |
| nups | byte | Number of upvalues. |
| numparams | byte | Number of fixed parameters. |
| is_vararg | byte | 1 when the parameter list ends in ..., else 0. Such a function gets the extra arguments in a local variable named arg, the one after the parameters (lparser.c); there is no vararg instruction. |
| maxstacksize | byte | Number of registers the function needs. |
| lineinfo | list of int | Source line of each instruction; empty or as long as the code. |
| locvars | list of (string, int, int) | Local variables: name, startpc (first instruction, counted from 0, where it is active) and endpc (first one where it is dead). |
| upvalues | list of string | Upvalue names; the count is 0 or nups. |
| constants | list of constant | See constants. |
| protos | list of function | Nested function prototypes, each in this same layout. |
| code | list of Instruction | The instructions, each sizeof(Instruction) bytes. |
The order differs from every later version: the debug information comes first and the code last.
Constants
| Tag byte | Type | Then |
|---|---|---|
0 | nil | nothing |
3 | number | a number |
4 | string | a string |
The tags are the values of LUA_TNIL, LUA_TNUMBER and
LUA_TSTRING in lua.h. There is no boolean constant:
true and false are loaded by LOADBOOL.
Instructions
An instruction is an unsigned number in the chunk’s byte order. The positions of the
fields follow from their widths (lopcodes.h): the opcode is lowest, then C,
B and A.
| Mode | Bits 31–24 | Bits 23–15 | Bits 14–6 | Bits 5–0 |
|---|---|---|---|---|
iABC | A (8) | B (9) | C (9) | opcode (6) |
iABx | A (8) | Bx (18), unsigned | opcode (6) | |
iAsBx | A (8) | sBx (18): stored as value + 131,071 | opcode (6) | |
This is not the 5.1 arrangement, which puts A just above the opcode.
When the instruction is 8 bytes, the code generator leaves the upper 32 bits zero.
GETARG_A does not mask the field: it shifts the instruction right by 24 and
converts to int, so on a common compiler bits 32–55 become the upper bits
of A (A is negative when bit 55 is set) and bits 56–63 are lost. B, C, Bx and the opcode
are masked.
R(x) is register x; Kst(x) is constant
x; RK(x) is register x when x is below 250
(MAXSTACK) and constant x − 250 otherwise. pc
counts instructions and has already moved past the current one when a jump is
added.
| Op | Name | Mode | Action |
|---|---|---|---|
| 0 | MOVE | A B | R(A) := R(B) |
| 1 | LOADK | A Bx | R(A) := Kst(Bx) |
| 2 | LOADBOOL | A B C | R(A) := (Bool)B; if C, skip the next instruction |
| 3 | LOADNIL | A B | R(A) to R(B) := nil |
| 4 | GETUPVAL | A B | R(A) := UpValue[B] |
| 5 | GETGLOBAL | A Bx | R(A) := Gbl[Kst(Bx)] |
| 6 | GETTABLE | A B C | R(A) := R(B)[RK(C)] |
| 7 | SETGLOBAL | A Bx | Gbl[Kst(Bx)] := R(A) |
| 8 | SETUPVAL | A B | UpValue[B] := R(A) |
| 9 | SETTABLE | A B C | R(A)[RK(B)] := RK(C) |
| 10 | NEWTABLE | A B C | R(A) := {}; B is the initial array size as a “floating point byte” (below), the hash part has 2C slots, none when C is 0 (the parser stores luaO_log2(n) + 1 for n named fields) |
| 11 | SELF | A B C | R(A+1) := R(B); R(A) := R(B)[RK(C)] |
| 12 | ADD | A B C | R(A) := RK(B) + RK(C) |
| 13 | SUB | A B C | R(A) := RK(B) − RK(C) |
| 14 | MUL | A B C | R(A) := RK(B) × RK(C) |
| 15 | DIV | A B C | R(A) := RK(B) / RK(C) |
| 16 | POW | A B C | R(A) := RK(B) ^ RK(C) |
| 17 | UNM | A B | R(A) := −R(B) |
| 18 | NOT | A B | R(A) := not R(B) |
| 19 | CONCAT | A B C | R(A) := R(B) .. … .. R(C) |
| 20 | JMP | sBx | pc += sBx |
| 21 | EQ | A B C | if (RK(B) == RK(C)) ~= A then skip the next instruction, else take the jump it holds |
| 22 | LT | A B C | the same with RK(B) < RK(C) |
| 23 | LE | A B C | the same with RK(B) <= RK(C) |
| 24 | TEST | A B C | if R(B) is false or nil and C is 1, or neither and C is 0, skip the next instruction; else R(A) := R(B) and take the jump |
| 25 | CALL | A B C | R(A), …, R(A+C−2) := R(A)(R(A+1), …, R(A+B−1)); B = 0 means arguments up to the top; C is the number of results plus 1, and 0 keeps them all and sets the top after the last |
| 26 | TAILCALL | A B C | return R(A)(R(A+1), …, R(A+B−1)) |
| 27 | RETURN | A B | return R(A), …, R(A+B−2); B = 0 means up to the top |
| 28 | FORLOOP | A sBx | if R(A) + R(A+2) has not passed the limit R(A+1) (in the direction of the step), R(A) := that sum and pc += sBx; otherwise R(A) is left unchanged |
| 29 | TFORLOOP | A C | R(A+2), …, R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+2) is nil, skip the next instruction, else take the jump it holds |
| 30 | TFORPREP | A sBx | if R(A) is a table, R(A+1) := R(A) and R(A) := the global next; then pc += sBx (kept “for compatibility”) |
| 31 | SETLIST | A Bx | with n = (Bx mod 32) + 1: R(A)[Bx − Bx mod 32 + i] := R(A+i) for 1 ≤ i ≤ n (LFIELDS_PER_FLUSH = 32) |
| 32 | SETLISTO | A Bx | the same, with n running to the top of the stack (the list ends in a call) |
| 33 | CLOSE | A | close all variables in the stack at or above R(A) |
| 34 | CLOSURE | A Bx | R(A) := closure(KPROTO[Bx]); the function’s nups following instructions are not executed but name the upvalues: MOVE B takes the local R(B), GETUPVAL B the enclosing function’s upvalue B |
Floating point byte (NEWTABLE B)
The byte is eeeeexxx and decodes to
(x & 7) << (x >> 3) (the macro fb2int in
lobject.h). luaO_int2fb (lobject.c) encodes with
m = 0; while (x >= 8) { x = (x + 1) >> 1; m++; } and returns
(m << 3) | x, so the decoded size is never smaller than the real one.
Example: 100 → 50 → 25 → 13 → 7 (m = 4), so the byte is 0x27, which
decodes to 7 << 4 = 112.
Operand modes (luaP_opmodes)
lopcodes.c holds one byte per opcode: the format and flags saying that B is
a register, that B or C is a register or constant (RK), that the instruction sets
register A, that Bx is a constant index, and that the instruction is a test.
| Ops | Names | Format | B | C | Sets A | Test |
|---|---|---|---|---|---|---|
| 0 | MOVE | iABC | register | yes | ||
| 1 | LOADK | iABx | constant | yes | ||
| 2 | LOADBOOL | iABC | yes | |||
| 3 | LOADNIL | iABC | register | yes | ||
| 4 | GETUPVAL | iABC | yes | |||
| 5 | GETGLOBAL | iABx | constant | yes | ||
| 6 | GETTABLE | iABC | register | RK | yes | |
| 7 | SETGLOBAL | iABx | constant | |||
| 8 | SETUPVAL | iABC | ||||
| 9 | SETTABLE | iABC | RK | RK | ||
| 10 | NEWTABLE | iABC | yes | |||
| 11 | SELF | iABC | register | RK | yes | |
| 12–16 | ADD, SUB, MUL, DIV, POW | iABC | RK | RK | yes | |
| 17–18 | UNM, NOT | iABC | register | yes | ||
| 19 | CONCAT | iABC | register | RK | yes | |
| 20 | JMP | iAsBx | jump offset | |||
| 21–23 | EQ, LT, LE | iABC | RK | RK | yes | |
| 24 | TEST | iABC | register | yes | yes | |
| 25–27 | CALL, TAILCALL, RETURN | iABC | ||||
| 28 | FORLOOP | iAsBx | jump offset | |||
| 29 | TFORLOOP | iABC | yes | |||
| 30 | TFORPREP | iAsBx | jump offset | |||
| 31–32 | SETLIST, SETLISTO | iABx | number | |||
| 33 | CLOSE | iABC | ||||
| 34 | CLOSURE | iABx | prototype | yes | ||
CONCAT’s C is flagged RK in the table, but the instruction uses it as a
register, and the verifier adds the rule that it is below 250.
What the loader rejects
- Signature: anything other than
1B 4C 75 61(“bad signature”). - Version: above
0x50(“too new”) or below it (“too old”). - Byte order: never refused by itself. When the endianness byte differs from the loading machine’s own value (0 or 1), the loader reverses the bytes of every int, size_t, number and instruction. A byte other than 0 or 1 therefore means “the opposite of this machine”.
- Sizes:
int,size_t,Instruction, SIZE_OP, SIZE_A, SIZE_B, SIZE_C andlua_Numbermust each equal the loading build’s (“virtual machine mismatch”). - Test number: converted to
long, it must equal the build’s own (“unknown number format”). This also catches a byte order that was reversed wrongly. - Truncation: every read must return all its bytes (“unexpected end of file”).
- Integers: a negative count,
lineDefined,startpcorendpc(“bad integer”). The line numbers inlineinfoare read as a block and not checked. - Upvalue names: a count that is neither 0 nor
nups(“bad nupvalues”). - Constants: a tag other than 0, 3 or 4 (“bad constant type”). A string constant of length 0 is not refused here, although it leaves the constant without a string.
- Code: after each function is loaded,
luaG_checkcodemust succeed (“bad code”), unless Lua was built withTRUST_BINARIES.
The loader reads the header and one function. It sets no limit on how deeply functions nest and does not look at bytes that follow the main function.
The code verifier (luaG_checkcode)
Per function, first (precheck):
maxstacksizeis at most 250;- the line information is empty or as long as the code;
- the last instruction is
RETURN(code of length 0 is not tested for: the check reads before the array).
That numparams plus is_vararg fits in
maxstacksize is only an assertion, which a normal build leaves out. Then for
every instruction:
- A is below
maxstacksize, whatever the opcode (a signed comparison); - a B flagged as register is below
maxstacksize; a B or C flagged RK is belowmaxstacksize, or at least 250 with x − 250 below the constant count; a Bx flagged as constant is below the constant count; - a test instruction (
EQ,LT,LE,TEST,TFORLOOP) has at least two instructions after it and the next one isJMP; LOADBOOLwith C not 0 has two instructions after it;GETUPVAL,SETUPVAL: B is belownups;GETGLOBAL,SETGLOBAL: the constant Bx is a string;SELF: A + 1 is belowmaxstacksize;CONCAT: C is below 250 and B is below C;TFORLOOP: A + C + 5 is belowmaxstacksize;TFORLOOP,FORLOOP: A + 2 is belowmaxstacksize;TFORLOOP,FORLOOP,JMP: pc + 1 + the operand (sBx; forTFORLOOPits B) is inside the code. The target ofTFORPREPis not checked;CALL,TAILCALL: if B is not 0, A + B − 1 is belowmaxstacksize; if C is 0, the next instruction isSETLISTO, orCALL,TAILCALLorRETURNwith B = 0; if C is above 1, A + C − 2 is belowmaxstacksize;RETURN: if B is above 1, A + B − 2 is belowmaxstacksize;SETLIST: A + (Bx mod 32) + 1 is belowmaxstacksize;CLOSURE: Bx is below the prototype count; thenupsinstructions after it exist and each isMOVEorGETUPVAL.
An opcode of 35 or more has no row in luaP_opmodes: the verifier of 5.0.3
reads past the table, so whether such a chunk is refused is not defined by the
source. The compiler never writes one.
Stripped chunks
luac -s removes the debug information before writing (the function
strip in luac.c): in every function lineinfo,
locvars and the upvalue names get a count of 0, and the source name becomes
=(none). The main function stores that string; nested functions, whose
source then equals the parent’s, store none. lineDefined,
nups, the code, the constants and the nesting are unchanged.
How a file is recognised
lua_load treats input as a precompiled chunk when its first byte is
1B, and as source otherwise (lapi.c).
luaL_loadfile (lauxlib.c) reopens a file in binary mode when
its first character is neither printable nor white space. It does not skip a first line
starting with # before a chunk.
Example
The header written by Lua 5.0.3 on a 64-bit little-endian machine whose
long is 8 bytes:
1B 4C 75 61 signature
50 version 5.0
01 little-endian
04 int: 4 bytes
08 size_t: 8 bytes
08 Instruction: 8 bytes
06 08 09 09 SIZE_OP, SIZE_A, SIZE_B, SIZE_C
08 lua_Number: 8 bytes
B6 09 93 68 E7 F5 7D 41 test number 31415926.535897933
The instruction 01 00 00 00 00 00 00 00 is opcode 1 (LOADK),
A = 0, Bx = 0, so R(0) := Kst(0).
Lua 5.0 alpha and beta
The two pre-releases of 5.0 write version byte 0x50 too, but a layout of
their own. Their test number is 3.14159265358979323846E8, ten times that
of the releases, and the instruction field widths tell the two apart. A reader should
ask for both the number and the widths. A lua_Number too small to hold
either test number, such as a 2-byte float that reads both as infinity, says nothing,
and such a chunk is read as a release chunk.
| 5.0 alpha | 5.0 beta | 5.0 to 5.0.3 | |
|---|---|---|---|
| test number | 314,159,265.358979… | 314,159,265.358979… | 31,415,926.5358979… |
| SIZE_OP, SIZE_A, SIZE_B, SIZE_C | 6, 8, 8, 10 | 6, 8, 9, 9 | 6, 8, 9, 9 |
| Constant tags: nil, number, string | 0, 1, 2 | 0, 3, 4 | 0, 3, 4 |
| Debug information of a function | locvars, then lineinfo; no upvalue names | lineinfo, locvars, upvalue names | |
| Opcodes | 37 | 35, as in the releases | 35 |
The header fields are in the order of the release header. A
function is: source, lineDefined, the bytes nups, numparams, is_vararg and
maxstacksize, locvars, lineinfo, constants, protos and code. A stripped chunk
(luac -s) has no local variables and no line numbers, and the source name
=(none).
The beta’s opcodes are those of the release table. In
the alpha, A is in bits 24–31, B in bits 16–23, C in bits 6–15 and Bx in bits 6–23.
Opcodes 0–23 have the release numbers and names; then follow 24 GT,
25 GE, 26 TEST, 27 CALL, 28 TAILCALL,
29 RETURN, 30 FORLOOP, 31 TFORLOOP,
32 TFORPREP, 33 SETLIST, 34 SETLISTO,
35 CLOSE and 36 CLOSURE. Operands that differ in the
alpha:
| Name | Action in 5.0 alpha |
|---|---|
SETTABLE | R(B)[RK(C)] := R(A) |
ADD to POW | R(A) := R(B) op RK(C); B is always a register |
EQ, LT, LE, GT, GE | if (R(A) op RK(C)) differs from B, skip the next instruction, else take the jump it holds |
TEST | if RK(C) is false or nil and B is 1, or neither and B is 0, skip the next instruction; else R(A) := RK(C) and take the jump |
In both pre-releases B of NEWTABLE gives an array part of
2B − 1 slots, none when B is 0, not a floating point byte. Both loaders
ask for their own sizes and field widths, compare the test number as a
long and run luaG_checkcode on each function. The loader of
the alpha also refuses bytes after the main function.
Sources
The pre-releases: the packages of Lua 5.0 alpha and 5.0 beta from
lua.org/work/old (lundump.c,
lundump.h, ldump.c or luac/dump.c,
lua.h, lopcodes.h, lopcodes.c,
lvm.c, lparser.c and luac/luac.c).
The Lua 5.0.3 source from lua.org
(also readable at lua.org/source/5.0):
lundump.c, lundump.h, ldump.c,
lopcodes.h, lopcodes.c, llimits.h,
lobject.h, lobject.c, lvm.c,
lparser.c, ldebug.c, ldo.c, lapi.c,
lauxlib.c, lua.h and luac.c.