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 | daddiu NARGS8:RC, NARGS8:RC, -8
1419 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1420 | bltz NARGS8:RC, ->fff_fallback
1422 | daddiu BASE, BASE, 16
1423 | // Remember active hook before pcall.
1424 | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT
1425 | andi TMP3, TMP3, 1
1426 | daddiu PC, TMP3, 16+FRAME_PCALL
1427 | beqz NARGS8:RC, ->vm_call_dispatch
1429 |. daddu TMP0, BASE, NARGS8:RC
1431 | ld TMP1, -16(TMP0)
1433 | daddiu TMP0, TMP0, -8
1434 | bne TMP0, BASE, <2
1436 | b ->vm_call_dispatch
1440 | daddiu NARGS8:TMP0, NARGS8:RC, -16
1443 | bltz NARGS8:TMP0, ->fff_fallback
1444 |. lbu TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1446 | daddiu AT, AT, -LJ_TFUNC
1447 | bnez AT, ->fff_fallback // Traceback must be a function.
1449 | move NARGS8:RC, NARGS8:TMP0
1450 | daddiu BASE, BASE, 24
1451 | // Remember active hook before pcall.
1452 | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT
1453 | sd CARG2, 0(TMP2) // Swap function and traceback.
1454 | andi TMP3, TMP3, 1
1456 | beqz NARGS8:RC, ->vm_call_dispatch
1457 |. daddiu PC, TMP3, 24+FRAME_PCALL
1461 |//-- Coroutine library --------------------------------------------------
1463 |.macro coroutine_resume_wrap, resume
1465 |.ffunc_1 coroutine_resume
1466 | checktp CARG1, CARG1, -LJ_TTHREAD, ->fff_fallback
1468 |.ffunc coroutine_wrap_aux
1469 | ld L:CARG1, CFUNC:RB->upvalue[0].gcr
1472 | lbu TMP0, L:CARG1->status
1473 | ld TMP1, L:CARG1->cframe
1474 | ld CARG2, L:CARG1->top
1475 | ld TMP2, L:CARG1->base
1476 | addiu AT, TMP0, -LUA_YIELD
1477 | daddu CARG3, CARG2, TMP0
1478 | daddiu TMP3, CARG2, 8
1480 | seleqz CARG2, CARG2, AT
1481 | selnez TMP3, TMP3, AT
1482 | bgtz AT, ->fff_fallback // st > LUA_YIELD?
1483 |. or CARG2, TMP3, CARG2
1485 | bgtz AT, ->fff_fallback // st > LUA_YIELD?
1486 |. movn CARG2, TMP3, AT
1488 | xor TMP2, TMP2, CARG3
1489 | bnez TMP1, ->fff_fallback // cframe != 0?
1490 |. or AT, TMP2, TMP0
1491 | ld TMP0, L:CARG1->maxstack
1492 | beqz AT, ->fff_fallback // base == top && st == 0?
1493 |. ld PC, FRAME_PC(BASE)
1494 | daddu TMP2, CARG2, NARGS8:RC
1495 | sltu AT, TMP0, TMP2
1496 | bnez AT, ->fff_fallback // Stack overflow?
1501 | daddiu BASE, BASE, 8 // Keep resumed thread in stack for GC.
1502 | daddiu NARGS8:RC, NARGS8:RC, -8
1503 | daddiu TMP2, TMP2, -8
1505 | sd TMP2, L:CARG1->top
1506 | daddu TMP1, BASE, NARGS8:RC
1509 |2: // Move args to coroutine.
1511 | sltu AT, BASE, TMP1
1513 |. daddiu BASE, BASE, 8
1514 | sd CRET1, 0(CARG3)
1516 |. daddiu CARG3, CARG3, 8
1518 | bal ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1519 |. move L:RA, L:CARG1
1520 | // Returns thread status.
1522 | ld TMP2, L:RA->base
1523 | sltiu AT, CRET1, LUA_YIELD+1
1524 | ld TMP3, L:RA->top
1527 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
1530 |. dsubu RD, TMP3, TMP2
1531 | ld TMP0, L->maxstack
1532 | beqz RD, >6 // No results?
1533 |. daddu TMP1, BASE, RD
1534 | sltu AT, TMP0, TMP1
1535 | bnez AT, >9 // Need to grow stack?
1536 |. daddu TMP3, TMP2, RD
1537 | sd TMP2, L:RA->top // Clear coroutine stack.
1539 |5: // Move results from coroutine.
1541 | daddiu TMP2, TMP2, 8
1542 | sltu AT, TMP2, TMP3
1545 |. daddiu TMP1, TMP1, 8
1547 | andi TMP0, PC, FRAME_TYPE
1550 | daddiu RA, BASE, -8
1551 | sd TMP1, -8(BASE) // Prepend true to results.
1559 | beqz TMP0, ->BC_RET_Z
1564 |8: // Coroutine returned with error (at co->top-1).
1566 | daddiu TMP3, TMP3, -8
1569 | sd TMP3, L:RA->top // Remove error from coroutine stack.
1571 | sd TMP1, -8(BASE) // Prepend false to results.
1572 | daddiu RA, BASE, -8
1573 | sd CRET1, 0(BASE) // Copy error message.
1575 |. andi TMP0, PC, FRAME_TYPE
1577 | load_got lj_ffh_coroutine_wrap_err
1579 | call_intern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1583 |9: // Handle stack expansion on return from yield.
1584 | load_got lj_state_growstack
1586 | call_intern lj_state_growstack // (lua_State *L, int n)
1592 | coroutine_resume_wrap 1 // coroutine.resume
1593 | coroutine_resume_wrap 0 // coroutine.wrap
1595 |.ffunc coroutine_yield
1596 | ld TMP0, L->cframe
1597 | daddu TMP1, BASE, NARGS8:RC
1599 | andi TMP0, TMP0, CFRAME_RESUME
1601 | beqz TMP0, ->fff_fallback
1602 |. li CRET1, LUA_YIELD
1605 |. sb CRET1, L->status
1607 |//-- Math library -------------------------------------------------------
1610 | gettp CARG2, CARG1
1611 | daddiu AT, CARG2, -LJ_TISNUM
1613 |. sextw TMP1, CARG1
1614 | sra TMP0, TMP1, 31 // Extract sign.
1615 | xor TMP1, TMP1, TMP0
1616 | dsubu CARG1, TMP1, TMP0
1617 | dsll TMP3, CARG1, 32
1618 | bgez TMP3, ->fff_restv
1619 |. settp CARG1, TISNUM
1620 | li CARG1, 0x41e0 // 2^31 as a double.
1622 |. dsll CARG1, CARG1, 48
1624 | sltiu AT, CARG2, LJ_TISNUM
1625 | beqz AT, ->fff_fallback
1626 |. dextm CARG1, CARG1, 0, 30
1630 | // CARG1 = TValue result.
1631 | ld PC, FRAME_PC(BASE)
1632 | daddiu RA, BASE, -16
1633 | sd CARG1, -16(BASE)
1635 | // RA = results, PC = return.
1638 | // RA = results, RD = (nresults+1)*8, PC = return.
1639 | andi TMP0, PC, FRAME_TYPE
1640 | bnez TMP0, ->vm_return
1643 | decode_RB8a RB, INS
1647 | bnez AT, >6 // More results expected?
1648 |. decode_RA8a TMP0, INS
1651 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1652 | dsubu BASE, RA, TMP0
1655 |6: // Fill up results with nil.
1656 | daddu TMP1, RA, RD
1659 |. sd TISNIL, -8(TMP1)
1661 |.macro math_extern, func
1662 | .ffunc_n math_ .. func
1670 |.macro math_extern2, func
1671 | .ffunc_nn math_ .. func
1679 |// TODO: Return integer type if result is integer (own sf implementation).
1680 |.macro math_round, func
1681 |->ff_math_ .. func:
1683 | beqz NARGS8:RC, ->fff_fallback
1684 |. gettp TMP0, CARG1
1685 | beq TMP0, TISNUM, ->fff_restv
1686 |. sltu AT, TMP0, TISNUM
1687 | beqz AT, ->fff_fallback
1689 |. ldc1 FARG1, 0(BASE)
1706 | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument.
1707 |. ld CARG1, 0(BASE)
1708 | checknum CARG1, ->fff_fallback
1712 |. ldc1 FARG1, 0(BASE)
1732 | math_extern2 atan2
1737 |. sqrt.d FRET1, FARG1
1738 |// fallthrough to ->fff_resn
1744 | ld PC, FRAME_PC(BASE)
1745 | daddiu RA, BASE, -16
1748 |. sdc1 FRET1, 0(RA)
1754 |.ffunc_2 math_ldexp
1755 | checknum CARG1, ->fff_fallback
1756 | checkint CARG2, ->fff_fallback
1758 | .FPU ldc1 FARG1, 0(BASE)
1760 |. lw CARG2, 8+LO(BASE)
1764 |.ffunc_n math_frexp
1766 | ld PC, FRAME_PC(BASE)
1768 |. daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv)
1769 | lw TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1770 | daddiu RA, BASE, -16
1774 | cvt.d.w FARG2, FARG2
1779 | settp TMP1, TISNUM
1787 | ld PC, FRAME_PC(BASE)
1789 |. daddiu CARG2, BASE, -16
1790 | daddiu RA, BASE, -16
1792 | sdc1 FRET1, -8(BASE)
1794 | sd CRET1, -8(BASE)
1799 |.macro math_minmax, name, intins, intinsc, fpins
1801 | daddu TMP3, BASE, NARGS8:RC
1802 | checkint CARG1, >5
1803 |. daddiu TMP2, BASE, 8
1804 |1: // Handle integers.
1805 | beq TMP2, TMP3, ->fff_restv
1806 |. ld CARG2, 0(TMP2)
1807 | checkint CARG2, >3
1808 |. sextw CARG1, CARG1
1809 | lw CARG2, LO(TMP2)
1810 |. slt AT, CARG1, CARG2
1812 | intins TMP1, CARG2, AT
1813 | intinsc CARG1, CARG1, AT
1814 | or CARG1, CARG1, TMP1
1816 | intins CARG1, CARG2, AT
1818 | daddiu TMP2, TMP2, 8
1819 | zextw CARG1, CARG1
1821 |. settp CARG1, TISNUM
1823 |3: // Convert intermediate result to number and continue with number loop.
1824 | checknum CARG2, ->fff_fallback
1826 |. mtc1 CARG1, FRET1
1827 | cvt.d.w FRET1, FRET1
1829 |. ldc1 FARG1, 0(TMP2)
1839 | .FPU ldc1 FRET1, 0(BASE)
1840 | checknum CARG1, ->fff_fallback
1841 |6: // Handle numbers.
1842 |. ld CARG2, 0(TMP2)
1843 | beq TMP2, TMP3, ->fff_resn
1845 | ldc1 FARG1, 0(TMP2)
1849 | checknum CARG2, >8
1854 | fpins FRET1, FRET1, FARG1
1857 | c.olt.d FARG1, FRET1
1859 | c.olt.d FRET1, FARG1
1861 | movf.d FRET1, FARG1
1871 | seleqz AT, CARG2, CRET1
1872 | selnez CARG1, CARG1, CRET1
1873 | or CARG1, CARG1, AT
1875 | movz CARG1, CARG2, CRET1
1879 |. daddiu TMP2, TMP2, 8
1881 |8: // Convert integer to number and continue with number loop.
1882 | checkint CARG2, ->fff_fallback
1884 |. lwc1 FARG1, LO(TMP2)
1886 |. cvt.d.w FARG1, FARG1
1888 |. lw CARG2, LO(TMP2)
1898 | math_minmax math_min, seleqz, selnez, min.d
1899 | math_minmax math_max, selnez, seleqz, max.d
1901 | math_minmax math_min, movz, _, 0
1902 | math_minmax math_max, movn, _, 1
1905 |//-- String library -----------------------------------------------------
1907 |.ffunc string_byte // Only handle the 1-arg case here.
1910 | xori AT, NARGS8:RC, 8
1911 | daddiu TMP0, TMP0, -LJ_TSTR
1913 | bnez AT, ->fff_fallback // Need exactly 1 string argument.
1914 |. cleartp STR:CARG1
1915 | lw TMP0, STR:CARG1->len
1916 | daddiu RA, BASE, -16
1917 | ld PC, FRAME_PC(BASE)
1919 | lbu TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
1921 | sll RD, RD, 3 // RD = ((str->len != 0)+1)*8
1922 | settp TMP1, TISNUM
1926 |.ffunc string_char // Only handle the 1-arg case here.
1933 | xori AT, NARGS8:RC, 8 // Exactly 1 argument.
1934 | daddiu TMP0, TMP0, -LJ_TISNUM // Integer.
1936 | sextw CARG1, CARG1
1938 | sltu TMP1, TMP1, CARG1 // !(255 < n).
1940 | bnez AT, ->fff_fallback
1942 | daddiu CARG2, sp, TMPD_OFS
1945 | load_got lj_str_new
1948 | call_intern lj_str_new // (lua_State *L, char *str, size_t l)
1950 | // Returns GCstr *.
1956 |. move CARG1, CRET1
1963 | addiu AT, NARGS8:RC, -16
1965 | bltz AT, ->fff_fallback
1967 | cleartp STR:CARG1, TMP0
1971 | ld CARG3, 16(BASE)
1972 | checkint CARG3, ->fff_fallback
1973 |. sextw CARG4, CARG3
1975 | checkint CARG2, ->fff_fallback
1977 | bne TMP3, AT, ->fff_fallback
1978 |. sextw CARG3, CARG2
1979 | lw CARG2, STR:CARG1->len
1980 | // STR:CARG1 = str, CARG2 = str->len, CARG3 = start, CARG4 = end
1982 | addiu TMP0, CARG2, 1
1983 | addu TMP1, CARG4, TMP0
1984 | slt TMP3, CARG3, r0
1986 | seleqz CARG4, CARG4, AT
1987 | selnez TMP1, TMP1, AT
1988 | or CARG4, TMP1, CARG4 // if (end < 0) end += len+1
1990 | movn CARG4, TMP1, AT // if (end < 0) end += len+1
1992 | addu TMP1, CARG3, TMP0
1994 | selnez TMP1, TMP1, TMP3
1995 | seleqz CARG3, CARG3, TMP3
1996 | or CARG3, TMP1, CARG3 // if (start < 0) start += len+1
1999 | slt TMP3, r0, CARG3
2000 | seleqz CARG4, CARG4, AT // if (end < 0) end = 0
2001 | selnez CARG3, CARG3, TMP3
2002 | seleqz TMP2, TMP2, TMP3
2003 | or CARG3, TMP2, CARG3 // if (start < 1) start = 1
2004 | slt AT, CARG2, CARG4
2005 | seleqz CARG4, CARG4, AT
2006 | selnez CARG2, CARG2, AT
2007 | or CARG4, CARG2, CARG4 // if (end > len) end = len
2009 | movn CARG3, TMP1, TMP3 // if (start < 0) start += len+1
2012 | slt TMP3, r0, CARG3
2013 | movn CARG4, r0, AT // if (end < 0) end = 0
2014 | movz CARG3, TMP2, TMP3 // if (start < 1) start = 1
2015 | slt AT, CARG2, CARG4
2016 | movn CARG4, CARG2, AT // if (end > len) end = len
2018 | daddu CARG2, STR:CARG1, CARG3
2019 | subu CARG3, CARG4, CARG3 // len = end - start
2020 | daddiu CARG2, CARG2, sizeof(GCstr)-1
2021 | bgez CARG3, ->fff_newstr
2022 |. addiu CARG3, CARG3, 1 // len++
2023 |->fff_emptystr: // Return empty string.
2025 | daddiu STR:CARG1, DISPATCH, DISPATCH_GL(strempty)
2029 |.macro ffstring_op, name
2030 | .ffunc string_ .. name
2033 | beqz NARGS8:RC, ->fff_fallback
2034 |. ld CARG2, 0(BASE)
2035 | checkstr STR:CARG2, ->fff_fallback
2036 | daddiu SBUF:CARG1, DISPATCH, DISPATCH_GL(tmpbuf)
2037 | load_got lj_buf_putstr_ .. name
2038 | ld TMP0, SBUF:CARG1->b
2039 | sd L, SBUF:CARG1->L
2041 | sd TMP0, SBUF:CARG1->w
2042 | call_intern extern lj_buf_putstr_ .. name
2044 | load_got lj_buf_tostr
2045 | call_intern lj_buf_tostr
2046 |. move SBUF:CARG1, SBUF:CRET1
2051 |ffstring_op reverse
2055 |//-- Bit library --------------------------------------------------------
2058 | beqz TMP1, ->fff_fallback
2060 |. ldc1 FARG1, 0(BASE)
2061 | add.d FARG1, FARG1, TOBIT
2064 |. zextw CRET1, CRET1
2066 |// FP number to bit conversion for soft-float.
2068 | dsll TMP0, CARG1, 1
2071 | dsubu CARG3, CARG3, AT
2072 | sltiu AT, CARG3, 54
2074 |. dextm TMP0, TMP0, 0, 20
2075 | dinsu TMP0, AT, 21, 21
2077 | dsrlv CRET1, TMP0, CARG3
2078 | dsubu TMP0, r0, CRET1
2080 | selnez TMP0, TMP0, AT
2081 | seleqz CRET1, CRET1, AT
2082 | or CRET1, CRET1, TMP0
2084 | movn CRET1, TMP0, AT
2087 |. zextw CRET1, CRET1
2092 |// FP number to int conversion with a check for soft-float.
2093 |// Modifies CARG1, CRET1, CRET2, TMP0, AT.
2096 | dsll CRET2, CARG1, 1
2099 | dsrl AT, CRET2, 53
2100 | dsubu TMP0, TMP0, AT
2101 | sltiu AT, TMP0, 54
2103 |. dextm CRET2, CRET2, 0, 20
2104 | dinsu CRET2, AT, 21, 21
2106 | dsrlv CRET1, CRET2, TMP0
2107 | dsubu CARG1, r0, CRET1
2109 | seleqz CRET1, CRET1, AT
2110 | selnez CARG1, CARG1, AT
2111 | or CRET1, CRET1, CARG1
2113 | movn CRET1, CARG1, AT
2116 | subu TMP0, CARG1, TMP0
2117 | dsllv CRET2, CRET2, TMP0 // Integer check.
2119 | xor AT, CRET1, AT // Range check.
2121 | seleqz AT, AT, CRET2
2122 | selnez CRET2, CRET2, CRET2
2124 |. or CRET2, AT, CRET2
2127 |. movz CRET2, AT, CRET2
2138 |.macro .ffunc_bit, name
2139 | .ffunc_1 bit_..name
2141 | beq TMP0, TISNUM, >6
2142 |. zextw CRET1, CARG1
2144 |. sltiu TMP1, TMP0, LJ_TISNUM
2148 |.macro .ffunc_bit_op, name, bins
2150 | daddiu TMP2, BASE, 8
2151 | daddu TMP3, BASE, NARGS8:RC
2153 | beq TMP2, TMP3, ->fff_resi
2154 |. ld CARG1, 0(TMP2)
2157 | bne TMP0, TISNUM, >2
2158 |. daddiu TMP2, TMP2, 8
2159 | zextw CARG1, CARG1
2161 |. bins CRET1, CRET1, CARG1
2163 | ldc1 FARG1, -8(TMP2)
2164 | sltiu AT, TMP0, LJ_TISNUM
2165 | beqz AT, ->fff_fallback
2166 |. add.d FARG1, FARG1, TOBIT
2168 | zextw CARG1, CARG1
2170 |. bins CRET1, CRET1, CARG1
2172 | beq TMP0, TISNUM, >2
2173 |. move CRET2, CRET1
2175 |. sltiu TMP1, TMP0, LJ_TISNUM
2178 | zextw CARG1, CARG1
2179 | bins CRET1, CRET1, CARG1
2181 |. daddiu TMP2, TMP2, 8
2185 |.ffunc_bit_op band, and
2186 |.ffunc_bit_op bor, or
2187 |.ffunc_bit_op bxor, xor
2190 | dsrl TMP0, CRET1, 8
2191 | dsrl TMP1, CRET1, 24
2192 | andi TMP2, TMP0, 0xff00
2193 | dins TMP1, CRET1, 24, 31
2194 | dins TMP2, TMP0, 16, 23
2196 |. or CRET1, TMP1, TMP2
2201 |. zextw CRET1, CRET1
2203 |.macro .ffunc_bit_sh, name, shins, shmod
2204 | .ffunc_2 bit_..name
2206 | beq TMP0, TISNUM, >1
2209 |. sltiu TMP1, TMP0, LJ_TISNUM
2213 | bne TMP0, TISNUM, ->fff_fallback
2214 |. zextw CARG2, CARG2
2215 | sextw CARG1, CARG1
2219 | shins CRET1, CARG1, CARG2
2221 |. zextw CRET1, CRET1
2224 |.ffunc_bit_sh lshift, sllv, 0
2225 |.ffunc_bit_sh rshift, srlv, 0
2226 |.ffunc_bit_sh arshift, srav, 0
2227 |.ffunc_bit_sh rol, rotrv, 1
2228 |.ffunc_bit_sh ror, rotrv, 0
2232 | ld PC, FRAME_PC(BASE)
2233 | daddiu RA, BASE, -16
2234 | settp CRET1, TISNUM
2236 |. sd CRET1, -16(BASE)
2238 |//-----------------------------------------------------------------------
2239 |->fff_fallback: // Call fast function fallback handler.
2240 | // BASE = new base, RB = CFUNC, RC = nargs*8
2241 | ld TMP3, CFUNC:RB->f
2242 | daddu TMP1, BASE, NARGS8:RC
2243 | ld PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2244 | daddiu TMP0, TMP1, 8*LUA_MINSTACK
2245 | ld TMP2, L->maxstack
2246 | sd PC, SAVE_PC // Redundant (but a defined value).
2247 | sltu AT, TMP2, TMP0
2250 | bnez AT, >5 // Need to grow stack.
2251 |. move CFUNCADDR, TMP3
2252 | jalr TMP3 // (lua_State *L)
2254 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2257 | bgtz CRET1, ->fff_res // Returned nresults+1?
2258 |. daddiu RA, BASE, -16
2259 |1: // Returned 0 or -1: retry fast path.
2260 | ld LFUNC:RB, FRAME_FUNC(BASE)
2263 | bnez CRET1, ->vm_call_tail // Returned -1?
2264 |. dsubu NARGS8:RC, TMP0, BASE
2265 | ins_callt // Returned 0: retry fast path.
2267 |// Reconstruct previous base for vmeta_call during tailcall.
2269 | andi TMP0, PC, FRAME_TYPE
2273 | lbu TMP1, OFS_RA(PC)
2275 | addiu TMP1, TMP1, 16
2277 | b ->vm_call_dispatch // Resolve again for tailcall.
2278 |. dsubu TMP2, BASE, TMP1
2280 |5: // Grow stack for fallback handler.
2281 | load_got lj_state_growstack
2282 | li CARG2, LUA_MINSTACK
2283 | call_intern lj_state_growstack // (lua_State *L, int n)
2287 |. li CRET1, 0 // Force retry.
2289 |->fff_gcstep: // Call GC step function.
2290 | // BASE = new base, RC = nargs*8
2292 | load_got lj_gc_step
2294 | daddu TMP0, BASE, NARGS8:RC
2295 | sd PC, SAVE_PC // Redundant (but a defined value).
2297 | call_intern lj_gc_step // (lua_State *L)
2302 | ld CFUNC:RB, FRAME_FUNC(BASE)
2305 |. dsubu NARGS8:RC, TMP0, BASE
2307 |//-----------------------------------------------------------------------
2308 |//-- Special dispatch targets -------------------------------------------
2309 |//-----------------------------------------------------------------------
2311 |->vm_record: // Dispatch target for recording phase.
2313 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2314 | andi AT, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2316 | // Decrement the hookcount for consistency, but always do the call.
2317 |. lw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2318 | andi AT, TMP3, HOOK_ACTIVE
2320 |. addiu TMP2, TMP2, -1
2321 | andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2325 |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2328 |->vm_rethook: // Dispatch target for return hooks.
2329 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2330 | andi AT, TMP3, HOOK_ACTIVE // Hook already active?
2332 |5: // Re-dispatch to static ins.
2333 |. ld AT, GG_DISP2STATIC(TMP0) // Assumes TMP0 holds DISPATCH+OP*4.
2337 |->vm_inshook: // Dispatch target for instr/line hooks.
2338 | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2339 | lw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2340 | andi AT, TMP3, HOOK_ACTIVE // Hook already active?
2342 |. andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2344 |. addiu TMP2, TMP2, -1
2346 |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2347 | andi AT, TMP3, LUA_MASKLINE
2350 |. load_got lj_dispatch_ins
2351 | sw MULTRES, SAVE_MULTRES
2354 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2355 | call_intern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2359 |4: // Re-dispatch to static ins.
2361 | decode_OP8a TMP1, INS
2363 | daddu TMP0, DISPATCH, TMP1
2364 | decode_RD8a RD, INS
2365 | ld AT, GG_DISP2STATIC(TMP0)
2366 | decode_RA8a RA, INS
2371 |->cont_hook: // Continue from hook yield.
2374 |. lw MULTRES, -24+LO(RB) // Restore MULTRES for *M ins.
2376 |->vm_hotloop: // Hot loop counter underflow.
2378 | ld LFUNC:TMP1, FRAME_FUNC(BASE)
2379 | daddiu CARG1, DISPATCH, GG_DISP2J
2380 | cleartp LFUNC:TMP1
2382 | ld TMP1, LFUNC:TMP1->pc
2384 | sd L, DISPATCH_J(L)(DISPATCH)
2385 | lbu TMP1, PC2PROTO(framesize)(TMP1)
2386 | load_got lj_trace_hot
2388 | dsll TMP1, TMP1, 3
2389 | daddu TMP1, BASE, TMP1
2390 | call_intern lj_trace_hot // (jit_State *J, const BCIns *pc)
2397 |->vm_callhook: // Dispatch target for call hooks.
2403 |->vm_hotcall: // Hot call counter underflow.
2408 | load_got lj_dispatch_call
2409 | daddu TMP0, BASE, RC
2412 | dsubu RA, RA, BASE
2414 | call_intern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2416 | // Returns ASMFunction.
2419 | sd r0, SAVE_PC // Invalidate for subsequent line hook.
2420 | dsubu NARGS8:RC, TMP0, BASE
2421 | daddu RA, BASE, RA
2422 | ld LFUNC:RB, FRAME_FUNC(BASE)
2427 |->cont_stitch: // Trace stitching.
2429 | // RA = resultptr, RB = meta base
2431 | ld TRACE:TMP2, -40(RB) // Save previous trace.
2432 | decode_RA8a RC, INS
2433 | daddiu AT, MULTRES, -8
2434 | cleartp TRACE:TMP2
2437 |. daddu RC, BASE, RC // Call base.
2438 |1: // Move results down.
2446 | decode_RA8a RA, INS
2447 | decode_RB8a RB, INS
2451 | daddu RA, BASE, RA
2454 | bnez AT, >9 // More results wanted?
2457 | lhu TMP3, TRACE:TMP2->traceno
2458 | lhu RD, TRACE:TMP2->link
2459 | beq RD, TMP3, ->cont_nop // Blacklisted.
2460 |. load_got lj_dispatch_stitch
2461 | bnez RD, =>BC_JLOOP // Jump to stitched trace.
2464 | // Stitch a new trace to the previous trace.
2465 | sw TMP3, DISPATCH_J(exitno)(DISPATCH)
2466 | sd L, DISPATCH_J(L)(DISPATCH)
2468 | daddiu CARG1, DISPATCH, GG_DISP2J
2469 | call_intern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2480 |->vm_profhook: // Dispatch target for profiler hook.
2482 | load_got lj_dispatch_profile
2483 | sw MULTRES, SAVE_MULTRES
2486 | call_intern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2488 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2494 |//-----------------------------------------------------------------------
2495 |//-- Trace exit handler -------------------------------------------------
2496 |//-----------------------------------------------------------------------
2498 |.macro savex_, a, b
2500 | sdc1 f..a, a*8(sp)
2501 | sdc1 f..b, b*8(sp)
2502 | sd r..a, 32*8+a*8(sp)
2503 | sd r..b, 32*8+b*8(sp)
2513 | daddiu sp, sp, -(32*8+32*8)
2515 | daddiu sp, sp, -(32*8)
2533 | sdc1 f29, 29*8(sp)
2534 | sdc1 f31, 31*8(sp)
2535 | sd r0, 32*8+31*8(sp) // Clear RID_TMP.
2536 | daddiu TMP2, sp, 32*8+32*8 // Recompute original value of sp.
2537 | sd TMP2, 32*8+29*8(sp) // Store sp in RID_SP
2539 | sd r0, 31*8(sp) // Clear RID_TMP.
2540 | daddiu TMP2, sp, 32*8 // Recompute original value of sp.
2541 | sd TMP2, 29*8(sp) // Store sp in RID_SP
2544 | daddiu DISPATCH, JGL, -GG_DISP2G-32768
2545 | lw TMP1, 0(TMP2) // Load exit number.
2547 | ld L, DISPATCH_GL(cur_L)(DISPATCH)
2548 | ld BASE, DISPATCH_GL(jit_base)(DISPATCH)
2549 | load_got lj_trace_exit
2550 | sd L, DISPATCH_J(L)(DISPATCH)
2551 | sw ra, DISPATCH_J(parent)(DISPATCH) // Store trace number.
2553 | sw TMP1, DISPATCH_J(exitno)(DISPATCH) // Store exit number.
2554 | daddiu CARG1, DISPATCH, GG_DISP2J
2555 | sd r0, DISPATCH_GL(jit_base)(DISPATCH)
2556 | call_intern lj_trace_exit // (jit_State *J, ExitState *ex)
2558 | // Returns MULTRES (unscaled) or negated error code.
2559 | ld TMP1, L->cframe
2563 | ld PC, SAVE_PC // Get SAVE_PC.
2565 |. sd L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2569 | // CRET1 = MULTRES or negated error code, BASE, PC and JGL set.
2571 | daddiu DISPATCH, JGL, -GG_DISP2G-32768
2574 | sltiu TMP0, CRET1, -LUA_ERRERR // Check for error from exit.
2576 |. ld LFUNC:RB, FRAME_FUNC(BASE)
2577 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2578 | dsll MULTRES, CRET1, 3
2580 | sw MULTRES, SAVE_MULTRES
2581 | li TISNIL, LJ_TNIL
2582 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2583 | .FPU mtc1 TMP3, TOBIT
2584 | ld TMP1, LFUNC:RB->pc
2585 | sd r0, DISPATCH_GL(jit_base)(DISPATCH)
2586 | ld KBASE, PC2PROTO(k)(TMP1)
2587 | .FPU cvt.d.s TOBIT, TOBIT
2588 | // Modified copy of ins_next which handles function header dispatch, too.
2590 | addiu CRET1, CRET1, 17 // Static dispatch?
2591 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1
2592 | sw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2593 | decode_RD8a RD, INS
2596 | decode_OP8a TMP1, INS
2598 | daddu TMP0, DISPATCH, TMP1
2599 | sltiu TMP2, TMP1, BC_FUNCF*8
2601 | decode_RA8a RA, INS
2607 | sltiu TMP2, TMP1, (BC_FUNCC+2)*8 // Fast function?
2609 |. ld TMP1, FRAME_PC(BASE)
2610 | // Check frame below fast function.
2611 | andi TMP0, TMP1, FRAME_TYPE
2612 | bnez TMP0, >3 // Trace stitching continuation?
2614 | // Otherwise set KBASE for Lua function below fast function.
2616 | decode_RA8a TMP0, TMP2
2618 | dsubu TMP1, BASE, TMP0
2619 | ld LFUNC:TMP2, -32(TMP1)
2620 | cleartp LFUNC:TMP2
2621 | ld TMP1, LFUNC:TMP2->pc
2622 | ld KBASE, PC2PROTO(k)(TMP1)
2624 | daddiu RC, MULTRES, -8
2626 |. daddu RA, RA, BASE
2628 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2629 | ld TMP0, DISPATCH_J(trace)(DISPATCH)
2631 | daddu TMP0, TMP0, RD
2632 | ld TRACE:TMP2, 0(TMP0)
2633 | lw INS, TRACE:TMP2->startins
2634 | decode_OP8a TMP1, INS
2636 | daddu TMP0, DISPATCH, TMP1
2637 | decode_RD8a RD, INS
2638 | ld AT, GG_DISP2STATIC(TMP0)
2639 | decode_RA8a RA, INS
2644 |9: // Rethrow error from the right C frame.
2645 | load_got lj_err_trace
2646 | sub CARG2, r0, CRET1
2647 | call_intern lj_err_trace // (lua_State *L, int errcode)
2651 |//-----------------------------------------------------------------------
2652 |//-- Math helper functions ----------------------------------------------
2653 |//-----------------------------------------------------------------------
2655 |// Hard-float round to integer.
2656 |// Modifies AT, TMP0, FRET1, FRET2, f4. Keeps all others incl. FARG1.
2657 |// MIPSR6: Modifies FTMP1, too.
2658 |.macro vm_round_hf, func
2659 | lui TMP0, 0x4330 // Hiword of 2^52 (double).
2660 | dsll TMP0, TMP0, 32
2662 | abs.d FRET2, FARG1 // |x|
2665 | cmp.lt.d FTMP1, FRET2, f4
2666 | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52
2667 | bc1eqz FTMP1, >1 // Truncate only if |x| < 2^52.
2669 | c.olt.d 0, FRET2, f4
2670 | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52
2671 | bc1f 0, >1 // Truncate only if |x| < 2^52.
2673 |. sub.d FRET1, FRET1, f4
2675 |.if "func" == "ceil"
2676 | lui TMP0, 0xbff0 // Hiword of -1 (double). Preserves -0.
2678 | lui TMP0, 0x3ff0 // Hiword of +1 (double).
2680 |.if "func" == "trunc"
2681 | dsll TMP0, TMP0, 32
2684 | cmp.lt.d FTMP1, FRET2, FRET1 // |x| < result?
2685 | sub.d FRET2, FRET1, f4
2686 | sel.d FTMP1, FRET1, FRET2 // If yes, subtract +1.
2688 | neg.d FRET2, FTMP1
2690 |. sel.d FRET1, FTMP1, FRET2 // Merge sign bit back in.
2692 | c.olt.d 0, FRET2, FRET1 // |x| < result?
2693 | sub.d FRET2, FRET1, f4
2694 | movt.d FRET1, FRET2, 0 // If yes, subtract +1.
2695 | neg.d FRET2, FRET1
2697 |. movn.d FRET1, FRET2, AT // Merge sign bit back in.
2700 | neg.d FRET2, FRET1
2701 | dsll TMP0, TMP0, 32
2705 | sel.d FTMP1, FRET1, FRET2
2706 |.if "func" == "ceil"
2707 | cmp.lt.d FRET1, FTMP1, FARG1 // x > result?
2709 | cmp.lt.d FRET1, FARG1, FTMP1 // x < result?
2711 | sub.d FRET2, FTMP1, f4 // If yes, subtract +-1.
2713 |. sel.d FRET1, FTMP1, FRET2
2715 | movn.d FRET1, FRET2, AT // Merge sign bit back in.
2716 |.if "func" == "ceil"
2717 | c.olt.d 0, FRET1, FARG1 // x > result?
2719 | c.olt.d 0, FARG1, FRET1 // x < result?
2721 | sub.d FRET2, FRET1, f4 // If yes, subtract +-1.
2723 |. movt.d FRET1, FRET2, 0
2728 |. mov.d FRET1, FARG1
2731 |.macro vm_round, func
2746 |// Soft-float integer to number conversion.
2749 | beqz ARG, >9 // Handle zero first.
2750 |. sra TMP0, ARG, 31
2751 | xor TMP1, ARG, TMP0
2752 | dsubu TMP1, TMP1, TMP0 // Absolute value in TMP1.
2754 | addiu ARG, ARG, -11
2755 | li AT, 0x3ff+63-11-1
2756 | dsllv TMP1, TMP1, ARG // Align mantissa left with leading 1.
2757 | subu ARG, AT, ARG // Exponent - 1.
2758 | ins ARG, TMP0, 11, 11 // Sign | Exponent.
2759 | dsll ARG, ARG, 52 // Align left.
2761 |. daddu ARG, ARG, TMP1 // Add mantissa, increment exponent.
2768 |// Input CARG1. Output: CARG1. Temporaries: AT, TMP0, TMP1.
2772 |// Input CARG2. Output: CARG2. Temporaries: AT, TMP0, TMP1.
2776 |// Soft-float comparison. Equivalent to c.eq.d.
2777 |// Input: CARG*. Output: CRET1. Temporaries: AT, TMP0, TMP1.
2781 | dsll TMP0, CARG2, 1
2783 | beqz TMP1, >8 // Both args +-0: return 1.
2785 | dsll TMP1, TMP1, 32
2787 | sltu TMP0, TMP1, TMP0
2789 | bnez TMP1, >9 // Either arg is NaN: return 0;
2790 |. xor AT, CARG1, CARG2
2792 |. sltiu CRET1, AT, 1 // Same values: return 1.
2801 |// Soft-float comparison. Equivalent to c.ult.d and c.olt.d.
2802 |// Input: CARG1, CARG2. Output: CRET1. Temporaries: AT, TMP0, TMP1, CRET2.
2814 | dsll TMP0, CARG2, 1
2816 | beqz TMP1, >8 // Both args +-0: return 0.
2818 | dsll TMP1, TMP1, 32
2820 | sltu TMP0, TMP1, TMP0
2822 | bnez TMP1, >9 // Either arg is NaN: return 0 or 1;
2823 |. and AT, CARG1, CARG2
2824 | bltz AT, >5 // Both args negative?
2827 |. slt CRET1, CARG1, CARG2
2828 |5: // Swap conditions if both operands are negative.
2830 |. slt CRET1, CARG2, CARG1
2836 |. move CRET1, CRET2
2842 | dsll TMP0, CARG1, 1
2844 | beqz TMP1, >8 // Both args +-0: return 0.
2846 | dsll TMP1, TMP1, 32
2848 | sltu TMP0, TMP1, TMP0
2850 | bnez TMP1, >9 // Either arg is NaN: return 0 or 1;
2851 |. and AT, CARG2, CARG1
2852 | bltz AT, >5 // Both args negative?
2855 |. slt CRET1, CARG2, CARG1
2856 |5: // Swap conditions if both operands are negative.
2858 |. slt CRET1, CARG1, CARG2
2867 |// Soft-float comparison. Equivalent to c.ole.d a, b or c.ole.d b, a.
2868 |// Input: CARG1, CARG2, TMP3. Output: CRET1. Temporaries: AT, TMP0, TMP1.
2872 | dsll TMP0, CARG2, 1
2874 | beqz TMP1, >8 // Both args +-0: return 1.
2876 | dsll TMP1, TMP1, 32
2878 | sltu TMP0, TMP1, TMP0
2880 | bnez TMP1, >9 // Either arg is NaN: return 0;
2881 |. and AT, CARG1, CARG2
2883 | bltz AT, >5 // Both args negative?
2886 |. slt CRET1, CARG2, CARG1
2887 |5: // Swap conditions if both operands are negative.
2889 |. slt CRET1, CARG1, CARG2
2898 |.macro sfmin_max, name, fpcall
2900 |.if JIT and not FPU
2908 | selnez CRET1, CRET1, TMP0
2909 | seleqz TMP0, CARG2, TMP0
2911 |. or CRET1, CRET1, TMP0
2914 |. movz CRET1, CARG2, TMP0
2919 | sfmin_max min, vm_sfcmpolt
2920 | sfmin_max max, vm_sfcmpogt
2922 |//-----------------------------------------------------------------------
2923 |//-- Miscellaneous functions --------------------------------------------
2924 |//-----------------------------------------------------------------------
2926 |.define NEXT_TAB, TAB:CARG1
2927 |.define NEXT_IDX, CARG2
2928 |.define NEXT_ASIZE, CARG3
2929 |.define NEXT_NIL, CARG4
2930 |.define NEXT_TMP0, r12
2931 |.define NEXT_TMP1, r13
2932 |.define NEXT_TMP2, r14
2933 |.define NEXT_RES_VK, CRET1
2934 |.define NEXT_RES_IDX, CRET2
2935 |.define NEXT_RES_PTR, sp
2936 |.define NEXT_RES_VAL, 0(sp)
2937 |.define NEXT_RES_KEY, 8(sp)
2939 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2940 |// Next idx returned in CRET2.
2942 |.if JIT and ENDIAN_LE
2943 | lw NEXT_ASIZE, NEXT_TAB->asize
2944 | ld NEXT_TMP0, NEXT_TAB->array
2945 | li NEXT_NIL, LJ_TNIL
2946 |1: // Traverse array part.
2947 | sltu AT, NEXT_IDX, NEXT_ASIZE
2948 | sll NEXT_TMP1, NEXT_IDX, 3
2950 |. daddu NEXT_TMP1, NEXT_TMP0, NEXT_TMP1
2952 | ld NEXT_TMP2, 0(NEXT_TMP1)
2954 | or NEXT_TMP1, NEXT_IDX, AT
2955 | beq NEXT_TMP2, NEXT_NIL, <1
2956 |. addiu NEXT_IDX, NEXT_IDX, 1
2957 | sd NEXT_TMP2, NEXT_RES_VAL
2958 | sd NEXT_TMP1, NEXT_RES_KEY
2959 | move NEXT_RES_VK, NEXT_RES_PTR
2961 |. move NEXT_RES_IDX, NEXT_IDX
2963 |5: // Traverse hash part.
2964 | subu NEXT_RES_IDX, NEXT_IDX, NEXT_ASIZE
2965 | ld NODE:NEXT_RES_VK, NEXT_TAB->node
2966 | sll NEXT_TMP2, NEXT_RES_IDX, 5
2967 | lw NEXT_TMP0, NEXT_TAB->hmask
2968 | sll AT, NEXT_RES_IDX, 3
2969 | subu AT, NEXT_TMP2, AT
2970 | daddu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, AT
2972 | sltu AT, NEXT_TMP0, NEXT_RES_IDX
2975 | ld NEXT_TMP2, NODE:NEXT_RES_VK->val
2976 | bne NEXT_TMP2, NEXT_NIL, >9
2977 |. addiu NEXT_RES_IDX, NEXT_RES_IDX, 1
2978 | // Skip holes in hash part.
2980 |. daddiu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, sizeof(Node)
2982 |8: // End of iteration. Set the key to nil (not the value).
2983 | sd NEXT_NIL, NEXT_RES_KEY
2984 | move NEXT_RES_VK, NEXT_RES_PTR
2987 |. addu NEXT_RES_IDX, NEXT_RES_IDX, NEXT_ASIZE
2990 |//-----------------------------------------------------------------------
2991 |//-- FFI helper functions -----------------------------------------------
2992 |//-----------------------------------------------------------------------
2994 |// Handler for callback functions. Callback slot number in r1, g in r2.
2997 |.type CTSTATE, CTState, PC
2999 | ld CTSTATE, GL:r2->ctype_state
3000 | daddiu DISPATCH, r2, GG_G2DISP
3001 | load_got lj_ccallback_enter
3002 | sw r1, CTSTATE->cb.slot
3003 | sd CARG1, CTSTATE->cb.gpr[0]
3004 | .FPU sdc1 FARG1, CTSTATE->cb.fpr[0]
3005 | sd CARG2, CTSTATE->cb.gpr[1]
3006 | .FPU sdc1 FARG2, CTSTATE->cb.fpr[1]
3007 | sd CARG3, CTSTATE->cb.gpr[2]
3008 | .FPU sdc1 FARG3, CTSTATE->cb.fpr[2]
3009 | sd CARG4, CTSTATE->cb.gpr[3]
3010 | .FPU sdc1 FARG4, CTSTATE->cb.fpr[3]
3011 | sd CARG5, CTSTATE->cb.gpr[4]
3012 | .FPU sdc1 FARG5, CTSTATE->cb.fpr[4]
3013 | sd CARG6, CTSTATE->cb.gpr[5]
3014 | .FPU sdc1 FARG6, CTSTATE->cb.fpr[5]
3015 | sd CARG7, CTSTATE->cb.gpr[6]
3016 | .FPU sdc1 FARG7, CTSTATE->cb.fpr[6]
3017 | sd CARG8, CTSTATE->cb.gpr[7]
3018 | .FPU sdc1 FARG8, CTSTATE->cb.fpr[7]
3019 | daddiu TMP0, sp, CFRAME_SPACE
3020 | sd TMP0, CTSTATE->cb.stack
3021 | sd r0, SAVE_PC // Any value outside of bytecode is ok.
3023 | call_intern lj_ccallback_enter // (CTState *cts, void *cf)
3024 |. move CARG1, CTSTATE
3025 | // Returns lua_State *.
3026 | ld BASE, L:CRET1->base
3027 | ld RC, L:CRET1->top
3029 | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
3030 | ld LFUNC:RB, FRAME_FUNC(BASE)
3031 | .FPU mtc1 TMP3, TOBIT
3032 | li TISNIL, LJ_TNIL
3033 | li TISNUM, LJ_TISNUM
3038 | .FPU cvt.d.s TOBIT, TOBIT
3042 |->cont_ffi_callback: // Return from FFI callback.
3044 | load_got lj_ccallback_leave
3045 | ld CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
3050 | call_intern lj_ccallback_leave // (CTState *cts, TValue *o)
3051 |. move CARG1, CTSTATE
3052 | .FPU ldc1 FRET1, CTSTATE->cb.fpr[0]
3053 | ld CRET1, CTSTATE->cb.gpr[0]
3054 | .FPU ldc1 FRET2, CTSTATE->cb.fpr[1]
3056 |. ld CRET2, CTSTATE->cb.gpr[1]
3059 |->vm_ffi_call: // Call C function via FFI.
3060 | // Caveat: needs special frame unwinding, see below.
3062 | .type CCSTATE, CCallState, CARG1
3063 | lw TMP1, CCSTATE->spadj
3064 | lbu CARG2, CCSTATE->nsp
3066 | dsubu sp, sp, TMP1
3069 | sd CCSTATE, -24(TMP2)
3071 | daddiu TMP1, CCSTATE, offsetof(CCallState, stack)
3074 |. daddu TMP3, TMP1, CARG2
3077 | daddiu TMP1, TMP1, 8
3078 | sltu AT, TMP1, TMP3
3081 |. daddiu TMP2, TMP2, 8
3083 | ld CFUNCADDR, CCSTATE->func
3084 | .FPU ldc1 FARG1, CCSTATE->gpr[0]
3085 | ld CARG2, CCSTATE->gpr[1]
3086 | .FPU ldc1 FARG2, CCSTATE->gpr[1]
3087 | ld CARG3, CCSTATE->gpr[2]
3088 | .FPU ldc1 FARG3, CCSTATE->gpr[2]
3089 | ld CARG4, CCSTATE->gpr[3]
3090 | .FPU ldc1 FARG4, CCSTATE->gpr[3]
3091 | ld CARG5, CCSTATE->gpr[4]
3092 | .FPU ldc1 FARG5, CCSTATE->gpr[4]
3093 | ld CARG6, CCSTATE->gpr[5]
3094 | .FPU ldc1 FARG6, CCSTATE->gpr[5]
3095 | ld CARG7, CCSTATE->gpr[6]
3096 | .FPU ldc1 FARG7, CCSTATE->gpr[6]
3097 | ld CARG8, CCSTATE->gpr[7]
3098 | .FPU ldc1 FARG8, CCSTATE->gpr[7]
3100 |. ld CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
3101 | ld CCSTATE:TMP1, -24(r16)
3104 | sd CRET1, CCSTATE:TMP1->gpr[0]
3105 | sd CRET2, CCSTATE:TMP1->gpr[1]
3107 | sdc1 FRET1, CCSTATE:TMP1->fpr[0]
3108 | sdc1 FRET2, CCSTATE:TMP1->fpr[1]
3110 | sd CARG1, CCSTATE:TMP1->gpr[2] // 2nd FP struct field for soft-float.
3116 |// Note: vm_ffi_call must be the last function in this object file!
3118 |//-----------------------------------------------------------------------
3121 /* Generate the code for a single instruction. */
3122 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3129 /* -- Comparison ops ---------------------------------------------------- */
3131 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3133 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3134 | // RA = src1*8, RD = src2*8, JMP with RD = target
3135 |.macro bc_comp, FRA, FRD, ARGRA, ARGRD, movop, fmovop, fcomp, sfcomp
3136 | daddu RA, BASE, RA
3137 | daddu RD, BASE, RD
3140 | lhu TMP2, OFS_RD(PC)
3141 | gettp CARG3, ARGRA
3142 | gettp CARG4, ARGRD
3143 | bne CARG3, TISNUM, >2
3145 | bne CARG4, TISNUM, >5
3147 | sextw ARGRA, ARGRA
3148 | sextw ARGRD, ARGRD
3149 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3150 | slt AT, CARG1, CARG2
3151 | addu TMP2, TMP2, TMP3
3153 | movop TMP2, TMP2, AT
3155 | movop TMP2, r0, AT
3158 | daddu PC, PC, TMP2
3161 |2: // RA is not an integer.
3162 | sltiu AT, CARG3, LJ_TISNUM
3163 | beqz AT, ->vmeta_comp
3164 |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3165 | sltiu AT, CARG4, LJ_TISNUM
3172 |3: // RA and RD are both numbers.
3175 | fcomp FTMP0, FTMP0, FTMP2
3176 | addu TMP2, TMP2, TMP3
3179 |. fmovop TMP2, TMP2, TMP3
3181 | fcomp FTMP0, FTMP2
3182 | addu TMP2, TMP2, TMP3
3188 |. addu TMP2, TMP2, TMP3
3191 |. movop TMP2, TMP2, CRET1
3193 |. movop TMP2, r0, CRET1
3197 |4: // RA is a number, RD is not a number.
3198 | bne CARG4, TISNUM, ->vmeta_comp
3199 | // RA is a number, RD is an integer. Convert RD to a number.
3206 |.if "ARGRD" == "CARG1"
3207 |. sextw CARG1, CARG1
3211 |. sextw CARG2, CARG2
3219 |5: // RA is an integer, RD is not an integer
3220 | sltiu AT, CARG4, LJ_TISNUM
3221 | beqz AT, ->vmeta_comp
3222 |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3223 | // RA is an integer, RD is a number. Convert RA to a number.
3230 |.if "ARGRA" == "CARG1"
3232 |. sextw CARG1, CARG1
3235 |. sextw CARG2, CARG2
3243 if (op == BC_ISLT) {
3244 | bc_comp FTMP0, FTMP2, CARG1, CARG2, selnez, selnez, cmp.lt.d, ->vm_sfcmpolt
3245 } else if (op == BC_ISGE) {
3246 | bc_comp FTMP0, FTMP2, CARG1, CARG2, seleqz, seleqz, cmp.lt.d, ->vm_sfcmpolt
3247 } else if (op == BC_ISLE) {
3248 | bc_comp FTMP2, FTMP0, CARG2, CARG1, seleqz, seleqz, cmp.ult.d, ->vm_sfcmpult
3250 | bc_comp FTMP2, FTMP0, CARG2, CARG1, selnez, selnez, cmp.ult.d, ->vm_sfcmpult
3253 if (op == BC_ISLT) {
3254 | bc_comp FTMP0, FTMP2, CARG1, CARG2, movz, movf, c.olt.d, ->vm_sfcmpolt
3255 } else if (op == BC_ISGE) {
3256 | bc_comp FTMP0, FTMP2, CARG1, CARG2, movn, movt, c.olt.d, ->vm_sfcmpolt
3257 } else if (op == BC_ISLE) {
3258 | bc_comp FTMP2, FTMP0, CARG2, CARG1, movn, movt, c.ult.d, ->vm_sfcmpult
3260 | bc_comp FTMP2, FTMP0, CARG2, CARG1, movz, movf, c.ult.d, ->vm_sfcmpult
3265 case BC_ISEQV: case BC_ISNEV:
3266 vk = op == BC_ISEQV;
3267 | // RA = src1*8, RD = src2*8, JMP with RD = target
3268 | daddu RA, BASE, RA
3270 | daddu RD, BASE, RD
3272 | lhu TMP2, -4+OFS_RD(PC)
3274 | gettp CARG3, CARG1
3275 | gettp CARG4, CARG2
3276 | sltu AT, TISNUM, CARG3
3277 | sltu TMP1, TISNUM, CARG4
3280 | beqz AT, ->BC_ISEQN_Z
3282 | beqz AT, ->BC_ISNEN_Z
3284 | // Either or both types are not numbers.
3285 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3288 | beq CARG3, AT, ->vmeta_equal_cd
3292 | beq CARG4, AT, ->vmeta_equal_cd
3295 | bne CARG1, CARG2, >2
3296 |. addu TMP2, TMP2, TMP3
3297 | // Tag and value are equal.
3300 | daddu PC, PC, TMP2
3305 |2: // Check if the tags are the same and it's a table or userdata.
3306 | xor AT, CARG3, CARG4 // Same type?
3307 | sltiu TMP0, CARG3, LJ_TISTABUD+1 // Table or userdata?
3309 | seleqz TMP0, TMP0, AT
3316 | beqz TMP0, ->BC_ISEQV_Z // Reuse code from opposite instruction.
3318 | // Different tables or userdatas. Need to check __eq metamethod.
3319 | // Field metatable must be at same offset for GCtab and GCudata!
3320 |. cleartp TAB:TMP1, CARG1
3321 | ld TAB:TMP3, TAB:TMP1->metatable
3323 | beqz TAB:TMP3, <1 // No metatable?
3325 | lbu TMP3, TAB:TMP3->nomm
3326 | andi TMP3, TMP3, 1<<MM_eq
3327 | bnez TMP3, >1 // Or 'no __eq' flag set?
3329 | beqz TAB:TMP3,->BC_ISEQV_Z // No metatable?
3331 | lbu TMP3, TAB:TMP3->nomm
3332 | andi TMP3, TMP3, 1<<MM_eq
3333 | bnez TMP3, ->BC_ISEQV_Z // Or 'no __eq' flag set?
3336 | b ->vmeta_equal // Handle __eq metamethod.
3337 |. li TMP0, 1-vk // ne = 0 or 1.
3340 case BC_ISEQS: case BC_ISNES:
3341 vk = op == BC_ISEQS;
3342 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3343 | daddu RA, BASE, RA
3346 | dsubu RD, KBASE, RD
3347 | lhu TMP2, -4+OFS_RD(PC)
3348 | ld CARG2, -8(RD) // KBASE-8-str_const*8
3356 | beq TMP0, AT, ->vmeta_equal_cd
3358 |. settp CARG2, TMP1
3359 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3360 | xor TMP1, CARG1, CARG2
3361 | addu TMP2, TMP2, TMP3
3364 | seleqz TMP2, TMP2, TMP1
3366 | selnez TMP2, TMP2, TMP1
3370 | movn TMP2, r0, TMP1
3372 | movz TMP2, r0, TMP1
3375 | daddu PC, PC, TMP2
3379 case BC_ISEQN: case BC_ISNEN:
3380 vk = op == BC_ISEQN;
3381 | // RA = src*8, RD = num_const*8, JMP with RD = target
3382 | daddu RA, BASE, RA
3383 | daddu RD, KBASE, RD
3386 | lhu TMP2, OFS_RD(PC)
3387 | gettp CARG3, CARG1
3388 | gettp CARG4, CARG2
3390 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3396 | bne CARG3, TISNUM, >3
3398 | bne CARG4, TISNUM, >6
3399 |. addu TMP2, TMP2, TMP3
3400 | xor AT, CARG1, CARG2
3403 | seleqz TMP2, TMP2, AT
3405 | daddu PC, PC, TMP2
3408 | selnez TMP2, TMP2, AT
3411 | daddu PC, PC, TMP2
3417 | daddu PC, PC, TMP2
3423 | daddu PC, PC, TMP2
3428 |3: // RA is not an integer.
3429 | sltu AT, CARG3, TISNUM
3435 |. addu TMP2, TMP2, TMP3
3436 | sltu AT, CARG4, TISNUM
3443 |4: // RA and RD are both numbers.
3446 | cmp.eq.d FTMP0, FTMP0, FTMP2
3450 |. selnez TMP2, TMP2, TMP1
3452 |. seleqz TMP2, TMP2, TMP1
3455 | c.eq.d FTMP0, FTMP2
3469 |. selnez TMP2, TMP2, CRET1
3471 |. seleqz TMP2, TMP2, CRET1
3475 |. movz TMP2, r0, CRET1
3477 |. movn TMP2, r0, CRET1
3482 |5: // RA is a number, RD is not a number.
3484 | bne CARG4, TISNUM, >9
3486 | bne CARG4, TISNUM, <2
3488 | // RA is a number, RD is an integer. Convert RD to a number.
3490 |. lwc1 FTMP2, LO(RD)
3492 |. cvt.d.w FTMP2, FTMP2
3494 |. sextw CARG2, CARG2
3501 |6: // RA is an integer, RD is not an integer
3502 | sltu AT, CARG4, TISNUM
3508 | // RA is an integer, RD is a number. Convert RA to a number.
3510 |. lwc1 FTMP0, LO(RA)
3513 | cvt.d.w FTMP0, FTMP0
3515 |. sextw CARG1, CARG1
3527 | b ->vmeta_equal_cd
3533 | b ->vmeta_equal_cd
3538 case BC_ISEQP: case BC_ISNEP:
3539 vk = op == BC_ISEQP;
3540 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3541 | daddu RA, BASE, RA
3544 | lhu TMP2, OFS_RD(PC)
3550 | beq TMP0, AT, ->vmeta_equal_cd
3552 |. xor TMP0, TMP0, TMP1
3554 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3555 | addu TMP2, TMP2, TMP3
3558 | seleqz TMP2, TMP2, TMP0
3560 | selnez TMP2, TMP2, TMP0
3564 | movn TMP2, r0, TMP0
3566 | movz TMP2, r0, TMP0
3569 | daddu PC, PC, TMP2
3573 /* -- Unary test and copy ops ------------------------------------------- */
3575 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3576 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3577 | daddu RD, BASE, RD
3578 | lhu TMP2, OFS_RD(PC)
3582 | sltiu TMP0, TMP0, LJ_TISTRUECOND
3583 if (op == BC_IST || op == BC_ISF) {
3585 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3586 | addu TMP2, TMP2, TMP3
3589 | selnez TMP2, TMP2, TMP0;
3591 | seleqz TMP2, TMP2, TMP0;
3595 | movz TMP2, r0, TMP0
3597 | movn TMP2, r0, TMP0
3600 | daddu PC, PC, TMP2
3603 if (op == BC_ISTC) {
3608 |. daddu RA, BASE, RA
3610 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
3611 | addu TMP2, TMP2, TMP3
3613 | daddu PC, PC, TMP2
3620 | // RA = src*8, RD = -type*8
3621 | daddu TMP2, BASE, RA
3626 | daddu AT, TMP0, TMP1
3627 | bnez AT, ->vmeta_istype
3631 | // RA = src*8, RD = -(TISNUM-1)*8
3632 | daddu TMP2, BASE, RA
3635 | checknum TMP0, ->vmeta_istype
3639 /* -- Unary ops --------------------------------------------------------- */
3642 | // RA = dst*8, RD = src*8
3643 | daddu RD, BASE, RD
3644 | daddu RA, BASE, RA
3651 | // RA = dst*8, RD = src*8
3652 | daddu RD, BASE, RD
3653 | daddu RA, BASE, RA
3657 | sltu TMP0, AT, TMP0
3658 | addiu TMP0, TMP0, 1
3659 | dsll TMP0, TMP0, 47
3666 | // RA = dst*8, RD = src*8
3667 | daddu RB, BASE, RD
3669 | daddu RA, BASE, RA
3670 | gettp CARG3, CARG1
3671 | bne CARG3, TISNUM, >2
3673 | sextw CARG1, CARG1
3674 | beq CARG1, TMP1, ->vmeta_unm // Meta handler deals with -2^31.
3675 |. negu CARG1, CARG1
3676 | zextw CARG1, CARG1
3677 | settp CARG1, TISNUM
3683 | sltiu AT, CARG3, LJ_TISNUM
3684 | beqz AT, ->vmeta_unm
3685 |. dsll TMP1, TMP1, 32
3687 |. xor CARG1, CARG1, TMP1
3690 | // RA = dst*8, RD = src*8
3691 | daddu CARG2, BASE, RD
3692 | daddu RA, BASE, RA
3695 | daddiu AT, TMP1, -LJ_TSTR
3697 |. cleartp STR:CARG1, TMP0
3698 | lw CRET1, STR:CARG1->len
3700 | settp CRET1, TISNUM
3705 | daddiu AT, TMP1, -LJ_TTAB
3706 | bnez AT, ->vmeta_len
3709 | ld TAB:TMP2, TAB:CARG1->metatable
3715 | load_got lj_tab_len
3716 | call_intern lj_tab_len // (GCtab *t)
3718 | // Returns uint32_t (but less than 2^31).
3723 | lbu TMP0, TAB:TMP2->nomm
3724 | andi TMP0, TMP0, 1<<MM_len
3725 | bnez TMP0, <3 // 'no __len' flag set: done.
3732 /* -- Binary ops -------------------------------------------------------- */
3734 |.macro fpmod, a, b, c
3735 | bal ->vm_floor // floor(b/c)
3736 |. div.d FARG1, b, c
3738 | sub.d a, b, a // b - floor(b/c)*c
3742 | daddiu sp, sp, -16
3751 |. move CARG1, CRET1
3761 |. move CARG2, CRET1
3766 |.macro ins_arithpre, label
3767 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3768 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3771 | decode_RB8a RB, INS
3773 | decode_RDtoRC8 RC, RD
3774 | // RA = dst*8, RB = src1*8, RC = num_const*8
3775 | daddu RB, BASE, RB
3776 |.if "label" ~= "none"
3779 |. daddu RC, KBASE, RC
3782 | decode_RB8a RC, INS
3784 | decode_RDtoRC8 RB, RD
3785 | // RA = dst*8, RB = num_const*8, RC = src1*8
3786 | daddu RC, BASE, RC
3787 |.if "label" ~= "none"
3790 |. daddu RB, KBASE, RB
3793 | decode_RB8a RB, INS
3795 | decode_RDtoRC8 RC, RD
3796 | // RA = dst*8, RB = src1*8, RC = src2*8
3797 | daddu RB, BASE, RB
3798 |.if "label" ~= "none"
3801 |. daddu RC, BASE, RC
3806 |.macro ins_arith, intins, fpins, fpcall, label
3809 |.if "label" ~= "none"
3819 |.if "intins" ~= "div"
3821 | // Check for two integers.
3822 | sextw CARG3, CARG1
3823 | bne TMP0, TISNUM, >5
3824 |. sextw CARG4, CARG2
3825 | bne TMP1, TISNUM, >5
3827 |.if "intins" == "addu"
3828 |. intins CRET1, CARG3, CARG4
3829 | xor TMP1, CRET1, CARG3 // ((y^a) & (y^b)) < 0: overflow.
3830 | xor TMP2, CRET1, CARG4
3831 | and TMP1, TMP1, TMP2
3832 | bltz TMP1, ->vmeta_arith
3833 |. daddu RA, BASE, RA
3834 |.elif "intins" == "subu"
3835 |. intins CRET1, CARG3, CARG4
3836 | xor TMP1, CRET1, CARG3 // ((y^a) & (a^b)) < 0: overflow.
3837 | xor TMP2, CARG3, CARG4
3838 | and TMP1, TMP1, TMP2
3839 | bltz TMP1, ->vmeta_arith
3840 |. daddu RA, BASE, RA
3841 |.elif "intins" == "mult"
3844 | mul CRET1, CARG3, CARG4
3845 | muh TMP2, CARG3, CARG4
3847 |. intins CARG3, CARG4
3851 | sra TMP1, CRET1, 31
3852 | bne TMP1, TMP2, ->vmeta_arith
3853 |. daddu RA, BASE, RA
3855 |. load_got lj_vm_modi
3856 | beqz CARG4, ->vmeta_arith
3857 |. daddu RA, BASE, RA
3860 |. move CARG2, CARG4
3863 | zextw CRET1, CRET1
3864 | settp CRET1, TISNUM
3872 |5: // Check for two numbers.
3873 | .FPU ldc1 FTMP0, 0(RB)
3874 | sltu AT, TMP0, TISNUM
3875 | sltu TMP0, TMP1, TISNUM
3876 | .FPU ldc1 FTMP2, 0(RC)
3878 | beqz AT, ->vmeta_arith
3879 |. daddu RA, BASE, RA
3882 | fpins FRET1, FTMP0, FTMP2
3883 |.elif "fpcall" == "sfpmod"
3892 |.if "intins" ~= "div"
3896 |. sdc1 FRET1, 0(RA)
3900 |.if "intins" == "div"
3906 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3907 | ins_arith addu, add.d, __adddf3, none
3909 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3910 | ins_arith subu, sub.d, __subdf3, none
3912 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3913 | ins_arith mult, mul.d, __muldf3, none
3916 | ins_arith div, div.d, __divdf3, ->BC_DIVVN_Z
3918 case BC_DIVNV: case BC_DIVVV:
3919 | ins_arithpre ->BC_DIVVN_Z
3922 | ins_arith modi, fpmod, sfpmod, ->BC_MODVN_Z
3924 case BC_MODNV: case BC_MODVV:
3925 | ins_arithpre ->BC_MODVN_Z
3933 | sltiu TMP0, TMP0, LJ_TISNUM
3934 | sltiu TMP1, TMP1, LJ_TISNUM
3935 | and AT, TMP0, TMP1
3937 | beqz AT, ->vmeta_arith
3938 |. daddu RA, BASE, RA
3955 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3956 | decode_RB8a RB, INS
3958 | decode_RDtoRC8 RC, RD
3959 | dsubu CARG3, RC, RB
3961 | daddu CARG2, BASE, RC
3964 | load_got lj_meta_cat
3965 | srl CARG3, CARG3, 3
3967 | call_intern lj_meta_cat // (lua_State *L, TValue *top, int left)
3969 | // Returns NULL (finished) or TValue * (metamethod).
3970 | bnez CRET1, ->vmeta_binop
3972 | daddu RB, BASE, MULTRES
3974 | daddu RA, BASE, RA
3980 /* -- Constant ops ------------------------------------------------------ */
3983 | // RA = dst*8, RD = str_const*8 (~)
3984 | dsubu TMP1, KBASE, RD
3987 | ld TMP0, -8(TMP1) // KBASE-8-str_const*8
3988 | daddu RA, BASE, RA
3995 | // RA = dst*8, RD = cdata_const*8 (~)
3996 | dsubu TMP1, KBASE, RD
3998 | ld TMP0, -8(TMP1) // KBASE-8-cdata_const*8
3999 | li TMP2, LJ_TCDATA
4000 | daddu RA, BASE, RA
4007 | // RA = dst*8, RD = int16_literal*8
4009 | daddu RA, BASE, RA
4017 | // RA = dst*8, RD = num_const*8
4018 | daddu RD, KBASE, RD
4019 | daddu RA, BASE, RA
4026 | // RA = dst*8, RD = primitive_type*8 (~)
4027 | daddu RA, BASE, RA
4035 | // RA = base*8, RD = end*8
4036 | daddu RA, BASE, RA
4039 | daddu RD, BASE, RD
4048 /* -- Upvalue and function ops ------------------------------------------ */
4051 | // RA = dst*8, RD = uvnum*8
4052 | ld LFUNC:RB, FRAME_FUNC(BASE)
4053 | daddu RA, BASE, RA
4055 | daddu RD, RD, LFUNC:RB
4056 | ld UPVAL:RB, LFUNC:RD->uvptr
4058 | ld TMP1, UPVAL:RB->v
4064 | // RA = uvnum*8, RD = src*8
4065 | ld LFUNC:RB, FRAME_FUNC(BASE)
4066 | daddu RD, BASE, RD
4068 | daddu RA, RA, LFUNC:RB
4069 | ld UPVAL:RB, LFUNC:RA->uvptr
4071 | lbu TMP3, UPVAL:RB->marked
4072 | ld CARG2, UPVAL:RB->v
4073 | andi TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
4074 | lbu TMP0, UPVAL:RB->closed
4076 | sd CRET1, 0(CARG2)
4077 | li AT, LJ_GC_BLACK|1
4078 | or TMP3, TMP3, TMP0
4079 | beq TMP3, AT, >2 // Upvalue is closed and black?
4080 |. daddiu TMP2, TMP2, -(LJ_TNUMX+1)
4084 |2: // Check if new value is collectable.
4085 | sltiu AT, TMP2, LJ_TISGCV - (LJ_TNUMX+1)
4086 | beqz AT, <1 // tvisgcv(v)
4087 |. cleartp GCOBJ:CRET1, CRET1
4088 | lbu TMP3, GCOBJ:CRET1->gch.marked
4089 | andi TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
4091 |. load_got lj_gc_barrieruv
4092 | // Crossed a write barrier. Move the barrier forward.
4093 | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv)
4094 |. daddiu CARG1, DISPATCH, GG_DISP2G
4099 | // RA = uvnum*8, RD = str_const*8 (~)
4100 | ld LFUNC:RB, FRAME_FUNC(BASE)
4101 | dsubu TMP1, KBASE, RD
4103 | daddu RA, RA, LFUNC:RB
4104 | ld UPVAL:RB, LFUNC:RA->uvptr
4105 | ld STR:TMP1, -8(TMP1) // KBASE-8-str_const*8
4106 | lbu TMP2, UPVAL:RB->marked
4107 | ld CARG2, UPVAL:RB->v
4108 | lbu TMP3, STR:TMP1->marked
4109 | andi AT, TMP2, LJ_GC_BLACK // isblack(uv)
4110 | lbu TMP2, UPVAL:RB->closed
4114 |. sd TMP1, 0(CARG2)
4118 |2: // Check if string is white and ensure upvalue is closed.
4120 |. andi AT, TMP3, LJ_GC_WHITES // iswhite(str)
4122 |. load_got lj_gc_barrieruv
4123 | // Crossed a write barrier. Move the barrier forward.
4124 | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv)
4125 |. daddiu CARG1, DISPATCH, GG_DISP2G
4130 | // RA = uvnum*8, RD = num_const*8
4131 | ld LFUNC:RB, FRAME_FUNC(BASE)
4132 | daddu RD, KBASE, RD
4134 | daddu RA, RA, LFUNC:RB
4135 | ld UPVAL:RB, LFUNC:RA->uvptr
4137 | ld TMP1, UPVAL:RB->v
4143 | // RA = uvnum*8, RD = primitive_type*8 (~)
4144 | ld LFUNC:RB, FRAME_FUNC(BASE)
4147 | daddu RA, RA, LFUNC:RB
4149 | ld UPVAL:RB, LFUNC:RA->uvptr
4151 | ld TMP1, UPVAL:RB->v
4157 | // RA = level*8, RD = target
4158 | ld TMP2, L->openupval
4159 | branch_RD // Do this first since RD is not saved.
4160 | load_got lj_func_closeuv
4164 | call_intern lj_func_closeuv // (lua_State *L, TValue *level)
4165 |. daddu CARG2, BASE, RA
4172 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
4173 | load_got lj_func_newL_gc
4174 | dsubu TMP1, KBASE, RD
4175 | ld CARG3, FRAME_FUNC(BASE)
4176 | ld CARG2, -8(TMP1) // KBASE-8-tab_const*8
4180 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4181 | call_intern lj_func_newL_gc
4183 | // Returns GCfuncL *.
4188 | daddu RA, BASE, RA
4193 /* -- Table ops --------------------------------------------------------- */
4197 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
4198 | ld TMP0, DISPATCH_GL(gc.total)(DISPATCH)
4199 | ld TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
4202 | sltu AT, TMP0, TMP1
4205 if (op == BC_TNEW) {
4206 | load_got lj_tab_new
4208 | andi CARG2, CARG2, 0x7ff
4210 | addiu AT, CARG2, -0x7ff
4213 | seleqz TMP0, TMP0, AT
4214 | selnez CARG2, CARG2, AT
4215 | or CARG2, CARG2, TMP0
4217 | movz CARG2, TMP0, AT
4219 | // (lua_State *L, int32_t asize, uint32_t hbits)
4220 | call_intern lj_tab_new
4222 | // Returns Table *.
4224 | load_got lj_tab_dup
4225 | dsubu TMP1, KBASE, RD
4227 | call_intern lj_tab_dup // (lua_State *L, Table *kt)
4228 |. ld CARG2, -8(TMP1) // KBASE-8-str_const*8
4229 | // Returns Table *.
4234 | daddu RA, BASE, RA
4239 | load_got lj_gc_step_fixtop
4241 | call_intern lj_gc_step_fixtop // (lua_State *L)
4248 | // RA = dst*8, RD = str_const*8 (~)
4250 | // RA = src*8, RD = str_const*8 (~)
4251 | ld LFUNC:TMP2, FRAME_FUNC(BASE)
4252 | dsubu TMP1, KBASE, RD
4253 | ld STR:RC, -8(TMP1) // KBASE-8-str_const*8
4254 | cleartp LFUNC:TMP2
4255 | ld TAB:RB, LFUNC:TMP2->env
4256 if (op == BC_GGET) {
4261 |. daddu RA, BASE, RA
4265 | // RA = dst*8, RB = table*8, RC = key*8
4266 | decode_RB8a RB, INS
4268 | decode_RDtoRC8 RC, RD
4269 | daddu CARG2, BASE, RB
4270 | daddu CARG3, BASE, RC
4271 | ld TAB:RB, 0(CARG2)
4273 | daddu RA, BASE, RA
4274 | checktab TAB:RB, ->vmeta_tgetv
4276 | bne TMP3, TISNUM, >5 // Integer key?
4277 |. lw TMP0, TAB:RB->asize
4279 | ld TMP1, TAB:RB->array
4280 | sltu AT, TMP2, TMP0
4282 | beqz AT, ->vmeta_tgetv // Integer key and in array part?
4283 |. daddu TMP2, TMP1, TMP2
4285 | beq AT, TISNIL, >2
4286 |. ld CRET1, 0(TMP2)
4292 |2: // Check for __index if table value is nil.
4293 | ld TAB:TMP2, TAB:RB->metatable
4294 | beqz TAB:TMP2, <1 // No metatable: done.
4296 | lbu TMP0, TAB:TMP2->nomm
4297 | andi TMP0, TMP0, 1<<MM_index
4298 | bnez TMP0, <1 // 'no __index' flag set: done.
4305 | bne TMP3, AT, ->vmeta_tgetv
4307 | b ->BC_TGETS_Z // String key?
4311 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
4312 | decode_RB8a RB, INS
4314 | decode_RC8a RC, INS
4315 | daddu CARG2, BASE, RB
4317 | ld TAB:RB, 0(CARG2)
4318 | dsubu CARG3, KBASE, RC
4319 | daddu RA, BASE, RA
4320 | ld STR:RC, -8(CARG3) // KBASE-8-str_const*8
4321 | checktab TAB:RB, ->vmeta_tgets1
4323 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
4324 | lw TMP0, TAB:RB->hmask
4325 | lw TMP1, STR:RC->sid
4326 | ld NODE:TMP2, TAB:RB->node
4327 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4330 | subu TMP1, TMP0, TMP1
4332 | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4333 | settp STR:RC, TMP3 // Tagged key to look for.
4335 | ld CARG1, NODE:TMP2->key
4336 | ld CRET1, NODE:TMP2->val
4337 | ld NODE:TMP1, NODE:TMP2->next
4339 |. ld TAB:TMP3, TAB:RB->metatable
4340 | beq CRET1, TISNIL, >5 // Key found, but nil value?
4347 |4: // Follow hash chain.
4348 | bnez NODE:TMP1, <1
4349 |. move NODE:TMP2, NODE:TMP1
4350 | // End of hash chain: key not found, nil result.
4352 |5: // Check for __index if table value is nil.
4353 | beqz TAB:TMP3, <3 // No metatable: done.
4354 |. move CRET1, TISNIL
4355 | lbu TMP0, TAB:TMP3->nomm
4356 | andi TMP0, TMP0, 1<<MM_index
4357 | bnez TMP0, <3 // 'no __index' flag set: done.
4363 | // RA = dst*8, RB = table*8, RC = index*8
4364 | decode_RB8a RB, INS
4366 | daddu CARG2, BASE, RB
4367 | decode_RDtoRC8 RC, RD
4368 | ld TAB:RB, 0(CARG2)
4369 | daddu RA, BASE, RA
4371 | checktab TAB:RB, ->vmeta_tgetb
4372 | lw TMP1, TAB:RB->asize
4373 | ld TMP2, TAB:RB->array
4374 | sltu AT, TMP0, TMP1
4375 | beqz AT, ->vmeta_tgetb
4376 |. daddu RC, TMP2, RC
4378 | beq AT, TISNIL, >5
4385 |5: // Check for __index if table value is nil.
4386 | ld TAB:TMP2, TAB:RB->metatable
4387 | beqz TAB:TMP2, <1 // No metatable: done.
4389 | lbu TMP1, TAB:TMP2->nomm
4390 | andi TMP1, TMP1, 1<<MM_index
4391 | bnez TMP1, <1 // 'no __index' flag set: done.
4393 | b ->vmeta_tgetb // Caveat: preserve TMP0 and CARG2!
4397 | // RA = dst*8, RB = table*8, RC = key*8
4398 | decode_RB8a RB, INS
4400 | decode_RDtoRC8 RC, RD
4401 | daddu RB, BASE, RB
4402 | daddu RC, BASE, RC
4403 | ld TAB:CARG1, 0(RB)
4405 | daddu RA, BASE, RA
4407 | lw TMP0, TAB:CARG1->asize
4408 | ld TMP1, TAB:CARG1->array
4409 | sltu AT, CARG2, TMP0
4410 | sll TMP2, CARG2, 3
4411 | beqz AT, ->vmeta_tgetr // In array part?
4412 |. daddu CRET1, TMP1, TMP2
4413 | ld CARG2, 0(CRET1)
4421 | // RA = src*8, RB = table*8, RC = key*8
4422 | decode_RB8a RB, INS
4424 | decode_RDtoRC8 RC, RD
4425 | daddu CARG2, BASE, RB
4426 | daddu CARG3, BASE, RC
4429 | daddu RA, BASE, RA
4430 | checktab RB, ->vmeta_tsetv
4433 | lw TMP0, TAB:RB->asize
4434 | ld TMP1, TAB:RB->array
4437 | beqz AT, ->vmeta_tsetv // Integer key and in array part?
4438 |. daddu TMP1, TMP1, TMP2
4440 | lbu TMP3, TAB:RB->marked
4441 | beq TMP0, TISNIL, >3
4444 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4446 |. sd CRET1, 0(TMP1)
4450 |3: // Check for __newindex if previous value is nil.
4451 | ld TAB:TMP2, TAB:RB->metatable
4452 | beqz TAB:TMP2, <1 // No metatable: done.
4454 | lbu TMP2, TAB:TMP2->nomm
4455 | andi TMP2, TMP2, 1<<MM_newindex
4456 | bnez TMP2, <1 // 'no __newindex' flag set: done.
4463 | daddiu AT, AT, -LJ_TSTR
4464 | bnez AT, ->vmeta_tsetv
4466 | b ->BC_TSETS_Z // String key?
4467 |. cleartp STR:RC, TMP2
4469 |7: // Possible table write barrier for the value. Skip valiswhite check.
4470 | barrierback TAB:RB, TMP3, TMP0, <2
4473 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4474 | decode_RB8a RB, INS
4476 | daddu CARG2, BASE, RB
4477 | decode_RC8a RC, INS
4478 | ld TAB:RB, 0(CARG2)
4480 | dsubu CARG3, KBASE, RC
4481 | ld RC, -8(CARG3) // KBASE-8-str_const*8
4482 | daddu RA, BASE, RA
4484 | checktab TAB:RB, ->vmeta_tsets1
4486 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = BASE+src*8
4487 | lw TMP0, TAB:RB->hmask
4488 | lw TMP1, STR:RC->sid
4489 | ld NODE:TMP2, TAB:RB->node
4490 | sb r0, TAB:RB->nomm // Clear metamethod cache.
4491 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4494 | subu TMP1, TMP0, TMP1
4496 | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4497 | settp STR:RC, TMP3 // Tagged key to look for.
4504 | ld TMP0, NODE:TMP2->key
4505 | ld CARG2, NODE:TMP2->val
4506 | ld NODE:TMP1, NODE:TMP2->next
4508 |. lbu TMP3, TAB:RB->marked
4509 | beq CARG2, TISNIL, >4 // Key found, but nil value?
4510 |. ld TAB:TMP0, TAB:RB->metatable
4512 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4515 |. sdc1 FTMP0, NODE:TMP2->val
4517 |. sd CRET1, NODE:TMP2->val
4522 |4: // Check for __newindex if previous value is nil.
4523 | beqz TAB:TMP0, <2 // No metatable: done.
4525 | lbu TMP0, TAB:TMP0->nomm
4526 | andi TMP0, TMP0, 1<<MM_newindex
4527 | bnez TMP0, <2 // 'no __newindex' flag set: done.
4532 |5: // Follow hash chain.
4533 | bnez NODE:TMP1, <1
4534 |. move NODE:TMP2, NODE:TMP1
4535 | // End of hash chain: key not found, add a new one
4537 | // But check for __newindex first.
4538 | ld TAB:TMP2, TAB:RB->metatable
4539 | beqz TAB:TMP2, >6 // No metatable: continue.
4540 |. daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv)
4541 | lbu TMP0, TAB:TMP2->nomm
4542 | andi TMP0, TMP0, 1<<MM_newindex
4543 | beqz TMP0, ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4545 | load_got lj_tab_newkey
4548 | move CARG2, TAB:RB
4550 | call_intern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k
4552 | // Returns TValue *.
4555 | b <3 // No 2nd write barrier needed.
4556 |. sdc1 FTMP0, 0(CRET1)
4559 | b <3 // No 2nd write barrier needed.
4560 |. sd CARG1, 0(CRET1)
4563 |7: // Possible table write barrier for the value. Skip valiswhite check.
4564 | barrierback TAB:RB, TMP3, TMP0, <3
4567 | // RA = src*8, RB = table*8, RC = index*8
4568 | decode_RB8a RB, INS
4570 | daddu CARG2, BASE, RB
4571 | decode_RDtoRC8 RC, RD
4572 | ld TAB:RB, 0(CARG2)
4573 | daddu RA, BASE, RA
4575 | checktab RB, ->vmeta_tsetb
4576 | lw TMP1, TAB:RB->asize
4577 | ld TMP2, TAB:RB->array
4578 | sltu AT, TMP0, TMP1
4579 | beqz AT, ->vmeta_tsetb
4580 |. daddu RC, TMP2, RC
4582 | lbu TMP3, TAB:RB->marked
4583 | beq TMP1, TISNIL, >5
4586 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4592 |5: // Check for __newindex if previous value is nil.
4593 | ld TAB:TMP2, TAB:RB->metatable
4594 | beqz TAB:TMP2, <1 // No metatable: done.
4596 | lbu TMP1, TAB:TMP2->nomm
4597 | andi TMP1, TMP1, 1<<MM_newindex
4598 | bnez TMP1, <1 // 'no __newindex' flag set: done.
4600 | b ->vmeta_tsetb // Caveat: preserve TMP0 and CARG2!
4603 |7: // Possible table write barrier for the value. Skip valiswhite check.
4604 | barrierback TAB:RB, TMP3, TMP0, <2
4607 | // RA = dst*8, RB = table*8, RC = key*8
4608 | decode_RB8a RB, INS
4610 | decode_RDtoRC8 RC, RD
4611 | daddu CARG1, BASE, RB
4612 | daddu CARG3, BASE, RC
4613 | ld TAB:CARG2, 0(CARG1)
4614 | lw CARG3, LO(CARG3)
4616 | lbu TMP3, TAB:CARG2->marked
4617 | lw TMP0, TAB:CARG2->asize
4618 | ld TMP1, TAB:CARG2->array
4619 | andi AT, TMP3, LJ_GC_BLACK // isblack(table)
4621 |. daddu RA, BASE, RA
4623 | sltu AT, CARG3, TMP0
4624 | sll TMP2, CARG3, 3
4625 | beqz AT, ->vmeta_tsetr // In array part?
4626 |. daddu CRET1, TMP1, TMP2
4630 | sd CARG1, 0(CRET1)
4633 |7: // Possible table write barrier for the value. Skip valiswhite check.
4634 | barrierback TAB:CARG2, TMP3, CRET1, <2
4638 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4639 | daddu RA, BASE, RA
4641 | daddu TMP3, KBASE, RD
4642 | ld TAB:CARG2, -8(RA) // Guaranteed to be a table.
4643 | addiu TMP0, MULTRES, -8
4644 | lw TMP3, LO(TMP3) // Integer constant is in lo-word.
4645 | beqz TMP0, >4 // Nothing to copy?
4646 |. srl CARG3, TMP0, 3
4648 | addu CARG3, CARG3, TMP3
4649 | lw TMP2, TAB:CARG2->asize
4651 | lbu TMP3, TAB:CARG2->marked
4652 | ld CARG1, TAB:CARG2->array
4653 | sltu AT, TMP2, CARG3
4655 |. daddu TMP2, RA, TMP0
4656 | daddu TMP1, TMP1, CARG1
4657 | andi TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4658 |3: // Copy result slots to table.
4664 |. daddiu TMP1, TMP1, 8
4670 |5: // Need to resize array part.
4671 | load_got lj_tab_reasize
4675 | call_intern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4677 | // Must not reallocate the stack.
4680 |. ld BASE, L->base // Reload BASE for lack of a saved register.
4682 |7: // Possible table write barrier for any value. Skip valiswhite check.
4683 | barrierback TAB:CARG2, TMP3, TMP0, <4
4686 /* -- Calls and vararg handling ----------------------------------------- */
4689 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4690 | decode_RDtoRC8 NARGS8:RC, RD
4692 |. addu NARGS8:RC, NARGS8:RC, MULTRES
4695 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4696 | decode_RDtoRC8 NARGS8:RC, RD
4699 | daddu BASE, BASE, RA
4700 | ld LFUNC:RB, 0(BASE)
4701 | daddiu BASE, BASE, 16
4702 | addiu NARGS8:RC, NARGS8:RC, -8
4703 | checkfunc RB, ->vmeta_call
4708 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4709 | addu NARGS8:RD, NARGS8:RD, MULTRES // BC_CALLT gets RC from RD.
4710 | // Fall through. Assumes BC_CALLT follows.
4713 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4714 | daddu RA, BASE, RA
4716 | move NARGS8:RC, RD
4717 | ld TMP1, FRAME_PC(BASE)
4719 | addiu NARGS8:RC, NARGS8:RC, -8
4720 | checktp CARG3, RB, -LJ_TFUNC, ->vmeta_callt
4722 | andi TMP0, TMP1, FRAME_TYPE // Caveat: preserve TMP0 until the 'or'.
4723 | lbu TMP3, LFUNC:CARG3->ffid
4725 |. xori TMP2, TMP1, FRAME_VARG
4727 | sd RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4728 | sltiu AT, TMP3, 2 // (> FF_C) Calling a fast function?
4731 | beqz NARGS8:RC, >3
4732 |. move TMP3, NARGS8:RC
4736 | addiu TMP3, TMP3, -8
4739 |. daddiu TMP2, TMP2, 8
4747 |5: // Tailcall to a fast function with a Lua frame below.
4749 | decode_RA8a RA, INS
4751 | dsubu TMP1, BASE, RA
4752 | ld TMP1, -32(TMP1)
4753 | cleartp LFUNC:TMP1
4754 | ld TMP1, LFUNC:TMP1->pc
4756 |. ld KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4758 |7: // Tailcall from a vararg function.
4759 | andi AT, TMP2, FRAME_TYPEP
4760 | bnez AT, <1 // Vararg frame below?
4761 |. dsubu TMP2, BASE, TMP2 // Relocate BASE down.
4763 | ld TMP1, FRAME_PC(TMP2)
4765 |. andi TMP0, TMP1, FRAME_TYPE
4769 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4770 | move TMP2, BASE // Save old BASE fir vmeta_call.
4771 | daddu BASE, BASE, RA
4773 | ld CARG1, -16(BASE)
4774 | ld CARG2, -8(BASE)
4775 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4776 | sd RB, 0(BASE) // Copy callable.
4777 | sd CARG1, 16(BASE) // Copy state.
4778 | sd CARG2, 24(BASE) // Copy control var.
4779 | daddiu BASE, BASE, 16
4780 | checkfunc RB, ->vmeta_call
4785 |.if JIT and ENDIAN_LE
4789 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4790 | daddu RA, BASE, RA
4791 | ld TAB:RB, -16(RA)
4792 | lw RC, -8+LO(RA) // Get index from control var.
4795 | lw TMP0, TAB:RB->asize
4796 | ld TMP1, TAB:RB->array
4797 | dsll CARG3, TISNUM, 47
4798 |1: // Traverse array part.
4800 | beqz AT, >5 // Index points after array part?
4802 | daddu TMP3, TMP1, TMP3
4804 | lhu RD, -4+OFS_RD(PC)
4805 | or TMP2, RC, CARG3
4806 | beq CARG1, TISNIL, <1 // Skip holes in array part.
4810 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
4812 | daddu RD, RD, TMP3
4813 | sw RC, -8+LO(RA) // Update control var.
4818 |5: // Traverse hash part.
4819 | lw TMP1, TAB:RB->hmask
4821 | ld TMP2, TAB:RB->node
4823 | sltu AT, TMP1, RC // End of iteration? Branch to ITERL+1.
4827 | subu TMP3, TMP3, RB
4828 | daddu NODE:TMP3, TMP3, TMP2
4829 | ld CARG1, 0(NODE:TMP3)
4830 | lhu RD, -4+OFS_RD(PC)
4831 | beq CARG1, TISNIL, <6 // Skip holes in hash part.
4833 | ld CARG2, NODE:TMP3->key
4834 | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535)
4842 |. sw RC, -8+LO(RA) // Update control var.
4846 | // RA = base*8, RD = target (points to ITERN)
4847 | daddu RA, BASE, RA
4849 | ld CFUNC:CARG1, -24(RA)
4850 | daddu TMP0, PC, TMP0
4853 | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535)
4854 | checkfunc CFUNC:CARG1, >5
4855 | gettp CARG2, CARG2
4856 | daddiu CARG2, CARG2, -LJ_TTAB
4857 | lbu TMP1, CFUNC:CARG1->ffid
4858 | daddiu CARG3, CARG3, -LJ_TNIL
4859 | or AT, CARG2, CARG3
4860 | daddiu TMP1, TMP1, -FF_next_N
4863 |. lui TMP1, (LJ_KEYINDEX >> 16)
4864 | daddu PC, TMP0, TMP2
4865 | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff)
4866 | dsll TMP1, TMP1, 32
4870 |5: // Despecialize bytecode if any of the checks fail.
4873 | sb TMP3, -4+OFS_OP(PC)
4874 | daddu PC, TMP0, TMP2
4876 | lb TMP0, OFS_OP(PC)
4879 |. lhu TMP2, OFS_RD(PC)
4882 |. sb TMP1, OFS_OP(PC)
4884 |6: // Unpatch JLOOP.
4885 | ld TMP0, DISPATCH_J(trace)(DISPATCH)
4887 | daddu TMP0, TMP0, TMP2
4888 | ld TRACE:TMP2, 0(TMP0)
4889 | lw TMP0, TRACE:TMP2->startins
4891 | and TMP0, TMP0, AT
4892 | or TMP0, TMP0, TMP1
4899 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4900 | ld TMP0, FRAME_PC(BASE)
4901 | decode_RDtoRC8 RC, RD
4902 | decode_RB8a RB, INS
4903 | daddu RC, BASE, RC
4905 | daddu RA, BASE, RA
4906 | daddiu RC, RC, FRAME_VARG
4907 | daddu TMP2, RA, RB
4908 | daddiu TMP3, BASE, -16 // TMP3 = vtop
4909 | dsubu RC, RC, TMP0 // RC = vbase
4910 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4911 | beqz RB, >5 // Copy all varargs?
4912 |. dsubu TMP1, TMP3, RC
4913 | daddiu TMP2, TMP2, -16
4914 |1: // Copy vararg slots to destination slots.
4919 | selnez CARG1, CARG1, AT
4920 | seleqz AT, TISNIL, AT
4921 | or CARG1, CARG1, AT
4923 | movz CARG1, TISNIL, AT
4932 |5: // Copy all varargs.
4933 | ld TMP0, L->maxstack
4934 | blez TMP1, <3 // No vararg slots?
4935 |. li MULTRES, 8 // MULTRES = (0+1)*8
4936 | daddu TMP2, RA, TMP1
4937 | sltu AT, TMP0, TMP2
4939 |. daddiu MULTRES, TMP1, 8
4945 | bnez AT, <6 // More vararg slots?
4950 |7: // Grow stack for varargs.
4951 | load_got lj_state_growstack
4953 | dsubu RA, RA, BASE
4955 | dsubu BASE, RC, BASE // Need delta, because BASE may change.
4957 | srl CARG2, TMP1, 3
4958 | call_intern lj_state_growstack // (lua_State *L, int n)
4962 | daddu RA, BASE, RA
4963 | daddu RC, BASE, RC
4965 |. daddiu TMP3, BASE, -16
4968 /* -- Returns ----------------------------------------------------------- */
4971 | // RA = results*8, RD = extra_nresults*8
4972 | addu RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4973 | // Fall through. Assumes BC_RET follows.
4977 | // RA = results*8, RD = (nresults+1)*8
4978 | ld PC, FRAME_PC(BASE)
4979 | daddu RA, BASE, RA
4982 | andi TMP0, PC, FRAME_TYPE
4983 | bnez TMP0, ->BC_RETV_Z
4984 |. xori TMP1, PC, FRAME_VARG
4987 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4989 | daddiu TMP2, BASE, -16
4991 | decode_RA8a TMP0, INS
4992 | decode_RB8a RB, INS
4995 | daddu TMP3, TMP2, RB
4997 |. dsubu BASE, TMP2, TMP0
5004 |. daddiu TMP2, TMP2, 8
5006 | daddiu TMP3, TMP3, -8
5008 | sltu AT, TMP2, TMP3
5010 |. ld LFUNC:TMP1, FRAME_FUNC(BASE)
5012 | cleartp LFUNC:TMP1
5013 | ld TMP1, LFUNC:TMP1->pc
5014 | ld KBASE, PC2PROTO(k)(TMP1)
5017 |6: // Fill up results with nil.
5018 | sd TISNIL, 0(TMP2)
5020 |. daddiu TMP2, TMP2, 8
5022 |->BC_RETV_Z: // Non-standard return case.
5023 | andi TMP2, TMP1, FRAME_TYPEP
5024 | bnez TMP2, ->vm_return
5026 | // Return from vararg function: relocate BASE down.
5027 | dsubu BASE, BASE, TMP1
5029 |. ld PC, FRAME_PC(BASE)
5032 case BC_RET0: case BC_RET1:
5033 | // RA = results*8, RD = (nresults+1)*8
5034 | ld PC, FRAME_PC(BASE)
5035 | daddu RA, BASE, RA
5037 | andi TMP0, PC, FRAME_TYPE
5038 | bnez TMP0, ->BC_RETV_Z
5039 |. xori TMP1, PC, FRAME_VARG
5041 | daddiu TMP2, BASE, -16
5042 if (op == BC_RET1) {
5045 | decode_RB8a RB, INS
5046 | decode_RA8a RA, INS
5049 | dsubu BASE, TMP2, RA
5050 if (op == BC_RET1) {
5056 |. ld TMP1, FRAME_FUNC(BASE)
5058 | cleartp LFUNC:TMP1
5059 | ld TMP1, LFUNC:TMP1->pc
5060 | ld KBASE, PC2PROTO(k)(TMP1)
5063 |6: // Fill up results with nil.
5064 | daddiu TMP2, TMP2, 8
5067 if (op == BC_RET1) {
5068 |. sd TISNIL, 0(TMP2)
5070 |. sd TISNIL, -8(TMP2)
5074 /* -- Loops and branches ------------------------------------------------ */
5080 | // Fall through. Assumes BC_IFORL follows.
5090 | // RA = base*8, RD = target (after end of loop or start of loop)
5091 vk = (op == BC_IFORL || op == BC_JFORL);
5092 | daddu RA, BASE, RA
5093 | ld CARG1, FORL_IDX*8(RA) // IDX CARG1 - CARG3 type
5094 | gettp CARG3, CARG1
5095 if (op != BC_JFORL) {
5097 | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535)
5098 | daddu TMP2, RD, TMP2
5101 | ld CARG2, FORL_STOP*8(RA) // STOP CARG2 - CARG4 type
5102 | ld CRET1, FORL_STEP*8(RA) // STEP CRET1 - CRET2 type
5103 | gettp CARG4, CARG2
5104 | bne CARG3, TISNUM, >5
5105 |. gettp CRET2, CRET1
5106 | bne CARG4, TISNUM, ->vmeta_for
5107 |. sextw CARG3, CARG1
5108 | bne CRET2, TISNUM, ->vmeta_for
5109 |. sextw CARG2, CARG2
5110 | dext AT, CRET1, 31, 0
5111 | slt CRET1, CARG2, CARG3
5112 | slt TMP1, CARG3, CARG2
5114 | selnez TMP1, TMP1, AT
5115 | seleqz CRET1, CRET1, AT
5116 | or CRET1, CRET1, TMP1
5118 | movn CRET1, TMP1, AT
5121 | bne CARG3, TISNUM, >5
5122 |. ld CARG2, FORL_STEP*8(RA) // STEP CARG2 - CARG4 type
5123 | ld CRET1, FORL_STOP*8(RA) // STOP CRET1 - CRET2 type
5125 | sextw CARG2, CARG2
5126 | sextw CRET1, CRET1
5127 | addu CARG1, TMP3, CARG2
5128 | xor TMP0, CARG1, TMP3
5129 | xor TMP1, CARG1, CARG2
5130 | and TMP0, TMP0, TMP1
5131 | slt TMP1, CARG1, CRET1
5132 | slt CRET1, CRET1, CARG1
5134 | slt TMP0, TMP0, r0 // ((y^a) & (y^b)) < 0: overflow.
5136 | selnez TMP1, TMP1, AT
5137 | seleqz CRET1, CRET1, AT
5138 | or CRET1, CRET1, TMP1
5140 | movn CRET1, TMP1, AT
5142 | or CRET1, CRET1, TMP0
5143 | zextw CARG1, CARG1
5144 | settp CARG1, TISNUM
5147 if (op == BC_FORI) {
5149 | selnez TMP2, TMP2, CRET1
5151 | movz TMP2, r0, CRET1
5153 | daddu PC, PC, TMP2
5154 } else if (op == BC_JFORI) {
5155 | daddu PC, PC, TMP2
5156 | lhu RD, -4+OFS_RD(PC)
5157 } else if (op == BC_IFORL) {
5159 | seleqz TMP2, TMP2, CRET1
5161 | movn TMP2, r0, CRET1
5163 | daddu PC, PC, TMP2
5166 | sd CARG1, FORL_IDX*8(RA)
5169 | sd CARG1, FORL_EXT*8(RA)
5171 if (op == BC_JFORI) {
5172 | beqz CRET1, =>BC_JLOOP
5174 } else if (op == BC_JFORL) {
5175 | beqz CRET1, =>BC_JLOOP
5182 | ldc1 f0, FORL_IDX*8(RA)
5183 | ldc1 f2, FORL_STOP*8(RA)
5184 | sltiu TMP0, CARG3, LJ_TISNUM
5185 | sltiu TMP1, CARG4, LJ_TISNUM
5186 | sltiu AT, CRET2, LJ_TISNUM
5187 | ld TMP3, FORL_STEP*8(RA)
5188 | and TMP0, TMP0, TMP1
5190 | beqz AT, ->vmeta_for
5191 |. slt TMP3, TMP3, r0
5194 | cmp.lt.d FTMP0, f0, f2
5195 | cmp.lt.d FTMP1, f2, f0
5196 | sel.d FTMP2, FTMP1, FTMP0
5198 |. dmfc1 CRET1, FTMP2
5206 |. movn CRET1, AT, TMP3
5209 | ldc1 f0, FORL_IDX*8(RA)
5210 | ldc1 f4, FORL_STEP*8(RA)
5211 | ldc1 f2, FORL_STOP*8(RA)
5212 | ld TMP3, FORL_STEP*8(RA)
5215 | slt TMP3, TMP3, r0
5217 | cmp.lt.d FTMP0, f0, f2
5218 | cmp.lt.d FTMP1, f2, f0
5219 | sel.d FTMP2, FTMP1, FTMP0
5220 | dmfc1 CRET1, FTMP2
5221 if (op == BC_IFORL) {
5222 | seleqz TMP2, TMP2, CRET1
5223 | daddu PC, PC, TMP2
5228 | slt TMP3, TMP3, r0
5233 | movn CRET1, AT, TMP3
5234 if (op == BC_IFORL) {
5235 | movn TMP2, r0, CRET1
5236 | daddu PC, PC, TMP2
5239 | sdc1 f0, FORL_IDX*8(RA)
5242 |. sdc1 f0, FORL_EXT*8(RA)
5246 | sltiu TMP0, CARG3, LJ_TISNUM
5247 | sltiu TMP1, CARG4, LJ_TISNUM
5248 | sltiu AT, CRET2, LJ_TISNUM
5249 | and TMP0, TMP0, TMP1
5251 | beqz AT, ->vmeta_for
5253 | bal ->vm_sfcmpolex
5254 |. lw TMP3, FORL_STEP*8+HI(RA)
5261 | ld CARG2, FORL_STOP*8(RA)
5263 if ( op == BC_JFORL ) {
5264 | lhu RD, -4+OFS_RD(PC)
5267 | bal ->vm_sfcmpolex
5268 |. lw TMP3, FORL_STEP*8+HI(RA)
5279 | // Fall through. Assumes BC_IITERL follows.
5287 | // RA = base*8, RD = target
5288 | daddu RA, BASE, RA
5290 | beq TMP1, TISNIL, >1 // Stop if iterator returned nil.
5292 if (op == BC_JITERL) {
5296 | branch_RD // Otherwise save control var + branch.
5304 | // RA = base*8, RD = target (loop extent)
5305 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5306 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5310 | // Fall through. Assumes BC_ILOOP follows.
5314 | // RA = base*8, RD = target (loop extent)
5320 | // RA = base*8 (ignored), RD = traceno*8
5321 | ld TMP1, DISPATCH_J(trace)(DISPATCH)
5323 | daddu TMP1, TMP1, RD
5324 | // Traces on MIPS don't store the trace number, so use 0.
5325 | sd AT, DISPATCH_GL(vmstate)(DISPATCH)
5326 | ld TRACE:TMP2, 0(TMP1)
5327 | sd BASE, DISPATCH_GL(jit_base)(DISPATCH)
5328 | ld TMP2, TRACE:TMP2->mcode
5329 | sd L, DISPATCH_GL(tmpbuf.L)(DISPATCH)
5331 |. daddiu JGL, DISPATCH, GG_DISP2G+32768
5336 | // RA = base*8 (only used by trace recorder), RD = target
5341 /* -- Function headers -------------------------------------------------- */
5347 case BC_FUNCV: /* NYI: compiled vararg functions. */
5348 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
5356 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5357 | ld TMP2, L->maxstack
5358 | lbu TMP1, -4+PC2PROTO(numparams)(PC)
5359 | ld KBASE, -4+PC2PROTO(k)(PC)
5361 | bnez AT, ->vm_growstack_l
5362 |. sll TMP1, TMP1, 3
5363 if (op != BC_JFUNCF) {
5367 | sltu AT, NARGS8:RC, TMP1 // Check for missing parameters.
5369 |. daddu AT, BASE, NARGS8:RC
5370 if (op == BC_JFUNCF) {
5371 | decode_RD8a RD, INS
5378 |3: // Clear missing parameters.
5381 |. addiu NARGS8:RC, NARGS8:RC, 8
5388 | NYI // NYI: compiled vararg functions
5389 break; /* NYI: compiled vararg functions. */
5392 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5394 | daddu TMP1, BASE, RC
5395 | ld TMP2, L->maxstack
5396 | settp LFUNC:RB, TMP0
5397 | daddu TMP0, RA, RC
5398 | sd LFUNC:RB, 0(TMP1) // Store (tagged) copy of LFUNC.
5399 | daddiu TMP2, TMP2, -8
5400 | daddiu TMP3, RC, 16+FRAME_VARG
5401 | sltu AT, TMP0, TMP2
5402 | ld KBASE, -4+PC2PROTO(k)(PC)
5403 | beqz AT, ->vm_growstack_l
5404 |. sd TMP3, 8(TMP1) // Store delta + FRAME_VARG.
5405 | lbu TMP2, -4+PC2PROTO(numparams)(PC)
5410 |. daddiu BASE, TMP1, 16
5413 | sltu AT, RA, RC // Less args than parameters?
5416 | selnez TMP0, TMP0, AT
5417 | seleqz TMP3, TISNIL, AT
5418 | or TMP0, TMP0, TMP3
5419 | seleqz TMP3, CARG1, AT
5420 | selnez CARG1, TISNIL, AT
5421 | or CARG1, CARG1, TMP3
5423 | movz TMP0, TISNIL, AT // Clear missing parameters.
5424 | movn CARG1, TISNIL, AT // Clear old fixarg slot (help the GC).
5426 | addiu TMP2, TMP2, -1
5428 | daddiu TMP1, TMP1, 8
5438 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
5439 if (op == BC_FUNCC) {
5440 | ld CFUNCADDR, CFUNC:RB->f
5442 | ld CFUNCADDR, DISPATCH_GL(wrapf)(DISPATCH)
5444 | daddu TMP1, RA, NARGS8:RC
5445 | ld TMP2, L->maxstack
5446 | daddu RC, BASE, NARGS8:RC
5448 | sltu AT, TMP2, TMP1
5451 if (op == BC_FUNCCW) {
5452 | ld CARG2, CFUNC:RB->f
5454 | bnez AT, ->vm_growstack_c // Need to grow stack.
5456 | jalr CFUNCADDR // (lua_State *L [, lua_CFunction f])
5458 | // Returns nresults.
5463 | ld PC, FRAME_PC(BASE) // Fetch PC of caller.
5464 | dsubu RA, TMP1, RD // RA = L->top - nresults*8
5465 | sd L, DISPATCH_GL(cur_L)(DISPATCH)
5470 /* ---------------------------------------------------------------------- */
5473 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5479 static int build_backend(BuildCtx *ctx)
5483 dasm_growpc(Dst, BC__MAX);
5485 build_subroutines(ctx);
5488 for (op = 0; op < BC__MAX; op++)
5489 build_ins(ctx, (BCOp)op, op);
5494 /* Emit pseudo frame-info for all assembler functions. */
5495 static void emit_asm_debug(BuildCtx *ctx)
5497 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5499 switch (ctx->mode) {
5501 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5504 "\t.4byte .LECIE0-.LSCIE0\n"
5506 "\t.4byte 0xffffffff\n"
5512 "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n"
5517 "\t.4byte .LEFDE0-.LASFDE0\n"
5519 "\t.4byte .Lframe0\n"
5520 "\t.8byte .Lbegin\n"
5522 "\t.byte 0xe\n\t.uleb128 %d\n"
5523 "\t.byte 0x9f\n\t.sleb128 2*5\n"
5524 "\t.byte 0x9e\n\t.sleb128 2*6\n",
5525 fcofs, CFRAME_SIZE);
5526 for (i = 23; i >= 16; i--)
5527 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2*(30-i));
5529 for (i = 31; i >= 24; i--)
5530 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 2*(46-i));
5538 "\t.4byte .LEFDE1-.LASFDE1\n"
5540 "\t.4byte .Lframe0\n"
5541 "\t.4byte lj_vm_ffi_call\n"
5543 "\t.byte 0x9f\n\t.uleb128 2*1\n"
5544 "\t.byte 0x90\n\t.uleb128 2*2\n"
5545 "\t.byte 0xd\n\t.uleb128 0x10\n"
5547 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);