release.sh changes & fixes
[minix3.git] / external / mit / lua / dist / src / lopcodes.h
blob83351d4a40cafadfda04dc1deb5c9540941a3350
1 /* $NetBSD: lopcodes.h,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $ */
3 /*
4 ** $Id: lopcodes.h,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $
5 ** Opcodes for Lua virtual machine
6 ** See Copyright Notice in lua.h
7 */
9 #ifndef lopcodes_h
10 #define lopcodes_h
12 #include "llimits.h"
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
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 ===========================================================================*/
33 enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
37 ** size and position of opcode arguments.
39 #define SIZE_C 9
40 #define SIZE_B 9
41 #define SIZE_Bx (SIZE_C + SIZE_B)
42 #define SIZE_A 8
44 #define SIZE_OP 6
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
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)
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
67 #define MAXARG_A ((1<<SIZE_A)-1)
68 #define MAXARG_B ((1<<SIZE_B)-1)
69 #define MAXARG_C ((1<<SIZE_C)-1)
72 /* creates a mask with `n' 1 bits at position `p' */
73 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
75 /* creates a mask with `n' 0 bits at position `p' */
76 #define MASK0(n,p) (~MASK1(n,p))
79 ** the following macros help to manipulate instructions
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))))
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))))
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))))
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))))
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))))
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))
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))
111 #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
112 | (cast(Instruction, a)<<POS_A) \
113 | (cast(Instruction, bc)<<POS_Bx))
117 ** Macros to operate RK indices
120 /* this bit 1 means constant (0 means register) */
121 #define BITRK (1 << (SIZE_B - 1))
123 /* test whether value is a constant */
124 #define ISK(x) ((x) & BITRK)
126 /* gets the index of the constant */
127 #define INDEXK(r) ((int)(r) & ~BITRK)
129 #define MAXINDEXRK (BITRK - 1)
131 /* code a constant index as a RK value */
132 #define RKASK(x) ((x) | BITRK)
136 ** invalid register that fits in 8 bits
138 #define NO_REG MAXARG_A
142 ** R(x) - register
143 ** Kst(x) - constant (in constant table)
144 ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
149 ** grep "ORDER OP" if you change these enums
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] */
162 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
163 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
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) */
169 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
171 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
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) */
183 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
185 OP_JMP,/* sBx pc+=sBx */
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++ */
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++ */
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) */
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 */
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 */
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)) */
209 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
210 } OpCode;
213 #define NUM_OPCODES (cast(int, OP_VARARG) + 1)
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'.
223 (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
224 set top (like in OP_CALL with C == 0).
226 (*) In OP_RETURN, if (B == 0) then return up to `top'
228 (*) In OP_SETLIST, if (B == 0) then B = `top';
229 if (C == 0) then next `instruction' is real C
231 (*) For comparisons, A specifies what condition the test should accept
232 (true or false).
234 (*) All `skips' (pc++) assume that next instruction is a jump
235 ===========================================================================*/
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
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 */
254 LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
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))
263 LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
266 /* number of list items to accumulate before a SETLIST instruction */
267 #define LFIELDS_PER_FLUSH 50
270 #endif