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

tarantool / luajit / 17762597284

16 Sep 2025 10:17AM UTC coverage: 93.021% (-0.06%) from 93.077%
17762597284

push

github

mandesero
test: disable long-running tests under ASAN

5709 of 6046 branches covered (94.43%)

Branch coverage included in aggregate %.

21788 of 23514 relevant lines covered (92.66%)

3871416.3 hits per line

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

99.61
/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)
3,082,002✔
90
{
91
  e->k = k;
3,082,002✔
92
  e->u.s.info = info;
3,082,002✔
93
  e->f = e->t = NO_JMP;
3,082,002✔
94
}
148,820✔
95

96
/* Check number constant for +-0. */
97
static int expr_numiszero(ExpDesc *e)
3,009✔
98
{
99
  TValue *o = expr_numtv(e);
3,009✔
100
  return tvisint(o) ? (intV(o) == 0) : tviszero(o);
3,009✔
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,372✔
181
{
182
  lj_lex_error(ls, ls->tok, em);
7,372✔
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)
150,120✔
212
{
213
  lua_State *L = fs->L;
150,120✔
214
  TValue *o;
150,120✔
215
  lj_assertFS(expr_isnumk(e), "bad usage");
150,120✔
216
  o = lj_tab_set(L, fs->kt, &e->u.nval);
150,120✔
217
  if (tvhaskslot(o))
150,120✔
218
    return tvkslot(o);
3,453✔
219
  o->u64 = fs->nkn;
146,667✔
220
  return fs->nkn++;
146,667✔
221
}
222

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

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

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

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

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

269
/* Get next element in jump list. */
270
static BCPos jmp_next(FuncState *fs, BCPos pc)
753,277✔
271
{
272
  ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);
753,277✔
273
  if ((BCPos)delta == NO_JMP)
753,277✔
274
    return NO_JMP;
275
  else
276
    return (BCPos)(((ptrdiff_t)pc+1)+delta);
76,917✔
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)
594,574✔
318
{
319
  for (; list != NO_JMP; list = jmp_next(fs, list))
630,652✔
320
    jmp_patchtestreg(fs, list, NO_REG);
36,078✔
321
}
594,574✔
322

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

334
/* Append to jump list. */
335
static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)
2,125,491✔
336
{
337
  if (l2 == NO_JMP) {
2,125,491✔
338
    return;
339
  } else if (*l1 == NO_JMP) {
928,552✔
340
    *l1 = l2;
851,633✔
341
  } else {
342
    BCPos list = *l1;
343
    BCPos next;
344
    while ((next = jmp_next(fs, list)) != NO_JMP)  /* Find last element. */
81,371✔
345
      list = next;
346
    jmp_patchins(fs, list, l2);
76,919✔
347
  }
348
}
349

350
/* Patch jump list and preserve produced values. */
351
static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,
4,863,110✔
352
                         BCReg reg, BCPos dtarget)
353
{
354
  while (list != NO_JMP) {
4,863,110✔
355
    BCPos next = jmp_next(fs, list);
635,828✔
356
    if (jmp_patchtestreg(fs, list, reg))
635,828✔
357
      jmp_patchins(fs, list, vtarget);  /* Jump to target with value. */
76,023✔
358
    else
359
      jmp_patchins(fs, list, dtarget);  /* Jump to default target. */
6,058,743✔
360
    list = next;
361
  }
362
}
4,863,110✔
363

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

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

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

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

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

402
/* Free register. */
403
static void bcreg_free(FuncState *fs, BCReg reg)
664,264✔
404
{
405
  if (reg >= fs->nactvar) {
664,264✔
406
    fs->freereg--;
353,729✔
407
    lj_assertFS(reg == fs->freereg, "bad regfree");
5,796✔
408
  }
409
}
410

411
/* Free register for expression. */
412
static void expr_free(FuncState *fs, ExpDesc *e)
2,153,830✔
413
{
414
  if (e->k == VNONRELOC)
2,153,830✔
415
    bcreg_free(fs, e->u.s.info);
544,609✔
416
}
417

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

420
/* Emit bytecode instruction. */
421
static BCPos bcemit_INS(FuncState *fs, BCIns ins)
4,224,615✔
422
{
423
  BCPos pc = fs->pc;
4,224,615✔
424
  LexState *ls = fs->ls;
4,224,615✔
425
  jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);
4,224,615✔
426
  fs->jpc = NO_JMP;
4,224,615✔
427
  if (LJ_UNLIKELY(pc >= fs->bclim)) {
4,224,615✔
428
    ptrdiff_t base = fs->bcbase - ls->bcstack;
167,607✔
429
    checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");
167,607✔
430
    lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);
167,607✔
431
    fs->bclim = (BCPos)(ls->sizebcstack - base);
167,607✔
432
    fs->bcbase = ls->bcstack + base;
167,607✔
433
  }
434
  fs->bcbase[pc].ins = ins;
4,224,615✔
435
  fs->bcbase[pc].line = ls->lastline;
4,224,615✔
436
  fs->pc = pc+1;
4,224,615✔
437
  return pc;
4,224,615✔
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)
6,640,451✔
450
{
451
  BCIns ins;
6,640,451✔
452
  if (e->k == VUPVAL) {
6,640,451✔
453
    ins = BCINS_AD(BC_UGET, 0, e->u.s.info);
26,866✔
454
  } else if (e->k == VGLOBAL) {
6,613,585✔
455
    ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));
292,889✔
456
  } else if (e->k == VINDEXED) {
6,320,696✔
457
    BCReg rc = e->u.s.aux;
113,859✔
458
    if ((int32_t)rc < 0) {
113,859✔
459
      ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);
105,884✔
460
    } else if (rc > BCMAX_C) {
7,975✔
461
      ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));
2,179✔
462
    } else {
463
      bcreg_free(fs, rc);
5,796✔
464
      ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);
5,796✔
465
    }
466
    bcreg_free(fs, e->u.s.info);
113,859✔
467
  } else if (e->k == VCALL) {
6,206,837✔
468
    e->u.s.info = e->u.s.aux;
247,568✔
469
    e->k = VNONRELOC;
247,568✔
470
    return;
247,568✔
471
  } else if (e->k == VLOCAL) {
5,959,269✔
472
    e->k = VNONRELOC;
492,771✔
473
    return;
492,771✔
474
  } else {
475
    return;
476
  }
477
  e->u.s.info = bcemit_INS(fs, ins);
433,614✔
478
  e->k = VRELOCABLE;
433,614✔
479
}
480

481
/* Emit bytecode to set a range of registers to nil. */
482
static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)
47,156✔
483
{
484
  if (fs->pc > fs->lasttarget) {  /* No jumps to current position? */
47,156✔
485
    BCIns *ip = &fs->bcbase[fs->pc-1].ins;
31,190✔
486
    BCReg pto, pfrom = bc_a(*ip);
31,190✔
487
    switch (bc_op(*ip)) {  /* Try to merge with the previous instruction. */
31,190✔
488
    case BC_KPRI:
762✔
489
      if (bc_d(*ip) != ~LJ_TNIL) break;
762✔
490
      if (from == pfrom) {
756✔
491
        if (n == 1) return;
×
492
      } else if (from == pfrom+1) {
756✔
493
        from = pfrom;
114✔
494
        n++;
114✔
495
      } else {
496
        break;
497
      }
498
      *ip = BCINS_AD(BC_KNIL, from, from+n-1);  /* Replace KPRI. */
114✔
499
      return;
114✔
500
    case BC_KNIL:
552✔
501
      pto = bc_d(*ip);
552✔
502
      if (pfrom <= from && from <= pto+1) {  /* Can we connect both ranges? */
552✔
503
        if (from+n-1 > pto)
541✔
504
          setbc_d(ip, from+n-1);  /* Patch previous instruction range. */
539✔
505
        return;
541✔
506
      }
507
      break;
508
    default:
509
      break;
510
    }
511
  }
16,614✔
512
  /* Emit new instruction or replace old instruction. */
513
  bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :
46,639✔
514
                          BCINS_AD(BC_KNIL, from, from+n-1));
138✔
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,750,441✔
519
{
520
  BCIns ins;
1,750,441✔
521
  expr_discharge(fs, e);
1,750,441✔
522
  if (e->k == VKSTR) {
1,750,441✔
523
    ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));
165,262✔
524
  } else if (e->k == VKNUM) {
1,585,179✔
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);
561,095✔
532
    int32_t k = lj_num2int(n);
561,095✔
533
    if (checki16(k) && n == (lua_Number)k)
561,095✔
534
      ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);
428,727✔
535
    else
536
#endif
537
      ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));
132,368✔
538
#if LJ_HASFFI
539
  } else if (e->k == VKCDATA) {
1,024,084✔
540
    fs->flags |= PROTO_FFI;
380✔
541
    ins = BCINS_AD(BC_KCDATA, reg,
380✔
542
                   const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));
543
#endif
544
  } else if (e->k == VRELOCABLE) {
1,023,704✔
545
    setbc_a(bcptr(fs, e), reg);
628,449✔
546
    goto noins;
628,449✔
547
  } else if (e->k == VNONRELOC) {
395,255✔
548
    if (reg == e->u.s.info)
101,329✔
549
      goto noins;
30,806✔
550
    ins = BCINS_AD(BC_MOV, reg, e->u.s.info);
70,523✔
551
  } else if (e->k == VKNIL) {
293,926✔
552
    bcemit_nil(fs, reg, 1);
29,372✔
553
    goto noins;
29,372✔
554
  } else if (e->k <= VKTRUE) {
264,554✔
555
    ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
37,516✔
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);
834,776✔
561
noins:
1,523,403✔
562
  e->u.s.info = reg;
1,523,403✔
563
  e->k = VNONRELOC;
1,523,403✔
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,710,032✔
571
{
572
  expr_toreg_nobranch(fs, e, reg);
1,710,032✔
573
  if (e->k == VJMP)
1,710,032✔
574
    jmp_append(fs, &e->t, e->u.s.info);  /* Add it to the true jump list. */
227,038✔
575
  if (expr_hasjump(e)) {  /* Discharge expression with branches. */
1,710,032✔
576
    BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;
294,394✔
577
    if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {
294,394✔
578
      BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);
263,350✔
579
      jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);
263,350✔
580
      bcemit_AJ(fs, BC_JMP, fs->freereg, 1);
263,350✔
581
      jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);
263,350✔
582
      jmp_tohere(fs, jval);
263,350✔
583
    }
584
    jend = fs->pc;
294,394✔
585
    fs->lasttarget = jend;
294,394✔
586
    jmp_patchval(fs, e->f, jend, reg, jfalse);
294,394✔
587
    jmp_patchval(fs, e->t, jend, reg, jtrue);
294,394✔
588
  }
589
  e->f = e->t = NO_JMP;
1,710,032✔
590
  e->u.s.info = reg;
1,710,032✔
591
  e->k = VNONRELOC;
1,710,032✔
592
}
1,710,032✔
593

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

603
/* Discharge an expression to any register. */
604
static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)
1,511,429✔
605
{
606
  expr_discharge(fs, e);
1,511,429✔
607
  if (e->k == VNONRELOC) {
1,511,429✔
608
    if (!expr_hasjump(e)) return e->u.s.info;  /* Already in a register. */
712,140✔
609
    if (e->u.s.info >= fs->nactvar) {
988✔
610
      expr_toreg(fs, e, e->u.s.info);  /* Discharge to temp. register. */
962✔
611
      return e->u.s.info;
962✔
612
    }
613
  }
614
  expr_tonextreg(fs, e);  /* Discharge to next register. */
799,315✔
615
  return e->u.s.info;
799,315✔
616
}
617

618
/* Partially discharge expression to a value. */
619
static void expr_toval(FuncState *fs, ExpDesc *e)
790,020✔
620
{
621
  if (expr_hasjump(e))
790,020✔
622
    expr_toanyreg(fs, e);
10,801✔
623
  else
624
    expr_discharge(fs, e);
779,219✔
625
}
790,020✔
626

