www/notes/lua.md

2.2 KiB

Lua Programming Language

Lua 5.4 C API

Lua 5.4 Bytecode

Note

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.

Note

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
iABC C (8 bits) B (8 bits) k (1 bit) A (8 bits) OP (7 bits)
iABx B (17 bits) A (8 bits) OP (7 bits)
iAsBx signed B 17 bits) A (8 bits) OP (7 bits)
iAx Ax (25 bits) OP (7 bits)
sJ signed 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