monotone

monotone Mtn Source Tree

Root/lua/lcode.cc

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status