Coverage Report

Created: 2025-08-25 07:03

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