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