opcode

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package api defines Lua 5.5.1 VM instruction encoding and all 85 opcodes.

Every instruction is a uint32 with a 7-bit opcode and operands in one of six formats: iABC, ivABC, iABx, iAsBx, iAx, isJ.

Reference: .analysis/02-opcodes-instruction-format.md

Opcode metadata tables — populated from lua-master/lopcodes.c and lopnames.h.

OpModes byte layout: bit7=mm, bit6=ot, bit5=it, bit4=t, bit3=a, bits[2:0]=format Format values: iABC=0, ivABC=1, iABx=2, iAsBx=3, iAx=4, isJ=5

C macro: opmode(mm, ot, it, t, a, m) = (mm<<7)|(ot<<6)|(it<<5)|(t<<4)|(a<<3)|m

Index

Constants

View Source
const (
	SizeOP = 7
	SizeA  = 8
	SizeB  = 8
	SizeC  = 8
	SizeBx = SizeC + SizeB + 1 // 17
	SizeAx = SizeBx + SizeA    // 25
	SizeSJ = SizeBx + SizeA    // 25
	SizeVB = 6
	SizeVC = 10
)

Field sizes (bits)

View Source
const (
	PosOP = 0
	PosA  = PosOP + SizeOP // 7
	PosK  = PosA + SizeA   // 15
	PosB  = PosK + 1       // 16
	PosC  = PosB + SizeB   // 24
	PosBx = PosK           // 15
	PosAx = PosA           // 7
	PosSJ = PosA           // 7
	PosVB = PosK + 1       // 16
	PosVC = PosVB + SizeVB // 22
)

Field positions (bit offset from LSB)

View Source
const (
	MaxArgA  = (1 << SizeA) - 1  // 255
	MaxArgB  = (1 << SizeB) - 1  // 255
	MaxArgC  = (1 << SizeC) - 1  // 255
	MaxArgBx = (1 << SizeBx) - 1 // 131071
	MaxArgAx = (1 << SizeAx) - 1 // 33554431
	MaxArgSJ = (1 << SizeSJ) - 1 // 33554431
	MaxArgVB = (1 << SizeVB) - 1 // 63
	MaxArgVC = (1 << SizeVC) - 1 // 1023
)

Operand limits

View Source
const (
	OffsetSBx = MaxArgBx >> 1 // 65535
	OffsetSJ  = MaxArgSJ >> 1 // 16777215
	OffsetSC  = MaxArgC >> 1  // 127
)

Signed encoding offsets (excess-K bias)

View Source
const NoReg = MaxArgA // 255

NoReg is the invalid register sentinel.

Variables

View Source
var OpModes [NumOpcodes]byte

OpModes holds the mode flags for each opcode. Populated by init() in the implementation file.

OpNames holds the human-readable name for each opcode. Populated by init() in the implementation file.

Functions

func GetArgA

func GetArgA(i Instruction) int

GetArgA extracts the A operand.

func GetArgAx

func GetArgAx(i Instruction) int

GetArgAx extracts the Ax operand (unsigned).

func GetArgB

func GetArgB(i Instruction) int

GetArgB extracts the B operand.

func GetArgBx

func GetArgBx(i Instruction) int

GetArgBx extracts the Bx operand (unsigned).

func GetArgC

func GetArgC(i Instruction) int

GetArgC extracts the C operand.

func GetArgK

func GetArgK(i Instruction) int

GetArgK extracts the k bit.

func GetArgSB

func GetArgSB(i Instruction) int

GetArgSB extracts the signed sB operand.

func GetArgSBx

func GetArgSBx(i Instruction) int

GetArgSBx extracts the signed sBx operand.

func GetArgSC

func GetArgSC(i Instruction) int

GetArgSC extracts the signed sC operand.

func GetArgSJ

func GetArgSJ(i Instruction) int

GetArgSJ extracts the signed sJ operand.

func GetArgVB