627
/* Emit store for LHS expression. */
628
static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
501,340✔
629
{
630
  BCIns ins;
501,340✔
631
  if (var->k == VLOCAL) {
501,340✔
632
    fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
270,208✔
633
    expr_free(fs, e);
270,208✔
634
    expr_toreg(fs, e, var->u.s.info);
270,208✔
635
    return;
270,208✔
636
  } else if (var->k == VUPVAL) {
231,132✔
637
    fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
853✔
638
    expr_toval(fs, e);
853✔
639
    if (e->k <= VKTRUE)
853✔
640
      ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
95✔
641
    else if (e->k == VKSTR)
758✔
642
      ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));
12✔
643
    else if (e->k == VKNUM)
746✔
644
      ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));
117✔
645
    else
646
      ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));
629✔
647
  } else if (var->k == VGLOBAL) {
230,279✔
648
    BCReg ra = expr_toanyreg(fs, e);
126,359✔
649
    ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
126,359✔
650
  } else {
651
    BCReg ra, rc;
103,920✔
652
    lj_assertFS(var->k == VINDEXED, "bad expr type %d", var->k);
103,920✔
653
    ra = expr_toanyreg(fs, e);
103,920✔
654
    rc = var->u.s.aux;
103,920✔
655
    if ((int32_t)rc < 0) {
103,920✔
656
      ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);
97,251✔
657
    } else if (rc > BCMAX_C) {
6,669✔
658
      ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
2,349✔
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);
4,320✔
667
    }
668
  }
669
  bcemit_INS(fs, ins);
231,132✔
670
  expr_free(fs, e);
231,132✔
671
}
672

673
/* Emit method lookup expression. */
674
static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
10,770✔
675
{
676
  BCReg idx, func, obj = expr_toanyreg(fs, e);
10,770✔
677
  expr_free(fs, e);
10,770✔
678
  func = fs->freereg;
10,770✔
679
  bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj);  /* Copy object to 1st argument. */
10,770✔
680
  lj_assertFS(expr_isstrk(key), "bad usage");
10,770✔
681
  idx = const_str(fs, key);
10,770✔
682
  if (idx <= BCMAX_C) {
10,770✔
683
    bcreg_reserve(fs, 2+LJ_FR2);
10,764✔
684
    bcemit_ABC(fs, BC_TGETS, func, obj, idx);
10,764✔
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;
10,770✔
692
  e->k = VNONRELOC;
10,770✔
693
}
10,770✔
694

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

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

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

721
/* Emit conditional branch. */
722
static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)
96,899✔
723
{
724
  BCPos pc;
96,899✔
725
  if (e->k == VRELOCABLE) {
96,899✔
726
    BCIns *ip = bcptr(fs, e);
49,078✔
727
    if (bc_op(*ip) == BC_NOT) {
49,078✔
728
      *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));
45,432✔
729
      return bcemit_jmp(fs);
45,432✔
730
    }
731
  }
732
  if (e->k != VNONRELOC) {
51,467✔
733
    bcreg_reserve(fs, 1);
3,648✔
734
    expr_toreg_nobranch(fs, e, fs->freereg-1);
3,648✔
735
  }
736
  bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);
85,824✔
737
  pc = bcemit_jmp(fs);
51,467✔
738
  expr_free(fs, e);
51,467✔
739
  return pc;
740
}
741

742
/* Emit branch on true condition. */
743
static void bcemit_branch_t(FuncState *fs, ExpDesc *e)
199,569✔
744
{
745
  BCPos pc;
199,569✔
746
  expr_discharge(fs, e);
199,569✔
747
  if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
199,569✔
748
    pc = NO_JMP;  /* Never jump. */
749
  else if (e->k == VJMP)
184,639✔
750
    invertcond(fs, e), pc = e->u.s.info;
86,083✔
751
  else if (e->k == VKFALSE || e->k == VKNIL)
98,556✔
752
    expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
22,878✔
753
  else
754
    pc = bcemit_branch(fs, e, 0);
75,678✔
755
  jmp_append(fs, &e->f, pc);
199,569✔
756
  jmp_tohere(fs, e->t);
199,569✔
757
  e->t = NO_JMP;
199,569✔
758
}
199,569✔
759

760
/* Emit branch on false condition. */
761
static void bcemit_branch_f(FuncState *fs, ExpDesc *e)
90,526✔
762
{
763
  BCPos pc;
90,526✔
764
  expr_discharge(fs, e);
90,526✔
765
  if (e->k == VKNIL || e->k == VKFALSE)
90,526✔
766
    pc = NO_JMP;  /* Never jump. */
767
  else if (e->k == VJMP)
69,221✔
768
    pc = e->u.s.info;
34,117✔
769
  else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
35,104✔
770
    expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
13,883✔
771
  else
772
    pc = bcemit_branch(fs, e, 1);
21,221✔
773
  jmp_append(fs, &e->t, pc);
90,526✔
774
  jmp_tohere(fs, e->f);
90,526✔
775
  e->f = NO_JMP;
90,526✔
776
}
90,526✔
777

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

780
/* Try constant-folding of arithmetic operators. */
781
static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)
88,324✔
782
{
783
  TValue o;
88,324✔
784
  lua_Number n;
88,324✔
785
  if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;
88,324✔
786
  n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);
1,539✔
787
  setnumV(&o, n);
1,539✔
788
  if (tvisnan(&o) || tvismzero(&o)) return 0;  /* Avoid NaN and -0 as consts. */
1,539✔
789
  if (LJ_DUALNUM) {
1,517✔
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);
1,517✔
797
  return 1;
1,517✔
798
}
799

800
/* Emit arithmetic operator. */
801
static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
88,324✔
802
{
803
  BCReg rb, rc, t;
88,324✔
804
  uint32_t op;
88,324✔
805
  if (foldarith(opr, e1, e2))
88,324✔
806
    return;
807
  if (opr == OPR_POW) {
86,807✔
808
    op = BC_POW;
92✔
809
    rc = expr_toanyreg(fs, e2);
92✔
810
    rb = expr_toanyreg(fs, e1);
92✔
811
  } else {
812
    op = opr-OPR_ADD+BC_ADDVV;
86,715✔
813
    /* Must discharge 2nd operand first since VINDEXED might free regs. */
814
    expr_toval(fs, e2);
86,715✔
815
    if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)
86,715✔
816
      op -= BC_ADDVV-BC_ADDVN;
11,104✔
817
    else
818
      rc = expr_toanyreg(fs, e2);
75,611✔
819
    /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
820
    lj_assertFS(expr_isnumk(e1) || e1->k == VNONRELOC,
86,715✔
821
                "bad expr type %d", e1->k);
822
    expr_toval(fs, e1);
86,715✔
823
    /* Avoid two consts to satisfy bytecode constraints. */
824
    if (expr_isnumk(e1) && !expr_isnumk(e2) &&
86,715✔
825
        (t = const_num(fs, e1)) <= BCMAX_B) {
1,251✔
826
      rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;
1,251✔
827
    } else {
828
      rb = expr_toanyreg(fs, e1);
85,464✔
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--;
86,807✔
833
  if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
86,807✔
834
  e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);
86,807✔
835
  e1->k = VRELOCABLE;
86,807✔
836
}
837

838
/* Emit comparison operator. */
839
static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
347,240✔
840
{
841
  ExpDesc *eret = e1;
347,240✔
842
  BCIns ins;
347,240✔
843
  expr_toval(fs, e1);
347,240✔
844
  if (opr == OPR_EQ || opr == OPR_NE) {
347,240✔
845
    BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;
148,444✔
846
    BCReg ra;
148,444✔
847
    if (expr_isk(e1)) { e1 = e2; e2 = eret; }  /* Need constant in 2nd arg. */
148,444✔
848
    ra = expr_toanyreg(fs, e1);  /* First arg must be in a reg. */
148,444✔
849
    expr_toval(fs, e2);
148,444✔
850
    switch (e2->k) {
148,444✔
851
    case VKNIL: case VKFALSE: case VKTRUE:
48,954✔
852
      ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));
48,954✔
853
      break;
48,954✔
854
    case VKSTR:
3,863✔
855
      ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));
3,863✔
856
      break;
3,863✔
857
    case VKNUM:
4,960✔
858
      ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));
4,960✔
859
      break;
4,960✔
860
    default:
90,667✔
861
      ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));
90,667✔
862
      break;
90,667✔
863
    }
864
  } else {
865
    uint32_t op = opr-OPR_LT+BC_ISLT;
198,796✔
866
    BCReg ra, rd;
198,796✔
867
    if ((op-BC_ISLT) & 1) {  /* GT -> LT, GE -> LE */
198,796✔
868
      e1 = e2; e2 = eret;  /* Swap operands. */
99,506✔
869
      op = ((op-BC_ISLT)^3)+BC_ISLT;
99,506✔
870
      expr_toval(fs, e1);
99,506✔
871
      ra = expr_toanyreg(fs, e1);
99,506✔
872
      rd = expr_toanyreg(fs, e2);
99,506✔
873
    } else {
874
      rd = expr_toanyreg(fs, e2);
99,290✔
875
      ra = expr_toanyreg(fs, e1);
99,290✔
876
    }
877
    ins = BCINS_AD(op, ra, rd);
198,796✔
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--;
347,240✔
881
  if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
347,240✔
882
  bcemit_INS(fs, ins);
347,240✔
883
  eret->u.s.info = bcemit_jmp(fs);
347,240✔
884
  eret->k = VJMP;
347,240✔
885
}
347,240✔
886

887
/* Fixup left side of binary operator. */
888
static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)
621,441✔
889
{
890
  if (op == OPR_AND) {
621,441✔
891
    bcemit_branch_t(fs, e);
89,045✔
892
  } else if (op == OPR_OR) {
532,396✔
893
    bcemit_branch_f(fs, e);
90,526✔
894
  } else if (op == OPR_CONCAT) {
441,870✔
895
    expr_tonextreg(fs, e);
6,040✔
896
  } else if (op == OPR_EQ || op == OPR_NE) {
435,830✔
897
    if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);
148,448✔
898
  } else {
899
    if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);
287,382✔
900
  }
901
}
621,441✔
902

903
/* Emit binary operator. */
904
static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
620,977✔
905
{
906
  if (op <= OPR_POW) {
620,977✔
907
    bcemit_arith(fs, op, e1, e2);
88,324✔
908
  } else if (op == OPR_AND) {
532,653✔
909
    lj_assertFS(e1->t == NO_JMP, "jump list not closed");
89,045✔
910
    expr_discharge(fs, e2);
89,045✔
911
    jmp_append(fs, &e2->f, e1->f);
89,045✔
912
    *e1 = *e2;
89,045✔
913
  } else if (op == OPR_OR) {
443,608✔
914
    lj_assertFS(e1->f == NO_JMP, "jump list not closed");
90,526✔
915
    expr_discharge(fs, e2);
90,526✔
916
    jmp_append(fs, &e2->t, e1->t);
90,526✔
917
    *e1 = *e2;
90,526✔
918
  } else if (op == OPR_CONCAT) {
353,082✔
919
    expr_toval(fs, e2);
5,842✔
920
    if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
5,842✔
921
      lj_assertFS(e1->u.s.info == bc_b(*bcptr(fs, e2))-1,
1,611✔
922
                  "bad CAT stack layout");
923
      expr_free(fs, e1);
1,611✔
924
      setbc_b(bcptr(fs, e2), e1->u.s.info);
1,611✔
925
      e1->u.s.info = e2->u.s.info;
1,611✔
926
    } else {
927
      expr_tonextreg(fs, e2);
4,231✔
928
      expr_free(fs, e2);
4,231✔
929
      expr_free(fs, e1);
4,231✔
930
      e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);
4,231✔
931
    }
932
    e1->k = VRELOCABLE;
5,842✔
933
  } else {
934
    lj_assertFS(op == OPR_NE || op == OPR_EQ ||
347,240✔
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);
347,240✔
938
  }
939
}
620,977✔
940

