Line data Source code
1 : /*
2 : ** $Id: lcode.c,v 2.112.1.1 2017/04/19 17:20:42 roberto Exp $
3 : ** Code generator for Lua
4 : ** See Copyright Notice in lua.h
5 : */
6 :
7 : #define lcode_c
8 : #define LUA_CORE
9 :
10 : #if defined(ARDUPILOT_BUILD)
11 : #pragma GCC diagnostic ignored "-Wfloat-equal"
12 : #if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang_major__) && __clang_major__ >= 10
13 : #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
14 : #endif
15 : #endif
16 :
17 : #include "lprefix.h"
18 :
19 :
20 : #include <math.h>
21 : #include <stdlib.h>
22 :
23 : #include "lua.h"
24 :
25 : #include "lcode.h"
26 : #include "ldebug.h"
27 : #include "ldo.h"
28 : #include "lgc.h"
29 : #include "llex.h"
30 : #include "lmem.h"
31 : #include "lobject.h"
32 : #include "lopcodes.h"
33 : #include "lparser.h"
34 : #include "lstring.h"
35 : #include "ltable.h"
36 : #include "lvm.h"
37 :
38 :
39 : /* Maximum number of registers in a Lua function (must fit in 8 bits) */
40 : #define MAXREGS 255
41 :
42 :
43 : #define hasjumps(e) ((e)->t != (e)->f)
44 :
45 :
46 : /*
47 : ** If expression is a numeric constant, fills 'v' with its value
48 : ** and returns 1. Otherwise, returns 0.
49 : */
50 9391 : static int tonumeral(const expdesc *e, TValue *v) {
51 9391 : if (hasjumps(e))
52 0 : return 0; /* not a numeral */
53 9391 : switch (e->k) {
54 2622 : case VKINT:
55 2622 : if (v) setivalue(v, e->u.ival);
56 2622 : return 1;
57 575 : case VKFLT:
58 575 : if (v) setfltvalue(v, e->u.nval);
59 575 : return 1;
60 6194 : default: return 0;
61 : }
62 : }
63 :
64 :
65 : /*
66 : ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
67 : ** instruction is also OP_LOADNIL and ranges are compatible, adjust
68 : ** range of previous instruction instead of emitting a new one. (For
69 : ** instance, 'local a; local b' will generate a single opcode.)
70 : */
71 386 : void luaK_nil (FuncState *fs, int from, int n) {
72 : Instruction *previous;
73 386 : int l = from + n - 1; /* last register to set nil */
74 386 : if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
75 266 : previous = &fs->f->code[fs->pc-1];
76 266 : if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
77 90 : int pfrom = GETARG_A(*previous); /* get previous range */
78 90 : int pl = pfrom + GETARG_B(*previous);
79 90 : if ((pfrom <= from && from <= pl + 1) ||
80 0 : (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
81 90 : if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
82 90 : if (pl > l) l = pl; /* l = max(l, pl) */
83 90 : SETARG_A(*previous, from);
84 90 : SETARG_B(*previous, l - from);
85 90 : return;
86 : }
87 : } /* else go through */
88 : }
89 296 : luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
90 : }
91 :
92 :
93 : /*
94 : ** Gets the destination address of a jump instruction. Used to traverse
95 : ** a list of jumps.
96 : */
97 10190 : static int getjump (FuncState *fs, int pc) {
98 10190 : int offset = GETARG_sBx(fs->f->code[pc]);
99 10190 : if (offset == NO_JUMP) /* point to itself represents end of list */
100 7689 : return NO_JUMP; /* end of list */
101 : else
102 2501 : return (pc+1)+offset; /* turn offset into absolute position */
103 : }
104 :
105 :
106 : /*
107 : ** Fix jump instruction at position 'pc' to jump to 'dest'.
108 : ** (Jump addresses are relative in Lua)
109 : */
110 9037 : static void fixjump (FuncState *fs, int pc, int dest) {
111 9037 : Instruction *jmp = &fs->f->code[pc];
112 9037 : int offset = dest - (pc + 1);
113 : lua_assert(dest != NO_JUMP);
114 9037 : if (abs(offset) > MAXARG_sBx)
115 0 : luaX_syntaxerror(fs->ls, "control structure too long");
116 9037 : SETARG_sBx(*jmp, offset);
117 9037 : }
118 :
119 :
120 : /*
121 : ** Concatenate jump-list 'l2' into jump-list 'l1'
122 : */
123 28006 : void luaK_concat (FuncState *fs, int *l1, int l2) {
124 28006 : if (l2 == NO_JUMP) return; /* nothing to concatenate? */
125 12718 : else if (*l1 == NO_JUMP) /* no original list? */
126 11064 : *l1 = l2; /* 'l1' points to 'l2' */
127 : else {
128 1654 : int list = *l1;
129 : int next;
130 2453 : while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
131 799 : list = next;
132 1654 : fixjump(fs, list, l2); /* last element links to 'l2' */
133 : }
134 : }
135 :
136 :
137 : /*
138 : ** Create a jump instruction and return its position, so its destination
139 : ** can be fixed later (with 'fixjump'). If there are jumps to
140 : ** this position (kept in 'jpc'), link them all together so that
141 : ** 'patchlistaux' will fix all them directly to the final destination.
142 : */
143 6895 : int luaK_jump (FuncState *fs) {
144 6895 : int jpc = fs->jpc; /* save list of jumps to here */
145 : int j;
146 6895 : fs->jpc = NO_JUMP; /* no more jumps to here */
147 6895 : j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
148 6895 : luaK_concat(fs, &j, jpc); /* keep them on hold */
149 6895 : return j;
150 : }
151 :
152 :
153 : /*
154 : ** Code a 'return' instruction
155 : */
156 4486 : void luaK_ret (FuncState *fs, int first, int nret) {
157 4486 : luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
158 4486 : }
159 :
160 :
161 : /*
162 : ** Code a "conditional jump", that is, a test or comparison opcode
163 : ** followed by a jump. Return jump position.
164 : */
165 5962 : static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
166 5962 : luaK_codeABC(fs, op, A, B, C);
167 5962 : return luaK_jump(fs);
168 : }
169 :
170 :
171 : /*
172 : ** returns current 'pc' and marks it as a jump target (to avoid wrong
173 : ** optimizations with consecutive instructions not in the same basic block).
174 : */
175 16723 : int luaK_getlabel (FuncState *fs) {
176 16723 : fs->lasttarget = fs->pc;
177 16723 : return fs->pc;
178 : }
179 :
180 :
181 : /*
182 : ** Returns the position of the instruction "controlling" a given
183 : ** jump (that is, its condition), or the jump itself if it is
184 : ** unconditional.
185 : */
186 11620 : static Instruction *getjumpcontrol (FuncState *fs, int pc) {
187 11620 : Instruction *pi = &fs->f->code[pc];
188 11620 : if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
189 10199 : return pi-1;
190 : else
191 1421 : return pi;
192 : }
193 :
194 :
195 : /*
196 : ** Patch destination register for a TESTSET instruction.
197 : ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
198 : ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
199 : ** register. Otherwise, change instruction to a simple 'TEST' (produces
200 : ** no register value)
201 : */
202 7437 : static int patchtestreg (FuncState *fs, int node, int reg) {
203 7437 : Instruction *i = getjumpcontrol(fs, node);
204 7437 : if (GET_OPCODE(*i) != OP_TESTSET)
205 6335 : return 0; /* cannot patch other instructions */
206 1102 : if (reg != NO_REG && reg != GETARG_B(*i))
207 72 : SETARG_A(*i, reg);
208 : else {
209 : /* no register to put value or register already has the value;
210 : change instruction to simple test */
211 1030 : *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
212 : }
213 1102 : return 1;
214 : }
215 :
216 :
217 : /*
218 : ** Traverse a list of tests ensuring no one produces a value
219 : */
220 1706 : static void removevalues (FuncState *fs, int list) {
221 1760 : for (; list != NO_JUMP; list = getjump(fs, list))
222 54 : patchtestreg(fs, list, NO_REG);
223 1706 : }
224 :
225 :
226 : /*
227 : ** Traverse a list of tests, patching their destination address and
228 : ** registers: tests producing values jump to 'vtarget' (and put their
229 : ** values in 'reg'), other tests jump to 'dtarget'.
230 : */
231 101769 : static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
232 : int dtarget) {
233 109152 : while (list != NO_JUMP) {
234 7383 : int next = getjump(fs, list);
235 7383 : if (patchtestreg(fs, list, reg))
236 1090 : fixjump(fs, list, vtarget);
237 : else
238 6293 : fixjump(fs, list, dtarget); /* jump to default target */
239 7383 : list = next;
240 : }
241 101769 : }
242 :
243 :
244 : /*
245 : ** Ensure all pending jumps to current position are fixed (jumping
246 : ** to current position with no values) and reset list of pending
247 : ** jumps
248 : */
249 98647 : static void dischargejpc (FuncState *fs) {
250 98647 : patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
251 98647 : fs->jpc = NO_JUMP;
252 98647 : }
253 :
254 :
255 : /*
256 : ** Add elements in 'list' to list of pending jumps to "here"
257 : ** (current position)
258 : */
259 13143 : void luaK_patchtohere (FuncState *fs, int list) {
260 13143 : luaK_getlabel(fs); /* mark "here" as a jump target */
261 13143 : luaK_concat(fs, &fs->jpc, list);
262 13143 : }
263 :
264 :
265 : /*
266 : ** Path all jumps in 'list' to jump to 'target'.
267 : ** (The assert means that we cannot fix a jump to a forward address
268 : ** because we only know addresses once code is generated.)
269 : */
270 642 : void luaK_patchlist (FuncState *fs, int list, int target) {
271 642 : if (target == fs->pc) /* 'target' is current position? */
272 184 : luaK_patchtohere(fs, list); /* add list to pending jumps */
273 : else {
274 : lua_assert(target < fs->pc);
275 458 : patchlistaux(fs, list, target, NO_REG, target);
276 : }
277 642 : }
278 :
279 :
280 : /*
281 : ** Path all jumps in 'list' to close upvalues up to given 'level'
282 : ** (The assertion checks that jumps either were closing nothing
283 : ** or were closing higher levels, from inner blocks.)
284 : */
285 12 : void luaK_patchclose (FuncState *fs, int list, int level) {
286 12 : level++; /* argument is +1 to reserve 0 as non-op */
287 32 : for (; list != NO_JUMP; list = getjump(fs, list)) {
288 : lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
289 : (GETARG_A(fs->f->code[list]) == 0 ||
290 : GETARG_A(fs->f->code[list]) >= level));
291 20 : SETARG_A(fs->f->code[list], level);
292 : }
293 12 : }
294 :
295 :
296 : /*
297 : ** Emit instruction 'i', checking for array sizes and saving also its
298 : ** line information. Return 'i' position.
299 : */
300 98647 : static int luaK_code (FuncState *fs, Instruction i) {
301 98647 : Proto *f = fs->f;
302 98647 : dischargejpc(fs); /* 'pc' will change */
303 : /* put new instruction in code array */
304 98647 : luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
305 : MAX_INT, "opcodes");
306 98647 : f->code[fs->pc] = i;
307 : /* save corresponding line information */
308 98647 : luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
309 : MAX_INT, "opcodes");
310 98647 : f->lineinfo[fs->pc] = fs->ls->lastline;
311 98647 : return fs->pc++;
312 : }
313 :
314 :
315 : /*
316 : ** Format and emit an 'iABC' instruction. (Assertions check consistency
317 : ** of parameters versus opcode.)
318 : */
319 79102 : int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
320 : lua_assert(getOpMode(o) == iABC);
321 : lua_assert(getBMode(o) != OpArgN || b == 0);
322 : lua_assert(getCMode(o) != OpArgN || c == 0);
323 : lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
324 79102 : return luaK_code(fs, CREATE_ABC(o, a, b, c));
325 : }
326 :
327 :
328 : /*
329 : ** Format and emit an 'iABx' instruction.
330 : */
331 19545 : int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
332 : lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
333 : lua_assert(getCMode(o) == OpArgN);
334 : lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
335 19545 : return luaK_code(fs, CREATE_ABx(o, a, bc));
336 : }
337 :
338 :
339 : /*
340 : ** Emit an "extra argument" instruction (format 'iAx')
341 : */
342 0 : static int codeextraarg (FuncState *fs, int a) {
343 : lua_assert(a <= MAXARG_Ax);
344 0 : return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
345 : }
346 :
347 :
348 : /*
349 : ** Emit a "load constant" instruction, using either 'OP_LOADK'
350 : ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
351 : ** instruction with "extra argument".
352 : */
353 10453 : int luaK_codek (FuncState *fs, int reg, int k) {
354 10453 : if (k <= MAXARG_Bx)
355 10453 : return luaK_codeABx(fs, OP_LOADK, reg, k);
356 : else {
357 0 : int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
358 0 : codeextraarg(fs, k);
359 0 : return p;
360 : }
361 : }
362 :
363 :
364 : /*
365 : ** Check register-stack level, keeping track of its maximum size
366 : ** in field 'maxstacksize'
367 : */
368 60927 : void luaK_checkstack (FuncState *fs, int n) {
369 60927 : int newstack = fs->freereg + n;
370 60927 : if (newstack > fs->f->maxstacksize) {
371 11754 : if (newstack >= MAXREGS)
372 0 : luaX_syntaxerror(fs->ls,
373 : "function or expression needs too many registers");
374 11754 : fs->f->maxstacksize = cast_byte(newstack);
375 : }
376 60927 : }
377 :
378 :
379 : /*
380 : ** Reserve 'n' registers in register stack
381 : */
382 60615 : void luaK_reserveregs (FuncState *fs, int n) {
383 60615 : luaK_checkstack(fs, n);
384 60615 : fs->freereg += n;
385 60615 : }
386 :
387 :
388 : /*
389 : ** Free register 'reg', if it is neither a constant index nor
390 : ** a local variable.
391 : )
392 : */
393 61676 : static void freereg (FuncState *fs, int reg) {
394 61676 : if (!ISK(reg) && reg >= fs->nactvar) {
395 23590 : fs->freereg--;
396 : lua_assert(reg == fs->freereg);
397 : }
398 61676 : }
399 :
400 :
401 : /*
402 : ** Free register used by expression 'e' (if any)
403 : */
404 69418 : static void freeexp (FuncState *fs, expdesc *e) {
405 69418 : if (e->k == VNONRELOC)
406 18301 : freereg(fs, e->u.info);
407 69418 : }
408 :
409 :
410 : /*
411 : ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
412 : ** order.
413 : */
414 8064 : static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
415 8064 : int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
416 8064 : int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
417 8064 : if (r1 > r2) {
418 4346 : freereg(fs, r1);
419 4346 : freereg(fs, r2);
420 : }
421 : else {
422 3718 : freereg(fs, r2);
423 3718 : freereg(fs, r1);
424 : }
425 8064 : }
426 :
427 :
428 : /*
429 : ** Add constant 'v' to prototype's list of constants (field 'k').
430 : ** Use scanner's table to cache position of constants in constant list
431 : ** and try to reuse constants. Because some values should not be used
432 : ** as keys (nil cannot be a key, integer keys can collapse with float
433 : ** keys), the caller must provide a useful 'key' for indexing the cache.
434 : */
435 47966 : static int addk (FuncState *fs, TValue *key, TValue *v) {
436 47966 : lua_State *L = fs->ls->L;
437 47966 : Proto *f = fs->f;
438 47966 : TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
439 : int k, oldsize;
440 47966 : if (ttisinteger(idx)) { /* is there an index there? */
441 33763 : k = cast_int(ivalue(idx));
442 : /* correct value? (warning: must distinguish floats from integers!) */
443 58895 : if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
444 25132 : luaV_rawequalobj(&f->k[k], v))
445 22373 : return k; /* reuse index */
446 : }
447 : /* constant not found; create a new entry */
448 25593 : oldsize = f->sizek;
449 25593 : k = fs->nk;
450 : /* numerical value does not need GC barrier;
451 : table has no metatable, so it does not need to invalidate cache */
452 25593 : setivalue(idx, k);
453 25593 : luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
454 61517 : while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
455 25593 : setobj(L, &f->k[k], v);
456 25593 : fs->nk++;
457 25593 : luaC_barrier(L, f, v);
458 25593 : return k;
459 : }
460 :
461 :
462 : /*
463 : ** Add a string to list of constants and return its index.
464 : */
465 35347 : int luaK_stringK (FuncState *fs, TString *s) {
466 : TValue o;
467 35347 : setsvalue(fs->ls->L, &o, s);
468 35347 : return addk(fs, &o, &o); /* use string itself as key */
469 : }
470 :
471 :
472 : /*
473 : ** Add an integer to list of constants and return its index.
474 : ** Integers use userdata as keys to avoid collision with floats with
475 : ** same value; conversion to 'void*' is used only for hashing, so there
476 : ** are no "precision" problems.
477 : */
478 9560 : int luaK_intK (FuncState *fs, lua_Integer n) {
479 : TValue k, o;
480 9560 : setpvalue(&k, cast(void*, cast(size_t, n)));
481 9560 : setivalue(&o, n);
482 9560 : return addk(fs, &k, &o);
483 : }
484 :
485 : /*
486 : ** Add a float to list of constants and return its index.
487 : */
488 1856 : static int luaK_numberK (FuncState *fs, lua_Number r) {
489 : TValue o;
490 1856 : setfltvalue(&o, r);
491 1856 : return addk(fs, &o, &o); /* use number itself as key */
492 : }
493 :
494 :
495 : /*
496 : ** Add a boolean to list of constants and return its index.
497 : */
498 452 : static int boolK (FuncState *fs, int b) {
499 : TValue o;
500 452 : setbvalue(&o, b);
501 452 : return addk(fs, &o, &o); /* use boolean itself as key */
502 : }
503 :
504 :
505 : /*
506 : ** Add nil to list of constants and return its index.
507 : */
508 751 : static int nilK (FuncState *fs) {
509 : TValue k, v;
510 751 : setnilvalue(&v);
511 : /* cannot use nil as key; instead use table itself to represent nil */
512 751 : sethvalue(fs->ls->L, &k, fs->ls->h);
513 751 : return addk(fs, &k, &v);
514 : }
515 :
516 :
517 : /*
518 : ** Fix an expression to return the number of results 'nresults'.
519 : ** Either 'e' is a multi-ret expression (function call or vararg)
520 : ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
521 : */
522 13739 : void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
523 13739 : if (e->k == VCALL) { /* expression is an open function call? */
524 4809 : SETARG_C(getinstruction(fs, e), nresults + 1);
525 : }
526 8930 : else if (e->k == VVARARG) {
527 4 : Instruction *pc = &getinstruction(fs, e);
528 4 : SETARG_B(*pc, nresults + 1);
529 4 : SETARG_A(*pc, fs->freereg);
530 4 : luaK_reserveregs(fs, 1);
531 : }
532 : else lua_assert(nresults == LUA_MULTRET);
533 13739 : }
534 :
535 :
536 : /*
537 : ** Fix an expression to return one result.
538 : ** If expression is not a multi-ret expression (function call or
539 : ** vararg), it already returns one result, so nothing needs to be done.
540 : ** Function calls become VNONRELOC expressions (as its result comes
541 : ** fixed in the base register of the call), while vararg expressions
542 : ** become VRELOCABLE (as OP_VARARG puts its results where it wants).
543 : ** (Calls are created returning one result, so that does not need
544 : ** to be fixed.)
545 : */
546 7678 : void luaK_setoneret (FuncState *fs, expdesc *e) {
547 7678 : if (e->k == VCALL) { /* expression is an open function call? */
548 : /* already returns 1 value */
549 : lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
550 4984 : e->k = VNONRELOC; /* result has fixed position */
551 4984 : e->u.info = GETARG_A(getinstruction(fs, e));
552 : }
553 2694 : else if (e->k == VVARARG) {
554 0 : SETARG_B(getinstruction(fs, e), 2);
555 0 : e->k = VRELOCABLE; /* can relocate its simple result */
556 : }
557 7678 : }
558 :
559 :
560 : /*
561 : ** Ensure that expression 'e' is not a variable.
562 : */
563 208320 : void luaK_dischargevars (FuncState *fs, expdesc *e) {
564 208320 : switch (e->k) {
565 15531 : case VLOCAL: { /* already in a register */
566 15531 : e->k = VNONRELOC; /* becomes a non-relocatable value */
567 15531 : break;
568 : }
569 4965 : case VUPVAL: { /* move value to some (pending) register */
570 4965 : e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
571 4965 : e->k = VRELOCABLE;
572 4965 : break;
573 : }
574 20656 : case VINDEXED: {
575 : OpCode op;
576 20656 : freereg(fs, e->u.ind.idx);
577 20656 : if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */
578 6591 : freereg(fs, e->u.ind.t);
579 6591 : op = OP_GETTABLE;
580 : }
581 : else {
582 : lua_assert(e->u.ind.vt == VUPVAL);
583 14065 : op = OP_GETTABUP; /* 't' is in an upvalue */
584 : }
585 20656 : e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
586 20656 : e->k = VRELOCABLE;
587 20656 : break;
588 : }
589 3674 : case VVARARG: case VCALL: {
590 3674 : luaK_setoneret(fs, e);
591 3674 : break;
592 : }
593 163494 : default: break; /* there is one value available (somewhere) */
594 : }
595 208320 : }
596 :
597 :
598 : /*
599 : ** Ensures expression value is in register 'reg' (and therefore
600 : ** 'e' will become a non-relocatable expression).
601 : */
602 53778 : static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
603 53778 : luaK_dischargevars(fs, e);
604 53778 : switch (e->k) {
605 190 : case VNIL: {
606 190 : luaK_nil(fs, reg, 1);
607 190 : break;
608 : }
609 1714 : case VFALSE: case VTRUE: {
610 1714 : luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
611 1714 : break;
612 : }
613 4675 : case VK: {
614 4675 : luaK_codek(fs, reg, e->u.info);
615 4675 : break;
616 : }
617 804 : case VKFLT: {
618 804 : luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
619 804 : break;
620 : }
621 4888 : case VKINT: {
622 4888 : luaK_codek(fs, reg, luaK_intK(fs, e->u.ival));
623 4888 : break;
624 : }
625 33025 : case VRELOCABLE: {
626 33025 : Instruction *pc = &getinstruction(fs, e);
627 33025 : SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
628 33025 : break;
629 : }
630 7427 : case VNONRELOC: {
631 7427 : if (reg != e->u.info)
632 5385 : luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
633 7427 : break;
634 : }
635 1055 : default: {
636 : lua_assert(e->k == VJMP);
637 1055 : return; /* nothing to do... */
638 : }
639 : }
640 52723 : e->u.info = reg;
641 52723 : e->k = VNONRELOC;
642 : }
643 :
644 :
645 : /*
646 : ** Ensures expression value is in any register.
647 : */
648 1833 : static void discharge2anyreg (FuncState *fs, expdesc *e) {
649 1833 : if (e->k != VNONRELOC) { /* no fixed register yet? */
650 744 : luaK_reserveregs(fs, 1); /* get a register */
651 744 : discharge2reg(fs, e, fs->freereg-1); /* put value there */
652 : }
653 1833 : }
654 :
655 :
656 2124 : static int code_loadbool (FuncState *fs, int A, int b, int jump) {
657 2124 : luaK_getlabel(fs); /* those instructions may be jump targets */
658 2124 : return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
659 : }
660 :
661 :
662 : /*
663 : ** check whether list has any jump that do not produce a value
664 : ** or produce an inverted value
665 : */
666 1608 : static int need_value (FuncState *fs, int list) {
667 1888 : for (; list != NO_JUMP; list = getjump(fs, list)) {
668 1342 : Instruction i = *getjumpcontrol(fs, list);
669 1342 : if (GET_OPCODE(i) != OP_TESTSET) return 1;
670 : }
671 546 : return 0; /* not found */
672 : }
673 :
674 :
675 : /*
676 : ** Ensures final expression result (including results from its jump
677 : ** lists) is in register 'reg'.
678 : ** If expression has jumps, need to patch these jumps either to
679 : ** its final position or to "load" instructions (for those tests
680 : ** that do not produce values).
681 : */
682 53034 : static void exp2reg (FuncState *fs, expdesc *e, int reg) {
683 53034 : discharge2reg(fs, e, reg);
684 53034 : if (e->k == VJMP) /* expression itself is a test? */
685 1055 : luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
686 53034 : if (hasjumps(e)) {
687 : int final; /* position after whole expression */
688 1332 : int p_f = NO_JUMP; /* position of an eventual LOAD false */
689 1332 : int p_t = NO_JUMP; /* position of an eventual LOAD true */
690 1332 : if (need_value(fs, e->t) || need_value(fs, e->f)) {
691 1062 : int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
692 1062 : p_f = code_loadbool(fs, reg, 0, 1);
693 1062 : p_t = code_loadbool(fs, reg, 1, 0);
694 1062 : luaK_patchtohere(fs, fj);
695 : }
696 1332 : final = luaK_getlabel(fs);
697 1332 : patchlistaux(fs, e->f, final, reg, p_f);
698 1332 : patchlistaux(fs, e->t, final, reg, p_t);
699 : }
700 53034 : e->f = e->t = NO_JUMP;
701 53034 : e->u.info = reg;
702 53034 : e->k = VNONRELOC;
703 53034 : }
704 :
705 :
706 : /*
707 : ** Ensures final expression result (including results from its jump
708 : ** lists) is in next available register.
709 : */
710 52029 : void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
711 52029 : luaK_dischargevars(fs, e);
712 52029 : freeexp(fs, e);
713 52029 : luaK_reserveregs(fs, 1);
714 52029 : exp2reg(fs, e, fs->freereg - 1);
715 52029 : }
716 :
717 :
718 : /*
719 : ** Ensures final expression result (including results from its jump
720 : ** lists) is in some (any) register and return that register.
721 : */
722 34476 : int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
723 34476 : luaK_dischargevars(fs, e);
724 34476 : if (e->k == VNONRELOC) { /* expression already has a register? */
725 18608 : if (!hasjumps(e)) /* no jumps? */
726 18586 : return e->u.info; /* result is already in a register */
727 22 : if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
728 13 : exp2reg(fs, e, e->u.info); /* put final result in it */
729 13 : return e->u.info;
730 : }
731 : }
732 15877 : luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
733 15877 : return e->u.info;
734 : }
735 :
736 :
737 : /*
738 : ** Ensures final expression result is either in a register or in an
739 : ** upvalue.
740 : */
741 9390 : void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
742 9390 : if (e->k != VUPVAL || hasjumps(e))
743 7678 : luaK_exp2anyreg(fs, e);
744 9390 : }
745 :
746 :
747 : /*
748 : ** Ensures final expression result is either in a register or it is
749 : ** a constant.
750 : */
751 59460 : void luaK_exp2val (FuncState *fs, expdesc *e) {
752 59460 : if (hasjumps(e))
753 25 : luaK_exp2anyreg(fs, e);
754 : else
755 59435 : luaK_dischargevars(fs, e);
756 59460 : }
757 :
758 :
759 : /*
760 : ** Ensures final expression result is in a valid R/K index
761 : ** (that is, it is either in a register or in 'k' with an index
762 : ** in the range of R/K indices).
763 : ** Returns R/K index.
764 : */
765 56283 : int luaK_exp2RK (FuncState *fs, expdesc *e) {
766 56283 : luaK_exp2val(fs, e);
767 56283 : switch (e->k) { /* move constants to 'k' */
768 347 : case VTRUE: e->u.info = boolK(fs, 1); goto vk;
769 105 : case VFALSE: e->u.info = boolK(fs, 0); goto vk;
770 751 : case VNIL: e->u.info = nilK(fs); goto vk;
771 4586 : case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk;
772 1052 : case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk;
773 : case VK:
774 38065 : vk:
775 38065 : e->k = VK;
776 38065 : if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
777 37529 : return RKASK(e->u.info);
778 536 : else break;
779 18218 : default: break;
780 : }
781 : /* not a constant in the right range: put it in a register */
782 18754 : return luaK_exp2anyreg(fs, e);
783 : }
784 :
785 :
786 : /*
787 : ** Generate code to store result of expression 'ex' into variable 'var'.
788 : */
789 5103 : void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
790 5103 : switch (var->k) {
791 992 : case VLOCAL: {
792 992 : freeexp(fs, ex);
793 992 : exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */
794 992 : return;
795 : }
796 1119 : case VUPVAL: {
797 1119 : int e = luaK_exp2anyreg(fs, ex);
798 1119 : luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
799 1119 : break;
800 : }
801 2992 : case VINDEXED: {
802 2992 : OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
803 2992 : int e = luaK_exp2RK(fs, ex);
804 2992 : luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
805 2992 : break;
806 : }
807 4111 : default: lua_assert(0); /* invalid var kind to store */
808 : }
809 4111 : freeexp(fs, ex);
810 : }
811 :
812 :
813 : /*
814 : ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
815 : */
816 4935 : void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
817 : int ereg;
818 4935 : luaK_exp2anyreg(fs, e);
819 4935 : ereg = e->u.info; /* register where 'e' was placed */
820 4935 : freeexp(fs, e);
821 4935 : e->u.info = fs->freereg; /* base register for op_self */
822 4935 : e->k = VNONRELOC; /* self expression has a fixed register */
823 4935 : luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
824 4935 : luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
825 4935 : freeexp(fs, key);
826 4935 : }
827 :
828 :
829 : /*
830 : ** Negate condition 'e' (where 'e' is a comparison).
831 : */
832 2841 : static void negatecondition (FuncState *fs, expdesc *e) {
833 2841 : Instruction *pc = getjumpcontrol(fs, e->u.info);
834 : lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
835 : GET_OPCODE(*pc) != OP_TEST);
836 2841 : SETARG_A(*pc, !(GETARG_A(*pc)));
837 2841 : }
838 :
839 :
840 : /*
841 : ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
842 : ** is true, code will jump if 'e' is true.) Return jump position.
843 : ** Optimize when 'e' is 'not' something, inverting the condition
844 : ** and removing the 'not'.
845 : */
846 1775 : static int jumponcond (FuncState *fs, expdesc *e, int cond) {
847 1775 : if (e->k == VRELOCABLE) {
848 1158 : Instruction ie = getinstruction(fs, e);
849 1158 : if (GET_OPCODE(ie) == OP_NOT) {
850 673 : fs->pc--; /* remove previous OP_NOT */
851 673 : return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
852 : }
853 : /* else go through */
854 : }
855 1102 : discharge2anyreg(fs, e);
856 1102 : freeexp(fs, e);
857 1102 : return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
858 : }
859 :
860 :
861 : /*
862 : ** Emit code to go through if 'e' is true, jump otherwise.
863 : */
864 4137 : void luaK_goiftrue (FuncState *fs, expdesc *e) {
865 : int pc; /* pc of new jump */
866 4137 : luaK_dischargevars(fs, e);
867 4137 : switch (e->k) {
868 2719 : case VJMP: { /* condition? */
869 2719 : negatecondition(fs, e); /* jump when it is false */
870 2719 : pc = e->u.info; /* save jump position */
871 2719 : break;
872 : }
873 34 : case VK: case VKFLT: case VKINT: case VTRUE: {
874 34 : pc = NO_JUMP; /* always true; do nothing */
875 34 : break;
876 : }
877 1384 : default: {
878 1384 : pc = jumponcond(fs, e, 0); /* jump when false */
879 1384 : break;
880 : }
881 : }
882 4137 : luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
883 4137 : luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
884 4137 : e->t = NO_JUMP;
885 4137 : }
886 :
887 :
888 : /*
889 : ** Emit code to go through if 'e' is false, jump otherwise.
890 : */
891 804 : void luaK_goiffalse (FuncState *fs, expdesc *e) {
892 : int pc; /* pc of new jump */
893 804 : luaK_dischargevars(fs, e);
894 804 : switch (e->k) {
895 413 : case VJMP: {
896 413 : pc = e->u.info; /* already jump if true */
897 413 : break;
898 : }
899 0 : case VNIL: case VFALSE: {
900 0 : pc = NO_JUMP; /* always false; do nothing */
901 0 : break;
902 : }
903 391 : default: {
904 391 : pc = jumponcond(fs, e, 1); /* jump if true */
905 391 : break;
906 : }
907 : }
908 804 : luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
909 804 : luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
910 804 : e->f = NO_JUMP;
911 804 : }
912 :
913 :
914 : /*
915 : ** Code 'not e', doing constant folding.
916 : */
917 853 : static void codenot (FuncState *fs, expdesc *e) {
918 853 : luaK_dischargevars(fs, e);
919 853 : switch (e->k) {
920 0 : case VNIL: case VFALSE: {
921 0 : e->k = VTRUE; /* true == not nil == not false */
922 0 : break;
923 : }
924 0 : case VK: case VKFLT: case VKINT: case VTRUE: {
925 0 : e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
926 0 : break;
927 : }
928 122 : case VJMP: {
929 122 : negatecondition(fs, e);
930 122 : break;
931 : }
932 731 : case VRELOCABLE:
933 : case VNONRELOC: {
934 731 : discharge2anyreg(fs, e);
935 731 : freeexp(fs, e);
936 731 : e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
937 731 : e->k = VRELOCABLE;
938 731 : break;
939 : }
940 853 : default: lua_assert(0); /* cannot happen */
941 : }
942 : /* interchange true and false lists */
943 853 : { int temp = e->f; e->f = e->t; e->t = temp; }
944 853 : removevalues(fs, e->f); /* values are useless when negated */
945 853 : removevalues(fs, e->t);
946 853 : }
947 :
948 :
949 : /*
950 : ** Create expression 't[k]'. 't' must have its final result already in a
951 : ** register or upvalue.
952 : */
953 23648 : void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
954 : lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL));
955 23648 : t->u.ind.t = t->u.info; /* register or upvalue index */
956 23648 : t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */
957 23648 : t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL;
958 23648 : t->k = VINDEXED;
959 23648 : }
960 :
961 :
962 : /*
963 : ** Return false if folding can raise an error.
964 : ** Bitwise operations need operands convertible to integers; division
965 : ** operations cannot have 0 as divisor.
966 : */
967 1004 : static int validop (int op, TValue *v1, TValue *v2) {
968 1004 : switch (op) {
969 58 : case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
970 : case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
971 : lua_Integer i;
972 58 : return (tointeger(v1, &i) && tointeger(v2, &i));
973 : }
974 77 : case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
975 77 : return (nvalue(v2) != 0);
976 869 : default: return 1; /* everything else is valid */
977 : }
978 : }
979 :
980 :
981 : /*
982 : ** Try to "constant-fold" an operation; return 1 iff successful.
983 : ** (In this case, 'e1' has the final result.)
984 : */
985 4462 : static int constfolding (FuncState *fs, int op, expdesc *e1,
986 : const expdesc *e2) {
987 : TValue v1, v2, res;
988 4462 : if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
989 3506 : return 0; /* non-numeric operands or not safe to fold */
990 956 : luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
991 956 : if (ttisinteger(&res)) {
992 725 : e1->k = VKINT;
993 725 : e1->u.ival = ivalue(&res);
994 : }
995 : else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
996 231 : lua_Number n = fltvalue(&res);
997 231 : if (luai_numisnan(n) || n == 0)
998 12 : return 0;
999 219 : e1->k = VKFLT;
1000 219 : e1->u.nval = n;
1001 : }
1002 944 : return 1;
1003 : }
1004 :
1005 :
1006 : /*
1007 : ** Emit code for unary expressions that "produce values"
1008 : ** (everything but 'not').
1009 : ** Expression to produce final result will be encoded in 'e'.
1010 : */
1011 379 : static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1012 379 : int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
1013 379 : freeexp(fs, e);
1014 379 : e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
1015 379 : e->k = VRELOCABLE; /* all those operations are relocatable */
1016 379 : luaK_fixline(fs, line);
1017 379 : }
1018 :
1019 :
1020 : /*
1021 : ** Emit code for binary expressions that "produce values"
1022 : ** (everything but logical operators 'and'/'or' and comparison
1023 : ** operators).
1024 : ** Expression to produce final result will be encoded in 'e1'.
1025 : ** Because 'luaK_exp2RK' can free registers, its calls must be
1026 : ** in "stack order" (that is, first on 'e2', which may have more
1027 : ** recent registers to be released).
1028 : */
1029 3877 : static void codebinexpval (FuncState *fs, OpCode op,
1030 : expdesc *e1, expdesc *e2, int line) {
1031 3877 : int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
1032 3877 : int rk1 = luaK_exp2RK(fs, e1);
1033 3877 : freeexps(fs, e1, e2);
1034 3877 : e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */
1035 3877 : e1->k = VRELOCABLE; /* all those operations are relocatable */
1036 3877 : luaK_fixline(fs, line);
1037 3877 : }
1038 :
1039 :
1040 : /*
1041 : ** Emit code for comparisons.
1042 : ** 'e1' was already put in R/K form by 'luaK_infix'.
1043 : */
1044 4187 : static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1045 8562 : int rk1 = (e1->k == VK) ? RKASK(e1->u.info)
1046 4187 : : check_exp(e1->k == VNONRELOC, e1->u.info);
1047 4187 : int rk2 = luaK_exp2RK(fs, e2);
1048 4187 : freeexps(fs, e1, e2);
1049 4187 : switch (opr) {
1050 672 : case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */
1051 672 : e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2);
1052 672 : break;
1053 : }
1054 764 : case OPR_GT: case OPR_GE: {
1055 : /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */
1056 764 : OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
1057 764 : e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */
1058 764 : break;
1059 : }
1060 2751 : default: { /* '==', '<', '<=' use their own opcodes */
1061 2751 : OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
1062 2751 : e1->u.info = condjump(fs, op, 1, rk1, rk2);
1063 2751 : break;
1064 : }
1065 : }
1066 4187 : e1->k = VJMP;
1067 4187 : }
1068 :
1069 :
1070 : /*
1071 : ** Apply prefix operation 'op' to expression 'e'.
1072 : */
1073 1967 : void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
1074 : static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1075 1967 : switch (op) {
1076 1000 : case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
1077 1000 : if (constfolding(fs, op + LUA_OPUNM, e, &ef))
1078 735 : break;
1079 : /* FALLTHROUGH */
1080 : case OPR_LEN:
1081 379 : codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
1082 379 : break;
1083 853 : case OPR_NOT: codenot(fs, e); break;
1084 1967 : default: lua_assert(0);
1085 : }
1086 1967 : }
1087 :
1088 :
1089 : /*
1090 : ** Process 1st operand 'v' of binary operation 'op' before reading
1091 : ** 2nd operand.
1092 : */
1093 9988 : void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1094 9988 : switch (op) {
1095 801 : case OPR_AND: {
1096 801 : luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
1097 801 : break;
1098 : }
1099 710 : case OPR_OR: {
1100 710 : luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
1101 710 : break;
1102 : }
1103 828 : case OPR_CONCAT: {
1104 828 : luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
1105 828 : break;
1106 : }
1107 3462 : case OPR_ADD: case OPR_SUB:
1108 : case OPR_MUL: case OPR_DIV: case OPR_IDIV:
1109 : case OPR_MOD: case OPR_POW:
1110 : case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1111 : case OPR_SHL: case OPR_SHR: {
1112 3462 : if (!tonumeral(v, NULL))
1113 2736 : luaK_exp2RK(fs, v);
1114 : /* else keep numeral, which may be folded with 2nd operand */
1115 3462 : break;
1116 : }
1117 4187 : default: {
1118 4187 : luaK_exp2RK(fs, v);
1119 4187 : break;
1120 : }
1121 : }
1122 9988 : }
1123 :
1124 :
1125 : /*
1126 : ** Finalize code for binary operation, after reading 2nd operand.
1127 : ** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
1128 : ** concatenation is right associative), merge second CONCAT into first
1129 : ** one.
1130 : */
1131 9988 : void luaK_posfix (FuncState *fs, BinOpr op,
1132 : expdesc *e1, expdesc *e2, int line) {
1133 9988 : switch (op) {
1134 801 : case OPR_AND: {
1135 : lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
1136 801 : luaK_dischargevars(fs, e2);
1137 801 : luaK_concat(fs, &e2->f, e1->f);
1138 801 : *e1 = *e2;
1139 801 : break;
1140 : }
1141 710 : case OPR_OR: {
1142 : lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
1143 710 : luaK_dischargevars(fs, e2);
1144 710 : luaK_concat(fs, &e2->t, e1->t);
1145 710 : *e1 = *e2;
1146 710 : break;
1147 : }
1148 828 : case OPR_CONCAT: {
1149 828 : luaK_exp2val(fs, e2);
1150 828 : if (e2->k == VRELOCABLE &&
1151 280 : GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) {
1152 : lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1);
1153 204 : freeexp(fs, e1);
1154 204 : SETARG_B(getinstruction(fs, e2), e1->u.info);
1155 204 : e1->k = VRELOCABLE; e1->u.info = e2->u.info;
1156 : }
1157 : else {
1158 624 : luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
1159 624 : codebinexpval(fs, OP_CONCAT, e1, e2, line);
1160 : }
1161 828 : break;
1162 : }
1163 3462 : case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
1164 : case OPR_IDIV: case OPR_MOD: case OPR_POW:
1165 : case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1166 : case OPR_SHL: case OPR_SHR: {
1167 3462 : if (!constfolding(fs, op + LUA_OPADD, e1, e2))
1168 3253 : codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line);
1169 3462 : break;
1170 : }
1171 4187 : case OPR_EQ: case OPR_LT: case OPR_LE:
1172 : case OPR_NE: case OPR_GT: case OPR_GE: {
1173 4187 : codecomp(fs, op, e1, e2);
1174 4187 : break;
1175 : }
1176 9988 : default: lua_assert(0);
1177 : }
1178 9988 : }
1179 :
1180 :
1181 : /*
1182 : ** Change line information associated with current position.
1183 : */
1184 20245 : void luaK_fixline (FuncState *fs, int line) {
1185 20245 : fs->f->lineinfo[fs->pc - 1] = line;
1186 20245 : }
1187 :
1188 :
1189 : /*
1190 : ** Emit a SETLIST instruction.
1191 : ** 'base' is register that keeps table;
1192 : ** 'nelems' is #table plus those to be stored now;
1193 : ** 'tostore' is number of values (in registers 'base + 1',...) to add to
1194 : ** table (or LUA_MULTRET to add up to stack top).
1195 : */
1196 523 : void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1197 523 : int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
1198 523 : int b = (tostore == LUA_MULTRET) ? 0 : tostore;
1199 : lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
1200 523 : if (c <= MAXARG_C)
1201 523 : luaK_codeABC(fs, OP_SETLIST, base, b, c);
1202 0 : else if (c <= MAXARG_Ax) {
1203 0 : luaK_codeABC(fs, OP_SETLIST, base, b, 0);
1204 0 : codeextraarg(fs, c);
1205 : }
1206 : else
1207 0 : luaX_syntaxerror(fs->ls, "constructor too long");
1208 523 : fs->freereg = base + 1; /* free registers with list values */
1209 523 : }
1210 :
|