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

tarantool / luajit / 8262096822

13 Mar 2024 09:08AM UTC coverage: 92.669% (+0.04%) from 92.629%
8262096822

push

github

ligurio
ci: bump version of actions/checkout

Bump version of actions/checkout to v4.
Bump fixes an annoying warning that appears in the Github WebUI:

| Node.js 16 actions are deprecated. Please update the following actions
| to use Node.js 20: actions/checkout@v3. For more information see:
| https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/.

5661 of 6016 branches covered (94.1%)

Branch coverage included in aggregate %.

21607 of 23409 relevant lines covered (92.3%)

2816109.52 hits per line

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

96.79
/src/lj_snap.c
1
/*
2
** Snapshot handling.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
*/
5

6
#define lj_snap_c
7
#define LUA_CORE
8

9
#include "lj_obj.h"
10

11
#if LJ_HASJIT
12

13
#include "lj_gc.h"
14
#include "lj_tab.h"
15
#include "lj_state.h"
16
#include "lj_frame.h"
17
#include "lj_bc.h"
18
#include "lj_ir.h"
19
#include "lj_jit.h"
20
#include "lj_iropt.h"
21
#include "lj_trace.h"
22
#include "lj_snap.h"
23
#include "lj_target.h"
24
#if LJ_HASFFI
25
#include "lj_ctype.h"
26
#include "lj_cdata.h"
27
#endif
28

29
/* Pass IR on to next optimization in chain (FOLD). */
30
#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
31

32
/* Emit raw IR without passing through optimizations. */
33
#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))
34

35
/* -- Snapshot buffer allocation ------------------------------------------ */
36

37
/* Grow snapshot buffer. */
38
void lj_snap_grow_buf_(jit_State *J, MSize need)
160✔
39
{
40
  MSize maxsnap = (MSize)J->param[JIT_P_maxsnap];
160✔
41
  if (need > maxsnap)
160✔
42
    lj_trace_err(J, LJ_TRERR_SNAPOV);
×
43
  lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot);
160✔
44
  J->cur.snap = J->snapbuf;
160✔
45
}
160✔
46

47
/* Grow snapshot map buffer. */
48
void lj_snap_grow_map_(jit_State *J, MSize need)
160✔
49
{
50
  if (need < 2*J->sizesnapmap)
160✔
51
    need = 2*J->sizesnapmap;
52
  else if (need < 64)
110✔
53
    need = 64;
54
  J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf,
320✔
55
                    J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry));
160✔
56
  J->cur.snapmap = J->snapmapbuf;
160✔
57
  J->sizesnapmap = need;
160✔
58
}
160✔
59

60
/* -- Snapshot generation ------------------------------------------------- */
61

62
/* Add all modified slots to the snapshot. */
63
static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots)
95,579✔
64
{
65
  IRRef retf = J->chain[IR_RETF];  /* Limits SLOAD restore elimination. */
95,579✔
66
  BCReg s;
95,579✔
67
  MSize n = 0;
95,579✔
68
  for (s = 0; s < nslots; s++) {
1,048,788✔
69
    TRef tr = J->slot[s];
953,209✔
70
    IRRef ref = tref_ref(tr);
953,209✔
71
#if LJ_FR2
72
    if (s == 1) {  /* Ignore slot 1 in LJ_FR2 mode, except if tailcalled. */
953,209✔
73
      if ((tr & TREF_FRAME))
95,579✔
74
        map[n++] = SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL);
18,095✔
75
      continue;
95,579✔
76
    }
77
    if ((tr & (TREF_FRAME | TREF_CONT)) && !ref) {
857,630✔
78
      cTValue *base = J->L->base - J->baseslot;
10,485✔
79
      tr = J->slot[s] = (tr & 0xff0000) | lj_ir_k64(J, IR_KNUM, base[s].u64);
10,485✔
80
      ref = tref_ref(tr);
10,485✔
81
    }
82
#endif
83
    if (ref) {
857,630✔
84
      SnapEntry sn = SNAP_TR(s, tr);
479,765✔
85
      IRIns *ir = &J->cur.ir[ref];
479,765✔
86
      if ((LJ_FR2 || !(sn & (SNAP_CONT|SNAP_FRAME))) &&
479,765✔
87
          ir->o == IR_SLOAD && ir->op1 == s && ref > retf) {
479,765✔
88
        /*
89
        ** No need to snapshot unmodified non-inherited slots.
90
        ** But always snapshot the function below a frame in LJ_FR2 mode.
91
        */
92
        if (!(ir->op2 & IRSLOAD_INHERIT) &&
255,407✔
93
            (!LJ_FR2 || s == 0 || s+1 == nslots ||
198,258✔
94
             !(J->slot[s+1] & (TREF_CONT|TREF_FRAME))))
195,079✔
95
          continue;
200,868✔
96
        /* No need to restore readonly slots and unmodified non-parent slots. */
97
        if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) &&
54,539✔
98
            (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT)
99
          sn |= SNAP_NORESTORE;
41,868✔
100
      }
101
      if (LJ_SOFTFP32 && irt_isnum(ir->t))
278,897✔
102
        sn |= SNAP_SOFTFPNUM;
103
      map[n++] = sn;
278,897✔
104
    }
105
  }
106
  return n;
95,579✔
107
}
108