941
/* Emit unary operator. */
942
static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
301,939✔
943
{
944
  if (op == BC_NOT) {
301,939✔
945
    /* Swap true and false lists. */
946
    { BCPos temp = e->f; e->f = e->t; e->t = temp; }
297,287✔
947
    jmp_dropval(fs, e->f);
297,287✔
948
    jmp_dropval(fs, e->t);
297,287✔
949
    expr_discharge(fs, e);
297,287✔
950
    if (e->k == VKNIL || e->k == VKFALSE) {
297,287✔
951
      e->k = VKTRUE;
35,858✔
952
      return;
35,858✔
953
    } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {
261,429✔
954
      e->k = VKFALSE;
3,090✔
955
      return;
3,090✔
956
    } else if (e->k == VJMP) {
258,339✔
957
      invertcond(fs, e);
118,961✔
958
      return;
118,961✔
959
    } else if (e->k == VRELOCABLE) {
139,378✔
960
      bcreg_reserve(fs, 1);
20,427✔
961
      setbc_a(bcptr(fs, e), fs->freereg-1);
20,427✔
962
      e->u.s.info = fs->freereg-1;
20,427✔
963
      e->k = VNONRELOC;
20,427✔
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);
4,652✔
969
    if (op == BC_UNM && !expr_hasjump(e)) {  /* Constant-fold negations. */
4,652✔
970
#if LJ_HASFFI
971
      if (e->k == VKCDATA) {  /* Fold in-place since cdata is not interned. */
2,875✔
972
        GCcdata *cd = cdataV(&e->u.nval);
21✔
973
        uint64_t *p = (uint64_t *)cdataptr(cd);
21✔
974
        if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
21✔
975
          p[1] ^= U64x(80000000,00000000);
3✔
976
        else
977
          *p = ~*p+1u;
18✔
978
        return;
21✔
979
      } else
980
#endif
981
      if (expr_isnumk(e) && !expr_numiszero(e)) {  /* Avoid folding to -0. */
2,854✔
982
        TValue *o = expr_numtv(e);
2,691✔
983
        if (tvisint(o)) {
2,691✔
984
          int32_t k = intV(o), negk = (int32_t)(~(uint32_t)k+1u);
985
          if (k == negk)
986
            setnumV(o, -(lua_Number)k);
987
          else
988
            setintV(o, negk);
989
          return;
990
        } else {
991
          o->u64 ^= U64x(80000000,00000000);
2,691✔
992
          return;
2,691✔
993
        }
994
      }
995
    }
996
    expr_toanyreg(fs, e);
1,940✔
997
  }
998
  expr_free(fs, e);
141,318✔
999
  e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);
141,318✔
1000
  e->k = VRELOCABLE;
141,318✔
1001
}
1002

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

1005
/* Check and consume optional token. */
1006
static int lex_opt(LexState *ls, LexToken tok)
3,460,256✔
1007
{
1008
  if (ls->tok == tok) {
3,460,256✔
1009
    lj_lex_next(ls);
957,331✔
1010
    return 1;
71,027✔
1011
  }
1012
  return 0;
1013
}
1014

1015
/* Check and consume token. */
1016
static void lex_check(LexState *ls, LexToken tok)
759,076✔
1017
{
1018
  if (ls->tok != tok)
759,076✔
1019
    err_token(ls, tok);
3,687✔
1020
  lj_lex_next(ls);
755,389✔
1021
}
755,372✔
1022

1023
/* Check for matching token. */
1024
static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)
847,061✔
1025
{
1026
  if (!lex_opt(ls, what)) {
847,061✔
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
}
846,088✔
1036

1037
/* Check for string token. */
1038
static GCstr *lex_str(LexState *ls)
1,635,887✔
1039
{
1040
  GCstr *s;
1,635,887✔
1041
  if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto))
1,635,887✔
1042
    err_token(ls, TK_name);
220✔
1043
  s = strV(&ls->tokval);
1,635,667✔
1044
  lj_lex_next(ls);
1,635,667✔
1045
  return s;
1,635,667✔
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)
156,962✔
1054
{
1055
  FuncState *fs = ls->fs;
156,962✔
1056
  MSize vtop = ls->vtop;
156,962✔
1057
  checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables");
156,962✔
1058
  if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
156,961✔
1059
    if (ls->sizevstack >= LJ_MAX_VSTACK)
31,647✔
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);
31,647✔
1062
  }
1063
  lj_assertFS((uintptr_t)name < VARNAME__MAX ||
156,961✔
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));
156,961✔
1068
  fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
156,961✔
1069
  ls->vtop = vtop+1;
156,961✔
1070
}
156,961✔
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)
119,789✔
1080
{
1081
  FuncState *fs = ls->fs;
119,789✔
1082
  BCReg nactvar = fs->nactvar;
119,789✔
1083
  while (nvars--) {
276,463✔
1084
    VarInfo *v = &var_get(ls, fs, nactvar);
156,674✔
1085
    v->startpc = fs->pc;
156,674✔
1086
    v->slot = nactvar++;
156,674✔
1087
    v->info = 0;
156,674✔
1088
  }
1089
  fs->nactvar = nactvar;
119,789✔
1090
}
1091

1092
/* Remove local variables. */
1093
static void var_remove(LexState *ls, BCReg tolevel)
293,964✔
1094
{
1095
  FuncState *fs = ls->fs;
293,964✔
1096
  while (fs->nactvar > tolevel)
446,042✔
1097
    var_get(ls, fs, --fs->nactvar).endpc = fs->pc;
152,078✔
1098
}
1099

1100
/* Lookup local variable name. */
1101
static BCReg var_lookup_local(FuncState *fs, GCstr *n)
1,300,462✔
1102
{
1103
  int i;
1,300,462✔
1104
  for (i = fs->nactvar-1; i >= 0; i--) {
4,894,308✔
1105
    if (n == strref(var_get(fs->ls, fs, i).name))
4,384,549✔
1106
      return (BCReg)i;
790,703✔
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,726,533✔
1132
{
1133
  if (fs) {
1,726,533✔
1134
    BCReg reg = var_lookup_local(fs, name);
1,300,462✔
1135
    if ((int32_t)reg >= 0) {  /* Local in this function? */
1,300,462✔
1136
      expr_init(e, VLOCAL, reg);
790,703✔
1137
      if (!first)
790,703✔
1138
        fscope_uvmark(fs, reg);  /* Scope now has an upvalue. */
818,423✔
1139
      return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);
790,703✔
1140
    } else {
1141
      MSize vidx = var_lookup_(fs->prev, name, e, 0);  /* Var in outer func? */
509,759✔
1142
      if ((int32_t)vidx >= 0) {  /* Yes, make it an upvalue here. */
509,699✔
1143
        e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e);
30,680✔
1144
        e->k = VUPVAL;
30,679✔
1145
        return vidx;
30,679✔
1146
      }
1147
    }
1148
  } else {  /* Not found in any function, must be a global. */
1149
    expr_init(e, VGLOBAL, 0);
426,071✔
1150
    e->u.sval = name;
426,071✔
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)
99,215✔
1163
{
1164
  FuncState *fs = ls->fs;
99,215✔
1165
  MSize vtop = ls->vtop;
99,215✔
1166
  if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
99,215✔
1167
    if (ls->sizevstack >= LJ_MAX_VSTACK)
243✔
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);
243✔
1170
  }
1171
  lj_assertFS(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL,
99,215✔
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));
99,215✔
1175
  ls->vstack[vtop].startpc = pc;
99,215✔
1176
  ls->vstack[vtop].slot = (uint8_t)fs->nactvar;
99,215✔
1177
  ls->vstack[vtop].info = info;
99,215✔
1178
  ls->vtop = vtop+1;
99,215✔
1179
  return vtop;
99,215✔
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)
5✔
1188
{
1189
  FuncState *fs = ls->fs;
5✔
1190
  BCPos pc = vg->startpc;
5✔
1191
  setgcrefnull(vg->name);  /* Invalidate pending goto. */
5✔
1192
  setbc_a(&fs->bcbase[pc].ins, vl->slot);
5✔
1193
  jmp_patch(fs, pc, vl->startpc);
5✔
1194
}
5✔
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)
66,528✔
1235
{
1236
  VarInfo *v = ls->vstack + bl->vstart;
66,528✔
1237
  VarInfo *ve = ls->vstack + ls->vtop;
66,528✔
1238
  for (; v < ve; v++) {
134,146✔
1239
    GCstr *name = strref(v->name);
67,626✔
1240
    if (name != NULL) {  /* Only consider remaining valid gotos/labels. */
67,626✔
1241
      if (gola_islabel(v)) {
67,559✔
1242
        VarInfo *vg;
25✔
1243
        setgcrefnull(v->name);  /* Invalidate label that goes out of scope. */
25✔
1244
        for (vg = v+1; vg < ve; vg++)  /* Resolve pending backward gotos. */
52✔
1245
          if (strref(vg->name) == name && gola_isgoto(vg)) {
27✔
1246
            if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot)
5✔
1247
              gola_close(ls, vg);
1✔
1248
            gola_patch(ls, vg, v);
32✔
1249
          }
1250
      } else if (gola_isgoto(v)) {
67,534✔
1251
        if (bl->prev) {  /* Propagate goto or break to outer scope. */
66,539✔
1252
          bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA;
66,531✔
1253
          v->slot = bl->nactvar;
66,531✔
1254
          if ((bl->flags & FSCOPE_UPVAL))
66,531✔
1255
            gola_close(ls, v);
17✔
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
}
66,520✔
1267

1268
/* Find existing label. */
1269
static VarInfo *gola_findlabel(LexState *ls, GCstr *name)
68✔
1270
{
1271
  VarInfo *v = ls->vstack + ls->fs->bl->vstart;
68✔
1272
  VarInfo *ve = ls->vstack + ls->vtop;
68✔
1273
  for (; v < ve; v++)
168✔
1274
    if (strref(v->name) == name && gola_islabel(v))
106✔
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)
294,824✔
1283
{
1284
  bl->nactvar = (uint8_t)fs->nactvar;
294,824✔
1285
  bl->flags = flags;
294,824✔
1286
  bl->vstart = fs->ls->vtop;
294,824✔
1287
  bl->prev = fs->bl;
294,824✔
1288
  fs->bl = bl;
294,824✔
1289
  lj_assertFS(fs->freereg == fs->nactvar, "bad regalloc");
294,824✔
1290
}
1291

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

1318
/* Mark scope as having an upvalue. */
1319
static void fscope_uvmark(FuncState *fs, BCReg level)
27,720✔
1320
{
1321
  FuncScope *bl;
27,720✔
1322
  for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev)
27,796✔
1323
    ;
76✔
1324
  if (bl)
27,720✔
1325
    bl->flags |= FSCOPE_UPVAL;
27,720✔
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)
75,441✔
1332
{
1333
  BCInsLine *base = fs->bcbase;
75,441✔
1334
  MSize i;
75,441✔
1335
  pt->sizebc = n;
75,441✔
1336
  bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,
150,882✔
1337
                   fs->framesize, 0);
1338
  for (i = 1; i < n; i++)
4,179,314✔
1339
    bc[i] = base[i].ins;
4,103,873✔
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)
75,441✔
1361
{
1362
  GCtab *kt;
75,441✔
1363
  TValue *array;
75,441✔
1364
  Node *node;
75,441✔
1365
  MSize i, hmask;
75,441✔
1366
  checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");
75,441✔
1367
  checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");
75,440✔
1368
  setmref(pt->k, kptr);
75,439✔
1369
  pt->sizekn = fs->nkn;
75,439✔
1370
  pt->sizekgc = fs->nkgc;
75,439✔
1371
  kt = fs->kt;
75,439✔
1372
  array = tvref(kt->array);
75,439✔
1373
  for (i = 0; i < kt->asize; i++)
83,351✔
1374
    if (tvhaskslot(&array[i])) {
7,912✔
1375
      TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])];
3,070✔
1376
      if (LJ_DUALNUM)
3,070✔
1377
        setintV(tv, (int32_t)i);
1378
      else
1379
        setnumV(tv, (lua_Number)i);
3,070✔
1380
    }
1381
  node = noderef(kt->node);
75,439✔
1382
  hmask = kt->hmask;
