summaryrefslogtreecommitdiff
path: root/src/lj_snap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_snap.c')
-rw-r--r--src/lj_snap.c53
1 files changed, 15 insertions, 38 deletions
diff --git a/src/lj_snap.c b/src/lj_snap.c
index 8a53e3f6..95dc77da 100644
--- a/src/lj_snap.c
+++ b/src/lj_snap.c
@@ -68,49 +68,26 @@ static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots)
68 return n; 68 return n;
69} 69}
70 70
71/* Add frame links at the end of the snapshot. */
72static MSize snapshot_framelinks(jit_State *J, SnapEntry *map)
73{
74 cTValue *frame = J->L->base - 1;
75 cTValue *lim = J->L->base - J->baseslot;
76 MSize f = 0;
77 map[f++] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */
78 while (frame > lim) { /* Backwards traversal of all frames above base. */
79 if (frame_islua(frame)) {
80 map[f++] = SNAP_MKPC(frame_pc(frame));
81 frame = frame_prevl(frame);
82 } else if (frame_ispcall(frame)) {
83 map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
84 frame = frame_prevd(frame);
85 } else if (frame_iscont(frame)) {
86 map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
87 map[f++] = SNAP_MKPC(frame_contpc(frame));
88 frame = frame_prevd(frame);
89 } else {
90 lua_assert(0);
91 }
92 }
93 return f;
94}
95
96/* Take a snapshot of the current stack. */ 71/* Take a snapshot of the current stack. */
97static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap) 72static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap)
98{ 73{
99 BCReg nslots = J->baseslot + J->maxslot; 74 BCReg nslots = J->baseslot + J->maxslot;
100 MSize nent, nframelinks; 75 MSize nent;
101 SnapEntry *p; 76 SnapEntry *p;
102 /* Conservative estimate. Continuation frames need 2 slots. */ 77 /* Conservative estimate. */
103 lj_snap_grow_map(J, nsnapmap + nslots + (MSize)J->framedepth*2+1); 78 lj_snap_grow_map(J, nsnapmap + nslots + (MSize)J->framedepth+1);
104 p = &J->cur.snapmap[nsnapmap]; 79 p = &J->cur.snapmap[nsnapmap];
105 nent = snapshot_slots(J, p, nslots); 80 nent = snapshot_slots(J, p, nslots);
106 nframelinks = snapshot_framelinks(J, p + nent);
107 J->cur.nsnapmap = (uint16_t)(nsnapmap + nent + nframelinks);
108 snap->mapofs = (uint16_t)nsnapmap; 81 snap->mapofs = (uint16_t)nsnapmap;
109 snap->ref = (IRRef1)J->cur.nins; 82 snap->ref = (IRRef1)J->cur.nins;
110 snap->nent = (uint8_t)nent; 83 snap->nent = (uint8_t)nent;
111 snap->nframelinks = (uint8_t)nframelinks; 84 snap->depth = (uint8_t)J->framedepth;
112 snap->nslots = (uint8_t)nslots; 85 snap->nslots = (uint8_t)nslots;
113 snap->count = 0; 86 snap->count = 0;
87 J->cur.nsnapmap = (uint16_t)(nsnapmap + nent + 1 + J->framedepth);
88 /* Add frame links at the end of the snapshot. */
89 p[nent] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */
90 memcpy(&p[nent+1], J->frame, sizeof(SnapEntry)*(size_t)J->framedepth);
114} 91}
115 92
116/* Add or merge a snapshot. */ 93/* Add or merge a snapshot. */
@@ -141,14 +118,14 @@ void lj_snap_shrink(jit_State *J)
141 lua_assert(nslots < snap->nslots); 118 lua_assert(nslots < snap->nslots);
142 snap->nslots = (uint8_t)nslots; 119 snap->nslots = (uint8_t)nslots;
143 if (nent > 0 && snap_slot(map[nent-1]) >= nslots) { 120 if (nent > 0 && snap_slot(map[nent-1]) >= nslots) {
144 MSize s, delta, nframelinks = snap->nframelinks; 121 MSize s, delta, depth = snap->depth;
145 for (nent--; nent > 0 && snap_slot(map[nent-1]) >= nslots; nent--) 122 for (nent--; nent > 0 && snap_slot(map[nent-1]) >= nslots; nent--)
146 ; 123 ;
147 delta = snap->nent - nent; 124 delta = snap->nent - nent;
148 snap->nent = (uint8_t)nent; 125 snap->nent = (uint8_t)nent;
149 J->cur.nsnapmap = (uint16_t)(snap->mapofs + nent + nframelinks); 126 J->cur.nsnapmap = (uint16_t)(snap->mapofs + nent + 1 + depth);
150 map += nent; 127 map += nent;
151 for (s = 0; s < nframelinks; s++) /* Move frame links down. */ 128 for (s = 0; s <= depth; s++) /* Move PC + frame links down. */
152 map[s] = map[s+delta]; 129 map[s] = map[s+delta];
153 } 130 }
154} 131}
@@ -210,7 +187,7 @@ void lj_snap_restore(jit_State *J, void *exptr)
210 SnapShot *snap = &T->snap[snapno]; 187 SnapShot *snap = &T->snap[snapno];
211 MSize n, nent = snap->nent; 188 MSize n, nent = snap->nent;
212 SnapEntry *map = &T->snapmap[snap->mapofs]; 189 SnapEntry *map = &T->snapmap[snap->mapofs];
213 SnapEntry *flinks = map + nent + snap->nframelinks; 190 SnapEntry *flinks = map + nent;
214 int32_t ftsz0; 191 int32_t ftsz0;
215 BCReg nslots = snap->nslots; 192 BCReg nslots = snap->nslots;
216 TValue *frame; 193 TValue *frame;
@@ -224,6 +201,7 @@ void lj_snap_restore(jit_State *J, void *exptr)
224 } 201 }
225 202
226 /* Fill stack slots with data from the registers and spill slots. */ 203 /* Fill stack slots with data from the registers and spill slots. */
204 J->pc = snap_pc(*flinks++);
227 frame = L->base-1; 205 frame = L->base-1;
228 ftsz0 = frame_ftsz(frame); /* Preserve link to previous frame in slot #0. */ 206 ftsz0 = frame_ftsz(frame); /* Preserve link to previous frame in slot #0. */
229 for (n = 0; n < nent; n++) { 207 for (n = 0; n < nent; n++) {
@@ -236,7 +214,7 @@ void lj_snap_restore(jit_State *J, void *exptr)
236 lj_ir_kvalue(L, o, ir); 214 lj_ir_kvalue(L, o, ir);
237 if ((sn & (SNAP_CONT|SNAP_FRAME))) { 215 if ((sn & (SNAP_CONT|SNAP_FRAME))) {
238 /* Overwrite tag with frame link. */ 216 /* Overwrite tag with frame link. */
239 o->fr.tp.ftsz = s != 0 ? (int32_t)*--flinks : ftsz0; 217 o->fr.tp.ftsz = s != 0 ? (int32_t)*flinks++ : ftsz0;
240 if ((sn & SNAP_FRAME)) { 218 if ((sn & SNAP_FRAME)) {
241 GCfunc *fn = ir_kfunc(ir); 219 GCfunc *fn = ir_kfunc(ir);
242 if (isluafunc(fn)) { 220 if (isluafunc(fn)) {
@@ -291,8 +269,7 @@ void lj_snap_restore(jit_State *J, void *exptr)
291 } 269 }
292 } 270 }
293 L->top = curr_topL(L); 271 L->top = curr_topL(L);
294 J->pc = snap_pc(*--flinks); 272 lua_assert(map + nent + 1 + snap->depth == flinks);
295 lua_assert(map + nent == flinks);
296} 273}
297 274
298#undef IR 275#undef IR