xref: /freebsd/sys/contrib/openzfs/module/lua/lopcodes.h (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /* BEGIN CSTYLED */
2*eda14cbcSMatt Macy /*
3*eda14cbcSMatt Macy ** $Id: lopcodes.h,v 1.142.1.2 2014/10/20 18:32:09 roberto Exp $
4*eda14cbcSMatt Macy ** Opcodes for Lua virtual machine
5*eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6*eda14cbcSMatt Macy */
7*eda14cbcSMatt Macy 
8*eda14cbcSMatt Macy #ifndef lopcodes_h
9*eda14cbcSMatt Macy #define lopcodes_h
10*eda14cbcSMatt Macy 
11*eda14cbcSMatt Macy #include "llimits.h"
12*eda14cbcSMatt Macy 
13*eda14cbcSMatt Macy 
14*eda14cbcSMatt Macy /*===========================================================================
15*eda14cbcSMatt Macy   We assume that instructions are unsigned numbers.
16*eda14cbcSMatt Macy   All instructions have an opcode in the first 6 bits.
17*eda14cbcSMatt Macy   Instructions can have the following fields:
18*eda14cbcSMatt Macy 	`A' : 8 bits
19*eda14cbcSMatt Macy 	`B' : 9 bits
20*eda14cbcSMatt Macy 	`C' : 9 bits
21*eda14cbcSMatt Macy 	'Ax' : 26 bits ('A', 'B', and 'C' together)
22*eda14cbcSMatt Macy 	`Bx' : 18 bits (`B' and `C' together)
23*eda14cbcSMatt Macy 	`sBx' : signed Bx
24*eda14cbcSMatt Macy 
25*eda14cbcSMatt Macy   A signed argument is represented in excess K; that is, the number
26*eda14cbcSMatt Macy   value is the unsigned value minus K. K is exactly the maximum value
27*eda14cbcSMatt Macy   for that argument (so that -max is represented by 0, and +max is
28*eda14cbcSMatt Macy   represented by 2*max), which is half the maximum for the corresponding
29*eda14cbcSMatt Macy   unsigned argument.
30*eda14cbcSMatt Macy ===========================================================================*/
31*eda14cbcSMatt Macy 
32*eda14cbcSMatt Macy 
33*eda14cbcSMatt Macy enum OpMode {iABC, iABx, iAsBx, iAx};  /* basic instruction format */
34*eda14cbcSMatt Macy 
35*eda14cbcSMatt Macy 
36*eda14cbcSMatt Macy /*
37*eda14cbcSMatt Macy ** size and position of opcode arguments.
38*eda14cbcSMatt Macy */
39*eda14cbcSMatt Macy #define SIZE_C		9
40*eda14cbcSMatt Macy #define SIZE_B		9
41*eda14cbcSMatt Macy #define SIZE_Bx		(SIZE_C + SIZE_B)
42*eda14cbcSMatt Macy #define SIZE_A		8
43*eda14cbcSMatt Macy #define SIZE_Ax		(SIZE_C + SIZE_B + SIZE_A)
44*eda14cbcSMatt Macy 
45*eda14cbcSMatt Macy #define SIZE_OP		6
46*eda14cbcSMatt Macy 
47*eda14cbcSMatt Macy #define POS_OP		0
48*eda14cbcSMatt Macy #define POS_A		(POS_OP + SIZE_OP)
49*eda14cbcSMatt Macy #define POS_C		(POS_A + SIZE_A)
50*eda14cbcSMatt Macy #define POS_B		(POS_C + SIZE_C)
51*eda14cbcSMatt Macy #define POS_Bx		POS_C
52*eda14cbcSMatt Macy #define POS_Ax		POS_A
53*eda14cbcSMatt Macy 
54*eda14cbcSMatt Macy 
55*eda14cbcSMatt Macy /*
56*eda14cbcSMatt Macy ** limits for opcode arguments.
57*eda14cbcSMatt Macy ** we use (signed) int to manipulate most arguments,
58*eda14cbcSMatt Macy ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
59*eda14cbcSMatt Macy */
60*eda14cbcSMatt Macy #if SIZE_Bx < LUAI_BITSINT-1
61*eda14cbcSMatt Macy #define MAXARG_Bx        ((1<<SIZE_Bx)-1)
62*eda14cbcSMatt Macy #define MAXARG_sBx        (MAXARG_Bx>>1)         /* `sBx' is signed */
63*eda14cbcSMatt Macy #else
64*eda14cbcSMatt Macy #define MAXARG_Bx        MAX_INT
65*eda14cbcSMatt Macy #define MAXARG_sBx        MAX_INT
66*eda14cbcSMatt Macy #endif
67*eda14cbcSMatt Macy 
68*eda14cbcSMatt Macy #if SIZE_Ax < LUAI_BITSINT-1
69*eda14cbcSMatt Macy #define MAXARG_Ax	((1<<SIZE_Ax)-1)
70*eda14cbcSMatt Macy #else
71*eda14cbcSMatt Macy #define MAXARG_Ax	MAX_INT
72*eda14cbcSMatt Macy #endif
73*eda14cbcSMatt Macy 
74*eda14cbcSMatt Macy 
75*eda14cbcSMatt Macy #define MAXARG_A        ((1<<SIZE_A)-1)
76*eda14cbcSMatt Macy #define MAXARG_B        ((1<<SIZE_B)-1)
77*eda14cbcSMatt Macy #define MAXARG_C        ((1<<SIZE_C)-1)
78*eda14cbcSMatt Macy 
79*eda14cbcSMatt Macy 
80*eda14cbcSMatt Macy /* creates a mask with `n' 1 bits at position `p' */
81*eda14cbcSMatt Macy #define MASK1(n,p)	((~((~(Instruction)0)<<(n)))<<(p))
82*eda14cbcSMatt Macy 
83*eda14cbcSMatt Macy /* creates a mask with `n' 0 bits at position `p' */
84*eda14cbcSMatt Macy #define MASK0(n,p)	(~MASK1(n,p))
85*eda14cbcSMatt Macy 
86*eda14cbcSMatt Macy /*
87*eda14cbcSMatt Macy ** the following macros help to manipulate instructions
88*eda14cbcSMatt Macy */
89*eda14cbcSMatt Macy 
90*eda14cbcSMatt Macy #define GET_OPCODE(i)	(cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
91*eda14cbcSMatt Macy #define SET_OPCODE(i,o)	((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
92*eda14cbcSMatt Macy 		((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
93*eda14cbcSMatt Macy 
94*eda14cbcSMatt Macy #define getarg(i,pos,size)	(cast(int, ((i)>>pos) & MASK1(size,0)))
95*eda14cbcSMatt Macy #define setarg(i,v,pos,size)	((i) = (((i)&MASK0(size,pos)) | \
96*eda14cbcSMatt Macy                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
97*eda14cbcSMatt Macy 
98*eda14cbcSMatt Macy #define GETARG_A(i)	getarg(i, POS_A, SIZE_A)
99*eda14cbcSMatt Macy #define SETARG_A(i,v)	setarg(i, v, POS_A, SIZE_A)
100*eda14cbcSMatt Macy 
101*eda14cbcSMatt Macy #define GETARG_B(i)	getarg(i, POS_B, SIZE_B)
102*eda14cbcSMatt Macy #define SETARG_B(i,v)	setarg(i, v, POS_B, SIZE_B)
103*eda14cbcSMatt Macy 
104*eda14cbcSMatt Macy #define GETARG_C(i)	getarg(i, POS_C, SIZE_C)
105*eda14cbcSMatt Macy #define SETARG_C(i,v)	setarg(i, v, POS_C, SIZE_C)
106*eda14cbcSMatt Macy 
107*eda14cbcSMatt Macy #define GETARG_Bx(i)	getarg(i, POS_Bx, SIZE_Bx)
108*eda14cbcSMatt Macy #define SETARG_Bx(i,v)	setarg(i, v, POS_Bx, SIZE_Bx)
109*eda14cbcSMatt Macy 
110*eda14cbcSMatt Macy #define GETARG_Ax(i)	getarg(i, POS_Ax, SIZE_Ax)
111*eda14cbcSMatt Macy #define SETARG_Ax(i,v)	setarg(i, v, POS_Ax, SIZE_Ax)
112*eda14cbcSMatt Macy 
113*eda14cbcSMatt Macy #define GETARG_sBx(i)	(GETARG_Bx(i)-MAXARG_sBx)
114*eda14cbcSMatt Macy #define SETARG_sBx(i,b)	SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
115*eda14cbcSMatt Macy 
116*eda14cbcSMatt Macy 
117*eda14cbcSMatt Macy #define CREATE_ABC(o,a,b,c)	((cast(Instruction, o)<<POS_OP) \
118*eda14cbcSMatt Macy 			| (cast(Instruction, a)<<POS_A) \
119*eda14cbcSMatt Macy 			| (cast(Instruction, b)<<POS_B) \
120*eda14cbcSMatt Macy 			| (cast(Instruction, c)<<POS_C))
121*eda14cbcSMatt Macy 
122*eda14cbcSMatt Macy #define CREATE_ABx(o,a,bc)	((cast(Instruction, o)<<POS_OP) \
123*eda14cbcSMatt Macy 			| (cast(Instruction, a)<<POS_A) \
124*eda14cbcSMatt Macy 			| (cast(Instruction, bc)<<POS_Bx))
125*eda14cbcSMatt Macy 
126*eda14cbcSMatt Macy #define CREATE_Ax(o,a)		((cast(Instruction, o)<<POS_OP) \
127*eda14cbcSMatt Macy 			| (cast(Instruction, a)<<POS_Ax))
128*eda14cbcSMatt Macy 
129*eda14cbcSMatt Macy 
130*eda14cbcSMatt Macy /*
131*eda14cbcSMatt Macy ** Macros to operate RK indices
132*eda14cbcSMatt Macy */
133*eda14cbcSMatt Macy 
134*eda14cbcSMatt Macy /* this bit 1 means constant (0 means register) */
135*eda14cbcSMatt Macy #define BITRK		(1 << (SIZE_B - 1))
136*eda14cbcSMatt Macy 
137*eda14cbcSMatt Macy /* test whether value is a constant */
138*eda14cbcSMatt Macy #define ISK(x)		((x) & BITRK)
139*eda14cbcSMatt Macy 
140*eda14cbcSMatt Macy /* gets the index of the constant */
141*eda14cbcSMatt Macy #define INDEXK(r)	((int)(r) & ~BITRK)
142*eda14cbcSMatt Macy 
143*eda14cbcSMatt Macy #define MAXINDEXRK	(BITRK - 1)
144*eda14cbcSMatt Macy 
145*eda14cbcSMatt Macy /* code a constant index as a RK value */
146*eda14cbcSMatt Macy #define RKASK(x)	((x) | BITRK)
147*eda14cbcSMatt Macy 
148*eda14cbcSMatt Macy 
149*eda14cbcSMatt Macy /*
150*eda14cbcSMatt Macy ** invalid register that fits in 8 bits
151*eda14cbcSMatt Macy */
152*eda14cbcSMatt Macy #define NO_REG		MAXARG_A
153*eda14cbcSMatt Macy 
154*eda14cbcSMatt Macy 
155*eda14cbcSMatt Macy /*
156*eda14cbcSMatt Macy ** R(x) - register
157*eda14cbcSMatt Macy ** Kst(x) - constant (in constant table)
158*eda14cbcSMatt Macy ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
159*eda14cbcSMatt Macy */
160*eda14cbcSMatt Macy 
161*eda14cbcSMatt Macy 
162*eda14cbcSMatt Macy /*
163*eda14cbcSMatt Macy ** grep "ORDER OP" if you change these enums
164*eda14cbcSMatt Macy */
165*eda14cbcSMatt Macy 
166*eda14cbcSMatt Macy typedef enum {
167*eda14cbcSMatt Macy /*----------------------------------------------------------------------
168*eda14cbcSMatt Macy name		args	description
169*eda14cbcSMatt Macy ------------------------------------------------------------------------*/
170*eda14cbcSMatt Macy OP_MOVE,/*	A B	R(A) := R(B)					*/
171*eda14cbcSMatt Macy OP_LOADK,/*	A Bx	R(A) := Kst(Bx)					*/
172*eda14cbcSMatt Macy OP_LOADKX,/*	A 	R(A) := Kst(extra arg)				*/
173*eda14cbcSMatt Macy OP_LOADBOOL,/*	A B C	R(A) := (Bool)B; if (C) pc++			*/
174*eda14cbcSMatt Macy OP_LOADNIL,/*	A B	R(A), R(A+1), ..., R(A+B) := nil		*/
175*eda14cbcSMatt Macy OP_GETUPVAL,/*	A B	R(A) := UpValue[B]				*/
176*eda14cbcSMatt Macy 
177*eda14cbcSMatt Macy OP_GETTABUP,/*	A B C	R(A) := UpValue[B][RK(C)]			*/
178*eda14cbcSMatt Macy OP_GETTABLE,/*	A B C	R(A) := R(B)[RK(C)]				*/
179*eda14cbcSMatt Macy 
180*eda14cbcSMatt Macy OP_SETTABUP,/*	A B C	UpValue[A][RK(B)] := RK(C)			*/
181*eda14cbcSMatt Macy OP_SETUPVAL,/*	A B	UpValue[B] := R(A)				*/
182*eda14cbcSMatt Macy OP_SETTABLE,/*	A B C	R(A)[RK(B)] := RK(C)				*/
183*eda14cbcSMatt Macy 
184*eda14cbcSMatt Macy OP_NEWTABLE,/*	A B C	R(A) := {} (size = B,C)				*/
185*eda14cbcSMatt Macy 
186*eda14cbcSMatt Macy OP_SELF,/*	A B C	R(A+1) := R(B); R(A) := R(B)[RK(C)]		*/
187*eda14cbcSMatt Macy 
188*eda14cbcSMatt Macy OP_ADD,/*	A B C	R(A) := RK(B) + RK(C)				*/
189*eda14cbcSMatt Macy OP_SUB,/*	A B C	R(A) := RK(B) - RK(C)				*/
190*eda14cbcSMatt Macy OP_MUL,/*	A B C	R(A) := RK(B) * RK(C)				*/
191*eda14cbcSMatt Macy OP_DIV,/*	A B C	R(A) := RK(B) / RK(C)				*/
192*eda14cbcSMatt Macy OP_MOD,/*	A B C	R(A) := RK(B) % RK(C)				*/
193*eda14cbcSMatt Macy OP_POW,/*	A B C	R(A) := RK(B) ^ RK(C)				*/
194*eda14cbcSMatt Macy OP_UNM,/*	A B	R(A) := -R(B)					*/
195*eda14cbcSMatt Macy OP_NOT,/*	A B	R(A) := not R(B)				*/
196*eda14cbcSMatt Macy OP_LEN,/*	A B	R(A) := length of R(B)				*/
197*eda14cbcSMatt Macy 
198*eda14cbcSMatt Macy OP_CONCAT,/*	A B C	R(A) := R(B).. ... ..R(C)			*/
199*eda14cbcSMatt Macy 
200*eda14cbcSMatt Macy OP_JMP,/*	A sBx	pc+=sBx; if (A) close all upvalues >= R(A - 1)	*/
201*eda14cbcSMatt Macy OP_EQ,/*	A B C	if ((RK(B) == RK(C)) ~= A) then pc++		*/
202*eda14cbcSMatt Macy OP_LT,/*	A B C	if ((RK(B) <  RK(C)) ~= A) then pc++		*/
203*eda14cbcSMatt Macy OP_LE,/*	A B C	if ((RK(B) <= RK(C)) ~= A) then pc++		*/
204*eda14cbcSMatt Macy 
205*eda14cbcSMatt Macy OP_TEST,/*	A C	if not (R(A) <=> C) then pc++			*/
206*eda14cbcSMatt Macy OP_TESTSET,/*	A B C	if (R(B) <=> C) then R(A) := R(B) else pc++	*/
207*eda14cbcSMatt Macy 
208*eda14cbcSMatt Macy OP_CALL,/*	A B C	R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
209*eda14cbcSMatt Macy OP_TAILCALL,/*	A B C	return R(A)(R(A+1), ... ,R(A+B-1))		*/
210*eda14cbcSMatt Macy OP_RETURN,/*	A B	return R(A), ... ,R(A+B-2)	(see note)	*/
211*eda14cbcSMatt Macy 
212*eda14cbcSMatt Macy OP_FORLOOP,/*	A sBx	R(A)+=R(A+2);
213*eda14cbcSMatt Macy 			if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
214*eda14cbcSMatt Macy OP_FORPREP,/*	A sBx	R(A)-=R(A+2); pc+=sBx				*/
215*eda14cbcSMatt Macy 
216*eda14cbcSMatt Macy OP_TFORCALL,/*	A C	R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));	*/
217*eda14cbcSMatt Macy OP_TFORLOOP,/*	A sBx	if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
218*eda14cbcSMatt Macy 
219*eda14cbcSMatt Macy OP_SETLIST,/*	A B C	R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B	*/
220*eda14cbcSMatt Macy 
221*eda14cbcSMatt Macy OP_CLOSURE,/*	A Bx	R(A) := closure(KPROTO[Bx])			*/
222*eda14cbcSMatt Macy 
223*eda14cbcSMatt Macy OP_VARARG,/*	A B	R(A), R(A+1), ..., R(A+B-2) = vararg		*/
224*eda14cbcSMatt Macy 
225*eda14cbcSMatt Macy OP_EXTRAARG/*	Ax	extra (larger) argument for previous opcode	*/
226*eda14cbcSMatt Macy } OpCode;
227*eda14cbcSMatt Macy 
228*eda14cbcSMatt Macy 
229*eda14cbcSMatt Macy #define NUM_OPCODES	(cast(int, OP_EXTRAARG) + 1)
230*eda14cbcSMatt Macy 
231*eda14cbcSMatt Macy 
232*eda14cbcSMatt Macy 
233*eda14cbcSMatt Macy /*===========================================================================
234*eda14cbcSMatt Macy   Notes:
235*eda14cbcSMatt Macy   (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is
236*eda14cbcSMatt Macy   set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
237*eda14cbcSMatt Macy   OP_SETLIST) may use `top'.
238*eda14cbcSMatt Macy 
239*eda14cbcSMatt Macy   (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
240*eda14cbcSMatt Macy   set top (like in OP_CALL with C == 0).
241*eda14cbcSMatt Macy 
242*eda14cbcSMatt Macy   (*) In OP_RETURN, if (B == 0) then return up to `top'.
243*eda14cbcSMatt Macy 
244*eda14cbcSMatt Macy   (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
245*eda14cbcSMatt Macy   'instruction' is EXTRAARG(real C).
246*eda14cbcSMatt Macy 
247*eda14cbcSMatt Macy   (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
248*eda14cbcSMatt Macy 
249*eda14cbcSMatt Macy   (*) For comparisons, A specifies what condition the test should accept
250*eda14cbcSMatt Macy   (true or false).
251*eda14cbcSMatt Macy 
252*eda14cbcSMatt Macy   (*) All `skips' (pc++) assume that next instruction is a jump.
253*eda14cbcSMatt Macy 
254*eda14cbcSMatt Macy ===========================================================================*/
255*eda14cbcSMatt Macy 
256*eda14cbcSMatt Macy 
257*eda14cbcSMatt Macy /*
258*eda14cbcSMatt Macy ** masks for instruction properties. The format is:
259*eda14cbcSMatt Macy ** bits 0-1: op mode
260*eda14cbcSMatt Macy ** bits 2-3: C arg mode
261*eda14cbcSMatt Macy ** bits 4-5: B arg mode
262*eda14cbcSMatt Macy ** bit 6: instruction set register A
263*eda14cbcSMatt Macy ** bit 7: operator is a test (next instruction must be a jump)
264*eda14cbcSMatt Macy */
265*eda14cbcSMatt Macy 
266*eda14cbcSMatt Macy enum OpArgMask {
267*eda14cbcSMatt Macy   OpArgN,  /* argument is not used */
268*eda14cbcSMatt Macy   OpArgU,  /* argument is used */
269*eda14cbcSMatt Macy   OpArgR,  /* argument is a register or a jump offset */
270*eda14cbcSMatt Macy   OpArgK   /* argument is a constant or register/constant */
271*eda14cbcSMatt Macy };
272*eda14cbcSMatt Macy 
273*eda14cbcSMatt Macy LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
274*eda14cbcSMatt Macy 
275*eda14cbcSMatt Macy #define getOpMode(m)	(cast(enum OpMode, luaP_opmodes[m] & 3))
276*eda14cbcSMatt Macy #define getBMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
277*eda14cbcSMatt Macy #define getCMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
278*eda14cbcSMatt Macy #define testAMode(m)	(luaP_opmodes[m] & (1 << 6))
279*eda14cbcSMatt Macy #define testTMode(m)	(luaP_opmodes[m] & (1 << 7))
280*eda14cbcSMatt Macy 
281*eda14cbcSMatt Macy 
282*eda14cbcSMatt Macy LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
283*eda14cbcSMatt Macy 
284*eda14cbcSMatt Macy 
285*eda14cbcSMatt Macy /* number of list items to accumulate before a SETLIST instruction */
286*eda14cbcSMatt Macy #define LFIELDS_PER_FLUSH	50
287*eda14cbcSMatt Macy 
288*eda14cbcSMatt Macy 
289*eda14cbcSMatt Macy #endif
290*eda14cbcSMatt Macy /* END CSTYLED */
291