75,439✔
1383
  for (i = 0; i <= hmask; i++) {
1,630,182✔
1384
    Node *n = &node[i];
1,554,743✔
1385
    if (tvhaskslot(&n->val)) {
1,554,743✔
1386
      ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);
428,980✔
1387
      lj_assertFS(!tvisint(&n->key), "unexpected integer key");
428,980✔
1388
      if (tvisnum(&n->key)) {
428,980✔
1389
        TValue *tv = &((TValue *)kptr)[kidx];
73,585✔
1390
        if (LJ_DUALNUM) {
73,585✔
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;
73,585✔
1400
        }
1401
      } else {
1402
        GCobj *o = gcV(&n->key);
355,395✔
1403
        setgcref(((GCRef *)kptr)[~kidx], o);
355,395✔
1404
        lj_gc_objbarrier(fs->L, pt, o);
355,395✔
1405
        if (tvisproto(&n->key))
355,395✔
1406
          fs_fixup_uv2(fs, gco2pt(o));
19,936✔
1407
      }
1408
    }
1409
  }
1410
}
75,439✔
1411

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

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

1427
/* Fixup lineinfo for prototype. */
1428
static void fs_fixup_line(FuncState *fs, GCproto *pt,
75,439✔
1429
                          void *lineinfo, BCLine numline)
1430
{
1431
  BCInsLine *base = fs->bcbase + 1;
75,439✔
1432
  BCLine first = fs->linedefined;
75,439✔
1433
  MSize i = 0, n = fs->pc-1;
75,439✔
1434
  pt->firstline = fs->linedefined;
75,439✔
1435
  pt->numline = numline;
75,439✔
1436
  setmref(pt->lineinfo, lineinfo);
75,439✔
1437
  if (LJ_LIKELY(numline < 256)) {
75,439✔
1438
    uint8_t *li = (uint8_t *)lineinfo;
3,788,349✔
1439
    do {
3,788,349✔
1440
      BCLine delta = base[i].line - first;
3,788,349✔
1441
      lj_assertFS(delta >= 0 && delta < 256, "bad line delta");
3,788,349✔
1442
      li[i] = (uint8_t)delta;
3,788,349✔
1443
    } while (++i < n);
3,788,349✔
1444
  } else if (LJ_LIKELY(numline < 65536)) {
324✔
1445
    uint16_t *li = (uint16_t *)lineinfo;
53,231✔
1446
    do {
53,231✔
1447
      BCLine delta = base[i].line - first;
53,231✔
1448
      lj_assertFS(delta >= 0 && delta < 65536, "bad line delta");
53,231✔
1449
      li[i] = (uint16_t)delta;
53,231✔
1450
    } while (++i < n);
53,231✔
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
}
75,439✔
1460

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

1503
/* Fixup variable info for prototype. */
1504
static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar)
75,439✔
1505
{
1506
  setmref(pt->uvinfo, p);
75,439✔
1507
  setmref(pt->varinfo, (char *)p + ofsvar);
75,439✔
1508
  memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb));  /* Copy from temp. buffer. */
75,439✔
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
/* Fixup return instruction for prototype. */
1523
static void fs_fixup_ret(FuncState *fs)
75,449✔
1524
{
1525
  BCPos lastpc = fs->pc;
75,449✔
1526
  if (lastpc <= fs->lasttarget || !bc_isret_or_tail(bc_op(fs->bcbase[lastpc-1].ins))) {
75,449✔
1527
    if ((fs->bl->flags & FSCOPE_UPVAL))
44,731✔
1528
      bcemit_AJ(fs, BC_UCLO, 0, 0);
591✔
1529
    bcemit_AD(fs, BC_RET0, 0, 1);  /* Need final return. */
44,731✔
1530
  }
1531
  fs->bl->flags |= FSCOPE_NOCLOSE;  /* Handled above. */
75,449✔
1532
  fscope_end(fs);
75,449✔
1533
  lj_assertFS(fs->bl == NULL, "bad scope nesting");
75,441✔
1534
  /* May need to fixup returns encoded before first function was created. */
1535
  if (fs->flags & PROTO_FIXUP_RETURN) {
75,441✔
1536
    BCPos pc;
1537
    for (pc = 1; pc < lastpc; pc++) {
3,156✔
1538
      BCIns ins = fs->bcbase[pc].ins;
3,156✔
1539
      BCPos offset;
3,156✔
1540
      switch (bc_op(ins)) {
3,156✔
1541
      case BC_CALLMT: case BC_CALLT:
7✔
1542
      case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1543
        offset = bcemit_INS(fs, ins);  /* Copy original instruction. */
7✔
1544
        fs->bcbase[offset].line = fs->bcbase[pc].line;
7✔
1545
        offset = offset-(pc+1)+BCBIAS_J;
7✔
1546
        if (offset > BCMAX_D)
7✔
1547
          err_syntax(fs->ls, LJ_ERR_XFIXUP);
×
1548
        /* Replace with UCLO plus branch. */
1549
        fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset);
7✔
1550
        break;
7✔
1551
      case BC_FNEW:
1552
        return;  /* We're done. */
1553
      default:
1554
        break;
1555
      }
1556
    }
1557
  }
1558
}
1559

1560
/* Finish a FuncState and return the new prototype. */
1561
static GCproto *fs_finish(LexState *ls, BCLine line)
75,449✔
1562
{
1563
  lua_State *L = ls->L;
75,449✔
1564
  FuncState *fs = ls->fs;
75,449✔
1565
  BCLine numline = line - fs->linedefined;
75,449✔
1566
  size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;
75,449✔
1567
  GCproto *pt;
75,449✔
1568

1569
  /* Apply final fixups. */
1570
  fs_fixup_ret(fs);
75,449✔
1571

1572
  /* Calculate total size of prototype including all colocated arrays. */
1573
  sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
75,441✔
1574
  sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);
75,441✔
1575
  ofsk = sizept; sizept += fs->nkn*sizeof(TValue);
75,441✔
1576
  ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;
75,441✔
1577
  ofsli = sizept; sizept += fs_prep_line(fs, numline);
75,441✔
1578
  ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);
75,441✔
1579

1580
  /* Allocate prototype and initialize its fields. */
1581
  pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
75,441✔
1582
  pt->gct = ~LJ_TPROTO;
75,441✔
1583
  pt->sizept = (MSize)sizept;
75,441✔
1584
  pt->trace = 0;
75,441✔
1585
  pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN));
75,441✔
1586
  pt->numparams = fs->numparams;
75,441✔
1587
  pt->framesize = fs->framesize;
75,441✔
1588
  setgcref(pt->chunkname, obj2gco(ls->chunkname));
75,441✔
1589

1590
  /* Close potentially uninitialized gap between bc and kgc. */
1591
  *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0;
75,441✔
1592
  fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc);
75,441✔
1593
  fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk));
75,441✔
1594
  fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv));
75,439✔
1595
  fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline);
75,439✔
1596
  fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar);
75,439✔
1597

1598
  lj_vmevent_send(L, BC,
75,439✔
1599
    setprotoV(L, L->top++, pt);
1600
  );
75,439✔
1601

1602
  /* Add a new prototype to the profiler. */
1603
#if LJ_HASMEMPROF
1604
  lj_memprof_add_proto(pt);
75,439✔
1605
#endif
1606
#if LJ_HASSYSPROF
1607
  lj_sysprof_add_proto(pt);
75,439✔
1608
#endif
1609

1610
  L->top--;  /* Pop table of constants. */
75,439✔
1611
  ls->vtop = fs->vbase;  /* Reset variable stack. */
75,439✔
1612
  ls->fs = fs->prev;
75,439✔
1613
  lj_assertL(ls->fs != NULL || ls->tok == TK_eof, "bad parser state");
75,439✔
1614
  return pt;
75,439✔
1615
}
1616

1617
/* Initialize a new FuncState. */
1618
static void fs_init(LexState *ls, FuncState *fs)
91,820✔
1619
{
1620
  lua_State *L = ls->L;
91,820✔
1621
  fs->prev = ls->fs; ls->fs = fs;  /* Append to list. */
91,820✔
1622
  fs->ls = ls;
91,820✔
1623
  fs->vbase = ls->vtop;
91,820✔
1624
  fs->L = L;
91,820✔
1625
  fs->pc = 0;
91,820✔
1626
  fs->lasttarget = 0;
91,820✔
1627
  fs->jpc = NO_JMP;
91,820✔
1628
  fs->freereg = 0;
91,820✔
1629
  fs->nkgc = 0;
91,820✔
1630
  fs->nkn = 0;
91,820✔
1631
  fs->nactvar = 0;
91,820✔
1632
  fs->nuv = 0;
91,820✔
1633
  fs->bl = NULL;
91,820✔
1634
  fs->flags = 0;
91,820✔
1635
  fs->framesize = 1;  /* Minimum frame size. */
91,820✔
1636
  fs->kt = lj_tab_new(L, 0, 0);
91,820✔
1637
  /* Anchor table of constants in stack to avoid being collected. */
1638
  settabV(L, L->top, fs->kt);
91,819✔
1639
  incr_top(L);
91,819✔
1640
}
91,819✔
1641

1642
/* -- Expressions --------------------------------------------------------- */
1643

1644
/* Forward declaration. */
1645
static void expr(LexState *ls, ExpDesc *v);
1646

1647
/* Return string expression. */
1648
static void expr_str(LexState *ls, ExpDesc *e)
284,981✔
1649
{
1650
  expr_init(e, VKSTR, 0);
284,981✔
1651
  e->u.sval = lex_str(ls);
569,962✔
1652
}
1653

1654
/* Return index expression. */
1655
static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e)
1656
{
1657
  /* Already called: expr_toval(fs, e). */
1658
  t->k = VINDEXED;
1659
  if (expr_isnumk(e)) {
1660
#if LJ_DUALNUM
1661
    if (tvisint(expr_numtv(e))) {
1662
      int32_t k = intV(expr_numtv(e));
1663
      if (checku8(k)) {
1664
        t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1665
        return;
1666
      }
1667
    }
1668
#else
1669
    lua_Number n = expr_numberV(e);
1670
    int32_t k = lj_num2int(n);
1671
    if (checku8(k) && n == (lua_Number)k) {
1672
      t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1673
      return;
1674
    }
1675
#endif
1676
  } else if (expr_isstrk(e)) {
1677
    BCReg idx = const_str(fs, e);
1678
    if (idx <= BCMAX_C) {
1679
      t->u.s.aux = ~idx;  /* -256..-1: const string key */
1680
      return;
1681
    }
1682
  }
1683
  t->u.s.aux = expr_toanyreg(fs, e);  /* 0..255: register */
1684
}
1685

1686
/* Parse index expression with named field. */
1687
static void expr_field(LexState *ls, ExpDesc *v)
189,674✔
1688
{
1689
  FuncState *fs = ls->fs;
189,674✔
1690
  ExpDesc key;
189,674✔
1691
  expr_toanyreg(fs, v);
189,674✔
1692
  lj_lex_next(ls);  /* Skip dot or colon. */
189,674✔
1693
  expr_str(ls, &key);
189,674✔
1694
  expr_index(fs, v, &key);
189,674✔
1695
}
189,674✔
1696

1697
/* Parse index expression with brackets. */
1698
static void expr_bracket(LexState *ls, ExpDesc *v)
14,705✔
1699
{
1700
  lj_lex_next(ls);  /* Skip '['. */
14,705✔
1701
  expr(ls, v);
29,410✔
1702
  expr_toval(ls->fs, v);
14,705✔
1703
  lex_check(ls, ']');
14,705✔
1704
}
14,705✔
1705

1706
/* Get value of constant expression. */
1707
static void expr_kvalue(FuncState *fs, TValue *v, ExpDesc *e)
394,901✔
1708
{
1709
  UNUSED(fs);
394,901✔
1710
  if (e->k <= VKTRUE) {
394,901✔
1711
    setpriV(v, ~(uint32_t)e->k);
1,190✔
1712
  } else if (e->k == VKSTR) {
393,711✔
1713
    setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);
276,720✔
1714
  } else {
1715
    lj_assertFS(tvisnumber(expr_numtv(e)), "bad number constant");
244,591✔
1716
    *v = *expr_numtv(e);
244,591✔
1717
  }
1718
}
1719

