Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_asm_ppc.h
blob1cac6fa98820a69d2113d10838e67bc748691949
1 /*
2 ** PPC IR assembler (SSA IR -> machine code).
3 ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4 */
6 /* -- Register allocator extensions --------------------------------------- */
8 /* Allocate a register with a hint. */
9 static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
11 Reg r = IR(ref)->r;
12 if (ra_noreg(r)) {
13 if (!ra_hashint(r) && !iscrossref(as, ref))
14 ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */
15 r = ra_allocref(as, ref, allow);
17 ra_noweak(as, r);
18 return r;
21 /* Allocate two source registers for three-operand instructions. */
22 static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
24 IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
25 Reg left = irl->r, right = irr->r;
26 if (ra_hasreg(left)) {
27 ra_noweak(as, left);
28 if (ra_noreg(right))
29 right = ra_allocref(as, ir->op2, rset_exclude(allow, left));
30 else
31 ra_noweak(as, right);
32 } else if (ra_hasreg(right)) {
33 ra_noweak(as, right);
34 left = ra_allocref(as, ir->op1, rset_exclude(allow, right));
35 } else if (ra_hashint(right)) {
36 right = ra_allocref(as, ir->op2, allow);
37 left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));
38 } else {
39 left = ra_allocref(as, ir->op1, allow);
40 right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));
42 return left | (right << 8);
45 /* -- Guard handling ------------------------------------------------------ */
47 /* Setup exit stubs after the end of each trace. */
48 static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
50 ExitNo i;
51 MCode *mxp = as->mctop;
52 /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */
53 for (i = nexits-1; (int32_t)i >= 0; i--)
54 *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2);
55 *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno; /* Read by exit handler. */
56 mxp--;
57 *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2);
58 *--mxp = PPCI_MFLR|PPCF_T(RID_TMP);
59 as->mctop = mxp;
62 static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)
64 /* Keep this in-sync with exitstub_trace_addr(). */
65 return as->mctop + exitno + 3;
68 /* Emit conditional branch to exit for guard. */
69 static void asm_guardcc(ASMState *as, PPCCC cc)
71 MCode *target = asm_exitstub_addr(as, as->snapno);
72 MCode *p = as->mcp;
73 if (LJ_UNLIKELY(p == as->invmcp)) {
74 as->loopinv = 1;
75 *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2);
76 emit_condbranch(as, PPCI_BC, cc^4, p);
77 return;
79 emit_condbranch(as, PPCI_BC, cc, target);
82 /* -- Operand fusion ------------------------------------------------------ */
84 /* Limit linear search to this distance. Avoids O(n^2) behavior. */
85 #define CONFLICT_SEARCH_LIM 31
87 /* Check if there's no conflicting instruction between curins and ref. */
88 static int noconflict(ASMState *as, IRRef ref, IROp conflict)
90 IRIns *ir = as->ir;
91 IRRef i = as->curins;
92 if (i > ref + CONFLICT_SEARCH_LIM)
93 return 0; /* Give up, ref is too far away. */
94 while (--i > ref)
95 if (ir[i].o == conflict)
96 return 0; /* Conflict found. */
97 return 1; /* Ok, no conflict. */
100 /* Fuse the array base of colocated arrays. */
101 static int32_t asm_fuseabase(ASMState *as, IRRef ref)
103 IRIns *ir = IR(ref);
104 if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
105 !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
106 return (int32_t)sizeof(GCtab);
107 return 0;
110 /* Indicates load/store indexed is ok. */
111 #define AHUREF_LSX ((int32_t)0x80000000)
113 /* Fuse array/hash/upvalue reference into register+offset operand. */
114 static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
116 IRIns *ir = IR(ref);
117 if (ra_noreg(ir->r)) {
118 if (ir->o == IR_AREF) {
119 if (mayfuse(as, ref)) {
120 if (irref_isk(ir->op2)) {
121 IRRef tab = IR(ir->op1)->op1;
122 int32_t ofs = asm_fuseabase(as, tab);
123 IRRef refa = ofs ? tab : ir->op1;
124 ofs += 8*IR(ir->op2)->i;
125 if (checki16(ofs)) {
126 *ofsp = ofs;
127 return ra_alloc1(as, refa, allow);
130 if (*ofsp == AHUREF_LSX) {
131 Reg base = ra_alloc1(as, ir->op1, allow);
132 Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
133 return base | (idx << 8);
136 } else if (ir->o == IR_HREFK) {
137 if (mayfuse(as, ref)) {
138 int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
139 if (checki16(ofs)) {
140 *ofsp = ofs;
141 return ra_alloc1(as, ir->op1, allow);
144 } else if (ir->o == IR_UREFC) {
145 if (irref_isk(ir->op1)) {
146 GCfunc *fn = ir_kfunc(IR(ir->op1));
147 int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);
148 int32_t jgl = (intptr_t)J2G(as->J);
149 if ((uint32_t)(ofs-jgl) < 65536) {
150 *ofsp = ofs-jgl-32768;
151 return RID_JGL;
152 } else {
153 *ofsp = (int16_t)ofs;
154 return ra_allock(as, ofs-(int16_t)ofs, allow);
159 *ofsp = 0;
160 return ra_alloc1(as, ref, allow);
163 /* Fuse XLOAD/XSTORE reference into load/store operand. */
164 static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref,
165 RegSet allow, int32_t ofs)
167 IRIns *ir = IR(ref);
168 Reg base;
169 if (ra_noreg(ir->r) && canfuse(as, ir)) {
170 if (ir->o == IR_ADD) {
171 int32_t ofs2;
172 if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) {
173 ofs = ofs2;
174 ref = ir->op1;
175 } else if (ofs == 0) {
176 Reg right, left = ra_alloc2(as, ir, allow);
177 right = (left >> 8); left &= 255;
178 emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
179 return;
181 } else if (ir->o == IR_STRREF) {
182 lua_assert(ofs == 0);
183 ofs = (int32_t)sizeof(GCstr);
184 if (irref_isk(ir->op2)) {
185 ofs += IR(ir->op2)->i;
186 ref = ir->op1;
187 } else if (irref_isk(ir->op1)) {
188 ofs += IR(ir->op1)->i;
189 ref = ir->op2;
190 } else {
191 /* NYI: Fuse ADD with constant. */
192 Reg tmp, right, left = ra_alloc2(as, ir, allow);
193 right = (left >> 8); left &= 255;
194 tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right));
195 emit_fai(as, pi, rt, tmp, ofs);
196 emit_tab(as, PPCI_ADD, tmp, left, right);
197 return;
199 if (!checki16(ofs)) {
200 Reg left = ra_alloc1(as, ref, allow);
201 Reg right = ra_allock(as, ofs, rset_exclude(allow, left));
202 emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
203 return;
207 base = ra_alloc1(as, ref, allow);
208 emit_fai(as, pi, rt, base, ofs);
211 /* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */
212 static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref,
213 RegSet allow)
215 IRIns *ira = IR(ref);
216 Reg right, left;
217 if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) {
218 left = ra_alloc2(as, ira, allow);
219 right = (left >> 8); left &= 255;
220 } else {
221 right = ra_alloc1(as, ref, allow);
222 left = RID_R0;
224 emit_tab(as, pi, rt, left, right);
227 /* Fuse to multiply-add/sub instruction. */
228 static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir)
230 IRRef lref = ir->op1, rref = ir->op2;
231 IRIns *irm;
232 if (lref != rref &&
233 ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
234 ra_noreg(irm->r)) ||
235 (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
236 (rref = lref, pi = pir, ra_noreg(irm->r))))) {
237 Reg dest = ra_dest(as, ir, RSET_FPR);
238 Reg add = ra_alloc1(as, rref, RSET_FPR);
239 Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add));
240 right = (left >> 8); left &= 255;
241 emit_facb(as, pi, dest, left, right, add);
242 return 1;
244 return 0;
247 /* -- Calls --------------------------------------------------------------- */
249 /* Generate a call to a C function. */
250 static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
252 uint32_t n, nargs = CCI_XNARGS(ci);
253 int32_t ofs = 8;
254 Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR;
255 if ((void *)ci->func)
256 emit_call(as, (void *)ci->func);
257 for (n = 0; n < nargs; n++) { /* Setup args. */
258 IRRef ref = args[n];
259 if (ref) {
260 IRIns *ir = IR(ref);
261 if (irt_isfp(ir->t)) {
262 if (fpr <= REGARG_LASTFPR) {
263 lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */
264 ra_leftov(as, fpr, ref);
265 fpr++;
266 } else {
267 Reg r = ra_alloc1(as, ref, RSET_FPR);
268 if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
269 emit_spstore(as, ir, r, ofs);
270 ofs += irt_isnum(ir->t) ? 8 : 4;
272 } else {
273 if (gpr <= REGARG_LASTGPR) {
274 lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */
275 ra_leftov(as, gpr, ref);
276 gpr++;
277 } else {
278 Reg r = ra_alloc1(as, ref, RSET_GPR);
279 emit_spstore(as, ir, r, ofs);
280 ofs += 4;
283 } else {
284 if (gpr <= REGARG_LASTGPR)
285 gpr++;
286 else
287 ofs += 4;
289 checkmclim(as);
291 if ((ci->flags & CCI_VARARG)) /* Vararg calls need to know about FPR use. */
292 emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6);
295 /* Setup result reg/sp for call. Evict scratch regs. */
296 static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
298 RegSet drop = RSET_SCRATCH;
299 int hiop = ((ir+1)->o == IR_HIOP);
300 if ((ci->flags & CCI_NOFPRCLOBBER))
301 drop &= ~RSET_FPR;
302 if (ra_hasreg(ir->r))
303 rset_clear(drop, ir->r); /* Dest reg handled below. */
304 if (hiop && ra_hasreg((ir+1)->r))
305 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
306 ra_evictset(as, drop); /* Evictions must be performed first. */
307 if (ra_used(ir)) {
308 lua_assert(!irt_ispri(ir->t));
309 if (irt_isfp(ir->t)) {
310 if ((ci->flags & CCI_CASTU64)) {
311 /* Use spill slot or temp slots. */
312 int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
313 Reg dest = ir->r;
314 if (ra_hasreg(dest)) {
315 ra_free(as, dest);
316 ra_modified(as, dest);
317 emit_fai(as, PPCI_LFD, dest, RID_SP, ofs);
319 emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs);
320 emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4);
321 } else {
322 ra_destreg(as, ir, RID_FPRET);
324 } else if (hiop) {
325 ra_destpair(as, ir);
326 } else {
327 ra_destreg(as, ir, RID_RET);
332 static void asm_callx(ASMState *as, IRIns *ir)
334 IRRef args[CCI_NARGS_MAX*2];
335 CCallInfo ci;
336 IRRef func;
337 IRIns *irf;
338 ci.flags = asm_callx_flags(as, ir);
339 asm_collectargs(as, ir, &ci, args);
340 asm_setupresult(as, ir, &ci);
341 func = ir->op2; irf = IR(func);
342 if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
343 if (irref_isk(func)) { /* Call to constant address. */
344 ci.func = (ASMFunction)(void *)(irf->i);
345 } else { /* Need a non-argument register for indirect calls. */
346 RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1);
347 Reg freg = ra_alloc1(as, func, allow);
348 *--as->mcp = PPCI_BCTRL;
349 *--as->mcp = PPCI_MTCTR | PPCF_T(freg);
350 ci.func = (ASMFunction)(void *)0;
352 asm_gencall(as, &ci, args);
355 /* -- Returns ------------------------------------------------------------- */
357 /* Return to lower frame. Guard that it goes to the right spot. */
358 static void asm_retf(ASMState *as, IRIns *ir)
360 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
361 void *pc = ir_kptr(IR(ir->op2));
362 int32_t delta = 1+bc_a(*((const BCIns *)pc - 1));
363 as->topslot -= (BCReg)delta;
364 if ((int32_t)as->topslot < 0) as->topslot = 0;
365 emit_setgl(as, base, jit_base);
366 emit_addptr(as, base, -8*delta);
367 asm_guardcc(as, CC_NE);
368 emit_ab(as, PPCI_CMPW, RID_TMP,
369 ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));
370 emit_tai(as, PPCI_LWZ, RID_TMP, base, -8);
373 /* -- Type conversions ---------------------------------------------------- */
375 static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
377 RegSet allow = RSET_FPR;
378 Reg tmp = ra_scratch(as, rset_clear(allow, left));
379 Reg fbias = ra_scratch(as, rset_clear(allow, tmp));
380 Reg dest = ra_dest(as, ir, RSET_GPR);
381 Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest));
382 asm_guardcc(as, CC_NE);
383 emit_fab(as, PPCI_FCMPU, 0, tmp, left);
384 emit_fab(as, PPCI_FSUB, tmp, tmp, fbias);
385 emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP);
386 emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO);
387 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
388 emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000);
389 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
390 emit_lsptr(as, PPCI_LFS, (fbias & 31),
391 (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
392 RSET_GPR);
393 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
394 emit_fb(as, PPCI_FCTIWZ, tmp, left);
397 static void asm_tobit(ASMState *as, IRIns *ir)
399 RegSet allow = RSET_FPR;
400 Reg dest = ra_dest(as, ir, RSET_GPR);
401 Reg left = ra_alloc1(as, ir->op1, allow);
402 Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
403 Reg tmp = ra_scratch(as, rset_clear(allow, right));
404 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
405 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
406 emit_fab(as, PPCI_FADD, tmp, left, right);
409 static void asm_conv(ASMState *as, IRIns *ir)
411 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
412 int stfp = (st == IRT_NUM || st == IRT_FLOAT);
413 IRRef lref = ir->op1;
414 lua_assert(irt_type(ir->t) != st);
415 lua_assert(!(irt_isint64(ir->t) ||
416 (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */
417 if (irt_isfp(ir->t)) {
418 Reg dest = ra_dest(as, ir, RSET_FPR);
419 if (stfp) { /* FP to FP conversion. */
420 if (st == IRT_NUM) /* double -> float conversion. */
421 emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR));
422 else /* float -> double conversion is a no-op on PPC. */
423 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
424 } else { /* Integer to FP conversion. */
425 /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */
426 /* IRT_U32: Bias with 2^52, subtract 2^52. */
427 RegSet allow = RSET_GPR;
428 Reg left = ra_alloc1(as, lref, allow);
429 Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left));
430 Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
431 const float *kbias;
432 if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest);
433 emit_fab(as, PPCI_FSUB, dest, dest, fbias);
434 emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
435 kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000));
436 if (st == IRT_U32) kbias++;
437 emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias,
438 rset_clear(allow, hibias));
439 emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP,
440 RID_SP, SPOFS_TMPLO);
441 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
442 if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000);
444 } else if (stfp) { /* FP to integer conversion. */
445 if (irt_isguard(ir->t)) {
446 /* Checked conversions are only supported from number to int. */
447 lua_assert(irt_isint(ir->t) && st == IRT_NUM);
448 asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
449 } else {
450 Reg dest = ra_dest(as, ir, RSET_GPR);
451 Reg left = ra_alloc1(as, lref, RSET_FPR);
452 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
453 if (irt_isu32(ir->t)) {
454 /* Convert both x and x-2^31 to int and merge results. */
455 Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest));
456 emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */
457 emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP);
458 emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP);
459 emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */
460 emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */
461 emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */
462 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
463 emit_tai(as, PPCI_LWZ, dest,
464 RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */
465 emit_fb(as, PPCI_FCTIWZ, tmp, left);
466 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
467 emit_fb(as, PPCI_FCTIWZ, tmp, tmp);
468 emit_fab(as, PPCI_FSUB, tmp, left, tmp);
469 emit_lsptr(as, PPCI_LFS, (tmp & 31),
470 (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)),
471 RSET_GPR);
472 } else {
473 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
474 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
475 emit_fb(as, PPCI_FCTIWZ, tmp, left);
478 } else {
479 Reg dest = ra_dest(as, ir, RSET_GPR);
480 if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
481 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
482 lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));
483 if ((ir->op2 & IRCONV_SEXT))
484 emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left);
485 else
486 emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31);
487 } else { /* 32/64 bit integer conversions. */
488 /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
489 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
494 static void asm_strto(ASMState *as, IRIns *ir)
496 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
497 IRRef args[2];
498 int32_t ofs;
499 RegSet drop = RSET_SCRATCH;
500 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
501 ra_evictset(as, drop);
502 asm_guardcc(as, CC_EQ);
503 emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */
504 args[0] = ir->op1; /* GCstr *str */
505 args[1] = ASMREF_TMP1; /* TValue *n */
506 asm_gencall(as, ci, args);
507 /* Store the result to the spill slot or temp slots. */
508 ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
509 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs);
512 /* -- Memory references --------------------------------------------------- */
514 /* Get pointer to TValue. */
515 static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)
517 IRIns *ir = IR(ref);
518 if (irt_isnum(ir->t)) {
519 if (irref_isk(ref)) /* Use the number constant itself as a TValue. */
520 ra_allockreg(as, i32ptr(ir_knum(ir)), dest);
521 else /* Otherwise force a spill and use the spill slot. */
522 emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir));
523 } else {
524 /* Otherwise use g->tmptv to hold the TValue. */
525 RegSet allow = rset_exclude(RSET_GPR, dest);
526 Reg type;
527 emit_tai(as, PPCI_ADDI, dest, RID_JGL, offsetof(global_State, tmptv)-32768);
528 if (!irt_ispri(ir->t)) {
529 Reg src = ra_alloc1(as, ref, allow);
530 emit_setgl(as, src, tmptv.gcr);
532 type = ra_allock(as, irt_toitype(ir->t), allow);
533 emit_setgl(as, type, tmptv.it);
537 static void asm_aref(ASMState *as, IRIns *ir)
539 Reg dest = ra_dest(as, ir, RSET_GPR);
540 Reg idx, base;
541 if (irref_isk(ir->op2)) {
542 IRRef tab = IR(ir->op1)->op1;
543 int32_t ofs = asm_fuseabase(as, tab);
544 IRRef refa = ofs ? tab : ir->op1;
545 ofs += 8*IR(ir->op2)->i;
546 if (checki16(ofs)) {
547 base = ra_alloc1(as, refa, RSET_GPR);
548 emit_tai(as, PPCI_ADDI, dest, base, ofs);
549 return;
552 base = ra_alloc1(as, ir->op1, RSET_GPR);
553 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
554 emit_tab(as, PPCI_ADD, dest, RID_TMP, base);
555 emit_slwi(as, RID_TMP, idx, 3);
558 /* Inlined hash lookup. Specialized for key type and for const keys.
559 ** The equivalent C code is:
560 ** Node *n = hashkey(t, key);
561 ** do {
562 ** if (lj_obj_equal(&n->key, key)) return &n->val;
563 ** } while ((n = nextnode(n)));
564 ** return niltv(L);
566 static void asm_href(ASMState *as, IRIns *ir, IROp merge)
568 RegSet allow = RSET_GPR;
569 int destused = ra_used(ir);
570 Reg dest = ra_dest(as, ir, allow);
571 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
572 Reg key = RID_NONE, tmp1 = RID_TMP, tmp2;
573 Reg tisnum = RID_NONE, tmpnum = RID_NONE;
574 IRRef refkey = ir->op2;
575 IRIns *irkey = IR(refkey);
576 IRType1 kt = irkey->t;
577 uint32_t khash;
578 MCLabel l_end, l_loop, l_next;
580 rset_clear(allow, tab);
581 if (irt_isnum(kt)) {
582 key = ra_alloc1(as, refkey, RSET_FPR);
583 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
584 tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
585 rset_clear(allow, tisnum);
586 } else if (!irt_ispri(kt)) {
587 key = ra_alloc1(as, refkey, allow);
588 rset_clear(allow, key);
590 tmp2 = ra_scratch(as, allow);
591 rset_clear(allow, tmp2);
593 /* Key not found in chain: jump to exit (if merged) or load niltv. */
594 l_end = emit_label(as);
595 as->invmcp = NULL;
596 if (merge == IR_NE)
597 asm_guardcc(as, CC_EQ);
598 else if (destused)
599 emit_loada(as, dest, niltvg(J2G(as->J)));
601 /* Follow hash chain until the end. */
602 l_loop = --as->mcp;
603 emit_ai(as, PPCI_CMPWI, dest, 0);
604 emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next));
605 l_next = emit_label(as);
607 /* Type and value comparison. */
608 if (merge == IR_EQ)
609 asm_guardcc(as, CC_EQ);
610 else
611 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
612 if (irt_isnum(kt)) {
613 emit_fab(as, PPCI_FCMPU, 0, tmpnum, key);
614 emit_condbranch(as, PPCI_BC, CC_GE, l_next);
615 emit_ab(as, PPCI_CMPLW, tmp1, tisnum);
616 emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n));
617 } else {
618 if (!irt_ispri(kt)) {
619 emit_ab(as, PPCI_CMPW, tmp2, key);
620 emit_condbranch(as, PPCI_BC, CC_NE, l_next);
622 emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t));
623 if (!irt_ispri(kt))
624 emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr));
626 emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it));
627 *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) |
628 (((char *)as->mcp-(char *)l_loop) & 0xffffu);
630 /* Load main position relative to tab->node into dest. */
631 khash = irref_isk(refkey) ? ir_khash(irkey) : 1;
632 if (khash == 0) {
633 emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
634 } else {
635 Reg tmphash = tmp1;
636 if (irref_isk(refkey))
637 tmphash = ra_allock(as, khash, allow);
638 emit_tab(as, PPCI_ADD, dest, dest, tmp1);
639 emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node));
640 emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash);
641 emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
642 emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
643 if (irref_isk(refkey)) {
644 /* Nothing to do. */
645 } else if (irt_isstr(kt)) {
646 emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash));
647 } else { /* Must match with hash*() in lj_tab.c. */
648 emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1);
649 emit_rotlwi(as, tmp2, tmp2, HASH_ROT3);
650 emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2);
651 emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31);
652 emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2);
653 if (irt_isnum(kt)) {
654 int32_t ofs = ra_spill(as, irkey);
655 emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1);
656 emit_rotlwi(as, dest, tmp1, HASH_ROT1);
657 emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1);
658 emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4);
659 emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs);
660 } else {
661 emit_asb(as, PPCI_XOR, tmp2, key, tmp1);
662 emit_rotlwi(as, dest, tmp1, HASH_ROT1);
663 emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS);
664 emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16);
670 static void asm_hrefk(ASMState *as, IRIns *ir)
672 IRIns *kslot = IR(ir->op2);
673 IRIns *irkey = IR(kslot->op1);
674 int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
675 int32_t kofs = ofs + (int32_t)offsetof(Node, key);
676 Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
677 Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
678 Reg key = RID_NONE, type = RID_TMP, idx = node;
679 RegSet allow = rset_exclude(RSET_GPR, node);
680 lua_assert(ofs % sizeof(Node) == 0);
681 if (ofs > 32736) {
682 idx = dest;
683 rset_clear(allow, dest);
684 kofs = (int32_t)offsetof(Node, key);
685 } else if (ra_hasreg(dest)) {
686 emit_tai(as, PPCI_ADDI, dest, node, ofs);
688 asm_guardcc(as, CC_NE);
689 if (!irt_ispri(irkey->t)) {
690 key = ra_scratch(as, allow);
691 rset_clear(allow, key);
693 rset_clear(allow, type);
694 if (irt_isnum(irkey->t)) {
695 emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo);
696 asm_guardcc(as, CC_NE);
697 emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi);
698 } else {
699 if (ra_hasreg(key)) {
700 emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */
701 asm_guardcc(as, CC_NE);
703 emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t));
705 if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4);
706 emit_tai(as, PPCI_LWZ, type, idx, kofs);
707 if (ofs > 32736) {
708 emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16);
709 emit_tai(as, PPCI_ADDI, dest, node, ofs);
713 static void asm_uref(ASMState *as, IRIns *ir)
715 /* NYI: Check that UREFO is still open and not aliasing a slot. */
716 Reg dest = ra_dest(as, ir, RSET_GPR);
717 if (irref_isk(ir->op1)) {
718 GCfunc *fn = ir_kfunc(IR(ir->op1));
719 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
720 emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR);
721 } else {
722 Reg uv = ra_scratch(as, RSET_GPR);
723 Reg func = ra_alloc1(as, ir->op1, RSET_GPR);
724 if (ir->o == IR_UREFC) {
725 asm_guardcc(as, CC_NE);
726 emit_ai(as, PPCI_CMPWI, RID_TMP, 1);
727 emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv));
728 emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));
729 } else {
730 emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v));
732 emit_tai(as, PPCI_LWZ, uv, func,
733 (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));
737 static void asm_fref(ASMState *as, IRIns *ir)
739 UNUSED(as); UNUSED(ir);
740 lua_assert(!ra_used(ir));
743 static void asm_strref(ASMState *as, IRIns *ir)
745 Reg dest = ra_dest(as, ir, RSET_GPR);
746 IRRef ref = ir->op2, refk = ir->op1;
747 int32_t ofs = (int32_t)sizeof(GCstr);
748 Reg r;
749 if (irref_isk(ref)) {
750 IRRef tmp = refk; refk = ref; ref = tmp;
751 } else if (!irref_isk(refk)) {
752 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
753 IRIns *irr = IR(ir->op2);
754 if (ra_hasreg(irr->r)) {
755 ra_noweak(as, irr->r);
756 right = irr->r;
757 } else if (mayfuse(as, irr->op2) &&
758 irr->o == IR_ADD && irref_isk(irr->op2) &&
759 checki16(ofs + IR(irr->op2)->i)) {
760 ofs += IR(irr->op2)->i;
761 right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
762 } else {
763 right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
765 emit_tai(as, PPCI_ADDI, dest, dest, ofs);
766 emit_tab(as, PPCI_ADD, dest, left, right);
767 return;
769 r = ra_alloc1(as, ref, RSET_GPR);
770 ofs += IR(refk)->i;
771 if (checki16(ofs))
772 emit_tai(as, PPCI_ADDI, dest, r, ofs);
773 else
774 emit_tab(as, PPCI_ADD, dest, r,
775 ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
778 /* -- Loads and stores ---------------------------------------------------- */
780 static PPCIns asm_fxloadins(IRIns *ir)
782 switch (irt_type(ir->t)) {
783 case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */
784 case IRT_U8: return PPCI_LBZ;
785 case IRT_I16: return PPCI_LHA;
786 case IRT_U16: return PPCI_LHZ;
787 case IRT_NUM: return PPCI_LFD;
788 case IRT_FLOAT: return PPCI_LFS;
789 default: return PPCI_LWZ;
793 static PPCIns asm_fxstoreins(IRIns *ir)
795 switch (irt_type(ir->t)) {
796 case IRT_I8: case IRT_U8: return PPCI_STB;
797 case IRT_I16: case IRT_U16: return PPCI_STH;
798 case IRT_NUM: return PPCI_STFD;
799 case IRT_FLOAT: return PPCI_STFS;
800 default: return PPCI_STW;
804 static void asm_fload(ASMState *as, IRIns *ir)
806 Reg dest = ra_dest(as, ir, RSET_GPR);
807 Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);
808 PPCIns pi = asm_fxloadins(ir);
809 int32_t ofs;
810 if (ir->op2 == IRFL_TAB_ARRAY) {
811 ofs = asm_fuseabase(as, ir->op1);
812 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
813 emit_tai(as, PPCI_ADDI, dest, idx, ofs);
814 return;
817 ofs = field_ofs[ir->op2];
818 lua_assert(!irt_isi8(ir->t));
819 emit_tai(as, pi, dest, idx, ofs);
822 static void asm_fstore(ASMState *as, IRIns *ir)
824 if (ir->r != RID_SINK) {
825 Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
826 IRIns *irf = IR(ir->op1);
827 Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
828 int32_t ofs = field_ofs[irf->op2];
829 PPCIns pi = asm_fxstoreins(ir);
830 emit_tai(as, pi, src, idx, ofs);
834 static void asm_xload(ASMState *as, IRIns *ir)
836 Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
837 lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));
838 if (irt_isi8(ir->t))
839 emit_as(as, PPCI_EXTSB, dest, dest);
840 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);
843 static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
845 IRIns *irb;
846 if (ir->r == RID_SINK)
847 return;
848 if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP &&
849 ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) {
850 /* Fuse BSWAP with XSTORE to stwbrx. */
851 Reg src = ra_alloc1(as, irb->op1, RSET_GPR);
852 asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src));
853 } else {
854 Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
855 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
856 rset_exclude(RSET_GPR, src), ofs);
860 #define asm_xstore(as, ir) asm_xstore_(as, ir, 0)
862 static void asm_ahuvload(ASMState *as, IRIns *ir)
864 IRType1 t = ir->t;
865 Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx;
866 RegSet allow = RSET_GPR;
867 int32_t ofs = AHUREF_LSX;
868 if (ra_used(ir)) {
869 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
870 if (!irt_isnum(t)) ofs = 0;
871 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
872 rset_clear(allow, dest);
874 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
875 if (irt_isnum(t)) {
876 Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx));
877 asm_guardcc(as, CC_GE);
878 emit_ab(as, PPCI_CMPLW, type, tisnum);
879 if (ra_hasreg(dest)) {
880 if (ofs == AHUREF_LSX) {
881 tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR,
882 (idx&255)), (idx>>8)));
883 emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp);
884 } else {
885 emit_fai(as, PPCI_LFD, dest, idx, ofs);
888 } else {
889 asm_guardcc(as, CC_NE);
890 emit_ai(as, PPCI_CMPWI, type, irt_toitype(t));
891 if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4);
893 if (ofs == AHUREF_LSX) {
894 emit_tab(as, PPCI_LWZX, type, (idx&255), tmp);
895 emit_slwi(as, tmp, (idx>>8), 3);
896 } else {
897 emit_tai(as, PPCI_LWZ, type, idx, ofs);
901 static void asm_ahustore(ASMState *as, IRIns *ir)
903 RegSet allow = RSET_GPR;
904 Reg idx, src = RID_NONE, type = RID_NONE;
905 int32_t ofs = AHUREF_LSX;
906 if (ir->r == RID_SINK)
907 return;
908 if (irt_isnum(ir->t)) {
909 src = ra_alloc1(as, ir->op2, RSET_FPR);
910 } else {
911 if (!irt_ispri(ir->t)) {
912 src = ra_alloc1(as, ir->op2, allow);
913 rset_clear(allow, src);
914 ofs = 0;
916 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
917 rset_clear(allow, type);
919 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
920 if (irt_isnum(ir->t)) {
921 if (ofs == AHUREF_LSX) {
922 emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP);
923 emit_slwi(as, RID_TMP, (idx>>8), 3);
924 } else {
925 emit_fai(as, PPCI_STFD, src, idx, ofs);
927 } else {
928 if (ra_hasreg(src))
929 emit_tai(as, PPCI_STW, src, idx, ofs+4);
930 if (ofs == AHUREF_LSX) {
931 emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP);
932 emit_slwi(as, RID_TMP, (idx>>8), 3);
933 } else {
934 emit_tai(as, PPCI_STW, type, idx, ofs);
939 static void asm_sload(ASMState *as, IRIns *ir)
941 int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4);
942 IRType1 t = ir->t;
943 Reg dest = RID_NONE, type = RID_NONE, base;
944 RegSet allow = RSET_GPR;
945 lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */
946 lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));
947 lua_assert(LJ_DUALNUM ||
948 !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));
949 if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
950 dest = ra_scratch(as, RSET_FPR);
951 asm_tointg(as, ir, dest);
952 t.irt = IRT_NUM; /* Continue with a regular number type check. */
953 } else if (ra_used(ir)) {
954 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
955 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
956 rset_clear(allow, dest);
957 base = ra_alloc1(as, REF_BASE, allow);
958 rset_clear(allow, base);
959 if ((ir->op2 & IRSLOAD_CONVERT)) {
960 if (irt_isint(t)) {
961 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
962 dest = ra_scratch(as, RSET_FPR);
963 emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP);
964 emit_fb(as, PPCI_FCTIWZ, dest, dest);
965 t.irt = IRT_NUM; /* Check for original type. */
966 } else {
967 Reg tmp = ra_scratch(as, allow);
968 Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp));
969 Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
970 emit_fab(as, PPCI_FSUB, dest, dest, fbias);
971 emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
972 emit_lsptr(as, PPCI_LFS, (fbias & 31),
973 (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
974 rset_clear(allow, hibias));
975 emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO);
976 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
977 emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000);
978 dest = tmp;
979 t.irt = IRT_INT; /* Check for original type. */
982 goto dotypecheck;
984 base = ra_alloc1(as, REF_BASE, allow);
985 rset_clear(allow, base);
986 dotypecheck:
987 if (irt_isnum(t)) {
988 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
989 Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
990 asm_guardcc(as, CC_GE);
991 emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum);
992 type = RID_TMP;
994 if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4);
995 } else {
996 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
997 asm_guardcc(as, CC_NE);
998 emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t));
999 type = RID_TMP;
1001 if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs);
1003 if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4);
1006 /* -- Allocations --------------------------------------------------------- */
1008 #if LJ_HASFFI
1009 static void asm_cnew(ASMState *as, IRIns *ir)
1011 CTState *cts = ctype_ctsG(J2G(as->J));
1012 CTypeID ctypeid = (CTypeID)IR(ir->op1)->i;
1013 CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ?
1014 lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i;
1015 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
1016 IRRef args[2];
1017 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1018 RegSet drop = RSET_SCRATCH;
1019 lua_assert(sz != CTSIZE_INVALID);
1021 args[0] = ASMREF_L; /* lua_State *L */
1022 args[1] = ASMREF_TMP1; /* MSize size */
1023 as->gcsteps++;
1025 if (ra_hasreg(ir->r))
1026 rset_clear(drop, ir->r); /* Dest reg handled below. */
1027 ra_evictset(as, drop);
1028 if (ra_used(ir))
1029 ra_destreg(as, ir, RID_RET); /* GCcdata * */
1031 /* Initialize immutable cdata object. */
1032 if (ir->o == IR_CNEWI) {
1033 int32_t ofs = sizeof(GCcdata);
1034 lua_assert(sz == 4 || sz == 8);
1035 if (sz == 8) {
1036 ofs += 4;
1037 lua_assert((ir+1)->o == IR_HIOP);
1039 for (;;) {
1040 Reg r = ra_alloc1(as, ir->op2, allow);
1041 emit_tai(as, PPCI_STW, r, RID_RET, ofs);
1042 rset_clear(allow, r);
1043 if (ofs == sizeof(GCcdata)) break;
1044 ofs -= 4; ir++;
1047 /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
1048 emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
1049 emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));
1050 emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA);
1051 emit_ti(as, PPCI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */
1052 asm_gencall(as, ci, args);
1053 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
1054 ra_releasetmp(as, ASMREF_TMP1));
1056 #else
1057 #define asm_cnew(as, ir) ((void)0)
1058 #endif
1060 /* -- Write barriers ------------------------------------------------------ */
1062 static void asm_tbar(ASMState *as, IRIns *ir)
1064 Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
1065 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
1066 Reg link = RID_TMP;
1067 MCLabel l_end = emit_label(as);
1068 emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist));
1069 emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked));
1070 emit_setgl(as, tab, gc.grayagain);
1071 lua_assert(LJ_GC_BLACK == 0x04);
1072 emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */
1073 emit_getgl(as, link, gc.grayagain);
1074 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
1075 emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK);
1076 emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked));
1079 static void asm_obar(ASMState *as, IRIns *ir)
1081 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
1082 IRRef args[2];
1083 MCLabel l_end;
1084 Reg obj, val, tmp;
1085 /* No need for other object barriers (yet). */
1086 lua_assert(IR(ir->op1)->o == IR_UREFC);
1087 ra_evictset(as, RSET_SCRATCH);
1088 l_end = emit_label(as);
1089 args[0] = ASMREF_TMP1; /* global_State *g */
1090 args[1] = ir->op1; /* TValue *tv */
1091 asm_gencall(as, ci, args);
1092 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1093 obj = IR(ir->op1)->r;
1094 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
1095 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
1096 emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK);
1097 emit_condbranch(as, PPCI_BC, CC_EQ, l_end);
1098 emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES);
1099 val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
1100 emit_tai(as, PPCI_LBZ, tmp, obj,
1101 (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
1102 emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked));
1105 /* -- Arithmetic and logic operations ------------------------------------- */
1107 static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi)
1109 Reg dest = ra_dest(as, ir, RSET_FPR);
1110 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1111 right = (left >> 8); left &= 255;
1112 if (pi == PPCI_FMUL)
1113 emit_fac(as, pi, dest, left, right);
1114 else
1115 emit_fab(as, pi, dest, left, right);
1118 static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi)
1120 Reg dest = ra_dest(as, ir, RSET_FPR);
1121 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
1122 emit_fb(as, pi, dest, left);
1125 static void asm_fpmath(ASMState *as, IRIns *ir)
1127 if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
1128 return;
1129 if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT))
1130 asm_fpunary(as, ir, PPCI_FSQRT);
1131 else
1132 asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
1135 static void asm_add(ASMState *as, IRIns *ir)
1137 if (irt_isnum(ir->t)) {
1138 if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD))
1139 asm_fparith(as, ir, PPCI_FADD);
1140 } else {
1141 Reg dest = ra_dest(as, ir, RSET_GPR);
1142 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1143 PPCIns pi;
1144 if (irref_isk(ir->op2)) {
1145 int32_t k = IR(ir->op2)->i;
1146 if (checki16(k)) {
1147 pi = PPCI_ADDI;
1148 /* May fail due to spills/restores above, but simplifies the logic. */
1149 if (as->flagmcp == as->mcp) {
1150 as->flagmcp = NULL;
1151 as->mcp++;
1152 pi = PPCI_ADDICDOT;
1154 emit_tai(as, pi, dest, left, k);
1155 return;
1156 } else if ((k & 0xffff) == 0) {
1157 emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16));
1158 return;
1159 } else if (!as->sectref) {
1160 emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16);
1161 emit_tai(as, PPCI_ADDI, dest, left, k);
1162 return;
1165 pi = PPCI_ADD;
1166 /* May fail due to spills/restores above, but simplifies the logic. */
1167 if (as->flagmcp == as->mcp) {
1168 as->flagmcp = NULL;
1169 as->mcp++;
1170 pi |= PPCF_DOT;
1172 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1173 emit_tab(as, pi, dest, left, right);
1177 static void asm_sub(ASMState *as, IRIns *ir)
1179 if (irt_isnum(ir->t)) {
1180 if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB))
1181 asm_fparith(as, ir, PPCI_FSUB);
1182 } else {
1183 PPCIns pi = PPCI_SUBF;
1184 Reg dest = ra_dest(as, ir, RSET_GPR);
1185 Reg left, right;
1186 if (irref_isk(ir->op1)) {
1187 int32_t k = IR(ir->op1)->i;
1188 if (checki16(k)) {
1189 right = ra_alloc1(as, ir->op2, RSET_GPR);
1190 emit_tai(as, PPCI_SUBFIC, dest, right, k);
1191 return;
1194 /* May fail due to spills/restores above, but simplifies the logic. */
1195 if (as->flagmcp == as->mcp) {
1196 as->flagmcp = NULL;
1197 as->mcp++;
1198 pi |= PPCF_DOT;
1200 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1201 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1202 emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */
1206 static void asm_mul(ASMState *as, IRIns *ir)
1208 if (irt_isnum(ir->t)) {
1209 asm_fparith(as, ir, PPCI_FMUL);
1210 } else {
1211 PPCIns pi = PPCI_MULLW;
1212 Reg dest = ra_dest(as, ir, RSET_GPR);
1213 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1214 if (irref_isk(ir->op2)) {
1215 int32_t k = IR(ir->op2)->i;
1216 if (checki16(k)) {
1217 emit_tai(as, PPCI_MULLI, dest, left, k);
1218 return;
1221 /* May fail due to spills/restores above, but simplifies the logic. */
1222 if (as->flagmcp == as->mcp) {
1223 as->flagmcp = NULL;
1224 as->mcp++;
1225 pi |= PPCF_DOT;
1227 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1228 emit_tab(as, pi, dest, left, right);
1232 #define asm_div(as, ir) asm_fparith(as, ir, PPCI_FDIV)
1233 #define asm_mod(as, ir) asm_callid(as, ir, IRCALL_lj_vm_modi)
1234 #define asm_pow(as, ir) asm_callid(as, ir, IRCALL_lj_vm_powi)
1236 static void asm_neg(ASMState *as, IRIns *ir)
1238 if (irt_isnum(ir->t)) {
1239 asm_fpunary(as, ir, PPCI_FNEG);
1240 } else {
1241 Reg dest, left;
1242 PPCIns pi = PPCI_NEG;
1243 if (as->flagmcp == as->mcp) {
1244 as->flagmcp = NULL;
1245 as->mcp++;
1246 pi |= PPCF_DOT;
1248 dest = ra_dest(as, ir, RSET_GPR);
1249 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1250 emit_tab(as, pi, dest, left, 0);
1254 #define asm_abs(as, ir) asm_fpunary(as, ir, PPCI_FABS)
1255 #define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
1256 #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
1258 static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi)
1260 Reg dest, left, right;
1261 if (as->flagmcp == as->mcp) {
1262 as->flagmcp = NULL;
1263 as->mcp++;
1265 asm_guardcc(as, CC_SO);
1266 dest = ra_dest(as, ir, RSET_GPR);
1267 left = ra_alloc2(as, ir, RSET_GPR);
1268 right = (left >> 8); left &= 255;
1269 if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; }
1270 emit_tab(as, pi|PPCF_DOT, dest, left, right);
1273 #define asm_addov(as, ir) asm_arithov(as, ir, PPCI_ADDO)
1274 #define asm_subov(as, ir) asm_arithov(as, ir, PPCI_SUBFO)
1275 #define asm_mulov(as, ir) asm_arithov(as, ir, PPCI_MULLWO)
1277 #if LJ_HASFFI
1278 static void asm_add64(ASMState *as, IRIns *ir)
1280 Reg dest = ra_dest(as, ir, RSET_GPR);
1281 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1282 PPCIns pi = PPCI_ADDE;
1283 if (irref_isk(ir->op2)) {
1284 int32_t k = IR(ir->op2)->i;
1285 if (k == 0)
1286 pi = PPCI_ADDZE;
1287 else if (k == -1)
1288 pi = PPCI_ADDME;
1289 else
1290 goto needright;
1291 right = 0;
1292 } else {
1293 needright:
1294 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1296 emit_tab(as, pi, dest, left, right);
1297 ir--;
1298 dest = ra_dest(as, ir, RSET_GPR);
1299 left = ra_alloc1(as, ir->op1, RSET_GPR);
1300 if (irref_isk(ir->op2)) {
1301 int32_t k = IR(ir->op2)->i;
1302 if (checki16(k)) {
1303 emit_tai(as, PPCI_ADDIC, dest, left, k);
1304 return;
1307 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1308 emit_tab(as, PPCI_ADDC, dest, left, right);
1311 static void asm_sub64(ASMState *as, IRIns *ir)
1313 Reg dest = ra_dest(as, ir, RSET_GPR);
1314 Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR);
1315 PPCIns pi = PPCI_SUBFE;
1316 if (irref_isk(ir->op1)) {
1317 int32_t k = IR(ir->op1)->i;
1318 if (k == 0)
1319 pi = PPCI_SUBFZE;
1320 else if (k == -1)
1321 pi = PPCI_SUBFME;
1322 else
1323 goto needleft;
1324 left = 0;
1325 } else {
1326 needleft:
1327 left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
1329 emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */
1330 ir--;
1331 dest = ra_dest(as, ir, RSET_GPR);
1332 right = ra_alloc1(as, ir->op2, RSET_GPR);
1333 if (irref_isk(ir->op1)) {
1334 int32_t k = IR(ir->op1)->i;
1335 if (checki16(k)) {
1336 emit_tai(as, PPCI_SUBFIC, dest, right, k);
1337 return;
1340 left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
1341 emit_tab(as, PPCI_SUBFC, dest, right, left);
1344 static void asm_neg64(ASMState *as, IRIns *ir)
1346 Reg dest = ra_dest(as, ir, RSET_GPR);
1347 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1348 emit_tab(as, PPCI_SUBFZE, dest, left, 0);
1349 ir--;
1350 dest = ra_dest(as, ir, RSET_GPR);
1351 left = ra_alloc1(as, ir->op1, RSET_GPR);
1352 emit_tai(as, PPCI_SUBFIC, dest, left, 0);
1354 #endif
1356 static void asm_bnot(ASMState *as, IRIns *ir)
1358 Reg dest, left, right;
1359 PPCIns pi = PPCI_NOR;
1360 if (as->flagmcp == as->mcp) {
1361 as->flagmcp = NULL;
1362 as->mcp++;
1363 pi |= PPCF_DOT;
1365 dest = ra_dest(as, ir, RSET_GPR);
1366 if (mayfuse(as, ir->op1)) {
1367 IRIns *irl = IR(ir->op1);
1368 if (irl->o == IR_BAND)
1369 pi ^= (PPCI_NOR ^ PPCI_NAND);
1370 else if (irl->o == IR_BXOR)
1371 pi ^= (PPCI_NOR ^ PPCI_EQV);
1372 else if (irl->o != IR_BOR)
1373 goto nofuse;
1374 left = ra_hintalloc(as, irl->op1, dest, RSET_GPR);
1375 right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left));
1376 } else {
1377 nofuse:
1378 left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1380 emit_asb(as, pi, dest, left, right);
1383 static void asm_bswap(ASMState *as, IRIns *ir)
1385 Reg dest = ra_dest(as, ir, RSET_GPR);
1386 IRIns *irx;
1387 if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD &&
1388 ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) {
1389 /* Fuse BSWAP with XLOAD to lwbrx. */
1390 asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR);
1391 } else {
1392 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1393 Reg tmp = dest;
1394 if (tmp == left) {
1395 tmp = RID_TMP;
1396 emit_mr(as, dest, RID_TMP);
1398 emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23);
1399 emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7);
1400 emit_rotlwi(as, tmp, left, 8);
1404 /* Fuse BAND with contiguous bitmask and a shift to rlwinm. */
1405 static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref)
1407 IRIns *ir;
1408 Reg left;
1409 if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) &&
1410 irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) {
1411 int32_t sh = (IR(ir->op2)->i & 31);
1412 switch (ir->o) {
1413 case IR_BSHL:
1414 if ((mask & ((1u<<sh)-1))) goto nofuse;
1415 break;
1416 case IR_BSHR:
1417 if ((mask & ~((~0u)>>sh))) goto nofuse;
1418 sh = ((32-sh)&31);
1419 break;
1420 case IR_BROL:
1421 break;
1422 default:
1423 goto nofuse;
1425 left = ra_alloc1(as, ir->op1, RSET_GPR);
1426 *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh);
1427 return;
1429 nofuse:
1430 left = ra_alloc1(as, ref, RSET_GPR);
1431 *--as->mcp = pi | PPCF_T(left);
1434 static void asm_band(ASMState *as, IRIns *ir)
1436 Reg dest, left, right;
1437 IRRef lref = ir->op1;
1438 PPCIns dot = 0;
1439 IRRef op2;
1440 if (as->flagmcp == as->mcp) {
1441 as->flagmcp = NULL;
1442 as->mcp++;
1443 dot = PPCF_DOT;
1445 dest = ra_dest(as, ir, RSET_GPR);
1446 if (irref_isk(ir->op2)) {
1447 int32_t k = IR(ir->op2)->i;
1448 if (k) {
1449 /* First check for a contiguous bitmask as used by rlwinm. */
1450 uint32_t s1 = lj_ffs((uint32_t)k);
1451 uint32_t k1 = ((uint32_t)k >> s1);
1452 if ((k1 & (k1+1)) == 0) {
1453 asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |
1454 PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1),
1455 k, lref);
1456 return;
1458 if (~(uint32_t)k) {
1459 uint32_t s2 = lj_ffs(~(uint32_t)k);
1460 uint32_t k2 = (~(uint32_t)k >> s2);
1461 if ((k2 & (k2+1)) == 0) {
1462 asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) |
1463 PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)),
1464 k, lref);
1465 return;
1469 if (checku16(k)) {
1470 left = ra_alloc1(as, lref, RSET_GPR);
1471 emit_asi(as, PPCI_ANDIDOT, dest, left, k);
1472 return;
1473 } else if ((k & 0xffff) == 0) {
1474 left = ra_alloc1(as, lref, RSET_GPR);
1475 emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16));
1476 return;
1479 op2 = ir->op2;
1480 if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) {
1481 dot ^= (PPCI_AND ^ PPCI_ANDC);
1482 op2 = IR(op2)->op1;
1484 left = ra_hintalloc(as, lref, dest, RSET_GPR);
1485 right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left));
1486 emit_asb(as, PPCI_AND ^ dot, dest, left, right);
1489 static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
1491 Reg dest = ra_dest(as, ir, RSET_GPR);
1492 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1493 if (irref_isk(ir->op2)) {
1494 int32_t k = IR(ir->op2)->i;
1495 Reg tmp = left;
1496 if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) {
1497 if (!checku16(k)) {
1498 emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16));
1499 if ((k & 0xffff) == 0) return;
1501 emit_asi(as, pik, dest, left, k);
1502 return;
1505 /* May fail due to spills/restores above, but simplifies the logic. */
1506 if (as->flagmcp == as->mcp) {
1507 as->flagmcp = NULL;
1508 as->mcp++;
1509 pi |= PPCF_DOT;
1511 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1512 emit_asb(as, pi, dest, left, right);
1515 #define asm_bor(as, ir) asm_bitop(as, ir, PPCI_OR, PPCI_ORI)
1516 #define asm_bxor(as, ir) asm_bitop(as, ir, PPCI_XOR, PPCI_XORI)
1518 static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
1520 Reg dest, left;
1521 Reg dot = 0;
1522 if (as->flagmcp == as->mcp) {
1523 as->flagmcp = NULL;
1524 as->mcp++;
1525 dot = PPCF_DOT;
1527 dest = ra_dest(as, ir, RSET_GPR);
1528 left = ra_alloc1(as, ir->op1, RSET_GPR);
1529 if (irref_isk(ir->op2)) { /* Constant shifts. */
1530 int32_t shift = (IR(ir->op2)->i & 31);
1531 if (pik == 0) /* SLWI */
1532 emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift);
1533 else if (pik == 1) /* SRWI */
1534 emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31);
1535 else
1536 emit_asb(as, pik|dot, dest, left, shift);
1537 } else {
1538 Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1539 emit_asb(as, pi|dot, dest, left, right);
1543 #define asm_bshl(as, ir) asm_bitshift(as, ir, PPCI_SLW, 0)
1544 #define asm_bshr(as, ir) asm_bitshift(as, ir, PPCI_SRW, 1)
1545 #define asm_bsar(as, ir) asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI)
1546 #define asm_brol(as, ir) \
1547 asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), \
1548 PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31))
1549 #define asm_bror(as, ir) lua_assert(0)
1551 static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1553 if (irt_isnum(ir->t)) {
1554 Reg dest = ra_dest(as, ir, RSET_FPR);
1555 Reg tmp = dest;
1556 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1557 right = (left >> 8); left &= 255;
1558 if (tmp == left || tmp == right)
1559 tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR,
1560 dest), left), right));
1561 emit_facb(as, PPCI_FSEL, dest, tmp,
1562 ismax ? left : right, ismax ? right : left);
1563 emit_fab(as, PPCI_FSUB, tmp, left, right);
1564 } else {
1565 Reg dest = ra_dest(as, ir, RSET_GPR);
1566 Reg tmp1 = RID_TMP, tmp2 = dest;
1567 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1568 right = (left >> 8); left &= 255;
1569 if (tmp2 == left || tmp2 == right)
1570 tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR,
1571 dest), left), right));
1572 emit_tab(as, PPCI_ADD, dest, tmp2, right);
1573 emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1);
1574 emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1);
1575 emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1);
1576 emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000);
1577 emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000);
1581 #define asm_min(as, ir) asm_min_max(as, ir, 0)
1582 #define asm_max(as, ir) asm_min_max(as, ir, 1)
1584 /* -- Comparisons --------------------------------------------------------- */
1586 #define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */
1587 #define CC_TWO 0x80 /* Check two flags for FP comparison. */
1589 /* Map of comparisons to flags. ORDER IR. */
1590 static const uint8_t asm_compmap[IR_ABC+1] = {
1591 /* op int cc FP cc */
1592 /* LT */ CC_GE + (CC_GE<<4),
1593 /* GE */ CC_LT + (CC_LE<<4) + CC_TWO,
1594 /* LE */ CC_GT + (CC_GE<<4) + CC_TWO,
1595 /* GT */ CC_LE + (CC_LE<<4),
1596 /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO,
1597 /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4),
1598 /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4),
1599 /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO,
1600 /* EQ */ CC_NE + (CC_NE<<4),
1601 /* NE */ CC_EQ + (CC_EQ<<4),
1602 /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */
1605 static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc)
1607 Reg right, left = ra_alloc1(as, lref, RSET_GPR);
1608 if (irref_isk(rref)) {
1609 int32_t k = IR(rref)->i;
1610 if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */
1611 if (checki16(k)) {
1612 emit_tai(as, PPCI_CMPWI, cr, left, k);
1613 /* Signed comparison with zero and referencing previous ins? */
1614 if (k == 0 && lref == as->curins-1)
1615 as->flagmcp = as->mcp; /* Allow elimination of the compare. */
1616 return;
1617 } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */
1618 if (checku16(k)) {
1619 emit_tai(as, PPCI_CMPLWI, cr, left, k);
1620 return;
1621 } else if (!as->sectref && ra_noreg(IR(rref)->r)) {
1622 emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k);
1623 emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16));
1624 return;
1627 } else { /* Unsigned comparison with constant. */
1628 if (checku16(k)) {
1629 emit_tai(as, PPCI_CMPLWI, cr, left, k);
1630 return;
1634 right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left));
1635 emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right);
1638 static void asm_comp(ASMState *as, IRIns *ir)
1640 PPCCC cc = asm_compmap[ir->o];
1641 if (irt_isnum(ir->t)) {
1642 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1643 right = (left >> 8); left &= 255;
1644 asm_guardcc(as, (cc >> 4));
1645 if ((cc & CC_TWO))
1646 emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3));
1647 emit_fab(as, PPCI_FCMPU, 0, left, right);
1648 } else {
1649 IRRef lref = ir->op1, rref = ir->op2;
1650 if (irref_isk(lref) && !irref_isk(rref)) {
1651 /* Swap constants to the right (only for ABC). */
1652 IRRef tmp = lref; lref = rref; rref = tmp;
1653 if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */
1655 asm_guardcc(as, cc);
1656 asm_intcomp_(as, lref, rref, 0, cc);
1660 #define asm_equal(as, ir) asm_comp(as, ir)
1662 #if LJ_HASFFI
1663 /* 64 bit integer comparisons. */
1664 static void asm_comp64(ASMState *as, IRIns *ir)
1666 PPCCC cc = asm_compmap[(ir-1)->o];
1667 if ((cc&3) == (CC_EQ&3)) {
1668 asm_guardcc(as, cc);
1669 emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR,
1670 (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3));
1671 } else {
1672 asm_guardcc(as, CC_EQ);
1673 emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1));
1674 emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC,
1675 (CC_EQ&3), (CC_EQ&3), 4+(cc&3));
1677 /* Loword comparison sets cr1 and is unsigned, except for equality. */
1678 asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4,
1679 cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED));
1680 /* Hiword comparison sets cr0. */
1681 asm_intcomp_(as, ir->op1, ir->op2, 0, cc);
1682 as->flagmcp = NULL; /* Doesn't work here. */
1684 #endif
1686 /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */
1688 /* Hiword op of a split 64 bit op. Previous op must be the loword op. */
1689 static void asm_hiop(ASMState *as, IRIns *ir)
1691 #if LJ_HASFFI
1692 /* HIOP is marked as a store because it needs its own DCE logic. */
1693 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
1694 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
1695 if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
1696 as->curins--; /* Always skip the CONV. */
1697 if (usehi || uselo)
1698 asm_conv64(as, ir);
1699 return;
1700 } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
1701 as->curins--; /* Always skip the loword comparison. */
1702 asm_comp64(as, ir);
1703 return;
1704 } else if ((ir-1)->o == IR_XSTORE) {
1705 as->curins--; /* Handle both stores here. */
1706 if ((ir-1)->r != RID_SINK) {
1707 asm_xstore_(as, ir, 0);
1708 asm_xstore_(as, ir-1, 4);
1710 return;
1712 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
1713 switch ((ir-1)->o) {
1714 case IR_ADD: as->curins--; asm_add64(as, ir); break;
1715 case IR_SUB: as->curins--; asm_sub64(as, ir); break;
1716 case IR_NEG: as->curins--; asm_neg64(as, ir); break;
1717 case IR_CALLN:
1718 case IR_CALLXS:
1719 if (!uselo)
1720 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
1721 break;
1722 case IR_CNEWI:
1723 /* Nothing to do here. Handled by lo op itself. */
1724 break;
1725 default: lua_assert(0); break;
1727 #else
1728 UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */
1729 #endif
1732 /* -- Stack handling ------------------------------------------------------ */
1734 /* Check Lua stack size for overflow. Use exit handler as fallback. */
1735 static void asm_stack_check(ASMState *as, BCReg topslot,
1736 IRIns *irp, RegSet allow, ExitNo exitno)
1738 /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */
1739 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
1740 rset_clear(allow, pbase);
1741 tmp = allow ? rset_pickbot(allow) :
1742 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
1743 emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno));
1744 if (allow == RSET_EMPTY) /* Restore temp. register. */
1745 emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW);
1746 else
1747 ra_modified(as, tmp);
1748 emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot));
1749 emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp);
1750 emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack));
1751 if (pbase == RID_TMP)
1752 emit_getgl(as, RID_TMP, jit_base);
1753 emit_getgl(as, tmp, jit_L);
1754 if (allow == RSET_EMPTY) /* Spill temp. register. */
1755 emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW);
1758 /* Restore Lua stack from on-trace state. */
1759 static void asm_stack_restore(ASMState *as, SnapShot *snap)
1761 SnapEntry *map = &as->T->snapmap[snap->mapofs];
1762 SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];
1763 MSize n, nent = snap->nent;
1764 /* Store the value of all modified slots to the Lua stack. */
1765 for (n = 0; n < nent; n++) {
1766 SnapEntry sn = map[n];
1767 BCReg s = snap_slot(sn);
1768 int32_t ofs = 8*((int32_t)s-1);
1769 IRRef ref = snap_ref(sn);
1770 IRIns *ir = IR(ref);
1771 if ((sn & SNAP_NORESTORE))
1772 continue;
1773 if (irt_isnum(ir->t)) {
1774 Reg src = ra_alloc1(as, ref, RSET_FPR);
1775 emit_fai(as, PPCI_STFD, src, RID_BASE, ofs);
1776 } else {
1777 Reg type;
1778 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
1779 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
1780 if (!irt_ispri(ir->t)) {
1781 Reg src = ra_alloc1(as, ref, allow);
1782 rset_clear(allow, src);
1783 emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4);
1785 if ((sn & (SNAP_CONT|SNAP_FRAME))) {
1786 if (s == 0) continue; /* Do not overwrite link to previous frame. */
1787 type = ra_allock(as, (int32_t)(*flinks--), allow);
1788 } else {
1789 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
1791 emit_tai(as, PPCI_STW, type, RID_BASE, ofs);
1793 checkmclim(as);
1795 lua_assert(map + nent == flinks);
1798 /* -- GC handling --------------------------------------------------------- */
1800 /* Check GC threshold and do one or more GC steps. */
1801 static void asm_gc_check(ASMState *as)
1803 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
1804 IRRef args[2];
1805 MCLabel l_end;
1806 Reg tmp;
1807 ra_evictset(as, RSET_SCRATCH);
1808 l_end = emit_label(as);
1809 /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
1810 asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */
1811 emit_ai(as, PPCI_CMPWI, RID_RET, 0);
1812 args[0] = ASMREF_TMP1; /* global_State *g */
1813 args[1] = ASMREF_TMP2; /* MSize steps */
1814 asm_gencall(as, ci, args);
1815 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1816 tmp = ra_releasetmp(as, ASMREF_TMP2);
1817 emit_loadi(as, tmp, as->gcsteps);
1818 /* Jump around GC step if GC total < GC threshold. */
1819 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end);
1820 emit_ab(as, PPCI_CMPLW, RID_TMP, tmp);
1821 emit_getgl(as, tmp, gc.threshold);
1822 emit_getgl(as, RID_TMP, gc.total);
1823 as->gcsteps = 0;
1824 checkmclim(as);
1827 /* -- Loop handling ------------------------------------------------------- */
1829 /* Fixup the loop branch. */
1830 static void asm_loop_fixup(ASMState *as)
1832 MCode *p = as->mctop;
1833 MCode *target = as->mcp;
1834 if (as->loopinv) { /* Inverted loop branch? */
1835 /* asm_guardcc already inverted the cond branch and patched the final b. */
1836 p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2);
1837 } else {
1838 p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
1842 /* -- Head of trace ------------------------------------------------------- */
1844 /* Coalesce BASE register for a root trace. */
1845 static void asm_head_root_base(ASMState *as)
1847 IRIns *ir = IR(REF_BASE);
1848 Reg r = ir->r;
1849 if (ra_hasreg(r)) {
1850 ra_free(as, r);
1851 if (rset_test(as->modset, r))
1852 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1853 if (r != RID_BASE)
1854 emit_mr(as, r, RID_BASE);
1858 /* Coalesce BASE register for a side trace. */
1859 static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)
1861 IRIns *ir = IR(REF_BASE);
1862 Reg r = ir->r;
1863 if (ra_hasreg(r)) {
1864 ra_free(as, r);
1865 if (rset_test(as->modset, r))
1866 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1867 if (irp->r == r) {
1868 rset_clear(allow, r); /* Mark same BASE register as coalesced. */
1869 } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
1870 rset_clear(allow, irp->r);
1871 emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */
1872 } else {
1873 emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
1876 return allow;
1879 /* -- Tail of trace ------------------------------------------------------- */
1881 /* Fixup the tail code. */
1882 static void asm_tail_fixup(ASMState *as, TraceNo lnk)
1884 MCode *p = as->mctop;
1885 MCode *target;
1886 int32_t spadj = as->T->spadjust;
1887 if (spadj == 0) {
1888 *--p = PPCI_NOP;
1889 *--p = PPCI_NOP;
1890 as->mctop = p;
1891 } else {
1892 /* Patch stack adjustment. */
1893 lua_assert(checki16(CFRAME_SIZE+spadj));
1894 p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj);
1895 p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj;
1897 /* Patch exit branch. */
1898 target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
1899 p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
1902 /* Prepare tail of code. */
1903 static void asm_tail_prep(ASMState *as)
1905 MCode *p = as->mctop - 1; /* Leave room for exit branch. */
1906 if (as->loopref) {
1907 as->invmcp = as->mcp = p;
1908 } else {
1909 as->mcp = p-2; /* Leave room for stack pointer adjustment. */
1910 as->invmcp = NULL;
1914 /* -- Trace setup --------------------------------------------------------- */
1916 /* Ensure there are enough stack slots for call arguments. */
1917 static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
1919 IRRef args[CCI_NARGS_MAX*2];
1920 uint32_t i, nargs = CCI_XNARGS(ci);
1921 int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
1922 asm_collectargs(as, ir, ci, args);
1923 for (i = 0; i < nargs; i++)
1924 if (args[i] && irt_isfp(IR(args[i])->t)) {
1925 if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1;
1926 } else {
1927 if (ngpr > 0) ngpr--; else nslots++;
1929 if (nslots > as->evenspill) /* Leave room for args in stack slots. */
1930 as->evenspill = nslots;
1931 return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
1934 static void asm_setup_target(ASMState *as)
1936 asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
1939 /* -- Trace patching ------------------------------------------------------ */
1941 /* Patch exit jumps of existing machine code to a new target. */
1942 void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
1944 MCode *p = T->mcode;
1945 MCode *pe = (MCode *)((char *)p + T->szmcode);
1946 MCode *px = exitstub_trace_addr(T, exitno);
1947 MCode *cstart = NULL;
1948 MCode *mcarea = lj_mcode_patch(J, p, 0);
1949 int clearso = 0;
1950 for (; p < pe; p++) {
1951 /* Look for exitstub branch, try to replace with branch to target. */
1952 uint32_t ins = *p;
1953 if ((ins & 0xfc000000u) == 0x40000000u &&
1954 ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) {
1955 ptrdiff_t delta = (char *)target - (char *)p;
1956 if (((ins >> 16) & 3) == (CC_SO&3)) {
1957 clearso = sizeof(MCode);
1958 delta -= sizeof(MCode);
1960 /* Many, but not all short-range branches can be patched directly. */
1961 if (((delta + 0x8000) >> 16) == 0) {
1962 *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) |
1963 ((delta & 0x8000) * (PPCF_Y/0x8000));
1964 if (!cstart) cstart = p;
1966 } else if ((ins & 0xfc000000u) == PPCI_B &&
1967 ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) {
1968 ptrdiff_t delta = (char *)target - (char *)p;
1969 lua_assert(((delta + 0x02000000) >> 26) == 0);
1970 *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
1971 if (!cstart) cstart = p;
1974 { /* Always patch long-range branch in exit stub itself. */
1975 ptrdiff_t delta = (char *)target - (char *)px - clearso;
1976 lua_assert(((delta + 0x02000000) >> 26) == 0);
1977 *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
1979 if (!cstart) cstart = px;
1980 lj_mcode_sync(cstart, px+1);
1981 if (clearso) { /* Extend the current trace. Ugly workaround. */
1982 MCode *pp = J->cur.mcode;
1983 J->cur.szmcode += sizeof(MCode);
1984 *--pp = PPCI_MCRXR; /* Clear SO flag. */
1985 J->cur.mcode = pp;
1986 lj_mcode_sync(pp, pp+1);
1988 lj_mcode_patch(J, mcarea, 1);