2 ** IR assembler (SSA IR -> machine code).
3 ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
22 #include "lj_ircall.h"
29 #include "lj_dispatch.h"
31 #include "lj_target.h"
37 /* -- Assembler state and common macros ----------------------------------- */
39 /* Assembler state. */
40 typedef struct ASMState
{
41 RegCost cost
[RID_MAX
]; /* Reference and blended allocation cost for regs. */
43 MCode
*mcp
; /* Current MCode pointer (grows down). */
44 MCode
*mclim
; /* Lower limit for MCode memory + red zone. */
46 MCode
*mcp_prev
; /* Red zone overflow check. */
49 IRIns
*ir
; /* Copy of pointer to IR instructions/constants. */
50 jit_State
*J
; /* JIT compiler state. */
52 #if LJ_TARGET_X86ORX64
53 x86ModRM mrm
; /* Fused x86 address operand. */
56 RegSet freeset
; /* Set of free registers. */
57 RegSet modset
; /* Set of registers modified inside the loop. */
58 RegSet weakset
; /* Set of weakly referenced registers. */
59 RegSet phiset
; /* Set of PHI registers. */
61 uint32_t flags
; /* Copy of JIT compiler flags. */
62 int loopinv
; /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */
64 int32_t evenspill
; /* Next even spill slot. */
65 int32_t oddspill
; /* Next odd spill slot (or 0). */
67 IRRef curins
; /* Reference of current instruction. */
68 IRRef stopins
; /* Stop assembly before hitting this instruction. */
69 IRRef orignins
; /* Original T->nins. */
71 IRRef snapref
; /* Current snapshot is active after this reference. */
72 IRRef snaprename
; /* Rename highwater mark for snapshot check. */
73 SnapNo snapno
; /* Current snapshot number. */
74 SnapNo loopsnapno
; /* Loop snapshot number. */
76 IRRef fuseref
; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */
77 IRRef sectref
; /* Section base reference (loopref or 0). */
78 IRRef loopref
; /* Reference of LOOP instruction (or 0). */
80 BCReg topslot
; /* Number of slots for stack check (unless 0). */
81 int32_t gcsteps
; /* Accumulated number of GC steps (per section). */
83 GCtrace
*T
; /* Trace to assemble. */
84 GCtrace
*parent
; /* Parent trace (or NULL). */
86 MCode
*mcbot
; /* Bottom of reserved MCode. */
87 MCode
*mctop
; /* Top of generated MCode. */
88 MCode
*mcloop
; /* Pointer to loop MCode (or NULL). */
89 MCode
*invmcp
; /* Points to invertible loop branch (or NULL). */
90 MCode
*flagmcp
; /* Pending opportunity to merge flag setting ins. */
91 MCode
*realign
; /* Realign loop if not NULL. */
94 int32_t krefk
[RID_NUM_KREF
];
96 IRRef1 phireg
[RID_MAX
]; /* PHI register references. */
97 uint16_t parentmap
[LJ_MAX_JSLOTS
]; /* Parent instruction to RegSP map. */
100 #define IR(ref) (&as->ir[(ref)])
102 #define ASMREF_TMP1 REF_TRUE /* Temp. register. */
103 #define ASMREF_TMP2 REF_FALSE /* Temp. register. */
104 #define ASMREF_L REF_NIL /* Stores register for L. */
106 /* Check for variant to invariant references. */
107 #define iscrossref(as, ref) ((ref) < as->sectref)
109 /* Inhibit memory op fusion from variant to invariant references. */
110 #define FUSE_DISABLED (~(IRRef)0)
111 #define mayfuse(as, ref) ((ref) > as->fuseref)
112 #define neverfuse(as) (as->fuseref == FUSE_DISABLED)
113 #define canfuse(as, ir) (!neverfuse(as) && !irt_isphi((ir)->t))
114 #define opisfusableload(o) \
115 ((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \
116 (o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD)
118 /* Sparse limit checks using a red zone before the actual limit. */
119 #define MCLIM_REDZONE 64
121 static LJ_NORET LJ_NOINLINE
void asm_mclimit(ASMState
*as
)
123 lj_mcode_limiterr(as
->J
, (size_t)(as
->mctop
- as
->mcp
+ 4*MCLIM_REDZONE
));
126 static LJ_AINLINE
void checkmclim(ASMState
*as
)
128 #ifdef LUA_USE_ASSERT
129 if (as
->mcp
+ MCLIM_REDZONE
< as
->mcp_prev
) {
130 IRIns
*ir
= IR(as
->curins
+1);
131 fprintf(stderr
, "RED ZONE OVERFLOW: %p IR %04d %02d %04d %04d\n", as
->mcp
,
132 as
->curins
+1-REF_BIAS
, ir
->o
, ir
->op1
-REF_BIAS
, ir
->op2
-REF_BIAS
);
136 if (LJ_UNLIKELY(as
->mcp
< as
->mclim
)) asm_mclimit(as
);
137 #ifdef LUA_USE_ASSERT
138 as
->mcp_prev
= as
->mcp
;
143 #define ra_iskref(ref) ((ref) < RID_NUM_KREF)
144 #define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref)))
145 #define ra_krefk(as, ref) (as->krefk[(ref)])
147 static LJ_AINLINE
void ra_setkref(ASMState
*as
, Reg r
, int32_t k
)
149 IRRef ref
= (IRRef
)(r
- RID_MIN_KREF
);
151 as
->cost
[r
] = REGCOST(ref
, ref
);
155 #define ra_iskref(ref) 0
156 #define ra_krefreg(ref) RID_MIN_GPR
157 #define ra_krefk(as, ref) 0
160 /* Arch-specific field offsets. */
161 static const uint8_t field_ofs
[IRFL__MAX
+1] = {
162 #define FLOFS(name, ofs) (uint8_t)(ofs),
168 /* -- Target-specific instruction emitter --------------------------------- */
170 #if LJ_TARGET_X86ORX64
171 #include "lj_emit_x86.h"
173 #include "lj_emit_arm.h"
175 #include "lj_emit_ppc.h"
177 #include "lj_emit_mips.h"
179 #error "Missing instruction emitter for target CPU"
182 /* Generic load/store of register from/to stack slot. */
183 #define emit_spload(as, ir, r, ofs) \
184 emit_loadofs(as, ir, (r), RID_SP, (ofs))
185 #define emit_spstore(as, ir, r, ofs) \
186 emit_storeofs(as, ir, (r), RID_SP, (ofs))
188 /* -- Register allocator debugging ---------------------------------------- */
190 /* #define LUAJIT_DEBUG_RA */
192 #ifdef LUAJIT_DEBUG_RA
197 #define RIDNAME(name) #name,
198 static const char *const ra_regname
[] = {
206 static char ra_dbg_buf
[65536];
207 static char *ra_dbg_p
;
208 static char *ra_dbg_merge
;
209 static MCode
*ra_dbg_mcp
;
211 static void ra_dstart(void)
213 ra_dbg_p
= ra_dbg_buf
;
218 static void ra_dflush(void)
220 fwrite(ra_dbg_buf
, 1, (size_t)(ra_dbg_p
-ra_dbg_buf
), stdout
);
224 static void ra_dprintf(ASMState
*as
, const char *fmt
, ...)
229 p
= ra_dbg_mcp
== as
->mcp
? ra_dbg_merge
: ra_dbg_p
;
231 p
+= sprintf(p
, "%08x \e[36m%04d ", (uintptr_t)as
->mcp
, as
->curins
-REF_BIAS
);
233 const char *e
= strchr(fmt
, '$');
234 if (e
== NULL
) break;
235 memcpy(p
, fmt
, (size_t)(e
-fmt
));
238 Reg r
= va_arg(argp
, Reg
) & RID_MASK
;
241 for (q
= ra_regname
[r
]; *q
; q
++)
242 *p
++ = *q
>= 'A' && *q
<= 'Z' ? *q
+ 0x20 : *q
;
247 } else if (e
[1] == 'f' || e
[1] == 'i') {
250 ref
= va_arg(argp
, IRRef
);
252 ref
= va_arg(argp
, IRIns
*) - as
->ir
;
254 p
+= sprintf(p
, "%04d", ref
- REF_BIAS
);
256 p
+= sprintf(p
, "K%03d", REF_BIAS
- ref
);
257 } else if (e
[1] == 's') {
258 uint32_t slot
= va_arg(argp
, uint32_t);
259 p
+= sprintf(p
, "[sp+0x%x]", sps_scale(slot
));
260 } else if (e
[1] == 'x') {
261 p
+= sprintf(p
, "%08x", va_arg(argp
, int32_t));
270 *p
++ = '\e'; *p
++ = '['; *p
++ = 'm'; *p
++ = '\n';
271 if (p
> ra_dbg_buf
+sizeof(ra_dbg_buf
)-256) {
272 fwrite(ra_dbg_buf
, 1, (size_t)(p
-ra_dbg_buf
), stdout
);
278 #define RA_DBG_START() ra_dstart()
279 #define RA_DBG_FLUSH() ra_dflush()
280 #define RA_DBG_REF() \
281 do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \
282 ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0)
283 #define RA_DBGX(x) ra_dprintf x
286 #define RA_DBG_START() ((void)0)
287 #define RA_DBG_FLUSH() ((void)0)
288 #define RA_DBG_REF() ((void)0)
289 #define RA_DBGX(x) ((void)0)
292 /* -- Register allocator -------------------------------------------------- */
294 #define ra_free(as, r) rset_set(as->freeset, (r))
295 #define ra_modified(as, r) rset_set(as->modset, (r))
296 #define ra_weak(as, r) rset_set(as->weakset, (r))
297 #define ra_noweak(as, r) rset_clear(as->weakset, (r))
299 #define ra_used(ir) (ra_hasreg((ir)->r) || ra_hasspill((ir)->s))
301 /* Setup register allocator. */
302 static void ra_setup(ASMState
*as
)
305 /* Initially all regs (except the stack pointer) are free for use. */
306 as
->freeset
= RSET_INIT
;
307 as
->modset
= RSET_EMPTY
;
308 as
->weakset
= RSET_EMPTY
;
309 as
->phiset
= RSET_EMPTY
;
310 memset(as
->phireg
, 0, sizeof(as
->phireg
));
311 for (r
= RID_MIN_GPR
; r
< RID_MAX
; r
++)
312 as
->cost
[r
] = REGCOST(~0u, 0u);
315 /* Rematerialize constants. */
316 static Reg
ra_rematk(ASMState
*as
, IRRef ref
)
320 if (ra_iskref(ref
)) {
322 lua_assert(!rset_test(as
->freeset
, r
));
325 emit_loadi(as
, r
, ra_krefk(as
, ref
));
330 lua_assert(ra_hasreg(r
) && !ra_hasspill(ir
->s
));
333 ir
->r
= RID_INIT
; /* Do not keep any hint. */
334 RA_DBGX((as
, "remat $i $r", ir
, r
));
336 if (ir
->o
== IR_KNUM
) {
337 emit_loadn(as
, r
, ir_knum(ir
));
340 if (emit_canremat(REF_BASE
) && ir
->o
== IR_BASE
) {
341 ra_sethint(ir
->r
, RID_BASE
); /* Restore BASE register hint. */
342 emit_getgl(as
, r
, jit_base
);
343 } else if (emit_canremat(ASMREF_L
) && ir
->o
== IR_KPRI
) {
344 lua_assert(irt_isnil(ir
->t
)); /* REF_NIL stores ASMREF_L register. */
345 emit_getgl(as
, r
, cur_L
);
347 } else if (ir
->o
== IR_KINT64
) {
348 emit_loadu64(as
, r
, ir_kint64(ir
)->u64
);
351 lua_assert(ir
->o
== IR_KINT
|| ir
->o
== IR_KGC
||
352 ir
->o
== IR_KPTR
|| ir
->o
== IR_KKPTR
|| ir
->o
== IR_KNULL
);
353 emit_loadi(as
, r
, ir
->i
);
358 /* Force a spill. Allocate a new spill slot if needed. */
359 static int32_t ra_spill(ASMState
*as
, IRIns
*ir
)
361 int32_t slot
= ir
->s
;
362 if (!ra_hasspill(slot
)) {
363 if (irt_is64(ir
->t
)) {
364 slot
= as
->evenspill
;
366 } else if (as
->oddspill
) {
370 slot
= as
->evenspill
;
371 as
->oddspill
= slot
+1;
374 if (as
->evenspill
> 256)
375 lj_trace_err(as
->J
, LJ_TRERR_SPILLOV
);
376 ir
->s
= (uint8_t)slot
;
378 return sps_scale(slot
);
381 /* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */
382 static Reg
ra_releasetmp(ASMState
*as
, IRRef ref
)
386 lua_assert(ra_hasreg(r
) && !ra_hasspill(ir
->s
));
393 /* Restore a register (marked as free). Rematerialize or force a spill. */
394 static Reg
ra_restore(ASMState
*as
, IRRef ref
)
396 if (emit_canremat(ref
)) {
397 return ra_rematk(as
, ref
);
400 int32_t ofs
= ra_spill(as
, ir
); /* Force a spill slot. */
402 lua_assert(ra_hasreg(r
));
403 ra_sethint(ir
->r
, r
); /* Keep hint. */
405 if (!rset_test(as
->weakset
, r
)) { /* Only restore non-weak references. */
407 RA_DBGX((as
, "restore $i $r", ir
, r
));
408 emit_spload(as
, ir
, r
, ofs
);
414 /* Save a register to a spill slot. */
415 static void ra_save(ASMState
*as
, IRIns
*ir
, Reg r
)
417 RA_DBGX((as
, "save $i $r", ir
, r
));
418 emit_spstore(as
, ir
, r
, sps_scale(ir
->s
));
421 #define MINCOST(name) \
422 if (rset_test(RSET_ALL, RID_##name) && \
423 LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \
424 cost = as->cost[RID_##name];
426 /* Evict the register with the lowest cost, forcing a restore. */
427 static Reg
ra_evict(ASMState
*as
, RegSet allow
)
430 RegCost cost
= ~(RegCost
)0;
431 lua_assert(allow
!= RSET_EMPTY
);
432 if (RID_NUM_FPR
== 0 || allow
< RID2RSET(RID_MAX_GPR
)) {
437 ref
= regcost_ref(cost
);
438 lua_assert(ra_iskref(ref
) || (ref
>= as
->T
->nk
&& ref
< as
->T
->nins
));
439 /* Preferably pick any weak ref instead of a non-weak, non-const ref. */
440 if (!irref_isk(ref
) && (as
->weakset
& allow
)) {
442 if (!rset_test(as
->weakset
, ir
->r
))
443 ref
= regcost_ref(as
->cost
[rset_pickbot((as
->weakset
& allow
))]);
445 return ra_restore(as
, ref
);
448 /* Pick any register (marked as free). Evict on-demand. */
449 static Reg
ra_pick(ASMState
*as
, RegSet allow
)
451 RegSet pick
= as
->freeset
& allow
;
453 return ra_evict(as
, allow
);
455 return rset_picktop(pick
);
458 /* Get a scratch register (marked as free). */
459 static Reg
ra_scratch(ASMState
*as
, RegSet allow
)
461 Reg r
= ra_pick(as
, allow
);
463 RA_DBGX((as
, "scratch $r", r
));
467 /* Evict all registers from a set (if not free). */
468 static void ra_evictset(ASMState
*as
, RegSet drop
)
473 work
= (drop
& ~as
->freeset
) & RSET_FPR
;
475 Reg r
= rset_pickbot(work
);
476 ra_restore(as
, regcost_ref(as
->cost
[r
]));
481 work
= (drop
& ~as
->freeset
);
483 Reg r
= rset_pickbot(work
);
484 ra_restore(as
, regcost_ref(as
->cost
[r
]));
490 /* Evict (rematerialize) all registers allocated to constants. */
491 static void ra_evictk(ASMState
*as
)
495 work
= ~as
->freeset
& RSET_FPR
;
497 Reg r
= rset_pickbot(work
);
498 IRRef ref
= regcost_ref(as
->cost
[r
]);
499 if (emit_canremat(ref
) && irref_isk(ref
)) {
506 work
= ~as
->freeset
& RSET_GPR
;
508 Reg r
= rset_pickbot(work
);
509 IRRef ref
= regcost_ref(as
->cost
[r
]);
510 if (emit_canremat(ref
) && irref_isk(ref
)) {
519 /* Allocate a register for a constant. */
520 static Reg
ra_allock(ASMState
*as
, int32_t k
, RegSet allow
)
522 /* First try to find a register which already holds the same constant. */
523 RegSet pick
, work
= ~as
->freeset
& RSET_GPR
;
527 r
= rset_pickbot(work
);
528 ref
= regcost_ref(as
->cost
[r
]);
529 if (ref
< ASMREF_L
&&
530 k
== (ra_iskref(ref
) ? ra_krefk(as
, ref
) : IR(ref
)->i
))
534 pick
= as
->freeset
& allow
;
536 /* Constants should preferably get unmodified registers. */
537 if ((pick
& ~as
->modset
))
539 r
= rset_pickbot(pick
); /* Reduce conflicts with inverse allocation. */
541 r
= ra_evict(as
, allow
);
543 RA_DBGX((as
, "allock $x $r", k
, r
));
544 ra_setkref(as
, r
, k
);
545 rset_clear(as
->freeset
, r
);
550 /* Allocate a specific register for a constant. */
551 static void ra_allockreg(ASMState
*as
, int32_t k
, Reg r
)
553 Reg kr
= ra_allock(as
, k
, RID2RSET(r
));
556 irdummy
.t
.irt
= IRT_INT
;
557 ra_scratch(as
, RID2RSET(r
));
558 emit_movrr(as
, &irdummy
, r
, kr
);
562 #define ra_allockreg(as, k, r) emit_loadi(as, (r), (k))
565 /* Allocate a register for ref from the allowed set of registers.
566 ** Note: this function assumes the ref does NOT have a register yet!
567 ** Picks an optimal register, sets the cost and marks the register as non-free.
569 static Reg
ra_allocref(ASMState
*as
, IRRef ref
, RegSet allow
)
572 RegSet pick
= as
->freeset
& allow
;
574 lua_assert(ra_noreg(ir
->r
));
576 /* First check register hint from propagation or PHI. */
577 if (ra_hashint(ir
->r
)) {
578 r
= ra_gethint(ir
->r
);
579 if (rset_test(pick
, r
)) /* Use hint register if possible. */
581 /* Rematerialization is cheaper than missing a hint. */
582 if (rset_test(allow
, r
) && emit_canremat(regcost_ref(as
->cost
[r
]))) {
583 ra_rematk(as
, regcost_ref(as
->cost
[r
]));
586 RA_DBGX((as
, "hintmiss $f $r", ref
, r
));
588 /* Invariants should preferably get unmodified registers. */
589 if (ref
< as
->loopref
&& !irt_isphi(ir
->t
)) {
590 if ((pick
& ~as
->modset
))
592 r
= rset_pickbot(pick
); /* Reduce conflicts with inverse allocation. */
594 /* We've got plenty of regs, so get callee-save regs if possible. */
595 if (RID_NUM_GPR
> 8 && (pick
& ~RSET_SCRATCH
))
596 pick
&= ~RSET_SCRATCH
;
597 r
= rset_picktop(pick
);
600 r
= ra_evict(as
, allow
);
603 RA_DBGX((as
, "alloc $f $r", ref
, r
));
605 rset_clear(as
->freeset
, r
);
607 as
->cost
[r
] = REGCOST_REF_T(ref
, irt_t(ir
->t
));
611 /* Allocate a register on-demand. */
612 static Reg
ra_alloc1(ASMState
*as
, IRRef ref
, RegSet allow
)
615 /* Note: allow is ignored if the register is already allocated. */
616 if (ra_noreg(r
)) r
= ra_allocref(as
, ref
, allow
);
621 /* Rename register allocation and emit move. */
622 static void ra_rename(ASMState
*as
, Reg down
, Reg up
)
624 IRRef ren
, ref
= regcost_ref(as
->cost
[up
] = as
->cost
[down
]);
628 lua_assert((down
< RID_MAX_GPR
) == (up
< RID_MAX_GPR
));
629 lua_assert(!rset_test(as
->freeset
, down
) && rset_test(as
->freeset
, up
));
630 ra_free(as
, down
); /* 'down' is free ... */
631 ra_modified(as
, down
);
632 rset_clear(as
->freeset
, up
); /* ... and 'up' is now allocated. */
634 RA_DBGX((as
, "rename $f $r $r", regcost_ref(as
->cost
[up
]), down
, up
));
635 emit_movrr(as
, ir
, down
, up
); /* Backwards codegen needs inverse move. */
636 if (!ra_hasspill(IR(ref
)->s
)) { /* Add the rename to the IR. */
637 lj_ir_set(as
->J
, IRT(IR_RENAME
, IRT_NIL
), ref
, as
->snapno
);
638 ren
= tref_ref(lj_ir_emit(as
->J
));
639 as
->ir
= as
->T
->ir
; /* The IR may have been reallocated. */
640 IR(ren
)->r
= (uint8_t)down
;
641 IR(ren
)->s
= SPS_NONE
;
645 /* Pick a destination register (marked as free).
646 ** Caveat: allow is ignored if there's already a destination register.
647 ** Use ra_destreg() to get a specific register.
649 static Reg
ra_dest(ASMState
*as
, IRIns
*ir
, RegSet allow
)
652 if (ra_hasreg(dest
)) {
654 ra_modified(as
, dest
);
656 if (ra_hashint(dest
) && rset_test((as
->freeset
&allow
), ra_gethint(dest
))) {
657 dest
= ra_gethint(dest
);
658 ra_modified(as
, dest
);
659 RA_DBGX((as
, "dest $r", dest
));
661 dest
= ra_scratch(as
, allow
);
665 if (LJ_UNLIKELY(ra_hasspill(ir
->s
))) ra_save(as
, ir
, dest
);
669 /* Force a specific destination register (marked as free). */
670 static void ra_destreg(ASMState
*as
, IRIns
*ir
, Reg r
)
672 Reg dest
= ra_dest(as
, ir
, RID2RSET(r
));
674 lua_assert(rset_test(as
->freeset
, r
));
676 emit_movrr(as
, ir
, dest
, r
);
680 #if LJ_TARGET_X86ORX64
681 /* Propagate dest register to left reference. Emit moves as needed.
682 ** This is a required fixup step for all 2-operand machine instructions.
684 static void ra_left(ASMState
*as
, Reg dest
, IRRef lref
)
686 IRIns
*ir
= IR(lref
);
688 if (ra_noreg(left
)) {
689 if (irref_isk(lref
)) {
690 if (ir
->o
== IR_KNUM
) {
691 cTValue
*tv
= ir_knum(ir
);
692 /* FP remat needs a load except for +0. Still better than eviction. */
693 if (tvispzero(tv
) || !(as
->freeset
& RSET_FPR
)) {
694 emit_loadn(as
, dest
, tv
);
698 } else if (ir
->o
== IR_KINT64
) {
699 emit_loadu64(as
, dest
, ir_kint64(ir
)->u64
);
703 lua_assert(ir
->o
== IR_KINT
|| ir
->o
== IR_KGC
||
704 ir
->o
== IR_KPTR
|| ir
->o
== IR_KKPTR
|| ir
->o
== IR_KNULL
);
705 emit_loadi(as
, dest
, ir
->i
);
709 if (!ra_hashint(left
) && !iscrossref(as
, lref
))
710 ra_sethint(ir
->r
, dest
); /* Propagate register hint. */
711 left
= ra_allocref(as
, lref
, dest
< RID_MAX_GPR
? RSET_GPR
: RSET_FPR
);
714 /* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */
716 /* Use register renaming if dest is the PHI reg. */
717 if (irt_isphi(ir
->t
) && as
->phireg
[dest
] == lref
) {
718 ra_modified(as
, left
);
719 ra_rename(as
, left
, dest
);
721 emit_movrr(as
, ir
, dest
, left
);
726 /* Similar to ra_left, except we override any hints. */
727 static void ra_leftov(ASMState
*as
, Reg dest
, IRRef lref
)
729 IRIns
*ir
= IR(lref
);
731 if (ra_noreg(left
)) {
732 ra_sethint(ir
->r
, dest
); /* Propagate register hint. */
733 left
= ra_allocref(as
, lref
,
734 (LJ_SOFTFP
|| dest
< RID_MAX_GPR
) ? RSET_GPR
: RSET_FPR
);
738 /* Use register renaming if dest is the PHI reg. */
739 if (irt_isphi(ir
->t
) && as
->phireg
[dest
] == lref
) {
740 ra_modified(as
, left
);
741 ra_rename(as
, left
, dest
);
743 emit_movrr(as
, ir
, dest
, left
);
750 /* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */
751 static void ra_destpair(ASMState
*as
, IRIns
*ir
)
753 Reg destlo
= ir
->r
, desthi
= (ir
+1)->r
;
754 /* First spill unrelated refs blocking the destination registers. */
755 if (!rset_test(as
->freeset
, RID_RETLO
) &&
756 destlo
!= RID_RETLO
&& desthi
!= RID_RETLO
)
757 ra_restore(as
, regcost_ref(as
->cost
[RID_RETLO
]));
758 if (!rset_test(as
->freeset
, RID_RETHI
) &&
759 destlo
!= RID_RETHI
&& desthi
!= RID_RETHI
)
760 ra_restore(as
, regcost_ref(as
->cost
[RID_RETHI
]));
761 /* Next free the destination registers (if any). */
762 if (ra_hasreg(destlo
)) {
764 ra_modified(as
, destlo
);
768 if (ra_hasreg(desthi
)) {
770 ra_modified(as
, desthi
);
774 /* Check for conflicts and shuffle the registers as needed. */
775 if (destlo
== RID_RETHI
) {
776 if (desthi
== RID_RETLO
) {
778 *--as
->mcp
= XI_XCHGa
+ RID_RETHI
;
780 emit_movrr(as
, ir
, RID_RETHI
, RID_TMP
);
781 emit_movrr(as
, ir
, RID_RETLO
, RID_RETHI
);
782 emit_movrr(as
, ir
, RID_TMP
, RID_RETLO
);
785 emit_movrr(as
, ir
, RID_RETHI
, RID_RETLO
);
786 if (desthi
!= RID_RETHI
) emit_movrr(as
, ir
, desthi
, RID_RETHI
);
788 } else if (desthi
== RID_RETLO
) {
789 emit_movrr(as
, ir
, RID_RETLO
, RID_RETHI
);
790 if (destlo
!= RID_RETLO
) emit_movrr(as
, ir
, destlo
, RID_RETLO
);
792 if (desthi
!= RID_RETHI
) emit_movrr(as
, ir
, desthi
, RID_RETHI
);
793 if (destlo
!= RID_RETLO
) emit_movrr(as
, ir
, destlo
, RID_RETLO
);
795 /* Restore spill slots (if any). */
796 if (ra_hasspill((ir
+1)->s
)) ra_save(as
, ir
+1, RID_RETHI
);
797 if (ra_hasspill(ir
->s
)) ra_save(as
, ir
, RID_RETLO
);
801 /* -- Snapshot handling --------- ----------------------------------------- */
803 /* Can we rematerialize a KNUM instead of forcing a spill? */
804 static int asm_snap_canremat(ASMState
*as
)
807 for (r
= RID_MIN_FPR
; r
< RID_MAX_FPR
; r
++)
808 if (irref_isk(regcost_ref(as
->cost
[r
])))
813 /* Check whether a sunk store corresponds to an allocation. */
814 static int asm_sunk_store(ASMState
*as
, IRIns
*ira
, IRIns
*irs
)
817 if (irs
->o
== IR_ASTORE
|| irs
->o
== IR_HSTORE
||
818 irs
->o
== IR_FSTORE
|| irs
->o
== IR_XSTORE
) {
819 IRIns
*irk
= IR(irs
->op1
);
820 if (irk
->o
== IR_AREF
|| irk
->o
== IR_HREFK
)
822 return (IR(irk
->op1
) == ira
);
826 return (ira
+ irs
->s
== irs
); /* Quick check. */
830 /* Allocate register or spill slot for a ref that escapes to a snapshot. */
831 static void asm_snap_alloc1(ASMState
*as
, IRRef ref
)
834 if (!irref_isk(ref
) && (!(ra_used(ir
) || ir
->r
== RID_SUNK
))) {
835 if (ir
->r
== RID_SINK
) {
838 if (ir
->o
== IR_CNEWI
) { /* Allocate CNEWI value. */
839 asm_snap_alloc1(as
, ir
->op2
);
840 if (LJ_32
&& (ir
+1)->o
== IR_HIOP
)
841 asm_snap_alloc1(as
, (ir
+1)->op2
);
844 { /* Allocate stored values for TNEW, TDUP and CNEW. */
846 lua_assert(ir
->o
== IR_TNEW
|| ir
->o
== IR_TDUP
|| ir
->o
== IR_CNEW
);
847 for (irs
= IR(as
->snapref
-1); irs
> ir
; irs
--)
848 if (irs
->r
== RID_SINK
&& asm_sunk_store(as
, ir
, irs
)) {
849 lua_assert(irs
->o
== IR_ASTORE
|| irs
->o
== IR_HSTORE
||
850 irs
->o
== IR_FSTORE
|| irs
->o
== IR_XSTORE
);
851 asm_snap_alloc1(as
, irs
->op2
);
852 if (LJ_32
&& (irs
+1)->o
== IR_HIOP
)
853 asm_snap_alloc1(as
, (irs
+1)->op2
);
858 if (ir
->o
== IR_CONV
&& ir
->op2
== IRCONV_NUM_INT
) {
860 for (irc
= IR(as
->curins
); irc
> ir
; irc
--)
861 if ((irc
->op1
== ref
|| irc
->op2
== ref
) &&
862 !(irc
->r
== RID_SINK
|| irc
->r
== RID_SUNK
))
863 goto nosink
; /* Don't sink conversion if result is used. */
864 asm_snap_alloc1(as
, ir
->op1
);
868 allow
= (!LJ_SOFTFP
&& irt_isfp(ir
->t
)) ? RSET_FPR
: RSET_GPR
;
869 if ((as
->freeset
& allow
) ||
870 (allow
== RSET_FPR
&& asm_snap_canremat(as
))) {
871 /* Get a weak register if we have a free one or can rematerialize. */
872 Reg r
= ra_allocref(as
, ref
, allow
); /* Allocate a register. */
873 if (!irt_isphi(ir
->t
))
874 ra_weak(as
, r
); /* But mark it as weakly referenced. */
876 RA_DBGX((as
, "snapreg $f $r", ref
, ir
->r
));
878 ra_spill(as
, ir
); /* Otherwise force a spill slot. */
879 RA_DBGX((as
, "snapspill $f $s", ref
, ir
->s
));
885 /* Allocate refs escaping to a snapshot. */
886 static void asm_snap_alloc(ASMState
*as
)
888 SnapShot
*snap
= &as
->T
->snap
[as
->snapno
];
889 SnapEntry
*map
= &as
->T
->snapmap
[snap
->mapofs
];
890 MSize n
, nent
= snap
->nent
;
891 for (n
= 0; n
< nent
; n
++) {
892 SnapEntry sn
= map
[n
];
893 IRRef ref
= snap_ref(sn
);
894 if (!irref_isk(ref
)) {
895 asm_snap_alloc1(as
, ref
);
896 if (LJ_SOFTFP
&& (sn
& SNAP_SOFTFPNUM
)) {
897 lua_assert(irt_type(IR(ref
+1)->t
) == IRT_SOFTFP
);
898 asm_snap_alloc1(as
, ref
+1);
904 /* All guards for a snapshot use the same exitno. This is currently the
905 ** same as the snapshot number. Since the exact origin of the exit cannot
906 ** be determined, all guards for the same snapshot must exit with the same
908 ** A renamed ref which has been used in a prior guard for the same snapshot
909 ** would cause an inconsistency. The easy way out is to force a spill slot.
911 static int asm_snap_checkrename(ASMState
*as
, IRRef ren
)
913 SnapShot
*snap
= &as
->T
->snap
[as
->snapno
];
914 SnapEntry
*map
= &as
->T
->snapmap
[snap
->mapofs
];
915 MSize n
, nent
= snap
->nent
;
916 for (n
= 0; n
< nent
; n
++) {
917 SnapEntry sn
= map
[n
];
918 IRRef ref
= snap_ref(sn
);
919 if (ref
== ren
|| (LJ_SOFTFP
&& (sn
& SNAP_SOFTFPNUM
) && ++ref
== ren
)) {
921 ra_spill(as
, ir
); /* Register renamed, so force a spill slot. */
922 RA_DBGX((as
, "snaprensp $f $s", ref
, ir
->s
));
923 return 1; /* Found. */
926 return 0; /* Not found. */
929 /* Prepare snapshot for next guard instruction. */
930 static void asm_snap_prep(ASMState
*as
)
932 if (as
->curins
< as
->snapref
) {
934 if (as
->snapno
== 0) return; /* Called by sunk stores before snap #0. */
936 as
->snapref
= as
->T
->snap
[as
->snapno
].ref
;
937 } while (as
->curins
< as
->snapref
);
939 as
->snaprename
= as
->T
->nins
;
941 /* Process any renames above the highwater mark. */
942 for (; as
->snaprename
< as
->T
->nins
; as
->snaprename
++) {
943 IRIns
*ir
= IR(as
->snaprename
);
944 if (asm_snap_checkrename(as
, ir
->op1
))
945 ir
->op2
= REF_BIAS
-1; /* Kill rename. */
950 /* -- Miscellaneous helpers ----------------------------------------------- */
952 /* Calculate stack adjustment. */
953 static int32_t asm_stack_adjust(ASMState
*as
)
955 if (as
->evenspill
<= SPS_FIXED
)
957 return sps_scale(sps_align(as
->evenspill
));
960 /* Must match with hash*() in lj_tab.c. */
961 static uint32_t ir_khash(IRIns
*ir
)
964 if (irt_isstr(ir
->t
)) {
965 return ir_kstr(ir
)->hash
;
966 } else if (irt_isnum(ir
->t
)) {
967 lo
= ir_knum(ir
)->u32
.lo
;
968 hi
= ir_knum(ir
)->u32
.hi
<< 1;
969 } else if (irt_ispri(ir
->t
)) {
970 lua_assert(!irt_isnil(ir
->t
));
971 return irt_type(ir
->t
)-IRT_FALSE
;
973 lua_assert(irt_isgcv(ir
->t
));
974 lo
= u32ptr(ir_kgc(ir
));
977 return hashrot(lo
, hi
);
980 /* -- Allocations --------------------------------------------------------- */
982 static void asm_gencall(ASMState
*as
, const CCallInfo
*ci
, IRRef
*args
);
983 static void asm_setupresult(ASMState
*as
, IRIns
*ir
, const CCallInfo
*ci
);
985 static void asm_snew(ASMState
*as
, IRIns
*ir
)
987 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_str_new
];
989 args
[0] = ASMREF_L
; /* lua_State *L */
990 args
[1] = ir
->op1
; /* const char *str */
991 args
[2] = ir
->op2
; /* size_t len */
993 asm_setupresult(as
, ir
, ci
); /* GCstr * */
994 asm_gencall(as
, ci
, args
);
997 static void asm_tnew(ASMState
*as
, IRIns
*ir
)
999 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_tab_new1
];
1001 args
[0] = ASMREF_L
; /* lua_State *L */
1002 args
[1] = ASMREF_TMP1
; /* uint32_t ahsize */
1004 asm_setupresult(as
, ir
, ci
); /* GCtab * */
1005 asm_gencall(as
, ci
, args
);
1006 ra_allockreg(as
, ir
->op1
| (ir
->op2
<< 24), ra_releasetmp(as
, ASMREF_TMP1
));
1009 static void asm_tdup(ASMState
*as
, IRIns
*ir
)
1011 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_tab_dup
];
1013 args
[0] = ASMREF_L
; /* lua_State *L */
1014 args
[1] = ir
->op1
; /* const GCtab *kt */
1016 asm_setupresult(as
, ir
, ci
); /* GCtab * */
1017 asm_gencall(as
, ci
, args
);
1020 static void asm_gc_check(ASMState
*as
);
1022 /* Explicit GC step. */
1023 static void asm_gcstep(ASMState
*as
, IRIns
*ir
)
1026 for (ira
= IR(as
->stopins
+1); ira
< ir
; ira
++)
1027 if ((ira
->o
== IR_TNEW
|| ira
->o
== IR_TDUP
||
1028 (LJ_HASFFI
&& (ira
->o
== IR_CNEW
|| ira
->o
== IR_CNEWI
))) &&
1033 as
->gcsteps
= 0x80000000; /* Prevent implicit GC check further up. */
1036 /* -- Buffer operations --------------------------------------------------- */
1038 static void asm_tvptr(ASMState
*as
, Reg dest
, IRRef ref
);
1040 static void asm_bufhdr(ASMState
*as
, IRIns
*ir
)
1042 Reg sb
= ra_dest(as
, ir
, RSET_GPR
);
1043 if ((ir
->op2
& IRBUFHDR_APPEND
)) {
1044 /* Rematerialize const buffer pointer instead of likely spill. */
1045 IRIns
*irp
= IR(ir
->op1
);
1046 if (!(ra_hasreg(irp
->r
) || irp
== ir
-1 ||
1047 (irp
== ir
-2 && !ra_used(ir
-1)))) {
1048 while (!(irp
->o
== IR_BUFHDR
&& !(irp
->op2
& IRBUFHDR_APPEND
)))
1050 if (irref_isk(irp
->op1
)) {
1051 ra_weak(as
, ra_allocref(as
, ir
->op1
, RSET_GPR
));
1056 Reg tmp
= ra_scratch(as
, rset_exclude(RSET_GPR
, sb
));
1057 /* Passing ir isn't strictly correct, but it's an IRT_P32, too. */
1058 emit_storeofs(as
, ir
, tmp
, sb
, offsetof(SBuf
, p
));
1059 emit_loadofs(as
, ir
, tmp
, sb
, offsetof(SBuf
, b
));
1061 #if LJ_TARGET_X86ORX64
1062 ra_left(as
, sb
, ir
->op1
);
1064 ra_leftov(as
, sb
, ir
->op1
);
1068 static void asm_bufput(ASMState
*as
, IRIns
*ir
)
1070 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_buf_putstr
];
1074 args
[0] = ir
->op1
; /* SBuf * */
1075 args
[1] = ir
->op2
; /* GCstr * */
1077 lua_assert(irt_isstr(irs
->t
));
1078 if (irs
->o
== IR_KGC
) {
1079 GCstr
*s
= ir_kstr(irs
);
1080 if (s
->len
== 1) { /* Optimize put of single-char string constant. */
1081 kchar
= strdata(s
)[0];
1082 args
[1] = ASMREF_TMP1
; /* int, truncated to char */
1083 ci
= &lj_ir_callinfo
[IRCALL_lj_buf_putchar
];
1085 } else if (mayfuse(as
, ir
->op2
) && ra_noreg(irs
->r
)) {
1086 if (irs
->o
== IR_TOSTR
) { /* Fuse number to string conversions. */
1087 if (irs
->op2
== IRTOSTR_NUM
) {
1088 args
[1] = ASMREF_TMP1
; /* TValue * */
1089 ci
= &lj_ir_callinfo
[IRCALL_lj_strfmt_putnum
];
1091 lua_assert(irt_isinteger(IR(irs
->op1
)->t
));
1092 args
[1] = irs
->op1
; /* int */
1093 if (irs
->op2
== IRTOSTR_INT
)
1094 ci
= &lj_ir_callinfo
[IRCALL_lj_strfmt_putint
];
1096 ci
= &lj_ir_callinfo
[IRCALL_lj_buf_putchar
];
1098 } else if (irs
->o
== IR_SNEW
) { /* Fuse string allocation. */
1099 args
[1] = irs
->op1
; /* const void * */
1100 args
[2] = irs
->op2
; /* MSize */
1101 ci
= &lj_ir_callinfo
[IRCALL_lj_buf_putmem
];
1104 asm_setupresult(as
, ir
, ci
); /* SBuf * */
1105 asm_gencall(as
, ci
, args
);
1106 if (args
[1] == ASMREF_TMP1
) {
1107 Reg tmp
= ra_releasetmp(as
, ASMREF_TMP1
);
1109 asm_tvptr(as
, tmp
, irs
->op1
);
1111 ra_allockreg(as
, kchar
, tmp
);
1115 static void asm_bufstr(ASMState
*as
, IRIns
*ir
)
1117 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_buf_tostr
];
1119 args
[0] = ir
->op1
; /* SBuf *sb */
1121 asm_setupresult(as
, ir
, ci
); /* GCstr * */
1122 asm_gencall(as
, ci
, args
);
1125 /* -- Type conversions ---------------------------------------------------- */
1127 static void asm_tostr(ASMState
*as
, IRIns
*ir
)
1129 const CCallInfo
*ci
;
1133 if (ir
->op2
== IRTOSTR_NUM
) {
1134 args
[1] = ASMREF_TMP1
; /* cTValue * */
1135 ci
= &lj_ir_callinfo
[IRCALL_lj_strfmt_num
];
1137 args
[1] = ir
->op1
; /* int32_t k */
1138 if (ir
->op2
== IRTOSTR_INT
)
1139 ci
= &lj_ir_callinfo
[IRCALL_lj_strfmt_int
];
1141 ci
= &lj_ir_callinfo
[IRCALL_lj_strfmt_char
];
1143 asm_setupresult(as
, ir
, ci
); /* GCstr * */
1144 asm_gencall(as
, ci
, args
);
1145 if (ir
->op2
== IRTOSTR_NUM
)
1146 asm_tvptr(as
, ra_releasetmp(as
, ASMREF_TMP1
), ir
->op1
);
1149 #if LJ_32 && LJ_HASFFI && !LJ_SOFTFP && !LJ_TARGET_X86
1150 static void asm_conv64(ASMState
*as
, IRIns
*ir
)
1152 IRType st
= (IRType
)((ir
-1)->op2
& IRCONV_SRCMASK
);
1153 IRType dt
= (((ir
-1)->op2
& IRCONV_DSTMASK
) >> IRCONV_DSH
);
1156 lua_assert((ir
-1)->o
== IR_CONV
&& ir
->o
== IR_HIOP
);
1157 args
[LJ_BE
] = (ir
-1)->op1
;
1158 args
[LJ_LE
] = ir
->op1
;
1159 if (st
== IRT_NUM
|| st
== IRT_FLOAT
) {
1160 id
= IRCALL_fp64_d2l
+ ((st
== IRT_FLOAT
) ? 2 : 0) + (dt
- IRT_I64
);
1163 id
= IRCALL_fp64_l2d
+ ((dt
== IRT_FLOAT
) ? 2 : 0) + (st
- IRT_I64
);
1166 #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP
1167 CCallInfo cim
= lj_ir_callinfo
[id
], *ci
= &cim
;
1168 cim
.flags
|= CCI_VARARG
; /* These calls don't use the hard-float ABI! */
1170 const CCallInfo
*ci
= &lj_ir_callinfo
[id
];
1172 asm_setupresult(as
, ir
, ci
);
1173 asm_gencall(as
, ci
, args
);
1178 /* -- Memory references --------------------------------------------------- */
1180 static void asm_newref(ASMState
*as
, IRIns
*ir
)
1182 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_lj_tab_newkey
];
1184 if (ir
->r
== RID_SINK
)
1186 args
[0] = ASMREF_L
; /* lua_State *L */
1187 args
[1] = ir
->op1
; /* GCtab *t */
1188 args
[2] = ASMREF_TMP1
; /* cTValue *key */
1189 asm_setupresult(as
, ir
, ci
); /* TValue * */
1190 asm_gencall(as
, ci
, args
);
1191 asm_tvptr(as
, ra_releasetmp(as
, ASMREF_TMP1
), ir
->op2
);
1194 /* -- Calls --------------------------------------------------------------- */
1196 /* Collect arguments from CALL* and CARG instructions. */
1197 static void asm_collectargs(ASMState
*as
, IRIns
*ir
,
1198 const CCallInfo
*ci
, IRRef
*args
)
1200 uint32_t n
= CCI_XNARGS(ci
);
1201 lua_assert(n
<= CCI_NARGS_MAX
*2); /* Account for split args. */
1202 if ((ci
->flags
& CCI_L
)) { *args
++ = ASMREF_L
; n
--; }
1205 lua_assert(ir
->o
== IR_CARG
);
1206 args
[n
] = ir
->op2
== REF_NIL
? 0 : ir
->op2
;
1208 args
[0] = ir
->op1
== REF_NIL
? 0 : ir
->op1
;
1209 lua_assert(IR(ir
->op1
)->o
!= IR_CARG
);
1212 /* Reconstruct CCallInfo flags for CALLX*. */
1213 static uint32_t asm_callx_flags(ASMState
*as
, IRIns
*ir
)
1216 if (ir
->op1
!= REF_NIL
) { /* Count number of arguments first. */
1217 IRIns
*ira
= IR(ir
->op1
);
1219 while (ira
->o
== IR_CARG
) { nargs
++; ira
= IR(ira
->op1
); }
1222 if (IR(ir
->op2
)->o
== IR_CARG
) { /* Copy calling convention info. */
1223 CTypeID id
= (CTypeID
)IR(IR(ir
->op2
)->op2
)->i
;
1224 CType
*ct
= ctype_get(ctype_ctsG(J2G(as
->J
)), id
);
1225 nargs
|= ((ct
->info
& CTF_VARARG
) ? CCI_VARARG
: 0);
1227 nargs
|= (ctype_cconv(ct
->info
) << CCI_CC_SHIFT
);
1231 return (nargs
| (ir
->t
.irt
<< CCI_OTSHIFT
));
1234 static void asm_callid(ASMState
*as
, IRIns
*ir
, IRCallID id
)
1236 const CCallInfo
*ci
= &lj_ir_callinfo
[id
];
1240 asm_setupresult(as
, ir
, ci
);
1241 asm_gencall(as
, ci
, args
);
1244 static void asm_call(ASMState
*as
, IRIns
*ir
)
1246 IRRef args
[CCI_NARGS_MAX
];
1247 const CCallInfo
*ci
= &lj_ir_callinfo
[ir
->op2
];
1248 asm_collectargs(as
, ir
, ci
, args
);
1249 asm_setupresult(as
, ir
, ci
);
1250 asm_gencall(as
, ci
, args
);
1254 static void asm_fppow(ASMState
*as
, IRIns
*ir
, IRRef lref
, IRRef rref
);
1256 #if !LJ_TARGET_X86ORX64
1257 static void asm_fppow(ASMState
*as
, IRIns
*ir
, IRRef lref
, IRRef rref
)
1259 const CCallInfo
*ci
= &lj_ir_callinfo
[IRCALL_pow
];
1263 asm_setupresult(as
, ir
, ci
);
1264 asm_gencall(as
, ci
, args
);
1268 static int asm_fpjoin_pow(ASMState
*as
, IRIns
*ir
)
1270 IRIns
*irp
= IR(ir
->op1
);
1271 if (irp
== ir
-1 && irp
->o
== IR_MUL
&& !ra_used(irp
)) {
1272 IRIns
*irpp
= IR(irp
->op1
);
1273 if (irpp
== ir
-2 && irpp
->o
== IR_FPMATH
&&
1274 irpp
->op2
== IRFPM_LOG2
&& !ra_used(irpp
)) {
1275 asm_fppow(as
, ir
, irpp
->op1
, irp
->op2
);
1283 /* -- PHI and loop handling ----------------------------------------------- */
1285 /* Break a PHI cycle by renaming to a free register (evict if needed). */
1286 static void asm_phi_break(ASMState
*as
, RegSet blocked
, RegSet blockedby
,
1289 RegSet candidates
= blocked
& allow
;
1290 if (candidates
) { /* If this register file has candidates. */
1291 /* Note: the set for ra_pick cannot be empty, since each register file
1292 ** has some registers never allocated to PHIs.
1294 Reg down
, up
= ra_pick(as
, ~blocked
& allow
); /* Get a free register. */
1295 if (candidates
& ~blockedby
) /* Optimize shifts, else it's a cycle. */
1296 candidates
= candidates
& ~blockedby
;
1297 down
= rset_picktop(candidates
); /* Pick candidate PHI register. */
1298 ra_rename(as
, down
, up
); /* And rename it to the free register. */
1302 /* PHI register shuffling.
1304 ** The allocator tries hard to preserve PHI register assignments across
1305 ** the loop body. Most of the time this loop does nothing, since there
1306 ** are no register mismatches.
1308 ** If a register mismatch is detected and ...
1309 ** - the register is currently free: rename it.
1310 ** - the register is blocked by an invariant: restore/remat and rename it.
1311 ** - Otherwise the register is used by another PHI, so mark it as blocked.
1313 ** The renames are order-sensitive, so just retry the loop if a register
1314 ** is marked as blocked, but has been freed in the meantime. A cycle is
1315 ** detected if all of the blocked registers are allocated. To break the
1316 ** cycle rename one of them to a free register and retry.
1318 ** Note that PHI spill slots are kept in sync and don't need to be shuffled.
1320 static void asm_phi_shuffle(ASMState
*as
)
1324 /* Find and resolve PHI register mismatches. */
1326 RegSet blocked
= RSET_EMPTY
;
1327 RegSet blockedby
= RSET_EMPTY
;
1328 RegSet phiset
= as
->phiset
;
1329 while (phiset
) { /* Check all left PHI operand registers. */
1330 Reg r
= rset_pickbot(phiset
);
1331 IRIns
*irl
= IR(as
->phireg
[r
]);
1333 if (r
!= left
) { /* Mismatch? */
1334 if (!rset_test(as
->freeset
, r
)) { /* PHI register blocked? */
1335 IRRef ref
= regcost_ref(as
->cost
[r
]);
1336 /* Blocked by other PHI (w/reg)? */
1337 if (!ra_iskref(ref
) && irt_ismarked(IR(ref
)->t
)) {
1338 rset_set(blocked
, r
);
1339 if (ra_hasreg(left
))
1340 rset_set(blockedby
, left
);
1342 } else { /* Otherwise grab register from invariant. */
1343 ra_restore(as
, ref
);
1347 if (ra_hasreg(left
)) {
1348 ra_rename(as
, left
, r
);
1352 rset_clear(phiset
, r
);
1354 if (!blocked
) break; /* Finished. */
1355 if (!(as
->freeset
& blocked
)) { /* Break cycles if none are free. */
1356 asm_phi_break(as
, blocked
, blockedby
, RSET_GPR
);
1357 if (!LJ_SOFTFP
) asm_phi_break(as
, blocked
, blockedby
, RSET_FPR
);
1359 } /* Else retry some more renames. */
1362 /* Restore/remat invariants whose registers are modified inside the loop. */
1364 work
= as
->modset
& ~(as
->freeset
| as
->phiset
) & RSET_FPR
;
1366 Reg r
= rset_pickbot(work
);
1367 ra_restore(as
, regcost_ref(as
->cost
[r
]));
1368 rset_clear(work
, r
);
1372 work
= as
->modset
& ~(as
->freeset
| as
->phiset
);
1374 Reg r
= rset_pickbot(work
);
1375 ra_restore(as
, regcost_ref(as
->cost
[r
]));
1376 rset_clear(work
, r
);
1380 /* Allocate and save all unsaved PHI regs and clear marks. */
1383 Reg r
= rset_picktop(work
);
1384 IRRef lref
= as
->phireg
[r
];
1385 IRIns
*ir
= IR(lref
);
1386 if (ra_hasspill(ir
->s
)) { /* Left PHI gained a spill slot? */
1387 irt_clearmark(ir
->t
); /* Handled here, so clear marker now. */
1388 ra_alloc1(as
, lref
, RID2RSET(r
));
1389 ra_save(as
, ir
, r
); /* Save to spill slot inside the loop. */
1392 rset_clear(work
, r
);
1396 /* Copy unsynced left/right PHI spill slots. Rarely needed. */
1397 static void asm_phi_copyspill(ASMState
*as
)
1401 for (ir
= IR(as
->orignins
-1); ir
->o
== IR_PHI
; ir
--)
1402 if (ra_hasspill(ir
->s
) && ra_hasspill(IR(ir
->op1
)->s
))
1403 need
|= irt_isfp(ir
->t
) ? 2 : 1; /* Unsynced spill slot? */
1404 if ((need
& 1)) { /* Copy integer spill slots. */
1405 #if !LJ_TARGET_X86ORX64
1409 if ((as
->freeset
& RSET_GPR
))
1410 r
= rset_pickbot((as
->freeset
& RSET_GPR
));
1412 emit_spload(as
, IR(regcost_ref(as
->cost
[r
])), r
, SPOFS_TMP
);
1414 for (ir
= IR(as
->orignins
-1); ir
->o
== IR_PHI
; ir
--) {
1415 if (ra_hasspill(ir
->s
)) {
1416 IRIns
*irl
= IR(ir
->op1
);
1417 if (ra_hasspill(irl
->s
) && !irt_isfp(ir
->t
)) {
1418 emit_spstore(as
, irl
, r
, sps_scale(irl
->s
));
1419 emit_spload(as
, ir
, r
, sps_scale(ir
->s
));
1424 #if LJ_TARGET_X86ORX64
1425 if (!rset_test(as
->freeset
, r
))
1426 emit_spstore(as
, IR(regcost_ref(as
->cost
[r
])), r
, SPOFS_TMP
);
1430 if ((need
& 2)) { /* Copy FP spill slots. */
1436 if ((as
->freeset
& RSET_FPR
))
1437 r
= rset_pickbot((as
->freeset
& RSET_FPR
));
1438 if (!rset_test(as
->freeset
, r
))
1439 emit_spload(as
, IR(regcost_ref(as
->cost
[r
])), r
, SPOFS_TMP
);
1440 for (ir
= IR(as
->orignins
-1); ir
->o
== IR_PHI
; ir
--) {
1441 if (ra_hasspill(ir
->s
)) {
1442 IRIns
*irl
= IR(ir
->op1
);
1443 if (ra_hasspill(irl
->s
) && irt_isfp(ir
->t
)) {
1444 emit_spstore(as
, irl
, r
, sps_scale(irl
->s
));
1445 emit_spload(as
, ir
, r
, sps_scale(ir
->s
));
1450 if (!rset_test(as
->freeset
, r
))
1451 emit_spstore(as
, IR(regcost_ref(as
->cost
[r
])), r
, SPOFS_TMP
);
1456 /* Emit renames for left PHIs which are only spilled outside the loop. */
1457 static void asm_phi_fixup(ASMState
*as
)
1459 RegSet work
= as
->phiset
;
1461 Reg r
= rset_picktop(work
);
1462 IRRef lref
= as
->phireg
[r
];
1463 IRIns
*ir
= IR(lref
);
1464 /* Left PHI gained a spill slot before the loop? */
1465 if (irt_ismarked(ir
->t
) && ra_hasspill(ir
->s
)) {
1467 lj_ir_set(as
->J
, IRT(IR_RENAME
, IRT_NIL
), lref
, as
->loopsnapno
);
1468 ren
= tref_ref(lj_ir_emit(as
->J
));
1469 as
->ir
= as
->T
->ir
; /* The IR may have been reallocated. */
1470 IR(ren
)->r
= (uint8_t)r
;
1471 IR(ren
)->s
= SPS_NONE
;
1473 irt_clearmark(ir
->t
); /* Always clear marker. */
1474 rset_clear(work
, r
);
1478 /* Setup right PHI reference. */
1479 static void asm_phi(ASMState
*as
, IRIns
*ir
)
1481 RegSet allow
= ((!LJ_SOFTFP
&& irt_isfp(ir
->t
)) ? RSET_FPR
: RSET_GPR
) &
1483 RegSet afree
= (as
->freeset
& allow
);
1484 IRIns
*irl
= IR(ir
->op1
);
1485 IRIns
*irr
= IR(ir
->op2
);
1486 if (ir
->r
== RID_SINK
) /* Sink PHI. */
1488 /* Spill slot shuffling is not implemented yet (but rarely needed). */
1489 if (ra_hasspill(irl
->s
) || ra_hasspill(irr
->s
))
1490 lj_trace_err(as
->J
, LJ_TRERR_NYIPHI
);
1491 /* Leave at least one register free for non-PHIs (and PHI cycle breaking). */
1492 if ((afree
& (afree
-1))) { /* Two or more free registers? */
1494 if (ra_noreg(irr
->r
)) { /* Get a register for the right PHI. */
1495 r
= ra_allocref(as
, ir
->op2
, allow
);
1496 } else { /* Duplicate right PHI, need a copy (rare). */
1497 r
= ra_scratch(as
, allow
);
1498 emit_movrr(as
, irr
, r
, irr
->r
);
1501 rset_set(as
->phiset
, r
);
1502 as
->phireg
[r
] = (IRRef1
)ir
->op1
;
1503 irt_setmark(irl
->t
); /* Marks left PHIs _with_ register. */
1504 if (ra_noreg(irl
->r
))
1505 ra_sethint(irl
->r
, r
); /* Set register hint for left PHI. */
1506 } else { /* Otherwise allocate a spill slot. */
1507 /* This is overly restrictive, but it triggers only on synthetic code. */
1508 if (ra_hasreg(irl
->r
) || ra_hasreg(irr
->r
))
1509 lj_trace_err(as
->J
, LJ_TRERR_NYIPHI
);
1511 irr
->s
= ir
->s
; /* Set right PHI spill slot. Sync left slot later. */
1515 static void asm_loop_fixup(ASMState
*as
);
1517 /* Middle part of a loop. */
1518 static void asm_loop(ASMState
*as
)
1521 /* LOOP is a guard, so the snapno is up to date. */
1522 as
->loopsnapno
= as
->snapno
;
1525 /* LOOP marks the transition from the variant to the invariant part. */
1526 as
->flagmcp
= as
->invmcp
= NULL
;
1528 if (!neverfuse(as
)) as
->fuseref
= 0;
1529 asm_phi_shuffle(as
);
1531 asm_phi_copyspill(as
);
1533 as
->mcloop
= as
->mcp
;
1534 RA_DBGX((as
, "===== LOOP ====="));
1535 if (!as
->realign
) RA_DBG_FLUSH();
1536 if (as
->mcp
!= mcspill
)
1537 emit_jmp(as
, mcspill
);
1540 /* -- Target-specific assembler ------------------------------------------- */
1542 #if LJ_TARGET_X86ORX64
1543 #include "lj_asm_x86.h"
1545 #include "lj_asm_arm.h"
1547 #include "lj_asm_ppc.h"
1548 #elif LJ_TARGET_MIPS
1549 #include "lj_asm_mips.h"
1551 #error "Missing assembler for target CPU"
1554 /* -- Instruction dispatch ------------------------------------------------ */
1556 /* Assemble a single instruction. */
1557 static void asm_ir(ASMState
*as
, IRIns
*ir
)
1559 switch ((IROp
)ir
->o
) {
1560 /* Miscellaneous ops. */
1561 case IR_LOOP
: asm_loop(as
); break;
1562 case IR_NOP
: case IR_XBAR
: lua_assert(!ra_used(ir
)); break;
1564 ra_alloc1(as
, ir
->op1
, irt_isfp(ir
->t
) ? RSET_FPR
: RSET_GPR
); break;
1565 case IR_PHI
: asm_phi(as
, ir
); break;
1566 case IR_HIOP
: asm_hiop(as
, ir
); break;
1567 case IR_GCSTEP
: asm_gcstep(as
, ir
); break;
1568 case IR_PROF
: asm_prof(as
, ir
); break;
1570 /* Guarded assertions. */
1571 case IR_LT
: case IR_GE
: case IR_LE
: case IR_GT
:
1572 case IR_ULT
: case IR_UGE
: case IR_ULE
: case IR_UGT
:
1576 case IR_EQ
: case IR_NE
:
1577 if ((ir
-1)->o
== IR_HREF
&& ir
->op1
== as
->curins
-1) {
1579 asm_href(as
, ir
-1, (IROp
)ir
->o
);
1585 case IR_RETF
: asm_retf(as
, ir
); break;
1588 case IR_BNOT
: asm_bnot(as
, ir
); break;
1589 case IR_BSWAP
: asm_bswap(as
, ir
); break;
1590 case IR_BAND
: asm_band(as
, ir
); break;
1591 case IR_BOR
: asm_bor(as
, ir
); break;
1592 case IR_BXOR
: asm_bxor(as
, ir
); break;
1593 case IR_BSHL
: asm_bshl(as
, ir
); break;
1594 case IR_BSHR
: asm_bshr(as
, ir
); break;
1595 case IR_BSAR
: asm_bsar(as
, ir
); break;
1596 case IR_BROL
: asm_brol(as
, ir
); break;
1597 case IR_BROR
: asm_bror(as
, ir
); break;
1599 /* Arithmetic ops. */
1600 case IR_ADD
: asm_add(as
, ir
); break;
1601 case IR_SUB
: asm_sub(as
, ir
); break;
1602 case IR_MUL
: asm_mul(as
, ir
); break;
1603 case IR_DIV
: asm_div(as
, ir
); break;
1604 case IR_MOD
: asm_mod(as
, ir
); break;
1605 case IR_POW
: asm_pow(as
, ir
); break;
1606 case IR_NEG
: asm_neg(as
, ir
); break;
1607 case IR_ABS
: asm_abs(as
, ir
); break;
1608 case IR_ATAN2
: asm_atan2(as
, ir
); break;
1609 case IR_LDEXP
: asm_ldexp(as
, ir
); break;
1610 case IR_MIN
: asm_min(as
, ir
); break;
1611 case IR_MAX
: asm_max(as
, ir
); break;
1612 case IR_FPMATH
: asm_fpmath(as
, ir
); break;
1614 /* Overflow-checking arithmetic ops. */
1615 case IR_ADDOV
: asm_addov(as
, ir
); break;
1616 case IR_SUBOV
: asm_subov(as
, ir
); break;
1617 case IR_MULOV
: asm_mulov(as
, ir
); break;
1619 /* Memory references. */
1620 case IR_AREF
: asm_aref(as
, ir
); break;
1621 case IR_HREF
: asm_href(as
, ir
, 0); break;
1622 case IR_HREFK
: asm_hrefk(as
, ir
); break;
1623 case IR_NEWREF
: asm_newref(as
, ir
); break;
1624 case IR_UREFO
: case IR_UREFC
: asm_uref(as
, ir
); break;
1625 case IR_FREF
: asm_fref(as
, ir
); break;
1626 case IR_STRREF
: asm_strref(as
, ir
); break;
1628 /* Loads and stores. */
1629 case IR_ALOAD
: case IR_HLOAD
: case IR_ULOAD
: case IR_VLOAD
:
1630 asm_ahuvload(as
, ir
);
1632 case IR_FLOAD
: asm_fload(as
, ir
); break;
1633 case IR_XLOAD
: asm_xload(as
, ir
); break;
1634 case IR_SLOAD
: asm_sload(as
, ir
); break;
1636 case IR_ASTORE
: case IR_HSTORE
: case IR_USTORE
: asm_ahustore(as
, ir
); break;
1637 case IR_FSTORE
: asm_fstore(as
, ir
); break;
1638 case IR_XSTORE
: asm_xstore(as
, ir
); break;
1641 case IR_SNEW
: case IR_XSNEW
: asm_snew(as
, ir
); break;
1642 case IR_TNEW
: asm_tnew(as
, ir
); break;
1643 case IR_TDUP
: asm_tdup(as
, ir
); break;
1644 case IR_CNEW
: case IR_CNEWI
: asm_cnew(as
, ir
); break;
1646 /* Buffer operations. */
1647 case IR_BUFHDR
: asm_bufhdr(as
, ir
); break;
1648 case IR_BUFPUT
: asm_bufput(as
, ir
); break;
1649 case IR_BUFSTR
: asm_bufstr(as
, ir
); break;
1651 /* Write barriers. */
1652 case IR_TBAR
: asm_tbar(as
, ir
); break;
1653 case IR_OBAR
: asm_obar(as
, ir
); break;
1655 /* Type conversions. */
1656 case IR_TOBIT
: asm_tobit(as
, ir
); break;
1657 case IR_CONV
: asm_conv(as
, ir
); break;
1658 case IR_TOSTR
: asm_tostr(as
, ir
); break;
1659 case IR_STRTO
: asm_strto(as
, ir
); break;
1665 case IR_CALLN
: case IR_CALLL
: case IR_CALLS
: asm_call(as
, ir
); break;
1666 case IR_CALLXS
: asm_callx(as
, ir
); break;
1667 case IR_CARG
: break;
1670 setintV(&as
->J
->errinfo
, ir
->o
);
1671 lj_trace_err_info(as
->J
, LJ_TRERR_NYIIR
);
1676 /* -- Head of trace ------------------------------------------------------- */
1678 /* Head of a root trace. */
1679 static void asm_head_root(ASMState
*as
)
1682 asm_head_root_base(as
);
1683 emit_setvmstate(as
, (int32_t)as
->T
->traceno
);
1684 spadj
= asm_stack_adjust(as
);
1685 as
->T
->spadjust
= (uint16_t)spadj
;
1686 emit_spsub(as
, spadj
);
1687 /* Root traces assume a checked stack for the starting proto. */
1688 as
->T
->topslot
= gcref(as
->T
->startpt
)->pt
.framesize
;
1691 /* Head of a side trace.
1693 ** The current simplistic algorithm requires that all slots inherited
1694 ** from the parent are live in a register between pass 2 and pass 3. This
1695 ** avoids the complexity of stack slot shuffling. But of course this may
1696 ** overflow the register set in some cases and cause the dreaded error:
1697 ** "NYI: register coalescing too complex". A refined algorithm is needed.
1699 static void asm_head_side(ASMState
*as
)
1701 IRRef1 sloadins
[RID_MAX
];
1702 RegSet allow
= RSET_ALL
; /* Inverse of all coalesced registers. */
1703 RegSet live
= RSET_EMPTY
; /* Live parent registers. */
1704 IRIns
*irp
= &as
->parent
->ir
[REF_BASE
]; /* Parent base. */
1705 int32_t spadj
, spdelta
;
1710 allow
= asm_head_side_base(as
, irp
, allow
);
1712 /* Scan all parent SLOADs and collect register dependencies. */
1713 for (i
= as
->stopins
; i
> REF_BASE
; i
--) {
1716 lua_assert((ir
->o
== IR_SLOAD
&& (ir
->op2
& IRSLOAD_PARENT
)) ||
1717 (LJ_SOFTFP
&& ir
->o
== IR_HIOP
) || ir
->o
== IR_PVAL
);
1718 rs
= as
->parentmap
[i
- REF_FIRST
];
1719 if (ra_hasreg(ir
->r
)) {
1720 rset_clear(allow
, ir
->r
);
1721 if (ra_hasspill(ir
->s
)) {
1722 ra_save(as
, ir
, ir
->r
);
1725 } else if (ra_hasspill(ir
->s
)) {
1729 if (ir
->r
== rs
) { /* Coalesce matching registers right now. */
1731 } else if (ra_hasspill(regsp_spill(rs
))) {
1732 if (ra_hasreg(ir
->r
))
1734 } else if (ra_used(ir
)) {
1735 sloadins
[rs
] = (IRRef1
)i
;
1736 rset_set(live
, rs
); /* Block live parent register. */
1740 /* Calculate stack frame adjustment. */
1741 spadj
= asm_stack_adjust(as
);
1742 spdelta
= spadj
- (int32_t)as
->parent
->spadjust
;
1743 if (spdelta
< 0) { /* Don't shrink the stack frame. */
1744 spadj
= (int32_t)as
->parent
->spadjust
;
1747 as
->T
->spadjust
= (uint16_t)spadj
;
1749 /* Reload spilled target registers. */
1751 for (i
= as
->stopins
; i
> REF_BASE
; i
--) {
1753 if (irt_ismarked(ir
->t
)) {
1757 irt_clearmark(ir
->t
);
1758 rs
= as
->parentmap
[i
- REF_FIRST
];
1759 if (!ra_hasspill(regsp_spill(rs
)))
1760 ra_sethint(ir
->r
, rs
); /* Hint may be gone, set it again. */
1761 else if (sps_scale(regsp_spill(rs
))+spdelta
== sps_scale(ir
->s
))
1762 continue; /* Same spill slot, do nothing. */
1763 mask
= ((!LJ_SOFTFP
&& irt_isfp(ir
->t
)) ? RSET_FPR
: RSET_GPR
) & allow
;
1764 if (mask
== RSET_EMPTY
)
1765 lj_trace_err(as
->J
, LJ_TRERR_NYICOAL
);
1766 r
= ra_allocref(as
, i
, mask
);
1768 rset_clear(allow
, r
);
1769 if (r
== rs
) { /* Coalesce matching registers right now. */
1771 rset_clear(live
, r
);
1772 } else if (ra_hasspill(regsp_spill(rs
))) {
1780 /* Store trace number and adjust stack frame relative to the parent. */
1781 emit_setvmstate(as
, (int32_t)as
->T
->traceno
);
1782 emit_spsub(as
, spdelta
);
1784 #if !LJ_TARGET_X86ORX64
1785 /* Restore BASE register from parent spill slot. */
1786 if (ra_hasspill(irp
->s
))
1787 emit_spload(as
, IR(REF_BASE
), IR(REF_BASE
)->r
, sps_scale(irp
->s
));
1790 /* Restore target registers from parent spill slots. */
1792 RegSet work
= ~as
->freeset
& RSET_ALL
;
1794 Reg r
= rset_pickbot(work
);
1795 IRRef ref
= regcost_ref(as
->cost
[r
]);
1796 RegSP rs
= as
->parentmap
[ref
- REF_FIRST
];
1797 rset_clear(work
, r
);
1798 if (ra_hasspill(regsp_spill(rs
))) {
1799 int32_t ofs
= sps_scale(regsp_spill(rs
));
1801 emit_spload(as
, IR(ref
), r
, ofs
);
1807 /* Shuffle registers to match up target regs with parent regs. */
1811 /* Repeatedly coalesce free live registers by moving to their target. */
1812 while ((work
= as
->freeset
& live
) != RSET_EMPTY
) {
1813 Reg rp
= rset_pickbot(work
);
1814 IRIns
*ir
= IR(sloadins
[rp
]);
1815 rset_clear(live
, rp
);
1816 rset_clear(allow
, rp
);
1818 emit_movrr(as
, ir
, ir
->r
, rp
);
1822 /* We're done if no live registers remain. */
1823 if (live
== RSET_EMPTY
)
1826 /* Break cycles by renaming one target to a temp. register. */
1827 if (live
& RSET_GPR
) {
1828 RegSet tmpset
= as
->freeset
& ~live
& allow
& RSET_GPR
;
1829 if (tmpset
== RSET_EMPTY
)
1830 lj_trace_err(as
->J
, LJ_TRERR_NYICOAL
);
1831 ra_rename(as
, rset_pickbot(live
& RSET_GPR
), rset_pickbot(tmpset
));
1833 if (!LJ_SOFTFP
&& (live
& RSET_FPR
)) {
1834 RegSet tmpset
= as
->freeset
& ~live
& allow
& RSET_FPR
;
1835 if (tmpset
== RSET_EMPTY
)
1836 lj_trace_err(as
->J
, LJ_TRERR_NYICOAL
);
1837 ra_rename(as
, rset_pickbot(live
& RSET_FPR
), rset_pickbot(tmpset
));
1840 /* Continue with coalescing to fix up the broken cycle(s). */
1843 /* Inherit top stack slot already checked by parent trace. */
1844 as
->T
->topslot
= as
->parent
->topslot
;
1845 if (as
->topslot
> as
->T
->topslot
) { /* Need to check for higher slot? */
1846 #ifdef EXITSTATE_CHECKEXIT
1847 /* Highest exit + 1 indicates stack check. */
1848 ExitNo exitno
= as
->T
->nsnap
;
1850 /* Reuse the parent exit in the context of the parent trace. */
1851 ExitNo exitno
= as
->J
->exitno
;
1853 as
->T
->topslot
= (uint8_t)as
->topslot
; /* Remember for child traces. */
1854 asm_stack_check(as
, as
->topslot
, irp
, allow
& RSET_GPR
, exitno
);
1858 /* -- Tail of trace ------------------------------------------------------- */
1860 /* Get base slot for a snapshot. */
1861 static BCReg
asm_baseslot(ASMState
*as
, SnapShot
*snap
, int *gotframe
)
1863 SnapEntry
*map
= &as
->T
->snapmap
[snap
->mapofs
];
1865 for (n
= snap
->nent
; n
> 0; n
--) {
1866 SnapEntry sn
= map
[n
-1];
1867 if ((sn
& SNAP_FRAME
)) {
1869 return snap_slot(sn
);
1875 /* Link to another trace. */
1876 static void asm_tail_link(ASMState
*as
)
1878 SnapNo snapno
= as
->T
->nsnap
-1; /* Last snapshot. */
1879 SnapShot
*snap
= &as
->T
->snap
[snapno
];
1881 BCReg baseslot
= asm_baseslot(as
, snap
, &gotframe
);
1883 as
->topslot
= snap
->topslot
;
1885 ra_allocref(as
, REF_BASE
, RID2RSET(RID_BASE
));
1887 if (as
->T
->link
== 0) {
1888 /* Setup fixed registers for exit to interpreter. */
1889 const BCIns
*pc
= snap_pc(as
->T
->snapmap
[snap
->mapofs
+ snap
->nent
]);
1891 if (bc_op(*pc
) == BC_JLOOP
) { /* NYI: find a better way to do this. */
1892 BCIns
*retpc
= &traceref(as
->J
, bc_d(*pc
))->startins
;
1893 if (bc_isret(bc_op(*retpc
)))
1896 ra_allockreg(as
, i32ptr(J2GG(as
->J
)->dispatch
), RID_DISPATCH
);
1897 ra_allockreg(as
, i32ptr(pc
), RID_LPC
);
1898 mres
= (int32_t)(snap
->nslots
- baseslot
);
1899 switch (bc_op(*pc
)) {
1900 case BC_CALLM
: case BC_CALLMT
:
1901 mres
-= (int32_t)(1 + bc_a(*pc
) + bc_c(*pc
)); break;
1902 case BC_RETM
: mres
-= (int32_t)(bc_a(*pc
) + bc_d(*pc
)); break;
1903 case BC_TSETM
: mres
-= (int32_t)bc_a(*pc
); break;
1904 default: if (bc_op(*pc
) < BC_FUNCF
) mres
= 0; break;
1906 ra_allockreg(as
, mres
, RID_RET
); /* Return MULTRES or 0. */
1907 } else if (baseslot
) {
1908 /* Save modified BASE for linking to trace with higher start frame. */
1909 emit_setgl(as
, RID_BASE
, jit_base
);
1911 emit_addptr(as
, RID_BASE
, 8*(int32_t)baseslot
);
1913 /* Sync the interpreter state with the on-trace state. */
1914 asm_stack_restore(as
, snap
);
1916 /* Root traces that add frames need to check the stack at the end. */
1917 if (!as
->parent
&& gotframe
)
1918 asm_stack_check(as
, as
->topslot
, NULL
, as
->freeset
& RSET_GPR
, snapno
);
1921 /* -- Trace setup --------------------------------------------------------- */
1923 /* Clear reg/sp for all instructions and add register hints. */
1924 static void asm_setup_regsp(ASMState
*as
)
1927 int sink
= T
->sinktags
;
1928 IRRef nins
= T
->nins
;
1932 uint32_t rload
= 0xa6402a64;
1937 /* Clear reg/sp for constants. */
1938 for (ir
= IR(T
->nk
), lastir
= IR(REF_BASE
); ir
< lastir
; ir
++)
1939 ir
->prev
= REGSP_INIT
;
1941 /* REF_BASE is used for implicit references to the BASE register. */
1942 lastir
->prev
= REGSP_HINT(RID_BASE
);
1945 if (ir
->o
== IR_RENAME
) {
1946 do { ir
--; nins
--; } while (ir
->o
== IR_RENAME
);
1947 T
->nins
= nins
; /* Remove any renames left over from ASM restart. */
1949 as
->snaprename
= nins
;
1951 as
->snapno
= T
->nsnap
;
1953 as
->stopins
= REF_BASE
;
1954 as
->orignins
= nins
;
1957 /* Setup register hints for parent link instructions. */
1961 lastir
= lj_snap_regspmap(as
->parent
, as
->J
->exitno
, ir
);
1962 if (lastir
- ir
> LJ_MAX_JSLOTS
)
1963 lj_trace_err(as
->J
, LJ_TRERR_NYICOAL
);
1964 as
->stopins
= (IRRef
)((lastir
-1) - as
->ir
);
1965 for (p
= as
->parentmap
; ir
< lastir
; ir
++) {
1966 RegSP rs
= ir
->prev
;
1967 *p
++ = (uint16_t)rs
; /* Copy original parent RegSP to parentmap. */
1968 if (!ra_hasspill(regsp_spill(rs
)))
1969 ir
->prev
= (uint16_t)REGSP_HINT(regsp_reg(rs
));
1971 ir
->prev
= REGSP_INIT
;
1976 as
->evenspill
= SPS_FIRST
;
1977 for (lastir
= IR(nins
); ir
< lastir
; ir
++) {
1979 if (ir
->r
== RID_SINK
)
1981 if (ir
->r
== RID_SUNK
) { /* Revert after ASM restart. */
1992 if (!((ir
->op2
& IRSLOAD_TYPECHECK
) || (ir
+1)->o
== IR_HIOP
))
1995 case IR_ALOAD
: case IR_HLOAD
: case IR_ULOAD
: case IR_VLOAD
:
1996 if (!LJ_SOFTFP
&& irt_isnum(ir
->t
)) break;
1997 ir
->prev
= (uint16_t)REGSP_HINT((rload
& 15));
1998 rload
= lj_ror(rload
, 4);
2003 ci
.flags
= asm_callx_flags(as
, ir
);
2004 ir
->prev
= asm_setup_call_slots(as
, ir
, &ci
);
2006 as
->modset
|= RSET_SCRATCH
;
2009 case IR_CALLN
: case IR_CALLL
: case IR_CALLS
: {
2010 const CCallInfo
*ci
= &lj_ir_callinfo
[ir
->op2
];
2011 ir
->prev
= asm_setup_call_slots(as
, ir
, ci
);
2013 as
->modset
|= (ci
->flags
& CCI_NOFPRCLOBBER
) ?
2014 (RSET_SCRATCH
& ~RSET_FPR
) : RSET_SCRATCH
;
2017 #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
2019 switch ((ir
-1)->o
) {
2020 #if LJ_SOFTFP && LJ_TARGET_ARM
2021 case IR_SLOAD
: case IR_ALOAD
: case IR_HLOAD
: case IR_ULOAD
: case IR_VLOAD
:
2022 if (ra_hashint((ir
-1)->r
)) {
2023 ir
->prev
= (ir
-1)->prev
+ 1;
2028 #if !LJ_SOFTFP && LJ_NEED_FP64
2030 if (irt_isfp((ir
-1)->t
)) {
2031 ir
->prev
= REGSP_HINT(RID_FPRET
);
2036 case IR_CALLN
: case IR_CALLXS
:
2038 case IR_MIN
: case IR_MAX
:
2040 (ir
-1)->prev
= REGSP_HINT(RID_RETLO
);
2041 ir
->prev
= REGSP_HINT(RID_RETHI
);
2049 case IR_MIN
: case IR_MAX
:
2050 if ((ir
+1)->o
!= IR_HIOP
) break;
2053 /* C calls evict all scratch regs and return results in RID_RET. */
2054 case IR_SNEW
: case IR_XSNEW
: case IR_NEWREF
: case IR_BUFPUT
:
2055 if (REGARG_NUMGPR
< 3 && as
->evenspill
< 3)
2056 as
->evenspill
= 3; /* lj_str_new and lj_tab_newkey need 3 args. */
2057 #if LJ_TARGET_X86 && LJ_HASFFI
2060 if (ir
->op2
!= REF_NIL
&& as
->evenspill
< 4)
2061 as
->evenspill
= 4; /* lj_cdata_newv needs 4 args. */
2066 case IR_TNEW
: case IR_TDUP
: case IR_CNEWI
: case IR_TOSTR
:
2068 ir
->prev
= REGSP_HINT(RID_RET
);
2070 as
->modset
= RSET_SCRATCH
;
2072 case IR_STRTO
: case IR_OBAR
:
2074 as
->modset
= RSET_SCRATCH
;
2076 #if !LJ_TARGET_X86ORX64 && !LJ_SOFTFP
2077 case IR_ATAN2
: case IR_LDEXP
:
2080 if (!LJ_SOFTFP
&& irt_isnum(ir
->t
)) {
2081 #if LJ_TARGET_X86ORX64
2082 ir
->prev
= REGSP_HINT(RID_XMM0
);
2084 as
->modset
|= RSET_RANGE(RID_XMM0
, RID_XMM1
+1)|RID2RSET(RID_EAX
);
2086 ir
->prev
= REGSP_HINT(RID_FPRET
);
2088 as
->modset
|= RSET_SCRATCH
;
2092 /* fallthrough for integer POW */
2093 case IR_DIV
: case IR_MOD
:
2094 if (!irt_isnum(ir
->t
)) {
2095 ir
->prev
= REGSP_HINT(RID_RET
);
2097 as
->modset
|= (RSET_SCRATCH
& RSET_GPR
);
2102 #if LJ_TARGET_X86ORX64
2103 if (ir
->op2
== IRFPM_EXP2
) { /* May be joined to pow. */
2104 ir
->prev
= REGSP_HINT(RID_XMM0
);
2106 if (as
->evenspill
< 4) /* Leave room for 16 byte scratch area. */
2110 as
->modset
|= RSET_RANGE(RID_XMM0
, RID_XMM2
+1)|RID2RSET(RID_EAX
);
2112 } else if (ir
->op2
<= IRFPM_TRUNC
&& !(as
->flags
& JIT_F_SSE4_1
)) {
2113 ir
->prev
= REGSP_HINT(RID_XMM0
);
2115 as
->modset
|= RSET_RANGE(RID_XMM0
, RID_XMM3
+1)|RID2RSET(RID_EAX
);
2120 ir
->prev
= REGSP_HINT(RID_FPRET
);
2122 as
->modset
|= RSET_SCRATCH
;
2125 #if LJ_TARGET_X86ORX64
2126 /* Non-constant shift counts need to be in RID_ECX on x86/x64. */
2127 case IR_BSHL
: case IR_BSHR
: case IR_BSAR
: case IR_BROL
: case IR_BROR
:
2128 if (!irref_isk(ir
->op2
) && !ra_hashint(IR(ir
->op2
)->r
)) {
2129 IR(ir
->op2
)->r
= REGSP_HINT(RID_ECX
);
2131 rset_set(as
->modset
, RID_ECX
);
2135 /* Do not propagate hints across type conversions or loads. */
2139 case IR_ALOAD
: case IR_HLOAD
: case IR_ULOAD
: case IR_VLOAD
:
2143 if (irt_isfp(ir
->t
) || (ir
->op2
& IRCONV_SRCMASK
) == IRT_NUM
||
2144 (ir
->op2
& IRCONV_SRCMASK
) == IRT_FLOAT
)
2148 /* Propagate hints across likely 'op reg, imm' or 'op reg'. */
2149 if (irref_isk(ir
->op2
) && !irref_isk(ir
->op1
) &&
2150 ra_hashint(regsp_reg(IR(ir
->op1
)->prev
))) {
2151 ir
->prev
= IR(ir
->op1
)->prev
;
2156 ir
->prev
= REGSP_INIT
;
2158 if ((as
->evenspill
& 1))
2159 as
->oddspill
= as
->evenspill
++;
2164 /* -- Assembler core ------------------------------------------------------ */
2166 /* Assemble a trace. */
2167 void lj_asm_trace(jit_State
*J
, GCtrace
*T
)
2170 ASMState
*as
= &as_
;
2173 /* Ensure an initialized instruction beyond the last one for HIOP checks. */
2174 J
->cur
.nins
= lj_ir_nextins(J
);
2175 J
->cur
.ir
[J
->cur
.nins
].o
= IR_NOP
;
2177 /* Setup initial state. Copy some fields to reduce indirections. */
2181 as
->flags
= J
->flags
;
2182 as
->loopref
= J
->loopref
;
2185 as
->parent
= J
->parent
? traceref(J
, J
->parent
) : NULL
;
2187 /* Reserve MCode memory. */
2188 as
->mctop
= origtop
= lj_mcode_reserve(J
, &as
->mcbot
);
2189 as
->mcp
= as
->mctop
;
2190 as
->mclim
= as
->mcbot
+ MCLIM_REDZONE
;
2191 asm_setup_target(as
);
2194 as
->mcp
= as
->mctop
;
2195 #ifdef LUA_USE_ASSERT
2196 as
->mcp_prev
= as
->mcp
;
2198 as
->curins
= T
->nins
;
2200 RA_DBGX((as
, "===== STOP ====="));
2202 /* General trace setup. Emit tail of trace. */
2208 as
->sectref
= as
->loopref
;
2209 as
->fuseref
= (as
->flags
& JIT_F_OPT_FUSE
) ? as
->loopref
: FUSE_DISABLED
;
2210 asm_setup_regsp(as
);
2214 /* Assemble a trace in linear backwards order. */
2215 for (as
->curins
--; as
->curins
> as
->stopins
; as
->curins
--) {
2216 IRIns
*ir
= IR(as
->curins
);
2217 lua_assert(!(LJ_32
&& irt_isint64(ir
->t
))); /* Handled by SPLIT. */
2218 if (!ra_used(ir
) && !ir_sideeff(ir
) && (as
->flags
& JIT_F_OPT_DCE
))
2219 continue; /* Dead-code elimination can be soooo easy. */
2220 if (irt_isguard(ir
->t
))
2226 } while (as
->realign
); /* Retry in case the MCode needs to be realigned. */
2228 /* Emit head of trace. */
2231 if (as
->gcsteps
> 0) {
2232 as
->curins
= as
->T
->snap
[0].ref
;
2233 asm_snap_prep(as
); /* The GC check is a guard. */
2243 RA_DBGX((as
, "===== START ===="));
2245 if (as
->freeset
!= RSET_ALL
)
2246 lj_trace_err(as
->J
, LJ_TRERR_BADRA
); /* Ouch! Should never happen. */
2248 /* Set trace entry point before fixing up tail to allow link to self. */
2250 T
->mcloop
= as
->mcloop
? (MSize
)((char *)as
->mcloop
- (char *)as
->mcp
) : 0;
2252 asm_tail_fixup(as
, T
->link
); /* Note: this may change as->mctop! */
2253 T
->szmcode
= (MSize
)((char *)as
->mctop
- (char *)as
->mcp
);
2254 lj_mcode_sync(T
->mcode
, origtop
);