1 |// Low-level VM code for PowerPC/e500 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
10 |.globalnames globnames
11 |.externnames extnames
13 |// Note: The ragged indentation of the instructions is intentional.
14 |// The starting columns indicate data dependencies.
16 |//-----------------------------------------------------------------------
18 |// Fixed register assignments for the interpreter.
19 |// Don't use: r1 = sp, r2 and r13 = reserved and/or small data area ptr
21 |// The following must be C callee-save (but BASE is often refetched).
22 |.define BASE, r14 // Base of current Lua stack frame.
23 |.define KBASE, r15 // Constants of current Lua function.
24 |.define PC, r16 // Next PC.
25 |.define DISPATCH, r17 // Opcode dispatch table.
26 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
27 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
29 |// Constants for vectorized type-comparisons (hi+low GPR). C callee-save.
36 |.define ZERO, TOBIT // Zero in lo word.
38 |// The following temporaries are not saved across C calls, except for RA.
39 |.define RA, r20 // Callee-save.
43 |.define INS, r7 // Overlaps CARG5.
48 |.define TMP3, r6 // Overlaps CARG4.
50 |// Saved temporaries.
53 |// Calling conventions.
57 |.define CARG4, r6 // Overlaps TMP3.
58 |.define CARG5, r7 // Overlaps INS.
63 |// Stack layout while in interpreter. Must match with lj_frame.h.
64 |.define SAVE_LR, 188(sp)
65 |.define CFRAME_SPACE, 184 // Delta for sp.
66 |// Back chain for sp: 184(sp) <-- sp entering interpreter
67 |.define SAVE_r31, 176(sp) // 64 bit register saves.
68 |.define SAVE_r30, 168(sp)
69 |.define SAVE_r29, 160(sp)
70 |.define SAVE_r28, 152(sp)
71 |.define SAVE_r27, 144(sp)
72 |.define SAVE_r26, 136(sp)
73 |.define SAVE_r25, 128(sp)
74 |.define SAVE_r24, 120(sp)
75 |.define SAVE_r23, 112(sp)
76 |.define SAVE_r22, 104(sp)
77 |.define SAVE_r21, 96(sp)
78 |.define SAVE_r20, 88(sp)
79 |.define SAVE_r19, 80(sp)
80 |.define SAVE_r18, 72(sp)
81 |.define SAVE_r17, 64(sp)
82 |.define SAVE_r16, 56(sp)
83 |.define SAVE_r15, 48(sp)
84 |.define SAVE_r14, 40(sp)
85 |.define SAVE_CR, 36(sp)
86 |.define UNUSED1, 32(sp)
87 |.define SAVE_ERRF, 28(sp) // 32 bit C frame info.
88 |.define SAVE_NRES, 24(sp)
89 |.define SAVE_CFRAME, 20(sp)
90 |.define SAVE_L, 16(sp)
91 |.define SAVE_PC, 12(sp)
92 |.define SAVE_MULTRES, 8(sp)
93 |// Next frame lr: 4(sp)
94 |// Back chain for sp: 0(sp) <-- sp while in interpreter
96 |.macro save_, reg; evstdd reg, SAVE_..reg; .endmacro
97 |.macro rest_, reg; evldd reg, SAVE_..reg; .endmacro
100 | stwu sp, -CFRAME_SPACE(sp)
101 | save_ r14; save_ r15; save_ r16; save_ r17; save_ r18; save_ r19
103 | save_ r20; save_ r21; save_ r22; save_ r23; save_ r24; save_ r25
104 | stw r0, SAVE_LR; stw r12, SAVE_CR
105 | save_ r26; save_ r27; save_ r28; save_ r29; save_ r30; save_ r31
109 | lwz r0, SAVE_LR; lwz r12, SAVE_CR
110 | rest_ r14; rest_ r15; rest_ r16; rest_ r17; rest_ r18; rest_ r19
111 | mtlr r0; mtcrf 0x38, r12
112 | rest_ r20; rest_ r21; rest_ r22; rest_ r23; rest_ r24; rest_ r25
113 | rest_ r26; rest_ r27; rest_ r28; rest_ r29; rest_ r30; rest_ r31
114 | addi sp, sp, CFRAME_SPACE
117 |// Type definitions. Some of these are only used for documentation.
118 |.type L, lua_State, LREG
119 |.type GL, global_State
120 |.type TVALUE, TValue
124 |.type LFUNC, GCfuncL
125 |.type CFUNC, GCfuncC
126 |.type PROTO, GCproto
127 |.type UPVAL, GCupval
130 |.type TRACE, GCtrace
132 |//-----------------------------------------------------------------------
134 |// These basic macros should really be part of DynASM.
135 |.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro
136 |.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro
137 |.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro
138 |.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro
139 |.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro
141 |// Trap for not-yet-implemented parts.
142 |.macro NYI; tw 4, sp, sp; .endmacro
144 |//-----------------------------------------------------------------------
146 |// Access to frame relative to BASE.
147 |.define FRAME_PC, -8
148 |.define FRAME_FUNC, -4
150 |// Instruction decode.
151 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
152 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
153 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
154 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
155 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
157 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
158 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
160 |// Instruction fetch.
165 |// Instruction decode+dispatch.
167 | decode_OP4 TMP1, INS
170 | lwzx TMP0, DISPATCH, TMP1
181 |// Instruction footer.
183 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
184 | .define ins_next, ins_NEXT
185 | .define ins_next_, ins_NEXT
186 | .define ins_next1, ins_NEXT1
187 | .define ins_next2, ins_NEXT2
189 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
190 | // Affects only certain kinds of benchmarks (and only with -j off).
205 |// Call decode and dispatch.
207 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
208 | lwz PC, LFUNC:RB->pc
211 | decode_OP4 TMP1, INS
213 | lwzx TMP0, DISPATCH, TMP1
220 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
221 | stw PC, FRAME_PC(BASE)
225 |//-----------------------------------------------------------------------
227 |// Macros to test operand types.
228 |.macro checknum, reg; evcmpltu reg, TISNUM; .endmacro
229 |.macro checkstr, reg; evcmpeq reg, TISSTR; .endmacro
230 |.macro checktab, reg; evcmpeq reg, TISTAB; .endmacro
231 |.macro checkfunc, reg; evcmpeq reg, TISFUNC; .endmacro
232 |.macro checknil, reg; evcmpeq reg, TISNIL; .endmacro
233 |.macro checkok, label; blt label; .endmacro
234 |.macro checkfail, label; bge label; .endmacro
235 |.macro checkanyfail, label; bns label; .endmacro
236 |.macro checkallok, label; bso label; .endmacro
241 | addis PC, PC, -(BCBIAS_J*4 >> 16)
244 |// Assumes DISPATCH is relative to GL.
245 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
246 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
248 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
258 |// Set current VM state. Uses TMP0.
259 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
260 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
262 |// Move table write barrier back. Overwrites mark and tmp.
263 |.macro barrierback, tab, mark, tmp
264 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
265 | // Assumes LJ_GC_BLACK is 0x04.
266 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
267 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
268 | stb mark, tab->marked
269 | stw tmp, tab->gclist
272 |//-----------------------------------------------------------------------
274 /* Generate subroutines used by opcodes and other parts of the VM. */
275 /* The .code_sub section should be last to help static branch prediction. */
276 static void build_subroutines(BuildCtx *ctx)
280 |//-----------------------------------------------------------------------
281 |//-- Return handling ----------------------------------------------------
282 |//-----------------------------------------------------------------------
285 | // See vm_return. Also: TMP2 = previous base.
286 | andi. TMP0, PC, FRAME_P
287 | evsplati TMP1, LJ_TTRUE
288 | beq ->cont_dispatch
290 | // Return from pcall or xpcall fast func.
291 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
292 | mr BASE, TMP2 // Restore caller base.
293 | // Prepending may overwrite the pcall frame, so do it at the end.
294 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
297 | addi RD, RD, 8 // RD = (nresults+1)*8.
298 | andi. TMP0, PC, FRAME_TYPE
300 | li CRET1, LUA_YIELD
301 | beq cr1, ->vm_unwind_c_eh
303 | beq ->BC_RET_Z // Handle regular return to Lua.
306 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
307 | // TMP0 = PC & FRAME_TYPE
308 | cmpwi TMP0, FRAME_C
309 | rlwinm TMP2, PC, 0, 0, 28
311 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
314 | addic. TMP1, RD, -8
316 | lwz TMP2, SAVE_NRES
322 | addic. TMP1, TMP1, -8
325 | evstdd TMP0, 0(BASE)
330 | cmpw TMP2, RD // More/less results wanted?
333 | stw BASE, L->top // Store new top.
336 | lwz TMP0, SAVE_CFRAME // Restore previous C frame.
337 | li CRET1, 0 // Ok return status for vm_pcall.
338 | stw TMP0, L->cframe
345 | ble >7 // Less results wanted?
346 | // More results wanted. Check stack size and fill up results with nil.
347 | lwz TMP1, L->maxstack
350 | evstdd TISNIL, 0(BASE)
355 |7: // Less results wanted.
357 | cmpwi TMP2, 0 // LUA_MULTRET+1 case?
358 | sub TMP0, BASE, TMP0 // Subtract the difference.
359 | iseleq BASE, BASE, TMP0 // Either keep top or shrink it.
362 |8: // Corner case: need to grow stack for filling up results.
363 | // This can happen if:
364 | // - A C function grows the stack (a lot).
365 | // - The GC shrinks the stack in between.
366 | // - A return back from a lua_call() with (high) nresults adjustment.
367 | stw BASE, L->top // Save current top held in BASE (yes).
371 | bl extern lj_state_growstack // (lua_State *L, int n)
372 | lwz TMP2, SAVE_NRES
375 | lwz BASE, L->top // Need the (realloced) L->top in BASE.
378 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
379 | // (void *cframe, int errcode)
382 |->vm_unwind_c_eh: // Landing pad for external unwinder.
384 | li TMP0, ~LJ_VMST_C
385 | lwz GL:TMP1, L->glref
386 | stw TMP0, GL:TMP1->vmstate
389 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
391 | rlwinm sp, CARG1, 0, 0, 29
392 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
394 | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
395 | evsplati TISFUNC, LJ_TFUNC
397 | evsplati TISTAB, LJ_TTAB
400 | evmergelo TOBIT, TOBIT, TMP0
401 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
402 | evsplati TISSTR, LJ_TSTR
404 | evsplati TISNIL, LJ_TNIL
406 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
407 | la RA, -8(BASE) // Results start at BASE-8.
408 | addi DISPATCH, DISPATCH, GG_G2DISP
409 | stw TMP1, 0(RA) // Prepend false to error message.
410 | li RD, 16 // 2 results: false + error message.
414 |//-----------------------------------------------------------------------
415 |//-- Grow stack for calls -----------------------------------------------
416 |//-----------------------------------------------------------------------
418 |->vm_growstack_c: // Grow stack for C function.
419 | li CARG2, LUA_MINSTACK
422 |->vm_growstack_l: // Grow stack for Lua function.
423 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
427 | addi PC, PC, 4 // Must point after first instruction.
431 | // L->base = new base, L->top = top
434 | bl extern lj_state_growstack // (lua_State *L, int n)
437 | lwz LFUNC:RB, FRAME_FUNC(BASE)
439 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
440 | ins_callt // Just retry the call.
442 |//-----------------------------------------------------------------------
443 |//-- Entry points into the assembler VM ---------------------------------
444 |//-----------------------------------------------------------------------
446 |->vm_resume: // Setup C frame and resume thread.
447 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
450 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
452 | lbz TMP1, L->status
455 | addi TMP0, sp, CFRAME_RESUME
456 | addi DISPATCH, DISPATCH, GG_G2DISP
457 | stw CARG3, SAVE_NRES
459 | stw CARG3, SAVE_ERRF
460 | stw TMP0, L->cframe
461 | stw CARG3, SAVE_CFRAME
462 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
465 | // Resume after yield (like a return).
468 | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
470 | evsplati TISFUNC, LJ_TFUNC
472 | evsplati TISTAB, LJ_TTAB
473 | lwz PC, FRAME_PC(BASE)
475 | evsplati TISSTR, LJ_TSTR
477 | evmergelo TOBIT, TOBIT, TMP2
478 | stb CARG3, L->status
479 | andi. TMP0, PC, FRAME_TYPE
482 | evsplati TISNIL, LJ_TNIL
488 |->vm_pcall: // Setup protected C frame and enter VM.
489 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
492 | stw CARG4, SAVE_ERRF
495 |->vm_call: // Setup C frame and enter VM.
496 | // (lua_State *L, TValue *base, int nres1)
500 |1: // Entry point for vm_pcall above (PC = ftype).
501 | lwz TMP1, L:CARG1->cframe
502 | stw CARG3, SAVE_NRES
506 | stw sp, L->cframe // Add our C frame to cframe chain.
507 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
508 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
509 | stw TMP1, SAVE_CFRAME
510 | addi DISPATCH, DISPATCH, GG_G2DISP
512 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
513 | lwz TMP2, L->base // TMP2 = old base (used in vmeta_call).
514 | evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
516 | evsplati TISFUNC, LJ_TFUNC
518 | evsplati TISTAB, LJ_TTAB
521 | sub PC, PC, TMP2 // PC = frame delta + frame type
522 | evsplati TISSTR, LJ_TSTR
523 | sub NARGS8:RC, TMP1, BASE
524 | evmergelo TOBIT, TOBIT, TMP0
526 | evsplati TISNIL, LJ_TNIL
530 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
532 | evlddx LFUNC:RB, BASE, TMP0
534 | checkfail ->vmeta_call
536 |->vm_call_dispatch_f:
538 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
540 |->vm_cpcall: // Setup protected C frame, call C.
541 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
544 | lwz TMP0, L:CARG1->stack
547 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
548 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
549 | lwz TMP1, L->cframe
550 | stw sp, L->cframe // Add our C frame to cframe chain.
552 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
553 | stw TMP2, SAVE_ERRF // No error function.
554 | stw TMP1, SAVE_CFRAME
556 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
558 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
560 | addi DISPATCH, DISPATCH, GG_G2DISP
561 | bne <3 // Else continue with the call.
562 | b ->vm_leave_cp // No base? Just remove C frame.
564 |//-----------------------------------------------------------------------
565 |//-- Metamethod handling ------------------------------------------------
566 |//-----------------------------------------------------------------------
568 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
569 |// stack, so BASE doesn't need to be reloaded across these calls.
571 |//-- Continuation dispatch ----------------------------------------------
574 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
575 | lwz TMP0, -12(BASE) // Continuation.
577 | mr BASE, TMP2 // Restore caller BASE.
578 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
580 | lwz PC, -16(RB) // Restore PC from [cont|PC].
583 | lwz TMP1, LFUNC:TMP1->pc
584 | evstddx TISNIL, RA, TMP2 // Ensure one valid arg.
585 | lwz KBASE, PC2PROTO(k)(TMP1)
586 | // BASE = base, RA = resultptr, RB = meta base
588 | bctr // Jump to continuation.
590 |1: // Tail call from C function.
595 |->cont_cat: // RA = resultptr, RB = meta base
598 | decode_RB8 SAVE0, INS
600 | add TMP1, BASE, SAVE0
603 | sub CARG3, CARG2, TMP1
605 | evstdd TMP0, 0(CARG2)
607 | evstddx TMP0, BASE, RA
610 |//-- Table indexing metamethods -----------------------------------------
613 | evmergelo STR:RC, TISSTR, STR:RC
614 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
616 | evstdd STR:RC, 0(CARG3)
617 | add CARG2, BASE, RB
621 | evmergelo TAB:RB, TISTAB, TAB:RB
622 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
623 | evmergelo STR:RC, TISSTR, STR:RC
624 | evstdd TAB:RB, 0(CARG2)
625 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
626 | evstdd STR:RC, 0(CARG3)
629 |->vmeta_tgetb: // TMP0 = index
632 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
633 | add CARG2, BASE, RB
634 | evstdd TMP0, 0(CARG3)
640 | add CARG2, BASE, RB
641 | add CARG3, BASE, RC
646 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
647 | // Returns TValue * (finished) or NULL (metamethod).
650 | evldd TMP0, 0(CRET1)
651 | evstddx TMP0, BASE, RA
654 |3: // Call __index metamethod.
655 | // BASE = base, L->top = new base, stack = cont/func/t/k
656 | subfic TMP1, BASE, FRAME_CONT
658 | stw PC, -16(BASE) // [cont|PC]
660 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
661 | li NARGS8:RC, 16 // 2 args for func(t, k).
662 | b ->vm_call_dispatch_f
664 |//-----------------------------------------------------------------------
667 | evmergelo STR:RC, TISSTR, STR:RC
668 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
670 | evstdd STR:RC, 0(CARG3)
671 | add CARG2, BASE, RB
675 | evmergelo TAB:RB, TISTAB, TAB:RB
676 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
677 | evmergelo STR:RC, TISSTR, STR:RC
678 | evstdd TAB:RB, 0(CARG2)
679 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
680 | evstdd STR:RC, 0(CARG3)
683 |->vmeta_tsetb: // TMP0 = index
686 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
687 | add CARG2, BASE, RB
688 | evstdd TMP0, 0(CARG3)
694 | add CARG2, BASE, RB
695 | add CARG3, BASE, RC
700 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
701 | // Returns TValue * (finished) or NULL (metamethod).
703 | evlddx TMP0, BASE, RA
705 | // NOBARRIER: lj_meta_tset ensures the table is not black.
706 | evstdd TMP0, 0(CRET1)
709 |3: // Call __newindex metamethod.
710 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
711 | subfic TMP1, BASE, FRAME_CONT
713 | stw PC, -16(BASE) // [cont|PC]
715 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
716 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
717 | evstdd TMP0, 16(BASE) // Copy value to third argument.
718 | b ->vm_call_dispatch_f
720 |//-- Comparison metamethods ---------------------------------------------
725 | add CARG2, BASE, RA
727 | add CARG3, BASE, RD
729 | decode_OP1 CARG4, INS
730 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
731 | // Returns 0/1 or TValue * (metamethod).
738 | decode_RD4 TMP2, INS
739 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
740 | add TMP2, TMP2, TMP3
741 | isellt PC, PC, TMP2
745 |->cont_ra: // RA = resultptr
748 | decode_RA8 TMP1, INS
749 | evstddx TMP0, BASE, TMP1
752 |->cont_condt: // RA = resultptr
755 | cmplw TMP1, TMP0 // Branch if result is true.
758 |->cont_condf: // RA = resultptr
761 | cmplw TMP0, TMP1 // Branch if result is false.
765 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
770 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
771 | // Returns 0/1 or TValue * (metamethod).
774 |//-- Arithmetic metamethods ---------------------------------------------
777 | add CARG3, BASE, RB
778 | add CARG4, KBASE, RC
782 | add CARG3, KBASE, RC
783 | add CARG4, BASE, RB
787 | add CARG3, BASE, RD
792 | add CARG3, BASE, RB
793 | add CARG4, BASE, RC
795 | add CARG2, BASE, RA
799 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
800 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
801 | // Returns NULL (finished) or TValue * (metamethod).
805 | // Call metamethod for binary op.
807 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
808 | sub TMP1, CRET1, BASE
809 | stw PC, -16(CRET1) // [cont|PC]
811 | addi PC, TMP1, FRAME_CONT
813 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
814 | b ->vm_call_dispatch
820 | add CARG2, BASE, RD
824 | bl extern lj_meta_len // (lua_State *L, TValue *o)
825 | // Returns NULL (retry) or TValue * (metamethod base).
828 | bne ->vmeta_binop // Binop call for compatibility.
832 | b ->vmeta_binop // Binop call for compatibility.
835 |//-- Call metamethod ----------------------------------------------------
837 |->vmeta_call: // Resolve and call __call metamethod.
838 | // TMP2 = old base, BASE = new base, RC = nargs*8
840 | stw TMP2, L->base // This is the callers base!
841 | subi CARG2, BASE, 8
843 | add CARG3, BASE, RC
844 | mr SAVE0, NARGS8:RC
845 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
846 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
847 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
850 |->vmeta_callt: // Resolve __call for BC_CALLT.
851 | // BASE = old base, RA = new base, RC = nargs*8
857 | mr SAVE0, NARGS8:RC
858 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
859 | lwz TMP1, FRAME_PC(BASE)
860 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
861 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
864 |//-- Argument coercion for 'for' statement ------------------------------
872 | bl extern lj_meta_for // (lua_State *L, TValue *base)
874 | decode_OP1 TMP0, SAVE0
876 | decode_RA8 RA, SAVE0
878 | cmpwi TMP0, BC_JFORI
880 | decode_RD8 RD, SAVE0
886 |//-----------------------------------------------------------------------
887 |//-- Fast functions -----------------------------------------------------
888 |//-----------------------------------------------------------------------
894 |.macro .ffunc_1, name
896 | cmplwi NARGS8:RC, 8
897 | evldd CARG1, 0(BASE)
901 |.macro .ffunc_2, name
903 | cmplwi NARGS8:RC, 16
904 | evldd CARG1, 0(BASE)
905 | evldd CARG2, 8(BASE)
909 |.macro .ffunc_n, name
912 | checkfail ->fff_fallback
915 |.macro .ffunc_nn, name
917 | evmergehi TMP0, CARG1, CARG2
919 | checkanyfail ->fff_fallback
922 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
924 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
925 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
930 |//-- Base library: checks -----------------------------------------------
933 | cmplwi NARGS8:RC, 8
934 | evldd TMP0, 0(BASE)
936 | evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE.
938 | evcmpltu cr1, TMP0, TMP1
939 | lwz PC, FRAME_PC(BASE)
940 | bge cr1, ->fff_fallback
942 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
943 | beq ->fff_res // Done if exactly 1 argument.
948 | evlddx TMP0, BASE, TMP1
949 | evstddx TMP0, RA, TMP1
955 | cmplwi NARGS8:RC, 8
959 | cmplw CARG1, TISNUM
961 | isellt TMP1, TMP2, TMP1
963 | la TMP2, CFUNC:RB->upvalue
964 | evlddx STR:CRET1, TMP2, TMP1
967 |//-- Base library: getters and setters ---------------------------------
969 |.ffunc_1 getmetatable
971 | evmergehi TMP1, CARG1, CARG1
973 |1: // Field metatable must be at same offset for GCtab and GCudata!
974 | lwz TAB:RB, TAB:CARG1->metatable
978 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
980 | lwz TMP0, TAB:RB->hmask
981 | evmergelo CRET1, TISTAB, TAB:RB // Use metatable as default result.
982 | lwz TMP1, STR:RC->hash
983 | lwz NODE:TMP2, TAB:RB->node
984 | evmergelo STR:RC, TISSTR, STR:RC
985 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
988 | sub TMP1, TMP0, TMP1
989 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
990 |3: // Rearranged logic, because we expect _not_ to find the key.
991 | evldd TMP0, NODE:TMP2->key
992 | evldd TMP1, NODE:TMP2->val
993 | evcmpeq TMP0, STR:RC
994 | lwz NODE:TMP2, NODE:TMP2->next
996 | cmplwi NODE:TMP2, 0
997 | beq ->fff_restv // Not found, keep default result.
1001 | checkok ->fff_restv // Ditto for nil value.
1002 | evmr CRET1, TMP1 // Return value of mt.__metatable.
1006 | cmpwi TMP1, LJ_TUDATA
1010 | slwi TMP1, TMP1, 2
1011 | li TMP2, 4*~LJ_TNUMX
1012 | isellt TMP1, TMP2, TMP1
1013 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1014 | lwzx TAB:RB, TMP2, TMP1
1017 |.ffunc_2 setmetatable
1018 | // Fast path: no mt for table yet and not clearing the mt.
1019 | evmergehi TMP0, TAB:CARG1, TAB:CARG2
1021 | checkanyfail ->fff_fallback
1022 | lwz TAB:TMP1, TAB:CARG1->metatable
1023 | cmplwi TAB:TMP1, 0
1024 | lbz TMP3, TAB:CARG1->marked
1025 | bne ->fff_fallback
1026 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1027 | stw TAB:CARG2, TAB:CARG1->metatable
1029 | barrierback TAB:CARG1, TMP3, TMP0
1033 | cmplwi NARGS8:RC, 16
1034 | evldd CARG2, 0(BASE)
1035 | blt ->fff_fallback
1038 | checkfail ->fff_fallback
1040 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1041 | // Returns cTValue *.
1042 | evldd CRET1, 0(CRET1)
1045 |//-- Base library: conversions ------------------------------------------
1048 | // Only handles the number case inline (without a base argument).
1049 | cmplwi NARGS8:RC, 8
1050 | evldd CARG1, 0(BASE)
1051 | bne ->fff_fallback // Exactly one argument.
1053 | checkok ->fff_restv
1057 | // Only handles the string or number case inline.
1059 | // A __tostring method in the string base metatable is ignored.
1060 | checkok ->fff_restv // String key?
1061 | // Handle numbers inline, unless a number base metatable is present.
1062 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1064 | cmplwi cr1, TMP0, 0
1065 | stw BASE, L->base // Add frame since C call can throw.
1066 | crand 4*cr0+eq, 4*cr0+lt, 4*cr1+eq
1067 | stw PC, SAVE_PC // Redundant (but a defined value).
1068 | bne ->fff_fallback
1072 | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np)
1073 | // Returns GCstr *.
1074 | evmergelo STR:CRET1, TISSTR, STR:CRET1
1077 |//-- Base library: iterators -------------------------------------------
1080 | cmplwi NARGS8:RC, 8
1081 | evldd CARG2, 0(BASE)
1082 | blt ->fff_fallback
1083 | evstddx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1084 | checktab TAB:CARG2
1085 | lwz PC, FRAME_PC(BASE)
1086 | checkfail ->fff_fallback
1087 | stw BASE, L->base // Add frame since C call can throw.
1089 | stw BASE, L->top // Dummy frame length is ok.
1092 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1093 | // Returns 0 at end of traversal.
1095 | evmr CRET1, TISNIL
1096 | beq ->fff_restv // End of traversal: return nil.
1097 | evldd TMP0, 8(BASE) // Copy key and value to results.
1099 | evldd TMP1, 16(BASE)
1100 | evstdd TMP0, 0(RA)
1102 | evstdd TMP1, 8(RA)
1106 | checktab TAB:CARG1
1107 | lwz PC, FRAME_PC(BASE)
1108 | checkfail ->fff_fallback
1110 | lwz TAB:TMP2, TAB:CARG1->metatable
1111 | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0]
1112 | cmplwi TAB:TMP2, 0
1114 | bne ->fff_fallback
1116 | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0]
1119 | evstdd TISNIL, 8(BASE)
1121 | evstdd CFUNC:TMP0, 0(RA)
1124 |.ffunc_2 ipairs_aux
1125 | checktab TAB:CARG1
1126 | lwz PC, FRAME_PC(BASE)
1127 | checkfail ->fff_fallback
1130 | checkfail ->fff_fallback
1131 | efdctsi TMP2, CARG2
1132 | lwz TMP0, TAB:CARG1->asize
1133 | evmergelo TMP3, TMP3, ZERO
1134 | lwz TMP1, TAB:CARG1->array
1135 | efdadd CARG2, CARG2, TMP3
1136 | addi TMP2, TMP2, 1
1139 | slwi TMP3, TMP2, 3
1140 | evstdd CARG2, 0(RA)
1141 | ble >2 // Not in array part?
1142 | evlddx TMP1, TMP1, TMP3
1146 | checkok ->fff_res // End of iteration, return 0 results.
1148 | evstdd TMP1, 8(RA)
1150 |2: // Check for empty hash part first. Otherwise call C function.
1151 | lwz TMP0, TAB:CARG1->hmask
1156 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1157 | // Returns cTValue * or NULL.
1161 | evldd TMP1, 0(CRET1)
1165 | checktab TAB:CARG1
1166 | lwz PC, FRAME_PC(BASE)
1167 | checkfail ->fff_fallback
1169 | lwz TAB:TMP2, TAB:CARG1->metatable
1170 | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0]
1171 | cmplwi TAB:TMP2, 0
1173 | bne ->fff_fallback
1175 | evldd CFUNC:TMP0, CFUNC:RB->upvalue[0]
1180 | evstdd TMP1, 8(BASE)
1181 | evstdd CFUNC:TMP0, 0(RA)
1184 |//-- Base library: catch errors ----------------------------------------
1187 | cmplwi NARGS8:RC, 8
1188 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1189 | blt ->fff_fallback
1192 | // Remember active hook before pcall.
1193 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1194 | subi NARGS8:RC, NARGS8:RC, 8
1195 | addi PC, TMP3, 8+FRAME_PCALL
1196 | b ->vm_call_dispatch
1199 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1201 | checkfunc CARG2 // Traceback must be a function.
1202 | checkfail ->fff_fallback
1204 | // Remember active hook before pcall.
1205 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1206 | evstdd CARG2, 0(TMP2) // Swap function and traceback.
1207 | subi NARGS8:RC, NARGS8:RC, 16
1208 | evstdd CARG1, 8(TMP2)
1209 | addi PC, TMP3, 16+FRAME_PCALL
1210 | b ->vm_call_dispatch
1212 |//-- Coroutine library --------------------------------------------------
1214 |.macro coroutine_resume_wrap, resume
1216 |.ffunc_1 coroutine_resume
1217 | evmergehi TMP0, L:CARG1, L:CARG1
1219 |.ffunc coroutine_wrap_aux
1220 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1223 | cmpwi TMP0, LJ_TTHREAD
1224 | bne ->fff_fallback
1226 | lbz TMP0, L:CARG1->status
1227 | lwz TMP1, L:CARG1->cframe
1228 | lwz CARG2, L:CARG1->top
1229 | cmplwi cr0, TMP0, LUA_YIELD
1230 | lwz TMP2, L:CARG1->base
1231 | cmplwi cr1, TMP1, 0
1232 | lwz TMP0, L:CARG1->maxstack
1233 | cmplw cr7, CARG2, TMP2
1234 | lwz PC, FRAME_PC(BASE)
1235 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1236 | add TMP2, CARG2, NARGS8:RC
1237 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1238 | cmplw cr1, TMP2, TMP0
1239 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1241 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1243 | blt cr6, ->fff_fallback
1246 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1247 | subi NARGS8:RC, NARGS8:RC, 8
1248 | subi TMP2, TMP2, 8
1250 | stw TMP2, L:CARG1->top
1253 |2: // Move args to coroutine.
1254 | cmpw TMP1, NARGS8:RC
1255 | evlddx TMP0, BASE, TMP1
1257 | evstddx TMP0, CARG2, TMP1
1258 | addi TMP1, TMP1, 8
1262 | mr L:SAVE0, L:CARG1
1264 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1265 | // Returns thread status.
1267 | lwz TMP2, L:SAVE0->base
1268 | cmplwi CRET1, LUA_YIELD
1269 | lwz TMP3, L:SAVE0->top
1274 | sub RD, TMP3, TMP2
1275 | lwz TMP0, L->maxstack
1277 | add TMP1, BASE, RD
1278 | beq >6 // No results?
1281 | bgt >9 // Need to grow stack?
1284 | stw TMP2, L:SAVE0->top // Clear coroutine stack.
1285 |5: // Move results from coroutine.
1287 | evlddx TMP0, TMP2, TMP1
1288 | evstddx TMP0, BASE, TMP1
1289 | addi TMP1, TMP1, 8
1292 | andi. TMP0, PC, FRAME_TYPE
1296 | stw TMP1, -8(BASE) // Prepend true to results.
1308 |8: // Coroutine returned with error (at co->top-1).
1310 | andi. TMP0, PC, FRAME_TYPE
1312 | li TMP1, LJ_TFALSE
1313 | evldd TMP0, 0(TMP3)
1314 | stw TMP3, L:SAVE0->top // Remove error from coroutine stack.
1316 | stw TMP1, -8(BASE) // Prepend false to results.
1318 | evstdd TMP0, 0(BASE) // Copy error message.
1323 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1326 |9: // Handle stack expansion on return from yield.
1329 | bl extern lj_state_growstack // (lua_State *L, int n)
1334 | coroutine_resume_wrap 1 // coroutine.resume
1335 | coroutine_resume_wrap 0 // coroutine.wrap
1337 |.ffunc coroutine_yield
1338 | lwz TMP0, L->cframe
1339 | add TMP1, BASE, NARGS8:RC
1341 | andi. TMP0, TMP0, CFRAME_RESUME
1343 | li CRET1, LUA_YIELD
1344 | beq ->fff_fallback
1345 | stw ZERO, L->cframe
1346 | stb CRET1, L->status
1349 |//-- Math library -------------------------------------------------------
1352 | efdabs CRET1, CARG1
1356 | // CRET1 = TValue result.
1357 | lwz PC, FRAME_PC(BASE)
1359 | evstdd CRET1, 0(RA)
1361 | // RA = results, PC = return.
1364 | // RA = results, RD = (nresults+1)*8, PC = return.
1365 | andi. TMP0, PC, FRAME_TYPE
1369 | decode_RB8 RB, INS
1371 | cmplw RB, RD // More results expected?
1372 | decode_RA8 TMP0, INS
1375 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1376 | sub BASE, RA, TMP0
1379 |6: // Fill up results with nil.
1382 | evstddx TISNIL, RA, TMP1
1385 |.macro math_extern, func
1386 | .ffunc math_ .. func
1387 | cmplwi NARGS8:RC, 8
1388 | evldd CARG2, 0(BASE)
1389 | blt ->fff_fallback
1391 | evmergehi CARG1, CARG2, CARG2
1392 | checkfail ->fff_fallback
1394 | evmergelo CRET1, CRET1, CRET2
1398 |.macro math_extern2, func
1399 | .ffunc math_ .. func
1400 | cmplwi NARGS8:RC, 16
1401 | evldd CARG2, 0(BASE)
1402 | evldd CARG4, 8(BASE)
1403 | blt ->fff_fallback
1404 | evmergehi CARG1, CARG4, CARG2
1406 | evmergehi CARG3, CARG4, CARG4
1407 | checkanyfail ->fff_fallback
1409 | evmergelo CRET1, CRET1, CRET2
1413 |.macro math_round, func
1414 | .ffunc math_ .. func
1415 | cmplwi NARGS8:RC, 8
1416 | evldd CARG2, 0(BASE)
1417 | blt ->fff_fallback
1419 | evmergehi CARG1, CARG2, CARG2
1420 | checkfail ->fff_fallback
1421 | lwz PC, FRAME_PC(BASE)
1422 | bl ->vm_..func.._hilo;
1424 | evstdd CRET2, 0(RA)
1434 | cmplwi NARGS8:RC, 8
1435 | evldd CARG2, 0(BASE)
1436 | bne ->fff_fallback // Need exactly 1 argument.
1438 | evmergehi CARG1, CARG2, CARG2
1439 | checkfail ->fff_fallback
1441 | evmergelo CRET1, CRET1, CRET2
1456 | math_extern2 atan2
1461 | evldd CARG2, CFUNC:RB->upvalue[0]
1462 | efdmul CRET1, CARG1, CARG2
1466 | cmplwi NARGS8:RC, 16
1467 | evldd CARG2, 0(BASE)
1468 | evldd CARG4, 8(BASE)
1469 | blt ->fff_fallback
1470 | evmergehi CARG1, CARG4, CARG2
1472 | checkanyfail ->fff_fallback
1473 | efdctsi CARG3, CARG4
1475 | evmergelo CRET1, CRET1, CRET2
1479 | cmplwi NARGS8:RC, 8
1480 | evldd CARG2, 0(BASE)
1481 | blt ->fff_fallback
1483 | evmergehi CARG1, CARG2, CARG2
1484 | checkfail ->fff_fallback
1485 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
1486 | lwz PC, FRAME_PC(BASE)
1488 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1489 | evmergelo CRET1, CRET1, CRET2
1490 | efdcfsi CRET2, TMP1
1492 | evstdd CRET1, 0(RA)
1494 | evstdd CRET2, 8(RA)
1498 | cmplwi NARGS8:RC, 8
1499 | evldd CARG2, 0(BASE)
1500 | blt ->fff_fallback
1502 | evmergehi CARG1, CARG2, CARG2
1503 | checkfail ->fff_fallback
1504 | la CARG3, -8(BASE)
1505 | lwz PC, FRAME_PC(BASE)
1507 | evmergelo CRET1, CRET1, CRET2
1509 | evstdd CRET1, 0(BASE)
1513 |.macro math_minmax, name, cmpop
1517 | checkfail ->fff_fallback
1519 | evlddx CARG2, BASE, TMP1
1520 | cmplw cr1, TMP1, NARGS8:RC
1522 | bge cr1, ->fff_restv // Ok, since CRET1 = CARG1.
1523 | checkfail ->fff_fallback
1524 | cmpop CARG2, CARG1
1525 | addi TMP1, TMP1, 8
1526 | crmove 4*cr0+lt, 4*cr0+gt
1527 | evsel CARG1, CARG2, CARG1
1531 | math_minmax math_min, efdtstlt
1532 | math_minmax math_max, efdtstgt
1534 |//-- String library -----------------------------------------------------
1536 |.ffunc_1 string_len
1537 | checkstr STR:CARG1
1538 | checkfail ->fff_fallback
1539 | lwz TMP0, STR:CARG1->len
1540 | efdcfsi CRET1, TMP0
1543 |.ffunc string_byte // Only handle the 1-arg case here.
1544 | cmplwi NARGS8:RC, 8
1545 | evldd STR:CARG1, 0(BASE)
1546 | bne ->fff_fallback // Need exactly 1 argument.
1547 | checkstr STR:CARG1
1549 | checkfail ->fff_fallback
1550 | lwz TMP0, STR:CARG1->len
1552 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
1555 | lwz PC, FRAME_PC(BASE)
1556 | efdcfsi CRET1, TMP1
1557 | iseleq RD, RD, TMP2
1558 | evstdd CRET1, 0(RA)
1561 |.ffunc string_char // Only handle the 1-arg case here.
1563 | cmplwi NARGS8:RC, 8
1564 | evldd CARG1, 0(BASE)
1565 | bne ->fff_fallback // Exactly 1 argument.
1567 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1568 | checkfail ->fff_fallback
1569 | efdctsiz TMP0, CARG1
1572 | stb TMP0, 0(CARG2)
1573 | bgt ->fff_fallback
1578 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1579 | // Returns GCstr *.
1581 | evmergelo STR:CRET1, TISSTR, STR:CRET1
1586 | cmplwi NARGS8:RC, 16
1587 | evldd CARG3, 16(BASE)
1588 | evldd STR:CARG1, 0(BASE)
1589 | blt ->fff_fallback
1590 | evldd CARG2, 8(BASE)
1594 | checkfail ->fff_fallback
1595 | efdctsiz TMP2, CARG3
1598 | checkfail ->fff_fallback
1599 | checkstr STR:CARG1
1600 | efdctsiz TMP1, CARG2
1601 | checkfail ->fff_fallback
1602 | lwz TMP0, STR:CARG1->len
1603 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
1604 | add TMP3, TMP2, TMP0
1607 | cmpwi TMP1, 0 // start <= 0?
1608 | add TMP3, TMP1, TMP0
1611 | sub. CARG3, TMP2, TMP1
1612 | addi CARG2, STR:CARG1, #STR-1
1613 | addi CARG3, CARG3, 1
1614 | add CARG2, CARG2, TMP1
1615 | isellt CARG3, r0, CARG3
1618 |5: // Negative end or overflow.
1620 | addi TMP3, TMP3, 1
1621 | iselgt TMP2, TMP3, TMP0 // end = end > len ? len : end+len+1
1624 |7: // Negative start or underflow.
1625 | cmpwi cr1, TMP3, 0
1626 | iseleq TMP1, r0, TMP3
1627 | isel TMP1, r0, TMP1, 4*cr1+lt
1628 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
1631 |.ffunc string_rep // Only handle the 1-char case inline.
1633 | cmplwi NARGS8:RC, 16
1634 | evldd CARG1, 0(BASE)
1635 | evldd CARG2, 8(BASE)
1636 | bne ->fff_fallback // Exactly 2 arguments.
1638 | checkfail ->fff_fallback
1639 | checkstr STR:CARG1
1640 | efdctsiz CARG3, CARG2
1641 | checkfail ->fff_fallback
1642 | lwz TMP0, STR:CARG1->len
1644 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
1645 | ble >2 // Count <= 0? (or non-int)
1647 | subi TMP2, CARG3, 1
1648 | blt >2 // Zero length string?
1649 | cmplw cr1, TMP1, CARG3
1650 | bne ->fff_fallback // Fallback for > 1-char strings.
1651 | lbz TMP0, STR:CARG1[1]
1652 | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
1653 | blt cr1, ->fff_fallback
1654 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
1656 | stbx TMP0, CARG2, TMP2
1657 | subi TMP2, TMP2, 1
1660 |2: // Return empty string.
1661 | la STR:CRET1, DISPATCH_GL(strempty)(DISPATCH)
1662 | evmergelo CRET1, TISSTR, STR:CRET1
1665 |.ffunc string_reverse
1667 | cmplwi NARGS8:RC, 8
1668 | evldd CARG1, 0(BASE)
1669 | blt ->fff_fallback
1670 | checkstr STR:CARG1
1671 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
1672 | checkfail ->fff_fallback
1673 | lwz CARG3, STR:CARG1->len
1674 | la CARG1, #STR(STR:CARG1)
1675 | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
1678 | subi TMP3, CARG3, 1
1679 | blt ->fff_fallback
1680 |1: // Reverse string copy.
1682 | lbzx TMP1, CARG1, TMP2
1684 | stbx TMP1, CARG2, TMP3
1685 | subi TMP3, TMP3, 1
1686 | addi TMP2, TMP2, 1
1689 |.macro ffstring_case, name, lo
1692 | cmplwi NARGS8:RC, 8
1693 | evldd CARG1, 0(BASE)
1694 | blt ->fff_fallback
1695 | checkstr STR:CARG1
1696 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
1697 | checkfail ->fff_fallback
1698 | lwz CARG3, STR:CARG1->len
1699 | la CARG1, #STR(STR:CARG1)
1700 | lwz CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
1703 | blt ->fff_fallback
1704 |1: // ASCII case conversion.
1706 | lbzx TMP1, CARG1, TMP2
1708 | subi TMP0, TMP1, lo
1709 | xori TMP3, TMP1, 0x20
1711 | isellt TMP1, TMP3, TMP1
1712 | stbx TMP1, CARG2, TMP2
1713 | addi TMP2, TMP2, 1
1717 |ffstring_case string_lower, 65
1718 |ffstring_case string_upper, 97
1720 |//-- Table library ------------------------------------------------------
1722 |.ffunc_1 table_getn
1724 | checkfail ->fff_fallback
1725 | bl extern lj_tab_len // (GCtab *t)
1726 | // Returns uint32_t (but less than 2^31).
1727 | efdcfsi CRET1, CRET1
1730 |//-- Bit library --------------------------------------------------------
1732 |.macro .ffunc_bit, name
1733 | .ffunc_n bit_..name
1734 | efdadd CARG1, CARG1, TOBIT
1739 | efdcfsi CRET1, CARG1
1742 |.macro .ffunc_bit_op, name, ins
1746 | evlddx CARG2, BASE, TMP1
1747 | cmplw cr1, TMP1, NARGS8:RC
1749 | bge cr1, ->fff_resbit
1750 | checkfail ->fff_fallback
1751 | efdadd CARG2, CARG2, TOBIT
1752 | ins CARG1, CARG1, CARG2
1753 | addi TMP1, TMP1, 8
1757 |.ffunc_bit_op band, and
1758 |.ffunc_bit_op bor, or
1759 |.ffunc_bit_op bxor, xor
1762 | rotlwi TMP0, CARG1, 8
1763 | rlwimi TMP0, CARG1, 24, 0, 7
1764 | rlwimi TMP0, CARG1, 24, 16, 23
1765 | efdcfsi CRET1, TMP0
1770 | efdcfsi CRET1, TMP0
1773 |.macro .ffunc_bit_sh, name, ins, shmod
1774 | .ffunc_nn bit_..name
1775 | efdadd CARG2, CARG2, TOBIT
1776 | efdadd CARG1, CARG1, TOBIT
1778 | rlwinm CARG2, CARG2, 0, 27, 31
1782 | ins TMP0, CARG1, CARG2
1783 | efdcfsi CRET1, TMP0
1787 |.ffunc_bit_sh lshift, slw, 1
1788 |.ffunc_bit_sh rshift, srw, 1
1789 |.ffunc_bit_sh arshift, sraw, 1
1790 |.ffunc_bit_sh rol, rotlw, 0
1791 |.ffunc_bit_sh ror, rotlw, 2
1793 |//-----------------------------------------------------------------------
1795 |->fff_fallback: // Call fast function fallback handler.
1796 | // BASE = new base, RB = CFUNC, RC = nargs*8
1797 | lwz TMP3, CFUNC:RB->f
1798 | add TMP1, BASE, NARGS8:RC
1799 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
1800 | addi TMP0, TMP1, 8*LUA_MINSTACK
1801 | lwz TMP2, L->maxstack
1802 | stw PC, SAVE_PC // Redundant (but a defined value).
1807 | bgt >5 // Need to grow stack.
1809 | bctrl // (lua_State *L)
1810 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1815 | bgt ->fff_res // Returned nresults+1?
1816 |1: // Returned 0 or -1: retry fast path.
1818 | lwz LFUNC:RB, FRAME_FUNC(BASE)
1819 | sub NARGS8:RC, TMP0, BASE
1820 | bne ->vm_call_tail // Returned -1?
1821 | ins_callt // Returned 0: retry fast path.
1823 |// Reconstruct previous base for vmeta_call during tailcall.
1825 | andi. TMP0, PC, FRAME_TYPE
1826 | rlwinm TMP1, PC, 0, 0, 28
1829 | decode_RA8 TMP1, INS
1830 | addi TMP1, TMP1, 8
1832 | sub TMP2, BASE, TMP1
1833 | b ->vm_call_dispatch // Resolve again for tailcall.
1835 |5: // Grow stack for fallback handler.
1836 | li CARG2, LUA_MINSTACK
1837 | bl extern lj_state_growstack // (lua_State *L, int n)
1839 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
1842 |->fff_gcstep: // Call GC step function.
1843 | // BASE = new base, RC = nargs*8
1846 | add TMP0, BASE, NARGS8:RC
1847 | stw PC, SAVE_PC // Redundant (but a defined value).
1850 | bl extern lj_gc_step // (lua_State *L)
1854 | sub NARGS8:RC, TMP0, BASE
1855 | lwz CFUNC:RB, FRAME_FUNC(BASE)
1858 |//-----------------------------------------------------------------------
1859 |//-- Special dispatch targets -------------------------------------------
1860 |//-----------------------------------------------------------------------
1862 |->vm_record: // Dispatch target for recording phase.
1867 |->vm_rethook: // Dispatch target for return hooks.
1868 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1869 | andi. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
1871 |5: // Re-dispatch to static ins.
1872 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OP4 TMP1, INS.
1873 | lwzx TMP0, DISPATCH, TMP1
1877 |->vm_inshook: // Dispatch target for instr/line hooks.
1878 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1879 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
1880 | andi. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
1881 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
1884 | cmpwi cr1, TMP0, 0
1885 | addic. TMP2, TMP2, -1
1887 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
1892 | stw MULTRES, SAVE_MULTRES
1895 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
1896 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
1899 |4: // Re-dispatch to static ins.
1901 | decode_OP4 TMP1, INS
1902 | decode_RB8 RB, INS
1903 | addi TMP1, TMP1, GG_DISP2STATIC
1904 | decode_RD8 RD, INS
1905 | lwzx TMP0, DISPATCH, TMP1
1906 | decode_RA8 RA, INS
1907 | decode_RC8 RC, INS
1911 |->cont_hook: // Continue from hook yield.
1913 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
1916 |->vm_hotloop: // Hot loop counter underflow.
1921 |->vm_callhook: // Dispatch target for call hooks.
1927 |->vm_hotcall: // Hot call counter underflow.
1932 | add TMP0, BASE, RC
1938 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
1939 | // Returns ASMFunction.
1942 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
1943 | sub NARGS8:RC, TMP0, BASE
1945 | lwz LFUNC:RB, FRAME_FUNC(BASE)
1949 |//-----------------------------------------------------------------------
1950 |//-- Trace exit handler -------------------------------------------------
1951 |//-----------------------------------------------------------------------
1962 |//-----------------------------------------------------------------------
1963 |//-- Math helper functions ----------------------------------------------
1964 |//-----------------------------------------------------------------------
1966 |// FP value rounding. Called by math.floor/math.ceil fast functions
1967 |// and from JIT code.
1969 |// This can be inlined if the CPU has the frin/friz/frip/frim instructions.
1970 |// The alternative hard-float approaches have a deep dependency chain.
1971 |// The resulting latency is at least 3x-7x the double-precision FP latency
1972 |// (e500v2: 6cy, e600: 5cy, Cell: 10cy) or around 20-70 cycles.
1974 |// The soft-float approach is tedious, but much faster (e500v2: ~11cy/~6cy).
1975 |// However it relies on a fast way to transfer the FP value to GPRs
1976 |// (e500v2: 0cy for lo-word, 1cy for hi-word).
1978 |.macro vm_round, name, mode
1979 | // Used temporaries: TMP0, TMP1, TMP2, TMP3.
1980 |->name.._efd: // Input: CARG2, output: CRET2
1981 | evmergehi CARG1, CARG2, CARG2
1983 | // Input: CARG1 (hi), CARG2 (hi, lo), output: CRET2
1984 | rlwinm TMP2, CARG1, 12, 21, 31
1985 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
1987 | cmplwi cr1, TMP2, 51 // 0 <= exp <= 51?
1988 | subfic TMP0, TMP2, 52
1991 | slw TMP0, TMP1, TMP0 // lomask = -1 << (52-exp)
1992 | sraw TMP1, TMP3, TMP2 // himask = (int32_t)0xfff00000 >> exp
1993 |.if mode == 2 // trunc(x):
1994 | evmergelo TMP0, TMP1, TMP0
1995 | evand CRET2, CARG2, TMP0 // hi &= himask, lo &= lomask
1997 | andc TMP2, CARG2, TMP0
1998 | andc TMP3, CARG1, TMP1
1999 | or TMP2, TMP2, TMP3 // ztest = (hi&~himask) | (lo&~lomask)
2000 | srawi TMP3, CARG1, 31 // signmask = (int32_t)hi >> 31
2001 |.if mode == 0 // floor(x):
2002 | and. TMP2, TMP2, TMP3 // iszero = ((ztest & signmask) == 0)
2004 | andc. TMP2, TMP2, TMP3 // iszero = ((ztest & ~signmask) == 0)
2006 | and CARG2, CARG2, TMP0 // lo &= lomask
2007 | and CARG1, CARG1, TMP1 // hi &= himask
2008 | subc TMP0, CARG2, TMP0
2009 | iseleq TMP0, CARG2, TMP0 // lo = iszero ? lo : lo-lomask
2010 | sube TMP1, CARG1, TMP1
2011 | iseleq TMP1, CARG1, TMP1 // hi = iszero ? hi : hi-himask+carry
2012 | evmergelo CRET2, TMP1, TMP0
2016 | bgtlr // Already done if >=2^52, +-inf or nan.
2017 |.if mode == 2 // trunc(x):
2018 | rlwinm TMP1, CARG1, 0, 0, 0 // hi = sign(x)
2020 | evmergelo CRET2, TMP1, TMP0
2022 | rlwinm TMP2, CARG1, 0, 1, 31
2023 | srawi TMP0, CARG1, 31 // signmask = (int32_t)hi >> 31
2024 | or TMP2, TMP2, CARG2 // ztest = abs(hi) | lo
2026 |.if mode == 0 // floor(x):
2027 | and. TMP2, TMP2, TMP0 // iszero = ((ztest & signmask) == 0)
2029 | andc. TMP2, TMP2, TMP0 // iszero = ((ztest & ~signmask) == 0)
2032 | iseleq TMP1, r0, TMP1
2033 | rlwimi CARG1, TMP1, 0, 1, 31 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2034 | evmergelo CRET2, CARG1, TMP0
2041 | bl ->vm_floor_hilo
2043 | evmergehi CRET1, CRET2, CRET2
2046 | vm_round vm_floor, 0
2047 | vm_round vm_ceil, 1
2049 | vm_round vm_trunc, 2
2055 |//-----------------------------------------------------------------------
2056 |//-- Miscellaneous functions --------------------------------------------
2057 |//-----------------------------------------------------------------------
2059 |//-----------------------------------------------------------------------
2060 |//-- FFI helper functions -----------------------------------------------
2061 |//-----------------------------------------------------------------------
2068 |//-----------------------------------------------------------------------
2071 /* Generate the code for a single instruction. */
2072 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2079 /* -- Comparison ops ---------------------------------------------------- */
2081 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2083 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2084 | // RA = src1*8, RD = src2*8, JMP with RD = target
2085 | evlddx TMP0, BASE, RA
2087 | evlddx TMP1, BASE, RD
2088 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2090 | evmergehi RB, TMP0, TMP1
2091 | decode_RD4 TMP2, TMP2
2093 | add TMP2, TMP2, TMP3
2094 | checkanyfail ->vmeta_comp
2095 | efdcmplt TMP0, TMP1
2096 if (op == BC_ISLE || op == BC_ISGT) {
2097 | efdcmpeq cr1, TMP0, TMP1
2098 | cror 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2100 if (op == BC_ISLT || op == BC_ISLE) {
2101 | iselgt PC, TMP2, PC
2103 | iselgt PC, PC, TMP2
2108 case BC_ISEQV: case BC_ISNEV:
2109 vk = op == BC_ISEQV;
2110 | // RA = src1*8, RD = src2*8, JMP with RD = target
2111 | evlddx CARG2, BASE, RA
2113 | evlddx CARG3, BASE, RD
2114 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2116 | evmergehi RB, CARG2, CARG3
2117 | decode_RD4 TMP2, TMP2
2119 | add TMP2, TMP2, TMP3
2121 | efdcmpeq CARG2, CARG3
2123 | iselgt PC, TMP2, PC
2125 | iselgt PC, PC, TMP2
2130 |5: // Either or both types are not numbers.
2131 | evcmpeq CARG2, CARG3
2133 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
2134 | crorc 4*cr7+lt, 4*cr0+so, 4*cr0+lt // 1: Same tv or different type.
2135 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
2136 | crandc 4*cr7+gt, 4*cr0+lt, 4*cr1+gt // 2: Same type and primitive.
2139 | isel PC, TMP2, PC, 4*cr7+gt
2141 | isel TMP2, PC, TMP2, 4*cr7+gt
2143 | cror 4*cr7+lt, 4*cr7+lt, 4*cr7+gt // 1 or 2.
2145 | isel PC, TMP2, PC, 4*cr0+so
2147 | isel PC, PC, TMP2, 4*cr0+so
2149 | blt cr7, <1 // Done if 1 or 2.
2150 | blt cr6, <1 // Done if not tab/ud.
2152 | // Different tables or userdatas. Need to check __eq metamethod.
2153 | // Field metatable must be at same offset for GCtab and GCudata!
2154 | lwz TAB:TMP2, TAB:CARG2->metatable
2155 | li CARG4, 1-vk // ne = 0 or 1.
2156 | cmplwi TAB:TMP2, 0
2157 | beq <1 // No metatable?
2158 | lbz TMP2, TAB:TMP2->nomm
2159 | andi. TMP2, TMP2, 1<<MM_eq
2160 | bne <1 // Or 'no __eq' flag set?
2161 | mr PC, SAVE0 // Restore old PC.
2162 | b ->vmeta_equal // Handle __eq metamethod.
2165 case BC_ISEQS: case BC_ISNES:
2166 vk = op == BC_ISEQS;
2167 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
2168 | evlddx TMP0, BASE, RA
2173 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
2174 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2175 | decode_RD4 TMP2, INS
2176 | evmergelo STR:TMP1, TISSTR, STR:TMP1
2177 | add TMP2, TMP2, TMP3
2178 | evcmpeq TMP0, STR:TMP1
2180 | isel PC, TMP2, PC, 4*cr0+so
2182 | isel PC, PC, TMP2, 4*cr0+so
2187 case BC_ISEQN: case BC_ISNEN:
2188 vk = op == BC_ISEQN;
2189 | // RA = src*8, RD = num_const*8, JMP with RD = target
2190 | evlddx TMP0, BASE, RA
2192 | evlddx TMP1, KBASE, RD
2193 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2197 | efdcmpeq TMP0, TMP1
2199 | decode_RD4 TMP2, INS
2200 | add TMP2, TMP2, TMP3
2202 | iselgt PC, TMP2, PC
2205 | iselgt PC, PC, TMP2
2211 | decode_RD4 TMP2, INS
2212 | add PC, TMP2, TMP3
2217 case BC_ISEQP: case BC_ISNEP:
2218 vk = op == BC_ISEQP;
2219 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
2220 | lwzx TMP0, BASE, RA
2225 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2227 | decode_RD4 TMP2, INS
2228 | add TMP2, TMP2, TMP3
2230 | iseleq PC, TMP2, PC
2232 | iseleq PC, PC, TMP2
2237 /* -- Unary test and copy ops ------------------------------------------- */
2239 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2240 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
2241 | evlddx TMP0, BASE, RD
2242 | evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE.
2244 | evcmpltu TMP0, TMP1
2246 if (op == BC_IST || op == BC_ISF) {
2247 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2248 | decode_RD4 TMP2, INS
2249 | add TMP2, TMP2, TMP3
2251 | isellt PC, TMP2, PC
2253 | isellt PC, PC, TMP2
2256 if (op == BC_ISTC) {
2261 | addis PC, PC, -(BCBIAS_J*4 >> 16)
2262 | decode_RD4 TMP2, INS
2263 | evstddx TMP0, BASE, RA
2270 /* -- Unary ops --------------------------------------------------------- */
2273 | // RA = dst*8, RD = src*8
2275 | evlddx TMP0, BASE, RD
2276 | evstddx TMP0, BASE, RA
2280 | // RA = dst*8, RD = src*8
2282 | lwzx TMP0, BASE, RD
2283 | subfic TMP1, TMP0, LJ_TTRUE
2284 | adde TMP0, TMP0, TMP1
2285 | stwx TMP0, BASE, RA
2289 | // RA = dst*8, RD = src*8
2290 | evlddx TMP0, BASE, RD
2292 | checkfail ->vmeta_unm
2295 | evstddx TMP0, BASE, RA
2299 | // RA = dst*8, RD = src*8
2300 | evlddx CARG1, BASE, RD
2303 | lwz CRET1, STR:CARG1->len
2306 | efdcfsi TMP0, CRET1
2307 | evstddx TMP0, BASE, RA
2311 | checkfail ->vmeta_len
2313 | lwz TAB:TMP2, TAB:CARG1->metatable
2314 | cmplwi TAB:TMP2, 0
2319 | bl extern lj_tab_len // (GCtab *t)
2320 | // Returns uint32_t (but less than 2^31).
2324 | lbz TMP0, TAB:TMP2->nomm
2325 | andi. TMP0, TMP0, 1<<MM_len
2326 | bne <3 // 'no __len' flag set: done.
2331 /* -- Binary ops -------------------------------------------------------- */
2333 |.macro ins_arithpre, t0, t1
2334 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
2335 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
2338 | evlddx t0, BASE, RB
2340 | evlddx t1, KBASE, RC
2341 | checkfail ->vmeta_arith_vn
2344 | evlddx t1, BASE, RB
2346 | evlddx t0, KBASE, RC
2347 | checkfail ->vmeta_arith_nv
2350 | evlddx t0, BASE, RB
2351 | evlddx t1, BASE, RC
2352 | evmergehi TMP2, t0, t1
2354 | checkanyfail ->vmeta_arith_vv
2359 |.macro ins_arith, ins
2360 | ins_arithpre TMP0, TMP1
2362 | ins TMP0, TMP0, TMP1
2363 | evstddx TMP0, BASE, RA
2367 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
2370 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
2373 case BC_MULVN: case BC_MULNV: case BC_MULVV:
2376 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
2380 | ins_arithpre RD, SAVE0
2382 | efddiv CARG2, RD, SAVE0
2383 | bl ->vm_floor_efd // floor(b/c)
2384 | efdmul TMP0, CRET2, SAVE0
2386 | efdsub TMP0, RD, TMP0 // b - floor(b/c)*c
2387 | evstddx TMP0, BASE, RA
2390 case BC_MODNV: case BC_MODVV:
2391 | ins_arithpre RD, SAVE0
2392 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
2395 | evlddx CARG2, BASE, RB
2396 | evlddx CARG4, BASE, RC
2397 | evmergehi CARG1, CARG4, CARG2
2399 | evmergehi CARG3, CARG4, CARG4
2400 | checkanyfail ->vmeta_arith_vv
2402 | evmergelo CRET2, CRET1, CRET2
2403 | evstddx CRET2, BASE, RA
2408 | // RA = dst*8, RB = src_start*8, RC = src_end*8
2411 | add CARG2, BASE, RC
2416 | srwi CARG3, CARG3, 3
2417 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
2418 | // Returns NULL (finished) or TValue * (metamethod).
2422 | evlddx TMP0, BASE, SAVE0 // Copy result from RB to RA.
2423 | evstddx TMP0, BASE, RA
2427 /* -- Constant ops ------------------------------------------------------ */
2430 | // RA = dst*8, RD = str_const*8 (~)
2433 | subfic TMP1, TMP1, -4
2434 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
2435 | evmergelo TMP0, TISSTR, TMP0
2436 | evstddx TMP0, BASE, RA
2441 | // RA = dst*8, RD = cdata_const*8 (~)
2444 | subfic TMP1, TMP1, -4
2445 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
2446 | li TMP2, LJ_TCDATA
2447 | evmergelo TMP0, TMP2, TMP0
2448 | evstddx TMP0, BASE, RA
2453 | // RA = dst*8, RD = int16_literal*8
2457 | efdcfsi TMP0, TMP1
2458 | evstddx TMP0, BASE, RA
2462 | // RA = dst*8, RD = num_const*8
2463 | evlddx TMP0, KBASE, RD
2465 | evstddx TMP0, BASE, RA
2469 | // RA = dst*8, RD = primitive_type*8 (~)
2473 | stwx TMP0, BASE, RA
2477 | // RA = base*8, RD = end*8
2478 | evstddx TISNIL, BASE, RA
2481 | evstddx TISNIL, BASE, RA
2488 /* -- Upvalue and function ops ------------------------------------------ */
2491 | // RA = dst*8, RD = uvnum*8
2493 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2495 | addi RD, RD, offsetof(GCfuncL, uvptr)
2496 | lwzx UPVAL:RB, LFUNC:RB, RD
2497 | lwz TMP1, UPVAL:RB->v
2498 | evldd TMP0, 0(TMP1)
2499 | evstddx TMP0, BASE, RA
2503 | // RA = uvnum*8, RD = src*8
2504 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2506 | addi RA, RA, offsetof(GCfuncL, uvptr)
2507 | evlddx TMP1, BASE, RD
2508 | lwzx UPVAL:RB, LFUNC:RB, RA
2509 | lbz TMP3, UPVAL:RB->marked
2510 | lwz CARG2, UPVAL:RB->v
2511 | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
2512 | lbz TMP0, UPVAL:RB->closed
2513 | evmergehi TMP2, TMP1, TMP1
2514 | evstdd TMP1, 0(CARG2)
2515 | cmplwi cr1, TMP0, 0
2516 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
2517 | subi TMP2, TMP2, (LJ_TISNUM+1)
2518 | bne >2 // Upvalue is closed and black?
2522 |2: // Check if new value is collectable.
2523 | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1)
2524 | bge <1 // tvisgcv(v)
2525 | lbz TMP3, GCOBJ:TMP1->gch.marked
2526 | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
2527 | la CARG1, GG_DISP2G(DISPATCH)
2528 | // Crossed a write barrier. Move the barrier forward.
2530 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2534 | // RA = uvnum*8, RD = str_const*8 (~)
2535 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2538 | subfic TMP1, TMP1, -4
2539 | addi RA, RA, offsetof(GCfuncL, uvptr)
2540 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
2541 | lwzx UPVAL:RB, LFUNC:RB, RA
2542 | evmergelo STR:TMP1, TISSTR, STR:TMP1
2543 | lbz TMP3, UPVAL:RB->marked
2544 | lwz CARG2, UPVAL:RB->v
2545 | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
2546 | lbz TMP3, STR:TMP1->marked
2547 | lbz TMP2, UPVAL:RB->closed
2548 | evstdd STR:TMP1, 0(CARG2)
2553 |2: // Check if string is white and ensure upvalue is closed.
2554 | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
2555 | cmplwi cr1, TMP2, 0
2556 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
2557 | la CARG1, GG_DISP2G(DISPATCH)
2558 | // Crossed a write barrier. Move the barrier forward.
2560 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2564 | // RA = uvnum*8, RD = num_const*8
2566 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2568 | addi RA, RA, offsetof(GCfuncL, uvptr)
2569 | evlddx TMP0, KBASE, RD
2570 | lwzx UPVAL:RB, LFUNC:RB, RA
2571 | lwz TMP1, UPVAL:RB->v
2572 | evstdd TMP0, 0(TMP1)
2576 | // RA = uvnum*8, RD = primitive_type*8 (~)
2578 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2580 | addi RA, RA, offsetof(GCfuncL, uvptr)
2582 | lwzx UPVAL:RB, LFUNC:RB, RA
2584 | lwz TMP1, UPVAL:RB->v
2590 | // RA = level*8, RD = target
2591 | lwz TMP1, L->openupval
2592 | branch_RD // Do this first since RD is not saved.
2597 | add CARG2, BASE, RA
2598 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
2605 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
2608 | subfic TMP1, TMP1, -4
2610 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
2612 | lwz CARG3, FRAME_FUNC(BASE)
2613 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
2614 | bl extern lj_func_newL_gc
2615 | // Returns GCfuncL *.
2617 | evmergelo LFUNC:CRET1, TISFUNC, LFUNC:CRET1
2618 | evstddx LFUNC:CRET1, BASE, RA
2622 /* -- Table ops --------------------------------------------------------- */
2626 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
2627 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
2629 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
2635 if (op == BC_TNEW) {
2636 | rlwinm CARG2, RD, 29, 21, 31
2637 | rlwinm CARG3, RD, 18, 27, 31
2638 | cmpwi CARG2, 0x7ff
2640 | iseleq CARG2, TMP1, CARG2
2641 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
2642 | // Returns Table *.
2645 | subfic TMP1, TMP1, -4
2646 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
2647 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
2648 | // Returns Table *.
2651 | evmergelo TAB:CRET1, TISTAB, TAB:CRET1
2652 | evstddx TAB:CRET1, BASE, RA
2656 | bl extern lj_gc_step_fixtop // (lua_State *L)
2663 | // RA = dst*8, RD = str_const*8 (~)
2665 | // RA = src*8, RD = str_const*8 (~)
2666 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
2668 | lwz TAB:RB, LFUNC:TMP2->env
2669 | subfic TMP1, TMP1, -4
2670 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2671 if (op == BC_GGET) {
2679 | // RA = dst*8, RB = table*8, RC = key*8
2680 | evlddx TAB:RB, BASE, RB
2681 | evlddx RC, BASE, RC
2683 | checkfail ->vmeta_tgetv
2686 | // Convert number key to integer
2688 | lwz TMP0, TAB:RB->asize
2689 | efdcfsi TMP1, TMP2
2690 | cmplw cr0, TMP0, TMP2
2691 | efdcmpeq cr1, RC, TMP1
2692 | lwz TMP1, TAB:RB->array
2693 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2694 | slwi TMP2, TMP2, 3
2695 | ble ->vmeta_tgetv // Integer key and in array part?
2696 | evlddx TMP1, TMP1, TMP2
2700 | evstddx TMP1, BASE, RA
2703 |2: // Check for __index if table value is nil.
2704 | lwz TAB:TMP2, TAB:RB->metatable
2705 | cmplwi TAB:TMP2, 0
2706 | beq <1 // No metatable: done.
2707 | lbz TMP0, TAB:TMP2->nomm
2708 | andi. TMP0, TMP0, 1<<MM_index
2709 | bne <1 // 'no __index' flag set: done.
2713 | checkstr STR:RC // String key?
2714 | checkok ->BC_TGETS_Z
2718 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
2719 | evlddx TAB:RB, BASE, RB
2722 | subfic TMP1, TMP1, -4
2723 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2724 | checkfail ->vmeta_tgets1
2726 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
2727 | lwz TMP0, TAB:RB->hmask
2728 | lwz TMP1, STR:RC->hash
2729 | lwz NODE:TMP2, TAB:RB->node
2730 | evmergelo STR:RC, TISSTR, STR:RC
2731 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
2732 | slwi TMP0, TMP1, 5
2733 | slwi TMP1, TMP1, 3
2734 | sub TMP1, TMP0, TMP1
2735 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
2737 | evldd TMP0, NODE:TMP2->key
2738 | evldd TMP1, NODE:TMP2->val
2739 | evcmpeq TMP0, STR:RC
2742 | checkok >5 // Key found, but nil value?
2744 | evstddx TMP1, BASE, RA
2747 |4: // Follow hash chain.
2748 | lwz NODE:TMP2, NODE:TMP2->next
2749 | cmplwi NODE:TMP2, 0
2751 | // End of hash chain: key not found, nil result.
2754 |5: // Check for __index if table value is nil.
2755 | lwz TAB:TMP2, TAB:RB->metatable
2756 | cmplwi TAB:TMP2, 0
2757 | beq <3 // No metatable: done.
2758 | lbz TMP0, TAB:TMP2->nomm
2759 | andi. TMP0, TMP0, 1<<MM_index
2760 | bne <3 // 'no __index' flag set: done.
2764 | // RA = dst*8, RB = table*8, RC = index*8
2765 | evlddx TAB:RB, BASE, RB
2768 | checkfail ->vmeta_tgetb
2769 | lwz TMP1, TAB:RB->asize
2770 | lwz TMP2, TAB:RB->array
2773 | evlddx TMP1, TMP2, RC
2778 | evstddx TMP1, BASE, RA
2781 |5: // Check for __index if table value is nil.
2782 | lwz TAB:TMP2, TAB:RB->metatable
2783 | cmplwi TAB:TMP2, 0
2784 | beq <1 // No metatable: done.
2785 | lbz TMP2, TAB:TMP2->nomm
2786 | andi. TMP2, TMP2, 1<<MM_index
2787 | bne <1 // 'no __index' flag set: done.
2788 | b ->vmeta_tgetb // Caveat: preserve TMP0!
2792 | // RA = src*8, RB = table*8, RC = key*8
2793 | evlddx TAB:RB, BASE, RB
2794 | evlddx RC, BASE, RC
2796 | checkfail ->vmeta_tsetv
2799 | // Convert number key to integer
2801 | evlddx SAVE0, BASE, RA
2802 | lwz TMP0, TAB:RB->asize
2803 | efdcfsi TMP1, TMP2
2804 | cmplw cr0, TMP0, TMP2
2805 | efdcmpeq cr1, RC, TMP1
2806 | lwz TMP1, TAB:RB->array
2807 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2808 | slwi TMP0, TMP2, 3
2809 | ble ->vmeta_tsetv // Integer key and in array part?
2810 | lbz TMP3, TAB:RB->marked
2811 | evlddx TMP2, TMP1, TMP0
2815 | andi. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
2816 | evstddx SAVE0, TMP1, TMP0
2821 |3: // Check for __newindex if previous value is nil.
2822 | lwz TAB:TMP2, TAB:RB->metatable
2823 | cmplwi TAB:TMP2, 0
2824 | beq <1 // No metatable: done.
2825 | lbz TMP2, TAB:TMP2->nomm
2826 | andi. TMP2, TMP2, 1<<MM_newindex
2827 | bne <1 // 'no __newindex' flag set: done.
2831 | checkstr STR:RC // String key?
2832 | checkok ->BC_TSETS_Z
2835 |7: // Possible table write barrier for the value. Skip valiswhite check.
2836 | barrierback TAB:RB, TMP3, TMP0
2840 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
2841 | evlddx TAB:RB, BASE, RB
2844 | subfic TMP1, TMP1, -4
2845 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2846 | checkfail ->vmeta_tsets1
2848 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
2849 | lwz TMP0, TAB:RB->hmask
2850 | lwz TMP1, STR:RC->hash
2851 | lwz NODE:TMP2, TAB:RB->node
2852 | evmergelo STR:RC, TISSTR, STR:RC
2853 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
2854 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
2855 | evlddx SAVE0, BASE, RA
2856 | slwi TMP0, TMP1, 5
2857 | slwi TMP1, TMP1, 3
2858 | sub TMP1, TMP0, TMP1
2859 | lbz TMP3, TAB:RB->marked
2860 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
2862 | evldd TMP0, NODE:TMP2->key
2863 | evldd TMP1, NODE:TMP2->val
2864 | evcmpeq TMP0, STR:RC
2867 | checkok >4 // Key found, but nil value?
2869 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
2870 | evstdd SAVE0, NODE:TMP2->val
2875 |4: // Check for __newindex if previous value is nil.
2876 | lwz TAB:TMP1, TAB:RB->metatable
2877 | cmplwi TAB:TMP1, 0
2878 | beq <2 // No metatable: done.
2879 | lbz TMP0, TAB:TMP1->nomm
2880 | andi. TMP0, TMP0, 1<<MM_newindex
2881 | bne <2 // 'no __newindex' flag set: done.
2884 |5: // Follow hash chain.
2885 | lwz NODE:TMP2, NODE:TMP2->next
2886 | cmplwi NODE:TMP2, 0
2888 | // End of hash chain: key not found, add a new one.
2890 | // But check for __newindex first.
2891 | lwz TAB:TMP1, TAB:RB->metatable
2892 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
2895 | cmplwi TAB:TMP1, 0
2897 | beq >6 // No metatable: continue.
2898 | lbz TMP0, TAB:TMP1->nomm
2899 | andi. TMP0, TMP0, 1<<MM_newindex
2900 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
2903 | evstdd STR:RC, 0(CARG3)
2904 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
2905 | // Returns TValue *.
2907 | evstdd SAVE0, 0(CRET1)
2908 | b <3 // No 2nd write barrier needed.
2910 |7: // Possible table write barrier for the value. Skip valiswhite check.
2911 | barrierback TAB:RB, TMP3, TMP0
2915 | // RA = src*8, RB = table*8, RC = index*8
2916 | evlddx TAB:RB, BASE, RB
2919 | checkfail ->vmeta_tsetb
2920 | lwz TMP1, TAB:RB->asize
2921 | lwz TMP2, TAB:RB->array
2922 | lbz TMP3, TAB:RB->marked
2924 | evlddx SAVE0, BASE, RA
2926 | evlddx TMP1, TMP2, RC
2930 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
2931 | evstddx SAVE0, TMP2, RC
2936 |5: // Check for __newindex if previous value is nil.
2937 | lwz TAB:TMP1, TAB:RB->metatable
2938 | cmplwi TAB:TMP1, 0
2939 | beq <1 // No metatable: done.
2940 | lbz TMP1, TAB:TMP1->nomm
2941 | andi. TMP1, TMP1, 1<<MM_newindex
2942 | bne <1 // 'no __newindex' flag set: done.
2943 | b ->vmeta_tsetb // Caveat: preserve TMP0!
2945 |7: // Possible table write barrier for the value. Skip valiswhite check.
2946 | barrierback TAB:RB, TMP3, TMP0
2951 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
2954 | add TMP3, KBASE, RD
2955 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
2956 | addic. TMP0, MULTRES, -8
2957 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
2958 | srwi CARG3, TMP0, 3
2959 | beq >4 // Nothing to copy?
2960 | add CARG3, CARG3, TMP3
2961 | lwz TMP2, TAB:CARG2->asize
2962 | slwi TMP1, TMP3, 3
2963 | lbz TMP3, TAB:CARG2->marked
2965 | add TMP2, RA, TMP0
2966 | lwz TMP0, TAB:CARG2->array
2968 | add TMP1, TMP1, TMP0
2969 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
2970 |3: // Copy result slots to table.
2973 | cmpw cr1, RA, TMP2
2974 | evstdd TMP0, 0(TMP1)
2975 | addi TMP1, TMP1, 8
2981 |5: // Need to resize array part.
2986 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
2987 | // Must not reallocate the stack.
2991 |7: // Possible table write barrier for any value. Skip valiswhite check.
2992 | barrierback TAB:CARG2, TMP3, TMP0
2996 /* -- Calls and vararg handling ----------------------------------------- */
2999 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
3000 | add NARGS8:RC, NARGS8:RC, MULTRES
3001 | // Fall through. Assumes BC_CALL follows.
3004 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
3005 | evlddx LFUNC:RB, BASE, RA
3007 | add BASE, BASE, RA
3008 | subi NARGS8:RC, NARGS8:RC, 8
3009 | checkfunc LFUNC:RB
3010 | addi BASE, BASE, 8
3011 | checkfail ->vmeta_call
3016 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
3017 | add NARGS8:RC, NARGS8:RC, MULTRES
3018 | // Fall through. Assumes BC_CALLT follows.
3021 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3022 | evlddx LFUNC:RB, BASE, RA
3024 | lwz TMP1, FRAME_PC(BASE)
3025 | subi NARGS8:RC, NARGS8:RC, 8
3026 | checkfunc LFUNC:RB
3028 | checkfail ->vmeta_callt
3030 | andi. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
3031 | lbz TMP3, LFUNC:RB->ffid
3032 | xori TMP2, TMP1, FRAME_VARG
3033 | cmplwi cr1, NARGS8:RC, 0
3036 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
3038 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
3041 | addi TMP3, TMP2, 8
3042 | evlddx TMP0, RA, TMP2
3043 | cmplw cr1, TMP3, NARGS8:RC
3044 | evstddx TMP0, BASE, TMP2
3048 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
3053 |5: // Tailcall to a fast function with a Lua frame below.
3055 | decode_RA8 RA, INS
3056 | sub TMP1, BASE, RA
3057 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
3058 | lwz TMP1, LFUNC:TMP1->pc
3059 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
3062 |7: // Tailcall from a vararg function.
3063 | andi. TMP0, TMP2, FRAME_TYPEP
3064 | bne <1 // Vararg frame below?
3065 | sub BASE, BASE, TMP2 // Relocate BASE down.
3066 | lwz TMP1, FRAME_PC(BASE)
3067 | andi. TMP0, TMP1, FRAME_TYPE
3072 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
3073 | subi RA, RA, 24 // evldd doesn't support neg. offsets.
3075 | evlddx LFUNC:RB, BASE, RA
3076 | add BASE, BASE, RA
3077 | evldd TMP0, 8(BASE)
3078 | evldd TMP1, 16(BASE)
3079 | evstdd LFUNC:RB, 24(BASE) // Copy callable.
3080 | checkfunc LFUNC:RB
3081 | evstdd TMP0, 32(BASE) // Copy state.
3082 | li NARGS8:RC, 16 // Iterators get 2 arguments.
3083 | evstdd TMP1, 40(BASE) // Copy control var.
3084 | addi BASE, BASE, 32
3085 | checkfail ->vmeta_call
3090 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
3092 | // NYI: add hotloop, record BC_ITERN.
3095 | lwz TAB:RB, -12(RA)
3096 | lwz RC, -4(RA) // Get index from control var.
3097 | lwz TMP0, TAB:RB->asize
3098 | lwz TMP1, TAB:RB->array
3100 |1: // Traverse array part.
3103 | bge >5 // Index points after array part?
3104 | evlddx TMP2, TMP1, TMP3
3110 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
3111 | evstdd TMP2, 8(RA)
3112 | decode_RD4 TMP1, INS
3113 | stw RC, -4(RA) // Update control var.
3114 | add PC, TMP1, TMP3
3115 | evstdd TMP0, 0(RA)
3119 |4: // Skip holes in array part.
3123 |5: // Traverse hash part.
3124 | lwz TMP1, TAB:RB->hmask
3126 | lwz TMP2, TAB:RB->node
3128 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
3132 | sub TMP3, TMP3, RB
3133 | evlddx RB, TMP2, TMP3
3134 | add NODE:TMP3, TMP2, TMP3
3138 | evldd TMP3, NODE:TMP3->key
3139 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
3142 | decode_RD4 TMP1, INS
3143 | evstdd TMP3, 0(RA)
3145 | add PC, TMP1, TMP2
3146 | stw RC, -4(RA) // Update control var.
3149 |7: // Skip holes in hash part.
3155 | // RA = base*8, RD = target (points to ITERN)
3158 | evlddx CFUNC:TMP1, RA, TMP2
3161 | evmergehi TMP0, CFUNC:TMP1, CFUNC:TMP1
3162 | cmpwi cr0, TMP2, LJ_TTAB
3163 | cmpwi cr1, TMP0, LJ_TFUNC
3164 | cmpwi cr6, TMP3, LJ_TNIL
3166 | lbz TMP1, CFUNC:TMP1->ffid
3167 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
3168 | cmpwi cr7, TMP1, FF_next_N
3170 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
3171 | add TMP3, PC, TMP0
3174 | ori TMP1, TMP1, 0x7fff
3175 | stw ZERO, -4(RA) // Initialize control var.
3177 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
3180 |5: // Despecialize bytecode if any of the checks fail.
3184 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
3190 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
3191 | lwz TMP0, FRAME_PC(BASE)
3194 | addi RC, RC, FRAME_VARG
3196 | subi TMP3, BASE, 8 // TMP3 = vtop
3197 | sub RC, RC, TMP0 // RC = vbase
3198 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
3200 | sub. TMP1, TMP3, RC
3201 | beq cr1, >5 // Copy all varargs?
3202 | subi TMP2, TMP2, 16
3203 | ble >2 // No vararg slots?
3204 |1: // Copy vararg slots to destination slots.
3207 | evstdd TMP0, 0(RA)
3209 | cmplw cr1, RC, TMP3
3210 | bge >3 // All destination slots filled?
3212 | blt cr1, <1 // More vararg slots?
3213 |2: // Fill up remainder with nil.
3214 | evstdd TISNIL, 0(RA)
3221 |5: // Copy all varargs.
3222 | lwz TMP0, L->maxstack
3223 | li MULTRES, 8 // MULTRES = (0+1)*8
3224 | ble <3 // No vararg slots?
3225 | add TMP2, RA, TMP1
3227 | addi MULTRES, TMP1, 8
3232 | evstdd TMP0, 0(RA)
3235 | blt <6 // More vararg slots?
3238 |7: // Grow stack for varargs.
3241 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
3245 | srwi CARG2, TMP1, 3
3246 | bl extern lj_state_growstack // (lua_State *L, int n)
3249 | add RC, BASE, SAVE0
3250 | subi TMP3, BASE, 8
3254 /* -- Returns ----------------------------------------------------------- */
3257 | // RA = results*8, RD = extra_nresults*8
3258 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
3259 | // Fall through. Assumes BC_RET follows.
3263 | // RA = results*8, RD = (nresults+1)*8
3264 | lwz PC, FRAME_PC(BASE)
3268 | andi. TMP0, PC, FRAME_TYPE
3269 | xori TMP1, PC, FRAME_VARG
3273 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
3276 | subi TMP2, BASE, 8
3278 | decode_RB8 RB, INS
3282 | addi TMP3, TMP1, 8
3283 | evlddx TMP0, RA, TMP1
3285 | evstddx TMP0, TMP2, TMP1
3287 | addi TMP1, TMP3, 8
3288 | evlddx TMP0, RA, TMP3
3290 | evstddx TMP0, TMP2, TMP3
3295 | decode_RA8 RA, INS
3297 | sub BASE, TMP2, RA
3298 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
3300 | lwz TMP1, LFUNC:TMP1->pc
3301 | lwz KBASE, PC2PROTO(k)(TMP1)
3304 |6: // Fill up results with nil.
3307 | evstddx TISNIL, TMP2, TMP1
3310 |->BC_RETV_Z: // Non-standard return case.
3311 | andi. TMP2, TMP1, FRAME_TYPEP
3313 | // Return from vararg function: relocate BASE down.
3314 | sub BASE, BASE, TMP1
3315 | lwz PC, FRAME_PC(BASE)
3319 case BC_RET0: case BC_RET1:
3320 | // RA = results*8, RD = (nresults+1)*8
3321 | lwz PC, FRAME_PC(BASE)
3324 | andi. TMP0, PC, FRAME_TYPE
3325 | xori TMP1, PC, FRAME_VARG
3329 | subi TMP2, BASE, 8
3330 | decode_RB8 RB, INS
3331 if (op == BC_RET1) {
3333 | evstdd TMP0, 0(TMP2)
3337 | decode_RA8 RA, INS
3339 | sub BASE, TMP2, RA
3340 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
3342 | lwz TMP1, LFUNC:TMP1->pc
3343 | lwz KBASE, PC2PROTO(k)(TMP1)
3346 |6: // Fill up results with nil.
3349 | evstddx TISNIL, TMP2, TMP1
3353 /* -- Loops and branches ------------------------------------------------ */
3359 | // Fall through. Assumes BC_IFORL follows.
3369 | // RA = base*8, RD = target (after end of loop or start of loop)
3370 vk = (op == BC_IFORL || op == BC_JFORL);
3372 | evldd TMP1, FORL_IDX*8(RA)
3373 | evldd TMP3, FORL_STEP*8(RA)
3374 | evldd TMP2, FORL_STOP*8(RA)
3376 | evcmpgtu cr0, TMP1, TISNUM
3377 | evcmpgtu cr7, TMP3, TISNUM
3378 | evcmpgtu cr1, TMP2, TISNUM
3379 | cror 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
3380 | cror 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3384 | efdadd TMP1, TMP1, TMP3
3385 | evstdd TMP1, FORL_IDX*8(RA)
3387 | evcmpgts TMP3, TISNIL
3388 | evstdd TMP1, FORL_EXT*8(RA)
3390 | efdcmpgt TMP1, TMP2
3392 if (op != BC_JFORL) {
3395 if (op == BC_JFORI) {
3396 | addis PC, RD, -(BCBIAS_J*4 >> 16)
3398 | addis RD, RD, -(BCBIAS_J*4 >> 16)
3401 if (op == BC_FORI) {
3403 } else if (op == BC_IFORL) {
3410 | efdcmpgt TMP2, TMP1
3418 | // Fall through. Assumes BC_IITERL follows.
3426 | // RA = base*8, RD = target
3427 | evlddx TMP1, BASE, RA
3430 | checkok >1 // Stop if iterator returned nil.
3431 if (op == BC_JITERL) {
3434 | branch_RD // Otherwise save control var + branch.
3435 | evstddx TMP1, BASE, RA
3442 | // RA = base*8, RD = target (loop extent)
3443 | // Note: RA/RD is only used by trace recorder to determine scope/extent
3444 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
3448 | // Fall through. Assumes BC_ILOOP follows.
3452 | // RA = base*8, RD = target (loop extent)
3463 | // RA = base*8 (only used by trace recorder), RD = target
3468 /* -- Function headers -------------------------------------------------- */
3474 case BC_FUNCV: /* NYI: compiled vararg functions. */
3475 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
3483 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
3484 | lwz TMP2, L->maxstack
3485 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
3486 | lwz KBASE, -4+PC2PROTO(k)(PC)
3488 | slwi TMP1, TMP1, 3
3489 | bgt ->vm_growstack_l
3492 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
3494 if (op == BC_JFUNCF) {
3500 |3: // Clear missing parameters.
3501 | evstddx TISNIL, BASE, NARGS8:RC
3502 | addi NARGS8:RC, NARGS8:RC, 8
3510 | NYI // NYI: compiled vararg functions
3511 break; /* NYI: compiled vararg functions. */
3514 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
3515 | lwz TMP2, L->maxstack
3516 | add TMP1, BASE, RC
3518 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
3519 | addi TMP3, RC, 8+FRAME_VARG
3520 | lwz KBASE, -4+PC2PROTO(k)(PC)
3522 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
3523 | bge ->vm_growstack_l
3524 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
3529 | addi BASE, TMP1, 8
3532 | cmplw RA, RC // Less args than parameters?
3535 | evstdd TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
3538 | addic. TMP2, TMP2, -1
3539 | evstdd TMP0, 8(TMP1)
3540 | addi TMP1, TMP1, 8
3545 |4: // Clear missing parameters.
3552 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
3553 if (op == BC_FUNCC) {
3554 | lwz TMP3, CFUNC:RB->f
3556 | lwz TMP3, DISPATCH_GL(wrapf)(DISPATCH)
3558 | add TMP1, RA, NARGS8:RC
3559 | lwz TMP2, L->maxstack
3560 | add RC, BASE, NARGS8:RC
3566 if (op == BC_FUNCCW) {
3567 | lwz CARG2, CFUNC:RB->f
3570 | bgt ->vm_growstack_c // Need to grow stack.
3572 | bctrl // (lua_State *L [, lua_CFunction f])
3573 | // Returns nresults.
3578 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
3579 | sub RA, TMP1, RD // RA = L->top - nresults*8
3584 /* ---------------------------------------------------------------------- */
3587 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
3593 static int build_backend(BuildCtx *ctx)
3597 dasm_growpc(Dst, BC__MAX);
3599 build_subroutines(ctx);
3602 for (op = 0; op < BC__MAX; op++)
3603 build_ins(ctx, (BCOp)op, op);
3608 /* Emit pseudo frame-info for all assembler functions. */
3609 static void emit_asm_debug(BuildCtx *ctx)
3612 switch (ctx->mode) {
3614 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
3617 "\t.long .LECIE0-.LSCIE0\n"
3619 "\t.long 0xffffffff\n"
3625 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
3630 "\t.long .LEFDE0-.LASFDE0\n"
3632 "\t.long .Lframe0\n"
3635 "\t.byte 0xe\n\t.uleb128 %d\n"
3636 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
3637 "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n",
3638 (int)ctx->codesz, CFRAME_SIZE);
3639 for (i = 14; i <= 31; i++)
3641 "\t.byte %d\n\t.uleb128 %d\n"
3642 "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n",
3643 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i));
3647 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
3650 "\t.long .LECIE1-.LSCIE1\n"
3654 "\t.string \"zPR\"\n"
3658 "\t.uleb128 6\n" /* augmentation length */
3659 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3660 "\t.long lj_err_unwind_dwarf-.\n"
3661 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3662 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
3667 "\t.long .LEFDE1-.LASFDE1\n"
3669 "\t.long .LASFDE1-.Lframe1\n"
3670 "\t.long .Lbegin-.\n"
3672 "\t.uleb128 0\n" /* augmentation length */
3673 "\t.byte 0xe\n\t.uleb128 %d\n"
3674 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
3675 "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n",
3676 (int)ctx->codesz, CFRAME_SIZE);
3677 for (i = 14; i <= 31; i++)
3679 "\t.byte %d\n\t.uleb128 %d\n"
3680 "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n",
3681 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i));