109
/* Add frame links at the end of the snapshot. */
110
static MSize snapshot_framelinks(jit_State *J, SnapEntry *map, uint8_t *topslot)
95,579✔
111
{
112
  cTValue *frame = J->L->base - 1;
95,579✔
113
  cTValue *lim = J->L->base - J->baseslot + LJ_FR2;
95,579✔
114
  GCfunc *fn = frame_func(frame);
95,579✔
115
  cTValue *ftop = isluafunc(fn) ? (frame+funcproto(fn)->framesize) : J->L->top;
95,579✔
116
#if LJ_FR2
117
  uint64_t pcbase = (u64ptr(J->pc) << 8) | (J->baseslot - 2);
95,579✔
118
  lj_assertJ(2 <= J->baseslot && J->baseslot <= 257, "bad baseslot");
95,579✔
119
  memcpy(map, &pcbase, sizeof(uint64_t));
95,579✔
120
#else
121
  MSize f = 0;
122
  map[f++] = SNAP_MKPC(J->pc);  /* The current PC is always the first entry. */
123
  lj_assertJ(!J->pt ||
124
             (J->pc >= proto_bc(J->pt) &&
125
              J->pc < proto_bc(J->pt) + J->pt->sizebc), "bad snapshot PC");
126
#endif
127
  while (frame > lim) {  /* Backwards traversal of all frames above base. */
130,898✔
128
    if (frame_islua(frame)) {
35,319✔
129
#if !LJ_FR2
130
      map[f++] = SNAP_MKPC(frame_pc(frame));
131
#endif
132
      frame = frame_prevl(frame);
34,488✔
133
    } else if (frame_iscont(frame)) {
831✔
134
#if !LJ_FR2
135
      map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
136
      map[f++] = SNAP_MKPC(frame_contpc(frame));
137
#endif
138
      frame = frame_prevd(frame);
278✔
139
    } else {
140
      lj_assertJ(!frame_isc(frame), "broken frame chain");
553✔
141
#if !LJ_FR2
142
      map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
143
#endif
144
      frame = frame_prevd(frame);
553✔
145
      continue;
553✔
146
    }
147
    if (frame + funcproto(frame_func(frame))->framesize > ftop)
34,766✔
148
      ftop = frame + funcproto(frame_func(frame))->framesize;
149
  }
150
  *topslot = (uint8_t)(ftop - lim);
95,579✔
151
#if LJ_FR2
152
  lj_assertJ(sizeof(SnapEntry) * 2 == sizeof(uint64_t), "bad SnapEntry def");
95,579✔
153
  return 2;
95,579✔
154
#else
155
  lj_assertJ(f == (MSize)(1 + J->framedepth), "miscalculated snapshot size");
156
  return f;
157
#endif
158
}
159

160
/* Take a snapshot of the current stack. */
161
static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap)
95,579✔
162
{
163
  BCReg nslots = J->baseslot + J->maxslot;
95,579✔
164
  MSize nent;
95,579✔
165
  SnapEntry *p;
95,579✔
166
  /* Conservative estimate. */
167
  lj_snap_grow_map(J, nsnapmap + nslots + (MSize)(LJ_FR2?2:J->framedepth+1));
95,579✔
168
  p = &J->cur.snapmap[nsnapmap];
95,579✔
169
  nent = snapshot_slots(J, p, nslots);
95,579✔
170
  snap->nent = (uint8_t)nent;
95,579✔
171
  nent += snapshot_framelinks(J, p + nent, &snap->topslot);
95,579✔
172
  snap->mapofs = (uint32_t)nsnapmap;
95,579✔
173
  snap->ref = (IRRef1)J->cur.nins;
95,579✔
174
  snap->mcofs = 0;
95,579✔
175
  snap->nslots = (uint8_t)nslots;
95,579✔
176
  snap->count = 0;
95,579✔
177
  J->cur.nsnapmap = (uint32_t)(nsnapmap + nent);
95,579✔
178
}
95,579✔
179

180
/* Add or merge a snapshot. */
181
void lj_snap_add(jit_State *J)
95,579✔
182
{
183
  MSize nsnap = J->cur.nsnap;
95,579✔
184
  MSize nsnapmap = J->cur.nsnapmap;
95,579✔
185
  /* Merge if no ins. inbetween or if requested and no guard inbetween. */
186
  if ((nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins) ||
95,579✔
187
      (J->mergesnap && !irt_isguard(J->guardemit))) {
81,599✔
188
    if (nsnap == 1) {  /* But preserve snap #0 PC. */
18,019✔
189
      emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0);
532✔
190
      goto nomerge;
532✔
191
    }
192
    nsnapmap = J->cur.snap[--nsnap].mapofs;
17,487✔
193
  } else {
194
  nomerge:
77,560✔
195
    lj_snap_grow_buf(J, nsnap+1);
78,092✔
196
    J->cur.nsnap = (uint16_t)(nsnap+1);
78,092✔
197
  }
198
  J->mergesnap = 0;
95,579✔
199
  J->guardemit.irt = 0;
95,579✔
200
  snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap);
95,579✔
201
}
95,579✔
202

203
/* -- Snapshot modification ----------------------------------------------- */
204

205
#define SNAP_USEDEF_SLOTS        (LJ_MAX_JSLOTS+LJ_STACK_EXTRA)
206

207
/* Find unused slots with reaching-definitions bytecode data-flow analysis. */
208
static BCReg snap_usedef(jit_State *J, uint8_t *udf,
209
                         const BCIns *pc, BCReg maxslot)
210
{
211
  BCReg s;
212
  GCobj *o;
213

214
  if (maxslot == 0) return 0;
215
#ifdef LUAJIT_USE_VALGRIND
216
  /* Avoid errors for harmless reads beyond maxslot. */
217
  memset(udf, 1, SNAP_USEDEF_SLOTS);
218
#else
219
  memset(udf, 1, maxslot);
220
#endif
221

222
  /* Treat open upvalues as used. */
223
  o = gcref(J->L->openupval);
224
  while (o) {
225
    if (uvval(gco2uv(o)) < J->L->base) break;
226
    udf[uvval(gco2uv(o)) - J->L->base] = 0;
227
    o = gcref(o->gch.nextgc);
228
  }
229

230
#define USE_SLOT(s)                udf[(s)] &= ~1
231
#define DEF_SLOT(s)                udf[(s)] *= 3
232

233
  /* Scan through following bytecode and check for uses/defs. */
234
  lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc,
235
             "snapshot PC out of range");
236
  for (;;) {
237
    BCIns ins = *pc++;
238
    BCOp op = bc_op(ins);
239
    switch (bcmode_b(op)) {
240
    case BCMvar: USE_SLOT(bc_b(ins)); break;
241
    default: break;
242
    }
243
    switch (bcmode_c(op)) {
244
    case BCMvar: USE_SLOT(bc_c(ins)); break;
245
    case BCMrbase:
246
      lj_assertJ(op == BC_CAT, "unhandled op %d with RC rbase", op);
247
      for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s);
248
      for (; s < maxslot; s++) DEF_SLOT(s);
249
      break;
250
    case BCMjump:
251
    handle_jump: {
252
      BCReg minslot = bc_a(ins);
253
      if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT;
254
      else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1;
255
      else if (op == BC_UCLO) { pc += bc_j(ins); break; }
256
      for (s = minslot; s < maxslot; s++) DEF_SLOT(s);
257
      return minslot < maxslot ? minslot : maxslot;
258
      }
259
    case BCMlit:
260
      if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
261
        goto handle_jump;
262
      } else if (bc_isret(op)) {
263
        BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1);