1720
/* Parse table constructor expression. */
1721
static void expr_table(LexState *ls, ExpDesc *e)
12,406✔
1722
{
1723
  FuncState *fs = ls->fs;
12,406✔
1724
  BCLine line = ls->linenumber;
12,406✔
1725
  GCtab *t = NULL;
12,406✔
1726
  int vcall = 0, needarr = 0, fixt = 0;
12,406✔
1727
  uint32_t narr = 1;  /* First array index. */
12,406✔
1728
  uint32_t nhash = 0;  /* Number of hash entries. */
12,406✔
1729
  BCReg freg = fs->freereg;
12,406✔
1730
  BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0);
12,406✔
1731
  expr_init(e, VNONRELOC, freg);
12,406✔
1732
  bcreg_reserve(fs, 1);
12,406✔
1733
  freg++;
12,406✔
1734
  lex_check(ls, '{');
12,406✔
1735
  while (ls->tok != '}') {
215,384✔
1736
    ExpDesc key, val;
206,257✔
1737
    vcall = 0;
206,257✔
1738
    if (ls->tok == '[') {
206,257✔
1739
      expr_bracket(ls, &key);  /* Already calls expr_toval. */
893✔
1740
      if (!expr_isk(&key)) expr_index(fs, e, &key);
893✔
1741
      if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++;
893✔
1742
      lex_check(ls, '=');
893✔
1743
    } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) &&
290,456✔
1744
               lj_lex_lookahead(ls) == '=') {
85,092✔
1745
      expr_str(ls, &key);
84,537✔
1746
      lex_check(ls, '=');
84,537✔
1747
      nhash++;
84,537✔
1748
    } else {
1749
      expr_init(&key, VKNUM, 0);
120,827✔
1750
      setintV(&key.u.nval, (int)narr);
120,827✔
1751
      narr++;
120,827✔
1752
      needarr = vcall = 1;
120,827✔
1753
    }
1754
    expr(ls, &val);
412,316✔
1755
    if (expr_isk(&key) && key.k != VKNIL &&
206,059✔
1756
        (key.k == VKSTR || expr_isk_nojump(&val))) {
120,880✔
1757
      TValue k, *v;
203,135✔
1758
      if (!t) {  /* Create template table on demand. */
203,135✔
1759
        BCReg kidx;
5,048✔
1760
        t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash));
6,923✔
1761
        kidx = const_gc(fs, obj2gco(t), LJ_TTAB);
5,048✔
1762
        fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);
5,048✔
1763
      }
1764
      vcall = 0;
203,135✔
1765
      expr_kvalue(fs, &k, &key);
203,135✔
1766
      v = lj_tab_set(fs->L, t, &k);
203,135✔
1767
      lj_gc_anybarriert(fs->L, t);
203,135✔
1768
      if (expr_isk_nojump(&val)) {  /* Add const key/value to template table. */
203,135✔
1769
        expr_kvalue(fs, v, &val);
191,766✔
1770
      } else {  /* Otherwise create dummy string key (avoids lj_tab_newkey). */
1771
        settabV(fs->L, v, t);  /* Preserve key with table itself as value. */
11,369✔
1772
        fixt = 1;   /* Fix this later, after all resizes. */
11,369✔
1773
        goto nonconst;
11,369✔
1774
      }
1775
    } else {
1776
    nonconst:
2,924✔
1777
      if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; }
14,293✔
1778
      if (expr_isk(&key)) expr_index(fs, e, &key);
14,293✔
1779
      bcemit_store(fs, e, &val);
14,293✔
1780
    }
1781
    fs->freereg = freg;
206,059✔
1782
    if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break;
206,059✔
1783
  }
1784
  lex_match(ls, '}', '{', line);
12,208✔
1785
  if (vcall) {
12,206✔
1786
    BCInsLine *ilp = &fs->bcbase[fs->pc-1];
320✔
1787
    ExpDesc en;
320✔
1788
    lj_assertFS(bc_a(ilp->ins) == freg &&
320✔
1789
                bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB),
1790
                "bad CALL code generation");
1791
    expr_init(&en, VKNUM, 0);
320✔
1792
    en.u.nval.u32.lo = narr-1;
320✔
1793
    en.u.nval.u32.hi = 0x43300000;  /* Biased integer to avoid denormals. */
320✔
1794
    if (narr > 256) { fs->pc--; ilp--; }
320✔
1795
    ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en));
320✔
1796
    setbc_b(&ilp[-1].ins, 0);
320✔
1797
  }
1798
  if (pc == fs->pc-1) {  /* Make expr relocable if possible. */
12,206✔
1799
    e->u.s.info = pc;
8,928✔
1800
    fs->freereg--;
8,928✔
1801
    e->k = VRELOCABLE;
8,928✔
1802
  } else {
1803
    e->k = VNONRELOC;  /* May have been changed by expr_index. */
3,278✔
1804
  }
1805
  if (!t) {  /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */
12,206✔
1806
    BCIns *ip = &fs->bcbase[pc].ins;
7,160✔
1807
    if (!needarr) narr = 0;
7,160✔
1808
    else if (narr < 3) narr = 3;
439✔
1809
    else if (narr > 0x7ff) narr = 0x7ff;
1810
    setbc_d(ip, narr|(hsize2hbits(nhash)<<11));
7,160✔
1811
  } else {
1812
    if (needarr && t->asize < narr)
5,046✔
1813
      lj_tab_reasize(fs->L, t, narr-1);
52✔
1814
    if (fixt) {  /* Fix value for dummy keys in template table. */
5,046✔
1815
      Node *node = noderef(t->node);
2,592✔
1816
      uint32_t i, hmask = t->hmask;
2,592✔
1817
      for (i = 0; i <= hmask; i++) {
18,272✔
1818
        Node *n = &node[i];
15,680✔
1819
        if (tvistab(&n->val)) {
15,680✔
1820
          lj_assertFS(tabV(&n->val) == t, "bad dummy key in template table");
11,369✔
1821
          setnilV(&n->val);  /* Turn value into nil. */
11,369✔
1822
        }
1823
      }
1824
    }
1825
    lj_gc_check(fs->L);
5,046✔
1826
  }
1827
}
12,206✔
1828

1829
/* Parse function parameters. */
1830
static BCReg parse_params(LexState *ls, int needself)
23,627✔
1831
{
1832
  FuncState *fs = ls->fs;
23,627✔
1833
  BCReg nparams = 0;
23,627✔
1834
  lex_check(ls, '(');
23,627✔
1835
  if (needself)
23,530✔
1836
    var_new_lit(ls, nparams++, "self");
31✔
1837
  if (ls->tok != ')') {
23,530✔
1838
    do {
41,257✔
1839
      if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
41,257✔
1840
        var_new(ls, nparams++, lex_str(ls));
40,353✔
1841
      } else if (ls->tok == TK_dots) {
904✔
1842
        lj_lex_next(ls);
848✔
1843
        fs->flags |= PROTO_VARARG;
848✔
1844
        break;
848✔
1845
      } else {
1846
        err_syntax(ls, LJ_ERR_XPARAM);
56✔
1847
      }
1848
    } while (lex_opt(ls, ','));
40,353✔
1849
  }
1850
  var_add(ls, nparams);
23,474✔
1851
  lj_assertFS(fs->nactvar == nparams, "bad regalloc");
23,474✔
1852
  bcreg_reserve(fs, nparams);
23,474✔
1853
  lex_check(ls, ')');
23,474✔
1854
  return nparams;
23,419✔
1855
}
1856

1857
/* Forward declaration. */
1858
static void parse_chunk(LexState *ls);
1859

1860
/* Parse body of a function. */
1861
static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
23,627✔
1862
{
1863
  FuncState fs, *pfs = ls->fs;
23,627✔
1864
  FuncScope bl;
23,627✔
1865
  GCproto *pt;
23,627✔
1866
  ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
23,627✔
1867
  fs_init(ls, &fs);
23,627✔
1868
  fscope_begin(&fs, &bl, 0);
23,627✔
1869
  fs.linedefined = line;
23,627✔
1870
  fs.numparams = (uint8_t)parse_params(ls, needself);
23,627✔
1871
  fs.bcbase = pfs->bcbase + pfs->pc;
23,419✔
1872
  fs.bclim = pfs->bclim - pfs->pc;
23,419✔
1873
  bcemit_AD(&fs, BC_FUNCF, 0, 0);  /* Placeholder. */
23,419✔
1874
  parse_chunk(ls);
23,419✔
1875
  if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line);
20,458✔
1876
  pt = fs_finish(ls, (ls->lastline = ls->linenumber));
19,962✔
1877
  pfs->bcbase = ls->bcstack + oldbase;  /* May have been reallocated. */
19,961✔
1878
  pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
19,961✔
1879
  /* Store new prototype in the constant array of the parent. */
1880
  expr_init(e, VRELOCABLE,
59,883✔
1881
            bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));
19,961✔
1882
#if LJ_HASFFI
1883
  pfs->flags |= (fs.flags & PROTO_FFI);
19,961✔
1884
#endif
1885
  if (!(pfs->flags & PROTO_CHILD)) {
19,961✔
1886
    if (pfs->flags & PROTO_HAS_RETURN)
8,606✔
1887
      pfs->flags |= PROTO_FIXUP_RETURN;
1,716✔
1888
    pfs->flags |= PROTO_CHILD;
8,606✔
1889
  }
1890
  lj_lex_next(ls);
19,961✔
1891
}
19,961✔
1892

1893
/* Parse expression list. Last expression is left open. */
1894
static BCReg expr_list(LexState *ls, ExpDesc *v)
791,695✔
1895
{
1896
  BCReg n = 1;
791,695✔
1897
  expr(ls, v);
1,620,752✔
1898
  while (lex_opt(ls, ',')) {
829,057✔
1899
    expr_tonextreg(ls->fs, v);
45,471✔
1900
    expr(ls, v);
90,936✔
1901
    n++;
45,465✔
1902
  }
1903
  return n;
783,586✔
1904
}
1905

1906
/* Parse function argument list. */
1907
static void parse_args(LexState *ls, ExpDesc *e)
318,732✔
1908
{
1909
  FuncState *fs = ls->fs;
318,732✔
1910
  ExpDesc args;
318,732✔
1911
  BCIns ins;
318,732✔
1912
  BCReg base;
318,732✔
1913
  BCLine line = ls->linenumber;
318,732✔
1914
  if (ls->tok == '(') {
318,732✔
1915
#if !LJ_52
1916
    if (line != ls->lastline)
317,250✔
1917
      err_syntax(ls, LJ_ERR_XAMBIG);
2✔
1918
#endif
1919
    lj_lex_next(ls);
317,248✔
1920
    if (ls->tok == ')') {  /* f(). */
317,248✔
1921
      args.k = VVOID;
101,709✔
1922
    } else {
1923
      expr_list(ls, &args);
215,539✔
1924
      if (args.k == VCALL)  /* f(a, b, g()) or f(a, b, ...). */
213,118✔
1925
        setbc_b(bcptr(fs, &args), 0);  /* Pass on multiple results. */
5,693✔
1926
    }
1927
    lex_match(ls, ')', '(', line);
314,827✔
1928
  } else if (ls->tok == '{') {
1,482✔
1929
    expr_table(ls, &args);
140✔
1930
  } else if (ls->tok == TK_string) {
1,342✔
1931
    expr_init(&args, VKSTR, 0);
1,341✔
1932
    args.u.sval = strV(&ls->tokval);
1,341✔
1933
    lj_lex_next(ls);
1,341✔
1934
  } else {
1935
    err_syntax(ls, LJ_ERR_XFUNARG);
1✔
1936
    return;  /* Silence compiler. */
1937
  }
1938
  lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k);
316,012✔
1939
  base = e->u.s.info;  /* Base register for call. */
316,012✔
1940
  if (args.k == VCALL) {
316,012✔
1941
    ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);
5,693✔
1942
  } else {
1943
    if (args.k != VVOID)
310,319✔
1944
      expr_tonextreg(fs, &args);
208,610✔
1945
    ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2);
310,319✔
1946
  }
1947
  expr_init(e, VCALL, bcemit_INS(fs, ins));
