xref: /minix/external/mit/lua/dist/src/lopcodes.h (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: lopcodes.h,v 1.3 2015/02/02 14:03:05 lneto Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lopcodes.h,v 1.148 2014/10/25 11:50:46 roberto Exp
511be35a1SLionel Sambuc ** Opcodes for Lua virtual machine
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc 
911be35a1SLionel Sambuc #ifndef lopcodes_h
1011be35a1SLionel Sambuc #define lopcodes_h
1111be35a1SLionel Sambuc 
1211be35a1SLionel Sambuc #include "llimits.h"
1311be35a1SLionel Sambuc 
1411be35a1SLionel Sambuc 
1511be35a1SLionel Sambuc /*===========================================================================
1611be35a1SLionel Sambuc   We assume that instructions are unsigned numbers.
1711be35a1SLionel Sambuc   All instructions have an opcode in the first 6 bits.
1811be35a1SLionel Sambuc   Instructions can have the following fields:
19*0a6a1f1dSLionel Sambuc 	'A' : 8 bits
20*0a6a1f1dSLionel Sambuc 	'B' : 9 bits
21*0a6a1f1dSLionel Sambuc 	'C' : 9 bits
22*0a6a1f1dSLionel Sambuc 	'Ax' : 26 bits ('A', 'B', and 'C' together)
23*0a6a1f1dSLionel Sambuc 	'Bx' : 18 bits ('B' and 'C' together)
24*0a6a1f1dSLionel Sambuc 	'sBx' : signed Bx
2511be35a1SLionel Sambuc 
2611be35a1SLionel Sambuc   A signed argument is represented in excess K; that is, the number
2711be35a1SLionel Sambuc   value is the unsigned value minus K. K is exactly the maximum value
2811be35a1SLionel Sambuc   for that argument (so that -max is represented by 0, and +max is
2911be35a1SLionel Sambuc   represented by 2*max), which is half the maximum for the corresponding
3011be35a1SLionel Sambuc   unsigned argument.
3111be35a1SLionel Sambuc ===========================================================================*/
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc 
34*0a6a1f1dSLionel Sambuc enum OpMode {iABC, iABx, iAsBx, iAx};  /* basic instruction format */
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc /*
3811be35a1SLionel Sambuc ** size and position of opcode arguments.
3911be35a1SLionel Sambuc */
4011be35a1SLionel Sambuc #define SIZE_C		9
4111be35a1SLionel Sambuc #define SIZE_B		9
4211be35a1SLionel Sambuc #define SIZE_Bx		(SIZE_C + SIZE_B)
4311be35a1SLionel Sambuc #define SIZE_A		8
44*0a6a1f1dSLionel Sambuc #define SIZE_Ax		(SIZE_C + SIZE_B + SIZE_A)
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc #define SIZE_OP		6
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc #define POS_OP		0
4911be35a1SLionel Sambuc #define POS_A		(POS_OP + SIZE_OP)
5011be35a1SLionel Sambuc #define POS_C		(POS_A + SIZE_A)
5111be35a1SLionel Sambuc #define POS_B		(POS_C + SIZE_C)
5211be35a1SLionel Sambuc #define POS_Bx		POS_C
53*0a6a1f1dSLionel Sambuc #define POS_Ax		POS_A
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc 
5611be35a1SLionel Sambuc /*
5711be35a1SLionel Sambuc ** limits for opcode arguments.
5811be35a1SLionel Sambuc ** we use (signed) int to manipulate most arguments,
5911be35a1SLionel Sambuc ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
6011be35a1SLionel Sambuc */
6111be35a1SLionel Sambuc #if SIZE_Bx < LUAI_BITSINT-1
6211be35a1SLionel Sambuc #define MAXARG_Bx        ((1<<SIZE_Bx)-1)
63*0a6a1f1dSLionel Sambuc #define MAXARG_sBx        (MAXARG_Bx>>1)         /* 'sBx' is signed */
6411be35a1SLionel Sambuc #else
6511be35a1SLionel Sambuc #define MAXARG_Bx        MAX_INT
6611be35a1SLionel Sambuc #define MAXARG_sBx        MAX_INT
6711be35a1SLionel Sambuc #endif
6811be35a1SLionel Sambuc 
69*0a6a1f1dSLionel Sambuc #if SIZE_Ax < LUAI_BITSINT-1
70*0a6a1f1dSLionel Sambuc #define MAXARG_Ax	((1<<SIZE_Ax)-1)
71*0a6a1f1dSLionel Sambuc #else
72*0a6a1f1dSLionel Sambuc #define MAXARG_Ax	MAX_INT
73*0a6a1f1dSLionel Sambuc #endif
74*0a6a1f1dSLionel Sambuc 
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc #define MAXARG_A        ((1<<SIZE_A)-1)
7711be35a1SLionel Sambuc #define MAXARG_B        ((1<<SIZE_B)-1)
7811be35a1SLionel Sambuc #define MAXARG_C        ((1<<SIZE_C)-1)
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc 
81*0a6a1f1dSLionel Sambuc /* creates a mask with 'n' 1 bits at position 'p' */
82*0a6a1f1dSLionel Sambuc #define MASK1(n,p)	((~((~(Instruction)0)<<(n)))<<(p))
8311be35a1SLionel Sambuc 
84*0a6a1f1dSLionel Sambuc /* creates a mask with 'n' 0 bits at position 'p' */
8511be35a1SLionel Sambuc #define MASK0(n,p)	(~MASK1(n,p))
8611be35a1SLionel Sambuc 
8711be35a1SLionel Sambuc /*
8811be35a1SLionel Sambuc ** the following macros help to manipulate instructions
8911be35a1SLionel Sambuc */
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc #define GET_OPCODE(i)	(cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
9211be35a1SLionel Sambuc #define SET_OPCODE(i,o)	((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
9311be35a1SLionel Sambuc 		((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
9411be35a1SLionel Sambuc 
95*0a6a1f1dSLionel Sambuc #define getarg(i,pos,size)	(cast(int, ((i)>>pos) & MASK1(size,0)))
96*0a6a1f1dSLionel Sambuc #define setarg(i,v,pos,size)	((i) = (((i)&MASK0(size,pos)) | \
97*0a6a1f1dSLionel Sambuc                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
9811be35a1SLionel Sambuc 
99*0a6a1f1dSLionel Sambuc #define GETARG_A(i)	getarg(i, POS_A, SIZE_A)
100*0a6a1f1dSLionel Sambuc #define SETARG_A(i,v)	setarg(i, v, POS_A, SIZE_A)
10111be35a1SLionel Sambuc 
102*0a6a1f1dSLionel Sambuc #define GETARG_B(i)	getarg(i, POS_B, SIZE_B)
103*0a6a1f1dSLionel Sambuc #define SETARG_B(i,v)	setarg(i, v, POS_B, SIZE_B)
10411be35a1SLionel Sambuc 
105*0a6a1f1dSLionel Sambuc #define GETARG_C(i)	getarg(i, POS_C, SIZE_C)
106*0a6a1f1dSLionel Sambuc #define SETARG_C(i,v)	setarg(i, v, POS_C, SIZE_C)
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc #define GETARG_Bx(i)	getarg(i, POS_Bx, SIZE_Bx)
109*0a6a1f1dSLionel Sambuc #define SETARG_Bx(i,v)	setarg(i, v, POS_Bx, SIZE_Bx)
110*0a6a1f1dSLionel Sambuc 
111*0a6a1f1dSLionel Sambuc #define GETARG_Ax(i)	getarg(i, POS_Ax, SIZE_Ax)
112*0a6a1f1dSLionel Sambuc #define SETARG_Ax(i,v)	setarg(i, v, POS_Ax, SIZE_Ax)
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc #define GETARG_sBx(i)	(GETARG_Bx(i)-MAXARG_sBx)
11511be35a1SLionel Sambuc #define SETARG_sBx(i,b)	SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
11611be35a1SLionel Sambuc 
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc #define CREATE_ABC(o,a,b,c)	((cast(Instruction, o)<<POS_OP) \
11911be35a1SLionel Sambuc 			| (cast(Instruction, a)<<POS_A) \
12011be35a1SLionel Sambuc 			| (cast(Instruction, b)<<POS_B) \
12111be35a1SLionel Sambuc 			| (cast(Instruction, c)<<POS_C))
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc #define CREATE_ABx(o,a,bc)	((cast(Instruction, o)<<POS_OP) \
12411be35a1SLionel Sambuc 			| (cast(Instruction, a)<<POS_A) \
12511be35a1SLionel Sambuc 			| (cast(Instruction, bc)<<POS_Bx))
12611be35a1SLionel Sambuc 
127*0a6a1f1dSLionel Sambuc #define CREATE_Ax(o,a)		((cast(Instruction, o)<<POS_OP) \
128*0a6a1f1dSLionel Sambuc 			| (cast(Instruction, a)<<POS_Ax))
129*0a6a1f1dSLionel Sambuc 
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc /*
13211be35a1SLionel Sambuc ** Macros to operate RK indices
13311be35a1SLionel Sambuc */
13411be35a1SLionel Sambuc 
13511be35a1SLionel Sambuc /* this bit 1 means constant (0 means register) */
13611be35a1SLionel Sambuc #define BITRK		(1 << (SIZE_B - 1))
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc /* test whether value is a constant */
13911be35a1SLionel Sambuc #define ISK(x)		((x) & BITRK)
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc /* gets the index of the constant */
14211be35a1SLionel Sambuc #define INDEXK(r)	((int)(r) & ~BITRK)
14311be35a1SLionel Sambuc 
14411be35a1SLionel Sambuc #define MAXINDEXRK	(BITRK - 1)
14511be35a1SLionel Sambuc 
14611be35a1SLionel Sambuc /* code a constant index as a RK value */
14711be35a1SLionel Sambuc #define RKASK(x)	((x) | BITRK)
14811be35a1SLionel Sambuc 
14911be35a1SLionel Sambuc 
15011be35a1SLionel Sambuc /*
15111be35a1SLionel Sambuc ** invalid register that fits in 8 bits
15211be35a1SLionel Sambuc */
15311be35a1SLionel Sambuc #define NO_REG		MAXARG_A
15411be35a1SLionel Sambuc 
15511be35a1SLionel Sambuc 
15611be35a1SLionel Sambuc /*
15711be35a1SLionel Sambuc ** R(x) - register
15811be35a1SLionel Sambuc ** Kst(x) - constant (in constant table)
15911be35a1SLionel Sambuc ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
16011be35a1SLionel Sambuc */
16111be35a1SLionel Sambuc 
16211be35a1SLionel Sambuc 
16311be35a1SLionel Sambuc /*
16411be35a1SLionel Sambuc ** grep "ORDER OP" if you change these enums
16511be35a1SLionel Sambuc */
16611be35a1SLionel Sambuc 
16711be35a1SLionel Sambuc typedef enum {
16811be35a1SLionel Sambuc /*----------------------------------------------------------------------
16911be35a1SLionel Sambuc name		args	description
17011be35a1SLionel Sambuc ------------------------------------------------------------------------*/
17111be35a1SLionel Sambuc OP_MOVE,/*	A B	R(A) := R(B)					*/
17211be35a1SLionel Sambuc OP_LOADK,/*	A Bx	R(A) := Kst(Bx)					*/
173*0a6a1f1dSLionel Sambuc OP_LOADKX,/*	A 	R(A) := Kst(extra arg)				*/
17411be35a1SLionel Sambuc OP_LOADBOOL,/*	A B C	R(A) := (Bool)B; if (C) pc++			*/
175*0a6a1f1dSLionel Sambuc OP_LOADNIL,/*	A B	R(A), R(A+1), ..., R(A+B) := nil		*/
17611be35a1SLionel Sambuc OP_GETUPVAL,/*	A B	R(A) := UpValue[B]				*/
17711be35a1SLionel Sambuc 
178*0a6a1f1dSLionel Sambuc OP_GETTABUP,/*	A B C	R(A) := UpValue[B][RK(C)]			*/
17911be35a1SLionel Sambuc OP_GETTABLE,/*	A B C	R(A) := R(B)[RK(C)]				*/
18011be35a1SLionel Sambuc 
181*0a6a1f1dSLionel Sambuc OP_SETTABUP,/*	A B C	UpValue[A][RK(B)] := RK(C)			*/
18211be35a1SLionel Sambuc OP_SETUPVAL,/*	A B	UpValue[B] := R(A)				*/
18311be35a1SLionel Sambuc OP_SETTABLE,/*	A B C	R(A)[RK(B)] := RK(C)				*/
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc OP_NEWTABLE,/*	A B C	R(A) := {} (size = B,C)				*/
18611be35a1SLionel Sambuc 
18711be35a1SLionel Sambuc OP_SELF,/*	A B C	R(A+1) := R(B); R(A) := R(B)[RK(C)]		*/
18811be35a1SLionel Sambuc 
18911be35a1SLionel Sambuc OP_ADD,/*	A B C	R(A) := RK(B) + RK(C)				*/
19011be35a1SLionel Sambuc OP_SUB,/*	A B C	R(A) := RK(B) - RK(C)				*/
19111be35a1SLionel Sambuc OP_MUL,/*	A B C	R(A) := RK(B) * RK(C)				*/
19211be35a1SLionel Sambuc OP_MOD,/*	A B C	R(A) := RK(B) % RK(C)				*/
193*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
19411be35a1SLionel Sambuc OP_POW,/*	A B C	R(A) := RK(B) ^ RK(C)				*/
195*0a6a1f1dSLionel Sambuc OP_DIV,/*	A B C	R(A) := RK(B) / RK(C)				*/
196*0a6a1f1dSLionel Sambuc #endif
197*0a6a1f1dSLionel Sambuc OP_IDIV,/*	A B C	R(A) := RK(B) // RK(C)				*/
198*0a6a1f1dSLionel Sambuc OP_BAND,/*	A B C	R(A) := RK(B) & RK(C)				*/
199*0a6a1f1dSLionel Sambuc OP_BOR,/*	A B C	R(A) := RK(B) | RK(C)				*/
200*0a6a1f1dSLionel Sambuc OP_BXOR,/*	A B C	R(A) := RK(B) ~ RK(C)				*/
201*0a6a1f1dSLionel Sambuc OP_SHL,/*	A B C	R(A) := RK(B) << RK(C)				*/
202*0a6a1f1dSLionel Sambuc OP_SHR,/*	A B C	R(A) := RK(B) >> RK(C)				*/
20311be35a1SLionel Sambuc OP_UNM,/*	A B	R(A) := -R(B)					*/
204*0a6a1f1dSLionel Sambuc OP_BNOT,/*	A B	R(A) := ~R(B)					*/
20511be35a1SLionel Sambuc OP_NOT,/*	A B	R(A) := not R(B)				*/
20611be35a1SLionel Sambuc OP_LEN,/*	A B	R(A) := length of R(B)				*/
20711be35a1SLionel Sambuc 
20811be35a1SLionel Sambuc OP_CONCAT,/*	A B C	R(A) := R(B).. ... ..R(C)			*/
20911be35a1SLionel Sambuc 
210*0a6a1f1dSLionel Sambuc OP_JMP,/*	A sBx	pc+=sBx; if (A) close all upvalues >= R(A - 1)	*/
21111be35a1SLionel Sambuc OP_EQ,/*	A B C	if ((RK(B) == RK(C)) ~= A) then pc++		*/
21211be35a1SLionel Sambuc OP_LT,/*	A B C	if ((RK(B) <  RK(C)) ~= A) then pc++		*/
21311be35a1SLionel Sambuc OP_LE,/*	A B C	if ((RK(B) <= RK(C)) ~= A) then pc++		*/
21411be35a1SLionel Sambuc 
21511be35a1SLionel Sambuc OP_TEST,/*	A C	if not (R(A) <=> C) then pc++			*/
21611be35a1SLionel Sambuc OP_TESTSET,/*	A B C	if (R(B) <=> C) then R(A) := R(B) else pc++	*/
21711be35a1SLionel Sambuc 
21811be35a1SLionel Sambuc OP_CALL,/*	A B C	R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
21911be35a1SLionel Sambuc OP_TAILCALL,/*	A B C	return R(A)(R(A+1), ... ,R(A+B-1))		*/
22011be35a1SLionel Sambuc OP_RETURN,/*	A B	return R(A), ... ,R(A+B-2)	(see note)	*/
22111be35a1SLionel Sambuc 
22211be35a1SLionel Sambuc OP_FORLOOP,/*	A sBx	R(A)+=R(A+2);
22311be35a1SLionel Sambuc 			if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
22411be35a1SLionel Sambuc OP_FORPREP,/*	A sBx	R(A)-=R(A+2); pc+=sBx				*/
22511be35a1SLionel Sambuc 
226*0a6a1f1dSLionel Sambuc OP_TFORCALL,/*	A C	R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));	*/
227*0a6a1f1dSLionel Sambuc OP_TFORLOOP,/*	A sBx	if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
228*0a6a1f1dSLionel Sambuc 
22911be35a1SLionel Sambuc OP_SETLIST,/*	A B C	R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B	*/
23011be35a1SLionel Sambuc 
231*0a6a1f1dSLionel Sambuc OP_CLOSURE,/*	A Bx	R(A) := closure(KPROTO[Bx])			*/
23211be35a1SLionel Sambuc 
233*0a6a1f1dSLionel Sambuc OP_VARARG,/*	A B	R(A), R(A+1), ..., R(A+B-2) = vararg		*/
234*0a6a1f1dSLionel Sambuc 
235*0a6a1f1dSLionel Sambuc OP_EXTRAARG/*	Ax	extra (larger) argument for previous opcode	*/
23611be35a1SLionel Sambuc } OpCode;
23711be35a1SLionel Sambuc 
23811be35a1SLionel Sambuc 
239*0a6a1f1dSLionel Sambuc #define NUM_OPCODES	(cast(int, OP_EXTRAARG) + 1)
24011be35a1SLionel Sambuc 
24111be35a1SLionel Sambuc 
24211be35a1SLionel Sambuc 
24311be35a1SLionel Sambuc /*===========================================================================
24411be35a1SLionel Sambuc   Notes:
245*0a6a1f1dSLionel Sambuc   (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
246*0a6a1f1dSLionel Sambuc   set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
247*0a6a1f1dSLionel Sambuc   OP_SETLIST) may use 'top'.
24811be35a1SLionel Sambuc 
24911be35a1SLionel Sambuc   (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
25011be35a1SLionel Sambuc   set top (like in OP_CALL with C == 0).
25111be35a1SLionel Sambuc 
252*0a6a1f1dSLionel Sambuc   (*) In OP_RETURN, if (B == 0) then return up to 'top'.
25311be35a1SLionel Sambuc 
254*0a6a1f1dSLionel Sambuc   (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
255*0a6a1f1dSLionel Sambuc   'instruction' is EXTRAARG(real C).
256*0a6a1f1dSLionel Sambuc 
257*0a6a1f1dSLionel Sambuc   (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
25811be35a1SLionel Sambuc 
25911be35a1SLionel Sambuc   (*) For comparisons, A specifies what condition the test should accept
26011be35a1SLionel Sambuc   (true or false).
26111be35a1SLionel Sambuc 
262*0a6a1f1dSLionel Sambuc   (*) All 'skips' (pc++) assume that next instruction is a jump.
263*0a6a1f1dSLionel Sambuc 
26411be35a1SLionel Sambuc ===========================================================================*/
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc 
26711be35a1SLionel Sambuc /*
26811be35a1SLionel Sambuc ** masks for instruction properties. The format is:
26911be35a1SLionel Sambuc ** bits 0-1: op mode
27011be35a1SLionel Sambuc ** bits 2-3: C arg mode
27111be35a1SLionel Sambuc ** bits 4-5: B arg mode
27211be35a1SLionel Sambuc ** bit 6: instruction set register A
273*0a6a1f1dSLionel Sambuc ** bit 7: operator is a test (next instruction must be a jump)
27411be35a1SLionel Sambuc */
27511be35a1SLionel Sambuc 
27611be35a1SLionel Sambuc enum OpArgMask {
27711be35a1SLionel Sambuc   OpArgN,  /* argument is not used */
27811be35a1SLionel Sambuc   OpArgU,  /* argument is used */
27911be35a1SLionel Sambuc   OpArgR,  /* argument is a register or a jump offset */
28011be35a1SLionel Sambuc   OpArgK   /* argument is a constant or register/constant */
28111be35a1SLionel Sambuc };
28211be35a1SLionel Sambuc 
283*0a6a1f1dSLionel Sambuc LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
28411be35a1SLionel Sambuc 
28511be35a1SLionel Sambuc #define getOpMode(m)	(cast(enum OpMode, luaP_opmodes[m] & 3))
28611be35a1SLionel Sambuc #define getBMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
28711be35a1SLionel Sambuc #define getCMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
28811be35a1SLionel Sambuc #define testAMode(m)	(luaP_opmodes[m] & (1 << 6))
28911be35a1SLionel Sambuc #define testTMode(m)	(luaP_opmodes[m] & (1 << 7))
29011be35a1SLionel Sambuc 
29111be35a1SLionel Sambuc 
292*0a6a1f1dSLionel Sambuc LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
29311be35a1SLionel Sambuc 
29411be35a1SLionel Sambuc 
29511be35a1SLionel Sambuc /* number of list items to accumulate before a SETLIST instruction */
29611be35a1SLionel Sambuc #define LFIELDS_PER_FLUSH	50
29711be35a1SLionel Sambuc 
29811be35a1SLionel Sambuc 
29911be35a1SLionel Sambuc #endif
300