1 |// Low-level VM code for PowerPC 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 |// DynASM defines used by the PPC port:
20 |// P64 64 bit pointers (only for GPR64 testing).
21 |// Note: a full PPC64 _LP64 port is not planned.
22 |// GPR64 64 bit registers (but possibly 32 bit pointers, e.g. PS3).
23 |// Affects reg saves, stack layout, carry/overflow/dot flags etc.
24 |// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3).
25 |// Function pointers are really a struct: code, TOC, env (optional).
26 |// TOCENV Function pointers have an environment pointer, too (not on PS3).
27 |// PPE Power Processor Element of Cell (PS3) or Xenon (XBox 360).
28 |// Must avoid (slow) micro-coded instructions.
33 |.macro lpx, a, b, c; ldx a, b, c; .endmacro
34 |.macro lp, a, b; ld a, b; .endmacro
35 |.macro stp, a, b; std a, b; .endmacro
36 |.define decode_OPP, decode_OP8
38 |// Missing: Calling conventions, 64 bit regs, TOC.
39 |.error lib_ffi not yet implemented for PPC64
42 |.macro lpx, a, b, c; lwzx a, b, c; .endmacro
43 |.macro lp, a, b; lwz a, b; .endmacro
44 |.macro stp, a, b; stw a, b; .endmacro
45 |.define decode_OPP, decode_OP4
48 |// Convenience macros for TOC handling.
50 |// Linker needs a TOC patch area for every external call relocation.
51 |.macro blex, target; bl extern target; nop; .endmacro
52 |.macro .toc, a, b; a, b; .endmacro
61 |.macro blex, target; bl extern target; .endmacro
62 |.macro .toc, a, b; .endmacro
64 |.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro
66 |.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro
68 |.macro andix., y, a, i
70 | rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i)
77 |//-----------------------------------------------------------------------
79 |// Fixed register assignments for the interpreter.
80 |// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA)
82 |// The following must be C callee-save (but BASE is often refetched).
83 |.define BASE, r14 // Base of current Lua stack frame.
84 |.define KBASE, r15 // Constants of current Lua function.
85 |.define PC, r16 // Next PC.
86 |.define DISPATCH, r17 // Opcode dispatch table.
87 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
88 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
89 |.define JGL, r31 // On-trace: global_State + 32768.
91 |// Constants for type-comparisons, stores and conversions. C callee-save.
95 |.define TOBIT, f30 // 2^52 + 2^51.
96 |.define TONUM, f31 // 2^52 + 2^51 + 2^31.
98 |// The following temporaries are not saved across C calls, except for RA.
99 |.define RA, r20 // Callee-save.
103 |.define INS, r7 // Overlaps CARG5.
108 |.define TMP3, r6 // Overlaps CARG4.
110 |// Saved temporaries.
113 |// Calling conventions.
117 |.define CARG4, r6 // Overlaps TMP3.
118 |.define CARG5, r7 // Overlaps INS.
126 |.define TOCREG, r2 // TOC register (only used by C code).
127 |.define ENVREG, r11 // Environment pointer (nested C functions).
129 |// Stack layout while in interpreter. Must match with lj_frame.h.
132 |// 508(sp) // \ 32 bit C frame info.
133 |.define SAVE_ERRF, 472(sp) // |
134 |.define SAVE_NRES, 468(sp) // |
135 |.define SAVE_L, 464(sp) // > Parameter save area.
136 |.define SAVE_PC, 460(sp) // |
137 |.define SAVE_MULTRES, 456(sp) // |
138 |.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain.
139 |.define SAVE_LR, 416(sp)
140 |.define CFRAME_SPACE, 400 // Delta for sp.
141 |// Back chain for sp: 400(sp) <-- sp entering interpreter
142 |.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves.
143 |.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves.
144 |// 48(sp) // Callee parameter save area (ABI mandated).
145 |.define SAVE_TOC, 40(sp) // TOC save area.
146 |.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated).
147 |.define TMPD_HI, 32(sp) // /
148 |.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated).
149 |.define TONUM_HI, 24(sp) // /
150 |// Next frame lr: 16(sp)
151 |.define SAVE_CR, 8(sp) // 64 bit CR save.
152 |// Back chain for sp: 0(sp) <-- sp while in interpreter
154 |.define TMPD_BLO, 39(sp)
155 |.define TMPD, TMPD_HI
156 |.define TONUM_D, TONUM_HI
160 |.define SAVE_LR, 276(sp)
161 |.define CFRAME_SPACE, 272 // Delta for sp.
162 |// Back chain for sp: 272(sp) <-- sp entering interpreter
163 |.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves.
164 |.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves.
165 |.define SAVE_CR, 52(sp) // 32 bit CR save.
166 |.define SAVE_ERRF, 48(sp) // 32 bit C frame info.
167 |.define SAVE_NRES, 44(sp)
168 |.define SAVE_CFRAME, 40(sp)
169 |.define SAVE_L, 36(sp)
170 |.define SAVE_PC, 32(sp)
171 |.define SAVE_MULTRES, 28(sp)
172 |.define UNUSED1, 24(sp)
173 |.define TMPD_LO, 20(sp)
174 |.define TMPD_HI, 16(sp)
175 |.define TONUM_LO, 12(sp)
176 |.define TONUM_HI, 8(sp)
177 |// Next frame lr: 4(sp)
178 |// Back chain for sp: 0(sp) <-- sp while in interpreter
180 |.define TMPD_BLO, 23(sp)
181 |.define TMPD, TMPD_HI
182 |.define TONUM_D, TONUM_HI
188 | std r..reg, SAVE_GPR_+(reg-14)*8(sp)
190 | stw r..reg, SAVE_GPR_+(reg-14)*4(sp)
192 | stfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
196 | ld r..reg, SAVE_GPR_+(reg-14)*8(sp)
198 | lwz r..reg, SAVE_GPR_+(reg-14)*4(sp)
200 | lfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
205 | stdu sp, -CFRAME_SPACE(sp)
207 | stwu sp, -CFRAME_SPACE(sp)
209 | save_ 14; save_ 15; save_ 16
211 | save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22
217 | save_ 23; save_ 24; save_ 25
219 | save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31
225 | .toc std TOCREG, SAVE_TOC
230 | ld r0, SAVE_LR; ld r12, SAVE_CR
232 | lwz r0, SAVE_LR; lwz r12, SAVE_CR
234 | rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19
236 |.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif
237 | rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25
238 |.if PPE; mtocrf 0x10, r12; .endif
239 | rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31
240 |.if PPE; mtocrf 0x08, r12; .endif
241 | addi sp, sp, CFRAME_SPACE
244 |// Type definitions. Some of these are only used for documentation.
245 |.type L, lua_State, LREG
246 |.type GL, global_State
247 |.type TVALUE, TValue
251 |.type LFUNC, GCfuncL
252 |.type CFUNC, GCfuncC
253 |.type PROTO, GCproto
254 |.type UPVAL, GCupval
257 |.type TRACE, GCtrace
259 |//-----------------------------------------------------------------------
261 |// These basic macros should really be part of DynASM.
262 |.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro
263 |.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro
264 |.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro
265 |.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro
266 |.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro
268 |// Trap for not-yet-implemented parts.
269 |.macro NYI; tw 4, sp, sp; .endmacro
271 |// int/FP conversions.
272 |.macro tonum_i, freg, reg
273 | xoris reg, reg, 0x8000
276 | fsub freg, freg, TONUM
279 |.macro tonum_u, freg, reg
282 | fsub freg, freg, TOBIT
285 |.macro toint, reg, freg, tmpfreg
286 | fctiwz tmpfreg, freg
291 |.macro toint, reg, freg
292 | toint reg, freg, freg
295 |//-----------------------------------------------------------------------
297 |// Access to frame relative to BASE.
298 |.define FRAME_PC, -8
299 |.define FRAME_FUNC, -4
301 |// Instruction decode.
302 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
303 |.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro
304 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
305 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
306 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
307 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
309 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
310 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
312 |// Instruction fetch.
317 |// Instruction decode+dispatch. Note: optimized for e300!
319 | decode_OPP TMP1, INS
320 | lpx TMP0, DISPATCH, TMP1
333 |// Instruction footer.
335 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
336 | .define ins_next, ins_NEXT
337 | .define ins_next_, ins_NEXT
338 | .define ins_next1, ins_NEXT1
339 | .define ins_next2, ins_NEXT2
341 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
342 | // Affects only certain kinds of benchmarks (and only with -j off).
357 |// Call decode and dispatch.
359 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
360 | lwz PC, LFUNC:RB->pc
363 | decode_OPP TMP1, INS
365 | lpx TMP0, DISPATCH, TMP1
372 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
373 | stw PC, FRAME_PC(BASE)
377 |//-----------------------------------------------------------------------
379 |// Macros to test operand types.
380 |.macro checknum, reg; cmplw reg, TISNUM; .endmacro
381 |.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro
382 |.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro
383 |.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro
384 |.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro
385 |.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro
389 | addis PC, PC, -(BCBIAS_J*4 >> 16)
393 |// Assumes DISPATCH is relative to GL.
394 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
395 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
397 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
399 |.macro hotcheck, delta, target
400 | rlwinm TMP1, PC, 31, 25, 30
401 | addi TMP1, TMP1, GG_DISP2HOT
402 | lhzx TMP2, DISPATCH, TMP1
403 | addic. TMP2, TMP2, -delta
404 | sthx TMP2, DISPATCH, TMP1
409 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
413 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
416 |// Set current VM state. Uses TMP0.
417 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
418 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
420 |// Move table write barrier back. Overwrites mark and tmp.
421 |.macro barrierback, tab, mark, tmp
422 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
423 | // Assumes LJ_GC_BLACK is 0x04.
424 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
425 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
426 | stb mark, tab->marked
427 | stw tmp, tab->gclist
430 |//-----------------------------------------------------------------------
432 /* Generate subroutines used by opcodes and other parts of the VM. */
433 /* The .code_sub section should be last to help static branch prediction. */
434 static void build_subroutines(BuildCtx *ctx)
438 |//-----------------------------------------------------------------------
439 |//-- Return handling ----------------------------------------------------
440 |//-----------------------------------------------------------------------
443 | // See vm_return. Also: TMP2 = previous base.
444 | andix. TMP0, PC, FRAME_P
446 | beq ->cont_dispatch
448 | // Return from pcall or xpcall fast func.
449 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
450 | mr BASE, TMP2 // Restore caller base.
451 | // Prepending may overwrite the pcall frame, so do it at the end.
452 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
455 | addi RD, RD, 8 // RD = (nresults+1)*8.
456 | andix. TMP0, PC, FRAME_TYPE
458 | li CRET1, LUA_YIELD
459 | beq cr1, ->vm_unwind_c_eh
461 | beq ->BC_RET_Z // Handle regular return to Lua.
464 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
465 | // TMP0 = PC & FRAME_TYPE
466 | cmpwi TMP0, FRAME_C
467 | rlwinm TMP2, PC, 0, 0, 28
469 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
472 | addic. TMP1, RD, -8
474 | lwz TMP2, SAVE_NRES
480 | addic. TMP1, TMP1, -8
488 | cmpw TMP2, RD // More/less results wanted?
491 | stp BASE, L->top // Store new top.
494 | lp TMP0, SAVE_CFRAME // Restore previous C frame.
495 | li CRET1, 0 // Ok return status for vm_pcall.
496 | stp TMP0, L->cframe
503 | ble >7 // Less results wanted?
504 | // More results wanted. Check stack size and fill up results with nil.
505 | lwz TMP1, L->maxstack
508 | stw TISNIL, 0(BASE)
513 |7: // Less results wanted.
514 | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case?
516 | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1
517 | and TMP0, TMP0, TMP1
518 | sub BASE, BASE, TMP0 // Either keep top or shrink it.
521 |8: // Corner case: need to grow stack for filling up results.
522 | // This can happen if:
523 | // - A C function grows the stack (a lot).
524 | // - The GC shrinks the stack in between.
525 | // - A return back from a lua_call() with (high) nresults adjustment.
526 | stp BASE, L->top // Save current top held in BASE (yes).
530 | bl extern lj_state_growstack // (lua_State *L, int n)
531 | lwz TMP2, SAVE_NRES
534 | lp BASE, L->top // Need the (realloced) L->top in BASE.
537 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
538 | // (void *cframe, int errcode)
541 |->vm_unwind_c_eh: // Landing pad for external unwinder.
543 | .toc ld TOCREG, SAVE_TOC
544 | li TMP0, ~LJ_VMST_C
545 | lwz GL:TMP1, L->glref
546 | stw TMP0, GL:TMP1->vmstate
549 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
552 | rldicr sp, CARG1, 0, 61
554 | rlwinm sp, CARG1, 0, 0, 29
556 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
558 | .toc ld TOCREG, SAVE_TOC
559 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
561 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
562 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
566 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
570 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
571 | la RA, -8(BASE) // Results start at BASE-8.
573 | addi DISPATCH, DISPATCH, GG_G2DISP
574 | stw TMP1, 0(RA) // Prepend false to error message.
575 | li RD, 16 // 2 results: false + error message.
580 |//-----------------------------------------------------------------------
581 |//-- Grow stack for calls -----------------------------------------------
582 |//-----------------------------------------------------------------------
584 |->vm_growstack_c: // Grow stack for C function.
585 | li CARG2, LUA_MINSTACK
588 |->vm_growstack_l: // Grow stack for Lua function.
589 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
593 | addi PC, PC, 4 // Must point after first instruction.
597 | // L->base = new base, L->top = top
600 | bl extern lj_state_growstack // (lua_State *L, int n)
603 | lwz LFUNC:RB, FRAME_FUNC(BASE)
605 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
606 | ins_callt // Just retry the call.
608 |//-----------------------------------------------------------------------
609 |//-- Entry points into the assembler VM ---------------------------------
610 |//-----------------------------------------------------------------------
612 |->vm_resume: // Setup C frame and resume thread.
613 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
616 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
618 | lbz TMP1, L->status
621 | addi TMP0, sp, CFRAME_RESUME
622 | addi DISPATCH, DISPATCH, GG_G2DISP
623 | stw CARG3, SAVE_NRES
625 | stw CARG3, SAVE_ERRF
626 | stp TMP0, L->cframe
627 | stp CARG3, SAVE_CFRAME
628 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
631 | // Resume after yield (like a return).
634 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
636 | lwz PC, FRAME_PC(BASE)
637 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
638 | stb CARG3, L->status
640 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
644 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
650 | andix. TMP0, PC, FRAME_TYPE
657 |->vm_pcall: // Setup protected C frame and enter VM.
658 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
661 | stw CARG4, SAVE_ERRF
664 |->vm_call: // Setup C frame and enter VM.
665 | // (lua_State *L, TValue *base, int nres1)
669 |1: // Entry point for vm_pcall above (PC = ftype).
670 | lp TMP1, L:CARG1->cframe
671 | stw CARG3, SAVE_NRES
675 | stp sp, L->cframe // Add our C frame to cframe chain.
676 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
677 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
678 | stp TMP1, SAVE_CFRAME
679 | addi DISPATCH, DISPATCH, GG_G2DISP
681 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
682 | lp TMP2, L->base // TMP2 = old base (used in vmeta_call).
683 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
685 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
689 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
691 | sub PC, PC, TMP2 // PC = frame delta + frame type
693 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
694 | sub NARGS8:RC, TMP1, BASE
702 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
703 | lwz TMP0, FRAME_PC(BASE)
704 | lwz LFUNC:RB, FRAME_FUNC(BASE)
705 | checkfunc TMP0; bne ->vmeta_call
707 |->vm_call_dispatch_f:
709 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
711 |->vm_cpcall: // Setup protected C frame, call C.
712 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
715 | lwz TMP0, L:CARG1->stack
718 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
719 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
721 | stp sp, L->cframe // Add our C frame to cframe chain.
722 | .toc lp CARG4, 0(CARG4)
724 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
725 | stw TMP2, SAVE_ERRF // No error function.
726 | stp TMP1, SAVE_CFRAME
728 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
735 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
737 | addi DISPATCH, DISPATCH, GG_G2DISP
738 | bne <3 // Else continue with the call.
739 | b ->vm_leave_cp // No base? Just remove C frame.
741 |//-----------------------------------------------------------------------
742 |//-- Metamethod handling ------------------------------------------------
743 |//-----------------------------------------------------------------------
745 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
746 |// stack, so BASE doesn't need to be reloaded across these calls.
748 |//-- Continuation dispatch ----------------------------------------------
751 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
752 | lwz TMP0, -12(BASE) // Continuation.
754 | mr BASE, TMP2 // Restore caller BASE.
755 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
759 | lwz PC, -16(RB) // Restore PC from [cont|PC].
761 | lwz TMP1, LFUNC:TMP1->pc
762 | stwx TISNIL, RA, TMP2 // Ensure one valid arg.
766 | lwz KBASE, PC2PROTO(k)(TMP1)
767 | // BASE = base, RA = resultptr, RB = meta base
769 | bctr // Jump to continuation.
773 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
774 | // cont = 0: tailcall from C function.
780 |->cont_cat: // RA = resultptr, RB = meta base
783 | decode_RB8 SAVE0, INS
785 | add TMP1, BASE, SAVE0
788 | sub CARG3, CARG2, TMP1
795 |//-- Table indexing metamethods -----------------------------------------
798 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
801 | stw STR:RC, 4(CARG3)
802 | add CARG2, BASE, RB
807 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
809 | stw TAB:RB, 4(CARG2)
810 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
813 | stw STR:RC, 4(CARG3)
817 |->vmeta_tgetb: // TMP0 = index
822 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
823 | add CARG2, BASE, RB
825 | stw TISNUM, 0(CARG3)
835 | add CARG2, BASE, RB
836 | add CARG3, BASE, RC
841 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
842 | // Returns TValue * (finished) or NULL (metamethod).
850 |3: // Call __index metamethod.
851 | // BASE = base, L->top = new base, stack = cont/func/t/k
852 | subfic TMP1, BASE, FRAME_CONT
854 | stw PC, -16(BASE) // [cont|PC]
856 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
857 | li NARGS8:RC, 16 // 2 args for func(t, k).
858 | b ->vm_call_dispatch_f
860 |//-----------------------------------------------------------------------
863 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
866 | stw STR:RC, 4(CARG3)
867 | add CARG2, BASE, RB
872 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
874 | stw TAB:RB, 4(CARG2)
875 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
878 | stw STR:RC, 4(CARG3)
882 |->vmeta_tsetb: // TMP0 = index
887 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
888 | add CARG2, BASE, RB
890 | stw TISNUM, 0(CARG3)
900 | add CARG2, BASE, RB
901 | add CARG3, BASE, RC
906 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
907 | // Returns TValue * (finished) or NULL (metamethod).
911 | // NOBARRIER: lj_meta_tset ensures the table is not black.
916 |3: // Call __newindex metamethod.
917 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
918 | subfic TMP1, BASE, FRAME_CONT
920 | stw PC, -16(BASE) // [cont|PC]
922 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
923 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
924 | stfd f0, 16(BASE) // Copy value to third argument.
925 | b ->vm_call_dispatch_f
927 |//-- Comparison metamethods ---------------------------------------------
935 | add CARG2, BASE, RA
941 | add CARG3, BASE, RD
944 | decode_OP1 CARG4, INS
945 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
946 | // Returns 0/1 or TValue * (metamethod).
950 | subfic CRET1, CRET1, 0
954 | decode_RD4 TMP2, INS
955 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
956 | and TMP2, TMP2, CRET1
961 |->cont_ra: // RA = resultptr
964 | decode_RA8 TMP1, INS
965 | stfdx f0, BASE, TMP1
968 |->cont_condt: // RA = resultptr
970 | .gpr64 extsw TMP0, TMP0
971 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true.
972 | subfe CRET1, CRET1, CRET1
976 |->cont_condf: // RA = resultptr
978 | .gpr64 extsw TMP0, TMP0
979 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false.
980 | subfe CRET1, CRET1, CRET1
984 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
989 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
990 | // Returns 0/1 or TValue * (metamethod).
1000 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
1001 | // Returns 0/1 or TValue * (metamethod).
1005 |//-- Arithmetic metamethods ---------------------------------------------
1008 | add CARG3, KBASE, RC
1009 | add CARG4, BASE, RB
1024 | add CARG3, BASE, RB
1025 | add CARG4, KBASE, RC
1029 | add CARG3, BASE, RB
1030 | add CARG4, BASE, RC
1041 | add CARG2, BASE, RA
1045 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
1046 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1047 | // Returns NULL (finished) or TValue * (metamethod).
1051 | // Call metamethod for binary op.
1053 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1054 | sub TMP1, CRET1, BASE
1055 | stw PC, -16(CRET1) // [cont|PC]
1057 | addi PC, TMP1, FRAME_CONT
1059 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
1060 | b ->vm_call_dispatch
1070 | bl extern lj_meta_len // (lua_State *L, TValue *o)
1071 | // Returns NULL (retry) or TValue * (metamethod base).
1074 | bne ->vmeta_binop // Binop call for compatibility.
1078 | b ->vmeta_binop // Binop call for compatibility.
1081 |//-- Call metamethod ----------------------------------------------------
1083 |->vmeta_call: // Resolve and call __call metamethod.
1084 | // TMP2 = old base, BASE = new base, RC = nargs*8
1086 | stp TMP2, L->base // This is the callers base!
1087 | subi CARG2, BASE, 8
1089 | add CARG3, BASE, RC
1090 | mr SAVE0, NARGS8:RC
1091 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1092 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1093 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1096 |->vmeta_callt: // Resolve __call for BC_CALLT.
1097 | // BASE = old base, RA = new base, RC = nargs*8
1103 | mr SAVE0, NARGS8:RC
1104 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1105 | lwz TMP1, FRAME_PC(BASE)
1106 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1107 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1110 |//-- Argument coercion for 'for' statement ------------------------------
1118 | bl extern lj_meta_for // (lua_State *L, TValue *base)
1120 | decode_OP1 TMP0, SAVE0
1122 | decode_RA8 RA, SAVE0
1124 | cmpwi TMP0, BC_JFORI
1126 | decode_RD8 RD, SAVE0
1132 |//-----------------------------------------------------------------------
1133 |//-- Fast functions -----------------------------------------------------
1134 |//-----------------------------------------------------------------------
1136 |.macro .ffunc, name
1140 |.macro .ffunc_1, name
1142 | cmplwi NARGS8:RC, 8
1143 | lwz CARG3, 0(BASE)
1144 | lwz CARG1, 4(BASE)
1145 | blt ->fff_fallback
1148 |.macro .ffunc_2, name
1150 | cmplwi NARGS8:RC, 16
1151 | lwz CARG3, 0(BASE)
1152 | lwz CARG4, 8(BASE)
1153 | lwz CARG1, 4(BASE)
1154 | lwz CARG2, 12(BASE)
1155 | blt ->fff_fallback
1158 |.macro .ffunc_n, name
1160 | cmplwi NARGS8:RC, 8
1161 | lwz CARG3, 0(BASE)
1162 | lfd FARG1, 0(BASE)
1163 | blt ->fff_fallback
1164 | checknum CARG3; bge ->fff_fallback
1167 |.macro .ffunc_nn, name
1169 | cmplwi NARGS8:RC, 16
1170 | lwz CARG3, 0(BASE)
1171 | lfd FARG1, 0(BASE)
1172 | lwz CARG4, 8(BASE)
1173 | lfd FARG2, 8(BASE)
1174 | blt ->fff_fallback
1175 | checknum CARG3; bge ->fff_fallback
1176 | checknum CARG4; bge ->fff_fallback
1179 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
1181 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1182 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1187 |//-- Base library: checks -----------------------------------------------
1190 | li TMP1, LJ_TFALSE
1192 | cmplw cr1, CARG3, TMP1
1193 | lwz PC, FRAME_PC(BASE)
1194 | bge cr1, ->fff_fallback
1196 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1198 | beq ->fff_res // Done if exactly 1 argument.
1203 | lfdx f0, BASE, TMP1
1204 | stfdx f0, RA, TMP1
1205 | addi TMP1, TMP1, 8
1210 | cmplwi NARGS8:RC, 8
1211 | lwz CARG1, 0(BASE)
1212 | blt ->fff_fallback
1213 | .gpr64 extsw CARG1, CARG1
1214 | subfc TMP0, TISNUM, CARG1
1215 | subfe TMP2, CARG1, CARG1
1216 | orc TMP1, TMP2, TMP0
1217 | addi TMP1, TMP1, ~LJ_TISNUM+1
1218 | slwi TMP1, TMP1, 3
1219 | la TMP2, CFUNC:RB->upvalue
1220 | lfdx FARG1, TMP2, TMP1
1223 |//-- Base library: getters and setters ---------------------------------
1225 |.ffunc_1 getmetatable
1226 | checktab CARG3; bne >6
1227 |1: // Field metatable must be at same offset for GCtab and GCudata!
1228 | lwz TAB:CARG1, TAB:CARG1->metatable
1231 | cmplwi TAB:CARG1, 0
1232 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1234 | lwz TMP0, TAB:CARG1->hmask
1235 | li CARG3, LJ_TTAB // Use metatable as default result.
1236 | lwz TMP1, STR:RC->hash
1237 | lwz NODE:TMP2, TAB:CARG1->node
1238 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
1239 | slwi TMP0, TMP1, 5
1240 | slwi TMP1, TMP1, 3
1241 | sub TMP1, TMP0, TMP1
1242 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1243 |3: // Rearranged logic, because we expect _not_ to find the key.
1244 | lwz CARG4, NODE:TMP2->key
1245 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
1246 | lwz CARG2, NODE:TMP2->val
1247 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
1248 | checkstr CARG4; bne >4
1249 | cmpw TMP0, STR:RC; beq >5
1251 | lwz NODE:TMP2, NODE:TMP2->next
1252 | cmplwi NODE:TMP2, 0
1253 | beq ->fff_restv // Not found, keep default result.
1257 | beq ->fff_restv // Ditto for nil value.
1258 | mr CARG3, CARG2 // Return value of mt.__metatable.
1263 | cmpwi CARG3, LJ_TUDATA; beq <1
1264 | .gpr64 extsw CARG3, CARG3
1265 | subfc TMP0, TISNUM, CARG3
1266 | subfe TMP2, CARG3, CARG3
1267 | orc TMP1, TMP2, TMP0
1268 | addi TMP1, TMP1, ~LJ_TISNUM+1
1269 | slwi TMP1, TMP1, 2
1270 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1271 | lwzx TAB:CARG1, TMP2, TMP1
1274 |.ffunc_2 setmetatable
1275 | // Fast path: no mt for table yet and not clearing the mt.
1276 | checktab CARG3; bne ->fff_fallback
1277 | lwz TAB:TMP1, TAB:CARG1->metatable
1278 | checktab CARG4; bne ->fff_fallback
1279 | cmplwi TAB:TMP1, 0
1280 | lbz TMP3, TAB:CARG1->marked
1281 | bne ->fff_fallback
1282 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1283 | stw TAB:CARG2, TAB:CARG1->metatable
1285 | barrierback TAB:CARG1, TMP3, TMP0
1289 | cmplwi NARGS8:RC, 16
1290 | lwz CARG4, 0(BASE)
1291 | lwz TAB:CARG2, 4(BASE)
1292 | blt ->fff_fallback
1293 | checktab CARG4; bne ->fff_fallback
1296 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1297 | // Returns cTValue *.
1298 | lfd FARG1, 0(CRET1)
1301 |//-- Base library: conversions ------------------------------------------
1304 | // Only handles the number case inline (without a base argument).
1305 | cmplwi NARGS8:RC, 8
1306 | lwz CARG1, 0(BASE)
1307 | lfd FARG1, 0(BASE)
1308 | bne ->fff_fallback // Exactly one argument.
1309 | checknum CARG1; bgt ->fff_fallback
1313 | // Only handles the string or number case inline.
1315 | // A __tostring method in the string base metatable is ignored.
1316 | beq ->fff_restv // String key?
1317 | // Handle numbers inline, unless a number base metatable is present.
1318 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1320 | cmplwi cr1, TMP0, 0
1321 | stp BASE, L->base // Add frame since C call can throw.
1322 | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq
1323 | stw PC, SAVE_PC // Redundant (but a defined value).
1324 | beq ->fff_fallback
1329 | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o)
1331 | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np)
1333 | // Returns GCstr *.
1337 |//-- Base library: iterators -------------------------------------------
1340 | cmplwi NARGS8:RC, 8
1341 | lwz CARG1, 0(BASE)
1342 | lwz TAB:CARG2, 4(BASE)
1343 | blt ->fff_fallback
1344 | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1346 | lwz PC, FRAME_PC(BASE)
1347 | bne ->fff_fallback
1348 | stp BASE, L->base // Add frame since C call can throw.
1350 | stp BASE, L->top // Dummy frame length is ok.
1353 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1354 | // Returns 0 at end of traversal.
1357 | beq ->fff_restv // End of traversal: return nil.
1358 | lfd f0, 8(BASE) // Copy key and value to results.
1368 | lwz PC, FRAME_PC(BASE)
1369 | bne ->fff_fallback
1371 | lwz TAB:TMP2, TAB:CARG1->metatable
1372 | lfd f0, CFUNC:RB->upvalue[0]
1373 | cmplwi TAB:TMP2, 0
1375 | bne ->fff_fallback
1377 | lfd f0, CFUNC:RB->upvalue[0]
1380 | stw TISNIL, 8(BASE)
1386 | cmplwi NARGS8:RC, 16
1387 | lwz CARG3, 0(BASE)
1388 | lwz TAB:CARG1, 4(BASE)
1389 | lwz CARG4, 8(BASE)
1391 | lwz TMP2, 12(BASE)
1393 | lfd FARG2, 8(BASE)
1395 | blt ->fff_fallback
1397 | checknum cr1, CARG4
1398 | lwz PC, FRAME_PC(BASE)
1400 | bne ->fff_fallback
1401 | bne cr1, ->fff_fallback
1405 | bne ->fff_fallback
1407 | bge cr1, ->fff_fallback
1409 | toint TMP2, FARG2, f0
1411 | lwz TMP0, TAB:CARG1->asize
1412 | lwz TMP1, TAB:CARG1->array
1414 | fadd FARG2, FARG2, FARG1
1416 | addi TMP2, TMP2, 1
1421 | slwi TMP3, TMP2, 3
1424 | slwi TMP3, TMP2, 3
1427 | ble >2 // Not in array part?
1428 | lwzx TMP2, TMP1, TMP3
1429 | lfdx f0, TMP1, TMP3
1433 | beq ->fff_res // End of iteration, return 0 results.
1437 |2: // Check for empty hash part first. Otherwise call C function.
1438 | lwz TMP0, TAB:CARG1->hmask
1443 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1444 | // Returns cTValue * or NULL.
1448 | lwz TMP2, 0(CRET1)
1454 | lwz PC, FRAME_PC(BASE)
1455 | bne ->fff_fallback
1457 | lwz TAB:TMP2, TAB:CARG1->metatable
1458 | lfd f0, CFUNC:RB->upvalue[0]
1459 | cmplwi TAB:TMP2, 0
1461 | bne ->fff_fallback
1463 | lfd f0, CFUNC:RB->upvalue[0]
1467 | stw TISNUM, 8(BASE)
1471 | stw ZERO, 12(BASE)
1476 |//-- Base library: catch errors ----------------------------------------
1479 | cmplwi NARGS8:RC, 8
1480 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1481 | blt ->fff_fallback
1484 | // Remember active hook before pcall.
1485 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1486 | subi NARGS8:RC, NARGS8:RC, 8
1487 | addi PC, TMP3, 8+FRAME_PCALL
1488 | b ->vm_call_dispatch
1491 | cmplwi NARGS8:RC, 16
1492 | lwz CARG4, 8(BASE)
1493 | lfd FARG2, 8(BASE)
1494 | lfd FARG1, 0(BASE)
1495 | blt ->fff_fallback
1496 | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1498 | checkfunc CARG4; bne ->fff_fallback // Traceback must be a function.
1500 | // Remember active hook before pcall.
1501 | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31
1502 | stfd FARG2, 0(TMP2) // Swap function and traceback.
1503 | subi NARGS8:RC, NARGS8:RC, 16
1504 | stfd FARG1, 8(TMP2)
1505 | addi PC, TMP1, 16+FRAME_PCALL
1506 | b ->vm_call_dispatch
1508 |//-- Coroutine library --------------------------------------------------
1510 |.macro coroutine_resume_wrap, resume
1512 |.ffunc_1 coroutine_resume
1513 | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback
1515 |.ffunc coroutine_wrap_aux
1516 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1518 | lbz TMP0, L:CARG1->status
1519 | lp TMP1, L:CARG1->cframe
1520 | lp CARG2, L:CARG1->top
1521 | cmplwi cr0, TMP0, LUA_YIELD
1522 | lp TMP2, L:CARG1->base
1523 | cmplwi cr1, TMP1, 0
1524 | lwz TMP0, L:CARG1->maxstack
1525 | cmplw cr7, CARG2, TMP2
1526 | lwz PC, FRAME_PC(BASE)
1527 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1528 | add TMP2, CARG2, NARGS8:RC
1529 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1530 | cmplw cr1, TMP2, TMP0
1531 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1533 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1535 | blt cr6, ->fff_fallback
1538 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1539 | subi NARGS8:RC, NARGS8:RC, 8
1540 | subi TMP2, TMP2, 8
1542 | stp TMP2, L:CARG1->top
1545 |2: // Move args to coroutine.
1546 | cmpw TMP1, NARGS8:RC
1547 | lfdx f0, BASE, TMP1
1549 | stfdx f0, CARG2, TMP1
1550 | addi TMP1, TMP1, 8
1554 | mr L:SAVE0, L:CARG1
1556 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1557 | // Returns thread status.
1559 | lp TMP2, L:SAVE0->base
1560 | cmplwi CRET1, LUA_YIELD
1561 | lp TMP3, L:SAVE0->top
1566 | sub RD, TMP3, TMP2
1567 | lwz TMP0, L->maxstack
1569 | add TMP1, BASE, RD
1570 | beq >6 // No results?
1573 | bgt >9 // Need to grow stack?
1576 | stp TMP2, L:SAVE0->top // Clear coroutine stack.
1577 |5: // Move results from coroutine.
1579 | lfdx f0, TMP2, TMP1
1580 | stfdx f0, BASE, TMP1
1581 | addi TMP1, TMP1, 8
1584 | andix. TMP0, PC, FRAME_TYPE
1588 | stw TMP1, -8(BASE) // Prepend true to results.
1600 |8: // Coroutine returned with error (at co->top-1).
1602 | andix. TMP0, PC, FRAME_TYPE
1604 | li TMP1, LJ_TFALSE
1606 | stp TMP3, L:SAVE0->top // Remove error from coroutine stack.
1608 | stw TMP1, -8(BASE) // Prepend false to results.
1610 | stfd f0, 0(BASE) // Copy error message.
1615 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1618 |9: // Handle stack expansion on return from yield.
1621 | bl extern lj_state_growstack // (lua_State *L, int n)
1626 | coroutine_resume_wrap 1 // coroutine.resume
1627 | coroutine_resume_wrap 0 // coroutine.wrap
1629 |.ffunc coroutine_yield
1630 | lp TMP0, L->cframe
1631 | add TMP1, BASE, NARGS8:RC
1633 | andix. TMP0, TMP0, CFRAME_RESUME
1635 | li CRET1, LUA_YIELD
1636 | beq ->fff_fallback
1637 | stp ZERO, L->cframe
1638 | stb CRET1, L->status
1641 |//-- Math library -------------------------------------------------------
1647 | srawi TMP1, CARG1, 31
1648 | xor TMP2, TMP1, CARG1
1651 | sub CARG1, TMP2, TMP1
1655 | sub. CARG1, TMP2, TMP1
1659 | lwz PC, FRAME_PC(BASE)
1661 | stw TISNUM, -8(BASE)
1662 | stw CRET1, -4(BASE)
1665 | lus CARG3, 0x41e0 // 2^31.
1670 | bge ->fff_fallback
1671 | rlwinm CARG3, CARG3, 0, 1, 31
1675 | // CARG3/CARG1 = TValue result.
1676 | lwz PC, FRAME_PC(BASE)
1677 | stw CARG3, -8(BASE)
1679 | stw CARG1, -4(BASE)
1681 | // RA = results, PC = return.
1684 | // RA = results, RD = (nresults+1)*8, PC = return.
1685 | andix. TMP0, PC, FRAME_TYPE
1689 | decode_RB8 RB, INS
1691 | cmplw RB, RD // More results expected?
1692 | decode_RA8 TMP0, INS
1695 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1696 | sub BASE, RA, TMP0
1699 |6: // Fill up results with nil.
1702 | stwx TISNIL, RA, TMP1
1705 |.macro math_extern, func
1706 | .ffunc_n math_ .. func
1711 |.macro math_extern2, func
1712 | .ffunc_nn math_ .. func
1717 |.macro math_round, func
1718 | .ffunc_1 math_ .. func
1719 | checknum CARG3; beqy ->fff_restv
1720 | rlwinm TMP2, CARG3, 12, 21, 31
1721 | bge ->fff_fallback
1722 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
1723 | cmplwi cr1, TMP2, 31 // 0 <= exp < 31?
1724 | subfic TMP0, TMP2, 31
1726 | slwi TMP1, CARG3, 11
1727 | srwi TMP3, CARG1, 21
1728 | oris TMP1, TMP1, 0x8000
1729 | addi TMP2, TMP2, 1
1730 | or TMP1, TMP1, TMP3
1731 | slwi CARG2, CARG1, 11
1733 | slw TMP3, TMP1, TMP2
1734 | srw RD, TMP1, TMP0
1735 | or TMP3, TMP3, CARG2
1736 | srawi TMP2, CARG3, 31
1737 |.if "func" == "floor"
1738 | and TMP1, TMP3, TMP2
1739 | addic TMP0, TMP1, -1
1740 | subfe TMP1, TMP0, TMP1
1741 | add CARG1, RD, TMP1
1742 | xor CARG1, CARG1, TMP2
1743 | sub CARG1, CARG1, TMP2
1746 | andc TMP1, TMP3, TMP2
1747 | addic TMP0, TMP1, -1
1748 | subfe TMP1, TMP0, TMP1
1749 | add CARG1, RD, TMP1
1751 | xor CARG1, CARG1, TMP2
1752 | sub CARG1, CARG1, TMP2
1754 | // Overflow to 2^31.
1755 | lus CARG3, 0x41e0 // 2^31.
1760 | slwi TMP2, CARG3, 1
1761 | srawi TMP1, CARG3, 31
1762 | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo
1763 |.if "func" == "floor"
1764 | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1
1765 | subfic TMP2, TMP1, 0
1766 | subfe CARG1, CARG1, CARG1
1768 | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1
1769 | addic TMP2, TMP1, -1
1770 | subfe CARG1, TMP2, TMP1
1773 |4: // exp >= 31. Check for -(2^31).
1774 | xoris TMP1, TMP1, 0x8000
1775 | srawi TMP2, CARG3, 31
1776 |.if "func" == "floor"
1777 | or TMP1, TMP1, CARG2
1780 | orc TMP1, TMP1, TMP2
1783 | orc. TMP1, TMP1, TMP2
1785 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
1786 | lus CARG1, 0x8000 // -(2^31).
1789 | lfd FARG1, 0(BASE)
1798 | // NYI: use internal implementation.
1805 | fsqrt FARG1, FARG1
1812 | cmplwi NARGS8:RC, 8
1813 | lwz CARG3, 0(BASE)
1814 | lfd FARG1, 0(BASE)
1815 | bne ->fff_fallback // Need exactly 1 argument.
1816 | checknum CARG3; bge ->fff_fallback
1832 | math_extern2 atan2
1837 | lfd FARG2, CFUNC:RB->upvalue[0]
1838 | fmul FARG1, FARG1, FARG2
1843 | cmplwi NARGS8:RC, 16
1844 | lwz CARG3, 0(BASE)
1845 | lfd FARG1, 0(BASE)
1846 | lwz CARG4, 8(BASE)
1848 | lwz CARG2, 12(BASE)
1850 | lwz CARG1, 12(BASE)
1852 | blt ->fff_fallback
1853 | checknum CARG3; bge ->fff_fallback
1854 | checknum CARG4; bne ->fff_fallback
1856 |.ffunc_nn math_ldexp
1858 | toint CARG2, FARG2
1860 | toint CARG1, FARG2
1866 |.ffunc_n math_frexp
1868 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1870 | la CARG1, DISPATCH_GL(tmptv)(DISPATCH)
1872 | lwz PC, FRAME_PC(BASE)
1874 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1877 | tonum_i FARG2, TMP1
1891 | la CARG2, -8(BASE)
1893 | la CARG1, -8(BASE)
1895 | lwz PC, FRAME_PC(BASE)
1898 | stfd FARG1, 0(BASE)
1902 |.macro math_minmax, name, ismax
1906 | addi TMP1, BASE, 8
1907 | add TMP2, BASE, NARGS8:RC
1909 |1: // Handle integers.
1910 | lwz CARG4, 0(TMP1)
1911 | cmplw cr1, TMP1, TMP2
1912 | lwz CARG2, 4(TMP1)
1913 | bge cr1, ->fff_resi
1915 | xoris TMP0, CARG1, 0x8000
1916 | xoris TMP3, CARG2, 0x8000
1918 | subfc TMP3, TMP3, TMP0
1919 | subfe TMP0, TMP0, TMP0
1921 | andc TMP3, TMP3, TMP0
1923 | and TMP3, TMP3, TMP0
1925 | add CARG1, TMP3, CARG2
1927 | rldicl CARG1, CARG1, 0, 32
1929 | addi TMP1, TMP1, 8
1932 | bge ->fff_fallback
1933 | // Convert intermediate result to number and continue below.
1934 | tonum_i FARG1, CARG1
1935 | lfd FARG2, 0(TMP1)
1938 | lfd FARG1, 0(BASE)
1939 | bge ->fff_fallback
1940 |5: // Handle numbers.
1941 | lwz CARG4, 0(TMP1)
1942 | cmplw cr1, TMP1, TMP2
1943 | lfd FARG2, 0(TMP1)
1944 | bge cr1, ->fff_resn
1945 | checknum CARG4; bge >7
1947 | fsub f0, FARG1, FARG2
1948 | addi TMP1, TMP1, 8
1950 | fsel FARG1, f0, FARG1, FARG2
1952 | fsel FARG1, f0, FARG2, FARG1
1955 |7: // Convert integer to number and continue above.
1956 | lwz CARG2, 4(TMP1)
1957 | bne ->fff_fallback
1958 | tonum_i FARG2, CARG2
1964 | lwzx CARG2, BASE, TMP1
1965 | lfdx FARG2, BASE, TMP1
1966 | cmplw cr1, TMP1, NARGS8:RC
1968 | bge cr1, ->fff_resn
1969 | bge ->fff_fallback
1970 | fsub f0, FARG1, FARG2
1971 | addi TMP1, TMP1, 8
1973 | fsel FARG1, f0, FARG1, FARG2
1975 | fsel FARG1, f0, FARG2, FARG1
1981 | math_minmax math_min, 0
1982 | math_minmax math_max, 1
1984 |//-- String library -----------------------------------------------------
1986 |.ffunc_1 string_len
1987 | checkstr CARG3; bne ->fff_fallback
1988 | lwz CRET1, STR:CARG1->len
1991 |.ffunc string_byte // Only handle the 1-arg case here.
1992 | cmplwi NARGS8:RC, 8
1993 | lwz CARG3, 0(BASE)
1994 | lwz STR:CARG1, 4(BASE)
1995 | bne ->fff_fallback // Need exactly 1 argument.
1997 | bne ->fff_fallback
1998 | lwz TMP0, STR:CARG1->len
2000 | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
2002 | lwz PC, FRAME_PC(BASE)
2008 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
2009 | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8
2010 | subfe RD, TMP3, TMP0
2011 | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1.
2015 | lwz PC, FRAME_PC(BASE)
2016 | fsub f0, f0, TOBIT
2022 |.ffunc string_char // Only handle the 1-arg case here.
2024 | cmplwi NARGS8:RC, 8
2025 | lwz CARG3, 0(BASE)
2028 | bne ->fff_fallback // Exactly 1 argument.
2029 | checknum CARG3; bne ->fff_fallback
2032 | lfd FARG1, 0(BASE)
2033 | bne ->fff_fallback // Exactly 1 argument.
2034 | checknum CARG3; bge ->fff_fallback
2036 | la CARG2, TMPD_BLO
2039 | cmplwi TMP0, 255; bgt ->fff_fallback
2044 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
2045 | // Returns GCstr *.
2052 | cmplwi NARGS8:RC, 16
2053 | lwz CARG3, 16(BASE)
2058 | lwz STR:CARG1, 4(BASE)
2059 | blt ->fff_fallback
2060 | lwz CARG2, 8(BASE)
2062 | lwz TMP1, 12(BASE)
2070 | lwz TMP2, 20(BASE)
2071 | bne ->fff_fallback
2073 | checknum CARG2; bne ->fff_fallback
2075 | checknum CARG3; bge ->fff_fallback
2078 | checknum CARG2; bge ->fff_fallback
2080 | checkstr TMP0; bne ->fff_fallback
2084 | lwz TMP0, STR:CARG1->len
2085 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
2086 | addi TMP3, TMP2, 1
2089 | cmpwi TMP1, 0 // start <= 0?
2090 | add TMP3, TMP1, TMP0
2093 | sub CARG3, TMP2, TMP1
2094 | addi CARG2, STR:CARG1, #STR-1
2095 | srawi TMP0, CARG3, 31
2096 | addi CARG3, CARG3, 1
2097 | add CARG2, CARG2, TMP1
2098 | andc CARG3, CARG3, TMP0
2100 | rldicl CARG2, CARG2, 0, 32
2101 | rldicl CARG3, CARG3, 0, 32
2105 |5: // Negative end or overflow.
2106 | cmpw TMP0, TMP2 // len >= end? (signed compare)
2107 | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1.
2109 | mr TMP2, TMP0 // Overflow: end = len.
2112 |7: // Negative start or underflow.
2113 | .gpr64 extsw TMP1, TMP1
2114 | addic CARG3, TMP1, -1
2115 | subfe CARG3, CARG3, CARG3
2116 | srawi CARG2, TMP3, 31 // Note: modifies carry.
2117 | andc TMP3, TMP3, CARG3
2118 | andc TMP1, TMP3, CARG2
2119 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
2122 |.ffunc string_rep // Only handle the 1-char case inline.
2124 | cmplwi NARGS8:RC, 16
2126 | lwz STR:CARG1, 4(BASE)
2127 | lwz CARG4, 8(BASE)
2129 | lwz CARG3, 12(BASE)
2131 | lfd FARG2, 8(BASE)
2133 | bne ->fff_fallback // Exactly 2 arguments.
2134 | checkstr TMP0; bne ->fff_fallback
2136 | checknum CARG4; bne ->fff_fallback
2138 | checknum CARG4; bge ->fff_fallback
2139 | toint CARG3, FARG2
2141 | lwz TMP0, STR:CARG1->len
2143 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2144 | ble >2 // Count <= 0? (or non-int)
2146 | subi TMP2, CARG3, 1
2147 | blt >2 // Zero length string?
2148 | cmplw cr1, TMP1, CARG3
2149 | bne ->fff_fallback // Fallback for > 1-char strings.
2150 | lbz TMP0, STR:CARG1[1]
2151 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2152 | blt cr1, ->fff_fallback
2153 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2155 | stbx TMP0, CARG2, TMP2
2156 | subi TMP2, TMP2, 1
2159 |2: // Return empty string.
2160 | la STR:CARG1, DISPATCH_GL(strempty)(DISPATCH)
2164 |.ffunc string_reverse
2166 | cmplwi NARGS8:RC, 8
2167 | lwz CARG3, 0(BASE)
2168 | lwz STR:CARG1, 4(BASE)
2169 | blt ->fff_fallback
2171 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2172 | bne ->fff_fallback
2173 | lwz CARG3, STR:CARG1->len
2174 | la CARG1, #STR(STR:CARG1)
2175 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2178 | subi TMP3, CARG3, 1
2179 | blt ->fff_fallback
2180 |1: // Reverse string copy.
2182 | lbzx TMP1, CARG1, TMP2
2184 | stbx TMP1, CARG2, TMP3
2185 | subi TMP3, TMP3, 1
2186 | addi TMP2, TMP2, 1
2189 |.macro ffstring_case, name, lo
2192 | cmplwi NARGS8:RC, 8
2193 | lwz CARG3, 0(BASE)
2194 | lwz STR:CARG1, 4(BASE)
2195 | blt ->fff_fallback
2197 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2198 | bne ->fff_fallback
2199 | lwz CARG3, STR:CARG1->len
2200 | la CARG1, #STR(STR:CARG1)
2201 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2204 | blt ->fff_fallback
2205 |1: // ASCII case conversion.
2207 | lbzx TMP1, CARG1, TMP2
2209 | subi TMP0, TMP1, lo
2210 | xori TMP3, TMP1, 0x20
2211 | addic TMP0, TMP0, -26
2212 | subfe TMP3, TMP3, TMP3
2213 | rlwinm TMP3, TMP3, 0, 26, 26 // x &= 0x20.
2214 | xor TMP1, TMP1, TMP3
2215 | stbx TMP1, CARG2, TMP2
2216 | addi TMP2, TMP2, 1
2220 |ffstring_case string_lower, 65
2221 |ffstring_case string_upper, 97
2223 |//-- Table library ------------------------------------------------------
2225 |.ffunc_1 table_getn
2226 | checktab CARG3; bne ->fff_fallback
2227 | bl extern lj_tab_len // (GCtab *t)
2228 | // Returns uint32_t (but less than 2^31).
2231 |//-- Bit library --------------------------------------------------------
2233 |.macro .ffunc_bit, name
2235 | .ffunc_1 bit_..name
2236 | checknum CARG3; bnel ->fff_tobit_fb
2238 | .ffunc_n bit_..name
2239 | fadd FARG1, FARG1, TOBIT
2241 | lwz CARG1, TMPD_LO
2245 |.macro .ffunc_bit_op, name, ins
2247 | addi TMP1, BASE, 8
2248 | add TMP2, BASE, NARGS8:RC
2250 | lwz CARG4, 0(TMP1)
2251 | cmplw cr1, TMP1, TMP2
2253 | lwz CARG2, 4(TMP1)
2255 | lfd FARG1, 0(TMP1)
2257 | bgey cr1, ->fff_resi
2260 | bnel ->fff_bitop_fb
2262 | fadd FARG1, FARG1, TOBIT
2263 | bge ->fff_fallback
2265 | lwz CARG2, TMPD_LO
2267 | ins CARG1, CARG1, CARG2
2268 | addi TMP1, TMP1, 8
2272 |.ffunc_bit_op band, and
2273 |.ffunc_bit_op bor, or
2274 |.ffunc_bit_op bxor, xor
2277 | rotlwi TMP0, CARG1, 8
2278 | rlwimi TMP0, CARG1, 24, 0, 7
2279 | rlwimi TMP0, CARG1, 24, 16, 23
2287 |.macro .ffunc_bit_sh, name, ins, shmod
2289 | .ffunc_2 bit_..name
2290 | checknum CARG3; bnel ->fff_tobit_fb
2291 | // Note: no inline conversion from number for 2nd argument!
2292 | checknum CARG4; bne ->fff_fallback
2294 | .ffunc_nn bit_..name
2295 | fadd FARG1, FARG1, TOBIT
2296 | fadd FARG2, FARG2, TOBIT
2298 | lwz CARG1, TMPD_LO
2300 | lwz CARG2, TMPD_LO
2303 | rlwinm CARG2, CARG2, 0, 27, 31
2307 | ins CRET1, CARG1, CARG2
2311 |.ffunc_bit_sh lshift, slw, 1
2312 |.ffunc_bit_sh rshift, srw, 1
2313 |.ffunc_bit_sh arshift, sraw, 1
2314 |.ffunc_bit_sh rol, rotlw, 0
2315 |.ffunc_bit_sh ror, rotlw, 2
2322 | tonum_i FARG1, CRET1
2325 | lwz PC, FRAME_PC(BASE)
2327 | stfd FARG1, -8(BASE)
2330 |// Fallback FP number to bit conversion.
2333 | lfd FARG1, 0(BASE)
2334 | bgt ->fff_fallback
2335 | fadd FARG1, FARG1, TOBIT
2337 | lwz CARG1, TMPD_LO
2342 | lfd FARG1, 0(TMP1)
2343 | bgt ->fff_fallback
2344 | fadd FARG1, FARG1, TOBIT
2346 | lwz CARG2, TMPD_LO
2350 |//-----------------------------------------------------------------------
2352 |->fff_fallback: // Call fast function fallback handler.
2353 | // BASE = new base, RB = CFUNC, RC = nargs*8
2354 | lp TMP3, CFUNC:RB->f
2355 | add TMP1, BASE, NARGS8:RC
2356 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2357 | addi TMP0, TMP1, 8*LUA_MINSTACK
2358 | lwz TMP2, L->maxstack
2359 | stw PC, SAVE_PC // Redundant (but a defined value).
2360 | .toc lp TMP3, 0(TMP3)
2365 | bgt >5 // Need to grow stack.
2367 | bctrl // (lua_State *L)
2368 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2373 | bgt ->fff_res // Returned nresults+1?
2374 |1: // Returned 0 or -1: retry fast path.
2376 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2377 | sub NARGS8:RC, TMP0, BASE
2378 | bne ->vm_call_tail // Returned -1?
2379 | ins_callt // Returned 0: retry fast path.
2381 |// Reconstruct previous base for vmeta_call during tailcall.
2383 | andix. TMP0, PC, FRAME_TYPE
2384 | rlwinm TMP1, PC, 0, 0, 28
2387 | decode_RA8 TMP1, INS
2388 | addi TMP1, TMP1, 8
2390 | sub TMP2, BASE, TMP1
2391 | b ->vm_call_dispatch // Resolve again for tailcall.
2393 |5: // Grow stack for fallback handler.
2394 | li CARG2, LUA_MINSTACK
2395 | bl extern lj_state_growstack // (lua_State *L, int n)
2397 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
2400 |->fff_gcstep: // Call GC step function.
2401 | // BASE = new base, RC = nargs*8
2404 | add TMP0, BASE, NARGS8:RC
2405 | stw PC, SAVE_PC // Redundant (but a defined value).
2408 | bl extern lj_gc_step // (lua_State *L)
2412 | sub NARGS8:RC, TMP0, BASE
2413 | lwz CFUNC:RB, FRAME_FUNC(BASE)
2416 |//-----------------------------------------------------------------------
2417 |//-- Special dispatch targets -------------------------------------------
2418 |//-----------------------------------------------------------------------
2420 |->vm_record: // Dispatch target for recording phase.
2422 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2423 | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2425 | // Decrement the hookcount for consistency, but always do the call.
2426 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2427 | andix. TMP0, TMP3, HOOK_ACTIVE
2429 | subi TMP2, TMP2, 1
2430 | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2432 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2436 |->vm_rethook: // Dispatch target for return hooks.
2437 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2438 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2440 |5: // Re-dispatch to static ins.
2441 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS.
2442 | lpx TMP0, DISPATCH, TMP1
2446 |->vm_inshook: // Dispatch target for instr/line hooks.
2447 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2448 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2449 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2450 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
2453 | cmpwi cr1, TMP0, 0
2454 | addic. TMP2, TMP2, -1
2456 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2461 | stw MULTRES, SAVE_MULTRES
2464 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2465 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2468 |4: // Re-dispatch to static ins.
2470 | decode_OPP TMP1, INS
2471 | decode_RB8 RB, INS
2472 | addi TMP1, TMP1, GG_DISP2STATIC
2473 | decode_RD8 RD, INS
2474 | lpx TMP0, DISPATCH, TMP1
2475 | decode_RA8 RA, INS
2476 | decode_RC8 RC, INS
2480 |->cont_hook: // Continue from hook yield.
2482 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
2485 |->vm_hotloop: // Hot loop counter underflow.
2487 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2488 | addi CARG1, DISPATCH, GG_DISP2J
2490 | lwz TMP1, LFUNC:TMP1->pc
2492 | stw L, DISPATCH_J(L)(DISPATCH)
2493 | lbz TMP1, PC2PROTO(framesize)(TMP1)
2495 | slwi TMP1, TMP1, 3
2496 | add TMP1, BASE, TMP1
2498 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2502 |->vm_callhook: // Dispatch target for call hooks.
2508 |->vm_hotcall: // Hot call counter underflow.
2513 | add TMP0, BASE, RC
2519 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2520 | // Returns ASMFunction.
2523 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
2524 | sub NARGS8:RC, TMP0, BASE
2526 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2531 |//-----------------------------------------------------------------------
2532 |//-- Trace exit handler -------------------------------------------------
2533 |//-----------------------------------------------------------------------
2535 |.macro savex_, a, b, c, d
2536 | stfd f..a, 16+a*8(sp)
2537 | stfd f..b, 16+b*8(sp)
2538 | stfd f..c, 16+c*8(sp)
2539 | stfd f..d, 16+d*8(sp)
2544 | addi sp, sp, -(16+32*8+32*4)
2545 | stmw r2, 16+32*8+2*4(sp)
2546 | addi DISPATCH, JGL, -GG_DISP2G-32768
2547 | li CARG2, ~LJ_VMST_EXIT
2548 | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain.
2549 | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH)
2551 | stw CARG1, 0(sp) // Store extended stack chain.
2552 | mcrxr cr0 // Clear SO flag.
2554 | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp.
2556 | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP.
2557 | savex_ 12,13,14,15
2560 | savex_ 16,17,18,19
2561 | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP.
2562 | savex_ 20,21,22,23
2563 | lhz CARG4, 2(CARG3) // Load trace number.
2564 | savex_ 24,25,26,27
2565 | lwz L, DISPATCH_GL(jit_L)(DISPATCH)
2566 | savex_ 28,29,30,31
2567 | sub CARG3, TMP0, CARG3 // Compute exit number.
2568 | lp BASE, DISPATCH_GL(jit_base)(DISPATCH)
2569 | srwi CARG3, CARG3, 2
2570 | stw L, DISPATCH_J(L)(DISPATCH)
2571 | subi CARG3, CARG3, 2
2572 | stw TMP1, DISPATCH_GL(jit_L)(DISPATCH)
2573 | stw CARG4, DISPATCH_J(parent)(DISPATCH)
2575 | addi CARG1, DISPATCH, GG_DISP2J
2576 | stw CARG3, DISPATCH_J(exitno)(DISPATCH)
2577 | addi CARG2, sp, 16
2578 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2579 | // Returns MULTRES (unscaled) or negated error code.
2580 | lp TMP1, L->cframe
2584 | rldicr sp, TMP1, 0, 61
2586 | rlwinm sp, TMP1, 0, 0, 29
2588 | lwz PC, SAVE_PC // Get SAVE_PC.
2590 | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2595 | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set.
2597 | addi DISPATCH, JGL, -GG_DISP2G-32768
2600 | blt >3 // Check for error from exit.
2601 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2602 | slwi MULTRES, CARG1, 3
2604 | stw MULTRES, SAVE_MULTRES
2605 | lwz TMP1, LFUNC:TMP1->pc
2606 | stw TMP2, DISPATCH_GL(jit_L)(DISPATCH)
2607 | lwz KBASE, PC2PROTO(k)(TMP1)
2608 | // Setup type comparison constants.
2609 | li TISNUM, LJ_TISNUM
2610 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2613 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2616 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2617 | li TISNIL, LJ_TNIL
2618 | stw TMP0, TONUM_HI
2620 | // Modified copy of ins_next which handles function header dispatch, too.
2623 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1.
2624 | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2625 | decode_OPP TMP1, INS
2626 | decode_RA8 RA, INS
2627 | lpx TMP0, DISPATCH, TMP1
2629 | cmplwi TMP1, BC_FUNCF*4 // Function header?
2631 | decode_RB8 RB, INS
2632 | decode_RD8 RD, INS
2633 | decode_RC8 RC, INS
2636 | subi RC, MULTRES, 8
2640 |3: // Rethrow error from the right C frame.
2643 | bl extern lj_err_throw // (lua_State *L, int errcode)
2646 |//-----------------------------------------------------------------------
2647 |//-- Math helper functions ----------------------------------------------
2648 |//-----------------------------------------------------------------------
2650 |// NYI: Use internal implementations of floor, ceil, trunc.
2653 | divwo. TMP0, CARG1, CARG2
2656 | xor CARG3, CARG1, CARG2
2659 | xor. CARG3, CARG1, CARG2
2661 | mullw TMP0, TMP0, CARG2
2662 | sub CARG1, CARG1, TMP0
2664 | cmpwi CARG1, 0; beqlr
2665 | add CARG1, CARG1, CARG2
2671 | mcrxr cr0 // Clear SO for -2147483648 % -1 and return 0.
2674 |//-----------------------------------------------------------------------
2675 |//-- Miscellaneous functions --------------------------------------------
2676 |//-----------------------------------------------------------------------
2678 |// void lj_vm_cachesync(void *start, void *end)
2679 |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size.
2680 |// This is a good lower bound, except for very ancient PPC models.
2683 | // Compute start of first cache line and number of cache lines.
2684 | rlwinm CARG1, CARG1, 0, 0, 26
2685 | sub CARG2, CARG2, CARG1
2686 | addi CARG2, CARG2, 31
2687 | rlwinm. CARG2, CARG2, 27, 5, 31
2691 |1: // Flush D-Cache.
2693 | addi CARG1, CARG1, 32
2697 |1: // Invalidate I-Cache.
2699 | addi CARG3, CARG3, 32
2705 |//-----------------------------------------------------------------------
2706 |//-- FFI helper functions -----------------------------------------------
2707 |//-----------------------------------------------------------------------
2709 |// Handler for callback functions. Callback slot number in r11, g in r12.
2712 |.type CTSTATE, CTState, PC
2714 | lwz CTSTATE, GL:r12->ctype_state
2715 | addi DISPATCH, r12, GG_G2DISP
2716 | stw r11, CTSTATE->cb.slot
2717 | stw r3, CTSTATE->cb.gpr[0]
2718 | stfd f1, CTSTATE->cb.fpr[0]
2719 | stw r4, CTSTATE->cb.gpr[1]
2720 | stfd f2, CTSTATE->cb.fpr[1]
2721 | stw r5, CTSTATE->cb.gpr[2]
2722 | stfd f3, CTSTATE->cb.fpr[2]
2723 | stw r6, CTSTATE->cb.gpr[3]
2724 | stfd f4, CTSTATE->cb.fpr[3]
2725 | stw r7, CTSTATE->cb.gpr[4]
2726 | stfd f5, CTSTATE->cb.fpr[4]
2727 | stw r8, CTSTATE->cb.gpr[5]
2728 | stfd f6, CTSTATE->cb.fpr[5]
2729 | stw r9, CTSTATE->cb.gpr[6]
2730 | stfd f7, CTSTATE->cb.fpr[6]
2731 | stw r10, CTSTATE->cb.gpr[7]
2732 | stfd f8, CTSTATE->cb.fpr[7]
2733 | addi TMP0, sp, CFRAME_SPACE+8
2734 | stw TMP0, CTSTATE->cb.stack
2736 | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2738 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2739 | // Returns lua_State *.
2740 | lp BASE, L:CRET1->base
2741 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2742 | lp RC, L:CRET1->top
2743 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2747 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2748 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2749 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2750 | stw TMP0, TONUM_HI
2751 | li TISNIL, LJ_TNIL
2761 |->cont_ffi_callback: // Return from FFI callback.
2763 | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
2769 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2770 | lwz CRET1, CTSTATE->cb.gpr[0]
2771 | lfd FARG1, CTSTATE->cb.fpr[0]
2772 | lwz CRET2, CTSTATE->cb.gpr[1]
2776 |->vm_ffi_call: // Call C function via FFI.
2777 | // Caveat: needs special frame unwinding, see below.
2779 | .type CCSTATE, CCallState, CARG1
2780 | lwz TMP1, CCSTATE->spadj
2782 | lbz CARG2, CCSTATE->nsp
2783 | lbz CARG3, CCSTATE->nfpr
2786 | cmpwi cr1, CARG3, 0
2788 | addic. CARG2, CARG2, -1
2789 | stwux sp, sp, TMP1
2790 | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls.
2792 | stw CCSTATE, -8(TMP2)
2794 | la TMP1, CCSTATE->stack
2795 | slwi CARG2, CARG2, 2
2799 | lwzx TMP0, TMP1, CARG2
2800 | stwx TMP0, TMP2, CARG2
2801 | addic. CARG2, CARG2, -4
2805 | lfd f1, CCSTATE->fpr[0]
2806 | lfd f2, CCSTATE->fpr[1]
2807 | lfd f3, CCSTATE->fpr[2]
2808 | lfd f4, CCSTATE->fpr[3]
2809 | lfd f5, CCSTATE->fpr[4]
2810 | lfd f6, CCSTATE->fpr[5]
2811 | lfd f7, CCSTATE->fpr[6]
2812 | lfd f8, CCSTATE->fpr[7]
2814 | lp TMP0, CCSTATE->func
2815 | lwz CARG2, CCSTATE->gpr[1]
2816 | lwz CARG3, CCSTATE->gpr[2]
2817 | lwz CARG4, CCSTATE->gpr[3]
2818 | lwz CARG5, CCSTATE->gpr[4]
2820 | lwz r8, CCSTATE->gpr[5]
2821 | lwz r9, CCSTATE->gpr[6]
2822 | lwz r10, CCSTATE->gpr[7]
2823 | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
2825 | lwz CCSTATE:TMP1, -8(r14)
2828 | stw CARG1, CCSTATE:TMP1->gpr[0]
2829 | stfd FARG1, CCSTATE:TMP1->fpr[0]
2830 | stw CARG2, CCSTATE:TMP1->gpr[1]
2832 | stw CARG3, CCSTATE:TMP1->gpr[2]
2834 | stw CARG4, CCSTATE:TMP1->gpr[3]
2838 |// Note: vm_ffi_call must be the last function in this object file!
2840 |//-----------------------------------------------------------------------
2843 /* Generate the code for a single instruction. */
2844 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2851 /* -- Comparison ops ---------------------------------------------------- */
2853 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2855 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2856 | // RA = src1*8, RD = src2*8, JMP with RD = target
2858 | lwzux TMP0, RA, BASE
2861 | lwzux TMP1, RD, BASE
2863 | checknum cr0, TMP0
2865 | decode_RD4 TMP2, TMP2
2866 | checknum cr1, TMP1
2867 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2871 if (op == BC_ISLT) {
2873 } else if (op == BC_ISGE) {
2875 } else if (op == BC_ISLE) {
2885 |7: // RA is not an integer.
2886 | bgt cr0, ->vmeta_comp
2887 | // RA is a number.
2889 | bgt cr1, ->vmeta_comp
2891 | // RA is a number, RD is an integer.
2895 |8: // RA is an integer, RD is not an integer.
2896 | bgt cr1, ->vmeta_comp
2897 | // RA is an integer, RD is a number.
2903 if (op == BC_ISLT) {
2905 } else if (op == BC_ISGE) {
2907 } else if (op == BC_ISLE) {
2908 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2911 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2916 | lwzx TMP0, BASE, RA
2919 | lwzx TMP1, BASE, RD
2920 | checknum cr0, TMP0
2923 | checknum cr1, TMP1
2924 | decode_RD4 TMP2, TMP2
2925 | bge cr0, ->vmeta_comp
2926 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2927 | bge cr1, ->vmeta_comp
2929 if (op == BC_ISLT) {
2931 } else if (op == BC_ISGE) {
2933 } else if (op == BC_ISLE) {
2934 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2937 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2946 case BC_ISEQV: case BC_ISNEV:
2947 vk = op == BC_ISEQV;
2948 | // RA = src1*8, RD = src2*8, JMP with RD = target
2950 | lwzux TMP0, RA, BASE
2953 | lwzux TMP1, RD, BASE
2954 | checknum cr0, TMP0
2956 | checknum cr1, TMP1
2957 | decode_RD4 TMP2, TMP2
2959 | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt
2960 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2962 | ble cr7, ->BC_ISEQN_Z
2964 | ble cr7, ->BC_ISNEN_Z
2967 | lwzux TMP0, RA, BASE
2971 | lwzux TMP1, RD, BASE
2972 | checknum cr0, TMP0
2973 | decode_RD4 TMP2, TMP2
2975 | checknum cr1, TMP1
2976 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2990 |5: // Either or both types are not numbers.
2996 | cmpwi cr7, TMP0, LJ_TCDATA
2997 | cmpwi cr5, TMP1, LJ_TCDATA
3001 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
3003 | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq
3005 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
3007 | beq cr7, ->vmeta_equal_cd
3009 | cmplw cr5, CARG2, CARG3
3010 | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive.
3011 | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type.
3012 | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv.
3014 | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2.
3015 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2.
3026 | bge cr0, >2 // Done if 1 or 2.
3031 | blt cr0, <1 // Done if 1 or 2.
3033 | blt cr6, <1 // Done if not tab/ud.
3035 | // Different tables or userdatas. Need to check __eq metamethod.
3036 | // Field metatable must be at same offset for GCtab and GCudata!
3037 | lwz TAB:TMP2, TAB:CARG2->metatable
3038 | li CARG4, 1-vk // ne = 0 or 1.
3039 | cmplwi TAB:TMP2, 0
3040 | beq <1 // No metatable?
3041 | lbz TMP2, TAB:TMP2->nomm
3042 | andix. TMP2, TMP2, 1<<MM_eq
3043 | bne <1 // Or 'no __eq' flag set?
3044 | mr PC, SAVE0 // Restore old PC.
3045 | b ->vmeta_equal // Handle __eq metamethod.
3048 case BC_ISEQS: case BC_ISNES:
3049 vk = op == BC_ISEQS;
3050 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3051 | lwzux TMP0, RA, BASE
3053 | lwz STR:TMP3, 4(RA)
3058 | cmpwi TMP0, LJ_TCDATA
3060 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
3061 | .gpr64 extsw TMP0, TMP0
3062 | subfic TMP0, TMP0, LJ_TSTR
3064 | beq ->vmeta_equal_cd
3066 | sub TMP1, STR:TMP1, STR:TMP3
3067 | or TMP0, TMP0, TMP1
3068 | decode_RD4 TMP2, TMP2
3069 | subfic TMP0, TMP0, 0
3070 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3071 | subfe TMP1, TMP1, TMP1
3073 | andc TMP2, TMP2, TMP1
3075 | and TMP2, TMP2, TMP1
3081 case BC_ISEQN: case BC_ISNEN:
3082 vk = op == BC_ISEQN;
3083 | // RA = src*8, RD = num_const*8, JMP with RD = target
3085 | lwzux TMP0, RA, BASE
3088 | lwzux TMP1, RD, KBASE
3089 | checknum cr0, TMP0
3091 | checknum cr1, TMP1
3092 | decode_RD4 TMP2, TMP2
3094 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3106 |->BC_ISEQN_Z: // Dummy label.
3108 |->BC_ISNEN_Z: // Dummy label.
3110 | lwzx TMP0, BASE, RA
3114 | lfdx f1, KBASE, RD
3115 | decode_RD4 TMP2, TMP2
3117 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3140 | cmpwi TMP0, LJ_TCDATA
3141 | beq ->vmeta_equal_cd
3145 |7: // RA is not an integer.
3147 | // RA is a number.
3150 | // RA is a number, RD is an integer.
3154 |8: // RA is an integer, RD is a number.
3164 case BC_ISEQP: case BC_ISNEP:
3165 vk = op == BC_ISEQP;
3166 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3167 | lwzx TMP0, BASE, RA
3173 | cmpwi TMP0, LJ_TCDATA
3175 | sub TMP0, TMP0, TMP1
3177 | beq ->vmeta_equal_cd
3179 | decode_RD4 TMP2, TMP2
3180 | .gpr64 extsw TMP0, TMP0
3181 | addic TMP0, TMP0, -1
3182 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3183 | subfe TMP1, TMP1, TMP1
3185 | and TMP2, TMP2, TMP1
3187 | andc TMP2, TMP2, TMP1
3193 /* -- Unary test and copy ops ------------------------------------------- */
3195 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3196 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3197 | lwzx TMP0, BASE, RD
3200 if (op == BC_IST || op == BC_ISF) {
3201 | .gpr64 extsw TMP0, TMP0
3202 | subfic TMP0, TMP0, LJ_TTRUE
3203 | decode_RD4 TMP2, INS
3204 | subfe TMP1, TMP1, TMP1
3205 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3207 | andc TMP2, TMP2, TMP1
3209 | and TMP2, TMP2, TMP1
3213 | li TMP1, LJ_TFALSE
3216 if (op == BC_ISTC) {
3221 | addis PC, PC, -(BCBIAS_J*4 >> 16)
3222 | decode_RD4 TMP2, INS
3223 | stfdx f0, BASE, RA
3230 /* -- Unary ops --------------------------------------------------------- */
3233 | // RA = dst*8, RD = src*8
3236 | stfdx f0, BASE, RA
3240 | // RA = dst*8, RD = src*8
3242 | lwzx TMP0, BASE, RD
3243 | .gpr64 extsw TMP0, TMP0
3244 | subfic TMP1, TMP0, LJ_TTRUE
3245 | adde TMP0, TMP0, TMP1
3246 | stwx TMP0, BASE, RA
3250 | // RA = dst*8, RD = src*8
3251 | lwzux TMP1, RD, BASE
3267 | stwux TISNUM, RA, BASE
3273 | // Potential overflow.
3274 | mcrxr cr0; bley <1 // Ignore unrelated overflow.
3276 | lus TMP1, 0x41e0 // 2^31.
3282 | xoris TMP1, TMP1, 0x8000
3285 | stwux TMP1, RA, BASE
3294 | // RA = dst*8, RD = src*8
3295 | lwzux TMP0, RD, BASE
3297 | checkstr TMP0; bne >2
3298 | lwz CRET1, STR:CARG1->len
3302 | stwux TISNUM, RA, BASE
3305 | tonum_u f0, CRET1 // Result is a non-negative integer.
3307 | stfdx f0, BASE, RA
3311 | checktab TMP0; bne ->vmeta_len
3313 | lwz TAB:TMP2, TAB:CARG1->metatable
3314 | cmplwi TAB:TMP2, 0
3319 | bl extern lj_tab_len // (GCtab *t)
3320 | // Returns uint32_t (but less than 2^31).
3324 | lbz TMP0, TAB:TMP2->nomm
3325 | andix. TMP0, TMP0, 1<<MM_len
3326 | bne <3 // 'no __len' flag set: done.
3331 /* -- Binary ops -------------------------------------------------------- */
3333 |.macro ins_arithpre
3334 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3335 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3338 | lwzx TMP1, BASE, RB
3340 | lwzx TMP2, KBASE, RC
3342 | lfdx f14, BASE, RB
3343 | lfdx f15, KBASE, RC
3345 | checknum cr0, TMP1
3346 | checknum cr1, TMP2
3347 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3348 | bge ->vmeta_arith_vn
3350 | checknum TMP1; bge ->vmeta_arith_vn
3354 | lwzx TMP1, BASE, RB
3356 | lwzx TMP2, KBASE, RC
3358 | lfdx f15, BASE, RB
3359 | lfdx f14, KBASE, RC
3361 | checknum cr0, TMP1
3362 | checknum cr1, TMP2
3363 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3364 | bge ->vmeta_arith_nv
3366 | checknum TMP1; bge ->vmeta_arith_nv
3370 | lwzx TMP1, BASE, RB
3371 | lwzx TMP2, BASE, RC
3372 | lfdx f14, BASE, RB
3373 | lfdx f15, BASE, RC
3374 | checknum cr0, TMP1
3375 | checknum cr1, TMP2
3376 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3377 | bge ->vmeta_arith_vv
3382 |.macro ins_arithfallback, ins
3385 | ins ->vmeta_arith_vn2
3388 | ins ->vmeta_arith_nv2
3391 | ins ->vmeta_arith_vv2
3396 |.macro intmod, a, b, c
3400 |.macro fpmod, a, b, c
3403 | // NYI: Use internal implementation of floor.
3404 | blex floor // floor(b/c)
3406 | fsub a, b, a // b - floor(b/c)*c
3409 |.macro ins_arithfp, fpins
3411 |.if "fpins" == "fpmod_"
3412 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3414 | fpins f0, f14, f15
3416 | stfdx f0, BASE, RA
3421 |.macro ins_arithdn, intins, fpins
3422 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3423 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3426 | lwzux TMP1, RB, BASE
3427 | lwzux TMP2, RC, KBASE
3429 | checknum cr0, TMP1
3433 | lwzux TMP1, RB, BASE
3434 | lwzux TMP2, RC, KBASE
3436 | checknum cr0, TMP1
3440 | lwzux TMP1, RB, BASE
3441 | lwzux TMP2, RC, BASE
3443 | checknum cr0, TMP1
3447 | checknum cr1, TMP2
3450 | intins CARG1, CARG1, CARG2
3454 | stwux TISNUM, RA, BASE
3459 | mcrxr cr0; bley <1 // Ignore unrelated overflow.
3460 | ins_arithfallback b
3464 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3468 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3471 | ins_arithfallback bge
3472 |.if "fpins" == "fpmod_"
3473 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3475 | fpins f0, f14, f15
3477 | stfdx f0, BASE, RA
3482 |.macro ins_arith, intins, fpins
3484 | ins_arithdn intins, fpins
3490 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3492 |.macro addo32., y, a, b
3493 | // Need to check overflow for (a<<32) + (b<<32).
3494 | rldicr TMP0, a, 32, 31
3495 | rldicr TMP3, b, 32, 31
3496 | addo. TMP0, TMP0, TMP3
3499 | ins_arith addo32., fadd
3501 | ins_arith addo., fadd
3504 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3506 |.macro subo32., y, a, b
3507 | // Need to check overflow for (a<<32) - (b<<32).
3508 | rldicr TMP0, a, 32, 31
3509 | rldicr TMP3, b, 32, 31
3510 | subo. TMP0, TMP0, TMP3
3513 | ins_arith subo32., fsub
3515 | ins_arith subo., fsub
3518 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3519 | ins_arith mullwo., fmul
3521 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3525 | ins_arith intmod, fpmod
3527 case BC_MODNV: case BC_MODVV:
3528 | ins_arith intmod, fpmod_
3531 | // NYI: (partial) integer arithmetic.
3532 | lwzx TMP1, BASE, RB
3533 | lfdx FARG1, BASE, RB
3534 | lwzx TMP2, BASE, RC
3535 | lfdx FARG2, BASE, RC
3536 | checknum cr0, TMP1
3537 | checknum cr1, TMP2
3538 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3539 | bge ->vmeta_arith_vv
3542 | stfdx FARG1, BASE, RA
3547 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3550 | add CARG2, BASE, RC
3555 | srwi CARG3, CARG3, 3
3556 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3557 | // Returns NULL (finished) or TValue * (metamethod).
3562 | lfdx f0, BASE, SAVE0 // Copy result from RB to RA.
3563 | stfdx f0, BASE, RA
3567 /* -- Constant ops ------------------------------------------------------ */
3570 | // RA = dst*8, RD = str_const*8 (~)
3572 | subfic TMP1, TMP1, -4
3574 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
3576 | stwux TMP2, RA, BASE
3582 | // RA = dst*8, RD = cdata_const*8 (~)
3584 | subfic TMP1, TMP1, -4
3586 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
3587 | li TMP2, LJ_TCDATA
3588 | stwux TMP2, RA, BASE
3594 | // RA = dst*8, RD = int16_literal*8
3599 | stwux TISNUM, RA, BASE
3603 | // The soft-float approach is faster.
3605 | srawi TMP1, RD, 31
3606 | xor TMP2, TMP1, RD
3607 | sub TMP2, TMP2, TMP1 // TMP2 = abs(x)
3609 | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1
3610 | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa
3611 | subfic TMP3, RD, 0
3612 | slwi TMP1, TMP1, 20
3613 | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11)
3614 | subfe TMP0, TMP0, TMP0
3615 | add RD, RD, TMP1 // hi = hi + exponent-1
3616 | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi
3618 | stwux RD, RA, BASE
3624 | // RA = dst*8, RD = num_const*8
3626 | lfdx f0, KBASE, RD
3627 | stfdx f0, BASE, RA
3631 | // RA = dst*8, RD = primitive_type*8 (~)
3635 | stwx TMP0, BASE, RA
3639 | // RA = base*8, RD = end*8
3640 | stwx TISNIL, BASE, RA
3643 | stwx TISNIL, BASE, RA
3650 /* -- Upvalue and function ops ------------------------------------------ */
3653 | // RA = dst*8, RD = uvnum*8
3654 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3656 | addi RD, RD, offsetof(GCfuncL, uvptr)
3657 | lwzx UPVAL:RB, LFUNC:RB, RD
3659 | lwz TMP1, UPVAL:RB->v
3661 | stfdx f0, BASE, RA
3665 | // RA = uvnum*8, RD = src*8
3666 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3668 | addi RA, RA, offsetof(GCfuncL, uvptr)
3669 | lfdux f0, RD, BASE
3670 | lwzx UPVAL:RB, LFUNC:RB, RA
3671 | lbz TMP3, UPVAL:RB->marked
3672 | lwz CARG2, UPVAL:RB->v
3673 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3674 | lbz TMP0, UPVAL:RB->closed
3677 | cmplwi cr1, TMP0, 0
3679 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3680 | subi TMP2, TMP2, (LJ_TISNUM+1)
3681 | bne >2 // Upvalue is closed and black?
3685 |2: // Check if new value is collectable.
3686 | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1)
3687 | bge <1 // tvisgcv(v)
3688 | lbz TMP3, GCOBJ:TMP1->gch.marked
3689 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
3690 | la CARG1, GG_DISP2G(DISPATCH)
3691 | // Crossed a write barrier. Move the barrier forward.
3693 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3697 | // RA = uvnum*8, RD = str_const*8 (~)
3698 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3701 | subfic TMP1, TMP1, -4
3702 | addi RA, RA, offsetof(GCfuncL, uvptr)
3703 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
3704 | lwzx UPVAL:RB, LFUNC:RB, RA
3705 | lbz TMP3, UPVAL:RB->marked
3706 | lwz CARG2, UPVAL:RB->v
3707 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3708 | lbz TMP3, STR:TMP1->marked
3709 | lbz TMP2, UPVAL:RB->closed
3711 | stw STR:TMP1, 4(CARG2)
3712 | stw TMP0, 0(CARG2)
3717 |2: // Check if string is white and ensure upvalue is closed.
3718 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
3719 | cmplwi cr1, TMP2, 0
3720 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3721 | la CARG1, GG_DISP2G(DISPATCH)
3722 | // Crossed a write barrier. Move the barrier forward.
3724 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3728 | // RA = uvnum*8, RD = num_const*8
3729 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3731 | addi RA, RA, offsetof(GCfuncL, uvptr)
3732 | lfdx f0, KBASE, RD
3733 | lwzx UPVAL:RB, LFUNC:RB, RA
3735 | lwz TMP1, UPVAL:RB->v
3740 | // RA = uvnum*8, RD = primitive_type*8 (~)
3741 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3744 | addi RA, RA, offsetof(GCfuncL, uvptr)
3746 | lwzx UPVAL:RB, LFUNC:RB, RA
3748 | lwz TMP1, UPVAL:RB->v
3754 | // RA = level*8, RD = target
3755 | lwz TMP1, L->openupval
3756 | branch_RD // Do this first since RD is not saved.
3761 | add CARG2, BASE, RA
3762 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3769 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
3772 | subfic TMP1, TMP1, -4
3774 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3776 | lwz CARG3, FRAME_FUNC(BASE)
3777 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3778 | bl extern lj_func_newL_gc
3779 | // Returns GCfuncL *.
3782 | stwux TMP0, RA, BASE
3783 | stw LFUNC:CRET1, 4(RA)
3787 /* -- Table ops --------------------------------------------------------- */
3791 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
3792 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
3794 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
3800 if (op == BC_TNEW) {
3801 | rlwinm CARG2, RD, 29, 21, 31
3802 | rlwinm CARG3, RD, 18, 27, 31
3803 | cmpwi CARG2, 0x7ff; beq >3
3805 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3806 | // Returns Table *.
3809 | subfic TMP1, TMP1, -4
3810 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3811 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3812 | // Returns Table *.
3816 | stwux TMP0, RA, BASE
3817 | stw TAB:CRET1, 4(RA)
3819 if (op == BC_TNEW) {
3826 | bl extern lj_gc_step_fixtop // (lua_State *L)
3833 | // RA = dst*8, RD = str_const*8 (~)
3835 | // RA = src*8, RD = str_const*8 (~)
3836 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
3838 | lwz TAB:RB, LFUNC:TMP2->env
3839 | subfic TMP1, TMP1, -4
3840 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3841 if (op == BC_GGET) {
3849 | // RA = dst*8, RB = table*8, RC = key*8
3850 | lwzux CARG1, RB, BASE
3851 | lwzux CARG2, RC, BASE
3859 | checknum cr1, CARG2
3862 | lwz TMP0, TAB:RB->asize
3864 | lwz TMP1, TAB:RB->array
3869 | // Convert number key to integer, check for integerness and range.
3871 | fadd f2, f0, TOBIT
3873 | lwz TMP0, TAB:RB->asize
3874 | fsub f2, f2, TOBIT
3876 | lwz TMP1, TAB:RB->array
3878 | cmplw cr0, TMP0, TMP2
3879 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
3880 | slwi TMP2, TMP2, 3
3882 | ble ->vmeta_tgetv // Integer key and in array part?
3883 | lwzx TMP0, TMP1, TMP2
3884 | lfdx f14, TMP1, TMP2
3885 | checknil TMP0; beq >2
3888 | stfdx f14, BASE, RA
3891 |2: // Check for __index if table value is nil.
3892 | lwz TAB:TMP2, TAB:RB->metatable
3893 | cmplwi TAB:TMP2, 0
3894 | beq <1 // No metatable: done.
3895 | lbz TMP0, TAB:TMP2->nomm
3896 | andix. TMP0, TMP0, 1<<MM_index
3897 | bne <1 // 'no __index' flag set: done.
3901 | checkstr CARG2; bne ->vmeta_tgetv
3905 | b ->BC_TGETS_Z // String key?
3908 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
3909 | lwzux CARG1, RB, BASE
3912 | subfic TMP1, TMP1, -4
3914 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3915 | bne ->vmeta_tgets1
3917 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
3918 | lwz TMP0, TAB:RB->hmask
3919 | lwz TMP1, STR:RC->hash
3920 | lwz NODE:TMP2, TAB:RB->node
3921 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
3922 | slwi TMP0, TMP1, 5
3923 | slwi TMP1, TMP1, 3
3924 | sub TMP1, TMP0, TMP1
3925 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
3927 | lwz CARG1, NODE:TMP2->key
3928 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
3929 | lwz CARG2, NODE:TMP2->val
3930 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
3931 | checkstr CARG1; bne >4
3932 | cmpw TMP0, STR:RC; bne >4
3933 | checknil CARG2; beq >5 // Key found, but nil value?
3935 | stwux CARG2, RA, BASE
3939 |4: // Follow hash chain.
3940 | lwz NODE:TMP2, NODE:TMP2->next
3941 | cmplwi NODE:TMP2, 0
3943 | // End of hash chain: key not found, nil result.
3946 |5: // Check for __index if table value is nil.
3947 | lwz TAB:TMP2, TAB:RB->metatable
3948 | cmplwi TAB:TMP2, 0
3949 | beq <3 // No metatable: done.
3950 | lbz TMP0, TAB:TMP2->nomm
3951 | andix. TMP0, TMP0, 1<<MM_index
3952 | bne <3 // 'no __index' flag set: done.
3956 | // RA = dst*8, RB = table*8, RC = index*8
3957 | lwzux CARG1, RB, BASE
3960 | checktab CARG1; bne ->vmeta_tgetb
3961 | lwz TMP1, TAB:RB->asize
3962 | lwz TMP2, TAB:RB->array
3963 | cmplw TMP0, TMP1; bge ->vmeta_tgetb
3964 | lwzx TMP1, TMP2, RC
3966 | checknil TMP1; beq >5
3969 | stfdx f0, BASE, RA
3972 |5: // Check for __index if table value is nil.
3973 | lwz TAB:TMP2, TAB:RB->metatable
3974 | cmplwi TAB:TMP2, 0
3975 | beq <1 // No metatable: done.
3976 | lbz TMP2, TAB:TMP2->nomm
3977 | andix. TMP2, TMP2, 1<<MM_index
3978 | bne <1 // 'no __index' flag set: done.
3979 | b ->vmeta_tgetb // Caveat: preserve TMP0!
3983 | // RA = src*8, RB = table*8, RC = key*8
3984 | lwzux CARG1, RB, BASE
3985 | lwzux CARG2, RC, BASE
3993 | checknum cr1, CARG2
3996 | lwz TMP0, TAB:RB->asize
3998 | lwz TMP1, TAB:RB->array
4003 | // Convert number key to integer, check for integerness and range.
4005 | fadd f2, f0, TOBIT
4007 | lwz TMP0, TAB:RB->asize
4008 | fsub f2, f2, TOBIT
4010 | lwz TMP1, TAB:RB->array
4012 | cmplw cr0, TMP0, TMP2
4013 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4014 | slwi TMP0, TMP2, 3
4016 | ble ->vmeta_tsetv // Integer key and in array part?
4017 | lwzx TMP2, TMP1, TMP0
4018 | lbz TMP3, TAB:RB->marked
4019 | lfdx f14, BASE, RA
4020 | checknil TMP2; beq >3
4022 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4023 | stfdx f14, TMP1, TMP0
4028 |3: // Check for __newindex if previous value is nil.
4029 | lwz TAB:TMP2, TAB:RB->metatable
4030 | cmplwi TAB:TMP2, 0
4031 | beq <1 // No metatable: done.
4032 | lbz TMP2, TAB:TMP2->nomm
4033 | andix. TMP2, TMP2, 1<<MM_newindex
4034 | bne <1 // 'no __newindex' flag set: done.
4038 | checkstr CARG2; bne ->vmeta_tsetv
4042 | b ->BC_TSETS_Z // String key?
4044 |7: // Possible table write barrier for the value. Skip valiswhite check.
4045 | barrierback TAB:RB, TMP3, TMP0
4049 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4050 | lwzux CARG1, RB, BASE
4053 | subfic TMP1, TMP1, -4
4055 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4056 | bne ->vmeta_tsets1
4058 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
4059 | lwz TMP0, TAB:RB->hmask
4060 | lwz TMP1, STR:RC->hash
4061 | lwz NODE:TMP2, TAB:RB->node
4062 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
4063 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
4064 | lfdx f14, BASE, RA
4065 | slwi TMP0, TMP1, 5
4066 | slwi TMP1, TMP1, 3
4067 | sub TMP1, TMP0, TMP1
4068 | lbz TMP3, TAB:RB->marked
4069 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4071 | lwz CARG1, NODE:TMP2->key
4072 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4073 | lwz CARG2, NODE:TMP2->val
4074 | lwz NODE:TMP1, NODE:TMP2->next
4075 | checkstr CARG1; bne >5
4076 | cmpw TMP0, STR:RC; bne >5
4077 | checknil CARG2; beq >4 // Key found, but nil value?
4079 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4080 | stfd f14, NODE:TMP2->val
4085 |4: // Check for __newindex if previous value is nil.
4086 | lwz TAB:TMP1, TAB:RB->metatable
4087 | cmplwi TAB:TMP1, 0
4088 | beq <2 // No metatable: done.
4089 | lbz TMP0, TAB:TMP1->nomm
4090 | andix. TMP0, TMP0, 1<<MM_newindex
4091 | bne <2 // 'no __newindex' flag set: done.
4094 |5: // Follow hash chain.
4095 | cmplwi NODE:TMP1, 0
4096 | mr NODE:TMP2, NODE:TMP1
4098 | // End of hash chain: key not found, add a new one.
4100 | // But check for __newindex first.
4101 | lwz TAB:TMP1, TAB:RB->metatable
4102 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
4105 | cmplwi TAB:TMP1, 0
4107 | beq >6 // No metatable: continue.
4108 | lbz TMP0, TAB:TMP1->nomm
4109 | andix. TMP0, TMP0, 1<<MM_newindex
4110 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4113 | stw STR:RC, 4(CARG3)
4115 | stw TMP0, 0(CARG3)
4116 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4117 | // Returns TValue *.
4119 | stfd f14, 0(CRET1)
4120 | b <3 // No 2nd write barrier needed.
4122 |7: // Possible table write barrier for the value. Skip valiswhite check.
4123 | barrierback TAB:RB, TMP3, TMP0
4127 | // RA = src*8, RB = table*8, RC = index*8
4128 | lwzux CARG1, RB, BASE
4131 | checktab CARG1; bne ->vmeta_tsetb
4132 | lwz TMP1, TAB:RB->asize
4133 | lwz TMP2, TAB:RB->array
4134 | lbz TMP3, TAB:RB->marked
4136 | lfdx f14, BASE, RA
4138 | lwzx TMP1, TMP2, RC
4139 | checknil TMP1; beq >5
4141 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4142 | stfdx f14, TMP2, RC
4147 |5: // Check for __newindex if previous value is nil.
4148 | lwz TAB:TMP1, TAB:RB->metatable
4149 | cmplwi TAB:TMP1, 0
4150 | beq <1 // No metatable: done.
4151 | lbz TMP1, TAB:TMP1->nomm
4152 | andix. TMP1, TMP1, 1<<MM_newindex
4153 | bne <1 // 'no __newindex' flag set: done.
4154 | b ->vmeta_tsetb // Caveat: preserve TMP0!
4156 |7: // Possible table write barrier for the value. Skip valiswhite check.
4157 | barrierback TAB:RB, TMP3, TMP0
4162 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4165 | add TMP3, KBASE, RD
4166 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
4167 | addic. TMP0, MULTRES, -8
4168 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
4169 | srwi CARG3, TMP0, 3
4170 | beq >4 // Nothing to copy?
4171 | add CARG3, CARG3, TMP3
4172 | lwz TMP2, TAB:CARG2->asize
4173 | slwi TMP1, TMP3, 3
4174 | lbz TMP3, TAB:CARG2->marked
4176 | add TMP2, RA, TMP0
4177 | lwz TMP0, TAB:CARG2->array
4179 | add TMP1, TMP1, TMP0
4180 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4181 |3: // Copy result slots to table.
4184 | cmpw cr1, RA, TMP2
4186 | addi TMP1, TMP1, 8
4192 |5: // Need to resize array part.
4197 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4198 | // Must not reallocate the stack.
4202 |7: // Possible table write barrier for any value. Skip valiswhite check.
4203 | barrierback TAB:CARG2, TMP3, TMP0
4207 /* -- Calls and vararg handling ----------------------------------------- */
4210 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4211 | add NARGS8:RC, NARGS8:RC, MULTRES
4212 | // Fall through. Assumes BC_CALL follows.
4215 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4217 | lwzux TMP0, BASE, RA
4218 | lwz LFUNC:RB, 4(BASE)
4219 | subi NARGS8:RC, NARGS8:RC, 8
4220 | addi BASE, BASE, 8
4221 | checkfunc TMP0; bne ->vmeta_call
4226 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4227 | add NARGS8:RC, NARGS8:RC, MULTRES
4228 | // Fall through. Assumes BC_CALLT follows.
4231 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4232 | lwzux TMP0, RA, BASE
4233 | lwz LFUNC:RB, 4(RA)
4234 | subi NARGS8:RC, NARGS8:RC, 8
4235 | lwz TMP1, FRAME_PC(BASE)
4240 | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
4241 | lbz TMP3, LFUNC:RB->ffid
4242 | xori TMP2, TMP1, FRAME_VARG
4243 | cmplwi cr1, NARGS8:RC, 0
4246 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4248 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
4251 | addi TMP3, TMP2, 8
4253 | cmplw cr1, TMP3, NARGS8:RC
4254 | stfdx f0, BASE, TMP2
4258 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
4263 |5: // Tailcall to a fast function with a Lua frame below.
4265 | decode_RA8 RA, INS
4266 | sub TMP1, BASE, RA
4267 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
4268 | lwz TMP1, LFUNC:TMP1->pc
4269 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4272 |7: // Tailcall from a vararg function.
4273 | andix. TMP0, TMP2, FRAME_TYPEP
4274 | bne <1 // Vararg frame below?
4275 | sub BASE, BASE, TMP2 // Relocate BASE down.
4276 | lwz TMP1, FRAME_PC(BASE)
4277 | andix. TMP0, TMP1, FRAME_TYPE
4282 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4284 | add BASE, BASE, RA
4285 | lwz TMP1, -24(BASE)
4286 | lwz LFUNC:RB, -20(BASE)
4289 | stw TMP1, 0(BASE) // Copy callable.
4290 | stw LFUNC:RB, 4(BASE)
4292 | stfd f1, 16(BASE) // Copy control var.
4293 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4294 | stfdu f0, 8(BASE) // Copy state.
4300 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4302 | // NYI: add hotloop, record BC_ITERN.
4305 | lwz TAB:RB, -12(RA)
4306 | lwz RC, -4(RA) // Get index from control var.
4307 | lwz TMP0, TAB:RB->asize
4308 | lwz TMP1, TAB:RB->array
4310 |1: // Traverse array part.
4313 | bge >5 // Index points after array part?
4314 | lwzx TMP2, TMP1, TMP3
4315 | lfdx f0, TMP1, TMP3
4326 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
4328 | decode_RD4 TMP1, INS
4329 | stw RC, -4(RA) // Update control var.
4330 | add PC, TMP1, TMP3
4337 |4: // Skip holes in array part.
4341 |5: // Traverse hash part.
4342 | lwz TMP1, TAB:RB->hmask
4344 | lwz TMP2, TAB:RB->node
4346 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
4350 | sub TMP3, TMP3, RB
4351 | lwzx RB, TMP2, TMP3
4352 | lfdx f0, TMP2, TMP3
4353 | add NODE:TMP3, TMP2, TMP3
4357 | lfd f1, NODE:TMP3->key
4358 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
4361 | decode_RD4 TMP1, INS
4364 | add PC, TMP1, TMP2
4365 | stw RC, -4(RA) // Update control var.
4368 |7: // Skip holes in hash part.
4374 | // RA = base*8, RD = target (points to ITERN)
4377 | lwz CFUNC:TMP1, -20(RA)
4380 | cmpwi cr0, TMP2, LJ_TTAB
4381 | cmpwi cr1, TMP0, LJ_TFUNC
4382 | cmpwi cr6, TMP3, LJ_TNIL
4384 | lbz TMP1, CFUNC:TMP1->ffid
4385 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
4386 | cmpwi cr7, TMP1, FF_next_N
4388 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4389 | add TMP3, PC, TMP0
4392 | ori TMP1, TMP1, 0x7fff
4393 | stw ZERO, -4(RA) // Initialize control var.
4395 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4398 |5: // Despecialize bytecode if any of the checks fail.
4402 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4408 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4409 | lwz TMP0, FRAME_PC(BASE)
4412 | addi RC, RC, FRAME_VARG
4414 | subi TMP3, BASE, 8 // TMP3 = vtop
4415 | sub RC, RC, TMP0 // RC = vbase
4416 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4419 | sub TMP1, TMP3, RC
4422 | sub. TMP1, TMP3, RC
4424 | beq cr1, >5 // Copy all varargs?
4425 | subi TMP2, TMP2, 16
4426 | ble >2 // No vararg slots?
4427 |1: // Copy vararg slots to destination slots.
4432 | cmplw cr1, RC, TMP3
4433 | bge >3 // All destination slots filled?
4435 | blt cr1, <1 // More vararg slots?
4436 |2: // Fill up remainder with nil.
4444 |5: // Copy all varargs.
4445 | lwz TMP0, L->maxstack
4446 | li MULTRES, 8 // MULTRES = (0+1)*8
4447 | bley <3 // No vararg slots?
4448 | add TMP2, RA, TMP1
4450 | addi MULTRES, TMP1, 8
4458 | blt <6 // More vararg slots?
4461 |7: // Grow stack for varargs.
4464 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
4468 | srwi CARG2, TMP1, 3
4469 | bl extern lj_state_growstack // (lua_State *L, int n)
4472 | add RC, BASE, SAVE0
4473 | subi TMP3, BASE, 8
4477 /* -- Returns ----------------------------------------------------------- */
4480 | // RA = results*8, RD = extra_nresults*8
4481 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4482 | // Fall through. Assumes BC_RET follows.
4486 | // RA = results*8, RD = (nresults+1)*8
4487 | lwz PC, FRAME_PC(BASE)
4491 | andix. TMP0, PC, FRAME_TYPE
4492 | xori TMP1, PC, FRAME_VARG
4496 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4499 | subi TMP2, BASE, 8
4501 | decode_RB8 RB, INS
4505 | addi TMP3, TMP1, 8
4508 | stfdx f0, TMP2, TMP1
4510 | addi TMP1, TMP3, 8
4513 | stfdx f1, TMP2, TMP3
4518 | decode_RA8 RA, INS
4520 | sub BASE, TMP2, RA
4521 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4523 | lwz TMP1, LFUNC:TMP1->pc
4524 | lwz KBASE, PC2PROTO(k)(TMP1)
4527 |6: // Fill up results with nil.
4530 | stwx TISNIL, TMP2, TMP1
4533 |->BC_RETV_Z: // Non-standard return case.
4534 | andix. TMP2, TMP1, FRAME_TYPEP
4536 | // Return from vararg function: relocate BASE down.
4537 | sub BASE, BASE, TMP1
4538 | lwz PC, FRAME_PC(BASE)
4542 case BC_RET0: case BC_RET1:
4543 | // RA = results*8, RD = (nresults+1)*8
4544 | lwz PC, FRAME_PC(BASE)
4547 | andix. TMP0, PC, FRAME_TYPE
4548 | xori TMP1, PC, FRAME_VARG
4552 | subi TMP2, BASE, 8
4553 | decode_RB8 RB, INS
4554 if (op == BC_RET1) {
4560 | decode_RA8 RA, INS
4562 | sub BASE, TMP2, RA
4563 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4565 | lwz TMP1, LFUNC:TMP1->pc
4566 | lwz KBASE, PC2PROTO(k)(TMP1)
4569 |6: // Fill up results with nil.
4572 | stwx TISNIL, TMP2, TMP1
4576 /* -- Loops and branches ------------------------------------------------ */
4582 | // Fall through. Assumes BC_IFORL follows.
4592 | // RA = base*8, RD = target (after end of loop or start of loop)
4593 vk = (op == BC_IFORL || op == BC_JFORL);
4596 | lwzux TMP1, RA, BASE
4597 | lwz CARG1, FORL_IDX*8+4(RA)
4598 | cmplw cr0, TMP1, TISNUM
4600 | lwz CARG3, FORL_STEP*8+4(RA)
4603 | // Need to check overflow for (a<<32) + (b<<32).
4604 | rldicr TMP0, CARG1, 32, 31
4605 | rldicr TMP2, CARG3, 32, 31
4606 | add CARG1, CARG1, CARG3
4607 | addo. TMP0, TMP0, TMP2
4609 | addo. CARG1, CARG1, CARG3
4611 | cmpwi cr6, CARG3, 0
4612 | lwz CARG2, FORL_STOP*8+4(RA)
4615 | stw CARG1, FORL_IDX*8+4(RA)
4617 | lwz TMP3, FORL_STEP*8(RA)
4618 | lwz CARG3, FORL_STEP*8+4(RA)
4619 | lwz TMP2, FORL_STOP*8(RA)
4620 | lwz CARG2, FORL_STOP*8+4(RA)
4621 | cmplw cr7, TMP3, TISNUM
4622 | cmplw cr1, TMP2, TISNUM
4623 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4624 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4625 | cmpwi cr6, CARG3, 0
4631 | stw TISNUM, FORL_EXT*8(RA)
4632 if (op != BC_JFORL) {
4635 | stw CARG1, FORL_EXT*8+4(RA)
4636 if (op != BC_JFORL) {
4639 if (op == BC_FORI) {
4640 | bgt >3 // See FP loop below.
4641 } else if (op == BC_JFORI) {
4642 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4644 } else if (op == BC_IFORL) {
4646 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4652 |5: // Invert check for negative step.
4656 |6: // Potential overflow.
4657 | mcrxr cr0; bley <4 // Ignore unrelated overflow.
4664 | lfd f1, FORL_IDX*8(RA)
4666 | lfdux f1, RA, BASE
4668 | lfd f3, FORL_STEP*8(RA)
4669 | lfd f2, FORL_STOP*8(RA)
4670 | lwz TMP3, FORL_STEP*8(RA)
4672 | stfd f1, FORL_IDX*8(RA)
4677 | lwzux TMP1, RA, BASE
4678 | lwz TMP3, FORL_STEP*8(RA)
4679 | lwz TMP2, FORL_STOP*8(RA)
4680 | cmplw cr0, TMP1, TISNUM
4681 | cmplw cr7, TMP3, TISNUM
4682 | cmplw cr1, TMP2, TISNUM
4684 | lfd f1, FORL_IDX*8(RA)
4685 | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
4686 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4687 | lfd f2, FORL_STOP*8(RA)
4690 | cmpwi cr6, TMP3, 0
4691 if (op != BC_JFORL) {
4694 | stfd f1, FORL_EXT*8(RA)
4695 if (op != BC_JFORL) {
4699 if (op == BC_JFORI) {
4700 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4703 if (op == BC_FORI) {
4705 } else if (op == BC_IFORL) {
4712 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4713 } else if (op == BC_JFORI) {
4724 |5: // Negative step.
4725 if (op == BC_FORI) {
4727 |3: // Used by integer loop, too.
4728 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4729 } else if (op == BC_IFORL) {
4731 } else if (op == BC_JFORI) {
4737 if (op == BC_JFORI) {
4740 | decode_RD8 RD, INS
4749 | // Fall through. Assumes BC_IITERL follows.
4757 | // RA = base*8, RD = target
4758 | lwzux TMP1, RA, BASE
4760 | checknil TMP1; beq >1 // Stop if iterator returned nil.
4761 if (op == BC_JITERL) {
4766 | branch_RD // Otherwise save control var + branch.
4775 | // RA = base*8, RD = target (loop extent)
4776 | // Note: RA/RD is only used by trace recorder to determine scope/extent
4777 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4781 | // Fall through. Assumes BC_ILOOP follows.
4785 | // RA = base*8, RD = target (loop extent)
4791 | // RA = base*8 (ignored), RD = traceno*8
4792 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
4794 | // Traces on PPC don't store the trace number, so use 0.
4795 | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH)
4796 | lwzx TRACE:TMP2, TMP1, RD
4797 | mcrxr cr0 // Clear SO flag.
4798 | lp TMP2, TRACE:TMP2->mcode
4799 | stw BASE, DISPATCH_GL(jit_base)(DISPATCH)
4801 | stw L, DISPATCH_GL(jit_L)(DISPATCH)
4802 | addi JGL, DISPATCH, GG_DISP2G+32768
4808 | // RA = base*8 (only used by trace recorder), RD = target
4813 /* -- Function headers -------------------------------------------------- */
4819 case BC_FUNCV: /* NYI: compiled vararg functions. */
4820 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4828 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4829 | lwz TMP2, L->maxstack
4830 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
4831 | lwz KBASE, -4+PC2PROTO(k)(PC)
4833 | slwi TMP1, TMP1, 3
4834 | bgt ->vm_growstack_l
4835 if (op != BC_JFUNCF) {
4839 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
4841 if (op == BC_JFUNCF) {
4842 | decode_RD8 RD, INS
4848 |3: // Clear missing parameters.
4849 | stwx TISNIL, BASE, NARGS8:RC
4850 | addi NARGS8:RC, NARGS8:RC, 8
4858 | NYI // NYI: compiled vararg functions
4859 break; /* NYI: compiled vararg functions. */
4862 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4863 | lwz TMP2, L->maxstack
4864 | add TMP1, BASE, RC
4866 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
4867 | addi TMP3, RC, 8+FRAME_VARG
4868 | lwz KBASE, -4+PC2PROTO(k)(PC)
4870 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
4871 | bge ->vm_growstack_l
4872 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
4877 | addi BASE, TMP1, 8
4880 | cmplw RA, RC // Less args than parameters?
4884 | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
4887 | addic. TMP2, TMP2, -1
4889 | stw TMP3, 12(TMP1)
4890 | addi TMP1, TMP1, 8
4895 |4: // Clear missing parameters.
4902 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
4903 if (op == BC_FUNCC) {
4904 | lp RD, CFUNC:RB->f
4906 | lp RD, DISPATCH_GL(wrapf)(DISPATCH)
4908 | add TMP1, RA, NARGS8:RC
4909 | lwz TMP2, L->maxstack
4910 | .toc lp TMP3, 0(RD)
4911 | add RC, BASE, NARGS8:RC
4921 if (op == BC_FUNCCW) {
4922 | lp CARG2, CFUNC:RB->f
4925 | bgt ->vm_growstack_c // Need to grow stack.
4926 | .toc lp TOCREG, TOC_OFS(RD)
4927 | .tocenv lp ENVREG, ENV_OFS(RD)
4929 | bctrl // (lua_State *L [, lua_CFunction f])
4930 | // Returns nresults.
4932 | .toc ld TOCREG, SAVE_TOC
4936 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
4937 | sub RA, TMP1, RD // RA = L->top - nresults*8
4942 /* ---------------------------------------------------------------------- */
4945 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4951 static int build_backend(BuildCtx *ctx)
4955 dasm_growpc(Dst, BC__MAX);
4957 build_subroutines(ctx);
4960 for (op = 0; op < BC__MAX; op++)
4961 build_ins(ctx, (BCOp)op, op);
4966 /* Emit pseudo frame-info for all assembler functions. */
4967 static void emit_asm_debug(BuildCtx *ctx)
4969 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4971 switch (ctx->mode) {
4973 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
4976 "\t.long .LECIE0-.LSCIE0\n"
4978 "\t.long 0xffffffff\n"
4984 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
4989 "\t.long .LEFDE0-.LASFDE0\n"
4991 "\t.long .Lframe0\n"
4994 "\t.byte 0xe\n\t.uleb128 %d\n"
4995 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
4996 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
4997 fcofs, CFRAME_SIZE);
4998 for (i = 14; i <= 31; i++)
5000 "\t.byte %d\n\t.uleb128 %d\n"
5001 "\t.byte %d\n\t.uleb128 %d\n",
5002 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5009 "\t.long .LEFDE1-.LASFDE1\n"
5011 "\t.long .Lframe0\n"
5013 "\t.long .lj_vm_ffi_call\n"
5015 "\t.long lj_vm_ffi_call\n"
5018 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5019 "\t.byte 0x8e\n\t.uleb128 2\n"
5020 "\t.byte 0xd\n\t.uleb128 0xe\n"
5022 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5025 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5028 "\t.long .LECIE1-.LSCIE1\n"
5032 "\t.string \"zPR\"\n"
5036 "\t.uleb128 6\n" /* augmentation length */
5037 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5038 "\t.long lj_err_unwind_dwarf-.\n"
5039 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5040 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5045 "\t.long .LEFDE2-.LASFDE2\n"
5047 "\t.long .LASFDE2-.Lframe1\n"
5048 "\t.long .Lbegin-.\n"
5050 "\t.uleb128 0\n" /* augmentation length */
5051 "\t.byte 0xe\n\t.uleb128 %d\n"
5052 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5053 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5054 fcofs, CFRAME_SIZE);
5055 for (i = 14; i <= 31; i++)
5057 "\t.byte %d\n\t.uleb128 %d\n"
5058 "\t.byte %d\n\t.uleb128 %d\n",
5059 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5066 "\t.long .LECIE2-.LSCIE2\n"
5070 "\t.string \"zR\"\n"
5074 "\t.uleb128 1\n" /* augmentation length */
5075 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5076 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5081 "\t.long .LEFDE3-.LASFDE3\n"
5083 "\t.long .LASFDE3-.Lframe2\n"
5084 "\t.long lj_vm_ffi_call-.\n"
5086 "\t.uleb128 0\n" /* augmentation length */
5087 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5088 "\t.byte 0x8e\n\t.uleb128 2\n"
5089 "\t.byte 0xd\n\t.uleb128 0xe\n"
5091 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);