Coverage Report

Created: 2025-10-10 06:41

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.04M
#define MAXVARS   200
36
37
38
9.32M
#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.20G
#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
574k
static l_noret error_expected (LexState *ls, int token) {
69
574k
  luaX_syntaxerror(ls,
70
574k
      luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
71
574k
}
72
73
74
8.81k
static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
75
8.81k
  lua_State *L = fs->ls->L;
76
8.81k
  const char *msg;
77
8.81k
  int line = fs->f->linedefined;
78
8.81k
  const char *where = (line == 0)
79
8.81k
                      ? "main function"
80
8.81k
                      : luaO_pushfstring(L, "function at line %d", line);
81
8.81k
  msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
82
8.81k
                             what, limit, where);
83
8.81k
  luaX_syntaxerror(fs->ls, msg);
84
8.81k
}
85
86
87
36.0M
void luaY_checklimit (FuncState *fs, int v, int l, const char *what) {
88
36.0M
  if (l_unlikely(v > l)) errorlimit(fs, l, what);
89
36.0M
}
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
25.9M
    luaX_next(ls);
98
25.9M
    return 1;
99
25.9M
  }
100
16.3M
  else return 0;
101
42.3M
}
102
103
104
/*
105
** Check that next token is 'c'.
106
*/
107
222M
static void check (LexState *ls, int c) {
108
222M
  if (ls->t.token != c)
109
316k
    error_expected(ls, c);
110
222M
}
111
112
113
/*
114
** Check that next token is 'c' and skip it.
115
*/
116
11.8M
static void checknext (LexState *ls, int c) {
117
11.8M
  check(ls, c);
118
11.8M
  luaX_next(ls);
119
11.8M
}
120
121
122
16.0M
#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.66M
static void check_match (LexState *ls, int what, int who, int where) {
131
5.66M
  if (l_unlikely(!testnext(ls, what))) {
132
264k
    if (where == ls->linenumber)  /* all in the same line? */
133
257k
      error_expected(ls, what);  /* do not need a complex message */
134
6.91k
    else {
135
6.91k
      luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
136
6.91k
             "%s expected (to close %s at line %d)",
137
6.91k
              luaX_token2str(ls, what), luaX_token2str(ls, who), where));
138
6.91k
    }
139
264k
  }
140
5.66M
}
141
142
143
207M
static TString *str_checkname (LexState *ls) {
144
207M
  TString *ts;
145
207M
  check(ls, TK_NAME);
146
207M
  ts = ls->t.seminfo.ts;
147
207M
  luaX_next(ls);
148
207M
  return ts;
149
207M
}
150
151
152
557M
static void init_exp (expdesc *e, expkind k, int i) {
153
557M
  e->f = e->t = NO_JUMP;
154
557M
  e->k = k;
155
557M
  e->u.info = i;
156
557M
}
157
158
159
168M
static void codestring (expdesc *e, TString *s) {
160
168M
  e->f = e->t = NO_JUMP;
161
168M
  e->k = VKSTR;
162
168M
  e->u.strval = s;
163
168M
}
164
165
166
11.0M
static void codename (LexState *ls, expdesc *e) {
167
11.0M
  codestring(e, str_checkname(ls));
168
11.0M
}
169
170
171
/*
172
** Register a new local variable in the active 'Proto' (for debug
173
** information).
174
*/
175
static short registerlocalvar (LexState *ls, FuncState *fs,
176
4.04M
                               TString *varname) {
177
4.04M
  Proto *f = fs->f;
178
4.04M
  int oldsize = f->sizelocvars;
179
4.04M
  luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars,
180
4.04M
                  LocVar, SHRT_MAX, "local variables");
181
11.2M
  while (oldsize < f->sizelocvars)
182
7.20M
    f->locvars[oldsize++].varname = NULL;
183
4.04M
  f->locvars[fs->ndebugvars].varname = varname;
184
4.04M
  f->locvars[fs->ndebugvars].startpc = fs->pc;
185
4.04M
  luaC_objbarrier(ls->L, f, varname);
186
4.04M
  return fs->ndebugvars++;
187
4.04M
}
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.42M
static int new_varkind (LexState *ls, TString *name, lu_byte kind) {
195
5.42M
  lua_State *L = ls->L;
196
5.42M
  FuncState *fs = ls->fs;
197
5.42M
  Dyndata *dyd = ls->dyd;
198
5.42M
  Vardesc *var;
199
5.42M
  luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
200
5.42M
             dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations");
201
5.42M
  var = &dyd->actvar.arr[dyd->actvar.n++];
202
5.42M
  var->vd.kind = kind;  /* default */
203
5.42M
  var->vd.name = name;
204
5.42M
  return dyd->actvar.n - 1 - fs->firstlocal;
205
5.42M
}
206
207
208
/*
209
** Create a new local variable with the given 'name' and regular kind.
210
*/
211
2.50M
static int new_localvar (LexState *ls, TString *name) {
212
2.50M
  return new_varkind(ls, name, VDKREG);
213
2.50M
}
214
215
#define new_localvarliteral(ls,v) \
216
248k
    new_localvar(ls,  \
217
248k
      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.07G
static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
227
2.07G
  return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx];
228
2.07G
}
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
519M
static lu_byte reglevel (FuncState *fs, int nvar) {
237
733M
  while (nvar-- > 0) {
238
457M
    Vardesc *vd = getlocalvardesc(fs, nvar);  /* get previous variable */
239
457M
    if (varinreg(vd))  /* is in a register? */
240
243M
      return cast_byte(vd->vd.ridx + 1);
241
457M
  }
242
275M
  return 0;  /* no variables in registers */
243
519M
}
244
245
246
/*
247
** Return the number of variables in the register stack for the given
248
** function.
249
*/
250
508M
lu_byte luaY_nvarstack (FuncState *fs) {
251
508M
  return reglevel(fs, fs->nactvar);
252
508M
}
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
137k
    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
9.77M
static void init_var (FuncState *fs, expdesc *e, int vidx) {
274
9.77M
  e->f = e->t = NO_JUMP;
275
9.77M
  e->k = VLOCAL;
276
9.77M
  e->u.var.vidx = cast_short(vidx);
277
9.77M
  e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx;
278
9.77M
}
279
280
281
/*
282
** Raises an error if variable described by 'e' is read only
283
*/
284
8.03M
static void check_readonly (LexState *ls, expdesc *e) {
285
8.03M
  FuncState *fs = ls->fs;
286
8.03M
  TString *varname = NULL;  /* to be set if variable is const */
287
8.03M
  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
214k
    case VLOCAL: {
293
214k
      Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx);
294
214k
      if (vardesc->vd.kind != VDKREG)  /* not a regular variable? */
295
428
        varname = vardesc->vd.name;
296
214k
      break;
297
0
    }
298
373k
    case VUPVAL: {
299
373k
      Upvaldesc *up = &fs->f->upvalues[e->u.info];
300
373k
      if (up->kind != VDKREG)
301
4
        varname = up->name;
302
373k
      break;
303
0
    }
304
7.43M
    case VINDEXUP: case VINDEXSTR: case VINDEXED: {  /* global variable */
305
7.43M
      if (e->u.ind.ro)  /* read-only? */
306
465
        varname = tsvalue(&fs->f->k[e->u.ind.keystr]);
307
7.43M
      break;
308
7.43M
    }
309
7.43M
    default:
310
7.27k
      lua_assert(e->k == VINDEXI);  /* this one doesn't need any check */
311
7.27k
      return;  /* integer index cannot be read-only */
312
8.03M
  }
313
8.02M
  if (varname)
314
900
    luaK_semerror(ls, "attempt to assign to const variable '%s'",
315
900
                      getstr(varname));
316
8.02M
}
317
318
319
/*
320
** Start the scope for the last 'nvars' created variables.
321
*/
322
2.73M
static void adjustlocalvars (LexState *ls, int nvars) {
323
2.73M
  FuncState *fs = ls->fs;
324
2.73M
  int reglevel = luaY_nvarstack(fs);
325
2.73M
  int i;
326
6.78M
  for (i = 0; i < nvars; i++) {
327
4.04M
    int vidx = fs->nactvar++;
328
4.04M
    Vardesc *var = getlocalvardesc(fs, vidx);
329
4.04M
    var->vd.ridx = cast_byte(reglevel++);
330
4.04M
    var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
331
4.04M
    luaY_checklimit(fs, reglevel, MAXVARS, "local variables");
332
4.04M
  }
333
2.73M
}
334
335
336
/*
337
** Close the scope for all variables up to level 'tolevel'.
338
** (debug info.)
339
*/
340
5.51M
static void removevars (FuncState *fs, int tolevel) {
341
5.51M
  fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
342
7.97M
  while (fs->nactvar > tolevel) {
343
2.45M
    LocVar *var = localdebuginfo(fs, --fs->nactvar);
344
2.45M
    if (var)  /* does it have debug information? */
345
2.31M
      var->endpc = fs->pc;
346
2.45M
  }
347
5.51M
}
348
349
350
/*
351
** Search the upvalues of the function 'fs' for one
352
** with the given 'name'.
353
*/
354
495M
static int searchupvalue (FuncState *fs, TString *name) {
355
495M
  int i;
356
495M
  Upvaldesc *up = fs->f->upvalues;
357
986M
  for (i = 0; i < fs->nups; i++) {
358
613M
    if (eqstr(up[i].name, name)) return i;
359
613M
  }
360
373M
  return -1;  /* not found */
361
495M
}
362
363
364
9.48M
static Upvaldesc *allocupvalue (FuncState *fs) {
365
9.48M
  Proto *f = fs->f;
366
9.48M
  int oldsize = f->sizeupvalues;
367
9.48M
  luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
368
9.48M
  luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
369
9.48M
                  Upvaldesc, MAXUPVAL, "upvalues");
370
46.0M
  while (oldsize < f->sizeupvalues)
371
36.5M
    f->upvalues[oldsize++].name = NULL;
372
9.48M
  return &f->upvalues[fs->nups++];