264
        for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);
265
        for (; s < top; s++) USE_SLOT(s);
266
        for (; s < maxslot; s++) DEF_SLOT(s);
267
        return 0;
268
      }
269
      break;
270
    case BCMfunc: return maxslot;  /* NYI: will abort, anyway. */
271
    default: break;
272
    }
273
    switch (bcmode_a(op)) {
274
    case BCMvar: USE_SLOT(bc_a(ins)); break;
275
    case BCMdst:
276
       if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins));
277
       break;
278
    case BCMbase:
279
      if (op >= BC_CALLM && op <= BC_ITERN) {
280
        BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ?
281
                    maxslot : (bc_a(ins) + bc_c(ins)+LJ_FR2);
282
        if (LJ_FR2) DEF_SLOT(bc_a(ins)+1);
283
        s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0);
284
        for (; s < top; s++) USE_SLOT(s);
285
        for (; s < maxslot; s++) DEF_SLOT(s);
286
        if (op == BC_CALLT || op == BC_CALLMT) {
287
          for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);
288
          return 0;
289
        }
290
      } else if (op == BC_VARG) {
291
        return maxslot;  /* NYI: punt. */
292
      } else if (op == BC_KNIL) {
293
        for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s);
294
      } else if (op == BC_TSETM) {
295
        for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s);
296
      }
297
      break;
298
    default: break;
299
    }
300
    lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc,
301
               "use/def analysis PC out of range");
302
  }
303

304
#undef USE_SLOT
305
#undef DEF_SLOT
306

307
  return 0;  /* unreachable */
308
}
309

310
/* Mark slots used by upvalues of child prototypes as used. */
311
void snap_useuv(GCproto *pt, uint8_t *udf)
68,133✔
312
{
313
  /* This is a coarse check, because it's difficult to correlate the lifetime
314
  ** of slots and closures. But the number of false positives is quite low.
315
  ** A false positive may cause a slot not to be purged, which is just
316
  ** a missed optimization.
317
  */
318
  if ((pt->flags & PROTO_CHILD)) {
68,133✔
319
    ptrdiff_t i, j, n = pt->sizekgc;
238✔
320
    GCRef *kr = mref(pt->k, GCRef) - 1;
238✔
321
    for (i = 0; i < n; i++, kr--) {
6,504✔
322
      GCobj *o = gcref(*kr);
6,266✔
323
      if (o->gch.gct == ~LJ_TPROTO) {
6,266✔
324
        for (j = 0; j < gco2pt(o)->sizeuv; j++) {
1,016✔
325
          uint32_t v = proto_uv(gco2pt(o))[j];
418✔
326
          if ((v & PROTO_UV_LOCAL)) {
418✔
327
            udf[(v & 0xff)] = 0;
407✔
328
          }
329
        }
330
      }
331
    }
332
  }
333
}
68,133✔
334

335
/* Purge dead slots before the next snapshot. */
336
void lj_snap_purge(jit_State *J)
66,241✔
337
{
338
  uint8_t udf[SNAP_USEDEF_SLOTS];
66,241✔
339
  BCReg s, maxslot = J->maxslot;
66,241✔
340
  if (bc_op(*J->pc) == BC_FUNCV && maxslot > J->pt->numparams)
66,241✔
341
    maxslot = J->pt->numparams;
342
  s = snap_usedef(J, udf, J->pc, maxslot);
66,241✔
343
  if (s < maxslot) {
66,241✔
344
    snap_useuv(J->pt, udf);
60,797✔
345
    for (; s < maxslot; s++)
241,305✔
346
      if (udf[s] != 0)
119,711✔
347
        J->base[s] = 0;  /* Purge dead slots. */
103,713✔
348
  }
349
}
66,241✔
350

351
/* Shrink last snapshot. */
352
void lj_snap_shrink(jit_State *J)
11,402✔
353
{
354
  SnapShot *snap = &J->cur.snap[J->cur.nsnap-1];
11,402✔
355
  SnapEntry *map = &J->cur.snapmap[snap->mapofs];
11,402✔
356
  MSize n, m, nlim, nent = snap->nent;
11,402✔
357
  uint8_t udf[SNAP_USEDEF_SLOTS];
11,402✔
358
  BCReg maxslot = J->maxslot;
11,402✔
359
  BCReg baseslot = J->baseslot;
11,402✔
360
  BCReg minslot = snap_usedef(J, udf, snap_pc(&map[nent]), maxslot);
11,402✔
361
  if (minslot < maxslot) snap_useuv(J->pt, udf);
11,402✔
362
  maxslot += baseslot;
11,402✔
363
  minslot += baseslot;
11,402✔
364
  snap->nslots = (uint8_t)maxslot;
11,402✔
365
  for (n = m = 0; n < nent; n++) {  /* Remove unused slots from snapshot. */
89,713✔
366
    BCReg s = snap_slot(map[n]);
78,311✔
367
    if (s < minslot || (s < maxslot && udf[s-baseslot] == 0))
78,311✔
368
      map[m++] = map[n];  /* Only copy used slots. */
66,722✔
369
  }
370
  snap->nent = (uint8_t)m;
11,402✔
371
  nlim = J->cur.nsnapmap - snap->mapofs - 1;
11,402✔
372
  while (n <= nlim) map[m++] = map[n++];  /* Move PC + frame links down. */
34,206✔
373
  J->cur.nsnapmap = (uint32_t)(snap->mapofs + m);  /* Free up space in map. */
11,402✔
374
}
11,402✔
375

376
/* -- Snapshot access ----------------------------------------------------- */
377

378
/* Initialize a Bloom Filter with all renamed refs.
379
** There are very few renames (often none), so the filter has
380
** very few bits set. This makes it suitable for negative filtering.
381
*/
382
static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim)
55,289✔
383
{
384
  BloomFilter rfilt = 0;
55,289✔
385
  IRIns *ir;
55,289✔
386
  for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)
57,794✔
387
    if (ir->op2 <= lim)
