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