diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lua/lparser.c (renamed from src/lua-5.3/lparser.c) | 1000 |
1 files changed, 673 insertions, 327 deletions
diff --git a/src/lua-5.3/lparser.c b/src/lua/lparser.c index cc54de4..bc7d9a4 100644 --- a/src/lua-5.3/lparser.c +++ b/src/lua/lparser.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $ | 2 | ** $Id: lparser.c $ |
| 3 | ** Lua Parser | 3 | ** Lua Parser |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include "lprefix.h" | 10 | #include "lprefix.h" |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | #include <limits.h> | ||
| 13 | #include <string.h> | 14 | #include <string.h> |
| 14 | 15 | ||
| 15 | #include "lua.h" | 16 | #include "lua.h" |
| @@ -52,6 +53,7 @@ typedef struct BlockCnt { | |||
| 52 | lu_byte nactvar; /* # active locals outside the block */ | 53 | lu_byte nactvar; /* # active locals outside the block */ |
| 53 | lu_byte upval; /* true if some variable in the block is an upvalue */ | 54 | lu_byte upval; /* true if some variable in the block is an upvalue */ |
| 54 | lu_byte isloop; /* true if 'block' is a loop */ | 55 | lu_byte isloop; /* true if 'block' is a loop */ |
| 56 | lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ | ||
| 55 | } BlockCnt; | 57 | } BlockCnt; |
| 56 | 58 | ||
| 57 | 59 | ||
| @@ -63,13 +65,6 @@ static void statement (LexState *ls); | |||
| 63 | static void expr (LexState *ls, expdesc *v); | 65 | static void expr (LexState *ls, expdesc *v); |
| 64 | 66 | ||
| 65 | 67 | ||
| 66 | /* semantic error */ | ||
| 67 | static l_noret semerror (LexState *ls, const char *msg) { | ||
| 68 | ls->t.token = 0; /* remove "near <token>" from final message */ | ||
| 69 | luaX_syntaxerror(ls, msg); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | static l_noret error_expected (LexState *ls, int token) { | 68 | static l_noret error_expected (LexState *ls, int token) { |
| 74 | luaX_syntaxerror(ls, | 69 | luaX_syntaxerror(ls, |
| 75 | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); | 70 | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); |
| @@ -94,6 +89,9 @@ static void checklimit (FuncState *fs, int v, int l, const char *what) { | |||
| 94 | } | 89 | } |
| 95 | 90 | ||
| 96 | 91 | ||
| 92 | /* | ||
| 93 | ** Test whether next token is 'c'; if so, skip it. | ||
| 94 | */ | ||
| 97 | static int testnext (LexState *ls, int c) { | 95 | static int testnext (LexState *ls, int c) { |
| 98 | if (ls->t.token == c) { | 96 | if (ls->t.token == c) { |
| 99 | luaX_next(ls); | 97 | luaX_next(ls); |
| @@ -103,12 +101,18 @@ static int testnext (LexState *ls, int c) { | |||
| 103 | } | 101 | } |
| 104 | 102 | ||
| 105 | 103 | ||
| 104 | /* | ||
| 105 | ** Check that next token is 'c'. | ||
| 106 | */ | ||
| 106 | static void check (LexState *ls, int c) { | 107 | static void check (LexState *ls, int c) { |
| 107 | if (ls->t.token != c) | 108 | if (ls->t.token != c) |
| 108 | error_expected(ls, c); | 109 | error_expected(ls, c); |
| 109 | } | 110 | } |
| 110 | 111 | ||
| 111 | 112 | ||
| 113 | /* | ||
| 114 | ** Check that next token is 'c' and skip it. | ||
| 115 | */ | ||
| 112 | static void checknext (LexState *ls, int c) { | 116 | static void checknext (LexState *ls, int c) { |
| 113 | check(ls, c); | 117 | check(ls, c); |
| 114 | luaX_next(ls); | 118 | luaX_next(ls); |
| @@ -118,11 +122,15 @@ static void checknext (LexState *ls, int c) { | |||
| 118 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } | 122 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } |
| 119 | 123 | ||
| 120 | 124 | ||
| 121 | 125 | /* | |
| 126 | ** Check that next token is 'what' and skip it. In case of error, | ||
| 127 | ** raise an error that the expected 'what' should match a 'who' | ||
| 128 | ** in line 'where' (if that is not the current line). | ||
| 129 | */ | ||
| 122 | static void check_match (LexState *ls, int what, int who, int where) { | 130 | static void check_match (LexState *ls, int what, int who, int where) { |
| 123 | if (!testnext(ls, what)) { | 131 | if (unlikely(!testnext(ls, what))) { |
| 124 | if (where == ls->linenumber) | 132 | if (where == ls->linenumber) /* all in the same line? */ |
| 125 | error_expected(ls, what); | 133 | error_expected(ls, what); /* do not need a complex message */ |
| 126 | else { | 134 | else { |
| 127 | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, | 135 | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, |
| 128 | "%s expected (to close %s at line %d)", | 136 | "%s expected (to close %s at line %d)", |
| @@ -148,73 +156,189 @@ static void init_exp (expdesc *e, expkind k, int i) { | |||
| 148 | } | 156 | } |
| 149 | 157 | ||
| 150 | 158 | ||
| 151 | static void codestring (LexState *ls, expdesc *e, TString *s) { | 159 | static void codestring (expdesc *e, TString *s) { |
| 152 | init_exp(e, VK, luaK_stringK(ls->fs, s)); | 160 | e->f = e->t = NO_JUMP; |
| 161 | e->k = VKSTR; | ||
| 162 | e->u.strval = s; | ||
| 153 | } | 163 | } |
| 154 | 164 | ||
| 155 | 165 | ||
| 156 | static void checkname (LexState *ls, expdesc *e) { | 166 | static void codename (LexState *ls, expdesc *e) { |
| 157 | codestring(ls, e, str_checkname(ls)); | 167 | codestring(e, str_checkname(ls)); |
| 158 | } | 168 | } |
| 159 | 169 | ||
| 160 | 170 | ||
| 161 | static int registerlocalvar (LexState *ls, TString *varname) { | 171 | /* |
| 162 | FuncState *fs = ls->fs; | 172 | ** Register a new local variable in the active 'Proto' (for debug |
| 173 | ** information). | ||
| 174 | */ | ||
| 175 | static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { | ||
| 163 | Proto *f = fs->f; | 176 | Proto *f = fs->f; |
| 164 | int oldsize = f->sizelocvars; | 177 | int oldsize = f->sizelocvars; |
| 165 | luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, | 178 | luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, |
| 166 | LocVar, SHRT_MAX, "local variables"); | 179 | LocVar, SHRT_MAX, "local variables"); |
| 167 | while (oldsize < f->sizelocvars) | 180 | while (oldsize < f->sizelocvars) |
| 168 | f->locvars[oldsize++].varname = NULL; | 181 | f->locvars[oldsize++].varname = NULL; |
| 169 | f->locvars[fs->nlocvars].varname = varname; | 182 | f->locvars[fs->ndebugvars].varname = varname; |
| 183 | f->locvars[fs->ndebugvars].startpc = fs->pc; | ||
| 170 | luaC_objbarrier(ls->L, f, varname); | 184 | luaC_objbarrier(ls->L, f, varname); |
| 171 | return fs->nlocvars++; | 185 | return fs->ndebugvars++; |
| 172 | } | 186 | } |
| 173 | 187 | ||
| 174 | 188 | ||
| 175 | static void new_localvar (LexState *ls, TString *name) { | 189 | /* |
| 190 | ** Create a new local variable with the given 'name'. Return its index | ||
| 191 | ** in the function. | ||
| 192 | */ | ||
| 193 | static int new_localvar (LexState *ls, TString *name) { | ||
| 194 | lua_State *L = ls->L; | ||
| 176 | FuncState *fs = ls->fs; | 195 | FuncState *fs = ls->fs; |
| 177 | Dyndata *dyd = ls->dyd; | 196 | Dyndata *dyd = ls->dyd; |
| 178 | int reg = registerlocalvar(ls, name); | 197 | Vardesc *var; |
| 179 | checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, | 198 | checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, |
| 180 | MAXVARS, "local variables"); | 199 | MAXVARS, "local variables"); |
| 181 | luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, | 200 | luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, |
| 182 | dyd->actvar.size, Vardesc, MAX_INT, "local variables"); | 201 | dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); |
| 183 | dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); | 202 | var = &dyd->actvar.arr[dyd->actvar.n++]; |
| 203 | var->vd.kind = VDKREG; /* default */ | ||
| 204 | var->vd.name = name; | ||
| 205 | return dyd->actvar.n - 1 - fs->firstlocal; | ||
| 184 | } | 206 | } |
| 185 | 207 | ||
| 208 | #define new_localvarliteral(ls,v) \ | ||
| 209 | new_localvar(ls, \ | ||
| 210 | luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); | ||
| 211 | |||
| 186 | 212 | ||
| 187 | static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { | 213 | |
| 188 | new_localvar(ls, luaX_newstring(ls, name, sz)); | 214 | /* |
| 215 | ** Return the "variable description" (Vardesc) of a given variable. | ||
| 216 | ** (Unless noted otherwise, all variables are referred to by their | ||
| 217 | ** compiler indices.) | ||
| 218 | */ | ||
| 219 | static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { | ||
| 220 | return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; | ||
| 189 | } | 221 | } |
| 190 | 222 | ||
| 191 | #define new_localvarliteral(ls,v) \ | 223 | |
| 192 | new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) | 224 | /* |
| 225 | ** Convert 'nvar', a compiler index level, to it corresponding | ||
| 226 | ** stack index level. For that, search for the highest variable | ||
| 227 | ** below that level that is in the stack and uses its stack | ||
| 228 | ** index ('sidx'). | ||
| 229 | */ | ||
| 230 | static int stacklevel (FuncState *fs, int nvar) { | ||
| 231 | while (nvar-- > 0) { | ||
| 232 | Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ | ||
| 233 | if (vd->vd.kind != RDKCTC) /* is in the stack? */ | ||
| 234 | return vd->vd.sidx + 1; | ||
| 235 | } | ||
| 236 | return 0; /* no variables in the stack */ | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | /* | ||
| 241 | ** Return the number of variables in the stack for function 'fs' | ||
| 242 | */ | ||
| 243 | int luaY_nvarstack (FuncState *fs) { | ||
| 244 | return stacklevel(fs, fs->nactvar); | ||
| 245 | } | ||
| 193 | 246 | ||
| 194 | 247 | ||
| 195 | static LocVar *getlocvar (FuncState *fs, int i) { | 248 | /* |
| 196 | int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; | 249 | ** Get the debug-information entry for current variable 'vidx'. |
| 197 | lua_assert(idx < fs->nlocvars); | 250 | */ |
| 198 | return &fs->f->locvars[idx]; | 251 | static LocVar *localdebuginfo (FuncState *fs, int vidx) { |
| 252 | Vardesc *vd = getlocalvardesc(fs, vidx); | ||
| 253 | if (vd->vd.kind == RDKCTC) | ||
| 254 | return NULL; /* no debug info. for constants */ | ||
| 255 | else { | ||
| 256 | int idx = vd->vd.pidx; | ||
| 257 | lua_assert(idx < fs->ndebugvars); | ||
| 258 | return &fs->f->locvars[idx]; | ||
| 259 | } | ||
| 199 | } | 260 | } |
| 200 | 261 | ||
| 201 | 262 | ||
| 263 | /* | ||
| 264 | ** Create an expression representing variable 'vidx' | ||
| 265 | */ | ||
| 266 | static void init_var (FuncState *fs, expdesc *e, int vidx) { | ||
| 267 | e->f = e->t = NO_JUMP; | ||
| 268 | e->k = VLOCAL; | ||
| 269 | e->u.var.vidx = vidx; | ||
| 270 | e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | /* | ||
| 275 | ** Raises an error if variable described by 'e' is read only | ||
| 276 | */ | ||
| 277 | static void check_readonly (LexState *ls, expdesc *e) { | ||
| 278 | FuncState *fs = ls->fs; | ||
| 279 | TString *varname = NULL; /* to be set if variable is const */ | ||
| 280 | switch (e->k) { | ||
| 281 | case VCONST: { | ||
| 282 | varname = ls->dyd->actvar.arr[e->u.info].vd.name; | ||
| 283 | break; | ||
| 284 | } | ||
| 285 | case VLOCAL: { | ||
| 286 | Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); | ||
| 287 | if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ | ||
| 288 | varname = vardesc->vd.name; | ||
| 289 | break; | ||
| 290 | } | ||
| 291 | case VUPVAL: { | ||
| 292 | Upvaldesc *up = &fs->f->upvalues[e->u.info]; | ||
| 293 | if (up->kind != VDKREG) | ||
| 294 | varname = up->name; | ||
| 295 | break; | ||
| 296 | } | ||
| 297 | default: | ||
| 298 | return; /* other cases cannot be read-only */ | ||
| 299 | } | ||
| 300 | if (varname) { | ||
| 301 | const char *msg = luaO_pushfstring(ls->L, | ||
| 302 | "attempt to assign to const variable '%s'", getstr(varname)); | ||
| 303 | luaK_semerror(ls, msg); /* error */ | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | /* | ||
| 309 | ** Start the scope for the last 'nvars' created variables. | ||
| 310 | */ | ||
| 202 | static void adjustlocalvars (LexState *ls, int nvars) { | 311 | static void adjustlocalvars (LexState *ls, int nvars) { |
| 203 | FuncState *fs = ls->fs; | 312 | FuncState *fs = ls->fs; |
| 204 | fs->nactvar = cast_byte(fs->nactvar + nvars); | 313 | int stklevel = luaY_nvarstack(fs); |
| 205 | for (; nvars; nvars--) { | 314 | int i; |
| 206 | getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; | 315 | for (i = 0; i < nvars; i++) { |
| 316 | int vidx = fs->nactvar++; | ||
| 317 | Vardesc *var = getlocalvardesc(fs, vidx); | ||
| 318 | var->vd.sidx = stklevel++; | ||
| 319 | var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); | ||
| 207 | } | 320 | } |
| 208 | } | 321 | } |
| 209 | 322 | ||
| 210 | 323 | ||
| 324 | /* | ||
| 325 | ** Close the scope for all variables up to level 'tolevel'. | ||
| 326 | ** (debug info.) | ||
| 327 | */ | ||
| 211 | static void removevars (FuncState *fs, int tolevel) { | 328 | static void removevars (FuncState *fs, int tolevel) { |
| 212 | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); | 329 | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); |
| 213 | while (fs->nactvar > tolevel) | 330 | while (fs->nactvar > tolevel) { |
| 214 | getlocvar(fs, --fs->nactvar)->endpc = fs->pc; | 331 | LocVar *var = localdebuginfo(fs, --fs->nactvar); |
| 332 | if (var) /* does it have debug information? */ | ||
| 333 | var->endpc = fs->pc; | ||
| 334 | } | ||
| 215 | } | 335 | } |
| 216 | 336 | ||
| 217 | 337 | ||
| 338 | /* | ||
| 339 | ** Search the upvalues of the function 'fs' for one | ||
| 340 | ** with the given 'name'. | ||
| 341 | */ | ||
| 218 | static int searchupvalue (FuncState *fs, TString *name) { | 342 | static int searchupvalue (FuncState *fs, TString *name) { |
| 219 | int i; | 343 | int i; |
| 220 | Upvaldesc *up = fs->f->upvalues; | 344 | Upvaldesc *up = fs->f->upvalues; |
| @@ -225,7 +349,7 @@ static int searchupvalue (FuncState *fs, TString *name) { | |||
| 225 | } | 349 | } |
| 226 | 350 | ||
| 227 | 351 | ||
| 228 | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { | 352 | static Upvaldesc *allocupvalue (FuncState *fs) { |
| 229 | Proto *f = fs->f; | 353 | Proto *f = fs->f; |
| 230 | int oldsize = f->sizeupvalues; | 354 | int oldsize = f->sizeupvalues; |
| 231 | checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); | 355 | checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); |
| @@ -233,58 +357,87 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { | |||
| 233 | Upvaldesc, MAXUPVAL, "upvalues"); | 357 | Upvaldesc, MAXUPVAL, "upvalues"); |
| 234 | while (oldsize < f->sizeupvalues) | 358 | while (oldsize < f->sizeupvalues) |
| 235 | f->upvalues[oldsize++].name = NULL; | 359 | f->upvalues[oldsize++].name = NULL; |
| 236 | f->upvalues[fs->nups].instack = (v->k == VLOCAL); | 360 | return &f->upvalues[fs->nups++]; |
| 237 | f->upvalues[fs->nups].idx = cast_byte(v->u.info); | ||
| 238 | f->upvalues[fs->nups].name = name; | ||
| 239 | luaC_objbarrier(fs->ls->L, f, name); | ||
| 240 | return fs->nups++; | ||
| 241 | } | 361 | } |
| 242 | 362 | ||
| 243 | 363 | ||
| 244 | static int searchvar (FuncState *fs, TString *n) { | 364 | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { |
| 365 | Upvaldesc *up = allocupvalue(fs); | ||
| 366 | FuncState *prev = fs->prev; | ||
| 367 | if (v->k == VLOCAL) { | ||
| 368 | up->instack = 1; | ||
| 369 | up->idx = v->u.var.sidx; | ||
| 370 | up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; | ||
| 371 | lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); | ||
| 372 | } | ||
| 373 | else { | ||
| 374 | up->instack = 0; | ||
| 375 | up->idx = cast_byte(v->u.info); | ||
| 376 | up->kind = prev->f->upvalues[v->u.info].kind; | ||
| 377 | lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); | ||
| 378 | } | ||
| 379 | up->name = name; | ||
| 380 | luaC_objbarrier(fs->ls->L, fs->f, name); | ||
| 381 | return fs->nups - 1; | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | /* | ||
| 386 | ** Look for an active local variable with the name 'n' in the | ||
| 387 | ** function 'fs'. If found, initialize 'var' with it and return | ||
| 388 | ** its expression kind; otherwise return -1. | ||
| 389 | */ | ||
| 390 | static int searchvar (FuncState *fs, TString *n, expdesc *var) { | ||
| 245 | int i; | 391 | int i; |
| 246 | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { | 392 | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { |
| 247 | if (eqstr(n, getlocvar(fs, i)->varname)) | 393 | Vardesc *vd = getlocalvardesc(fs, i); |
| 248 | return i; | 394 | if (eqstr(n, vd->vd.name)) { /* found? */ |
| 395 | if (vd->vd.kind == RDKCTC) /* compile-time constant? */ | ||
| 396 | init_exp(var, VCONST, fs->firstlocal + i); | ||
| 397 | else /* real variable */ | ||
| 398 | init_var(fs, var, i); | ||
| 399 | return var->k; | ||
| 400 | } | ||
| 249 | } | 401 | } |
| 250 | return -1; /* not found */ | 402 | return -1; /* not found */ |
| 251 | } | 403 | } |
| 252 | 404 | ||
| 253 | 405 | ||
| 254 | /* | 406 | /* |
| 255 | Mark block where variable at given level was defined | 407 | ** Mark block where variable at given level was defined |
| 256 | (to emit close instructions later). | 408 | ** (to emit close instructions later). |
| 257 | */ | 409 | */ |
| 258 | static void markupval (FuncState *fs, int level) { | 410 | static void markupval (FuncState *fs, int level) { |
| 259 | BlockCnt *bl = fs->bl; | 411 | BlockCnt *bl = fs->bl; |
| 260 | while (bl->nactvar > level) | 412 | while (bl->nactvar > level) |
| 261 | bl = bl->previous; | 413 | bl = bl->previous; |
| 262 | bl->upval = 1; | 414 | bl->upval = 1; |
| 415 | fs->needclose = 1; | ||
| 263 | } | 416 | } |
| 264 | 417 | ||
| 265 | 418 | ||
| 266 | /* | 419 | /* |
| 267 | Find variable with given name 'n'. If it is an upvalue, add this | 420 | ** Find a variable with the given name 'n'. If it is an upvalue, add |
| 268 | upvalue into all intermediate functions. | 421 | ** this upvalue into all intermediate functions. If it is a global, set |
| 422 | ** 'var' as 'void' as a flag. | ||
| 269 | */ | 423 | */ |
| 270 | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { | 424 | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
| 271 | if (fs == NULL) /* no more levels? */ | 425 | if (fs == NULL) /* no more levels? */ |
| 272 | init_exp(var, VVOID, 0); /* default is global */ | 426 | init_exp(var, VVOID, 0); /* default is global */ |
| 273 | else { | 427 | else { |
| 274 | int v = searchvar(fs, n); /* look up locals at current level */ | 428 | int v = searchvar(fs, n, var); /* look up locals at current level */ |
| 275 | if (v >= 0) { /* found? */ | 429 | if (v >= 0) { /* found? */ |
| 276 | init_exp(var, VLOCAL, v); /* variable is local */ | 430 | if (v == VLOCAL && !base) |
| 277 | if (!base) | 431 | markupval(fs, var->u.var.vidx); /* local will be used as an upval */ |
| 278 | markupval(fs, v); /* local will be used as an upval */ | ||
| 279 | } | 432 | } |
| 280 | else { /* not found as local at current level; try upvalues */ | 433 | else { /* not found as local at current level; try upvalues */ |
| 281 | int idx = searchupvalue(fs, n); /* try existing upvalues */ | 434 | int idx = searchupvalue(fs, n); /* try existing upvalues */ |
| 282 | if (idx < 0) { /* not found? */ | 435 | if (idx < 0) { /* not found? */ |
| 283 | singlevaraux(fs->prev, n, var, 0); /* try upper levels */ | 436 | singlevaraux(fs->prev, n, var, 0); /* try upper levels */ |
| 284 | if (var->k == VVOID) /* not found? */ | 437 | if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ |
| 285 | return; /* it is a global */ | 438 | idx = newupvalue(fs, n, var); /* will be a new upvalue */ |
| 286 | /* else was LOCAL or UPVAL */ | 439 | else /* it is a global or a constant */ |
| 287 | idx = newupvalue(fs, n, var); /* will be a new upvalue */ | 440 | return; /* don't need to do anything at this level */ |
| 288 | } | 441 | } |
| 289 | init_exp(var, VUPVAL, idx); /* new or old upvalue */ | 442 | init_exp(var, VUPVAL, idx); /* new or old upvalue */ |
| 290 | } | 443 | } |
| @@ -292,6 +445,10 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { | |||
| 292 | } | 445 | } |
| 293 | 446 | ||
| 294 | 447 | ||
| 448 | /* | ||
| 449 | ** Find a variable with the given name 'n', handling global variables | ||
| 450 | ** too. | ||
| 451 | */ | ||
| 295 | static void singlevar (LexState *ls, expdesc *var) { | 452 | static void singlevar (LexState *ls, expdesc *var) { |
| 296 | TString *varname = str_checkname(ls); | 453 | TString *varname = str_checkname(ls); |
| 297 | FuncState *fs = ls->fs; | 454 | FuncState *fs = ls->fs; |
| @@ -300,88 +457,96 @@ static void singlevar (LexState *ls, expdesc *var) { | |||
| 300 | expdesc key; | 457 | expdesc key; |
| 301 | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ | 458 | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ |
| 302 | lua_assert(var->k != VVOID); /* this one must exist */ | 459 | lua_assert(var->k != VVOID); /* this one must exist */ |
| 303 | codestring(ls, &key, varname); /* key is variable name */ | 460 | codestring(&key, varname); /* key is variable name */ |
| 304 | luaK_indexed(fs, var, &key); /* env[varname] */ | 461 | luaK_indexed(fs, var, &key); /* env[varname] */ |
| 305 | } | 462 | } |
| 306 | } | 463 | } |
| 307 | 464 | ||
| 308 | 465 | ||
| 466 | /* | ||
| 467 | ** Adjust the number of results from an expression list 'e' with 'nexps' | ||
| 468 | ** expressions to 'nvars' values. | ||
| 469 | */ | ||
| 309 | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { | 470 | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { |
| 310 | FuncState *fs = ls->fs; | 471 | FuncState *fs = ls->fs; |
| 311 | int extra = nvars - nexps; | 472 | int needed = nvars - nexps; /* extra values needed */ |
| 312 | if (hasmultret(e->k)) { | 473 | if (hasmultret(e->k)) { /* last expression has multiple returns? */ |
| 313 | extra++; /* includes call itself */ | 474 | int extra = needed + 1; /* discount last expression itself */ |
| 314 | if (extra < 0) extra = 0; | 475 | if (extra < 0) |
| 476 | extra = 0; | ||
| 315 | luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ | 477 | luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ |
| 316 | if (extra > 1) luaK_reserveregs(fs, extra-1); | ||
| 317 | } | 478 | } |
| 318 | else { | 479 | else { |
| 319 | if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ | 480 | if (e->k != VVOID) /* at least one expression? */ |
| 320 | if (extra > 0) { | 481 | luaK_exp2nextreg(fs, e); /* close last expression */ |
| 321 | int reg = fs->freereg; | 482 | if (needed > 0) /* missing values? */ |
| 322 | luaK_reserveregs(fs, extra); | 483 | luaK_nil(fs, fs->freereg, needed); /* complete with nils */ |
| 323 | luaK_nil(fs, reg, extra); | ||
| 324 | } | ||
| 325 | } | 484 | } |
| 326 | if (nexps > nvars) | 485 | if (needed > 0) |
| 327 | ls->fs->freereg -= nexps - nvars; /* remove extra values */ | 486 | luaK_reserveregs(fs, needed); /* registers for extra values */ |
| 487 | else /* adding 'needed' is actually a subtraction */ | ||
| 488 | fs->freereg += needed; /* remove extra values */ | ||
| 328 | } | 489 | } |
| 329 | 490 | ||
| 330 | 491 | ||
| 331 | static void enterlevel (LexState *ls) { | 492 | /* |
| 332 | lua_State *L = ls->L; | 493 | ** Macros to limit the maximum recursion depth while parsing |
| 333 | ++L->nCcalls; | 494 | */ |
| 334 | checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); | 495 | #define enterlevel(ls) luaE_enterCcall((ls)->L) |
| 335 | } | 496 | |
| 497 | #define leavelevel(ls) luaE_exitCcall((ls)->L) | ||
| 336 | 498 | ||
| 337 | 499 | ||
| 338 | #define leavelevel(ls) ((ls)->L->nCcalls--) | 500 | /* |
| 501 | ** Generates an error that a goto jumps into the scope of some | ||
| 502 | ** local variable. | ||
| 503 | */ | ||
| 504 | static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { | ||
| 505 | const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); | ||
| 506 | const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'"; | ||
| 507 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); | ||
| 508 | luaK_semerror(ls, msg); /* raise the error */ | ||
| 509 | } | ||
| 339 | 510 | ||
| 340 | 511 | ||
| 341 | static void closegoto (LexState *ls, int g, Labeldesc *label) { | 512 | /* |
| 513 | ** Solves the goto at index 'g' to given 'label' and removes it | ||
| 514 | ** from the list of pending goto's. | ||
| 515 | ** If it jumps into the scope of some variable, raises an error. | ||
| 516 | */ | ||
| 517 | static void solvegoto (LexState *ls, int g, Labeldesc *label) { | ||
| 342 | int i; | 518 | int i; |
| 343 | FuncState *fs = ls->fs; | 519 | Labellist *gl = &ls->dyd->gt; /* list of goto's */ |
| 344 | Labellist *gl = &ls->dyd->gt; | 520 | Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ |
| 345 | Labeldesc *gt = &gl->arr[g]; | ||
| 346 | lua_assert(eqstr(gt->name, label->name)); | 521 | lua_assert(eqstr(gt->name, label->name)); |
| 347 | if (gt->nactvar < label->nactvar) { | 522 | if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ |
| 348 | TString *vname = getlocvar(fs, gt->nactvar)->varname; | 523 | jumpscopeerror(ls, gt); |
| 349 | const char *msg = luaO_pushfstring(ls->L, | 524 | luaK_patchlist(ls->fs, gt->pc, label->pc); |
| 350 | "<goto %s> at line %d jumps into the scope of local '%s'", | 525 | for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ |
| 351 | getstr(gt->name), gt->line, getstr(vname)); | ||
| 352 | semerror(ls, msg); | ||
| 353 | } | ||
| 354 | luaK_patchlist(fs, gt->pc, label->pc); | ||
| 355 | /* remove goto from pending list */ | ||
| 356 | for (i = g; i < gl->n - 1; i++) | ||
| 357 | gl->arr[i] = gl->arr[i + 1]; | 526 | gl->arr[i] = gl->arr[i + 1]; |
| 358 | gl->n--; | 527 | gl->n--; |
| 359 | } | 528 | } |
| 360 | 529 | ||
| 361 | 530 | ||
| 362 | /* | 531 | /* |
| 363 | ** try to close a goto with existing labels; this solves backward jumps | 532 | ** Search for an active label with the given name. |
| 364 | */ | 533 | */ |
| 365 | static int findlabel (LexState *ls, int g) { | 534 | static Labeldesc *findlabel (LexState *ls, TString *name) { |
| 366 | int i; | 535 | int i; |
| 367 | BlockCnt *bl = ls->fs->bl; | ||
| 368 | Dyndata *dyd = ls->dyd; | 536 | Dyndata *dyd = ls->dyd; |
| 369 | Labeldesc *gt = &dyd->gt.arr[g]; | 537 | /* check labels in current function for a match */ |
| 370 | /* check labels in current block for a match */ | 538 | for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { |
| 371 | for (i = bl->firstlabel; i < dyd->label.n; i++) { | ||
| 372 | Labeldesc *lb = &dyd->label.arr[i]; | 539 | Labeldesc *lb = &dyd->label.arr[i]; |
| 373 | if (eqstr(lb->name, gt->name)) { /* correct label? */ | 540 | if (eqstr(lb->name, name)) /* correct label? */ |
| 374 | if (gt->nactvar > lb->nactvar && | 541 | return lb; |
| 375 | (bl->upval || dyd->label.n > bl->firstlabel)) | ||
| 376 | luaK_patchclose(ls->fs, gt->pc, lb->nactvar); | ||
| 377 | closegoto(ls, g, lb); /* close it */ | ||
| 378 | return 1; | ||
| 379 | } | ||
| 380 | } | 542 | } |
| 381 | return 0; /* label not found; cannot close goto */ | 543 | return NULL; /* label not found */ |
| 382 | } | 544 | } |
| 383 | 545 | ||
| 384 | 546 | ||
| 547 | /* | ||
| 548 | ** Adds a new label/goto in the corresponding list. | ||
| 549 | */ | ||
| 385 | static int newlabelentry (LexState *ls, Labellist *l, TString *name, | 550 | static int newlabelentry (LexState *ls, Labellist *l, TString *name, |
| 386 | int line, int pc) { | 551 | int line, int pc) { |
| 387 | int n = l->n; | 552 | int n = l->n; |
| @@ -390,48 +555,76 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, | |||
| 390 | l->arr[n].name = name; | 555 | l->arr[n].name = name; |
| 391 | l->arr[n].line = line; | 556 | l->arr[n].line = line; |
| 392 | l->arr[n].nactvar = ls->fs->nactvar; | 557 | l->arr[n].nactvar = ls->fs->nactvar; |
| 558 | l->arr[n].close = 0; | ||
| 393 | l->arr[n].pc = pc; | 559 | l->arr[n].pc = pc; |
| 394 | l->n = n + 1; | 560 | l->n = n + 1; |
| 395 | return n; | 561 | return n; |
| 396 | } | 562 | } |
| 397 | 563 | ||
| 398 | 564 | ||
| 565 | static int newgotoentry (LexState *ls, TString *name, int line, int pc) { | ||
| 566 | return newlabelentry(ls, &ls->dyd->gt, name, line, pc); | ||
| 567 | } | ||
| 568 | |||
| 569 | |||
| 399 | /* | 570 | /* |
| 400 | ** check whether new label 'lb' matches any pending gotos in current | 571 | ** Solves forward jumps. Check whether new label 'lb' matches any |
| 401 | ** block; solves forward jumps | 572 | ** pending gotos in current block and solves them. Return true |
| 573 | ** if any of the goto's need to close upvalues. | ||
| 402 | */ | 574 | */ |
| 403 | static void findgotos (LexState *ls, Labeldesc *lb) { | 575 | static int solvegotos (LexState *ls, Labeldesc *lb) { |
| 404 | Labellist *gl = &ls->dyd->gt; | 576 | Labellist *gl = &ls->dyd->gt; |
| 405 | int i = ls->fs->bl->firstgoto; | 577 | int i = ls->fs->bl->firstgoto; |
| 578 | int needsclose = 0; | ||
| 406 | while (i < gl->n) { | 579 | while (i < gl->n) { |
| 407 | if (eqstr(gl->arr[i].name, lb->name)) | 580 | if (eqstr(gl->arr[i].name, lb->name)) { |
| 408 | closegoto(ls, i, lb); | 581 | needsclose |= gl->arr[i].close; |
| 582 | solvegoto(ls, i, lb); /* will remove 'i' from the list */ | ||
| 583 | } | ||
| 409 | else | 584 | else |
| 410 | i++; | 585 | i++; |
| 411 | } | 586 | } |
| 587 | return needsclose; | ||
| 412 | } | 588 | } |
| 413 | 589 | ||
| 414 | 590 | ||
| 415 | /* | 591 | /* |
| 416 | ** export pending gotos to outer level, to check them against | 592 | ** Create a new label with the given 'name' at the given 'line'. |
| 417 | ** outer labels; if the block being exited has upvalues, and | 593 | ** 'last' tells whether label is the last non-op statement in its |
| 418 | ** the goto exits the scope of any variable (which can be the | 594 | ** block. Solves all pending goto's to this new label and adds |
| 419 | ** upvalue), close those variables being exited. | 595 | ** a close instruction if necessary. |
| 596 | ** Returns true iff it added a close instruction. | ||
| 597 | */ | ||
| 598 | static int createlabel (LexState *ls, TString *name, int line, | ||
| 599 | int last) { | ||
| 600 | FuncState *fs = ls->fs; | ||
| 601 | Labellist *ll = &ls->dyd->label; | ||
| 602 | int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); | ||
| 603 | if (last) { /* label is last no-op statement in the block? */ | ||
| 604 | /* assume that locals are already out of scope */ | ||
| 605 | ll->arr[l].nactvar = fs->bl->nactvar; | ||
| 606 | } | ||
| 607 | if (solvegotos(ls, &ll->arr[l])) { /* need close? */ | ||
| 608 | luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); | ||
| 609 | return 1; | ||
| 610 | } | ||
| 611 | return 0; | ||
| 612 | } | ||
| 613 | |||
| 614 | |||
| 615 | /* | ||
| 616 | ** Adjust pending gotos to outer level of a block. | ||
| 420 | */ | 617 | */ |
| 421 | static void movegotosout (FuncState *fs, BlockCnt *bl) { | 618 | static void movegotosout (FuncState *fs, BlockCnt *bl) { |
| 422 | int i = bl->firstgoto; | 619 | int i; |
| 423 | Labellist *gl = &fs->ls->dyd->gt; | 620 | Labellist *gl = &fs->ls->dyd->gt; |
| 424 | /* correct pending gotos to current block and try to close it | 621 | /* correct pending gotos to current block */ |
| 425 | with visible labels */ | 622 | for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ |
| 426 | while (i < gl->n) { | ||
| 427 | Labeldesc *gt = &gl->arr[i]; | 623 | Labeldesc *gt = &gl->arr[i]; |
| 428 | if (gt->nactvar > bl->nactvar) { | 624 | /* leaving a variable scope? */ |
| 429 | if (bl->upval) | 625 | if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) |
| 430 | luaK_patchclose(fs, gt->pc, bl->nactvar); | 626 | gt->close |= bl->upval; /* jump may need a close */ |
| 431 | gt->nactvar = bl->nactvar; | 627 | gt->nactvar = bl->nactvar; /* update goto level */ |
| 432 | } | ||
| 433 | if (!findlabel(fs->ls, i)) | ||
| 434 | i++; /* move to next one */ | ||
| 435 | } | 628 | } |
| 436 | } | 629 | } |
| 437 | 630 | ||
| @@ -442,54 +635,50 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { | |||
| 442 | bl->firstlabel = fs->ls->dyd->label.n; | 635 | bl->firstlabel = fs->ls->dyd->label.n; |
| 443 | bl->firstgoto = fs->ls->dyd->gt.n; | 636 | bl->firstgoto = fs->ls->dyd->gt.n; |
| 444 | bl->upval = 0; | 637 | bl->upval = 0; |
| 638 | bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); | ||
| 445 | bl->previous = fs->bl; | 639 | bl->previous = fs->bl; |
| 446 | fs->bl = bl; | 640 | fs->bl = bl; |
| 447 | lua_assert(fs->freereg == fs->nactvar); | 641 | lua_assert(fs->freereg == luaY_nvarstack(fs)); |
| 448 | } | 642 | } |
| 449 | 643 | ||
| 450 | 644 | ||
| 451 | /* | 645 | /* |
| 452 | ** create a label named 'break' to resolve break statements | 646 | ** generates an error for an undefined 'goto'. |
| 453 | */ | ||
| 454 | static void breaklabel (LexState *ls) { | ||
| 455 | TString *n = luaS_new(ls->L, "break"); | ||
| 456 | int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); | ||
| 457 | findgotos(ls, &ls->dyd->label.arr[l]); | ||
| 458 | } | ||
| 459 | |||
| 460 | /* | ||
| 461 | ** generates an error for an undefined 'goto'; choose appropriate | ||
| 462 | ** message when label name is a reserved word (which can only be 'break') | ||
| 463 | */ | 647 | */ |
| 464 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { | 648 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { |
| 465 | const char *msg = isreserved(gt->name) | 649 | const char *msg; |
| 466 | ? "<%s> at line %d not inside a loop" | 650 | if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { |
| 467 | : "no visible label '%s' for <goto> at line %d"; | 651 | msg = "break outside loop at line %d"; |
| 468 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); | 652 | msg = luaO_pushfstring(ls->L, msg, gt->line); |
| 469 | semerror(ls, msg); | 653 | } |
| 654 | else { | ||
| 655 | msg = "no visible label '%s' for <goto> at line %d"; | ||
| 656 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); | ||
| 657 | } | ||
| 658 | luaK_semerror(ls, msg); | ||
| 470 | } | 659 | } |
| 471 | 660 | ||
| 472 | 661 | ||
| 473 | static void leaveblock (FuncState *fs) { | 662 | static void leaveblock (FuncState *fs) { |
| 474 | BlockCnt *bl = fs->bl; | 663 | BlockCnt *bl = fs->bl; |
| 475 | LexState *ls = fs->ls; | 664 | LexState *ls = fs->ls; |
| 476 | if (bl->previous && bl->upval) { | 665 | int hasclose = 0; |
| 477 | /* create a 'jump to here' to close upvalues */ | 666 | int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ |
| 478 | int j = luaK_jump(fs); | 667 | if (bl->isloop) /* fix pending breaks? */ |
| 479 | luaK_patchclose(fs, j, bl->nactvar); | 668 | hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); |
| 480 | luaK_patchtohere(fs, j); | 669 | if (!hasclose && bl->previous && bl->upval) |
| 481 | } | 670 | luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); |
| 482 | if (bl->isloop) | ||
| 483 | breaklabel(ls); /* close pending breaks */ | ||
| 484 | fs->bl = bl->previous; | 671 | fs->bl = bl->previous; |
| 485 | removevars(fs, bl->nactvar); | 672 | removevars(fs, bl->nactvar); |
| 486 | lua_assert(bl->nactvar == fs->nactvar); | 673 | lua_assert(bl->nactvar == fs->nactvar); |
| 487 | fs->freereg = fs->nactvar; /* free registers */ | 674 | fs->freereg = stklevel; /* free registers */ |
| 488 | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ | 675 | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ |
| 489 | if (bl->previous) /* inner block? */ | 676 | if (bl->previous) /* inner block? */ |
| 490 | movegotosout(fs, bl); /* update pending gotos to outer block */ | 677 | movegotosout(fs, bl); /* update pending gotos to outer block */ |
| 491 | else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ | 678 | else { |
| 492 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ | 679 | if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ |
| 680 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ | ||
| 681 | } | ||
| 493 | } | 682 | } |
| 494 | 683 | ||
| 495 | 684 | ||
| @@ -515,35 +704,40 @@ static Proto *addprototype (LexState *ls) { | |||
| 515 | 704 | ||
| 516 | /* | 705 | /* |
| 517 | ** codes instruction to create new closure in parent function. | 706 | ** codes instruction to create new closure in parent function. |
| 518 | ** The OP_CLOSURE instruction must use the last available register, | 707 | ** The OP_CLOSURE instruction uses the last available register, |
| 519 | ** so that, if it invokes the GC, the GC knows which registers | 708 | ** so that, if it invokes the GC, the GC knows which registers |
| 520 | ** are in use at that time. | 709 | ** are in use at that time. |
| 710 | |||
| 521 | */ | 711 | */ |
| 522 | static void codeclosure (LexState *ls, expdesc *v) { | 712 | static void codeclosure (LexState *ls, expdesc *v) { |
| 523 | FuncState *fs = ls->fs->prev; | 713 | FuncState *fs = ls->fs->prev; |
| 524 | init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); | 714 | init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); |
| 525 | luaK_exp2nextreg(fs, v); /* fix it at the last register */ | 715 | luaK_exp2nextreg(fs, v); /* fix it at the last register */ |
| 526 | } | 716 | } |
| 527 | 717 | ||
| 528 | 718 | ||
| 529 | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { | 719 | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { |
| 530 | Proto *f; | 720 | Proto *f = fs->f; |
| 531 | fs->prev = ls->fs; /* linked list of funcstates */ | 721 | fs->prev = ls->fs; /* linked list of funcstates */ |
| 532 | fs->ls = ls; | 722 | fs->ls = ls; |
| 533 | ls->fs = fs; | 723 | ls->fs = fs; |
| 534 | fs->pc = 0; | 724 | fs->pc = 0; |
| 725 | fs->previousline = f->linedefined; | ||
| 726 | fs->iwthabs = 0; | ||
| 535 | fs->lasttarget = 0; | 727 | fs->lasttarget = 0; |
| 536 | fs->jpc = NO_JUMP; | ||
| 537 | fs->freereg = 0; | 728 | fs->freereg = 0; |
| 538 | fs->nk = 0; | 729 | fs->nk = 0; |
| 730 | fs->nabslineinfo = 0; | ||
| 539 | fs->np = 0; | 731 | fs->np = 0; |
| 540 | fs->nups = 0; | 732 | fs->nups = 0; |
| 541 | fs->nlocvars = 0; | 733 | fs->ndebugvars = 0; |
| 542 | fs->nactvar = 0; | 734 | fs->nactvar = 0; |
| 735 | fs->needclose = 0; | ||
| 543 | fs->firstlocal = ls->dyd->actvar.n; | 736 | fs->firstlocal = ls->dyd->actvar.n; |
| 737 | fs->firstlabel = ls->dyd->label.n; | ||
| 544 | fs->bl = NULL; | 738 | fs->bl = NULL; |
| 545 | f = fs->f; | ||
| 546 | f->source = ls->source; | 739 | f->source = ls->source; |
| 740 | luaC_objbarrier(ls->L, f, f->source); | ||
| 547 | f->maxstacksize = 2; /* registers 0/1 are always valid */ | 741 | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
| 548 | enterblock(fs, bl, 0); | 742 | enterblock(fs, bl, 0); |
| 549 | } | 743 | } |
| @@ -553,21 +747,18 @@ static void close_func (LexState *ls) { | |||
| 553 | lua_State *L = ls->L; | 747 | lua_State *L = ls->L; |
| 554 | FuncState *fs = ls->fs; | 748 | FuncState *fs = ls->fs; |
| 555 | Proto *f = fs->f; | 749 | Proto *f = fs->f; |
| 556 | luaK_ret(fs, 0, 0); /* final return */ | 750 | luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ |
| 557 | leaveblock(fs); | 751 | leaveblock(fs); |
| 558 | luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); | ||
| 559 | f->sizecode = fs->pc; | ||
| 560 | luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); | ||
| 561 | f->sizelineinfo = fs->pc; | ||
| 562 | luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); | ||
| 563 | f->sizek = fs->nk; | ||
| 564 | luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); | ||
| 565 | f->sizep = fs->np; | ||
| 566 | luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); | ||
| 567 | f->sizelocvars = fs->nlocvars; | ||
| 568 | luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); | ||
| 569 | f->sizeupvalues = fs->nups; | ||
| 570 | lua_assert(fs->bl == NULL); | 752 | lua_assert(fs->bl == NULL); |
| 753 | luaK_finish(fs); | ||
| 754 | luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); | ||
| 755 | luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); | ||
| 756 | luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, | ||
| 757 | fs->nabslineinfo, AbsLineInfo); | ||
| 758 | luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); | ||
| 759 | luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); | ||
| 760 | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); | ||
| 761 | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); | ||
| 571 | ls->fs = fs->prev; | 762 | ls->fs = fs->prev; |
| 572 | luaC_checkGC(L); | 763 | luaC_checkGC(L); |
| 573 | } | 764 | } |
| @@ -613,7 +804,7 @@ static void fieldsel (LexState *ls, expdesc *v) { | |||
| 613 | expdesc key; | 804 | expdesc key; |
| 614 | luaK_exp2anyregup(fs, v); | 805 | luaK_exp2anyregup(fs, v); |
| 615 | luaX_next(ls); /* skip the dot or colon */ | 806 | luaX_next(ls); /* skip the dot or colon */ |
| 616 | checkname(ls, &key); | 807 | codename(ls, &key); |
| 617 | luaK_indexed(fs, v, &key); | 808 | luaK_indexed(fs, v, &key); |
| 618 | } | 809 | } |
| 619 | 810 | ||
| @@ -634,48 +825,49 @@ static void yindex (LexState *ls, expdesc *v) { | |||
| 634 | */ | 825 | */ |
| 635 | 826 | ||
| 636 | 827 | ||
| 637 | struct ConsControl { | 828 | typedef struct ConsControl { |
| 638 | expdesc v; /* last list item read */ | 829 | expdesc v; /* last list item read */ |
| 639 | expdesc *t; /* table descriptor */ | 830 | expdesc *t; /* table descriptor */ |
| 640 | int nh; /* total number of 'record' elements */ | 831 | int nh; /* total number of 'record' elements */ |
| 641 | int na; /* total number of array elements */ | 832 | int na; /* number of array elements already stored */ |
| 642 | int tostore; /* number of array elements pending to be stored */ | 833 | int tostore; /* number of array elements pending to be stored */ |
| 643 | }; | 834 | } ConsControl; |
| 644 | 835 | ||
| 645 | 836 | ||
| 646 | static void recfield (LexState *ls, struct ConsControl *cc) { | 837 | static void recfield (LexState *ls, ConsControl *cc) { |
| 647 | /* recfield -> (NAME | '['exp1']') = exp1 */ | 838 | /* recfield -> (NAME | '['exp']') = exp */ |
| 648 | FuncState *fs = ls->fs; | 839 | FuncState *fs = ls->fs; |
| 649 | int reg = ls->fs->freereg; | 840 | int reg = ls->fs->freereg; |
| 650 | expdesc key, val; | 841 | expdesc tab, key, val; |
| 651 | int rkkey; | ||
| 652 | if (ls->t.token == TK_NAME) { | 842 | if (ls->t.token == TK_NAME) { |
| 653 | checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); | 843 | checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); |
| 654 | checkname(ls, &key); | 844 | codename(ls, &key); |
| 655 | } | 845 | } |
| 656 | else /* ls->t.token == '[' */ | 846 | else /* ls->t.token == '[' */ |
| 657 | yindex(ls, &key); | 847 | yindex(ls, &key); |
| 658 | cc->nh++; | 848 | cc->nh++; |
| 659 | checknext(ls, '='); | 849 | checknext(ls, '='); |
| 660 | rkkey = luaK_exp2RK(fs, &key); | 850 | tab = *cc->t; |
| 851 | luaK_indexed(fs, &tab, &key); | ||
| 661 | expr(ls, &val); | 852 | expr(ls, &val); |
| 662 | luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); | 853 | luaK_storevar(fs, &tab, &val); |
| 663 | fs->freereg = reg; /* free registers */ | 854 | fs->freereg = reg; /* free registers */ |
| 664 | } | 855 | } |
| 665 | 856 | ||
| 666 | 857 | ||
| 667 | static void closelistfield (FuncState *fs, struct ConsControl *cc) { | 858 | static void closelistfield (FuncState *fs, ConsControl *cc) { |
| 668 | if (cc->v.k == VVOID) return; /* there is no list item */ | 859 | if (cc->v.k == VVOID) return; /* there is no list item */ |
| 669 | luaK_exp2nextreg(fs, &cc->v); | 860 | luaK_exp2nextreg(fs, &cc->v); |
| 670 | cc->v.k = VVOID; | 861 | cc->v.k = VVOID; |
| 671 | if (cc->tostore == LFIELDS_PER_FLUSH) { | 862 | if (cc->tostore == LFIELDS_PER_FLUSH) { |
| 672 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ | 863 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ |
| 864 | cc->na += cc->tostore; | ||
| 673 | cc->tostore = 0; /* no more items pending */ | 865 | cc->tostore = 0; /* no more items pending */ |
| 674 | } | 866 | } |
| 675 | } | 867 | } |
| 676 | 868 | ||
| 677 | 869 | ||
| 678 | static void lastlistfield (FuncState *fs, struct ConsControl *cc) { | 870 | static void lastlistfield (FuncState *fs, ConsControl *cc) { |
| 679 | if (cc->tostore == 0) return; | 871 | if (cc->tostore == 0) return; |
| 680 | if (hasmultret(cc->v.k)) { | 872 | if (hasmultret(cc->v.k)) { |
| 681 | luaK_setmultret(fs, &cc->v); | 873 | luaK_setmultret(fs, &cc->v); |
| @@ -687,19 +879,18 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { | |||
| 687 | luaK_exp2nextreg(fs, &cc->v); | 879 | luaK_exp2nextreg(fs, &cc->v); |
| 688 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); | 880 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); |
| 689 | } | 881 | } |
| 882 | cc->na += cc->tostore; | ||
| 690 | } | 883 | } |
| 691 | 884 | ||
| 692 | 885 | ||
| 693 | static void listfield (LexState *ls, struct ConsControl *cc) { | 886 | static void listfield (LexState *ls, ConsControl *cc) { |
| 694 | /* listfield -> exp */ | 887 | /* listfield -> exp */ |
| 695 | expr(ls, &cc->v); | 888 | expr(ls, &cc->v); |
| 696 | checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); | ||
| 697 | cc->na++; | ||
| 698 | cc->tostore++; | 889 | cc->tostore++; |
| 699 | } | 890 | } |
| 700 | 891 | ||
| 701 | 892 | ||
| 702 | static void field (LexState *ls, struct ConsControl *cc) { | 893 | static void field (LexState *ls, ConsControl *cc) { |
| 703 | /* field -> listfield | recfield */ | 894 | /* field -> listfield | recfield */ |
| 704 | switch(ls->t.token) { | 895 | switch(ls->t.token) { |
| 705 | case TK_NAME: { /* may be 'listfield' or 'recfield' */ | 896 | case TK_NAME: { /* may be 'listfield' or 'recfield' */ |
| @@ -727,12 +918,13 @@ static void constructor (LexState *ls, expdesc *t) { | |||
| 727 | FuncState *fs = ls->fs; | 918 | FuncState *fs = ls->fs; |
| 728 | int line = ls->linenumber; | 919 | int line = ls->linenumber; |
| 729 | int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); | 920 | int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); |
| 730 | struct ConsControl cc; | 921 | ConsControl cc; |
| 922 | luaK_code(fs, 0); /* space for extra arg. */ | ||
| 731 | cc.na = cc.nh = cc.tostore = 0; | 923 | cc.na = cc.nh = cc.tostore = 0; |
| 732 | cc.t = t; | 924 | cc.t = t; |
| 733 | init_exp(t, VRELOCABLE, pc); | 925 | init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ |
| 926 | luaK_reserveregs(fs, 1); | ||
| 734 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ | 927 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
| 735 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ | ||
| 736 | checknext(ls, '{'); | 928 | checknext(ls, '{'); |
| 737 | do { | 929 | do { |
| 738 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); | 930 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
| @@ -742,20 +934,24 @@ static void constructor (LexState *ls, expdesc *t) { | |||
| 742 | } while (testnext(ls, ',') || testnext(ls, ';')); | 934 | } while (testnext(ls, ',') || testnext(ls, ';')); |
| 743 | check_match(ls, '}', '{', line); | 935 | check_match(ls, '}', '{', line); |
| 744 | lastlistfield(fs, &cc); | 936 | lastlistfield(fs, &cc); |
| 745 | SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ | 937 | luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); |
| 746 | SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ | ||
| 747 | } | 938 | } |
| 748 | 939 | ||
| 749 | /* }====================================================================== */ | 940 | /* }====================================================================== */ |
| 750 | 941 | ||
| 751 | 942 | ||
| 943 | static void setvararg (FuncState *fs, int nparams) { | ||
| 944 | fs->f->is_vararg = 1; | ||
| 945 | luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); | ||
| 946 | } | ||
| 947 | |||
| 752 | 948 | ||
| 753 | static void parlist (LexState *ls) { | 949 | static void parlist (LexState *ls) { |
| 754 | /* parlist -> [ param { ',' param } ] */ | 950 | /* parlist -> [ param { ',' param } ] */ |
| 755 | FuncState *fs = ls->fs; | 951 | FuncState *fs = ls->fs; |
| 756 | Proto *f = fs->f; | 952 | Proto *f = fs->f; |
| 757 | int nparams = 0; | 953 | int nparams = 0; |
| 758 | f->is_vararg = 0; | 954 | int isvararg = 0; |
| 759 | if (ls->t.token != ')') { /* is 'parlist' not empty? */ | 955 | if (ls->t.token != ')') { /* is 'parlist' not empty? */ |
| 760 | do { | 956 | do { |
| 761 | switch (ls->t.token) { | 957 | switch (ls->t.token) { |
| @@ -766,16 +962,18 @@ static void parlist (LexState *ls) { | |||
| 766 | } | 962 | } |
| 767 | case TK_DOTS: { /* param -> '...' */ | 963 | case TK_DOTS: { /* param -> '...' */ |
| 768 | luaX_next(ls); | 964 | luaX_next(ls); |
| 769 | f->is_vararg = 1; /* declared vararg */ | 965 | isvararg = 1; |
| 770 | break; | 966 | break; |
| 771 | } | 967 | } |
| 772 | default: luaX_syntaxerror(ls, "<name> or '...' expected"); | 968 | default: luaX_syntaxerror(ls, "<name> or '...' expected"); |
| 773 | } | 969 | } |
| 774 | } while (!f->is_vararg && testnext(ls, ',')); | 970 | } while (!isvararg && testnext(ls, ',')); |
| 775 | } | 971 | } |
| 776 | adjustlocalvars(ls, nparams); | 972 | adjustlocalvars(ls, nparams); |
| 777 | f->numparams = cast_byte(fs->nactvar); | 973 | f->numparams = cast_byte(fs->nactvar); |
| 778 | luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ | 974 | if (isvararg) |
| 975 | setvararg(fs, f->numparams); /* declared vararg */ | ||
| 976 | luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ | ||
| 779 | } | 977 | } |
| 780 | 978 | ||
| 781 | 979 | ||
| @@ -825,7 +1023,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { | |||
| 825 | args.k = VVOID; | 1023 | args.k = VVOID; |
| 826 | else { | 1024 | else { |
| 827 | explist(ls, &args); | 1025 | explist(ls, &args); |
| 828 | luaK_setmultret(fs, &args); | 1026 | if (hasmultret(args.k)) |
| 1027 | luaK_setmultret(fs, &args); | ||
| 829 | } | 1028 | } |
| 830 | check_match(ls, ')', '(', line); | 1029 | check_match(ls, ')', '(', line); |
| 831 | break; | 1030 | break; |
| @@ -835,7 +1034,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { | |||
| 835 | break; | 1034 | break; |
| 836 | } | 1035 | } |
| 837 | case TK_STRING: { /* funcargs -> STRING */ | 1036 | case TK_STRING: { /* funcargs -> STRING */ |
| 838 | codestring(ls, &args, ls->t.seminfo.ts); | 1037 | codestring(&args, ls->t.seminfo.ts); |
| 839 | luaX_next(ls); /* must use 'seminfo' before 'next' */ | 1038 | luaX_next(ls); /* must use 'seminfo' before 'next' */ |
| 840 | break; | 1039 | break; |
| 841 | } | 1040 | } |
| @@ -902,7 +1101,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { | |||
| 902 | fieldsel(ls, v); | 1101 | fieldsel(ls, v); |
| 903 | break; | 1102 | break; |
| 904 | } | 1103 | } |
| 905 | case '[': { /* '[' exp1 ']' */ | 1104 | case '[': { /* '[' exp ']' */ |
| 906 | expdesc key; | 1105 | expdesc key; |
| 907 | luaK_exp2anyregup(fs, v); | 1106 | luaK_exp2anyregup(fs, v); |
| 908 | yindex(ls, &key); | 1107 | yindex(ls, &key); |
| @@ -912,7 +1111,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { | |||
| 912 | case ':': { /* ':' NAME funcargs */ | 1111 | case ':': { /* ':' NAME funcargs */ |
| 913 | expdesc key; | 1112 | expdesc key; |
| 914 | luaX_next(ls); | 1113 | luaX_next(ls); |
| 915 | checkname(ls, &key); | 1114 | codename(ls, &key); |
| 916 | luaK_self(fs, v, &key); | 1115 | luaK_self(fs, v, &key); |
| 917 | funcargs(ls, v, line); | 1116 | funcargs(ls, v, line); |
| 918 | break; | 1117 | break; |
| @@ -943,7 +1142,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
| 943 | break; | 1142 | break; |
| 944 | } | 1143 | } |
| 945 | case TK_STRING: { | 1144 | case TK_STRING: { |
| 946 | codestring(ls, v, ls->t.seminfo.ts); | 1145 | codestring(v, ls->t.seminfo.ts); |
| 947 | break; | 1146 | break; |
| 948 | } | 1147 | } |
| 949 | case TK_NIL: { | 1148 | case TK_NIL: { |
| @@ -962,7 +1161,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
| 962 | FuncState *fs = ls->fs; | 1161 | FuncState *fs = ls->fs; |
| 963 | check_condition(ls, fs->f->is_vararg, | 1162 | check_condition(ls, fs->f->is_vararg, |
| 964 | "cannot use '...' outside a vararg function"); | 1163 | "cannot use '...' outside a vararg function"); |
| 965 | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); | 1164 | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); |
| 966 | break; | 1165 | break; |
| 967 | } | 1166 | } |
| 968 | case '{': { /* constructor */ | 1167 | case '{': { /* constructor */ |
| @@ -1022,6 +1221,9 @@ static BinOpr getbinopr (int op) { | |||
| 1022 | } | 1221 | } |
| 1023 | 1222 | ||
| 1024 | 1223 | ||
| 1224 | /* | ||
| 1225 | ** Priority table for binary operators. | ||
| 1226 | */ | ||
| 1025 | static const struct { | 1227 | static const struct { |
| 1026 | lu_byte left; /* left priority for each binary operator */ | 1228 | lu_byte left; /* left priority for each binary operator */ |
| 1027 | lu_byte right; /* right priority */ | 1229 | lu_byte right; /* right priority */ |
| @@ -1050,9 +1252,9 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { | |||
| 1050 | UnOpr uop; | 1252 | UnOpr uop; |
| 1051 | enterlevel(ls); | 1253 | enterlevel(ls); |
| 1052 | uop = getunopr(ls->t.token); | 1254 | uop = getunopr(ls->t.token); |
| 1053 | if (uop != OPR_NOUNOPR) { | 1255 | if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ |
| 1054 | int line = ls->linenumber; | 1256 | int line = ls->linenumber; |
| 1055 | luaX_next(ls); | 1257 | luaX_next(ls); /* skip operator */ |
| 1056 | subexpr(ls, v, UNARY_PRIORITY); | 1258 | subexpr(ls, v, UNARY_PRIORITY); |
| 1057 | luaK_prefix(ls->fs, uop, v, line); | 1259 | luaK_prefix(ls->fs, uop, v, line); |
| 1058 | } | 1260 | } |
| @@ -1063,7 +1265,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { | |||
| 1063 | expdesc v2; | 1265 | expdesc v2; |
| 1064 | BinOpr nextop; | 1266 | BinOpr nextop; |
| 1065 | int line = ls->linenumber; | 1267 | int line = ls->linenumber; |
| 1066 | luaX_next(ls); | 1268 | luaX_next(ls); /* skip operator */ |
| 1067 | luaK_infix(ls->fs, op, v); | 1269 | luaK_infix(ls->fs, op, v); |
| 1068 | /* read sub-expression with higher priority */ | 1270 | /* read sub-expression with higher priority */ |
| 1069 | nextop = subexpr(ls, &v2, priority[op].right); | 1271 | nextop = subexpr(ls, &v2, priority[op].right); |
| @@ -1121,43 +1323,60 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { | |||
| 1121 | int extra = fs->freereg; /* eventual position to save local variable */ | 1323 | int extra = fs->freereg; /* eventual position to save local variable */ |
| 1122 | int conflict = 0; | 1324 | int conflict = 0; |
| 1123 | for (; lh; lh = lh->prev) { /* check all previous assignments */ | 1325 | for (; lh; lh = lh->prev) { /* check all previous assignments */ |
| 1124 | if (lh->v.k == VINDEXED) { /* assigning to a table? */ | 1326 | if (vkisindexed(lh->v.k)) { /* assignment to table field? */ |
| 1125 | /* table is the upvalue/local being assigned now? */ | 1327 | if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ |
| 1126 | if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) { | 1328 | if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { |
| 1127 | conflict = 1; | 1329 | conflict = 1; /* table is the upvalue being assigned now */ |
| 1128 | lh->v.u.ind.vt = VLOCAL; | 1330 | lh->v.k = VINDEXSTR; |
| 1129 | lh->v.u.ind.t = extra; /* previous assignment will use safe copy */ | 1331 | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
| 1332 | } | ||
| 1130 | } | 1333 | } |
| 1131 | /* index is the local being assigned? (index cannot be upvalue) */ | 1334 | else { /* table is a register */ |
| 1132 | if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) { | 1335 | if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { |
| 1133 | conflict = 1; | 1336 | conflict = 1; /* table is the local being assigned now */ |
| 1134 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ | 1337 | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
| 1338 | } | ||
| 1339 | /* is index the local being assigned? */ | ||
| 1340 | if (lh->v.k == VINDEXED && v->k == VLOCAL && | ||
| 1341 | lh->v.u.ind.idx == v->u.var.sidx) { | ||
| 1342 | conflict = 1; | ||
| 1343 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ | ||
| 1344 | } | ||
| 1135 | } | 1345 | } |
| 1136 | } | 1346 | } |
| 1137 | } | 1347 | } |
| 1138 | if (conflict) { | 1348 | if (conflict) { |
| 1139 | /* copy upvalue/local value to a temporary (in position 'extra') */ | 1349 | /* copy upvalue/local value to a temporary (in position 'extra') */ |
| 1140 | OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; | 1350 | if (v->k == VLOCAL) |
| 1141 | luaK_codeABC(fs, op, extra, v->u.info, 0); | 1351 | luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); |
| 1352 | else | ||
| 1353 | luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); | ||
| 1142 | luaK_reserveregs(fs, 1); | 1354 | luaK_reserveregs(fs, 1); |
| 1143 | } | 1355 | } |
| 1144 | } | 1356 | } |
| 1145 | 1357 | ||
| 1146 | 1358 | /* | |
| 1147 | static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | 1359 | ** Parse and compile a multiple assignment. The first "variable" |
| 1360 | ** (a 'suffixedexp') was already read by the caller. | ||
| 1361 | ** | ||
| 1362 | ** assignment -> suffixedexp restassign | ||
| 1363 | ** restassign -> ',' suffixedexp restassign | '=' explist | ||
| 1364 | */ | ||
| 1365 | static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { | ||
| 1148 | expdesc e; | 1366 | expdesc e; |
| 1149 | check_condition(ls, vkisvar(lh->v.k), "syntax error"); | 1367 | check_condition(ls, vkisvar(lh->v.k), "syntax error"); |
| 1150 | if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ | 1368 | check_readonly(ls, &lh->v); |
| 1369 | if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ | ||
| 1151 | struct LHS_assign nv; | 1370 | struct LHS_assign nv; |
| 1152 | nv.prev = lh; | 1371 | nv.prev = lh; |
| 1153 | suffixedexp(ls, &nv.v); | 1372 | suffixedexp(ls, &nv.v); |
| 1154 | if (nv.v.k != VINDEXED) | 1373 | if (!vkisindexed(nv.v.k)) |
| 1155 | check_conflict(ls, lh, &nv.v); | 1374 | check_conflict(ls, lh, &nv.v); |
| 1156 | checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, | 1375 | enterlevel(ls); /* control recursion depth */ |
| 1157 | "C levels"); | 1376 | restassign(ls, &nv, nvars+1); |
| 1158 | assignment(ls, &nv, nvars+1); | 1377 | leavelevel(ls); |
| 1159 | } | 1378 | } |
| 1160 | else { /* assignment -> '=' explist */ | 1379 | else { /* restassign -> '=' explist */ |
| 1161 | int nexps; | 1380 | int nexps; |
| 1162 | checknext(ls, '='); | 1381 | checknext(ls, '='); |
| 1163 | nexps = explist(ls, &e); | 1382 | nexps = explist(ls, &e); |
| @@ -1184,57 +1403,55 @@ static int cond (LexState *ls) { | |||
| 1184 | } | 1403 | } |
| 1185 | 1404 | ||
| 1186 | 1405 | ||
| 1187 | static void gotostat (LexState *ls, int pc) { | 1406 | static void gotostat (LexState *ls) { |
| 1407 | FuncState *fs = ls->fs; | ||
| 1188 | int line = ls->linenumber; | 1408 | int line = ls->linenumber; |
| 1189 | TString *label; | 1409 | TString *name = str_checkname(ls); /* label's name */ |
| 1190 | int g; | 1410 | Labeldesc *lb = findlabel(ls, name); |
| 1191 | if (testnext(ls, TK_GOTO)) | 1411 | if (lb == NULL) /* no label? */ |
| 1192 | label = str_checkname(ls); | 1412 | /* forward jump; will be resolved when the label is declared */ |
| 1193 | else { | 1413 | newgotoentry(ls, name, line, luaK_jump(fs)); |
| 1194 | luaX_next(ls); /* skip break */ | 1414 | else { /* found a label */ |
| 1195 | label = luaS_new(ls->L, "break"); | 1415 | /* backward jump; will be resolved here */ |
| 1416 | int lblevel = stacklevel(fs, lb->nactvar); /* label level */ | ||
| 1417 | if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ | ||
| 1418 | luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); | ||
| 1419 | /* create jump and link it to the label */ | ||
| 1420 | luaK_patchlist(fs, luaK_jump(fs), lb->pc); | ||
| 1196 | } | 1421 | } |
| 1197 | g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); | ||
| 1198 | findlabel(ls, g); /* close it if label already defined */ | ||
| 1199 | } | 1422 | } |
| 1200 | 1423 | ||
| 1201 | 1424 | ||
| 1202 | /* check for repeated labels on the same block */ | 1425 | /* |
| 1203 | static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { | 1426 | ** Break statement. Semantically equivalent to "goto break". |
| 1204 | int i; | 1427 | */ |
| 1205 | for (i = fs->bl->firstlabel; i < ll->n; i++) { | 1428 | static void breakstat (LexState *ls) { |
| 1206 | if (eqstr(label, ll->arr[i].name)) { | 1429 | int line = ls->linenumber; |
| 1207 | const char *msg = luaO_pushfstring(fs->ls->L, | 1430 | luaX_next(ls); /* skip break */ |
| 1208 | "label '%s' already defined on line %d", | 1431 | newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); |
| 1209 | getstr(label), ll->arr[i].line); | ||
| 1210 | semerror(fs->ls, msg); | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | } | 1432 | } |
| 1214 | 1433 | ||
| 1215 | 1434 | ||
| 1216 | /* skip no-op statements */ | 1435 | /* |
| 1217 | static void skipnoopstat (LexState *ls) { | 1436 | ** Check whether there is already a label with the given 'name'. |
| 1218 | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) | 1437 | */ |
| 1219 | statement(ls); | 1438 | static void checkrepeated (LexState *ls, TString *name) { |
| 1439 | Labeldesc *lb = findlabel(ls, name); | ||
| 1440 | if (unlikely(lb != NULL)) { /* already defined? */ | ||
| 1441 | const char *msg = "label '%s' already defined on line %d"; | ||
| 1442 | msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); | ||
| 1443 | luaK_semerror(ls, msg); /* error */ | ||
| 1444 | } | ||
| 1220 | } | 1445 | } |
| 1221 | 1446 | ||
| 1222 | 1447 | ||
| 1223 | static void labelstat (LexState *ls, TString *label, int line) { | 1448 | static void labelstat (LexState *ls, TString *name, int line) { |
| 1224 | /* label -> '::' NAME '::' */ | 1449 | /* label -> '::' NAME '::' */ |
| 1225 | FuncState *fs = ls->fs; | ||
| 1226 | Labellist *ll = &ls->dyd->label; | ||
| 1227 | int l; /* index of new label being created */ | ||
| 1228 | checkrepeated(fs, ll, label); /* check for repeated labels */ | ||
| 1229 | checknext(ls, TK_DBCOLON); /* skip double colon */ | 1450 | checknext(ls, TK_DBCOLON); /* skip double colon */ |
| 1230 | /* create new entry for this label */ | 1451 | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) |
| 1231 | l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); | 1452 | statement(ls); /* skip other no-op statements */ |
| 1232 | skipnoopstat(ls); /* skip other no-op statements */ | 1453 | checkrepeated(ls, name); /* check for repeated labels */ |
| 1233 | if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ | 1454 | createlabel(ls, name, line, block_follow(ls, 0)); |
| 1234 | /* assume that locals are already out of scope */ | ||
| 1235 | ll->arr[l].nactvar = fs->bl->nactvar; | ||
| 1236 | } | ||
| 1237 | findgotos(ls, &ll->arr[l]); | ||
| 1238 | } | 1455 | } |
| 1239 | 1456 | ||
| 1240 | 1457 | ||
| @@ -1269,58 +1486,83 @@ static void repeatstat (LexState *ls, int line) { | |||
| 1269 | statlist(ls); | 1486 | statlist(ls); |
| 1270 | check_match(ls, TK_UNTIL, TK_REPEAT, line); | 1487 | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
| 1271 | condexit = cond(ls); /* read condition (inside scope block) */ | 1488 | condexit = cond(ls); /* read condition (inside scope block) */ |
| 1272 | if (bl2.upval) /* upvalues? */ | ||
| 1273 | luaK_patchclose(fs, condexit, bl2.nactvar); | ||
| 1274 | leaveblock(fs); /* finish scope */ | 1489 | leaveblock(fs); /* finish scope */ |
| 1490 | if (bl2.upval) { /* upvalues? */ | ||
| 1491 | int exit = luaK_jump(fs); /* normal exit must jump over fix */ | ||
| 1492 | luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ | ||
| 1493 | luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); | ||
| 1494 | condexit = luaK_jump(fs); /* repeat after closing upvalues */ | ||
| 1495 | luaK_patchtohere(fs, exit); /* normal exit comes to here */ | ||
| 1496 | } | ||
| 1275 | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ | 1497 | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ |
| 1276 | leaveblock(fs); /* finish loop */ | 1498 | leaveblock(fs); /* finish loop */ |
| 1277 | } | 1499 | } |
| 1278 | 1500 | ||
| 1279 | 1501 | ||
| 1280 | static int exp1 (LexState *ls) { | 1502 | /* |
| 1503 | ** Read an expression and generate code to put its results in next | ||
| 1504 | ** stack slot. | ||
| 1505 | ** | ||
| 1506 | */ | ||
| 1507 | static void exp1 (LexState *ls) { | ||
| 1281 | expdesc e; | 1508 | expdesc e; |
| 1282 | int reg; | ||
| 1283 | expr(ls, &e); | 1509 | expr(ls, &e); |
| 1284 | luaK_exp2nextreg(ls->fs, &e); | 1510 | luaK_exp2nextreg(ls->fs, &e); |
| 1285 | lua_assert(e.k == VNONRELOC); | 1511 | lua_assert(e.k == VNONRELOC); |
| 1286 | reg = e.u.info; | ||
| 1287 | return reg; | ||
| 1288 | } | 1512 | } |
| 1289 | 1513 | ||
| 1290 | 1514 | ||
| 1291 | static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { | 1515 | /* |
| 1516 | ** Fix for instruction at position 'pc' to jump to 'dest'. | ||
| 1517 | ** (Jump addresses are relative in Lua). 'back' true means | ||
| 1518 | ** a back jump. | ||
| 1519 | */ | ||
| 1520 | static void fixforjump (FuncState *fs, int pc, int dest, int back) { | ||
| 1521 | Instruction *jmp = &fs->f->code[pc]; | ||
| 1522 | int offset = dest - (pc + 1); | ||
| 1523 | if (back) | ||
| 1524 | offset = -offset; | ||
| 1525 | if (unlikely(offset > MAXARG_Bx)) | ||
| 1526 | luaX_syntaxerror(fs->ls, "control structure too long"); | ||
| 1527 | SETARG_Bx(*jmp, offset); | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | |||
| 1531 | /* | ||
| 1532 | ** Generate code for a 'for' loop. | ||
| 1533 | */ | ||
| 1534 | static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { | ||
| 1292 | /* forbody -> DO block */ | 1535 | /* forbody -> DO block */ |
| 1536 | static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; | ||
| 1537 | static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; | ||
| 1293 | BlockCnt bl; | 1538 | BlockCnt bl; |
| 1294 | FuncState *fs = ls->fs; | 1539 | FuncState *fs = ls->fs; |
| 1295 | int prep, endfor; | 1540 | int prep, endfor; |
| 1296 | adjustlocalvars(ls, 3); /* control variables */ | ||
| 1297 | checknext(ls, TK_DO); | 1541 | checknext(ls, TK_DO); |
| 1298 | prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); | 1542 | prep = luaK_codeABx(fs, forprep[isgen], base, 0); |
| 1299 | enterblock(fs, &bl, 0); /* scope for declared variables */ | 1543 | enterblock(fs, &bl, 0); /* scope for declared variables */ |
| 1300 | adjustlocalvars(ls, nvars); | 1544 | adjustlocalvars(ls, nvars); |
| 1301 | luaK_reserveregs(fs, nvars); | 1545 | luaK_reserveregs(fs, nvars); |
| 1302 | block(ls); | 1546 | block(ls); |
| 1303 | leaveblock(fs); /* end of scope for declared variables */ | 1547 | leaveblock(fs); /* end of scope for declared variables */ |
| 1304 | luaK_patchtohere(fs, prep); | 1548 | fixforjump(fs, prep, luaK_getlabel(fs), 0); |
| 1305 | if (isnum) /* numeric for? */ | 1549 | if (isgen) { /* generic for? */ |
| 1306 | endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); | ||
| 1307 | else { /* generic for */ | ||
| 1308 | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); | 1550 | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); |
| 1309 | luaK_fixline(fs, line); | 1551 | luaK_fixline(fs, line); |
| 1310 | endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); | ||
| 1311 | } | 1552 | } |
| 1312 | luaK_patchlist(fs, endfor, prep + 1); | 1553 | endfor = luaK_codeABx(fs, forloop[isgen], base, 0); |
| 1554 | fixforjump(fs, endfor, prep + 1, 1); | ||
| 1313 | luaK_fixline(fs, line); | 1555 | luaK_fixline(fs, line); |
| 1314 | } | 1556 | } |
| 1315 | 1557 | ||
| 1316 | 1558 | ||
| 1317 | static void fornum (LexState *ls, TString *varname, int line) { | 1559 | static void fornum (LexState *ls, TString *varname, int line) { |
| 1318 | /* fornum -> NAME = exp1,exp1[,exp1] forbody */ | 1560 | /* fornum -> NAME = exp,exp[,exp] forbody */ |
| 1319 | FuncState *fs = ls->fs; | 1561 | FuncState *fs = ls->fs; |
| 1320 | int base = fs->freereg; | 1562 | int base = fs->freereg; |
| 1321 | new_localvarliteral(ls, "(for index)"); | 1563 | new_localvarliteral(ls, "(for state)"); |
| 1322 | new_localvarliteral(ls, "(for limit)"); | 1564 | new_localvarliteral(ls, "(for state)"); |
| 1323 | new_localvarliteral(ls, "(for step)"); | 1565 | new_localvarliteral(ls, "(for state)"); |
| 1324 | new_localvar(ls, varname); | 1566 | new_localvar(ls, varname); |
| 1325 | checknext(ls, '='); | 1567 | checknext(ls, '='); |
| 1326 | exp1(ls); /* initial value */ | 1568 | exp1(ls); /* initial value */ |
| @@ -1329,10 +1571,11 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
| 1329 | if (testnext(ls, ',')) | 1571 | if (testnext(ls, ',')) |
| 1330 | exp1(ls); /* optional step */ | 1572 | exp1(ls); /* optional step */ |
| 1331 | else { /* default step = 1 */ | 1573 | else { /* default step = 1 */ |
| 1332 | luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); | 1574 | luaK_int(fs, fs->freereg, 1); |
| 1333 | luaK_reserveregs(fs, 1); | 1575 | luaK_reserveregs(fs, 1); |
| 1334 | } | 1576 | } |
| 1335 | forbody(ls, base, line, 1, 1); | 1577 | adjustlocalvars(ls, 3); /* control variables */ |
| 1578 | forbody(ls, base, line, 1, 0); | ||
| 1336 | } | 1579 | } |
| 1337 | 1580 | ||
| 1338 | 1581 | ||
| @@ -1340,13 +1583,14 @@ static void forlist (LexState *ls, TString *indexname) { | |||
| 1340 | /* forlist -> NAME {,NAME} IN explist forbody */ | 1583 | /* forlist -> NAME {,NAME} IN explist forbody */ |
| 1341 | FuncState *fs = ls->fs; | 1584 | FuncState *fs = ls->fs; |
| 1342 | expdesc e; | 1585 | expdesc e; |
| 1343 | int nvars = 4; /* gen, state, control, plus at least one declared var */ | 1586 | int nvars = 5; /* gen, state, control, toclose, 'indexname' */ |
| 1344 | int line; | 1587 | int line; |
| 1345 | int base = fs->freereg; | 1588 | int base = fs->freereg; |
| 1346 | /* create control variables */ | 1589 | /* create control variables */ |
| 1347 | new_localvarliteral(ls, "(for generator)"); | ||
| 1348 | new_localvarliteral(ls, "(for state)"); | 1590 | new_localvarliteral(ls, "(for state)"); |
| 1349 | new_localvarliteral(ls, "(for control)"); | 1591 | new_localvarliteral(ls, "(for state)"); |
| 1592 | new_localvarliteral(ls, "(for state)"); | ||
| 1593 | new_localvarliteral(ls, "(for state)"); | ||
| 1350 | /* create declared variables */ | 1594 | /* create declared variables */ |
| 1351 | new_localvar(ls, indexname); | 1595 | new_localvar(ls, indexname); |
| 1352 | while (testnext(ls, ',')) { | 1596 | while (testnext(ls, ',')) { |
| @@ -1355,9 +1599,11 @@ static void forlist (LexState *ls, TString *indexname) { | |||
| 1355 | } | 1599 | } |
| 1356 | checknext(ls, TK_IN); | 1600 | checknext(ls, TK_IN); |
| 1357 | line = ls->linenumber; | 1601 | line = ls->linenumber; |
| 1358 | adjust_assign(ls, 3, explist(ls, &e), &e); | 1602 | adjust_assign(ls, 4, explist(ls, &e), &e); |
| 1603 | adjustlocalvars(ls, 4); /* control variables */ | ||
| 1604 | markupval(fs, fs->nactvar); /* last control var. must be closed */ | ||
| 1359 | luaK_checkstack(fs, 3); /* extra space to call generator */ | 1605 | luaK_checkstack(fs, 3); /* extra space to call generator */ |
| 1360 | forbody(ls, base, line, nvars - 3, 0); | 1606 | forbody(ls, base, line, nvars - 4, 1); |
| 1361 | } | 1607 | } |
| 1362 | 1608 | ||
| 1363 | 1609 | ||
| @@ -1379,28 +1625,68 @@ static void forstat (LexState *ls, int line) { | |||
| 1379 | } | 1625 | } |
| 1380 | 1626 | ||
| 1381 | 1627 | ||
| 1628 | /* | ||
| 1629 | ** Check whether next instruction is a single jump (a 'break', a 'goto' | ||
| 1630 | ** to a forward label, or a 'goto' to a backward label with no variable | ||
| 1631 | ** to close). If so, set the name of the 'label' it is jumping to | ||
| 1632 | ** ("break" for a 'break') or to where it is jumping to ('target') and | ||
| 1633 | ** return true. If not a single jump, leave input unchanged, to be | ||
| 1634 | ** handled as a regular statement. | ||
| 1635 | */ | ||
| 1636 | static int issinglejump (LexState *ls, TString **label, int *target) { | ||
| 1637 | if (testnext(ls, TK_BREAK)) { /* a break? */ | ||
| 1638 | *label = luaS_newliteral(ls->L, "break"); | ||
| 1639 | return 1; | ||
| 1640 | } | ||
| 1641 | else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) | ||
| 1642 | return 0; /* not a valid goto */ | ||
| 1643 | else { | ||
| 1644 | TString *lname = ls->lookahead.seminfo.ts; /* label's id */ | ||
| 1645 | Labeldesc *lb = findlabel(ls, lname); | ||
| 1646 | if (lb) { /* a backward jump? */ | ||
| 1647 | /* does it need to close variables? */ | ||
| 1648 | if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) | ||
| 1649 | return 0; /* not a single jump; cannot optimize */ | ||
| 1650 | *target = lb->pc; | ||
| 1651 | } | ||
| 1652 | else /* jump forward */ | ||
| 1653 | *label = lname; | ||
| 1654 | luaX_next(ls); /* skip goto */ | ||
| 1655 | luaX_next(ls); /* skip name */ | ||
| 1656 | return 1; | ||
| 1657 | } | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | |||
| 1382 | static void test_then_block (LexState *ls, int *escapelist) { | 1661 | static void test_then_block (LexState *ls, int *escapelist) { |
| 1383 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ | 1662 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
| 1384 | BlockCnt bl; | 1663 | BlockCnt bl; |
| 1664 | int line; | ||
| 1385 | FuncState *fs = ls->fs; | 1665 | FuncState *fs = ls->fs; |
| 1666 | TString *jlb = NULL; | ||
| 1667 | int target = NO_JUMP; | ||
| 1386 | expdesc v; | 1668 | expdesc v; |
| 1387 | int jf; /* instruction to skip 'then' code (if condition is false) */ | 1669 | int jf; /* instruction to skip 'then' code (if condition is false) */ |
| 1388 | luaX_next(ls); /* skip IF or ELSEIF */ | 1670 | luaX_next(ls); /* skip IF or ELSEIF */ |
| 1389 | expr(ls, &v); /* read condition */ | 1671 | expr(ls, &v); /* read condition */ |
| 1390 | checknext(ls, TK_THEN); | 1672 | checknext(ls, TK_THEN); |
| 1391 | if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { | 1673 | line = ls->linenumber; |
| 1674 | if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ | ||
| 1392 | luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ | 1675 | luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ |
| 1393 | enterblock(fs, &bl, 0); /* must enter block before 'goto' */ | 1676 | enterblock(fs, &bl, 0); /* must enter block before 'goto' */ |
| 1394 | gotostat(ls, v.t); /* handle goto/break */ | 1677 | if (jlb != NULL) /* forward jump? */ |
| 1395 | while (testnext(ls, ';')) {} /* skip colons */ | 1678 | newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ |
| 1396 | if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ | 1679 | else /* backward jump */ |
| 1680 | luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ | ||
| 1681 | while (testnext(ls, ';')) {} /* skip semicolons */ | ||
| 1682 | if (block_follow(ls, 0)) { /* jump is the entire block? */ | ||
| 1397 | leaveblock(fs); | 1683 | leaveblock(fs); |
| 1398 | return; /* and that is it */ | 1684 | return; /* and that is it */ |
| 1399 | } | 1685 | } |
| 1400 | else /* must skip over 'then' part if condition is false */ | 1686 | else /* must skip over 'then' part if condition is false */ |
| 1401 | jf = luaK_jump(fs); | 1687 | jf = luaK_jump(fs); |
| 1402 | } | 1688 | } |
| 1403 | else { /* regular case (not goto/break) */ | 1689 | else { /* regular case (not a jump) */ |
| 1404 | luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ | 1690 | luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ |
| 1405 | enterblock(fs, &bl, 0); | 1691 | enterblock(fs, &bl, 0); |
| 1406 | jf = v.f; | 1692 | jf = v.f; |
| @@ -1431,21 +1717,60 @@ static void ifstat (LexState *ls, int line) { | |||
| 1431 | static void localfunc (LexState *ls) { | 1717 | static void localfunc (LexState *ls) { |
| 1432 | expdesc b; | 1718 | expdesc b; |
| 1433 | FuncState *fs = ls->fs; | 1719 | FuncState *fs = ls->fs; |
| 1720 | int fvar = fs->nactvar; /* function's variable index */ | ||
| 1434 | new_localvar(ls, str_checkname(ls)); /* new local variable */ | 1721 | new_localvar(ls, str_checkname(ls)); /* new local variable */ |
| 1435 | adjustlocalvars(ls, 1); /* enter its scope */ | 1722 | adjustlocalvars(ls, 1); /* enter its scope */ |
| 1436 | body(ls, &b, 0, ls->linenumber); /* function created in next register */ | 1723 | body(ls, &b, 0, ls->linenumber); /* function created in next register */ |
| 1437 | /* debug information will only see the variable after this point! */ | 1724 | /* debug information will only see the variable after this point! */ |
| 1438 | getlocvar(fs, b.u.info)->startpc = fs->pc; | 1725 | localdebuginfo(fs, fvar)->startpc = fs->pc; |
| 1726 | } | ||
| 1727 | |||
| 1728 | |||
| 1729 | static int getlocalattribute (LexState *ls) { | ||
| 1730 | /* ATTRIB -> ['<' Name '>'] */ | ||
| 1731 | if (testnext(ls, '<')) { | ||
| 1732 | const char *attr = getstr(str_checkname(ls)); | ||
| 1733 | checknext(ls, '>'); | ||
| 1734 | if (strcmp(attr, "const") == 0) | ||
| 1735 | return RDKCONST; /* read-only variable */ | ||
| 1736 | else if (strcmp(attr, "close") == 0) | ||
| 1737 | return RDKTOCLOSE; /* to-be-closed variable */ | ||
| 1738 | else | ||
| 1739 | luaK_semerror(ls, | ||
| 1740 | luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); | ||
| 1741 | } | ||
| 1742 | return VDKREG; /* regular variable */ | ||
| 1743 | } | ||
| 1744 | |||
| 1745 | |||
| 1746 | static void checktoclose (LexState *ls, int level) { | ||
| 1747 | if (level != -1) { /* is there a to-be-closed variable? */ | ||
| 1748 | FuncState *fs = ls->fs; | ||
| 1749 | markupval(fs, level + 1); | ||
| 1750 | fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ | ||
| 1751 | luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); | ||
| 1752 | } | ||
| 1439 | } | 1753 | } |
| 1440 | 1754 | ||
| 1441 | 1755 | ||
| 1442 | static void localstat (LexState *ls) { | 1756 | static void localstat (LexState *ls) { |
| 1443 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ | 1757 | /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ |
| 1758 | FuncState *fs = ls->fs; | ||
| 1759 | int toclose = -1; /* index of to-be-closed variable (if any) */ | ||
| 1760 | Vardesc *var; /* last variable */ | ||
| 1761 | int vidx, kind; /* index and kind of last variable */ | ||
| 1444 | int nvars = 0; | 1762 | int nvars = 0; |
| 1445 | int nexps; | 1763 | int nexps; |
| 1446 | expdesc e; | 1764 | expdesc e; |
| 1447 | do { | 1765 | do { |
| 1448 | new_localvar(ls, str_checkname(ls)); | 1766 | vidx = new_localvar(ls, str_checkname(ls)); |
| 1767 | kind = getlocalattribute(ls); | ||
| 1768 | getlocalvardesc(fs, vidx)->vd.kind = kind; | ||
| 1769 | if (kind == RDKTOCLOSE) { /* to-be-closed? */ | ||
| 1770 | if (toclose != -1) /* one already present? */ | ||
| 1771 | luaK_semerror(ls, "multiple to-be-closed variables in local list"); | ||
| 1772 | toclose = fs->nactvar + nvars; | ||
| 1773 | } | ||
| 1449 | nvars++; | 1774 | nvars++; |
| 1450 | } while (testnext(ls, ',')); | 1775 | } while (testnext(ls, ',')); |
| 1451 | if (testnext(ls, '=')) | 1776 | if (testnext(ls, '=')) |
| @@ -1454,8 +1779,19 @@ static void localstat (LexState *ls) { | |||
| 1454 | e.k = VVOID; | 1779 | e.k = VVOID; |
| 1455 | nexps = 0; | 1780 | nexps = 0; |
| 1456 | } | 1781 | } |
| 1457 | adjust_assign(ls, nvars, nexps, &e); | 1782 | var = getlocalvardesc(fs, vidx); /* get last variable */ |
| 1458 | adjustlocalvars(ls, nvars); | 1783 | if (nvars == nexps && /* no adjustments? */ |
| 1784 | var->vd.kind == RDKCONST && /* last variable is const? */ | ||
| 1785 | luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ | ||
| 1786 | var->vd.kind = RDKCTC; /* variable is a compile-time constant */ | ||
| 1787 | adjustlocalvars(ls, nvars - 1); /* exclude last variable */ | ||
| 1788 | fs->nactvar++; /* but count it */ | ||
| 1789 | } | ||
| 1790 | else { | ||
| 1791 | adjust_assign(ls, nvars, nexps, &e); | ||
| 1792 | adjustlocalvars(ls, nvars); | ||
| 1793 | } | ||
| 1794 | checktoclose(ls, toclose); | ||
| 1459 | } | 1795 | } |
| 1460 | 1796 | ||
| 1461 | 1797 | ||
| @@ -1492,11 +1828,13 @@ static void exprstat (LexState *ls) { | |||
| 1492 | suffixedexp(ls, &v.v); | 1828 | suffixedexp(ls, &v.v); |
| 1493 | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ | 1829 | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ |
| 1494 | v.prev = NULL; | 1830 | v.prev = NULL; |
| 1495 | assignment(ls, &v, 1); | 1831 | restassign(ls, &v, 1); |
| 1496 | } | 1832 | } |
| 1497 | else { /* stat -> func */ | 1833 | else { /* stat -> func */ |
| 1834 | Instruction *inst; | ||
| 1498 | check_condition(ls, v.v.k == VCALL, "syntax error"); | 1835 | check_condition(ls, v.v.k == VCALL, "syntax error"); |
| 1499 | SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ | 1836 | inst = &getinstruction(fs, &v.v); |
| 1837 | SETARG_C(*inst, 1); /* call statement uses no results */ | ||
| 1500 | } | 1838 | } |
| 1501 | } | 1839 | } |
| 1502 | 1840 | ||
| @@ -1505,26 +1843,25 @@ static void retstat (LexState *ls) { | |||
| 1505 | /* stat -> RETURN [explist] [';'] */ | 1843 | /* stat -> RETURN [explist] [';'] */ |
| 1506 | FuncState *fs = ls->fs; | 1844 | FuncState *fs = ls->fs; |
| 1507 | expdesc e; | 1845 | expdesc e; |
| 1508 | int first, nret; /* registers with returned values */ | 1846 | int nret; /* number of values being returned */ |
| 1847 | int first = luaY_nvarstack(fs); /* first slot to be returned */ | ||
| 1509 | if (block_follow(ls, 1) || ls->t.token == ';') | 1848 | if (block_follow(ls, 1) || ls->t.token == ';') |
| 1510 | first = nret = 0; /* return no values */ | 1849 | nret = 0; /* return no values */ |
| 1511 | else { | 1850 | else { |
| 1512 | nret = explist(ls, &e); /* optional return values */ | 1851 | nret = explist(ls, &e); /* optional return values */ |
| 1513 | if (hasmultret(e.k)) { | 1852 | if (hasmultret(e.k)) { |
| 1514 | luaK_setmultret(fs, &e); | 1853 | luaK_setmultret(fs, &e); |
| 1515 | if (e.k == VCALL && nret == 1) { /* tail call? */ | 1854 | if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ |
| 1516 | SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); | 1855 | SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); |
| 1517 | lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); | 1856 | lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); |
| 1518 | } | 1857 | } |
| 1519 | first = fs->nactvar; | ||
| 1520 | nret = LUA_MULTRET; /* return all values */ | 1858 | nret = LUA_MULTRET; /* return all values */ |
| 1521 | } | 1859 | } |
| 1522 | else { | 1860 | else { |
| 1523 | if (nret == 1) /* only one single value? */ | 1861 | if (nret == 1) /* only one single value? */ |
| 1524 | first = luaK_exp2anyreg(fs, &e); | 1862 | first = luaK_exp2anyreg(fs, &e); /* can use original slot */ |
| 1525 | else { | 1863 | else { /* values must go to the top of the stack */ |
| 1526 | luaK_exp2nextreg(fs, &e); /* values must go to the stack */ | 1864 | luaK_exp2nextreg(fs, &e); |
| 1527 | first = fs->nactvar; /* return all active values */ | ||
| 1528 | lua_assert(nret == fs->freereg - first); | 1865 | lua_assert(nret == fs->freereg - first); |
| 1529 | } | 1866 | } |
| 1530 | } | 1867 | } |
| @@ -1586,9 +1923,13 @@ static void statement (LexState *ls) { | |||
| 1586 | retstat(ls); | 1923 | retstat(ls); |
| 1587 | break; | 1924 | break; |
| 1588 | } | 1925 | } |
| 1589 | case TK_BREAK: /* stat -> breakstat */ | 1926 | case TK_BREAK: { /* stat -> breakstat */ |
| 1927 | breakstat(ls); | ||
| 1928 | break; | ||
| 1929 | } | ||
| 1590 | case TK_GOTO: { /* stat -> 'goto' NAME */ | 1930 | case TK_GOTO: { /* stat -> 'goto' NAME */ |
| 1591 | gotostat(ls, luaK_jump(ls->fs)); | 1931 | luaX_next(ls); /* skip 'goto' */ |
| 1932 | gotostat(ls); | ||
| 1592 | break; | 1933 | break; |
| 1593 | } | 1934 | } |
| 1594 | default: { /* stat -> func | assignment */ | 1935 | default: { /* stat -> func | assignment */ |
| @@ -1597,8 +1938,8 @@ static void statement (LexState *ls) { | |||
| 1597 | } | 1938 | } |
| 1598 | } | 1939 | } |
| 1599 | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && | 1940 | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && |
| 1600 | ls->fs->freereg >= ls->fs->nactvar); | 1941 | ls->fs->freereg >= luaY_nvarstack(ls->fs)); |
| 1601 | ls->fs->freereg = ls->fs->nactvar; /* free registers */ | 1942 | ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ |
| 1602 | leavelevel(ls); | 1943 | leavelevel(ls); |
| 1603 | } | 1944 | } |
| 1604 | 1945 | ||
| @@ -1611,11 +1952,15 @@ static void statement (LexState *ls) { | |||
| 1611 | */ | 1952 | */ |
| 1612 | static void mainfunc (LexState *ls, FuncState *fs) { | 1953 | static void mainfunc (LexState *ls, FuncState *fs) { |
| 1613 | BlockCnt bl; | 1954 | BlockCnt bl; |
| 1614 | expdesc v; | 1955 | Upvaldesc *env; |
| 1615 | open_func(ls, fs, &bl); | 1956 | open_func(ls, fs, &bl); |
| 1616 | fs->f->is_vararg = 1; /* main function is always declared vararg */ | 1957 | setvararg(fs, 0); /* main function is always declared vararg */ |
| 1617 | init_exp(&v, VLOCAL, 0); /* create and... */ | 1958 | env = allocupvalue(fs); /* ...set environment upvalue */ |
| 1618 | newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ | 1959 | env->instack = 1; |
| 1960 | env->idx = 0; | ||
| 1961 | env->kind = VDKREG; | ||
| 1962 | env->name = ls->envn; | ||
| 1963 | luaC_objbarrier(ls->L, fs->f, env->name); | ||
| 1619 | luaX_next(ls); /* read first token */ | 1964 | luaX_next(ls); /* read first token */ |
| 1620 | statlist(ls); /* parse main body */ | 1965 | statlist(ls); /* parse main body */ |
| 1621 | check(ls, TK_EOS); | 1966 | check(ls, TK_EOS); |
| @@ -1628,14 +1973,15 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | |||
| 1628 | LexState lexstate; | 1973 | LexState lexstate; |
| 1629 | FuncState funcstate; | 1974 | FuncState funcstate; |
| 1630 | LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ | 1975 | LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ |
| 1631 | setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ | 1976 | setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ |
| 1632 | luaD_inctop(L); | 1977 | luaD_inctop(L); |
| 1633 | lexstate.h = luaH_new(L); /* create table for scanner */ | 1978 | lexstate.h = luaH_new(L); /* create table for scanner */ |
| 1634 | sethvalue(L, L->top, lexstate.h); /* anchor it */ | 1979 | sethvalue2s(L, L->top, lexstate.h); /* anchor it */ |
| 1635 | luaD_inctop(L); | 1980 | luaD_inctop(L); |
| 1636 | funcstate.f = cl->p = luaF_newproto(L); | 1981 | funcstate.f = cl->p = luaF_newproto(L); |
| 1982 | luaC_objbarrier(L, cl, cl->p); | ||
| 1637 | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ | 1983 | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ |
| 1638 | lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ | 1984 | luaC_objbarrier(L, funcstate.f, funcstate.f->source); |
| 1639 | lexstate.buff = buff; | 1985 | lexstate.buff = buff; |
| 1640 | lexstate.dyd = dyd; | 1986 | lexstate.dyd = dyd; |
| 1641 | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; | 1987 | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; |
