Lua’s authors do not document the precompiled format; it is defined by the source of
each release. This page follows lundump.c, lopcodes.h,
llimits.h, lvm.c and the luac sources of Lua 4.0
and 4.0.1, from lua.org (see sources). The two releases have the
same chunk format and version byte. 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. Lua 4.0 runs a stack machine: its instructions work
on the top of the stack, unlike the register instructions of Lua 5.0 and later.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: the byte ID_CHUNK (escape) and Lua. |
4 | 1 | version | 0x40: major × 16 + minor. |
5 | 1 | endianness | 1 little-endian, 0 big-endian. |
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_INSTRUCTION | Bits of an instruction that are used; 32. |
10 | 1 | SIZE_OP | Width of the opcode field in bits; 6. |
11 | 1 | SIZE_B | Width of field B; 9. |
12 | 1 | sizeof(Number) | Bytes in a number; 8 for the default double. |
13 | sizeof(Number) | test number | (Number)3.14159265358979323846E8, in the writer’s number format. |
With 8-byte numbers the header is 21 bytes. There is no format byte. The main function
follows directly. The header has no flag that says whether Number is a
floating-point or an integer type. A reader can tell from the test number: a
floating-point build writes 314,159,265.358979… (314,159,264 in single precision), an
integer build writes the integer 314,159,265. The loader converts both the number it
read and its own to long and compares only those.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| int | sizeof(int) bytes, in the header’s byte order. The loader checks no int for a negative value. |
| size_t | sizeof(size_t) bytes. |
| number | sizeof(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, stored in every function. |
| lineDefined | int | Line where the function starts; 0 for the main function. There is no end line. |
| numparams | int | Number of fixed parameters. |
| is_vararg | byte | 1 when the parameter list ends in ..., else 0. The extra arguments arrive in a local table named arg. |
| maxstacksize | int | Number of stack slots the function needs. |
| 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). |
| lineinfo | list of int | See line information. |
| kstr | list of string | The string constants. |
| knum | list of number | The number constants. |
| kproto | list of function | Nested function prototypes, each in this same layout. |
| code | list of Instruction | The instructions, each sizeof(Instruction) bytes. The last one is END. |
The chunk has no constant tags: strings and numbers are separate lists, and
instructions that name a constant say by their opcode which list they mean. It stores
neither the number of upvalues of a function nor their names: the
CLOSURE instruction of the enclosing function states how many values it
gives the closure.
Line information
The list does not hold one line per instruction. Reading starts at line 1. An entry
of 0 or more is the index of the first instruction (counted from 0) of a line that has
code; each such entry after the first moves one line down. A negative entry
−n before it moves n further lines down, for lines without code.
The list ends with MAX_INT (INT_MAX − 2, so 2147483645 with a
4-byte int). The line of an instruction is the line of the last index entry that is not
above it (luaG_getline in ldebug.c). Instructions that the
code generator merges into an earlier one add no entry.
Instructions
An instruction is an unsigned number in the chunk’s byte order, of which 32 bits are
used. The opcode is in the lowest 6 bits (lopcodes.h,
llimits.h). The other 26 bits are one operand, or two.
| Form | Bits 31–15 | Bits 14–6 | Bits 5–0 |
|---|---|---|---|
| none | unused | opcode (6) | |
| U | U (26), unsigned | opcode (6) | |
| S | S (26): stored as value + 33,554,431 | opcode (6) | |
| A B | A (17) | B (9) | opcode (6) |
In the table below, K is an index into the string constants, N into the number
constants, L a stack slot counted from the base of the function (a local variable)
and J a jump offset in S, added to the index of the following instruction.
nil is the false value; Lua 4.0 has no boolean type.
| Op | Name | Operands | Action |
|---|---|---|---|
| 0 | END | return from the function with no results | |
| 1 | RETURN | U | return the values from stack slot U to the top |
| 2 | CALL | A B | call the function in slot A with the values above it; keep B results, all of them when B is 255 |
| 3 | TAILCALL | A B | call the function in slot A, then return the values from slot B |
| 4 | PUSHNIL | U | push U nils |
| 5 | POP | U | pop U values |
| 6 | PUSHINT | S | push the number S |
| 7 | PUSHSTRING | K | push the string constant |
| 8 | PUSHNUM | N | push the number constant |
| 9 | PUSHNEGNUM | N | push the negative of the number constant |
| 10 | PUSHUPVALUE | U | push upvalue U of the closure |
| 11 | GETLOCAL | L | push local L |
| 12 | GETGLOBAL | K | push the global named by the constant |
| 13 | GETTABLE | pop a key; replace the table under it with its value at that key | |
| 14 | GETDOTTED | K | replace the table on top with its value at the constant key |
| 15 | GETINDEXED | L | replace the table on top with its value at the key in local L |
| 16 | PUSHSELF | K | replace the object on top with its value at the constant key, then push the object again |
| 17 | CREATETABLE | U | push a new table with room for U items |
| 18 | SETLOCAL | L | pop a value into local L |
| 19 | SETGLOBAL | K | pop a value into the global named by the constant |
| 20 | SETTABLE | A B | with the table A slots below the top and the key above it, store the top value there; pop B values |
| 21 | SETLIST | A B | pop B values into the table below them, as items A × 62 + 1 onwards (LFIELDS_PER_FLUSH = 62) |
| 22 | SETMAP | U | pop U key and value pairs into the table below them |
| 23 | ADD | pop y and x, push x + y | |
| 24 | ADDI | S | add S to the value on top |
| 25 | SUB | pop y and x, push x − y | |
| 26 | MULT | pop y and x, push x × y | |
| 27 | DIV | pop y and x, push x / y | |
| 28 | POW | pop y and x, push x ^ y | |
| 29 | CONCAT | U | replace the U values on top with their concatenation |
| 30 | MINUS | negate the value on top | |
| 31 | NOT | replace nil on top with 1, and any other value with nil | |
| 32 | JMPNE | J | pop two values; jump if they differ |
| 33 | JMPEQ | J | pop two values; jump if they are equal |
| 34 | JMPLT | J | pop y and x; jump if x < y |
| 35 | JMPLE | J | pop y and x; jump if x <= y (not y < x) |
| 36 | JMPGT | J | pop y and x; jump if x > y |
| 37 | JMPGE | J | pop y and x; jump if x >= y (not x < y) |
| 38 | JMPT | J | pop a value; jump if it is not nil |
| 39 | JMPF | J | pop a value; jump if it is nil |
| 40 | JMPONT | J | if the value on top is not nil, keep it and jump; else pop it |
| 41 | JMPONF | J | if the value on top is nil, keep it and jump; else pop it |
| 42 | JMP | J | jump |
| 43 | PUSHNILJMP | push nil and skip the next instruction | |
| 44 | FORPREP | J | start a numeric for with index, limit and step on top; if it runs no times, pop all three and jump |
| 45 | FORLOOP | J | add the step to the index; while the limit is not passed, jump back; else pop all three |
| 46 | LFORPREP | J | start a for over the table on top: push the first key and value, or, for an empty table, pop it and jump |
| 47 | LFORLOOP | J | take the next key and value and jump back, or pop table, key and value at the end |
| 48 | CLOSURE | A B | pop B values and push a closure of nested function A with them as its upvalues |
The virtual machine has no case for the opcodes 49 to 63 and goes on to the next instruction.
What the loader rejects
- Signature: anything other than
1B 4C 75 61(“bad signature”). - Version: above
0x40(“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.
- Sizes:
int,size_t,Instruction, SIZE_INSTRUCTION, SIZE_OP, SIZE_B andNumbermust each equal the loading build’s (“virtual machine mismatch”). - Test number: converted to
long, it must equal the build’s own (“unknown number format”). - Truncation: every read must return all its bytes (“unexpected end of file”).
- Code: the last instruction of each function must be the word
END, all bits zero (“bad code”). - One chunk: nothing may follow the main function (“apparently contains more than one chunk”).
The loader checks nothing else. It allocates whatever a count or a string length
states before reading the elements, it does not refuse negative integers, and it has no
code verifier: operands that point outside the constants, locals or code are not
noticed when the chunk is loaded. A function with no instructions makes the
END test read before the start of the code.
What luac writes
luac removes repeated number and string constants of a function before it
writes the chunk and renumbers the instructions that name them (opt.c).
Given several source files, it writes one main function with the source name
=(luac) and a stack size of 1, whose nested functions are the files and
whose code makes a closure of each and calls it.
luac -s strips the debug information (the function strip in
luac.c): in every function lineinfo and locvars
get a count of 0 and the source name becomes =(none).
lineDefined, the code, the constants and the nesting are unchanged.
How a file is recognised
lua_dofile and lua_dobuffer treat input as a precompiled
chunk when its first byte is 1B, and as source otherwise
(ldo.c); lua_dofile then reopens the file in binary mode.
A first line starting with # is skipped in source only, so it cannot
precede a chunk. luac writes luac.out unless another name is
given with -o.
Example
The header written by Lua 4.0.1 on a 64-bit little-endian machine whose
long is 8 bytes:
1B 4C 75 61 signature
40 version 4.0
01 little-endian
04 int: 4 bytes
08 size_t: 8 bytes
08 Instruction: 8 bytes
20 06 09 SIZE_INSTRUCTION 32, SIZE_OP 6, SIZE_B 9
08 Number: 8 bytes
12 E6 5B A1 B0 B9 B2 41 test number 314159265.358979…
The instruction word 0x00000095 is opcode 21 (SETLIST),
B = 2, A = 0: store the two values on top into the table below them as its items 1
and 2.
Sources
The Lua 4.0 and 4.0.1 sources from lua.org
(also readable at lua.org/source/4.0):
lundump.c, lundump.h, lopcodes.h,
llimits.h, lobject.h, lvm.c,
lcode.c, lparser.c, ldebug.c,
lfunc.c, ldo.c, llex.c and, of the compiler,
luac.c, dump.c, opt.c and print.c.