2,505✔
388
      bloomset(rfilt, ir->op1);
1,425✔
389
  return rfilt;
52,111✔
390
}
391

392
/* Process matching renames to find the original RegSP. */
393
static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs)
968✔
394
{
395
  IRIns *ir;
968✔
396
  for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)
4,291✔
397
    if (ir->op1 == ref && ir->op2 <= lim)
3,323✔
398
      rs = ir->prev;
1,034✔
399
  return rs;
400
}
401

402
/* Copy RegSP from parent snapshot to the parent links of the IR. */
403
IRIns *lj_snap_regspmap(jit_State *J, GCtrace *T, SnapNo snapno, IRIns *ir)
3,178✔
404
{
405
  SnapShot *snap = &T->snap[snapno];
3,178✔
406
  SnapEntry *map = &T->snapmap[snap->mapofs];
3,178✔
407
  BloomFilter rfilt = snap_renamefilter(T, snapno);
3,178✔
408
  MSize n = 0;
409
  IRRef ref = 0;
9,214✔
410
  UNUSED(J);
9,214✔
411
  for ( ; ; ir++) {
15,250✔
412
    uint32_t rs;
9,214✔
413
    if (ir->o == IR_SLOAD) {
9,214✔
414
      if (!(ir->op2 & IRSLOAD_PARENT)) break;
6,096✔
415
      for ( ; ; n++) {
18,147✔
416
        lj_assertJ(n < snap->nent, "slot %d not found in snapshot", ir->op1);
6,817✔
417
        if (snap_slot(map[n]) == ir->op1) {
11,330✔
418
          ref = snap_ref(map[n++]);
4,513✔
419
          break;
4,513✔
420
        }
421
      }
422
    } else if (LJ_SOFTFP32 && ir->o == IR_HIOP) {
3,118✔
423
      ref++;
424
    } else if (ir->o == IR_PVAL) {
3,118✔
425
      ref = ir->op1 + REF_BIAS;
1,523✔
426
    } else {
427
      break;
428
    }
429
    rs = T->ir[ref].prev;
6,036✔
430
    if (bloomtest(rfilt, ref))
6,036✔
431
      rs = snap_renameref(T, snapno, ref, rs);
52✔
432
    ir->prev = (uint16_t)rs;
6,036✔
433
    lj_assertJ(regsp_used(rs), "unused IR %04d in snapshot", ref - REF_BIAS);
6,036✔
434
  }
435
  return ir;
3,178✔
436
}
437

438
/* -- Snapshot replay ----------------------------------------------------- */
439

440
/* Replay constant from parent trace. */
441
static TRef snap_replay_const(jit_State *J, IRIns *ir)
9,064✔
442
{
443
  /* Only have to deal with constants that can occur in stack slots. */
444
  switch ((IROp)ir->o) {
9,064✔
445
  case IR_KPRI: return TREF_PRI(irt_type(ir->t));
73✔
446
  case IR_KINT: return lj_ir_kint(J, ir->i);
3,198✔
447
  case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t));
3,861✔
448
  case IR_KNUM: case IR_KINT64:
1,932✔
449
    return lj_ir_k64(J, (IROp)ir->o, ir_k64(ir)->u64);
1,932✔
450
  case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir));  /* Continuation. */
×
451
  case IR_KNULL: return lj_ir_knull(J, irt_type(ir->t));
×
452
  default: lj_assertJ(0, "bad IR constant op %d", ir->o); return TREF_NIL;
453
  }
454
}
455

456
/* De-duplicate parent reference. */
457
static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref)
458
{
459
  MSize j;
460
  for (j = 0; j < nmax; j++)
9,614✔
461
    if (snap_ref(map[j]) == ref)
9,357✔
462
      return J->slot[snap_slot(map[j])] & ~(SNAP_CONT|SNAP_FRAME);
952✔
463
  return 0;
464
}
465

466
/* Emit parent reference with de-duplication. */
467
static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax,
468
                      BloomFilter seen, IRRef ref)
469
{
470
  IRIns *ir = &T->ir[ref];
471
  TRef tr;
472
  if (irref_isk(ref))
473
    tr = snap_replay_const(J, ir);
474
  else if (!regsp_used(ir->prev))
475
    tr = 0;
476
  else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0)
477
    tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0);
478
  return tr;
479
}
480

481
/* Check whether a sunk store corresponds to an allocation. Slow path. */
482
static int snap_sunk_store2(GCtrace *T, IRIns *ira, IRIns *irs)
483
{
484
  if (irs->o == IR_ASTORE || irs->o == IR_HSTORE ||
485
      irs->o == IR_FSTORE || irs->o == IR_XSTORE) {
486
    IRIns *irk = &T->ir[irs->op1];
487
    if (irk->o == IR_AREF || irk->o == IR_HREFK)
488
      irk = &T->ir[irk->op1];
489
    return (&T->ir[irk->op1] == ira);
490
  }
491
  return 0;
492
}
493

494
/* Check whether a sunk store corresponds to an allocation. Fast path. */
495
static LJ_AINLINE int snap_sunk_store(GCtrace *T, IRIns *ira, IRIns *irs)
86✔
496
{
497
  if (irs->s != 255)
86✔
498
    return (ira + irs->s == irs);  /* Fast check. */
86✔
499
  return snap_sunk_store2(T, ira, irs);
×
500
}
501

502
/* Replay snapshot state to setup side trace. */
503
void lj_snap_replay(jit_State *J, GCtrace *T)
3,034✔
504
{
505
  SnapShot *snap = &T->snap[J->exitno];
3,034✔
506
  SnapEntry *map = &T->snapmap[snap->mapofs];
3,034✔
507
  MSize n, nent = snap->nent;
3,034✔
508
  BloomFilter seen = 0;
3,034✔
509
  int pass23 = 0;
3,034✔
510
  J->framedepth = 0;
3,034✔
511
  /* Emit IR for slots inherited from parent snapshot. */
512
  for (n = 0; n < nent; n++) {
16,557✔
513
    SnapEntry sn = map[n];
13,523✔
514
    BCReg s = snap_slot(sn);
13,523✔
515
    IRRef ref = snap_ref(sn);
13,523✔
516
    IRIns *ir = &T->ir[ref];
13,523✔
517
    TRef tr;
13,523✔
518
    /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */
519
    if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0)
14,732✔
520
      goto setslot;
952✔
521
    bloomset(seen, ref);
12,571✔
522
    if (irref_isk(ref)) {
12,571✔
523
      /* See special treatment of LJ_FR2 slot 1 in snapshot_slots() above. */
524
      if (LJ_FR2 && (sn == SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL)))
6,291✔
525
        tr = 0;
526
      else
527
        tr = snap_replay_const(J, ir);
5,984✔
528
    } else if (!regsp_used(ir->prev)) {
6,280✔
529
      pass23 = 1;
530
      lj_assertJ(s != 0, "unused slot 0 in snapshot");
531
      tr = s;
532
    } else {
533
      IRType t = irt_type(ir->t);
4,757✔
534
      uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT;
4,757✔
535
      if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM;
4,757✔
536
      if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY);
