3 ** Copyright (C) 2005-2015 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 /* Allocate space for copy of trace. */
121 static GCtrace
*trace_save_alloc(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 return lj_mem_newt(J
->L
, (MSize
)sz
, GCtrace
);
131 /* Save current trace by copying and compacting it. */
132 static void trace_save(jit_State
*J
, GCtrace
*T
)
134 size_t sztr
= ((sizeof(GCtrace
)+7)&~7);
135 size_t szins
= (J
->cur
.nins
-J
->cur
.nk
)*sizeof(IRIns
);
136 char *p
= (char *)T
+ sztr
;
137 memcpy(T
, &J
->cur
, sizeof(GCtrace
));
138 setgcrefr(T
->nextgc
, J2G(J
)->gc
.root
);
139 setgcrefp(J2G(J
)->gc
.root
, T
);
142 T
->ir
= (IRIns
*)p
- J
->cur
.nk
;
143 memcpy(p
, J
->cur
.ir
+J
->cur
.nk
, szins
);
145 TRACE_APPENDVEC(snap
, nsnap
, SnapShot
)
146 TRACE_APPENDVEC(snapmap
, nsnapmap
, SnapEntry
)
148 setgcrefp(J
->trace
[T
->traceno
], T
);
149 lj_gc_barriertrace(J2G(J
), T
->traceno
);
150 lj_gdbjit_addtrace(J
, T
);
151 #ifdef LUAJIT_USE_PERFTOOLS
152 perftools_addtrace(T
);
156 void LJ_FASTCALL
lj_trace_free(global_State
*g
, GCtrace
*T
)
158 jit_State
*J
= G2J(g
);
160 lj_gdbjit_deltrace(J
, T
);
161 if (T
->traceno
< J
->freetrace
)
162 J
->freetrace
= T
->traceno
;
163 setgcrefnull(J
->trace
[T
->traceno
]);
166 ((sizeof(GCtrace
)+7)&~7) + (T
->nins
-T
->nk
)*sizeof(IRIns
) +
167 T
->nsnap
*sizeof(SnapShot
) + T
->nsnapmap
*sizeof(SnapEntry
));
170 /* Re-enable compiling a prototype by unpatching any modified bytecode. */
171 void lj_trace_reenableproto(GCproto
*pt
)
173 if ((pt
->flags
& PROTO_ILOOP
)) {
174 BCIns
*bc
= proto_bc(pt
);
175 BCPos i
, sizebc
= pt
->sizebc
;;
176 pt
->flags
&= ~PROTO_ILOOP
;
177 if (bc_op(bc
[0]) == BC_IFUNCF
)
178 setbc_op(&bc
[0], BC_FUNCF
);
179 for (i
= 1; i
< sizebc
; i
++) {
180 BCOp op
= bc_op(bc
[i
]);
181 if (op
== BC_IFORL
|| op
== BC_IITERL
|| op
== BC_ILOOP
)
182 setbc_op(&bc
[i
], (int)op
+(int)BC_LOOP
-(int)BC_ILOOP
);
187 /* Unpatch the bytecode modified by a root trace. */
188 static void trace_unpatch(jit_State
*J
, GCtrace
*T
)
190 BCOp op
= bc_op(T
->startins
);
191 BCIns
*pc
= mref(T
->startpc
, BCIns
);
194 return; /* No need to unpatch branches in parent traces (yet). */
195 switch (bc_op(*pc
)) {
197 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
199 pc
+= bc_j(T
->startins
);
200 lua_assert(bc_op(*pc
) == BC_JFORI
);
201 setbc_op(pc
, BC_FORI
);
205 lua_assert(op
== BC_ITERL
|| op
== BC_LOOP
|| bc_isret(op
));
209 lua_assert(op
== BC_ITERL
);
211 if (bc_op(*pc
) == BC_JITERL
) {
212 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
217 lua_assert(op
== BC_FUNCF
);
220 default: /* Already unpatched. */
225 /* Flush a root trace. */
226 static void trace_flushroot(jit_State
*J
, GCtrace
*T
)
228 GCproto
*pt
= &gcref(T
->startpt
)->pt
;
229 lua_assert(T
->root
== 0 && pt
!= NULL
);
230 /* First unpatch any modified bytecode. */
232 /* Unlink root trace from chain anchored in prototype. */
233 if (pt
->trace
== T
->traceno
) { /* Trace is first in chain. Easy. */
234 pt
->trace
= T
->nextroot
;
235 } else if (pt
->trace
) { /* Otherwise search in chain of root traces. */
236 GCtrace
*T2
= traceref(J
, pt
->trace
);
238 for (; T2
->nextroot
; T2
= traceref(J
, T2
->nextroot
))
239 if (T2
->nextroot
== T
->traceno
) {
240 T2
->nextroot
= T
->nextroot
; /* Unlink from chain. */
247 /* Flush a trace. Only root traces are considered. */
248 void lj_trace_flush(jit_State
*J
, TraceNo traceno
)
250 if (traceno
> 0 && traceno
< J
->sizetrace
) {
251 GCtrace
*T
= traceref(J
, traceno
);
252 if (T
&& T
->root
== 0)
253 trace_flushroot(J
, T
);
257 /* Flush all traces associated with a prototype. */
258 void lj_trace_flushproto(global_State
*g
, GCproto
*pt
)
260 while (pt
->trace
!= 0)
261 trace_flushroot(G2J(g
), traceref(G2J(g
), pt
->trace
));
264 /* Flush all traces. */
265 int lj_trace_flushall(lua_State
*L
)
267 jit_State
*J
= L2J(L
);
269 if ((J2G(J
)->hookmask
& HOOK_GC
))
271 for (i
= (ptrdiff_t)J
->sizetrace
-1; i
> 0; i
--) {
272 GCtrace
*T
= traceref(J
, i
);
275 trace_flushroot(J
, T
);
276 lj_gdbjit_deltrace(J
, T
);
278 setgcrefnull(J
->trace
[i
]);
283 /* Clear penalty cache. */
284 memset(J
->penalty
, 0, sizeof(J
->penalty
));
285 /* Free the whole machine code and invalidate all exit stub groups. */
287 memset(J
->exitstubgroup
, 0, sizeof(J
->exitstubgroup
));
288 lj_vmevent_send(L
, TRACE
,
289 setstrV(L
, L
->top
++, lj_str_newlit(L
, "flush"));
294 /* Initialize JIT compiler state. */
295 void lj_trace_initstate(global_State
*g
)
297 jit_State
*J
= G2J(g
);
299 /* Initialize SIMD constants. */
300 tv
= LJ_KSIMD(J
, LJ_KSIMD_ABS
);
301 tv
[0].u64
= U64x(7fffffff
,ffffffff
);
302 tv
[1].u64
= U64x(7fffffff
,ffffffff
);
303 tv
= LJ_KSIMD(J
, LJ_KSIMD_NEG
);
304 tv
[0].u64
= U64x(80000000,00000000);
305 tv
[1].u64
= U64x(80000000,00000000);
308 /* Free everything associated with the JIT compiler state. */
309 void lj_trace_freestate(global_State
*g
)
311 jit_State
*J
= G2J(g
);
312 #ifdef LUA_USE_ASSERT
313 { /* This assumes all traces have already been freed. */
315 for (i
= 1; i
< (ptrdiff_t)J
->sizetrace
; i
++)
316 lua_assert(i
== (ptrdiff_t)J
->cur
.traceno
|| traceref(J
, i
) == NULL
);
320 lj_ir_k64_freeall(J
);
321 lj_mem_freevec(g
, J
->snapmapbuf
, J
->sizesnapmap
, SnapEntry
);
322 lj_mem_freevec(g
, J
->snapbuf
, J
->sizesnap
, SnapShot
);
323 lj_mem_freevec(g
, J
->irbuf
+ J
->irbotlim
, J
->irtoplim
- J
->irbotlim
, IRIns
);
324 lj_mem_freevec(g
, J
->trace
, J
->sizetrace
, GCRef
);
327 /* -- Penalties and blacklisting ------------------------------------------ */
329 /* Blacklist a bytecode instruction. */
330 static void blacklist_pc(GCproto
*pt
, BCIns
*pc
)
332 setbc_op(pc
, (int)bc_op(*pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
333 pt
->flags
|= PROTO_ILOOP
;
336 /* Penalize a bytecode instruction. */
337 static void penalty_pc(jit_State
*J
, GCproto
*pt
, BCIns
*pc
, TraceError e
)
339 uint32_t i
, val
= PENALTY_MIN
;
340 for (i
= 0; i
< PENALTY_SLOTS
; i
++)
341 if (mref(J
->penalty
[i
].pc
, const BCIns
) == pc
) { /* Cache slot found? */
342 /* First try to bump its hotcount several times. */
343 val
= ((uint32_t)J
->penalty
[i
].val
<< 1) +
344 LJ_PRNG_BITS(J
, PENALTY_RNDBITS
);
345 if (val
> PENALTY_MAX
) {
346 blacklist_pc(pt
, pc
); /* Blacklist it, if that didn't help. */
351 /* Assign a new penalty cache slot. */
353 J
->penaltyslot
= (J
->penaltyslot
+ 1) & (PENALTY_SLOTS
-1);
354 setmref(J
->penalty
[i
].pc
, pc
);
356 J
->penalty
[i
].val
= (uint16_t)val
;
357 J
->penalty
[i
].reason
= e
;
358 hotcount_set(J2GG(J
), pc
+1, val
);
361 /* -- Trace compiler state machine ---------------------------------------- */
364 static void trace_start(jit_State
*J
)
369 if ((J
->pt
->flags
& PROTO_NOJIT
)) { /* JIT disabled for this proto? */
370 if (J
->parent
== 0 && J
->exitno
== 0) {
371 /* Lazy bytecode patching to disable hotcount events. */
372 lua_assert(bc_op(*J
->pc
) == BC_FORL
|| bc_op(*J
->pc
) == BC_ITERL
||
373 bc_op(*J
->pc
) == BC_LOOP
|| bc_op(*J
->pc
) == BC_FUNCF
);
374 setbc_op(J
->pc
, (int)bc_op(*J
->pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
375 J
->pt
->flags
|= PROTO_ILOOP
;
377 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
381 /* Get a new trace number. */
382 traceno
= trace_findfree(J
);
383 if (LJ_UNLIKELY(traceno
== 0)) { /* No free trace? */
384 lua_assert((J2G(J
)->hookmask
& HOOK_GC
) == 0);
385 lj_trace_flushall(J
->L
);
386 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
389 setgcrefp(J
->trace
[traceno
], &J
->cur
);
391 /* Setup enough of the current trace to be able to send the vmevent. */
392 memset(&J
->cur
, 0, sizeof(GCtrace
));
393 J
->cur
.traceno
= traceno
;
394 J
->cur
.nins
= J
->cur
.nk
= REF_BASE
;
395 J
->cur
.ir
= J
->irbuf
;
396 J
->cur
.snap
= J
->snapbuf
;
397 J
->cur
.snapmap
= J
->snapmapbuf
;
401 J
->guardemit
.irt
= 0;
402 J
->postproc
= LJ_POST_NONE
;
405 setgcref(J
->cur
.startpt
, obj2gco(J
->pt
));
408 lj_vmevent_send(L
, TRACE
,
409 setstrV(L
, L
->top
++, lj_str_newlit(L
, "start"));
410 setintV(L
->top
++, traceno
);
411 setfuncV(L
, L
->top
++, J
->fn
);
412 setintV(L
->top
++, proto_bcpos(J
->pt
, J
->pc
));
414 setintV(L
->top
++, J
->parent
);
415 setintV(L
->top
++, J
->exitno
);
422 static void trace_stop(jit_State
*J
)
424 BCIns
*pc
= mref(J
->cur
.startpc
, BCIns
);
425 BCOp op
= bc_op(J
->cur
.startins
);
426 GCproto
*pt
= &gcref(J
->cur
.startpt
)->pt
;
427 TraceNo traceno
= J
->cur
.traceno
;
428 GCtrace
*T
= trace_save_alloc(J
); /* Do this first. May throw OOM. */
433 setbc_op(pc
+bc_j(J
->cur
.startins
), BC_JFORI
); /* Patch FORI, too. */
438 /* Patch bytecode of starting instruction in root trace. */
439 setbc_op(pc
, (int)op
+(int)BC_JLOOP
-(int)BC_LOOP
);
440 setbc_d(pc
, traceno
);
442 /* Add to root trace chain in prototype. */
443 J
->cur
.nextroot
= pt
->trace
;
444 pt
->trace
= (TraceNo1
)traceno
;
449 *pc
= BCINS_AD(BC_JLOOP
, J
->cur
.snap
[0].nslots
, traceno
);
452 /* Patch exit branch in parent to side trace entry. */
453 lua_assert(J
->parent
!= 0 && J
->cur
.root
!= 0);
454 lj_asm_patchexit(J
, traceref(J
, J
->parent
), J
->exitno
, J
->cur
.mcode
);
455 /* Avoid compiling a side trace twice (stack resizing uses parent exit). */
456 traceref(J
, J
->parent
)->snap
[J
->exitno
].count
= SNAPCOUNT_DONE
;
457 /* Add to side trace chain in root trace. */
459 GCtrace
*root
= traceref(J
, J
->cur
.root
);
461 J
->cur
.nextside
= root
->nextside
;
462 root
->nextside
= (TraceNo1
)traceno
;
468 /* Trace stitching: patch link of previous trace. */
469 traceref(J
, J
->exitno
)->link
= traceno
;
476 /* Commit new mcode only after all patching is done. */
477 lj_mcode_commit(J
, J
->cur
.mcode
);
478 J
->postproc
= LJ_POST_NONE
;
482 lj_vmevent_send(L
, TRACE
,
483 setstrV(L
, L
->top
++, lj_str_newlit(L
, "stop"));
484 setintV(L
->top
++, traceno
);
485 setfuncV(L
, L
->top
++, J
->fn
);
489 /* Start a new root trace for down-recursion. */
490 static int trace_downrec(jit_State
*J
)
492 /* Restart recording at the return instruction. */
493 lua_assert(J
->pt
!= NULL
);
494 lua_assert(bc_isret(bc_op(*J
->pc
)));
495 if (bc_op(*J
->pc
) == BC_RETM
)
496 return 0; /* NYI: down-recursion with RETM. */
499 J
->state
= LJ_TRACE_RECORD
;
505 static int trace_abort(jit_State
*J
)
508 TraceError e
= LJ_TRERR_RECERR
;
511 J
->postproc
= LJ_POST_NONE
;
513 if (tvisnumber(L
->top
-1))
514 e
= (TraceError
)numberVint(L
->top
-1);
515 if (e
== LJ_TRERR_MCODELM
) {
516 L
->top
--; /* Remove error object */
517 J
->state
= LJ_TRACE_ASM
;
518 return 1; /* Retry ASM with new MCode area. */
520 /* Penalize or blacklist starting bytecode instruction. */
521 if (J
->parent
== 0 && !bc_isret(bc_op(J
->cur
.startins
))) {
522 if (J
->exitno
== 0) {
523 BCIns
*startpc
= mref(J
->cur
.startpc
, BCIns
);
524 if (e
== LJ_TRERR_RETRY
)
525 hotcount_set(J2GG(J
), startpc
+1, 1); /* Immediate retry. */
527 penalty_pc(J
, &gcref(J
->cur
.startpt
)->pt
, startpc
, e
);
529 traceref(J
, J
->exitno
)->link
= J
->exitno
; /* Self-link is blacklisted. */
533 /* Is there anything to abort? */
534 traceno
= J
->cur
.traceno
;
536 ptrdiff_t errobj
= savestack(L
, L
->top
-1); /* Stack may be resized. */
538 J
->cur
.linktype
= LJ_TRLINK_NONE
;
539 lj_vmevent_send(L
, TRACE
,
543 setstrV(L
, L
->top
++, lj_str_newlit(L
, "abort"));
544 setintV(L
->top
++, traceno
);
545 /* Find original Lua function call to generate a better error message. */
546 frame
= J
->L
->base
-1;
548 while (!isluafunc(frame_func(frame
))) {
549 pc
= (frame_iscont(frame
) ? frame_contpc(frame
) : frame_pc(frame
)) - 1;
550 frame
= frame_prev(frame
);
552 fn
= frame_func(frame
);
553 setfuncV(L
, L
->top
++, fn
);
554 setintV(L
->top
++, proto_bcpos(funcproto(fn
), pc
));
555 copyTV(L
, L
->top
++, restorestack(L
, errobj
));
556 copyTV(L
, L
->top
++, &J
->errinfo
);
558 /* Drop aborted trace after the vmevent (which may still access it). */
559 setgcrefnull(J
->trace
[traceno
]);
560 if (traceno
< J
->freetrace
)
561 J
->freetrace
= traceno
;
564 L
->top
--; /* Remove error object */
565 if (e
== LJ_TRERR_DOWNREC
)
566 return trace_downrec(J
);
567 else if (e
== LJ_TRERR_MCODEAL
)
568 lj_trace_flushall(L
);
572 /* Perform pending re-patch of a bytecode instruction. */
573 static LJ_AINLINE
void trace_pendpatch(jit_State
*J
, int force
)
575 if (LJ_UNLIKELY(J
->patchpc
)) {
576 if (force
|| J
->bcskip
== 0) {
577 *J
->patchpc
= J
->patchins
;
585 /* State machine for the trace compiler. Protected callback. */
586 static TValue
*trace_state(lua_State
*L
, lua_CFunction dummy
, void *ud
)
588 jit_State
*J
= (jit_State
*)ud
;
594 J
->state
= LJ_TRACE_RECORD
; /* trace_start() may change state. */
596 lj_dispatch_update(J2G(J
));
599 case LJ_TRACE_RECORD
:
600 trace_pendpatch(J
, 0);
601 setvmstate(J2G(J
), RECORD
);
602 lj_vmevent_send_(L
, RECORD
,
603 /* Save/restore tmptv state for trace recorder. */
604 TValue savetv
= J2G(J
)->tmptv
;
605 TValue savetv2
= J2G(J
)->tmptv2
;
606 setintV(L
->top
++, J
->cur
.traceno
);
607 setfuncV(L
, L
->top
++, J
->fn
);
608 setintV(L
->top
++, J
->pt
? (int32_t)proto_bcpos(J
->pt
, J
->pc
) : -1);
609 setintV(L
->top
++, J
->framedepth
);
611 J2G(J
)->tmptv
= savetv
;
612 J2G(J
)->tmptv2
= savetv2
;
618 trace_pendpatch(J
, 1);
620 if ((J
->flags
& JIT_F_OPT_LOOP
) &&
621 J
->cur
.link
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
622 setvmstate(J2G(J
), OPT
);
624 if (lj_opt_loop(J
)) { /* Loop optimization failed? */
626 J
->cur
.linktype
= LJ_TRLINK_NONE
;
627 J
->loopref
= J
->cur
.nins
;
628 J
->state
= LJ_TRACE_RECORD
; /* Try to continue recording. */
631 J
->loopref
= J
->chain
[IR_LOOP
]; /* Needed by assembler. */
635 if (!J
->loopref
) J
->cur
.snap
[J
->cur
.nsnap
-1].count
= SNAPCOUNT_DONE
;
636 J
->state
= LJ_TRACE_ASM
;
640 setvmstate(J2G(J
), ASM
);
641 lj_asm_trace(J
, &J
->cur
);
643 setvmstate(J2G(J
), INTERP
);
644 J
->state
= LJ_TRACE_IDLE
;
645 lj_dispatch_update(J2G(J
));
648 default: /* Trace aborted asynchronously. */
649 setintV(L
->top
++, (int32_t)LJ_TRERR_RECERR
);
652 trace_pendpatch(J
, 1);
655 setvmstate(J2G(J
), INTERP
);
656 J
->state
= LJ_TRACE_IDLE
;
657 lj_dispatch_update(J2G(J
));
660 } while (J
->state
> LJ_TRACE_RECORD
);
664 /* -- Event handling ------------------------------------------------------ */
666 /* A bytecode instruction is about to be executed. Record it. */
667 void lj_trace_ins(jit_State
*J
, const BCIns
*pc
)
669 /* Note: J->L must already be set. pc is the true bytecode PC here. */
671 J
->fn
= curr_func(J
->L
);
672 J
->pt
= isluafunc(J
->fn
) ? funcproto(J
->fn
) : NULL
;
673 while (lj_vm_cpcall(J
->L
, NULL
, (void *)J
, trace_state
) != 0)
674 J
->state
= LJ_TRACE_ERR
;
677 /* A hotcount triggered. Start recording a root trace. */
678 void LJ_FASTCALL
lj_trace_hot(jit_State
*J
, const BCIns
*pc
)
680 /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */
682 /* Reset hotcount. */
683 hotcount_set(J2GG(J
), pc
, J
->param
[JIT_P_hotloop
]*HOTCOUNT_LOOP
);
684 /* Only start a new trace if not recording or inside __gc call or vmevent. */
685 if (J
->state
== LJ_TRACE_IDLE
&&
686 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
687 J
->parent
= 0; /* Root trace. */
689 J
->state
= LJ_TRACE_START
;
690 lj_trace_ins(J
, pc
-1);
695 /* Check for a hot side exit. If yes, start recording a side trace. */
696 static void trace_hotside(jit_State
*J
, const BCIns
*pc
)
698 SnapShot
*snap
= &traceref(J
, J
->parent
)->snap
[J
->exitno
];
699 if (!(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
)) &&
700 isluafunc(curr_func(J
->L
)) &&
701 snap
->count
!= SNAPCOUNT_DONE
&&
702 ++snap
->count
>= J
->param
[JIT_P_hotexit
]) {
703 lua_assert(J
->state
== LJ_TRACE_IDLE
);
704 /* J->parent is non-zero for a side trace. */
705 J
->state
= LJ_TRACE_START
;
710 /* Stitch a new trace to the previous trace. */
711 void LJ_FASTCALL
lj_trace_stitch(jit_State
*J
, const BCIns
*pc
)
713 /* Only start a new trace if not recording or inside __gc call or vmevent. */
714 if (J
->state
== LJ_TRACE_IDLE
&&
715 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
716 J
->parent
= 0; /* Have to treat it like a root trace. */
717 /* J->exitno is set to the invoking trace. */
718 J
->state
= LJ_TRACE_START
;
724 /* Tiny struct to pass data to protected call. */
725 typedef struct ExitDataCP
{
727 void *exptr
; /* Pointer to exit state. */
728 const BCIns
*pc
; /* Restart interpreter at this PC. */
731 /* Need to protect lj_snap_restore because it may throw. */
732 static TValue
*trace_exit_cp(lua_State
*L
, lua_CFunction dummy
, void *ud
)
734 ExitDataCP
*exd
= (ExitDataCP
*)ud
;
735 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
736 exd
->pc
= lj_snap_restore(exd
->J
, exd
->exptr
);
741 #ifndef LUAJIT_DISABLE_VMEVENT
742 /* Push all registers from exit state. */
743 static void trace_exit_regs(lua_State
*L
, ExitState
*ex
)
746 setintV(L
->top
++, RID_NUM_GPR
);
747 setintV(L
->top
++, RID_NUM_FPR
);
748 for (i
= 0; i
< RID_NUM_GPR
; i
++) {
749 if (sizeof(ex
->gpr
[i
]) == sizeof(int32_t))
750 setintV(L
->top
++, (int32_t)ex
->gpr
[i
]);
752 setnumV(L
->top
++, (lua_Number
)ex
->gpr
[i
]);
755 for (i
= 0; i
< RID_NUM_FPR
; i
++) {
756 setnumV(L
->top
, ex
->fpr
[i
]);
757 if (LJ_UNLIKELY(tvisnan(L
->top
)))
765 #ifdef EXITSTATE_PCREG
766 /* Determine trace number from pc of exit instruction. */
767 static TraceNo
trace_exit_find(jit_State
*J
, MCode
*pc
)
770 for (traceno
= 1; traceno
< J
->sizetrace
; traceno
++) {
771 GCtrace
*T
= traceref(J
, traceno
);
772 if (T
&& pc
>= T
->mcode
&& pc
< (MCode
*)((char *)T
->mcode
+ T
->szmcode
))
780 /* A trace exited. Restore interpreter state. */
781 int LJ_FASTCALL
lj_trace_exit(jit_State
*J
, void *exptr
)
785 ExitState
*ex
= (ExitState
*)exptr
;
791 #ifdef EXITSTATE_PCREG
792 J
->parent
= trace_exit_find(J
, (MCode
*)(intptr_t)ex
->gpr
[EXITSTATE_PCREG
]);
794 T
= traceref(J
, J
->parent
); UNUSED(T
);
795 #ifdef EXITSTATE_CHECKEXIT
796 if (J
->exitno
== T
->nsnap
) { /* Treat stack check like a parent exit. */
797 lua_assert(T
->root
!= 0);
798 J
->exitno
= T
->ir
[REF_BASE
].op2
;
799 J
->parent
= T
->ir
[REF_BASE
].op1
;
800 T
= traceref(J
, J
->parent
);
803 lua_assert(T
!= NULL
&& J
->exitno
< T
->nsnap
);
806 errcode
= lj_vm_cpcall(L
, NULL
, &exd
, trace_exit_cp
);
808 return -errcode
; /* Return negated error code. */
810 if (!(LJ_HASPROFILE
&& (G(L
)->hookmask
& HOOK_PROFILE
)))
811 lj_vmevent_send(L
, TEXIT
,
812 lj_state_checkstack(L
, 4+RID_NUM_GPR
+RID_NUM_FPR
+LUA_MINSTACK
);
813 setintV(L
->top
++, J
->parent
);
814 setintV(L
->top
++, J
->exitno
);
815 trace_exit_regs(L
, ex
);
819 cf
= cframe_raw(L
->cframe
);
820 setcframe_pc(cf
, pc
);
821 if (LJ_HASPROFILE
&& (G(L
)->hookmask
& HOOK_PROFILE
)) {
822 /* Just exit to interpreter. */
823 } else if (G(L
)->gc
.state
== GCSatomic
|| G(L
)->gc
.state
== GCSfinalize
) {
824 if (!(G(L
)->hookmask
& HOOK_GC
))
825 lj_gc_step(L
); /* Exited because of GC: drive GC forward. */
827 trace_hotside(J
, pc
);
829 if (bc_op(*pc
) == BC_JLOOP
) {
830 BCIns
*retpc
= &traceref(J
, bc_d(*pc
))->startins
;
831 if (bc_isret(bc_op(*retpc
))) {
832 if (J
->state
== LJ_TRACE_RECORD
) {
834 J
->patchpc
= (BCIns
*)pc
;
835 *J
->patchpc
= *retpc
;
839 setcframe_pc(cf
, pc
);
843 /* Return MULTRES or 0. */
845 switch (bc_op(*pc
)) {
846 case BC_CALLM
: case BC_CALLMT
:
847 return (int)((BCReg
)(L
->top
- L
->base
) - bc_a(*pc
) - bc_c(*pc
) + LJ_FR2
);
849 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
) - bc_d(*pc
));
851 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
));
853 if (bc_op(*pc
) >= BC_FUNCF
)
854 return (int)((BCReg
)(L
->top
- L
->base
) + 1);