| 1 |
/* |
| 2 |
** IR assembler (SSA IR -> machine code). |
| 3 |
** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h |
| 4 |
*/ |
| 5 |
|
| 6 |
#define lj_asm_c |
| 7 |
#define LUA_CORE |
| 8 |
|
| 9 |
#include "lj_obj.h" |
| 10 |
|
| 11 |
#if LJ_HASJIT |
| 12 |
|
| 13 |
#include "lj_gc.h" |
| 14 |
#include "lj_buf.h" |
| 15 |
#include "lj_str.h" |
| 16 |
#include "lj_tab.h" |
| 17 |
#include "lj_frame.h" |
| 18 |
#if LJ_HASFFI |
| 19 |
#include "lj_ctype.h" |
| 20 |
#endif |
| 21 |
#include "lj_ir.h" |
| 22 |
#include "lj_jit.h" |
| 23 |
#include "lj_ircall.h" |
| 24 |
#include "lj_iropt.h" |
| 25 |
#include "lj_mcode.h" |
| 26 |
#include "lj_trace.h" |
| 27 |
#include "lj_snap.h" |
| 28 |
#include "lj_asm.h" |
| 29 |
#include "lj_dispatch.h" |
| 30 |
#include "lj_vm.h" |
| 31 |
#include "lj_target.h" |
| 32 |
#include "lj_prng.h" |
| 33 |
|
| 34 |
#ifdef LUA_USE_ASSERT |
| 35 |
#include <stdio.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
/* -- Assembler state and common macros ----------------------------------- */ |
| 39 |
|
| 40 |
/* Assembler state. */ |
| 41 |
typedef struct ASMState { |
| 42 |
RegCost cost[RID_MAX]; /* Reference and blended allocation cost for regs. */ |
| 43 |
|
| 44 |
MCode *mcp; /* Current MCode pointer (grows down). */ |
| 45 |
MCode *mclim; /* Lower limit for MCode memory + red zone. */ |
| 46 |
#ifdef LUA_USE_ASSERT |
| 47 |
MCode *mcp_prev; /* Red zone overflow check. */ |
| 48 |
#endif |
| 49 |
|
| 50 |
IRIns *ir; /* Copy of pointer to IR instructions/constants. */ |
| 51 |
jit_State *J; /* JIT compiler state. */ |
| 52 |
|
| 53 |
#if LJ_TARGET_X86ORX64 |
| 54 |
x86ModRM mrm; /* Fused x86 address operand. */ |
| 55 |
#endif |
| 56 |
|
| 57 |
RegSet freeset; /* Set of free registers. */ |
| 58 |
RegSet modset; /* Set of registers modified inside the loop. */ |
| 59 |
RegSet weakset; /* Set of weakly referenced registers. */ |
| 60 |
RegSet phiset; /* Set of PHI registers. */ |
| 61 |
|
| 62 |
uint32_t flags; /* Copy of JIT compiler flags. */ |
| 63 |
int loopinv; /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */ |
| 64 |
|
| 65 |
int32_t evenspill; /* Next even spill slot. */ |
| 66 |
int32_t oddspill; /* Next odd spill slot (or 0). */ |
| 67 |
|
| 68 |
IRRef curins; /* Reference of current instruction. */ |
| 69 |
IRRef stopins; /* Stop assembly before hitting this instruction. */ |
| 70 |
IRRef orignins; /* Original T->nins. */ |
| 71 |
|
| 72 |
IRRef snapref; /* Current snapshot is active after this reference. */ |
| 73 |
IRRef snaprename; /* Rename highwater mark for snapshot check. */ |
| 74 |
SnapNo snapno; /* Current snapshot number. */ |
| 75 |
SnapNo loopsnapno; /* Loop snapshot number. */ |
| 76 |
int snapalloc; /* Current snapshot needs allocation. */ |
| 77 |
BloomFilter snapfilt1, snapfilt2; /* Filled with snapshot refs. */ |
| 78 |
|
| 79 |
IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ |
| 80 |
IRRef sectref; /* Section base reference (loopref or 0). */ |
| 81 |
IRRef loopref; /* Reference of LOOP instruction (or 0). */ |
| 82 |
|
| 83 |
BCReg topslot; /* Number of slots for stack check (unless 0). */ |
| 84 |
int32_t gcsteps; /* Accumulated number of GC steps (per section). */ |
| 85 |
|
| 86 |
GCtrace *T; /* Trace to assemble. */ |
| 87 |
GCtrace *parent; /* Parent trace (or NULL). */ |
| 88 |
|
| 89 |
MCode *mcbot; /* Bottom of reserved MCode. */ |
| 90 |
MCode *mctop; /* Top of generated MCode. */ |
| 91 |
MCode *mctoporig; /* Original top of generated MCode. */ |
| 92 |
MCode *mcloop; /* Pointer to loop MCode (or NULL). */ |
| 93 |
MCode *invmcp; /* Points to invertible loop branch (or NULL). */ |
| 94 |
MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ |
| 95 |
MCode *realign; /* Realign loop if not NULL. */ |
| 96 |
|
| 97 |
#ifdef LUAJIT_RANDOM_RA |
| 98 |
/* Randomize register allocation. OK for fuzz testing, not for production. */ |
| 99 |
uint64_t prngbits; |
| 100 |
PRNGState prngstate; |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifdef RID_NUM_KREF |
| 104 |
intptr_t krefk[RID_NUM_KREF]; |
| 105 |
#endif |
| 106 |
IRRef1 phireg[RID_MAX]; /* PHI register references. */ |
| 107 |
uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ |
| 108 |
} ASMState; |
| 109 |
|
| 110 |
#ifdef LUA_USE_ASSERT |
| 111 |
#define lj_assertA(c, ...) lj_assertG_(J2G(as->J), (c), __VA_ARGS__) |
| 112 |
#else |
| 113 |
#define lj_assertA(c, ...) ((void)as) |
| 114 |
#endif |
| 115 |
|
| 116 |
#define IR(ref) (&as->ir[(ref)]) |
| 117 |
|
| 118 |
#define ASMREF_TMP1 REF_TRUE /* Temp. register. */ |
| 119 |
#define ASMREF_TMP2 REF_FALSE /* Temp. register. */ |
| 120 |
#define ASMREF_L REF_NIL /* Stores register for L. */ |
| 121 |
|
| 122 |
/* Check for variant to invariant references. */ |
| 123 |
#define iscrossref(as, ref) ((ref) < as->sectref) |
| 124 |
|
| 125 |
/* Inhibit memory op fusion from variant to invariant references. */ |
| 126 |
#define FUSE_DISABLED (~(IRRef)0) |
| 127 |
#define mayfuse(as, ref) ((ref) > as->fuseref) |
| 128 |
#define neverfuse(as) (as->fuseref == FUSE_DISABLED) |
| 129 |
#define canfuse(as, ir) (!neverfuse(as) && !irt_isphi((ir)->t)) |
| 130 |
#define opisfusableload(o) \ |
| 131 |
((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \ |
| 132 |
(o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD) |
| 133 |
|
| 134 |
/* Sparse limit checks using a red zone before the actual limit. */ |
| 135 |
#define MCLIM_REDZONE 64 |
| 136 |
|
| 137 |
static LJ_NORET LJ_NOINLINE void asm_mclimit(ASMState *as) |
| 138 |
{ |
| 139 |
lj_mcode_limiterr(as->J, (size_t)(as->mctop - as->mcp + 4*MCLIM_REDZONE)); |
| 140 |
} |
| 141 |
|
| 142 |
static LJ_AINLINE void checkmclim(ASMState *as) |
| 143 |
{ |
| 144 |
#ifdef LUA_USE_ASSERT |
| 145 |
if (as->mcp + MCLIM_REDZONE < as->mcp_prev) { |
| 146 |
IRIns *ir = IR(as->curins+1); |
| 147 |
lj_assertA(0, "red zone overflow: %p IR %04d %02d %04d %04d\n", as->mcp, |
| 148 |
as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS); |
| 149 |
} |
| 150 |
#endif |
| 151 |
if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as); |
| 152 |
#ifdef LUA_USE_ASSERT |
| 153 |
as->mcp_prev = as->mcp; |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
#ifdef RID_NUM_KREF |
| 158 |
#define ra_iskref(ref) ((ref) < RID_NUM_KREF) |
| 159 |
#define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) |
| 160 |
#define ra_krefk(as, ref) (as->krefk[(ref)]) |
| 161 |
|
| 162 |
static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, intptr_t k) |
| 163 |
{ |
| 164 |
IRRef ref = (IRRef)(r - RID_MIN_KREF); |
| 165 |
as->krefk[ref] = k; |
| 166 |
as->cost[r] = REGCOST(ref, ref); |
| 167 |
} |
| 168 |
|
| 169 |
#else |
| 170 |
#define ra_iskref(ref) 0 |
| 171 |
#define ra_krefreg(ref) RID_MIN_GPR |
| 172 |
#define ra_krefk(as, ref) 0 |
| 173 |
#endif |
| 174 |
|
| 175 |
/* Arch-specific field offsets. */ |
| 176 |
static const uint8_t field_ofs[IRFL__MAX+1] = { |
| 177 |
#define FLOFS(name, ofs) (uint8_t)(ofs), |
| 178 |
IRFLDEF(FLOFS) |
| 179 |
#undef FLOFS |
| 180 |
0 |
| 181 |
}; |
| 182 |
|
| 183 |
#ifdef LUAJIT_RANDOM_RA |
| 184 |
/* Return a fixed number of random bits from the local PRNG state. */ |
| 185 |
static uint32_t ra_random_bits(ASMState *as, uint32_t nbits) { |
| 186 |
uint64_t b = as->prngbits; |
| 187 |
uint32_t res = (1u << nbits) - 1u; |
| 188 |
if (b <= res) b = lj_prng_u64(&as->prngstate) | (1ull << 63); |
| 189 |
res &= (uint32_t)b; |
| 190 |
as->prngbits = b >> nbits; |
| 191 |
return res; |
| 192 |
} |
| 193 |
|
| 194 |
/* Pick a random register from a register set. */ |
| 195 |
static Reg rset_pickrandom(ASMState *as, RegSet rs) |
| 196 |
{ |
| 197 |
Reg r = rset_pickbot_(rs); |
| 198 |
rs >>= r; |
| 199 |
if (rs > 1) { /* More than one bit set? */ |
| 200 |
while (1) { |
| 201 |
/* We need to sample max. the GPR or FPR half of the set. */ |
| 202 |
uint32_t d = ra_random_bits(as, RSET_BITS-1); |
| 203 |
if ((rs >> d) & 1) { |
| 204 |
r += d; |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
return r; |
| 210 |
} |
| 211 |
#define rset_picktop(rs) rset_pickrandom(as, rs) |
| 212 |
#define rset_pickbot(rs) rset_pickrandom(as, rs) |
| 213 |
#else |
| 214 |
#define rset_picktop(rs) rset_picktop_(rs) |
| 215 |
#define rset_pickbot(rs) rset_pickbot_(rs) |
| 216 |
#endif |
| 217 |
|
| 218 |
/* -- Target-specific instruction emitter --------------------------------- */ |
| 219 |
|
| 220 |
#if LJ_TARGET_X86ORX64 |
| 221 |
#include "lj_emit_x86.h" |
| 222 |
#elif LJ_TARGET_ARM |
| 223 |
#include "lj_emit_arm.h" |
| 224 |
#elif LJ_TARGET_ARM64 |
| 225 |
#include "lj_emit_arm64.h" |
| 226 |
#elif LJ_TARGET_PPC |
| 227 |
#include "lj_emit_ppc.h" |
| 228 |
#elif LJ_TARGET_MIPS |
| 229 |
#include "lj_emit_mips.h" |
| 230 |
#else |
| 231 |
#error "Missing instruction emitter for target CPU" |
| 232 |
#endif |
| 233 |
|
| 234 |
/* Generic load/store of register from/to stack slot. */ |
| 235 |
#define emit_spload(as, ir, r, ofs) \ |
| 236 |
emit_loadofs(as, ir, (r), RID_SP, (ofs)) |
| 237 |
#define emit_spstore(as, ir, r, ofs) \ |
| 238 |
emit_storeofs(as, ir, (r), RID_SP, (ofs)) |
| 239 |
|
| 240 |
/* -- Register allocator debugging ---------------------------------------- */ |
| 241 |
|
| 242 |
/* #define LUAJIT_DEBUG_RA */ |
| 243 |
|
| 244 |
#ifdef LUAJIT_DEBUG_RA |
| 245 |
|
| 246 |
#include <stdio.h> |
| 247 |
#include <stdarg.h> |
| 248 |
|
| 249 |
#define RIDNAME(name) #name, |
| 250 |
static const char *const ra_regname[] = { |
| 251 |
GPRDEF(RIDNAME) |
| 252 |
FPRDEF(RIDNAME) |
| 253 |
VRIDDEF(RIDNAME) |
| 254 |
NULL |
| 255 |
}; |
| 256 |
#undef RIDNAME |
| 257 |
|
| 258 |
static char ra_dbg_buf[65536]; |
| 259 |
static char *ra_dbg_p; |
| 260 |
static char *ra_dbg_merge; |
| 261 |
static MCode *ra_dbg_mcp; |
| 262 |
|
| 263 |
static void ra_dstart(void) |
| 264 |
{ |
| 265 |
ra_dbg_p = ra_dbg_buf; |
| 266 |
ra_dbg_merge = NULL; |
| 267 |
ra_dbg_mcp = NULL; |
| 268 |
} |
| 269 |
|
| 270 |
static void ra_dflush(void) |
| 271 |
{ |
| 272 |
fwrite(ra_dbg_buf, 1, (size_t)(ra_dbg_p-ra_dbg_buf), stdout); |
| 273 |
ra_dstart(); |
| 274 |
} |
| 275 |
|
| 276 |
static void ra_dprintf(ASMState *as, const char *fmt, ...) |
| 277 |
{ |
| 278 |
char *p; |
| 279 |
va_list argp; |
| 280 |
va_start(argp, fmt); |
| 281 |
p = ra_dbg_mcp == as->mcp ? ra_dbg_merge : ra_dbg_p; |
| 282 |
ra_dbg_mcp = NULL; |
| 283 |
p += sprintf(p, "%08x \e[36m%04d ", (uintptr_t)as->mcp, as->curins-REF_BIAS); |
| 284 |
for (;;) { |
| 285 |
const char *e = strchr(fmt, '$'); |
| 286 |
if (e == NULL) break; |
| 287 |
memcpy(p, fmt, (size_t)(e-fmt)); |
| 288 |
p += e-fmt; |
| 289 |
if (e[1] == 'r') { |
| 290 |
Reg r = va_arg(argp, Reg) & RID_MASK; |
| 291 |
if (r <= RID_MAX) { |
| 292 |
const char *q; |
| 293 |
for (q = ra_regname[r]; *q; q++) |
| 294 |
*p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; |
| 295 |
} else { |
| 296 |
*p++ = '?'; |
| 297 |
lj_assertA(0, "bad register %d for debug format \"%s\"", r, fmt); |
| 298 |
} |
| 299 |
} else if (e[1] == 'f' || e[1] == 'i') { |
| 300 |
IRRef ref; |
| 301 |
if (e[1] == 'f') |
| 302 |
ref = va_arg(argp, IRRef); |
| 303 |
else |
| 304 |
ref = va_arg(argp, IRIns *) - as->ir; |
| 305 |
if (ref >= REF_BIAS) |
| 306 |
p += sprintf(p, "%04d", ref - REF_BIAS); |
| 307 |
else |
| 308 |
p += sprintf(p, "K%03d", REF_BIAS - ref); |
| 309 |
} else if (e[1] == 's') { |
| 310 |
uint32_t slot = va_arg(argp, uint32_t); |
| 311 |
p += sprintf(p, "[sp+0x%x]", sps_scale(slot)); |
| 312 |
} else if (e[1] == 'x') { |
| 313 |
p += sprintf(p, "%08x", va_arg(argp, int32_t)); |
| 314 |
} else { |
| 315 |
lj_assertA(0, "bad debug format code"); |
| 316 |
} |
| 317 |
fmt = e+2; |
| 318 |
} |
| 319 |
va_end(argp); |
| 320 |
while (*fmt) |
| 321 |
*p++ = *fmt++; |
| 322 |
*p++ = '\e'; *p++ = '['; *p++ = 'm'; *p++ = '\n'; |
| 323 |
if (p > ra_dbg_buf+sizeof(ra_dbg_buf)-256) { |
| 324 |
fwrite(ra_dbg_buf, 1, (size_t)(p-ra_dbg_buf), stdout); |
| 325 |
p = ra_dbg_buf; |
| 326 |
} |
| 327 |
ra_dbg_p = p; |
| 328 |
} |
| 329 |
|
| 330 |
#define RA_DBG_START() ra_dstart() |
| 331 |
#define RA_DBG_FLUSH() ra_dflush() |
| 332 |
#define RA_DBG_REF() \ |
| 333 |
do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \ |
| 334 |
ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0) |
| 335 |
#define RA_DBGX(x) ra_dprintf x |
| 336 |
|
| 337 |
#else |
| 338 |
#define RA_DBG_START() ((void)0) |
| 339 |
#define RA_DBG_FLUSH() ((void)0) |
| 340 |
#define RA_DBG_REF() ((void)0) |
| 341 |
#define RA_DBGX(x) ((void)0) |
| 342 |
#endif |
| 343 |
|
| 344 |
/* -- Register allocator -------------------------------------------------- */ |
| 345 |
|
| 346 |
#define ra_free(as, r) rset_set(as->freeset, (r)) |
| 347 |
#define ra_modified(as, r) rset_set(as->modset, (r)) |
| 348 |
#define ra_weak(as, r) rset_set(as->weakset, (r)) |
| 349 |
#define ra_noweak(as, r) rset_clear(as->weakset, (r)) |
| 350 |
|
| 351 |
#define ra_used(ir) (ra_hasreg((ir)->r) || ra_hasspill((ir)->s)) |
| 352 |
|
| 353 |
/* Setup register allocator. */ |
| 354 |
static void ra_setup(ASMState *as) |
| 355 |
{ |
| 356 |
Reg r; |
| 357 |
/* Initially all regs (except the stack pointer) are free for use. */ |
| 358 |
as->freeset = RSET_INIT; |
| 359 |
as->modset = RSET_EMPTY; |
| 360 |
as->weakset = RSET_EMPTY; |
| 361 |
as->phiset = RSET_EMPTY; |
| 362 |
memset(as->phireg, 0, sizeof(as->phireg)); |
| 363 |
for (r = RID_MIN_GPR; r < RID_MAX; r++) |
| 364 |
as->cost[r] = REGCOST(~0u, 0u); |
| 365 |
} |
| 366 |
|
| 367 |
/* Rematerialize constants. */ |
| 368 |
static Reg ra_rematk(ASMState *as, IRRef ref) |
| 369 |
{ |
| 370 |
IRIns *ir; |
| 371 |
Reg r; |
| 372 |
if (ra_iskref(ref)) { |
| 373 |
r = ra_krefreg(ref); |
| 374 |
lj_assertA(!rset_test(as->freeset, r), "rematk of free reg %d", r); |
| 375 |
ra_free(as, r); |
| 376 |
ra_modified(as, r); |
| 377 |
#if LJ_64 |
| 378 |
emit_loadu64(as, r, ra_krefk(as, ref)); |
| 379 |
#else |
| 380 |
emit_loadi(as, r, ra_krefk(as, ref)); |
| 381 |
#endif |
| 382 |
return r; |
| 383 |
} |
| 384 |
ir = IR(ref); |
| 385 |
r = ir->r; |
| 386 |
lj_assertA(ra_hasreg(r), "rematk of K%03d has no reg", REF_BIAS - ref); |
| 387 |
lj_assertA(!ra_hasspill(ir->s), |
| 388 |
"rematk of K%03d has spill slot [%x]", REF_BIAS - ref, ir->s); |
| 389 |
ra_free(as, r); |
| 390 |
ra_modified(as, r); |
| 391 |
ir->r = RID_INIT; /* Do not keep any hint. */ |
| 392 |
RA_DBGX((as, "remat $i $r", ir, r)); |
| 393 |
#if !LJ_SOFTFP32 |
| 394 |
if (ir->o == IR_KNUM) { |
| 395 |
emit_loadk64(as, r, ir); |
| 396 |
} else |
| 397 |
#endif |
| 398 |
if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { |
| 399 |
ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ |
| 400 |
emit_getgl(as, r, jit_base); |
| 401 |
} else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { |
| 402 |
/* REF_NIL stores ASMREF_L register. */ |
| 403 |
lj_assertA(irt_isnil(ir->t), "rematk of bad ASMREF_L"); |
| 404 |
emit_getgl(as, r, cur_L); |
| 405 |
#if LJ_64 |
| 406 |
} else if (ir->o == IR_KINT64) { |
| 407 |
emit_loadu64(as, r, ir_kint64(ir)->u64); |
| 408 |
#if LJ_GC64 |
| 409 |
} else if (ir->o == IR_KGC) { |
| 410 |
emit_loadu64(as, r, (uintptr_t)ir_kgc(ir)); |
| 411 |
} else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { |
| 412 |
emit_loadu64(as, r, (uintptr_t)ir_kptr(ir)); |
| 413 |
#endif |
| 414 |
#endif |
| 415 |
} else { |
| 416 |
lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || |
| 417 |
ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, |
| 418 |
"rematk of bad IR op %d", ir->o); |
| 419 |
emit_loadi(as, r, ir->i); |
| 420 |
} |
| 421 |
return r; |
| 422 |
} |
| 423 |
|
| 424 |
/* Force a spill. Allocate a new spill slot if needed. */ |
| 425 |
static int32_t ra_spill(ASMState *as, IRIns *ir) |
| 426 |
{ |
| 427 |
int32_t slot = ir->s; |
| 428 |
lj_assertA(ir >= as->ir + REF_TRUE, |
| 429 |
"spill of K%03d", REF_BIAS - (int)(ir - as->ir)); |
| 430 |
if (!ra_hasspill(slot)) { |
| 431 |
if (irt_is64(ir->t)) { |
| 432 |
slot = as->evenspill; |
| 433 |
as->evenspill += 2; |
| 434 |
} else if (as->oddspill) { |
| 435 |
slot = as->oddspill; |
| 436 |
as->oddspill = 0; |
| 437 |
} else { |
| 438 |
slot = as->evenspill; |
| 439 |
as->oddspill = slot+1; |
| 440 |
as->evenspill += 2; |
| 441 |
} |
| 442 |
if (as->evenspill > 256) |
| 443 |
lj_trace_err(as->J, LJ_TRERR_SPILLOV); |
| 444 |
ir->s = (uint8_t)slot; |
| 445 |
} |
| 446 |
return sps_scale(slot); |
| 447 |
} |
| 448 |
|
| 449 |
/* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */ |
| 450 |
static Reg ra_releasetmp(ASMState *as, IRRef ref) |
| 451 |
{ |
| 452 |
IRIns *ir = IR(ref); |
| 453 |
Reg r = ir->r; |
| 454 |
lj_assertA(ra_hasreg(r), "release of TMP%d has no reg", ref-ASMREF_TMP1+1); |
| 455 |
lj_assertA(!ra_hasspill(ir->s), |
| 456 |
"release of TMP%d has spill slot [%x]", ref-ASMREF_TMP1+1, ir->s); |
| 457 |
ra_free(as, r); |
| 458 |
ra_modified(as, r); |
| 459 |
ir->r = RID_INIT; |
| 460 |
return r; |
| 461 |
} |
| 462 |
|
| 463 |
/* Restore a register (marked as free). Rematerialize or force a spill. */ |
| 464 |
static Reg ra_restore(ASMState *as, IRRef ref) |
| 465 |
{ |
| 466 |
if (emit_canremat(ref)) { |
| 467 |
return ra_rematk(as, ref); |
| 468 |
} else { |
| 469 |
IRIns *ir = IR(ref); |
| 470 |
int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ |
| 471 |
Reg r = ir->r; |
| 472 |
lj_assertA(ra_hasreg(r), "restore of IR %04d has no reg", ref - REF_BIAS); |
| 473 |
ra_sethint(ir->r, r); /* Keep hint. */ |
| 474 |
ra_free(as, r); |
| 475 |
if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ |
| 476 |
ra_modified(as, r); |
| 477 |
RA_DBGX((as, "restore $i $r", ir, r)); |
| 478 |
emit_spload(as, ir, r, ofs); |
| 479 |
} |
| 480 |
return r; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/* Save a register to a spill slot. */ |
| 485 |
static void ra_save(ASMState *as, IRIns *ir, Reg r) |
| 486 |
{ |
| 487 |
RA_DBGX((as, "save $i $r", ir, r)); |
| 488 |
emit_spstore(as, ir, r, sps_scale(ir->s)); |
| 489 |
} |
| 490 |
|
| 491 |
#define MINCOST(name) \ |
| 492 |
if (rset_test(RSET_ALL, RID_##name) && \ |
| 493 |
LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \ |
| 494 |
cost = as->cost[RID_##name]; |
| 495 |
|
| 496 |
/* Evict the register with the lowest cost, forcing a restore. */ |
| 497 |
static Reg ra_evict(ASMState *as, RegSet allow) |
| 498 |
{ |
| 499 |
IRRef ref; |
| 500 |
RegCost cost = ~(RegCost)0; |
| 501 |
lj_assertA(allow != RSET_EMPTY, "evict from empty set"); |
| 502 |
if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { |
| 503 |
GPRDEF(MINCOST) |
| 504 |
} else { |
| 505 |
FPRDEF(MINCOST) |
| 506 |
} |
| 507 |
ref = regcost_ref(cost); |
| 508 |
lj_assertA(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins), |
| 509 |
"evict of out-of-range IR %04d", ref - REF_BIAS); |
| 510 |
/* Preferably pick any weak ref instead of a non-weak, non-const ref. */ |
| 511 |
if (!irref_isk(ref) && (as->weakset & allow)) { |
| 512 |
IRIns *ir = IR(ref); |
| 513 |
if (!rset_test(as->weakset, ir->r)) |
| 514 |
ref = regcost_ref(as->cost[rset_pickbot((as->weakset & allow))]); |
| 515 |
} |
| 516 |
return ra_restore(as, ref); |
| 517 |
} |
| 518 |
|
| 519 |
/* Pick any register (marked as free). Evict on-demand. */ |
| 520 |
static Reg ra_pick(ASMState *as, RegSet allow) |
| 521 |
{ |
| 522 |
RegSet pick = as->freeset & allow; |
| 523 |
if (!pick) |
| 524 |
return ra_evict(as, allow); |
| 525 |
else |
| 526 |
return rset_picktop(pick); |
| 527 |
} |
| 528 |
|
| 529 |
/* Get a scratch register (marked as free). */ |
| 530 |
static Reg ra_scratch(ASMState *as, RegSet allow) |
| 531 |
{ |
| 532 |
Reg r = ra_pick(as, allow); |
| 533 |
ra_modified(as, r); |
| 534 |
RA_DBGX((as, "scratch $r", r)); |
| 535 |
return r; |
| 536 |
} |
| 537 |
|
| 538 |
/* Evict all registers from a set (if not free). */ |
| 539 |
static void ra_evictset(ASMState *as, RegSet drop) |
| 540 |
{ |
| 541 |
RegSet work; |
| 542 |
as->modset |= drop; |
| 543 |
#if !LJ_SOFTFP |
| 544 |
work = (drop & ~as->freeset) & RSET_FPR; |
| 545 |
while (work) { |
| 546 |
Reg r = rset_pickbot(work); |
| 547 |
ra_restore(as, regcost_ref(as->cost[r])); |
| 548 |
rset_clear(work, r); |
| 549 |
checkmclim(as); |
| 550 |
} |
| 551 |
#endif |
| 552 |
work = (drop & ~as->freeset); |
| 553 |
while (work) { |
| 554 |
Reg r = rset_pickbot(work); |
| 555 |
ra_restore(as, regcost_ref(as->cost[r])); |
| 556 |
rset_clear(work, r); |
| 557 |
checkmclim(as); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
/* Evict (rematerialize) all registers allocated to constants. */ |
| 562 |
static void ra_evictk(ASMState *as) |
| 563 |
{ |
| 564 |
RegSet work; |
| 565 |
#if !LJ_SOFTFP |
| 566 |
work = ~as->freeset & RSET_FPR; |
| 567 |
while (work) { |
| 568 |
Reg r = rset_pickbot(work); |
| 569 |
IRRef ref = regcost_ref(as->cost[r]); |
| 570 |
if (emit_canremat(ref) && irref_isk(ref)) { |
| 571 |
ra_rematk(as, ref); |
| 572 |
checkmclim(as); |
| 573 |
} |
| 574 |
rset_clear(work, r); |
| 575 |
} |
| 576 |
#endif |
| 577 |
work = ~as->freeset & RSET_GPR; |
| 578 |
while (work) { |
| 579 |
Reg r = rset_pickbot(work); |
| 580 |
IRRef ref = regcost_ref(as->cost[r]); |
| 581 |
if (emit_canremat(ref) && irref_isk(ref)) { |
| 582 |
ra_rematk(as, ref); |
| 583 |
checkmclim(as); |
| 584 |
} |
| 585 |
rset_clear(work, r); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
#ifdef RID_NUM_KREF |
| 590 |
/* Allocate a register for a constant. */ |
| 591 |
static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow) |
| 592 |
{ |
| 593 |
/* First try to find a register which already holds the same constant. */ |
| 594 |
RegSet pick, work = ~as->freeset & RSET_GPR; |
| 595 |
Reg r; |
| 596 |
while (work) { |
| 597 |
IRRef ref; |
| 598 |
r = rset_pickbot(work); |
| 599 |
ref = regcost_ref(as->cost[r]); |
| 600 |
#if LJ_64 |
| 601 |
if (ref < ASMREF_L) { |
| 602 |
if (ra_iskref(ref)) { |
| 603 |
if (k == ra_krefk(as, ref)) |
| 604 |
return r; |
| 605 |
} else { |
| 606 |
IRIns *ir = IR(ref); |
| 607 |
if ((ir->o == IR_KINT64 && k == (int64_t)ir_kint64(ir)->u64) || |
| 608 |
#if LJ_GC64 |
| 609 |
#if LJ_TARGET_ARM64 |
| 610 |
(ir->o == IR_KINT && (uint64_t)k == (uint32_t)ir->i) || |
| 611 |
#else |
| 612 |
(ir->o == IR_KINT && k == ir->i) || |
| 613 |
#endif |
| 614 |
(ir->o == IR_KGC && k == (intptr_t)ir_kgc(ir)) || |
| 615 |
((ir->o == IR_KPTR || ir->o == IR_KKPTR) && |
| 616 |
k == (intptr_t)ir_kptr(ir)) |
| 617 |
#else |
| 618 |
(ir->o != IR_KINT64 && k == ir->i) |
| 619 |
#endif |
| 620 |
) |
| 621 |
return r; |
| 622 |
} |
| 623 |
} |
| 624 |
#else |
| 625 |
if (ref < ASMREF_L && |
| 626 |
k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) |
| 627 |
return r; |
| 628 |
#endif |
| 629 |
rset_clear(work, r); |
| 630 |
} |
| 631 |
pick = as->freeset & allow; |
| 632 |
if (pick) { |
| 633 |
/* Constants should preferably get unmodified registers. */ |
| 634 |
if ((pick & ~as->modset)) |
| 635 |
pick &= ~as->modset; |
| 636 |
r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ |
| 637 |
} else { |
| 638 |
r = ra_evict(as, allow); |
| 639 |
} |
| 640 |
RA_DBGX((as, "allock $x $r", k, r)); |
| 641 |
ra_setkref(as, r, k); |
| 642 |
rset_clear(as->freeset, r); |
| 643 |
ra_noweak(as, r); |
| 644 |
return r; |
| 645 |
} |
| 646 |
|
| 647 |
/* Allocate a specific register for a constant. */ |
| 648 |
static void ra_allockreg(ASMState *as, intptr_t k, Reg r) |
| 649 |
{ |
| 650 |
Reg kr = ra_allock(as, k, RID2RSET(r)); |
| 651 |
if (kr != r) { |
| 652 |
IRIns irdummy; |
| 653 |
irdummy.t.irt = IRT_INT; |
| 654 |
ra_scratch(as, RID2RSET(r)); |
| 655 |
emit_movrr(as, &irdummy, r, kr); |
| 656 |
} |
| 657 |
} |
| 658 |
#else |
| 659 |
#define ra_allockreg(as, k, r) emit_loadi(as, (r), (k)) |
| 660 |
#endif |
| 661 |
|
| 662 |
/* Allocate a register for ref from the allowed set of registers. |
| 663 |
** Note: this function assumes the ref does NOT have a register yet! |
| 664 |
** Picks an optimal register, sets the cost and marks the register as non-free. |
| 665 |
*/ |
| 666 |
static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow) |
| 667 |
{ |
| 668 |
IRIns *ir = IR(ref); |
| 669 |
RegSet pick = as->freeset & allow; |
| 670 |
Reg r; |
| 671 |
lj_assertA(ra_noreg(ir->r), |
| 672 |
"IR %04d already has reg %d", ref - REF_BIAS, ir->r); |
| 673 |
if (pick) { |
| 674 |
/* First check register hint from propagation or PHI. */ |
| 675 |
if (ra_hashint(ir->r)) { |
| 676 |
r = ra_gethint(ir->r); |
| 677 |
if (rset_test(pick, r)) /* Use hint register if possible. */ |
| 678 |
goto found; |
| 679 |
/* Rematerialization is cheaper than missing a hint. */ |
| 680 |
if (rset_test(allow, r) && emit_canremat(regcost_ref(as->cost[r]))) { |
| 681 |
ra_rematk(as, regcost_ref(as->cost[r])); |
| 682 |
goto found; |
| 683 |
} |
| 684 |
RA_DBGX((as, "hintmiss $f $r", ref, r)); |
| 685 |
} |
| 686 |
/* Invariants should preferably get unmodified registers. */ |
| 687 |
if (ref < as->loopref && !irt_isphi(ir->t)) { |
| 688 |
if ((pick & ~as->modset)) |
| 689 |
pick &= ~as->modset; |
| 690 |
r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ |
| 691 |
} else { |
| 692 |
/* We've got plenty of regs, so get callee-save regs if possible. */ |
| 693 |
if (RID_NUM_GPR > 8 && (pick & ~RSET_SCRATCH)) |
| 694 |
pick &= ~RSET_SCRATCH; |
| 695 |
r = rset_picktop(pick); |
| 696 |
} |
| 697 |
} else { |
| 698 |
r = ra_evict(as, allow); |
| 699 |
} |
| 700 |
found: |
| 701 |
RA_DBGX((as, "alloc $f $r", ref, r)); |
| 702 |
ir->r = (uint8_t)r; |
| 703 |
rset_clear(as->freeset, r); |
| 704 |
ra_noweak(as, r); |
| 705 |
as->cost[r] = REGCOST_REF_T(ref, irt_t(ir->t)); |
| 706 |
return r; |
| 707 |
} |
| 708 |
|
| 709 |
/* Allocate a register on-demand. */ |
| 710 |
static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow) |
| 711 |
{ |
| 712 |
Reg r = IR(ref)->r; |
| 713 |
/* Note: allow is ignored if the register is already allocated. */ |
| 714 |
if (ra_noreg(r)) r = ra_allocref(as, ref, allow); |
| 715 |
ra_noweak(as, r); |
| 716 |
return r; |
| 717 |
} |
| 718 |
|
| 719 |
/* Add a register rename to the IR. */ |
| 720 |
static void ra_addrename(ASMState *as, Reg down, IRRef ref, SnapNo snapno) |
| 721 |
{ |
| 722 |
IRRef ren; |
| 723 |
lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, snapno); |
| 724 |
ren = tref_ref(lj_ir_emit(as->J)); |
| 725 |
as->J->cur.ir[ren].r = (uint8_t)down; |
| 726 |
as->J->cur.ir[ren].s = SPS_NONE; |
| 727 |
} |
| 728 |
|
| 729 |
/* Rename register allocation and emit move. */ |
| 730 |
static void ra_rename(ASMState *as, Reg down, Reg up) |
| 731 |
{ |
| 732 |
IRRef ref = regcost_ref(as->cost[up] = as->cost[down]); |
| 733 |
IRIns *ir = IR(ref); |
| 734 |
ir->r = (uint8_t)up; |
| 735 |
as->cost[down] = 0; |
| 736 |
lj_assertA((down < RID_MAX_GPR) == (up < RID_MAX_GPR), |
| 737 |
"rename between GPR/FPR %d and %d", down, up); |
| 738 |
lj_assertA(!rset_test(as->freeset, down), "rename from free reg %d", down); |
| 739 |
lj_assertA(rset_test(as->freeset, up), "rename to non-free reg %d", up); |
| 740 |
ra_free(as, down); /* 'down' is free ... */ |
| 741 |
ra_modified(as, down); |
| 742 |
rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ |
| 743 |
ra_noweak(as, up); |
| 744 |
RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); |
| 745 |
emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ |
| 746 |
if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ |
| 747 |
/* |
| 748 |
** The rename is effective at the subsequent (already emitted) exit |
| 749 |
** branch. This is for the current snapshot (as->snapno). Except if we |
| 750 |
** haven't yet allocated any refs for the snapshot (as->snapalloc == 1), |
| 751 |
** then it belongs to the next snapshot. |
| 752 |
** See also the discussion at asm_snap_checkrename(). |
| 753 |
*/ |
| 754 |
ra_addrename(as, down, ref, as->snapno + as->snapalloc); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
/* Pick a destination register (marked as free). |
| 759 |
** Caveat: allow is ignored if there's already a destination register. |
| 760 |
** Use ra_destreg() to get a specific register. |
| 761 |
*/ |
| 762 |
static Reg ra_dest(ASMState *as, IRIns *ir, RegSet allow) |
| 763 |
{ |
| 764 |
Reg dest = ir->r; |
| 765 |
if (ra_hasreg(dest)) { |
| 766 |
ra_free(as, dest); |
| 767 |
ra_modified(as, dest); |
| 768 |
} else { |
| 769 |
if (ra_hashint(dest) && rset_test((as->freeset&allow), ra_gethint(dest))) { |
| 770 |
dest = ra_gethint(dest); |
| 771 |
ra_modified(as, dest); |
| 772 |
RA_DBGX((as, "dest $r", dest)); |
| 773 |
} else { |
| 774 |
dest = ra_scratch(as, allow); |
| 775 |
} |
| 776 |
ir->r = dest; |
| 777 |
} |
| 778 |
if (LJ_UNLIKELY(ra_hasspill(ir->s))) ra_save(as, ir, dest); |
| 779 |
return dest; |
| 780 |
} |
| 781 |
|
| 782 |
/* Force a specific destination register (marked as free). */ |
| 783 |
static void ra_destreg(ASMState *as, IRIns *ir, Reg r) |
| 784 |
{ |
| 785 |
Reg dest = ra_dest(as, ir, RID2RSET(r)); |
| 786 |
if (dest != r) { |
| 787 |
lj_assertA(rset_test(as->freeset, r), "dest reg %d is not free", r); |
| 788 |
ra_modified(as, r); |
| 789 |
emit_movrr(as, ir, dest, r); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
#if LJ_TARGET_X86ORX64 |
| 794 |
/* Propagate dest register to left reference. Emit moves as needed. |
| 795 |
** This is a required fixup step for all 2-operand machine instructions. |
| 796 |
*/ |
| 797 |
static void ra_left(ASMState *as, Reg dest, IRRef lref) |
| 798 |
{ |
| 799 |
IRIns *ir = IR(lref); |
| 800 |
Reg left = ir->r; |
| 801 |
if (ra_noreg(left)) { |
| 802 |
if (irref_isk(lref)) { |
| 803 |
if (ir->o == IR_KNUM) { |
| 804 |
/* FP remat needs a load except for +0. Still better than eviction. */ |
| 805 |
if (tvispzero(ir_knum(ir)) || !(as->freeset & RSET_FPR)) { |
| 806 |
emit_loadk64(as, dest, ir); |
| 807 |
return; |
| 808 |
} |
| 809 |
#if LJ_64 |
| 810 |
} else if (ir->o == IR_KINT64) { |
| 811 |
emit_loadk64(as, dest, ir); |
| 812 |
return; |
| 813 |
#if LJ_GC64 |
| 814 |
} else if (ir->o == IR_KGC || ir->o == IR_KPTR || ir->o == IR_KKPTR) { |
| 815 |
emit_loadk64(as, dest, ir); |
| 816 |
return; |
| 817 |
#endif |
| 818 |
#endif |
| 819 |
} else if (ir->o != IR_KPRI) { |
| 820 |
lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || |
| 821 |
ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, |
| 822 |
"K%03d has bad IR op %d", REF_BIAS - lref, ir->o); |
| 823 |
emit_loadi(as, dest, ir->i); |
| 824 |
return; |
| 825 |
} |
| 826 |
} |
| 827 |
if (!ra_hashint(left) && !iscrossref(as, lref)) |
| 828 |
ra_sethint(ir->r, dest); /* Propagate register hint. */ |
| 829 |
left = ra_allocref(as, lref, dest < RID_MAX_GPR ? RSET_GPR : RSET_FPR); |
| 830 |
} |
| 831 |
ra_noweak(as, left); |
| 832 |
/* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */ |
| 833 |
if (dest != left) { |
| 834 |
/* Use register renaming if dest is the PHI reg. */ |
| 835 |
if (irt_isphi(ir->t) && as->phireg[dest] == lref) { |
| 836 |
ra_modified(as, left); |
| 837 |
ra_rename(as, left, dest); |
| 838 |
} else { |
| 839 |
emit_movrr(as, ir, dest, left); |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
#else |
| 844 |
/* Similar to ra_left, except we override any hints. */ |
| 845 |
static void ra_leftov(ASMState *as, Reg dest, IRRef lref) |
| 846 |
{ |
| 847 |
IRIns *ir = IR(lref); |
| 848 |
Reg left = ir->r; |
| 849 |
if (ra_noreg(left)) { |
| 850 |
ra_sethint(ir->r, dest); /* Propagate register hint. */ |
| 851 |
left = ra_allocref(as, lref, |
| 852 |
(LJ_SOFTFP || dest < RID_MAX_GPR) ? RSET_GPR : RSET_FPR); |
| 853 |
} |
| 854 |
ra_noweak(as, left); |
| 855 |
if (dest != left) { |
| 856 |
/* Use register renaming if dest is the PHI reg. */ |
| 857 |
if (irt_isphi(ir->t) && as->phireg[dest] == lref) { |
| 858 |
ra_modified(as, left); |
| 859 |
ra_rename(as, left, dest); |
| 860 |
} else { |
| 861 |
emit_movrr(as, ir, dest, left); |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
#endif |
| 866 |
|
| 867 |
/* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */ |
| 868 |
static void ra_destpair(ASMState *as, IRIns *ir) |
| 869 |
{ |
| 870 |
Reg destlo = ir->r, desthi = (ir+1)->r; |
| 871 |
IRIns *irx = (LJ_64 && !irt_is64(ir->t)) ? ir+1 : ir; |
| 872 |
/* First spill unrelated refs blocking the destination registers. */ |
| 873 |
if (!rset_test(as->freeset, RID_RETLO) && |
| 874 |
destlo != RID_RETLO && desthi != RID_RETLO) |
| 875 |
ra_restore(as, regcost_ref(as->cost[RID_RETLO])); |
| 876 |
if (!rset_test(as->freeset, RID_RETHI) && |
| 877 |
destlo != RID_RETHI && desthi != RID_RETHI) |
| 878 |
ra_restore(as, regcost_ref(as->cost[RID_RETHI])); |
| 879 |
/* Next free the destination registers (if any). */ |
| 880 |
if (ra_hasreg(destlo)) { |
| 881 |
ra_free(as, destlo); |
| 882 |
ra_modified(as, destlo); |
| 883 |
} else { |
| 884 |
destlo = RID_RETLO; |
| 885 |
} |
| 886 |
if (ra_hasreg(desthi)) { |
| 887 |
ra_free(as, desthi); |
| 888 |
ra_modified(as, desthi); |
| 889 |
} else { |
| 890 |
desthi = RID_RETHI; |
| 891 |
} |
| 892 |
/* Check for conflicts and shuffle the registers as needed. */ |
| 893 |
if (destlo == RID_RETHI) { |
| 894 |
if (desthi == RID_RETLO) { |
| 895 |
#if LJ_TARGET_X86ORX64 |
| 896 |
*--as->mcp = XI_XCHGa + RID_RETHI; |
| 897 |
if (LJ_64 && irt_is64(irx->t)) *--as->mcp = 0x48; |
| 898 |
#else |
| 899 |
emit_movrr(as, irx, RID_RETHI, RID_TMP); |
| 900 |
emit_movrr(as, irx, RID_RETLO, RID_RETHI); |
| 901 |
emit_movrr(as, irx, RID_TMP, RID_RETLO); |
| 902 |
#endif |
| 903 |
} else { |
| 904 |
emit_movrr(as, irx, RID_RETHI, RID_RETLO); |
| 905 |
if (desthi != RID_RETHI) emit_movrr(as, irx, desthi, RID_RETHI); |
| 906 |
} |
| 907 |
} else if (desthi == RID_RETLO) { |
| 908 |
emit_movrr(as, irx, RID_RETLO, RID_RETHI); |
| 909 |
if (destlo != RID_RETLO) emit_movrr(as, irx, destlo, RID_RETLO); |
| 910 |
} else { |
| 911 |
if (desthi != RID_RETHI) emit_movrr(as, irx, desthi, RID_RETHI); |
| 912 |
if (destlo != RID_RETLO) emit_movrr(as, irx, destlo, RID_RETLO); |
| 913 |
} |
| 914 |
/* Restore spill slots (if any). */ |
| 915 |
if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI); |
| 916 |
if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RETLO); |
| 917 |
} |
| 918 |
|
| 919 |
/* -- Snapshot handling --------- ----------------------------------------- */ |
| 920 |
|
| 921 |
/* Can we rematerialize a KNUM instead of forcing a spill? */ |
| 922 |
static int asm_snap_canremat(ASMState *as) |
| 923 |
{ |
| 924 |
Reg r; |
| 925 |
for (r = RID_MIN_FPR; r < RID_MAX_FPR; r++) |
| 926 |
if (irref_isk(regcost_ref(as->cost[r]))) |
| 927 |
return 1; |
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
/* Check whether a sunk store corresponds to an allocation. */ |
| 932 |
static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs) |
| 933 |
{ |
| 934 |
if (irs->s == 255) { |
| 935 |
if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || |
| 936 |
irs->o == IR_FSTORE || irs->o == IR_XSTORE) { |
| 937 |
IRIns *irk = IR(irs->op1); |
| 938 |
if (irk->o == IR_AREF || irk->o == IR_HREFK) |
| 939 |
irk = IR(irk->op1); |
| 940 |
return (IR(irk->op1) == ira); |
| 941 |
} |
| 942 |
return 0; |
| 943 |
} else { |
| 944 |
return (ira + irs->s == irs); /* Quick check. */ |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
/* Allocate register or spill slot for a ref that escapes to a snapshot. */ |
| 949 |
static void asm_snap_alloc1(ASMState *as, IRRef ref) |
| 950 |
{ |
| 951 |
IRIns *ir = IR(ref); |
| 952 |
if (!irref_isk(ref)) { |
| 953 |
bloomset(as->snapfilt1, ref); |
| 954 |
bloomset(as->snapfilt2, hashrot(ref, ref + HASH_BIAS)); |
| 955 |
if (ra_used(ir)) return; |
| 956 |
if (ir->r == RID_SINK || ir->r == RID_SUNK) { |
| 957 |
ir->r = RID_SUNK; |
| 958 |
#if LJ_HASFFI |
| 959 |
if (ir->o == IR_CNEWI) { /* Allocate CNEWI value. */ |
| 960 |
asm_snap_alloc1(as, ir->op2); |
| 961 |
if (LJ_32 && (ir+1)->o == IR_HIOP) |
| 962 |
asm_snap_alloc1(as, (ir+1)->op2); |
| 963 |
} else |
| 964 |
#endif |
| 965 |
{ /* Allocate stored values for TNEW, TDUP and CNEW. */ |
| 966 |
IRIns *irs; |
| 967 |
lj_assertA(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW, |
| 968 |
"sink of IR %04d has bad op %d", ref - REF_BIAS, ir->o); |
| 969 |
for (irs = IR(as->snapref-1); irs > ir; irs--) |
| 970 |
if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { |
| 971 |
lj_assertA(irs->o == IR_ASTORE || irs->o == IR_HSTORE || |
| 972 |
irs->o == IR_FSTORE || irs->o == IR_XSTORE, |
| 973 |
"sunk store IR %04d has bad op %d", |
| 974 |
(int)(irs - as->ir) - REF_BIAS, irs->o); |
| 975 |
asm_snap_alloc1(as, irs->op2); |
| 976 |
if (LJ_32 && (irs+1)->o == IR_HIOP) |
| 977 |
asm_snap_alloc1(as, (irs+1)->op2); |
| 978 |
} |
| 979 |
} |
| 980 |
} else { |
| 981 |
RegSet allow; |
| 982 |
if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) { |
| 983 |
IRIns *irc; |
| 984 |
for (irc = IR(as->curins); irc > ir; irc--) |
| 985 |
if ((irc->op1 == ref || irc->op2 == ref) && |
| 986 |
!(irc->r == RID_SINK || irc->r == RID_SUNK)) |
| 987 |
goto nosink; /* Don't sink conversion if result is used. */ |
| 988 |
asm_snap_alloc1(as, ir->op1); |
| 989 |
return; |
| 990 |
} |
| 991 |
nosink: |
| 992 |
allow = (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR; |
| 993 |
if ((as->freeset & allow) || |
| 994 |
(allow == RSET_FPR && asm_snap_canremat(as))) { |
| 995 |
/* Get a weak register if we have a free one or can rematerialize. */ |
| 996 |
Reg r = ra_allocref(as, ref, allow); /* Allocate a register. */ |
| 997 |
if (!irt_isphi(ir->t)) |
| 998 |
ra_weak(as, r); /* But mark it as weakly referenced. */ |
| 999 |
checkmclim(as); |
| 1000 |
RA_DBGX((as, "snapreg $f $r", ref, ir->r)); |
| 1001 |
} else { |
| 1002 |
ra_spill(as, ir); /* Otherwise force a spill slot. */ |
| 1003 |
RA_DBGX((as, "snapspill $f $s", ref, ir->s)); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* Allocate refs escaping to a snapshot. */ |
| 1010 |
static void asm_snap_alloc(ASMState *as, int snapno) |
| 1011 |
{ |
| 1012 |
SnapShot *snap = &as->T->snap[snapno]; |
| 1013 |
SnapEntry *map = &as->T->snapmap[snap->mapofs]; |
| 1014 |
MSize n, nent = snap->nent; |
| 1015 |
as->snapfilt1 = as->snapfilt2 = 0; |
| 1016 |
for (n = 0; n < nent; n++) { |
| 1017 |
SnapEntry sn = map[n]; |
| 1018 |
IRRef ref = snap_ref(sn); |
| 1019 |
if (!irref_isk(ref)) { |
| 1020 |
asm_snap_alloc1(as, ref); |
| 1021 |
if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { |
| 1022 |
lj_assertA(irt_type(IR(ref+1)->t) == IRT_SOFTFP, |
| 1023 |
"snap %d[%d] points to bad SOFTFP IR %04d", |
| 1024 |
snapno, n, ref - REF_BIAS); |
| 1025 |
asm_snap_alloc1(as, ref+1); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* All guards for a snapshot use the same exitno. This is currently the |
| 1032 |
** same as the snapshot number. Since the exact origin of the exit cannot |
| 1033 |
** be determined, all guards for the same snapshot must exit with the same |
| 1034 |
** RegSP mapping. |
| 1035 |
** A renamed ref which has been used in a prior guard for the same snapshot |
| 1036 |
** would cause an inconsistency. The easy way out is to force a spill slot. |
| 1037 |
*/ |
| 1038 |
static int asm_snap_checkrename(ASMState *as, IRRef ren) |
| 1039 |
{ |
| 1040 |
if (bloomtest(as->snapfilt1, ren) && |
| 1041 |
bloomtest(as->snapfilt2, hashrot(ren, ren + HASH_BIAS))) { |
| 1042 |
IRIns *ir = IR(ren); |
| 1043 |
ra_spill(as, ir); /* Register renamed, so force a spill slot. */ |
| 1044 |
RA_DBGX((as, "snaprensp $f $s", ren, ir->s)); |
| 1045 |
return 1; /* Found. */ |
| 1046 |
} |
| 1047 |
return 0; /* Not found. */ |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* Prepare snapshot for next guard or throwing instruction. */ |
| 1051 |
static void asm_snap_prep(ASMState *as) |
| 1052 |
{ |
| 1053 |
if (as->snapalloc) { |
| 1054 |
/* Alloc on first invocation for each snapshot. */ |
| 1055 |
as->snapalloc = 0; |
| 1056 |
asm_snap_alloc(as, as->snapno); |
| 1057 |
as->snaprename = as->T->nins; |
| 1058 |
} else { |
| 1059 |
/* Check any renames above the highwater mark. */ |
| 1060 |
for (; as->snaprename < as->T->nins; as->snaprename++) { |
| 1061 |
IRIns *ir = &as->T->ir[as->snaprename]; |
| 1062 |
if (asm_snap_checkrename(as, ir->op1)) |
| 1063 |
ir->op2 = REF_BIAS-1; /* Kill rename. */ |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
/* Move to previous snapshot when we cross the current snapshot ref. */ |
| 1069 |
static void asm_snap_prev(ASMState *as) |
| 1070 |
{ |
| 1071 |
if (as->curins < as->snapref) { |
| 1072 |
uintptr_t ofs = (uintptr_t)(as->mctoporig - as->mcp); |
| 1073 |
if (ofs >= 0x10000) lj_trace_err(as->J, LJ_TRERR_MCODEOV); |
| 1074 |
do { |
| 1075 |
if (as->snapno == 0) return; |
| 1076 |
as->snapno--; |
| 1077 |
as->snapref = as->T->snap[as->snapno].ref; |
| 1078 |
as->T->snap[as->snapno].mcofs = (uint16_t)ofs; /* Remember mcode ofs. */ |
| 1079 |
} while (as->curins < as->snapref); /* May have no ins inbetween. */ |
| 1080 |
as->snapalloc = 1; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* Fixup snapshot mcode offsetst. */ |
| 1085 |
static void asm_snap_fixup_mcofs(ASMState *as) |
| 1086 |
{ |
| 1087 |
uint32_t sz = (uint32_t)(as->mctoporig - as->mcp); |
| 1088 |
SnapShot *snap = as->T->snap; |
| 1089 |
SnapNo i; |
| 1090 |
for (i = as->T->nsnap-1; i > 0; i--) { |
| 1091 |
/* Compute offset from mcode start and store in correct snapshot. */ |
| 1092 |
snap[i].mcofs = (uint16_t)(sz - snap[i-1].mcofs); |
| 1093 |
} |
| 1094 |
snap[0].mcofs = 0; |
| 1095 |
} |
| 1096 |
|
| 1097 |
/* -- Miscellaneous helpers ----------------------------------------------- */ |
| 1098 |
|
| 1099 |
/* Calculate stack adjustment. */ |
| 1100 |
static int32_t asm_stack_adjust(ASMState *as) |
| 1101 |
{ |
| 1102 |
if (as->evenspill <= SPS_FIXED) |
| 1103 |
return 0; |
| 1104 |
return sps_scale(sps_align(as->evenspill)); |
| 1105 |
} |
| 1106 |
|
| 1107 |
/* Must match with hash*() in lj_tab.c. */ |
| 1108 |
static uint32_t ir_khash(ASMState *as, IRIns *ir) |
| 1109 |
{ |
| 1110 |
uint32_t lo, hi; |
| 1111 |
UNUSED(as); |
| 1112 |
if (irt_isstr(ir->t)) { |
| 1113 |
return ir_kstr(ir)->sid; |
| 1114 |
} else if (irt_isnum(ir->t)) { |
| 1115 |
lo = ir_knum(ir)->u32.lo; |
| 1116 |
hi = ir_knum(ir)->u32.hi << 1; |
| 1117 |
} else if (irt_ispri(ir->t)) { |
| 1118 |
lj_assertA(!irt_isnil(ir->t), "hash of nil key"); |
| 1119 |
return irt_type(ir->t)-IRT_FALSE; |
| 1120 |
} else { |
| 1121 |
lj_assertA(irt_isgcv(ir->t), "hash of bad IR type %d", irt_type(ir->t)); |
| 1122 |
lo = u32ptr(ir_kgc(ir)); |
| 1123 |
#if LJ_GC64 |
| 1124 |
hi = (uint32_t)(u64ptr(ir_kgc(ir)) >> 32) | (irt_toitype(ir->t) << 15); |
| 1125 |
#else |
| 1126 |
hi = lo + HASH_BIAS; |
| 1127 |
#endif |
| 1128 |
} |
| 1129 |
return hashrot(lo, hi); |
| 1130 |
} |
| 1131 |
|
| 1132 |
/* -- Allocations --------------------------------------------------------- */ |
| 1133 |
|
| 1134 |
static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args); |
| 1135 |
static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci); |
| 1136 |
|
| 1137 |
static void asm_snew(ASMState *as, IRIns *ir) |
| 1138 |
{ |
| 1139 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; |
| 1140 |
IRRef args[3]; |
| 1141 |
asm_snap_prep(as); |
| 1142 |
args[0] = ASMREF_L; /* lua_State *L */ |
| 1143 |
args[1] = ir->op1; /* const char *str */ |
| 1144 |
args[2] = ir->op2; /* size_t len */ |
| 1145 |
as->gcsteps++; |
| 1146 |
asm_setupresult(as, ir, ci); /* GCstr * */ |
| 1147 |
asm_gencall(as, ci, args); |
| 1148 |
} |
| 1149 |
|
| 1150 |
static void asm_tnew(ASMState *as, IRIns *ir) |
| 1151 |
{ |
| 1152 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; |
| 1153 |
IRRef args[2]; |
| 1154 |
asm_snap_prep(as); |
| 1155 |
args[0] = ASMREF_L; /* lua_State *L */ |
| 1156 |
args[1] = ASMREF_TMP1; /* uint32_t ahsize */ |
| 1157 |
as->gcsteps++; |
| 1158 |
asm_setupresult(as, ir, ci); /* GCtab * */ |
| 1159 |
asm_gencall(as, ci, args); |
| 1160 |
ra_allockreg(as, ir->op1 | (ir->op2 << 24), ra_releasetmp(as, ASMREF_TMP1)); |
| 1161 |
} |
| 1162 |
|
| 1163 |
static void asm_tdup(ASMState *as, IRIns *ir) |
| 1164 |
{ |
| 1165 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; |
| 1166 |
IRRef args[2]; |
| 1167 |
asm_snap_prep(as); |
| 1168 |
args[0] = ASMREF_L; /* lua_State *L */ |
| 1169 |
args[1] = ir->op1; /* const GCtab *kt */ |
| 1170 |
as->gcsteps++; |
| 1171 |
asm_setupresult(as, ir, ci); /* GCtab * */ |
| 1172 |
asm_gencall(as, ci, args); |
| 1173 |
} |
| 1174 |
|
| 1175 |
static void asm_gc_check(ASMState *as); |
| 1176 |
|
| 1177 |
/* Explicit GC step. */ |
| 1178 |
static void asm_gcstep(ASMState *as, IRIns *ir) |
| 1179 |
{ |
| 1180 |
IRIns *ira; |
| 1181 |
for (ira = IR(as->stopins+1); ira < ir; ira++) |
| 1182 |
if ((ira->o == IR_TNEW || ira->o == IR_TDUP || |
| 1183 |
(LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI))) && |
| 1184 |
ra_used(ira)) |
| 1185 |
as->gcsteps++; |
| 1186 |
if (as->gcsteps) |
| 1187 |
asm_gc_check(as); |
| 1188 |
as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ |
| 1189 |
} |
| 1190 |
|
| 1191 |
/* -- Buffer operations --------------------------------------------------- */ |
| 1192 |
|
| 1193 |
static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode); |
| 1194 |
#if LJ_HASBUFFER |
| 1195 |
static void asm_bufhdr_write(ASMState *as, Reg sb); |
| 1196 |
#endif |
| 1197 |
|
| 1198 |
static void asm_bufhdr(ASMState *as, IRIns *ir) |
| 1199 |
{ |
| 1200 |
Reg sb = ra_dest(as, ir, RSET_GPR); |
| 1201 |
switch (ir->op2) { |
| 1202 |
case IRBUFHDR_RESET: { |
| 1203 |
Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); |
| 1204 |
IRIns irbp; |
| 1205 |
irbp.ot = IRT(0, IRT_PTR); /* Buffer data pointer type. */ |
| 1206 |
emit_storeofs(as, &irbp, tmp, sb, offsetof(SBuf, w)); |
| 1207 |
emit_loadofs(as, &irbp, tmp, sb, offsetof(SBuf, b)); |
| 1208 |
break; |
| 1209 |
} |
| 1210 |
case IRBUFHDR_APPEND: { |
| 1211 |
/* Rematerialize const buffer pointer instead of likely spill. */ |
| 1212 |
IRIns *irp = IR(ir->op1); |
| 1213 |
if (!(ra_hasreg(irp->r) || irp == ir-1 || |
| 1214 |
(irp == ir-2 && !ra_used(ir-1)))) { |
| 1215 |
while (!(irp->o == IR_BUFHDR && irp->op2 == IRBUFHDR_RESET)) |
| 1216 |
irp = IR(irp->op1); |
| 1217 |
if (irref_isk(irp->op1)) { |
| 1218 |
ra_weak(as, ra_allocref(as, ir->op1, RSET_GPR)); |
| 1219 |
ir = irp; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
break; |
| 1223 |
} |
| 1224 |
#if LJ_HASBUFFER |
| 1225 |
case IRBUFHDR_WRITE: |
| 1226 |
asm_bufhdr_write(as, sb); |
| 1227 |
break; |
| 1228 |
#endif |
| 1229 |
default: lj_assertA(0, "bad BUFHDR op2 %d", ir->op2); break; |
| 1230 |
} |
| 1231 |
#if LJ_TARGET_X86ORX64 |
| 1232 |
ra_left(as, sb, ir->op1); |
| 1233 |
#else |
| 1234 |
ra_leftov(as, sb, ir->op1); |
| 1235 |
#endif |
| 1236 |
} |
| 1237 |
|
| 1238 |
static void asm_bufput(ASMState *as, IRIns *ir) |
| 1239 |
{ |
| 1240 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_putstr]; |
| 1241 |
IRRef args[3]; |
| 1242 |
IRIns *irs; |
| 1243 |
int kchar = -129; |
| 1244 |
args[0] = ir->op1; /* SBuf * */ |
| 1245 |
args[1] = ir->op2; /* GCstr * */ |
| 1246 |
irs = IR(ir->op2); |
| 1247 |
lj_assertA(irt_isstr(irs->t), |
| 1248 |
"BUFPUT of non-string IR %04d", ir->op2 - REF_BIAS); |
| 1249 |
if (irs->o == IR_KGC) { |
| 1250 |
GCstr *s = ir_kstr(irs); |
| 1251 |
if (s->len == 1) { /* Optimize put of single-char string constant. */ |
| 1252 |
kchar = (int8_t)strdata(s)[0]; /* Signed! */ |
| 1253 |
args[1] = ASMREF_TMP1; /* int, truncated to char */ |
| 1254 |
ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; |
| 1255 |
} |
| 1256 |
} else if (mayfuse(as, ir->op2) && ra_noreg(irs->r)) { |
| 1257 |
if (irs->o == IR_TOSTR) { /* Fuse number to string conversions. */ |
| 1258 |
if (irs->op2 == IRTOSTR_NUM) { |
| 1259 |
args[1] = ASMREF_TMP1; /* TValue * */ |
| 1260 |
ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putnum]; |
| 1261 |
} else { |
| 1262 |
lj_assertA(irt_isinteger(IR(irs->op1)->t), |
| 1263 |
"TOSTR of non-numeric IR %04d", irs->op1); |
| 1264 |
args[1] = irs->op1; /* int */ |
| 1265 |
if (irs->op2 == IRTOSTR_INT) |
| 1266 |
ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putint]; |
| 1267 |
else |
| 1268 |
ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; |
| 1269 |
} |
| 1270 |
} else if (irs->o == IR_SNEW) { /* Fuse string allocation. */ |
| 1271 |
args[1] = irs->op1; /* const void * */ |
| 1272 |
args[2] = irs->op2; /* MSize */ |
| 1273 |
ci = &lj_ir_callinfo[IRCALL_lj_buf_putmem]; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
asm_setupresult(as, ir, ci); /* SBuf * */ |
| 1277 |
asm_gencall(as, ci, args); |
| 1278 |
if (args[1] == ASMREF_TMP1) { |
| 1279 |
Reg tmp = ra_releasetmp(as, ASMREF_TMP1); |
| 1280 |
if (kchar == -129) |
| 1281 |
asm_tvptr(as, tmp, irs->op1, IRTMPREF_IN1); |
| 1282 |
else |
| 1283 |
ra_allockreg(as, kchar, tmp); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
static void asm_bufstr(ASMState *as, IRIns *ir) |
| 1288 |
{ |
| 1289 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_tostr]; |
| 1290 |
IRRef args[1]; |
| 1291 |
args[0] = ir->op1; /* SBuf *sb */ |
| 1292 |
as->gcsteps++; |
| 1293 |
asm_setupresult(as, ir, ci); /* GCstr * */ |
| 1294 |
asm_gencall(as, ci, args); |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* -- Type conversions ---------------------------------------------------- */ |
| 1298 |
|
| 1299 |
static void asm_tostr(ASMState *as, IRIns *ir) |
| 1300 |
{ |
| 1301 |
const CCallInfo *ci; |
| 1302 |
IRRef args[2]; |
| 1303 |
asm_snap_prep(as); |
| 1304 |
args[0] = ASMREF_L; |
| 1305 |
as->gcsteps++; |
| 1306 |
if (ir->op2 == IRTOSTR_NUM) { |
| 1307 |
args[1] = ASMREF_TMP1; /* cTValue * */ |
| 1308 |
ci = &lj_ir_callinfo[IRCALL_lj_strfmt_num]; |
| 1309 |
} else { |
| 1310 |
args[1] = ir->op1; /* int32_t k */ |
| 1311 |
if (ir->op2 == IRTOSTR_INT) |
| 1312 |
ci = &lj_ir_callinfo[IRCALL_lj_strfmt_int]; |
| 1313 |
else |
| 1314 |
ci = &lj_ir_callinfo[IRCALL_lj_strfmt_char]; |
| 1315 |
} |
| 1316 |
asm_setupresult(as, ir, ci); /* GCstr * */ |
| 1317 |
asm_gencall(as, ci, args); |
| 1318 |
if (ir->op2 == IRTOSTR_NUM) |
| 1319 |
asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1, IRTMPREF_IN1); |
| 1320 |
} |
| 1321 |
|
| 1322 |
#if LJ_32 && LJ_HASFFI && !LJ_SOFTFP && !LJ_TARGET_X86 |
| 1323 |
static void asm_conv64(ASMState *as, IRIns *ir) |
| 1324 |
{ |
| 1325 |
IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); |
| 1326 |
IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); |
| 1327 |
IRCallID id; |
| 1328 |
IRRef args[2]; |
| 1329 |
lj_assertA((ir-1)->o == IR_CONV && ir->o == IR_HIOP, |
| 1330 |
"not a CONV/HIOP pair at IR %04d", (int)(ir - as->ir) - REF_BIAS); |
| 1331 |
args[LJ_BE] = (ir-1)->op1; |
| 1332 |
args[LJ_LE] = ir->op1; |
| 1333 |
if (st == IRT_NUM || st == IRT_FLOAT) { |
| 1334 |
id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); |
| 1335 |
ir--; |
| 1336 |
} else { |
| 1337 |
id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); |
| 1338 |
} |
| 1339 |
{ |
| 1340 |
#if LJ_TARGET_ARM && !LJ_ABI_SOFTFP |
| 1341 |
CCallInfo cim = lj_ir_callinfo[id], *ci = &cim; |
| 1342 |
cim.flags |= CCI_VARARG; /* These calls don't use the hard-float ABI! */ |
| 1343 |
#else |
| 1344 |
const CCallInfo *ci = &lj_ir_callinfo[id]; |
| 1345 |
#endif |
| 1346 |
asm_setupresult(as, ir, ci); |
| 1347 |
asm_gencall(as, ci, args); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
#endif |
| 1351 |
|
| 1352 |
/* -- Memory references --------------------------------------------------- */ |
| 1353 |
|
| 1354 |
static void asm_newref(ASMState *as, IRIns *ir) |
| 1355 |
{ |
| 1356 |
const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; |
| 1357 |
IRRef args[3]; |
| 1358 |
if (ir->r == RID_SINK) |
| 1359 |
return; |
| 1360 |
asm_snap_prep(as); |
| 1361 |
args[0] = ASMREF_L; /* lua_State *L */ |
| 1362 |
args[1] = ir->op1; /* GCtab *t */ |
| 1363 |
args[2] = ASMREF_TMP1; /* cTValue *key */ |
| 1364 |
asm_setupresult(as, ir, ci); /* TValue * */ |
| 1365 |
asm_gencall(as, ci, args); |
| 1366 |
asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2, IRTMPREF_IN1); |
| 1367 |
} |
| 1368 |
|
| 1369 |
static void asm_tmpref(ASMState *as, IRIns *ir) |
| 1370 |
{ |
| 1371 |
Reg r = ra_dest(as, ir, RSET_GPR); |
| 1372 |
asm_tvptr(as, r, ir->op1, ir->op2); |
| 1373 |
} |
| 1374 |
|
| 1375 |
static void asm_lref(ASMState *as, IRIns *ir) |
| 1376 |
{ |
| 1377 |
Reg r = ra_dest(as, ir, RSET_GPR); |
| 1378 |
#if LJ_TARGET_X86ORX64 |
| 1379 |
ra_left(as, r, ASMREF_L); |
| 1380 |
#else |
| 1381 |
ra_leftov(as, r, ASMREF_L); |
| 1382 |
#endif |
| 1383 |
} |
| 1384 |
|
| 1385 |
/* -- Calls --------------------------------------------------------------- */ |
| 1386 |
|
| 1387 |
/* Collect arguments from CALL* and CARG instructions. */ |
| 1388 |
static void asm_collectargs(ASMState *as, IRIns *ir, |
| 1389 |
const CCallInfo *ci, IRRef *args) |
| 1390 |
{ |
| 1391 |
uint32_t n = CCI_XNARGS(ci); |
| 1392 |
/* Account for split args. */ |
| 1393 |
lj_assertA(n <= CCI_NARGS_MAX*2, "too many args %d to collect", n); |
| 1394 |
if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } |
| 1395 |
while (n-- > 1) { |
| 1396 |
ir = IR(ir->op1); |
| 1397 |
lj_assertA(ir->o == IR_CARG, "malformed CALL arg tree"); |
| 1398 |
args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; |
| 1399 |
} |
| 1400 |
args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; |
| 1401 |
lj_assertA(IR(ir->op1)->o != IR_CARG, "malformed CALL arg tree"); |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* Reconstruct CCallInfo flags for CALLX*. */ |
| 1405 |
static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) |
| 1406 |
{ |
| 1407 |
uint32_t nargs = 0; |
| 1408 |
if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ |
| 1409 |
IRIns *ira = IR(ir->op1); |
| 1410 |
nargs++; |
| 1411 |
while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } |
| 1412 |
} |
| 1413 |
#if LJ_HASFFI |
| 1414 |
if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ |
| 1415 |
CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; |
| 1416 |
CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); |
| 1417 |
nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); |
| 1418 |
#if LJ_TARGET_X86 |
| 1419 |
nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); |
| 1420 |
#endif |
| 1421 |
} |
| 1422 |
#endif |
| 1423 |
return (nargs | (ir->t.irt << CCI_OTSHIFT)); |
| 1424 |
} |
| 1425 |
|
| 1426 |
static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) |
| 1427 |
{ |
| 1428 |
const CCallInfo *ci = &lj_ir_callinfo[id]; |
| 1429 |
IRRef args[2]; |
| 1430 |
args[0] = ir->op1; |
| 1431 |
args[1] = ir->op2; |
| 1432 |
asm_setupresult(as, ir, ci); |
| 1433 |
asm_gencall(as, ci, args); |
| 1434 |
} |
| 1435 |
|
| 1436 |
static void asm_call(ASMState *as, IRIns *ir) |
| 1437 |
{ |
| 1438 |
IRRef args[CCI_NARGS_MAX]; |
| 1439 |
const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; |
| 1440 |
asm_collectargs(as, ir, ci, args); |
| 1441 |
asm_setupresult(as, ir, ci); |
| 1442 |
asm_gencall(as, ci, args); |
| 1443 |
} |
| 1444 |
|
| 1445 |
/* -- PHI and loop handling ----------------------------------------------- */ |
| 1446 |
|
| 1447 |
/* Break a PHI cycle by renaming to a free register (evict if needed). */ |
| 1448 |
static void asm_phi_break(ASMState *as, RegSet blocked, RegSet blockedby, |
| 1449 |
RegSet allow) |
| 1450 |
{ |
| 1451 |
RegSet candidates = blocked & allow; |
| 1452 |
if (candidates) { /* If this register file has candidates. */ |
| 1453 |
/* Note: the set for ra_pick cannot be empty, since each register file |
| 1454 |
** has some registers never allocated to PHIs. |
| 1455 |
*/ |
| 1456 |
Reg down, up = ra_pick(as, ~blocked & allow); /* Get a free register. */ |
| 1457 |
if (candidates & ~blockedby) /* Optimize shifts, else it's a cycle. */ |
| 1458 |
candidates = candidates & ~blockedby; |
| 1459 |
down = rset_picktop(candidates); /* Pick candidate PHI register. */ |
| 1460 |
ra_rename(as, down, up); /* And rename it to the free register. */ |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* PHI register shuffling. |
| 1465 |
** |
| 1466 |
** The allocator tries hard to preserve PHI register assignments across |
| 1467 |
** the loop body. Most of the time this loop does nothing, since there |
| 1468 |
** are no register mismatches. |
| 1469 |
** |
| 1470 |
** If a register mismatch is detected and ... |
| 1471 |
** - the register is currently free: rename it. |
| 1472 |
** - the register is blocked by an invariant: restore/remat and rename it. |
| 1473 |
** - Otherwise the register is used by another PHI, so mark it as blocked. |
| 1474 |
** |
| 1475 |
** The renames are order-sensitive, so just retry the loop if a register |
| 1476 |
** is marked as blocked, but has been freed in the meantime. A cycle is |
| 1477 |
** detected if all of the blocked registers are allocated. To break the |
| 1478 |
** cycle rename one of them to a free register and retry. |
| 1479 |
** |
| 1480 |
** Note that PHI spill slots are kept in sync and don't need to be shuffled. |
| 1481 |
*/ |
| 1482 |
static void asm_phi_shuffle(ASMState *as) |
| 1483 |
{ |
| 1484 |
RegSet work; |
| 1485 |
|
| 1486 |
/* Find and resolve PHI register mismatches. */ |
| 1487 |
for (;;) { |
| 1488 |
RegSet blocked = RSET_EMPTY; |
| 1489 |
RegSet blockedby = RSET_EMPTY; |
| 1490 |
RegSet phiset = as->phiset; |
| 1491 |
while (phiset) { /* Check all left PHI operand registers. */ |
| 1492 |
Reg r = rset_pickbot(phiset); |
| 1493 |
IRIns *irl = IR(as->phireg[r]); |
| 1494 |
Reg left = irl->r; |
| 1495 |
if (r != left) { /* Mismatch? */ |
| 1496 |
if (!rset_test(as->freeset, r)) { /* PHI register blocked? */ |
| 1497 |
IRRef ref = regcost_ref(as->cost[r]); |
| 1498 |
/* Blocked by other PHI (w/reg)? */ |
| 1499 |
if (!ra_iskref(ref) && irt_ismarked(IR(ref)->t)) { |
| 1500 |
rset_set(blocked, r); |
| 1501 |
if (ra_hasreg(left)) |
| 1502 |
rset_set(blockedby, left); |
| 1503 |
left = RID_NONE; |
| 1504 |
} else { /* Otherwise grab register from invariant. */ |
| 1505 |
ra_restore(as, ref); |
| 1506 |
checkmclim(as); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
if (ra_hasreg(left)) { |
| 1510 |
ra_rename(as, left, r); |
| 1511 |
checkmclim(as); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
rset_clear(phiset, r); |
| 1515 |
} |
| 1516 |
if (!blocked) break; /* Finished. */ |
| 1517 |
if (!(as->freeset & blocked)) { /* Break cycles if none are free. */ |
| 1518 |
asm_phi_break(as, blocked, blockedby, RSET_GPR); |
| 1519 |
if (!LJ_SOFTFP) asm_phi_break(as, blocked, blockedby, RSET_FPR); |
| 1520 |
checkmclim(as); |
| 1521 |
} /* Else retry some more renames. */ |
| 1522 |
} |
| 1523 |
|
| 1524 |
/* Restore/remat invariants whose registers are modified inside the loop. */ |
| 1525 |
#if !LJ_SOFTFP |
| 1526 |
work = as->modset & ~(as->freeset | as->phiset) & RSET_FPR; |
| 1527 |
while (work) { |
| 1528 |
Reg r = rset_pickbot(work); |
| 1529 |
ra_restore(as, regcost_ref(as->cost[r])); |
| 1530 |
rset_clear(work, r); |
| 1531 |
checkmclim(as); |
| 1532 |
} |
| 1533 |
#endif |
| 1534 |
work = as->modset & ~(as->freeset | as->phiset); |
| 1535 |
while (work) { |
| 1536 |
Reg r = rset_pickbot(work); |
| 1537 |
ra_restore(as, regcost_ref(as->cost[r])); |
| 1538 |
rset_clear(work, r); |
| 1539 |
checkmclim(as); |
| 1540 |
} |
| 1541 |
|
| 1542 |
/* Allocate and save all unsaved PHI regs and clear marks. */ |
| 1543 |
work = as->phiset; |
| 1544 |
while (work) { |
| 1545 |
Reg r = rset_picktop(work); |
| 1546 |
IRRef lref = as->phireg[r]; |
| 1547 |
IRIns *ir = IR(lref); |
| 1548 |
if (ra_hasspill(ir->s)) { /* Left PHI gained a spill slot? */ |
| 1549 |
irt_clearmark(ir->t); /* Handled here, so clear marker now. */ |
| 1550 |
ra_alloc1(as, lref, RID2RSET(r)); |
| 1551 |
ra_save(as, ir, r); /* Save to spill slot inside the loop. */ |
| 1552 |
checkmclim(as); |
| 1553 |
} |
| 1554 |
rset_clear(work, r); |
| 1555 |
} |
| 1556 |
} |
| 1557 |
|
| 1558 |
/* Copy unsynced left/right PHI spill slots. Rarely needed. */ |
| 1559 |
static void asm_phi_copyspill(ASMState *as) |
| 1560 |
{ |
| 1561 |
int need = 0; |
| 1562 |
IRIns *ir; |
| 1563 |
for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) |
| 1564 |
if (ra_hasspill(ir->s) && ra_hasspill(IR(ir->op1)->s)) |
| 1565 |
need |= irt_isfp(ir->t) ? 2 : 1; /* Unsynced spill slot? */ |
| 1566 |
if ((need & 1)) { /* Copy integer spill slots. */ |
| 1567 |
#if !LJ_TARGET_X86ORX64 |
| 1568 |
Reg r = RID_TMP; |
| 1569 |
#else |
| 1570 |
Reg r = RID_RET; |
| 1571 |
if ((as->freeset & RSET_GPR)) |
| 1572 |
r = rset_pickbot((as->freeset & RSET_GPR)); |
| 1573 |
else |
| 1574 |
emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); |
| 1575 |
#endif |
| 1576 |
for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { |
| 1577 |
if (ra_hasspill(ir->s)) { |
| 1578 |
IRIns *irl = IR(ir->op1); |
| 1579 |
if (ra_hasspill(irl->s) && !irt_isfp(ir->t)) { |
| 1580 |
emit_spstore(as, irl, r, sps_scale(irl->s)); |
| 1581 |
emit_spload(as, ir, r, sps_scale(ir->s)); |
| 1582 |
checkmclim(as); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
} |
| 1586 |
#if LJ_TARGET_X86ORX64 |
| 1587 |
if (!rset_test(as->freeset, r)) |
| 1588 |
emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); |
| 1589 |
#endif |
| 1590 |
} |
| 1591 |
#if !LJ_SOFTFP |
| 1592 |
if ((need & 2)) { /* Copy FP spill slots. */ |
| 1593 |
#if LJ_TARGET_X86 |
| 1594 |
Reg r = RID_XMM0; |
| 1595 |
#else |
| 1596 |
Reg r = RID_FPRET; |
| 1597 |
#endif |
| 1598 |
if ((as->freeset & RSET_FPR)) |
| 1599 |
r = rset_pickbot((as->freeset & RSET_FPR)); |
| 1600 |
if (!rset_test(as->freeset, r)) |
| 1601 |
emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); |
| 1602 |
for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { |
| 1603 |
if (ra_hasspill(ir->s)) { |
| 1604 |
IRIns *irl = IR(ir->op1); |
| 1605 |
if (ra_hasspill(irl->s) && irt_isfp(ir->t)) { |
| 1606 |
emit_spstore(as, irl, r, sps_scale(irl->s)); |
| 1607 |
emit_spload(as, ir, r, sps_scale(ir->s)); |
| 1608 |
checkmclim(as); |
| 1609 |
} |
| 1610 |
} |
| 1611 |
} |
| 1612 |
if (!rset_test(as->freeset, r)) |
| 1613 |
emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); |
| 1614 |
} |
| 1615 |
#endif |
| 1616 |
} |
| 1617 |
|
| 1618 |
/* Emit renames for left PHIs which are only spilled outside the loop. */ |
| 1619 |
static void asm_phi_fixup(ASMState *as) |
| 1620 |
{ |
| 1621 |
RegSet work = as->phiset; |
| 1622 |
while (work) { |
| 1623 |
Reg r = rset_picktop(work); |
| 1624 |
IRRef lref = as->phireg[r]; |
| 1625 |
IRIns *ir = IR(lref); |
| 1626 |
if (irt_ismarked(ir->t)) { |
| 1627 |
irt_clearmark(ir->t); |
| 1628 |
/* Left PHI gained a spill slot before the loop? */ |
| 1629 |
if (ra_hasspill(ir->s)) { |
| 1630 |
ra_addrename(as, r, lref, as->loopsnapno); |
| 1631 |
} |
| 1632 |
} |
| 1633 |
rset_clear(work, r); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
/* Setup right PHI reference. */ |
| 1638 |
static void asm_phi(ASMState *as, IRIns *ir) |
| 1639 |
{ |
| 1640 |
RegSet allow = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & |
| 1641 |
~as->phiset; |
| 1642 |
RegSet afree = (as->freeset & allow); |
| 1643 |
IRIns *irl = IR(ir->op1); |
| 1644 |
IRIns *irr = IR(ir->op2); |
| 1645 |
if (ir->r == RID_SINK) /* Sink PHI. */ |
| 1646 |
return; |
| 1647 |
/* Spill slot shuffling is not implemented yet (but rarely needed). */ |
| 1648 |
if (ra_hasspill(irl->s) || ra_hasspill(irr->s)) |
| 1649 |
lj_trace_err(as->J, LJ_TRERR_NYIPHI); |
| 1650 |
/* Leave at least one register free for non-PHIs (and PHI cycle breaking). */ |
| 1651 |
if ((afree & (afree-1))) { /* Two or more free registers? */ |
| 1652 |
Reg r; |
| 1653 |
if (ra_noreg(irr->r)) { /* Get a register for the right PHI. */ |
| 1654 |
r = ra_allocref(as, ir->op2, allow); |
| 1655 |
} else { /* Duplicate right PHI, need a copy (rare). */ |
| 1656 |
r = ra_scratch(as, allow); |
| 1657 |
emit_movrr(as, irr, r, irr->r); |
| 1658 |
} |
| 1659 |
ir->r = (uint8_t)r; |
| 1660 |
rset_set(as->phiset, r); |
| 1661 |
as->phireg[r] = (IRRef1)ir->op1; |
| 1662 |
irt_setmark(irl->t); /* Marks left PHIs _with_ register. */ |
| 1663 |
if (ra_noreg(irl->r)) |
| 1664 |
ra_sethint(irl->r, r); /* Set register hint for left PHI. */ |
| 1665 |
} else { /* Otherwise allocate a spill slot. */ |
| 1666 |
/* This is overly restrictive, but it triggers only on synthetic code. */ |
| 1667 |
if (ra_hasreg(irl->r) || ra_hasreg(irr->r)) |
| 1668 |
lj_trace_err(as->J, LJ_TRERR_NYIPHI); |
| 1669 |
ra_spill(as, ir); |
| 1670 |
irr->s = ir->s; /* Set right PHI spill slot. Sync left slot later. */ |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
static void asm_loop_fixup(ASMState *as); |
| 1675 |
|
| 1676 |
/* Middle part of a loop. */ |
| 1677 |
static void asm_loop(ASMState *as) |
| 1678 |
{ |
| 1679 |
MCode *mcspill; |
| 1680 |
/* LOOP is a guard, so the snapno is up to date. */ |
| 1681 |
as->loopsnapno = as->snapno; |
| 1682 |
if (as->gcsteps) |
| 1683 |
asm_gc_check(as); |
| 1684 |
/* LOOP marks the transition from the variant to the invariant part. */ |
| 1685 |
as->flagmcp = as->invmcp = NULL; |
| 1686 |
as->sectref = 0; |
| 1687 |
if (!neverfuse(as)) as->fuseref = 0; |
| 1688 |
asm_phi_shuffle(as); |
| 1689 |
mcspill = as->mcp; |
| 1690 |
asm_phi_copyspill(as); |
| 1691 |
asm_loop_fixup(as); |
| 1692 |
as->mcloop = as->mcp; |
| 1693 |
RA_DBGX((as, "===== LOOP =====")); |
| 1694 |
if (!as->realign) RA_DBG_FLUSH(); |
| 1695 |
if (as->mcp != mcspill) |
| 1696 |
emit_jmp(as, mcspill); |
| 1697 |
} |
| 1698 |
|
| 1699 |
/* -- Target-specific assembler ------------------------------------------- */ |
| 1700 |
|
| 1701 |
#if LJ_TARGET_X86ORX64 |
| 1702 |
#include "lj_asm_x86.h" |
| 1703 |
#elif LJ_TARGET_ARM |
| 1704 |
#include "lj_asm_arm.h" |
| 1705 |
#elif LJ_TARGET_ARM64 |
| 1706 |
#include "lj_asm_arm64.h" |
| 1707 |
#elif LJ_TARGET_PPC |
| 1708 |
#include "lj_asm_ppc.h" |
| 1709 |
#elif LJ_TARGET_MIPS |
| 1710 |
#include "lj_asm_mips.h" |
| 1711 |
#elif LJ_TARGET_S390X |
| 1712 |
#include "lj_asm_s390x.h" |
| 1713 |
#else |
| 1714 |
#error "Missing assembler for target CPU" |
| 1715 |
#endif |
| 1716 |
|
| 1717 |
/* -- Common instruction helpers ------------------------------------------ */ |
| 1718 |
|
| 1719 |
#if !LJ_SOFTFP32 |
| 1720 |
#if !LJ_TARGET_X86ORX64 |
| 1721 |
#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) |
| 1722 |
#endif |
| 1723 |
|
| 1724 |
static void asm_pow(ASMState *as, IRIns *ir) |
| 1725 |
{ |
| 1726 |
#if LJ_64 && LJ_HASFFI |
| 1727 |
if (!irt_isnum(ir->t)) |
| 1728 |
asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : |
| 1729 |
IRCALL_lj_carith_powu64); |
| 1730 |
else |
| 1731 |
#endif |
| 1732 |
asm_callid(as, ir, IRCALL_pow); |
| 1733 |
} |
| 1734 |
|
| 1735 |
static void asm_div(ASMState *as, IRIns *ir) |
| 1736 |
{ |
| 1737 |
#if LJ_64 && LJ_HASFFI |
| 1738 |
if (!irt_isnum(ir->t)) |
| 1739 |
asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : |
| 1740 |
IRCALL_lj_carith_divu64); |
| 1741 |
else |
| 1742 |
#endif |
| 1743 |
asm_fpdiv(as, ir); |
| 1744 |
} |
| 1745 |
#endif |
| 1746 |
|
| 1747 |
static void asm_mod(ASMState *as, IRIns *ir) |
| 1748 |
{ |
| 1749 |
#if LJ_64 && LJ_HASFFI |
| 1750 |
if (!irt_isint(ir->t)) |
| 1751 |
asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : |
| 1752 |
IRCALL_lj_carith_modu64); |
| 1753 |
else |
| 1754 |
#endif |
| 1755 |
asm_callid(as, ir, IRCALL_lj_vm_modi); |
| 1756 |
} |
| 1757 |
|
| 1758 |
static void asm_fuseequal(ASMState *as, IRIns *ir) |
| 1759 |
{ |
| 1760 |
/* Fuse HREF + EQ/NE. */ |
| 1761 |
if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { |
| 1762 |
as->curins--; |
| 1763 |
asm_href(as, ir-1, (IROp)ir->o); |
| 1764 |
} else { |
| 1765 |
asm_equal(as, ir); |
| 1766 |
} |
| 1767 |
} |
| 1768 |
|
| 1769 |
static void asm_alen(ASMState *as, IRIns *ir) |
| 1770 |
{ |
| 1771 |
asm_callid(as, ir, ir->op2 == REF_NIL ? IRCALL_lj_tab_len : |
| 1772 |
IRCALL_lj_tab_len_hint); |
| 1773 |
} |
| 1774 |
|
| 1775 |
/* -- Instruction dispatch ------------------------------------------------ */ |
| 1776 |
|
| 1777 |
/* Assemble a single instruction. */ |
| 1778 |
static void asm_ir(ASMState *as, IRIns *ir) |
| 1779 |
{ |
| 1780 |
switch ((IROp)ir->o) { |
| 1781 |
/* Miscellaneous ops. */ |
| 1782 |
case IR_LOOP: asm_loop(as); break; |
| 1783 |
case IR_NOP: case IR_XBAR: |
| 1784 |
lj_assertA(!ra_used(ir), |
| 1785 |
"IR %04d not unused", (int)(ir - as->ir) - REF_BIAS); |
| 1786 |
break; |
| 1787 |
case IR_USE: |
| 1788 |
ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; |
| 1789 |
case IR_PHI: asm_phi(as, ir); break; |
| 1790 |
case IR_HIOP: asm_hiop(as, ir); break; |
| 1791 |
case IR_GCSTEP: asm_gcstep(as, ir); break; |
| 1792 |
case IR_PROF: asm_prof(as, ir); break; |
| 1793 |
|
| 1794 |
/* Guarded assertions. */ |
| 1795 |
case IR_LT: case IR_GE: case IR_LE: case IR_GT: |
| 1796 |
case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: |
| 1797 |
case IR_ABC: |
| 1798 |
asm_comp(as, ir); |
| 1799 |
break; |
| 1800 |
case IR_EQ: case IR_NE: asm_fuseequal(as, ir); break; |
| 1801 |
|
| 1802 |
case IR_RETF: asm_retf(as, ir); break; |
| 1803 |
|
| 1804 |
/* Bit ops. */ |
| 1805 |
case IR_BNOT: asm_bnot(as, ir); break; |
| 1806 |
case IR_BSWAP: asm_bswap(as, ir); break; |
| 1807 |
case IR_BAND: asm_band(as, ir); break; |
| 1808 |
case IR_BOR: asm_bor(as, ir); break; |
| 1809 |
case IR_BXOR: asm_bxor(as, ir); break; |
| 1810 |
case IR_BSHL: asm_bshl(as, ir); break; |
| 1811 |
case IR_BSHR: asm_bshr(as, ir); break; |
| 1812 |
case IR_BSAR: asm_bsar(as, ir); break; |
| 1813 |
case IR_BROL: asm_brol(as, ir); break; |
| 1814 |
case IR_BROR: asm_bror(as, ir); break; |
| 1815 |
|
| 1816 |
/* Arithmetic ops. */ |
| 1817 |
case IR_ADD: asm_add(as, ir); break; |
| 1818 |
case IR_SUB: asm_sub(as, ir); break; |
| 1819 |
case IR_MUL: asm_mul(as, ir); break; |
| 1820 |
case IR_MOD: asm_mod(as, ir); break; |
| 1821 |
case IR_NEG: asm_neg(as, ir); break; |
| 1822 |
#if LJ_SOFTFP32 |
| 1823 |
case IR_DIV: case IR_POW: case IR_ABS: |
| 1824 |
case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: |
| 1825 |
/* Unused for LJ_SOFTFP32. */ |
| 1826 |
lj_assertA(0, "IR %04d with unused op %d", |
| 1827 |
(int)(ir - as->ir) - REF_BIAS, ir->o); |
| 1828 |
break; |
| 1829 |
#else |
| 1830 |
case IR_DIV: asm_div(as, ir); break; |
| 1831 |
case IR_POW: asm_pow(as, ir); break; |
| 1832 |
case IR_ABS: asm_abs(as, ir); break; |
| 1833 |
case IR_LDEXP: asm_ldexp(as, ir); break; |
| 1834 |
case IR_FPMATH: asm_fpmath(as, ir); break; |
| 1835 |
case IR_TOBIT: asm_tobit(as, ir); break; |
| 1836 |
#endif |
| 1837 |
case IR_MIN: asm_min(as, ir); break; |
| 1838 |
case IR_MAX: asm_max(as, ir); break; |
| 1839 |
|
| 1840 |
/* Overflow-checking arithmetic ops. */ |
| 1841 |
case IR_ADDOV: asm_addov(as, ir); break; |
| 1842 |
case IR_SUBOV: asm_subov(as, ir); break; |
| 1843 |
case IR_MULOV: asm_mulov(as, ir); break; |
| 1844 |
|
| 1845 |
/* Memory references. */ |
| 1846 |
case IR_AREF: asm_aref(as, ir); break; |
| 1847 |
case IR_HREF: asm_href(as, ir, 0); break; |
| 1848 |
case IR_HREFK: asm_hrefk(as, ir); break; |
| 1849 |
case IR_NEWREF: asm_newref(as, ir); break; |
| 1850 |
case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; |
| 1851 |
case IR_FREF: asm_fref(as, ir); break; |
| 1852 |
case IR_TMPREF: asm_tmpref(as, ir); break; |
| 1853 |
case IR_STRREF: asm_strref(as, ir); break; |
| 1854 |
case IR_LREF: asm_lref(as, ir); break; |
| 1855 |
|
| 1856 |
/* Loads and stores. */ |
| 1857 |
case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: |
| 1858 |
asm_ahuvload(as, ir); |
| 1859 |
break; |
| 1860 |
case IR_FLOAD: asm_fload(as, ir); break; |
| 1861 |
case IR_XLOAD: asm_xload(as, ir); break; |
| 1862 |
case IR_SLOAD: asm_sload(as, ir); break; |
| 1863 |
case IR_ALEN: asm_alen(as, ir); break; |
| 1864 |
|
| 1865 |
case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; |
| 1866 |
case IR_FSTORE: asm_fstore(as, ir); break; |
| 1867 |
case IR_XSTORE: asm_xstore(as, ir); break; |
| 1868 |
|
| 1869 |
/* Allocations. */ |
| 1870 |
case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; |
| 1871 |
case IR_TNEW: asm_tnew(as, ir); break; |
| 1872 |
case IR_TDUP: asm_tdup(as, ir); break; |
| 1873 |
case IR_CNEW: case IR_CNEWI: |
| 1874 |
#if LJ_HASFFI |
| 1875 |
asm_cnew(as, ir); |
| 1876 |
#else |
| 1877 |
lj_assertA(0, "IR %04d with unused op %d", |
| 1878 |
(int)(ir - as->ir) - REF_BIAS, ir->o); |
| 1879 |
#endif |
| 1880 |
break; |
| 1881 |
|
| 1882 |
/* Buffer operations. */ |
| 1883 |
case IR_BUFHDR: asm_bufhdr(as, ir); break; |
| 1884 |
case IR_BUFPUT: asm_bufput(as, ir); break; |
| 1885 |
case IR_BUFSTR: asm_bufstr(as, ir); break; |
| 1886 |
|
| 1887 |
/* Write barriers. */ |
| 1888 |
case IR_TBAR: asm_tbar(as, ir); break; |
| 1889 |
case IR_OBAR: asm_obar(as, ir); break; |
| 1890 |
|
| 1891 |
/* Type conversions. */ |
| 1892 |
case IR_CONV: asm_conv(as, ir); break; |
| 1893 |
case IR_TOSTR: asm_tostr(as, ir); break; |
| 1894 |
case IR_STRTO: asm_strto(as, ir); break; |
| 1895 |
|
| 1896 |
/* Calls. */ |
| 1897 |
case IR_CALLA: |
| 1898 |
as->gcsteps++; |
| 1899 |
/* fallthrough */ |
| 1900 |
case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; |
| 1901 |
case IR_CALLXS: asm_callx(as, ir); break; |
| 1902 |
case IR_CARG: break; |
| 1903 |
|
| 1904 |
default: |
| 1905 |
setintV(&as->J->errinfo, ir->o); |
| 1906 |
lj_trace_err_info(as->J, LJ_TRERR_NYIIR); |
| 1907 |
break; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
|
| 1911 |
/* -- Head of trace ------------------------------------------------------- */ |
| 1912 |
|
| 1913 |
/* Head of a root trace. */ |
| 1914 |
static void asm_head_root(ASMState *as) |
| 1915 |
{ |
| 1916 |
int32_t spadj; |
| 1917 |
asm_head_root_base(as); |
| 1918 |
emit_setvmstate(as, (int32_t)as->T->traceno); |
| 1919 |
spadj = asm_stack_adjust(as); |
| 1920 |
as->T->spadjust = (uint16_t)spadj; |
| 1921 |
emit_spsub(as, spadj); |
| 1922 |
/* Root traces assume a checked stack for the starting proto. */ |
| 1923 |
as->T->topslot = gcref(as->T->startpt)->pt.framesize; |
| 1924 |
} |
| 1925 |
|
| 1926 |
/* Head of a side trace. |
| 1927 |
** |
| 1928 |
** The current simplistic algorithm requires that all slots inherited |
| 1929 |
** from the parent are live in a register between pass 2 and pass 3. This |
| 1930 |
** avoids the complexity of stack slot shuffling. But of course this may |
| 1931 |
** overflow the register set in some cases and cause the dreaded error: |
| 1932 |
** "NYI: register coalescing too complex". A refined algorithm is needed. |
| 1933 |
*/ |
| 1934 |
static void asm_head_side(ASMState *as) |
| 1935 |
{ |
| 1936 |
IRRef1 sloadins[RID_MAX]; |
| 1937 |
RegSet allow = RSET_ALL; /* Inverse of all coalesced registers. */ |
| 1938 |
RegSet live = RSET_EMPTY; /* Live parent registers. */ |
| 1939 |
RegSet pallow = RSET_GPR; /* Registers needed by the parent stack check. */ |
| 1940 |
Reg pbase; |
| 1941 |
IRIns *irp = &as->parent->ir[REF_BASE]; /* Parent base. */ |
| 1942 |
int32_t spadj, spdelta; |
| 1943 |
int pass2 = 0; |
| 1944 |
int pass3 = 0; |
| 1945 |
IRRef i; |
| 1946 |
|
| 1947 |
if (as->snapno && as->topslot > as->parent->topslot) { |
| 1948 |
/* Force snap #0 alloc to prevent register overwrite in stack check. */ |
| 1949 |
asm_snap_alloc(as, 0); |
| 1950 |
} |
| 1951 |
pbase = asm_head_side_base(as, irp); |
| 1952 |
if (pbase != RID_NONE) { |
| 1953 |
rset_clear(allow, pbase); |
| 1954 |
rset_clear(pallow, pbase); |
| 1955 |
} |
| 1956 |
|
| 1957 |
/* Scan all parent SLOADs and collect register dependencies. */ |
| 1958 |
for (i = as->stopins; i > REF_BASE; i--) { |
| 1959 |
IRIns *ir = IR(i); |
| 1960 |
RegSP rs; |
| 1961 |
lj_assertA((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || |
| 1962 |
(LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL, |
| 1963 |
"IR %04d has bad parent op %d", |
| 1964 |
(int)(ir - as->ir) - REF_BIAS, ir->o); |
| 1965 |
rs = as->parentmap[i - REF_FIRST]; |
| 1966 |
if (ra_hasreg(ir->r)) { |
| 1967 |
rset_clear(allow, ir->r); |
| 1968 |
if (ra_hasspill(ir->s)) { |
| 1969 |
ra_save(as, ir, ir->r); |
| 1970 |
checkmclim(as); |
| 1971 |
} |
| 1972 |
} else if (ra_hasspill(ir->s)) { |
| 1973 |
irt_setmark(ir->t); |
| 1974 |
pass2 = 1; |
| 1975 |
} |
| 1976 |
if (ir->r == rs) { /* Coalesce matching registers right now. */ |
| 1977 |
ra_free(as, ir->r); |
| 1978 |
} else if (ra_hasspill(regsp_spill(rs))) { |
| 1979 |
if (ra_hasreg(ir->r)) |
| 1980 |
pass3 = 1; |
| 1981 |
} else if (ra_used(ir)) { |
| 1982 |
sloadins[rs] = (IRRef1)i; |
| 1983 |
rset_set(live, rs); /* Block live parent register. */ |
| 1984 |
} |
| 1985 |
if (!ra_hasspill(regsp_spill(rs))) rset_clear(pallow, regsp_reg(rs)); |
| 1986 |
} |
| 1987 |
|
| 1988 |
/* Calculate stack frame adjustment. */ |
| 1989 |
spadj = asm_stack_adjust(as); |
| 1990 |
spdelta = spadj - (int32_t)as->parent->spadjust; |
| 1991 |
if (spdelta < 0) { /* Don't shrink the stack frame. */ |
| 1992 |
spadj = (int32_t)as->parent->spadjust; |
| 1993 |
spdelta = 0; |
| 1994 |
} |
| 1995 |
as->T->spadjust = (uint16_t)spadj; |
| 1996 |
|
| 1997 |
/* Reload spilled target registers. */ |
| 1998 |
if (pass2) { |
| 1999 |
for (i = as->stopins; i > REF_BASE; i--) { |
| 2000 |
IRIns *ir = IR(i); |
| 2001 |
if (irt_ismarked(ir->t)) { |
| 2002 |
RegSet mask; |
| 2003 |
Reg r; |
| 2004 |
RegSP rs; |
| 2005 |
irt_clearmark(ir->t); |
| 2006 |
rs = as->parentmap[i - REF_FIRST]; |
| 2007 |
if (!ra_hasspill(regsp_spill(rs))) |
| 2008 |
ra_sethint(ir->r, rs); /* Hint may be gone, set it again. */ |
| 2009 |
else if (sps_scale(regsp_spill(rs))+spdelta == sps_scale(ir->s)) |
| 2010 |
continue; /* Same spill slot, do nothing. */ |
| 2011 |
mask = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & allow; |
| 2012 |
if (mask == RSET_EMPTY) |
| 2013 |
lj_trace_err(as->J, LJ_TRERR_NYICOAL); |
| 2014 |
r = ra_allocref(as, i, mask); |
| 2015 |
ra_save(as, ir, r); |
| 2016 |
rset_clear(allow, r); |
| 2017 |
if (r == rs) { /* Coalesce matching registers right now. */ |
| 2018 |
ra_free(as, r); |
| 2019 |
rset_clear(live, r); |
| 2020 |
} else if (ra_hasspill(regsp_spill(rs))) { |
| 2021 |
pass3 = 1; |
| 2022 |
} |
| 2023 |
checkmclim(as); |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
/* Store trace number and adjust stack frame relative to the parent. */ |
| 2029 |
emit_setvmstate(as, (int32_t)as->T->traceno); |
| 2030 |
emit_spsub(as, spdelta); |
| 2031 |
|
| 2032 |
#if !LJ_TARGET_X86ORX64 |
| 2033 |
/* Restore BASE register from parent spill slot. */ |
| 2034 |
if (ra_hasspill(irp->s)) |
| 2035 |
emit_spload(as, IR(REF_BASE), IR(REF_BASE)->r, sps_scale(irp->s)); |
| 2036 |
#endif |
| 2037 |
|
| 2038 |
/* Restore target registers from parent spill slots. */ |
| 2039 |
if (pass3) { |
| 2040 |
RegSet work = ~as->freeset & RSET_ALL; |
| 2041 |
while (work) { |
| 2042 |
Reg r = rset_pickbot(work); |
| 2043 |
IRRef ref = regcost_ref(as->cost[r]); |
| 2044 |
RegSP rs = as->parentmap[ref - REF_FIRST]; |
| 2045 |
rset_clear(work, r); |
| 2046 |
if (ra_hasspill(regsp_spill(rs))) { |
| 2047 |
int32_t ofs = sps_scale(regsp_spill(rs)); |
| 2048 |
ra_free(as, r); |
| 2049 |
emit_spload(as, IR(ref), r, ofs); |
| 2050 |
checkmclim(as); |
| 2051 |
} |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
/* Shuffle registers to match up target regs with parent regs. */ |
| 2056 |
for (;;) { |
| 2057 |
RegSet work; |
| 2058 |
|
| 2059 |
/* Repeatedly coalesce free live registers by moving to their target. */ |
| 2060 |
while ((work = as->freeset & live) != RSET_EMPTY) { |
| 2061 |
Reg rp = rset_pickbot(work); |
| 2062 |
IRIns *ir = IR(sloadins[rp]); |
| 2063 |
rset_clear(live, rp); |
| 2064 |
rset_clear(allow, rp); |
| 2065 |
ra_free(as, ir->r); |
| 2066 |
emit_movrr(as, ir, ir->r, rp); |
| 2067 |
checkmclim(as); |
| 2068 |
} |
| 2069 |
|
| 2070 |
/* We're done if no live registers remain. */ |
| 2071 |
if (live == RSET_EMPTY) |
| 2072 |
break; |
| 2073 |
|
| 2074 |
/* Break cycles by renaming one target to a temp. register. */ |
| 2075 |
if (live & RSET_GPR) { |
| 2076 |
RegSet tmpset = as->freeset & ~live & allow & RSET_GPR; |
| 2077 |
if (tmpset == RSET_EMPTY) |
| 2078 |
lj_trace_err(as->J, LJ_TRERR_NYICOAL); |
| 2079 |
ra_rename(as, rset_pickbot(live & RSET_GPR), rset_pickbot(tmpset)); |
| 2080 |
} |
| 2081 |
if (!LJ_SOFTFP && (live & RSET_FPR)) { |
| 2082 |
RegSet tmpset = as->freeset & ~live & allow & RSET_FPR; |
| 2083 |
if (tmpset == RSET_EMPTY) |
| 2084 |
lj_trace_err(as->J, LJ_TRERR_NYICOAL); |
| 2085 |
ra_rename(as, rset_pickbot(live & RSET_FPR), rset_pickbot(tmpset)); |
| 2086 |
} |
| 2087 |
checkmclim(as); |
| 2088 |
/* Continue with coalescing to fix up the broken cycle(s). */ |
| 2089 |
} |
| 2090 |
|
| 2091 |
/* Inherit top stack slot already checked by parent trace. */ |
| 2092 |
as->T->topslot = as->parent->topslot; |
| 2093 |
if (as->topslot > as->T->topslot) { /* Need to check for higher slot? */ |
| 2094 |
#ifdef EXITSTATE_CHECKEXIT |
| 2095 |
/* Highest exit + 1 indicates stack check. */ |
| 2096 |
ExitNo exitno = as->T->nsnap; |
| 2097 |
#else |
| 2098 |
/* Reuse the parent exit in the context of the parent trace. */ |
| 2099 |
ExitNo exitno = as->J->exitno; |
| 2100 |
#endif |
| 2101 |
as->T->topslot = (uint8_t)as->topslot; /* Remember for child traces. */ |
| 2102 |
asm_stack_check(as, as->topslot, irp, pallow, exitno); |
| 2103 |
} |
| 2104 |
} |
| 2105 |
|
| 2106 |
/* -- Tail of trace ------------------------------------------------------- */ |
| 2107 |
|
| 2108 |
/* Get base slot for a snapshot. */ |
| 2109 |
static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe) |
| 2110 |
{ |
| 2111 |
SnapEntry *map = &as->T->snapmap[snap->mapofs]; |
| 2112 |
MSize n; |
| 2113 |
for (n = snap->nent; n > 0; n--) { |
| 2114 |
SnapEntry sn = map[n-1]; |
| 2115 |
if ((sn & SNAP_FRAME)) { |
| 2116 |
*gotframe = 1; |
| 2117 |
return snap_slot(sn) - LJ_FR2; |
| 2118 |
} |
| 2119 |
} |
| 2120 |
return 0; |
| 2121 |
} |
| 2122 |
|
| 2123 |
/* Link to another trace. */ |
| 2124 |
static void asm_tail_link(ASMState *as) |
| 2125 |
{ |
| 2126 |
SnapNo snapno = as->T->nsnap-1; /* Last snapshot. */ |
| 2127 |
SnapShot *snap = &as->T->snap[snapno]; |
| 2128 |
int gotframe = 0; |
| 2129 |
BCReg baseslot = asm_baseslot(as, snap, &gotframe); |
| 2130 |
|
| 2131 |
as->topslot = snap->topslot; |
| 2132 |
checkmclim(as); |
| 2133 |
ra_allocref(as, REF_BASE, RID2RSET(RID_BASE)); |
| 2134 |
|
| 2135 |
if (as->T->link == 0) { |
| 2136 |
/* Setup fixed registers for exit to interpreter. */ |
| 2137 |
const BCIns *pc = snap_pc(&as->T->snapmap[snap->mapofs + snap->nent]); |
| 2138 |
int32_t mres; |
| 2139 |
if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ |
| 2140 |
BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; |
| 2141 |
if (bc_isret(bc_op(*retpc))) |
| 2142 |
pc = retpc; |
| 2143 |
} |
| 2144 |
#if LJ_GC64 |
| 2145 |
emit_loadu64(as, RID_LPC, u64ptr(pc)); |
| 2146 |
#else |
| 2147 |
ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); |
| 2148 |
ra_allockreg(as, i32ptr(pc), RID_LPC); |
| 2149 |
#endif |
| 2150 |
mres = (int32_t)(snap->nslots - baseslot - LJ_FR2); |
| 2151 |
switch (bc_op(*pc)) { |
| 2152 |
case BC_CALLM: case BC_CALLMT: |
| 2153 |
mres -= (int32_t)(1 + LJ_FR2 + bc_a(*pc) + bc_c(*pc)); break; |
| 2154 |
case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; |
| 2155 |
case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; |
| 2156 |
default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; |
| 2157 |
} |
| 2158 |
ra_allockreg(as, mres, RID_RET); /* Return MULTRES or 0. */ |
| 2159 |
} else if (baseslot) { |
| 2160 |
/* Save modified BASE for linking to trace with higher start frame. */ |
| 2161 |
emit_setgl(as, RID_BASE, jit_base); |
| 2162 |
} |
| 2163 |
emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); |
| 2164 |
|
| 2165 |
if (as->J->ktrace) { /* Patch ktrace slot with the final GCtrace pointer. */ |
| 2166 |
setgcref(IR(as->J->ktrace)[LJ_GC64].gcr, obj2gco(as->J->curfinal)); |
| 2167 |
IR(as->J->ktrace)->o = IR_KGC; |
| 2168 |
} |
| 2169 |
|
| 2170 |
/* Sync the interpreter state with the on-trace state. */ |
| 2171 |
asm_stack_restore(as, snap); |
| 2172 |
|
| 2173 |
/* Root traces that add frames need to check the stack at the end. */ |
| 2174 |
if (!as->parent && gotframe) |
| 2175 |
asm_stack_check(as, as->topslot, NULL, as->freeset & RSET_GPR, snapno); |
| 2176 |
} |
| 2177 |
|
| 2178 |
/* -- Trace setup --------------------------------------------------------- */ |
| 2179 |
|
| 2180 |
/* Clear reg/sp for all instructions and add register hints. */ |
| 2181 |
static void asm_setup_regsp(ASMState *as) |
| 2182 |
{ |
| 2183 |
GCtrace *T = as->T; |
| 2184 |
int sink = T->sinktags; |
| 2185 |
IRRef nins = T->nins; |
| 2186 |
IRIns *ir, *lastir; |
| 2187 |
int inloop; |
| 2188 |
#if LJ_TARGET_ARM |
| 2189 |
uint32_t rload = 0xa6402a64; |
| 2190 |
#endif |
| 2191 |
|
| 2192 |
ra_setup(as); |
| 2193 |
#if LJ_TARGET_ARM64 |
| 2194 |
ra_setkref(as, RID_GL, (intptr_t)J2G(as->J)); |
| 2195 |
#endif |
| 2196 |
|
| 2197 |
/* Clear reg/sp for constants. */ |
| 2198 |
for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) { |
| 2199 |
ir->prev = REGSP_INIT; |
| 2200 |
if (irt_is64(ir->t) && ir->o != IR_KNULL) { |
| 2201 |
#if LJ_GC64 |
| 2202 |
/* The false-positive of irt_is64() for ASMREF_L (REF_NIL) is OK here. */ |
| 2203 |
ir->i = 0; /* Will become non-zero only for RIP-relative addresses. */ |
| 2204 |
#else |
| 2205 |
/* Make life easier for backends by putting address of constant in i. */ |
| 2206 |
ir->i = (int32_t)(intptr_t)(ir+1); |
| 2207 |
#endif |
| 2208 |
ir++; |
| 2209 |
} |
| 2210 |
} |
| 2211 |
|
| 2212 |
/* REF_BASE is used for implicit references to the BASE register. */ |
| 2213 |
lastir->prev = REGSP_HINT(RID_BASE); |
| 2214 |
|
| 2215 |
as->snaprename = nins; |
| 2216 |
as->snapref = nins; |
| 2217 |
as->snapno = T->nsnap; |
| 2218 |
as->snapalloc = 0; |
| 2219 |
|
| 2220 |
as->stopins = REF_BASE; |
| 2221 |
as->orignins = nins; |
| 2222 |
as->curins = nins; |
| 2223 |
|
| 2224 |
/* Setup register hints for parent link instructions. */ |
| 2225 |
ir = IR(REF_FIRST); |
| 2226 |
if (as->parent) { |
| 2227 |
uint16_t *p; |
| 2228 |
lastir = lj_snap_regspmap(as->J, as->parent, as->J->exitno, ir); |
| 2229 |
if (lastir - ir > LJ_MAX_JSLOTS) |
| 2230 |
lj_trace_err(as->J, LJ_TRERR_NYICOAL); |
| 2231 |
as->stopins = (IRRef)((lastir-1) - as->ir); |
| 2232 |
for (p = as->parentmap; ir < lastir; ir++) { |
| 2233 |
RegSP rs = ir->prev; |
| 2234 |
*p++ = (uint16_t)rs; /* Copy original parent RegSP to parentmap. */ |
| 2235 |
if (!ra_hasspill(regsp_spill(rs))) |
| 2236 |
ir->prev = (uint16_t)REGSP_HINT(regsp_reg(rs)); |
| 2237 |
else |
| 2238 |
ir->prev = REGSP_INIT; |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
inloop = 0; |
| 2243 |
as->evenspill = SPS_FIRST; |
| 2244 |
for (lastir = IR(nins); ir < lastir; ir++) { |
| 2245 |
if (sink) { |
| 2246 |
if (ir->r == RID_SINK) |
| 2247 |
continue; |
| 2248 |
if (ir->r == RID_SUNK) { /* Revert after ASM restart. */ |
| 2249 |
ir->r = RID_SINK; |
| 2250 |
continue; |
| 2251 |
} |
| 2252 |
} |
| 2253 |
switch (ir->o) { |
| 2254 |
case IR_LOOP: |
| 2255 |
inloop = 1; |
| 2256 |
break; |
| 2257 |
#if LJ_TARGET_ARM |
| 2258 |
case IR_SLOAD: |
| 2259 |
if (!((ir->op2 & IRSLOAD_TYPECHECK) || (ir+1)->o == IR_HIOP)) |
| 2260 |
break; |
| 2261 |
/* fallthrough */ |
| 2262 |
case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: |
| 2263 |
if (!LJ_SOFTFP && irt_isnum(ir->t)) break; |
| 2264 |
ir->prev = (uint16_t)REGSP_HINT((rload & 15)); |
| 2265 |
rload = lj_ror(rload, 4); |
| 2266 |
continue; |
| 2267 |
case IR_TMPREF: |
| 2268 |
if ((ir->op2 & IRTMPREF_OUT2) && as->evenspill < 4) |
| 2269 |
as->evenspill = 4; /* TMPREF OUT2 needs two TValues on the stack. */ |
| 2270 |
break; |
| 2271 |
#endif |
| 2272 |
case IR_CALLXS: { |
| 2273 |
CCallInfo ci; |
| 2274 |
ci.flags = asm_callx_flags(as, ir); |
| 2275 |
ir->prev = asm_setup_call_slots(as, ir, &ci); |
| 2276 |
if (inloop) |
| 2277 |
as->modset |= RSET_SCRATCH; |
| 2278 |
continue; |
| 2279 |
} |
| 2280 |
case IR_CALLL: |
| 2281 |
/* lj_vm_next needs two TValues on the stack. */ |
| 2282 |
#if LJ_TARGET_X64 && LJ_ABI_WIN |
| 2283 |
if (ir->op2 == IRCALL_lj_vm_next && as->evenspill < SPS_FIRST + 4) |
| 2284 |
as->evenspill = SPS_FIRST + 4; |
| 2285 |
#else |
| 2286 |
if (SPS_FIRST < 4 && ir->op2 == IRCALL_lj_vm_next && as->evenspill < 4) |
| 2287 |
as->evenspill = 4; |
| 2288 |
#endif |
| 2289 |
/* fallthrough */ |
| 2290 |
case IR_CALLN: case IR_CALLA: case IR_CALLS: { |
| 2291 |
const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; |
| 2292 |
ir->prev = asm_setup_call_slots(as, ir, ci); |
| 2293 |
if (inloop) |
| 2294 |
as->modset |= (ci->flags & CCI_NOFPRCLOBBER) ? |
| 2295 |
(RSET_SCRATCH & ~RSET_FPR) : RSET_SCRATCH; |
| 2296 |
continue; |
| 2297 |
} |
| 2298 |
case IR_HIOP: |
| 2299 |
switch ((ir-1)->o) { |
| 2300 |
#if LJ_SOFTFP && LJ_TARGET_ARM |
| 2301 |
case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: |
| 2302 |
if (ra_hashint((ir-1)->r)) { |
| 2303 |
ir->prev = (ir-1)->prev + 1; |
| 2304 |
continue; |
| 2305 |
} |
| 2306 |
break; |
| 2307 |
#endif |
| 2308 |
#if !LJ_SOFTFP && LJ_NEED_FP64 && LJ_32 && LJ_HASFFI |
| 2309 |
case IR_CONV: |
| 2310 |
if (irt_isfp((ir-1)->t)) { |
| 2311 |
ir->prev = REGSP_HINT(RID_FPRET); |
| 2312 |
continue; |
| 2313 |
} |
| 2314 |
#endif |
| 2315 |
/* fallthrough */ |
| 2316 |
case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: |
| 2317 |
#if LJ_SOFTFP |
| 2318 |
case IR_MIN: case IR_MAX: |
| 2319 |
#endif |
| 2320 |
(ir-1)->prev = REGSP_HINT(RID_RETLO); |
| 2321 |
ir->prev = REGSP_HINT(RID_RETHI); |
| 2322 |
continue; |
| 2323 |
default: |
| 2324 |
break; |
| 2325 |
} |
| 2326 |
break; |
| 2327 |
#if LJ_SOFTFP |
| 2328 |
case IR_MIN: case IR_MAX: |
| 2329 |
if ((ir+1)->o != IR_HIOP) break; |
| 2330 |
#endif |
| 2331 |
/* fallthrough */ |
| 2332 |
/* C calls evict all scratch regs and return results in RID_RET. */ |
| 2333 |
case IR_SNEW: case IR_XSNEW: case IR_NEWREF: case IR_BUFPUT: |
| 2334 |
if (REGARG_NUMGPR < 3 && as->evenspill < 3) |
| 2335 |
as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ |
| 2336 |
#if LJ_TARGET_X86 && LJ_HASFFI |
| 2337 |
if (0) { |
| 2338 |
case IR_CNEW: |
| 2339 |
if (ir->op2 != REF_NIL && as->evenspill < 4) |
| 2340 |
as->evenspill = 4; /* lj_cdata_newv needs 4 args. */ |
| 2341 |
} |
| 2342 |
/* fallthrough */ |
| 2343 |
#else |
| 2344 |
/* fallthrough */ |
| 2345 |
case IR_CNEW: |
| 2346 |
#endif |
| 2347 |
/* fallthrough */ |
| 2348 |
case IR_TNEW: case IR_TDUP: case IR_CNEWI: case IR_TOSTR: |
| 2349 |
case IR_BUFSTR: |
| 2350 |
ir->prev = REGSP_HINT(RID_RET); |
| 2351 |
if (inloop) |
| 2352 |
as->modset = RSET_SCRATCH; |
| 2353 |
continue; |
| 2354 |
case IR_STRTO: case IR_OBAR: |
| 2355 |
if (inloop) |
| 2356 |
as->modset = RSET_SCRATCH; |
| 2357 |
break; |
| 2358 |
#if !LJ_SOFTFP |
| 2359 |
#if !LJ_TARGET_X86ORX64 |
| 2360 |
case IR_LDEXP: |
| 2361 |
#endif |
| 2362 |
#endif |
| 2363 |
/* fallthrough */ |
| 2364 |
case IR_POW: |
| 2365 |
if (!LJ_SOFTFP && irt_isnum(ir->t)) { |
| 2366 |
if (inloop) |
| 2367 |
as->modset |= RSET_SCRATCH; |
| 2368 |
#if LJ_TARGET_X86 |
| 2369 |
if (irt_isnum(IR(ir->op2)->t)) { |
| 2370 |
if (as->evenspill < 4) /* Leave room to call pow(). */ |
| 2371 |
as->evenspill = 4; |
| 2372 |
} |
| 2373 |
break; |
| 2374 |
#else |
| 2375 |
ir->prev = REGSP_HINT(RID_FPRET); |
| 2376 |
continue; |
| 2377 |
#endif |
| 2378 |
} |
| 2379 |
/* fallthrough */ /* for integer POW */ |
| 2380 |
case IR_DIV: case IR_MOD: |
| 2381 |
if ((LJ_64 && LJ_SOFTFP) || !irt_isnum(ir->t)) { |
| 2382 |
ir->prev = REGSP_HINT(RID_RET); |
| 2383 |
if (inloop) |
| 2384 |
as->modset |= (RSET_SCRATCH & RSET_GPR); |
| 2385 |
continue; |
| 2386 |
} |
| 2387 |
break; |
| 2388 |
#if LJ_64 && LJ_SOFTFP |
| 2389 |
case IR_ADD: case IR_SUB: case IR_MUL: |
| 2390 |
if (irt_isnum(ir->t)) { |
| 2391 |
ir->prev = REGSP_HINT(RID_RET); |
| 2392 |
if (inloop) |
| 2393 |
as->modset |= (RSET_SCRATCH & RSET_GPR); |
| 2394 |
continue; |
| 2395 |
} |
| 2396 |
break; |
| 2397 |
#endif |
| 2398 |
case IR_FPMATH: |
| 2399 |
#if LJ_TARGET_X86ORX64 |
| 2400 |
if (ir->op2 <= IRFPM_TRUNC) { |
| 2401 |
if (!(as->flags & JIT_F_SSE4_1)) { |
| 2402 |
ir->prev = REGSP_HINT(RID_XMM0); |
| 2403 |
if (inloop) |
| 2404 |
as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); |
| 2405 |
continue; |
| 2406 |
} |
| 2407 |
break; |
| 2408 |
} |
| 2409 |
#endif |
| 2410 |
if (inloop) |
| 2411 |
as->modset |= RSET_SCRATCH; |
| 2412 |
#if LJ_TARGET_X86 |
| 2413 |
break; |
| 2414 |
#else |
| 2415 |
ir->prev = REGSP_HINT(RID_FPRET); |
| 2416 |
continue; |
| 2417 |
#endif |
| 2418 |
#if LJ_TARGET_X86ORX64 |
| 2419 |
/* Non-constant shift counts need to be in RID_ECX on x86/x64. */ |
| 2420 |
case IR_BSHL: case IR_BSHR: case IR_BSAR: |
| 2421 |
if ((as->flags & JIT_F_BMI2)) /* Except if BMI2 is available. */ |
| 2422 |
break; |
| 2423 |
/* fallthrough */ |
| 2424 |
case IR_BROL: case IR_BROR: |
| 2425 |
if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { |
| 2426 |
IR(ir->op2)->r = REGSP_HINT(RID_ECX); |
| 2427 |
if (inloop) |
| 2428 |
rset_set(as->modset, RID_ECX); |
| 2429 |
} |
| 2430 |
break; |
| 2431 |
#endif |
| 2432 |
/* Do not propagate hints across type conversions or loads. */ |
| 2433 |
case IR_TOBIT: |
| 2434 |
case IR_XLOAD: |
| 2435 |
#if !LJ_TARGET_ARM |
| 2436 |
case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: |
| 2437 |
#endif |
| 2438 |
break; |
| 2439 |
case IR_CONV: |
| 2440 |
if (irt_isfp(ir->t) || (ir->op2 & IRCONV_SRCMASK) == IRT_NUM || |
| 2441 |
(ir->op2 & IRCONV_SRCMASK) == IRT_FLOAT) |
| 2442 |
break; |
| 2443 |
/* fallthrough */ |
| 2444 |
default: |
| 2445 |
/* Propagate hints across likely 'op reg, imm' or 'op reg'. */ |
| 2446 |
if (irref_isk(ir->op2) && !irref_isk(ir->op1) && |
| 2447 |
ra_hashint(regsp_reg(IR(ir->op1)->prev))) { |
| 2448 |
ir->prev = IR(ir->op1)->prev; |
| 2449 |
continue; |
| 2450 |
} |
| 2451 |
break; |
| 2452 |
} |
| 2453 |
ir->prev = REGSP_INIT; |
| 2454 |
} |
| 2455 |
if ((as->evenspill & 1)) |
| 2456 |
as->oddspill = as->evenspill++; |
| 2457 |
else |
| 2458 |
as->oddspill = 0; |
| 2459 |
} |
| 2460 |
|
| 2461 |
/* -- Assembler core ------------------------------------------------------ */ |
| 2462 |
|
| 2463 |
/* Assemble a trace. */ |
| 2464 |
void lj_asm_trace(jit_State *J, GCtrace *T) |
| 2465 |
{ |
| 2466 |
ASMState as_; |
| 2467 |
ASMState *as = &as_; |
| 2468 |
|
| 2469 |
/* Remove nops/renames left over from ASM restart due to LJ_TRERR_MCODELM. */ |
| 2470 |
{ |
| 2471 |
IRRef nins = T->nins; |
| 2472 |
IRIns *ir = &T->ir[nins-1]; |
| 2473 |
if (ir->o == IR_NOP || ir->o == IR_RENAME) { |
| 2474 |
do { ir--; nins--; } while (ir->o == IR_NOP || ir->o == IR_RENAME); |
| 2475 |
T->nins = nins; |
| 2476 |
} |
| 2477 |
} |
| 2478 |
|
| 2479 |
/* Ensure an initialized instruction beyond the last one for HIOP checks. */ |
| 2480 |
/* This also allows one RENAME to be added without reallocating curfinal. */ |
| 2481 |
as->orignins = lj_ir_nextins(J); |
| 2482 |
lj_ir_nop(&J->cur.ir[as->orignins]); |
| 2483 |
|
| 2484 |
/* Setup initial state. Copy some fields to reduce indirections. */ |
| 2485 |
as->J = J; |
| 2486 |
as->T = T; |
| 2487 |
J->curfinal = lj_trace_alloc(J->L, T); /* This copies the IR, too. */ |
| 2488 |
as->flags = J->flags; |
| 2489 |
as->loopref = J->loopref; |
| 2490 |
as->realign = NULL; |
| 2491 |
as->loopinv = 0; |
| 2492 |
as->parent = J->parent ? traceref(J, J->parent) : NULL; |
| 2493 |
#ifdef LUAJIT_RANDOM_RA |
| 2494 |
(void)lj_prng_u64(&J2G(J)->prng); /* Ensure PRNG step between traces. */ |
| 2495 |
#endif |
| 2496 |
|
| 2497 |
/* Reserve MCode memory. */ |
| 2498 |
as->mctop = as->mctoporig = lj_mcode_reserve(J, &as->mcbot); |
| 2499 |
as->mcp = as->mctop; |
| 2500 |
as->mclim = as->mcbot + MCLIM_REDZONE; |
| 2501 |
asm_setup_target(as); |
| 2502 |
|
| 2503 |
/* |
| 2504 |
** This is a loop, because the MCode may have to be (re-)assembled |
| 2505 |
** multiple times: |
| 2506 |
** |
| 2507 |
** 1. as->realign is set (and the assembly aborted), if the arch-specific |
| 2508 |
** backend wants the MCode to be aligned differently. |
| 2509 |
** |
| 2510 |
** This is currently only the case on x86/x64, where small loops get |
| 2511 |
** an aligned loop body plus a short branch. Not much effort is wasted, |
| 2512 |
** because the abort happens very quickly and only once. |
| 2513 |
** |
| 2514 |
** 2. The IR is immovable, since the MCode embeds pointers to various |
| 2515 |
** constants inside the IR. But RENAMEs may need to be added to the IR |
| 2516 |
** during assembly, which might grow and reallocate the IR. We check |
| 2517 |
** at the end if the IR (in J->cur.ir) has actually grown, resize the |
| 2518 |
** copy (in J->curfinal.ir) and try again. |
| 2519 |
** |
| 2520 |
** 95% of all traces have zero RENAMEs, 3% have one RENAME, 1.5% have |
| 2521 |
** 2 RENAMEs and only 0.5% have more than that. That's why we opt to |
| 2522 |
** always have one spare slot in the IR (see above), which means we |
| 2523 |
** have to redo the assembly for only ~2% of all traces. |
| 2524 |
** |
| 2525 |
** Very, very rarely, this needs to be done repeatedly, since the |
| 2526 |
** location of constants inside the IR (actually, reachability from |
| 2527 |
** a global pointer) may affect register allocation and thus the |
| 2528 |
** number of RENAMEs. |
| 2529 |
*/ |
| 2530 |
for (;;) { |
| 2531 |
as->mcp = as->mctop; |
| 2532 |
#ifdef LUA_USE_ASSERT |
| 2533 |
as->mcp_prev = as->mcp; |
| 2534 |
#endif |
| 2535 |
as->ir = J->curfinal->ir; /* Use the copied IR. */ |
| 2536 |
as->curins = J->cur.nins = as->orignins; |
| 2537 |
#ifdef LUAJIT_RANDOM_RA |
| 2538 |
as->prngstate = J2G(J)->prng; /* Must (re)start from identical state. */ |
| 2539 |
as->prngbits = 0; |
| 2540 |
#endif |
| 2541 |
|
| 2542 |
RA_DBG_START(); |
| 2543 |
RA_DBGX((as, "===== STOP =====")); |
| 2544 |
|
| 2545 |
/* General trace setup. Emit tail of trace. */ |
| 2546 |
asm_tail_prep(as); |
| 2547 |
as->mcloop = NULL; |
| 2548 |
as->flagmcp = NULL; |
| 2549 |
as->topslot = 0; |
| 2550 |
as->gcsteps = 0; |
| 2551 |
as->sectref = as->loopref; |
| 2552 |
as->fuseref = (as->flags & JIT_F_OPT_FUSE) ? as->loopref : FUSE_DISABLED; |
| 2553 |
asm_setup_regsp(as); |
| 2554 |
if (!as->loopref) |
| 2555 |
asm_tail_link(as); |
| 2556 |
|
| 2557 |
/* Assemble a trace in linear backwards order. */ |
| 2558 |
for (as->curins--; as->curins > as->stopins; as->curins--) { |
| 2559 |
IRIns *ir = IR(as->curins); |
| 2560 |
/* 64 bit types handled by SPLIT for 32 bit archs. */ |
| 2561 |
lj_assertA(!(LJ_32 && irt_isint64(ir->t)), |
| 2562 |
"IR %04d has unsplit 64 bit type", |
| 2563 |
(int)(ir - as->ir) - REF_BIAS); |
| 2564 |
asm_snap_prev(as); |
| 2565 |
if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) |
| 2566 |
continue; /* Dead-code elimination can be soooo easy. */ |
| 2567 |
if (irt_isguard(ir->t)) |
| 2568 |
asm_snap_prep(as); |
| 2569 |
RA_DBG_REF(); |
| 2570 |
checkmclim(as); |
| 2571 |
asm_ir(as, ir); |
| 2572 |
} |
| 2573 |
|
| 2574 |
if (as->realign && J->curfinal->nins >= T->nins) |
| 2575 |
continue; /* Retry in case only the MCode needs to be realigned. */ |
| 2576 |
|
| 2577 |
/* Emit head of trace. */ |
| 2578 |
RA_DBG_REF(); |
| 2579 |
checkmclim(as); |
| 2580 |
if (as->gcsteps > 0) { |
| 2581 |
as->curins = as->T->snap[0].ref; |
| 2582 |
asm_snap_prep(as); /* The GC check is a guard. */ |
| 2583 |
asm_gc_check(as); |
| 2584 |
as->curins = as->stopins; |
| 2585 |
} |
| 2586 |
ra_evictk(as); |
| 2587 |
if (as->parent) |
| 2588 |
asm_head_side(as); |
| 2589 |
else |
| 2590 |
asm_head_root(as); |
| 2591 |
#if LJ_ABI_BRANCH_TRACK |
| 2592 |
emit_branch_track(as); |
| 2593 |
#endif |
| 2594 |
asm_phi_fixup(as); |
| 2595 |
|
| 2596 |
if (J->curfinal->nins >= T->nins) { /* IR didn't grow? */ |
| 2597 |
lj_assertA(J->curfinal->nk == T->nk, "unexpected IR constant growth"); |
| 2598 |
memcpy(J->curfinal->ir + as->orignins, T->ir + as->orignins, |
| 2599 |
(T->nins - as->orignins) * sizeof(IRIns)); /* Copy RENAMEs. */ |
| 2600 |
T->nins = J->curfinal->nins; |
| 2601 |
/* Fill mcofs of any unprocessed snapshots. */ |
| 2602 |
as->curins = REF_FIRST; |
| 2603 |
asm_snap_prev(as); |
| 2604 |
break; /* Done. */ |
| 2605 |
} |
| 2606 |
|
| 2607 |
/* Otherwise try again with a bigger IR. */ |
| 2608 |
lj_trace_free(J2G(J), J->curfinal); |
| 2609 |
J->curfinal = NULL; /* In case lj_trace_alloc() OOMs. */ |
| 2610 |
J->curfinal = lj_trace_alloc(J->L, T); |
| 2611 |
as->realign = NULL; |
| 2612 |
} |
| 2613 |
|
| 2614 |
RA_DBGX((as, "===== START ====")); |
| 2615 |
RA_DBG_FLUSH(); |
| 2616 |
if (as->freeset != RSET_ALL) |
| 2617 |
lj_trace_err(as->J, LJ_TRERR_BADRA); /* Ouch! Should never happen. */ |
| 2618 |
|
| 2619 |
/* Set trace entry point before fixing up tail to allow link to self. */ |
| 2620 |
T->mcode = as->mcp; |
| 2621 |
T->mcloop = as->mcloop ? (MSize)((char *)as->mcloop - (char *)as->mcp) : 0; |
| 2622 |
if (as->loopref) |
| 2623 |
asm_loop_tail_fixup(as); |
| 2624 |
else |
| 2625 |
asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ |
| 2626 |
T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); |
| 2627 |
asm_snap_fixup_mcofs(as); |
| 2628 |
#if LJ_TARGET_MCODE_FIXUP |
| 2629 |
asm_mcode_fixup(T->mcode, T->szmcode); |
| 2630 |
#endif |
| 2631 |
lj_mcode_sync(T->mcode, as->mctoporig); |
| 2632 |
} |
| 2633 |
|
| 2634 |
#undef IR |
| 2635 |
|
| 2636 |
#endif |