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 |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
2056 |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
2057 |// and basic math functions. ORDER ARITH
2059 | evmergelo CARG2, CARG1, CARG2
2061 | evmergelo CARG4, CARG3, CARG4
2063 | efdadd CRET2, CARG2, CARG4; evmergehi CRET1, CRET2, CRET2; blr
2065 | efdsub CRET2, CARG2, CARG4; evmergehi CRET1, CRET2, CRET2; blr
2067 | cmplwi CARG5, 3; beq >1; bgt >2
2068 | efdmul CRET2, CARG2, CARG4; evmergehi CRET1, CRET2, CRET2; blr
2070 | efddiv CRET2, CARG2, CARG4; evmergehi CRET1, CRET2, CRET2; blr
2072 | cmplwi CARG5, 5; beq >1; bgt >2
2073 | evmr CARG3, CARG2; efddiv CRET2, CARG2, CARG4; evmr RB, CARG4
2074 | mflr RC; bl ->vm_floor_efd; mtlr RC
2075 | efdmul CRET2, CRET2, RB; efdsub CRET2, CARG3, CRET2
2076 | evmergehi CRET1, CRET2, CRET2; blr
2080 | cmplwi CARG5, 7; beq >1; bgt >2
2081 | xoris CARG1, CARG1, 0x8000; blr
2083 | rlwinm CARG1, CARG1, 0, 1, 31; blr
2085 | NYI // Other operations only needed by JIT compiler.
2087 |//-----------------------------------------------------------------------
2088 |//-- Miscellaneous functions --------------------------------------------
2089 |//-----------------------------------------------------------------------
2091 |//-----------------------------------------------------------------------
2092 |//-- FFI helper functions -----------------------------------------------
2093 |//-----------------------------------------------------------------------
2100 |//-----------------------------------------------------------------------
2103 /* Generate the code for a single instruction. */
2104 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2111 /* -- Comparison ops ---------------------------------------------------- */
2113 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2115 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2116 | // RA = src1*8, RD = src2*8, JMP with RD = target
2117 | evlddx TMP0, BASE, RA
2119 | evlddx TMP1, BASE, RD
2120 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2122 | evmergehi RB, TMP0, TMP1
2123 | decode_RD4 TMP2, TMP2
2125 | add TMP2, TMP2, TMP3
2126 | checkanyfail ->vmeta_comp
2127 | efdcmplt TMP0, TMP1
2128 if (op == BC_ISLE || op == BC_ISGT) {
2129 | efdcmpeq cr1, TMP0, TMP1
2130 | cror 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2132 if (op == BC_ISLT || op == BC_ISLE) {
2133 | iselgt PC, TMP2, PC
2135 | iselgt PC, PC, TMP2
2140 case BC_ISEQV: case BC_ISNEV:
2141 vk = op == BC_ISEQV;
2142 | // RA = src1*8, RD = src2*8, JMP with RD = target
2143 | evlddx CARG2, BASE, RA
2145 | evlddx CARG3, BASE, RD
2146 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2148 | evmergehi RB, CARG2, CARG3
2149 | decode_RD4 TMP2, TMP2
2151 | add TMP2, TMP2, TMP3
2153 | efdcmpeq CARG2, CARG3
2155 | iselgt PC, TMP2, PC
2157 | iselgt PC, PC, TMP2
2162 |5: // Either or both types are not numbers.
2163 | evcmpeq CARG2, CARG3
2165 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
2166 | crorc 4*cr7+lt, 4*cr0+so, 4*cr0+lt // 1: Same tv or different type.
2167 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
2168 | crandc 4*cr7+gt, 4*cr0+lt, 4*cr1+gt // 2: Same type and primitive.
2171 | isel PC, TMP2, PC, 4*cr7+gt
2173 | isel TMP2, PC, TMP2, 4*cr7+gt
2175 | cror 4*cr7+lt, 4*cr7+lt, 4*cr7+gt // 1 or 2.
2177 | isel PC, TMP2, PC, 4*cr0+so
2179 | isel PC, PC, TMP2, 4*cr0+so
2181 | blt cr7, <1 // Done if 1 or 2.
2182 | blt cr6, <1 // Done if not tab/ud.
2184 | // Different tables or userdatas. Need to check __eq metamethod.
2185 | // Field metatable must be at same offset for GCtab and GCudata!
2186 | lwz TAB:TMP2, TAB:CARG2->metatable
2187 | li CARG4, 1-vk // ne = 0 or 1.
2188 | cmplwi TAB:TMP2, 0
2189 | beq <1 // No metatable?
2190 | lbz TMP2, TAB:TMP2->nomm
2191 | andi. TMP2, TMP2, 1<<MM_eq
2192 | bne <1 // Or 'no __eq' flag set?
2193 | mr PC, SAVE0 // Restore old PC.
2194 | b ->vmeta_equal // Handle __eq metamethod.
2197 case BC_ISEQS: case BC_ISNES:
2198 vk = op == BC_ISEQS;
2199 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
2200 | evlddx TMP0, BASE, RA
2205 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
2206 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2207 | decode_RD4 TMP2, INS
2208 | evmergelo STR:TMP1, TISSTR, STR:TMP1
2209 | add TMP2, TMP2, TMP3
2210 | evcmpeq TMP0, STR:TMP1
2212 | isel PC, TMP2, PC, 4*cr0+so
2214 | isel PC, PC, TMP2, 4*cr0+so
2219 case BC_ISEQN: case BC_ISNEN:
2220 vk = op == BC_ISEQN;
2221 | // RA = src*8, RD = num_const*8, JMP with RD = target
2222 | evlddx TMP0, BASE, RA
2224 | evlddx TMP1, KBASE, RD
2225 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2229 | efdcmpeq TMP0, TMP1
2231 | decode_RD4 TMP2, INS
2232 | add TMP2, TMP2, TMP3
2234 | iselgt PC, TMP2, PC
2237 | iselgt PC, PC, TMP2
2243 | decode_RD4 TMP2, INS
2244 | add PC, TMP2, TMP3
2249 case BC_ISEQP: case BC_ISNEP:
2250 vk = op == BC_ISEQP;
2251 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
2252 | lwzx TMP0, BASE, RA
2257 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2259 | decode_RD4 TMP2, INS
2260 | add TMP2, TMP2, TMP3
2262 | iseleq PC, TMP2, PC
2264 | iseleq PC, PC, TMP2
2269 /* -- Unary test and copy ops ------------------------------------------- */
2271 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2272 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
2273 | evlddx TMP0, BASE, RD
2274 | evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE.
2276 | evcmpltu TMP0, TMP1
2278 if (op == BC_IST || op == BC_ISF) {
2279 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
2280 | decode_RD4 TMP2, INS
2281 | add TMP2, TMP2, TMP3
2283 | isellt PC, TMP2, PC
2285 | isellt PC, PC, TMP2
2288 if (op == BC_ISTC) {
2293 | addis PC, PC, -(BCBIAS_J*4 >> 16)
2294 | decode_RD4 TMP2, INS
2295 | evstddx TMP0, BASE, RA
2302 /* -- Unary ops --------------------------------------------------------- */
2305 | // RA = dst*8, RD = src*8
2307 | evlddx TMP0, BASE, RD
2308 | evstddx TMP0, BASE, RA
2312 | // RA = dst*8, RD = src*8
2314 | lwzx TMP0, BASE, RD
2315 | subfic TMP1, TMP0, LJ_TTRUE
2316 | adde TMP0, TMP0, TMP1
2317 | stwx TMP0, BASE, RA
2321 | // RA = dst*8, RD = src*8
2322 | evlddx TMP0, BASE, RD
2324 | checkfail ->vmeta_unm
2327 | evstddx TMP0, BASE, RA
2331 | // RA = dst*8, RD = src*8
2332 | evlddx CARG1, BASE, RD
2335 | lwz CRET1, STR:CARG1->len
2338 | efdcfsi TMP0, CRET1
2339 | evstddx TMP0, BASE, RA
2343 | checkfail ->vmeta_len
2345 | lwz TAB:TMP2, TAB:CARG1->metatable
2346 | cmplwi TAB:TMP2, 0
2351 | bl extern lj_tab_len // (GCtab *t)
2352 | // Returns uint32_t (but less than 2^31).
2356 | lbz TMP0, TAB:TMP2->nomm
2357 | andi. TMP0, TMP0, 1<<MM_len
2358 | bne <3 // 'no __len' flag set: done.
2363 /* -- Binary ops -------------------------------------------------------- */
2365 |.macro ins_arithpre, t0, t1
2366 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
2367 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
2370 | evlddx t0, BASE, RB
2372 | evlddx t1, KBASE, RC
2373 | checkfail ->vmeta_arith_vn
2376 | evlddx t1, BASE, RB
2378 | evlddx t0, KBASE, RC
2379 | checkfail ->vmeta_arith_nv
2382 | evlddx t0, BASE, RB
2383 | evlddx t1, BASE, RC
2384 | evmergehi TMP2, t0, t1
2386 | checkanyfail ->vmeta_arith_vv
2391 |.macro ins_arith, ins
2392 | ins_arithpre TMP0, TMP1
2394 | ins TMP0, TMP0, TMP1
2395 | evstddx TMP0, BASE, RA
2399 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
2402 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
2405 case BC_MULVN: case BC_MULNV: case BC_MULVV:
2408 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
2412 | ins_arithpre RD, SAVE0
2414 | efddiv CARG2, RD, SAVE0
2415 | bl ->vm_floor_efd // floor(b/c)
2416 | efdmul TMP0, CRET2, SAVE0
2418 | efdsub TMP0, RD, TMP0 // b - floor(b/c)*c
2419 | evstddx TMP0, BASE, RA
2422 case BC_MODNV: case BC_MODVV:
2423 | ins_arithpre RD, SAVE0
2424 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
2427 | evlddx CARG2, BASE, RB
2428 | evlddx CARG4, BASE, RC
2429 | evmergehi CARG1, CARG4, CARG2
2431 | evmergehi CARG3, CARG4, CARG4
2432 | checkanyfail ->vmeta_arith_vv
2434 | evmergelo CRET2, CRET1, CRET2
2435 | evstddx CRET2, BASE, RA
2440 | // RA = dst*8, RB = src_start*8, RC = src_end*8
2443 | add CARG2, BASE, RC
2448 | srwi CARG3, CARG3, 3
2449 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
2450 | // Returns NULL (finished) or TValue * (metamethod).
2454 | evlddx TMP0, BASE, SAVE0 // Copy result from RB to RA.
2455 | evstddx TMP0, BASE, RA
2459 /* -- Constant ops ------------------------------------------------------ */
2462 | // RA = dst*8, RD = str_const*8 (~)
2465 | subfic TMP1, TMP1, -4
2466 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
2467 | evmergelo TMP0, TISSTR, TMP0
2468 | evstddx TMP0, BASE, RA
2473 | // RA = dst*8, RD = cdata_const*8 (~)
2476 | subfic TMP1, TMP1, -4
2477 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
2478 | li TMP2, LJ_TCDATA
2479 | evmergelo TMP0, TMP2, TMP0
2480 | evstddx TMP0, BASE, RA
2485 | // RA = dst*8, RD = int16_literal*8
2489 | efdcfsi TMP0, TMP1
2490 | evstddx TMP0, BASE, RA
2494 | // RA = dst*8, RD = num_const*8
2495 | evlddx TMP0, KBASE, RD
2497 | evstddx TMP0, BASE, RA
2501 | // RA = dst*8, RD = primitive_type*8 (~)
2505 | stwx TMP0, BASE, RA
2509 | // RA = base*8, RD = end*8
2510 | evstddx TISNIL, BASE, RA
2513 | evstddx TISNIL, BASE, RA
2520 /* -- Upvalue and function ops ------------------------------------------ */
2523 | // RA = dst*8, RD = uvnum*8
2525 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2527 | addi RD, RD, offsetof(GCfuncL, uvptr)
2528 | lwzx UPVAL:RB, LFUNC:RB, RD
2529 | lwz TMP1, UPVAL:RB->v
2530 | evldd TMP0, 0(TMP1)
2531 | evstddx TMP0, BASE, RA
2535 | // RA = uvnum*8, RD = src*8
2536 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2538 | addi RA, RA, offsetof(GCfuncL, uvptr)
2539 | evlddx TMP1, BASE, RD
2540 | lwzx UPVAL:RB, LFUNC:RB, RA
2541 | lbz TMP3, UPVAL:RB->marked
2542 | lwz CARG2, UPVAL:RB->v
2543 | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
2544 | lbz TMP0, UPVAL:RB->closed
2545 | evmergehi TMP2, TMP1, TMP1
2546 | evstdd TMP1, 0(CARG2)
2547 | cmplwi cr1, TMP0, 0
2548 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
2549 | subi TMP2, TMP2, (LJ_TISNUM+1)
2550 | bne >2 // Upvalue is closed and black?
2554 |2: // Check if new value is collectable.
2555 | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1)
2556 | bge <1 // tvisgcv(v)
2557 | lbz TMP3, GCOBJ:TMP1->gch.marked
2558 | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
2559 | la CARG1, GG_DISP2G(DISPATCH)
2560 | // Crossed a write barrier. Move the barrier forward.
2562 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2566 | // RA = uvnum*8, RD = str_const*8 (~)
2567 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2570 | subfic TMP1, TMP1, -4
2571 | addi RA, RA, offsetof(GCfuncL, uvptr)
2572 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
2573 | lwzx UPVAL:RB, LFUNC:RB, RA
2574 | evmergelo STR:TMP1, TISSTR, STR:TMP1
2575 | lbz TMP3, UPVAL:RB->marked
2576 | lwz CARG2, UPVAL:RB->v
2577 | andi. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
2578 | lbz TMP3, STR:TMP1->marked
2579 | lbz TMP2, UPVAL:RB->closed
2580 | evstdd STR:TMP1, 0(CARG2)
2585 |2: // Check if string is white and ensure upvalue is closed.
2586 | andi. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
2587 | cmplwi cr1, TMP2, 0
2588 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
2589 | la CARG1, GG_DISP2G(DISPATCH)
2590 | // Crossed a write barrier. Move the barrier forward.
2592 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
2596 | // RA = uvnum*8, RD = num_const*8
2598 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2600 | addi RA, RA, offsetof(GCfuncL, uvptr)
2601 | evlddx TMP0, KBASE, RD
2602 | lwzx UPVAL:RB, LFUNC:RB, RA
2603 | lwz TMP1, UPVAL:RB->v
2604 | evstdd TMP0, 0(TMP1)
2608 | // RA = uvnum*8, RD = primitive_type*8 (~)
2610 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2612 | addi RA, RA, offsetof(GCfuncL, uvptr)
2614 | lwzx UPVAL:RB, LFUNC:RB, RA
2616 | lwz TMP1, UPVAL:RB->v
2622 | // RA = level*8, RD = target
2623 | lwz TMP1, L->openupval
2624 | branch_RD // Do this first since RD is not saved.
2629 | add CARG2, BASE, RA
2630 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
2637 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
2640 | subfic TMP1, TMP1, -4
2642 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
2644 | lwz CARG3, FRAME_FUNC(BASE)
2645 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
2646 | bl extern lj_func_newL_gc
2647 | // Returns GCfuncL *.
2649 | evmergelo LFUNC:CRET1, TISFUNC, LFUNC:CRET1
2650 | evstddx LFUNC:CRET1, BASE, RA
2654 /* -- Table ops --------------------------------------------------------- */
2658 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
2659 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
2661 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
2667 if (op == BC_TNEW) {
2668 | rlwinm CARG2, RD, 29, 21, 31
2669 | rlwinm CARG3, RD, 18, 27, 31
2670 | cmpwi CARG2, 0x7ff
2672 | iseleq CARG2, TMP1, CARG2
2673 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
2674 | // Returns Table *.
2677 | subfic TMP1, TMP1, -4
2678 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
2679 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
2680 | // Returns Table *.
2683 | evmergelo TAB:CRET1, TISTAB, TAB:CRET1
2684 | evstddx TAB:CRET1, BASE, RA
2688 | bl extern lj_gc_step_fixtop // (lua_State *L)
2695 | // RA = dst*8, RD = str_const*8 (~)
2697 | // RA = src*8, RD = str_const*8 (~)
2698 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
2700 | lwz TAB:RB, LFUNC:TMP2->env
2701 | subfic TMP1, TMP1, -4
2702 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2703 if (op == BC_GGET) {
2711 | // RA = dst*8, RB = table*8, RC = key*8
2712 | evlddx TAB:RB, BASE, RB
2713 | evlddx RC, BASE, RC
2715 | checkfail ->vmeta_tgetv
2718 | // Convert number key to integer
2720 | lwz TMP0, TAB:RB->asize
2721 | efdcfsi TMP1, TMP2
2722 | cmplw cr0, TMP0, TMP2
2723 | efdcmpeq cr1, RC, TMP1
2724 | lwz TMP1, TAB:RB->array
2725 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2726 | slwi TMP2, TMP2, 3
2727 | ble ->vmeta_tgetv // Integer key and in array part?
2728 | evlddx TMP1, TMP1, TMP2
2732 | evstddx TMP1, BASE, RA
2735 |2: // Check for __index if table value is nil.
2736 | lwz TAB:TMP2, TAB:RB->metatable
2737 | cmplwi TAB:TMP2, 0
2738 | beq <1 // No metatable: done.
2739 | lbz TMP0, TAB:TMP2->nomm
2740 | andi. TMP0, TMP0, 1<<MM_index
2741 | bne <1 // 'no __index' flag set: done.
2745 | checkstr STR:RC // String key?
2746 | checkok ->BC_TGETS_Z
2750 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
2751 | evlddx TAB:RB, BASE, RB
2754 | subfic TMP1, TMP1, -4
2755 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2756 | checkfail ->vmeta_tgets1
2758 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
2759 | lwz TMP0, TAB:RB->hmask
2760 | lwz TMP1, STR:RC->hash
2761 | lwz NODE:TMP2, TAB:RB->node
2762 | evmergelo STR:RC, TISSTR, STR:RC
2763 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
2764 | slwi TMP0, TMP1, 5
2765 | slwi TMP1, TMP1, 3
2766 | sub TMP1, TMP0, TMP1
2767 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
2769 | evldd TMP0, NODE:TMP2->key
2770 | evldd TMP1, NODE:TMP2->val
2771 | evcmpeq TMP0, STR:RC
2774 | checkok >5 // Key found, but nil value?
2776 | evstddx TMP1, BASE, RA
2779 |4: // Follow hash chain.
2780 | lwz NODE:TMP2, NODE:TMP2->next
2781 | cmplwi NODE:TMP2, 0
2783 | // End of hash chain: key not found, nil result.
2786 |5: // Check for __index if table value is nil.
2787 | lwz TAB:TMP2, TAB:RB->metatable
2788 | cmplwi TAB:TMP2, 0
2789 | beq <3 // No metatable: done.
2790 | lbz TMP0, TAB:TMP2->nomm
2791 | andi. TMP0, TMP0, 1<<MM_index
2792 | bne <3 // 'no __index' flag set: done.
2796 | // RA = dst*8, RB = table*8, RC = index*8
2797 | evlddx TAB:RB, BASE, RB
2800 | checkfail ->vmeta_tgetb
2801 | lwz TMP1, TAB:RB->asize
2802 | lwz TMP2, TAB:RB->array
2805 | evlddx TMP1, TMP2, RC
2810 | evstddx TMP1, BASE, RA
2813 |5: // Check for __index if table value is nil.
2814 | lwz TAB:TMP2, TAB:RB->metatable
2815 | cmplwi TAB:TMP2, 0
2816 | beq <1 // No metatable: done.
2817 | lbz TMP2, TAB:TMP2->nomm
2818 | andi. TMP2, TMP2, 1<<MM_index
2819 | bne <1 // 'no __index' flag set: done.
2820 | b ->vmeta_tgetb // Caveat: preserve TMP0!
2824 | // RA = src*8, RB = table*8, RC = key*8
2825 | evlddx TAB:RB, BASE, RB
2826 | evlddx RC, BASE, RC
2828 | checkfail ->vmeta_tsetv
2831 | // Convert number key to integer
2833 | evlddx SAVE0, BASE, RA
2834 | lwz TMP0, TAB:RB->asize
2835 | efdcfsi TMP1, TMP2
2836 | cmplw cr0, TMP0, TMP2
2837 | efdcmpeq cr1, RC, TMP1
2838 | lwz TMP1, TAB:RB->array
2839 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+gt
2840 | slwi TMP0, TMP2, 3
2841 | ble ->vmeta_tsetv // Integer key and in array part?
2842 | lbz TMP3, TAB:RB->marked
2843 | evlddx TMP2, TMP1, TMP0
2847 | andi. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
2848 | evstddx SAVE0, TMP1, TMP0
2853 |3: // Check for __newindex if previous value is nil.
2854 | lwz TAB:TMP2, TAB:RB->metatable
2855 | cmplwi TAB:TMP2, 0
2856 | beq <1 // No metatable: done.
2857 | lbz TMP2, TAB:TMP2->nomm
2858 | andi. TMP2, TMP2, 1<<MM_newindex
2859 | bne <1 // 'no __newindex' flag set: done.
2863 | checkstr STR:RC // String key?
2864 | checkok ->BC_TSETS_Z
2867 |7: // Possible table write barrier for the value. Skip valiswhite check.
2868 | barrierback TAB:RB, TMP3, TMP0
2872 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
2873 | evlddx TAB:RB, BASE, RB
2876 | subfic TMP1, TMP1, -4
2877 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
2878 | checkfail ->vmeta_tsets1
2880 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
2881 | lwz TMP0, TAB:RB->hmask
2882 | lwz TMP1, STR:RC->hash
2883 | lwz NODE:TMP2, TAB:RB->node
2884 | evmergelo STR:RC, TISSTR, STR:RC
2885 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
2886 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
2887 | evlddx SAVE0, BASE, RA
2888 | slwi TMP0, TMP1, 5
2889 | slwi TMP1, TMP1, 3
2890 | sub TMP1, TMP0, TMP1
2891 | lbz TMP3, TAB:RB->marked
2892 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
2894 | evldd TMP0, NODE:TMP2->key
2895 | evldd TMP1, NODE:TMP2->val
2896 | evcmpeq TMP0, STR:RC
2899 | checkok >4 // Key found, but nil value?
2901 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
2902 | evstdd SAVE0, NODE:TMP2->val
2907 |4: // Check for __newindex if previous value is nil.
2908 | lwz TAB:TMP1, TAB:RB->metatable
2909 | cmplwi TAB:TMP1, 0
2910 | beq <2 // No metatable: done.
2911 | lbz TMP0, TAB:TMP1->nomm
2912 | andi. TMP0, TMP0, 1<<MM_newindex
2913 | bne <2 // 'no __newindex' flag set: done.
2916 |5: // Follow hash chain.
2917 | lwz NODE:TMP2, NODE:TMP2->next
2918 | cmplwi NODE:TMP2, 0
2920 | // End of hash chain: key not found, add a new one.
2922 | // But check for __newindex first.
2923 | lwz TAB:TMP1, TAB:RB->metatable
2924 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
2927 | cmplwi TAB:TMP1, 0
2929 | beq >6 // No metatable: continue.
2930 | lbz TMP0, TAB:TMP1->nomm
2931 | andi. TMP0, TMP0, 1<<MM_newindex
2932 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
2935 | evstdd STR:RC, 0(CARG3)
2936 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
2937 | // Returns TValue *.
2939 | evstdd SAVE0, 0(CRET1)
2940 | b <3 // No 2nd write barrier needed.
2942 |7: // Possible table write barrier for the value. Skip valiswhite check.
2943 | barrierback TAB:RB, TMP3, TMP0
2947 | // RA = src*8, RB = table*8, RC = index*8
2948 | evlddx TAB:RB, BASE, RB
2951 | checkfail ->vmeta_tsetb
2952 | lwz TMP1, TAB:RB->asize
2953 | lwz TMP2, TAB:RB->array
2954 | lbz TMP3, TAB:RB->marked
2956 | evlddx SAVE0, BASE, RA
2958 | evlddx TMP1, TMP2, RC
2962 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
2963 | evstddx SAVE0, TMP2, RC
2968 |5: // Check for __newindex if previous value is nil.
2969 | lwz TAB:TMP1, TAB:RB->metatable
2970 | cmplwi TAB:TMP1, 0
2971 | beq <1 // No metatable: done.
2972 | lbz TMP1, TAB:TMP1->nomm
2973 | andi. TMP1, TMP1, 1<<MM_newindex
2974 | bne <1 // 'no __newindex' flag set: done.
2975 | b ->vmeta_tsetb // Caveat: preserve TMP0!
2977 |7: // Possible table write barrier for the value. Skip valiswhite check.
2978 | barrierback TAB:RB, TMP3, TMP0
2983 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
2986 | add TMP3, KBASE, RD
2987 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
2988 | addic. TMP0, MULTRES, -8
2989 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
2990 | srwi CARG3, TMP0, 3
2991 | beq >4 // Nothing to copy?
2992 | add CARG3, CARG3, TMP3
2993 | lwz TMP2, TAB:CARG2->asize
2994 | slwi TMP1, TMP3, 3
2995 | lbz TMP3, TAB:CARG2->marked
2997 | add TMP2, RA, TMP0
2998 | lwz TMP0, TAB:CARG2->array
3000 | add TMP1, TMP1, TMP0
3001 | andi. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
3002 |3: // Copy result slots to table.
3005 | cmpw cr1, RA, TMP2
3006 | evstdd TMP0, 0(TMP1)
3007 | addi TMP1, TMP1, 8
3013 |5: // Need to resize array part.
3018 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3019 | // Must not reallocate the stack.
3023 |7: // Possible table write barrier for any value. Skip valiswhite check.
3024 | barrierback TAB:CARG2, TMP3, TMP0
3028 /* -- Calls and vararg handling ----------------------------------------- */
3031 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
3032 | add NARGS8:RC, NARGS8:RC, MULTRES
3033 | // Fall through. Assumes BC_CALL follows.
3036 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
3037 | evlddx LFUNC:RB, BASE, RA
3039 | add BASE, BASE, RA
3040 | subi NARGS8:RC, NARGS8:RC, 8
3041 | checkfunc LFUNC:RB
3042 | addi BASE, BASE, 8
3043 | checkfail ->vmeta_call
3048 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
3049 | add NARGS8:RC, NARGS8:RC, MULTRES
3050 | // Fall through. Assumes BC_CALLT follows.
3053 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3054 | evlddx LFUNC:RB, BASE, RA
3056 | lwz TMP1, FRAME_PC(BASE)
3057 | subi NARGS8:RC, NARGS8:RC, 8
3058 | checkfunc LFUNC:RB
3060 | checkfail ->vmeta_callt
3062 | andi. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
3063 | lbz TMP3, LFUNC:RB->ffid
3064 | xori TMP2, TMP1, FRAME_VARG
3065 | cmplwi cr1, NARGS8:RC, 0
3068 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
3070 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
3073 | addi TMP3, TMP2, 8
3074 | evlddx TMP0, RA, TMP2
3075 | cmplw cr1, TMP3, NARGS8:RC
3076 | evstddx TMP0, BASE, TMP2
3080 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
3085 |5: // Tailcall to a fast function with a Lua frame below.
3087 | decode_RA8 RA, INS
3088 | sub TMP1, BASE, RA
3089 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
3090 | lwz TMP1, LFUNC:TMP1->pc
3091 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
3094 |7: // Tailcall from a vararg function.
3095 | andi. TMP0, TMP2, FRAME_TYPEP
3096 | bne <1 // Vararg frame below?
3097 | sub BASE, BASE, TMP2 // Relocate BASE down.
3098 | lwz TMP1, FRAME_PC(BASE)
3099 | andi. TMP0, TMP1, FRAME_TYPE
3104 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
3105 | subi RA, RA, 24 // evldd doesn't support neg. offsets.
3107 | evlddx LFUNC:RB, BASE, RA
3108 | add BASE, BASE, RA
3109 | evldd TMP0, 8(BASE)
3110 | evldd TMP1, 16(BASE)
3111 | evstdd LFUNC:RB, 24(BASE) // Copy callable.
3112 | checkfunc LFUNC:RB
3113 | evstdd TMP0, 32(BASE) // Copy state.
3114 | li NARGS8:RC, 16 // Iterators get 2 arguments.
3115 | evstdd TMP1, 40(BASE) // Copy control var.
3116 | addi BASE, BASE, 32
3117 | checkfail ->vmeta_call
3122 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
3124 | // NYI: add hotloop, record BC_ITERN.
3127 | lwz TAB:RB, -12(RA)
3128 | lwz RC, -4(RA) // Get index from control var.
3129 | lwz TMP0, TAB:RB->asize
3130 | lwz TMP1, TAB:RB->array
3132 |1: // Traverse array part.
3135 | bge >5 // Index points after array part?
3136 | evlddx TMP2, TMP1, TMP3
3142 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
3143 | evstdd TMP2, 8(RA)
3144 | decode_RD4 TMP1, INS
3145 | stw RC, -4(RA) // Update control var.
3146 | add PC, TMP1, TMP3
3147 | evstdd TMP0, 0(RA)
3151 |4: // Skip holes in array part.
3155 |5: // Traverse hash part.
3156 | lwz TMP1, TAB:RB->hmask
3158 | lwz TMP2, TAB:RB->node
3160 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
3164 | sub TMP3, TMP3, RB
3165 | evlddx RB, TMP2, TMP3
3166 | add NODE:TMP3, TMP2, TMP3
3170 | evldd TMP3, NODE:TMP3->key
3171 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
3174 | decode_RD4 TMP1, INS
3175 | evstdd TMP3, 0(RA)
3177 | add PC, TMP1, TMP2
3178 | stw RC, -4(RA) // Update control var.
3181 |7: // Skip holes in hash part.
3187 | // RA = base*8, RD = target (points to ITERN)
3190 | evlddx CFUNC:TMP1, RA, TMP2
3193 | evmergehi TMP0, CFUNC:TMP1, CFUNC:TMP1
3194 | cmpwi cr0, TMP2, LJ_TTAB
3195 | cmpwi cr1, TMP0, LJ_TFUNC
3196 | cmpwi cr6, TMP3, LJ_TNIL
3198 | lbz TMP1, CFUNC:TMP1->ffid
3199 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
3200 | cmpwi cr7, TMP1, FF_next_N
3202 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
3203 | add TMP3, PC, TMP0
3206 | ori TMP1, TMP1, 0x7fff
3207 | stw ZERO, -4(RA) // Initialize control var.
3209 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
3212 |5: // Despecialize bytecode if any of the checks fail.
3216 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
3222 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
3223 | lwz TMP0, FRAME_PC(BASE)
3226 | addi RC, RC, FRAME_VARG
3228 | subi TMP3, BASE, 8 // TMP3 = vtop
3229 | sub RC, RC, TMP0 // RC = vbase
3230 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
3232 | sub. TMP1, TMP3, RC
3233 | beq cr1, >5 // Copy all varargs?
3234 | subi TMP2, TMP2, 16
3235 | ble >2 // No vararg slots?
3236 |1: // Copy vararg slots to destination slots.
3239 | evstdd TMP0, 0(RA)
3241 | cmplw cr1, RC, TMP3
3242 | bge >3 // All destination slots filled?
3244 | blt cr1, <1 // More vararg slots?
3245 |2: // Fill up remainder with nil.
3246 | evstdd TISNIL, 0(RA)
3253 |5: // Copy all varargs.
3254 | lwz TMP0, L->maxstack
3255 | li MULTRES, 8 // MULTRES = (0+1)*8
3256 | ble <3 // No vararg slots?
3257 | add TMP2, RA, TMP1
3259 | addi MULTRES, TMP1, 8
3264 | evstdd TMP0, 0(RA)
3267 | blt <6 // More vararg slots?
3270 |7: // Grow stack for varargs.
3273 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
3277 | srwi CARG2, TMP1, 3
3278 | bl extern lj_state_growstack // (lua_State *L, int n)
3281 | add RC, BASE, SAVE0
3282 | subi TMP3, BASE, 8
3286 /* -- Returns ----------------------------------------------------------- */
3289 | // RA = results*8, RD = extra_nresults*8
3290 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
3291 | // Fall through. Assumes BC_RET follows.
3295 | // RA = results*8, RD = (nresults+1)*8
3296 | lwz PC, FRAME_PC(BASE)
3300 | andi. TMP0, PC, FRAME_TYPE
3301 | xori TMP1, PC, FRAME_VARG
3305 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
3308 | subi TMP2, BASE, 8
3310 | decode_RB8 RB, INS
3314 | addi TMP3, TMP1, 8
3315 | evlddx TMP0, RA, TMP1
3317 | evstddx TMP0, TMP2, TMP1
3319 | addi TMP1, TMP3, 8
3320 | evlddx TMP0, RA, TMP3
3322 | evstddx TMP0, TMP2, TMP3
3327 | decode_RA8 RA, INS
3329 | sub BASE, TMP2, RA
3330 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
3332 | lwz TMP1, LFUNC:TMP1->pc
3333 | lwz KBASE, PC2PROTO(k)(TMP1)
3336 |6: // Fill up results with nil.
3339 | evstddx TISNIL, TMP2, TMP1
3342 |->BC_RETV_Z: // Non-standard return case.
3343 | andi. TMP2, TMP1, FRAME_TYPEP
3345 | // Return from vararg function: relocate BASE down.
3346 | sub BASE, BASE, TMP1
3347 | lwz PC, FRAME_PC(BASE)
3351 case BC_RET0: case BC_RET1:
3352 | // RA = results*8, RD = (nresults+1)*8
3353 | lwz PC, FRAME_PC(BASE)
3356 | andi. TMP0, PC, FRAME_TYPE
3357 | xori TMP1, PC, FRAME_VARG
3361 | subi TMP2, BASE, 8
3362 | decode_RB8 RB, INS
3363 if (op == BC_RET1) {
3365 | evstdd TMP0, 0(TMP2)
3369 | decode_RA8 RA, INS
3371 | sub BASE, TMP2, RA
3372 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
3374 | lwz TMP1, LFUNC:TMP1->pc
3375 | lwz KBASE, PC2PROTO(k)(TMP1)
3378 |6: // Fill up results with nil.
3381 | evstddx TISNIL, TMP2, TMP1
3385 /* -- Loops and branches ------------------------------------------------ */
3391 | // Fall through. Assumes BC_IFORL follows.
3401 | // RA = base*8, RD = target (after end of loop or start of loop)
3402 vk = (op == BC_IFORL || op == BC_JFORL);
3404 | evldd TMP1, FORL_IDX*8(RA)
3405 | evldd TMP3, FORL_STEP*8(RA)
3406 | evldd TMP2, FORL_STOP*8(RA)
3408 | evcmpgtu cr0, TMP1, TISNUM
3409 | evcmpgtu cr7, TMP3, TISNUM
3410 | evcmpgtu cr1, TMP2, TISNUM
3411 | cror 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
3412 | cror 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3416 | efdadd TMP1, TMP1, TMP3
3417 | evstdd TMP1, FORL_IDX*8(RA)
3419 | evcmpgts TMP3, TISNIL
3420 | evstdd TMP1, FORL_EXT*8(RA)
3422 | efdcmpgt TMP1, TMP2
3424 if (op != BC_JFORL) {
3427 if (op == BC_JFORI) {
3428 | addis PC, RD, -(BCBIAS_J*4 >> 16)
3430 | addis RD, RD, -(BCBIAS_J*4 >> 16)
3433 if (op == BC_FORI) {
3435 } else if (op == BC_IFORL) {
3442 | efdcmpgt TMP2, TMP1
3450 | // Fall through. Assumes BC_IITERL follows.
3458 | // RA = base*8, RD = target
3459 | evlddx TMP1, BASE, RA
3462 | checkok >1 // Stop if iterator returned nil.
3463 if (op == BC_JITERL) {
3466 | branch_RD // Otherwise save control var + branch.
3467 | evstddx TMP1, BASE, RA
3474 | // RA = base*8, RD = target (loop extent)
3475 | // Note: RA/RD is only used by trace recorder to determine scope/extent
3476 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
3480 | // Fall through. Assumes BC_ILOOP follows.
3484 | // RA = base*8, RD = target (loop extent)
3495 | // RA = base*8 (only used by trace recorder), RD = target
3500 /* -- Function headers -------------------------------------------------- */
3506 case BC_FUNCV: /* NYI: compiled vararg functions. */
3507 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
3515 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
3516 | lwz TMP2, L->maxstack
3517 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
3518 | lwz KBASE, -4+PC2PROTO(k)(PC)
3520 | slwi TMP1, TMP1, 3
3521 | bgt ->vm_growstack_l
3524 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
3526 if (op == BC_JFUNCF) {
3532 |3: // Clear missing parameters.
3533 | evstddx TISNIL, BASE, NARGS8:RC
3534 | addi NARGS8:RC, NARGS8:RC, 8
3542 | NYI // NYI: compiled vararg functions
3543 break; /* NYI: compiled vararg functions. */
3546 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
3547 | lwz TMP2, L->maxstack
3548 | add TMP1, BASE, RC
3550 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
3551 | addi TMP3, RC, 8+FRAME_VARG
3552 | lwz KBASE, -4+PC2PROTO(k)(PC)
3554 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
3555 | bge ->vm_growstack_l
3556 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
3561 | addi BASE, TMP1, 8
3564 | cmplw RA, RC // Less args than parameters?
3567 | evstdd TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
3570 | addic. TMP2, TMP2, -1
3571 | evstdd TMP0, 8(TMP1)
3572 | addi TMP1, TMP1, 8
3577 |4: // Clear missing parameters.
3584 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
3585 if (op == BC_FUNCC) {
3586 | lwz TMP3, CFUNC:RB->f
3588 | lwz TMP3, DISPATCH_GL(wrapf)(DISPATCH)
3590 | add TMP1, RA, NARGS8:RC
3591 | lwz TMP2, L->maxstack
3592 | add RC, BASE, NARGS8:RC
3598 if (op == BC_FUNCCW) {
3599 | lwz CARG2, CFUNC:RB->f
3602 | bgt ->vm_growstack_c // Need to grow stack.
3604 | bctrl // (lua_State *L [, lua_CFunction f])
3605 | // Returns nresults.
3610 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
3611 | sub RA, TMP1, RD // RA = L->top - nresults*8
3616 /* ---------------------------------------------------------------------- */
3619 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
3625 static int build_backend(BuildCtx *ctx)
3629 dasm_growpc(Dst, BC__MAX);
3631 build_subroutines(ctx);
3634 for (op = 0; op < BC__MAX; op++)
3635 build_ins(ctx, (BCOp)op, op);
3640 /* Emit pseudo frame-info for all assembler functions. */
3641 static void emit_asm_debug(BuildCtx *ctx)
3644 switch (ctx->mode) {
3646 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
3649 "\t.long .LECIE0-.LSCIE0\n"
3651 "\t.long 0xffffffff\n"
3657 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
3662 "\t.long .LEFDE0-.LASFDE0\n"
3664 "\t.long .Lframe0\n"
3667 "\t.byte 0xe\n\t.uleb128 %d\n"
3668 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
3669 "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n",
3670 (int)ctx->codesz, CFRAME_SIZE);
3671 for (i = 14; i <= 31; i++)
3673 "\t.byte %d\n\t.uleb128 %d\n"
3674 "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n",
3675 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i));
3679 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
3682 "\t.long .LECIE1-.LSCIE1\n"
3686 "\t.string \"zPR\"\n"
3690 "\t.uleb128 6\n" /* augmentation length */
3691 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3692 "\t.long lj_err_unwind_dwarf-.\n"
3693 "\t.byte 0x1b\n" /* pcrel|sdata4 */
3694 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
3699 "\t.long .LEFDE1-.LASFDE1\n"
3701 "\t.long .LASFDE1-.Lframe1\n"
3702 "\t.long .Lbegin-.\n"
3704 "\t.uleb128 0\n" /* augmentation length */
3705 "\t.byte 0xe\n\t.uleb128 %d\n"
3706 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
3707 "\t.byte 0x5\n\t.uleb128 70\n\t.sleb128 37\n",
3708 (int)ctx->codesz, CFRAME_SIZE);
3709 for (i = 14; i <= 31; i++)
3711 "\t.byte %d\n\t.uleb128 %d\n"
3712 "\t.byte 5\n\t.uleb128 %d\n\t.uleb128 %d\n",
3713 0x80+i, 1+2*(31-i), 1200+i, 2+2*(31-i));