316,012✔
1948
  e->u.s.aux = base;
316,012✔
1949
  fs->bcbase[fs->pc - 1].line = line;
316,012✔
1950
  fs->freereg = base+1;  /* Leave one result by default. */
316,012✔
1951
}
1952

1953
/* Parse primary expression. */
1954
static void expr_primary(LexState *ls, ExpDesc *v)
1,617,462✔
1955
{
1956
  FuncState *fs = ls->fs;
1,617,462✔
1957
  /* Parse prefix expression. */
1958
  if (ls->tok == '(') {
1,617,462✔
1959
    BCLine line = ls->linenumber;
402,572✔
1960
    lj_lex_next(ls);
402,572✔
1961
    expr(ls, v);
804,873✔
1962
    lex_match(ls, ')', '(', line);
402,301✔
1963
    expr_discharge(ls->fs, v);
402,155✔
1964
  } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
1,214,890✔
1965
    var_lookup(ls, v);
1,207,845✔
1966
  } else {
1967
    err_syntax(ls, LJ_ERR_XSYMBOL);
7,045✔
1968
  }
1969
  for (;;) {  /* Parse multiple expression suffixes. */
2,128,913✔
1970
    if (ls->tok == '.') {
2,128,913✔
1971
      expr_field(ls, v);
189,090✔
1972
    } else if (ls->tok == '[') {
1,939,823✔
1973
      ExpDesc key;
13,812✔
1974
      expr_toanyreg(fs, v);
13,812✔
1975
      expr_bracket(ls, &key);
13,812✔
1976
      expr_index(fs, v, &key);
13,812✔
1977
    } else if (ls->tok == ':') {
1,926,011✔
1978
      ExpDesc key;
10,770✔
1979
      lj_lex_next(ls);
10,770✔
1980
      expr_str(ls, &key);
10,770✔
1981
      bcemit_method(fs, v, &key);
10,770✔
1982
      parse_args(ls, v);
10,770✔
1983
    } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') {
1,915,241✔
1984
      expr_tonextreg(fs, v);
307,962✔
1985
      if (LJ_FR2) bcreg_reserve(fs, 1);
307,962✔
1986
      parse_args(ls, v);
307,962✔
1987
    } else {
1988
      break;
1989
    }
1990
  }
1991
}
1,607,279✔
1992

1993
/* Parse simple expression. */
1994
static void expr_simple(LexState *ls, ExpDesc *v)
2,204,782✔
1995
{
1996
  switch (ls->tok) {
2,204,782✔
1997
  case TK_number:
713,461✔
1998
    expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0);
713,461✔
1999
    copyTV(ls->L, &v->u.nval, &ls->tokval);
713,461✔
2000
    break;
2001
  case TK_string:
2002
    expr_init(v, VKSTR, 0);
234,473✔
2003
    v->u.sval = strV(&ls->tokval);
234,473✔
2004
    break;
234,473✔
2005
  case TK_nil:
2006
    expr_init(v, VKNIL, 0);
115,675✔
2007
    break;
2008
  case TK_true:
2009
    expr_init(v, VKTRUE, 0);
17,418✔
2010
    break;
2011
  case TK_false:
2012
    expr_init(v, VKFALSE, 0);
15,727✔
2013
    break;
2014
  case TK_dots: {  /* Vararg. */
3,060✔
2015
    FuncState *fs = ls->fs;
3,060✔
2016
    BCReg base;
3,060✔
2017
    checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS);
3,060✔
2018
    bcreg_reserve(fs, 1);
3,059✔
2019
    base = fs->freereg-1;
3,059✔
2020
    expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams));
3,059✔
2021
    v->u.s.aux = base;
3,059✔
2022
    break;
3,059✔
2023
  }
2024
  case '{':  /* Table constructor. */
12,266✔
2025
    expr_table(ls, v);
12,266✔
2026
    return;
12,266✔
2027
  case TK_function:
6,090✔
2028
    lj_lex_next(ls);
6,090✔
2029
    parse_body(ls, v, 0, ls->linenumber);
6,090✔
2030
    return;
6,090✔
2031
  default:
1,086,612✔
2032
    expr_primary(ls, v);
1,086,612✔
2033
    return;
1,086,612✔
2034
  }
2035
  lj_lex_next(ls);
1,099,813✔
2036
}
2037

2038
/* Manage syntactic levels to avoid blowing up the stack. */
2039
static void synlevel_begin(LexState *ls)
2,752,504✔
2040
{
2041
  if (++ls->level >= LJ_MAX_XLEVEL)
2,752,504✔
2042
    lj_lex_error(ls, 0, LJ_ERR_XLEVELS);
8✔
2043
}
2044

2045
#define synlevel_end(ls)        ((ls)->level--)
2046

2047
/* Convert token to binary operator. */
2048
static BinOpr token2binop(LexToken tok)
2,498,189✔
2049
{
2050
  switch (tok) {
2,498,189✔
2051
  case '+':        return OPR_ADD;
2052
  case '-':        return OPR_SUB;
4,316✔
2053
  case '*':        return OPR_MUL;
36,625✔
2054
  case '/':        return OPR_DIV;
183✔
2055
  case '%':        return OPR_MOD;
142✔
2056
  case '^':        return OPR_POW;
1,143✔
2057
  case TK_concat: return OPR_CONCAT;
6,040✔
2058
  case TK_ne:        return OPR_NE;
87,313✔
2059
  case TK_eq:        return OPR_EQ;
94,260✔
2060
  case '<':        return OPR_LT;
98,914✔
2061
  case TK_le:        return OPR_LE;
400✔
2062
  case '>':        return OPR_GT;
811✔
2063
  case TK_ge:        return OPR_GE;
98,735✔
2064
  case TK_and:        return OPR_AND;
105,622✔
2065
  case TK_or:        return OPR_OR;
106,931✔
2066
  default:        return OPR_NOBINOPR;
1,810,129✔
2067
  }
2068
}
2069

2070
/* Priorities for each binary operator. ORDER OPR. */
2071
static const struct {
2072
  uint8_t left;                /* Left priority. */
2073
  uint8_t right;        /* Right priority. */
2074
} priority[] = {
2075
  {6,6}, {6,6}, {7,7}, {7,7}, {7,7},        /* ADD SUB MUL DIV MOD */
2076
  {10,9}, {5,4},                        /* POW CONCAT (right associative) */
2077
  {3,3}, {3,3},                                /* EQ NE */
2078
  {3,3}, {3,3}, {3,3}, {3,3},                /* LT GE GT LE */
2079
  {2,2}, {1,1}                                /* AND OR */
2080
};
2081

2082
#define UNARY_PRIORITY                8  /* Priority for unary operators. */
2083

2084
/* Forward declaration. */
2085
static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit);
2086

2087
/* Parse unary expression. */
2088
static void expr_unop(LexState *ls, ExpDesc *v)
2,506,721✔
2089
{
2090
  BCOp op;
2,506,721✔
2091
  if (ls->tok == TK_not) {
2,506,721✔
2092
    op = BC_NOT;
2093
  } else if (ls->tok == '-') {
2,209,434✔
2094
    op = BC_UNM;
2095
  } else if (ls->tok == '#') {
2,206,558✔
2096
    op = BC_LEN;
2097
  } else {
2098
    expr_simple(ls, v);
2,204,782✔
2099
    return;
2,204,782✔
2100
  }
2101
  lj_lex_next(ls);
301,939✔
2102
  expr_binop(ls, v, UNARY_PRIORITY);
301,939✔
2103
  bcemit_unop(ls->fs, op, v);
301,939✔
2104
}
2105

2106
/* Parse binary expressions with priority higher than the limit. */
2107
static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit)
2,506,727✔
2108
{
2109
  BinOpr op;
2,506,727✔
2110
  synlevel_begin(ls);
2,506,727✔
2111
  expr_unop(ls, v);
2,506,721✔
2112
  op = token2binop(ls->tok);
2,498,189✔
2113
  while (op != OPR_NOBINOPR && priority[op].left > limit) {
3,119,166✔
2114
    ExpDesc v2;
621,487✔
2115
    BinOpr nextop;
621,487✔
2116
    lj_lex_next(ls);
621,487✔
2117
    bcemit_binop_left(ls->fs, op, v);
621,441✔
2118
    /* Parse binary expression with higher priority. */
2119
    nextop = expr_binop(ls, &v2, priority[op].right);
621,441✔
2120
    bcemit_binop(ls->fs, op, v, &v2);
620,977✔
2121
    op = nextop;
620,977✔
2122
  }
2123
  synlevel_end(ls);
2,497,679✔
2124
  return op;  /* Return unconsumed binary operator (if any). */
2,497,679✔
2125
}
2126

2127
/* Parse expression. */
2128
static void expr(LexState *ls, ExpDesc *v)
1,583,347✔
2129
{
2130
  expr_binop(ls, v, 0);  /* Priority 0: parse whole expression. */
1,460,700✔
2131
}
783,592✔
2132

2133
/* Assign expression to the next register. */
2134
static void expr_next(LexState *ls)
12,117✔
2135
{
2136
  ExpDesc e;
12,117✔
2137
  expr(ls, &e);
12,117✔
2138
  expr_tonextreg(ls->fs, &e);
12,117✔
2139
}
12,117✔
2140

2141
/* Parse conditional expression. */
2142
static BCPos expr_cond(LexState *ls)
110,530✔
2143
{
2144
  ExpDesc v;
110,530✔
2145
  expr(ls, &v);
110,530✔
2146
  if (v.k == VKNIL) v.k = VKFALSE;
110,524✔
2147
  bcemit_branch_t(ls->fs, &v);
110,524✔
2148
  return v.f;
110,524✔
2149
}
2150

2151
/* -- Assignments --------------------------------------------------------- */
2152

2153
/* List of LHS variables. */
2154
typedef struct LHSVarList {
2155
  ExpDesc v;                        /* LHS variable. */
2156
  struct LHSVarList *prev;        /* Link to previous LHS variable. */
2157
} LHSVarList;
2158

2159
/* Eliminate write-after-read hazards for local variable assignment. */
2160
static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v)
2161
{
2162
  FuncState *fs = ls->fs;
2163
  BCReg reg = v->u.s.info;  /* Check against this variable. */
2164
  BCReg tmp = fs->freereg;  /* Rename to this temp. register (if needed). */
2165
  int hazard = 0;
2166
  for (; lh; lh = lh->prev) {
2167
    if (lh->v.k == VINDEXED) {
2168
      if (lh->v.u.s.info == reg) {  /* t[i], t = 1, 2 */
2169
        hazard = 1;
2170
        lh->v.u.s.info = tmp;
2171
      }
2172
      if (lh->v.u.s.aux == reg) {  /* t[i], i = 1, 2 */
2173
        hazard = 1;
2174
        lh->v.u.s.aux = tmp;
2175
      }
2176
    }
2177
  }
2178
  if (hazard) {
2179
    bcemit_AD(fs, BC_MOV, tmp, reg);  /* Rename conflicting variable. */
2180
    bcreg_reserve(fs, 1);
2181
  }
2182
}
2183

2184
/* Adjust LHS/RHS of an assignment. */
2185
static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e)
2186
{
2187
  FuncState *fs = ls->fs;
2188
  int32_t extra = (int32_t)nvars - (int32_t)nexps;
2189
  if (e->k == VCALL) {
2190
    extra++;  /* Compensate for the VCALL itself. */
2191
    if (extra < 0) extra = 0;
2192
    setbc_b(bcptr(fs, e), extra+1);  /* Fixup call results. */
2193
    if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1);
2194
  } else {
2195
    if (e->k != VVOID)
2196
      expr_tonextreg(fs, e);  /* Close last expression. */
2197
    if (extra > 0) {  /* Leftover LHS are set to nil. */
2198
      BCReg reg = fs->freereg;
2199
      bcreg_reserve(fs, (BCReg)extra);
2200
      bcemit_nil(fs, reg, (BCReg)extra);
2201
    }
2202
  }
2203
  if (nexps > nvars)
2204
    ls->fs->freereg -= nexps - nvars;  /* Drop leftover regs. */
