summaryrefslogtreecommitdiff
path: root/src/lj_asm_ppc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_asm_ppc.h')
-rw-r--r--src/lj_asm_ppc.h2074
1 files changed, 2074 insertions, 0 deletions
diff --git a/src/lj_asm_ppc.h b/src/lj_asm_ppc.h
new file mode 100644
index 00000000..72e4c956
--- /dev/null
+++ b/src/lj_asm_ppc.h
@@ -0,0 +1,2074 @@
1/*
2** PPC IR assembler (SSA IR -> machine code).
3** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6/* -- Register allocator extensions --------------------------------------- */
7
8/* Allocate a register with a hint. */
9static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
10{
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);
16 }
17 ra_noweak(as, r);
18 return r;
19}
20
21/* Allocate two source registers for three-operand instructions. */
22static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
23{
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));
41 }
42 return left | (right << 8);
43}
44
45/* -- Guard handling ------------------------------------------------------ */
46
47/* Setup exit stubs after the end of each trace. */
48static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
49{
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;
60}
61
62static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)
63{
64 /* Keep this in-sync with exitstub_trace_addr(). */
65 return as->mctop + exitno + 3;
66}
67
68/* Emit conditional branch to exit for guard. */
69static void asm_guardcc(ASMState *as, PPCCC cc)
70{
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;
78 }
79 emit_condbranch(as, PPCI_BC, cc, target);
80}
81
82/* -- Operand fusion ------------------------------------------------------ */
83
84/* Limit linear search to this distance. Avoids O(n^2) behavior. */
85#define CONFLICT_SEARCH_LIM 31
86
87/* Check if there's no conflicting instruction between curins and ref. */
88static int noconflict(ASMState *as, IRRef ref, IROp conflict)
89{
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. */
98}
99
100/* Fuse the array base of colocated arrays. */
101static int32_t asm_fuseabase(ASMState *as, IRRef ref)
102{
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;
108}
109
110/* Indicates load/store indexed is ok. */
111#define AHUREF_LSX ((int32_t)0x80000000)
112
113/* Fuse array/hash/upvalue reference into register+offset operand. */
114static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
115{
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);
128 }
129 }
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);
134 }
135 }
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);
142 }
143 }
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);
155 }
156 }
157 }
158 }
159 *ofsp = 0;
160 return ra_alloc1(as, ref, allow);
161}
162
163/* Fuse XLOAD/XSTORE reference into load/store operand. */
164static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref,
165 RegSet allow)
166{
167 IRIns *ir = IR(ref);
168 int32_t ofs = 0;
169 Reg base;
170 if (ra_noreg(ir->r) && mayfuse(as, ref)) {
171 if (ir->o == IR_ADD) {
172 if (irref_isk(ir->op2) && (ofs = IR(ir->op2)->i, checki16(ofs))) {
173 ref = ir->op1;
174 } else {
175 Reg right, left = ra_alloc2(as, ir, allow);
176 right = (left >> 8); left &= 255;
177 emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
178 return;
179 }
180 } else if (ir->o == IR_STRREF) {
181 ofs = (int32_t)sizeof(GCstr);
182 if (irref_isk(ir->op2)) {
183 ofs += IR(ir->op2)->i;
184 ref = ir->op1;
185 } else if (irref_isk(ir->op1)) {
186 ofs += IR(ir->op1)->i;
187 ref = ir->op2;
188 } else {
189 /* NYI: Fuse ADD with constant. */
190 Reg right, left = ra_alloc2(as, ir, allow);
191 right = (left >> 8); left &= 255;
192 emit_fai(as, pi, rt, rt, ofs);
193 emit_tab(as, PPCI_ADD, rt, left, right);
194 return;
195 }
196 if (!checki16(ofs)) {
197 Reg left = ra_alloc1(as, ref, allow);
198 Reg right = ra_allock(as, ofs, rset_exclude(allow, left));
199 emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right);
200 return;
201 }
202 }
203 }
204 base = ra_alloc1(as, ref, allow);
205 emit_fai(as, pi, rt, base, ofs);
206}
207
208/* Fuse to multiply-add/sub instruction. */
209static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir)
210{
211 IRRef lref = ir->op1, rref = ir->op2;
212 IRIns *irm;
213 if (lref != rref &&
214 ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
215 ra_noreg(irm->r)) ||
216 (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
217 (rref = lref, pi = pir, ra_noreg(irm->r))))) {
218 Reg dest = ra_dest(as, ir, RSET_FPR);
219 Reg add = ra_alloc1(as, rref, RSET_FPR);
220 Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add));
221 right = (left >> 8); left &= 255;
222 emit_facb(as, pi, dest, left, right, add);
223 return 1;
224 }
225 return 0;
226}
227
228/* -- Calls --------------------------------------------------------------- */
229
230/* Generate a call to a C function. */
231static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
232{
233 uint32_t n, nargs = CCI_NARGS(ci);
234 int32_t ofs = 8;
235 Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR;
236 if ((void *)ci->func)
237 emit_call(as, (void *)ci->func);
238 for (n = 0; n < nargs; n++) { /* Setup args. */
239 IRRef ref = args[n];
240 if (ref) {
241 IRIns *ir = IR(ref);
242 if (irt_isfp(ir->t)) {
243 if (fpr <= REGARG_LASTFPR) {
244 lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */
245 ra_leftov(as, fpr, ref);
246 fpr++;
247 } else {
248 Reg r = ra_alloc1(as, ref, RSET_FPR);
249 if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
250 emit_spstore(as, ir, r, ofs);
251 ofs += irt_isnum(ir->t) ? 8 : 4;
252 }
253 } else {
254 if (gpr <= REGARG_LASTGPR) {
255 lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */
256 ra_leftov(as, gpr, ref);
257 gpr++;
258 } else {
259 Reg r = ra_alloc1(as, ref, RSET_GPR);
260 emit_spstore(as, ir, r, ofs);
261 ofs += 4;
262 }
263 }
264 } else {
265 if (gpr <= REGARG_LASTGPR)
266 gpr++;
267 else
268 ofs += 4;
269 }
270 }
271}
272
273/* Setup result reg/sp for call. Evict scratch regs. */
274static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
275{
276 RegSet drop = RSET_SCRATCH;
277 int hiop = ((ir+1)->o == IR_HIOP);
278 if ((ci->flags & CCI_NOFPRCLOBBER))
279 drop &= ~RSET_FPR;
280 if (ra_hasreg(ir->r))
281 rset_clear(drop, ir->r); /* Dest reg handled below. */
282 if (hiop && ra_hasreg((ir+1)->r))
283 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
284 ra_evictset(as, drop); /* Evictions must be performed first. */
285 if (ra_used(ir)) {
286 lua_assert(!irt_ispri(ir->t));
287 if (irt_isfp(ir->t)) {
288 if ((ci->flags & CCI_CASTU64)) {
289 /* Use spill slot or temp slots. */
290 int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
291 Reg dest = ir->r;
292 if (ra_hasreg(dest)) {
293 ra_free(as, dest);
294 ra_modified(as, dest);
295 emit_fai(as, PPCI_LFD, dest, RID_SP, ofs);
296 }
297 emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs);
298 emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4);
299 } else {
300 ra_destreg(as, ir, RID_FPRET);
301 }
302 } else if (hiop) {
303 ra_destpair(as, ir);
304 } else {
305 ra_destreg(as, ir, RID_RET);
306 }
307 }
308}
309
310static void asm_call(ASMState *as, IRIns *ir)
311{
312 IRRef args[CCI_NARGS_MAX];
313 const CCallInfo *ci = &lj_ir_callinfo[ir->op2];
314 asm_collectargs(as, ir, ci, args);
315 asm_setupresult(as, ir, ci);
316 asm_gencall(as, ci, args);
317}
318
319static void asm_callx(ASMState *as, IRIns *ir)
320{
321 IRRef args[CCI_NARGS_MAX];
322 CCallInfo ci;
323 ci.flags = asm_callx_flags(as, ir);
324 asm_collectargs(as, ir, &ci, args);
325 asm_setupresult(as, ir, &ci);
326 if (irref_isk(ir->op2)) { /* Call to constant address. */
327 ci.func = (ASMFunction)(void *)(IR(ir->op2)->i);
328 } else { /* Need a non-argument register for indirect calls. */
329 RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1);
330 Reg freg = ra_alloc1(as, ir->op2, allow);
331 *--as->mcp = PPCI_BCTRL;
332 *--as->mcp = PPCI_MTCTR | PPCF_T(freg);
333 ci.func = (ASMFunction)(void *)0;
334 }
335 asm_gencall(as, &ci, args);
336}
337
338static void asm_callid(ASMState *as, IRIns *ir, IRCallID id)
339{
340 const CCallInfo *ci = &lj_ir_callinfo[id];
341 IRRef args[2];
342 args[0] = ir->op1;
343 args[1] = ir->op2;
344 asm_setupresult(as, ir, ci);
345 asm_gencall(as, ci, args);
346}
347
348/* -- Returns ------------------------------------------------------------- */
349
350/* Return to lower frame. Guard that it goes to the right spot. */
351static void asm_retf(ASMState *as, IRIns *ir)
352{
353 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
354 void *pc = ir_kptr(IR(ir->op2));
355 int32_t delta = 1+bc_a(*((const BCIns *)pc - 1));
356 as->topslot -= (BCReg)delta;
357 if ((int32_t)as->topslot < 0) as->topslot = 0;
358 emit_setgl(as, base, jit_base);
359 emit_addptr(as, base, -8*delta);
360 asm_guardcc(as, CC_NE);
361 emit_ab(as, PPCI_CMPW, RID_TMP,
362 ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));
363 emit_tai(as, PPCI_LWZ, RID_TMP, base, -8);
364}
365
366/* -- Type conversions ---------------------------------------------------- */
367
368static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
369{
370 RegSet allow = RSET_FPR;
371 Reg tmp = ra_scratch(as, rset_clear(allow, left));
372 Reg fbias = ra_scratch(as, rset_clear(allow, tmp));
373 Reg dest = ra_dest(as, ir, RSET_GPR);
374 Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest));
375 asm_guardcc(as, CC_NE);
376 emit_fab(as, PPCI_FCMPU, 0, tmp, left);
377 emit_fab(as, PPCI_FSUB, tmp, tmp, fbias);
378 emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP);
379 emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO);
380 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
381 emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000);
382 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
383 emit_lsptr(as, PPCI_LFS, (fbias & 31),
384 (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
385 RSET_GPR);
386 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
387 emit_fb(as, PPCI_FCTIWZ, tmp, left);
388}
389
390static void asm_tobit(ASMState *as, IRIns *ir)
391{
392 RegSet allow = RSET_FPR;
393 Reg dest = ra_dest(as, ir, RSET_GPR);
394 Reg left = ra_alloc1(as, ir->op1, allow);
395 Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
396 Reg tmp = ra_scratch(as, rset_clear(allow, right));
397 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
398 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
399 emit_fab(as, PPCI_FADD, tmp, left, right);
400}
401
402static void asm_conv(ASMState *as, IRIns *ir)
403{
404 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
405 int stfp = (st == IRT_NUM || st == IRT_FLOAT);
406 IRRef lref = ir->op1;
407 lua_assert(irt_type(ir->t) != st);
408 lua_assert(!(irt_isint64(ir->t) ||
409 (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */
410 if (irt_isfp(ir->t)) {
411 Reg dest = ra_dest(as, ir, RSET_FPR);
412 if (stfp) { /* FP to FP conversion. */
413 if (st == IRT_NUM) /* double -> float conversion. */
414 emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR));
415 else /* float -> double conversion is a no-op on PPC. */
416 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
417 } else { /* Integer to FP conversion. */
418 /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */
419 /* IRT_U32: Bias with 2^52, subtract 2^52. */
420 RegSet allow = RSET_GPR;
421 Reg left = ra_alloc1(as, lref, allow);
422 Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left));
423 Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
424 const float *kbias;
425 if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest);
426 emit_fab(as, PPCI_FSUB, dest, dest, fbias);
427 emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
428 kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000));
429 if (st == IRT_U32) kbias++;
430 emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias,
431 rset_clear(allow, hibias));
432 emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP,
433 RID_SP, SPOFS_TMPLO);
434 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
435 if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000);
436 }
437 } else if (stfp) { /* FP to integer conversion. */
438 if (irt_isguard(ir->t)) {
439 /* Checked conversions are only supported from number to int. */
440 lua_assert(irt_isint(ir->t) && st == IRT_NUM);
441 asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
442 } else {
443 Reg dest = ra_dest(as, ir, RSET_GPR);
444 Reg left = ra_alloc1(as, lref, RSET_FPR);
445 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
446 if (irt_isu32(ir->t)) {
447 /* Convert both x and x-2^31 to int and merge results. */
448 Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest));
449 emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */
450 emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP);
451 emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP);
452 emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */
453 emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */
454 emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */
455 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
456 emit_tai(as, PPCI_LWZ, dest,
457 RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */
458 emit_fb(as, PPCI_FCTIWZ, tmp, left);
459 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
460 emit_fb(as, PPCI_FCTIWZ, tmp, tmp);
461 emit_fab(as, PPCI_FSUB, tmp, left, tmp);
462 emit_lsptr(as, PPCI_LFS, (tmp & 31),
463 (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)),
464 RSET_GPR);
465 } else {
466 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
467 emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP);
468 emit_fb(as, PPCI_FCTIWZ, tmp, left);
469 }
470 }
471 } else {
472 Reg dest = ra_dest(as, ir, RSET_GPR);
473 if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
474 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
475 lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));
476 if ((ir->op2 & IRCONV_SEXT))
477 emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left);
478 else
479 emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31);
480 } else { /* 32/64 bit integer conversions. */
481 /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
482 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
483 }
484 }
485}
486
487#if LJ_HASFFI
488static void asm_conv64(ASMState *as, IRIns *ir)
489{
490 IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);
491 IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);
492 IRCallID id;
493 const CCallInfo *ci;
494 IRRef args[2];
495 args[0] = ir->op1;
496 args[1] = (ir-1)->op1;
497 if (st == IRT_NUM || st == IRT_FLOAT) {
498 id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64);
499 ir--;
500 } else {
501 id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64);
502 }
503 ci = &lj_ir_callinfo[id];
504 asm_setupresult(as, ir, ci);
505 asm_gencall(as, ci, args);
506}
507#endif
508
509static void asm_strto(ASMState *as, IRIns *ir)
510{
511 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_tonum];
512 IRRef args[2];
513 int32_t ofs;
514 RegSet drop = RSET_SCRATCH;
515 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
516 ra_evictset(as, drop);
517 asm_guardcc(as, CC_EQ);
518 emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */
519 args[0] = ir->op1; /* GCstr *str */
520 args[1] = ASMREF_TMP1; /* TValue *n */
521 asm_gencall(as, ci, args);
522 /* Store the result to the spill slot or temp slots. */
523 ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP;
524 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs);
525}
526
527/* Get pointer to TValue. */
528static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)
529{
530 IRIns *ir = IR(ref);
531 if (irt_isnum(ir->t)) {
532 if (irref_isk(ref)) /* Use the number constant itself as a TValue. */
533 ra_allockreg(as, i32ptr(ir_knum(ir)), dest);
534 else /* Otherwise force a spill and use the spill slot. */
535 emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir));
536 } else {
537 /* Otherwise use g->tmptv to hold the TValue. */
538 RegSet allow = rset_exclude(RSET_GPR, dest);
539 Reg type;
540 emit_tai(as, PPCI_ADDI, dest, RID_JGL, offsetof(global_State, tmptv)-32768);
541 if (!irt_ispri(ir->t)) {
542 Reg src = ra_alloc1(as, ref, allow);
543 emit_setgl(as, src, tmptv.gcr);
544 }
545 type = ra_allock(as, irt_toitype(ir->t), allow);
546 emit_setgl(as, type, tmptv.it);
547 }
548}
549
550static void asm_tostr(ASMState *as, IRIns *ir)
551{
552 IRRef args[2];
553 args[0] = ASMREF_L;
554 as->gcsteps++;
555 if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) {
556 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum];
557 args[1] = ASMREF_TMP1; /* const lua_Number * */
558 asm_setupresult(as, ir, ci); /* GCstr * */
559 asm_gencall(as, ci, args);
560 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1);
561 } else {
562 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint];
563 args[1] = ir->op1; /* int32_t k */
564 asm_setupresult(as, ir, ci); /* GCstr * */
565 asm_gencall(as, ci, args);
566 }
567}
568
569/* -- Memory references --------------------------------------------------- */
570
571static void asm_aref(ASMState *as, IRIns *ir)
572{
573 Reg dest = ra_dest(as, ir, RSET_GPR);
574 Reg idx, base;
575 if (irref_isk(ir->op2)) {
576 IRRef tab = IR(ir->op1)->op1;
577 int32_t ofs = asm_fuseabase(as, tab);
578 IRRef refa = ofs ? tab : ir->op1;
579 ofs += 8*IR(ir->op2)->i;
580 if (checki16(ofs)) {
581 base = ra_alloc1(as, refa, RSET_GPR);
582 emit_tai(as, PPCI_ADDI, dest, base, ofs);
583 return;
584 }
585 }
586 base = ra_alloc1(as, ir->op1, RSET_GPR);
587 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
588 emit_tab(as, PPCI_ADD, dest, RID_TMP, base);
589 emit_slwi(as, RID_TMP, idx, 3);
590}
591
592/* Inlined hash lookup. Specialized for key type and for const keys.
593** The equivalent C code is:
594** Node *n = hashkey(t, key);
595** do {
596** if (lj_obj_equal(&n->key, key)) return &n->val;
597** } while ((n = nextnode(n)));
598** return niltv(L);
599*/
600static void asm_href(ASMState *as, IRIns *ir, IROp merge)
601{
602 RegSet allow = RSET_GPR;
603 int destused = ra_used(ir);
604 Reg dest = ra_dest(as, ir, allow);
605 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
606 Reg key = RID_NONE, tmp1 = RID_TMP, tmp2;
607 Reg tisnum = RID_NONE, tmpnum = RID_NONE;
608 IRRef refkey = ir->op2;
609 IRIns *irkey = IR(refkey);
610 IRType1 kt = irkey->t;
611 uint32_t khash;
612 MCLabel l_end, l_loop, l_next;
613
614 rset_clear(allow, tab);
615 if (irt_isnum(kt)) {
616 key = ra_alloc1(as, refkey, RSET_FPR);
617 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
618 tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
619 rset_clear(allow, tisnum);
620 } else if (!irt_ispri(kt)) {
621 key = ra_alloc1(as, refkey, allow);
622 rset_clear(allow, key);
623 }
624 tmp2 = ra_scratch(as, allow);
625 rset_clear(allow, tmp2);
626
627 /* Key not found in chain: jump to exit (if merged) or load niltv. */
628 l_end = emit_label(as);
629 as->invmcp = NULL;
630 if (merge == IR_NE)
631 asm_guardcc(as, CC_EQ);
632 else if (destused)
633 emit_loada(as, dest, niltvg(J2G(as->J)));
634
635 /* Follow hash chain until the end. */
636 l_loop = --as->mcp;
637 emit_ai(as, PPCI_CMPWI, dest, 0);
638 emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next));
639 l_next = emit_label(as);
640
641 /* Type and value comparison. */
642 if (merge == IR_EQ)
643 asm_guardcc(as, CC_EQ);
644 else
645 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
646 if (irt_isnum(kt)) {
647 emit_fab(as, PPCI_FCMPU, 0, tmpnum, key);
648 emit_condbranch(as, PPCI_BC, CC_GE, l_next);
649 emit_ab(as, PPCI_CMPLW, tmp1, tisnum);
650 emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n));
651 } else {
652 if (!irt_ispri(kt)) {
653 emit_ab(as, PPCI_CMPW, tmp2, key);
654 emit_condbranch(as, PPCI_BC, CC_NE, l_next);
655 }
656 emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t));
657 if (!irt_ispri(kt))
658 emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr));
659 }
660 emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it));
661 *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) |
662 (((char *)as->mcp-(char *)l_loop) & 0xffffu);
663
664 /* Load main position relative to tab->node into dest. */
665 khash = irref_isk(refkey) ? ir_khash(irkey) : 1;
666 if (khash == 0) {
667 emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
668 } else {
669 Reg tmphash = tmp1;
670 if (irref_isk(refkey))
671 tmphash = ra_allock(as, khash, allow);
672 emit_tab(as, PPCI_ADD, dest, dest, tmp1);
673 emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node));
674 emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash);
675 emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node));
676 emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
677 if (irref_isk(refkey)) {
678 /* Nothing to do. */
679 } else if (irt_isstr(kt)) {
680 emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash));
681 } else { /* Must match with hash*() in lj_tab.c. */
682 emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1);
683 emit_rotlwi(as, tmp2, tmp2, HASH_ROT3);
684 emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2);
685 emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31);
686 emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2);
687 if (irt_isnum(kt)) {
688 int32_t ofs = ra_spill(as, irkey);
689 emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1);
690 emit_rotlwi(as, dest, tmp1, HASH_ROT1);
691 emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1);
692 emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4);
693 emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs);
694 } else {
695 emit_asb(as, PPCI_XOR, tmp2, key, tmp1);
696 emit_rotlwi(as, dest, tmp1, HASH_ROT1);
697 emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS);
698 emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16);
699 }
700 }
701 }
702}
703
704static void asm_hrefk(ASMState *as, IRIns *ir)
705{
706 IRIns *kslot = IR(ir->op2);
707 IRIns *irkey = IR(kslot->op1);
708 int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
709 int32_t kofs = ofs + (int32_t)offsetof(Node, key);
710 Reg dest = (ra_used(ir)||ofs > 65535) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
711 Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
712 Reg key = RID_NONE, type = RID_TMP, idx = node;
713 RegSet allow = rset_exclude(RSET_GPR, node);
714 lua_assert(ofs % sizeof(Node) == 0);
715 if (ofs > 65535) {
716 idx = dest;
717 rset_clear(allow, dest);
718 kofs = (int32_t)offsetof(Node, key);
719 } else if (ra_hasreg(dest)) {
720 emit_tai(as, PPCI_ADDI, dest, node, ofs);
721 }
722 asm_guardcc(as, CC_NE);
723 if (!irt_ispri(irkey->t)) {
724 key = ra_scratch(as, allow);
725 rset_clear(allow, key);
726 }
727 rset_clear(allow, type);
728 if (irt_isnum(irkey->t)) {
729 emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo);
730 asm_guardcc(as, CC_NE);
731 emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi);
732 } else {
733 if (ra_hasreg(key)) {
734 emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */
735 asm_guardcc(as, CC_NE);
736 }
737 emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t));
738 }
739 if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4);
740 emit_tai(as, PPCI_LWZ, type, idx, kofs);
741 if (ofs > 65535) {
742 emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16);
743 emit_tai(as, PPCI_ADDI, dest, node, ofs);
744 }
745}
746
747static void asm_newref(ASMState *as, IRIns *ir)
748{
749 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey];
750 IRRef args[3];
751 args[0] = ASMREF_L; /* lua_State *L */
752 args[1] = ir->op1; /* GCtab *t */
753 args[2] = ASMREF_TMP1; /* cTValue *key */
754 asm_setupresult(as, ir, ci); /* TValue * */
755 asm_gencall(as, ci, args);
756 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2);
757}
758
759static void asm_uref(ASMState *as, IRIns *ir)
760{
761 /* NYI: Check that UREFO is still open and not aliasing a slot. */
762 Reg dest = ra_dest(as, ir, RSET_GPR);
763 if (irref_isk(ir->op1)) {
764 GCfunc *fn = ir_kfunc(IR(ir->op1));
765 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
766 emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR);
767 } else {
768 Reg uv = ra_scratch(as, RSET_GPR);
769 Reg func = ra_alloc1(as, ir->op1, RSET_GPR);
770 if (ir->o == IR_UREFC) {
771 asm_guardcc(as, CC_NE);
772 emit_ai(as, PPCI_CMPWI, RID_TMP, 1);
773 emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv));
774 emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));
775 } else {
776 emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v));
777 }
778 emit_tai(as, PPCI_LWZ, uv, func,
779 (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));
780 }
781}
782
783static void asm_fref(ASMState *as, IRIns *ir)
784{
785 UNUSED(as); UNUSED(ir);
786 lua_assert(!ra_used(ir));
787}
788
789static void asm_strref(ASMState *as, IRIns *ir)
790{
791 Reg dest = ra_dest(as, ir, RSET_GPR);
792 IRRef ref = ir->op2, refk = ir->op1;
793 int32_t ofs = (int32_t)sizeof(GCstr);
794 Reg r;
795 if (irref_isk(ref)) {
796 IRRef tmp = refk; refk = ref; ref = tmp;
797 } else if (!irref_isk(refk)) {
798 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
799 IRIns *irr = IR(ir->op2);
800 if (ra_hasreg(irr->r)) {
801 ra_noweak(as, irr->r);
802 right = irr->r;
803 } else if (mayfuse(as, irr->op2) &&
804 irr->o == IR_ADD && irref_isk(irr->op2) &&
805 checki16(ofs + IR(irr->op2)->i)) {
806 ofs += IR(irr->op2)->i;
807 right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
808 } else {
809 right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
810 }
811 emit_tai(as, PPCI_ADDI, dest, dest, ofs);
812 emit_tab(as, PPCI_ADD, dest, left, right);
813 return;
814 }
815 r = ra_alloc1(as, ref, RSET_GPR);
816 ofs += IR(refk)->i;
817 if (checki16(ofs))
818 emit_tai(as, PPCI_ADDI, dest, r, ofs);
819 else
820 emit_tab(as, PPCI_ADD, dest, r,
821 ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
822}
823
824/* -- Loads and stores ---------------------------------------------------- */
825
826static PPCIns asm_fxloadins(IRIns *ir)
827{
828 switch (irt_type(ir->t)) {
829 case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */
830 case IRT_U8: return PPCI_LBZ;
831 case IRT_I16: return PPCI_LHA;
832 case IRT_U16: return PPCI_LHZ;
833 case IRT_NUM: return PPCI_LFD;
834 case IRT_FLOAT: return PPCI_LFS;
835 default: return PPCI_LWZ;
836 }
837}
838
839static PPCIns asm_fxstoreins(IRIns *ir)
840{
841 switch (irt_type(ir->t)) {
842 case IRT_I8: case IRT_U8: return PPCI_STB;
843 case IRT_I16: case IRT_U16: return PPCI_STH;
844 case IRT_NUM: return PPCI_STFD;
845 case IRT_FLOAT: return PPCI_STFS;
846 default: return PPCI_STW;
847 }
848}
849
850static void asm_fload(ASMState *as, IRIns *ir)
851{
852 Reg dest = ra_dest(as, ir, RSET_GPR);
853 Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);
854 PPCIns pi = asm_fxloadins(ir);
855 int32_t ofs;
856 if (ir->op2 == IRFL_TAB_ARRAY) {
857 ofs = asm_fuseabase(as, ir->op1);
858 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
859 emit_tai(as, PPCI_ADDI, dest, idx, ofs);
860 return;
861 }
862 }
863 ofs = field_ofs[ir->op2];
864 lua_assert(!irt_isi8(ir->t));
865 emit_tai(as, pi, dest, idx, ofs);
866}
867
868static void asm_fstore(ASMState *as, IRIns *ir)
869{
870 Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
871 IRIns *irf = IR(ir->op1);
872 Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
873 int32_t ofs = field_ofs[irf->op2];
874 PPCIns pi = asm_fxstoreins(ir);
875 emit_tai(as, pi, src, idx, ofs);
876}
877
878static void asm_xload(ASMState *as, IRIns *ir)
879{
880 Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
881 lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));
882 if (irt_isi8(ir->t))
883 emit_as(as, PPCI_EXTSB, dest, dest);
884 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR);
885}
886
887static void asm_xstore(ASMState *as, IRIns *ir)
888{
889 // NYI: fuse with bswap to stwbrx.
890 Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
891 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
892 rset_exclude(RSET_GPR, src));
893}
894
895static void asm_ahuvload(ASMState *as, IRIns *ir)
896{
897 IRType1 t = ir->t;
898 Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx;
899 RegSet allow = RSET_GPR;
900 int32_t ofs = AHUREF_LSX;
901 if (ra_used(ir)) {
902 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
903 if (!irt_isnum(t)) ofs = 0;
904 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
905 rset_clear(allow, dest);
906 }
907 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
908 if (irt_isnum(t)) {
909 Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx));
910 asm_guardcc(as, CC_GE);
911 emit_ab(as, PPCI_CMPLW, type, tisnum);
912 if (ra_hasreg(dest)) {
913 if (ofs == AHUREF_LSX) {
914 tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR,
915 (idx&255)), (idx>>8)));
916 emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp);
917 } else {
918 emit_fai(as, PPCI_LFD, dest, idx, ofs);
919 }
920 }
921 } else {
922 asm_guardcc(as, CC_NE);
923 emit_ai(as, PPCI_CMPWI, type, irt_toitype(t));
924 if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4);
925 }
926 if (ofs == AHUREF_LSX) {
927 emit_tab(as, PPCI_LWZX, type, (idx&255), tmp);
928 emit_slwi(as, tmp, (idx>>8), 3);
929 } else {
930 emit_tai(as, PPCI_LWZ, type, idx, ofs);
931 }
932}
933
934static void asm_ahustore(ASMState *as, IRIns *ir)
935{
936 RegSet allow = RSET_GPR;
937 Reg idx, src = RID_NONE, type = RID_NONE;
938 int32_t ofs = AHUREF_LSX;
939 if (irt_isnum(ir->t)) {
940 src = ra_alloc1(as, ir->op2, RSET_FPR);
941 } else {
942 if (!irt_ispri(ir->t)) {
943 src = ra_alloc1(as, ir->op2, allow);
944 rset_clear(allow, src);
945 ofs = 0;
946 }
947 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
948 rset_clear(allow, type);
949 }
950 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
951 if (irt_isnum(ir->t)) {
952 if (ofs == AHUREF_LSX) {
953 emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP);
954 emit_slwi(as, RID_TMP, (idx>>8), 3);
955 } else {
956 emit_fai(as, PPCI_STFD, src, idx, ofs);
957 }
958 } else {
959 if (ra_hasreg(src))
960 emit_tai(as, PPCI_STW, src, idx, ofs+4);
961 if (ofs == AHUREF_LSX) {
962 emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP);
963 emit_slwi(as, RID_TMP, (idx>>8), 3);
964 } else {
965 emit_tai(as, PPCI_STW, type, idx, ofs);
966 }
967 }
968}
969
970static void asm_sload(ASMState *as, IRIns *ir)
971{
972 int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4);
973 IRType1 t = ir->t;
974 Reg dest = RID_NONE, type = RID_NONE, base;
975 RegSet allow = RSET_GPR;
976 lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */
977 lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));
978 lua_assert(LJ_DUALNUM ||
979 !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME)));
980 if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
981 dest = ra_scratch(as, RSET_FPR);
982 asm_tointg(as, ir, dest);
983 t.irt = IRT_NUM; /* Continue with a regular number type check. */
984 } else if (ra_used(ir)) {
985 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
986 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
987 rset_clear(allow, dest);
988 base = ra_alloc1(as, REF_BASE, allow);
989 rset_clear(allow, base);
990 if ((ir->op2 & IRSLOAD_CONVERT)) {
991 if (irt_isint(t)) {
992 emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO);
993 dest = ra_scratch(as, RSET_FPR);
994 emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP);
995 emit_fb(as, PPCI_FCTIWZ, dest, dest);
996 t.irt = IRT_NUM; /* Check for original type. */
997 } else {
998 Reg tmp = ra_scratch(as, allow);
999 Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp));
1000 Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
1001 emit_fab(as, PPCI_FSUB, dest, dest, fbias);
1002 emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP);
1003 emit_lsptr(as, PPCI_LFS, (fbias & 31),
1004 (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)),
1005 rset_clear(allow, hibias));
1006 emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO);
1007 emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI);
1008 emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000);
1009 dest = tmp;
1010 t.irt = IRT_INT; /* Check for original type. */
1011 }
1012 }
1013 goto dotypecheck;
1014 }
1015 base = ra_alloc1(as, REF_BASE, allow);
1016 rset_clear(allow, base);
1017dotypecheck:
1018 if (irt_isnum(t)) {
1019 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1020 Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow);
1021 asm_guardcc(as, CC_GE);
1022 emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum);
1023 type = RID_TMP;
1024 }
1025 if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4);
1026 } else {
1027 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1028 asm_guardcc(as, CC_NE);
1029 emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t));
1030 type = RID_TMP;
1031 }
1032 if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs);
1033 }
1034 if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4);
1035}
1036
1037/* -- Allocations --------------------------------------------------------- */
1038
1039#if LJ_HASFFI
1040static void asm_cnew(ASMState *as, IRIns *ir)
1041{
1042 CTState *cts = ctype_ctsG(J2G(as->J));
1043 CTypeID typeid = (CTypeID)IR(ir->op1)->i;
1044 CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ?
1045 lj_ctype_size(cts, typeid) : (CTSize)IR(ir->op2)->i;
1046 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
1047 IRRef args[2];
1048 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1049 RegSet drop = RSET_SCRATCH;
1050 lua_assert(sz != CTSIZE_INVALID);
1051
1052 args[0] = ASMREF_L; /* lua_State *L */
1053 args[1] = ASMREF_TMP1; /* MSize size */
1054 as->gcsteps++;
1055
1056 if (ra_hasreg(ir->r))
1057 rset_clear(drop, ir->r); /* Dest reg handled below. */
1058 ra_evictset(as, drop);
1059 if (ra_used(ir))
1060 ra_destreg(as, ir, RID_RET); /* GCcdata * */
1061
1062 /* Initialize immutable cdata object. */
1063 if (ir->o == IR_CNEWI) {
1064 int32_t ofs = sizeof(GCcdata);
1065 lua_assert(sz == 4 || sz == 8);
1066 if (sz == 8) {
1067 ofs += 4;
1068 lua_assert((ir+1)->o == IR_HIOP);
1069 }
1070 for (;;) {
1071 Reg r = ra_alloc1(as, ir->op2, allow);
1072 emit_tai(as, PPCI_STW, r, RID_RET, ofs);
1073 rset_clear(allow, r);
1074 if (ofs == sizeof(GCcdata)) break;
1075 ofs -= 4; ir++;
1076 }
1077 }
1078 /* Initialize gct and typeid. lj_mem_newgco() already sets marked. */
1079 emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
1080 emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, typeid));
1081 emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA);
1082 emit_ti(as, PPCI_LI, RID_TMP, typeid); /* Lower 16 bit used. Sign-ext ok. */
1083 asm_gencall(as, ci, args);
1084 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
1085 ra_releasetmp(as, ASMREF_TMP1));
1086}
1087#else
1088#define asm_cnew(as, ir) ((void)0)
1089#endif
1090
1091/* -- Write barriers ------------------------------------------------------ */
1092
1093static void asm_tbar(ASMState *as, IRIns *ir)
1094{
1095 Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
1096 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
1097 Reg link = RID_TMP;
1098 MCLabel l_end = emit_label(as);
1099 emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist));
1100 emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked));
1101 emit_setgl(as, tab, gc.grayagain);
1102 lua_assert(LJ_GC_BLACK == 0x04);
1103 emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */
1104 emit_getgl(as, link, gc.grayagain);
1105 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
1106 emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK);
1107 emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked));
1108}
1109
1110static void asm_obar(ASMState *as, IRIns *ir)
1111{
1112 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
1113 IRRef args[2];
1114 MCLabel l_end;
1115 Reg obj, val, tmp;
1116 /* No need for other object barriers (yet). */
1117 lua_assert(IR(ir->op1)->o == IR_UREFC);
1118 ra_evictset(as, RSET_SCRATCH);
1119 l_end = emit_label(as);
1120 args[0] = ASMREF_TMP1; /* global_State *g */
1121 args[1] = ir->op1; /* TValue *tv */
1122 asm_gencall(as, ci, args);
1123 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1124 obj = IR(ir->op1)->r;
1125 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
1126 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end);
1127 emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK);
1128 emit_condbranch(as, PPCI_BC, CC_EQ, l_end);
1129 emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES);
1130 val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
1131 emit_tai(as, PPCI_LBZ, tmp, obj,
1132 (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
1133 emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked));
1134}
1135
1136/* -- Arithmetic and logic operations ------------------------------------- */
1137
1138static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi)
1139{
1140 Reg dest = ra_dest(as, ir, RSET_FPR);
1141 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1142 right = (left >> 8); left &= 255;
1143 if (pi == PPCI_FMUL)
1144 emit_fac(as, pi, dest, left, right);
1145 else
1146 emit_fab(as, pi, dest, left, right);
1147}
1148
1149static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi)
1150{
1151 Reg dest = ra_dest(as, ir, RSET_FPR);
1152 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
1153 emit_fb(as, pi, dest, left);
1154}
1155
1156static int asm_fpjoin_pow(ASMState *as, IRIns *ir)
1157{
1158 IRIns *irp = IR(ir->op1);
1159 if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) {
1160 IRIns *irpp = IR(irp->op1);
1161 if (irpp == ir-2 && irpp->o == IR_FPMATH &&
1162 irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) {
1163 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow];
1164 IRRef args[2];
1165 args[0] = irpp->op1;
1166 args[1] = irp->op2;
1167 asm_setupresult(as, ir, ci);
1168 asm_gencall(as, ci, args);
1169 return 1;
1170 }
1171 }
1172 return 0;
1173}
1174
1175static void asm_add(ASMState *as, IRIns *ir)
1176{
1177 if (irt_isnum(ir->t)) {
1178 if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD))
1179 asm_fparith(as, ir, PPCI_FADD);
1180 } else {
1181 Reg dest = ra_dest(as, ir, RSET_GPR);
1182 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1183 PPCIns pi;
1184 if (irref_isk(ir->op2)) {
1185 int32_t k = IR(ir->op2)->i;
1186 if (checki16(k)) {
1187 pi = PPCI_ADDI;
1188 /* May fail due to spills/restores above, but simplifies the logic. */
1189 if (as->flagmcp == as->mcp) {
1190 as->flagmcp = NULL;
1191 as->mcp++;
1192 pi = PPCI_ADDICDOT;
1193 }
1194 emit_tai(as, pi, dest, left, k);
1195 return;
1196 } else if ((k & 0xffff) == 0) {
1197 emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16));
1198 return;
1199 } else if (!as->sectref) {
1200 emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16);
1201 emit_tai(as, PPCI_ADDI, dest, left, k);
1202 return;
1203 }
1204 }
1205 pi = PPCI_ADD;
1206 /* May fail due to spills/restores above, but simplifies the logic. */
1207 if (as->flagmcp == as->mcp) {
1208 as->flagmcp = NULL;
1209 as->mcp++;
1210 pi |= PPCF_DOT;
1211 }
1212 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1213 emit_tab(as, pi, dest, left, right);
1214 }
1215}
1216
1217static void asm_sub(ASMState *as, IRIns *ir)
1218{
1219 if (irt_isnum(ir->t)) {
1220 if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB))
1221 asm_fparith(as, ir, PPCI_FSUB);
1222 } else {
1223 PPCIns pi = PPCI_SUBF;
1224 Reg dest = ra_dest(as, ir, RSET_GPR);
1225 Reg left, right;
1226 if (irref_isk(ir->op1)) {
1227 int32_t k = IR(ir->op1)->i;
1228 if (checki16(k)) {
1229 right = ra_alloc1(as, ir->op2, RSET_GPR);
1230 emit_tai(as, PPCI_SUBFIC, dest, right, k);
1231 return;
1232 }
1233 }
1234 /* May fail due to spills/restores above, but simplifies the logic. */
1235 if (as->flagmcp == as->mcp) {
1236 as->flagmcp = NULL;
1237 as->mcp++;
1238 pi |= PPCF_DOT;
1239 }
1240 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1241 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1242 emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */
1243 }
1244}
1245
1246static void asm_mul(ASMState *as, IRIns *ir)
1247{
1248 if (irt_isnum(ir->t)) {
1249 asm_fparith(as, ir, PPCI_FMUL);
1250 } else {
1251 PPCIns pi = PPCI_MULLW;
1252 Reg dest = ra_dest(as, ir, RSET_GPR);
1253 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1254 if (irref_isk(ir->op2)) {
1255 int32_t k = IR(ir->op2)->i;
1256 if (checki16(k)) {
1257 emit_tai(as, PPCI_MULLI, dest, left, k);
1258 return;
1259 }
1260 }
1261 /* May fail due to spills/restores above, but simplifies the logic. */
1262 if (as->flagmcp == as->mcp) {
1263 as->flagmcp = NULL;
1264 as->mcp++;
1265 pi |= PPCF_DOT;
1266 }
1267 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1268 emit_tab(as, pi, dest, left, right);
1269 }
1270}
1271
1272static void asm_neg(ASMState *as, IRIns *ir)
1273{
1274 if (irt_isnum(ir->t)) {
1275 asm_fpunary(as, ir, PPCI_FNEG);
1276 } else {
1277 Reg dest, left;
1278 PPCIns pi = PPCI_NEG;
1279 if (as->flagmcp == as->mcp) {
1280 as->flagmcp = NULL;
1281 as->mcp++;
1282 pi |= PPCF_DOT;
1283 }
1284 dest = ra_dest(as, ir, RSET_GPR);
1285 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1286 emit_tab(as, pi, dest, left, 0);
1287 }
1288}
1289
1290static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi)
1291{
1292 Reg dest, left, right;
1293 if (as->flagmcp == as->mcp) {
1294 as->flagmcp = NULL;
1295 as->mcp++;
1296 }
1297 asm_guardcc(as, CC_SO);
1298 dest = ra_dest(as, ir, RSET_GPR);
1299 left = ra_alloc2(as, ir, RSET_GPR);
1300 right = (left >> 8); left &= 255;
1301 if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; }
1302 emit_tab(as, pi|PPCF_DOT, dest, left, right);
1303}
1304
1305#if LJ_HASFFI
1306static void asm_add64(ASMState *as, IRIns *ir)
1307{
1308 Reg dest = ra_dest(as, ir, RSET_GPR);
1309 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1310 PPCIns pi = PPCI_ADDE;
1311 if (irref_isk(ir->op2)) {
1312 int32_t k = IR(ir->op2)->i;
1313 if (k == 0)
1314 pi = PPCI_ADDZE;
1315 else if (k == -1)
1316 pi = PPCI_ADDME;
1317 else
1318 goto needright;
1319 right = 0;
1320 } else {
1321 needright:
1322 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1323 }
1324 emit_tab(as, pi, dest, left, right);
1325 ir--;
1326 dest = ra_dest(as, ir, RSET_GPR);
1327 left = ra_alloc1(as, ir->op1, RSET_GPR);
1328 if (irref_isk(ir->op2)) {
1329 int32_t k = IR(ir->op2)->i;
1330 if (checki16(k)) {
1331 emit_tai(as, PPCI_ADDIC, dest, left, k);
1332 return;
1333 }
1334 }
1335 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1336 emit_tab(as, PPCI_ADDC, dest, left, right);
1337}
1338
1339static void asm_sub64(ASMState *as, IRIns *ir)
1340{
1341 Reg dest = ra_dest(as, ir, RSET_GPR);
1342 Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR);
1343 PPCIns pi = PPCI_SUBFE;
1344 if (irref_isk(ir->op1)) {
1345 int32_t k = IR(ir->op1)->i;
1346 if (k == 0)
1347 pi = PPCI_SUBFZE;
1348 else if (k == -1)
1349 pi = PPCI_SUBFME;
1350 else
1351 goto needleft;
1352 left = 0;
1353 } else {
1354 needleft:
1355 left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
1356 }
1357 emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */
1358 ir--;
1359 dest = ra_dest(as, ir, RSET_GPR);
1360 right = ra_alloc1(as, ir->op2, RSET_GPR);
1361 if (irref_isk(ir->op1)) {
1362 int32_t k = IR(ir->op1)->i;
1363 if (checki16(k)) {
1364 emit_tai(as, PPCI_SUBFIC, dest, right, k);
1365 return;
1366 }
1367 }
1368 left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right));
1369 emit_tab(as, PPCI_SUBFC, dest, right, left);
1370}
1371
1372static void asm_neg64(ASMState *as, IRIns *ir)
1373{
1374 Reg dest = ra_dest(as, ir, RSET_GPR);
1375 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1376 emit_tab(as, PPCI_SUBFZE, dest, left, 0);
1377 ir--;
1378 dest = ra_dest(as, ir, RSET_GPR);
1379 left = ra_alloc1(as, ir->op1, RSET_GPR);
1380 emit_tai(as, PPCI_SUBFIC, dest, left, 0);
1381}
1382#endif
1383
1384static void asm_bitnot(ASMState *as, IRIns *ir)
1385{
1386 Reg dest, left, right;
1387 PPCIns pi = PPCI_NOR;
1388 if (as->flagmcp == as->mcp) {
1389 as->flagmcp = NULL;
1390 as->mcp++;
1391 pi |= PPCF_DOT;
1392 }
1393 dest = ra_dest(as, ir, RSET_GPR);
1394 if (mayfuse(as, ir->op1)) {
1395 IRIns *irl = IR(ir->op1);
1396 if (irl->o == IR_BAND)
1397 pi ^= (PPCI_NOR ^ PPCI_NAND);
1398 else if (irl->o == IR_BXOR)
1399 pi ^= (PPCI_NOR ^ PPCI_EQV);
1400 else if (irl->o != IR_BOR)
1401 goto nofuse;
1402 left = ra_hintalloc(as, irl->op1, dest, RSET_GPR);
1403 right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left));
1404 } else {
1405nofuse:
1406 left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1407 }
1408 emit_asb(as, pi, dest, left, right);
1409}
1410
1411static void asm_bitswap(ASMState *as, IRIns *ir)
1412{
1413 // NYI: fuse with XLOAD to lwbrx.
1414 Reg dest = ra_dest(as, ir, RSET_GPR);
1415 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1416 Reg tmp = dest;
1417 if (tmp == left) {
1418 tmp = RID_TMP;
1419 emit_mr(as, dest, RID_TMP);
1420 }
1421 emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23);
1422 emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7);
1423 emit_rotlwi(as, tmp, left, 8);
1424}
1425
1426static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
1427{
1428 Reg dest = ra_dest(as, ir, RSET_GPR);
1429 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1430 if (irref_isk(ir->op2)) {
1431 int32_t k = IR(ir->op2)->i;
1432 Reg tmp = left;
1433 if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) {
1434 if (!checku16(k)) {
1435 emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16));
1436 if ((k & 0xffff) == 0) return;
1437 }
1438 emit_asi(as, pik, dest, left, k);
1439 return;
1440 }
1441 }
1442 /* May fail due to spills/restores above, but simplifies the logic. */
1443 if (as->flagmcp == as->mcp) {
1444 as->flagmcp = NULL;
1445 as->mcp++;
1446 pi |= PPCF_DOT;
1447 }
1448 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1449 emit_asb(as, pi, dest, left, right);
1450}
1451
1452static void asm_bitand(ASMState *as, IRIns *ir)
1453{
1454 Reg dest, left, right;
1455 PPCIns dot = 0;
1456 IRRef op2;
1457 if (as->flagmcp == as->mcp) {
1458 as->flagmcp = NULL;
1459 as->mcp++;
1460 dot = PPCF_DOT;
1461 }
1462 dest = ra_dest(as, ir, RSET_GPR);
1463 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1464 if (irref_isk(ir->op2)) {
1465 int32_t k = IR(ir->op2)->i;
1466 if (k) {
1467 // NYI: fuse with shifts/rotates.
1468 uint32_t s1 = lj_ffs((uint32_t)k);
1469 uint32_t k1 = ((uint32_t)k >> s1);
1470 if ((k1 & (k1+1)) == 0) {
1471 emit_rot(as, PPCI_RLWINM|dot, dest, left, 0,
1472 31-lj_fls((uint32_t)k), 31-s1);
1473 return;
1474 }
1475 if (~(uint32_t)k) {
1476 uint32_t s2 = lj_ffs(~(uint32_t)k);
1477 uint32_t k2 = (~(uint32_t)k >> s2);
1478 if ((k2 & (k2+1)) == 0) {
1479 emit_rot(as, PPCI_RLWINM|dot, dest, left, 0,
1480 32-s2, 30-lj_fls(~(uint32_t)k));
1481 return;
1482 }
1483 }
1484 }
1485 if (checku16(k)) {
1486 emit_asi(as, PPCI_ANDIDOT, dest, left, k);
1487 return;
1488 } else if ((k & 0xffff) == 0) {
1489 emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16));
1490 return;
1491 }
1492 }
1493 op2 = ir->op2;
1494 if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT) {
1495 dot ^= (PPCI_AND ^ PPCI_ANDC);
1496 op2 = IR(op2)->op1;
1497 }
1498 right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left));
1499 emit_asb(as, PPCI_AND ^ dot, dest, left, right);
1500}
1501
1502static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik)
1503{
1504 // NYI: fuse with IR_BAND.
1505 Reg dest, left;
1506 Reg dot = 0;
1507 if (as->flagmcp == as->mcp) {
1508 as->flagmcp = NULL;
1509 as->mcp++;
1510 dot = PPCF_DOT;
1511 }
1512 dest = ra_dest(as, ir, RSET_GPR);
1513 left = ra_alloc1(as, ir->op1, RSET_GPR);
1514 if (irref_isk(ir->op2)) { /* Constant shifts. */
1515 int32_t shift = (IR(ir->op2)->i & 31);
1516 if (pik == 0) /* SLWI */
1517 emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift);
1518 else if (pik == 1) /* SRWI */
1519 emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31);
1520 else
1521 emit_asb(as, pik|dot, dest, left, shift);
1522 } else {
1523 Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1524 emit_asb(as, pi|dot, dest, left, right);
1525 }
1526}
1527
1528static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1529{
1530 if (irt_isnum(ir->t)) {
1531 Reg dest = ra_dest(as, ir, RSET_FPR);
1532 Reg tmp = dest;
1533 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1534 right = (left >> 8); left &= 255;
1535 if (tmp == left || tmp == right)
1536 tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR,
1537 dest), left), right));
1538 emit_facb(as, PPCI_FSEL, dest, tmp,
1539 ismax ? left : right, ismax ? right : left);
1540 emit_fab(as, PPCI_FSUB, tmp, left, right);
1541 } else {
1542 Reg dest = ra_dest(as, ir, RSET_GPR);
1543 Reg tmp1 = RID_TMP, tmp2 = dest;
1544 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1545 right = (left >> 8); left &= 255;
1546 if (tmp2 == left || tmp2 == right)
1547 tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR,
1548 dest), left), right));
1549 emit_tab(as, PPCI_ADD, dest, tmp2, right);
1550 emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1);
1551 emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1);
1552 emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1);
1553 emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000);
1554 emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000);
1555 }
1556}
1557
1558/* -- Comparisons --------------------------------------------------------- */
1559
1560#define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */
1561#define CC_TWO 0x80 /* Check two flags for FP comparison. */
1562
1563/* Map of comparisons to flags. ORDER IR. */
1564static const uint8_t asm_compmap[IR_ABC+1] = {
1565 /* op int cc FP cc */
1566 /* LT */ CC_GE + (CC_GE<<4),
1567 /* GE */ CC_LT + (CC_LE<<4) + CC_TWO,
1568 /* LE */ CC_GT + (CC_GE<<4) + CC_TWO,
1569 /* GT */ CC_LE + (CC_LE<<4),
1570 /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO,
1571 /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4),
1572 /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4),
1573 /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO,
1574 /* EQ */ CC_NE + (CC_NE<<4),
1575 /* NE */ CC_EQ + (CC_EQ<<4),
1576 /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */
1577};
1578
1579static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc)
1580{
1581 Reg right, left = ra_alloc1(as, lref, RSET_GPR);
1582 if (irref_isk(rref)) {
1583 int32_t k = IR(rref)->i;
1584 if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */
1585 if (checki16(k)) {
1586 emit_tai(as, PPCI_CMPWI, cr, left, k);
1587 /* Signed comparison with zero and referencing previous ins? */
1588 if (k == 0 && lref == as->curins-1)
1589 as->flagmcp = as->mcp; /* Allow elimination of the compare. */
1590 return;
1591 } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */
1592 if (checku16(k)) {
1593 emit_tai(as, PPCI_CMPLWI, cr, left, k);
1594 return;
1595 } else if (!as->sectref && ra_noreg(IR(rref)->r)) {
1596 emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k);
1597 emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16));
1598 return;
1599 }
1600 }
1601 } else { /* Unsigned comparison with constant. */
1602 if (checku16(k)) {
1603 emit_tai(as, PPCI_CMPLWI, cr, left, k);
1604 return;
1605 }
1606 }
1607 }
1608 right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left));
1609 emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right);
1610}
1611
1612static void asm_comp(ASMState *as, IRIns *ir)
1613{
1614 PPCCC cc = asm_compmap[ir->o];
1615 if (irt_isnum(ir->t)) {
1616 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1617 right = (left >> 8); left &= 255;
1618 asm_guardcc(as, (cc >> 4));
1619 if ((cc & CC_TWO))
1620 emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3));
1621 emit_fab(as, PPCI_FCMPU, 0, left, right);
1622 } else {
1623 IRRef lref = ir->op1, rref = ir->op2;
1624 if (irref_isk(lref) && !irref_isk(rref)) {
1625 /* Swap constants to the right (only for ABC). */
1626 IRRef tmp = lref; lref = rref; rref = tmp;
1627 if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */
1628 }
1629 asm_guardcc(as, cc);
1630 asm_intcomp_(as, lref, rref, 0, cc);
1631 }
1632}
1633
1634#if LJ_HASFFI
1635/* 64 bit integer comparisons. */
1636static void asm_comp64(ASMState *as, IRIns *ir)
1637{
1638 PPCCC cc = asm_compmap[(ir-1)->o];
1639 if ((cc&3) == (CC_EQ&3)) {
1640 asm_guardcc(as, cc);
1641 emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR,
1642 (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3));
1643 } else {
1644 asm_guardcc(as, CC_EQ);
1645 emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1));
1646 emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC,
1647 (CC_EQ&3), (CC_EQ&3), 4+(cc&3));
1648 }
1649 /* Loword comparison sets cr1 and is unsigned, except for equality. */
1650 asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4,
1651 cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED));
1652 /* Hiword comparison sets cr0. */
1653 asm_intcomp_(as, ir->op1, ir->op2, 0, cc);
1654 as->flagmcp = NULL; /* Doesn't work here. */
1655}
1656#endif
1657
1658/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */
1659
1660/* Hiword op of a split 64 bit op. Previous op must be the loword op. */
1661static void asm_hiop(ASMState *as, IRIns *ir)
1662{
1663#if LJ_HASFFI
1664 /* HIOP is marked as a store because it needs its own DCE logic. */
1665 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
1666 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
1667 if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
1668 as->curins--; /* Always skip the CONV. */
1669 if (usehi || uselo)
1670 asm_conv64(as, ir);
1671 return;
1672 } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
1673 as->curins--; /* Always skip the loword comparison. */
1674 asm_comp64(as, ir);
1675 return;
1676 }
1677 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
1678 switch ((ir-1)->o) {
1679 case IR_ADD: as->curins--; asm_add64(as, ir); break;
1680 case IR_SUB: as->curins--; asm_sub64(as, ir); break;
1681 case IR_NEG: as->curins--; asm_neg64(as, ir); break;
1682 case IR_CALLN:
1683 case IR_CALLXS:
1684 if (!uselo)
1685 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
1686 break;
1687 case IR_CNEWI:
1688 /* Nothing to do here. Handled by lo op itself. */
1689 break;
1690 default: lua_assert(0); break;
1691 }
1692#else
1693 UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */
1694#endif
1695}
1696
1697/* -- Stack handling ------------------------------------------------------ */
1698
1699/* Check Lua stack size for overflow. Use exit handler as fallback. */
1700static void asm_stack_check(ASMState *as, BCReg topslot,
1701 IRIns *irp, RegSet allow, ExitNo exitno)
1702{
1703 /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */
1704 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
1705 rset_clear(allow, pbase);
1706 tmp = allow ? rset_pickbot(allow) :
1707 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
1708 emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno));
1709 if (allow == RSET_EMPTY) /* Restore temp. register. */
1710 emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW);
1711 else
1712 ra_modified(as, tmp);
1713 emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot));
1714 emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp);
1715 emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack));
1716 if (pbase == RID_TMP)
1717 emit_getgl(as, RID_TMP, jit_base);
1718 emit_getgl(as, tmp, jit_L);
1719 if (allow == RSET_EMPTY) /* Spill temp. register. */
1720 emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW);
1721}
1722
1723/* Restore Lua stack from on-trace state. */
1724static void asm_stack_restore(ASMState *as, SnapShot *snap)
1725{
1726 SnapEntry *map = &as->T->snapmap[snap->mapofs];
1727 MSize n, nent = snap->nent;
1728 SnapEntry *flinks = map + nent + snap->depth;
1729 /* Store the value of all modified slots to the Lua stack. */
1730 for (n = 0; n < nent; n++) {
1731 SnapEntry sn = map[n];
1732 BCReg s = snap_slot(sn);
1733 int32_t ofs = 8*((int32_t)s-1);
1734 IRRef ref = snap_ref(sn);
1735 IRIns *ir = IR(ref);
1736 if ((sn & SNAP_NORESTORE))
1737 continue;
1738 if (irt_isnum(ir->t)) {
1739 Reg src = ra_alloc1(as, ref, RSET_FPR);
1740 emit_fai(as, PPCI_STFD, src, RID_BASE, ofs);
1741 } else {
1742 Reg type;
1743 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
1744 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
1745 if (!irt_ispri(ir->t)) {
1746 Reg src = ra_alloc1(as, ref, allow);
1747 rset_clear(allow, src);
1748 emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4);
1749 }
1750 if ((sn & (SNAP_CONT|SNAP_FRAME))) {
1751 if (s == 0) continue; /* Do not overwrite link to previous frame. */
1752 type = ra_allock(as, (int32_t)(*flinks--), allow);
1753 } else {
1754 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
1755 }
1756 emit_tai(as, PPCI_STW, type, RID_BASE, ofs);
1757 }
1758 checkmclim(as);
1759 }
1760 lua_assert(map + nent == flinks);
1761}
1762
1763/* -- GC handling --------------------------------------------------------- */
1764
1765/* Check GC threshold and do one or more GC steps. */
1766static void asm_gc_check(ASMState *as)
1767{
1768 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
1769 IRRef args[2];
1770 MCLabel l_end;
1771 Reg tmp;
1772 ra_evictset(as, RSET_SCRATCH);
1773 l_end = emit_label(as);
1774 /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
1775 asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */
1776 emit_ai(as, PPCI_CMPWI, RID_RET, 0);
1777 args[0] = ASMREF_TMP1; /* global_State *g */
1778 args[1] = ASMREF_TMP2; /* MSize steps */
1779 asm_gencall(as, ci, args);
1780 emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1781 tmp = ra_releasetmp(as, ASMREF_TMP2);
1782 emit_loadi(as, tmp, (int32_t)as->gcsteps);
1783 /* Jump around GC step if GC total < GC threshold. */
1784 emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end);
1785 emit_ab(as, PPCI_CMPLW, RID_TMP, tmp);
1786 emit_getgl(as, tmp, gc.threshold);
1787 emit_getgl(as, RID_TMP, gc.total);
1788 as->gcsteps = 0;
1789 checkmclim(as);
1790}
1791
1792/* -- Loop handling ------------------------------------------------------- */
1793
1794/* Fixup the loop branch. */
1795static void asm_loop_fixup(ASMState *as)
1796{
1797 MCode *p = as->mctop;
1798 MCode *target = as->mcp;
1799 if (as->loopinv) { /* Inverted loop branch? */
1800 /* asm_guardcc already inverted the cond branch and patched the final b. */
1801 p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2);
1802 } else {
1803 p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
1804 }
1805}
1806
1807/* -- Head of trace ------------------------------------------------------- */
1808
1809/* Coalesce BASE register for a root trace. */
1810static void asm_head_root_base(ASMState *as)
1811{
1812 IRIns *ir = IR(REF_BASE);
1813 Reg r = ir->r;
1814 if (ra_hasreg(r)) {
1815 ra_free(as, r);
1816 if (rset_test(as->modset, r))
1817 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1818 if (r != RID_BASE)
1819 emit_mr(as, r, RID_BASE);
1820 }
1821}
1822
1823/* Coalesce BASE register for a side trace. */
1824static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)
1825{
1826 IRIns *ir = IR(REF_BASE);
1827 Reg r = ir->r;
1828 if (ra_hasreg(r)) {
1829 ra_free(as, r);
1830 if (rset_test(as->modset, r))
1831 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1832 if (irp->r == r) {
1833 rset_clear(allow, r); /* Mark same BASE register as coalesced. */
1834 } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
1835 rset_clear(allow, irp->r);
1836 emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */
1837 } else {
1838 emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
1839 }
1840 }
1841 return allow;
1842}
1843
1844/* -- Tail of trace ------------------------------------------------------- */
1845
1846/* Fixup the tail code. */
1847static void asm_tail_fixup(ASMState *as, TraceNo lnk)
1848{
1849 MCode *p = as->mctop;
1850 MCode *target;
1851 int32_t spadj = as->T->spadjust;
1852 if (spadj == 0) {
1853 *--p = PPCI_NOP;
1854 *--p = PPCI_NOP;
1855 as->mctop = p;
1856 } else {
1857 /* Patch stack adjustment. */
1858 lua_assert(checki16(CFRAME_SIZE+spadj));
1859 p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj);
1860 p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj;
1861 }
1862 /* Patch exit branch. */
1863 target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
1864 p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2);
1865}
1866
1867/* Prepare tail of code. */
1868static void asm_tail_prep(ASMState *as)
1869{
1870 MCode *p = as->mctop - 1; /* Leave room for exit branch. */
1871 if (as->loopref) {
1872 as->invmcp = as->mcp = p;
1873 } else {
1874 as->mcp = p-2; /* Leave room for stack pointer adjustment. */
1875 as->invmcp = NULL;
1876 }
1877}
1878
1879/* -- Instruction dispatch ------------------------------------------------ */
1880
1881/* Assemble a single instruction. */
1882static void asm_ir(ASMState *as, IRIns *ir)
1883{
1884 switch ((IROp)ir->o) {
1885 /* Miscellaneous ops. */
1886 case IR_LOOP: asm_loop(as); break;
1887 case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break;
1888 case IR_USE:
1889 ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break;
1890 case IR_PHI: asm_phi(as, ir); break;
1891 case IR_HIOP: asm_hiop(as, ir); break;
1892
1893 /* Guarded assertions. */
1894 case IR_EQ: case IR_NE:
1895 if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) {
1896 as->curins--;
1897 asm_href(as, ir-1, (IROp)ir->o);
1898 break;
1899 }
1900 /* fallthrough */
1901 case IR_LT: case IR_GE: case IR_LE: case IR_GT:
1902 case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT:
1903 case IR_ABC:
1904 asm_comp(as, ir);
1905 break;
1906
1907 case IR_RETF: asm_retf(as, ir); break;
1908
1909 /* Bit ops. */
1910 case IR_BNOT: asm_bitnot(as, ir); break;
1911 case IR_BSWAP: asm_bitswap(as, ir); break;
1912
1913 case IR_BAND: asm_bitand(as, ir); break;
1914 case IR_BOR: asm_bitop(as, ir, PPCI_OR, PPCI_ORI); break;
1915 case IR_BXOR: asm_bitop(as, ir, PPCI_XOR, PPCI_XORI); break;
1916
1917 case IR_BSHL: asm_bitshift(as, ir, PPCI_SLW, 0); break;
1918 case IR_BSHR: asm_bitshift(as, ir, PPCI_SRW, 1); break;
1919 case IR_BSAR: asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI); break;
1920 case IR_BROL: asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31),
1921 PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31)); break;
1922 case IR_BROR: lua_assert(0); break;
1923
1924 /* Arithmetic ops. */
1925 case IR_ADD: asm_add(as, ir); break;
1926 case IR_SUB: asm_sub(as, ir); break;
1927 case IR_MUL: asm_mul(as, ir); break;
1928 case IR_DIV: asm_fparith(as, ir, PPCI_FDIV); break;
1929 case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break;
1930 case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break;
1931 case IR_NEG: asm_neg(as, ir); break;
1932
1933 case IR_ABS: asm_fpunary(as, ir, PPCI_FABS); break;
1934 case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break;
1935 case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break;
1936 case IR_MIN: asm_min_max(as, ir, 0); break;
1937 case IR_MAX: asm_min_max(as, ir, 1); break;
1938 case IR_FPMATH:
1939 if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
1940 break;
1941 asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
1942 break;
1943
1944 /* Overflow-checking arithmetic ops. */
1945 case IR_ADDOV: asm_arithov(as, ir, PPCI_ADDO); break;
1946 case IR_SUBOV: asm_arithov(as, ir, PPCI_SUBFO); break;
1947 case IR_MULOV: asm_arithov(as, ir, PPCI_MULLWO); break;
1948
1949 /* Memory references. */
1950 case IR_AREF: asm_aref(as, ir); break;
1951 case IR_HREF: asm_href(as, ir, 0); break;
1952 case IR_HREFK: asm_hrefk(as, ir); break;
1953 case IR_NEWREF: asm_newref(as, ir); break;
1954 case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break;
1955 case IR_FREF: asm_fref(as, ir); break;
1956 case IR_STRREF: asm_strref(as, ir); break;
1957
1958 /* Loads and stores. */
1959 case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
1960 asm_ahuvload(as, ir);
1961 break;
1962 case IR_FLOAD: asm_fload(as, ir); break;
1963 case IR_XLOAD: asm_xload(as, ir); break;
1964 case IR_SLOAD: asm_sload(as, ir); break;
1965
1966 case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break;
1967 case IR_FSTORE: asm_fstore(as, ir); break;
1968 case IR_XSTORE: asm_xstore(as, ir); break;
1969
1970 /* Allocations. */
1971 case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break;
1972 case IR_TNEW: asm_tnew(as, ir); break;
1973 case IR_TDUP: asm_tdup(as, ir); break;
1974 case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break;
1975
1976 /* Write barriers. */
1977 case IR_TBAR: asm_tbar(as, ir); break;
1978 case IR_OBAR: asm_obar(as, ir); break;
1979
1980 /* Type conversions. */
1981 case IR_CONV: asm_conv(as, ir); break;
1982 case IR_TOBIT: asm_tobit(as, ir); break;
1983 case IR_TOSTR: asm_tostr(as, ir); break;
1984 case IR_STRTO: asm_strto(as, ir); break;
1985
1986 /* Calls. */
1987 case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break;
1988 case IR_CALLXS: asm_callx(as, ir); break;
1989 case IR_CARG: break;
1990
1991 default:
1992 setintV(&as->J->errinfo, ir->o);
1993 lj_trace_err_info(as->J, LJ_TRERR_NYIIR);
1994 break;
1995 }
1996}
1997
1998/* -- Trace setup --------------------------------------------------------- */
1999
2000/* Ensure there are enough stack slots for call arguments. */
2001static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
2002{
2003 IRRef args[CCI_NARGS_MAX];
2004 uint32_t i, nargs = (int)CCI_NARGS(ci);
2005 int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
2006 asm_collectargs(as, ir, ci, args);
2007 for (i = 0; i < nargs; i++)
2008 if (args[i] && irt_isfp(IR(args[i])->t)) {
2009 if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1;
2010 } else {
2011 if (ngpr > 0) ngpr--; else nslots++;
2012 }
2013 if (nslots > as->evenspill) /* Leave room for args in stack slots. */
2014 as->evenspill = nslots;
2015 return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
2016}
2017
2018static void asm_setup_target(ASMState *as)
2019{
2020 asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
2021}
2022
2023/* -- Trace patching ------------------------------------------------------ */
2024
2025/* Patch exit jumps of existing machine code to a new target. */
2026void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
2027{
2028 MCode *p = T->mcode;
2029 MCode *pe = (MCode *)((char *)p + T->szmcode);
2030 MCode *px = exitstub_trace_addr(T, exitno);
2031 MCode *cstart = NULL;
2032 MCode *mcarea = lj_mcode_patch(J, p, 0);
2033 int clearso = 0;
2034 for (; p < pe; p++) {
2035 /* Look for exitstub branch, try to replace with branch to target. */
2036 uint32_t ins = *p;
2037 if ((ins & 0xfc000000u) == 0x40000000u &&
2038 ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) {
2039 ptrdiff_t delta = (char *)target - (char *)p;
2040 if (((ins >> 16) & 3) == (CC_SO&3)) {
2041 clearso = sizeof(MCode);
2042 delta -= sizeof(MCode);
2043 }
2044 /* Many, but not all short-range branches can be patched directly. */
2045 if (((delta + 0x8000) >> 16) == 0) {
2046 *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) |
2047 ((delta & 0x8000) * (PPCF_Y/0x8000));
2048 if (!cstart) cstart = p;
2049 }
2050 } else if ((ins & 0xfc000000u) == PPCI_B &&
2051 ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) {
2052 ptrdiff_t delta = (char *)target - (char *)p;
2053 lua_assert(((delta + 0x02000000) >> 26) == 0);
2054 *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
2055 if (!cstart) cstart = p;
2056 }
2057 }
2058 { /* Always patch long-range branch in exit stub itself. */
2059 ptrdiff_t delta = (char *)target - (char *)px - clearso;
2060 lua_assert(((delta + 0x02000000) >> 26) == 0);
2061 *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
2062 }
2063 if (!cstart) cstart = px;
2064 asm_cache_flush(cstart, px+1);
2065 if (clearso) { /* Extend the current trace. Ugly workaround. */
2066 MCode *pp = J->cur.mcode;
2067 J->cur.szmcode += sizeof(MCode);
2068 *--pp = PPCI_MCRXR; /* Clear SO flag. */
2069 J->cur.mcode = pp;
2070 asm_cache_flush(pp, pp+1);
2071 }
2072 lj_mcode_patch(J, mcarea, 1);
2073}
2074