• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 5973601545

25 Aug 2023 08:22AM UTC coverage: 88.113% (+0.2%) from 87.9%
5973601545

push

github

igormunkin
Revert to trival pow() optimizations to prevent inaccuracies.

(cherry-picked from commit 96d6d5032)

This patch fixes different misbehaviours between JIT-compiled code and
the interpreter for power operator in the following ways:
* Drop folding optimizations for base ^ n => base * base ..., as far as
  pow(base, n) isn't interchangeable with just multiplicity of numbers
  and depends on the <math.h> implementation.
* Since the internal power function is inaccurate for very big or small
  powers, it is dropped, and `pow()` from the standard library is used
  instead. To save consistency between JIT behaviour and the VM,
  narrowing optimization is dropped, and only trivial folding
  optimizations are used. Also, `math_extern2` version with two
  parameters is dropped, since it's no longer used.

Also, this fixes failures of the [220/502] lib/string/format/num.lua
test [1] from the LuaJIT-test suite.

[1]: https://www.exploringbinary.com/incorrect-floating-point-to-decimal-conversions/

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#8825

Reviewed-by: Maxim Kokryashkin <m.kokryashkin@tarantool.org>
Reviewed-by: Sergey Bronnikov <sergeyb@tarantool.org>
Signed-off-by: Igor Munkin <imun@tarantool.org>

5322 of 5963 branches covered (0.0%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 5 files covered. (100.0%)

20443 of 23278 relevant lines covered (87.82%)

1292502.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.56
/src/lj_parse.c
1
/*
2
** Lua parser (source code -> bytecode).
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major portions taken verbatim or adapted from the Lua interpreter.
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7
*/
8

9
#define lj_parse_c
10
#define LUA_CORE
11

12
#include "lj_obj.h"
13
#include "lj_gc.h"
14
#include "lj_err.h"
15
#include "lj_debug.h"
16
#include "lj_buf.h"
17
#include "lj_str.h"
18
#include "lj_tab.h"
19
#include "lj_func.h"
20
#include "lj_state.h"
21
#include "lj_bc.h"
22
#if LJ_HASFFI
23
#include "lj_ctype.h"
24
#endif
25
#include "lj_strfmt.h"
26
#include "lj_lex.h"
27
#include "lj_parse.h"
28
#include "lj_vm.h"
29
#include "lj_vmevent.h"
30
#if LJ_HASMEMPROF
31
#include "lj_memprof.h"
32
#endif
33
#if LJ_HASSYSPROF
34
#include "lj_sysprof.h"
35
#endif
36

37
/* -- Parser structures and definitions ----------------------------------- */
38

39
/* Expression kinds. */
40
typedef enum {
41
  /* Constant expressions must be first and in this order: */
42
  VKNIL,
43
  VKFALSE,
44
  VKTRUE,
45
  VKSTR,        /* sval = string value */
46
  VKNUM,        /* nval = number value */
47
  VKLAST = VKNUM,
48
  VKCDATA,        /* nval = cdata value, not treated as a constant expression */
49
  /* Non-constant expressions follow: */
50
  VLOCAL,        /* info = local register, aux = vstack index */
51
  VUPVAL,        /* info = upvalue index, aux = vstack index */
52
  VGLOBAL,        /* sval = string value */
53
  VINDEXED,        /* info = table register, aux = index reg/byte/string const */
54
  VJMP,                /* info = instruction PC */
55
  VRELOCABLE,        /* info = instruction PC */
56
  VNONRELOC,        /* info = result register */
57
  VCALL,        /* info = instruction PC, aux = base */
58
  VVOID
59
} ExpKind;
60

61
/* Expression descriptor. */
62
typedef struct ExpDesc {
63
  union {
64
    struct {
65
      uint32_t info;        /* Primary info. */
66
      uint32_t aux;        /* Secondary info. */
67
    } s;
68
    TValue nval;        /* Number value. */
69
    GCstr *sval;        /* String value. */
70
  } u;
71
  ExpKind k;
72
  BCPos t;                /* True condition jump list. */
73
  BCPos f;                /* False condition jump list. */
74
} ExpDesc;
75

76
/* Macros for expressions. */
77
#define expr_hasjump(e)                ((e)->t != (e)->f)
78

79
#define expr_isk(e)                ((e)->k <= VKLAST)
80
#define expr_isk_nojump(e)        (expr_isk(e) && !expr_hasjump(e))
81
#define expr_isnumk(e)                ((e)->k == VKNUM)
82
#define expr_isnumk_nojump(e)        (expr_isnumk(e) && !expr_hasjump(e))
83
#define expr_isstrk(e)                ((e)->k == VKSTR)
84

85
#define expr_numtv(e)                check_exp(expr_isnumk((e)), &(e)->u.nval)
86
#define expr_numberV(e)                numberVnum(expr_numtv((e)))
87

88
/* Initialize expression. */
89
static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info)
2,455,321✔
90
{
91
  e->k = k;
2,455,321✔
92
  e->u.s.info = info;
2,455,321✔
93
  e->f = e->t = NO_JMP;
2,455,321✔
94
}
142,458✔
95

96
/* Check number constant for +-0. */
97
static int expr_numiszero(ExpDesc *e)
2,331✔
98
{
99
  TValue *o = expr_numtv(e);
2,331✔
100
  return tvisint(o) ? (intV(o) == 0) : tviszero(o);
2,331✔
101
}
102

103
/* Per-function linked list of scope blocks. */
104
typedef struct FuncScope {
105
  struct FuncScope *prev;        /* Link to outer scope. */
106
  MSize vstart;                        /* Start of block-local variables. */
107
  uint8_t nactvar;                /* Number of active vars outside the scope. */
108
  uint8_t flags;                /* Scope flags. */
109
} FuncScope;
110

111
#define FSCOPE_LOOP                0x01        /* Scope is a (breakable) loop. */
112
#define FSCOPE_BREAK                0x02        /* Break used in scope. */
113
#define FSCOPE_GOLA                0x04        /* Goto or label used in scope. */
114
#define FSCOPE_UPVAL                0x08        /* Upvalue in scope. */
115
#define FSCOPE_NOCLOSE                0x10        /* Do not close upvalues. */
116

117
#define NAME_BREAK                ((GCstr *)(uintptr_t)1)
118

119
/* Index into variable stack. */
120
typedef uint16_t VarIndex;
121
#define LJ_MAX_VSTACK                (65536 - LJ_MAX_UPVAL)
122

123
/* Variable/goto/label info. */
124
#define VSTACK_VAR_RW                0x01        /* R/W variable. */
125
#define VSTACK_GOTO                0x02        /* Pending goto. */
126
#define VSTACK_LABEL                0x04        /* Label. */
127

128
/* Per-function state. */
129
typedef struct FuncState {
130
  GCtab *kt;                        /* Hash table for constants. */
131
  LexState *ls;                        /* Lexer state. */
132
  lua_State *L;                        /* Lua state. */
133
  FuncScope *bl;                /* Current scope. */
134
  struct FuncState *prev;        /* Enclosing function. */
135
  BCPos pc;                        /* Next bytecode position. */
136
  BCPos lasttarget;                /* Bytecode position of last jump target. */
137
  BCPos jpc;                        /* Pending jump list to next bytecode. */
138
  BCReg freereg;                /* First free register. */
139
  BCReg nactvar;                /* Number of active local variables. */
140
  BCReg nkn, nkgc;                /* Number of lua_Number/GCobj constants */
141
  BCLine linedefined;                /* First line of the function definition. */
142
  BCInsLine *bcbase;                /* Base of bytecode stack. */
143
  BCPos bclim;                        /* Limit of bytecode stack. */
144
  MSize vbase;                        /* Base of variable stack for this function. */
145
  uint8_t flags;                /* Prototype flags. */
146
  uint8_t numparams;                /* Number of parameters. */
147
  uint8_t framesize;                /* Fixed frame size. */
148
  uint8_t nuv;                        /* Number of upvalues */
149
  VarIndex varmap[LJ_MAX_LOCVAR];  /* Map from register to variable idx. */
150
  VarIndex uvmap[LJ_MAX_UPVAL];        /* Map from upvalue to variable idx. */
151
  VarIndex uvtmp[LJ_MAX_UPVAL];        /* Temporary upvalue map. */
152
} FuncState;
153

154
/* Binary and unary operators. ORDER OPR */
155
typedef enum BinOpr {
156
  OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,  /* ORDER ARITH */
157
  OPR_CONCAT,
158
  OPR_NE, OPR_EQ,
159
  OPR_LT, OPR_GE, OPR_LE, OPR_GT,
160
  OPR_AND, OPR_OR,
161
  OPR_NOBINOPR
162
} BinOpr;
163

164
LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT);
165
LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT);
166
LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT);
167
LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD);
168
LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);
169
LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);
170
LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);
171

172
#ifdef LUA_USE_ASSERT
173
#define lj_assertFS(c, ...)        (lj_assertG_(G(fs->L), (c), __VA_ARGS__))
174
#else
175
#define lj_assertFS(c, ...)        ((void)fs)
176
#endif
177

178
/* -- Error handling ------------------------------------------------------ */
179

180
LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)
7,371✔
181
{
182
  lj_lex_error(ls, ls->tok, em);
7,371✔
183
}
184

185
LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken tok)
4,936✔
186
{
187
  lj_lex_error(ls, ls->tok, LJ_ERR_XTOKEN, lj_lex_token2str(ls, tok));
4,936✔
188
}
189

190
LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)
191
{
192
  if (fs->linedefined == 0)
193
    lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what);
194
  else
195
    lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what);
196
}
197

198
#define checklimit(fs, v, l, m)                if ((v) >= (l)) err_limit(fs, l, m)
199
#define checklimitgt(fs, v, l, m)        if ((v) > (l)) err_limit(fs, l, m)
200
#define checkcond(ls, c, em)                { if (!(c)) err_syntax(ls, em); }
201

202
/* -- Management of constants --------------------------------------------- */
203

204
/* Return bytecode encoding for primitive constant. */
205
#define const_pri(e)                check_exp((e)->k <= VKTRUE, (e)->k)
206

207
#define tvhaskslot(o)        ((o)->u32.hi == 0)
208
#define tvkslot(o)        ((o)->u32.lo)
209

210
/* Add a number constant. */
211
static BCReg const_num(FuncState *fs, ExpDesc *e)
145,078✔
212
{
213
  lua_State *L = fs->L;
145,078✔
214
  TValue *o;
145,078✔
215
  lj_assertFS(expr_isnumk(e), "bad usage");
145,078✔
216
  o = lj_tab_set(L, fs->kt, &e->u.nval);
145,078✔
217
  if (tvhaskslot(o))
145,078✔
218
    return tvkslot(o);
1,785✔
219
  o->u64 = fs->nkn;
143,293✔
220
  return fs->nkn++;
143,293✔
221
}
222

223
/* Add a GC object constant. */
224
static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)
604,510✔
225
{
226
  lua_State *L = fs->L;
604,510✔
227
  TValue key, *o;
604,510✔
228
  setgcV(L, &key, gc, itype);
604,510✔
229
  /* NOBARRIER: the key is new or kept alive. */
230
  o = lj_tab_set(L, fs->kt, &key);
604,510✔
231
  if (tvhaskslot(o))
604,510✔
232
    return tvkslot(o);
267,265✔
233
  o->u64 = fs->nkgc;
337,245✔
234
  return fs->nkgc++;
337,245✔
235
}
236

237
/* Add a string constant. */
238
static BCReg const_str(FuncState *fs, ExpDesc *e)
565,465✔
239
{
240
  lj_assertFS(expr_isstrk(e) || e->k == VGLOBAL, "bad usage");
565,465✔
241
  return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);
565,465✔
242
}
243

244
/* Anchor string constant to avoid GC. */
245
GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len)
2,413,504✔
246
{
247
  /* NOBARRIER: the key is new or kept alive. */
248
  lua_State *L = ls->L;
2,413,504✔
249
  GCstr *s = lj_str_new(L, str, len);
2,413,504✔
250
  TValue *tv = lj_tab_setstr(L, ls->fs->kt, s);
2,413,504✔
251
  if (tvisnil(tv)) setboolV(tv, 1);
2,413,504✔
252
  lj_gc_check(L);
2,413,504✔
253
  return s;
2,413,504✔
254
}
255

256
#if LJ_HASFFI
257
/* Anchor cdata to avoid GC. */
258
void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd)
139✔
259
{
260
  /* NOBARRIER: the key is new or kept alive. */
261
  lua_State *L = ls->L;
139✔
262
  setcdataV(L, tv, cd);
139✔
263
  setboolV(lj_tab_set(L, ls->fs->kt, tv), 1);
139✔
264
}
139✔
265
#endif
266

267
/* -- Jump list handling -------------------------------------------------- */
268

269
/* Get next element in jump list. */
270
static BCPos jmp_next(FuncState *fs, BCPos pc)
731,653✔
271
{
272
  ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);
731,653✔
273
  if ((BCPos)delta == NO_JMP)
731,653✔
274
    return NO_JMP;
275
  else
276
    return (BCPos)(((ptrdiff_t)pc+1)+delta);
74,482✔
277
}
278

279
/* Check if any of the instructions on the jump list produce no value. */
280
static int jmp_novalue(FuncState *fs, BCPos list)
281
{
282
  for (; list != NO_JMP; list = jmp_next(fs, list)) {
283
    BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins;
284
    if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG))
285
      return 1;
286
  }
287
  return 0;
288
}
289

290
/* Patch register of test instructions. */
291
static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg)
292
{
293
  BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc];
294
  BCOp op = bc_op(ilp->ins);
295
  if (op == BC_ISTC || op == BC_ISFC) {
296
    if (reg != NO_REG && reg != bc_d(ilp->ins)) {
297
      setbc_a(&ilp->ins, reg);
298
    } else {  /* Nothing to store or already in the right register. */
299
      setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC));
300
      setbc_a(&ilp->ins, 0);
301
    }
302
  } else if (bc_a(ilp->ins) == NO_REG) {
303
    if (reg == NO_REG) {
304
      ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0);
305
    } else {
306
      setbc_a(&ilp->ins, reg);
307
      if (reg >= bc_a(ilp[1].ins))
308
        setbc_a(&ilp[1].ins, reg+1);
309
    }
310
  } else {
311
    return 0;  /* Cannot patch other instructions. */
312
  }
313
  return 1;
314
}
315

316
/* Drop values for all instructions on jump list. */
317
static void jmp_dropval(FuncState *fs, BCPos list)
592,554✔
318
{
319
  for (; list != NO_JMP; list = jmp_next(fs, list))
628,496✔
320
    jmp_patchtestreg(fs, list, NO_REG);
35,942✔
321
}
592,554✔
322

323
/* Patch jump instruction to target. */
324
static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)
746,476✔
325
{
326
  BCIns *jmp = &fs->bcbase[pc].ins;
746,476✔
327
  BCPos offset = dest-(pc+1)+BCBIAS_J;
746,476✔
328
  lj_assertFS(dest != NO_JMP, "uninitialized jump target");
746,476✔
329
  if (offset > BCMAX_D)
746,476✔
330
    err_syntax(fs->ls, LJ_ERR_XJUMP);
×
331
  setbc_d(jmp, offset);
746,476✔
332
}
692,301✔
333

334
/* Append to jump list. */
335
static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)
2,054,948✔
336
{
337
  if (l2 == NO_JMP) {
2,054,948✔
338
    return;
339
  } else if (*l1 == NO_JMP) {
889,685✔
340
    *l1 = l2;
815,201✔
341
  } else {
342
    BCPos list = *l1;
343
    BCPos next;
344
    while ((next = jmp_next(fs, list)) != NO_JMP)  /* Find last element. */
77,894✔
345
      list = next;
346
    jmp_patchins(fs, list, l2);
74,484✔
347
  }
348
}
349

350
/* Patch jump list and preserve produced values. */
351
static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,
4,325,681✔
352
                         BCReg reg, BCPos dtarget)
353
{
354
  while (list != NO_JMP) {
4,325,681✔
355
    BCPos next = jmp_next(fs, list);
617,817✔
356
    if (jmp_patchtestreg(fs, list, reg))
617,817✔
357
      jmp_patchins(fs, list, vtarget);  /* Jump to target with value. */
70,443✔
358
    else
359
      jmp_patchins(fs, list, dtarget);  /* Jump to default target. */
5,490,872✔
360
    list = next;
361
  }
362
}
4,325,681✔
363

