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