MIPS: Fix calls to floor/ceil/trunc.
[luajit-2.0/celess22.git] / src / lj_asm_mips.h
blob299b4439c5c462df4e0dcdbfd58e9fc378859f53
1 /*
2 ** MIPS IR assembler (SSA IR -> machine code).
3 ** Copyright (C) 2005-2012 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 a register or RID_ZERO. */
22 static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow)
24 Reg r = IR(ref)->r;
25 if (ra_noreg(r)) {
26 if (!(allow & RSET_FPR) && irref_isk(ref) && IR(ref)->i == 0)
27 return RID_ZERO;
28 r = ra_allocref(as, ref, allow);
29 } else {
30 ra_noweak(as, r);
32 return r;
35 /* Allocate two source registers for three-operand instructions. */
36 static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
38 IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
39 Reg left = irl->r, right = irr->r;
40 if (ra_hasreg(left)) {
41 ra_noweak(as, left);
42 if (ra_noreg(right))
43 right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
44 else
45 ra_noweak(as, right);
46 } else if (ra_hasreg(right)) {
47 ra_noweak(as, right);
48 left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
49 } else if (ra_hashint(right)) {
50 right = ra_alloc1z(as, ir->op2, allow);
51 left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
52 } else {
53 left = ra_alloc1z(as, ir->op1, allow);
54 right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
56 return left | (right << 8);
59 /* -- Guard handling ------------------------------------------------------ */
61 /* Need some spare long-range jump slots, for out-of-range branches. */
62 #define MIPS_SPAREJUMP 4
64 /* Setup spare long-range jump slots per mcarea. */
65 static void asm_sparejump_setup(ASMState *as)
67 MCode *mxp = as->mcbot;
68 /* Assumes sizeof(MCLink) == 8. */
69 if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == 8) {
70 lua_assert(MIPSI_NOP == 0);
71 memset(mxp+2, 0, MIPS_SPAREJUMP*8);
72 mxp += MIPS_SPAREJUMP*2;
73 lua_assert(mxp < as->mctop);
74 lj_mcode_commitbot(as->J, mxp);
75 as->mcbot = mxp;
76 as->mclim = as->mcbot + MCLIM_REDZONE;
80 /* Setup exit stub after the end of each trace. */
81 static void asm_exitstub_setup(ASMState *as)
83 MCode *mxp = as->mctop;
84 /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */
85 *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno;
86 *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu);
87 lua_assert(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0);
88 *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0;
89 as->mctop = mxp;
92 /* Keep this in-sync with exitstub_trace_addr(). */
93 #define asm_exitstub_addr(as) ((as)->mctop)
95 /* Emit conditional branch to exit for guard. */
96 static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt)
98 MCode *target = asm_exitstub_addr(as);
99 MCode *p = as->mcp;
100 if (LJ_UNLIKELY(p == as->invmcp)) {
101 as->invmcp = NULL;
102 as->loopinv = 1;
103 as->mcp = p+1;
104 mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u); /* Invert cond. */
105 target = p; /* Patch target later in asm_loop_fixup. */
107 emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);
108 emit_branch(as, mi, rs, rt, target);
111 /* -- Operand fusion ------------------------------------------------------ */
113 /* Limit linear search to this distance. Avoids O(n^2) behavior. */
114 #define CONFLICT_SEARCH_LIM 31
116 /* Check if there's no conflicting instruction between curins and ref. */
117 static int noconflict(ASMState *as, IRRef ref, IROp conflict)
119 IRIns *ir = as->ir;
120 IRRef i = as->curins;
121 if (i > ref + CONFLICT_SEARCH_LIM)
122 return 0; /* Give up, ref is too far away. */
123 while (--i > ref)
124 if (ir[i].o == conflict)
125 return 0; /* Conflict found. */
126 return 1; /* Ok, no conflict. */
129 /* Fuse the array base of colocated arrays. */
130 static int32_t asm_fuseabase(ASMState *as, IRRef ref)
132 IRIns *ir = IR(ref);
133 if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
134 !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
135 return (int32_t)sizeof(GCtab);
136 return 0;
139 /* Fuse array/hash/upvalue reference into register+offset operand. */
140 static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
142 IRIns *ir = IR(ref);
143 if (ra_noreg(ir->r)) {
144 if (ir->o == IR_AREF) {
145 if (mayfuse(as, ref)) {
146 if (irref_isk(ir->op2)) {
147 IRRef tab = IR(ir->op1)->op1;
148 int32_t ofs = asm_fuseabase(as, tab);
149 IRRef refa = ofs ? tab : ir->op1;
150 ofs += 8*IR(ir->op2)->i;
151 if (checki16(ofs)) {
152 *ofsp = ofs;
153 return ra_alloc1(as, refa, allow);
157 } else if (ir->o == IR_HREFK) {
158 if (mayfuse(as, ref)) {
159 int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
160 if (checki16(ofs)) {
161 *ofsp = ofs;
162 return ra_alloc1(as, ir->op1, allow);
165 } else if (ir->o == IR_UREFC) {
166 if (irref_isk(ir->op1)) {
167 GCfunc *fn = ir_kfunc(IR(ir->op1));
168 int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);
169 int32_t jgl = (intptr_t)J2G(as->J);
170 if ((uint32_t)(ofs-jgl) < 65536) {
171 *ofsp = ofs-jgl-32768;
172 return RID_JGL;
173 } else {
174 *ofsp = (int16_t)ofs;
175 return ra_allock(as, ofs-(int16_t)ofs, allow);
180 *ofsp = 0;
181 return ra_alloc1(as, ref, allow);
184 /* Fuse XLOAD/XSTORE reference into load/store operand. */
185 static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,
186 RegSet allow, int32_t ofs)
188 IRIns *ir = IR(ref);
189 Reg base;
190 if (ra_noreg(ir->r) && mayfuse(as, ref)) {
191 if (ir->o == IR_ADD) {
192 int32_t ofs2;
193 if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) {
194 ref = ir->op1;
195 ofs = ofs2;
197 } else if (ir->o == IR_STRREF) {
198 int32_t ofs2 = 65536;
199 lua_assert(ofs == 0);
200 ofs = (int32_t)sizeof(GCstr);
201 if (irref_isk(ir->op2)) {
202 ofs2 = ofs + IR(ir->op2)->i;
203 ref = ir->op1;
204 } else if (irref_isk(ir->op1)) {
205 ofs2 = ofs + IR(ir->op1)->i;
206 ref = ir->op2;
208 if (!checki16(ofs2)) {
209 /* NYI: Fuse ADD with constant. */
210 Reg right, left = ra_alloc2(as, ir, allow);
211 right = (left >> 8); left &= 255;
212 emit_hsi(as, mi, rt, RID_TMP, ofs);
213 emit_dst(as, MIPSI_ADDU, RID_TMP, left, right);
214 return;
216 ofs = ofs2;
219 base = ra_alloc1(as, ref, allow);
220 emit_hsi(as, mi, rt, base, ofs);
223 /* -- Calls --------------------------------------------------------------- */
225 /* Generate a call to a C function. */
226 static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
228 uint32_t n, nargs = CCI_NARGS(ci);
229 int32_t ofs = 16;
230 Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR;
231 if ((void *)ci->func)
232 emit_call(as, (void *)ci->func);
233 for (n = 0; n < nargs; n++) { /* Setup args. */
234 IRRef ref = args[n];
235 if (ref) {
236 IRIns *ir = IR(ref);
237 if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR &&
238 !(ci->flags & CCI_VARARG)) {
239 lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */
240 ra_leftov(as, fpr, ref);
241 fpr += 2;
242 gpr += irt_isnum(ir->t) ? 2 : 1;
243 } else {
244 fpr = REGARG_LASTFPR+1;
245 if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1;
246 if (gpr <= REGARG_LASTGPR) {
247 lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */
248 if (irt_isnum(ir->t)) {
249 Reg r = ra_alloc1(as, ref, RSET_FPR);
250 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1);
251 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r);
252 lua_assert(rset_test(as->freeset, gpr+1)); /* Already evicted. */
253 gpr += 2;
254 } else if (irt_isfloat(ir->t)) {
255 Reg r = ra_alloc1(as, ref, RSET_FPR);
256 emit_tg(as, MIPSI_MFC1, gpr, r);
257 gpr++;
258 } else {
259 ra_leftov(as, gpr, ref);
260 gpr++;
262 } else {
263 Reg r = ra_alloc1z(as, ref, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
264 if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
265 emit_spstore(as, ir, r, ofs);
266 ofs += irt_isnum(ir->t) ? 8 : 4;
269 } else {
270 fpr = REGARG_LASTFPR+1;
271 if (gpr <= REGARG_LASTGPR)
272 gpr++;
273 else
274 ofs += 4;
279 /* Setup result reg/sp for call. Evict scratch regs. */
280 static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
282 RegSet drop = RSET_SCRATCH;
283 int hiop = ((ir+1)->o == IR_HIOP);
284 if ((ci->flags & CCI_NOFPRCLOBBER))
285 drop &= ~RSET_FPR;
286 if (ra_hasreg(ir->r))
287 rset_clear(drop, ir->r); /* Dest reg handled below. */
288 if (hiop && ra_hasreg((ir+1)->r))
289 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
290 ra_evictset(as, drop); /* Evictions must be performed first. */
291 if (ra_used(ir)) {
292 lua_assert(!irt_ispri(ir->t));
293 if (irt_isfp(ir->t)) {
294 if ((ci->flags & CCI_CASTU64)) {
295 int32_t ofs = sps_scale(ir->s);
296 Reg dest = ir->r;
297 if (ra_hasreg(dest)) {
298 ra_free(as, dest);
299 ra_modified(as, dest);
300 emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1);
301 emit_tg(as, MIPSI_MTC1, RID_RETLO, dest);
303 if (ofs) {
304 emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0));
305 emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4));
307 } else {
308 ra_destreg(as, ir, RID_FPRET);
310 } else if (hiop) {
311 ra_destpair(as, ir);
312 } else {
313 ra_destreg(as, ir, RID_RET);
318 static void asm_call(ASMState *as, IRIns *ir)
320 IRRef args[CCI_NARGS_MAX];
321 const CCallInfo *ci = &lj_ir_callinfo[ir->op2];
322 asm_collectargs(as, ir, ci, args);
323 asm_setupresult(as, ir, ci);
324 asm_gencall(as, ci, args);
327 static void asm_callx(ASMState *as, IRIns *ir)
329 IRRef args[CCI_NARGS_MAX];
330 CCallInfo ci;
331 IRRef func;
332 IRIns *irf;
333 ci.flags = asm_callx_flags(as, ir);
334 asm_collectargs(as, ir, &ci, args);
335 asm_setupresult(as, ir, &ci);
336 func = ir->op2; irf = IR(func);
337 if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
338 if (irref_isk(func)) { /* Call to constant address. */
339 ci.func = (ASMFunction)(void *)(irf->i);
340 } else { /* Need specific register for indirect calls. */
341 Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR));
342 MCode *p = as->mcp;
343 if (r == RID_CFUNCADDR)
344 *--p = MIPSI_NOP;
345 else
346 *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r);
347 *--p = MIPSI_JALR | MIPSF_S(r);
348 as->mcp = p;
349 ci.func = (ASMFunction)(void *)0;
351 asm_gencall(as, &ci, args);
354 static void asm_callid(ASMState *as, IRIns *ir, IRCallID id)
356 const CCallInfo *ci = &lj_ir_callinfo[id];
357 IRRef args[2];
358 args[0] = ir->op1;
359 args[1] = ir->op2;
360 asm_setupresult(as, ir, ci);
361 asm_gencall(as, ci, args);
364 static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)
366 /* The modified regs must match with the *.dasc implementation. */
367 RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)|
368 RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR);
369 if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
370 ra_evictset(as, drop);
371 ra_destreg(as, ir, RID_FPRET);
372 emit_call(as, (void *)lj_ir_callinfo[id].func);
373 ra_leftov(as, REGARG_FIRSTFPR, ir->op1);
376 /* -- Returns ------------------------------------------------------------- */
378 /* Return to lower frame. Guard that it goes to the right spot. */
379 static void asm_retf(ASMState *as, IRIns *ir)
381 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
382 void *pc = ir_kptr(IR(ir->op2));
383 int32_t delta = 1+bc_a(*((const BCIns *)pc - 1));
384 as->topslot -= (BCReg)delta;
385 if ((int32_t)as->topslot < 0) as->topslot = 0;
386 emit_setgl(as, base, jit_base);
387 emit_addptr(as, base, -8*delta);
388 asm_guard(as, MIPSI_BNE, RID_TMP,
389 ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));
390 emit_tsi(as, MIPSI_LW, RID_TMP, base, -8);
393 /* -- Type conversions ---------------------------------------------------- */
395 static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
397 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
398 Reg dest = ra_dest(as, ir, RSET_GPR);
399 asm_guard(as, MIPSI_BC1F, 0, 0);
400 emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left);
401 emit_fg(as, MIPSI_CVT_D_W, tmp, tmp);
402 emit_tg(as, MIPSI_MFC1, dest, tmp);
403 emit_fg(as, MIPSI_CVT_W_D, tmp, left);
406 static void asm_tobit(ASMState *as, IRIns *ir)
408 RegSet allow = RSET_FPR;
409 Reg dest = ra_dest(as, ir, RSET_GPR);
410 Reg left = ra_alloc1(as, ir->op1, allow);
411 Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
412 Reg tmp = ra_scratch(as, rset_clear(allow, right));
413 emit_tg(as, MIPSI_MFC1, dest, tmp);
414 emit_fgh(as, MIPSI_ADD_D, tmp, left, right);
417 static void asm_conv(ASMState *as, IRIns *ir)
419 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
420 int stfp = (st == IRT_NUM || st == IRT_FLOAT);
421 IRRef lref = ir->op1;
422 lua_assert(irt_type(ir->t) != st);
423 lua_assert(!(irt_isint64(ir->t) ||
424 (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */
425 if (irt_isfp(ir->t)) {
426 Reg dest = ra_dest(as, ir, RSET_FPR);
427 if (stfp) { /* FP to FP conversion. */
428 emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S,
429 dest, ra_alloc1(as, lref, RSET_FPR));
430 } else if (st == IRT_U32) { /* U32 to FP conversion. */
431 /* y = (x ^ 0x8000000) + 2147483648.0 */
432 Reg left = ra_alloc1(as, lref, RSET_GPR);
433 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest));
434 emit_fgh(as, irt_isfloat(ir->t) ? MIPSI_ADD_S : MIPSI_ADD_D,
435 dest, dest, tmp);
436 emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,
437 dest, dest);
438 if (irt_isfloat(ir->t))
439 emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
440 (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)),
441 RSET_GPR);
442 else
443 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
444 (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)),
445 RSET_GPR);
446 emit_tg(as, MIPSI_MTC1, RID_TMP, dest);
447 emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left);
448 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
449 } else { /* Integer to FP conversion. */
450 Reg left = ra_alloc1(as, lref, RSET_GPR);
451 emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,
452 dest, dest);
453 emit_tg(as, MIPSI_MTC1, left, dest);
455 } else if (stfp) { /* FP to integer conversion. */
456 if (irt_isguard(ir->t)) {
457 /* Checked conversions are only supported from number to int. */
458 lua_assert(irt_isint(ir->t) && st == IRT_NUM);
459 asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
460 } else {
461 Reg dest = ra_dest(as, ir, RSET_GPR);
462 Reg left = ra_alloc1(as, lref, RSET_FPR);
463 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
464 if (irt_isu32(ir->t)) {
465 /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */
466 emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP);
467 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
468 emit_tg(as, MIPSI_MFC1, dest, tmp);
469 emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D,
470 tmp, tmp);
471 emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D,
472 tmp, left, tmp);
473 if (st == IRT_FLOAT)
474 emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
475 (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)),
476 RSET_GPR);
477 else
478 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
479 (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)),
480 RSET_GPR);
481 } else {
482 emit_tg(as, MIPSI_MFC1, dest, tmp);
483 emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D,
484 tmp, left);
487 } else {
488 Reg dest = ra_dest(as, ir, RSET_GPR);
489 if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
490 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
491 lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));
492 if ((ir->op2 & IRCONV_SEXT)) {
493 if ((as->flags & JIT_F_MIPS32R2)) {
494 emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left);
495 } else {
496 uint32_t shift = st == IRT_I8 ? 24 : 16;
497 emit_dta(as, MIPSI_SRA, dest, dest, shift);
498 emit_dta(as, MIPSI_SLL, dest, left, shift);
500 } else {
501 emit_tsi(as, MIPSI_ANDI, dest, left,
502 (int32_t)(st == IRT_U8 ? 0xff : 0xffff));
504 } else { /* 32/64 bit integer conversions. */
505 /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
506 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
511 #if LJ_HASFFI
512 static void asm_conv64(ASMState *as, IRIns *ir)
514 IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);
515 IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);
516 IRCallID id;
517 const CCallInfo *ci;
518 IRRef args[2];
519 args[LJ_BE?0:1] = ir->op1;
520 args[LJ_BE?1:0] = (ir-1)->op1;
521 if (st == IRT_NUM || st == IRT_FLOAT) {
522 id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64);
523 ir--;
524 } else {
525 id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64);
527 ci = &lj_ir_callinfo[id];
528 asm_setupresult(as, ir, ci);
529 asm_gencall(as, ci, args);
531 #endif
533 static void asm_strto(ASMState *as, IRIns *ir)
535 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_tonum];
536 IRRef args[2];
537 RegSet drop = RSET_SCRATCH;
538 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
539 ra_evictset(as, drop);
540 asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */
541 args[0] = ir->op1; /* GCstr *str */
542 args[1] = ASMREF_TMP1; /* TValue *n */
543 asm_gencall(as, ci, args);
544 /* Store the result to the spill slot or temp slots. */
545 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1),
546 RID_SP, sps_scale(ir->s));
549 /* Get pointer to TValue. */
550 static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)
552 IRIns *ir = IR(ref);
553 if (irt_isnum(ir->t)) {
554 if (irref_isk(ref)) /* Use the number constant itself as a TValue. */
555 ra_allockreg(as, i32ptr(ir_knum(ir)), dest);
556 else /* Otherwise force a spill and use the spill slot. */
557 emit_tsi(as, MIPSI_ADDIU, dest, RID_SP, ra_spill(as, ir));
558 } else {
559 /* Otherwise use g->tmptv to hold the TValue. */
560 RegSet allow = rset_exclude(RSET_GPR, dest);
561 Reg type;
562 emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, offsetof(global_State, tmptv)-32768);
563 if (!irt_ispri(ir->t)) {
564 Reg src = ra_alloc1(as, ref, allow);
565 emit_setgl(as, src, tmptv.gcr);
567 type = ra_allock(as, irt_toitype(ir->t), allow);
568 emit_setgl(as, type, tmptv.it);
572 static void asm_tostr(ASMState *as, IRIns *ir)
574 IRRef args[2];
575 args[0] = ASMREF_L;
576 as->gcsteps++;
577 if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) {
578 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum];
579 args[1] = ASMREF_TMP1; /* const lua_Number * */
580 asm_setupresult(as, ir, ci); /* GCstr * */
581 asm_gencall(as, ci, args);
582 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1);
583 } else {
584 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint];
585 args[1] = ir->op1; /* int32_t k */
586 asm_setupresult(as, ir, ci); /* GCstr * */
587 asm_gencall(as, ci, args);
591 /* -- Memory references --------------------------------------------------- */
593 static void asm_aref(ASMState *as, IRIns *ir)
595 Reg dest = ra_dest(as, ir, RSET_GPR);
596 Reg idx, base;
597 if (irref_isk(ir->op2)) {
598 IRRef tab = IR(ir->op1)->op1;
599 int32_t ofs = asm_fuseabase(as, tab);
600 IRRef refa = ofs ? tab : ir->op1;
601 ofs += 8*IR(ir->op2)->i;
602 if (checki16(ofs)) {
603 base = ra_alloc1(as, refa, RSET_GPR);
604 emit_tsi(as, MIPSI_ADDIU, dest, base, ofs);
605 return;
608 base = ra_alloc1(as, ir->op1, RSET_GPR);
609 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
610 emit_dst(as, MIPSI_ADDU, dest, RID_TMP, base);
611 emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3);
614 /* Inlined hash lookup. Specialized for key type and for const keys.
615 ** The equivalent C code is:
616 ** Node *n = hashkey(t, key);
617 ** do {
618 ** if (lj_obj_equal(&n->key, key)) return &n->val;
619 ** } while ((n = nextnode(n)));
620 ** return niltv(L);
622 static void asm_href(ASMState *as, IRIns *ir)
624 RegSet allow = RSET_GPR;
625 int destused = ra_used(ir);
626 Reg dest = ra_dest(as, ir, allow);
627 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
628 Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2;
629 IRRef refkey = ir->op2;
630 IRIns *irkey = IR(refkey);
631 IRType1 kt = irkey->t;
632 uint32_t khash;
633 MCLabel l_end, l_loop, l_next;
635 rset_clear(allow, tab);
636 if (irt_isnum(kt)) {
637 key = ra_alloc1(as, refkey, RSET_FPR);
638 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
639 } else if (!irt_ispri(kt)) {
640 key = ra_alloc1(as, refkey, allow);
641 rset_clear(allow, key);
642 type = ra_allock(as, irt_toitype(irkey->t), allow);
643 rset_clear(allow, type);
645 tmp2 = ra_scratch(as, allow);
646 rset_clear(allow, tmp2);
648 /* Key not found in chain: load niltv. */
649 l_end = emit_label(as);
650 if (destused)
651 emit_loada(as, dest, niltvg(J2G(as->J)));
652 else
653 *--as->mcp = MIPSI_NOP;
654 /* Follow hash chain until the end. */
655 emit_move(as, dest, tmp1);
656 l_loop = --as->mcp;
657 emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, next));
658 l_next = emit_label(as);
660 /* Type and value comparison. */
661 if (irt_isnum(kt)) {
662 emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
663 emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key);
664 emit_tg(as, MIPSI_MFC1, tmp1, key+1);
665 emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next);
666 emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM);
667 emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n));
668 } else {
669 if (irt_ispri(kt)) {
670 emit_branch(as, MIPSI_BEQ, tmp1, type, l_end);
671 } else {
672 emit_branch(as, MIPSI_BEQ, tmp2, key, l_end);
673 emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.gcr));
674 emit_branch(as, MIPSI_BNE, tmp1, type, l_next);
677 emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it));
678 *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu);
680 /* Load main position relative to tab->node into dest. */
681 khash = irref_isk(refkey) ? ir_khash(irkey) : 1;
682 if (khash == 0) {
683 emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node));
684 } else {
685 Reg tmphash = tmp1;
686 if (irref_isk(refkey))
687 tmphash = ra_allock(as, khash, allow);
688 emit_dst(as, MIPSI_ADDU, dest, dest, tmp1);
689 lua_assert(sizeof(Node) == 24);
690 emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1);
691 emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3);
692 emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5);
693 emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash);
694 emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node));
695 emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
696 if (irref_isk(refkey)) {
697 /* Nothing to do. */
698 } else if (irt_isstr(kt)) {
699 emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash));
700 } else { /* Must match with hash*() in lj_tab.c. */
701 emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2);
702 emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31);
703 emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2);
704 emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31);
705 emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest);
706 if (irt_isnum(kt)) {
707 emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);
708 if ((as->flags & JIT_F_MIPS32R2)) {
709 emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);
710 } else {
711 emit_dst(as, MIPSI_OR, dest, dest, tmp1);
712 emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1);
713 emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31);
715 emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);
716 emit_tg(as, MIPSI_MFC1, tmp2, key);
717 emit_tg(as, MIPSI_MFC1, tmp1, key+1);
718 } else {
719 emit_dst(as, MIPSI_XOR, tmp2, key, tmp1);
720 emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31);
721 emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow));
727 static void asm_hrefk(ASMState *as, IRIns *ir)
729 IRIns *kslot = IR(ir->op2);
730 IRIns *irkey = IR(kslot->op1);
731 int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
732 int32_t kofs = ofs + (int32_t)offsetof(Node, key);
733 Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
734 Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
735 Reg key = RID_NONE, type = RID_TMP, idx = node;
736 RegSet allow = rset_exclude(RSET_GPR, node);
737 int32_t lo, hi;
738 lua_assert(ofs % sizeof(Node) == 0);
739 if (ofs > 32736) {
740 idx = dest;
741 rset_clear(allow, dest);
742 kofs = (int32_t)offsetof(Node, key);
743 } else if (ra_hasreg(dest)) {
744 emit_tsi(as, MIPSI_ADDIU, dest, node, ofs);
746 if (!irt_ispri(irkey->t)) {
747 key = ra_scratch(as, allow);
748 rset_clear(allow, key);
750 if (irt_isnum(irkey->t)) {
751 lo = (int32_t)ir_knum(irkey)->u32.lo;
752 hi = (int32_t)ir_knum(irkey)->u32.hi;
753 } else {
754 lo = irkey->i;
755 hi = irt_toitype(irkey->t);
756 if (!ra_hasreg(key))
757 goto nolo;
759 asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO);
760 nolo:
761 asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO);
762 if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0));
763 emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4));
764 if (ofs > 32736)
765 emit_tsi(as, MIPSI_ADDU, dest, node, ra_allock(as, ofs, allow));
768 static void asm_newref(ASMState *as, IRIns *ir)
770 if (ir->r != RID_SINK) {
771 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey];
772 IRRef args[3];
773 args[0] = ASMREF_L; /* lua_State *L */
774 args[1] = ir->op1; /* GCtab *t */
775 args[2] = ASMREF_TMP1; /* cTValue *key */
776 asm_setupresult(as, ir, ci); /* TValue * */
777 asm_gencall(as, ci, args);
778 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2);
782 static void asm_uref(ASMState *as, IRIns *ir)
784 /* NYI: Check that UREFO is still open and not aliasing a slot. */
785 Reg dest = ra_dest(as, ir, RSET_GPR);
786 if (irref_isk(ir->op1)) {
787 GCfunc *fn = ir_kfunc(IR(ir->op1));
788 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
789 emit_lsptr(as, MIPSI_LW, dest, v, RSET_GPR);
790 } else {
791 Reg uv = ra_scratch(as, RSET_GPR);
792 Reg func = ra_alloc1(as, ir->op1, RSET_GPR);
793 if (ir->o == IR_UREFC) {
794 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
795 emit_tsi(as, MIPSI_ADDIU, dest, uv, (int32_t)offsetof(GCupval, tv));
796 emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));
797 } else {
798 emit_tsi(as, MIPSI_LW, dest, uv, (int32_t)offsetof(GCupval, v));
800 emit_tsi(as, MIPSI_LW, uv, func,
801 (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));
805 static void asm_fref(ASMState *as, IRIns *ir)
807 UNUSED(as); UNUSED(ir);
808 lua_assert(!ra_used(ir));
811 static void asm_strref(ASMState *as, IRIns *ir)
813 Reg dest = ra_dest(as, ir, RSET_GPR);
814 IRRef ref = ir->op2, refk = ir->op1;
815 int32_t ofs = (int32_t)sizeof(GCstr);
816 Reg r;
817 if (irref_isk(ref)) {
818 IRRef tmp = refk; refk = ref; ref = tmp;
819 } else if (!irref_isk(refk)) {
820 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
821 IRIns *irr = IR(ir->op2);
822 if (ra_hasreg(irr->r)) {
823 ra_noweak(as, irr->r);
824 right = irr->r;
825 } else if (mayfuse(as, irr->op2) &&
826 irr->o == IR_ADD && irref_isk(irr->op2) &&
827 checki16(ofs + IR(irr->op2)->i)) {
828 ofs += IR(irr->op2)->i;
829 right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
830 } else {
831 right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
833 emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs);
834 emit_dst(as, MIPSI_ADDU, dest, left, right);
835 return;
837 r = ra_alloc1(as, ref, RSET_GPR);
838 ofs += IR(refk)->i;
839 if (checki16(ofs))
840 emit_tsi(as, MIPSI_ADDIU, dest, r, ofs);
841 else
842 emit_dst(as, MIPSI_ADDU, dest, r,
843 ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
846 /* -- Loads and stores ---------------------------------------------------- */
848 static MIPSIns asm_fxloadins(IRIns *ir)
850 switch (irt_type(ir->t)) {
851 case IRT_I8: return MIPSI_LB;
852 case IRT_U8: return MIPSI_LBU;
853 case IRT_I16: return MIPSI_LH;
854 case IRT_U16: return MIPSI_LHU;
855 case IRT_NUM: return MIPSI_LDC1;
856 case IRT_FLOAT: return MIPSI_LWC1;
857 default: return MIPSI_LW;
861 static MIPSIns asm_fxstoreins(IRIns *ir)
863 switch (irt_type(ir->t)) {
864 case IRT_I8: case IRT_U8: return MIPSI_SB;
865 case IRT_I16: case IRT_U16: return MIPSI_SH;
866 case IRT_NUM: return MIPSI_SDC1;
867 case IRT_FLOAT: return MIPSI_SWC1;
868 default: return MIPSI_SW;
872 static void asm_fload(ASMState *as, IRIns *ir)
874 Reg dest = ra_dest(as, ir, RSET_GPR);
875 Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);
876 MIPSIns mi = asm_fxloadins(ir);
877 int32_t ofs;
878 if (ir->op2 == IRFL_TAB_ARRAY) {
879 ofs = asm_fuseabase(as, ir->op1);
880 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
881 emit_tsi(as, MIPSI_ADDIU, dest, idx, ofs);
882 return;
885 ofs = field_ofs[ir->op2];
886 lua_assert(!irt_isfp(ir->t));
887 emit_tsi(as, mi, dest, idx, ofs);
890 static void asm_fstore(ASMState *as, IRIns *ir)
892 if (ir->r != RID_SINK) {
893 Reg src = ra_alloc1z(as, ir->op2, RSET_GPR);
894 IRIns *irf = IR(ir->op1);
895 Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
896 int32_t ofs = field_ofs[irf->op2];
897 MIPSIns mi = asm_fxstoreins(ir);
898 lua_assert(!irt_isfp(ir->t));
899 emit_tsi(as, mi, src, idx, ofs);
903 static void asm_xload(ASMState *as, IRIns *ir)
905 Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
906 lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));
907 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);
910 static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs)
912 if (ir->r != RID_SINK) {
913 Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
914 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
915 rset_exclude(RSET_GPR, src), ofs);
919 static void asm_ahuvload(ASMState *as, IRIns *ir)
921 IRType1 t = ir->t;
922 Reg dest = RID_NONE, type = RID_TMP, idx;
923 RegSet allow = RSET_GPR;
924 int32_t ofs = 0;
925 if (ra_used(ir)) {
926 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t));
927 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR);
928 rset_clear(allow, dest);
930 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
931 rset_clear(allow, idx);
932 if (irt_isnum(t)) {
933 asm_guard(as, MIPSI_BEQ, type, RID_ZERO);
934 emit_tsi(as, MIPSI_SLTIU, type, type, (int32_t)LJ_TISNUM);
935 if (ra_hasreg(dest))
936 emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
937 } else {
938 asm_guard(as, MIPSI_BNE, type, ra_allock(as, irt_toitype(t), allow));
939 if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0));
941 emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4));
944 static void asm_ahustore(ASMState *as, IRIns *ir)
946 RegSet allow = RSET_GPR;
947 Reg idx, src = RID_NONE, type = RID_NONE;
948 int32_t ofs = 0;
949 if (ir->r == RID_SINK)
950 return;
951 if (irt_isnum(ir->t)) {
952 src = ra_alloc1(as, ir->op2, RSET_FPR);
953 } else {
954 if (!irt_ispri(ir->t)) {
955 src = ra_alloc1(as, ir->op2, allow);
956 rset_clear(allow, src);
958 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
959 rset_clear(allow, type);
961 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
962 if (irt_isnum(ir->t)) {
963 emit_hsi(as, MIPSI_SDC1, src, idx, ofs);
964 } else {
965 if (ra_hasreg(src))
966 emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0));
967 emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4));
971 static void asm_sload(ASMState *as, IRIns *ir)
973 int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
974 IRType1 t = ir->t;
975 Reg dest = RID_NONE, type = RID_NONE, base;
976 RegSet allow = RSET_GPR;
977 lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */
978 lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK));
979 lua_assert(!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 Reg tmp = ra_scratch(as, RSET_FPR);
993 emit_tg(as, MIPSI_MFC1, dest, tmp);
994 emit_fg(as, MIPSI_CVT_W_D, tmp, tmp);
995 dest = tmp;
996 t.irt = IRT_NUM; /* Check for original type. */
997 } else {
998 Reg tmp = ra_scratch(as, RSET_GPR);
999 emit_fg(as, MIPSI_CVT_D_W, dest, dest);
1000 emit_tg(as, MIPSI_MTC1, tmp, dest);
1001 dest = tmp;
1002 t.irt = IRT_INT; /* Check for original type. */
1005 goto dotypecheck;
1007 base = ra_alloc1(as, REF_BASE, allow);
1008 rset_clear(allow, base);
1009 dotypecheck:
1010 if (irt_isnum(t)) {
1011 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1012 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
1013 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM);
1014 type = RID_TMP;
1016 if (ra_hasreg(dest)) emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
1017 } else {
1018 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1019 Reg ktype = ra_allock(as, irt_toitype(t), allow);
1020 asm_guard(as, MIPSI_BNE, RID_TMP, ktype);
1021 type = RID_TMP;
1023 if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0));
1025 if (ra_hasreg(type)) emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4));
1028 /* -- Allocations --------------------------------------------------------- */
1030 #if LJ_HASFFI
1031 static void asm_cnew(ASMState *as, IRIns *ir)
1033 CTState *cts = ctype_ctsG(J2G(as->J));
1034 CTypeID ctypeid = (CTypeID)IR(ir->op1)->i;
1035 CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ?
1036 lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i;
1037 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
1038 IRRef args[2];
1039 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1040 RegSet drop = RSET_SCRATCH;
1041 lua_assert(sz != CTSIZE_INVALID);
1043 args[0] = ASMREF_L; /* lua_State *L */
1044 args[1] = ASMREF_TMP1; /* MSize size */
1045 as->gcsteps++;
1047 if (ra_hasreg(ir->r))
1048 rset_clear(drop, ir->r); /* Dest reg handled below. */
1049 ra_evictset(as, drop);
1050 if (ra_used(ir))
1051 ra_destreg(as, ir, RID_RET); /* GCcdata * */
1053 /* Initialize immutable cdata object. */
1054 if (ir->o == IR_CNEWI) {
1055 int32_t ofs = sizeof(GCcdata);
1056 lua_assert(sz == 4 || sz == 8);
1057 if (sz == 8) {
1058 ofs += 4;
1059 lua_assert((ir+1)->o == IR_HIOP);
1060 if (LJ_LE) ir++;
1062 for (;;) {
1063 Reg r = ra_alloc1z(as, ir->op2, allow);
1064 emit_tsi(as, MIPSI_SW, r, RID_RET, ofs);
1065 rset_clear(allow, r);
1066 if (ofs == sizeof(GCcdata)) break;
1067 ofs -= 4; if (LJ_BE) ir++; else ir--;
1070 /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
1071 emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
1072 emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));
1073 emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA);
1074 emit_ti(as, MIPSI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */
1075 asm_gencall(as, ci, args);
1076 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
1077 ra_releasetmp(as, ASMREF_TMP1));
1079 #else
1080 #define asm_cnew(as, ir) ((void)0)
1081 #endif
1083 /* -- Write barriers ------------------------------------------------------ */
1085 static void asm_tbar(ASMState *as, IRIns *ir)
1087 Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
1088 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
1089 Reg link = RID_TMP;
1090 MCLabel l_end = emit_label(as);
1091 emit_tsi(as, MIPSI_SW, link, tab, (int32_t)offsetof(GCtab, gclist));
1092 emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked));
1093 emit_setgl(as, tab, gc.grayagain);
1094 emit_getgl(as, link, gc.grayagain);
1095 emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP); /* Clear black bit. */
1096 emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
1097 emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK);
1098 emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked));
1101 static void asm_obar(ASMState *as, IRIns *ir)
1103 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
1104 IRRef args[2];
1105 MCLabel l_end;
1106 Reg obj, val, tmp;
1107 /* No need for other object barriers (yet). */
1108 lua_assert(IR(ir->op1)->o == IR_UREFC);
1109 ra_evictset(as, RSET_SCRATCH);
1110 l_end = emit_label(as);
1111 args[0] = ASMREF_TMP1; /* global_State *g */
1112 args[1] = ir->op1; /* TValue *tv */
1113 asm_gencall(as, ci, args);
1114 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1115 obj = IR(ir->op1)->r;
1116 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
1117 emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
1118 emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK);
1119 emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
1120 emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES);
1121 val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
1122 emit_tsi(as, MIPSI_LBU, tmp, obj,
1123 (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
1124 emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked));
1127 /* -- Arithmetic and logic operations ------------------------------------- */
1129 static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi)
1131 Reg dest = ra_dest(as, ir, RSET_FPR);
1132 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1133 right = (left >> 8); left &= 255;
1134 emit_fgh(as, mi, dest, left, right);
1137 static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi)
1139 Reg dest = ra_dest(as, ir, RSET_FPR);
1140 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
1141 emit_fg(as, mi, dest, left);
1144 static int asm_fpjoin_pow(ASMState *as, IRIns *ir)
1146 IRIns *irp = IR(ir->op1);
1147 if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) {
1148 IRIns *irpp = IR(irp->op1);
1149 if (irpp == ir-2 && irpp->o == IR_FPMATH &&
1150 irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) {
1151 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow];
1152 IRRef args[2];
1153 args[0] = irpp->op1;
1154 args[1] = irp->op2;
1155 asm_setupresult(as, ir, ci);
1156 asm_gencall(as, ci, args);
1157 return 1;
1160 return 0;
1163 static void asm_add(ASMState *as, IRIns *ir)
1165 if (irt_isnum(ir->t)) {
1166 asm_fparith(as, ir, MIPSI_ADD_D);
1167 } else {
1168 Reg dest = ra_dest(as, ir, RSET_GPR);
1169 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1170 if (irref_isk(ir->op2)) {
1171 int32_t k = IR(ir->op2)->i;
1172 if (checki16(k)) {
1173 emit_tsi(as, MIPSI_ADDIU, dest, left, k);
1174 return;
1177 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1178 emit_dst(as, MIPSI_ADDU, dest, left, right);
1182 static void asm_sub(ASMState *as, IRIns *ir)
1184 if (irt_isnum(ir->t)) {
1185 asm_fparith(as, ir, MIPSI_SUB_D);
1186 } else {
1187 Reg dest = ra_dest(as, ir, RSET_GPR);
1188 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1189 right = (left >> 8); left &= 255;
1190 emit_dst(as, MIPSI_SUBU, dest, left, right);
1194 static void asm_mul(ASMState *as, IRIns *ir)
1196 if (irt_isnum(ir->t)) {
1197 asm_fparith(as, ir, MIPSI_MUL_D);
1198 } else {
1199 Reg dest = ra_dest(as, ir, RSET_GPR);
1200 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1201 right = (left >> 8); left &= 255;
1202 emit_dst(as, MIPSI_MUL, dest, left, right);
1206 static void asm_neg(ASMState *as, IRIns *ir)
1208 if (irt_isnum(ir->t)) {
1209 asm_fpunary(as, ir, MIPSI_NEG_D);
1210 } else {
1211 Reg dest = ra_dest(as, ir, RSET_GPR);
1212 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1213 emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);
1217 static void asm_arithov(ASMState *as, IRIns *ir)
1219 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);
1220 if (irref_isk(ir->op2)) {
1221 int k = IR(ir->op2)->i;
1222 if (ir->o == IR_SUBOV) k = -k;
1223 if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */
1224 left = ra_alloc1(as, ir->op1, RSET_GPR);
1225 asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
1226 emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left);
1227 emit_tsi(as, MIPSI_ADDIU, dest, left, k);
1228 if (dest == left) emit_move(as, RID_TMP, left);
1229 return;
1232 left = ra_alloc2(as, ir, RSET_GPR);
1233 right = (left >> 8); left &= 255;
1234 tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),
1235 right), dest));
1236 asm_guard(as, MIPSI_BLTZ, RID_TMP, 0);
1237 emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp);
1238 if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */
1239 emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right);
1240 } else { /* ((dest^left) & (dest^~right)) < 0 */
1241 emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest);
1242 emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO);
1244 emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left);
1245 emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right);
1246 if (dest == left || dest == right)
1247 emit_move(as, RID_TMP, dest == left ? left : right);
1250 static void asm_mulov(ASMState *as, IRIns *ir)
1252 #if LJ_DUALNUM
1253 #error "NYI: MULOV"
1254 #else
1255 UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused in single-number mode. */
1256 #endif
1259 #if LJ_HASFFI
1260 static void asm_add64(ASMState *as, IRIns *ir)
1262 Reg dest = ra_dest(as, ir, RSET_GPR);
1263 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1264 if (irref_isk(ir->op2)) {
1265 int32_t k = IR(ir->op2)->i;
1266 if (k == 0) {
1267 emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP);
1268 goto loarith;
1269 } else if (checki16(k)) {
1270 emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);
1271 emit_tsi(as, MIPSI_ADDIU, dest, left, k);
1272 goto loarith;
1275 emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);
1276 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1277 emit_dst(as, MIPSI_ADDU, dest, left, right);
1278 loarith:
1279 ir--;
1280 dest = ra_dest(as, ir, RSET_GPR);
1281 left = ra_alloc1(as, ir->op1, RSET_GPR);
1282 if (irref_isk(ir->op2)) {
1283 int32_t k = IR(ir->op2)->i;
1284 if (k == 0) {
1285 if (dest != left)
1286 emit_move(as, dest, left);
1287 return;
1288 } else if (checki16(k)) {
1289 if (dest == left) {
1290 Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left));
1291 emit_move(as, dest, tmp);
1292 dest = tmp;
1294 emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left);
1295 emit_tsi(as, MIPSI_ADDIU, dest, left, k);
1296 return;
1299 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1300 if (dest == left && dest == right) {
1301 Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
1302 emit_move(as, dest, tmp);
1303 dest = tmp;
1305 emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left);
1306 emit_dst(as, MIPSI_ADDU, dest, left, right);
1309 static void asm_sub64(ASMState *as, IRIns *ir)
1311 Reg dest = ra_dest(as, ir, RSET_GPR);
1312 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1313 right = (left >> 8); left &= 255;
1314 emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);
1315 emit_dst(as, MIPSI_SUBU, dest, left, right);
1316 ir--;
1317 dest = ra_dest(as, ir, RSET_GPR);
1318 left = ra_alloc2(as, ir, RSET_GPR);
1319 right = (left >> 8); left &= 255;
1320 if (dest == left) {
1321 Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
1322 emit_move(as, dest, tmp);
1323 dest = tmp;
1325 emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest);
1326 emit_dst(as, MIPSI_SUBU, dest, left, right);
1329 static void asm_neg64(ASMState *as, IRIns *ir)
1331 Reg dest = ra_dest(as, ir, RSET_GPR);
1332 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1333 emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);
1334 emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);
1335 ir--;
1336 dest = ra_dest(as, ir, RSET_GPR);
1337 left = ra_alloc1(as, ir->op1, RSET_GPR);
1338 emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest);
1339 emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);
1341 #endif
1343 static void asm_bitnot(ASMState *as, IRIns *ir)
1345 Reg left, right, dest = ra_dest(as, ir, RSET_GPR);
1346 IRIns *irl = IR(ir->op1);
1347 if (mayfuse(as, ir->op1) && irl->o == IR_BOR) {
1348 left = ra_alloc2(as, irl, RSET_GPR);
1349 right = (left >> 8); left &= 255;
1350 } else {
1351 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1352 right = RID_ZERO;
1354 emit_dst(as, MIPSI_NOR, dest, left, right);
1357 static void asm_bitswap(ASMState *as, IRIns *ir)
1359 Reg dest = ra_dest(as, ir, RSET_GPR);
1360 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1361 if ((as->flags & JIT_F_MIPS32R2)) {
1362 emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);
1363 emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);
1364 } else {
1365 Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest));
1366 emit_dst(as, MIPSI_OR, dest, dest, tmp);
1367 emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);
1368 emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00);
1369 emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8);
1370 emit_dta(as, MIPSI_SRL, dest, left, 8);
1371 emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00);
1372 emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP);
1373 emit_dta(as, MIPSI_SRL, tmp, left, 24);
1374 emit_dta(as, MIPSI_SLL, RID_TMP, left, 24);
1378 static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
1380 Reg dest = ra_dest(as, ir, RSET_GPR);
1381 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1382 if (irref_isk(ir->op2)) {
1383 int32_t k = IR(ir->op2)->i;
1384 if (checku16(k)) {
1385 emit_tsi(as, mik, dest, left, k);
1386 return;
1389 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1390 emit_dst(as, mi, dest, left, right);
1393 static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
1395 Reg dest = ra_dest(as, ir, RSET_GPR);
1396 if (irref_isk(ir->op2)) { /* Constant shifts. */
1397 uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31);
1398 emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), shift);
1399 } else {
1400 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1401 right = (left >> 8); left &= 255;
1402 emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */
1406 static void asm_bitror(ASMState *as, IRIns *ir)
1408 if ((as->flags & JIT_F_MIPS32R2)) {
1409 asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR);
1410 } else {
1411 Reg dest = ra_dest(as, ir, RSET_GPR);
1412 if (irref_isk(ir->op2)) { /* Constant shifts. */
1413 uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31);
1414 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1415 emit_rotr(as, dest, left, RID_TMP, shift);
1416 } else {
1417 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1418 right = (left >> 8); left &= 255;
1419 emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);
1420 emit_dst(as, MIPSI_SRLV, dest, right, left);
1421 emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left);
1422 emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right);
1427 static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1429 if (irt_isnum(ir->t)) {
1430 Reg dest = ra_dest(as, ir, RSET_FPR);
1431 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1432 right = (left >> 8); left &= 255;
1433 if (dest == left) {
1434 emit_fg(as, MIPSI_MOVT_D, dest, right);
1435 } else {
1436 emit_fg(as, MIPSI_MOVF_D, dest, left);
1437 if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right);
1439 emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left);
1440 } else {
1441 Reg dest = ra_dest(as, ir, RSET_GPR);
1442 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1443 right = (left >> 8); left &= 255;
1444 if (dest == left) {
1445 emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP);
1446 } else {
1447 emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP);
1448 if (dest != right) emit_move(as, dest, right);
1450 emit_dst(as, MIPSI_SLT, RID_TMP,
1451 ismax ? left : right, ismax ? right : left);
1455 /* -- Comparisons --------------------------------------------------------- */
1457 static void asm_comp(ASMState *as, IRIns *ir)
1459 /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
1460 IROp op = ir->o;
1461 if (irt_isnum(ir->t)) {
1462 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1463 right = (left >> 8); left &= 255;
1464 asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
1465 emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right);
1466 } else {
1467 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1468 if (op == IR_ABC) op = IR_UGT;
1469 if ((op&4) == 0 && irref_isk(ir->op2) && IR(ir->op2)->i == 0) {
1470 MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) :
1471 ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ);
1472 asm_guard(as, mi, left, 0);
1473 } else {
1474 if (irref_isk(ir->op2)) {
1475 int32_t k = IR(ir->op2)->i;
1476 if ((op&2)) k++;
1477 if (checki16(k)) {
1478 asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
1479 emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI,
1480 RID_TMP, left, k);
1481 return;
1484 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1485 asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
1486 emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT,
1487 RID_TMP, (op&2) ? right : left, (op&2) ? left : right);
1492 static void asm_compeq(ASMState *as, IRIns *ir)
1494 Reg right, left = ra_alloc2(as, ir, irt_isnum(ir->t) ? RSET_FPR : RSET_GPR);
1495 right = (left >> 8); left &= 255;
1496 if (irt_isnum(ir->t)) {
1497 asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
1498 emit_fgh(as, MIPSI_C_EQ_D, 0, left, right);
1499 } else {
1500 asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right);
1504 #if LJ_HASFFI
1505 /* 64 bit integer comparisons. */
1506 static void asm_comp64(ASMState *as, IRIns *ir)
1508 /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
1509 IROp op = (ir-1)->o;
1510 MCLabel l_end;
1511 Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR);
1512 righthi = (lefthi >> 8); lefthi &= 255;
1513 leftlo = ra_alloc2(as, ir-1,
1514 rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi));
1515 rightlo = (leftlo >> 8); leftlo &= 255;
1516 asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
1517 l_end = emit_label(as);
1518 if (lefthi != righthi)
1519 emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP,
1520 (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi);
1521 emit_dst(as, MIPSI_SLTU, RID_TMP,
1522 (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo);
1523 if (lefthi != righthi)
1524 emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end);
1527 static void asm_comp64eq(ASMState *as, IRIns *ir)
1529 Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);
1530 right = (left >> 8); left &= 255;
1531 asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO);
1532 tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
1533 emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp);
1534 emit_dst(as, MIPSI_XOR, tmp, left, right);
1535 left = ra_alloc2(as, ir-1, RSET_GPR);
1536 right = (left >> 8); left &= 255;
1537 emit_dst(as, MIPSI_XOR, RID_TMP, left, right);
1539 #endif
1541 /* -- Support for 64 bit ops in 32 bit mode ------------------------------- */
1543 /* Hiword op of a split 64 bit op. Previous op must be the loword op. */
1544 static void asm_hiop(ASMState *as, IRIns *ir)
1546 #if LJ_HASFFI
1547 /* HIOP is marked as a store because it needs its own DCE logic. */
1548 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
1549 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
1550 if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
1551 as->curins--; /* Always skip the CONV. */
1552 if (usehi || uselo)
1553 asm_conv64(as, ir);
1554 return;
1555 } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */
1556 as->curins--; /* Always skip the loword comparison. */
1557 asm_comp64(as, ir);
1558 return;
1559 } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
1560 as->curins--; /* Always skip the loword comparison. */
1561 asm_comp64eq(as, ir);
1562 return;
1563 } else if ((ir-1)->o == IR_XSTORE) {
1564 as->curins--; /* Handle both stores here. */
1565 if ((ir-1)->r != RID_SINK) {
1566 asm_xstore(as, ir, LJ_LE ? 4 : 0);
1567 asm_xstore(as, ir-1, LJ_LE ? 0 : 4);
1569 return;
1571 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
1572 switch ((ir-1)->o) {
1573 case IR_ADD: as->curins--; asm_add64(as, ir); break;
1574 case IR_SUB: as->curins--; asm_sub64(as, ir); break;
1575 case IR_NEG: as->curins--; asm_neg64(as, ir); break;
1576 case IR_CALLN:
1577 case IR_CALLXS:
1578 if (!uselo)
1579 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
1580 break;
1581 case IR_CNEWI:
1582 /* Nothing to do here. Handled by lo op itself. */
1583 break;
1584 default: lua_assert(0); break;
1586 #else
1587 UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */
1588 #endif
1591 /* -- Stack handling ------------------------------------------------------ */
1593 /* Check Lua stack size for overflow. Use exit handler as fallback. */
1594 static void asm_stack_check(ASMState *as, BCReg topslot,
1595 IRIns *irp, RegSet allow, ExitNo exitno)
1597 /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */
1598 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
1599 ExitNo oldsnap = as->snapno;
1600 rset_clear(allow, pbase);
1601 tmp = allow ? rset_pickbot(allow) :
1602 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
1603 as->snapno = exitno;
1604 asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);
1605 as->snapno = oldsnap;
1606 if (allow == RSET_EMPTY) /* Restore temp. register. */
1607 emit_tsi(as, MIPSI_LW, tmp, RID_SP, 0);
1608 else
1609 ra_modified(as, tmp);
1610 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot));
1611 emit_dst(as, MIPSI_SUBU, RID_TMP, tmp, pbase);
1612 emit_tsi(as, MIPSI_LW, tmp, tmp, offsetof(lua_State, maxstack));
1613 if (pbase == RID_TMP)
1614 emit_getgl(as, RID_TMP, jit_base);
1615 emit_getgl(as, tmp, jit_L);
1616 if (allow == RSET_EMPTY) /* Spill temp. register. */
1617 emit_tsi(as, MIPSI_SW, tmp, RID_SP, 0);
1620 /* Restore Lua stack from on-trace state. */
1621 static void asm_stack_restore(ASMState *as, SnapShot *snap)
1623 SnapEntry *map = &as->T->snapmap[snap->mapofs];
1624 SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];
1625 MSize n, nent = snap->nent;
1626 /* Store the value of all modified slots to the Lua stack. */
1627 for (n = 0; n < nent; n++) {
1628 SnapEntry sn = map[n];
1629 BCReg s = snap_slot(sn);
1630 int32_t ofs = 8*((int32_t)s-1);
1631 IRRef ref = snap_ref(sn);
1632 IRIns *ir = IR(ref);
1633 if ((sn & SNAP_NORESTORE))
1634 continue;
1635 if (irt_isnum(ir->t)) {
1636 Reg src = ra_alloc1(as, ref, RSET_FPR);
1637 emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs);
1638 } else {
1639 Reg type;
1640 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
1641 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
1642 if (!irt_ispri(ir->t)) {
1643 Reg src = ra_alloc1(as, ref, allow);
1644 rset_clear(allow, src);
1645 emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0));
1647 if ((sn & (SNAP_CONT|SNAP_FRAME))) {
1648 if (s == 0) continue; /* Do not overwrite link to previous frame. */
1649 type = ra_allock(as, (int32_t)(*flinks--), allow);
1650 } else {
1651 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
1653 emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4));
1655 checkmclim(as);
1657 lua_assert(map + nent == flinks);
1660 /* -- GC handling --------------------------------------------------------- */
1662 /* Check GC threshold and do one or more GC steps. */
1663 static void asm_gc_check(ASMState *as)
1665 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
1666 IRRef args[2];
1667 MCLabel l_end;
1668 Reg tmp;
1669 ra_evictset(as, RSET_SCRATCH);
1670 l_end = emit_label(as);
1671 /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
1672 /* Assumes asm_snap_prep() already done. */
1673 asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO);
1674 args[0] = ASMREF_TMP1; /* global_State *g */
1675 args[1] = ASMREF_TMP2; /* MSize steps */
1676 asm_gencall(as, ci, args);
1677 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1678 tmp = ra_releasetmp(as, ASMREF_TMP2);
1679 emit_loadi(as, tmp, as->gcsteps);
1680 /* Jump around GC step if GC total < GC threshold. */
1681 emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end);
1682 emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp);
1683 emit_getgl(as, tmp, gc.threshold);
1684 emit_getgl(as, RID_TMP, gc.total);
1685 as->gcsteps = 0;
1686 checkmclim(as);
1689 /* -- Loop handling ------------------------------------------------------- */
1691 /* Fixup the loop branch. */
1692 static void asm_loop_fixup(ASMState *as)
1694 MCode *p = as->mctop;
1695 MCode *target = as->mcp;
1696 p[-1] = MIPSI_NOP;
1697 if (as->loopinv) { /* Inverted loop branch? */
1698 /* asm_guard already inverted the cond branch. Only patch the target. */
1699 p[-3] |= ((target-p+2) & 0x0000ffffu);
1700 } else {
1701 p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
1705 /* -- Head of trace ------------------------------------------------------- */
1707 /* Coalesce BASE register for a root trace. */
1708 static void asm_head_root_base(ASMState *as)
1710 IRIns *ir = IR(REF_BASE);
1711 Reg r = ir->r;
1712 if (as->loopinv) as->mctop--;
1713 if (ra_hasreg(r)) {
1714 ra_free(as, r);
1715 if (rset_test(as->modset, r))
1716 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1717 if (r != RID_BASE)
1718 emit_move(as, r, RID_BASE);
1722 /* Coalesce BASE register for a side trace. */
1723 static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow)
1725 IRIns *ir = IR(REF_BASE);
1726 Reg r = ir->r;
1727 if (as->loopinv) as->mctop--;
1728 if (ra_hasreg(r)) {
1729 ra_free(as, r);
1730 if (rset_test(as->modset, r))
1731 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1732 if (irp->r == r) {
1733 rset_clear(allow, r); /* Mark same BASE register as coalesced. */
1734 } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
1735 rset_clear(allow, irp->r);
1736 emit_move(as, r, irp->r); /* Move from coalesced parent reg. */
1737 } else {
1738 emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
1741 return allow;
1744 /* -- Tail of trace ------------------------------------------------------- */
1746 /* Fixup the tail code. */
1747 static void asm_tail_fixup(ASMState *as, TraceNo lnk)
1749 MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp;
1750 int32_t spadj = as->T->spadjust;
1751 MCode *p = as->mctop-1;
1752 *p = spadj ? (MIPSI_ADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP;
1753 p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
1756 /* Prepare tail of code. */
1757 static void asm_tail_prep(ASMState *as)
1759 as->mcp = as->mctop-2; /* Leave room for branch plus nop or stack adj. */
1760 as->invmcp = as->loopref ? as->mcp : NULL;
1763 /* -- Instruction dispatch ------------------------------------------------ */
1765 /* Assemble a single instruction. */
1766 static void asm_ir(ASMState *as, IRIns *ir)
1768 switch ((IROp)ir->o) {
1769 /* Miscellaneous ops. */
1770 case IR_LOOP: asm_loop(as); break;
1771 case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break;
1772 case IR_USE:
1773 ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break;
1774 case IR_PHI: asm_phi(as, ir); break;
1775 case IR_HIOP: asm_hiop(as, ir); break;
1776 case IR_GCSTEP: asm_gcstep(as, ir); break;
1778 /* Guarded assertions. */
1779 case IR_EQ: case IR_NE: asm_compeq(as, ir); break;
1780 case IR_LT: case IR_GE: case IR_LE: case IR_GT:
1781 case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT:
1782 case IR_ABC:
1783 asm_comp(as, ir);
1784 break;
1786 case IR_RETF: asm_retf(as, ir); break;
1788 /* Bit ops. */
1789 case IR_BNOT: asm_bitnot(as, ir); break;
1790 case IR_BSWAP: asm_bitswap(as, ir); break;
1792 case IR_BAND: asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI); break;
1793 case IR_BOR: asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI); break;
1794 case IR_BXOR: asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI); break;
1796 case IR_BSHL: asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL); break;
1797 case IR_BSHR: asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL); break;
1798 case IR_BSAR: asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA); break;
1799 case IR_BROL: lua_assert(0); break;
1800 case IR_BROR: asm_bitror(as, ir); break;
1802 /* Arithmetic ops. */
1803 case IR_ADD: asm_add(as, ir); break;
1804 case IR_SUB: asm_sub(as, ir); break;
1805 case IR_MUL: asm_mul(as, ir); break;
1806 case IR_DIV: asm_fparith(as, ir, MIPSI_DIV_D); break;
1807 case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break;
1808 case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break;
1809 case IR_NEG: asm_neg(as, ir); break;
1811 case IR_ABS: asm_fpunary(as, ir, MIPSI_ABS_D); break;
1812 case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break;
1813 case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break;
1814 case IR_MIN: asm_min_max(as, ir, 0); break;
1815 case IR_MAX: asm_min_max(as, ir, 1); break;
1816 case IR_FPMATH:
1817 if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
1818 break;
1819 if (ir->op2 <= IRFPM_TRUNC)
1820 asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2);
1821 else
1822 asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
1823 break;
1825 /* Overflow-checking arithmetic ops. */
1826 case IR_ADDOV: asm_arithov(as, ir); break;
1827 case IR_SUBOV: asm_arithov(as, ir); break;
1828 case IR_MULOV: asm_mulov(as, ir); break;
1830 /* Memory references. */
1831 case IR_AREF: asm_aref(as, ir); break;
1832 case IR_HREF: asm_href(as, ir); break;
1833 case IR_HREFK: asm_hrefk(as, ir); break;
1834 case IR_NEWREF: asm_newref(as, ir); break;
1835 case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break;
1836 case IR_FREF: asm_fref(as, ir); break;
1837 case IR_STRREF: asm_strref(as, ir); break;
1839 /* Loads and stores. */
1840 case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
1841 asm_ahuvload(as, ir);
1842 break;
1843 case IR_FLOAD: asm_fload(as, ir); break;
1844 case IR_XLOAD: asm_xload(as, ir); break;
1845 case IR_SLOAD: asm_sload(as, ir); break;
1847 case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break;
1848 case IR_FSTORE: asm_fstore(as, ir); break;
1849 case IR_XSTORE: asm_xstore(as, ir, 0); break;
1851 /* Allocations. */
1852 case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break;
1853 case IR_TNEW: asm_tnew(as, ir); break;
1854 case IR_TDUP: asm_tdup(as, ir); break;
1855 case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break;
1857 /* Write barriers. */
1858 case IR_TBAR: asm_tbar(as, ir); break;
1859 case IR_OBAR: asm_obar(as, ir); break;
1861 /* Type conversions. */
1862 case IR_CONV: asm_conv(as, ir); break;
1863 case IR_TOBIT: asm_tobit(as, ir); break;
1864 case IR_TOSTR: asm_tostr(as, ir); break;
1865 case IR_STRTO: asm_strto(as, ir); break;
1867 /* Calls. */
1868 case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break;
1869 case IR_CALLXS: asm_callx(as, ir); break;
1870 case IR_CARG: break;
1872 default:
1873 setintV(&as->J->errinfo, ir->o);
1874 lj_trace_err_info(as->J, LJ_TRERR_NYIIR);
1875 break;
1879 /* -- Trace setup --------------------------------------------------------- */
1881 /* Ensure there are enough stack slots for call arguments. */
1882 static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
1884 IRRef args[CCI_NARGS_MAX];
1885 uint32_t i, nargs = (int)CCI_NARGS(ci);
1886 int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
1887 asm_collectargs(as, ir, ci, args);
1888 for (i = 0; i < nargs; i++) {
1889 if (args[i] && irt_isfp(IR(args[i])->t) &&
1890 nfpr > 0 && !(ci->flags & CCI_VARARG)) {
1891 nfpr--;
1892 ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1;
1893 } else if (args[i] && irt_isnum(IR(args[i])->t)) {
1894 nfpr = 0;
1895 ngpr = ngpr & ~1;
1896 if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1;
1897 } else {
1898 nfpr = 0;
1899 if (ngpr > 0) ngpr--; else nslots++;
1902 if (nslots > as->evenspill) /* Leave room for args in stack slots. */
1903 as->evenspill = nslots;
1904 return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
1907 static void asm_setup_target(ASMState *as)
1909 asm_sparejump_setup(as);
1910 asm_exitstub_setup(as);
1913 /* -- Trace patching ------------------------------------------------------ */
1915 /* Patch exit jumps of existing machine code to a new target. */
1916 void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
1918 MCode *p = T->mcode;
1919 MCode *pe = (MCode *)((char *)p + T->szmcode);
1920 MCode *px = exitstub_trace_addr(T, exitno);
1921 MCode *cstart = NULL, *cstop = NULL;
1922 MCode *mcarea = lj_mcode_patch(J, p, 0);
1923 MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno;
1924 MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
1925 for (p++; p < pe; p++) {
1926 if (*p == exitload) { /* Look for load of exit number. */
1927 if (((p[-1] ^ (px-p)) & 0xffffu) == 0) { /* Look for exitstub branch. */
1928 ptrdiff_t delta = target - p;
1929 if (((delta + 0x8000) >> 16) == 0) { /* Patch in-range branch. */
1930 patchbranch:
1931 p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu);
1932 *p = MIPSI_NOP; /* Replace the load of the exit number. */
1933 cstop = p;
1934 if (!cstart) cstart = p-1;
1935 } else { /* Branch out of range. Use spare jump slot in mcarea. */
1936 int i;
1937 for (i = 2; i < 2+MIPS_SPAREJUMP*2; i += 2) {
1938 if (mcarea[i] == tjump) {
1939 delta = mcarea+i - p;
1940 goto patchbranch;
1941 } else if (mcarea[i] == MIPSI_NOP) {
1942 mcarea[i] = tjump;
1943 cstart = mcarea+i;
1944 delta = mcarea+i - p;
1945 goto patchbranch;
1948 /* Ignore jump slot overflow. Child trace is simply not attached. */
1950 } else if (p+1 == pe) {
1951 /* Patch NOP after code for inverted loop branch. Use of J is ok. */
1952 lua_assert(p[1] == MIPSI_NOP);
1953 p[1] = tjump;
1954 *p = MIPSI_NOP; /* Replace the load of the exit number. */
1955 cstop = p+2;
1956 if (!cstart) cstart = p+1;
1960 if (cstart) lj_mcode_sync(cstart, cstop);
1961 lj_mcode_patch(J, mcarea, 1);