/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.47M | #define MAXVARS 200 |
36 | | |
37 | | |
38 | 9.01M | #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.89G | #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 | 589k | static l_noret error_expected (LexState *ls, int token) { |
69 | 589k | luaX_syntaxerror(ls, |
70 | 589k | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); |
71 | 589k | } |
72 | | |
73 | | |
74 | 11.5k | static l_noret errorlimit (FuncState *fs, int limit, const char *what) { |
75 | 11.5k | lua_State *L = fs->ls->L; |
76 | 11.5k | const char *msg; |
77 | 11.5k | int line = fs->f->linedefined; |
78 | 11.5k | const char *where = (line == 0) |
79 | 11.5k | ? "main function" |
80 | 11.5k | : luaO_pushfstring(L, "function at line %d", line); |
81 | 11.5k | msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", |
82 | 11.5k | what, limit, where); |
83 | 11.5k | luaX_syntaxerror(fs->ls, msg); |
84 | 11.5k | } |
85 | | |
86 | | |
87 | 30.7M | void luaY_checklimit (FuncState *fs, int v, int l, const char *what) { |
88 | 30.7M | if (l_unlikely(v > l)) errorlimit(fs, l, what); |
89 | 30.7M | } |
90 | | |
91 | | |
92 | | /* |
93 | | ** Test whether next token is 'c'; if so, skip it. |
94 | | */ |
95 | 42.0M | static int testnext (LexState *ls, int c) { |
96 | 42.0M | if (ls->t.token == c) { |
97 | 26.8M | luaX_next(ls); |
98 | 26.8M | return 1; |
99 | 26.8M | } |
100 | 15.2M | else return 0; |
101 | 42.0M | } |
102 | | |
103 | | |
104 | | /* |
105 | | ** Check that next token is 'c'. |
106 | | */ |
107 | 197M | static void check (LexState *ls, int c) { |
108 | 197M | if (ls->t.token != c) |
109 | 393k | error_expected(ls, c); |
110 | 197M | } |
111 | | |
112 | | |
113 | | /* |
114 | | ** Check that next token is 'c' and skip it. |
115 | | */ |
116 | 11.6M | static void checknext (LexState *ls, int c) { |
117 | 11.6M | check(ls, c); |
118 | 11.6M | luaX_next(ls); |
119 | 11.6M | } |
120 | | |
121 | | |
122 | 15.4M | #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.15M | static void check_match (LexState *ls, int what, int who, int where) { |
131 | 5.15M | if (l_unlikely(!testnext(ls, what))) { |
132 | 199k | if (where == ls->linenumber) /* all in the same line? */ |
133 | 195k | error_expected(ls, what); /* do not need a complex message */ |
134 | 3.49k | else { |
135 | 3.49k | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, |
136 | 3.49k | "%s expected (to close %s at line %d)", |
137 | 3.49k | luaX_token2str(ls, what), luaX_token2str(ls, who), where)); |
138 | 3.49k | } |
139 | 199k | } |
140 | 5.15M | } |
141 | | |
142 | | |
143 | 182M | static TString *str_checkname (LexState *ls) { |
144 | 182M | TString *ts; |
145 | 182M | check(ls, TK_NAME); |
146 | 182M | ts = ls->t.seminfo.ts; |
147 | 182M | luaX_next(ls); |
148 | 182M | return ts; |
149 | 182M | } |
150 | | |
151 | | |
152 | 489M | static void init_exp (expdesc *e, expkind k, int i) { |
153 | 489M | e->f = e->t = NO_JUMP; |
154 | 489M | e->k = k; |
155 | 489M | e->u.info = i; |
156 | 489M | } |
157 | | |
158 | | |
159 | 145M | static void codestring (expdesc *e, TString *s) { |
160 | 145M | e->f = e->t = NO_JUMP; |
161 | 145M | e->k = VKSTR; |
162 | 145M | e->u.strval = s; |
163 | 145M | } |
164 | | |
165 | | |
166 | 9.65M | static void codename (LexState *ls, expdesc *e) { |
167 | 9.65M | codestring(e, str_checkname(ls)); |
168 | 9.65M | } |
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.47M | TString *varname) { |
177 | 3.47M | Proto *f = fs->f; |
178 | 3.47M | int oldsize = f->sizelocvars; |
179 | 3.47M | luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, |
180 | 3.47M | LocVar, SHRT_MAX, "local variables"); |
181 | 9.96M | while (oldsize < f->sizelocvars) |
182 | 6.49M | f->locvars[oldsize++].varname = NULL; |
183 | 3.47M | f->locvars[fs->ndebugvars].varname = varname; |
184 | 3.47M | f->locvars[fs->ndebugvars].startpc = fs->pc; |
185 | 3.47M | luaC_objbarrier(ls->L, f, varname); |
186 | 3.47M | return fs->ndebugvars++; |
187 | 3.47M | } |
188 | | |
189 | | |
190 | | /* |
191 | | ** Create a new variable with the given 'name' and given 'kind'. |
192 | | ** Return its index in the function. |
193 | | */ |
194 | 4.58M | static int new_varkind (LexState *ls, TString *name, lu_byte kind) { |
195 | 4.58M | lua_State *L = ls->L; |
196 | 4.58M | FuncState *fs = ls->fs; |
197 | 4.58M | Dyndata *dyd = ls->dyd; |
198 | 4.58M | Vardesc *var; |
199 | 4.58M | luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, |
200 | 4.58M | dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations"); |
201 | 4.58M | var = &dyd->actvar.arr[dyd->actvar.n++]; |
202 | 4.58M | var->vd.kind = kind; /* default */ |
203 | 4.58M | var->vd.name = name; |
204 | 4.58M | return dyd->actvar.n - 1 - fs->firstlocal; |
205 | 4.58M | } |
206 | | |
207 | | |
208 | | /* |
209 | | ** Create a new local variable with the given 'name' and regular kind. |
210 | | */ |
211 | 1.89M | static int new_localvar (LexState *ls, TString *name) { |
212 | 1.89M | return new_varkind(ls, name, VDKREG); |
213 | 1.89M | } |
214 | | |
215 | | #define new_localvarliteral(ls,v) \ |
216 | 288k | new_localvar(ls, \ |
217 | 288k | 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.82G | static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { |
227 | 1.82G | return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; |
228 | 1.82G | } |
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 | 448M | static lu_byte reglevel (FuncState *fs, int nvar) { |
237 | 695M | while (nvar-- > 0) { |
238 | 453M | Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ |
239 | 453M | if (varinreg(vd)) /* is in a register? */ |
240 | 206M | return cast_byte(vd->vd.ridx + 1); |
241 | 453M | } |
242 | 242M | return 0; /* no variables in registers */ |
243 | 448M | } |
244 | | |
245 | | |
246 | | /* |
247 | | ** Return the number of variables in the register stack for the given |
248 | | ** function. |
249 | | */ |
250 | 436M | lu_byte luaY_nvarstack (FuncState *fs) { |
251 | 436M | return reglevel(fs, fs->nactvar); |
252 | 436M | } |
253 | | |
254 | | |
255 | | /* |
256 | | ** Get the debug-information entry for current variable 'vidx'. |
257 | | */ |
258 | 2.34M | static LocVar *localdebuginfo (FuncState *fs, int vidx) { |
259 | 2.34M | Vardesc *vd = getlocalvardesc(fs, vidx); |
260 | 2.34M | if (!varinreg(vd)) |
261 | 56.4k | return NULL; /* no debug info. for constants */ |
262 | 2.28M | else { |
263 | 2.28M | int idx = vd->vd.pidx; |
264 | 2.28M | lua_assert(idx < fs->ndebugvars); |
265 | 2.28M | return &fs->f->locvars[idx]; |
266 | 2.28M | } |
267 | 2.34M | } |
268 | | |
269 | | |
270 | | /* |
271 | | ** Create an expression representing variable 'vidx' |
272 | | */ |
273 | 6.42M | static void init_var (FuncState *fs, expdesc *e, int vidx) { |
274 | 6.42M | e->f = e->t = NO_JUMP; |
275 | 6.42M | e->k = VLOCAL; |
276 | 6.42M | e->u.var.vidx = cast_short(vidx); |
277 | 6.42M | e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; |
278 | 6.42M | } |
279 | | |
280 | | |
281 | | /* |
282 | | ** Raises an error if variable described by 'e' is read only |
283 | | */ |
284 | 7.71M | static void check_readonly (LexState *ls, expdesc *e) { |
285 | 7.71M | FuncState *fs = ls->fs; |
286 | 7.71M | TString *varname = NULL; /* to be set if variable is const */ |
287 | 7.71M | 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 | 190k | case VLOCAL: { |
293 | 190k | Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); |
294 | 190k | if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ |
295 | 426 | varname = vardesc->vd.name; |
296 | 190k | break; |
297 | 0 | } |
298 | 318k | case VUPVAL: { |
299 | 318k | Upvaldesc *up = &fs->f->upvalues[e->u.info]; |
300 | 318k | if (up->kind != VDKREG) |
301 | 16 | varname = up->name; |
302 | 318k | break; |
303 | 0 | } |
304 | 7.20M | case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ |
305 | 7.20M | if (e->u.ind.ro) /* read-only? */ |
306 | 8 | varname = tsvalue(&fs->f->k[e->u.ind.keystr]); |
307 | 7.20M | break; |
308 | 7.20M | } |
309 | 7.20M | default: |
310 | 4.57k | lua_assert(e->k == VINDEXI); /* this one doesn't need any check */ |
311 | 4.57k | return; /* integer index cannot be read-only */ |
312 | 7.71M | } |
313 | 7.71M | if (varname) |
314 | 453 | luaK_semerror(ls, "attempt to assign to const variable '%s'", |
315 | 453 | getstr(varname)); |
316 | 7.71M | } |
317 | | |
318 | | |
319 | | /* |
320 | | ** Start the scope for the last 'nvars' created variables. |
321 | | */ |
322 | 2.71M | static void adjustlocalvars (LexState *ls, int nvars) { |
323 | 2.71M | FuncState *fs = ls->fs; |
324 | 2.71M | int reglevel = luaY_nvarstack(fs); |
325 | 2.71M | int i; |
326 | 6.18M | for (i = 0; i < nvars; i++) { |
327 | 3.47M | int vidx = fs->nactvar++; |
328 | 3.47M | Vardesc *var = getlocalvardesc(fs, vidx); |
329 | 3.47M | var->vd.ridx = cast_byte(reglevel++); |
330 | 3.47M | var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); |
331 | 3.47M | luaY_checklimit(fs, reglevel, MAXVARS, "local variables"); |
332 | 3.47M | } |
333 | 2.71M | } |
334 | | |
335 | | |
336 | | /* |
337 | | ** Close the scope for all variables up to level 'tolevel'. |
338 | | ** (debug info.) |
339 | | */ |
340 | 5.86M | static void removevars (FuncState *fs, int tolevel) { |
341 | 5.86M | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); |
342 | 8.16M | while (fs->nactvar > tolevel) { |
343 | 2.29M | LocVar *var = localdebuginfo(fs, --fs->nactvar); |
344 | 2.29M | if (var) /* does it have debug information? */ |
345 | 2.24M | var->endpc = fs->pc; |
346 | 2.29M | } |
347 | 5.86M | } |
348 | | |
349 | | |
350 | | /* |
351 | | ** Search the upvalues of the function 'fs' for one |
352 | | ** with the given 'name'. |
353 | | */ |
354 | 437M | static int searchupvalue (FuncState *fs, TString *name) { |
355 | 437M | int i; |
356 | 437M | Upvaldesc *up = fs->f->upvalues; |
357 | 870M | for (i = 0; i < fs->nups; i++) { |
358 | 538M | if (eqstr(up[i].name, name)) return i; |
359 | 538M | } |
360 | 331M | return -1; /* not found */ |
361 | 437M | } |
362 | | |
363 | | |
364 | 10.2M | static Upvaldesc *allocupvalue (FuncState *fs) { |
365 | 10.2M | Proto *f = fs->f; |
366 | 10.2M | int oldsize = f->sizeupvalues; |
367 | 10.2M | luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); |
368 | 10.2M | luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, |
369 | 10.2M | Upvaldesc, MAXUPVAL, "upvalues"); |
370 | 50.1M | while (oldsize < f->sizeupvalues) |
371 | 39.8M | f->upvalues[oldsize++].name = NULL; |
372 | 10.2M | return &f->upvalues[fs->nups++]; |
373 | 10.2M | } |
374 | | |
375 | | |
376 | 1.19M | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { |
377 | 1.19M | Upvaldesc *up = allocupvalue(fs); |
378 | 1.19M | FuncState *prev = fs->prev; |
379 | 1.19M | if (v->k == VLOCAL) { |
380 | 317k | up->instack = 1; |
381 | 317k | up->idx = v->u.var.ridx; |
382 | 317k | up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; |
383 | 317k | lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); |
384 | 317k | } |
385 | 880k | else { |
386 | 880k | up->instack = 0; |
387 | 880k | up->idx = cast_byte(v->u.info); |
388 | 880k | up->kind = prev->f->upvalues[v->u.info].kind; |
389 | 880k | lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); |
390 | 880k | } |
391 | 1.19M | up->name = name; |
392 | 1.19M | luaC_objbarrier(fs->ls->L, fs->f, name); |
393 | 1.19M | return fs->nups - 1; |
394 | 1.19M | } |
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 | 500M | static int searchvar (FuncState *fs, TString *n, expdesc *var) { |
409 | 500M | int i; |
410 | 1.79G | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { |
411 | 1.35G | Vardesc *vd = getlocalvardesc(fs, i); |
412 | 1.35G | if (varglobal(vd)) { /* global declaration? */ |
413 | 19.4M | if (vd->vd.name == NULL) { /* collective declaration? */ |
414 | 1.34M | if (var->u.info < 0) /* no previous collective declaration? */ |
415 | 146k | var->u.info = fs->firstlocal + i; /* this is the first one */ |
416 | 1.34M | } |
417 | 18.0M | else { /* global name */ |
418 | 18.0M | if (eqstr(n, vd->vd.name)) { /* found? */ |
419 | 11.3k | init_exp(var, VGLOBAL, fs->firstlocal + i); |
420 | 11.3k | return VGLOBAL; |
421 | 11.3k | } |
422 | 18.0M | else if (var->u.info == -1) /* active preambular declaration? */ |
423 | 28.5k | var->u.info = -2; /* invalidate preambular declaration */ |
424 | 18.0M | } |
425 | 19.4M | } |
426 | 1.33G | else if (eqstr(n, vd->vd.name)) { /* found? */ |
427 | 62.7M | if (vd->vd.kind == RDKCTC) /* compile-time constant? */ |
428 | 56.2M | init_exp(var, VCONST, fs->firstlocal + i); |
429 | 6.42M | else /* local variable */ |
430 | 6.42M | init_var(fs, var, i); |
431 | 62.7M | return cast_int(var->k); |
432 | 62.7M | } |
433 | 1.35G | } |
434 | 437M | return -1; /* not found */ |
435 | 500M | } |
436 | | |
437 | | |
438 | | /* |
439 | | ** Mark block where variable at given level was defined |
440 | | ** (to emit close instructions later). |
441 | | */ |
442 | 317k | static void markupval (FuncState *fs, int level) { |
443 | 317k | BlockCnt *bl = fs->bl; |
444 | 336k | while (bl->nactvar > level) |
445 | 18.8k | bl = bl->previous; |
446 | 317k | bl->upval = 1; |
447 | 317k | fs->needclose = 1; |
448 | 317k | } |
449 | | |
450 | | |
451 | | /* |
452 | | ** Mark that current block has a to-be-closed variable. |
453 | | */ |
454 | 51.6k | static void marktobeclosed (FuncState *fs) { |
455 | 51.6k | BlockCnt *bl = fs->bl; |
456 | 51.6k | bl->upval = 1; |
457 | 51.6k | bl->insidetbc = 1; |
458 | 51.6k | fs->needclose = 1; |
459 | 51.6k | } |
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 | 500M | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
468 | 500M | int v = searchvar(fs, n, var); /* look up variables at current level */ |
469 | 500M | if (v >= 0) { /* found? */ |
470 | 62.7M | if (v == VLOCAL && !base) |
471 | 317k | markupval(fs, var->u.var.vidx); /* local will be used as an upval */ |
472 | 62.7M | } |
473 | 437M | else { /* not found at current level; try upvalues */ |
474 | 437M | int idx = searchupvalue(fs, n); /* try existing upvalues */ |
475 | 437M | if (idx < 0) { /* not found? */ |
476 | 331M | if (fs->prev != NULL) /* more levels? */ |
477 | 198M | singlevaraux(fs->prev, n, var, 0); /* try upper levels */ |
478 | 331M | if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ |
479 | 1.19M | idx = newupvalue(fs, n, var); /* will be a new upvalue */ |
480 | 330M | else /* it is a global or a constant */ |
481 | 330M | return; /* don't need to do anything at this level */ |
482 | 331M | } |
483 | 106M | init_exp(var, VUPVAL, idx); /* new or old upvalue */ |
484 | 106M | } |
485 | 500M | } |
486 | | |
487 | | |
488 | 133M | static void buildglobal (LexState *ls, TString *varname, expdesc *var) { |
489 | 133M | FuncState *fs = ls->fs; |
490 | 133M | expdesc key; |
491 | 133M | init_exp(var, VGLOBAL, -1); /* global by default */ |
492 | 133M | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ |
493 | 133M | if (var->k == VGLOBAL) |
494 | 1.54k | luaK_semerror(ls, "_ENV is global when accessing variable '%s'", |
495 | 1.54k | getstr(varname)); |
496 | 133M | luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ |
497 | 133M | codestring(&key, varname); /* key is variable name */ |
498 | 133M | luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ |
499 | 133M | } |
500 | | |
501 | | |
502 | | /* |
503 | | ** Find a variable with the given name 'n', handling global variables |
504 | | ** too. |
505 | | */ |
506 | 168M | static void buildvar (LexState *ls, TString *varname, expdesc *var) { |
507 | 168M | FuncState *fs = ls->fs; |
508 | 168M | init_exp(var, VGLOBAL, -1); /* global by default */ |
509 | 168M | singlevaraux(fs, varname, var, 1); |
510 | 168M | if (var->k == VGLOBAL) { /* global name? */ |
511 | 133M | int info = var->u.info; |
512 | | /* global by default in the scope of a global declaration? */ |
513 | 133M | if (info == -2) |
514 | 2.29k | luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); |
515 | 133M | buildglobal(ls, varname, var); |
516 | 133M | if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) |
517 | 2.30k | var->u.ind.ro = 1; /* mark variable as read-only */ |
518 | 133M | else /* anyway must be a global */ |
519 | 133M | lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG); |
520 | 133M | } |
521 | 168M | } |
522 | | |
523 | | |
524 | 168M | static void singlevar (LexState *ls, expdesc *var) { |
525 | 168M | buildvar(ls, str_checkname(ls), var); |
526 | 168M | } |
527 | | |
528 | | |
529 | | /* |
530 | | ** Adjust the number of results from an expression list 'e' with 'nexps' |
531 | | ** expressions to 'nvars' values. |
532 | | */ |
533 | 1.18M | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { |
534 | 1.18M | FuncState *fs = ls->fs; |
535 | 1.18M | int needed = nvars - nexps; /* extra values needed */ |
536 | 1.18M | if (hasmultret(e->k)) { /* last expression has multiple returns? */ |
537 | 341k | int extra = needed + 1; /* discount last expression itself */ |
538 | 341k | if (extra < 0) |
539 | 10.2k | extra = 0; |
540 | 341k | luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ |
541 | 341k | } |
542 | 842k | else { |
543 | 842k | if (e->k != VVOID) /* at least one expression? */ |
544 | 768k | luaK_exp2nextreg(fs, e); /* close last expression */ |
545 | 842k | if (needed > 0) /* missing values? */ |
546 | 275k | luaK_nil(fs, fs->freereg, needed); /* complete with nils */ |
547 | 842k | } |
548 | 1.18M | if (needed > 0) |
549 | 431k | luaK_reserveregs(fs, needed); /* registers for extra values */ |
550 | 752k | else /* adding 'needed' is actually a subtraction */ |
551 | 752k | fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */ |
552 | 1.18M | } |
553 | | |
554 | | |
555 | 206M | #define enterlevel(ls) luaE_incCstack(ls->L) |
556 | | |
557 | | |
558 | 201M | #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 | 6 | static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { |
566 | 6 | TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; |
567 | 6 | const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; |
568 | 6 | luaK_semerror(ls, |
569 | 6 | "<goto %s> at line %d jumps into the scope of '%s'", |
570 | 6 | getstr(gt->name), gt->line, varname); /* raise the error */ |
571 | 6 | } |
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 | 208k | static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { |
583 | 208k | int i; |
584 | 208k | FuncState *fs = ls->fs; |
585 | 208k | Labellist *gl = &ls->dyd->gt; /* list of gotos */ |
586 | 208k | Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ |
587 | 208k | lua_assert(eqstr(gt->name, label->name)); |
588 | 208k | if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ |
589 | 6 | jumpscopeerror(ls, gt); |
590 | 208k | if (gt->close || |
591 | 208k | (label->nactvar < gt->nactvar && bup)) { /* needs close? */ |
592 | 29.4k | lu_byte stklevel = reglevel(fs, label->nactvar); |
593 | | /* move jump to CLOSE position */ |
594 | 29.4k | fs->f->code[gt->pc + 1] = fs->f->code[gt->pc]; |
595 | | /* put CLOSE instruction at original position */ |
596 | 29.4k | fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0); |
597 | 29.4k | gt->pc++; /* must point to jump instruction */ |
598 | 29.4k | } |
599 | 208k | 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 | 470M | gl->arr[i] = gl->arr[i + 1]; |
602 | 208k | gl->n--; |
603 | 208k | } |
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 | 545k | static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { |
612 | 545k | Dyndata *dyd = ls->dyd; |
613 | 600k | for (; ilb < dyd->label.n; ilb++) { |
614 | 265k | Labeldesc *lb = &dyd->label.arr[ilb]; |
615 | 265k | if (eqstr(lb->name, name)) /* correct label? */ |
616 | 209k | return lb; |
617 | 265k | } |
618 | 335k | return NULL; /* label not found */ |
619 | 545k | } |
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 | 499k | int line, int pc) { |
627 | 499k | int n = l->n; |
628 | 499k | luaM_growvector(ls->L, l->arr, n, l->size, |
629 | 499k | Labeldesc, SHRT_MAX, "labels/gotos"); |
630 | 499k | l->arr[n].name = name; |
631 | 499k | l->arr[n].line = line; |
632 | 499k | l->arr[n].nactvar = ls->fs->nactvar; |
633 | 499k | l->arr[n].close = 0; |
634 | 499k | l->arr[n].pc = pc; |
635 | 499k | l->n = n + 1; |
636 | 499k | return n; |
637 | 499k | } |
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 | 362k | static int newgotoentry (LexState *ls, TString *name, int line) { |
649 | 362k | FuncState *fs = ls->fs; |
650 | 362k | int pc = luaK_jump(fs); /* create jump */ |
651 | 362k | luaK_codeABC(fs, OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */ |
652 | 362k | return newlabelentry(ls, &ls->dyd->gt, name, line, pc); |
653 | 362k | } |
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 | 137k | static void createlabel (LexState *ls, TString *name, int line, int last) { |
664 | 137k | FuncState *fs = ls->fs; |
665 | 137k | Labellist *ll = &ls->dyd->label; |
666 | 137k | int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); |
667 | 137k | if (last) { /* label is last no-op statement in the block? */ |
668 | | /* assume that locals are already out of scope */ |
669 | 4.81k | ll->arr[l].nactvar = fs->bl->nactvar; |
670 | 4.81k | } |
671 | 137k | } |
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.86M | static void solvegotos (FuncState *fs, BlockCnt *bl) { |
682 | 5.86M | LexState *ls = fs->ls; |
683 | 5.86M | Labellist *gl = &ls->dyd->gt; |
684 | 5.86M | int outlevel = reglevel(fs, bl->nactvar); /* level outside the block */ |
685 | 5.86M | int igt = bl->firstgoto; /* first goto in the finishing block */ |
686 | 6.38M | while (igt < gl->n) { /* for each pending goto */ |
687 | 521k | Labeldesc *gt = &gl->arr[igt]; |
688 | | /* search for a matching label in the current block */ |
689 | 521k | Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel); |
690 | 521k | if (lb != NULL) /* found a match? */ |
691 | 208k | closegoto(ls, igt, lb, bl->upval); /* close and remove goto */ |
692 | 312k | else { /* adjust 'goto' for outer block */ |
693 | | /* block has variables to be closed and goto escapes the scope of |
694 | | some variable? */ |
695 | 312k | if (bl->upval && reglevel(fs, gt->nactvar) > outlevel) |
696 | 5.54k | gt->close = 1; /* jump may need a close */ |
697 | 312k | gt->nactvar = bl->nactvar; /* correct level for outer block */ |
698 | 312k | igt++; /* go to next goto */ |
699 | 312k | } |
700 | 521k | } |
701 | 5.86M | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ |
702 | 5.86M | } |
703 | | |
704 | | |
705 | 11.8M | static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { |
706 | 11.8M | bl->isloop = isloop; |
707 | 11.8M | bl->nactvar = fs->nactvar; |
708 | 11.8M | bl->firstlabel = fs->ls->dyd->label.n; |
709 | 11.8M | bl->firstgoto = fs->ls->dyd->gt.n; |
710 | 11.8M | bl->upval = 0; |
711 | | /* inherit 'insidetbc' from enclosing block */ |
712 | 11.8M | bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); |
713 | 11.8M | bl->previous = fs->bl; /* link block in function's block list */ |
714 | 11.8M | fs->bl = bl; |
715 | 11.8M | lua_assert(fs->freereg == luaY_nvarstack(fs)); |
716 | 11.8M | } |
717 | | |
718 | | |
719 | | /* |
720 | | ** generates an error for an undefined 'goto'. |
721 | | */ |
722 | 499 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { |
723 | | /* breaks are checked when created, cannot be undefined */ |
724 | 499 | lua_assert(!eqstr(gt->name, ls->brkn)); |
725 | 499 | luaK_semerror(ls, "no visible label '%s' for <goto> at line %d", |
726 | 499 | getstr(gt->name), gt->line); |
727 | 499 | } |
728 | | |
729 | | |
730 | 5.86M | static void leaveblock (FuncState *fs) { |
731 | 5.86M | BlockCnt *bl = fs->bl; |
732 | 5.86M | LexState *ls = fs->ls; |
733 | 5.86M | lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside block */ |
734 | 5.86M | if (bl->previous && bl->upval) /* need a 'close'? */ |
735 | 44.3k | luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); |
736 | 5.86M | fs->freereg = stklevel; /* free registers */ |
737 | 5.86M | removevars(fs, bl->nactvar); /* remove block locals */ |
738 | 5.86M | lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ |
739 | 5.86M | if (bl->isloop == 2) /* has to fix pending breaks? */ |
740 | 114k | createlabel(ls, ls->brkn, 0, 0); |
741 | 5.86M | solvegotos(fs, bl); |
742 | 5.86M | if (bl->previous == NULL) { /* was it the last block? */ |
743 | 5.06M | if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ |
744 | 499 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ |
745 | 5.06M | } |
746 | 5.86M | fs->bl = bl->previous; /* current block now is previous one */ |
747 | 5.86M | } |
748 | | |
749 | | |
750 | | /* |
751 | | ** adds a new prototype into list of prototypes |
752 | | */ |
753 | 1.70M | static Proto *addprototype (LexState *ls) { |
754 | 1.70M | Proto *clp; |
755 | 1.70M | lua_State *L = ls->L; |
756 | 1.70M | FuncState *fs = ls->fs; |
757 | 1.70M | Proto *f = fs->f; /* prototype of current function */ |
758 | 1.70M | if (fs->np >= f->sizep) { |
759 | 551k | int oldsize = f->sizep; |
760 | 551k | luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); |
761 | 4.15M | while (oldsize < f->sizep) |
762 | 3.60M | f->p[oldsize++] = NULL; |
763 | 551k | } |
764 | 1.70M | f->p[fs->np++] = clp = luaF_newproto(L); |
765 | 1.70M | luaC_objbarrier(L, f, clp); |
766 | 1.70M | return clp; |
767 | 1.70M | } |
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.32M | static void codeclosure (LexState *ls, expdesc *v) { |
778 | 1.32M | FuncState *fs = ls->fs->prev; |
779 | 1.32M | init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); |
780 | 1.32M | luaK_exp2nextreg(fs, v); /* fix it at the last register */ |
781 | 1.32M | } |
782 | | |
783 | | |
784 | 10.7M | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { |
785 | 10.7M | lua_State *L = ls->L; |
786 | 10.7M | Proto *f = fs->f; |
787 | 10.7M | fs->prev = ls->fs; /* linked list of funcstates */ |
788 | 10.7M | fs->ls = ls; |
789 | 10.7M | ls->fs = fs; |
790 | 10.7M | fs->pc = 0; |
791 | 10.7M | fs->previousline = f->linedefined; |
792 | 10.7M | fs->iwthabs = 0; |
793 | 10.7M | fs->lasttarget = 0; |
794 | 10.7M | fs->freereg = 0; |
795 | 10.7M | fs->nk = 0; |
796 | 10.7M | fs->nabslineinfo = 0; |
797 | 10.7M | fs->np = 0; |
798 | 10.7M | fs->nups = 0; |
799 | 10.7M | fs->ndebugvars = 0; |
800 | 10.7M | fs->nactvar = 0; |
801 | 10.7M | fs->needclose = 0; |
802 | 10.7M | fs->firstlocal = ls->dyd->actvar.n; |
803 | 10.7M | fs->firstlabel = ls->dyd->label.n; |
804 | 10.7M | fs->bl = NULL; |
805 | 10.7M | f->source = ls->source; |
806 | 10.7M | luaC_objbarrier(L, f, f->source); |
807 | 10.7M | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
808 | 10.7M | fs->kcache = luaH_new(L); /* create table for function */ |
809 | 10.7M | sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ |
810 | 10.7M | luaD_inctop(L); |
811 | 10.7M | enterblock(fs, bl, 0); |
812 | 10.7M | } |
813 | | |
814 | | |
815 | 5.06M | static void close_func (LexState *ls) { |
816 | 5.06M | lua_State *L = ls->L; |
817 | 5.06M | FuncState *fs = ls->fs; |
818 | 5.06M | Proto *f = fs->f; |
819 | 5.06M | luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ |
820 | 5.06M | leaveblock(fs); |
821 | 5.06M | lua_assert(fs->bl == NULL); |
822 | 5.06M | luaK_finish(fs); |
823 | 5.06M | luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); |
824 | 5.06M | luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); |
825 | 5.06M | luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, |
826 | 5.06M | fs->nabslineinfo, AbsLineInfo); |
827 | 5.06M | luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); |
828 | 5.06M | luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); |
829 | 5.06M | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); |
830 | 5.06M | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); |
831 | 5.06M | ls->fs = fs->prev; |
832 | 5.06M | L->top.p--; /* pop kcache table */ |
833 | 5.06M | luaC_checkGC(L); |
834 | 5.06M | } |
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 | 17.5M | static int block_follow (LexState *ls, int withuntil) { |
850 | 17.5M | switch (ls->t.token) { |
851 | 26.2k | case TK_ELSE: case TK_ELSEIF: |
852 | 4.94M | case TK_END: case TK_EOS: |
853 | 4.94M | return 1; |
854 | 72.6k | case TK_UNTIL: return withuntil; |
855 | 12.5M | default: return 0; |
856 | 17.5M | } |
857 | 17.5M | } |
858 | | |
859 | | |
860 | 11.2M | static void statlist (LexState *ls) { |
861 | | /* statlist -> { stat [';'] } */ |
862 | 22.4M | while (!block_follow(ls, 1)) { |
863 | 11.8M | if (ls->t.token == TK_RETURN) { |
864 | 724k | statement(ls); |
865 | 724k | return; /* 'return' must be last statement */ |
866 | 724k | } |
867 | 11.1M | statement(ls); |
868 | 11.1M | } |
869 | 11.2M | } |
870 | | |
871 | | |
872 | 4.89M | static void fieldsel (LexState *ls, expdesc *v) { |
873 | | /* fieldsel -> ['.' | ':'] NAME */ |
874 | 4.89M | FuncState *fs = ls->fs; |
875 | 4.89M | expdesc key; |
876 | 4.89M | luaK_exp2anyregup(fs, v); |
877 | 4.89M | luaX_next(ls); /* skip the dot or colon */ |
878 | 4.89M | codename(ls, &key); |
879 | 4.89M | luaK_indexed(fs, v, &key); |
880 | 4.89M | } |
881 | | |
882 | | |
883 | 613k | static void yindex (LexState *ls, expdesc *v) { |
884 | | /* index -> '[' expr ']' */ |
885 | 613k | luaX_next(ls); /* skip the '[' */ |
886 | 613k | expr(ls, v); |
887 | 613k | luaK_exp2val(ls->fs, v); |
888 | 613k | checknext(ls, ']'); |
889 | 613k | } |
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.55M | static void recfield (LexState *ls, ConsControl *cc) { |
909 | | /* recfield -> (NAME | '['exp']') = exp */ |
910 | 3.55M | FuncState *fs = ls->fs; |
911 | 3.55M | lu_byte reg = ls->fs->freereg; |
912 | 3.55M | expdesc tab, key, val; |
913 | 3.55M | if (ls->t.token == TK_NAME) |
914 | 3.34M | codename(ls, &key); |
915 | 214k | else /* ls->t.token == '[' */ |
916 | 214k | yindex(ls, &key); |
917 | 3.55M | cc->nh++; |
918 | 3.55M | checknext(ls, '='); |
919 | 3.55M | tab = *cc->t; |
920 | 3.55M | luaK_indexed(fs, &tab, &key); |
921 | 3.55M | expr(ls, &val); |
922 | 3.55M | luaK_storevar(fs, &tab, &val); |
923 | 3.55M | fs->freereg = reg; /* free registers */ |
924 | 3.55M | } |
925 | | |
926 | | |
927 | 10.2M | static void closelistfield (FuncState *fs, ConsControl *cc) { |
928 | 10.2M | if (cc->v.k == VVOID) return; /* there is no list item */ |
929 | 5.56M | luaK_exp2nextreg(fs, &cc->v); |
930 | 5.56M | cc->v.k = VVOID; |
931 | 5.56M | if (cc->tostore >= cc->maxtostore) { |
932 | 159k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ |
933 | 159k | cc->na += cc->tostore; |
934 | 159k | cc->tostore = 0; /* no more items pending */ |
935 | 159k | } |
936 | 5.56M | } |
937 | | |
938 | | |
939 | 386k | static void lastlistfield (FuncState *fs, ConsControl *cc) { |
940 | 386k | if (cc->tostore == 0) return; |
941 | 83.2k | if (hasmultret(cc->v.k)) { |
942 | 40.8k | luaK_setmultret(fs, &cc->v); |
943 | 40.8k | luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); |
944 | 40.8k | cc->na--; /* do not count last expression (unknown number of elements) */ |
945 | 40.8k | } |
946 | 42.3k | else { |
947 | 42.3k | if (cc->v.k != VVOID) |
948 | 40.2k | luaK_exp2nextreg(fs, &cc->v); |
949 | 42.3k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); |
950 | 42.3k | } |
951 | 83.2k | cc->na += cc->tostore; |
952 | 83.2k | } |
953 | | |
954 | | |
955 | 6.69M | static void listfield (LexState *ls, ConsControl *cc) { |
956 | | /* listfield -> exp */ |
957 | 6.69M | expr(ls, &cc->v); |
958 | 6.69M | cc->tostore++; |
959 | 6.69M | } |
960 | | |
961 | | |
962 | 10.2M | static void field (LexState *ls, ConsControl *cc) { |
963 | | /* field -> listfield | recfield */ |
964 | 10.2M | switch(ls->t.token) { |
965 | 9.24M | case TK_NAME: { /* may be 'listfield' or 'recfield' */ |
966 | 9.24M | if (luaX_lookahead(ls) != '=') /* expression? */ |
967 | 5.89M | listfield(ls, cc); |
968 | 3.35M | else |
969 | 3.35M | recfield(ls, cc); |
970 | 9.24M | break; |
971 | 0 | } |
972 | 214k | case '[': { |
973 | 214k | recfield(ls, cc); |
974 | 214k | break; |
975 | 0 | } |
976 | 802k | default: { |
977 | 802k | listfield(ls, cc); |
978 | 802k | break; |
979 | 0 | } |
980 | 10.2M | } |
981 | 10.2M | } |
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.55M | static int maxtostore (FuncState *fs) { |
990 | 1.55M | int numfreeregs = MAX_FSTACK - fs->freereg; |
991 | 1.55M | if (numfreeregs >= 160) /* "lots" of registers? */ |
992 | 1.36M | return numfreeregs / 5; /* use up to 1/5 of them */ |
993 | 189k | else if (numfreeregs >= 80) /* still "enough" registers? */ |
994 | 143k | return 10; /* one 'SETLIST' instruction for each 10 values */ |
995 | 45.0k | else /* save registers for potential more nesting */ |
996 | 45.0k | return 1; |
997 | 1.55M | } |
998 | | |
999 | | |
1000 | 1.55M | static void constructor (LexState *ls, expdesc *t) { |
1001 | | /* constructor -> '{' [ field { sep field } [sep] ] '}' |
1002 | | sep -> ',' | ';' */ |
1003 | 1.55M | FuncState *fs = ls->fs; |
1004 | 1.55M | int line = ls->linenumber; |
1005 | 1.55M | int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0); |
1006 | 1.55M | ConsControl cc; |
1007 | 1.55M | luaK_code(fs, 0); /* space for extra arg. */ |
1008 | 1.55M | cc.na = cc.nh = cc.tostore = 0; |
1009 | 1.55M | cc.t = t; |
1010 | 1.55M | init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ |
1011 | 1.55M | luaK_reserveregs(fs, 1); |
1012 | 1.55M | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
1013 | 1.55M | checknext(ls, '{' /*}*/); |
1014 | 1.55M | cc.maxtostore = maxtostore(fs); |
1015 | 10.5M | do { |
1016 | 10.5M | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
1017 | 10.5M | if (ls->t.token == /*{*/ '}') break; |
1018 | 10.2M | closelistfield(fs, &cc); |
1019 | 10.2M | field(ls, &cc); |
1020 | 10.2M | } while (testnext(ls, ',') || testnext(ls, ';')); |
1021 | 1.55M | check_match(ls, /*{*/ '}', '{' /*}*/, line); |
1022 | 1.55M | lastlistfield(fs, &cc); |
1023 | 1.55M | luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); |
1024 | 1.55M | } |
1025 | | |
1026 | | /* }====================================================================== */ |
1027 | | |
1028 | | |
1029 | 9.10M | static void setvararg (FuncState *fs, int nparams) { |
1030 | 9.10M | fs->f->flag |= PF_ISVARARG; |
1031 | 9.10M | luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); |
1032 | 9.10M | } |
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.53M | do { |
1043 | 1.53M | switch (ls->t.token) { |
1044 | 1.52M | case TK_NAME: { |
1045 | 1.52M | new_localvar(ls, str_checkname(ls)); |
1046 | 1.52M | nparams++; |
1047 | 1.52M | break; |
1048 | 0 | } |
1049 | 7.24k | case TK_DOTS: { |
1050 | 7.24k | luaX_next(ls); |
1051 | 7.24k | isvararg = 1; |
1052 | 7.24k | break; |
1053 | 0 | } |
1054 | 2.51k | default: luaX_syntaxerror(ls, "<name> or '...' expected"); |
1055 | 1.53M | } |
1056 | 1.53M | } while (!isvararg && testnext(ls, ',')); |
1057 | 763k | } |
1058 | 1.69M | adjustlocalvars(ls, nparams); |
1059 | 1.69M | f->numparams = cast_byte(fs->nactvar); |
1060 | 1.69M | if (isvararg) |
1061 | 7.24k | 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.70M | static void body (LexState *ls, expdesc *e, int ismethod, int line) { |
1067 | | /* body -> '(' parlist ')' block END */ |
1068 | 1.70M | FuncState new_fs; |
1069 | 1.70M | BlockCnt bl; |
1070 | 1.70M | new_fs.f = addprototype(ls); |
1071 | 1.70M | new_fs.f->linedefined = line; |
1072 | 1.70M | open_func(ls, &new_fs, &bl); |
1073 | 1.70M | checknext(ls, '('); |
1074 | 1.70M | if (ismethod) { |
1075 | 9.22k | new_localvarliteral(ls, "self"); /* create 'self' parameter */ |
1076 | 9.22k | adjustlocalvars(ls, 1); |
1077 | 9.22k | } |
1078 | 1.70M | parlist(ls); |
1079 | 1.70M | checknext(ls, ')'); |
1080 | 1.70M | statlist(ls); |
1081 | 1.70M | new_fs.f->lastlinedefined = ls->linenumber; |
1082 | 1.70M | check_match(ls, TK_END, TK_FUNCTION, line); |
1083 | 1.70M | codeclosure(ls, e); |
1084 | 1.70M | close_func(ls); |
1085 | 1.70M | } |
1086 | | |
1087 | | |
1088 | 7.33M | static int explist (LexState *ls, expdesc *v) { |
1089 | | /* explist -> expr { ',' expr } */ |
1090 | 7.33M | int n = 1; /* at least one expression */ |
1091 | 7.33M | expr(ls, v); |
1092 | 11.3M | while (testnext(ls, ',')) { |
1093 | 3.99M | luaK_exp2nextreg(ls->fs, v); |
1094 | 3.99M | expr(ls, v); |
1095 | 3.99M | n++; |
1096 | 3.99M | } |
1097 | 7.33M | return n; |
1098 | 7.33M | } |
1099 | | |
1100 | | |
1101 | 5.51M | static void funcargs (LexState *ls, expdesc *f) { |
1102 | 5.51M | FuncState *fs = ls->fs; |
1103 | 5.51M | expdesc args; |
1104 | 5.51M | int base, nparams; |
1105 | 5.51M | int line = ls->linenumber; |
1106 | 5.51M | switch (ls->t.token) { |
1107 | 3.97M | case '(': { /* funcargs -> '(' [ explist ] ')' */ |
1108 | 3.97M | luaX_next(ls); |
1109 | 3.97M | if (ls->t.token == ')') /* arg list is empty? */ |
1110 | 249k | args.k = VVOID; |
1111 | 3.72M | else { |
1112 | 3.72M | explist(ls, &args); |
1113 | 3.72M | if (hasmultret(args.k)) |
1114 | 168k | luaK_setmultret(fs, &args); |
1115 | 3.72M | } |
1116 | 3.97M | check_match(ls, ')', '(', line); |
1117 | 3.97M | break; |
1118 | 0 | } |
1119 | 479k | case '{' /*}*/: { /* funcargs -> constructor */ |
1120 | 479k | constructor(ls, &args); |
1121 | 479k | break; |
1122 | 0 | } |
1123 | 1.03M | case TK_STRING: { /* funcargs -> STRING */ |
1124 | 1.03M | codestring(&args, ls->t.seminfo.ts); |
1125 | 1.03M | luaX_next(ls); /* must use 'seminfo' before 'next' */ |
1126 | 1.03M | break; |
1127 | 0 | } |
1128 | 26.5k | default: { |
1129 | 26.5k | luaX_syntaxerror(ls, "function arguments expected"); |
1130 | 0 | } |
1131 | 5.51M | } |
1132 | 3.36M | lua_assert(f->k == VNONRELOC); |
1133 | 3.36M | base = f->u.info; /* base register for call */ |
1134 | 3.36M | if (hasmultret(args.k)) |
1135 | 166k | nparams = LUA_MULTRET; /* open call */ |
1136 | 3.20M | else { |
1137 | 3.20M | if (args.k != VVOID) |
1138 | 2.95M | luaK_exp2nextreg(fs, &args); /* close last argument */ |
1139 | 3.20M | nparams = fs->freereg - (base+1); |
1140 | 3.20M | } |
1141 | 3.36M | init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); |
1142 | 3.36M | luaK_fixline(fs, line); |
1143 | | /* call removes function and arguments and leaves one result (unless |
1144 | | changed later) */ |
1145 | 3.36M | fs->freereg = cast_byte(base + 1); |
1146 | 3.36M | } |
1147 | | |
1148 | | |
1149 | | |
1150 | | |
1151 | | /* |
1152 | | ** {====================================================================== |
1153 | | ** Expression parsing |
1154 | | ** ======================================================================= |
1155 | | */ |
1156 | | |
1157 | | |
1158 | 172M | static void primaryexp (LexState *ls, expdesc *v) { |
1159 | | /* primaryexp -> NAME | '(' expr ')' */ |
1160 | 172M | switch (ls->t.token) { |
1161 | 955k | case '(': { |
1162 | 955k | int line = ls->linenumber; |
1163 | 955k | luaX_next(ls); |
1164 | 955k | expr(ls, v); |
1165 | 955k | check_match(ls, ')', '(', line); |
1166 | 955k | luaK_dischargevars(ls->fs, v); |
1167 | 955k | return; |
1168 | 0 | } |
1169 | 168M | case TK_NAME: { |
1170 | 168M | singlevar(ls, v); |
1171 | 168M | return; |
1172 | 0 | } |
1173 | 3.36M | default: { |
1174 | 3.36M | luaX_syntaxerror(ls, "unexpected symbol"); |
1175 | 0 | } |
1176 | 172M | } |
1177 | 172M | } |
1178 | | |
1179 | | |
1180 | 172M | static void suffixedexp (LexState *ls, expdesc *v) { |
1181 | | /* suffixedexp -> |
1182 | | primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ |
1183 | 172M | FuncState *fs = ls->fs; |
1184 | 172M | primaryexp(ls, v); |
1185 | 176M | for (;;) { |
1186 | 176M | switch (ls->t.token) { |
1187 | 4.87M | case '.': { /* fieldsel */ |
1188 | 4.87M | fieldsel(ls, v); |
1189 | 4.87M | break; |
1190 | 0 | } |
1191 | 399k | case '[': { /* '[' exp ']' */ |
1192 | 399k | expdesc key; |
1193 | 399k | luaK_exp2anyregup(fs, v); |
1194 | 399k | yindex(ls, &key); |
1195 | 399k | luaK_indexed(fs, v, &key); |
1196 | 399k | break; |
1197 | 0 | } |
1198 | 1.45M | case ':': { /* ':' NAME funcargs */ |
1199 | 1.45M | expdesc key; |
1200 | 1.45M | luaX_next(ls); |
1201 | 1.45M | codename(ls, &key); |
1202 | 1.45M | luaK_self(fs, v, &key); |
1203 | 1.45M | funcargs(ls, v); |
1204 | 1.45M | break; |
1205 | 0 | } |
1206 | 4.16M | case '(': case TK_STRING: case '{' /*}*/: { /* funcargs */ |
1207 | 4.16M | luaK_exp2nextreg(fs, v); |
1208 | 4.16M | funcargs(ls, v); |
1209 | 4.16M | break; |
1210 | 3.70M | } |
1211 | 165M | default: return; |
1212 | 176M | } |
1213 | 176M | } |
1214 | 172M | } |
1215 | | |
1216 | | |
1217 | 178M | static void simpleexp (LexState *ls, expdesc *v) { |
1218 | | /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | |
1219 | | constructor | FUNCTION body | suffixedexp */ |
1220 | 178M | switch (ls->t.token) { |
1221 | 2.56M | case TK_FLT: { |
1222 | 2.56M | init_exp(v, VKFLT, 0); |
1223 | 2.56M | v->u.nval = ls->t.seminfo.r; |
1224 | 2.56M | break; |
1225 | 0 | } |
1226 | 12.6M | case TK_INT: { |
1227 | 12.6M | init_exp(v, VKINT, 0); |
1228 | 12.6M | v->u.ival = ls->t.seminfo.i; |
1229 | 12.6M | break; |
1230 | 0 | } |
1231 | 1.09M | case TK_STRING: { |
1232 | 1.09M | codestring(v, ls->t.seminfo.ts); |
1233 | 1.09M | break; |
1234 | 0 | } |
1235 | 220k | case TK_NIL: { |
1236 | 220k | init_exp(v, VNIL, 0); |
1237 | 220k | break; |
1238 | 0 | } |
1239 | 101k | case TK_TRUE: { |
1240 | 101k | init_exp(v, VTRUE, 0); |
1241 | 101k | break; |
1242 | 0 | } |
1243 | 33.4k | case TK_FALSE: { |
1244 | 33.4k | init_exp(v, VFALSE, 0); |
1245 | 33.4k | break; |
1246 | 0 | } |
1247 | 243k | case TK_DOTS: { /* vararg */ |
1248 | 243k | FuncState *fs = ls->fs; |
1249 | 243k | check_condition(ls, fs->f->flag & PF_ISVARARG, |
1250 | 243k | "cannot use '...' outside a vararg function"); |
1251 | 242k | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); |
1252 | 242k | break; |
1253 | 243k | } |
1254 | 1.07M | case '{' /*}*/: { /* constructor */ |
1255 | 1.07M | constructor(ls, v); |
1256 | 1.07M | return; |
1257 | 243k | } |
1258 | 1.59M | case TK_FUNCTION: { |
1259 | 1.59M | luaX_next(ls); |
1260 | 1.59M | body(ls, v, 0, ls->linenumber); |
1261 | 1.59M | return; |
1262 | 243k | } |
1263 | 159M | default: { |
1264 | 159M | suffixedexp(ls, v); |
1265 | 159M | return; |
1266 | 243k | } |
1267 | 178M | } |
1268 | 16.9M | luaX_next(ls); |
1269 | 16.9M | } |
1270 | | |
1271 | | |
1272 | 189M | static UnOpr getunopr (int op) { |
1273 | 189M | switch (op) { |
1274 | 154k | case TK_NOT: return OPR_NOT; |
1275 | 1.51M | case '-': return OPR_MINUS; |
1276 | 8.94M | case '~': return OPR_BNOT; |
1277 | 254k | case '#': return OPR_LEN; |
1278 | 178M | default: return OPR_NOUNOPR; |
1279 | 189M | } |
1280 | 189M | } |
1281 | | |
1282 | | |
1283 | 185M | static BinOpr getbinopr (int op) { |
1284 | 185M | switch (op) { |
1285 | 651k | case '+': return OPR_ADD; |
1286 | 2.40M | case '-': return OPR_SUB; |
1287 | 6.91M | case '*': return OPR_MUL; |
1288 | 1.33M | case '%': return OPR_MOD; |
1289 | 3.73M | case '^': return OPR_POW; |
1290 | 7.69M | case '/': return OPR_DIV; |
1291 | 622k | case TK_IDIV: return OPR_IDIV; |
1292 | 111k | case '&': return OPR_BAND; |
1293 | 776k | case '|': return OPR_BOR; |
1294 | 4.58M | case '~': return OPR_BXOR; |
1295 | 791k | case TK_SHL: return OPR_SHL; |
1296 | 1.47M | case TK_SHR: return OPR_SHR; |
1297 | 1.03M | case TK_CONCAT: return OPR_CONCAT; |
1298 | 122k | case TK_NE: return OPR_NE; |
1299 | 477k | case TK_EQ: return OPR_EQ; |
1300 | 9.04M | case '<': return OPR_LT; |
1301 | 344k | case TK_LE: return OPR_LE; |
1302 | 121M | case '>': return OPR_GT; |
1303 | 52.8k | case TK_GE: return OPR_GE; |
1304 | 633k | case TK_AND: return OPR_AND; |
1305 | 712k | case TK_OR: return OPR_OR; |
1306 | 20.4M | default: return OPR_NOBINOPR; |
1307 | 185M | } |
1308 | 185M | } |
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 | 10.8M | #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 | 189M | static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { |
1338 | 189M | BinOpr op; |
1339 | 189M | UnOpr uop; |
1340 | 189M | enterlevel(ls); |
1341 | 189M | uop = getunopr(ls->t.token); |
1342 | 189M | if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ |
1343 | 10.8M | int line = ls->linenumber; |
1344 | 10.8M | luaX_next(ls); /* skip operator */ |
1345 | 10.8M | subexpr(ls, v, UNARY_PRIORITY); |
1346 | 10.8M | luaK_prefix(ls->fs, uop, v, line); |
1347 | 10.8M | } |
1348 | 178M | else simpleexp(ls, v); |
1349 | | /* expand while operators have priorities higher than 'limit' */ |
1350 | 189M | op = getbinopr(ls->t.token); |
1351 | 344M | while (op != OPR_NOBINOPR && priority[op].left > limit) { |
1352 | 155M | expdesc v2; |
1353 | 155M | BinOpr nextop; |
1354 | 155M | int line = ls->linenumber; |
1355 | 155M | luaX_next(ls); /* skip operator */ |
1356 | 155M | luaK_infix(ls->fs, op, v); |
1357 | | /* read sub-expression with higher priority */ |
1358 | 155M | nextop = subexpr(ls, &v2, priority[op].right); |
1359 | 155M | luaK_posfix(ls->fs, op, v, &v2, line); |
1360 | 155M | op = nextop; |
1361 | 155M | } |
1362 | 189M | leavelevel(ls); |
1363 | 189M | return op; /* return first untreated operator */ |
1364 | 189M | } |
1365 | | |
1366 | | |
1367 | 23.6M | static void expr (LexState *ls, expdesc *v) { |
1368 | 23.6M | subexpr(ls, v, 0); |
1369 | 23.6M | } |
1370 | | |
1371 | | /* }==================================================================== */ |
1372 | | |
1373 | | |
1374 | | |
1375 | | /* |
1376 | | ** {====================================================================== |
1377 | | ** Rules for Statements |
1378 | | ** ======================================================================= |
1379 | | */ |
1380 | | |
1381 | | |
1382 | 597k | static void block (LexState *ls) { |
1383 | | /* block -> statlist */ |
1384 | 597k | FuncState *fs = ls->fs; |
1385 | 597k | BlockCnt bl; |
1386 | 597k | enterblock(fs, &bl, 0); |
1387 | 597k | statlist(ls); |
1388 | 597k | leaveblock(fs); |
1389 | 597k | } |
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 | 417k | static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { |
1409 | 417k | FuncState *fs = ls->fs; |
1410 | 417k | lu_byte extra = fs->freereg; /* eventual position to save local variable */ |
1411 | 417k | int conflict = 0; |
1412 | 7.97M | for (; lh; lh = lh->prev) { /* check all previous assignments */ |
1413 | 7.56M | if (vkisindexed(lh->v.k)) { /* assignment to table field? */ |
1414 | 5.91M | if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ |
1415 | 1.28M | if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { |
1416 | 1.15M | conflict = 1; /* table is the upvalue being assigned now */ |
1417 | 1.15M | lh->v.k = VINDEXSTR; |
1418 | 1.15M | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1419 | 1.15M | } |
1420 | 1.28M | } |
1421 | 4.62M | else { /* table is a register */ |
1422 | 4.62M | if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { |
1423 | 4.01k | conflict = 1; /* table is the local being assigned now */ |
1424 | 4.01k | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1425 | 4.01k | } |
1426 | | /* is index the local being assigned? */ |
1427 | 4.62M | if (lh->v.k == VINDEXED && v->k == VLOCAL && |
1428 | 4.62M | lh->v.u.ind.idx == v->u.var.ridx) { |
1429 | 463 | conflict = 1; |
1430 | 463 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ |
1431 | 463 | } |
1432 | 4.62M | } |
1433 | 5.91M | } |
1434 | 7.56M | } |
1435 | 417k | if (conflict) { |
1436 | | /* copy upvalue/local value to a temporary (in position 'extra') */ |
1437 | 258k | if (v->k == VLOCAL) |
1438 | 2.04k | luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); |
1439 | 256k | else |
1440 | 256k | luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); |
1441 | 258k | luaK_reserveregs(fs, 1); |
1442 | 258k | } |
1443 | 417k | } |
1444 | | |
1445 | | |
1446 | | /* Create code to store the "top" register in 'var' */ |
1447 | 2.05M | static void storevartop (FuncState *fs, expdesc *var) { |
1448 | 2.05M | expdesc e; |
1449 | 2.05M | init_exp(&e, VNONRELOC, fs->freereg - 1); |
1450 | 2.05M | luaK_storevar(fs, var, &e); /* will also free the top register */ |
1451 | 2.05M | } |
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 | 7.73M | static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { |
1462 | 7.73M | expdesc e; |
1463 | 7.73M | check_condition(ls, vkisvar(lh->v.k), "syntax error"); |
1464 | 7.66M | check_readonly(ls, &lh->v); |
1465 | 7.66M | if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ |
1466 | 5.39M | struct LHS_assign nv; |
1467 | 5.39M | nv.prev = lh; |
1468 | 5.39M | suffixedexp(ls, &nv.v); |
1469 | 5.39M | if (!vkisindexed(nv.v.k)) |
1470 | 417k | check_conflict(ls, lh, &nv.v); |
1471 | 5.39M | enterlevel(ls); /* control recursion depth */ |
1472 | 5.39M | restassign(ls, &nv, nvars+1); |
1473 | 5.39M | leavelevel(ls); |
1474 | 5.39M | } |
1475 | 2.26M | else { /* restassign -> '=' explist */ |
1476 | 2.26M | int nexps; |
1477 | 2.26M | checknext(ls, '='); |
1478 | 2.26M | nexps = explist(ls, &e); |
1479 | 2.26M | if (nexps != nvars) |
1480 | 344k | adjust_assign(ls, nvars, nexps, &e); |
1481 | 1.92M | else { |
1482 | 1.92M | luaK_setoneret(ls->fs, &e); /* close last expression */ |
1483 | 1.92M | luaK_storevar(ls->fs, &lh->v, &e); |
1484 | 1.92M | return; /* avoid default */ |
1485 | 1.92M | } |
1486 | 2.26M | } |
1487 | 5.74M | storevartop(ls->fs, &lh->v); /* default assignment */ |
1488 | 5.74M | } |
1489 | | |
1490 | | |
1491 | 473k | static int cond (LexState *ls) { |
1492 | | /* cond -> exp */ |
1493 | 473k | expdesc v; |
1494 | 473k | expr(ls, &v); /* read condition */ |
1495 | 473k | if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ |
1496 | 473k | luaK_goiftrue(ls->fs, &v); |
1497 | 473k | return v.f; |
1498 | 473k | } |
1499 | | |
1500 | | |
1501 | 232k | static void gotostat (LexState *ls, int line) { |
1502 | 232k | TString *name = str_checkname(ls); /* label's name */ |
1503 | 232k | newgotoentry(ls, name, line); |
1504 | 232k | } |
1505 | | |
1506 | | |
1507 | | /* |
1508 | | ** Break statement. Semantically equivalent to "goto break". |
1509 | | */ |
1510 | 134k | static void breakstat (LexState *ls, int line) { |
1511 | 134k | BlockCnt *bl; /* to look for an enclosing loop */ |
1512 | 507k | for (bl = ls->fs->bl; bl != NULL; bl = bl->previous) { |
1513 | 506k | if (bl->isloop) /* found one? */ |
1514 | 133k | goto ok; |
1515 | 506k | } |
1516 | 874 | luaX_syntaxerror(ls, "break outside loop"); |
1517 | 133k | ok: |
1518 | 133k | bl->isloop = 2; /* signal that block has pending breaks */ |
1519 | 133k | luaX_next(ls); /* skip break */ |
1520 | 133k | newgotoentry(ls, ls->brkn, line); |
1521 | 133k | } |
1522 | | |
1523 | | |
1524 | | /* |
1525 | | ** Check whether there is already a label with the given 'name' at |
1526 | | ** current function. |
1527 | | */ |
1528 | 24.0k | static void checkrepeated (LexState *ls, TString *name) { |
1529 | 24.0k | Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); |
1530 | 24.0k | if (l_unlikely(lb != NULL)) /* already defined? */ |
1531 | 1.20k | luaK_semerror(ls, "label '%s' already defined on line %d", |
1532 | 1.20k | getstr(name), lb->line); /* error */ |
1533 | 24.0k | } |
1534 | | |
1535 | | |
1536 | 24.8k | static void labelstat (LexState *ls, TString *name, int line) { |
1537 | | /* label -> '::' NAME '::' */ |
1538 | 24.8k | checknext(ls, TK_DBCOLON); /* skip double colon */ |
1539 | 40.5k | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) |
1540 | 15.7k | statement(ls); /* skip other no-op statements */ |
1541 | 24.8k | checkrepeated(ls, name); /* check for repeated labels */ |
1542 | 24.8k | createlabel(ls, name, line, block_follow(ls, 0)); |
1543 | 24.8k | } |
1544 | | |
1545 | | |
1546 | 41.0k | static void whilestat (LexState *ls, int line) { |
1547 | | /* whilestat -> WHILE cond DO block END */ |
1548 | 41.0k | FuncState *fs = ls->fs; |
1549 | 41.0k | int whileinit; |
1550 | 41.0k | int condexit; |
1551 | 41.0k | BlockCnt bl; |
1552 | 41.0k | luaX_next(ls); /* skip WHILE */ |
1553 | 41.0k | whileinit = luaK_getlabel(fs); |
1554 | 41.0k | condexit = cond(ls); |
1555 | 41.0k | enterblock(fs, &bl, 1); |
1556 | 41.0k | checknext(ls, TK_DO); |
1557 | 41.0k | block(ls); |
1558 | 41.0k | luaK_jumpto(fs, whileinit); |
1559 | 41.0k | check_match(ls, TK_END, TK_WHILE, line); |
1560 | 41.0k | leaveblock(fs); |
1561 | 41.0k | luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ |
1562 | 41.0k | } |
1563 | | |
1564 | | |
1565 | 83.8k | static void repeatstat (LexState *ls, int line) { |
1566 | | /* repeatstat -> REPEAT block UNTIL cond */ |
1567 | 83.8k | int condexit; |
1568 | 83.8k | FuncState *fs = ls->fs; |
1569 | 83.8k | int repeat_init = luaK_getlabel(fs); |
1570 | 83.8k | BlockCnt bl1, bl2; |
1571 | 83.8k | enterblock(fs, &bl1, 1); /* loop block */ |
1572 | 83.8k | enterblock(fs, &bl2, 0); /* scope block */ |
1573 | 83.8k | luaX_next(ls); /* skip REPEAT */ |
1574 | 83.8k | statlist(ls); |
1575 | 83.8k | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
1576 | 83.8k | condexit = cond(ls); /* read condition (inside scope block) */ |
1577 | 83.8k | leaveblock(fs); /* finish scope */ |
1578 | 83.8k | if (bl2.upval) { /* upvalues? */ |
1579 | 6.23k | int exit = luaK_jump(fs); /* normal exit must jump over fix */ |
1580 | 6.23k | luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ |
1581 | 6.23k | luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); |
1582 | 6.23k | condexit = luaK_jump(fs); /* repeat after closing upvalues */ |
1583 | 6.23k | luaK_patchtohere(fs, exit); /* normal exit comes to here */ |
1584 | 6.23k | } |
1585 | 83.8k | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ |
1586 | 83.8k | leaveblock(fs); /* finish loop */ |
1587 | 83.8k | } |
1588 | | |
1589 | | |
1590 | | /* |
1591 | | ** Read an expression and generate code to put its results in next |
1592 | | ** stack slot. |
1593 | | ** |
1594 | | */ |
1595 | 124k | static void exp1 (LexState *ls) { |
1596 | 124k | expdesc e; |
1597 | 124k | expr(ls, &e); |
1598 | 124k | luaK_exp2nextreg(ls->fs, &e); |
1599 | 124k | lua_assert(e.k == VNONRELOC); |
1600 | 124k | } |
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 | 115k | static void fixforjump (FuncState *fs, int pc, int dest, int back) { |
1609 | 115k | Instruction *jmp = &fs->f->code[pc]; |
1610 | 115k | int offset = dest - (pc + 1); |
1611 | 115k | if (back) |
1612 | 57.8k | offset = -offset; |
1613 | 115k | if (l_unlikely(offset > MAXARG_Bx)) |
1614 | 18 | luaX_syntaxerror(fs->ls, "control structure too long"); |
1615 | 115k | SETARG_Bx(*jmp, offset); |
1616 | 115k | } |
1617 | | |
1618 | | |
1619 | | /* |
1620 | | ** Generate code for a 'for' loop. |
1621 | | */ |
1622 | 80.6k | static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { |
1623 | | /* forbody -> DO block */ |
1624 | 80.6k | static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; |
1625 | 80.6k | static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; |
1626 | 80.6k | BlockCnt bl; |
1627 | 80.6k | FuncState *fs = ls->fs; |
1628 | 80.6k | int prep, endfor; |
1629 | 80.6k | checknext(ls, TK_DO); |
1630 | 80.6k | prep = luaK_codeABx(fs, forprep[isgen], base, 0); |
1631 | 80.6k | fs->freereg--; /* both 'forprep' remove one register from the stack */ |
1632 | 80.6k | enterblock(fs, &bl, 0); /* scope for declared variables */ |
1633 | 80.6k | adjustlocalvars(ls, nvars); |
1634 | 80.6k | luaK_reserveregs(fs, nvars); |
1635 | 80.6k | block(ls); |
1636 | 80.6k | leaveblock(fs); /* end of scope for declared variables */ |
1637 | 80.6k | fixforjump(fs, prep, luaK_getlabel(fs), 0); |
1638 | 80.6k | if (isgen) { /* generic for? */ |
1639 | 36.3k | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); |
1640 | 36.3k | luaK_fixline(fs, line); |
1641 | 36.3k | } |
1642 | 80.6k | endfor = luaK_codeABx(fs, forloop[isgen], base, 0); |
1643 | 80.6k | fixforjump(fs, endfor, prep + 1, 1); |
1644 | 80.6k | luaK_fixline(fs, line); |
1645 | 80.6k | } |
1646 | | |
1647 | | |
1648 | 72.9k | static void fornum (LexState *ls, TString *varname, int line) { |
1649 | | /* fornum -> NAME = exp,exp[,exp] forbody */ |
1650 | 72.9k | FuncState *fs = ls->fs; |
1651 | 72.9k | int base = fs->freereg; |
1652 | 72.9k | new_localvarliteral(ls, "(for state)"); |
1653 | 72.9k | new_localvarliteral(ls, "(for state)"); |
1654 | 72.9k | new_varkind(ls, varname, RDKCONST); /* control variable */ |
1655 | 72.9k | checknext(ls, '='); |
1656 | 72.9k | exp1(ls); /* initial value */ |
1657 | 72.9k | checknext(ls, ','); |
1658 | 72.9k | exp1(ls); /* limit */ |
1659 | 72.9k | if (testnext(ls, ',')) |
1660 | 11.4k | exp1(ls); /* optional step */ |
1661 | 61.4k | else { /* default step = 1 */ |
1662 | 61.4k | luaK_int(fs, fs->freereg, 1); |
1663 | 61.4k | luaK_reserveregs(fs, 1); |
1664 | 61.4k | } |
1665 | 72.9k | adjustlocalvars(ls, 2); /* start scope for internal variables */ |
1666 | 72.9k | forbody(ls, base, line, 1, 0); |
1667 | 72.9k | } |
1668 | | |
1669 | | |
1670 | 44.5k | static void forlist (LexState *ls, TString *indexname) { |
1671 | | /* forlist -> NAME {,NAME} IN explist forbody */ |
1672 | 44.5k | FuncState *fs = ls->fs; |
1673 | 44.5k | expdesc e; |
1674 | 44.5k | int nvars = 4; /* function, state, closing, control */ |
1675 | 44.5k | int line; |
1676 | 44.5k | int base = fs->freereg; |
1677 | | /* create internal variables */ |
1678 | 44.5k | new_localvarliteral(ls, "(for state)"); /* iterator function */ |
1679 | 44.5k | new_localvarliteral(ls, "(for state)"); /* state */ |
1680 | 44.5k | new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ |
1681 | 44.5k | new_varkind(ls, indexname, RDKCONST); /* control variable */ |
1682 | | /* other declared variables */ |
1683 | 81.0k | while (testnext(ls, ',')) { |
1684 | 36.4k | new_localvar(ls, str_checkname(ls)); |
1685 | 36.4k | nvars++; |
1686 | 36.4k | } |
1687 | 44.5k | checknext(ls, TK_IN); |
1688 | 44.5k | line = ls->linenumber; |
1689 | 44.5k | adjust_assign(ls, 4, explist(ls, &e), &e); |
1690 | 44.5k | adjustlocalvars(ls, 3); /* start scope for internal variables */ |
1691 | 44.5k | marktobeclosed(fs); /* last internal var. must be closed */ |
1692 | 44.5k | luaK_checkstack(fs, 2); /* extra space to call iterator */ |
1693 | 44.5k | forbody(ls, base, line, nvars - 3, 1); |
1694 | 44.5k | } |
1695 | | |
1696 | | |
1697 | 135k | static void forstat (LexState *ls, int line) { |
1698 | | /* forstat -> FOR (fornum | forlist) END */ |
1699 | 135k | FuncState *fs = ls->fs; |
1700 | 135k | TString *varname; |
1701 | 135k | BlockCnt bl; |
1702 | 135k | enterblock(fs, &bl, 1); /* scope for loop and control variables */ |
1703 | 135k | luaX_next(ls); /* skip 'for' */ |
1704 | 135k | varname = str_checkname(ls); /* first variable name */ |
1705 | 135k | switch (ls->t.token) { |
1706 | 72.9k | case '=': fornum(ls, varname, line); break; |
1707 | 44.5k | case ',': case TK_IN: forlist(ls, varname); break; |
1708 | 15.7k | default: luaX_syntaxerror(ls, "'=' or 'in' expected"); |
1709 | 135k | } |
1710 | 57.8k | check_match(ls, TK_END, TK_FOR, line); |
1711 | 57.8k | leaveblock(fs); /* loop scope ('break' jumps to this point) */ |
1712 | 57.8k | } |
1713 | | |
1714 | | |
1715 | 354k | static void test_then_block (LexState *ls, int *escapelist) { |
1716 | | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
1717 | 354k | FuncState *fs = ls->fs; |
1718 | 354k | int condtrue; |
1719 | 354k | luaX_next(ls); /* skip IF or ELSEIF */ |
1720 | 354k | condtrue = cond(ls); /* read condition */ |
1721 | 354k | checknext(ls, TK_THEN); |
1722 | 354k | block(ls); /* 'then' part */ |
1723 | 354k | if (ls->t.token == TK_ELSE || |
1724 | 354k | ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ |
1725 | 91.1k | luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ |
1726 | 354k | luaK_patchtohere(fs, condtrue); |
1727 | 354k | } |
1728 | | |
1729 | | |
1730 | 307k | static void ifstat (LexState *ls, int line) { |
1731 | | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ |
1732 | 307k | FuncState *fs = ls->fs; |
1733 | 307k | int escapelist = NO_JUMP; /* exit list for finished parts */ |
1734 | 307k | test_then_block(ls, &escapelist); /* IF cond THEN block */ |
1735 | 354k | while (ls->t.token == TK_ELSEIF) |
1736 | 47.6k | test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ |
1737 | 307k | if (testnext(ls, TK_ELSE)) |
1738 | 43.5k | block(ls); /* 'else' part */ |
1739 | 307k | check_match(ls, TK_END, TK_IF, line); |
1740 | 307k | luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ |
1741 | 307k | } |
1742 | | |
1743 | | |
1744 | 48.5k | static void localfunc (LexState *ls) { |
1745 | 48.5k | expdesc b; |
1746 | 48.5k | FuncState *fs = ls->fs; |
1747 | 48.5k | int fvar = fs->nactvar; /* function's variable index */ |
1748 | 48.5k | new_localvar(ls, str_checkname(ls)); /* new local variable */ |
1749 | 48.5k | adjustlocalvars(ls, 1); /* enter its scope */ |
1750 | 48.5k | body(ls, &b, 0, ls->linenumber); /* function created in next register */ |
1751 | | /* debug information will only see the variable after this point! */ |
1752 | 48.5k | localdebuginfo(fs, fvar)->startpc = fs->pc; |
1753 | 48.5k | } |
1754 | | |
1755 | | |
1756 | 3.44M | static lu_byte getvarattribute (LexState *ls, lu_byte df) { |
1757 | | /* attrib -> ['<' NAME '>'] */ |
1758 | 3.44M | if (testnext(ls, '<')) { |
1759 | 47.0k | TString *ts = str_checkname(ls); |
1760 | 47.0k | const char *attr = getstr(ts); |
1761 | 47.0k | checknext(ls, '>'); |
1762 | 47.0k | if (strcmp(attr, "const") == 0) |
1763 | 22.8k | return RDKCONST; /* read-only variable */ |
1764 | 24.1k | else if (strcmp(attr, "close") == 0) |
1765 | 13.1k | return RDKTOCLOSE; /* to-be-closed variable */ |
1766 | 11.0k | else |
1767 | 11.0k | luaK_semerror(ls, "unknown attribute '%s'", attr); |
1768 | 47.0k | } |
1769 | 3.39M | return df; /* return default value */ |
1770 | 3.44M | } |
1771 | | |
1772 | | |
1773 | 812k | static void checktoclose (FuncState *fs, int level) { |
1774 | 812k | 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 | 812k | } |
1779 | | |
1780 | | |
1781 | 833k | static void localstat (LexState *ls) { |
1782 | | /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */ |
1783 | 833k | FuncState *fs = ls->fs; |
1784 | 833k | int toclose = -1; /* index of to-be-closed variable (if any) */ |
1785 | 833k | Vardesc *var; /* last variable */ |
1786 | 833k | int vidx; /* index of last variable */ |
1787 | 833k | int nvars = 0; |
1788 | 833k | int nexps; |
1789 | 833k | expdesc e; |
1790 | | /* get prefixed attribute (if any); default is regular local variable */ |
1791 | 833k | lu_byte defkind = getvarattribute(ls, VDKREG); |
1792 | 1.98M | do { /* for each variable */ |
1793 | 1.98M | TString *vname = str_checkname(ls); /* get its name */ |
1794 | 1.98M | lu_byte kind = getvarattribute(ls, defkind); /* postfixed attribute */ |
1795 | 1.98M | vidx = new_varkind(ls, vname, kind); /* predeclare it */ |
1796 | 1.98M | if (kind == RDKTOCLOSE) { /* to-be-closed? */ |
1797 | 10.9k | if (toclose != -1) /* one already present? */ |
1798 | 135 | luaK_semerror(ls, "multiple to-be-closed variables in local list"); |
1799 | 10.8k | toclose = fs->nactvar + nvars; |
1800 | 10.8k | } |
1801 | 1.98M | nvars++; |
1802 | 1.98M | } while (testnext(ls, ',')); |
1803 | 833k | if (testnext(ls, '=')) /* initialization? */ |
1804 | 739k | nexps = explist(ls, &e); |
1805 | 94.4k | else { |
1806 | 94.4k | e.k = VVOID; |
1807 | 94.4k | nexps = 0; |
1808 | 94.4k | } |
1809 | 833k | var = getlocalvardesc(fs, vidx); /* retrieve last variable */ |
1810 | 833k | if (nvars == nexps && /* no adjustments? */ |
1811 | 833k | var->vd.kind == RDKCONST && /* last variable is const? */ |
1812 | 833k | luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ |
1813 | 17.2k | var->vd.kind = RDKCTC; /* variable is a compile-time constant */ |
1814 | 17.2k | adjustlocalvars(ls, nvars - 1); /* exclude last variable */ |
1815 | 17.2k | fs->nactvar++; /* but count it */ |
1816 | 17.2k | } |
1817 | 816k | else { |
1818 | 816k | adjust_assign(ls, nvars, nexps, &e); |
1819 | 816k | adjustlocalvars(ls, nvars); |
1820 | 816k | } |
1821 | 833k | checktoclose(fs, toclose); |
1822 | 833k | } |
1823 | | |
1824 | | |
1825 | 630k | static lu_byte getglobalattribute (LexState *ls, lu_byte df) { |
1826 | 630k | lu_byte kind = getvarattribute(ls, df); |
1827 | 630k | switch (kind) { |
1828 | 2.14k | case RDKTOCLOSE: |
1829 | 2.14k | luaK_semerror(ls, "global variables cannot be to-be-closed"); |
1830 | 0 | break; /* to avoid warnings */ |
1831 | 777 | case RDKCONST: |
1832 | 777 | return GDKCONST; /* adjust kind for global variable */ |
1833 | 626k | default: |
1834 | 626k | return kind; |
1835 | 630k | } |
1836 | 630k | } |
1837 | | |
1838 | | |
1839 | 26.6k | static void globalnames (LexState *ls, lu_byte defkind) { |
1840 | 26.6k | FuncState *fs = ls->fs; |
1841 | 26.6k | int nvars = 0; |
1842 | 26.6k | int lastidx; /* index of last registered variable */ |
1843 | 604k | do { /* for each name */ |
1844 | 604k | TString *vname = str_checkname(ls); |
1845 | 604k | lu_byte kind = getglobalattribute(ls, defkind); |
1846 | 604k | lastidx = new_varkind(ls, vname, kind); |
1847 | 604k | nvars++; |
1848 | 604k | } while (testnext(ls, ',')); |
1849 | 26.6k | if (testnext(ls, '=')) { /* initialization? */ |
1850 | 3.35k | expdesc e; |
1851 | 3.35k | int i; |
1852 | 3.35k | int nexps = explist(ls, &e); /* read list of expressions */ |
1853 | 3.35k | adjust_assign(ls, nvars, nexps, &e); |
1854 | 15.4k | for (i = 0; i < nvars; i++) { /* for each variable */ |
1855 | 12.1k | expdesc var; |
1856 | 12.1k | TString *varname = getlocalvardesc(fs, lastidx - i)->vd.name; |
1857 | 12.1k | buildglobal(ls, varname, &var); /* create global variable in 'var' */ |
1858 | 12.1k | storevartop(fs, &var); |
1859 | 12.1k | } |
1860 | 3.35k | } |
1861 | 26.6k | fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ |
1862 | 26.6k | } |
1863 | | |
1864 | | |
1865 | 28.8k | static void globalstat (LexState *ls) { |
1866 | | /* globalstat -> (GLOBAL) attrib '*' |
1867 | | globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ |
1868 | 28.8k | FuncState *fs = ls->fs; |
1869 | | /* get prefixed attribute (if any); default is regular global variable */ |
1870 | 28.8k | lu_byte defkind = getglobalattribute(ls, GDKREG); |
1871 | 28.8k | if (!testnext(ls, '*')) |
1872 | 26.6k | globalnames(ls, defkind); |
1873 | 2.27k | else { |
1874 | | /* use NULL as name to represent '*' entries */ |
1875 | 2.27k | new_varkind(ls, NULL, defkind); |
1876 | 2.27k | fs->nactvar++; /* activate declaration */ |
1877 | 2.27k | } |
1878 | 28.8k | } |
1879 | | |
1880 | | |
1881 | 1.49k | static void globalfunc (LexState *ls, int line) { |
1882 | | /* globalfunc -> (GLOBAL FUNCTION) NAME body */ |
1883 | 1.49k | expdesc var, b; |
1884 | 1.49k | FuncState *fs = ls->fs; |
1885 | 1.49k | TString *fname = str_checkname(ls); |
1886 | 1.49k | new_varkind(ls, fname, GDKREG); /* declare global variable */ |
1887 | 1.49k | fs->nactvar++; /* enter its scope */ |
1888 | 1.49k | buildglobal(ls, fname, &var); |
1889 | 1.49k | body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ |
1890 | 1.49k | luaK_storevar(fs, &var, &b); |
1891 | 1.49k | luaK_fixline(fs, line); /* definition "happens" in the first line */ |
1892 | 1.49k | } |
1893 | | |
1894 | | |
1895 | 30.3k | static void globalstatfunc (LexState *ls, int line) { |
1896 | | /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ |
1897 | 30.3k | luaX_next(ls); /* skip 'global' */ |
1898 | 30.3k | if (testnext(ls, TK_FUNCTION)) |
1899 | 1.49k | globalfunc(ls, line); |
1900 | 28.8k | else |
1901 | 28.8k | globalstat(ls); |
1902 | 30.3k | } |
1903 | | |
1904 | | |
1905 | 65.1k | static int funcname (LexState *ls, expdesc *v) { |
1906 | | /* funcname -> NAME {fieldsel} [':' NAME] */ |
1907 | 65.1k | int ismethod = 0; |
1908 | 65.1k | singlevar(ls, v); |
1909 | 75.1k | while (ls->t.token == '.') |
1910 | 10.0k | fieldsel(ls, v); |
1911 | 65.1k | if (ls->t.token == ':') { |
1912 | 10.2k | ismethod = 1; |
1913 | 10.2k | fieldsel(ls, v); |
1914 | 10.2k | } |
1915 | 65.1k | return ismethod; |
1916 | 65.1k | } |
1917 | | |
1918 | | |
1919 | 65.1k | static void funcstat (LexState *ls, int line) { |
1920 | | /* funcstat -> FUNCTION funcname body */ |
1921 | 65.1k | int ismethod; |
1922 | 65.1k | expdesc v, b; |
1923 | 65.1k | luaX_next(ls); /* skip FUNCTION */ |
1924 | 65.1k | ismethod = funcname(ls, &v); |
1925 | 65.1k | check_readonly(ls, &v); |
1926 | 65.1k | body(ls, &b, ismethod, line); |
1927 | 65.1k | luaK_storevar(ls->fs, &v, &b); |
1928 | 65.1k | luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ |
1929 | 65.1k | } |
1930 | | |
1931 | | |
1932 | 8.01M | static void exprstat (LexState *ls) { |
1933 | | /* stat -> func | assignment */ |
1934 | 8.01M | FuncState *fs = ls->fs; |
1935 | 8.01M | struct LHS_assign v; |
1936 | 8.01M | suffixedexp(ls, &v.v); |
1937 | 8.01M | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ |
1938 | 2.56M | v.prev = NULL; |
1939 | 2.56M | restassign(ls, &v, 1); |
1940 | 2.56M | } |
1941 | 5.45M | else { /* stat -> func */ |
1942 | 5.45M | Instruction *inst; |
1943 | 5.45M | check_condition(ls, v.v.k == VCALL, "syntax error"); |
1944 | 4.74M | inst = &getinstruction(fs, &v.v); |
1945 | 4.74M | SETARG_C(*inst, 1); /* call statement uses no results */ |
1946 | 4.74M | } |
1947 | 8.01M | } |
1948 | | |
1949 | | |
1950 | 724k | static void retstat (LexState *ls) { |
1951 | | /* stat -> RETURN [explist] [';'] */ |
1952 | 724k | FuncState *fs = ls->fs; |
1953 | 724k | expdesc e; |
1954 | 724k | int nret; /* number of values being returned */ |
1955 | 724k | int first = luaY_nvarstack(fs); /* first slot to be returned */ |
1956 | 724k | if (block_follow(ls, 1) || ls->t.token == ';') |
1957 | 66.4k | nret = 0; /* return no values */ |
1958 | 657k | else { |
1959 | 657k | nret = explist(ls, &e); /* optional return values */ |
1960 | 657k | if (hasmultret(e.k)) { |
1961 | 76.4k | luaK_setmultret(fs, &e); |
1962 | 76.4k | if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ |
1963 | 63.9k | SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); |
1964 | 63.9k | lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); |
1965 | 63.9k | } |
1966 | 76.4k | nret = LUA_MULTRET; /* return all values */ |
1967 | 76.4k | } |
1968 | 581k | else { |
1969 | 581k | if (nret == 1) /* only one single value? */ |
1970 | 562k | first = luaK_exp2anyreg(fs, &e); /* can use original slot */ |
1971 | 18.8k | else { /* values must go to the top of the stack */ |
1972 | 18.8k | luaK_exp2nextreg(fs, &e); |
1973 | 18.8k | lua_assert(nret == fs->freereg - first); |
1974 | 18.8k | } |
1975 | 581k | } |
1976 | 657k | } |
1977 | 719k | luaK_ret(fs, first, nret); |
1978 | 719k | testnext(ls, ';'); /* skip optional semicolon */ |
1979 | 719k | } |
1980 | | |
1981 | | |
1982 | 11.9M | static void statement (LexState *ls) { |
1983 | 11.9M | int line = ls->linenumber; /* may be needed for error messages */ |
1984 | 11.9M | enterlevel(ls); |
1985 | 11.9M | 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 | 307k | case TK_IF: { /* stat -> ifstat */ |
1991 | 307k | ifstat(ls, line); |
1992 | 307k | break; |
1993 | 0 | } |
1994 | 41.0k | case TK_WHILE: { /* stat -> whilestat */ |
1995 | 41.0k | whilestat(ls, line); |
1996 | 41.0k | break; |
1997 | 0 | } |
1998 | 97.2k | case TK_DO: { /* stat -> DO block END */ |
1999 | 97.2k | luaX_next(ls); /* skip DO */ |
2000 | 97.2k | block(ls); |
2001 | 97.2k | check_match(ls, TK_END, TK_DO, line); |
2002 | 97.2k | break; |
2003 | 0 | } |
2004 | 135k | case TK_FOR: { /* stat -> forstat */ |
2005 | 135k | forstat(ls, line); |
2006 | 135k | break; |
2007 | 0 | } |
2008 | 83.8k | case TK_REPEAT: { /* stat -> repeatstat */ |
2009 | 83.8k | repeatstat(ls, line); |
2010 | 83.8k | break; |
2011 | 0 | } |
2012 | 65.1k | case TK_FUNCTION: { /* stat -> funcstat */ |
2013 | 65.1k | funcstat(ls, line); |
2014 | 65.1k | break; |
2015 | 0 | } |
2016 | 882k | case TK_LOCAL: { /* stat -> localstat */ |
2017 | 882k | luaX_next(ls); /* skip LOCAL */ |
2018 | 882k | if (testnext(ls, TK_FUNCTION)) /* local function? */ |
2019 | 48.5k | localfunc(ls); |
2020 | 833k | else |
2021 | 833k | localstat(ls); |
2022 | 882k | break; |
2023 | 0 | } |
2024 | 0 | case TK_GLOBAL: { /* stat -> globalstatfunc */ |
2025 | 0 | globalstatfunc(ls, line); |
2026 | 0 | break; |
2027 | 0 | } |
2028 | 29.1k | case TK_DBCOLON: { /* stat -> label */ |
2029 | 29.1k | luaX_next(ls); /* skip double colon */ |
2030 | 29.1k | labelstat(ls, str_checkname(ls), line); |
2031 | 29.1k | break; |
2032 | 0 | } |
2033 | 724k | case TK_RETURN: { /* stat -> retstat */ |
2034 | 724k | luaX_next(ls); /* skip RETURN */ |
2035 | 724k | retstat(ls); |
2036 | 724k | break; |
2037 | 0 | } |
2038 | 134k | case TK_BREAK: { /* stat -> breakstat */ |
2039 | 134k | breakstat(ls, line); |
2040 | 134k | break; |
2041 | 0 | } |
2042 | 232k | case TK_GOTO: { /* stat -> 'goto' NAME */ |
2043 | 232k | luaX_next(ls); /* skip 'goto' */ |
2044 | 232k | gotostat(ls, line); |
2045 | 232k | 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 | 31.3k | int lk = luaX_lookahead(ls); |
2053 | 31.3k | if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { |
2054 | | /* 'global <attrib>' or 'global name' or 'global *' or |
2055 | | 'global function' */ |
2056 | 30.3k | globalstatfunc(ls, line); |
2057 | 30.3k | break; |
2058 | 30.3k | } |
2059 | 31.3k | } /* else... */ |
2060 | 4.89M | } |
2061 | 4.86M | #endif |
2062 | | /* FALLTHROUGH */ |
2063 | 8.01M | default: { /* stat -> func | assignment */ |
2064 | 8.01M | exprstat(ls); |
2065 | 8.01M | break; |
2066 | 4.89M | } |
2067 | 11.9M | } |
2068 | 6.32M | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && |
2069 | 6.32M | ls->fs->freereg >= luaY_nvarstack(ls->fs)); |
2070 | 6.32M | ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ |
2071 | 6.32M | leavelevel(ls); |
2072 | 6.32M | } |
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 | 9.09M | static void mainfunc (LexState *ls, FuncState *fs) { |
2084 | 9.09M | BlockCnt bl; |
2085 | 9.09M | Upvaldesc *env; |
2086 | 9.09M | open_func(ls, fs, &bl); |
2087 | 9.09M | setvararg(fs, 0); /* main function is always declared vararg */ |
2088 | 9.09M | env = allocupvalue(fs); /* ...set environment upvalue */ |
2089 | 9.09M | env->instack = 1; |
2090 | 9.09M | env->idx = 0; |
2091 | 9.09M | env->kind = VDKREG; |
2092 | 9.09M | env->name = ls->envn; |
2093 | 9.09M | luaC_objbarrier(ls->L, fs->f, env->name); |
2094 | 9.09M | luaX_next(ls); /* read first token */ |
2095 | 9.09M | statlist(ls); /* parse main body */ |
2096 | 9.09M | check(ls, TK_EOS); |
2097 | 9.09M | close_func(ls); |
2098 | 9.09M | } |
2099 | | |
2100 | | |
2101 | | LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, |
2102 | 9.09M | Dyndata *dyd, const char *name, int firstchar) { |
2103 | 9.09M | LexState lexstate; |
2104 | 9.09M | FuncState funcstate; |
2105 | 9.09M | LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ |
2106 | 9.09M | setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ |
2107 | 9.09M | luaD_inctop(L); |
2108 | 9.09M | lexstate.h = luaH_new(L); /* create table for scanner */ |
2109 | 9.09M | sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ |
2110 | 9.09M | luaD_inctop(L); |
2111 | 9.09M | funcstate.f = cl->p = luaF_newproto(L); |
2112 | 9.09M | luaC_objbarrier(L, cl, cl->p); |
2113 | 9.09M | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ |
2114 | 9.09M | luaC_objbarrier(L, funcstate.f, funcstate.f->source); |
2115 | 9.09M | lexstate.buff = buff; |
2116 | 9.09M | lexstate.dyd = dyd; |
2117 | 9.09M | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; |
2118 | 9.09M | luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); |
2119 | 9.09M | mainfunc(&lexstate, &funcstate); |
2120 | 9.09M | lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); |
2121 | | /* all scopes should be correctly finished */ |
2122 | 3.73M | lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); |
2123 | 3.73M | L->top.p--; /* remove scanner's table */ |
2124 | 3.73M | return cl; /* closure is on the stack, too */ |
2125 | 3.73M | } |
2126 | | |