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