1 | /* |
---|
2 | ** $Id: lcode.c,v 1.117a 2003/04/03 13:35:34 roberto Exp $ |
---|
3 | ** Code generator for Lua |
---|
4 | ** See Copyright Notice in lua.h |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | #include <stdlib.h> |
---|
9 | |
---|
10 | #define lcode_c |
---|
11 | |
---|
12 | #include "lua.h" |
---|
13 | |
---|
14 | #include "lcode.h" |
---|
15 | #include "ldebug.h" |
---|
16 | #include "ldo.h" |
---|
17 | #include "llex.h" |
---|
18 | #include "lmem.h" |
---|
19 | #include "lobject.h" |
---|
20 | #include "lopcodes.h" |
---|
21 | #include "lparser.h" |
---|
22 | #include "ltable.h" |
---|
23 | |
---|
24 | |
---|
25 | #define hasjumps(e) ((e)->t != (e)->f) |
---|
26 | |
---|
27 | |
---|
28 | void luaK_nil (FuncState *fs, int from, int n) { |
---|
29 | Instruction *previous; |
---|
30 | if (fs->pc > fs->lasttarget && /* no jumps to current position? */ |
---|
31 | GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) { |
---|
32 | int pfrom = GETARG_A(*previous); |
---|
33 | int pto = GETARG_B(*previous); |
---|
34 | if (pfrom <= from && from <= pto+1) { /* can connect both? */ |
---|
35 | if (from+n-1 > pto) |
---|
36 | SETARG_B(*previous, from+n-1); |
---|
37 | return; |
---|
38 | } |
---|
39 | } |
---|
40 | luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | int luaK_jump (FuncState *fs) { |
---|
45 | int jpc = fs->jpc; /* save list of jumps to here */ |
---|
46 | int j; |
---|
47 | fs->jpc = NO_JUMP; |
---|
48 | j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); |
---|
49 | luaK_concat(fs, &j, jpc); /* keep them on hold */ |
---|
50 | return j; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) { |
---|
55 | luaK_codeABC(fs, op, A, B, C); |
---|
56 | return luaK_jump(fs); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | static void luaK_fixjump (FuncState *fs, int pc, int dest) { |
---|
61 | Instruction *jmp = &fs->f->code[pc]; |
---|
62 | int offset = dest-(pc+1); |
---|
63 | lua_assert(dest != NO_JUMP); |
---|
64 | if (abs(offset) > MAXARG_sBx) |
---|
65 | luaX_syntaxerror(fs->ls, "control structure too long"); |
---|
66 | SETARG_sBx(*jmp, offset); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /* |
---|
71 | ** returns current `pc' and marks it as a jump target (to avoid wrong |
---|
72 | ** optimizations with consecutive instructions not in the same basic block). |
---|
73 | */ |
---|
74 | int luaK_getlabel (FuncState *fs) { |
---|
75 | fs->lasttarget = fs->pc; |
---|
76 | return fs->pc; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | static int luaK_getjump (FuncState *fs, int pc) { |
---|
81 | int offset = GETARG_sBx(fs->f->code[pc]); |
---|
82 | if (offset == NO_JUMP) /* point to itself represents end of list */ |
---|
83 | return NO_JUMP; /* end of list */ |
---|
84 | else |
---|
85 | return (pc+1)+offset; /* turn offset into absolute position */ |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | static Instruction *getjumpcontrol (FuncState *fs, int pc) { |
---|
90 | Instruction *pi = &fs->f->code[pc]; |
---|
91 | if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT)) |
---|
92 | return pi-1; |
---|
93 | else |
---|
94 | return pi; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* |
---|
99 | ** check whether list has any jump that do not produce a value |
---|
100 | ** (or produce an inverted value) |
---|
101 | */ |
---|
102 | static int need_value (FuncState *fs, int list, int cond) { |
---|
103 | for (; list != NO_JUMP; list = luaK_getjump(fs, list)) { |
---|
104 | Instruction i = *getjumpcontrol(fs, list); |
---|
105 | if (GET_OPCODE(i) != OP_TEST || |
---|
106 | GETARG_A(i) != NO_REG || |
---|
107 | GETARG_C(i) != cond) |
---|
108 | return 1; |
---|
109 | } |
---|
110 | return 0; /* not found */ |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | static void patchtestreg (Instruction *i, int reg) { |
---|
115 | if (reg == NO_REG) reg = GETARG_B(*i); |
---|
116 | SETARG_A(*i, reg); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | static void removevalues (FuncState *fs, int list) { |
---|
121 | for (; list != NO_JUMP; list = luaK_getjump(fs, list)) { |
---|
122 | Instruction *i = getjumpcontrol(fs, list); |
---|
123 | if (GET_OPCODE(*i) == OP_TEST) |
---|
124 | patchtestreg(i, NO_REG); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | static void luaK_patchlistaux (FuncState *fs, int list, int vtarget, int reg, |
---|
130 | int dtarget) { |
---|
131 | while (list != NO_JUMP) { |
---|
132 | int next = luaK_getjump(fs, list); |
---|
133 | Instruction *i = getjumpcontrol(fs, list); |
---|
134 | if (GET_OPCODE(*i) == OP_TEST && GETARG_A(*i) == NO_REG) { |
---|
135 | patchtestreg(i, reg); |
---|
136 | luaK_fixjump(fs, list, vtarget); |
---|
137 | } |
---|
138 | else |
---|
139 | luaK_fixjump(fs, list, dtarget); /* jump to default target */ |
---|
140 | list = next; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | static void luaK_dischargejpc (FuncState *fs) { |
---|
146 | luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); |
---|
147 | fs->jpc = NO_JUMP; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | void luaK_patchlist (FuncState *fs, int list, int target) { |
---|
152 | if (target == fs->pc) |
---|
153 | luaK_patchtohere(fs, list); |
---|
154 | else { |
---|
155 | lua_assert(target < fs->pc); |
---|
156 | luaK_patchlistaux(fs, list, target, NO_REG, target); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void luaK_patchtohere (FuncState *fs, int list) { |
---|
162 | luaK_getlabel(fs); |
---|
163 | luaK_concat(fs, &fs->jpc, list); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void luaK_concat (FuncState *fs, int *l1, int l2) { |
---|
168 | if (l2 == NO_JUMP) return; |
---|
169 | else if (*l1 == NO_JUMP) |
---|
170 | *l1 = l2; |
---|
171 | else { |
---|
172 | int list = *l1; |
---|
173 | int next; |
---|
174 | while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */ |
---|
175 | list = next; |
---|
176 | luaK_fixjump(fs, list, l2); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | void luaK_checkstack (FuncState *fs, int n) { |
---|
182 | int newstack = fs->freereg + n; |
---|
183 | if (newstack > fs->f->maxstacksize) { |
---|
184 | if (newstack >= MAXSTACK) |
---|
185 | luaX_syntaxerror(fs->ls, "function or expression too complex"); |
---|
186 | fs->f->maxstacksize = cast(lu_byte, newstack); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | void luaK_reserveregs (FuncState *fs, int n) { |
---|
192 | luaK_checkstack(fs, n); |
---|
193 | fs->freereg += n; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | static void freereg (FuncState *fs, int reg) { |
---|
198 | if (reg >= fs->nactvar && reg < MAXSTACK) { |
---|
199 | fs->freereg--; |
---|
200 | lua_assert(reg == fs->freereg); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | static void freeexp (FuncState *fs, expdesc *e) { |
---|
206 | if (e->k == VNONRELOC) |
---|
207 | freereg(fs, e->info); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | static int addk (FuncState *fs, TObject *k, TObject *v) { |
---|
212 | const TObject *idx = luaH_get(fs->h, k); |
---|
213 | if (ttisnumber(idx)) { |
---|
214 | lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(idx))], v)); |
---|
215 | return cast(int, nvalue(idx)); |
---|
216 | } |
---|
217 | else { /* constant not found; create a new entry */ |
---|
218 | Proto *f = fs->f; |
---|
219 | luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject, |
---|
220 | MAXARG_Bx, "constant table overflow"); |
---|
221 | setobj2n(&f->k[fs->nk], v); |
---|
222 | setnvalue(luaH_set(fs->L, fs->h, k), cast(lua_Number, fs->nk)); |
---|
223 | return fs->nk++; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | int luaK_stringK (FuncState *fs, TString *s) { |
---|
229 | TObject o; |
---|
230 | setsvalue(&o, s); |
---|
231 | return addk(fs, &o, &o); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | int luaK_numberK (FuncState *fs, lua_Number r) { |
---|
236 | TObject o; |
---|
237 | setnvalue(&o, r); |
---|
238 | return addk(fs, &o, &o); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | static int nil_constant (FuncState *fs) { |
---|
243 | TObject k, v; |
---|
244 | setnilvalue(&v); |
---|
245 | sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */ |
---|
246 | return addk(fs, &k, &v); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) { |
---|
251 | if (e->k == VCALL) { /* expression is an open function call? */ |
---|
252 | SETARG_C(getcode(fs, e), nresults+1); |
---|
253 | if (nresults == 1) { /* `regular' expression? */ |
---|
254 | e->k = VNONRELOC; |
---|
255 | e->info = GETARG_A(getcode(fs, e)); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | void luaK_dischargevars (FuncState *fs, expdesc *e) { |
---|
262 | switch (e->k) { |
---|
263 | case VLOCAL: { |
---|
264 | e->k = VNONRELOC; |
---|
265 | break; |
---|
266 | } |
---|
267 | case VUPVAL: { |
---|
268 | e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0); |
---|
269 | e->k = VRELOCABLE; |
---|
270 | break; |
---|
271 | } |
---|
272 | case VGLOBAL: { |
---|
273 | e->info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->info); |
---|
274 | e->k = VRELOCABLE; |
---|
275 | break; |
---|
276 | } |
---|
277 | case VINDEXED: { |
---|
278 | freereg(fs, e->aux); |
---|
279 | freereg(fs, e->info); |
---|
280 | e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux); |
---|
281 | e->k = VRELOCABLE; |
---|
282 | break; |
---|
283 | } |
---|
284 | case VCALL: { |
---|
285 | luaK_setcallreturns(fs, e, 1); |
---|
286 | break; |
---|
287 | } |
---|
288 | default: break; /* there is one value available (somewhere) */ |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | static int code_label (FuncState *fs, int A, int b, int jump) { |
---|
294 | luaK_getlabel(fs); /* those instructions may be jump targets */ |
---|
295 | return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | static void discharge2reg (FuncState *fs, expdesc *e, int reg) { |
---|
300 | luaK_dischargevars(fs, e); |
---|
301 | switch (e->k) { |
---|
302 | case VNIL: { |
---|
303 | luaK_nil(fs, reg, 1); |
---|
304 | break; |
---|
305 | } |
---|
306 | case VFALSE: case VTRUE: { |
---|
307 | luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); |
---|
308 | break; |
---|
309 | } |
---|
310 | case VK: { |
---|
311 | luaK_codeABx(fs, OP_LOADK, reg, e->info); |
---|
312 | break; |
---|
313 | } |
---|
314 | case VRELOCABLE: { |
---|
315 | Instruction *pc = &getcode(fs, e); |
---|
316 | SETARG_A(*pc, reg); |
---|
317 | break; |
---|
318 | } |
---|
319 | case VNONRELOC: { |
---|
320 | if (reg != e->info) |
---|
321 | luaK_codeABC(fs, OP_MOVE, reg, e->info, 0); |
---|
322 | break; |
---|
323 | } |
---|
324 | default: { |
---|
325 | lua_assert(e->k == VVOID || e->k == VJMP); |
---|
326 | return; /* nothing to do... */ |
---|
327 | } |
---|
328 | } |
---|
329 | e->info = reg; |
---|
330 | e->k = VNONRELOC; |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | static void discharge2anyreg (FuncState *fs, expdesc *e) { |
---|
335 | if (e->k != VNONRELOC) { |
---|
336 | luaK_reserveregs(fs, 1); |
---|
337 | discharge2reg(fs, e, fs->freereg-1); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) { |
---|
343 | discharge2reg(fs, e, reg); |
---|
344 | if (e->k == VJMP) |
---|
345 | luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */ |
---|
346 | if (hasjumps(e)) { |
---|
347 | int final; /* position after whole expression */ |
---|
348 | int p_f = NO_JUMP; /* position of an eventual LOAD false */ |
---|
349 | int p_t = NO_JUMP; /* position of an eventual LOAD true */ |
---|
350 | if (need_value(fs, e->t, 1) || need_value(fs, e->f, 0)) { |
---|
351 | int fj = NO_JUMP; /* first jump (over LOAD ops.) */ |
---|
352 | if (e->k != VJMP) |
---|
353 | fj = luaK_jump(fs); |
---|
354 | p_f = code_label(fs, reg, 0, 1); |
---|
355 | p_t = code_label(fs, reg, 1, 0); |
---|
356 | luaK_patchtohere(fs, fj); |
---|
357 | } |
---|
358 | final = luaK_getlabel(fs); |
---|
359 | luaK_patchlistaux(fs, e->f, final, reg, p_f); |
---|
360 | luaK_patchlistaux(fs, e->t, final, reg, p_t); |
---|
361 | } |
---|
362 | e->f = e->t = NO_JUMP; |
---|
363 | e->info = reg; |
---|
364 | e->k = VNONRELOC; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | void luaK_exp2nextreg (FuncState *fs, expdesc *e) { |
---|
369 | luaK_dischargevars(fs, e); |
---|
370 | freeexp(fs, e); |
---|
371 | luaK_reserveregs(fs, 1); |
---|
372 | luaK_exp2reg(fs, e, fs->freereg - 1); |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | int luaK_exp2anyreg (FuncState *fs, expdesc *e) { |
---|
377 | luaK_dischargevars(fs, e); |
---|
378 | if (e->k == VNONRELOC) { |
---|
379 | if (!hasjumps(e)) return e->info; /* exp is already in a register */ |
---|
380 | if (e->info >= fs->nactvar) { /* reg. is not a local? */ |
---|
381 | luaK_exp2reg(fs, e, e->info); /* put value on it */ |
---|
382 | return e->info; |
---|
383 | } |
---|
384 | } |
---|
385 | luaK_exp2nextreg(fs, e); /* default */ |
---|
386 | return e->info; |
---|
387 | } |
---|
388 | |
---|
389 | |
---|
390 | void luaK_exp2val (FuncState *fs, expdesc *e) { |
---|
391 | if (hasjumps(e)) |
---|
392 | luaK_exp2anyreg(fs, e); |
---|
393 | else |
---|
394 | luaK_dischargevars(fs, e); |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | int luaK_exp2RK (FuncState *fs, expdesc *e) { |
---|
399 | luaK_exp2val(fs, e); |
---|
400 | switch (e->k) { |
---|
401 | case VNIL: { |
---|
402 | if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */ |
---|
403 | e->info = nil_constant(fs); |
---|
404 | e->k = VK; |
---|
405 | return e->info + MAXSTACK; |
---|
406 | } |
---|
407 | else break; |
---|
408 | } |
---|
409 | case VK: { |
---|
410 | if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */ |
---|
411 | return e->info + MAXSTACK; |
---|
412 | else break; |
---|
413 | } |
---|
414 | default: break; |
---|
415 | } |
---|
416 | /* not a constant in the right range: put it in a register */ |
---|
417 | return luaK_exp2anyreg(fs, e); |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) { |
---|
422 | switch (var->k) { |
---|
423 | case VLOCAL: { |
---|
424 | freeexp(fs, exp); |
---|
425 | luaK_exp2reg(fs, exp, var->info); |
---|
426 | return; |
---|
427 | } |
---|
428 | case VUPVAL: { |
---|
429 | int e = luaK_exp2anyreg(fs, exp); |
---|
430 | luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0); |
---|
431 | break; |
---|
432 | } |
---|
433 | case VGLOBAL: { |
---|
434 | int e = luaK_exp2anyreg(fs, exp); |
---|
435 | luaK_codeABx(fs, OP_SETGLOBAL, e, var->info); |
---|
436 | break; |
---|
437 | } |
---|
438 | case VINDEXED: { |
---|
439 | int e = luaK_exp2RK(fs, exp); |
---|
440 | luaK_codeABC(fs, OP_SETTABLE, var->info, var->aux, e); |
---|
441 | break; |
---|
442 | } |
---|
443 | default: { |
---|
444 | lua_assert(0); /* invalid var kind to store */ |
---|
445 | break; |
---|
446 | } |
---|
447 | } |
---|
448 | freeexp(fs, exp); |
---|
449 | } |
---|
450 | |
---|
451 | |
---|
452 | void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { |
---|
453 | int func; |
---|
454 | luaK_exp2anyreg(fs, e); |
---|
455 | freeexp(fs, e); |
---|
456 | func = fs->freereg; |
---|
457 | luaK_reserveregs(fs, 2); |
---|
458 | luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key)); |
---|
459 | freeexp(fs, key); |
---|
460 | e->info = func; |
---|
461 | e->k = VNONRELOC; |
---|
462 | } |
---|
463 | |
---|
464 | |
---|
465 | static void invertjump (FuncState *fs, expdesc *e) { |
---|
466 | Instruction *pc = getjumpcontrol(fs, e->info); |
---|
467 | lua_assert(testOpMode(GET_OPCODE(*pc), OpModeT) && |
---|
468 | GET_OPCODE(*pc) != OP_TEST); |
---|
469 | SETARG_A(*pc, !(GETARG_A(*pc))); |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | static int jumponcond (FuncState *fs, expdesc *e, int cond) { |
---|
474 | if (e->k == VRELOCABLE) { |
---|
475 | Instruction ie = getcode(fs, e); |
---|
476 | if (GET_OPCODE(ie) == OP_NOT) { |
---|
477 | fs->pc--; /* remove previous OP_NOT */ |
---|
478 | return luaK_condjump(fs, OP_TEST, GETARG_B(ie), GETARG_B(ie), !cond); |
---|
479 | } |
---|
480 | /* else go through */ |
---|
481 | } |
---|
482 | discharge2anyreg(fs, e); |
---|
483 | freeexp(fs, e); |
---|
484 | return luaK_condjump(fs, OP_TEST, NO_REG, e->info, cond); |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | void luaK_goiftrue (FuncState *fs, expdesc *e) { |
---|
489 | int pc; /* pc of last jump */ |
---|
490 | luaK_dischargevars(fs, e); |
---|
491 | switch (e->k) { |
---|
492 | case VK: case VTRUE: { |
---|
493 | pc = NO_JUMP; /* always true; do nothing */ |
---|
494 | break; |
---|
495 | } |
---|
496 | case VFALSE: { |
---|
497 | pc = luaK_jump(fs); /* always jump */ |
---|
498 | break; |
---|
499 | } |
---|
500 | case VJMP: { |
---|
501 | invertjump(fs, e); |
---|
502 | pc = e->info; |
---|
503 | break; |
---|
504 | } |
---|
505 | default: { |
---|
506 | pc = jumponcond(fs, e, 0); |
---|
507 | break; |
---|
508 | } |
---|
509 | } |
---|
510 | luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ |
---|
511 | } |
---|
512 | |
---|
513 | |
---|
514 | void luaK_goiffalse (FuncState *fs, expdesc *e) { |
---|
515 | int pc; /* pc of last jump */ |
---|
516 | luaK_dischargevars(fs, e); |
---|
517 | switch (e->k) { |
---|
518 | case VNIL: case VFALSE: { |
---|
519 | pc = NO_JUMP; /* always false; do nothing */ |
---|
520 | break; |
---|
521 | } |
---|
522 | case VTRUE: { |
---|
523 | pc = luaK_jump(fs); /* always jump */ |
---|
524 | break; |
---|
525 | } |
---|
526 | case VJMP: { |
---|
527 | pc = e->info; |
---|
528 | break; |
---|
529 | } |
---|
530 | default: { |
---|
531 | pc = jumponcond(fs, e, 1); |
---|
532 | break; |
---|
533 | } |
---|
534 | } |
---|
535 | luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ |
---|
536 | } |
---|
537 | |
---|
538 | |
---|
539 | static void codenot (FuncState *fs, expdesc *e) { |
---|
540 | luaK_dischargevars(fs, e); |
---|
541 | switch (e->k) { |
---|
542 | case VNIL: case VFALSE: { |
---|
543 | e->k = VTRUE; |
---|
544 | break; |
---|
545 | } |
---|
546 | case VK: case VTRUE: { |
---|
547 | e->k = VFALSE; |
---|
548 | break; |
---|
549 | } |
---|
550 | case VJMP: { |
---|
551 | invertjump(fs, e); |
---|
552 | break; |
---|
553 | } |
---|
554 | case VRELOCABLE: |
---|
555 | case VNONRELOC: { |
---|
556 | discharge2anyreg(fs, e); |
---|
557 | freeexp(fs, e); |
---|
558 | e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0); |
---|
559 | e->k = VRELOCABLE; |
---|
560 | break; |
---|
561 | } |
---|
562 | default: { |
---|
563 | lua_assert(0); /* cannot happen */ |
---|
564 | break; |
---|
565 | } |
---|
566 | } |
---|
567 | /* interchange true and false lists */ |
---|
568 | { int temp = e->f; e->f = e->t; e->t = temp; } |
---|
569 | removevalues(fs, e->f); |
---|
570 | removevalues(fs, e->t); |
---|
571 | } |
---|
572 | |
---|
573 | |
---|
574 | void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { |
---|
575 | t->aux = luaK_exp2RK(fs, k); |
---|
576 | t->k = VINDEXED; |
---|
577 | } |
---|
578 | |
---|
579 | |
---|
580 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { |
---|
581 | if (op == OPR_MINUS) { |
---|
582 | luaK_exp2val(fs, e); |
---|
583 | if (e->k == VK && ttisnumber(&fs->f->k[e->info])) |
---|
584 | e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info])); |
---|
585 | else { |
---|
586 | luaK_exp2anyreg(fs, e); |
---|
587 | freeexp(fs, e); |
---|
588 | e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0); |
---|
589 | e->k = VRELOCABLE; |
---|
590 | } |
---|
591 | } |
---|
592 | else /* op == NOT */ |
---|
593 | codenot(fs, e); |
---|
594 | } |
---|
595 | |
---|
596 | |
---|
597 | void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { |
---|
598 | switch (op) { |
---|
599 | case OPR_AND: { |
---|
600 | luaK_goiftrue(fs, v); |
---|
601 | luaK_patchtohere(fs, v->t); |
---|
602 | v->t = NO_JUMP; |
---|
603 | break; |
---|
604 | } |
---|
605 | case OPR_OR: { |
---|
606 | luaK_goiffalse(fs, v); |
---|
607 | luaK_patchtohere(fs, v->f); |
---|
608 | v->f = NO_JUMP; |
---|
609 | break; |
---|
610 | } |
---|
611 | case OPR_CONCAT: { |
---|
612 | luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ |
---|
613 | break; |
---|
614 | } |
---|
615 | default: { |
---|
616 | luaK_exp2RK(fs, v); |
---|
617 | break; |
---|
618 | } |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | |
---|
623 | static void codebinop (FuncState *fs, expdesc *res, BinOpr op, |
---|
624 | int o1, int o2) { |
---|
625 | if (op <= OPR_POW) { /* arithmetic operator? */ |
---|
626 | OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD); /* ORDER OP */ |
---|
627 | res->info = luaK_codeABC(fs, opc, 0, o1, o2); |
---|
628 | res->k = VRELOCABLE; |
---|
629 | } |
---|
630 | else { /* test operator */ |
---|
631 | static const OpCode ops[] = {OP_EQ, OP_EQ, OP_LT, OP_LE, OP_LT, OP_LE}; |
---|
632 | int cond = 1; |
---|
633 | if (op >= OPR_GT) { /* `>' or `>='? */ |
---|
634 | int temp; /* exchange args and replace by `<' or `<=' */ |
---|
635 | temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ |
---|
636 | } |
---|
637 | else if (op == OPR_NE) cond = 0; |
---|
638 | res->info = luaK_condjump(fs, ops[op - OPR_NE], cond, o1, o2); |
---|
639 | res->k = VJMP; |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | |
---|
644 | void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { |
---|
645 | switch (op) { |
---|
646 | case OPR_AND: { |
---|
647 | lua_assert(e1->t == NO_JUMP); /* list must be closed */ |
---|
648 | luaK_dischargevars(fs, e2); |
---|
649 | luaK_concat(fs, &e1->f, e2->f); |
---|
650 | e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t; |
---|
651 | break; |
---|
652 | } |
---|
653 | case OPR_OR: { |
---|
654 | lua_assert(e1->f == NO_JUMP); /* list must be closed */ |
---|
655 | luaK_dischargevars(fs, e2); |
---|
656 | luaK_concat(fs, &e1->t, e2->t); |
---|
657 | e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f; |
---|
658 | break; |
---|
659 | } |
---|
660 | case OPR_CONCAT: { |
---|
661 | luaK_exp2val(fs, e2); |
---|
662 | if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { |
---|
663 | lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1); |
---|
664 | freeexp(fs, e1); |
---|
665 | SETARG_B(getcode(fs, e2), e1->info); |
---|
666 | e1->k = e2->k; e1->info = e2->info; |
---|
667 | } |
---|
668 | else { |
---|
669 | luaK_exp2nextreg(fs, e2); |
---|
670 | freeexp(fs, e2); |
---|
671 | freeexp(fs, e1); |
---|
672 | e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info); |
---|
673 | e1->k = VRELOCABLE; |
---|
674 | } |
---|
675 | break; |
---|
676 | } |
---|
677 | default: { |
---|
678 | int o1 = luaK_exp2RK(fs, e1); |
---|
679 | int o2 = luaK_exp2RK(fs, e2); |
---|
680 | freeexp(fs, e2); |
---|
681 | freeexp(fs, e1); |
---|
682 | codebinop(fs, e1, op, o1, o2); |
---|
683 | } |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | |
---|
688 | void luaK_fixline (FuncState *fs, int line) { |
---|
689 | fs->f->lineinfo[fs->pc - 1] = line; |
---|
690 | } |
---|
691 | |
---|
692 | |
---|
693 | int luaK_code (FuncState *fs, Instruction i, int line) { |
---|
694 | Proto *f = fs->f; |
---|
695 | luaK_dischargejpc(fs); /* `pc' will change */ |
---|
696 | /* put new instruction in code array */ |
---|
697 | luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, |
---|
698 | MAX_INT, "code size overflow"); |
---|
699 | f->code[fs->pc] = i; |
---|
700 | /* save corresponding line information */ |
---|
701 | luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, |
---|
702 | MAX_INT, "code size overflow"); |
---|
703 | f->lineinfo[fs->pc] = line; |
---|
704 | return fs->pc++; |
---|
705 | } |
---|
706 | |
---|
707 | |
---|
708 | int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { |
---|
709 | lua_assert(getOpMode(o) == iABC); |
---|
710 | return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); |
---|
711 | } |
---|
712 | |
---|
713 | |
---|
714 | int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { |
---|
715 | lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); |
---|
716 | return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); |
---|
717 | } |
---|
718 | |
---|