373
9.48M
}
374
375
376
1.24M
static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
377
1.24M
  Upvaldesc *up = allocupvalue(fs);
378
1.24M
  FuncState *prev = fs->prev;
379
1.24M
  if (v->k == VLOCAL) {
380
339k
    up->instack = 1;
381
339k
    up->idx = v->u.var.ridx;
382
339k
    up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind;
383
339k
    lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name));
384
339k
  }
385
906k
  else {
386
906k
    up->instack = 0;
387
906k
    up->idx = cast_byte(v->u.info);
388
906k
    up->kind = prev->f->upvalues[v->u.info].kind;
389
906k
    lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name));
390
906k
  }
391
1.24M
  up->name = name;
392
1.24M
  luaC_objbarrier(fs->ls->L, fs->f, name);
393
1.24M
  return fs->nups - 1;
394
1.24M
}
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
564M
static int searchvar (FuncState *fs, TString *n, expdesc *var) {
409
564M
  int i;
410
2.09G
  for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
411
1.60G
    Vardesc *vd = getlocalvardesc(fs, i);
412
1.60G
    if (varglobal(vd)) {  /* global declaration? */
413
53.4M
      if (vd->vd.name == NULL) {  /* collective declaration? */
414
5.83M
        if (var->u.info < 0)  /* no previous collective declaration? */
415
69.9k
          var->u.info = fs->firstlocal + i;  /* this is the first one */
416
5.83M
      }
417
47.6M
      else {  /* global name */
418
47.6M
        if (eqstr(n, vd->vd.name)) {  /* found? */
419
13.2k
          init_exp(var, VGLOBAL, fs->firstlocal + i);
420
13.2k
          return VGLOBAL;
421
13.2k
        }
422
47.6M
        else if (var->u.info == -1)  /* active preambular declaration? */
423
43.1k
          var->u.info = -2;  /* invalidate preambular declaration */
424
47.6M
      }
425
53.4M
    }
426
1.54G
    else if (eqstr(n, vd->vd.name)) {  /* found? */
427
68.3M
      if (vd->vd.kind == RDKCTC)  /* compile-time constant? */
428
58.5M
        init_exp(var, VCONST, fs->firstlocal + i);
429
9.77M
      else  /* local variable */
430
9.77M
        init_var(fs, var, i);
431
68.3M
      return cast_int(var->k);
432
68.3M
    }
433
1.60G
  }
434
495M
  return -1;  /* not found */
435
564M
}
436
437
438
/*
439
** Mark block where variable at given level was defined
440
** (to emit close instructions later).
441
*/
442
339k
static void markupval (FuncState *fs, int level) {
443
339k
  BlockCnt *bl = fs->bl;
444
357k
  while (bl->nactvar > level)
445
18.3k
    bl = bl->previous;
446
339k
  bl->upval = 1;
447
339k
  fs->needclose = 1;
448
339k
}
449
450
451
/*
452
** Mark that current block has a to-be-closed variable.
453
*/
454
45.1k
static void marktobeclosed (FuncState *fs) {
455
45.1k
  BlockCnt *bl = fs->bl;
456
45.1k
  bl->upval = 1;
457
45.1k
  bl->insidetbc = 1;
458
45.1k
  fs->needclose = 1;
459
45.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
564M
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
468
564M
  int v = searchvar(fs, n, var);  /* look up variables at current level */
469
564M
  if (v >= 0) {  /* found? */
470
68.3M
    if (v == VLOCAL && !base)
471
339k
      markupval(fs, var->u.var.vidx);  /* local will be used as an upval */
472
68.3M
  }
473
495M
  else {  /* not found at current level; try upvalues */
474
495M
    int idx = searchupvalue(fs, n);  /* try existing upvalues */
475
495M
    if (idx < 0) {  /* not found? */
476
373M
      if (fs->prev != NULL)  /* more levels? */
477
217M
        singlevaraux(fs->prev, n, var, 0);  /* try upper levels */
478
373M
      if (var->k == VLOCAL || var->k == VUPVAL)  /* local or upvalue? */
479
1.24M
        idx  = newupvalue(fs, n, var);  /* will be a new upvalue */
480
372M
      else  /* it is a global or a constant */
481
372M
        return;  /* don't need to do anything at this level */
482
373M
    }
483
123M
    init_exp(var, VUPVAL, idx);  /* new or old upvalue */
484
123M
  }
485
564M
}
486
487
488
155M
static void buildglobal (LexState *ls, TString *varname, expdesc *var) {
489
155M
  FuncState *fs = ls->fs;
490
155M
  expdesc key;
491
155M
  init_exp(var, VGLOBAL, -1);  /* global by default */
492
155M
  singlevaraux(fs, ls->envn, var, 1);  /* get environment variable */
493
155M
  if (var->k == VGLOBAL)
494
727
    luaK_semerror(ls, "_ENV is global when accessing variable '%s'",
495
727
                      getstr(varname));
496
155M
  luaK_exp2anyregup(fs, var);  /* _ENV could be a constant */
497
155M
  codestring(&key, varname);  /* key is variable name */
498
155M
  luaK_indexed(fs, var, &key);  /* 'var' represents _ENV[varname] */
499
155M
}
500
501
502
/*
503
** Find a variable with the given name 'n', handling global variables
504
** too.
505
*/
506
190M
static void buildvar (LexState *ls, TString *varname, expdesc *var) {
507
190M
  FuncState *fs = ls->fs;
508
190M
  init_exp(var, VGLOBAL, -1);  /* global by default */
509
190M
  singlevaraux(fs, varname, var, 1);
510
190M
  if (var->k == VGLOBAL) {  /* global name? */
511
155M
    int info = var->u.info;
512
    /* global by default in the scope of a global declaration? */
513
155M
    if (info == -2)
514
3.77k
      luaK_semerror(ls, "variable '%s' not declared", getstr(varname));
515
155M
    buildglobal(ls, varname, var);
516
155M
    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
155M
    else  /* anyway must be a global */
519
155M
      lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG);
520
155M
  }
521
190M
}
522
523
524
190M
static void singlevar (LexState *ls, expdesc *var) {
525
190M
  buildvar(ls, str_checkname(ls), var);
526
190M
}
527
528
529
/*
530
** Adjust the number of results from an expression list 'e' with 'nexps'
531
** expressions to 'nvars' values.
532
*/
533
1.24M
static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
534
1.24M
  FuncState *fs = ls->fs;
535
1.24M
  int needed = nvars - nexps;  /* extra values needed */
536
1.24M
  if (hasmultret(e->k)) {  /* last expression has multiple returns? */
537
401k
    int extra = needed + 1;  /* discount last expression itself */
538
401k
    if (extra < 0)
539
8.40k
      extra = 0;
540
401k
    luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
541
401k
  }
542
847k
  else {
543
847k
    if (e->k != VVOID)  /* at least one expression? */
544
777k
      luaK_exp2nextreg(fs, e);  /* close last expression */
545
847k
    if (needed > 0)  /* missing values? */
546
293k
      luaK_nil(fs, fs->freereg, needed);  /* complete with nils */
547
847k
  }
548
1.24M
  if (needed > 0)
549
500k
    luaK_reserveregs(fs, needed);  /* registers for extra values */
550
748k
  else  /* adding 'needed' is actually a subtraction */
551
748k
    fs->freereg = cast_byte(fs->freereg + needed);  /* remove extra values */
552
1.24M
}
553
554
555
232M
#define enterlevel(ls)  luaE_incCstack(ls->L)
556
557
558
227M
#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
26.1k
    lu_byte stklevel = reglevel(fs, label->nactvar);
593
    /* move jump to CLOSE position */
594
26.1k
    fs->f->code[gt->pc + 1] = fs->f->code[gt->pc];
595
    /* put CLOSE instruction at original position */
596
26.1k
    fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0);
597
26.1k
    gt->pc++;  /* must point to jump instruction */
598
26.1k
  }
599
218k
  luaK_patchlist(ls->fs, gt->pc, label->pc);  /* goto jumps to label */
600
581M
  for (i = g; i < gl->n - 1; i++)  /* remove goto from pending list */
601
581M
    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
599k
static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) {
612
599k
  Dyndata *dyd = ls->dyd;
613
722k
  for (; ilb < dyd->label.n; ilb++) {
614
343k
    Labeldesc *lb = &dyd->label.arr[ilb];
615
343k
    if (eqstr(lb->name, name))  /* correct label? */
616
220k
      return lb;
617
343k
  }
618
379k
  return NULL;  /* label not found */
619
599k
}
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
590k
                          int line, int pc) {
627
590k
  int n = l->n;
628
590k
  luaM_growvector(ls->L, l->arr, n, l->size,
629
590k
                  Labeldesc, SHRT_MAX, "labels/gotos");
630
590k
  l->arr[n].name = name;
631
590k
  l->arr[n].line = line;
632
590k
  l->arr[n].nactvar = ls->fs->nactvar;
633
590k
  l->arr[n].close = 0;
634
590k
  l->arr[n].pc = pc;
635
590k
  l->n = n + 1;
636
590k
  return n;
637
590k
}
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
414k
static int newgotoentry (LexState *ls, TString *name, int line) {
649
414k
  FuncState *fs = ls->fs;
650
414k
  int pc = luaK_jump(fs);  /* create jump */
651
414k
  luaK_codeABC(fs, OP_CLOSE, 0, 1, 0);  /* spaceholder, marked as dead */
652
414k
  return newlabelentry(ls, &ls->dyd->gt, name, line, pc);
653
414k
}
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
176k
static void createlabel (LexState *ls, TString *name, int line, int last) {
664
176k
  FuncState *fs = ls->fs;
665
176k
  Labellist *ll = &ls->dyd->label;
666
176k
  int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs));
667
176k
  if (last) {  /* label is last no-op statement in the block? */
668
    /* assume that locals are already out of scope */
669
4.19k
    ll->arr[l].nactvar = fs->bl->nactvar;
670
4.19k
  }
