2 ** Trace recorder (bytecode -> SSA IR).
3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
28 #include "lj_ircall.h"
31 #include "lj_record.h"
32 #include "lj_ffrecord.h"
34 #include "lj_dispatch.h"
38 /* Some local macros to save typing. Undef'd at the end. */
39 #define IR(ref) (&J->cur.ir[(ref)])
41 /* Pass IR on to next optimization in chain (FOLD). */
42 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
44 /* Emit raw IR without passing through optimizations. */
45 #define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))
47 /* -- Sanity checks ------------------------------------------------------- */
50 /* Sanity check the whole IR -- sloooow. */
51 static void rec_check_ir(jit_State
*J
)
53 IRRef i
, nins
= J
->cur
.nins
, nk
= J
->cur
.nk
;
54 lj_assertJ(nk
<= REF_BIAS
&& nins
>= REF_BIAS
&& nins
< 65536,
55 "inconsistent IR layout");
56 for (i
= nk
; i
< nins
; i
++) {
58 uint32_t mode
= lj_ir_mode
[ir
->o
];
61 const char *err
= NULL
;
62 switch (irm_op1(mode
)) {
64 if (op1
!= 0) err
= "IRMnone op1 used";
67 if (op1
< nk
|| (i
>= REF_BIAS
? op1
>= i
: op1
<= i
))
68 err
= "IRMref op1 out of range";
72 if (i
>= REF_BIAS
) { err
= "constant in IR range"; break; }
73 if (irt_is64(ir
->t
) && ir
->o
!= IR_KNULL
)
77 switch (irm_op2(mode
)) {
79 if (op2
) err
= "IRMnone op2 used";
82 if (op2
< nk
|| (i
>= REF_BIAS
? op2
>= i
: op2
<= i
))
83 err
= "IRMref op2 out of range";
86 case IRMcst
: err
= "IRMcst op2"; break;
88 if (!err
&& ir
->prev
) {
89 if (ir
->prev
< nk
|| (i
>= REF_BIAS
? ir
->prev
>= i
: ir
->prev
<= i
))
90 err
= "chain out of range";
91 else if (ir
->o
!= IR_NOP
&& IR(ir
->prev
)->o
!= ir
->o
)
92 err
= "chain to different op";
94 lj_assertJ(!err
, "bad IR %04d op %d(%04d,%04d): %s",
97 irm_op1(mode
) == IRMref
? op1
-REF_BIAS
: op1
,
98 irm_op2(mode
) == IRMref
? op2
-REF_BIAS
: op2
,
103 /* Compare stack slots and frames of the recorder and the VM. */
104 static void rec_check_slots(jit_State
*J
)
106 BCReg s
, nslots
= J
->baseslot
+ J
->maxslot
;
108 cTValue
*base
= J
->L
->base
- J
->baseslot
;
109 lj_assertJ(J
->baseslot
>= 1+LJ_FR2
, "bad baseslot");
110 lj_assertJ(J
->baseslot
== 1+LJ_FR2
|| (J
->slot
[J
->baseslot
-1] & TREF_FRAME
),
111 "baseslot does not point to frame");
112 lj_assertJ(nslots
<= LJ_MAX_JSLOTS
, "slot overflow");
113 for (s
= 0; s
< nslots
; s
++) {
114 TRef tr
= J
->slot
[s
];
116 cTValue
*tv
= &base
[s
];
117 IRRef ref
= tref_ref(tr
);
118 IRIns
*ir
= NULL
; /* Silence compiler. */
119 lj_assertJ(tv
< J
->L
->top
, "slot %d above top of Lua stack", s
);
120 if (!LJ_FR2
|| ref
|| !(tr
& (TREF_FRAME
| TREF_CONT
))) {
121 lj_assertJ(ref
>= J
->cur
.nk
&& ref
< J
->cur
.nins
,
122 "slot %d ref %04d out of range", s
, ref
- REF_BIAS
);
124 lj_assertJ(irt_t(ir
->t
) == tref_t(tr
), "slot %d IR type mismatch", s
);
127 lj_assertJ(tref_isfunc(tr
), "frame slot 0 is not a function");
130 lj_assertJ((tr
& ~TREF_FRAME
) == 0, "bad frame slot 1");
132 } else if ((tr
& TREF_FRAME
)) {
133 GCfunc
*fn
= gco2func(frame_gc(tv
));
134 BCReg delta
= (BCReg
)(tv
- frame_prev(tv
));
136 lj_assertJ(!ref
|| ir_knum(ir
)->u64
== tv
->u64
,
137 "frame slot %d PC mismatch", s
);
139 ir
= IR(tref_ref(tr
));
141 lj_assertJ(tref_isfunc(tr
),
142 "frame slot %d is not a function", s
-LJ_FR2
);
143 lj_assertJ(!tref_isk(tr
) || fn
== ir_kfunc(ir
),
144 "frame slot %d function mismatch", s
-LJ_FR2
);
145 lj_assertJ(s
> delta
+ LJ_FR2
? (J
->slot
[s
-delta
] & TREF_FRAME
)
146 : (s
== delta
+ LJ_FR2
),
147 "frame slot %d broken chain", s
-LJ_FR2
);
149 } else if ((tr
& TREF_CONT
)) {
151 lj_assertJ(!ref
|| ir_knum(ir
)->u64
== tv
->u64
,
152 "cont slot %d continuation mismatch", s
);
154 lj_assertJ(ir_kptr(ir
) == gcrefp(tv
->gcr
, void),
155 "cont slot %d continuation mismatch", s
);
157 lj_assertJ((J
->slot
[s
+1+LJ_FR2
] & TREF_FRAME
),
158 "cont slot %d not followed by frame", s
);
160 } else if ((tr
& TREF_KEYINDEX
)) {
161 lj_assertJ(tref_isint(tr
), "keyindex slot %d bad type %d",
164 /* Number repr. may differ, but other types must be the same. */
165 lj_assertJ(tvisnumber(tv
) ? tref_isnumber(tr
) :
166 itype2irt(tv
) == tref_type(tr
),
167 "slot %d type mismatch: stack type %d vs IR type %d",
168 s
, itypemap(tv
), tref_type(tr
));
169 if (tref_isk(tr
)) { /* Compare constants. */
171 lj_ir_kvalue(J
->L
, &tvk
, ir
);
172 lj_assertJ((tvisnum(&tvk
) && tvisnan(&tvk
)) ?
173 (tvisnum(tv
) && tvisnan(tv
)) :
174 lj_obj_equal(tv
, &tvk
),
175 "slot %d const mismatch: stack %016llx vs IR %016llx",
176 s
, tv
->u64
, tvk
.u64
);
181 lj_assertJ(J
->framedepth
== depth
,
182 "frame depth mismatch %d vs %d", J
->framedepth
, depth
);
186 /* -- Type handling and specialization ------------------------------------ */
188 /* Note: these functions return tagged references (TRef). */
190 /* Specialize a slot to a specific type. Note: slot can be negative! */
191 static TRef
sloadt(jit_State
*J
, int32_t slot
, IRType t
, int mode
)
193 /* Caller may set IRT_GUARD in t. */
194 TRef ref
= emitir_raw(IRT(IR_SLOAD
, t
), (int32_t)J
->baseslot
+slot
, mode
);
199 /* Specialize a slot to the runtime type. Note: slot can be negative! */
200 static TRef
sload(jit_State
*J
, int32_t slot
)
202 IRType t
= itype2irt(&J
->L
->base
[slot
]);
203 TRef ref
= emitir_raw(IRTG(IR_SLOAD
, t
), (int32_t)J
->baseslot
+slot
,
205 if (irtype_ispri(t
)) ref
= TREF_PRI(t
); /* Canonicalize primitive refs. */
210 /* Get TRef from slot. Load slot and specialize if not done already. */
211 #define getslot(J, s) (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s)))
213 /* Get TRef for current function. */
214 static TRef
getcurrf(jit_State
*J
)
216 if (J
->base
[-1-LJ_FR2
])
217 return J
->base
[-1-LJ_FR2
];
218 /* Non-base frame functions ought to be loaded already. */
219 lj_assertJ(J
->baseslot
== 1+LJ_FR2
, "bad baseslot");
220 return sloadt(J
, -1-LJ_FR2
, IRT_FUNC
, IRSLOAD_READONLY
);
223 /* Compare for raw object equality.
224 ** Returns 0 if the objects are the same.
225 ** Returns 1 if they are different, but the same type.
226 ** Returns 2 for two different types.
227 ** Comparisons between primitives always return 1 -- no caller cares about it.
229 int lj_record_objcmp(jit_State
*J
, TRef a
, TRef b
, cTValue
*av
, cTValue
*bv
)
231 int diff
= !lj_obj_equal(av
, bv
);
232 if (!tref_isk2(a
, b
)) { /* Shortcut, also handles primitives. */
233 IRType ta
= tref_isinteger(a
) ? IRT_INT
: tref_type(a
);
234 IRType tb
= tref_isinteger(b
) ? IRT_INT
: tref_type(b
);
236 /* Widen mixed number/int comparisons to number/number comparison. */
237 if (ta
== IRT_INT
&& tb
== IRT_NUM
) {
238 a
= emitir(IRTN(IR_CONV
), a
, IRCONV_NUM_INT
);
240 } else if (ta
== IRT_NUM
&& tb
== IRT_INT
) {
241 b
= emitir(IRTN(IR_CONV
), b
, IRCONV_NUM_INT
);
243 return 2; /* Two different types are never equal. */
246 emitir(IRTG(diff
? IR_NE
: IR_EQ
, ta
), a
, b
);
251 /* Constify a value. Returns 0 for non-representable object types. */
252 TRef
lj_record_constify(jit_State
*J
, cTValue
*o
)
255 return lj_ir_kgc(J
, gcV(o
), itype2irt(o
));
257 return lj_ir_kint(J
, intV(o
));
259 return lj_ir_knumint(J
, numV(o
));
260 else if (tvisbool(o
))
261 return TREF_PRI(itype2irt(o
));
263 return 0; /* Can't represent lightuserdata (pointless). */
266 /* Emit a VLOAD with the correct type. */
267 TRef
lj_record_vload(jit_State
*J
, TRef ref
, MSize idx
, IRType t
)
269 TRef tr
= emitir(IRTG(IR_VLOAD
, t
), ref
, idx
);
270 if (irtype_ispri(t
)) tr
= TREF_PRI(t
); /* Canonicalize primitives. */
274 /* -- Record loop ops ----------------------------------------------------- */
278 LOOPEV_LEAVE
, /* Loop is left or not entered. */
279 LOOPEV_ENTERLO
, /* Loop is entered with a low iteration count left. */
280 LOOPEV_ENTER
/* Loop is entered. */
283 /* Canonicalize slots: convert integers to numbers. */
284 static void canonicalize_slots(jit_State
*J
)
287 if (LJ_DUALNUM
) return;
288 for (s
= J
->baseslot
+J
->maxslot
-1; s
>= 1; s
--) {
289 TRef tr
= J
->slot
[s
];
290 if (tref_isinteger(tr
) && !(tr
& TREF_KEYINDEX
)) {
291 IRIns
*ir
= IR(tref_ref(tr
));
292 if (!(ir
->o
== IR_SLOAD
&& (ir
->op2
& (IRSLOAD_READONLY
))))
293 J
->slot
[s
] = emitir(IRTN(IR_CONV
), tr
, IRCONV_NUM_INT
);
298 /* Stop recording. */
299 void lj_record_stop(jit_State
*J
, TraceLink linktype
, TraceNo lnk
)
301 #ifdef LUAJIT_ENABLE_TABLE_BUMP
303 lj_trace_err(J
, LJ_TRERR_RETRY
);
306 J
->cur
.linktype
= (uint8_t)linktype
;
307 J
->cur
.link
= (uint16_t)lnk
;
308 /* Looping back at the same stack level? */
309 if (lnk
== J
->cur
.traceno
&& J
->framedepth
+ J
->retdepth
== 0) {
310 if ((J
->flags
& JIT_F_OPT_LOOP
)) /* Shall we try to create a loop? */
311 goto nocanon
; /* Do not canonicalize or we lose the narrowing. */
312 if (J
->cur
.root
) /* Otherwise ensure we always link to the root trace. */
313 J
->cur
.link
= J
->cur
.root
;
315 canonicalize_slots(J
);
317 /* Note: all loop ops must set J->pc to the following instruction! */
318 lj_snap_add(J
); /* Add loop snapshot. */
320 J
->mergesnap
= 1; /* In case recording continues. */
323 /* Search bytecode backwards for a int/num constant slot initializer. */
324 static TRef
find_kinit(jit_State
*J
, const BCIns
*endpc
, BCReg slot
, IRType t
)
326 /* This algorithm is rather simplistic and assumes quite a bit about
327 ** how the bytecode is generated. It works fine for FORI initializers,
328 ** but it won't necessarily work in other cases (e.g. iterator arguments).
329 ** It doesn't do anything fancy, either (like backpropagating MOVs).
331 const BCIns
*pc
, *startpc
= proto_bc(J
->pt
);
332 for (pc
= endpc
-1; pc
> startpc
; pc
--) {
334 BCOp op
= bc_op(ins
);
335 /* First try to find the last instruction that stores to this slot. */
336 if (bcmode_a(op
) == BCMbase
&& bc_a(ins
) <= slot
) {
337 return 0; /* Multiple results, e.g. from a CALL or KNIL. */
338 } else if (bcmode_a(op
) == BCMdst
&& bc_a(ins
) == slot
) {
339 if (op
== BC_KSHORT
|| op
== BC_KNUM
) { /* Found const. initializer. */
340 /* Now try to verify there's no forward jump across it. */
341 const BCIns
*kpc
= pc
;
342 for (; pc
> startpc
; pc
--)
343 if (bc_op(*pc
) == BC_JMP
) {
344 const BCIns
*target
= pc
+bc_j(*pc
)+1;
345 if (target
> kpc
&& target
<= endpc
)
346 return 0; /* Conditional assignment. */
348 if (op
== BC_KSHORT
) {
349 int32_t k
= (int32_t)(int16_t)bc_d(ins
);
350 return t
== IRT_INT
? lj_ir_kint(J
, k
) : lj_ir_knum(J
, (lua_Number
)k
);
352 cTValue
*tv
= proto_knumtv(J
->pt
, bc_d(ins
));
354 int32_t k
= numberVint(tv
);
355 if (tvisint(tv
) || numV(tv
) == (lua_Number
)k
) /* -0 is ok here. */
356 return lj_ir_kint(J
, k
);
357 return 0; /* Type mismatch. */
359 return lj_ir_knum(J
, numberVnum(tv
));
363 return 0; /* Non-constant initializer. */
366 return 0; /* No assignment to this slot found? */
369 /* Load and optionally convert a FORI argument from a slot. */
370 static TRef
fori_load(jit_State
*J
, BCReg slot
, IRType t
, int mode
)
372 int conv
= (tvisint(&J
->L
->base
[slot
]) != (t
==IRT_INT
)) ? IRSLOAD_CONVERT
: 0;
373 return sloadt(J
, (int32_t)slot
,
374 t
+ (((mode
& IRSLOAD_TYPECHECK
) ||
375 (conv
&& t
== IRT_INT
&& !(mode
>> 16))) ?
380 /* Peek before FORI to find a const initializer. Otherwise load from slot. */
381 static TRef
fori_arg(jit_State
*J
, const BCIns
*fori
, BCReg slot
,
384 TRef tr
= J
->base
[slot
];
386 tr
= find_kinit(J
, fori
, slot
, t
);
388 tr
= fori_load(J
, slot
, t
, mode
);
393 /* Return the direction of the FOR loop iterator.
394 ** It's important to exactly reproduce the semantics of the interpreter.
396 static int rec_for_direction(cTValue
*o
)
398 return (tvisint(o
) ? intV(o
) : (int32_t)o
->u32
.hi
) >= 0;
401 /* Simulate the runtime behavior of the FOR loop iterator. */
402 static LoopEvent
rec_for_iter(IROp
*op
, cTValue
*o
, int isforl
)
404 lua_Number stopv
= numberVnum(&o
[FORL_STOP
]);
405 lua_Number idxv
= numberVnum(&o
[FORL_IDX
]);
406 lua_Number stepv
= numberVnum(&o
[FORL_STEP
]);
409 if (rec_for_direction(&o
[FORL_STEP
])) {
412 return idxv
+ 2*stepv
> stopv
? LOOPEV_ENTERLO
: LOOPEV_ENTER
;
414 *op
= IR_GT
; return LOOPEV_LEAVE
;
418 return idxv
+ 2*stepv
< stopv
? LOOPEV_ENTERLO
: LOOPEV_ENTER
;
420 *op
= IR_LT
; return LOOPEV_LEAVE
;
424 /* Record checks for FOR loop overflow and step direction. */
425 static void rec_for_check(jit_State
*J
, IRType t
, int dir
,
426 TRef stop
, TRef step
, int init
)
428 if (!tref_isk(step
)) {
429 /* Non-constant step: need a guard for the direction. */
430 TRef zero
= (t
== IRT_INT
) ? lj_ir_kint(J
, 0) : lj_ir_knum_zero(J
);
431 emitir(IRTG(dir
? IR_GE
: IR_LT
, t
), step
, zero
);
432 /* Add hoistable overflow checks for a narrowed FORL index. */
433 if (init
&& t
== IRT_INT
) {
434 if (tref_isk(stop
)) {
435 /* Constant stop: optimize check away or to a range check for step. */
436 int32_t k
= IR(tref_ref(stop
))->i
;
439 emitir(IRTGI(IR_LE
), step
, lj_ir_kint(J
, (int32_t)0x7fffffff-k
));
442 emitir(IRTGI(IR_GE
), step
, lj_ir_kint(J
, (int32_t)0x80000000-k
));
445 /* Stop+step variable: need full overflow check. */
446 TRef tr
= emitir(IRTGI(IR_ADDOV
), step
, stop
);
447 emitir(IRTI(IR_USE
), tr
, 0); /* ADDOV is weak. Avoid dead result. */
450 } else if (init
&& t
== IRT_INT
&& !tref_isk(stop
)) {
451 /* Constant step: optimize overflow check to a range check for stop. */
452 int32_t k
= IR(tref_ref(step
))->i
;
453 k
= (int32_t)(dir
? 0x7fffffff : 0x80000000) - k
;
454 emitir(IRTGI(dir
? IR_LE
: IR_GE
), stop
, lj_ir_kint(J
, k
));
458 /* Record a FORL instruction. */
459 static void rec_for_loop(jit_State
*J
, const BCIns
*fori
, ScEvEntry
*scev
,
462 BCReg ra
= bc_a(*fori
);
463 cTValue
*tv
= &J
->L
->base
[ra
];
464 TRef idx
= J
->base
[ra
+FORL_IDX
];
465 IRType t
= idx
? tref_type(idx
) :
466 (init
|| LJ_DUALNUM
) ? lj_opt_narrow_forl(J
, tv
) : IRT_NUM
;
467 int mode
= IRSLOAD_INHERIT
+
468 ((!LJ_DUALNUM
|| tvisint(tv
) == (t
== IRT_INT
)) ? IRSLOAD_READONLY
: 0);
469 TRef stop
= fori_arg(J
, fori
, ra
+FORL_STOP
, t
, mode
);
470 TRef step
= fori_arg(J
, fori
, ra
+FORL_STEP
, t
, mode
);
471 int tc
, dir
= rec_for_direction(&tv
[FORL_STEP
]);
472 lj_assertJ(bc_op(*fori
) == BC_FORI
|| bc_op(*fori
) == BC_JFORI
,
473 "bad bytecode %d instead of FORI/JFORI", bc_op(*fori
));
476 scev
->stop
= tref_ref(stop
);
477 scev
->step
= tref_ref(step
);
478 rec_for_check(J
, t
, dir
, stop
, step
, init
);
479 scev
->start
= tref_ref(find_kinit(J
, fori
, ra
+FORL_IDX
, IRT_INT
));
481 !(scev
->start
&& irref_isk(scev
->stop
) && irref_isk(scev
->step
) &&
482 tvisint(&tv
[FORL_IDX
]) == (t
== IRT_INT
))) ?
483 IRSLOAD_TYPECHECK
: 0;
485 J
->base
[ra
+FORL_STOP
] = stop
;
486 J
->base
[ra
+FORL_STEP
] = step
;
489 idx
= fori_load(J
, ra
+FORL_IDX
, t
,
490 IRSLOAD_INHERIT
+ tc
+ (J
->scev
.start
<< 16));
492 J
->base
[ra
+FORL_IDX
] = idx
= emitir(IRT(IR_ADD
, t
), idx
, step
);
493 J
->base
[ra
+FORL_EXT
] = idx
;
494 scev
->idx
= tref_ref(idx
);
495 setmref(scev
->pc
, fori
);
496 J
->maxslot
= ra
+FORL_EXT
+1;
499 /* Record FORL/JFORL or FORI/JFORI. */
500 static LoopEvent
rec_for(jit_State
*J
, const BCIns
*fori
, int isforl
)
502 BCReg ra
= bc_a(*fori
);
503 TValue
*tv
= &J
->L
->base
[ra
];
504 TRef
*tr
= &J
->base
[ra
];
509 if (isforl
) { /* Handle FORL/JFORL opcodes. */
510 TRef idx
= tr
[FORL_IDX
];
511 if (mref(J
->scev
.pc
, const BCIns
) == fori
&& tref_ref(idx
) == J
->scev
.idx
) {
514 idx
= emitir(IRT(IR_ADD
, t
), idx
, J
->scev
.step
);
515 tr
[FORL_EXT
] = tr
[FORL_IDX
] = idx
;
518 rec_for_loop(J
, fori
, &scev
, 0);
522 } else { /* Handle FORI/JFORI opcodes. */
524 lj_meta_for(J
->L
, tv
);
525 t
= (LJ_DUALNUM
|| tref_isint(tr
[FORL_IDX
])) ? lj_opt_narrow_forl(J
, tv
) :
527 for (i
= FORL_IDX
; i
<= FORL_STEP
; i
++) {
528 if (!tr
[i
]) sload(J
, ra
+i
);
529 lj_assertJ(tref_isnumber_str(tr
[i
]), "bad FORI argument type");
530 if (tref_isstr(tr
[i
]))
531 tr
[i
] = emitir(IRTG(IR_STRTO
, IRT_NUM
), tr
[i
], 0);
533 if (!tref_isinteger(tr
[i
]))
534 tr
[i
] = emitir(IRTGI(IR_CONV
), tr
[i
], IRCONV_INT_NUM
|IRCONV_CHECK
);
536 if (!tref_isnum(tr
[i
]))
537 tr
[i
] = emitir(IRTN(IR_CONV
), tr
[i
], IRCONV_NUM_INT
);
540 tr
[FORL_EXT
] = tr
[FORL_IDX
];
541 stop
= tr
[FORL_STOP
];
542 rec_for_check(J
, t
, rec_for_direction(&tv
[FORL_STEP
]),
543 stop
, tr
[FORL_STEP
], 1);
546 ev
= rec_for_iter(&op
, tv
, isforl
);
547 if (ev
== LOOPEV_LEAVE
) {
548 J
->maxslot
= ra
+FORL_EXT
+1;
552 J
->pc
= fori
+bc_j(*fori
)+1;
556 emitir(IRTG(op
, t
), tr
[FORL_IDX
], stop
);
558 if (ev
== LOOPEV_LEAVE
) {
560 J
->pc
= fori
+bc_j(*fori
)+1;
562 J
->maxslot
= ra
+FORL_EXT
+1;
569 /* Record ITERL/JITERL. */
570 static LoopEvent
rec_iterl(jit_State
*J
, const BCIns iterins
)
572 BCReg ra
= bc_a(iterins
);
573 if (!tref_isnil(getslot(J
, ra
))) { /* Looping back? */
574 J
->base
[ra
-1] = J
->base
[ra
]; /* Copy result of ITERC to control var. */
575 J
->maxslot
= ra
-1+bc_b(J
->pc
[-1]);
576 J
->pc
+= bc_j(iterins
)+1;
585 /* Record LOOP/JLOOP. Now, that was easy. */
586 static LoopEvent
rec_loop(jit_State
*J
, BCReg ra
, int skip
)
588 if (ra
< J
->maxslot
) J
->maxslot
= ra
;
593 /* Check if a loop repeatedly failed to trace because it didn't loop back. */
594 static int innerloopleft(jit_State
*J
, const BCIns
*pc
)
597 for (i
= 0; i
< PENALTY_SLOTS
; i
++)
598 if (mref(J
->penalty
[i
].pc
, const BCIns
) == pc
) {
599 if ((J
->penalty
[i
].reason
== LJ_TRERR_LLEAVE
||
600 J
->penalty
[i
].reason
== LJ_TRERR_LINNER
) &&
601 J
->penalty
[i
].val
>= 2*PENALTY_MIN
)
608 /* Handle the case when an interpreted loop op is hit. */
609 static void rec_loop_interp(jit_State
*J
, const BCIns
*pc
, LoopEvent ev
)
611 if (J
->parent
== 0 && J
->exitno
== 0) {
612 if (pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0) {
613 if (bc_op(J
->cur
.startins
) == BC_ITERN
) return; /* See rec_itern(). */
615 if (ev
== LOOPEV_LEAVE
) /* Must loop back to form a root trace. */
616 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
617 lj_record_stop(J
, LJ_TRLINK_LOOP
, J
->cur
.traceno
); /* Looping trace. */
618 } else if (ev
!= LOOPEV_LEAVE
) { /* Entering inner loop? */
619 /* It's usually better to abort here and wait until the inner loop
620 ** is traced. But if the inner loop repeatedly didn't loop back,
621 ** this indicates a low trip count. In this case try unrolling
622 ** an inner loop even in a root trace. But it's better to be a bit
623 ** more conservative here and only do it for very short loops.
625 if (bc_j(*pc
) != -1 && !innerloopleft(J
, pc
))
626 lj_trace_err(J
, LJ_TRERR_LINNER
); /* Root trace hit an inner loop. */
627 if ((ev
!= LOOPEV_ENTERLO
&&
628 J
->loopref
&& J
->cur
.nins
- J
->loopref
> 24) || --J
->loopunroll
< 0)
629 lj_trace_err(J
, LJ_TRERR_LUNROLL
); /* Limit loop unrolling. */
630 J
->loopref
= J
->cur
.nins
;
632 } else if (ev
!= LOOPEV_LEAVE
) { /* Side trace enters an inner loop. */
633 J
->loopref
= J
->cur
.nins
;
634 if (--J
->loopunroll
< 0)
635 lj_trace_err(J
, LJ_TRERR_LUNROLL
); /* Limit loop unrolling. */
636 } /* Side trace continues across a loop that's left or not entered. */
639 /* Handle the case when an already compiled loop op is hit. */
640 static void rec_loop_jit(jit_State
*J
, TraceNo lnk
, LoopEvent ev
)
642 if (J
->parent
== 0 && J
->exitno
== 0) { /* Root trace hit an inner loop. */
643 /* Better let the inner loop spawn a side trace back here. */
644 lj_trace_err(J
, LJ_TRERR_LINNER
);
645 } else if (ev
!= LOOPEV_LEAVE
) { /* Side trace enters a compiled loop. */
646 J
->instunroll
= 0; /* Cannot continue across a compiled loop op. */
647 if (J
->pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0)
648 lj_record_stop(J
, LJ_TRLINK_LOOP
, J
->cur
.traceno
); /* Form extra loop. */
650 lj_record_stop(J
, LJ_TRLINK_ROOT
, lnk
); /* Link to the loop. */
651 } /* Side trace continues across a loop that's left or not entered. */
655 static LoopEvent
rec_itern(jit_State
*J
, BCReg ra
, BCReg rb
)
658 /* YAGNI: Disabled on big-endian due to issues with lj_vm_next,
659 ** IR_HIOP, RID_RETLO/RID_RETHI and ra_destpair.
661 UNUSED(ra
); UNUSED(rb
);
662 setintV(&J
->errinfo
, (int32_t)BC_ITERN
);
663 lj_trace_err_info(J
, LJ_TRERR_NYIBC
);
666 /* Since ITERN is recorded at the start, we need our own loop detection. */
667 if (J
->pc
== J
->startpc
&&
668 J
->framedepth
+ J
->retdepth
== 0 && J
->parent
== 0 && J
->exitno
== 0) {
669 IRRef ref
= REF_FIRST
+ LJ_HASPROFILE
;
670 #ifdef LUAJIT_ENABLE_CHECKHOOK
673 if (J
->cur
.nins
> ref
||
674 (LJ_HASPROFILE
&& J
->cur
.nins
== ref
&& J
->cur
.ir
[ref
-1].o
!= IR_PROF
)) {
675 J
->instunroll
= 0; /* Cannot continue unrolling across an ITERN. */
676 lj_record_stop(J
, LJ_TRLINK_LOOP
, J
->cur
.traceno
); /* Looping trace. */
681 lj_snap_add(J
); /* Required to make JLOOP the first ins in a side-trace. */
682 ix
.tab
= getslot(J
, ra
-2);
683 ix
.key
= J
->base
[ra
-1] ? J
->base
[ra
-1] :
684 sloadt(J
, (int32_t)(ra
-1), IRT_GUARD
|IRT_INT
,
685 IRSLOAD_TYPECHECK
|IRSLOAD_KEYINDEX
);
686 copyTV(J
->L
, &ix
.tabv
, &J
->L
->base
[ra
-2]);
687 copyTV(J
->L
, &ix
.keyv
, &J
->L
->base
[ra
-1]);
688 ix
.idxchain
= (rb
< 3); /* Omit value type check, if unused. */
689 ix
.mobj
= 1; /* We need the next index, too. */
690 J
->maxslot
= ra
+ lj_record_next(J
, &ix
);
692 if (!tref_isnil(ix
.key
)) { /* Looping back? */
693 J
->base
[ra
-1] = ix
.mobj
| TREF_KEYINDEX
; /* Control var has next index. */
694 J
->base
[ra
] = ix
.key
;
695 J
->base
[ra
+1] = ix
.val
;
696 J
->pc
+= bc_j(J
->pc
[1])+2;
707 static void rec_isnext(jit_State
*J
, BCReg ra
)
709 cTValue
*b
= &J
->L
->base
[ra
-3];
710 if (tvisfunc(b
) && funcV(b
)->c
.ffid
== FF_next
&&
711 tvistab(b
+1) && tvisnil(b
+2)) {
712 /* These checks are folded away for a compiled pairs(). */
713 TRef func
= getslot(J
, ra
-3);
714 TRef trid
= emitir(IRT(IR_FLOAD
, IRT_U8
), func
, IRFL_FUNC_FFID
);
715 emitir(IRTGI(IR_EQ
), trid
, lj_ir_kint(J
, FF_next
));
716 (void)getslot(J
, ra
-2); /* Type check for table. */
717 (void)getslot(J
, ra
-1); /* Type check for nil key. */
718 J
->base
[ra
-1] = lj_ir_kint(J
, 0) | TREF_KEYINDEX
;
720 } else { /* Abort trace. Interpreter will despecialize bytecode. */
721 lj_trace_err(J
, LJ_TRERR_RECERR
);
725 /* -- Record profiler hook checks ----------------------------------------- */
729 /* Need to insert profiler hook check? */
730 static int rec_profile_need(jit_State
*J
, GCproto
*pt
, const BCIns
*pc
)
733 lj_assertJ(J
->prof_mode
== 'f' || J
->prof_mode
== 'l',
734 "bad profiler mode %c", J
->prof_mode
);
739 if (pt
!= ppt
&& ppt
) {
743 if (J
->prof_mode
== 'l') {
744 BCLine line
= lj_debug_line(pt
, proto_bcpos(pt
, pc
));
745 BCLine pline
= J
->prev_line
;
753 static void rec_profile_ins(jit_State
*J
, const BCIns
*pc
)
755 if (J
->prof_mode
&& rec_profile_need(J
, J
->pt
, pc
)) {
756 emitir(IRTG(IR_PROF
, IRT_NIL
), 0, 0);
761 static void rec_profile_ret(jit_State
*J
)
763 if (J
->prof_mode
== 'f') {
764 emitir(IRTG(IR_PROF
, IRT_NIL
), 0, 0);
772 /* -- Record calls and returns -------------------------------------------- */
774 /* Specialize to the runtime value of the called function or its prototype. */
775 static TRef
rec_call_specialize(jit_State
*J
, GCfunc
*fn
, TRef tr
)
779 GCproto
*pt
= funcproto(fn
);
780 /* Too many closures created? Probably not a monomorphic function. */
781 if (pt
->flags
>= PROTO_CLC_POLY
) { /* Specialize to prototype instead. */
782 TRef trpt
= emitir(IRT(IR_FLOAD
, IRT_PGC
), tr
, IRFL_FUNC_PC
);
783 emitir(IRTG(IR_EQ
, IRT_PGC
), trpt
, lj_ir_kptr(J
, proto_bc(pt
)));
784 (void)lj_ir_kgc(J
, obj2gco(pt
), IRT_PROTO
); /* Prevent GC of proto. */
788 /* Don't specialize to non-monomorphic builtins. */
789 switch (fn
->c
.ffid
) {
790 case FF_coroutine_wrap_aux
:
791 case FF_string_gmatch_aux
:
792 /* NYI: io_file_iter doesn't have an ffid, yet. */
793 { /* Specialize to the ffid. */
794 TRef trid
= emitir(IRT(IR_FLOAD
, IRT_U8
), tr
, IRFL_FUNC_FFID
);
795 emitir(IRTGI(IR_EQ
), trid
, lj_ir_kint(J
, fn
->c
.ffid
));
799 /* NYI: don't specialize to non-monomorphic C functions. */
803 /* Otherwise specialize to the function (closure) value itself. */
804 kfunc
= lj_ir_kfunc(J
, fn
);
805 emitir(IRTG(IR_EQ
, IRT_FUNC
), tr
, kfunc
);
809 /* Record call setup. */
810 static void rec_call_setup(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
813 TValue
*functv
= &J
->L
->base
[func
];
814 TRef kfunc
, *fbase
= &J
->base
[func
];
816 (void)getslot(J
, func
); /* Ensure func has a reference. */
817 for (i
= 1; i
<= nargs
; i
++)
818 (void)getslot(J
, func
+LJ_FR2
+i
); /* Ensure all args have a reference. */
819 if (!tref_isfunc(fbase
[0])) { /* Resolve __call metamethod. */
821 copyTV(J
->L
, &ix
.tabv
, functv
);
822 if (!lj_record_mm_lookup(J
, &ix
, MM_call
) || !tref_isfunc(ix
.mobj
))
823 lj_trace_err(J
, LJ_TRERR_NOMM
);
824 for (i
= ++nargs
; i
> LJ_FR2
; i
--) /* Shift arguments up. */
825 fbase
[i
+LJ_FR2
] = fbase
[i
+LJ_FR2
-1];
829 fbase
[0] = ix
.mobj
; /* Replace function. */
832 kfunc
= rec_call_specialize(J
, funcV(functv
), fbase
[0]);
835 fbase
[1] = TREF_FRAME
;
837 fbase
[0] = kfunc
| TREF_FRAME
;
839 J
->maxslot
= (BCReg
)nargs
;
843 void lj_record_call(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
845 rec_call_setup(J
, func
, nargs
);
848 J
->base
+= func
+1+LJ_FR2
;
849 J
->baseslot
+= func
+1+LJ_FR2
;
850 if (J
->baseslot
+ J
->maxslot
>= LJ_MAX_JSLOTS
)
851 lj_trace_err(J
, LJ_TRERR_STACKOV
);
854 /* Record tail call. */
855 void lj_record_tailcall(jit_State
*J
, BCReg func
, ptrdiff_t nargs
)
857 rec_call_setup(J
, func
, nargs
);
858 if (frame_isvarg(J
->L
->base
- 1)) {
859 BCReg cbase
= (BCReg
)frame_delta(J
->L
->base
- 1);
860 if (--J
->framedepth
< 0)
861 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
862 J
->baseslot
-= (BCReg
)cbase
;
866 /* Move func + args down. */
867 if (LJ_FR2
&& J
->baseslot
== 2)
868 J
->base
[func
+1] = TREF_FRAME
;
869 memmove(&J
->base
[-1-LJ_FR2
], &J
->base
[func
], sizeof(TRef
)*(J
->maxslot
+1+LJ_FR2
));
870 /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */
871 /* Tailcalls can form a loop, so count towards the loop unroll limit. */
872 if (++J
->tailcalled
> J
->loopunroll
)
873 lj_trace_err(J
, LJ_TRERR_LUNROLL
);
876 /* Check unroll limits for down-recursion. */
877 static int check_downrec_unroll(jit_State
*J
, GCproto
*pt
)
880 for (ptref
= J
->chain
[IR_KGC
]; ptref
; ptref
= IR(ptref
)->prev
)
881 if (ir_kgc(IR(ptref
)) == obj2gco(pt
)) {
884 for (ref
= J
->chain
[IR_RETF
]; ref
; ref
= IR(ref
)->prev
)
885 if (IR(ref
)->op1
== ptref
)
888 if (J
->pc
== J
->startpc
) {
889 if (count
+ J
->tailcalled
> J
->param
[JIT_P_recunroll
])
892 lj_trace_err(J
, LJ_TRERR_DOWNREC
);
899 static TRef
rec_cat(jit_State
*J
, BCReg baseslot
, BCReg topslot
);
902 void lj_record_ret(jit_State
*J
, BCReg rbase
, ptrdiff_t gotresults
)
904 TValue
*frame
= J
->L
->base
- 1;
907 for (i
= 0; i
< gotresults
; i
++)
908 (void)getslot(J
, rbase
+i
); /* Ensure all results have a reference. */
909 while (frame_ispcall(frame
)) { /* Immediately resolve pcall() returns. */
910 BCReg cbase
= (BCReg
)frame_delta(frame
);
911 if (--J
->framedepth
<= 0)
912 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
913 lj_assertJ(J
->baseslot
> 1+LJ_FR2
, "bad baseslot for return");
917 J
->baseslot
-= (BCReg
)cbase
;
919 J
->base
[--rbase
] = TREF_TRUE
; /* Prepend true to results. */
920 frame
= frame_prevd(frame
);
921 J
->needsnap
= 1; /* Stop catching on-trace errors. */
923 /* Return to lower frame via interpreter for unhandled cases. */
924 if (J
->framedepth
== 0 && J
->pt
&& bc_isret(bc_op(*J
->pc
)) &&
925 (!frame_islua(frame
) ||
926 (J
->parent
== 0 && J
->exitno
== 0 &&
927 !bc_isret(bc_op(J
->cur
.startins
))))) {
928 /* NYI: specialize to frame type and return directly, not via RET*. */
929 for (i
= 0; i
< (ptrdiff_t)rbase
; i
++)
930 J
->base
[i
] = 0; /* Purge dead slots. */
931 J
->maxslot
= rbase
+ (BCReg
)gotresults
;
932 lj_record_stop(J
, LJ_TRLINK_RETURN
, 0); /* Return to interpreter. */
935 if (frame_isvarg(frame
)) {
936 BCReg cbase
= (BCReg
)frame_delta(frame
);
937 if (--J
->framedepth
< 0) /* NYI: return of vararg func to lower frame. */
938 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
939 lj_assertJ(J
->baseslot
> 1+LJ_FR2
, "bad baseslot for return");
942 J
->baseslot
-= (BCReg
)cbase
;
944 frame
= frame_prevd(frame
);
946 if (frame_islua(frame
)) { /* Return to Lua frame. */
947 BCIns callins
= *(frame_pc(frame
)-1);
948 ptrdiff_t nresults
= bc_b(callins
) ? (ptrdiff_t)bc_b(callins
)-1 :gotresults
;
949 BCReg cbase
= bc_a(callins
);
950 GCproto
*pt
= funcproto(frame_func(frame
- (cbase
+1+LJ_FR2
)));
951 if ((pt
->flags
& PROTO_NOJIT
))
952 lj_trace_err(J
, LJ_TRERR_CJITOFF
);
953 if (J
->framedepth
== 0 && J
->pt
&& frame
== J
->L
->base
- 1) {
954 if (!J
->cur
.root
&& check_downrec_unroll(J
, pt
)) {
955 J
->maxslot
= (BCReg
)(rbase
+ gotresults
);
957 lj_record_stop(J
, LJ_TRLINK_DOWNREC
, J
->cur
.traceno
); /* Down-rec. */
962 for (i
= 0; i
< nresults
; i
++) /* Adjust results. */
963 J
->base
[i
-1-LJ_FR2
] = i
< gotresults
? J
->base
[rbase
+i
] : TREF_NIL
;
964 J
->maxslot
= cbase
+(BCReg
)nresults
;
965 if (J
->framedepth
> 0) { /* Return to a frame that is part of the trace. */
967 lj_assertJ(J
->baseslot
> cbase
+1+LJ_FR2
, "bad baseslot for return");
968 J
->baseslot
-= cbase
+1+LJ_FR2
;
969 J
->base
-= cbase
+1+LJ_FR2
;
970 } else if (J
->parent
== 0 && J
->exitno
== 0 &&
971 !bc_isret(bc_op(J
->cur
.startins
))) {
972 /* Return to lower frame would leave the loop in a root trace. */
973 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
974 } else if (J
->needsnap
) { /* Tailcalled to ff with side-effects. */
975 lj_trace_err(J
, LJ_TRERR_NYIRETL
); /* No way to insert snapshot here. */
976 } else if (1 + pt
->framesize
>= LJ_MAX_JSLOTS
) {
977 lj_trace_err(J
, LJ_TRERR_STACKOV
);
978 } else { /* Return to lower frame. Guard for the target we return to. */
979 TRef trpt
= lj_ir_kgc(J
, obj2gco(pt
), IRT_PROTO
);
980 TRef trpc
= lj_ir_kptr(J
, (void *)frame_pc(frame
));
981 emitir(IRTG(IR_RETF
, IRT_PGC
), trpt
, trpc
);
984 J
->scev
.idx
= REF_NIL
;
985 lj_assertJ(J
->baseslot
== 1+LJ_FR2
, "bad baseslot for return");
986 /* Shift result slots up and clear the slots of the new frame below. */
987 memmove(J
->base
+ cbase
, J
->base
-1-LJ_FR2
, sizeof(TRef
)*nresults
);
988 memset(J
->base
-1-LJ_FR2
, 0, sizeof(TRef
)*(cbase
+1+LJ_FR2
));
990 } else if (frame_iscont(frame
)) { /* Return to continuation frame. */
991 ASMFunction cont
= frame_contf(frame
);
992 BCReg cbase
= (BCReg
)frame_delta(frame
);
993 if ((J
->framedepth
-= 2) < 0)
994 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
995 J
->baseslot
-= (BCReg
)cbase
;
997 J
->maxslot
= cbase
-(2<<LJ_FR2
);
998 if (cont
== lj_cont_ra
) {
999 /* Copy result to destination slot. */
1000 BCReg dst
= bc_a(*(frame_contpc(frame
)-1));
1001 J
->base
[dst
] = gotresults
? J
->base
[cbase
+rbase
] : TREF_NIL
;
1002 if (dst
>= J
->maxslot
) {
1005 } else if (cont
== lj_cont_nop
) {
1006 /* Nothing to do here. */
1007 } else if (cont
== lj_cont_cat
) {
1008 BCReg bslot
= bc_b(*(frame_contpc(frame
)-1));
1009 TRef tr
= gotresults
? J
->base
[cbase
+rbase
] : TREF_NIL
;
1010 if (bslot
!= J
->maxslot
) { /* Concatenate the remainder. */
1011 /* Simulate lower frame and result. */
1012 TValue
*b
= J
->L
->base
- baseadj
, save
;
1013 /* Can't handle MM_concat + CALLT + fast func side-effects. */
1014 if (J
->postproc
!= LJ_POST_NONE
)
1015 lj_trace_err(J
, LJ_TRERR_NYIRETL
);
1016 J
->base
[J
->maxslot
] = tr
;
1017 copyTV(J
->L
, &save
, b
-(2<<LJ_FR2
));
1019 copyTV(J
->L
, b
-(2<<LJ_FR2
), b
+rbase
);
1021 setnilV(b
-(2<<LJ_FR2
));
1022 J
->L
->base
= b
- cbase
;
1023 tr
= rec_cat(J
, bslot
, cbase
-(2<<LJ_FR2
));
1024 b
= J
->L
->base
+ cbase
; /* Undo. */
1025 J
->L
->base
= b
+ baseadj
;
1026 copyTV(J
->L
, b
-(2<<LJ_FR2
), &save
);
1028 if (tr
>= 0xffffff00) {
1029 lj_err_throw(J
->L
, -(int32_t)tr
); /* Propagate errors. */
1030 } else if (tr
) { /* Store final result. */
1031 BCReg dst
= bc_a(*(frame_contpc(frame
)-1));
1033 if (dst
>= J
->maxslot
) {
1036 } /* Otherwise continue with another __concat call. */
1038 /* Result type already specialized. */
1039 lj_assertJ(cont
== lj_cont_condf
|| cont
== lj_cont_condt
,
1040 "bad continuation type");
1043 lj_trace_err(J
, LJ_TRERR_NYIRETL
); /* NYI: handle return to C frame. */
1045 lj_assertJ(J
->baseslot
>= 1+LJ_FR2
, "bad baseslot for return");
1048 /* -- Metamethod handling ------------------------------------------------- */
1050 /* Prepare to record call to metamethod. */
1051 static BCReg
rec_mm_prep(jit_State
*J
, ASMFunction cont
)
1053 BCReg s
, top
= cont
== lj_cont_cat
? J
->maxslot
: curr_proto(J
->L
)->framesize
;
1055 J
->base
[top
] = lj_ir_k64(J
, IR_KNUM
, u64ptr(contptr(cont
)));
1056 J
->base
[top
+1] = TREF_CONT
;
1058 J
->base
[top
] = lj_ir_kptr(J
, contptr(cont
)) | TREF_CONT
;
1061 for (s
= J
->maxslot
; s
< top
; s
++)
1062 J
->base
[s
] = 0; /* Clear frame gap to avoid resurrecting previous refs. */
1063 return top
+1+LJ_FR2
;
1066 /* Record metamethod lookup. */
1067 int lj_record_mm_lookup(jit_State
*J
, RecordIndex
*ix
, MMS mm
)
1071 if (tref_istab(ix
->tab
)) {
1072 mt
= tabref(tabV(&ix
->tabv
)->metatable
);
1073 mix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_TAB_META
);
1074 } else if (tref_isudata(ix
->tab
)) {
1075 int udtype
= udataV(&ix
->tabv
)->udtype
;
1076 mt
= tabref(udataV(&ix
->tabv
)->metatable
);
1077 mix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_UDATA_META
);
1078 /* The metatables of special userdata objects are treated as immutable. */
1079 if (udtype
!= UDTYPE_USERDATA
) {
1081 if (LJ_HASFFI
&& udtype
== UDTYPE_FFI_CLIB
) {
1082 /* Specialize to the C library namespace object. */
1083 emitir(IRTG(IR_EQ
, IRT_PGC
), ix
->tab
, lj_ir_kptr(J
, udataV(&ix
->tabv
)));
1085 /* Specialize to the type of userdata. */
1086 TRef tr
= emitir(IRT(IR_FLOAD
, IRT_U8
), ix
->tab
, IRFL_UDATA_UDTYPE
);
1087 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, udtype
));
1090 mo
= lj_tab_getstr(mt
, mmname_str(J2G(J
), mm
));
1093 if (!mo
|| tvisnil(mo
))
1094 return 0; /* No metamethod. */
1095 /* Treat metamethod or index table as immutable, too. */
1096 if (!(tvisfunc(mo
) || tvistab(mo
)))
1097 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1098 copyTV(J
->L
, &ix
->mobjv
, mo
);
1099 ix
->mobj
= lj_ir_kgc(J
, gcV(mo
), tvisfunc(mo
) ? IRT_FUNC
: IRT_TAB
);
1100 return 1; /* Got metamethod or index table. */
1103 /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */
1104 mt
= tabref(basemt_obj(J2G(J
), &ix
->tabv
));
1107 return 0; /* No metamethod. */
1109 /* The cdata metatable is treated as immutable. */
1110 if (LJ_HASFFI
&& tref_iscdata(ix
->tab
)) goto immutable_mt
;
1111 ix
->mt
= mix
.tab
= lj_ir_ggfload(J
, IRT_TAB
,
1112 GG_OFS(g
.gcroot
[GCROOT_BASEMT
+itypemap(&ix
->tabv
)]));
1115 ix
->mt
= mt
? mix
.tab
: TREF_NIL
;
1116 emitir(IRTG(mt
? IR_NE
: IR_EQ
, IRT_TAB
), mix
.tab
, lj_ir_knull(J
, IRT_TAB
));
1119 GCstr
*mmstr
= mmname_str(J2G(J
), mm
);
1120 cTValue
*mo
= lj_tab_getstr(mt
, mmstr
);
1121 if (mo
&& !tvisnil(mo
))
1122 copyTV(J
->L
, &ix
->mobjv
, mo
);
1124 settabV(J
->L
, &mix
.tabv
, mt
);
1125 setstrV(J
->L
, &mix
.keyv
, mmstr
);
1126 mix
.key
= lj_ir_kstr(J
, mmstr
);
1129 ix
->mobj
= lj_record_idx(J
, &mix
);
1130 return !tref_isnil(ix
->mobj
); /* 1 if metamethod found, 0 if not. */
1132 return 0; /* No metamethod. */
1135 /* Record call to arithmetic metamethod. */
1136 static TRef
rec_mm_arith(jit_State
*J
, RecordIndex
*ix
, MMS mm
)
1138 /* Set up metamethod call first to save ix->tab and ix->tabv. */
1139 BCReg func
= rec_mm_prep(J
, mm
== MM_concat
? lj_cont_cat
: lj_cont_ra
);
1140 TRef
*base
= J
->base
+ func
;
1141 TValue
*basev
= J
->L
->base
+ func
;
1142 base
[1+LJ_FR2
] = ix
->tab
; base
[2+LJ_FR2
] = ix
->key
;
1143 copyTV(J
->L
, basev
+1+LJ_FR2
, &ix
->tabv
);
1144 copyTV(J
->L
, basev
+2+LJ_FR2
, &ix
->keyv
);
1145 if (!lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
1148 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1149 if (lj_record_mm_lookup(J
, ix
, mm
)) /* Lookup mm on 2nd operand. */
1152 lj_trace_err(J
, LJ_TRERR_NOMM
);
1159 copyTV(J
->L
, basev
+0, &ix
->mobjv
);
1160 lj_record_call(J
, func
, 2);
1161 return 0; /* No result yet. */
1164 /* Record call to __len metamethod. */
1165 static TRef
rec_mm_len(jit_State
*J
, TRef tr
, TValue
*tv
)
1169 copyTV(J
->L
, &ix
.tabv
, tv
);
1170 if (lj_record_mm_lookup(J
, &ix
, MM_len
)) {
1171 BCReg func
= rec_mm_prep(J
, lj_cont_ra
);
1172 TRef
*base
= J
->base
+ func
;
1173 TValue
*basev
= J
->L
->base
+ func
;
1174 base
[0] = ix
.mobj
; copyTV(J
->L
, basev
+0, &ix
.mobjv
);
1177 base
[1] = tr
; copyTV(J
->L
, basev
+1, tv
);
1179 base
[2] = tr
; copyTV(J
->L
, basev
+2, tv
);
1181 base
[2] = TREF_NIL
; setnilV(basev
+2);
1183 lj_record_call(J
, func
, 2);
1185 if (LJ_52
&& tref_istab(tr
))
1186 return emitir(IRTI(IR_ALEN
), tr
, TREF_NIL
);
1187 lj_trace_err(J
, LJ_TRERR_NOMM
);
1189 return 0; /* No result yet. */
1192 /* Call a comparison metamethod. */
1193 static void rec_mm_callcomp(jit_State
*J
, RecordIndex
*ix
, int op
)
1195 BCReg func
= rec_mm_prep(J
, (op
&1) ? lj_cont_condf
: lj_cont_condt
);
1196 TRef
*base
= J
->base
+ func
+ LJ_FR2
;
1197 TValue
*tv
= J
->L
->base
+ func
+ LJ_FR2
;
1198 base
[-LJ_FR2
] = ix
->mobj
; base
[1] = ix
->val
; base
[2] = ix
->key
;
1199 copyTV(J
->L
, tv
-LJ_FR2
, &ix
->mobjv
);
1200 copyTV(J
->L
, tv
+1, &ix
->valv
);
1201 copyTV(J
->L
, tv
+2, &ix
->keyv
);
1202 lj_record_call(J
, func
, 2);
1205 /* Record call to equality comparison metamethod (for tab and udata only). */
1206 static void rec_mm_equal(jit_State
*J
, RecordIndex
*ix
, int op
)
1209 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
1210 if (lj_record_mm_lookup(J
, ix
, MM_eq
)) { /* Lookup mm on 1st operand. */
1212 TRef mo1
= ix
->mobj
;
1214 copyTV(J
->L
, &mo1v
, &ix
->mobjv
);
1215 /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */
1217 if (tvistab(bv
) && tabref(tabV(bv
)->metatable
) == ix
->mtv
) {
1218 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_TAB_META
);
1219 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
1220 } else if (tvisudata(bv
) && tabref(udataV(bv
)->metatable
) == ix
->mtv
) {
1221 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_UDATA_META
);
1222 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
1223 } else { /* Lookup metamethod on 2nd operand and compare both. */
1225 copyTV(J
->L
, &ix
->tabv
, bv
);
1226 if (!lj_record_mm_lookup(J
, ix
, MM_eq
) ||
1227 lj_record_objcmp(J
, mo1
, ix
->mobj
, &mo1v
, &ix
->mobjv
))
1230 rec_mm_callcomp(J
, ix
, op
);
1234 /* Record call to ordered comparison metamethods (for arbitrary objects). */
1235 static void rec_mm_comp(jit_State
*J
, RecordIndex
*ix
, int op
)
1238 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
1240 MMS mm
= (op
& 2) ? MM_le
: MM_lt
; /* Try __le + __lt or only __lt. */
1242 if (!lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
1244 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1245 if (!lj_record_mm_lookup(J
, ix
, mm
)) /* Lookup mm on 2nd operand. */
1248 rec_mm_callcomp(J
, ix
, op
);
1251 if (lj_record_mm_lookup(J
, ix
, mm
)) { /* Lookup mm on 1st operand. */
1253 TRef mo1
= ix
->mobj
;
1255 copyTV(J
->L
, &mo1v
, &ix
->mobjv
);
1256 /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */
1258 if (tvistab(bv
) && tabref(tabV(bv
)->metatable
) == ix
->mtv
) {
1259 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_TAB_META
);
1260 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
1261 } else if (tvisudata(bv
) && tabref(udataV(bv
)->metatable
) == ix
->mtv
) {
1262 TRef mt2
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->key
, IRFL_UDATA_META
);
1263 emitir(IRTG(IR_EQ
, IRT_TAB
), mt2
, ix
->mt
);
1264 } else { /* Lookup metamethod on 2nd operand and compare both. */
1266 copyTV(J
->L
, &ix
->tabv
, bv
);
1267 if (!lj_record_mm_lookup(J
, ix
, mm
) ||
1268 lj_record_objcmp(J
, mo1
, ix
->mobj
, &mo1v
, &ix
->mobjv
))
1271 rec_mm_callcomp(J
, ix
, op
);
1276 /* Lookup failed. Retry with __lt and swapped operands. */
1277 if (!(op
& 2)) break; /* Already at __lt. Interpreter will throw. */
1278 ix
->tab
= ix
->key
; ix
->key
= ix
->val
; ix
->val
= ix
->tab
;
1279 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1280 copyTV(J
->L
, &ix
->keyv
, &ix
->valv
);
1281 copyTV(J
->L
, &ix
->valv
, &ix
->tabv
);
1287 /* Setup call to cdata comparison metamethod. */
1288 static void rec_mm_comp_cdata(jit_State
*J
, RecordIndex
*ix
, int op
, MMS mm
)
1291 if (tref_iscdata(ix
->val
)) {
1293 copyTV(J
->L
, &ix
->tabv
, &ix
->valv
);
1295 lj_assertJ(tref_iscdata(ix
->key
), "cdata expected");
1297 copyTV(J
->L
, &ix
->tabv
, &ix
->keyv
);
1299 lj_record_mm_lookup(J
, ix
, mm
);
1300 rec_mm_callcomp(J
, ix
, op
);
1304 /* -- Indexed access ------------------------------------------------------ */
1306 #ifdef LUAJIT_ENABLE_TABLE_BUMP
1307 /* Bump table allocations in bytecode when they grow during recording. */
1308 static void rec_idx_bump(jit_State
*J
, RecordIndex
*ix
)
1310 RBCHashEntry
*rbc
= &J
->rbchash
[(ix
->tab
& (RBCHASH_SLOTS
-1))];
1311 if (tref_ref(ix
->tab
) == rbc
->ref
) {
1312 const BCIns
*pc
= mref(rbc
->pc
, const BCIns
);
1313 GCtab
*tb
= tabV(&ix
->tabv
);
1316 if (!tvisnil(&ix
->keyv
))
1317 (void)lj_tab_set(J
->L
, tb
, &ix
->keyv
); /* Grow table right now. */
1318 nhbits
= tb
->hmask
> 0 ? lj_fls(tb
->hmask
)+1 : 0;
1319 ir
= IR(tref_ref(ix
->tab
));
1320 if (ir
->o
== IR_TNEW
) {
1321 uint32_t ah
= bc_d(*pc
);
1322 uint32_t asize
= ah
& 0x7ff, hbits
= ah
>> 11;
1323 if (nhbits
> hbits
) hbits
= nhbits
;
1324 if (tb
->asize
> asize
) {
1325 asize
= tb
->asize
<= 0x7ff ? tb
->asize
: 0x7ff;
1327 if ((asize
| (hbits
<<11)) != ah
) { /* Has the size changed? */
1328 /* Patch bytecode, but continue recording (for more patching). */
1329 setbc_d(pc
, (asize
| (hbits
<<11)));
1330 /* Patching TNEW operands is only safe if the trace is aborted. */
1331 ir
->op1
= asize
; ir
->op2
= hbits
;
1332 J
->retryrec
= 1; /* Abort the trace at the end of recording. */
1334 } else if (ir
->o
== IR_TDUP
) {
1335 GCtab
*tpl
= gco2tab(proto_kgc(&gcref(rbc
->pt
)->pt
, ~(ptrdiff_t)bc_d(*pc
)));
1336 /* Grow template table, but preserve keys with nil values. */
1337 if ((tb
->asize
> tpl
->asize
&& (1u << nhbits
)-1 == tpl
->hmask
) ||
1338 (tb
->asize
== tpl
->asize
&& (1u << nhbits
)-1 > tpl
->hmask
)) {
1339 Node
*node
= noderef(tpl
->node
);
1340 uint32_t i
, hmask
= tpl
->hmask
, asize
;
1342 for (i
= 0; i
<= hmask
; i
++) {
1343 if (!tvisnil(&node
[i
].key
) && tvisnil(&node
[i
].val
))
1344 settabV(J
->L
, &node
[i
].val
, tpl
);
1346 if (!tvisnil(&ix
->keyv
) && tref_isk(ix
->key
)) {
1347 TValue
*o
= lj_tab_set(J
->L
, tpl
, &ix
->keyv
);
1348 if (tvisnil(o
)) settabV(J
->L
, o
, tpl
);
1350 lj_tab_resize(J
->L
, tpl
, tb
->asize
, nhbits
);
1351 node
= noderef(tpl
->node
);
1353 for (i
= 0; i
<= hmask
; i
++) {
1354 /* This is safe, since template tables only hold immutable values. */
1355 if (tvistab(&node
[i
].val
))
1356 setnilV(&node
[i
].val
);
1358 /* The shape of the table may have changed. Clean up array part, too. */
1360 array
= tvref(tpl
->array
);
1361 for (i
= 0; i
< asize
; i
++) {
1362 if (tvistab(&array
[i
]))
1365 J
->retryrec
= 1; /* Abort the trace at the end of recording. */
1372 /* Record bounds-check. */
1373 static void rec_idx_abc(jit_State
*J
, TRef asizeref
, TRef ikey
, uint32_t asize
)
1375 /* Try to emit invariant bounds checks. */
1376 if ((J
->flags
& (JIT_F_OPT_LOOP
|JIT_F_OPT_ABC
)) ==
1377 (JIT_F_OPT_LOOP
|JIT_F_OPT_ABC
)) {
1378 IRRef ref
= tref_ref(ikey
);
1379 IRIns
*ir
= IR(ref
);
1382 /* Handle constant offsets. */
1383 if (ir
->o
== IR_ADD
&& irref_isk(ir
->op2
)) {
1385 ofs
= IR(ofsref
)->i
;
1389 /* Got scalar evolution analysis results for this reference? */
1390 if (ref
== J
->scev
.idx
) {
1392 lj_assertJ(irt_isint(J
->scev
.t
) && ir
->o
== IR_SLOAD
,
1393 "only int SCEV supported");
1394 stop
= numberVint(&(J
->L
->base
- J
->baseslot
)[ir
->op1
+ FORL_STOP
]);
1395 /* Runtime value for stop of loop is within bounds? */
1396 if ((uint64_t)stop
+ ofs
< (uint64_t)asize
) {
1397 /* Emit invariant bounds check for stop. */
1398 uint32_t abc
= IRTG(IR_ABC
, tref_isk(asizeref
) ? IRT_U32
: IRT_P32
);
1399 emitir(abc
, asizeref
, ofs
== 0 ? J
->scev
.stop
:
1400 emitir(IRTI(IR_ADD
), J
->scev
.stop
, ofsref
));
1401 /* Emit invariant bounds check for start, if not const or negative. */
1402 if (!(J
->scev
.dir
&& J
->scev
.start
&&
1403 (int64_t)IR(J
->scev
.start
)->i
+ ofs
>= 0))
1404 emitir(abc
, asizeref
, ikey
);
1409 emitir(IRTGI(IR_ABC
), asizeref
, ikey
); /* Emit regular bounds check. */
1412 /* Record indexed key lookup. */
1413 static TRef
rec_idx_key(jit_State
*J
, RecordIndex
*ix
, IRRef
*rbref
,
1417 GCtab
*t
= tabV(&ix
->tabv
);
1418 ix
->oldv
= lj_tab_get(J
->L
, t
, &ix
->keyv
); /* Lookup previous value. */
1422 /* Integer keys are looked up in the array part first. */
1424 if (tref_isnumber(key
)) {
1425 int32_t k
= numberVint(&ix
->keyv
);
1426 if (!tvisint(&ix
->keyv
) && numV(&ix
->keyv
) != (lua_Number
)k
)
1428 if ((MSize
)k
< LJ_MAX_ASIZE
) { /* Potential array key? */
1429 TRef ikey
= lj_opt_narrow_index(J
, key
);
1430 TRef asizeref
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_ASIZE
);
1431 if ((MSize
)k
< t
->asize
) { /* Currently an array key? */
1433 rec_idx_abc(J
, asizeref
, ikey
, t
->asize
);
1434 arrayref
= emitir(IRT(IR_FLOAD
, IRT_PGC
), ix
->tab
, IRFL_TAB_ARRAY
);
1435 return emitir(IRT(IR_AREF
, IRT_PGC
), arrayref
, ikey
);
1436 } else { /* Currently not in array (may be an array extension)? */
1437 emitir(IRTGI(IR_ULE
), asizeref
, ikey
); /* Inv. bounds check. */
1438 if (k
== 0 && tref_isk(key
))
1439 key
= lj_ir_knum_zero(J
); /* Canonicalize 0 or +-0.0 to +0.0. */
1440 /* And continue with the hash lookup. */
1442 } else if (!tref_isk(key
)) {
1443 /* We can rule out const numbers which failed the integerness test
1444 ** above. But all other numbers are potential array keys.
1446 if (t
->asize
== 0) { /* True sparse tables have an empty array part. */
1447 /* Guard that the array part stays empty. */
1448 TRef tmp
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_ASIZE
);
1449 emitir(IRTGI(IR_EQ
), tmp
, lj_ir_kint(J
, 0));
1451 lj_trace_err(J
, LJ_TRERR_NYITMIX
);
1456 /* Otherwise the key is located in the hash part. */
1457 if (t
->hmask
== 0) { /* Shortcut for empty hash part. */
1458 /* Guard that the hash part stays empty. */
1459 TRef tmp
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_HMASK
);
1460 emitir(IRTGI(IR_EQ
), tmp
, lj_ir_kint(J
, 0));
1461 return lj_ir_kkptr(J
, niltvg(J2G(J
)));
1463 if (tref_isinteger(key
)) /* Hash keys are based on numbers, not ints. */
1464 key
= emitir(IRTN(IR_CONV
), key
, IRCONV_NUM_INT
);
1465 if (tref_isk(key
)) {
1466 /* Optimize lookup of constant hash keys. */
1467 GCSize hslot
= (GCSize
)((char *)ix
->oldv
-(char *)&noderef(t
->node
)[0].val
);
1468 if (hslot
<= t
->hmask
*(GCSize
)sizeof(Node
) &&
1469 hslot
<= 65535*(GCSize
)sizeof(Node
)) {
1470 TRef node
, kslot
, hm
;
1471 *rbref
= J
->cur
.nins
; /* Mark possible rollback point. */
1472 *rbguard
= J
->guardemit
;
1473 hm
= emitir(IRTI(IR_FLOAD
), ix
->tab
, IRFL_TAB_HMASK
);
1474 emitir(IRTGI(IR_EQ
), hm
, lj_ir_kint(J
, (int32_t)t
->hmask
));
1475 node
= emitir(IRT(IR_FLOAD
, IRT_PGC
), ix
->tab
, IRFL_TAB_NODE
);
1476 kslot
= lj_ir_kslot(J
, key
, (IRRef
)(hslot
/ sizeof(Node
)));
1477 return emitir(IRTG(IR_HREFK
, IRT_PGC
), node
, kslot
);
1480 /* Fall back to a regular hash lookup. */
1481 return emitir(IRT(IR_HREF
, IRT_PGC
), ix
->tab
, key
);
1484 /* Determine whether a key is NOT one of the fast metamethod names. */
1485 static int nommstr(jit_State
*J
, TRef key
)
1487 if (tref_isstr(key
)) {
1488 if (tref_isk(key
)) {
1489 GCstr
*str
= ir_kstr(IR(tref_ref(key
)));
1491 for (mm
= 0; mm
<= MM_FAST
; mm
++)
1492 if (mmname_str(J2G(J
), mm
) == str
)
1493 return 0; /* MUST be one the fast metamethod names. */
1495 return 0; /* Variable string key MAY be a metamethod name. */
1498 return 1; /* CANNOT be a metamethod name. */
1501 /* Record indexed load/store. */
1502 TRef
lj_record_idx(jit_State
*J
, RecordIndex
*ix
)
1505 IROp xrefop
, loadop
;
1510 while (!tref_istab(ix
->tab
)) { /* Handle non-table lookup. */
1511 /* Never call raw lj_record_idx() on non-table. */
1512 lj_assertJ(ix
->idxchain
!= 0, "bad usage");
1513 if (!lj_record_mm_lookup(J
, ix
, ix
->val
? MM_newindex
: MM_index
))
1514 lj_trace_err(J
, LJ_TRERR_NOMM
);
1516 if (tref_isfunc(ix
->mobj
)) { /* Handle metamethod call. */
1517 BCReg func
= rec_mm_prep(J
, ix
->val
? lj_cont_nop
: lj_cont_ra
);
1518 TRef
*base
= J
->base
+ func
+ LJ_FR2
;
1519 TValue
*tv
= J
->L
->base
+ func
+ LJ_FR2
;
1520 base
[-LJ_FR2
] = ix
->mobj
; base
[1] = ix
->tab
; base
[2] = ix
->key
;
1521 setfuncV(J
->L
, tv
-LJ_FR2
, funcV(&ix
->mobjv
));
1522 copyTV(J
->L
, tv
+1, &ix
->tabv
);
1523 copyTV(J
->L
, tv
+2, &ix
->keyv
);
1526 copyTV(J
->L
, tv
+3, &ix
->valv
);
1527 lj_record_call(J
, func
, 3); /* mobj(tab, key, val) */
1530 lj_record_call(J
, func
, 2); /* res = mobj(tab, key) */
1531 return 0; /* No result yet. */
1535 /* The index table of buffer objects is treated as immutable. */
1536 if (ix
->mt
== TREF_NIL
&& !ix
->val
&&
1537 tref_isudata(ix
->tab
) && udataV(&ix
->tabv
)->udtype
== UDTYPE_BUFFER
&&
1538 tref_istab(ix
->mobj
) && tref_isstr(ix
->key
) && tref_isk(ix
->key
)) {
1539 cTValue
*val
= lj_tab_getstr(tabV(&ix
->mobjv
), strV(&ix
->keyv
));
1540 TRef tr
= lj_record_constify(J
, val
);
1541 if (tr
) return tr
; /* Specialize to the value, i.e. a method. */
1544 /* Otherwise retry lookup with metaobject. */
1546 copyTV(J
->L
, &ix
->tabv
, &ix
->mobjv
);
1547 if (--ix
->idxchain
== 0)
1548 lj_trace_err(J
, LJ_TRERR_IDXLOOP
);
1551 /* First catch nil and NaN keys for tables. */
1552 if (tvisnil(&ix
->keyv
) || (tvisnum(&ix
->keyv
) && tvisnan(&ix
->keyv
))) {
1553 if (ix
->val
) /* Better fail early. */
1554 lj_trace_err(J
, LJ_TRERR_STORENN
);
1555 if (tref_isk(ix
->key
)) {
1556 if (ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_index
))
1562 /* Record the key lookup. */
1563 xref
= rec_idx_key(J
, ix
, &rbref
, &rbguard
);
1564 xrefop
= IR(tref_ref(xref
))->o
;
1565 loadop
= xrefop
== IR_AREF
? IR_ALOAD
: IR_HLOAD
;
1566 /* The lj_meta_tset() inconsistency is gone, but better play safe. */
1567 oldv
= xrefop
== IR_KKPTR
? (cTValue
*)ir_kptr(IR(tref_ref(xref
))) : ix
->oldv
;
1569 if (ix
->val
== 0) { /* Indexed load */
1570 IRType t
= itype2irt(oldv
);
1572 if (oldv
== niltvg(J2G(J
))) {
1573 emitir(IRTG(IR_EQ
, IRT_PGC
), xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1576 res
= emitir(IRTG(loadop
, t
), xref
, 0);
1578 if (tref_ref(res
) < rbref
) { /* HREFK + load forwarded? */
1579 lj_ir_rollback(J
, rbref
); /* Rollback to eliminate hmask guard. */
1580 J
->guardemit
= rbguard
;
1582 if (t
== IRT_NIL
&& ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_index
))
1584 if (irtype_ispri(t
)) res
= TREF_PRI(t
); /* Canonicalize primitives. */
1586 } else { /* Indexed store. */
1587 GCtab
*mt
= tabref(tabV(&ix
->tabv
)->metatable
);
1588 int keybarrier
= tref_isgcv(ix
->key
) && !tref_isnil(ix
->val
);
1589 if (tref_ref(xref
) < rbref
) { /* HREFK forwarded? */
1590 lj_ir_rollback(J
, rbref
); /* Rollback to eliminate hmask guard. */
1591 J
->guardemit
= rbguard
;
1593 if (tvisnil(oldv
)) { /* Previous value was nil? */
1594 /* Need to duplicate the hasmm check for the early guards. */
1596 if (ix
->idxchain
&& mt
) {
1597 cTValue
*mo
= lj_tab_getstr(mt
, mmname_str(J2G(J
), MM_newindex
));
1598 hasmm
= mo
&& !tvisnil(mo
);
1601 emitir(IRTG(loadop
, IRT_NIL
), xref
, 0); /* Guard for nil value. */
1602 else if (xrefop
== IR_HREF
)
1603 emitir(IRTG(oldv
== niltvg(J2G(J
)) ? IR_EQ
: IR_NE
, IRT_PGC
),
1604 xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1605 if (ix
->idxchain
&& lj_record_mm_lookup(J
, ix
, MM_newindex
)) {
1606 lj_assertJ(hasmm
, "inconsistent metamethod handling");
1609 lj_assertJ(!hasmm
, "inconsistent metamethod handling");
1610 if (oldv
== niltvg(J2G(J
))) { /* Need to insert a new key. */
1612 if (tref_isinteger(key
)) { /* NEWREF needs a TValue as a key. */
1613 key
= emitir(IRTN(IR_CONV
), key
, IRCONV_NUM_INT
);
1614 } else if (tref_isnum(key
)) {
1615 if (tref_isk(key
)) {
1616 if (tvismzero(&ix
->keyv
))
1617 key
= lj_ir_knum_zero(J
); /* Canonicalize -0.0 to +0.0. */
1619 emitir(IRTG(IR_EQ
, IRT_NUM
), key
, key
); /* Check for !NaN. */
1622 xref
= emitir(IRT(IR_NEWREF
, IRT_PGC
), ix
->tab
, key
);
1623 keybarrier
= 0; /* NEWREF already takes care of the key barrier. */
1624 #ifdef LUAJIT_ENABLE_TABLE_BUMP
1625 if ((J
->flags
& JIT_F_OPT_SINK
)) /* Avoid a separate flag. */
1626 rec_idx_bump(J
, ix
);
1629 } else if (!lj_opt_fwd_wasnonnil(J
, loadop
, tref_ref(xref
))) {
1630 /* Cannot derive that the previous value was non-nil, must do checks. */
1631 if (xrefop
== IR_HREF
) /* Guard against store to niltv. */
1632 emitir(IRTG(IR_NE
, IRT_PGC
), xref
, lj_ir_kkptr(J
, niltvg(J2G(J
))));
1633 if (ix
->idxchain
) { /* Metamethod lookup required? */
1634 /* A check for NULL metatable is cheaper (hoistable) than a load. */
1636 TRef mtref
= emitir(IRT(IR_FLOAD
, IRT_TAB
), ix
->tab
, IRFL_TAB_META
);
1637 emitir(IRTG(IR_EQ
, IRT_TAB
), mtref
, lj_ir_knull(J
, IRT_TAB
));
1639 IRType t
= itype2irt(oldv
);
1640 emitir(IRTG(loadop
, t
), xref
, 0); /* Guard for non-nil value. */
1644 keybarrier
= 0; /* Previous non-nil value kept the key alive. */
1646 /* Convert int to number before storing. */
1647 if (!LJ_DUALNUM
&& tref_isinteger(ix
->val
))
1648 ix
->val
= emitir(IRTN(IR_CONV
), ix
->val
, IRCONV_NUM_INT
);
1649 emitir(IRT(loadop
+IRDELTA_L2S
, tref_type(ix
->val
)), xref
, ix
->val
);
1650 if (keybarrier
|| tref_isgcv(ix
->val
))
1651 emitir(IRT(IR_TBAR
, IRT_NIL
), ix
->tab
, 0);
1652 /* Invalidate neg. metamethod cache for stores with certain string keys. */
1653 if (!nommstr(J
, ix
->key
)) {
1654 TRef fref
= emitir(IRT(IR_FREF
, IRT_PGC
), ix
->tab
, IRFL_TAB_NOMM
);
1655 emitir(IRT(IR_FSTORE
, IRT_U8
), fref
, lj_ir_kint(J
, 0));
1662 /* Determine result type of table traversal. */
1663 static IRType
rec_next_types(GCtab
*t
, uint32_t idx
)
1665 for (; idx
< t
->asize
; idx
++) {
1666 cTValue
*a
= arrayslot(t
, idx
);
1667 if (LJ_LIKELY(!tvisnil(a
)))
1668 return (LJ_DUALNUM
? IRT_INT
: IRT_NUM
) + (itype2irt(a
) << 8);
1671 for (; idx
<= t
->hmask
; idx
++) {
1672 Node
*n
= &noderef(t
->node
)[idx
];
1673 if (!tvisnil(&n
->val
))
1674 return itype2irt(&n
->key
) + (itype2irt(&n
->val
) << 8);
1676 return IRT_NIL
+ (IRT_NIL
<< 8);
1679 /* Record a table traversal step aka next(). */
1680 int lj_record_next(jit_State
*J
, RecordIndex
*ix
)
1682 IRType t
, tkey
, tval
;
1684 t
= rec_next_types(tabV(&ix
->tabv
), ix
->keyv
.u32
.lo
);
1685 tkey
= (t
& 0xff); tval
= (t
>> 8);
1686 trvk
= lj_ir_call(J
, IRCALL_lj_vm_next
, ix
->tab
, ix
->key
);
1687 if (ix
->mobj
|| tkey
== IRT_NIL
) {
1688 TRef idx
= emitir(IRTI(IR_HIOP
), trvk
, trvk
);
1689 /* Always check for invalid key from next() for nil result. */
1690 if (!ix
->mobj
) emitir(IRTGI(IR_NE
), idx
, lj_ir_kint(J
, -1));
1693 ix
->key
= lj_record_vload(J
, trvk
, 1, tkey
);
1694 if (tkey
== IRT_NIL
|| ix
->idxchain
) { /* Omit value type check. */
1697 } else { /* Need value. */
1698 ix
->val
= lj_record_vload(J
, trvk
, 0, tval
);
1703 static void rec_tsetm(jit_State
*J
, BCReg ra
, BCReg rn
, int32_t i
)
1706 cTValue
*basev
= J
->L
->base
;
1707 GCtab
*t
= tabV(&basev
[ra
-1]);
1708 settabV(J
->L
, &ix
.tabv
, t
);
1709 ix
.tab
= getslot(J
, ra
-1);
1711 #ifdef LUAJIT_ENABLE_TABLE_BUMP
1712 if ((J
->flags
& JIT_F_OPT_SINK
)) {
1713 if (t
->asize
< i
+rn
-ra
)
1714 lj_tab_reasize(J
->L
, t
, i
+rn
-ra
);
1716 rec_idx_bump(J
, &ix
);
1719 for (; ra
< rn
; i
++, ra
++) {
1720 setintV(&ix
.keyv
, i
);
1721 ix
.key
= lj_ir_kint(J
, i
);
1722 copyTV(J
->L
, &ix
.valv
, &basev
[ra
]);
1723 ix
.val
= getslot(J
, ra
);
1724 lj_record_idx(J
, &ix
);
1728 /* -- Upvalue access ------------------------------------------------------ */
1730 /* Check whether upvalue is immutable and ok to constify. */
1731 static int rec_upvalue_constify(jit_State
*J
, GCupval
*uvp
)
1733 if (uvp
->immutable
) {
1734 cTValue
*o
= uvval(uvp
);
1735 /* Don't constify objects that may retain large amounts of memory. */
1738 GCcdata
*cd
= cdataV(o
);
1739 if (!cdataisv(cd
) && !(cd
->marked
& LJ_GC_CDATA_FIN
)) {
1740 CType
*ct
= ctype_raw(ctype_ctsG(J2G(J
)), cd
->ctypeid
);
1741 if (!ctype_hassize(ct
->info
) || ct
->size
<= 16)
1749 if (!(tvistab(o
) || tvisudata(o
) || tvisthread(o
)))
1755 /* Record upvalue load/store. */
1756 static TRef
rec_upvalue(jit_State
*J
, uint32_t uv
, TRef val
)
1758 GCupval
*uvp
= &gcref(J
->fn
->l
.uvptr
[uv
])->uv
;
1759 TRef fn
= getcurrf(J
);
1761 int needbarrier
= 0;
1762 if (rec_upvalue_constify(J
, uvp
)) { /* Try to constify immutable upvalue. */
1764 lj_assertJ(val
== 0, "bad usage");
1765 if (!tref_isk(fn
)) { /* Late specialization of current function. */
1766 if (J
->pt
->flags
>= PROTO_CLC_POLY
)
1768 kfunc
= lj_ir_kfunc(J
, J
->fn
);
1769 emitir(IRTG(IR_EQ
, IRT_FUNC
), fn
, kfunc
);
1771 J
->base
[-2] = kfunc
;
1773 J
->base
[-1] = kfunc
| TREF_FRAME
;
1777 tr
= lj_record_constify(J
, uvval(uvp
));
1782 /* Note: this effectively limits LJ_MAX_UPVAL to 127. */
1783 uv
= (uv
<< 8) | (hashrot(uvp
->dhash
, uvp
->dhash
+ HASH_BIAS
) & 0xff);
1785 /* In current stack? */
1786 if (uvval(uvp
) >= tvref(J
->L
->stack
) &&
1787 uvval(uvp
) < tvref(J
->L
->maxstack
)) {
1788 int32_t slot
= (int32_t)(uvval(uvp
) - (J
->L
->base
- J
->baseslot
));
1789 if (slot
>= 0) { /* Aliases an SSA slot? */
1790 uref
= tref_ref(emitir(IRT(IR_UREFO
, IRT_PGC
), fn
, uv
));
1791 emitir(IRTG(IR_EQ
, IRT_PGC
),
1793 emitir(IRT(IR_ADD
, IRT_PGC
), uref
,
1794 lj_ir_kintpgc(J
, (slot
- 1 - LJ_FR2
) * -8)));
1795 slot
-= (int32_t)J
->baseslot
; /* Note: slot number may be negative! */
1797 return getslot(J
, slot
);
1799 J
->base
[slot
] = val
;
1800 if (slot
>= (int32_t)J
->maxslot
) J
->maxslot
= (BCReg
)(slot
+1);
1805 /* IR_UREFO+IRT_IGC is not checked for open-ness at runtime.
1806 ** Always marked as a guard, since it might get promoted to IRT_PGC later.
1808 uref
= emitir(IRTG(IR_UREFO
, tref_isgcv(val
) ? IRT_PGC
: IRT_IGC
), fn
, uv
);
1809 uref
= tref_ref(uref
);
1810 emitir(IRTG(IR_UGT
, IRT_PGC
),
1811 emitir(IRT(IR_SUB
, IRT_PGC
), uref
, REF_BASE
),
1812 lj_ir_kintpgc(J
, (J
->baseslot
+ J
->maxslot
) * 8));
1814 /* If fn is constant, then so is the GCupval*, and the upvalue cannot
1815 ** transition back to open, so no guard is required in this case.
1817 IRType t
= (tref_isk(fn
) ? 0 : IRT_GUARD
) | IRT_PGC
;
1818 uref
= tref_ref(emitir(IRT(IR_UREFC
, t
), fn
, uv
));
1821 if (val
== 0) { /* Upvalue load */
1822 IRType t
= itype2irt(uvval(uvp
));
1823 TRef res
= emitir(IRTG(IR_ULOAD
, t
), uref
, 0);
1824 if (irtype_ispri(t
)) res
= TREF_PRI(t
); /* Canonicalize primitive refs. */
1826 } else { /* Upvalue store. */
1827 /* Convert int to number before storing. */
1828 if (!LJ_DUALNUM
&& tref_isinteger(val
))
1829 val
= emitir(IRTN(IR_CONV
), val
, IRCONV_NUM_INT
);
1830 emitir(IRT(IR_USTORE
, tref_type(val
)), uref
, val
);
1831 if (needbarrier
&& tref_isgcv(val
))
1832 emitir(IRT(IR_OBAR
, IRT_NIL
), uref
, val
);
1838 /* -- Record calls to Lua functions --------------------------------------- */
1840 /* Check unroll limits for calls. */
1841 static void check_call_unroll(jit_State
*J
, TraceNo lnk
)
1843 cTValue
*frame
= J
->L
->base
- 1;
1844 void *pc
= mref(frame_func(frame
)->l
.pc
, void);
1845 int32_t depth
= J
->framedepth
;
1847 if ((J
->pt
->flags
& PROTO_VARARG
)) depth
--; /* Vararg frame still missing. */
1848 for (; depth
> 0; depth
--) { /* Count frames with same prototype. */
1849 if (frame_iscont(frame
)) depth
--;
1850 frame
= frame_prev(frame
);
1851 if (mref(frame_func(frame
)->l
.pc
, void) == pc
)
1854 if (J
->pc
== J
->startpc
) {
1855 if (count
+ J
->tailcalled
> J
->param
[JIT_P_recunroll
]) {
1857 if (J
->framedepth
+ J
->retdepth
== 0)
1858 lj_record_stop(J
, LJ_TRLINK_TAILREC
, J
->cur
.traceno
); /* Tail-rec. */
1860 lj_record_stop(J
, LJ_TRLINK_UPREC
, J
->cur
.traceno
); /* Up-recursion. */
1863 if (count
> J
->param
[JIT_P_callunroll
]) {
1864 if (lnk
) { /* Possible tail- or up-recursion. */
1865 lj_trace_flush(J
, lnk
); /* Flush trace that only returns. */
1866 /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */
1867 hotcount_set(J2GG(J
), J
->pc
+1, lj_prng_u64(&J2G(J
)->prng
) & 15u);
1869 lj_trace_err(J
, LJ_TRERR_CUNROLL
);
1874 /* Record Lua function setup. */
1875 static void rec_func_setup(jit_State
*J
)
1877 GCproto
*pt
= J
->pt
;
1878 BCReg s
, numparams
= pt
->numparams
;
1879 if ((pt
->flags
& PROTO_NOJIT
))
1880 lj_trace_err(J
, LJ_TRERR_CJITOFF
);
1881 if (J
->baseslot
+ pt
->framesize
>= LJ_MAX_JSLOTS
)
1882 lj_trace_err(J
, LJ_TRERR_STACKOV
);
1883 /* Fill up missing parameters with nil. */
1884 for (s
= J
->maxslot
; s
< numparams
; s
++)
1885 J
->base
[s
] = TREF_NIL
;
1886 /* The remaining slots should never be read before they are written. */
1887 J
->maxslot
= numparams
;
1890 /* Record Lua vararg function setup. */
1891 static void rec_func_vararg(jit_State
*J
)
1893 GCproto
*pt
= J
->pt
;
1894 BCReg s
, fixargs
, vframe
= J
->maxslot
+1+LJ_FR2
;
1895 lj_assertJ((pt
->flags
& PROTO_VARARG
), "FUNCV in non-vararg function");
1896 if (J
->baseslot
+ vframe
+ pt
->framesize
>= LJ_MAX_JSLOTS
)
1897 lj_trace_err(J
, LJ_TRERR_STACKOV
);
1898 J
->base
[vframe
-1-LJ_FR2
] = J
->base
[-1-LJ_FR2
]; /* Copy function up. */
1900 J
->base
[vframe
-1] = TREF_FRAME
;
1902 /* Copy fixarg slots up and set their original slots to nil. */
1903 fixargs
= pt
->numparams
< J
->maxslot
? pt
->numparams
: J
->maxslot
;
1904 for (s
= 0; s
< fixargs
; s
++) {
1905 J
->base
[vframe
+s
] = J
->base
[s
];
1906 J
->base
[s
] = TREF_NIL
;
1908 J
->maxslot
= fixargs
;
1911 J
->baseslot
+= vframe
;
1914 /* Record entry to a Lua function. */
1915 static void rec_func_lua(jit_State
*J
)
1918 check_call_unroll(J
, 0);
1921 /* Record entry to an already compiled function. */
1922 static void rec_func_jit(jit_State
*J
, TraceNo lnk
)
1926 T
= traceref(J
, lnk
);
1927 if (T
->linktype
== LJ_TRLINK_RETURN
) { /* Trace returns to interpreter? */
1928 check_call_unroll(J
, lnk
);
1929 /* Temporarily unpatch JFUNC* to continue recording across function. */
1930 J
->patchins
= *J
->pc
;
1931 J
->patchpc
= (BCIns
*)J
->pc
;
1932 *J
->patchpc
= T
->startins
;
1935 J
->instunroll
= 0; /* Cannot continue across a compiled function. */
1936 if (J
->pc
== J
->startpc
&& J
->framedepth
+ J
->retdepth
== 0)
1937 lj_record_stop(J
, LJ_TRLINK_TAILREC
, J
->cur
.traceno
); /* Extra tail-rec. */
1939 lj_record_stop(J
, LJ_TRLINK_ROOT
, lnk
); /* Link to the function. */
1942 /* -- Vararg handling ----------------------------------------------------- */
1944 /* Detect y = select(x, ...) idiom. */
1945 static int select_detect(jit_State
*J
)
1947 BCIns ins
= J
->pc
[1];
1948 if (bc_op(ins
) == BC_CALLM
&& bc_b(ins
) == 2 && bc_c(ins
) == 1) {
1949 cTValue
*func
= &J
->L
->base
[bc_a(ins
)];
1950 if (tvisfunc(func
) && funcV(func
)->c
.ffid
== FF_select
) {
1951 TRef kfunc
= lj_ir_kfunc(J
, funcV(func
));
1952 emitir(IRTG(IR_EQ
, IRT_FUNC
), getslot(J
, bc_a(ins
)), kfunc
);
1959 /* Record vararg instruction. */
1960 static void rec_varg(jit_State
*J
, BCReg dst
, ptrdiff_t nresults
)
1962 int32_t numparams
= J
->pt
->numparams
;
1963 ptrdiff_t nvararg
= frame_delta(J
->L
->base
-1) - numparams
- 1 - LJ_FR2
;
1964 lj_assertJ(frame_isvarg(J
->L
->base
-1), "VARG in non-vararg frame");
1965 if (LJ_FR2
&& dst
> J
->maxslot
)
1966 J
->base
[dst
-1] = 0; /* Prevent resurrection of unrelated slot. */
1967 if (J
->framedepth
> 0) { /* Simple case: varargs defined on-trace. */
1969 if (nvararg
< 0) nvararg
= 0;
1970 if (nresults
!= 1) {
1971 if (nresults
== -1) nresults
= nvararg
;
1972 J
->maxslot
= dst
+ (BCReg
)nresults
;
1973 } else if (dst
>= J
->maxslot
) {
1974 J
->maxslot
= dst
+ 1;
1976 if (J
->baseslot
+ J
->maxslot
>= LJ_MAX_JSLOTS
)
1977 lj_trace_err(J
, LJ_TRERR_STACKOV
);
1978 for (i
= 0; i
< nresults
; i
++)
1979 J
->base
[dst
+i
] = i
< nvararg
? getslot(J
, i
- nvararg
- 1 - LJ_FR2
) : TREF_NIL
;
1980 } else { /* Unknown number of varargs passed to trace. */
1981 TRef fr
= emitir(IRTI(IR_SLOAD
), LJ_FR2
, IRSLOAD_READONLY
|IRSLOAD_FRAME
);
1982 int32_t frofs
= 8*(1+LJ_FR2
+numparams
)+FRAME_VARG
;
1983 if (nresults
>= 0) { /* Known fixed number of results. */
1986 ptrdiff_t nload
= nvararg
>= nresults
? nresults
: nvararg
;
1988 if (nvararg
>= nresults
)
1989 emitir(IRTGI(IR_GE
), fr
, lj_ir_kint(J
, frofs
+8*(int32_t)nresults
));
1991 emitir(IRTGI(IR_EQ
), fr
,
1992 lj_ir_kint(J
, (int32_t)frame_ftsz(J
->L
->base
-1)));
1993 vbase
= emitir(IRT(IR_SUB
, IRT_IGC
), REF_BASE
, fr
);
1994 vbase
= emitir(IRT(IR_ADD
, IRT_PGC
), vbase
,
1995 lj_ir_kintpgc(J
, frofs
-8*(1+LJ_FR2
)));
1996 for (i
= 0; i
< nload
; i
++) {
1997 IRType t
= itype2irt(&J
->L
->base
[i
-1-LJ_FR2
-nvararg
]);
1998 J
->base
[dst
+i
] = lj_record_vload(J
, vbase
, (MSize
)i
, t
);
2001 emitir(IRTGI(IR_LE
), fr
, lj_ir_kint(J
, frofs
));
2004 for (i
= nvararg
; i
< nresults
; i
++)
2005 J
->base
[dst
+i
] = TREF_NIL
;
2006 if (nresults
!= 1 || dst
>= J
->maxslot
) {
2007 J
->maxslot
= dst
+ (BCReg
)nresults
;
2009 } else if (select_detect(J
)) { /* y = select(x, ...) */
2010 TRef tridx
= getslot(J
, dst
-1);
2012 ptrdiff_t idx
= lj_ffrecord_select_mode(J
, tridx
, &J
->L
->base
[dst
-1]);
2013 if (idx
< 0) goto nyivarg
;
2014 if (idx
!= 0 && !tref_isinteger(tridx
)) {
2015 if (tref_isstr(tridx
))
2016 tridx
= emitir(IRTG(IR_STRTO
, IRT_NUM
), tridx
, 0);
2017 tridx
= emitir(IRTGI(IR_CONV
), tridx
, IRCONV_INT_NUM
|IRCONV_INDEX
);
2019 if (idx
!= 0 && tref_isk(tridx
)) {
2020 emitir(IRTGI(idx
<= nvararg
? IR_GE
: IR_LT
),
2021 fr
, lj_ir_kint(J
, frofs
+8*(int32_t)idx
));
2022 frofs
-= 8; /* Bias for 1-based index. */
2023 } else if (idx
<= nvararg
) { /* Compute size. */
2024 TRef tmp
= emitir(IRTI(IR_ADD
), fr
, lj_ir_kint(J
, -frofs
));
2026 emitir(IRTGI(IR_GE
), tmp
, lj_ir_kint(J
, 0));
2027 tr
= emitir(IRTI(IR_BSHR
), tmp
, lj_ir_kint(J
, 3));
2029 tridx
= emitir(IRTI(IR_ADD
), tridx
, lj_ir_kint(J
, -1));
2030 rec_idx_abc(J
, tr
, tridx
, (uint32_t)nvararg
);
2033 TRef tmp
= lj_ir_kint(J
, frofs
);
2035 TRef tmp2
= emitir(IRTI(IR_BSHL
), tridx
, lj_ir_kint(J
, 3));
2036 tmp
= emitir(IRTI(IR_ADD
), tmp2
, tmp
);
2038 tr
= lj_ir_kint(J
, 0);
2040 emitir(IRTGI(IR_LT
), fr
, tmp
);
2042 if (idx
!= 0 && idx
<= nvararg
) {
2044 TRef aref
, vbase
= emitir(IRT(IR_SUB
, IRT_IGC
), REF_BASE
, fr
);
2045 vbase
= emitir(IRT(IR_ADD
, IRT_PGC
), vbase
,
2046 lj_ir_kintpgc(J
, frofs
-(8<<LJ_FR2
)));
2047 t
= itype2irt(&J
->L
->base
[idx
-2-LJ_FR2
-nvararg
]);
2048 aref
= emitir(IRT(IR_AREF
, IRT_PGC
), vbase
, tridx
);
2049 tr
= lj_record_vload(J
, aref
, 0, t
);
2051 J
->base
[dst
-2-LJ_FR2
] = tr
;
2052 J
->maxslot
= dst
-1-LJ_FR2
;
2053 J
->bcskip
= 2; /* Skip CALLM + select. */
2056 setintV(&J
->errinfo
, BC_VARG
);
2057 lj_trace_err_info(J
, LJ_TRERR_NYIBC
);
2062 /* -- Record allocations -------------------------------------------------- */
2064 static TRef
rec_tnew(jit_State
*J
, uint32_t ah
)
2066 uint32_t asize
= ah
& 0x7ff;
2067 uint32_t hbits
= ah
>> 11;
2069 if (asize
== 0x7ff) asize
= 0x801;
2070 tr
= emitir(IRTG(IR_TNEW
, IRT_TAB
), asize
, hbits
);
2071 #ifdef LUAJIT_ENABLE_TABLE_BUMP
2072 J
->rbchash
[(tr
& (RBCHASH_SLOTS
-1))].ref
= tref_ref(tr
);
2073 setmref(J
->rbchash
[(tr
& (RBCHASH_SLOTS
-1))].pc
, J
->pc
);
2074 setgcref(J
->rbchash
[(tr
& (RBCHASH_SLOTS
-1))].pt
, obj2gco(J
->pt
));
2079 /* -- Concatenation ------------------------------------------------------- */
2081 typedef struct RecCatDataCP
{
2083 BCReg baseslot
, topslot
;
2087 static TValue
*rec_mm_concat_cp(lua_State
*L
, lua_CFunction dummy
, void *ud
)
2089 RecCatDataCP
*rcd
= (RecCatDataCP
*)ud
;
2090 jit_State
*J
= rcd
->J
;
2091 BCReg baseslot
= rcd
->baseslot
, topslot
= rcd
->topslot
;
2092 TRef
*top
= &J
->base
[topslot
];
2095 UNUSED(L
); UNUSED(dummy
);
2096 lj_assertJ(baseslot
< topslot
, "bad CAT arg");
2097 for (s
= baseslot
; s
<= topslot
; s
++)
2098 (void)getslot(J
, s
); /* Ensure all arguments have a reference. */
2099 if (tref_isnumber_str(top
[0]) && tref_isnumber_str(top
[-1])) {
2100 TRef tr
, hdr
, *trp
, *xbase
, *base
= &J
->base
[baseslot
];
2101 /* First convert numbers to strings. */
2102 for (trp
= top
; trp
>= base
; trp
--) {
2103 if (tref_isnumber(*trp
))
2104 *trp
= emitir(IRT(IR_TOSTR
, IRT_STR
), *trp
,
2105 tref_isnum(*trp
) ? IRTOSTR_NUM
: IRTOSTR_INT
);
2106 else if (!tref_isstr(*trp
))
2110 tr
= hdr
= emitir(IRT(IR_BUFHDR
, IRT_PGC
),
2111 lj_ir_kptr(J
, &J2G(J
)->tmpbuf
), IRBUFHDR_RESET
);
2113 tr
= emitir(IRTG(IR_BUFPUT
, IRT_PGC
), tr
, *trp
++);
2114 } while (trp
<= top
);
2115 tr
= emitir(IRTG(IR_BUFSTR
, IRT_STR
), tr
, hdr
);
2116 J
->maxslot
= (BCReg
)(xbase
- J
->base
);
2117 if (xbase
== base
) {
2118 rcd
->tr
= tr
; /* Return simple concatenation result. */
2121 /* Pass partial result. */
2122 topslot
= J
->maxslot
--;
2125 setstrV(J
->L
, &ix
.keyv
, &J2G(J
)->strempty
); /* Simulate string result. */
2127 J
->maxslot
= topslot
-1;
2128 copyTV(J
->L
, &ix
.keyv
, &J
->L
->base
[topslot
]);
2130 copyTV(J
->L
, &ix
.tabv
, &J
->L
->base
[topslot
-1]);
2133 rec_mm_arith(J
, &ix
, MM_concat
); /* Call __concat metamethod. */
2134 rcd
->tr
= 0; /* No result yet. */
2138 static TRef
rec_cat(jit_State
*J
, BCReg baseslot
, BCReg topslot
)
2140 lua_State
*L
= J
->L
;
2141 ptrdiff_t delta
= L
->top
- L
->base
;
2142 TValue savetv
[5+LJ_FR2
], errobj
;
2146 rcd
.baseslot
= baseslot
;
2147 rcd
.topslot
= topslot
;
2148 memcpy(savetv
, &L
->base
[topslot
-1], sizeof(savetv
)); /* Save slots. */
2149 errcode
= lj_vm_cpcall(L
, NULL
, &rcd
, rec_mm_concat_cp
);
2150 if (errcode
) copyTV(L
, &errobj
, L
->top
-1);
2151 memcpy(&L
->base
[topslot
-1], savetv
, sizeof(savetv
)); /* Restore slots. */
2153 L
->top
= L
->base
+ delta
;
2154 copyTV(L
, L
->top
++, &errobj
);
2155 return (TRef
)(-errcode
);
2160 /* -- Record bytecode ops ------------------------------------------------- */
2162 /* Prepare for comparison. */
2163 static void rec_comp_prep(jit_State
*J
)
2165 /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */
2166 if (J
->cur
.nsnap
== 1 && J
->cur
.snap
[0].ref
== J
->cur
.nins
)
2167 emitir_raw(IRT(IR_NOP
, IRT_NIL
), 0, 0);
2171 /* Fixup comparison. */
2172 static void rec_comp_fixup(jit_State
*J
, const BCIns
*pc
, int cond
)
2174 BCIns jmpins
= pc
[1];
2175 const BCIns
*npc
= pc
+ 2 + (cond
? bc_j(jmpins
) : 0);
2176 SnapShot
*snap
= &J
->cur
.snap
[J
->cur
.nsnap
-1];
2177 /* Set PC to opposite target to avoid re-recording the comp. in side trace. */
2179 SnapEntry
*flink
= &J
->cur
.snapmap
[snap
->mapofs
+ snap
->nent
];
2181 memcpy(&pcbase
, flink
, sizeof(uint64_t));
2182 pcbase
= (pcbase
& 0xff) | (u64ptr(npc
) << 8);
2183 memcpy(flink
, &pcbase
, sizeof(uint64_t));
2185 J
->cur
.snapmap
[snap
->mapofs
+ snap
->nent
] = SNAP_MKPC(npc
);
2188 if (bc_a(jmpins
) < J
->maxslot
) J
->maxslot
= bc_a(jmpins
);
2189 lj_snap_shrink(J
); /* Shrink last snapshot if possible. */
2192 /* Record the next bytecode instruction (_before_ it's executed). */
2193 void lj_record_ins(jit_State
*J
)
2202 /* Perform post-processing action before recording the next instruction. */
2203 if (LJ_UNLIKELY(J
->postproc
!= LJ_POST_NONE
)) {
2204 switch (J
->postproc
) {
2205 case LJ_POST_FIXCOMP
: /* Fixup comparison. */
2206 pc
= (const BCIns
*)(uintptr_t)J2G(J
)->tmptv
.u64
;
2207 rec_comp_fixup(J
, pc
, (!tvistruecond(&J2G(J
)->tmptv2
) ^ (bc_op(*pc
)&1)));
2209 case LJ_POST_FIXGUARD
: /* Fixup and emit pending guard. */
2210 case LJ_POST_FIXGUARDSNAP
: /* Fixup and emit pending guard and snapshot. */
2211 if (!tvistruecond(&J2G(J
)->tmptv2
)) {
2212 J
->fold
.ins
.o
^= 1; /* Flip guard to opposite. */
2213 if (J
->postproc
== LJ_POST_FIXGUARDSNAP
) {
2214 SnapShot
*snap
= &J
->cur
.snap
[J
->cur
.nsnap
-1];
2215 J
->cur
.snapmap
[snap
->mapofs
+snap
->nent
-1]--; /* False -> true. */
2218 lj_opt_fold(J
); /* Emit pending guard. */
2220 case LJ_POST_FIXBOOL
:
2221 if (!tvistruecond(&J2G(J
)->tmptv2
)) {
2223 TValue
*tv
= J
->L
->base
;
2224 for (s
= 0; s
< J
->maxslot
; s
++) /* Fixup stack slot (if any). */
2225 if (J
->base
[s
] == TREF_TRUE
&& tvisfalse(&tv
[s
])) {
2226 J
->base
[s
] = TREF_FALSE
;
2231 case LJ_POST_FIXCONST
:
2234 TValue
*tv
= J
->L
->base
;
2235 for (s
= 0; s
< J
->maxslot
; s
++) /* Constify stack slots (if any). */
2236 if (J
->base
[s
] == TREF_NIL
&& !tvisnil(&tv
[s
]))
2237 J
->base
[s
] = lj_record_constify(J
, &tv
[s
]);
2240 case LJ_POST_FFRETRY
: /* Suppress recording of retried fast function. */
2241 if (bc_op(*J
->pc
) >= BC__MAX
)
2244 default: lj_assertJ(0, "bad post-processing mode"); break;
2246 J
->postproc
= LJ_POST_NONE
;
2249 /* Need snapshot before recording next bytecode (e.g. after a store). */
2252 if (J
->pt
) lj_snap_purge(J
);
2257 /* Skip some bytecodes. */
2258 if (LJ_UNLIKELY(J
->bcskip
> 0)) {
2263 /* Record only closed loops for root traces. */
2265 if (J
->framedepth
== 0 &&
2266 (MSize
)((char *)pc
- (char *)J
->bc_min
) >= J
->bc_extent
)
2267 lj_trace_err(J
, LJ_TRERR_LLEAVE
);
2269 #ifdef LUA_USE_ASSERT
2275 rec_profile_ins(J
, pc
);
2278 /* Keep a copy of the runtime values of var/num/str operands. */
2279 #define rav (&ix.valv)
2280 #define rbv (&ix.tabv)
2281 #define rcv (&ix.keyv)
2288 switch (bcmode_a(op
)) {
2290 copyTV(J
->L
, rav
, &lbase
[ra
]); ix
.val
= ra
= getslot(J
, ra
); break;
2291 default: break; /* Handled later. */
2295 switch (bcmode_b(op
)) {
2296 case BCMnone
: rb
= 0; rc
= bc_d(ins
); break; /* Upgrade rc to 'rd'. */
2298 copyTV(J
->L
, rbv
, &lbase
[rb
]); ix
.tab
= rb
= getslot(J
, rb
); break;
2299 default: break; /* Handled later. */
2301 switch (bcmode_c(op
)) {
2303 copyTV(J
->L
, rcv
, &lbase
[rc
]); ix
.key
= rc
= getslot(J
, rc
); break;
2304 case BCMpri
: setpriV(rcv
, ~rc
); ix
.key
= rc
= TREF_PRI(IRT_NIL
+rc
); break;
2305 case BCMnum
: { cTValue
*tv
= proto_knumtv(J
->pt
, rc
);
2306 copyTV(J
->L
, rcv
, tv
); ix
.key
= rc
= tvisint(tv
) ? lj_ir_kint(J
, intV(tv
)) :
2307 tv
->u32
.hi
== LJ_KEYINDEX
? (lj_ir_kint(J
, 0) | TREF_KEYINDEX
) :
2308 lj_ir_knumint(J
, numV(tv
)); } break;
2309 case BCMstr
: { GCstr
*s
= gco2str(proto_kgc(J
->pt
, ~(ptrdiff_t)rc
));
2310 setstrV(J
->L
, rcv
, s
); ix
.key
= rc
= lj_ir_kstr(J
, s
); } break;
2311 default: break; /* Handled later. */
2316 /* -- Comparison ops ---------------------------------------------------- */
2318 case BC_ISLT
: case BC_ISGE
: case BC_ISLE
: case BC_ISGT
:
2320 if (tref_iscdata(ra
) || tref_iscdata(rc
)) {
2321 rec_mm_comp_cdata(J
, &ix
, op
, ((int)op
& 2) ? MM_le
: MM_lt
);
2325 /* Emit nothing for two numeric or string consts. */
2326 if (!(tref_isk2(ra
,rc
) && tref_isnumber_str(ra
) && tref_isnumber_str(rc
))) {
2327 IRType ta
= tref_isinteger(ra
) ? IRT_INT
: tref_type(ra
);
2328 IRType tc
= tref_isinteger(rc
) ? IRT_INT
: tref_type(rc
);
2331 /* Widen mixed number/int comparisons to number/number comparison. */
2332 if (ta
== IRT_INT
&& tc
== IRT_NUM
) {
2333 ra
= emitir(IRTN(IR_CONV
), ra
, IRCONV_NUM_INT
);
2335 } else if (ta
== IRT_NUM
&& tc
== IRT_INT
) {
2336 rc
= emitir(IRTN(IR_CONV
), rc
, IRCONV_NUM_INT
);
2338 ta
= IRT_NIL
; /* Force metamethod for different types. */
2339 } else if (!((ta
== IRT_FALSE
|| ta
== IRT_TRUE
) &&
2340 (tc
== IRT_FALSE
|| tc
== IRT_TRUE
))) {
2341 break; /* Interpreter will throw for two different types. */
2345 irop
= (int)op
- (int)BC_ISLT
+ (int)IR_LT
;
2346 if (ta
== IRT_NUM
) {
2347 if ((irop
& 1)) irop
^= 4; /* ISGE/ISGT are unordered. */
2348 if (!lj_ir_numcmp(numberVnum(rav
), numberVnum(rcv
), (IROp
)irop
))
2350 } else if (ta
== IRT_INT
) {
2351 if (!lj_ir_numcmp(numberVnum(rav
), numberVnum(rcv
), (IROp
)irop
))
2353 } else if (ta
== IRT_STR
) {
2354 if (!lj_ir_strcmp(strV(rav
), strV(rcv
), (IROp
)irop
)) irop
^= 1;
2355 ra
= lj_ir_call(J
, IRCALL_lj_str_cmp
, ra
, rc
);
2356 rc
= lj_ir_kint(J
, 0);
2359 rec_mm_comp(J
, &ix
, (int)op
);
2362 emitir(IRTG(irop
, ta
), ra
, rc
);
2363 rec_comp_fixup(J
, J
->pc
, ((int)op
^ irop
) & 1);
2367 case BC_ISEQV
: case BC_ISNEV
:
2368 case BC_ISEQS
: case BC_ISNES
:
2369 case BC_ISEQN
: case BC_ISNEN
:
2370 case BC_ISEQP
: case BC_ISNEP
:
2372 if (tref_iscdata(ra
) || tref_iscdata(rc
)) {
2373 rec_mm_comp_cdata(J
, &ix
, op
, MM_eq
);
2377 /* Emit nothing for two non-table, non-udata consts. */
2378 if (!(tref_isk2(ra
, rc
) && !(tref_istab(ra
) || tref_isudata(ra
)))) {
2381 diff
= lj_record_objcmp(J
, ra
, rc
, rav
, rcv
);
2382 if (diff
== 2 || !(tref_istab(ra
) || tref_isudata(ra
)))
2383 rec_comp_fixup(J
, J
->pc
, ((int)op
& 1) == !diff
);
2384 else if (diff
== 1) /* Only check __eq if different, but same type. */
2385 rec_mm_equal(J
, &ix
, (int)op
);
2389 /* -- Unary test and copy ops ------------------------------------------- */
2391 case BC_ISTC
: case BC_ISFC
:
2392 if ((op
& 1) == tref_istruecond(rc
))
2393 rc
= 0; /* Don't store if condition is not true. */
2395 case BC_IST
: case BC_ISF
: /* Type specialization suffices. */
2396 if (bc_a(pc
[1]) < J
->maxslot
)
2397 J
->maxslot
= bc_a(pc
[1]); /* Shrink used slots. */
2400 case BC_ISTYPE
: case BC_ISNUM
:
2401 /* These coercions need to correspond with lj_meta_istype(). */
2402 if (LJ_DUALNUM
&& rc
== ~LJ_TNUMX
+1)
2403 ra
= lj_opt_narrow_toint(J
, ra
);
2404 else if (rc
== ~LJ_TNUMX
+2)
2405 ra
= lj_ir_tonum(J
, ra
);
2406 else if (rc
== ~LJ_TSTR
+1)
2407 ra
= lj_ir_tostr(J
, ra
);
2408 /* else: type specialization suffices. */
2409 J
->base
[bc_a(ins
)] = ra
;
2412 /* -- Unary ops --------------------------------------------------------- */
2415 /* Type specialization already forces const result. */
2416 rc
= tref_istruecond(rc
) ? TREF_FALSE
: TREF_TRUE
;
2421 rc
= emitir(IRTI(IR_FLOAD
), rc
, IRFL_STR_LEN
);
2422 else if (!LJ_52
&& tref_istab(rc
))
2423 rc
= emitir(IRTI(IR_ALEN
), rc
, TREF_NIL
);
2425 rc
= rec_mm_len(J
, rc
, rcv
);
2428 /* -- Arithmetic ops ---------------------------------------------------- */
2431 if (tref_isnumber_str(rc
)) {
2432 rc
= lj_opt_narrow_unm(J
, rc
, rcv
);
2435 copyTV(J
->L
, &ix
.tabv
, rcv
);
2436 rc
= rec_mm_arith(J
, &ix
, MM_unm
);
2440 case BC_ADDNV
: case BC_SUBNV
: case BC_MULNV
: case BC_DIVNV
: case BC_MODNV
:
2441 /* Swap rb/rc and rbv/rcv. rav is temp. */
2442 ix
.tab
= rc
; ix
.key
= rc
= rb
; rb
= ix
.tab
;
2443 copyTV(J
->L
, rav
, rbv
);
2444 copyTV(J
->L
, rbv
, rcv
);
2445 copyTV(J
->L
, rcv
, rav
);
2449 case BC_ADDVN
: case BC_SUBVN
: case BC_MULVN
: case BC_DIVVN
:
2450 case BC_ADDVV
: case BC_SUBVV
: case BC_MULVV
: case BC_DIVVV
: {
2451 MMS mm
= bcmode_mm(op
);
2452 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
2453 rc
= lj_opt_narrow_arith(J
, rb
, rc
, rbv
, rcv
,
2454 (int)mm
- (int)MM_add
+ (int)IR_ADD
);
2456 rc
= rec_mm_arith(J
, &ix
, mm
);
2460 case BC_MODVN
: case BC_MODVV
:
2462 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
2463 rc
= lj_opt_narrow_mod(J
, rb
, rc
, rbv
, rcv
);
2465 rc
= rec_mm_arith(J
, &ix
, MM_mod
);
2469 if (tref_isnumber_str(rb
) && tref_isnumber_str(rc
))
2470 rc
= lj_opt_narrow_arith(J
, rb
, rc
, rbv
, rcv
, IR_POW
);
2472 rc
= rec_mm_arith(J
, &ix
, MM_pow
);
2475 /* -- Miscellaneous ops ------------------------------------------------- */
2478 rc
= rec_cat(J
, rb
, rc
);
2479 if (rc
>= 0xffffff00)
2480 lj_err_throw(J
->L
, -(int32_t)rc
); /* Propagate errors. */
2483 /* -- Constant and move ops --------------------------------------------- */
2486 /* Clear gap of method call to avoid resurrecting previous refs. */
2487 if (ra
> J
->maxslot
) {
2489 memset(J
->base
+ J
->maxslot
, 0, (ra
- J
->maxslot
) * sizeof(TRef
));
2495 case BC_KSTR
: case BC_KNUM
: case BC_KPRI
:
2498 rc
= lj_ir_kint(J
, (int32_t)(int16_t)rc
);
2501 if (LJ_FR2
&& ra
> J
->maxslot
)
2504 J
->base
[ra
++] = TREF_NIL
;
2505 if (rc
>= J
->maxslot
) J
->maxslot
= rc
+1;
2509 rc
= lj_ir_kgc(J
, proto_kgc(J
->pt
, ~(ptrdiff_t)rc
), IRT_CDATA
);
2513 /* -- Upvalue and function ops ------------------------------------------ */
2516 rc
= rec_upvalue(J
, rc
, 0);
2518 case BC_USETV
: case BC_USETS
: case BC_USETN
: case BC_USETP
:
2519 rec_upvalue(J
, ra
, rc
);
2522 /* -- Table ops --------------------------------------------------------- */
2524 case BC_GGET
: case BC_GSET
:
2525 settabV(J
->L
, &ix
.tabv
, tabref(J
->fn
->l
.env
));
2526 ix
.tab
= emitir(IRT(IR_FLOAD
, IRT_TAB
), getcurrf(J
), IRFL_FUNC_ENV
);
2527 ix
.idxchain
= LJ_MAX_IDXCHAIN
;
2528 rc
= lj_record_idx(J
, &ix
);
2531 case BC_TGETB
: case BC_TSETB
:
2532 setintV(&ix
.keyv
, (int32_t)rc
);
2533 ix
.key
= lj_ir_kint(J
, (int32_t)rc
);
2535 case BC_TGETV
: case BC_TGETS
: case BC_TSETV
: case BC_TSETS
:
2536 ix
.idxchain
= LJ_MAX_IDXCHAIN
;
2537 rc
= lj_record_idx(J
, &ix
);
2539 case BC_TGETR
: case BC_TSETR
:
2541 rc
= lj_record_idx(J
, &ix
);
2545 rec_tsetm(J
, ra
, (BCReg
)(J
->L
->top
- J
->L
->base
), (int32_t)rcv
->u32
.lo
);
2546 J
->maxslot
= ra
; /* The table slot at ra-1 is the highest used slot. */
2550 rc
= rec_tnew(J
, rc
);
2553 rc
= emitir(IRTG(IR_TDUP
, IRT_TAB
),
2554 lj_ir_ktab(J
, gco2tab(proto_kgc(J
->pt
, ~(ptrdiff_t)rc
))), 0);
2555 #ifdef LUAJIT_ENABLE_TABLE_BUMP
2556 J
->rbchash
[(rc
& (RBCHASH_SLOTS
-1))].ref
= tref_ref(rc
);
2557 setmref(J
->rbchash
[(rc
& (RBCHASH_SLOTS
-1))].pc
, pc
);
2558 setgcref(J
->rbchash
[(rc
& (RBCHASH_SLOTS
-1))].pt
, obj2gco(J
->pt
));
2562 /* -- Calls and vararg handling ----------------------------------------- */
2565 J
->base
[ra
] = getslot(J
, ra
-3);
2566 J
->base
[ra
+1+LJ_FR2
] = getslot(J
, ra
-2);
2567 J
->base
[ra
+2+LJ_FR2
] = getslot(J
, ra
-1);
2568 { /* Do the actual copy now because lj_record_call needs the values. */
2569 TValue
*b
= &J
->L
->base
[ra
];
2570 copyTV(J
->L
, b
, b
-3);
2571 copyTV(J
->L
, b
+1+LJ_FR2
, b
-2);
2572 copyTV(J
->L
, b
+2+LJ_FR2
, b
-1);
2574 lj_record_call(J
, ra
, (ptrdiff_t)rc
-1);
2577 /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */
2579 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
- LJ_FR2
;
2582 lj_record_call(J
, ra
, (ptrdiff_t)rc
-1);
2586 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
- LJ_FR2
;
2589 lj_record_tailcall(J
, ra
, (ptrdiff_t)rc
-1);
2593 rec_varg(J
, ra
, (ptrdiff_t)rb
-1);
2596 /* -- Returns ----------------------------------------------------------- */
2599 /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */
2600 rc
= (BCReg
)(J
->L
->top
- J
->L
->base
) - ra
+ 1;
2602 case BC_RET
: case BC_RET0
: case BC_RET1
:
2606 lj_record_ret(J
, ra
, (ptrdiff_t)rc
-1);
2609 /* -- Loops and branches ------------------------------------------------ */
2612 if (rec_for(J
, pc
, 0) != LOOPEV_LEAVE
)
2613 J
->loopref
= J
->cur
.nins
;
2616 lj_assertJ(bc_op(pc
[(ptrdiff_t)rc
-BCBIAS_J
]) == BC_JFORL
,
2617 "JFORI does not point to JFORL");
2618 if (rec_for(J
, pc
, 0) != LOOPEV_LEAVE
) /* Link to existing loop. */
2619 lj_record_stop(J
, LJ_TRLINK_ROOT
, bc_d(pc
[(ptrdiff_t)rc
-BCBIAS_J
]));
2620 /* Continue tracing if the loop is not entered. */
2624 rec_loop_interp(J
, pc
, rec_for(J
, pc
+((ptrdiff_t)rc
-BCBIAS_J
), 1));
2627 rec_loop_interp(J
, pc
, rec_iterl(J
, *pc
));
2630 rec_loop_interp(J
, pc
, rec_itern(J
, ra
, rb
));
2633 rec_loop_interp(J
, pc
, rec_loop(J
, ra
, 1));
2637 rec_loop_jit(J
, rc
, rec_for(J
, pc
+bc_j(traceref(J
, rc
)->startins
), 1));
2640 rec_loop_jit(J
, rc
, rec_iterl(J
, traceref(J
, rc
)->startins
));
2643 rec_loop_jit(J
, rc
, rec_loop(J
, ra
,
2644 !bc_isret(bc_op(traceref(J
, rc
)->startins
)) &&
2645 bc_op(traceref(J
, rc
)->startins
) != BC_ITERN
));
2653 lj_trace_err(J
, LJ_TRERR_BLACKL
);
2657 if (ra
< J
->maxslot
)
2658 J
->maxslot
= ra
; /* Shrink used slots. */
2665 /* -- Function headers -------------------------------------------------- */
2671 rec_func_jit(J
, rc
);
2679 /* Cannot happen. No hotcall counting for varag funcs. */
2680 lj_assertJ(0, "unsupported vararg hotcall");
2685 lj_ffrecord_func(J
);
2689 if (op
>= BC__MAX
) {
2690 lj_ffrecord_func(J
);
2696 setintV(&J
->errinfo
, (int32_t)op
);
2697 lj_trace_err_info(J
, LJ_TRERR_NYIBC
);
2701 /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */
2702 if (bcmode_a(op
) == BCMdst
&& rc
) {
2704 if (ra
>= J
->maxslot
) {
2706 if (ra
> J
->maxslot
) J
->base
[ra
-1] = 0;
2716 /* Limit the number of recorded IR instructions and constants. */
2717 if (J
->cur
.nins
> REF_FIRST
+(IRRef
)J
->param
[JIT_P_maxrecord
] ||
2718 J
->cur
.nk
< REF_BIAS
-(IRRef
)J
->param
[JIT_P_maxirconst
])
2719 lj_trace_err(J
, LJ_TRERR_TRACEOV
);
2722 /* -- Recording setup ----------------------------------------------------- */
2724 /* Setup recording for a root trace started by a hot loop. */
2725 static const BCIns
*rec_setup_root(jit_State
*J
)
2727 /* Determine the next PC and the bytecode range for the loop. */
2728 const BCIns
*pcj
, *pc
= J
->pc
;
2730 BCReg ra
= bc_a(ins
);
2731 switch (bc_op(ins
)) {
2733 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2738 if (bc_op(pc
[-1]) == BC_JLOOP
)
2739 lj_trace_err(J
, LJ_TRERR_LINNER
);
2740 lj_assertJ(bc_op(pc
[-1]) == BC_ITERC
, "no ITERC before ITERL");
2741 J
->maxslot
= ra
+ bc_b(pc
[-1]) - 1;
2742 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2744 lj_assertJ(bc_op(pc
[-1]) == BC_JMP
, "ITERL does not point to JMP+1");
2748 lj_assertJ(bc_op(pc
[1]) == BC_ITERL
, "no ITERL after ITERN");
2750 J
->bc_extent
= (MSize
)(-bc_j(pc
[1]))*sizeof(BCIns
);
2751 J
->bc_min
= pc
+2 + bc_j(pc
[1]);
2752 J
->state
= LJ_TRACE_RECORD_1ST
; /* Record the first ITERN, too. */
2755 /* Only check BC range for real loops, but not for "repeat until true". */
2756 pcj
= pc
+ bc_j(ins
);
2758 if (bc_op(ins
) == BC_JMP
&& bc_j(ins
) < 0) {
2759 J
->bc_min
= pcj
+1 + bc_j(ins
);
2760 J
->bc_extent
= (MSize
)(-bc_j(ins
))*sizeof(BCIns
);
2768 /* No bytecode range check for down-recursive root traces. */
2769 J
->maxslot
= ra
+ bc_d(ins
) - 1;
2772 /* No bytecode range check for root traces started by a hot call. */
2773 J
->maxslot
= J
->pt
->numparams
;
2779 /* No bytecode range check for stitched traces. */
2783 lj_assertJ(0, "bad root trace start bytecode %d", bc_op(ins
));
2789 /* Setup for recording a new trace. */
2790 void lj_record_setup(jit_State
*J
)
2794 /* Initialize state related to current trace. */
2795 memset(J
->slot
, 0, sizeof(J
->slot
));
2796 memset(J
->chain
, 0, sizeof(J
->chain
));
2797 #ifdef LUAJIT_ENABLE_TABLE_BUMP
2798 memset(J
->rbchash
, 0, sizeof(J
->rbchash
));
2800 memset(J
->bpropcache
, 0, sizeof(J
->bpropcache
));
2801 J
->scev
.idx
= REF_NIL
;
2802 setmref(J
->scev
.pc
, NULL
);
2804 J
->baseslot
= 1+LJ_FR2
; /* Invoking function is at base[-1-LJ_FR2]. */
2805 J
->base
= J
->slot
+ J
->baseslot
;
2810 J
->instunroll
= J
->param
[JIT_P_instunroll
];
2811 J
->loopunroll
= J
->param
[JIT_P_loopunroll
];
2815 J
->bc_min
= NULL
; /* Means no limit. */
2816 J
->bc_extent
= ~(MSize
)0;
2818 /* Emit instructions for fixed references. Also triggers initial IR alloc. */
2819 emitir_raw(IRT(IR_BASE
, IRT_PGC
), J
->parent
, J
->exitno
);
2820 for (i
= 0; i
<= 2; i
++) {
2821 IRIns
*ir
= IR(REF_NIL
-i
);
2823 ir
->t
.irt
= (uint8_t)(IRT_NIL
+i
);
2827 J
->cur
.nk
= REF_TRUE
;
2830 setmref(J
->cur
.startpc
, J
->pc
);
2831 if (J
->parent
) { /* Side trace. */
2832 GCtrace
*T
= traceref(J
, J
->parent
);
2833 TraceNo root
= T
->root
? T
->root
: J
->parent
;
2834 J
->cur
.root
= (uint16_t)root
;
2835 J
->cur
.startins
= BCINS_AD(BC_JMP
, 0, 0);
2836 /* Check whether we could at least potentially form an extra loop. */
2837 if (J
->exitno
== 0 && T
->snap
[0].nent
== 0) {
2838 /* We can narrow a FORL for some side traces, too. */
2839 if (J
->pc
> proto_bc(J
->pt
) && bc_op(J
->pc
[-1]) == BC_JFORI
&&
2840 bc_d(J
->pc
[bc_j(J
->pc
[-1])-1]) == root
) {
2842 rec_for_loop(J
, J
->pc
-1, &J
->scev
, 1);
2846 J
->startpc
= NULL
; /* Prevent forming an extra loop. */
2848 lj_snap_replay(J
, T
);
2850 if ((traceref(J
, J
->cur
.root
)->nchild
>= J
->param
[JIT_P_maxside
] ||
2851 T
->snap
[J
->exitno
].count
>= J
->param
[JIT_P_hotexit
] +
2852 J
->param
[JIT_P_tryside
])) {
2853 if (bc_op(*J
->pc
) == BC_JLOOP
) {
2854 BCIns startins
= traceref(J
, bc_d(*J
->pc
))->startins
;
2855 if (bc_op(startins
) == BC_ITERN
)
2856 rec_itern(J
, bc_a(startins
), bc_b(startins
));
2858 lj_record_stop(J
, LJ_TRLINK_INTERP
, 0);
2860 } else { /* Root trace. */
2862 J
->cur
.startins
= *J
->pc
;
2863 J
->pc
= rec_setup_root(J
);
2864 /* Note: the loop instruction itself is recorded at the end and not
2865 ** at the start! So snapshot #0 needs to point to the *next* instruction.
2866 ** The one exception is BC_ITERN, which sets LJ_TRACE_RECORD_1ST.
2869 if (bc_op(J
->cur
.startins
) == BC_FORL
)
2870 rec_for_loop(J
, J
->pc
-1, &J
->scev
, 1);
2871 else if (bc_op(J
->cur
.startins
) == BC_ITERC
)
2873 if (1 + J
->pt
->framesize
>= LJ_MAX_JSLOTS
)
2874 lj_trace_err(J
, LJ_TRERR_STACKOV
);
2880 #ifdef LUAJIT_ENABLE_CHECKHOOK
2881 /* Regularly check for instruction/line hooks from compiled code and
2882 ** exit to the interpreter if the hooks are set.
2884 ** This is a compile-time option and disabled by default, since the
2885 ** hook checks may be quite expensive in tight loops.
2887 ** Note this is only useful if hooks are *not* set most of the time.
2888 ** Use this only if you want to *asynchronously* interrupt the execution.
2890 ** You can set the instruction hook via lua_sethook() with a count of 1
2891 ** from a signal handler or another native thread. Please have a look
2892 ** at the first few functions in luajit.c for an example (Ctrl-C handler).
2895 TRef tr
= emitir(IRT(IR_XLOAD
, IRT_U8
),
2896 lj_ir_kptr(J
, &J2G(J
)->hookmask
), IRXLOAD_VOLATILE
);
2897 tr
= emitir(IRTI(IR_BAND
), tr
, lj_ir_kint(J
, (LUA_MASKLINE
|LUA_MASKCOUNT
)));
2898 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, 0));