This page follows Lua 5.4’s ldump.c, lundump.c,
lundump.h and lopcodes.h (5.4.6). The format is not
documented by the Lua authors. Compared with 5.3, sizes and counts became
variable-length numbers, the header no longer records int and
size_t sizes, booleans became two constant tags, line information became
compact, and the instruction set was redesigned with a 7-bit opcode.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61. |
4 | 1 | version | 0x54. |
5 | 1 | format | 0, the official format. |
6 | 6 | LUAC_DATA | 19 93 0D 0A 1A 0A. |
12 | 1 | sizeof(Instruction) | Bytes in an instruction. |
13 | 1 | sizeof(lua_Integer) | Bytes in an integer. |
14 | 1 | sizeof(lua_Number) | Bytes in a float. |
15 | 8* | LUAC_INT | 0x5678 as a lua_Integer: shows the byte order. |
23* | 8* | LUAC_NUM | 370.5 as a lua_Number: shows the float format. |
31* | 1 | upvalues | Number of upvalues of the main closure. |
* With 8-byte integers and floats.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| varint | Used for every size, count and int. Seven bits per byte, most significant group first; the last byte has its top bit set. 300 is 02 AC; 5 is 85. |
| integer | sizeof(lua_Integer) bytes, native byte order. |
| number | sizeof(lua_Number) bytes, native representation. |
| string | A varint holding the length plus one (0 means no string), then the bytes with no terminating zero. |
| list of T | A varint count, then that many T. |
Function prototype
| Field | Type | Meaning |
|---|---|---|
| source | string | Source name; when absent (no string), the parent function’s source is used. |
| linedefined | varint | First line. |
| lastlinedefined | varint | Last line. |
| numparams | byte | Fixed parameters. |
| is_vararg | byte | Whether the function takes .... |
| maxstacksize | byte | Registers needed. |
| code | list of Instruction | 4 bytes each, native byte order. |
| constants | list of constant | Below. |
| upvalues | list of (byte, byte, byte) | instack, idx and kind (below), one byte each. |
| protos | list of function | Nested prototypes. |
| lineinfo | list of signed byte | For each instruction, the change in line number from the previous instruction. |
| abslineinfo | list of (varint, varint) | Absolute positions: an instruction index and its line. At most MAXIWTHABS (128) instructions in a row go without one. |
| locvars | list of (string, varint, varint) | Name, start pc, end pc. |
| upvalue names | list of string | One per upvalue; with a count of 0 none are read. |
A lineinfo entry of −128 (ABSLINEINFO, byte 80)
marks an instruction whose line must be taken from abslineinfo (from
Lua’s ldebug.h).
Constants
| Tag byte | Value | Then |
|---|---|---|
0x00 | nil (LUA_VNIL) | nothing |
0x01 | false (LUA_VFALSE) | nothing |
0x11 | true (LUA_VTRUE) | nothing |
0x03 | integer (LUA_VNUMINT) | an integer |
0x13 | float (LUA_VNUMFLT) | a number |
0x04 | short string (LUA_VSHRSTR) | a string |
0x14 | long string (LUA_VLNGSTR) | a string |
Note that 5.4 swapped the integer and float variants of 5.3: 0x03 is a
float in 5.3 and an integer in 5.4.
Upvalue kinds
| Kind | Name | Variable is |
|---|---|---|
0 | VDKREG | a regular variable |
1 | RDKCONST | a constant |
2 | RDKTOCLOSE | to be closed |
3 | RDKCTC | a compile-time constant |
Instructions
| Mode | Bits 31–24 | Bits 23–16 | Bit 15 | Bits 14–7 | Bits 6–0 |
|---|---|---|---|---|---|
iABC | C (8) | B (8) | k | A (8) | opcode (7) |
iABx | Bx (17), unsigned | A (8) | opcode (7) | ||
iAsBx | sBx (17), stored + 65,535 | A (8) | opcode (7) | ||
iAx | Ax (25) | opcode (7) | |||
isJ | sJ (25), stored + 16,777,215 | opcode (7) | |||
R[x] is a register, K[x] a constant and
RK(x) is K[x] when the k bit is set, otherwise
R[x]. sB and sC are signed 8-bit values stored
+ 127. In comparisons k gives the expected result: the next instruction
(normally a JMP) is skipped when the result differs.
| Op | Name | Mode | Action |
|---|---|---|---|
| 0 | MOVE | A B | R[A] := R[B] |
| 1 | LOADI | A sBx | R[A] := sBx |
| 2 | LOADF | A sBx | R[A] := (float)sBx |
| 3 | LOADK | A Bx | R[A] := K[Bx] |
| 4 | LOADKX | A | R[A] := K[extra arg] |
| 5 | LOADFALSE | A | R[A] := false |
| 6 | LFALSESKIP | A | R[A] := false; skip next |
| 7 | LOADTRUE | A | R[A] := true |
| 8 | LOADNIL | A B | R[A], …, R[A+B] := nil |
| 9 | GETUPVAL | A B | R[A] := UpValue[B] |
| 10 | SETUPVAL | A B | UpValue[B] := R[A] |
| 11 | GETTABUP | A B C | R[A] := UpValue[B][K[C]:string] |
| 12 | GETTABLE | A B C | R[A] := R[B][R[C]] |
| 13 | GETI | A B C | R[A] := R[B][C] |
| 14 | GETFIELD | A B C | R[A] := R[B][K[C]:string] |
| 15 | SETTABUP | A B C | UpValue[A][K[B]:string] := RK(C) |
| 16 | SETTABLE | A B C | R[A][R[B]] := RK(C) |
| 17 | SETI | A B C | R[A][B] := RK(C) |
| 18 | SETFIELD | A B C | R[A][K[B]:string] := RK(C) |
| 19 | NEWTABLE | A B C k | R[A] := {}; B is the log₂ of the hash size plus 1, or 0 for hash size 0 (the size is 2B−1); the array size is C, or, when k is set, EXTRAARG _ C (below). The next instruction is always an EXTRAARG |
| 20 | SELF | A B C | R[A+1] := R[B]; R[A] := R[B][RK(C):string] |
| 21 | ADDI | A B sC | R[A] := R[B] + sC |
| 22 | ADDK | A B C | R[A] := R[B] + K[C]:number |
| 23 | SUBK | A B C | R[A] := R[B] − K[C]:number |
| 24 | MULK | A B C | R[A] := R[B] × K[C]:number |
| 25 | MODK | A B C | R[A] := R[B] % K[C]:number |
| 26 | POWK | A B C | R[A] := R[B] ^ K[C]:number |
| 27 | DIVK | A B C | R[A] := R[B] / K[C]:number |
| 28 | IDIVK | A B C | R[A] := R[B] // K[C]:number |
| 29 | BANDK | A B C | R[A] := R[B] & K[C]:integer |
| 30 | BORK | A B C | R[A] := R[B] | K[C]:integer |
| 31 | BXORK | A B C | R[A] := R[B] ~ K[C]:integer |
| 32 | SHRI | A B sC | R[A] := R[B] >> sC |
| 33 | SHLI | A B sC | R[A] := sC << R[B] |
| 34 | ADD | A B C | R[A] := R[B] + R[C] |
| 35 | SUB | A B C | R[A] := R[B] − R[C] |
| 36 | MUL | A B C | R[A] := R[B] × R[C] |
| 37 | MOD | A B C | R[A] := R[B] % R[C] |
| 38 | POW | A B C | R[A] := R[B] ^ R[C] |
| 39 | DIV | A B C | R[A] := R[B] / R[C] |
| 40 | IDIV | A B C | R[A] := R[B] // R[C] |
| 41 | BAND | A B C | R[A] := R[B] & R[C] |
| 42 | BOR | A B C | R[A] := R[B] | R[C] |
| 43 | BXOR | A B C | R[A] := R[B] ~ R[C] |
| 44 | SHL | A B C | R[A] := R[B] << R[C] |
| 45 | SHR | A B C | R[A] := R[B] >> R[C] |
| 46 | MMBIN | A B C | Call the metamethod C over R[A] and R[B]. MMBIN and its variants follow each arithmetic and bitwise opcode; if the operation succeeds, that instruction skips the MMBIN, otherwise the MMBIN calls the metamethod |
| 47 | MMBINI | A sB C k | Call metamethod C over R[A] and sB |
| 48 | MMBINK | A B C k | Call metamethod C over R[A] and K[B] |
| 49 | UNM | A B | R[A] := −R[B] |
| 50 | BNOT | A B | R[A] := ~R[B] |
| 51 | NOT | A B | R[A] := not R[B] |
| 52 | LEN | A B | R[A] := #R[B] |
| 53 | CONCAT | A B | R[A] := R[A] .. … .. R[A+B−1] |
| 54 | CLOSE | A | Close upvalues and to-be-closed variables at or above R[A] |
| 55 | TBC | A | Mark R[A] as to-be-closed |
| 56 | JMP | sJ | pc += sJ |
| 57 | EQ | A B k | if ((R[A] == R[B]) ~= k) then skip next |
| 58 | LT | A B k | if ((R[A] < R[B]) ~= k) then skip next |
| 59 | LE | A B k | if ((R[A] <= R[B]) ~= k) then skip next |
| 60 | EQK | A B k | if ((R[A] == K[B]) ~= k) then skip next |
| 61 | EQI | A sB k | if ((R[A] == sB) ~= k) then skip next |
| 62 | LTI | A sB k | if ((R[A] < sB) ~= k) then skip next |
| 63 | LEI | A sB k | if ((R[A] <= sB) ~= k) then skip next |
| 64 | GTI | A sB k | if ((R[A] > sB) ~= k) then skip next |
| 65 | GEI | A sB k | if ((R[A] >= sB) ~= k) then skip next |
| 66 | TEST | A k | if (not R[A] == k) then skip next |
| 67 | TESTSET | A B k | if (not R[B] == k) then skip next else R[A] := R[B] |
| 68 | CALL | A B C | R[A], …, R[A+C−2] := R[A](R[A+1], …, R[A+B−1]); B = 0 means B = top − A (arguments up to the top); C = 0 means top is set to last_result + 1, for the next open instruction (CALL, RETURN*, SETLIST) to use |
| 69 | TAILCALL | A B C k | return R[A](R[A+1], …, R[A+B−1]); k and C as for RETURN |
| 70 | RETURN | A B C k | return R[A], …, R[A+B−2]; B = 0 means up to the top; k: the function builds upvalues that may need closing; C > 0: the function is vararg, so its func must be corrected before returning, and C − 1 is its number of fixed parameters |
| 71 | RETURN0 | return with no values | |
| 72 | RETURN1 | A | return R[A] |
| 73 | FORLOOP | A Bx | Update the numeric loop counters; if the loop continues, pc −= Bx |
| 74 | FORPREP | A Bx | Check and prepare the counters; if the loop does not run, pc += Bx + 1 |
| 75 | TFORPREP | A Bx | Create an upvalue for R[A+3]; pc += Bx |
| 76 | TFORCALL | A C | R[A+4], …, R[A+3+C] := R[A](R[A+1], R[A+2]) |
| 77 | TFORLOOP | A Bx | if R[A+2] ~= nil then { R[A] := R[A+2]; pc −= Bx } |
| 78 | SETLIST | A B C k | R[A][C+i] := R[A+i], 1 ≤ i ≤ B; B = 0 means the real B is the top; when k is set, the real C is EXTRAARG _ C (the bits of the next instruction’s Ax concatenated above the bits of C, as for NEWTABLE) |
| 79 | CLOSURE | A Bx | R[A] := closure(KPROTO[Bx]) |
| 80 | VARARG | A C | R[A], …, R[A+C−2] := vararg; C = 0 means use the actual number of varargs and set top (like CALL with C = 0) |
| 81 | VARARGPREP | A | Adjust the vararg parameters on entry |
| 82 | EXTRAARG | Ax | A larger argument for the previous instruction |
Notes from lopcodes.h, in addition to those in the table: in
MMBINI and MMBINK, k means the arguments were
flipped (the constant is the first operand). In the comparisons with an immediate
operand (EQI, LTI, LEI, GTI,
GEI) the C field signals whether the original operand was a
float (it must be corrected in case of metamethods). LOADKX and
NEWTABLE are always followed by an EXTRAARG. TESTSET
is used in short-circuit expressions that need both to jump and to produce a value,
such as a = b or c. LFALSESKIP converts a condition to a
boolean: it produces false and skips the next instruction, which produces true. Every
skip assumes the next instruction is a jump.
NEWTABLE and SETLIST sizes
The compiler (luaK_settablesize in lcode.c) writes
B = ceil(log2(hash size)) + 1, or 0 when the hash size is 0. It splits the
array size: C = size mod 256 (MAXARG_C + 1) and the
EXTRAARG that always follows holds size ÷ 256; k is set when
that quotient is not 0. The VM (lvm.c, OP_NEWTABLE) reads
B as 2B−1 when B > 0 and the array size as
C + Ax × 256 when k is set, then skips the extra instruction.
“EXTRAARG _ C” in lopcodes.h means the Ax bits concatenated above the 8 bits
of C. SETLIST reads its C the same way.
Operand modes (luaP_opmodes)
lopcodes.c holds one byte per opcode, luaP_opmodes, built by
opmode(mm, ot, it, t, a, m) as
(mm << 7) | (ot << 6) | (it << 5) | (t << 4) | (a << 3) | m.
Bits 0–2 are the instruction format; bit 3 (A) means the instruction sets register A;
bit 4 (T) means it is a test, so the next instruction must be a jump; bit 5 (IT) means it
uses L->top set by the previous instruction, when B = 0; bit 6 (OT) means
it sets L->top for the next instruction, when C = 0 (TAILCALL
counts as OT whatever C is); bit 7 (MM) means it is a metamethod-call instruction. Unlike
5.1 to 5.3 there are no per-argument B and C modes. “Yes” is a set bit; every omitted
flag is 0.
| Ops | Names | Format | A | T | IT | OT | MM |
|---|---|---|---|---|---|---|---|
| 0 | MOVE | iABC | yes | ||||
| 1–2 | LOADI, LOADF | iAsBx | yes | ||||
| 3–4 | LOADK, LOADKX | iABx | yes | ||||
| 5–9 | LOADFALSE, LFALSESKIP, LOADTRUE, LOADNIL, GETUPVAL | iABC | yes | ||||
| 10 | SETUPVAL | iABC | |||||
| 11–14 | GETTABUP, GETTABLE, GETI, GETFIELD | iABC | yes | ||||
| 15–18 | SETTABUP, SETTABLE, SETI, SETFIELD | iABC | |||||
| 19–45 | NEWTABLE, SELF, ADDI, ADDK to IDIVK, BANDK, BORK, BXORK, SHRI, SHLI, ADD to SHR | iABC | yes | ||||
| 46–48 | MMBIN, MMBINI, MMBINK | iABC | yes | ||||
| 49–53 | UNM, BNOT, NOT, LEN, CONCAT | iABC | yes | ||||
| 54–55 | CLOSE, TBC | iABC | |||||
| 56 | JMP | isJ | |||||
| 57–66 | EQ, LT, LE, EQK, EQI, LTI, LEI, GTI, GEI, TEST | iABC | yes | ||||
| 67 | TESTSET | iABC | yes | yes | |||
| 68–69 | CALL, TAILCALL | iABC | yes | yes | yes | ||
| 70 | RETURN | iABC | yes | ||||
| 71–72 | RETURN0, RETURN1 | iABC | |||||
| 73–74 | FORLOOP, FORPREP | iABx | yes | ||||
| 75 | TFORPREP | iABx | |||||
| 76 | TFORCALL | iABC | |||||
| 77 | TFORLOOP | iABx | yes | ||||
| 78 | SETLIST | iABC | yes | ||||
| 79 | CLOSURE | iABx | yes | ||||
| 80 | VARARG | iABC | yes | yes | |||
| 81 | VARARGPREP | iABC | yes | yes | |||
| 82 | EXTRAARG | iAx |
Source: luaP_opmodes in lopcodes.c and the macros in
lopcodes.h, Lua 5.4.7.
What the loader rejects
Every rule below makes luaU_undump fail with a syntax error; message wording is
not part of the format. Source: Lua 5.4.7 lundump.c, lundump.h,
ldo.c, lmem.h and llimits.h, raw files of the
v5.4.7 tag.
- Mode:
f_parser(ldo.c) refuses a binary chunk when the mode string given toloadorlua_loaddoes not containb. - Header, checked in this order, each field refused if it differs: the signature after its first byte (
Lua); the version byte (0x54); the format byte (0);LUAC_DATA(19 93 0D 0A 1A 0A); the size bytes ofInstruction,lua_Integerandlua_Number(5.4 has nointorsize_tsize byte);LUAC_INT(0x5678) in the host’s byte order;LUAC_NUM(370.5). The byte after the header is the main function’s upvalue count. - Truncation: running out of input in any read, including inside a varint, is refused.
- Integer overflow: sizes and counts are varints (7 bits per byte, the last byte has its high bit set).
loadUnsignedcomputeslimit >> 7once and, after reading each byte and before adding its 7 bits, refuses the number if the value so far is>= limit >> 7(“integer overflow”). The limit isINT_MAXfor everyint(counts, line numbers, pcs) and the largestsize_tfor string sizes, so the largestintaccepted is 2,147,483,519, not 2,147,483,647. No count or line number can be negative. - Constants: an unknown tag is not refused in a normal build (
lua_assert( 0 )does nothing unless built with assertions): the constant stays nil. - Constant strings: a string constant (tag 0x04 or 0x14) is loaded by
loadString, which refuses a size of 0 (no string). Only constants have this rule: the source name, local variable names and upvalue names may be absent (loadStringN). - Upvalue names: in the debug section the count of upvalue names is read, but if it is not 0 the loader replaces it by the function’s real upvalue count and reads that many names. A valid chunk therefore has a count of 0 (no names) or exactly the upvalue count. The loader does not compare the two and does not reject another non-zero count: it reads the upvalue count’s worth of names regardless, so the rest of such a chunk is read out of step.
- Nesting: no depth limit in the loader (
LUAI_MAXCCALLSis 200 inllimits.h, butloadFunctiondoes not use it). - Code: not verified;
luai_verifycodeis empty. The check that the main closure’s upvalue count equals the function’ssizeupvaluesis alua_assertonly. - Chunk name: never a cause of refusal; used only in messages: a leading
@or=is dropped, a name beginning with the signature byte shows as “binary string”, any other is shown as is.
String tags and length
A string is a varint size (the length plus 1, so 0 means no string), then the bytes. The loader treats tags 0x04 (short) and 0x14 (long) alike and ignores the tag: it makes a short string when the length (size − 1) is at most LUAI_MAXSHORTLEN, otherwise a long string. LUAI_MAXSHORTLEN is 40 (llimits.h 5.4.7, line 216, #define LUAI_MAXSHORTLEN 40 under #if !defined, re-read from the raw file). A chunk whose tag disagrees with the length still loads.
Stripped chunks
luac -s (and string.dump(f, true)) passes strip
to luaU_dump. In ldump.c (5.4.7), with strip set:
- every function’s
sourceis written as no string (size 0), and so is a source equal to its parent’s even without strip; the loader then reuses the parent’s source (loadFunctioninlundump.c). The main function has no parent:luaU_undumppassesNULL, so a stripped chunk’s main function has an empty (null)source; lineinfo,abslineinfo,locvarsand the upvalue name list are each written with a count of 0;- everything else (header, code, constants, upvalue descriptors, nested prototypes) is unchanged.
The header byte after LUAC_NUM still holds the real upvalue count of the
main function (sizeupvalues).
Example
1B 4C 75 61 54 00 signature, version 5.4, format 0
19 93 0D 0A 1A 0A LUAC_DATA
04 08 08 sizes: Instruction, lua_Integer, lua_Number
78 56 00 00 00 00 00 00 LUAC_INT
00 00 00 00 00 28 77 40 LUAC_NUM 370.5
01 main closure has 1 upvalue (_ENV)
8B 40 68 65 6C 6C 6F 2E 6C 75 61 source "@hello.lua": varint 11 (8B), then 10 bytes
80 linedefined 0
80 lastlinedefined 0
00 01 02 numparams 0, is_vararg 1, maxstacksize 2
The values after the header are illustrative. The instruction
VARARGPREP 0 is 51 00 00 00 (opcode 81 = 0x51,
A = 0).
Sources
Lua 5.4.6 source (lua.org and the
lua/lua repository):
lundump.c (checkHeader, loadUnsigned,
loadStringN, loadFunction, constants, upvalues, debug
information), lundump.h (LUAC_DATA, LUAC_INT,
LUAC_NUM, LUAC_VERSION, LUAC_FORMAT),
lobject.h (variant tags), lparser.h (variable kinds),
ldebug.h (ABSLINEINFO, MAXIWTHABS) and
lopcodes.h (instruction formats, signed offsets, every opcode, comments
and notes). The operand-mode, NEWTABLE and stripping sections were read
from the raw files of the v5.4.7 tag on GitHub (lua/lua):
lopcodes.c, lopcodes.h, lcode.c
(luaK_settablesize), lvm.c (OP_NEWTABLE),
lobject.c (luaO_ceillog2), ldump.c and
lundump.c; no format difference from 5.4.6 was seen.