671
176k
}
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.51M
static void solvegotos (FuncState *fs, BlockCnt *bl) {
682
5.51M
  LexState *ls = fs->ls;
683
5.51M
  Labellist *gl = &ls->dyd->gt;
684
5.51M
  int outlevel = reglevel(fs, bl->nactvar);  /* level outside the block */
685
5.51M
  int igt = bl->firstgoto;  /* first goto in the finishing block */
686
6.05M
  while (igt < gl->n) {   /* for each pending goto */
687
533k
    Labeldesc *gt = &gl->arr[igt];
688
    /* search for a matching label in the current block */
689
533k
    Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel);
690
533k
    if (lb != NULL)  /* found a match? */
691
218k
      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.16k
        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
533k
  }
701
5.51M
  ls->dyd->label.n = bl->firstlabel;  /* remove local labels */
702
5.51M
}
703
704
705
10.8M
static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
706
10.8M
  bl->isloop = isloop;
707
10.8M
  bl->nactvar = fs->nactvar;
708
10.8M
  bl->firstlabel = fs->ls->dyd->label.n;
709
10.8M
  bl->firstgoto = fs->ls->dyd->gt.n;
710
10.8M
  bl->upval = 0;
711
  /* inherit 'insidetbc' from enclosing block */
712
10.8M
  bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc);
713
10.8M
  bl->previous = fs->bl;  /* link block in function's block list */
714
10.8M
  fs->bl = bl;
715
10.8M
  lua_assert(fs->freereg == luaY_nvarstack(fs));
716
10.8M
}
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.51M
static void leaveblock (FuncState *fs) {
731
5.51M
  BlockCnt *bl = fs->bl;
732
5.51M
  LexState *ls = fs->ls;
733
5.51M
  lu_byte stklevel = reglevel(fs, bl->nactvar);  /* level outside block */
734
5.51M
  if (bl->previous && bl->upval)  /* need a 'close'? */
735
40.6k
    luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0);
736
5.51M
  fs->freereg = stklevel;  /* free registers */
737
5.51M
  removevars(fs, bl->nactvar);  /* remove block locals */
738
5.51M
  lua_assert(bl->nactvar == fs->nactvar);  /* back to level on entry */
739
5.51M
  if (bl->isloop == 2)  /* has to fix pending breaks? */
740
111k
    createlabel(ls, ls->brkn, 0, 0);
741
5.51M
  solvegotos(fs, bl);
742
5.51M
  if (bl->previous == NULL) {  /* was it the last block? */
743
4.70M
    if (bl->firstgoto < ls->dyd->gt.n)  /* still pending gotos? */
744
274
      undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]);  /* error */
745
4.70M
  }
746
5.51M
  fs->bl = bl->previous;  /* current block now is previous one */
747
5.51M
}
748
749
750
/*
751
** adds a new prototype into list of prototypes
752
*/
753
1.69M
static Proto *addprototype (LexState *ls) {
754
1.69M
  Proto *clp;
755
1.69M
  lua_State *L = ls->L;
756
1.69M
  FuncState *fs = ls->fs;
757
1.69M
  Proto *f = fs->f;  /* prototype of current function */
758
1.69M
  if (fs->np >= f->sizep) {
759
539k
    int oldsize = f->sizep;
760
539k
    luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
761
4.07M
    while (oldsize < f->sizep)
762
3.53M
      f->p[oldsize++] = NULL;
763
539k
  }
764
1.69M
  f->p[fs->np++] = clp = luaF_newproto(L);
765
1.69M
  luaC_objbarrier(L, f, clp);
766
1.69M
  return clp;
767
1.69M
}
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.34M
static void codeclosure (LexState *ls, expdesc *v) {
778
1.34M
  FuncState *fs = ls->fs->prev;
779
1.34M
  init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
780
1.34M
  luaK_exp2nextreg(fs, v);  /* fix it at the last register */
781
1.34M
}
782
783
784
9.94M
static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
785
9.94M
  lua_State *L = ls->L;
786
9.94M
  Proto *f = fs->f;
787
9.94M
  fs->prev = ls->fs;  /* linked list of funcstates */
788
9.94M
  fs->ls = ls;
789
9.94M
  ls->fs = fs;
790
9.94M
  fs->pc = 0;
791
9.94M
  fs->previousline = f->linedefined;
792
9.94M
  fs->iwthabs = 0;
793
9.94M
  fs->lasttarget = 0;
794
9.94M
  fs->freereg = 0;
795
9.94M
  fs->nk = 0;
796
9.94M
  fs->nabslineinfo = 0;
797
9.94M
  fs->np = 0;
798
9.94M
  fs->nups = 0;
799
9.94M
  fs->ndebugvars = 0;
800
9.94M
  fs->nactvar = 0;
801
9.94M
  fs->needclose = 0;
802
9.94M
  fs->firstlocal = ls->dyd->actvar.n;
803
9.94M
  fs->firstlabel = ls->dyd->label.n;
804
9.94M
  fs->bl = NULL;
805
9.94M
  f->source = ls->source;
806
9.94M
  luaC_objbarrier(L, f, f->source);
807
9.94M
  f->maxstacksize = 2;  /* registers 0/1 are always valid */
808
9.94M
  fs->kcache = luaH_new(L);  /* create table for function */
809
9.94M
  sethvalue2s(L, L->top.p, fs->kcache);  /* anchor it */
810
9.94M
  luaD_inctop(L);
811
9.94M
  enterblock(fs, bl, 0);
812
9.94M
}
813
814
815
4.70M
static void close_func (LexState *ls) {
816
4.70M
  lua_State *L = ls->L;
817
4.70M
  FuncState *fs = ls->fs;
818
4.70M
  Proto *f = fs->f;
819
4.70M
  luaK_ret(fs, luaY_nvarstack(fs), 0);  /* final return */
820
4.70M
  leaveblock(fs);
821
4.70M
  lua_assert(fs->bl == NULL);
822
4.70M
  luaK_finish(fs);
823
4.70M
  luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction);
824
4.70M
  luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte);
825
4.70M
  luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo,
826
4.70M
                       fs->nabslineinfo, AbsLineInfo);
827
4.70M
  luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue);
828
4.70M
  luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *);
829
4.70M
  luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
830
4.70M
  luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
831
4.70M
  ls->fs = fs->prev;
832
4.70M
  L->top.p--;  /* pop kcache table */
833
4.70M
  luaC_checkGC(L);
834
4.70M
}
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.5M
static int block_follow (LexState *ls, int withuntil) {
850
17.5M
  switch (ls->t.token) {
851
23.0k
    case TK_ELSE: case TK_ELSEIF:
852
4.55M
    case TK_END: case TK_EOS:
853
4.55M
      return 1;
854
72.4k
    case TK_UNTIL: return withuntil;
855
12.8M
    default: return 0;
856
17.5M
  }
857
17.5M
}
858
859
860
10.4M
static void statlist (LexState *ls) {
861
  /* statlist -> { stat [';'] } */
862
21.8M
  while (!block_follow(ls, 1)) {
863
12.1M
    if (ls->t.token == TK_RETURN) {
864
760k
      statement(ls);
865
760k
      return;  /* 'return' must be last statement */
866
760k
    }
867
11.3M
    statement(ls);
868
11.3M
  }
869
10.4M
}
870
871
872
7.09M
static void fieldsel (LexState *ls, expdesc *v) {
873
  /* fieldsel -> ['.' | ':'] NAME */
874
7.09M
  FuncState *fs = ls->fs;
875
7.09M
  expdesc key;
876
7.09M
  luaK_exp2anyregup(fs, v);
877
7.09M
  luaX_next(ls);  /* skip the dot or colon */
878
7.09M
  codename(ls, &key);
879
7.09M
  luaK_indexed(fs, v, &key);
880
7.09M
}
881
882
883
436k
static void yindex (LexState *ls, expdesc *v) {
884
  /* index -> '[' expr ']' */
885
436k
  luaX_next(ls);  /* skip the '[' */
886
436k
  expr(ls, v);
887
436k
  luaK_exp2val(ls->fs, v);
888
436k
  checknext(ls, ']');
889
436k
}
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
9.90M
#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.28M
static void recfield (LexState *ls, ConsControl *cc) {
922
  /* recfield -> (NAME | '['exp']') = exp */
923
3.28M
  FuncState *fs = ls->fs;
924
3.28M
  lu_byte reg = ls->fs->freereg;
925
3.28M
  expdesc tab, key, val;
926
3.28M
  if (ls->t.token == TK_NAME)
927
3.14M
    codename(ls, &key);
928
136k
  else  /* ls->t.token == '[' */
929
136k
    yindex(ls, &key);
930
3.28M
  cc->nh++;
931
3.28M
  checknext(ls, '=');
932
3.28M
  tab = *cc->t;
933
3.28M
  luaK_indexed(fs, &tab, &key);
934
3.28M
  expr(ls, &val);
935
3.28M
  luaK_storevar(fs, &tab, &val);
936
3.28M
  fs->freereg = reg;  /* free registers */
937
3.28M
}
938
939
940
5.18M
static void closelistfield (FuncState *fs, ConsControl *cc) {
941
5.18M
  lua_assert(cc->tostore > 0);
942
5.18M
  luaK_exp2nextreg(fs, &cc->v);
943
5.18M
  cc->v.k = VVOID;
944
5.18M
  if (cc->tostore >= cc->maxtostore) {
945
155k
    luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);  /* flush */
946
155k
    cc->na += cc->tostore;
947
155k
    cc->tostore = 0;  /* no more items pending */
948
155k
  }
949
5.18M
}
950
951
952
435k
static void lastlistfield (FuncState *fs, ConsControl *cc) {
953
435k
  if (cc->tostore == 0) return;
954
87.9k
  if (hasmultret(cc->v.k)) {
955
38.8k
    luaK_setmultret(fs, &cc->v);
956
38.8k
    luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET);
957
38.8k
    cc->na--;  /* do not count last expression (unknown number of elements) */
958
38.8k
  }
