renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / liblua / lcode.c
blobc572c28471a19cff744f06f0e4b4a594e88e8c63
1 /*
2 ** $Id: lcode.c,v 2.62 2012/08/16 17:34:28 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 "lstring.h"
25 #include "ltable.h"
26 #include "lvm.h"
29 #define hasjumps(e) ((e)->t != (e)->f)
32 static int isnumeral(expdesc *e) {
33 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
37 void luaK_nil(FuncState *fs, int from, int n) {
38 Instruction *previous;
39 int l = from + n - 1; /* last register to set nil */
40 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
41 previous = &fs->f->code[fs->pc - 1];
42 if (GET_OPCODE(*previous) == OP_LOADNIL) {
43 int pfrom = GETARG_A(*previous);
44 int pl = pfrom + GETARG_B(*previous);
45 if ((pfrom <= from && from <= pl + 1) ||
46 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
47 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
48 if (pl > l) l = pl; /* l = max(l, pl) */
49 SETARG_A(*previous, from);
50 SETARG_B(*previous, l - from);
51 return;
53 } /* else go through */
55 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
59 int luaK_jump(FuncState *fs) {
60 int jpc = fs->jpc; /* save list of jumps to here */
61 int j;
62 fs->jpc = NO_JUMP;
63 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
64 luaK_concat(fs, &j, jpc); /* keep them on hold */
65 return j;
69 void luaK_ret(FuncState *fs, int first, int nret) {
70 luaK_codeABC(fs, OP_RETURN, first, nret + 1, 0);
74 static int condjump(FuncState *fs, OpCode op, int A, int B, int C) {
75 luaK_codeABC(fs, op, A, B, C);
76 return luaK_jump(fs);
80 static void fixjump(FuncState *fs, int pc, int dest) {
81 Instruction *jmp = &fs->f->code[pc];
82 int offset = dest - (pc + 1);
83 lua_assert(dest != NO_JUMP);
84 if (abs(offset) > MAXARG_sBx)
85 luaX_syntaxerror(fs->ls, "control structure too long");
86 SETARG_sBx(*jmp, offset);
91 ** returns current `pc' and marks it as a jump target (to avoid wrong
92 ** optimizations with consecutive instructions not in the same basic block).
94 int luaK_getlabel(FuncState *fs) {
95 fs->lasttarget = fs->pc;
96 return fs->pc;
100 static int getjump(FuncState *fs, int pc) {
101 int offset = GETARG_sBx(fs->f->code[pc]);
102 if (offset == NO_JUMP) /* point to itself represents end of list */
103 return NO_JUMP; /* end of list */
104 else
105 return (pc + 1) + offset; /* turn offset into absolute position */
109 static Instruction *getjumpcontrol(FuncState *fs, int pc) {
110 Instruction *pi = &fs->f->code[pc];
111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi - 1))))
112 return pi - 1;
113 else
114 return pi;
119 ** check whether list has any jump that do not produce a value
120 ** (or produce an inverted value)
122 static int need_value(FuncState *fs, int list) {
123 for (; list != NO_JUMP; list = getjump(fs, list)) {
124 Instruction i = *getjumpcontrol(fs, list);
125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
127 return 0; /* not found */
131 static int patchtestreg(FuncState *fs, int node, int reg) {
132 Instruction *i = getjumpcontrol(fs, node);
133 if (GET_OPCODE(*i) != OP_TESTSET)
134 return 0; /* cannot patch other instructions */
135 if (reg != NO_REG && reg != GETARG_B(*i))
136 SETARG_A(*i, reg);
137 else /* no register to put value or register already has the value */
138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
140 return 1;
144 static void removevalues(FuncState *fs, int list) {
145 for (; list != NO_JUMP; list = getjump(fs, list))
146 patchtestreg(fs, list, NO_REG);
150 static void patchlistaux(FuncState *fs, int list, int vtarget, int reg,
151 int dtarget) {
152 while (list != NO_JUMP) {
153 int next = getjump(fs, list);
154 if (patchtestreg(fs, list, reg))
155 fixjump(fs, list, vtarget);
156 else
157 fixjump(fs, list, dtarget); /* jump to default target */
158 list = next;
163 static void dischargejpc(FuncState *fs) {
164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
165 fs->jpc = NO_JUMP;
169 void luaK_patchlist(FuncState *fs, int list, int target) {
170 if (target == fs->pc)
171 luaK_patchtohere(fs, list);
172 else {
173 lua_assert(target < fs->pc);
174 patchlistaux(fs, list, target, NO_REG, target);
179 LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level) {
180 level++; /* argument is +1 to reserve 0 as non-op */
181 while (list != NO_JUMP) {
182 int next = getjump(fs, list);
183 lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
184 (GETARG_A(fs->f->code[list]) == 0 ||
185 GETARG_A(fs->f->code[list]) >= level));
186 SETARG_A(fs->f->code[list], level);
187 list = next;
192 void luaK_patchtohere(FuncState *fs, int list) {
193 luaK_getlabel(fs);
194 luaK_concat(fs, &fs->jpc, list);
198 void luaK_concat(FuncState *fs, int *l1, int l2) {
199 if (l2 == NO_JUMP) return;
200 else if (*l1 == NO_JUMP)
201 *l1 = l2;
202 else {
203 int list = *l1;
204 int next;
205 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
206 list = next;
207 fixjump(fs, list, l2);
212 static int luaK_code(FuncState *fs, Instruction i) {
213 Proto *f = fs->f;
214 dischargejpc(fs); /* `pc' will change */
215 /* put new instruction in code array */
216 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
217 MAX_INT, "opcodes");
218 f->code[fs->pc] = i;
219 /* save corresponding line information */
220 luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
221 MAX_INT, "opcodes");
222 f->lineinfo[fs->pc] = fs->ls->lastline;
223 return fs->pc++;
227 int luaK_codeABC(FuncState *fs, OpCode o, int a, int b, int c) {
228 lua_assert(getOpMode(o) == iABC);
229 lua_assert(getBMode(o) != OpArgN || b == 0);
230 lua_assert(getCMode(o) != OpArgN || c == 0);
231 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
232 return luaK_code(fs, CREATE_ABC(o, a, b, c));
236 int luaK_codeABx(FuncState *fs, OpCode o, int a, unsigned int bc) {
237 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
238 lua_assert(getCMode(o) == OpArgN);
239 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
240 return luaK_code(fs, CREATE_ABx(o, a, bc));
244 static int codeextraarg(FuncState *fs, int a) {
245 lua_assert(a <= MAXARG_Ax);
246 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
250 int luaK_codek(FuncState *fs, int reg, int k) {
251 if (k <= MAXARG_Bx)
252 return luaK_codeABx(fs, OP_LOADK, reg, k);
253 else {
254 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
255 codeextraarg(fs, k);
256 return p;
261 void luaK_checkstack(FuncState *fs, int n) {
262 int newstack = fs->freereg + n;
263 if (newstack > fs->f->maxstacksize) {
264 if (newstack >= MAXSTACK)
265 luaX_syntaxerror(fs->ls, "function or expression too complex");
266 fs->f->maxstacksize = cast_byte(newstack);
271 void luaK_reserveregs(FuncState *fs, int n) {
272 luaK_checkstack(fs, n);
273 fs->freereg += n;
277 static void freereg(FuncState *fs, int reg) {
278 if (!ISK(reg) && reg >= fs->nactvar) {
279 fs->freereg--;
280 lua_assert(reg == fs->freereg);
285 static void freeexp(FuncState *fs, expdesc *e) {
286 if (e->k == VNONRELOC)
287 freereg(fs, e->u.info);
291 static int addk(FuncState *fs, TValue *key, TValue *v) {
292 lua_State *L = fs->ls->L;
293 TValue *idx = luaH_set(L, fs->h, key);
294 Proto *f = fs->f;
295 int k, oldsize;
296 if (ttisnumber(idx)) {
297 lua_Number n = nvalue(idx);
298 lua_number2int(k, n);
299 if (luaV_rawequalobj(&f->k[k], v))
300 return k;
301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
302 go through and create a new entry for this value */
304 /* constant not found; create a new entry */
305 oldsize = f->sizek;
306 k = fs->nk;
307 /* numerical value does not need GC barrier;
308 table has no metatable, so it does not need to invalidate cache */
309 setnvalue(idx, cast_num(k));
310 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
311 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
312 setobj(L, &f->k[k], v);
313 fs->nk++;
314 luaC_barrier(L, f, v);
315 return k;
319 int luaK_stringK(FuncState *fs, TString *s) {
320 TValue o;
321 setsvalue(fs->ls->L, &o, s);
322 return addk(fs, &o, &o);
326 int luaK_numberK(FuncState *fs, lua_Number r) {
327 int n;
328 lua_State *L = fs->ls->L;
329 TValue o;
330 setnvalue(&o, r);
331 if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
332 /* use raw representation as key to avoid numeric problems */
333 setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
334 n = addk(fs, L->top - 1, &o);
335 L->top--;
336 } else
337 n = addk(fs, &o, &o); /* regular case */
338 return n;
342 static int boolK(FuncState *fs, int b) {
343 TValue o;
344 setbvalue(&o, b);
345 return addk(fs, &o, &o);
349 static int nilK(FuncState *fs) {
350 TValue k, v;
351 setnilvalue(&v);
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);
361 } else if (e->k == VVARARG) {
362 SETARG_B(getcode(fs, e), nresults + 1);
363 SETARG_A(getcode(fs, e), fs->freereg);
364 luaK_reserveregs(fs, 1);
369 void luaK_setoneret(FuncState *fs, expdesc *e) {
370 if (e->k == VCALL) { /* expression is an open function call? */
371 e->k = VNONRELOC;
372 e->u.info = GETARG_A(getcode(fs, e));
373 } else if (e->k == VVARARG) {
374 SETARG_B(getcode(fs, e), 2);
375 e->k = VRELOCABLE; /* can relocate its simple result */
380 void luaK_dischargevars(FuncState *fs, expdesc *e) {
381 switch (e->k) {
382 case VLOCAL: {
383 e->k = VNONRELOC;
384 break;
386 case VUPVAL: {
387 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
388 e->k = VRELOCABLE;
389 break;
391 case VINDEXED: {
392 OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
393 freereg(fs, e->u.ind.idx);
394 if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
395 freereg(fs, e->u.ind.t);
396 op = OP_GETTABLE;
398 e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
399 e->k = VRELOCABLE;
400 break;
402 case VVARARG:
403 case VCALL: {
404 luaK_setoneret(fs, e);
405 break;
407 default:
408 break; /* there is one value available (somewhere) */
413 static int code_label(FuncState *fs, int A, int b, int jump) {
414 luaK_getlabel(fs); /* those instructions may be jump targets */
415 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
419 static void discharge2reg(FuncState *fs, expdesc *e, int reg) {
420 luaK_dischargevars(fs, e);
421 switch (e->k) {
422 case VNIL: {
423 luaK_nil(fs, reg, 1);
424 break;
426 case VFALSE:
427 case VTRUE: {
428 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
429 break;
431 case VK: {
432 luaK_codek(fs, reg, e->u.info);
433 break;
435 case VKNUM: {
436 luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
437 break;
439 case VRELOCABLE: {
440 Instruction *pc = &getcode(fs, e);
441 SETARG_A(*pc, reg);
442 break;
444 case VNONRELOC: {
445 if (reg != e->u.info)
446 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
447 break;
449 default: {
450 lua_assert(e->k == VVOID || e->k == VJMP);
451 return; /* nothing to do... */
454 e->u.info = reg;
455 e->k = VNONRELOC;
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);
469 if (e->k == VJMP)
470 luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
471 if (hasjumps(e)) {
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;
486 e->u.info = reg;
487 e->k = VNONRELOC;
491 void luaK_exp2nextreg(FuncState *fs, expdesc *e) {
492 luaK_dischargevars(fs, e);
493 freeexp(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 */
505 return e->u.info;
508 luaK_exp2nextreg(fs, e); /* default */
509 return e->u.info;
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) {
520 if (hasjumps(e))
521 luaK_exp2anyreg(fs, e);
522 else
523 luaK_dischargevars(fs, e);
527 int luaK_exp2RK(FuncState *fs, expdesc *e) {
528 luaK_exp2val(fs, e);
529 switch (e->k) {
530 case VTRUE:
531 case VFALSE:
532 case VNIL: {
533 if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
534 e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
535 e->k = VK;
536 return RKASK(e->u.info);
537 } else break;
539 case VKNUM: {
540 e->u.info = luaK_numberK(fs, e->u.nval);
541 e->k = VK;
542 /* go through */
544 case VK: {
545 if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
546 return RKASK(e->u.info);
547 else break;
549 default:
550 break;
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) {
558 switch (var->k) {
559 case VLOCAL: {
560 freeexp(fs, ex);
561 exp2reg(fs, ex, var->u.info);
562 return;
564 case VUPVAL: {
565 int e = luaK_exp2anyreg(fs, ex);
566 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
567 break;
569 case VINDEXED: {
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);
573 break;
575 default: {
576 lua_assert(0); /* invalid var kind to store */
577 break;
580 freeexp(fs, ex);
584 void luaK_self(FuncState *fs, expdesc *e, expdesc *key) {
585 int ereg;
586 luaK_exp2anyreg(fs, e);
587 ereg = e->u.info; /* register where 'e' was placed */
588 freeexp(fs, e);
589 e->u.info = fs->freereg; /* base register for op_self */
590 e->k = VNONRELOC;
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));
593 freeexp(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);
615 freeexp(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);
623 switch (e->k) {
624 case VJMP: {
625 invertjump(fs, e);
626 pc = e->u.info;
627 break;
629 case VK:
630 case VKNUM:
631 case VTRUE: {
632 pc = NO_JUMP; /* always true; do nothing */
633 break;
635 default: {
636 pc = jumponcond(fs, e, 0);
637 break;
640 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
641 luaK_patchtohere(fs, e->t);
642 e->t = NO_JUMP;
646 void luaK_goiffalse(FuncState *fs, expdesc *e) {
647 int pc; /* pc of last jump */
648 luaK_dischargevars(fs, e);
649 switch (e->k) {
650 case VJMP: {
651 pc = e->u.info;
652 break;
654 case VNIL:
655 case VFALSE: {
656 pc = NO_JUMP; /* always false; do nothing */
657 break;
659 default: {
660 pc = jumponcond(fs, e, 1);
661 break;
664 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
665 luaK_patchtohere(fs, e->f);
666 e->f = NO_JUMP;
670 static void codenot(FuncState *fs, expdesc *e) {
671 luaK_dischargevars(fs, e);
672 switch (e->k) {
673 case VNIL:
674 case VFALSE: {
675 e->k = VTRUE;
676 break;
678 case VK:
679 case VKNUM:
680 case VTRUE: {
681 e->k = VFALSE;
682 break;
684 case VJMP: {
685 invertjump(fs, e);
686 break;
688 case VRELOCABLE:
689 case VNONRELOC: {
690 discharge2anyreg(fs, e);
691 freeexp(fs, e);
692 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
693 e->k = VRELOCABLE;
694 break;
696 default: {
697 lua_assert(0); /* cannot happen */
698 break;
701 /* interchange true and false lists */
702 { int temp = e->f; e->f = e->t; e->t = temp; }
703 removevalues(fs, e->f);
704 removevalues(fs, e->t);
708 void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k) {
709 lua_assert(!hasjumps(t));
710 t->u.ind.t = t->u.info;
711 t->u.ind.idx = luaK_exp2RK(fs, k);
712 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
713 : check_exp(vkisinreg(t->k), VLOCAL);
714 t->k = VINDEXED;
718 static int constfolding(OpCode op, expdesc *e1, expdesc *e2) {
719 lua_Number r;
720 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
721 if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
722 return 0; /* do not attempt to divide by 0 */
723 r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
724 e1->u.nval = r;
725 return 1;
729 static void codearith(FuncState *fs, OpCode op,
730 expdesc *e1, expdesc *e2, int line) {
731 if (constfolding(op, e1, e2))
732 return;
733 else {
734 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
735 int o1 = luaK_exp2RK(fs, e1);
736 if (o1 > o2) {
737 freeexp(fs, e1);
738 freeexp(fs, e2);
739 } else {
740 freeexp(fs, e2);
741 freeexp(fs, e1);
743 e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
744 e1->k = VRELOCABLE;
745 luaK_fixline(fs, line);
750 static void codecomp(FuncState *fs, OpCode op, int cond, expdesc *e1,
751 expdesc *e2) {
752 int o1 = luaK_exp2RK(fs, e1);
753 int o2 = luaK_exp2RK(fs, e2);
754 freeexp(fs, e2);
755 freeexp(fs, e1);
756 if (cond == 0 && op != OP_EQ) {
757 int temp; /* exchange args to replace by `<' or `<=' */
758 temp = o1;
759 o1 = o2;
760 o2 = temp; /* o1 <==> o2 */
761 cond = 1;
763 e1->u.info = condjump(fs, op, cond, o1, o2);
764 e1->k = VJMP;
768 void luaK_prefix(FuncState *fs, UnOpr op, expdesc *e, int line) {
769 expdesc e2;
770 e2.t = e2.f = NO_JUMP;
771 e2.k = VKNUM;
772 e2.u.nval = 0;
773 switch (op) {
774 case OPR_MINUS: {
775 if (isnumeral(e)) /* minus constant? */
776 e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
777 else {
778 luaK_exp2anyreg(fs, e);
779 codearith(fs, OP_UNM, e, &e2, line);
781 break;
783 case OPR_NOT:
784 codenot(fs, e);
785 break;
786 case OPR_LEN: {
787 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
788 codearith(fs, OP_LEN, e, &e2, line);
789 break;
791 default:
792 lua_assert(0);
797 void luaK_infix(FuncState *fs, BinOpr op, expdesc *v) {
798 switch (op) {
799 case OPR_AND: {
800 luaK_goiftrue(fs, v);
801 break;
803 case OPR_OR: {
804 luaK_goiffalse(fs, v);
805 break;
807 case OPR_CONCAT: {
808 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
809 break;
811 case OPR_ADD:
812 case OPR_SUB:
813 case OPR_MUL:
814 case OPR_DIV:
815 case OPR_MOD:
816 case OPR_POW: {
817 if (!isnumeral(v)) luaK_exp2RK(fs, v);
818 break;
820 default: {
821 luaK_exp2RK(fs, v);
822 break;
828 void luaK_posfix(FuncState *fs, BinOpr op,
829 expdesc *e1, expdesc *e2, int line) {
830 switch (op) {
831 case OPR_AND: {
832 lua_assert(e1->t == NO_JUMP); /* list must be closed */
833 luaK_dischargevars(fs, e2);
834 luaK_concat(fs, &e2->f, e1->f);
835 *e1 = *e2;
836 break;
838 case OPR_OR: {
839 lua_assert(e1->f == NO_JUMP); /* list must be closed */
840 luaK_dischargevars(fs, e2);
841 luaK_concat(fs, &e2->t, e1->t);
842 *e1 = *e2;
843 break;
845 case OPR_CONCAT: {
846 luaK_exp2val(fs, e2);
847 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
848 lua_assert(e1->u.info == GETARG_B(getcode(fs, e2)) - 1);
849 freeexp(fs, e1);
850 SETARG_B(getcode(fs, e2), e1->u.info);
851 e1->k = VRELOCABLE;
852 e1->u.info = e2->u.info;
853 } else {
854 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
855 codearith(fs, OP_CONCAT, e1, e2, line);
857 break;
859 case OPR_ADD:
860 case OPR_SUB:
861 case OPR_MUL:
862 case OPR_DIV:
863 case OPR_MOD:
864 case OPR_POW: {
865 codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
866 break;
868 case OPR_EQ:
869 case OPR_LT:
870 case OPR_LE: {
871 codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
872 break;
874 case OPR_NE:
875 case OPR_GT:
876 case OPR_GE: {
877 codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
878 break;
880 default:
881 lua_assert(0);
886 void luaK_fixline(FuncState *fs, int line) {
887 fs->f->lineinfo[fs->pc - 1] = line;
891 void luaK_setlist(FuncState *fs, int base, int nelems, int tostore) {
892 int c = (nelems - 1) / LFIELDS_PER_FLUSH + 1;
893 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
894 lua_assert(tostore != 0);
895 if (c <= MAXARG_C)
896 luaK_codeABC(fs, OP_SETLIST, base, b, c);
897 else if (c <= MAXARG_Ax) {
898 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
899 codeextraarg(fs, c);
900 } else
901 luaX_syntaxerror(fs->ls, "constructor too long");
902 fs->freereg = base + 1; /* free registers with list values */