This page follows Lua 5.5’s ldump.c, lundump.c,
lundump.h, lopcodes.h and lopcodes.c (5.5.0). The
format is not documented by the Lua authors. Compared with 5.4, the header checks the size
and value of int and Instruction as well, the variable-length
number changed direction, integer constants are stored in a compact zigzag form, a string
that occurs again is stored once and then referred to by number, the code and the absolute
line table are aligned, each function has a flag byte instead of the vararg byte, the
source name moved after the nested functions, and the instruction set gained
GETVARG and ERRNNIL and a new layout for NEWTABLE
and SETLIST.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61. |
4 | 1 | version | 0x55. |
5 | 1 | format | 0, the official format. |
6 | 6 | LUAC_DATA | 19 93 0D 0A 1A 0A. |
12 | 1 | sizeof(int) | Followed by int −0x5678 in the writer’s byte order (88 A9 FF FF with 4-byte ints, little-endian). |
17* | 1 | sizeof(Instruction) | Followed by LUAC_INST, 0x12345678 (78 56 34 12). |
22* | 1 | sizeof(lua_Integer) | Followed by −0x5678 as a lua_Integer. |
31* | 1 | sizeof(lua_Number) | Followed by LUAC_NUM, −370.5 as a lua_Number (00 00 00 00 00 28 77 C0 with 8-byte floats, little-endian). |
40* | 1 | upvalues | Number of upvalues of the main closure. |
* With 4-byte int and Instruction, 8-byte integers and floats. Each
size is a byte and is followed by that many bytes of value, so the offsets move with the sizes.
There are four checks: size, then value, for each of the four types; 5.4 had three.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| varint | Used for every size, count, line number and pc. Seven bits per byte, most significant group first; a byte with its top bit set means more bytes follow, and the last byte has it clear. This is the reverse of 5.4. 300 is 82 2C; 5 is 05. The loader refuses a number when the value so far exceeds the limit shifted right by 7 before it adds the next 7 bits. |
| integer | Constants only: a varint holding the zigzag code of the value. A non-negative x is stored as 2x, a negative one as −2x−1, so 0, −1, 1, −2, 2 become 0, 1, 2, 3, 4. Stored as an unsigned 64-bit number whatever the size of lua_Integer. |
| number | sizeof(lua_Number) bytes, native representation. |
| string | A varint size. When it is greater than 0 the string has size−1 characters, and size bytes follow: the characters and a terminating zero, which is stored in 5.5. The string is also given the next index, counting from 1 in the order strings are first stored in the chunk. When size is 0, a varint index follows: 0 means no string, any other value means the string stored earlier with that index, stored again as a reference rather than as bytes. |
| list of T | A varint count, then that many T. |
| padding | Zero to n−1 bytes that bring the file offset (counted from the first byte of the chunk) to a multiple of n. The loader reads and ignores them. |
Function prototype
| Field | Type | Meaning |
|---|---|---|
| linedefined | varint | First line. |
| lastlinedefined | varint | Last line. |
| numparams | byte | Fixed parameters. |
| flag | byte | Bit 0 (PF_VAHID, 1): the function has hidden vararg arguments. Bit 1 (PF_VATAB, 2): it has a vararg table. Bit 2 (PF_FIXED, 4): the prototype lives in fixed memory; the loader clears this bit and sets it only for a chunk loaded from a fixed buffer, so it is not meaningful in a file. |
| maxstacksize | byte | Registers needed. |
| code | count, padding to 4, Instructions | The count is a varint; after it come padding bytes up to a multiple of 4 (sizeof(Instruction)), then the instructions, 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. |
| source | string | Source name, stored after the nested functions (in 5.4 it came first). No string (size 0, index 0) when stripped. 5.5 does not fall back to the parent’s source: every function writes its source, and one that equals a string already written is stored as a reference to it. Because the nested functions come first, the first nested function holds the text and the functions after it, and the parent, hold references. |
| lineinfo | list of signed byte | For each instruction, the change in line number from the previous instruction. |
| abslineinfo | count, padding to 4, pairs | A varint count; when it is greater than 0, padding up to a multiple of 4 (sizeof(int)) and then that many pairs of two native ints (4 bytes each): an instruction index and its line. These two numbers are not varints. 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 (byte 80) marks an instruction whose line must be taken
from abslineinfo.
Constants
| Tag byte | Value | Then |
|---|---|---|
0x00 | nil (LUA_VNIL) | nothing |
0x01 | false (LUA_VFALSE) | nothing |
0x11 | true (LUA_VTRUE) | nothing |
0x03 | integer (LUA_VNUMINT) | a zigzag varint |
0x13 | float (LUA_VNUMFLT) | a number |
0x04 | short string (LUA_VSHRSTR) | a string |
0x14 | long string (LUA_VLNGSTR) | a string |
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) |
ivABC | vC (10) in bits 31–22, vB (6) in bits 21–16 | 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. vB and vC are the
6-bit and 10-bit arguments of NEWTABLE and SETLIST.
| 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]:shortstring] |
| 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]:shortstring] |
| 15 | SETTABUP | A B C | UpValue[A][K[B]:shortstring] := 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]:shortstring] := RK(C) |
| 19 | NEWTABLE | A vB vC k | R[A] := {}; vB is the log₂ of the hash size plus 1, or 0 for hash size 0; the array size is vC, or, when k is set, EXTRAARG _ vC (below). The next instruction is always an EXTRAARG |
| 20 | SELF | A B C | R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] (C always names a constant; 5.4 allowed RK) |
| 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 | SHLI | A B sC | R[A] := sC << R[B] |
| 33 | SHRI | A B sC | R[A] := R[B] >> sC |
| 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; k: the arguments were flipped |
| 48 | MMBINK | A B C k | Call metamethod C over R[A] and K[B]; k: the arguments were flipped |
| 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; C = 0 means top is set to last_result + 1 for the next open instruction |
| 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 has hidden vararg arguments, 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 vB vC k | R[A][vC+i] := R[A+i], 1 ≤ i ≤ vB; vB = 0 means the real count is the top; when k is set, the real C is EXTRAARG _ vC |
| 79 | CLOSURE | A Bx | R[A] := closure(KPROTO[Bx]) |
| 80 | VARARG | A B C k | R[A], …, R[A+C−2] := varargs; C = 0 means use the actual number of varargs and set top; k: the function has a vararg table, which is in R[B] |
| 81 | GETVARG | A B C | R[A] := R[B][R[C]], where R[B] is the vararg parameter (new in 5.5) |
| 82 | ERRNNIL | A Bx | Raise an error if R[A] ~= nil; K[Bx − 1] is the global name, and Bx = 0 means the index of the name did not fit in Bx (new in 5.5) |
| 83 | VARARGPREP | A | Adjust the vararg parameters on entry |
| 84 | EXTRAARG | Ax | A larger argument for the previous instruction |
Notes from lopcodes.h, in addition to those in the table: in the comparisons with an
immediate operand (EQI, LTI, LEI, GTI,
GEI) the C field signals whether the original operand was a float. For
comparisons k gives the result the test should accept. LOADKX and
NEWTABLE are always followed by an EXTRAARG. Every skip assumes the next
instruction is a jump.
NEWTABLE and SETLIST sizes
The compiler (luaK_settablesize in lcode.c) writes
vB = ceil(log2(hash size)) + 1, or 0 when the hash size is 0. It splits the array size:
vC = size mod 1024 (MAXARG_vC + 1) and the EXTRAARG that always
follows holds size ÷ 1024; k is set when that quotient is not 0. In 5.4 the split was at
256 with an 8-bit C. “EXTRAARG _ vC” in lopcodes.h means the Ax bits concatenated above
the 10 bits of vC. SETLIST reads its vC 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 (now including ivABC); 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 (vB = 0 for
SETLIST); 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. “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 | NEWTABLE | ivABC | yes | ||||
| 20–45 | SELF, ADDI, ADDK to IDIVK, BANDK, BORK, BXORK, SHLI, SHRI, 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 | ivABC | yes | ||||
| 79 | CLOSURE | iABx | yes | ||||
| 80 | VARARG | iABC | yes | yes | |||
| 81 | GETVARG | iABC | yes | ||||
| 82 | ERRNNIL | iABx | |||||
| 83 | VARARGPREP | iABC | yes | yes | |||
| 84 | EXTRAARG | iAx |
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.5.0 lundump.c.
- Header, checked in this order, each field refused if it differs: the signature after its first byte (
Lua); the version byte (0x55); the format byte (0);LUAC_DATA; the size byte and then the value ofint(−0x5678),Instruction(0x12345678),lua_Integer(−0x5678) andlua_Number(−370.5), each in the host’s byte order and format. The byte after the header is the main function’s upvalue count. - Truncation: running out of input in any read, including inside a varint or the padding, is refused.
- Integer overflow: before adding each byte’s 7 bits,
loadVarintrefuses the number if the value so far is greater thanlimit >> 7. The limit isINT_MAXfor everyint,MAX_SIZEfor string sizes and the largest unsigned 64-bit number for integer constants and string indexes. - Constants: an unknown tag is refused (“invalid constant”), unlike 5.4. A string constant that comes out as no string (size 0 and index 0) is refused (“bad format for constant string”); the source name, local variable names and upvalue names may be absent.
- String reuse: an index that is not the index of a string already stored is refused (“invalid string index”).
- Upvalue names: as in 5.4, a non-zero count of names is replaced by the real upvalue count; a valid chunk has 0 or exactly that many.
- Main upvalues: after the main function is loaded, the header’s upvalue count must equal the number of upvalues the function lists, or the chunk is refused (“corrupted chunk”). 5.4 has this check only as an assertion in debug builds.
- Padding: its content is not checked.
- Code: not verified;
luai_verifycodeis empty.
Stripped chunks
luac -s (and string.dump(f, true)) writes every function’s
lineinfo, abslineinfo, locvars and upvalue name list with a count
of 0 and its source as no string (size 0, index 0). Everything else (header, code,
constants, upvalue descriptors, nested prototypes) is unchanged. A stripped chunk has no
absolute-line padding, since that table is empty.
Example
1B 4C 75 61 55 00 signature, version 5.5, format 0
19 93 0D 0A 1A 0A LUAC_DATA
04 88 A9 FF FF sizeof(int) 4, int -0x5678
04 78 56 34 12 sizeof(Instruction) 4, LUAC_INST
08 88 A9 FF FF FF FF FF FF sizeof(lua_Integer) 8, -0x5678
08 00 00 00 00 00 28 77 C0 sizeof(lua_Number) 8, LUAC_NUM -370.5
01 main closure has 1 upvalue (_ENV)
00 00 00 01 16 linedefined 0, lastlinedefined 0, numparams 0, flag 1 (vararg), maxstacksize 22
30 48 instructions
00 one padding byte: the code starts at offset 0x30
53 00 00 00 VARARGPREP 0 (opcode 83 = 0x53)
The bytes are the start of a chunk written by luac 5.5.1.
Sources
Lua 5.5.0 source (lua.org and the
lua/lua repository): ldump.c
(dumpVarint, dumpInteger, dumpString,
dumpAlign, dumpFunction), lundump.c
(checkHeader, loadVarint, loadString,
loadAlign, loadFunction, luaU_undump),
lundump.h, lobject.h (variant tags, PF_ flags),
lopcodes.h and lopcodes.c (formats, every opcode, notes and
luaP_opmodes) and lcode.c (luaK_settablesize). The listing of
a chunk was compared with the output of luac -l -l 5.5.1.