959
49.0k
  else {
960
49.0k
    if (cc->v.k != VVOID)
961
46.3k
      luaK_exp2nextreg(fs, &cc->v);
962
49.0k
    luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
963
49.0k
  }
964
87.9k
  cc->na += cc->tostore;
965
87.9k
}
966
967
968
6.59M
static void listfield (LexState *ls, ConsControl *cc) {
969
  /* listfield -> exp */
970
6.59M
  expr(ls, &cc->v);
971
6.59M
  cc->tostore++;
972
6.59M
}
973
974
975
9.89M
static void field (LexState *ls, ConsControl *cc) {
976
  /* field -> listfield | recfield */
977
9.89M
  switch(ls->t.token) {
978
8.69M
    case TK_NAME: {  /* may be 'listfield' or 'recfield' */
979
8.69M
      if (luaX_lookahead(ls) != '=')  /* expression? */
980
5.53M
        listfield(ls, cc);
981
3.15M
      else
982
3.15M
        recfield(ls, cc);
983
8.69M
      break;
984
0
    }
985
136k
    case '[': {
986
136k
      recfield(ls, cc);
987
136k
      break;
988
0
    }
989
1.06M
    default: {
990
1.06M
      listfield(ls, cc);
991
1.06M
      break;
992
0
    }
993
9.89M
  }
994
9.89M
}
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.80M
static int maxtostore (FuncState *fs) {
1003
1.80M
  int numfreeregs = MAX_FSTACK - fs->freereg;
1004
1.80M
  if (numfreeregs >= 160)  /* "lots" of registers? */
1005
1.49M
    return numfreeregs / 5;  /* use up to 1/5 of them */
1006
307k
  else if (numfreeregs >= 80)  /* still "enough" registers? */
1007
232k
    return 10;  /* one 'SETLIST' instruction for each 10 values */
1008
75.2k
  else  /* save registers for potential more nesting */
1009
75.2k
    return 1;
1010
1.80M
}
1011
1012
1013
1.81M
static void constructor (LexState *ls, expdesc *t) {
1014
  /* constructor -> '{' [ field { sep field } [sep] ] '}'
1015
     sep -> ',' | ';' */
1016
1.81M
  FuncState *fs = ls->fs;
1017
1.81M
  int line = ls->linenumber;
1018
1.81M
  int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0);
1019
1.81M
  ConsControl cc;
1020
1.81M
  luaK_code(fs, 0);  /* space for extra arg. */
1021
1.81M
  cc.na = cc.nh = cc.tostore = 0;
1022
1.81M
  cc.t = t;
1023
1.81M
  init_exp(t, VNONRELOC, fs->freereg);  /* table will be at stack top */
1024
1.81M
  luaK_reserveregs(fs, 1);
1025
1.81M
  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
1026
1.81M
  checknext(ls, '{' /*}*/);
1027
1.81M
  cc.maxtostore = maxtostore(fs);
1028
10.2M
  do {
1029
10.2M
    if (ls->t.token == /*{*/ '}') break;
1030
9.90M
    if (cc.v.k != VVOID)  /* is there a previous list item? */
1031
5.18M
      closelistfield(fs, &cc);  /* close it */
1032
9.90M
    field(ls, &cc);
1033
9.90M
    luaY_checklimit(fs, cc.tostore + cc.na + cc.nh, MAX_CNST,
1034
9.90M
                    "items in a constructor");
1035
9.90M
  } while (testnext(ls, ',') || testnext(ls, ';'));
1036
1.81M
  check_match(ls, /*{*/ '}', '{' /*}*/, line);
1037
1.81M
  lastlistfield(fs, &cc);
1038
1.81M
  luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh);
1039
1.81M
}
1040
1041
/* }====================================================================== */
1042
1043
1044
8.24M
static void setvararg (FuncState *fs, int nparams) {
1045
8.24M
  fs->f->flag |= PF_ISVARARG;
1046
8.24M
  luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0);
1047
8.24M
}
1048
1049
1050
1.69M
static void parlist (LexState *ls) {
1051
  /* parlist -> [ {NAME ','} (NAME | '...') ] */
1052
1.69M
  FuncState *fs = ls->fs;
1053
1.69M
  Proto *f = fs->f;
1054
1.69M
  int nparams = 0;
1055
1.69M
  int isvararg = 0;
1056
1.69M
  if (ls->t.token != ')') {  /* is 'parlist' not empty? */
1057
2.17M
    do {
1058
2.17M
      switch (ls->t.token) {
1059
2.16M
        case TK_NAME: {
1060
2.16M
          new_localvar(ls, str_checkname(ls));
1061
2.16M
          nparams++;
1062
2.16M
          break;
1063
0
        }
1064
5.48k
        case TK_DOTS: {
1065
5.48k
          luaX_next(ls);
1066
5.48k
          isvararg = 1;
1067
5.48k
          break;
1068
0
        }
1069
7.58k
        default: luaX_syntaxerror(ls, "<name> or '...' expected");
1070
2.17M
      }
1071
2.17M
    } while (!isvararg && testnext(ls, ','));
1072
774k
  }
1073
1.68M
  adjustlocalvars(ls, nparams);
1074
1.68M
  f->numparams = cast_byte(fs->nactvar);
1075
1.68M
  if (isvararg)
1076
5.48k
    setvararg(fs, f->numparams);  /* declared vararg */
1077
1.68M
  luaK_reserveregs(fs, fs->nactvar);  /* reserve registers for parameters */
1078
1.68M
}
1079
1080
1081
1.69M
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
1082
  /* body ->  '(' parlist ')' block END */
1083
1.69M
  FuncState new_fs;
1084
1.69M
  BlockCnt bl;
1085
1.69M
  new_fs.f = addprototype(ls);
1086
1.69M
  new_fs.f->linedefined = line;
1087
1.69M
  open_func(ls, &new_fs, &bl);
1088
1.69M
  checknext(ls, '(');
1089
1.69M
  if (ismethod) {
1090
7.04k
    new_localvarliteral(ls, "self");  /* create 'self' parameter */
1091
7.04k
    adjustlocalvars(ls, 1);
1092
7.04k
  }
1093
1.69M
  parlist(ls);
1094
1.69M
  checknext(ls, ')');
1095
1.69M
  statlist(ls);
1096
1.69M
  new_fs.f->lastlinedefined = ls->linenumber;
1097
1.69M
  check_match(ls, TK_END, TK_FUNCTION, line);
1098
1.69M
  codeclosure(ls, e);
1099
1.69M
  close_func(ls);
1100
1.69M
}
1101
1102
1103
7.19M
static int explist (LexState *ls, expdesc *v) {
1104
  /* explist -> expr { ',' expr } */
1105
7.19M
  int n = 1;  /* at least one expression */
1106
7.19M
  expr(ls, v);
1107
9.33M
  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
7.19M
  return n;
1113
7.19M
}
1114
1115
1116
5.42M
static void funcargs (LexState *ls, expdesc *f) {
1117
5.42M
  FuncState *fs = ls->fs;
1118
5.42M
  expdesc args;
1119
5.42M
  int base, nparams;
1120
5.42M
  int line = ls->linenumber;
1121
5.42M
  switch (ls->t.token) {
1122
3.63M
    case '(': {  /* funcargs -> '(' [ explist ] ')' */
1123
3.63M
      luaX_next(ls);
1124
3.63M
      if (ls->t.token == ')')  /* arg list is empty? */
1125
241k
        args.k = VVOID;
1126
3.39M
      else {
1127
3.39M
        explist(ls, &args);
1128
3.39M
        if (hasmultret(args.k))
1129
297k
          luaK_setmultret(fs, &args);
1130
3.39M
      }
1131
3.63M
      check_match(ls, ')', '(', line);
1132
3.63M
      break;
1133
0
    }
1134
517k
    case '{' /*}*/: {  /* funcargs -> constructor */
1135
517k
      constructor(ls, &args);
1136
517k
      break;
1137
0
    }
1138
1.24M
    case TK_STRING: {  /* funcargs -> STRING */
1139
1.24M
      codestring(&args, ls->t.seminfo.ts);
1140
1.24M
      luaX_next(ls);  /* must use 'seminfo' before 'next' */
1141
1.24M
      break;
1142
0
    }
1143
27.5k
    default: {
1144
27.5k
      luaX_syntaxerror(ls, "function arguments expected");
1145
0
    }
1146
5.42M
  }
1147
3.90M
  lua_assert(f->k == VNONRELOC);
1148
3.90M
  base = f->u.info;  /* base register for call */
1149
3.90M
  if (hasmultret(args.k))
1150
290k
    nparams = LUA_MULTRET;  /* open call */
1151
3.61M
  else {
1152
3.61M
    if (args.k != VVOID)
1153
3.37M
      luaK_exp2nextreg(fs, &args);  /* close last argument */
1154
3.61M
    nparams = fs->freereg - (base+1);
1155
3.61M
  }
1156
3.90M
  init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
1157
3.90M
  luaK_fixline(fs, line);
1158
  /* call removes function and arguments and leaves one result (unless
1159
     changed later) */
1160
3.90M
  fs->freereg = cast_byte(base + 1);
1161
3.90M
}
1162
1163
1164
1165
1166
/*
1167
** {======================================================================
1168
** Expression parsing
1169
** =======================================================================
1170
*/
1171
1172
1173
194M
static void primaryexp (LexState *ls, expdesc *v) {
1174
  /* primaryexp -> NAME | '(' expr ')' */
1175
194M
  switch (ls->t.token) {
1176
970k
    case '(': {
1177
970k
      int line = ls->linenumber;
1178
970k
      luaX_next(ls);
1179
970k
      expr(ls, v);
1180
970k
      check_match(ls, ')', '(', line);
1181
970k
      luaK_dischargevars(ls->fs, v);
1182
970k
      return;
1183
0
    }
1184
190M
    case TK_NAME: {
1185
190M
      singlevar(ls, v);
1186
190M
      return;
1187
0
    }
1188
3.10M
    default: {
1189
3.10M
      luaX_syntaxerror(ls, "unexpected symbol");
1190
0
    }
1191
194M
  }
1192
194M
}
1193
1194
1195
194M
static void suffixedexp (LexState *ls, expdesc *v) {
1196
  /* suffixedexp ->
1197
       primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
1198
194M
  FuncState *fs = ls->fs;
1199
194M
  primaryexp(ls, v);
1200
202M
  for (;;) {
1201
202M
    switch (ls->t.token) {
1202
7.07M
      case '.': {  /* fieldsel */
1203
7.07M
        fieldsel(ls, v);
1204
7.07M
        break;
1205
0
      }
