3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
25 #include "lj_gdbjit.h"
26 #include "lj_record.h"
28 #include "lj_dispatch.h"
30 #include "lj_vmevent.h"
31 #include "lj_target.h"
33 /* -- Error handling ------------------------------------------------------ */
35 /* Synchronous abort with error message. */
36 void lj_trace_err(jit_State
*J
, TraceError e
)
38 setnilV(&J
->errinfo
); /* No error info. */
39 setintV(J
->L
->top
++, (int32_t)e
);
40 lj_err_throw(J
->L
, LUA_ERRRUN
);
43 /* Synchronous abort with error message and error info. */
44 void lj_trace_err_info(jit_State
*J
, TraceError e
)
46 setintV(J
->L
->top
++, (int32_t)e
);
47 lj_err_throw(J
->L
, LUA_ERRRUN
);
50 /* -- Trace management ---------------------------------------------------- */
52 /* The current trace is first assembled in J->cur. The variable length
53 ** arrays point to shared, growable buffers (J->irbuf etc.). When trace
54 ** recording ends successfully, the current trace and its data structures
55 ** are copied to a new (compact) GCtrace object.
58 /* Find a free trace number. */
59 static TraceNo
trace_findfree(jit_State
*J
)
62 if (J
->freetrace
== 0)
64 for (; J
->freetrace
< J
->sizetrace
; J
->freetrace
++)
65 if (traceref(J
, J
->freetrace
) == NULL
)
66 return J
->freetrace
++;
67 /* Need to grow trace array. */
68 lim
= (MSize
)J
->param
[JIT_P_maxtrace
] + 1;
69 if (lim
< 2) lim
= 2; else if (lim
> 65535) lim
= 65535;
72 return 0; /* Too many traces. */
73 lj_mem_growvec(J
->L
, J
->trace
, J
->sizetrace
, lim
, GCRef
);
74 for (; osz
< J
->sizetrace
; osz
++)
75 setgcrefnull(J
->trace
[osz
]);
79 #define TRACE_APPENDVEC(field, szfield, tp) \
81 memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \
82 p += J->cur.szfield*sizeof(tp);
84 #ifdef LUAJIT_USE_PERFTOOLS
86 ** Create symbol table of JIT-compiled code. For use with Linux perf tools.
88 ** perf record -f -e cycles luajit test.lua
89 ** perf report -s symbol
90 ** rm perf.data /tmp/perf-*.map
95 static void perftools_addtrace(GCtrace
*T
)
98 GCproto
*pt
= &gcref(T
->startpt
)->pt
;
99 const BCIns
*startpc
= mref(T
->startpc
, const BCIns
);
100 const char *name
= strdata(proto_chunkname(pt
));
102 if (name
[0] == '@' || name
[0] == '=')
106 if (startpc
>= proto_bc(pt
) && startpc
< proto_bc(pt
) + pt
->sizebc
)
107 lineno
= proto_line(pt
, proto_bcpos(pt
, startpc
));
109 lineno
= proto_line(pt
, 0); /* Wrong, but better than nothing. */
112 sprintf(fname
, "/tmp/perf-%d.map", getpid());
113 if (!(fp
= fopen(fname
, "w"))) return;
116 fprintf(fp
, "%lx %x TRACE_%d::%s:%u\n",
117 (long)T
->mcode
, T
->szmcode
, T
->traceno
, name
, lineno
);
121 /* Save current trace by copying and compacting it. */
122 static void trace_save(jit_State
*J
)
124 size_t sztr
= ((sizeof(GCtrace
)+7)&~7);
125 size_t szins
= (J
->cur
.nins
-J
->cur
.nk
)*sizeof(IRIns
);
126 size_t sz
= sztr
+ szins
+
127 J
->cur
.nsnap
*sizeof(SnapShot
) +
128 J
->cur
.nsnapmap
*sizeof(SnapEntry
);
129 GCtrace
*T
= lj_mem_newt(J
->L
, (MSize
)sz
, GCtrace
);
130 char *p
= (char *)T
+ sztr
;
131 memcpy(T
, &J
->cur
, sizeof(GCtrace
));
132 setgcrefr(T
->nextgc
, J2G(J
)->gc
.root
);
133 setgcrefp(J2G(J
)->gc
.root
, T
);
136 T
->ir
= (IRIns
*)p
- J
->cur
.nk
;
137 memcpy(p
, J
->cur
.ir
+J
->cur
.nk
, szins
);
139 TRACE_APPENDVEC(snap
, nsnap
, SnapShot
)
140 TRACE_APPENDVEC(snapmap
, nsnapmap
, SnapEntry
)
142 setgcrefp(J
->trace
[T
->traceno
], T
);
143 lj_gc_barriertrace(J2G(J
), T
->traceno
);
144 lj_gdbjit_addtrace(J
, T
);
145 #ifdef LUAJIT_USE_PERFTOOLS
146 perftools_addtrace(T
);
150 void LJ_FASTCALL
lj_trace_free(global_State
*g
, GCtrace
*T
)
152 jit_State
*J
= G2J(g
);
154 lj_gdbjit_deltrace(J
, T
);
155 if (T
->traceno
< J
->freetrace
)
156 J
->freetrace
= T
->traceno
;
157 setgcrefnull(J
->trace
[T
->traceno
]);
160 ((sizeof(GCtrace
)+7)&~7) + (T
->nins
-T
->nk
)*sizeof(IRIns
) +
161 T
->nsnap
*sizeof(SnapShot
) + T
->nsnapmap
*sizeof(SnapEntry
));
164 /* Re-enable compiling a prototype by unpatching any modified bytecode. */
165 void lj_trace_reenableproto(GCproto
*pt
)
167 if ((pt
->flags
& PROTO_HAS_ILOOP
)) {
168 BCIns
*bc
= proto_bc(pt
);
169 BCPos i
, sizebc
= pt
->sizebc
;;
170 pt
->flags
&= ~PROTO_HAS_ILOOP
;
171 if (bc_op(bc
[0]) == BC_IFUNCF
)
172 setbc_op(&bc
[0], BC_FUNCF
);
173 for (i
= 1; i
< sizebc
; i
++) {
174 BCOp op
= bc_op(bc
[i
]);
175 if (op
== BC_IFORL
|| op
== BC_IITERL
|| op
== BC_ILOOP
)
176 setbc_op(&bc
[i
], (int)op
+(int)BC_LOOP
-(int)BC_ILOOP
);
181 /* Unpatch the bytecode modified by a root trace. */
182 static void trace_unpatch(jit_State
*J
, GCtrace
*T
)
184 BCOp op
= bc_op(T
->startins
);
185 BCIns
*pc
= mref(T
->startpc
, BCIns
);
188 return; /* No need to unpatch branches in parent traces (yet). */
189 switch (bc_op(*pc
)) {
191 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
193 pc
+= bc_j(T
->startins
);
194 lua_assert(bc_op(*pc
) == BC_JFORI
);
195 setbc_op(pc
, BC_FORI
);
199 lua_assert(op
== BC_ITERL
|| op
== BC_LOOP
|| bc_isret(op
));
203 lua_assert(op
== BC_ITERL
);
205 if (bc_op(*pc
) == BC_JITERL
) {
206 lua_assert(traceref(J
, bc_d(*pc
)) == T
);
211 lua_assert(op
== BC_FUNCF
);
214 default: /* Already unpatched. */
219 /* Flush a root trace. */
220 static void trace_flushroot(jit_State
*J
, GCtrace
*T
)
222 GCproto
*pt
= &gcref(T
->startpt
)->pt
;
223 lua_assert(T
->root
== 0 && pt
!= NULL
);
224 /* First unpatch any modified bytecode. */
226 /* Unlink root trace from chain anchored in prototype. */
227 if (pt
->trace
== T
->traceno
) { /* Trace is first in chain. Easy. */
228 pt
->trace
= T
->nextroot
;
229 } else if (pt
->trace
) { /* Otherwise search in chain of root traces. */
230 GCtrace
*T2
= traceref(J
, pt
->trace
);
232 for (; T2
->nextroot
; T2
= traceref(J
, T2
->nextroot
))
233 if (T2
->nextroot
== T
->traceno
) {
234 T2
->nextroot
= T
->nextroot
; /* Unlink from chain. */
241 /* Flush a trace. Only root traces are considered. */
242 void lj_trace_flush(jit_State
*J
, TraceNo traceno
)
244 if (traceno
> 0 && traceno
< J
->sizetrace
) {
245 GCtrace
*T
= traceref(J
, traceno
);
246 if (T
&& T
->root
== 0)
247 trace_flushroot(J
, T
);
251 /* Flush all traces associated with a prototype. */
252 void lj_trace_flushproto(global_State
*g
, GCproto
*pt
)
254 while (pt
->trace
!= 0)
255 trace_flushroot(G2J(g
), traceref(G2J(g
), pt
->trace
));
258 /* Flush all traces. */
259 int lj_trace_flushall(lua_State
*L
)
261 jit_State
*J
= L2J(L
);
263 if ((J2G(J
)->hookmask
& HOOK_GC
))
265 for (i
= (ptrdiff_t)J
->sizetrace
-1; i
> 0; i
--) {
266 GCtrace
*T
= traceref(J
, i
);
269 trace_flushroot(J
, T
);
270 lj_gdbjit_deltrace(J
, T
);
272 setgcrefnull(J
->trace
[i
]);
277 /* Clear penalty cache. */
278 memset(J
->penalty
, 0, sizeof(J
->penalty
));
279 /* Free the whole machine code and invalidate all exit stub groups. */
281 memset(J
->exitstubgroup
, 0, sizeof(J
->exitstubgroup
));
282 lj_vmevent_send(L
, TRACE
,
283 setstrV(L
, L
->top
++, lj_str_newlit(L
, "flush"));
288 /* Initialize JIT compiler state. */
289 void lj_trace_initstate(global_State
*g
)
291 jit_State
*J
= G2J(g
);
293 /* Initialize SIMD constants. */
294 tv
= LJ_KSIMD(J
, LJ_KSIMD_ABS
);
295 tv
[0].u64
= U64x(7fffffff
,ffffffff
);
296 tv
[1].u64
= U64x(7fffffff
,ffffffff
);
297 tv
= LJ_KSIMD(J
, LJ_KSIMD_NEG
);
298 tv
[0].u64
= U64x(80000000,00000000);
299 tv
[1].u64
= U64x(80000000,00000000);
302 /* Free everything associated with the JIT compiler state. */
303 void lj_trace_freestate(global_State
*g
)
305 jit_State
*J
= G2J(g
);
306 #ifdef LUA_USE_ASSERT
307 { /* This assumes all traces have already been freed. */
309 for (i
= 1; i
< (ptrdiff_t)J
->sizetrace
; i
++)
310 lua_assert(i
== (ptrdiff_t)J
->cur
.traceno
|| traceref(J
, i
) == NULL
);
314 lj_ir_k64_freeall(J
);
315 lj_mem_freevec(g
, J
->snapmapbuf
, J
->sizesnapmap
, SnapEntry
);
316 lj_mem_freevec(g
, J
->snapbuf
, J
->sizesnap
, SnapShot
);
317 lj_mem_freevec(g
, J
->irbuf
+ J
->irbotlim
, J
->irtoplim
- J
->irbotlim
, IRIns
);
318 lj_mem_freevec(g
, J
->trace
, J
->sizetrace
, GCRef
);
321 /* -- Penalties and blacklisting ------------------------------------------ */
323 /* Blacklist a bytecode instruction. */
324 static void blacklist_pc(GCproto
*pt
, BCIns
*pc
)
326 setbc_op(pc
, (int)bc_op(*pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
327 pt
->flags
|= PROTO_HAS_ILOOP
;
330 /* Penalize a bytecode instruction. */
331 static void penalty_pc(jit_State
*J
, GCproto
*pt
, BCIns
*pc
, TraceError e
)
333 uint32_t i
, val
= PENALTY_MIN
;
334 for (i
= 0; i
< PENALTY_SLOTS
; i
++)
335 if (mref(J
->penalty
[i
].pc
, const BCIns
) == pc
) { /* Cache slot found? */
336 /* First try to bump its hotcount several times. */
337 val
= ((uint32_t)J
->penalty
[i
].val
<< 1) +
338 LJ_PRNG_BITS(J
, PENALTY_RNDBITS
);
339 if (val
> PENALTY_MAX
) {
340 blacklist_pc(pt
, pc
); /* Blacklist it, if that didn't help. */
345 /* Assign a new penalty cache slot. */
347 J
->penaltyslot
= (J
->penaltyslot
+ 1) & (PENALTY_SLOTS
-1);
348 setmref(J
->penalty
[i
].pc
, pc
);
350 J
->penalty
[i
].val
= (uint16_t)val
;
351 J
->penalty
[i
].reason
= e
;
352 hotcount_set(J2GG(J
), pc
+1, val
);
355 /* -- Trace compiler state machine ---------------------------------------- */
358 static void trace_start(jit_State
*J
)
363 if ((J
->pt
->flags
& PROTO_NO_JIT
)) { /* JIT disabled for this proto? */
364 if (J
->parent
== 0) {
365 /* Lazy bytecode patching to disable hotcount events. */
366 lua_assert(bc_op(*J
->pc
) == BC_FORL
|| bc_op(*J
->pc
) == BC_ITERL
||
367 bc_op(*J
->pc
) == BC_LOOP
|| bc_op(*J
->pc
) == BC_FUNCF
);
368 setbc_op(J
->pc
, (int)bc_op(*J
->pc
)+(int)BC_ILOOP
-(int)BC_LOOP
);
369 J
->pt
->flags
|= PROTO_HAS_ILOOP
;
371 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
375 /* Get a new trace number. */
376 traceno
= trace_findfree(J
);
377 if (LJ_UNLIKELY(traceno
== 0)) { /* No free trace? */
378 lua_assert((J2G(J
)->hookmask
& HOOK_GC
) == 0);
379 lj_trace_flushall(J
->L
);
380 J
->state
= LJ_TRACE_IDLE
; /* Silently ignored. */
383 setgcrefp(J
->trace
[traceno
], &J
->cur
);
385 /* Setup enough of the current trace to be able to send the vmevent. */
386 memset(&J
->cur
, 0, sizeof(GCtrace
));
387 J
->cur
.traceno
= traceno
;
388 J
->cur
.nins
= J
->cur
.nk
= REF_BASE
;
389 J
->cur
.ir
= J
->irbuf
;
390 J
->cur
.snap
= J
->snapbuf
;
391 J
->cur
.snapmap
= J
->snapmapbuf
;
395 J
->guardemit
.irt
= 0;
396 J
->postproc
= LJ_POST_NONE
;
398 setgcref(J
->cur
.startpt
, obj2gco(J
->pt
));
401 lj_vmevent_send(L
, TRACE
,
402 setstrV(L
, L
->top
++, lj_str_newlit(L
, "start"));
403 setintV(L
->top
++, traceno
);
404 setfuncV(L
, L
->top
++, J
->fn
);
405 setintV(L
->top
++, proto_bcpos(J
->pt
, J
->pc
));
407 setintV(L
->top
++, J
->parent
);
408 setintV(L
->top
++, J
->exitno
);
415 static void trace_stop(jit_State
*J
)
417 BCIns
*pc
= mref(J
->cur
.startpc
, BCIns
);
418 BCOp op
= bc_op(J
->cur
.startins
);
419 GCproto
*pt
= &gcref(J
->cur
.startpt
)->pt
;
420 TraceNo traceno
= J
->cur
.traceno
;
425 setbc_op(pc
+bc_j(J
->cur
.startins
), BC_JFORI
); /* Patch FORI, too. */
430 /* Patch bytecode of starting instruction in root trace. */
431 setbc_op(pc
, (int)op
+(int)BC_JLOOP
-(int)BC_LOOP
);
432 setbc_d(pc
, traceno
);
434 /* Add to root trace chain in prototype. */
435 J
->cur
.nextroot
= pt
->trace
;
436 pt
->trace
= (TraceNo1
)traceno
;
441 *pc
= BCINS_AD(BC_JLOOP
, J
->cur
.snap
[0].nslots
, traceno
);
444 /* Patch exit branch in parent to side trace entry. */
445 lua_assert(J
->parent
!= 0 && J
->cur
.root
!= 0);
446 lj_asm_patchexit(J
, traceref(J
, J
->parent
), J
->exitno
, J
->cur
.mcode
);
447 /* Avoid compiling a side trace twice (stack resizing uses parent exit). */
448 traceref(J
, J
->parent
)->snap
[J
->exitno
].count
= SNAPCOUNT_DONE
;
449 /* Add to side trace chain in root trace. */
451 GCtrace
*root
= traceref(J
, J
->cur
.root
);
453 J
->cur
.nextside
= root
->nextside
;
454 root
->nextside
= (TraceNo1
)traceno
;
462 /* Commit new mcode only after all patching is done. */
463 lj_mcode_commit(J
, J
->cur
.mcode
);
464 J
->postproc
= LJ_POST_NONE
;
468 lj_vmevent_send(L
, TRACE
,
469 setstrV(L
, L
->top
++, lj_str_newlit(L
, "stop"));
470 setintV(L
->top
++, traceno
);
474 /* Start a new root trace for down-recursion. */
475 static int trace_downrec(jit_State
*J
)
477 /* Restart recording at the return instruction. */
478 lua_assert(J
->pt
!= NULL
);
479 lua_assert(bc_isret(bc_op(*J
->pc
)));
480 if (bc_op(*J
->pc
) == BC_RETM
)
481 return 0; /* NYI: down-recursion with RETM. */
484 J
->state
= LJ_TRACE_RECORD
;
490 static int trace_abort(jit_State
*J
)
493 TraceError e
= LJ_TRERR_RECERR
;
496 J
->postproc
= LJ_POST_NONE
;
498 if (tvisnum(L
->top
-1))
499 e
= (TraceError
)lj_num2int(numV(L
->top
-1));
500 if (e
== LJ_TRERR_MCODELM
) {
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
, (BCIns
*)J
->startpc
, 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 lj_vmevent_send(L
, TRACE
,
517 setstrV(L
, L
->top
++, lj_str_newlit(L
, "abort"));
518 setintV(L
->top
++, traceno
);
519 /* Find original Lua function call to generate a better error message. */
520 frame
= J
->L
->base
-1;
522 while (!isluafunc(frame_func(frame
))) {
523 pc
= (frame_iscont(frame
) ? frame_contpc(frame
) : frame_pc(frame
)) - 1;
524 frame
= frame_prev(frame
);
526 fn
= frame_func(frame
);
527 setfuncV(L
, L
->top
++, fn
);
528 setintV(L
->top
++, proto_bcpos(funcproto(fn
), pc
));
529 copyTV(L
, L
->top
++, restorestack(L
, errobj
));
530 copyTV(L
, L
->top
++, &J
->errinfo
);
532 /* Drop aborted trace after the vmevent (which may still access it). */
533 setgcrefnull(J
->trace
[traceno
]);
534 if (traceno
< J
->freetrace
)
535 J
->freetrace
= traceno
;
538 L
->top
--; /* Remove error object */
539 if (e
== LJ_TRERR_DOWNREC
)
540 return trace_downrec(J
);
541 else if (e
== LJ_TRERR_MCODEAL
)
542 lj_trace_flushall(L
);
546 /* Perform pending re-patch of a bytecode instruction. */
547 static LJ_AINLINE
void trace_pendpatch(jit_State
*J
, int force
)
549 if (LJ_UNLIKELY(J
->patchpc
) && (force
|| J
->chain
[IR_RETF
])) {
550 *J
->patchpc
= J
->patchins
;
555 /* State machine for the trace compiler. Protected callback. */
556 static TValue
*trace_state(lua_State
*L
, lua_CFunction dummy
, void *ud
)
558 jit_State
*J
= (jit_State
*)ud
;
564 J
->state
= LJ_TRACE_RECORD
; /* trace_start() may change state. */
566 lj_dispatch_update(J2G(J
));
569 case LJ_TRACE_RECORD
:
570 trace_pendpatch(J
, 0);
571 setvmstate(J2G(J
), RECORD
);
572 lj_vmevent_send(L
, RECORD
,
573 setintV(L
->top
++, J
->cur
.traceno
);
574 setfuncV(L
, L
->top
++, J
->fn
);
575 setintV(L
->top
++, J
->pt
? (int32_t)proto_bcpos(J
->pt
, J
->pc
) : -1);
576 setintV(L
->top
++, J
->framedepth
);
582 trace_pendpatch(J
, 1);
584 if ((J
->flags
& JIT_F_OPT_LOOP
) &&
585 J
->cur
.link
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
586 setvmstate(J2G(J
), OPT
);
588 if (lj_opt_loop(J
)) { /* Loop optimization failed? */
590 J
->loopref
= J
->cur
.nins
;
591 J
->state
= LJ_TRACE_RECORD
; /* Try to continue recording. */
594 J
->loopref
= J
->chain
[IR_LOOP
]; /* Needed by assembler. */
597 J
->state
= LJ_TRACE_ASM
;
601 setvmstate(J2G(J
), ASM
);
602 lj_asm_trace(J
, &J
->cur
);
604 setvmstate(J2G(J
), INTERP
);
605 J
->state
= LJ_TRACE_IDLE
;
606 lj_dispatch_update(J2G(J
));
609 default: /* Trace aborted asynchronously. */
610 setintV(L
->top
++, (int32_t)LJ_TRERR_RECERR
);
613 trace_pendpatch(J
, 1);
616 setvmstate(J2G(J
), INTERP
);
617 J
->state
= LJ_TRACE_IDLE
;
618 lj_dispatch_update(J2G(J
));
621 } while (J
->state
> LJ_TRACE_RECORD
);
625 /* -- Event handling ------------------------------------------------------ */
627 /* A bytecode instruction is about to be executed. Record it. */
628 void lj_trace_ins(jit_State
*J
, const BCIns
*pc
)
630 /* Note: J->L must already be set. pc is the true bytecode PC here. */
632 J
->fn
= curr_func(J
->L
);
633 J
->pt
= isluafunc(J
->fn
) ? funcproto(J
->fn
) : NULL
;
634 while (lj_vm_cpcall(J
->L
, NULL
, (void *)J
, trace_state
) != 0)
635 J
->state
= LJ_TRACE_ERR
;
638 /* A hotcount triggered. Start recording a root trace. */
639 void LJ_FASTCALL
lj_trace_hot(jit_State
*J
, const BCIns
*pc
)
641 /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */
642 hotcount_set(J2GG(J
), pc
, J
->param
[JIT_P_hotloop
]+1); /* Reset hotcount. */
643 /* Only start a new trace if not recording or inside __gc call or vmevent. */
644 if (J
->state
== LJ_TRACE_IDLE
&&
645 !(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
))) {
646 J
->parent
= 0; /* Root trace. */
648 J
->state
= LJ_TRACE_START
;
649 lj_trace_ins(J
, pc
-1);
653 /* Check for a hot side exit. If yes, start recording a side trace. */
654 static void trace_hotside(jit_State
*J
, const BCIns
*pc
)
656 SnapShot
*snap
= &traceref(J
, J
->parent
)->snap
[J
->exitno
];
657 if (!(J2G(J
)->hookmask
& (HOOK_GC
|HOOK_VMEVENT
)) &&
658 snap
->count
!= SNAPCOUNT_DONE
&&
659 ++snap
->count
>= J
->param
[JIT_P_hotexit
]) {
660 lua_assert(J
->state
== LJ_TRACE_IDLE
);
661 /* J->parent is non-zero for a side trace. */
662 J
->state
= LJ_TRACE_START
;
667 /* Tiny struct to pass data to protected call. */
668 typedef struct ExitDataCP
{
670 void *exptr
; /* Pointer to exit state. */
671 const BCIns
*pc
; /* Restart interpreter at this PC. */
674 /* Need to protect lj_snap_restore because it may throw. */
675 static TValue
*trace_exit_cp(lua_State
*L
, lua_CFunction dummy
, void *ud
)
677 ExitDataCP
*exd
= (ExitDataCP
*)ud
;
678 cframe_errfunc(L
->cframe
) = -1; /* Inherit error function. */
679 exd
->pc
= lj_snap_restore(exd
->J
, exd
->exptr
);
684 /* A trace exited. Restore interpreter state. */
685 int LJ_FASTCALL
lj_trace_exit(jit_State
*J
, void *exptr
)
694 errcode
= lj_vm_cpcall(L
, NULL
, &exd
, trace_exit_cp
);
696 return -errcode
; /* Return negated error code. */
698 lj_vmevent_send(L
, TEXIT
,
699 ExitState
*ex
= (ExitState
*)exptr
;
701 lj_state_checkstack(L
, 4+RID_NUM_GPR
+RID_NUM_FPR
+LUA_MINSTACK
);
702 setintV(L
->top
++, J
->parent
);
703 setintV(L
->top
++, J
->exitno
);
704 setintV(L
->top
++, RID_NUM_GPR
);
705 setintV(L
->top
++, RID_NUM_FPR
);
706 for (i
= 0; i
< RID_NUM_GPR
; i
++)
707 setnumV(L
->top
++, cast_num(ex
->gpr
[i
]));
708 for (i
= 0; i
< RID_NUM_FPR
; i
++) {
709 setnumV(L
->top
, ex
->fpr
[i
]);
710 if (LJ_UNLIKELY(tvisnan(L
->top
)))
717 cf
= cframe_raw(L
->cframe
);
718 setcframe_pc(cf
, pc
);
719 if (G(L
)->gc
.state
== GCSatomic
|| G(L
)->gc
.state
== GCSfinalize
)
720 lj_gc_step(L
); /* Exited because of GC: drive GC forward. */
722 trace_hotside(J
, pc
);
723 if (bc_op(*pc
) == BC_JLOOP
) {
724 BCIns
*retpc
= &traceref(J
, bc_d(*pc
))->startins
;
725 if (bc_isret(bc_op(*retpc
))) {
726 if (J
->state
== LJ_TRACE_RECORD
) {
728 J
->patchpc
= (BCIns
*)pc
;
729 *J
->patchpc
= *retpc
;
732 setcframe_pc(cf
, pc
);
736 /* Return MULTRES or 0. */
737 switch (bc_op(*pc
)) {
738 case BC_CALLM
: case BC_CALLMT
:
739 return (int)((BCReg
)(L
->top
- L
->base
) - bc_a(*pc
) - bc_c(*pc
));
741 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
) - bc_d(*pc
));
743 return (int)((BCReg
)(L
->top
- L
->base
) + 1 - bc_a(*pc
));
745 if (bc_op(*pc
) >= BC_FUNCF
)
746 return (int)((BCReg
)(L
->top
- L
->base
) + 1);