Lua Programming Language

PaulW.XYZ / Notes / Lua Programming Language
Last updated: December 28th, 2024 at 5:23 P.M.

Lua 5.4 Bytecode

These are unstable and may differ in different versions of the language. They are not part of the language specification but an implementation detail, which in this case is the reference implementation.

The reference implementation used to have a stack based but now uses a register based VM similar to how modern real computer architectures.

The instructions are 32 bits wide; every instruction has an opcode that takes up 7 bits, which leaves out 25 bits for the addresses and values.

The instructions work with three register referred to as: A, B, C; each are of length 8 bits.

31...2423...161514...76...0
iABCC (8 bits)B (8 bits)k (1 bit)A (8 bits)OP (7 bits)
iABxB (17 bits)A (8 bits)OP (7 bits)
iAsBxsigned B 17 bits)A (8 bits)OP (7 bits)
iAxAx (25 bits)OP (7 bits)
sJsigned jump address (25 bits)OP (7 bits)
-- arithmetic to calculate the lengths used from https://www.lua.org/source/5.4/lopcodes.h.html
A = 8
B = 8
C = 8
Bx = A + B + 1  -- 17
Ax = A + Bx     -- 25
sJ = A + Bx     -- 25

This page contains excerpts from Lua's source code which is the copyright of Lua.org, PUC-Rio and is licensed under the MIT License. lua.org/license.html