364
/* Jump to following instruction. Append to list of pending jumps. */
365
static void jmp_tohere(FuncState *fs, BCPos list)
708,291✔
366
{
367
  fs->lasttarget = fs->pc;
708,291✔
368
  jmp_append(fs, &fs->jpc, list);
708,291✔
369
}
309,810✔
370

371
/* Patch jump list to target. */
372
static void jmp_patch(FuncState *fs, BCPos list, BCPos target)
98,779✔
373
{
374
  if (target == fs->pc) {
98,779✔
375
    jmp_tohere(fs, list);
49,350✔
376
  } else {
377
    lj_assertFS(target < fs->pc, "bad jump target");
49,429✔
378
    jmp_patchval(fs, list, target, NO_REG, target);
49,429✔
379
  }
380
}
98,779✔
381

382
/* -- Bytecode register allocator ----------------------------------------- */
383

384
/* Bump frame size. */
385
static void bcreg_bump(FuncState *fs, BCReg n)
1,518,623✔
386
{
387
  BCReg sz = fs->freereg + n;
1,518,623✔
388
  if (sz > fs->framesize) {
1,518,623✔
389
    if (sz >= LJ_MAX_SLOTS)
214,776✔
390
      err_syntax(fs->ls, LJ_ERR_XSLOTS);
×
391
    fs->framesize = (uint8_t)sz;
214,776✔
392
  }
393
}
394

395
/* Reserve registers. */
396
static void bcreg_reserve(FuncState *fs, BCReg n)
1,517,917✔
397
{
398
  bcreg_bump(fs, n);
1,517,917✔
399
  fs->freereg += n;
1,517,917✔
400
}
1,517,917✔
401

402
/* Free register. */
403
static void bcreg_free(FuncState *fs, BCReg reg)
429,090✔
404
{
405
  if (reg >= fs->nactvar) {
429,090✔
406
    fs->freereg--;
247,655✔
407
    lj_assertFS(reg == fs->freereg, "bad regfree");
3,530✔
408
  }
409
}
410

411
/* Free register for expression. */
412
static void expr_free(FuncState *fs, ExpDesc *e)
1,769,279✔
413
{
414
  if (e->k == VNONRELOC)
1,769,279✔
415
    bcreg_free(fs, e->u.s.info);
405,481✔
416
}
417

418
/* -- Bytecode emitter ---------------------------------------------------- */
419

420
/* Emit bytecode instruction. */
421
static BCPos bcemit_INS(FuncState *fs, BCIns ins)
3,699,202✔
422
{
423
  BCPos pc = fs->pc;
3,699,202✔
424
  LexState *ls = fs->ls;
3,699,202✔
425
  jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);
3,699,202✔
426
  fs->jpc = NO_JMP;
3,699,202✔
427
  if (LJ_UNLIKELY(pc >= fs->bclim)) {
3,699,202✔
428
    ptrdiff_t base = fs->bcbase - ls->bcstack;
139,158✔
429
    checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");
139,158✔
430
    lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);
139,158✔
431
    fs->bclim = (BCPos)(ls->sizebcstack - base);
139,158✔
432
    fs->bcbase = ls->bcstack + base;
139,158✔
433
  }
434
  fs->bcbase[pc].ins = ins;
3,699,202✔
435
  fs->bcbase[pc].line = ls->lastline;
3,699,202✔
436
  fs->pc = pc+1;
3,699,202✔
437
  return pc;
3,699,202✔
438
}
439

440
#define bcemit_ABC(fs, o, a, b, c)        bcemit_INS(fs, BCINS_ABC(o, a, b, c))
441
#define bcemit_AD(fs, o, a, d)                bcemit_INS(fs, BCINS_AD(o, a, d))
442
#define bcemit_AJ(fs, o, a, j)                bcemit_INS(fs, BCINS_AJ(o, a, j))
443

444
#define bcptr(fs, e)                        (&(fs)->bcbase[(e)->u.s.info].ins)
445

446
/* -- Bytecode emitter for expressions ------------------------------------ */
447

448
/* Discharge non-constant expression to any register. */
449
static void expr_discharge(FuncState *fs, ExpDesc *e)
5,356,429✔
450
{
451
  BCIns ins;
5,356,429✔
452
  if (e->k == VUPVAL) {
5,356,429✔
453
    ins = BCINS_AD(BC_UGET, 0, e->u.s.info);
11,285✔
454
  } else if (e->k == VGLOBAL) {
5,345,144✔
455
    ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));
279,839✔
456
  } else if (e->k == VINDEXED) {
5,065,305✔
457
    BCReg rc = e->u.s.aux;
20,079✔
458
    if ((int32_t)rc < 0) {
20,079✔
459
      ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);
15,213✔
460
    } else if (rc > BCMAX_C) {
4,866✔
461
      ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));
1,336✔
462
    } else {
463
      bcreg_free(fs, rc);
3,530✔
464
      ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);
3,530✔
465
    }
466
    bcreg_free(fs, e->u.s.info);
20,079✔
467
  } else if (e->k == VCALL) {
5,045,226✔
468
    e->u.s.info = e->u.s.aux;
238,924✔
469
    e->k = VNONRELOC;
238,924✔
470
    return;
238,924✔
471
  } else if (e->k == VLOCAL) {
4,806,302✔
472
    e->k = VNONRELOC;
200,563✔
473
    return;
200,563✔
474
  } else {
475
    return;
476
  }
477
  e->u.s.info = bcemit_INS(fs, ins);
311,203✔
478
  e->k = VRELOCABLE;
311,203✔
479
}
480

481
/* Emit bytecode to set a range of registers to nil. */
482
static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)
45,944✔
483
{
484
  if (fs->pc > fs->lasttarget) {  /* No jumps to current position? */
45,944✔
485
    BCIns *ip = &fs->bcbase[fs->pc-1].ins;
30,080✔
486
    BCReg pto, pfrom = bc_a(*ip);
30,080✔
487
    switch (bc_op(*ip)) {  /* Try to merge with the previous instruction. */
30,080✔
488
    case BC_KPRI:
721✔
489
      if (bc_d(*ip) != ~LJ_TNIL) break;
721✔
490
      if (from == pfrom) {
715✔
491
        if (n == 1) return;
×
492
      } else if (from == pfrom+1) {
715✔
493
        from = pfrom;
73✔
494
        n++;
73✔
495
      } else {
496
        break;
497
      }
498
      *ip = BCINS_AD(BC_KNIL, from, from+n-1);  /* Replace KPRI. */
73✔
499
      return;
73✔
500
    case BC_KNIL:
129✔
501
      pto = bc_d(*ip);
129✔
502
      if (pfrom <= from && from <= pto+1) {  /* Can we connect both ranges? */
129✔
503
        if (from+n-1 > pto)
128✔
504
          setbc_d(ip, from+n-1);  /* Patch previous instruction range. */
126✔
505
        return;
128✔
506
      }
507
      break;
508
    default:
509
      break;
510
    }
511
  }
16,512✔
512
  /* Emit new instruction or replace old instruction. */
513
  bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :
45,834✔
514
                          BCINS_AD(BC_KNIL, from, from+n-1));
91✔
515
}
516

517
/* Discharge an expression to a specific register. Ignore branches. */
518
static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)
1,471,019✔
519
{
520
  BCIns ins;
1,471,019✔
521
  expr_discharge(fs, e);
1,471,019✔
522
  if (e->k == VKSTR) {
1,471,019✔
523
    ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));
153,012✔
524
  } else if (e->k == VKNUM) {
1,318,007✔
525
#if LJ_DUALNUM
526
    cTValue *tv = expr_numtv(e);
527
    if (tvisint(tv) && checki16(intV(tv)))
528
      ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv));
529
    else
530
#else
531
    lua_Number n = expr_numberV(e);
545,328✔
532
    int32_t k = lj_num2int(n);
545,328✔
533
    if (checki16(k) && n == (lua_Number)k)
545,328✔
534
      ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);
413,563✔
535
    else
536
#endif
537
      ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));
131,765✔
538
#if LJ_HASFFI
539
  } else if (e->k == VKCDATA) {
772,679✔
540
    fs->flags |= PROTO_FFI;
139✔
541
    ins = BCINS_AD(BC_KCDATA, reg,
139✔
542
                   const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));
543
#endif
544
  } else if (e->k == VRELOCABLE) {
772,540✔
545
    setbc_a(bcptr(fs, e), reg);
415,520✔
546
    goto noins;
415,520✔
547
  } else if (e->k == VNONRELOC) {
357,020✔
548
    if (reg == e->u.s.info)
69,654✔
549
      goto noins;
26,549✔
550
    ins = BCINS_AD(BC_MOV, reg, e->u.s.info);
43,105✔
551
  } else if (e->k == VKNIL) {
287,366✔
552
    bcemit_nil(fs, reg, 1);
28,911✔
553
    goto noins;
28,911✔
554
  } else if (e->k <= VKTRUE) {
258,455✔
555
    ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
34,308✔
556
  } else {
557
    lj_assertFS(e->k == VVOID || e->k == VJMP, "bad expr type %d", e->k);
558
    return;
559
  }
560
  bcemit_INS(fs, ins);
775,892✔
561
noins:
1,246,872✔
562
  e->u.s.info = reg;
1,246,872✔
563
  e->k = VNONRELOC;
1,246,872✔
564
}
565

566
/* Forward declaration. */
567
static BCPos bcemit_jmp(FuncState *fs);
568

569
/* Discharge an expression to a specific register. */
570
static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg)
1,433,090✔
571
{
572
  expr_toreg_nobranch(fs, e, reg);
1,433,090✔
573
  if (e->k == VJMP)
1,433,090✔
574
    jmp_append(fs, &e->t, e->u.s.info);  /* Add it to the true jump list. */
224,147✔
575
  if (expr_hasjump(e)) {  /* Discharge expression with branches. */
1,433,090✔
576
    BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;
288,525✔
577
    if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {
288,525✔
578
      BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);
260,460✔
579
      jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);
260,460✔
580
      bcemit_AJ(fs, BC_JMP, fs->freereg, 1);
260,460✔
581
      jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);
260,460✔
582
      jmp_tohere(fs, jval);
260,460✔
583
    }
584
    jend = fs->pc;
288,525✔
585
    fs->lasttarget = jend;
288,525✔
586
    jmp_patchval(fs, e->f, jend, reg, jfalse);
288,525✔
587
    jmp_patchval(fs, e->t, jend, reg, jtrue);
288,525✔
588
  }
589
  e->f = e->t = NO_JMP;
1,433,090✔
590
  e->u.s.info = reg;
1,433,090✔
591
  e->k = VNONRELOC;
1,433,090✔
592
}
1,433,090✔
593

594
/* Discharge an expression to the next free register. */
595
static void expr_tonextreg(FuncState *fs, ExpDesc *e)
1,161,634✔
596
{
597
  expr_discharge(fs, e);
1,161,634✔
598
  expr_free(fs, e);
1,161,634✔
599
  bcreg_reserve(fs, 1);
1,161,634✔
600
  expr_toreg(fs, e, fs->freereg - 1);
1,161,634✔
601
}
1,161,634✔
602

603
/* Discharge an expression to any register. */
604
static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)
976,268✔
605
{
606
  expr_discharge(fs, e);
976,268✔
607
  if (e->k == VNONRELOC) {
976,268✔
608
    if (!expr_hasjump(e)) return e->u.s.info;  /* Already in a register. */
351,115✔
609
    if (e->u.s.info >= fs->nactvar) {
944✔
610
      expr_toreg(fs, e, e->u.s.info);  /* Discharge to temp. register. */
926✔
611
      return e->u.s.info;
926✔
612
    }
613
  }
614
  expr_tonextreg(fs, e);  /* Discharge to next register. */
625,171✔
615
  return e->u.s.info;
625,171✔
616
}
617

618
/* Partially discharge expression to a value. */
619
static void expr_toval(FuncState *fs, ExpDesc *e)
613,127✔
620
{
621
  if (expr_hasjump(e))
613,127✔
622
    expr_toanyreg(fs, e);
10,784✔
623
  else
624
    expr_discharge(fs, e);
602,343✔
625
}
613,127✔
626

627
/* Emit store for LHS expression. */
628
static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
407,420✔
629
{
630
  BCIns ins;
407,420✔
631
  if (var->k == VLOCAL) {
407,420✔
632
    fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
267,050✔
633
    expr_free(fs, e);
267,050✔
634
    expr_toreg(fs, e, var->u.s.info);
267,050✔
635
    return;
267,050✔
636
  } else if (var->k == VUPVAL) {
140,370✔
637
    fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
521✔
638
    expr_toval(fs, e);
521✔
639
    if (e->k <= VKTRUE)
521✔
640
      ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
44✔
641
    else if (e->k == VKSTR)
477✔
642
      ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));
10✔
643
    else if (e->k == VKNUM)
467✔
644
      ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));
78✔
645
    else
646
      ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));
389✔
647
  } else if (var->k == VGLOBAL) {
139,849✔
648
    BCReg ra = expr_toanyreg(fs, e);
126,034✔
649
    ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
126,034✔
650
  } else {
651
    BCReg ra, rc;
13,815✔
652
    lj_assertFS(var->k == VINDEXED, "bad expr type %d", var->k);
13,815✔
653
    ra = expr_toanyreg(fs, e);
13,815✔
654
    rc = var->u.s.aux;
13,815✔
655
    if ((int32_t)rc < 0) {
13,815✔
656
      ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);
8,914✔
657
    } else if (rc > BCMAX_C) {
4,901✔
658
      ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
1,549✔
659
    } else {
660
#ifdef LUA_USE_ASSERT
661
      /* Free late alloced key reg to avoid assert on free of value reg. */
662
      /* This can only happen when called from expr_table(). */
663
      if (e->k == VNONRELOC && ra >= fs->nactvar && rc >= ra)
664
        bcreg_free(fs, rc);
665
#endif
666
      ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);
3,352✔
667
    }
668
  }
669
  bcemit_INS(fs, ins);
140,370✔
670
  expr_free(fs, e);
140,370✔
671
}
672

673
/* Emit method lookup expression. */
674
static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
4,469✔
675
{
676
  BCReg idx, func, obj = expr_toanyreg(fs, e);
4,469✔
677
  expr_free(fs, e);
4,469✔
678
  func = fs->freereg;
4,469✔
679
  bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj);  /* Copy object to 1st argument. */
4,469✔
680
  lj_assertFS(expr_isstrk(key), "bad usage");
4,469✔
681
  idx = const_str(fs, key);
4,469✔
682
  if (idx <= BCMAX_C) {
4,469✔
683
    bcreg_reserve(fs, 2+LJ_FR2);
4,463✔
684
    bcemit_ABC(fs, BC_TGETS, func, obj, idx);
4,463✔
685
  } else {
686
    bcreg_reserve(fs, 3+LJ_FR2);
6✔
687
    bcemit_AD(fs, BC_KSTR, func+2+LJ_FR2, idx);
6✔
688
    bcemit_ABC(fs, BC_TGETV, func, obj, func+2+LJ_FR2);
6✔
689
    fs->freereg--;
6✔
690
  }
691
  e->u.s.info = func;
4,469✔
692
  e->k = VNONRELOC;
4,469✔
693
}
4,469✔
694

695
/* -- Bytecode emitter for branches --------------------------------------- */
696

697
/* Emit unconditional branch. */
698
static BCPos bcemit_jmp(FuncState *fs)
618,266✔
699
{
700
  BCPos jpc = fs->jpc;
618,266✔
701
  BCPos j = fs->pc - 1;
618,266✔
702
  BCIns *ip = &fs->bcbase[j].ins;
618,266✔
703
  fs->jpc = NO_JMP;
618,266✔
704
  if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) {
618,266✔
705
    setbc_j(ip, NO_JMP);
35✔
706
    fs->lasttarget = j+1;
35✔
707
  } else {
708
    j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP);
618,231✔
709
  }
710
  jmp_append(fs, &j, jpc);
618,266✔
711
  return j;
618,266✔
712
}
713

714
/* Invert branch condition of bytecode instruction. */
715
static void invertcond(FuncState *fs, ExpDesc *e)
199,327✔
716
{
717
  BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins;
199,327✔
718
  setbc_op(ip, bc_op(*ip)^1);
199,327✔
719
}
720

