1 |// Low-level VM code for PowerPC 32 bit or 32on64 bit mode.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2015 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: see vm_ppc64.dasc for a full PPC64 _LP64 port.
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 |// FRAME32 Use 32 bit frame layout, even with GPR64 (Xbox 360).
25 |// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3).
26 |// Function pointers are really a struct: code, TOC, env (optional).
27 |// TOCENV Function pointers have an environment pointer, too (not on PS3).
28 |// PPE Power Processor Element of Cell (PS3) or Xenon (Xbox 360).
29 |// Must avoid (slow) micro-coded instructions.
34 |.macro lpx, a, b, c; ldx a, b, c; .endmacro
35 |.macro lp, a, b; ld a, b; .endmacro
36 |.macro stp, a, b; std a, b; .endmacro
37 |.define decode_OPP, decode_OP8
39 |// Missing: Calling conventions, 64 bit regs, TOC.
40 |.error lib_ffi not yet implemented for PPC64
43 |.macro lpx, a, b, c; lwzx a, b, c; .endmacro
44 |.macro lp, a, b; lwz a, b; .endmacro
45 |.macro stp, a, b; stw a, b; .endmacro
46 |.define decode_OPP, decode_OP4
49 |// Convenience macros for TOC handling.
51 |// Linker needs a TOC patch area for every external call relocation.
52 |.macro blex, target; bl extern target@plt; nop; .endmacro
53 |.macro .toc, a, b; a, b; .endmacro
62 |.macro blex, target; bl extern target@plt; .endmacro
63 |.macro .toc, a, b; .endmacro
65 |.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro
67 |.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro
69 |.macro andix., y, a, i
71 | rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i)
87 |.macro checkov, reg, noov
101 |//-----------------------------------------------------------------------
103 |// Fixed register assignments for the interpreter.
104 |// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA)
106 |// The following must be C callee-save (but BASE is often refetched).
107 |.define BASE, r14 // Base of current Lua stack frame.
108 |.define KBASE, r15 // Constants of current Lua function.
109 |.define PC, r16 // Next PC.
110 |.define DISPATCH, r17 // Opcode dispatch table.
111 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
112 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
113 |.define JGL, r31 // On-trace: global_State + 32768.
115 |// Constants for type-comparisons, stores and conversions. C callee-save.
119 |.define TOBIT, f30 // 2^52 + 2^51.
120 |.define TONUM, f31 // 2^52 + 2^51 + 2^31.
122 |// The following temporaries are not saved across C calls, except for RA.
123 |.define RA, r20 // Callee-save.
127 |.define INS, r7 // Overlaps CARG5.
132 |.define TMP3, r6 // Overlaps CARG4.
134 |// Saved temporaries.
137 |// Calling conventions.
141 |.define CARG4, r6 // Overlaps TMP3.
142 |.define CARG5, r7 // Overlaps INS.
150 |.define TOCREG, r2 // TOC register (only used by C code).
151 |.define ENVREG, r11 // Environment pointer (nested C functions).
153 |// Stack layout while in interpreter. Must match with lj_frame.h.
157 |// 456(sp) // \ 32/64 bit C frame info
158 |.define TONUM_LO, 452(sp) // |
159 |.define TONUM_HI, 448(sp) // |
160 |.define TMPD_LO, 444(sp) // |
161 |.define TMPD_HI, 440(sp) // |
162 |.define SAVE_CR, 432(sp) // | 64 bit CR save.
163 |.define SAVE_ERRF, 424(sp) // > Parameter save area.
164 |.define SAVE_NRES, 420(sp) // |
165 |.define SAVE_L, 416(sp) // |
166 |.define SAVE_PC, 412(sp) // |
167 |.define SAVE_MULTRES, 408(sp) // |
168 |.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain.
169 |// 392(sp) // Reserved.
170 |.define CFRAME_SPACE, 384 // Delta for sp.
171 |// Back chain for sp: 384(sp) <-- sp entering interpreter
172 |.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part.
173 |.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves.
174 |.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves.
175 |// 80(sp) // Needed for 16 byte stack frame alignment.
176 |// 16(sp) // Callee parameter save area (ABI mandated).
177 |// 8(sp) // Reserved
178 |// Back chain for sp: 0(sp) <-- sp while in interpreter
179 |// 32 bit sp stored in hi-part of 0(sp).
181 |.define TMPD_BLO, 447(sp)
182 |.define TMPD, TMPD_HI
183 |.define TONUM_D, TONUM_HI
187 |// 508(sp) // \ 32 bit C frame info.
188 |.define SAVE_ERRF, 472(sp) // |
189 |.define SAVE_NRES, 468(sp) // |
190 |.define SAVE_L, 464(sp) // > Parameter save area.
191 |.define SAVE_PC, 460(sp) // |
192 |.define SAVE_MULTRES, 456(sp) // |
193 |.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain.
194 |.define SAVE_LR, 416(sp)
195 |.define CFRAME_SPACE, 400 // Delta for sp.
196 |// Back chain for sp: 400(sp) <-- sp entering interpreter
197 |.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves.
198 |.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves.
199 |// 48(sp) // Callee parameter save area (ABI mandated).
200 |.define SAVE_TOC, 40(sp) // TOC save area.
201 |.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated).
202 |.define TMPD_HI, 32(sp) // /
203 |.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated).
204 |.define TONUM_HI, 24(sp) // /
205 |// Next frame lr: 16(sp)
206 |.define SAVE_CR, 8(sp) // 64 bit CR save.
207 |// Back chain for sp: 0(sp) <-- sp while in interpreter
209 |.define TMPD_BLO, 39(sp)
210 |.define TMPD, TMPD_HI
211 |.define TONUM_D, TONUM_HI
216 |.define SAVE_LR, 276(sp)
217 |.define CFRAME_SPACE, 272 // Delta for sp.
218 |// Back chain for sp: 272(sp) <-- sp entering interpreter
219 |.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves.
220 |.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves.
221 |.define SAVE_CR, 52(sp) // 32 bit CR save.
222 |.define SAVE_ERRF, 48(sp) // 32 bit C frame info.
223 |.define SAVE_NRES, 44(sp)
224 |.define SAVE_CFRAME, 40(sp)
225 |.define SAVE_L, 36(sp)
226 |.define SAVE_PC, 32(sp)
227 |.define SAVE_MULTRES, 28(sp)
228 |.define UNUSED1, 24(sp)
229 |.define TMPD_LO, 20(sp)
230 |.define TMPD_HI, 16(sp)
231 |.define TONUM_LO, 12(sp)
232 |.define TONUM_HI, 8(sp)
233 |// Next frame lr: 4(sp)
234 |// Back chain for sp: 0(sp) <-- sp while in interpreter
236 |.define TMPD_BLO, 23(sp)
237 |.define TMPD, TMPD_HI
238 |.define TONUM_D, TONUM_HI
244 | std r..reg, SAVE_GPR_+(reg-14)*8(sp)
246 | stw r..reg, SAVE_GPR_+(reg-14)*4(sp)
248 | stfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
252 | ld r..reg, SAVE_GPR_+(reg-14)*8(sp)
254 | lwz r..reg, SAVE_GPR_+(reg-14)*4(sp)
256 | lfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
260 |.if GPR64 and not FRAME32
261 | stdu sp, -CFRAME_SPACE(sp)
263 | stwu sp, -CFRAME_SPACE(sp)
265 | save_ 14; save_ 15; save_ 16
267 | save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22
268 |.if GPR64 and not FRAME32
273 | save_ 23; save_ 24; save_ 25
275 | save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31
281 | .toc std TOCREG, SAVE_TOC
285 |.if GPR64 and not FRAME32
295 | rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19
297 |.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif
298 | rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25
299 |.if PPE; mtocrf 0x10, r12; .endif
300 | rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31
301 |.if PPE; mtocrf 0x08, r12; .endif
302 | addi sp, sp, CFRAME_SPACE
305 |// Type definitions. Some of these are only used for documentation.
306 |.type L, lua_State, LREG
307 |.type GL, global_State
308 |.type TVALUE, TValue
312 |.type LFUNC, GCfuncL
313 |.type CFUNC, GCfuncC
314 |.type PROTO, GCproto
315 |.type UPVAL, GCupval
318 |.type TRACE, GCtrace
321 |//-----------------------------------------------------------------------
323 |// Trap for not-yet-implemented parts.
324 |.macro NYI; tw 4, sp, sp; .endmacro
326 |// int/FP conversions.
327 |.macro tonum_i, freg, reg
328 | xoris reg, reg, 0x8000
331 | fsub freg, freg, TONUM
334 |.macro tonum_u, freg, reg
337 | fsub freg, freg, TOBIT
340 |.macro toint, reg, freg, tmpfreg
341 | fctiwz tmpfreg, freg
346 |.macro toint, reg, freg
347 | toint reg, freg, freg
350 |//-----------------------------------------------------------------------
352 |// Access to frame relative to BASE.
353 |.define FRAME_PC, -8
354 |.define FRAME_FUNC, -4
356 |// Instruction decode.
357 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
358 |.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro
359 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
360 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
361 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
362 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
364 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
365 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
367 |// Instruction fetch.
372 |// Instruction decode+dispatch. Note: optimized for e300!
374 | decode_OPP TMP1, INS
375 | lpx TMP0, DISPATCH, TMP1
388 |// Instruction footer.
390 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
391 | .define ins_next, ins_NEXT
392 | .define ins_next_, ins_NEXT
393 | .define ins_next1, ins_NEXT1
394 | .define ins_next2, ins_NEXT2
396 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
397 | // Affects only certain kinds of benchmarks (and only with -j off).
412 |// Call decode and dispatch.
414 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
415 | lwz PC, LFUNC:RB->pc
418 | decode_OPP TMP1, INS
420 | lpx TMP0, DISPATCH, TMP1
427 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
428 | stw PC, FRAME_PC(BASE)
432 |//-----------------------------------------------------------------------
434 |// Macros to test operand types.
435 |.macro checknum, reg; cmplw reg, TISNUM; .endmacro
436 |.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro
437 |.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro
438 |.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro
439 |.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro
440 |.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro
444 | addis PC, PC, -(BCBIAS_J*4 >> 16)
448 |// Assumes DISPATCH is relative to GL.
449 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
450 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
452 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
454 |.macro hotcheck, delta, target
455 | rlwinm TMP1, PC, 31, 25, 30
456 | addi TMP1, TMP1, GG_DISP2HOT
457 | lhzx TMP2, DISPATCH, TMP1
458 | addic. TMP2, TMP2, -delta
459 | sthx TMP2, DISPATCH, TMP1
464 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
468 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
471 |// Set current VM state. Uses TMP0.
472 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
473 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
475 |// Move table write barrier back. Overwrites mark and tmp.
476 |.macro barrierback, tab, mark, tmp
477 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
478 | // Assumes LJ_GC_BLACK is 0x04.
479 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
480 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
481 | stb mark, tab->marked
482 | stw tmp, tab->gclist
485 |//-----------------------------------------------------------------------
487 /* Generate subroutines used by opcodes and other parts of the VM. */
488 /* The .code_sub section should be last to help static branch prediction. */
489 static void build_subroutines(BuildCtx *ctx)
493 |//-----------------------------------------------------------------------
494 |//-- Return handling ----------------------------------------------------
495 |//-----------------------------------------------------------------------
498 | // See vm_return. Also: TMP2 = previous base.
499 | andix. TMP0, PC, FRAME_P
501 | beq ->cont_dispatch
503 | // Return from pcall or xpcall fast func.
504 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
505 | mr BASE, TMP2 // Restore caller base.
506 | // Prepending may overwrite the pcall frame, so do it at the end.
507 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
510 | addi RD, RD, 8 // RD = (nresults+1)*8.
511 | andix. TMP0, PC, FRAME_TYPE
513 | li CRET1, LUA_YIELD
514 | beq cr1, ->vm_unwind_c_eh
516 | beq ->BC_RET_Z // Handle regular return to Lua.
519 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
520 | // TMP0 = PC & FRAME_TYPE
521 | cmpwi TMP0, FRAME_C
522 | rlwinm TMP2, PC, 0, 0, 28
524 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
527 | addic. TMP1, RD, -8
529 | lwz TMP2, SAVE_NRES
535 | addic. TMP1, TMP1, -8
543 | cmpw TMP2, RD // More/less results wanted?
546 | stp BASE, L->top // Store new top.
549 | lp TMP0, SAVE_CFRAME // Restore previous C frame.
550 | li CRET1, 0 // Ok return status for vm_pcall.
551 | stp TMP0, L->cframe
558 | ble >7 // Less results wanted?
559 | // More results wanted. Check stack size and fill up results with nil.
560 | lwz TMP1, L->maxstack
563 | stw TISNIL, 0(BASE)
568 |7: // Less results wanted.
569 | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case?
571 | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1
572 | and TMP0, TMP0, TMP1
573 | sub BASE, BASE, TMP0 // Either keep top or shrink it.
576 |8: // Corner case: need to grow stack for filling up results.
577 | // This can happen if:
578 | // - A C function grows the stack (a lot).
579 | // - The GC shrinks the stack in between.
580 | // - A return back from a lua_call() with (high) nresults adjustment.
581 | stp BASE, L->top // Save current top held in BASE (yes).
583 | srwi CARG2, TMP2, 3
585 | bl extern lj_state_growstack // (lua_State *L, int n)
586 | lwz TMP2, SAVE_NRES
589 | lp BASE, L->top // Need the (realloced) L->top in BASE.
592 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
593 | // (void *cframe, int errcode)
596 |->vm_unwind_c_eh: // Landing pad for external unwinder.
598 | .toc ld TOCREG, SAVE_TOC
599 | li TMP0, ~LJ_VMST_C
600 | lwz GL:TMP1, L->glref
601 | stw TMP0, GL:TMP1->vmstate
604 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
607 | rldicr sp, CARG1, 0, 61
609 | rlwinm sp, CARG1, 0, 0, 29
611 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
613 | .toc ld TOCREG, SAVE_TOC
614 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
616 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
617 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
621 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
625 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
626 | la RA, -8(BASE) // Results start at BASE-8.
628 | addi DISPATCH, DISPATCH, GG_G2DISP
629 | stw TMP1, 0(RA) // Prepend false to error message.
630 | li RD, 16 // 2 results: false + error message.
635 |//-----------------------------------------------------------------------
636 |//-- Grow stack for calls -----------------------------------------------
637 |//-----------------------------------------------------------------------
639 |->vm_growstack_c: // Grow stack for C function.
640 | li CARG2, LUA_MINSTACK
643 |->vm_growstack_l: // Grow stack for Lua function.
644 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
648 | addi PC, PC, 4 // Must point after first instruction.
652 | // L->base = new base, L->top = top
655 | bl extern lj_state_growstack // (lua_State *L, int n)
658 | lwz LFUNC:RB, FRAME_FUNC(BASE)
660 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
661 | ins_callt // Just retry the call.
663 |//-----------------------------------------------------------------------
664 |//-- Entry points into the assembler VM ---------------------------------
665 |//-----------------------------------------------------------------------
667 |->vm_resume: // Setup C frame and resume thread.
668 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
671 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
673 | lbz TMP1, L->status
676 | addi TMP0, sp, CFRAME_RESUME
677 | addi DISPATCH, DISPATCH, GG_G2DISP
678 | stw CARG3, SAVE_NRES
680 | stw CARG3, SAVE_ERRF
681 | stp CARG3, SAVE_CFRAME
682 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
683 | stp TMP0, L->cframe
686 | // Resume after yield (like a return).
687 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
690 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
692 | lwz PC, FRAME_PC(BASE)
693 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
694 | stb CARG3, L->status
696 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
700 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
706 | andix. TMP0, PC, FRAME_TYPE
713 |->vm_pcall: // Setup protected C frame and enter VM.
714 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
717 | stw CARG4, SAVE_ERRF
720 |->vm_call: // Setup C frame and enter VM.
721 | // (lua_State *L, TValue *base, int nres1)
725 |1: // Entry point for vm_pcall above (PC = ftype).
726 | lp TMP1, L:CARG1->cframe
728 | stw CARG3, SAVE_NRES
729 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
732 | addi DISPATCH, DISPATCH, GG_G2DISP
733 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
734 | stp TMP1, SAVE_CFRAME
735 | stp sp, L->cframe // Add our C frame to cframe chain.
737 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
738 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
739 | lp TMP2, L->base // TMP2 = old base (used in vmeta_call).
740 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
742 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
746 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
748 | sub PC, PC, TMP2 // PC = frame delta + frame type
750 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
751 | sub NARGS8:RC, TMP1, BASE
759 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
760 | lwz TMP0, FRAME_PC(BASE)
761 | lwz LFUNC:RB, FRAME_FUNC(BASE)
762 | checkfunc TMP0; bne ->vmeta_call
764 |->vm_call_dispatch_f:
766 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
768 |->vm_cpcall: // Setup protected C frame, call C.
769 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
772 | lwz TMP0, L:CARG1->stack
775 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
776 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
777 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
779 | addi DISPATCH, DISPATCH, GG_G2DISP
780 | .toc lp CARG4, 0(CARG4)
782 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
783 | stw TMP2, SAVE_ERRF // No error function.
784 | stp TMP1, SAVE_CFRAME
785 | stp sp, L->cframe // Add our C frame to cframe chain.
786 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
788 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
796 | bne <3 // Else continue with the call.
797 | b ->vm_leave_cp // No base? Just remove C frame.
799 |//-----------------------------------------------------------------------
800 |//-- Metamethod handling ------------------------------------------------
801 |//-----------------------------------------------------------------------
803 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
804 |// stack, so BASE doesn't need to be reloaded across these calls.
806 |//-- Continuation dispatch ----------------------------------------------
809 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
810 | lwz TMP0, -12(BASE) // Continuation.
812 | mr BASE, TMP2 // Restore caller BASE.
813 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
817 | lwz PC, -16(RB) // Restore PC from [cont|PC].
819 | lwz TMP1, LFUNC:TMP1->pc
820 | stwx TISNIL, RA, TMP2 // Ensure one valid arg.
824 | lwz KBASE, PC2PROTO(k)(TMP1)
825 | // BASE = base, RA = resultptr, RB = meta base
827 | bctr // Jump to continuation.
831 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
832 | // cont = 0: tailcall from C function.
838 |->cont_cat: // RA = resultptr, RB = meta base
841 | decode_RB8 SAVE0, INS
843 | add TMP1, BASE, SAVE0
846 | sub CARG3, CARG2, TMP1
853 |//-- Table indexing metamethods -----------------------------------------
856 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
859 | stw STR:RC, 4(CARG3)
860 | add CARG2, BASE, RB
865 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
867 | stw TAB:RB, 4(CARG2)
868 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
871 | stw STR:RC, 4(CARG3)
875 |->vmeta_tgetb: // TMP0 = index
880 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
881 | add CARG2, BASE, RB
883 | stw TISNUM, 0(CARG3)
893 | add CARG2, BASE, RB
894 | add CARG3, BASE, RC
899 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
900 | // Returns TValue * (finished) or NULL (metamethod).
908 |3: // Call __index metamethod.
909 | // BASE = base, L->top = new base, stack = cont/func/t/k
910 | subfic TMP1, BASE, FRAME_CONT
912 | stw PC, -16(BASE) // [cont|PC]
914 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
915 | li NARGS8:RC, 16 // 2 args for func(t, k).
916 | b ->vm_call_dispatch_f
919 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
920 | // Returns cTValue * or NULL.
926 | stwx TISNIL, BASE, RA
929 |//-----------------------------------------------------------------------
932 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
935 | stw STR:RC, 4(CARG3)
936 | add CARG2, BASE, RB
941 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
943 | stw TAB:RB, 4(CARG2)
944 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
947 | stw STR:RC, 4(CARG3)
951 |->vmeta_tsetb: // TMP0 = index
956 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
957 | add CARG2, BASE, RB
959 | stw TISNUM, 0(CARG3)
969 | add CARG2, BASE, RB
970 | add CARG3, BASE, RC
975 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
976 | // Returns TValue * (finished) or NULL (metamethod).
980 | // NOBARRIER: lj_meta_tset ensures the table is not black.
985 |3: // Call __newindex metamethod.
986 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
987 | subfic TMP1, BASE, FRAME_CONT
989 | stw PC, -16(BASE) // [cont|PC]
991 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
992 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
993 | stfd f0, 16(BASE) // Copy value to third argument.
994 | b ->vm_call_dispatch_f
999 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
1000 | // Returns TValue *.
1001 | stfd f14, 0(CRET1)
1004 |//-- Comparison metamethods ---------------------------------------------
1012 | add CARG2, BASE, RA
1018 | add CARG3, BASE, RD
1021 | decode_OP1 CARG4, INS
1022 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1023 | // Returns 0/1 or TValue * (metamethod).
1027 | subfic CRET1, CRET1, 0
1031 | decode_RD4 TMP2, INS
1032 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
1033 | and TMP2, TMP2, CRET1
1038 |->cont_ra: // RA = resultptr
1041 | decode_RA8 TMP1, INS
1042 | stfdx f0, BASE, TMP1
1045 |->cont_condt: // RA = resultptr
1047 | .gpr64 extsw TMP0, TMP0
1048 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true.
1049 | subfe CRET1, CRET1, CRET1
1053 |->cont_condf: // RA = resultptr
1055 | .gpr64 extsw TMP0, TMP0
1056 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false.
1057 | subfe CRET1, CRET1, CRET1
1061 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
1066 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1067 | // Returns 0/1 or TValue * (metamethod).
1077 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
1078 | // Returns 0/1 or TValue * (metamethod).
1089 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
1092 |//-- Arithmetic metamethods ---------------------------------------------
1095 | add CARG3, KBASE, RC
1096 | add CARG4, BASE, RB
1111 | add CARG3, BASE, RB
1112 | add CARG4, KBASE, RC
1116 | add CARG3, BASE, RB
1117 | add CARG4, BASE, RC
1128 | add CARG2, BASE, RA
1132 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
1133 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1134 | // Returns NULL (finished) or TValue * (metamethod).
1138 | // Call metamethod for binary op.
1140 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1141 | sub TMP1, CRET1, BASE
1142 | stw PC, -16(CRET1) // [cont|PC]
1144 | addi PC, TMP1, FRAME_CONT
1146 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
1147 | b ->vm_call_dispatch
1157 | bl extern lj_meta_len // (lua_State *L, TValue *o)
1158 | // Returns NULL (retry) or TValue * (metamethod base).
1161 | bne ->vmeta_binop // Binop call for compatibility.
1165 | b ->vmeta_binop // Binop call for compatibility.
1168 |//-- Call metamethod ----------------------------------------------------
1170 |->vmeta_call: // Resolve and call __call metamethod.
1171 | // TMP2 = old base, BASE = new base, RC = nargs*8
1173 | stp TMP2, L->base // This is the callers base!
1174 | subi CARG2, BASE, 8
1176 | add CARG3, BASE, RC
1177 | mr SAVE0, NARGS8:RC
1178 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1179 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1180 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1183 |->vmeta_callt: // Resolve __call for BC_CALLT.
1184 | // BASE = old base, RA = new base, RC = nargs*8
1190 | mr SAVE0, NARGS8:RC
1191 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1192 | lwz TMP1, FRAME_PC(BASE)
1193 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1194 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1197 |//-- Argument coercion for 'for' statement ------------------------------
1205 | bl extern lj_meta_for // (lua_State *L, TValue *base)
1207 | decode_OP1 TMP0, SAVE0
1209 | decode_RA8 RA, SAVE0
1211 | cmpwi TMP0, BC_JFORI
1213 | decode_RD8 RD, SAVE0
1219 |//-----------------------------------------------------------------------
1220 |//-- Fast functions -----------------------------------------------------
1221 |//-----------------------------------------------------------------------
1223 |.macro .ffunc, name
1227 |.macro .ffunc_1, name
1229 | cmplwi NARGS8:RC, 8
1230 | lwz CARG3, 0(BASE)
1231 | lwz CARG1, 4(BASE)
1232 | blt ->fff_fallback
1235 |.macro .ffunc_2, name
1237 | cmplwi NARGS8:RC, 16
1238 | lwz CARG3, 0(BASE)
1239 | lwz CARG4, 8(BASE)
1240 | lwz CARG1, 4(BASE)
1241 | lwz CARG2, 12(BASE)
1242 | blt ->fff_fallback
1245 |.macro .ffunc_n, name
1247 | cmplwi NARGS8:RC, 8
1248 | lwz CARG3, 0(BASE)
1249 | lfd FARG1, 0(BASE)
1250 | blt ->fff_fallback
1251 | checknum CARG3; bge ->fff_fallback
1254 |.macro .ffunc_nn, name
1256 | cmplwi NARGS8:RC, 16
1257 | lwz CARG3, 0(BASE)
1258 | lfd FARG1, 0(BASE)
1259 | lwz CARG4, 8(BASE)
1260 | lfd FARG2, 8(BASE)
1261 | blt ->fff_fallback
1262 | checknum CARG3; bge ->fff_fallback
1263 | checknum CARG4; bge ->fff_fallback
1266 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
1268 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1269 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1274 |//-- Base library: checks -----------------------------------------------
1277 | li TMP1, LJ_TFALSE
1279 | cmplw cr1, CARG3, TMP1
1280 | lwz PC, FRAME_PC(BASE)
1281 | bge cr1, ->fff_fallback
1283 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1285 | beq ->fff_res // Done if exactly 1 argument.
1290 | lfdx f0, BASE, TMP1
1291 | stfdx f0, RA, TMP1
1292 | addi TMP1, TMP1, 8
1297 | cmplwi NARGS8:RC, 8
1298 | lwz CARG1, 0(BASE)
1299 | blt ->fff_fallback
1300 | .gpr64 extsw CARG1, CARG1
1301 | subfc TMP0, TISNUM, CARG1
1302 | subfe TMP2, CARG1, CARG1
1303 | orc TMP1, TMP2, TMP0
1304 | addi TMP1, TMP1, ~LJ_TISNUM+1
1305 | slwi TMP1, TMP1, 3
1306 | la TMP2, CFUNC:RB->upvalue
1307 | lfdx FARG1, TMP2, TMP1
1310 |//-- Base library: getters and setters ---------------------------------
1312 |.ffunc_1 getmetatable
1313 | checktab CARG3; bne >6
1314 |1: // Field metatable must be at same offset for GCtab and GCudata!
1315 | lwz TAB:CARG1, TAB:CARG1->metatable
1318 | cmplwi TAB:CARG1, 0
1319 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1321 | lwz TMP0, TAB:CARG1->hmask
1322 | li CARG3, LJ_TTAB // Use metatable as default result.
1323 | lwz TMP1, STR:RC->hash
1324 | lwz NODE:TMP2, TAB:CARG1->node
1325 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
1326 | slwi TMP0, TMP1, 5
1327 | slwi TMP1, TMP1, 3
1328 | sub TMP1, TMP0, TMP1
1329 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1330 |3: // Rearranged logic, because we expect _not_ to find the key.
1331 | lwz CARG4, NODE:TMP2->key
1332 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
1333 | lwz CARG2, NODE:TMP2->val
1334 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
1335 | checkstr CARG4; bne >4
1336 | cmpw TMP0, STR:RC; beq >5
1338 | lwz NODE:TMP2, NODE:TMP2->next
1339 | cmplwi NODE:TMP2, 0
1340 | beq ->fff_restv // Not found, keep default result.
1344 | beq ->fff_restv // Ditto for nil value.
1345 | mr CARG3, CARG2 // Return value of mt.__metatable.
1350 | cmpwi CARG3, LJ_TUDATA; beq <1
1351 | .gpr64 extsw CARG3, CARG3
1352 | subfc TMP0, TISNUM, CARG3
1353 | subfe TMP2, CARG3, CARG3
1354 | orc TMP1, TMP2, TMP0
1355 | addi TMP1, TMP1, ~LJ_TISNUM+1
1356 | slwi TMP1, TMP1, 2
1357 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1358 | lwzx TAB:CARG1, TMP2, TMP1
1361 |.ffunc_2 setmetatable
1362 | // Fast path: no mt for table yet and not clearing the mt.
1363 | checktab CARG3; bne ->fff_fallback
1364 | lwz TAB:TMP1, TAB:CARG1->metatable
1365 | checktab CARG4; bne ->fff_fallback
1366 | cmplwi TAB:TMP1, 0
1367 | lbz TMP3, TAB:CARG1->marked
1368 | bne ->fff_fallback
1369 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1370 | stw TAB:CARG2, TAB:CARG1->metatable
1372 | barrierback TAB:CARG1, TMP3, TMP0
1376 | cmplwi NARGS8:RC, 16
1377 | lwz CARG4, 0(BASE)
1378 | lwz TAB:CARG2, 4(BASE)
1379 | blt ->fff_fallback
1380 | checktab CARG4; bne ->fff_fallback
1383 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1384 | // Returns cTValue *.
1385 | lfd FARG1, 0(CRET1)
1388 |//-- Base library: conversions ------------------------------------------
1391 | // Only handles the number case inline (without a base argument).
1392 | cmplwi NARGS8:RC, 8
1393 | lwz CARG1, 0(BASE)
1394 | lfd FARG1, 0(BASE)
1395 | bne ->fff_fallback // Exactly one argument.
1396 | checknum CARG1; bgt ->fff_fallback
1400 | // Only handles the string or number case inline.
1402 | // A __tostring method in the string base metatable is ignored.
1403 | beq ->fff_restv // String key?
1404 | // Handle numbers inline, unless a number base metatable is present.
1405 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1407 | cmplwi cr1, TMP0, 0
1408 | stp BASE, L->base // Add frame since C call can throw.
1409 | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq
1410 | stw PC, SAVE_PC // Redundant (but a defined value).
1411 | beq ->fff_fallback
1416 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1418 | bl extern lj_strfmt_num // (lua_State *L, lua_Number *np)
1420 | // Returns GCstr *.
1424 |//-- Base library: iterators -------------------------------------------
1427 | cmplwi NARGS8:RC, 8
1428 | lwz CARG1, 0(BASE)
1429 | lwz TAB:CARG2, 4(BASE)
1430 | blt ->fff_fallback
1431 | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1433 | lwz PC, FRAME_PC(BASE)
1434 | bne ->fff_fallback
1435 | stp BASE, L->base // Add frame since C call can throw.
1437 | stp BASE, L->top // Dummy frame length is ok.
1440 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1441 | // Returns 0 at end of traversal.
1444 | beq ->fff_restv // End of traversal: return nil.
1445 | lfd f0, 8(BASE) // Copy key and value to results.
1455 | lwz PC, FRAME_PC(BASE)
1456 | bne ->fff_fallback
1458 | lwz TAB:TMP2, TAB:CARG1->metatable
1459 | lfd f0, CFUNC:RB->upvalue[0]
1460 | cmplwi TAB:TMP2, 0
1462 | bne ->fff_fallback
1464 | lfd f0, CFUNC:RB->upvalue[0]
1467 | stw TISNIL, 8(BASE)
1473 | cmplwi NARGS8:RC, 16
1474 | lwz CARG3, 0(BASE)
1475 | lwz TAB:CARG1, 4(BASE)
1476 | lwz CARG4, 8(BASE)
1478 | lwz TMP2, 12(BASE)
1480 | lfd FARG2, 8(BASE)
1482 | blt ->fff_fallback
1484 | checknum cr1, CARG4
1485 | lwz PC, FRAME_PC(BASE)
1487 | bne ->fff_fallback
1488 | bne cr1, ->fff_fallback
1492 | bne ->fff_fallback
1494 | bge cr1, ->fff_fallback
1496 | toint TMP2, FARG2, f0
1498 | lwz TMP0, TAB:CARG1->asize
1499 | lwz TMP1, TAB:CARG1->array
1501 | fadd FARG2, FARG2, FARG1
1503 | addi TMP2, TMP2, 1
1508 | slwi TMP3, TMP2, 3
1511 | slwi TMP3, TMP2, 3
1514 | ble >2 // Not in array part?
1515 | lwzx TMP2, TMP1, TMP3
1516 | lfdx f0, TMP1, TMP3
1520 | beq ->fff_res // End of iteration, return 0 results.
1524 |2: // Check for empty hash part first. Otherwise call C function.
1525 | lwz TMP0, TAB:CARG1->hmask
1530 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1531 | // Returns cTValue * or NULL.
1535 | lwz TMP2, 0(CRET1)
1541 | lwz PC, FRAME_PC(BASE)
1542 | bne ->fff_fallback
1544 | lwz TAB:TMP2, TAB:CARG1->metatable
1545 | lfd f0, CFUNC:RB->upvalue[0]
1546 | cmplwi TAB:TMP2, 0
1548 | bne ->fff_fallback
1550 | lfd f0, CFUNC:RB->upvalue[0]
1554 | stw TISNUM, 8(BASE)
1558 | stw ZERO, 12(BASE)
1563 |//-- Base library: catch errors ----------------------------------------
1566 | cmplwi NARGS8:RC, 8
1567 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1568 | blt ->fff_fallback
1571 | // Remember active hook before pcall.
1572 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1573 | subi NARGS8:RC, NARGS8:RC, 8
1574 | addi PC, TMP3, 8+FRAME_PCALL
1575 | b ->vm_call_dispatch
1578 | cmplwi NARGS8:RC, 16
1579 | lwz CARG4, 8(BASE)
1580 | lfd FARG2, 8(BASE)
1581 | lfd FARG1, 0(BASE)
1582 | blt ->fff_fallback
1583 | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1585 | checkfunc CARG4; bne ->fff_fallback // Traceback must be a function.
1587 | // Remember active hook before pcall.
1588 | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31
1589 | stfd FARG2, 0(TMP2) // Swap function and traceback.
1590 | subi NARGS8:RC, NARGS8:RC, 16
1591 | stfd FARG1, 8(TMP2)
1592 | addi PC, TMP1, 16+FRAME_PCALL
1593 | b ->vm_call_dispatch
1595 |//-- Coroutine library --------------------------------------------------
1597 |.macro coroutine_resume_wrap, resume
1599 |.ffunc_1 coroutine_resume
1600 | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback
1602 |.ffunc coroutine_wrap_aux
1603 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1605 | lbz TMP0, L:CARG1->status
1606 | lp TMP1, L:CARG1->cframe
1607 | lp CARG2, L:CARG1->top
1608 | cmplwi cr0, TMP0, LUA_YIELD
1609 | lp TMP2, L:CARG1->base
1610 | cmplwi cr1, TMP1, 0
1611 | lwz TMP0, L:CARG1->maxstack
1612 | cmplw cr7, CARG2, TMP2
1613 | lwz PC, FRAME_PC(BASE)
1614 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1615 | add TMP2, CARG2, NARGS8:RC
1616 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1617 | cmplw cr1, TMP2, TMP0
1618 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1620 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1622 | blt cr6, ->fff_fallback
1625 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1626 | subi NARGS8:RC, NARGS8:RC, 8
1627 | subi TMP2, TMP2, 8
1629 | stp TMP2, L:CARG1->top
1632 |2: // Move args to coroutine.
1633 | cmpw TMP1, NARGS8:RC
1634 | lfdx f0, BASE, TMP1
1636 | stfdx f0, CARG2, TMP1
1637 | addi TMP1, TMP1, 8
1641 | mr L:SAVE0, L:CARG1
1643 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1644 | // Returns thread status.
1646 | lp TMP2, L:SAVE0->base
1647 | cmplwi CRET1, LUA_YIELD
1648 | lp TMP3, L:SAVE0->top
1651 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
1654 | sub RD, TMP3, TMP2
1655 | lwz TMP0, L->maxstack
1657 | add TMP1, BASE, RD
1658 | beq >6 // No results?
1661 | bgt >9 // Need to grow stack?
1664 | stp TMP2, L:SAVE0->top // Clear coroutine stack.
1665 |5: // Move results from coroutine.
1667 | lfdx f0, TMP2, TMP1
1668 | stfdx f0, BASE, TMP1
1669 | addi TMP1, TMP1, 8
1672 | andix. TMP0, PC, FRAME_TYPE
1676 | stw TMP1, -8(BASE) // Prepend true to results.
1688 |8: // Coroutine returned with error (at co->top-1).
1690 | andix. TMP0, PC, FRAME_TYPE
1692 | li TMP1, LJ_TFALSE
1694 | stp TMP3, L:SAVE0->top // Remove error from coroutine stack.
1696 | stw TMP1, -8(BASE) // Prepend false to results.
1698 | stfd f0, 0(BASE) // Copy error message.
1703 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1706 |9: // Handle stack expansion on return from yield.
1709 | bl extern lj_state_growstack // (lua_State *L, int n)
1714 | coroutine_resume_wrap 1 // coroutine.resume
1715 | coroutine_resume_wrap 0 // coroutine.wrap
1717 |.ffunc coroutine_yield
1718 | lp TMP0, L->cframe
1719 | add TMP1, BASE, NARGS8:RC
1721 | andix. TMP0, TMP0, CFRAME_RESUME
1723 | li CRET1, LUA_YIELD
1724 | beq ->fff_fallback
1725 | stp ZERO, L->cframe
1726 | stb CRET1, L->status
1729 |//-- Math library -------------------------------------------------------
1735 | srawi TMP1, CARG1, 31
1736 | xor TMP2, TMP1, CARG1
1739 | sub CARG1, TMP2, TMP1
1743 | sub. CARG1, TMP2, TMP1
1747 | lwz PC, FRAME_PC(BASE)
1749 | stw TISNUM, -8(BASE)
1750 | stw CRET1, -4(BASE)
1753 | lus CARG3, 0x41e0 // 2^31.
1758 | bge ->fff_fallback
1759 | rlwinm CARG3, CARG3, 0, 1, 31
1763 | // CARG3/CARG1 = TValue result.
1764 | lwz PC, FRAME_PC(BASE)
1765 | stw CARG3, -8(BASE)
1767 | stw CARG1, -4(BASE)
1769 | // RA = results, PC = return.
1772 | // RA = results, RD = (nresults+1)*8, PC = return.
1773 | andix. TMP0, PC, FRAME_TYPE
1777 | decode_RB8 RB, INS
1779 | cmplw RB, RD // More results expected?
1780 | decode_RA8 TMP0, INS
1783 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1784 | sub BASE, RA, TMP0
1787 |6: // Fill up results with nil.
1790 | stwx TISNIL, RA, TMP1
1793 |.macro math_extern, func
1794 | .ffunc_n math_ .. func
1799 |.macro math_extern2, func
1800 | .ffunc_nn math_ .. func
1805 |.macro math_round, func
1806 | .ffunc_1 math_ .. func
1807 | checknum CARG3; beqy ->fff_restv
1808 | rlwinm TMP2, CARG3, 12, 21, 31
1809 | bge ->fff_fallback
1810 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
1811 | cmplwi cr1, TMP2, 31 // 0 <= exp < 31?
1812 | subfic TMP0, TMP2, 31
1814 | slwi TMP1, CARG3, 11
1815 | srwi TMP3, CARG1, 21
1816 | oris TMP1, TMP1, 0x8000
1817 | addi TMP2, TMP2, 1
1818 | or TMP1, TMP1, TMP3
1819 | slwi CARG2, CARG1, 11
1821 | slw TMP3, TMP1, TMP2
1822 | srw RD, TMP1, TMP0
1823 | or TMP3, TMP3, CARG2
1824 | srawi TMP2, CARG3, 31
1825 |.if "func" == "floor"
1826 | and TMP1, TMP3, TMP2
1827 | addic TMP0, TMP1, -1
1828 | subfe TMP1, TMP0, TMP1
1829 | add CARG1, RD, TMP1
1830 | xor CARG1, CARG1, TMP2
1831 | sub CARG1, CARG1, TMP2
1834 | andc TMP1, TMP3, TMP2
1835 | addic TMP0, TMP1, -1
1836 | subfe TMP1, TMP0, TMP1
1837 | add CARG1, RD, TMP1
1839 | xor CARG1, CARG1, TMP2
1840 | sub CARG1, CARG1, TMP2
1842 | // Overflow to 2^31.
1843 | lus CARG3, 0x41e0 // 2^31.
1848 | slwi TMP2, CARG3, 1
1849 | srawi TMP1, CARG3, 31
1850 | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo
1851 |.if "func" == "floor"
1852 | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1
1853 | subfic TMP2, TMP1, 0
1854 | subfe CARG1, CARG1, CARG1
1856 | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1
1857 | addic TMP2, TMP1, -1
1858 | subfe CARG1, TMP2, TMP1
1861 |4: // exp >= 31. Check for -(2^31).
1862 | xoris TMP1, TMP1, 0x8000
1863 | srawi TMP2, CARG3, 31
1864 |.if "func" == "floor"
1865 | or TMP1, TMP1, CARG2
1868 | orc TMP1, TMP1, TMP2
1871 | orc. TMP1, TMP1, TMP2
1873 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
1874 | lus CARG1, 0x8000 // -(2^31).
1877 | lfd FARG1, 0(BASE)
1886 | // NYI: use internal implementation.
1893 | fsqrt FARG1, FARG1
1900 | cmplwi NARGS8:RC, 8
1901 | lwz CARG3, 0(BASE)
1902 | lfd FARG1, 0(BASE)
1903 | bne ->fff_fallback // Need exactly 1 argument.
1904 | checknum CARG3; bge ->fff_fallback
1920 | math_extern2 atan2
1925 | cmplwi NARGS8:RC, 16
1926 | lwz CARG3, 0(BASE)
1927 | lfd FARG1, 0(BASE)
1928 | lwz CARG4, 8(BASE)
1930 | lwz CARG2, 12(BASE)
1932 | lwz CARG1, 12(BASE)
1934 | blt ->fff_fallback
1935 | checknum CARG3; bge ->fff_fallback
1936 | checknum CARG4; bne ->fff_fallback
1938 |.ffunc_nn math_ldexp
1940 | toint CARG2, FARG2
1942 | toint CARG1, FARG2
1948 |.ffunc_n math_frexp
1950 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1952 | la CARG1, DISPATCH_GL(tmptv)(DISPATCH)
1954 | lwz PC, FRAME_PC(BASE)
1956 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1959 | tonum_i FARG2, TMP1
1973 | la CARG2, -8(BASE)
1975 | la CARG1, -8(BASE)
1977 | lwz PC, FRAME_PC(BASE)
1980 | stfd FARG1, 0(BASE)
1984 |.macro math_minmax, name, ismax
1988 | addi TMP1, BASE, 8
1989 | add TMP2, BASE, NARGS8:RC
1991 |1: // Handle integers.
1992 | lwz CARG4, 0(TMP1)
1993 | cmplw cr1, TMP1, TMP2
1994 | lwz CARG2, 4(TMP1)
1995 | bge cr1, ->fff_resi
1997 | xoris TMP0, CARG1, 0x8000
1998 | xoris TMP3, CARG2, 0x8000
2000 | subfc TMP3, TMP3, TMP0
2001 | subfe TMP0, TMP0, TMP0
2003 | andc TMP3, TMP3, TMP0
2005 | and TMP3, TMP3, TMP0
2007 | add CARG1, TMP3, CARG2
2009 | rldicl CARG1, CARG1, 0, 32
2011 | addi TMP1, TMP1, 8
2014 | bge ->fff_fallback
2015 | // Convert intermediate result to number and continue below.
2016 | tonum_i FARG1, CARG1
2017 | lfd FARG2, 0(TMP1)
2020 | lfd FARG1, 0(BASE)
2021 | bge ->fff_fallback
2022 |5: // Handle numbers.
2023 | lwz CARG4, 0(TMP1)
2024 | cmplw cr1, TMP1, TMP2
2025 | lfd FARG2, 0(TMP1)
2026 | bge cr1, ->fff_resn
2027 | checknum CARG4; bge >7
2029 | fsub f0, FARG1, FARG2
2030 | addi TMP1, TMP1, 8
2032 | fsel FARG1, f0, FARG1, FARG2
2034 | fsel FARG1, f0, FARG2, FARG1
2037 |7: // Convert integer to number and continue above.
2038 | lwz CARG2, 4(TMP1)
2039 | bne ->fff_fallback
2040 | tonum_i FARG2, CARG2
2046 | lwzx CARG2, BASE, TMP1
2047 | lfdx FARG2, BASE, TMP1
2048 | cmplw cr1, TMP1, NARGS8:RC
2050 | bge cr1, ->fff_resn
2051 | bge ->fff_fallback
2052 | fsub f0, FARG1, FARG2
2053 | addi TMP1, TMP1, 8
2055 | fsel FARG1, f0, FARG1, FARG2
2057 | fsel FARG1, f0, FARG2, FARG1
2063 | math_minmax math_min, 0
2064 | math_minmax math_max, 1
2066 |//-- String library -----------------------------------------------------
2068 |.ffunc string_byte // Only handle the 1-arg case here.
2069 | cmplwi NARGS8:RC, 8
2070 | lwz CARG3, 0(BASE)
2071 | lwz STR:CARG1, 4(BASE)
2072 | bne ->fff_fallback // Need exactly 1 argument.
2074 | bne ->fff_fallback
2075 | lwz TMP0, STR:CARG1->len
2077 | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
2079 | lwz PC, FRAME_PC(BASE)
2085 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
2086 | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8
2087 | subfe RD, TMP3, TMP0
2088 | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1.
2092 | lwz PC, FRAME_PC(BASE)
2093 | fsub f0, f0, TOBIT
2099 |.ffunc string_char // Only handle the 1-arg case here.
2101 | cmplwi NARGS8:RC, 8
2102 | lwz CARG3, 0(BASE)
2105 | bne ->fff_fallback // Exactly 1 argument.
2106 | checknum CARG3; bne ->fff_fallback
2109 | lfd FARG1, 0(BASE)
2110 | bne ->fff_fallback // Exactly 1 argument.
2111 | checknum CARG3; bge ->fff_fallback
2113 | la CARG2, TMPD_BLO
2116 | cmplwi TMP0, 255; bgt ->fff_fallback
2121 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
2123 | // Returns GCstr *.
2130 | cmplwi NARGS8:RC, 16
2131 | lwz CARG3, 16(BASE)
2136 | lwz STR:CARG1, 4(BASE)
2137 | blt ->fff_fallback
2138 | lwz CARG2, 8(BASE)
2140 | lwz TMP1, 12(BASE)
2148 | lwz TMP2, 20(BASE)
2149 | bne ->fff_fallback
2151 | checknum CARG2; bne ->fff_fallback
2153 | checknum CARG3; bge ->fff_fallback
2156 | checknum CARG2; bge ->fff_fallback
2158 | checkstr TMP0; bne ->fff_fallback
2162 | lwz TMP0, STR:CARG1->len
2163 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
2164 | addi TMP3, TMP2, 1
2167 | cmpwi TMP1, 0 // start <= 0?
2168 | add TMP3, TMP1, TMP0
2171 | sub CARG3, TMP2, TMP1
2172 | addi CARG2, STR:CARG1, #STR-1
2173 | srawi TMP0, CARG3, 31
2174 | addi CARG3, CARG3, 1
2175 | add CARG2, CARG2, TMP1
2176 | andc CARG3, CARG3, TMP0
2178 | rldicl CARG2, CARG2, 0, 32
2179 | rldicl CARG3, CARG3, 0, 32
2183 |5: // Negative end or overflow.
2184 | cmpw TMP0, TMP2 // len >= end? (signed compare)
2185 | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1.
2187 | mr TMP2, TMP0 // Overflow: end = len.
2190 |7: // Negative start or underflow.
2191 | .gpr64 extsw TMP1, TMP1
2192 | addic CARG3, TMP1, -1
2193 | subfe CARG3, CARG3, CARG3
2194 | srawi CARG2, TMP3, 31 // Note: modifies carry.
2195 | andc TMP3, TMP3, CARG3
2196 | andc TMP1, TMP3, CARG2
2197 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
2200 |.macro ffstring_op, name
2201 | .ffunc string_ .. name
2203 | cmplwi NARGS8:RC, 8
2204 | lwz CARG3, 0(BASE)
2205 | lwz STR:CARG2, 4(BASE)
2206 | blt ->fff_fallback
2208 | la SBUF:CARG1, DISPATCH_GL(tmpbuf)(DISPATCH)
2209 | bne ->fff_fallback
2210 | lwz TMP0, SBUF:CARG1->b
2211 | stw L, SBUF:CARG1->L
2214 | stw TMP0, SBUF:CARG1->p
2215 | bl extern lj_buf_putstr_ .. name
2216 | bl extern lj_buf_tostr
2220 |ffstring_op reverse
2224 |//-- Bit library --------------------------------------------------------
2226 |.macro .ffunc_bit, name
2228 | .ffunc_1 bit_..name
2229 | checknum CARG3; bnel ->fff_tobit_fb
2231 | .ffunc_n bit_..name
2232 | fadd FARG1, FARG1, TOBIT
2234 | lwz CARG1, TMPD_LO
2238 |.macro .ffunc_bit_op, name, ins
2240 | addi TMP1, BASE, 8
2241 | add TMP2, BASE, NARGS8:RC
2243 | lwz CARG4, 0(TMP1)
2244 | cmplw cr1, TMP1, TMP2
2246 | lwz CARG2, 4(TMP1)
2248 | lfd FARG1, 0(TMP1)
2250 | bgey cr1, ->fff_resi
2253 | bnel ->fff_bitop_fb
2255 | fadd FARG1, FARG1, TOBIT
2256 | bge ->fff_fallback
2258 | lwz CARG2, TMPD_LO
2260 | ins CARG1, CARG1, CARG2
2261 | addi TMP1, TMP1, 8
2265 |.ffunc_bit_op band, and
2266 |.ffunc_bit_op bor, or
2267 |.ffunc_bit_op bxor, xor
2270 | rotlwi TMP0, CARG1, 8
2271 | rlwimi TMP0, CARG1, 24, 0, 7
2272 | rlwimi TMP0, CARG1, 24, 16, 23
2280 |.macro .ffunc_bit_sh, name, ins, shmod
2282 | .ffunc_2 bit_..name
2283 | checknum CARG3; bnel ->fff_tobit_fb
2284 | // Note: no inline conversion from number for 2nd argument!
2285 | checknum CARG4; bne ->fff_fallback
2287 | .ffunc_nn bit_..name
2288 | fadd FARG1, FARG1, TOBIT
2289 | fadd FARG2, FARG2, TOBIT
2291 | lwz CARG1, TMPD_LO
2293 | lwz CARG2, TMPD_LO
2296 | rlwinm CARG2, CARG2, 0, 27, 31
2300 | ins CRET1, CARG1, CARG2
2304 |.ffunc_bit_sh lshift, slw, 1
2305 |.ffunc_bit_sh rshift, srw, 1
2306 |.ffunc_bit_sh arshift, sraw, 1
2307 |.ffunc_bit_sh rol, rotlw, 0
2308 |.ffunc_bit_sh ror, rotlw, 2
2315 | tonum_i FARG1, CRET1
2318 | lwz PC, FRAME_PC(BASE)
2320 | stfd FARG1, -8(BASE)
2323 |// Fallback FP number to bit conversion.
2326 | lfd FARG1, 0(BASE)
2327 | bgt ->fff_fallback
2328 | fadd FARG1, FARG1, TOBIT
2330 | lwz CARG1, TMPD_LO
2335 | lfd FARG1, 0(TMP1)
2336 | bgt ->fff_fallback
2337 | fadd FARG1, FARG1, TOBIT
2339 | lwz CARG2, TMPD_LO
2343 |//-----------------------------------------------------------------------
2345 |->fff_fallback: // Call fast function fallback handler.
2346 | // BASE = new base, RB = CFUNC, RC = nargs*8
2347 | lp TMP3, CFUNC:RB->f
2348 | add TMP1, BASE, NARGS8:RC
2349 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2350 | addi TMP0, TMP1, 8*LUA_MINSTACK
2351 | lwz TMP2, L->maxstack
2352 | stw PC, SAVE_PC // Redundant (but a defined value).
2353 | .toc lp TMP3, 0(TMP3)
2358 | bgt >5 // Need to grow stack.
2360 | bctrl // (lua_State *L)
2361 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2366 | bgt ->fff_res // Returned nresults+1?
2367 |1: // Returned 0 or -1: retry fast path.
2369 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2370 | sub NARGS8:RC, TMP0, BASE
2371 | bne ->vm_call_tail // Returned -1?
2372 | ins_callt // Returned 0: retry fast path.
2374 |// Reconstruct previous base for vmeta_call during tailcall.
2376 | andix. TMP0, PC, FRAME_TYPE
2377 | rlwinm TMP1, PC, 0, 0, 28
2380 | decode_RA8 TMP1, INS
2381 | addi TMP1, TMP1, 8
2383 | sub TMP2, BASE, TMP1
2384 | b ->vm_call_dispatch // Resolve again for tailcall.
2386 |5: // Grow stack for fallback handler.
2387 | li CARG2, LUA_MINSTACK
2388 | bl extern lj_state_growstack // (lua_State *L, int n)
2390 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
2393 |->fff_gcstep: // Call GC step function.
2394 | // BASE = new base, RC = nargs*8
2397 | add TMP0, BASE, NARGS8:RC
2398 | stw PC, SAVE_PC // Redundant (but a defined value).
2401 | bl extern lj_gc_step // (lua_State *L)
2405 | sub NARGS8:RC, TMP0, BASE
2406 | lwz CFUNC:RB, FRAME_FUNC(BASE)
2409 |//-----------------------------------------------------------------------
2410 |//-- Special dispatch targets -------------------------------------------
2411 |//-----------------------------------------------------------------------
2413 |->vm_record: // Dispatch target for recording phase.
2415 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2416 | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2418 | // Decrement the hookcount for consistency, but always do the call.
2419 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2420 | andix. TMP0, TMP3, HOOK_ACTIVE
2422 | subi TMP2, TMP2, 1
2423 | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2425 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2429 |->vm_rethook: // Dispatch target for return hooks.
2430 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2431 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2433 |5: // Re-dispatch to static ins.
2434 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS.
2435 | lpx TMP0, DISPATCH, TMP1
2439 |->vm_inshook: // Dispatch target for instr/line hooks.
2440 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2441 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2442 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2443 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
2446 | cmpwi cr1, TMP0, 0
2447 | addic. TMP2, TMP2, -1
2449 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2454 | stw MULTRES, SAVE_MULTRES
2457 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2458 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2461 |4: // Re-dispatch to static ins.
2463 | decode_OPP TMP1, INS
2464 | decode_RB8 RB, INS
2465 | addi TMP1, TMP1, GG_DISP2STATIC
2466 | decode_RD8 RD, INS
2467 | lpx TMP0, DISPATCH, TMP1
2468 | decode_RA8 RA, INS
2469 | decode_RC8 RC, INS
2473 |->cont_hook: // Continue from hook yield.
2475 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
2478 |->vm_hotloop: // Hot loop counter underflow.
2480 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2481 | addi CARG1, DISPATCH, GG_DISP2J
2483 | lwz TMP1, LFUNC:TMP1->pc
2485 | stw L, DISPATCH_J(L)(DISPATCH)
2486 | lbz TMP1, PC2PROTO(framesize)(TMP1)
2488 | slwi TMP1, TMP1, 3
2489 | add TMP1, BASE, TMP1
2491 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2495 |->vm_callhook: // Dispatch target for call hooks.
2501 |->vm_hotcall: // Hot call counter underflow.
2506 | add TMP0, BASE, RC
2512 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2513 | // Returns ASMFunction.
2516 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
2517 | sub NARGS8:RC, TMP0, BASE
2519 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2524 |->cont_stitch: // Trace stitching.
2526 | // RA = resultptr, RB = meta base
2528 | lwz TMP3, -20(RB) // Save previous trace number.
2529 | addic. TMP1, MULTRES, -8
2530 | decode_RA8 RC, INS // Call base.
2532 |1: // Move results down.
2534 | addic. TMP1, TMP1, -8
2536 | stfdx f0, BASE, RC
2540 | decode_RA8 RA, INS
2541 | decode_RB8 RB, INS
2543 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
2546 | bgt >9 // More results wanted?
2548 | slwi TMP2, TMP3, 2
2549 | lwzx TRACE:TMP2, TMP1, TMP2
2550 | cmpwi TRACE:TMP2, 0
2552 | lhz RD, TRACE:TMP2->link
2555 | beq ->cont_nop // Blacklisted.
2557 | bne cr1, =>BC_JLOOP // Jump to stitched trace.
2559 | // Stitch a new trace to the previous trace.
2560 | stw TMP3, DISPATCH_J(exitno)(DISPATCH)
2561 | stp L, DISPATCH_J(L)(DISPATCH)
2563 | addi CARG1, DISPATCH, GG_DISP2J
2565 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2570 | stwx TISNIL, BASE, RC
2575 |->vm_profhook: // Dispatch target for profiler hook.
2578 | stw MULTRES, SAVE_MULTRES
2581 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2582 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2588 |//-----------------------------------------------------------------------
2589 |//-- Trace exit handler -------------------------------------------------
2590 |//-----------------------------------------------------------------------
2592 |.macro savex_, a, b, c, d
2593 | stfd f..a, 16+a*8(sp)
2594 | stfd f..b, 16+b*8(sp)
2595 | stfd f..c, 16+c*8(sp)
2596 | stfd f..d, 16+d*8(sp)
2601 | addi sp, sp, -(16+32*8+32*4)
2602 | stmw r2, 16+32*8+2*4(sp)
2603 | addi DISPATCH, JGL, -GG_DISP2G-32768
2604 | li CARG2, ~LJ_VMST_EXIT
2605 | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain.
2606 | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH)
2608 | stw CARG1, 0(sp) // Store extended stack chain.
2611 | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp.
2613 | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP.
2614 | savex_ 12,13,14,15
2617 | savex_ 16,17,18,19
2618 | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP.
2619 | savex_ 20,21,22,23
2620 | lhz CARG4, 2(CARG3) // Load trace number.
2621 | savex_ 24,25,26,27
2622 | lwz L, DISPATCH_GL(cur_L)(DISPATCH)
2623 | savex_ 28,29,30,31
2624 | sub CARG3, TMP0, CARG3 // Compute exit number.
2625 | lp BASE, DISPATCH_GL(jit_base)(DISPATCH)
2626 | srwi CARG3, CARG3, 2
2627 | stp L, DISPATCH_J(L)(DISPATCH)
2628 | subi CARG3, CARG3, 2
2630 | stw CARG4, DISPATCH_J(parent)(DISPATCH)
2631 | stw TMP1, DISPATCH_GL(jit_base)(DISPATCH)
2632 | addi CARG1, DISPATCH, GG_DISP2J
2633 | stw CARG3, DISPATCH_J(exitno)(DISPATCH)
2634 | addi CARG2, sp, 16
2635 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2636 | // Returns MULTRES (unscaled) or negated error code.
2637 | lp TMP1, L->cframe
2641 | rldicr sp, TMP1, 0, 61
2643 | rlwinm sp, TMP1, 0, 0, 29
2645 | lwz PC, SAVE_PC // Get SAVE_PC.
2647 | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2652 | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set.
2654 | addi DISPATCH, JGL, -GG_DISP2G-32768
2658 | blt >9 // Check for error from exit.
2659 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2660 | slwi MULTRES, CARG1, 3
2662 | stw MULTRES, SAVE_MULTRES
2663 | lwz TMP1, LFUNC:RB->pc
2664 | stw TMP2, DISPATCH_GL(jit_base)(DISPATCH)
2665 | lwz KBASE, PC2PROTO(k)(TMP1)
2666 | // Setup type comparison constants.
2667 | li TISNUM, LJ_TISNUM
2668 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2671 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2674 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2675 | li TISNIL, LJ_TNIL
2676 | stw TMP0, TONUM_HI
2678 | // Modified copy of ins_next which handles function header dispatch, too.
2681 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1.
2682 | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2683 | decode_OPP TMP1, INS
2684 | decode_RA8 RA, INS
2685 | lpx TMP0, DISPATCH, TMP1
2687 | cmplwi TMP1, BC_FUNCF*4 // Function header?
2689 | decode_RB8 RB, INS
2690 | decode_RD8 RD, INS
2691 | decode_RC8 RC, INS
2694 | cmplwi TMP1, (BC_FUNCC+2)*4 // Fast function?
2696 | // Check frame below fast function.
2697 | lwz TMP1, FRAME_PC(BASE)
2698 | andix. TMP0, TMP1, FRAME_TYPE
2699 | bney >3 // Trace stitching continuation?
2700 | // Otherwise set KBASE for Lua function below fast function.
2701 | lwz TMP2, -4(TMP1)
2702 | decode_RA8 TMP0, TMP2
2703 | sub TMP1, BASE, TMP0
2704 | lwz LFUNC:TMP2, -12(TMP1)
2705 | lwz TMP1, LFUNC:TMP2->pc
2706 | lwz KBASE, PC2PROTO(k)(TMP1)
2708 | subi RC, MULTRES, 8
2712 |9: // Rethrow error from the right C frame.
2715 | bl extern lj_err_throw // (lua_State *L, int errcode)
2718 |//-----------------------------------------------------------------------
2719 |//-- Math helper functions ----------------------------------------------
2720 |//-----------------------------------------------------------------------
2722 |// NYI: Use internal implementations of floor, ceil, trunc.
2725 | divwo. TMP0, CARG1, CARG2
2728 | xor CARG3, CARG1, CARG2
2731 | xor. CARG3, CARG1, CARG2
2733 | mullw TMP0, TMP0, CARG2
2734 | sub CARG1, CARG1, TMP0
2736 | cmpwi CARG1, 0; beqlr
2737 | add CARG1, CARG1, CARG2
2743 | clrso TMP0 // Clear SO for -2147483648 % -1 and return 0.
2746 |//-----------------------------------------------------------------------
2747 |//-- Miscellaneous functions --------------------------------------------
2748 |//-----------------------------------------------------------------------
2750 |// void lj_vm_cachesync(void *start, void *end)
2751 |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size.
2752 |// This is a good lower bound, except for very ancient PPC models.
2755 | // Compute start of first cache line and number of cache lines.
2756 | rlwinm CARG1, CARG1, 0, 0, 26
2757 | sub CARG2, CARG2, CARG1
2758 | addi CARG2, CARG2, 31
2759 | rlwinm. CARG2, CARG2, 27, 5, 31
2763 |1: // Flush D-Cache.
2765 | addi CARG1, CARG1, 32
2769 |1: // Invalidate I-Cache.
2771 | addi CARG3, CARG3, 32
2777 |//-----------------------------------------------------------------------
2778 |//-- FFI helper functions -----------------------------------------------
2779 |//-----------------------------------------------------------------------
2781 |// Handler for callback functions. Callback slot number in r11, g in r12.
2784 |.type CTSTATE, CTState, PC
2786 | lwz CTSTATE, GL:r12->ctype_state
2787 | addi DISPATCH, r12, GG_G2DISP
2788 | stw r11, CTSTATE->cb.slot
2789 | stw r3, CTSTATE->cb.gpr[0]
2790 | stfd f1, CTSTATE->cb.fpr[0]
2791 | stw r4, CTSTATE->cb.gpr[1]
2792 | stfd f2, CTSTATE->cb.fpr[1]
2793 | stw r5, CTSTATE->cb.gpr[2]
2794 | stfd f3, CTSTATE->cb.fpr[2]
2795 | stw r6, CTSTATE->cb.gpr[3]
2796 | stfd f4, CTSTATE->cb.fpr[3]
2797 | stw r7, CTSTATE->cb.gpr[4]
2798 | stfd f5, CTSTATE->cb.fpr[4]
2799 | stw r8, CTSTATE->cb.gpr[5]
2800 | stfd f6, CTSTATE->cb.fpr[5]
2801 | stw r9, CTSTATE->cb.gpr[6]
2802 | stfd f7, CTSTATE->cb.fpr[6]
2803 | stw r10, CTSTATE->cb.gpr[7]
2804 | stfd f8, CTSTATE->cb.fpr[7]
2805 | addi TMP0, sp, CFRAME_SPACE+8
2806 | stw TMP0, CTSTATE->cb.stack
2808 | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2810 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2811 | // Returns lua_State *.
2812 | lp BASE, L:CRET1->base
2813 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2814 | lp RC, L:CRET1->top
2815 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2819 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2820 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2821 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2822 | stw TMP0, TONUM_HI
2823 | li TISNIL, LJ_TNIL
2833 |->cont_ffi_callback: // Return from FFI callback.
2835 | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
2841 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2842 | lwz CRET1, CTSTATE->cb.gpr[0]
2843 | lfd FARG1, CTSTATE->cb.fpr[0]
2844 | lwz CRET2, CTSTATE->cb.gpr[1]
2848 |->vm_ffi_call: // Call C function via FFI.
2849 | // Caveat: needs special frame unwinding, see below.
2851 | .type CCSTATE, CCallState, CARG1
2852 | lwz TMP1, CCSTATE->spadj
2854 | lbz CARG2, CCSTATE->nsp
2855 | lbz CARG3, CCSTATE->nfpr
2858 | cmpwi cr1, CARG3, 0
2860 | addic. CARG2, CARG2, -1
2861 | stwux sp, sp, TMP1
2862 | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls.
2864 | stw CCSTATE, -8(TMP2)
2866 | la TMP1, CCSTATE->stack
2867 | slwi CARG2, CARG2, 2
2871 | lwzx TMP0, TMP1, CARG2
2872 | stwx TMP0, TMP2, CARG2
2873 | addic. CARG2, CARG2, -4
2877 | lfd f1, CCSTATE->fpr[0]
2878 | lfd f2, CCSTATE->fpr[1]
2879 | lfd f3, CCSTATE->fpr[2]
2880 | lfd f4, CCSTATE->fpr[3]
2881 | lfd f5, CCSTATE->fpr[4]
2882 | lfd f6, CCSTATE->fpr[5]
2883 | lfd f7, CCSTATE->fpr[6]
2884 | lfd f8, CCSTATE->fpr[7]
2886 | lp TMP0, CCSTATE->func
2887 | lwz CARG2, CCSTATE->gpr[1]
2888 | lwz CARG3, CCSTATE->gpr[2]
2889 | lwz CARG4, CCSTATE->gpr[3]
2890 | lwz CARG5, CCSTATE->gpr[4]
2892 | lwz r8, CCSTATE->gpr[5]
2893 | lwz r9, CCSTATE->gpr[6]
2894 | lwz r10, CCSTATE->gpr[7]
2895 | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
2897 | lwz CCSTATE:TMP1, -8(r14)
2900 | stw CARG1, CCSTATE:TMP1->gpr[0]
2901 | stfd FARG1, CCSTATE:TMP1->fpr[0]
2902 | stw CARG2, CCSTATE:TMP1->gpr[1]
2904 | stw CARG3, CCSTATE:TMP1->gpr[2]
2906 | stw CARG4, CCSTATE:TMP1->gpr[3]
2910 |// Note: vm_ffi_call must be the last function in this object file!
2912 |//-----------------------------------------------------------------------
2915 /* Generate the code for a single instruction. */
2916 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2923 /* -- Comparison ops ---------------------------------------------------- */
2925 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2927 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2928 | // RA = src1*8, RD = src2*8, JMP with RD = target
2930 | lwzux TMP0, RA, BASE
2933 | lwzux TMP1, RD, BASE
2935 | checknum cr0, TMP0
2937 | decode_RD4 TMP2, TMP2
2938 | checknum cr1, TMP1
2939 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2943 if (op == BC_ISLT) {
2945 } else if (op == BC_ISGE) {
2947 } else if (op == BC_ISLE) {
2957 |7: // RA is not an integer.
2958 | bgt cr0, ->vmeta_comp
2959 | // RA is a number.
2961 | bgt cr1, ->vmeta_comp
2963 | // RA is a number, RD is an integer.
2967 |8: // RA is an integer, RD is not an integer.
2968 | bgt cr1, ->vmeta_comp
2969 | // RA is an integer, RD is a number.
2975 if (op == BC_ISLT) {
2977 } else if (op == BC_ISGE) {
2979 } else if (op == BC_ISLE) {
2980 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2983 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2988 | lwzx TMP0, BASE, RA
2991 | lwzx TMP1, BASE, RD
2992 | checknum cr0, TMP0
2995 | checknum cr1, TMP1
2996 | decode_RD4 TMP2, TMP2
2997 | bge cr0, ->vmeta_comp
2998 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2999 | bge cr1, ->vmeta_comp
3001 if (op == BC_ISLT) {
3003 } else if (op == BC_ISGE) {
3005 } else if (op == BC_ISLE) {
3006 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3009 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3018 case BC_ISEQV: case BC_ISNEV:
3019 vk = op == BC_ISEQV;
3020 | // RA = src1*8, RD = src2*8, JMP with RD = target
3022 | lwzux TMP0, RA, BASE
3025 | lwzux TMP1, RD, BASE
3026 | checknum cr0, TMP0
3028 | checknum cr1, TMP1
3029 | decode_RD4 TMP2, TMP2
3031 | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt
3032 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3034 | ble cr7, ->BC_ISEQN_Z
3036 | ble cr7, ->BC_ISNEN_Z
3039 | lwzux TMP0, RA, BASE
3043 | lwzux TMP1, RD, BASE
3044 | checknum cr0, TMP0
3045 | decode_RD4 TMP2, TMP2
3047 | checknum cr1, TMP1
3048 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3062 |5: // Either or both types are not numbers.
3068 | cmpwi cr7, TMP0, LJ_TCDATA
3069 | cmpwi cr5, TMP1, LJ_TCDATA
3073 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
3075 | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq
3077 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
3079 | beq cr7, ->vmeta_equal_cd
3081 | cmplw cr5, CARG2, CARG3
3082 | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive.
3083 | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type.
3084 | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv.
3086 | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2.
3087 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2.
3098 | bge cr0, >2 // Done if 1 or 2.
3103 | blt cr0, <1 // Done if 1 or 2.
3105 | blt cr6, <1 // Done if not tab/ud.
3107 | // Different tables or userdatas. Need to check __eq metamethod.
3108 | // Field metatable must be at same offset for GCtab and GCudata!
3109 | lwz TAB:TMP2, TAB:CARG2->metatable
3110 | li CARG4, 1-vk // ne = 0 or 1.
3111 | cmplwi TAB:TMP2, 0
3112 | beq <1 // No metatable?
3113 | lbz TMP2, TAB:TMP2->nomm
3114 | andix. TMP2, TMP2, 1<<MM_eq
3115 | bne <1 // Or 'no __eq' flag set?
3116 | mr PC, SAVE0 // Restore old PC.
3117 | b ->vmeta_equal // Handle __eq metamethod.
3120 case BC_ISEQS: case BC_ISNES:
3121 vk = op == BC_ISEQS;
3122 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3123 | lwzux TMP0, RA, BASE
3125 | lwz STR:TMP3, 4(RA)
3130 | cmpwi TMP0, LJ_TCDATA
3132 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
3133 | .gpr64 extsw TMP0, TMP0
3134 | subfic TMP0, TMP0, LJ_TSTR
3136 | beq ->vmeta_equal_cd
3138 | sub TMP1, STR:TMP1, STR:TMP3
3139 | or TMP0, TMP0, TMP1
3140 | decode_RD4 TMP2, TMP2
3141 | subfic TMP0, TMP0, 0
3142 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3143 | subfe TMP1, TMP1, TMP1
3145 | andc TMP2, TMP2, TMP1
3147 | and TMP2, TMP2, TMP1
3153 case BC_ISEQN: case BC_ISNEN:
3154 vk = op == BC_ISEQN;
3155 | // RA = src*8, RD = num_const*8, JMP with RD = target
3157 | lwzux TMP0, RA, BASE
3160 | lwzux TMP1, RD, KBASE
3161 | checknum cr0, TMP0
3163 | checknum cr1, TMP1
3164 | decode_RD4 TMP2, TMP2
3166 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3178 |->BC_ISEQN_Z: // Dummy label.
3180 |->BC_ISNEN_Z: // Dummy label.
3182 | lwzx TMP0, BASE, RA
3186 | lfdx f1, KBASE, RD
3187 | decode_RD4 TMP2, TMP2
3189 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3212 | cmpwi TMP0, LJ_TCDATA
3213 | beq ->vmeta_equal_cd
3217 |7: // RA is not an integer.
3219 | // RA is a number.
3222 | // RA is a number, RD is an integer.
3226 |8: // RA is an integer, RD is a number.
3236 case BC_ISEQP: case BC_ISNEP:
3237 vk = op == BC_ISEQP;
3238 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3239 | lwzx TMP0, BASE, RA
3245 | cmpwi TMP0, LJ_TCDATA
3247 | sub TMP0, TMP0, TMP1
3249 | beq ->vmeta_equal_cd
3251 | decode_RD4 TMP2, TMP2
3252 | .gpr64 extsw TMP0, TMP0
3253 | addic TMP0, TMP0, -1
3254 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3255 | subfe TMP1, TMP1, TMP1
3257 | and TMP2, TMP2, TMP1
3259 | andc TMP2, TMP2, TMP1
3265 /* -- Unary test and copy ops ------------------------------------------- */
3267 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3268 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3269 | lwzx TMP0, BASE, RD
3272 if (op == BC_IST || op == BC_ISF) {
3273 | .gpr64 extsw TMP0, TMP0
3274 | subfic TMP0, TMP0, LJ_TTRUE
3275 | decode_RD4 TMP2, INS
3276 | subfe TMP1, TMP1, TMP1
3277 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3279 | andc TMP2, TMP2, TMP1
3281 | and TMP2, TMP2, TMP1
3285 | li TMP1, LJ_TFALSE
3288 if (op == BC_ISTC) {
3293 | addis PC, PC, -(BCBIAS_J*4 >> 16)
3294 | decode_RD4 TMP2, INS
3295 | stfdx f0, BASE, RA
3303 | // RA = src*8, RD = -type*8
3304 | lwzx TMP0, BASE, RA
3307 |.if not PPE and not GPR64
3308 | add. TMP0, TMP0, TMP1
3313 | bne ->vmeta_istype
3317 | // RA = src*8, RD = -(TISNUM-1)*8
3318 | lwzx TMP0, BASE, RA
3321 | bge ->vmeta_istype
3325 /* -- Unary ops --------------------------------------------------------- */
3328 | // RA = dst*8, RD = src*8
3331 | stfdx f0, BASE, RA
3335 | // RA = dst*8, RD = src*8
3337 | lwzx TMP0, BASE, RD
3338 | .gpr64 extsw TMP0, TMP0
3339 | subfic TMP1, TMP0, LJ_TTRUE
3340 | adde TMP0, TMP0, TMP1
3341 | stwx TMP0, BASE, RA
3345 | // RA = dst*8, RD = src*8
3346 | lwzux TMP1, RD, BASE
3362 | stwux TISNUM, RA, BASE
3368 | // Potential overflow.
3369 | checkov TMP1, <1 // Ignore unrelated overflow.
3371 | lus TMP1, 0x41e0 // 2^31.
3377 | xoris TMP1, TMP1, 0x8000
3380 | stwux TMP1, RA, BASE
3389 | // RA = dst*8, RD = src*8
3390 | lwzux TMP0, RD, BASE
3392 | checkstr TMP0; bne >2
3393 | lwz CRET1, STR:CARG1->len
3397 | stwux TISNUM, RA, BASE
3400 | tonum_u f0, CRET1 // Result is a non-negative integer.
3402 | stfdx f0, BASE, RA
3406 | checktab TMP0; bne ->vmeta_len
3408 | lwz TAB:TMP2, TAB:CARG1->metatable
3409 | cmplwi TAB:TMP2, 0
3414 | bl extern lj_tab_len // (GCtab *t)
3415 | // Returns uint32_t (but less than 2^31).
3419 | lbz TMP0, TAB:TMP2->nomm
3420 | andix. TMP0, TMP0, 1<<MM_len
3421 | bne <3 // 'no __len' flag set: done.
3426 /* -- Binary ops -------------------------------------------------------- */
3428 |.macro ins_arithpre
3429 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3430 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3433 | lwzx TMP1, BASE, RB
3435 | lwzx TMP2, KBASE, RC
3437 | lfdx f14, BASE, RB
3438 | lfdx f15, KBASE, RC
3440 | checknum cr0, TMP1
3441 | checknum cr1, TMP2
3442 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3443 | bge ->vmeta_arith_vn
3445 | checknum TMP1; bge ->vmeta_arith_vn
3449 | lwzx TMP1, BASE, RB
3451 | lwzx TMP2, KBASE, RC
3453 | lfdx f15, BASE, RB
3454 | lfdx f14, KBASE, RC
3456 | checknum cr0, TMP1
3457 | checknum cr1, TMP2
3458 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3459 | bge ->vmeta_arith_nv
3461 | checknum TMP1; bge ->vmeta_arith_nv
3465 | lwzx TMP1, BASE, RB
3466 | lwzx TMP2, BASE, RC
3467 | lfdx f14, BASE, RB
3468 | lfdx f15, BASE, RC
3469 | checknum cr0, TMP1
3470 | checknum cr1, TMP2
3471 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3472 | bge ->vmeta_arith_vv
3477 |.macro ins_arithfallback, ins
3480 | ins ->vmeta_arith_vn2
3483 | ins ->vmeta_arith_nv2
3486 | ins ->vmeta_arith_vv2
3491 |.macro intmod, a, b, c
3495 |.macro fpmod, a, b, c
3498 | // NYI: Use internal implementation of floor.
3499 | blex floor // floor(b/c)
3501 | fsub a, b, a // b - floor(b/c)*c
3504 |.macro ins_arithfp, fpins
3506 |.if "fpins" == "fpmod_"
3507 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3509 | fpins f0, f14, f15
3511 | stfdx f0, BASE, RA
3516 |.macro ins_arithdn, intins, fpins
3517 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3518 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3521 | lwzux TMP1, RB, BASE
3522 | lwzux TMP2, RC, KBASE
3524 | checknum cr0, TMP1
3528 | lwzux TMP1, RB, BASE
3529 | lwzux TMP2, RC, KBASE
3531 | checknum cr0, TMP1
3535 | lwzux TMP1, RB, BASE
3536 | lwzux TMP2, RC, BASE
3538 | checknum cr0, TMP1
3542 | checknum cr1, TMP2
3545 | intins CARG1, CARG1, CARG2
3549 | stwux TISNUM, RA, BASE
3554 | checkov TMP0, <1 // Ignore unrelated overflow.
3555 | ins_arithfallback b
3559 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3563 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3566 | ins_arithfallback bge
3567 |.if "fpins" == "fpmod_"
3568 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3570 | fpins f0, f14, f15
3572 | stfdx f0, BASE, RA
3577 |.macro ins_arith, intins, fpins
3579 | ins_arithdn intins, fpins
3585 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3587 |.macro addo32., y, a, b
3588 | // Need to check overflow for (a<<32) + (b<<32).
3589 | rldicr TMP0, a, 32, 31
3590 | rldicr TMP3, b, 32, 31
3591 | addo. TMP0, TMP0, TMP3
3594 | ins_arith addo32., fadd
3596 | ins_arith addo., fadd
3599 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3601 |.macro subo32., y, a, b
3602 | // Need to check overflow for (a<<32) - (b<<32).
3603 | rldicr TMP0, a, 32, 31
3604 | rldicr TMP3, b, 32, 31
3605 | subo. TMP0, TMP0, TMP3
3608 | ins_arith subo32., fsub
3610 | ins_arith subo., fsub
3613 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3614 | ins_arith mullwo., fmul
3616 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3620 | ins_arith intmod, fpmod
3622 case BC_MODNV: case BC_MODVV:
3623 | ins_arith intmod, fpmod_
3626 | // NYI: (partial) integer arithmetic.
3627 | lwzx TMP1, BASE, RB
3628 | lfdx FARG1, BASE, RB
3629 | lwzx TMP2, BASE, RC
3630 | lfdx FARG2, BASE, RC
3631 | checknum cr0, TMP1
3632 | checknum cr1, TMP2
3633 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3634 | bge ->vmeta_arith_vv
3637 | stfdx FARG1, BASE, RA
3642 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3645 | add CARG2, BASE, RC
3650 | srwi CARG3, CARG3, 3
3651 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3652 | // Returns NULL (finished) or TValue * (metamethod).
3657 | lfdx f0, BASE, SAVE0 // Copy result from RB to RA.
3658 | stfdx f0, BASE, RA
3662 /* -- Constant ops ------------------------------------------------------ */
3665 | // RA = dst*8, RD = str_const*8 (~)
3667 | subfic TMP1, TMP1, -4
3669 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
3671 | stwux TMP2, RA, BASE
3677 | // RA = dst*8, RD = cdata_const*8 (~)
3679 | subfic TMP1, TMP1, -4
3681 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
3682 | li TMP2, LJ_TCDATA
3683 | stwux TMP2, RA, BASE
3689 | // RA = dst*8, RD = int16_literal*8
3694 | stwux TISNUM, RA, BASE
3698 | // The soft-float approach is faster.
3700 | srawi TMP1, RD, 31
3701 | xor TMP2, TMP1, RD
3702 | sub TMP2, TMP2, TMP1 // TMP2 = abs(x)
3704 | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1
3705 | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa
3706 | subfic TMP3, RD, 0
3707 | slwi TMP1, TMP1, 20
3708 | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11)
3709 | subfe TMP0, TMP0, TMP0
3710 | add RD, RD, TMP1 // hi = hi + exponent-1
3711 | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi
3713 | stwux RD, RA, BASE
3719 | // RA = dst*8, RD = num_const*8
3721 | lfdx f0, KBASE, RD
3722 | stfdx f0, BASE, RA
3726 | // RA = dst*8, RD = primitive_type*8 (~)
3730 | stwx TMP0, BASE, RA
3734 | // RA = base*8, RD = end*8
3735 | stwx TISNIL, BASE, RA
3738 | stwx TISNIL, BASE, RA
3745 /* -- Upvalue and function ops ------------------------------------------ */
3748 | // RA = dst*8, RD = uvnum*8
3749 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3751 | addi RD, RD, offsetof(GCfuncL, uvptr)
3752 | lwzx UPVAL:RB, LFUNC:RB, RD
3754 | lwz TMP1, UPVAL:RB->v
3756 | stfdx f0, BASE, RA
3760 | // RA = uvnum*8, RD = src*8
3761 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3763 | addi RA, RA, offsetof(GCfuncL, uvptr)
3764 | lfdux f0, RD, BASE
3765 | lwzx UPVAL:RB, LFUNC:RB, RA
3766 | lbz TMP3, UPVAL:RB->marked
3767 | lwz CARG2, UPVAL:RB->v
3768 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3769 | lbz TMP0, UPVAL:RB->closed
3772 | cmplwi cr1, TMP0, 0
3774 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3775 | subi TMP2, TMP2, (LJ_TNUMX+1)
3776 | bne >2 // Upvalue is closed and black?
3780 |2: // Check if new value is collectable.
3781 | cmplwi TMP2, LJ_TISGCV - (LJ_TNUMX+1)
3782 | bge <1 // tvisgcv(v)
3783 | lbz TMP3, GCOBJ:TMP1->gch.marked
3784 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
3785 | la CARG1, GG_DISP2G(DISPATCH)
3786 | // Crossed a write barrier. Move the barrier forward.
3788 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3792 | // RA = uvnum*8, RD = str_const*8 (~)
3793 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3796 | subfic TMP1, TMP1, -4
3797 | addi RA, RA, offsetof(GCfuncL, uvptr)
3798 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
3799 | lwzx UPVAL:RB, LFUNC:RB, RA
3800 | lbz TMP3, UPVAL:RB->marked
3801 | lwz CARG2, UPVAL:RB->v
3802 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3803 | lbz TMP3, STR:TMP1->marked
3804 | lbz TMP2, UPVAL:RB->closed
3806 | stw STR:TMP1, 4(CARG2)
3807 | stw TMP0, 0(CARG2)
3812 |2: // Check if string is white and ensure upvalue is closed.
3813 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
3814 | cmplwi cr1, TMP2, 0
3815 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3816 | la CARG1, GG_DISP2G(DISPATCH)
3817 | // Crossed a write barrier. Move the barrier forward.
3819 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3823 | // RA = uvnum*8, RD = num_const*8
3824 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3826 | addi RA, RA, offsetof(GCfuncL, uvptr)
3827 | lfdx f0, KBASE, RD
3828 | lwzx UPVAL:RB, LFUNC:RB, RA
3830 | lwz TMP1, UPVAL:RB->v
3835 | // RA = uvnum*8, RD = primitive_type*8 (~)
3836 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3839 | addi RA, RA, offsetof(GCfuncL, uvptr)
3841 | lwzx UPVAL:RB, LFUNC:RB, RA
3843 | lwz TMP1, UPVAL:RB->v
3849 | // RA = level*8, RD = target
3850 | lwz TMP1, L->openupval
3851 | branch_RD // Do this first since RD is not saved.
3856 | add CARG2, BASE, RA
3857 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3864 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
3867 | subfic TMP1, TMP1, -4
3869 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3871 | lwz CARG3, FRAME_FUNC(BASE)
3872 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3873 | bl extern lj_func_newL_gc
3874 | // Returns GCfuncL *.
3877 | stwux TMP0, RA, BASE
3878 | stw LFUNC:CRET1, 4(RA)
3882 /* -- Table ops --------------------------------------------------------- */
3886 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
3887 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
3889 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
3895 if (op == BC_TNEW) {
3896 | rlwinm CARG2, RD, 29, 21, 31
3897 | rlwinm CARG3, RD, 18, 27, 31
3898 | cmpwi CARG2, 0x7ff; beq >3
3900 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3901 | // Returns Table *.
3904 | subfic TMP1, TMP1, -4
3905 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3906 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3907 | // Returns Table *.
3911 | stwux TMP0, RA, BASE
3912 | stw TAB:CRET1, 4(RA)
3914 if (op == BC_TNEW) {
3921 | bl extern lj_gc_step_fixtop // (lua_State *L)
3928 | // RA = dst*8, RD = str_const*8 (~)
3930 | // RA = src*8, RD = str_const*8 (~)
3931 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
3933 | lwz TAB:RB, LFUNC:TMP2->env
3934 | subfic TMP1, TMP1, -4
3935 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3936 if (op == BC_GGET) {
3944 | // RA = dst*8, RB = table*8, RC = key*8
3945 | lwzux CARG1, RB, BASE
3946 | lwzux CARG2, RC, BASE
3954 | checknum cr1, CARG2
3957 | lwz TMP0, TAB:RB->asize
3959 | lwz TMP1, TAB:RB->array
3964 | // Convert number key to integer, check for integerness and range.
3966 | fadd f2, f0, TOBIT
3968 | lwz TMP0, TAB:RB->asize
3969 | fsub f2, f2, TOBIT
3971 | lwz TMP1, TAB:RB->array
3973 | cmplw cr0, TMP0, TMP2
3974 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
3975 | slwi TMP2, TMP2, 3
3977 | ble ->vmeta_tgetv // Integer key and in array part?
3978 | lwzx TMP0, TMP1, TMP2
3979 | lfdx f14, TMP1, TMP2
3980 | checknil TMP0; beq >2
3983 | stfdx f14, BASE, RA
3986 |2: // Check for __index if table value is nil.
3987 | lwz TAB:TMP2, TAB:RB->metatable
3988 | cmplwi TAB:TMP2, 0
3989 | beq <1 // No metatable: done.
3990 | lbz TMP0, TAB:TMP2->nomm
3991 | andix. TMP0, TMP0, 1<<MM_index
3992 | bne <1 // 'no __index' flag set: done.
3996 | checkstr CARG2; bne ->vmeta_tgetv
4000 | b ->BC_TGETS_Z // String key?
4003 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
4004 | lwzux CARG1, RB, BASE
4007 | subfic TMP1, TMP1, -4
4009 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4010 | bne ->vmeta_tgets1
4012 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
4013 | lwz TMP0, TAB:RB->hmask
4014 | lwz TMP1, STR:RC->hash
4015 | lwz NODE:TMP2, TAB:RB->node
4016 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
4017 | slwi TMP0, TMP1, 5
4018 | slwi TMP1, TMP1, 3
4019 | sub TMP1, TMP0, TMP1
4020 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4022 | lwz CARG1, NODE:TMP2->key
4023 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4024 | lwz CARG2, NODE:TMP2->val
4025 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
4026 | checkstr CARG1; bne >4
4027 | cmpw TMP0, STR:RC; bne >4
4028 | checknil CARG2; beq >5 // Key found, but nil value?
4030 | stwux CARG2, RA, BASE
4034 |4: // Follow hash chain.
4035 | lwz NODE:TMP2, NODE:TMP2->next
4036 | cmplwi NODE:TMP2, 0
4038 | // End of hash chain: key not found, nil result.
4041 |5: // Check for __index if table value is nil.
4042 | lwz TAB:TMP2, TAB:RB->metatable
4043 | cmplwi TAB:TMP2, 0
4044 | beq <3 // No metatable: done.
4045 | lbz TMP0, TAB:TMP2->nomm
4046 | andix. TMP0, TMP0, 1<<MM_index
4047 | bne <3 // 'no __index' flag set: done.
4051 | // RA = dst*8, RB = table*8, RC = index*8
4052 | lwzux CARG1, RB, BASE
4055 | checktab CARG1; bne ->vmeta_tgetb
4056 | lwz TMP1, TAB:RB->asize
4057 | lwz TMP2, TAB:RB->array
4058 | cmplw TMP0, TMP1; bge ->vmeta_tgetb
4059 | lwzx TMP1, TMP2, RC
4061 | checknil TMP1; beq >5
4064 | stfdx f0, BASE, RA
4067 |5: // Check for __index if table value is nil.
4068 | lwz TAB:TMP2, TAB:RB->metatable
4069 | cmplwi TAB:TMP2, 0
4070 | beq <1 // No metatable: done.
4071 | lbz TMP2, TAB:TMP2->nomm
4072 | andix. TMP2, TMP2, 1<<MM_index
4073 | bne <1 // 'no __index' flag set: done.
4074 | b ->vmeta_tgetb // Caveat: preserve TMP0!
4077 | // RA = dst*8, RB = table*8, RC = key*8
4079 | lwz TAB:CARG1, 4(RB)
4082 | lwz TMP0, TAB:CARG1->asize
4084 | lwz TMP1, TAB:CARG1->array
4087 | lwz TMP0, TAB:CARG1->asize
4089 | lwz TMP1, TAB:CARG1->array
4092 | slwi TMP2, CARG2, 3
4093 | ble ->vmeta_tgetr // In array part?
4094 | lfdx f14, TMP1, TMP2
4097 | stfdx f14, BASE, RA
4102 | // RA = src*8, RB = table*8, RC = key*8
4103 | lwzux CARG1, RB, BASE
4104 | lwzux CARG2, RC, BASE
4112 | checknum cr1, CARG2
4115 | lwz TMP0, TAB:RB->asize
4117 | lwz TMP1, TAB:RB->array
4122 | // Convert number key to integer, check for integerness and range.
4124 | fadd f2, f0, TOBIT
4126 | lwz TMP0, TAB:RB->asize
4127 | fsub f2, f2, TOBIT
4129 | lwz TMP1, TAB:RB->array
4131 | cmplw cr0, TMP0, TMP2
4132 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4133 | slwi TMP0, TMP2, 3
4135 | ble ->vmeta_tsetv // Integer key and in array part?
4136 | lwzx TMP2, TMP1, TMP0
4137 | lbz TMP3, TAB:RB->marked
4138 | lfdx f14, BASE, RA
4139 | checknil TMP2; beq >3
4141 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4142 | stfdx f14, TMP1, TMP0
4147 |3: // Check for __newindex if previous value is nil.
4148 | lwz TAB:TMP2, TAB:RB->metatable
4149 | cmplwi TAB:TMP2, 0
4150 | beq <1 // No metatable: done.
4151 | lbz TMP2, TAB:TMP2->nomm
4152 | andix. TMP2, TMP2, 1<<MM_newindex
4153 | bne <1 // 'no __newindex' flag set: done.
4157 | checkstr CARG2; bne ->vmeta_tsetv
4161 | b ->BC_TSETS_Z // String key?
4163 |7: // Possible table write barrier for the value. Skip valiswhite check.
4164 | barrierback TAB:RB, TMP3, TMP0
4168 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4169 | lwzux CARG1, RB, BASE
4172 | subfic TMP1, TMP1, -4
4174 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4175 | bne ->vmeta_tsets1
4177 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
4178 | lwz TMP0, TAB:RB->hmask
4179 | lwz TMP1, STR:RC->hash
4180 | lwz NODE:TMP2, TAB:RB->node
4181 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
4182 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
4183 | lfdx f14, BASE, RA
4184 | slwi TMP0, TMP1, 5
4185 | slwi TMP1, TMP1, 3
4186 | sub TMP1, TMP0, TMP1
4187 | lbz TMP3, TAB:RB->marked
4188 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4190 | lwz CARG1, NODE:TMP2->key
4191 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4192 | lwz CARG2, NODE:TMP2->val
4193 | lwz NODE:TMP1, NODE:TMP2->next
4194 | checkstr CARG1; bne >5
4195 | cmpw TMP0, STR:RC; bne >5
4196 | checknil CARG2; beq >4 // Key found, but nil value?
4198 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4199 | stfd f14, NODE:TMP2->val
4204 |4: // Check for __newindex if previous value is nil.
4205 | lwz TAB:TMP1, TAB:RB->metatable
4206 | cmplwi TAB:TMP1, 0
4207 | beq <2 // No metatable: done.
4208 | lbz TMP0, TAB:TMP1->nomm
4209 | andix. TMP0, TMP0, 1<<MM_newindex
4210 | bne <2 // 'no __newindex' flag set: done.
4213 |5: // Follow hash chain.
4214 | cmplwi NODE:TMP1, 0
4215 | mr NODE:TMP2, NODE:TMP1
4217 | // End of hash chain: key not found, add a new one.
4219 | // But check for __newindex first.
4220 | lwz TAB:TMP1, TAB:RB->metatable
4221 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
4224 | cmplwi TAB:TMP1, 0
4226 | beq >6 // No metatable: continue.
4227 | lbz TMP0, TAB:TMP1->nomm
4228 | andix. TMP0, TMP0, 1<<MM_newindex
4229 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4232 | stw STR:RC, 4(CARG3)
4234 | stw TMP0, 0(CARG3)
4235 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4236 | // Returns TValue *.
4238 | stfd f14, 0(CRET1)
4239 | b <3 // No 2nd write barrier needed.
4241 |7: // Possible table write barrier for the value. Skip valiswhite check.
4242 | barrierback TAB:RB, TMP3, TMP0
4246 | // RA = src*8, RB = table*8, RC = index*8
4247 | lwzux CARG1, RB, BASE
4250 | checktab CARG1; bne ->vmeta_tsetb
4251 | lwz TMP1, TAB:RB->asize
4252 | lwz TMP2, TAB:RB->array
4253 | lbz TMP3, TAB:RB->marked
4255 | lfdx f14, BASE, RA
4257 | lwzx TMP1, TMP2, RC
4258 | checknil TMP1; beq >5
4260 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4261 | stfdx f14, TMP2, RC
4266 |5: // Check for __newindex if previous value is nil.
4267 | lwz TAB:TMP1, TAB:RB->metatable
4268 | cmplwi TAB:TMP1, 0
4269 | beq <1 // No metatable: done.
4270 | lbz TMP1, TAB:TMP1->nomm
4271 | andix. TMP1, TMP1, 1<<MM_newindex
4272 | bne <1 // 'no __newindex' flag set: done.
4273 | b ->vmeta_tsetb // Caveat: preserve TMP0!
4275 |7: // Possible table write barrier for the value. Skip valiswhite check.
4276 | barrierback TAB:RB, TMP3, TMP0
4280 | // RA = dst*8, RB = table*8, RC = key*8
4282 | lwz TAB:CARG2, 4(RB)
4285 | lbz TMP3, TAB:CARG2->marked
4286 | lwz TMP0, TAB:CARG2->asize
4288 | lwz TMP1, TAB:CARG2->array
4291 | lbz TMP3, TAB:CARG2->marked
4292 | lwz TMP0, TAB:CARG2->asize
4294 | lwz TMP1, TAB:CARG2->array
4296 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4300 | slwi TMP2, CARG3, 3
4301 | lfdx f14, BASE, RA
4302 | ble ->vmeta_tsetr // In array part?
4304 | stfdx f14, TMP1, TMP2
4307 |7: // Possible table write barrier for the value. Skip valiswhite check.
4308 | barrierback TAB:CARG2, TMP3, TMP2
4314 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4317 | add TMP3, KBASE, RD
4318 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
4319 | addic. TMP0, MULTRES, -8
4320 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
4321 | srwi CARG3, TMP0, 3
4322 | beq >4 // Nothing to copy?
4323 | add CARG3, CARG3, TMP3
4324 | lwz TMP2, TAB:CARG2->asize
4325 | slwi TMP1, TMP3, 3
4326 | lbz TMP3, TAB:CARG2->marked
4328 | add TMP2, RA, TMP0
4329 | lwz TMP0, TAB:CARG2->array
4331 | add TMP1, TMP1, TMP0
4332 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4333 |3: // Copy result slots to table.
4336 | cmpw cr1, RA, TMP2
4338 | addi TMP1, TMP1, 8
4344 |5: // Need to resize array part.
4349 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4350 | // Must not reallocate the stack.
4354 |7: // Possible table write barrier for any value. Skip valiswhite check.
4355 | barrierback TAB:CARG2, TMP3, TMP0
4359 /* -- Calls and vararg handling ----------------------------------------- */
4362 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4363 | add NARGS8:RC, NARGS8:RC, MULTRES
4364 | // Fall through. Assumes BC_CALL follows.
4367 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4369 | lwzux TMP0, BASE, RA
4370 | lwz LFUNC:RB, 4(BASE)
4371 | subi NARGS8:RC, NARGS8:RC, 8
4372 | addi BASE, BASE, 8
4373 | checkfunc TMP0; bne ->vmeta_call
4378 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4379 | add NARGS8:RC, NARGS8:RC, MULTRES
4380 | // Fall through. Assumes BC_CALLT follows.
4383 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4384 | lwzux TMP0, RA, BASE
4385 | lwz LFUNC:RB, 4(RA)
4386 | subi NARGS8:RC, NARGS8:RC, 8
4387 | lwz TMP1, FRAME_PC(BASE)
4392 | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
4393 | lbz TMP3, LFUNC:RB->ffid
4394 | xori TMP2, TMP1, FRAME_VARG
4395 | cmplwi cr1, NARGS8:RC, 0
4398 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4400 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
4403 | addi TMP3, TMP2, 8
4405 | cmplw cr1, TMP3, NARGS8:RC
4406 | stfdx f0, BASE, TMP2
4410 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
4415 |5: // Tailcall to a fast function with a Lua frame below.
4417 | decode_RA8 RA, INS
4418 | sub TMP1, BASE, RA
4419 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
4420 | lwz TMP1, LFUNC:TMP1->pc
4421 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4424 |7: // Tailcall from a vararg function.
4425 | andix. TMP0, TMP2, FRAME_TYPEP
4426 | bne <1 // Vararg frame below?
4427 | sub BASE, BASE, TMP2 // Relocate BASE down.
4428 | lwz TMP1, FRAME_PC(BASE)
4429 | andix. TMP0, TMP1, FRAME_TYPE
4434 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4436 | add BASE, BASE, RA
4437 | lwz TMP1, -24(BASE)
4438 | lwz LFUNC:RB, -20(BASE)
4441 | stw TMP1, 0(BASE) // Copy callable.
4442 | stw LFUNC:RB, 4(BASE)
4444 | stfd f1, 16(BASE) // Copy control var.
4445 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4446 | stfdu f0, 8(BASE) // Copy state.
4452 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4454 | // NYI: add hotloop, record BC_ITERN.
4457 | lwz TAB:RB, -12(RA)
4458 | lwz RC, -4(RA) // Get index from control var.
4459 | lwz TMP0, TAB:RB->asize
4460 | lwz TMP1, TAB:RB->array
4462 |1: // Traverse array part.
4465 | bge >5 // Index points after array part?
4466 | lwzx TMP2, TMP1, TMP3
4467 | lfdx f0, TMP1, TMP3
4478 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
4480 | decode_RD4 TMP1, INS
4481 | stw RC, -4(RA) // Update control var.
4482 | add PC, TMP1, TMP3
4489 |4: // Skip holes in array part.
4493 |5: // Traverse hash part.
4494 | lwz TMP1, TAB:RB->hmask
4496 | lwz TMP2, TAB:RB->node
4498 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
4502 | sub TMP3, TMP3, RB
4503 | lwzx RB, TMP2, TMP3
4504 | lfdx f0, TMP2, TMP3
4505 | add NODE:TMP3, TMP2, TMP3
4509 | lfd f1, NODE:TMP3->key
4510 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
4513 | decode_RD4 TMP1, INS
4516 | add PC, TMP1, TMP2
4517 | stw RC, -4(RA) // Update control var.
4520 |7: // Skip holes in hash part.
4526 | // RA = base*8, RD = target (points to ITERN)
4529 | lwz CFUNC:TMP1, -20(RA)
4532 | cmpwi cr0, TMP2, LJ_TTAB
4533 | cmpwi cr1, TMP0, LJ_TFUNC
4534 | cmpwi cr6, TMP3, LJ_TNIL
4536 | lbz TMP1, CFUNC:TMP1->ffid
4537 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
4538 | cmpwi cr7, TMP1, FF_next_N
4540 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4541 | add TMP3, PC, TMP0
4544 | ori TMP1, TMP1, 0x7fff
4545 | stw ZERO, -4(RA) // Initialize control var.
4547 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4550 |5: // Despecialize bytecode if any of the checks fail.
4554 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4560 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4561 | lwz TMP0, FRAME_PC(BASE)
4564 | addi RC, RC, FRAME_VARG
4566 | subi TMP3, BASE, 8 // TMP3 = vtop
4567 | sub RC, RC, TMP0 // RC = vbase
4568 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4571 | sub TMP1, TMP3, RC
4574 | sub. TMP1, TMP3, RC
4576 | beq cr1, >5 // Copy all varargs?
4577 | subi TMP2, TMP2, 16
4578 | ble >2 // No vararg slots?
4579 |1: // Copy vararg slots to destination slots.
4584 | cmplw cr1, RC, TMP3
4585 | bge >3 // All destination slots filled?
4587 | blt cr1, <1 // More vararg slots?
4588 |2: // Fill up remainder with nil.
4596 |5: // Copy all varargs.
4597 | lwz TMP0, L->maxstack
4598 | li MULTRES, 8 // MULTRES = (0+1)*8
4599 | bley <3 // No vararg slots?
4600 | add TMP2, RA, TMP1
4602 | addi MULTRES, TMP1, 8
4610 | blt <6 // More vararg slots?
4613 |7: // Grow stack for varargs.
4616 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
4620 | srwi CARG2, TMP1, 3
4621 | bl extern lj_state_growstack // (lua_State *L, int n)
4624 | add RC, BASE, SAVE0
4625 | subi TMP3, BASE, 8
4629 /* -- Returns ----------------------------------------------------------- */
4632 | // RA = results*8, RD = extra_nresults*8
4633 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4634 | // Fall through. Assumes BC_RET follows.
4638 | // RA = results*8, RD = (nresults+1)*8
4639 | lwz PC, FRAME_PC(BASE)
4643 | andix. TMP0, PC, FRAME_TYPE
4644 | xori TMP1, PC, FRAME_VARG
4648 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4651 | subi TMP2, BASE, 8
4653 | decode_RB8 RB, INS
4657 | addi TMP3, TMP1, 8
4660 | stfdx f0, TMP2, TMP1
4662 | addi TMP1, TMP3, 8
4665 | stfdx f1, TMP2, TMP3
4670 | decode_RA8 RA, INS
4672 | sub BASE, TMP2, RA
4673 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4675 | lwz TMP1, LFUNC:TMP1->pc
4676 | lwz KBASE, PC2PROTO(k)(TMP1)
4679 |6: // Fill up results with nil.
4682 | stwx TISNIL, TMP2, TMP1
4685 |->BC_RETV_Z: // Non-standard return case.
4686 | andix. TMP2, TMP1, FRAME_TYPEP
4688 | // Return from vararg function: relocate BASE down.
4689 | sub BASE, BASE, TMP1
4690 | lwz PC, FRAME_PC(BASE)
4694 case BC_RET0: case BC_RET1:
4695 | // RA = results*8, RD = (nresults+1)*8
4696 | lwz PC, FRAME_PC(BASE)
4699 | andix. TMP0, PC, FRAME_TYPE
4700 | xori TMP1, PC, FRAME_VARG
4704 | subi TMP2, BASE, 8
4705 | decode_RB8 RB, INS
4706 if (op == BC_RET1) {
4712 | decode_RA8 RA, INS
4714 | sub BASE, TMP2, RA
4715 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4717 | lwz TMP1, LFUNC:TMP1->pc
4718 | lwz KBASE, PC2PROTO(k)(TMP1)
4721 |6: // Fill up results with nil.
4724 | stwx TISNIL, TMP2, TMP1
4728 /* -- Loops and branches ------------------------------------------------ */
4734 | // Fall through. Assumes BC_IFORL follows.
4744 | // RA = base*8, RD = target (after end of loop or start of loop)
4745 vk = (op == BC_IFORL || op == BC_JFORL);
4748 | lwzux TMP1, RA, BASE
4749 | lwz CARG1, FORL_IDX*8+4(RA)
4750 | cmplw cr0, TMP1, TISNUM
4752 | lwz CARG3, FORL_STEP*8+4(RA)
4755 | // Need to check overflow for (a<<32) + (b<<32).
4756 | rldicr TMP0, CARG1, 32, 31
4757 | rldicr TMP2, CARG3, 32, 31
4758 | add CARG1, CARG1, CARG3
4759 | addo. TMP0, TMP0, TMP2
4761 | addo. CARG1, CARG1, CARG3
4763 | cmpwi cr6, CARG3, 0
4764 | lwz CARG2, FORL_STOP*8+4(RA)
4767 | stw CARG1, FORL_IDX*8+4(RA)
4769 | lwz TMP3, FORL_STEP*8(RA)
4770 | lwz CARG3, FORL_STEP*8+4(RA)
4771 | lwz TMP2, FORL_STOP*8(RA)
4772 | lwz CARG2, FORL_STOP*8+4(RA)
4773 | cmplw cr7, TMP3, TISNUM
4774 | cmplw cr1, TMP2, TISNUM
4775 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4776 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4777 | cmpwi cr6, CARG3, 0
4783 | stw TISNUM, FORL_EXT*8(RA)
4784 if (op != BC_JFORL) {
4787 | stw CARG1, FORL_EXT*8+4(RA)
4788 if (op != BC_JFORL) {
4791 if (op == BC_FORI) {
4792 | bgt >3 // See FP loop below.
4793 } else if (op == BC_JFORI) {
4794 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4796 } else if (op == BC_IFORL) {
4798 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4804 |5: // Invert check for negative step.
4808 |6: // Potential overflow.
4809 | checkov TMP0, <4 // Ignore unrelated overflow.
4816 | lfd f1, FORL_IDX*8(RA)
4818 | lfdux f1, RA, BASE
4820 | lfd f3, FORL_STEP*8(RA)
4821 | lfd f2, FORL_STOP*8(RA)
4822 | lwz TMP3, FORL_STEP*8(RA)
4824 | stfd f1, FORL_IDX*8(RA)
4829 | lwzux TMP1, RA, BASE
4830 | lwz TMP3, FORL_STEP*8(RA)
4831 | lwz TMP2, FORL_STOP*8(RA)
4832 | cmplw cr0, TMP1, TISNUM
4833 | cmplw cr7, TMP3, TISNUM
4834 | cmplw cr1, TMP2, TISNUM
4836 | lfd f1, FORL_IDX*8(RA)
4837 | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
4838 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4839 | lfd f2, FORL_STOP*8(RA)
4842 | cmpwi cr6, TMP3, 0
4843 if (op != BC_JFORL) {
4846 | stfd f1, FORL_EXT*8(RA)
4847 if (op != BC_JFORL) {
4851 if (op == BC_JFORI) {
4852 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4855 if (op == BC_FORI) {
4857 } else if (op == BC_IFORL) {
4864 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4865 } else if (op == BC_JFORI) {
4876 |5: // Negative step.
4877 if (op == BC_FORI) {
4879 |3: // Used by integer loop, too.
4880 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4881 } else if (op == BC_IFORL) {
4883 } else if (op == BC_JFORI) {
4889 if (op == BC_JFORI) {
4892 | decode_RD8 RD, INS
4901 | // Fall through. Assumes BC_IITERL follows.
4909 | // RA = base*8, RD = target
4910 | lwzux TMP1, RA, BASE
4912 | checknil TMP1; beq >1 // Stop if iterator returned nil.
4913 if (op == BC_JITERL) {
4918 | branch_RD // Otherwise save control var + branch.
4927 | // RA = base*8, RD = target (loop extent)
4928 | // Note: RA/RD is only used by trace recorder to determine scope/extent
4929 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4933 | // Fall through. Assumes BC_ILOOP follows.
4937 | // RA = base*8, RD = target (loop extent)
4943 | // RA = base*8 (ignored), RD = traceno*8
4944 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
4946 | // Traces on PPC don't store the trace number, so use 0.
4947 | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH)
4948 | lwzx TRACE:TMP2, TMP1, RD
4950 | lp TMP2, TRACE:TMP2->mcode
4951 | stw BASE, DISPATCH_GL(jit_base)(DISPATCH)
4953 | addi JGL, DISPATCH, GG_DISP2G+32768
4954 | stw L, DISPATCH_GL(tmpbuf.L)(DISPATCH)
4960 | // RA = base*8 (only used by trace recorder), RD = target
4965 /* -- Function headers -------------------------------------------------- */
4971 case BC_FUNCV: /* NYI: compiled vararg functions. */
4972 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4980 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4981 | lwz TMP2, L->maxstack
4982 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
4983 | lwz KBASE, -4+PC2PROTO(k)(PC)
4985 | slwi TMP1, TMP1, 3
4986 | bgt ->vm_growstack_l
4987 if (op != BC_JFUNCF) {
4991 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
4993 if (op == BC_JFUNCF) {
4994 | decode_RD8 RD, INS
5000 |3: // Clear missing parameters.
5001 | stwx TISNIL, BASE, NARGS8:RC
5002 | addi NARGS8:RC, NARGS8:RC, 8
5010 | NYI // NYI: compiled vararg functions
5011 break; /* NYI: compiled vararg functions. */
5014 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5015 | lwz TMP2, L->maxstack
5016 | add TMP1, BASE, RC
5018 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
5019 | addi TMP3, RC, 8+FRAME_VARG
5020 | lwz KBASE, -4+PC2PROTO(k)(PC)
5022 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
5023 | bge ->vm_growstack_l
5024 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
5029 | addi BASE, TMP1, 8
5032 | cmplw RA, RC // Less args than parameters?
5036 | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
5039 | addic. TMP2, TMP2, -1
5041 | stw TMP3, 12(TMP1)
5042 | addi TMP1, TMP1, 8
5047 |4: // Clear missing parameters.
5054 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
5055 if (op == BC_FUNCC) {
5056 | lp RD, CFUNC:RB->f
5058 | lp RD, DISPATCH_GL(wrapf)(DISPATCH)
5060 | add TMP1, RA, NARGS8:RC
5061 | lwz TMP2, L->maxstack
5062 | .toc lp TMP3, 0(RD)
5063 | add RC, BASE, NARGS8:RC
5073 if (op == BC_FUNCCW) {
5074 | lp CARG2, CFUNC:RB->f
5077 | bgt ->vm_growstack_c // Need to grow stack.
5078 | .toc lp TOCREG, TOC_OFS(RD)
5079 | .tocenv lp ENVREG, ENV_OFS(RD)
5081 | bctrl // (lua_State *L [, lua_CFunction f])
5082 | // Returns nresults.
5084 | .toc ld TOCREG, SAVE_TOC
5088 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
5089 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
5090 | sub RA, TMP1, RD // RA = L->top - nresults*8
5095 /* ---------------------------------------------------------------------- */
5098 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5104 static int build_backend(BuildCtx *ctx)
5108 dasm_growpc(Dst, BC__MAX);
5110 build_subroutines(ctx);
5113 for (op = 0; op < BC__MAX; op++)
5114 build_ins(ctx, (BCOp)op, op);
5119 /* Emit pseudo frame-info for all assembler functions. */
5120 static void emit_asm_debug(BuildCtx *ctx)
5122 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5124 switch (ctx->mode) {
5126 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5129 "\t.long .LECIE0-.LSCIE0\n"
5131 "\t.long 0xffffffff\n"
5137 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5142 "\t.long .LEFDE0-.LASFDE0\n"
5144 "\t.long .Lframe0\n"
5147 "\t.byte 0xe\n\t.uleb128 %d\n"
5148 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5149 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5150 fcofs, CFRAME_SIZE);
5151 for (i = 14; i <= 31; i++)
5153 "\t.byte %d\n\t.uleb128 %d\n"
5154 "\t.byte %d\n\t.uleb128 %d\n",
5155 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5162 "\t.long .LEFDE1-.LASFDE1\n"
5164 "\t.long .Lframe0\n"
5166 "\t.long .lj_vm_ffi_call\n"
5168 "\t.long lj_vm_ffi_call\n"
5171 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5172 "\t.byte 0x8e\n\t.uleb128 2\n"
5173 "\t.byte 0xd\n\t.uleb128 0xe\n"
5175 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5178 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5181 "\t.long .LECIE1-.LSCIE1\n"
5185 "\t.string \"zPR\"\n"
5189 "\t.uleb128 6\n" /* augmentation length */
5190 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5191 "\t.long lj_err_unwind_dwarf-.\n"
5192 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5193 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5198 "\t.long .LEFDE2-.LASFDE2\n"
5200 "\t.long .LASFDE2-.Lframe1\n"
5201 "\t.long .Lbegin-.\n"
5203 "\t.uleb128 0\n" /* augmentation length */
5204 "\t.byte 0xe\n\t.uleb128 %d\n"
5205 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5206 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5207 fcofs, CFRAME_SIZE);
5208 for (i = 14; i <= 31; i++)
5210 "\t.byte %d\n\t.uleb128 %d\n"
5211 "\t.byte %d\n\t.uleb128 %d\n",
5212 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5219 "\t.long .LECIE2-.LSCIE2\n"
5223 "\t.string \"zR\"\n"
5227 "\t.uleb128 1\n" /* augmentation length */
5228 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5229 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5234 "\t.long .LEFDE3-.LASFDE3\n"
5236 "\t.long .LASFDE3-.Lframe2\n"
5237 "\t.long lj_vm_ffi_call-.\n"
5239 "\t.uleb128 0\n" /* augmentation length */
5240 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5241 "\t.byte 0x8e\n\t.uleb128 2\n"
5242 "\t.byte 0xd\n\t.uleb128 0xe\n"
5244 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);