2205
}
2206

2207
/* Recursively parse assignment statement. */
2208
static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars)
485,032✔
2209
{
2210
  ExpDesc e;
485,032✔
2211
  checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX);
485,032✔
2212
  if (lex_opt(ls, ',')) {  /* Collect LHS list and recurse upwards. */
484,766✔
2213
    LHSVarList vl;
832✔
2214
    vl.prev = lh;
832✔
2215
    expr_primary(ls, &vl.v);
832✔
2216
    if (vl.v.k == VLOCAL)
831✔
2217
      assign_hazard(ls, lh, &vl.v);
465✔
2218
    checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names");
831✔
2219
    parse_assignment(ls, &vl, nvars+1);
831✔
2220
  } else {  /* Parse RHS. */
2221
    BCReg nexps;
483,934✔
2222
    lex_check(ls, '=');
483,934✔
2223
    nexps = expr_list(ls, &e);
480,389✔
2224
    if (nexps == nvars) {
478,944✔
2225
      if (e.k == VCALL) {
478,691✔
2226
        if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) {  /* Vararg assignment. */
6,968✔
2227
          ls->fs->freereg--;
14✔
2228
          e.k = VRELOCABLE;
14✔
2229
        } else {  /* Multiple call results. */
2230
          e.u.s.info = e.u.s.aux;  /* Base of call is not relocatable. */
6,954✔
2231
          e.k = VNONRELOC;
6,954✔
2232
        }
2233
      }
2234
      bcemit_store(ls->fs, &lh->v, &e);
478,691✔
2235
      return;
478,691✔
2236
    }
2237
    assign_adjust(ls, nvars, nexps, &e);
253✔
2238
  }
2239
  /* Assign RHS to LHS and recurse downwards. */
2240
  expr_init(&e, VNONRELOC, ls->fs->freereg-1);
959✔
2241
  bcemit_store(ls->fs, &lh->v, &e);
959✔
2242
}
2243

2244
/* Parse call statement or assignment. */
2245
static void parse_call_assign(LexState *ls)
530,018✔
2246
{
2247
  FuncState *fs = ls->fs;
530,018✔
2248
  LHSVarList vl;
530,018✔
2249
  expr_primary(ls, &vl.v);
530,018✔
2250
  if (vl.v.k == VCALL) {  /* Function call statement. */
526,033✔
2251
    setbc_b(bcptr(fs, &vl.v), 1);  /* No results. */
41,832✔
2252
  } else {  /* Start of an assignment. */
2253
    vl.prev = NULL;
484,201✔
2254
    parse_assignment(ls, &vl, 1);
484,201✔
2255
  }
2256
}
520,776✔
2257

2258
/* Parse 'local' statement. */
2259
static void parse_local(LexState *ls)
81,013✔
2260
{
2261
  if (lex_opt(ls, TK_function)) {  /* Local function declaration. */
81,013✔
2262
    ExpDesc v, b;
8,608✔
2263
    FuncState *fs = ls->fs;
8,608✔
2264
    var_new(ls, 0, lex_str(ls));
8,608✔
2265
    expr_init(&v, VLOCAL, fs->freereg);
8,608✔
2266
    v.u.s.aux = fs->varmap[fs->freereg];
8,608✔
2267
    bcreg_reserve(fs, 1);
8,608✔
2268
    var_add(ls, 1);
8,608✔
2269
    parse_body(ls, &b, 0, ls->linenumber);
8,608✔
2270
    /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */
2271
    expr_free(fs, &b);
8,608✔
2272
    expr_toreg(fs, &b, v.u.s.info);
8,608✔
2273
    /* The upvalue is in scope, but the local is only valid after the store. */
2274
    var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc;
8,608✔
2275
  } else {  /* Local variable declaration. */
2276
    ExpDesc e;
2277
    BCReg nexps, nvars = 0;
2278
    do {  /* Collect LHS. */
75,614✔
2279
      var_new(ls, nvars++, lex_str(ls));
75,614✔
2280
    } while (lex_opt(ls, ','));
75,601✔
2281
    if (lex_opt(ls, '=')) {  /* Optional RHS. */
72,392✔
2282
      nexps = expr_list(ls, &e);
54,636✔
2283
    } else {  /* Or implicitly set to nil. */
2284
      e.k = VVOID;
17,753✔
2285
      nexps = 0;
17,753✔
2286
    }
2287
    assign_adjust(ls, nvars, nexps, &e);
72,316✔
2288
    var_add(ls, nvars);
144,632✔
2289
  }
2290
}
80,924✔
2291

2292
/* Parse 'function' statement. */
2293
static void parse_func(LexState *ls, BCLine line)
9,137✔
2294
{
2295
  FuncState *fs;
9,137✔
2296
  ExpDesc v, b;
9,137✔
2297
  int needself = 0;
9,137✔
2298
  lj_lex_next(ls);  /* Skip 'function'. */
9,137✔
2299
  /* Parse function name. */
2300
  var_lookup(ls, &v);
9,137✔
2301
  while (ls->tok == '.')  /* Multiple dot-separated fields. */
9,482✔
2302
    expr_field(ls, &v);
553✔
2303
  if (ls->tok == ':') {  /* Optional colon to signify method call. */
8,929✔
2304
    needself = 1;
31✔
2305
    expr_field(ls, &v);
31✔
2306
  }
2307
  parse_body(ls, &b, needself, line);
8,929✔
2308
  fs = ls->fs;
7,397✔
2309
  bcemit_store(fs, &v, &b);
7,397✔
2310
  fs->bcbase[fs->pc - 1].line = line;  /* Set line for the store. */
7,397✔
2311
}
7,397✔
2312

2313
/* -- Control transfer statements ----------------------------------------- */
2314

2315
/* Check for end of block. */
2316
static int parse_isend(LexToken tok)
1,011,971✔
2317
{
2318
  switch (tok) {
1,011,971✔
2319
  case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof:
2320
    return 1;
2321
  default:
2322
    return 0;
867,077✔
2323
  }
2324
}
2325

2326
/* Parse 'return' statement. */
2327
static void parse_return(LexState *ls)
39,826✔
2328
{
2329
  BCIns ins;
39,826✔
2330
  FuncState *fs = ls->fs;
39,826✔
2331
  lj_lex_next(ls);  /* Skip 'return'. */
39,826✔
2332
  fs->flags |= PROTO_HAS_RETURN;
39,814✔
2333
  if (parse_isend(ls->tok) || ls->tok == ';') {  /* Bare return. */
39,814✔
2334
    ins = BCINS_AD(BC_RET0, 0, 1);
2335
  } else {  /* Return with one or more values. */
2336
    ExpDesc e;  /* Receives the _last_ expression in the list. */
39,443✔
2337
    BCReg nret = expr_list(ls, &e);
39,443✔
2338
    if (nret == 1) {  /* Return one result. */
35,273✔
2339
      if (e.k == VCALL) {  /* Check for tail call. */
35,005✔
2340
        BCIns *ip = bcptr(fs, &e);
6,744✔
2341
        /* It doesn't pay off to add BC_VARGT just for 'return ...'. */
2342
        if (bc_op(*ip) == BC_VARG) goto notailcall;
6,744✔
2343
        fs->pc--;
6,738✔
2344
        ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip));
6,738✔
2345
      } else {  /* Can return the result from any register. */
2346
        ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2);
28,261✔
2347
      }
2348
    } else {
2349
      if (e.k == VCALL) {  /* Append all results from a call. */
268✔
2350
      notailcall:
59✔
2351
        setbc_b(bcptr(fs, &e), 0);
65✔
2352
        ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar);
65✔
2353
      } else {
2354
        expr_tonextreg(fs, &e);  /* Force contiguous registers. */
209✔
2355
        ins = BCINS_AD(BC_RET, fs->nactvar, nret+1);
209✔
2356
      }
2357
    }
2358
  }
2359
  if (fs->flags & PROTO_CHILD)
35,644✔
2360
    bcemit_AJ(fs, BC_UCLO, 0, 0);  /* May need to close upvalues first. */
2,654✔
2361
  bcemit_INS(fs, ins);
35,644✔
2362
}
35,644✔
2363

2364
/* Parse 'break' statement. */
2365
static void parse_break(LexState *ls)
49,590✔
2366
{
2367
  ls->fs->bl->flags |= FSCOPE_BREAK;
49,590✔
2368
  gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs));
49,590✔
2369
}
49,590✔
2370

2371
/* Parse 'goto' statement. */
2372
static void parse_goto(LexState *ls)
30✔
2373
{
2374
  FuncState *fs = ls->fs;
30✔
2375
  GCstr *name = lex_str(ls);
30✔
2376
  VarInfo *vl = gola_findlabel(ls, name);
30✔
2377
  if (vl)  /* Treat backwards goto within same scope like a loop. */
30✔
2378
    bcemit_AJ(fs, BC_LOOP, vl->slot, -1);  /* No BC range check. */
3✔
2379
  fs->bl->flags |= FSCOPE_GOLA;
30✔
2380
  gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs));
30✔
2381
}
30✔
2382

2383
/* Parse label. */
2384
static void parse_label(LexState *ls)
38✔
2385
{
2386
  FuncState *fs = ls->fs;
38✔
2387
  GCstr *name;
38✔
2388
  MSize idx;
38✔
2389
  fs->lasttarget = fs->pc;
38✔
2390
  fs->bl->flags |= FSCOPE_GOLA;
38✔
2391
  lj_lex_next(ls);  /* Skip '::'. */
38✔
2392
  name = lex_str(ls);
38✔
2393
  if (gola_findlabel(ls, name))
76✔
2394
    lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name));
3✔
2395
  idx = gola_new(ls, name, VSTACK_LABEL, fs->pc);
35✔
2396
  lex_check(ls, TK_label);
35✔
2397
  /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */
2398
  for (;;) {
39✔
2399
    if (ls->tok == TK_label) {
37✔
2400
      synlevel_begin(ls);
3✔
2401
      parse_label(ls);
3✔
2402
      synlevel_end(ls);
2✔
2403
    } else if (LJ_52 && ls->tok == ';') {
34✔
2404
      lj_lex_next(ls);
2405
    } else {
2406
      break;
2407
    }
2408
  }
2409
  /* Trailing label is considered to be outside of scope. */
2410
  if (parse_isend(ls->tok) && ls->tok != TK_until)
34✔
2411
    ls->vstack[idx].slot = fs->bl->nactvar;
13✔
2412
  gola_resolve(ls, fs->bl, idx);
34✔
2413
}
30✔
2414

2415
/* -- Blocks, loops and conditional statements ---------------------------- */
2416

2417
/* Parse a block. */
2418
static void parse_block(LexState *ls)
137,711✔
2419
{
2420
  FuncState *fs = ls->fs;
137,711✔
2421
  FuncScope bl;
137,711✔
2422
  fscope_begin(fs, &bl, 0);
137,711✔
2423
  parse_chunk(ls);
137,711✔
2424
  fscope_end(fs);
136,969✔
2425
}
136,969✔
2426

2427
/* Parse 'while' statement. */
2428
static void parse_while(LexState *ls, BCLine line)
33,442✔
2429
{
2430
  FuncState *fs = ls->fs;
33,442✔
2431
  BCPos start, loop, condexit;
33,442✔
2432
  FuncScope bl;
33,442✔
2433
  lj_lex_next(ls);  /* Skip 'while'. */
33,442✔
2434
  start = fs->lasttarget = fs->pc;
33,442✔
2435
  condexit = expr_cond(ls);
33,442✔
2436
  fscope_begin(fs, &bl, FSCOPE_LOOP);
33,441✔
2437
  lex_check(ls, TK_do);
33,441✔
2438
  loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
33,440✔
2439
  parse_block(ls);
33,440✔
2440
  jmp_patch(fs, bcemit_jmp(fs), start);
33,241✔
2441
  lex_match(ls, TK_end, TK_while, line);
33,241✔
2442
  fscope_end(fs);
33,239✔
2443
  jmp_tohere(fs, condexit);
33,239✔
2444
  jmp_patchins(fs, loop, fs->pc);
33,239✔
2445
}
33,239✔
2446