721
/* Emit conditional branch. */
722
static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)
90,778✔
723
{
724
  BCPos pc;
90,778✔
725
  if (e->k == VRELOCABLE) {
90,778✔
726
    BCIns *ip = bcptr(fs, e);
46,188✔
727
    if (bc_op(*ip) == BC_NOT) {
46,188✔
728
      *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));
44,592✔
729
      return bcemit_jmp(fs);
44,592✔
730
    }
731
  }
732
  if (e->k != VNONRELOC) {
46,186✔
733
    bcreg_reserve(fs, 1);
1,598✔
734
    expr_toreg_nobranch(fs, e, fs->freereg-1);
1,598✔
735
  }
736
  bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);
78,042✔
737
  pc = bcemit_jmp(fs);
46,186✔
738
  expr_free(fs, e);
46,186✔
739
  return pc;
740
}
741

742
/* Emit branch on true condition. */
743
static void bcemit_branch_t(FuncState *fs, ExpDesc *e)
190,379✔
744
{
745
  BCPos pc;
190,379✔
746
  expr_discharge(fs, e);
190,379✔
747
  if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
190,379✔
748
    pc = NO_JMP;  /* Never jump. */
749
  else if (e->k == VJMP)
175,594✔
750
    invertcond(fs, e), pc = e->u.s.info;
80,398✔
751
  else if (e->k == VKFALSE || e->k == VKNIL)
95,196✔
752
    expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
22,859✔
753
  else
754
    pc = bcemit_branch(fs, e, 0);
72,337✔
755
  jmp_append(fs, &e->f, pc);
190,379✔
756
  jmp_tohere(fs, e->t);
190,379✔
757
  e->t = NO_JMP;
190,379✔
758
}
190,379✔
759

760
/* Emit branch on false condition. */
761
static void bcemit_branch_f(FuncState *fs, ExpDesc *e)
86,558✔
762
{
763
  BCPos pc;
86,558✔
764
  expr_discharge(fs, e);
86,558✔
765
  if (e->k == VKNIL || e->k == VKFALSE)
86,558✔
766
    pc = NO_JMP;  /* Never jump. */
767
  else if (e->k == VJMP)
65,253✔
768
    pc = e->u.s.info;
33,340✔
769
  else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
31,913✔
770
    expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
13,472✔
771
  else
772
    pc = bcemit_branch(fs, e, 1);
18,441✔
773
  jmp_append(fs, &e->t, pc);
86,558✔
774
  jmp_tohere(fs, e->f);
86,558✔
775
  e->f = NO_JMP;
86,558✔
776
}
86,558✔
777

778
/* -- Bytecode emitter for operators -------------------------------------- */
779

780
/* Try constant-folding of arithmetic operators. */
781
static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)
12,962✔
782
{
783
  TValue o;
12,962✔
784
  lua_Number n;
12,962✔
785
  if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;
12,962✔
786
  n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);
980✔
787
  setnumV(&o, n);
980✔
788
  if (tvisnan(&o) || tvismzero(&o)) return 0;  /* Avoid NaN and -0 as consts. */
980✔
789
  if (LJ_DUALNUM) {
963✔
790
    int32_t k = lj_num2int(n);
791
    if ((lua_Number)k == n) {
792
      setintV(&e1->u.nval, k);
793
      return 1;
794
    }
795
  }
796
  setnumV(&e1->u.nval, n);
963✔
797
  return 1;
963✔
798
}
799

800
/* Emit arithmetic operator. */
801
static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
12,962✔
802
{
803
  BCReg rb, rc, t;
12,962✔
804
  uint32_t op;
12,962✔
805
  if (foldarith(opr, e1, e2))
12,962✔
806
    return;
807
  if (opr == OPR_POW) {
11,999✔
808
    op = BC_POW;
68✔
809
    rc = expr_toanyreg(fs, e2);
68✔
810
    rb = expr_toanyreg(fs, e1);
68✔
811
  } else {
812
    op = opr-OPR_ADD+BC_ADDVV;
11,931✔
813
    /* Must discharge 2nd operand first since VINDEXED might free regs. */
814
    expr_toval(fs, e2);
11,931✔
815
    if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)
11,931✔
816
      op -= BC_ADDVV-BC_ADDVN;
8,924✔
817
    else
818
      rc = expr_toanyreg(fs, e2);
3,007✔
819
    /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
820
    lj_assertFS(expr_isnumk(e1) || e1->k == VNONRELOC,
11,931✔
821
                "bad expr type %d", e1->k);
822
    expr_toval(fs, e1);
11,931✔
823
    /* Avoid two consts to satisfy bytecode constraints. */
824
    if (expr_isnumk(e1) && !expr_isnumk(e2) &&
11,931✔
825
        (t = const_num(fs, e1)) <= BCMAX_B) {
1,163✔
826
      rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;
1,163✔
827
    } else {
828
      rb = expr_toanyreg(fs, e1);
10,768✔
829
    }
830
  }
831
  /* Using expr_free might cause asserts if the order is wrong. */
832
  if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
11,999✔
833
  if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
11,999✔
834
  e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);
11,999✔
835
  e1->k = VRELOCABLE;
11,999✔
836
}
837

838
/* Emit comparison operator. */
839
static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
337,887✔
840
{
841
  ExpDesc *eret = e1;
337,887✔
842
  BCIns ins;
337,887✔
843
  expr_toval(fs, e1);
337,887✔
844
  if (opr == OPR_EQ || opr == OPR_NE) {
337,887✔
845
    BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;
140,063✔
846
    BCReg ra;
140,063✔
847
    if (expr_isk(e1)) { e1 = e2; e2 = eret; }  /* Need constant in 2nd arg. */
140,063✔
848
    ra = expr_toanyreg(fs, e1);  /* First arg must be in a reg. */
140,063✔
849
    expr_toval(fs, e2);
140,063✔
850
    switch (e2->k) {
140,063✔
851
    case VKNIL: case VKFALSE: case VKTRUE:
47,009✔
852
      ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));
47,009✔
853
      break;
47,009✔
854
    case VKSTR:
2,101✔
855
      ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));
2,101✔
856
      break;
2,101✔
857
    case VKNUM:
2,853✔
858
      ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));
2,853✔
859
      break;
2,853✔
860
    default:
88,100✔
861
      ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));
88,100✔
862
      break;
88,100✔
863
    }
864
  } else {
865
    uint32_t op = opr-OPR_LT+BC_ISLT;
197,824✔
866
    BCReg ra, rd;
197,824✔
867
    if ((op-BC_ISLT) & 1) {  /* GT -> LT, GE -> LE */
197,824✔
868
      e1 = e2; e2 = eret;  /* Swap operands. */
98,990✔
869
      op = ((op-BC_ISLT)^3)+BC_ISLT;
98,990✔
870
      expr_toval(fs, e1);
98,990✔
871
      ra = expr_toanyreg(fs, e1);
98,990✔
872
      rd = expr_toanyreg(fs, e2);
98,990✔
873
    } else {
874
      rd = expr_toanyreg(fs, e2);
98,834✔
875
      ra = expr_toanyreg(fs, e1);
98,834✔
876
    }
877
    ins = BCINS_AD(op, ra, rd);
197,824✔
878
  }
879
  /* Using expr_free might cause asserts if the order is wrong. */
880
  if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
337,887✔
881
  if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
337,887✔
882
  bcemit_INS(fs, ins);
337,887✔
883
  eret->u.s.info = bcemit_jmp(fs);
337,887✔
884
  eret->k = VJMP;
337,887✔
885
}
337,887✔
886

887
/* Fixup left side of binary operator. */
888
static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)
527,919✔
889
{
890
  if (op == OPR_AND) {
527,919✔
891
    bcemit_branch_t(fs, e);
86,199✔
892
  } else if (op == OPR_OR) {
441,720✔
893
    bcemit_branch_f(fs, e);
86,558✔
894
  } else if (op == OPR_CONCAT) {
355,162✔
895
    expr_tonextreg(fs, e);
4,047✔
896
  } else if (op == OPR_EQ || op == OPR_NE) {
351,115✔
897
    if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);
140,067✔
898
  } else {
899
    if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);
211,048✔
900
  }
901
}
527,919✔
902

903
/* Emit binary operator. */
904
static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
527,455✔
905
{
906
  if (op <= OPR_POW) {
527,455✔
907
    bcemit_arith(fs, op, e1, e2);
12,962✔
908
  } else if (op == OPR_AND) {
514,493✔
909
    lj_assertFS(e1->t == NO_JMP, "jump list not closed");
86,199✔
910
    expr_discharge(fs, e2);
86,199✔
911
    jmp_append(fs, &e2->f, e1->f);
86,199✔
912
    *e1 = *e2;
86,199✔
913
  } else if (op == OPR_OR) {
428,294✔
914
    lj_assertFS(e1->f == NO_JMP, "jump list not closed");
86,558✔
915
    expr_discharge(fs, e2);
86,558✔
916
    jmp_append(fs, &e2->t, e1->t);
86,558✔
917
    *e1 = *e2;
86,558✔
918
  } else if (op == OPR_CONCAT) {
341,736✔
919
    expr_toval(fs, e2);
3,849✔
920
    if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
3,849✔
921
      lj_assertFS(e1->u.s.info == bc_b(*bcptr(fs, e2))-1,
1,249✔
922
                  "bad CAT stack layout");
923
      expr_free(fs, e1);
1,249✔
924
      setbc_b(bcptr(fs, e2), e1->u.s.info);
1,249✔
925
      e1->u.s.info = e2->u.s.info;
1,249✔
926
    } else {
927
      expr_tonextreg(fs, e2);
2,600✔
928
      expr_free(fs, e2);
2,600✔
929
      expr_free(fs, e1);
2,600✔
930
      e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);
2,600✔
931
    }
932
    e1->k = VRELOCABLE;
3,849✔
933
  } else {
934
    lj_assertFS(op == OPR_NE || op == OPR_EQ ||
337,887✔
935
               op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT,
936
               "bad binop %d", op);
937
    bcemit_comp(fs, op, e1, e2);
337,887✔
938
  }
939
}
527,455✔
940

941
/* Emit unary operator. */
942
static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
299,686✔
943
{
944
  if (op == BC_NOT) {
299,686✔
945
    /* Swap true and false lists. */
946
    { BCPos temp = e->f; e->f = e->t; e->t = temp; }
296,277✔
947
    jmp_dropval(fs, e->f);
296,277✔
948
    jmp_dropval(fs, e->t);
296,277✔
949
    expr_discharge(fs, e);
296,277✔
950
    if (e->k == VKNIL || e->k == VKFALSE) {
296,277✔
951
      e->k = VKTRUE;
35,858✔
952
      return;
35,858✔
953
    } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {
260,419✔
954
      e->k = VKFALSE;
3,090✔
955
      return;
3,090✔
956
    } else if (e->k == VJMP) {
257,329✔
957
      invertcond(fs, e);
118,929✔
958
      return;
118,929✔
959
    } else if (e->k == VRELOCABLE) {
138,400✔
960
      bcreg_reserve(fs, 1);
20,170✔
961
      setbc_a(bcptr(fs, e), fs->freereg-1);
20,170✔
962
      e->u.s.info = fs->freereg-1;
20,170✔
963
      e->k = VNONRELOC;
20,170✔
964
    } else {
965
      lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k);
966
    }
967
  } else {
968
    lj_assertFS(op == BC_UNM || op == BC_LEN, "bad unop %d", op);
3,409✔
969
    if (op == BC_UNM && !expr_hasjump(e)) {  /* Constant-fold negations. */
3,409✔
970
#if LJ_HASFFI
971
      if (e->k == VKCDATA) {  /* Fold in-place since cdata is not interned. */
2,306✔
972
        GCcdata *cd = cdataV(&e->u.nval);
4✔
973
        int64_t *p = (int64_t *)cdataptr(cd);
4✔
974
        if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
4✔
975
          p[1] ^= (int64_t)U64x(80000000,00000000);
×
976
        else
977
          *p = -*p;
4✔
978
        return;
4✔
979
      } else
980
#endif
981
      if (expr_isnumk(e) && !expr_numiszero(e)) {  /* Avoid folding to -0. */
2,302✔
982
        TValue *o = expr_numtv(e);
2,164✔
983
        if (tvisint(o)) {
2,164✔
984
          int32_t k = intV(o);
985
          if (k == -k)
986
            setnumV(o, -(lua_Number)k);
987
          else
988
            setintV(o, -k);
989
          return;
990
        } else {
991
          o->u64 ^= U64x(80000000,00000000);
2,164✔
992
          return;
2,164✔
993
        }
994
      }
995
    }
996
    expr_toanyreg(fs, e);
1,241✔
997
  }
998
  expr_free(fs, e);
139,641✔
999
  e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);
139,641✔
1000
  e->k = VRELOCABLE;
139,641✔
1001
}
1002

1003
/* -- Lexer support ------------------------------------------------------- */
1004

1005
/* Check and consume optional token. */
1006
static int lex_opt(LexState *ls, LexToken tok)
2,932,817✔
1007
{
1008
  if (ls->tok == tok) {
2,932,817✔
1009
    lj_lex_next(ls);
863,094✔
1010
    return 1;
70,710✔
1011
  }
1012
  return 0;
1013
}
1014

1015
/* Check and consume token. */
1016
static void lex_check(LexState *ls, LexToken tok)
613,435✔
1017
{
1018
  if (ls->tok != tok)
613,435✔
1019
    err_token(ls, tok);
3,687✔
1020
  lj_lex_next(ls);
609,748✔
1021
}
609,731✔
1022

1023
/* Check for matching token. */
1024
static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)
795,278✔
1025
{
1026
  if (!lex_opt(ls, what)) {
795,278✔
1027
    if (line == ls->linenumber) {
965✔
1028
      err_token(ls, what);
681✔
1029
    } else {
1030
      const char *swhat = lj_lex_token2str(ls, what);
284✔
1031
      const char *swho = lj_lex_token2str(ls, who);
284✔
1032
      lj_lex_error(ls, ls->tok, LJ_ERR_XMATCH, swhat, swho, line);
284✔
1033
    }
1034
  }
1035
}
794,305✔
1036

1037
/* Check for string token. */
1038
static GCstr *lex_str(LexState *ls)
1,081,986✔
1039
{
1040
  GCstr *s;
1,081,986✔
1041
  if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto))
1,081,986✔
1042
    err_token(ls, TK_name);
220✔
1043
  s = strV(&ls->tokval);
1,081,766✔
1044
  lj_lex_next(ls);
1,081,766✔
1045
  return s;
1,081,766✔
1046
}
1047

1048
/* -- Variable handling --------------------------------------------------- */
1049

1050
#define var_get(ls, fs, i)        ((ls)->vstack[(fs)->varmap[(i)]])
1051

1052
/* Define a new local variable. */
1053
static void var_new(LexState *ls, BCReg n, GCstr *name)
96,384✔
1054
{
1055
  FuncState *fs = ls->fs;
96,384✔
1056
  MSize vtop = ls->vtop;
96,384✔
1057
  checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables");
96,384✔
1058
  if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
96,383✔
1059
    if (ls->sizevstack >= LJ_MAX_VSTACK)
26,673✔
1060
      lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
×
1061
    lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
26,673✔
1062
  }
1063
  lj_assertFS((uintptr_t)name < VARNAME__MAX ||
96,383✔
1064
              lj_tab_getstr(fs->kt, name) != NULL,
1065
              "unanchored variable name");
1066
  /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1067
  setgcref(ls->vstack[vtop].name, obj2gco(name));
96,383✔
1068
  fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
96,383✔
1069
  ls->vtop = vtop+1;
96,383✔
1070
}
96,383✔
1071

1072
#define var_new_lit(ls, n, v) \
1073
  var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1))
1074

1075
#define var_new_fixed(ls, n, vn) \
1076
  var_new(ls, (n), (GCstr *)(uintptr_t)(vn))
1077

1078
/* Add local variables. */
1079
static void var_add(LexState *ls, BCReg nvars)
81,410✔
1080
{
1081
  FuncState *fs = ls->fs;
81,410✔
1082
  BCReg nactvar = fs->nactvar;
81,410✔
1083
  while (nvars--) {
177,506✔
1084
    VarInfo *v = &var_get(ls, fs, nactvar);
96,096✔
1085
    v->startpc = fs->pc;
96,096✔
1086
    v->slot = nactvar++;
96,096✔
1087
    v->info = 0;
96,096✔
1088
  }
1089
  fs->nactvar = nactvar;
81,410✔
1090
}
1091

