diff options
Diffstat (limited to 'src/lj_asm.c')
-rw-r--r-- | src/lj_asm.c | 939 |
1 files changed, 775 insertions, 164 deletions
diff --git a/src/lj_asm.c b/src/lj_asm.c index 41006873..b613e6d3 100644 --- a/src/lj_asm.c +++ b/src/lj_asm.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #if LJ_HASJIT | 11 | #if LJ_HASJIT |
12 | 12 | ||
13 | #include "lj_gc.h" | 13 | #include "lj_gc.h" |
14 | #include "lj_buf.h" | ||
14 | #include "lj_str.h" | 15 | #include "lj_str.h" |
15 | #include "lj_tab.h" | 16 | #include "lj_tab.h" |
16 | #include "lj_frame.h" | 17 | #include "lj_frame.h" |
@@ -71,6 +72,7 @@ typedef struct ASMState { | |||
71 | IRRef snaprename; /* Rename highwater mark for snapshot check. */ | 72 | IRRef snaprename; /* Rename highwater mark for snapshot check. */ |
72 | SnapNo snapno; /* Current snapshot number. */ | 73 | SnapNo snapno; /* Current snapshot number. */ |
73 | SnapNo loopsnapno; /* Loop snapshot number. */ | 74 | SnapNo loopsnapno; /* Loop snapshot number. */ |
75 | int snapalloc; /* Current snapshot needs allocation. */ | ||
74 | BloomFilter snapfilt1, snapfilt2; /* Filled with snapshot refs. */ | 76 | BloomFilter snapfilt1, snapfilt2; /* Filled with snapshot refs. */ |
75 | 77 | ||
76 | IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ | 78 | IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ |
@@ -85,18 +87,25 @@ typedef struct ASMState { | |||
85 | 87 | ||
86 | MCode *mcbot; /* Bottom of reserved MCode. */ | 88 | MCode *mcbot; /* Bottom of reserved MCode. */ |
87 | MCode *mctop; /* Top of generated MCode. */ | 89 | MCode *mctop; /* Top of generated MCode. */ |
90 | MCode *mctoporig; /* Original top of generated MCode. */ | ||
88 | MCode *mcloop; /* Pointer to loop MCode (or NULL). */ | 91 | MCode *mcloop; /* Pointer to loop MCode (or NULL). */ |
89 | MCode *invmcp; /* Points to invertible loop branch (or NULL). */ | 92 | MCode *invmcp; /* Points to invertible loop branch (or NULL). */ |
90 | MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ | 93 | MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ |
91 | MCode *realign; /* Realign loop if not NULL. */ | 94 | MCode *realign; /* Realign loop if not NULL. */ |
92 | 95 | ||
93 | #ifdef RID_NUM_KREF | 96 | #ifdef RID_NUM_KREF |
94 | int32_t krefk[RID_NUM_KREF]; | 97 | intptr_t krefk[RID_NUM_KREF]; |
95 | #endif | 98 | #endif |
96 | IRRef1 phireg[RID_MAX]; /* PHI register references. */ | 99 | IRRef1 phireg[RID_MAX]; /* PHI register references. */ |
97 | uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ | 100 | uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ |
98 | } ASMState; | 101 | } ASMState; |
99 | 102 | ||
103 | #ifdef LUA_USE_ASSERT | ||
104 | #define lj_assertA(c, ...) lj_assertG_(J2G(as->J), (c), __VA_ARGS__) | ||
105 | #else | ||
106 | #define lj_assertA(c, ...) ((void)as) | ||
107 | #endif | ||
108 | |||
100 | #define IR(ref) (&as->ir[(ref)]) | 109 | #define IR(ref) (&as->ir[(ref)]) |
101 | 110 | ||
102 | #define ASMREF_TMP1 REF_TRUE /* Temp. register. */ | 111 | #define ASMREF_TMP1 REF_TRUE /* Temp. register. */ |
@@ -128,9 +137,8 @@ static LJ_AINLINE void checkmclim(ASMState *as) | |||
128 | #ifdef LUA_USE_ASSERT | 137 | #ifdef LUA_USE_ASSERT |
129 | if (as->mcp + MCLIM_REDZONE < as->mcp_prev) { | 138 | if (as->mcp + MCLIM_REDZONE < as->mcp_prev) { |
130 | IRIns *ir = IR(as->curins+1); | 139 | IRIns *ir = IR(as->curins+1); |
131 | fprintf(stderr, "RED ZONE OVERFLOW: %p IR %04d %02d %04d %04d\n", as->mcp, | 140 | lj_assertA(0, "red zone overflow: %p IR %04d %02d %04d %04d\n", as->mcp, |
132 | as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS); | 141 | as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS); |
133 | lua_assert(0); | ||
134 | } | 142 | } |
135 | #endif | 143 | #endif |
136 | if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as); | 144 | if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as); |
@@ -144,7 +152,7 @@ static LJ_AINLINE void checkmclim(ASMState *as) | |||
144 | #define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) | 152 | #define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) |
145 | #define ra_krefk(as, ref) (as->krefk[(ref)]) | 153 | #define ra_krefk(as, ref) (as->krefk[(ref)]) |
146 | 154 | ||
147 | static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, int32_t k) | 155 | static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, intptr_t k) |
148 | { | 156 | { |
149 | IRRef ref = (IRRef)(r - RID_MIN_KREF); | 157 | IRRef ref = (IRRef)(r - RID_MIN_KREF); |
150 | as->krefk[ref] = k; | 158 | as->krefk[ref] = k; |
@@ -171,6 +179,8 @@ IRFLDEF(FLOFS) | |||
171 | #include "lj_emit_x86.h" | 179 | #include "lj_emit_x86.h" |
172 | #elif LJ_TARGET_ARM | 180 | #elif LJ_TARGET_ARM |
173 | #include "lj_emit_arm.h" | 181 | #include "lj_emit_arm.h" |
182 | #elif LJ_TARGET_ARM64 | ||
183 | #include "lj_emit_arm64.h" | ||
174 | #elif LJ_TARGET_PPC | 184 | #elif LJ_TARGET_PPC |
175 | #include "lj_emit_ppc.h" | 185 | #include "lj_emit_ppc.h" |
176 | #elif LJ_TARGET_MIPS | 186 | #elif LJ_TARGET_MIPS |
@@ -179,6 +189,12 @@ IRFLDEF(FLOFS) | |||
179 | #error "Missing instruction emitter for target CPU" | 189 | #error "Missing instruction emitter for target CPU" |
180 | #endif | 190 | #endif |
181 | 191 | ||
192 | /* Generic load/store of register from/to stack slot. */ | ||
193 | #define emit_spload(as, ir, r, ofs) \ | ||
194 | emit_loadofs(as, ir, (r), RID_SP, (ofs)) | ||
195 | #define emit_spstore(as, ir, r, ofs) \ | ||
196 | emit_storeofs(as, ir, (r), RID_SP, (ofs)) | ||
197 | |||
182 | /* -- Register allocator debugging ---------------------------------------- */ | 198 | /* -- Register allocator debugging ---------------------------------------- */ |
183 | 199 | ||
184 | /* #define LUAJIT_DEBUG_RA */ | 200 | /* #define LUAJIT_DEBUG_RA */ |
@@ -236,7 +252,7 @@ static void ra_dprintf(ASMState *as, const char *fmt, ...) | |||
236 | *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; | 252 | *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; |
237 | } else { | 253 | } else { |
238 | *p++ = '?'; | 254 | *p++ = '?'; |
239 | lua_assert(0); | 255 | lj_assertA(0, "bad register %d for debug format \"%s\"", r, fmt); |
240 | } | 256 | } |
241 | } else if (e[1] == 'f' || e[1] == 'i') { | 257 | } else if (e[1] == 'f' || e[1] == 'i') { |
242 | IRRef ref; | 258 | IRRef ref; |
@@ -254,7 +270,7 @@ static void ra_dprintf(ASMState *as, const char *fmt, ...) | |||
254 | } else if (e[1] == 'x') { | 270 | } else if (e[1] == 'x') { |
255 | p += sprintf(p, "%08x", va_arg(argp, int32_t)); | 271 | p += sprintf(p, "%08x", va_arg(argp, int32_t)); |
256 | } else { | 272 | } else { |
257 | lua_assert(0); | 273 | lj_assertA(0, "bad debug format code"); |
258 | } | 274 | } |
259 | fmt = e+2; | 275 | fmt = e+2; |
260 | } | 276 | } |
@@ -313,37 +329,51 @@ static Reg ra_rematk(ASMState *as, IRRef ref) | |||
313 | Reg r; | 329 | Reg r; |
314 | if (ra_iskref(ref)) { | 330 | if (ra_iskref(ref)) { |
315 | r = ra_krefreg(ref); | 331 | r = ra_krefreg(ref); |
316 | lua_assert(!rset_test(as->freeset, r)); | 332 | lj_assertA(!rset_test(as->freeset, r), "rematk of free reg %d", r); |
317 | ra_free(as, r); | 333 | ra_free(as, r); |
318 | ra_modified(as, r); | 334 | ra_modified(as, r); |
335 | #if LJ_64 | ||
336 | emit_loadu64(as, r, ra_krefk(as, ref)); | ||
337 | #else | ||
319 | emit_loadi(as, r, ra_krefk(as, ref)); | 338 | emit_loadi(as, r, ra_krefk(as, ref)); |
339 | #endif | ||
320 | return r; | 340 | return r; |
321 | } | 341 | } |
322 | ir = IR(ref); | 342 | ir = IR(ref); |
323 | r = ir->r; | 343 | r = ir->r; |
324 | lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); | 344 | lj_assertA(ra_hasreg(r), "rematk of K%03d has no reg", REF_BIAS - ref); |
345 | lj_assertA(!ra_hasspill(ir->s), | ||
346 | "rematk of K%03d has spill slot [%x]", REF_BIAS - ref, ir->s); | ||
325 | ra_free(as, r); | 347 | ra_free(as, r); |
326 | ra_modified(as, r); | 348 | ra_modified(as, r); |
327 | ir->r = RID_INIT; /* Do not keep any hint. */ | 349 | ir->r = RID_INIT; /* Do not keep any hint. */ |
328 | RA_DBGX((as, "remat $i $r", ir, r)); | 350 | RA_DBGX((as, "remat $i $r", ir, r)); |
329 | #if !LJ_SOFTFP | 351 | #if !LJ_SOFTFP32 |
330 | if (ir->o == IR_KNUM) { | 352 | if (ir->o == IR_KNUM) { |
331 | emit_loadn(as, r, ir_knum(ir)); | 353 | emit_loadk64(as, r, ir); |
332 | } else | 354 | } else |
333 | #endif | 355 | #endif |
334 | if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { | 356 | if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { |
335 | ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ | 357 | ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ |
336 | emit_getgl(as, r, jit_base); | 358 | emit_getgl(as, r, jit_base); |
337 | } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { | 359 | } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { |
338 | lua_assert(irt_isnil(ir->t)); /* REF_NIL stores ASMREF_L register. */ | 360 | /* REF_NIL stores ASMREF_L register. */ |
339 | emit_getgl(as, r, jit_L); | 361 | lj_assertA(irt_isnil(ir->t), "rematk of bad ASMREF_L"); |
362 | emit_getgl(as, r, cur_L); | ||
340 | #if LJ_64 | 363 | #if LJ_64 |
341 | } else if (ir->o == IR_KINT64) { | 364 | } else if (ir->o == IR_KINT64) { |
342 | emit_loadu64(as, r, ir_kint64(ir)->u64); | 365 | emit_loadu64(as, r, ir_kint64(ir)->u64); |
366 | #if LJ_GC64 | ||
367 | } else if (ir->o == IR_KGC) { | ||
368 | emit_loadu64(as, r, (uintptr_t)ir_kgc(ir)); | ||
369 | } else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { | ||
370 | emit_loadu64(as, r, (uintptr_t)ir_kptr(ir)); | ||
371 | #endif | ||
343 | #endif | 372 | #endif |
344 | } else { | 373 | } else { |
345 | lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || | 374 | lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || |
346 | ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); | 375 | ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, |
376 | "rematk of bad IR op %d", ir->o); | ||
347 | emit_loadi(as, r, ir->i); | 377 | emit_loadi(as, r, ir->i); |
348 | } | 378 | } |
349 | return r; | 379 | return r; |
@@ -353,7 +383,8 @@ static Reg ra_rematk(ASMState *as, IRRef ref) | |||
353 | static int32_t ra_spill(ASMState *as, IRIns *ir) | 383 | static int32_t ra_spill(ASMState *as, IRIns *ir) |
354 | { | 384 | { |
355 | int32_t slot = ir->s; | 385 | int32_t slot = ir->s; |
356 | lua_assert(ir >= as->ir + REF_TRUE); | 386 | lj_assertA(ir >= as->ir + REF_TRUE, |
387 | "spill of K%03d", REF_BIAS - (int)(ir - as->ir)); | ||
357 | if (!ra_hasspill(slot)) { | 388 | if (!ra_hasspill(slot)) { |
358 | if (irt_is64(ir->t)) { | 389 | if (irt_is64(ir->t)) { |
359 | slot = as->evenspill; | 390 | slot = as->evenspill; |
@@ -378,7 +409,9 @@ static Reg ra_releasetmp(ASMState *as, IRRef ref) | |||
378 | { | 409 | { |
379 | IRIns *ir = IR(ref); | 410 | IRIns *ir = IR(ref); |
380 | Reg r = ir->r; | 411 | Reg r = ir->r; |
381 | lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); | 412 | lj_assertA(ra_hasreg(r), "release of TMP%d has no reg", ref-ASMREF_TMP1+1); |
413 | lj_assertA(!ra_hasspill(ir->s), | ||
414 | "release of TMP%d has spill slot [%x]", ref-ASMREF_TMP1+1, ir->s); | ||
382 | ra_free(as, r); | 415 | ra_free(as, r); |
383 | ra_modified(as, r); | 416 | ra_modified(as, r); |
384 | ir->r = RID_INIT; | 417 | ir->r = RID_INIT; |
@@ -394,7 +427,7 @@ static Reg ra_restore(ASMState *as, IRRef ref) | |||
394 | IRIns *ir = IR(ref); | 427 | IRIns *ir = IR(ref); |
395 | int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ | 428 | int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ |
396 | Reg r = ir->r; | 429 | Reg r = ir->r; |
397 | lua_assert(ra_hasreg(r)); | 430 | lj_assertA(ra_hasreg(r), "restore of IR %04d has no reg", ref - REF_BIAS); |
398 | ra_sethint(ir->r, r); /* Keep hint. */ | 431 | ra_sethint(ir->r, r); /* Keep hint. */ |
399 | ra_free(as, r); | 432 | ra_free(as, r); |
400 | if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ | 433 | if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ |
@@ -423,14 +456,15 @@ static Reg ra_evict(ASMState *as, RegSet allow) | |||
423 | { | 456 | { |
424 | IRRef ref; | 457 | IRRef ref; |
425 | RegCost cost = ~(RegCost)0; | 458 | RegCost cost = ~(RegCost)0; |
426 | lua_assert(allow != RSET_EMPTY); | 459 | lj_assertA(allow != RSET_EMPTY, "evict from empty set"); |
427 | if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { | 460 | if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { |
428 | GPRDEF(MINCOST) | 461 | GPRDEF(MINCOST) |
429 | } else { | 462 | } else { |
430 | FPRDEF(MINCOST) | 463 | FPRDEF(MINCOST) |
431 | } | 464 | } |
432 | ref = regcost_ref(cost); | 465 | ref = regcost_ref(cost); |
433 | lua_assert(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins)); | 466 | lj_assertA(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins), |
467 | "evict of out-of-range IR %04d", ref - REF_BIAS); | ||
434 | /* Preferably pick any weak ref instead of a non-weak, non-const ref. */ | 468 | /* Preferably pick any weak ref instead of a non-weak, non-const ref. */ |
435 | if (!irref_isk(ref) && (as->weakset & allow)) { | 469 | if (!irref_isk(ref) && (as->weakset & allow)) { |
436 | IRIns *ir = IR(ref); | 470 | IRIns *ir = IR(ref); |
@@ -512,7 +546,7 @@ static void ra_evictk(ASMState *as) | |||
512 | 546 | ||
513 | #ifdef RID_NUM_KREF | 547 | #ifdef RID_NUM_KREF |
514 | /* Allocate a register for a constant. */ | 548 | /* Allocate a register for a constant. */ |
515 | static Reg ra_allock(ASMState *as, int32_t k, RegSet allow) | 549 | static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow) |
516 | { | 550 | { |
517 | /* First try to find a register which already holds the same constant. */ | 551 | /* First try to find a register which already holds the same constant. */ |
518 | RegSet pick, work = ~as->freeset & RSET_GPR; | 552 | RegSet pick, work = ~as->freeset & RSET_GPR; |
@@ -521,9 +555,31 @@ static Reg ra_allock(ASMState *as, int32_t k, RegSet allow) | |||
521 | IRRef ref; | 555 | IRRef ref; |
522 | r = rset_pickbot(work); | 556 | r = rset_pickbot(work); |
523 | ref = regcost_ref(as->cost[r]); | 557 | ref = regcost_ref(as->cost[r]); |
558 | #if LJ_64 | ||
559 | if (ref < ASMREF_L) { | ||
560 | if (ra_iskref(ref)) { | ||
561 | if (k == ra_krefk(as, ref)) | ||
562 | return r; | ||
563 | } else { | ||
564 | IRIns *ir = IR(ref); | ||
565 | if ((ir->o == IR_KINT64 && k == (int64_t)ir_kint64(ir)->u64) || | ||
566 | #if LJ_GC64 | ||
567 | (ir->o == IR_KINT && k == ir->i) || | ||
568 | (ir->o == IR_KGC && k == (intptr_t)ir_kgc(ir)) || | ||
569 | ((ir->o == IR_KPTR || ir->o == IR_KKPTR) && | ||
570 | k == (intptr_t)ir_kptr(ir)) | ||
571 | #else | ||
572 | (ir->o != IR_KINT64 && k == ir->i) | ||
573 | #endif | ||
574 | ) | ||
575 | return r; | ||
576 | } | ||
577 | } | ||
578 | #else | ||
524 | if (ref < ASMREF_L && | 579 | if (ref < ASMREF_L && |
525 | k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) | 580 | k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) |
526 | return r; | 581 | return r; |
582 | #endif | ||
527 | rset_clear(work, r); | 583 | rset_clear(work, r); |
528 | } | 584 | } |
529 | pick = as->freeset & allow; | 585 | pick = as->freeset & allow; |
@@ -543,7 +599,7 @@ static Reg ra_allock(ASMState *as, int32_t k, RegSet allow) | |||
543 | } | 599 | } |
544 | 600 | ||
545 | /* Allocate a specific register for a constant. */ | 601 | /* Allocate a specific register for a constant. */ |
546 | static void ra_allockreg(ASMState *as, int32_t k, Reg r) | 602 | static void ra_allockreg(ASMState *as, intptr_t k, Reg r) |
547 | { | 603 | { |
548 | Reg kr = ra_allock(as, k, RID2RSET(r)); | 604 | Reg kr = ra_allock(as, k, RID2RSET(r)); |
549 | if (kr != r) { | 605 | if (kr != r) { |
@@ -566,7 +622,8 @@ static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow) | |||
566 | IRIns *ir = IR(ref); | 622 | IRIns *ir = IR(ref); |
567 | RegSet pick = as->freeset & allow; | 623 | RegSet pick = as->freeset & allow; |
568 | Reg r; | 624 | Reg r; |
569 | lua_assert(ra_noreg(ir->r)); | 625 | lj_assertA(ra_noreg(ir->r), |
626 | "IR %04d already has reg %d", ref - REF_BIAS, ir->r); | ||
570 | if (pick) { | 627 | if (pick) { |
571 | /* First check register hint from propagation or PHI. */ | 628 | /* First check register hint from propagation or PHI. */ |
572 | if (ra_hashint(ir->r)) { | 629 | if (ra_hashint(ir->r)) { |
@@ -613,15 +670,27 @@ static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow) | |||
613 | return r; | 670 | return r; |
614 | } | 671 | } |
615 | 672 | ||
673 | /* Add a register rename to the IR. */ | ||
674 | static void ra_addrename(ASMState *as, Reg down, IRRef ref, SnapNo snapno) | ||
675 | { | ||
676 | IRRef ren; | ||
677 | lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, snapno); | ||
678 | ren = tref_ref(lj_ir_emit(as->J)); | ||
679 | as->J->cur.ir[ren].r = (uint8_t)down; | ||
680 | as->J->cur.ir[ren].s = SPS_NONE; | ||
681 | } | ||
682 | |||
616 | /* Rename register allocation and emit move. */ | 683 | /* Rename register allocation and emit move. */ |
617 | static void ra_rename(ASMState *as, Reg down, Reg up) | 684 | static void ra_rename(ASMState *as, Reg down, Reg up) |
618 | { | 685 | { |
619 | IRRef ren, ref = regcost_ref(as->cost[up] = as->cost[down]); | 686 | IRRef ref = regcost_ref(as->cost[up] = as->cost[down]); |
620 | IRIns *ir = IR(ref); | 687 | IRIns *ir = IR(ref); |
621 | ir->r = (uint8_t)up; | 688 | ir->r = (uint8_t)up; |
622 | as->cost[down] = 0; | 689 | as->cost[down] = 0; |
623 | lua_assert((down < RID_MAX_GPR) == (up < RID_MAX_GPR)); | 690 | lj_assertA((down < RID_MAX_GPR) == (up < RID_MAX_GPR), |
624 | lua_assert(!rset_test(as->freeset, down) && rset_test(as->freeset, up)); | 691 | "rename between GPR/FPR %d and %d", down, up); |
692 | lj_assertA(!rset_test(as->freeset, down), "rename from free reg %d", down); | ||
693 | lj_assertA(rset_test(as->freeset, up), "rename to non-free reg %d", up); | ||
625 | ra_free(as, down); /* 'down' is free ... */ | 694 | ra_free(as, down); /* 'down' is free ... */ |
626 | ra_modified(as, down); | 695 | ra_modified(as, down); |
627 | rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ | 696 | rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ |
@@ -629,11 +698,14 @@ static void ra_rename(ASMState *as, Reg down, Reg up) | |||
629 | RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); | 698 | RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); |
630 | emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ | 699 | emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ |
631 | if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ | 700 | if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ |
632 | lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, as->snapno); | 701 | /* |
633 | ren = tref_ref(lj_ir_emit(as->J)); | 702 | ** The rename is effective at the subsequent (already emitted) exit |
634 | as->ir = as->T->ir; /* The IR may have been reallocated. */ | 703 | ** branch. This is for the current snapshot (as->snapno). Except if we |
635 | IR(ren)->r = (uint8_t)down; | 704 | ** haven't yet allocated any refs for the snapshot (as->snapalloc == 1), |
636 | IR(ren)->s = SPS_NONE; | 705 | ** then it belongs to the next snapshot. |
706 | ** See also the discussion at asm_snap_checkrename(). | ||
707 | */ | ||
708 | ra_addrename(as, down, ref, as->snapno + as->snapalloc); | ||
637 | } | 709 | } |
638 | } | 710 | } |
639 | 711 | ||
@@ -666,7 +738,7 @@ static void ra_destreg(ASMState *as, IRIns *ir, Reg r) | |||
666 | { | 738 | { |
667 | Reg dest = ra_dest(as, ir, RID2RSET(r)); | 739 | Reg dest = ra_dest(as, ir, RID2RSET(r)); |
668 | if (dest != r) { | 740 | if (dest != r) { |
669 | lua_assert(rset_test(as->freeset, r)); | 741 | lj_assertA(rset_test(as->freeset, r), "dest reg %d is not free", r); |
670 | ra_modified(as, r); | 742 | ra_modified(as, r); |
671 | emit_movrr(as, ir, dest, r); | 743 | emit_movrr(as, ir, dest, r); |
672 | } | 744 | } |
@@ -683,20 +755,25 @@ static void ra_left(ASMState *as, Reg dest, IRRef lref) | |||
683 | if (ra_noreg(left)) { | 755 | if (ra_noreg(left)) { |
684 | if (irref_isk(lref)) { | 756 | if (irref_isk(lref)) { |
685 | if (ir->o == IR_KNUM) { | 757 | if (ir->o == IR_KNUM) { |
686 | cTValue *tv = ir_knum(ir); | ||
687 | /* FP remat needs a load except for +0. Still better than eviction. */ | 758 | /* FP remat needs a load except for +0. Still better than eviction. */ |
688 | if (tvispzero(tv) || !(as->freeset & RSET_FPR)) { | 759 | if (tvispzero(ir_knum(ir)) || !(as->freeset & RSET_FPR)) { |
689 | emit_loadn(as, dest, tv); | 760 | emit_loadk64(as, dest, ir); |
690 | return; | 761 | return; |
691 | } | 762 | } |
692 | #if LJ_64 | 763 | #if LJ_64 |
693 | } else if (ir->o == IR_KINT64) { | 764 | } else if (ir->o == IR_KINT64) { |
694 | emit_loadu64(as, dest, ir_kint64(ir)->u64); | 765 | emit_loadk64(as, dest, ir); |
766 | return; | ||
767 | #if LJ_GC64 | ||
768 | } else if (ir->o == IR_KGC || ir->o == IR_KPTR || ir->o == IR_KKPTR) { | ||
769 | emit_loadk64(as, dest, ir); | ||
695 | return; | 770 | return; |
696 | #endif | 771 | #endif |
697 | } else { | 772 | #endif |
698 | lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || | 773 | } else if (ir->o != IR_KPRI) { |
699 | ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); | 774 | lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || |
775 | ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, | ||
776 | "K%03d has bad IR op %d", REF_BIAS - lref, ir->o); | ||
700 | emit_loadi(as, dest, ir->i); | 777 | emit_loadi(as, dest, ir->i); |
701 | return; | 778 | return; |
702 | } | 779 | } |
@@ -841,11 +918,14 @@ static void asm_snap_alloc1(ASMState *as, IRRef ref) | |||
841 | #endif | 918 | #endif |
842 | { /* Allocate stored values for TNEW, TDUP and CNEW. */ | 919 | { /* Allocate stored values for TNEW, TDUP and CNEW. */ |
843 | IRIns *irs; | 920 | IRIns *irs; |
844 | lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW); | 921 | lj_assertA(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW, |
922 | "sink of IR %04d has bad op %d", ref - REF_BIAS, ir->o); | ||
845 | for (irs = IR(as->snapref-1); irs > ir; irs--) | 923 | for (irs = IR(as->snapref-1); irs > ir; irs--) |
846 | if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { | 924 | if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { |
847 | lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE || | 925 | lj_assertA(irs->o == IR_ASTORE || irs->o == IR_HSTORE || |
848 | irs->o == IR_FSTORE || irs->o == IR_XSTORE); | 926 | irs->o == IR_FSTORE || irs->o == IR_XSTORE, |
927 | "sunk store IR %04d has bad op %d", | ||
928 | (int)(irs - as->ir) - REF_BIAS, irs->o); | ||
849 | asm_snap_alloc1(as, irs->op2); | 929 | asm_snap_alloc1(as, irs->op2); |
850 | if (LJ_32 && (irs+1)->o == IR_HIOP) | 930 | if (LJ_32 && (irs+1)->o == IR_HIOP) |
851 | asm_snap_alloc1(as, (irs+1)->op2); | 931 | asm_snap_alloc1(as, (irs+1)->op2); |
@@ -881,9 +961,9 @@ static void asm_snap_alloc1(ASMState *as, IRRef ref) | |||
881 | } | 961 | } |
882 | 962 | ||
883 | /* Allocate refs escaping to a snapshot. */ | 963 | /* Allocate refs escaping to a snapshot. */ |
884 | static void asm_snap_alloc(ASMState *as) | 964 | static void asm_snap_alloc(ASMState *as, int snapno) |
885 | { | 965 | { |
886 | SnapShot *snap = &as->T->snap[as->snapno]; | 966 | SnapShot *snap = &as->T->snap[snapno]; |
887 | SnapEntry *map = &as->T->snapmap[snap->mapofs]; | 967 | SnapEntry *map = &as->T->snapmap[snap->mapofs]; |
888 | MSize n, nent = snap->nent; | 968 | MSize n, nent = snap->nent; |
889 | as->snapfilt1 = as->snapfilt2 = 0; | 969 | as->snapfilt1 = as->snapfilt2 = 0; |
@@ -893,7 +973,9 @@ static void asm_snap_alloc(ASMState *as) | |||
893 | if (!irref_isk(ref)) { | 973 | if (!irref_isk(ref)) { |
894 | asm_snap_alloc1(as, ref); | 974 | asm_snap_alloc1(as, ref); |
895 | if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { | 975 | if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { |
896 | lua_assert(irt_type(IR(ref+1)->t) == IRT_SOFTFP); | 976 | lj_assertA(irt_type(IR(ref+1)->t) == IRT_SOFTFP, |
977 | "snap %d[%d] points to bad SOFTFP IR %04d", | ||
978 | snapno, n, ref - REF_BIAS); | ||
897 | asm_snap_alloc1(as, ref+1); | 979 | asm_snap_alloc1(as, ref+1); |
898 | } | 980 | } |
899 | } | 981 | } |
@@ -919,67 +1001,55 @@ static int asm_snap_checkrename(ASMState *as, IRRef ren) | |||
919 | return 0; /* Not found. */ | 1001 | return 0; /* Not found. */ |
920 | } | 1002 | } |
921 | 1003 | ||
922 | /* Prepare snapshot for next guard instruction. */ | 1004 | /* Prepare snapshot for next guard or throwing instruction. */ |
923 | static void asm_snap_prep(ASMState *as) | 1005 | static void asm_snap_prep(ASMState *as) |
924 | { | 1006 | { |
925 | if (as->curins < as->snapref) { | 1007 | if (as->snapalloc) { |
926 | do { | 1008 | /* Alloc on first invocation for each snapshot. */ |
927 | if (as->snapno == 0) return; /* Called by sunk stores before snap #0. */ | 1009 | as->snapalloc = 0; |
928 | as->snapno--; | 1010 | asm_snap_alloc(as, as->snapno); |
929 | as->snapref = as->T->snap[as->snapno].ref; | ||
930 | } while (as->curins < as->snapref); | ||
931 | asm_snap_alloc(as); | ||
932 | as->snaprename = as->T->nins; | 1011 | as->snaprename = as->T->nins; |
933 | } else { | 1012 | } else { |
934 | /* Process any renames above the highwater mark. */ | 1013 | /* Check any renames above the highwater mark. */ |
935 | for (; as->snaprename < as->T->nins; as->snaprename++) { | 1014 | for (; as->snaprename < as->T->nins; as->snaprename++) { |
936 | IRIns *ir = IR(as->snaprename); | 1015 | IRIns *ir = &as->T->ir[as->snaprename]; |
937 | if (asm_snap_checkrename(as, ir->op1)) | 1016 | if (asm_snap_checkrename(as, ir->op1)) |
938 | ir->op2 = REF_BIAS-1; /* Kill rename. */ | 1017 | ir->op2 = REF_BIAS-1; /* Kill rename. */ |
939 | } | 1018 | } |
940 | } | 1019 | } |
941 | } | 1020 | } |
942 | 1021 | ||
943 | /* -- Miscellaneous helpers ----------------------------------------------- */ | 1022 | /* Move to previous snapshot when we cross the current snapshot ref. */ |
944 | 1023 | static void asm_snap_prev(ASMState *as) | |
945 | /* Collect arguments from CALL* and CARG instructions. */ | ||
946 | static void asm_collectargs(ASMState *as, IRIns *ir, | ||
947 | const CCallInfo *ci, IRRef *args) | ||
948 | { | 1024 | { |
949 | uint32_t n = CCI_NARGS(ci); | 1025 | if (as->curins < as->snapref) { |
950 | lua_assert(n <= CCI_NARGS_MAX*2); /* Account for split args. */ | 1026 | ptrdiff_t ofs = as->mctoporig - as->mcp; |
951 | if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } | 1027 | if (ofs >= 0x10000) lj_trace_err(as->J, LJ_TRERR_MCODEOV); |
952 | while (n-- > 1) { | 1028 | do { |
953 | ir = IR(ir->op1); | 1029 | if (as->snapno == 0) return; |
954 | lua_assert(ir->o == IR_CARG); | 1030 | as->snapno--; |
955 | args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; | 1031 | as->snapref = as->T->snap[as->snapno].ref; |
1032 | as->T->snap[as->snapno].mcofs = ofs; /* Remember mcode offset. */ | ||
1033 | } while (as->curins < as->snapref); /* May have no ins inbetween. */ | ||
1034 | as->snapalloc = 1; | ||
956 | } | 1035 | } |
957 | args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; | ||
958 | lua_assert(IR(ir->op1)->o != IR_CARG); | ||
959 | } | 1036 | } |
960 | 1037 | ||
961 | /* Reconstruct CCallInfo flags for CALLX*. */ | 1038 | /* Fixup snapshot mcode offsetst. */ |
962 | static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) | 1039 | static void asm_snap_fixup_mcofs(ASMState *as) |
963 | { | 1040 | { |
964 | uint32_t nargs = 0; | 1041 | uint32_t sz = (uint32_t)(as->mctoporig - as->mcp); |
965 | if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ | 1042 | SnapShot *snap = as->T->snap; |
966 | IRIns *ira = IR(ir->op1); | 1043 | SnapNo i; |
967 | nargs++; | 1044 | for (i = as->T->nsnap-1; i > 0; i--) { |
968 | while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } | 1045 | /* Compute offset from mcode start and store in correct snapshot. */ |
1046 | snap[i].mcofs = (uint16_t)(sz - snap[i-1].mcofs); | ||
969 | } | 1047 | } |
970 | #if LJ_HASFFI | 1048 | snap[0].mcofs = 0; |
971 | if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ | ||
972 | CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; | ||
973 | CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); | ||
974 | nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); | ||
975 | #if LJ_TARGET_X86 | ||
976 | nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); | ||
977 | #endif | ||
978 | } | ||
979 | #endif | ||
980 | return (nargs | (ir->t.irt << CCI_OTSHIFT)); | ||
981 | } | 1049 | } |
982 | 1050 | ||
1051 | /* -- Miscellaneous helpers ----------------------------------------------- */ | ||
1052 | |||
983 | /* Calculate stack adjustment. */ | 1053 | /* Calculate stack adjustment. */ |
984 | static int32_t asm_stack_adjust(ASMState *as) | 1054 | static int32_t asm_stack_adjust(ASMState *as) |
985 | { | 1055 | { |
@@ -989,21 +1059,26 @@ static int32_t asm_stack_adjust(ASMState *as) | |||
989 | } | 1059 | } |
990 | 1060 | ||
991 | /* Must match with hash*() in lj_tab.c. */ | 1061 | /* Must match with hash*() in lj_tab.c. */ |
992 | static uint32_t ir_khash(IRIns *ir) | 1062 | static uint32_t ir_khash(ASMState *as, IRIns *ir) |
993 | { | 1063 | { |
994 | uint32_t lo, hi; | 1064 | uint32_t lo, hi; |
1065 | UNUSED(as); | ||
995 | if (irt_isstr(ir->t)) { | 1066 | if (irt_isstr(ir->t)) { |
996 | return ir_kstr(ir)->hash; | 1067 | return ir_kstr(ir)->sid; |
997 | } else if (irt_isnum(ir->t)) { | 1068 | } else if (irt_isnum(ir->t)) { |
998 | lo = ir_knum(ir)->u32.lo; | 1069 | lo = ir_knum(ir)->u32.lo; |
999 | hi = ir_knum(ir)->u32.hi << 1; | 1070 | hi = ir_knum(ir)->u32.hi << 1; |
1000 | } else if (irt_ispri(ir->t)) { | 1071 | } else if (irt_ispri(ir->t)) { |
1001 | lua_assert(!irt_isnil(ir->t)); | 1072 | lj_assertA(!irt_isnil(ir->t), "hash of nil key"); |
1002 | return irt_type(ir->t)-IRT_FALSE; | 1073 | return irt_type(ir->t)-IRT_FALSE; |
1003 | } else { | 1074 | } else { |
1004 | lua_assert(irt_isgcv(ir->t)); | 1075 | lj_assertA(irt_isgcv(ir->t), "hash of bad IR type %d", irt_type(ir->t)); |
1005 | lo = u32ptr(ir_kgc(ir)); | 1076 | lo = u32ptr(ir_kgc(ir)); |
1077 | #if LJ_GC64 | ||
1078 | hi = (uint32_t)(u64ptr(ir_kgc(ir)) >> 32) | (irt_toitype(ir->t) << 15); | ||
1079 | #else | ||
1006 | hi = lo + HASH_BIAS; | 1080 | hi = lo + HASH_BIAS; |
1081 | #endif | ||
1007 | } | 1082 | } |
1008 | return hashrot(lo, hi); | 1083 | return hashrot(lo, hi); |
1009 | } | 1084 | } |
@@ -1017,6 +1092,7 @@ static void asm_snew(ASMState *as, IRIns *ir) | |||
1017 | { | 1092 | { |
1018 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; | 1093 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; |
1019 | IRRef args[3]; | 1094 | IRRef args[3]; |
1095 | asm_snap_prep(as); | ||
1020 | args[0] = ASMREF_L; /* lua_State *L */ | 1096 | args[0] = ASMREF_L; /* lua_State *L */ |
1021 | args[1] = ir->op1; /* const char *str */ | 1097 | args[1] = ir->op1; /* const char *str */ |
1022 | args[2] = ir->op2; /* size_t len */ | 1098 | args[2] = ir->op2; /* size_t len */ |
@@ -1029,6 +1105,7 @@ static void asm_tnew(ASMState *as, IRIns *ir) | |||
1029 | { | 1105 | { |
1030 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; | 1106 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; |
1031 | IRRef args[2]; | 1107 | IRRef args[2]; |
1108 | asm_snap_prep(as); | ||
1032 | args[0] = ASMREF_L; /* lua_State *L */ | 1109 | args[0] = ASMREF_L; /* lua_State *L */ |
1033 | args[1] = ASMREF_TMP1; /* uint32_t ahsize */ | 1110 | args[1] = ASMREF_TMP1; /* uint32_t ahsize */ |
1034 | as->gcsteps++; | 1111 | as->gcsteps++; |
@@ -1041,6 +1118,7 @@ static void asm_tdup(ASMState *as, IRIns *ir) | |||
1041 | { | 1118 | { |
1042 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; | 1119 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; |
1043 | IRRef args[2]; | 1120 | IRRef args[2]; |
1121 | asm_snap_prep(as); | ||
1044 | args[0] = ASMREF_L; /* lua_State *L */ | 1122 | args[0] = ASMREF_L; /* lua_State *L */ |
1045 | args[1] = ir->op1; /* const GCtab *kt */ | 1123 | args[1] = ir->op1; /* const GCtab *kt */ |
1046 | as->gcsteps++; | 1124 | as->gcsteps++; |
@@ -1064,6 +1142,240 @@ static void asm_gcstep(ASMState *as, IRIns *ir) | |||
1064 | as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ | 1142 | as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ |
1065 | } | 1143 | } |
1066 | 1144 | ||
1145 | /* -- Buffer operations --------------------------------------------------- */ | ||
1146 | |||
1147 | static void asm_tvptr(ASMState *as, Reg dest, IRRef ref); | ||
1148 | |||
1149 | static void asm_bufhdr(ASMState *as, IRIns *ir) | ||
1150 | { | ||
1151 | Reg sb = ra_dest(as, ir, RSET_GPR); | ||
1152 | if ((ir->op2 & IRBUFHDR_APPEND)) { | ||
1153 | /* Rematerialize const buffer pointer instead of likely spill. */ | ||
1154 | IRIns *irp = IR(ir->op1); | ||
1155 | if (!(ra_hasreg(irp->r) || irp == ir-1 || | ||
1156 | (irp == ir-2 && !ra_used(ir-1)))) { | ||
1157 | while (!(irp->o == IR_BUFHDR && !(irp->op2 & IRBUFHDR_APPEND))) | ||
1158 | irp = IR(irp->op1); | ||
1159 | if (irref_isk(irp->op1)) { | ||
1160 | ra_weak(as, ra_allocref(as, ir->op1, RSET_GPR)); | ||
1161 | ir = irp; | ||
1162 | } | ||
1163 | } | ||
1164 | } else { | ||
1165 | Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); | ||
1166 | IRIns irbp; | ||
1167 | irbp.ot = IRT(0, IRT_PTR); /* Buffer data pointer type. */ | ||
1168 | emit_storeofs(as, &irbp, tmp, sb, offsetof(SBuf, w)); | ||
1169 | emit_loadofs(as, &irbp, tmp, sb, offsetof(SBuf, b)); | ||
1170 | } | ||
1171 | #if LJ_TARGET_X86ORX64 | ||
1172 | ra_left(as, sb, ir->op1); | ||
1173 | #else | ||
1174 | ra_leftov(as, sb, ir->op1); | ||
1175 | #endif | ||
1176 | } | ||
1177 | |||
1178 | static void asm_bufput(ASMState *as, IRIns *ir) | ||
1179 | { | ||
1180 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_putstr]; | ||
1181 | IRRef args[3]; | ||
1182 | IRIns *irs; | ||
1183 | int kchar = -129; | ||
1184 | args[0] = ir->op1; /* SBuf * */ | ||
1185 | args[1] = ir->op2; /* GCstr * */ | ||
1186 | irs = IR(ir->op2); | ||
1187 | lj_assertA(irt_isstr(irs->t), | ||
1188 | "BUFPUT of non-string IR %04d", ir->op2 - REF_BIAS); | ||
1189 | if (irs->o == IR_KGC) { | ||
1190 | GCstr *s = ir_kstr(irs); | ||
1191 | if (s->len == 1) { /* Optimize put of single-char string constant. */ | ||
1192 | kchar = (int8_t)strdata(s)[0]; /* Signed! */ | ||
1193 | args[1] = ASMREF_TMP1; /* int, truncated to char */ | ||
1194 | ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; | ||
1195 | } | ||
1196 | } else if (mayfuse(as, ir->op2) && ra_noreg(irs->r)) { | ||
1197 | if (irs->o == IR_TOSTR) { /* Fuse number to string conversions. */ | ||
1198 | if (irs->op2 == IRTOSTR_NUM) { | ||
1199 | args[1] = ASMREF_TMP1; /* TValue * */ | ||
1200 | ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putnum]; | ||
1201 | } else { | ||
1202 | lj_assertA(irt_isinteger(IR(irs->op1)->t), | ||
1203 | "TOSTR of non-numeric IR %04d", irs->op1); | ||
1204 | args[1] = irs->op1; /* int */ | ||
1205 | if (irs->op2 == IRTOSTR_INT) | ||
1206 | ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putint]; | ||
1207 | else | ||
1208 | ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; | ||
1209 | } | ||
1210 | } else if (irs->o == IR_SNEW) { /* Fuse string allocation. */ | ||
1211 | args[1] = irs->op1; /* const void * */ | ||
1212 | args[2] = irs->op2; /* MSize */ | ||
1213 | ci = &lj_ir_callinfo[IRCALL_lj_buf_putmem]; | ||
1214 | } | ||
1215 | } | ||
1216 | asm_setupresult(as, ir, ci); /* SBuf * */ | ||
1217 | asm_gencall(as, ci, args); | ||
1218 | if (args[1] == ASMREF_TMP1) { | ||
1219 | Reg tmp = ra_releasetmp(as, ASMREF_TMP1); | ||
1220 | if (kchar == -129) | ||
1221 | asm_tvptr(as, tmp, irs->op1); | ||
1222 | else | ||
1223 | ra_allockreg(as, kchar, tmp); | ||
1224 | } | ||
1225 | } | ||
1226 | |||
1227 | static void asm_bufstr(ASMState *as, IRIns *ir) | ||
1228 | { | ||
1229 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_tostr]; | ||
1230 | IRRef args[1]; | ||
1231 | args[0] = ir->op1; /* SBuf *sb */ | ||
1232 | as->gcsteps++; | ||
1233 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
1234 | asm_gencall(as, ci, args); | ||
1235 | } | ||
1236 | |||
1237 | /* -- Type conversions ---------------------------------------------------- */ | ||
1238 | |||
1239 | static void asm_tostr(ASMState *as, IRIns *ir) | ||
1240 | { | ||
1241 | const CCallInfo *ci; | ||
1242 | IRRef args[2]; | ||
1243 | asm_snap_prep(as); | ||
1244 | args[0] = ASMREF_L; | ||
1245 | as->gcsteps++; | ||
1246 | if (ir->op2 == IRTOSTR_NUM) { | ||
1247 | args[1] = ASMREF_TMP1; /* cTValue * */ | ||
1248 | ci = &lj_ir_callinfo[IRCALL_lj_strfmt_num]; | ||
1249 | } else { | ||
1250 | args[1] = ir->op1; /* int32_t k */ | ||
1251 | if (ir->op2 == IRTOSTR_INT) | ||
1252 | ci = &lj_ir_callinfo[IRCALL_lj_strfmt_int]; | ||
1253 | else | ||
1254 | ci = &lj_ir_callinfo[IRCALL_lj_strfmt_char]; | ||
1255 | } | ||
1256 | asm_setupresult(as, ir, ci); /* GCstr * */ | ||
1257 | asm_gencall(as, ci, args); | ||
1258 | if (ir->op2 == IRTOSTR_NUM) | ||
1259 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); | ||
1260 | } | ||
1261 | |||
1262 | #if LJ_32 && LJ_HASFFI && !LJ_SOFTFP && !LJ_TARGET_X86 | ||
1263 | static void asm_conv64(ASMState *as, IRIns *ir) | ||
1264 | { | ||
1265 | IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); | ||
1266 | IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); | ||
1267 | IRCallID id; | ||
1268 | IRRef args[2]; | ||
1269 | lj_assertA((ir-1)->o == IR_CONV && ir->o == IR_HIOP, | ||
1270 | "not a CONV/HIOP pair at IR %04d", (int)(ir - as->ir) - REF_BIAS); | ||
1271 | args[LJ_BE] = (ir-1)->op1; | ||
1272 | args[LJ_LE] = ir->op1; | ||
1273 | if (st == IRT_NUM || st == IRT_FLOAT) { | ||
1274 | id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); | ||
1275 | ir--; | ||
1276 | } else { | ||
1277 | id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); | ||
1278 | } | ||
1279 | { | ||
1280 | #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP | ||
1281 | CCallInfo cim = lj_ir_callinfo[id], *ci = &cim; | ||
1282 | cim.flags |= CCI_VARARG; /* These calls don't use the hard-float ABI! */ | ||
1283 | #else | ||
1284 | const CCallInfo *ci = &lj_ir_callinfo[id]; | ||
1285 | #endif | ||
1286 | asm_setupresult(as, ir, ci); | ||
1287 | asm_gencall(as, ci, args); | ||
1288 | } | ||
1289 | } | ||
1290 | #endif | ||
1291 | |||
1292 | /* -- Memory references --------------------------------------------------- */ | ||
1293 | |||
1294 | static void asm_newref(ASMState *as, IRIns *ir) | ||
1295 | { | ||
1296 | const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; | ||
1297 | IRRef args[3]; | ||
1298 | if (ir->r == RID_SINK) | ||
1299 | return; | ||
1300 | asm_snap_prep(as); | ||
1301 | args[0] = ASMREF_L; /* lua_State *L */ | ||
1302 | args[1] = ir->op1; /* GCtab *t */ | ||
1303 | args[2] = ASMREF_TMP1; /* cTValue *key */ | ||
1304 | asm_setupresult(as, ir, ci); /* TValue * */ | ||
1305 | asm_gencall(as, ci, args); | ||
1306 | asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); | ||
1307 | } | ||
1308 | |||
1309 | static void asm_lref(ASMState *as, IRIns *ir) | ||
1310 | { | ||
1311 | Reg r = ra_dest(as, ir, RSET_GPR); | ||
1312 | #if LJ_TARGET_X86ORX64 | ||
1313 | ra_left(as, r, ASMREF_L); | ||
1314 | #else | ||
1315 | ra_leftov(as, r, ASMREF_L); | ||
1316 | #endif | ||
1317 | } | ||
1318 | |||
1319 | /* -- Calls --------------------------------------------------------------- */ | ||
1320 | |||
1321 | /* Collect arguments from CALL* and CARG instructions. */ | ||
1322 | static void asm_collectargs(ASMState *as, IRIns *ir, | ||
1323 | const CCallInfo *ci, IRRef *args) | ||
1324 | { | ||
1325 | uint32_t n = CCI_XNARGS(ci); | ||
1326 | /* Account for split args. */ | ||
1327 | lj_assertA(n <= CCI_NARGS_MAX*2, "too many args %d to collect", n); | ||
1328 | if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } | ||
1329 | while (n-- > 1) { | ||
1330 | ir = IR(ir->op1); | ||
1331 | lj_assertA(ir->o == IR_CARG, "malformed CALL arg tree"); | ||
1332 | args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; | ||
1333 | } | ||
1334 | args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; | ||
1335 | lj_assertA(IR(ir->op1)->o != IR_CARG, "malformed CALL arg tree"); | ||
1336 | } | ||
1337 | |||
1338 | /* Reconstruct CCallInfo flags for CALLX*. */ | ||
1339 | static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) | ||
1340 | { | ||
1341 | uint32_t nargs = 0; | ||
1342 | if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ | ||
1343 | IRIns *ira = IR(ir->op1); | ||
1344 | nargs++; | ||
1345 | while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } | ||
1346 | } | ||
1347 | #if LJ_HASFFI | ||
1348 | if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ | ||
1349 | CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; | ||
1350 | CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); | ||
1351 | nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); | ||
1352 | #if LJ_TARGET_X86 | ||
1353 | nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); | ||
1354 | #endif | ||
1355 | } | ||
1356 | #endif | ||
1357 | return (nargs | (ir->t.irt << CCI_OTSHIFT)); | ||
1358 | } | ||
1359 | |||
1360 | static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) | ||
1361 | { | ||
1362 | const CCallInfo *ci = &lj_ir_callinfo[id]; | ||
1363 | IRRef args[2]; | ||
1364 | args[0] = ir->op1; | ||
1365 | args[1] = ir->op2; | ||
1366 | asm_setupresult(as, ir, ci); | ||
1367 | asm_gencall(as, ci, args); | ||
1368 | } | ||
1369 | |||
1370 | static void asm_call(ASMState *as, IRIns *ir) | ||
1371 | { | ||
1372 | IRRef args[CCI_NARGS_MAX]; | ||
1373 | const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; | ||
1374 | asm_collectargs(as, ir, ci, args); | ||
1375 | asm_setupresult(as, ir, ci); | ||
1376 | asm_gencall(as, ci, args); | ||
1377 | } | ||
1378 | |||
1067 | /* -- PHI and loop handling ----------------------------------------------- */ | 1379 | /* -- PHI and loop handling ----------------------------------------------- */ |
1068 | 1380 | ||
1069 | /* Break a PHI cycle by renaming to a free register (evict if needed). */ | 1381 | /* Break a PHI cycle by renaming to a free register (evict if needed). */ |
@@ -1249,12 +1561,7 @@ static void asm_phi_fixup(ASMState *as) | |||
1249 | irt_clearmark(ir->t); | 1561 | irt_clearmark(ir->t); |
1250 | /* Left PHI gained a spill slot before the loop? */ | 1562 | /* Left PHI gained a spill slot before the loop? */ |
1251 | if (ra_hasspill(ir->s)) { | 1563 | if (ra_hasspill(ir->s)) { |
1252 | IRRef ren; | 1564 | ra_addrename(as, r, lref, as->loopsnapno); |
1253 | lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), lref, as->loopsnapno); | ||
1254 | ren = tref_ref(lj_ir_emit(as->J)); | ||
1255 | as->ir = as->T->ir; /* The IR may have been reallocated. */ | ||
1256 | IR(ren)->r = (uint8_t)r; | ||
1257 | IR(ren)->s = SPS_NONE; | ||
1258 | } | 1565 | } |
1259 | } | 1566 | } |
1260 | rset_clear(work, r); | 1567 | rset_clear(work, r); |
@@ -1329,6 +1636,8 @@ static void asm_loop(ASMState *as) | |||
1329 | #include "lj_asm_x86.h" | 1636 | #include "lj_asm_x86.h" |
1330 | #elif LJ_TARGET_ARM | 1637 | #elif LJ_TARGET_ARM |
1331 | #include "lj_asm_arm.h" | 1638 | #include "lj_asm_arm.h" |
1639 | #elif LJ_TARGET_ARM64 | ||
1640 | #include "lj_asm_arm64.h" | ||
1332 | #elif LJ_TARGET_PPC | 1641 | #elif LJ_TARGET_PPC |
1333 | #include "lj_asm_ppc.h" | 1642 | #include "lj_asm_ppc.h" |
1334 | #elif LJ_TARGET_MIPS | 1643 | #elif LJ_TARGET_MIPS |
@@ -1337,6 +1646,203 @@ static void asm_loop(ASMState *as) | |||
1337 | #error "Missing assembler for target CPU" | 1646 | #error "Missing assembler for target CPU" |
1338 | #endif | 1647 | #endif |
1339 | 1648 | ||
1649 | /* -- Common instruction helpers ------------------------------------------ */ | ||
1650 | |||
1651 | #if !LJ_SOFTFP32 | ||
1652 | #if !LJ_TARGET_X86ORX64 | ||
1653 | #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) | ||
1654 | #define asm_fppowi(as, ir) asm_callid(as, ir, IRCALL_lj_vm_powi) | ||
1655 | #endif | ||
1656 | |||
1657 | static void asm_pow(ASMState *as, IRIns *ir) | ||
1658 | { | ||
1659 | #if LJ_64 && LJ_HASFFI | ||
1660 | if (!irt_isnum(ir->t)) | ||
1661 | asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : | ||
1662 | IRCALL_lj_carith_powu64); | ||
1663 | else | ||
1664 | #endif | ||
1665 | if (irt_isnum(IR(ir->op2)->t)) | ||
1666 | asm_callid(as, ir, IRCALL_pow); | ||
1667 | else | ||
1668 | asm_fppowi(as, ir); | ||
1669 | } | ||
1670 | |||
1671 | static void asm_div(ASMState *as, IRIns *ir) | ||
1672 | { | ||
1673 | #if LJ_64 && LJ_HASFFI | ||
1674 | if (!irt_isnum(ir->t)) | ||
1675 | asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : | ||
1676 | IRCALL_lj_carith_divu64); | ||
1677 | else | ||
1678 | #endif | ||
1679 | asm_fpdiv(as, ir); | ||
1680 | } | ||
1681 | #endif | ||
1682 | |||
1683 | static void asm_mod(ASMState *as, IRIns *ir) | ||
1684 | { | ||
1685 | #if LJ_64 && LJ_HASFFI | ||
1686 | if (!irt_isint(ir->t)) | ||
1687 | asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : | ||
1688 | IRCALL_lj_carith_modu64); | ||
1689 | else | ||
1690 | #endif | ||
1691 | asm_callid(as, ir, IRCALL_lj_vm_modi); | ||
1692 | } | ||
1693 | |||
1694 | static void asm_fuseequal(ASMState *as, IRIns *ir) | ||
1695 | { | ||
1696 | /* Fuse HREF + EQ/NE. */ | ||
1697 | if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { | ||
1698 | as->curins--; | ||
1699 | asm_href(as, ir-1, (IROp)ir->o); | ||
1700 | } else { | ||
1701 | asm_equal(as, ir); | ||
1702 | } | ||
1703 | } | ||
1704 | |||
1705 | static void asm_alen(ASMState *as, IRIns *ir) | ||
1706 | { | ||
1707 | asm_callid(as, ir, ir->op2 == REF_NIL ? IRCALL_lj_tab_len : | ||
1708 | IRCALL_lj_tab_len_hint); | ||
1709 | } | ||
1710 | |||
1711 | /* -- Instruction dispatch ------------------------------------------------ */ | ||
1712 | |||
1713 | /* Assemble a single instruction. */ | ||
1714 | static void asm_ir(ASMState *as, IRIns *ir) | ||
1715 | { | ||
1716 | switch ((IROp)ir->o) { | ||
1717 | /* Miscellaneous ops. */ | ||
1718 | case IR_LOOP: asm_loop(as); break; | ||
1719 | case IR_NOP: case IR_XBAR: | ||
1720 | lj_assertA(!ra_used(ir), | ||
1721 | "IR %04d not unused", (int)(ir - as->ir) - REF_BIAS); | ||
1722 | break; | ||
1723 | case IR_USE: | ||
1724 | ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; | ||
1725 | case IR_PHI: asm_phi(as, ir); break; | ||
1726 | case IR_HIOP: asm_hiop(as, ir); break; | ||
1727 | case IR_GCSTEP: asm_gcstep(as, ir); break; | ||
1728 | case IR_PROF: asm_prof(as, ir); break; | ||
1729 | |||
1730 | /* Guarded assertions. */ | ||
1731 | case IR_LT: case IR_GE: case IR_LE: case IR_GT: | ||
1732 | case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: | ||
1733 | case IR_ABC: | ||
1734 | asm_comp(as, ir); | ||
1735 | break; | ||
1736 | case IR_EQ: case IR_NE: asm_fuseequal(as, ir); break; | ||
1737 | |||
1738 | case IR_RETF: asm_retf(as, ir); break; | ||
1739 | |||
1740 | /* Bit ops. */ | ||
1741 | case IR_BNOT: asm_bnot(as, ir); break; | ||
1742 | case IR_BSWAP: asm_bswap(as, ir); break; | ||
1743 | case IR_BAND: asm_band(as, ir); break; | ||
1744 | case IR_BOR: asm_bor(as, ir); break; | ||
1745 | case IR_BXOR: asm_bxor(as, ir); break; | ||
1746 | case IR_BSHL: asm_bshl(as, ir); break; | ||
1747 | case IR_BSHR: asm_bshr(as, ir); break; | ||
1748 | case IR_BSAR: asm_bsar(as, ir); break; | ||
1749 | case IR_BROL: asm_brol(as, ir); break; | ||
1750 | case IR_BROR: asm_bror(as, ir); break; | ||
1751 | |||
1752 | /* Arithmetic ops. */ | ||
1753 | case IR_ADD: asm_add(as, ir); break; | ||
1754 | case IR_SUB: asm_sub(as, ir); break; | ||
1755 | case IR_MUL: asm_mul(as, ir); break; | ||
1756 | case IR_MOD: asm_mod(as, ir); break; | ||
1757 | case IR_NEG: asm_neg(as, ir); break; | ||
1758 | #if LJ_SOFTFP32 | ||
1759 | case IR_DIV: case IR_POW: case IR_ABS: | ||
1760 | case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: | ||
1761 | /* Unused for LJ_SOFTFP32. */ | ||
1762 | lj_assertA(0, "IR %04d with unused op %d", | ||
1763 | (int)(ir - as->ir) - REF_BIAS, ir->o); | ||
1764 | break; | ||
1765 | #else | ||
1766 | case IR_DIV: asm_div(as, ir); break; | ||
1767 | case IR_POW: asm_pow(as, ir); break; | ||
1768 | case IR_ABS: asm_abs(as, ir); break; | ||
1769 | case IR_LDEXP: asm_ldexp(as, ir); break; | ||
1770 | case IR_FPMATH: asm_fpmath(as, ir); break; | ||
1771 | case IR_TOBIT: asm_tobit(as, ir); break; | ||
1772 | #endif | ||
1773 | case IR_MIN: asm_min(as, ir); break; | ||
1774 | case IR_MAX: asm_max(as, ir); break; | ||
1775 | |||
1776 | /* Overflow-checking arithmetic ops. */ | ||
1777 | case IR_ADDOV: asm_addov(as, ir); break; | ||
1778 | case IR_SUBOV: asm_subov(as, ir); break; | ||
1779 | case IR_MULOV: asm_mulov(as, ir); break; | ||
1780 | |||
1781 | /* Memory references. */ | ||
1782 | case IR_AREF: asm_aref(as, ir); break; | ||
1783 | case IR_HREF: asm_href(as, ir, 0); break; | ||
1784 | case IR_HREFK: asm_hrefk(as, ir); break; | ||
1785 | case IR_NEWREF: asm_newref(as, ir); break; | ||
1786 | case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; | ||
1787 | case IR_FREF: asm_fref(as, ir); break; | ||
1788 | case IR_STRREF: asm_strref(as, ir); break; | ||
1789 | case IR_LREF: asm_lref(as, ir); break; | ||
1790 | |||
1791 | /* Loads and stores. */ | ||
1792 | case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: | ||
1793 | asm_ahuvload(as, ir); | ||
1794 | break; | ||
1795 | case IR_FLOAD: asm_fload(as, ir); break; | ||
1796 | case IR_XLOAD: asm_xload(as, ir); break; | ||
1797 | case IR_SLOAD: asm_sload(as, ir); break; | ||
1798 | case IR_ALEN: asm_alen(as, ir); break; | ||
1799 | |||
1800 | case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; | ||
1801 | case IR_FSTORE: asm_fstore(as, ir); break; | ||
1802 | case IR_XSTORE: asm_xstore(as, ir); break; | ||
1803 | |||
1804 | /* Allocations. */ | ||
1805 | case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; | ||
1806 | case IR_TNEW: asm_tnew(as, ir); break; | ||
1807 | case IR_TDUP: asm_tdup(as, ir); break; | ||
1808 | case IR_CNEW: case IR_CNEWI: | ||
1809 | #if LJ_HASFFI | ||
1810 | asm_cnew(as, ir); | ||
1811 | #else | ||
1812 | lj_assertA(0, "IR %04d with unused op %d", | ||
1813 | (int)(ir - as->ir) - REF_BIAS, ir->o); | ||
1814 | #endif | ||
1815 | break; | ||
1816 | |||
1817 | /* Buffer operations. */ | ||
1818 | case IR_BUFHDR: asm_bufhdr(as, ir); break; | ||
1819 | case IR_BUFPUT: asm_bufput(as, ir); break; | ||
1820 | case IR_BUFSTR: asm_bufstr(as, ir); break; | ||
1821 | |||
1822 | /* Write barriers. */ | ||
1823 | case IR_TBAR: asm_tbar(as, ir); break; | ||
1824 | case IR_OBAR: asm_obar(as, ir); break; | ||
1825 | |||
1826 | /* Type conversions. */ | ||
1827 | case IR_CONV: asm_conv(as, ir); break; | ||
1828 | case IR_TOSTR: asm_tostr(as, ir); break; | ||
1829 | case IR_STRTO: asm_strto(as, ir); break; | ||
1830 | |||
1831 | /* Calls. */ | ||
1832 | case IR_CALLA: | ||
1833 | as->gcsteps++; | ||
1834 | /* fallthrough */ | ||
1835 | case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; | ||
1836 | case IR_CALLXS: asm_callx(as, ir); break; | ||
1837 | case IR_CARG: break; | ||
1838 | |||
1839 | default: | ||
1840 | setintV(&as->J->errinfo, ir->o); | ||
1841 | lj_trace_err_info(as->J, LJ_TRERR_NYIIR); | ||
1842 | break; | ||
1843 | } | ||
1844 | } | ||
1845 | |||
1340 | /* -- Head of trace ------------------------------------------------------- */ | 1846 | /* -- Head of trace ------------------------------------------------------- */ |
1341 | 1847 | ||
1342 | /* Head of a root trace. */ | 1848 | /* Head of a root trace. */ |
@@ -1373,8 +1879,7 @@ static void asm_head_side(ASMState *as) | |||
1373 | 1879 | ||
1374 | if (as->snapno && as->topslot > as->parent->topslot) { | 1880 | if (as->snapno && as->topslot > as->parent->topslot) { |
1375 | /* Force snap #0 alloc to prevent register overwrite in stack check. */ | 1881 | /* Force snap #0 alloc to prevent register overwrite in stack check. */ |
1376 | as->snapno = 0; | 1882 | asm_snap_alloc(as, 0); |
1377 | asm_snap_alloc(as); | ||
1378 | } | 1883 | } |
1379 | allow = asm_head_side_base(as, irp, allow); | 1884 | allow = asm_head_side_base(as, irp, allow); |
1380 | 1885 | ||
@@ -1382,8 +1887,10 @@ static void asm_head_side(ASMState *as) | |||
1382 | for (i = as->stopins; i > REF_BASE; i--) { | 1887 | for (i = as->stopins; i > REF_BASE; i--) { |
1383 | IRIns *ir = IR(i); | 1888 | IRIns *ir = IR(i); |
1384 | RegSP rs; | 1889 | RegSP rs; |
1385 | lua_assert((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || | 1890 | lj_assertA((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || |
1386 | (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL); | 1891 | (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL, |
1892 | "IR %04d has bad parent op %d", | ||
1893 | (int)(ir - as->ir) - REF_BIAS, ir->o); | ||
1387 | rs = as->parentmap[i - REF_FIRST]; | 1894 | rs = as->parentmap[i - REF_FIRST]; |
1388 | if (ra_hasreg(ir->r)) { | 1895 | if (ra_hasreg(ir->r)) { |
1389 | rset_clear(allow, ir->r); | 1896 | rset_clear(allow, ir->r); |
@@ -1535,7 +2042,7 @@ static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe) | |||
1535 | SnapEntry sn = map[n-1]; | 2042 | SnapEntry sn = map[n-1]; |
1536 | if ((sn & SNAP_FRAME)) { | 2043 | if ((sn & SNAP_FRAME)) { |
1537 | *gotframe = 1; | 2044 | *gotframe = 1; |
1538 | return snap_slot(sn); | 2045 | return snap_slot(sn) - LJ_FR2; |
1539 | } | 2046 | } |
1540 | } | 2047 | } |
1541 | return 0; | 2048 | return 0; |
@@ -1555,19 +2062,23 @@ static void asm_tail_link(ASMState *as) | |||
1555 | 2062 | ||
1556 | if (as->T->link == 0) { | 2063 | if (as->T->link == 0) { |
1557 | /* Setup fixed registers for exit to interpreter. */ | 2064 | /* Setup fixed registers for exit to interpreter. */ |
1558 | const BCIns *pc = snap_pc(as->T->snapmap[snap->mapofs + snap->nent]); | 2065 | const BCIns *pc = snap_pc(&as->T->snapmap[snap->mapofs + snap->nent]); |
1559 | int32_t mres; | 2066 | int32_t mres; |
1560 | if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ | 2067 | if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ |
1561 | BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; | 2068 | BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; |
1562 | if (bc_isret(bc_op(*retpc))) | 2069 | if (bc_isret(bc_op(*retpc))) |
1563 | pc = retpc; | 2070 | pc = retpc; |
1564 | } | 2071 | } |
2072 | #if LJ_GC64 | ||
2073 | emit_loadu64(as, RID_LPC, u64ptr(pc)); | ||
2074 | #else | ||
1565 | ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); | 2075 | ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); |
1566 | ra_allockreg(as, i32ptr(pc), RID_LPC); | 2076 | ra_allockreg(as, i32ptr(pc), RID_LPC); |
1567 | mres = (int32_t)(snap->nslots - baseslot); | 2077 | #endif |
2078 | mres = (int32_t)(snap->nslots - baseslot - LJ_FR2); | ||
1568 | switch (bc_op(*pc)) { | 2079 | switch (bc_op(*pc)) { |
1569 | case BC_CALLM: case BC_CALLMT: | 2080 | case BC_CALLM: case BC_CALLMT: |
1570 | mres -= (int32_t)(1 + bc_a(*pc) + bc_c(*pc)); break; | 2081 | mres -= (int32_t)(1 + LJ_FR2 + bc_a(*pc) + bc_c(*pc)); break; |
1571 | case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; | 2082 | case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; |
1572 | case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; | 2083 | case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; |
1573 | default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; | 2084 | default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; |
@@ -1579,6 +2090,11 @@ static void asm_tail_link(ASMState *as) | |||
1579 | } | 2090 | } |
1580 | emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); | 2091 | emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); |
1581 | 2092 | ||
2093 | if (as->J->ktrace) { /* Patch ktrace slot with the final GCtrace pointer. */ | ||
2094 | setgcref(IR(as->J->ktrace)[LJ_GC64].gcr, obj2gco(as->J->curfinal)); | ||
2095 | IR(as->J->ktrace)->o = IR_KGC; | ||
2096 | } | ||
2097 | |||
1582 | /* Sync the interpreter state with the on-trace state. */ | 2098 | /* Sync the interpreter state with the on-trace state. */ |
1583 | asm_stack_restore(as, snap); | 2099 | asm_stack_restore(as, snap); |
1584 | 2100 | ||
@@ -1602,22 +2118,32 @@ static void asm_setup_regsp(ASMState *as) | |||
1602 | #endif | 2118 | #endif |
1603 | 2119 | ||
1604 | ra_setup(as); | 2120 | ra_setup(as); |
2121 | #if LJ_TARGET_ARM64 | ||
2122 | ra_setkref(as, RID_GL, (intptr_t)J2G(as->J)); | ||
2123 | #endif | ||
1605 | 2124 | ||
1606 | /* Clear reg/sp for constants. */ | 2125 | /* Clear reg/sp for constants. */ |
1607 | for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) | 2126 | for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) { |
1608 | ir->prev = REGSP_INIT; | 2127 | ir->prev = REGSP_INIT; |
2128 | if (irt_is64(ir->t) && ir->o != IR_KNULL) { | ||
2129 | #if LJ_GC64 | ||
2130 | /* The false-positive of irt_is64() for ASMREF_L (REF_NIL) is OK here. */ | ||
2131 | ir->i = 0; /* Will become non-zero only for RIP-relative addresses. */ | ||
2132 | #else | ||
2133 | /* Make life easier for backends by putting address of constant in i. */ | ||
2134 | ir->i = (int32_t)(intptr_t)(ir+1); | ||
2135 | #endif | ||
2136 | ir++; | ||
2137 | } | ||
2138 | } | ||
1609 | 2139 | ||
1610 | /* REF_BASE is used for implicit references to the BASE register. */ | 2140 | /* REF_BASE is used for implicit references to the BASE register. */ |
1611 | lastir->prev = REGSP_HINT(RID_BASE); | 2141 | lastir->prev = REGSP_HINT(RID_BASE); |
1612 | 2142 | ||
1613 | ir = IR(nins-1); | ||
1614 | if (ir->o == IR_RENAME) { | ||
1615 | do { ir--; nins--; } while (ir->o == IR_RENAME); | ||
1616 | T->nins = nins; /* Remove any renames left over from ASM restart. */ | ||
1617 | } | ||
1618 | as->snaprename = nins; | 2143 | as->snaprename = nins; |
1619 | as->snapref = nins; | 2144 | as->snapref = nins; |
1620 | as->snapno = T->nsnap; | 2145 | as->snapno = T->nsnap; |
2146 | as->snapalloc = 0; | ||
1621 | 2147 | ||
1622 | as->stopins = REF_BASE; | 2148 | as->stopins = REF_BASE; |
1623 | as->orignins = nins; | 2149 | as->orignins = nins; |
@@ -1627,7 +2153,7 @@ static void asm_setup_regsp(ASMState *as) | |||
1627 | ir = IR(REF_FIRST); | 2153 | ir = IR(REF_FIRST); |
1628 | if (as->parent) { | 2154 | if (as->parent) { |
1629 | uint16_t *p; | 2155 | uint16_t *p; |
1630 | lastir = lj_snap_regspmap(as->parent, as->J->exitno, ir); | 2156 | lastir = lj_snap_regspmap(as->J, as->parent, as->J->exitno, ir); |
1631 | if (lastir - ir > LJ_MAX_JSLOTS) | 2157 | if (lastir - ir > LJ_MAX_JSLOTS) |
1632 | lj_trace_err(as->J, LJ_TRERR_NYICOAL); | 2158 | lj_trace_err(as->J, LJ_TRERR_NYICOAL); |
1633 | as->stopins = (IRRef)((lastir-1) - as->ir); | 2159 | as->stopins = (IRRef)((lastir-1) - as->ir); |
@@ -1675,7 +2201,7 @@ static void asm_setup_regsp(ASMState *as) | |||
1675 | as->modset |= RSET_SCRATCH; | 2201 | as->modset |= RSET_SCRATCH; |
1676 | continue; | 2202 | continue; |
1677 | } | 2203 | } |
1678 | case IR_CALLN: case IR_CALLL: case IR_CALLS: { | 2204 | case IR_CALLN: case IR_CALLA: case IR_CALLL: case IR_CALLS: { |
1679 | const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; | 2205 | const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; |
1680 | ir->prev = asm_setup_call_slots(as, ir, ci); | 2206 | ir->prev = asm_setup_call_slots(as, ir, ci); |
1681 | if (inloop) | 2207 | if (inloop) |
@@ -1700,8 +2226,8 @@ static void asm_setup_regsp(ASMState *as) | |||
1700 | ir->prev = REGSP_HINT(RID_FPRET); | 2226 | ir->prev = REGSP_HINT(RID_FPRET); |
1701 | continue; | 2227 | continue; |
1702 | } | 2228 | } |
1703 | /* fallthrough */ | ||
1704 | #endif | 2229 | #endif |
2230 | /* fallthrough */ | ||
1705 | case IR_CALLN: case IR_CALLXS: | 2231 | case IR_CALLN: case IR_CALLXS: |
1706 | #if LJ_SOFTFP | 2232 | #if LJ_SOFTFP |
1707 | case IR_MIN: case IR_MAX: | 2233 | case IR_MIN: case IR_MAX: |
@@ -1720,11 +2246,23 @@ static void asm_setup_regsp(ASMState *as) | |||
1720 | #endif | 2246 | #endif |
1721 | /* fallthrough */ | 2247 | /* fallthrough */ |
1722 | /* C calls evict all scratch regs and return results in RID_RET. */ | 2248 | /* C calls evict all scratch regs and return results in RID_RET. */ |
1723 | case IR_SNEW: case IR_XSNEW: case IR_NEWREF: | 2249 | case IR_SNEW: case IR_XSNEW: case IR_NEWREF: case IR_BUFPUT: |
1724 | if (REGARG_NUMGPR < 3 && as->evenspill < 3) | 2250 | if (REGARG_NUMGPR < 3 && as->evenspill < 3) |
1725 | as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ | 2251 | as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ |
2252 | #if LJ_TARGET_X86 && LJ_HASFFI | ||
2253 | if (0) { | ||
2254 | case IR_CNEW: | ||
2255 | if (ir->op2 != REF_NIL && as->evenspill < 4) | ||
2256 | as->evenspill = 4; /* lj_cdata_newv needs 4 args. */ | ||
2257 | } | ||
1726 | /* fallthrough */ | 2258 | /* fallthrough */ |
1727 | case IR_TNEW: case IR_TDUP: case IR_CNEW: case IR_CNEWI: case IR_TOSTR: | 2259 | #else |
2260 | /* fallthrough */ | ||
2261 | case IR_CNEW: | ||
2262 | #endif | ||
2263 | /* fallthrough */ | ||
2264 | case IR_TNEW: case IR_TDUP: case IR_CNEWI: case IR_TOSTR: | ||
2265 | case IR_BUFSTR: | ||
1728 | ir->prev = REGSP_HINT(RID_RET); | 2266 | ir->prev = REGSP_HINT(RID_RET); |
1729 | if (inloop) | 2267 | if (inloop) |
1730 | as->modset = RSET_SCRATCH; | 2268 | as->modset = RSET_SCRATCH; |
@@ -1733,21 +2271,26 @@ static void asm_setup_regsp(ASMState *as) | |||
1733 | if (inloop) | 2271 | if (inloop) |
1734 | as->modset = RSET_SCRATCH; | 2272 | as->modset = RSET_SCRATCH; |
1735 | break; | 2273 | break; |
1736 | #if !LJ_TARGET_X86ORX64 && !LJ_SOFTFP | 2274 | #if !LJ_SOFTFP |
1737 | case IR_ATAN2: case IR_LDEXP: | 2275 | #if !LJ_TARGET_X86ORX64 |
2276 | case IR_LDEXP: | ||
2277 | #endif | ||
1738 | #endif | 2278 | #endif |
2279 | /* fallthrough */ | ||
1739 | case IR_POW: | 2280 | case IR_POW: |
1740 | if (!LJ_SOFTFP && irt_isnum(ir->t)) { | 2281 | if (!LJ_SOFTFP && irt_isnum(ir->t)) { |
1741 | #if LJ_TARGET_X86ORX64 | ||
1742 | ir->prev = REGSP_HINT(RID_XMM0); | ||
1743 | if (inloop) | 2282 | if (inloop) |
1744 | as->modset |= RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX); | 2283 | as->modset |= RSET_SCRATCH; |
2284 | #if LJ_TARGET_X86 | ||
2285 | if (irt_isnum(IR(ir->op2)->t)) { | ||
2286 | if (as->evenspill < 4) /* Leave room to call pow(). */ | ||
2287 | as->evenspill = 4; | ||
2288 | } | ||
2289 | break; | ||
1745 | #else | 2290 | #else |
1746 | ir->prev = REGSP_HINT(RID_FPRET); | 2291 | ir->prev = REGSP_HINT(RID_FPRET); |
1747 | if (inloop) | ||
1748 | as->modset |= RSET_SCRATCH; | ||
1749 | #endif | ||
1750 | continue; | 2292 | continue; |
2293 | #endif | ||
1751 | } | 2294 | } |
1752 | /* fallthrough */ /* for integer POW */ | 2295 | /* fallthrough */ /* for integer POW */ |
1753 | case IR_DIV: case IR_MOD: | 2296 | case IR_DIV: case IR_MOD: |
@@ -1760,31 +2303,31 @@ static void asm_setup_regsp(ASMState *as) | |||
1760 | break; | 2303 | break; |
1761 | case IR_FPMATH: | 2304 | case IR_FPMATH: |
1762 | #if LJ_TARGET_X86ORX64 | 2305 | #if LJ_TARGET_X86ORX64 |
1763 | if (ir->op2 == IRFPM_EXP2) { /* May be joined to lj_vm_pow_sse. */ | 2306 | if (ir->op2 <= IRFPM_TRUNC) { |
1764 | ir->prev = REGSP_HINT(RID_XMM0); | 2307 | if (!(as->flags & JIT_F_SSE4_1)) { |
1765 | #if !LJ_64 | 2308 | ir->prev = REGSP_HINT(RID_XMM0); |
1766 | if (as->evenspill < 4) /* Leave room for 16 byte scratch area. */ | 2309 | if (inloop) |
1767 | as->evenspill = 4; | 2310 | as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); |
1768 | #endif | 2311 | continue; |
1769 | if (inloop) | 2312 | } |
1770 | as->modset |= RSET_RANGE(RID_XMM0, RID_XMM2+1)|RID2RSET(RID_EAX); | 2313 | break; |
1771 | continue; | ||
1772 | } else if (ir->op2 <= IRFPM_TRUNC && !(as->flags & JIT_F_SSE4_1)) { | ||
1773 | ir->prev = REGSP_HINT(RID_XMM0); | ||
1774 | if (inloop) | ||
1775 | as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); | ||
1776 | continue; | ||
1777 | } | 2314 | } |
2315 | #endif | ||
2316 | if (inloop) | ||
2317 | as->modset |= RSET_SCRATCH; | ||
2318 | #if LJ_TARGET_X86 | ||
1778 | break; | 2319 | break; |
1779 | #else | 2320 | #else |
1780 | ir->prev = REGSP_HINT(RID_FPRET); | 2321 | ir->prev = REGSP_HINT(RID_FPRET); |
1781 | if (inloop) | ||
1782 | as->modset |= RSET_SCRATCH; | ||
1783 | continue; | 2322 | continue; |
1784 | #endif | 2323 | #endif |
1785 | #if LJ_TARGET_X86ORX64 | 2324 | #if LJ_TARGET_X86ORX64 |
1786 | /* Non-constant shift counts need to be in RID_ECX on x86/x64. */ | 2325 | /* Non-constant shift counts need to be in RID_ECX on x86/x64. */ |
1787 | case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR: | 2326 | case IR_BSHL: case IR_BSHR: case IR_BSAR: |
2327 | if ((as->flags & JIT_F_BMI2)) /* Except if BMI2 is available. */ | ||
2328 | break; | ||
2329 | /* fallthrough */ | ||
2330 | case IR_BROL: case IR_BROR: | ||
1788 | if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { | 2331 | if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { |
1789 | IR(ir->op2)->r = REGSP_HINT(RID_ECX); | 2332 | IR(ir->op2)->r = REGSP_HINT(RID_ECX); |
1790 | if (inloop) | 2333 | if (inloop) |
@@ -1828,16 +2371,26 @@ void lj_asm_trace(jit_State *J, GCtrace *T) | |||
1828 | { | 2371 | { |
1829 | ASMState as_; | 2372 | ASMState as_; |
1830 | ASMState *as = &as_; | 2373 | ASMState *as = &as_; |
1831 | MCode *origtop; | 2374 | |
2375 | /* Remove nops/renames left over from ASM restart due to LJ_TRERR_MCODELM. */ | ||
2376 | { | ||
2377 | IRRef nins = T->nins; | ||
2378 | IRIns *ir = &T->ir[nins-1]; | ||
2379 | if (ir->o == IR_NOP || ir->o == IR_RENAME) { | ||
2380 | do { ir--; nins--; } while (ir->o == IR_NOP || ir->o == IR_RENAME); | ||
2381 | T->nins = nins; | ||
2382 | } | ||
2383 | } | ||
1832 | 2384 | ||
1833 | /* Ensure an initialized instruction beyond the last one for HIOP checks. */ | 2385 | /* Ensure an initialized instruction beyond the last one for HIOP checks. */ |
1834 | J->cur.nins = lj_ir_nextins(J); | 2386 | /* This also allows one RENAME to be added without reallocating curfinal. */ |
1835 | lj_ir_nop(&J->cur.ir[J->cur.nins]); | 2387 | as->orignins = lj_ir_nextins(J); |
2388 | lj_ir_nop(&J->cur.ir[as->orignins]); | ||
1836 | 2389 | ||
1837 | /* Setup initial state. Copy some fields to reduce indirections. */ | 2390 | /* Setup initial state. Copy some fields to reduce indirections. */ |
1838 | as->J = J; | 2391 | as->J = J; |
1839 | as->T = T; | 2392 | as->T = T; |
1840 | as->ir = T->ir; | 2393 | J->curfinal = lj_trace_alloc(J->L, T); /* This copies the IR, too. */ |
1841 | as->flags = J->flags; | 2394 | as->flags = J->flags; |
1842 | as->loopref = J->loopref; | 2395 | as->loopref = J->loopref; |
1843 | as->realign = NULL; | 2396 | as->realign = NULL; |
@@ -1845,17 +2398,46 @@ void lj_asm_trace(jit_State *J, GCtrace *T) | |||
1845 | as->parent = J->parent ? traceref(J, J->parent) : NULL; | 2398 | as->parent = J->parent ? traceref(J, J->parent) : NULL; |
1846 | 2399 | ||
1847 | /* Reserve MCode memory. */ | 2400 | /* Reserve MCode memory. */ |
1848 | as->mctop = origtop = lj_mcode_reserve(J, &as->mcbot); | 2401 | as->mctop = as->mctoporig = lj_mcode_reserve(J, &as->mcbot); |
1849 | as->mcp = as->mctop; | 2402 | as->mcp = as->mctop; |
1850 | as->mclim = as->mcbot + MCLIM_REDZONE; | 2403 | as->mclim = as->mcbot + MCLIM_REDZONE; |
1851 | asm_setup_target(as); | 2404 | asm_setup_target(as); |
1852 | 2405 | ||
1853 | do { | 2406 | /* |
2407 | ** This is a loop, because the MCode may have to be (re-)assembled | ||
2408 | ** multiple times: | ||
2409 | ** | ||
2410 | ** 1. as->realign is set (and the assembly aborted), if the arch-specific | ||
2411 | ** backend wants the MCode to be aligned differently. | ||
2412 | ** | ||
2413 | ** This is currently only the case on x86/x64, where small loops get | ||
2414 | ** an aligned loop body plus a short branch. Not much effort is wasted, | ||
2415 | ** because the abort happens very quickly and only once. | ||
2416 | ** | ||
2417 | ** 2. The IR is immovable, since the MCode embeds pointers to various | ||
2418 | ** constants inside the IR. But RENAMEs may need to be added to the IR | ||
2419 | ** during assembly, which might grow and reallocate the IR. We check | ||
2420 | ** at the end if the IR (in J->cur.ir) has actually grown, resize the | ||
2421 | ** copy (in J->curfinal.ir) and try again. | ||
2422 | ** | ||
2423 | ** 95% of all traces have zero RENAMEs, 3% have one RENAME, 1.5% have | ||
2424 | ** 2 RENAMEs and only 0.5% have more than that. That's why we opt to | ||
2425 | ** always have one spare slot in the IR (see above), which means we | ||
2426 | ** have to redo the assembly for only ~2% of all traces. | ||
2427 | ** | ||
2428 | ** Very, very rarely, this needs to be done repeatedly, since the | ||
2429 | ** location of constants inside the IR (actually, reachability from | ||
2430 | ** a global pointer) may affect register allocation and thus the | ||
2431 | ** number of RENAMEs. | ||
2432 | */ | ||
2433 | for (;;) { | ||
1854 | as->mcp = as->mctop; | 2434 | as->mcp = as->mctop; |
1855 | #ifdef LUA_USE_ASSERT | 2435 | #ifdef LUA_USE_ASSERT |
1856 | as->mcp_prev = as->mcp; | 2436 | as->mcp_prev = as->mcp; |
1857 | #endif | 2437 | #endif |
1858 | as->curins = T->nins; | 2438 | as->ir = J->curfinal->ir; /* Use the copied IR. */ |
2439 | as->curins = J->cur.nins = as->orignins; | ||
2440 | |||
1859 | RA_DBG_START(); | 2441 | RA_DBG_START(); |
1860 | RA_DBGX((as, "===== STOP =====")); | 2442 | RA_DBGX((as, "===== STOP =====")); |
1861 | 2443 | ||
@@ -1874,7 +2456,11 @@ void lj_asm_trace(jit_State *J, GCtrace *T) | |||
1874 | /* Assemble a trace in linear backwards order. */ | 2456 | /* Assemble a trace in linear backwards order. */ |
1875 | for (as->curins--; as->curins > as->stopins; as->curins--) { | 2457 | for (as->curins--; as->curins > as->stopins; as->curins--) { |
1876 | IRIns *ir = IR(as->curins); | 2458 | IRIns *ir = IR(as->curins); |
1877 | lua_assert(!(LJ_32 && irt_isint64(ir->t))); /* Handled by SPLIT. */ | 2459 | /* 64 bit types handled by SPLIT for 32 bit archs. */ |
2460 | lj_assertA(!(LJ_32 && irt_isint64(ir->t)), | ||
2461 | "IR %04d has unsplit 64 bit type", | ||
2462 | (int)(ir - as->ir) - REF_BIAS); | ||
2463 | asm_snap_prev(as); | ||
1878 | if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) | 2464 | if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) |
1879 | continue; /* Dead-code elimination can be soooo easy. */ | 2465 | continue; /* Dead-code elimination can be soooo easy. */ |
1880 | if (irt_isguard(ir->t)) | 2466 | if (irt_isguard(ir->t)) |
@@ -1883,22 +2469,43 @@ void lj_asm_trace(jit_State *J, GCtrace *T) | |||
1883 | checkmclim(as); | 2469 | checkmclim(as); |
1884 | asm_ir(as, ir); | 2470 | asm_ir(as, ir); |
1885 | } | 2471 | } |
1886 | } while (as->realign); /* Retry in case the MCode needs to be realigned. */ | ||
1887 | 2472 | ||
1888 | /* Emit head of trace. */ | 2473 | if (as->realign && J->curfinal->nins >= T->nins) |
1889 | RA_DBG_REF(); | 2474 | continue; /* Retry in case only the MCode needs to be realigned. */ |
1890 | checkmclim(as); | 2475 | |
1891 | if (as->gcsteps > 0) { | 2476 | /* Emit head of trace. */ |
1892 | as->curins = as->T->snap[0].ref; | 2477 | RA_DBG_REF(); |
1893 | asm_snap_prep(as); /* The GC check is a guard. */ | 2478 | checkmclim(as); |
1894 | asm_gc_check(as); | 2479 | if (as->gcsteps > 0) { |
2480 | as->curins = as->T->snap[0].ref; | ||
2481 | asm_snap_prep(as); /* The GC check is a guard. */ | ||
2482 | asm_gc_check(as); | ||
2483 | as->curins = as->stopins; | ||
2484 | } | ||
2485 | ra_evictk(as); | ||
2486 | if (as->parent) | ||
2487 | asm_head_side(as); | ||
2488 | else | ||
2489 | asm_head_root(as); | ||
2490 | asm_phi_fixup(as); | ||
2491 | |||
2492 | if (J->curfinal->nins >= T->nins) { /* IR didn't grow? */ | ||
2493 | lj_assertA(J->curfinal->nk == T->nk, "unexpected IR constant growth"); | ||
2494 | memcpy(J->curfinal->ir + as->orignins, T->ir + as->orignins, | ||
2495 | (T->nins - as->orignins) * sizeof(IRIns)); /* Copy RENAMEs. */ | ||
2496 | T->nins = J->curfinal->nins; | ||
2497 | /* Fill mcofs of any unprocessed snapshots. */ | ||
2498 | as->curins = REF_FIRST; | ||
2499 | asm_snap_prev(as); | ||
2500 | break; /* Done. */ | ||
2501 | } | ||
2502 | |||
2503 | /* Otherwise try again with a bigger IR. */ | ||
2504 | lj_trace_free(J2G(J), J->curfinal); | ||
2505 | J->curfinal = NULL; /* In case lj_trace_alloc() OOMs. */ | ||
2506 | J->curfinal = lj_trace_alloc(J->L, T); | ||
2507 | as->realign = NULL; | ||
1895 | } | 2508 | } |
1896 | ra_evictk(as); | ||
1897 | if (as->parent) | ||
1898 | asm_head_side(as); | ||
1899 | else | ||
1900 | asm_head_root(as); | ||
1901 | asm_phi_fixup(as); | ||
1902 | 2509 | ||
1903 | RA_DBGX((as, "===== START ====")); | 2510 | RA_DBGX((as, "===== START ====")); |
1904 | RA_DBG_FLUSH(); | 2511 | RA_DBG_FLUSH(); |
@@ -1911,7 +2518,11 @@ void lj_asm_trace(jit_State *J, GCtrace *T) | |||
1911 | if (!as->loopref) | 2518 | if (!as->loopref) |
1912 | asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ | 2519 | asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ |
1913 | T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); | 2520 | T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); |
1914 | lj_mcode_sync(T->mcode, origtop); | 2521 | asm_snap_fixup_mcofs(as); |
2522 | #if LJ_TARGET_MCODE_FIXUP | ||
2523 | asm_mcode_fixup(T->mcode, T->szmcode); | ||
2524 | #endif | ||
2525 | lj_mcode_sync(T->mcode, as->mctoporig); | ||
1915 | } | 2526 | } |
1916 | 2527 | ||
1917 | #undef IR | 2528 | #undef IR |