4,757✔
537
      tr = emitir_raw(IRT(IR_SLOAD, t), s, mode);
4,757✔
538
    }
539
  setslot:
13,523✔
540
    J->slot[s] = tr | (sn&(SNAP_CONT|SNAP_FRAME));  /* Same as TREF_* flags. */
13,523✔
541
    J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && (s != LJ_FR2));
13,523✔
542
    if ((sn & SNAP_FRAME))
13,523✔
543
      J->baseslot = s+1;
2,305✔
544
  }
545
  if (pass23) {
3,034✔
546
    IRIns *irlast = &T->ir[snap->ref];
132✔
547
    pass23 = 0;
132✔
548
    /* Emit dependent PVALs. */
549
    for (n = 0; n < nent; n++) {
2,614✔
550
      SnapEntry sn = map[n];
2,482✔
551
      IRRef refp = snap_ref(sn);
2,482✔
552
      IRIns *ir = &T->ir[refp];
2,482✔
553
      if (regsp_reg(ir->r) == RID_SUNK) {
2,482✔
554
        uint8_t m;
1,521✔
555
        if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue;
1,521✔
556
        pass23 = 1;
1,521✔
557
        lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP ||
1,521✔
558
                   ir->o == IR_CNEW || ir->o == IR_CNEWI,
559
                   "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o);
560
        m = lj_ir_mode[ir->o];
1,521✔
561
        if (irm_op1(m) == IRMref) snap_pref(J, T, map, nent, seen, ir->op1);
1,521✔
562
        if (irm_op2(m) == IRMref) snap_pref(J, T, map, nent, seen, ir->op2);
1,521✔
563
        if (LJ_HASFFI && ir->o == IR_CNEWI) {
1,521✔
564
          if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP)
565
            snap_pref(J, T, map, nent, seen, (ir+1)->op2);
566
        } else {
567
          IRIns *irs;
14✔
568
          for (irs = ir+1; irs < irlast; irs++)
206✔
569
            if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
235✔
570
              if (snap_pref(J, T, map, nent, seen, irs->op2) == 0)
29✔
571
                snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1);
5✔
572
              else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) &&
573
                       irs+1 < irlast && (irs+1)->o == IR_HIOP)
574
                snap_pref(J, T, map, nent, seen, (irs+1)->op2);
575
            }
576
        }
577
      } else if (!irref_isk(refp) && !regsp_used(ir->prev)) {
961✔
578
        lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
2✔
579
                   "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o);
580
        J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1);
2✔
581
      }
582
    }
583
    /* Replay sunk instructions. */
584
    for (n = 0; pass23 && n < nent; n++) {
2,611✔
585
      SnapEntry sn = map[n];
2,479✔
586
      IRRef refp = snap_ref(sn);
2,479✔
587
      IRIns *ir = &T->ir[refp];
2,479✔
588
      if (regsp_reg(ir->r) == RID_SUNK) {
2,479✔
589
        TRef op1, op2;
1,521✔
590
        uint8_t m;
1,521✔
591
        if (J->slot[snap_slot(sn)] != snap_slot(sn)) {  /* De-dup allocs. */
1,521✔
592
          J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]];
×
593
          continue;
×
594
        }
595
        op1 = ir->op1;
1,521✔
596
        m = lj_ir_mode[ir->o];
1,521✔
597
        if (irm_op1(m) == IRMref) op1 = snap_pref(J, T, map, nent, seen, op1);
1,521✔
598
        op2 = ir->op2;
1,521✔
599
        if (irm_op2(m) == IRMref) op2 = snap_pref(J, T, map, nent, seen, op2);
1,521✔
600
        if (LJ_HASFFI && ir->o == IR_CNEWI) {
1,521✔
601
          if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) {
1,507✔
602
            lj_needsplit(J);  /* Emit joining HIOP. */
603
            op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2,
604
                             snap_pref(J, T, map, nent, seen, (ir+1)->op2));
605
          }
606
          J->slot[snap_slot(sn)] = emitir(ir->ot & ~(IRT_MARK|IRT_ISPHI), op1, op2);
1,507✔
607
        } else {
608
          IRIns *irs;
14✔
609
          TRef tr = emitir(ir->ot, op1, op2);
14✔
610
          J->slot[snap_slot(sn)] = tr;
14✔
611
          for (irs = ir+1; irs < irlast; irs++)
206✔
612
            if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
264✔
613
              IRIns *irr = &T->ir[irs->op1];
29✔
614
              TRef val, key = irr->op2, tmp = tr;
29✔
615
              if (irr->o != IR_FREF) {
29✔
616
                IRIns *irk = &T->ir[key];
28✔
617
                if (irr->o == IR_HREFK)
28✔
618
                  key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]),
2✔
619
                                    irk->op2);
2✔
620
                else
621
                  key = snap_replay_const(J, irk);
26✔
622
                if (irr->o == IR_HREFK || irr->o == IR_AREF) {
28✔
623
                  IRIns *irf = &T->ir[irr->op1];
8✔
624
                  tmp = emitir(irf->ot, tmp, irf->op2);
8✔
625
                } else if (irr->o == IR_NEWREF) {
20✔
626
                  IRRef allocref = tref_ref(tr);
12✔
627
                  IRRef keyref = tref_ref(key);
12✔
628
                  IRRef newref_ref = J->chain[IR_NEWREF];
12✔
629
                  IRIns *newref = &J->cur.ir[newref_ref];
12✔
630
                  lj_assertJ(irref_isk(keyref),
12✔
631
                             "sunk store for parent IR %04d with bad key %04d",
632
                             refp - REF_BIAS, keyref - REF_BIAS);
633
                  if (newref_ref > allocref && newref->op2 == keyref) {
12✔
634
                    lj_assertJ(newref->op1 == allocref,
6✔
635
                               "sunk store for parent IR %04d with bad tab %04d",
636
                               refp - REF_BIAS, allocref - REF_BIAS);
637
                    tmp = newref_ref;
6✔
638
                    goto skip_newref;
6✔
639
                  }
640
                }