func GetArgVB(i Instruction) int

GetArgVB extracts the vB operand (6-bit).

func GetArgVC

func GetArgVC(i Instruction) int

GetArgVC extracts the vC operand (10-bit).

func OpName

func OpName(op OpCode) string

OpName returns the human-readable name of an opcode.

func TestAMode

func TestAMode(op OpCode) bool

TestAMode returns true if the opcode sets register A.

func TestITMode

func TestITMode(op OpCode) bool

TestITMode returns true if the opcode uses L->top from the previous instruction (B==0).

func TestMMMode

func TestMMMode(op OpCode) bool

TestMMMode returns true if the opcode is a metamethod fallback instruction.

func TestOTMode

func TestOTMode(op OpCode) bool

TestOTMode returns true if the opcode sets L->top for the next instruction (C==0).

func TestTMode

func TestTMode(op OpCode) bool

TestTMode returns true if the opcode is a test (next instruction is a jump).

Types

type Instruction

type Instruction = uint32

Instruction is a 32-bit encoded Lua VM instruction.

func CreateABCK

func CreateABCK(op OpCode, a, b, c, k int) Instruction

CreateABCK creates an iABC format instruction.

func CreateABx

func CreateABx(op OpCode, a, bx int) Instruction

CreateABx creates an iABx format instruction.

func CreateAsBx

func CreateAsBx(op OpCode, a, sbx int) Instruction

CreateAsBx creates an iAsBx format instruction (signed Bx).

func CreateAx

func CreateAx(op OpCode, ax int) Instruction

CreateAx creates an iAx format instruction.

func CreateSJ

func CreateSJ(op OpCode, sj int) Instruction

CreateSJ creates an isJ format instruction (signed jump).

func CreateSJK

func CreateSJK(op OpCode, sj, k int) Instruction

CreateSJK creates an isJ format instruction with a k bit. Used by codesJ in codegen when the sJ format needs a k flag.

func CreateVABCK

func CreateVABCK(op OpCode, a, vb, vc, k int) Instruction

CreateVABCK creates an ivABC format instruction.

func SetArgA

func SetArgA(i Instruction, v int) Instruction

SetArgA replaces the A field in an existing instruction.

func SetArgB

func SetArgB(i Instruction, v int) Instruction

SetArgB replaces the B field in an existing instruction.

func SetArgBx

func SetArgBx(i Instruction, v int) Instruction

SetArgBx replaces the unsigned Bx field in an existing instruction.

func SetArgC

func SetArgC(i Instruction, v int) Instruction

SetArgC replaces the C field in an existing instruction.

func SetArgK

func SetArgK(i Instruction, v int) Instruction

SetArgK replaces the k bit in an existing instruction.

func SetArgSBx

func SetArgSBx(i Instruction, v int) Instruction

SetArgSBx replaces the sBx field in an existing instruction.

func SetArgSJ

func SetArgSJ(i Instruction, v int) Instruction

SetArgSJ replaces the sJ field in an existing instruction.

func SetOpCode

func SetOpCode(i Instruction, op OpCode) Instruction

SetOpCode replaces the opcode field in an existing instruction.

type OpCode

type OpCode = uint8

OpCode is the type for Lua VM opcodes.

