interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lcode.c

1974 lines · 57364 bytes Raw Blame History
1/*
2** $Id: lcode.c $
3** Code generator for Lua
4** See Copyright Notice in lua.h
5*/
6
7#define lcode_c
8#define LUA_CORE
9
10#include "lprefix.h"
11
12
13#include <float.h>
14#include <limits.h>
15#include <math.h>
16#include <stdlib.h>
17
18#include "lua.h"
19
20#include "lcode.h"
21#include "ldebug.h"
22#include "ldo.h"
23#include "lgc.h"
24#include "llex.h"
25#include "lmem.h"
26#include "lobject.h"
27#include "lopcodes.h"
28#include "lparser.h"
29#include "lstring.h"
30#include "ltable.h"
31#include "lvm.h"
32
33
34/* (note that expressions VJMP also have jumps.) */
35#define hasjumps(e) ((e)->t != (e)->f)
36
37
38static int codesJ (FuncState *fs, OpCode o, int sj, int k);
39
40
41
42/* semantic error */
43l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
44 const char *msg;
45 va_list argp;
46 pushvfstring(ls->L, argp, fmt, msg);
47 ls->t.token = 0; /* remove "near <token>" from final message */
48 ls->linenumber = ls->lastline; /* back to line of last used token */
49 luaX_syntaxerror(ls, msg);
50}
51
52
53/*
54** If expression is a numeric constant, fills 'v' with its value
55** and returns 1. Otherwise, returns 0.
56*/
57static int tonumeral (const expdesc *e, TValue *v) {
58 if (hasjumps(e))
59 return 0; /* not a numeral */
60 switch (e->k) {
61 case VKINT:
62 if (v) setivalue(v, e->u.ival);
63 return 1;
64 case VKFLT:
65 if (v) setfltvalue(v, e->u.nval);
66 return 1;
67 default: return 0;
68 }
69}
70
71
72/*
73** Get the constant value from a constant expression
74*/
75static TValue *const2val (FuncState *fs, const expdesc *e) {
76 lua_assert(e->k == VCONST);
77 return &fs->ls->dyd->actvar.arr[e->u.info].k;
78}
79
80
81/*
82** If expression is a constant, fills 'v' with its value
83** and returns 1. Otherwise, returns 0.
84*/
85int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
86 if (hasjumps(e))
87 return 0; /* not a constant */
88 switch (e->k) {
89 case VFALSE:
90 setbfvalue(v);
91 return 1;
92 case VTRUE:
93 setbtvalue(v);
94 return 1;
95 case VNIL:
96 setnilvalue(v);
97 return 1;
98 case VKSTR: {
99 setsvalue(fs->ls->L, v, e->u.strval);
100 return 1;
101 }
102 case VCONST: {
103 setobj(fs->ls->L, v, const2val(fs, e));
104 return 1;
105 }
106 default: return tonumeral(e, v);
107 }
108}
109
110
111/*
112** Return the previous instruction of the current code. If there
113** may be a jump target between the current instruction and the
114** previous one, return an invalid instruction (to avoid wrong
115** optimizations).
116*/
117static Instruction *previousinstruction (FuncState *fs) {
118 static const Instruction invalidinstruction = ~(Instruction)0;
119 if (fs->pc > fs->lasttarget)
120 return &fs->f->code[fs->pc - 1]; /* previous instruction */
121 else
122 return cast(Instruction*, &invalidinstruction);
123}
124
125
126/*
127** Create a OP_LOADNIL instruction, but try to optimize: if the previous
128** instruction is also OP_LOADNIL and ranges are compatible, adjust
129** range of previous instruction instead of emitting a new one. (For
130** instance, 'local a; local b' will generate a single opcode.)
131*/
132void luaK_nil (FuncState *fs, int from, int n) {
133 int l = from + n - 1; /* last register to set nil */
134 Instruction *previous = previousinstruction(fs);
135 if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
136 int pfrom = GETARG_A(*previous); /* get previous range */
137 int pl = pfrom + GETARG_B(*previous);
138 if ((pfrom <= from && from <= pl + 1) ||
139 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
140 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
141 if (pl > l) l = pl; /* l = max(l, pl) */
142 SETARG_A(*previous, from);
143 SETARG_B(*previous, l - from);
144 return;
145 } /* else go through */
146 }
147 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
148}
149
150
151/*
152** Gets the destination address of a jump instruction. Used to traverse
153** a list of jumps.
154*/
155static int getjump (FuncState *fs, int pc) {
156 int offset = GETARG_sJ(fs->f->code[pc]);
157 if (offset == NO_JUMP) /* point to itself represents end of list */
158 return NO_JUMP; /* end of list */
159 else
160 return (pc+1)+offset; /* turn offset into absolute position */
161}
162
163
164/*
165** Fix jump instruction at position 'pc' to jump to 'dest'.
166** (Jump addresses are relative in Lua)
167*/
168static void fixjump (FuncState *fs, int pc, int dest) {
169 Instruction *jmp = &fs->f->code[pc];
170 int offset = dest - (pc + 1);
171 lua_assert(dest != NO_JUMP);
172 if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
173 luaX_syntaxerror(fs->ls, "control structure too long");
174 lua_assert(GET_OPCODE(*jmp) == OP_JMP);
175 SETARG_sJ(*jmp, offset);
176}
177
178
179/*
180** Concatenate jump-list 'l2' into jump-list 'l1'
181*/
182void luaK_concat (FuncState *fs, int *l1, int l2) {
183 if (l2 == NO_JUMP) return; /* nothing to concatenate? */
184 else if (*l1 == NO_JUMP) /* no original list? */
185 *l1 = l2; /* 'l1' points to 'l2' */
186 else {
187 int list = *l1;
188 int next;
189 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
190 list = next;
191 fixjump(fs, list, l2); /* last element links to 'l2' */
192 }
193}
194
195
196/*
197** Create a jump instruction and return its position, so its destination
198** can be fixed later (with 'fixjump').
199*/
200int luaK_jump (FuncState *fs) {
201 return codesJ(fs, OP_JMP, NO_JUMP, 0);
202}
203
204
205/*
206** Code a 'return' instruction
207*/
208void luaK_ret (FuncState *fs, int first, int nret) {
209 OpCode op;
210 switch (nret) {
211 case 0: op = OP_RETURN0; break;
212 case 1: op = OP_RETURN1; break;
213 default: op = OP_RETURN; break;
214 }
215 luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
216 luaK_codeABC(fs, op, first, nret + 1, 0);
217}
218
219
220/*
221** Code a "conditional jump", that is, a test or comparison opcode
222** followed by a jump. Return jump position.
223*/
224static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
225 luaK_codeABCk(fs, op, A, B, C, k);
226 return luaK_jump(fs);
227}
228
229
230/*
231** returns current 'pc' and marks it as a jump target (to avoid wrong
232** optimizations with consecutive instructions not in the same basic block).
233*/
234int luaK_getlabel (FuncState *fs) {
235 fs->lasttarget = fs->pc;
236 return fs->pc;
237}
238
239
240/*
241** Returns the position of the instruction "controlling" a given
242** jump (that is, its condition), or the jump itself if it is
243** unconditional.
244*/
245static Instruction *getjumpcontrol (FuncState *fs, int pc) {
246 Instruction *pi = &fs->f->code[pc];
247 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
248 return pi-1;
249 else
250 return pi;
251}
252
253
254/*
255** Patch destination register for a TESTSET instruction.
256** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
257** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
258** register. Otherwise, change instruction to a simple 'TEST' (produces
259** no register value)
260*/
261static int patchtestreg (FuncState *fs, int node, int reg) {
262 Instruction *i = getjumpcontrol(fs, node);
263 if (GET_OPCODE(*i) != OP_TESTSET)
264 return 0; /* cannot patch other instructions */
265 if (reg != NO_REG && reg != GETARG_B(*i))
266 SETARG_A(*i, reg);
267 else {
268 /* no register to put value or register already has the value;
269 change instruction to simple test */
270 *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
271 }
272 return 1;
273}
274
275
276/*
277** Traverse a list of tests ensuring no one produces a value
278*/
279static void removevalues (FuncState *fs, int list) {
280 for (; list != NO_JUMP; list = getjump(fs, list))
281 patchtestreg(fs, list, NO_REG);
282}
283
284
285/*
286** Traverse a list of tests, patching their destination address and
287** registers: tests producing values jump to 'vtarget' (and put their
288** values in 'reg'), other tests jump to 'dtarget'.
289*/
290static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
291 int dtarget) {
292 while (list != NO_JUMP) {
293 int next = getjump(fs, list);
294 if (patchtestreg(fs, list, reg))
295 fixjump(fs, list, vtarget);
296 else
297 fixjump(fs, list, dtarget); /* jump to default target */
298 list = next;
299 }
300}
301
302
303/*
304** Path all jumps in 'list' to jump to 'target'.
305** (The assert means that we cannot fix a jump to a forward address
306** because we only know addresses once code is generated.)
307*/
308void luaK_patchlist (FuncState *fs, int list, int target) {
309 lua_assert(target <= fs->pc);
310 patchlistaux(fs, list, target, NO_REG, target);
311}
312
313
314void luaK_patchtohere (FuncState *fs, int list) {
315 int hr = luaK_getlabel(fs); /* mark "here" as a jump target */
316 luaK_patchlist(fs, list, hr);
317}
318
319
320/* limit for difference between lines in relative line info. */
321#define LIMLINEDIFF 0x80
322
323
324/*
325** Save line info for a new instruction. If difference from last line
326** does not fit in a byte, of after that many instructions, save a new
327** absolute line info; (in that case, the special value 'ABSLINEINFO'
328** in 'lineinfo' signals the existence of this absolute information.)
329** Otherwise, store the difference from last line in 'lineinfo'.
330*/
331static void savelineinfo (FuncState *fs, Proto *f, int line) {
332 int linedif = line - fs->previousline;
333 int pc = fs->pc - 1; /* last instruction coded */
334 if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
335 luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
336 f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
337 f->abslineinfo[fs->nabslineinfo].pc = pc;
338 f->abslineinfo[fs->nabslineinfo++].line = line;
339 linedif = ABSLINEINFO; /* signal that there is absolute information */
340 fs->iwthabs = 1; /* restart counter */
341 }
342 luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
343 INT_MAX, "opcodes");
344 f->lineinfo[pc] = cast(ls_byte, linedif);
345 fs->previousline = line; /* last line saved */
346}
347
348
349/*
350** Remove line information from the last instruction.
351** If line information for that instruction is absolute, set 'iwthabs'
352** above its max to force the new (replacing) instruction to have
353** absolute line info, too.
354*/
355static void removelastlineinfo (FuncState *fs) {
356 Proto *f = fs->f;
357 int pc = fs->pc - 1; /* last instruction coded */
358 if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */
359 fs->previousline -= f->lineinfo[pc]; /* correct last line saved */
360 fs->iwthabs--; /* undo previous increment */
361 }
362 else { /* absolute line information */
363 lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
364 fs->nabslineinfo--; /* remove it */
365 fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */
366 }
367}
368
369
370/*
371** Remove the last instruction created, correcting line information
372** accordingly.
373*/
374static void removelastinstruction (FuncState *fs) {
375 removelastlineinfo(fs);
376 fs->pc--;
377}
378
379
380/*
381** Emit instruction 'i', checking for array sizes and saving also its
382** line information. Return 'i' position.
383*/
384int luaK_code (FuncState *fs, Instruction i) {
385 Proto *f = fs->f;
386 /* put new instruction in code array */
387 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
388 INT_MAX, "opcodes");
389 f->code[fs->pc++] = i;
390 savelineinfo(fs, f, fs->ls->lastline);
391 return fs->pc - 1; /* index of new instruction */
392}
393
394
395/*
396** Format and emit an 'iABC' instruction. (Assertions check consistency
397** of parameters versus opcode.)
398*/
399int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
400 lua_assert(getOpMode(o) == iABC);
401 lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
402 C <= MAXARG_C && (k & ~1) == 0);
403 return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
404}
405
406
407int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
408 lua_assert(getOpMode(o) == ivABC);
409 lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
410 C <= MAXARG_vC && (k & ~1) == 0);
411 return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
412}
413
414
415/*
416** Format and emit an 'iABx' instruction.
417*/
418int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
419 lua_assert(getOpMode(o) == iABx);
420 lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
421 return luaK_code(fs, CREATE_ABx(o, A, Bc));
422}
423
424
425/*
426** Format and emit an 'iAsBx' instruction.
427*/
428static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
429 int b = Bc + OFFSET_sBx;
430 lua_assert(getOpMode(o) == iAsBx);
431 lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
432 return luaK_code(fs, CREATE_ABx(o, A, b));
433}
434
435
436/*
437** Format and emit an 'isJ' instruction.
438*/
439static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
440 int j = sj + OFFSET_sJ;
441 lua_assert(getOpMode(o) == isJ);
442 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
443 return luaK_code(fs, CREATE_sJ(o, j, k));
444}
445
446
447/*
448** Emit an "extra argument" instruction (format 'iAx')
449*/
450static int codeextraarg (FuncState *fs, int A) {
451 lua_assert(A <= MAXARG_Ax);
452 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
453}
454
455
456/*
457** Emit a "load constant" instruction, using either 'OP_LOADK'
458** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
459** instruction with "extra argument".
460*/
461static int luaK_codek (FuncState *fs, int reg, int k) {
462 if (k <= MAXARG_Bx)
463 return luaK_codeABx(fs, OP_LOADK, reg, k);
464 else {
465 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
466 codeextraarg(fs, k);
467 return p;
468 }
469}
470
471
472/*
473** Check register-stack level, keeping track of its maximum size
474** in field 'maxstacksize'
475*/
476void luaK_checkstack (FuncState *fs, int n) {
477 int newstack = fs->freereg + n;
478 if (newstack > fs->f->maxstacksize) {
479 luaY_checklimit(fs, newstack, MAX_FSTACK, "registers");
480 fs->f->maxstacksize = cast_byte(newstack);
481 }
482}
483
484
485/*
486** Reserve 'n' registers in register stack
487*/
488void luaK_reserveregs (FuncState *fs, int n) {
489 luaK_checkstack(fs, n);
490 fs->freereg = cast_byte(fs->freereg + n);
491}
492
493
494/*
495** Free register 'reg', if it is neither a constant index nor
496** a local variable.
497)
498*/
499static void freereg (FuncState *fs, int reg) {
500 if (reg >= luaY_nvarstack(fs)) {
501 fs->freereg--;
502 lua_assert(reg == fs->freereg);
503 }
504}
505
506
507/*
508** Free two registers in proper order
509*/
510static void freeregs (FuncState *fs, int r1, int r2) {
511 if (r1 > r2) {
512 freereg(fs, r1);
513 freereg(fs, r2);
514 }
515 else {
516 freereg(fs, r2);
517 freereg(fs, r1);
518 }
519}
520
521
522/*
523** Free register used by expression 'e' (if any)
524*/
525static void freeexp (FuncState *fs, expdesc *e) {
526 if (e->k == VNONRELOC)
527 freereg(fs, e->u.info);
528}
529
530
531/*
532** Free registers used by expressions 'e1' and 'e2' (if any) in proper
533** order.
534*/
535static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
536 int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
537 int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
538 freeregs(fs, r1, r2);
539}
540
541
542/*
543** Add constant 'v' to prototype's list of constants (field 'k').
544*/
545static int addk (FuncState *fs, Proto *f, TValue *v) {
546 lua_State *L = fs->ls->L;
547 int oldsize = f->sizek;
548 int k = fs->nk;
549 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
550 while (oldsize < f->sizek)
551 setnilvalue(&f->k[oldsize++]);
552 setobj(L, &f->k[k], v);
553 fs->nk++;
554 luaC_barrier(L, f, v);
555 return k;
556}
557
558
559/*
560** Use scanner's table to cache position of constants in constant list
561** and try to reuse constants. Because some values should not be used
562** as keys (nil cannot be a key, integer keys can collapse with float
563** keys), the caller must provide a useful 'key' for indexing the cache.
564*/
565static int k2proto (FuncState *fs, TValue *key, TValue *v) {
566 TValue val;
567 Proto *f = fs->f;
568 int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */
569 if (!tagisempty(tag)) { /* is there an index there? */
570 int k = cast_int(ivalue(&val));
571 /* collisions can happen only for float keys */
572 lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
573 return k; /* reuse index */
574 }
575 else { /* constant not found; create a new entry */
576 int k = addk(fs, f, v);
577 /* cache it for reuse; numerical value does not need GC barrier;
578 table is not a metatable, so it does not need to invalidate cache */
579 setivalue(&val, k);
580 luaH_set(fs->ls->L, fs->kcache, key, &val);
581 return k;
582 }
583}
584
585
586/*
587** Add a string to list of constants and return its index.
588*/
589static int stringK (FuncState *fs, TString *s) {
590 TValue o;
591 setsvalue(fs->ls->L, &o, s);
592 return k2proto(fs, &o, &o); /* use string itself as key */
593}
594
595
596/*
597** Add an integer to list of constants and return its index.
598*/
599static int luaK_intK (FuncState *fs, lua_Integer n) {
600 TValue o;
601 setivalue(&o, n);
602 return k2proto(fs, &o, &o); /* use integer itself as key */
603}
604
605/*
606** Add a float to list of constants and return its index. Floats
607** with integral values need a different key, to avoid collision
608** with actual integers. To that end, we add to the number its smaller
609** power-of-two fraction that is still significant in its scale.
610** (For doubles, the fraction would be 2^-52).
611** This method is not bulletproof: different numbers may generate the
612** same key (e.g., very large numbers will overflow to 'inf') and for
613** floats larger than 2^53 the result is still an integer. For those
614** cases, just generate a new entry. At worst, this only wastes an entry
615** with a duplicate.
616*/
617static int luaK_numberK (FuncState *fs, lua_Number r) {
618 TValue o, kv;
619 setfltvalue(&o, r); /* value as a TValue */
620 if (r == 0) { /* handle zero as a special case */
621 setpvalue(&kv, fs); /* use FuncState as index */
622 return k2proto(fs, &kv, &o); /* cannot collide */
623 }
624 else {
625 const int nbm = l_floatatt(MANT_DIG);
626 const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
627 const lua_Number k = r * (1 + q); /* key */
628 lua_Integer ik;
629 setfltvalue(&kv, k); /* key as a TValue */
630 if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */
631 int n = k2proto(fs, &kv, &o); /* use key */
632 if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */
633 return n;
634 }
635 /* else, either key is still an integer or there was a collision;
636 anyway, do not try to reuse constant; instead, create a new one */
637 return addk(fs, fs->f, &o);
638 }
639}
640
641
642/*
643** Add a false to list of constants and return its index.
644*/
645static int boolF (FuncState *fs) {
646 TValue o;
647 setbfvalue(&o);
648 return k2proto(fs, &o, &o); /* use boolean itself as key */
649}
650
651
652/*
653** Add a true to list of constants and return its index.
654*/
655static int boolT (FuncState *fs) {
656 TValue o;
657 setbtvalue(&o);
658 return k2proto(fs, &o, &o); /* use boolean itself as key */
659}
660
661
662/*
663** Add nil to list of constants and return its index.
664*/
665static int nilK (FuncState *fs) {
666 lua_State *L = fs->ls->L;
667 TValue k;
668 /* cannot use nil as key; instead use table itself */
669 sethvalue(L, &k, fs->kcache);
670 return k2proto(fs, &k, &G(L)->nilvalue);
671}
672
673
674/*
675** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
676** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
677** overflows in the hidden addition inside 'int2sC'.
678*/
679static int fitsC (lua_Integer i) {
680 return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
681}
682
683
684/*
685** Check whether 'i' can be stored in an 'sBx' operand.
686*/
687static int fitsBx (lua_Integer i) {
688 return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
689}
690
691
692void luaK_int (FuncState *fs, int reg, lua_Integer i) {
693 if (fitsBx(i))
694 codeAsBx(fs, OP_LOADI, reg, cast_int(i));
695 else
696 luaK_codek(fs, reg, luaK_intK(fs, i));
697}
698
699
700static void luaK_float (FuncState *fs, int reg, lua_Number f) {
701 lua_Integer fi;
702 if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
703 codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
704 else
705 luaK_codek(fs, reg, luaK_numberK(fs, f));
706}
707
708
709/*
710** Get the value of 'var' in a register and generate an opcode to check
711** whether that register is nil. 'k' is the index of the variable name
712** in the list of constants. If its value cannot be encoded in Bx, a 0
713** will use '?' for the name.
714*/
715void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) {
716 luaK_exp2anyreg(fs, var);
717 luaK_fixline(fs, line);
718 k = (k >= MAXARG_Bx) ? 0 : k + 1;
719 luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k);
720 luaK_fixline(fs, line);
721 freeexp(fs, var);
722}
723
724
725/*
726** Convert a constant in 'v' into an expression description 'e'
727*/
728static void const2exp (TValue *v, expdesc *e) {
729 switch (ttypetag(v)) {
730 case LUA_VNUMINT:
731 e->k = VKINT; e->u.ival = ivalue(v);
732 break;
733 case LUA_VNUMFLT:
734 e->k = VKFLT; e->u.nval = fltvalue(v);
735 break;
736 case LUA_VFALSE:
737 e->k = VFALSE;
738 break;
739 case LUA_VTRUE:
740 e->k = VTRUE;
741 break;
742 case LUA_VNIL:
743 e->k = VNIL;
744 break;
745 case LUA_VSHRSTR: case LUA_VLNGSTR:
746 e->k = VKSTR; e->u.strval = tsvalue(v);
747 break;
748 default: lua_assert(0);
749 }
750}
751
752
753/*
754** Fix an expression to return the number of results 'nresults'.
755** 'e' must be a multi-ret expression (function call or vararg).
756*/
757void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
758 Instruction *pc = &getinstruction(fs, e);
759 luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");
760 if (e->k == VCALL) /* expression is an open function call? */
761 SETARG_C(*pc, nresults + 1);
762 else {
763 lua_assert(e->k == VVARARG);
764 SETARG_C(*pc, nresults + 1);
765 SETARG_A(*pc, fs->freereg);
766 luaK_reserveregs(fs, 1);
767 }
768}
769
770
771/*
772** Convert a VKSTR to a VK
773*/
774static int str2K (FuncState *fs, expdesc *e) {
775 lua_assert(e->k == VKSTR);
776 e->u.info = stringK(fs, e->u.strval);
777 e->k = VK;
778 return e->u.info;
779}
780
781
782/*
783** Fix an expression to return one result.
784** If expression is not a multi-ret expression (function call or
785** vararg), it already returns one result, so nothing needs to be done.
786** Function calls become VNONRELOC expressions (as its result comes
787** fixed in the base register of the call), while vararg expressions
788** become VRELOC (as OP_VARARG puts its results where it wants).
789** (Calls are created returning one result, so that does not need
790** to be fixed.)
791*/
792void luaK_setoneret (FuncState *fs, expdesc *e) {
793 if (e->k == VCALL) { /* expression is an open function call? */
794 /* already returns 1 value */
795 lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
796 e->k = VNONRELOC; /* result has fixed position */
797 e->u.info = GETARG_A(getinstruction(fs, e));
798 }
799 else if (e->k == VVARARG) {
800 SETARG_C(getinstruction(fs, e), 2);
801 e->k = VRELOC; /* can relocate its simple result */
802 }
803}
804
805/*
806** Change a vararg parameter into a regular local variable
807*/
808void luaK_vapar2local (FuncState *fs, expdesc *var) {
809 needvatab(fs->f); /* function will need a vararg table */
810 /* now a vararg parameter is equivalent to a regular local variable */
811 var->k = VLOCAL;
812}
813
814
815/*
816** Ensure that expression 'e' is not a variable (nor a <const>).
817** (Expression still may have jump lists.)
818*/
819void luaK_dischargevars (FuncState *fs, expdesc *e) {
820 switch (e->k) {
821 case VCONST: {
822 const2exp(const2val(fs, e), e);
823 break;
824 }
825 case VVARGVAR: {
826 luaK_vapar2local(fs, e); /* turn it into a local variable */
827 } /* FALLTHROUGH */
828 case VLOCAL: { /* already in a register */
829 int temp = e->u.var.ridx;
830 e->u.info = temp; /* (avoid a direct assignment; values overlap) */
831 e->k = VNONRELOC; /* becomes a non-relocatable value */
832 break;
833 }
834 case VUPVAL: { /* move value to some (pending) register */
835 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
836 e->k = VRELOC;
837 break;
838 }
839 case VINDEXUP: {
840 e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
841 e->k = VRELOC;
842 break;
843 }
844 case VINDEXI: {
845 freereg(fs, e->u.ind.t);
846 e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
847 e->k = VRELOC;
848 break;
849 }
850 case VINDEXSTR: {
851 freereg(fs, e->u.ind.t);
852 e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
853 e->k = VRELOC;
854 break;
855 }
856 case VINDEXED: {
857 freeregs(fs, e->u.ind.t, e->u.ind.idx);
858 e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
859 e->k = VRELOC;
860 break;
861 }
862 case VVARGIND: {
863 freeregs(fs, e->u.ind.t, e->u.ind.idx);
864 e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx);
865 e->k = VRELOC;
866 break;
867 }
868 case VVARARG: case VCALL: {
869 luaK_setoneret(fs, e);
870 break;
871 }
872 default: break; /* there is one value available (somewhere) */
873 }
874}
875
876
877/*
878** Ensure expression value is in register 'reg', making 'e' a
879** non-relocatable expression.
880** (Expression still may have jump lists.)
881*/
882static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
883 luaK_dischargevars(fs, e);
884 switch (e->k) {
885 case VNIL: {
886 luaK_nil(fs, reg, 1);
887 break;
888 }
889 case VFALSE: {
890 luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
891 break;
892 }
893 case VTRUE: {
894 luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
895 break;
896 }
897 case VKSTR: {
898 str2K(fs, e);
899 } /* FALLTHROUGH */
900 case VK: {
901 luaK_codek(fs, reg, e->u.info);
902 break;
903 }
904 case VKFLT: {
905 luaK_float(fs, reg, e->u.nval);
906 break;
907 }
908 case VKINT: {
909 luaK_int(fs, reg, e->u.ival);
910 break;
911 }
912 case VRELOC: {
913 Instruction *pc = &getinstruction(fs, e);
914 SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
915 break;
916 }
917 case VNONRELOC: {
918 if (reg != e->u.info)
919 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
920 break;
921 }
922 default: {
923 lua_assert(e->k == VJMP);
924 return; /* nothing to do... */
925 }
926 }
927 e->u.info = reg;
928 e->k = VNONRELOC;
929}
930
931
932/*
933** Ensure expression value is in a register, making 'e' a
934** non-relocatable expression.
935** (Expression still may have jump lists.)
936*/
937static void discharge2anyreg (FuncState *fs, expdesc *e) {
938 if (e->k != VNONRELOC) { /* no fixed register yet? */
939 luaK_reserveregs(fs, 1); /* get a register */
940 discharge2reg(fs, e, fs->freereg-1); /* put value there */
941 }
942}
943
944
945static int code_loadbool (FuncState *fs, int A, OpCode op) {
946 luaK_getlabel(fs); /* those instructions may be jump targets */
947 return luaK_codeABC(fs, op, A, 0, 0);
948}
949
950
951/*
952** check whether list has any jump that do not produce a value
953** or produce an inverted value
954*/
955static int need_value (FuncState *fs, int list) {
956 for (; list != NO_JUMP; list = getjump(fs, list)) {
957 Instruction i = *getjumpcontrol(fs, list);
958 if (GET_OPCODE(i) != OP_TESTSET) return 1;
959 }
960 return 0; /* not found */
961}
962
963
964/*
965** Ensures final expression result (which includes results from its
966** jump lists) is in register 'reg'.
967** If expression has jumps, need to patch these jumps either to
968** its final position or to "load" instructions (for those tests
969** that do not produce values).
970*/
971static void exp2reg (FuncState *fs, expdesc *e, int reg) {
972 discharge2reg(fs, e, reg);
973 if (e->k == VJMP) /* expression itself is a test? */
974 luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
975 if (hasjumps(e)) {
976 int final; /* position after whole expression */
977 int p_f = NO_JUMP; /* position of an eventual LOAD false */
978 int p_t = NO_JUMP; /* position of an eventual LOAD true */
979 if (need_value(fs, e->t) || need_value(fs, e->f)) {
980 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
981 p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */
982 p_t = code_loadbool(fs, reg, OP_LOADTRUE);
983 /* jump around these booleans if 'e' is not a test */
984 luaK_patchtohere(fs, fj);
985 }
986 final = luaK_getlabel(fs);
987 patchlistaux(fs, e->f, final, reg, p_f);
988 patchlistaux(fs, e->t, final, reg, p_t);
989 }
990 e->f = e->t = NO_JUMP;
991 e->u.info = reg;
992 e->k = VNONRELOC;
993}
994
995
996/*
997** Ensures final expression result is in next available register.
998*/
999void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
1000 luaK_dischargevars(fs, e);
1001 freeexp(fs, e);
1002 luaK_reserveregs(fs, 1);
1003 exp2reg(fs, e, fs->freereg - 1);
1004}
1005
1006
1007/*
1008** Ensures final expression result is in some (any) register
1009** and return that register.
1010*/
1011int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
1012 luaK_dischargevars(fs, e);
1013 if (e->k == VNONRELOC) { /* expression already has a register? */
1014 if (!hasjumps(e)) /* no jumps? */
1015 return e->u.info; /* result is already in a register */
1016 if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
1017 exp2reg(fs, e, e->u.info); /* put final result in it */
1018 return e->u.info;
1019 }
1020 /* else expression has jumps and cannot change its register
1021 to hold the jump values, because it is a local variable.
1022 Go through to the default case. */
1023 }
1024 luaK_exp2nextreg(fs, e); /* default: use next available register */
1025 return e->u.info;
1026}
1027
1028
1029/*
1030** Ensures final expression result is either in a register,
1031** in an upvalue, or it is the vararg parameter.
1032*/
1033void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
1034 if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e))
1035 luaK_exp2anyreg(fs, e);
1036}
1037
1038
1039/*
1040** Ensures final expression result is either in a register
1041** or it is a constant.
1042*/
1043void luaK_exp2val (FuncState *fs, expdesc *e) {
1044 if (e->k == VJMP || hasjumps(e))
1045 luaK_exp2anyreg(fs, e);
1046 else
1047 luaK_dischargevars(fs, e);
1048}
1049
1050
1051/*
1052** Try to make 'e' a K expression with an index in the range of R/K
1053** indices. Return true iff succeeded.
1054*/
1055static int luaK_exp2K (FuncState *fs, expdesc *e) {
1056 if (!hasjumps(e)) {
1057 int info;
1058 switch (e->k) { /* move constants to 'k' */
1059 case VTRUE: info = boolT(fs); break;
1060 case VFALSE: info = boolF(fs); break;
1061 case VNIL: info = nilK(fs); break;
1062 case VKINT: info = luaK_intK(fs, e->u.ival); break;
1063 case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
1064 case VKSTR: info = stringK(fs, e->u.strval); break;
1065 case VK: info = e->u.info; break;
1066 default: return 0; /* not a constant */
1067 }
1068 if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
1069 e->k = VK; /* make expression a 'K' expression */
1070 e->u.info = info;
1071 return 1;
1072 }
1073 }
1074 /* else, expression doesn't fit; leave it unchanged */
1075 return 0;
1076}
1077
1078
1079/*
1080** Ensures final expression result is in a valid R/K index
1081** (that is, it is either in a register or in 'k' with an index
1082** in the range of R/K indices).
1083** Returns 1 iff expression is K.
1084*/
1085static int exp2RK (FuncState *fs, expdesc *e) {
1086 if (luaK_exp2K(fs, e))
1087 return 1;
1088 else { /* not a constant in the right range: put it in a register */
1089 luaK_exp2anyreg(fs, e);
1090 return 0;
1091 }
1092}
1093
1094
1095static void codeABRK (FuncState *fs, OpCode o, int A, int B,
1096 expdesc *ec) {
1097 int k = exp2RK(fs, ec);
1098 luaK_codeABCk(fs, o, A, B, ec->u.info, k);
1099}
1100
1101
1102/*
1103** Generate code to store result of expression 'ex' into variable 'var'.
1104*/
1105void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
1106 switch (var->k) {
1107 case VLOCAL: {
1108 freeexp(fs, ex);
1109 exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */
1110 return;
1111 }
1112 case VUPVAL: {
1113 int e = luaK_exp2anyreg(fs, ex);
1114 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
1115 break;
1116 }
1117 case VINDEXUP: {
1118 codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
1119 break;
1120 }
1121 case VINDEXI: {
1122 codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
1123 break;
1124 }
1125 case VINDEXSTR: {
1126 codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
1127 break;
1128 }
1129 case VVARGIND: {
1130 needvatab(fs->f); /* function will need a vararg table */
1131 /* now, assignment is to a regular table */
1132 } /* FALLTHROUGH */
1133 case VINDEXED: {
1134 codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
1135 break;
1136 }
1137 default: lua_assert(0); /* invalid var kind to store */
1138 }
1139 freeexp(fs, ex);
1140}
1141
1142
1143/*
1144** Negate condition 'e' (where 'e' is a comparison).
1145*/
1146static void negatecondition (FuncState *fs, expdesc *e) {
1147 Instruction *pc = getjumpcontrol(fs, e->u.info);
1148 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
1149 GET_OPCODE(*pc) != OP_TEST);
1150 SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
1151}
1152
1153
1154/*
1155** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
1156** is true, code will jump if 'e' is true.) Return jump position.
1157** Optimize when 'e' is 'not' something, inverting the condition
1158** and removing the 'not'.
1159*/
1160static int jumponcond (FuncState *fs, expdesc *e, int cond) {
1161 if (e->k == VRELOC) {
1162 Instruction ie = getinstruction(fs, e);
1163 if (GET_OPCODE(ie) == OP_NOT) {
1164 removelastinstruction(fs); /* remove previous OP_NOT */
1165 return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
1166 }
1167 /* else go through */
1168 }
1169 discharge2anyreg(fs, e);
1170 freeexp(fs, e);
1171 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
1172}
1173
1174
1175/*
1176** Emit code to go through if 'e' is true, jump otherwise.
1177*/
1178void luaK_goiftrue (FuncState *fs, expdesc *e) {
1179 int pc; /* pc of new jump */
1180 luaK_dischargevars(fs, e);
1181 switch (e->k) {
1182 case VJMP: { /* condition? */
1183 negatecondition(fs, e); /* jump when it is false */
1184 pc = e->u.info; /* save jump position */
1185 break;
1186 }
1187 case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1188 pc = NO_JUMP; /* always true; do nothing */
1189 break;
1190 }
1191 default: {
1192 pc = jumponcond(fs, e, 0); /* jump when false */
1193 break;
1194 }
1195 }
1196 luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
1197 luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
1198 e->t = NO_JUMP;
1199}
1200
1201
1202/*
1203** Emit code to go through if 'e' is false, jump otherwise.
1204*/
1205static void luaK_goiffalse (FuncState *fs, expdesc *e) {
1206 int pc; /* pc of new jump */
1207 luaK_dischargevars(fs, e);
1208 switch (e->k) {
1209 case VJMP: {
1210 pc = e->u.info; /* already jump if true */
1211 break;
1212 }
1213 case VNIL: case VFALSE: {
1214 pc = NO_JUMP; /* always false; do nothing */
1215 break;
1216 }
1217 default: {
1218 pc = jumponcond(fs, e, 1); /* jump if true */
1219 break;
1220 }
1221 }
1222 luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
1223 luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
1224 e->f = NO_JUMP;
1225}
1226
1227
1228/*
1229** Code 'not e', doing constant folding.
1230*/
1231static void codenot (FuncState *fs, expdesc *e) {
1232 switch (e->k) {
1233 case VNIL: case VFALSE: {
1234 e->k = VTRUE; /* true == not nil == not false */
1235 break;
1236 }
1237 case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1238 e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
1239 break;
1240 }
1241 case VJMP: {
1242 negatecondition(fs, e);
1243 break;
1244 }
1245 case VRELOC:
1246 case VNONRELOC: {
1247 discharge2anyreg(fs, e);
1248 freeexp(fs, e);
1249 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
1250 e->k = VRELOC;
1251 break;
1252 }
1253 default: lua_assert(0); /* cannot happen */
1254 }
1255 /* interchange true and false lists */
1256 { int temp = e->f; e->f = e->t; e->t = temp; }
1257 removevalues(fs, e->f); /* values are useless when negated */
1258 removevalues(fs, e->t);
1259}
1260
1261
1262/*
1263** Check whether expression 'e' is a short literal string
1264*/
1265static int isKstr (FuncState *fs, expdesc *e) {
1266 return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK &&
1267 ttisshrstring(&fs->f->k[e->u.info]));
1268}
1269
1270/*
1271** Check whether expression 'e' is a literal integer.
1272*/
1273static int isKint (expdesc *e) {
1274 return (e->k == VKINT && !hasjumps(e));
1275}
1276
1277
1278/*
1279** Check whether expression 'e' is a literal integer in
1280** proper range to fit in register C
1281*/
1282static int isCint (expdesc *e) {
1283 return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1284}
1285
1286
1287/*
1288** Check whether expression 'e' is a literal integer in
1289** proper range to fit in register sC
1290*/
1291static int isSCint (expdesc *e) {
1292 return isKint(e) && fitsC(e->u.ival);
1293}
1294
1295
1296/*
1297** Check whether expression 'e' is a literal integer or float in
1298** proper range to fit in a register (sB or sC).
1299*/
1300static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
1301 lua_Integer i;
1302 if (e->k == VKINT)
1303 i = e->u.ival;
1304 else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
1305 *isfloat = 1;
1306 else
1307 return 0; /* not a number */
1308 if (!hasjumps(e) && fitsC(i)) {
1309 *pi = int2sC(cast_int(i));
1310 return 1;
1311 }
1312 else
1313 return 0;
1314}
1315
1316
1317/*
1318** Emit SELF instruction or equivalent: the code will convert
1319** expression 'e' into 'e.key(e,'.
1320*/
1321void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
1322 int ereg, base;
1323 luaK_exp2anyreg(fs, e);
1324 ereg = e->u.info; /* register where 'e' (the receiver) was placed */
1325 freeexp(fs, e);
1326 base = e->u.info = fs->freereg; /* base register for op_self */
1327 e->k = VNONRELOC; /* self expression has a fixed register */
1328 luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */
1329 lua_assert(key->k == VKSTR);
1330 /* is method name a short string in a valid K index? */
1331 if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) {
1332 /* can use 'self' opcode */
1333 luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0);
1334 }
1335 else { /* cannot use 'self' opcode; use move+gettable */
1336 luaK_exp2anyreg(fs, key); /* put method name in a register */
1337 luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */
1338 luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */
1339 }
1340 freeexp(fs, key);
1341}
1342
1343
1344/* auxiliary function to define indexing expressions */
1345static void fillidxk (expdesc *t, int idx, expkind k) {
1346 t->u.ind.idx = cast_byte(idx);
1347 t->k = k;
1348}
1349
1350
1351/*
1352** Create expression 't[k]'. 't' must have its final result already in a
1353** register or upvalue. Upvalues can only be indexed by literal strings.
1354** Keys can be literal strings in the constant table or arbitrary
1355** values in registers.
1356*/
1357void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1358 int keystr = -1;
1359 if (k->k == VKSTR)
1360 keystr = str2K(fs, k);
1361 lua_assert(!hasjumps(t) &&
1362 (t->k == VLOCAL || t->k == VVARGVAR ||
1363 t->k == VNONRELOC || t->k == VUPVAL));
1364 if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
1365 luaK_exp2anyreg(fs, t); /* put it in a register */
1366 if (t->k == VUPVAL) {
1367 lu_byte temp = cast_byte(t->u.info); /* upvalue index */
1368 t->u.ind.t = temp; /* (avoid a direct assignment; values overlap) */
1369 lua_assert(isKstr(fs, k));
1370 fillidxk(t, k->u.info, VINDEXUP); /* literal short string */
1371 }
1372 else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */
1373 int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */
1374 lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */
1375 lua_assert(vreg == fs->f->numparams);
1376 t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap?) */
1377 fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */
1378 }
1379 else {
1380 /* register index of the table */
1381 lu_byte temp = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
1382 t->u.ind.t = temp; /* (avoid a direct assignment; values may overlap?) */
1383 if (isKstr(fs, k))
1384 fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */
1385 else if (isCint(k)) /* int. constant in proper range? */
1386 fillidxk(t, cast_int(k->u.ival), VINDEXI);
1387 else
1388 fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED); /* register */
1389 }
1390 t->u.ind.keystr = keystr; /* string index in 'k' */
1391 t->u.ind.ro = 0; /* by default, not read-only */
1392}
1393
1394
1395/*
1396** Return false if folding can raise an error.
1397** Bitwise operations need operands convertible to integers; division
1398** operations cannot have 0 as divisor.
1399*/
1400static int validop (int op, TValue *v1, TValue *v2) {
1401 switch (op) {
1402 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
1403 case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
1404 lua_Integer i;
1405 return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
1406 luaV_tointegerns(v2, &i, LUA_FLOORN2I));
1407 }
1408 case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
1409 return (nvalue(v2) != 0);
1410 default: return 1; /* everything else is valid */
1411 }
1412}
1413
1414
1415/*
1416** Try to "constant-fold" an operation; return 1 iff successful.
1417** (In this case, 'e1' has the final result.)
1418*/
1419static int constfolding (FuncState *fs, int op, expdesc *e1,
1420 const expdesc *e2) {
1421 TValue v1, v2, res;
1422 if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
1423 return 0; /* non-numeric operands or not safe to fold */
1424 luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
1425 if (ttisinteger(&res)) {
1426 e1->k = VKINT;
1427 e1->u.ival = ivalue(&res);
1428 }
1429 else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
1430 lua_Number n = fltvalue(&res);
1431 if (luai_numisnan(n) || n == 0)
1432 return 0;
1433 e1->k = VKFLT;
1434 e1->u.nval = n;
1435 }
1436 return 1;
1437}
1438
1439
1440/*
1441** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
1442*/
1443l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1444 lua_assert(baser <= opr &&
1445 ((baser == OPR_ADD && opr <= OPR_SHR) ||
1446 (baser == OPR_LT && opr <= OPR_LE)));
1447 return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1448}
1449
1450
1451/*
1452** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
1453*/
1454l_sinline OpCode unopr2op (UnOpr opr) {
1455 return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1456 cast_int(OP_UNM));
1457}
1458
1459
1460/*
1461** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
1462*/
1463l_sinline TMS binopr2TM (BinOpr opr) {
1464 lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1465 return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1466}
1467
1468
1469/*
1470** Emit code for unary expressions that "produce values"
1471** (everything but 'not').
1472** Expression to produce final result will be encoded in 'e'.
1473*/
1474static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1475 int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
1476 freeexp(fs, e);
1477 e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
1478 e->k = VRELOC; /* all those operations are relocatable */
1479 luaK_fixline(fs, line);
1480}
1481
1482
1483/*
1484** Emit code for binary expressions that "produce values"
1485** (everything but logical operators 'and'/'or' and comparison
1486** operators).
1487** Expression to produce final result will be encoded in 'e1'.
1488*/
1489static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1490 OpCode op, int v2, int flip, int line,
1491 OpCode mmop, TMS event) {
1492 int v1 = luaK_exp2anyreg(fs, e1);
1493 int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
1494 freeexps(fs, e1, e2);
1495 e1->u.info = pc;
1496 e1->k = VRELOC; /* all those operations are relocatable */
1497 luaK_fixline(fs, line);
1498 luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip); /* metamethod */
1499 luaK_fixline(fs, line);
1500}
1501
1502
1503/*
1504** Emit code for binary expressions that "produce values" over
1505** two registers.
1506*/
1507static void codebinexpval (FuncState *fs, BinOpr opr,
1508 expdesc *e1, expdesc *e2, int line) {
1509 OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1510 int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
1511 /* 'e1' must be already in a register or it is a constant */
1512 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1513 e1->k == VNONRELOC || e1->k == VRELOC);
1514 lua_assert(OP_ADD <= op && op <= OP_SHR);
1515 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1516}
1517
1518
1519/*
1520** Code binary operators with immediate operands.
1521*/
1522static void codebini (FuncState *fs, OpCode op,
1523 expdesc *e1, expdesc *e2, int flip, int line,
1524 TMS event) {
1525 int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */
1526 lua_assert(e2->k == VKINT);
1527 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
1528}
1529
1530
1531/*
1532** Code binary operators with K operand.
1533*/
1534static void codebinK (FuncState *fs, BinOpr opr,
1535 expdesc *e1, expdesc *e2, int flip, int line) {
1536 TMS event = binopr2TM(opr);
1537 int v2 = e2->u.info; /* K index */
1538 OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1539 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1540}
1541
1542
1543/* Try to code a binary operator negating its second operand.
1544** For the metamethod, 2nd operand must keep its original value.
1545** As a special case, avoid the optimization for (exp - 0), because
1546** (-0.0 - 0) is -0.0, while (-0.0 + 0) is +0.0.
1547*/
1548static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
1549 OpCode op, int line, TMS event) {
1550 if (!isKint(e2))
1551 return 0; /* not an integer constant */
1552 else {
1553 lua_Integer i2 = e2->u.ival;
1554 if (!(fitsC(i2) && fitsC(-i2)))
1555 return 0; /* not in the proper range */
1556 if (event == TM_SUB && i2 == 0)
1557 return 0; /* do not optimize (exp - 0) */
1558 else { /* operating a small integer constant */
1559 int v2 = cast_int(i2);
1560 finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
1561 /* correct metamethod argument */
1562 SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
1563 return 1; /* successfully coded */
1564 }
1565 }
1566}
1567
1568
1569static void swapexps (expdesc *e1, expdesc *e2) {
1570 expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
1571}
1572
1573
1574/*
1575** Code binary operators with no constant operand.
1576*/
1577static void codebinNoK (FuncState *fs, BinOpr opr,
1578 expdesc *e1, expdesc *e2, int flip, int line) {
1579 if (flip)
1580 swapexps(e1, e2); /* back to original order */
1581 codebinexpval(fs, opr, e1, e2, line); /* use standard operators */
1582}
1583
1584
1585/*
1586** Code arithmetic operators ('+', '-', ...). If second operand is a
1587** constant in the proper range, use variant opcodes with K operands.
1588*/
1589static void codearith (FuncState *fs, BinOpr opr,
1590 expdesc *e1, expdesc *e2, int flip, int line) {
1591 if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */
1592 codebinK(fs, opr, e1, e2, flip, line);
1593 else /* 'e2' is neither an immediate nor a K operand */
1594 codebinNoK(fs, opr, e1, e2, flip, line);
1595}
1596
1597
1598/*
1599** Code commutative operators ('+', '*'). If first operand is a
1600** numeric constant, change order of operands to try to use an
1601** immediate or K operator.
1602*/
1603static void codecommutative (FuncState *fs, BinOpr op,
1604 expdesc *e1, expdesc *e2, int line) {
1605 int flip = 0;
1606 if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
1607 swapexps(e1, e2); /* change order */
1608 flip = 1;
1609 }
1610 if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
1611 codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1612 else
1613 codearith(fs, op, e1, e2, flip, line);
1614}
1615
1616
1617/*
1618** Code bitwise operations; they are all commutative, so the function
1619** tries to put an integer constant as the 2nd operand (a K operand).
1620*/
1621static void codebitwise (FuncState *fs, BinOpr opr,
1622 expdesc *e1, expdesc *e2, int line) {
1623 int flip = 0;
1624 if (e1->k == VKINT) {
1625 swapexps(e1, e2); /* 'e2' will be the constant operand */
1626 flip = 1;
1627 }
1628 if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */
1629 codebinK(fs, opr, e1, e2, flip, line);
1630 else /* no constants */
1631 codebinNoK(fs, opr, e1, e2, flip, line);
1632}
1633
1634
1635/*
1636** Emit code for order comparisons. When using an immediate operand,
1637** 'isfloat' tells whether the original value was a float.
1638*/
1639static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1640 int r1, r2;
1641 int im;
1642 int isfloat = 0;
1643 OpCode op;
1644 if (isSCnumber(e2, &im, &isfloat)) {
1645 /* use immediate operand */
1646 r1 = luaK_exp2anyreg(fs, e1);
1647 r2 = im;
1648 op = binopr2op(opr, OPR_LT, OP_LTI);
1649 }
1650 else if (isSCnumber(e1, &im, &isfloat)) {
1651 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1652 r1 = luaK_exp2anyreg(fs, e2);
1653 r2 = im;
1654 op = binopr2op(opr, OPR_LT, OP_GTI);
1655 }
1656 else { /* regular case, compare two registers */
1657 r1 = luaK_exp2anyreg(fs, e1);
1658 r2 = luaK_exp2anyreg(fs, e2);
1659 op = binopr2op(opr, OPR_LT, OP_LT);
1660 }
1661 freeexps(fs, e1, e2);
1662 e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
1663 e1->k = VJMP;
1664}
1665
1666
1667/*
1668** Emit code for equality comparisons ('==', '~=').
1669** 'e1' was already put as RK by 'luaK_infix'.
1670*/
1671static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1672 int r1, r2;
1673 int im;
1674 int isfloat = 0; /* not needed here, but kept for symmetry */
1675 OpCode op;
1676 if (e1->k != VNONRELOC) {
1677 lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
1678 swapexps(e1, e2);
1679 }
1680 r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
1681 if (isSCnumber(e2, &im, &isfloat)) {
1682 op = OP_EQI;
1683 r2 = im; /* immediate operand */
1684 }
1685 else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
1686 op = OP_EQK;
1687 r2 = e2->u.info; /* constant index */
1688 }
1689 else {
1690 op = OP_EQ; /* will compare two registers */
1691 r2 = luaK_exp2anyreg(fs, e2);
1692 }
1693 freeexps(fs, e1, e2);
1694 e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
1695 e1->k = VJMP;
1696}
1697
1698
1699/*
1700** Apply prefix operation 'op' to expression 'e'.
1701*/
1702void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1703 static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1704 luaK_dischargevars(fs, e);
1705 switch (opr) {
1706 case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
1707 if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
1708 break;
1709 /* else */ /* FALLTHROUGH */
1710 case OPR_LEN:
1711 codeunexpval(fs, unopr2op(opr), e, line);
1712 break;
1713 case OPR_NOT: codenot(fs, e); break;
1714 default: lua_assert(0);
1715 }
1716}
1717
1718
1719/*
1720** Process 1st operand 'v' of binary operation 'op' before reading
1721** 2nd operand.
1722*/
1723void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1724 luaK_dischargevars(fs, v);
1725 switch (op) {
1726 case OPR_AND: {
1727 luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
1728 break;
1729 }
1730 case OPR_OR: {
1731 luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
1732 break;
1733 }
1734 case OPR_CONCAT: {
1735 luaK_exp2nextreg(fs, v); /* operand must be on the stack */
1736 break;
1737 }
1738 case OPR_ADD: case OPR_SUB:
1739 case OPR_MUL: case OPR_DIV: case OPR_IDIV:
1740 case OPR_MOD: case OPR_POW:
1741 case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1742 case OPR_SHL: case OPR_SHR: {
1743 if (!tonumeral(v, NULL))
1744 luaK_exp2anyreg(fs, v);
1745 /* else keep numeral, which may be folded or used as an immediate
1746 operand */
1747 break;
1748 }
1749 case OPR_EQ: case OPR_NE: {
1750 if (!tonumeral(v, NULL))
1751 exp2RK(fs, v);
1752 /* else keep numeral, which may be an immediate operand */
1753 break;
1754 }
1755 case OPR_LT: case OPR_LE:
1756 case OPR_GT: case OPR_GE: {
1757 int dummy, dummy2;
1758 if (!isSCnumber(v, &dummy, &dummy2))
1759 luaK_exp2anyreg(fs, v);
1760 /* else keep numeral, which may be an immediate operand */
1761 break;
1762 }
1763 default: lua_assert(0);
1764 }
1765}
1766
1767/*
1768** Create code for '(e1 .. e2)'.
1769** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
1770** because concatenation is right associative), merge both CONCATs.
1771*/
1772static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
1773 Instruction *ie2 = previousinstruction(fs);
1774 if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
1775 int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
1776 lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1777 freeexp(fs, e2);
1778 SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
1779 SETARG_B(*ie2, n + 1); /* will concatenate one more element */
1780 }
1781 else { /* 'e2' is not a concatenation */
1782 luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
1783 freeexp(fs, e2);
1784 luaK_fixline(fs, line);
1785 }
1786}
1787
1788
1789/*
1790** Finalize code for binary operation, after reading 2nd operand.
1791*/
1792void luaK_posfix (FuncState *fs, BinOpr opr,
1793 expdesc *e1, expdesc *e2, int line) {
1794 luaK_dischargevars(fs, e2);
1795 if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
1796 return; /* done by folding */
1797 switch (opr) {
1798 case OPR_AND: {
1799 lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
1800 luaK_concat(fs, &e2->f, e1->f);
1801 *e1 = *e2;
1802 break;
1803 }
1804 case OPR_OR: {
1805 lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
1806 luaK_concat(fs, &e2->t, e1->t);
1807 *e1 = *e2;
1808 break;
1809 }
1810 case OPR_CONCAT: { /* e1 .. e2 */
1811 luaK_exp2nextreg(fs, e2);
1812 codeconcat(fs, e1, e2, line);
1813 break;
1814 }
1815 case OPR_ADD: case OPR_MUL: {
1816 codecommutative(fs, opr, e1, e2, line);
1817 break;
1818 }
1819 case OPR_SUB: {
1820 if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
1821 break; /* coded as (r1 + -I) */
1822 /* ELSE */
1823 } /* FALLTHROUGH */
1824 case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
1825 codearith(fs, opr, e1, e2, 0, line);
1826 break;
1827 }
1828 case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1829 codebitwise(fs, opr, e1, e2, line);
1830 break;
1831 }
1832 case OPR_SHL: {
1833 if (isSCint(e1)) {
1834 swapexps(e1, e2);
1835 codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */
1836 }
1837 else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
1838 /* coded as (r1 >> -I) */;
1839 }
1840 else /* regular case (two registers) */
1841 codebinexpval(fs, opr, e1, e2, line);
1842 break;
1843 }
1844 case OPR_SHR: {
1845 if (isSCint(e2))
1846 codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
1847 else /* regular case (two registers) */
1848 codebinexpval(fs, opr, e1, e2, line);
1849 break;
1850 }
1851 case OPR_EQ: case OPR_NE: {
1852 codeeq(fs, opr, e1, e2);
1853 break;
1854 }
1855 case OPR_GT: case OPR_GE: {
1856 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
1857 swapexps(e1, e2);
1858 opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1859 } /* FALLTHROUGH */
1860 case OPR_LT: case OPR_LE: {
1861 codeorder(fs, opr, e1, e2);
1862 break;
1863 }
1864 default: lua_assert(0);
1865 }
1866}
1867
1868
1869/*
1870** Change line information associated with current position, by removing
1871** previous info and adding it again with new line.
1872*/
1873void luaK_fixline (FuncState *fs, int line) {
1874 removelastlineinfo(fs);
1875 savelineinfo(fs, fs->f, line);
1876}
1877
1878
1879void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
1880 Instruction *inst = &fs->f->code[pc];
1881 int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */
1882 int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */
1883 int k = (extra > 0); /* true iff needs extra argument */
1884 hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
1885 *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
1886 *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1887}
1888
1889
1890/*
1891** Emit a SETLIST instruction.
1892** 'base' is register that keeps table;
1893** 'nelems' is #table plus those to be stored now;
1894** 'tostore' is number of values (in registers 'base + 1',...) to add to
1895** table (or LUA_MULTRET to add up to stack top).
1896*/
1897void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1898 lua_assert(tostore != 0);
1899 if (tostore == LUA_MULTRET)
1900 tostore = 0;
1901 if (nelems <= MAXARG_vC)
1902 luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
1903 else {
1904 int extra = nelems / (MAXARG_vC + 1);
1905 nelems %= (MAXARG_vC + 1);
1906 luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1907 codeextraarg(fs, extra);
1908 }
1909 fs->freereg = cast_byte(base + 1); /* free registers with list values */
1910}
1911
1912
1913/*
1914** return the final target of a jump (skipping jumps to jumps)
1915*/
1916static int finaltarget (Instruction *code, int i) {
1917 int count;
1918 for (count = 0; count < 100; count++) { /* avoid infinite loops */
1919 Instruction pc = code[i];
1920 if (GET_OPCODE(pc) != OP_JMP)
1921 break;
1922 else
1923 i += GETARG_sJ(pc) + 1;
1924 }
1925 return i;
1926}
1927
1928
1929/*
1930** Do a final pass over the code of a function, doing small peephole
1931** optimizations and adjustments.
1932*/
1933#include "lopnames.h"
1934void luaK_finish (FuncState *fs) {
1935 int i;
1936 Proto *p = fs->f;
1937 if (p->flag & PF_VATAB) /* will it use a vararg table? */
1938 p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */
1939 for (i = 0; i < fs->pc; i++) {
1940 Instruction *pc = &p->code[i];
1941 lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
1942 switch (GET_OPCODE(*pc)) {
1943 case OP_RETURN0: case OP_RETURN1: {
1944 if (!(fs->needclose || (p->flag & PF_VAHID)))
1945 break; /* no extra work */
1946 /* else use OP_RETURN to do the extra work */
1947 SET_OPCODE(*pc, OP_RETURN);
1948 } /* FALLTHROUGH */
1949 case OP_RETURN: case OP_TAILCALL: {
1950 if (fs->needclose)
1951 SETARG_k(*pc, 1); /* signal that it needs to close */
1952 if (p->flag & PF_VAHID) /* does it use hidden arguments? */
1953 SETARG_C(*pc, p->numparams + 1); /* signal that */
1954 break;
1955 }
1956 case OP_GETVARG: {
1957 if (p->flag & PF_VATAB) /* function has a vararg table? */
1958 SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */
1959 break;
1960 }
1961 case OP_VARARG: {
1962 if (p->flag & PF_VATAB) /* function has a vararg table? */
1963 SETARG_k(*pc, 1); /* must get vararg there */
1964 break;
1965 }
1966 case OP_JMP: { /* to optimize jumps to jumps */
1967 int target = finaltarget(p->code, i);
1968 fixjump(fs, i, target); /* jump directly to final target */
1969 break;
1970 }
1971 default: break;
1972 }
1973 }
1974}