Coverage Report

Created: 2023-09-11 06:55

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