641
              }
642
              tmp = emitir(irr->ot, tmp, key);
23✔
643
            skip_newref:
29✔
644
              val = snap_pref(J, T, map, nent, seen, irs->op2);
29✔
645
              if (val == 0) {
29✔
646
                IRIns *irc = &T->ir[irs->op2];
5✔
647
                lj_assertJ(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT,
5✔
648
                           "sunk store for parent IR %04d with bad op %d",
649
                           refp - REF_BIAS, irc->o);
650
                val = snap_pref(J, T, map, nent, seen, irc->op1);
5✔
651
                val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT);
5✔
652
              } else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) &&
653
                         irs+1 < irlast && (irs+1)->o == IR_HIOP) {
654
                IRType t = IRT_I64;
655
                if (LJ_SOFTFP32 && irt_type((irs+1)->t) == IRT_SOFTFP)
656
                  t = IRT_NUM;
657
                lj_needsplit(J);
658
                if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) {
659
                  uint64_t k = (uint32_t)T->ir[irs->op2].i +
660
                               ((uint64_t)T->ir[(irs+1)->op2].i << 32);
661
                  val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM, k);
662
                } else {
663
                  val = emitir_raw(IRT(IR_HIOP, t), val,
664
                          snap_pref(J, T, map, nent, seen, (irs+1)->op2));
665
                }
666
                tmp = emitir(IRT(irs->o, t), tmp, val);
667
                continue;
668
              }
669
              tmp = emitir(irs->ot, tmp, val);
29✔
670
            } else if (LJ_HASFFI && irs->o == IR_XBAR && ir->o == IR_CNEW) {
163✔
671
              emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
×
672
            }
673
        }
674
      }
675
    }
676
  }
677
  J->base = J->slot + J->baseslot;
3,034✔
678
  J->maxslot = snap->nslots - J->baseslot;
3,034✔
679
  lj_snap_add(J);
3,034✔
680
  if (pass23)  /* Need explicit GC step _after_ initial snapshot. */
3,034✔
681
    emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0);
131✔
682
}
3,034✔
683

684
/* -- Snapshot restore ---------------------------------------------------- */
685

686
static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,
687
                        SnapNo snapno, BloomFilter rfilt,
688
                        IRIns *ir, TValue *o);
689

690
/* Restore a value from the trace exit state. */
691
static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex,
133,101✔
692
                            SnapNo snapno, BloomFilter rfilt,
693
                            IRRef ref, TValue *o)
694
{
695
  IRIns *ir = &T->ir[ref];
133,170✔
696
  IRType1 t = ir->t;
133,170✔
697
  RegSP rs = ir->prev;
133,170✔
698
  if (irref_isk(ref)) {  /* Restore constant slot. */
133,170✔
699
    if (ir->o == IR_KPTR) {
78,485✔
700
      o->u64 = (uint64_t)(uintptr_t)ir_kptr(ir);
×
701
    } else {
702
      lj_assertJ(!(ir->o == IR_KKPTR || ir->o == IR_KNULL),
78,485✔
703
                 "restore of const from IR %04d with bad op %d",
704
                 ref - REF_BIAS, ir->o);
705
      lj_ir_kvalue(J->L, o, ir);
78,485✔
706
    }
707
    return;
78,485✔
708
  }
709
  if (LJ_UNLIKELY(bloomtest(rfilt, ref)))
54,685✔
710
    rs = snap_renameref(T, snapno, ref, rs);
916✔
711
  if (ra_hasspill(regsp_spill(rs))) {  /* Restore from spill slot. */
54,685✔
712
    int32_t *sps = &ex->spill[regsp_spill(rs)];
2,540✔
713
    if (irt_isinteger(t)) {
2,540✔
714
      setintV(o, *sps);
378✔
715
#if !LJ_SOFTFP32
716
    } else if (irt_isnum(t)) {
2,162✔
717
      o->u64 = *(uint64_t *)sps;
1,225✔
718
#endif
719
#if LJ_64 && !LJ_GC64
720
    } else if (irt_islightud(t)) {
721
      /* 64 bit lightuserdata which may escape already has the tag bits. */
722
      o->u64 = *(uint64_t *)sps;
723
#endif
724
    } else {
725
      lj_assertJ(!irt_ispri(t), "PRI ref with spill slot");
937✔
726
      setgcV(J->L, o, (GCobj *)(uintptr_t)*(GCSize *)sps, irt_toitype(t));
937✔
727
    }
728
  } else {  /* Restore from register. */
729
    Reg r = regsp_reg(rs);
52,145✔
730
    if (ra_noreg(r)) {
52,145✔
731
      lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
69✔
732
                 "restore from IR %04d has no reg", ref - REF_BIAS);
733
      snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o);
69✔
734
      if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o));
735
      return;
69✔
736
    } else if (irt_isinteger(t)) {
52,076✔
737
      setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]);
5,702✔
738
#if !LJ_SOFTFP
739
    } else if (irt_isnum(t)) {
46,374✔
740
      setnumV(o, ex->fpr[r-RID_MIN_FPR]);
15,181✔
741
#elif LJ_64  /* && LJ_SOFTFP */
742
    } else if (irt_isnum(t)) {
743
      o->u64 = ex->gpr[r-RID_MIN_GPR];
744
#endif
745
#if LJ_64 && !LJ_GC64
746
    } else if (irt_is64(t)) {
747
      /* 64 bit values that already have the tag bits. */
748
      o->u64 = ex->gpr[r-RID_MIN_GPR];
749
#endif
750
    } else if (irt_ispri(t)) {
31,193✔
751
      setpriV(o, irt_toitype(t));
×
752
    } else {
753
      setgcV(J->L, o, (GCobj *)ex->gpr[r-RID_MIN_GPR], irt_toitype(t));
31,193✔
754
    }
