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