Path: blob/main/sys/contrib/openzfs/module/lua/lopcodes.h
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lopcodes.h,v 1.142.1.2 2014/10/20 18:32:09 roberto Exp $3** Opcodes for Lua virtual machine4** See Copyright Notice in lua.h5*/67#ifndef lopcodes_h8#define lopcodes_h910#include "llimits.h"111213/*===========================================================================14We assume that instructions are unsigned numbers.15All instructions have an opcode in the first 6 bits.16Instructions can have the following fields:17`A' : 8 bits18`B' : 9 bits19`C' : 9 bits20'Ax' : 26 bits ('A', 'B', and 'C' together)21`Bx' : 18 bits (`B' and `C' together)22`sBx' : signed Bx2324A signed argument is represented in excess K; that is, the number25value is the unsigned value minus K. K is exactly the maximum value26for that argument (so that -max is represented by 0, and +max is27represented by 2*max), which is half the maximum for the corresponding28unsigned argument.29===========================================================================*/303132enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */333435/*36** size and position of opcode arguments.37*/38#define SIZE_C 939#define SIZE_B 940#define SIZE_Bx (SIZE_C + SIZE_B)41#define SIZE_A 842#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)4344#define SIZE_OP 64546#define POS_OP 047#define POS_A (POS_OP + SIZE_OP)48#define POS_C (POS_A + SIZE_A)49#define POS_B (POS_C + SIZE_C)50#define POS_Bx POS_C51#define POS_Ax POS_A525354/*55** limits for opcode arguments.56** we use (signed) int to manipulate most arguments,57** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)58*/59#if SIZE_Bx < LUAI_BITSINT-160#define MAXARG_Bx ((1<<SIZE_Bx)-1)61#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */62#else63#define MAXARG_Bx MAX_INT64#define MAXARG_sBx MAX_INT65#endif6667#if SIZE_Ax < LUAI_BITSINT-168#define MAXARG_Ax ((1<<SIZE_Ax)-1)69#else70#define MAXARG_Ax MAX_INT71#endif727374#define MAXARG_A ((1<<SIZE_A)-1)75#define MAXARG_B ((1<<SIZE_B)-1)76#define MAXARG_C ((1<<SIZE_C)-1)777879/* creates a mask with `n' 1 bits at position `p' */80#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))8182/* creates a mask with `n' 0 bits at position `p' */83#define MASK0(n,p) (~MASK1(n,p))8485/*86** the following macros help to manipulate instructions87*/8889#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))90#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \91((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))9293#define getarg(i,pos,size) (cast(int, ((i)>>pos) & MASK1(size,0)))94#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \95((cast(Instruction, v)<<pos)&MASK1(size,pos))))9697#define GETARG_A(i) getarg(i, POS_A, SIZE_A)98#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)99100#define GETARG_B(i) getarg(i, POS_B, SIZE_B)101#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)102103#define GETARG_C(i) getarg(i, POS_C, SIZE_C)104#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)105106#define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)107#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)108109#define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)110#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)111112#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)113#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))114115116#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \117| (cast(Instruction, a)<<POS_A) \118| (cast(Instruction, b)<<POS_B) \119| (cast(Instruction, c)<<POS_C))120121#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \122| (cast(Instruction, a)<<POS_A) \123| (cast(Instruction, bc)<<POS_Bx))124125#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \126| (cast(Instruction, a)<<POS_Ax))127128129/*130** Macros to operate RK indices131*/132133/* this bit 1 means constant (0 means register) */134#define BITRK (1 << (SIZE_B - 1))135136/* test whether value is a constant */137#define ISK(x) ((x) & BITRK)138139/* gets the index of the constant */140#define INDEXK(r) ((int)(r) & ~BITRK)141142#define MAXINDEXRK (BITRK - 1)143144/* code a constant index as a RK value */145#define RKASK(x) ((x) | BITRK)146147148/*149** invalid register that fits in 8 bits150*/151#define NO_REG MAXARG_A152153154/*155** R(x) - register156** Kst(x) - constant (in constant table)157** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)158*/159160161/*162** grep "ORDER OP" if you change these enums163*/164165typedef enum {166/*----------------------------------------------------------------------167name args description168------------------------------------------------------------------------*/169OP_MOVE,/* A B R(A) := R(B) */170OP_LOADK,/* A Bx R(A) := Kst(Bx) */171OP_LOADKX,/* A R(A) := Kst(extra arg) */172OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */173OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */174OP_GETUPVAL,/* A B R(A) := UpValue[B] */175176OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */177OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */178179OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */180OP_SETUPVAL,/* A B UpValue[B] := R(A) */181OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */182183OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */184185OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */186187OP_ADD,/* A B C R(A) := RK(B) + RK(C) */188OP_SUB,/* A B C R(A) := RK(B) - RK(C) */189OP_MUL,/* A B C R(A) := RK(B) * RK(C) */190OP_DIV,/* A B C R(A) := RK(B) / RK(C) */191OP_MOD,/* A B C R(A) := RK(B) % RK(C) */192OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */193OP_UNM,/* A B R(A) := -R(B) */194OP_NOT,/* A B R(A) := not R(B) */195OP_LEN,/* A B R(A) := length of R(B) */196197OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */198199OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */200OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */201OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */202OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */203204OP_TEST,/* A C if not (R(A) <=> C) then pc++ */205OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */206207OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */208OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */209OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */210211OP_FORLOOP,/* A sBx R(A)+=R(A+2);212if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/213OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */214215OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */216OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/217218OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */219220OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */221222OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */223224OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */225} OpCode;226227228#define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)229230231232/*===========================================================================233Notes:234(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is235set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,236OP_SETLIST) may use `top'.237238(*) In OP_VARARG, if (B == 0) then use actual number of varargs and239set top (like in OP_CALL with C == 0).240241(*) In OP_RETURN, if (B == 0) then return up to `top'.242243(*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next244'instruction' is EXTRAARG(real C).245246(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.247248(*) For comparisons, A specifies what condition the test should accept249(true or false).250251(*) All `skips' (pc++) assume that next instruction is a jump.252253===========================================================================*/254255256/*257** masks for instruction properties. The format is:258** bits 0-1: op mode259** bits 2-3: C arg mode260** bits 4-5: B arg mode261** bit 6: instruction set register A262** bit 7: operator is a test (next instruction must be a jump)263*/264265enum OpArgMask {266OpArgN, /* argument is not used */267OpArgU, /* argument is used */268OpArgR, /* argument is a register or a jump offset */269OpArgK /* argument is a constant or register/constant */270};271272LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];273274#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))275#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))276#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))277#define testAMode(m) (luaP_opmodes[m] & (1 << 6))278#define testTMode(m) (luaP_opmodes[m] & (1 << 7))279280281LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */282283284/* number of list items to accumulate before a SETLIST instruction */285#define LFIELDS_PER_FLUSH 50286287288#endif289290291