Lua’s authors do not document the precompiled format; it is defined by the source of
each release. This page follows lundump.c, lundump.h,
lopcodes.h, lvm.c and the luac sources of Lua 3.2
and 3.2.2, from lua.org (see sources). Both write version byte
0x32. All integers in a chunk are big-endian, whatever machine wrote it.
Numbers are stored as text unless luac -n asks for the machine’s own form.
The code is a byte code for a stack machine.
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: the byte ID_CHUNK (escape) and Lua. |
4 | 1 | version | 0x32. |
5 | 1 | sizeofR | 0: numbers are stored as text. Otherwise numbers are stored in the machine’s own form and this is sizeof(real). |
6 | sizeofR | test number | Only when sizeofR is not 0: 3.14159265358979323846E8 in the writer’s number form. |
The main function follows directly. The header records neither the byte order of native numbers nor whether their type is floating-point or integer. A reader can tell both from the test number.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| word | 2 bytes, big-endian. |
| long | 4 bytes, big-endian. |
| number (text) | A length byte, then that many characters: an optional - and a Lua number literal. There is no terminating zero. luac writes it with %.16g. |
| number (native) | sizeofR bytes, as they are in memory: an IEEE 754 double in the writer’s byte order in a default build. |
| string | A long 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. |
Function
| Field | Type | Meaning |
|---|---|---|
| lineDefined | long | Line where the function starts; 0 for the main function. |
| source | string | Source name. When there is no string, the loader uses the name of the chunk. |
| codeSize | long | Number of bytes of code. |
| code | codeSize bytes | The byte code. The last byte is ENDCODE (0). |
| nlocals | long | Number of local variable records; 0 without debug information. |
| locals | nlocals × (long, string) | The local variable records: a line and a name. |
| nconsts | long | Number of constants. |
| consts | nconsts constants | Each a tag byte and a value. |
| Tag | Constant | Value that follows |
|---|---|---|
| 1 | number | a number, as text or native by the header |
| 2 | string | a string |
| 4 | function | a nested function in this same layout |
| 6 | nil | nothing |
A tag is the negated type number of lobject.h. Global names, field
names, numbers, strings and nested functions share the one constant list. A function
stores no parameter count outside its code and no count of upvalues.
Local variable records
The records are in source order. A record with a name declares a local on that
line; the parameters come first. A record without a name (string length 0) ends the
scope of the last declared local that is still open. The name of stack slot
n at a line is found by reading the records before that line and keeping
the open declarations in order (luaF_getlocalname in
lfunc.c).
Instructions
The first two bytes of the code are not instructions. Byte 0 is the stack size the
function needs. Byte 1 describes the parameters: below 128
(ZEROVARARG) it is the number of fixed parameters; from 128 on the
function takes ..., has (byte − 128) fixed parameters, and gets the
extra arguments in a table named arg. Instructions start at byte 2.
| Form | Bytes | Layout |
|---|---|---|
| none | 1 | opcode |
| b | 2 | opcode, byte |
| w | 3 | opcode, word (big-endian) |
| b c | 3 | opcode, byte b, byte c |
| w c | 4 | opcode, word, byte c |
An opcode ending in W is the word form of the one without it. In the
table below K is a constant index, L a stack slot counted from the base of the
function, and J a jump distance in bytes, counted from the following instruction.
nil is the false value; Lua 3.2 has no boolean type.
| Op | Name | Operands | Action |
|---|---|---|---|
| 0 | ENDCODE | return from the function with no results | |
| 1 | RETCODE | b | return the values from stack slot b to the top |
| 2 | CALL | b c | call the function that has c arguments above it; keep b results, all of them when b is 255 |
| 3 | TAILCALL | b c | call the function that has c arguments above it, then return the values from slot b |
| 4 | PUSHNIL | b | push b + 1 nils |
| 5 | POP | b | pop b values |
| 6 | PUSHNUMBERW | w | push the number w |
| 7 | PUSHNUMBER | b | push the number b |
| 8 | PUSHNUMBERNEGW | w | push the number −w |
| 9 | PUSHNUMBERNEG | b | push the number −b |
| 10 | PUSHCONSTANTW | K (w) | push the constant |
| 11 | PUSHCONSTANT | K (b) | push the constant |
| 12 | PUSHUPVALUE | b | push upvalue b of the closure, counted from 0 |
| 13 | PUSHLOCAL | L (b) | push local L |
| 14 | GETGLOBALW | K (w) | push the global named by the constant |
| 15 | GETGLOBAL | K (b) | push the global named by the constant |
| 16 | GETTABLE | pop a key; replace the table under it with its value at that key | |
| 17 | GETDOTTEDW | K (w) | replace the table on top with its value at the constant key |
| 18 | GETDOTTED | K (b) | replace the table on top with its value at the constant key |
| 19 | PUSHSELFW | K (w) | replace the object on top with its value at the constant key, then push the object again |
| 20 | PUSHSELF | K (b) | replace the object on top with its value at the constant key, then push the object again |
| 21 | CREATEARRAYW | w | push a new table with room for w items |
| 22 | CREATEARRAY | b | push a new table with room for b items |
| 23 | SETLOCAL | L (b) | pop a value into local L |
| 24 | SETGLOBALW | K (w) | pop a value into the global named by the constant |
| 25 | SETGLOBAL | K (b) | pop a value into the global named by the constant |
| 26 | SETTABLEPOP | with table, key and value on top, store the value and pop all three | |
| 27 | SETTABLE | b | with a table b + 3 places from the top and its key just above it, store the top value there; pop only the value |
| 28 | SETLISTW | w c | pop c values into the table below them, as items w × 64 + 1 onwards |
| 29 | SETLIST | b c | pop c values into the table below them, as items b × 64 + 1 onwards (LFIELDS_PER_FLUSH = 64) |
| 30 | SETMAP | b | pop b + 1 key and value pairs into the table below them |
| 31 | NEQOP | pop y and x, push 1 if they differ, else nil | |
| 32 | EQOP | pop y and x, push 1 if they are equal, else nil | |
| 33 | LTOP | pop y and x, push 1 if x < y, else nil | |
| 34 | LEOP | pop y and x, push 1 if x <= y, else nil | |
| 35 | GTOP | pop y and x, push 1 if x > y, else nil | |
| 36 | GEOP | pop y and x, push 1 if x >= y, else nil | |
| 37 | ADDOP | pop y and x, push x + y | |
| 38 | SUBOP | pop y and x, push x − y | |
| 39 | MULTOP | pop y and x, push x × y | |
| 40 | DIVOP | pop y and x, push x / y | |
| 41 | POWOP | pop y and x, push x ^ y through the pow tag method | |
| 42 | CONCOP | pop y and x, push x .. y | |
| 43 | MINUSOP | negate the value on top | |
| 44 | NOTOP | replace nil on top with 1, and any other value with nil | |
| 45 | ONTJMPW | J (w) | if the value on top is not nil, keep it and jump forward; else pop it |
| 46 | ONTJMP | J (b) | the same with a byte |
| 47 | ONFJMPW | J (w) | if the value on top is nil, keep it and jump forward; else pop it |
| 48 | ONFJMP | J (b) | the same with a byte |
| 49 | JMPW | J (w) | jump forward |
| 50 | JMP | J (b) | jump forward |
| 51 | IFFJMPW | J (w) | pop a value; jump forward if it is nil |
| 52 | IFFJMP | J (b) | the same with a byte |
| 53 | IFTUPJMPW | J (w) | pop a value; jump back if it is not nil |
| 54 | IFTUPJMP | J (b) | the same with a byte |
| 55 | IFFUPJMPW | J (w) | pop a value; jump back if it is nil |
| 56 | IFFUPJMP | J (b) | the same with a byte |
| 57 | CLOSUREW | K (w) c | pop c values and push a closure of the constant function with them as its upvalues |
| 58 | CLOSURE | K (b) c | the same with a byte for K |
| 59 | SETLINEW | w | record that line w is running (debug information) |
| 60 | SETLINE | b | record that line b is running |
| 61 | LONGARGW | w | add w × 65536 to the operand of the next instruction |
| 62 | LONGARG | b | add b × 65536 to the operand of the next instruction |
| 63 | CHECKSTACK | b | an assertion on the stack height, for debugging Lua itself |
An operand is at most 16,777,215 (MAX_ARG). Line numbers are carried only
by SETLINE instructions.
What the loader rejects
- First byte: at the start of a chunk, anything but
1Bor the end of the input (“is not a Lua binary file”). - Signature: anything other than
Luaafter it (“bad signature”). - Version: above
0x32(“too new”) or below it (“too old”). - Number size: a sizeofR that is neither 0 nor the loading build’s
sizeof(real)(“unknown number size”). - Test number: converted to
long, it must equal the build’s own (“unknown number format”). Native numbers in the other byte order are not converted. - Counts and lines: a long that does not fit in an
unsigned int(“too many constants”, “code too long” and the like), which only a build with a 16-bitintcan meet. - Code: the last byte of the code must be
ENDCODE(“bad code”). The loader adds five moreENDCODEbytes after it in memory. - Number text: text that is not an optional
-and a number (“cannot convert number”). - Constant tag: a tag other than 1, 2, 4 or 6 (“cannot load constant”).
- Truncation: every read must return all its bytes (“unexpected end of file”).
The loader checks nothing else. It allocates whatever a length or a count states before reading the data, it has no code verifier, and it does not check a code size of 0 before testing the last byte.
What luac writes
Given several source files, luac writes one chunk for each, one after
the other in the output file.
- Debug information: only with
-d. It adds theSETLINEinstructions and the local variable records. -n: numbers in native form, with sizeofR and the test number in the header.-O: removes unused and repeated constants and renumbers the instructions that name them, removes theSETLINEinstructions and the local records, adjusts the jumps and sets the source name to the empty string (luac/opt.c).
Program behaviour
Text numbers carry 16 significant digits (%.16g), fewer than the 17 that
some doubles need to come back unchanged. Before writing a number as text,
luac converts the text back; it stops with “cannot convert number” for
one that does not read, such as an infinity from the literal 1e999.
How a file is recognised
lua_dofile and lua_dobuffer treat input as precompiled when
its first byte is 1B, and as source otherwise (ldo.c);
lua_dofile then reopens the file in binary mode (3.2 tries this for
standard input too, which has no file name; 3.2.2 does not). They load and run one
chunk after another until the input ends. luac writes
luac.out unless another name is given with -o.
Example
The 53-byte chunk that luac 3.2.2 writes for a file
ex.lua holding x = 1.5:
1B 4C 75 61 signature
32 version 3.2
00 numbers as text
00 00 00 00 lineDefined 0
00 00 00 08 source: 8 bytes
40 65 78 2E 6C 75 61 00 "@ex.lua"
00 00 00 07 code: 7 bytes
01 stack size 1
00 no parameters
0B 01 PUSHCONSTANT 1 (1.5)
19 00 SETGLOBAL 0 (x)
00 ENDCODE
00 00 00 00 no local records
00 00 00 02 2 constants
02 00 00 00 02 78 00 0: string "x"
01 03 31 2E 35 1: number "1.5"
Sources
The Lua 3.2 and 3.2.2 sources from lua.org:
lundump.c, lundump.h, lopcodes.h,
lobject.h, lvm.c, lparser.c,
lfunc.c, ldo.c and, of the compiler,
luac.c, dump.c and opt.c.