1092
/* Remove local variables. */
1093
static void var_remove(LexState *ls, BCReg tolevel)
245,096✔
1094
{
1095
  FuncState *fs = ls->fs;
245,096✔
1096
  while (fs->nactvar > tolevel)
336,597✔
1097
    var_get(ls, fs, --fs->nactvar).endpc = fs->pc;
91,501✔
1098
}
1099

1100
/* Lookup local variable name. */
1101
static BCReg var_lookup_local(FuncState *fs, GCstr *n)
946,466✔
1102
{
1103
  int i;
946,466✔
1104
  for (i = fs->nactvar-1; i >= 0; i--) {
2,682,574✔
1105
    if (n == strref(var_get(fs->ls, fs, i).name))
2,215,532✔
1106
      return (BCReg)i;
479,424✔
1107
  }
1108
  return (BCReg)-1;  /* Not found. */
1109
}
1110

1111
/* Lookup or add upvalue index. */
1112
static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)
1113
{
1114
  MSize i, n = fs->nuv;
1115
  for (i = 0; i < n; i++)
1116
    if (fs->uvmap[i] == vidx)
1117
      return i;  /* Already exists. */
1118
  /* Otherwise create a new one. */
1119
  checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");
1120
  lj_assertFS(e->k == VLOCAL || e->k == VUPVAL, "bad expr type %d", e->k);
1121
  fs->uvmap[n] = (uint16_t)vidx;
1122
  fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info);
1123
  fs->nuv = n+1;
1124
  return n;
1125
}
1126

1127
/* Forward declaration. */
1128
static void fscope_uvmark(FuncState *fs, BCReg level);
1129

1130
/* Recursively lookup variables in enclosing functions. */
1131
static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)
1,359,162✔
1132
{
1133
  if (fs) {
1,359,162✔
1134
    BCReg reg = var_lookup_local(fs, name);
946,466✔
1135
    if ((int32_t)reg >= 0) {  /* Local in this function? */
946,466✔
1136
      expr_init(e, VLOCAL, reg);
479,424✔
1137
      if (!first)
479,424✔
1138
        fscope_uvmark(fs, reg);  /* Scope now has an upvalue. */
491,231✔
1139
      return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);
479,424✔
1140
    } else {
1141
      MSize vidx = var_lookup_(fs->prev, name, e, 0);  /* Var in outer func? */
467,042✔
1142
      if ((int32_t)vidx >= 0) {  /* Yes, make it an upvalue here. */
466,982✔
1143
        e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e);
14,194✔
1144
        e->k = VUPVAL;
14,193✔
1145
        return vidx;
14,193✔
1146
      }
1147
    }
1148
  } else {  /* Not found in any function, must be a global. */
1149
    expr_init(e, VGLOBAL, 0);
412,696✔
1150
    e->u.sval = name;
412,696✔
1151
  }
1152
  return (MSize)-1;  /* Global. */
1153
}
1154

1155
/* Lookup variable name. */
1156
#define var_lookup(ls, e) \
1157
  var_lookup_((ls)->fs, lex_str(ls), (e), 1)
1158

1159
/* -- Goto an label handling ---------------------------------------------- */
1160

1161
/* Add a new goto or label. */
1162
static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc)
98,735✔
1163
{
1164
  FuncState *fs = ls->fs;
98,735✔
1165
  MSize vtop = ls->vtop;
98,735✔
1166
  if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
98,735✔
1167
    if (ls->sizevstack >= LJ_MAX_VSTACK)
111✔
1168
      lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
×
1169
    lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
111✔
1170
  }
1171
  lj_assertFS(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL,
98,735✔
1172
              "unanchored label name");
1173
  /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1174
  setgcref(ls->vstack[vtop].name, obj2gco(name));
98,735✔
1175
  ls->vstack[vtop].startpc = pc;
98,735✔
1176
  ls->vstack[vtop].slot = (uint8_t)fs->nactvar;
98,735✔
1177
  ls->vstack[vtop].info = info;
98,735✔
1178
  ls->vtop = vtop+1;
98,735✔
1179
  return vtop;
98,735✔
1180
}
1181

1182
#define gola_isgoto(v)                ((v)->info & VSTACK_GOTO)
1183
#define gola_islabel(v)                ((v)->info & VSTACK_LABEL)
1184
#define gola_isgotolabel(v)        ((v)->info & (VSTACK_GOTO|VSTACK_LABEL))
1185

1186
/* Patch goto to jump to label. */
1187
static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl)
4✔
1188
{
1189
  FuncState *fs = ls->fs;
4✔
1190
  BCPos pc = vg->startpc;
4✔
1191
  setgcrefnull(vg->name);  /* Invalidate pending goto. */
4✔
1192
  setbc_a(&fs->bcbase[pc].ins, vl->slot);
4✔
1193
  jmp_patch(fs, pc, vl->startpc);
4✔
1194
}
4✔
1195

1196
/* Patch goto to close upvalues. */
1197
static void gola_close(LexState *ls, VarInfo *vg)
1198
{
1199
  FuncState *fs = ls->fs;
1200
  BCPos pc = vg->startpc;
1201
  BCIns *ip = &fs->bcbase[pc].ins;
1202
  lj_assertFS(gola_isgoto(vg), "expected goto");
1203
  lj_assertFS(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO,
1204
              "bad bytecode op %d", bc_op(*ip));
1205
  setbc_a(ip, vg->slot);
1206
  if (bc_op(*ip) == BC_JMP) {
1207
    BCPos next = jmp_next(fs, pc);
1208
    if (next != NO_JMP) jmp_patch(fs, next, pc);  /* Jump to UCLO. */
1209
    setbc_op(ip, BC_UCLO);  /* Turn into UCLO. */
1210
    setbc_j(ip, NO_JMP);
1211
  }
1212
}
1213

1214
/* Resolve pending forward gotos for label. */
1215
static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx)
1216
{
1217
  VarInfo *vg = ls->vstack + bl->vstart;
1218
  VarInfo *vl = ls->vstack + idx;
1219
  for (; vg < vl; vg++)
1220
    if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) {
1221
      if (vg->slot < vl->slot) {
1222
        GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name);
1223
        lj_assertLS((uintptr_t)name >= VARNAME__MAX, "expected goto name");
1224
        ls->linenumber = ls->fs->bcbase[vg->startpc].line;
1225
        lj_assertLS(strref(vg->name) != NAME_BREAK, "unexpected break");
1226
        lj_lex_error(ls, 0, LJ_ERR_XGSCOPE,
1227
                     strdata(strref(vg->name)), strdata(name));
1228
      }
1229
      gola_patch(ls, vg, vl);
1230
    }
1231
}
1232

1233
/* Fixup remaining gotos and labels for scope. */
1234
static void gola_fixup(LexState *ls, FuncScope *bl)
65,996✔
1235
{
1236
  VarInfo *v = ls->vstack + bl->vstart;
65,996✔
1237
  VarInfo *ve = ls->vstack + ls->vtop;
65,996✔
1238
  for (; v < ve; v++) {
132,501✔
1239
    GCstr *name = strref(v->name);
66,513✔
1240
    if (name != NULL) {  /* Only consider remaining valid gotos/labels. */
66,513✔
1241
      if (gola_islabel(v)) {
66,467✔
1242
        VarInfo *vg;
21✔
1243
        setgcrefnull(v->name);  /* Invalidate label that goes out of scope. */
21✔
1244
        for (vg = v+1; vg < ve; vg++)  /* Resolve pending backward gotos. */
45✔
1245
          if (strref(vg->name) == name && gola_isgoto(vg)) {
24✔
1246
            if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot)
4✔
1247
              gola_close(ls, vg);
1✔
1248
            gola_patch(ls, vg, v);
28✔
1249
          }
1250
      } else if (gola_isgoto(v)) {
66,446✔
1251
        if (bl->prev) {  /* Propagate goto or break to outer scope. */
65,990✔
1252
          bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA;
65,982✔
1253
          v->slot = bl->nactvar;
65,982✔
1254
          if ((bl->flags & FSCOPE_UPVAL))
65,982✔
1255
            gola_close(ls, v);
14✔
1256
        } else {  /* No outer scope: undefined goto label or no loop. */
1257
          ls->linenumber = ls->fs->bcbase[v->startpc].line;
8✔
1258
          if (name == NAME_BREAK)
8✔
1259
            lj_lex_error(ls, 0, LJ_ERR_XBREAK);
3✔
1260
          else
1261
            lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name));
5✔
1262
        }
1263
      }
1264
    }
1265
  }
1266
}
65,988✔
1267

1268
/* Find existing label. */
1269
static VarInfo *gola_findlabel(LexState *ls, GCstr *name)
59✔
1270
{
1271
  VarInfo *v = ls->vstack + ls->fs->bl->vstart;
59✔
1272
  VarInfo *ve = ls->vstack + ls->vtop;
59✔
1273
  for (; v < ve; v++)
138✔
1274
    if (strref(v->name) == name && gola_islabel(v))
85✔
1275
      return v;
1276
  return NULL;
1277
}
1278

1279
/* -- Scope handling ------------------------------------------------------ */
1280

1281
/* Begin a scope. */
1282
static void fscope_begin(FuncState *fs, FuncScope *bl, int flags)
245,985✔
1283
{
1284
  bl->nactvar = (uint8_t)fs->nactvar;
245,985✔
1285
  bl->flags = flags;
245,985✔
1286
  bl->vstart = fs->ls->vtop;
245,985✔
1287
  bl->prev = fs->bl;
245,985✔
1288
  fs->bl = bl;
245,985✔
1289
  lj_assertFS(fs->freereg == fs->nactvar, "bad regalloc");
245,985✔
1290
}
1291

1292
/* End a scope. */
1293
static void fscope_end(FuncState *fs)
245,096✔
1294
{
1295
  FuncScope *bl = fs->bl;
245,096✔
1296
  LexState *ls = fs->ls;
245,096✔
1297
  fs->bl = bl->prev;
245,096✔
1298
  var_remove(ls, bl->nactvar);
245,096✔
1299
  fs->freereg = fs->nactvar;
245,096✔
1300
  lj_assertFS(bl->nactvar == fs->nactvar, "bad regalloc");
245,096✔
1301
  if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL)
245,096✔
1302
    bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0);
117✔
1303
  if ((bl->flags & FSCOPE_BREAK)) {
245,096✔
1304
    if ((bl->flags & FSCOPE_LOOP)) {
115,303✔
1305
      MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc);
49,334✔
1306
      ls->vtop = idx;  /* Drop break label immediately. */
49,334✔
1307
      gola_resolve(ls, bl, idx);
49,334✔
1308
    } else {  /* Need the fixup step to propagate the breaks. */
1309
      gola_fixup(ls, bl);
65,969✔
1310
      return;
65,969✔
1311
    }
1312
  }
1313
  if ((bl->flags & FSCOPE_GOLA)) {
179,127✔
1314
    gola_fixup(ls, bl);
27✔
1315
  }
1316
}
1317

1318
/* Mark scope as having an upvalue. */
1319
static void fscope_uvmark(FuncState *fs, BCReg level)
11,807✔
1320
{
1321
  FuncScope *bl;
11,807✔
1322
  for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev)
11,870✔
1323
    ;
63✔
1324
  if (bl)
11,807✔
1325
    bl->flags |= FSCOPE_UPVAL;
11,807✔
1326
}
1327

1328
/* -- Function state management ------------------------------------------- */
1329

1330
/* Fixup bytecode for prototype. */
1331
static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n)
50,176✔
1332
{
1333
  BCInsLine *base = fs->bcbase;
50,176✔
1334
  MSize i;
50,176✔
1335
  pt->sizebc = n;
50,176✔
1336
  bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,
100,352✔
1337
                   fs->framesize, 0);
1338
  for (i = 1; i < n; i++)
3,657,751✔
1339
    bc[i] = base[i].ins;
3,607,575✔
1340
}
1341

1342
/* Fixup upvalues for child prototype, step #2. */
1343
static void fs_fixup_uv2(FuncState *fs, GCproto *pt)
1344
{
1345
  VarInfo *vstack = fs->ls->vstack;
1346
  uint16_t *uv = proto_uv(pt);
1347
  MSize i, n = pt->sizeuv;
1348
  for (i = 0; i < n; i++) {
1349
    VarIndex vidx = uv[i];
1350
    if (vidx >= LJ_MAX_VSTACK)
1351
      uv[i] = vidx - LJ_MAX_VSTACK;
1352
    else if ((vstack[vidx].info & VSTACK_VAR_RW))
1353
      uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL;
1354
    else
1355
      uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE;
1356
  }
1357
}
1358

1359
/* Fixup constants for prototype. */
1360
static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)
50,176✔
1361
{
1362
  GCtab *kt;
50,176✔
1363
  TValue *array;
50,176✔
1364
  Node *node;
50,176✔
1365
  MSize i, hmask;
50,176✔
1366
  checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");
50,176✔
1367
  checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");
50,175✔
1368
  setmref(pt->k, kptr);
50,174✔
1369
  pt->sizekn = fs->nkn;
50,174✔
1370
  pt->sizekgc = fs->nkgc;
50,174✔
1371
  kt = fs->kt;
50,174✔
1372
  array = tvref(kt->array);
50,174✔
1373
  for (i = 0; i < kt->asize; i++)
54,042✔
1374
    if (tvhaskslot(&array[i])) {
3,868✔
1375
      TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])];
1,474✔
1376
      if (LJ_DUALNUM)
1,474✔
1377
        setintV(tv, (int32_t)i);
1378
      else
1379
        setnumV(tv, (lua_Number)i);
1,474✔
1380
    }
1381
  node = noderef(kt->node);
50,174✔
1382
  hmask = kt->hmask;
50,174✔
1383
  for (i = 0; i <= hmask; i++) {
1,286,911✔
1384
    Node *n = &node[i];
1,236,737✔
1385
    if (tvhaskslot(&n->val)) {
1,236,737✔
1386
      ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);
333,022✔
1387
      lj_assertFS(!tvisint(&n->key), "unexpected integer key");
333,022✔
1388
      if (tvisnum(&n->key)) {
333,022✔
1389
        TValue *tv = &((TValue *)kptr)[kidx];
71,807✔
1390
        if (LJ_DUALNUM) {
71,807✔
1391
          lua_Number nn = numV(&n->key);
1392
          int32_t k = lj_num2int(nn);
1393
          lj_assertFS(!tvismzero(&n->key), "unexpected -0 key");
1394
          if ((lua_Number)k == nn)
1395
            setintV(tv, k);
1396
          else
1397
            *tv = n->key;
1398
        } else {
1399
          *tv = n->key;
71,807✔
1400
        }
1401
      } else {
1402
        GCobj *o = gcV(&n->key);
261,215✔
1403
        setgcref(((GCRef *)kptr)[~kidx], o);
261,215✔
1404
        lj_gc_objbarrier(fs->L, pt, o);
261,215✔
1405
        if (tvisproto(&n->key))
261,215✔
1406
          fs_fixup_uv2(fs, gco2pt(o));
11,710✔
1407
      }
1408
    }
1409
  }
1410
}
50,174✔
1411

1412
/* Fixup upvalues for prototype, step #1. */
1413
static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv)
50,174✔
1414
{
1415
  setmref(pt->uv, uv);
50,174✔
1416
  pt->sizeuv = fs->nuv;
50,174✔
1417
  memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex));
50,174✔
1418
}
1419

1420
#ifndef LUAJIT_DISABLE_DEBUGINFO
1421
/* Prepare lineinfo for prototype. */
1422
static size_t fs_prep_line(FuncState *fs, BCLine numline)
50,176✔
1423
{
1424
  return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
149✔
1425
}
1426

1427
/* Fixup lineinfo for prototype. */
1428
static void fs_fixup_line(FuncState *fs, GCproto *pt,
50,174✔
1429
                          void *lineinfo, BCLine numline)