1206
300k
      case '[': {  /* '[' exp ']' */
1207
300k
        expdesc key;
1208
300k
        luaK_exp2anyregup(fs, v);
1209
300k
        yindex(ls, &key);
1210
300k
        luaK_indexed(fs, v, &key);
1211
300k
        break;
1212
0
      }
1213
854k
      case ':': {  /* ':' NAME funcargs */
1214
854k
        expdesc key;
1215
854k
        luaX_next(ls);
1216
854k
        codename(ls, &key);
1217
854k
        luaK_self(fs, v, &key);
1218
854k
        funcargs(ls, v);
1219
854k
        break;
1220
0
      }
1221
4.66M
      case '(': case TK_STRING: case '{' /*}*/: {  /* funcargs */
1222
4.66M
        luaK_exp2nextreg(fs, v);
1223
4.66M
        funcargs(ls, v);
1224
4.66M
        break;
1225
4.16M
      }
1226
189M
      default: return;
1227
202M
    }
1228
202M
  }
1229
194M
}
1230
1231
1232
201M
static void simpleexp (LexState *ls, expdesc *v) {
1233
  /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... |
1234
                  constructor | FUNCTION body | suffixedexp */
1235
201M
  switch (ls->t.token) {
1236
2.17M
    case TK_FLT: {
1237
2.17M
      init_exp(v, VKFLT, 0);
1238
2.17M
      v->u.nval = ls->t.seminfo.r;
1239
2.17M
      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
322k
    case TK_NIL: {
1251
322k
      init_exp(v, VNIL, 0);
1252
322k
      break;
1253
0
    }
1254
131k
    case TK_TRUE: {
1255
131k
      init_exp(v, VTRUE, 0);
1256
131k
      break;
1257
0
    }
1258
37.5k
    case TK_FALSE: {
1259
37.5k
      init_exp(v, VFALSE, 0);
1260
37.5k
      break;
1261
0
    }
1262
375k
    case TK_DOTS: {  /* vararg */
1263
375k
      FuncState *fs = ls->fs;
1264
375k
      check_condition(ls, fs->f->flag & PF_ISVARARG,
1265
375k
                      "cannot use '...' outside a vararg function");
1266
374k
      init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1));
1267
374k
      break;
1268
375k
    }
1269
1.30M
    case '{' /*}*/: {  /* constructor */
1270
1.30M
      constructor(ls, v);
1271
1.30M
      return;
1272
375k
    }
1273
1.59M
    case TK_FUNCTION: {
1274
1.59M
      luaX_next(ls);
1275
1.59M
      body(ls, v, 0, ls->linenumber);
1276
1.59M
      return;
1277
375k
    }
1278
181M
    default: {
1279
181M
      suffixedexp(ls, v);
1280
181M
      return;
1281
375k
    }
1282
201M
  }
1283
17.0M
  luaX_next(ls);
1284
17.0M
}
1285
1286
1287
214M
static UnOpr getunopr (int op) {
1288
214M
  switch (op) {
1289
178k
    case TK_NOT: return OPR_NOT;
1290
1.82M
    case '-': return OPR_MINUS;
1291
11.3M
    case '~': return OPR_BNOT;
1292
246k
    case '#': return OPR_LEN;
1293
201M
    default: return OPR_NOUNOPR;
1294
214M
  }
1295
214M
}
1296
1297
1298
211M
static BinOpr getbinopr (int op) {
1299
211M
  switch (op) {
1300
1.00M
    case '+': return OPR_ADD;
1301
2.95M
    case '-': return OPR_SUB;
1302
8.21M
    case '*': return OPR_MUL;
1303
1.37M
    case '%': return OPR_MOD;
1304
4.16M
    case '^': return OPR_POW;
1305
10.7M
    case '/': return OPR_DIV;
1306
595k
    case TK_IDIV: return OPR_IDIV;
1307
143k
    case '&': return OPR_BAND;
1308
942k
    case '|': return OPR_BOR;
1309
4.98M
    case '~': return OPR_BXOR;
1310
1.13M
    case TK_SHL: return OPR_SHL;
1311
1.51M
    case TK_SHR: return OPR_SHR;
1312
1.50M
    case TK_CONCAT: return OPR_CONCAT;
1313
121k
    case TK_NE: return OPR_NE;
1314
546k
    case TK_EQ: return OPR_EQ;
1315
14.7M
    case '<': return OPR_LT;
1316
311k
    case TK_LE: return OPR_LE;
1317
136M
    case '>': return OPR_GT;
1318
53.9k
    case TK_GE: return OPR_GE;
1319
712k
    case TK_AND: return OPR_AND;
1320
954k
    case TK_OR: return OPR_OR;
1321
18.6M
    default: return OPR_NOBINOPR;
1322
211M
  }
1323
211M
}
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.6M
#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
214M
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
1353
214M
  BinOpr op;
1354
214M
  UnOpr uop;
1355
214M
  enterlevel(ls);
1356
214M
  uop = getunopr(ls->t.token);
1357
214M
  if (uop != OPR_NOUNOPR) {  /* prefix (unary) operator? */
1358
13.6M
    int line = ls->linenumber;
1359
13.6M
    luaX_next(ls);  /* skip operator */
1360
13.6M
    subexpr(ls, v, UNARY_PRIORITY);
1361
13.6M
    luaK_prefix(ls->fs, uop, v, line);
1362
13.6M
  }
1363
201M
  else simpleexp(ls, v);
1364
  /* expand while operators have priorities higher than 'limit' */
1365
214M
  op = getbinopr(ls->t.token);
1366
394M
  while (op != OPR_NOBINOPR && priority[op].left > limit) {
1367
180M
    expdesc v2;
1368
180M
    BinOpr nextop;
1369
180M
    int line = ls->linenumber;
1370
180M
    luaX_next(ls);  /* skip operator */
1371
180M
    luaK_infix(ls->fs, op, v);
1372
    /* read sub-expression with higher priority */
1373
180M
    nextop = subexpr(ls, &v2, priority[op].right);
1374
180M
    luaK_posfix(ls->fs, op, v, &v2, line);
1375
180M
    op = nextop;
1376
180M
  }
1377
214M
  leavelevel(ls);
1378
214M
  return op;  /* return first untreated operator */
1379
214M
}
1380
1381
1382
21.1M
static void expr (LexState *ls, expdesc *v) {
1383
21.1M
  subexpr(ls, v, 0);
1384
21.1M
}
1385
1386
/* }==================================================================== */
1387
1388
1389
1390
/*
1391
** {======================================================================
1392
** Rules for Statements
1393
** =======================================================================
1394
*/
1395
1396
1397
575k
static void block (LexState *ls) {
1398
  /* block -> statlist */
1399
575k
  FuncState *fs = ls->fs;
1400
575k
  BlockCnt bl;
1401
575k
  enterblock(fs, &bl, 0);
1402
575k
  statlist(ls);
1403
575k
  leaveblock(fs);
1404
575k
}
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.71M
  for (; lh; lh = lh->prev) {  /* check all previous assignments */
1428
6.25M
    if (vkisindexed(lh->v.k)) {  /* assignment to table field? */
1429
4.95M
      if (lh->v.k == VINDEXUP) {  /* is table an upvalue? */
1430
1.43M
        if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) {
1431
1.31M
          conflict = 1;  /* table is the upvalue being assigned now */
1432
1.31M
          lh->v.k = VINDEXSTR;
1433
1.31M
          lh->v.u.ind.t = extra;  /* assignment will use safe copy */
1434
1.31M
        }
1435
1.43M
      }
1436
3.52M
      else {  /* table is a register */
1437
3.52M
        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.52M
        if (lh->v.k == VINDEXED && v->k == VLOCAL &&
1443
12.1k
            lh->v.u.ind.idx == v->u.var.ridx) {
1444
604
          conflict = 1;
1445
604
          lh->v.u.ind.idx = extra;  /* previous assignment will use safe copy */
1446
604
        }
1447
3.52M
      }
1448
4.95M
    }
1449
6.25M
  }
1450
465k
  if (conflict) {
1451
    /* copy upvalue/local value to a temporary (in position 'extra') */
1452
291k
    if (v->k == VLOCAL)
1453
1.89k
      luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0);
1454
289k
    else
1455
289k
      luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0);
1456
291k
    luaK_reserveregs(fs, 1);
1457
291k
  }
