66 #define MAXARG_B ((1<<SIZE_B)-1) |
66 #define MAXARG_B ((1<<SIZE_B)-1) |
67 #define MAXARG_C ((1<<SIZE_C)-1) |
67 #define MAXARG_C ((1<<SIZE_C)-1) |
68 |
68 |
69 |
69 |
70 /* creates a mask with `n' 1 bits at position `p' */ |
70 /* creates a mask with `n' 1 bits at position `p' */ |
71 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p) |
71 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p) |
72 |
72 |
73 /* creates a mask with `n' 0 bits at position `p' */ |
73 /* creates a mask with `n' 0 bits at position `p' */ |
74 #define MASK0(n,p) (~MASK1(n,p)) |
74 #define MASK0(n,p) (~MASK1(n,p)) |
75 |
75 |
76 /* |
76 /* |
77 ** the following macros help to manipulate instructions |
77 ** the following macros help to manipulate instructions |
78 */ |
78 */ |
79 |
79 |
80 #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) |
80 #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) |
81 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ |
81 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ |
82 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) |
82 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) |
83 |
83 |
84 #define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0))) |
84 #define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0))) |
85 #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ |
85 #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ |
86 ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A)))) |
86 ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A)))) |
87 |
87 |
88 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) |
88 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) |
89 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ |
89 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ |
90 ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) |
90 ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) |
91 |
91 |
92 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) |
92 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) |
93 #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ |
93 #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ |
94 ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) |
94 ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) |
95 |
95 |
96 #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) |
96 #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) |
97 #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ |
97 #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ |
98 ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) |
98 ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) |
99 |
99 |
100 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) |
100 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) |
101 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) |
101 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) |
102 |
102 |
103 |
103 |
104 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ |
104 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ |
105 | (cast(Instruction, a)<<POS_A) \ |
105 | (cast(Instruction, a)<<POS_A) \ |
106 | (cast(Instruction, b)<<POS_B) \ |
106 | (cast(Instruction, b)<<POS_B) \ |
107 | (cast(Instruction, c)<<POS_C)) |
107 | (cast(Instruction, c)<<POS_C)) |
108 |
108 |
109 #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ |
109 #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ |
110 | (cast(Instruction, a)<<POS_A) \ |
110 | (cast(Instruction, a)<<POS_A) \ |
111 | (cast(Instruction, bc)<<POS_Bx)) |
111 | (cast(Instruction, bc)<<POS_Bx)) |
112 |
112 |
113 |
113 |
114 /* |
114 /* |
115 ** Macros to operate RK indices |
115 ** Macros to operate RK indices |
116 */ |
116 */ |
117 |
117 |
118 /* this bit 1 means constant (0 means register) */ |
118 /* this bit 1 means constant (0 means register) */ |
119 #define BITRK (1 << (SIZE_B - 1)) |
119 #define BITRK (1 << (SIZE_B - 1)) |
120 |
120 |
121 /* test whether value is a constant */ |
121 /* test whether value is a constant */ |
122 #define ISK(x) ((x) & BITRK) |
122 #define ISK(x) ((x) & BITRK) |
123 |
123 |
124 /* gets the index of the constant */ |
124 /* gets the index of the constant */ |
125 #define INDEXK(r) ((int)(r) & ~BITRK) |
125 #define INDEXK(r) ((int)(r) & ~BITRK) |
126 |
126 |
127 #define MAXINDEXRK (BITRK - 1) |
127 #define MAXINDEXRK (BITRK - 1) |
128 |
128 |
129 /* code a constant index as a RK value */ |
129 /* code a constant index as a RK value */ |
130 #define RKASK(x) ((x) | BITRK) |
130 #define RKASK(x) ((x) | BITRK) |
131 |
131 |
132 |
132 |
133 /* |
133 /* |
134 ** invalid register that fits in 8 bits |
134 ** invalid register that fits in 8 bits |
135 */ |
135 */ |
136 #define NO_REG MAXARG_A |
136 #define NO_REG MAXARG_A |
137 |
137 |
138 |
138 |
139 /* |
139 /* |
140 ** R(x) - register |
140 ** R(x) - register |
141 ** Kst(x) - constant (in constant table) |
141 ** Kst(x) - constant (in constant table) |
147 ** grep "ORDER OP" if you change these enums |
147 ** grep "ORDER OP" if you change these enums |
148 */ |
148 */ |
149 |
149 |
150 typedef enum { |
150 typedef enum { |
151 /*---------------------------------------------------------------------- |
151 /*---------------------------------------------------------------------- |
152 name args description |
152 name args description |
153 ------------------------------------------------------------------------*/ |
153 ------------------------------------------------------------------------*/ |
154 OP_MOVE,/* A B R(A) := R(B) */ |
154 OP_MOVE,/* A B R(A) := R(B) */ |
155 OP_LOADK,/* A Bx R(A) := Kst(Bx) */ |
155 OP_LOADK,/* A Bx R(A) := Kst(Bx) */ |
156 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ |
156 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ |
157 OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ |
157 OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ |
158 OP_GETUPVAL,/* A B R(A) := UpValue[B] */ |
158 OP_GETUPVAL,/* A B R(A) := UpValue[B] */ |
159 |
159 |
160 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ |
160 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ |
161 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ |
161 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ |
162 |
162 |
163 OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ |
163 OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ |
164 OP_SETUPVAL,/* A B UpValue[B] := R(A) */ |
164 OP_SETUPVAL,/* A B UpValue[B] := R(A) */ |
165 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ |
165 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ |
166 |
166 |
167 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ |
167 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ |
168 |
168 |
169 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ |
169 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ |
170 |
170 |
171 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ |
171 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ |
172 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ |
172 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ |
173 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ |
173 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ |
174 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ |
174 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ |
175 OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ |
175 OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ |
176 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ |
176 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ |
177 OP_UNM,/* A B R(A) := -R(B) */ |
177 OP_UNM,/* A B R(A) := -R(B) */ |
178 OP_NOT,/* A B R(A) := not R(B) */ |
178 OP_NOT,/* A B R(A) := not R(B) */ |
179 OP_LEN,/* A B R(A) := length of R(B) */ |
179 OP_LEN,/* A B R(A) := length of R(B) */ |
180 |
180 |
181 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ |
181 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ |
182 |
182 |
183 OP_JMP,/* sBx pc+=sBx */ |
183 OP_JMP,/* sBx pc+=sBx */ |
184 |
184 |
185 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ |
185 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ |
186 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ |
186 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ |
187 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ |
187 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ |
188 |
188 |
189 OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ |
189 OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ |
190 OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ |
190 OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ |
191 |
191 |
192 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ |
192 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ |
193 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ |
193 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ |
194 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ |
194 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ |
195 |
195 |
196 OP_FORLOOP,/* A sBx R(A)+=R(A+2); |
196 OP_FORLOOP,/* A sBx R(A)+=R(A+2); |
197 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ |
197 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ |
198 OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ |
198 OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ |
199 |
199 |
200 OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); |
200 OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); |
201 if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ |
201 if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ |
202 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ |
202 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ |
203 |
203 |
204 OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ |
204 OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ |
205 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ |
205 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ |
206 |
206 |
207 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ |
207 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ |
208 } OpCode; |
208 } OpCode; |
209 |
209 |
210 |
210 |
211 #define NUM_OPCODES (cast(int, OP_VARARG) + 1) |
211 #define NUM_OPCODES (cast(int, OP_VARARG) + 1) |
212 |
212 |
213 |
213 |
214 |
214 |
215 /*=========================================================================== |
215 /*=========================================================================== |
216 Notes: |
216 Notes: |