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