2 ** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Code generator for Lua
4 ** See Copyright Notice in lua.h
7 #include <sys/zfs_context.h>
28 #define hasjumps(e) ((e)->t != (e)->f)
31 static int isnumeral(expdesc *e) {
32 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
36 void luaK_nil (FuncState *fs, int from, int n) {
37 Instruction *previous;
38 int l = from + n - 1; /* last register to set nil */
39 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
40 previous = &fs->f->code[fs->pc-1];
41 if (GET_OPCODE(*previous) == OP_LOADNIL) {
42 int pfrom = GETARG_A(*previous);
43 int pl = pfrom + GETARG_B(*previous);
44 if ((pfrom <= from && from <= pl + 1) ||
45 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
46 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
47 if (pl > l) l = pl; /* l = max(l, pl) */
48 SETARG_A(*previous, from);
49 SETARG_B(*previous, l - from);
52 } /* else go through */
54 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
58 int luaK_jump (FuncState *fs) {
59 int jpc = fs->jpc; /* save list of jumps to here */
62 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
63 luaK_concat(fs, &j, jpc); /* keep them on hold */
68 void luaK_ret (FuncState *fs, int first, int nret) {
69 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
73 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
74 luaK_codeABC(fs, op, A, B, C);
79 static void fixjump (FuncState *fs, int pc, int dest) {
80 Instruction *jmp = &fs->f->code[pc];
81 int offset = dest-(pc+1);
82 lua_assert(dest != NO_JUMP);
83 if (abs(offset) > MAXARG_sBx)
84 luaX_syntaxerror(fs->ls, "control structure too long");
85 SETARG_sBx(*jmp, offset);
90 ** returns current `pc' and marks it as a jump target (to avoid wrong
91 ** optimizations with consecutive instructions not in the same basic block).
93 int luaK_getlabel (FuncState *fs) {
94 fs->lasttarget = fs->pc;
99 static int getjump (FuncState *fs, int pc) {
100 int offset = GETARG_sBx(fs->f->code[pc]);
101 if (offset == NO_JUMP) /* point to itself represents end of list */
102 return NO_JUMP; /* end of list */
104 return (pc+1)+offset; /* turn offset into absolute position */
108 static Instruction *getjumpcontrol (FuncState *fs, int pc) {
109 Instruction *pi = &fs->f->code[pc];
110 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
118 ** check whether list has any jump that do not produce a value
119 ** (or produce an inverted value)
121 static int need_value (FuncState *fs, int list) {
122 for (; list != NO_JUMP; list = getjump(fs, list)) {
123 Instruction i = *getjumpcontrol(fs, list);
124 if (GET_OPCODE(i) != OP_TESTSET) return 1;
126 return 0; /* not found */
130 static int patchtestreg (FuncState *fs, int node, int reg) {
131 Instruction *i = getjumpcontrol(fs, node);
132 if (GET_OPCODE(*i) != OP_TESTSET)
133 return 0; /* cannot patch other instructions */
134 if (reg != NO_REG && reg != GETARG_B(*i))
136 else /* no register to put value or register already has the value */
137 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
143 static void removevalues (FuncState *fs, int list) {
144 for (; list != NO_JUMP; list = getjump(fs, list))
145 patchtestreg(fs, list, NO_REG);
149 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
151 while (list != NO_JUMP) {
152 int next = getjump(fs, list);
153 if (patchtestreg(fs, list, reg))
154 fixjump(fs, list, vtarget);
156 fixjump(fs, list, dtarget); /* jump to default target */
162 static void dischargejpc (FuncState *fs) {
163 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
168 void luaK_patchlist (FuncState *fs, int list, int target) {
169 if (target == fs->pc)
170 luaK_patchtohere(fs, list);
172 lua_assert(target < fs->pc);
173 patchlistaux(fs, list, target, NO_REG, target);
178 LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
179 level++; /* argument is +1 to reserve 0 as non-op */
180 while (list != NO_JUMP) {
181 int next = getjump(fs, list);
182 lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
183 (GETARG_A(fs->f->code[list]) == 0 ||
184 GETARG_A(fs->f->code[list]) >= level));
185 SETARG_A(fs->f->code[list], level);
191 void luaK_patchtohere (FuncState *fs, int list) {
193 luaK_concat(fs, &fs->jpc, list);
197 void luaK_concat (FuncState *fs, int *l1, int l2) {
198 if (l2 == NO_JUMP) return;
199 else if (*l1 == NO_JUMP)
204 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
206 fixjump(fs, list, l2);
211 static int luaK_code (FuncState *fs, Instruction i) {
213 dischargejpc(fs); /* `pc' will change */
214 /* put new instruction in code array */
215 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
218 /* save corresponding line information */
219 luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
221 f->lineinfo[fs->pc] = fs->ls->lastline;
226 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
227 lua_assert(getOpMode(o) == iABC);
228 lua_assert(getBMode(o) != OpArgN || b == 0);
229 lua_assert(getCMode(o) != OpArgN || c == 0);
230 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
231 return luaK_code(fs, CREATE_ABC(o, a, b, c));
235 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
236 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
237 lua_assert(getCMode(o) == OpArgN);
238 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
239 return luaK_code(fs, CREATE_ABx(o, a, bc));
243 static int codeextraarg (FuncState *fs, int a) {
244 lua_assert(a <= MAXARG_Ax);
245 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
249 int luaK_codek (FuncState *fs, int reg, int k) {
251 return luaK_codeABx(fs, OP_LOADK, reg, k);
253 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
260 void luaK_checkstack (FuncState *fs, int n) {
261 int newstack = fs->freereg + n;
262 if (newstack > fs->f->maxstacksize) {
263 if (newstack >= MAXSTACK)
264 luaX_syntaxerror(fs->ls, "function or expression too complex");
265 fs->f->maxstacksize = cast_byte(newstack);
270 void luaK_reserveregs (FuncState *fs, int n) {
271 luaK_checkstack(fs, n);
276 static void freereg (FuncState *fs, int reg) {
277 if (!ISK(reg) && reg >= fs->nactvar) {
279 lua_assert(reg == fs->freereg);
284 static void freeexp (FuncState *fs, expdesc *e) {
285 if (e->k == VNONRELOC)
286 freereg(fs, e->u.info);
290 static int addk (FuncState *fs, TValue *key, TValue *v) {
291 lua_State *L = fs->ls->L;
292 TValue *idx = luaH_set(L, fs->h, key);
295 if (ttisnumber(idx)) {
296 lua_Number n = nvalue(idx);
297 lua_number2int(k, n);
298 if (luaV_rawequalobj(&f->k[k], v))
300 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
301 go through and create a new entry for this value */
303 /* constant not found; create a new entry */
306 /* numerical value does not need GC barrier;
307 table has no metatable, so it does not need to invalidate cache */
308 setnvalue(idx, cast_num(k));
309 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
310 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
311 setobj(L, &f->k[k], v);
313 luaC_barrier(L, f, v);
318 int luaK_stringK (FuncState *fs, TString *s) {
320 setsvalue(fs->ls->L, &o, s);
321 return addk(fs, &o, &o);
325 int luaK_numberK (FuncState *fs, lua_Number r) {
327 lua_State *L = fs->ls->L;
330 if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
331 /* use raw representation as key to avoid numeric problems */
332 setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
333 n = addk(fs, L->top - 1, &o);
337 n = addk(fs, &o, &o); /* regular case */
342 static int boolK (FuncState *fs, int b) {
345 return addk(fs, &o, &o);
349 static int nilK (FuncState *fs) {
352 /* cannot use nil as key; instead use table itself to represent nil */
353 sethvalue(fs->ls->L, &k, fs->h);
354 return addk(fs, &k, &v);
358 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
359 if (e->k == VCALL) { /* expression is an open function call? */
360 SETARG_C(getcode(fs, e), nresults+1);
362 else if (e->k == VVARARG) {
363 SETARG_B(getcode(fs, e), nresults+1);
364 SETARG_A(getcode(fs, e), fs->freereg);
365 luaK_reserveregs(fs, 1);
370 void luaK_setoneret (FuncState *fs, expdesc *e) {
371 if (e->k == VCALL) { /* expression is an open function call? */
373 e->u.info = GETARG_A(getcode(fs, e));
375 else if (e->k == VVARARG) {
376 SETARG_B(getcode(fs, e), 2);
377 e->k = VRELOCABLE; /* can relocate its simple result */
382 void luaK_dischargevars (FuncState *fs, expdesc *e) {
389 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
394 OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
395 freereg(fs, e->u.ind.idx);
396 if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
397 freereg(fs, e->u.ind.t);
400 e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
406 luaK_setoneret(fs, e);
409 default: break; /* there is one value available (somewhere) */
414 static int code_label (FuncState *fs, int A, int b, int jump) {
415 luaK_getlabel(fs); /* those instructions may be jump targets */
416 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
420 static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
421 luaK_dischargevars(fs, e);
424 luaK_nil(fs, reg, 1);
427 case VFALSE: case VTRUE: {
428 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
432 luaK_codek(fs, reg, e->u.info);
436 luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
440 Instruction *pc = &getcode(fs, e);
445 if (reg != e->u.info)
446 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
450 lua_assert(e->k == VVOID || e->k == VJMP);
451 return; /* nothing to do... */
459 static void discharge2anyreg (FuncState *fs, expdesc *e) {
460 if (e->k != VNONRELOC) {
461 luaK_reserveregs(fs, 1);
462 discharge2reg(fs, e, fs->freereg-1);
467 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
468 discharge2reg(fs, e, reg);
470 luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
472 int final; /* position after whole expression */
473 int p_f = NO_JUMP; /* position of an eventual LOAD false */
474 int p_t = NO_JUMP; /* position of an eventual LOAD true */
475 if (need_value(fs, e->t) || need_value(fs, e->f)) {
476 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
477 p_f = code_label(fs, reg, 0, 1);
478 p_t = code_label(fs, reg, 1, 0);
479 luaK_patchtohere(fs, fj);
481 final = luaK_getlabel(fs);
482 patchlistaux(fs, e->f, final, reg, p_f);
483 patchlistaux(fs, e->t, final, reg, p_t);
485 e->f = e->t = NO_JUMP;
491 void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
492 luaK_dischargevars(fs, e);
494 luaK_reserveregs(fs, 1);
495 exp2reg(fs, e, fs->freereg - 1);
499 int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
500 luaK_dischargevars(fs, e);
501 if (e->k == VNONRELOC) {
502 if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
503 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
504 exp2reg(fs, e, e->u.info); /* put value on it */
508 luaK_exp2nextreg(fs, e); /* default */
513 void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
514 if (e->k != VUPVAL || hasjumps(e))
515 luaK_exp2anyreg(fs, e);
519 void luaK_exp2val (FuncState *fs, expdesc *e) {
521 luaK_exp2anyreg(fs, e);
523 luaK_dischargevars(fs, e);
527 int luaK_exp2RK (FuncState *fs, expdesc *e) {
533 if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
534 e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
536 return RKASK(e->u.info);
541 e->u.info = luaK_numberK(fs, e->u.nval);
546 if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
547 return RKASK(e->u.info);
552 /* not a constant in the right range: put it in a register */
553 return luaK_exp2anyreg(fs, e);
557 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
561 exp2reg(fs, ex, var->u.info);
565 int e = luaK_exp2anyreg(fs, ex);
566 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
570 OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
571 int e = luaK_exp2RK(fs, ex);
572 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
576 lua_assert(0); /* invalid var kind to store */
584 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
586 luaK_exp2anyreg(fs, e);
587 ereg = e->u.info; /* register where 'e' was placed */
589 e->u.info = fs->freereg; /* base register for op_self */
591 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
592 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
597 static void invertjump (FuncState *fs, expdesc *e) {
598 Instruction *pc = getjumpcontrol(fs, e->u.info);
599 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
600 GET_OPCODE(*pc) != OP_TEST);
601 SETARG_A(*pc, !(GETARG_A(*pc)));
605 static int jumponcond (FuncState *fs, expdesc *e, int cond) {
606 if (e->k == VRELOCABLE) {
607 Instruction ie = getcode(fs, e);
608 if (GET_OPCODE(ie) == OP_NOT) {
609 fs->pc--; /* remove previous OP_NOT */
610 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
612 /* else go through */
614 discharge2anyreg(fs, e);
616 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
620 void luaK_goiftrue (FuncState *fs, expdesc *e) {
621 int pc; /* pc of last jump */
622 luaK_dischargevars(fs, e);
629 case VK: case VKNUM: case VTRUE: {
630 pc = NO_JUMP; /* always true; do nothing */
634 pc = jumponcond(fs, e, 0);
638 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
639 luaK_patchtohere(fs, e->t);
644 void luaK_goiffalse (FuncState *fs, expdesc *e) {
645 int pc; /* pc of last jump */
646 luaK_dischargevars(fs, e);
652 case VNIL: case VFALSE: {
653 pc = NO_JUMP; /* always false; do nothing */
657 pc = jumponcond(fs, e, 1);
661 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
662 luaK_patchtohere(fs, e->f);
667 static void codenot (FuncState *fs, expdesc *e) {
668 luaK_dischargevars(fs, e);
670 case VNIL: case VFALSE: {
674 case VK: case VKNUM: case VTRUE: {
684 discharge2anyreg(fs, e);
686 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
691 lua_assert(0); /* cannot happen */
695 /* interchange true and false lists */
696 { int temp = e->f; e->f = e->t; e->t = temp; }
697 removevalues(fs, e->f);
698 removevalues(fs, e->t);
702 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
703 lua_assert(!hasjumps(t));
704 t->u.ind.t = t->u.info;
705 t->u.ind.idx = luaK_exp2RK(fs, k);
706 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
707 : check_exp(vkisinreg(t->k), VLOCAL);
712 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
714 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
715 if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
716 return 0; /* do not attempt to divide by 0 */
718 * Patched: check for MIN_INT / -1
720 if (op == OP_DIV && e1->u.nval == INT64_MIN && e2->u.nval == -1)
722 r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
728 static void codearith (FuncState *fs, OpCode op,
729 expdesc *e1, expdesc *e2, int line) {
730 if (constfolding(op, e1, e2))
733 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
734 int o1 = luaK_exp2RK(fs, e1);
743 e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
745 luaK_fixline(fs, line);
750 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
752 int o1 = luaK_exp2RK(fs, e1);
753 int o2 = luaK_exp2RK(fs, e2);
756 if (cond == 0 && op != OP_EQ) {
757 int temp; /* exchange args to replace by `<' or `<=' */
758 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
761 e1->u.info = condjump(fs, op, cond, o1, o2);
766 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
768 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
771 if (isnumeral(e)) /* minus constant? */
772 e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
774 luaK_exp2anyreg(fs, e);
775 codearith(fs, OP_UNM, e, &e2, line);
779 case OPR_NOT: codenot(fs, e); break;
781 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
782 codearith(fs, OP_LEN, e, &e2, line);
785 default: lua_assert(0);
790 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
793 luaK_goiftrue(fs, v);
797 luaK_goiffalse(fs, v);
801 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
804 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
805 case OPR_MOD: case OPR_POW: {
806 if (!isnumeral(v)) luaK_exp2RK(fs, v);
817 void luaK_posfix (FuncState *fs, BinOpr op,
818 expdesc *e1, expdesc *e2, int line) {
821 lua_assert(e1->t == NO_JUMP); /* list must be closed */
822 luaK_dischargevars(fs, e2);
823 luaK_concat(fs, &e2->f, e1->f);
828 lua_assert(e1->f == NO_JUMP); /* list must be closed */
829 luaK_dischargevars(fs, e2);
830 luaK_concat(fs, &e2->t, e1->t);
835 luaK_exp2val(fs, e2);
836 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
837 lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
839 SETARG_B(getcode(fs, e2), e1->u.info);
840 e1->k = VRELOCABLE; e1->u.info = e2->u.info;
843 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
844 codearith(fs, OP_CONCAT, e1, e2, line);
848 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
849 case OPR_MOD: case OPR_POW: {
850 codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
853 case OPR_EQ: case OPR_LT: case OPR_LE: {
854 codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
857 case OPR_NE: case OPR_GT: case OPR_GE: {
858 codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
861 default: lua_assert(0);
866 void luaK_fixline (FuncState *fs, int line) {
867 fs->f->lineinfo[fs->pc - 1] = line;
871 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
872 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
873 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
874 lua_assert(tostore != 0);
876 luaK_codeABC(fs, OP_SETLIST, base, b, c);
877 else if (c <= MAXARG_Ax) {
878 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
882 luaX_syntaxerror(fs->ls, "constructor too long");
883 fs->freereg = base + 1; /* free registers with list values */