Lua 4.1 was never released: the work towards it became Lua 5.0. Three of its
pre-releases write precompiled chunks of their own: 4.1-alpha,
4.1-work3 and 4.1-work4. All three write version byte
0x41, the same header and the same function layout, and run register
instructions, but they number their opcodes and their constant type bytes differently.
This page follows the source of each (see sources).
Header
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | signature | 1B 4C 75 61: escape and Lua. |
4 | 1 | version | 0x41. |
5 | 1 | endianness | 1 little-endian, 0 big-endian. |
6 | 1 | sizeof(int) | Bytes in an int. |
7 | 1 | sizeof(size_t) | Bytes in a size_t. |
8 | 1 | sizeof(Instruction) | Bytes in an instruction (unsigned long): 4 or 8. |
9 | 1 | SIZE_OP | 6. |
10 | 1 | SIZE_A | 8. |
11 | 1 | SIZE_B | 8. |
12 | 1 | SIZE_C | 10. |
13 | 1 | sizeof(lua_Number) | Bytes in a number; 8 for double. |
14 | sizeof(lua_Number) | test number | 3.14159265358979323846E8 in the writer’s number format. |
The main function follows directly. The loader compares the test number after
converting it to long, and swaps the bytes of a chunk of the other byte
order.
Function
An int and a size_t have the sizes the header gives. A string
is a size_t length that counts the terminating zero, then that many
bytes. A list is an int count and then the items.
| Field | Type | Meaning |
|---|---|---|
| source | string | Source name. A length of 0 means that of the enclosing function. |
| lineDefined | int | Line where the function starts; 0 for the main function. |
| nupvalues | int | Number of upvalues. Their names are not stored. |
| numparams | int | Number of fixed parameters. |
| is_vararg | int | Whether the function takes .... |
| maxstacksize | int | Number of registers. |
| locvars | list of (string, int, int) | Name, first pc where the local is active and first pc where it is dead. |
| lineinfo | list of int | The line information of Lua 4.0, ending with INT_MAX − 2. |
| constants | list of constant | See constants. |
| protos | list of function | The nested functions, each in this layout. |
| code | list of Instruction | The instructions. Each compiler ends every function with its own RETURN. |
Constants
Each constant is a type byte and its value. The bytes are the type numbers of
lua.h, which differ between the three.
| Type | 4.1-alpha | 4.1-work3 | 4.1-work4 | Value |
|---|---|---|---|---|
| nil | 1 (not written) | 1 | 0 | None. |
| number | 2 | 2 | 1 | sizeof(lua_Number) bytes. |
| string | 3 | 4 | 2 | A string. |
Instructions
The opcode is in bits 0 to 5, C in bits 6 to 15, B in bits 16 to 23 and A in bits 24 to 31. Bc is C and B read as one 18-bit number, and sBc is Bc − 131071. In an 8-byte instruction the upper 32 bits are not used. R is a register, K a constant, U an upvalue and P a nested function. RK(C) is register C below 250 and constant C − 250 from 250 on. A dash means that the version has no such opcode.
| Name | alpha | work3 | work4 | Operands | Effect |
|---|---|---|---|---|---|
| MOVE | 0 | 0 | 0 | A B | R[A] = R[B]. |
| LOADK | 1 | 1 | 1 | A Bc | R[A] = K[Bc]. |
| LOADINT | 2 | 2 | – | A sBc | R[A] = the number sBc. |
| LOADBOOL | – | 3 | 2 | A B C | R[A] = true when B is not 0, else false; skips the next instruction when C is not 0. |
| LOADNIL | 3 | 4 | 3 | A B | nil into R[A] to R[B]. |
| LOADUPVAL | 4 | – | – | A Bc | R[A] = U[Bc]. |
| GETUPVAL | – | 5 | 4 | A B | R[A] = U[B]. |
| GETGLOBAL | 5 | 6 | 5 | A Bc | R[A] = the global named K[Bc]. |
| GETTABLE | 6 | 7 | 6 | A B C | R[A] = R[B][RK(C)]. |
| SETGLOBAL | 7 | 8 | 7 | A Bc | The global named K[Bc] = R[A]. |
| SETUPVAL | – | 9 | 8 | A B | U[B] = R[A]. |
| SETTABLE | 8 | 10 | 9 | A B C | R[B][RK(C)] = R[A]. |
| NEWTABLE | 9 | 11 | 10 | alpha: A Bc; others: A B C | R[A] = a new table. alpha: of size Bc. Others: array size 2B−1 (0 when B is 0), hash size 2C. |
| SELF | 10 | 12 | 11 | A B C | R[A+1] = R[B]; R[A] = R[B][RK(C)]. |
| ADD, SUB, MUL, DIV, POW | 11–15 | 13–17 | 12–16 | A B C | R[A] = R[B] operator RK(C). |
| UNM | 16 | 18 | 17 | A B | R[A] = −R[B]. |
| NOT | 17 | 19 | 18 | A B | R[A] = not R[B]. |
| CONCAT | 18 | 20 | 19 | A B C | Joins R[B] to R[C] into R[A]. |
| JMP | 19 | 21 | 20 | sBc | Jumps by sBc from the next instruction. |
| CJMP | 20 | 22 | – | sBc | The jump that belongs to the test before it. |
| TESTEQ, TESTNE, TESTLT, TESTLE, TESTGT, TESTGE | 21–26 | 23–28 | 21–26 | A C | Compares R[A] with RK(C). alpha and work3 take the following CJMP when true, else skip it; work4 goes on to the following JMP when true, else skips it. |
| TESTT | 27 | 29 | 27 | A B | The test succeeds when R[B] counts as true (alpha: is not nil); R[B] is then copied to R[A]. |
| TESTF | 28 | 30 | 28 | A B | The test succeeds when R[B] counts as false (alpha: is nil); R[B] is then copied to R[A]. |
| NILJMP | 29 | – | – | A | R[A] = nil; skips the next instruction. |
| CALL | 30 | 31 | 29 | A B C | Calls R[A]. alpha and work3: B arguments and C results, 255 meaning up to the top. work4: B − 1 arguments and C − 1 results, 0 meaning up to the top. |
| RETURN | 31 | 32 | 30 | A B | Returns values from R[A]. alpha and work3: B values, 255 meaning up to the top. work4: B − 1 values, 0 meaning up to the top. |
| FORPREP | 32 | 33 | – | A sBc | Prepares a numeric for and goes to its FORLOOP at pc − sBc. |
| FORLOOP | 33 | 34 | 31 | A sBc | Numeric for: jumps by sBc while the loop goes on. |
| TFORPREP | 34 | 35 | – | A sBc | Prepares a table for and goes to its TFORLOOP at pc − sBc. |
| TFORLOOP | 35 | 36 | 32 | A sBc | Table for: jumps by sBc while the loop goes on. |
| SETLIST | 36 | 37 | 33 | A Bc | Stores (Bc mod 32) + 1 values after R[A] into the table R[A], from item Bc − (Bc mod 32) + 1. |
| SETLISTO | 37 | 38 | 34 | A Bc | As SETLIST, with the values up to the top. |
| CLOSE | – | 39 | 35 | A | Closes the upvalues from R[A]. |
| CLOSURE | 38 | 40 | 36 | A Bc | R[A] = a closure of P[Bc]. alpha takes the upvalues from the registers from R[A]. work3 and work4 follow it with one MOVE or GETUPVAL for each upvalue, which names its source and is not executed. |
Which pre-release wrote a chunk
No header field says it. Every compiler ends each function with a fixed instruction:
| Version | Last instruction | Word |
|---|---|---|
| 4.1-alpha | RETURN 0 0 0 | 0x0000001F |
| 4.1-work3 | RETURN 0 0 0 | 0x00000020 |
| 4.1-work4 | RETURN 0 1 0 | 0x0001001E; 0x0000001E for the main function that luac makes to join several files |
The viewer reads the file with the type bytes, opcode count and final instruction of
each version. A chunk that reads wholly as exactly one of them is of that version. When
the file reads wholly as none, as with a cut or damaged chunk, a version is named only
if it alone has a complete function that ends in its RETURN and also has a
string constant under its type byte. Otherwise the code is not decoded.
What luac writes
luac -s removes the line information and the local variables and sets the
source name of every function to =(none). When it joins several files, it
adds a main function with the source name =(luac) that calls each of them.
What the loader rejects
Sizes of int, size_t, Instruction or
lua_Number or field widths that differ from its own build’s, a wrong test
number, a version byte that is too new or too old, an unknown constant type, a chunk
that ends early, code that its verifier turns down and anything after the first chunk
(“apparently contains more than one chunk”).
Divergence from spec
The luac of 4.1-work3 and 4.1-work4 stores a nil constant
when the source compares a value with nil: the type byte and no value. The
dump and load code of the same release has no case for it, and the loader refuses the
chunk as a bad constant type. Such a file is therefore written by luac but
cannot be run. The viewer reads the constant as nil and reports it on the Problems
tab.
Sources
The packages of Lua 4.1-alpha, 4.1-work3 and 4.1-work4 from
lua.org/work/old: lundump.c,
lundump.h, lopcodes.h, lopcodes.c,
llimits.h, lua.h, lvm.c,
lparser.c, lcode.c, ldebug.c and, of the
compiler, luac.c, dump.c, print.c and
opt.c (alpha).