Coverage Report

Created: 2025-10-13 06:32

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