const (
	OP_MOVE       OpCode = 0
	OP_LOADI      OpCode = 1
	OP_LOADF      OpCode = 2
	OP_LOADK      OpCode = 3
	OP_LOADKX     OpCode = 4
	OP_LOADFALSE  OpCode = 5
	OP_LFALSESKIP OpCode = 6
	OP_LOADTRUE   OpCode = 7
	OP_LOADNIL    OpCode = 8
	OP_GETUPVAL   OpCode = 9
	OP_SETUPVAL   OpCode = 10
	OP_GETTABUP   OpCode = 11
	OP_GETTABLE   OpCode = 12
	OP_GETI       OpCode = 13
	OP_GETFIELD   OpCode = 14
	OP_SETTABUP   OpCode = 15
	OP_SETTABLE   OpCode = 16
	OP_SETI       OpCode = 17
	OP_SETFIELD   OpCode = 18
	OP_NEWTABLE   OpCode = 19
	OP_SELF       OpCode = 20
	OP_ADDI       OpCode = 21
	OP_ADDK       OpCode = 22
	OP_SUBK       OpCode = 23
	OP_MULK       OpCode = 24
	OP_MODK       OpCode = 25
	OP_POWK       OpCode = 26
	OP_DIVK       OpCode = 27
	OP_IDIVK      OpCode = 28
	OP_BANDK      OpCode = 29
	OP_BORK       OpCode = 30
	OP_BXORK      OpCode = 31
	OP_SHLI       OpCode = 32
	OP_SHRI       OpCode = 33
	OP_ADD        OpCode = 34
	OP_SUB        OpCode = 35
	OP_MUL        OpCode = 36
	OP_MOD        OpCode = 37
	OP_POW        OpCode = 38
	OP_DIV        OpCode = 39
	OP_IDIV       OpCode = 40
	OP_BAND       OpCode = 41
	OP_BOR        OpCode = 42
	OP_BXOR       OpCode = 43
	OP_SHL        OpCode = 44
	OP_SHR        OpCode = 45
	OP_MMBIN      OpCode = 46
	OP_MMBINI     OpCode = 47
	OP_MMBINK     OpCode = 48
	OP_UNM        OpCode = 49
	OP_BNOT       OpCode = 50
	OP_NOT        OpCode = 51
	OP_LEN        OpCode = 52
	OP_CONCAT     OpCode = 53
	OP_CLOSE      OpCode = 54
	OP_TBC        OpCode = 55
	OP_JMP        OpCode = 56
	OP_EQ         OpCode = 57
	OP_LT         OpCode = 58
	OP_LE         OpCode = 59
	OP_EQK        OpCode = 60
	OP_EQI        OpCode = 61
	OP_LTI        OpCode = 62
	OP_LEI        OpCode = 63
	OP_GTI        OpCode = 64
	OP_GEI        OpCode = 65
	OP_TEST       OpCode = 66
	OP_TESTSET    OpCode = 67
	OP_CALL       OpCode = 68
	OP_TAILCALL   OpCode = 69
	OP_RETURN     OpCode = 70
	OP_RETURN0    OpCode = 71
	OP_RETURN1    OpCode = 72
	OP_FORLOOP    OpCode = 73
	OP_FORPREP    OpCode = 74
	OP_TFORPREP   OpCode = 75
	OP_TFORCALL   OpCode = 76
	OP_TFORLOOP   OpCode = 77
	OP_SETLIST    OpCode = 78
	OP_CLOSURE    OpCode = 79
	OP_VARARG     OpCode = 80
	OP_GETVARG    OpCode = 81
	OP_ERRNNIL    OpCode = 82
	OP_VARARGPREP OpCode = 83
	OP_EXTRAARG   OpCode = 84

	NumOpcodes = 85
)

func GetOpCode

func GetOpCode(i Instruction) OpCode

GetOpCode extracts the opcode from an instruction.

type OpMode

type OpMode byte

OpMode identifies the operand encoding format.

const (
	IABC  OpMode = 0 // iABC:  A(8) k(1) B(8) C(8)
	IVABC OpMode = 1 // ivABC: A(8) k(1) vB(6) vC(10)
	IABx  OpMode = 2 // iABx:  A(8) Bx(17)
	IAsBx OpMode = 3 // iAsBx: A(8) sBx(17 signed)
	IAx   OpMode = 4 // iAx:   Ax(25)
	ISJ   OpMode = 5 // isJ:   sJ(25 signed)
)

func GetMode

func GetMode(op OpCode) OpMode

GetMode returns the instruction format for the given opcode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL