aboutsummaryrefslogtreecommitdiff
path: root/src/lj_asm_mips.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_asm_mips.h')
-rw-r--r--src/lj_asm_mips.h1492
1 files changed, 1087 insertions, 405 deletions
diff --git a/src/lj_asm_mips.h b/src/lj_asm_mips.h
index 76311903..3a4679b8 100644
--- a/src/lj_asm_mips.h
+++ b/src/lj_asm_mips.h
@@ -23,7 +23,7 @@ static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow)
23{ 23{
24 Reg r = IR(ref)->r; 24 Reg r = IR(ref)->r;
25 if (ra_noreg(r)) { 25 if (ra_noreg(r)) {
26 if (!(allow & RSET_FPR) && irref_isk(ref) && IR(ref)->i == 0) 26 if (!(allow & RSET_FPR) && irref_isk(ref) && get_kval(IR(ref)) == 0)
27 return RID_ZERO; 27 return RID_ZERO;
28 r = ra_allocref(as, ref, allow); 28 r = ra_allocref(as, ref, allow);
29 } else { 29 } else {
@@ -165,9 +165,9 @@ static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
165 } else if (ir->o == IR_UREFC) { 165 } else if (ir->o == IR_UREFC) {
166 if (irref_isk(ir->op1)) { 166 if (irref_isk(ir->op1)) {
167 GCfunc *fn = ir_kfunc(IR(ir->op1)); 167 GCfunc *fn = ir_kfunc(IR(ir->op1));
168 int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); 168 intptr_t ofs = (intptr_t)&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv;
169 int32_t jgl = (intptr_t)J2G(as->J); 169 intptr_t jgl = (intptr_t)J2G(as->J);
170 if ((uint32_t)(ofs-jgl) < 65536) { 170 if ((uintptr_t)(ofs-jgl) < 65536) {
171 *ofsp = ofs-jgl-32768; 171 *ofsp = ofs-jgl-32768;
172 return RID_JGL; 172 return RID_JGL;
173 } else { 173 } else {
@@ -189,20 +189,21 @@ static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,
189 Reg base; 189 Reg base;
190 if (ra_noreg(ir->r) && canfuse(as, ir)) { 190 if (ra_noreg(ir->r) && canfuse(as, ir)) {
191 if (ir->o == IR_ADD) { 191 if (ir->o == IR_ADD) {
192 int32_t ofs2; 192 intptr_t ofs2;
193 if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { 193 if (irref_isk(ir->op2) && (ofs2 = ofs + get_kval(IR(ir->op2)),
194 checki16(ofs2))) {
194 ref = ir->op1; 195 ref = ir->op1;
195 ofs = ofs2; 196 ofs = (int32_t)ofs2;
196 } 197 }
197 } else if (ir->o == IR_STRREF) { 198 } else if (ir->o == IR_STRREF) {
198 int32_t ofs2 = 65536; 199 intptr_t ofs2 = 65536;
199 lua_assert(ofs == 0); 200 lua_assert(ofs == 0);
200 ofs = (int32_t)sizeof(GCstr); 201 ofs = (int32_t)sizeof(GCstr);
201 if (irref_isk(ir->op2)) { 202 if (irref_isk(ir->op2)) {
202 ofs2 = ofs + IR(ir->op2)->i; 203 ofs2 = ofs + get_kval(IR(ir->op2));
203 ref = ir->op1; 204 ref = ir->op1;
204 } else if (irref_isk(ir->op1)) { 205 } else if (irref_isk(ir->op1)) {
205 ofs2 = ofs + IR(ir->op1)->i; 206 ofs2 = ofs + get_kval(IR(ir->op1));
206 ref = ir->op2; 207 ref = ir->op2;
207 } 208 }
208 if (!checki16(ofs2)) { 209 if (!checki16(ofs2)) {
@@ -210,7 +211,7 @@ static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,
210 Reg right, left = ra_alloc2(as, ir, allow); 211 Reg right, left = ra_alloc2(as, ir, allow);
211 right = (left >> 8); left &= 255; 212 right = (left >> 8); left &= 255;
212 emit_hsi(as, mi, rt, RID_TMP, ofs); 213 emit_hsi(as, mi, rt, RID_TMP, ofs);
213 emit_dst(as, MIPSI_ADDU, RID_TMP, left, right); 214 emit_dst(as, MIPSI_AADDU, RID_TMP, left, right);
214 return; 215 return;
215 } 216 }
216 ofs = ofs2; 217 ofs = ofs2;
@@ -225,29 +226,41 @@ static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,
225/* Generate a call to a C function. */ 226/* Generate a call to a C function. */
226static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) 227static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
227{ 228{
228 uint32_t n, nargs = CCI_NARGS(ci); 229 uint32_t n, nargs = CCI_XNARGS(ci);
229 int32_t ofs = 16; 230 int32_t ofs = LJ_32 ? 16 : 0;
231#if LJ_SOFTFP
232 Reg gpr = REGARG_FIRSTGPR;
233#else
230 Reg gpr, fpr = REGARG_FIRSTFPR; 234 Reg gpr, fpr = REGARG_FIRSTFPR;
235#endif
231 if ((void *)ci->func) 236 if ((void *)ci->func)
232 emit_call(as, (void *)ci->func); 237 emit_call(as, (void *)ci->func, 1);
238#if !LJ_SOFTFP
233 for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) 239 for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)
234 as->cost[gpr] = REGCOST(~0u, ASMREF_L); 240 as->cost[gpr] = REGCOST(~0u, ASMREF_L);
235 gpr = REGARG_FIRSTGPR; 241 gpr = REGARG_FIRSTGPR;
242#endif
236 for (n = 0; n < nargs; n++) { /* Setup args. */ 243 for (n = 0; n < nargs; n++) { /* Setup args. */
237 IRRef ref = args[n]; 244 IRRef ref = args[n];
238 if (ref) { 245 if (ref) {
239 IRIns *ir = IR(ref); 246 IRIns *ir = IR(ref);
247#if !LJ_SOFTFP
240 if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR && 248 if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR &&
241 !(ci->flags & CCI_VARARG)) { 249 !(ci->flags & CCI_VARARG)) {
242 lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ 250 lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */
243 ra_leftov(as, fpr, ref); 251 ra_leftov(as, fpr, ref);
244 fpr += 2; 252 fpr += LJ_32 ? 2 : 1;
245 gpr += irt_isnum(ir->t) ? 2 : 1; 253 gpr += (LJ_32 && irt_isnum(ir->t)) ? 2 : 1;
246 } else { 254 } else
255#endif
256 {
257#if LJ_32 && !LJ_SOFTFP
247 fpr = REGARG_LASTFPR+1; 258 fpr = REGARG_LASTFPR+1;
248 if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1; 259#endif
260 if (LJ_32 && irt_isnum(ir->t)) gpr = (gpr+1) & ~1;
249 if (gpr <= REGARG_LASTGPR) { 261 if (gpr <= REGARG_LASTGPR) {
250 lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ 262 lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */
263#if !LJ_SOFTFP
251 if (irt_isfp(ir->t)) { 264 if (irt_isfp(ir->t)) {
252 RegSet of = as->freeset; 265 RegSet of = as->freeset;
253 Reg r; 266 Reg r;
@@ -256,31 +269,55 @@ static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
256 r = ra_alloc1(as, ref, RSET_FPR); 269 r = ra_alloc1(as, ref, RSET_FPR);
257 as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); 270 as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));
258 if (irt_isnum(ir->t)) { 271 if (irt_isnum(ir->t)) {
272#if LJ_32
259 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1); 273 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1);
260 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r); 274 emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r);
261 lua_assert(rset_test(as->freeset, gpr+1)); /* Already evicted. */ 275 lua_assert(rset_test(as->freeset, gpr+1)); /* Already evicted. */
262 gpr += 2; 276 gpr += 2;
277#else
278 emit_tg(as, MIPSI_DMFC1, gpr, r);
279 gpr++; fpr++;
280#endif
263 } else if (irt_isfloat(ir->t)) { 281 } else if (irt_isfloat(ir->t)) {
264 emit_tg(as, MIPSI_MFC1, gpr, r); 282 emit_tg(as, MIPSI_MFC1, gpr, r);
265 gpr++; 283 gpr++;
284#if LJ_64
285 fpr++;
286#endif
266 } 287 }
267 } else { 288 } else
289#endif
290 {
268 ra_leftov(as, gpr, ref); 291 ra_leftov(as, gpr, ref);
269 gpr++; 292 gpr++;
293#if LJ_64 && !LJ_SOFTFP
294 fpr++;
295#endif
270 } 296 }
271 } else { 297 } else {
272 Reg r = ra_alloc1z(as, ref, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); 298 Reg r = ra_alloc1z(as, ref, !LJ_SOFTFP && irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
299#if LJ_32
273 if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; 300 if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
274 emit_spstore(as, ir, r, ofs); 301 emit_spstore(as, ir, r, ofs);
275 ofs += irt_isnum(ir->t) ? 8 : 4; 302 ofs += irt_isnum(ir->t) ? 8 : 4;
303#else
304 emit_spstore(as, ir, r, ofs + ((LJ_BE && !irt_isfp(ir->t) && !irt_is64(ir->t)) ? 4 : 0));
305 ofs += 8;
306#endif
276 } 307 }
277 } 308 }
278 } else { 309 } else {
310#if !LJ_SOFTFP
279 fpr = REGARG_LASTFPR+1; 311 fpr = REGARG_LASTFPR+1;
280 if (gpr <= REGARG_LASTGPR) 312#endif
313 if (gpr <= REGARG_LASTGPR) {
281 gpr++; 314 gpr++;
282 else 315#if LJ_64 && !LJ_SOFTFP
283 ofs += 4; 316 fpr++;
317#endif
318 } else {
319 ofs += LJ_32 ? 4 : 8;
320 }
284 } 321 }
285 checkmclim(as); 322 checkmclim(as);
286 } 323 }
@@ -290,50 +327,57 @@ static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
290static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) 327static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
291{ 328{
292 RegSet drop = RSET_SCRATCH; 329 RegSet drop = RSET_SCRATCH;
330#if LJ_32
293 int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); 331 int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
332#endif
333#if !LJ_SOFTFP
294 if ((ci->flags & CCI_NOFPRCLOBBER)) 334 if ((ci->flags & CCI_NOFPRCLOBBER))
295 drop &= ~RSET_FPR; 335 drop &= ~RSET_FPR;
336#endif
296 if (ra_hasreg(ir->r)) 337 if (ra_hasreg(ir->r))
297 rset_clear(drop, ir->r); /* Dest reg handled below. */ 338 rset_clear(drop, ir->r); /* Dest reg handled below. */
339#if LJ_32
298 if (hiop && ra_hasreg((ir+1)->r)) 340 if (hiop && ra_hasreg((ir+1)->r))
299 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ 341 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
342#endif
300 ra_evictset(as, drop); /* Evictions must be performed first. */ 343 ra_evictset(as, drop); /* Evictions must be performed first. */
301 if (ra_used(ir)) { 344 if (ra_used(ir)) {
302 lua_assert(!irt_ispri(ir->t)); 345 lua_assert(!irt_ispri(ir->t));
303 if (irt_isfp(ir->t)) { 346 if (!LJ_SOFTFP && irt_isfp(ir->t)) {
304 if ((ci->flags & CCI_CASTU64)) { 347 if ((ci->flags & CCI_CASTU64)) {
305 int32_t ofs = sps_scale(ir->s); 348 int32_t ofs = sps_scale(ir->s);
306 Reg dest = ir->r; 349 Reg dest = ir->r;
307 if (ra_hasreg(dest)) { 350 if (ra_hasreg(dest)) {
308 ra_free(as, dest); 351 ra_free(as, dest);
309 ra_modified(as, dest); 352 ra_modified(as, dest);
353#if LJ_32
310 emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1); 354 emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1);
311 emit_tg(as, MIPSI_MTC1, RID_RETLO, dest); 355 emit_tg(as, MIPSI_MTC1, RID_RETLO, dest);
356#else
357 emit_tg(as, MIPSI_DMTC1, RID_RET, dest);
358#endif
312 } 359 }
313 if (ofs) { 360 if (ofs) {
361#if LJ_32
314 emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0)); 362 emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0));
315 emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4)); 363 emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4));
364#else
365 emit_tsi(as, MIPSI_SD, RID_RET, RID_SP, ofs);
366#endif
316 } 367 }
317 } else { 368 } else {
318 ra_destreg(as, ir, RID_FPRET); 369 ra_destreg(as, ir, RID_FPRET);
319 } 370 }
371#if LJ_32
320 } else if (hiop) { 372 } else if (hiop) {
321 ra_destpair(as, ir); 373 ra_destpair(as, ir);
374#endif
322 } else { 375 } else {
323 ra_destreg(as, ir, RID_RET); 376 ra_destreg(as, ir, RID_RET);
324 } 377 }
325 } 378 }
326} 379}
327 380
328static void asm_call(ASMState *as, IRIns *ir)
329{
330 IRRef args[CCI_NARGS_MAX];
331 const CCallInfo *ci = &lj_ir_callinfo[ir->op2];
332 asm_collectargs(as, ir, ci, args);
333 asm_setupresult(as, ir, ci);
334 asm_gencall(as, ci, args);
335}
336
337static void asm_callx(ASMState *as, IRIns *ir) 381static void asm_callx(ASMState *as, IRIns *ir)
338{ 382{
339 IRRef args[CCI_NARGS_MAX*2]; 383 IRRef args[CCI_NARGS_MAX*2];
@@ -346,7 +390,7 @@ static void asm_callx(ASMState *as, IRIns *ir)
346 func = ir->op2; irf = IR(func); 390 func = ir->op2; irf = IR(func);
347 if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } 391 if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
348 if (irref_isk(func)) { /* Call to constant address. */ 392 if (irref_isk(func)) { /* Call to constant address. */
349 ci.func = (ASMFunction)(void *)(irf->i); 393 ci.func = (ASMFunction)(void *)get_kval(irf);
350 } else { /* Need specific register for indirect calls. */ 394 } else { /* Need specific register for indirect calls. */
351 Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR)); 395 Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR));
352 MCode *p = as->mcp; 396 MCode *p = as->mcp;
@@ -361,16 +405,7 @@ static void asm_callx(ASMState *as, IRIns *ir)
361 asm_gencall(as, &ci, args); 405 asm_gencall(as, &ci, args);
362} 406}
363 407
364static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) 408#if !LJ_SOFTFP
365{
366 const CCallInfo *ci = &lj_ir_callinfo[id];
367 IRRef args[2];
368 args[0] = ir->op1;
369 args[1] = ir->op2;
370 asm_setupresult(as, ir, ci);
371 asm_gencall(as, ci, args);
372}
373
374static void asm_callround(ASMState *as, IRIns *ir, IRCallID id) 409static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)
375{ 410{
376 /* The modified regs must match with the *.dasc implementation. */ 411 /* The modified regs must match with the *.dasc implementation. */
@@ -379,9 +414,10 @@ static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)
379 if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); 414 if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
380 ra_evictset(as, drop); 415 ra_evictset(as, drop);
381 ra_destreg(as, ir, RID_FPRET); 416 ra_destreg(as, ir, RID_FPRET);
382 emit_call(as, (void *)lj_ir_callinfo[id].func); 417 emit_call(as, (void *)lj_ir_callinfo[id].func, 0);
383 ra_leftov(as, REGARG_FIRSTFPR, ir->op1); 418 ra_leftov(as, REGARG_FIRSTFPR, ir->op1);
384} 419}
420#endif
385 421
386/* -- Returns ------------------------------------------------------------- */ 422/* -- Returns ------------------------------------------------------------- */
387 423
@@ -390,19 +426,20 @@ static void asm_retf(ASMState *as, IRIns *ir)
390{ 426{
391 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); 427 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
392 void *pc = ir_kptr(IR(ir->op2)); 428 void *pc = ir_kptr(IR(ir->op2));
393 int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); 429 int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
394 as->topslot -= (BCReg)delta; 430 as->topslot -= (BCReg)delta;
395 if ((int32_t)as->topslot < 0) as->topslot = 0; 431 if ((int32_t)as->topslot < 0) as->topslot = 0;
396 irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ 432 irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
397 emit_setgl(as, base, jit_base); 433 emit_setgl(as, base, jit_base);
398 emit_addptr(as, base, -8*delta); 434 emit_addptr(as, base, -8*delta);
399 asm_guard(as, MIPSI_BNE, RID_TMP, 435 asm_guard(as, MIPSI_BNE, RID_TMP,
400 ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); 436 ra_allock(as, igcptr(pc), rset_exclude(RSET_GPR, base)));
401 emit_tsi(as, MIPSI_LW, RID_TMP, base, -8); 437 emit_tsi(as, MIPSI_AL, RID_TMP, base, -8);
402} 438}
403 439
404/* -- Type conversions ---------------------------------------------------- */ 440/* -- Type conversions ---------------------------------------------------- */
405 441
442#if !LJ_SOFTFP
406static void asm_tointg(ASMState *as, IRIns *ir, Reg left) 443static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
407{ 444{
408 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); 445 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
@@ -424,15 +461,53 @@ static void asm_tobit(ASMState *as, IRIns *ir)
424 emit_tg(as, MIPSI_MFC1, dest, tmp); 461 emit_tg(as, MIPSI_MFC1, dest, tmp);
425 emit_fgh(as, MIPSI_ADD_D, tmp, left, right); 462 emit_fgh(as, MIPSI_ADD_D, tmp, left, right);
426} 463}
464#elif LJ_64 /* && LJ_SOFTFP */
465static void asm_tointg(ASMState *as, IRIns *ir, Reg r)
466{
467 /* The modified regs must match with the *.dasc implementation. */
468 RegSet drop = RID2RSET(REGARG_FIRSTGPR)|RID2RSET(RID_RET)|RID2RSET(RID_RET+1)|
469 RID2RSET(RID_R1)|RID2RSET(RID_R12);
470 if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
471 ra_evictset(as, drop);
472 /* Return values are in RID_RET (converted value) and RID_RET+1 (status). */
473 ra_destreg(as, ir, RID_RET);
474 asm_guard(as, MIPSI_BNE, RID_RET+1, RID_ZERO);
475 emit_call(as, (void *)lj_ir_callinfo[IRCALL_lj_vm_tointg].func, 0);
476 if (r == RID_NONE)
477 ra_leftov(as, REGARG_FIRSTGPR, ir->op1);
478 else if (r != REGARG_FIRSTGPR)
479 emit_move(as, REGARG_FIRSTGPR, r);
480}
481
482static void asm_tobit(ASMState *as, IRIns *ir)
483{
484 Reg dest = ra_dest(as, ir, RSET_GPR);
485 emit_dta(as, MIPSI_SLL, dest, dest, 0);
486 asm_callid(as, ir, IRCALL_lj_vm_tobit);
487}
488#endif
427 489
428static void asm_conv(ASMState *as, IRIns *ir) 490static void asm_conv(ASMState *as, IRIns *ir)
429{ 491{
430 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); 492 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
493#if !LJ_SOFTFP32
431 int stfp = (st == IRT_NUM || st == IRT_FLOAT); 494 int stfp = (st == IRT_NUM || st == IRT_FLOAT);
495#endif
496#if LJ_64
497 int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64);
498#endif
432 IRRef lref = ir->op1; 499 IRRef lref = ir->op1;
433 lua_assert(irt_type(ir->t) != st); 500#if LJ_32
434 lua_assert(!(irt_isint64(ir->t) || 501 lua_assert(!(irt_isint64(ir->t) ||
435 (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ 502 (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */
503#endif
504#if LJ_SOFTFP32
505 /* FP conversions are handled by SPLIT. */
506 lua_assert(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT));
507 /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */
508#else
509 lua_assert(irt_type(ir->t) != st);
510#if !LJ_SOFTFP
436 if (irt_isfp(ir->t)) { 511 if (irt_isfp(ir->t)) {
437 Reg dest = ra_dest(as, ir, RSET_FPR); 512 Reg dest = ra_dest(as, ir, RSET_FPR);
438 if (stfp) { /* FP to FP conversion. */ 513 if (stfp) { /* FP to FP conversion. */
@@ -448,16 +523,44 @@ static void asm_conv(ASMState *as, IRIns *ir)
448 emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp); 523 emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp);
449 emit_fg(as, MIPSI_CVT_D_W, dest, dest); 524 emit_fg(as, MIPSI_CVT_D_W, dest, dest);
450 emit_lsptr(as, MIPSI_LDC1, (tmp & 31), 525 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
451 (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), 526 (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR);
452 RSET_GPR);
453 emit_tg(as, MIPSI_MTC1, RID_TMP, dest); 527 emit_tg(as, MIPSI_MTC1, RID_TMP, dest);
454 emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left); 528 emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left);
455 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); 529 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
530#if LJ_64
531 } else if(st == IRT_U64) { /* U64 to FP conversion. */
532 /* if (x >= 1u<<63) y = (double)(int64_t)(x&(1u<<63)-1) + pow(2.0, 63) */
533 Reg left = ra_alloc1(as, lref, RSET_GPR);
534 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest));
535 MCLabel l_end = emit_label(as);
536 if (irt_isfloat(ir->t)) {
537 emit_fgh(as, MIPSI_ADD_S, dest, dest, tmp);
538 emit_lsptr(as, MIPSI_LWC1, (tmp & 31), (void *)&as->J->k32[LJ_K32_2P63],
539 rset_exclude(RSET_GPR, left));
540 emit_fg(as, MIPSI_CVT_S_L, dest, dest);
541 } else {
542 emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp);
543 emit_lsptr(as, MIPSI_LDC1, (tmp & 31), (void *)&as->J->k64[LJ_K64_2P63],
544 rset_exclude(RSET_GPR, left));
545 emit_fg(as, MIPSI_CVT_D_L, dest, dest);
546 }
547 emit_branch(as, MIPSI_BGEZ, left, RID_ZERO, l_end);
548 emit_tg(as, MIPSI_DMTC1, RID_TMP, dest);
549 emit_tsml(as, MIPSI_DEXTM, RID_TMP, left, 30, 0);
550#endif
456 } else { /* Integer to FP conversion. */ 551 } else { /* Integer to FP conversion. */
457 Reg left = ra_alloc1(as, lref, RSET_GPR); 552 Reg left = ra_alloc1(as, lref, RSET_GPR);
553#if LJ_32
458 emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, 554 emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,
459 dest, dest); 555 dest, dest);
460 emit_tg(as, MIPSI_MTC1, left, dest); 556 emit_tg(as, MIPSI_MTC1, left, dest);
557#else
558 MIPSIns mi = irt_isfloat(ir->t) ?
559 (st64 ? MIPSI_CVT_S_L : MIPSI_CVT_S_W) :
560 (st64 ? MIPSI_CVT_D_L : MIPSI_CVT_D_W);
561 emit_fg(as, mi, dest, dest);
562 emit_tg(as, st64 ? MIPSI_DMTC1 : MIPSI_MTC1, left, dest);
563#endif
461 } 564 }
462 } else if (stfp) { /* FP to integer conversion. */ 565 } else if (stfp) { /* FP to integer conversion. */
463 if (irt_isguard(ir->t)) { 566 if (irt_isguard(ir->t)) {
@@ -468,7 +571,7 @@ static void asm_conv(ASMState *as, IRIns *ir)
468 Reg dest = ra_dest(as, ir, RSET_GPR); 571 Reg dest = ra_dest(as, ir, RSET_GPR);
469 Reg left = ra_alloc1(as, lref, RSET_FPR); 572 Reg left = ra_alloc1(as, lref, RSET_FPR);
470 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); 573 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
471 if (irt_isu32(ir->t)) { 574 if (irt_isu32(ir->t)) { /* FP to U32 conversion. */
472 /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */ 575 /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */
473 emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP); 576 emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP);
474 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); 577 emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
@@ -479,25 +582,101 @@ static void asm_conv(ASMState *as, IRIns *ir)
479 tmp, left, tmp); 582 tmp, left, tmp);
480 if (st == IRT_FLOAT) 583 if (st == IRT_FLOAT)
481 emit_lsptr(as, MIPSI_LWC1, (tmp & 31), 584 emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
482 (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)), 585 (void *)&as->J->k32[LJ_K32_2P31], RSET_GPR);
483 RSET_GPR);
484 else 586 else
485 emit_lsptr(as, MIPSI_LDC1, (tmp & 31), 587 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
486 (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), 588 (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR);
487 RSET_GPR); 589#if LJ_64
590 } else if (irt_isu64(ir->t)) { /* FP to U64 conversion. */
591 MCLabel l_end;
592 emit_tg(as, MIPSI_DMFC1, dest, tmp);
593 l_end = emit_label(as);
594 /* For inputs >= 2^63 add -2^64 and convert again. */
595 if (st == IRT_NUM) {
596 emit_fg(as, MIPSI_TRUNC_L_D, tmp, tmp);
597 emit_fgh(as, MIPSI_ADD_D, tmp, left, tmp);
598 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
599 (void *)&as->J->k64[LJ_K64_M2P64],
600 rset_exclude(RSET_GPR, dest));
601 emit_fg(as, MIPSI_TRUNC_L_D, tmp, left); /* Delay slot. */
602 emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
603 emit_fgh(as, MIPSI_C_OLT_D, 0, left, tmp);
604 emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
605 (void *)&as->J->k64[LJ_K64_2P63],
606 rset_exclude(RSET_GPR, dest));
607 } else {
608 emit_fg(as, MIPSI_TRUNC_L_S, tmp, tmp);
609 emit_fgh(as, MIPSI_ADD_S, tmp, left, tmp);
610 emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
611 (void *)&as->J->k32[LJ_K32_M2P64],
612 rset_exclude(RSET_GPR, dest));
613 emit_fg(as, MIPSI_TRUNC_L_S, tmp, left); /* Delay slot. */
614 emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
615 emit_fgh(as, MIPSI_C_OLT_S, 0, left, tmp);
616 emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
617 (void *)&as->J->k32[LJ_K32_2P63],
618 rset_exclude(RSET_GPR, dest));
619 }
620#endif
488 } else { 621 } else {
622#if LJ_32
489 emit_tg(as, MIPSI_MFC1, dest, tmp); 623 emit_tg(as, MIPSI_MFC1, dest, tmp);
490 emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D, 624 emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D,
491 tmp, left); 625 tmp, left);
626#else
627 MIPSIns mi = irt_is64(ir->t) ?
628 (st == IRT_NUM ? MIPSI_TRUNC_L_D : MIPSI_TRUNC_L_S) :
629 (st == IRT_NUM ? MIPSI_TRUNC_W_D : MIPSI_TRUNC_W_S);
630 emit_tg(as, irt_is64(ir->t) ? MIPSI_DMFC1 : MIPSI_MFC1, dest, left);
631 emit_fg(as, mi, left, left);
632#endif
492 } 633 }
493 } 634 }
494 } else { 635 } else
636#else
637 if (irt_isfp(ir->t)) {
638#if LJ_64 && LJ_HASFFI
639 if (stfp) { /* FP to FP conversion. */
640 asm_callid(as, ir, irt_isnum(ir->t) ? IRCALL_softfp_f2d :
641 IRCALL_softfp_d2f);
642 } else { /* Integer to FP conversion. */
643 IRCallID cid = ((IRT_IS64 >> st) & 1) ?
644 (irt_isnum(ir->t) ?
645 (st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d) :
646 (st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f)) :
647 (irt_isnum(ir->t) ?
648 (st == IRT_INT ? IRCALL_softfp_i2d : IRCALL_softfp_ui2d) :
649 (st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f));
650 asm_callid(as, ir, cid);
651 }
652#else
653 asm_callid(as, ir, IRCALL_softfp_i2d);
654#endif
655 } else if (stfp) { /* FP to integer conversion. */
656 if (irt_isguard(ir->t)) {
657 /* Checked conversions are only supported from number to int. */
658 lua_assert(irt_isint(ir->t) && st == IRT_NUM);
659 asm_tointg(as, ir, RID_NONE);
660 } else {
661 IRCallID cid = irt_is64(ir->t) ?
662 ((st == IRT_NUM) ?
663 (irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul) :
664 (irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul)) :
665 ((st == IRT_NUM) ?
666 (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) :
667 (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui));
668 asm_callid(as, ir, cid);
669 }
670 } else
671#endif
672#endif
673 {
495 Reg dest = ra_dest(as, ir, RSET_GPR); 674 Reg dest = ra_dest(as, ir, RSET_GPR);
496 if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ 675 if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
497 Reg left = ra_alloc1(as, ir->op1, RSET_GPR); 676 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
498 lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); 677 lua_assert(irt_isint(ir->t) || irt_isu32(ir->t));
499 if ((ir->op2 & IRCONV_SEXT)) { 678 if ((ir->op2 & IRCONV_SEXT)) {
500 if ((as->flags & JIT_F_MIPS32R2)) { 679 if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) {
501 emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left); 680 emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left);
502 } else { 681 } else {
503 uint32_t shift = st == IRT_I8 ? 24 : 16; 682 uint32_t shift = st == IRT_I8 ? 24 : 16;
@@ -509,94 +688,141 @@ static void asm_conv(ASMState *as, IRIns *ir)
509 (int32_t)(st == IRT_U8 ? 0xff : 0xffff)); 688 (int32_t)(st == IRT_U8 ? 0xff : 0xffff));
510 } 689 }
511 } else { /* 32/64 bit integer conversions. */ 690 } else { /* 32/64 bit integer conversions. */
691#if LJ_32
512 /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ 692 /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
513 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ 693 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
694#else
695 if (irt_is64(ir->t)) {
696 if (st64) {
697 /* 64/64 bit no-op (cast)*/
698 ra_leftov(as, dest, lref);
699 } else {
700 Reg left = ra_alloc1(as, lref, RSET_GPR);
701 if ((ir->op2 & IRCONV_SEXT)) { /* 32 to 64 bit sign extension. */
702 emit_dta(as, MIPSI_SLL, dest, left, 0);
703 } else { /* 32 to 64 bit zero extension. */
704 emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0);
705 }
706 }
707 } else {
708 if (st64) {
709 /* This is either a 32 bit reg/reg mov which zeroes the hiword
710 ** or a load of the loword from a 64 bit address.
711 */
712 Reg left = ra_alloc1(as, lref, RSET_GPR);
713 emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0);
714 } else { /* 32/32 bit no-op (cast). */
715 /* Do nothing, but may need to move regs. */
716 ra_leftov(as, dest, lref);
717 }
718 }
719#endif
514 } 720 }
515 } 721 }
516} 722}
517 723
518#if LJ_HASFFI
519static void asm_conv64(ASMState *as, IRIns *ir)
520{
521 IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);
522 IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);
523 IRCallID id;
524 const CCallInfo *ci;
525 IRRef args[2];
526 args[LJ_BE?0:1] = ir->op1;
527 args[LJ_BE?1:0] = (ir-1)->op1;
528 if (st == IRT_NUM || st == IRT_FLOAT) {
529 id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64);
530 ir--;
531 } else {
532 id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64);
533 }
534 ci = &lj_ir_callinfo[id];
535 asm_setupresult(as, ir, ci);
536 asm_gencall(as, ci, args);
537}
538#endif
539
540static void asm_strto(ASMState *as, IRIns *ir) 724static void asm_strto(ASMState *as, IRIns *ir)
541{ 725{
542 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; 726 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
543 IRRef args[2]; 727 IRRef args[2];
728 int32_t ofs = 0;
729#if LJ_SOFTFP32
730 ra_evictset(as, RSET_SCRATCH);
731 if (ra_used(ir)) {
732 if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) &&
733 (ir->s & 1) == LJ_BE && (ir->s ^ 1) == (ir+1)->s) {
734 int i;
735 for (i = 0; i < 2; i++) {
736 Reg r = (ir+i)->r;
737 if (ra_hasreg(r)) {
738 ra_free(as, r);
739 ra_modified(as, r);
740 emit_spload(as, ir+i, r, sps_scale((ir+i)->s));
741 }
742 }
743 ofs = sps_scale(ir->s & ~1);
744 } else {
745 Reg rhi = ra_dest(as, ir+1, RSET_GPR);
746 Reg rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi));
747 emit_tsi(as, MIPSI_LW, rhi, RID_SP, ofs+(LJ_BE?0:4));
748 emit_tsi(as, MIPSI_LW, rlo, RID_SP, ofs+(LJ_BE?4:0));
749 }
750 }
751#else
544 RegSet drop = RSET_SCRATCH; 752 RegSet drop = RSET_SCRATCH;
545 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ 753 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
546 ra_evictset(as, drop); 754 ra_evictset(as, drop);
755 ofs = sps_scale(ir->s);
756#endif
547 asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */ 757 asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */
548 args[0] = ir->op1; /* GCstr *str */ 758 args[0] = ir->op1; /* GCstr *str */
549 args[1] = ASMREF_TMP1; /* TValue *n */ 759 args[1] = ASMREF_TMP1; /* TValue *n */
550 asm_gencall(as, ci, args); 760 asm_gencall(as, ci, args);
551 /* Store the result to the spill slot or temp slots. */ 761 /* Store the result to the spill slot or temp slots. */
552 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), 762 emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1),
553 RID_SP, sps_scale(ir->s)); 763 RID_SP, ofs);
554} 764}
555 765
766/* -- Memory references --------------------------------------------------- */
767
768#if LJ_64
769/* Store tagged value for ref at base+ofs. */
770static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref)
771{
772 RegSet allow = rset_exclude(RSET_GPR, base);
773 IRIns *ir = IR(ref);
774 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
775 if (irref_isk(ref)) {
776 TValue k;
777 lj_ir_kvalue(as->J->L, &k, ir);
778 emit_tsi(as, MIPSI_SD, ra_allock(as, (int64_t)k.u64, allow), base, ofs);
779 } else {
780 Reg src = ra_alloc1(as, ref, allow);
781 Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47,
782 rset_exclude(allow, src));
783 emit_tsi(as, MIPSI_SD, RID_TMP, base, ofs);
784 if (irt_isinteger(ir->t)) {
785 emit_dst(as, MIPSI_DADDU, RID_TMP, RID_TMP, type);
786 emit_tsml(as, MIPSI_DEXT, RID_TMP, src, 31, 0);
787 } else {
788 emit_dst(as, MIPSI_DADDU, RID_TMP, src, type);
789 }
790 }
791}
792#endif
793
556/* Get pointer to TValue. */ 794/* Get pointer to TValue. */
557static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) 795static void asm_tvptr(ASMState *as, Reg dest, IRRef ref)
558{ 796{
559 IRIns *ir = IR(ref); 797 IRIns *ir = IR(ref);
560 if (irt_isnum(ir->t)) { 798 if (irt_isnum(ir->t)) {
561 if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ 799 if (irref_isk(ref)) /* Use the number constant itself as a TValue. */
562 ra_allockreg(as, i32ptr(ir_knum(ir)), dest); 800 ra_allockreg(as, igcptr(ir_knum(ir)), dest);
563 else /* Otherwise force a spill and use the spill slot. */ 801 else /* Otherwise force a spill and use the spill slot. */
564 emit_tsi(as, MIPSI_ADDIU, dest, RID_SP, ra_spill(as, ir)); 802 emit_tsi(as, MIPSI_AADDIU, dest, RID_SP, ra_spill(as, ir));
565 } else { 803 } else {
566 /* Otherwise use g->tmptv to hold the TValue. */ 804 /* Otherwise use g->tmptv to hold the TValue. */
805#if LJ_32
567 RegSet allow = rset_exclude(RSET_GPR, dest); 806 RegSet allow = rset_exclude(RSET_GPR, dest);
568 Reg type; 807 Reg type;
569 emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, offsetof(global_State, tmptv)-32768); 808 emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, (int32_t)(offsetof(global_State, tmptv)-32768));
570 if (!irt_ispri(ir->t)) { 809 if (!irt_ispri(ir->t)) {
571 Reg src = ra_alloc1(as, ref, allow); 810 Reg src = ra_alloc1(as, ref, allow);
572 emit_setgl(as, src, tmptv.gcr); 811 emit_setgl(as, src, tmptv.gcr);
573 } 812 }
574 type = ra_allock(as, irt_toitype(ir->t), allow); 813 if (LJ_SOFTFP && (ir+1)->o == IR_HIOP)
814 type = ra_alloc1(as, ref+1, allow);
815 else
816 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
575 emit_setgl(as, type, tmptv.it); 817 emit_setgl(as, type, tmptv.it);
818#else
819 asm_tvstore64(as, dest, 0, ref);
820 emit_tsi(as, MIPSI_DADDIU, dest, RID_JGL,
821 (int32_t)(offsetof(global_State, tmptv)-32768));
822#endif
576 } 823 }
577} 824}
578 825
579static void asm_tostr(ASMState *as, IRIns *ir)
580{
581 IRRef args[2];
582 args[0] = ASMREF_L;
583 as->gcsteps++;
584 if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) {
585 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum];
586 args[1] = ASMREF_TMP1; /* const lua_Number * */
587 asm_setupresult(as, ir, ci); /* GCstr * */
588 asm_gencall(as, ci, args);
589 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1);
590 } else {
591 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint];
592 args[1] = ir->op1; /* int32_t k */
593 asm_setupresult(as, ir, ci); /* GCstr * */
594 asm_gencall(as, ci, args);
595 }
596}
597
598/* -- Memory references --------------------------------------------------- */
599
600static void asm_aref(ASMState *as, IRIns *ir) 826static void asm_aref(ASMState *as, IRIns *ir)
601{ 827{
602 Reg dest = ra_dest(as, ir, RSET_GPR); 828 Reg dest = ra_dest(as, ir, RSET_GPR);
@@ -608,13 +834,13 @@ static void asm_aref(ASMState *as, IRIns *ir)
608 ofs += 8*IR(ir->op2)->i; 834 ofs += 8*IR(ir->op2)->i;
609 if (checki16(ofs)) { 835 if (checki16(ofs)) {
610 base = ra_alloc1(as, refa, RSET_GPR); 836 base = ra_alloc1(as, refa, RSET_GPR);
611 emit_tsi(as, MIPSI_ADDIU, dest, base, ofs); 837 emit_tsi(as, MIPSI_AADDIU, dest, base, ofs);
612 return; 838 return;
613 } 839 }
614 } 840 }
615 base = ra_alloc1(as, ir->op1, RSET_GPR); 841 base = ra_alloc1(as, ir->op1, RSET_GPR);
616 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); 842 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
617 emit_dst(as, MIPSI_ADDU, dest, RID_TMP, base); 843 emit_dst(as, MIPSI_AADDU, dest, RID_TMP, base);
618 emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3); 844 emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3);
619} 845}
620 846
@@ -626,51 +852,104 @@ static void asm_aref(ASMState *as, IRIns *ir)
626** } while ((n = nextnode(n))); 852** } while ((n = nextnode(n)));
627** return niltv(L); 853** return niltv(L);
628*/ 854*/
629static void asm_href(ASMState *as, IRIns *ir) 855static void asm_href(ASMState *as, IRIns *ir, IROp merge)
630{ 856{
631 RegSet allow = RSET_GPR; 857 RegSet allow = RSET_GPR;
632 int destused = ra_used(ir); 858 int destused = ra_used(ir);
633 Reg dest = ra_dest(as, ir, allow); 859 Reg dest = ra_dest(as, ir, allow);
634 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); 860 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
635 Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2; 861 Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2;
862#if LJ_64
863 Reg cmp64 = RID_NONE;
864#endif
636 IRRef refkey = ir->op2; 865 IRRef refkey = ir->op2;
637 IRIns *irkey = IR(refkey); 866 IRIns *irkey = IR(refkey);
867 int isk = irref_isk(refkey);
638 IRType1 kt = irkey->t; 868 IRType1 kt = irkey->t;
639 uint32_t khash; 869 uint32_t khash;
640 MCLabel l_end, l_loop, l_next; 870 MCLabel l_end, l_loop, l_next;
641 871
642 rset_clear(allow, tab); 872 rset_clear(allow, tab);
643 if (irt_isnum(kt)) { 873#if LJ_SOFTFP32
874 if (!isk) {
875 key = ra_alloc1(as, refkey, allow);
876 rset_clear(allow, key);
877 if (irkey[1].o == IR_HIOP) {
878 if (ra_hasreg((irkey+1)->r)) {
879 type = tmpnum = (irkey+1)->r;
880 tmp1 = ra_scratch(as, allow);
881 rset_clear(allow, tmp1);
882 ra_noweak(as, tmpnum);
883 } else {
884 type = tmpnum = ra_allocref(as, refkey+1, allow);
885 }
886 rset_clear(allow, tmpnum);
887 } else {
888 type = ra_allock(as, (int32_t)irt_toitype(irkey->t), allow);
889 rset_clear(allow, type);
890 }
891 }
892#else
893 if (!LJ_SOFTFP && irt_isnum(kt)) {
644 key = ra_alloc1(as, refkey, RSET_FPR); 894 key = ra_alloc1(as, refkey, RSET_FPR);
645 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); 895 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
646 } else if (!irt_ispri(kt)) { 896 } else if (!irt_ispri(kt)) {
647 key = ra_alloc1(as, refkey, allow); 897 key = ra_alloc1(as, refkey, allow);
648 rset_clear(allow, key); 898 rset_clear(allow, key);
649 type = ra_allock(as, irt_toitype(irkey->t), allow); 899#if LJ_32
900 type = ra_allock(as, (int32_t)irt_toitype(irkey->t), allow);
650 rset_clear(allow, type); 901 rset_clear(allow, type);
902#endif
651 } 903 }
904#endif
652 tmp2 = ra_scratch(as, allow); 905 tmp2 = ra_scratch(as, allow);
653 rset_clear(allow, tmp2); 906 rset_clear(allow, tmp2);
907#if LJ_64
908 if (LJ_SOFTFP || !irt_isnum(kt)) {
909 /* Allocate cmp64 register used for 64-bit comparisons */
910 if (LJ_SOFTFP && irt_isnum(kt)) {
911 cmp64 = key;
912 } else if (!isk && irt_isaddr(kt)) {
913 cmp64 = tmp2;
914 } else {
915 int64_t k;
916 if (isk && irt_isaddr(kt)) {
917 k = ((int64_t)irt_toitype(irkey->t) << 47) | irkey[1].tv.u64;
918 } else {
919 lua_assert(irt_ispri(kt) && !irt_isnil(kt));
920 k = ~((int64_t)~irt_toitype(ir->t) << 47);
921 }
922 cmp64 = ra_allock(as, k, allow);
923 rset_clear(allow, cmp64);
924 }
925 }
926#endif
654 927
655 /* Key not found in chain: load niltv. */ 928 /* Key not found in chain: jump to exit (if merged) or load niltv. */
656 l_end = emit_label(as); 929 l_end = emit_label(as);
657 if (destused) 930 as->invmcp = NULL;
931 if (merge == IR_NE)
932 asm_guard(as, MIPSI_B, RID_ZERO, RID_ZERO);
933 else if (destused)
658 emit_loada(as, dest, niltvg(J2G(as->J))); 934 emit_loada(as, dest, niltvg(J2G(as->J)));
659 else
660 *--as->mcp = MIPSI_NOP;
661 /* Follow hash chain until the end. */ 935 /* Follow hash chain until the end. */
662 emit_move(as, dest, tmp1); 936 emit_move(as, dest, tmp1);
663 l_loop = --as->mcp; 937 l_loop = --as->mcp;
664 emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, next)); 938 emit_tsi(as, MIPSI_AL, tmp1, dest, (int32_t)offsetof(Node, next));
665 l_next = emit_label(as); 939 l_next = emit_label(as);
666 940
667 /* Type and value comparison. */ 941 /* Type and value comparison. */
668 if (irt_isnum(kt)) { 942 if (merge == IR_EQ) { /* Must match asm_guard(). */
943 emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);
944 l_end = asm_exitstub_addr(as);
945 }
946 if (!LJ_SOFTFP && irt_isnum(kt)) {
669 emit_branch(as, MIPSI_BC1T, 0, 0, l_end); 947 emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
670 emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key); 948 emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key);
671 emit_tg(as, MIPSI_MFC1, tmp1, key+1); 949 *--as->mcp = MIPSI_NOP; /* Avoid NaN comparison overhead. */
672 emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next); 950 emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next);
673 emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM); 951 emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM);
952#if LJ_32
674 emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n)); 953 emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n));
675 } else { 954 } else {
676 if (irt_ispri(kt)) { 955 if (irt_ispri(kt)) {
@@ -683,24 +962,39 @@ static void asm_href(ASMState *as, IRIns *ir)
683 } 962 }
684 emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it)); 963 emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it));
685 *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu); 964 *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu);
965#else
966 emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 15);
967 emit_tg(as, MIPSI_DMTC1, tmp1, tmpnum);
968 emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64));
969 } else {
970 emit_branch(as, MIPSI_BEQ, tmp1, cmp64, l_end);
971 emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64));
972 }
973 *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu);
974 if (!isk && irt_isaddr(kt)) {
975 type = ra_allock(as, (int64_t)irt_toitype(kt) << 47, allow);
976 emit_dst(as, MIPSI_DADDU, tmp2, key, type);
977 rset_clear(allow, type);
978 }
979#endif
686 980
687 /* Load main position relative to tab->node into dest. */ 981 /* Load main position relative to tab->node into dest. */
688 khash = irref_isk(refkey) ? ir_khash(irkey) : 1; 982 khash = isk ? ir_khash(irkey) : 1;
689 if (khash == 0) { 983 if (khash == 0) {
690 emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); 984 emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node));
691 } else { 985 } else {
692 Reg tmphash = tmp1; 986 Reg tmphash = tmp1;
693 if (irref_isk(refkey)) 987 if (isk)
694 tmphash = ra_allock(as, khash, allow); 988 tmphash = ra_allock(as, khash, allow);
695 emit_dst(as, MIPSI_ADDU, dest, dest, tmp1); 989 emit_dst(as, MIPSI_AADDU, dest, dest, tmp1);
696 lua_assert(sizeof(Node) == 24); 990 lua_assert(sizeof(Node) == 24);
697 emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1); 991 emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1);
698 emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3); 992 emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3);
699 emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5); 993 emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5);
700 emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash); 994 emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash);
701 emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); 995 emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node));
702 emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); 996 emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
703 if (irref_isk(refkey)) { 997 if (isk) {
704 /* Nothing to do. */ 998 /* Nothing to do. */
705 } else if (irt_isstr(kt)) { 999 } else if (irt_isstr(kt)) {
706 emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash)); 1000 emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash));
@@ -710,9 +1004,10 @@ static void asm_href(ASMState *as, IRIns *ir)
710 emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2); 1004 emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2);
711 emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31); 1005 emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31);
712 emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest); 1006 emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest);
713 if (irt_isnum(kt)) { 1007#if LJ_32
1008 if (LJ_SOFTFP ? (irkey[1].o == IR_HIOP) : irt_isnum(kt)) {
714 emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1); 1009 emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);
715 if ((as->flags & JIT_F_MIPS32R2)) { 1010 if ((as->flags & JIT_F_MIPSXXR2)) {
716 emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31); 1011 emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);
717 } else { 1012 } else {
718 emit_dst(as, MIPSI_OR, dest, dest, tmp1); 1013 emit_dst(as, MIPSI_OR, dest, dest, tmp1);
@@ -720,13 +1015,35 @@ static void asm_href(ASMState *as, IRIns *ir)
720 emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31); 1015 emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31);
721 } 1016 }
722 emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1); 1017 emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);
1018#if LJ_SOFTFP
1019 emit_ds(as, MIPSI_MOVE, tmp1, type);
1020 emit_ds(as, MIPSI_MOVE, tmp2, key);
1021#else
723 emit_tg(as, MIPSI_MFC1, tmp2, key); 1022 emit_tg(as, MIPSI_MFC1, tmp2, key);
724 emit_tg(as, MIPSI_MFC1, tmp1, key+1); 1023 emit_tg(as, MIPSI_MFC1, tmp1, key+1);
1024#endif
725 } else { 1025 } else {
726 emit_dst(as, MIPSI_XOR, tmp2, key, tmp1); 1026 emit_dst(as, MIPSI_XOR, tmp2, key, tmp1);
727 emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31); 1027 emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31);
728 emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow)); 1028 emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow));
729 } 1029 }
1030#else
1031 emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);
1032 emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);
1033 if (irt_isnum(kt)) {
1034 emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);
1035 emit_dta(as, MIPSI_DSRA32, tmp1, LJ_SOFTFP ? key : tmp1, 0);
1036 emit_dta(as, MIPSI_SLL, tmp2, LJ_SOFTFP ? key : tmp1, 0);
1037#if !LJ_SOFTFP
1038 emit_tg(as, MIPSI_DMFC1, tmp1, key);
1039#endif
1040 } else {
1041 checkmclim(as);
1042 emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 0);
1043 emit_dta(as, MIPSI_SLL, tmp2, key, 0);
1044 emit_dst(as, MIPSI_DADDU, tmp1, key, type);
1045 }
1046#endif
730 } 1047 }
731 } 1048 }
732} 1049}
@@ -739,17 +1056,24 @@ static void asm_hrefk(ASMState *as, IRIns *ir)
739 int32_t kofs = ofs + (int32_t)offsetof(Node, key); 1056 int32_t kofs = ofs + (int32_t)offsetof(Node, key);
740 Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; 1057 Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
741 Reg node = ra_alloc1(as, ir->op1, RSET_GPR); 1058 Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
742 Reg key = RID_NONE, type = RID_TMP, idx = node;
743 RegSet allow = rset_exclude(RSET_GPR, node); 1059 RegSet allow = rset_exclude(RSET_GPR, node);
1060 Reg idx = node;
1061#if LJ_32
1062 Reg key = RID_NONE, type = RID_TMP;
744 int32_t lo, hi; 1063 int32_t lo, hi;
1064#else
1065 Reg key = ra_scratch(as, allow);
1066 int64_t k;
1067#endif
745 lua_assert(ofs % sizeof(Node) == 0); 1068 lua_assert(ofs % sizeof(Node) == 0);
746 if (ofs > 32736) { 1069 if (ofs > 32736) {
747 idx = dest; 1070 idx = dest;
748 rset_clear(allow, dest); 1071 rset_clear(allow, dest);
749 kofs = (int32_t)offsetof(Node, key); 1072 kofs = (int32_t)offsetof(Node, key);
750 } else if (ra_hasreg(dest)) { 1073 } else if (ra_hasreg(dest)) {
751 emit_tsi(as, MIPSI_ADDIU, dest, node, ofs); 1074 emit_tsi(as, MIPSI_AADDIU, dest, node, ofs);
752 } 1075 }
1076#if LJ_32
753 if (!irt_ispri(irkey->t)) { 1077 if (!irt_ispri(irkey->t)) {
754 key = ra_scratch(as, allow); 1078 key = ra_scratch(as, allow);
755 rset_clear(allow, key); 1079 rset_clear(allow, key);
@@ -768,22 +1092,20 @@ nolo:
768 asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO); 1092 asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO);
769 if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0)); 1093 if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0));
770 emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4)); 1094 emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4));
771 if (ofs > 32736) 1095#else
772 emit_tsi(as, MIPSI_ADDU, dest, node, ra_allock(as, ofs, allow)); 1096 if (irt_ispri(irkey->t)) {
773} 1097 lua_assert(!irt_isnil(irkey->t));
774 1098 k = ~((int64_t)~irt_toitype(irkey->t) << 47);
775static void asm_newref(ASMState *as, IRIns *ir) 1099 } else if (irt_isnum(irkey->t)) {
776{ 1100 k = (int64_t)ir_knum(irkey)->u64;
777 if (ir->r != RID_SINK) { 1101 } else {
778 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; 1102 k = ((int64_t)irt_toitype(irkey->t) << 47) | (int64_t)ir_kgc(irkey);
779 IRRef args[3];
780 args[0] = ASMREF_L; /* lua_State *L */
781 args[1] = ir->op1; /* GCtab *t */
782 args[2] = ASMREF_TMP1; /* cTValue *key */
783 asm_setupresult(as, ir, ci); /* TValue * */
784 asm_gencall(as, ci, args);
785 asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2);
786 } 1103 }
1104 asm_guard(as, MIPSI_BNE, key, ra_allock(as, k, allow));
1105 emit_tsi(as, MIPSI_LD, key, idx, kofs);
1106#endif
1107 if (ofs > 32736)
1108 emit_tsi(as, MIPSI_AADDU, dest, node, ra_allock(as, ofs, allow));
787} 1109}
788 1110
789static void asm_uref(ASMState *as, IRIns *ir) 1111static void asm_uref(ASMState *as, IRIns *ir)
@@ -792,19 +1114,19 @@ static void asm_uref(ASMState *as, IRIns *ir)
792 if (irref_isk(ir->op1)) { 1114 if (irref_isk(ir->op1)) {
793 GCfunc *fn = ir_kfunc(IR(ir->op1)); 1115 GCfunc *fn = ir_kfunc(IR(ir->op1));
794 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; 1116 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
795 emit_lsptr(as, MIPSI_LW, dest, v, RSET_GPR); 1117 emit_lsptr(as, MIPSI_AL, dest, v, RSET_GPR);
796 } else { 1118 } else {
797 Reg uv = ra_scratch(as, RSET_GPR); 1119 Reg uv = ra_scratch(as, RSET_GPR);
798 Reg func = ra_alloc1(as, ir->op1, RSET_GPR); 1120 Reg func = ra_alloc1(as, ir->op1, RSET_GPR);
799 if (ir->o == IR_UREFC) { 1121 if (ir->o == IR_UREFC) {
800 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); 1122 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
801 emit_tsi(as, MIPSI_ADDIU, dest, uv, (int32_t)offsetof(GCupval, tv)); 1123 emit_tsi(as, MIPSI_AADDIU, dest, uv, (int32_t)offsetof(GCupval, tv));
802 emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); 1124 emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed));
803 } else { 1125 } else {
804 emit_tsi(as, MIPSI_LW, dest, uv, (int32_t)offsetof(GCupval, v)); 1126 emit_tsi(as, MIPSI_AL, dest, uv, (int32_t)offsetof(GCupval, v));
805 } 1127 }
806 emit_tsi(as, MIPSI_LW, uv, func, 1128 emit_tsi(as, MIPSI_AL, uv, func, (int32_t)offsetof(GCfuncL, uvptr) +
807 (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); 1129 (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8));
808 } 1130 }
809} 1131}
810 1132
@@ -816,6 +1138,7 @@ static void asm_fref(ASMState *as, IRIns *ir)
816 1138
817static void asm_strref(ASMState *as, IRIns *ir) 1139static void asm_strref(ASMState *as, IRIns *ir)
818{ 1140{
1141#if LJ_32
819 Reg dest = ra_dest(as, ir, RSET_GPR); 1142 Reg dest = ra_dest(as, ir, RSET_GPR);
820 IRRef ref = ir->op2, refk = ir->op1; 1143 IRRef ref = ir->op2, refk = ir->op1;
821 int32_t ofs = (int32_t)sizeof(GCstr); 1144 int32_t ofs = (int32_t)sizeof(GCstr);
@@ -847,6 +1170,20 @@ static void asm_strref(ASMState *as, IRIns *ir)
847 else 1170 else
848 emit_dst(as, MIPSI_ADDU, dest, r, 1171 emit_dst(as, MIPSI_ADDU, dest, r,
849 ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); 1172 ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
1173#else
1174 RegSet allow = RSET_GPR;
1175 Reg dest = ra_dest(as, ir, allow);
1176 Reg base = ra_alloc1(as, ir->op1, allow);
1177 IRIns *irr = IR(ir->op2);
1178 int32_t ofs = sizeof(GCstr);
1179 rset_clear(allow, base);
1180 if (irref_isk(ir->op2) && checki16(ofs + irr->i)) {
1181 emit_tsi(as, MIPSI_DADDIU, dest, base, ofs + irr->i);
1182 } else {
1183 emit_tsi(as, MIPSI_DADDIU, dest, dest, ofs);
1184 emit_dst(as, MIPSI_DADDU, dest, base, ra_alloc1(as, ir->op2, allow));
1185 }
1186#endif
850} 1187}
851 1188
852/* -- Loads and stores ---------------------------------------------------- */ 1189/* -- Loads and stores ---------------------------------------------------- */
@@ -858,9 +1195,9 @@ static MIPSIns asm_fxloadins(IRIns *ir)
858 case IRT_U8: return MIPSI_LBU; 1195 case IRT_U8: return MIPSI_LBU;
859 case IRT_I16: return MIPSI_LH; 1196 case IRT_I16: return MIPSI_LH;
860 case IRT_U16: return MIPSI_LHU; 1197 case IRT_U16: return MIPSI_LHU;
861 case IRT_NUM: return MIPSI_LDC1; 1198 case IRT_NUM: lua_assert(!LJ_SOFTFP32); if (!LJ_SOFTFP) return MIPSI_LDC1;
862 case IRT_FLOAT: return MIPSI_LWC1; 1199 case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_LWC1;
863 default: return MIPSI_LW; 1200 default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_LD : MIPSI_LW;
864 } 1201 }
865} 1202}
866 1203
@@ -869,26 +1206,32 @@ static MIPSIns asm_fxstoreins(IRIns *ir)
869 switch (irt_type(ir->t)) { 1206 switch (irt_type(ir->t)) {
870 case IRT_I8: case IRT_U8: return MIPSI_SB; 1207 case IRT_I8: case IRT_U8: return MIPSI_SB;
871 case IRT_I16: case IRT_U16: return MIPSI_SH; 1208 case IRT_I16: case IRT_U16: return MIPSI_SH;
872 case IRT_NUM: return MIPSI_SDC1; 1209 case IRT_NUM: lua_assert(!LJ_SOFTFP32); if (!LJ_SOFTFP) return MIPSI_SDC1;
873 case IRT_FLOAT: return MIPSI_SWC1; 1210 case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_SWC1;
874 default: return MIPSI_SW; 1211 default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_SD : MIPSI_SW;
875 } 1212 }
876} 1213}
877 1214
878static void asm_fload(ASMState *as, IRIns *ir) 1215static void asm_fload(ASMState *as, IRIns *ir)
879{ 1216{
880 Reg dest = ra_dest(as, ir, RSET_GPR); 1217 Reg dest = ra_dest(as, ir, RSET_GPR);
881 Reg idx = ra_alloc1(as, ir->op1, RSET_GPR);
882 MIPSIns mi = asm_fxloadins(ir); 1218 MIPSIns mi = asm_fxloadins(ir);
1219 Reg idx;
883 int32_t ofs; 1220 int32_t ofs;
884 if (ir->op2 == IRFL_TAB_ARRAY) { 1221 if (ir->op1 == REF_NIL) {
885 ofs = asm_fuseabase(as, ir->op1); 1222 idx = RID_JGL;
886 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ 1223 ofs = (ir->op2 << 2) - 32768 - GG_OFS(g);
887 emit_tsi(as, MIPSI_ADDIU, dest, idx, ofs); 1224 } else {
888 return; 1225 idx = ra_alloc1(as, ir->op1, RSET_GPR);
1226 if (ir->op2 == IRFL_TAB_ARRAY) {
1227 ofs = asm_fuseabase(as, ir->op1);
1228 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
1229 emit_tsi(as, MIPSI_AADDIU, dest, idx, ofs);
1230 return;
1231 }
889 } 1232 }
1233 ofs = field_ofs[ir->op2];
890 } 1234 }
891 ofs = field_ofs[ir->op2];
892 lua_assert(!irt_isfp(ir->t)); 1235 lua_assert(!irt_isfp(ir->t));
893 emit_tsi(as, mi, dest, idx, ofs); 1236 emit_tsi(as, mi, dest, idx, ofs);
894} 1237}
@@ -908,43 +1251,79 @@ static void asm_fstore(ASMState *as, IRIns *ir)
908 1251
909static void asm_xload(ASMState *as, IRIns *ir) 1252static void asm_xload(ASMState *as, IRIns *ir)
910{ 1253{
911 Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); 1254 Reg dest = ra_dest(as, ir,
1255 (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
912 lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); 1256 lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED));
913 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); 1257 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);
914} 1258}
915 1259
916static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) 1260static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
917{ 1261{
918 if (ir->r != RID_SINK) { 1262 if (ir->r != RID_SINK) {
919 Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); 1263 Reg src = ra_alloc1z(as, ir->op2,
1264 (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
920 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, 1265 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
921 rset_exclude(RSET_GPR, src), ofs); 1266 rset_exclude(RSET_GPR, src), ofs);
922 } 1267 }
923} 1268}
924 1269
1270#define asm_xstore(as, ir) asm_xstore_(as, ir, 0)
1271
925static void asm_ahuvload(ASMState *as, IRIns *ir) 1272static void asm_ahuvload(ASMState *as, IRIns *ir)
926{ 1273{
927 IRType1 t = ir->t; 1274 int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP);
928 Reg dest = RID_NONE, type = RID_TMP, idx; 1275 Reg dest = RID_NONE, type = RID_TMP, idx;
929 RegSet allow = RSET_GPR; 1276 RegSet allow = RSET_GPR;
930 int32_t ofs = 0; 1277 int32_t ofs = 0;
1278 IRType1 t = ir->t;
1279 if (hiop) {
1280 t.irt = IRT_NUM;
1281 if (ra_used(ir+1)) {
1282 type = ra_dest(as, ir+1, allow);
1283 rset_clear(allow, type);
1284 }
1285 }
931 if (ra_used(ir)) { 1286 if (ra_used(ir)) {
932 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); 1287 lua_assert((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) ||
933 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); 1288 irt_isint(ir->t) || irt_isaddr(ir->t));
1289 dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow);
934 rset_clear(allow, dest); 1290 rset_clear(allow, dest);
1291#if LJ_64
1292 if (irt_isaddr(t))
1293 emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0);
1294 else if (irt_isint(t))
1295 emit_dta(as, MIPSI_SLL, dest, dest, 0);
1296#endif
935 } 1297 }
936 idx = asm_fuseahuref(as, ir->op1, &ofs, allow); 1298 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
937 rset_clear(allow, idx); 1299 rset_clear(allow, idx);
938 if (irt_isnum(t)) { 1300 if (irt_isnum(t)) {
939 asm_guard(as, MIPSI_BEQ, type, RID_ZERO); 1301 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
940 emit_tsi(as, MIPSI_SLTIU, type, type, (int32_t)LJ_TISNUM); 1302 emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM);
941 if (ra_hasreg(dest))
942 emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
943 } else { 1303 } else {
944 asm_guard(as, MIPSI_BNE, type, ra_allock(as, irt_toitype(t), allow)); 1304 asm_guard(as, MIPSI_BNE, type,
945 if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0)); 1305 ra_allock(as, (int32_t)irt_toitype(t), allow));
1306 }
1307#if LJ_32
1308 if (ra_hasreg(dest)) {
1309 if (!LJ_SOFTFP && irt_isnum(t))
1310 emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
1311 else
1312 emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0));
946 } 1313 }
947 emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4)); 1314 emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4));
1315#else
1316 if (ra_hasreg(dest)) {
1317 if (!LJ_SOFTFP && irt_isnum(t)) {
1318 emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
1319 dest = type;
1320 }
1321 } else {
1322 dest = type;
1323 }
1324 emit_dta(as, MIPSI_DSRA32, type, dest, 15);
1325 emit_tsi(as, MIPSI_LD, dest, idx, ofs);
1326#endif
948} 1327}
949 1328
950static void asm_ahustore(ASMState *as, IRIns *ir) 1329static void asm_ahustore(ASMState *as, IRIns *ir)
@@ -954,81 +1333,176 @@ static void asm_ahustore(ASMState *as, IRIns *ir)
954 int32_t ofs = 0; 1333 int32_t ofs = 0;
955 if (ir->r == RID_SINK) 1334 if (ir->r == RID_SINK)
956 return; 1335 return;
957 if (irt_isnum(ir->t)) { 1336 if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
958 src = ra_alloc1(as, ir->op2, RSET_FPR); 1337 src = ra_alloc1(as, ir->op2, LJ_SOFTFP ? RSET_GPR : RSET_FPR);
1338 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
1339 emit_hsi(as, LJ_SOFTFP ? MIPSI_SD : MIPSI_SDC1, src, idx, ofs);
959 } else { 1340 } else {
1341#if LJ_32
960 if (!irt_ispri(ir->t)) { 1342 if (!irt_ispri(ir->t)) {
961 src = ra_alloc1(as, ir->op2, allow); 1343 src = ra_alloc1(as, ir->op2, allow);
962 rset_clear(allow, src); 1344 rset_clear(allow, src);
963 } 1345 }
964 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); 1346 if (LJ_SOFTFP && (ir+1)->o == IR_HIOP)
1347 type = ra_alloc1(as, (ir+1)->op2, allow);
1348 else
1349 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
965 rset_clear(allow, type); 1350 rset_clear(allow, type);
966 } 1351 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
967 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
968 if (irt_isnum(ir->t)) {
969 emit_hsi(as, MIPSI_SDC1, src, idx, ofs);
970 } else {
971 if (ra_hasreg(src)) 1352 if (ra_hasreg(src))
972 emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0)); 1353 emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0));
973 emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4)); 1354 emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4));
1355#else
1356 Reg tmp = RID_TMP;
1357 if (irt_ispri(ir->t)) {
1358 tmp = ra_allock(as, ~((int64_t)~irt_toitype(ir->t) << 47), allow);
1359 rset_clear(allow, tmp);
1360 } else {
1361 src = ra_alloc1(as, ir->op2, allow);
1362 rset_clear(allow, src);
1363 type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow);
1364 rset_clear(allow, type);
1365 }
1366 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
1367 emit_tsi(as, MIPSI_SD, tmp, idx, ofs);
1368 if (ra_hasreg(src)) {
1369 if (irt_isinteger(ir->t)) {
1370 emit_dst(as, MIPSI_DADDU, tmp, tmp, type);
1371 emit_tsml(as, MIPSI_DEXT, tmp, src, 31, 0);
1372 } else {
1373 emit_dst(as, MIPSI_DADDU, tmp, src, type);
1374 }
1375 }
1376#endif
974 } 1377 }
975} 1378}
976 1379
977static void asm_sload(ASMState *as, IRIns *ir) 1380static void asm_sload(ASMState *as, IRIns *ir)
978{ 1381{
979 int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
980 IRType1 t = ir->t;
981 Reg dest = RID_NONE, type = RID_NONE, base; 1382 Reg dest = RID_NONE, type = RID_NONE, base;
982 RegSet allow = RSET_GPR; 1383 RegSet allow = RSET_GPR;
1384 IRType1 t = ir->t;
1385#if LJ_32
1386 int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
1387 int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP);
1388 if (hiop)
1389 t.irt = IRT_NUM;
1390#else
1391 int32_t ofs = 8*((int32_t)ir->op1-2);
1392#endif
983 lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ 1393 lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */
984 lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); 1394 lua_assert(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK));
985 lua_assert(!irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); 1395#if LJ_SOFTFP32
1396 lua_assert(!(ir->op2 & IRSLOAD_CONVERT)); /* Handled by LJ_SOFTFP SPLIT. */
1397 if (hiop && ra_used(ir+1)) {
1398 type = ra_dest(as, ir+1, allow);
1399 rset_clear(allow, type);
1400 }
1401#else
986 if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { 1402 if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
987 dest = ra_scratch(as, RSET_FPR); 1403 dest = ra_scratch(as, LJ_SOFTFP ? allow : RSET_FPR);
988 asm_tointg(as, ir, dest); 1404 asm_tointg(as, ir, dest);
989 t.irt = IRT_NUM; /* Continue with a regular number type check. */ 1405 t.irt = IRT_NUM; /* Continue with a regular number type check. */
990 } else if (ra_used(ir)) { 1406 } else
991 lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); 1407#endif
992 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); 1408 if (ra_used(ir)) {
1409 lua_assert((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) ||
1410 irt_isint(ir->t) || irt_isaddr(ir->t));
1411 dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow);
993 rset_clear(allow, dest); 1412 rset_clear(allow, dest);
994 base = ra_alloc1(as, REF_BASE, allow); 1413 base = ra_alloc1(as, REF_BASE, allow);
995 rset_clear(allow, base); 1414 rset_clear(allow, base);
996 if ((ir->op2 & IRSLOAD_CONVERT)) { 1415 if (!LJ_SOFTFP32 && (ir->op2 & IRSLOAD_CONVERT)) {
997 if (irt_isint(t)) { 1416 if (irt_isint(t)) {
998 Reg tmp = ra_scratch(as, RSET_FPR); 1417 Reg tmp = ra_scratch(as, LJ_SOFTFP ? RSET_GPR : RSET_FPR);
1418#if LJ_SOFTFP
1419 ra_evictset(as, rset_exclude(RSET_SCRATCH, dest));
1420 ra_destreg(as, ir, RID_RET);
1421 emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_d2i].func, 0);
1422 if (tmp != REGARG_FIRSTGPR)
1423 emit_move(as, REGARG_FIRSTGPR, tmp);
1424#else
999 emit_tg(as, MIPSI_MFC1, dest, tmp); 1425 emit_tg(as, MIPSI_MFC1, dest, tmp);
1000 emit_fg(as, MIPSI_CVT_W_D, tmp, tmp); 1426 emit_fg(as, MIPSI_TRUNC_W_D, tmp, tmp);
1427#endif
1001 dest = tmp; 1428 dest = tmp;
1002 t.irt = IRT_NUM; /* Check for original type. */ 1429 t.irt = IRT_NUM; /* Check for original type. */
1003 } else { 1430 } else {
1004 Reg tmp = ra_scratch(as, RSET_GPR); 1431 Reg tmp = ra_scratch(as, RSET_GPR);
1432#if LJ_SOFTFP
1433 ra_evictset(as, rset_exclude(RSET_SCRATCH, dest));
1434 ra_destreg(as, ir, RID_RET);
1435 emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_i2d].func, 0);
1436 emit_dta(as, MIPSI_SLL, REGARG_FIRSTGPR, tmp, 0);
1437#else
1005 emit_fg(as, MIPSI_CVT_D_W, dest, dest); 1438 emit_fg(as, MIPSI_CVT_D_W, dest, dest);
1006 emit_tg(as, MIPSI_MTC1, tmp, dest); 1439 emit_tg(as, MIPSI_MTC1, tmp, dest);
1440#endif
1007 dest = tmp; 1441 dest = tmp;
1008 t.irt = IRT_INT; /* Check for original type. */ 1442 t.irt = IRT_INT; /* Check for original type. */
1009 } 1443 }
1010 } 1444 }
1445#if LJ_64
1446 else if (irt_isaddr(t)) {
1447 /* Clear type from pointers. */
1448 emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0);
1449 } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) {
1450 /* Sign-extend integers. */
1451 emit_dta(as, MIPSI_SLL, dest, dest, 0);
1452 }
1453#endif
1011 goto dotypecheck; 1454 goto dotypecheck;
1012 } 1455 }
1013 base = ra_alloc1(as, REF_BASE, allow); 1456 base = ra_alloc1(as, REF_BASE, allow);
1014 rset_clear(allow, base); 1457 rset_clear(allow, base);
1015dotypecheck: 1458dotypecheck:
1016 if (irt_isnum(t)) { 1459#if LJ_32
1017 if ((ir->op2 & IRSLOAD_TYPECHECK)) { 1460 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1018 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); 1461 if (ra_noreg(type))
1019 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM);
1020 type = RID_TMP; 1462 type = RID_TMP;
1021 } 1463 if (irt_isnum(t)) {
1022 if (ra_hasreg(dest)) emit_hsi(as, MIPSI_LDC1, dest, base, ofs); 1464 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
1023 } else { 1465 emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM);
1024 if ((ir->op2 & IRSLOAD_TYPECHECK)) { 1466 } else {
1025 Reg ktype = ra_allock(as, irt_toitype(t), allow); 1467 Reg ktype = ra_allock(as, irt_toitype(t), allow);
1026 asm_guard(as, MIPSI_BNE, RID_TMP, ktype); 1468 asm_guard(as, MIPSI_BNE, type, ktype);
1027 type = RID_TMP; 1469 }
1470 }
1471 if (ra_hasreg(dest)) {
1472 if (!LJ_SOFTFP && irt_isnum(t))
1473 emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
1474 else
1475 emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0));
1476 }
1477 if (ra_hasreg(type))
1478 emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4));
1479#else
1480 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1481 type = dest < RID_MAX_GPR ? dest : RID_TMP;
1482 if (irt_ispri(t)) {
1483 asm_guard(as, MIPSI_BNE, type,
1484 ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow));
1485 } else {
1486 if (irt_isnum(t)) {
1487 asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
1488 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM);
1489 if (!LJ_SOFTFP && ra_hasreg(dest))
1490 emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
1491 } else {
1492 asm_guard(as, MIPSI_BNE, RID_TMP,
1493 ra_allock(as, (int32_t)irt_toitype(t), allow));
1494 }
1495 emit_dta(as, MIPSI_DSRA32, RID_TMP, type, 15);
1028 } 1496 }
1029 if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0)); 1497 emit_tsi(as, MIPSI_LD, type, base, ofs);
1498 } else if (ra_hasreg(dest)) {
1499 if (!LJ_SOFTFP && irt_isnum(t))
1500 emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
1501 else
1502 emit_tsi(as, irt_isint(t) ? MIPSI_LW : MIPSI_LD, dest, base,
1503 ofs ^ ((LJ_BE && irt_isint(t)) ? 4 : 0));
1030 } 1504 }
1031 if (ra_hasreg(type)) emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4)); 1505#endif
1032} 1506}
1033 1507
1034/* -- Allocations --------------------------------------------------------- */ 1508/* -- Allocations --------------------------------------------------------- */
@@ -1037,19 +1511,15 @@ dotypecheck:
1037static void asm_cnew(ASMState *as, IRIns *ir) 1511static void asm_cnew(ASMState *as, IRIns *ir)
1038{ 1512{
1039 CTState *cts = ctype_ctsG(J2G(as->J)); 1513 CTState *cts = ctype_ctsG(J2G(as->J));
1040 CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; 1514 CTypeID id = (CTypeID)IR(ir->op1)->i;
1041 CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? 1515 CTSize sz;
1042 lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; 1516 CTInfo info = lj_ctype_info(cts, id, &sz);
1043 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; 1517 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
1044 IRRef args[2]; 1518 IRRef args[4];
1045 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1046 RegSet drop = RSET_SCRATCH; 1519 RegSet drop = RSET_SCRATCH;
1047 lua_assert(sz != CTSIZE_INVALID); 1520 lua_assert(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL));
1048 1521
1049 args[0] = ASMREF_L; /* lua_State *L */
1050 args[1] = ASMREF_TMP1; /* MSize size */
1051 as->gcsteps++; 1522 as->gcsteps++;
1052
1053 if (ra_hasreg(ir->r)) 1523 if (ra_hasreg(ir->r))
1054 rset_clear(drop, ir->r); /* Dest reg handled below. */ 1524 rset_clear(drop, ir->r); /* Dest reg handled below. */
1055 ra_evictset(as, drop); 1525 ra_evictset(as, drop);
@@ -1058,8 +1528,9 @@ static void asm_cnew(ASMState *as, IRIns *ir)
1058 1528
1059 /* Initialize immutable cdata object. */ 1529 /* Initialize immutable cdata object. */
1060 if (ir->o == IR_CNEWI) { 1530 if (ir->o == IR_CNEWI) {
1531 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1532#if LJ_32
1061 int32_t ofs = sizeof(GCcdata); 1533 int32_t ofs = sizeof(GCcdata);
1062 lua_assert(sz == 4 || sz == 8);
1063 if (sz == 8) { 1534 if (sz == 8) {
1064 ofs += 4; 1535 ofs += 4;
1065 lua_assert((ir+1)->o == IR_HIOP); 1536 lua_assert((ir+1)->o == IR_HIOP);
@@ -1072,12 +1543,29 @@ static void asm_cnew(ASMState *as, IRIns *ir)
1072 if (ofs == sizeof(GCcdata)) break; 1543 if (ofs == sizeof(GCcdata)) break;
1073 ofs -= 4; if (LJ_BE) ir++; else ir--; 1544 ofs -= 4; if (LJ_BE) ir++; else ir--;
1074 } 1545 }
1546#else
1547 emit_tsi(as, MIPSI_SD, ra_alloc1(as, ir->op2, allow),
1548 RID_RET, sizeof(GCcdata));
1549#endif
1550 lua_assert(sz == 4 || sz == 8);
1551 } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
1552 ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
1553 args[0] = ASMREF_L; /* lua_State *L */
1554 args[1] = ir->op1; /* CTypeID id */
1555 args[2] = ir->op2; /* CTSize sz */
1556 args[3] = ASMREF_TMP1; /* CTSize align */
1557 asm_gencall(as, ci, args);
1558 emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
1559 return;
1075 } 1560 }
1561
1076 /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ 1562 /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
1077 emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); 1563 emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
1078 emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); 1564 emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));
1079 emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA); 1565 emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA);
1080 emit_ti(as, MIPSI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */ 1566 emit_ti(as, MIPSI_LI, RID_TMP, id); /* Lower 16 bit used. Sign-ext ok. */
1567 args[0] = ASMREF_L; /* lua_State *L */
1568 args[1] = ASMREF_TMP1; /* MSize size */
1081 asm_gencall(as, ci, args); 1569 asm_gencall(as, ci, args);
1082 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), 1570 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
1083 ra_releasetmp(as, ASMREF_TMP1)); 1571 ra_releasetmp(as, ASMREF_TMP1));
@@ -1094,7 +1582,7 @@ static void asm_tbar(ASMState *as, IRIns *ir)
1094 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); 1582 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
1095 Reg link = RID_TMP; 1583 Reg link = RID_TMP;
1096 MCLabel l_end = emit_label(as); 1584 MCLabel l_end = emit_label(as);
1097 emit_tsi(as, MIPSI_SW, link, tab, (int32_t)offsetof(GCtab, gclist)); 1585 emit_tsi(as, MIPSI_AS, link, tab, (int32_t)offsetof(GCtab, gclist));
1098 emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked)); 1586 emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked));
1099 emit_setgl(as, tab, gc.grayagain); 1587 emit_setgl(as, tab, gc.grayagain);
1100 emit_getgl(as, link, gc.grayagain); 1588 emit_getgl(as, link, gc.grayagain);
@@ -1117,7 +1605,7 @@ static void asm_obar(ASMState *as, IRIns *ir)
1117 args[0] = ASMREF_TMP1; /* global_State *g */ 1605 args[0] = ASMREF_TMP1; /* global_State *g */
1118 args[1] = ir->op1; /* TValue *tv */ 1606 args[1] = ir->op1; /* TValue *tv */
1119 asm_gencall(as, ci, args); 1607 asm_gencall(as, ci, args);
1120 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); 1608 emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1121 obj = IR(ir->op1)->r; 1609 obj = IR(ir->op1)->r;
1122 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); 1610 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
1123 emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); 1611 emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
@@ -1132,6 +1620,7 @@ static void asm_obar(ASMState *as, IRIns *ir)
1132 1620
1133/* -- Arithmetic and logic operations ------------------------------------- */ 1621/* -- Arithmetic and logic operations ------------------------------------- */
1134 1622
1623#if !LJ_SOFTFP
1135static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi) 1624static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi)
1136{ 1625{
1137 Reg dest = ra_dest(as, ir, RSET_FPR); 1626 Reg dest = ra_dest(as, ir, RSET_FPR);
@@ -1146,83 +1635,174 @@ static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi)
1146 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); 1635 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
1147 emit_fg(as, mi, dest, left); 1636 emit_fg(as, mi, dest, left);
1148} 1637}
1638#endif
1149 1639
1150static int asm_fpjoin_pow(ASMState *as, IRIns *ir) 1640#if !LJ_SOFTFP32
1151{ 1641static void asm_fpmath(ASMState *as, IRIns *ir)
1152 IRIns *irp = IR(ir->op1); 1642{
1153 if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { 1643 if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
1154 IRIns *irpp = IR(irp->op1); 1644 return;
1155 if (irpp == ir-2 && irpp->o == IR_FPMATH && 1645#if !LJ_SOFTFP
1156 irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { 1646 if (ir->op2 <= IRFPM_TRUNC)
1157 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; 1647 asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2);
1158 IRRef args[2]; 1648 else if (ir->op2 == IRFPM_SQRT)
1159 args[0] = irpp->op1; 1649 asm_fpunary(as, ir, MIPSI_SQRT_D);
1160 args[1] = irp->op2; 1650 else
1161 asm_setupresult(as, ir, ci); 1651#endif
1162 asm_gencall(as, ci, args); 1652 asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
1163 return 1;
1164 }
1165 }
1166 return 0;
1167} 1653}
1654#endif
1655
1656#if !LJ_SOFTFP
1657#define asm_fpadd(as, ir) asm_fparith(as, ir, MIPSI_ADD_D)
1658#define asm_fpsub(as, ir) asm_fparith(as, ir, MIPSI_SUB_D)
1659#define asm_fpmul(as, ir) asm_fparith(as, ir, MIPSI_MUL_D)
1660#elif LJ_64 /* && LJ_SOFTFP */
1661#define asm_fpadd(as, ir) asm_callid(as, ir, IRCALL_softfp_add)
1662#define asm_fpsub(as, ir) asm_callid(as, ir, IRCALL_softfp_sub)
1663#define asm_fpmul(as, ir) asm_callid(as, ir, IRCALL_softfp_mul)
1664#endif
1168 1665
1169static void asm_add(ASMState *as, IRIns *ir) 1666static void asm_add(ASMState *as, IRIns *ir)
1170{ 1667{
1171 if (irt_isnum(ir->t)) { 1668 IRType1 t = ir->t;
1172 asm_fparith(as, ir, MIPSI_ADD_D); 1669#if !LJ_SOFTFP32
1173 } else { 1670 if (irt_isnum(t)) {
1671 asm_fpadd(as, ir);
1672 } else
1673#endif
1674 {
1174 Reg dest = ra_dest(as, ir, RSET_GPR); 1675 Reg dest = ra_dest(as, ir, RSET_GPR);
1175 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); 1676 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1176 if (irref_isk(ir->op2)) { 1677 if (irref_isk(ir->op2)) {
1177 int32_t k = IR(ir->op2)->i; 1678 intptr_t k = get_kval(IR(ir->op2));
1178 if (checki16(k)) { 1679 if (checki16(k)) {
1179 emit_tsi(as, MIPSI_ADDIU, dest, left, k); 1680 emit_tsi(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDIU : MIPSI_ADDIU, dest,
1681 left, k);
1180 return; 1682 return;
1181 } 1683 }
1182 } 1684 }
1183 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); 1685 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1184 emit_dst(as, MIPSI_ADDU, dest, left, right); 1686 emit_dst(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDU : MIPSI_ADDU, dest,
1687 left, right);
1185 } 1688 }
1186} 1689}
1187 1690
1188static void asm_sub(ASMState *as, IRIns *ir) 1691static void asm_sub(ASMState *as, IRIns *ir)
1189{ 1692{
1693#if !LJ_SOFTFP32
1190 if (irt_isnum(ir->t)) { 1694 if (irt_isnum(ir->t)) {
1191 asm_fparith(as, ir, MIPSI_SUB_D); 1695 asm_fpsub(as, ir);
1192 } else { 1696 } else
1697#endif
1698 {
1193 Reg dest = ra_dest(as, ir, RSET_GPR); 1699 Reg dest = ra_dest(as, ir, RSET_GPR);
1194 Reg right, left = ra_alloc2(as, ir, RSET_GPR); 1700 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1195 right = (left >> 8); left &= 255; 1701 right = (left >> 8); left &= 255;
1196 emit_dst(as, MIPSI_SUBU, dest, left, right); 1702 emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest,
1703 left, right);
1197 } 1704 }
1198} 1705}
1199 1706
1200static void asm_mul(ASMState *as, IRIns *ir) 1707static void asm_mul(ASMState *as, IRIns *ir)
1201{ 1708{
1709#if !LJ_SOFTFP32
1202 if (irt_isnum(ir->t)) { 1710 if (irt_isnum(ir->t)) {
1203 asm_fparith(as, ir, MIPSI_MUL_D); 1711 asm_fpmul(as, ir);
1204 } else { 1712 } else
1713#endif
1714 {
1205 Reg dest = ra_dest(as, ir, RSET_GPR); 1715 Reg dest = ra_dest(as, ir, RSET_GPR);
1206 Reg right, left = ra_alloc2(as, ir, RSET_GPR); 1716 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1207 right = (left >> 8); left &= 255; 1717 right = (left >> 8); left &= 255;
1208 emit_dst(as, MIPSI_MUL, dest, left, right); 1718 if (LJ_64 && irt_is64(ir->t)) {
1719 emit_dst(as, MIPSI_MFLO, dest, 0, 0);
1720 emit_dst(as, MIPSI_DMULT, 0, left, right);
1721 } else {
1722 emit_dst(as, MIPSI_MUL, dest, left, right);
1723 }
1209 } 1724 }
1210} 1725}
1211 1726
1727static void asm_mod(ASMState *as, IRIns *ir)
1728{
1729#if LJ_64 && LJ_HASFFI
1730 if (!irt_isint(ir->t))
1731 asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 :
1732 IRCALL_lj_carith_modu64);
1733 else
1734#endif
1735 asm_callid(as, ir, IRCALL_lj_vm_modi);
1736}
1737
1738#if !LJ_SOFTFP32
1739static void asm_pow(ASMState *as, IRIns *ir)
1740{
1741#if LJ_64 && LJ_HASFFI
1742 if (!irt_isnum(ir->t))
1743 asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 :
1744 IRCALL_lj_carith_powu64);
1745 else
1746#endif
1747 asm_callid(as, ir, IRCALL_lj_vm_powi);
1748}
1749
1750static void asm_div(ASMState *as, IRIns *ir)
1751{
1752#if LJ_64 && LJ_HASFFI
1753 if (!irt_isnum(ir->t))
1754 asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 :
1755 IRCALL_lj_carith_divu64);
1756 else
1757#endif
1758#if !LJ_SOFTFP
1759 asm_fparith(as, ir, MIPSI_DIV_D);
1760#else
1761 asm_callid(as, ir, IRCALL_softfp_div);
1762#endif
1763}
1764#endif
1765
1212static void asm_neg(ASMState *as, IRIns *ir) 1766static void asm_neg(ASMState *as, IRIns *ir)
1213{ 1767{
1768#if !LJ_SOFTFP
1214 if (irt_isnum(ir->t)) { 1769 if (irt_isnum(ir->t)) {
1215 asm_fpunary(as, ir, MIPSI_NEG_D); 1770 asm_fpunary(as, ir, MIPSI_NEG_D);
1216 } else { 1771 } else
1772#elif LJ_64 /* && LJ_SOFTFP */
1773 if (irt_isnum(ir->t)) {
1774 Reg dest = ra_dest(as, ir, RSET_GPR);
1775 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1776 emit_dst(as, MIPSI_XOR, dest, left,
1777 ra_allock(as, 0x8000000000000000ll, rset_exclude(RSET_GPR, dest)));
1778 } else
1779#endif
1780 {
1217 Reg dest = ra_dest(as, ir, RSET_GPR); 1781 Reg dest = ra_dest(as, ir, RSET_GPR);
1218 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); 1782 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1219 emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); 1783 emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest,
1784 RID_ZERO, left);
1220 } 1785 }
1221} 1786}
1222 1787
1788#if !LJ_SOFTFP
1789#define asm_abs(as, ir) asm_fpunary(as, ir, MIPSI_ABS_D)
1790#elif LJ_64 /* && LJ_SOFTFP */
1791static void asm_abs(ASMState *as, IRIns *ir)
1792{
1793 Reg dest = ra_dest(as, ir, RSET_GPR);
1794 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1795 emit_tsml(as, MIPSI_DEXTM, dest, left, 30, 0);
1796}
1797#endif
1798
1799#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
1800#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
1801
1223static void asm_arithov(ASMState *as, IRIns *ir) 1802static void asm_arithov(ASMState *as, IRIns *ir)
1224{ 1803{
1225 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR); 1804 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);
1805 lua_assert(!irt_is64(ir->t));
1226 if (irref_isk(ir->op2)) { 1806 if (irref_isk(ir->op2)) {
1227 int k = IR(ir->op2)->i; 1807 int k = IR(ir->op2)->i;
1228 if (ir->o == IR_SUBOV) k = -k; 1808 if (ir->o == IR_SUBOV) k = -k;
@@ -1253,16 +1833,24 @@ static void asm_arithov(ASMState *as, IRIns *ir)
1253 emit_move(as, RID_TMP, dest == left ? left : right); 1833 emit_move(as, RID_TMP, dest == left ? left : right);
1254} 1834}
1255 1835
1836#define asm_addov(as, ir) asm_arithov(as, ir)
1837#define asm_subov(as, ir) asm_arithov(as, ir)
1838
1256static void asm_mulov(ASMState *as, IRIns *ir) 1839static void asm_mulov(ASMState *as, IRIns *ir)
1257{ 1840{
1258#if LJ_DUALNUM 1841 Reg dest = ra_dest(as, ir, RSET_GPR);
1259#error "NYI: MULOV" 1842 Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);
1260#else 1843 right = (left >> 8); left &= 255;
1261 UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused in single-number mode. */ 1844 tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),
1262#endif 1845 right), dest));
1846 asm_guard(as, MIPSI_BNE, RID_TMP, tmp);
1847 emit_dta(as, MIPSI_SRA, RID_TMP, dest, 31);
1848 emit_dst(as, MIPSI_MFHI, tmp, 0, 0);
1849 emit_dst(as, MIPSI_MFLO, dest, 0, 0);
1850 emit_dst(as, MIPSI_MULT, 0, left, right);
1263} 1851}
1264 1852
1265#if LJ_HASFFI 1853#if LJ_32 && LJ_HASFFI
1266static void asm_add64(ASMState *as, IRIns *ir) 1854static void asm_add64(ASMState *as, IRIns *ir)
1267{ 1855{
1268 Reg dest = ra_dest(as, ir, RSET_GPR); 1856 Reg dest = ra_dest(as, ir, RSET_GPR);
@@ -1346,7 +1934,7 @@ static void asm_neg64(ASMState *as, IRIns *ir)
1346} 1934}
1347#endif 1935#endif
1348 1936
1349static void asm_bitnot(ASMState *as, IRIns *ir) 1937static void asm_bnot(ASMState *as, IRIns *ir)
1350{ 1938{
1351 Reg left, right, dest = ra_dest(as, ir, RSET_GPR); 1939 Reg left, right, dest = ra_dest(as, ir, RSET_GPR);
1352 IRIns *irl = IR(ir->op1); 1940 IRIns *irl = IR(ir->op1);
@@ -1360,11 +1948,12 @@ static void asm_bitnot(ASMState *as, IRIns *ir)
1360 emit_dst(as, MIPSI_NOR, dest, left, right); 1948 emit_dst(as, MIPSI_NOR, dest, left, right);
1361} 1949}
1362 1950
1363static void asm_bitswap(ASMState *as, IRIns *ir) 1951static void asm_bswap(ASMState *as, IRIns *ir)
1364{ 1952{
1365 Reg dest = ra_dest(as, ir, RSET_GPR); 1953 Reg dest = ra_dest(as, ir, RSET_GPR);
1366 Reg left = ra_alloc1(as, ir->op1, RSET_GPR); 1954 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1367 if ((as->flags & JIT_F_MIPS32R2)) { 1955#if LJ_32
1956 if ((as->flags & JIT_F_MIPSXXR2)) {
1368 emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16); 1957 emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);
1369 emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left); 1958 emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);
1370 } else { 1959 } else {
@@ -1379,6 +1968,15 @@ static void asm_bitswap(ASMState *as, IRIns *ir)
1379 emit_dta(as, MIPSI_SRL, tmp, left, 24); 1968 emit_dta(as, MIPSI_SRL, tmp, left, 24);
1380 emit_dta(as, MIPSI_SLL, RID_TMP, left, 24); 1969 emit_dta(as, MIPSI_SLL, RID_TMP, left, 24);
1381 } 1970 }
1971#else
1972 if (irt_is64(ir->t)) {
1973 emit_dst(as, MIPSI_DSHD, dest, 0, RID_TMP);
1974 emit_dst(as, MIPSI_DSBH, RID_TMP, 0, left);
1975 } else {
1976 emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);
1977 emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);
1978 }
1979#endif
1382} 1980}
1383 1981
1384static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) 1982static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
@@ -1386,7 +1984,7 @@ static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
1386 Reg dest = ra_dest(as, ir, RSET_GPR); 1984 Reg dest = ra_dest(as, ir, RSET_GPR);
1387 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); 1985 Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1388 if (irref_isk(ir->op2)) { 1986 if (irref_isk(ir->op2)) {
1389 int32_t k = IR(ir->op2)->i; 1987 intptr_t k = get_kval(IR(ir->op2));
1390 if (checku16(k)) { 1988 if (checku16(k)) {
1391 emit_tsi(as, mik, dest, left, k); 1989 emit_tsi(as, mik, dest, left, k);
1392 return; 1990 return;
@@ -1396,22 +1994,34 @@ static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
1396 emit_dst(as, mi, dest, left, right); 1994 emit_dst(as, mi, dest, left, right);
1397} 1995}
1398 1996
1997#define asm_band(as, ir) asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI)
1998#define asm_bor(as, ir) asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI)
1999#define asm_bxor(as, ir) asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI)
2000
1399static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) 2001static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
1400{ 2002{
1401 Reg dest = ra_dest(as, ir, RSET_GPR); 2003 Reg dest = ra_dest(as, ir, RSET_GPR);
1402 if (irref_isk(ir->op2)) { /* Constant shifts. */ 2004 if (irref_isk(ir->op2)) { /* Constant shifts. */
1403 uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); 2005 uint32_t shift = (uint32_t)IR(ir->op2)->i;
1404 emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), shift); 2006 if (LJ_64 && irt_is64(ir->t)) mik |= (shift & 32) ? MIPSI_D32 : MIPSI_D;
2007 emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR),
2008 (shift & 31));
1405 } else { 2009 } else {
1406 Reg right, left = ra_alloc2(as, ir, RSET_GPR); 2010 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1407 right = (left >> 8); left &= 255; 2011 right = (left >> 8); left &= 255;
2012 if (LJ_64 && irt_is64(ir->t)) mi |= MIPSI_DV;
1408 emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */ 2013 emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */
1409 } 2014 }
1410} 2015}
1411 2016
1412static void asm_bitror(ASMState *as, IRIns *ir) 2017#define asm_bshl(as, ir) asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL)
2018#define asm_bshr(as, ir) asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL)
2019#define asm_bsar(as, ir) asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA)
2020#define asm_brol(as, ir) lua_assert(0)
2021
2022static void asm_bror(ASMState *as, IRIns *ir)
1413{ 2023{
1414 if ((as->flags & JIT_F_MIPS32R2)) { 2024 if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) {
1415 asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR); 2025 asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR);
1416 } else { 2026 } else {
1417 Reg dest = ra_dest(as, ir, RSET_GPR); 2027 Reg dest = ra_dest(as, ir, RSET_GPR);
@@ -1430,9 +2040,34 @@ static void asm_bitror(ASMState *as, IRIns *ir)
1430 } 2040 }
1431} 2041}
1432 2042
2043#if LJ_SOFTFP
2044static void asm_sfpmin_max(ASMState *as, IRIns *ir)
2045{
2046 CCallInfo ci = lj_ir_callinfo[(IROp)ir->o == IR_MIN ? IRCALL_lj_vm_sfmin : IRCALL_lj_vm_sfmax];
2047#if LJ_64
2048 IRRef args[2];
2049 args[0] = ir->op1;
2050 args[1] = ir->op2;
2051#else
2052 IRRef args[4];
2053 args[0^LJ_BE] = ir->op1;
2054 args[1^LJ_BE] = (ir+1)->op1;
2055 args[2^LJ_BE] = ir->op2;
2056 args[3^LJ_BE] = (ir+1)->op2;
2057#endif
2058 asm_setupresult(as, ir, &ci);
2059 emit_call(as, (void *)ci.func, 0);
2060 ci.func = NULL;
2061 asm_gencall(as, &ci, args);
2062}
2063#endif
2064
1433static void asm_min_max(ASMState *as, IRIns *ir, int ismax) 2065static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1434{ 2066{
1435 if (irt_isnum(ir->t)) { 2067 if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
2068#if LJ_SOFTFP
2069 asm_sfpmin_max(as, ir);
2070#else
1436 Reg dest = ra_dest(as, ir, RSET_FPR); 2071 Reg dest = ra_dest(as, ir, RSET_FPR);
1437 Reg right, left = ra_alloc2(as, ir, RSET_FPR); 2072 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1438 right = (left >> 8); left &= 255; 2073 right = (left >> 8); left &= 255;
@@ -1443,6 +2078,7 @@ static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1443 if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right); 2078 if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right);
1444 } 2079 }
1445 emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left); 2080 emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left);
2081#endif
1446 } else { 2082 } else {
1447 Reg dest = ra_dest(as, ir, RSET_GPR); 2083 Reg dest = ra_dest(as, ir, RSET_GPR);
1448 Reg right, left = ra_alloc2(as, ir, RSET_GPR); 2084 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
@@ -1458,27 +2094,101 @@ static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1458 } 2094 }
1459} 2095}
1460 2096
2097#define asm_min(as, ir) asm_min_max(as, ir, 0)
2098#define asm_max(as, ir) asm_min_max(as, ir, 1)
2099
1461/* -- Comparisons --------------------------------------------------------- */ 2100/* -- Comparisons --------------------------------------------------------- */
1462 2101
2102#if LJ_SOFTFP
2103/* SFP comparisons. */
2104static void asm_sfpcomp(ASMState *as, IRIns *ir)
2105{
2106 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];
2107 RegSet drop = RSET_SCRATCH;
2108 Reg r;
2109#if LJ_64
2110 IRRef args[2];
2111 args[0] = ir->op1;
2112 args[1] = ir->op2;
2113#else
2114 IRRef args[4];
2115 args[LJ_LE ? 0 : 1] = ir->op1; args[LJ_LE ? 1 : 0] = (ir+1)->op1;
2116 args[LJ_LE ? 2 : 3] = ir->op2; args[LJ_LE ? 3 : 2] = (ir+1)->op2;
2117#endif
2118
2119 for (r = REGARG_FIRSTGPR; r <= REGARG_FIRSTGPR+(LJ_64?1:3); r++) {
2120 if (!rset_test(as->freeset, r) &&
2121 regcost_ref(as->cost[r]) == args[r-REGARG_FIRSTGPR])
2122 rset_clear(drop, r);
2123 }
2124 ra_evictset(as, drop);
2125
2126 asm_setupresult(as, ir, ci);
2127
2128 switch ((IROp)ir->o) {
2129 case IR_LT:
2130 asm_guard(as, MIPSI_BGEZ, RID_RET, 0);
2131 break;
2132 case IR_ULT:
2133 asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
2134 emit_loadi(as, RID_TMP, 1);
2135 asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO);
2136 break;
2137 case IR_GE:
2138 asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
2139 emit_loadi(as, RID_TMP, 2);
2140 asm_guard(as, MIPSI_BLTZ, RID_RET, 0);
2141 break;
2142 case IR_LE:
2143 asm_guard(as, MIPSI_BGTZ, RID_RET, 0);
2144 break;
2145 case IR_GT:
2146 asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
2147 emit_loadi(as, RID_TMP, 2);
2148 asm_guard(as, MIPSI_BLEZ, RID_RET, 0);
2149 break;
2150 case IR_UGE:
2151 asm_guard(as, MIPSI_BLTZ, RID_RET, 0);
2152 break;
2153 case IR_ULE:
2154 asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
2155 emit_loadi(as, RID_TMP, 1);
2156 break;
2157 case IR_UGT: case IR_ABC:
2158 asm_guard(as, MIPSI_BLEZ, RID_RET, 0);
2159 break;
2160 case IR_EQ: case IR_NE:
2161 asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_RET, RID_ZERO);
2162 default:
2163 break;
2164 }
2165 asm_gencall(as, ci, args);
2166}
2167#endif
2168
1463static void asm_comp(ASMState *as, IRIns *ir) 2169static void asm_comp(ASMState *as, IRIns *ir)
1464{ 2170{
1465 /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ 2171 /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
1466 IROp op = ir->o; 2172 IROp op = ir->o;
1467 if (irt_isnum(ir->t)) { 2173 if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
2174#if LJ_SOFTFP
2175 asm_sfpcomp(as, ir);
2176#else
1468 Reg right, left = ra_alloc2(as, ir, RSET_FPR); 2177 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1469 right = (left >> 8); left &= 255; 2178 right = (left >> 8); left &= 255;
1470 asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); 2179 asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
1471 emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right); 2180 emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right);
2181#endif
1472 } else { 2182 } else {
1473 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); 2183 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1474 if (op == IR_ABC) op = IR_UGT; 2184 if (op == IR_ABC) op = IR_UGT;
1475 if ((op&4) == 0 && irref_isk(ir->op2) && IR(ir->op2)->i == 0) { 2185 if ((op&4) == 0 && irref_isk(ir->op2) && get_kval(IR(ir->op2)) == 0) {
1476 MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) : 2186 MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) :
1477 ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ); 2187 ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ);
1478 asm_guard(as, mi, left, 0); 2188 asm_guard(as, mi, left, 0);
1479 } else { 2189 } else {
1480 if (irref_isk(ir->op2)) { 2190 if (irref_isk(ir->op2)) {
1481 int32_t k = IR(ir->op2)->i; 2191 intptr_t k = get_kval(IR(ir->op2));
1482 if ((op&2)) k++; 2192 if ((op&2)) k++;
1483 if (checki16(k)) { 2193 if (checki16(k)) {
1484 asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); 2194 asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
@@ -1495,19 +2205,24 @@ static void asm_comp(ASMState *as, IRIns *ir)
1495 } 2205 }
1496} 2206}
1497 2207
1498static void asm_compeq(ASMState *as, IRIns *ir) 2208static void asm_equal(ASMState *as, IRIns *ir)
1499{ 2209{
1500 Reg right, left = ra_alloc2(as, ir, irt_isnum(ir->t) ? RSET_FPR : RSET_GPR); 2210 Reg right, left = ra_alloc2(as, ir, (!LJ_SOFTFP && irt_isnum(ir->t)) ?
2211 RSET_FPR : RSET_GPR);
1501 right = (left >> 8); left &= 255; 2212 right = (left >> 8); left &= 255;
1502 if (irt_isnum(ir->t)) { 2213 if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
2214#if LJ_SOFTFP
2215 asm_sfpcomp(as, ir);
2216#else
1503 asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); 2217 asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
1504 emit_fgh(as, MIPSI_C_EQ_D, 0, left, right); 2218 emit_fgh(as, MIPSI_C_EQ_D, 0, left, right);
2219#endif
1505 } else { 2220 } else {
1506 asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right); 2221 asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right);
1507 } 2222 }
1508} 2223}
1509 2224
1510#if LJ_HASFFI 2225#if LJ_32 && LJ_HASFFI
1511/* 64 bit integer comparisons. */ 2226/* 64 bit integer comparisons. */
1512static void asm_comp64(ASMState *as, IRIns *ir) 2227static void asm_comp64(ASMState *as, IRIns *ir)
1513{ 2228{
@@ -1549,41 +2264,79 @@ static void asm_comp64eq(ASMState *as, IRIns *ir)
1549/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ 2264/* Hiword op of a split 64 bit op. Previous op must be the loword op. */
1550static void asm_hiop(ASMState *as, IRIns *ir) 2265static void asm_hiop(ASMState *as, IRIns *ir)
1551{ 2266{
1552#if LJ_HASFFI 2267#if LJ_32 && (LJ_HASFFI || LJ_SOFTFP)
1553 /* HIOP is marked as a store because it needs its own DCE logic. */ 2268 /* HIOP is marked as a store because it needs its own DCE logic. */
1554 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ 2269 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
1555 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; 2270 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
1556 if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ 2271 if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
1557 as->curins--; /* Always skip the CONV. */ 2272 as->curins--; /* Always skip the CONV. */
2273#if LJ_HASFFI && !LJ_SOFTFP
1558 if (usehi || uselo) 2274 if (usehi || uselo)
1559 asm_conv64(as, ir); 2275 asm_conv64(as, ir);
1560 return; 2276 return;
2277#endif
1561 } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */ 2278 } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */
1562 as->curins--; /* Always skip the loword comparison. */ 2279 as->curins--; /* Always skip the loword comparison. */
2280#if LJ_SOFTFP
2281 if (!irt_isint(ir->t)) {
2282 asm_sfpcomp(as, ir-1);
2283 return;
2284 }
2285#endif
2286#if LJ_HASFFI
1563 asm_comp64(as, ir); 2287 asm_comp64(as, ir);
2288#endif
1564 return; 2289 return;
1565 } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ 2290 } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
1566 as->curins--; /* Always skip the loword comparison. */ 2291 as->curins--; /* Always skip the loword comparison. */
2292#if LJ_SOFTFP
2293 if (!irt_isint(ir->t)) {
2294 asm_sfpcomp(as, ir-1);
2295 return;
2296 }
2297#endif
2298#if LJ_HASFFI
1567 asm_comp64eq(as, ir); 2299 asm_comp64eq(as, ir);
2300#endif
2301 return;
2302#if LJ_SOFTFP
2303 } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) {
2304 as->curins--; /* Always skip the loword min/max. */
2305 if (uselo || usehi)
2306 asm_sfpmin_max(as, ir-1);
1568 return; 2307 return;
2308#endif
1569 } else if ((ir-1)->o == IR_XSTORE) { 2309 } else if ((ir-1)->o == IR_XSTORE) {
1570 as->curins--; /* Handle both stores here. */ 2310 as->curins--; /* Handle both stores here. */
1571 if ((ir-1)->r != RID_SINK) { 2311 if ((ir-1)->r != RID_SINK) {
1572 asm_xstore(as, ir, LJ_LE ? 4 : 0); 2312 asm_xstore_(as, ir, LJ_LE ? 4 : 0);
1573 asm_xstore(as, ir-1, LJ_LE ? 0 : 4); 2313 asm_xstore_(as, ir-1, LJ_LE ? 0 : 4);
1574 } 2314 }
1575 return; 2315 return;
1576 } 2316 }
1577 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ 2317 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
1578 switch ((ir-1)->o) { 2318 switch ((ir-1)->o) {
2319#if LJ_HASFFI
1579 case IR_ADD: as->curins--; asm_add64(as, ir); break; 2320 case IR_ADD: as->curins--; asm_add64(as, ir); break;
1580 case IR_SUB: as->curins--; asm_sub64(as, ir); break; 2321 case IR_SUB: as->curins--; asm_sub64(as, ir); break;
1581 case IR_NEG: as->curins--; asm_neg64(as, ir); break; 2322 case IR_NEG: as->curins--; asm_neg64(as, ir); break;
2323#endif
2324#if LJ_SOFTFP
2325 case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
2326 case IR_STRTO:
2327 if (!uselo)
2328 ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */
2329 break;
2330#endif
1582 case IR_CALLN: 2331 case IR_CALLN:
2332 case IR_CALLS:
1583 case IR_CALLXS: 2333 case IR_CALLXS:
1584 if (!uselo) 2334 if (!uselo)
1585 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ 2335 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
1586 break; 2336 break;
2337#if LJ_SOFTFP
2338 case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR:
2339#endif
1587 case IR_CNEWI: 2340 case IR_CNEWI:
1588 /* Nothing to do here. Handled by lo op itself. */ 2341 /* Nothing to do here. Handled by lo op itself. */
1589 break; 2342 break;
@@ -1594,6 +2347,17 @@ static void asm_hiop(ASMState *as, IRIns *ir)
1594#endif 2347#endif
1595} 2348}
1596 2349
2350/* -- Profiling ----------------------------------------------------------- */
2351
2352static void asm_prof(ASMState *as, IRIns *ir)
2353{
2354 UNUSED(ir);
2355 asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);
2356 emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, HOOK_PROFILE);
2357 emit_lsglptr(as, MIPSI_LBU, RID_TMP,
2358 (int32_t)offsetof(global_State, hookmask));
2359}
2360
1597/* -- Stack handling ------------------------------------------------------ */ 2361/* -- Stack handling ------------------------------------------------------ */
1598 2362
1599/* Check Lua stack size for overflow. Use exit handler as fallback. */ 2363/* Check Lua stack size for overflow. Use exit handler as fallback. */
@@ -1604,46 +2368,67 @@ static void asm_stack_check(ASMState *as, BCReg topslot,
1604 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; 2368 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
1605 ExitNo oldsnap = as->snapno; 2369 ExitNo oldsnap = as->snapno;
1606 rset_clear(allow, pbase); 2370 rset_clear(allow, pbase);
2371#if LJ_32
1607 tmp = allow ? rset_pickbot(allow) : 2372 tmp = allow ? rset_pickbot(allow) :
1608 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); 2373 (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
2374#else
2375 tmp = allow ? rset_pickbot(allow) : RID_RET;
2376#endif
1609 as->snapno = exitno; 2377 as->snapno = exitno;
1610 asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO); 2378 asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);
1611 as->snapno = oldsnap; 2379 as->snapno = oldsnap;
1612 if (allow == RSET_EMPTY) /* Restore temp. register. */ 2380 if (allow == RSET_EMPTY) /* Restore temp. register. */
1613 emit_tsi(as, MIPSI_LW, tmp, RID_SP, 0); 2381 emit_tsi(as, MIPSI_AL, tmp, RID_SP, 0);
1614 else 2382 else
1615 ra_modified(as, tmp); 2383 ra_modified(as, tmp);
1616 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot)); 2384 emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot));
1617 emit_dst(as, MIPSI_SUBU, RID_TMP, tmp, pbase); 2385 emit_dst(as, MIPSI_ASUBU, RID_TMP, tmp, pbase);
1618 emit_tsi(as, MIPSI_LW, tmp, tmp, offsetof(lua_State, maxstack)); 2386 emit_tsi(as, MIPSI_AL, tmp, tmp, offsetof(lua_State, maxstack));
1619 if (pbase == RID_TMP) 2387 if (pbase == RID_TMP)
1620 emit_getgl(as, RID_TMP, jit_base); 2388 emit_getgl(as, RID_TMP, jit_base);
1621 emit_getgl(as, tmp, jit_L); 2389 emit_getgl(as, tmp, cur_L);
1622 if (allow == RSET_EMPTY) /* Spill temp. register. */ 2390 if (allow == RSET_EMPTY) /* Spill temp. register. */
1623 emit_tsi(as, MIPSI_SW, tmp, RID_SP, 0); 2391 emit_tsi(as, MIPSI_AS, tmp, RID_SP, 0);
1624} 2392}
1625 2393
1626/* Restore Lua stack from on-trace state. */ 2394/* Restore Lua stack from on-trace state. */
1627static void asm_stack_restore(ASMState *as, SnapShot *snap) 2395static void asm_stack_restore(ASMState *as, SnapShot *snap)
1628{ 2396{
1629 SnapEntry *map = &as->T->snapmap[snap->mapofs]; 2397 SnapEntry *map = &as->T->snapmap[snap->mapofs];
1630 SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; 2398#if LJ_32 || defined(LUA_USE_ASSERT)
2399 SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2];
2400#endif
1631 MSize n, nent = snap->nent; 2401 MSize n, nent = snap->nent;
1632 /* Store the value of all modified slots to the Lua stack. */ 2402 /* Store the value of all modified slots to the Lua stack. */
1633 for (n = 0; n < nent; n++) { 2403 for (n = 0; n < nent; n++) {
1634 SnapEntry sn = map[n]; 2404 SnapEntry sn = map[n];
1635 BCReg s = snap_slot(sn); 2405 BCReg s = snap_slot(sn);
1636 int32_t ofs = 8*((int32_t)s-1); 2406 int32_t ofs = 8*((int32_t)s-1-LJ_FR2);
1637 IRRef ref = snap_ref(sn); 2407 IRRef ref = snap_ref(sn);
1638 IRIns *ir = IR(ref); 2408 IRIns *ir = IR(ref);
1639 if ((sn & SNAP_NORESTORE)) 2409 if ((sn & SNAP_NORESTORE))
1640 continue; 2410 continue;
1641 if (irt_isnum(ir->t)) { 2411 if (irt_isnum(ir->t)) {
2412#if LJ_SOFTFP32
2413 Reg tmp;
2414 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
2415 lua_assert(irref_isk(ref)); /* LJ_SOFTFP: must be a number constant. */
2416 tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, allow);
2417 emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?4:0));
2418 if (rset_test(as->freeset, tmp+1)) allow = RID2RSET(tmp+1);
2419 tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, allow);
2420 emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?0:4));
2421#elif LJ_SOFTFP /* && LJ_64 */
2422 Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE));
2423 emit_tsi(as, MIPSI_SD, src, RID_BASE, ofs);
2424#else
1642 Reg src = ra_alloc1(as, ref, RSET_FPR); 2425 Reg src = ra_alloc1(as, ref, RSET_FPR);
1643 emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs); 2426 emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs);
2427#endif
1644 } else { 2428 } else {
1645 Reg type; 2429#if LJ_32
1646 RegSet allow = rset_exclude(RSET_GPR, RID_BASE); 2430 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
2431 Reg type;
1647 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); 2432 lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t));
1648 if (!irt_ispri(ir->t)) { 2433 if (!irt_ispri(ir->t)) {
1649 Reg src = ra_alloc1(as, ref, allow); 2434 Reg src = ra_alloc1(as, ref, allow);
@@ -1653,10 +2438,17 @@ static void asm_stack_restore(ASMState *as, SnapShot *snap)
1653 if ((sn & (SNAP_CONT|SNAP_FRAME))) { 2438 if ((sn & (SNAP_CONT|SNAP_FRAME))) {
1654 if (s == 0) continue; /* Do not overwrite link to previous frame. */ 2439 if (s == 0) continue; /* Do not overwrite link to previous frame. */
1655 type = ra_allock(as, (int32_t)(*flinks--), allow); 2440 type = ra_allock(as, (int32_t)(*flinks--), allow);
2441#if LJ_SOFTFP
2442 } else if ((sn & SNAP_SOFTFPNUM)) {
2443 type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPR, RID_BASE));
2444#endif
1656 } else { 2445 } else {
1657 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); 2446 type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
1658 } 2447 }
1659 emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4)); 2448 emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4));
2449#else
2450 asm_tvstore64(as, RID_BASE, ofs, ref);
2451#endif
1660 } 2452 }
1661 checkmclim(as); 2453 checkmclim(as);
1662 } 2454 }
@@ -1680,7 +2472,7 @@ static void asm_gc_check(ASMState *as)
1680 args[0] = ASMREF_TMP1; /* global_State *g */ 2472 args[0] = ASMREF_TMP1; /* global_State *g */
1681 args[1] = ASMREF_TMP2; /* MSize steps */ 2473 args[1] = ASMREF_TMP2; /* MSize steps */
1682 asm_gencall(as, ci, args); 2474 asm_gencall(as, ci, args);
1683 emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); 2475 emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
1684 tmp = ra_releasetmp(as, ASMREF_TMP2); 2476 tmp = ra_releasetmp(as, ASMREF_TMP2);
1685 emit_loadi(as, tmp, as->gcsteps); 2477 emit_loadi(as, tmp, as->gcsteps);
1686 /* Jump around GC step if GC total < GC threshold. */ 2478 /* Jump around GC step if GC total < GC threshold. */
@@ -1755,7 +2547,7 @@ static void asm_tail_fixup(ASMState *as, TraceNo lnk)
1755 MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp; 2547 MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp;
1756 int32_t spadj = as->T->spadjust; 2548 int32_t spadj = as->T->spadjust;
1757 MCode *p = as->mctop-1; 2549 MCode *p = as->mctop-1;
1758 *p = spadj ? (MIPSI_ADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP; 2550 *p = spadj ? (MIPSI_AADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP;
1759 p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); 2551 p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
1760} 2552}
1761 2553
@@ -1766,139 +2558,26 @@ static void asm_tail_prep(ASMState *as)
1766 as->invmcp = as->loopref ? as->mcp : NULL; 2558 as->invmcp = as->loopref ? as->mcp : NULL;
1767} 2559}
1768 2560
1769/* -- Instruction dispatch ------------------------------------------------ */
1770
1771/* Assemble a single instruction. */
1772static void asm_ir(ASMState *as, IRIns *ir)
1773{
1774 switch ((IROp)ir->o) {
1775 /* Miscellaneous ops. */
1776 case IR_LOOP: asm_loop(as); break;
1777 case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break;
1778 case IR_USE:
1779 ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break;
1780 case IR_PHI: asm_phi(as, ir); break;
1781 case IR_HIOP: asm_hiop(as, ir); break;
1782 case IR_GCSTEP: asm_gcstep(as, ir); break;
1783
1784 /* Guarded assertions. */
1785 case IR_EQ: case IR_NE: asm_compeq(as, ir); break;
1786 case IR_LT: case IR_GE: case IR_LE: case IR_GT:
1787 case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT:
1788 case IR_ABC:
1789 asm_comp(as, ir);
1790 break;
1791
1792 case IR_RETF: asm_retf(as, ir); break;
1793
1794 /* Bit ops. */
1795 case IR_BNOT: asm_bitnot(as, ir); break;
1796 case IR_BSWAP: asm_bitswap(as, ir); break;
1797
1798 case IR_BAND: asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI); break;
1799 case IR_BOR: asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI); break;
1800 case IR_BXOR: asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI); break;
1801
1802 case IR_BSHL: asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL); break;
1803 case IR_BSHR: asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL); break;
1804 case IR_BSAR: asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA); break;
1805 case IR_BROL: lua_assert(0); break;
1806 case IR_BROR: asm_bitror(as, ir); break;
1807
1808 /* Arithmetic ops. */
1809 case IR_ADD: asm_add(as, ir); break;
1810 case IR_SUB: asm_sub(as, ir); break;
1811 case IR_MUL: asm_mul(as, ir); break;
1812 case IR_DIV: asm_fparith(as, ir, MIPSI_DIV_D); break;
1813 case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break;
1814 case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break;
1815 case IR_NEG: asm_neg(as, ir); break;
1816
1817 case IR_ABS: asm_fpunary(as, ir, MIPSI_ABS_D); break;
1818 case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break;
1819 case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break;
1820 case IR_MIN: asm_min_max(as, ir, 0); break;
1821 case IR_MAX: asm_min_max(as, ir, 1); break;
1822 case IR_FPMATH:
1823 if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir))
1824 break;
1825 if (ir->op2 <= IRFPM_TRUNC)
1826 asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2);
1827 else if (ir->op2 == IRFPM_SQRT)
1828 asm_fpunary(as, ir, MIPSI_SQRT_D);
1829 else
1830 asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
1831 break;
1832
1833 /* Overflow-checking arithmetic ops. */
1834 case IR_ADDOV: asm_arithov(as, ir); break;
1835 case IR_SUBOV: asm_arithov(as, ir); break;
1836 case IR_MULOV: asm_mulov(as, ir); break;
1837
1838 /* Memory references. */
1839 case IR_AREF: asm_aref(as, ir); break;
1840 case IR_HREF: asm_href(as, ir); break;
1841 case IR_HREFK: asm_hrefk(as, ir); break;
1842 case IR_NEWREF: asm_newref(as, ir); break;
1843 case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break;
1844 case IR_FREF: asm_fref(as, ir); break;
1845 case IR_STRREF: asm_strref(as, ir); break;
1846
1847 /* Loads and stores. */
1848 case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
1849 asm_ahuvload(as, ir);
1850 break;
1851 case IR_FLOAD: asm_fload(as, ir); break;
1852 case IR_XLOAD: asm_xload(as, ir); break;
1853 case IR_SLOAD: asm_sload(as, ir); break;
1854
1855 case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break;
1856 case IR_FSTORE: asm_fstore(as, ir); break;
1857 case IR_XSTORE: asm_xstore(as, ir, 0); break;
1858
1859 /* Allocations. */
1860 case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break;
1861 case IR_TNEW: asm_tnew(as, ir); break;
1862 case IR_TDUP: asm_tdup(as, ir); break;
1863 case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break;
1864
1865 /* Write barriers. */
1866 case IR_TBAR: asm_tbar(as, ir); break;
1867 case IR_OBAR: asm_obar(as, ir); break;
1868
1869 /* Type conversions. */
1870 case IR_CONV: asm_conv(as, ir); break;
1871 case IR_TOBIT: asm_tobit(as, ir); break;
1872 case IR_TOSTR: asm_tostr(as, ir); break;
1873 case IR_STRTO: asm_strto(as, ir); break;
1874
1875 /* Calls. */
1876 case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break;
1877 case IR_CALLXS: asm_callx(as, ir); break;
1878 case IR_CARG: break;
1879
1880 default:
1881 setintV(&as->J->errinfo, ir->o);
1882 lj_trace_err_info(as->J, LJ_TRERR_NYIIR);
1883 break;
1884 }
1885}
1886
1887/* -- Trace setup --------------------------------------------------------- */ 2561/* -- Trace setup --------------------------------------------------------- */
1888 2562
1889/* Ensure there are enough stack slots for call arguments. */ 2563/* Ensure there are enough stack slots for call arguments. */
1890static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) 2564static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
1891{ 2565{
1892 IRRef args[CCI_NARGS_MAX*2]; 2566 IRRef args[CCI_NARGS_MAX*2];
1893 uint32_t i, nargs = (int)CCI_NARGS(ci); 2567 uint32_t i, nargs = CCI_XNARGS(ci);
2568#if LJ_32
1894 int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; 2569 int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
2570#else
2571 int nslots = 0, ngpr = REGARG_NUMGPR;
2572#endif
1895 asm_collectargs(as, ir, ci, args); 2573 asm_collectargs(as, ir, ci, args);
1896 for (i = 0; i < nargs; i++) { 2574 for (i = 0; i < nargs; i++) {
1897 if (args[i] && irt_isfp(IR(args[i])->t) && 2575#if LJ_32
2576 if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t) &&
1898 nfpr > 0 && !(ci->flags & CCI_VARARG)) { 2577 nfpr > 0 && !(ci->flags & CCI_VARARG)) {
1899 nfpr--; 2578 nfpr--;
1900 ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1; 2579 ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1;
1901 } else if (args[i] && irt_isnum(IR(args[i])->t)) { 2580 } else if (!LJ_SOFTFP && args[i] && irt_isnum(IR(args[i])->t)) {
1902 nfpr = 0; 2581 nfpr = 0;
1903 ngpr = ngpr & ~1; 2582 ngpr = ngpr & ~1;
1904 if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1; 2583 if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1;
@@ -1906,6 +2585,9 @@ static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
1906 nfpr = 0; 2585 nfpr = 0;
1907 if (ngpr > 0) ngpr--; else nslots++; 2586 if (ngpr > 0) ngpr--; else nslots++;
1908 } 2587 }
2588#else
2589 if (ngpr > 0) ngpr--; else nslots += 2;
2590#endif
1909 } 2591 }
1910 if (nslots > as->evenspill) /* Leave room for args in stack slots. */ 2592 if (nslots > as->evenspill) /* Leave room for args in stack slots. */
1911 as->evenspill = nslots; 2593 as->evenspill = nslots;