755
  }
756
}
757

758
#if LJ_HASFFI
759
/* Restore raw data from the trace exit state. */
760
static void snap_restoredata(jit_State *J, GCtrace *T, ExitState *ex,
2,150✔
761
                             SnapNo snapno, BloomFilter rfilt,
762
                             IRRef ref, void *dst, CTSize sz)
763
{
764
  IRIns *ir = &T->ir[ref];
2,150✔
765
  RegSP rs = ir->prev;
2,150✔
766
  int32_t *src;
2,150✔
767
  uint64_t tmp;
2,150✔
768
  UNUSED(J);
2,150✔
769
  if (irref_isk(ref)) {
2,150✔
770
    if (ir_isk64(ir)) {
8✔
771
      src = (int32_t *)&ir[1];
2✔
772
    } else if (sz == 8) {
6✔
773
      tmp = (uint64_t)(uint32_t)ir->i;
×
774
      src = (int32_t *)&tmp;
×
775
    } else {
776
      src = &ir->i;
6✔
777
    }
778
  } else {
779
    if (LJ_UNLIKELY(bloomtest(rfilt, ref)))
2,142✔
780
      rs = snap_renameref(T, snapno, ref, rs);
×
781
    if (ra_hasspill(regsp_spill(rs))) {
2,142✔
782
      src = &ex->spill[regsp_spill(rs)];
818✔
783
      if (sz == 8 && !irt_is64(ir->t)) {
818✔
784
        tmp = (uint64_t)(uint32_t)*src;
×
785
        src = (int32_t *)&tmp;
×
786
      }
787
    } else {
788
      Reg r = regsp_reg(rs);
1,324✔
789
      if (ra_noreg(r)) {
1,324✔
790
        /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */
791
        lj_assertJ(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
13✔
792
                   "restore from IR %04d has no reg", ref - REF_BIAS);
793
        snap_restoredata(J, T, ex, snapno, rfilt, ir->op1, dst, 4);
13✔
794
        *(lua_Number *)dst = (lua_Number)*(int32_t *)dst;
13✔
795
        return;
13✔
796
      }
797
      src = (int32_t *)&ex->gpr[r-RID_MIN_GPR];
1,311✔
798
#if !LJ_SOFTFP
799
      if (r >= RID_MAX_GPR) {
1,311✔
800
        src = (int32_t *)&ex->fpr[r-RID_MIN_FPR];
16✔
801
#if LJ_TARGET_PPC
802
        if (sz == 4) {  /* PPC FPRs are always doubles. */
803
          *(float *)dst = (float)*(double *)src;
804
          return;
805
        }
806
#else
807
        if (LJ_BE && sz == 4) src++;
16✔
808
#endif
809
      } else
810
#endif
811
      if (LJ_64 && LJ_BE && sz == 4) src++;
812
    }
813
  }
814
  lj_assertJ(sz == 1 || sz == 2 || sz == 4 || sz == 8,
2,137✔
815
             "restore from IR %04d with bad size %d", ref - REF_BIAS, sz);
816
  if (sz == 4) *(int32_t *)dst = *src;
2,137✔
817
  else if (sz == 8) *(int64_t *)dst = *(int64_t *)src;
2,078✔
818
  else if (sz == 1) *(int8_t *)dst = (int8_t)*src;
×
819
  else *(int16_t *)dst = (int16_t)*src;
×
820
}
821
#endif
822

823
/* Unsink allocation from the trace exit state. Unsink sunk stores. */
824
static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,
825
                        SnapNo snapno, BloomFilter rfilt,
826
                        IRIns *ir, TValue *o)
827
{
828
  lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP ||
829
             ir->o == IR_CNEW || ir->o == IR_CNEWI,
830
             "sunk allocation with bad op %d", ir->o);
831
#if LJ_HASFFI
832
  if (ir->o == IR_CNEW || ir->o == IR_CNEWI) {
833
    CTState *cts = ctype_cts(J->L);
834
    CTypeID id = (CTypeID)T->ir[ir->op1].i;
835
    CTSize sz;
836
    CTInfo info = lj_ctype_info(cts, id, &sz);
837
    GCcdata *cd = lj_cdata_newx(cts, id, sz, info);
838
    setcdataV(J->L, o, cd);
839
    if (ir->o == IR_CNEWI) {
840
      uint8_t *p = (uint8_t *)cdataptr(cd);
841
      lj_assertJ(sz == 4 || sz == 8, "sunk cdata with bad size %d", sz);
842
      if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) {
843
        snap_restoredata(J, T, ex, snapno, rfilt, (ir+1)->op2,
844
                         LJ_LE ? p+4 : p, 4);
845
        if (LJ_BE) p += 4;
846
        sz = 4;
847
      }
848
      snap_restoredata(J, T, ex, snapno, rfilt, ir->op2, p, sz);
849
    } else {
850
      IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref];
851
      for (irs = ir+1; irs < irlast; irs++)
852
        if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
853
          IRIns *iro = &T->ir[T->ir[irs->op1].op2];
854
          uint8_t *p = (uint8_t *)cd;
855
          CTSize szs;
856
          lj_assertJ(irs->o == IR_XSTORE, "sunk store with bad op %d", irs->o);
857
          lj_assertJ(T->ir[irs->op1].o == IR_ADD,
858
                     "sunk store with bad add op %d", T->ir[irs->op1].o);
859
          lj_assertJ(iro->o == IR_KINT || iro->o == IR_KINT64,
860
                     "sunk store with bad const offset op %d", iro->o);
861
          if (irt_is64(irs->t)) szs = 8;
862
          else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1;
863
          else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2;
864
          else szs = 4;
865
          if (LJ_64 && iro->o == IR_KINT64)
866
            p += (int64_t)ir_k64(iro)->u64;
867
          else
868
            p += iro->i;
869
          lj_assertJ(p >= (uint8_t *)cdataptr(cd) &&
870
                     p + szs <= (uint8_t *)cdataptr(cd) + sz,
871
                     "sunk store with offset out of range");
872
          if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {
873
            lj_assertJ(szs == 4, "sunk store with bad size %d", szs);
874
            snap_restoredata(J, T, ex, snapno, rfilt, (irs+1)->op2,
875
                             LJ_LE ? p+4 : p, 4);
876
            if (LJ_BE) p += 4;
877
          }
