Coverage Report

Created: 2025-12-11 06:33

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