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