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