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.1,
from lua.org (see sources). Words and longs in a chunk are
big-endian. The number type is chosen when luac is built, and the header
records it. 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 | 0x31. |
5 | 1 | number type | ID_NUMBER: see below. |
6 | 1 | sizeofR | sizeof(real) of the writing build. |
7 | sizeofR | test number | 3.14159265358979323846E8 in the chunk’s number form. |
| Number type | Form |
|---|---|
64 (d), ID_REAL8 | IEEE 754 double, big-endian; the default. |
66 (f), ID_REAL4 | IEEE 754 single, big-endian. |
6C (l), ID_INT4 | 4-byte integer, big-endian. |
3F (?), ID_NATIVE | The machine’s own bytes, sizeofR of them. The header does not record their byte order or whether they are integers; the test number shows both. |
The main function follows directly.
Basic types
| Type | Encoding |
|---|---|
| byte | One byte. |
| word | 2 bytes, big-endian. |
| long | 4 bytes, big-endian. |
| number | sizeofR bytes in the form the header names. |
| string | A word length that counts the terminating zero, then that many bytes including the zero. A length of 0 means no string. The loader drops the last byte without looking at it. luac refuses longer strings than a word can count. |
Function
| Field | Type | Meaning |
|---|---|---|
| lineDefined | word | Line where the function starts; 0 for the main function. |
| fileName | string | Name of the source. |
| codeSize | long | Number of bytes of code. luac warns above 65,535. |
| code | codeSize bytes | The byte code, ending in ENDCODE. |
| nlocals | word | Number of local variable records; 0 without debug information. |
| locals | nlocals × (word, string) | The local variable records: a line and a name. |
| nconsts | word | Number of constants. |
| consts | nconsts constants | Each a tag byte and a value. |
| Tag | Constant | Value that follows |
|---|---|---|
| 1 | number | a number |
| 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 64
(ZEROVARARG) it is the number of fixed parameters; from 64 on the
function takes ..., has (byte − 64) 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 |
Many opcodes have short forms with the operand built in: PUSHLOCAL3
is PUSHLOCAL with operand 3, in one byte. A name ending in
W is the word form. 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.1 has no
boolean type.
| Op | Name | Operands | Action |
|---|---|---|---|
| 0 | ENDCODE | return from the function with no results | |
| 1 | PUSHNIL | b | push b + 1 nils |
| 2 | PUSHNIL0 | push one nil | |
| 3 | PUSHNUMBER | b | push the number b |
| 4–6 | PUSHNUMBER0 to PUSHNUMBER2 | push the number 0 to 2 | |
| 7 | PUSHNUMBERW | w | push the number w |
| 8 | PUSHCONSTANT | K (b) | push the constant |
| 9–16 | PUSHCONSTANT0 to PUSHCONSTANT7 | push constant 0 to 7 | |
| 17 | PUSHCONSTANTW | K (w) | push the constant |
| 18 | PUSHUPVALUE | b | push upvalue b of the closure, counted from 0 |
| 19–20 | PUSHUPVALUE0, PUSHUPVALUE1 | push upvalue 0 or 1 | |
| 21 | PUSHLOCAL | L (b) | push local L |
| 22–29 | PUSHLOCAL0 to PUSHLOCAL7 | push local 0 to 7 | |
| 30 | GETGLOBAL | K (b) | push the global named by the constant |
| 31–38 | GETGLOBAL0 to GETGLOBAL7 | the same for constant 0 to 7 | |
| 39 | GETGLOBALW | K (w) | push the global named by the constant |
| 40 | GETTABLE | pop a key; replace the table under it with its value at that key | |
| 41 | GETDOTTED | K (b) | replace the table on top with its value at the constant key |
| 42–49 | GETDOTTED0 to GETDOTTED7 | the same for constant 0 to 7 | |
| 50 | GETDOTTEDW | K (w) | replace the table on top with its value at the constant key |
| 51 | PUSHSELF | K (b) | replace the object on top with its value at the constant key, then push the object again |
| 52–59 | PUSHSELF0 to PUSHSELF7 | the same for constant 0 to 7 | |
| 60 | PUSHSELFW | K (w) | the same with a word for K |
| 61 | CREATEARRAY | b | push a new table with room for b items |
| 62–63 | CREATEARRAY0, CREATEARRAY1 | push a new table with room for 0 or 1 items | |
| 64 | CREATEARRAYW | w | push a new table with room for w items |
| 65 | SETLOCAL | L (b) | pop a value into local L |
| 66–73 | SETLOCAL0 to SETLOCAL7 | pop a value into local 0 to 7 | |
| 74 | SETGLOBAL | K (b) | pop a value into the global named by the constant |
| 75–82 | SETGLOBAL0 to SETGLOBAL7 | the same for constant 0 to 7 | |
| 83 | SETGLOBALW | K (w) | pop a value into the global named by the constant |
| 84 | SETTABLE0 | with table, key and value on top, store the value and pop all three | |
| 85 | 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 |
| 86 | SETLIST | b c | pop c values into the table below them, as items b × 64 + 1 onwards (LFIELDS_PER_FLUSH = 64) |
| 87 | SETLIST0 | b | pop b values into the table below them, as items 1 onwards |
| 88 | SETLISTW | w c | pop c values into the table below them, as items w × 64 + 1 onwards |
| 89 | SETMAP | b | pop b + 1 key and value pairs into the table below them |
| 90 | SETMAP0 | pop one key and value pair into the table below it | |
| 91 | EQOP | pop y and x, push 1 if they are equal, else nil | |
| 92 | NEQOP | pop y and x, push 1 if they differ, else nil | |
| 93 | LTOP | pop y and x, push 1 if x < y, else nil | |
| 94 | LEOP | pop y and x, push 1 if x <= y, else nil | |
| 95 | GTOP | pop y and x, push 1 if x > y, else nil | |
| 96 | GEOP | pop y and x, push 1 if x >= y, else nil | |
| 97 | ADDOP | pop y and x, push x + y | |
| 98 | SUBOP | pop y and x, push x − y | |
| 99 | MULTOP | pop y and x, push x × y | |
| 100 | DIVOP | pop y and x, push x / y | |
| 101 | POWOP | pop y and x, push x ^ y through the pow tag method | |
| 102 | CONCOP | pop y and x, push x .. y | |
| 103 | MINUSOP | negate the value on top | |
| 104 | NOTOP | replace nil on top with 1, and any other value with nil | |
| 105 | ONTJMP | J (b) | if the value on top is not nil, keep it and jump forward; else pop it |
| 106 | ONTJMPW | J (w) | the same with a word |
| 107 | ONFJMP | J (b) | if the value on top is nil, keep it and jump forward; else pop it |
| 108 | ONFJMPW | J (w) | the same with a word |
| 109 | JMP | J (b) | jump forward |
| 110 | JMPW | J (w) | the same with a word |
| 111 | IFFJMP | J (b) | pop a value; jump forward if it is nil |
| 112 | IFFJMPW | J (w) | the same with a word |
| 113 | IFTUPJMP | J (b) | pop a value; jump back if it is not nil |
| 114 | IFTUPJMPW | J (w) | the same with a word |
| 115 | IFFUPJMP | J (b) | pop a value; jump back if it is nil |
| 116 | IFFUPJMPW | J (w) | the same with a word |
| 117 | CLOSURE | K (b) c | pop c values and push a closure of the constant function with them as its upvalues |
| 118 | CLOSUREW | K (w) c | the same with a word for K |
| 119 | CALLFUNC | b c | call the function that has c arguments above it; keep b results, all of them when b is 255 |
| 120 | CALLFUNC0 | b | call with b arguments; keep no result |
| 121 | CALLFUNC1 | b | call with b arguments; keep one result |
| 122 | RETCODE | b | return the values from stack slot b to the top |
| 123 | SETLINE | b | record that line b is running (debug information) |
| 124 | SETLINEW | w | record that line w is running |
| 125 | POP | b | pop b + 1 values |
| 126 | POP0 | pop one value | |
| 127 | POP1 | pop two values |
The virtual machine has no case for the byte values 128 to 255. 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
0x31(“too new”) or below it (“too old”). - Number signature: the number type and sizeofR must both equal the loading build’s (“unknown number signature”).
- Test number: it must equal the build’s own exactly (“unknown number representation”).
- Code size: a size that does not fit in an
unsigned int(“code too long”), which only a build with a 16-bitintcan meet. - Constant tag: a tag other than 1, 2, 4 or 6 (“bad constant”).
- Truncation: every read must return all its bytes (“unexpected end of file”).
The loader checks nothing else. It does not test that the code ends in
ENDCODE, it allocates whatever a length or a count states before reading
the data, and it has no code verifier.
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. -O: removes theSETLINEinstructions and the local records, replaces long forms by short ones, merges repeated constants and renumbers the instructions that name constants, and closes the gaps in the code (luac/opt.c).
Program behaviour
- Default double form on 64-bit systems:
DumpDoubleandLoadDoublehandle a double as twolongvalues and assume a 4-bytelong. Built wherelongis 8 bytes,luacwrites 8 bytes of which 4 are the low half of the number and 4 come from memory beside it: on a little-endian machine the unrelated 4 come first, on a big-endian machine second. The high half of the number is not written. -Oand closures: the renumbering of constants leaves the constant index ofCLOSUREunchanged, so an optimised chunk can name a wrong or missing constant there.-Oand numbers: the search for repeated constants compares only the integer view of the value (value.i). A build with double numbers on a 64-bit little-endian machine merges 2.5 with 3.5, whose low 32 bits are the same, sow = 3.5afterz = 2.5stores 2.5.
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. 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 for a file ex.lua holding x = 1.5, from
luac 3.1 built with native numbers on a 64-bit little-endian
machine:
1B 4C 75 61 signature
31 version 3.1
3F 08 native numbers, 8 bytes
12 E6 5B A1 B0 B9 B2 41 test number 314159265.358979…
00 00 lineDefined 0
00 07 fileName: 7 bytes
65 78 2E 6C 75 61 00 "ex.lua"
00 00 00 05 code: 5 bytes
01 stack size 1
00 no parameters
0A PUSHCONSTANT1 (1.5)
4B SETGLOBAL0 (x)
00 ENDCODE
00 00 no local records
00 02 2 constants
02 00 02 78 00 0: string "x"
01 00 00 00 00 00 00 F8 3F 1: number 1.5
Sources
The Lua 3.1 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.