src/lj_asm_ppc.h - luajit-2.0-src

Global variables defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** PPC IR assembler (SSA IR -> machine code).
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. /* -- Register allocator extensions --------------------------------------- */

  6. /* Allocate a register with a hint. */
  7. static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
  8. {
  9.   Reg r = IR(ref)->r;
  10.   if (ra_noreg(r)) {
  11.     if (!ra_hashint(r) && !iscrossref(as, ref))
  12.       ra_sethint(IR(ref)->r, hint);  /* Propagate register hint. */
  13.     r = ra_allocref(as, ref, allow);
  14.   }
  15.   ra_noweak(as, r);
  16.   return r;
  17. }

  18. /* Allocate two source registers for three-operand instructions. */
  19. static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
  20. {
  21.   IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
  22.   Reg left = irl->r, right = irr->r;
  23.   if (ra_hasreg(left)) {
  24.     ra_noweak(as, left);
  25.     if (ra_noreg(right))
  26.       right = ra_allocref(as, ir->op2, rset_exclude(allow, left));
  27.     else
  28.       ra_noweak(as, right);
  29.   } else if (ra_hasreg(right)) {
  30.     ra_noweak(as, right);
  31.     left = ra_allocref(as, ir->op1, rset_exclude(allow, right));
  32.   } else if (ra_hashint(right)) {
  33.     right = ra_allocref(as, ir->op2, allow);
  34.     left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));
  35.   } else {
  36.     left = ra_allocref(as, ir->op1, allow);
  37.     right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));
  38.   }
  39.   return left | (right << 8);
  40. }

  41. /* -- Guard handling ------------------------------------------------------ */

  42. /* Setup exit stubs after the end of each trace. */
  43. static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
  44. {
  45.   ExitNo i;
  46.   MCode *mxp = as->mctop;
  47.   if (mxp - (nexits + 3 + MCLIM_REDZONE) < as->mclim)
  48.     asm_mclimit(as);
  49.   /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */
  50.   for (i = nexits-1; (int32_t)i >= 0; i--)
  51.     *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2);
  52.   *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno;  /* Read by exit handler. */
  53.   mxp--;
  54.   *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2);
  55.   *--mxp = PPCI_MFLR|PPCF_T(RID_TMP);
  56.   as->mctop = mxp;
  57. }

  58. static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)
  59. {
  60.   /* Keep this in-sync with exitstub_trace_addr(). */
  61.   return as->mctop + exitno + 3;
  62. }

  63. /* Emit conditional branch to exit for guard. */
  64. static void asm_guardcc(ASMState *as, PPCCC cc)
  65. {
  66.   MCode *target = asm_exitstub_addr(as, as->snapno);
  67.   MCode *p = as->mcp;
  68.   if (LJ_UNLIKELY(p == as->invmcp)) {
  69.     as->loopinv = 1;
  70.     *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2);
  71.     emit_condbranch(as, PPCI_BC, cc^4, p);
  72.     return;
  73.   }
  74.   emit_condbranch(as, PPCI_BC, cc, target);
  75. }

  76. /* -- Operand fusion ------------------------------------------------------ */

  77. /* Limit linear search to this distance. Avoids O(n^2) behavior. */
  78. #define CONFLICT_SEARCH_LIM        31

  79. /* Check if there's no conflicting instruction between curins and ref. */
  80. static int noconflict(ASMState *as, IRRef ref, IROp conflict)
  81. {
  82.   IRIns *ir = as->ir;
  83.   IRRef i = as->curins;
  84.   if (i > ref + CONFLICT_SEARCH_LIM)
  85.     return 0/* Give up, ref is too far away. */
  86.   while (--i > ref)
  87.     if (ir[i].o == conflict)
  88.       return 0/* Conflict found. */
  89.   return 1/* Ok, no conflict. */
  90. }

  91. /* Fuse the array base of colocated arrays. */
  92. static int32_t asm_fuseabase(ASMState *as, IRRef ref)
  93. {
  94.   IRIns *ir = IR(ref);
  95.   if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
  96.       !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
  97.     return (int32_t)sizeof(GCtab);
  98.   return 0;
  99. }

  100. /* Indicates load/store indexed is ok. */
  101. #define AHUREF_LSX        ((int32_t)0x80000000)

  102. /* Fuse array/hash/upvalue reference into register+offset operand. */
  103. static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
  104. {
  105.   IRIns *ir = IR(ref);
  106.   if (ra_noreg(ir->r)) {
  107.     if (ir->o == IR_AREF) {
  108.       if (mayfuse(as, ref)) {
  109.         if (irref_isk(ir->op2)) {
  110.           IRRef tab = IR(ir->op1)->op1;
  111.           int32_t ofs = asm_fuseabase(as, tab);
  112.           IRRef refa = ofs ? tab : ir->op1;
  113.           ofs += 8*IR(ir->op2)->i;
  114.           if (checki16(ofs)) {
  115.             *ofsp = ofs;
  116.             return ra_alloc1(as, refa, allow);
  117.           }
  118.         }
  119.         if (*ofsp == AHUREF_LSX) {
  120.           Reg base = ra_alloc1(as, ir->op1, allow);
  121.           Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
  122.           return base | (idx << 8);
  123.         }
  124.       }
  125.     } else if (ir->o == IR_HREFK) {
  126.       if (mayfuse(as, ref)) {
  127.         int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
  128.         if (checki16(ofs)) {
  129.           *ofsp = ofs;
  130.           return ra_alloc1(as, ir->op1, allow);
  131.         }
  132.       }
  133.     } else if (ir->o == IR_UREFC) {
  134.       if (irref_isk(ir->op1)) {
  135.         GCfunc *fn = ir_kfunc(IR(ir->op1));
  136.         int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);
  137.         int32_t jgl = (intptr_t)J2G(as->J);
  138.         if ((uint32_t)(ofs-jgl) < 65536) {
  139.           *ofsp = ofs-jgl-32768;
  140.           return RID_JGL;
  141.         } else {
  142.           *ofsp = (int16_t)ofs;
  143.           return ra_allock(as, ofs-(int16_t)ofs, allow);
  144.         }
  145.       }
  146.     }
  147.   }
  148.   *ofsp = 0;
  149.   return ra_alloc1(as, ref, allow);
  150. }

  151. /* Fuse XLOAD/XSTORE reference into load/store operand. */
  152. static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref,
  153.                          RegSet allow, int32_t ofs)
  154. {
  155.   IRIns *ir = IR(ref);
  156.   Reg base;
  157.   if (ra_noreg(ir->r) && canfuse(as, ir)) {
  158.     if (ir->o == IR_ADD) {
  159.       int32_t ofs2;
  160.       if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) {
  161.         ofs = ofs2;
  162.         ref = ir->op1;
  163.       } else if (ofs == 0) {
  164.         Reg right, left = ra_alloc2(as, ir, allow);
  165.         right = (left >> 8); left &= 255;
  166.         emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
  167.         return;
  168.       }
  169.     } else if (ir->o == IR_STRREF) {
  170.       lua_assert(ofs == 0);
  171.       ofs = (int32_t)sizeof(GCstr);
  172.       if (irref_isk(ir->op2)) {
  173.         ofs += IR(ir->op2)->i;
  174.         ref = ir->op1;
  175.       } else if (irref_isk(ir->op1)) {
  176.         ofs += IR(ir->op1)->i;
  177.         ref = ir->op2;
  178.       } else {
  179.         /* NYI: Fuse ADD with constant. */
  180.         Reg tmp, right, left = ra_alloc2(as, ir, allow);
  181.         right = (left >> 8); left &= 255;
  182.         tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right));
  183.         emit_fai(as, pi, rt, tmp, ofs);
  184.         emit_tab(as, PPCI_ADD, tmp, left, right);
  185.         return;
  186.       }
  187.       if (!checki16(ofs)) {
  188.         Reg left = ra_alloc1(as, ref, allow);
  189.         Reg right = ra_allock(as, ofs, rset_exclude(allow, left));
  190.         emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
  191.         return;
  192.       }
  193.     }
  194.   }
  195.   base = ra_alloc1(as, ref, allow);
  196.   emit_fai(as, pi, rt, base, ofs);
  197. }

  198. /* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */
  199. static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref,
  200.                           RegSet allow)
  201. {
  202.   IRIns *ira = IR(ref);
  203.   Reg right, left;
  204.   if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) {
  205.     left = ra_alloc2(as, ira, allow);
  206.     right = (left >> 8); left &= 255;
  207.   } else {
  208.     right = ra_alloc1(as, ref, allow);
  209.     left = RID_R0;
  210.   }
  211.   emit_tab(as, pi, rt, left, right);
  212. }

  213. /* Fuse to multiply-add/sub instruction. */
  214. static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir)
  215. {
  216.   IRRef lref = ir->op1, rref = ir->op2;
  217.   IRIns *irm;
  218.   if (lref != rref &&
  219.       ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
  220.         ra_noreg(irm->r)) ||
  221.        (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
  222.         (rref = lref, pi = pir, ra_noreg(irm->r))))) {
  223.     Reg dest = ra_dest(as, ir, RSET_FPR);
  224.     Reg add = ra_alloc1(as, rref, RSET_FPR);
  225.     Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add));
  226.     right = (left >> 8); left &= 255;
  227.     emit_facb(as, pi, dest, left, right, add);
  228.     return 1;
  229.   }
  230.   return 0;
  231. }

  232. /* -- Calls --------------------------------------------------------------- */

  233. /* Generate a call to a C function. */
  234. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
  235. {
  236.   uint32_t n, nargs = CCI_XNARGS(ci);
  237.   int32_t ofs = 8;
  238.   Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR;
  239.   if ((void *)ci->func)
  240.     emit_call(as, (void *)ci->func);
  241.   for (n = 0; n < nargs; n++) {  /* Setup args. */
  242.     IRRef ref = args[n];
  243.     if (ref) {
  244.       IRIns *ir = IR(ref);
  245.       if (irt_isfp(ir->t)) {
  246.         if (fpr <= REGARG_LASTFPR) {
  247.           lua_assert(rset_test(as->freeset, fpr));  /* Already evicted. */
  248.           ra_leftov(as, fpr, ref);
  249.           fpr++;
  250.         } else {
  251.           Reg r = ra_alloc1(as, ref, RSET_FPR);
  252.           if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
  253.           emit_spstore(as, ir, r, ofs);
  254.           ofs += irt_isnum(ir->t) ? 8 : 4;
  255.         }
  256.       } else {
  257.         if (gpr <= REGARG_LASTGPR) {
  258.           lua_assert(rset_test(as->freeset, gpr));  /* Already evicted. */
  259.           ra_leftov(as, gpr, ref);
  260.           gpr++;
  261.         } else {
  262.           Reg r = ra_alloc1(as, ref, RSET_GPR);
  263.           emit_spstore(as, ir, r, ofs);
  264.           ofs += 4;
  265.         }
  266.       }
  267.     } else {
  268.       if (gpr <= REGARG_LASTGPR)
  269.         gpr++;
  270.       else
  271.         ofs += 4;
  272.     }
  273.     checkmclim(as);
  274.   }
  275.   if ((ci->flags & CCI_VARARG))  /* Vararg calls need to know about FPR use. */
  276.     emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6);
  277. }

  278. /* Setup result reg/sp for call. Evict scratch regs. */
  279. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
  280. {
  281.   RegSet drop = RSET_SCRATCH;
  282.   int hiop = ((ir+1)->o == IR_HIOP);
  283.   if ((ci->flags & CCI_NOFPRCLOBBER))
  284.     drop &= ~RSET_FPR;
  285.   if (ra_hasreg(ir->r))
  286.     rset_clear(drop, ir->r);  /* Dest reg handled below. */
  287.   if (hiop && ra_hasreg((ir+1)->r))
  288.     rset_clear(drop, (ir+1)->r);  /* Dest reg handled below. */
  289.   ra_evictset(as, drop);  /* Evictions must be performed first. */
  290.   if (ra_used(ir)) {
  291.     lua_assert(!irt_ispri(ir->t));
  292.     if (irt_isfp(ir->t)) {
  293.       if ((ci->flags & CCI_CASTU64)) {
  294.         /* Use spill slot or temp slots. */
  295.         int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
  296.         Reg dest = ir->r;
  297.         if (ra_hasreg(dest)) {
  298.           ra_free(as, dest);
  299.           ra_modified(as, dest);
  300.           emit_fai(as, PPCI_LFD, dest, RID_SP, ofs);
  301.         }
  302.         emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs);
  303.         emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4);
  304.       } else {
  305.         ra_destreg(as, ir, RID_FPRET);
  306.       }
  307. #if LJ_32
  308.     } else if (hiop) {
  309.       ra_destpair(as, ir);
  310. #endif
  311.     } else {
  312.       ra_destreg(as, ir, RID_RET);
  313.     }
  314.   }
  315. }

  316. static void asm_callx(ASMState *as, IRIns *ir)
  317. {
  318.   IRRef args[CCI_NARGS_MAX*2];
  319.   CCallInfo ci;
  320.   IRRef func;
  321.   IRIns *irf;
  322.   ci.flags = asm_callx_flags(as, ir);
  323.   asm_collectargs(as, ir, &ci, args);
  324.   asm_setupresult(as, ir, &ci);
  325.   func = ir->op2; irf = IR(func);
  326.   if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
  327.   if (irref_isk(func)) {  /* Call to constant address. */
  328.     ci.func = (ASMFunction)(void *)(intptr_t)(irf->i);
  329.   } else/* Need a non-argument register for indirect calls. */
  330.     RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1);
  331.     Reg freg = ra_alloc1(as, func, allow);
  332.     *--as->mcp = PPCI_BCTRL;
  333.     *--as->mcp = PPCI_MTCTR | PPCF_T(freg);
  334.     ci.func = (ASMFunction)(void *)0;
  335.   }
  336.   asm_gencall(as, &ci, args);
  337. }

  338. /* -- Returns ------------------------------------------------------------- */

  339. /* Return to lower frame. Guard that it goes to the right spot. */
  340. static void asm_retf(ASMState *as, IRIns *ir)
  341. {
  342.   Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
  343.   void *pc = ir_kptr(IR(ir->op2));
  344.   int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
  345.   as->topslot -= (BCReg)delta;
  346.   if ((int32_t)as->topslot < 0) as->topslot = 0;
  347.   irt_setmark(IR(REF_BASE)->t);  /* Children must not coalesce with BASE reg. */
  348.   emit_setgl(as, base, jit_base);
  349.   emit_addptr(as, base, -8*delta);
  350.   asm_guardcc(as, CC_NE);
  351.   emit_ab(as, PPCI_CMPW, RID_TMP,
  352.           ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));
  353.   emit_tai(as, PPCI_LWZ, RID_TMP, base, -8);
  354. }

  355. /* -- Type conversions ---------------------------------------------------- */

  356. static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
  357. {
  358.   RegSet allow = RSET_FPR;
  359.   Reg tmp = ra_scratch(as, rset_clear(allow, left));
  360.   Reg fbias = ra_scratch(as, rset_clear(allow, tmp));
  361.   Reg dest = ra_dest(as, ir, RSET_GPR);
  362.   Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest));
  363.   asm_guardcc(as, CC_NE);
  364.   emit_fab(as, PPCI_FCMPU, 0, tmp, left);
  365.   emit_fab(as, PPCI_FSUB, tmp, tmp, fbias);
  366.   emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP);
  367.   emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO);
  368.   emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
  369.   emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000);
  370.   emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
  371.   emit_lsptr(as, PPCI_LFS, (fbias & 31),
  372.              (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
  373.              RSET_GPR);
  374.   emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
  375.   emit_fb(as, PPCI_FCTIWZ, tmp, left);
  376. }

  377. static void asm_tobit(ASMState *as, IRIns *ir)
  378. {
  379.   RegSet allow = RSET_FPR;
  380.   Reg dest = ra_dest(as, ir, RSET_GPR);
  381.   Reg left = ra_alloc1(as, ir->op1, allow);
  382.   Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
  383.   Reg tmp = ra_scratch(as, rset_clear(allow, right));
  384.   emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
  385.   emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
  386.   emit_fab(as, PPCI_FADD, tmp, left, right);
  387. }

  388. static void asm_conv(ASMState *as, IRIns *ir)
  389. {
  390.   IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  391.   int stfp = (st == IRT_NUM || st == IRT_FLOAT);
  392.   IRRef lref = ir->op1;
  393.   lua_assert(irt_type(ir->t) != st);
  394.   lua_assert(!(irt_isint64(ir->t) ||
  395.                (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */
  396.   if (irt_isfp(ir->t)) {
  397.     Reg dest = ra_dest(as, ir, RSET_FPR);
  398.     if (stfp) {  /* FP to FP conversion. */
  399.       if (st == IRT_NUM)  /* double -> float conversion. */
  400.         emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR));
  401.       else  /* float -> double conversion is a no-op on PPC. */
  402.         ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */
  403.     } else/* Integer to FP conversion. */
  404.       /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */
  405.       /* IRT_U32: Bias with 2^52, subtract 2^52. */
  406.       RegSet allow = RSET_GPR;
  407.       Reg left = ra_alloc1(as, lref, allow);
  408.       Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left));
  409.       Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
  410.       const float *kbias;
  411.       if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest);
  412.       emit_fab(as, PPCI_FSUB, dest, dest, fbias);
  413.       emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
  414.       kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000));
  415.       if (st == IRT_U32) kbias++;
  416.       emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias,
  417.                  rset_clear(allow, hibias));
  418.       emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP,
  419.                RID_SP, SPOFS_TMPLO);
  420.       emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
  421.       if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000);
  422.     }
  423.   } else if (stfp) {  /* FP to integer conversion. */
  424.     if (irt_isguard(ir->t)) {
  425.       /* Checked conversions are only supported from number to int. */
  426.       lua_assert(irt_isint(ir->t) && st == IRT_NUM);
  427.       asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
  428.     } else {
  429.       Reg dest = ra_dest(as, ir, RSET_GPR);
  430.       Reg left = ra_alloc1(as, lref, RSET_FPR);
  431.       Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  432.       if (irt_isu32(ir->t)) {
  433.         /* Convert both x and x-2^31 to int and merge results. */
  434.         Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest));
  435.         emit_asb(as, PPCI_OR, dest, dest, tmpi);  /* Select with mask idiom. */
  436.         emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP);
  437.         emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP);
  438.         emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO);  /* tmp = (int)(x) */
  439.         emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000);  /* dest += 2^31 */
  440.         emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31);  /* mask = -(dest < 0) */
  441.         emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
  442.         emit_tai(as, PPCI_LWZ, dest,
  443.                  RID_SP, SPOFS_TMPLO);  /* dest = (int)(x-2^31) */
  444.         emit_fb(as, PPCI_FCTIWZ, tmp, left);
  445.         emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
  446.         emit_fb(as, PPCI_FCTIWZ, tmp, tmp);
  447.         emit_fab(as, PPCI_FSUB, tmp, left, tmp);
  448.         emit_lsptr(as, PPCI_LFS, (tmp & 31),
  449.                    (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)),
  450.                    RSET_GPR);
  451.       } else {
  452.         emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
  453.         emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
  454.         emit_fb(as, PPCI_FCTIWZ, tmp, left);
  455.       }
  456.     }
  457.   } else {
  458.     Reg dest = ra_dest(as, ir, RSET_GPR);
  459.     if (st >= IRT_I8 && st <= IRT_U16) {  /* Extend to 32 bit integer. */
  460.       Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  461.       lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));
  462.       if ((ir->op2 & IRCONV_SEXT))
  463.         emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left);
  464.       else
  465.         emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31);
  466.     } else/* 32/64 bit integer conversions. */
  467.       /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
  468.       ra_leftov(as, dest, lref);  /* Do nothing, but may need to move regs. */
  469.     }
  470.   }
  471. }

  472. static void asm_strto(ASMState *as, IRIns *ir)
  473. {
  474.   const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
  475.   IRRef args[2];
  476.   int32_t ofs;
  477.   RegSet drop = RSET_SCRATCH;
  478.   if (ra_hasreg(ir->r)) rset_set(drop, ir->r);  /* Spill dest reg (if any). */
  479.   ra_evictset(as, drop);
  480.   asm_guardcc(as, CC_EQ);
  481.   emit_ai(as, PPCI_CMPWI, RID_RET, 0);  /* Test return status. */
  482.   args[0] = ir->op1;      /* GCstr *str */
  483.   args[1] = ASMREF_TMP1/* TValue *n  */
  484.   asm_gencall(as, ci, args);
  485.   /* Store the result to the spill slot or temp slots. */
  486.   ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
  487.   emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs);
  488. }

  489. /* -- Memory references --------------------------------------------------- */

  490. /* Get pointer to TValue. */
  491. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)
  492. {
  493.   IRIns *ir = IR(ref);
  494.   if (irt_isnum(ir->t)) {
  495.     if (irref_isk(ref))  /* Use the number constant itself as a TValue. */
  496.       ra_allockreg(as, i32ptr(ir_knum(ir)), dest);
  497.     else  /* Otherwise force a spill and use the spill slot. */
  498.       emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir));
  499.   } else {
  500.     /* Otherwise use g->tmptv to hold the TValue. */
  501.     RegSet allow = rset_exclude(RSET_GPR, dest);
  502.     Reg type;
  503.     emit_tai(as, PPCI_ADDI, dest, RID_JGL, (int32_t)offsetof(global_State, tmptv)-32768);
  504.     if (!irt_ispri(ir->t)) {
  505.       Reg src = ra_alloc1(as, ref, allow);
  506.       emit_setgl(as, src, tmptv.gcr);
  507.     }
  508.     type = ra_allock(as, irt_toitype(ir->t), allow);
  509.     emit_setgl(as, type, tmptv.it);
  510.   }
  511. }

  512. static void asm_aref(ASMState *as, IRIns *ir)
  513. {
  514.   Reg dest = ra_dest(as, ir, RSET_GPR);
  515.   Reg idx, base;
  516.   if (irref_isk(ir->op2)) {
  517.     IRRef tab = IR(ir->op1)->op1;
  518.     int32_t ofs = asm_fuseabase(as, tab);
  519.     IRRef refa = ofs ? tab : ir->op1;
  520.     ofs += 8*IR(ir->op2)->i;
  521.     if (checki16(ofs)) {
  522.       base = ra_alloc1(as, refa, RSET_GPR);
  523.       emit_tai(as, PPCI_ADDI, dest, base, ofs);
  524.       return;
  525.     }
  526.   }
  527.   base = ra_alloc1(as, ir->op1, RSET_GPR);
  528.   idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
  529.   emit_tab(as, PPCI_ADD, dest, RID_TMP, base);
  530.   emit_slwi(as, RID_TMP, idx, 3);
  531. }

  532. /* Inlined hash lookup. Specialized for key type and for const keys.
  533. ** The equivalent C code is:
  534. **   Node *n = hashkey(t, key);
  535. **   do {
  536. **     if (lj_obj_equal(&n->key, key)) return &n->val;
  537. **   } while ((n = nextnode(n)));
  538. **   return niltv(L);
  539. */
  540. static void asm_href(ASMState *as, IRIns *ir, IROp merge)
  541. {
  542.   RegSet allow = RSET_GPR;
  543.   int destused = ra_used(ir);
  544.   Reg dest = ra_dest(as, ir, allow);
  545.   Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
  546.   Reg key = RID_NONE, tmp1 = RID_TMP, tmp2;
  547.   Reg tisnum = RID_NONE, tmpnum = RID_NONE;
  548.   IRRef refkey = ir->op2;
  549.   IRIns *irkey = IR(refkey);
  550.   IRType1 kt = irkey->t;
  551.   uint32_t khash;
  552.   MCLabel l_end, l_loop, l_next;

  553.   rset_clear(allow, tab);
  554.   if (irt_isnum(kt)) {
  555.     key = ra_alloc1(as, refkey, RSET_FPR);
  556.     tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
  557.     tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
  558.     rset_clear(allow, tisnum);
  559.   } else if (!irt_ispri(kt)) {
  560.     key = ra_alloc1(as, refkey, allow);
  561.     rset_clear(allow, key);
  562.   }
  563.   tmp2 = ra_scratch(as, allow);
  564.   rset_clear(allow, tmp2);

  565.   /* Key not found in chain: jump to exit (if merged) or load niltv. */
  566.   l_end = emit_label(as);
  567.   as->invmcp = NULL;
  568.   if (merge == IR_NE)
  569.     asm_guardcc(as, CC_EQ);
  570.   else if (destused)
  571.     emit_loada(as, dest, niltvg(J2G(as->J)));

  572.   /* Follow hash chain until the end. */
  573.   l_loop = --as->mcp;
  574.   emit_ai(as, PPCI_CMPWI, dest, 0);
  575.   emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next));
  576.   l_next = emit_label(as);

  577.   /* Type and value comparison. */
  578.   if (merge == IR_EQ)
  579.     asm_guardcc(as, CC_EQ);
  580.   else
  581.     emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
  582.   if (irt_isnum(kt)) {
  583.     emit_fab(as, PPCI_FCMPU, 0, tmpnum, key);
  584.     emit_condbranch(as, PPCI_BC, CC_GE, l_next);
  585.     emit_ab(as, PPCI_CMPLW, tmp1, tisnum);
  586.     emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n));
  587.   } else {
  588.     if (!irt_ispri(kt)) {
  589.       emit_ab(as, PPCI_CMPW, tmp2, key);
  590.       emit_condbranch(as, PPCI_BC, CC_NE, l_next);
  591.     }
  592.     emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t));
  593.     if (!irt_ispri(kt))
  594.       emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr));
  595.   }
  596.   emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it));
  597.   *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) |
  598.             (((char *)as->mcp-(char *)l_loop) & 0xffffu);

  599.   /* Load main position relative to tab->node into dest. */
  600.   khash = irref_isk(refkey) ? ir_khash(irkey) : 1;
  601.   if (khash == 0) {
  602.     emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
  603.   } else {
  604.     Reg tmphash = tmp1;
  605.     if (irref_isk(refkey))
  606.       tmphash = ra_allock(as, khash, allow);
  607.     emit_tab(as, PPCI_ADD, dest, dest, tmp1);
  608.     emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node));
  609.     emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash);
  610.     emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
  611.     emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
  612.     if (irref_isk(refkey)) {
  613.       /* Nothing to do. */
  614.     } else if (irt_isstr(kt)) {
  615.       emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash));
  616.     } else/* Must match with hash*() in lj_tab.c. */
  617.       emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1);
  618.       emit_rotlwi(as, tmp2, tmp2, HASH_ROT3);
  619.       emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2);
  620.       emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31);
  621.       emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2);
  622.       if (irt_isnum(kt)) {
  623.         int32_t ofs = ra_spill(as, irkey);
  624.         emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1);
  625.         emit_rotlwi(as, dest, tmp1, HASH_ROT1);
  626.         emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1);
  627.         emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4);
  628.         emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs);
  629.       } else {
  630.         emit_asb(as, PPCI_XOR, tmp2, key, tmp1);
  631.         emit_rotlwi(as, dest, tmp1, HASH_ROT1);
  632.         emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS);
  633.         emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16);
  634.       }
  635.     }
  636.   }
  637. }

  638. static void asm_hrefk(ASMState *as, IRIns *ir)
  639. {
  640.   IRIns *kslot = IR(ir->op2);
  641.   IRIns *irkey = IR(kslot->op1);
  642.   int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
  643.   int32_t kofs = ofs + (int32_t)offsetof(Node, key);
  644.   Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
  645.   Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
  646.   Reg key = RID_NONE, type = RID_TMP, idx = node;
  647.   RegSet allow = rset_exclude(RSET_GPR, node);
  648.   lua_assert(ofs % sizeof(Node) == 0);
  649.   if (ofs > 32736) {
  650.     idx = dest;
  651.     rset_clear(allow, dest);
  652.     kofs = (int32_t)offsetof(Node, key);
  653.   } else if (ra_hasreg(dest)) {
  654.     emit_tai(as, PPCI_ADDI, dest, node, ofs);
  655.   }
  656.   asm_guardcc(as, CC_NE);
  657.   if (!irt_ispri(irkey->t)) {
  658.     key = ra_scratch(as, allow);
  659.     rset_clear(allow, key);
  660.   }
  661.   rset_clear(allow, type);
  662.   if (irt_isnum(irkey->t)) {
  663.     emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo);
  664.     asm_guardcc(as, CC_NE);
  665.     emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi);
  666.   } else {
  667.     if (ra_hasreg(key)) {
  668.       emit_cmpi(as, key, irkey->i);  /* May use RID_TMP, i.e. type. */
  669.       asm_guardcc(as, CC_NE);
  670.     }
  671.     emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t));
  672.   }
  673.   if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4);
  674.   emit_tai(as, PPCI_LWZ, type, idx, kofs);
  675.   if (ofs > 32736) {
  676.     emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16);
  677.     emit_tai(as, PPCI_ADDI, dest, node, ofs);
  678.   }
  679. }

  680. static void asm_uref(ASMState *as, IRIns *ir)
  681. {
  682.   /* NYI: Check that UREFO is still open and not aliasing a slot. */
  683.   Reg dest = ra_dest(as, ir, RSET_GPR);
  684.   if (irref_isk(ir->op1)) {
  685.     GCfunc *fn = ir_kfunc(IR(ir->op1));
  686.     MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
  687.     emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR);
  688.   } else {
  689.     Reg uv = ra_scratch(as, RSET_GPR);
  690.     Reg func = ra_alloc1(as, ir->op1, RSET_GPR);
  691.     if (ir->o == IR_UREFC) {
  692.       asm_guardcc(as, CC_NE);
  693.       emit_ai(as, PPCI_CMPWI, RID_TMP, 1);
  694.       emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv));
  695.       emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));
  696.     } else {
  697.       emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v));
  698.     }
  699.     emit_tai(as, PPCI_LWZ, uv, func,
  700.              (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));
  701.   }
  702. }

  703. static void asm_fref(ASMState *as, IRIns *ir)
  704. {
  705.   UNUSED(as); UNUSED(ir);
  706.   lua_assert(!ra_used(ir));
  707. }

  708. static void asm_strref(ASMState *as, IRIns *ir)
  709. {
  710.   Reg dest = ra_dest(as, ir, RSET_GPR);
  711.   IRRef ref = ir->op2, refk = ir->op1;
  712.   int32_t ofs = (int32_t)sizeof(GCstr);
  713.   Reg r;
  714.   if (irref_isk(ref)) {
  715.     IRRef tmp = refk; refk = ref; ref = tmp;
  716.   } else if (!irref_isk(refk)) {
  717.     Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  718.     IRIns *irr = IR(ir->op2);
  719.     if (ra_hasreg(irr->r)) {
  720.       ra_noweak(as, irr->r);
  721.       right = irr->r;
  722.     } else if (mayfuse(as, irr->op2) &&
  723.                irr->o == IR_ADD && irref_isk(irr->op2) &&
  724.                checki16(ofs + IR(irr->op2)->i)) {
  725.       ofs += IR(irr->op2)->i;
  726.       right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
  727.     } else {
  728.       right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
  729.     }
  730.     emit_tai(as, PPCI_ADDI, dest, dest, ofs);
  731.     emit_tab(as, PPCI_ADD, dest, left, right);
  732.     return;
  733.   }
  734.   r = ra_alloc1(as, ref, RSET_GPR);
  735.   ofs += IR(refk)->i;
  736.   if (checki16(ofs))
  737.     emit_tai(as, PPCI_ADDI, dest, r, ofs);
  738.   else
  739.     emit_tab(as, PPCI_ADD, dest, r,
  740.              ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
  741. }

  742. /* -- Loads and stores ---------------------------------------------------- */

  743. static PPCIns asm_fxloadins(IRIns *ir)
  744. {
  745.   switch (irt_type(ir->t)) {
  746.   case IRT_I8: return PPCI_LBZ;  /* Needs sign-extension. */
  747.   case IRT_U8: return PPCI_LBZ;
  748.   case IRT_I16: return PPCI_LHA;
  749.   case IRT_U16: return PPCI_LHZ;
  750.   case IRT_NUM: return PPCI_LFD;
  751.   case IRT_FLOAT: return PPCI_LFS;
  752.   default: return PPCI_LWZ;
  753.   }
  754. }

  755. static PPCIns asm_fxstoreins(IRIns *ir)
  756. {
  757.   switch (irt_type(ir->t)) {
  758.   case IRT_I8: case IRT_U8: return PPCI_STB;
  759.   case IRT_I16: case IRT_U16: return PPCI_STH;
  760.   case IRT_NUM: return PPCI_STFD;
  761.   case IRT_FLOAT: return PPCI_STFS;
  762.   default: return PPCI_STW;
  763.   }
  764. }

  765. static void asm_fload(ASMState *as, IRIns *ir)
  766. {
  767.   Reg dest = ra_dest(as, ir, RSET_GPR);
  768.   Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);
  769.   PPCIns pi = asm_fxloadins(ir);
  770.   int32_t ofs;
  771.   if (ir->op2 == IRFL_TAB_ARRAY) {
  772.     ofs = asm_fuseabase(as, ir->op1);
  773.     if (ofs) {  /* Turn the t->array load into an add for colocated arrays. */
  774.       emit_tai(as, PPCI_ADDI, dest, idx, ofs);
  775.       return;
  776.     }
  777.   }
  778.   ofs = field_ofs[ir->op2];
  779.   lua_assert(!irt_isi8(ir->t));
  780.   emit_tai(as, pi, dest, idx, ofs);
  781. }

  782. static void asm_fstore(ASMState *as, IRIns *ir)
  783. {
  784.   if (ir->r != RID_SINK) {
  785.     Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
  786.     IRIns *irf = IR(ir->op1);
  787.     Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
  788.     int32_t ofs = field_ofs[irf->op2];
  789.     PPCIns pi = asm_fxstoreins(ir);
  790.     emit_tai(as, pi, src, idx, ofs);
  791.   }
  792. }

  793. static void asm_xload(ASMState *as, IRIns *ir)
  794. {
  795.   Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  796.   lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));
  797.   if (irt_isi8(ir->t))
  798.     emit_as(as, PPCI_EXTSB, dest, dest);
  799.   asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);
  800. }

  801. static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
  802. {
  803.   IRIns *irb;
  804.   if (ir->r == RID_SINK)
  805.     return;
  806.   if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP &&
  807.       ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) {
  808.     /* Fuse BSWAP with XSTORE to stwbrx. */
  809.     Reg src = ra_alloc1(as, irb->op1, RSET_GPR);
  810.     asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src));
  811.   } else {
  812.     Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  813.     asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
  814.                  rset_exclude(RSET_GPR, src), ofs);
  815.   }
  816. }

  817. #define asm_xstore(as, ir)        asm_xstore_(as, ir, 0)

  818. static void asm_ahuvload(ASMState *as, IRIns *ir)
  819. {
  820.   IRType1 t = ir->t;
  821.   Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx;
  822.   RegSet allow = RSET_GPR;
  823.   int32_t ofs = AHUREF_LSX;
  824.   if (ra_used(ir)) {
  825.     lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
  826.     if (!irt_isnum(t)) ofs = 0;
  827.     dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
  828.     rset_clear(allow, dest);
  829.   }
  830.   idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  831.   if (irt_isnum(t)) {
  832.     Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx));
  833.     asm_guardcc(as, CC_GE);
  834.     emit_ab(as, PPCI_CMPLW, type, tisnum);
  835.     if (ra_hasreg(dest)) {
  836.       if (ofs == AHUREF_LSX) {
  837.         tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR,
  838.                                                        (idx&255)), (idx>>8)));
  839.         emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp);
  840.       } else {
  841.         emit_fai(as, PPCI_LFD, dest, idx, ofs);
  842.       }
  843.     }
  844.   } else {
  845.     asm_guardcc(as, CC_NE);
  846.     emit_ai(as, PPCI_CMPWI, type, irt_toitype(t));
  847.     if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4);
  848.   }
  849.   if (ofs == AHUREF_LSX) {
  850.     emit_tab(as, PPCI_LWZX, type, (idx&255), tmp);
  851.     emit_slwi(as, tmp, (idx>>8), 3);
  852.   } else {
  853.     emit_tai(as, PPCI_LWZ, type, idx, ofs);
  854.   }
  855. }

  856. static void asm_ahustore(ASMState *as, IRIns *ir)
  857. {
  858.   RegSet allow = RSET_GPR;
  859.   Reg idx, src = RID_NONE, type = RID_NONE;
  860.   int32_t ofs = AHUREF_LSX;
  861.   if (ir->r == RID_SINK)
  862.     return;
  863.   if (irt_isnum(ir->t)) {
  864.     src = ra_alloc1(as, ir->op2, RSET_FPR);
  865.   } else {
  866.     if (!irt_ispri(ir->t)) {
  867.       src = ra_alloc1(as, ir->op2, allow);
  868.       rset_clear(allow, src);
  869.       ofs = 0;
  870.     }
  871.     type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  872.     rset_clear(allow, type);
  873.   }
  874.   idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  875.   if (irt_isnum(ir->t)) {
  876.     if (ofs == AHUREF_LSX) {
  877.       emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP);
  878.       emit_slwi(as, RID_TMP, (idx>>8), 3);
  879.     } else {
  880.       emit_fai(as, PPCI_STFD, src, idx, ofs);
  881.     }
  882.   } else {
  883.     if (ra_hasreg(src))
  884.       emit_tai(as, PPCI_STW, src, idx, ofs+4);
  885.     if (ofs == AHUREF_LSX) {
  886.       emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP);
  887.       emit_slwi(as, RID_TMP, (idx>>8), 3);
  888.     } else {
  889.       emit_tai(as, PPCI_STW, type, idx, ofs);
  890.     }
  891.   }
  892. }

  893. static void asm_sload(ASMState *as, IRIns *ir)
  894. {
  895.   int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4);
  896.   IRType1 t = ir->t;
  897.   Reg dest = RID_NONE, type = RID_NONE, base;
  898.   RegSet allow = RSET_GPR;
  899.   lua_assert(!(ir->op2 & IRSLOAD_PARENT));  /* Handled by asm_head_side(). */
  900.   lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));
  901.   lua_assert(LJ_DUALNUM ||
  902.              !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));
  903.   if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
  904.     dest = ra_scratch(as, RSET_FPR);
  905.     asm_tointg(as, ir, dest);
  906.     t.irt = IRT_NUM;  /* Continue with a regular number type check. */
  907.   } else if (ra_used(ir)) {
  908.     lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
  909.     dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
  910.     rset_clear(allow, dest);
  911.     base = ra_alloc1(as, REF_BASE, allow);
  912.     rset_clear(allow, base);
  913.     if ((ir->op2 & IRSLOAD_CONVERT)) {
  914.       if (irt_isint(t)) {
  915.         emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
  916.         dest = ra_scratch(as, RSET_FPR);
  917.         emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP);
  918.         emit_fb(as, PPCI_FCTIWZ, dest, dest);
  919.         t.irt = IRT_NUM;  /* Check for original type. */
  920.       } else {
  921.         Reg tmp = ra_scratch(as, allow);
  922.         Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp));
  923.         Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
  924.         emit_fab(as, PPCI_FSUB, dest, dest, fbias);
  925.         emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
  926.         emit_lsptr(as, PPCI_LFS, (fbias & 31),
  927.                    (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
  928.                    rset_clear(allow, hibias));
  929.         emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO);
  930.         emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
  931.         emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000);
  932.         dest = tmp;
  933.         t.irt = IRT_INT;  /* Check for original type. */
  934.       }
  935.     }
  936.     goto dotypecheck;
  937.   }
  938.   base = ra_alloc1(as, REF_BASE, allow);
  939.   rset_clear(allow, base);
  940. dotypecheck:
  941.   if (irt_isnum(t)) {
  942.     if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  943.       Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
  944.       asm_guardcc(as, CC_GE);
  945.       emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum);
  946.       type = RID_TMP;
  947.     }
  948.     if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4);
  949.   } else {
  950.     if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  951.       asm_guardcc(as, CC_NE);
  952.       emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t));
  953.       type = RID_TMP;
  954.     }
  955.     if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs);
  956.   }
  957.   if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4);
  958. }

  959. /* -- Allocations --------------------------------------------------------- */

  960. #if LJ_HASFFI
  961. static void asm_cnew(ASMState *as, IRIns *ir)
  962. {
  963.   CTState *cts = ctype_ctsG(J2G(as->J));
  964.   CTypeID id = (CTypeID)IR(ir->op1)->i;
  965.   CTSize sz;
  966.   CTInfo info = lj_ctype_info(cts, id, &sz);
  967.   const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
  968.   IRRef args[4];
  969.   RegSet drop = RSET_SCRATCH;
  970.   lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));

  971.   as->gcsteps++;
  972.   if (ra_hasreg(ir->r))
  973.     rset_clear(drop, ir->r);  /* Dest reg handled below. */
  974.   ra_evictset(as, drop);
  975.   if (ra_used(ir))
  976.     ra_destreg(as, ir, RID_RET);  /* GCcdata * */

  977.   /* Initialize immutable cdata object. */
  978.   if (ir->o == IR_CNEWI) {
  979.     RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  980.     int32_t ofs = sizeof(GCcdata);
  981.     lua_assert(sz == 4 || sz == 8);
  982.     if (sz == 8) {
  983.       ofs += 4;
  984.       lua_assert((ir+1)->o == IR_HIOP);
  985.     }
  986.     for (;;) {
  987.       Reg r = ra_alloc1(as, ir->op2, allow);
  988.       emit_tai(as, PPCI_STW, r, RID_RET, ofs);
  989.       rset_clear(allow, r);
  990.       if (ofs == sizeof(GCcdata)) break;
  991.       ofs -= 4; ir++;
  992.     }
  993.   } else if (ir->op2 != REF_NIL) {  /* Create VLA/VLS/aligned cdata. */
  994.     ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
  995.     args[0] = ASMREF_L;     /* lua_State *L */
  996.     args[1] = ir->op1;      /* CTypeID id   */
  997.     args[2] = ir->op2;      /* CTSize sz    */
  998.     args[3] = ASMREF_TMP1/* CTSize align */
  999.     asm_gencall(as, ci, args);
  1000.     emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
  1001.     return;
  1002.   }

  1003.   /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
  1004.   emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
  1005.   emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));
  1006.   emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA);
  1007.   emit_ti(as, PPCI_LI, RID_TMP, id);  /* Lower 16 bit used. Sign-ext ok. */
  1008.   args[0] = ASMREF_L;     /* lua_State *L */
  1009.   args[1] = ASMREF_TMP1/* MSize size   */
  1010.   asm_gencall(as, ci, args);
  1011.   ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
  1012.                ra_releasetmp(as, ASMREF_TMP1));
  1013. }
  1014. #else
  1015. #define asm_cnew(as, ir)        ((void)0)
  1016. #endif

  1017. /* -- Write barriers ------------------------------------------------------ */

  1018. static void asm_tbar(ASMState *as, IRIns *ir)
  1019. {
  1020.   Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
  1021.   Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
  1022.   Reg link = RID_TMP;
  1023.   MCLabel l_end = emit_label(as);
  1024.   emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist));
  1025.   emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1026.   emit_setgl(as, tab, gc.grayagain);
  1027.   lua_assert(LJ_GC_BLACK == 0x04);
  1028.   emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28);  /* Clear black bit. */
  1029.   emit_getgl(as, link, gc.grayagain);
  1030.   emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
  1031.   emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK);
  1032.   emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked));
  1033. }

  1034. static void asm_obar(ASMState *as, IRIns *ir)
  1035. {
  1036.   const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
  1037.   IRRef args[2];
  1038.   MCLabel l_end;
  1039.   Reg obj, val, tmp;
  1040.   /* No need for other object barriers (yet). */
  1041.   lua_assert(IR(ir->op1)->o == IR_UREFC);
  1042.   ra_evictset(as, RSET_SCRATCH);
  1043.   l_end = emit_label(as);
  1044.   args[0] = ASMREF_TMP1/* global_State *g */
  1045.   args[1] = ir->op1;      /* TValue *tv      */
  1046.   asm_gencall(as, ci, args);
  1047.   emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
  1048.   obj = IR(ir->op1)->r;
  1049.   tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
  1050.   emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
  1051.   emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK);
  1052.   emit_condbranch(as, PPCI_BC, CC_EQ, l_end);
  1053.   emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES);
  1054.   val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
  1055.   emit_tai(as, PPCI_LBZ, tmp, obj,
  1056.            (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
  1057.   emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked));
  1058. }

  1059. /* -- Arithmetic and logic operations ------------------------------------- */

  1060. static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi)
  1061. {
  1062.   Reg dest = ra_dest(as, ir, RSET_FPR);
  1063.   Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1064.   right = (left >> 8); left &= 255;
  1065.   if (pi == PPCI_FMUL)
  1066.     emit_fac(as, pi, dest, left, right);
  1067.   else
  1068.     emit_fab(as, pi, dest, left, right);
  1069. }

  1070. static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi)
  1071. {
  1072.   Reg dest = ra_dest(as, ir, RSET_FPR);
  1073.   Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
  1074.   emit_fb(as, pi, dest, left);
  1075. }

  1076. static void asm_fpmath(ASMState *as, IRIns *ir)
  1077. {
  1078.   if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
  1079.     return;
  1080.   if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT))
  1081.     asm_fpunary(as, ir, PPCI_FSQRT);
  1082.   else
  1083.     asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
  1084. }

  1085. static void asm_add(ASMState *as, IRIns *ir)
  1086. {
  1087.   if (irt_isnum(ir->t)) {
  1088.     if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD))
  1089.       asm_fparith(as, ir, PPCI_FADD);
  1090.   } else {
  1091.     Reg dest = ra_dest(as, ir, RSET_GPR);
  1092.     Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1093.     PPCIns pi;
  1094.     if (irref_isk(ir->op2)) {
  1095.       int32_t k = IR(ir->op2)->i;
  1096.       if (checki16(k)) {
  1097.         pi = PPCI_ADDI;
  1098.         /* May fail due to spills/restores above, but simplifies the logic. */
  1099.         if (as->flagmcp == as->mcp) {
  1100.           as->flagmcp = NULL;
  1101.           as->mcp++;
  1102.           pi = PPCI_ADDICDOT;
  1103.         }
  1104.         emit_tai(as, pi, dest, left, k);
  1105.         return;
  1106.       } else if ((k & 0xffff) == 0) {
  1107.         emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16));
  1108.         return;
  1109.       } else if (!as->sectref) {
  1110.         emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16);
  1111.         emit_tai(as, PPCI_ADDI, dest, left, k);
  1112.         return;
  1113.       }
  1114.     }
  1115.     pi = PPCI_ADD;
  1116.     /* May fail due to spills/restores above, but simplifies the logic. */
  1117.     if (as->flagmcp == as->mcp) {
  1118.       as->flagmcp = NULL;
  1119.       as->mcp++;
  1120.       pi |= PPCF_DOT;
  1121.     }
  1122.     right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1123.     emit_tab(as, pi, dest, left, right);
  1124.   }
  1125. }

  1126. static void asm_sub(ASMState *as, IRIns *ir)
  1127. {
  1128.   if (irt_isnum(ir->t)) {
  1129.     if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB))
  1130.       asm_fparith(as, ir, PPCI_FSUB);
  1131.   } else {
  1132.     PPCIns pi = PPCI_SUBF;
  1133.     Reg dest = ra_dest(as, ir, RSET_GPR);
  1134.     Reg left, right;
  1135.     if (irref_isk(ir->op1)) {
  1136.       int32_t k = IR(ir->op1)->i;
  1137.       if (checki16(k)) {
  1138.         right = ra_alloc1(as, ir->op2, RSET_GPR);
  1139.         emit_tai(as, PPCI_SUBFIC, dest, right, k);
  1140.         return;
  1141.       }
  1142.     }
  1143.     /* May fail due to spills/restores above, but simplifies the logic. */
  1144.     if (as->flagmcp == as->mcp) {
  1145.       as->flagmcp = NULL;
  1146.       as->mcp++;
  1147.       pi |= PPCF_DOT;
  1148.     }
  1149.     left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1150.     right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1151.     emit_tab(as, pi, dest, right, left);  /* Subtract right _from_ left. */
  1152.   }
  1153. }

  1154. static void asm_mul(ASMState *as, IRIns *ir)
  1155. {
  1156.   if (irt_isnum(ir->t)) {
  1157.     asm_fparith(as, ir, PPCI_FMUL);
  1158.   } else {
  1159.     PPCIns pi = PPCI_MULLW;
  1160.     Reg dest = ra_dest(as, ir, RSET_GPR);
  1161.     Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1162.     if (irref_isk(ir->op2)) {
  1163.       int32_t k = IR(ir->op2)->i;
  1164.       if (checki16(k)) {
  1165.         emit_tai(as, PPCI_MULLI, dest, left, k);
  1166.         return;
  1167.       }
  1168.     }
  1169.     /* May fail due to spills/restores above, but simplifies the logic. */
  1170.     if (as->flagmcp == as->mcp) {
  1171.       as->flagmcp = NULL;
  1172.       as->mcp++;
  1173.       pi |= PPCF_DOT;
  1174.     }
  1175.     right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1176.     emit_tab(as, pi, dest, left, right);
  1177.   }
  1178. }

  1179. #define asm_div(as, ir)                asm_fparith(as, ir, PPCI_FDIV)
  1180. #define asm_mod(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_modi)
  1181. #define asm_pow(as, ir)                asm_callid(as, ir, IRCALL_lj_vm_powi)

  1182. static void asm_neg(ASMState *as, IRIns *ir)
  1183. {
  1184.   if (irt_isnum(ir->t)) {
  1185.     asm_fpunary(as, ir, PPCI_FNEG);
  1186.   } else {
  1187.     Reg dest, left;
  1188.     PPCIns pi = PPCI_NEG;
  1189.     if (as->flagmcp == as->mcp) {
  1190.       as->flagmcp = NULL;
  1191.       as->mcp++;
  1192.       pi |= PPCF_DOT;
  1193.     }
  1194.     dest = ra_dest(as, ir, RSET_GPR);
  1195.     left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1196.     emit_tab(as, pi, dest, left, 0);
  1197.   }
  1198. }

  1199. #define asm_abs(as, ir)                asm_fpunary(as, ir, PPCI_FABS)
  1200. #define asm_atan2(as, ir)        asm_callid(as, ir, IRCALL_atan2)
  1201. #define asm_ldexp(as, ir)        asm_callid(as, ir, IRCALL_ldexp)

  1202. static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi)
  1203. {
  1204.   Reg dest, left, right;
  1205.   if (as->flagmcp == as->mcp) {
  1206.     as->flagmcp = NULL;
  1207.     as->mcp++;
  1208.   }
  1209.   asm_guardcc(as, CC_SO);
  1210.   dest = ra_dest(as, ir, RSET_GPR);
  1211.   left = ra_alloc2(as, ir, RSET_GPR);
  1212.   right = (left >> 8); left &= 255;
  1213.   if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; }
  1214.   emit_tab(as, pi|PPCF_DOT, dest, left, right);
  1215. }

  1216. #define asm_addov(as, ir)        asm_arithov(as, ir, PPCI_ADDO)
  1217. #define asm_subov(as, ir)        asm_arithov(as, ir, PPCI_SUBFO)
  1218. #define asm_mulov(as, ir)        asm_arithov(as, ir, PPCI_MULLWO)

  1219. #if LJ_HASFFI
  1220. static void asm_add64(ASMState *as, IRIns *ir)
  1221. {
  1222.   Reg dest = ra_dest(as, ir, RSET_GPR);
  1223.   Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  1224.   PPCIns pi = PPCI_ADDE;
  1225.   if (irref_isk(ir->op2)) {
  1226.     int32_t k = IR(ir->op2)->i;
  1227.     if (k == 0)
  1228.       pi = PPCI_ADDZE;
  1229.     else if (k == -1)
  1230.       pi = PPCI_ADDME;
  1231.     else
  1232.       goto needright;
  1233.     right = 0;
  1234.   } else {
  1235.   needright:
  1236.     right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1237.   }
  1238.   emit_tab(as, pi, dest, left, right);
  1239.   ir--;
  1240.   dest = ra_dest(as, ir, RSET_GPR);
  1241.   left = ra_alloc1(as, ir->op1, RSET_GPR);
  1242.   if (irref_isk(ir->op2)) {
  1243.     int32_t k = IR(ir->op2)->i;
  1244.     if (checki16(k)) {
  1245.       emit_tai(as, PPCI_ADDIC, dest, left, k);
  1246.       return;
  1247.     }
  1248.   }
  1249.   right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1250.   emit_tab(as, PPCI_ADDC, dest, left, right);
  1251. }

  1252. static void asm_sub64(ASMState *as, IRIns *ir)
  1253. {
  1254.   Reg dest = ra_dest(as, ir, RSET_GPR);
  1255.   Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR);
  1256.   PPCIns pi = PPCI_SUBFE;
  1257.   if (irref_isk(ir->op1)) {
  1258.     int32_t k = IR(ir->op1)->i;
  1259.     if (k == 0)
  1260.       pi = PPCI_SUBFZE;
  1261.     else if (k == -1)
  1262.       pi = PPCI_SUBFME;
  1263.     else
  1264.       goto needleft;
  1265.     left = 0;
  1266.   } else {
  1267.   needleft:
  1268.     left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
  1269.   }
  1270.   emit_tab(as, pi, dest, right, left);  /* Subtract right _from_ left. */
  1271.   ir--;
  1272.   dest = ra_dest(as, ir, RSET_GPR);
  1273.   right = ra_alloc1(as, ir->op2, RSET_GPR);
  1274.   if (irref_isk(ir->op1)) {
  1275.     int32_t k = IR(ir->op1)->i;
  1276.     if (checki16(k)) {
  1277.       emit_tai(as, PPCI_SUBFIC, dest, right, k);
  1278.       return;
  1279.     }
  1280.   }
  1281.   left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
  1282.   emit_tab(as, PPCI_SUBFC, dest, right, left);
  1283. }

  1284. static void asm_neg64(ASMState *as, IRIns *ir)
  1285. {
  1286.   Reg dest = ra_dest(as, ir, RSET_GPR);
  1287.   Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1288.   emit_tab(as, PPCI_SUBFZE, dest, left, 0);
  1289.   ir--;
  1290.   dest = ra_dest(as, ir, RSET_GPR);
  1291.   left = ra_alloc1(as, ir->op1, RSET_GPR);
  1292.   emit_tai(as, PPCI_SUBFIC, dest, left, 0);
  1293. }
  1294. #endif

  1295. static void asm_bnot(ASMState *as, IRIns *ir)
  1296. {
  1297.   Reg dest, left, right;
  1298.   PPCIns pi = PPCI_NOR;
  1299.   if (as->flagmcp == as->mcp) {
  1300.     as->flagmcp = NULL;
  1301.     as->mcp++;
  1302.     pi |= PPCF_DOT;
  1303.   }
  1304.   dest = ra_dest(as, ir, RSET_GPR);
  1305.   if (mayfuse(as, ir->op1)) {
  1306.     IRIns *irl = IR(ir->op1);
  1307.     if (irl->o == IR_BAND)
  1308.       pi ^= (PPCI_NOR ^ PPCI_NAND);
  1309.     else if (irl->o == IR_BXOR)
  1310.       pi ^= (PPCI_NOR ^ PPCI_EQV);
  1311.     else if (irl->o != IR_BOR)
  1312.       goto nofuse;
  1313.     left = ra_hintalloc(as, irl->op1, dest, RSET_GPR);
  1314.     right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left));
  1315.   } else {
  1316. nofuse:
  1317.     left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1318.   }
  1319.   emit_asb(as, pi, dest, left, right);
  1320. }

  1321. static void asm_bswap(ASMState *as, IRIns *ir)
  1322. {
  1323.   Reg dest = ra_dest(as, ir, RSET_GPR);
  1324.   IRIns *irx;
  1325.   if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD &&
  1326.       ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) {
  1327.     /* Fuse BSWAP with XLOAD to lwbrx. */
  1328.     asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR);
  1329.   } else {
  1330.     Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1331.     Reg tmp = dest;
  1332.     if (tmp == left) {
  1333.       tmp = RID_TMP;
  1334.       emit_mr(as, dest, RID_TMP);
  1335.     }
  1336.     emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23);
  1337.     emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7);
  1338.     emit_rotlwi(as, tmp, left, 8);
  1339.   }
  1340. }

  1341. /* Fuse BAND with contiguous bitmask and a shift to rlwinm. */
  1342. static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref)
  1343. {
  1344.   IRIns *ir;
  1345.   Reg left;
  1346.   if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) &&
  1347.       irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) {
  1348.     int32_t sh = (IR(ir->op2)->i & 31);
  1349.     switch (ir->o) {
  1350.     case IR_BSHL:
  1351.       if ((mask & ((1u<<sh)-1))) goto nofuse;
  1352.       break;
  1353.     case IR_BSHR:
  1354.       if ((mask & ~((~0u)>>sh))) goto nofuse;
  1355.       sh = ((32-sh)&31);
  1356.       break;
  1357.     case IR_BROL:
  1358.       break;
  1359.     default:
  1360.       goto nofuse;
  1361.     }
  1362.     left = ra_alloc1(as, ir->op1, RSET_GPR);
  1363.     *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh);
  1364.     return;
  1365.   }
  1366. nofuse:
  1367.   left = ra_alloc1(as, ref, RSET_GPR);
  1368.   *--as->mcp = pi | PPCF_T(left);
  1369. }

  1370. static void asm_band(ASMState *as, IRIns *ir)
  1371. {
  1372.   Reg dest, left, right;
  1373.   IRRef lref = ir->op1;
  1374.   PPCIns dot = 0;
  1375.   IRRef op2;
  1376.   if (as->flagmcp == as->mcp) {
  1377.     as->flagmcp = NULL;
  1378.     as->mcp++;
  1379.     dot = PPCF_DOT;
  1380.   }
  1381.   dest = ra_dest(as, ir, RSET_GPR);
  1382.   if (irref_isk(ir->op2)) {
  1383.     int32_t k = IR(ir->op2)->i;
  1384.     if (k) {
  1385.       /* First check for a contiguous bitmask as used by rlwinm. */
  1386.       uint32_t s1 = lj_ffs((uint32_t)k);
  1387.       uint32_t k1 = ((uint32_t)k >> s1);
  1388.       if ((k1 & (k1+1)) == 0) {
  1389.         asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |
  1390.                           PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1),
  1391.                           k, lref);
  1392.         return;
  1393.       }
  1394.       if (~(uint32_t)k) {
  1395.         uint32_t s2 = lj_ffs(~(uint32_t)k);
  1396.         uint32_t k2 = (~(uint32_t)k >> s2);
  1397.         if ((k2 & (k2+1)) == 0) {
  1398.           asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |
  1399.                             PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)),
  1400.                             k, lref);
  1401.           return;
  1402.         }
  1403.       }
  1404.     }
  1405.     if (checku16(k)) {
  1406.       left = ra_alloc1(as, lref, RSET_GPR);
  1407.       emit_asi(as, PPCI_ANDIDOT, dest, left, k);
  1408.       return;
  1409.     } else if ((k & 0xffff) == 0) {
  1410.       left = ra_alloc1(as, lref, RSET_GPR);
  1411.       emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16));
  1412.       return;
  1413.     }
  1414.   }
  1415.   op2 = ir->op2;
  1416.   if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) {
  1417.     dot ^= (PPCI_AND ^ PPCI_ANDC);
  1418.     op2 = IR(op2)->op1;
  1419.   }
  1420.   left = ra_hintalloc(as, lref, dest, RSET_GPR);
  1421.   right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left));
  1422.   emit_asb(as, PPCI_AND ^ dot, dest, left, right);
  1423. }

  1424. static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
  1425. {
  1426.   Reg dest = ra_dest(as, ir, RSET_GPR);
  1427.   Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1428.   if (irref_isk(ir->op2)) {
  1429.     int32_t k = IR(ir->op2)->i;
  1430.     Reg tmp = left;
  1431.     if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) {
  1432.       if (!checku16(k)) {
  1433.         emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16));
  1434.         if ((k & 0xffff) == 0) return;
  1435.       }
  1436.       emit_asi(as, pik, dest, left, k);
  1437.       return;
  1438.     }
  1439.   }
  1440.   /* May fail due to spills/restores above, but simplifies the logic. */
  1441.   if (as->flagmcp == as->mcp) {
  1442.     as->flagmcp = NULL;
  1443.     as->mcp++;
  1444.     pi |= PPCF_DOT;
  1445.   }
  1446.   right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1447.   emit_asb(as, pi, dest, left, right);
  1448. }

  1449. #define asm_bor(as, ir)                asm_bitop(as, ir, PPCI_OR, PPCI_ORI)
  1450. #define asm_bxor(as, ir)        asm_bitop(as, ir, PPCI_XOR, PPCI_XORI)

  1451. static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
  1452. {
  1453.   Reg dest, left;
  1454.   Reg dot = 0;
  1455.   if (as->flagmcp == as->mcp) {
  1456.     as->flagmcp = NULL;
  1457.     as->mcp++;
  1458.     dot = PPCF_DOT;
  1459.   }
  1460.   dest = ra_dest(as, ir, RSET_GPR);
  1461.   left = ra_alloc1(as, ir->op1, RSET_GPR);
  1462.   if (irref_isk(ir->op2)) {  /* Constant shifts. */
  1463.     int32_t shift = (IR(ir->op2)->i & 31);
  1464.     if (pik == 0/* SLWI */
  1465.       emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift);
  1466.     else if (pik == 1/* SRWI */
  1467.       emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31);
  1468.     else
  1469.       emit_asb(as, pik|dot, dest, left, shift);
  1470.   } else {
  1471.     Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1472.     emit_asb(as, pi|dot, dest, left, right);
  1473.   }
  1474. }

  1475. #define asm_bshl(as, ir)        asm_bitshift(as, ir, PPCI_SLW, 0)
  1476. #define asm_bshr(as, ir)        asm_bitshift(as, ir, PPCI_SRW, 1)
  1477. #define asm_bsar(as, ir)        asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI)
  1478. #define asm_brol(as, ir) \
  1479.   asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), \
  1480.                        PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31))
  1481. #define asm_bror(as, ir)        lua_assert(0)

  1482. static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
  1483. {
  1484.   if (irt_isnum(ir->t)) {
  1485.     Reg dest = ra_dest(as, ir, RSET_FPR);
  1486.     Reg tmp = dest;
  1487.     Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1488.     right = (left >> 8); left &= 255;
  1489.     if (tmp == left || tmp == right)
  1490.       tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR,
  1491.                                         dest), left), right));
  1492.     emit_facb(as, PPCI_FSEL, dest, tmp,
  1493.               ismax ? left : right, ismax ? right : left);
  1494.     emit_fab(as, PPCI_FSUB, tmp, left, right);
  1495.   } else {
  1496.     Reg dest = ra_dest(as, ir, RSET_GPR);
  1497.     Reg tmp1 = RID_TMP, tmp2 = dest;
  1498.     Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  1499.     right = (left >> 8); left &= 255;
  1500.     if (tmp2 == left || tmp2 == right)
  1501.       tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR,
  1502.                                          dest), left), right));
  1503.     emit_tab(as, PPCI_ADD, dest, tmp2, right);
  1504.     emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1);
  1505.     emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1);
  1506.     emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1);
  1507.     emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000);
  1508.     emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000);
  1509.   }
  1510. }

  1511. #define asm_min(as, ir)                asm_min_max(as, ir, 0)
  1512. #define asm_max(as, ir)                asm_min_max(as, ir, 1)

  1513. /* -- Comparisons --------------------------------------------------------- */

  1514. #define CC_UNSIGNED        0x08        /* Unsigned integer comparison. */
  1515. #define CC_TWO                0x80        /* Check two flags for FP comparison. */

  1516. /* Map of comparisons to flags. ORDER IR. */
  1517. static const uint8_t asm_compmap[IR_ABC+1] = {
  1518.   /* op     int cc                 FP cc */
  1519.   /* LT  */ CC_GE               + (CC_GE<<4),
  1520.   /* GE  */ CC_LT               + (CC_LE<<4) + CC_TWO,
  1521.   /* LE  */ CC_GT               + (CC_GE<<4) + CC_TWO,
  1522.   /* GT  */ CC_LE               + (CC_LE<<4),
  1523.   /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO,
  1524.   /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4),
  1525.   /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4),
  1526.   /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO,
  1527.   /* EQ  */ CC_NE               + (CC_NE<<4),
  1528.   /* NE  */ CC_EQ               + (CC_EQ<<4),
  1529.   /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO  /* Same as UGT. */
  1530. };

  1531. static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc)
  1532. {
  1533.   Reg right, left = ra_alloc1(as, lref, RSET_GPR);
  1534.   if (irref_isk(rref)) {
  1535.     int32_t k = IR(rref)->i;
  1536.     if ((cc & CC_UNSIGNED) == 0) {  /* Signed comparison with constant. */
  1537.       if (checki16(k)) {
  1538.         emit_tai(as, PPCI_CMPWI, cr, left, k);
  1539.         /* Signed comparison with zero and referencing previous ins? */
  1540.         if (k == 0 && lref == as->curins-1)
  1541.           as->flagmcp = as->mcp;  /* Allow elimination of the compare. */
  1542.         return;
  1543.       } else if ((cc & 3) == (CC_EQ & 3)) {  /* Use CMPLWI for EQ or NE. */
  1544.         if (checku16(k)) {
  1545.           emit_tai(as, PPCI_CMPLWI, cr, left, k);
  1546.           return;
  1547.         } else if (!as->sectref && ra_noreg(IR(rref)->r)) {
  1548.           emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k);
  1549.           emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16));
  1550.           return;
  1551.         }
  1552.       }
  1553.     } else/* Unsigned comparison with constant. */
  1554.       if (checku16(k)) {
  1555.         emit_tai(as, PPCI_CMPLWI, cr, left, k);
  1556.         return;
  1557.       }
  1558.     }
  1559.   }
  1560.   right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left));
  1561.   emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right);
  1562. }

  1563. static void asm_comp(ASMState *as, IRIns *ir)
  1564. {
  1565.   PPCCC cc = asm_compmap[ir->o];
  1566.   if (irt_isnum(ir->t)) {
  1567.     Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1568.     right = (left >> 8); left &= 255;
  1569.     asm_guardcc(as, (cc >> 4));
  1570.     if ((cc & CC_TWO))
  1571.       emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3));
  1572.     emit_fab(as, PPCI_FCMPU, 0, left, right);
  1573.   } else {
  1574.     IRRef lref = ir->op1, rref = ir->op2;
  1575.     if (irref_isk(lref) && !irref_isk(rref)) {
  1576.       /* Swap constants to the right (only for ABC). */
  1577.       IRRef tmp = lref; lref = rref; rref = tmp;
  1578.       if ((cc & 2) == 0) cc ^= 1/* LT <-> GT, LE <-> GE */
  1579.     }
  1580.     asm_guardcc(as, cc);
  1581.     asm_intcomp_(as, lref, rref, 0, cc);
  1582.   }
  1583. }

  1584. #define asm_equal(as, ir)        asm_comp(as, ir)

  1585. #if LJ_HASFFI
  1586. /* 64 bit integer comparisons. */
  1587. static void asm_comp64(ASMState *as, IRIns *ir)
  1588. {
  1589.   PPCCC cc = asm_compmap[(ir-1)->o];
  1590.   if ((cc&3) == (CC_EQ&3)) {
  1591.     asm_guardcc(as, cc);
  1592.     emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR,
  1593.              (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3));
  1594.   } else {
  1595.     asm_guardcc(as, CC_EQ);
  1596.     emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1));
  1597.     emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC,
  1598.              (CC_EQ&3), (CC_EQ&3), 4+(cc&3));
  1599.   }
  1600.   /* Loword comparison sets cr1 and is unsigned, except for equality. */
  1601.   asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4,
  1602.                cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED));
  1603.   /* Hiword comparison sets cr0. */
  1604.   asm_intcomp_(as, ir->op1, ir->op2, 0, cc);
  1605.   as->flagmcp = NULL/* Doesn't work here. */
  1606. }
  1607. #endif

  1608. /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */

  1609. /* Hiword op of a split 64 bit op. Previous op must be the loword op. */
  1610. static void asm_hiop(ASMState *as, IRIns *ir)
  1611. {
  1612. #if LJ_HASFFI
  1613.   /* HIOP is marked as a store because it needs its own DCE logic. */
  1614.   int uselo = ra_used(ir-1), usehi = ra_used(ir);  /* Loword/hiword used? */
  1615.   if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
  1616.   if ((ir-1)->o == IR_CONV) {  /* Conversions to/from 64 bit. */
  1617.     as->curins--;  /* Always skip the CONV. */
  1618.     if (usehi || uselo)
  1619.       asm_conv64(as, ir);
  1620.     return;
  1621.   } else if ((ir-1)->o <= IR_NE) {  /* 64 bit integer comparisons. ORDER IR. */
  1622.     as->curins--;  /* Always skip the loword comparison. */
  1623.     asm_comp64(as, ir);
  1624.     return;
  1625.   } else if ((ir-1)->o == IR_XSTORE) {
  1626.     as->curins--;  /* Handle both stores here. */
  1627.     if ((ir-1)->r != RID_SINK) {
  1628.       asm_xstore_(as, ir, 0);
  1629.       asm_xstore_(as, ir-1, 4);
  1630.     }
  1631.     return;
  1632.   }
  1633.   if (!usehi) return/* Skip unused hiword op for all remaining ops. */
  1634.   switch ((ir-1)->o) {
  1635.   case IR_ADD: as->curins--; asm_add64(as, ir); break;
  1636.   case IR_SUB: as->curins--; asm_sub64(as, ir); break;
  1637.   case IR_NEG: as->curins--; asm_neg64(as, ir); break;
  1638.   case IR_CALLN:
  1639.   case IR_CALLXS:
  1640.     if (!uselo)
  1641.       ra_allocref(as, ir->op1, RID2RSET(RID_RETLO));  /* Mark lo op as used. */
  1642.     break;
  1643.   case IR_CNEWI:
  1644.     /* Nothing to do here. Handled by lo op itself. */
  1645.     break;
  1646.   default: lua_assert(0); break;
  1647.   }
  1648. #else
  1649.   UNUSED(as); UNUSED(ir); lua_assert(0);  /* Unused without FFI. */
  1650. #endif
  1651. }

  1652. /* -- Profiling ----------------------------------------------------------- */

  1653. static void asm_prof(ASMState *as, IRIns *ir)
  1654. {
  1655.   UNUSED(ir);
  1656.   asm_guardcc(as, CC_NE);
  1657.   emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, HOOK_PROFILE);
  1658.   emit_lsglptr(as, PPCI_LBZ, RID_TMP,
  1659.                (int32_t)offsetof(global_State, hookmask));
  1660. }

  1661. /* -- Stack handling ------------------------------------------------------ */

  1662. /* Check Lua stack size for overflow. Use exit handler as fallback. */
  1663. static void asm_stack_check(ASMState *as, BCReg topslot,
  1664.                             IRIns *irp, RegSet allow, ExitNo exitno)
  1665. {
  1666.   /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */
  1667.   Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
  1668.   rset_clear(allow, pbase);
  1669.   tmp = allow ? rset_pickbot(allow) :
  1670.                 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
  1671.   emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno));
  1672.   if (allow == RSET_EMPTY/* Restore temp. register. */
  1673.     emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW);
  1674.   else
  1675.     ra_modified(as, tmp);
  1676.   emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot));
  1677.   emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp);
  1678.   emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack));
  1679.   if (pbase == RID_TMP)
  1680.     emit_getgl(as, RID_TMP, jit_base);
  1681.   emit_getgl(as, tmp, cur_L);
  1682.   if (allow == RSET_EMPTY/* Spill temp. register. */
  1683.     emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW);
  1684. }

  1685. /* Restore Lua stack from on-trace state. */
  1686. static void asm_stack_restore(ASMState *as, SnapShot *snap)
  1687. {
  1688.   SnapEntry *map = &as->T->snapmap[snap->mapofs];
  1689.   SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];
  1690.   MSize n, nent = snap->nent;
  1691.   /* Store the value of all modified slots to the Lua stack. */
  1692.   for (n = 0; n < nent; n++) {
  1693.     SnapEntry sn = map[n];
  1694.     BCReg s = snap_slot(sn);
  1695.     int32_t ofs = 8*((int32_t)s-1);
  1696.     IRRef ref = snap_ref(sn);
  1697.     IRIns *ir = IR(ref);
  1698.     if ((sn & SNAP_NORESTORE))
  1699.       continue;
  1700.     if (irt_isnum(ir->t)) {
  1701.       Reg src = ra_alloc1(as, ref, RSET_FPR);
  1702.       emit_fai(as, PPCI_STFD, src, RID_BASE, ofs);
  1703.     } else {
  1704.       Reg type;
  1705.       RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
  1706.       lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
  1707.       if (!irt_ispri(ir->t)) {
  1708.         Reg src = ra_alloc1(as, ref, allow);
  1709.         rset_clear(allow, src);
  1710.         emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4);
  1711.       }
  1712.       if ((sn & (SNAP_CONT|SNAP_FRAME))) {
  1713.         if (s == 0) continue/* Do not overwrite link to previous frame. */
  1714.         type = ra_allock(as, (int32_t)(*flinks--), allow);
  1715.       } else {
  1716.         type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  1717.       }
  1718.       emit_tai(as, PPCI_STW, type, RID_BASE, ofs);
  1719.     }
  1720.     checkmclim(as);
  1721.   }
  1722.   lua_assert(map + nent == flinks);
  1723. }

  1724. /* -- GC handling --------------------------------------------------------- */

  1725. /* Check GC threshold and do one or more GC steps. */
  1726. static void asm_gc_check(ASMState *as)
  1727. {
  1728.   const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
  1729.   IRRef args[2];
  1730.   MCLabel l_end;
  1731.   Reg tmp;
  1732.   ra_evictset(as, RSET_SCRATCH);
  1733.   l_end = emit_label(as);
  1734.   /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
  1735.   asm_guardcc(as, CC_NE);  /* Assumes asm_snap_prep() already done. */
  1736.   emit_ai(as, PPCI_CMPWI, RID_RET, 0);
  1737.   args[0] = ASMREF_TMP1/* global_State *g */
  1738.   args[1] = ASMREF_TMP2/* MSize steps     */
  1739.   asm_gencall(as, ci, args);
  1740.   emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
  1741.   tmp = ra_releasetmp(as, ASMREF_TMP2);
  1742.   emit_loadi(as, tmp, as->gcsteps);
  1743.   /* Jump around GC step if GC total < GC threshold. */
  1744.   emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end);
  1745.   emit_ab(as, PPCI_CMPLW, RID_TMP, tmp);
  1746.   emit_getgl(as, tmp, gc.threshold);
  1747.   emit_getgl(as, RID_TMP, gc.total);
  1748.   as->gcsteps = 0;
  1749.   checkmclim(as);
  1750. }

  1751. /* -- Loop handling ------------------------------------------------------- */

  1752. /* Fixup the loop branch. */
  1753. static void asm_loop_fixup(ASMState *as)
  1754. {
  1755.   MCode *p = as->mctop;
  1756.   MCode *target = as->mcp;
  1757.   if (as->loopinv) {  /* Inverted loop branch? */
  1758.     /* asm_guardcc already inverted the cond branch and patched the final b. */
  1759.     p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2);
  1760.   } else {
  1761.     p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
  1762.   }
  1763. }

  1764. /* -- Head of trace ------------------------------------------------------- */

  1765. /* Coalesce BASE register for a root trace. */
  1766. static void asm_head_root_base(ASMState *as)
  1767. {
  1768.   IRIns *ir = IR(REF_BASE);
  1769.   Reg r = ir->r;
  1770.   if (ra_hasreg(r)) {
  1771.     ra_free(as, r);
  1772.     if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  1773.       ir->r = RID_INIT/* No inheritance for modified BASE register. */
  1774.     if (r != RID_BASE)
  1775.       emit_mr(as, r, RID_BASE);
  1776.   }
  1777. }

  1778. /* Coalesce BASE register for a side trace. */
  1779. static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)
  1780. {
  1781.   IRIns *ir = IR(REF_BASE);
  1782.   Reg r = ir->r;
  1783.   if (ra_hasreg(r)) {
  1784.     ra_free(as, r);
  1785.     if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  1786.       ir->r = RID_INIT/* No inheritance for modified BASE register. */
  1787.     if (irp->r == r) {
  1788.       rset_clear(allow, r);  /* Mark same BASE register as coalesced. */
  1789.     } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
  1790.       rset_clear(allow, irp->r);
  1791.       emit_mr(as, r, irp->r);  /* Move from coalesced parent reg. */
  1792.     } else {
  1793.       emit_getgl(as, r, jit_base);  /* Otherwise reload BASE. */
  1794.     }
  1795.   }
  1796.   return allow;
  1797. }

  1798. /* -- Tail of trace ------------------------------------------------------- */

  1799. /* Fixup the tail code. */
  1800. static void asm_tail_fixup(ASMState *as, TraceNo lnk)
  1801. {
  1802.   MCode *p = as->mctop;
  1803.   MCode *target;
  1804.   int32_t spadj = as->T->spadjust;
  1805.   if (spadj == 0) {
  1806.     *--p = PPCI_NOP;
  1807.     *--p = PPCI_NOP;
  1808.     as->mctop = p;
  1809.   } else {
  1810.     /* Patch stack adjustment. */
  1811.     lua_assert(checki16(CFRAME_SIZE+spadj));
  1812.     p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj);
  1813.     p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj;
  1814.   }
  1815.   /* Patch exit branch. */
  1816.   target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
  1817.   p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
  1818. }

  1819. /* Prepare tail of code. */
  1820. static void asm_tail_prep(ASMState *as)
  1821. {
  1822.   MCode *p = as->mctop - 1/* Leave room for exit branch. */
  1823.   if (as->loopref) {
  1824.     as->invmcp = as->mcp = p;
  1825.   } else {
  1826.     as->mcp = p-2/* Leave room for stack pointer adjustment. */
  1827.     as->invmcp = NULL;
  1828.   }
  1829. }

  1830. /* -- Trace setup --------------------------------------------------------- */

  1831. /* Ensure there are enough stack slots for call arguments. */
  1832. static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
  1833. {
  1834.   IRRef args[CCI_NARGS_MAX*2];
  1835.   uint32_t i, nargs = CCI_XNARGS(ci);
  1836.   int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
  1837.   asm_collectargs(as, ir, ci, args);
  1838.   for (i = 0; i < nargs; i++)
  1839.     if (args[i] && irt_isfp(IR(args[i])->t)) {
  1840.       if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1;
  1841.     } else {
  1842.       if (ngpr > 0) ngpr--; else nslots++;
  1843.     }
  1844.   if (nslots > as->evenspill)  /* Leave room for args in stack slots. */
  1845.     as->evenspill = nslots;
  1846.   return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
  1847. }

  1848. static void asm_setup_target(ASMState *as)
  1849. {
  1850.   asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
  1851. }

  1852. /* -- Trace patching ------------------------------------------------------ */

  1853. /* Patch exit jumps of existing machine code to a new target. */
  1854. void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
  1855. {
  1856.   MCode *p = T->mcode;
  1857.   MCode *pe = (MCode *)((char *)p + T->szmcode);
  1858.   MCode *px = exitstub_trace_addr(T, exitno);
  1859.   MCode *cstart = NULL;
  1860.   MCode *mcarea = lj_mcode_patch(J, p, 0);
  1861.   int clearso = 0;
  1862.   for (; p < pe; p++) {
  1863.     /* Look for exitstub branch, try to replace with branch to target. */
  1864.     uint32_t ins = *p;
  1865.     if ((ins & 0xfc000000u) == 0x40000000u &&
  1866.         ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) {
  1867.       ptrdiff_t delta = (char *)target - (char *)p;
  1868.       if (((ins >> 16) & 3) == (CC_SO&3)) {
  1869.         clearso = sizeof(MCode);
  1870.         delta -= sizeof(MCode);
  1871.       }
  1872.       /* Many, but not all short-range branches can be patched directly. */
  1873.       if (((delta + 0x8000) >> 16) == 0) {
  1874.         *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) |
  1875.              ((delta & 0x8000) * (PPCF_Y/0x8000));
  1876.         if (!cstart) cstart = p;
  1877.       }
  1878.     } else if ((ins & 0xfc000000u) == PPCI_B &&
  1879.                ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) {
  1880.       ptrdiff_t delta = (char *)target - (char *)p;
  1881.       lua_assert(((delta + 0x02000000) >> 26) == 0);
  1882.       *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
  1883.       if (!cstart) cstart = p;
  1884.     }
  1885.   }
  1886.   {  /* Always patch long-range branch in exit stub itself. */
  1887.     ptrdiff_t delta = (char *)target - (char *)px - clearso;
  1888.     lua_assert(((delta + 0x02000000) >> 26) == 0);
  1889.     *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
  1890.   }
  1891.   if (!cstart) cstart = px;
  1892.   lj_mcode_sync(cstart, px+1);
  1893.   if (clearso) {  /* Extend the current trace. Ugly workaround. */
  1894.     MCode *pp = J->cur.mcode;
  1895.     J->cur.szmcode += sizeof(MCode);
  1896.     *--pp = PPCI_MCRXR;  /* Clear SO flag. */
  1897.     J->cur.mcode = pp;
  1898.     lj_mcode_sync(pp, pp+1);
  1899.   }
  1900.   lj_mcode_patch(J, mcarea, 1);
  1901. }