3 ** Copyright (C) 2005-2011 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) {
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
;
461 /* Commit new mcode only after all patching is done. */
462 lj_mcode_commit(J
, J
->cur
.mcode
);
463 J
->postproc
= LJ_POST_NONE
;
467 lj_vmevent_send(L
, TRACE
,
468 setstrV(L
, L
->top
++, lj_str_newlit(L
, "stop"));
469 setintV(L
->top
++, traceno
);
473 /* Start a new root trace for down-recursion. */
474 static int trace_downrec(jit_State
*J
)
476 /* Restart recording at the return instruction. */
477 lua_assert(J
->pt
!= NULL
);
478 lua_assert(bc_isret(bc_op(*J
->pc
)));
479 if (bc_op(*J
->pc
) == BC_RETM
)
480 return 0; /* NYI: down-recursion with RETM. */
483 J
->state
= LJ_TRACE_RECORD
;
489 static int trace_abort(jit_State
*J
)
492 TraceError e
= LJ_TRERR_RECERR
;
495 J
->postproc
= LJ_POST_NONE
;
497 if (tvisnumber(L
->top
-1))
498 e
= (TraceError
)numberVint(L
->top
-1);
499 if (e
== LJ_TRERR_MCODELM
) {
500 L
->top
--; /* Remove error object */
501 J
->state
= LJ_TRACE_ASM
;
502 return 1; /* Retry ASM with new MCode area. */
504 /* Penalize or blacklist starting bytecode instruction. */
505 if (J
->parent
== 0 && !bc_isret(bc_op(J
->cur
.startins
)))
506 penalty_pc(J
, &gcref(J
->cur
.startpt
)->pt
, mref(J
->cur
.startpc
, BCIns
), e
);
508 /* Is there anything to abort? */
509 traceno
= J
->cur
.traceno
;
511 ptrdiff_t errobj
= savestack(L
, L
->top
-1); /* Stack may be resized. */
513 J
->cur
.linktype
= LJ_TRLINK_NONE
;
514 lj_vmevent_send(L
, TRACE
,
518 setstrV(L
, L
->top
++, lj_str_newlit(L
, "abort"));
519 setintV(L
->top
++, traceno
);
520 /* Find original Lua function call to generate a better error message. */
521 frame
= J
->L
->base
-1;
523 while (!isluafunc(frame_func(frame
))) {
524 pc
= (frame_iscont(frame
) ? frame_contpc(frame
) : frame_pc(frame
)) - 1;
525 frame
= frame_prev(frame
);
527 fn
= frame_func(frame
);
528 setfuncV(L
, L
->top
++, fn
);
529 setintV(L
->top
++, proto_bcpos(funcproto(fn
), pc
));
530 copyTV(L
, L
->top
++, restorestack(L
, errobj
));
531 copyTV(L
, L
->top
++, &J
->errinfo
);
533 /* Drop aborted trace after the vmevent (which may still access it). */
534 setgcrefnull(J
->trace
[traceno
]);
535 if (traceno
< J
->freetrace
)
536 J
->freetrace
= traceno
;
539 L
->top
--; /* Remove error object */
540 if (e
== LJ_TRERR_DOWNREC
)
541 return trace_downrec(J
);
542 else if (e
== LJ_TRERR_MCODEAL
)
543 lj_trace_flushall(L
);
547 /* Perform pending re-patch of a bytecode instruction. */
548 static LJ_AINLINE
void trace_pendpatch(jit_State
*J
, int force
)
550 if (LJ_UNLIKELY(J
->patchpc
)) {
551 if (force
|| J
->bcskip
== 0) {
552 *J
->patchpc
= J
->patchins
;
560 /* State machine for the trace compiler. Protected callback. */
561 static TValue
*trace_state(lua_State
*L
, lua_CFunction dummy
, void *ud
)
563 jit_State
*J
= (jit_State
*)ud
;
569 J
->state
= LJ_TRACE_RECORD
; /* trace_start() may change state. */
571 lj_dispatch_update(J2G(J
));
574 case LJ_TRACE_RECORD
:
575 trace_pendpatch(J
, 0);
576 setvmstate(J2G(J
), RECORD
);
577 lj_vmevent_send_(L
, RECORD
,
578 /* Save/restore tmptv state for trace recorder. */
579 TValue savetv
= J2G(J
)->tmptv
;
580 TValue savetv2
= J2G(J
)->tmptv2
;
581 setintV(L
->top
++, J
->cur
.traceno
);
582 setfuncV(L
, L
->top
++, J
->fn
);
583 setintV(L
->top
++, J
->pt
? (int32_t)proto_bcpos(J
->pt
, J
->pc
) : -1);
584 setintV(L
->top
++, J
->framedepth
);
586 J2G(J
)->tmptv
= savetv
;
587 J2G(J
)->tmptv2
= savetv2
;
593 trace_pendpatch(J
, 1);
595 if ((J
->flags
& JIT_F_OPT_LOOP
) &&
596 J
->cur
.link
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
597 setvmstate(J2G(J
), OPT
);
599 if (lj_opt_loop(J
)) { /* Loop optimization failed? */
601 J
->cur
.linktype
= LJ_TRLINK_NONE
;
602 J
->loopref
= J
->cur
.nins
;
603 J
->state
= LJ_TRACE_RECORD
; /* Try to continue recording. */
606 J
->loopref
= J
->chain
[IR_LOOP
]; /* Needed by assembler. */
609 J
->state
= LJ_TRACE_ASM
;
613 setvmstate(J2G(J
), ASM
);
614 lj_asm_trace(J
, &J
->cur
);
616 setvmstate(J2G(J
), INTERP
);
617 J
->state
= LJ_TRACE_IDLE
;
618 lj_dispatch_update(J2G(J
));
621 default: /* Trace aborted asynchronously. */
622 setintV(L
->top
++, (int32_t)LJ_TRERR_RECERR
);
625 trace_pendpatch(J
, 1);
628 setvmstate(J2G(J
), INTERP
);
629 J
->state
= LJ_TRACE_IDLE
;
630 lj_dispatch_update(J2G(J
));
633 } while (J
->state
> LJ_TRACE_RECORD
);
637 /* -- Event handling ------------------------------------------------------ */
639 /* A bytecode instruction is about to be executed. Record it. */
640 void lj_trace_ins(jit_State
*J
, const BCIns
*pc
)
642 /* Note: J->L must already be set. pc is the true bytecode PC here. */
644 J
->fn
= curr_func(J
->L
);
645 J
->pt
= isluafunc(J
->fn
) ? funcproto(J
->fn
) : NULL
;
646 while (lj_vm_cpcall(J
->L
, NULL
, (void *)J
, trace_state
) != 0)
647 J
->state
= LJ_TRACE_ERR
;
650 /* A hotcount triggered. Start recording a root trace. */
651 void LJ_FASTCALL
lj_trace_hot(jit_State
*J
, const BCIns
*pc
)
653 /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */
655 /* Reset hotcount. */
656 hotcount_set(J2GG(J
), pc
, J
->param
[JIT_P_hotloop
]*HOTCOUNT_LOOP
);
657 /* Only start a new trace if not recording or inside __gc call or vmevent. */
658 if (J
->state
== LJ_TRACE_IDLE
&&
659 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
660 J
->parent
= 0; /* Root trace. */
662 J
->state
= LJ_TRACE_START
;
663 lj_trace_ins(J
, pc
-1);
668 /* Check for a hot side exit. If yes, start recording a side trace. */
669 static void trace_hotside(jit_State
*J
, const BCIns
*pc
)
671 SnapShot
*snap
= &traceref(J
, J
->parent
)->snap
[J
->exitno
];
672 if (!(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
)) &&
673 snap
->count
!= SNAPCOUNT_DONE
&&
674 ++snap
->count
>= J
->param
[JIT_P_hotexit
]) {
675 lua_assert(J
->state
== LJ_TRACE_IDLE
);
676 /* J->parent is non-zero for a side trace. */
677 J
->state
= LJ_TRACE_START
;
682 /* Tiny struct to pass data to protected call. */
683 typedef struct ExitDataCP
{
685 void *exptr
; /* Pointer to exit state. */
686 const BCIns
*pc
; /* Restart interpreter at this PC. */
689 /* Need to protect lj_snap_restore because it may throw. */
690 static TValue
*trace_exit_cp(lua_State
*L
, lua_CFunction dummy
, void *ud
)
692 ExitDataCP
*exd
= (ExitDataCP
*)ud
;
693 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
694 exd
->pc
= lj_snap_restore(exd
->J
, exd
->exptr
);
699 #ifndef LUAJIT_DISABLE_VMEVENT
700 /* Push all registers from exit state. */
701 static void trace_exit_regs(lua_State
*L
, ExitState
*ex
)
704 setintV(L
->top
++, RID_NUM_GPR
);
705 setintV(L
->top
++, RID_NUM_FPR
);
706 for (i
= 0; i
< RID_NUM_GPR
; i
++) {
707 if (sizeof(ex
->gpr
[i
]) == sizeof(int32_t))
708 setintV(L
->top
++, (int32_t)ex
->gpr
[i
]);
710 setnumV(L
->top
++, (lua_Number
)ex
->gpr
[i
]);
713 for (i
= 0; i
< RID_NUM_FPR
; i
++) {
714 setnumV(L
->top
, ex
->fpr
[i
]);
715 if (LJ_UNLIKELY(tvisnan(L
->top
)))
723 #ifdef EXITSTATE_PCREG
724 /* Determine trace number from pc of exit instruction. */
725 static TraceNo
trace_exit_find(jit_State
*J
, MCode
*pc
)
728 for (traceno
= 1; traceno
< J
->sizetrace
; traceno
++) {
729 GCtrace
*T
= traceref(J
, traceno
);
730 if (T
&& pc
>= T
->mcode
&& pc
< (MCode
*)((char *)T
->mcode
+ T
->szmcode
))
738 /* A trace exited. Restore interpreter state. */
739 int LJ_FASTCALL
lj_trace_exit(jit_State
*J
, void *exptr
)
743 ExitState
*ex
= (ExitState
*)exptr
;
749 #ifdef EXITSTATE_PCREG
750 J
->parent
= trace_exit_find(J
, (MCode
*)(intptr_t)ex
->gpr
[EXITSTATE_PCREG
]);
752 T
= traceref(J
, J
->parent
); UNUSED(T
);
753 #ifdef EXITSTATE_CHECKEXIT
754 if (J
->exitno
== T
->nsnap
) { /* Treat stack check like a parent exit. */
755 lua_assert(T
->root
!= 0);
756 J
->exitno
= T
->ir
[REF_BASE
].op2
;
757 J
->parent
= T
->ir
[REF_BASE
].op1
;
758 T
= traceref(J
, J
->parent
);
761 lua_assert(T
!= NULL
&& J
->exitno
< T
->nsnap
);
764 errcode
= lj_vm_cpcall(L
, NULL
, &exd
, trace_exit_cp
);
766 return -errcode
; /* Return negated error code. */
768 lj_vmevent_send(L
, TEXIT
,
769 lj_state_checkstack(L
, 4+RID_NUM_GPR
+RID_NUM_FPR
+LUA_MINSTACK
);
770 setintV(L
->top
++, J
->parent
);
771 setintV(L
->top
++, J
->exitno
);
772 trace_exit_regs(L
, ex
);
776 cf
= cframe_raw(L
->cframe
);
777 setcframe_pc(cf
, pc
);
778 if (G(L
)->gc
.state
== GCSatomic
|| G(L
)->gc
.state
== GCSfinalize
)
779 lj_gc_step(L
); /* Exited because of GC: drive GC forward. */
781 trace_hotside(J
, pc
);
782 if (bc_op(*pc
) == BC_JLOOP
) {
783 BCIns
*retpc
= &traceref(J
, bc_d(*pc
))->startins
;
784 if (bc_isret(bc_op(*retpc
))) {
785 if (J
->state
== LJ_TRACE_RECORD
) {
787 J
->patchpc
= (BCIns
*)pc
;
788 *J
->patchpc
= *retpc
;
792 setcframe_pc(cf
, pc
);
796 /* Return MULTRES or 0. */
798 switch (bc_op(*pc
)) {
799 case BC_CALLM
: case BC_CALLMT
:
800 return (int)((BCReg
)(L
->top
- L
->base
) - bc_a(*pc
) - bc_c(*pc
));
802 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
) - bc_d(*pc
));
804 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
));
806 if (bc_op(*pc
) >= BC_FUNCF
)
807 return (int)((BCReg
)(L
->top
- L
->base
) + 1);