rpm  4.5
lopcodes.h
Go to the documentation of this file.
1 /*
2 ** $Id: lopcodes.h,v 1.1 2004/03/16 21:58:30 niemeyer Exp $
3 ** Opcodes for Lua virtual machine
4 ** See Copyright Notice in lua.h
5 */
6 
7 #ifndef lopcodes_h
8 #define lopcodes_h
9 
10 #include "llimits.h"
11 
12 
13 /*===========================================================================
14  We assume that instructions are unsigned numbers.
15  All instructions have an opcode in the first 6 bits.
16  Instructions can have the following fields:
17  `A' : 8 bits
18  `B' : 9 bits
19  `C' : 9 bits
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}; /* 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 
42 #define SIZE_OP 6
43 
44 #define POS_C SIZE_OP
45 #define POS_B (POS_C + SIZE_C)
46 #define POS_Bx POS_C
47 #define POS_A (POS_B + SIZE_B)
48 
49 
50 /*
51 ** limits for opcode arguments.
52 ** we use (signed) int to manipulate most arguments,
53 ** so they must fit in BITS_INT-1 bits (-1 for sign)
54 */
55 #if SIZE_Bx < BITS_INT-1
56 #define MAXARG_Bx ((1<<SIZE_Bx)-1)
57 #define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
58 #else
59 #define MAXARG_Bx MAX_INT
60 #define MAXARG_sBx MAX_INT
61 #endif
62 
63 
64 #define MAXARG_A ((1<<SIZE_A)-1)
65 #define MAXARG_B ((1<<SIZE_B)-1)
66 #define MAXARG_C ((1<<SIZE_C)-1)
67 
68 
69 /* creates a mask with `n' 1 bits at position `p' */
70 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
71 
72 /* creates a mask with `n' 0 bits at position `p' */
73 #define MASK0(n,p) (~MASK1(n,p))
74 
75 /*
76 ** the following macros help to manipulate instructions
77 */
78 
79 #define GET_OPCODE(i) (cast(OpCode, (i)&MASK1(SIZE_OP,0)))
80 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | cast(Instruction, o)))
81 
82 #define GETARG_A(i) (cast(int, (i)>>POS_A))
83 #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
84  ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
85 
86 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
87 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
88  ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
89 
90 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
91 #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
92  ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
93 
94 #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
95 #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
96  ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
97 
98 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
99 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
100 
101 
102 #define CREATE_ABC(o,a,b,c) (cast(Instruction, o) \
103  | (cast(Instruction, a)<<POS_A) \
104  | (cast(Instruction, b)<<POS_B) \
105  | (cast(Instruction, c)<<POS_C))
106 
107 #define CREATE_ABx(o,a,bc) (cast(Instruction, o) \
108  | (cast(Instruction, a)<<POS_A) \
109  | (cast(Instruction, bc)<<POS_Bx))
110 
111 
112 
113 
114 /*
115 ** invalid register that fits in 8 bits
116 */
117 #define NO_REG MAXARG_A
118 
119 
120 /*
121 ** R(x) - register
122 ** Kst(x) - constant (in constant table)
123 ** RK(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
124 */
125 
126 
127 /*
128 ** grep "ORDER OP" if you change these enums
129 */
130 
131 typedef enum {
132 /*----------------------------------------------------------------------
133 name args description
134 ------------------------------------------------------------------------*/
135 OP_MOVE,/* A B R(A) := R(B) */
136 OP_LOADK,/* A Bx R(A) := Kst(Bx) */
137 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) PC++ */
138 OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
139 OP_GETUPVAL,/* A B R(A) := UpValue[B] */
140 
141 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
142 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
143 
144 OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
145 OP_SETUPVAL,/* A B UpValue[B] := R(A) */
146 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
147 
148 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
149 
150 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
151 
152 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
153 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
154 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
155 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
156 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
157 OP_UNM,/* A B R(A) := -R(B) */
158 OP_NOT,/* A B R(A) := not R(B) */
159 
160 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
161 
162 OP_JMP,/* sBx PC += sBx */
163 
164 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
165 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
166 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
167 
168 OP_TEST,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
169 
170 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
171 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
172 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
173 
174 OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */
175 
176 OP_TFORLOOP,/* A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
177  if R(A+2) ~= nil then pc++ */
178 OP_TFORPREP,/* A sBx if type(R(A)) == table then R(A+1):=R(A), R(A):=next;
179  PC += sBx */
180 
181 OP_SETLIST,/* A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1 */
182 OP_SETLISTO,/* A Bx */
183 
184 OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
185 OP_CLOSURE/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
186 } OpCode;
187 
188 
189 #define NUM_OPCODES (cast(int, OP_CLOSURE+1))
190 
191 
192 
193 /*===========================================================================
194  Notes:
195  (1) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
196  and can be 0: OP_CALL then sets `top' to last_result+1, so
197  next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
198 
199  (2) In OP_RETURN, if (B == 0) then return up to `top'
200 
201  (3) For comparisons, B specifies what conditions the test should accept.
202 
203  (4) All `skips' (pc++) assume that next instruction is a jump
204 ===========================================================================*/
205 
206 
207 /*
208 ** masks for instruction properties
209 */
211  OpModeBreg = 2, /* B is a register */
212  OpModeBrk, /* B is a register/constant */
213  OpModeCrk, /* C is a register/constant */
214  OpModesetA, /* instruction set register A */
215  OpModeK, /* Bx is a constant */
216  OpModeT /* operator is a test */
217 
218 };
219 
220 
221 /*@unchecked@*/
222 extern const lu_byte luaP_opmodes[NUM_OPCODES];
223 
224 #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
225 #define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
226 
227 
228 #ifdef LUA_OPNAMES
229 extern const char *const luaP_opnames[]; /* opcode names */
230 #endif
231 
232 
233 
234 /* number of list items to accumulate before a SETLIST instruction */
235 /* (must be a power of 2) */
236 #define LFIELDS_PER_FLUSH 32
237 
238 
239 #endif