Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: lopcodes.h,v 1.10 2023/06/08 21:12:08 nikita Exp $	*/
      2 
      3 /*
      4 ** Id: lopcodes.h
      5 ** Opcodes for Lua virtual machine
      6 ** See Copyright Notice in lua.h
      7 */
      8 
      9 #ifndef lopcodes_h
     10 #define lopcodes_h
     11 
     12 #include "llimits.h"
     13 
     14 
     15 /*===========================================================================
     16   We assume that instructions are unsigned 32-bit integers.
     17   All instructions have an opcode in the first 7 bits.
     18   Instructions can have the following formats:
     19 
     20         3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
     21         1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
     22 iABC          C(8)     |      B(8)     |k|     A(8)      |   Op(7)     |
     23 iABx                Bx(17)               |     A(8)      |   Op(7)     |
     24 iAsBx              sBx (signed)(17)      |     A(8)      |   Op(7)     |
     25 iAx                           Ax(25)                     |   Op(7)     |
     26 isJ                           sJ (signed)(25)            |   Op(7)     |
     27 
     28   A signed argument is represented in excess K: the represented value is
     29   the written unsigned value minus K, where K is half the maximum for the
     30   corresponding unsigned argument.
     31 ===========================================================================*/
     32 
     33 
     34 enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
     35 
     36 
     37 /*
     38 ** size and position of opcode arguments.
     39 */
     40 #define SIZE_C		8
     41 #define SIZE_B		8
     42 #define SIZE_Bx		(SIZE_C + SIZE_B + 1)
     43 #define SIZE_A		8
     44 #define SIZE_Ax		(SIZE_Bx + SIZE_A)
     45 #define SIZE_sJ		(SIZE_Bx + SIZE_A)
     46 
     47 #define SIZE_OP		7
     48 
     49 #define POS_OP		0
     50 
     51 #define POS_A		(POS_OP + SIZE_OP)
     52 #define POS_k		(POS_A + SIZE_A)
     53 #define POS_B		(POS_k + 1)
     54 #define POS_C		(POS_B + SIZE_B)
     55 
     56 #define POS_Bx		POS_k
     57 
     58 #define POS_Ax		POS_A
     59 
     60 #define POS_sJ		POS_A
     61 
     62 
     63 /*
     64 ** limits for opcode arguments.
     65 ** we use (signed) 'int' to manipulate most arguments,
     66 ** so they must fit in ints.
     67 */
     68 
     69 /* Check whether type 'int' has at least 'b' bits ('b' < 32) */
     70 #define L_INTHASBITS(b)		((UINT_MAX >> ((b) - 1)) >= 1)
     71 
     72 
     73 #if L_INTHASBITS(SIZE_Bx)
     74 #define MAXARG_Bx	((1<<SIZE_Bx)-1)
     75 #else
     76 #define MAXARG_Bx	MAX_INT
     77 #endif
     78 
     79 #define OFFSET_sBx	(MAXARG_Bx>>1)         /* 'sBx' is signed */
     80 
     81 
     82 #if L_INTHASBITS(SIZE_Ax)
     83 #define MAXARG_Ax	((1<<SIZE_Ax)-1)
     84 #else
     85 #define MAXARG_Ax	MAX_INT
     86 #endif
     87 
     88 #if L_INTHASBITS(SIZE_sJ)
     89 #define MAXARG_sJ	((1 << SIZE_sJ) - 1)
     90 #else
     91 #define MAXARG_sJ	MAX_INT
     92 #endif
     93 
     94 #define OFFSET_sJ	(MAXARG_sJ >> 1)
     95 
     96 
     97 #define MAXARG_A	((1<<SIZE_A)-1)
     98 #define MAXARG_B	((1<<SIZE_B)-1)
     99 #define MAXARG_C	((1<<SIZE_C)-1)
    100 #define OFFSET_sC	(MAXARG_C >> 1)
    101 
    102 #define int2sC(i)	((i) + OFFSET_sC)
    103 #define sC2int(i)	((i) - OFFSET_sC)
    104 
    105 
    106 /* creates a mask with 'n' 1 bits at position 'p' */
    107 #define MASK1(n,p)	((~((~(Instruction)0)<<(n)))<<(p))
    108 
    109 /* creates a mask with 'n' 0 bits at position 'p' */
    110 #define MASK0(n,p)	(~MASK1(n,p))
    111 
    112 /*
    113 ** the following macros help to manipulate instructions
    114 */
    115 
    116 #define GET_OPCODE(i)	(cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
    117 #define SET_OPCODE(i,o)	((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
    118 		((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
    119 
    120 #define checkopm(i,m)	(getOpMode(GET_OPCODE(i)) == m)
    121 
    122 
    123 #define getarg(i,pos,size)	(cast_int(((i)>>(pos)) & MASK1(size,0)))
    124 #define setarg(i,v,pos,size)	((i) = (((i)&MASK0(size,pos)) | \
    125                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
    126 
    127 #define GETARG_A(i)	getarg(i, POS_A, SIZE_A)
    128 #define SETARG_A(i,v)	setarg(i, v, POS_A, SIZE_A)
    129 
    130 #define GETARG_B(i)	check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
    131 #define GETARG_sB(i)	sC2int(GETARG_B(i))
    132 #define SETARG_B(i,v)	setarg(i, v, POS_B, SIZE_B)
    133 
    134 #define GETARG_C(i)	check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
    135 #define GETARG_sC(i)	sC2int(GETARG_C(i))
    136 #define SETARG_C(i,v)	setarg(i, v, POS_C, SIZE_C)
    137 
    138 #define TESTARG_k(i)	check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
    139 #define GETARG_k(i)	check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
    140 #define SETARG_k(i,v)	setarg(i, v, POS_k, 1)
    141 
    142 #define GETARG_Bx(i)	check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
    143 #define SETARG_Bx(i,v)	setarg(i, v, POS_Bx, SIZE_Bx)
    144 
    145 #define GETARG_Ax(i)	check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
    146 #define SETARG_Ax(i,v)	setarg(i, v, POS_Ax, SIZE_Ax)
    147 
    148 #define GETARG_sBx(i)  \
    149 	check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
    150 #define SETARG_sBx(i,b)	SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
    151 
    152 #define GETARG_sJ(i)  \
    153 	check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
    154 #define SETARG_sJ(i,j) \
    155 	setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
    156 
    157 
    158 #define CREATE_ABCk(o,a,b,c,k)	((cast(Instruction, o)<<POS_OP) \
    159 			| (cast(Instruction, a)<<POS_A) \
    160 			| (cast(Instruction, b)<<POS_B) \
    161 			| (cast(Instruction, c)<<POS_C) \
    162 			| (cast(Instruction, k)<<POS_k))
    163 
    164 #define CREATE_ABx(o,a,bc)	((cast(Instruction, o)<<POS_OP) \
    165 			| (cast(Instruction, a)<<POS_A) \
    166 			| (cast(Instruction, bc)<<POS_Bx))
    167 
    168 #define CREATE_Ax(o,a)		((cast(Instruction, o)<<POS_OP) \
    169 			| (cast(Instruction, a)<<POS_Ax))
    170 
    171 #define CREATE_sJ(o,j,k)	((cast(Instruction, o) << POS_OP) \
    172 			| (cast(Instruction, j) << POS_sJ) \
    173 			| (cast(Instruction, k) << POS_k))
    174 
    175 
    176 #if !defined(MAXINDEXRK)  /* (for debugging only) */
    177 #define MAXINDEXRK	MAXARG_B
    178 #endif
    179 
    180 
    181 /*
    182 ** invalid register that fits in 8 bits
    183 */
    184 #define NO_REG		MAXARG_A
    185 
    186 
    187 /*
    188 ** R[x] - register
    189 ** K[x] - constant (in constant table)
    190 ** RK(x) == if k(i) then K[x] else R[x]
    191 */
    192 
    193 
    194 /*
    195 ** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
    196 ** has extra descriptions in the notes after the enumeration.
    197 */
    198 
    199 typedef enum {
    200 /*----------------------------------------------------------------------
    201   name		args	description
    202 ------------------------------------------------------------------------*/
    203 OP_MOVE,/*	A B	R[A] := R[B]					*/
    204 OP_LOADI,/*	A sBx	R[A] := sBx					*/
    205 #ifndef _KERNEL
    206 OP_LOADF,/*	A sBx	R[A] := (lua_Number)sBx				*/
    207 #endif /* _KERNEL */
    208 OP_LOADK,/*	A Bx	R[A] := K[Bx]					*/
    209 OP_LOADKX,/*	A	R[A] := K[extra arg]				*/
    210 OP_LOADFALSE,/*	A	R[A] := false					*/
    211 OP_LFALSESKIP,/*A	R[A] := false; pc++	(*)			*/
    212 OP_LOADTRUE,/*	A	R[A] := true					*/
    213 OP_LOADNIL,/*	A B	R[A], R[A+1], ..., R[A+B] := nil		*/
    214 OP_GETUPVAL,/*	A B	R[A] := UpValue[B]				*/
    215 OP_SETUPVAL,/*	A B	UpValue[B] := R[A]				*/
    216 
    217 OP_GETTABUP,/*	A B C	R[A] := UpValue[B][K[C]:string]			*/
    218 OP_GETTABLE,/*	A B C	R[A] := R[B][R[C]]				*/
    219 OP_GETI,/*	A B C	R[A] := R[B][C]					*/
    220 OP_GETFIELD,/*	A B C	R[A] := R[B][K[C]:string]			*/
    221 
    222 OP_SETTABUP,/*	A B C	UpValue[A][K[B]:string] := RK(C)		*/
    223 OP_SETTABLE,/*	A B C	R[A][R[B]] := RK(C)				*/
    224 OP_SETI,/*	A B C	R[A][B] := RK(C)				*/
    225 OP_SETFIELD,/*	A B C	R[A][K[B]:string] := RK(C)			*/
    226 
    227 OP_NEWTABLE,/*	A B C k	R[A] := {}					*/
    228 
    229 OP_SELF,/*	A B C	R[A+1] := R[B]; R[A] := R[B][RK(C):string]	*/
    230 
    231 OP_ADDI,/*	A B sC	R[A] := R[B] + sC				*/
    232 
    233 OP_ADDK,/*	A B C	R[A] := R[B] + K[C]:number			*/
    234 OP_SUBK,/*	A B C	R[A] := R[B] - K[C]:number			*/
    235 OP_MULK,/*	A B C	R[A] := R[B] * K[C]:number			*/
    236 OP_MODK,/*	A B C	R[A] := R[B] % K[C]:number			*/
    237 #ifndef _KERNEL
    238 OP_POWK,/*	A B C	R[A] := R[B] ^ K[C]:number			*/
    239 OP_DIVK,/*	A B C	R[A] := R[B] / K[C]:number			*/
    240 #endif /* _KERNEL */
    241 OP_IDIVK,/*	A B C	R[A] := R[B] // K[C]:number			*/
    242 
    243 OP_BANDK,/*	A B C	R[A] := R[B] & K[C]:integer			*/
    244 OP_BORK,/*	A B C	R[A] := R[B] | K[C]:integer			*/
    245 OP_BXORK,/*	A B C	R[A] := R[B] ~ K[C]:integer			*/
    246 
    247 OP_SHRI,/*	A B sC	R[A] := R[B] >> sC				*/
    248 OP_SHLI,/*	A B sC	R[A] := sC << R[B]				*/
    249 
    250 OP_ADD,/*	A B C	R[A] := R[B] + R[C]				*/
    251 OP_SUB,/*	A B C	R[A] := R[B] - R[C]				*/
    252 OP_MUL,/*	A B C	R[A] := R[B] * R[C]				*/
    253 OP_MOD,/*	A B C	R[A] := R[B] % R[C]				*/
    254 #ifndef _KERNEL
    255 OP_POW,/*	A B C	R[A] := R[B] ^ R[C]				*/
    256 OP_DIV,/*	A B C	R[A] := R[B] / R[C]				*/
    257 #endif /* _KERNEL */
    258 OP_IDIV,/*	A B C	R[A] := R[B] // R[C]				*/
    259 
    260 OP_BAND,/*	A B C	R[A] := R[B] & R[C]				*/
    261 OP_BOR,/*	A B C	R[A] := R[B] | R[C]				*/
    262 OP_BXOR,/*	A B C	R[A] := R[B] ~ R[C]				*/
    263 OP_SHL,/*	A B C	R[A] := R[B] << R[C]				*/
    264 OP_SHR,/*	A B C	R[A] := R[B] >> R[C]				*/
    265 
    266 OP_MMBIN,/*	A B C	call C metamethod over R[A] and R[B]	(*)	*/
    267 OP_MMBINI,/*	A sB C k	call C metamethod over R[A] and sB	*/
    268 OP_MMBINK,/*	A B C k		call C metamethod over R[A] and K[B]	*/
    269 
    270 OP_UNM,/*	A B	R[A] := -R[B]					*/
    271 OP_BNOT,/*	A B	R[A] := ~R[B]					*/
    272 OP_NOT,/*	A B	R[A] := not R[B]				*/
    273 OP_LEN,/*	A B	R[A] := #R[B] (length operator)			*/
    274 
    275 OP_CONCAT,/*	A B	R[A] := R[A].. ... ..R[A + B - 1]		*/
    276 
    277 OP_CLOSE,/*	A	close all upvalues >= R[A]			*/
    278 OP_TBC,/*	A	mark variable A "to be closed"			*/
    279 OP_JMP,/*	sJ	pc += sJ					*/
    280 OP_EQ,/*	A B k	if ((R[A] == R[B]) ~= k) then pc++		*/
    281 OP_LT,/*	A B k	if ((R[A] <  R[B]) ~= k) then pc++		*/
    282 OP_LE,/*	A B k	if ((R[A] <= R[B]) ~= k) then pc++		*/
    283 
    284 OP_EQK,/*	A B k	if ((R[A] == K[B]) ~= k) then pc++		*/
    285 OP_EQI,/*	A sB k	if ((R[A] == sB) ~= k) then pc++		*/
    286 OP_LTI,/*	A sB k	if ((R[A] < sB) ~= k) then pc++			*/
    287 OP_LEI,/*	A sB k	if ((R[A] <= sB) ~= k) then pc++		*/
    288 OP_GTI,/*	A sB k	if ((R[A] > sB) ~= k) then pc++			*/
    289 OP_GEI,/*	A sB k	if ((R[A] >= sB) ~= k) then pc++		*/
    290 
    291 OP_TEST,/*	A k	if (not R[A] == k) then pc++			*/
    292 OP_TESTSET,/*	A B k	if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
    293 
    294 OP_CALL,/*	A B C	R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
    295 OP_TAILCALL,/*	A B C k	return R[A](R[A+1], ... ,R[A+B-1])		*/
    296 
    297 OP_RETURN,/*	A B C k	return R[A], ... ,R[A+B-2]	(see note)	*/
    298 OP_RETURN0,/*		return						*/
    299 OP_RETURN1,/*	A	return R[A]					*/
    300 
    301 OP_FORLOOP,/*	A Bx	update counters; if loop continues then pc-=Bx; */
    302 OP_FORPREP,/*	A Bx	<check values and prepare counters>;
    303                         if not to run then pc+=Bx+1;			*/
    304 
    305 OP_TFORPREP,/*	A Bx	create upvalue for R[A + 3]; pc+=Bx		*/
    306 OP_TFORCALL,/*	A C	R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]);	*/
    307 OP_TFORLOOP,/*	A Bx	if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx }	*/
    308 
    309 OP_SETLIST,/*	A B C k	R[A][C+i] := R[A+i], 1 <= i <= B		*/
    310 
    311 OP_CLOSURE,/*	A Bx	R[A] := closure(KPROTO[Bx])			*/
    312 
    313 OP_VARARG,/*	A C	R[A], R[A+1], ..., R[A+C-2] = vararg		*/
    314 
    315 OP_VARARGPREP,/*A	(adjust vararg parameters)			*/
    316 
    317 OP_EXTRAARG/*	Ax	extra (larger) argument for previous opcode	*/
    318 } OpCode;
    319 
    320 
    321 #define NUM_OPCODES	((int)(OP_EXTRAARG) + 1)
    322 
    323 
    324 
    325 /*===========================================================================
    326   Notes:
    327 
    328   (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
    329   value, in a code equivalent to (not cond ? false : true).  (It
    330   produces false and skips the next instruction producing true.)
    331 
    332   (*) Opcodes OP_MMBIN and variants follow each arithmetic and
    333   bitwise opcode. If the operation succeeds, it skips this next
    334   opcode. Otherwise, this opcode calls the corresponding metamethod.
    335 
    336   (*) Opcode OP_TESTSET is used in short-circuit expressions that need
    337   both to jump and to produce a value, such as (a = b or c).
    338 
    339   (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
    340   'top' is set to last_result+1, so next open instruction (OP_CALL,
    341   OP_RETURN*, OP_SETLIST) may use 'top'.
    342 
    343   (*) In OP_VARARG, if (C == 0) then use actual number of varargs and
    344   set top (like in OP_CALL with C == 0).
    345 
    346   (*) In OP_RETURN, if (B == 0) then return up to 'top'.
    347 
    348   (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
    349   OP_EXTRAARG.
    350 
    351   (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
    352   real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
    353   bits of C).
    354 
    355   (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
    356   power of 2) plus 1, or zero for size zero. If not k, the array size
    357   is C. Otherwise, the array size is EXTRAARG _ C.
    358 
    359   (*) For comparisons, k specifies what condition the test should accept
    360   (true or false).
    361 
    362   (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
    363    (the constant is the first operand).
    364 
    365   (*) All 'skips' (pc++) assume that next instruction is a jump.
    366 
    367   (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
    368   function builds upvalues, which may need to be closed. C > 0 means
    369   the function is vararg, so that its 'func' must be corrected before
    370   returning; in this case, (C - 1) is its number of fixed parameters.
    371 
    372   (*) In comparisons with an immediate operand, C signals whether the
    373   original operand was a float. (It must be corrected in case of
    374   metamethods.)
    375 
    376 ===========================================================================*/
    377 
    378 
    379 /*
    380 ** masks for instruction properties. The format is:
    381 ** bits 0-2: op mode
    382 ** bit 3: instruction set register A
    383 ** bit 4: operator is a test (next instruction must be a jump)
    384 ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
    385 ** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
    386 ** bit 7: instruction is an MM instruction (call a metamethod)
    387 */
    388 
    389 LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
    390 
    391 #define getOpMode(m)	(cast(enum OpMode, luaP_opmodes[m] & 7))
    392 #define testAMode(m)	(luaP_opmodes[m] & (1 << 3))
    393 #define testTMode(m)	(luaP_opmodes[m] & (1 << 4))
    394 #define testITMode(m)	(luaP_opmodes[m] & (1 << 5))
    395 #define testOTMode(m)	(luaP_opmodes[m] & (1 << 6))
    396 #define testMMMode(m)	(luaP_opmodes[m] & (1 << 7))
    397 
    398 /* "out top" (set top for next instruction) */
    399 #define isOT(i)  \
    400 	((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
    401           GET_OPCODE(i) == OP_TAILCALL)
    402 
    403 /* "in top" (uses top from previous instruction) */
    404 #define isIT(i)		(testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
    405 
    406 #define opmode(mm,ot,it,t,a,m)  \
    407     (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
    408 
    409 
    410 /* number of list items to accumulate before a SETLIST instruction */
    411 #define LFIELDS_PER_FLUSH	50
    412 
    413 #endif
    414