Coverage Report

Created: 2024-04-23 06:32

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