1430
{
1431
  BCInsLine *base = fs->bcbase + 1;
50,174✔
1432
  BCLine first = fs->linedefined;
50,174✔
1433
  MSize i = 0, n = fs->pc-1;
50,174✔
1434
  pt->firstline = fs->linedefined;
50,174✔
1435
  pt->numline = numline;
50,174✔
1436
  setmref(pt->lineinfo, lineinfo);
50,174✔
1437
  if (LJ_LIKELY(numline < 256)) {
50,174✔
1438
    uint8_t *li = (uint8_t *)lineinfo;
3,304,102✔
1439
    do {
3,304,102✔
1440
      BCLine delta = base[i].line - first;
3,304,102✔
1441
      lj_assertFS(delta >= 0 && delta < 256, "bad line delta");
3,304,102✔
1442
      li[i] = (uint8_t)delta;
3,304,102✔
1443
    } while (++i < n);
3,304,102✔
1444
  } else if (LJ_LIKELY(numline < 65536)) {
147✔
1445
    uint16_t *li = (uint16_t *)lineinfo;
41,180✔
1446
    do {
41,180✔
1447
      BCLine delta = base[i].line - first;
41,180✔
1448
      lj_assertFS(delta >= 0 && delta < 65536, "bad line delta");
41,180✔
1449
      li[i] = (uint16_t)delta;
41,180✔
1450
    } while (++i < n);
41,180✔
1451
  } else {
1452
    uint32_t *li = (uint32_t *)lineinfo;
131,215✔
1453
    do {
131,215✔
1454
      BCLine delta = base[i].line - first;
131,215✔
1455
      lj_assertFS(delta >= 0, "bad line delta");
131,215✔
1456
      li[i] = (uint32_t)delta;
131,215✔
1457
    } while (++i < n);
131,215✔
1458
  }
1459
}
50,174✔
1460

1461
/* Prepare variable info for prototype. */
1462
static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)
50,176✔
1463
{
1464
  VarInfo *vs =ls->vstack, *ve;
50,176✔
1465
  MSize i, n;
50,176✔
1466
  BCPos lastpc;
50,176✔
1467
  lj_buf_reset(&ls->sb);  /* Copy to temp. string buffer. */
50,176✔
1468
  /* Store upvalue names. */
1469
  for (i = 0, n = fs->nuv; i < n; i++) {
58,500✔
1470
    GCstr *s = strref(vs[fs->uvmap[i]].name);
8,324✔
1471
    MSize len = s->len+1;
8,324✔
1472
    char *p = lj_buf_more(&ls->sb, len);
8,324✔
1473
    p = lj_buf_wmem(p, strdata(s), len);
8,324✔
1474
    setsbufP(&ls->sb, p);
8,324✔
1475
  }
1476
  *ofsvar = sbuflen(&ls->sb);
50,176✔
1477
  lastpc = 0;
50,176✔
1478
  /* Store local variable names and compressed ranges. */
1479
  for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) {
191,044✔
1480
    if (!gola_isgotolabel(vs)) {
140,868✔
1481
      GCstr *s = strref(vs->name);
91,497✔
1482
      BCPos startpc;
91,497✔
1483
      char *p;
91,497✔
1484
      if ((uintptr_t)s < VARNAME__MAX) {
91,497✔
1485
        p = lj_buf_more(&ls->sb, 1 + 2*5);
7,128✔
1486
        *p++ = (char)(uintptr_t)s;
7,128✔
1487
      } else {
1488
        MSize len = s->len+1;
84,369✔
1489
        p = lj_buf_more(&ls->sb, len + 2*5);
84,369✔
1490
        p = lj_buf_wmem(p, strdata(s), len);
84,369✔
1491
      }
1492
      startpc = vs->startpc;
91,497✔
1493
      p = lj_strfmt_wuleb128(p, startpc-lastpc);
91,497✔
1494
      p = lj_strfmt_wuleb128(p, vs->endpc-startpc);
91,497✔
1495
      setsbufP(&ls->sb, p);
91,497✔
1496
      lastpc = startpc;
91,497✔
1497
    }
1498
  }
1499
  lj_buf_putb(&ls->sb, '\0');  /* Terminator for varinfo. */
50,176✔
1500
  return sbuflen(&ls->sb);
50,176✔
1501
}
1502

1503
/* Fixup variable info for prototype. */
1504
static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar)
50,174✔
1505
{
1506
  setmref(pt->uvinfo, p);
50,174✔
1507
  setmref(pt->varinfo, (char *)p + ofsvar);
50,174✔
1508
  memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb));  /* Copy from temp. buffer. */
50,174✔
1509
}
1510
#else
1511

1512
/* Initialize with empty debug info, if disabled. */
1513
#define fs_prep_line(fs, numline)                (UNUSED(numline), 0)
1514
#define fs_fixup_line(fs, pt, li, numline) \
1515
  pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL)
1516
#define fs_prep_var(ls, fs, ofsvar)                (UNUSED(ofsvar), 0)
1517
#define fs_fixup_var(ls, pt, p, ofsvar) \
1518
  setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL)
1519

1520
#endif
1521

1522
/* Check if bytecode op returns. */
1523
static int bcopisret(BCOp op)
32,835✔
1524
{
1525
  switch (op) {
32,835✔
1526
  case BC_CALLMT: case BC_CALLT:
1527
  case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1528
    return 1;
1529
  default:
1530
    return 0;
1531
  }
1532
}
1533

1534
/* Fixup return instruction for prototype. */
1535
static void fs_fixup_ret(FuncState *fs)
50,184✔
1536
{
1537
  BCPos lastpc = fs->pc;
50,184✔
1538
  if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) {
50,184✔
1539
    if ((fs->bl->flags & FSCOPE_UPVAL))
27,250✔
1540
      bcemit_AJ(fs, BC_UCLO, 0, 0);
319✔
1541
    bcemit_AD(fs, BC_RET0, 0, 1);  /* Need final return. */
27,250✔
1542
  }
1543
  fs->bl->flags |= FSCOPE_NOCLOSE;  /* Handled above. */
50,184✔
1544
  fscope_end(fs);
50,184✔
1545
  lj_assertFS(fs->bl == NULL, "bad scope nesting");
50,176✔
1546
  /* May need to fixup returns encoded before first function was created. */
1547
  if (fs->flags & PROTO_FIXUP_RETURN) {
50,176✔
1548
    BCPos pc;
1549
    for (pc = 1; pc < lastpc; pc++) {
840✔
1550
      BCIns ins = fs->bcbase[pc].ins;
840✔
1551
      BCPos offset;
840✔
1552
      switch (bc_op(ins)) {
840✔
1553
      case BC_CALLMT: case BC_CALLT:
5✔
1554
      case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1555
        offset = bcemit_INS(fs, ins);  /* Copy original instruction. */
5✔
1556
        fs->bcbase[offset].line = fs->bcbase[pc].line;
5✔
1557
        offset = offset-(pc+1)+BCBIAS_J;
5✔
1558
        if (offset > BCMAX_D)
5✔
1559
          err_syntax(fs->ls, LJ_ERR_XFIXUP);
×
1560
        /* Replace with UCLO plus branch. */
1561
        fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset);
5✔
1562
        break;
5✔
1563
      case BC_FNEW:
1564
        return;  /* We're done. */
1565
      default:
1566
        break;
1567
      }
1568
    }
1569
  }
1570
}
1571

1572
/* Finish a FuncState and return the new prototype. */
1573
static GCproto *fs_finish(LexState *ls, BCLine line)
50,184✔
1574
{
1575
  lua_State *L = ls->L;
50,184✔
1576
  FuncState *fs = ls->fs;
50,184✔
1577
  BCLine numline = line - fs->linedefined;
50,184✔
1578
  size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;
50,184✔
1579
  GCproto *pt;
50,184✔
1580

1581
  /* Apply final fixups. */
1582
  fs_fixup_ret(fs);
50,184✔
1583

1584
  /* Calculate total size of prototype including all colocated arrays. */
1585
  sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
50,176✔
1586
  sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);
50,176✔
1587
  ofsk = sizept; sizept += fs->nkn*sizeof(TValue);
50,176✔
1588
  ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;
50,176✔
1589
  ofsli = sizept; sizept += fs_prep_line(fs, numline);
50,176✔
1590
  ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);
50,176✔
1591

1592
  /* Allocate prototype and initialize its fields. */
1593
  pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
50,176✔
1594
  pt->gct = ~LJ_TPROTO;
50,176✔
1595
  pt->sizept = (MSize)sizept;
50,176✔
1596
  pt->trace = 0;
50,176✔
1597
  pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN));
50,176✔
1598
  pt->numparams = fs->numparams;
50,176✔
1599
  pt->framesize = fs->framesize;
50,176✔
1600
  setgcref(pt->chunkname, obj2gco(ls->chunkname));
50,176✔
1601

1602
  /* Close potentially uninitialized gap between bc and kgc. */
1603
  *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0;
50,176✔
1604
  fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc);
50,176✔
1605
  fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk));
50,176✔
1606
  fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv));
50,174✔
1607
  fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline);
50,174✔
1608
  fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar);
50,174✔
1609

1610
  lj_vmevent_send(L, BC,
50,174✔
1611
    setprotoV(L, L->top++, pt);
1612
  );
50,174✔
1613

1614
  /* Add a new prototype to the profiler. */
1615
#if LJ_HASMEMPROF
1616
  lj_memprof_add_proto(pt);
50,174✔
1617
#endif
1618
#if LJ_HASSYSPROF
1619
  lj_sysprof_add_proto(pt);
50,174✔
1620
#endif
1621

1622
  L->top--;  /* Pop table of constants. */
50,174✔
1623
  ls->vtop = fs->vbase;  /* Reset variable stack. */
50,174✔
1624
  ls->fs = fs->prev;
50,174✔
1625
  lj_assertL(ls->fs != NULL || ls->tok == TK_eof, "bad parser state");
50,174✔
1626
  return pt;
50,174✔
1627
}
1628

1629
/* Initialize a new FuncState. */
1630
static void fs_init(LexState *ls, FuncState *fs)
66,542✔
1631
{
1632
  lua_State *L = ls->L;
66,542✔
1633
  fs->prev = ls->fs; ls->fs = fs;  /* Append to list. */
66,542✔
1634
  fs->ls = ls;
66,542✔
1635
  fs->vbase = ls->vtop;
66,542✔
1636
  fs->L = L;
66,542✔
1637
  fs->pc = 0;
66,542✔
1638
  fs->lasttarget = 0;
66,542✔
1639
  fs->jpc = NO_JMP;
66,542✔
1640
  fs->freereg = 0;
66,542✔
1641
  fs->nkgc = 0;
66,542✔
1642
  fs->nkn = 0;
66,542✔
1643
  fs->nactvar = 0;
66,542✔
1644
  fs->nuv = 0;
66,542✔
1645
  fs->bl = NULL;
66,542✔
1646
  fs->flags = 0;
66,542✔
1647
  fs->framesize = 1;  /* Minimum frame size. */
66,542✔
1648
  fs->kt = lj_tab_new(L, 0, 0);
66,542✔
1649
  /* Anchor table of constants in stack to avoid being collected. */
1650
  settabV(L, L->top, fs->kt);
66,542✔
1651
  incr_top(L);
66,542✔
1652
}
66,542✔
1653

1654
/* -- Expressions --------------------------------------------------------- */
1655

1656
/* Forward declaration. */
1657
static void expr(LexState *ls, ExpDesc *v);
1658

1659
/* Return string expression. */
1660
static void expr_str(LexState *ls, ExpDesc *e)
100,367✔
1661
{
1662
  expr_init(e, VKSTR, 0);
100,367✔
1663
  e->u.sval = lex_str(ls);
200,734✔
1664
}
1665

1666
/* Return index expression. */
1667
static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e)
1668
{
1669
  /* Already called: expr_toval(fs, e). */
1670
  t->k = VINDEXED;
1671
  if (expr_isnumk(e)) {
1672
#if LJ_DUALNUM
1673
    if (tvisint(expr_numtv(e))) {
1674
      int32_t k = intV(expr_numtv(e));
1675
      if (checku8(k)) {
1676
        t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1677
        return;
1678
      }
1679
    }
1680
#else
1681
    lua_Number n = expr_numberV(e);
1682
    int32_t k = lj_num2int(n);
1683
    if (checku8(k) && n == (lua_Number)k) {
1684
      t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1685
      return;
1686
    }
1687
#endif
1688
  } else if (expr_isstrk(e)) {
1689
    BCReg idx = const_str(fs, e);
1690
    if (idx <= BCMAX_C) {
1691
      t->u.s.aux = ~idx;  /* -256..-1: const string key */
1692
      return;
1693
    }
1694
  }
1695
  t->u.s.aux = expr_toanyreg(fs, e);  /* 0..255: register */
1696
}
1697

1698
/* Parse index expression with named field. */
1699
static void expr_field(LexState *ls, ExpDesc *v)
19,538✔
1700
{
1701
  FuncState *fs = ls->fs;
19,538✔
1702
  ExpDesc key;
19,538✔
1703
  expr_toanyreg(fs, v);
19,538✔
1704
  lj_lex_next(ls);  /* Skip dot or colon. */
19,538✔
1705
  expr_str(ls, &key);
19,538✔
1706
  expr_index(fs, v, &key);
19,538✔
1707
}
19,538✔
1708

1709
/* Parse index expression with brackets. */
1710
static void expr_bracket(LexState *ls, ExpDesc *v)
7,955✔
1711
{
1712
  lj_lex_next(ls);  /* Skip '['. */
7,955✔
1713
  expr(ls, v);
15,910✔
1714
  expr_toval(ls->fs, v);
7,955✔
1715
  lex_check(ls, ']');
7,955✔
1716
}
7,955✔
1717

1718
/* Get value of constant expression. */
1719
static void expr_kvalue(FuncState *fs, TValue *v, ExpDesc *e)
368,178✔
1720
{
1721
  UNUSED(fs);
368,178✔
1722
  if (e->k <= VKTRUE) {
368,178✔
1723
    setpriV(v, ~(uint32_t)e->k);
638✔
1724
  } else if (e->k == VKSTR) {
367,540✔
1725
    setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);
258,311✔
1726
  } else {
1727
    lj_assertFS(tvisnumber(expr_numtv(e)), "bad number constant");
235,031✔
1728
    *v = *expr_numtv(e);
235,031✔
1729
  }
1730
}
1731

1732
/* Parse table constructor expression. */
1733
static void expr_table(LexState *ls, ExpDesc *e)
5,392✔
1734
{
1735
  FuncState *fs = ls->fs;
5,392✔
1736
  BCLine line = ls->linenumber;
5,392✔
1737
  GCtab *t = NULL;
5,392✔
1738
  int vcall = 0, needarr = 0, fixt = 0;
5,392✔
1739
  uint32_t narr = 1;  /* First array index. */
5,392✔
1740
  uint32_t nhash = 0;  /* Number of hash entries. */
5,392✔
1741
  BCReg freg = fs->freereg;
5,392✔
1742
  BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0);
5,392✔
1743
  expr_init(e, VNONRELOC, freg);
5,392✔
1744
  bcreg_reserve(fs, 1);
5,392✔
1745
  freg++;
5,392✔
1746
  lex_check(ls, '{');
5,392✔
1747
  while (ls->tok != '}') {
191,469✔
1748
    ExpDesc key, val;
188,696✔
1749
    vcall = 0;
188,696✔
1750
    if (ls->tok == '[') {
188,696✔
1751
      expr_bracket(ls, &key);  /* Already calls expr_toval. */
285✔
1752
      if (!expr_isk(&key)) expr_index(fs, e, &key);
285✔
1753
      if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++;
285✔
1754
      lex_check(ls, '=');
285✔
1755
    } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) &&
265,228✔
1756
               lj_lex_lookahead(ls) == '=') {
76,817✔
1757
      expr_str(ls, &key);
76,360✔
1758
      lex_check(ls, '=');
76,360✔
1759
      nhash++;
76,360✔
1760
    } else {
1761
      expr_init(&key, VKNUM, 0);
112,051✔
1762
      setintV(&key.u.nval, (int)narr);
112,051✔
1763
      narr++;
112,051✔
1764
      needarr = vcall = 1;
112,051✔
1765
    }
1766
    expr(ls, &val);
377,194✔
1767
    if (expr_isk(&key) && key.k != VKNIL &&
188,498✔
1768
        (key.k == VKSTR || expr_isk_nojump(&val))) {
111,958✔
1769
      TValue k, *v;
186,366✔
1770
      if (!t) {  /* Create template table on demand. */
186,366✔
1771
        BCReg kidx;
3,034✔
1772
        t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash));
4,693✔
1773
        kidx = const_gc(fs, obj2gco(t), LJ_TTAB);
3,034✔
1774
        fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);
3,034✔
1775
      }