878
          snap_restoredata(J, T, ex, snapno, rfilt, irs->op2, p, szs);
879
        }
880
    }
881
  } else
882
#endif
883
  {
884
    IRIns *irs, *irlast;
885
    GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) :
886
                                  lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1]));
887
    settabV(J->L, o, t);
888
    irlast = &T->ir[T->snap[snapno].ref];
889
    for (irs = ir+1; irs < irlast; irs++)
890
      if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
891
        IRIns *irk = &T->ir[irs->op1];
892
        TValue tmp, *val;
893
        lj_assertJ(irs->o == IR_ASTORE || irs->o == IR_HSTORE ||
894
                   irs->o == IR_FSTORE,
895
                   "sunk store with bad op %d", irs->o);
896
        if (irk->o == IR_FREF) {
897
          switch (irk->op2) {
898
          case IRFL_TAB_META:
899
            if (T->ir[irs->op2].o == IR_KNULL) {
900
              setgcrefnull(t->metatable);
901
            } else {
902
              snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp);
903
              /* NOBARRIER: The table is new (marked white). */
904
              setgcref(t->metatable, obj2gco(tabV(&tmp)));
905
            }
906
            break;
907
          case IRFL_TAB_NOMM:
908
            /* Negative metamethod cache invalidated by lj_tab_set() below. */
909
            break;
910
          default:
911
            lj_assertJ(0, "sunk store with bad field %d", irk->op2);
912
            break;
913
          }
914
        } else {
915
          irk = &T->ir[irk->op2];
916
          if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1];
917
          lj_ir_kvalue(J->L, &tmp, irk);
918
          val = lj_tab_set(J->L, t, &tmp);
919
          /* NOBARRIER: The table is new (marked white). */
920
          snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val);
921
          if (LJ_SOFTFP32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {
922
            snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp);
923
            val->u32.hi = tmp.u32.lo;
924
          }
925
        }
926
      }
927
  }
928
}
929

930
/* Restore interpreter state from exit state with the help of a snapshot. */
931
const BCIns *lj_snap_restore(jit_State *J, void *exptr)
52,111✔
932
{
933
  ExitState *ex = (ExitState *)exptr;
52,111✔
934
  SnapNo snapno = J->exitno;  /* For now, snapno == exitno. */
52,111✔
935
  GCtrace *T = traceref(J, J->parent);
52,111✔
936
  SnapShot *snap = &T->snap[snapno];
52,111✔
937
  MSize n, nent = snap->nent;
52,111✔
938
  SnapEntry *map = &T->snapmap[snap->mapofs];
52,111✔
939
#if !LJ_FR2 || defined(LUA_USE_ASSERT)
940
  SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1-LJ_FR2];
941
#endif
942
#if !LJ_FR2
943
  ptrdiff_t ftsz0;
944
#endif
945
  TValue *frame;
52,111✔
946
  BloomFilter rfilt = snap_renamefilter(T, snapno);
52,111✔
947
  const BCIns *pc = snap_pc(&map[nent]);
52,111✔
948
  lua_State *L = J->L;
52,111✔
949

950
  /* Set interpreter PC to the next PC to get correct error messages. */
951
  setcframe_pc(cframe_raw(L->cframe), pc+1);
52,111✔
952

953
  /* Make sure the stack is big enough for the slots from the snapshot. */
954
  if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) {
52,111✔
955
    L->top = curr_topL(L);
33✔
956
    lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize);
33✔
957
  }
958

959
  /* Fill stack slots with data from the registers and spill slots. */
960
  frame = L->base-1-LJ_FR2;
52,107✔
961
#if !LJ_FR2
962
  ftsz0 = frame_ftsz(frame);  /* Preserve link to previous frame in slot #0. */
963
#endif
964
  for (n = 0; n < nent; n++) {
193,205✔
965
    SnapEntry sn = map[n];
141,098✔
966
    if (!(sn & SNAP_NORESTORE)) {
141,098✔
967
      TValue *o = &frame[snap_slot(sn)];
135,077✔
968
      IRRef ref = snap_ref(sn);
135,077✔
969
      IRIns *ir = &T->ir[ref];
135,077✔
970
      if (ir->r == RID_SUNK) {
135,077✔
971
        MSize j;
972
        for (j = 0; j < n; j++)
31,142✔
973
          if (snap_ref(map[j]) == ref) {  /* De-duplicate sunk allocations. */
28,960✔
974
            copyTV(L, o, &frame[snap_slot(map[j])]);
10✔
975
            goto dupslot;
10✔
976
          }
977
        snap_unsink(J, T, ex, snapno, rfilt, ir, o);
2,182✔
978
      dupslot:
2,192✔
979
        continue;
2,192✔
980
      }
981
      snap_restoreval(J, T, ex, snapno, rfilt, ref, o);
132,885✔
982
      if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM) && tvisint(o)) {
132,885✔
983
        TValue tmp;
984
        snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp);
985
        o->u32.hi = tmp.u32.lo;
986
#if !LJ_FR2
987
      } else if ((sn & (SNAP_CONT|SNAP_FRAME))) {
988
        /* Overwrite tag with frame link. */
989
        setframe_ftsz(o, snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0);
990
        L->base = o+1;
991
#endif
992
      }
993
    }
994
  }
995
#if LJ_FR2
996
  L->base += (map[nent+LJ_BE] & 0xff);
52,107✔
997
#endif
998
  lj_assertJ(map + nent == flinks, "inconsistent frames in snapshot");
52,107✔
999

1000
  /* Compute current stack top. */
1001
  switch (bc_op(*pc)) {
52,107✔
1002
  default:
51,916✔
1003
    if (bc_op(*pc) < BC_FUNCF) {
51,916✔
1004
      L->top = curr_topL(L);
51,905✔
1005
      break;
51,905✔
1006
    }
1007
    /* fallthrough */
1008
  case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM:
1009
    L->top = frame + snap->nslots;
202✔
1010
    break;
202✔
1011
  }
1012
  J->nsnaprestore++;
52,107✔
1013
  return pc;
52,107✔
1014
}
1015

1016
#undef emitir_raw
1017
#undef emitir
1018

1019
#endif
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

© 2025 Coveralls, Inc