1 |// Low-level VM code for MIPS64 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
5 |// Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com.
6 |// Sponsored by Cisco Systems, Inc.
9 |.section code_op, code_sub
11 |.actionlist build_actionlist
13 |.globalnames globnames
14 |.externnames extnames
16 |// Note: The ragged indentation of the instructions is intentional.
17 |// The starting columns indicate data dependencies.
19 |//-----------------------------------------------------------------------
21 |// Fixed register assignments for the interpreter.
22 |// Don't use: r0 = 0, r26/r27 = reserved, r28 = gp, r29 = sp, r31 = ra
30 |// The following must be C callee-save (but BASE is often refetched).
31 |.define BASE, r16 // Base of current Lua stack frame.
32 |.define KBASE, r17 // Constants of current Lua function.
33 |.define PC, r18 // Next PC.
34 |.define DISPATCH, r19 // Opcode dispatch table.
35 |.define LREG, r20 // Register holding lua_State (also in SAVE_L).
36 |.define MULTRES, r21 // Size of multi-result: (nresults+1)*8.
38 |.define JGL, r30 // On-trace: global_State + 32768.
40 |// Constants for type-comparisons, stores and conversions. C callee-save.
44 |.define TOBIT, f30 // 2^52 + 2^51.
47 |// The following temporaries are not saved across C calls, except for RA.
48 |.define RA, r23 // Callee-save.
54 |.define AT, r1 // Assembler temporary.
60 |// MIPS n64 calling convention.
61 |.define CFUNCADDR, r25
92 |// Stack layout while in interpreter. Must match with lj_frame.h.
93 |.if FPU // MIPS64 hard-float.
95 |.define CFRAME_SPACE, 192 // Delta for sp.
97 |//----- 16 byte aligned, <-- sp entering interpreter
98 |.define SAVE_ERRF, 188(sp) // 32 bit values.
99 |.define SAVE_NRES, 184(sp)
100 |.define SAVE_CFRAME, 176(sp) // 64 bit values.
101 |.define SAVE_L, 168(sp)
102 |.define SAVE_PC, 160(sp)
103 |//----- 16 byte aligned
104 |.define SAVE_GPR_, 80 // .. 80+10*8: 64 bit GPR saves.
105 |.define SAVE_FPR_, 16 // .. 16+8*8: 64 bit FPR saves.
107 |.else // MIPS64 soft-float
109 |.define CFRAME_SPACE, 128 // Delta for sp.
111 |//----- 16 byte aligned, <-- sp entering interpreter
112 |.define SAVE_ERRF, 124(sp) // 32 bit values.
113 |.define SAVE_NRES, 120(sp)
114 |.define SAVE_CFRAME, 112(sp) // 64 bit values.
115 |.define SAVE_L, 104(sp)
116 |.define SAVE_PC, 96(sp)
117 |//----- 16 byte aligned
118 |.define SAVE_GPR_, 16 // .. 16+10*8: 64 bit GPR saves.
122 |.define TMPX, 8(sp) // Unused by interpreter, temp for JIT code.
124 |//----- 16 byte aligned
128 |.define SAVE_MULTRES, TMPD
130 |//-----------------------------------------------------------------------
133 | daddiu sp, sp, -CFRAME_SPACE
134 | sd ra, SAVE_GPR_+9*8(sp)
135 | sd r30, SAVE_GPR_+8*8(sp)
136 | .FPU sdc1 f31, SAVE_FPR_+7*8(sp)
137 | sd r23, SAVE_GPR_+7*8(sp)
138 | .FPU sdc1 f30, SAVE_FPR_+6*8(sp)
139 | sd r22, SAVE_GPR_+6*8(sp)
140 | .FPU sdc1 f29, SAVE_FPR_+5*8(sp)
141 | sd r21, SAVE_GPR_+5*8(sp)
142 | .FPU sdc1 f28, SAVE_FPR_+4*8(sp)
143 | sd r20, SAVE_GPR_+4*8(sp)
144 | .FPU sdc1 f27, SAVE_FPR_+3*8(sp)
145 | sd r19, SAVE_GPR_+3*8(sp)
146 | .FPU sdc1 f26, SAVE_FPR_+2*8(sp)
147 | sd r18, SAVE_GPR_+2*8(sp)
148 | .FPU sdc1 f25, SAVE_FPR_+1*8(sp)
149 | sd r17, SAVE_GPR_+1*8(sp)
150 | .FPU sdc1 f24, SAVE_FPR_+0*8(sp)
151 | sd r16, SAVE_GPR_+0*8(sp)
154 |.macro restoreregs_ret
155 | ld ra, SAVE_GPR_+9*8(sp)
156 | ld r30, SAVE_GPR_+8*8(sp)
157 | ld r23, SAVE_GPR_+7*8(sp)
158 | .FPU ldc1 f31, SAVE_FPR_+7*8(sp)
159 | ld r22, SAVE_GPR_+6*8(sp)
160 | .FPU ldc1 f30, SAVE_FPR_+6*8(sp)
161 | ld r21, SAVE_GPR_+5*8(sp)
162 | .FPU ldc1 f29, SAVE_FPR_+5*8(sp)
163 | ld r20, SAVE_GPR_+4*8(sp)
164 | .FPU ldc1 f28, SAVE_FPR_+4*8(sp)
165 | ld r19, SAVE_GPR_+3*8(sp)
166 | .FPU ldc1 f27, SAVE_FPR_+3*8(sp)
167 | ld r18, SAVE_GPR_+2*8(sp)
168 | .FPU ldc1 f26, SAVE_FPR_+2*8(sp)
169 | ld r17, SAVE_GPR_+1*8(sp)
170 | .FPU ldc1 f25, SAVE_FPR_+1*8(sp)
171 | ld r16, SAVE_GPR_+0*8(sp)
172 | .FPU ldc1 f24, SAVE_FPR_+0*8(sp)
174 | daddiu sp, sp, CFRAME_SPACE
177 |// Type definitions. Some of these are only used for documentation.
178 |.type L, lua_State, LREG
179 |.type GL, global_State
180 |.type TVALUE, TValue
184 |.type LFUNC, GCfuncL
185 |.type CFUNC, GCfuncC
186 |.type PROTO, GCproto
187 |.type UPVAL, GCupval
190 |.type TRACE, GCtrace
193 |//-----------------------------------------------------------------------
195 |// Trap for not-yet-implemented parts.
196 |.macro NYI; .long 0xec1cf0f0; .endmacro
198 |// Macros to mark delay slots.
199 |.macro ., a; a; .endmacro
200 |.macro ., a,b; a,b; .endmacro
201 |.macro ., a,b,c; a,b,c; .endmacro
202 |.macro ., a,b,c,d; a,b,c,d; .endmacro
204 |.define FRAME_PC, -8
205 |.define FRAME_FUNC, -16
207 |//-----------------------------------------------------------------------
209 |// Endian-specific defines.
224 |// Instruction decode.
225 |.macro decode_OP1, dst, ins; andi dst, ins, 0xff; .endmacro
226 |.macro decode_OP8a, dst, ins; andi dst, ins, 0xff; .endmacro
227 |.macro decode_OP8b, dst; sll dst, dst, 3; .endmacro
228 |.macro decode_RC8a, dst, ins; srl dst, ins, 13; .endmacro
229 |.macro decode_RC8b, dst; andi dst, dst, 0x7f8; .endmacro
230 |.macro decode_RD4b, dst; sll dst, dst, 2; .endmacro
231 |.macro decode_RA8a, dst, ins; srl dst, ins, 5; .endmacro
232 |.macro decode_RA8b, dst; andi dst, dst, 0x7f8; .endmacro
233 |.macro decode_RB8a, dst, ins; srl dst, ins, 21; .endmacro
234 |.macro decode_RB8b, dst; andi dst, dst, 0x7f8; .endmacro
235 |.macro decode_RD8a, dst, ins; srl dst, ins, 16; .endmacro
236 |.macro decode_RD8b, dst; sll dst, dst, 3; .endmacro
237 |.macro decode_RDtoRC8, dst, src; andi dst, src, 0x7f8; .endmacro
239 |// Instruction fetch.
244 |// Instruction decode+dispatch.
246 | decode_OP8a TMP1, INS
248 | daddu TMP0, DISPATCH, TMP1
249 | decode_RD8a RD, INS
251 | decode_RA8a RA, INS
261 |// Instruction footer.
263 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
264 | .define ins_next, ins_NEXT
265 | .define ins_next_, ins_NEXT
266 | .define ins_next1, ins_NEXT1
267 | .define ins_next2, ins_NEXT2
269 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
270 | // Affects only certain kinds of benchmarks (and only with -j off).
285 |// Call decode and dispatch.
287 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
288 | ld PC, LFUNC:RB->pc
291 | decode_OP8a TMP1, INS
292 | decode_RA8a RA, INS
295 | daddu TMP0, DISPATCH, TMP1
302 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
303 | sd PC, FRAME_PC(BASE)
307 |//-----------------------------------------------------------------------
311 | lui AT, (-(BCBIAS_J*4 >> 16) & 65535)
312 | addu TMP0, TMP0, AT
316 |// Assumes DISPATCH is relative to GL.
317 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
318 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
319 #define GG_DISP2GOT (GG_OFS(got) - GG_OFS(dispatch))
320 #define DISPATCH_GOT(name) (GG_DISP2GOT + sizeof(void*)*LJ_GOT_##name)
322 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
324 |.macro load_got, func
325 | ld CFUNCADDR, DISPATCH_GOT(func)(DISPATCH)
327 |// Much faster. Sadly, there's no easy way to force the required code layout.
328 |// .macro call_intern, func; bal extern func; .endmacro
329 |.macro call_intern, func; jalr CFUNCADDR; .endmacro
330 |.macro call_extern; jalr CFUNCADDR; .endmacro
331 |.macro jmp_extern; jr CFUNCADDR; .endmacro
333 |.macro hotcheck, delta, target
335 | andi TMP1, TMP1, 126
336 | daddu TMP1, TMP1, DISPATCH
337 | lhu TMP2, GG_DISP2HOT(TMP1)
338 | addiu TMP2, TMP2, -delta
340 |. sh TMP2, GG_DISP2HOT(TMP1)
344 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
348 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
351 |// Set current VM state. Uses TMP0.
352 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
353 |.macro st_vmstate; sw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
355 |// Move table write barrier back. Overwrites mark and tmp.
356 |.macro barrierback, tab, mark, tmp, target
357 | ld tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
358 | andi mark, mark, ~LJ_GC_BLACK & 255 // black2gray(tab)
359 | sd tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
360 | sb mark, tab->marked
362 |. sd tmp, tab->gclist
365 |// Clear type tag. Isolate lowest 14+32+1=47 bits of reg.
366 |.macro cleartp, reg; dextm reg, reg, 0, 14; .endmacro
367 |.macro cleartp, dst, reg; dextm dst, reg, 0, 14; .endmacro
369 |// Set type tag: Merge 17 type bits into bits [15+32=47, 31+32+1=64) of dst.
370 |.macro settp, dst, tp; dinsu dst, tp, 15, 31; .endmacro
372 |// Extract (negative) type tag.
373 |.macro gettp, dst, src; dsra dst, src, 47; .endmacro
375 |// Macros to check the TValue type and extract the GCobj. Branch on failure.
376 |.macro checktp, reg, tp, target
382 |.macro checktp, dst, reg, tp, target
388 |.macro checkstr, reg, target; checktp reg, -LJ_TSTR, target; .endmacro
389 |.macro checktab, reg, target; checktp reg, -LJ_TTAB, target; .endmacro
390 |.macro checkfunc, reg, target; checktp reg, -LJ_TFUNC, target; .endmacro
391 |.macro checkint, reg, target // Caveat: has delay slot!
393 | bne AT, TISNUM, target
395 |.macro checknum, reg, target // Caveat: has delay slot!
397 | sltiu AT, AT, LJ_TISNUM
401 |.macro mov_false, reg
406 |.macro mov_true, reg
412 |//-----------------------------------------------------------------------
414 /* Generate subroutines used by opcodes and other parts of the VM. */
415 /* The .code_sub section should be last to help static branch prediction. */
416 static void build_subroutines(BuildCtx *ctx)
420 |//-----------------------------------------------------------------------
421 |//-- Return handling ----------------------------------------------------
422 |//-----------------------------------------------------------------------
425 | // See vm_return. Also: TMP2 = previous base.
426 | andi AT, PC, FRAME_P
427 | beqz AT, ->cont_dispatch
429 | // Return from pcall or xpcall fast func.
431 | ld PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
432 | move BASE, TMP2 // Restore caller base.
433 | // Prepending may overwrite the pcall frame, so do it at the end.
434 | sd TMP1, -8(RA) // Prepend true to results.
438 | addiu RD, RD, 8 // RD = (nresults+1)*8.
439 | andi TMP0, PC, FRAME_TYPE
440 | beqz RD, ->vm_unwind_c_eh
441 |. li CRET1, LUA_YIELD
442 | beqz TMP0, ->BC_RET_Z // Handle regular return to Lua.
446 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
447 | // TMP0 = PC & FRAME_TYPE
449 | xori AT, TMP0, FRAME_C
451 | bnez AT, ->vm_returnp
452 | dsubu TMP2, BASE, TMP2 // TMP2 = previous base.
458 | daddiu BASE, BASE, -16
463 | addiu TMP1, TMP1, -8
468 |. daddiu BASE, BASE, 8
473 |. sd BASE, L->top // Store new top.
476 | ld TMP0, SAVE_CFRAME // Restore previous C frame.
477 | move CRET1, r0 // Ok return status for vm_pcall.
484 | ld TMP1, L->maxstack
486 | bnez AT, >7 // Less results wanted?
487 | // More results wanted. Check stack size and fill up results with nil.
488 |. slt AT, BASE, TMP1
494 |. daddiu BASE, BASE, 8
496 |7: // Less results wanted.
497 | subu TMP0, RD, TMP2
498 | dsubu TMP0, BASE, TMP0 // Either keep top or shrink it.
500 | selnez TMP0, TMP0, TMP2 // LUA_MULTRET+1 case?
501 | seleqz BASE, BASE, TMP2
503 |. or BASE, BASE, TMP0
506 |. movn BASE, TMP0, TMP2 // LUA_MULTRET+1 case?
509 |8: // Corner case: need to grow stack for filling up results.
510 | // This can happen if:
511 | // - A C function grows the stack (a lot).
512 | // - The GC shrinks the stack in between.
513 | // - A return back from a lua_call() with (high) nresults adjustment.
514 | load_got lj_state_growstack
517 | call_intern lj_state_growstack // (lua_State *L, int n)
520 | ld BASE, L->top // Need the (realloced) L->top in BASE.
525 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
526 | // (void *cframe, int errcode)
529 |->vm_unwind_c_eh: // Landing pad for external unwinder.
531 | li TMP0, ~LJ_VMST_C
532 | ld GL:TMP1, L->glref
534 |. sw TMP0, GL:TMP1->vmstate
536 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
540 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
542 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
544 | li TISNUM, LJ_TISNUM
546 | ld DISPATCH, L->glref // Setup pointer to dispatch table.
547 | .FPU mtc1 TMP3, TOBIT
550 | ld PC, FRAME_PC(BASE) // Fetch PC of previous frame.
551 | .FPU cvt.d.s TOBIT, TOBIT
552 | daddiu RA, BASE, -8 // Results start at BASE-8.
553 | daddiu DISPATCH, DISPATCH, GG_G2DISP
554 | sd TMP1, 0(RA) // Prepend false to error message.
557 |. li RD, 16 // 2 results: false + error message.
559 |->vm_unwind_stub: // Jump to exit stub from unwinder.
563 |//-----------------------------------------------------------------------
564 |//-- Grow stack for calls -----------------------------------------------
565 |//-----------------------------------------------------------------------
567 |->vm_growstack_c: // Grow stack for C function.
569 |. li CARG2, LUA_MINSTACK
571 |->vm_growstack_l: // Grow stack for Lua function.
572 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
576 | daddiu PC, PC, 4 // Must point after first instruction.
580 | // L->base = new base, L->top = top
581 | load_got lj_state_growstack
583 | call_intern lj_state_growstack // (lua_State *L, int n)
587 | ld LFUNC:RB, FRAME_FUNC(BASE)
590 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
591 | ins_callt // Just retry the call.
593 |//-----------------------------------------------------------------------
594 |//-- Entry points into the assembler VM ---------------------------------
595 |//-----------------------------------------------------------------------
597 |->vm_resume: // Setup C frame and resume thread.
598 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
601 | ld DISPATCH, L->glref // Setup pointer to dispatch table.
603 | lbu TMP1, L->status
606 | daddiu TMP0, sp, CFRAME_RESUME
607 | daddiu DISPATCH, DISPATCH, GG_G2DISP
610 | sd CARG1, SAVE_PC // Any value outside of bytecode is ok.
613 |. sd TMP0, L->cframe
615 | // Resume after yield (like a return).
616 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
620 | ld PC, FRAME_PC(BASE)
621 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
622 | dsubu RD, TMP1, BASE
623 | .FPU mtc1 TMP3, TOBIT
625 | .FPU cvt.d.s TOBIT, TOBIT
630 | andi TMP0, PC, FRAME_TYPE
632 | beqz TMP0, ->BC_RET_Z
633 |. li TISNUM, LJ_TISNUM
637 |->vm_pcall: // Setup protected C frame and enter VM.
638 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
640 | sw CARG4, SAVE_ERRF
644 |->vm_call: // Setup C frame and enter VM.
645 | // (lua_State *L, TValue *base, int nres1)
649 |1: // Entry point for vm_pcall above (PC = ftype).
650 | ld TMP1, L:CARG1->cframe
652 | sw CARG3, SAVE_NRES
653 | ld DISPATCH, L->glref // Setup pointer to dispatch table.
656 | daddiu DISPATCH, DISPATCH, GG_G2DISP
657 | sd CARG1, SAVE_PC // Any value outside of bytecode is ok.
658 | sd TMP1, SAVE_CFRAME
659 | sd sp, L->cframe // Add our C frame to cframe chain.
661 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
662 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
663 | ld TMP2, L->base // TMP2 = old base (used in vmeta_call).
664 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
666 | .FPU mtc1 TMP3, TOBIT
668 | dsubu NARGS8:RC, TMP1, BASE
669 | li TISNUM, LJ_TISNUM
670 | dsubu PC, PC, TMP2 // PC = frame delta + frame type
671 | .FPU cvt.d.s TOBIT, TOBIT
677 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
678 | ld LFUNC:RB, FRAME_FUNC(BASE)
679 | checkfunc LFUNC:RB, ->vmeta_call
681 |->vm_call_dispatch_f:
683 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
685 |->vm_cpcall: // Setup protected C frame, call C.
686 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
689 | ld TMP0, L:CARG1->stack
692 | ld DISPATCH, L->glref // Setup pointer to dispatch table.
693 | sd CARG1, SAVE_PC // Any value outside of bytecode is ok.
694 | dsubu TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
696 | daddiu DISPATCH, DISPATCH, GG_G2DISP
697 | sw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
698 | sw r0, SAVE_ERRF // No error function.
699 | sd TMP1, SAVE_CFRAME
700 | sd sp, L->cframe // Add our C frame to cframe chain.
701 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
702 | jalr CARG4 // (lua_State *L, lua_CFunction func, void *ud)
703 |. move CFUNCADDR, CARG4
705 | bnez CRET1, <3 // Else continue with the call.
707 | b ->vm_leave_cp // No base? Just remove C frame.
710 |//-----------------------------------------------------------------------
711 |//-- Metamethod handling ------------------------------------------------
712 |//-----------------------------------------------------------------------
714 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
715 |// stack, so BASE doesn't need to be reloaded across these calls.
717 |//-- Continuation dispatch ----------------------------------------------
720 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
721 | ld TMP0, -32(BASE) // Continuation.
723 | move BASE, TMP2 // Restore caller BASE.
724 | ld LFUNC:TMP1, FRAME_FUNC(TMP2)
728 | ld PC, -24(RB) // Restore PC from [cont|PC].
734 |. sd TISNIL, -8(TMP2) // Ensure one valid arg.
735 | ld TMP1, LFUNC:TMP1->pc
736 | // BASE = base, RA = resultptr, RB = meta base
737 | jr TMP0 // Jump to continuation.
738 |. ld KBASE, PC2PROTO(k)(TMP1)
742 | bnez TMP0, ->cont_ffi_callback // cont = 1: return from FFI callback.
743 | // cont = 0: tailcall from C function.
744 |. daddiu TMP1, RB, -32
746 |. dsubu RC, TMP1, BASE
749 |->cont_cat: // RA = resultptr, RB = meta base
751 | daddiu CARG2, RB, -32
753 | decode_RB8a MULTRES, INS
754 | decode_RA8a RA, INS
755 | decode_RB8b MULTRES
757 | daddu TMP1, BASE, MULTRES
759 | dsubu CARG3, CARG2, TMP1
760 | bne TMP1, CARG2, ->BC_CAT_Z
761 |. sd CRET1, 0(CARG2)
766 |//-- Table indexing metamethods -----------------------------------------
769 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
773 |. sd STR:RC, 0(CARG3)
776 | daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv)
780 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv2)
781 | sd TAB:RB, 0(CARG2)
784 |. sd STR:RC, 0(CARG3)
786 |->vmeta_tgetb: // TMP0 = index
787 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
793 | load_got lj_meta_tget
796 | call_intern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
798 | // Returns TValue * (finished) or NULL (metamethod).
800 |. daddiu TMP1, BASE, -FRAME_CONT
806 |3: // Call __index metamethod.
807 | // BASE = base, L->top = new base, stack = cont/func/t/k
809 | sd PC, -24(BASE) // [cont|PC]
810 | dsubu PC, BASE, TMP1
811 | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
813 | b ->vm_call_dispatch_f
814 |. li NARGS8:RC, 16 // 2 args for func(t, k).
817 | load_got lj_tab_getinth
818 | call_intern lj_tab_getinth // (GCtab *t, int32_t key)
820 | // Returns cTValue * or NULL.
821 | beqz CRET1, ->BC_TGETR_Z
822 |. move CARG2, TISNIL
824 |. ld CARG2, 0(CRET1)
826 |//-----------------------------------------------------------------------
829 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
833 |. sd STR:RC, 0(CARG3)
836 | daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv)
840 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv2)
841 | sd TAB:RB, 0(CARG2)
844 |. sd STR:RC, 0(CARG3)
846 |->vmeta_tsetb: // TMP0 = index
847 | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
853 | load_got lj_meta_tset
856 | call_intern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
858 | // Returns TValue * (finished) or NULL (metamethod).
861 | // NOBARRIER: lj_meta_tset ensures the table is not black.
866 |3: // Call __newindex metamethod.
867 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
868 | daddiu TMP1, BASE, -FRAME_CONT
870 | sd PC, -24(BASE) // [cont|PC]
871 | dsubu PC, BASE, TMP1
872 | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
874 | sd CARG1, 16(BASE) // Copy value to third argument.
875 | b ->vm_call_dispatch_f
876 |. li NARGS8:RC, 24 // 3 args for func(t, k, v)
879 | load_got lj_tab_setinth
882 | call_intern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
884 | // Returns TValue *.
888 |//-- Comparison metamethods ---------------------------------------------
891 | // RA/RD point to o1/o2.
894 | load_got lj_meta_comp
898 | decode_OP1 CARG4, INS
899 | call_intern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
901 | // Returns 0/1 or TValue * (metamethod).
904 | beqz AT, ->vmeta_binop
909 | lui TMP1, (-(BCBIAS_J*4 >> 16) & 65535)
917 |->cont_ra: // RA = resultptr
918 | lbu TMP1, -4+OFS_RA(PC)
921 | daddu TMP1, BASE, TMP1
925 |->cont_condt: // RA = resultptr
928 | sltiu AT, TMP0, LJ_TISTRUECOND
930 |. negu TMP2, AT // Branch if result is true.
932 |->cont_condf: // RA = resultptr
935 | sltiu AT, TMP0, LJ_TISTRUECOND
937 |. addiu TMP2, AT, -1 // Branch if result is false.
940 | // CARG1/CARG2 point to o1/o2. TMP0 is set to 0/1.
941 | load_got lj_meta_equal
942 | cleartp LFUNC:CARG3, CARG2
943 | cleartp LFUNC:CARG2, CARG1
948 | call_intern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
950 | // Returns 0/1 or TValue * (metamethod).
956 | load_got lj_meta_equal_cd
961 | call_intern lj_meta_equal_cd // (lua_State *L, BCIns op)
963 | // Returns 0/1 or TValue * (metamethod).
969 | load_got lj_meta_istype
975 | call_intern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
980 |//-- Arithmetic metamethods ---------------------------------------------
986 | load_got lj_meta_arith
992 | decode_OP1 CARG5, INS // CARG5 == RB.
993 | call_intern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
995 | // Returns NULL (finished) or TValue * (metamethod).
996 | beqz CRET1, ->cont_nop
999 | // Call metamethod for binary op.
1001 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1002 | dsubu TMP1, CRET1, BASE
1003 | sd PC, -24(CRET1) // [cont|PC]
1005 | daddiu PC, TMP1, FRAME_CONT
1007 | b ->vm_call_dispatch
1008 |. li NARGS8:RC, 16 // 2 args for func(o1, o2).
1011 | // CARG2 already set by BC_LEN.
1013 | move MULTRES, CARG1
1015 | load_got lj_meta_len
1018 | call_intern lj_meta_len // (lua_State *L, TValue *o)
1020 | // Returns NULL (retry) or TValue * (metamethod base).
1022 | bnez CRET1, ->vmeta_binop // Binop call for compatibility.
1025 |. move CARG1, MULTRES
1027 | b ->vmeta_binop // Binop call for compatibility.
1031 |//-- Call metamethod ----------------------------------------------------
1033 |->vmeta_call: // Resolve and call __call metamethod.
1034 | // TMP2 = old base, BASE = new base, RC = nargs*8
1035 | load_got lj_meta_call
1036 | sd TMP2, L->base // This is the callers base!
1037 | daddiu CARG2, BASE, -16
1039 | daddu CARG3, BASE, RC
1040 | move MULTRES, NARGS8:RC
1041 | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1043 | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1044 | daddiu NARGS8:RC, MULTRES, 8 // Got one more argument now.
1048 |->vmeta_callt: // Resolve __call for BC_CALLT.
1049 | // BASE = old base, RA = new base, RC = nargs*8
1050 | load_got lj_meta_call
1052 | daddiu CARG2, RA, -16
1054 | daddu CARG3, RA, RC
1055 | move MULTRES, NARGS8:RC
1056 | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1058 | ld RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1059 | ld TMP1, FRAME_PC(BASE)
1060 | daddiu NARGS8:RC, MULTRES, 8 // Got one more argument now.
1062 |. cleartp LFUNC:CARG3, RB
1064 |//-- Argument coercion for 'for' statement ------------------------------
1067 | load_got lj_meta_for
1072 | call_intern lj_meta_for // (lua_State *L, TValue *base)
1075 | decode_OP1 TMP0, MULTRES
1078 | decode_RA8a RA, MULTRES
1079 | decode_RD8a RD, MULTRES
1082 | beq TMP0, AT, =>BC_JFORI
1091 |//-----------------------------------------------------------------------
1092 |//-- Fast functions -----------------------------------------------------
1093 |//-----------------------------------------------------------------------
1095 |.macro .ffunc, name
1099 |.macro .ffunc_1, name
1101 | beqz NARGS8:RC, ->fff_fallback
1102 |. ld CARG1, 0(BASE)
1105 |.macro .ffunc_2, name
1107 | sltiu AT, NARGS8:RC, 16
1109 | bnez AT, ->fff_fallback
1110 |. ld CARG2, 8(BASE)
1113 |.macro .ffunc_n, name // Caveat: has delay slot!
1116 | beqz NARGS8:RC, ->fff_fallback
1117 | // Either ldc1 or the 1st instruction of checknum is in the delay slot.
1118 | .FPU ldc1 FARG1, 0(BASE)
1119 | checknum CARG1, ->fff_fallback
1122 |.macro .ffunc_nn, name // Caveat: has delay slot!
1125 | sltiu AT, NARGS8:RC, 16
1127 | bnez AT, ->fff_fallback
1128 |. gettp TMP0, CARG1
1130 | sltiu TMP0, TMP0, LJ_TISNUM
1131 | sltiu TMP1, TMP1, LJ_TISNUM
1132 | .FPU ldc1 FARG1, 0(BASE)
1133 | and TMP0, TMP0, TMP1
1134 | .FPU ldc1 FARG2, 8(BASE)
1135 | beqz TMP0, ->fff_fallback
1138 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1 and has delay slot!
1139 |// MIPSR6: no delay slot, but a forbidden slot.
1141 | ld TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1142 | ld TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1143 | dsubu AT, TMP0, TMP1
1145 | bgezalc AT, ->fff_gcstep
1147 | bgezal AT, ->fff_gcstep
1151 |//-- Base library: checks -----------------------------------------------
1154 | sltiu AT, AT, LJ_TISTRUECOND
1155 | beqz AT, ->fff_fallback
1156 |. daddiu RA, BASE, -16
1157 | ld PC, FRAME_PC(BASE)
1158 | addiu RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1159 | daddu TMP2, RA, RD
1160 | daddiu TMP1, BASE, 8
1161 | beq BASE, TMP2, ->fff_res // Done if exactly 1 argument.
1165 | sd CRET1, -16(TMP1)
1166 | bne TMP1, TMP2, <1
1167 |. daddiu TMP1, TMP1, 8
1173 | sltu TMP1, TISNUM, TMP0
1175 | li TMP3, ~LJ_TISNUM
1177 | selnez TMP2, TMP2, TMP1
1178 | seleqz TMP3, TMP3, TMP1
1179 | or TMP2, TMP2, TMP3
1181 | movz TMP2, TMP3, TMP1
1183 | dsll TMP2, TMP2, 3
1184 | daddu TMP2, CFUNC:RB, TMP2
1186 |. ld CARG1, CFUNC:TMP2->upvalue
1188 |//-- Base library: getters and setters ---------------------------------
1190 |.ffunc_1 getmetatable
1192 | daddiu TMP0, TMP2, -LJ_TTAB
1193 | daddiu TMP1, TMP2, -LJ_TUDATA
1195 | selnez TMP0, TMP1, TMP0
1197 | movn TMP0, TMP1, TMP0
1200 |. cleartp TAB:CARG1
1201 |1: // Field metatable must be at same offset for GCtab and GCudata!
1202 | ld TAB:RB, TAB:CARG1->metatable
1204 | ld STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1205 | beqz TAB:RB, ->fff_restv
1206 |. li CARG1, LJ_TNIL
1207 | lw TMP0, TAB:RB->hmask
1208 | lw TMP1, STR:RC->sid
1209 | ld NODE:TMP2, TAB:RB->node
1210 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
1211 | dsll TMP0, TMP1, 5
1212 | dsll TMP1, TMP1, 3
1213 | dsubu TMP1, TMP0, TMP1
1214 | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1216 | settp STR:RC, CARG4 // Tagged key to look for.
1217 |3: // Rearranged logic, because we expect _not_ to find the key.
1218 | ld TMP0, NODE:TMP2->key
1219 | ld CARG1, NODE:TMP2->val
1220 | ld NODE:TMP2, NODE:TMP2->next
1223 | bnez NODE:TMP2, <3
1227 | b ->fff_restv // Not found, keep default result.
1230 | bne CARG1, TISNIL, ->fff_restv
1232 | b <4 // Ditto for nil value.
1236 | sltiu AT, TMP2, LJ_TISNUM
1238 | selnez TMP0, TISNUM, AT
1239 | seleqz AT, TMP2, AT
1242 | movn TMP2, TISNUM, AT
1244 | dsll TMP2, TMP2, 3
1245 | dsubu TMP0, DISPATCH, TMP2
1247 |. ld TAB:RB, DISPATCH_GL(gcroot[GCROOT_BASEMT])-8(TMP0)
1249 |.ffunc_2 setmetatable
1250 | // Fast path: no mt for table yet and not clearing the mt.
1251 | checktp TMP1, CARG1, -LJ_TTAB, ->fff_fallback
1253 | ld TAB:TMP0, TAB:TMP1->metatable
1254 | lbu TMP2, TAB:TMP1->marked
1255 | daddiu AT, TMP3, -LJ_TTAB
1257 | or AT, AT, TAB:TMP0
1258 | bnez AT, ->fff_fallback
1259 |. andi AT, TMP2, LJ_GC_BLACK // isblack(table)
1260 | beqz AT, ->fff_restv
1261 |. sd TAB:CARG2, TAB:TMP1->metatable
1262 | barrierback TAB:TMP1, TMP2, TMP0, ->fff_restv
1266 | sltiu AT, NARGS8:RC, 16
1267 | load_got lj_tab_get
1270 | daddiu TMP0, TMP0, -LJ_TTAB
1272 | bnez AT, ->fff_fallback
1273 |. daddiu CARG3, BASE, 8
1274 | call_intern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1277 |. ld CARG1, 0(CRET1)
1279 |//-- Base library: conversions ------------------------------------------
1282 | // Only handles the number case inline (without a base argument).
1284 | xori AT, NARGS8:RC, 8 // Exactly one number argument.
1286 | sltu TMP0, TISNUM, TMP1
1288 | bnez AT, ->fff_fallback
1294 | // Only handles the string or number case inline.
1296 | daddiu AT, TMP0, -LJ_TSTR
1297 | // A __tostring method in the string base metatable is ignored.
1298 | beqz AT, ->fff_restv // String key?
1299 | // Handle numbers inline, unless a number base metatable is present.
1300 |. ld TMP1, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1301 | sltu TMP0, TISNUM, TMP0
1302 | or TMP0, TMP0, TMP1
1303 | bnez TMP0, ->fff_fallback
1304 |. sd BASE, L->base // Add frame since C call can throw.
1306 | sd PC, SAVE_PC // Redundant (but a defined value).
1310 |. sd PC, SAVE_PC // Redundant (but a defined value).
1312 | load_got lj_strfmt_number
1314 | call_intern lj_strfmt_number // (lua_State *L, cTValue *o)
1316 | // Returns GCstr *.
1320 |. move CARG1, CRET1
1322 |//-- Base library: iterators -------------------------------------------
1325 | checktp CARG1, -LJ_TTAB, ->fff_fallback
1326 | daddu TMP2, BASE, NARGS8:RC
1327 | sd TISNIL, 0(TMP2) // Set missing 2nd arg to nil.
1328 | load_got lj_tab_next
1329 | ld PC, FRAME_PC(BASE)
1330 | daddiu CARG2, BASE, 8
1331 | call_intern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1332 |. daddiu CARG3, BASE, -16
1333 | // Returns 1=found, 0=end, -1=error.
1334 | daddiu RA, BASE, -16
1335 | bgtz CRET1, ->fff_res // Found key/value.
1337 | beqz CRET1, ->fff_restv // End of traversal: return nil.
1338 |. move CARG1, TISNIL
1339 | ld CFUNC:RB, FRAME_FUNC(BASE)
1341 | b ->fff_fallback // Invalid key.
1345 | checktp TAB:TMP1, CARG1, -LJ_TTAB, ->fff_fallback
1346 | ld PC, FRAME_PC(BASE)
1348 | ld TAB:TMP2, TAB:TMP1->metatable
1349 | ld TMP0, CFUNC:RB->upvalue[0]
1350 | bnez TAB:TMP2, ->fff_fallback
1352 | ld TMP0, CFUNC:RB->upvalue[0]
1354 |. daddiu RA, BASE, -16
1355 | sd TISNIL, 0(BASE)
1356 | sd CARG1, -8(BASE)
1361 |.ffunc_2 ipairs_aux
1362 | checktab CARG1, ->fff_fallback
1363 | checkint CARG2, ->fff_fallback
1364 |. lw TMP0, TAB:CARG1->asize
1365 | ld TMP1, TAB:CARG1->array
1366 | ld PC, FRAME_PC(BASE)
1368 | addiu TMP2, TMP2, 1
1369 | sltu AT, TMP2, TMP0
1370 | daddiu RA, BASE, -16
1372 | settp TMP0, TISNUM
1373 | beqz AT, >2 // Not in array part?
1375 | dsll TMP3, TMP2, 3
1376 | daddu TMP3, TMP1, TMP3
1379 | beq TMP1, TISNIL, ->fff_res // End of iteration, return 0 results.
1384 |2: // Check for empty hash part first. Otherwise call C function.
1385 | lw TMP0, TAB:CARG1->hmask
1386 | load_got lj_tab_getinth
1387 | beqz TMP0, ->fff_res
1389 | call_intern lj_tab_getinth // (GCtab *t, int32_t key)
1391 | // Returns cTValue * or NULL.
1392 | beqz CRET1, ->fff_res
1395 |. ld TMP1, 0(CRET1)
1398 | checktp TAB:TMP1, CARG1, -LJ_TTAB, ->fff_fallback
1399 | ld PC, FRAME_PC(BASE)
1401 | ld TAB:TMP2, TAB:TMP1->metatable
1402 | ld CFUNC:TMP0, CFUNC:RB->upvalue[0]
1403 | bnez TAB:TMP2, ->fff_fallback
1405 | ld TMP0, CFUNC:RB->upvalue[0]
1407 | daddiu RA, BASE, -16
1408 | dsll AT, TISNUM, 47
1409 | sd CARG1, -8(BASE)
1411 | sd CFUNC:TMP0, 0(RA)
1415 |//-- Base library: catch errors ----------------------------------------
1418 | ld TMP1, L->maxstack
1419 | daddu TMP2, BASE, NARGS8:RC
1420 | sltu AT, TMP1, TMP2
1421 | bnez AT, ->fff_fallback
1422 |. lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1423 | daddiu NARGS8:RC, NARGS8:RC, -8
1424 | bltz NARGS8:RC, ->fff_fallback
1426 | daddiu BASE, BASE, 16
1427 | // Remember active hook before pcall.
1428 | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT
1429 | andi TMP3, TMP3, 1
1430 | daddiu PC, TMP3, 16+FRAME_PCALL
1431 | beqz NARGS8:RC, ->vm_call_dispatch
1433 |. daddu TMP0, BASE, NARGS8:RC
1435 | ld TMP1, -16(TMP0)
1437 | daddiu TMP0, TMP0, -8
1438 | bne TMP0, BASE, <2
1440 | b ->vm_call_dispatch
1444 | ld TMP1, L->maxstack
1445 | daddu TMP2, BASE, NARGS8:RC
1446 | sltu AT, TMP1, TMP2
1447 | bnez AT, ->fff_fallback
1448 |. ld CARG1, 0(BASE)
1449 | daddiu NARGS8:TMP0, NARGS8:RC, -16
1451 | bltz NARGS8:TMP0, ->fff_fallback
1452 |. lbu TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1454 | daddiu AT, AT, -LJ_TFUNC
1455 | bnez AT, ->fff_fallback // Traceback must be a function.
1457 | move NARGS8:RC, NARGS8:TMP0
1458 | daddiu BASE, BASE, 24
1459 | // Remember active hook before pcall.
1460 | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT
1461 | sd CARG2, 0(TMP2) // Swap function and traceback.
1462 | andi TMP3, TMP3, 1
1464 | beqz NARGS8:RC, ->vm_call_dispatch
1465 |. daddiu PC, TMP3, 24+FRAME_PCALL
1469 |//-- Coroutine library --------------------------------------------------
1471 |.macro coroutine_resume_wrap, resume
1473 |.ffunc_1 coroutine_resume
1474 | checktp CARG1, CARG1, -LJ_TTHREAD, ->fff_fallback
1476 |.ffunc coroutine_wrap_aux
1477 | ld L:CARG1, CFUNC:RB->upvalue[0].gcr
1480 | lbu TMP0, L:CARG1->status
1481 | ld TMP1, L:CARG1->cframe
1482 | ld CARG2, L:CARG1->top
1483 | ld TMP2, L:CARG1->base
1484 | addiu AT, TMP0, -LUA_YIELD
1485 | daddu CARG3, CARG2, TMP0
1486 | daddiu TMP3, CARG2, 8
1488 | seleqz CARG2, CARG2, AT
1489 | selnez TMP3, TMP3, AT
1490 | bgtz AT, ->fff_fallback // st > LUA_YIELD?
1491 |. or CARG2, TMP3, CARG2
1493 | bgtz AT, ->fff_fallback // st > LUA_YIELD?
1494 |. movn CARG2, TMP3, AT
1496 | xor TMP2, TMP2, CARG3
1497 | bnez TMP1, ->fff_fallback // cframe != 0?
1498 |. or AT, TMP2, TMP0
1499 | ld TMP0, L:CARG1->maxstack
1500 | beqz AT, ->fff_fallback // base == top && st == 0?
1501 |. ld PC, FRAME_PC(BASE)
1502 | daddu TMP2, CARG2, NARGS8:RC
1503 | sltu AT, TMP0, TMP2
1504 | bnez AT, ->fff_fallback // Stack overflow?
1509 | daddiu BASE, BASE, 8 // Keep resumed thread in stack for GC.
1510 | daddiu NARGS8:RC, NARGS8:RC, -8
1511 | daddiu TMP2, TMP2, -8
1513 | sd TMP2, L:CARG1->top
1514 | daddu TMP1, BASE, NARGS8:RC
1517 |2: // Move args to coroutine.
1519 | sltu AT, BASE, TMP1
1521 |. daddiu BASE, BASE, 8
1522 | sd CRET1, 0(CARG3)
1524 |. daddiu CARG3, CARG3, 8
1526 | bal ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1527 |. move L:RA, L:CARG1
1528 | // Returns thread status.
1530 | ld TMP2, L:RA->base
1531 | sltiu AT, CRET1, LUA_YIELD+1
1532 | ld TMP3, L:RA->top
1535 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
1538 |. dsubu RD, TMP3, TMP2
1539 | ld TMP0, L->maxstack
1540 | beqz RD, >6 // No results?
1541 |. daddu TMP1, BASE, RD
1542 | sltu AT, TMP0, TMP1
1543 | bnez AT, >9 // Need to grow stack?
1544 |. daddu TMP3, TMP2, RD
1545 | sd TMP2, L:RA->top // Clear coroutine stack.
1547 |5: // Move results from coroutine.
1549 | daddiu TMP2, TMP2, 8
1550 | sltu AT, TMP2, TMP3
1553 |. daddiu TMP1, TMP1, 8
1555 | andi TMP0, PC, FRAME_TYPE
1558 | daddiu RA, BASE, -8
1559 | sd TMP1, -8(BASE) // Prepend true to results.
1567 | beqz TMP0, ->BC_RET_Z
1572 |8: // Coroutine returned with error (at co->top-1).
1574 | daddiu TMP3, TMP3, -8
1577 | sd TMP3, L:RA->top // Remove error from coroutine stack.
1579 | sd TMP1, -8(BASE) // Prepend false to results.
1580 | daddiu RA, BASE, -8
1581 | sd CRET1, 0(BASE) // Copy error message.
1583 |. andi TMP0, PC, FRAME_TYPE
1585 | load_got lj_ffh_coroutine_wrap_err
1587 | call_intern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1591 |9: // Handle stack expansion on return from yield.
1592 | load_got lj_state_growstack
1594 | call_intern lj_state_growstack // (lua_State *L, int n)
1600 | coroutine_resume_wrap 1 // coroutine.resume
1601 | coroutine_resume_wrap 0 // coroutine.wrap
1603 |.ffunc coroutine_yield
1604 | ld TMP0, L->cframe
1605 | daddu TMP1, BASE, NARGS8:RC
1607 | andi TMP0, TMP0, CFRAME_RESUME
1609 | beqz TMP0, ->fff_fallback
1610 |. li CRET1, LUA_YIELD
1613 |. sb CRET1, L->status
1615 |//-- Math library -------------------------------------------------------
1618 | gettp CARG2, CARG1
1619 | daddiu AT, CARG2, -LJ_TISNUM
1621 |. sextw TMP1, CARG1
1622 | sra TMP0, TMP1, 31 // Extract sign.
1623 | xor TMP1, TMP1, TMP0
1624 | dsubu CARG1, TMP1, TMP0
1625 | dsll TMP3, CARG1, 32
1626 | bgez TMP3, ->fff_restv
1627 |. settp CARG1, TISNUM
1628 | li CARG1, 0x41e0 // 2^31 as a double.
1630 |. dsll CARG1, CARG1, 48
1632 | sltiu AT, CARG2, LJ_TISNUM
1633 | beqz AT, ->fff_fallback
1634 |. dextm CARG1, CARG1, 0, 30
1638 | // CARG1 = TValue result.
1639 | ld PC, FRAME_PC(BASE)
1640 | daddiu RA, BASE, -16
1641 | sd CARG1, -16(BASE)
1643 | // RA = results, PC = return.
1646 | // RA = results, RD = (nresults+1)*8, PC = return.
1647 | andi TMP0, PC, FRAME_TYPE
1648 | bnez TMP0, ->vm_return
1651 | decode_RB8a RB, INS
1655 | bnez AT, >6 // More results expected?
1656 |. decode_RA8a TMP0, INS
1659 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1660 | dsubu BASE, RA, TMP0
1663 |6: // Fill up results with nil.
1664 | daddu TMP1, RA, RD
1667 |. sd TISNIL, -8(TMP1)
1669 |.macro math_extern, func
1670 | .ffunc_n math_ .. func
1678 |.macro math_extern2, func
1679 | .ffunc_nn math_ .. func
1687 |// TODO: Return integer type if result is integer (own sf implementation).
1688 |.macro math_round, func
1689 |->ff_math_ .. func:
1691 | beqz NARGS8:RC, ->fff_fallback
1692 |. gettp TMP0, CARG1
1693 | beq TMP0, TISNUM, ->fff_restv
1694 |. sltu AT, TMP0, TISNUM
1695 | beqz AT, ->fff_fallback
1697 |. ldc1 FARG1, 0(BASE)
1714 | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument.
1715 |. ld CARG1, 0(BASE)
1716 | checknum CARG1, ->fff_fallback
1720 |. ldc1 FARG1, 0(BASE)
1740 | math_extern2 atan2
1745 |. sqrt.d FRET1, FARG1
1746 |// fallthrough to ->fff_resn
1752 | ld PC, FRAME_PC(BASE)
1753 | daddiu RA, BASE, -16
1756 |. sdc1 FRET1, 0(RA)
1762 |.ffunc_2 math_ldexp
1763 | checknum CARG1, ->fff_fallback
1764 | checkint CARG2, ->fff_fallback
1766 | .FPU ldc1 FARG1, 0(BASE)
1768 |. lw CARG2, 8+LO(BASE)
1772 |.ffunc_n math_frexp
1774 | ld PC, FRAME_PC(BASE)
1776 |. daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv)
1777 | lw TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1778 | daddiu RA, BASE, -16
1782 | cvt.d.w FARG2, FARG2
1787 | settp TMP1, TISNUM
1795 | ld PC, FRAME_PC(BASE)
1797 |. daddiu CARG2, BASE, -16
1798 | daddiu RA, BASE, -16
1800 | sdc1 FRET1, -8(BASE)
1802 | sd CRET1, -8(BASE)
1807 |.macro math_minmax, name, intins, intinsc, fpins
1809 | daddu TMP3, BASE, NARGS8:RC
1810 | checkint CARG1, >5
1811 |. daddiu TMP2, BASE, 8
1812 |1: // Handle integers.
1813 | beq TMP2, TMP3, ->fff_restv
1814 |. ld CARG2, 0(TMP2)
1815 | checkint CARG2, >3
1816 |. sextw CARG1, CARG1
1817 | lw CARG2, LO(TMP2)
1818 |. slt AT, CARG1, CARG2
1820 | intins TMP1, CARG2, AT
1821 | intinsc CARG1, CARG1, AT
1822 | or CARG1, CARG1, TMP1
1824 | intins CARG1, CARG2, AT
1826 | daddiu TMP2, TMP2, 8
1827 | zextw CARG1, CARG1
1829 |. settp CARG1, TISNUM
1831 |3: // Convert intermediate result to number and continue with number loop.
1832 | checknum CARG2, ->fff_fallback
1834 |. mtc1 CARG1, FRET1
1835 | cvt.d.w FRET1, FRET1
1837 |. ldc1 FARG1, 0(TMP2)
1847 | .FPU ldc1 FRET1, 0(BASE)
1848 | checknum CARG1, ->fff_fallback
1849 |6: // Handle numbers.
1850 |. ld CARG2, 0(TMP2)
1851 | beq TMP2, TMP3, ->fff_resn
1853 | ldc1 FARG1, 0(TMP2)
1857 | checknum CARG2, >8
1862 | fpins FRET1, FRET1, FARG1
1865 | c.olt.d FARG1, FRET1
1867 | c.olt.d FRET1, FARG1
1869 | movf.d FRET1, FARG1
1879 | seleqz AT, CARG2, CRET1
1880 | selnez CARG1, CARG1, CRET1
1881 | or CARG1, CARG1, AT
1883 | movz CARG1, CARG2, CRET1
1887 |. daddiu TMP2, TMP2, 8
1889 |8: // Convert integer to number and continue with number loop.
1890 | checkint CARG2, ->fff_fallback
1892 |. lwc1 FARG1, LO(TMP2)
1894 |. cvt.d.w FARG1, FARG1
1896 |. lw CARG2, LO(TMP2)
1906 | math_minmax math_min, seleqz, selnez, min.d
1907 | math_minmax math_max, selnez, seleqz, max.d
1909 | math_minmax math_min, movz, _, 0
1910 | math_minmax math_max, movn, _, 1
1913 |//-- String library -----------------------------------------------------
1915 |.ffunc string_byte // Only handle the 1-arg case here.
1918 | xori AT, NARGS8:RC, 8
1919 | daddiu TMP0, TMP0, -LJ_TSTR
1921 | bnez AT, ->fff_fallback // Need exactly 1 string argument.
1922 |. cleartp STR:CARG1
1923 | lw TMP0, STR:CARG1->len
1924 | daddiu RA, BASE, -16
1925 | ld PC, FRAME_PC(BASE)
1927 | lbu TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
1929 | sll RD, RD, 3 // RD = ((str->len != 0)+1)*8
1930 | settp TMP1, TISNUM
1934 |.ffunc string_char // Only handle the 1-arg case here.
1941 | xori AT, NARGS8:RC, 8 // Exactly 1 argument.
1942 | daddiu TMP0, TMP0, -LJ_TISNUM // Integer.
1944 | sextw CARG1, CARG1
1946 | sltu TMP1, TMP1, CARG1 // !(255 < n).
1948 | bnez AT, ->fff_fallback
1950 | daddiu CARG2, sp, TMPD_OFS
1953 | load_got lj_str_new
1956 | call_intern lj_str_new // (lua_State *L, char *str, size_t l)
1958 | // Returns GCstr *.
1964 |. move CARG1, CRET1
1971 | addiu AT, NARGS8:RC, -16
1973 | bltz AT, ->fff_fallback
1975 | cleartp STR:CARG1, TMP0
1979 | ld CARG3, 16(BASE)
1980 | checkint CARG3, ->fff_fallback
1981 |. sextw CARG4, CARG3
1983 | checkint CARG2, ->fff_fallback
1985 | bne TMP3, AT, ->fff_fallback
1986 |. sextw CARG3, CARG2
1987 | lw CARG2, STR:CARG1->len
1988 | // STR:CARG1 = str, CARG2 = str->len, CARG3 = start, CARG4 = end
1990 | addiu TMP0, CARG2, 1
1991 | addu TMP1, CARG4, TMP0
1992 | slt TMP3, CARG3, r0
1994 | seleqz CARG4, CARG4, AT
1995 | selnez TMP1, TMP1, AT
1996 | or CARG4, TMP1, CARG4 // if (end < 0) end += len+1
1998 | movn CARG4, TMP1, AT // if (end < 0) end += len+1
2000 | addu TMP1, CARG3, TMP0
2002 | selnez TMP1, TMP1, TMP3
2003 | seleqz CARG3, CARG3, TMP3
2004 | or CARG3, TMP1, CARG3 // if (start < 0) start += len+1
2007 | slt TMP3, r0, CARG3
2008 | seleqz CARG4, CARG4, AT // if (end < 0) end = 0
2009 | selnez CARG3, CARG3, TMP3
2010 | seleqz TMP2, TMP2, TMP3
2011 | or CARG3, TMP2, CARG3 // if (start < 1) start = 1
2012 | slt AT, CARG2, CARG4
2013 | seleqz CARG4, CARG4, AT
2014 | selnez CARG2, CARG2, AT
2015 | or CARG4, CARG2, CARG4 // if (end > len) end = len
2017 | movn CARG3, TMP1, TMP3 // if (start < 0) start += len+1
2020 | slt TMP3, r0, CARG3
2021 | movn CARG4, r0, AT // if (end < 0) end = 0
2022 | movz CARG3, TMP2, TMP3 // if (start < 1) start = 1
2023 | slt AT, CARG2, CARG4
2024 | movn CARG4, CARG2, AT // if (end > len) end = len
2026 | daddu CARG2, STR:CARG1, CARG3
2027 | subu CARG3, CARG4, CARG3 // len = end - start
2028 | daddiu CARG2, CARG2, sizeof(GCstr)-1
2029 | bgez CARG3, ->fff_newstr
2030 |. addiu CARG3, CARG3, 1 // len++
2031 |->fff_emptystr: // Return empty string.
2033 | daddiu STR:CARG1, DISPATCH, DISPATCH_GL(strempty)
2037 |.macro ffstring_op, name
2038 | .ffunc string_ .. name
2041 | beqz NARGS8:RC, ->fff_fallback
2042 |. ld CARG2, 0(BASE)
2043 | checkstr STR:CARG2, ->fff_fallback
2044 | daddiu SBUF:CARG1, DISPATCH, DISPATCH_GL(tmpbuf)
2045 | load_got lj_buf_putstr_ .. name
2046 | ld TMP0, SBUF:CARG1->b
2047 | sd L, SBUF:CARG1->L
2049 | sd TMP0, SBUF:CARG1->w
2050 | call_intern extern lj_buf_putstr_ .. name
2052 | load_got lj_buf_tostr
2053 | call_intern lj_buf_tostr
2054 |. move SBUF:CARG1, SBUF:CRET1
2059 |ffstring_op reverse
2063 |//-- Bit library --------------------------------------------------------
2066 | beqz TMP1, ->fff_fallback
2068 |. ldc1 FARG1, 0(BASE)
2069 | add.d FARG1, FARG1, TOBIT
2072 |. zextw CRET1, CRET1
2074 |// FP number to bit conversion for soft-float.
2076 | dsll TMP0, CARG1, 1
2079 | dsubu CARG3, CARG3, AT
2080 | sltiu AT, CARG3, 54
2082 |. dextm TMP0, TMP0, 0, 20
2083 | dinsu TMP0, AT, 21, 21
2085 | dsrlv CRET1, TMP0, CARG3
2086 | dsubu TMP0, r0, CRET1
2088 | selnez TMP0, TMP0, AT
2089 | seleqz CRET1, CRET1, AT
2090 | or CRET1, CRET1, TMP0
2092 | movn CRET1, TMP0, AT
2095 |. zextw CRET1, CRET1
2100 |// FP number to int conversion with a check for soft-float.
2101 |// Modifies CARG1, CRET1, CRET2, TMP0, AT.
2104 | dsll CRET2, CARG1, 1
2107 | dsrl AT, CRET2, 53
2108 | dsubu TMP0, TMP0, AT
2109 | sltiu AT, TMP0, 54
2111 |. dextm CRET2, CRET2, 0, 20
2112 | dinsu CRET2, AT, 21, 21
2114 | dsrlv CRET1, CRET2, TMP0
2115 | dsubu CARG1, r0, CRET1
2117 | seleqz CRET1, CRET1, AT
2118 | selnez CARG1, CARG1, AT
2119 | or CRET1, CRET1, CARG1
2121 | movn CRET1, CARG1, AT
2124 | subu TMP0, CARG1, TMP0
2125 | dsllv CRET2, CRET2, TMP0 // Integer check.
2127 | xor AT, CRET1, AT // Range check.
2129 | seleqz AT, AT, CRET2
2130 | selnez CRET2, CRET2, CRET2
2132 |. or CRET2, AT, CRET2
2135 |. movz CRET2, AT, CRET2
2146 |.macro .ffunc_bit, name
2147 | .ffunc_1 bit_..name
2149 | beq TMP0, TISNUM, >6
2150 |. zextw CRET1, CARG1
2152 |. sltiu TMP1, TMP0, LJ_TISNUM
2156 |.macro .ffunc_bit_op, name, bins
2158 | daddiu TMP2, BASE, 8
2159 | daddu TMP3, BASE, NARGS8:RC
2161 | beq TMP2, TMP3, ->fff_resi
2162 |. ld CARG1, 0(TMP2)
2165 | bne TMP0, TISNUM, >2
2166 |. daddiu TMP2, TMP2, 8
2167 | zextw CARG1, CARG1
2169 |. bins CRET1, CRET1, CARG1
2171 | ldc1 FARG1, -8(TMP2)
2172 | sltiu AT, TMP0, LJ_TISNUM
2173 | beqz AT, ->fff_fallback
2174 |. add.d FARG1, FARG1, TOBIT
2176 | zextw CARG1, CARG1
2178 |. bins CRET1, CRET1, CARG1
2180 | beq TMP0, TISNUM, >2
2181 |. move CRET2, CRET1
2183 |. sltiu TMP1, TMP0, LJ_TISNUM
2186 | zextw CARG1, CARG1
2187 | bins CRET1, CRET1, CARG1
2189 |. daddiu TMP2, TMP2, 8
2193 |.ffunc_bit_op band, and
2194 |.ffunc_bit_op bor, or
2195 |.ffunc_bit_op bxor, xor
2198 | dsrl TMP0, CRET1, 8
2199 | dsrl TMP1, CRET1, 24
2200 | andi TMP2, TMP0, 0xff00
2201 | dins TMP1, CRET1, 24, 31
2202 | dins TMP2, TMP0, 16, 23
2204 |. or CRET1, TMP1, TMP2
2209 |. zextw CRET1, CRET1
2211 |.macro .ffunc_bit_sh, name, shins, shmod
2212 | .ffunc_2 bit_..name
2214 | beq TMP0, TISNUM, >1
2217 |. sltiu TMP1, TMP0, LJ_TISNUM
2221 | bne TMP0, TISNUM, ->fff_fallback
2222 |. zextw CARG2, CARG2
2223 | sextw CARG1, CARG1
2227 | shins CRET1, CARG1, CARG2
2229 |. zextw CRET1, CRET1
2232 |.ffunc_bit_sh lshift, sllv, 0
2233 |.ffunc_bit_sh rshift, srlv, 0
2234 |.ffunc_bit_sh arshift, srav, 0
2235 |.ffunc_bit_sh rol, rotrv, 1
2236 |.ffunc_bit_sh ror, rotrv, 0
2240 | ld PC, FRAME_PC(BASE)
2241 | daddiu RA, BASE, -16
2242 | settp CRET1, TISNUM
2244 |. sd CRET1, -16(BASE)
2246 |//-----------------------------------------------------------------------
2247 |->fff_fallback: // Call fast function fallback handler.
2248 | // BASE = new base, RB = CFUNC, RC = nargs*8
2249 | ld TMP3, CFUNC:RB->f
2250 | daddu TMP1, BASE, NARGS8:RC
2251 | ld PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2252 | daddiu TMP0, TMP1, 8*LUA_MINSTACK
2253 | ld TMP2, L->maxstack
2254 | sd PC, SAVE_PC // Redundant (but a defined value).
2255 | sltu AT, TMP2, TMP0
2258 | bnez AT, >5 // Need to grow stack.
2259 |. move CFUNCADDR, TMP3
2260 | jalr TMP3 // (lua_State *L)
2262 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2265 | bgtz CRET1, ->fff_res // Returned nresults+1?
2266 |. daddiu RA, BASE, -16
2267 |1: // Returned 0 or -1: retry fast path.
2268 | ld LFUNC:RB, FRAME_FUNC(BASE)
2271 | bnez CRET1, ->vm_call_tail // Returned -1?
2272 |. dsubu NARGS8:RC, TMP0, BASE
2273 | ins_callt // Returned 0: retry fast path.
2275 |// Reconstruct previous base for vmeta_call during tailcall.
2277 | andi TMP0, PC, FRAME_TYPE
2281 | lbu TMP1, OFS_RA(PC)
2283 | addiu TMP1, TMP1, 16
2285 | b ->vm_call_dispatch // Resolve again for tailcall.
2286 |. dsubu TMP2, BASE, TMP1
2288 |5: // Grow stack for fallback handler.
2289 | load_got lj_state_growstack
2290 | li CARG2, LUA_MINSTACK
2291 | call_intern lj_state_growstack // (lua_State *L, int n)
2295 |. li CRET1, 0 // Force retry.
2297 |->fff_gcstep: // Call GC step function.
2298 | // BASE = new base, RC = nargs*8
2300 | load_got lj_gc_step
2302 | daddu TMP0, BASE, NARGS8:RC
2303 | sd PC, SAVE_PC // Redundant (but a defined value).
2305 | call_intern lj_gc_step // (lua_State *L)
2310 | ld CFUNC:RB, FRAME_FUNC(BASE)
2313 |. dsubu NARGS8:RC, TMP0, BASE
2315 |//-----------------------------------------------------------------------
2316 |//-- Special dispatch targets -------------------------------------------
2317 |//-----------------------------------------------------------------------
2319 |->vm_record: // Dispatch target for recording phase.
2321 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2322 | andi AT, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2324 | // Decrement the hookcount for consistency, but always do the call.
2325 |. lw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2326 | andi AT, TMP3, HOOK_ACTIVE
2328 |. addiu TMP2, TMP2, -1
2329 | andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2333 |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2336 |->vm_rethook: // Dispatch target for return hooks.
2337 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2338 | andi AT, TMP3, HOOK_ACTIVE // Hook already active?
2340 |5: // Re-dispatch to static ins.
2341 |. ld AT, GG_DISP2STATIC(TMP0) // Assumes TMP0 holds DISPATCH+OP*4.
2345 |->vm_inshook: // Dispatch target for instr/line hooks.
2346 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2347 | lw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2348 | andi AT, TMP3, HOOK_ACTIVE // Hook already active?
2350 |. andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2352 |. addiu TMP2, TMP2, -1
2354 |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2355 | andi AT, TMP3, LUA_MASKLINE
2358 |. load_got lj_dispatch_ins
2359 | sw MULTRES, SAVE_MULTRES
2362 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2363 | call_intern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2367 |4: // Re-dispatch to static ins.
2369 | decode_OP8a TMP1, INS
2371 | daddu TMP0, DISPATCH, TMP1
2372 | decode_RD8a RD, INS
2373 | ld AT, GG_DISP2STATIC(TMP0)
2374 | decode_RA8a RA, INS
2379 |->cont_hook: // Continue from hook yield.
2382 |. lw MULTRES, -24+LO(RB) // Restore MULTRES for *M ins.
2384 |->vm_hotloop: // Hot loop counter underflow.
2386 | ld LFUNC:TMP1, FRAME_FUNC(BASE)
2387 | daddiu CARG1, DISPATCH, GG_DISP2J
2388 | cleartp LFUNC:TMP1
2390 | ld TMP1, LFUNC:TMP1->pc
2392 | sd L, DISPATCH_J(L)(DISPATCH)
2393 | lbu TMP1, PC2PROTO(framesize)(TMP1)
2394 | load_got lj_trace_hot
2396 | dsll TMP1, TMP1, 3
2397 | daddu TMP1, BASE, TMP1
2398 | call_intern lj_trace_hot // (jit_State *J, const BCIns *pc)
2405 |->vm_callhook: // Dispatch target for call hooks.
2411 |->vm_hotcall: // Hot call counter underflow.
2416 | load_got lj_dispatch_call
2417 | daddu TMP0, BASE, RC
2420 | dsubu RA, RA, BASE
2422 | call_intern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2424 | // Returns ASMFunction.
2427 | sd r0, SAVE_PC // Invalidate for subsequent line hook.
2428 | dsubu NARGS8:RC, TMP0, BASE
2429 | daddu RA, BASE, RA
2430 | ld LFUNC:RB, FRAME_FUNC(BASE)
2435 |->cont_stitch: // Trace stitching.
2437 | // RA = resultptr, RB = meta base
2439 | ld TRACE:TMP2, -40(RB) // Save previous trace.
2440 | decode_RA8a RC, INS
2441 | daddiu AT, MULTRES, -8
2442 | cleartp TRACE:TMP2
2445 |. daddu RC, BASE, RC // Call base.
2446 |1: // Move results down.
2454 | decode_RA8a RA, INS
2455 | decode_RB8a RB, INS
2459 | daddu RA, BASE, RA
2462 | bnez AT, >9 // More results wanted?
2465 | lhu TMP3, TRACE:TMP2->traceno
2466 | lhu RD, TRACE:TMP2->link
2467 | beq RD, TMP3, ->cont_nop // Blacklisted.
2468 |. load_got lj_dispatch_stitch
2469 | bnez RD, =>BC_JLOOP // Jump to stitched trace.
2472 | // Stitch a new trace to the previous trace.
2473 | sw TMP3, DISPATCH_J(exitno)(DISPATCH)
2474 | sd L, DISPATCH_J(L)(DISPATCH)
2476 | daddiu CARG1, DISPATCH, GG_DISP2J
2477 | call_intern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2488 |->vm_profhook: // Dispatch target for profiler hook.
2490 | load_got lj_dispatch_profile
2491 | sw MULTRES, SAVE_MULTRES
2494 | call_intern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2496 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2502 |//-----------------------------------------------------------------------
2503 |//-- Trace exit handler -------------------------------------------------
2504 |//-----------------------------------------------------------------------
2506 |.macro savex_, a, b
2508 | sdc1 f..a, a*8(sp)
2509 | sdc1 f..b, b*8(sp)
2510 | sd r..a, 32*8+a*8(sp)
2511 | sd r..b, 32*8+b*8(sp)
2521 | daddiu sp, sp, -(32*8+32*8)
2523 | daddiu sp, sp, -(32*8)
2541 | sdc1 f29, 29*8(sp)
2542 | sdc1 f31, 31*8(sp)
2543 | sd r0, 32*8+31*8(sp) // Clear RID_TMP.
2544 | daddiu TMP2, sp, 32*8+32*8 // Recompute original value of sp.
2545 | sd TMP2, 32*8+29*8(sp) // Store sp in RID_SP
2547 | sd r0, 31*8(sp) // Clear RID_TMP.
2548 | daddiu TMP2, sp, 32*8 // Recompute original value of sp.
2549 | sd TMP2, 29*8(sp) // Store sp in RID_SP
2552 | daddiu DISPATCH, JGL, -GG_DISP2G-32768
2553 | lw TMP1, 0(TMP2) // Load exit number.
2555 | ld L, DISPATCH_GL(cur_L)(DISPATCH)
2556 | ld BASE, DISPATCH_GL(jit_base)(DISPATCH)
2557 | load_got lj_trace_exit
2558 | sd L, DISPATCH_J(L)(DISPATCH)
2559 | sw ra, DISPATCH_J(parent)(DISPATCH) // Store trace number.
2561 | sw TMP1, DISPATCH_J(exitno)(DISPATCH) // Store exit number.
2562 | daddiu CARG1, DISPATCH, GG_DISP2J
2563 | sd r0, DISPATCH_GL(jit_base)(DISPATCH)
2564 | call_intern lj_trace_exit // (jit_State *J, ExitState *ex)
2566 | // Returns MULTRES (unscaled) or negated error code.
2567 | ld TMP1, L->cframe
2571 | ld PC, SAVE_PC // Get SAVE_PC.
2573 |. sd L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2577 | // CRET1 = MULTRES or negated error code, BASE, PC and JGL set.
2579 | daddiu DISPATCH, JGL, -GG_DISP2G-32768
2582 | sltiu TMP0, CRET1, -LUA_ERRERR // Check for error from exit.
2584 |. ld LFUNC:RB, FRAME_FUNC(BASE)
2585 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2586 | dsll MULTRES, CRET1, 3
2588 | sw MULTRES, SAVE_MULTRES
2589 | li TISNIL, LJ_TNIL
2590 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2591 | .FPU mtc1 TMP3, TOBIT
2592 | ld TMP1, LFUNC:RB->pc
2593 | sd r0, DISPATCH_GL(jit_base)(DISPATCH)
2594 | ld KBASE, PC2PROTO(k)(TMP1)
2595 | .FPU cvt.d.s TOBIT, TOBIT
2596 | // Modified copy of ins_next which handles function header dispatch, too.
2598 | addiu CRET1, CRET1, 17 // Static dispatch?
2599 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1
2600 | sw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2601 | decode_RD8a RD, INS
2604 | decode_OP8a TMP1, INS
2606 | daddu TMP0, DISPATCH, TMP1
2607 | sltiu TMP2, TMP1, BC_FUNCF*8
2609 | decode_RA8a RA, INS
2615 | sltiu TMP2, TMP1, (BC_FUNCC+2)*8 // Fast function?
2617 |. ld TMP1, FRAME_PC(BASE)
2618 | // Check frame below fast function.
2619 | andi TMP0, TMP1, FRAME_TYPE
2620 | bnez TMP0, >3 // Trace stitching continuation?
2622 | // Otherwise set KBASE for Lua function below fast function.
2624 | decode_RA8a TMP0, TMP2
2626 | dsubu TMP1, BASE, TMP0
2627 | ld LFUNC:TMP2, -32(TMP1)
2628 | cleartp LFUNC:TMP2
2629 | ld TMP1, LFUNC:TMP2->pc
2630 | ld KBASE, PC2PROTO(k)(TMP1)
2632 | daddiu RC, MULTRES, -8
2634 |. daddu RA, RA, BASE
2636 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2637 | ld TMP0, DISPATCH_J(trace)(DISPATCH)
2639 | daddu TMP0, TMP0, RD
2640 | ld TRACE:TMP2, 0(TMP0)
2641 | lw INS, TRACE:TMP2->startins
2642 | decode_OP8a TMP1, INS
2644 | daddu TMP0, DISPATCH, TMP1
2645 | decode_RD8a RD, INS
2646 | ld AT, GG_DISP2STATIC(TMP0)
2647 | decode_RA8a RA, INS
2652 |9: // Rethrow error from the right C frame.
2653 | load_got lj_err_trace
2654 | sub CARG2, r0, CRET1
2655 | call_intern lj_err_trace // (lua_State *L, int errcode)
2659 |//-----------------------------------------------------------------------
2660 |//-- Math helper functions ----------------------------------------------
2661 |//-----------------------------------------------------------------------
2663 |// Hard-float round to integer.
2664 |// Modifies AT, TMP0, FRET1, FRET2, f4. Keeps all others incl. FARG1.
2665 |// MIPSR6: Modifies FTMP1, too.
2666 |.macro vm_round_hf, func
2667 | lui TMP0, 0x4330 // Hiword of 2^52 (double).
2668 | dsll TMP0, TMP0, 32
2670 | abs.d FRET2, FARG1 // |x|
2673 | cmp.lt.d FTMP1, FRET2, f4
2674 | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52
2675 | bc1eqz FTMP1, >1 // Truncate only if |x| < 2^52.
2677 | c.olt.d 0, FRET2, f4
2678 | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52
2679 | bc1f 0, >1 // Truncate only if |x| < 2^52.
2681 |. sub.d FRET1, FRET1, f4
2683 |.if "func" == "ceil"
2684 | lui TMP0, 0xbff0 // Hiword of -1 (double). Preserves -0.
2686 | lui TMP0, 0x3ff0 // Hiword of +1 (double).
2688 |.if "func" == "trunc"
2689 | dsll TMP0, TMP0, 32
2692 | cmp.lt.d FTMP1, FRET2, FRET1 // |x| < result?
2693 | sub.d FRET2, FRET1, f4
2694 | sel.d FTMP1, FRET1, FRET2 // If yes, subtract +1.
2696 | neg.d FRET2, FTMP1
2698 |. sel.d FRET1, FTMP1, FRET2 // Merge sign bit back in.
2700 | c.olt.d 0, FRET2, FRET1 // |x| < result?
2701 | sub.d FRET2, FRET1, f4
2702 | movt.d FRET1, FRET2, 0 // If yes, subtract +1.
2703 | neg.d FRET2, FRET1
2705 |. movn.d FRET1, FRET2, AT // Merge sign bit back in.
2708 | neg.d FRET2, FRET1
2709 | dsll TMP0, TMP0, 32
2713 | sel.d FTMP1, FRET1, FRET2
2714 |.if "func" == "ceil"
2715 | cmp.lt.d FRET1, FTMP1, FARG1 // x > result?
2717 | cmp.lt.d FRET1, FARG1, FTMP1 // x < result?
2719 | sub.d FRET2, FTMP1, f4 // If yes, subtract +-1.
2721 |. sel.d FRET1, FTMP1, FRET2
2723 | movn.d FRET1, FRET2, AT // Merge sign bit back in.
2724 |.if "func" == "ceil"
2725 | c.olt.d 0, FRET1, FARG1 // x > result?
2727 | c.olt.d 0, FARG1, FRET1 // x < result?
2729 | sub.d FRET2, FRET1, f4 // If yes, subtract +-1.
2731 |. movt.d FRET1, FRET2, 0
2736 |. mov.d FRET1, FARG1
2739 |.macro vm_round, func
2754 |// Soft-float integer to number conversion.
2757 | beqz ARG, >9 // Handle zero first.
2758 |. sra TMP0, ARG, 31
2759 | xor TMP1, ARG, TMP0
2760 | dsubu TMP1, TMP1, TMP0 // Absolute value in TMP1.
2762 | addiu ARG, ARG, -11
2763 | li AT, 0x3ff+63-11-1
2764 | dsllv TMP1, TMP1, ARG // Align mantissa left with leading 1.
2765 | subu ARG, AT, ARG // Exponent - 1.
2766 | ins ARG, TMP0, 11, 11 // Sign | Exponent.
2767 | dsll ARG, ARG, 52 // Align left.
2769 |. daddu ARG, ARG, TMP1 // Add mantissa, increment exponent.
2776 |// Input CARG1. Output: CARG1. Temporaries: AT, TMP0, TMP1.
2780 |// Input CARG2. Output: CARG2. Temporaries: AT, TMP0, TMP1.
2784 |// Soft-float comparison. Equivalent to c.eq.d.
2785 |// Input: CARG*. Output: CRET1. Temporaries: AT, TMP0, TMP1.
2789 | dsll TMP0, CARG2, 1
2791 | beqz TMP1, >8 // Both args +-0: return 1.
2793 | dsll TMP1, TMP1, 32
2795 | sltu TMP0, TMP1, TMP0
2797 | bnez TMP1, >9 // Either arg is NaN: return 0;
2798 |. xor AT, CARG1, CARG2
2800 |. sltiu CRET1, AT, 1 // Same values: return 1.
2809 |// Soft-float comparison. Equivalent to c.ult.d and c.olt.d.
2810 |// Input: CARG1, CARG2. Output: CRET1. Temporaries: AT, TMP0, TMP1, CRET2.
2822 | dsll TMP0, CARG2, 1
2824 | beqz TMP1, >8 // Both args +-0: return 0.
2826 | dsll TMP1, TMP1, 32
2828 | sltu TMP0, TMP1, TMP0
2830 | bnez TMP1, >9 // Either arg is NaN: return 0 or 1;
2831 |. and AT, CARG1, CARG2
2832 | bltz AT, >5 // Both args negative?
2835 |. slt CRET1, CARG1, CARG2
2836 |5: // Swap conditions if both operands are negative.
2838 |. slt CRET1, CARG2, CARG1
2844 |. move CRET1, CRET2
2850 | dsll TMP0, CARG1, 1
2852 | beqz TMP1, >8 // Both args +-0: return 0.
2854 | dsll TMP1, TMP1, 32
2856 | sltu TMP0, TMP1, TMP0
2858 | bnez TMP1, >9 // Either arg is NaN: return 0 or 1;
2859 |. and AT, CARG2, CARG1
2860 | bltz AT, >5 // Both args negative?
2863 |. slt CRET1, CARG2, CARG1
2864 |5: // Swap conditions if both operands are negative.
2866 |. slt CRET1, CARG1, CARG2
2875 |// Soft-float comparison. Equivalent to c.ole.d a, b or c.ole.d b, a.
2876 |// Input: CARG1, CARG2, TMP3. Output: CRET1. Temporaries: AT, TMP0, TMP1.
2880 | dsll TMP0, CARG2, 1
2882 | beqz TMP1, >8 // Both args +-0: return 1.
2884 | dsll TMP1, TMP1, 32
2886 | sltu TMP0, TMP1, TMP0
2888 | bnez TMP1, >9 // Either arg is NaN: return 0;
2889 |. and AT, CARG1, CARG2
2891 | bltz AT, >5 // Both args negative?
2894 |. slt CRET1, CARG2, CARG1
2895 |5: // Swap conditions if both operands are negative.
2897 |. slt CRET1, CARG1, CARG2
2906 |.macro sfmin_max, name, fpcall
2908 |.if JIT and not FPU
2916 | selnez CRET1, CRET1, TMP0
2917 | seleqz TMP0, CARG2, TMP0
2919 |. or CRET1, CRET1, TMP0
2922 |. movz CRET1, CARG2, TMP0
2927 | sfmin_max min, vm_sfcmpolt
2928 | sfmin_max max, vm_sfcmpogt
2930 |//-----------------------------------------------------------------------
2931 |//-- Miscellaneous functions --------------------------------------------
2932 |//-----------------------------------------------------------------------
2934 |.define NEXT_TAB, TAB:CARG1
2935 |.define NEXT_IDX, CARG2
2936 |.define NEXT_ASIZE, CARG3
2937 |.define NEXT_NIL, CARG4
2938 |.define NEXT_TMP0, r12
2939 |.define NEXT_TMP1, r13
2940 |.define NEXT_TMP2, r14
2941 |.define NEXT_RES_VK, CRET1
2942 |.define NEXT_RES_IDX, CRET2
2943 |.define NEXT_RES_PTR, sp
2944 |.define NEXT_RES_VAL, 0(sp)
2945 |.define NEXT_RES_KEY, 8(sp)
2947 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2948 |// Next idx returned in CRET2.
2950 |.if JIT and ENDIAN_LE
2951 | lw NEXT_ASIZE, NEXT_TAB->asize
2952 | ld NEXT_TMP0, NEXT_TAB->array
2953 | li NEXT_NIL, LJ_TNIL
2954 |1: // Traverse array part.
2955 | sltu AT, NEXT_IDX, NEXT_ASIZE
2956 | sll NEXT_TMP1, NEXT_IDX, 3
2958 |. daddu NEXT_TMP1, NEXT_TMP0, NEXT_TMP1
2960 | ld NEXT_TMP2, 0(NEXT_TMP1)
2962 | or NEXT_TMP1, NEXT_IDX, AT
2963 | beq NEXT_TMP2, NEXT_NIL, <1
2964 |. addiu NEXT_IDX, NEXT_IDX, 1
2965 | sd NEXT_TMP2, NEXT_RES_VAL
2966 | sd NEXT_TMP1, NEXT_RES_KEY
2967 | move NEXT_RES_VK, NEXT_RES_PTR
2969 |. move NEXT_RES_IDX, NEXT_IDX
2971 |5: // Traverse hash part.
2972 | subu NEXT_RES_IDX, NEXT_IDX, NEXT_ASIZE
2973 | ld NODE:NEXT_RES_VK, NEXT_TAB->node
2974 | sll NEXT_TMP2, NEXT_RES_IDX, 5
2975 | lw NEXT_TMP0, NEXT_TAB->hmask
2976 | sll AT, NEXT_RES_IDX, 3
2977 | subu AT, NEXT_TMP2, AT
2978 | daddu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, AT
2980 | sltu AT, NEXT_TMP0, NEXT_RES_IDX
2983 | ld NEXT_TMP2, NODE:NEXT_RES_VK->val
2984 | bne NEXT_TMP2, NEXT_NIL, >9
2985 |. addiu NEXT_RES_IDX, NEXT_RES_IDX, 1
2986 | // Skip holes in hash part.
2988 |. daddiu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, sizeof(Node)
2990 |8: // End of iteration. Set the key to nil (not the value).
2991 | sd NEXT_NIL, NEXT_RES_KEY
2992 | move NEXT_RES_VK, NEXT_RES_PTR
2995 |. addu NEXT_RES_IDX, NEXT_RES_IDX, NEXT_ASIZE
2998 |//-----------------------------------------------------------------------
2999 |//-- FFI helper functions -----------------------------------------------
3000 |//-----------------------------------------------------------------------
3002 |// Handler for callback functions. Callback slot number in r1, g in r2.
3005 |.type CTSTATE, CTState, PC
3007 | ld CTSTATE, GL:r2->ctype_state
3008 | daddiu DISPATCH, r2, GG_G2DISP
3009 | load_got lj_ccallback_enter
3010 | sw r1, CTSTATE->cb.slot
3011 | sd CARG1, CTSTATE->cb.gpr[0]
3012 | .FPU sdc1 FARG1, CTSTATE->cb.fpr[0]
3013 | sd CARG2, CTSTATE->cb.gpr[1]
3014 | .FPU sdc1 FARG2, CTSTATE->cb.fpr[1]
3015 | sd CARG3, CTSTATE->cb.gpr[2]
3016 | .FPU sdc1 FARG3, CTSTATE->cb.fpr[2]
3017 | sd CARG4, CTSTATE->cb.gpr[3]
3018 | .FPU sdc1 FARG4, CTSTATE->cb.fpr[3]
3019 | sd CARG5, CTSTATE->cb.gpr[4]
3020 | .FPU sdc1 FARG5, CTSTATE->cb.fpr[4]
3021 | sd CARG6, CTSTATE->cb.gpr[5]
3022 | .FPU sdc1 FARG6, CTSTATE->cb.fpr[5]
3023 | sd CARG7, CTSTATE->cb.gpr[6]
3024 | .FPU sdc1 FARG7, CTSTATE->cb.fpr[6]
3025 | sd CARG8, CTSTATE->cb.gpr[7]
3026 | .FPU sdc1 FARG8, CTSTATE->cb.fpr[7]
3027 | daddiu TMP0, sp, CFRAME_SPACE
3028 | sd TMP0, CTSTATE->cb.stack
3029 | sd r0, SAVE_PC // Any value outside of bytecode is ok.
3031 | call_intern lj_ccallback_enter // (CTState *cts, void *cf)
3032 |. move CARG1, CTSTATE
3033 | // Returns lua_State *.
3034 | ld BASE, L:CRET1->base
3035 | ld RC, L:CRET1->top
3037 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
3038 | ld LFUNC:RB, FRAME_FUNC(BASE)
3039 | .FPU mtc1 TMP3, TOBIT
3040 | li TISNIL, LJ_TNIL
3041 | li TISNUM, LJ_TISNUM
3046 | .FPU cvt.d.s TOBIT, TOBIT
3050 |->cont_ffi_callback: // Return from FFI callback.
3052 | load_got lj_ccallback_leave
3053 | ld CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
3058 | call_intern lj_ccallback_leave // (CTState *cts, TValue *o)
3059 |. move CARG1, CTSTATE
3060 | .FPU ldc1 FRET1, CTSTATE->cb.fpr[0]
3061 | ld CRET1, CTSTATE->cb.gpr[0]
3062 | .FPU ldc1 FRET2, CTSTATE->cb.fpr[1]
3064 |. ld CRET2, CTSTATE->cb.gpr[1]
3067 |->vm_ffi_call: // Call C function via FFI.
3068 | // Caveat: needs special frame unwinding, see below.
3070 | .type CCSTATE, CCallState, CARG1
3071 | lw TMP1, CCSTATE->spadj
3072 | lbu CARG2, CCSTATE->nsp
3074 | dsubu sp, sp, TMP1
3077 | sd CCSTATE, -24(TMP2)
3079 | daddiu TMP1, CCSTATE, offsetof(CCallState, stack)
3082 |. daddu TMP3, TMP1, CARG2
3085 | daddiu TMP1, TMP1, 8
3086 | sltu AT, TMP1, TMP3
3089 |. daddiu TMP2, TMP2, 8
3091 | ld CFUNCADDR, CCSTATE->func
3092 | .FPU ldc1 FARG1, CCSTATE->gpr[0]
3093 | ld CARG2, CCSTATE->gpr[1]
3094 | .FPU ldc1 FARG2, CCSTATE->gpr[1]
3095 | ld CARG3, CCSTATE->gpr[2]
3096 | .FPU ldc1 FARG3, CCSTATE->gpr[2]
3097 | ld CARG4, CCSTATE->gpr[3]
3098 | .FPU ldc1 FARG4, CCSTATE->gpr[3]
3099 | ld CARG5, CCSTATE->gpr[4]
3100 | .FPU ldc1 FARG5, CCSTATE->gpr[4]
3101 | ld CARG6, CCSTATE->gpr[5]
3102 | .FPU ldc1 FARG6, CCSTATE->gpr[5]
3103 | ld CARG7, CCSTATE->gpr[6]
3104 | .FPU ldc1 FARG7, CCSTATE->gpr[6]
3105 | ld CARG8, CCSTATE->gpr[7]
3106 | .FPU ldc1 FARG8, CCSTATE->gpr[7]
3108 |. ld CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
3109 | ld CCSTATE:TMP1, -24(r16)
3112 | sd CRET1, CCSTATE:TMP1->gpr[0]
3113 | sd CRET2, CCSTATE:TMP1->gpr[1]
3115 | sdc1 FRET1, CCSTATE:TMP1->fpr[0]
3116 | sdc1 FRET2, CCSTATE:TMP1->fpr[1]
3118 | sd CARG1, CCSTATE:TMP1->gpr[2] // 2nd FP struct field for soft-float.
3124 |// Note: vm_ffi_call must be the last function in this object file!
3126 |//-----------------------------------------------------------------------
3129 /* Generate the code for a single instruction. */
3130 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3137 /* -- Comparison ops ---------------------------------------------------- */
3139 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3141 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3142 | // RA = src1*8, RD = src2*8, JMP with RD = target
3143 |.macro bc_comp, FRA, FRD, ARGRA, ARGRD, movop, fmovop, fcomp, sfcomp
3144 | daddu RA, BASE, RA
3145 | daddu RD, BASE, RD
3148 | lhu TMP2, OFS_RD(PC)
3149 | gettp CARG3, ARGRA
3150 | gettp CARG4, ARGRD
3151 | bne CARG3, TISNUM, >2
3153 | bne CARG4, TISNUM, >5
3155 | sextw ARGRA, ARGRA
3156 | sextw ARGRD, ARGRD
3157 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3158 | slt AT, CARG1, CARG2
3159 | addu TMP2, TMP2, TMP3
3161 | movop TMP2, TMP2, AT
3163 | movop TMP2, r0, AT
3166 | daddu PC, PC, TMP2
3169 |2: // RA is not an integer.
3170 | sltiu AT, CARG3, LJ_TISNUM
3171 | beqz AT, ->vmeta_comp
3172 |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3173 | sltiu AT, CARG4, LJ_TISNUM
3180 |3: // RA and RD are both numbers.
3183 | fcomp FTMP0, FTMP0, FTMP2
3184 | addu TMP2, TMP2, TMP3
3187 |. fmovop TMP2, TMP2, TMP3
3189 | fcomp FTMP0, FTMP2
3190 | addu TMP2, TMP2, TMP3
3196 |. addu TMP2, TMP2, TMP3
3199 |. movop TMP2, TMP2, CRET1
3201 |. movop TMP2, r0, CRET1
3205 |4: // RA is a number, RD is not a number.
3206 | bne CARG4, TISNUM, ->vmeta_comp
3207 | // RA is a number, RD is an integer. Convert RD to a number.
3214 |.if "ARGRD" == "CARG1"
3215 |. sextw CARG1, CARG1
3219 |. sextw CARG2, CARG2
3227 |5: // RA is an integer, RD is not an integer
3228 | sltiu AT, CARG4, LJ_TISNUM
3229 | beqz AT, ->vmeta_comp
3230 |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3231 | // RA is an integer, RD is a number. Convert RA to a number.
3238 |.if "ARGRA" == "CARG1"
3240 |. sextw CARG1, CARG1
3243 |. sextw CARG2, CARG2
3251 if (op == BC_ISLT) {
3252 | bc_comp FTMP0, FTMP2, CARG1, CARG2, selnez, selnez, cmp.lt.d, ->vm_sfcmpolt
3253 } else if (op == BC_ISGE) {
3254 | bc_comp FTMP0, FTMP2, CARG1, CARG2, seleqz, seleqz, cmp.lt.d, ->vm_sfcmpolt
3255 } else if (op == BC_ISLE) {
3256 | bc_comp FTMP2, FTMP0, CARG2, CARG1, seleqz, seleqz, cmp.ult.d, ->vm_sfcmpult
3258 | bc_comp FTMP2, FTMP0, CARG2, CARG1, selnez, selnez, cmp.ult.d, ->vm_sfcmpult
3261 if (op == BC_ISLT) {
3262 | bc_comp FTMP0, FTMP2, CARG1, CARG2, movz, movf, c.olt.d, ->vm_sfcmpolt
3263 } else if (op == BC_ISGE) {
3264 | bc_comp FTMP0, FTMP2, CARG1, CARG2, movn, movt, c.olt.d, ->vm_sfcmpolt
3265 } else if (op == BC_ISLE) {
3266 | bc_comp FTMP2, FTMP0, CARG2, CARG1, movn, movt, c.ult.d, ->vm_sfcmpult
3268 | bc_comp FTMP2, FTMP0, CARG2, CARG1, movz, movf, c.ult.d, ->vm_sfcmpult
3273 case BC_ISEQV: case BC_ISNEV:
3274 vk = op == BC_ISEQV;
3275 | // RA = src1*8, RD = src2*8, JMP with RD = target
3276 | daddu RA, BASE, RA
3278 | daddu RD, BASE, RD
3280 | lhu TMP2, -4+OFS_RD(PC)
3282 | gettp CARG3, CARG1
3283 | gettp CARG4, CARG2
3284 | sltu AT, TISNUM, CARG3
3285 | sltu TMP1, TISNUM, CARG4
3288 | beqz AT, ->BC_ISEQN_Z
3290 | beqz AT, ->BC_ISNEN_Z
3292 | // Either or both types are not numbers.
3293 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3296 | beq CARG3, AT, ->vmeta_equal_cd
3300 | beq CARG4, AT, ->vmeta_equal_cd
3303 | bne CARG1, CARG2, >2
3304 |. addu TMP2, TMP2, TMP3
3305 | // Tag and value are equal.
3308 | daddu PC, PC, TMP2
3313 |2: // Check if the tags are the same and it's a table or userdata.
3314 | xor AT, CARG3, CARG4 // Same type?
3315 | sltiu TMP0, CARG3, LJ_TISTABUD+1 // Table or userdata?
3317 | seleqz TMP0, TMP0, AT
3324 | beqz TMP0, ->BC_ISEQV_Z // Reuse code from opposite instruction.
3326 | // Different tables or userdatas. Need to check __eq metamethod.
3327 | // Field metatable must be at same offset for GCtab and GCudata!
3328 |. cleartp TAB:TMP1, CARG1
3329 | ld TAB:TMP3, TAB:TMP1->metatable
3331 | beqz TAB:TMP3, <1 // No metatable?
3333 | lbu TMP3, TAB:TMP3->nomm
3334 | andi TMP3, TMP3, 1<<MM_eq
3335 | bnez TMP3, >1 // Or 'no __eq' flag set?
3337 | beqz TAB:TMP3,->BC_ISEQV_Z // No metatable?
3339 | lbu TMP3, TAB:TMP3->nomm
3340 | andi TMP3, TMP3, 1<<MM_eq
3341 | bnez TMP3, ->BC_ISEQV_Z // Or 'no __eq' flag set?
3344 | b ->vmeta_equal // Handle __eq metamethod.
3345 |. li TMP0, 1-vk // ne = 0 or 1.
3348 case BC_ISEQS: case BC_ISNES:
3349 vk = op == BC_ISEQS;
3350 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3351 | daddu RA, BASE, RA
3354 | dsubu RD, KBASE, RD
3355 | lhu TMP2, -4+OFS_RD(PC)
3356 | ld CARG2, -8(RD) // KBASE-8-str_const*8
3364 | beq TMP0, AT, ->vmeta_equal_cd
3366 |. settp CARG2, TMP1
3367 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3368 | xor TMP1, CARG1, CARG2
3369 | addu TMP2, TMP2, TMP3
3372 | seleqz TMP2, TMP2, TMP1
3374 | selnez TMP2, TMP2, TMP1
3378 | movn TMP2, r0, TMP1
3380 | movz TMP2, r0, TMP1
3383 | daddu PC, PC, TMP2
3387 case BC_ISEQN: case BC_ISNEN:
3388 vk = op == BC_ISEQN;
3389 | // RA = src*8, RD = num_const*8, JMP with RD = target
3390 | daddu RA, BASE, RA
3391 | daddu RD, KBASE, RD
3394 | lhu TMP2, OFS_RD(PC)
3395 | gettp CARG3, CARG1
3396 | gettp CARG4, CARG2
3398 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3404 | bne CARG3, TISNUM, >3
3406 | bne CARG4, TISNUM, >6
3407 |. addu TMP2, TMP2, TMP3
3408 | xor AT, CARG1, CARG2
3411 | seleqz TMP2, TMP2, AT
3413 | daddu PC, PC, TMP2
3416 | selnez TMP2, TMP2, AT
3419 | daddu PC, PC, TMP2
3425 | daddu PC, PC, TMP2
3431 | daddu PC, PC, TMP2
3436 |3: // RA is not an integer.
3437 | sltu AT, CARG3, TISNUM
3443 |. addu TMP2, TMP2, TMP3
3444 | sltu AT, CARG4, TISNUM
3451 |4: // RA and RD are both numbers.
3454 | cmp.eq.d FTMP0, FTMP0, FTMP2
3458 |. selnez TMP2, TMP2, TMP1
3460 |. seleqz TMP2, TMP2, TMP1
3463 | c.eq.d FTMP0, FTMP2
3477 |. selnez TMP2, TMP2, CRET1
3479 |. seleqz TMP2, TMP2, CRET1
3483 |. movz TMP2, r0, CRET1
3485 |. movn TMP2, r0, CRET1
3490 |5: // RA is a number, RD is not a number.
3492 | bne CARG4, TISNUM, >9
3494 | bne CARG4, TISNUM, <2
3496 | // RA is a number, RD is an integer. Convert RD to a number.
3498 |. lwc1 FTMP2, LO(RD)
3500 |. cvt.d.w FTMP2, FTMP2
3502 |. sextw CARG2, CARG2
3509 |6: // RA is an integer, RD is not an integer
3510 | sltu AT, CARG4, TISNUM
3516 | // RA is an integer, RD is a number. Convert RA to a number.
3518 |. lwc1 FTMP0, LO(RA)
3521 | cvt.d.w FTMP0, FTMP0
3523 |. sextw CARG1, CARG1
3535 | b ->vmeta_equal_cd
3541 | b ->vmeta_equal_cd
3546 case BC_ISEQP: case BC_ISNEP:
3547 vk = op == BC_ISEQP;
3548 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3549 | daddu RA, BASE, RA
3552 | lhu TMP2, OFS_RD(PC)
3558 | beq TMP0, AT, ->vmeta_equal_cd
3560 |. xor TMP0, TMP0, TMP1
3562 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3563 | addu TMP2, TMP2, TMP3
3566 | seleqz TMP2, TMP2, TMP0
3568 | selnez TMP2, TMP2, TMP0
3572 | movn TMP2, r0, TMP0
3574 | movz TMP2, r0, TMP0
3577 | daddu PC, PC, TMP2
3581 /* -- Unary test and copy ops ------------------------------------------- */
3583 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3584 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3585 | daddu RD, BASE, RD
3586 | lhu TMP2, OFS_RD(PC)
3590 | sltiu TMP0, TMP0, LJ_TISTRUECOND
3591 if (op == BC_IST || op == BC_ISF) {
3593 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3594 | addu TMP2, TMP2, TMP3
3597 | selnez TMP2, TMP2, TMP0;
3599 | seleqz TMP2, TMP2, TMP0;
3603 | movz TMP2, r0, TMP0
3605 | movn TMP2, r0, TMP0
3608 | daddu PC, PC, TMP2
3611 if (op == BC_ISTC) {
3616 |. daddu RA, BASE, RA
3618 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3619 | addu TMP2, TMP2, TMP3
3621 | daddu PC, PC, TMP2
3628 | // RA = src*8, RD = -type*8
3629 | daddu TMP2, BASE, RA
3634 | daddu AT, TMP0, TMP1
3635 | bnez AT, ->vmeta_istype
3639 | // RA = src*8, RD = -(TISNUM-1)*8
3640 | daddu TMP2, BASE, RA
3643 | checknum TMP0, ->vmeta_istype
3647 /* -- Unary ops --------------------------------------------------------- */
3650 | // RA = dst*8, RD = src*8
3651 | daddu RD, BASE, RD
3652 | daddu RA, BASE, RA
3659 | // RA = dst*8, RD = src*8
3660 | daddu RD, BASE, RD
3661 | daddu RA, BASE, RA
3665 | sltu TMP0, AT, TMP0
3666 | addiu TMP0, TMP0, 1
3667 | dsll TMP0, TMP0, 47
3674 | // RA = dst*8, RD = src*8
3675 | daddu RB, BASE, RD
3677 | daddu RA, BASE, RA
3678 | gettp CARG3, CARG1
3679 | bne CARG3, TISNUM, >2
3681 | sextw CARG1, CARG1
3682 | beq CARG1, TMP1, ->vmeta_unm // Meta handler deals with -2^31.
3683 |. negu CARG1, CARG1
3684 | zextw CARG1, CARG1
3685 | settp CARG1, TISNUM
3691 | sltiu AT, CARG3, LJ_TISNUM
3692 | beqz AT, ->vmeta_unm
3693 |. dsll TMP1, TMP1, 32
3695 |. xor CARG1, CARG1, TMP1
3698 | // RA = dst*8, RD = src*8
3699 | daddu CARG2, BASE, RD
3700 | daddu RA, BASE, RA
3703 | daddiu AT, TMP1, -LJ_TSTR
3705 |. cleartp STR:CARG1, TMP0
3706 | lw CRET1, STR:CARG1->len
3708 | settp CRET1, TISNUM
3713 | daddiu AT, TMP1, -LJ_TTAB
3714 | bnez AT, ->vmeta_len
3717 | ld TAB:TMP2, TAB:CARG1->metatable
3723 | load_got lj_tab_len
3724 | call_intern lj_tab_len // (GCtab *t)
3726 | // Returns uint32_t (but less than 2^31).
3731 | lbu TMP0, TAB:TMP2->nomm
3732 | andi TMP0, TMP0, 1<<MM_len
3733 | bnez TMP0, <3 // 'no __len' flag set: done.
3740 /* -- Binary ops -------------------------------------------------------- */
3742 |.macro fpmod, a, b, c
3743 | bal ->vm_floor // floor(b/c)
3744 |. div.d FARG1, b, c
3746 | sub.d a, b, a // b - floor(b/c)*c
3750 | daddiu sp, sp, -16
3759 |. move CARG1, CRET1
3769 |. move CARG2, CRET1
3774 |.macro ins_arithpre, label
3775 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3776 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3779 | decode_RB8a RB, INS
3781 | decode_RDtoRC8 RC, RD
3782 | // RA = dst*8, RB = src1*8, RC = num_const*8
3783 | daddu RB, BASE, RB
3784 |.if "label" ~= "none"
3787 |. daddu RC, KBASE, RC
3790 | decode_RB8a RC, INS
3792 | decode_RDtoRC8 RB, RD
3793 | // RA = dst*8, RB = num_const*8, RC = src1*8
3794 | daddu RC, BASE, RC
3795 |.if "label" ~= "none"
3798 |. daddu RB, KBASE, RB
3801 | decode_RB8a RB, INS
3803 | decode_RDtoRC8 RC, RD
3804 | // RA = dst*8, RB = src1*8, RC = src2*8
3805 | daddu RB, BASE, RB
3806 |.if "label" ~= "none"
3809 |. daddu RC, BASE, RC
3814 |.macro ins_arith, intins, fpins, fpcall, label
3817 |.if "label" ~= "none"
3827 |.if "intins" ~= "div"
3829 | // Check for two integers.
3830 | sextw CARG3, CARG1
3831 | bne TMP0, TISNUM, >5
3832 |. sextw CARG4, CARG2
3833 | bne TMP1, TISNUM, >5
3835 |.if "intins" == "addu"
3836 |. intins CRET1, CARG3, CARG4
3837 | xor TMP1, CRET1, CARG3 // ((y^a) & (y^b)) < 0: overflow.
3838 | xor TMP2, CRET1, CARG4
3839 | and TMP1, TMP1, TMP2
3840 | bltz TMP1, ->vmeta_arith
3841 |. daddu RA, BASE, RA
3842 |.elif "intins" == "subu"
3843 |. intins CRET1, CARG3, CARG4
3844 | xor TMP1, CRET1, CARG3 // ((y^a) & (a^b)) < 0: overflow.
3845 | xor TMP2, CARG3, CARG4
3846 | and TMP1, TMP1, TMP2
3847 | bltz TMP1, ->vmeta_arith
3848 |. daddu RA, BASE, RA
3849 |.elif "intins" == "mult"
3852 | mul CRET1, CARG3, CARG4
3853 | muh TMP2, CARG3, CARG4
3855 |. intins CARG3, CARG4
3859 | sra TMP1, CRET1, 31
3860 | bne TMP1, TMP2, ->vmeta_arith
3861 |. daddu RA, BASE, RA
3863 |. load_got lj_vm_modi
3864 | beqz CARG4, ->vmeta_arith
3865 |. daddu RA, BASE, RA
3868 |. move CARG2, CARG4
3871 | zextw CRET1, CRET1
3872 | settp CRET1, TISNUM
3880 |5: // Check for two numbers.
3881 | .FPU ldc1 FTMP0, 0(RB)
3882 | sltu AT, TMP0, TISNUM
3883 | sltu TMP0, TMP1, TISNUM
3884 | .FPU ldc1 FTMP2, 0(RC)
3886 | beqz AT, ->vmeta_arith
3887 |. daddu RA, BASE, RA
3890 | fpins FRET1, FTMP0, FTMP2
3891 |.elif "fpcall" == "sfpmod"
3900 |.if "intins" ~= "div"
3904 |. sdc1 FRET1, 0(RA)
3908 |.if "intins" == "div"
3914 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3915 | ins_arith addu, add.d, __adddf3, none
3917 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3918 | ins_arith subu, sub.d, __subdf3, none
3920 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3921 | ins_arith mult, mul.d, __muldf3, none
3924 | ins_arith div, div.d, __divdf3, ->BC_DIVVN_Z
3926 case BC_DIVNV: case BC_DIVVV:
3927 | ins_arithpre ->BC_DIVVN_Z
3930 | ins_arith modi, fpmod, sfpmod, ->BC_MODVN_Z
3932 case BC_MODNV: case BC_MODVV:
3933 | ins_arithpre ->BC_MODVN_Z
3941 | sltiu TMP0, TMP0, LJ_TISNUM
3942 | sltiu TMP1, TMP1, LJ_TISNUM
3943 | and AT, TMP0, TMP1
3945 | beqz AT, ->vmeta_arith
3946 |. daddu RA, BASE, RA
3963 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3964 | decode_RB8a RB, INS
3966 | decode_RDtoRC8 RC, RD
3967 | dsubu CARG3, RC, RB
3969 | daddu CARG2, BASE, RC
3972 | load_got lj_meta_cat
3973 | srl CARG3, CARG3, 3
3975 | call_intern lj_meta_cat // (lua_State *L, TValue *top, int left)
3977 | // Returns NULL (finished) or TValue * (metamethod).
3978 | bnez CRET1, ->vmeta_binop
3980 | daddu RB, BASE, MULTRES
3982 | daddu RA, BASE, RA
3988 /* -- Constant ops ------------------------------------------------------ */
3991 | // RA = dst*8, RD = str_const*8 (~)
3992 | dsubu TMP1, KBASE, RD
3995 | ld TMP0, -8(TMP1) // KBASE-8-str_const*8
3996 | daddu RA, BASE, RA
4003 | // RA = dst*8, RD = cdata_const*8 (~)
4004 | dsubu TMP1, KBASE, RD
4006 | ld TMP0, -8(TMP1) // KBASE-8-cdata_const*8
4007 | li TMP2, LJ_TCDATA
4008 | daddu RA, BASE, RA
4015 | // RA = dst*8, RD = int16_literal*8
4017 | daddu RA, BASE, RA
4025 | // RA = dst*8, RD = num_const*8
4026 | daddu RD, KBASE, RD
4027 | daddu RA, BASE, RA
4034 | // RA = dst*8, RD = primitive_type*8 (~)
4035 | daddu RA, BASE, RA
4043 | // RA = base*8, RD = end*8
4044 | daddu RA, BASE, RA
4047 | daddu RD, BASE, RD
4056 /* -- Upvalue and function ops ------------------------------------------ */
4059 | // RA = dst*8, RD = uvnum*8
4060 | ld LFUNC:RB, FRAME_FUNC(BASE)
4061 | daddu RA, BASE, RA
4063 | daddu RD, RD, LFUNC:RB
4064 | ld UPVAL:RB, LFUNC:RD->uvptr
4066 | ld TMP1, UPVAL:RB->v
4072 | // RA = uvnum*8, RD = src*8
4073 | ld LFUNC:RB, FRAME_FUNC(BASE)
4074 | daddu RD, BASE, RD
4076 | daddu RA, RA, LFUNC:RB
4077 | ld UPVAL:RB, LFUNC:RA->uvptr
4079 | lbu TMP3, UPVAL:RB->marked
4080 | ld CARG2, UPVAL:RB->v
4081 | andi TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
4082 | lbu TMP0, UPVAL:RB->closed
4084 | sd CRET1, 0(CARG2)
4085 | li AT, LJ_GC_BLACK|1
4086 | or TMP3, TMP3, TMP0
4087 | beq TMP3, AT, >2 // Upvalue is closed and black?
4088 |. daddiu TMP2, TMP2, -(LJ_TNUMX+1)
4092 |2: // Check if new value is collectable.
4093 | sltiu AT, TMP2, LJ_TISGCV - (LJ_TNUMX+1)
4094 | beqz AT, <1 // tvisgcv(v)
4095 |. cleartp GCOBJ:CRET1, CRET1
4096 | lbu TMP3, GCOBJ:CRET1->gch.marked
4097 | andi TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
4099 |. load_got lj_gc_barrieruv
4100 | // Crossed a write barrier. Move the barrier forward.
4101 | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv)
4102 |. daddiu CARG1, DISPATCH, GG_DISP2G
4107 | // RA = uvnum*8, RD = str_const*8 (~)
4108 | ld LFUNC:RB, FRAME_FUNC(BASE)
4109 | dsubu TMP1, KBASE, RD
4111 | daddu RA, RA, LFUNC:RB
4112 | ld UPVAL:RB, LFUNC:RA->uvptr
4113 | ld STR:TMP1, -8(TMP1) // KBASE-8-str_const*8
4114 | lbu TMP2, UPVAL:RB->marked
4115 | ld CARG2, UPVAL:RB->v
4116 | lbu TMP3, STR:TMP1->marked
4117 | andi AT, TMP2, LJ_GC_BLACK // isblack(uv)
4118 | lbu TMP2, UPVAL:RB->closed
4122 |. sd TMP1, 0(CARG2)
4126 |2: // Check if string is white and ensure upvalue is closed.
4128 |. andi AT, TMP3, LJ_GC_WHITES // iswhite(str)
4130 |. load_got lj_gc_barrieruv
4131 | // Crossed a write barrier. Move the barrier forward.
4132 | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv)
4133 |. daddiu CARG1, DISPATCH, GG_DISP2G
4138 | // RA = uvnum*8, RD = num_const*8
4139 | ld LFUNC:RB, FRAME_FUNC(BASE)
4140 | daddu RD, KBASE, RD
4142 | daddu RA, RA, LFUNC:RB
4143 | ld UPVAL:RB, LFUNC:RA->uvptr
4145 | ld TMP1, UPVAL:RB->v
4151 | // RA = uvnum*8, RD = primitive_type*8 (~)
4152 | ld LFUNC:RB, FRAME_FUNC(BASE)
4155 | daddu RA, RA, LFUNC:RB
4157 | ld UPVAL:RB, LFUNC:RA->uvptr
4159 | ld TMP1, UPVAL:RB->v
4165 | // RA = level*8, RD = target
4166 | ld TMP2, L->openupval
4167 | branch_RD // Do this first since RD is not saved.
4168 | load_got lj_func_closeuv
4172 | call_intern lj_func_closeuv // (lua_State *L, TValue *level)
4173 |. daddu CARG2, BASE, RA
4180 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
4181 | load_got lj_func_newL_gc
4182 | dsubu TMP1, KBASE, RD
4183 | ld CARG3, FRAME_FUNC(BASE)
4184 | ld CARG2, -8(TMP1) // KBASE-8-tab_const*8
4188 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4189 | call_intern lj_func_newL_gc
4191 | // Returns GCfuncL *.
4196 | daddu RA, BASE, RA
4201 /* -- Table ops --------------------------------------------------------- */
4205 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
4206 | ld TMP0, DISPATCH_GL(gc.total)(DISPATCH)
4207 | ld TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
4210 | sltu AT, TMP0, TMP1
4213 if (op == BC_TNEW) {
4214 | load_got lj_tab_new
4216 | andi CARG2, CARG2, 0x7ff
4218 | addiu AT, CARG2, -0x7ff
4221 | seleqz TMP0, TMP0, AT
4222 | selnez CARG2, CARG2, AT
4223 | or CARG2, CARG2, TMP0
4225 | movz CARG2, TMP0, AT
4227 | // (lua_State *L, int32_t asize, uint32_t hbits)
4228 | call_intern lj_tab_new
4230 | // Returns Table *.
4232 | load_got lj_tab_dup
4233 | dsubu TMP1, KBASE, RD
4235 | call_intern lj_tab_dup // (lua_State *L, Table *kt)
4236 |. ld CARG2, -8(TMP1) // KBASE-8-str_const*8
4237 | // Returns Table *.
4242 | daddu RA, BASE, RA
4247 | load_got lj_gc_step_fixtop
4249 | call_intern lj_gc_step_fixtop // (lua_State *L)
4256 | // RA = dst*8, RD = str_const*8 (~)
4258 | // RA = src*8, RD = str_const*8 (~)
4259 | ld LFUNC:TMP2, FRAME_FUNC(BASE)
4260 | dsubu TMP1, KBASE, RD
4261 | ld STR:RC, -8(TMP1) // KBASE-8-str_const*8
4262 | cleartp LFUNC:TMP2
4263 | ld TAB:RB, LFUNC:TMP2->env
4264 if (op == BC_GGET) {
4269 |. daddu RA, BASE, RA
4273 | // RA = dst*8, RB = table*8, RC = key*8
4274 | decode_RB8a RB, INS
4276 | decode_RDtoRC8 RC, RD
4277 | daddu CARG2, BASE, RB
4278 | daddu CARG3, BASE, RC
4279 | ld TAB:RB, 0(CARG2)
4281 | daddu RA, BASE, RA
4282 | checktab TAB:RB, ->vmeta_tgetv
4284 | bne TMP3, TISNUM, >5 // Integer key?
4285 |. lw TMP0, TAB:RB->asize
4287 | ld TMP1, TAB:RB->array
4288 | sltu AT, TMP2, TMP0
4290 | beqz AT, ->vmeta_tgetv // Integer key and in array part?
4291 |. daddu TMP2, TMP1, TMP2
4293 | beq AT, TISNIL, >2
4294 |. ld CRET1, 0(TMP2)
4300 |2: // Check for __index if table value is nil.
4301 | ld TAB:TMP2, TAB:RB->metatable
4302 | beqz TAB:TMP2, <1 // No metatable: done.
4304 | lbu TMP0, TAB:TMP2->nomm
4305 | andi TMP0, TMP0, 1<<MM_index
4306 | bnez TMP0, <1 // 'no __index' flag set: done.
4313 | bne TMP3, AT, ->vmeta_tgetv
4315 | b ->BC_TGETS_Z // String key?
4319 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
4320 | decode_RB8a RB, INS
4322 | decode_RC8a RC, INS
4323 | daddu CARG2, BASE, RB
4325 | ld TAB:RB, 0(CARG2)
4326 | dsubu CARG3, KBASE, RC
4327 | daddu RA, BASE, RA
4328 | ld STR:RC, -8(CARG3) // KBASE-8-str_const*8
4329 | checktab TAB:RB, ->vmeta_tgets1
4331 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
4332 | lw TMP0, TAB:RB->hmask
4333 | lw TMP1, STR:RC->sid
4334 | ld NODE:TMP2, TAB:RB->node
4335 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4338 | subu TMP1, TMP0, TMP1
4340 | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4341 | settp STR:RC, TMP3 // Tagged key to look for.
4343 | ld CARG1, NODE:TMP2->key
4344 | ld CRET1, NODE:TMP2->val
4345 | ld NODE:TMP1, NODE:TMP2->next
4347 |. ld TAB:TMP3, TAB:RB->metatable
4348 | beq CRET1, TISNIL, >5 // Key found, but nil value?
4355 |4: // Follow hash chain.
4356 | bnez NODE:TMP1, <1
4357 |. move NODE:TMP2, NODE:TMP1
4358 | // End of hash chain: key not found, nil result.
4360 |5: // Check for __index if table value is nil.
4361 | beqz TAB:TMP3, <3 // No metatable: done.
4362 |. move CRET1, TISNIL
4363 | lbu TMP0, TAB:TMP3->nomm
4364 | andi TMP0, TMP0, 1<<MM_index
4365 | bnez TMP0, <3 // 'no __index' flag set: done.
4371 | // RA = dst*8, RB = table*8, RC = index*8
4372 | decode_RB8a RB, INS
4374 | daddu CARG2, BASE, RB
4375 | decode_RDtoRC8 RC, RD
4376 | ld TAB:RB, 0(CARG2)
4377 | daddu RA, BASE, RA
4379 | checktab TAB:RB, ->vmeta_tgetb
4380 | lw TMP1, TAB:RB->asize
4381 | ld TMP2, TAB:RB->array
4382 | sltu AT, TMP0, TMP1
4383 | beqz AT, ->vmeta_tgetb
4384 |. daddu RC, TMP2, RC
4386 | beq AT, TISNIL, >5
4393 |5: // Check for __index if table value is nil.
4394 | ld TAB:TMP2, TAB:RB->metatable
4395 | beqz TAB:TMP2, <1 // No metatable: done.
4397 | lbu TMP1, TAB:TMP2->nomm
4398 | andi TMP1, TMP1, 1<<MM_index
4399 | bnez TMP1, <1 // 'no __index' flag set: done.
4401 | b ->vmeta_tgetb // Caveat: preserve TMP0 and CARG2!
4405 | // RA = dst*8, RB = table*8, RC = key*8
4406 | decode_RB8a RB, INS
4408 | decode_RDtoRC8 RC, RD
4409 | daddu RB, BASE, RB
4410 | daddu RC, BASE, RC
4411 | ld TAB:CARG1, 0(RB)
4413 | daddu RA, BASE, RA
4415 | lw TMP0, TAB:CARG1->asize
4416 | ld TMP1, TAB:CARG1->array
4417 | sltu AT, CARG2, TMP0
4418 | sll TMP2, CARG2, 3
4419 | beqz AT, ->vmeta_tgetr // In array part?
4420 |. daddu CRET1, TMP1, TMP2
4421 | ld CARG2, 0(CRET1)
4429 | // RA = src*8, RB = table*8, RC = key*8
4430 | decode_RB8a RB, INS
4432 | decode_RDtoRC8 RC, RD
4433 | daddu CARG2, BASE, RB
4434 | daddu CARG3, BASE, RC
4437 | daddu RA, BASE, RA
4438 | checktab RB, ->vmeta_tsetv
4441 | lw TMP0, TAB:RB->asize
4442 | ld TMP1, TAB:RB->array
4445 | beqz AT, ->vmeta_tsetv // Integer key and in array part?
4446 |. daddu TMP1, TMP1, TMP2
4448 | lbu TMP3, TAB:RB->marked
4449 | beq TMP0, TISNIL, >3
4452 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4454 |. sd CRET1, 0(TMP1)
4458 |3: // Check for __newindex if previous value is nil.
4459 | ld TAB:TMP2, TAB:RB->metatable
4460 | beqz TAB:TMP2, <1 // No metatable: done.
4462 | lbu TMP2, TAB:TMP2->nomm
4463 | andi TMP2, TMP2, 1<<MM_newindex
4464 | bnez TMP2, <1 // 'no __newindex' flag set: done.
4471 | daddiu AT, AT, -LJ_TSTR
4472 | bnez AT, ->vmeta_tsetv
4474 | b ->BC_TSETS_Z // String key?
4475 |. cleartp STR:RC, TMP2
4477 |7: // Possible table write barrier for the value. Skip valiswhite check.
4478 | barrierback TAB:RB, TMP3, TMP0, <2
4481 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4482 | decode_RB8a RB, INS
4484 | daddu CARG2, BASE, RB
4485 | decode_RC8a RC, INS
4486 | ld TAB:RB, 0(CARG2)
4488 | dsubu CARG3, KBASE, RC
4489 | ld RC, -8(CARG3) // KBASE-8-str_const*8
4490 | daddu RA, BASE, RA
4492 | checktab TAB:RB, ->vmeta_tsets1
4494 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = BASE+src*8
4495 | lw TMP0, TAB:RB->hmask
4496 | lw TMP1, STR:RC->sid
4497 | ld NODE:TMP2, TAB:RB->node
4498 | sb r0, TAB:RB->nomm // Clear metamethod cache.
4499 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4502 | subu TMP1, TMP0, TMP1
4504 | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4505 | settp STR:RC, TMP3 // Tagged key to look for.
4512 | ld TMP0, NODE:TMP2->key
4513 | ld CARG2, NODE:TMP2->val
4514 | ld NODE:TMP1, NODE:TMP2->next
4516 |. lbu TMP3, TAB:RB->marked
4517 | beq CARG2, TISNIL, >4 // Key found, but nil value?
4518 |. ld TAB:TMP0, TAB:RB->metatable
4520 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4523 |. sdc1 FTMP0, NODE:TMP2->val
4525 |. sd CRET1, NODE:TMP2->val
4530 |4: // Check for __newindex if previous value is nil.
4531 | beqz TAB:TMP0, <2 // No metatable: done.
4533 | lbu TMP0, TAB:TMP0->nomm
4534 | andi TMP0, TMP0, 1<<MM_newindex
4535 | bnez TMP0, <2 // 'no __newindex' flag set: done.
4540 |5: // Follow hash chain.
4541 | bnez NODE:TMP1, <1
4542 |. move NODE:TMP2, NODE:TMP1
4543 | // End of hash chain: key not found, add a new one
4545 | // But check for __newindex first.
4546 | ld TAB:TMP2, TAB:RB->metatable
4547 | beqz TAB:TMP2, >6 // No metatable: continue.
4548 |. daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
4549 | lbu TMP0, TAB:TMP2->nomm
4550 | andi TMP0, TMP0, 1<<MM_newindex
4551 | beqz TMP0, ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4553 | load_got lj_tab_newkey
4556 | move CARG2, TAB:RB
4558 | call_intern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k
4560 | // Returns TValue *.
4563 | b <3 // No 2nd write barrier needed.
4564 |. sdc1 FTMP0, 0(CRET1)
4567 | b <3 // No 2nd write barrier needed.
4568 |. sd CARG1, 0(CRET1)
4571 |7: // Possible table write barrier for the value. Skip valiswhite check.
4572 | barrierback TAB:RB, TMP3, TMP0, <3
4575 | // RA = src*8, RB = table*8, RC = index*8
4576 | decode_RB8a RB, INS
4578 | daddu CARG2, BASE, RB
4579 | decode_RDtoRC8 RC, RD
4580 | ld TAB:RB, 0(CARG2)
4581 | daddu RA, BASE, RA
4583 | checktab RB, ->vmeta_tsetb
4584 | lw TMP1, TAB:RB->asize
4585 | ld TMP2, TAB:RB->array
4586 | sltu AT, TMP0, TMP1
4587 | beqz AT, ->vmeta_tsetb
4588 |. daddu RC, TMP2, RC
4590 | lbu TMP3, TAB:RB->marked
4591 | beq TMP1, TISNIL, >5
4594 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4600 |5: // Check for __newindex if previous value is nil.
4601 | ld TAB:TMP2, TAB:RB->metatable
4602 | beqz TAB:TMP2, <1 // No metatable: done.
4604 | lbu TMP1, TAB:TMP2->nomm
4605 | andi TMP1, TMP1, 1<<MM_newindex
4606 | bnez TMP1, <1 // 'no __newindex' flag set: done.
4608 | b ->vmeta_tsetb // Caveat: preserve TMP0 and CARG2!
4611 |7: // Possible table write barrier for the value. Skip valiswhite check.
4612 | barrierback TAB:RB, TMP3, TMP0, <2
4615 | // RA = dst*8, RB = table*8, RC = key*8
4616 | decode_RB8a RB, INS
4618 | decode_RDtoRC8 RC, RD
4619 | daddu CARG1, BASE, RB
4620 | daddu CARG3, BASE, RC
4621 | ld TAB:CARG2, 0(CARG1)
4622 | lw CARG3, LO(CARG3)
4624 | lbu TMP3, TAB:CARG2->marked
4625 | lw TMP0, TAB:CARG2->asize
4626 | ld TMP1, TAB:CARG2->array
4627 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4629 |. daddu RA, BASE, RA
4631 | sltu AT, CARG3, TMP0
4632 | sll TMP2, CARG3, 3
4633 | beqz AT, ->vmeta_tsetr // In array part?
4634 |. daddu CRET1, TMP1, TMP2
4638 | sd CARG1, 0(CRET1)
4641 |7: // Possible table write barrier for the value. Skip valiswhite check.
4642 | barrierback TAB:CARG2, TMP3, CRET1, <2
4646 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4647 | daddu RA, BASE, RA
4649 | daddu TMP3, KBASE, RD
4650 | ld TAB:CARG2, -8(RA) // Guaranteed to be a table.
4651 | addiu TMP0, MULTRES, -8
4652 | lw TMP3, LO(TMP3) // Integer constant is in lo-word.
4653 | beqz TMP0, >4 // Nothing to copy?
4654 |. srl CARG3, TMP0, 3
4656 | addu CARG3, CARG3, TMP3
4657 | lw TMP2, TAB:CARG2->asize
4659 | lbu TMP3, TAB:CARG2->marked
4660 | ld CARG1, TAB:CARG2->array
4661 | sltu AT, TMP2, CARG3
4663 |. daddu TMP2, RA, TMP0
4664 | daddu TMP1, TMP1, CARG1
4665 | andi TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4666 |3: // Copy result slots to table.
4672 |. daddiu TMP1, TMP1, 8
4678 |5: // Need to resize array part.
4679 | load_got lj_tab_reasize
4683 | call_intern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4685 | // Must not reallocate the stack.
4688 |. ld BASE, L->base // Reload BASE for lack of a saved register.
4690 |7: // Possible table write barrier for any value. Skip valiswhite check.
4691 | barrierback TAB:CARG2, TMP3, TMP0, <4
4694 /* -- Calls and vararg handling ----------------------------------------- */
4697 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4698 | decode_RDtoRC8 NARGS8:RC, RD
4700 |. addu NARGS8:RC, NARGS8:RC, MULTRES
4703 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4704 | decode_RDtoRC8 NARGS8:RC, RD
4707 | daddu BASE, BASE, RA
4708 | ld LFUNC:RB, 0(BASE)
4709 | daddiu BASE, BASE, 16
4710 | addiu NARGS8:RC, NARGS8:RC, -8
4711 | checkfunc RB, ->vmeta_call
4716 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4717 | addu NARGS8:RD, NARGS8:RD, MULTRES // BC_CALLT gets RC from RD.
4718 | // Fall through. Assumes BC_CALLT follows.
4721 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4722 | daddu RA, BASE, RA
4724 | move NARGS8:RC, RD
4725 | ld TMP1, FRAME_PC(BASE)
4727 | addiu NARGS8:RC, NARGS8:RC, -8
4728 | checktp CARG3, RB, -LJ_TFUNC, ->vmeta_callt
4730 | andi TMP0, TMP1, FRAME_TYPE // Caveat: preserve TMP0 until the 'or'.
4731 | lbu TMP3, LFUNC:CARG3->ffid
4733 |. xori TMP2, TMP1, FRAME_VARG
4735 | sd RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4736 | sltiu AT, TMP3, 2 // (> FF_C) Calling a fast function?
4739 | beqz NARGS8:RC, >3
4740 |. move TMP3, NARGS8:RC
4744 | addiu TMP3, TMP3, -8
4747 |. daddiu TMP2, TMP2, 8
4755 |5: // Tailcall to a fast function with a Lua frame below.
4757 | decode_RA8a RA, INS
4759 | dsubu TMP1, BASE, RA
4760 | ld TMP1, -32(TMP1)
4761 | cleartp LFUNC:TMP1
4762 | ld TMP1, LFUNC:TMP1->pc
4764 |. ld KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4766 |7: // Tailcall from a vararg function.
4767 | andi AT, TMP2, FRAME_TYPEP
4768 | bnez AT, <1 // Vararg frame below?
4769 |. dsubu TMP2, BASE, TMP2 // Relocate BASE down.
4771 | ld TMP1, FRAME_PC(TMP2)
4773 |. andi TMP0, TMP1, FRAME_TYPE
4777 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4778 | move TMP2, BASE // Save old BASE fir vmeta_call.
4779 | daddu BASE, BASE, RA
4781 | ld CARG1, -16(BASE)
4782 | ld CARG2, -8(BASE)
4783 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4784 | sd RB, 0(BASE) // Copy callable.
4785 | sd CARG1, 16(BASE) // Copy state.
4786 | sd CARG2, 24(BASE) // Copy control var.
4787 | daddiu BASE, BASE, 16
4788 | checkfunc RB, ->vmeta_call
4793 |.if JIT and ENDIAN_LE
4797 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4798 | daddu RA, BASE, RA
4799 | ld TAB:RB, -16(RA)
4800 | lw RC, -8+LO(RA) // Get index from control var.
4803 | lw TMP0, TAB:RB->asize
4804 | ld TMP1, TAB:RB->array
4805 | dsll CARG3, TISNUM, 47
4806 |1: // Traverse array part.
4808 | beqz AT, >5 // Index points after array part?
4810 | daddu TMP3, TMP1, TMP3
4812 | lhu RD, -4+OFS_RD(PC)
4813 | or TMP2, RC, CARG3
4814 | beq CARG1, TISNIL, <1 // Skip holes in array part.
4818 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
4820 | daddu RD, RD, TMP3
4821 | sw RC, -8+LO(RA) // Update control var.
4826 |5: // Traverse hash part.
4827 | lw TMP1, TAB:RB->hmask
4829 | ld TMP2, TAB:RB->node
4831 | sltu AT, TMP1, RC // End of iteration? Branch to ITERL+1.
4835 | subu TMP3, TMP3, RB
4836 | daddu NODE:TMP3, TMP3, TMP2
4837 | ld CARG1, 0(NODE:TMP3)
4838 | lhu RD, -4+OFS_RD(PC)
4839 | beq CARG1, TISNIL, <6 // Skip holes in hash part.
4841 | ld CARG2, NODE:TMP3->key
4842 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
4850 |. sw RC, -8+LO(RA) // Update control var.
4854 | // RA = base*8, RD = target (points to ITERN)
4855 | daddu RA, BASE, RA
4857 | ld CFUNC:CARG1, -24(RA)
4858 | daddu TMP0, PC, TMP0
4861 | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535)
4862 | checkfunc CFUNC:CARG1, >5
4863 | gettp CARG2, CARG2
4864 | daddiu CARG2, CARG2, -LJ_TTAB
4865 | lbu TMP1, CFUNC:CARG1->ffid
4866 | daddiu CARG3, CARG3, -LJ_TNIL
4867 | or AT, CARG2, CARG3
4868 | daddiu TMP1, TMP1, -FF_next_N
4871 |. lui TMP1, (LJ_KEYINDEX >> 16)
4872 | daddu PC, TMP0, TMP2
4873 | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff)
4874 | dsll TMP1, TMP1, 32
4878 |5: // Despecialize bytecode if any of the checks fail.
4881 | sb TMP3, -4+OFS_OP(PC)
4882 | daddu PC, TMP0, TMP2
4884 | lb TMP0, OFS_OP(PC)
4887 |. lhu TMP2, OFS_RD(PC)
4890 |. sb TMP1, OFS_OP(PC)
4892 |6: // Unpatch JLOOP.
4893 | ld TMP0, DISPATCH_J(trace)(DISPATCH)
4895 | daddu TMP0, TMP0, TMP2
4896 | ld TRACE:TMP2, 0(TMP0)
4897 | lw TMP0, TRACE:TMP2->startins
4899 | and TMP0, TMP0, AT
4900 | or TMP0, TMP0, TMP1
4907 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4908 | ld TMP0, FRAME_PC(BASE)
4909 | decode_RDtoRC8 RC, RD
4910 | decode_RB8a RB, INS
4911 | daddu RC, BASE, RC
4913 | daddu RA, BASE, RA
4914 | daddiu RC, RC, FRAME_VARG
4915 | daddu TMP2, RA, RB
4916 | daddiu TMP3, BASE, -16 // TMP3 = vtop
4917 | dsubu RC, RC, TMP0 // RC = vbase
4918 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4919 | beqz RB, >5 // Copy all varargs?
4920 |. dsubu TMP1, TMP3, RC
4921 | daddiu TMP2, TMP2, -16
4922 |1: // Copy vararg slots to destination slots.
4927 | selnez CARG1, CARG1, AT
4928 | seleqz AT, TISNIL, AT
4929 | or CARG1, CARG1, AT
4931 | movz CARG1, TISNIL, AT
4940 |5: // Copy all varargs.
4941 | ld TMP0, L->maxstack
4942 | blez TMP1, <3 // No vararg slots?
4943 |. li MULTRES, 8 // MULTRES = (0+1)*8
4944 | daddu TMP2, RA, TMP1
4945 | sltu AT, TMP0, TMP2
4947 |. daddiu MULTRES, TMP1, 8
4953 | bnez AT, <6 // More vararg slots?
4958 |7: // Grow stack for varargs.
4959 | load_got lj_state_growstack
4961 | dsubu RA, RA, BASE
4963 | dsubu BASE, RC, BASE // Need delta, because BASE may change.
4965 | srl CARG2, TMP1, 3
4966 | call_intern lj_state_growstack // (lua_State *L, int n)
4970 | daddu RA, BASE, RA
4971 | daddu RC, BASE, RC
4973 |. daddiu TMP3, BASE, -16
4976 /* -- Returns ----------------------------------------------------------- */
4979 | // RA = results*8, RD = extra_nresults*8
4980 | addu RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4981 | // Fall through. Assumes BC_RET follows.
4985 | // RA = results*8, RD = (nresults+1)*8
4986 | ld PC, FRAME_PC(BASE)
4987 | daddu RA, BASE, RA
4990 | andi TMP0, PC, FRAME_TYPE
4991 | bnez TMP0, ->BC_RETV_Z
4992 |. xori TMP1, PC, FRAME_VARG
4995 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4997 | daddiu TMP2, BASE, -16
4999 | decode_RA8a TMP0, INS
5000 | decode_RB8a RB, INS
5003 | daddu TMP3, TMP2, RB
5005 |. dsubu BASE, TMP2, TMP0
5012 |. daddiu TMP2, TMP2, 8
5014 | daddiu TMP3, TMP3, -8
5016 | sltu AT, TMP2, TMP3
5018 |. ld LFUNC:TMP1, FRAME_FUNC(BASE)
5020 | cleartp LFUNC:TMP1
5021 | ld TMP1, LFUNC:TMP1->pc
5022 | ld KBASE, PC2PROTO(k)(TMP1)
5025 |6: // Fill up results with nil.
5026 | sd TISNIL, 0(TMP2)
5028 |. daddiu TMP2, TMP2, 8
5030 |->BC_RETV_Z: // Non-standard return case.
5031 | andi TMP2, TMP1, FRAME_TYPEP
5032 | bnez TMP2, ->vm_return
5034 | // Return from vararg function: relocate BASE down.
5035 | dsubu BASE, BASE, TMP1
5037 |. ld PC, FRAME_PC(BASE)
5040 case BC_RET0: case BC_RET1:
5041 | // RA = results*8, RD = (nresults+1)*8
5042 | ld PC, FRAME_PC(BASE)
5043 | daddu RA, BASE, RA
5045 | andi TMP0, PC, FRAME_TYPE
5046 | bnez TMP0, ->BC_RETV_Z
5047 |. xori TMP1, PC, FRAME_VARG
5049 | daddiu TMP2, BASE, -16
5050 if (op == BC_RET1) {
5053 | decode_RB8a RB, INS
5054 | decode_RA8a RA, INS
5057 | dsubu BASE, TMP2, RA
5058 if (op == BC_RET1) {
5064 |. ld TMP1, FRAME_FUNC(BASE)
5066 | cleartp LFUNC:TMP1
5067 | ld TMP1, LFUNC:TMP1->pc
5068 | ld KBASE, PC2PROTO(k)(TMP1)
5071 |6: // Fill up results with nil.
5072 | daddiu TMP2, TMP2, 8
5075 if (op == BC_RET1) {
5076 |. sd TISNIL, 0(TMP2)
5078 |. sd TISNIL, -8(TMP2)
5082 /* -- Loops and branches ------------------------------------------------ */
5088 | // Fall through. Assumes BC_IFORL follows.
5098 | // RA = base*8, RD = target (after end of loop or start of loop)
5099 vk = (op == BC_IFORL || op == BC_JFORL);
5100 | daddu RA, BASE, RA
5101 | ld CARG1, FORL_IDX*8(RA) // IDX CARG1 - CARG3 type
5102 | gettp CARG3, CARG1
5103 if (op != BC_JFORL) {
5105 | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535)
5106 | daddu TMP2, RD, TMP2
5109 | ld CARG2, FORL_STOP*8(RA) // STOP CARG2 - CARG4 type
5110 | ld CRET1, FORL_STEP*8(RA) // STEP CRET1 - CRET2 type
5111 | gettp CARG4, CARG2
5112 | bne CARG3, TISNUM, >5
5113 |. gettp CRET2, CRET1
5114 | bne CARG4, TISNUM, ->vmeta_for
5115 |. sextw CARG3, CARG1
5116 | bne CRET2, TISNUM, ->vmeta_for
5117 |. sextw CARG2, CARG2
5118 | dext AT, CRET1, 31, 0
5119 | slt CRET1, CARG2, CARG3
5120 | slt TMP1, CARG3, CARG2
5122 | selnez TMP1, TMP1, AT
5123 | seleqz CRET1, CRET1, AT
5124 | or CRET1, CRET1, TMP1
5126 | movn CRET1, TMP1, AT
5129 | bne CARG3, TISNUM, >5
5130 |. ld CARG2, FORL_STEP*8(RA) // STEP CARG2 - CARG4 type
5131 | ld CRET1, FORL_STOP*8(RA) // STOP CRET1 - CRET2 type
5133 | sextw CARG2, CARG2
5134 | sextw CRET1, CRET1
5135 | addu CARG1, TMP3, CARG2
5136 | xor TMP0, CARG1, TMP3
5137 | xor TMP1, CARG1, CARG2
5138 | and TMP0, TMP0, TMP1
5139 | slt TMP1, CARG1, CRET1
5140 | slt CRET1, CRET1, CARG1
5142 | slt TMP0, TMP0, r0 // ((y^a) & (y^b)) < 0: overflow.
5144 | selnez TMP1, TMP1, AT
5145 | seleqz CRET1, CRET1, AT
5146 | or CRET1, CRET1, TMP1
5148 | movn CRET1, TMP1, AT
5150 | or CRET1, CRET1, TMP0
5151 | zextw CARG1, CARG1
5152 | settp CARG1, TISNUM
5155 if (op == BC_FORI) {
5157 | selnez TMP2, TMP2, CRET1
5159 | movz TMP2, r0, CRET1
5161 | daddu PC, PC, TMP2
5162 } else if (op == BC_JFORI) {
5163 | daddu PC, PC, TMP2
5164 | lhu RD, -4+OFS_RD(PC)
5165 } else if (op == BC_IFORL) {
5167 | seleqz TMP2, TMP2, CRET1
5169 | movn TMP2, r0, CRET1
5171 | daddu PC, PC, TMP2
5174 | sd CARG1, FORL_IDX*8(RA)
5177 | sd CARG1, FORL_EXT*8(RA)
5179 if (op == BC_JFORI) {
5180 | beqz CRET1, =>BC_JLOOP
5182 } else if (op == BC_JFORL) {
5183 | beqz CRET1, =>BC_JLOOP
5190 | ldc1 f0, FORL_IDX*8(RA)
5191 | ldc1 f2, FORL_STOP*8(RA)
5192 | sltiu TMP0, CARG3, LJ_TISNUM
5193 | sltiu TMP1, CARG4, LJ_TISNUM
5194 | sltiu AT, CRET2, LJ_TISNUM
5195 | ld TMP3, FORL_STEP*8(RA)
5196 | and TMP0, TMP0, TMP1
5198 | beqz AT, ->vmeta_for
5199 |. slt TMP3, TMP3, r0
5202 | cmp.lt.d FTMP0, f0, f2
5203 | cmp.lt.d FTMP1, f2, f0
5204 | sel.d FTMP2, FTMP1, FTMP0
5206 |. dmfc1 CRET1, FTMP2
5214 |. movn CRET1, AT, TMP3
5217 | ldc1 f0, FORL_IDX*8(RA)
5218 | ldc1 f4, FORL_STEP*8(RA)
5219 | ldc1 f2, FORL_STOP*8(RA)
5220 | ld TMP3, FORL_STEP*8(RA)
5223 | slt TMP3, TMP3, r0
5225 | cmp.lt.d FTMP0, f0, f2
5226 | cmp.lt.d FTMP1, f2, f0
5227 | sel.d FTMP2, FTMP1, FTMP0
5228 | dmfc1 CRET1, FTMP2
5229 if (op == BC_IFORL) {
5230 | seleqz TMP2, TMP2, CRET1
5231 | daddu PC, PC, TMP2
5236 | slt TMP3, TMP3, r0
5241 | movn CRET1, AT, TMP3
5242 if (op == BC_IFORL) {
5243 | movn TMP2, r0, CRET1
5244 | daddu PC, PC, TMP2
5247 | sdc1 f0, FORL_IDX*8(RA)
5250 |. sdc1 f0, FORL_EXT*8(RA)
5254 | sltiu TMP0, CARG3, LJ_TISNUM
5255 | sltiu TMP1, CARG4, LJ_TISNUM
5256 | sltiu AT, CRET2, LJ_TISNUM
5257 | and TMP0, TMP0, TMP1
5259 | beqz AT, ->vmeta_for
5261 | bal ->vm_sfcmpolex
5262 |. lw TMP3, FORL_STEP*8+HI(RA)
5269 | ld CARG2, FORL_STOP*8(RA)
5271 if ( op == BC_JFORL ) {
5272 | lhu RD, -4+OFS_RD(PC)
5275 | bal ->vm_sfcmpolex
5276 |. lw TMP3, FORL_STEP*8+HI(RA)
5287 | // Fall through. Assumes BC_IITERL follows.
5295 | // RA = base*8, RD = target
5296 | daddu RA, BASE, RA
5298 | beq TMP1, TISNIL, >1 // Stop if iterator returned nil.
5300 if (op == BC_JITERL) {
5304 | branch_RD // Otherwise save control var + branch.
5312 | // RA = base*8, RD = target (loop extent)
5313 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5314 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5318 | // Fall through. Assumes BC_ILOOP follows.
5322 | // RA = base*8, RD = target (loop extent)
5328 | // RA = base*8 (ignored), RD = traceno*8
5329 | ld TMP1, DISPATCH_J(trace)(DISPATCH)
5331 | daddu TMP1, TMP1, RD
5332 | // Traces on MIPS don't store the trace number, so use 0.
5333 | sd AT, DISPATCH_GL(vmstate)(DISPATCH)
5334 | ld TRACE:TMP2, 0(TMP1)
5335 | sd BASE, DISPATCH_GL(jit_base)(DISPATCH)
5336 | ld TMP2, TRACE:TMP2->mcode
5337 | sd L, DISPATCH_GL(tmpbuf.L)(DISPATCH)
5339 |. daddiu JGL, DISPATCH, GG_DISP2G+32768
5344 | // RA = base*8 (only used by trace recorder), RD = target
5349 /* -- Function headers -------------------------------------------------- */
5355 case BC_FUNCV: /* NYI: compiled vararg functions. */
5356 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
5364 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5365 | ld TMP2, L->maxstack
5366 | lbu TMP1, -4+PC2PROTO(numparams)(PC)
5367 | ld KBASE, -4+PC2PROTO(k)(PC)
5369 | bnez AT, ->vm_growstack_l
5370 |. sll TMP1, TMP1, 3
5371 if (op != BC_JFUNCF) {
5375 | sltu AT, NARGS8:RC, TMP1 // Check for missing parameters.
5377 |. daddu AT, BASE, NARGS8:RC
5378 if (op == BC_JFUNCF) {
5379 | decode_RD8a RD, INS
5386 |3: // Clear missing parameters.
5389 |. addiu NARGS8:RC, NARGS8:RC, 8
5396 | NYI // NYI: compiled vararg functions
5397 break; /* NYI: compiled vararg functions. */
5400 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5402 | daddu TMP1, BASE, RC
5403 | ld TMP2, L->maxstack
5404 | settp LFUNC:RB, TMP0
5405 | daddu TMP0, RA, RC
5406 | sd LFUNC:RB, 0(TMP1) // Store (tagged) copy of LFUNC.
5407 | daddiu TMP2, TMP2, -8
5408 | daddiu TMP3, RC, 16+FRAME_VARG
5409 | sltu AT, TMP0, TMP2
5410 | ld KBASE, -4+PC2PROTO(k)(PC)
5411 | beqz AT, ->vm_growstack_l
5412 |. sd TMP3, 8(TMP1) // Store delta + FRAME_VARG.
5413 | lbu TMP2, -4+PC2PROTO(numparams)(PC)
5418 |. daddiu BASE, TMP1, 16
5421 | sltu AT, RA, RC // Less args than parameters?
5424 | selnez TMP0, TMP0, AT
5425 | seleqz TMP3, TISNIL, AT
5426 | or TMP0, TMP0, TMP3
5427 | seleqz TMP3, CARG1, AT
5428 | selnez CARG1, TISNIL, AT
5429 | or CARG1, CARG1, TMP3
5431 | movz TMP0, TISNIL, AT // Clear missing parameters.
5432 | movn CARG1, TISNIL, AT // Clear old fixarg slot (help the GC).
5434 | addiu TMP2, TMP2, -1
5436 | daddiu TMP1, TMP1, 8
5446 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
5447 if (op == BC_FUNCC) {
5448 | ld CFUNCADDR, CFUNC:RB->f
5450 | ld CFUNCADDR, DISPATCH_GL(wrapf)(DISPATCH)
5452 | daddu TMP1, RA, NARGS8:RC
5453 | ld TMP2, L->maxstack
5454 | daddu RC, BASE, NARGS8:RC
5456 | sltu AT, TMP2, TMP1
5459 if (op == BC_FUNCCW) {
5460 | ld CARG2, CFUNC:RB->f
5462 | bnez AT, ->vm_growstack_c // Need to grow stack.
5464 | jalr CFUNCADDR // (lua_State *L [, lua_CFunction f])
5466 | // Returns nresults.
5471 | ld PC, FRAME_PC(BASE) // Fetch PC of caller.
5472 | dsubu RA, TMP1, RD // RA = L->top - nresults*8
5473 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
5478 /* ---------------------------------------------------------------------- */
5481 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5487 static int build_backend(BuildCtx *ctx)
5491 dasm_growpc(Dst, BC__MAX);
5493 build_subroutines(ctx);
5496 for (op = 0; op < BC__MAX; op++)
5497 build_ins(ctx, (BCOp)op, op);
5502 /* Emit pseudo frame-info for all assembler functions. */
5503 static void emit_asm_debug(BuildCtx *ctx)
5505 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5507 switch (ctx->mode) {
5509 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5512 "\t.4byte .LECIE0-.LSCIE0\n"
5514 "\t.4byte 0xffffffff\n"
5520 "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n"
5525 "\t.4byte .LEFDE0-.LASFDE0\n"
5527 "\t.4byte .Lframe0\n"
5528 "\t.8byte .Lbegin\n"
5530 "\t.byte 0xe\n\t.uleb128 %d\n"
5531 "\t.byte 0x9f\n\t.sleb128 2*5\n"
5532 "\t.byte 0x9e\n\t.sleb128 2*6\n",
5533 fcofs, CFRAME_SIZE);
5534 for (i = 23; i >= 16; i--)
5535 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2*(30-i));
5537 for (i = 31; i >= 24; i--)
5538 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 2*(46-i));
5546 "\t.4byte .LEFDE1-.LASFDE1\n"
5548 "\t.4byte .Lframe0\n"
5549 "\t.4byte lj_vm_ffi_call\n"
5551 "\t.byte 0x9f\n\t.uleb128 2*1\n"
5552 "\t.byte 0x90\n\t.uleb128 2*2\n"
5553 "\t.byte 0xd\n\t.uleb128 0x10\n"
5555 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);