LuaJIT saves compiled functions as bytecode dumps, with string.dump or the
command luajit -b. The format is its own: it shares nothing with the chunks of
the official luac. This page describes dump version 1, written by LuaJIT 2.0,
from the first release with bytecode dumps (2.0.0-beta8, June 2011) to the
v2.0 branch today. LuaJIT 2.1 writes
dump version 2. The LuaJIT authors do not
publish a specification; the page follows the source, whose header file
lj_bcdump.h holds the grammar of the format.
Header
| Field | Type | Meaning |
|---|---|---|
| signature | 3 bytes | 1B 4C 4A (escape, LJ). |
| version | byte | 01. A private change of the format must use 80 or higher. |
| flags | ULEB128 | The bits below. The loader refuses a bit it does not know. |
| name length | ULEB128 | Only when the dump is not stripped. |
| name | bytes | Only when not stripped: the chunk name, without a terminating zero. A name that starts with @ is a file name, as in Lua. |
| Flag | Name | Meaning |
|---|---|---|
0x01 | BCDUMP_F_BE | Written on a big-endian machine: instructions, upvalue references and the line table are big-endian. |
0x02 | BCDUMP_F_STRIP | No chunk name and no debug information. |
0x04 | BCDUMP_F_FFI | The code uses FFI constants; a build without the FFI library refuses the dump. |
There is no flag for the word size: 32-bit and 64-bit builds of LuaJIT 2.0 share one frame layout.
Basic types
| Type | Encoding |
|---|---|
| byte | 8 bits. |
| ULEB128 | An unsigned 32-bit number, seven bits per byte, least significant group first; a set top bit means another byte follows. |
| 33-bit ULEB128 | The same encoding of a 33-bit value. Its lowest bit is a tag; the other 32 bits are the number. Used only for number constants. |
| instruction | 4 bytes, in the byte order of the flags. |
| upvalue reference | 2 bytes, in the byte order of the flags. |
| string | Raw bytes. Its length comes from a type code or a length before it, so it may contain zero bytes. |
Prototypes
After the header come the functions, each preceded by its length, and a zero byte ends the dump:
dump = header proto+ 0
proto = length(ULEB128) data
data = head instructions upvalue-references gc-constants number-constants [debug]
| Field of the head | Type | Meaning |
|---|---|---|
| flags | byte | The bits below. |
| numparams | byte | Fixed parameters. |
| framesize | byte | Stack slots needed. |
| numuv | byte | Upvalues. |
| numkgc | ULEB128 | Constants that are objects: strings, tables, functions and FFI values. |
| numkn | ULEB128 | Number constants. |
| numbc | ULEB128 | Instructions stored. |
| debuglen | ULEB128 | Only when the dump is not stripped: the size of the debug information in bytes. |
| firstline | ULEB128 | Only when debuglen is not 0: the line the function starts on, 0 for the main chunk. |
| numline | ULEB128 | Only when debuglen is not 0: the number of lines the function spans. |
| Prototype flag | Name | Meaning |
|---|---|---|
0x01 | PROTO_CHILD | Has nested functions. |
0x02 | PROTO_VARARG | Takes variable arguments. |
0x04 | PROTO_FFI | Uses KCDATA constants of the FFI. |
The writer stores only these bits. The others in lj_obj.h describe a running
function.
Order of the functions
Nested functions are written before the function that contains them, and the main function is written last. The loader keeps a stack: each prototype read is pushed, and a constant of the nested function kind takes the one on top. After the last prototype exactly one must be left, the main function.
The first instruction of a function in memory is a function header (FUNCF or
FUNCV). It is not stored, so numbc is one less than the function’s
instruction count, and the first stored instruction is pc 1.
Constants
Object constants
Each starts with a ULEB128 type code. An instruction names an object constant by its distance from the end of the list: operand D means stored entry numkgc − 1 − D.
| Code | Constant | Then |
|---|---|---|
| 0 | nested function | Nothing: the prototype on top of the loader’s stack. |
| 1 | template table | A table, below. |
| 2 | signed 64-bit integer (int64_t cdata, written 1LL) | Two ULEB128: low word, high word. |
| 3 | unsigned 64-bit integer (uint64_t cdata, 1ULL) | Two ULEB128: low word, high word. |
| 4 | complex number (complex double cdata, 1i) | Four ULEB128: the real part’s low and high words, then the imaginary part’s. |
| 5 + n | string of n bytes | The n bytes. |
Template tables
A table constructor whose keys and values are constants is stored ready made, and
TDUP copies it.
table = narray(ULEB128) nhash(ULEB128) value[narray] (key value)[nhash]
The array part starts at index 0 and runs to the last element that is not nil, so a
constructor such as {1, 2} stores three elements, the first of them nil. Each
key and each value starts with a ULEB128 type code:
| Code | Value | Then |
|---|---|---|
| 0 | nil | Nothing. |
| 1 | false | Nothing. |
| 2 | true | Nothing. |
| 3 | integer | ULEB128 of the 32-bit two’s complement value. |
| 4 | floating-point number | Two ULEB128: the low and high words of the double. |
| 5 + n | string of n bytes | The n bytes. |
A key is never nil. On builds without dual numbers, the writer stores a whole number as an integer in array elements and hash values, but leaves a number key as a floating-point number. The order of the hash entries is the order of the table in memory and may differ between dumps of one source.
Number constants
An instruction names a number constant by its index from the start of the list. Each starts with a 33-bit ULEB128; its lowest bit, which is the lowest bit of the first byte, gives the kind:
| Low bit | Number | Encoding |
|---|---|---|
| 0 | 32-bit signed integer | The other 32 bits are the value in two’s complement. |
| 1 | IEEE 754 double | The other 32 bits are the low word; a plain ULEB128 with the high word follows. |
The writer stores a double that holds a 32-bit integer as an integer (−0.0 stays a
double). The start index of a TSETM instruction is a number constant with
the value 252 + index.
A fault in early writers
Until February 2012, so in 2.0.0-beta8 and beta9, the writer produced a wrong 33-bit
ULEB128 for integers from −2,147,483,648 to −2,013,265,921 and for doubles whose low word
lies from 0x80000000 to 0x87FFFFFF. Such a file holds another
number than its source did; nothing in the file says which writer made it.
Upvalue references
One 16-bit value per upvalue, telling the loader where the closure takes it from.
| Bits | Meaning |
|---|---|
0x8000 | PROTO_UV_LOCAL: the upvalue is a local variable of the enclosing function, in the slot given by the low bits. Without it, the low bits are the number of an upvalue of the enclosing function. |
0x4000 | PROTO_UV_IMMUTABLE: set with 0x8000 when the local is never assigned after its initial value. Added in July 2012; earlier dumps do not set it. |
| low bits | The slot or upvalue number. |
Debug information
Present when the dump is not stripped; its size is debuglen. It has three
parts.
Line table
One entry per stored instruction: its line minus firstline. The width of an
entry follows from numline: 1 byte below 256, 2 bytes below 65,536,
otherwise 4 bytes. Wide entries are in the byte order of the flags.
Upvalue names
One zero-terminated name per upvalue.
Local variables
A list in the order in which the variables start, ended by a zero byte. Each entry is:
| Field | Type | Meaning |
|---|---|---|
| name | byte, or zero-terminated string | A byte below 7 names an internal variable (table below). Any other first byte starts a name, ended by a zero. |
| start | ULEB128 | The first pc at which the variable exists, as the difference from the previous entry’s start. |
| length | ULEB128 | The end pc minus the start pc. |
| Byte | Internal variable |
|---|---|
1 | (for index) |
2 | (for limit) |
3 | (for step) |
4 | (for generator) |
5 | (for state) |
6 | (for control) |
The debug information holds no slot numbers: at a given pc, the variable in slot n is the nth one, counting from 0, among those that exist there.
Instructions
| Format | Bits 31–24 | Bits 23–16 | Bits 15–8 | Bits 7–0 |
|---|---|---|---|---|
| ABC | B | C | A | opcode |
| AD | D (16 bits) | A | opcode | |
An opcode with a B operand uses ABC, any other uses AD. A jump target is the pc of the
instruction + 1 + D − 32,768. The operand kinds in the names are: V a variable slot, S a
string constant, N a number constant, P a primitive (0 nil, 1 false, 2 true), B a literal
byte and M multiple arguments or results. R[x] is a stack slot,
K[x] an object constant (counted from the end of the list),
N[x] a number constant and U[x] an upvalue.
A comparison or test is followed by a JMP: when the condition holds the jump
is taken, otherwise it is skipped. MULTRES stands for the number of values left
by the previous instruction that returned all its results.
| Op | Name | Operands | Action |
|---|---|---|---|
| 0 | ISLT | A var, D var | R[A] < R[D] |
| 1 | ISGE | A var, D var | not R[A] < R[D] |
| 2 | ISLE | A var, D var | R[A] <= R[D] |
| 3 | ISGT | A var, D var | not R[A] <= R[D] |
| 4 | ISEQV | A var, D var | R[A] == R[D] |
| 5 | ISNEV | A var, D var | R[A] ~= R[D] |
| 6 | ISEQS | A var, D str | R[A] == K[D] |
| 7 | ISNES | A var, D str | R[A] ~= K[D] |
| 8 | ISEQN | A var, D num | R[A] == N[D] |
| 9 | ISNEN | A var, D num | R[A] ~= N[D] |
| 10 | ISEQP | A var, D pri | R[A] == primitive D |
| 11 | ISNEP | A var, D pri | R[A] ~= primitive D |
| 12 | ISTC | A dst, D var | If R[D] is true: R[A] := R[D] and jump |
| 13 | ISFC | A dst, D var | If R[D] is false or nil: R[A] := R[D] and jump |
| 14 | IST | D var | R[D] is true |
| 15 | ISF | D var | R[D] is false or nil |
| 16 | MOV | A dst, D var | R[A] := R[D] |
| 17 | NOT | A dst, D var | R[A] := not R[D] |
| 18 | UNM | A dst, D var | R[A] := −R[D] |
| 19 | LEN | A dst, D var | R[A] := #R[D] |
| 20 | ADDVN | A dst, B var, C num | R[A] := R[B] + N[C] |
| 21 | SUBVN | A dst, B var, C num | R[A] := R[B] − N[C] |
| 22 | MULVN | A dst, B var, C num | R[A] := R[B] × N[C] |
| 23 | DIVVN | A dst, B var, C num | R[A] := R[B] / N[C] |
| 24 | MODVN | A dst, B var, C num | R[A] := R[B] % N[C] |
| 25 | ADDNV | A dst, B var, C num | R[A] := N[C] + R[B] |
| 26 | SUBNV | A dst, B var, C num | R[A] := N[C] − R[B] |
| 27 | MULNV | A dst, B var, C num | R[A] := N[C] × R[B] |
| 28 | DIVNV | A dst, B var, C num | R[A] := N[C] / R[B] |
| 29 | MODNV | A dst, B var, C num | R[A] := N[C] % R[B] |
| 30 | ADDVV | A dst, B var, C var | R[A] := R[B] + R[C] |
| 31 | SUBVV | A dst, B var, C var | R[A] := R[B] − R[C] |
| 32 | MULVV | A dst, B var, C var | R[A] := R[B] × R[C] |
| 33 | DIVVV | A dst, B var, C var | R[A] := R[B] / R[C] |
| 34 | MODVV | A dst, B var, C var | R[A] := R[B] % R[C] |
| 35 | POW | A dst, B var, C var | R[A] := R[B] ^ R[C] |
| 36 | CAT | A dst, B base, C base | R[A] := R[B] .. … .. R[C] |
| 37 | KSTR | A dst, D str | R[A] := K[D] |
| 38 | KCDATA | A dst, D cdata | R[A] := K[D], an FFI value |
| 39 | KSHORT | A dst, D signed literal | R[A] := D as a signed 16-bit integer |
| 40 | KNUM | A dst, D num | R[A] := N[D] |
| 41 | KPRI | A dst, D pri | R[A] := primitive D |
| 42 | KNIL | A base, D base | R[A], …, R[D] := nil |
| 43 | UGET | A dst, D uv | R[A] := U[D] |
| 44 | USETV | A uv, D var | U[A] := R[D] |
| 45 | USETS | A uv, D str | U[A] := K[D] |
| 46 | USETN | A uv, D num | U[A] := N[D] |
| 47 | USETP | A uv, D pri | U[A] := primitive D |
| 48 | UCLO | A base, D jump | Close the upvalues at slot A and above, then jump |
| 49 | FNEW | A dst, D func | R[A] := closure of the function constant K[D] |
| 50 | TNEW | A dst, D lit | R[A] := new table. The low 11 bits of D are the array size (0x7FF means 2049), the high 5 bits the base-2 logarithm of the hash size |
| 51 | TDUP | A dst, D tab | R[A] := copy of the template table K[D] |
| 52 | GGET | A dst, D str | R[A] := global K[D] |
| 53 | GSET | A var, D str | global K[D] := R[A] |
| 54 | TGETV | A dst, B var, C var | R[A] := R[B][R[C]] |
| 55 | TGETS | A dst, B var, C str | R[A] := R[B][K[C]] |
| 56 | TGETB | A dst, B var, C lit | R[A] := R[B][C] |
| 57 | TSETV | A var, B var, C var | R[B][R[C]] := R[A] |
| 58 | TSETS | A var, B var, C str | R[B][K[C]] := R[A] |
| 59 | TSETB | A var, B var, C lit | R[B][C] := R[A] |
| 60 | TSETM | A base, D num | Store R[A] and the further MULTRES values into the table in R[A−1], from the index that N[D] holds (252 + index) |
| 61 | CALLM | A base, B lit, C lit | Call R[A] with C fixed arguments plus MULTRES; B as for CALL |
| 62 | CALL | A base, B lit, C lit | Call R[A] with the C − 1 arguments from R[A+1] on and keep B − 1 results from R[A] on; B = 0 keeps all |
| 63 | CALLMT | A base, D lit | Tail call R[A] with D fixed arguments plus MULTRES |
| 64 | CALLT | A base, D lit | Tail call R[A] with D − 1 arguments |
| 65 | ITERC | A base, B lit, C lit | Call the iterator in R[A−3] with R[A−2] and R[A−1], results from R[A] on |
| 66 | ITERN | A base, B lit, C lit | ITERC specialised for next over a table |
| 67 | VARG | A base, B lit, C lit | Copy variable arguments to R[A] on: B − 1 of them, or all when B = 0. C is the number of fixed parameters |
| 68 | ISNEXT | A base, D jump | Check that the loop over R[A−3] to R[A−1] can use ITERN, and jump to it; otherwise turn the pair back into JMP and ITERC |
| 69 | RETM | A base, D lit | Return D fixed values from R[A] on, plus MULTRES |
| 70 | RET | A base, D lit | Return R[A], …, R[A+D−2] |
| 71 | RET0 | A base, D lit | Return nothing |
| 72 | RET1 | A base, D lit | Return R[A] |
| 73 | FORI | A base, D jump | Start a numeric loop (R[A] index, R[A+1] limit, R[A+2] step, R[A+3] the visible variable); jump past it when it runs no times |
| 74 | JFORI | A base, D jump | FORI as patched by the JIT compiler; never in a dump |
| 75 | FORL | A base, D jump | Step the numeric loop and jump back while it continues |
| 76 | IFORL | A base, D jump | Never in a dump (JIT) |
| 77 | JFORL | A base, D lit | Never in a dump (JIT) |
| 78 | ITERL | A base, D jump | If R[A] is not nil: R[A−1] := R[A] and jump back |
| 79 | IITERL | A base, D jump | Never in a dump (JIT) |
| 80 | JITERL | A base, D lit | Never in a dump (JIT) |
| 81 | LOOP | A base, D jump | Marks the head of a loop for the JIT compiler; D leads to its end. Does not jump |
| 82 | ILOOP | A base, D jump | Never in a dump (JIT) |
| 83 | JLOOP | A base, D lit | Never in a dump (JIT) |
| 84 | JMP | A base, D jump | Jump; A is the first slot not in use |
| 85 | FUNCF | A base | Header of a function with fixed arguments; never stored |
| 86 | IFUNCF | A base | Never in a dump (JIT) |
| 87 | JFUNCF | A base, D lit | Never in a dump (JIT) |
| 88 | FUNCV | A base | Header of a vararg function; never stored |
| 89 | IFUNCV | A base | Never in a dump (JIT) |
| 90 | JFUNCV | A base, D lit | Never in a dump (JIT) |
| 91 | FUNCC | A base | Header of a C function; never in a dump |
| 92 | FUNCCW | A base | Header of a wrapped C function; never in a dump |
The opcode numbers are the same from 2.0.0-beta8 to the end of the branch. When the JIT compiler has patched a running function, the writer puts the plain instructions back before it dumps them.
What the loader rejects
- Header: a wrong signature or version byte, an unknown flag bit, or
BCDUMP_F_FFIin a build without the FFI. A dump of the other byte order is accepted: the loader swaps its instructions, upvalue references and line table. - Prototype length: a prototype that does not use exactly the number of bytes its length gives.
- Nesting: a nested function constant when no prototype is waiting, and an end of the dump that does not leave exactly one prototype.
- Truncation: input that ends early.
- Code: the instructions are not checked. LuaJIT warns that loading bytecode from an untrusted source is unsafe.
Stripped and wrapped dumps
luajit -b strips by default (-g keeps the debug information);
string.dump(f, true) strips too. A stripped dump has the strip flag, no chunk
name and no debuglen field.
luajit -b can also wrap a dump in a C header or source file, or in an object
file, under a symbol that starts with luaJIT_BC_. The raw output is the dump
described here.
Sources
The LuaJIT source repository, branch
v2.0 (commit d7b8df8b) and the releases 2.0.0-beta8 and 2.0.5:
lj_bcdump.h (grammar, flags and type codes), lj_bcread.c,
lj_bcwrite.c, lj_bc.h (instruction format and opcodes),
lj_obj.h (prototype and upvalue flags), lj_debug.c and
lj_debug.h (line table and variable names), lj_parse.c (table
sizes and TSETM) and src/jit/bc.lua and
src/jit/bcsave.lua (listing and saving). The dates of the changes come from
the commit history.
Running LuaJIT describes the commands.