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
- Variables
- func GetArgA(i Instruction) int
- func GetArgAx(i Instruction) int
- func GetArgB(i Instruction) int
- func GetArgBx(i Instruction) int
- func GetArgC(i Instruction) int
- func GetArgK(i Instruction) int
- func GetArgSB(i Instruction) int
- func GetArgSBx(i Instruction) int
- func GetArgSC(i Instruction) int
- func GetArgSJ(i Instruction) int
- func GetArgVB(i Instruction) int
- func GetArgVC(i Instruction) int
- func OpName(op OpCode) string
- func TestAMode(op OpCode) bool
- func TestITMode(op OpCode) bool
- func TestMMMode(op OpCode) bool
- func TestOTMode(op OpCode) bool
- func TestTMode(op OpCode) bool
- type Instruction
- func CreateABCK(op OpCode, a, b, c, k int) Instruction
- func CreateABx(op OpCode, a, bx int) Instruction
- func CreateAsBx(op OpCode, a, sbx int) Instruction
- func CreateAx(op OpCode, ax int) Instruction
- func CreateSJ(op OpCode, sj int) Instruction
- func CreateSJK(op OpCode, sj, k int) Instruction
- func CreateVABCK(op OpCode, a, vb, vc, k int) Instruction
- func SetArgA(i Instruction, v int) Instruction
- func SetArgB(i Instruction, v int) Instruction
- func SetArgBx(i Instruction, v int) Instruction
- func SetArgC(i Instruction, v int) Instruction
- func SetArgK(i Instruction, v int) Instruction
- func SetArgSBx(i Instruction, v int) Instruction
- func SetArgSJ(i Instruction, v int) Instruction
- func SetOpCode(i Instruction, op OpCode) Instruction
- type OpCode
- type OpMode
Constants ¶
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)
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)
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
const ( OffsetSBx = MaxArgBx >> 1 // 65535 OffsetSJ = MaxArgSJ >> 1 // 16777215 OffsetSC = MaxArgC >> 1 // 127 )
Signed encoding offsets (excess-K bias)
const NoReg = MaxArgA // 255
NoReg is the invalid register sentinel.
Variables ¶
var OpModes [NumOpcodes]byte
OpModes holds the mode flags for each opcode. Populated by init() in the implementation file.
var OpNames [NumOpcodes]string
OpNames holds the human-readable name for each opcode. Populated by init() in the implementation file.
Functions ¶
func TestITMode ¶
TestITMode returns true if the opcode uses L->top from the previous instruction (B==0).
func TestMMMode ¶
TestMMMode returns true if the opcode is a metamethod fallback instruction.
func TestOTMode ¶
TestOTMode returns true if the opcode sets L->top for the next instruction (C==0).
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.