Imported from ../lua-5.1.1.tar.gz.
[lua.git] / src / lcode.c
blobb71acd8429a3988e51947e5dada7a6ce30cdc6c9
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 */
8 #include <stdlib.h>
10 #define lcode_c
11 #define LUA_CORE
13 #include "lua.h"
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"
27 #define hasjumps(e) ((e)->t != (e)->f)
30 static int isnumeral(expdesc *e) {
31 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
35 void 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;
50 luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
54 int 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;
64 void luaK_ret (FuncState *fs, int first, int nret) {
65 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
69 static 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);
75 static 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);
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).
89 int luaK_getlabel (FuncState *fs) {
90 fs->lasttarget = fs->pc;
91 return fs->pc;
95 static 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 */
104 static 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;
114 ** check whether list has any jump that do not produce a value
115 ** (or produce an inverted value)
117 static 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;
122 return 0; /* not found */
126 static 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));
135 return 1;
139 static void removevalues (FuncState *fs, int list) {
140 for (; list != NO_JUMP; list = getjump(fs, list))
141 patchtestreg(fs, list, NO_REG);
145 static 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;
158 static void dischargejpc (FuncState *fs) {
159 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
160 fs->jpc = NO_JUMP;
164 void 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);
174 void luaK_patchtohere (FuncState *fs, int list) {
175 luaK_getlabel(fs);
176 luaK_concat(fs, &fs->jpc, list);
180 void 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);
194 void 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);
204 void luaK_reserveregs (FuncState *fs, int n) {
205 luaK_checkstack(fs, n);
206 fs->freereg += n;
210 static void freereg (FuncState *fs, int reg) {
211 if (!ISK(reg) && reg >= fs->nactvar) {
212 fs->freereg--;
213 lua_assert(reg == fs->freereg);
218 static void freeexp (FuncState *fs, expdesc *e) {
219 if (e->k == VNONRELOC)
220 freereg(fs, e->u.s.info);
224 static 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));
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++;
245 int luaK_stringK (FuncState *fs, TString *s) {
246 TValue o;
247 setsvalue(fs->L, &o, s);
248 return addk(fs, &o, &o);
252 int luaK_numberK (FuncState *fs, lua_Number r) {
253 TValue o;
254 setnvalue(&o, r);
255 return addk(fs, &o, &o);
259 static int boolK (FuncState *fs, int b) {
260 TValue o;
261 setbvalue(&o, b);
262 return addk(fs, &o, &o);
266 static 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);
275 void 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);
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);
287 void 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));
292 else if (e->k == VVARARG) {
293 SETARG_B(getcode(fs, e), 2);
294 e->k = VRELOCABLE; /* can relocate its simple result */
299 void luaK_dischargevars (FuncState *fs, expdesc *e) {
300 switch (e->k) {
301 case VLOCAL: {
302 e->k = VNONRELOC;
303 break;
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;
310 case VGLOBAL: {
311 e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
312 e->k = VRELOCABLE;
313 break;
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;
322 case VVARARG:
323 case VCALL: {
324 luaK_setoneret(fs, e);
325 break;
327 default: break; /* there is one value available (somewhere) */
332 static 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);
338 static 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;
345 case VFALSE: case VTRUE: {
346 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
347 break;
349 case VK: {
350 luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
351 break;
353 case VKNUM: {
354 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
355 break;
357 case VRELOCABLE: {
358 Instruction *pc = &getcode(fs, e);
359 SETARG_A(*pc, reg);
360 break;
362 case VNONRELOC: {
363 if (reg != e->u.s.info)
364 luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
365 break;
367 default: {
368 lua_assert(e->k == VVOID || e->k == VJMP);
369 return; /* nothing to do... */
372 e->u.s.info = reg;
373 e->k = VNONRELOC;
377 static void discharge2anyreg (FuncState *fs, expdesc *e) {
378 if (e->k != VNONRELOC) {
379 luaK_reserveregs(fs, 1);
380 discharge2reg(fs, e, fs->freereg-1);
385 static 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);
399 final = luaK_getlabel(fs);
400 patchlistaux(fs, e->f, final, reg, p_f);
401 patchlistaux(fs, e->t, final, reg, p_t);
403 e->f = e->t = NO_JUMP;
404 e->u.s.info = reg;
405 e->k = VNONRELOC;
409 void 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);
417 int 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;
426 luaK_exp2nextreg(fs, e); /* default */
427 return e->u.s.info;
431 void luaK_exp2val (FuncState *fs, expdesc *e) {
432 if (hasjumps(e))
433 luaK_exp2anyreg(fs, e);
434 else
435 luaK_dischargevars(fs, e);
439 int 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);
453 else break;
455 case VK: {
456 if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
457 return RKASK(e->u.s.info);
458 else break;
460 default: break;
462 /* not a constant in the right range: put it in a register */
463 return luaK_exp2anyreg(fs, e);
467 void 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;
474 case VUPVAL: {
475 int e = luaK_exp2anyreg(fs, ex);
476 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
477 break;
479 case VGLOBAL: {
480 int e = luaK_exp2anyreg(fs, ex);
481 luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
482 break;
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;
489 default: {
490 lua_assert(0); /* invalid var kind to store */
491 break;
494 freeexp(fs, ex);
498 void 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;
511 static 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)));
519 static 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);
526 /* else go through */
528 discharge2anyreg(fs, e);
529 freeexp(fs, e);
530 return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
534 void 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;
542 case VFALSE: {
543 pc = luaK_jump(fs); /* always jump */
544 break;
546 case VJMP: {
547 invertjump(fs, e);
548 pc = e->u.s.info;
549 break;
551 default: {
552 pc = jumponcond(fs, e, 0);
553 break;
556 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
557 luaK_patchtohere(fs, e->t);
558 e->t = NO_JUMP;
562 static 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;
570 case VTRUE: {
571 pc = luaK_jump(fs); /* always jump */
572 break;
574 case VJMP: {
575 pc = e->u.s.info;
576 break;
578 default: {
579 pc = jumponcond(fs, e, 1);
580 break;
583 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
584 luaK_patchtohere(fs, e->f);
585 e->f = NO_JUMP;
589 static 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;
596 case VK: case VKNUM: case VTRUE: {
597 e->k = VFALSE;
598 break;
600 case VJMP: {
601 invertjump(fs, e);
602 break;
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;
612 default: {
613 lua_assert(0); /* cannot happen */
614 break;
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);
624 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
625 t->u.s.aux = luaK_exp2RK(fs, k);
626 t->k = VINDEXED;
630 static 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;
650 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
651 e1->u.nval = r;
652 return 1;
656 static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
657 if (constfolding(op, e1, e2))
658 return;
659 else {
660 int o1 = luaK_exp2RK(fs, e1);
661 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
662 freeexp(fs, e2);
663 freeexp(fs, e1);
664 e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
665 e1->k = VRELOCABLE;
670 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
671 expdesc *e2) {
672 int o1 = luaK_exp2RK(fs, e1);
673 int o2 = luaK_exp2RK(fs, e2);
674 freeexp(fs, e2);
675 freeexp(fs, e1);
676 if (cond == 0 && op != OP_EQ) {
677 int temp; /* exchange args to replace by `<' or `<=' */
678 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
679 cond = 1;
681 e1->u.s.info = condjump(fs, op, cond, o1, o2);
682 e1->k = VJMP;
686 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
687 expdesc e2;
688 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
689 switch (op) {
690 case OPR_MINUS: {
691 if (e->k == VK)
692 luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
693 codearith(fs, OP_UNM, e, &e2);
694 break;
696 case OPR_NOT: codenot(fs, e); break;
697 case OPR_LEN: {
698 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
699 codearith(fs, OP_LEN, e, &e2);
700 break;
702 default: lua_assert(0);
707 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
708 switch (op) {
709 case OPR_AND: {
710 luaK_goiftrue(fs, v);
711 break;
713 case OPR_OR: {
714 luaK_goiffalse(fs, v);
715 break;
717 case OPR_CONCAT: {
718 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
719 break;
721 default: {
722 if (!isnumeral(v)) luaK_exp2RK(fs, v);
723 break;
729 void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
730 switch (op) {
731 case OPR_AND: {
732 lua_assert(e1->t == NO_JUMP); /* list must be closed */
733 luaK_dischargevars(fs, e2);
734 luaK_concat(fs, &e2->f, e1->f);
735 *e1 = *e2;
736 break;
738 case OPR_OR: {
739 lua_assert(e1->f == NO_JUMP); /* list must be closed */
740 luaK_dischargevars(fs, e2);
741 luaK_concat(fs, &e2->t, e1->t);
742 *e1 = *e2;
743 break;
745 case OPR_CONCAT: {
746 luaK_exp2val(fs, e2);
747 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
748 lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
749 freeexp(fs, e1);
750 SETARG_B(getcode(fs, e2), e1->u.s.info);
751 e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
753 else {
754 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
755 codearith(fs, OP_CONCAT, e1, e2);
757 break;
759 case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
760 case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
761 case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
762 case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
763 case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
764 case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
765 case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
766 case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
767 case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
768 case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
769 case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
770 case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
771 default: lua_assert(0);
776 void luaK_fixline (FuncState *fs, int line) {
777 fs->f->lineinfo[fs->pc - 1] = line;
781 static int luaK_code (FuncState *fs, Instruction i, int line) {
782 Proto *f = fs->f;
783 dischargejpc(fs); /* `pc' will change */
784 /* put new instruction in code array */
785 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
786 MAX_INT, "code size overflow");
787 f->code[fs->pc] = i;
788 /* save corresponding line information */
789 luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
790 MAX_INT, "code size overflow");
791 f->lineinfo[fs->pc] = line;
792 return fs->pc++;
796 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
797 lua_assert(getOpMode(o) == iABC);
798 lua_assert(getBMode(o) != OpArgN || b == 0);
799 lua_assert(getCMode(o) != OpArgN || c == 0);
800 return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
804 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
805 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
806 lua_assert(getCMode(o) == OpArgN);
807 return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
811 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
812 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
813 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
814 lua_assert(tostore != 0);
815 if (c <= MAXARG_C)
816 luaK_codeABC(fs, OP_SETLIST, base, b, c);
817 else {
818 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
819 luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
821 fs->freereg = base + 1; /* free registers with list values */