2447
/* Parse 'repeat' statement. */
2448
static void parse_repeat(LexState *ls, BCLine line)
16,461✔
2449
{
2450
  FuncState *fs = ls->fs;
16,461✔
2451
  BCPos loop = fs->lasttarget = fs->pc;
16,461✔
2452
  BCPos condexit;
16,461✔
2453
  FuncScope bl1, bl2;
16,461✔
2454
  fscope_begin(fs, &bl1, FSCOPE_LOOP);  /* Breakable loop scope. */
16,461✔
2455
  fscope_begin(fs, &bl2, 0);  /* Inner scope. */
16,461✔
2456
  lj_lex_next(ls);  /* Skip 'repeat'. */
16,461✔
2457
  bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
16,461✔
2458
  parse_chunk(ls);
16,461✔
2459
  lex_match(ls, TK_until, TK_repeat, line);
16,460✔
2460
  condexit = expr_cond(ls);  /* Parse condition (still inside inner scope). */
16,459✔
2461
  if (!(bl2.flags & FSCOPE_UPVAL)) {  /* No upvalues? Just end inner scope. */
16,459✔
2462
    fscope_end(fs);
16,455✔
2463
  } else {  /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */
2464
    parse_break(ls);  /* Break from loop and close upvalues. */
4✔
2465
    jmp_tohere(fs, condexit);
4✔
2466
    fscope_end(fs);  /* End inner scope and close upvalues. */
4✔
2467
    condexit = bcemit_jmp(fs);
4✔
2468
  }
2469
  jmp_patch(fs, condexit, loop);  /* Jump backwards if !cond. */
16,459✔
2470
  jmp_patchins(fs, loop, fs->pc);
16,459✔
2471
  fscope_end(fs);  /* End loop scope. */
16,459✔
2472
}
16,459✔
2473

2474
/* Parse numeric 'for'. */
2475
static void parse_for_num(LexState *ls, GCstr *varname, BCLine line)
6,008✔
2476
{
2477
  FuncState *fs = ls->fs;
6,008✔
2478
  BCReg base = fs->freereg;
6,008✔
2479
  FuncScope bl;
6,008✔
2480
  BCPos loop, loopend;
6,008✔
2481
  /* Hidden control variables. */
2482
  var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);
6,008✔
2483
  var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);
6,008✔
2484
  var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);
6,008✔
2485
  /* Visible copy of index variable. */
2486
  var_new(ls, FORL_EXT, varname);
6,008✔
2487
  lex_check(ls, '=');
6,008✔
2488
  expr_next(ls);
6,008✔
2489
  lex_check(ls, ',');
6,008✔
2490
  expr_next(ls);
6,008✔
2491
  if (lex_opt(ls, ',')) {
6,008✔
2492
    expr_next(ls);
101✔
2493
  } else {
2494
    bcemit_AD(fs, BC_KSHORT, fs->freereg, 1);  /* Default step is 1. */
5,907✔
2495
    bcreg_reserve(fs, 1);
5,907✔
2496
  }
2497
  var_add(ls, 3);  /* Hidden control variables. */
6,008✔
2498
  lex_check(ls, TK_do);
6,008✔
2499
  loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP);
6,007✔
2500
  fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
6,007✔
2501
  var_add(ls, 1);
6,007✔
2502
  bcreg_reserve(fs, 1);
6,007✔
2503
  parse_block(ls);
6,007✔
2504
  fscope_end(fs);
6,007✔
2505
  /* Perform loop inversion. Loop control instructions are at the end. */
2506
  loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP);
6,007✔
2507
  fs->bcbase[loopend].line = line;  /* Fix line for control ins. */
6,007✔
2508
  jmp_patchins(fs, loopend, loop+1);
6,007✔
2509
  jmp_patchins(fs, loop, fs->pc);
6,007✔
2510
}
6,007✔
2511

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

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

2587
/* Parse 'for' statement. */
2588
static void parse_for(LexState *ls, BCLine line)
7,697✔
2589
{
2590
  FuncState *fs = ls->fs;
7,697✔
2591
  GCstr *varname;
7,697✔
2592
  FuncScope bl;
7,697✔
2593
  fscope_begin(fs, &bl, FSCOPE_LOOP);
7,697✔
2594
  lj_lex_next(ls);  /* Skip 'for'. */
7,697✔
2595
  varname = lex_str(ls);  /* Get first variable name. */
7,697✔
2596
  if (ls->tok == '=')
7,697✔
2597
    parse_for_num(ls, varname, line);
6,008✔
2598
  else if (ls->tok == ',' || ls->tok == TK_in)
1,689✔
2599
    parse_for_iter(ls, varname);
1,688✔
2600
  else
2601
    err_syntax(ls, LJ_ERR_XFOR);
1✔
2602
  lex_match(ls, TK_end, TK_for, line);
7,695✔
2603
  fscope_end(fs);  /* Resolve break list. */
7,694✔
2604
}
7,694✔
2605

2606
/* Parse condition and 'then' block. */
2607
static BCPos parse_then(LexState *ls)
60,629✔
2608
{
2609
  BCPos condexit;
60,629✔
2610
  lj_lex_next(ls);  /* Skip 'if' or 'elseif'. */
60,629✔
2611
  condexit = expr_cond(ls);
60,629✔
2612
  lex_check(ls, TK_then);
60,624✔
2613
  parse_block(ls);
60,619✔
2614
  return condexit;
60,617✔
2615
}
2616

2617
/* Parse 'if' statement. */
2618
static void parse_if(LexState *ls, BCLine line)
59,616✔
2619
{
2620
  FuncState *fs = ls->fs;
59,616✔
2621
  BCPos flist;
59,616✔
2622
  BCPos escapelist = NO_JMP;
59,616✔
2623
  flist = parse_then(ls);
59,616✔
2624
  while (ls->tok == TK_elseif) {  /* Parse multiple 'elseif' blocks. */
60,629✔
2625
    jmp_append(fs, &escapelist, bcemit_jmp(fs));
1,013✔
2626
    jmp_tohere(fs, flist);
1,013✔
2627
    flist = parse_then(ls);
1,013✔
2628
  }
2629
  if (ls->tok == TK_else) {  /* Parse optional 'else' block. */
59,604✔
2630
    jmp_append(fs, &escapelist, bcemit_jmp(fs));
35,187✔
2631
    jmp_tohere(fs, flist);
35,187✔
2632
    lj_lex_next(ls);  /* Skip 'else'. */
35,187✔
2633
    parse_block(ls);
35,187✔
2634
  } else {
2635
    jmp_append(fs, &escapelist, flist);
24,417✔
2636
  }
2637
  jmp_tohere(fs, escapelist);
59,406✔
2638
  lex_match(ls, TK_end, TK_if, line);
59,406✔
2639
}
59,388✔
2640

2641
/* -- Parse statements ---------------------------------------------------- */
2642

2643
/* Parse a statement. Returns 1 if it must be the last one in a chunk. */
2644
static int parse_stmt(LexState *ls)
827,631✔
2645
{
2646
  BCLine line = ls->linenumber;
827,631✔
2647
  switch (ls->tok) {
827,631✔
2648
  case TK_if:
59,616✔
2649
    parse_if(ls, line);
59,616✔
2650
    break;
59,616✔
2651
  case TK_while:
33,442✔
2652
    parse_while(ls, line);
33,442✔
2653
    break;
33,442✔
2654
  case TK_do:
770✔
2655
    lj_lex_next(ls);
770✔
2656
    parse_block(ls);
770✔
2657
    lex_match(ls, TK_end, TK_do, line);
427✔
2658
    break;
427✔
2659
  case TK_for:
7,697✔
2660
    parse_for(ls, line);
7,697✔
2661
    break;
7,697✔
2662
  case TK_repeat:
16,461✔
2663
    parse_repeat(ls, line);
16,461✔
2664
    break;
16,461✔
2665
  case TK_function:
9,137✔
2666
    parse_func(ls, line);
9,137✔
2667
    break;
9,137✔
2668
  case TK_local:
81,013✔
2669
    lj_lex_next(ls);
81,013✔
2670
    parse_local(ls);
81,013✔
2671
    break;
81,013✔
2672
  case TK_return:
39,826✔
2673
    parse_return(ls);
39,826✔
2674
    return 1;  /* Must be last. */
39,826✔
2675
  case TK_break:
49,586✔
2676
    lj_lex_next(ls);
49,586✔
2677
    parse_break(ls);
49,586✔
2678
    return !LJ_52;  /* Must be last in Lua 5.1. */
49,586✔
2679
#if LJ_52
2680
  case ';':
2681
    lj_lex_next(ls);
2682
    break;
2683
#endif
2684
  case TK_label:
35✔
2685
    parse_label(ls);
35✔
2686
    break;
35✔
2687
  case TK_goto:
31✔
2688
    if (LJ_52 || lj_lex_lookahead(ls) == TK_name) {
31✔
2689
      lj_lex_next(ls);
30✔
2690
      parse_goto(ls);
30✔
2691
      break;
30✔
2692
    }
2693
    /* fallthrough */
2694
  default:
2695
    parse_call_assign(ls);
530,018✔
2696
    break;
530,018✔
2697
  }
2698
  return 0;
2699
}
2700

2701
/* A chunk is a list of statements optionally separated by semicolons. */
2702
static void parse_chunk(LexState *ls)
245,774✔
2703
{
2704
  int islast = 0;
245,774✔
2705
  synlevel_begin(ls);
245,774✔
2706
  while (!islast && !parse_isend(ls->tok)) {
1,057,353✔
2707
    islast = parse_stmt(ls);
827,631✔
2708
    lex_opt(ls, ';');
811,581✔
2709
    lj_assertLS(ls->fs->framesize >= ls->fs->freereg &&
811,581✔
2710
                ls->fs->freereg >= ls->fs->nactvar,
2711
                "bad regalloc");
2712
    ls->fs->freereg = ls->fs->nactvar;  /* Free registers after each stmt. */
811,581✔
2713
  }
2714
  synlevel_end(ls);
229,722✔
2715
}
229,722✔
2716

2717
/* Entry point of bytecode parser. */
2718
GCproto *lj_parse(LexState *ls)
68,193✔
2719
{
2720
  FuncState fs;
68,193✔
2721
  FuncScope bl;
68,193✔
2722
  GCproto *pt;
68,193✔
2723
  lua_State *L = ls->L;
68,193✔
2724
#ifdef LUAJIT_DISABLE_DEBUGINFO
2725
  ls->chunkname = lj_str_newlit(L, "=");
2726
#else
2727
  ls->chunkname = lj_str_newz(L, ls->chunkarg);
68,193✔
2728
#endif
2729
  setstrV(L, L->top, ls->chunkname);  /* Anchor chunkname string. */
68,193✔
2730
  incr_top(L);
68,193✔
2731
  ls->level = 0;
68,193✔
2732
  fs_init(ls, &fs);
68,193✔
2733
  fs.linedefined = 0;
68,192✔
2734
  fs.numparams = 0;
68,192✔
2735
  fs.bcbase = NULL;
68,192✔
2736
  fs.bclim = 0;
68,192✔
2737
  fs.flags |= PROTO_VARARG;  /* Main chunk is always a vararg func. */
68,192✔
2738
  fscope_begin(&fs, &bl, 0);
68,192✔
2739
  bcemit_AD(&fs, BC_FUNCV, 0, 0);  /* Placeholder. */
68,192✔
2740
  lj_lex_next(ls);  /* Read-ahead first token. */
68,192✔
2741
  parse_chunk(ls);
68,183✔
2742
  if (ls->tok != TK_eof)
55,835✔
2743
    err_token(ls, TK_eof);
348✔
2744
  pt = fs_finish(ls, ls->linenumber);
55,487✔
2745
  L->top--;  /* Drop chunkname. */
55,478✔
2746
  lj_assertL(fs.prev == NULL && ls->fs == NULL, "mismatched frame nesting");
55,478✔
2747
  lj_assertL(pt->sizeuv == 0, "toplevel proto has upvalues");
55,478✔
2748
  return pt;
55,478✔
2749
}
2750

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