1776
      vcall = 0;
186,366✔
1777
      expr_kvalue(fs, &k, &key);
186,366✔
1778
      v = lj_tab_set(fs->L, t, &k);
186,366✔
1779
      lj_gc_anybarriert(fs->L, t);
186,366✔
1780
      if (expr_isk_nojump(&val)) {  /* Add const key/value to template table. */
186,366✔
1781
        expr_kvalue(fs, v, &val);
181,812✔
1782
      } else {  /* Otherwise create dummy string key (avoids lj_tab_newkey). */
1783
        settabV(fs->L, v, t);  /* Preserve key with table itself as value. */
4,554✔
1784
        fixt = 1;   /* Fix this later, after all resizes. */
4,554✔
1785
        goto nonconst;
4,554✔
1786
      }
1787
    } else {
1788
    nonconst:
2,132✔
1789
      if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; }
6,686✔
1790
      if (expr_isk(&key)) expr_index(fs, e, &key);
6,686✔
1791
      bcemit_store(fs, e, &val);
6,686✔
1792
    }
1793
    fs->freereg = freg;
188,498✔
1794
    if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break;
188,498✔
1795
  }
1796
  lex_match(ls, '}', '{', line);
5,194✔
1797
  if (vcall) {
5,192✔
1798
    BCInsLine *ilp = &fs->bcbase[fs->pc-1];
295✔
1799
    ExpDesc en;
295✔
1800
    lj_assertFS(bc_a(ilp->ins) == freg &&
295✔
1801
                bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB),
1802
                "bad CALL code generation");
1803
    expr_init(&en, VKNUM, 0);
295✔
1804
    en.u.nval.u32.lo = narr-1;
295✔
1805
    en.u.nval.u32.hi = 0x43300000;  /* Biased integer to avoid denormals. */
295✔
1806
    if (narr > 256) { fs->pc--; ilp--; }
295✔
1807
    ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en));
295✔
1808
    setbc_b(&ilp[-1].ins, 0);
295✔
1809
  }
1810
  if (pc == fs->pc-1) {  /* Make expr relocable if possible. */
5,192✔
1811
    e->u.s.info = pc;
3,658✔
1812
    fs->freereg--;
3,658✔
1813
    e->k = VRELOCABLE;
3,658✔
1814
  } else {
1815
    e->k = VNONRELOC;  /* May have been changed by expr_index. */
1,534✔
1816
  }
1817
  if (!t) {  /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */
5,192✔
1818
    BCIns *ip = &fs->bcbase[pc].ins;
2,160✔
1819
    if (!needarr) narr = 0;
2,160✔
1820
    else if (narr < 3) narr = 3;
386✔
1821
    else if (narr > 0x7ff) narr = 0x7ff;
1822
    setbc_d(ip, narr|(hsize2hbits(nhash)<<11));
2,160✔
1823
  } else {
1824
    if (needarr && t->asize < narr)
3,032✔
1825
      lj_tab_reasize(fs->L, t, narr-1);
33✔
1826
    if (fixt) {  /* Fix value for dummy keys in template table. */
3,032✔
1827
      Node *node = noderef(t->node);
1,008✔
1828
      uint32_t i, hmask = t->hmask;
1,008✔
1829
      for (i = 0; i <= hmask; i++) {
7,242✔
1830
        Node *n = &node[i];
6,234✔
1831
        if (tvistab(&n->val)) {
6,234✔
1832
          lj_assertFS(tabV(&n->val) == t, "bad dummy key in template table");
4,554✔
1833
          setnilV(&n->val);  /* Turn value into nil. */
4,554✔
1834
        }
1835
      }
1836
    }
1837
    lj_gc_check(fs->L);
3,032✔
1838
  }
1839
}
5,192✔
1840

1841
/* Parse function parameters. */
1842
static BCReg parse_params(LexState *ls, int needself)
15,400✔
1843
{
1844
  FuncState *fs = ls->fs;
15,400✔
1845
  BCReg nparams = 0;
15,400✔
1846
  lex_check(ls, '(');
15,400✔
1847
  if (needself)
15,303✔
1848
    var_new_lit(ls, nparams++, "self");
31✔
1849
  if (ls->tok != ')') {
15,303✔
1850
    do {
22,938✔
1851
      if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
22,938✔
1852
        var_new(ls, nparams++, lex_str(ls));
22,527✔
1853
      } else if (ls->tok == TK_dots) {
411✔
1854
        lj_lex_next(ls);
355✔
1855
        fs->flags |= PROTO_VARARG;
355✔
1856
        break;
355✔
1857
      } else {
1858
        err_syntax(ls, LJ_ERR_XPARAM);
56✔
1859
      }
1860
    } while (lex_opt(ls, ','));
22,527✔
1861
  }
1862
  var_add(ls, nparams);
15,247✔
1863
  lj_assertFS(fs->nactvar == nparams, "bad regalloc");
15,247✔
1864
  bcreg_reserve(fs, nparams);
15,247✔
1865
  lex_check(ls, ')');
15,247✔
1866
  return nparams;
15,192✔
1867
}
1868

1869
/* Forward declaration. */
1870
static void parse_chunk(LexState *ls);
1871

1872
/* Parse body of a function. */
1873
static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
15,400✔
1874
{
1875
  FuncState fs, *pfs = ls->fs;
15,400✔
1876
  FuncScope bl;
15,400✔
1877
  GCproto *pt;
15,400✔
1878
  ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
15,400✔
1879
  fs_init(ls, &fs);
15,400✔
1880
  fscope_begin(&fs, &bl, 0);
15,400✔
1881
  fs.linedefined = line;
15,400✔
1882
  fs.numparams = (uint8_t)parse_params(ls, needself);
15,400✔
1883
  fs.bcbase = pfs->bcbase + pfs->pc;
15,192✔
1884
  fs.bclim = pfs->bclim - pfs->pc;
15,192✔
1885
  bcemit_AD(&fs, BC_FUNCF, 0, 0);  /* Placeholder. */
15,192✔
1886
  parse_chunk(ls);
15,192✔
1887
  if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line);
12,231✔
1888
  pt = fs_finish(ls, (ls->lastline = ls->linenumber));
11,735✔
1889
  pfs->bcbase = ls->bcstack + oldbase;  /* May have been reallocated. */
11,734✔
1890
  pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
11,734✔
1891
  /* Store new prototype in the constant array of the parent. */
1892
  expr_init(e, VRELOCABLE,
35,202✔
1893
            bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));
11,734✔
1894
#if LJ_HASFFI
1895
  pfs->flags |= (fs.flags & PROTO_FFI);
11,734✔
1896
#endif
1897
  if (!(pfs->flags & PROTO_CHILD)) {
11,734✔
1898
    if (pfs->flags & PROTO_HAS_RETURN)
6,216✔
1899
      pfs->flags |= PROTO_FIXUP_RETURN;
241✔
1900
    pfs->flags |= PROTO_CHILD;
6,216✔
1901
  }
1902
  lj_lex_next(ls);
11,734✔
1903
}
11,734✔
1904

1905
/* Parse expression list. Last expression is left open. */
1906
static BCReg expr_list(LexState *ls, ExpDesc *v)
652,322✔
1907
{
1908
  BCReg n = 1;
652,322✔
1909
  expr(ls, v);
1,319,456✔
1910
  while (lex_opt(ls, ',')) {
667,134✔
1911
    expr_tonextreg(ls->fs, v);
22,920✔
1912
    expr(ls, v);
45,834✔
1913
    n++;
22,914✔
1914
  }
1915
  return n;
644,214✔
1916
}
1917

1918
/* Parse function argument list. */
1919
static void parse_args(LexState *ls, ExpDesc *e)
287,530✔
1920
{
1921
  FuncState *fs = ls->fs;
287,530✔
1922
  ExpDesc args;
287,530✔
1923
  BCIns ins;
287,530✔
1924
  BCReg base;
287,530✔
1925
  BCLine line = ls->linenumber;
287,530✔
1926
  if (ls->tok == '(') {
287,530✔
1927
#if !LJ_52
1928
    if (line != ls->lastline)
286,509✔
1929
      err_syntax(ls, LJ_ERR_XAMBIG);
1✔
1930
#endif
1931
    lj_lex_next(ls);
286,508✔
1932
    if (ls->tok == ')') {  /* f(). */
286,508✔
1933
      args.k = VVOID;
100,104✔
1934
    } else {
1935
      expr_list(ls, &args);
186,404✔
1936
      if (args.k == VCALL)  /* f(a, b, g()) or f(a, b, ...). */
183,983✔
1937
        setbc_b(bcptr(fs, &args), 0);  /* Pass on multiple results. */
2,545✔
1938
    }
1939
    lex_match(ls, ')', '(', line);
284,087✔
1940
  } else if (ls->tok == '{') {
1,021✔
1941
    expr_table(ls, &args);
110✔
1942
  } else if (ls->tok == TK_string) {
911✔
1943
    expr_init(&args, VKSTR, 0);
910✔
1944
    args.u.sval = strV(&ls->tokval);
910✔
1945
    lj_lex_next(ls);
910✔
1946
  } else {
1947
    err_syntax(ls, LJ_ERR_XFUNARG);
1✔
1948
    return;  /* Silence compiler. */
1949
  }
1950
  lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k);
284,811✔
1951
  base = e->u.s.info;  /* Base register for call. */
284,811✔
1952
  if (args.k == VCALL) {
284,811✔
1953
    ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);
2,545✔
1954
  } else {
1955
    if (args.k != VVOID)
282,266✔
1956
      expr_tonextreg(fs, &args);
182,162✔
1957
    ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2);
282,266✔
1958
  }
1959
  expr_init(e, VCALL, bcemit_INS(fs, ins));
284,811✔
1960
  e->u.s.aux = base;
284,811✔
1961
  fs->bcbase[fs->pc - 1].line = line;
284,811✔
1962
  fs->freereg = base+1;  /* Leave one result by default. */
284,811✔
1963
}
1964

1965
/* Parse primary expression. */
1966
static void expr_primary(LexState *ls, ExpDesc *v)
1,290,620✔
1967
{
1968
  FuncState *fs = ls->fs;
1,290,620✔
1969
  /* Parse prefix expression. */
1970
  if (ls->tok == '(') {
1,290,620✔
1971
    BCLine line = ls->linenumber;
399,611✔
1972
    lj_lex_next(ls);
399,611✔
1973
    expr(ls, v);
798,951✔
1974
    lex_match(ls, ')', '(', line);
399,340✔
1975
    expr_discharge(ls->fs, v);
399,194✔
1976
  } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
891,009✔
1977
    var_lookup(ls, v);
883,964✔
1978
  } else {
1979
    err_syntax(ls, LJ_ERR_XSYMBOL);
7,045✔
1980
  }
1981
  for (;;) {  /* Parse multiple expression suffixes. */
1,595,022✔
1982
    if (ls->tok == '.') {
1,595,022✔
1983
      expr_field(ls, v);
19,384✔
1984
    } else if (ls->tok == '[') {
1,575,638✔
1985
      ExpDesc key;
7,670✔
1986
      expr_toanyreg(fs, v);
7,670✔
1987
      expr_bracket(ls, &key);
7,670✔
1988
      expr_index(fs, v, &key);
7,670✔
1989
    } else if (ls->tok == ':') {
1,567,968✔
1990
      ExpDesc key;
4,469✔
1991
      lj_lex_next(ls);
4,469✔
1992
      expr_str(ls, &key);
4,469✔
1993
      bcemit_method(fs, v, &key);
4,469✔
1994
      parse_args(ls, v);
4,469✔
1995
    } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') {
1,563,499✔
1996
      expr_tonextreg(fs, v);
283,061✔
1997
      if (LJ_FR2) bcreg_reserve(fs, 1);
283,061✔
1998
      parse_args(ls, v);
283,061✔
1999
    } else {
2000
      break;
2001
    }
2002
  }
2003
}
1,280,438✔
2004

2005
/* Parse simple expression. */
2006
static void expr_simple(LexState *ls, ExpDesc *v)
1,907,025✔
2007
{
2008
  switch (ls->tok) {
1,907,025✔
2009
  case TK_number:
690,058✔
2010
    expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0);
690,058✔
2011
    copyTV(ls->L, &v->u.nval, &ls->tokval);
690,058✔
2012
    break;
2013
  case TK_string:
2014
    expr_init(v, VKSTR, 0);
210,399✔
2015
    v->u.sval = strV(&ls->tokval);
210,399✔
2016
    break;
210,399✔
2017
  case TK_nil:
2018
    expr_init(v, VKNIL, 0);
113,530✔
2019
    break;
2020
  case TK_true:
2021
    expr_init(v, VKTRUE, 0);
14,761✔
2022
    break;
2023
  case TK_false:
2024
    expr_init(v, VKFALSE, 0);
14,167✔
2025
    break;
2026
  case TK_dots: {  /* Vararg. */
504✔
2027
    FuncState *fs = ls->fs;
504✔
2028
    BCReg base;
504✔
2029
    checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS);
504✔
2030
    bcreg_reserve(fs, 1);
503✔
2031
    base = fs->freereg-1;
503✔
2032
    expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams));
503✔
2033
    v->u.s.aux = base;
503✔
2034
    break;
503✔
2035
  }
2036
  case '{':  /* Table constructor. */
5,282✔
2037
    expr_table(ls, v);
5,282✔
2038
    return;
5,282✔
2039
  case TK_function:
3,764✔
2040
    lj_lex_next(ls);
3,764✔
2041
    parse_body(ls, v, 0, ls->linenumber);
3,764✔
2042
    return;
3,764✔
2043
  default:
854,560✔
2044
    expr_primary(ls, v);
854,560✔
2045
    return;
854,560✔
2046
  }
2047
  lj_lex_next(ls);
1,043,418✔
2048
}
2049

2050
/* Manage syntactic levels to avoid blowing up the stack. */
2051
static void synlevel_begin(LexState *ls)
2,414,527✔
2052
{
2053
  if (++ls->level >= LJ_MAX_XLEVEL)
2,414,527✔
2054
    lj_lex_error(ls, 0, LJ_ERR_XLEVELS);
8✔
2055
}
2056

2057
#define synlevel_end(ls)        ((ls)->level--)
2058

2059
/* Convert token to binary operator. */
2060
static BinOpr token2binop(LexToken tok)
2,198,180✔
2061
{
2062
  switch (tok) {
2,198,180✔
2063
  case '+':        return OPR_ADD;
2064
  case '-':        return OPR_SUB;
3,714✔
2065
  case '*':        return OPR_MUL;
484✔
2066
  case '/':        return OPR_DIV;
147✔
2067
  case '%':        return OPR_MOD;
86✔
2068
  case '^':        return OPR_POW;
992✔
2069
  case TK_concat: return OPR_CONCAT;
4,047✔
2070
  case TK_ne:        return OPR_NE;
84,170✔
2071
  case TK_eq:        return OPR_EQ;
88,958✔
2072
  case '<':        return OPR_LT;
98,588✔
2073
  case TK_le:        return OPR_LE;
252✔
2074
  case '>':        return OPR_GT;
376✔
2075
  case TK_ge:        return OPR_GE;
98,635✔
2076
  case TK_and:        return OPR_AND;
102,678✔
2077
  case TK_or:        return OPR_OR;
102,964✔
2078
  default:        return OPR_NOBINOPR;
1,604,018✔
2079
  }
2080
}
2081

2082
/* Priorities for each binary operator. ORDER OPR. */
2083
static const struct {
2084
  uint8_t left;                /* Left priority. */
2085
  uint8_t right;        /* Right priority. */
2086
} priority[] = {
2087
  {6,6}, {6,6}, {7,7}, {7,7}, {7,7},        /* ADD SUB MUL DIV MOD */
2088
  {10,9}, {5,4},                        /* POW CONCAT (right associative) */
2089
  {3,3}, {3,3},                                /* EQ NE */
2090
  {3,3}, {3,3}, {3,3}, {3,3},                /* LT GE GT LE */
2091
  {2,2}, {1,1}                                /* AND OR */
2092
};
2093

2094
#define UNARY_PRIORITY                8  /* Priority for unary operators. */
2095

2096
/* Forward declaration. */
2097
static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit);
2098

