2 ** Trace recorder (bytecode -> SSA IR).
3 ** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
25 #include "lj_ircall.h"
28 #include "lj_record.h"
29 #include "lj_ffrecord.h"
31 #include "lj_dispatch.h"
34 /* Some local macros to save typing. Undef'd at the end. */
35 #define IR(ref) (&J->cur.ir[(ref)])
37 /* Pass IR on to next optimization in chain (FOLD). */
38 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
40 /* Emit raw IR without passing through optimizations. */
41 #define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))
43 /* -- Sanity checks ------------------------------------------------------- */
46 /* Sanity check the whole IR -- sloooow. */
47 static void rec_check_ir(jit_State
*J
)
49 IRRef i
, nins
= J
->cur
.nins
, nk
= J
->cur
.nk
;
50 lua_assert(nk
<= REF_BIAS
&& nins
>= REF_BIAS
&& nins
< 65536);
51 for (i
= nins
-1; i
>= nk
; i
--) {
53 uint32_t mode
= lj_ir_mode
[ir
->o
];
56 switch (irm_op1(mode
)) {
57 case IRMnone
: lua_assert(op1
== 0); break;
58 case IRMref
: lua_assert(op1
>= nk
);
59 lua_assert(i
>= REF_BIAS
? op1
< i
: op1
> i
); break;
61 case IRMcst
: lua_assert(i
< REF_BIAS
); continue;
63 switch (irm_op2(mode
)) {
64 case IRMnone
: lua_assert(op2
== 0); break;
65 case IRMref
: lua_assert(op2
>= nk
);
66 lua_assert(i
>= REF_BIAS
? op2
< i
: op2
> i
); break;
68 case IRMcst
: lua_assert(0); break;
71 lua_assert(ir
->prev
>= nk
);
72 lua_assert(i
>= REF_BIAS
? ir
->prev
< i
: ir
->prev
> i
);
73 lua_assert(ir
->o
== IR_NOP
|| IR(ir
->prev
)->o
== ir
->o
);
78 /* Compare stack slots and frames of the recorder and the VM. */
79 static void rec_check_slots(jit_State
*J
)
81 BCReg s
, nslots
= J
->baseslot
+ J
->maxslot
;
83 cTValue
*base
= J
->L
->base
- J
->baseslot
;
84 lua_assert(J
->baseslot
>= 1);
85 lua_assert(J
->baseslot
== 1 || (J
->slot
[J
->baseslot
-1] & TREF_FRAME
));
86 lua_assert(nslots
<= LJ_MAX_JSLOTS
);
87 for (s
= 0; s
< nslots
; s
++) {
90 cTValue
*tv
= &base
[s
];
91 IRRef ref
= tref_ref(tr
);
93 lua_assert(ref
>= J
->cur
.nk
&& ref
< J
->cur
.nins
);
95 lua_assert(irt_t(ir
->t
) == tref_t(tr
));
97 lua_assert(tref_isfunc(tr
));
98 } else if ((tr
& TREF_FRAME
)) {
99 GCfunc
*fn
= gco2func(frame_gc(tv
));
100 BCReg delta
= (BCReg
)(tv
- frame_prev(tv
));
101 lua_assert(tref_isfunc(tr
));
102 if (tref_isk(tr
)) lua_assert(fn
== ir_kfunc(ir
));
103 lua_assert(s
> delta
? (J
->slot
[s
-delta
] & TREF_FRAME
) : (s
== delta
));
105 } else if ((tr
& TREF_CONT
)) {
106 lua_assert(ir_kptr(ir
) == gcrefp(tv
->gcr
, void));
107 lua_assert((J
->slot
[s
+1] & TREF_FRAME
));
111 lua_assert(tref_isnumber(tr
)); /* Could be IRT_INT etc., too. */
113 lua_assert(itype2irt(tv
) == tref_type(tr
));
114 if (tref_isk(tr
)) { /* Compare constants. */
116 lj_ir_kvalue(J
->L
, &tvk
, ir
);
117 if (!(tvisnum(&tvk
) && tvisnan(&tvk
)))
118 lua_assert(lj_obj_equal(tv
, &tvk
));
120 lua_assert(tvisnum(tv
) && tvisnan(tv
));
125 lua_assert(J
->framedepth
== depth
);
129 /* -- Type handling and specialization ------------------------------------ */
131 /* Note: these functions return tagged references (TRef). */
133 /* Specialize a slot to a specific type. Note: slot can be negative! */
134 static TRef
sloadt(jit_State
*J
, int32_t slot
, IRType t
, int mode
)
136 /* Caller may set IRT_GUARD in t. */
137 TRef ref
= emitir_raw(IRT(IR_SLOAD
, t
), (int32_t)J
->baseslot
+slot
, mode
);
142 /* Specialize a slot to the runtime type. Note: slot can be negative! */
143 static TRef
sload(jit_State
*J
, int32_t slot
)
145 IRType t
= itype2irt(&J
->L
->base
[slot
]);
146 TRef ref
= emitir_raw(IRTG(IR_SLOAD
, t
), (int32_t)J
->baseslot
+slot
,
148 if (irtype_ispri(t
)) ref
= TREF_PRI(t
); /* Canonicalize primitive refs. */
153 /* Get TRef from slot. Load slot and specialize if not done already. */
154 #define getslot(J, s) (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s)))
156 /* Get TRef for current function. */
157 static TRef
getcurrf(jit_State
*J
)
161 lua_assert(J
->baseslot
== 1);
162 return sloadt(J
, -1, IRT_FUNC
, IRSLOAD_READONLY
);
165 /* Compare for raw object equality.
166 ** Returns 0 if the objects are the same.
167 ** Returns 1 if they are different, but the same type.
168 ** Returns 2 for two different types.
169 ** Comparisons between primitives always return 1 -- no caller cares about it.
171 int lj_record_objcmp(jit_State
*J
, TRef a
, TRef b
, cTValue
*av
, cTValue
*bv
)
173 int diff
= !lj_obj_equal(av
, bv
);
174 if (!tref_isk2(a
, b
)) { /* Shortcut, also handles primitives. */
175 IRType ta
= tref_isinteger(a
) ? IRT_INT
: tref_type(a
);
176 IRType tb
= tref_isinteger(b
) ? IRT_INT
: tref_type(b
);
178 /* Widen mixed number/int comparisons to number/number comparison. */
179 if (ta
== IRT_INT
&& tb
== IRT_NUM
) {
180 a
= emitir(IRTN(IR_CONV
), a
, IRCONV_NUM_INT
);
182 } else if (ta
== IRT_NUM
&& tb
== IRT_INT
) {
183 b
= emitir(IRTN(IR_CONV
), b
, IRCONV_NUM_INT
);
185 return 2; /* Two different types are never equal. */
188 emitir(IRTG(diff
? IR_NE
: IR_EQ
, ta
), a
, b
);
193 /* Constify a value. Returns 0 for non-representable object types. */
194 TRef
lj_record_constify(jit_State
*J
, cTValue
*o
)
197 return lj_ir_kgc(J
, gcV(o
), itype2irt(o
));
199 return lj_ir_kint(J
, intV(o
));
201 return lj_ir_knumint(J
, numV(o
));
202 else if (tvisbool(o
))
203 return TREF_PRI(itype2irt(o
));
205 return 0; /* Can't represent lightuserdata (pointless). */
208 /* -- Record loop ops ----------------------------------------------------- */
212 LOOPEV_LEAVE
, /* Loop is left or not entered. */
213 LOOPEV_ENTERLO
, /* Loop is entered with a low iteration count left. */
214 LOOPEV_ENTER
/* Loop is entered. */
217 /* Canonicalize slots: convert integers to numbers. */
218 static void canonicalize_slots(jit_State
*J
)
221 if (LJ_DUALNUM
) return;
222 for (s
= J
->baseslot
+J
->maxslot
-1; s
>= 1; s
--) {
223 TRef tr
= J
->slot
[s
];
224 if (tref_isinteger(tr
)) {
225 IRIns
*ir
= IR(tref_ref(tr
));
226 if (!(ir
->o
== IR_SLOAD
&& (ir
->op2
& IRSLOAD_READONLY
)))
227 J
->slot
[s
] = emitir(IRTN(IR_CONV
), tr
, IRCONV_NUM_INT
);
232 /* Stop recording. */
233 static void rec_stop(jit_State
*J
, TraceLink linktype
, TraceNo lnk
)
236 J
->cur
.linktype
= (uint8_t)linktype
;
237 J
->cur
.link
= (uint16_t)lnk
;
238 /* Looping back at the same stack level? */
239 if (lnk
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
240 if ((J
->flags
& JIT_F_OPT_LOOP
)) /* Shall we try to create a loop? */
241 goto nocanon
; /* Do not canonicalize or we lose the narrowing. */
242 if (J
->cur
.root
) /* Otherwise ensure we always link to the root trace. */
243 J
->cur
.link
= J
->cur
.root
;
245 canonicalize_slots(J
);
247 /* Note: all loop ops must set J->pc to the following instruction! */
248 lj_snap_add(J
); /* Add loop snapshot. */
250 J
->mergesnap
= 1; /* In case recording continues. */
253 /* Search bytecode backwards for a int/num constant slot initializer. */
254 static TRef
find_kinit(jit_State
*J
, const BCIns
*endpc
, BCReg slot
, IRType t
)
256 /* This algorithm is rather simplistic and assumes quite a bit about
257 ** how the bytecode is generated. It works fine for FORI initializers,
258 ** but it won't necessarily work in other cases (e.g. iterator arguments).
259 ** It doesn't do anything fancy, either (like backpropagating MOVs).
261 const BCIns
*pc
, *startpc
= proto_bc(J
->pt
);
262 for (pc
= endpc
-1; pc
> startpc
; pc
--) {
264 BCOp op
= bc_op(ins
);
265 /* First try to find the last instruction that stores to this slot. */
266 if (bcmode_a(op
) == BCMbase
&& bc_a(ins
) <= slot
) {
267 return 0; /* Multiple results, e.g. from a CALL or KNIL. */
268 } else if (bcmode_a(op
) == BCMdst
&& bc_a(ins
) == slot
) {
269 if (op
== BC_KSHORT
|| op
== BC_KNUM
) { /* Found const. initializer. */
270 /* Now try to verify there's no forward jump across it. */
271 const BCIns
*kpc
= pc
;
272 for (; pc
> startpc
; pc
--)
273 if (bc_op(*pc
) == BC_JMP
) {
274 const BCIns
*target
= pc
+bc_j(*pc
)+1;
275 if (target
> kpc
&& target
<= endpc
)
276 return 0; /* Conditional assignment. */
278 if (op
== BC_KSHORT
) {
279 int32_t k
= (int32_t)(int16_t)bc_d(ins
);
280 return t
== IRT_INT
? lj_ir_kint(J
, k
) : lj_ir_knum(J
, (lua_Number
)k
);
282 cTValue
*tv
= proto_knumtv(J
->pt
, bc_d(ins
));
284 int32_t k
= numberVint(tv
);
285 if (tvisint(tv
) || numV(tv
) == (lua_Number
)k
) /* -0 is ok here. */
286 return lj_ir_kint(J
, k
);
287 return 0; /* Type mismatch. */
289 return lj_ir_knum(J
, numberVnum(tv
));
293 return 0; /* Non-constant initializer. */
296 return 0; /* No assignment to this slot found? */
299 /* Load and optionally convert a FORI argument from a slot. */
300 static TRef
fori_load(jit_State
*J
, BCReg slot
, IRType t
, int mode
)
302 int conv
= (tvisint(&J
->L
->base
[slot
]) != (t
==IRT_INT
)) ? IRSLOAD_CONVERT
: 0;
303 return sloadt(J
, (int32_t)slot
,
304 t
+ (((mode
& IRSLOAD_TYPECHECK
) ||
305 (conv
&& t
== IRT_INT
&& !(mode
>> 16))) ?
310 /* Peek before FORI to find a const initializer. Otherwise load from slot. */
311 static TRef
fori_arg(jit_State
*J
, const BCIns
*fori
, BCReg slot
,
314 TRef tr
= J
->base
[slot
];
316 tr
= find_kinit(J
, fori
, slot
, t
);
318 tr
= fori_load(J
, slot
, t
, mode
);
323 /* Return the direction of the FOR loop iterator.
324 ** It's important to exactly reproduce the semantics of the interpreter.
326 static int rec_for_direction(cTValue
*o
)
328 return (tvisint(o
) ? intV(o
) : (int32_t)o
->u32
.hi
) >= 0;
331 /* Simulate the runtime behavior of the FOR loop iterator. */
332 static LoopEvent
rec_for_iter(IROp
*op
, cTValue
*o
, int isforl
)
334 lua_Number stopv
= numberVnum(&o
[FORL_STOP
]);
335 lua_Number idxv
= numberVnum(&o
[FORL_IDX
]);
336 lua_Number stepv
= numberVnum(&o
[FORL_STEP
]);
339 if (rec_for_direction(&o
[FORL_STEP
])) {
342 return idxv
+ 2*stepv
> stopv
? LOOPEV_ENTERLO
: LOOPEV_ENTER
;
344 *op
= IR_GT
; return LOOPEV_LEAVE
;
348 return idxv
+ 2*stepv
< stopv
? LOOPEV_ENTERLO
: LOOPEV_ENTER
;
350 *op
= IR_LT
; return LOOPEV_LEAVE
;
354 /* Record checks for FOR loop overflow and step direction. */
355 static void rec_for_check(jit_State
*J
, IRType t
, int dir
,
356 TRef stop
, TRef step
, int init
)
358 if (!tref_isk(step
)) {
359 /* Non-constant step: need a guard for the direction. */
360 TRef zero
= (t
== IRT_INT
) ? lj_ir_kint(J
, 0) : lj_ir_knum_zero(J
);
361 emitir(IRTG(dir
? IR_GE
: IR_LT
, t
), step
, zero
);
362 /* Add hoistable overflow checks for a narrowed FORL index. */
363 if (init
&& t
== IRT_INT
) {
364 if (tref_isk(stop
)) {
365 /* Constant stop: optimize check away or to a range check for step. */
366 int32_t k
= IR(tref_ref(stop
))->i
;
369 emitir(IRTGI(IR_LE
), step
, lj_ir_kint(J
, (int32_t)0x7fffffff-k
));
372 emitir(IRTGI(IR_GE
), step
, lj_ir_kint(J
, (int32_t)0x80000000-k
));
375 /* Stop+step variable: need full overflow check. */
376 TRef tr
= emitir(IRTGI(IR_ADDOV
), step
, stop
);
377 emitir(IRTI(IR_USE
), tr
, 0); /* ADDOV is weak. Avoid dead result. */
380 } else if (init
&& t
== IRT_INT
&& !tref_isk(stop
)) {
381 /* Constant step: optimize overflow check to a range check for stop. */
382 int32_t k
= IR(tref_ref(step
))->i
;
383 k
= (int32_t)(dir
? 0x7fffffff : 0x80000000) - k
;
384 emitir(IRTGI(dir
? IR_LE
: IR_GE
), stop
, lj_ir_kint(J
, k
));
388 /* Record a FORL instruction. */
389 static void rec_for_loop(jit_State
*J
, const BCIns
*fori
, ScEvEntry
*scev
,
392 BCReg ra
= bc_a(*fori
);
393 cTValue
*tv
= &J
->L
->base
[ra
];
394 TRef idx
= J
->base
[ra
+FORL_IDX
];
395 IRType t
= idx
? tref_type(idx
) :
396 (init
|| LJ_DUALNUM
) ? lj_opt_narrow_forl(J
, tv
) : IRT_NUM
;
397 int mode
= IRSLOAD_INHERIT
+
398 ((!LJ_DUALNUM
|| tvisint(tv
) == (t
== IRT_INT
)) ? IRSLOAD_READONLY
: 0);
399 TRef stop
= fori_arg(J
, fori
, ra
+FORL_STOP
, t
, mode
);
400 TRef step
= fori_arg(J
, fori
, ra
+FORL_STEP
, t
, mode
);
401 int tc
, dir
= rec_for_direction(&tv
[FORL_STEP
]);
402 lua_assert(bc_op(*fori
) == BC_FORI
|| bc_op(*fori
) == BC_JFORI
);
405 scev
->stop
= tref_ref(stop
);
406 scev
->step
= tref_ref(step
);
407 rec_for_check(J
, t
, dir
, stop
, step
, init
);
408 scev
->start
= tref_ref(find_kinit(J
, fori
, ra
+FORL_IDX
, IRT_INT
));
410 !(scev
->start
&& irref_isk(scev
->stop
) && irref_isk(scev
->step
) &&
411 tvisint(&tv
[FORL_IDX
]) == (t
== IRT_INT
))) ?
412 IRSLOAD_TYPECHECK
: 0;
414 J
->base
[ra
+FORL_STOP
] = stop
;
415 J
->base
[ra
+FORL_STEP
] = step
;
418 idx
= fori_load(J
, ra
+FORL_IDX
, t
,
419 IRSLOAD_INHERIT
+ tc
+ (J
->scev
.start
<< 16));
421 J
->base
[ra
+FORL_IDX
] = idx
= emitir(IRT(IR_ADD
, t
), idx
, step
);
422 J
->base
[ra
+FORL_EXT
] = idx
;
423 scev
->idx
= tref_ref(idx
);
424 setmref(scev
->pc
, fori
);
425 J
->maxslot
= ra
+FORL_EXT
+1;
428 /* Record FORL/JFORL or FORI/JFORI. */
429 static LoopEvent
rec_for(jit_State
*J
, const BCIns
*fori
, int isforl
)
431 BCReg ra
= bc_a(*fori
);
432 TValue
*tv
= &J
->L
->base
[ra
];
433 TRef
*tr
= &J
->base
[ra
];
438 if (isforl
) { /* Handle FORL/JFORL opcodes. */
439 TRef idx
= tr
[FORL_IDX
];
440 if (mref(J
->scev
.pc
, const BCIns
) == fori
&& tref_ref(idx
) == J
->scev
.idx
) {
443 idx
= emitir(IRT(IR_ADD
, t
), idx
, J
->scev
.step
);
444 tr
[FORL_EXT
] = tr
[FORL_IDX
] = idx
;
447 rec_for_loop(J
, fori
, &scev
, 0);
451 } else { /* Handle FORI/JFORI opcodes. */
453 lj_meta_for(J
->L
, tv
);
454 t
= (LJ_DUALNUM
|| tref_isint(tr
[FORL_IDX
])) ? lj_opt_narrow_forl(J
, tv
) :
456 for (i
= FORL_IDX
; i
<= FORL_STEP
; i
++) {
457 if (!tr
[i
]) sload(J
, ra
+i
);
458 lua_assert(tref_isnumber_str(tr
[i
]));
459 if (tref_isstr(tr
[i
]))
460 tr
[i
] = emitir(IRTG(IR_STRTO
, IRT_NUM
), tr
[i
], 0);
462 if (!tref_isinteger(tr
[i
]))
463 tr
[i
] = emitir(IRTGI(IR_CONV
), tr
[i
], IRCONV_INT_NUM
|IRCONV_CHECK
);
465 if (!tref_isnum(tr
[i
]))
466 tr
[i
] = emitir(IRTN(IR_CONV
), tr
[i
], IRCONV_NUM_INT
);
469 tr
[FORL_EXT
] = tr
[FORL_IDX
];
470 stop
= tr
[FORL_STOP
];
471 rec_for_check(J
, t
, rec_for_direction(&tv
[FORL_STEP
]),
472 stop
, tr
[FORL_STEP
], 1);
475 ev
= rec_for_iter(&op
, tv
, isforl
);
476 if (ev
== LOOPEV_LEAVE
) {
477 J
->maxslot
= ra
+FORL_EXT
+1;
481 J
->pc
= fori
+bc_j(*fori
)+1;
485 emitir(IRTG(op
, t
), tr
[FORL_IDX
], stop
);
487 if (ev
== LOOPEV_LEAVE
) {
489 J
->pc
= fori
+bc_j(*fori
)+1;
491 J
->maxslot
= ra
+FORL_EXT
+1;
498 /* Record ITERL/JITERL. */
499 static LoopEvent
rec_iterl(jit_State
*J
, const BCIns iterins
)
501 BCReg ra
= bc_a(iterins
);
502 lua_assert(J
->base
[ra
] != 0);
503 if (!tref_isnil(J
->base
[ra
])) { /* Looping back? */
504 J
->base
[ra
-1] = J
->base
[ra
]; /* Copy result of ITERC to control var. */
505 J
->maxslot
= ra
-1+bc_b(J
->pc
[-1]);
506 J
->pc
+= bc_j(iterins
)+1;
515 /* Record LOOP/JLOOP. Now, that was easy. */
516 static LoopEvent
rec_loop(jit_State
*J
, BCReg ra
)
518 if (ra
< J
->maxslot
) J
->maxslot
= ra
;
523 /* Check if a loop repeatedly failed to trace because it didn't loop back. */
524 static int innerloopleft(jit_State
*J
, const BCIns
*pc
)
527 for (i
= 0; i
< PENALTY_SLOTS
; i
++)
528 if (mref(J
->penalty
[i
].pc
, const BCIns
) == pc
) {
529 if ((J
->penalty
[i
].reason
== LJ_TRERR_LLEAVE
||
530 J
->penalty
[i
].reason
== LJ_TRERR_LINNER
) &&
531 J
->penalty
[i
].val
>= 2*PENALTY_MIN
)
538 /* Handle the case when an interpreted loop op is hit. */
539 static void rec_loop_interp(jit_State
*J
, const BCIns
*pc
, LoopEvent ev
)
541 if (J
->parent
== 0) {
542 if (pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0) {
544 if (ev
== LOOPEV_LEAVE
) /* Must loop back to form a root trace. */
545 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
546 rec_stop(J
, LJ_TRLINK_LOOP
, J
->cur
.traceno
); /* Looping root trace. */
547 } else if (ev
!= LOOPEV_LEAVE
) { /* Entering inner loop? */
548 /* It's usually better to abort here and wait until the inner loop
549 ** is traced. But if the inner loop repeatedly didn't loop back,
550 ** this indicates a low trip count. In this case try unrolling
551 ** an inner loop even in a root trace. But it's better to be a bit
552 ** more conservative here and only do it for very short loops.
554 if (bc_j(*pc
) != -1 && !innerloopleft(J
, pc
))
555 lj_trace_err(J
, LJ_TRERR_LINNER
); /* Root trace hit an inner loop. */
556 if ((ev
!= LOOPEV_ENTERLO
&&
557 J
->loopref
&& J
->cur
.nins
- J
->loopref
> 24) || --J
->loopunroll
< 0)
558 lj_trace_err(J
, LJ_TRERR_LUNROLL
); /* Limit loop unrolling. */
559 J
->loopref
= J
->cur
.nins
;
561 } else if (ev
!= LOOPEV_LEAVE
) { /* Side trace enters an inner loop. */
562 J
->loopref
= J
->cur
.nins
;
563 if (--J
->loopunroll
< 0)
564 lj_trace_err(J
, LJ_TRERR_LUNROLL
); /* Limit loop unrolling. */
565 } /* Side trace continues across a loop that's left or not entered. */
568 /* Handle the case when an already compiled loop op is hit. */
569 static void rec_loop_jit(jit_State
*J
, TraceNo lnk
, LoopEvent ev
)
571 if (J
->parent
== 0) { /* Root trace hit an inner loop. */
572 /* Better let the inner loop spawn a side trace back here. */
573 lj_trace_err(J
, LJ_TRERR_LINNER
);
574 } else if (ev
!= LOOPEV_LEAVE
) { /* Side trace enters a compiled loop. */
575 J
->instunroll
= 0; /* Cannot continue across a compiled loop op. */
576 if (J
->pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0)
577 rec_stop(J
, LJ_TRLINK_LOOP
, J
->cur
.traceno
); /* Form an extra loop. */
579 rec_stop(J
, LJ_TRLINK_ROOT
, lnk
); /* Link to the loop. */
580 } /* Side trace continues across a loop that's left or not entered. */
583 /* -- Record calls and returns -------------------------------------------- */
585 /* Specialize to the runtime value of the called function or its prototype. */
586 static TRef
rec_call_specialize(jit_State
*J
, GCfunc
*fn
, TRef tr
)
590 GCproto
*pt
= funcproto(fn
);
591 /* Too many closures created? Probably not a monomorphic function. */
592 if (pt
->flags
>= PROTO_CLC_POLY
) { /* Specialize to prototype instead. */
593 TRef trpt
= emitir(IRT(IR_FLOAD
, IRT_P32
), tr
, IRFL_FUNC_PC
);
594 emitir(IRTG(IR_EQ
, IRT_P32
), trpt
, lj_ir_kptr(J
, proto_bc(pt
)));
595 (void)lj_ir_kgc(J
, obj2gco(pt
), IRT_PROTO
); /* Prevent GC of proto. */
599 /* Otherwise specialize to the function (closure) value itself. */
600 kfunc
= lj_ir_kfunc(J
, fn
);
601 emitir(IRTG(IR_EQ
, IRT_FUNC
), tr
, kfunc
);
605 /* Record call setup. */
606 static void rec_call_setup(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
609 TValue
*functv
= &J
->L
->base
[func
];
610 TRef
*fbase
= &J
->base
[func
];
612 for (i
= 0; i
<= nargs
; i
++)
613 (void)getslot(J
, func
+i
); /* Ensure func and all args have a reference. */
614 if (!tref_isfunc(fbase
[0])) { /* Resolve __call metamethod. */
616 copyTV(J
->L
, &ix
.tabv
, functv
);
617 if (!lj_record_mm_lookup(J
, &ix
, MM_call
) || !tref_isfunc(ix
.mobj
))
618 lj_trace_err(J
, LJ_TRERR_NOMM
);
619 for (i
= ++nargs
; i
> 0; i
--) /* Shift arguments up. */
620 fbase
[i
] = fbase
[i
-1];
621 fbase
[0] = ix
.mobj
; /* Replace function. */
624 fbase
[0] = TREF_FRAME
| rec_call_specialize(J
, funcV(functv
), fbase
[0]);
625 J
->maxslot
= (BCReg
)nargs
;
629 void lj_record_call(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
631 rec_call_setup(J
, func
, nargs
);
635 J
->baseslot
+= func
+1;
638 /* Record tail call. */
639 void lj_record_tailcall(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
641 rec_call_setup(J
, func
, nargs
);
642 if (frame_isvarg(J
->L
->base
- 1)) {
643 BCReg cbase
= (BCReg
)frame_delta(J
->L
->base
- 1);
644 if (--J
->framedepth
< 0)
645 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
646 J
->baseslot
-= (BCReg
)cbase
;
650 /* Move func + args down. */
651 memmove(&J
->base
[-1], &J
->base
[func
], sizeof(TRef
)*(J
->maxslot
+1));
652 /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */
653 /* Tailcalls can form a loop, so count towards the loop unroll limit. */
654 if (++J
->tailcalled
> J
->loopunroll
)
655 lj_trace_err(J
, LJ_TRERR_LUNROLL
);
658 /* Check unroll limits for down-recursion. */
659 static int check_downrec_unroll(jit_State
*J
, GCproto
*pt
)
662 for (ptref
= J
->chain
[IR_KGC
]; ptref
; ptref
= IR(ptref
)->prev
)
663 if (ir_kgc(IR(ptref
)) == obj2gco(pt
)) {
666 for (ref
= J
->chain
[IR_RETF
]; ref
; ref
= IR(ref
)->prev
)
667 if (IR(ref
)->op1
== ptref
)
670 if (J
->pc
== J
->startpc
) {
671 if (count
+ J
->tailcalled
> J
->param
[JIT_P_recunroll
])
674 lj_trace_err(J
, LJ_TRERR_DOWNREC
);
682 void lj_record_ret(jit_State
*J
, BCReg rbase
, ptrdiff_t gotresults
)
684 TValue
*frame
= J
->L
->base
- 1;
686 for (i
= 0; i
< gotresults
; i
++)
687 (void)getslot(J
, rbase
+i
); /* Ensure all results have a reference. */
688 while (frame_ispcall(frame
)) { /* Immediately resolve pcall() returns. */
689 BCReg cbase
= (BCReg
)frame_delta(frame
);
690 if (--J
->framedepth
<= 0)
691 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
692 lua_assert(J
->baseslot
> 1);
695 J
->baseslot
-= (BCReg
)cbase
;
697 J
->base
[--rbase
] = TREF_TRUE
; /* Prepend true to results. */
698 frame
= frame_prevd(frame
);
700 /* Return to lower frame via interpreter for unhandled cases. */
701 if (J
->framedepth
== 0 && J
->pt
&& bc_isret(bc_op(*J
->pc
)) &&
702 (!frame_islua(frame
) ||
703 (J
->parent
== 0 && !bc_isret(bc_op(J
->cur
.startins
))))) {
704 /* NYI: specialize to frame type and return directly, not via RET*. */
705 for (i
= 0; i
< (ptrdiff_t)rbase
; i
++)
706 J
->base
[i
] = 0; /* Purge dead slots. */
707 J
->maxslot
= rbase
+ (BCReg
)gotresults
;
708 rec_stop(J
, LJ_TRLINK_RETURN
, 0); /* Return to interpreter. */
711 if (frame_isvarg(frame
)) {
712 BCReg cbase
= (BCReg
)frame_delta(frame
);
713 if (--J
->framedepth
< 0) /* NYI: return of vararg func to lower frame. */
714 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
715 lua_assert(J
->baseslot
> 1);
717 J
->baseslot
-= (BCReg
)cbase
;
719 frame
= frame_prevd(frame
);
721 if (frame_islua(frame
)) { /* Return to Lua frame. */
722 BCIns callins
= *(frame_pc(frame
)-1);
723 ptrdiff_t nresults
= bc_b(callins
) ? (ptrdiff_t)bc_b(callins
)-1 :gotresults
;
724 BCReg cbase
= bc_a(callins
);
725 GCproto
*pt
= funcproto(frame_func(frame
- (cbase
+1)));
726 if ((pt
->flags
& PROTO_NOJIT
))
727 lj_trace_err(J
, LJ_TRERR_CJITOFF
);
728 if (J
->framedepth
== 0 && J
->pt
&& frame
== J
->L
->base
- 1) {
729 if (check_downrec_unroll(J
, pt
)) {
730 J
->maxslot
= (BCReg
)(rbase
+ gotresults
);
732 rec_stop(J
, LJ_TRLINK_DOWNREC
, J
->cur
.traceno
); /* Down-recursion. */
737 for (i
= 0; i
< nresults
; i
++) /* Adjust results. */
738 J
->base
[i
-1] = i
< gotresults
? J
->base
[rbase
+i
] : TREF_NIL
;
739 J
->maxslot
= cbase
+(BCReg
)nresults
;
740 if (J
->framedepth
> 0) { /* Return to a frame that is part of the trace. */
742 lua_assert(J
->baseslot
> cbase
+1);
743 J
->baseslot
-= cbase
+1;
745 } else if (J
->parent
== 0 && !bc_isret(bc_op(J
->cur
.startins
))) {
746 /* Return to lower frame would leave the loop in a root trace. */
747 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
748 } else if (J
->needsnap
) { /* Tailcalled to ff with side-effects. */
749 lj_trace_err(J
, LJ_TRERR_NYIRETL
); /* No way to insert snapshot here. */
750 } else { /* Return to lower frame. Guard for the target we return to. */
751 TRef trpt
= lj_ir_kgc(J
, obj2gco(pt
), IRT_PROTO
);
752 TRef trpc
= lj_ir_kptr(J
, (void *)frame_pc(frame
));
753 emitir(IRTG(IR_RETF
, IRT_P32
), trpt
, trpc
);
756 lua_assert(J
->baseslot
== 1);
757 /* Shift result slots up and clear the slots of the new frame below. */
758 memmove(J
->base
+ cbase
, J
->base
-1, sizeof(TRef
)*nresults
);
759 memset(J
->base
-1, 0, sizeof(TRef
)*(cbase
+1));
761 } else if (frame_iscont(frame
)) { /* Return to continuation frame. */
762 ASMFunction cont
= frame_contf(frame
);
763 BCReg cbase
= (BCReg
)frame_delta(frame
);
764 if ((J
->framedepth
-= 2) < 0)
765 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
766 J
->baseslot
-= (BCReg
)cbase
;
768 J
->maxslot
= cbase
-2;
769 if (cont
== lj_cont_ra
) {
770 /* Copy result to destination slot. */
771 BCReg dst
= bc_a(*(frame_contpc(frame
)-1));
772 J
->base
[dst
] = gotresults
? J
->base
[cbase
+rbase
] : TREF_NIL
;
773 if (dst
>= J
->maxslot
) J
->maxslot
= dst
+1;
774 } else if (cont
== lj_cont_nop
) {
775 /* Nothing to do here. */
776 } else if (cont
== lj_cont_cat
) {
779 /* Result type already specialized. */
780 lua_assert(cont
== lj_cont_condf
|| cont
== lj_cont_condt
);
783 lj_trace_err(J
, LJ_TRERR_NYIRETL
); /* NYI: handle return to C frame. */
785 lua_assert(J
->baseslot
>= 1);
788 /* -- Metamethod handling ------------------------------------------------- */
790 /* Prepare to record call to metamethod. */
791 static BCReg
rec_mm_prep(jit_State
*J
, ASMFunction cont
)
793 BCReg s
, top
= curr_proto(J
->L
)->framesize
;
795 setcont(&J
->L
->base
[top
], cont
);
797 trcont
= lj_ir_kptr(J
, (void *)((int64_t)cont
- (int64_t)lj_vm_asm_begin
));
799 trcont
= lj_ir_kptr(J
, (void *)cont
);
801 J
->base
[top
] = trcont
| TREF_CONT
;
803 for (s
= J
->maxslot
; s
< top
; s
++)
804 J
->base
[s
] = 0; /* Clear frame gap to avoid resurrecting previous refs. */
808 /* Record metamethod lookup. */
809 int lj_record_mm_lookup(jit_State
*J
, RecordIndex
*ix
, MMS mm
)
813 if (tref_istab(ix
->tab
)) {
814 mt
= tabref(tabV(&ix
->tabv
)->metatable
);
815 mix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_TAB_META
);
816 } else if (tref_isudata(ix
->tab
)) {
817 int udtype
= udataV(&ix
->tabv
)->udtype
;
818 mt
= tabref(udataV(&ix
->tabv
)->metatable
);
819 /* The metatables of special userdata objects are treated as immutable. */
820 if (udtype
!= UDTYPE_USERDATA
) {
822 if (LJ_HASFFI
&& udtype
== UDTYPE_FFI_CLIB
) {
823 /* Specialize to the C library namespace object. */
824 emitir(IRTG(IR_EQ
, IRT_P32
), ix
->tab
, lj_ir_kptr(J
, udataV(&ix
->tabv
)));
826 /* Specialize to the type of userdata. */
827 TRef tr
= emitir(IRT(IR_FLOAD
, IRT_U8
), ix
->tab
, IRFL_UDATA_UDTYPE
);
828 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, udtype
));
831 mo
= lj_tab_getstr(mt
, mmname_str(J2G(J
), mm
));
832 if (!mo
|| tvisnil(mo
))
833 return 0; /* No metamethod. */
834 /* Treat metamethod or index table as immutable, too. */
835 if (!(tvisfunc(mo
) || tvistab(mo
)))
836 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
837 copyTV(J
->L
, &ix
->mobjv
, mo
);
838 ix
->mobj
= lj_ir_kgc(J
, gcV(mo
), tvisfunc(mo
) ? IRT_FUNC
: IRT_TAB
);
840 ix
->mt
= TREF_NIL
; /* Dummy value for comparison semantics. */
841 return 1; /* Got metamethod or index table. */
843 mix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_UDATA_META
);
845 /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */
846 mt
= tabref(basemt_obj(J2G(J
), &ix
->tabv
));
849 return 0; /* No metamethod. */
851 /* The cdata metatable is treated as immutable. */
852 if (LJ_HASFFI
&& tref_iscdata(ix
->tab
)) goto immutable_mt
;
853 ix
->mt
= mix
.tab
= lj_ir_ktab(J
, mt
);
856 ix
->mt
= mt
? mix
.tab
: TREF_NIL
;
857 emitir(IRTG(mt
? IR_NE
: IR_EQ
, IRT_TAB
), mix
.tab
, lj_ir_knull(J
, IRT_TAB
));
860 GCstr
*mmstr
= mmname_str(J2G(J
), mm
);
861 cTValue
*mo
= lj_tab_getstr(mt
, mmstr
);
862 if (mo
&& !tvisnil(mo
))
863 copyTV(J
->L
, &ix
->mobjv
, mo
);
865 settabV(J
->L
, &mix
.tabv
, mt
);
866 setstrV(J
->L
, &mix
.keyv
, mmstr
);
867 mix
.key
= lj_ir_kstr(J
, mmstr
);
870 ix
->mobj
= lj_record_idx(J
, &mix
);
871 return !tref_isnil(ix
->mobj
); /* 1 if metamethod found, 0 if not. */
873 return 0; /* No metamethod. */
876 /* Record call to arithmetic metamethod. */
877 static TRef
rec_mm_arith(jit_State
*J
, RecordIndex
*ix
, MMS mm
)
879 /* Set up metamethod call first to save ix->tab and ix->tabv. */
880 BCReg func
= rec_mm_prep(J
, lj_cont_ra
);
881 TRef
*base
= J
->base
+ func
;
882 TValue
*basev
= J
->L
->base
+ func
;
883 base
[1] = ix
->tab
; base
[2] = ix
->key
;
884 copyTV(J
->L
, basev
+1, &ix
->tabv
);
885 copyTV(J
->L
, basev
+2, &ix
->keyv
);
886 if (!lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
889 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
890 if (lj_record_mm_lookup(J
, ix
, mm
)) /* Lookup mm on 2nd operand. */
893 lj_trace_err(J
, LJ_TRERR_NOMM
);
897 copyTV(J
->L
, basev
+0, &ix
->mobjv
);
898 lj_record_call(J
, func
, 2);
899 return 0; /* No result yet. */
902 /* Record call to __len metamethod. */
903 static TRef
rec_mm_len(jit_State
*J
, TRef tr
, TValue
*tv
)
907 copyTV(J
->L
, &ix
.tabv
, tv
);
908 if (lj_record_mm_lookup(J
, &ix
, MM_len
)) {
909 BCReg func
= rec_mm_prep(J
, lj_cont_ra
);
910 TRef
*base
= J
->base
+ func
;
911 TValue
*basev
= J
->L
->base
+ func
;
912 base
[0] = ix
.mobj
; copyTV(J
->L
, basev
+0, &ix
.mobjv
);
913 base
[1] = tr
; copyTV(J
->L
, basev
+1, tv
);
915 base
[2] = tr
; copyTV(J
->L
, basev
+2, tv
);
917 base
[2] = TREF_NIL
; setnilV(basev
+2);
919 lj_record_call(J
, func
, 2);
921 if (LJ_52
&& tref_istab(tr
))
922 return lj_ir_call(J
, IRCALL_lj_tab_len
, tr
);
923 lj_trace_err(J
, LJ_TRERR_NOMM
);
925 return 0; /* No result yet. */
928 /* Call a comparison metamethod. */
929 static void rec_mm_callcomp(jit_State
*J
, RecordIndex
*ix
, int op
)
931 BCReg func
= rec_mm_prep(J
, (op
&1) ? lj_cont_condf
: lj_cont_condt
);
932 TRef
*base
= J
->base
+ func
;
933 TValue
*tv
= J
->L
->base
+ func
;
934 base
[0] = ix
->mobj
; base
[1] = ix
->val
; base
[2] = ix
->key
;
935 copyTV(J
->L
, tv
+0, &ix
->mobjv
);
936 copyTV(J
->L
, tv
+1, &ix
->valv
);
937 copyTV(J
->L
, tv
+2, &ix
->keyv
);
938 lj_record_call(J
, func
, 2);
941 /* Record call to equality comparison metamethod (for tab and udata only). */
942 static void rec_mm_equal(jit_State
*J
, RecordIndex
*ix
, int op
)
945 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
946 if (lj_record_mm_lookup(J
, ix
, MM_eq
)) { /* Lookup mm on 1st operand. */
950 copyTV(J
->L
, &mo1v
, &ix
->mobjv
);
951 /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */
953 if (tvistab(bv
) && tabref(tabV(bv
)->metatable
) == ix
->mtv
) {
954 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_TAB_META
);
955 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
956 } else if (tvisudata(bv
) && tabref(udataV(bv
)->metatable
) == ix
->mtv
) {
957 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_UDATA_META
);
958 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
959 } else { /* Lookup metamethod on 2nd operand and compare both. */
961 copyTV(J
->L
, &ix
->tabv
, bv
);
962 if (!lj_record_mm_lookup(J
, ix
, MM_eq
) ||
963 lj_record_objcmp(J
, mo1
, ix
->mobj
, &mo1v
, &ix
->mobjv
))
966 rec_mm_callcomp(J
, ix
, op
);
970 /* Record call to ordered comparison metamethods (for arbitrary objects). */
971 static void rec_mm_comp(jit_State
*J
, RecordIndex
*ix
, int op
)
974 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
976 MMS mm
= (op
& 2) ? MM_le
: MM_lt
; /* Try __le + __lt or only __lt. */
978 if (!lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
980 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
981 if (!lj_record_mm_lookup(J
, ix
, mm
)) /* Lookup mm on 2nd operand. */
984 rec_mm_callcomp(J
, ix
, op
);
987 if (lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
991 copyTV(J
->L
, &mo1v
, &ix
->mobjv
);
992 /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */
994 if (tvistab(bv
) && tabref(tabV(bv
)->metatable
) == ix
->mtv
) {
995 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_TAB_META
);
996 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
997 } else if (tvisudata(bv
) && tabref(udataV(bv
)->metatable
) == ix
->mtv
) {
998 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_UDATA_META
);
999 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
1000 } else { /* Lookup metamethod on 2nd operand and compare both. */
1002 copyTV(J
->L
, &ix
->tabv
, bv
);
1003 if (!lj_record_mm_lookup(J
, ix
, mm
) ||
1004 lj_record_objcmp(J
, mo1
, ix
->mobj
, &mo1v
, &ix
->mobjv
))
1007 rec_mm_callcomp(J
, ix
, op
);
1012 /* Lookup failed. Retry with __lt and swapped operands. */
1013 if (!(op
& 2)) break; /* Already at __lt. Interpreter will throw. */
1014 ix
->tab
= ix
->key
; ix
->key
= ix
->val
; ix
->val
= ix
->tab
;
1015 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1016 copyTV(J
->L
, &ix
->keyv
, &ix
->valv
);
1017 copyTV(J
->L
, &ix
->valv
, &ix
->tabv
);
1023 /* Setup call to cdata comparison metamethod. */
1024 static void rec_mm_comp_cdata(jit_State
*J
, RecordIndex
*ix
, int op
, MMS mm
)
1027 if (tref_iscdata(ix
->val
)) {
1029 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
1031 lua_assert(tref_iscdata(ix
->key
));
1033 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1035 lj_record_mm_lookup(J
, ix
, mm
);
1036 rec_mm_callcomp(J
, ix
, op
);
1040 /* -- Indexed access ------------------------------------------------------ */
1042 /* Record bounds-check. */
1043 static void rec_idx_abc(jit_State
*J
, TRef asizeref
, TRef ikey
, uint32_t asize
)
1045 /* Try to emit invariant bounds checks. */
1046 if ((J
->flags
& (JIT_F_OPT_LOOP
|JIT_F_OPT_ABC
)) ==
1047 (JIT_F_OPT_LOOP
|JIT_F_OPT_ABC
)) {
1048 IRRef ref
= tref_ref(ikey
);
1049 IRIns
*ir
= IR(ref
);
1052 /* Handle constant offsets. */
1053 if (ir
->o
== IR_ADD
&& irref_isk(ir
->op2
)) {
1055 ofs
= IR(ofsref
)->i
;
1059 /* Got scalar evolution analysis results for this reference? */
1060 if (ref
== J
->scev
.idx
) {
1062 lua_assert(irt_isint(J
->scev
.t
) && ir
->o
== IR_SLOAD
);
1063 stop
= numberVint(&(J
->L
->base
- J
->baseslot
)[ir
->op1
+ FORL_STOP
]);
1064 /* Runtime value for stop of loop is within bounds? */
1065 if ((uint64_t)stop
+ ofs
< (uint64_t)asize
) {
1066 /* Emit invariant bounds check for stop. */
1067 emitir(IRTG(IR_ABC
, IRT_P32
), asizeref
, ofs
== 0 ? J
->scev
.stop
:
1068 emitir(IRTI(IR_ADD
), J
->scev
.stop
, ofsref
));
1069 /* Emit invariant bounds check for start, if not const or negative. */
1070 if (!(J
->scev
.dir
&& J
->scev
.start
&&
1071 (int64_t)IR(J
->scev
.start
)->i
+ ofs
>= 0))
1072 emitir(IRTG(IR_ABC
, IRT_P32
), asizeref
, ikey
);
1077 emitir(IRTGI(IR_ABC
), asizeref
, ikey
); /* Emit regular bounds check. */
1080 /* Record indexed key lookup. */
1081 static TRef
rec_idx_key(jit_State
*J
, RecordIndex
*ix
)
1084 GCtab
*t
= tabV(&ix
->tabv
);
1085 ix
->oldv
= lj_tab_get(J
->L
, t
, &ix
->keyv
); /* Lookup previous value. */
1087 /* Integer keys are looked up in the array part first. */
1089 if (tref_isnumber(key
)) {
1090 int32_t k
= numberVint(&ix
->keyv
);
1091 if (!tvisint(&ix
->keyv
) && numV(&ix
->keyv
) != (lua_Number
)k
)
1093 if ((MSize
)k
< LJ_MAX_ASIZE
) { /* Potential array key? */
1094 TRef ikey
= lj_opt_narrow_index(J
, key
);
1095 TRef asizeref
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_ASIZE
);
1096 if ((MSize
)k
< t
->asize
) { /* Currently an array key? */
1098 rec_idx_abc(J
, asizeref
, ikey
, t
->asize
);
1099 arrayref
= emitir(IRT(IR_FLOAD
, IRT_P32
), ix
->tab
, IRFL_TAB_ARRAY
);
1100 return emitir(IRT(IR_AREF
, IRT_P32
), arrayref
, ikey
);
1101 } else { /* Currently not in array (may be an array extension)? */
1102 emitir(IRTGI(IR_ULE
), asizeref
, ikey
); /* Inv. bounds check. */
1103 if (k
== 0 && tref_isk(key
))
1104 key
= lj_ir_knum_zero(J
); /* Canonicalize 0 or +-0.0 to +0.0. */
1105 /* And continue with the hash lookup. */
1107 } else if (!tref_isk(key
)) {
1108 /* We can rule out const numbers which failed the integerness test
1109 ** above. But all other numbers are potential array keys.
1111 if (t
->asize
== 0) { /* True sparse tables have an empty array part. */
1112 /* Guard that the array part stays empty. */
1113 TRef tmp
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_ASIZE
);
1114 emitir(IRTGI(IR_EQ
), tmp
, lj_ir_kint(J
, 0));
1116 lj_trace_err(J
, LJ_TRERR_NYITMIX
);
1121 /* Otherwise the key is located in the hash part. */
1122 if (t
->hmask
== 0) { /* Shortcut for empty hash part. */
1123 /* Guard that the hash part stays empty. */
1124 TRef tmp
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_HMASK
);
1125 emitir(IRTGI(IR_EQ
), tmp
, lj_ir_kint(J
, 0));
1126 return lj_ir_kkptr(J
, niltvg(J2G(J
)));
1128 if (tref_isinteger(key
)) /* Hash keys are based on numbers, not ints. */
1129 key
= emitir(IRTN(IR_CONV
), key
, IRCONV_NUM_INT
);
1130 if (tref_isk(key
)) {
1131 /* Optimize lookup of constant hash keys. */
1132 MSize hslot
= (MSize
)((char *)ix
->oldv
- (char *)&noderef(t
->node
)[0].val
);
1133 if (t
->hmask
> 0 && hslot
<= t
->hmask
*(MSize
)sizeof(Node
) &&
1134 hslot
<= 65535*(MSize
)sizeof(Node
)) {
1136 TRef hm
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_HMASK
);
1137 emitir(IRTGI(IR_EQ
), hm
, lj_ir_kint(J
, (int32_t)t
->hmask
));
1138 node
= emitir(IRT(IR_FLOAD
, IRT_P32
), ix
->tab
, IRFL_TAB_NODE
);
1139 kslot
= lj_ir_kslot(J
, key
, hslot
/ sizeof(Node
));
1140 return emitir(IRTG(IR_HREFK
, IRT_P32
), node
, kslot
);
1143 /* Fall back to a regular hash lookup. */
1144 return emitir(IRT(IR_HREF
, IRT_P32
), ix
->tab
, key
);
1147 /* Determine whether a key is NOT one of the fast metamethod names. */
1148 static int nommstr(jit_State
*J
, TRef key
)
1150 if (tref_isstr(key
)) {
1151 if (tref_isk(key
)) {
1152 GCstr
*str
= ir_kstr(IR(tref_ref(key
)));
1154 for (mm
= 0; mm
<= MM_FAST
; mm
++)
1155 if (mmname_str(J2G(J
), mm
) == str
)
1156 return 0; /* MUST be one the fast metamethod names. */
1158 return 0; /* Variable string key MAY be a metamethod name. */
1161 return 1; /* CANNOT be a metamethod name. */
1164 /* Record indexed load/store. */
1165 TRef
lj_record_idx(jit_State
*J
, RecordIndex
*ix
)
1168 IROp xrefop
, loadop
;
1171 while (!tref_istab(ix
->tab
)) { /* Handle non-table lookup. */
1172 /* Never call raw lj_record_idx() on non-table. */
1173 lua_assert(ix
->idxchain
!= 0);
1174 if (!lj_record_mm_lookup(J
, ix
, ix
->val
? MM_newindex
: MM_index
))
1175 lj_trace_err(J
, LJ_TRERR_NOMM
);
1177 if (tref_isfunc(ix
->mobj
)) { /* Handle metamethod call. */
1178 BCReg func
= rec_mm_prep(J
, ix
->val
? lj_cont_nop
: lj_cont_ra
);
1179 TRef
*base
= J
->base
+ func
;
1180 TValue
*tv
= J
->L
->base
+ func
;
1181 base
[0] = ix
->mobj
; base
[1] = ix
->tab
; base
[2] = ix
->key
;
1182 setfuncV(J
->L
, tv
+0, funcV(&ix
->mobjv
));
1183 copyTV(J
->L
, tv
+1, &ix
->tabv
);
1184 copyTV(J
->L
, tv
+2, &ix
->keyv
);
1187 copyTV(J
->L
, tv
+3, &ix
->valv
);
1188 lj_record_call(J
, func
, 3); /* mobj(tab, key, val) */
1191 lj_record_call(J
, func
, 2); /* res = mobj(tab, key) */
1192 return 0; /* No result yet. */
1195 /* Otherwise retry lookup with metaobject. */
1197 copyTV(J
->L
, &ix
->tabv
, &ix
->mobjv
);
1198 if (--ix
->idxchain
== 0)
1199 lj_trace_err(J
, LJ_TRERR_IDXLOOP
);
1202 /* First catch nil and NaN keys for tables. */
1203 if (tvisnil(&ix
->keyv
) || (tvisnum(&ix
->keyv
) && tvisnan(&ix
->keyv
))) {
1204 if (ix
->val
) /* Better fail early. */
1205 lj_trace_err(J
, LJ_TRERR_STORENN
);
1206 if (tref_isk(ix
->key
)) {
1207 if (ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_index
))
1213 /* Record the key lookup. */
1214 xref
= rec_idx_key(J
, ix
);
1215 xrefop
= IR(tref_ref(xref
))->o
;
1216 loadop
= xrefop
== IR_AREF
? IR_ALOAD
: IR_HLOAD
;
1217 /* The lj_meta_tset() inconsistency is gone, but better play safe. */
1218 oldv
= xrefop
== IR_KKPTR
? (cTValue
*)ir_kptr(IR(tref_ref(xref
))) : ix
->oldv
;
1220 if (ix
->val
== 0) { /* Indexed load */
1221 IRType t
= itype2irt(oldv
);
1223 if (oldv
== niltvg(J2G(J
))) {
1224 emitir(IRTG(IR_EQ
, IRT_P32
), xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1227 res
= emitir(IRTG(loadop
, t
), xref
, 0);
1229 if (t
== IRT_NIL
&& ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_index
))
1231 if (irtype_ispri(t
)) res
= TREF_PRI(t
); /* Canonicalize primitives. */
1233 } else { /* Indexed store. */
1234 GCtab
*mt
= tabref(tabV(&ix
->tabv
)->metatable
);
1235 int keybarrier
= tref_isgcv(ix
->key
) && !tref_isnil(ix
->val
);
1236 if (tvisnil(oldv
)) { /* Previous value was nil? */
1237 /* Need to duplicate the hasmm check for the early guards. */
1239 if (ix
->idxchain
&& mt
) {
1240 cTValue
*mo
= lj_tab_getstr(mt
, mmname_str(J2G(J
), MM_newindex
));
1241 hasmm
= mo
&& !tvisnil(mo
);
1244 emitir(IRTG(loadop
, IRT_NIL
), xref
, 0); /* Guard for nil value. */
1245 else if (xrefop
== IR_HREF
)
1246 emitir(IRTG(oldv
== niltvg(J2G(J
)) ? IR_EQ
: IR_NE
, IRT_P32
),
1247 xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1248 if (ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_newindex
)) {
1253 if (oldv
== niltvg(J2G(J
))) { /* Need to insert a new key. */
1255 if (tref_isinteger(key
)) /* NEWREF needs a TValue as a key. */
1256 key
= emitir(IRTN(IR_CONV
), key
, IRCONV_NUM_INT
);
1257 xref
= emitir(IRT(IR_NEWREF
, IRT_P32
), ix
->tab
, key
);
1258 keybarrier
= 0; /* NEWREF already takes care of the key barrier. */
1260 } else if (!lj_opt_fwd_wasnonnil(J
, loadop
, tref_ref(xref
))) {
1261 /* Cannot derive that the previous value was non-nil, must do checks. */
1262 if (xrefop
== IR_HREF
) /* Guard against store to niltv. */
1263 emitir(IRTG(IR_NE
, IRT_P32
), xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1264 if (ix
->idxchain
) { /* Metamethod lookup required? */
1265 /* A check for NULL metatable is cheaper (hoistable) than a load. */
1267 TRef mtref
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_TAB_META
);
1268 emitir(IRTG(IR_EQ
, IRT_TAB
), mtref
, lj_ir_knull(J
, IRT_TAB
));
1270 IRType t
= itype2irt(oldv
);
1271 emitir(IRTG(loadop
, t
), xref
, 0); /* Guard for non-nil value. */
1275 keybarrier
= 0; /* Previous non-nil value kept the key alive. */
1277 /* Convert int to number before storing. */
1278 if (!LJ_DUALNUM
&& tref_isinteger(ix
->val
))
1279 ix
->val
= emitir(IRTN(IR_CONV
), ix
->val
, IRCONV_NUM_INT
);
1280 emitir(IRT(loadop
+IRDELTA_L2S
, tref_type(ix
->val
)), xref
, ix
->val
);
1281 if (keybarrier
|| tref_isgcv(ix
->val
))
1282 emitir(IRT(IR_TBAR
, IRT_NIL
), ix
->tab
, 0);
1283 /* Invalidate neg. metamethod cache for stores with certain string keys. */
1284 if (!nommstr(J
, ix
->key
)) {
1285 TRef fref
= emitir(IRT(IR_FREF
, IRT_P32
), ix
->tab
, IRFL_TAB_NOMM
);
1286 emitir(IRT(IR_FSTORE
, IRT_U8
), fref
, lj_ir_kint(J
, 0));
1293 /* -- Upvalue access ------------------------------------------------------ */
1295 /* Check whether upvalue is immutable and ok to constify. */
1296 static int rec_upvalue_constify(jit_State
*J
, GCupval
*uvp
)
1298 if (uvp
->immutable
) {
1299 cTValue
*o
= uvval(uvp
);
1300 /* Don't constify objects that may retain large amounts of memory. */
1303 GCcdata
*cd
= cdataV(o
);
1304 if (!cdataisv(cd
) && !(cd
->marked
& LJ_GC_CDATA_FIN
)) {
1305 CType
*ct
= ctype_raw(ctype_ctsG(J2G(J
)), cd
->ctypeid
);
1306 if (!ctype_hassize(ct
->info
) || ct
->size
<= 16)
1314 if (!(tvistab(o
) || tvisudata(o
) || tvisthread(o
)))
1320 /* Record upvalue load/store. */
1321 static TRef
rec_upvalue(jit_State
*J
, uint32_t uv
, TRef val
)
1323 GCupval
*uvp
= &gcref(J
->fn
->l
.uvptr
[uv
])->uv
;
1324 TRef fn
= getcurrf(J
);
1326 int needbarrier
= 0;
1327 if (rec_upvalue_constify(J
, uvp
)) { /* Try to constify immutable upvalue. */
1329 lua_assert(val
== 0);
1330 if (!tref_isk(fn
)) { /* Late specialization of current function. */
1331 if (J
->pt
->flags
>= PROTO_CLC_POLY
)
1333 kfunc
= lj_ir_kfunc(J
, J
->fn
);
1334 emitir(IRTG(IR_EQ
, IRT_FUNC
), fn
, kfunc
);
1335 J
->base
[-1] = TREF_FRAME
| kfunc
;
1338 tr
= lj_record_constify(J
, uvval(uvp
));
1343 /* Note: this effectively limits LJ_MAX_UPVAL to 127. */
1344 uv
= (uv
<< 8) | (hashrot(uvp
->dhash
, uvp
->dhash
+ HASH_BIAS
) & 0xff);
1346 uref
= tref_ref(emitir(IRTG(IR_UREFO
, IRT_P32
), fn
, uv
));
1347 /* In current stack? */
1348 if (uvval(uvp
) >= tvref(J
->L
->stack
) &&
1349 uvval(uvp
) < tvref(J
->L
->maxstack
)) {
1350 int32_t slot
= (int32_t)(uvval(uvp
) - (J
->L
->base
- J
->baseslot
));
1351 if (slot
>= 0) { /* Aliases an SSA slot? */
1352 emitir(IRTG(IR_EQ
, IRT_P32
),
1354 emitir(IRT(IR_ADD
, IRT_P32
), uref
,
1355 lj_ir_kint(J
, (slot
- 1) * -8)));
1356 slot
-= (int32_t)J
->baseslot
; /* Note: slot number may be negative! */
1358 return getslot(J
, slot
);
1360 J
->base
[slot
] = val
;
1361 if (slot
>= (int32_t)J
->maxslot
) J
->maxslot
= (BCReg
)(slot
+1);
1366 emitir(IRTG(IR_UGT
, IRT_P32
),
1367 emitir(IRT(IR_SUB
, IRT_P32
), uref
, REF_BASE
),
1368 lj_ir_kint(J
, (J
->baseslot
+ J
->maxslot
) * 8));
1371 uref
= tref_ref(emitir(IRTG(IR_UREFC
, IRT_P32
), fn
, uv
));
1373 if (val
== 0) { /* Upvalue load */
1374 IRType t
= itype2irt(uvval(uvp
));
1375 TRef res
= emitir(IRTG(IR_ULOAD
, t
), uref
, 0);
1376 if (irtype_ispri(t
)) res
= TREF_PRI(t
); /* Canonicalize primitive refs. */
1378 } else { /* Upvalue store. */
1379 /* Convert int to number before storing. */
1380 if (!LJ_DUALNUM
&& tref_isinteger(val
))
1381 val
= emitir(IRTN(IR_CONV
), val
, IRCONV_NUM_INT
);
1382 emitir(IRT(IR_USTORE
, tref_type(val
)), uref
, val
);
1383 if (needbarrier
&& tref_isgcv(val
))
1384 emitir(IRT(IR_OBAR
, IRT_NIL
), uref
, val
);
1390 /* -- Record calls to Lua functions --------------------------------------- */
1392 /* Check unroll limits for calls. */
1393 static void check_call_unroll(jit_State
*J
, TraceNo lnk
)
1395 cTValue
*frame
= J
->L
->base
- 1;
1396 void *pc
= mref(frame_func(frame
)->l
.pc
, void);
1397 int32_t depth
= J
->framedepth
;
1399 if ((J
->pt
->flags
& PROTO_VARARG
)) depth
--; /* Vararg frame still missing. */
1400 for (; depth
> 0; depth
--) { /* Count frames with same prototype. */
1401 if (frame_iscont(frame
)) depth
--;
1402 frame
= frame_prev(frame
);
1403 if (mref(frame_func(frame
)->l
.pc
, void) == pc
)
1406 if (J
->pc
== J
->startpc
) {
1407 if (count
+ J
->tailcalled
> J
->param
[JIT_P_recunroll
]) {
1409 if (J
->framedepth
+ J
->retdepth
== 0)
1410 rec_stop(J
, LJ_TRLINK_TAILREC
, J
->cur
.traceno
); /* Tail-recursion. */
1412 rec_stop(J
, LJ_TRLINK_UPREC
, J
->cur
.traceno
); /* Up-recursion. */
1415 if (count
> J
->param
[JIT_P_callunroll
]) {
1416 if (lnk
) { /* Possible tail- or up-recursion. */
1417 lj_trace_flush(J
, lnk
); /* Flush trace that only returns. */
1418 /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */
1419 hotcount_set(J2GG(J
), J
->pc
+1, LJ_PRNG_BITS(J
, 4));
1421 lj_trace_err(J
, LJ_TRERR_CUNROLL
);
1426 /* Record Lua function setup. */
1427 static void rec_func_setup(jit_State
*J
)
1429 GCproto
*pt
= J
->pt
;
1430 BCReg s
, numparams
= pt
->numparams
;
1431 if ((pt
->flags
& PROTO_NOJIT
))
1432 lj_trace_err(J
, LJ_TRERR_CJITOFF
);
1433 if (J
->baseslot
+ pt
->framesize
>= LJ_MAX_JSLOTS
)
1434 lj_trace_err(J
, LJ_TRERR_STACKOV
);
1435 /* Fill up missing parameters with nil. */
1436 for (s
= J
->maxslot
; s
< numparams
; s
++)
1437 J
->base
[s
] = TREF_NIL
;
1438 /* The remaining slots should never be read before they are written. */
1439 J
->maxslot
= numparams
;
1442 /* Record Lua vararg function setup. */
1443 static void rec_func_vararg(jit_State
*J
)
1445 GCproto
*pt
= J
->pt
;
1446 BCReg s
, fixargs
, vframe
= J
->maxslot
+1;
1447 lua_assert((pt
->flags
& PROTO_VARARG
));
1448 if (J
->baseslot
+ vframe
+ pt
->framesize
>= LJ_MAX_JSLOTS
)
1449 lj_trace_err(J
, LJ_TRERR_STACKOV
);
1450 J
->base
[vframe
-1] = J
->base
[-1]; /* Copy function up. */
1451 /* Copy fixarg slots up and set their original slots to nil. */
1452 fixargs
= pt
->numparams
< J
->maxslot
? pt
->numparams
: J
->maxslot
;
1453 for (s
= 0; s
< fixargs
; s
++) {
1454 J
->base
[vframe
+s
] = J
->base
[s
];
1455 J
->base
[s
] = TREF_NIL
;
1457 J
->maxslot
= fixargs
;
1460 J
->baseslot
+= vframe
;
1463 /* Record entry to a Lua function. */
1464 static void rec_func_lua(jit_State
*J
)
1467 check_call_unroll(J
, 0);
1470 /* Record entry to an already compiled function. */
1471 static void rec_func_jit(jit_State
*J
, TraceNo lnk
)
1475 T
= traceref(J
, lnk
);
1476 if (T
->linktype
== LJ_TRLINK_RETURN
) { /* Trace returns to interpreter? */
1477 check_call_unroll(J
, lnk
);
1478 /* Temporarily unpatch JFUNC* to continue recording across function. */
1479 J
->patchins
= *J
->pc
;
1480 J
->patchpc
= (BCIns
*)J
->pc
;
1481 *J
->patchpc
= T
->startins
;
1484 J
->instunroll
= 0; /* Cannot continue across a compiled function. */
1485 if (J
->pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0)
1486 rec_stop(J
, LJ_TRLINK_TAILREC
, J
->cur
.traceno
); /* Extra tail-recursion. */
1488 rec_stop(J
, LJ_TRLINK_ROOT
, lnk
); /* Link to the function. */
1491 /* -- Vararg handling ----------------------------------------------------- */
1493 /* Detect y = select(x, ...) idiom. */
1494 static int select_detect(jit_State
*J
)
1496 BCIns ins
= J
->pc
[1];
1497 if (bc_op(ins
) == BC_CALLM
&& bc_b(ins
) == 2 && bc_c(ins
) == 1) {
1498 cTValue
*func
= &J
->L
->base
[bc_a(ins
)];
1499 if (tvisfunc(func
) && funcV(func
)->c
.ffid
== FF_select
) {
1500 TRef kfunc
= lj_ir_kfunc(J
, funcV(func
));
1501 emitir(IRTG(IR_EQ
, IRT_FUNC
), getslot(J
, bc_a(ins
)), kfunc
);
1508 /* Record vararg instruction. */
1509 static void rec_varg(jit_State
*J
, BCReg dst
, ptrdiff_t nresults
)
1511 int32_t numparams
= J
->pt
->numparams
;
1512 ptrdiff_t nvararg
= frame_delta(J
->L
->base
-1) - numparams
- 1;
1513 lua_assert(frame_isvarg(J
->L
->base
-1));
1514 if (J
->framedepth
> 0) { /* Simple case: varargs defined on-trace. */
1516 if (nvararg
< 0) nvararg
= 0;
1517 if (nresults
== -1) {
1519 J
->maxslot
= dst
+ (BCReg
)nvararg
;
1520 } else if (dst
+ nresults
> J
->maxslot
) {
1521 J
->maxslot
= dst
+ (BCReg
)nresults
;
1523 for (i
= 0; i
< nresults
; i
++)
1524 J
->base
[dst
+i
] = i
< nvararg
? getslot(J
, i
- nvararg
- 1) : TREF_NIL
;
1525 } else { /* Unknown number of varargs passed to trace. */
1526 TRef fr
= emitir(IRTI(IR_SLOAD
), 0, IRSLOAD_READONLY
|IRSLOAD_FRAME
);
1527 int32_t frofs
= 8*(1+numparams
)+FRAME_VARG
;
1528 if (nresults
>= 0) { /* Known fixed number of results. */
1531 ptrdiff_t nload
= nvararg
>= nresults
? nresults
: nvararg
;
1533 if (nvararg
>= nresults
)
1534 emitir(IRTGI(IR_GE
), fr
, lj_ir_kint(J
, frofs
+8*(int32_t)nresults
));
1536 emitir(IRTGI(IR_EQ
), fr
, lj_ir_kint(J
, frame_ftsz(J
->L
->base
-1)));
1537 vbase
= emitir(IRTI(IR_SUB
), REF_BASE
, fr
);
1538 vbase
= emitir(IRT(IR_ADD
, IRT_P32
), vbase
, lj_ir_kint(J
, frofs
-8));
1539 for (i
= 0; i
< nload
; i
++) {
1540 IRType t
= itype2irt(&J
->L
->base
[i
-1-nvararg
]);
1541 TRef aref
= emitir(IRT(IR_AREF
, IRT_P32
),
1542 vbase
, lj_ir_kint(J
, (int32_t)i
));
1543 TRef tr
= emitir(IRTG(IR_VLOAD
, t
), aref
, 0);
1544 if (irtype_ispri(t
)) tr
= TREF_PRI(t
); /* Canonicalize primitives. */
1545 J
->base
[dst
+i
] = tr
;
1548 emitir(IRTGI(IR_LE
), fr
, lj_ir_kint(J
, frofs
));
1551 for (i
= nvararg
; i
< nresults
; i
++)
1552 J
->base
[dst
+i
] = TREF_NIL
;
1553 if (dst
+ (BCReg
)nresults
> J
->maxslot
)
1554 J
->maxslot
= dst
+ (BCReg
)nresults
;
1555 } else if (select_detect(J
)) { /* y = select(x, ...) */
1556 TRef tridx
= J
->base
[dst
-1];
1558 ptrdiff_t idx
= lj_ffrecord_select_mode(J
, tridx
, &J
->L
->base
[dst
-1]);
1559 if (idx
< 0) goto nyivarg
;
1560 if (idx
!= 0 && !tref_isinteger(tridx
))
1561 tridx
= emitir(IRTGI(IR_CONV
), tridx
, IRCONV_INT_NUM
|IRCONV_INDEX
);
1562 if (idx
!= 0 && tref_isk(tridx
)) {
1563 emitir(IRTGI(idx
<= nvararg
? IR_GE
: IR_LT
),
1564 fr
, lj_ir_kint(J
, frofs
+8*(int32_t)idx
));
1565 frofs
-= 8; /* Bias for 1-based index. */
1566 } else if (idx
<= nvararg
) { /* Compute size. */
1567 TRef tmp
= emitir(IRTI(IR_ADD
), fr
, lj_ir_kint(J
, -frofs
));
1569 emitir(IRTGI(IR_GE
), tmp
, lj_ir_kint(J
, 0));
1570 tr
= emitir(IRTI(IR_BSHR
), tmp
, lj_ir_kint(J
, 3));
1572 tridx
= emitir(IRTI(IR_ADD
), tridx
, lj_ir_kint(J
, -1));
1573 rec_idx_abc(J
, tr
, tridx
, (uint32_t)nvararg
);
1576 TRef tmp
= lj_ir_kint(J
, frofs
);
1578 TRef tmp2
= emitir(IRTI(IR_BSHL
), tridx
, lj_ir_kint(J
, 3));
1579 tmp
= emitir(IRTI(IR_ADD
), tmp2
, tmp
);
1581 tr
= lj_ir_kint(J
, 0);
1583 emitir(IRTGI(IR_LT
), fr
, tmp
);
1585 if (idx
!= 0 && idx
<= nvararg
) {
1587 TRef aref
, vbase
= emitir(IRTI(IR_SUB
), REF_BASE
, fr
);
1588 vbase
= emitir(IRT(IR_ADD
, IRT_P32
), vbase
, lj_ir_kint(J
, frofs
-8));
1589 t
= itype2irt(&J
->L
->base
[idx
-2-nvararg
]);
1590 aref
= emitir(IRT(IR_AREF
, IRT_P32
), vbase
, tridx
);
1591 tr
= emitir(IRTG(IR_VLOAD
, t
), aref
, 0);
1592 if (irtype_ispri(t
)) tr
= TREF_PRI(t
); /* Canonicalize primitives. */
1594 J
->base
[dst
-2] = tr
;
1596 J
->bcskip
= 2; /* Skip CALLM + select. */
1599 setintV(&J
->errinfo
, BC_VARG
);
1600 lj_trace_err_info(J
, LJ_TRERR_NYIBC
);
1605 /* -- Record allocations -------------------------------------------------- */
1607 static TRef
rec_tnew(jit_State
*J
, uint32_t ah
)
1609 uint32_t asize
= ah
& 0x7ff;
1610 uint32_t hbits
= ah
>> 11;
1611 if (asize
== 0x7ff) asize
= 0x801;
1612 return emitir(IRTG(IR_TNEW
, IRT_TAB
), asize
, hbits
);
1615 /* -- Record bytecode ops ------------------------------------------------- */
1617 /* Prepare for comparison. */
1618 static void rec_comp_prep(jit_State
*J
)
1620 /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */
1621 if (J
->cur
.nsnap
== 1 && J
->cur
.snap
[0].ref
== J
->cur
.nins
)
1622 emitir_raw(IRT(IR_NOP
, IRT_NIL
), 0, 0);
1626 /* Fixup comparison. */
1627 static void rec_comp_fixup(jit_State
*J
, const BCIns
*pc
, int cond
)
1629 BCIns jmpins
= pc
[1];
1630 const BCIns
*npc
= pc
+ 2 + (cond
? bc_j(jmpins
) : 0);
1631 SnapShot
*snap
= &J
->cur
.snap
[J
->cur
.nsnap
-1];
1632 /* Set PC to opposite target to avoid re-recording the comp. in side trace. */
1633 J
->cur
.snapmap
[snap
->mapofs
+ snap
->nent
] = SNAP_MKPC(npc
);
1635 if (bc_a(jmpins
) < J
->maxslot
) J
->maxslot
= bc_a(jmpins
);
1636 lj_snap_shrink(J
); /* Shrink last snapshot if possible. */
1639 /* Record the next bytecode instruction (_before_ it's executed). */
1640 void lj_record_ins(jit_State
*J
)
1649 /* Perform post-processing action before recording the next instruction. */
1650 if (LJ_UNLIKELY(J
->postproc
!= LJ_POST_NONE
)) {
1651 switch (J
->postproc
) {
1652 case LJ_POST_FIXCOMP
: /* Fixup comparison. */
1653 pc
= frame_pc(&J2G(J
)->tmptv
);
1654 rec_comp_fixup(J
, pc
, (!tvistruecond(&J2G(J
)->tmptv2
) ^ (bc_op(*pc
)&1)));
1656 case LJ_POST_FIXGUARD
: /* Fixup and emit pending guard. */
1657 case LJ_POST_FIXGUARDSNAP
: /* Fixup and emit pending guard and snapshot. */
1658 if (!tvistruecond(&J2G(J
)->tmptv2
)) {
1659 J
->fold
.ins
.o
^= 1; /* Flip guard to opposite. */
1660 if (J
->postproc
== LJ_POST_FIXGUARDSNAP
) {
1661 SnapShot
*snap
= &J
->cur
.snap
[J
->cur
.nsnap
-1];
1662 J
->cur
.snapmap
[snap
->mapofs
+snap
->nent
-1]--; /* False -> true. */
1665 lj_opt_fold(J
); /* Emit pending guard. */
1667 case LJ_POST_FIXBOOL
:
1668 if (!tvistruecond(&J2G(J
)->tmptv2
)) {
1670 TValue
*tv
= J
->L
->base
;
1671 for (s
= 0; s
< J
->maxslot
; s
++) /* Fixup stack slot (if any). */
1672 if (J
->base
[s
] == TREF_TRUE
&& tvisfalse(&tv
[s
])) {
1673 J
->base
[s
] = TREF_FALSE
;
1678 case LJ_POST_FIXCONST
:
1681 TValue
*tv
= J
->L
->base
;
1682 for (s
= 0; s
< J
->maxslot
; s
++) /* Constify stack slots (if any). */
1683 if (J
->base
[s
] == TREF_NIL
&& !tvisnil(&tv
[s
]))
1684 J
->base
[s
] = lj_record_constify(J
, &tv
[s
]);
1687 case LJ_POST_FFRETRY
: /* Suppress recording of retried fast function. */
1688 if (bc_op(*J
->pc
) >= BC__MAX
)
1691 default: lua_assert(0); break;
1693 J
->postproc
= LJ_POST_NONE
;
1696 /* Need snapshot before recording next bytecode (e.g. after a store). */
1704 /* Skip some bytecodes. */
1705 if (LJ_UNLIKELY(J
->bcskip
> 0)) {
1710 /* Record only closed loops for root traces. */
1712 if (J
->framedepth
== 0 &&
1713 (MSize
)((char *)pc
- (char *)J
->bc_min
) >= J
->bc_extent
)
1714 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
1716 #ifdef LUA_USE_ASSERT
1721 /* Keep a copy of the runtime values of var/num/str operands. */
1722 #define rav (&ix.valv)
1723 #define rbv (&ix.tabv)
1724 #define rcv (&ix.keyv)
1731 switch (bcmode_a(op
)) {
1733 copyTV(J
->L
, rav
, &lbase
[ra
]); ix
.val
= ra
= getslot(J
, ra
); break;
1734 default: break; /* Handled later. */
1738 switch (bcmode_b(op
)) {
1739 case BCMnone
: rb
= 0; rc
= bc_d(ins
); break; /* Upgrade rc to 'rd'. */
1741 copyTV(J
->L
, rbv
, &lbase
[rb
]); ix
.tab
= rb
= getslot(J
, rb
); break;
1742 default: break; /* Handled later. */
1744 switch (bcmode_c(op
)) {
1746 copyTV(J
->L
, rcv
, &lbase
[rc
]); ix
.key
= rc
= getslot(J
, rc
); break;
1747 case BCMpri
: setitype(rcv
, ~rc
); ix
.key
= rc
= TREF_PRI(IRT_NIL
+rc
); break;
1748 case BCMnum
: { cTValue
*tv
= proto_knumtv(J
->pt
, rc
);
1749 copyTV(J
->L
, rcv
, tv
); ix
.key
= rc
= tvisint(tv
) ? lj_ir_kint(J
, intV(tv
)) :
1750 lj_ir_knumint(J
, numV(tv
)); } break;
1751 case BCMstr
: { GCstr
*s
= gco2str(proto_kgc(J
->pt
, ~(ptrdiff_t)rc
));
1752 setstrV(J
->L
, rcv
, s
); ix
.key
= rc
= lj_ir_kstr(J
, s
); } break;
1753 default: break; /* Handled later. */
1758 /* -- Comparison ops ---------------------------------------------------- */
1760 case BC_ISLT
: case BC_ISGE
: case BC_ISLE
: case BC_ISGT
:
1762 if (tref_iscdata(ra
) || tref_iscdata(rc
)) {
1763 rec_mm_comp_cdata(J
, &ix
, op
, ((int)op
& 2) ? MM_le
: MM_lt
);
1767 /* Emit nothing for two numeric or string consts. */
1768 if (!(tref_isk2(ra
,rc
) && tref_isnumber_str(ra
) && tref_isnumber_str(rc
))) {
1769 IRType ta
= tref_isinteger(ra
) ? IRT_INT
: tref_type(ra
);
1770 IRType tc
= tref_isinteger(rc
) ? IRT_INT
: tref_type(rc
);
1773 /* Widen mixed number/int comparisons to number/number comparison. */
1774 if (ta
== IRT_INT
&& tc
== IRT_NUM
) {
1775 ra
= emitir(IRTN(IR_CONV
), ra
, IRCONV_NUM_INT
);
1777 } else if (ta
== IRT_NUM
&& tc
== IRT_INT
) {
1778 rc
= emitir(IRTN(IR_CONV
), rc
, IRCONV_NUM_INT
);
1780 ta
= IRT_NIL
; /* Force metamethod for different types. */
1781 } else if (!((ta
== IRT_FALSE
|| ta
== IRT_TRUE
) &&
1782 (tc
== IRT_FALSE
|| tc
== IRT_TRUE
))) {
1783 break; /* Interpreter will throw for two different types. */
1787 irop
= (int)op
- (int)BC_ISLT
+ (int)IR_LT
;
1788 if (ta
== IRT_NUM
) {
1789 if ((irop
& 1)) irop
^= 4; /* ISGE/ISGT are unordered. */
1790 if (!lj_ir_numcmp(numberVnum(rav
), numberVnum(rcv
), (IROp
)irop
))
1792 } else if (ta
== IRT_INT
) {
1793 if (!lj_ir_numcmp(numberVnum(rav
), numberVnum(rcv
), (IROp
)irop
))
1795 } else if (ta
== IRT_STR
) {
1796 if (!lj_ir_strcmp(strV(rav
), strV(rcv
), (IROp
)irop
)) irop
^= 1;
1797 ra
= lj_ir_call(J
, IRCALL_lj_str_cmp
, ra
, rc
);
1798 rc
= lj_ir_kint(J
, 0);
1801 rec_mm_comp(J
, &ix
, (int)op
);
1804 emitir(IRTG(irop
, ta
), ra
, rc
);
1805 rec_comp_fixup(J
, J
->pc
, ((int)op
^ irop
) & 1);
1809 case BC_ISEQV
: case BC_ISNEV
:
1810 case BC_ISEQS
: case BC_ISNES
:
1811 case BC_ISEQN
: case BC_ISNEN
:
1812 case BC_ISEQP
: case BC_ISNEP
:
1814 if (tref_iscdata(ra
) || tref_iscdata(rc
)) {
1815 rec_mm_comp_cdata(J
, &ix
, op
, MM_eq
);
1819 /* Emit nothing for two non-table, non-udata consts. */
1820 if (!(tref_isk2(ra
, rc
) && !(tref_istab(ra
) || tref_isudata(ra
)))) {
1823 diff
= lj_record_objcmp(J
, ra
, rc
, rav
, rcv
);
1824 if (diff
== 2 || !(tref_istab(ra
) || tref_isudata(ra
)))
1825 rec_comp_fixup(J
, J
->pc
, ((int)op
& 1) == !diff
);
1826 else if (diff
== 1) /* Only check __eq if different, but same type. */
1827 rec_mm_equal(J
, &ix
, (int)op
);
1831 /* -- Unary test and copy ops ------------------------------------------- */
1833 case BC_ISTC
: case BC_ISFC
:
1834 if ((op
& 1) == tref_istruecond(rc
))
1835 rc
= 0; /* Don't store if condition is not true. */
1837 case BC_IST
: case BC_ISF
: /* Type specialization suffices. */
1838 if (bc_a(pc
[1]) < J
->maxslot
)
1839 J
->maxslot
= bc_a(pc
[1]); /* Shrink used slots. */
1842 /* -- Unary ops --------------------------------------------------------- */
1845 /* Type specialization already forces const result. */
1846 rc
= tref_istruecond(rc
) ? TREF_FALSE
: TREF_TRUE
;
1851 rc
= emitir(IRTI(IR_FLOAD
), rc
, IRFL_STR_LEN
);
1852 else if (!LJ_52
&& tref_istab(rc
))
1853 rc
= lj_ir_call(J
, IRCALL_lj_tab_len
, rc
);
1855 rc
= rec_mm_len(J
, rc
, rcv
);
1858 /* -- Arithmetic ops ---------------------------------------------------- */
1861 if (tref_isnumber_str(rc
)) {
1862 rc
= lj_opt_narrow_unm(J
, rc
, rcv
);
1865 copyTV(J
->L
, &ix
.tabv
, rcv
);
1866 rc
= rec_mm_arith(J
, &ix
, MM_unm
);
1870 case BC_ADDNV
: case BC_SUBNV
: case BC_MULNV
: case BC_DIVNV
: case BC_MODNV
:
1871 /* Swap rb/rc and rbv/rcv. rav is temp. */
1872 ix
.tab
= rc
; ix
.key
= rc
= rb
; rb
= ix
.tab
;
1873 copyTV(J
->L
, rav
, rbv
);
1874 copyTV(J
->L
, rbv
, rcv
);
1875 copyTV(J
->L
, rcv
, rav
);
1879 case BC_ADDVN
: case BC_SUBVN
: case BC_MULVN
: case BC_DIVVN
:
1880 case BC_ADDVV
: case BC_SUBVV
: case BC_MULVV
: case BC_DIVVV
: {
1881 MMS mm
= bcmode_mm(op
);
1882 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
1883 rc
= lj_opt_narrow_arith(J
, rb
, rc
, rbv
, rcv
,
1884 (int)mm
- (int)MM_add
+ (int)IR_ADD
);
1886 rc
= rec_mm_arith(J
, &ix
, mm
);
1890 case BC_MODVN
: case BC_MODVV
:
1892 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
1893 rc
= lj_opt_narrow_mod(J
, rb
, rc
, rbv
, rcv
);
1895 rc
= rec_mm_arith(J
, &ix
, MM_mod
);
1899 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
1900 rc
= lj_opt_narrow_pow(J
, rb
, rc
, rbv
, rcv
);
1902 rc
= rec_mm_arith(J
, &ix
, MM_pow
);
1905 /* -- Constant and move ops --------------------------------------------- */
1908 /* Clear gap of method call to avoid resurrecting previous refs. */
1909 if (ra
> J
->maxslot
) J
->base
[ra
-1] = 0;
1911 case BC_KSTR
: case BC_KNUM
: case BC_KPRI
:
1914 rc
= lj_ir_kint(J
, (int32_t)(int16_t)rc
);
1918 J
->base
[ra
++] = TREF_NIL
;
1919 if (rc
>= J
->maxslot
) J
->maxslot
= rc
+1;
1923 rc
= lj_ir_kgc(J
, proto_kgc(J
->pt
, ~(ptrdiff_t)rc
), IRT_CDATA
);
1927 /* -- Upvalue and function ops ------------------------------------------ */
1930 rc
= rec_upvalue(J
, rc
, 0);
1932 case BC_USETV
: case BC_USETS
: case BC_USETN
: case BC_USETP
:
1933 rec_upvalue(J
, ra
, rc
);
1936 /* -- Table ops --------------------------------------------------------- */
1938 case BC_GGET
: case BC_GSET
:
1939 settabV(J
->L
, &ix
.tabv
, tabref(J
->fn
->l
.env
));
1940 ix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), getcurrf(J
), IRFL_FUNC_ENV
);
1941 ix
.idxchain
= LJ_MAX_IDXCHAIN
;
1942 rc
= lj_record_idx(J
, &ix
);
1945 case BC_TGETB
: case BC_TSETB
:
1946 setintV(&ix
.keyv
, (int32_t)rc
);
1947 ix
.key
= lj_ir_kint(J
, (int32_t)rc
);
1949 case BC_TGETV
: case BC_TGETS
: case BC_TSETV
: case BC_TSETS
:
1950 ix
.idxchain
= LJ_MAX_IDXCHAIN
;
1951 rc
= lj_record_idx(J
, &ix
);
1955 rc
= rec_tnew(J
, rc
);
1958 rc
= emitir(IRTG(IR_TDUP
, IRT_TAB
),
1959 lj_ir_ktab(J
, gco2tab(proto_kgc(J
->pt
, ~(ptrdiff_t)rc
))), 0);
1962 /* -- Calls and vararg handling ----------------------------------------- */
1965 J
->base
[ra
] = getslot(J
, ra
-3);
1966 J
->base
[ra
+1] = getslot(J
, ra
-2);
1967 J
->base
[ra
+2] = getslot(J
, ra
-1);
1968 { /* Do the actual copy now because lj_record_call needs the values. */
1969 TValue
*b
= &J
->L
->base
[ra
];
1970 copyTV(J
->L
, b
, b
-3);
1971 copyTV(J
->L
, b
+1, b
-2);
1972 copyTV(J
->L
, b
+2, b
-1);
1974 lj_record_call(J
, ra
, (ptrdiff_t)rc
-1);
1977 /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */
1979 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
;
1982 lj_record_call(J
, ra
, (ptrdiff_t)rc
-1);
1986 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
;
1989 lj_record_tailcall(J
, ra
, (ptrdiff_t)rc
-1);
1993 rec_varg(J
, ra
, (ptrdiff_t)rb
-1);
1996 /* -- Returns ----------------------------------------------------------- */
1999 /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */
2000 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
+ 1;
2002 case BC_RET
: case BC_RET0
: case BC_RET1
:
2003 lj_record_ret(J
, ra
, (ptrdiff_t)rc
-1);
2006 /* -- Loops and branches ------------------------------------------------ */
2009 if (rec_for(J
, pc
, 0) != LOOPEV_LEAVE
)
2010 J
->loopref
= J
->cur
.nins
;
2013 lua_assert(bc_op(pc
[(ptrdiff_t)rc
-BCBIAS_J
]) == BC_JFORL
);
2014 if (rec_for(J
, pc
, 0) != LOOPEV_LEAVE
) /* Link to existing loop. */
2015 rec_stop(J
, LJ_TRLINK_ROOT
, bc_d(pc
[(ptrdiff_t)rc
-BCBIAS_J
]));
2016 /* Continue tracing if the loop is not entered. */
2020 rec_loop_interp(J
, pc
, rec_for(J
, pc
+((ptrdiff_t)rc
-BCBIAS_J
), 1));
2023 rec_loop_interp(J
, pc
, rec_iterl(J
, *pc
));
2026 rec_loop_interp(J
, pc
, rec_loop(J
, ra
));
2030 rec_loop_jit(J
, rc
, rec_for(J
, pc
+bc_j(traceref(J
, rc
)->startins
), 1));
2033 rec_loop_jit(J
, rc
, rec_iterl(J
, traceref(J
, rc
)->startins
));
2036 rec_loop_jit(J
, rc
, rec_loop(J
, ra
));
2044 lj_trace_err(J
, LJ_TRERR_BLACKL
);
2048 if (ra
< J
->maxslot
)
2049 J
->maxslot
= ra
; /* Shrink used slots. */
2052 /* -- Function headers -------------------------------------------------- */
2058 rec_func_jit(J
, rc
);
2066 lua_assert(0); /* Cannot happen. No hotcall counting for varag funcs. */
2071 lj_ffrecord_func(J
);
2075 if (op
>= BC__MAX
) {
2076 lj_ffrecord_func(J
);
2086 setintV(&J
->errinfo
, (int32_t)op
);
2087 lj_trace_err_info(J
, LJ_TRERR_NYIBC
);
2091 /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */
2092 if (bcmode_a(op
) == BCMdst
&& rc
) {
2094 if (ra
>= J
->maxslot
) J
->maxslot
= ra
+1;
2101 /* Limit the number of recorded IR instructions. */
2102 if (J
->cur
.nins
> REF_FIRST
+(IRRef
)J
->param
[JIT_P_maxrecord
])
2103 lj_trace_err(J
, LJ_TRERR_TRACEOV
);
2106 /* -- Recording setup ----------------------------------------------------- */
2108 /* Setup recording for a root trace started by a hot loop. */
2109 static const BCIns
*rec_setup_root(jit_State
*J
)
2111 /* Determine the next PC and the bytecode range for the loop. */
2112 const BCIns
*pcj
, *pc
= J
->pc
;
2114 BCReg ra
= bc_a(ins
);
2115 switch (bc_op(ins
)) {
2117 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2122 lua_assert(bc_op(pc
[-1]) == BC_ITERC
);
2123 J
->maxslot
= ra
+ bc_b(pc
[-1]) - 1;
2124 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2126 lua_assert(bc_op(pc
[-1]) == BC_JMP
);
2130 /* Only check BC range for real loops, but not for "repeat until true". */
2131 pcj
= pc
+ bc_j(ins
);
2133 if (bc_op(ins
) == BC_JMP
&& bc_j(ins
) < 0) {
2134 J
->bc_min
= pcj
+1 + bc_j(ins
);
2135 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2143 /* No bytecode range check for down-recursive root traces. */
2144 J
->maxslot
= ra
+ bc_d(ins
) - 1;
2147 /* No bytecode range check for root traces started by a hot call. */
2148 J
->maxslot
= J
->pt
->numparams
;
2158 /* Setup for recording a new trace. */
2159 void lj_record_setup(jit_State
*J
)
2163 /* Initialize state related to current trace. */
2164 memset(J
->slot
, 0, sizeof(J
->slot
));
2165 memset(J
->chain
, 0, sizeof(J
->chain
));
2166 memset(J
->bpropcache
, 0, sizeof(J
->bpropcache
));
2167 J
->scev
.idx
= REF_NIL
;
2168 setmref(J
->scev
.pc
, NULL
);
2170 J
->baseslot
= 1; /* Invoking function is at base[-1]. */
2171 J
->base
= J
->slot
+ J
->baseslot
;
2176 J
->instunroll
= J
->param
[JIT_P_instunroll
];
2177 J
->loopunroll
= J
->param
[JIT_P_loopunroll
];
2181 J
->bc_min
= NULL
; /* Means no limit. */
2182 J
->bc_extent
= ~(MSize
)0;
2184 /* Emit instructions for fixed references. Also triggers initial IR alloc. */
2185 emitir_raw(IRT(IR_BASE
, IRT_P32
), J
->parent
, J
->exitno
);
2186 for (i
= 0; i
<= 2; i
++) {
2187 IRIns
*ir
= IR(REF_NIL
-i
);
2189 ir
->t
.irt
= (uint8_t)(IRT_NIL
+i
);
2193 J
->cur
.nk
= REF_TRUE
;
2196 setmref(J
->cur
.startpc
, J
->pc
);
2197 if (J
->parent
) { /* Side trace. */
2198 GCtrace
*T
= traceref(J
, J
->parent
);
2199 TraceNo root
= T
->root
? T
->root
: J
->parent
;
2200 J
->cur
.root
= (uint16_t)root
;
2201 J
->cur
.startins
= BCINS_AD(BC_JMP
, 0, 0);
2202 /* Check whether we could at least potentially form an extra loop. */
2203 if (J
->exitno
== 0 && T
->snap
[0].nent
== 0) {
2204 /* We can narrow a FORL for some side traces, too. */
2205 if (J
->pc
> proto_bc(J
->pt
) && bc_op(J
->pc
[-1]) == BC_JFORI
&&
2206 bc_d(J
->pc
[bc_j(J
->pc
[-1])-1]) == root
) {
2208 rec_for_loop(J
, J
->pc
-1, &J
->scev
, 1);
2212 J
->startpc
= NULL
; /* Prevent forming an extra loop. */
2214 lj_snap_replay(J
, T
);
2216 if (traceref(J
, J
->cur
.root
)->nchild
>= J
->param
[JIT_P_maxside
] ||
2217 T
->snap
[J
->exitno
].count
>= J
->param
[JIT_P_hotexit
] +
2218 J
->param
[JIT_P_tryside
]) {
2219 rec_stop(J
, LJ_TRLINK_INTERP
, 0);
2221 } else { /* Root trace. */
2223 J
->cur
.startins
= *J
->pc
;
2224 J
->pc
= rec_setup_root(J
);
2225 /* Note: the loop instruction itself is recorded at the end and not
2226 ** at the start! So snapshot #0 needs to point to the *next* instruction.
2229 if (bc_op(J
->cur
.startins
) == BC_FORL
)
2230 rec_for_loop(J
, J
->pc
-1, &J
->scev
, 1);
2231 if (1 + J
->pt
->framesize
>= LJ_MAX_JSLOTS
)
2232 lj_trace_err(J
, LJ_TRERR_STACKOV
);
2234 #ifdef LUAJIT_ENABLE_CHECKHOOK
2235 /* Regularly check for instruction/line hooks from compiled code and
2236 ** exit to the interpreter if the hooks are set.
2238 ** This is a compile-time option and disabled by default, since the
2239 ** hook checks may be quite expensive in tight loops.
2241 ** Note this is only useful if hooks are *not* set most of the time.
2242 ** Use this only if you want to *asynchronously* interrupt the execution.
2244 ** You can set the instruction hook via lua_sethook() with a count of 1
2245 ** from a signal handler or another native thread. Please have a look
2246 ** at the first few functions in luajit.c for an example (Ctrl-C handler).
2249 TRef tr
= emitir(IRT(IR_XLOAD
, IRT_U8
),
2250 lj_ir_kptr(J
, &J2G(J
)->hookmask
), IRXLOAD_VOLATILE
);
2251 tr
= emitir(IRTI(IR_BAND
), tr
, lj_ir_kint(J
, (LUA_MASKLINE
|LUA_MASKCOUNT
)));
2252 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, 0));