1458
465k
}
1459
1460
1461
/* Create code to store the "top" register in 'var' */
1462
3.95M
static void storevartop (FuncState *fs, expdesc *var) {
1463
3.95M
  expdesc e;
1464
3.95M
  init_exp(&e, VNONRELOC, fs->freereg - 1);
1465
3.95M
  luaK_storevar(fs, var, &e);  /* will also free the top register */
1466
3.95M
}
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
8.05M
static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) {
1477
8.05M
  expdesc e;
1478
8.05M
  check_condition(ls, vkisvar(lh->v.k), "syntax error");
1479
7.97M
  check_readonly(ls, &lh->v);
1480
7.97M
  if (testnext(ls, ',')) {  /* restassign -> ',' suffixedexp restassign */
1481
5.61M
    struct LHS_assign nv;
1482
5.61M
    nv.prev = lh;
1483
5.61M
    suffixedexp(ls, &nv.v);
1484
5.61M
    if (!vkisindexed(nv.v.k))
1485
465k
      check_conflict(ls, lh, &nv.v);
1486
5.61M
    enterlevel(ls);  /* control recursion depth */
1487
5.61M
    restassign(ls, &nv, nvars+1);
1488
5.61M
    leavelevel(ls);
1489
5.61M
  }
1490
2.36M
  else {  /* restassign -> '=' explist */
1491
2.36M
    int nexps;
1492
2.36M
    checknext(ls, '=');
1493
2.36M
    nexps = explist(ls, &e);
1494
2.36M
    if (nexps != nvars)
1495
363k
      adjust_assign(ls, nvars, nexps, &e);
1496
1.99M
    else {
1497
1.99M
      luaK_setoneret(ls->fs, &e);  /* close last expression */
1498
1.99M
      luaK_storevar(ls->fs, &lh->v, &e);
1499
1.99M
      return;  /* avoid default */
1500
1.99M
    }
1501
2.36M
  }
1502
5.97M
  storevartop(ls->fs, &lh->v);  /* default assignment */
1503
5.97M
}
1504
1505
1506
482k
static int cond (LexState *ls) {
1507
  /* cond -> exp */
1508
482k
  expdesc v;
1509
482k
  expr(ls, &v);  /* read condition */
1510
482k
  if (v.k == VNIL) v.k = VFALSE;  /* 'falses' are all equal here */
1511
482k
  luaK_goiftrue(ls->fs, &v);
1512
482k
  return v.f;
1513
482k
}
1514
1515
1516
286k
static void gotostat (LexState *ls, int line) {
1517
286k
  TString *name = str_checkname(ls);  /* label's name */
1518
286k
  newgotoentry(ls, name, line);
1519
286k
}
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.13k
  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.2k
static void checkrepeated (LexState *ls, TString *name) {
1544
66.2k
  Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel);
1545
66.2k
  if (l_unlikely(lb != NULL))  /* already defined? */
1546
1.30k
    luaK_semerror(ls, "label '%s' already defined on line %d",
1547
1.30k
                      getstr(name), lb->line);  /* error */
1548
66.2k
}
1549
1550
1551
68.9k
static void labelstat (LexState *ls, TString *name, int line) {
1552
  /* label -> '::' NAME '::' */
1553
68.9k
  checknext(ls, TK_DBCOLON);  /* skip double colon */
1554
111k
  while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
1555
42.6k
    statement(ls);  /* skip other no-op statements */
1556
68.9k
  checkrepeated(ls, name);  /* check for repeated labels */
1557
68.9k
  createlabel(ls, name, line, block_follow(ls, 0));
1558
68.9k
}
1559
1560
1561
38.6k
static void whilestat (LexState *ls, int line) {
1562
  /* whilestat -> WHILE cond DO block END */
1563
38.6k
  FuncState *fs = ls->fs;
1564
38.6k
  int whileinit;
1565
38.6k
  int condexit;
1566
38.6k
  BlockCnt bl;
1567
38.6k
  luaX_next(ls);  /* skip WHILE */
1568
38.6k
  whileinit = luaK_getlabel(fs);
1569
38.6k
  condexit = cond(ls);
1570
38.6k
  enterblock(fs, &bl, 1);
1571
38.6k
  checknext(ls, TK_DO);
1572
38.6k
  block(ls);
1573
38.6k
  luaK_jumpto(fs, whileinit);
1574
38.6k
  check_match(ls, TK_END, TK_WHILE, line);
1575
38.6k
  leaveblock(fs);
1576
38.6k
  luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
1577
38.6k
}
1578
1579
1580
75.8k
static void repeatstat (LexState *ls, int line) {
1581
  /* repeatstat -> REPEAT block UNTIL cond */
1582
75.8k
  int condexit;
1583
75.8k
  FuncState *fs = ls->fs;
1584
75.8k
  int repeat_init = luaK_getlabel(fs);
1585
75.8k
  BlockCnt bl1, bl2;
1586
75.8k
  enterblock(fs, &bl1, 1);  /* loop block */
1587
75.8k
  enterblock(fs, &bl2, 0);  /* scope block */
1588
75.8k
  luaX_next(ls);  /* skip REPEAT */
1589
75.8k
  statlist(ls);
1590
75.8k
  check_match(ls, TK_UNTIL, TK_REPEAT, line);
1591
75.8k
  condexit = cond(ls);  /* read condition (inside scope block) */
1592
75.8k
  leaveblock(fs);  /* finish scope */
1593
75.8k
  if (bl2.upval) {  /* upvalues? */
1594
3.36k
    int exit = luaK_jump(fs);  /* normal exit must jump over fix */
1595
3.36k
    luaK_patchtohere(fs, condexit);  /* repetition must close upvalues */
1596
3.36k
    luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0);
1597
3.36k
    condexit = luaK_jump(fs);  /* repeat after closing upvalues */
1598
3.36k
    luaK_patchtohere(fs, exit);  /* normal exit comes to here */
1599
3.36k
  }
1600
75.8k
  luaK_patchlist(fs, condexit, repeat_init);  /* close the loop */
1601
75.8k
  leaveblock(fs);  /* finish loop */
1602
75.8k
}
1603
1604
1605
/*
1606
** Read an expression and generate code to put its results in next
1607
** stack slot.
1608
**
1609
*/
1610
93.7k
static void exp1 (LexState *ls) {
1611
93.7k
  expdesc e;
1612
93.7k
  expr(ls, &e);
1613
93.7k
  luaK_exp2nextreg(ls->fs, &e);
1614
93.7k
  lua_assert(e.k == VNONRELOC);
1615
93.7k
}
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
112k
static void fixforjump (FuncState *fs, int pc, int dest, int back) {
1624
112k
  Instruction *jmp = &fs->f->code[pc];
1625
112k
  int offset = dest - (pc + 1);
1626
112k
  if (back)
1627
56.1k
    offset = -offset;
1628
112k
  if (l_unlikely(offset > MAXARG_Bx))
1629
24
    luaX_syntaxerror(fs->ls, "control structure too long");
1630
112k
  SETARG_Bx(*jmp, offset);
1631
112k
}
1632
1633
1634
/*
1635
** Generate code for a 'for' loop.
1636
*/
1637
71.5k
static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
1638
  /* forbody -> DO block */
1639
71.5k
  static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP};
1640
71.5k
  static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP};
1641
71.5k
  BlockCnt bl;
1642
71.5k
  FuncState *fs = ls->fs;
1643
71.5k
  int prep, endfor;
1644
71.5k
  checknext(ls, TK_DO);
1645
71.5k
  prep = luaK_codeABx(fs, forprep[isgen], base, 0);
1646
71.5k
  fs->freereg--;  /* both 'forprep' remove one register from the stack */
1647
71.5k
  enterblock(fs, &bl, 0);  /* scope for declared variables */
1648
71.5k
  adjustlocalvars(ls, nvars);
1649
71.5k
  luaK_reserveregs(fs, nvars);
1650
71.5k
  block(ls);
1651
71.5k
  leaveblock(fs);  /* end of scope for declared variables */
1652
71.5k
  fixforjump(fs, prep, luaK_getlabel(fs), 0);
1653
71.5k
  if (isgen) {  /* generic for? */
1654
35.1k
    luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
1655
35.1k
    luaK_fixline(fs, line);
1656
35.1k
  }
1657
71.5k
  endfor = luaK_codeABx(fs, forloop[isgen], base, 0);
1658
71.5k
  fixforjump(fs, endfor, prep + 1, 1);
1659
71.5k
  luaK_fixline(fs, line);