2099
/* Parse unary expression. */
2100
static void expr_unop(LexState *ls, ExpDesc *v)
2,206,711✔
2101
{
2102
  BCOp op;
2,206,711✔
2103
  if (ls->tok == TK_not) {
2,206,711✔
2104
    op = BC_NOT;
2105
  } else if (ls->tok == '-') {
1,910,434✔
2106
    op = BC_UNM;
2107
  } else if (ls->tok == '#') {
1,908,127✔
2108
    op = BC_LEN;
2109
  } else {
2110
    expr_simple(ls, v);
1,907,025✔
2111
    return;
1,907,025✔
2112
  }
2113
  lj_lex_next(ls);
299,686✔
2114
  expr_binop(ls, v, UNARY_PRIORITY);
299,686✔
2115
  bcemit_unop(ls->fs, op, v);
299,686✔
2116
}
2117

2118
/* Parse binary expressions with priority higher than the limit. */
2119
static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit)
2,206,717✔
2120
{
2121
  BinOpr op;
2,206,717✔
2122
  synlevel_begin(ls);
2,206,717✔
2123
  expr_unop(ls, v);
2,206,711✔
2124
  op = token2binop(ls->tok);
2,198,180✔
2125
  while (op != OPR_NOBINOPR && priority[op].left > limit) {
2,725,635✔
2126
    ExpDesc v2;
527,965✔
2127
    BinOpr nextop;
527,965✔
2128
    lj_lex_next(ls);
527,965✔
2129
    bcemit_binop_left(ls->fs, op, v);
527,919✔
2130
    /* Parse binary expression with higher priority. */
2131
    nextop = expr_binop(ls, &v2, priority[op].right);
527,919✔
2132
    bcemit_binop(ls->fs, op, v, &v2);
527,455✔
2133
    op = nextop;
527,455✔
2134
  }
2135
  synlevel_end(ls);
2,197,670✔
2136
  return op;  /* Return unconsumed binary operator (if any). */
2,197,670✔
2137
}
2138

2139
/* Parse expression. */
2140
static void expr(LexState *ls, ExpDesc *v)
1,379,112✔
2141
{
2142
  expr_binop(ls, v, 0);  /* Priority 0: parse whole expression. */
1,271,504✔
2143
}
644,220✔
2144

2145
/* Assign expression to the next register. */
2146
static void expr_next(LexState *ls)
3,422✔
2147
{
2148
  ExpDesc e;
3,422✔
2149
  expr(ls, &e);
3,422✔
2150
  expr_tonextreg(ls->fs, &e);
3,422✔
2151
}
3,422✔
2152

2153
/* Parse conditional expression. */
2154
static BCPos expr_cond(LexState *ls)
104,186✔
2155
{
2156
  ExpDesc v;
104,186✔
2157
  expr(ls, &v);
104,186✔
2158
  if (v.k == VKNIL) v.k = VKFALSE;
104,180✔
2159
  bcemit_branch_t(ls->fs, &v);
104,180✔
2160
  return v.f;
104,180✔
2161
}
2162

2163
/* -- Assignments --------------------------------------------------------- */
2164

2165
/* List of LHS variables. */
2166
typedef struct LHSVarList {
2167
  ExpDesc v;                        /* LHS variable. */
2168
  struct LHSVarList *prev;        /* Link to previous LHS variable. */
2169
} LHSVarList;
2170

2171
/* Eliminate write-after-read hazards for local variable assignment. */
2172
static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v)
2173
{
2174
  FuncState *fs = ls->fs;
2175
  BCReg reg = v->u.s.info;  /* Check against this variable. */
2176
  BCReg tmp = fs->freereg;  /* Rename to this temp. register (if needed). */
2177
  int hazard = 0;
2178
  for (; lh; lh = lh->prev) {
2179
    if (lh->v.k == VINDEXED) {
2180
      if (lh->v.u.s.info == reg) {  /* t[i], t = 1, 2 */
2181
        hazard = 1;
2182
        lh->v.u.s.info = tmp;
2183
      }
2184
      if (lh->v.u.s.aux == reg) {  /* t[i], i = 1, 2 */
2185
        hazard = 1;
2186
        lh->v.u.s.aux = tmp;
2187
      }
2188
    }
2189
  }
2190
  if (hazard) {
2191
    bcemit_AD(fs, BC_MOV, tmp, reg);  /* Rename conflicting variable. */
2192
    bcreg_reserve(fs, 1);
2193
  }
2194
}
2195

2196
/* Adjust LHS/RHS of an assignment. */
2197
static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e)
2198
{
2199
  FuncState *fs = ls->fs;
2200
  int32_t extra = (int32_t)nvars - (int32_t)nexps;
2201
  if (e->k == VCALL) {
2202
    extra++;  /* Compensate for the VCALL itself. */
2203
    if (extra < 0) extra = 0;
2204
    setbc_b(bcptr(fs, e), extra+1);  /* Fixup call results. */
2205
    if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1);
2206
  } else {
2207
    if (e->k != VVOID)
2208
      expr_tonextreg(fs, e);  /* Close last expression. */
2209
    if (extra > 0) {  /* Leftover LHS are set to nil. */
2210
      BCReg reg = fs->freereg;
2211
      bcreg_reserve(fs, (BCReg)extra);
2212
      bcemit_nil(fs, reg, (BCReg)extra);
2213
    }
2214
  }
2215
  if (nexps > nvars)
2216
    ls->fs->freereg -= nexps - nvars;  /* Drop leftover regs. */
2217
}
2218

2219
/* Recursively parse assignment statement. */
2220
static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars)
399,492✔
2221
{
2222
  ExpDesc e;
399,492✔
2223
  checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX);
399,492✔
2224
  if (lex_opt(ls, ',')) {  /* Collect LHS list and recurse upwards. */
399,226✔
2225
    LHSVarList vl;
651✔
2226
    vl.prev = lh;
651✔
2227
    expr_primary(ls, &vl.v);
651✔
2228
    if (vl.v.k == VLOCAL)
650✔
2229
      assign_hazard(ls, lh, &vl.v);
434✔
2230
    checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names");
650✔
2231
    parse_assignment(ls, &vl, nvars+1);
650✔
2232
  } else {  /* Parse RHS. */
2233
    BCReg nexps;
398,575✔
2234
    lex_check(ls, '=');
398,575✔
2235
    nexps = expr_list(ls, &e);
395,030✔
2236
    if (nexps == nvars) {
393,585✔
2237
      if (e.k == VCALL) {
393,367✔
2238
        if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) {  /* Vararg assignment. */
4,637✔
2239
          ls->fs->freereg--;
12✔
2240
          e.k = VRELOCABLE;
12✔
2241
        } else {  /* Multiple call results. */
2242
          e.u.s.info = e.u.s.aux;  /* Base of call is not relocatable. */
4,625✔
2243
          e.k = VNONRELOC;
4,625✔
2244
        }
2245
      }
2246
      bcemit_store(ls->fs, &lh->v, &e);
393,367✔
2247
      return;
393,367✔
2248
    }
2249
    assign_adjust(ls, nvars, nexps, &e);
218✔
2250
  }
2251
  /* Assign RHS to LHS and recurse downwards. */
2252
  expr_init(&e, VNONRELOC, ls->fs->freereg-1);
743✔
2253
  bcemit_store(ls->fs, &lh->v, &e);
743✔
2254
}
2255

2256
/* Parse call statement or assignment. */
2257
static void parse_call_assign(LexState *ls)
435,409✔
2258
{
2259
  FuncState *fs = ls->fs;
435,409✔
2260
  LHSVarList vl;
435,409✔
2261
  expr_primary(ls, &vl.v);
435,409✔
2262
  if (vl.v.k == VCALL) {  /* Function call statement. */
431,424✔
2263
    setbc_b(bcptr(fs, &vl.v), 1);  /* No results. */
32,582✔
2264
  } else {  /* Start of an assignment. */
2265
    vl.prev = NULL;
398,842✔
2266
    parse_assignment(ls, &vl, 1);
398,842✔
2267
  }
2268
}
426,167✔
2269

2270
/* Parse 'local' statement. */
2271
static void parse_local(LexState *ls)
61,497✔
2272
{
2273
  if (lex_opt(ls, TK_function)) {  /* Local function declaration. */
61,497✔
2274
    ExpDesc v, b;
3,480✔
2275
    FuncState *fs = ls->fs;
3,480✔
2276
    var_new(ls, 0, lex_str(ls));
3,480✔
2277
    expr_init(&v, VLOCAL, fs->freereg);
3,480✔
2278
    v.u.s.aux = fs->varmap[fs->freereg];
3,480✔
2279
    bcreg_reserve(fs, 1);
3,480✔
2280
    var_add(ls, 1);
3,480✔
2281
    parse_body(ls, &b, 0, ls->linenumber);
3,480✔
2282
    /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */
2283
    expr_free(fs, &b);
3,480✔
2284
    expr_toreg(fs, &b, v.u.s.info);
3,480✔
2285
    /* The upvalue is in scope, but the local is only valid after the store. */
2286
    var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc;
3,480✔
2287
  } else {  /* Local variable declaration. */
2288
    ExpDesc e;
2289
    BCReg nexps, nvars = 0;
2290
    do {  /* Collect LHS. */
60,193✔
2291
      var_new(ls, nvars++, lex_str(ls));
60,193✔
2292
    } while (lex_opt(ls, ','));
60,180✔
2293
    if (lex_opt(ls, '=')) {  /* Optional RHS. */
58,004✔
2294
      nexps = expr_list(ls, &e);
40,996✔
2295
    } else {  /* Or implicitly set to nil. */
2296
      e.k = VVOID;
17,005✔
2297
      nexps = 0;
17,005✔
2298
    }
2299
    assign_adjust(ls, nvars, nexps, &e);
57,928✔
2300
    var_add(ls, nvars);
115,856✔
2301
  }
2302
}
61,408✔
2303

2304
/* Parse 'function' statement. */
2305
static void parse_func(LexState *ls, BCLine line)
8,364✔
2306
{
2307
  FuncState *fs;
8,364✔
2308
  ExpDesc v, b;
8,364✔
2309
  int needself = 0;
8,364✔
2310
  lj_lex_next(ls);  /* Skip 'function'. */
8,364✔
2311
  /* Parse function name. */
2312
  var_lookup(ls, &v);
8,364✔
2313
  while (ls->tok == '.')  /* Multiple dot-separated fields. */
8,279✔
2314
    expr_field(ls, &v);
123✔
2315
  if (ls->tok == ':') {  /* Optional colon to signify method call. */
8,156✔
2316
    needself = 1;
31✔
2317
    expr_field(ls, &v);
31✔
2318
  }
2319
  parse_body(ls, &b, needself, line);
8,156✔
2320
  fs = ls->fs;
6,624✔
2321
  bcemit_store(fs, &v, &b);
6,624✔
2322
  fs->bcbase[fs->pc - 1].line = line;  /* Set line for the store. */
6,624✔
2323
}
6,624✔
2324

2325
/* -- Control transfer statements ----------------------------------------- */
2326

2327
/* Check for end of block. */
2328
static int parse_isend(LexToken tok)
837,672✔
2329
{
2330
  switch (tok) {
837,672✔
2331
  case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof:
2332
    return 1;
2333
  default:
2334
    return 0;
720,236✔
2335
  }
2336
}
2337

2338
/* Parse 'return' statement. */
2339
static void parse_return(LexState *ls)
29,462✔
2340
{
2341
  BCIns ins;
29,462✔
2342
  FuncState *fs = ls->fs;
29,462✔
2343
  lj_lex_next(ls);  /* Skip 'return'. */
29,462✔
2344
  fs->flags |= PROTO_HAS_RETURN;
29,461✔
2345
  if (parse_isend(ls->tok) || ls->tok == ';') {  /* Bare return. */
29,461✔
2346
    ins = BCINS_AD(BC_RET0, 0, 1);
2347
  } else {  /* Return with one or more values. */
2348
    ExpDesc e;  /* Receives the _last_ expression in the list. */
29,186✔
2349
    BCReg nret = expr_list(ls, &e);
29,186✔
2350
    if (nret == 1) {  /* Return one result. */
25,017✔
2351
      if (e.k == VCALL) {  /* Check for tail call. */
24,893✔
2352
        BCIns *ip = bcptr(fs, &e);
2,904✔
2353
        /* It doesn't pay off to add BC_VARGT just for 'return ...'. */
2354
        if (bc_op(*ip) == BC_VARG) goto notailcall;
2,904✔
2355
        fs->pc--;
2,902✔
2356
        ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip));
2,902✔
2357
      } else {  /* Can return the result from any register. */
2358
        ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2);
21,989✔
2359
      }
2360
    } else {
2361
      if (e.k == VCALL) {  /* Append all results from a call. */
124✔
2362
      notailcall:
23✔
2363
        setbc_b(bcptr(fs, &e), 0);
25✔
2364
        ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar);
25✔
2365
      } else {
2366
        expr_tonextreg(fs, &e);  /* Force contiguous registers. */
101✔
2367
        ins = BCINS_AD(BC_RET, fs->nactvar, nret+1);
101✔
2368
      }
2369
    }
2370
  }
2371
  if (fs->flags & PROTO_CHILD)
25,292✔
2372
    bcemit_AJ(fs, BC_UCLO, 0, 0);  /* May need to close upvalues first. */
686✔
2373
  bcemit_INS(fs, ins);
25,292✔
2374
}
25,292✔
2375

2376
/* Parse 'break' statement. */
2377
static void parse_break(LexState *ls)
49,345✔
2378
{
2379
  ls->fs->bl->flags |= FSCOPE_BREAK;
49,345✔
2380
  gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs));
49,345✔
2381
}
49,345✔
2382

2383
/* Parse 'goto' statement. */
2384
static void parse_goto(LexState *ls)
25✔
2385
{
2386
  FuncState *fs = ls->fs;
25✔
2387
  GCstr *name = lex_str(ls);
25✔
2388
  VarInfo *vl = gola_findlabel(ls, name);
25✔
2389
  if (vl)  /* Treat backwards goto within same scope like a loop. */
25✔
2390
    bcemit_AJ(fs, BC_LOOP, vl->slot, -1);  /* No BC range check. */
3✔
2391
  fs->bl->flags |= FSCOPE_GOLA;
25✔
2392
  gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs));
25✔
2393
}
25✔
2394

2395
/* Parse label. */
2396
static void parse_label(LexState *ls)
34✔
2397
{
2398
  FuncState *fs = ls->fs;
34✔
2399
  GCstr *name;
34✔
2400
  MSize idx;
34✔
2401
  fs->lasttarget = fs->pc;
34✔
2402
  fs->bl->flags |= FSCOPE_GOLA;
34✔
2403
  lj_lex_next(ls);  /* Skip '::'. */
34✔
2404
  name = lex_str(ls);
34✔
2405
  if (gola_findlabel(ls, name))
68✔
2406
    lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name));
3✔
2407
  idx = gola_new(ls, name, VSTACK_LABEL, fs->pc);
31✔
2408
  lex_check(ls, TK_label);
31✔
2409
  /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */
2410
  for (;;) {
35✔
2411
    if (ls->tok == TK_label) {
33✔
2412
      synlevel_begin(ls);
3✔
2413
      parse_label(ls);
3✔
2414
      synlevel_end(ls);
2✔
2415
    } else if (LJ_52 && ls->tok == ';') {
30✔
2416
      lj_lex_next(ls);
2417
    } else {
2418
      break;
2419
    }
2420
  }
2421
  /* Trailing label is considered to be outside of scope. */
2422
  if (parse_isend(ls->tok) && ls->tok != TK_until)
30✔
2423
    ls->vstack[idx].slot = fs->bl->nactvar;
11✔
2424
  gola_resolve(ls, fs->bl, idx);
30✔
2425
}
26✔
2426

2427
/* -- Blocks, loops and conditional statements ---------------------------- */
2428

2429
/* Parse a block. */
2430
static void parse_block(LexState *ls)
125,062✔
2431
{
2432
  FuncState *fs = ls->fs;
125,062✔
2433
  FuncScope bl;
125,062✔
2434
  fscope_begin(fs, &bl, 0);
125,062✔
2435
  parse_chunk(ls);
125,062✔
2436
  fscope_end(fs);
124,320✔
2437
}
124,320✔
2438

