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