Lines Matching +full:fs +full:- +full:extra
56 lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */
62 ** prototypes for recursive non-terminal functions
70 luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); in error_expected()
74 static l_noret errorlimit (FuncState *fs, int limit, const char *what) { in errorlimit() argument
75 lua_State *L = fs->ls->L; in errorlimit()
77 int line = fs->f->linedefined; in errorlimit()
83 luaX_syntaxerror(fs->ls, msg); in errorlimit()
87 static void checklimit (FuncState *fs, int v, int l, const char *what) { in checklimit() argument
88 if (v > l) errorlimit(fs, l, what); in checklimit()
96 if (ls->t.token == c) { in testnext()
108 if (ls->t.token != c) in check()
132 if (where == ls->linenumber) /* all in the same line? */ in check_match()
135 luaX_syntaxerror(ls, luaO_pushfstring(ls->L, in check_match()
146 ts = ls->t.seminfo.ts; in str_checkname()
153 e->f = e->t = NO_JUMP; in init_exp()
154 e->k = k; in init_exp()
155 e->u.info = i; in init_exp()
160 e->f = e->t = NO_JUMP; in codestring()
161 e->k = VKSTR; in codestring()
162 e->u.strval = s; in codestring()
175 static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { in registerlocalvar() argument
176 Proto *f = fs->f; in registerlocalvar()
177 int oldsize = f->sizelocvars; in registerlocalvar()
178 luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, in registerlocalvar()
180 while (oldsize < f->sizelocvars) in registerlocalvar()
181 f->locvars[oldsize++].varname = NULL; in registerlocalvar()
182 f->locvars[fs->ndebugvars].varname = varname; in registerlocalvar()
183 f->locvars[fs->ndebugvars].startpc = fs->pc; in registerlocalvar()
184 luaC_objbarrier(ls->L, f, varname); in registerlocalvar()
185 return fs->ndebugvars++; in registerlocalvar()
194 lua_State *L = ls->L; in new_localvar()
195 FuncState *fs = ls->fs; in new_localvar() local
196 Dyndata *dyd = ls->dyd; in new_localvar()
198 checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, in new_localvar()
200 luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, in new_localvar()
201 dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); in new_localvar()
202 var = &dyd->actvar.arr[dyd->actvar.n++]; in new_localvar()
203 var->vd.kind = VDKREG; /* default */ in new_localvar()
204 var->vd.name = name; in new_localvar()
205 return dyd->actvar.n - 1 - fs->firstlocal; in new_localvar()
210 luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1));
219 static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { in getlocalvardesc() argument
220 return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; in getlocalvardesc()
230 static int stacklevel (FuncState *fs, int nvar) { in stacklevel() argument
231 while (nvar-- > 0) { in stacklevel()
232 Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ in stacklevel()
233 if (vd->vd.kind != RDKCTC) /* is in the stack? */ in stacklevel()
234 return vd->vd.sidx + 1; in stacklevel()
241 ** Return the number of variables in the stack for function 'fs'
243 int luaY_nvarstack (FuncState *fs) { in luaY_nvarstack() argument
244 return stacklevel(fs, fs->nactvar); in luaY_nvarstack()
249 ** Get the debug-information entry for current variable 'vidx'.
251 static LocVar *localdebuginfo (FuncState *fs, int vidx) { in localdebuginfo() argument
252 Vardesc *vd = getlocalvardesc(fs, vidx); in localdebuginfo()
253 if (vd->vd.kind == RDKCTC) in localdebuginfo()
256 int idx = vd->vd.pidx; in localdebuginfo()
257 lua_assert(idx < fs->ndebugvars); in localdebuginfo()
258 return &fs->f->locvars[idx]; in localdebuginfo()
266 static void init_var (FuncState *fs, expdesc *e, int vidx) { in init_var() argument
267 e->f = e->t = NO_JUMP; in init_var()
268 e->k = VLOCAL; in init_var()
269 e->u.var.vidx = vidx; in init_var()
270 e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; in init_var()
278 FuncState *fs = ls->fs; in check_readonly() local
280 switch (e->k) { in check_readonly()
282 varname = ls->dyd->actvar.arr[e->u.info].vd.name; in check_readonly()
286 Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); in check_readonly()
287 if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ in check_readonly()
288 varname = vardesc->vd.name; in check_readonly()
292 Upvaldesc *up = &fs->f->upvalues[e->u.info]; in check_readonly()
293 if (up->kind != VDKREG) in check_readonly()
294 varname = up->name; in check_readonly()
298 return; /* other cases cannot be read-only */ in check_readonly()
301 const char *msg = luaO_pushfstring(ls->L, in check_readonly()
312 FuncState *fs = ls->fs; in adjustlocalvars() local
313 int stklevel = luaY_nvarstack(fs); in adjustlocalvars()
316 int vidx = fs->nactvar++; in adjustlocalvars()
317 Vardesc *var = getlocalvardesc(fs, vidx); in adjustlocalvars()
318 var->vd.sidx = stklevel++; in adjustlocalvars()
319 var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); in adjustlocalvars()
328 static void removevars (FuncState *fs, int tolevel) { in removevars() argument
329 fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); in removevars()
330 while (fs->nactvar > tolevel) { in removevars()
331 LocVar *var = localdebuginfo(fs, --fs->nactvar); in removevars()
333 var->endpc = fs->pc; in removevars()
339 ** Search the upvalues of the function 'fs' for one
342 static int searchupvalue (FuncState *fs, TString *name) { in searchupvalue() argument
344 Upvaldesc *up = fs->f->upvalues; in searchupvalue()
345 for (i = 0; i < fs->nups; i++) { in searchupvalue()
348 return -1; /* not found */ in searchupvalue()
352 static Upvaldesc *allocupvalue (FuncState *fs) { in allocupvalue() argument
353 Proto *f = fs->f; in allocupvalue()
354 int oldsize = f->sizeupvalues; in allocupvalue()
355 checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); in allocupvalue()
356 luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, in allocupvalue()
358 while (oldsize < f->sizeupvalues) in allocupvalue()
359 f->upvalues[oldsize++].name = NULL; in allocupvalue()
360 return &f->upvalues[fs->nups++]; in allocupvalue()
364 static int newupvalue (FuncState *fs, TString *name, expdesc *v) { in newupvalue() argument
365 Upvaldesc *up = allocupvalue(fs); in newupvalue()
366 FuncState *prev = fs->prev; in newupvalue()
367 if (v->k == VLOCAL) { in newupvalue()
368 up->instack = 1; in newupvalue()
369 up->idx = v->u.var.sidx; in newupvalue()
370 up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; in newupvalue()
371 lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); in newupvalue()
374 up->instack = 0; in newupvalue()
375 up->idx = cast_byte(v->u.info); in newupvalue()
376 up->kind = prev->f->upvalues[v->u.info].kind; in newupvalue()
377 lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); in newupvalue()
379 up->name = name; in newupvalue()
380 luaC_objbarrier(fs->ls->L, fs->f, name); in newupvalue()
381 return fs->nups - 1; in newupvalue()
387 ** function 'fs'. If found, initialize 'var' with it and return
388 ** its expression kind; otherwise return -1.
390 static int searchvar (FuncState *fs, TString *n, expdesc *var) { in searchvar() argument
392 for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { in searchvar()
393 Vardesc *vd = getlocalvardesc(fs, i); in searchvar()
394 if (eqstr(n, vd->vd.name)) { /* found? */ in searchvar()
395 if (vd->vd.kind == RDKCTC) /* compile-time constant? */ in searchvar()
396 init_exp(var, VCONST, fs->firstlocal + i); in searchvar()
398 init_var(fs, var, i); in searchvar()
399 return var->k; in searchvar()
402 return -1; /* not found */ in searchvar()
410 static void markupval (FuncState *fs, int level) { in markupval() argument
411 BlockCnt *bl = fs->bl; in markupval()
412 while (bl->nactvar > level) in markupval()
413 bl = bl->previous; in markupval()
414 bl->upval = 1; in markupval()
415 fs->needclose = 1; in markupval()
424 static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { in singlevaraux() argument
425 if (fs == NULL) /* no more levels? */ in singlevaraux()
428 int v = searchvar(fs, n, var); /* look up locals at current level */ in singlevaraux()
431 markupval(fs, var->u.var.vidx); /* local will be used as an upval */ in singlevaraux()
434 int idx = searchupvalue(fs, n); /* try existing upvalues */ in singlevaraux()
436 singlevaraux(fs->prev, n, var, 0); /* try upper levels */ in singlevaraux()
437 if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ in singlevaraux()
438 idx = newupvalue(fs, n, var); /* will be a new upvalue */ in singlevaraux()
454 FuncState *fs = ls->fs; in singlevar() local
455 singlevaraux(fs, varname, var, 1); in singlevar()
456 if (var->k == VVOID) { /* global name? */ in singlevar()
458 singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ in singlevar()
459 lua_assert(var->k != VVOID); /* this one must exist */ in singlevar()
461 luaK_indexed(fs, var, &key); /* env[varname] */ in singlevar()
471 FuncState *fs = ls->fs; in adjust_assign() local
472 int needed = nvars - nexps; /* extra values needed */ in adjust_assign()
473 if (hasmultret(e->k)) { /* last expression has multiple returns? */ in adjust_assign()
474 int extra = needed + 1; /* discount last expression itself */ in adjust_assign() local
475 if (extra < 0) in adjust_assign()
476 extra = 0; in adjust_assign()
477 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ in adjust_assign()
480 if (e->k != VVOID) /* at least one expression? */ in adjust_assign()
481 luaK_exp2nextreg(fs, e); /* close last expression */ in adjust_assign()
483 luaK_nil(fs, fs->freereg, needed); /* complete with nils */ in adjust_assign()
486 luaK_reserveregs(fs, needed); /* registers for extra values */ in adjust_assign()
488 fs->freereg += needed; /* remove extra values */ in adjust_assign()
495 #define enterlevel(ls) luaE_enterCcall((ls)->L)
497 #define leavelevel(ls) luaE_exitCcall((ls)->L)
505 const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); in jumpscopeerror()
507 msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); in jumpscopeerror()
519 Labellist *gl = &ls->dyd->gt; /* list of goto's */ in solvegoto()
520 Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ in solvegoto()
521 lua_assert(eqstr(gt->name, label->name)); in solvegoto()
522 if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ in solvegoto()
524 luaK_patchlist(ls->fs, gt->pc, label->pc); in solvegoto()
525 for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ in solvegoto()
526 gl->arr[i] = gl->arr[i + 1]; in solvegoto()
527 gl->n--; in solvegoto()
536 Dyndata *dyd = ls->dyd; in findlabel()
538 for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { in findlabel()
539 Labeldesc *lb = &dyd->label.arr[i]; in findlabel()
540 if (eqstr(lb->name, name)) /* correct label? */ in findlabel()
552 int n = l->n; in newlabelentry()
553 luaM_growvector(ls->L, l->arr, n, l->size, in newlabelentry()
555 l->arr[n].name = name; in newlabelentry()
556 l->arr[n].line = line; in newlabelentry()
557 l->arr[n].nactvar = ls->fs->nactvar; in newlabelentry()
558 l->arr[n].close = 0; in newlabelentry()
559 l->arr[n].pc = pc; in newlabelentry()
560 l->n = n + 1; in newlabelentry()
566 return newlabelentry(ls, &ls->dyd->gt, name, line, pc); in newgotoentry()
576 Labellist *gl = &ls->dyd->gt; in solvegotos()
577 int i = ls->fs->bl->firstgoto; in solvegotos()
579 while (i < gl->n) { in solvegotos()
580 if (eqstr(gl->arr[i].name, lb->name)) { in solvegotos()
581 needsclose |= gl->arr[i].close; in solvegotos()
593 ** 'last' tells whether label is the last non-op statement in its
600 FuncState *fs = ls->fs; in createlabel() local
601 Labellist *ll = &ls->dyd->label; in createlabel()
602 int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); in createlabel()
603 if (last) { /* label is last no-op statement in the block? */ in createlabel()
605 ll->arr[l].nactvar = fs->bl->nactvar; in createlabel()
607 if (solvegotos(ls, &ll->arr[l])) { /* need close? */ in createlabel()
608 luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); in createlabel()
618 static void movegotosout (FuncState *fs, BlockCnt *bl) { in movegotosout() argument
620 Labellist *gl = &fs->ls->dyd->gt; in movegotosout()
622 for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ in movegotosout()
623 Labeldesc *gt = &gl->arr[i]; in movegotosout()
625 if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) in movegotosout()
626 gt->close |= bl->upval; /* jump may need a close */ in movegotosout()
627 gt->nactvar = bl->nactvar; /* update goto level */ in movegotosout()
632 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { in enterblock() argument
633 bl->isloop = isloop; in enterblock()
634 bl->nactvar = fs->nactvar; in enterblock()
635 bl->firstlabel = fs->ls->dyd->label.n; in enterblock()
636 bl->firstgoto = fs->ls->dyd->gt.n; in enterblock()
637 bl->upval = 0; in enterblock()
638 bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); in enterblock()
639 bl->previous = fs->bl; in enterblock()
640 fs->bl = bl; in enterblock()
641 lua_assert(fs->freereg == luaY_nvarstack(fs)); in enterblock()
650 if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { in undefgoto()
652 msg = luaO_pushfstring(ls->L, msg, gt->line); in undefgoto()
656 msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); in undefgoto()
662 static void leaveblock (FuncState *fs) { in leaveblock() argument
663 BlockCnt *bl = fs->bl; in leaveblock()
664 LexState *ls = fs->ls; in leaveblock()
666 int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ in leaveblock()
667 if (bl->isloop) /* fix pending breaks? */ in leaveblock()
668 hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); in leaveblock()
669 if (!hasclose && bl->previous && bl->upval) in leaveblock()
670 luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); in leaveblock()
671 fs->bl = bl->previous; in leaveblock()
672 removevars(fs, bl->nactvar); in leaveblock()
673 lua_assert(bl->nactvar == fs->nactvar); in leaveblock()
674 fs->freereg = stklevel; /* free registers */ in leaveblock()
675 ls->dyd->label.n = bl->firstlabel; /* remove local labels */ in leaveblock()
676 if (bl->previous) /* inner block? */ in leaveblock()
677 movegotosout(fs, bl); /* update pending gotos to outer block */ in leaveblock()
679 if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ in leaveblock()
680 undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ in leaveblock()
690 lua_State *L = ls->L; in addprototype()
691 FuncState *fs = ls->fs; in addprototype() local
692 Proto *f = fs->f; /* prototype of current function */ in addprototype()
693 if (fs->np >= f->sizep) { in addprototype()
694 int oldsize = f->sizep; in addprototype()
695 luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); in addprototype()
696 while (oldsize < f->sizep) in addprototype()
697 f->p[oldsize++] = NULL; in addprototype()
699 f->p[fs->np++] = clp = luaF_newproto(L); in addprototype()
713 FuncState *fs = ls->fs->prev; in codeclosure() local
714 init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); in codeclosure()
715 luaK_exp2nextreg(fs, v); /* fix it at the last register */ in codeclosure()
719 static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { in open_func() argument
720 Proto *f = fs->f; in open_func()
721 fs->prev = ls->fs; /* linked list of funcstates */ in open_func()
722 fs->ls = ls; in open_func()
723 ls->fs = fs; in open_func()
724 fs->pc = 0; in open_func()
725 fs->previousline = f->linedefined; in open_func()
726 fs->iwthabs = 0; in open_func()
727 fs->lasttarget = 0; in open_func()
728 fs->freereg = 0; in open_func()
729 fs->nk = 0; in open_func()
730 fs->nabslineinfo = 0; in open_func()
731 fs->np = 0; in open_func()
732 fs->nups = 0; in open_func()
733 fs->ndebugvars = 0; in open_func()
734 fs->nactvar = 0; in open_func()
735 fs->needclose = 0; in open_func()
736 fs->firstlocal = ls->dyd->actvar.n; in open_func()
737 fs->firstlabel = ls->dyd->label.n; in open_func()
738 fs->bl = NULL; in open_func()
739 f->source = ls->source; in open_func()
740 luaC_objbarrier(ls->L, f, f->source); in open_func()
741 f->maxstacksize = 2; /* registers 0/1 are always valid */ in open_func()
742 enterblock(fs, bl, 0); in open_func()
747 lua_State *L = ls->L; in close_func()
748 FuncState *fs = ls->fs; in close_func() local
749 Proto *f = fs->f; in close_func()
750 luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ in close_func()
751 leaveblock(fs); in close_func()
752 lua_assert(fs->bl == NULL); in close_func()
753 luaK_finish(fs); in close_func()
754 luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); in close_func()
755 luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); in close_func()
756 luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, in close_func()
757 fs->nabslineinfo, AbsLineInfo); in close_func()
758 luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); in close_func()
759 luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); in close_func()
760 luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); in close_func()
761 luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); in close_func()
762 ls->fs = fs->prev; in close_func()
779 switch (ls->t.token) { in block_follow()
790 /* statlist -> { stat [';'] } */ in statlist()
792 if (ls->t.token == TK_RETURN) { in statlist()
802 /* fieldsel -> ['.' | ':'] NAME */ in fieldsel()
803 FuncState *fs = ls->fs; in fieldsel() local
805 luaK_exp2anyregup(fs, v); in fieldsel()
808 luaK_indexed(fs, v, &key); in fieldsel()
813 /* index -> '[' expr ']' */ in yindex()
816 luaK_exp2val(ls->fs, v); in yindex()
838 /* recfield -> (NAME | '['exp']') = exp */ in recfield()
839 FuncState *fs = ls->fs; in recfield() local
840 int reg = ls->fs->freereg; in recfield()
842 if (ls->t.token == TK_NAME) { in recfield()
843 checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); in recfield()
846 else /* ls->t.token == '[' */ in recfield()
848 cc->nh++; in recfield()
850 tab = *cc->t; in recfield()
851 luaK_indexed(fs, &tab, &key); in recfield()
853 luaK_storevar(fs, &tab, &val); in recfield()
854 fs->freereg = reg; /* free registers */ in recfield()
858 static void closelistfield (FuncState *fs, ConsControl *cc) { in closelistfield() argument
859 if (cc->v.k == VVOID) return; /* there is no list item */ in closelistfield()
860 luaK_exp2nextreg(fs, &cc->v); in closelistfield()
861 cc->v.k = VVOID; in closelistfield()
862 if (cc->tostore == LFIELDS_PER_FLUSH) { in closelistfield()
863 luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ in closelistfield()
864 cc->na += cc->tostore; in closelistfield()
865 cc->tostore = 0; /* no more items pending */ in closelistfield()
870 static void lastlistfield (FuncState *fs, ConsControl *cc) { in lastlistfield() argument
871 if (cc->tostore == 0) return; in lastlistfield()
872 if (hasmultret(cc->v.k)) { in lastlistfield()
873 luaK_setmultret(fs, &cc->v); in lastlistfield()
874 luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); in lastlistfield()
875 cc->na--; /* do not count last expression (unknown number of elements) */ in lastlistfield()
878 if (cc->v.k != VVOID) in lastlistfield()
879 luaK_exp2nextreg(fs, &cc->v); in lastlistfield()
880 luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); in lastlistfield()
882 cc->na += cc->tostore; in lastlistfield()
887 /* listfield -> exp */ in listfield()
888 expr(ls, &cc->v); in listfield()
889 cc->tostore++; in listfield()
894 /* field -> listfield | recfield */ in field()
895 switch(ls->t.token) { in field()
916 /* constructor -> '{' [ field { sep field } [sep] ] '}' in constructor()
917 sep -> ',' | ';' */ in constructor()
918 FuncState *fs = ls->fs; in constructor() local
919 int line = ls->linenumber; in constructor()
920 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); in constructor()
922 luaK_code(fs, 0); /* space for extra arg. */ in constructor()
925 init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ in constructor()
926 luaK_reserveregs(fs, 1); in constructor()
931 if (ls->t.token == '}') break; in constructor()
932 closelistfield(fs, &cc); in constructor()
936 lastlistfield(fs, &cc); in constructor()
937 luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); in constructor()
943 static void setvararg (FuncState *fs, int nparams) { in setvararg() argument
944 fs->f->is_vararg = 1; in setvararg()
945 luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); in setvararg()
950 /* parlist -> [ param { ',' param } ] */ in parlist()
951 FuncState *fs = ls->fs; in parlist() local
952 Proto *f = fs->f; in parlist()
955 if (ls->t.token != ')') { /* is 'parlist' not empty? */ in parlist()
957 switch (ls->t.token) { in parlist()
958 case TK_NAME: { /* param -> NAME */ in parlist()
963 case TK_DOTS: { /* param -> '...' */ in parlist()
973 f->numparams = cast_byte(fs->nactvar); in parlist()
975 setvararg(fs, f->numparams); /* declared vararg */ in parlist()
976 luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ in parlist()
981 /* body -> '(' parlist ')' block END */ in body()
985 new_fs.f->linedefined = line; in body()
995 new_fs.f->lastlinedefined = ls->linenumber; in body()
1003 /* explist -> expr { ',' expr } */ in explist()
1007 luaK_exp2nextreg(ls->fs, v); in explist()
1016 FuncState *fs = ls->fs; in funcargs() local
1019 switch (ls->t.token) { in funcargs()
1020 case '(': { /* funcargs -> '(' [ explist ] ')' */ in funcargs()
1022 if (ls->t.token == ')') /* arg list is empty? */ in funcargs()
1027 luaK_setmultret(fs, &args); in funcargs()
1032 case '{': { /* funcargs -> constructor */ in funcargs()
1036 case TK_STRING: { /* funcargs -> STRING */ in funcargs()
1037 codestring(&args, ls->t.seminfo.ts); in funcargs()
1045 lua_assert(f->k == VNONRELOC); in funcargs()
1046 base = f->u.info; /* base register for call */ in funcargs()
1051 luaK_exp2nextreg(fs, &args); /* close last argument */ in funcargs()
1052 nparams = fs->freereg - (base+1); in funcargs()
1054 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); in funcargs()
1055 luaK_fixline(fs, line); in funcargs()
1056 fs->freereg = base+1; /* call remove function and arguments and leaves in funcargs()
1071 /* primaryexp -> NAME | '(' expr ')' */ in primaryexp()
1072 switch (ls->t.token) { in primaryexp()
1074 int line = ls->linenumber; in primaryexp()
1078 luaK_dischargevars(ls->fs, v); in primaryexp()
1093 /* suffixedexp -> in suffixedexp()
1095 FuncState *fs = ls->fs; in suffixedexp() local
1096 int line = ls->linenumber; in suffixedexp()
1099 switch (ls->t.token) { in suffixedexp()
1106 luaK_exp2anyregup(fs, v); in suffixedexp()
1108 luaK_indexed(fs, v, &key); in suffixedexp()
1115 luaK_self(fs, v, &key); in suffixedexp()
1120 luaK_exp2nextreg(fs, v); in suffixedexp()
1131 /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | in simpleexp()
1133 switch (ls->t.token) { in simpleexp()
1136 v->u.nval = ls->t.seminfo.r; in simpleexp()
1141 v->u.ival = ls->t.seminfo.i; in simpleexp()
1145 codestring(v, ls->t.seminfo.ts); in simpleexp()
1161 FuncState *fs = ls->fs; in simpleexp() local
1162 check_condition(ls, fs->f->is_vararg, in simpleexp()
1164 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); in simpleexp()
1173 body(ls, v, 0, ls->linenumber); in simpleexp()
1188 case '-': return OPR_MINUS; in getunopr()
1199 case '-': return OPR_SUB; in getbinopr()
1231 {10, 10}, {10, 10}, /* '+' '-' */
1247 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
1254 uop = getunopr(ls->t.token); in subexpr()
1256 int line = ls->linenumber; in subexpr()
1259 luaK_prefix(ls->fs, uop, v, line); in subexpr()
1263 op = getbinopr(ls->t.token); in subexpr()
1267 int line = ls->linenumber; in subexpr()
1269 luaK_infix(ls->fs, op, v); in subexpr()
1270 /* read sub-expression with higher priority */ in subexpr()
1272 luaK_posfix(ls->fs, op, v, &v2, line); in subexpr()
1296 /* block -> statlist */ in block()
1297 FuncState *fs = ls->fs; in block() local
1299 enterblock(fs, &bl, 0); in block()
1301 leaveblock(fs); in block()
1306 ** structure to chain all variables in the left-hand side of an
1322 FuncState *fs = ls->fs; in check_conflict() local
1323 int extra = fs->freereg; /* eventual position to save local variable */ in check_conflict() local
1325 for (; lh; lh = lh->prev) { /* check all previous assignments */ in check_conflict()
1326 if (vkisindexed(lh->v.k)) { /* assignment to table field? */ in check_conflict()
1327 if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ in check_conflict()
1328 if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { in check_conflict()
1330 lh->v.k = VINDEXSTR; in check_conflict()
1331 lh->v.u.ind.t = extra; /* assignment will use safe copy */ in check_conflict()
1335 if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { in check_conflict()
1337 lh->v.u.ind.t = extra; /* assignment will use safe copy */ in check_conflict()
1340 if (lh->v.k == VINDEXED && v->k == VLOCAL && in check_conflict()
1341 lh->v.u.ind.idx == v->u.var.sidx) { in check_conflict()
1343 lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ in check_conflict()
1349 /* copy upvalue/local value to a temporary (in position 'extra') */ in check_conflict()
1350 if (v->k == VLOCAL) in check_conflict()
1351 luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); in check_conflict()
1353 luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); in check_conflict()
1354 luaK_reserveregs(fs, 1); in check_conflict()
1362 ** assignment -> suffixedexp restassign
1363 ** restassign -> ',' suffixedexp restassign | '=' explist
1367 check_condition(ls, vkisvar(lh->v.k), "syntax error"); in restassign()
1368 check_readonly(ls, &lh->v); in restassign()
1369 if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ in restassign()
1379 else { /* restassign -> '=' explist */ in restassign()
1386 luaK_setoneret(ls->fs, &e); /* close last expression */ in restassign()
1387 luaK_storevar(ls->fs, &lh->v, &e); in restassign()
1391 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ in restassign()
1392 luaK_storevar(ls->fs, &lh->v, &e); in restassign()
1397 /* cond -> exp */ in cond()
1401 luaK_goiftrue(ls->fs, &v); in cond()
1407 FuncState *fs = ls->fs; in gotostat() local
1408 int line = ls->linenumber; in gotostat()
1413 newgotoentry(ls, name, line, luaK_jump(fs)); in gotostat()
1416 int lblevel = stacklevel(fs, lb->nactvar); /* label level */ in gotostat()
1417 if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ in gotostat()
1418 luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); in gotostat()
1420 luaK_patchlist(fs, luaK_jump(fs), lb->pc); in gotostat()
1429 int line = ls->linenumber; in breakstat()
1431 newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); in breakstat()
1442 msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); in checkrepeated()
1449 /* label -> '::' NAME '::' */ in labelstat()
1451 while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) in labelstat()
1452 statement(ls); /* skip other no-op statements */ in labelstat()
1459 /* whilestat -> WHILE cond DO block END */ in whilestat()
1460 FuncState *fs = ls->fs; in whilestat() local
1465 whileinit = luaK_getlabel(fs); in whilestat()
1467 enterblock(fs, &bl, 1); in whilestat()
1470 luaK_jumpto(fs, whileinit); in whilestat()
1472 leaveblock(fs); in whilestat()
1473 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ in whilestat()
1478 /* repeatstat -> REPEAT block UNTIL cond */ in repeatstat()
1480 FuncState *fs = ls->fs; in repeatstat() local
1481 int repeat_init = luaK_getlabel(fs); in repeatstat()
1483 enterblock(fs, &bl1, 1); /* loop block */ in repeatstat()
1484 enterblock(fs, &bl2, 0); /* scope block */ in repeatstat()
1489 leaveblock(fs); /* finish scope */ in repeatstat()
1491 int exit = luaK_jump(fs); /* normal exit must jump over fix */ in repeatstat()
1492 luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ in repeatstat()
1493 luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); in repeatstat()
1494 condexit = luaK_jump(fs); /* repeat after closing upvalues */ in repeatstat()
1495 luaK_patchtohere(fs, exit); /* normal exit comes to here */ in repeatstat()
1497 luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ in repeatstat()
1498 leaveblock(fs); /* finish loop */ in repeatstat()
1510 luaK_exp2nextreg(ls->fs, &e); in exp1()
1520 static void fixforjump (FuncState *fs, int pc, int dest, int back) { in fixforjump() argument
1521 Instruction *jmp = &fs->f->code[pc]; in fixforjump()
1522 int offset = dest - (pc + 1); in fixforjump()
1524 offset = -offset; in fixforjump()
1526 luaX_syntaxerror(fs->ls, "control structure too long"); in fixforjump()
1535 /* forbody -> DO block */ in forbody()
1539 FuncState *fs = ls->fs; in forbody() local
1542 prep = luaK_codeABx(fs, forprep[isgen], base, 0); in forbody()
1543 enterblock(fs, &bl, 0); /* scope for declared variables */ in forbody()
1545 luaK_reserveregs(fs, nvars); in forbody()
1547 leaveblock(fs); /* end of scope for declared variables */ in forbody()
1548 fixforjump(fs, prep, luaK_getlabel(fs), 0); in forbody()
1550 luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); in forbody()
1551 luaK_fixline(fs, line); in forbody()
1553 endfor = luaK_codeABx(fs, forloop[isgen], base, 0); in forbody()
1554 fixforjump(fs, endfor, prep + 1, 1); in forbody()
1555 luaK_fixline(fs, line); in forbody()
1560 /* fornum -> NAME = exp,exp[,exp] forbody */ in fornum()
1561 FuncState *fs = ls->fs; in fornum() local
1562 int base = fs->freereg; in fornum()
1574 luaK_int(fs, fs->freereg, 1); in fornum()
1575 luaK_reserveregs(fs, 1); in fornum()
1583 /* forlist -> NAME {,NAME} IN explist forbody */ in forlist()
1584 FuncState *fs = ls->fs; in forlist() local
1588 int base = fs->freereg; in forlist()
1601 line = ls->linenumber; in forlist()
1604 markupval(fs, fs->nactvar); /* last control var. must be closed */ in forlist()
1605 luaK_checkstack(fs, 3); /* extra space to call generator */ in forlist()
1606 forbody(ls, base, line, nvars - 4, 1); in forlist()
1611 /* forstat -> FOR (fornum | forlist) END */ in forstat()
1612 FuncState *fs = ls->fs; in forstat() local
1615 enterblock(fs, &bl, 1); /* scope for loop and control variables */ in forstat()
1618 switch (ls->t.token) { in forstat()
1624 leaveblock(fs); /* loop scope ('break' jumps to this point) */ in forstat()
1638 *label = luaS_newliteral(ls->L, "break"); in issinglejump()
1641 else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) in issinglejump()
1644 TString *lname = ls->lookahead.seminfo.ts; /* label's id */ in issinglejump()
1648 if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) in issinglejump()
1650 *target = lb->pc; in issinglejump()
1662 /* test_then_block -> [IF | ELSEIF] cond THEN block */ in test_then_block()
1665 FuncState *fs = ls->fs; in test_then_block() local
1673 line = ls->linenumber; in test_then_block()
1675 luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ in test_then_block()
1676 enterblock(fs, &bl, 0); /* must enter block before 'goto' */ in test_then_block()
1680 luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ in test_then_block()
1683 leaveblock(fs); in test_then_block()
1687 jf = luaK_jump(fs); in test_then_block()
1690 luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ in test_then_block()
1691 enterblock(fs, &bl, 0); in test_then_block()
1695 leaveblock(fs); in test_then_block()
1696 if (ls->t.token == TK_ELSE || in test_then_block()
1697 ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ in test_then_block()
1698 luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ in test_then_block()
1699 luaK_patchtohere(fs, jf); in test_then_block()
1704 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ in ifstat()
1705 FuncState *fs = ls->fs; in ifstat() local
1708 while (ls->t.token == TK_ELSEIF) in ifstat()
1713 luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ in ifstat()
1719 FuncState *fs = ls->fs; in localfunc() local
1720 int fvar = fs->nactvar; /* function's variable index */ in localfunc()
1723 body(ls, &b, 0, ls->linenumber); /* function created in next register */ in localfunc()
1725 localdebuginfo(fs, fvar)->startpc = fs->pc; in localfunc()
1730 /* ATTRIB -> ['<' Name '>'] */ in getlocalattribute()
1735 return RDKCONST; /* read-only variable */ in getlocalattribute()
1737 return RDKTOCLOSE; /* to-be-closed variable */ in getlocalattribute()
1740 luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); in getlocalattribute()
1747 if (level != -1) { /* is there a to-be-closed variable? */ in checktoclose()
1748 FuncState *fs = ls->fs; in checktoclose() local
1749 markupval(fs, level + 1); in checktoclose()
1750 fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ in checktoclose()
1751 luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); in checktoclose()
1757 /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ in localstat()
1758 FuncState *fs = ls->fs; in localstat() local
1759 int toclose = -1; /* index of to-be-closed variable (if any) */ in localstat()
1768 getlocalvardesc(fs, vidx)->vd.kind = kind; in localstat()
1769 if (kind == RDKTOCLOSE) { /* to-be-closed? */ in localstat()
1770 if (toclose != -1) /* one already present? */ in localstat()
1771 luaK_semerror(ls, "multiple to-be-closed variables in local list"); in localstat()
1772 toclose = fs->nactvar + nvars; in localstat()
1782 var = getlocalvardesc(fs, vidx); /* get last variable */ in localstat()
1784 var->vd.kind == RDKCONST && /* last variable is const? */ in localstat()
1785 luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ in localstat()
1786 var->vd.kind = RDKCTC; /* variable is a compile-time constant */ in localstat()
1787 adjustlocalvars(ls, nvars - 1); /* exclude last variable */ in localstat()
1788 fs->nactvar++; /* but count it */ in localstat()
1799 /* funcname -> NAME {fieldsel} [':' NAME] */ in funcname()
1802 while (ls->t.token == '.') in funcname()
1804 if (ls->t.token == ':') { in funcname()
1813 /* funcstat -> FUNCTION funcname body */ in funcstat()
1819 luaK_storevar(ls->fs, &v, &b); in funcstat()
1820 luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ in funcstat()
1825 /* stat -> func | assignment */ in exprstat()
1826 FuncState *fs = ls->fs; in exprstat() local
1829 if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ in exprstat()
1833 else { /* stat -> func */ in exprstat()
1836 inst = &getinstruction(fs, &v.v); in exprstat()
1843 /* stat -> RETURN [explist] [';'] */ in retstat()
1844 FuncState *fs = ls->fs; in retstat() local
1847 int first = luaY_nvarstack(fs); /* first slot to be returned */ in retstat()
1848 if (block_follow(ls, 1) || ls->t.token == ';') in retstat()
1853 luaK_setmultret(fs, &e); in retstat()
1854 if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ in retstat()
1855 SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); in retstat()
1856 lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); in retstat()
1862 first = luaK_exp2anyreg(fs, &e); /* can use original slot */ in retstat()
1864 luaK_exp2nextreg(fs, &e); in retstat()
1865 lua_assert(nret == fs->freereg - first); in retstat()
1869 luaK_ret(fs, first, nret); in retstat()
1875 int line = ls->linenumber; /* may be needed for error messages */ in statement()
1877 switch (ls->t.token) { in statement()
1878 case ';': { /* stat -> ';' (empty statement) */ in statement()
1882 case TK_IF: { /* stat -> ifstat */ in statement()
1886 case TK_WHILE: { /* stat -> whilestat */ in statement()
1890 case TK_DO: { /* stat -> DO block END */ in statement()
1896 case TK_FOR: { /* stat -> forstat */ in statement()
1900 case TK_REPEAT: { /* stat -> repeatstat */ in statement()
1904 case TK_FUNCTION: { /* stat -> funcstat */ in statement()
1908 case TK_LOCAL: { /* stat -> localstat */ in statement()
1916 case TK_DBCOLON: { /* stat -> label */ in statement()
1921 case TK_RETURN: { /* stat -> retstat */ in statement()
1926 case TK_BREAK: { /* stat -> breakstat */ in statement()
1930 case TK_GOTO: { /* stat -> 'goto' NAME */ in statement()
1935 default: { /* stat -> func | assignment */ in statement()
1940 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && in statement()
1941 ls->fs->freereg >= luaY_nvarstack(ls->fs)); in statement()
1942 ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ in statement()
1953 static void mainfunc (LexState *ls, FuncState *fs) { in mainfunc() argument
1956 open_func(ls, fs, &bl); in mainfunc()
1957 setvararg(fs, 0); /* main function is always declared vararg */ in mainfunc()
1958 env = allocupvalue(fs); /* ...set environment upvalue */ in mainfunc()
1959 env->instack = 1; in mainfunc()
1960 env->idx = 0; in mainfunc()
1961 env->kind = VDKREG; in mainfunc()
1962 env->name = ls->envn; in mainfunc()
1963 luaC_objbarrier(ls->L, fs->f, env->name); in mainfunc()
1976 setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ in luaY_parser()
1979 sethvalue2s(L, L->top, lexstate.h); /* anchor it */ in luaY_parser()
1981 funcstate.f = cl->p = luaF_newproto(L); in luaY_parser()
1982 luaC_objbarrier(L, cl, cl->p); in luaY_parser()
1983 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ in luaY_parser()
1984 luaC_objbarrier(L, funcstate.f, funcstate.f->source); in luaY_parser()
1987 dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; in luaY_parser()
1988 luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); in luaY_parser()
1990 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); in luaY_parser()
1992 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); in luaY_parser()
1993 L->top--; /* remove scanner's table */ in luaY_parser()