2439
/* Parse 'while' statement. */
2440
static void parse_while(LexState *ls, BCLine line)
33,206✔
2441
{
2442
  FuncState *fs = ls->fs;
33,206✔
2443
  BCPos start, loop, condexit;
33,206✔
2444
  FuncScope bl;
33,206✔
2445
  lj_lex_next(ls);  /* Skip 'while'. */
33,206✔
2446
  start = fs->lasttarget = fs->pc;
33,206✔
2447
  condexit = expr_cond(ls);
33,206✔
2448
  fscope_begin(fs, &bl, FSCOPE_LOOP);
33,205✔
2449
  lex_check(ls, TK_do);
33,205✔
2450
  loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
33,204✔
2451
  parse_block(ls);
33,204✔
2452
  jmp_patch(fs, bcemit_jmp(fs), start);
33,005✔
2453
  lex_match(ls, TK_end, TK_while, line);
33,005✔
2454
  fscope_end(fs);
33,003✔
2455
  jmp_tohere(fs, condexit);
33,003✔
2456
  jmp_patchins(fs, loop, fs->pc);
33,003✔
2457
}
33,003✔
2458

2459
/* Parse 'repeat' statement. */
2460
static void parse_repeat(LexState *ls, BCLine line)
16,420✔
2461
{
2462
  FuncState *fs = ls->fs;
16,420✔
2463
  BCPos loop = fs->lasttarget = fs->pc;
16,420✔
2464
  BCPos condexit;
16,420✔
2465
  FuncScope bl1, bl2;
16,420✔
2466
  fscope_begin(fs, &bl1, FSCOPE_LOOP);  /* Breakable loop scope. */
16,420✔
2467
  fscope_begin(fs, &bl2, 0);  /* Inner scope. */
16,420✔
2468
  lj_lex_next(ls);  /* Skip 'repeat'. */
16,420✔
2469
  bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
16,420✔
2470
  parse_chunk(ls);
16,420✔
2471
  lex_match(ls, TK_until, TK_repeat, line);
16,419✔
2472
  condexit = expr_cond(ls);  /* Parse condition (still inside inner scope). */
16,418✔
2473
  if (!(bl2.flags & FSCOPE_UPVAL)) {  /* No upvalues? Just end inner scope. */
16,418✔
2474
    fscope_end(fs);
16,414✔
2475
  } else {  /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */
2476
    parse_break(ls);  /* Break from loop and close upvalues. */
4✔
2477
    jmp_tohere(fs, condexit);
4✔
2478
    fscope_end(fs);  /* End inner scope and close upvalues. */
4✔
2479
    condexit = bcemit_jmp(fs);
4✔
2480
  }
2481
  jmp_patch(fs, condexit, loop);  /* Jump backwards if !cond. */
16,418✔
2482
  jmp_patchins(fs, loop, fs->pc);
16,418✔
2483
  fscope_end(fs);  /* End loop scope. */
16,418✔
2484
}
16,418✔
2485

2486
/* Parse numeric 'for'. */
2487
static void parse_for_num(LexState *ls, GCstr *varname, BCLine line)
1,672✔
2488
{
2489
  FuncState *fs = ls->fs;
1,672✔
2490
  BCReg base = fs->freereg;
1,672✔
2491
  FuncScope bl;
1,672✔
2492
  BCPos loop, loopend;
1,672✔
2493
  /* Hidden control variables. */
2494
  var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);
1,672✔
2495
  var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);
1,672✔
2496
  var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);
1,672✔
2497
  /* Visible copy of index variable. */
2498
  var_new(ls, FORL_EXT, varname);
1,672✔
2499
  lex_check(ls, '=');
1,672✔
2500
  expr_next(ls);
1,672✔
2501
  lex_check(ls, ',');
1,672✔
2502
  expr_next(ls);
1,672✔
2503
  if (lex_opt(ls, ',')) {
1,672✔
2504
    expr_next(ls);
78✔
2505
  } else {
2506
    bcemit_AD(fs, BC_KSHORT, fs->freereg, 1);  /* Default step is 1. */
1,594✔
2507
    bcreg_reserve(fs, 1);
1,594✔
2508
  }
2509
  var_add(ls, 3);  /* Hidden control variables. */
1,672✔
2510
  lex_check(ls, TK_do);
1,672✔
2511
  loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP);
1,671✔
2512
  fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
1,671✔
2513
  var_add(ls, 1);
1,671✔
2514
  bcreg_reserve(fs, 1);
1,671✔
2515
  parse_block(ls);
1,671✔
2516
  fscope_end(fs);
1,671✔
2517
  /* Perform loop inversion. Loop control instructions are at the end. */
2518
  loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP);
1,671✔
2519
  fs->bcbase[loopend].line = line;  /* Fix line for control ins. */
1,671✔
2520
  jmp_patchins(fs, loopend, loop+1);
1,671✔
2521
  jmp_patchins(fs, loop, fs->pc);
1,671✔
2522
}
1,671✔
2523

2524
/* Try to predict whether the iterator is next() and specialize the bytecode.
2525
** Detecting next() and pairs() by name is simplistic, but quite effective.
2526
** The interpreter backs off if the check for the closure fails at runtime.
2527
*/
2528
static int predict_next(LexState *ls, FuncState *fs, BCPos pc)
2529
{
2530
  BCIns ins = fs->bcbase[pc].ins;
2531
  GCstr *name;
2532
  cTValue *o;
2533
  switch (bc_op(ins)) {
2534
  case BC_MOV:
2535
    name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name));
2536
    break;
2537
  case BC_UGET:
2538
    name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name));
2539
    break;
2540
  case BC_GGET:
2541
    /* There's no inverse index (yet), so lookup the strings. */
2542
    o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs"));
2543
    if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
2544
      return 1;
2545
    o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next"));
2546
    if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
2547
      return 1;
2548
    return 0;
2549
  default:
2550
    return 0;
2551
  }
2552
  return (name->len == 5 && !strcmp(strdata(name), "pairs")) ||
2553
         (name->len == 4 && !strcmp(strdata(name), "next"));
2554
}
2555

2556
/* Parse 'for' iterator. */
2557
static void parse_for_iter(LexState *ls, GCstr *indexname)
706✔
2558
{
2559
  FuncState *fs = ls->fs;
706✔
2560
  ExpDesc e;
706✔
2561
  BCReg nvars = 0;
706✔
2562
  BCLine line;
706✔
2563
  BCReg base = fs->freereg + 3;
706✔
2564
  BCPos loop, loopend, exprpc = fs->pc;
706✔
2565
  FuncScope bl;
706✔
2566
  int isnext;
706✔
2567
  /* Hidden control variables. */
2568
  var_new_fixed(ls, nvars++, VARNAME_FOR_GEN);
706✔
2569
  var_new_fixed(ls, nvars++, VARNAME_FOR_STATE);
706✔
2570
  var_new_fixed(ls, nvars++, VARNAME_FOR_CTL);
706✔
2571
  /* Visible variables returned from iterator. */
2572
  var_new(ls, nvars++, indexname);
706✔
2573
  while (lex_opt(ls, ','))
1,359✔
2574
    var_new(ls, nvars++, lex_str(ls));
653✔
2575
  lex_check(ls, TK_in);
706✔
2576
  line = ls->linenumber;
706✔
2577
  assign_adjust(ls, 3, expr_list(ls, &e), &e);
706✔
2578
  /* The iterator needs another 3 [4] slots (func [pc] | state ctl). */
2579
  bcreg_bump(fs, 3+LJ_FR2);
706✔
2580
  isnext = (nvars <= 5 && predict_next(ls, fs, exprpc));
706✔
2581
  var_add(ls, 3);  /* Hidden control variables. */
706✔
2582
  lex_check(ls, TK_do);
706✔
2583
  loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP);
903✔
2584
  fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
706✔
2585
  var_add(ls, nvars-3);
706✔
2586
  bcreg_reserve(fs, nvars-3);
706✔
2587
  parse_block(ls);
706✔
2588
  fscope_end(fs);
706✔
2589
  /* Perform loop inversion. Loop control instructions are at the end. */
2590
  jmp_patchins(fs, loop, fs->pc);
706✔
2591
  bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1);
706✔
2592
  loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP);
706✔
2593
  fs->bcbase[loopend-1].line = line;  /* Fix line for control ins. */
706✔
2594
  fs->bcbase[loopend].line = line;
706✔
2595
  jmp_patchins(fs, loopend, loop+1);
706✔
2596
}
706✔
2597

2598
/* Parse 'for' statement. */
2599
static void parse_for(LexState *ls, BCLine line)
2,379✔
2600
{
2601
  FuncState *fs = ls->fs;
2,379✔
2602
  GCstr *varname;
2,379✔
2603
  FuncScope bl;
2,379✔
2604
  fscope_begin(fs, &bl, FSCOPE_LOOP);
2,379✔
2605
  lj_lex_next(ls);  /* Skip 'for'. */
2,379✔
2606
  varname = lex_str(ls);  /* Get first variable name. */
2,379✔
2607
  if (ls->tok == '=')
2,379✔
2608
    parse_for_num(ls, varname, line);
1,672✔
2609
  else if (ls->tok == ',' || ls->tok == TK_in)
707✔
2610
    parse_for_iter(ls, varname);
706✔
2611
  else
2612
    err_syntax(ls, LJ_ERR_XFOR);
1✔
2613
  lex_match(ls, TK_end, TK_for, line);
2,377✔
2614
  fscope_end(fs);  /* Resolve break list. */
2,376✔
2615
}
2,376✔
2616

2617
/* Parse condition and 'then' block. */
2618
static BCPos parse_then(LexState *ls)
54,562✔
2619
{
2620
  BCPos condexit;
54,562✔
2621
  lj_lex_next(ls);  /* Skip 'if' or 'elseif'. */
54,562✔
2622
  condexit = expr_cond(ls);
54,562✔
2623
  lex_check(ls, TK_then);
54,557✔
2624
  parse_block(ls);
54,552✔
2625
  return condexit;
54,550✔
2626
}
2627

2628
/* Parse 'if' statement. */
2629
static void parse_if(LexState *ls, BCLine line)
54,169✔
2630
{
2631
  FuncState *fs = ls->fs;
54,169✔
2632
  BCPos flist;
54,169✔
2633
  BCPos escapelist = NO_JMP;
54,169✔
2634
  flist = parse_then(ls);
54,169✔
2635
  while (ls->tok == TK_elseif) {  /* Parse multiple 'elseif' blocks. */
54,562✔
2636
    jmp_append(fs, &escapelist, bcemit_jmp(fs));
393✔
2637
    jmp_tohere(fs, flist);
393✔
2638
    flist = parse_then(ls);
393✔
2639
  }
2640
  if (ls->tok == TK_else) {  /* Parse optional 'else' block. */
54,157✔
2641
    jmp_append(fs, &escapelist, bcemit_jmp(fs));
34,185✔
2642
    jmp_tohere(fs, flist);
34,185✔
2643
    lj_lex_next(ls);  /* Skip 'else'. */
34,185✔
2644
    parse_block(ls);
34,185✔
2645
  } else {
2646
    jmp_append(fs, &escapelist, flist);
19,972✔
2647
  }
2648
  jmp_tohere(fs, escapelist);
53,959✔
2649
  lex_match(ls, TK_end, TK_if, line);
53,959✔
2650
}
53,941✔
2651

2652
/* -- Parse statements ---------------------------------------------------- */
2653

2654
/* Parse a statement. Returns 1 if it must be the last one in a chunk. */
2655
static int parse_stmt(LexState *ls)
691,047✔
2656
{
2657
  BCLine line = ls->linenumber;
691,047✔
2658
  switch (ls->tok) {
691,047✔
2659
  case TK_if:
54,169✔
2660
    parse_if(ls, line);
54,169✔
2661
    break;
54,169✔
2662
  case TK_while:
33,206✔
2663
    parse_while(ls, line);
33,206✔
2664
    break;
33,206✔
2665
  case TK_do:
744✔
2666
    lj_lex_next(ls);
744✔
2667
    parse_block(ls);
744✔
2668
    lex_match(ls, TK_end, TK_do, line);
401✔
2669
    break;
401✔
2670
  case TK_for:
2,379✔
2671
    parse_for(ls, line);
2,379✔
2672
    break;
2,379✔
2673
  case TK_repeat:
16,420✔
2674
    parse_repeat(ls, line);
16,420✔
2675
    break;
16,420✔
2676
  case TK_function:
8,364✔
2677
    parse_func(ls, line);
8,364✔
2678
    break;
8,364✔
2679
  case TK_local:
61,497✔
2680
    lj_lex_next(ls);
61,497✔
2681
    parse_local(ls);
61,497✔
2682
    break;
61,497✔
2683
  case TK_return:
29,462✔
2684
    parse_return(ls);
29,462✔
2685
    return 1;  /* Must be last. */
29,462✔
2686
  case TK_break:
49,341✔
2687
    lj_lex_next(ls);
49,341✔
2688
    parse_break(ls);
49,341✔
2689
    return !LJ_52;  /* Must be last in Lua 5.1. */
49,341✔
2690
#if LJ_52
2691
  case ';':
2692
    lj_lex_next(ls);
2693
    break;
2694
#endif
2695
  case TK_label:
31✔
2696
    parse_label(ls);
31✔
2697
    break;
31✔
2698
  case TK_goto:
26✔
2699
    if (LJ_52 || lj_lex_lookahead(ls) == TK_name) {
26✔
2700
      lj_lex_next(ls);
25✔
2701
      parse_goto(ls);
25✔
2702
      break;
25✔
2703
    }
2704
    /* fallthrough */
2705
  default:
2706
    parse_call_assign(ls);
435,409✔
2707
    break;
435,409✔
2708
  }
2709
  return 0;
2710
}
2711

2712
/* A chunk is a list of statements optionally separated by semicolons. */
2713
static void parse_chunk(LexState *ls)
207,807✔
2714
{
2715
  int islast = 0;
207,807✔
2716
  synlevel_begin(ls);
207,807✔
2717
  while (!islast && !parse_isend(ls->tok)) {
882,814✔
2718
    islast = parse_stmt(ls);
691,047✔
2719
    lex_opt(ls, ';');
675,009✔
2720
    lj_assertLS(ls->fs->framesize >= ls->fs->freereg &&
675,009✔
2721
                ls->fs->freereg >= ls->fs->nactvar,
2722
                "bad regalloc");
2723
    ls->fs->freereg = ls->fs->nactvar;  /* Free registers after each stmt. */
675,009✔
2724
  }
2725
  synlevel_end(ls);
191,767✔
2726
}
191,767✔
2727

2728
/* Entry point of bytecode parser. */
2729
GCproto *lj_parse(LexState *ls)
51,142✔
2730
{
2731
  FuncState fs;
51,142✔
2732
  FuncScope bl;
51,142✔
2733
  GCproto *pt;
51,142✔
2734
  lua_State *L = ls->L;
51,142✔
2735
#ifdef LUAJIT_DISABLE_DEBUGINFO
2736
  ls->chunkname = lj_str_newlit(L, "=");
2737
#else
2738
  ls->chunkname = lj_str_newz(L, ls->chunkarg);
51,142✔
2739
#endif
2740
  setstrV(L, L->top, ls->chunkname);  /* Anchor chunkname string. */
51,142✔
2741
  incr_top(L);
51,142✔
2742
  ls->level = 0;
51,142✔
2743
  fs_init(ls, &fs);
51,142✔
2744
  fs.linedefined = 0;
51,142✔
2745
  fs.numparams = 0;
51,142✔
2746
  fs.bcbase = NULL;
51,142✔
2747
  fs.bclim = 0;
51,142✔
2748
  fs.flags |= PROTO_VARARG;  /* Main chunk is always a vararg func. */
51,142✔
2749
  fscope_begin(&fs, &bl, 0);
51,142✔
2750
  bcemit_AD(&fs, BC_FUNCV, 0, 0);  /* Placeholder. */
51,142✔
2751
  lj_lex_next(ls);  /* Read-ahead first token. */
51,142✔
2752
  parse_chunk(ls);
51,133✔
2753
  if (ls->tok != TK_eof)
38,797✔
2754
    err_token(ls, TK_eof);
348✔
2755
  pt = fs_finish(ls, ls->linenumber);
38,449✔
2756
  L->top--;  /* Drop chunkname. */
38,440✔
2757
  lj_assertL(fs.prev == NULL && ls->fs == NULL, "mismatched frame nesting");
38,440✔
2758
  lj_assertL(pt->sizeuv == 0, "toplevel proto has upvalues");
38,440✔
2759
  return pt;
38,440✔
2760
}
2761

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc