Lua’s authors do not document the precompiled format; it is defined by the source of
each release. This page follows Lua 5.1’s ldump.c,
lundump.c, lobject.h and lopcodes.h as published on
lua.org (see sources). A chunk
is written in the byte order and with the number sizes of the machine that wrote it,
which the header records; a standard Lua refuses a chunk whose header differs from
its own.
Header (12 bytes)
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: escape and Lua. |
4 | 1 | version | 0x51: major × 16 + minor. |
5 | 1 | format | 0 for the official format. |
6 | 1 | endianness | 1 little-endian, 0 big-endian: the first byte of an int holding 1. |
7 | 1 | sizeof(int) | Bytes in an int on the writing machine. |
8 | 1 | sizeof(size_t) | Bytes in a size_t. |
9 | 1 | sizeof(Instruction) | Bytes in an instruction. |
10 | 1 | sizeof(lua_Number) | Bytes in a number. |
11 | 1 | integral flag | 0 if lua_Number is floating point, 1 if it is an integer type. |
The main function follows directly.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| int | sizeof(int) bytes, in the header’s byte order. |
| 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. |
| list of T | An int count, then that many T. |
Function prototype
| Field | Type | Meaning |
|---|---|---|
| source | string | Source name. When it is empty (no string), the loader uses the enclosing function’s source. |
| linedefined | int | Line where the function starts. |
| lastlinedefined | int | Line where it ends. |
| nups | byte | Number of upvalues. |
| numparams | byte | Number of fixed parameters. |
| is_vararg | byte | Flags (“masks for new-style vararg” in lobject.h): 1 VARARG_HASARG, 2 VARARG_ISVARARG, 4 VARARG_NEEDSARG. |
| maxstacksize | byte | Number of registers the function needs. |
| code | list of Instruction | The instructions, each 4 bytes. |
| constants | list of constant | See constants. |
| protos | list of function | Nested function prototypes, each in this same layout. |
| lineinfo | list of int | Source line of each instruction. |
| locvars | list of (string, int, int) | Local variables: name, first and last instruction where it is active. |
| upvalues | list of string | Upvalue names. |
Constants
| Tag byte | Type | Then |
|---|---|---|
0 | nil | nothing |
1 | boolean | one byte, 0 false, otherwise true |
3 | number | a number |
4 | string | a string |
The tags are the LUA_T type constants; their values (0, 1, 3, 4) are
those of LUA_TNIL, LUA_TBOOLEAN, LUA_TNUMBER
and LUA_TSTRING in 5.1’s own
lua.h on lua.org,
read 2026-11-12. Any other tag is an
error (“bad constant”).
Instructions
Each instruction is a 32-bit unsigned number in the chunk’s byte order. The opcode is in the low six bits.
| Mode | Bits 31–23 | Bits 22–14 | Bits 13–6 | Bits 5–0 |
|---|---|---|---|---|
iABC | B (9) | C (9) | A (8) | opcode (6) |
iABx | Bx (18), unsigned | A (8) | opcode (6) | |
iAsBx | sBx (18): stored as value + 131,071 | A (8) | opcode (6) | |
R(x) is register x; Kst(x) is constant
x; RK(x) is a constant when bit 8 of the 9-bit field is set
(BITRK = 256, so constant x − 256) and a register otherwise.
SETLIST stores 50 items per batch (LFIELDS_PER_FLUSH). Every
“skip” assumes the next instruction is a jump.
| 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 and C the initial hash size, each a “floating point byte” (below) |
| 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 | MOD | A B C | R(A) := RK(B) % RK(C) |
| 17 | POW | A B C | R(A) := RK(B) ^ RK(C) |
| 18 | UNM | A B | R(A) := −R(B) |
| 19 | NOT | A B | R(A) := not R(B) |
| 20 | LEN | A B | R(A) := length of R(B) |
| 21 | CONCAT | A B C | R(A) := R(B) .. … .. R(C) |
| 22 | JMP | sBx | pc += sBx |
| 23 | EQ | A B C | if (RK(B) == RK(C)) ~= A then skip the next instruction |
| 24 | LT | A B C | if (RK(B) < RK(C)) ~= A then skip |
| 25 | LE | A B C | if (RK(B) <= RK(C)) ~= A then skip |
| 26 | TEST | A C | if not (R(A) <=> C) then skip |
| 27 | TESTSET | A B C | if R(B) <=> C then R(A) := R(B) else skip |
| 28 | 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 sets the top after the last result |
| 29 | TAILCALL | A B C | return R(A)(R(A+1), …, R(A+B−1)) |
| 30 | RETURN | A B | return R(A), …, R(A+B−2); B = 0 means up to the top |
| 31 | FORLOOP | A sBx | R(A) += R(A+2); if R(A) is within R(A+1), pc += sBx and R(A+3) := R(A) |
| 32 | FORPREP | A sBx | R(A) −= R(A+2); pc += sBx |
| 33 | TFORLOOP | A C | R(A+3), …, R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+3) ~= nil then R(A+2) := R(A+3) else skip |
| 34 | SETLIST | A B C | R(A)[(C−1) × 50 + i] := R(A+i) for 1 ≤ i ≤ B; B = 0 means up to the top; C = 0 means the real C is the next instruction word |
| 35 | CLOSE | A | close all variables in the stack at or above R(A) |
| 36 | CLOSURE | A Bx | R(A) := closure(KPROTO[Bx], upvalues) |
| 37 | VARARG | A B | R(A), R(A+1), … := the vararg values; B = 0 means use the actual number of varargs and set top (like CALL with C = 0) |
Floating point byte (NEWTABLE B and C)
The parser stores the constructor’s array count and hash count with
luaO_int2fb, and the VM calls luaH_new(L, luaO_fb2int(B),
luaO_fb2int(C)) (lobject.c, lvm.c; the parameters of
luaH_new in ltable.c are narray and
nhash). A “floating point byte” is eeeeexxx: when
eeeee is 0 the value is xxx; otherwise it is
(1xxx)2 × 2eeeee−1, that is
((x & 7) + 8) << (e − 1). luaO_int2fb loops
while (x >= 16) { x = (x + 1) >> 1; e++; }, then returns
x if it is below 8, else ((e + 1) << 3) | (x − 8). The
encoded size is therefore never smaller than the real one. Example: 100 → 50 → 25 → 13
(e = 3), so the byte is (4 << 3) | 5 = 0x25, which decodes to
13 << 3 = 104.
Operand modes (luaP_opmodes)
lopcodes.c holds one byte per opcode, built by
opmode(t, a, b, c, m) as
(t << 7) | (a << 6) | (b << 4) | (c << 2) | m: bits 0–1
the format, bits 2–3 the C argument mode, bits 4–5 the B argument mode, bit 6 “instruction
sets register A” and bit 7 “operator is a test”. The argument modes
(enum OpArgMask) are N, argument not used; U,
used (a number, count or flag); R, a register or a jump offset;
K, a constant or register/constant (RK).
| Ops | Names | Format | B | C | Sets A | Test |
|---|---|---|---|---|---|---|
| 0 | MOVE | iABC | R | N | yes | |
| 1 | LOADK | iABx | K | N | yes | |
| 2 | LOADBOOL | iABC | U | U | yes | |
| 3 | LOADNIL | iABC | R | N | yes | |
| 4 | GETUPVAL | iABC | U | N | yes | |
| 5 | GETGLOBAL | iABx | K | N | yes | |
| 6 | GETTABLE | iABC | R | K | yes | |
| 7 | SETGLOBAL | iABx | K | N | ||
| 8 | SETUPVAL | iABC | U | N | ||
| 9 | SETTABLE | iABC | K | K | ||
| 10 | NEWTABLE | iABC | U | U | yes | |
| 11 | SELF | iABC | R | K | yes | |
| 12–17 | ADD, SUB, MUL, DIV, MOD, POW | iABC | K | K | yes | |
| 18–20 | UNM, NOT, LEN | iABC | R | N | yes | |
| 21 | CONCAT | iABC | R | R | yes | |
| 22 | JMP | iAsBx | R | N | ||
| 23–25 | EQ, LT, LE | iABC | K | K | yes | |
| 26–27 | TEST, TESTSET | iABC | R | U | yes | yes |
| 28–29 | CALL, TAILCALL | iABC | U | U | yes | |
| 30 | RETURN | iABC | U | N | ||
| 31–32 | FORLOOP, FORPREP | iAsBx | R | N | yes | |
| 33 | TFORLOOP | iABC | N | U | yes | |
| 34 | SETLIST | iABC | U | U | ||
| 35 | CLOSE | iABC | N | N | ||
| 36 | CLOSURE | iABx | U | N | yes | |
| 37 | VARARG | iABC | U | N | yes |
Source: luaP_opmodes in lopcodes.c and the mask comment in
lopcodes.h, Lua 5.1 on lua.org. Unlike the later versions, 5.1’s
TEST is listed as setting register A (opmode(1, 1, R, U)), and
TFORLOOP is marked as a test.
What the loader rejects
Every rule below makes luaU_undump fail with a syntax error; the exact message
wording is not part of the format. Source: Lua 5.1.5 lundump.c,
ldebug.c, lobject.h, llimits.h and luaconf.h
on lua.org, read as raw HTML text.
- Header: the whole 12-byte header is compared at once with the header this build would write (
luaU_header). Any difference is refused: signature, version byte (0x51), format (0), byte order, the sizes ofint,size_t,Instructionandlua_Number, and the integral-number flag. A chunk is therefore readable only by a build with the same sizes and byte order. - Truncation: every read must return all its bytes; running out of input anywhere is refused.
- Integers: every
intread throughLoadInt(the code count, constant count, prototype count, the three debug counts,linedefined,lastlinedefined,startpc,endpc) must be 0 or more; a negative value is refused. The line-number entries insidelineinfoare read as a block and not checked. - Constants: a tag other than nil (0), boolean (1), number (3) or string (4) is refused. Strings: a size of 0 means no string (
NULL) and the loader does not forbid it for a string constant, so a string constant with size 0 is accepted and holds a null string pointer. There is no rule that a constant string must be non-empty or present. - Nesting:
LoadFunctionadds 1 to the state’s C-call counter (nCcalls) and refuses the chunk when it exceedsLUAI_MAXCCALLS, 200 inluaconf.h. The counter also holds the C calls already active, so a chunk may nest fewer than 200 functions deep; the counter is lowered again when each function is done. - Code verifier: after each function is loaded,
luaG_checkcode(ldebug.c, the “symbolic execution” checker) must succeed or the chunk is refused. It applies the checks below. - Chunk name: the name given to the loader never causes a refusal. It is used only in messages: a leading
@or=is dropped, a name that starts with the signature byte (ESC) shows as “binary string”, any other name is shown as it is.
The code verifier (luaG_checkcode)
Per function, first (precheck):
maxstacksizeis at mostMAXSTACK(250,llimits.h);numparamsplus theVARARG_HASARGbit (1) ofis_varargis at mostmaxstacksize;- if
VARARG_NEEDSARG(4) is set thenVARARG_HASARG(1) is set too (VARARG_ISVARARGis 2); - the number of upvalue names is at most
nups; - the line-info size is 0 or equals the code size;
- the code size is above 0 and the last instruction is
RETURN.
Then for every instruction:
- the opcode number is below the number of opcodes (38), and A is below
maxstacksizewhatever the opcode; - iABC: B and C are checked by their operand modes from
luaP_opmodes: mode N must be 0; mode U is free; mode R must be belowmaxstacksize; mode K, when the constant bit (256) is set, must have its index below the constant count, otherwise be belowmaxstacksize. iABx with a constant operand (B mode K): Bx is below the constant count; - jumps (iAsBx with B mode R:
JMP,FORLOOP,FORPREP): the targetpc + 1 + sBxis inside the code, and does not land on the count word that follows aSETLISTwith C = 0 (the checker counts back through consecutive suchSETLISTs and the run must have an even length); - every test instruction (
EQ,LT,LE,TEST,TESTSET,TFORLOOPper the T bit): there are at least two instructions after it and the next one isJMP; LOADBOOLwith C = 1: there are two instructions after it, and the next is not aSETLISTwith C = 0;GETUPVAL,SETUPVAL: B is belownups;GETGLOBAL,SETGLOBAL: the constant Bx is a string;SELF: A + 1 is belowmaxstacksize;CONCAT: B is below C;TFORLOOP: C is at least 1 and A + 2 + C is belowmaxstacksize;FORLOOP,FORPREP: A + 3 is belowmaxstacksize;CALL,TAILCALL: if B is not 0, A + B − 1 is belowmaxstacksize; if C is 0 (multiple results), the next instruction must beCALL,TAILCALL,RETURNorSETLISTwith B = 0; if C is above 1, A + C − 2 is belowmaxstacksize;RETURN: if B is above 1, A + B − 2 is belowmaxstacksize;SETLIST: if B is above 0, A + B is belowmaxstacksize; if C is 0, the count word after it must exist and not be the last instruction;CLOSURE: Bx is below the prototype count; thenupsinstructions after it exist and each isGETUPVALorMOVE;VARARG: the function must haveVARARG_ISVARARGset andVARARG_NEEDSARGclear; if B is 0 the next instruction must be one of the open-result consumers listed underCALL; otherwise A + B − 2 is belowmaxstacksize.
That is everything the checker does. It does not check that the constants used by an instruction have the right type (except for the global names), that registers hold usable values, or that a prototype’s upvalue descriptors are consistent.
Stripped chunks
luac -s passes strip to luaU_dump. In 5.1’s
ldump.c, with strip set, DumpFunction writes every function’s
source as no string (a source equal to the parent’s is written as no string
even without strip, and the loader then takes the parent’s), and DumpDebug
writes lineinfo (a size of 0 and no entries), locvars and the
upvalue-name list each with a count of 0. Header, code, constants and nested
prototypes are unchanged.
On loading, a function with no source takes its parent’s. The main function has no
parent, so luaU_undump starts it with the literal =? as the
parent source (LoadFunction( &S, luaS_newliteral( L, "=?" ) )): a stripped
5.1 chunk’s main function, and every nested function that also had no source, shows
=? as its source name. (Lua 5.1.5 lundump.c, lua.org raw HTML.)
Example
The header written by Lua 5.1 on a 64-bit little-endian machine:
1B 4C 75 61 signature
51 version 5.1
00 official format
01 little-endian
04 int: 4 bytes
08 size_t: 8 bytes
04 Instruction: 4 bytes
08 lua_Number: 8 bytes
00 floating-point numbers
The instruction 01 00 00 00 read little-endian is 0x00000001:
opcode 1 (LOADK), A = 0, Bx = 0, so R(0) := Kst(0).
Sources
Lua 5.1 source on lua.org:
lundump.c (luaU_header, LoadFunction,
LoadConstants, LoadDebug), lobject.h (vararg
masks) and lopcodes.h (field sizes and positions, BITRK,
MAXARG_sBx, LFIELDS_PER_FLUSH, every opcode and its comment,
and the notes on B or C = 0). The header values in the example are those of a
64-bit little-endian build and are illustrative. The operand-mode, floating-point-byte
and stripping sections were read from the same viewer: lopcodes.c
(luaP_opmodes), lobject.c (luaO_int2fb,
luaO_fb2int), lvm.c (OP_NEWTABLE),
lparser.c, ltable.c (luaH_new) and
ldump.c (strip).