1660
71.5k
}
1661
1662
1663
54.4k
static void fornum (LexState *ls, TString *varname, int line) {
1664
  /* fornum -> NAME = exp,exp[,exp] forbody */
1665
54.4k
  FuncState *fs = ls->fs;
1666
54.4k
  int base = fs->freereg;
1667
54.4k
  new_localvarliteral(ls, "(for state)");
1668
54.4k
  new_localvarliteral(ls, "(for state)");
1669
54.4k
  new_varkind(ls, varname, RDKCONST);  /* control variable */
1670
54.4k
  checknext(ls, '=');
1671
54.4k
  exp1(ls);  /* initial value */
1672
54.4k
  checknext(ls, ',');
1673
54.4k
  exp1(ls);  /* limit */
1674
54.4k
  if (testnext(ls, ','))
1675
8.49k
    exp1(ls);  /* optional step */
1676
45.9k
  else {  /* default step = 1 */
1677
45.9k
    luaK_int(fs, fs->freereg, 1);
1678
45.9k
    luaK_reserveregs(fs, 1);
1679
45.9k
  }
1680
54.4k
  adjustlocalvars(ls, 2);  /* start scope for internal variables */
1681
54.4k
  forbody(ls, base, line, 1, 0);
1682
54.4k
}
1683
1684
1685
44.0k
static void forlist (LexState *ls, TString *indexname) {
1686
  /* forlist -> NAME {,NAME} IN explist forbody */
1687
44.0k
  FuncState *fs = ls->fs;
1688
44.0k
  expdesc e;
1689
44.0k
  int nvars = 4;  /* function, state, closing, control */
1690
44.0k
  int line;
1691
44.0k
  int base = fs->freereg;
1692
  /* create internal variables */
1693
44.0k
  new_localvarliteral(ls, "(for state)");  /* iterator function */
1694
44.0k
  new_localvarliteral(ls, "(for state)");  /* state */
1695
44.0k
  new_localvarliteral(ls, "(for state)");  /* closing var. (after swap) */
1696
44.0k
  new_varkind(ls, indexname, RDKCONST);  /* control variable */
1697
  /* other declared variables */
1698
90.4k
  while (testnext(ls, ',')) {
1699
46.3k
    new_localvar(ls, str_checkname(ls));
1700
46.3k
    nvars++;
1701
46.3k
  }
1702
44.0k
  checknext(ls, TK_IN);
1703
44.0k
  line = ls->linenumber;
1704
44.0k
  adjust_assign(ls, 4, explist(ls, &e), &e);
1705
44.0k
  adjustlocalvars(ls, 3);  /* start scope for internal variables */
1706
44.0k
  marktobeclosed(fs);  /* last internal var. must be closed */
1707
44.0k
  luaK_checkstack(fs, 2);  /* extra space to call iterator */
1708
44.0k
  forbody(ls, base, line, nvars - 3, 1);
1709
44.0k
}
1710
1711
1712
106k
static void forstat (LexState *ls, int line) {
1713
  /* forstat -> FOR (fornum | forlist) END */
1714
106k
  FuncState *fs = ls->fs;
1715
106k
  TString *varname;
1716
106k
  BlockCnt bl;
1717
106k
  enterblock(fs, &bl, 1);  /* scope for loop and control variables */
1718
106k
  luaX_next(ls);  /* skip 'for' */
1719
106k
  varname = str_checkname(ls);  /* first variable name */
1720
106k
  switch (ls->t.token) {
1721
54.4k
    case '=': fornum(ls, varname, line); break;
1722
44.0k
    case ',': case TK_IN: forlist(ls, varname); break;
1723
5.05k
    default: luaX_syntaxerror(ls, "'=' or 'in' expected");
1724
106k
  }
1725
56.1k
  check_match(ls, TK_END, TK_FOR, line);
1726
56.1k
  leaveblock(fs);  /* loop scope ('break' jumps to this point) */
1727
56.1k
}
1728
1729
1730
369k
static void test_then_block (LexState *ls, int *escapelist) {
1731
  /* test_then_block -> [IF | ELSEIF] cond THEN block */
1732
369k
  FuncState *fs = ls->fs;
1733
369k
  int condtrue;
1734
369k
  luaX_next(ls);  /* skip IF or ELSEIF */
1735
369k
  condtrue = cond(ls);  /* read condition */
1736
369k
  checknext(ls, TK_THEN);
1737
369k
  block(ls);  /* 'then' part */
1738
369k
  if (ls->t.token == TK_ELSE ||
1739
319k
      ls->t.token == TK_ELSEIF)  /* followed by 'else'/'elseif'? */
1740
97.2k
    luaK_concat(fs, escapelist, luaK_jump(fs));  /* must jump over it */
1741
369k
  luaK_patchtohere(fs, condtrue);
1742
369k
}
1743
1744
1745
318k
static void ifstat (LexState *ls, int line) {
1746
  /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1747
318k
  FuncState *fs = ls->fs;
1748
318k
  int escapelist = NO_JUMP;  /* exit list for finished parts */
1749
318k
  test_then_block(ls, &escapelist);  /* IF cond THEN block */
1750
369k
  while (ls->t.token == TK_ELSEIF)
1751
50.8k
    test_then_block(ls, &escapelist);  /* ELSEIF cond THEN block */
1752
318k
  if (testnext(ls, TK_ELSE))
1753
46.3k
    block(ls);  /* 'else' part */
1754
318k
  check_match(ls, TK_END, TK_IF, line);
1755
318k
  luaK_patchtohere(fs, escapelist);  /* patch escape list to 'if' end */
1756
318k
}
1757
1758
1759
49.1k
static void localfunc (LexState *ls) {
1760
49.1k
  expdesc b;
1761
49.1k
  FuncState *fs = ls->fs;
1762
49.1k
  int fvar = fs->nactvar;  /* function's variable index */
1763
49.1k
  new_localvar(ls, str_checkname(ls));  /* new local variable */
1764
49.1k
  adjustlocalvars(ls, 1);  /* enter its scope */
1765
49.1k
  body(ls, &b, 0, ls->linenumber);  /* function created in next register */
1766
  /* debug information will only see the variable after this point! */
1767
49.1k
  localdebuginfo(fs, fvar)->startpc = fs->pc;
1768
49.1k
}
1769
1770
1771
3.72M
static lu_byte getvarattribute (LexState *ls, lu_byte df) {
1772
  /* attrib -> ['<' NAME '>'] */
1773
3.72M
  if (testnext(ls, '<')) {
1774
36.9k
    TString *ts = str_checkname(ls);
1775
36.9k
    const char *attr = getstr(ts);
1776
36.9k
    checknext(ls, '>');
1777
36.9k
    if (strcmp(attr, "const") == 0)
1778
23.0k
      return RDKCONST;  /* read-only variable */
1779
13.8k
    else if (strcmp(attr, "close") == 0)
1780
4.66k
      return RDKTOCLOSE;  /* to-be-closed variable */
1781
9.21k
    else
1782
9.21k
      luaK_semerror(ls, "unknown attribute '%s'", attr);
1783
36.9k
  }
1784
3.68M
  return df;  /* return default value */
1785
3.72M
}
1786
1787
1788
855k
static void checktoclose (FuncState *fs, int level) {
1789
855k
  if (level != -1) {  /* is there a to-be-closed variable? */
1790
4.34k
    marktobeclosed(fs);
1791
4.34k
    luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0);
1792
4.34k
  }
1793
855k
}
1794
1795
1796
885k
static void localstat (LexState *ls) {
1797
  /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */
1798
885k
  FuncState *fs = ls->fs;
1799
885k
  int toclose = -1;  /* index of to-be-closed variable (if any) */
1800
885k
  Vardesc *var;  /* last variable */
1801
885k
  int vidx;  /* index of last variable */
1802
885k
  int nvars = 0;
1803
885k
  int nexps;
1804
885k
  expdesc e;
1805
  /* get prefixed attribute (if any); default is regular local variable */
1806
885k
  lu_byte defkind = getvarattribute(ls, VDKREG);
1807
2.17M
  do {  /* for each variable */
1808
2.17M
    TString *vname = str_checkname(ls);  /* get its name */
1809
2.17M
    lu_byte kind = getvarattribute(ls, defkind);  /* postfixed attribute */
1810
2.17M
    vidx = new_varkind(ls, vname, kind);  /* predeclare it */
1811
2.17M
    if (kind == RDKTOCLOSE) {  /* to-be-closed? */
1812
4.63k
      if (toclose != -1)  /* one already present? */
1813
143
        luaK_semerror(ls, "multiple to-be-closed variables in local list");
1814
4.49k
      toclose = fs->nactvar + nvars;
1815
4.49k
    }
1816
2.17M
    nvars++;
1817
2.17M
  } while (testnext(ls, ','));
1818
885k
  if (testnext(ls, '='))  /* initialization? */
1819
790k
    nexps = explist(ls, &e);
1820
95.4k
  else {
1821
95.4k
    e.k = VVOID;
1822
95.4k
    nexps = 0;
1823
95.4k
  }
1824
885k
  var = getlocalvardesc(fs, vidx);  /* retrieve last variable */
1825
885k
  if (nvars == nexps &&  /* no adjustments? */
1826
653k
      var->vd.kind == RDKCONST &&  /* last variable is const? */
1827
20.0k
      luaK_exp2const(fs, &e, &var->k)) {  /* compile-time constant? */
1828
17.1k
    var->vd.kind = RDKCTC;  /* variable is a compile-time constant */
1829
17.1k
    adjustlocalvars(ls, nvars - 1);  /* exclude last variable */
1830
17.1k
    fs->nactvar++;  /* but count it */
1831
17.1k
  }
1832
868k
  else {
1833
868k
    adjust_assign(ls, nvars, nexps, &e);
1834
868k
    adjustlocalvars(ls, nvars);
1835
868k
  }
1836
885k
  checktoclose(fs, toclose);
1837
885k
}
1838
1839
1840
678k
static lu_byte getglobalattribute (LexState *ls, lu_byte df) {
1841
678k
  lu_byte kind = getvarattribute(ls, df);
1842
678k
  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
674k
    default:
1849
674k
      return kind;
1850
678k
  }
1851
678k
}
1852
1853
1854
13.7k
static void globalnames (LexState *ls, lu_byte defkind) {
1855
13.7k
  FuncState *fs = ls->fs;
1856
13.7k
  int nvars = 0;
1857
13.7k
  int lastidx;  /* index of last registered variable */
1858
660k
  do {  /* for each name */
1859
660k
    TString *vname = str_checkname(ls);
1860
660k
    lu_byte kind = getglobalattribute(ls, defkind);
1861
660k
    lastidx = new_varkind(ls, vname, kind);
1862
660k
    nvars++;
1863
660k
  } while (testnext(ls, ','));
1864
13.7k
  if (testnext(ls, '=')) {  /* initialization? */
1865
5.46k
    expdesc e;
1866
5.46k
    int i;
1867
5.46k
    int nexps = explist(ls, &e);  /* read list of expressions */
1868
5.46k
    adjust_assign(ls, nvars, nexps, &e);
1869
30.7k
    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.46k
  }
1876
13.7k
  fs->nactvar = cast_short(fs->nactvar + nvars);  /* activate declaration */
1877
13.7k
}
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.7k
    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
