Coverage Report

Created: 2025-08-29 06:37

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