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 2, written by LuaJIT 2.1:
its beta releases and the rolling releases that follow from the same branch. LuaJIT 2.0
writes dump version 1. 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 | 02. 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. |
0x08 | BCDUMP_F_FR2 | The bytecode is for the two-slot frame layout of 64-bit (LJ_GC64) builds. Added in January 2015. |
0x10 | BCDUMP_F_BITOP | The dump uses the seven bit operator opcodes, which changes the opcode numbers after JMP. Added in July 2026. |
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. |
0x80 | PROTO_BITOP | Uses bit operator opcodes (since July 2026). |
The writer stores only these bits. The others in lj_obj.h (PROTO_NOJIT,
PROTO_ILOOP and the counters) 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 nil value in the hash part stands for a key that is in the constructor but whose value the code assigns when it runs. 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. A deterministic dump (below) sorts them: by type, strings by their bytes and numbers by their 64-bit pattern.
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.
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. |
| 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 | Op with bit operators | Name | Operands | Action |
|---|---|---|---|---|
| 0 | 0 | ISLT | A var, D var | R[A] < R[D] |
| 1 | 1 | ISGE | A var, D var | not R[A] < R[D] |
| 2 | 2 | ISLE | A var, D var | R[A] <= R[D] |
| 3 | 3 | ISGT | A var, D var | not R[A] <= R[D] |
| 4 | 4 | ISEQV | A var, D var | R[A] == R[D] |
| 5 | 5 | ISNEV | A var, D var | R[A] ~= R[D] |
| 6 | 6 | ISEQS | A var, D str | R[A] == K[D] |
| 7 | 7 | ISNES | A var, D str | R[A] ~= K[D] |
| 8 | 8 | ISEQN | A var, D num | R[A] == N[D] |
| 9 | 9 | ISNEN | A var, D num | R[A] ~= N[D] |
| 10 | 10 | ISEQP | A var, D pri | R[A] == primitive D |
| 11 | 11 | ISNEP | A var, D pri | R[A] ~= primitive D |
| 12 | 12 | ISTC | A dst, D var | If R[D] is true: R[A] := R[D] and jump |
| 13 | 13 | ISFC | A dst, D var | If R[D] is false or nil: R[A] := R[D] and jump |
| 14 | 14 | IST | D var | R[D] is true |
| 15 | 15 | ISF | D var | R[D] is false or nil |
| 16 | 16 | ISTYPE | A var, D lit | Assert that R[A] has the type that the code D names; used inside the library |
| 17 | 17 | ISNUM | A var, D lit | Assert that R[A] is a number; used inside the library |
| 18 | 18 | MOV | A dst, D var | R[A] := R[D] |
| 19 | 19 | NOT | A dst, D var | R[A] := not R[D] |
| 20 | 20 | UNM | A dst, D var | R[A] := −R[D] |
| 21 | 21 | LEN | A dst, D var | R[A] := #R[D] |
| 22 | 22 | ADDVN | A dst, B var, C num | R[A] := R[B] + N[C] |
| 23 | 23 | SUBVN | A dst, B var, C num | R[A] := R[B] − N[C] |
| 24 | 24 | MULVN | A dst, B var, C num | R[A] := R[B] × N[C] |
| 25 | 25 | DIVVN | A dst, B var, C num | R[A] := R[B] / N[C] |
| 26 | 26 | MODVN | A dst, B var, C num | R[A] := R[B] % N[C] |
| 27 | 27 | ADDNV | A dst, B var, C num | R[A] := N[C] + R[B] |
| 28 | 28 | SUBNV | A dst, B var, C num | R[A] := N[C] − R[B] |
| 29 | 29 | MULNV | A dst, B var, C num | R[A] := N[C] × R[B] |
| 30 | 30 | DIVNV | A dst, B var, C num | R[A] := N[C] / R[B] |
| 31 | 31 | MODNV | A dst, B var, C num | R[A] := N[C] % R[B] |
| 32 | 32 | ADDVV | A dst, B var, C var | R[A] := R[B] + R[C] |
| 33 | 33 | SUBVV | A dst, B var, C var | R[A] := R[B] − R[C] |
| 34 | 34 | MULVV | A dst, B var, C var | R[A] := R[B] × R[C] |
| 35 | 35 | DIVVV | A dst, B var, C var | R[A] := R[B] / R[C] |
| 36 | 36 | MODVV | A dst, B var, C var | R[A] := R[B] % R[C] |
| 37 | 37 | POW | A dst, B var, C var | R[A] := R[B] ^ R[C] |
| 38 | 38 | CAT | A dst, B base, C base | R[A] := R[B] .. … .. R[C] |
| 39 | 39 | KSTR | A dst, D str | R[A] := K[D] |
| 40 | 40 | KCDATA | A dst, D cdata | R[A] := K[D], an FFI value |
| 41 | 41 | KSHORT | A dst, D signed literal | R[A] := D as a signed 16-bit integer |
| 42 | 42 | KNUM | A dst, D num | R[A] := N[D] |
| 43 | 43 | KPRI | A dst, D pri | R[A] := primitive D |
| 44 | 44 | KNIL | A base, D base | R[A], …, R[D] := nil |
| 45 | 45 | UGET | A dst, D uv | R[A] := U[D] |
| 46 | 46 | USETV | A uv, D var | U[A] := R[D] |
| 47 | 47 | USETS | A uv, D str | U[A] := K[D] |
| 48 | 48 | USETN | A uv, D num | U[A] := N[D] |
| 49 | 49 | USETP | A uv, D pri | U[A] := primitive D |
| 50 | 50 | UCLO | A base, D jump | Close the upvalues at slot A and above, then jump |
| 51 | 51 | FNEW | A dst, D func | R[A] := closure of the function constant K[D] |
| 52 | 52 | 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 |
| 53 | 53 | TDUP | A dst, D tab | R[A] := copy of the template table K[D] |
| 54 | 54 | GGET | A dst, D str | R[A] := global K[D] |
| 55 | 55 | GSET | A var, D str | global K[D] := R[A] |
| 56 | 56 | TGETV | A dst, B var, C var | R[A] := R[B][R[C]] |
| 57 | 57 | TGETS | A dst, B var, C str | R[A] := R[B][K[C]] |
| 58 | 58 | TGETB | A dst, B var, C lit | R[A] := R[B][C] |
| 59 | 59 | TGETR | A dst, B var, C var | R[A] := R[B][R[C]] without metamethods; used inside the library |
| 60 | 60 | TSETV | A var, B var, C var | R[B][R[C]] := R[A] |
| 61 | 61 | TSETS | A var, B var, C str | R[B][K[C]] := R[A] |
| 62 | 62 | TSETB | A var, B var, C lit | R[B][C] := R[A] |
| 63 | 63 | 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) |
| 64 | 64 | TSETR | A var, B var, C var | R[B][R[C]] := R[A] without metamethods; used inside the library |
| 65 | 65 | CALLM | A base, B lit, C lit | Call R[A] with C fixed arguments plus MULTRES; B as for CALL |
| 66 | 66 | CALL | A base, B lit, C lit | Call R[A] with C − 1 arguments and keep B − 1 results from R[A] on; B = 0 keeps all. In the two-slot layout the arguments start at R[A+2], otherwise at R[A+1] |
| 67 | 67 | CALLMT | A base, D lit | Tail call R[A] with D fixed arguments plus MULTRES |
| 68 | 68 | CALLT | A base, D lit | Tail call R[A] with D − 1 arguments |
| 69 | 69 | 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 |
| 70 | 70 | ITERN | A base, B lit, C lit | ITERC specialised for next over a table |
| 71 | 71 | 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 |
| 72 | 72 | 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 |
| 73 | 73 | RETM | A base, D lit | Return D fixed values from R[A] on, plus MULTRES |
| 74 | 74 | RET | A base, D lit | Return R[A], …, R[A+D−2] |
| 75 | 75 | RET0 | A base, D lit | Return nothing |
| 76 | 76 | RET1 | A base, D lit | Return R[A] |
| 77 | 77 | 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 |
| 78 | 78 | JFORI | A base, D jump | FORI as patched by the JIT compiler; never in a dump |
| 79 | 79 | FORL | A base, D jump | Step the numeric loop and jump back while it continues |
| 80 | 80 | IFORL | A base, D jump | Never in a dump (JIT) |
| 81 | 81 | JFORL | A base, D lit | Never in a dump (JIT) |
| 82 | 82 | ITERL | A base, D jump | If R[A] is not nil: R[A−1] := R[A] and jump back |
| 83 | 83 | IITERL | A base, D jump | Never in a dump (JIT) |
| 84 | 84 | JITERL | A base, D lit | Never in a dump (JIT) |
| 85 | 85 | LOOP | A base, D jump | Marks the head of a loop for the JIT compiler; D leads to its end. Does not jump |
| 86 | 86 | ILOOP | A base, D jump | Never in a dump (JIT) |
| 87 | 87 | JLOOP | A base, D lit | Never in a dump (JIT) |
| 88 | 88 | JMP | A base, D jump | Jump; A is the first slot not in use |
| 89 | BNOT | A dst, D var | R[A] := ~R[D] | |
| 90 | BAND | A dst, B var, C var | R[A] := R[B] & R[C] | |
| 91 | BOR | A dst, B var, C var | R[A] := R[B] | R[C] | |
| 92 | BXOR | A dst, B var, C var | R[A] := R[B] ~ R[C] | |
| 93 | BSHL | A dst, B var, C var | R[A] := R[B] << R[C] | |
| 94 | BSHR | A dst, B var, C var | R[A] := R[B] >> R[C] | |
| 95 | BSAR | A dst, B var, C var | R[A] := R[B] ~>> R[C], the arithmetic shift | |
| 89 | 96 | FUNCF | A base | Header of a function with fixed arguments; never stored |
| 90 | 97 | IFUNCF | A base | Never in a dump (JIT) |
| 91 | 98 | JFUNCF | A base, D lit | Never in a dump (JIT) |
| 92 | 99 | FUNCV | A base | Header of a vararg function; never stored |
| 93 | 100 | IFUNCV | A base | Never in a dump (JIT) |
| 94 | 101 | JFUNCV | A base, D lit | Never in a dump (JIT) |
| 95 | 102 | FUNCC | A base | Header of a C function; never in a dump |
| 96 | 103 | FUNCCW | A base | Header of a wrapped C function; never in a dump |
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,
BCDUMP_F_FFIin a build without the FFI, or a frame layout flag that does not match what the load asked for. - Byte order is not a reason: the loader swaps the instructions, upvalue references and line table of a dump from the other kind of machine.
- 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.
A file that starts with a byte order mark or a # line is not loaded as
bytecode: LuaJIT refuses a dump that follows one.
Builds, frame layouts and deterministic dumps
64-bit builds with LJ_GC64 keep two stack slots per call frame (the FR2
layout); 32-bit builds and older 64-bit x86 builds keep one. Call arguments therefore sit
in different slots, and a dump runs only on a build with the same layout. Since January
2024 a build can compile and load for the other layout, which allows cross compiling
(luajit -bW for 32-bit bytecode, -bX for 64-bit); such a
function can be dumped again but not run.
Since the same change a dump can be deterministic (luajit -bd, or
string.dump with the mode letter d): the entries of template
tables are written in a fixed order. The request is not recorded in the file.
Stripped dumps
luajit -b strips by default (-g keeps the debug information);
string.dump(f, true) or the mode letter s 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.
Changes within version 2
| Date | Change |
|---|---|
| 2013-02-23 | Version byte 2 on the 2.1 branch, with the opcodes ISTYPE, ISNUM, TGETR and TSETR. LuaJIT 2.1.0-beta1 followed in 2015. |
| 2015-01-03 | Flag BCDUMP_F_FR2 for the two-slot frame layout. |
| 2024-01-22 | Loading for the other frame layout, and deterministic dumps. Template tables keep keys whose values are set at run time. |
| 2026-07-20 | Bit operator syntax, flag BCDUMP_F_BITOP, prototype flag PROTO_BITOP and seven opcodes. The version byte did not change: a dump without the flag keeps the earlier opcode numbers. |
The version byte stayed 2 through these changes, so only the flags tell such dumps apart. A dump written by a build between the changes reads by the same rules. Bytecode that uses a bit operator loads only in LuaJIT 2.1.1784535649 or later.
Sources
The LuaJIT source repository, branch
v2.1 (commit b82d612f) and its history: 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_load.c and lj_lex.c (loading),
lj_parse.c (table sizes and TSETM), vm_arm64.dasc
(what instructions do) and src/jit/bc.lua and src/jit/bcsave.lua
(listing and saving).
Running LuaJIT and
string.dump in LuaJIT’s
extensions describe the commands and note that bytecode is not checked when
loaded.