4.76k
static void globalfunc (LexState *ls, int line) {
1897
  /* globalfunc -> (GLOBAL FUNCTION) NAME body */
1898
4.76k
  expdesc var, b;
1899
4.76k
  FuncState *fs = ls->fs;
1900
4.76k
  TString *fname = str_checkname(ls);
1901
4.76k
  new_varkind(ls, fname, GDKREG);  /* declare global variable */
1902
4.76k
  fs->nactvar++;  /* enter its scope */
1903
4.76k
  buildglobal(ls, fname, &var);
1904
4.76k
  body(ls, &b, 0, ls->linenumber);  /* compile and return closure in 'b' */
1905
4.76k
  luaK_storevar(fs, &var, &b);
1906
4.76k
  luaK_fixline(fs, line);  /* definition "happens" in the first line */
1907
4.76k
}
1908
1909
1910
23.1k
static void globalstatfunc (LexState *ls, int line) {
1911
  /* stat -> GLOBAL globalfunc | GLOBAL globalstat */
1912
23.1k
  luaX_next(ls);  /* skip 'global' */
1913
23.1k
  if (testnext(ls, TK_FUNCTION))
1914
4.76k
    globalfunc(ls, line);
1915
18.3k
  else
1916
18.3k
    globalstat(ls);
1917
23.1k
}
1918
1919
1920
58.5k
static int funcname (LexState *ls, expdesc *v) {
1921
  /* funcname -> NAME {fieldsel} [':' NAME] */
1922
58.5k
  int ismethod = 0;
1923
58.5k
  singlevar(ls, v);
1924
64.4k
  while (ls->t.token == '.')
1925
5.86k
    fieldsel(ls, v);
1926
58.5k
  if (ls->t.token == ':') {
1927
7.91k
    ismethod = 1;
1928
7.91k
    fieldsel(ls, v);
1929
7.91k
  }
1930
58.5k
  return ismethod;
1931
58.5k
}
1932
1933
1934
58.5k
static void funcstat (LexState *ls, int line) {
1935
  /* funcstat -> FUNCTION funcname body */
1936
58.5k
  int ismethod;
1937
58.5k
  expdesc v, b;
1938
58.5k
  luaX_next(ls);  /* skip FUNCTION */
1939
58.5k
  ismethod = funcname(ls, &v);
1940
58.5k
  check_readonly(ls, &v);
1941
58.5k
  body(ls, &b, ismethod, line);
1942
58.5k
  luaK_storevar(ls->fs, &v, &b);
1943
58.5k
  luaK_fixline(ls->fs, line);  /* definition "happens" in the first line */
1944
58.5k
}
1945
1946
1947
8.10M
static void exprstat (LexState *ls) {
1948
  /* stat -> func | assignment */
1949
8.10M
  FuncState *fs = ls->fs;
1950
8.10M
  struct LHS_assign v;
1951
8.10M
  suffixedexp(ls, &v.v);
1952
8.10M
  if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
1953
2.71M
    v.prev = NULL;
1954
2.71M
    restassign(ls, &v, 1);
1955
2.71M
  }
1956
5.38M
  else {  /* stat -> func */
1957
5.38M
    Instruction *inst;
1958
5.38M
    check_condition(ls, v.v.k == VCALL, "syntax error");
1959
4.78M
    inst = &getinstruction(fs, &v.v);
1960
4.78M
    SETARG_C(*inst, 1);  /* call statement uses no results */
1961
4.78M
  }
1962
8.10M
}
1963
1964
1965
760k
static void retstat (LexState *ls) {
1966
  /* stat -> RETURN [explist] [';'] */
1967
760k
  FuncState *fs = ls->fs;
1968
760k
  expdesc e;
1969
760k
  int nret;  /* number of values being returned */
1970
760k
  int first = luaY_nvarstack(fs);  /* first slot to be returned */
1971
760k
  if (block_follow(ls, 1) || ls->t.token == ';')
1972
74.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.3k
      luaK_setmultret(fs, &e);
1977
73.3k
      if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) {  /* tail call? */
1978
68.3k
        SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
1979
68.3k
        lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs));
1980
68.3k
      }
1981
73.3k
      nret = LUA_MULTRET;  /* return all values */
1982
73.3k
    }
1983
612k
    else {
1984
612k
      if (nret == 1)  /* only one single value? */
1985
596k
        first = luaK_exp2anyreg(fs, &e);  /* can use original slot */
1986
16.0k
      else {  /* values must go to the top of the stack */
1987
16.0k
        luaK_exp2nextreg(fs, &e);
1988
16.0k
        lua_assert(nret == fs->freereg - first);
1989
16.0k
      }
1990
612k
    }
1991
685k
  }
1992
760k
  luaK_ret(fs, first, nret);
1993
753k
  testnext(ls, ';');  /* skip optional semicolon */
1994
753k
}
1995
1996
1997
12.1M
static void statement (LexState *ls) {
1998
12.1M
  int line = ls->linenumber;  /* may be needed for error messages */
1999
12.1M
  enterlevel(ls);
2000
12.1M
  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
318k
    case TK_IF: {  /* stat -> ifstat */
2006
318k
      ifstat(ls, line);
2007
318k
      break;
2008
0
    }
2009
38.6k
    case TK_WHILE: {  /* stat -> whilestat */
2010
38.6k
      whilestat(ls, line);
2011
38.6k
      break;
2012
0
    }
2013
63.7k
    case TK_DO: {  /* stat -> DO block END */
2014
63.7k
      luaX_next(ls);  /* skip DO */
2015
63.7k
      block(ls);
2016
63.7k
      check_match(ls, TK_END, TK_DO, line);
2017
63.7k
      break;
2018
0
    }
2019
106k
    case TK_FOR: {  /* stat -> forstat */
2020
106k
      forstat(ls, line);
2021
106k
      break;
2022
0
    }
2023
75.8k
    case TK_REPEAT: {  /* stat -> repeatstat */
2024
75.8k
      repeatstat(ls, line);
2025
75.8k
      break;
2026
0
    }
2027
58.5k
    case TK_FUNCTION: {  /* stat -> funcstat */
2028
58.5k
      funcstat(ls, line);
2029
58.5k
      break;
2030
0
    }
2031
934k
    case TK_LOCAL: {  /* stat -> localstat */
2032
934k
      luaX_next(ls);  /* skip LOCAL */
2033
934k
      if (testnext(ls, TK_FUNCTION))  /* local function? */
2034
49.1k
        localfunc(ls);
2035
885k
      else
2036
885k
        localstat(ls);
2037
934k
      break;
2038
0
    }
2039
0
    case TK_GLOBAL: {  /* stat -> globalstatfunc */
2040
0
      globalstatfunc(ls, line);
2041
0
      break;
2042
0
    }
2043
72.0k
    case TK_DBCOLON: {  /* stat -> label */
2044
72.0k
      luaX_next(ls);  /* skip double colon */
2045
72.0k
      labelstat(ls, str_checkname(ls), line);
2046
72.0k
      break;
2047
0
    }
2048
760k
    case TK_RETURN: {  /* stat -> retstat */
2049
760k
      luaX_next(ls);  /* skip RETURN */
2050
760k
      retstat(ls);
2051
760k
      break;
2052
0
    }
2053
130k
    case TK_BREAK: {  /* stat -> breakstat */
2054
130k
      breakstat(ls, line);
2055
130k
      break;
2056
0
    }
2057
286k
    case TK_GOTO: {  /* stat -> 'goto' NAME */
2058
286k
      luaX_next(ls);  /* skip 'goto' */
2059
286k
      gotostat(ls, line);
2060
286k
      break;
2061
0
    }
2062
0
#if defined(LUA_COMPAT_GLOBAL)
2063
5.23M
    case TK_NAME: {
2064
      /* compatibility code to parse global keyword when "global"
2065
         is not reserved */
2066
5.23M
      if (ls->t.seminfo.ts == ls->glbn) {  /* current = "global"? */
2067
27.4k
        int lk = luaX_lookahead(ls);
2068
27.4k
        if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) {
2069
          /* 'global <attrib>' or 'global name' or 'global *' or
2070
             'global function' */
2071
23.1k
          globalstatfunc(ls, line);
2072
23.1k
          break;
2073
23.1k
        }
2074
27.4k
      }  /* else... */
2075
5.23M
    }
2076
5.21M
#endif
2077
    /* FALLTHROUGH */
2078
8.10M
    default: {  /* stat -> func | assignment */
2079
8.10M
      exprstat(ls);
2080
8.10M
      break;
2081
5.23M
    }
2082
12.1M
  }
2083
6.99M
  lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
2084
6.99M
             ls->fs->freereg >= luaY_nvarstack(ls->fs));
2085
6.99M
  ls->fs->freereg = luaY_nvarstack(ls->fs);  /* free registers */
2086
6.99M
  leavelevel(ls);
2087
6.99M
}
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
8.24M
static void mainfunc (LexState *ls, FuncState *fs) {
2099
8.24M
  BlockCnt bl;
2100
8.24M
  Upvaldesc *env;
2101
8.24M
  open_func(ls, fs, &bl);
2102
8.24M
  setvararg(fs, 0);  /* main function is always declared vararg */
2103
8.24M
  env = allocupvalue(fs);  /* ...set environment upvalue */
2104
8.24M
  env->instack = 1;
2105
8.24M
  env->idx = 0;
2106
8.24M
  env->kind = VDKREG;
2107
8.24M
  env->name = ls->envn;
2108
8.24M
  luaC_objbarrier(ls->L, fs->f, env->name);
2109
8.24M
  luaX_next(ls);  /* read first token */
2110
8.24M
  statlist(ls);  /* parse main body */
2111
8.24M
  check(ls, TK_EOS);
2112
8.24M
  close_func(ls);
2113
8.24M
}
2114
2115
2116
LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
2117
8.24M
                       Dyndata *dyd, const char *name, int firstchar) {
2118
8.24M
  LexState lexstate;
2119
8.24M
  FuncState funcstate;
2120
8.24M
  LClosure *cl = luaF_newLclosure(L, 1);  /* create main closure */
2121
8.24M
  setclLvalue2s(L, L->top.p, cl);  /* anchor it (to avoid being collected) */
2122
8.24M
  luaD_inctop(L);
2123
8.24M
  lexstate.h = luaH_new(L);  /* create table for scanner */
2124
8.24M
  sethvalue2s(L, L->top.p, lexstate.h);  /* anchor it */
2125
8.24M
  luaD_inctop(L);
2126
8.24M
  funcstate.f = cl->p = luaF_newproto(L);
2127
8.24M
  luaC_objbarrier(L, cl, cl->p);
2128
8.24M
  funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
2129
8.24M
  luaC_objbarrier(L, funcstate.f, funcstate.f->source);
2130
8.24M
  lexstate.buff = buff;
2131
8.24M
  lexstate.dyd = dyd;
2132
8.24M
  dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
2133
8.24M
  luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
2134
8.24M
  mainfunc(&lexstate, &funcstate);
2135
8.24M
  lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
2136
  /* all scopes should be correctly finished */
2137
8.24M
  lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
2138
3.36M
  L->top.p--;  /* remove scanner's table */
2139
3.36M
  return cl;  /* closure is on the stack, too */
2140
3.36M
}
2141