3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
26 #include "lj_gdbjit.h"
27 #include "lj_record.h"
29 #include "lj_dispatch.h"
31 #include "lj_vmevent.h"
32 #include "lj_target.h"
34 /* -- Error handling ------------------------------------------------------ */
36 /* Synchronous abort with error message. */
37 void lj_trace_err(jit_State
*J
, TraceError e
)
39 setnilV(&J
->errinfo
); /* No error info. */
40 setintV(J
->L
->top
++, (int32_t)e
);
41 lj_err_throw(J
->L
, LUA_ERRRUN
);
44 /* Synchronous abort with error message and error info. */
45 void lj_trace_err_info(jit_State
*J
, TraceError e
)
47 setintV(J
->L
->top
++, (int32_t)e
);
48 lj_err_throw(J
->L
, LUA_ERRRUN
);
51 /* -- Trace management ---------------------------------------------------- */
53 /* The current trace is first assembled in J->cur. The variable length
54 ** arrays point to shared, growable buffers (J->irbuf etc.). When trace
55 ** recording ends successfully, the current trace and its data structures
56 ** are copied to a new (compact) GCtrace object.
59 /* Find a free trace number. */
60 static TraceNo
trace_findfree(jit_State
*J
)
63 if (J
->freetrace
== 0)
65 for (; J
->freetrace
< J
->sizetrace
; J
->freetrace
++)
66 if (traceref(J
, J
->freetrace
) == NULL
)
67 return J
->freetrace
++;
68 /* Need to grow trace array. */
69 lim
= (MSize
)J
->param
[JIT_P_maxtrace
] + 1;
70 if (lim
< 2) lim
= 2; else if (lim
> 65535) lim
= 65535;
73 return 0; /* Too many traces. */
74 lj_mem_growvec(J
->L
, J
->trace
, J
->sizetrace
, lim
, GCRef
);
75 for (; osz
< J
->sizetrace
; osz
++)
76 setgcrefnull(J
->trace
[osz
]);
80 #define TRACE_APPENDVEC(field, szfield, tp) \
82 memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \
83 p += J->cur.szfield*sizeof(tp);
85 #ifdef LUAJIT_USE_PERFTOOLS
87 ** Create symbol table of JIT-compiled code. For use with Linux perf tools.
89 ** perf record -f -e cycles luajit test.lua
90 ** perf report -s symbol
91 ** rm perf.data /tmp/perf-*.map
96 static void perftools_addtrace(GCtrace
*T
)
99 GCproto
*pt
= &gcref(T
->startpt
)->pt
;
100 const BCIns
*startpc
= mref(T
->startpc
, const BCIns
);
101 const char *name
= proto_chunknamestr(pt
);
103 if (name
[0] == '@' || name
[0] == '=')
107 lua_assert(startpc
>= proto_bc(pt
) && startpc
< proto_bc(pt
) + pt
->sizebc
);
108 lineno
= lj_debug_line(pt
, proto_bcpos(pt
, startpc
));
111 sprintf(fname
, "/tmp/perf-%d.map", getpid());
112 if (!(fp
= fopen(fname
, "w"))) return;
115 fprintf(fp
, "%lx %x TRACE_%d::%s:%u\n",
116 (long)T
->mcode
, T
->szmcode
, T
->traceno
, name
, lineno
);
120 /* Save current trace by copying and compacting it. */
121 static void trace_save(jit_State
*J
)
123 size_t sztr
= ((sizeof(GCtrace
)+7)&~7);
124 size_t szins
= (J
->cur
.nins
-J
->cur
.nk
)*sizeof(IRIns
);
125 size_t sz
= sztr
+ szins
+
126 J
->cur
.nsnap
*sizeof(SnapShot
) +
127 J
->cur
.nsnapmap
*sizeof(SnapEntry
);
128 GCtrace
*T
= lj_mem_newt(J
->L
, (MSize
)sz
, GCtrace
);
129 char *p
= (char *)T
+ sztr
;
130 memcpy(T
, &J
->cur
, sizeof(GCtrace
));
131 setgcrefr(T
->nextgc
, J2G(J
)->gc
.root
);
132 setgcrefp(J2G(J
)->gc
.root
, T
);
135 T
->ir
= (IRIns
*)p
- J
->cur
.nk
;
136 memcpy(p
, J
->cur
.ir
+J
->cur
.nk
, szins
);
138 TRACE_APPENDVEC(snap
, nsnap
, SnapShot
)
139 TRACE_APPENDVEC(snapmap
, nsnapmap
, SnapEntry
)
141 setgcrefp(J
->trace
[T
->traceno
], T
);
142 lj_gc_barriertrace(J2G(J
), T
->traceno
);
143 lj_gdbjit_addtrace(J
, T
);
144 #ifdef LUAJIT_USE_PERFTOOLS
145 perftools_addtrace(T
);
149 void LJ_FASTCALL
lj_trace_free(global_State
*g
, GCtrace
*T
)
151 jit_State
*J
= G2J(g
);
153 lj_gdbjit_deltrace(J
, T
);
154 if (T
->traceno
< J
->freetrace
)
155 J
->freetrace
= T
->traceno
;
156 setgcrefnull(J
->trace
[T
->traceno
]);
159 ((sizeof(GCtrace
)+7)&~7) + (T
->nins
-T
->nk
)*sizeof(IRIns
) +
160 T
->nsnap
*sizeof(SnapShot
) + T
->nsnapmap
*sizeof(SnapEntry
));
163 /* Re-enable compiling a prototype by unpatching any modified bytecode. */
164 void lj_trace_reenableproto(GCproto
*pt
)
166 if ((pt
->flags
& PROTO_ILOOP
)) {
167 BCIns
*bc
= proto_bc(pt
);
168 BCPos i
, sizebc
= pt
->sizebc
;;
169 pt
->flags
&= ~PROTO_ILOOP
;
170 if (bc_op(bc
[0]) == BC_IFUNCF
)
171 setbc_op(&bc
[0], BC_FUNCF
);
172 for (i
= 1; i
< sizebc
; i
++) {
173 BCOp op
= bc_op(bc
[i
]);
174 if (op
== BC_IFORL
|| op
== BC_IITERL
|| op
== BC_ILOOP
)
175 setbc_op(&bc
[i
], (int)op
+(int)BC_LOOP
-(int)BC_ILOOP
);
180 /* Unpatch the bytecode modified by a root trace. */
181 static void trace_unpatch(jit_State
*J
, GCtrace
*T
)
183 BCOp op
= bc_op(T
->startins
);
184 BCIns
*pc
= mref(T
->startpc
, BCIns
);
187 return; /* No need to unpatch branches in parent traces (yet). */
188 switch (bc_op(*pc
)) {
190 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
192 pc
+= bc_j(T
->startins
);
193 lua_assert(bc_op(*pc
) == BC_JFORI
);
194 setbc_op(pc
, BC_FORI
);
198 lua_assert(op
== BC_ITERL
|| op
== BC_LOOP
|| bc_isret(op
));
202 lua_assert(op
== BC_ITERL
);
204 if (bc_op(*pc
) == BC_JITERL
) {
205 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
210 lua_assert(op
== BC_FUNCF
);
213 default: /* Already unpatched. */
218 /* Flush a root trace. */
219 static void trace_flushroot(jit_State
*J
, GCtrace
*T
)
221 GCproto
*pt
= &gcref(T
->startpt
)->pt
;
222 lua_assert(T
->root
== 0 && pt
!= NULL
);
223 /* First unpatch any modified bytecode. */
225 /* Unlink root trace from chain anchored in prototype. */
226 if (pt
->trace
== T
->traceno
) { /* Trace is first in chain. Easy. */
227 pt
->trace
= T
->nextroot
;
228 } else if (pt
->trace
) { /* Otherwise search in chain of root traces. */
229 GCtrace
*T2
= traceref(J
, pt
->trace
);
231 for (; T2
->nextroot
; T2
= traceref(J
, T2
->nextroot
))
232 if (T2
->nextroot
== T
->traceno
) {
233 T2
->nextroot
= T
->nextroot
; /* Unlink from chain. */
240 /* Flush a trace. Only root traces are considered. */
241 void lj_trace_flush(jit_State
*J
, TraceNo traceno
)
243 if (traceno
> 0 && traceno
< J
->sizetrace
) {
244 GCtrace
*T
= traceref(J
, traceno
);
245 if (T
&& T
->root
== 0)
246 trace_flushroot(J
, T
);
250 /* Flush all traces associated with a prototype. */
251 void lj_trace_flushproto(global_State
*g
, GCproto
*pt
)
253 while (pt
->trace
!= 0)
254 trace_flushroot(G2J(g
), traceref(G2J(g
), pt
->trace
));
257 /* Flush all traces. */
258 int lj_trace_flushall(lua_State
*L
)
260 jit_State
*J
= L2J(L
);
262 if ((J2G(J
)->hookmask
& HOOK_GC
))
264 for (i
= (ptrdiff_t)J
->sizetrace
-1; i
> 0; i
--) {
265 GCtrace
*T
= traceref(J
, i
);
268 trace_flushroot(J
, T
);
269 lj_gdbjit_deltrace(J
, T
);
271 setgcrefnull(J
->trace
[i
]);
276 /* Clear penalty cache. */
277 memset(J
->penalty
, 0, sizeof(J
->penalty
));
278 /* Free the whole machine code and invalidate all exit stub groups. */
280 memset(J
->exitstubgroup
, 0, sizeof(J
->exitstubgroup
));
281 lj_vmevent_send(L
, TRACE
,
282 setstrV(L
, L
->top
++, lj_str_newlit(L
, "flush"));
287 /* Initialize JIT compiler state. */
288 void lj_trace_initstate(global_State
*g
)
290 jit_State
*J
= G2J(g
);
292 /* Initialize SIMD constants. */
293 tv
= LJ_KSIMD(J
, LJ_KSIMD_ABS
);
294 tv
[0].u64
= U64x(7fffffff
,ffffffff
);
295 tv
[1].u64
= U64x(7fffffff
,ffffffff
);
296 tv
= LJ_KSIMD(J
, LJ_KSIMD_NEG
);
297 tv
[0].u64
= U64x(80000000,00000000);
298 tv
[1].u64
= U64x(80000000,00000000);
301 /* Free everything associated with the JIT compiler state. */
302 void lj_trace_freestate(global_State
*g
)
304 jit_State
*J
= G2J(g
);
305 #ifdef LUA_USE_ASSERT
306 { /* This assumes all traces have already been freed. */
308 for (i
= 1; i
< (ptrdiff_t)J
->sizetrace
; i
++)
309 lua_assert(i
== (ptrdiff_t)J
->cur
.traceno
|| traceref(J
, i
) == NULL
);
313 lj_ir_k64_freeall(J
);
314 lj_mem_freevec(g
, J
->snapmapbuf
, J
->sizesnapmap
, SnapEntry
);
315 lj_mem_freevec(g
, J
->snapbuf
, J
->sizesnap
, SnapShot
);
316 lj_mem_freevec(g
, J
->irbuf
+ J
->irbotlim
, J
->irtoplim
- J
->irbotlim
, IRIns
);
317 lj_mem_freevec(g
, J
->trace
, J
->sizetrace
, GCRef
);
320 /* -- Penalties and blacklisting ------------------------------------------ */
322 /* Blacklist a bytecode instruction. */
323 static void blacklist_pc(GCproto
*pt
, BCIns
*pc
)
325 setbc_op(pc
, (int)bc_op(*pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
326 pt
->flags
|= PROTO_ILOOP
;
329 /* Penalize a bytecode instruction. */
330 static void penalty_pc(jit_State
*J
, GCproto
*pt
, BCIns
*pc
, TraceError e
)
332 uint32_t i
, val
= PENALTY_MIN
;
333 for (i
= 0; i
< PENALTY_SLOTS
; i
++)
334 if (mref(J
->penalty
[i
].pc
, const BCIns
) == pc
) { /* Cache slot found? */
335 /* First try to bump its hotcount several times. */
336 val
= ((uint32_t)J
->penalty
[i
].val
<< 1) +
337 LJ_PRNG_BITS(J
, PENALTY_RNDBITS
);
338 if (val
> PENALTY_MAX
) {
339 blacklist_pc(pt
, pc
); /* Blacklist it, if that didn't help. */
344 /* Assign a new penalty cache slot. */
346 J
->penaltyslot
= (J
->penaltyslot
+ 1) & (PENALTY_SLOTS
-1);
347 setmref(J
->penalty
[i
].pc
, pc
);
349 J
->penalty
[i
].val
= (uint16_t)val
;
350 J
->penalty
[i
].reason
= e
;
351 hotcount_set(J2GG(J
), pc
+1, val
);
354 /* -- Trace compiler state machine ---------------------------------------- */
357 static void trace_start(jit_State
*J
)
362 if ((J
->pt
->flags
& PROTO_NOJIT
)) { /* JIT disabled for this proto? */
363 if (J
->parent
== 0 && J
->exitno
== 0) {
364 /* Lazy bytecode patching to disable hotcount events. */
365 lua_assert(bc_op(*J
->pc
) == BC_FORL
|| bc_op(*J
->pc
) == BC_ITERL
||
366 bc_op(*J
->pc
) == BC_LOOP
|| bc_op(*J
->pc
) == BC_FUNCF
);
367 setbc_op(J
->pc
, (int)bc_op(*J
->pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
368 J
->pt
->flags
|= PROTO_ILOOP
;
370 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
374 /* Get a new trace number. */
375 traceno
= trace_findfree(J
);
376 if (LJ_UNLIKELY(traceno
== 0)) { /* No free trace? */
377 lua_assert((J2G(J
)->hookmask
& HOOK_GC
) == 0);
378 lj_trace_flushall(J
->L
);
379 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
382 setgcrefp(J
->trace
[traceno
], &J
->cur
);
384 /* Setup enough of the current trace to be able to send the vmevent. */
385 memset(&J
->cur
, 0, sizeof(GCtrace
));
386 J
->cur
.traceno
= traceno
;
387 J
->cur
.nins
= J
->cur
.nk
= REF_BASE
;
388 J
->cur
.ir
= J
->irbuf
;
389 J
->cur
.snap
= J
->snapbuf
;
390 J
->cur
.snapmap
= J
->snapmapbuf
;
394 J
->guardemit
.irt
= 0;
395 J
->postproc
= LJ_POST_NONE
;
397 setgcref(J
->cur
.startpt
, obj2gco(J
->pt
));
400 lj_vmevent_send(L
, TRACE
,
401 setstrV(L
, L
->top
++, lj_str_newlit(L
, "start"));
402 setintV(L
->top
++, traceno
);
403 setfuncV(L
, L
->top
++, J
->fn
);
404 setintV(L
->top
++, proto_bcpos(J
->pt
, J
->pc
));
406 setintV(L
->top
++, J
->parent
);
407 setintV(L
->top
++, J
->exitno
);
414 static void trace_stop(jit_State
*J
)
416 BCIns
*pc
= mref(J
->cur
.startpc
, BCIns
);
417 BCOp op
= bc_op(J
->cur
.startins
);
418 GCproto
*pt
= &gcref(J
->cur
.startpt
)->pt
;
419 TraceNo traceno
= J
->cur
.traceno
;
424 setbc_op(pc
+bc_j(J
->cur
.startins
), BC_JFORI
); /* Patch FORI, too. */
429 /* Patch bytecode of starting instruction in root trace. */
430 setbc_op(pc
, (int)op
+(int)BC_JLOOP
-(int)BC_LOOP
);
431 setbc_d(pc
, traceno
);
433 /* Add to root trace chain in prototype. */
434 J
->cur
.nextroot
= pt
->trace
;
435 pt
->trace
= (TraceNo1
)traceno
;
440 *pc
= BCINS_AD(BC_JLOOP
, J
->cur
.snap
[0].nslots
, traceno
);
443 /* Patch exit branch in parent to side trace entry. */
444 lua_assert(J
->parent
!= 0 && J
->cur
.root
!= 0);
445 lj_asm_patchexit(J
, traceref(J
, J
->parent
), J
->exitno
, J
->cur
.mcode
);
446 /* Avoid compiling a side trace twice (stack resizing uses parent exit). */
447 traceref(J
, J
->parent
)->snap
[J
->exitno
].count
= SNAPCOUNT_DONE
;
448 /* Add to side trace chain in root trace. */
450 GCtrace
*root
= traceref(J
, J
->cur
.root
);
452 J
->cur
.nextside
= root
->nextside
;
453 root
->nextside
= (TraceNo1
)traceno
;
459 /* Trace stitching: patch link of previous trace. */
460 traceref(J
, J
->exitno
)->link
= traceno
;
467 /* Commit new mcode only after all patching is done. */
468 lj_mcode_commit(J
, J
->cur
.mcode
);
469 J
->postproc
= LJ_POST_NONE
;
473 lj_vmevent_send(L
, TRACE
,
474 setstrV(L
, L
->top
++, lj_str_newlit(L
, "stop"));
475 setintV(L
->top
++, traceno
);
476 setfuncV(L
, L
->top
++, J
->fn
);
480 /* Start a new root trace for down-recursion. */
481 static int trace_downrec(jit_State
*J
)
483 /* Restart recording at the return instruction. */
484 lua_assert(J
->pt
!= NULL
);
485 lua_assert(bc_isret(bc_op(*J
->pc
)));
486 if (bc_op(*J
->pc
) == BC_RETM
)
487 return 0; /* NYI: down-recursion with RETM. */
490 J
->state
= LJ_TRACE_RECORD
;
496 static int trace_abort(jit_State
*J
)
499 TraceError e
= LJ_TRERR_RECERR
;
502 J
->postproc
= LJ_POST_NONE
;
504 if (tvisnumber(L
->top
-1))
505 e
= (TraceError
)numberVint(L
->top
-1);
506 if (e
== LJ_TRERR_MCODELM
) {
507 L
->top
--; /* Remove error object */
508 J
->state
= LJ_TRACE_ASM
;
509 return 1; /* Retry ASM with new MCode area. */
511 /* Penalize or blacklist starting bytecode instruction. */
512 if (J
->parent
== 0 && !bc_isret(bc_op(J
->cur
.startins
))) {
514 penalty_pc(J
, &gcref(J
->cur
.startpt
)->pt
, mref(J
->cur
.startpc
, BCIns
), e
);
516 traceref(J
, J
->exitno
)->link
= J
->exitno
; /* Self-link is blacklisted. */
519 /* Is there anything to abort? */
520 traceno
= J
->cur
.traceno
;
522 ptrdiff_t errobj
= savestack(L
, L
->top
-1); /* Stack may be resized. */
524 J
->cur
.linktype
= LJ_TRLINK_NONE
;
525 lj_vmevent_send(L
, TRACE
,
529 setstrV(L
, L
->top
++, lj_str_newlit(L
, "abort"));
530 setintV(L
->top
++, traceno
);
531 /* Find original Lua function call to generate a better error message. */
532 frame
= J
->L
->base
-1;
534 while (!isluafunc(frame_func(frame
))) {
535 pc
= (frame_iscont(frame
) ? frame_contpc(frame
) : frame_pc(frame
)) - 1;
536 frame
= frame_prev(frame
);
538 fn
= frame_func(frame
);
539 setfuncV(L
, L
->top
++, fn
);
540 setintV(L
->top
++, proto_bcpos(funcproto(fn
), pc
));
541 copyTV(L
, L
->top
++, restorestack(L
, errobj
));
542 copyTV(L
, L
->top
++, &J
->errinfo
);
544 /* Drop aborted trace after the vmevent (which may still access it). */
545 setgcrefnull(J
->trace
[traceno
]);
546 if (traceno
< J
->freetrace
)
547 J
->freetrace
= traceno
;
550 L
->top
--; /* Remove error object */
551 if (e
== LJ_TRERR_DOWNREC
)
552 return trace_downrec(J
);
553 else if (e
== LJ_TRERR_MCODEAL
)
554 lj_trace_flushall(L
);
558 /* Perform pending re-patch of a bytecode instruction. */
559 static LJ_AINLINE
void trace_pendpatch(jit_State
*J
, int force
)
561 if (LJ_UNLIKELY(J
->patchpc
)) {
562 if (force
|| J
->bcskip
== 0) {
563 *J
->patchpc
= J
->patchins
;
571 /* State machine for the trace compiler. Protected callback. */
572 static TValue
*trace_state(lua_State
*L
, lua_CFunction dummy
, void *ud
)
574 jit_State
*J
= (jit_State
*)ud
;
580 J
->state
= LJ_TRACE_RECORD
; /* trace_start() may change state. */
582 lj_dispatch_update(J2G(J
));
585 case LJ_TRACE_RECORD
:
586 trace_pendpatch(J
, 0);
587 setvmstate(J2G(J
), RECORD
);
588 lj_vmevent_send_(L
, RECORD
,
589 /* Save/restore tmptv state for trace recorder. */
590 TValue savetv
= J2G(J
)->tmptv
;
591 TValue savetv2
= J2G(J
)->tmptv2
;
592 setintV(L
->top
++, J
->cur
.traceno
);
593 setfuncV(L
, L
->top
++, J
->fn
);
594 setintV(L
->top
++, J
->pt
? (int32_t)proto_bcpos(J
->pt
, J
->pc
) : -1);
595 setintV(L
->top
++, J
->framedepth
);
597 J2G(J
)->tmptv
= savetv
;
598 J2G(J
)->tmptv2
= savetv2
;
604 trace_pendpatch(J
, 1);
606 if ((J
->flags
& JIT_F_OPT_LOOP
) &&
607 J
->cur
.link
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
608 setvmstate(J2G(J
), OPT
);
610 if (lj_opt_loop(J
)) { /* Loop optimization failed? */
612 J
->cur
.linktype
= LJ_TRLINK_NONE
;
613 J
->loopref
= J
->cur
.nins
;
614 J
->state
= LJ_TRACE_RECORD
; /* Try to continue recording. */
617 J
->loopref
= J
->chain
[IR_LOOP
]; /* Needed by assembler. */
621 if (!J
->loopref
) J
->cur
.snap
[J
->cur
.nsnap
-1].count
= SNAPCOUNT_DONE
;
622 J
->state
= LJ_TRACE_ASM
;
626 setvmstate(J2G(J
), ASM
);
627 lj_asm_trace(J
, &J
->cur
);
629 setvmstate(J2G(J
), INTERP
);
630 J
->state
= LJ_TRACE_IDLE
;
631 lj_dispatch_update(J2G(J
));
634 default: /* Trace aborted asynchronously. */
635 setintV(L
->top
++, (int32_t)LJ_TRERR_RECERR
);
638 trace_pendpatch(J
, 1);
641 setvmstate(J2G(J
), INTERP
);
642 J
->state
= LJ_TRACE_IDLE
;
643 lj_dispatch_update(J2G(J
));
646 } while (J
->state
> LJ_TRACE_RECORD
);
650 /* -- Event handling ------------------------------------------------------ */
652 /* A bytecode instruction is about to be executed. Record it. */
653 void lj_trace_ins(jit_State
*J
, const BCIns
*pc
)
655 /* Note: J->L must already be set. pc is the true bytecode PC here. */
657 J
->fn
= curr_func(J
->L
);
658 J
->pt
= isluafunc(J
->fn
) ? funcproto(J
->fn
) : NULL
;
659 while (lj_vm_cpcall(J
->L
, NULL
, (void *)J
, trace_state
) != 0)
660 J
->state
= LJ_TRACE_ERR
;
663 /* A hotcount triggered. Start recording a root trace. */
664 void LJ_FASTCALL
lj_trace_hot(jit_State
*J
, const BCIns
*pc
)
666 /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */
668 /* Reset hotcount. */
669 hotcount_set(J2GG(J
), pc
, J
->param
[JIT_P_hotloop
]*HOTCOUNT_LOOP
);
670 /* Only start a new trace if not recording or inside __gc call or vmevent. */
671 if (J
->state
== LJ_TRACE_IDLE
&&
672 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
673 J
->parent
= 0; /* Root trace. */
675 J
->state
= LJ_TRACE_START
;
676 lj_trace_ins(J
, pc
-1);
681 /* Check for a hot side exit. If yes, start recording a side trace. */
682 static void trace_hotside(jit_State
*J
, const BCIns
*pc
)
684 SnapShot
*snap
= &traceref(J
, J
->parent
)->snap
[J
->exitno
];
685 if (!(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
)) &&
686 isluafunc(curr_func(J
->L
)) &&
687 snap
->count
!= SNAPCOUNT_DONE
&&
688 ++snap
->count
>= J
->param
[JIT_P_hotexit
]) {
689 lua_assert(J
->state
== LJ_TRACE_IDLE
);
690 /* J->parent is non-zero for a side trace. */
691 J
->state
= LJ_TRACE_START
;
696 /* Stitch a new trace to the previous trace. */
697 void LJ_FASTCALL
lj_trace_stitch(jit_State
*J
, const BCIns
*pc
)
699 /* Only start a new trace if not recording or inside __gc call or vmevent. */
700 if (J
->state
== LJ_TRACE_IDLE
&&
701 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
702 J
->parent
= 0; /* Have to treat it like a root trace. */
703 /* J->exitno is set to the invoking trace. */
704 J
->state
= LJ_TRACE_START
;
710 /* Tiny struct to pass data to protected call. */
711 typedef struct ExitDataCP
{
713 void *exptr
; /* Pointer to exit state. */
714 const BCIns
*pc
; /* Restart interpreter at this PC. */
717 /* Need to protect lj_snap_restore because it may throw. */
718 static TValue
*trace_exit_cp(lua_State
*L
, lua_CFunction dummy
, void *ud
)
720 ExitDataCP
*exd
= (ExitDataCP
*)ud
;
721 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
722 exd
->pc
= lj_snap_restore(exd
->J
, exd
->exptr
);
727 #ifndef LUAJIT_DISABLE_VMEVENT
728 /* Push all registers from exit state. */
729 static void trace_exit_regs(lua_State
*L
, ExitState
*ex
)
732 setintV(L
->top
++, RID_NUM_GPR
);
733 setintV(L
->top
++, RID_NUM_FPR
);
734 for (i
= 0; i
< RID_NUM_GPR
; i
++) {
735 if (sizeof(ex
->gpr
[i
]) == sizeof(int32_t))
736 setintV(L
->top
++, (int32_t)ex
->gpr
[i
]);
738 setnumV(L
->top
++, (lua_Number
)ex
->gpr
[i
]);
741 for (i
= 0; i
< RID_NUM_FPR
; i
++) {
742 setnumV(L
->top
, ex
->fpr
[i
]);
743 if (LJ_UNLIKELY(tvisnan(L
->top
)))
751 #ifdef EXITSTATE_PCREG
752 /* Determine trace number from pc of exit instruction. */
753 static TraceNo
trace_exit_find(jit_State
*J
, MCode
*pc
)
756 for (traceno
= 1; traceno
< J
->sizetrace
; traceno
++) {
757 GCtrace
*T
= traceref(J
, traceno
);
758 if (T
&& pc
>= T
->mcode
&& pc
< (MCode
*)((char *)T
->mcode
+ T
->szmcode
))
766 /* A trace exited. Restore interpreter state. */
767 int LJ_FASTCALL
lj_trace_exit(jit_State
*J
, void *exptr
)
771 ExitState
*ex
= (ExitState
*)exptr
;
777 #ifdef EXITSTATE_PCREG
778 J
->parent
= trace_exit_find(J
, (MCode
*)(intptr_t)ex
->gpr
[EXITSTATE_PCREG
]);
780 T
= traceref(J
, J
->parent
); UNUSED(T
);
781 #ifdef EXITSTATE_CHECKEXIT
782 if (J
->exitno
== T
->nsnap
) { /* Treat stack check like a parent exit. */
783 lua_assert(T
->root
!= 0);
784 J
->exitno
= T
->ir
[REF_BASE
].op2
;
785 J
->parent
= T
->ir
[REF_BASE
].op1
;
786 T
= traceref(J
, J
->parent
);
789 lua_assert(T
!= NULL
&& J
->exitno
< T
->nsnap
);
792 errcode
= lj_vm_cpcall(L
, NULL
, &exd
, trace_exit_cp
);
794 return -errcode
; /* Return negated error code. */
796 if (!(LJ_HASPROFILE
&& (G(L
)->hookmask
& HOOK_PROFILE
)))
797 lj_vmevent_send(L
, TEXIT
,
798 lj_state_checkstack(L
, 4+RID_NUM_GPR
+RID_NUM_FPR
+LUA_MINSTACK
);
799 setintV(L
->top
++, J
->parent
);
800 setintV(L
->top
++, J
->exitno
);
801 trace_exit_regs(L
, ex
);
805 cf
= cframe_raw(L
->cframe
);
806 setcframe_pc(cf
, pc
);
807 if (LJ_HASPROFILE
&& (G(L
)->hookmask
& HOOK_PROFILE
)) {
808 /* Just exit to interpreter. */
809 } else if (G(L
)->gc
.state
== GCSatomic
|| G(L
)->gc
.state
== GCSfinalize
) {
810 if (!(G(L
)->hookmask
& HOOK_GC
))
811 lj_gc_step(L
); /* Exited because of GC: drive GC forward. */
813 trace_hotside(J
, pc
);
815 if (bc_op(*pc
) == BC_JLOOP
) {
816 BCIns
*retpc
= &traceref(J
, bc_d(*pc
))->startins
;
817 if (bc_isret(bc_op(*retpc
))) {
818 if (J
->state
== LJ_TRACE_RECORD
) {
820 J
->patchpc
= (BCIns
*)pc
;
821 *J
->patchpc
= *retpc
;
825 setcframe_pc(cf
, pc
);
829 /* Return MULTRES or 0. */
831 switch (bc_op(*pc
)) {
832 case BC_CALLM
: case BC_CALLMT
:
833 return (int)((BCReg
)(L
->top
- L
->base
) - bc_a(*pc
) - bc_c(*pc
));
835 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
) - bc_d(*pc
));
837 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
));
839 if (bc_op(*pc
) >= BC_FUNCF
)
840 return (int)((BCReg
)(L
->top
- L
->base
) + 1);