/src/testdir/build/lua-master/source/lparser.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lparser.c $ |
3 | | ** Lua Parser |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lparser_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <string.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lcode.h" |
19 | | #include "ldebug.h" |
20 | | #include "ldo.h" |
21 | | #include "lfunc.h" |
22 | | #include "llex.h" |
23 | | #include "lmem.h" |
24 | | #include "lobject.h" |
25 | | #include "lopcodes.h" |
26 | | #include "lparser.h" |
27 | | #include "lstate.h" |
28 | | #include "lstring.h" |
29 | | #include "ltable.h" |
30 | | |
31 | | |
32 | | |
33 | | /* maximum number of variable declarations per function (must be |
34 | | smaller than 250, due to the bytecode format) */ |
35 | 3.70M | #define MAXVARS 200 |
36 | | |
37 | | |
38 | 8.18M | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) |
39 | | |
40 | | |
41 | | /* because all strings are unified by the scanner, the parser |
42 | | can use pointer equality for string equality */ |
43 | 1.87G | #define eqstr(a,b) ((a) == (b)) |
44 | | |
45 | | |
46 | | /* |
47 | | ** nodes for block list (list of active blocks) |
48 | | */ |
49 | | typedef struct BlockCnt { |
50 | | struct BlockCnt *previous; /* chain */ |
51 | | int firstlabel; /* index of first label in this block */ |
52 | | int firstgoto; /* index of first pending goto in this block */ |
53 | | short nactvar; /* number of active declarations at block entry */ |
54 | | lu_byte upval; /* true if some variable in the block is an upvalue */ |
55 | | lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ |
56 | | lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ |
57 | | } BlockCnt; |
58 | | |
59 | | |
60 | | |
61 | | /* |
62 | | ** prototypes for recursive non-terminal functions |
63 | | */ |
64 | | static void statement (LexState *ls); |
65 | | static void expr (LexState *ls, expdesc *v); |
66 | | |
67 | | |
68 | 544k | static l_noret error_expected (LexState *ls, int token) { |
69 | 544k | luaX_syntaxerror(ls, |
70 | 544k | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); |
71 | 544k | } |
72 | | |
73 | | |
74 | 1.47k | static l_noret errorlimit (FuncState *fs, int limit, const char *what) { |
75 | 1.47k | lua_State *L = fs->ls->L; |
76 | 1.47k | const char *msg; |
77 | 1.47k | int line = fs->f->linedefined; |
78 | 1.47k | const char *where = (line == 0) |
79 | 1.47k | ? "main function" |
80 | 1.47k | : luaO_pushfstring(L, "function at line %d", line); |
81 | 1.47k | msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", |
82 | 1.47k | what, limit, where); |
83 | 1.47k | luaX_syntaxerror(fs->ls, msg); |
84 | 1.47k | } |
85 | | |
86 | | |
87 | 25.2M | void luaY_checklimit (FuncState *fs, int v, int l, const char *what) { |
88 | 25.2M | if (l_unlikely(v > l)) errorlimit(fs, l, what); |
89 | 25.2M | } |
90 | | |
91 | | |
92 | | /* |
93 | | ** Test whether next token is 'c'; if so, skip it. |
94 | | */ |
95 | 41.2M | static int testnext (LexState *ls, int c) { |
96 | 41.2M | if (ls->t.token == c) { |
97 | 25.1M | luaX_next(ls); |
98 | 25.1M | return 1; |
99 | 25.1M | } |
100 | 16.0M | else return 0; |
101 | 41.2M | } |
102 | | |
103 | | |
104 | | /* |
105 | | ** Check that next token is 'c'. |
106 | | */ |
107 | 192M | static void check (LexState *ls, int c) { |
108 | 192M | if (ls->t.token != c) |
109 | 344k | error_expected(ls, c); |
110 | 192M | } |
111 | | |
112 | | |
113 | | /* |
114 | | ** Check that next token is 'c' and skip it. |
115 | | */ |
116 | 11.5M | static void checknext (LexState *ls, int c) { |
117 | 11.5M | check(ls, c); |
118 | 11.5M | luaX_next(ls); |
119 | 11.5M | } |
120 | | |
121 | | |
122 | 13.6M | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } |
123 | | |
124 | | |
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 | | */ |
130 | 5.21M | static void check_match (LexState *ls, int what, int who, int where) { |
131 | 5.21M | if (l_unlikely(!testnext(ls, what))) { |
132 | 203k | if (where == ls->linenumber) /* all in the same line? */ |
133 | 199k | error_expected(ls, what); /* do not need a complex message */ |
134 | 3.80k | else { |
135 | 3.80k | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, |
136 | 3.80k | "%s expected (to close %s at line %d)", |
137 | 3.80k | luaX_token2str(ls, what), luaX_token2str(ls, who), where)); |
138 | 3.80k | } |
139 | 203k | } |
140 | 5.21M | } |
141 | | |
142 | | |
143 | 177M | static TString *str_checkname (LexState *ls) { |
144 | 177M | TString *ts; |
145 | 177M | check(ls, TK_NAME); |
146 | 177M | ts = ls->t.seminfo.ts; |
147 | 177M | luaX_next(ls); |
148 | 177M | return ts; |
149 | 177M | } |
150 | | |
151 | | |
152 | 479M | static void init_exp (expdesc *e, expkind k, int i) { |
153 | 479M | e->f = e->t = NO_JUMP; |
154 | 479M | e->k = k; |
155 | 479M | e->u.info = i; |
156 | 479M | } |
157 | | |
158 | | |
159 | 141M | static void codestring (expdesc *e, TString *s) { |
160 | 141M | e->f = e->t = NO_JUMP; |
161 | 141M | e->k = VKSTR; |
162 | 141M | e->u.strval = s; |
163 | 141M | } |
164 | | |
165 | | |
166 | 7.87M | static void codename (LexState *ls, expdesc *e) { |
167 | 7.87M | codestring(e, str_checkname(ls)); |
168 | 7.87M | } |
169 | | |
170 | | |
171 | | /* |
172 | | ** Register a new local variable in the active 'Proto' (for debug |
173 | | ** information). |
174 | | */ |
175 | | static short registerlocalvar (LexState *ls, FuncState *fs, |
176 | 3.70M | TString *varname) { |
177 | 3.70M | Proto *f = fs->f; |
178 | 3.70M | int oldsize = f->sizelocvars; |
179 | 3.70M | luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, |
180 | 3.70M | LocVar, SHRT_MAX, "local variables"); |
181 | 10.7M | while (oldsize < f->sizelocvars) |
182 | 7.00M | f->locvars[oldsize++].varname = NULL; |
183 | 3.70M | f->locvars[fs->ndebugvars].varname = varname; |
184 | 3.70M | f->locvars[fs->ndebugvars].startpc = fs->pc; |
185 | 3.70M | luaC_objbarrier(ls->L, f, varname); |
186 | 3.70M | return fs->ndebugvars++; |
187 | 3.70M | } |
188 | | |
189 | | |
190 | | /* |
191 | | ** Create a new variable with the given 'name' and given 'kind'. |
192 | | ** Return its index in the function. |
193 | | */ |
194 | 5.26M | static int new_varkind (LexState *ls, TString *name, lu_byte kind) { |
195 | 5.26M | lua_State *L = ls->L; |
196 | 5.26M | FuncState *fs = ls->fs; |
197 | 5.26M | Dyndata *dyd = ls->dyd; |
198 | 5.26M | Vardesc *var; |
199 | 5.26M | luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, |
200 | 5.26M | dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations"); |
201 | 5.26M | var = &dyd->actvar.arr[dyd->actvar.n++]; |
202 | 5.26M | var->vd.kind = kind; /* default */ |
203 | 5.26M | var->vd.name = name; |
204 | 5.26M | return dyd->actvar.n - 1 - fs->firstlocal; |
205 | 5.26M | } |
206 | | |
207 | | |
208 | | /* |
209 | | ** Create a new local variable with the given 'name' and regular kind. |
210 | | */ |
211 | 2.19M | static int new_localvar (LexState *ls, TString *name) { |
212 | 2.19M | return new_varkind(ls, name, VDKREG); |
213 | 2.19M | } |
214 | | |
215 | | #define new_localvarliteral(ls,v) \ |
216 | 301k | new_localvar(ls, \ |
217 | 301k | luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); |
218 | | |
219 | | |
220 | | |
221 | | /* |
222 | | ** Return the "variable description" (Vardesc) of a given variable. |
223 | | ** (Unless noted otherwise, all variables are referred to by their |
224 | | ** compiler indices.) |
225 | | */ |
226 | 1.80G | static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { |
227 | 1.80G | return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; |
228 | 1.80G | } |
229 | | |
230 | | |
231 | | /* |
232 | | ** Convert 'nvar', a compiler index level, to its corresponding |
233 | | ** register. For that, search for the highest variable below that level |
234 | | ** that is in a register and uses its register index ('ridx') plus one. |
235 | | */ |
236 | 442M | static lu_byte reglevel (FuncState *fs, int nvar) { |
237 | 665M | while (nvar-- > 0) { |
238 | 425M | Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ |
239 | 425M | if (varinreg(vd)) /* is in a register? */ |
240 | 202M | return cast_byte(vd->vd.ridx + 1); |
241 | 425M | } |
242 | 239M | return 0; /* no variables in registers */ |
243 | 442M | } |
244 | | |
245 | | |
246 | | /* |
247 | | ** Return the number of variables in the register stack for the given |
248 | | ** function. |
249 | | */ |
250 | 431M | lu_byte luaY_nvarstack (FuncState *fs) { |
251 | 431M | return reglevel(fs, fs->nactvar); |
252 | 431M | } |
253 | | |
254 | | |
255 | | /* |
256 | | ** Get the debug-information entry for current variable 'vidx'. |
257 | | */ |
258 | 2.45M | static LocVar *localdebuginfo (FuncState *fs, int vidx) { |
259 | 2.45M | Vardesc *vd = getlocalvardesc(fs, vidx); |
260 | 2.45M | if (!varinreg(vd)) |
261 | 155k | return NULL; /* no debug info. for constants */ |
262 | 2.29M | else { |
263 | 2.29M | int idx = vd->vd.pidx; |
264 | 2.29M | lua_assert(idx < fs->ndebugvars); |
265 | 2.29M | return &fs->f->locvars[idx]; |
266 | 2.29M | } |
267 | 2.45M | } |
268 | | |
269 | | |
270 | | /* |
271 | | ** Create an expression representing variable 'vidx' |
272 | | */ |
273 | 6.94M | static void init_var (FuncState *fs, expdesc *e, int vidx) { |
274 | 6.94M | e->f = e->t = NO_JUMP; |
275 | 6.94M | e->k = VLOCAL; |
276 | 6.94M | e->u.var.vidx = cast_short(vidx); |
277 | 6.94M | e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; |
278 | 6.94M | } |
279 | | |
280 | | |
281 | | /* |
282 | | ** Raises an error if variable described by 'e' is read only |
283 | | */ |
284 | 6.84M | static void check_readonly (LexState *ls, expdesc *e) { |
285 | 6.84M | FuncState *fs = ls->fs; |
286 | 6.84M | TString *varname = NULL; /* to be set if variable is const */ |
287 | 6.84M | switch (e->k) { |
288 | 4 | case VCONST: { |
289 | 4 | varname = ls->dyd->actvar.arr[e->u.info].vd.name; |
290 | 4 | break; |
291 | 0 | } |
292 | 202k | case VLOCAL: { |
293 | 202k | Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); |
294 | 202k | if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ |
295 | 423 | varname = vardesc->vd.name; |
296 | 202k | break; |
297 | 0 | } |
298 | 386k | case VUPVAL: { |
299 | 386k | Upvaldesc *up = &fs->f->upvalues[e->u.info]; |
300 | 386k | if (up->kind != VDKREG) |
301 | 4 | varname = up->name; |
302 | 386k | break; |
303 | 0 | } |
304 | 6.24M | case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ |
305 | 6.24M | if (e->u.ind.ro) /* read-only? */ |
306 | 422 | varname = tsvalue(&fs->f->k[e->u.ind.keystr]); |
307 | 6.24M | break; |
308 | 6.24M | } |
309 | 6.24M | default: |
310 | 5.56k | lua_assert(e->k == VINDEXI); /* this one doesn't need any check */ |
311 | 5.56k | return; /* integer index cannot be read-only */ |
312 | 6.84M | } |
313 | 6.83M | if (varname) |
314 | 853 | luaK_semerror(ls, "attempt to assign to const variable '%s'", |
315 | 853 | getstr(varname)); |
316 | 6.83M | } |
317 | | |
318 | | |
319 | | /* |
320 | | ** Start the scope for the last 'nvars' created variables. |
321 | | */ |
322 | 2.73M | static void adjustlocalvars (LexState *ls, int nvars) { |
323 | 2.73M | FuncState *fs = ls->fs; |
324 | 2.73M | int reglevel = luaY_nvarstack(fs); |
325 | 2.73M | int i; |
326 | 6.43M | for (i = 0; i < nvars; i++) { |
327 | 3.70M | int vidx = fs->nactvar++; |
328 | 3.70M | Vardesc *var = getlocalvardesc(fs, vidx); |
329 | 3.70M | var->vd.ridx = cast_byte(reglevel++); |
330 | 3.70M | var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); |
331 | 3.70M | luaY_checklimit(fs, reglevel, MAXVARS, "local variables"); |
332 | 3.70M | } |
333 | 2.73M | } |
334 | | |
335 | | |
336 | | /* |
337 | | ** Close the scope for all variables up to level 'tolevel'. |
338 | | ** (debug info.) |
339 | | */ |
340 | 5.37M | static void removevars (FuncState *fs, int tolevel) { |
341 | 5.37M | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); |
342 | 7.78M | while (fs->nactvar > tolevel) { |
343 | 2.40M | LocVar *var = localdebuginfo(fs, --fs->nactvar); |
344 | 2.40M | if (var) /* does it have debug information? */ |
345 | 2.25M | var->endpc = fs->pc; |
346 | 2.40M | } |
347 | 5.37M | } |
348 | | |
349 | | |
350 | | /* |
351 | | ** Search the upvalues of the function 'fs' for one |
352 | | ** with the given 'name'. |
353 | | */ |
354 | 413M | static int searchupvalue (FuncState *fs, TString *name) { |
355 | 413M | int i; |
356 | 413M | Upvaldesc *up = fs->f->upvalues; |
357 | 816M | for (i = 0; i < fs->nups; i++) { |
358 | 506M | if (eqstr(up[i].name, name)) return i; |
359 | 506M | } |
360 | 310M | return -1; /* not found */ |
361 | 413M | } |
362 | | |
363 | | |
364 | 9.18M | static Upvaldesc *allocupvalue (FuncState *fs) { |
365 | 9.18M | Proto *f = fs->f; |
366 | 9.18M | int oldsize = f->sizeupvalues; |
367 | 9.18M | luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); |
368 | 9.18M | luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, |
369 | 9.18M | Upvaldesc, MAXUPVAL, "upvalues"); |
370 | 44.5M | while (oldsize < f->sizeupvalues) |
371 | 35.3M | f->upvalues[oldsize++].name = NULL; |
372 | 9.18M | return &f->upvalues[fs->nups++]; |
373 | 9.18M | } |
374 | | |
375 | | |
376 | 1.25M | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { |
377 | 1.25M | Upvaldesc *up = allocupvalue(fs); |
378 | 1.25M | FuncState *prev = fs->prev; |
379 | 1.25M | if (v->k == VLOCAL) { |
380 | 348k | up->instack = 1; |
381 | 348k | up->idx = v->u.var.ridx; |
382 | 348k | up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; |
383 | 348k | lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); |
384 | 348k | } |
385 | 905k | else { |
386 | 905k | up->instack = 0; |
387 | 905k | up->idx = cast_byte(v->u.info); |
388 | 905k | up->kind = prev->f->upvalues[v->u.info].kind; |
389 | 905k | lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); |
390 | 905k | } |
391 | 1.25M | up->name = name; |
392 | 1.25M | luaC_objbarrier(fs->ls->L, fs->f, name); |
393 | 1.25M | return fs->nups - 1; |
394 | 1.25M | } |
395 | | |
396 | | |
397 | | /* |
398 | | ** Look for an active variable with the name 'n' in the |
399 | | ** function 'fs'. If found, initialize 'var' with it and return |
400 | | ** its expression kind; otherwise return -1. While searching, |
401 | | ** var->u.info==-1 means that the preambular global declaration is |
402 | | ** active (the default while there is no other global declaration); |
403 | | ** var->u.info==-2 means there is no active collective declaration |
404 | | ** (some previous global declaration but no collective declaration); |
405 | | ** and var->u.info>=0 points to the inner-most (the first one found) |
406 | | ** collective declaration, if there is one. |
407 | | */ |
408 | 474M | static int searchvar (FuncState *fs, TString *n, expdesc *var) { |
409 | 474M | int i; |
410 | 1.78G | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { |
411 | 1.36G | Vardesc *vd = getlocalvardesc(fs, i); |
412 | 1.36G | if (varglobal(vd)) { /* global declaration? */ |
413 | 17.3M | if (vd->vd.name == NULL) { /* collective declaration? */ |
414 | 1.02M | if (var->u.info < 0) /* no previous collective declaration? */ |
415 | 74.6k | var->u.info = fs->firstlocal + i; /* this is the first one */ |
416 | 1.02M | } |
417 | 16.3M | else { /* global name */ |
418 | 16.3M | if (eqstr(n, vd->vd.name)) { /* found? */ |
419 | 10.4k | init_exp(var, VGLOBAL, fs->firstlocal + i); |
420 | 10.4k | return VGLOBAL; |
421 | 10.4k | } |
422 | 16.3M | else if (var->u.info == -1) /* active preambular declaration? */ |
423 | 29.2k | var->u.info = -2; /* invalidate preambular declaration */ |
424 | 16.3M | } |
425 | 17.3M | } |
426 | 1.34G | else if (eqstr(n, vd->vd.name)) { /* found? */ |
427 | 60.6M | if (vd->vd.kind == RDKCTC) /* compile-time constant? */ |
428 | 53.7M | init_exp(var, VCONST, fs->firstlocal + i); |
429 | 6.94M | else /* local variable */ |
430 | 6.94M | init_var(fs, var, i); |
431 | 60.6M | return cast_int(var->k); |
432 | 60.6M | } |
433 | 1.36G | } |
434 | 413M | return -1; /* not found */ |
435 | 474M | } |
436 | | |
437 | | |
438 | | /* |
439 | | ** Mark block where variable at given level was defined |
440 | | ** (to emit close instructions later). |
441 | | */ |
442 | 348k | static void markupval (FuncState *fs, int level) { |
443 | 348k | BlockCnt *bl = fs->bl; |
444 | 365k | while (bl->nactvar > level) |
445 | 16.8k | bl = bl->previous; |
446 | 348k | bl->upval = 1; |
447 | 348k | fs->needclose = 1; |
448 | 348k | } |
449 | | |
450 | | |
451 | | /* |
452 | | ** Mark that current block has a to-be-closed variable. |
453 | | */ |
454 | 54.7k | static void marktobeclosed (FuncState *fs) { |
455 | 54.7k | BlockCnt *bl = fs->bl; |
456 | 54.7k | bl->upval = 1; |
457 | 54.7k | bl->insidetbc = 1; |
458 | 54.7k | fs->needclose = 1; |
459 | 54.7k | } |
460 | | |
461 | | |
462 | | /* |
463 | | ** Find a variable with the given name 'n'. If it is an upvalue, add |
464 | | ** this upvalue into all intermediate functions. If it is a global, set |
465 | | ** 'var' as 'void' as a flag. |
466 | | */ |
467 | 474M | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
468 | 474M | int v = searchvar(fs, n, var); /* look up variables at current level */ |
469 | 474M | if (v >= 0) { /* found? */ |
470 | 60.6M | if (v == VLOCAL && !base) |
471 | 348k | markupval(fs, var->u.var.vidx); /* local will be used as an upval */ |
472 | 60.6M | } |
473 | 413M | else { /* not found at current level; try upvalues */ |
474 | 413M | int idx = searchupvalue(fs, n); /* try existing upvalues */ |
475 | 413M | if (idx < 0) { /* not found? */ |
476 | 310M | if (fs->prev != NULL) /* more levels? */ |
477 | 178M | singlevaraux(fs->prev, n, var, 0); /* try upper levels */ |
478 | 310M | if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ |
479 | 1.25M | idx = newupvalue(fs, n, var); /* will be a new upvalue */ |
480 | 309M | else /* it is a global or a constant */ |
481 | 309M | return; /* don't need to do anything at this level */ |
482 | 310M | } |
483 | 104M | init_exp(var, VUPVAL, idx); /* new or old upvalue */ |
484 | 104M | } |
485 | 474M | } |
486 | | |
487 | | |
488 | 131M | static void buildglobal (LexState *ls, TString *varname, expdesc *var) { |
489 | 131M | FuncState *fs = ls->fs; |
490 | 131M | expdesc key; |
491 | 131M | init_exp(var, VGLOBAL, -1); /* global by default */ |
492 | 131M | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ |
493 | 131M | if (var->k == VGLOBAL) |
494 | 1.50k | luaK_semerror(ls, "_ENV is global when accessing variable '%s'", |
495 | 1.50k | getstr(varname)); |
496 | 131M | luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ |
497 | 131M | codestring(&key, varname); /* key is variable name */ |
498 | 131M | luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ |
499 | 131M | } |
500 | | |
501 | | |
502 | | /* |
503 | | ** Find a variable with the given name 'n', handling global variables |
504 | | ** too. |
505 | | */ |
506 | 164M | static void buildvar (LexState *ls, TString *varname, expdesc *var) { |
507 | 164M | FuncState *fs = ls->fs; |
508 | 164M | init_exp(var, VGLOBAL, -1); /* global by default */ |
509 | 164M | singlevaraux(fs, varname, var, 1); |
510 | 164M | if (var->k == VGLOBAL) { /* global name? */ |
511 | 131M | int info = var->u.info; |
512 | | /* global by default in the scope of a global declaration? */ |
513 | 131M | if (info == -2) |
514 | 2.60k | luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); |
515 | 131M | buildglobal(ls, varname, var); |
516 | 131M | if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) |
517 | 11.4k | var->u.ind.ro = 1; /* mark variable as read-only */ |
518 | 131M | else /* anyway must be a global */ |
519 | 131M | lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG); |
520 | 131M | } |
521 | 164M | } |
522 | | |
523 | | |
524 | 164M | static void singlevar (LexState *ls, expdesc *var) { |
525 | 164M | buildvar(ls, str_checkname(ls), var); |
526 | 164M | } |
527 | | |
528 | | |
529 | | /* |
530 | | ** Adjust the number of results from an expression list 'e' with 'nexps' |
531 | | ** expressions to 'nvars' values. |
532 | | */ |
533 | 1.21M | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { |
534 | 1.21M | FuncState *fs = ls->fs; |
535 | 1.21M | int needed = nvars - nexps; /* extra values needed */ |
536 | 1.21M | if (hasmultret(e->k)) { /* last expression has multiple returns? */ |
537 | 346k | int extra = needed + 1; /* discount last expression itself */ |
538 | 346k | if (extra < 0) |
539 | 10.1k | extra = 0; |
540 | 346k | luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ |
541 | 346k | } |
542 | 871k | else { |
543 | 871k | if (e->k != VVOID) /* at least one expression? */ |
544 | 793k | luaK_exp2nextreg(fs, e); /* close last expression */ |
545 | 871k | if (needed > 0) /* missing values? */ |
546 | 293k | luaK_nil(fs, fs->freereg, needed); /* complete with nils */ |
547 | 871k | } |
548 | 1.21M | if (needed > 0) |
549 | 449k | luaK_reserveregs(fs, needed); /* registers for extra values */ |
550 | 767k | else /* adding 'needed' is actually a subtraction */ |
551 | 767k | fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */ |
552 | 1.21M | } |
553 | | |
554 | | |
555 | 201M | #define enterlevel(ls) luaE_incCstack(ls->L) |
556 | | |
557 | | |
558 | 196M | #define leavelevel(ls) ((ls)->L->nCcalls--) |
559 | | |
560 | | |
561 | | /* |
562 | | ** Generates an error that a goto jumps into the scope of some |
563 | | ** variable declaration. |
564 | | */ |
565 | 8 | static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { |
566 | 8 | TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; |
567 | 8 | const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; |
568 | 8 | luaK_semerror(ls, |
569 | 8 | "<goto %s> at line %d jumps into the scope of '%s'", |
570 | 8 | getstr(gt->name), gt->line, varname); /* raise the error */ |
571 | 8 | } |
572 | | |
573 | | |
574 | | /* |
575 | | ** Closes the goto at index 'g' to given 'label' and removes it |
576 | | ** from the list of pending gotos. |
577 | | ** If it jumps into the scope of some variable, raises an error. |
578 | | ** The goto needs a CLOSE if it jumps out of a block with upvalues, |
579 | | ** or out of the scope of some variable and the block has upvalues |
580 | | ** (signaled by parameter 'bup'). |
581 | | */ |
582 | 209k | static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { |
583 | 209k | int i; |
584 | 209k | FuncState *fs = ls->fs; |
585 | 209k | Labellist *gl = &ls->dyd->gt; /* list of gotos */ |
586 | 209k | Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ |
587 | 209k | lua_assert(eqstr(gt->name, label->name)); |
588 | 209k | if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ |
589 | 8 | jumpscopeerror(ls, gt); |
590 | 209k | if (gt->close || |
591 | 209k | (label->nactvar < gt->nactvar && bup)) { /* needs close? */ |
592 | 29.6k | lu_byte stklevel = reglevel(fs, label->nactvar); |
593 | | /* move jump to CLOSE position */ |
594 | 29.6k | fs->f->code[gt->pc + 1] = fs->f->code[gt->pc]; |
595 | | /* put CLOSE instruction at original position */ |
596 | 29.6k | fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0); |
597 | 29.6k | gt->pc++; /* must point to jump instruction */ |
598 | 29.6k | } |
599 | 209k | luaK_patchlist(ls->fs, gt->pc, label->pc); /* goto jumps to label */ |
600 | 471M | for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ |
601 | 471M | gl->arr[i] = gl->arr[i + 1]; |
602 | 209k | gl->n--; |
603 | 209k | } |
604 | | |
605 | | |
606 | | /* |
607 | | ** Search for an active label with the given name, starting at |
608 | | ** index 'ilb' (so that it can search for all labels in current block |
609 | | ** or all labels in current function). |
610 | | */ |
611 | 605k | static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { |
612 | 605k | Dyndata *dyd = ls->dyd; |
613 | 718k | for (; ilb < dyd->label.n; ilb++) { |
614 | 324k | Labeldesc *lb = &dyd->label.arr[ilb]; |
615 | 324k | if (eqstr(lb->name, name)) /* correct label? */ |
616 | 211k | return lb; |
617 | 324k | } |
618 | 394k | return NULL; /* label not found */ |
619 | 605k | } |
620 | | |
621 | | |
622 | | /* |
623 | | ** Adds a new label/goto in the corresponding list. |
624 | | */ |
625 | | static int newlabelentry (LexState *ls, Labellist *l, TString *name, |
626 | 569k | int line, int pc) { |
627 | 569k | int n = l->n; |
628 | 569k | luaM_growvector(ls->L, l->arr, n, l->size, |
629 | 569k | Labeldesc, SHRT_MAX, "labels/gotos"); |
630 | 569k | l->arr[n].name = name; |
631 | 569k | l->arr[n].line = line; |
632 | 569k | l->arr[n].nactvar = ls->fs->nactvar; |
633 | 569k | l->arr[n].close = 0; |
634 | 569k | l->arr[n].pc = pc; |
635 | 569k | l->n = n + 1; |
636 | 569k | return n; |
637 | 569k | } |
638 | | |
639 | | |
640 | | /* |
641 | | ** Create an entry for the goto and the code for it. As it is not known |
642 | | ** at this point whether the goto may need a CLOSE, the code has a jump |
643 | | ** followed by an CLOSE. (As the CLOSE comes after the jump, it is a |
644 | | ** dead instruction; it works as a placeholder.) When the goto is closed |
645 | | ** against a label, if it needs a CLOSE, the two instructions swap |
646 | | ** positions, so that the CLOSE comes before the jump. |
647 | | */ |
648 | 380k | static int newgotoentry (LexState *ls, TString *name, int line) { |
649 | 380k | FuncState *fs = ls->fs; |
650 | 380k | int pc = luaK_jump(fs); /* create jump */ |
651 | 380k | luaK_codeABC(fs, OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */ |
652 | 380k | return newlabelentry(ls, &ls->dyd->gt, name, line, pc); |
653 | 380k | } |
654 | | |
655 | | |
656 | | /* |
657 | | ** Create a new label with the given 'name' at the given 'line'. |
658 | | ** 'last' tells whether label is the last non-op statement in its |
659 | | ** block. Solves all pending gotos to this new label and adds |
660 | | ** a close instruction if necessary. |
661 | | ** Returns true iff it added a close instruction. |
662 | | */ |
663 | 189k | static void createlabel (LexState *ls, TString *name, int line, int last) { |
664 | 189k | FuncState *fs = ls->fs; |
665 | 189k | Labellist *ll = &ls->dyd->label; |
666 | 189k | int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); |
667 | 189k | if (last) { /* label is last no-op statement in the block? */ |
668 | | /* assume that locals are already out of scope */ |
669 | 6.70k | ll->arr[l].nactvar = fs->bl->nactvar; |
670 | 6.70k | } |
671 | 189k | } |
672 | | |
673 | | |
674 | | /* |
675 | | ** Traverse the pending gotos of the finishing block checking whether |
676 | | ** each match some label of that block. Those that do not match are |
677 | | ** "exported" to the outer block, to be solved there. In particular, |
678 | | ** its 'nactvar' is updated with the level of the inner block, |
679 | | ** as the variables of the inner block are now out of scope. |
680 | | */ |
681 | 5.37M | static void solvegotos (FuncState *fs, BlockCnt *bl) { |
682 | 5.37M | LexState *ls = fs->ls; |
683 | 5.37M | Labellist *gl = &ls->dyd->gt; |
684 | 5.37M | int outlevel = reglevel(fs, bl->nactvar); /* level outside the block */ |
685 | 5.37M | int igt = bl->firstgoto; /* first goto in the finishing block */ |
686 | 5.90M | while (igt < gl->n) { /* for each pending goto */ |
687 | 527k | Labeldesc *gt = &gl->arr[igt]; |
688 | | /* search for a matching label in the current block */ |
689 | 527k | Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel); |
690 | 527k | if (lb != NULL) /* found a match? */ |
691 | 209k | closegoto(ls, igt, lb, bl->upval); /* close and remove goto */ |
692 | 318k | else { /* adjust 'goto' for outer block */ |
693 | | /* block has variables to be closed and goto escapes the scope of |
694 | | some variable? */ |
695 | 318k | if (bl->upval && reglevel(fs, gt->nactvar) > outlevel) |
696 | 6.32k | gt->close = 1; /* jump may need a close */ |
697 | 318k | gt->nactvar = bl->nactvar; /* correct level for outer block */ |
698 | 318k | igt++; /* go to next goto */ |
699 | 318k | } |
700 | 527k | } |
701 | 5.37M | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ |
702 | 5.37M | } |
703 | | |
704 | | |
705 | 10.6M | static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { |
706 | 10.6M | bl->isloop = isloop; |
707 | 10.6M | bl->nactvar = fs->nactvar; |
708 | 10.6M | bl->firstlabel = fs->ls->dyd->label.n; |
709 | 10.6M | bl->firstgoto = fs->ls->dyd->gt.n; |
710 | 10.6M | bl->upval = 0; |
711 | | /* inherit 'insidetbc' from enclosing block */ |
712 | 10.6M | bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); |
713 | 10.6M | bl->previous = fs->bl; /* link block in function's block list */ |
714 | 10.6M | fs->bl = bl; |
715 | 10.6M | lua_assert(fs->freereg == luaY_nvarstack(fs)); |
716 | 10.6M | } |
717 | | |
718 | | |
719 | | /* |
720 | | ** generates an error for an undefined 'goto'. |
721 | | */ |
722 | 491 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { |
723 | | /* breaks are checked when created, cannot be undefined */ |
724 | 491 | lua_assert(!eqstr(gt->name, ls->brkn)); |
725 | 491 | luaK_semerror(ls, "no visible label '%s' for <goto> at line %d", |
726 | 491 | getstr(gt->name), gt->line); |
727 | 491 | } |
728 | | |
729 | | |
730 | 5.37M | static void leaveblock (FuncState *fs) { |
731 | 5.37M | BlockCnt *bl = fs->bl; |
732 | 5.37M | LexState *ls = fs->ls; |
733 | 5.37M | lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside block */ |
734 | 5.37M | if (bl->previous && bl->upval) /* need a 'close'? */ |
735 | 45.6k | luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); |
736 | 5.37M | fs->freereg = stklevel; /* free registers */ |
737 | 5.37M | removevars(fs, bl->nactvar); /* remove block locals */ |
738 | 5.37M | lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ |
739 | 5.37M | if (bl->isloop == 2) /* has to fix pending breaks? */ |
740 | 113k | createlabel(ls, ls->brkn, 0, 0); |
741 | 5.37M | solvegotos(fs, bl); |
742 | 5.37M | if (bl->previous == NULL) { /* was it the last block? */ |
743 | 4.56M | if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ |
744 | 491 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ |
745 | 4.56M | } |
746 | 5.37M | fs->bl = bl->previous; /* current block now is previous one */ |
747 | 5.37M | } |
748 | | |
749 | | |
750 | | /* |
751 | | ** adds a new prototype into list of prototypes |
752 | | */ |
753 | 1.69M | static Proto *addprototype (LexState *ls) { |
754 | 1.69M | Proto *clp; |
755 | 1.69M | lua_State *L = ls->L; |
756 | 1.69M | FuncState *fs = ls->fs; |
757 | 1.69M | Proto *f = fs->f; /* prototype of current function */ |
758 | 1.69M | if (fs->np >= f->sizep) { |
759 | 604k | int oldsize = f->sizep; |
760 | 604k | luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); |
761 | 4.34M | while (oldsize < f->sizep) |
762 | 3.74M | f->p[oldsize++] = NULL; |
763 | 604k | } |
764 | 1.69M | f->p[fs->np++] = clp = luaF_newproto(L); |
765 | 1.69M | luaC_objbarrier(L, f, clp); |
766 | 1.69M | return clp; |
767 | 1.69M | } |
768 | | |
769 | | |
770 | | /* |
771 | | ** codes instruction to create new closure in parent function. |
772 | | ** The OP_CLOSURE instruction uses the last available register, |
773 | | ** so that, if it invokes the GC, the GC knows which registers |
774 | | ** are in use at that time. |
775 | | |
776 | | */ |
777 | 1.26M | static void codeclosure (LexState *ls, expdesc *v) { |
778 | 1.26M | FuncState *fs = ls->fs->prev; |
779 | 1.26M | init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); |
780 | 1.26M | luaK_exp2nextreg(fs, v); /* fix it at the last register */ |
781 | 1.26M | } |
782 | | |
783 | | |
784 | 9.62M | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { |
785 | 9.62M | lua_State *L = ls->L; |
786 | 9.62M | Proto *f = fs->f; |
787 | 9.62M | fs->prev = ls->fs; /* linked list of funcstates */ |
788 | 9.62M | fs->ls = ls; |
789 | 9.62M | ls->fs = fs; |
790 | 9.62M | fs->pc = 0; |
791 | 9.62M | fs->previousline = f->linedefined; |
792 | 9.62M | fs->iwthabs = 0; |
793 | 9.62M | fs->lasttarget = 0; |
794 | 9.62M | fs->freereg = 0; |
795 | 9.62M | fs->nk = 0; |
796 | 9.62M | fs->nabslineinfo = 0; |
797 | 9.62M | fs->np = 0; |
798 | 9.62M | fs->nups = 0; |
799 | 9.62M | fs->ndebugvars = 0; |
800 | 9.62M | fs->nactvar = 0; |
801 | 9.62M | fs->needclose = 0; |
802 | 9.62M | fs->firstlocal = ls->dyd->actvar.n; |
803 | 9.62M | fs->firstlabel = ls->dyd->label.n; |
804 | 9.62M | fs->bl = NULL; |
805 | 9.62M | f->source = ls->source; |
806 | 9.62M | luaC_objbarrier(L, f, f->source); |
807 | 9.62M | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
808 | 9.62M | fs->kcache = luaH_new(L); /* create table for function */ |
809 | 9.62M | sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ |
810 | 9.62M | luaD_inctop(L); |
811 | 9.62M | enterblock(fs, bl, 0); |
812 | 9.62M | } |
813 | | |
814 | | |
815 | 4.56M | static void close_func (LexState *ls) { |
816 | 4.56M | lua_State *L = ls->L; |
817 | 4.56M | FuncState *fs = ls->fs; |
818 | 4.56M | Proto *f = fs->f; |
819 | 4.56M | luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ |
820 | 4.56M | leaveblock(fs); |
821 | 4.56M | lua_assert(fs->bl == NULL); |
822 | 4.56M | luaK_finish(fs); |
823 | 4.56M | luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); |
824 | 4.56M | luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); |
825 | 4.56M | luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, |
826 | 4.56M | fs->nabslineinfo, AbsLineInfo); |
827 | 4.56M | luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); |
828 | 4.56M | luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); |
829 | 4.56M | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); |
830 | 4.56M | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); |
831 | 4.56M | ls->fs = fs->prev; |
832 | 4.56M | L->top.p--; /* pop kcache table */ |
833 | 4.56M | luaC_checkGC(L); |
834 | 4.56M | } |
835 | | |
836 | | |
837 | | /* |
838 | | ** {====================================================================== |
839 | | ** GRAMMAR RULES |
840 | | ** ======================================================================= |
841 | | */ |
842 | | |
843 | | |
844 | | /* |
845 | | ** check whether current token is in the follow set of a block. |
846 | | ** 'until' closes syntactical blocks, but do not close scope, |
847 | | ** so it is handled in separate. |
848 | | */ |
849 | 16.6M | static int block_follow (LexState *ls, int withuntil) { |
850 | 16.6M | switch (ls->t.token) { |
851 | 25.5k | case TK_ELSE: case TK_ELSEIF: |
852 | 4.44M | case TK_END: case TK_EOS: |
853 | 4.44M | return 1; |
854 | 71.6k | case TK_UNTIL: return withuntil; |
855 | 12.1M | default: return 0; |
856 | 16.6M | } |
857 | 16.6M | } |
858 | | |
859 | | |
860 | 10.0M | static void statlist (LexState *ls) { |
861 | | /* statlist -> { stat [';'] } */ |
862 | 20.7M | while (!block_follow(ls, 1)) { |
863 | 11.3M | if (ls->t.token == TK_RETURN) { |
864 | 734k | statement(ls); |
865 | 734k | return; /* 'return' must be last statement */ |
866 | 734k | } |
867 | 10.6M | statement(ls); |
868 | 10.6M | } |
869 | 10.0M | } |
870 | | |
871 | | |
872 | 4.46M | static void fieldsel (LexState *ls, expdesc *v) { |
873 | | /* fieldsel -> ['.' | ':'] NAME */ |
874 | 4.46M | FuncState *fs = ls->fs; |
875 | 4.46M | expdesc key; |
876 | 4.46M | luaK_exp2anyregup(fs, v); |
877 | 4.46M | luaX_next(ls); /* skip the dot or colon */ |
878 | 4.46M | codename(ls, &key); |
879 | 4.46M | luaK_indexed(fs, v, &key); |
880 | 4.46M | } |
881 | | |
882 | | |
883 | 411k | static void yindex (LexState *ls, expdesc *v) { |
884 | | /* index -> '[' expr ']' */ |
885 | 411k | luaX_next(ls); /* skip the '[' */ |
886 | 411k | expr(ls, v); |
887 | 411k | luaK_exp2val(ls->fs, v); |
888 | 411k | checknext(ls, ']'); |
889 | 411k | } |
890 | | |
891 | | |
892 | | /* |
893 | | ** {====================================================================== |
894 | | ** Rules for Constructors |
895 | | ** ======================================================================= |
896 | | */ |
897 | | |
898 | | typedef struct ConsControl { |
899 | | expdesc v; /* last list item read */ |
900 | | expdesc *t; /* table descriptor */ |
901 | | int nh; /* total number of 'record' elements */ |
902 | | int na; /* number of array elements already stored */ |
903 | | int tostore; /* number of array elements pending to be stored */ |
904 | | int maxtostore; /* maximum number of pending elements */ |
905 | | } ConsControl; |
906 | | |
907 | | |
908 | 3.25M | static void recfield (LexState *ls, ConsControl *cc) { |
909 | | /* recfield -> (NAME | '['exp']') = exp */ |
910 | 3.25M | FuncState *fs = ls->fs; |
911 | 3.25M | lu_byte reg = ls->fs->freereg; |
912 | 3.25M | expdesc tab, key, val; |
913 | 3.25M | if (ls->t.token == TK_NAME) |
914 | 3.11M | codename(ls, &key); |
915 | 139k | else /* ls->t.token == '[' */ |
916 | 139k | yindex(ls, &key); |
917 | 3.25M | cc->nh++; |
918 | 3.25M | checknext(ls, '='); |
919 | 3.25M | tab = *cc->t; |
920 | 3.25M | luaK_indexed(fs, &tab, &key); |
921 | 3.25M | expr(ls, &val); |
922 | 3.25M | luaK_storevar(fs, &tab, &val); |
923 | 3.25M | fs->freereg = reg; /* free registers */ |
924 | 3.25M | } |
925 | | |
926 | | |
927 | 10.7M | static void closelistfield (FuncState *fs, ConsControl *cc) { |
928 | 10.7M | if (cc->v.k == VVOID) return; /* there is no list item */ |
929 | 6.26M | luaK_exp2nextreg(fs, &cc->v); |
930 | 6.26M | cc->v.k = VVOID; |
931 | 6.26M | if (cc->tostore >= cc->maxtostore) { |
932 | 266k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ |
933 | 266k | cc->na += cc->tostore; |
934 | 266k | cc->tostore = 0; /* no more items pending */ |
935 | 266k | } |
936 | 6.26M | } |
937 | | |
938 | | |
939 | 456k | static void lastlistfield (FuncState *fs, ConsControl *cc) { |
940 | 456k | if (cc->tostore == 0) return; |
941 | 144k | if (hasmultret(cc->v.k)) { |
942 | 93.6k | luaK_setmultret(fs, &cc->v); |
943 | 93.6k | luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); |
944 | 93.6k | cc->na--; /* do not count last expression (unknown number of elements) */ |
945 | 93.6k | } |
946 | 50.8k | else { |
947 | 50.8k | if (cc->v.k != VVOID) |
948 | 47.8k | luaK_exp2nextreg(fs, &cc->v); |
949 | 50.8k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); |
950 | 50.8k | } |
951 | 144k | cc->na += cc->tostore; |
952 | 144k | } |
953 | | |
954 | | |
955 | 7.45M | static void listfield (LexState *ls, ConsControl *cc) { |
956 | | /* listfield -> exp */ |
957 | 7.45M | expr(ls, &cc->v); |
958 | 7.45M | cc->tostore++; |
959 | 7.45M | } |
960 | | |
961 | | |
962 | 10.7M | static void field (LexState *ls, ConsControl *cc) { |
963 | | /* field -> listfield | recfield */ |
964 | 10.7M | switch(ls->t.token) { |
965 | 9.72M | case TK_NAME: { /* may be 'listfield' or 'recfield' */ |
966 | 9.72M | if (luaX_lookahead(ls) != '=') /* expression? */ |
967 | 6.60M | listfield(ls, cc); |
968 | 3.12M | else |
969 | 3.12M | recfield(ls, cc); |
970 | 9.72M | break; |
971 | 0 | } |
972 | 139k | case '[': { |
973 | 139k | recfield(ls, cc); |
974 | 139k | break; |
975 | 0 | } |
976 | 855k | default: { |
977 | 855k | listfield(ls, cc); |
978 | 855k | break; |
979 | 0 | } |
980 | 10.7M | } |
981 | 10.7M | } |
982 | | |
983 | | |
984 | | /* |
985 | | ** Compute a limit for how many registers a constructor can use before |
986 | | ** emitting a 'SETLIST' instruction, based on how many registers are |
987 | | ** available. |
988 | | */ |
989 | 1.53M | static int maxtostore (FuncState *fs) { |
990 | 1.53M | int numfreeregs = MAX_FSTACK - fs->freereg; |
991 | 1.53M | if (numfreeregs >= 160) /* "lots" of registers? */ |
992 | 1.38M | return numfreeregs / 5; /* use up to 1/5 of them */ |
993 | 152k | else if (numfreeregs >= 80) /* still "enough" registers? */ |
994 | 113k | return 10; /* one 'SETLIST' instruction for each 10 values */ |
995 | 39.4k | else /* save registers for potential more nesting */ |
996 | 39.4k | return 1; |
997 | 1.53M | } |
998 | | |
999 | | |
1000 | 1.54M | static void constructor (LexState *ls, expdesc *t) { |
1001 | | /* constructor -> '{' [ field { sep field } [sep] ] '}' |
1002 | | sep -> ',' | ';' */ |
1003 | 1.54M | FuncState *fs = ls->fs; |
1004 | 1.54M | int line = ls->linenumber; |
1005 | 1.54M | int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0); |
1006 | 1.54M | ConsControl cc; |
1007 | 1.54M | luaK_code(fs, 0); /* space for extra arg. */ |
1008 | 1.54M | cc.na = cc.nh = cc.tostore = 0; |
1009 | 1.54M | cc.t = t; |
1010 | 1.54M | init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ |
1011 | 1.54M | luaK_reserveregs(fs, 1); |
1012 | 1.54M | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
1013 | 1.54M | checknext(ls, '{' /*}*/); |
1014 | 1.54M | cc.maxtostore = maxtostore(fs); |
1015 | 11.0M | do { |
1016 | 11.0M | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
1017 | 11.0M | if (ls->t.token == /*{*/ '}') break; |
1018 | 10.7M | closelistfield(fs, &cc); |
1019 | 10.7M | field(ls, &cc); |
1020 | 10.7M | } while (testnext(ls, ',') || testnext(ls, ';')); |
1021 | 1.53M | check_match(ls, /*{*/ '}', '{' /*}*/, line); |
1022 | 1.53M | lastlistfield(fs, &cc); |
1023 | 1.53M | luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); |
1024 | 1.53M | } |
1025 | | |
1026 | | /* }====================================================================== */ |
1027 | | |
1028 | | |
1029 | 7.93M | static void setvararg (FuncState *fs, int nparams) { |
1030 | 7.93M | fs->f->flag |= PF_ISVARARG; |
1031 | 7.93M | luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); |
1032 | 7.93M | } |
1033 | | |
1034 | | |
1035 | 1.69M | static void parlist (LexState *ls) { |
1036 | | /* parlist -> [ {NAME ','} (NAME | '...') ] */ |
1037 | 1.69M | FuncState *fs = ls->fs; |
1038 | 1.69M | Proto *f = fs->f; |
1039 | 1.69M | int nparams = 0; |
1040 | 1.69M | int isvararg = 0; |
1041 | 1.69M | if (ls->t.token != ')') { /* is 'parlist' not empty? */ |
1042 | 1.76M | do { |
1043 | 1.76M | switch (ls->t.token) { |
1044 | 1.75M | case TK_NAME: { |
1045 | 1.75M | new_localvar(ls, str_checkname(ls)); |
1046 | 1.75M | nparams++; |
1047 | 1.75M | break; |
1048 | 0 | } |
1049 | 5.78k | case TK_DOTS: { |
1050 | 5.78k | luaX_next(ls); |
1051 | 5.78k | isvararg = 1; |
1052 | 5.78k | break; |
1053 | 0 | } |
1054 | 1.87k | default: luaX_syntaxerror(ls, "<name> or '...' expected"); |
1055 | 1.76M | } |
1056 | 1.76M | } while (!isvararg && testnext(ls, ',')); |
1057 | 817k | } |
1058 | 1.69M | adjustlocalvars(ls, nparams); |
1059 | 1.69M | f->numparams = cast_byte(fs->nactvar); |
1060 | 1.69M | if (isvararg) |
1061 | 5.78k | setvararg(fs, f->numparams); /* declared vararg */ |
1062 | 1.69M | luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ |
1063 | 1.69M | } |
1064 | | |
1065 | | |
1066 | 1.69M | static void body (LexState *ls, expdesc *e, int ismethod, int line) { |
1067 | | /* body -> '(' parlist ')' block END */ |
1068 | 1.69M | FuncState new_fs; |
1069 | 1.69M | BlockCnt bl; |
1070 | 1.69M | new_fs.f = addprototype(ls); |
1071 | 1.69M | new_fs.f->linedefined = line; |
1072 | 1.69M | open_func(ls, &new_fs, &bl); |
1073 | 1.69M | checknext(ls, '('); |
1074 | 1.69M | if (ismethod) { |
1075 | 9.53k | new_localvarliteral(ls, "self"); /* create 'self' parameter */ |
1076 | 9.53k | adjustlocalvars(ls, 1); |
1077 | 9.53k | } |
1078 | 1.69M | parlist(ls); |
1079 | 1.69M | checknext(ls, ')'); |
1080 | 1.69M | statlist(ls); |
1081 | 1.69M | new_fs.f->lastlinedefined = ls->linenumber; |
1082 | 1.69M | check_match(ls, TK_END, TK_FUNCTION, line); |
1083 | 1.69M | codeclosure(ls, e); |
1084 | 1.69M | close_func(ls); |
1085 | 1.69M | } |
1086 | | |
1087 | | |
1088 | 6.34M | static int explist (LexState *ls, expdesc *v) { |
1089 | | /* explist -> expr { ',' expr } */ |
1090 | 6.34M | int n = 1; /* at least one expression */ |
1091 | 6.34M | expr(ls, v); |
1092 | 8.45M | while (testnext(ls, ',')) { |
1093 | 2.11M | luaK_exp2nextreg(ls->fs, v); |
1094 | 2.11M | expr(ls, v); |
1095 | 2.11M | n++; |
1096 | 2.11M | } |
1097 | 6.34M | return n; |
1098 | 6.34M | } |
1099 | | |
1100 | | |
1101 | 4.50M | static void funcargs (LexState *ls, expdesc *f) { |
1102 | 4.50M | FuncState *fs = ls->fs; |
1103 | 4.50M | expdesc args; |
1104 | 4.50M | int base, nparams; |
1105 | 4.50M | int line = ls->linenumber; |
1106 | 4.50M | switch (ls->t.token) { |
1107 | 2.87M | case '(': { /* funcargs -> '(' [ explist ] ')' */ |
1108 | 2.87M | luaX_next(ls); |
1109 | 2.87M | if (ls->t.token == ')') /* arg list is empty? */ |
1110 | 235k | args.k = VVOID; |
1111 | 2.63M | else { |
1112 | 2.63M | explist(ls, &args); |
1113 | 2.63M | if (hasmultret(args.k)) |
1114 | 158k | luaK_setmultret(fs, &args); |
1115 | 2.63M | } |
1116 | 2.87M | check_match(ls, ')', '(', line); |
1117 | 2.87M | break; |
1118 | 0 | } |
1119 | 513k | case '{' /*}*/: { /* funcargs -> constructor */ |
1120 | 513k | constructor(ls, &args); |
1121 | 513k | break; |
1122 | 0 | } |
1123 | 1.10M | case TK_STRING: { /* funcargs -> STRING */ |
1124 | 1.10M | codestring(&args, ls->t.seminfo.ts); |
1125 | 1.10M | luaX_next(ls); /* must use 'seminfo' before 'next' */ |
1126 | 1.10M | break; |
1127 | 0 | } |
1128 | 12.8k | default: { |
1129 | 12.8k | luaX_syntaxerror(ls, "function arguments expected"); |
1130 | 0 | } |
1131 | 4.50M | } |
1132 | 3.52M | lua_assert(f->k == VNONRELOC); |
1133 | 3.52M | base = f->u.info; /* base register for call */ |
1134 | 3.52M | if (hasmultret(args.k)) |
1135 | 155k | nparams = LUA_MULTRET; /* open call */ |
1136 | 3.36M | else { |
1137 | 3.36M | if (args.k != VVOID) |
1138 | 3.13M | luaK_exp2nextreg(fs, &args); /* close last argument */ |
1139 | 3.36M | nparams = fs->freereg - (base+1); |
1140 | 3.36M | } |
1141 | 3.52M | init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); |
1142 | 3.52M | luaK_fixline(fs, line); |
1143 | | /* call removes function and arguments and leaves one result (unless |
1144 | | changed later) */ |
1145 | 3.52M | fs->freereg = cast_byte(base + 1); |
1146 | 3.52M | } |
1147 | | |
1148 | | |
1149 | | |
1150 | | |
1151 | | /* |
1152 | | ** {====================================================================== |
1153 | | ** Expression parsing |
1154 | | ** ======================================================================= |
1155 | | */ |
1156 | | |
1157 | | |
1158 | 167M | static void primaryexp (LexState *ls, expdesc *v) { |
1159 | | /* primaryexp -> NAME | '(' expr ')' */ |
1160 | 167M | switch (ls->t.token) { |
1161 | 978k | case '(': { |
1162 | 978k | int line = ls->linenumber; |
1163 | 978k | luaX_next(ls); |
1164 | 978k | expr(ls, v); |
1165 | 978k | check_match(ls, ')', '(', line); |
1166 | 978k | luaK_dischargevars(ls->fs, v); |
1167 | 978k | return; |
1168 | 0 | } |
1169 | 164M | case TK_NAME: { |
1170 | 164M | singlevar(ls, v); |
1171 | 164M | return; |
1172 | 0 | } |
1173 | 2.73M | default: { |
1174 | 2.73M | luaX_syntaxerror(ls, "unexpected symbol"); |
1175 | 0 | } |
1176 | 167M | } |
1177 | 167M | } |
1178 | | |
1179 | | |
1180 | 167M | static void suffixedexp (LexState *ls, expdesc *v) { |
1181 | | /* suffixedexp -> |
1182 | | primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ |
1183 | 167M | FuncState *fs = ls->fs; |
1184 | 167M | primaryexp(ls, v); |
1185 | 172M | for (;;) { |
1186 | 172M | switch (ls->t.token) { |
1187 | 4.44M | case '.': { /* fieldsel */ |
1188 | 4.44M | fieldsel(ls, v); |
1189 | 4.44M | break; |
1190 | 0 | } |
1191 | 271k | case '[': { /* '[' exp ']' */ |
1192 | 271k | expdesc key; |
1193 | 271k | luaK_exp2anyregup(fs, v); |
1194 | 271k | yindex(ls, &key); |
1195 | 271k | luaK_indexed(fs, v, &key); |
1196 | 271k | break; |
1197 | 0 | } |
1198 | 332k | case ':': { /* ':' NAME funcargs */ |
1199 | 332k | expdesc key; |
1200 | 332k | luaX_next(ls); |
1201 | 332k | codename(ls, &key); |
1202 | 332k | luaK_self(fs, v, &key); |
1203 | 332k | funcargs(ls, v); |
1204 | 332k | break; |
1205 | 0 | } |
1206 | 4.24M | case '(': case TK_STRING: case '{' /*}*/: { /* funcargs */ |
1207 | 4.24M | luaK_exp2nextreg(fs, v); |
1208 | 4.24M | funcargs(ls, v); |
1209 | 4.24M | break; |
1210 | 3.74M | } |
1211 | 163M | default: return; |
1212 | 172M | } |
1213 | 172M | } |
1214 | 167M | } |
1215 | | |
1216 | | |
1217 | 174M | static void simpleexp (LexState *ls, expdesc *v) { |
1218 | | /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | |
1219 | | constructor | FUNCTION body | suffixedexp */ |
1220 | 174M | switch (ls->t.token) { |
1221 | 2.16M | case TK_FLT: { |
1222 | 2.16M | init_exp(v, VKFLT, 0); |
1223 | 2.16M | v->u.nval = ls->t.seminfo.r; |
1224 | 2.16M | break; |
1225 | 0 | } |
1226 | 11.5M | case TK_INT: { |
1227 | 11.5M | init_exp(v, VKINT, 0); |
1228 | 11.5M | v->u.ival = ls->t.seminfo.i; |
1229 | 11.5M | break; |
1230 | 0 | } |
1231 | 1.12M | case TK_STRING: { |
1232 | 1.12M | codestring(v, ls->t.seminfo.ts); |
1233 | 1.12M | break; |
1234 | 0 | } |
1235 | 241k | case TK_NIL: { |
1236 | 241k | init_exp(v, VNIL, 0); |
1237 | 241k | break; |
1238 | 0 | } |
1239 | 95.3k | case TK_TRUE: { |
1240 | 95.3k | init_exp(v, VTRUE, 0); |
1241 | 95.3k | break; |
1242 | 0 | } |
1243 | 33.5k | case TK_FALSE: { |
1244 | 33.5k | init_exp(v, VFALSE, 0); |
1245 | 33.5k | break; |
1246 | 0 | } |
1247 | 291k | case TK_DOTS: { /* vararg */ |
1248 | 291k | FuncState *fs = ls->fs; |
1249 | 291k | check_condition(ls, fs->f->flag & PF_ISVARARG, |
1250 | 291k | "cannot use '...' outside a vararg function"); |
1251 | 290k | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); |
1252 | 290k | break; |
1253 | 291k | } |
1254 | 1.02M | case '{' /*}*/: { /* constructor */ |
1255 | 1.02M | constructor(ls, v); |
1256 | 1.02M | return; |
1257 | 291k | } |
1258 | 1.58M | case TK_FUNCTION: { |
1259 | 1.58M | luaX_next(ls); |
1260 | 1.58M | body(ls, v, 0, ls->linenumber); |
1261 | 1.58M | return; |
1262 | 291k | } |
1263 | 156M | default: { |
1264 | 156M | suffixedexp(ls, v); |
1265 | 156M | return; |
1266 | 291k | } |
1267 | 174M | } |
1268 | 15.5M | luaX_next(ls); |
1269 | 15.5M | } |
1270 | | |
1271 | | |
1272 | 185M | static UnOpr getunopr (int op) { |
1273 | 185M | switch (op) { |
1274 | 145k | case TK_NOT: return OPR_NOT; |
1275 | 1.50M | case '-': return OPR_MINUS; |
1276 | 9.11M | case '~': return OPR_BNOT; |
1277 | 247k | case '#': return OPR_LEN; |
1278 | 174M | default: return OPR_NOUNOPR; |
1279 | 185M | } |
1280 | 185M | } |
1281 | | |
1282 | | |
1283 | 182M | static BinOpr getbinopr (int op) { |
1284 | 182M | switch (op) { |
1285 | 496k | case '+': return OPR_ADD; |
1286 | 2.20M | case '-': return OPR_SUB; |
1287 | 6.92M | case '*': return OPR_MUL; |
1288 | 1.15M | case '%': return OPR_MOD; |
1289 | 3.38M | case '^': return OPR_POW; |
1290 | 8.36M | case '/': return OPR_DIV; |
1291 | 552k | case TK_IDIV: return OPR_IDIV; |
1292 | 105k | case '&': return OPR_BAND; |
1293 | 790k | case '|': return OPR_BOR; |
1294 | 3.99M | case '~': return OPR_BXOR; |
1295 | 918k | case TK_SHL: return OPR_SHL; |
1296 | 1.44M | case TK_SHR: return OPR_SHR; |
1297 | 1.34M | case TK_CONCAT: return OPR_CONCAT; |
1298 | 113k | case TK_NE: return OPR_NE; |
1299 | 487k | case TK_EQ: return OPR_EQ; |
1300 | 10.4M | case '<': return OPR_LT; |
1301 | 324k | case TK_LE: return OPR_LE; |
1302 | 118M | case '>': return OPR_GT; |
1303 | 43.2k | case TK_GE: return OPR_GE; |
1304 | 649k | case TK_AND: return OPR_AND; |
1305 | 774k | case TK_OR: return OPR_OR; |
1306 | 19.2M | default: return OPR_NOBINOPR; |
1307 | 182M | } |
1308 | 182M | } |
1309 | | |
1310 | | |
1311 | | /* |
1312 | | ** Priority table for binary operators. |
1313 | | */ |
1314 | | static const struct { |
1315 | | lu_byte left; /* left priority for each binary operator */ |
1316 | | lu_byte right; /* right priority */ |
1317 | | } priority[] = { /* ORDER OPR */ |
1318 | | {10, 10}, {10, 10}, /* '+' '-' */ |
1319 | | {11, 11}, {11, 11}, /* '*' '%' */ |
1320 | | {14, 13}, /* '^' (right associative) */ |
1321 | | {11, 11}, {11, 11}, /* '/' '//' */ |
1322 | | {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ |
1323 | | {7, 7}, {7, 7}, /* '<<' '>>' */ |
1324 | | {9, 8}, /* '..' (right associative) */ |
1325 | | {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ |
1326 | | {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ |
1327 | | {2, 2}, {1, 1} /* and, or */ |
1328 | | }; |
1329 | | |
1330 | 11.0M | #define UNARY_PRIORITY 12 /* priority for unary operators */ |
1331 | | |
1332 | | |
1333 | | /* |
1334 | | ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } |
1335 | | ** where 'binop' is any binary operator with a priority higher than 'limit' |
1336 | | */ |
1337 | 185M | static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { |
1338 | 185M | BinOpr op; |
1339 | 185M | UnOpr uop; |
1340 | 185M | enterlevel(ls); |
1341 | 185M | uop = getunopr(ls->t.token); |
1342 | 185M | if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ |
1343 | 11.0M | int line = ls->linenumber; |
1344 | 11.0M | luaX_next(ls); /* skip operator */ |
1345 | 11.0M | subexpr(ls, v, UNARY_PRIORITY); |
1346 | 11.0M | luaK_prefix(ls->fs, uop, v, line); |
1347 | 11.0M | } |
1348 | 174M | else simpleexp(ls, v); |
1349 | | /* expand while operators have priorities higher than 'limit' */ |
1350 | 185M | op = getbinopr(ls->t.token); |
1351 | 338M | while (op != OPR_NOBINOPR && priority[op].left > limit) { |
1352 | 153M | expdesc v2; |
1353 | 153M | BinOpr nextop; |
1354 | 153M | int line = ls->linenumber; |
1355 | 153M | luaX_next(ls); /* skip operator */ |
1356 | 153M | luaK_infix(ls->fs, op, v); |
1357 | | /* read sub-expression with higher priority */ |
1358 | 153M | nextop = subexpr(ls, &v2, priority[op].right); |
1359 | 153M | luaK_posfix(ls->fs, op, v, &v2, line); |
1360 | 153M | op = nextop; |
1361 | 153M | } |
1362 | 185M | leavelevel(ls); |
1363 | 185M | return op; /* return first untreated operator */ |
1364 | 185M | } |
1365 | | |
1366 | | |
1367 | 21.1M | static void expr (LexState *ls, expdesc *v) { |
1368 | 21.1M | subexpr(ls, v, 0); |
1369 | 21.1M | } |
1370 | | |
1371 | | /* }==================================================================== */ |
1372 | | |
1373 | | |
1374 | | |
1375 | | /* |
1376 | | ** {====================================================================== |
1377 | | ** Rules for Statements |
1378 | | ** ======================================================================= |
1379 | | */ |
1380 | | |
1381 | | |
1382 | 610k | static void block (LexState *ls) { |
1383 | | /* block -> statlist */ |
1384 | 610k | FuncState *fs = ls->fs; |
1385 | 610k | BlockCnt bl; |
1386 | 610k | enterblock(fs, &bl, 0); |
1387 | 610k | statlist(ls); |
1388 | 610k | leaveblock(fs); |
1389 | 610k | } |
1390 | | |
1391 | | |
1392 | | /* |
1393 | | ** structure to chain all variables in the left-hand side of an |
1394 | | ** assignment |
1395 | | */ |
1396 | | struct LHS_assign { |
1397 | | struct LHS_assign *prev; |
1398 | | expdesc v; /* variable (global, local, upvalue, or indexed) */ |
1399 | | }; |
1400 | | |
1401 | | |
1402 | | /* |
1403 | | ** check whether, in an assignment to an upvalue/local variable, the |
1404 | | ** upvalue/local variable is begin used in a previous assignment to a |
1405 | | ** table. If so, save original upvalue/local value in a safe place and |
1406 | | ** use this safe copy in the previous assignment. |
1407 | | */ |
1408 | 468k | static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { |
1409 | 468k | FuncState *fs = ls->fs; |
1410 | 468k | lu_byte extra = fs->freereg; /* eventual position to save local variable */ |
1411 | 468k | int conflict = 0; |
1412 | 9.33M | for (; lh; lh = lh->prev) { /* check all previous assignments */ |
1413 | 8.86M | if (vkisindexed(lh->v.k)) { /* assignment to table field? */ |
1414 | 7.04M | if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ |
1415 | 1.55M | if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { |
1416 | 1.43M | conflict = 1; /* table is the upvalue being assigned now */ |
1417 | 1.43M | lh->v.k = VINDEXSTR; |
1418 | 1.43M | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1419 | 1.43M | } |
1420 | 1.55M | } |
1421 | 5.49M | else { /* table is a register */ |
1422 | 5.49M | if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { |
1423 | 4.56k | conflict = 1; /* table is the local being assigned now */ |
1424 | 4.56k | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1425 | 4.56k | } |
1426 | | /* is index the local being assigned? */ |
1427 | 5.49M | if (lh->v.k == VINDEXED && v->k == VLOCAL && |
1428 | 5.49M | lh->v.u.ind.idx == v->u.var.ridx) { |
1429 | 593 | conflict = 1; |
1430 | 593 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ |
1431 | 593 | } |
1432 | 5.49M | } |
1433 | 7.04M | } |
1434 | 8.86M | } |
1435 | 468k | if (conflict) { |
1436 | | /* copy upvalue/local value to a temporary (in position 'extra') */ |
1437 | 310k | if (v->k == VLOCAL) |
1438 | 2.27k | luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); |
1439 | 307k | else |
1440 | 307k | luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); |
1441 | 310k | luaK_reserveregs(fs, 1); |
1442 | 310k | } |
1443 | 468k | } |
1444 | | |
1445 | | |
1446 | | /* Create code to store the "top" register in 'var' */ |
1447 | 2.65M | static void storevartop (FuncState *fs, expdesc *var) { |
1448 | 2.65M | expdesc e; |
1449 | 2.65M | init_exp(&e, VNONRELOC, fs->freereg - 1); |
1450 | 2.65M | luaK_storevar(fs, var, &e); /* will also free the top register */ |
1451 | 2.65M | } |
1452 | | |
1453 | | |
1454 | | /* |
1455 | | ** Parse and compile a multiple assignment. The first "variable" |
1456 | | ** (a 'suffixedexp') was already read by the caller. |
1457 | | ** |
1458 | | ** assignment -> suffixedexp restassign |
1459 | | ** restassign -> ',' suffixedexp restassign | '=' explist |
1460 | | */ |
1461 | 6.84M | static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { |
1462 | 6.84M | expdesc e; |
1463 | 6.84M | check_condition(ls, vkisvar(lh->v.k), "syntax error"); |
1464 | 6.77M | check_readonly(ls, &lh->v); |
1465 | 6.77M | if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ |
1466 | 4.44M | struct LHS_assign nv; |
1467 | 4.44M | nv.prev = lh; |
1468 | 4.44M | suffixedexp(ls, &nv.v); |
1469 | 4.44M | if (!vkisindexed(nv.v.k)) |
1470 | 468k | check_conflict(ls, lh, &nv.v); |
1471 | 4.44M | enterlevel(ls); /* control recursion depth */ |
1472 | 4.44M | restassign(ls, &nv, nvars+1); |
1473 | 4.44M | leavelevel(ls); |
1474 | 4.44M | } |
1475 | 2.33M | else { /* restassign -> '=' explist */ |
1476 | 2.33M | int nexps; |
1477 | 2.33M | checknext(ls, '='); |
1478 | 2.33M | nexps = explist(ls, &e); |
1479 | 2.33M | if (nexps != nvars) |
1480 | 355k | adjust_assign(ls, nvars, nexps, &e); |
1481 | 1.97M | else { |
1482 | 1.97M | luaK_setoneret(ls->fs, &e); /* close last expression */ |
1483 | 1.97M | luaK_storevar(ls->fs, &lh->v, &e); |
1484 | 1.97M | return; /* avoid default */ |
1485 | 1.97M | } |
1486 | 2.33M | } |
1487 | 4.79M | storevartop(ls->fs, &lh->v); /* default assignment */ |
1488 | 4.79M | } |
1489 | | |
1490 | | |
1491 | 478k | static int cond (LexState *ls) { |
1492 | | /* cond -> exp */ |
1493 | 478k | expdesc v; |
1494 | 478k | expr(ls, &v); /* read condition */ |
1495 | 478k | if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ |
1496 | 478k | luaK_goiftrue(ls->fs, &v); |
1497 | 478k | return v.f; |
1498 | 478k | } |
1499 | | |
1500 | | |
1501 | 246k | static void gotostat (LexState *ls, int line) { |
1502 | 246k | TString *name = str_checkname(ls); /* label's name */ |
1503 | 246k | newgotoentry(ls, name, line); |
1504 | 246k | } |
1505 | | |
1506 | | |
1507 | | /* |
1508 | | ** Break statement. Semantically equivalent to "goto break". |
1509 | | */ |
1510 | 136k | static void breakstat (LexState *ls, int line) { |
1511 | 136k | BlockCnt *bl; /* to look for an enclosing loop */ |
1512 | 537k | for (bl = ls->fs->bl; bl != NULL; bl = bl->previous) { |
1513 | 536k | if (bl->isloop) /* found one? */ |
1514 | 135k | goto ok; |
1515 | 536k | } |
1516 | 1.17k | luaX_syntaxerror(ls, "break outside loop"); |
1517 | 135k | ok: |
1518 | 135k | bl->isloop = 2; /* signal that block has pending breaks */ |
1519 | 135k | luaX_next(ls); /* skip break */ |
1520 | 135k | newgotoentry(ls, ls->brkn, line); |
1521 | 135k | } |
1522 | | |
1523 | | |
1524 | | /* |
1525 | | ** Check whether there is already a label with the given 'name' at |
1526 | | ** current function. |
1527 | | */ |
1528 | 78.5k | static void checkrepeated (LexState *ls, TString *name) { |
1529 | 78.5k | Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); |
1530 | 78.5k | if (l_unlikely(lb != NULL)) /* already defined? */ |
1531 | 2.73k | luaK_semerror(ls, "label '%s' already defined on line %d", |
1532 | 2.73k | getstr(name), lb->line); /* error */ |
1533 | 78.5k | } |
1534 | | |
1535 | | |
1536 | 83.0k | static void labelstat (LexState *ls, TString *name, int line) { |
1537 | | /* label -> '::' NAME '::' */ |
1538 | 83.0k | checknext(ls, TK_DBCOLON); /* skip double colon */ |
1539 | 132k | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) |
1540 | 49.3k | statement(ls); /* skip other no-op statements */ |
1541 | 83.0k | checkrepeated(ls, name); /* check for repeated labels */ |
1542 | 83.0k | createlabel(ls, name, line, block_follow(ls, 0)); |
1543 | 83.0k | } |
1544 | | |
1545 | | |
1546 | 40.4k | static void whilestat (LexState *ls, int line) { |
1547 | | /* whilestat -> WHILE cond DO block END */ |
1548 | 40.4k | FuncState *fs = ls->fs; |
1549 | 40.4k | int whileinit; |
1550 | 40.4k | int condexit; |
1551 | 40.4k | BlockCnt bl; |
1552 | 40.4k | luaX_next(ls); /* skip WHILE */ |
1553 | 40.4k | whileinit = luaK_getlabel(fs); |
1554 | 40.4k | condexit = cond(ls); |
1555 | 40.4k | enterblock(fs, &bl, 1); |
1556 | 40.4k | checknext(ls, TK_DO); |
1557 | 40.4k | block(ls); |
1558 | 40.4k | luaK_jumpto(fs, whileinit); |
1559 | 40.4k | check_match(ls, TK_END, TK_WHILE, line); |
1560 | 40.4k | leaveblock(fs); |
1561 | 40.4k | luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ |
1562 | 40.4k | } |
1563 | | |
1564 | | |
1565 | 83.2k | static void repeatstat (LexState *ls, int line) { |
1566 | | /* repeatstat -> REPEAT block UNTIL cond */ |
1567 | 83.2k | int condexit; |
1568 | 83.2k | FuncState *fs = ls->fs; |
1569 | 83.2k | int repeat_init = luaK_getlabel(fs); |
1570 | 83.2k | BlockCnt bl1, bl2; |
1571 | 83.2k | enterblock(fs, &bl1, 1); /* loop block */ |
1572 | 83.2k | enterblock(fs, &bl2, 0); /* scope block */ |
1573 | 83.2k | luaX_next(ls); /* skip REPEAT */ |
1574 | 83.2k | statlist(ls); |
1575 | 83.2k | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
1576 | 83.2k | condexit = cond(ls); /* read condition (inside scope block) */ |
1577 | 83.2k | leaveblock(fs); /* finish scope */ |
1578 | 83.2k | if (bl2.upval) { /* upvalues? */ |
1579 | 6.31k | int exit = luaK_jump(fs); /* normal exit must jump over fix */ |
1580 | 6.31k | luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ |
1581 | 6.31k | luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); |
1582 | 6.31k | condexit = luaK_jump(fs); /* repeat after closing upvalues */ |
1583 | 6.31k | luaK_patchtohere(fs, exit); /* normal exit comes to here */ |
1584 | 6.31k | } |
1585 | 83.2k | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ |
1586 | 83.2k | leaveblock(fs); /* finish loop */ |
1587 | 83.2k | } |
1588 | | |
1589 | | |
1590 | | /* |
1591 | | ** Read an expression and generate code to put its results in next |
1592 | | ** stack slot. |
1593 | | ** |
1594 | | */ |
1595 | 117k | static void exp1 (LexState *ls) { |
1596 | 117k | expdesc e; |
1597 | 117k | expr(ls, &e); |
1598 | 117k | luaK_exp2nextreg(ls->fs, &e); |
1599 | 117k | lua_assert(e.k == VNONRELOC); |
1600 | 117k | } |
1601 | | |
1602 | | |
1603 | | /* |
1604 | | ** Fix for instruction at position 'pc' to jump to 'dest'. |
1605 | | ** (Jump addresses are relative in Lua). 'back' true means |
1606 | | ** a back jump. |
1607 | | */ |
1608 | 119k | static void fixforjump (FuncState *fs, int pc, int dest, int back) { |
1609 | 119k | Instruction *jmp = &fs->f->code[pc]; |
1610 | 119k | int offset = dest - (pc + 1); |
1611 | 119k | if (back) |
1612 | 59.9k | offset = -offset; |
1613 | 119k | if (l_unlikely(offset > MAXARG_Bx)) |
1614 | 19 | luaX_syntaxerror(fs->ls, "control structure too long"); |
1615 | 119k | SETARG_Bx(*jmp, offset); |
1616 | 119k | } |
1617 | | |
1618 | | |
1619 | | /* |
1620 | | ** Generate code for a 'for' loop. |
1621 | | */ |
1622 | 82.9k | static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { |
1623 | | /* forbody -> DO block */ |
1624 | 82.9k | static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; |
1625 | 82.9k | static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; |
1626 | 82.9k | BlockCnt bl; |
1627 | 82.9k | FuncState *fs = ls->fs; |
1628 | 82.9k | int prep, endfor; |
1629 | 82.9k | checknext(ls, TK_DO); |
1630 | 82.9k | prep = luaK_codeABx(fs, forprep[isgen], base, 0); |
1631 | 82.9k | fs->freereg--; /* both 'forprep' remove one register from the stack */ |
1632 | 82.9k | enterblock(fs, &bl, 0); /* scope for declared variables */ |
1633 | 82.9k | adjustlocalvars(ls, nvars); |
1634 | 82.9k | luaK_reserveregs(fs, nvars); |
1635 | 82.9k | block(ls); |
1636 | 82.9k | leaveblock(fs); /* end of scope for declared variables */ |
1637 | 82.9k | fixforjump(fs, prep, luaK_getlabel(fs), 0); |
1638 | 82.9k | if (isgen) { /* generic for? */ |
1639 | 37.5k | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); |
1640 | 37.5k | luaK_fixline(fs, line); |
1641 | 37.5k | } |
1642 | 82.9k | endfor = luaK_codeABx(fs, forloop[isgen], base, 0); |
1643 | 82.9k | fixforjump(fs, endfor, prep + 1, 1); |
1644 | 82.9k | luaK_fixline(fs, line); |
1645 | 82.9k | } |
1646 | | |
1647 | | |
1648 | 68.2k | static void fornum (LexState *ls, TString *varname, int line) { |
1649 | | /* fornum -> NAME = exp,exp[,exp] forbody */ |
1650 | 68.2k | FuncState *fs = ls->fs; |
1651 | 68.2k | int base = fs->freereg; |
1652 | 68.2k | new_localvarliteral(ls, "(for state)"); |
1653 | 68.2k | new_localvarliteral(ls, "(for state)"); |
1654 | 68.2k | new_varkind(ls, varname, RDKCONST); /* control variable */ |
1655 | 68.2k | checknext(ls, '='); |
1656 | 68.2k | exp1(ls); /* initial value */ |
1657 | 68.2k | checknext(ls, ','); |
1658 | 68.2k | exp1(ls); /* limit */ |
1659 | 68.2k | if (testnext(ls, ',')) |
1660 | 10.5k | exp1(ls); /* optional step */ |
1661 | 57.7k | else { /* default step = 1 */ |
1662 | 57.7k | luaK_int(fs, fs->freereg, 1); |
1663 | 57.7k | luaK_reserveregs(fs, 1); |
1664 | 57.7k | } |
1665 | 68.2k | adjustlocalvars(ls, 2); /* start scope for internal variables */ |
1666 | 68.2k | forbody(ls, base, line, 1, 0); |
1667 | 68.2k | } |
1668 | | |
1669 | | |
1670 | 51.9k | static void forlist (LexState *ls, TString *indexname) { |
1671 | | /* forlist -> NAME {,NAME} IN explist forbody */ |
1672 | 51.9k | FuncState *fs = ls->fs; |
1673 | 51.9k | expdesc e; |
1674 | 51.9k | int nvars = 4; /* function, state, closing, control */ |
1675 | 51.9k | int line; |
1676 | 51.9k | int base = fs->freereg; |
1677 | | /* create internal variables */ |
1678 | 51.9k | new_localvarliteral(ls, "(for state)"); /* iterator function */ |
1679 | 51.9k | new_localvarliteral(ls, "(for state)"); /* state */ |
1680 | 51.9k | new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ |
1681 | 51.9k | new_varkind(ls, indexname, RDKCONST); /* control variable */ |
1682 | | /* other declared variables */ |
1683 | 143k | while (testnext(ls, ',')) { |
1684 | 91.4k | new_localvar(ls, str_checkname(ls)); |
1685 | 91.4k | nvars++; |
1686 | 91.4k | } |
1687 | 51.9k | checknext(ls, TK_IN); |
1688 | 51.9k | line = ls->linenumber; |
1689 | 51.9k | adjust_assign(ls, 4, explist(ls, &e), &e); |
1690 | 51.9k | adjustlocalvars(ls, 3); /* start scope for internal variables */ |
1691 | 51.9k | marktobeclosed(fs); /* last internal var. must be closed */ |
1692 | 51.9k | luaK_checkstack(fs, 2); /* extra space to call iterator */ |
1693 | 51.9k | forbody(ls, base, line, nvars - 3, 1); |
1694 | 51.9k | } |
1695 | | |
1696 | | |
1697 | 138k | static void forstat (LexState *ls, int line) { |
1698 | | /* forstat -> FOR (fornum | forlist) END */ |
1699 | 138k | FuncState *fs = ls->fs; |
1700 | 138k | TString *varname; |
1701 | 138k | BlockCnt bl; |
1702 | 138k | enterblock(fs, &bl, 1); /* scope for loop and control variables */ |
1703 | 138k | luaX_next(ls); /* skip 'for' */ |
1704 | 138k | varname = str_checkname(ls); /* first variable name */ |
1705 | 138k | switch (ls->t.token) { |
1706 | 68.2k | case '=': fornum(ls, varname, line); break; |
1707 | 51.9k | case ',': case TK_IN: forlist(ls, varname); break; |
1708 | 15.8k | default: luaX_syntaxerror(ls, "'=' or 'in' expected"); |
1709 | 138k | } |
1710 | 59.9k | check_match(ls, TK_END, TK_FOR, line); |
1711 | 59.9k | leaveblock(fs); /* loop scope ('break' jumps to this point) */ |
1712 | 59.9k | } |
1713 | | |
1714 | | |
1715 | 360k | static void test_then_block (LexState *ls, int *escapelist) { |
1716 | | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
1717 | 360k | FuncState *fs = ls->fs; |
1718 | 360k | int condtrue; |
1719 | 360k | luaX_next(ls); /* skip IF or ELSEIF */ |
1720 | 360k | condtrue = cond(ls); /* read condition */ |
1721 | 360k | checknext(ls, TK_THEN); |
1722 | 360k | block(ls); /* 'then' part */ |
1723 | 360k | if (ls->t.token == TK_ELSE || |
1724 | 360k | ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ |
1725 | 94.8k | luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ |
1726 | 360k | luaK_patchtohere(fs, condtrue); |
1727 | 360k | } |
1728 | | |
1729 | | |
1730 | 310k | static void ifstat (LexState *ls, int line) { |
1731 | | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ |
1732 | 310k | FuncState *fs = ls->fs; |
1733 | 310k | int escapelist = NO_JUMP; /* exit list for finished parts */ |
1734 | 310k | test_then_block(ls, &escapelist); /* IF cond THEN block */ |
1735 | 360k | while (ls->t.token == TK_ELSEIF) |
1736 | 50.7k | test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ |
1737 | 310k | if (testnext(ls, TK_ELSE)) |
1738 | 44.1k | block(ls); /* 'else' part */ |
1739 | 310k | check_match(ls, TK_END, TK_IF, line); |
1740 | 310k | luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ |
1741 | 310k | } |
1742 | | |
1743 | | |
1744 | 44.2k | static void localfunc (LexState *ls) { |
1745 | 44.2k | expdesc b; |
1746 | 44.2k | FuncState *fs = ls->fs; |
1747 | 44.2k | int fvar = fs->nactvar; /* function's variable index */ |
1748 | 44.2k | new_localvar(ls, str_checkname(ls)); /* new local variable */ |
1749 | 44.2k | adjustlocalvars(ls, 1); /* enter its scope */ |
1750 | 44.2k | body(ls, &b, 0, ls->linenumber); /* function created in next register */ |
1751 | | /* debug information will only see the variable after this point! */ |
1752 | 44.2k | localdebuginfo(fs, fvar)->startpc = fs->pc; |
1753 | 44.2k | } |
1754 | | |
1755 | | |
1756 | 3.83M | static lu_byte getvarattribute (LexState *ls, lu_byte df) { |
1757 | | /* attrib -> ['<' NAME '>'] */ |
1758 | 3.83M | if (testnext(ls, '<')) { |
1759 | 48.5k | TString *ts = str_checkname(ls); |
1760 | 48.5k | const char *attr = getstr(ts); |
1761 | 48.5k | checknext(ls, '>'); |
1762 | 48.5k | if (strcmp(attr, "const") == 0) |
1763 | 23.9k | return RDKCONST; /* read-only variable */ |
1764 | 24.5k | else if (strcmp(attr, "close") == 0) |
1765 | 13.0k | return RDKTOCLOSE; /* to-be-closed variable */ |
1766 | 11.5k | else |
1767 | 11.5k | luaK_semerror(ls, "unknown attribute '%s'", attr); |
1768 | 48.5k | } |
1769 | 3.78M | return df; /* return default value */ |
1770 | 3.83M | } |
1771 | | |
1772 | | |
1773 | 830k | static void checktoclose (FuncState *fs, int level) { |
1774 | 830k | if (level != -1) { /* is there a to-be-closed variable? */ |
1775 | 10.6k | marktobeclosed(fs); |
1776 | 10.6k | luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); |
1777 | 10.6k | } |
1778 | 830k | } |
1779 | | |
1780 | | |
1781 | 851k | static void localstat (LexState *ls) { |
1782 | | /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */ |
1783 | 851k | FuncState *fs = ls->fs; |
1784 | 851k | int toclose = -1; /* index of to-be-closed variable (if any) */ |
1785 | 851k | Vardesc *var; /* last variable */ |
1786 | 851k | int vidx; /* index of last variable */ |
1787 | 851k | int nvars = 0; |
1788 | 851k | int nexps; |
1789 | 851k | expdesc e; |
1790 | | /* get prefixed attribute (if any); default is regular local variable */ |
1791 | 851k | lu_byte defkind = getvarattribute(ls, VDKREG); |
1792 | 2.07M | do { /* for each variable */ |
1793 | 2.07M | TString *vname = str_checkname(ls); /* get its name */ |
1794 | 2.07M | lu_byte kind = getvarattribute(ls, defkind); /* postfixed attribute */ |
1795 | 2.07M | vidx = new_varkind(ls, vname, kind); /* predeclare it */ |
1796 | 2.07M | if (kind == RDKTOCLOSE) { /* to-be-closed? */ |
1797 | 10.8k | if (toclose != -1) /* one already present? */ |
1798 | 140 | luaK_semerror(ls, "multiple to-be-closed variables in local list"); |
1799 | 10.7k | toclose = fs->nactvar + nvars; |
1800 | 10.7k | } |
1801 | 2.07M | nvars++; |
1802 | 2.07M | } while (testnext(ls, ',')); |
1803 | 851k | if (testnext(ls, '=')) /* initialization? */ |
1804 | 755k | nexps = explist(ls, &e); |
1805 | 95.8k | else { |
1806 | 95.8k | e.k = VVOID; |
1807 | 95.8k | nexps = 0; |
1808 | 95.8k | } |
1809 | 851k | var = getlocalvardesc(fs, vidx); /* retrieve last variable */ |
1810 | 851k | if (nvars == nexps && /* no adjustments? */ |
1811 | 851k | var->vd.kind == RDKCONST && /* last variable is const? */ |
1812 | 851k | luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ |
1813 | 15.7k | var->vd.kind = RDKCTC; /* variable is a compile-time constant */ |
1814 | 15.7k | adjustlocalvars(ls, nvars - 1); /* exclude last variable */ |
1815 | 15.7k | fs->nactvar++; /* but count it */ |
1816 | 15.7k | } |
1817 | 835k | else { |
1818 | 835k | adjust_assign(ls, nvars, nexps, &e); |
1819 | 835k | adjustlocalvars(ls, nvars); |
1820 | 835k | } |
1821 | 851k | checktoclose(fs, toclose); |
1822 | 851k | } |
1823 | | |
1824 | | |
1825 | 915k | static lu_byte getglobalattribute (LexState *ls, lu_byte df) { |
1826 | 915k | lu_byte kind = getvarattribute(ls, df); |
1827 | 915k | switch (kind) { |
1828 | 2.14k | case RDKTOCLOSE: |
1829 | 2.14k | luaK_semerror(ls, "global variables cannot be to-be-closed"); |
1830 | 0 | return kind; /* to avoid warnings */ |
1831 | 3.27k | case RDKCONST: |
1832 | 3.27k | return GDKCONST; /* adjust kind for global variable */ |
1833 | 908k | default: |
1834 | 908k | return kind; |
1835 | 915k | } |
1836 | 915k | } |
1837 | | |
1838 | | |
1839 | 21.5k | static void globalnames (LexState *ls, lu_byte defkind) { |
1840 | 21.5k | FuncState *fs = ls->fs; |
1841 | 21.5k | int nvars = 0; |
1842 | 21.5k | int lastidx; /* index of last registered variable */ |
1843 | 891k | do { /* for each name */ |
1844 | 891k | TString *vname = str_checkname(ls); |
1845 | 891k | lu_byte kind = getglobalattribute(ls, defkind); |
1846 | 891k | lastidx = new_varkind(ls, vname, kind); |
1847 | 891k | nvars++; |
1848 | 891k | } while (testnext(ls, ',')); |
1849 | 21.5k | if (testnext(ls, '=')) { /* initialization? */ |
1850 | 3.92k | expdesc e; |
1851 | 3.92k | int i; |
1852 | 3.92k | int nexps = explist(ls, &e); /* read list of expressions */ |
1853 | 3.92k | adjust_assign(ls, nvars, nexps, &e); |
1854 | 16.5k | for (i = 0; i < nvars; i++) { /* for each variable */ |
1855 | 12.5k | expdesc var; |
1856 | 12.5k | TString *varname = getlocalvardesc(fs, lastidx - i)->vd.name; |
1857 | 12.5k | buildglobal(ls, varname, &var); /* create global variable in 'var' */ |
1858 | 12.5k | storevartop(fs, &var); |
1859 | 12.5k | } |
1860 | 3.92k | } |
1861 | 21.5k | fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ |
1862 | 21.5k | } |
1863 | | |
1864 | | |
1865 | 27.1k | static void globalstat (LexState *ls) { |
1866 | | /* globalstat -> (GLOBAL) attrib '*' |
1867 | | globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ |
1868 | 27.1k | FuncState *fs = ls->fs; |
1869 | | /* get prefixed attribute (if any); default is regular global variable */ |
1870 | 27.1k | lu_byte defkind = getglobalattribute(ls, GDKREG); |
1871 | 27.1k | if (!testnext(ls, '*')) |
1872 | 21.5k | globalnames(ls, defkind); |
1873 | 5.61k | else { |
1874 | | /* use NULL as name to represent '*' entries */ |
1875 | 5.61k | new_varkind(ls, NULL, defkind); |
1876 | 5.61k | fs->nactvar++; /* activate declaration */ |
1877 | 5.61k | } |
1878 | 27.1k | } |
1879 | | |
1880 | | |
1881 | 1.40k | static void globalfunc (LexState *ls, int line) { |
1882 | | /* globalfunc -> (GLOBAL FUNCTION) NAME body */ |
1883 | 1.40k | expdesc var, b; |
1884 | 1.40k | FuncState *fs = ls->fs; |
1885 | 1.40k | TString *fname = str_checkname(ls); |
1886 | 1.40k | new_varkind(ls, fname, GDKREG); /* declare global variable */ |
1887 | 1.40k | fs->nactvar++; /* enter its scope */ |
1888 | 1.40k | buildglobal(ls, fname, &var); |
1889 | 1.40k | body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ |
1890 | 1.40k | luaK_storevar(fs, &var, &b); |
1891 | 1.40k | luaK_fixline(fs, line); /* definition "happens" in the first line */ |
1892 | 1.40k | } |
1893 | | |
1894 | | |
1895 | 28.5k | static void globalstatfunc (LexState *ls, int line) { |
1896 | | /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ |
1897 | 28.5k | luaX_next(ls); /* skip 'global' */ |
1898 | 28.5k | if (testnext(ls, TK_FUNCTION)) |
1899 | 1.40k | globalfunc(ls, line); |
1900 | 27.1k | else |
1901 | 27.1k | globalstat(ls); |
1902 | 28.5k | } |
1903 | | |
1904 | | |
1905 | 71.4k | static int funcname (LexState *ls, expdesc *v) { |
1906 | | /* funcname -> NAME {fieldsel} [':' NAME] */ |
1907 | 71.4k | int ismethod = 0; |
1908 | 71.4k | singlevar(ls, v); |
1909 | 82.0k | while (ls->t.token == '.') |
1910 | 10.6k | fieldsel(ls, v); |
1911 | 71.4k | if (ls->t.token == ':') { |
1912 | 11.3k | ismethod = 1; |
1913 | 11.3k | fieldsel(ls, v); |
1914 | 11.3k | } |
1915 | 71.4k | return ismethod; |
1916 | 71.4k | } |
1917 | | |
1918 | | |
1919 | 71.4k | static void funcstat (LexState *ls, int line) { |
1920 | | /* funcstat -> FUNCTION funcname body */ |
1921 | 71.4k | int ismethod; |
1922 | 71.4k | expdesc v, b; |
1923 | 71.4k | luaX_next(ls); /* skip FUNCTION */ |
1924 | 71.4k | ismethod = funcname(ls, &v); |
1925 | 71.4k | check_readonly(ls, &v); |
1926 | 71.4k | body(ls, &b, ismethod, line); |
1927 | 71.4k | luaK_storevar(ls->fs, &v, &b); |
1928 | 71.4k | luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ |
1929 | 71.4k | } |
1930 | | |
1931 | | |
1932 | 7.41M | static void exprstat (LexState *ls) { |
1933 | | /* stat -> func | assignment */ |
1934 | 7.41M | FuncState *fs = ls->fs; |
1935 | 7.41M | struct LHS_assign v; |
1936 | 7.41M | suffixedexp(ls, &v.v); |
1937 | 7.41M | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ |
1938 | 2.65M | v.prev = NULL; |
1939 | 2.65M | restassign(ls, &v, 1); |
1940 | 2.65M | } |
1941 | 4.76M | else { /* stat -> func */ |
1942 | 4.76M | Instruction *inst; |
1943 | 4.76M | check_condition(ls, v.v.k == VCALL, "syntax error"); |
1944 | 4.09M | inst = &getinstruction(fs, &v.v); |
1945 | 4.09M | SETARG_C(*inst, 1); /* call statement uses no results */ |
1946 | 4.09M | } |
1947 | 7.41M | } |
1948 | | |
1949 | | |
1950 | 734k | static void retstat (LexState *ls) { |
1951 | | /* stat -> RETURN [explist] [';'] */ |
1952 | 734k | FuncState *fs = ls->fs; |
1953 | 734k | expdesc e; |
1954 | 734k | int nret; /* number of values being returned */ |
1955 | 734k | int first = luaY_nvarstack(fs); /* first slot to be returned */ |
1956 | 734k | if (block_follow(ls, 1) || ls->t.token == ';') |
1957 | 65.5k | nret = 0; /* return no values */ |
1958 | 669k | else { |
1959 | 669k | nret = explist(ls, &e); /* optional return values */ |
1960 | 669k | if (hasmultret(e.k)) { |
1961 | 77.3k | luaK_setmultret(fs, &e); |
1962 | 77.3k | if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ |
1963 | 65.5k | SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); |
1964 | 65.5k | lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); |
1965 | 65.5k | } |
1966 | 77.3k | nret = LUA_MULTRET; /* return all values */ |
1967 | 77.3k | } |
1968 | 592k | else { |
1969 | 592k | if (nret == 1) /* only one single value? */ |
1970 | 576k | first = luaK_exp2anyreg(fs, &e); /* can use original slot */ |
1971 | 15.2k | else { /* values must go to the top of the stack */ |
1972 | 15.2k | luaK_exp2nextreg(fs, &e); |
1973 | 15.2k | lua_assert(nret == fs->freereg - first); |
1974 | 15.2k | } |
1975 | 592k | } |
1976 | 669k | } |
1977 | 731k | luaK_ret(fs, first, nret); |
1978 | 731k | testnext(ls, ';'); /* skip optional semicolon */ |
1979 | 731k | } |
1980 | | |
1981 | | |
1982 | 11.4M | static void statement (LexState *ls) { |
1983 | 11.4M | int line = ls->linenumber; /* may be needed for error messages */ |
1984 | 11.4M | enterlevel(ls); |
1985 | 11.4M | switch (ls->t.token) { |
1986 | 1.10M | case ';': { /* stat -> ';' (empty statement) */ |
1987 | 1.10M | luaX_next(ls); /* skip ';' */ |
1988 | 1.10M | break; |
1989 | 0 | } |
1990 | 310k | case TK_IF: { /* stat -> ifstat */ |
1991 | 310k | ifstat(ls, line); |
1992 | 310k | break; |
1993 | 0 | } |
1994 | 40.4k | case TK_WHILE: { /* stat -> whilestat */ |
1995 | 40.4k | whilestat(ls, line); |
1996 | 40.4k | break; |
1997 | 0 | } |
1998 | 104k | case TK_DO: { /* stat -> DO block END */ |
1999 | 104k | luaX_next(ls); /* skip DO */ |
2000 | 104k | block(ls); |
2001 | 104k | check_match(ls, TK_END, TK_DO, line); |
2002 | 104k | break; |
2003 | 0 | } |
2004 | 138k | case TK_FOR: { /* stat -> forstat */ |
2005 | 138k | forstat(ls, line); |
2006 | 138k | break; |
2007 | 0 | } |
2008 | 83.2k | case TK_REPEAT: { /* stat -> repeatstat */ |
2009 | 83.2k | repeatstat(ls, line); |
2010 | 83.2k | break; |
2011 | 0 | } |
2012 | 71.4k | case TK_FUNCTION: { /* stat -> funcstat */ |
2013 | 71.4k | funcstat(ls, line); |
2014 | 71.4k | break; |
2015 | 0 | } |
2016 | 896k | case TK_LOCAL: { /* stat -> localstat */ |
2017 | 896k | luaX_next(ls); /* skip LOCAL */ |
2018 | 896k | if (testnext(ls, TK_FUNCTION)) /* local function? */ |
2019 | 44.2k | localfunc(ls); |
2020 | 851k | else |
2021 | 851k | localstat(ls); |
2022 | 896k | break; |
2023 | 0 | } |
2024 | 0 | case TK_GLOBAL: { /* stat -> globalstatfunc */ |
2025 | 0 | globalstatfunc(ls, line); |
2026 | 0 | break; |
2027 | 0 | } |
2028 | 88.5k | case TK_DBCOLON: { /* stat -> label */ |
2029 | 88.5k | luaX_next(ls); /* skip double colon */ |
2030 | 88.5k | labelstat(ls, str_checkname(ls), line); |
2031 | 88.5k | break; |
2032 | 0 | } |
2033 | 734k | case TK_RETURN: { /* stat -> retstat */ |
2034 | 734k | luaX_next(ls); /* skip RETURN */ |
2035 | 734k | retstat(ls); |
2036 | 734k | break; |
2037 | 0 | } |
2038 | 136k | case TK_BREAK: { /* stat -> breakstat */ |
2039 | 136k | breakstat(ls, line); |
2040 | 136k | break; |
2041 | 0 | } |
2042 | 246k | case TK_GOTO: { /* stat -> 'goto' NAME */ |
2043 | 246k | luaX_next(ls); /* skip 'goto' */ |
2044 | 246k | gotostat(ls, line); |
2045 | 246k | break; |
2046 | 0 | } |
2047 | 0 | #if defined(LUA_COMPAT_GLOBAL) |
2048 | 4.89M | case TK_NAME: { |
2049 | | /* compatibility code to parse global keyword when "global" |
2050 | | is not reserved */ |
2051 | 4.89M | if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ |
2052 | 32.8k | int lk = luaX_lookahead(ls); |
2053 | 32.8k | if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { |
2054 | | /* 'global <attrib>' or 'global name' or 'global *' or |
2055 | | 'global function' */ |
2056 | 28.5k | globalstatfunc(ls, line); |
2057 | 28.5k | break; |
2058 | 28.5k | } |
2059 | 32.8k | } /* else... */ |
2060 | 4.89M | } |
2061 | 4.86M | #endif |
2062 | | /* FALLTHROUGH */ |
2063 | 7.41M | default: { /* stat -> func | assignment */ |
2064 | 7.41M | exprstat(ls); |
2065 | 7.41M | break; |
2066 | 4.89M | } |
2067 | 11.4M | } |
2068 | 6.51M | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && |
2069 | 6.51M | ls->fs->freereg >= luaY_nvarstack(ls->fs)); |
2070 | 6.51M | ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ |
2071 | 6.51M | leavelevel(ls); |
2072 | 6.51M | } |
2073 | | |
2074 | | /* }====================================================================== */ |
2075 | | |
2076 | | /* }====================================================================== */ |
2077 | | |
2078 | | |
2079 | | /* |
2080 | | ** compiles the main function, which is a regular vararg function with an |
2081 | | ** upvalue named LUA_ENV |
2082 | | */ |
2083 | 7.92M | static void mainfunc (LexState *ls, FuncState *fs) { |
2084 | 7.92M | BlockCnt bl; |
2085 | 7.92M | Upvaldesc *env; |
2086 | 7.92M | open_func(ls, fs, &bl); |
2087 | 7.92M | setvararg(fs, 0); /* main function is always declared vararg */ |
2088 | 7.92M | env = allocupvalue(fs); /* ...set environment upvalue */ |
2089 | 7.92M | env->instack = 1; |
2090 | 7.92M | env->idx = 0; |
2091 | 7.92M | env->kind = VDKREG; |
2092 | 7.92M | env->name = ls->envn; |
2093 | 7.92M | luaC_objbarrier(ls->L, fs->f, env->name); |
2094 | 7.92M | luaX_next(ls); /* read first token */ |
2095 | 7.92M | statlist(ls); /* parse main body */ |
2096 | 7.92M | check(ls, TK_EOS); |
2097 | 7.92M | close_func(ls); |
2098 | 7.92M | } |
2099 | | |
2100 | | |
2101 | | LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, |
2102 | 7.92M | Dyndata *dyd, const char *name, int firstchar) { |
2103 | 7.92M | LexState lexstate; |
2104 | 7.92M | FuncState funcstate; |
2105 | 7.92M | LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ |
2106 | 7.92M | setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ |
2107 | 7.92M | luaD_inctop(L); |
2108 | 7.92M | lexstate.h = luaH_new(L); /* create table for scanner */ |
2109 | 7.92M | sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ |
2110 | 7.92M | luaD_inctop(L); |
2111 | 7.92M | funcstate.f = cl->p = luaF_newproto(L); |
2112 | 7.92M | luaC_objbarrier(L, cl, cl->p); |
2113 | 7.92M | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ |
2114 | 7.92M | luaC_objbarrier(L, funcstate.f, funcstate.f->source); |
2115 | 7.92M | lexstate.buff = buff; |
2116 | 7.92M | lexstate.dyd = dyd; |
2117 | 7.92M | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; |
2118 | 7.92M | luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); |
2119 | 7.92M | mainfunc(&lexstate, &funcstate); |
2120 | 7.92M | lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); |
2121 | | /* all scopes should be correctly finished */ |
2122 | 3.29M | lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); |
2123 | 3.29M | L->top.p--; /* remove scanner's table */ |
2124 | 3.29M | return cl; /* closure is on the stack, too */ |
2125 | 3.29M | } |
2126 | | |