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-2023 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 |// GPR64 64 bit registers (but possibly 32 bit pointers, e.g. PS3).
22 |// Affects reg saves, stack layout, carry/overflow/dot flags etc.
23 |// FRAME32 Use 32 bit frame layout, even with GPR64 (Xbox 360).
24 |// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3).
25 |// Function pointers are really a struct: code, TOC, env (optional).
26 |// TOCENV Function pointers have an environment pointer, too (not on PS3).
27 |// PPE Power Processor Element of Cell (PS3) or Xenon (Xbox 360).
28 |// Must avoid (slow) micro-coded instructions.
33 |.macro lpx, a, b, c; ldx a, b, c; .endmacro
34 |.macro lp, a, b; ld a, b; .endmacro
35 |.macro stp, a, b; std a, b; .endmacro
36 |.define decode_OPP, decode_OP8
38 |// Missing: Calling conventions, 64 bit regs, TOC.
39 |.error lib_ffi not yet implemented for PPC64
42 |.macro lpx, a, b, c; lwzx a, b, c; .endmacro
43 |.macro lp, a, b; lwz a, b; .endmacro
44 |.macro stp, a, b; stw a, b; .endmacro
45 |.define decode_OPP, decode_OP4
48 |// Convenience macros for TOC handling.
50 |// Linker needs a TOC patch area for every external call relocation.
51 |.macro blex, target; bl extern target@plt; nop; .endmacro
52 |.macro .toc, a, b; a, b; .endmacro
61 |.macro blex, target; bl extern target@plt; .endmacro
62 |.macro .toc, a, b; .endmacro
64 |.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro
66 |.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro
68 |.macro andix., y, a, i
70 | rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i)
86 |.macro checkov, reg, noov
100 |//-----------------------------------------------------------------------
102 |// Fixed register assignments for the interpreter.
103 |// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA)
111 |.macro .FPU, a, b, c
117 |// The following must be C callee-save (but BASE is often refetched).
118 |.define BASE, r14 // Base of current Lua stack frame.
119 |.define KBASE, r15 // Constants of current Lua function.
120 |.define PC, r16 // Next PC.
121 |.define DISPATCH, r17 // Opcode dispatch table.
122 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
123 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
124 |.define JGL, r31 // On-trace: global_State + 32768.
126 |// Constants for type-comparisons, stores and conversions. C callee-save.
131 |.define TOBIT, f30 // 2^52 + 2^51.
132 |.define TONUM, f31 // 2^52 + 2^51 + 2^31.
135 |// The following temporaries are not saved across C calls, except for RA.
136 |.define RA, r20 // Callee-save.
140 |.define INS, r7 // Overlaps CARG5.
145 |.define TMP3, r6 // Overlaps CARG4.
147 |// Saved temporaries.
151 |// Calling conventions.
155 |.define CARG4, r6 // Overlaps TMP3.
156 |.define CARG5, r7 // Overlaps INS.
166 |.define TOCREG, r2 // TOC register (only used by C code).
167 |.define ENVREG, r11 // Environment pointer (nested C functions).
169 |// Stack layout while in interpreter. Must match with lj_frame.h.
173 |// 456(sp) // \ 32/64 bit C frame info
174 |.define TONUM_LO, 452(sp) // |
175 |.define TONUM_HI, 448(sp) // |
176 |.define TMPD_LO, 444(sp) // |
177 |.define TMPD_HI, 440(sp) // |
178 |.define SAVE_CR, 432(sp) // | 64 bit CR save.
179 |.define SAVE_ERRF, 424(sp) // > Parameter save area.
180 |.define SAVE_NRES, 420(sp) // |
181 |.define SAVE_L, 416(sp) // |
182 |.define SAVE_PC, 412(sp) // |
183 |.define SAVE_MULTRES, 408(sp) // |
184 |.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain.
185 |// 392(sp) // Reserved.
186 |.define CFRAME_SPACE, 384 // Delta for sp.
187 |// Back chain for sp: 384(sp) <-- sp entering interpreter
188 |.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part.
189 |.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves.
190 |.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves.
191 |// 80(sp) // Needed for 16 byte stack frame alignment.
192 |// 16(sp) // Callee parameter save area (ABI mandated).
193 |// 8(sp) // Reserved
194 |// Back chain for sp: 0(sp) <-- sp while in interpreter
195 |// 32 bit sp stored in hi-part of 0(sp).
197 |.define TMPD_BLO, 447(sp)
198 |.define TMPD, TMPD_HI
199 |.define TONUM_D, TONUM_HI
203 |// 508(sp) // \ 32 bit C frame info.
204 |.define SAVE_ERRF, 472(sp) // |
205 |.define SAVE_NRES, 468(sp) // |
206 |.define SAVE_L, 464(sp) // > Parameter save area.
207 |.define SAVE_PC, 460(sp) // |
208 |.define SAVE_MULTRES, 456(sp) // |
209 |.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain.
210 |.define SAVE_LR, 416(sp)
211 |.define CFRAME_SPACE, 400 // Delta for sp.
212 |// Back chain for sp: 400(sp) <-- sp entering interpreter
213 |.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves.
214 |.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves.
215 |// 48(sp) // Callee parameter save area (ABI mandated).
216 |.define SAVE_TOC, 40(sp) // TOC save area.
217 |.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated).
218 |.define TMPD_HI, 32(sp) // /
219 |.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated).
220 |.define TONUM_HI, 24(sp) // /
221 |// Next frame lr: 16(sp)
222 |.define SAVE_CR, 8(sp) // 64 bit CR save.
223 |// Back chain for sp: 0(sp) <-- sp while in interpreter
225 |.define TMPD_BLO, 39(sp)
226 |.define TMPD, TMPD_HI
227 |.define TONUM_D, TONUM_HI
233 |.define SAVE_LR, 276(sp)
234 |.define CFRAME_SPACE, 272 // Delta for sp.
235 |// Back chain for sp: 272(sp) <-- sp entering interpreter
236 |.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves.
238 |.define SAVE_LR, 132(sp)
239 |.define CFRAME_SPACE, 128 // Delta for sp.
240 |// Back chain for sp: 128(sp) <-- sp entering interpreter
242 |.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves.
243 |.define SAVE_CR, 52(sp) // 32 bit CR save.
244 |.define SAVE_ERRF, 48(sp) // 32 bit C frame info.
245 |.define SAVE_NRES, 44(sp)
246 |.define SAVE_CFRAME, 40(sp)
247 |.define SAVE_L, 36(sp)
248 |.define SAVE_PC, 32(sp)
249 |.define SAVE_MULTRES, 28(sp)
250 |.define UNUSED1, 24(sp)
252 |.define TMPD_LO, 20(sp)
253 |.define TMPD_HI, 16(sp)
254 |.define TONUM_LO, 12(sp)
255 |.define TONUM_HI, 8(sp)
257 |.define SFSAVE_4, 20(sp)
258 |.define SFSAVE_3, 16(sp)
259 |.define SFSAVE_2, 12(sp)
260 |.define SFSAVE_1, 8(sp)
262 |// Next frame lr: 4(sp)
263 |// Back chain for sp: 0(sp) <-- sp while in interpreter
266 |.define TMPD_BLO, 23(sp)
267 |.define TMPD, TMPD_HI
268 |.define TONUM_D, TONUM_HI
275 | std r..reg, SAVE_GPR_+(reg-14)*8(sp)
277 | stw r..reg, SAVE_GPR_+(reg-14)*4(sp)
279 | .FPU stfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
283 | ld r..reg, SAVE_GPR_+(reg-14)*8(sp)
285 | lwz r..reg, SAVE_GPR_+(reg-14)*4(sp)
287 | .FPU lfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
291 |.if GPR64 and not FRAME32
292 | stdu sp, -CFRAME_SPACE(sp)
294 | stwu sp, -CFRAME_SPACE(sp)
296 | save_ 14; save_ 15; save_ 16
298 | save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22
299 |.if GPR64 and not FRAME32
304 | save_ 23; save_ 24; save_ 25
306 | save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31
312 | .toc std TOCREG, SAVE_TOC
316 |.if GPR64 and not FRAME32
326 | rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19
328 |.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif
329 | rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25
330 |.if PPE; mtocrf 0x10, r12; .endif
331 | rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31
332 |.if PPE; mtocrf 0x08, r12; .endif
333 | addi sp, sp, CFRAME_SPACE
336 |// Type definitions. Some of these are only used for documentation.
337 |.type L, lua_State, LREG
338 |.type GL, global_State
339 |.type TVALUE, TValue
343 |.type LFUNC, GCfuncL
344 |.type CFUNC, GCfuncC
345 |.type PROTO, GCproto
346 |.type UPVAL, GCupval
349 |.type TRACE, GCtrace
352 |//-----------------------------------------------------------------------
354 |// Trap for not-yet-implemented parts.
355 |.macro NYI; tw 4, sp, sp; .endmacro
358 |// int/FP conversions.
359 |.macro tonum_i, freg, reg
360 | xoris reg, reg, 0x8000
363 | fsub freg, freg, TONUM
366 |.macro tonum_u, freg, reg
369 | fsub freg, freg, TOBIT
372 |.macro toint, reg, freg, tmpfreg
373 | fctiwz tmpfreg, freg
378 |.macro toint, reg, freg
379 | toint reg, freg, freg
383 |//-----------------------------------------------------------------------
385 |// Access to frame relative to BASE.
386 |.define FRAME_PC, -8
387 |.define FRAME_FUNC, -4
389 |// Instruction decode.
390 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
391 |.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro
392 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
393 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
394 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
395 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
397 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
398 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
400 |// Instruction fetch.
405 |// Instruction decode+dispatch. Note: optimized for e300!
407 | decode_OPP TMP1, INS
408 | lpx TMP0, DISPATCH, TMP1
421 |// Instruction footer.
423 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
424 | .define ins_next, ins_NEXT
425 | .define ins_next_, ins_NEXT
426 | .define ins_next1, ins_NEXT1
427 | .define ins_next2, ins_NEXT2
429 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
430 | // Affects only certain kinds of benchmarks (and only with -j off).
445 |// Call decode and dispatch.
447 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
448 | lwz PC, LFUNC:RB->pc
451 | decode_OPP TMP1, INS
453 | lpx TMP0, DISPATCH, TMP1
460 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
461 | stw PC, FRAME_PC(BASE)
465 |//-----------------------------------------------------------------------
467 |// Macros to test operand types.
468 |.macro checknum, reg; cmplw reg, TISNUM; .endmacro
469 |.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro
470 |.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro
471 |.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro
472 |.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro
473 |.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro
477 | addis PC, PC, -(BCBIAS_J*4 >> 16)
481 |// Assumes DISPATCH is relative to GL.
482 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
483 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
485 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
487 |.macro hotcheck, delta, target
488 | rlwinm TMP1, PC, 31, 25, 30
489 | addi TMP1, TMP1, GG_DISP2HOT
490 | lhzx TMP2, DISPATCH, TMP1
491 | addic. TMP2, TMP2, -delta
492 | sthx TMP2, DISPATCH, TMP1
497 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
501 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
504 |// Set current VM state. Uses TMP0.
505 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
506 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
508 |// Move table write barrier back. Overwrites mark and tmp.
509 |.macro barrierback, tab, mark, tmp
510 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
511 | // Assumes LJ_GC_BLACK is 0x04.
512 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
513 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
514 | stb mark, tab->marked
515 | stw tmp, tab->gclist
518 |//-----------------------------------------------------------------------
520 /* Generate subroutines used by opcodes and other parts of the VM. */
521 /* The .code_sub section should be last to help static branch prediction. */
522 static void build_subroutines(BuildCtx *ctx)
526 |//-----------------------------------------------------------------------
527 |//-- Return handling ----------------------------------------------------
528 |//-----------------------------------------------------------------------
531 | // See vm_return. Also: TMP2 = previous base.
532 | andix. TMP0, PC, FRAME_P
534 | beq ->cont_dispatch
536 | // Return from pcall or xpcall fast func.
537 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
538 | mr BASE, TMP2 // Restore caller base.
539 | // Prepending may overwrite the pcall frame, so do it at the end.
540 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
543 | addi RD, RD, 8 // RD = (nresults+1)*8.
544 | andix. TMP0, PC, FRAME_TYPE
546 | li CRET1, LUA_YIELD
547 | beq cr1, ->vm_unwind_c_eh
549 | beq ->BC_RET_Z // Handle regular return to Lua.
552 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
553 | // TMP0 = PC & FRAME_TYPE
554 | cmpwi TMP0, FRAME_C
555 | rlwinm TMP2, PC, 0, 0, 28
557 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
560 | addic. TMP1, RD, -8
562 | lwz TMP2, SAVE_NRES
568 | addic. TMP1, TMP1, -8
586 | cmpw TMP2, RD // More/less results wanted?
589 | stp BASE, L->top // Store new top.
592 | lp TMP0, SAVE_CFRAME // Restore previous C frame.
593 | li CRET1, 0 // Ok return status for vm_pcall.
594 | stp TMP0, L->cframe
601 | ble >7 // Less results wanted?
602 | // More results wanted. Check stack size and fill up results with nil.
603 | lwz TMP1, L->maxstack
606 | stw TISNIL, 0(BASE)
611 |7: // Less results wanted.
612 | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case?
614 | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1
615 | and TMP0, TMP0, TMP1
616 | sub BASE, BASE, TMP0 // Either keep top or shrink it.
619 |8: // Corner case: need to grow stack for filling up results.
620 | // This can happen if:
621 | // - A C function grows the stack (a lot).
622 | // - The GC shrinks the stack in between.
623 | // - A return back from a lua_call() with (high) nresults adjustment.
624 | stp BASE, L->top // Save current top held in BASE (yes).
626 | srwi CARG2, TMP2, 3
628 | bl extern lj_state_growstack // (lua_State *L, int n)
629 | lwz TMP2, SAVE_NRES
632 | lp BASE, L->top // Need the (realloced) L->top in BASE.
635 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
636 | // (void *cframe, int errcode)
639 |->vm_unwind_c_eh: // Landing pad for external unwinder.
641 | .toc ld TOCREG, SAVE_TOC
642 | li TMP0, ~LJ_VMST_C
643 | lwz GL:TMP1, L->glref
644 | stw TMP0, GL:TMP1->vmstate
647 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
650 | rldicr sp, CARG1, 0, 61
652 | rlwinm sp, CARG1, 0, 0, 29
654 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
656 | .toc ld TOCREG, SAVE_TOC
657 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
659 | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
660 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
662 | .FPU stw TMP3, TMPD
664 | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
667 | .FPU lfs TOBIT, TMPD
668 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
669 | la RA, -8(BASE) // Results start at BASE-8.
670 | .FPU stw TMP3, TMPD
671 | addi DISPATCH, DISPATCH, GG_G2DISP
672 | stw TMP1, 0(RA) // Prepend false to error message.
673 | li RD, 16 // 2 results: false + error message.
675 | .FPU lfs TONUM, TMPD
678 |//-----------------------------------------------------------------------
679 |//-- Grow stack for calls -----------------------------------------------
680 |//-----------------------------------------------------------------------
682 |->vm_growstack_c: // Grow stack for C function.
683 | li CARG2, LUA_MINSTACK
686 |->vm_growstack_l: // Grow stack for Lua function.
687 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
691 | addi PC, PC, 4 // Must point after first instruction.
695 | // L->base = new base, L->top = top
698 | bl extern lj_state_growstack // (lua_State *L, int n)
701 | lwz LFUNC:RB, FRAME_FUNC(BASE)
703 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
704 | ins_callt // Just retry the call.
706 |//-----------------------------------------------------------------------
707 |//-- Entry points into the assembler VM ---------------------------------
708 |//-----------------------------------------------------------------------
710 |->vm_resume: // Setup C frame and resume thread.
711 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
714 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
716 | lbz TMP1, L->status
719 | addi TMP0, sp, CFRAME_RESUME
720 | addi DISPATCH, DISPATCH, GG_G2DISP
721 | stw CARG3, SAVE_NRES
723 | stw CARG3, SAVE_ERRF
724 | stp CARG3, SAVE_CFRAME
725 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
726 | stp TMP0, L->cframe
729 | // Resume after yield (like a return).
730 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
733 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
735 | lwz PC, FRAME_PC(BASE)
736 | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
737 | stb CARG3, L->status
738 | .FPU stw TMP3, TMPD
739 | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
740 | .FPU lfs TOBIT, TMPD
742 | .FPU stw TMP3, TMPD
743 | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
745 | .FPU stw TMP0, TONUM_HI
749 | andix. TMP0, PC, FRAME_TYPE
751 | .FPU lfs TONUM, TMPD
756 |->vm_pcall: // Setup protected C frame and enter VM.
757 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
760 | stw CARG4, SAVE_ERRF
763 |->vm_call: // Setup C frame and enter VM.
764 | // (lua_State *L, TValue *base, int nres1)
768 |1: // Entry point for vm_pcall above (PC = ftype).
769 | lp TMP1, L:CARG1->cframe
771 | stw CARG3, SAVE_NRES
772 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
775 | addi DISPATCH, DISPATCH, GG_G2DISP
776 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
777 | stp TMP1, SAVE_CFRAME
778 | stp sp, L->cframe // Add our C frame to cframe chain.
780 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
781 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
782 | lp TMP2, L->base // TMP2 = old base (used in vmeta_call).
783 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
785 | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
787 | .FPU stw TMP3, TMPD
789 | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
790 | .FPU lfs TOBIT, TMPD
791 | sub PC, PC, TMP2 // PC = frame delta + frame type
792 | .FPU stw TMP3, TMPD
793 | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
794 | sub NARGS8:RC, TMP1, BASE
795 | .FPU stw TMP0, TONUM_HI
797 | .FPU lfs TONUM, TMPD
802 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
803 | lwz TMP0, FRAME_PC(BASE)
804 | lwz LFUNC:RB, FRAME_FUNC(BASE)
805 | checkfunc TMP0; bne ->vmeta_call
807 |->vm_call_dispatch_f:
809 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
811 |->vm_cpcall: // Setup protected C frame, call C.
812 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
815 | lwz TMP0, L:CARG1->stack
818 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
819 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
820 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
822 | addi DISPATCH, DISPATCH, GG_G2DISP
823 | .toc lp CARG4, 0(CARG4)
825 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
826 | stw TMP2, SAVE_ERRF // No error function.
827 | stp TMP1, SAVE_CFRAME
828 | stp sp, L->cframe // Add our C frame to cframe chain.
829 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
831 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
839 | bne <3 // Else continue with the call.
840 | b ->vm_leave_cp // No base? Just remove C frame.
842 |//-----------------------------------------------------------------------
843 |//-- Metamethod handling ------------------------------------------------
844 |//-----------------------------------------------------------------------
846 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
847 |// stack, so BASE doesn't need to be reloaded across these calls.
849 |//-- Continuation dispatch ----------------------------------------------
852 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
853 | lwz TMP0, -12(BASE) // Continuation.
855 | mr BASE, TMP2 // Restore caller BASE.
856 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
860 | lwz PC, -16(RB) // Restore PC from [cont|PC].
862 | stwx TISNIL, RA, TMP2 // Ensure one valid arg.
866 | lwz TMP1, LFUNC:TMP1->pc
867 | lwz KBASE, PC2PROTO(k)(TMP1)
868 | // BASE = base, RA = resultptr, RB = meta base
870 | bctr // Jump to continuation.
874 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
875 | // cont = 0: tailcall from C function.
881 |->cont_cat: // RA = resultptr, RB = meta base
884 | decode_RB8 SAVE0, INS
891 | add TMP1, BASE, SAVE0
894 | sub CARG3, CARG2, TMP1
906 | stwux TMP2, RA, BASE
911 |//-- Table indexing metamethods -----------------------------------------
914 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
917 | stw STR:RC, 4(CARG3)
918 | add CARG2, BASE, RB
923 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
925 | stw TAB:RB, 4(CARG2)
926 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
929 | stw STR:RC, 4(CARG3)
933 |->vmeta_tgetb: // TMP0 = index
938 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
939 | add CARG2, BASE, RB
941 | stw TISNUM, 0(CARG3)
951 | add CARG2, BASE, RB
952 | add CARG3, BASE, RC
957 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
958 | // Returns TValue * (finished) or NULL (metamethod).
971 | stwux TMP0, RA, BASE
976 |3: // Call __index metamethod.
977 | // BASE = base, L->top = new base, stack = cont/func/t/k
978 | subfic TMP1, BASE, FRAME_CONT
980 | stw PC, -16(BASE) // [cont|PC]
982 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
983 | li NARGS8:RC, 16 // 2 args for func(t, k).
984 | b ->vm_call_dispatch_f
987 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
988 | // Returns cTValue * or NULL.
994 | lwz SAVE0, 0(CRET1)
995 | lwz SAVE1, 4(CRET1)
999 | stwx TISNIL, BASE, RA
1002 |//-----------------------------------------------------------------------
1005 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
1007 | decode_RB8 RB, INS
1008 | stw STR:RC, 4(CARG3)
1009 | add CARG2, BASE, RB
1010 | stw TMP0, 0(CARG3)
1014 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1016 | stw TAB:RB, 4(CARG2)
1017 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
1018 | stw TMP0, 0(CARG2)
1020 | stw STR:RC, 4(CARG3)
1021 | stw TMP1, 0(CARG3)
1024 |->vmeta_tsetb: // TMP0 = index
1028 | decode_RB8 RB, INS
1029 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
1030 | add CARG2, BASE, RB
1032 | stw TISNUM, 0(CARG3)
1033 | stw TMP0, 4(CARG3)
1040 | decode_RB8 RB, INS
1041 | decode_RC8 RC, INS
1042 | add CARG2, BASE, RB
1043 | add CARG3, BASE, RC
1048 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
1049 | // Returns TValue * (finished) or NULL (metamethod).
1054 | lwzux TMP2, RA, BASE
1058 | // NOBARRIER: lj_meta_tset ensures the table is not black.
1063 | stw TMP2, 0(CRET1)
1064 | stw TMP3, 4(CRET1)
1068 |3: // Call __newindex metamethod.
1069 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
1070 | subfic TMP1, BASE, FRAME_CONT
1072 | stw PC, -16(BASE) // [cont|PC]
1073 | add PC, TMP1, BASE
1074 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1075 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
1077 | stfd f0, 16(BASE) // Copy value to third argument.
1079 | stw TMP2, 16(BASE)
1080 | stw TMP3, 20(BASE)
1082 | b ->vm_call_dispatch_f
1088 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
1089 | // Returns TValue *.
1091 | stfd f14, 0(CRET1)
1093 | stw SAVE0, 0(CRET1)
1094 | stw SAVE1, 4(CRET1)
1098 |//-- Comparison metamethods ---------------------------------------------
1106 | add CARG2, BASE, RA
1112 | add CARG3, BASE, RD
1115 | decode_OP1 CARG4, INS
1116 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1117 | // Returns 0/1 or TValue * (metamethod).
1121 | subfic CRET1, CRET1, 0
1125 | decode_RD4 TMP2, INS
1126 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
1127 | and TMP2, TMP2, CRET1
1132 |->cont_ra: // RA = resultptr
1140 | decode_RA8 TMP1, INS
1142 | stfdx f0, BASE, TMP1
1144 | stwux CARG1, TMP1, BASE
1145 | stw CARG2, 4(TMP1)
1149 |->cont_condt: // RA = resultptr
1151 | .gpr64 extsw TMP0, TMP0
1152 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true.
1153 | subfe CRET1, CRET1, CRET1
1157 |->cont_condf: // RA = resultptr
1159 | .gpr64 extsw TMP0, TMP0
1160 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false.
1161 | subfe CRET1, CRET1, CRET1
1165 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
1170 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1171 | // Returns 0/1 or TValue * (metamethod).
1181 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
1182 | // Returns 0/1 or TValue * (metamethod).
1193 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
1196 |//-- Arithmetic metamethods ---------------------------------------------
1199 | add CARG3, KBASE, RC
1200 | add CARG4, BASE, RB
1215 | add CARG3, BASE, RB
1216 | add CARG4, KBASE, RC
1220 | add CARG3, BASE, RB
1221 | add CARG4, BASE, RC
1232 | add CARG2, BASE, RA
1236 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
1237 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1238 | // Returns NULL (finished) or TValue * (metamethod).
1242 | // Call metamethod for binary op.
1244 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1245 | sub TMP1, CRET1, BASE
1246 | stw PC, -16(CRET1) // [cont|PC]
1248 | addi PC, TMP1, FRAME_CONT
1250 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
1251 | b ->vm_call_dispatch
1261 | bl extern lj_meta_len // (lua_State *L, TValue *o)
1262 | // Returns NULL (retry) or TValue * (metamethod base).
1265 | bne ->vmeta_binop // Binop call for compatibility.
1269 | b ->vmeta_binop // Binop call for compatibility.
1272 |//-- Call metamethod ----------------------------------------------------
1274 |->vmeta_call: // Resolve and call __call metamethod.
1275 | // TMP2 = old base, BASE = new base, RC = nargs*8
1277 | stp TMP2, L->base // This is the callers base!
1278 | subi CARG2, BASE, 8
1280 | add CARG3, BASE, RC
1281 | mr SAVE0, NARGS8:RC
1282 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1283 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1284 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1287 |->vmeta_callt: // Resolve __call for BC_CALLT.
1288 | // BASE = old base, RA = new base, RC = nargs*8
1294 | mr SAVE0, NARGS8:RC
1295 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1296 | lwz TMP1, FRAME_PC(BASE)
1297 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1298 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1301 |//-- Argument coercion for 'for' statement ------------------------------
1309 | bl extern lj_meta_for // (lua_State *L, TValue *base)
1311 | decode_OP1 TMP0, SAVE0
1313 | decode_RA8 RA, SAVE0
1315 | cmpwi TMP0, BC_JFORI
1317 | decode_RD8 RD, SAVE0
1323 |//-----------------------------------------------------------------------
1324 |//-- Fast functions -----------------------------------------------------
1325 |//-----------------------------------------------------------------------
1327 |.macro .ffunc, name
1331 |.macro .ffunc_1, name
1333 | cmplwi NARGS8:RC, 8
1334 | lwz CARG3, 0(BASE)
1335 | lwz CARG1, 4(BASE)
1336 | blt ->fff_fallback
1339 |.macro .ffunc_2, name
1341 | cmplwi NARGS8:RC, 16
1342 | lwz CARG3, 0(BASE)
1343 | lwz CARG4, 8(BASE)
1344 | lwz CARG1, 4(BASE)
1345 | lwz CARG2, 12(BASE)
1346 | blt ->fff_fallback
1349 |.macro .ffunc_n, name
1351 | cmplwi NARGS8:RC, 8
1352 | lwz CARG1, 0(BASE)
1354 | lfd FARG1, 0(BASE)
1356 | lwz CARG2, 4(BASE)
1358 | blt ->fff_fallback
1359 | checknum CARG1; bge ->fff_fallback
1362 |.macro .ffunc_nn, name
1364 | cmplwi NARGS8:RC, 16
1365 | lwz CARG1, 0(BASE)
1367 | lfd FARG1, 0(BASE)
1368 | lwz CARG3, 8(BASE)
1369 | lfd FARG2, 8(BASE)
1371 | lwz CARG2, 4(BASE)
1372 | lwz CARG3, 8(BASE)
1373 | lwz CARG4, 12(BASE)
1375 | blt ->fff_fallback
1376 | checknum CARG1; bge ->fff_fallback
1377 | checknum CARG3; bge ->fff_fallback
1380 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
1382 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1383 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1388 |//-- Base library: checks -----------------------------------------------
1391 | li TMP1, LJ_TFALSE
1393 | cmplw cr1, CARG3, TMP1
1394 | lwz PC, FRAME_PC(BASE)
1395 | bge cr1, ->fff_fallback
1397 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1398 | addi TMP1, BASE, 8
1399 | add TMP2, RA, NARGS8:RC
1401 | beq ->fff_res // Done if exactly 1 argument.
1408 | lwz CARG1, 0(TMP1)
1409 | lwz CARG2, 4(TMP1)
1410 | stw CARG1, -8(TMP1)
1411 | stw CARG2, -4(TMP1)
1413 | addi TMP1, TMP1, 8
1418 | cmplwi NARGS8:RC, 8
1419 | lwz CARG1, 0(BASE)
1420 | blt ->fff_fallback
1421 | .gpr64 extsw CARG1, CARG1
1422 | subfc TMP0, TISNUM, CARG1
1423 | subfe TMP2, CARG1, CARG1
1424 | orc TMP1, TMP2, TMP0
1425 | addi TMP1, TMP1, ~LJ_TISNUM+1
1426 | slwi TMP1, TMP1, 3
1428 | la TMP2, CFUNC:RB->upvalue
1429 | lfdx FARG1, TMP2, TMP1
1431 | add TMP1, CFUNC:RB, TMP1
1432 | lwz CARG1, CFUNC:TMP1->upvalue[0].u32.hi
1433 | lwz CARG2, CFUNC:TMP1->upvalue[0].u32.lo
1437 |//-- Base library: getters and setters ---------------------------------
1439 |.ffunc_1 getmetatable
1440 | checktab CARG3; bne >6
1441 |1: // Field metatable must be at same offset for GCtab and GCudata!
1442 | lwz TAB:CARG1, TAB:CARG1->metatable
1445 | cmplwi TAB:CARG1, 0
1446 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1448 | lwz TMP0, TAB:CARG1->hmask
1449 | li CARG3, LJ_TTAB // Use metatable as default result.
1450 | lwz TMP1, STR:RC->sid
1451 | lwz NODE:TMP2, TAB:CARG1->node
1452 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
1453 | slwi TMP0, TMP1, 5
1454 | slwi TMP1, TMP1, 3
1455 | sub TMP1, TMP0, TMP1
1456 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1457 |3: // Rearranged logic, because we expect _not_ to find the key.
1458 | lwz CARG4, NODE:TMP2->key
1459 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
1460 | lwz CARG2, NODE:TMP2->val
1461 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
1462 | checkstr CARG4; bne >4
1463 | cmpw TMP0, STR:RC; beq >5
1465 | lwz NODE:TMP2, NODE:TMP2->next
1466 | cmplwi NODE:TMP2, 0
1467 | beq ->fff_restv // Not found, keep default result.
1471 | beq ->fff_restv // Ditto for nil value.
1472 | mr CARG3, CARG2 // Return value of mt.__metatable.
1477 | cmpwi CARG3, LJ_TUDATA; beq <1
1478 | .gpr64 extsw CARG3, CARG3
1479 | subfc TMP0, TISNUM, CARG3
1480 | subfe TMP2, CARG3, CARG3
1481 | orc TMP1, TMP2, TMP0
1482 | addi TMP1, TMP1, ~LJ_TISNUM+1
1483 | slwi TMP1, TMP1, 2
1484 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1485 | lwzx TAB:CARG1, TMP2, TMP1
1488 |.ffunc_2 setmetatable
1489 | // Fast path: no mt for table yet and not clearing the mt.
1490 | checktab CARG3; bne ->fff_fallback
1491 | lwz TAB:TMP1, TAB:CARG1->metatable
1492 | checktab CARG4; bne ->fff_fallback
1493 | cmplwi TAB:TMP1, 0
1494 | lbz TMP3, TAB:CARG1->marked
1495 | bne ->fff_fallback
1496 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1497 | stw TAB:CARG2, TAB:CARG1->metatable
1499 | barrierback TAB:CARG1, TMP3, TMP0
1503 | cmplwi NARGS8:RC, 16
1504 | lwz CARG4, 0(BASE)
1505 | lwz TAB:CARG2, 4(BASE)
1506 | blt ->fff_fallback
1507 | checktab CARG4; bne ->fff_fallback
1510 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1511 | // Returns cTValue *.
1513 | lfd FARG1, 0(CRET1)
1515 | lwz CARG2, 4(CRET1)
1516 | lwz CARG1, 0(CRET1) // Caveat: CARG1 == CRET1.
1520 |//-- Base library: conversions ------------------------------------------
1523 | // Only handles the number case inline (without a base argument).
1524 | cmplwi NARGS8:RC, 8
1525 | lwz CARG1, 0(BASE)
1527 | lfd FARG1, 0(BASE)
1529 | lwz CARG2, 4(BASE)
1531 | bne ->fff_fallback // Exactly one argument.
1532 | checknum CARG1; bgt ->fff_fallback
1536 | // Only handles the string or number case inline.
1538 | // A __tostring method in the string base metatable is ignored.
1539 | beq ->fff_restv // String key?
1540 | // Handle numbers inline, unless a number base metatable is present.
1541 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1543 | cmplwi cr1, TMP0, 0
1544 | stp BASE, L->base // Add frame since C call can throw.
1545 | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq
1546 | stw PC, SAVE_PC // Redundant (but a defined value).
1547 | beq ->fff_fallback
1552 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1554 | bl extern lj_strfmt_num // (lua_State *L, lua_Number *np)
1556 | // Returns GCstr *.
1560 |//-- Base library: iterators -------------------------------------------
1563 | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1565 | lwz PC, FRAME_PC(BASE)
1566 | bne ->fff_fallback
1568 | la CARG3, -8(BASE)
1569 | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1570 | // Returns 1=found, 0=end, -1=error.
1574 | bgt ->fff_res // Found key/value.
1576 | beq ->fff_restv // End of traversal: return nil.
1577 | lwz CFUNC:RB, FRAME_FUNC(BASE)
1579 | b ->fff_fallback // Invalid key.
1583 | lwz PC, FRAME_PC(BASE)
1584 | bne ->fff_fallback
1586 | lwz TAB:TMP2, TAB:CARG1->metatable
1588 | lfd f0, CFUNC:RB->upvalue[0]
1590 | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi
1591 | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo
1593 | cmplwi TAB:TMP2, 0
1595 | bne ->fff_fallback
1598 | lfd f0, CFUNC:RB->upvalue[0]
1600 | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi
1601 | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo
1605 | stw TISNIL, 8(BASE)
1616 | cmplwi NARGS8:RC, 16
1617 | lwz CARG3, 0(BASE)
1618 | lwz TAB:CARG1, 4(BASE)
1619 | lwz CARG4, 8(BASE)
1621 | lwz TMP2, 12(BASE)
1623 | lfd FARG2, 8(BASE)
1625 | blt ->fff_fallback
1627 | checknum cr1, CARG4
1628 | lwz PC, FRAME_PC(BASE)
1630 | bne ->fff_fallback
1631 | bne cr1, ->fff_fallback
1635 | bne ->fff_fallback
1637 | bge cr1, ->fff_fallback
1639 | toint TMP2, FARG2, f0
1641 | lwz TMP0, TAB:CARG1->asize
1642 | lwz TMP1, TAB:CARG1->array
1644 | fadd FARG2, FARG2, FARG1
1646 | addi TMP2, TMP2, 1
1651 | slwi TMP3, TMP2, 3
1654 | slwi TMP3, TMP2, 3
1657 | ble >2 // Not in array part?
1659 | lwzx TMP2, TMP1, TMP3
1660 | lfdx f0, TMP1, TMP3
1662 | lwzux TMP2, TMP1, TMP3
1668 | beq ->fff_res // End of iteration, return 0 results.
1677 |2: // Check for empty hash part first. Otherwise call C function.
1678 | lwz TMP0, TAB:CARG1->hmask
1683 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1684 | // Returns cTValue * or NULL.
1688 | lwz TMP2, 0(CRET1)
1692 | lwz TMP3, 4(CRET1)
1698 | lwz PC, FRAME_PC(BASE)
1699 | bne ->fff_fallback
1701 | lwz TAB:TMP2, TAB:CARG1->metatable
1703 | lfd f0, CFUNC:RB->upvalue[0]
1705 | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi
1706 | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo
1708 | cmplwi TAB:TMP2, 0
1710 | bne ->fff_fallback
1713 | lfd f0, CFUNC:RB->upvalue[0]
1715 | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi
1716 | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo
1721 | stw TISNUM, 8(BASE)
1725 | stw ZERO, 12(BASE)
1735 |//-- Base library: catch errors ----------------------------------------
1738 | cmplwi NARGS8:RC, 8
1739 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1740 | blt ->fff_fallback
1743 | // Remember active hook before pcall.
1744 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1745 | subi NARGS8:RC, NARGS8:RC, 8
1746 | addi PC, TMP3, 8+FRAME_PCALL
1747 | b ->vm_call_dispatch
1750 | cmplwi NARGS8:RC, 16
1751 | lwz CARG3, 8(BASE)
1753 | lfd FARG2, 8(BASE)
1754 | lfd FARG1, 0(BASE)
1756 | lwz CARG1, 0(BASE)
1757 | lwz CARG2, 4(BASE)
1758 | lwz CARG4, 12(BASE)
1760 | blt ->fff_fallback
1761 | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1763 | checkfunc CARG3; bne ->fff_fallback // Traceback must be a function.
1765 | // Remember active hook before pcall.
1766 | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31
1768 | stfd FARG2, 0(TMP2) // Swap function and traceback.
1769 | stfd FARG1, 8(TMP2)
1771 | stw CARG3, 0(TMP2)
1772 | stw CARG4, 4(TMP2)
1773 | stw CARG1, 8(TMP2)
1774 | stw CARG2, 12(TMP2)
1776 | subi NARGS8:RC, NARGS8:RC, 16
1777 | addi PC, TMP1, 16+FRAME_PCALL
1778 | b ->vm_call_dispatch
1780 |//-- Coroutine library --------------------------------------------------
1782 |.macro coroutine_resume_wrap, resume
1784 |.ffunc_1 coroutine_resume
1785 | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback
1787 |.ffunc coroutine_wrap_aux
1788 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1790 | lbz TMP0, L:CARG1->status
1791 | lp TMP1, L:CARG1->cframe
1792 | lp CARG2, L:CARG1->top
1793 | cmplwi cr0, TMP0, LUA_YIELD
1794 | lp TMP2, L:CARG1->base
1795 | cmplwi cr1, TMP1, 0
1796 | lwz TMP0, L:CARG1->maxstack
1797 | cmplw cr7, CARG2, TMP2
1798 | lwz PC, FRAME_PC(BASE)
1799 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1800 | add TMP2, CARG2, NARGS8:RC
1801 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1802 | cmplw cr1, TMP2, TMP0
1803 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1805 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1807 | blt cr6, ->fff_fallback
1810 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1811 | subi NARGS8:RC, NARGS8:RC, 8
1812 | subi TMP2, TMP2, 8
1814 | stp TMP2, L:CARG1->top
1817 |2: // Move args to coroutine.
1818 | cmpw TMP1, NARGS8:RC
1820 | lfdx f0, BASE, TMP1
1822 | add CARG3, BASE, TMP1
1823 | lwz TMP2, 0(CARG3)
1824 | lwz TMP3, 4(CARG3)
1828 | stfdx f0, CARG2, TMP1
1830 | add CARG3, CARG2, TMP1
1831 | stw TMP2, 0(CARG3)
1832 | stw TMP3, 4(CARG3)
1834 | addi TMP1, TMP1, 8
1838 | mr L:SAVE0, L:CARG1
1840 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1841 | // Returns thread status.
1843 | lp TMP2, L:SAVE0->base
1844 | cmplwi CRET1, LUA_YIELD
1845 | lp TMP3, L:SAVE0->top
1848 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
1851 | sub RD, TMP3, TMP2
1852 | lwz TMP0, L->maxstack
1854 | add TMP1, BASE, RD
1855 | beq >6 // No results?
1858 | bgt >9 // Need to grow stack?
1861 | stp TMP2, L:SAVE0->top // Clear coroutine stack.
1862 |5: // Move results from coroutine.
1865 | lfdx f0, TMP2, TMP1
1866 | stfdx f0, BASE, TMP1
1868 | add CARG3, TMP2, TMP1
1869 | lwz CARG1, 0(CARG3)
1870 | lwz CARG2, 4(CARG3)
1871 | add CARG3, BASE, TMP1
1872 | stw CARG1, 0(CARG3)
1873 | stw CARG2, 4(CARG3)
1875 | addi TMP1, TMP1, 8
1878 | andix. TMP0, PC, FRAME_TYPE
1882 | stw TMP1, -8(BASE) // Prepend true to results.
1894 |8: // Coroutine returned with error (at co->top-1).
1896 | andix. TMP0, PC, FRAME_TYPE
1898 | li TMP1, LJ_TFALSE
1902 | lwz CARG1, 0(TMP3)
1903 | lwz CARG2, 4(TMP3)
1905 | stp TMP3, L:SAVE0->top // Remove error from coroutine stack.
1907 | stw TMP1, -8(BASE) // Prepend false to results.
1910 | stfd f0, 0(BASE) // Copy error message.
1912 | stw CARG1, 0(BASE) // Copy error message.
1913 | stw CARG2, 4(BASE)
1919 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1922 |9: // Handle stack expansion on return from yield.
1925 | bl extern lj_state_growstack // (lua_State *L, int n)
1930 | coroutine_resume_wrap 1 // coroutine.resume
1931 | coroutine_resume_wrap 0 // coroutine.wrap
1933 |.ffunc coroutine_yield
1934 | lp TMP0, L->cframe
1935 | add TMP1, BASE, NARGS8:RC
1937 | andix. TMP0, TMP0, CFRAME_RESUME
1939 | li CRET1, LUA_YIELD
1940 | beq ->fff_fallback
1941 | stp ZERO, L->cframe
1942 | stb CRET1, L->status
1945 |//-- Math library -------------------------------------------------------
1951 | srawi TMP1, CARG1, 31
1952 | xor TMP2, TMP1, CARG1
1955 | sub CARG1, TMP2, TMP1
1959 | sub. CARG1, TMP2, TMP1
1963 | lwz PC, FRAME_PC(BASE)
1965 | stw TISNUM, -8(BASE)
1966 | stw CRET1, -4(BASE)
1969 | lus CARG3, 0x41e0 // 2^31.
1974 | bge ->fff_fallback
1975 | rlwinm CARG3, CARG3, 0, 1, 31
1979 | // CARG3/CARG1 = TValue result.
1980 | lwz PC, FRAME_PC(BASE)
1981 | stw CARG3, -8(BASE)
1983 | stw CARG1, -4(BASE)
1985 | // RA = results, PC = return.
1988 | // RA = results, RD = (nresults+1)*8, PC = return.
1989 | andix. TMP0, PC, FRAME_TYPE
1993 | decode_RB8 RB, INS
1995 | cmplw RB, RD // More results expected?
1996 | decode_RA8 TMP0, INS
1999 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
2000 | sub BASE, RA, TMP0
2003 |6: // Fill up results with nil.
2006 | stwx TISNIL, RA, TMP1
2009 |.macro math_extern, func
2010 | .ffunc_n math_ .. func
2015 |.macro math_extern2, func
2016 | .ffunc_nn math_ .. func
2021 |.macro math_round, func
2022 | .ffunc_1 math_ .. func
2023 | checknum CARG3; beqy ->fff_restv
2024 | rlwinm TMP2, CARG3, 12, 21, 31
2025 | bge ->fff_fallback
2026 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
2027 | cmplwi cr1, TMP2, 31 // 0 <= exp < 31?
2028 | subfic TMP0, TMP2, 31
2030 | slwi TMP1, CARG3, 11
2031 | srwi TMP3, CARG1, 21
2032 | oris TMP1, TMP1, 0x8000
2033 | addi TMP2, TMP2, 1
2034 | or TMP1, TMP1, TMP3
2035 | slwi CARG2, CARG1, 11
2037 | slw TMP3, TMP1, TMP2
2038 | srw RD, TMP1, TMP0
2039 | or TMP3, TMP3, CARG2
2040 | srawi TMP2, CARG3, 31
2041 |.if "func" == "floor"
2042 | and TMP1, TMP3, TMP2
2043 | addic TMP0, TMP1, -1
2044 | subfe TMP1, TMP0, TMP1
2045 | add CARG1, RD, TMP1
2046 | xor CARG1, CARG1, TMP2
2047 | sub CARG1, CARG1, TMP2
2050 | andc TMP1, TMP3, TMP2
2051 | addic TMP0, TMP1, -1
2052 | subfe TMP1, TMP0, TMP1
2053 | add CARG1, RD, TMP1
2055 | xor CARG1, CARG1, TMP2
2056 | sub CARG1, CARG1, TMP2
2058 | // Overflow to 2^31.
2059 | lus CARG3, 0x41e0 // 2^31.
2064 | slwi TMP2, CARG3, 1
2065 | srawi TMP1, CARG3, 31
2066 | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo
2067 |.if "func" == "floor"
2068 | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1
2069 | subfic TMP2, TMP1, 0
2070 | subfe CARG1, CARG1, CARG1
2072 | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1
2073 | addic TMP2, TMP1, -1
2074 | subfe CARG1, TMP2, TMP1
2077 |4: // exp >= 31. Check for -(2^31).
2078 | xoris TMP1, TMP1, 0x8000
2079 | srawi TMP2, CARG3, 31
2080 |.if "func" == "floor"
2081 | or TMP1, TMP1, CARG2
2084 | orc TMP1, TMP1, TMP2
2087 | orc. TMP1, TMP1, TMP2
2089 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
2090 | lus CARG1, 0x8000 // -(2^31).
2094 | lfd FARG1, 0(BASE)
2096 | lwz CARG1, 0(BASE)
2097 | lwz CARG2, 4(BASE)
2107 | // NYI: use internal implementation.
2114 | fsqrt FARG1, FARG1
2121 | cmplwi NARGS8:RC, 8
2122 | lwz CARG1, 0(BASE)
2123 | bne ->fff_fallback // Need exactly 1 argument.
2124 | checknum CARG1; bge ->fff_fallback
2126 | lfd FARG1, 0(BASE)
2128 | lwz CARG2, 4(BASE)
2145 | math_extern2 atan2
2150 | cmplwi NARGS8:RC, 16
2153 | lfd FARG1, 0(BASE)
2155 | lwz CARG1, 0(BASE)
2156 | lwz CARG2, 4(BASE)
2160 | lwz CARG2, 12(BASE)
2162 | lwz CARG1, 12(BASE)
2164 | lwz CARG3, 12(BASE)
2166 | blt ->fff_fallback
2167 | checknum TMP0; bge ->fff_fallback
2168 | checknum TMP1; bne ->fff_fallback
2170 |.ffunc_nn math_ldexp
2172 | toint CARG2, FARG2
2174 | toint CARG1, FARG2
2180 |.ffunc_n math_frexp
2182 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
2184 | la CARG1, DISPATCH_GL(tmptv)(DISPATCH)
2186 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
2188 | lwz PC, FRAME_PC(BASE)
2190 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
2193 | tonum_i FARG2, TMP1
2212 | la CARG2, -8(BASE)
2214 | la CARG1, -8(BASE)
2216 | la CARG3, -8(BASE)
2218 | lwz PC, FRAME_PC(BASE)
2222 | stfd FARG1, 0(BASE)
2224 | stw CRET1, 0(BASE)
2225 | stw CRET2, 4(BASE)
2230 |.macro math_minmax, name, ismax
2234 | addi SAVE0, BASE, 8
2235 | add SAVE1, BASE, NARGS8:RC
2237 |1: // Handle integers.
2238 | lwz CARG4, 0(SAVE0)
2239 | cmplw cr1, SAVE0, SAVE1
2240 | lwz CARG2, 4(SAVE0)
2241 | bge cr1, ->fff_resi
2243 | xoris TMP0, CARG1, 0x8000
2244 | xoris TMP3, CARG2, 0x8000
2246 | subfc TMP3, TMP3, TMP0
2247 | subfe TMP0, TMP0, TMP0
2249 | andc TMP3, TMP3, TMP0
2251 | and TMP3, TMP3, TMP0
2253 | add CARG1, TMP3, CARG2
2255 | rldicl CARG1, CARG1, 0, 32
2257 | addi SAVE0, SAVE0, 8
2260 | bge ->fff_fallback
2261 | // Convert intermediate result to number and continue below.
2263 | tonum_i FARG1, CARG1
2264 | lfd FARG2, 0(SAVE0)
2268 | lwz CARG3, 0(SAVE0)
2269 | lwz CARG4, 4(SAVE0)
2274 | lfd FARG1, 0(BASE)
2276 | lwz CARG1, 0(BASE)
2277 | lwz CARG2, 4(BASE)
2279 | bge ->fff_fallback
2280 |5: // Handle numbers.
2281 | lwz CARG3, 0(SAVE0)
2282 | cmplw cr1, SAVE0, SAVE1
2284 | lfd FARG2, 0(SAVE0)
2286 | lwz CARG4, 4(SAVE0)
2288 | bge cr1, ->fff_resn
2289 | checknum CARG3; bge >7
2291 | addi SAVE0, SAVE0, 8
2294 | fsub f0, FARG1, FARG2
2296 | fsub f0, FARG2, FARG1
2298 | fsel FARG1, f0, FARG1, FARG2
2300 | stw CARG1, SFSAVE_1
2301 | stw CARG2, SFSAVE_2
2302 | stw CARG3, SFSAVE_3
2303 | stw CARG4, SFSAVE_4
2311 | lwz CARG1, SFSAVE_1
2312 | lwz CARG2, SFSAVE_2
2315 | lwz CARG1, SFSAVE_3
2316 | lwz CARG2, SFSAVE_4
2319 |7: // Convert integer to number and continue above.
2320 | lwz CARG3, 4(SAVE0)
2321 | bne ->fff_fallback
2323 | tonum_i FARG2, CARG3
2332 | lwzx CARG2, BASE, TMP1
2333 | lfdx FARG2, BASE, TMP1
2334 | cmplw cr1, TMP1, NARGS8:RC
2336 | bge cr1, ->fff_resn
2337 | bge ->fff_fallback
2339 | fsub f0, FARG1, FARG2
2341 | fsub f0, FARG2, FARG1
2343 | addi TMP1, TMP1, 8
2344 | fsel FARG1, f0, FARG1, FARG2
2349 | math_minmax math_min, 0
2350 | math_minmax math_max, 1
2352 |//-- String library -----------------------------------------------------
2354 |.ffunc string_byte // Only handle the 1-arg case here.
2355 | cmplwi NARGS8:RC, 8
2356 | lwz CARG3, 0(BASE)
2357 | lwz STR:CARG1, 4(BASE)
2358 | bne ->fff_fallback // Need exactly 1 argument.
2360 | bne ->fff_fallback
2361 | lwz TMP0, STR:CARG1->len
2363 | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
2365 | lwz PC, FRAME_PC(BASE)
2371 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
2372 | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8
2373 | subfe RD, TMP3, TMP0
2374 | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1.
2378 | lwz PC, FRAME_PC(BASE)
2379 | fsub f0, f0, TOBIT
2385 |.ffunc string_char // Only handle the 1-arg case here.
2387 | cmplwi NARGS8:RC, 8
2388 | lwz CARG3, 0(BASE)
2391 | bne ->fff_fallback // Exactly 1 argument.
2392 | checknum CARG3; bne ->fff_fallback
2395 | lfd FARG1, 0(BASE)
2396 | bne ->fff_fallback // Exactly 1 argument.
2397 | checknum CARG3; bge ->fff_fallback
2399 | la CARG2, TMPD_BLO
2402 | cmplwi TMP0, 255; bgt ->fff_fallback
2407 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
2409 | // Returns GCstr *.
2416 | cmplwi NARGS8:RC, 16
2417 | lwz CARG3, 16(BASE)
2422 | lwz STR:CARG1, 4(BASE)
2423 | blt ->fff_fallback
2424 | lwz CARG2, 8(BASE)
2426 | lwz TMP1, 12(BASE)
2434 | lwz TMP2, 20(BASE)
2435 | bne ->fff_fallback
2437 | checknum CARG2; bne ->fff_fallback
2439 | checknum CARG3; bge ->fff_fallback
2442 | checknum CARG2; bge ->fff_fallback
2444 | checkstr TMP0; bne ->fff_fallback
2448 | lwz TMP0, STR:CARG1->len
2449 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
2450 | addi TMP3, TMP2, 1
2453 | cmpwi TMP1, 0 // start <= 0?
2454 | add TMP3, TMP1, TMP0
2457 | sub CARG3, TMP2, TMP1
2458 | addi CARG2, STR:CARG1, #STR-1
2459 | srawi TMP0, CARG3, 31
2460 | addi CARG3, CARG3, 1
2461 | add CARG2, CARG2, TMP1
2462 | andc CARG3, CARG3, TMP0
2464 | rldicl CARG2, CARG2, 0, 32
2465 | rldicl CARG3, CARG3, 0, 32
2469 |5: // Negative end or overflow.
2470 | cmpw TMP0, TMP2 // len >= end? (signed compare)
2471 | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1.
2473 | mr TMP2, TMP0 // Overflow: end = len.
2476 |7: // Negative start or underflow.
2477 | .gpr64 extsw TMP1, TMP1
2478 | addic CARG3, TMP1, -1
2479 | subfe CARG3, CARG3, CARG3
2480 | srawi CARG2, TMP3, 31 // Note: modifies carry.
2481 | andc TMP3, TMP3, CARG3
2482 | andc TMP1, TMP3, CARG2
2483 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
2486 |.macro ffstring_op, name
2487 | .ffunc string_ .. name
2489 | cmplwi NARGS8:RC, 8
2490 | lwz CARG3, 0(BASE)
2491 | lwz STR:CARG2, 4(BASE)
2492 | blt ->fff_fallback
2494 | la SBUF:CARG1, DISPATCH_GL(tmpbuf)(DISPATCH)
2495 | bne ->fff_fallback
2496 | lwz TMP0, SBUF:CARG1->b
2497 | stw L, SBUF:CARG1->L
2500 | stw TMP0, SBUF:CARG1->w
2501 | bl extern lj_buf_putstr_ .. name
2502 | bl extern lj_buf_tostr
2506 |ffstring_op reverse
2510 |//-- Bit library --------------------------------------------------------
2512 |.macro .ffunc_bit, name
2514 | .ffunc_1 bit_..name
2515 | checknum CARG3; bnel ->fff_tobit_fb
2517 | .ffunc_n bit_..name
2518 | fadd FARG1, FARG1, TOBIT
2520 | lwz CARG1, TMPD_LO
2524 |.macro .ffunc_bit_op, name, ins
2526 | addi SAVE0, BASE, 8
2527 | add SAVE1, BASE, NARGS8:RC
2529 | lwz CARG4, 0(SAVE0)
2530 | cmplw cr1, SAVE0, SAVE1
2532 | lwz CARG2, 4(SAVE0)
2534 | lfd FARG1, 0(SAVE0)
2536 | bgey cr1, ->fff_resi
2540 | bnel ->fff_bitop_fb
2543 | stw CARG1, SFSAVE_1
2546 | lwz CARG1, SFSAVE_1
2550 | fadd FARG1, FARG1, TOBIT
2551 | bge ->fff_fallback
2553 | lwz CARG2, TMPD_LO
2555 | ins CARG1, CARG1, CARG2
2556 | addi SAVE0, SAVE0, 8
2560 |.ffunc_bit_op band, and
2561 |.ffunc_bit_op bor, or
2562 |.ffunc_bit_op bxor, xor
2565 | rotlwi TMP0, CARG1, 8
2566 | rlwimi TMP0, CARG1, 24, 0, 7
2567 | rlwimi TMP0, CARG1, 24, 16, 23
2575 |.macro .ffunc_bit_sh, name, ins, shmod
2577 | .ffunc_2 bit_..name
2579 | checknum CARG3; bnel ->fff_tobit_fb
2581 | checknum CARG3; beq >1
2583 | lwz CARG2, 12(BASE) // Conversion polluted CARG2.
2586 | // Note: no inline conversion from number for 2nd argument!
2587 | checknum CARG4; bne ->fff_fallback
2589 | .ffunc_nn bit_..name
2590 | fadd FARG1, FARG1, TOBIT
2591 | fadd FARG2, FARG2, TOBIT
2593 | lwz CARG1, TMPD_LO
2595 | lwz CARG2, TMPD_LO
2598 | rlwinm CARG2, CARG2, 0, 27, 31
2602 | ins CRET1, CARG1, CARG2
2606 |.ffunc_bit_sh lshift, slw, 1
2607 |.ffunc_bit_sh rshift, srw, 1
2608 |.ffunc_bit_sh arshift, sraw, 1
2609 |.ffunc_bit_sh rol, rotlw, 0
2610 |.ffunc_bit_sh ror, rotlw, 2
2617 | tonum_i FARG1, CRET1
2620 | lwz PC, FRAME_PC(BASE)
2623 | stfd FARG1, -8(BASE)
2625 | stw CARG1, -8(BASE)
2626 | stw CARG2, -4(BASE)
2630 |// Fallback FP number to bit conversion.
2634 | lfd FARG1, 0(BASE)
2635 | bgt ->fff_fallback
2636 | fadd FARG1, FARG1, TOBIT
2638 | lwz CARG1, TMPD_LO
2641 | bgt ->fff_fallback
2644 |// Modifies: CARG1, CARG2, TMP0, TMP1, TMP2.
2646 | slwi TMP2, CARG1, 1
2647 | addis TMP2, TMP2, 0x0020
2651 | srawi TMP2, TMP2, 21
2653 | sub. TMP2, TMP1, TMP2
2654 | cmpwi cr7, CARG1, 0
2656 | slwi TMP1, CARG1, 11
2657 | srwi TMP0, CARG2, 21
2658 | oris TMP1, TMP1, 0x8000
2659 | or TMP1, TMP1, TMP0
2660 | srw CARG1, TMP1, TMP2
2661 | bclr 4, 28 // Return if cr7[lt] == 0, no hint.
2665 | addi TMP2, TMP2, 21
2666 | srw TMP1, CARG2, TMP2
2667 | slwi CARG2, CARG1, 12
2668 | subfic TMP2, TMP2, 20
2669 | slw TMP0, CARG2, TMP2
2670 | or CARG1, TMP1, TMP0
2671 | bclr 4, 28 // Return if cr7[lt] == 0, no hint.
2682 | lfd FARG1, 0(SAVE0)
2683 | bgt ->fff_fallback
2684 | fadd FARG1, FARG1, TOBIT
2686 | lwz CARG2, TMPD_LO
2689 | bgt ->fff_fallback
2695 |//-----------------------------------------------------------------------
2697 |->fff_fallback: // Call fast function fallback handler.
2698 | // BASE = new base, RB = CFUNC, RC = nargs*8
2699 | lp TMP3, CFUNC:RB->f
2700 | add TMP1, BASE, NARGS8:RC
2701 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2702 | addi TMP0, TMP1, 8*LUA_MINSTACK
2703 | lwz TMP2, L->maxstack
2704 | stw PC, SAVE_PC // Redundant (but a defined value).
2705 | .toc lp TMP3, 0(TMP3)
2710 | bgt >5 // Need to grow stack.
2712 | bctrl // (lua_State *L)
2713 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2718 | bgt ->fff_res // Returned nresults+1?
2719 |1: // Returned 0 or -1: retry fast path.
2721 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2722 | sub NARGS8:RC, TMP0, BASE
2723 | bne ->vm_call_tail // Returned -1?
2724 | ins_callt // Returned 0: retry fast path.
2726 |// Reconstruct previous base for vmeta_call during tailcall.
2728 | andix. TMP0, PC, FRAME_TYPE
2729 | rlwinm TMP1, PC, 0, 0, 28
2732 | decode_RA8 TMP1, INS
2733 | addi TMP1, TMP1, 8
2735 | sub TMP2, BASE, TMP1
2736 | b ->vm_call_dispatch // Resolve again for tailcall.
2738 |5: // Grow stack for fallback handler.
2739 | li CARG2, LUA_MINSTACK
2740 | bl extern lj_state_growstack // (lua_State *L, int n)
2742 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
2745 |->fff_gcstep: // Call GC step function.
2746 | // BASE = new base, RC = nargs*8
2749 | add TMP0, BASE, NARGS8:RC
2750 | stw PC, SAVE_PC // Redundant (but a defined value).
2753 | bl extern lj_gc_step // (lua_State *L)
2757 | sub NARGS8:RC, TMP0, BASE
2758 | lwz CFUNC:RB, FRAME_FUNC(BASE)
2761 |//-----------------------------------------------------------------------
2762 |//-- Special dispatch targets -------------------------------------------
2763 |//-----------------------------------------------------------------------
2765 |->vm_record: // Dispatch target for recording phase.
2767 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2768 | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2770 | // Decrement the hookcount for consistency, but always do the call.
2771 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2772 | andix. TMP0, TMP3, HOOK_ACTIVE
2774 | subi TMP2, TMP2, 1
2775 | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2777 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2781 |->vm_rethook: // Dispatch target for return hooks.
2782 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2783 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2785 |5: // Re-dispatch to static ins.
2786 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS.
2787 | lpx TMP0, DISPATCH, TMP1
2791 |->vm_inshook: // Dispatch target for instr/line hooks.
2792 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2793 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2794 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2795 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
2798 | cmpwi cr1, TMP0, 0
2799 | addic. TMP2, TMP2, -1
2801 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2806 | stw MULTRES, SAVE_MULTRES
2809 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2810 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2813 |4: // Re-dispatch to static ins.
2815 | decode_OPP TMP1, INS
2816 | decode_RB8 RB, INS
2817 | addi TMP1, TMP1, GG_DISP2STATIC
2818 | decode_RD8 RD, INS
2819 | lpx TMP0, DISPATCH, TMP1
2820 | decode_RA8 RA, INS
2821 | decode_RC8 RC, INS
2825 |->cont_hook: // Continue from hook yield.
2827 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
2830 |->vm_hotloop: // Hot loop counter underflow.
2832 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2833 | addi CARG1, DISPATCH, GG_DISP2J
2835 | lwz TMP1, LFUNC:TMP1->pc
2837 | stw L, DISPATCH_J(L)(DISPATCH)
2838 | lbz TMP1, PC2PROTO(framesize)(TMP1)
2840 | slwi TMP1, TMP1, 3
2841 | add TMP1, BASE, TMP1
2843 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2847 |->vm_callhook: // Dispatch target for call hooks.
2853 |->vm_hotcall: // Hot call counter underflow.
2858 | add TMP0, BASE, RC
2864 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2865 | // Returns ASMFunction.
2868 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
2869 | sub NARGS8:RC, TMP0, BASE
2871 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2876 |->cont_stitch: // Trace stitching.
2878 | // RA = resultptr, RB = meta base
2880 | lwz TRACE:TMP2, -20(RB) // Save previous trace.
2881 | addic. TMP1, MULTRES, -8
2882 | decode_RA8 RC, INS // Call base.
2884 |1: // Move results down.
2891 | addic. TMP1, TMP1, -8
2894 | stfdx f0, BASE, RC
2896 | add CARG3, BASE, RC
2897 | stw CARG1, 0(CARG3)
2898 | stw CARG2, 4(CARG3)
2903 | decode_RA8 RA, INS
2904 | decode_RB8 RB, INS
2908 | bgt >9 // More results wanted?
2910 | lhz TMP3, TRACE:TMP2->traceno
2911 | lhz RD, TRACE:TMP2->link
2914 | beq ->cont_nop // Blacklisted.
2916 | bne cr1, =>BC_JLOOP // Jump to stitched trace.
2918 | // Stitch a new trace to the previous trace.
2919 | stw TMP3, DISPATCH_J(exitno)(DISPATCH)
2920 | stp L, DISPATCH_J(L)(DISPATCH)
2922 | addi CARG1, DISPATCH, GG_DISP2J
2924 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2929 | stwx TISNIL, BASE, RC
2934 |->vm_profhook: // Dispatch target for profiler hook.
2937 | stw MULTRES, SAVE_MULTRES
2940 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2941 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2947 |//-----------------------------------------------------------------------
2948 |//-- Trace exit handler -------------------------------------------------
2949 |//-----------------------------------------------------------------------
2951 |.macro savex_, a, b, c, d
2953 | stfd f..a, 16+a*8(sp)
2954 | stfd f..b, 16+b*8(sp)
2955 | stfd f..c, 16+c*8(sp)
2956 | stfd f..d, 16+d*8(sp)
2962 | addi sp, sp, -(16+32*8+32*4)
2963 | stmw r2, 16+32*8+2*4(sp)
2964 | addi DISPATCH, JGL, -GG_DISP2G-32768
2965 | li CARG2, ~LJ_VMST_EXIT
2966 | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain.
2967 | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH)
2969 | stw CARG1, 0(sp) // Store extended stack chain.
2972 | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp.
2974 | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP.
2975 | savex_ 12,13,14,15
2978 | savex_ 16,17,18,19
2979 | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP.
2980 | savex_ 20,21,22,23
2981 | lhz CARG4, 2(CARG3) // Load trace number.
2982 | savex_ 24,25,26,27
2983 | lwz L, DISPATCH_GL(cur_L)(DISPATCH)
2984 | savex_ 28,29,30,31
2985 | sub CARG3, TMP0, CARG3 // Compute exit number.
2986 | lp BASE, DISPATCH_GL(jit_base)(DISPATCH)
2987 | srwi CARG3, CARG3, 2
2988 | stp L, DISPATCH_J(L)(DISPATCH)
2989 | subi CARG3, CARG3, 2
2991 | stw CARG4, DISPATCH_J(parent)(DISPATCH)
2992 | stw TMP1, DISPATCH_GL(jit_base)(DISPATCH)
2993 | addi CARG1, DISPATCH, GG_DISP2J
2994 | stw CARG3, DISPATCH_J(exitno)(DISPATCH)
2995 | addi CARG2, sp, 16
2996 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2997 | // Returns MULTRES (unscaled) or negated error code.
2998 | lp TMP1, L->cframe
3002 | rldicr sp, TMP1, 0, 61
3004 | rlwinm sp, TMP1, 0, 0, 29
3006 | lwz PC, SAVE_PC // Get SAVE_PC.
3008 | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield).
3013 | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set.
3015 | addi DISPATCH, JGL, -GG_DISP2G-32768
3018 | li TMP2, -LUA_ERRERR
3020 | bge >9 // Check for error from exit.
3021 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3022 | slwi MULTRES, CARG1, 3
3024 | stw MULTRES, SAVE_MULTRES
3025 | lwz TMP1, LFUNC:RB->pc
3026 | stw TMP2, DISPATCH_GL(jit_base)(DISPATCH)
3027 | lwz KBASE, PC2PROTO(k)(TMP1)
3028 | // Setup type comparison constants.
3029 | li TISNUM, LJ_TISNUM
3030 | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
3031 | .FPU stw TMP3, TMPD
3033 | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
3034 | .FPU lfs TOBIT, TMPD
3035 | .FPU stw TMP3, TMPD
3036 | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
3037 | li TISNIL, LJ_TNIL
3038 | .FPU stw TMP0, TONUM_HI
3039 | .FPU lfs TONUM, TMPD
3040 | // Modified copy of ins_next which handles function header dispatch, too.
3043 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1.
3044 | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
3045 | cmpwi CARG1, -17 // Static dispatch?
3047 | decode_OPP TMP1, INS
3048 | decode_RA8 RA, INS
3049 | lpx TMP0, DISPATCH, TMP1
3051 | cmplwi TMP1, BC_FUNCF*4 // Function header?
3053 | decode_RB8 RB, INS
3054 | decode_RD8 RD, INS
3055 | decode_RC8 RC, INS
3058 | cmplwi TMP1, (BC_FUNCC+2)*4 // Fast function?
3060 | // Check frame below fast function.
3061 | lwz TMP1, FRAME_PC(BASE)
3062 | andix. TMP0, TMP1, FRAME_TYPE
3063 | bney >3 // Trace stitching continuation?
3064 | // Otherwise set KBASE for Lua function below fast function.
3065 | lwz TMP2, -4(TMP1)
3066 | decode_RA8 TMP0, TMP2
3067 | sub TMP1, BASE, TMP0
3068 | lwz LFUNC:TMP2, -12(TMP1)
3069 | lwz TMP1, LFUNC:TMP2->pc
3070 | lwz KBASE, PC2PROTO(k)(TMP1)
3072 | subi RC, MULTRES, 8
3076 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
3077 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
3078 | decode_RD4 RD, INS
3079 | lwzx TRACE:TMP1, TMP1, RD
3080 | lwz INS, TRACE:TMP1->startins
3081 | decode_OPP TMP1, INS
3082 | addi TMP1, TMP1, GG_DISP2STATIC
3083 | lpx TMP0, DISPATCH, TMP1
3085 | decode_RB8 RB, INS
3086 | decode_RD8 RD, INS
3087 | decode_RA8 RA, INS
3088 | decode_RC8 RC, INS
3091 |9: // Rethrow error from the right C frame.
3094 | bl extern lj_err_trace // (lua_State *L, int errcode)
3097 |//-----------------------------------------------------------------------
3098 |//-- Math helper functions ----------------------------------------------
3099 |//-----------------------------------------------------------------------
3101 |// NYI: Use internal implementations of floor, ceil, trunc, sfcmp.
3103 |.macro sfi2d, AHI, ALO
3106 | bclr 12, 2 // Handle zero first.
3107 | srawi TMP0, ALO, 31
3108 | xor TMP1, ALO, TMP0
3109 | sub TMP1, TMP1, TMP0 // Absolute value in TMP1.
3111 | andix. TMP0, TMP0, 0x800 // Mask sign bit.
3112 | slw TMP1, TMP1, AHI // Align mantissa left with leading 1.
3113 | subfic AHI, AHI, 0x3ff+31-1 // Exponent -1 in AHI.
3114 | slwi ALO, TMP1, 21
3115 | or AHI, AHI, TMP0 // Sign | Exponent.
3116 | srwi TMP1, TMP1, 11
3117 | slwi AHI, AHI, 20 // Align left.
3118 | add AHI, AHI, TMP1 // Add mantissa, increment exponent.
3123 |// Input: CARG2. Output: CARG1, CARG2. Temporaries: TMP0, TMP1.
3125 | sfi2d CARG1, CARG2
3127 |// Input: CARG4. Output: CARG3, CARG4. Temporaries: TMP0, TMP1.
3129 | sfi2d CARG3, CARG4
3132 | divwo. TMP0, CARG1, CARG2
3135 | xor CARG3, CARG1, CARG2
3138 | xor. CARG3, CARG1, CARG2
3140 | mullw TMP0, TMP0, CARG2
3141 | sub CARG1, CARG1, TMP0
3143 | cmpwi CARG1, 0; beqlr
3144 | add CARG1, CARG1, CARG2
3150 | clrso TMP0 // Clear SO for -2147483648 % -1 and return 0.
3153 |//-----------------------------------------------------------------------
3154 |//-- Miscellaneous functions --------------------------------------------
3155 |//-----------------------------------------------------------------------
3157 |// void lj_vm_cachesync(void *start, void *end)
3158 |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size.
3159 |// This is a good lower bound, except for very ancient PPC models.
3162 | // Compute start of first cache line and number of cache lines.
3163 | rlwinm CARG1, CARG1, 0, 0, 26
3164 | sub CARG2, CARG2, CARG1
3165 | addi CARG2, CARG2, 31
3166 | rlwinm. CARG2, CARG2, 27, 5, 31
3170 |1: // Flush D-Cache.
3172 | addi CARG1, CARG1, 32
3176 |1: // Invalidate I-Cache.
3178 | addi CARG3, CARG3, 32
3186 | NYI // On big-endian.
3189 |//-----------------------------------------------------------------------
3190 |//-- FFI helper functions -----------------------------------------------
3191 |//-----------------------------------------------------------------------
3193 |// Handler for callback functions. Callback slot number in r11, g in r12.
3196 |.type CTSTATE, CTState, PC
3198 | lwz CTSTATE, GL:r12->ctype_state
3199 | addi DISPATCH, r12, GG_G2DISP
3200 | stw r11, CTSTATE->cb.slot
3201 | stw r3, CTSTATE->cb.gpr[0]
3202 | .FPU stfd f1, CTSTATE->cb.fpr[0]
3203 | stw r4, CTSTATE->cb.gpr[1]
3204 | .FPU stfd f2, CTSTATE->cb.fpr[1]
3205 | stw r5, CTSTATE->cb.gpr[2]
3206 | .FPU stfd f3, CTSTATE->cb.fpr[2]
3207 | stw r6, CTSTATE->cb.gpr[3]
3208 | .FPU stfd f4, CTSTATE->cb.fpr[3]
3209 | stw r7, CTSTATE->cb.gpr[4]
3210 | .FPU stfd f5, CTSTATE->cb.fpr[4]
3211 | stw r8, CTSTATE->cb.gpr[5]
3212 | .FPU stfd f6, CTSTATE->cb.fpr[5]
3213 | stw r9, CTSTATE->cb.gpr[6]
3214 | .FPU stfd f7, CTSTATE->cb.fpr[6]
3215 | stw r10, CTSTATE->cb.gpr[7]
3216 | .FPU stfd f8, CTSTATE->cb.fpr[7]
3217 | addi TMP0, sp, CFRAME_SPACE+8
3218 | stw TMP0, CTSTATE->cb.stack
3220 | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
3222 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
3223 | // Returns lua_State *.
3224 | lp BASE, L:CRET1->base
3225 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
3226 | lp RC, L:CRET1->top
3227 | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
3230 | .FPU stw TMP3, TMPD
3231 | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
3232 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3233 | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
3234 | .FPU stw TMP0, TONUM_HI
3235 | li TISNIL, LJ_TNIL
3237 | .FPU lfs TOBIT, TMPD
3238 | .FPU stw TMP3, TMPD
3241 | .FPU lfs TONUM, TMPD
3245 |->cont_ffi_callback: // Return from FFI callback.
3247 | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
3253 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
3254 | lwz CRET1, CTSTATE->cb.gpr[0]
3255 | .FPU lfd FARG1, CTSTATE->cb.fpr[0]
3256 | lwz CRET2, CTSTATE->cb.gpr[1]
3260 |->vm_ffi_call: // Call C function via FFI.
3261 | // Caveat: needs special frame unwinding, see below.
3263 | .type CCSTATE, CCallState, CARG1
3264 | lwz TMP1, CCSTATE->spadj
3266 | lbz CARG2, CCSTATE->nsp
3267 | lbz CARG3, CCSTATE->nfpr
3270 | cmpwi cr1, CARG3, 0
3272 | addic. CARG2, CARG2, -4
3273 | stwux sp, sp, TMP1
3274 | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls.
3276 | stw CCSTATE, -8(TMP2)
3278 | la TMP1, CCSTATE->stack
3282 | lwzx TMP0, TMP1, CARG2
3283 | stwx TMP0, TMP2, CARG2
3284 | addic. CARG2, CARG2, -4
3288 | .FPU lfd f1, CCSTATE->fpr[0]
3289 | .FPU lfd f2, CCSTATE->fpr[1]
3290 | .FPU lfd f3, CCSTATE->fpr[2]
3291 | .FPU lfd f4, CCSTATE->fpr[3]
3292 | .FPU lfd f5, CCSTATE->fpr[4]
3293 | .FPU lfd f6, CCSTATE->fpr[5]
3294 | .FPU lfd f7, CCSTATE->fpr[6]
3295 | .FPU lfd f8, CCSTATE->fpr[7]
3297 | lp TMP0, CCSTATE->func
3298 | lwz CARG2, CCSTATE->gpr[1]
3299 | lwz CARG3, CCSTATE->gpr[2]
3300 | lwz CARG4, CCSTATE->gpr[3]
3301 | lwz CARG5, CCSTATE->gpr[4]
3303 | lwz r8, CCSTATE->gpr[5]
3304 | lwz r9, CCSTATE->gpr[6]
3305 | lwz r10, CCSTATE->gpr[7]
3306 | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
3308 | lwz CCSTATE:TMP1, -8(r14)
3311 | stw CARG1, CCSTATE:TMP1->gpr[0]
3312 | .FPU stfd FARG1, CCSTATE:TMP1->fpr[0]
3313 | stw CARG2, CCSTATE:TMP1->gpr[1]
3315 | stw CARG3, CCSTATE:TMP1->gpr[2]
3317 | stw CARG4, CCSTATE:TMP1->gpr[3]
3321 |// Note: vm_ffi_call must be the last function in this object file!
3323 |//-----------------------------------------------------------------------
3326 /* Generate the code for a single instruction. */
3327 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3334 /* -- Comparison ops ---------------------------------------------------- */
3336 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3338 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3339 | // RA = src1*8, RD = src2*8, JMP with RD = target
3341 | lwzux CARG1, RA, BASE
3344 | lwzux CARG3, RD, BASE
3346 | checknum cr0, CARG1
3348 | decode_RD4 TMP2, TMP2
3349 | checknum cr1, CARG3
3350 | addis SAVE0, TMP2, -(BCBIAS_J*4 >> 16)
3354 if (op == BC_ISLT) {
3356 } else if (op == BC_ISGE) {
3358 } else if (op == BC_ISLE) {
3368 |7: // RA is not an integer.
3369 | bgt cr0, ->vmeta_comp
3370 | // RA is a number.
3371 | .FPU lfd f0, 0(RA)
3372 | bgt cr1, ->vmeta_comp
3374 | // RA is a number, RD is an integer.
3382 |8: // RA is an integer, RD is not an integer.
3383 | bgt cr1, ->vmeta_comp
3384 | // RA is an integer, RD is a number.
3391 | .FPU lfd f1, 0(RD)
3399 if (op == BC_ISLT) {
3401 } else if (op == BC_ISGE) {
3403 } else if (op == BC_ISLE) {
3404 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3407 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3412 | lwzx TMP0, BASE, RA
3415 | lwzx TMP1, BASE, RD
3416 | checknum cr0, TMP0
3419 | checknum cr1, TMP1
3420 | decode_RD4 TMP2, TMP2
3421 | bge cr0, ->vmeta_comp
3422 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3423 | bge cr1, ->vmeta_comp
3425 if (op == BC_ISLT) {
3427 } else if (op == BC_ISGE) {
3429 } else if (op == BC_ISLE) {
3430 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3433 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3442 case BC_ISEQV: case BC_ISNEV:
3443 vk = op == BC_ISEQV;
3444 | // RA = src1*8, RD = src2*8, JMP with RD = target
3446 | lwzux CARG1, RA, BASE
3449 | lwzux CARG3, RD, BASE
3450 | checknum cr0, CARG1
3452 | checknum cr1, CARG3
3453 | decode_RD4 SAVE0, SAVE0
3455 | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt
3456 | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16)
3458 | ble cr7, ->BC_ISEQN_Z
3460 | ble cr7, ->BC_ISNEN_Z
3463 | lwzux CARG1, RA, BASE
3467 | lwzux CARG3, RD, BASE
3468 | checknum cr0, CARG1
3469 | decode_RD4 SAVE0, SAVE0
3471 | checknum cr1, CARG3
3472 | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16)
3486 |5: // Either or both types are not numbers.
3492 | cmpwi cr7, CARG1, LJ_TCDATA
3493 | cmpwi cr5, CARG3, LJ_TCDATA
3496 | cmplw CARG1, CARG3
3497 | cmplwi cr1, TMP2, ~LJ_TISPRI // Primitive?
3499 | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq
3501 | cmplwi cr6, TMP2, ~LJ_TISTABUD // Table or userdata?
3503 | beq cr7, ->vmeta_equal_cd
3505 | cmplw cr5, CARG2, CARG4
3506 | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive.
3507 | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type.
3508 | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv.
3510 | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2.
3511 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2.
3522 | bge cr0, >2 // Done if 1 or 2.
3527 | blt cr0, <1 // Done if 1 or 2.
3529 | blt cr6, <1 // Done if not tab/ud.
3531 | // Different tables or userdatas. Need to check __eq metamethod.
3532 | // Field metatable must be at same offset for GCtab and GCudata!
3534 | lwz TAB:TMP2, TAB:CARG2->metatable
3535 | li CARG4, 1-vk // ne = 0 or 1.
3536 | cmplwi TAB:TMP2, 0
3537 | beq <1 // No metatable?
3538 | lbz TMP2, TAB:TMP2->nomm
3539 | andix. TMP2, TMP2, 1<<MM_eq
3540 | bne <1 // Or 'no __eq' flag set?
3541 | mr PC, SAVE1 // Restore old PC.
3542 | b ->vmeta_equal // Handle __eq metamethod.
3545 case BC_ISEQS: case BC_ISNES:
3546 vk = op == BC_ISEQS;
3547 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3548 | lwzux TMP0, RA, BASE
3550 | lwz STR:TMP3, 4(RA)
3555 | cmpwi TMP0, LJ_TCDATA
3557 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
3558 | .gpr64 extsw TMP0, TMP0
3559 | subfic TMP0, TMP0, LJ_TSTR
3561 | beq ->vmeta_equal_cd
3563 | sub TMP1, STR:TMP1, STR:TMP3
3564 | or TMP0, TMP0, TMP1
3565 | decode_RD4 TMP2, TMP2
3566 | subfic TMP0, TMP0, 0
3567 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3568 | subfe TMP1, TMP1, TMP1
3570 | andc TMP2, TMP2, TMP1
3572 | and TMP2, TMP2, TMP1
3578 case BC_ISEQN: case BC_ISNEN:
3579 vk = op == BC_ISEQN;
3580 | // RA = src*8, RD = num_const*8, JMP with RD = target
3582 | lwzux CARG1, RA, BASE
3585 | lwzux CARG3, RD, KBASE
3586 | checknum cr0, CARG1
3588 | checknum cr1, CARG3
3589 | decode_RD4 SAVE0, SAVE0
3591 | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16)
3603 |->BC_ISEQN_Z: // Dummy label.
3605 |->BC_ISNEN_Z: // Dummy label.
3607 | lwzx CARG1, BASE, RA
3611 | lfdx f1, KBASE, RD
3612 | decode_RD4 SAVE0, SAVE0
3614 | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16)
3637 | cmpwi CARG1, LJ_TCDATA
3638 | beq ->vmeta_equal_cd
3642 |7: // RA is not an integer.
3644 | // RA is a number.
3645 | .FPU lfd f0, 0(RA)
3647 | // RA is a number, RD is an integer.
3655 |8: // RA is an integer, RD is a number.
3662 | .FPU lfd f1, 0(RD)
3674 case BC_ISEQP: case BC_ISNEP:
3675 vk = op == BC_ISEQP;
3676 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3677 | lwzx TMP0, BASE, RA
3683 | cmpwi TMP0, LJ_TCDATA
3685 | sub TMP0, TMP0, TMP1
3687 | beq ->vmeta_equal_cd
3689 | decode_RD4 TMP2, TMP2
3690 | .gpr64 extsw TMP0, TMP0
3691 | addic TMP0, TMP0, -1
3692 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3693 | subfe TMP1, TMP1, TMP1
3695 | and TMP2, TMP2, TMP1
3697 | andc TMP2, TMP2, TMP1
3703 /* -- Unary test and copy ops ------------------------------------------- */
3705 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3706 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3707 | lwzx TMP0, BASE, RD
3710 if (op == BC_IST || op == BC_ISF) {
3711 | .gpr64 extsw TMP0, TMP0
3712 | subfic TMP0, TMP0, LJ_TTRUE
3713 | decode_RD4 TMP2, INS
3714 | subfe TMP1, TMP1, TMP1
3715 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3717 | andc TMP2, TMP2, TMP1
3719 | and TMP2, TMP2, TMP1
3723 | li TMP1, LJ_TFALSE
3727 | lwzux CARG1, RD, BASE
3731 if (op == BC_ISTC) {
3736 | addis PC, PC, -(BCBIAS_J*4 >> 16)
3737 | decode_RD4 TMP2, INS
3739 | stfdx f0, BASE, RA
3741 | stwux CARG1, RA, BASE
3751 | // RA = src*8, RD = -type*8
3752 | lwzx TMP0, BASE, RA
3755 |.if not PPE and not GPR64
3756 | add. TMP0, TMP0, TMP1
3761 | bne ->vmeta_istype
3765 | // RA = src*8, RD = -(TISNUM-1)*8
3766 | lwzx TMP0, BASE, RA
3769 | bge ->vmeta_istype
3773 /* -- Unary ops --------------------------------------------------------- */
3776 | // RA = dst*8, RD = src*8
3780 | stfdx f0, BASE, RA
3782 | lwzux TMP0, RD, BASE
3784 | stwux TMP0, RA, BASE
3790 | // RA = dst*8, RD = src*8
3792 | lwzx TMP0, BASE, RD
3793 | .gpr64 extsw TMP0, TMP0
3794 | subfic TMP1, TMP0, LJ_TTRUE
3795 | adde TMP0, TMP0, TMP1
3796 | stwx TMP0, BASE, RA
3800 | // RA = dst*8, RD = src*8
3801 | lwzux TMP1, RD, BASE
3817 | stwux TISNUM, RA, BASE
3823 | // Potential overflow.
3824 | checkov TMP1, <1 // Ignore unrelated overflow.
3826 | lus TMP1, 0x41e0 // 2^31.
3832 | xoris TMP1, TMP1, 0x8000
3835 | stwux TMP1, RA, BASE
3844 | // RA = dst*8, RD = src*8
3845 | lwzux TMP0, RD, BASE
3847 | checkstr TMP0; bne >2
3848 | lwz CRET1, STR:CARG1->len
3852 | stwux TISNUM, RA, BASE
3855 | tonum_u f0, CRET1 // Result is a non-negative integer.
3857 | stfdx f0, BASE, RA
3861 | checktab TMP0; bne ->vmeta_len
3863 | lwz TAB:TMP2, TAB:CARG1->metatable
3864 | cmplwi TAB:TMP2, 0
3869 | bl extern lj_tab_len // (GCtab *t)
3870 | // Returns uint32_t (but less than 2^31).
3874 | lbz TMP0, TAB:TMP2->nomm
3875 | andix. TMP0, TMP0, 1<<MM_len
3876 | bne <3 // 'no __len' flag set: done.
3881 /* -- Binary ops -------------------------------------------------------- */
3883 |.macro ins_arithpre
3884 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3885 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3888 | lwzx CARG1, BASE, RB
3890 | lwzx CARG3, KBASE, RC
3893 | lfdx f14, BASE, RB
3894 | lfdx f15, KBASE, RC
3896 | add TMP1, BASE, RB
3897 | add TMP2, KBASE, RC
3898 | lwz CARG2, 4(TMP1)
3899 | lwz CARG4, 4(TMP2)
3902 | checknum cr0, CARG1
3903 | checknum cr1, CARG3
3904 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3905 | bge ->vmeta_arith_vn
3907 | checknum CARG1; bge ->vmeta_arith_vn
3911 | lwzx CARG1, BASE, RB
3913 | lwzx CARG3, KBASE, RC
3916 | lfdx f15, BASE, RB
3917 | lfdx f14, KBASE, RC
3919 | add TMP1, BASE, RB
3920 | add TMP2, KBASE, RC
3921 | lwz CARG2, 4(TMP1)
3922 | lwz CARG4, 4(TMP2)
3925 | checknum cr0, CARG1
3926 | checknum cr1, CARG3
3927 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3928 | bge ->vmeta_arith_nv
3930 | checknum CARG1; bge ->vmeta_arith_nv
3934 | lwzx CARG1, BASE, RB
3935 | lwzx CARG3, BASE, RC
3937 | lfdx f14, BASE, RB
3938 | lfdx f15, BASE, RC
3940 | add TMP1, BASE, RB
3941 | add TMP2, BASE, RC
3942 | lwz CARG2, 4(TMP1)
3943 | lwz CARG4, 4(TMP2)
3945 | checknum cr0, CARG1
3946 | checknum cr1, CARG3
3947 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3948 | bge ->vmeta_arith_vv
3953 |.macro ins_arithfallback, ins
3956 | ins ->vmeta_arith_vn2
3959 | ins ->vmeta_arith_nv2
3962 | ins ->vmeta_arith_vv2
3967 |.macro intmod, a, b, c
3971 |.macro fpmod, a, b, c
3974 | // NYI: Use internal implementation of floor.
3975 | blex floor // floor(b/c)
3977 | fsub a, b, a // b - floor(b/c)*c
3982 | stw CARG1, SFSAVE_1
3983 | stw CARG2, SFSAVE_2
3993 | lwz CARG1, SFSAVE_1
3994 | lwz CARG2, SFSAVE_2
3998 |.macro ins_arithfp, fpins
4000 |.if "fpins" == "fpmod_"
4001 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
4003 | fpins f0, f14, f15
4005 | stfdx f0, BASE, RA
4008 | blex __divdf3 // Only soft-float div uses this macro.
4010 | stwux CRET1, RA, BASE
4016 |.macro ins_arithdn, intins, fpins, fpcall
4017 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
4018 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4021 | lwzux CARG1, RB, BASE
4022 | lwzux CARG3, RC, KBASE
4024 | checknum cr0, CARG1
4026 | checknum cr1, CARG3
4029 | lwzux CARG3, RB, BASE
4030 | lwzux CARG1, RC, KBASE
4032 | checknum cr0, CARG3
4034 | checknum cr1, CARG1
4037 | lwzux CARG1, RB, BASE
4038 | lwzux CARG3, RC, BASE
4040 | checknum cr0, CARG1
4042 | checknum cr1, CARG3
4047 |.if "intins" == "intmod"
4051 | intins CARG1, CARG2, CARG4
4055 | stwux TISNUM, RA, BASE
4060 | checkov TMP0, <1 // Ignore unrelated overflow.
4061 | ins_arithfallback b
4072 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4073 | ins_arithfallback bge
4074 |.if "fpins" == "fpmod_"
4075 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
4078 | fpins f0, f14, f15
4079 | stfdx f0, BASE, RA
4081 |.if "fpcall" == "sfpmod"
4086 | stwux CRET1, RA, BASE
4094 |.macro ins_arith, intins, fpins, fpcall
4096 | ins_arithdn intins, fpins, fpcall
4102 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
4104 |.macro addo32., y, a, b
4105 | // Need to check overflow for (a<<32) + (b<<32).
4106 | rldicr TMP0, a, 32, 31
4107 | rldicr TMP1, b, 32, 31
4108 | addo. TMP0, TMP0, TMP1
4111 | ins_arith addo32., fadd, __adddf3
4113 | ins_arith addo., fadd, __adddf3
4116 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
4118 |.macro subo32., y, a, b
4119 | // Need to check overflow for (a<<32) - (b<<32).
4120 | rldicr TMP0, a, 32, 31
4121 | rldicr TMP1, b, 32, 31
4122 | subo. TMP0, TMP0, TMP1
4125 | ins_arith subo32., fsub, __subdf3
4127 | ins_arith subo., fsub, __subdf3
4130 case BC_MULVN: case BC_MULNV: case BC_MULVV:
4131 | ins_arith mullwo., fmul, __muldf3
4133 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
4137 | ins_arith intmod, fpmod, sfpmod
4139 case BC_MODNV: case BC_MODVV:
4140 | ins_arith intmod, fpmod_, sfpmod
4143 | // NYI: (partial) integer arithmetic.
4144 | lwzx CARG1, BASE, RB
4145 | lwzx CARG3, BASE, RC
4147 | lfdx FARG1, BASE, RB
4148 | lfdx FARG2, BASE, RC
4150 | add TMP1, BASE, RB
4151 | add TMP2, BASE, RC
4152 | lwz CARG2, 4(TMP1)
4153 | lwz CARG4, 4(TMP2)
4155 | checknum cr0, CARG1
4156 | checknum cr1, CARG3
4157 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4158 | bge ->vmeta_arith_vv
4162 | stfdx FARG1, BASE, RA
4164 | stwux CARG1, RA, BASE
4171 | // RA = dst*8, RB = src_start*8, RC = src_end*8
4174 | add CARG2, BASE, RC
4179 | srwi CARG3, CARG3, 3
4180 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
4181 | // Returns NULL (finished) or TValue * (metamethod).
4187 | lfdx f0, BASE, SAVE0 // Copy result from RB to RA.
4188 | stfdx f0, BASE, RA
4190 | lwzux TMP0, SAVE0, BASE
4191 | lwz TMP1, 4(SAVE0)
4192 | stwux TMP0, RA, BASE
4198 /* -- Constant ops ------------------------------------------------------ */
4201 | // RA = dst*8, RD = str_const*8 (~)
4203 | subfic TMP1, TMP1, -4
4205 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
4207 | stwux TMP2, RA, BASE
4213 | // RA = dst*8, RD = cdata_const*8 (~)
4215 | subfic TMP1, TMP1, -4
4217 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
4218 | li TMP2, LJ_TCDATA
4219 | stwux TMP2, RA, BASE
4225 | // RA = dst*8, RD = int16_literal*8
4230 | stwux TISNUM, RA, BASE
4234 | // The soft-float approach is faster.
4236 | srawi TMP1, RD, 31
4237 | xor TMP2, TMP1, RD
4238 | sub TMP2, TMP2, TMP1 // TMP2 = abs(x)
4240 | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1
4241 | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa
4242 | subfic TMP3, RD, 0
4243 | slwi TMP1, TMP1, 20
4244 | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11)
4245 | subfe TMP0, TMP0, TMP0
4246 | add RD, RD, TMP1 // hi = hi + exponent-1
4247 | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi
4249 | stwux RD, RA, BASE
4255 | // RA = dst*8, RD = num_const*8
4258 | lfdx f0, KBASE, RD
4259 | stfdx f0, BASE, RA
4261 | lwzux TMP0, RD, KBASE
4263 | stwux TMP0, RA, BASE
4269 | // RA = dst*8, RD = primitive_type*8 (~)
4273 | stwx TMP0, BASE, RA
4277 | // RA = base*8, RD = end*8
4278 | stwx TISNIL, BASE, RA
4281 | stwx TISNIL, BASE, RA
4288 /* -- Upvalue and function ops ------------------------------------------ */
4291 | // RA = dst*8, RD = uvnum*8
4292 | lwz LFUNC:RB, FRAME_FUNC(BASE)
4294 | addi RD, RD, offsetof(GCfuncL, uvptr)
4295 | lwzx UPVAL:RB, LFUNC:RB, RD
4297 | lwz TMP1, UPVAL:RB->v
4300 | stfdx f0, BASE, RA
4304 | stwux TMP2, RA, BASE
4310 | // RA = uvnum*8, RD = src*8
4311 | lwz LFUNC:RB, FRAME_FUNC(BASE)
4313 | addi RA, RA, offsetof(GCfuncL, uvptr)
4315 | lfdux f0, RD, BASE
4317 | lwzux CARG1, RD, BASE
4320 | lwzx UPVAL:RB, LFUNC:RB, RA
4321 | lbz TMP3, UPVAL:RB->marked
4322 | lwz CARG2, UPVAL:RB->v
4323 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
4324 | lbz TMP0, UPVAL:RB->closed
4329 | stw CARG1, 0(CARG2)
4330 | stw CARG3, 4(CARG2)
4332 | cmplwi cr1, TMP0, 0
4334 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4335 | subi TMP2, TMP2, (LJ_TNUMX+1)
4336 | bne >2 // Upvalue is closed and black?
4340 |2: // Check if new value is collectable.
4341 | cmplwi TMP2, LJ_TISGCV - (LJ_TNUMX+1)
4342 | bge <1 // tvisgcv(v)
4343 | lbz TMP3, GCOBJ:TMP1->gch.marked
4344 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
4345 | la CARG1, GG_DISP2G(DISPATCH)
4346 | // Crossed a write barrier. Move the barrier forward.
4348 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
4352 | // RA = uvnum*8, RD = str_const*8 (~)
4353 | lwz LFUNC:RB, FRAME_FUNC(BASE)
4356 | subfic TMP1, TMP1, -4
4357 | addi RA, RA, offsetof(GCfuncL, uvptr)
4358 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
4359 | lwzx UPVAL:RB, LFUNC:RB, RA
4360 | lbz TMP3, UPVAL:RB->marked
4361 | lwz CARG2, UPVAL:RB->v
4362 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
4363 | lbz TMP3, STR:TMP1->marked
4364 | lbz TMP2, UPVAL:RB->closed
4366 | stw STR:TMP1, 4(CARG2)
4367 | stw TMP0, 0(CARG2)
4372 |2: // Check if string is white and ensure upvalue is closed.
4373 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
4374 | cmplwi cr1, TMP2, 0
4375 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4376 | la CARG1, GG_DISP2G(DISPATCH)
4377 | // Crossed a write barrier. Move the barrier forward.
4379 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
4383 | // RA = uvnum*8, RD = num_const*8
4384 | lwz LFUNC:RB, FRAME_FUNC(BASE)
4386 | addi RA, RA, offsetof(GCfuncL, uvptr)
4388 | lfdx f0, KBASE, RD
4390 | lwzux TMP2, RD, KBASE
4393 | lwzx UPVAL:RB, LFUNC:RB, RA
4395 | lwz TMP1, UPVAL:RB->v
4405 | // RA = uvnum*8, RD = primitive_type*8 (~)
4406 | lwz LFUNC:RB, FRAME_FUNC(BASE)
4409 | addi RA, RA, offsetof(GCfuncL, uvptr)
4411 | lwzx UPVAL:RB, LFUNC:RB, RA
4413 | lwz TMP1, UPVAL:RB->v
4419 | // RA = level*8, RD = target
4420 | lwz TMP1, L->openupval
4421 | branch_RD // Do this first since RD is not saved.
4426 | add CARG2, BASE, RA
4427 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
4434 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
4437 | subfic TMP1, TMP1, -4
4439 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
4441 | lwz CARG3, FRAME_FUNC(BASE)
4442 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4443 | bl extern lj_func_newL_gc
4444 | // Returns GCfuncL *.
4447 | stwux TMP0, RA, BASE
4448 | stw LFUNC:CRET1, 4(RA)
4452 /* -- Table ops --------------------------------------------------------- */
4456 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
4457 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
4459 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
4465 if (op == BC_TNEW) {
4466 | rlwinm CARG2, RD, 29, 21, 31
4467 | rlwinm CARG3, RD, 18, 27, 31
4468 | cmpwi CARG2, 0x7ff; beq >3
4470 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4471 | // Returns Table *.
4474 | subfic TMP1, TMP1, -4
4475 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
4476 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
4477 | // Returns Table *.
4481 | stwux TMP0, RA, BASE
4482 | stw TAB:CRET1, 4(RA)
4484 if (op == BC_TNEW) {
4491 | bl extern lj_gc_step_fixtop // (lua_State *L)
4498 | // RA = dst*8, RD = str_const*8 (~)
4500 | // RA = src*8, RD = str_const*8 (~)
4501 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
4503 | lwz TAB:RB, LFUNC:TMP2->env
4504 | subfic TMP1, TMP1, -4
4505 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4506 if (op == BC_GGET) {
4514 | // RA = dst*8, RB = table*8, RC = key*8
4515 | lwzux CARG1, RB, BASE
4516 | lwzux CARG2, RC, BASE
4524 | checknum cr1, CARG2
4527 | lwz TMP0, TAB:RB->asize
4529 | lwz TMP1, TAB:RB->array
4534 | // Convert number key to integer, check for integerness and range.
4536 | fadd f2, f0, TOBIT
4538 | lwz TMP0, TAB:RB->asize
4539 | fsub f2, f2, TOBIT
4541 | lwz TMP1, TAB:RB->array
4543 | cmplw cr0, TMP0, TMP2
4544 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4545 | slwi TMP2, TMP2, 3
4547 | ble ->vmeta_tgetv // Integer key and in array part?
4548 | lwzx TMP0, TMP1, TMP2
4550 | lfdx f14, TMP1, TMP2
4552 | lwzux SAVE0, TMP1, TMP2
4553 | lwz SAVE1, 4(TMP1)
4555 | checknil TMP0; beq >2
4559 | stfdx f14, BASE, RA
4561 | stwux SAVE0, RA, BASE
4566 |2: // Check for __index if table value is nil.
4567 | lwz TAB:TMP2, TAB:RB->metatable
4568 | cmplwi TAB:TMP2, 0
4569 | beq <1 // No metatable: done.
4570 | lbz TMP0, TAB:TMP2->nomm
4571 | andix. TMP0, TMP0, 1<<MM_index
4572 | bne <1 // 'no __index' flag set: done.
4576 | checkstr CARG2; bne ->vmeta_tgetv
4580 | b ->BC_TGETS_Z // String key?
4583 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
4584 | lwzux CARG1, RB, BASE
4587 | subfic TMP1, TMP1, -4
4589 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4590 | bne ->vmeta_tgets1
4592 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
4593 | lwz TMP0, TAB:RB->hmask
4594 | lwz TMP1, STR:RC->sid
4595 | lwz NODE:TMP2, TAB:RB->node
4596 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4597 | slwi TMP0, TMP1, 5
4598 | slwi TMP1, TMP1, 3
4599 | sub TMP1, TMP0, TMP1
4600 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4602 | lwz CARG1, NODE:TMP2->key
4603 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4604 | lwz CARG2, NODE:TMP2->val
4605 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
4606 | checkstr CARG1; bne >4
4607 | cmpw TMP0, STR:RC; bne >4
4608 | checknil CARG2; beq >5 // Key found, but nil value?
4610 | stwux CARG2, RA, BASE
4614 |4: // Follow hash chain.
4615 | lwz NODE:TMP2, NODE:TMP2->next
4616 | cmplwi NODE:TMP2, 0
4618 | // End of hash chain: key not found, nil result.
4621 |5: // Check for __index if table value is nil.
4622 | lwz TAB:TMP2, TAB:RB->metatable
4623 | cmplwi TAB:TMP2, 0
4624 | beq <3 // No metatable: done.
4625 | lbz TMP0, TAB:TMP2->nomm
4626 | andix. TMP0, TMP0, 1<<MM_index
4627 | bne <3 // 'no __index' flag set: done.
4631 | // RA = dst*8, RB = table*8, RC = index*8
4632 | lwzux CARG1, RB, BASE
4635 | checktab CARG1; bne ->vmeta_tgetb
4636 | lwz TMP1, TAB:RB->asize
4637 | lwz TMP2, TAB:RB->array
4638 | cmplw TMP0, TMP1; bge ->vmeta_tgetb
4640 | lwzx TMP1, TMP2, RC
4643 | lwzux TMP1, TMP2, RC
4646 | checknil TMP1; beq >5
4650 | stfdx f0, BASE, RA
4652 | stwux TMP1, RA, BASE
4657 |5: // Check for __index if table value is nil.
4658 | lwz TAB:TMP2, TAB:RB->metatable
4659 | cmplwi TAB:TMP2, 0
4660 | beq <1 // No metatable: done.
4661 | lbz TMP2, TAB:TMP2->nomm
4662 | andix. TMP2, TMP2, 1<<MM_index
4663 | bne <1 // 'no __index' flag set: done.
4664 | b ->vmeta_tgetb // Caveat: preserve TMP0!
4667 | // RA = dst*8, RB = table*8, RC = key*8
4669 | lwz TAB:CARG1, 4(RB)
4672 | lwz TMP0, TAB:CARG1->asize
4674 | lwz TMP1, TAB:CARG1->array
4677 | lwz TMP0, TAB:CARG1->asize
4679 | lwz TMP1, TAB:CARG1->array
4682 | slwi TMP2, CARG2, 3
4683 | ble ->vmeta_tgetr // In array part?
4685 | lfdx f14, TMP1, TMP2
4687 | lwzux SAVE0, TMP2, TMP1
4688 | lwz SAVE1, 4(TMP2)
4693 | stfdx f14, BASE, RA
4695 | stwux SAVE0, RA, BASE
4702 | // RA = src*8, RB = table*8, RC = key*8
4703 | lwzux CARG1, RB, BASE
4704 | lwzux CARG2, RC, BASE
4712 | checknum cr1, CARG2
4715 | lwz TMP0, TAB:RB->asize
4717 | lwz TMP1, TAB:RB->array
4722 | // Convert number key to integer, check for integerness and range.
4724 | fadd f2, f0, TOBIT
4726 | lwz TMP0, TAB:RB->asize
4727 | fsub f2, f2, TOBIT
4729 | lwz TMP1, TAB:RB->array
4731 | cmplw cr0, TMP0, TMP2
4732 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4733 | slwi TMP0, TMP2, 3
4735 | ble ->vmeta_tsetv // Integer key and in array part?
4736 | lwzx TMP2, TMP1, TMP0
4737 | lbz TMP3, TAB:RB->marked
4739 | lfdx f14, BASE, RA
4741 | add SAVE1, BASE, RA
4742 | lwz SAVE0, 0(SAVE1)
4743 | lwz SAVE1, 4(SAVE1)
4745 | checknil TMP2; beq >3
4747 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4749 | stfdx f14, TMP1, TMP0
4751 | stwux SAVE0, TMP1, TMP0
4752 | stw SAVE1, 4(TMP1)
4758 |3: // Check for __newindex if previous value is nil.
4759 | lwz TAB:TMP2, TAB:RB->metatable
4760 | cmplwi TAB:TMP2, 0
4761 | beq <1 // No metatable: done.
4762 | lbz TMP2, TAB:TMP2->nomm
4763 | andix. TMP2, TMP2, 1<<MM_newindex
4764 | bne <1 // 'no __newindex' flag set: done.
4768 | checkstr CARG2; bne ->vmeta_tsetv
4772 | b ->BC_TSETS_Z // String key?
4774 |7: // Possible table write barrier for the value. Skip valiswhite check.
4775 | barrierback TAB:RB, TMP3, TMP0
4779 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4780 | lwzux CARG1, RB, BASE
4783 | subfic TMP1, TMP1, -4
4785 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4786 | bne ->vmeta_tsets1
4788 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
4789 | lwz TMP0, TAB:RB->hmask
4790 | lwz TMP1, STR:RC->sid
4791 | lwz NODE:TMP2, TAB:RB->node
4792 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
4793 | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask
4795 | lfdx f14, BASE, RA
4797 | add CARG2, BASE, RA
4798 | lwz SAVE0, 0(CARG2)
4799 | lwz SAVE1, 4(CARG2)
4801 | slwi TMP0, TMP1, 5
4802 | slwi TMP1, TMP1, 3
4803 | sub TMP1, TMP0, TMP1
4804 | lbz TMP3, TAB:RB->marked
4805 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4807 | lwz CARG1, NODE:TMP2->key
4808 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4809 | lwz CARG2, NODE:TMP2->val
4810 | lwz NODE:TMP1, NODE:TMP2->next
4811 | checkstr CARG1; bne >5
4812 | cmpw TMP0, STR:RC; bne >5
4813 | checknil CARG2; beq >4 // Key found, but nil value?
4815 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4817 | stfd f14, NODE:TMP2->val
4819 | stw SAVE0, NODE:TMP2->val.u32.hi
4820 | stw SAVE1, NODE:TMP2->val.u32.lo
4826 |4: // Check for __newindex if previous value is nil.
4827 | lwz TAB:TMP1, TAB:RB->metatable
4828 | cmplwi TAB:TMP1, 0
4829 | beq <2 // No metatable: done.
4830 | lbz TMP0, TAB:TMP1->nomm
4831 | andix. TMP0, TMP0, 1<<MM_newindex
4832 | bne <2 // 'no __newindex' flag set: done.
4835 |5: // Follow hash chain.
4836 | cmplwi NODE:TMP1, 0
4837 | mr NODE:TMP2, NODE:TMP1
4839 | // End of hash chain: key not found, add a new one.
4841 | // But check for __newindex first.
4842 | lwz TAB:TMP1, TAB:RB->metatable
4843 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
4846 | cmplwi TAB:TMP1, 0
4848 | beq >6 // No metatable: continue.
4849 | lbz TMP0, TAB:TMP1->nomm
4850 | andix. TMP0, TMP0, 1<<MM_newindex
4851 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4854 | stw STR:RC, 4(CARG3)
4856 | stw TMP0, 0(CARG3)
4857 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4858 | // Returns TValue *.
4861 | stfd f14, 0(CRET1)
4863 | stw SAVE0, 0(CRET1)
4864 | stw SAVE1, 4(CRET1)
4866 | b <3 // No 2nd write barrier needed.
4868 |7: // Possible table write barrier for the value. Skip valiswhite check.
4869 | barrierback TAB:RB, TMP3, TMP0
4873 | // RA = src*8, RB = table*8, RC = index*8
4874 | lwzux CARG1, RB, BASE
4877 | checktab CARG1; bne ->vmeta_tsetb
4878 | lwz TMP1, TAB:RB->asize
4879 | lwz TMP2, TAB:RB->array
4880 | lbz TMP3, TAB:RB->marked
4883 | lfdx f14, BASE, RA
4885 | add CARG2, BASE, RA
4886 | lwz SAVE0, 0(CARG2)
4887 | lwz SAVE1, 4(CARG2)
4890 | lwzx TMP1, TMP2, RC
4891 | checknil TMP1; beq >5
4893 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4895 | stfdx f14, TMP2, RC
4897 | stwux SAVE0, RC, TMP2
4904 |5: // Check for __newindex if previous value is nil.
4905 | lwz TAB:TMP1, TAB:RB->metatable
4906 | cmplwi TAB:TMP1, 0
4907 | beq <1 // No metatable: done.
4908 | lbz TMP1, TAB:TMP1->nomm
4909 | andix. TMP1, TMP1, 1<<MM_newindex
4910 | bne <1 // 'no __newindex' flag set: done.
4911 | b ->vmeta_tsetb // Caveat: preserve TMP0!
4913 |7: // Possible table write barrier for the value. Skip valiswhite check.
4914 | barrierback TAB:RB, TMP3, TMP0
4918 | // RA = dst*8, RB = table*8, RC = key*8
4920 | lwz TAB:CARG2, 4(RB)
4923 | lbz TMP3, TAB:CARG2->marked
4924 | lwz TMP0, TAB:CARG2->asize
4926 | lwz TMP1, TAB:CARG2->array
4929 | lbz TMP3, TAB:CARG2->marked
4930 | lwz TMP0, TAB:CARG2->asize
4932 | lwz TMP1, TAB:CARG2->array
4934 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4938 | slwi TMP2, CARG3, 3
4940 | lfdx f14, BASE, RA
4942 | lwzux SAVE0, RA, BASE
4945 | ble ->vmeta_tsetr // In array part?
4948 | stfdx f14, TMP1, TMP2
4950 | stwux SAVE0, TMP1, TMP2
4951 | stw SAVE1, 4(TMP1)
4955 |7: // Possible table write barrier for the value. Skip valiswhite check.
4956 | barrierback TAB:CARG2, TMP3, TMP2
4962 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4965 | add TMP3, KBASE, RD
4966 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
4967 | addic. TMP0, MULTRES, -8
4968 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
4969 | srwi CARG3, TMP0, 3
4970 | beq >4 // Nothing to copy?
4971 | add CARG3, CARG3, TMP3
4972 | lwz TMP2, TAB:CARG2->asize
4973 | slwi TMP1, TMP3, 3
4974 | lbz TMP3, TAB:CARG2->marked
4976 | add TMP2, RA, TMP0
4977 | lwz TMP0, TAB:CARG2->array
4979 | add TMP1, TMP1, TMP0
4980 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4981 |3: // Copy result slots to table.
4989 | cmpw cr1, RA, TMP2
4993 | stw SAVE0, 0(TMP1)
4994 | stw SAVE1, 4(TMP1)
4996 | addi TMP1, TMP1, 8
5002 |5: // Need to resize array part.
5007 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
5008 | // Must not reallocate the stack.
5012 |7: // Possible table write barrier for any value. Skip valiswhite check.
5013 | barrierback TAB:CARG2, TMP3, TMP0
5017 /* -- Calls and vararg handling ----------------------------------------- */
5020 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
5021 | add NARGS8:RC, NARGS8:RC, MULTRES
5022 | // Fall through. Assumes BC_CALL follows.
5025 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
5027 | lwzux TMP0, BASE, RA
5028 | lwz LFUNC:RB, 4(BASE)
5029 | subi NARGS8:RC, NARGS8:RC, 8
5030 | addi BASE, BASE, 8
5031 | checkfunc TMP0; bne ->vmeta_call
5036 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
5037 | add NARGS8:RC, NARGS8:RC, MULTRES
5038 | // Fall through. Assumes BC_CALLT follows.
5041 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
5042 | lwzux TMP0, RA, BASE
5043 | lwz LFUNC:RB, 4(RA)
5044 | subi NARGS8:RC, NARGS8:RC, 8
5045 | lwz TMP1, FRAME_PC(BASE)
5050 | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
5051 | lbz TMP3, LFUNC:RB->ffid
5052 | xori TMP2, TMP1, FRAME_VARG
5053 | cmplwi cr1, NARGS8:RC, 0
5056 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
5058 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
5061 | addi TMP3, TMP2, 8
5065 | add CARG3, RA, TMP2
5066 | lwz CARG1, 0(CARG3)
5067 | lwz CARG2, 4(CARG3)
5069 | cmplw cr1, TMP3, NARGS8:RC
5071 | stfdx f0, BASE, TMP2
5073 | stwux CARG1, TMP2, BASE
5074 | stw CARG2, 4(TMP2)
5079 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
5084 |5: // Tailcall to a fast function with a Lua frame below.
5086 | decode_RA8 RA, INS
5087 | sub TMP1, BASE, RA
5088 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
5089 | lwz TMP1, LFUNC:TMP1->pc
5090 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
5093 |7: // Tailcall from a vararg function.
5094 | andix. TMP0, TMP2, FRAME_TYPEP
5095 | bne <1 // Vararg frame below?
5096 | sub BASE, BASE, TMP2 // Relocate BASE down.
5097 | lwz TMP1, FRAME_PC(BASE)
5098 | andix. TMP0, TMP1, FRAME_TYPE
5103 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
5105 | add BASE, BASE, RA
5106 | lwz TMP1, -24(BASE)
5107 | lwz LFUNC:RB, -20(BASE)
5112 | lwz CARG1, -8(BASE)
5113 | lwz CARG2, -4(BASE)
5114 | lwz CARG3, -16(BASE)
5115 | lwz CARG4, -12(BASE)
5117 | stw TMP1, 0(BASE) // Copy callable.
5118 | stw LFUNC:RB, 4(BASE)
5120 | li NARGS8:RC, 16 // Iterators get 2 arguments.
5122 | stfd f1, 16(BASE) // Copy control var.
5123 | stfdu f0, 8(BASE) // Copy state.
5125 | stw CARG1, 16(BASE) // Copy control var.
5126 | stw CARG2, 20(BASE)
5127 | stwu CARG3, 8(BASE) // Copy state.
5128 | stw CARG4, 4(BASE)
5135 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
5137 | // NYI on big-endian
5141 | lwz TAB:RB, -12(RA)
5142 | lwz RC, -4(RA) // Get index from control var.
5143 | lwz TMP0, TAB:RB->asize
5144 | lwz TMP1, TAB:RB->array
5146 |1: // Traverse array part.
5149 | bge >5 // Index points after array part?
5150 | lwzx TMP2, TMP1, TMP3
5152 | lfdx f0, TMP1, TMP3
5154 | lwzux CARG1, TMP3, TMP1
5155 | lwz CARG2, 4(TMP3)
5167 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
5174 | decode_RD4 TMP1, INS
5175 | stw RC, -4(RA) // Update control var.
5176 | add PC, TMP1, TMP3
5183 |4: // Skip holes in array part.
5187 |5: // Traverse hash part.
5188 | lwz TMP1, TAB:RB->hmask
5190 | lwz TMP2, TAB:RB->node
5192 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
5196 | sub TMP3, TMP3, RB
5197 | lwzx RB, TMP2, TMP3
5199 | lfdx f0, TMP2, TMP3
5201 | add CARG3, TMP2, TMP3
5202 | lwz CARG1, 0(CARG3)
5203 | lwz CARG2, 4(CARG3)
5205 | add NODE:TMP3, TMP2, TMP3
5210 | lfd f1, NODE:TMP3->key
5212 | lwz CARG3, NODE:TMP3->key.u32.hi
5213 | lwz CARG4, NODE:TMP3->key.u32.lo
5215 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
5223 | decode_RD4 TMP1, INS
5231 | add PC, TMP1, TMP2
5232 | stw RC, -4(RA) // Update control var.
5235 |7: // Skip holes in hash part.
5241 | // RA = base*8, RD = target (points to ITERN)
5244 | lwz CFUNC:TMP1, -20(RA)
5247 | cmpwi cr0, TMP2, LJ_TTAB
5248 | cmpwi cr1, TMP0, LJ_TFUNC
5249 | cmpwi cr6, TMP3, LJ_TNIL
5251 | lbz TMP1, CFUNC:TMP1->ffid
5252 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
5253 | cmpwi cr7, TMP1, FF_next_N
5255 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
5256 | add TMP3, PC, TMP0
5258 | lus TMP1, (LJ_KEYINDEX >> 16)
5259 | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff)
5260 | stw ZERO, -4(RA) // Initialize control var.
5262 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
5265 |5: // Despecialize bytecode if any of the checks fail.
5269 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
5270 | // NYI on big-endian: unpatch JLOOP.
5276 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
5277 | lwz TMP0, FRAME_PC(BASE)
5280 | addi RC, RC, FRAME_VARG
5282 | subi TMP3, BASE, 8 // TMP3 = vtop
5283 | sub RC, RC, TMP0 // RC = vbase
5284 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
5287 | sub TMP1, TMP3, RC
5290 | sub. TMP1, TMP3, RC
5292 | beq cr1, >5 // Copy all varargs?
5293 | subi TMP2, TMP2, 16
5294 | ble >2 // No vararg slots?
5295 |1: // Copy vararg slots to destination slots.
5310 | cmplw cr1, RC, TMP3
5311 | bge >3 // All destination slots filled?
5313 | blt cr1, <1 // More vararg slots?
5314 |2: // Fill up remainder with nil.
5322 |5: // Copy all varargs.
5323 | lwz TMP0, L->maxstack
5324 | li MULTRES, 8 // MULTRES = (0+1)*8
5325 | bley <3 // No vararg slots?
5326 | add TMP2, RA, TMP1
5328 | addi MULTRES, TMP1, 8
5346 | blt <6 // More vararg slots?
5349 |7: // Grow stack for varargs.
5352 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
5356 | srwi CARG2, TMP1, 3
5357 | bl extern lj_state_growstack // (lua_State *L, int n)
5360 | add RC, BASE, SAVE0
5361 | subi TMP3, BASE, 8
5365 /* -- Returns ----------------------------------------------------------- */
5368 | // RA = results*8, RD = extra_nresults*8
5369 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
5370 | // Fall through. Assumes BC_RET follows.
5374 | // RA = results*8, RD = (nresults+1)*8
5375 | lwz PC, FRAME_PC(BASE)
5379 | andix. TMP0, PC, FRAME_TYPE
5380 | xori TMP1, PC, FRAME_VARG
5384 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
5387 | subi TMP2, BASE, 8
5389 | decode_RB8 RB, INS
5393 | addi TMP3, TMP1, 8
5397 | add CARG3, RA, TMP1
5398 | lwz CARG1, 0(CARG3)
5399 | lwz CARG2, 4(CARG3)
5403 | stfdx f0, TMP2, TMP1
5405 | add CARG3, TMP2, TMP1
5406 | stw CARG1, 0(CARG3)
5407 | stw CARG2, 4(CARG3)
5410 | addi TMP1, TMP3, 8
5414 | add CARG3, RA, TMP3
5415 | lwz CARG1, 0(CARG3)
5416 | lwz CARG2, 4(CARG3)
5420 | stfdx f1, TMP2, TMP3
5422 | add CARG3, TMP2, TMP3
5423 | stw CARG1, 0(CARG3)
5424 | stw CARG2, 4(CARG3)
5430 | decode_RA8 RA, INS
5432 | sub BASE, TMP2, RA
5433 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
5435 | lwz TMP1, LFUNC:TMP1->pc
5436 | lwz KBASE, PC2PROTO(k)(TMP1)
5439 |6: // Fill up results with nil.
5442 | stwx TISNIL, TMP2, TMP1
5445 |->BC_RETV_Z: // Non-standard return case.
5446 | andix. TMP2, TMP1, FRAME_TYPEP
5448 | // Return from vararg function: relocate BASE down.
5449 | sub BASE, BASE, TMP1
5450 | lwz PC, FRAME_PC(BASE)
5454 case BC_RET0: case BC_RET1:
5455 | // RA = results*8, RD = (nresults+1)*8
5456 | lwz PC, FRAME_PC(BASE)
5459 | andix. TMP0, PC, FRAME_TYPE
5460 | xori TMP1, PC, FRAME_VARG
5464 | subi TMP2, BASE, 8
5465 | decode_RB8 RB, INS
5466 if (op == BC_RET1) {
5473 | stw CARG1, 0(TMP2)
5474 | stw CARG2, 4(TMP2)
5479 | decode_RA8 RA, INS
5481 | sub BASE, TMP2, RA
5482 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
5484 | lwz TMP1, LFUNC:TMP1->pc
5485 | lwz KBASE, PC2PROTO(k)(TMP1)
5488 |6: // Fill up results with nil.
5491 | stwx TISNIL, TMP2, TMP1
5495 /* -- Loops and branches ------------------------------------------------ */
5501 | // Fall through. Assumes BC_IFORL follows.
5511 | // RA = base*8, RD = target (after end of loop or start of loop)
5512 vk = (op == BC_IFORL || op == BC_JFORL);
5515 | lwzux TMP1, RA, BASE
5516 | lwz CARG1, FORL_IDX*8+4(RA)
5517 | cmplw cr0, TMP1, TISNUM
5519 | lwz CARG3, FORL_STEP*8+4(RA)
5522 | // Need to check overflow for (a<<32) + (b<<32).
5523 | rldicr TMP0, CARG1, 32, 31
5524 | rldicr TMP2, CARG3, 32, 31
5525 | add CARG1, CARG1, CARG3
5526 | addo. TMP0, TMP0, TMP2
5528 | addo. CARG1, CARG1, CARG3
5530 | cmpwi cr6, CARG3, 0
5531 | lwz CARG2, FORL_STOP*8+4(RA)
5534 | stw CARG1, FORL_IDX*8+4(RA)
5536 | lwz SAVE0, FORL_STEP*8(RA)
5537 | lwz CARG3, FORL_STEP*8+4(RA)
5538 | lwz TMP2, FORL_STOP*8(RA)
5539 | lwz CARG2, FORL_STOP*8+4(RA)
5540 | cmplw cr7, SAVE0, TISNUM
5541 | cmplw cr1, TMP2, TISNUM
5542 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
5543 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
5544 | cmpwi cr6, CARG3, 0
5550 | stw TISNUM, FORL_EXT*8(RA)
5551 if (op != BC_JFORL) {
5554 | stw CARG1, FORL_EXT*8+4(RA)
5555 if (op != BC_JFORL) {
5558 if (op == BC_FORI) {
5559 | bgt >3 // See FP loop below.
5560 } else if (op == BC_JFORI) {
5561 | addis PC, RD, -(BCBIAS_J*4 >> 16)
5563 } else if (op == BC_IFORL) {
5565 | addis PC, RD, -(BCBIAS_J*4 >> 16)
5571 |5: // Invert check for negative step.
5575 |6: // Potential overflow.
5576 | checkov TMP0, <4 // Ignore unrelated overflow.
5584 | lfd f1, FORL_IDX*8(RA)
5586 | lwz CARG1, FORL_IDX*8(RA)
5587 | lwz CARG2, FORL_IDX*8+4(RA)
5590 | lfdux f1, RA, BASE
5593 | lfd f3, FORL_STEP*8(RA)
5594 | lfd f2, FORL_STOP*8(RA)
5596 | stfd f1, FORL_IDX*8(RA)
5598 | lwz CARG3, FORL_STEP*8(RA)
5599 | lwz CARG4, FORL_STEP*8+4(RA)
5603 | stw CRET1, FORL_IDX*8(RA)
5604 | stw CRET2, FORL_IDX*8+4(RA)
5605 | lwz CARG3, FORL_STOP*8(RA)
5606 | lwz CARG4, FORL_STOP*8+4(RA)
5608 | lwz SAVE0, FORL_STEP*8(RA)
5613 | lwzux TMP1, RA, BASE
5614 | lwz SAVE0, FORL_STEP*8(RA)
5615 | lwz TMP2, FORL_STOP*8(RA)
5616 | cmplw cr0, TMP1, TISNUM
5617 | cmplw cr7, SAVE0, TISNUM
5618 | cmplw cr1, TMP2, TISNUM
5621 | lfd f1, FORL_IDX*8(RA)
5623 | lwz CARG1, FORL_IDX*8(RA)
5624 | lwz CARG2, FORL_IDX*8+4(RA)
5626 | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
5627 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
5629 | lfd f2, FORL_STOP*8(RA)
5631 | lwz CARG3, FORL_STOP*8(RA)
5632 | lwz CARG4, FORL_STOP*8+4(RA)
5636 | cmpwi cr6, SAVE0, 0
5637 if (op != BC_JFORL) {
5641 | stfd f1, FORL_EXT*8(RA)
5643 | stw CARG1, FORL_EXT*8(RA)
5644 | stw CARG2, FORL_EXT*8+4(RA)
5646 if (op != BC_JFORL) {
5657 if (op == BC_JFORI) {
5658 | addis PC, RD, -(BCBIAS_J*4 >> 16)
5661 if (op == BC_FORI) {
5663 } else if (op == BC_IFORL) {
5670 | addis PC, RD, -(BCBIAS_J*4 >> 16)
5671 } else if (op == BC_JFORI) {
5682 |5: // Negative step.
5683 if (op == BC_FORI) {
5685 |3: // Used by integer loop, too.
5686 | addis PC, RD, -(BCBIAS_J*4 >> 16)
5687 } else if (op == BC_IFORL) {
5689 } else if (op == BC_JFORI) {
5695 if (op == BC_JFORI) {
5698 | decode_RD8 RD, INS
5707 | // Fall through. Assumes BC_IITERL follows.
5715 | // RA = base*8, RD = target
5716 | lwzux TMP1, RA, BASE
5718 | checknil TMP1; beq >1 // Stop if iterator returned nil.
5719 if (op == BC_JITERL) {
5724 | branch_RD // Otherwise save control var + branch.
5733 | // RA = base*8, RD = target (loop extent)
5734 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5735 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5739 | // Fall through. Assumes BC_ILOOP follows.
5743 | // RA = base*8, RD = target (loop extent)
5749 | // RA = base*8 (ignored), RD = traceno*8
5750 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
5752 | // Traces on PPC don't store the trace number, so use 0.
5753 | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH)
5754 | lwzx TRACE:TMP2, TMP1, RD
5756 | lp TMP2, TRACE:TMP2->mcode
5757 | stw BASE, DISPATCH_GL(jit_base)(DISPATCH)
5759 | addi JGL, DISPATCH, GG_DISP2G+32768
5760 | stw L, DISPATCH_GL(tmpbuf.L)(DISPATCH)
5766 | // RA = base*8 (only used by trace recorder), RD = target
5771 /* -- Function headers -------------------------------------------------- */
5777 case BC_FUNCV: /* NYI: compiled vararg functions. */
5778 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
5786 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5787 | lwz TMP2, L->maxstack
5788 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
5789 | lwz KBASE, -4+PC2PROTO(k)(PC)
5791 | slwi TMP1, TMP1, 3
5792 | bgt ->vm_growstack_l
5793 if (op != BC_JFUNCF) {
5797 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
5799 if (op == BC_JFUNCF) {
5800 | decode_RD8 RD, INS
5806 |3: // Clear missing parameters.
5807 | stwx TISNIL, BASE, NARGS8:RC
5808 | addi NARGS8:RC, NARGS8:RC, 8
5816 | NYI // NYI: compiled vararg functions
5817 break; /* NYI: compiled vararg functions. */
5820 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
5821 | lwz TMP2, L->maxstack
5822 | add TMP1, BASE, RC
5824 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
5825 | addi TMP3, RC, 8+FRAME_VARG
5826 | lwz KBASE, -4+PC2PROTO(k)(PC)
5828 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
5829 | bge ->vm_growstack_l
5830 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
5835 | addi BASE, TMP1, 8
5838 | cmplw RA, RC // Less args than parameters?
5842 | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
5845 | addic. TMP2, TMP2, -1
5847 | stw TMP3, 12(TMP1)
5848 | addi TMP1, TMP1, 8
5853 |4: // Clear missing parameters.
5860 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
5861 if (op == BC_FUNCC) {
5862 | lp RD, CFUNC:RB->f
5864 | lp RD, DISPATCH_GL(wrapf)(DISPATCH)
5866 | add TMP1, RA, NARGS8:RC
5867 | lwz TMP2, L->maxstack
5868 | .toc lp TMP3, 0(RD)
5869 | add RC, BASE, NARGS8:RC
5879 if (op == BC_FUNCCW) {
5880 | lp CARG2, CFUNC:RB->f
5883 | bgt ->vm_growstack_c // Need to grow stack.
5884 | .toc lp TOCREG, TOC_OFS(RD)
5885 | .tocenv lp ENVREG, ENV_OFS(RD)
5887 | bctrl // (lua_State *L [, lua_CFunction f])
5888 | // Returns nresults.
5890 | .toc ld TOCREG, SAVE_TOC
5894 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
5895 | stw L, DISPATCH_GL(cur_L)(DISPATCH)
5896 | sub RA, TMP1, RD // RA = L->top - nresults*8
5901 /* ---------------------------------------------------------------------- */
5904 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5910 static int build_backend(BuildCtx *ctx)
5914 dasm_growpc(Dst, BC__MAX);
5916 build_subroutines(ctx);
5919 for (op = 0; op < BC__MAX; op++)
5920 build_ins(ctx, (BCOp)op, op);
5925 /* Emit pseudo frame-info for all assembler functions. */
5926 static void emit_asm_debug(BuildCtx *ctx)
5928 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5930 switch (ctx->mode) {
5932 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5935 "\t.long .LECIE0-.LSCIE0\n"
5937 "\t.long 0xffffffff\n"
5943 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5948 "\t.long .LEFDE0-.LASFDE0\n"
5950 "\t.long .Lframe0\n"
5953 "\t.byte 0xe\n\t.uleb128 %d\n"
5954 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5955 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5956 fcofs, CFRAME_SIZE);
5957 for (i = 14; i <= 31; i++)
5959 "\t.byte %d\n\t.uleb128 %d\n"
5960 "\t.byte %d\n\t.uleb128 %d\n",
5961 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5968 "\t.long .LEFDE1-.LASFDE1\n"
5970 "\t.long .Lframe0\n"
5972 "\t.long .lj_vm_ffi_call\n"
5974 "\t.long lj_vm_ffi_call\n"
5977 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5978 "\t.byte 0x8e\n\t.uleb128 2\n"
5979 "\t.byte 0xd\n\t.uleb128 0xe\n"
5981 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5984 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5987 "\t.long .LECIE1-.LSCIE1\n"
5991 "\t.string \"zPR\"\n"
5995 "\t.uleb128 6\n" /* augmentation length */
5996 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5997 "\t.long lj_err_unwind_dwarf-.\n"
5998 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5999 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
6004 "\t.long .LEFDE2-.LASFDE2\n"
6006 "\t.long .LASFDE2-.Lframe1\n"
6007 "\t.long .Lbegin-.\n"
6009 "\t.uleb128 0\n" /* augmentation length */
6010 "\t.byte 0xe\n\t.uleb128 %d\n"
6011 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
6012 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
6013 fcofs, CFRAME_SIZE);
6014 for (i = 14; i <= 31; i++)
6016 "\t.byte %d\n\t.uleb128 %d\n"
6017 "\t.byte %d\n\t.uleb128 %d\n",
6018 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
6025 "\t.long .LECIE2-.LSCIE2\n"
6029 "\t.string \"zR\"\n"
6033 "\t.uleb128 1\n" /* augmentation length */
6034 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6035 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
6040 "\t.long .LEFDE3-.LASFDE3\n"
6042 "\t.long .LASFDE3-.Lframe2\n"
6043 "\t.long lj_vm_ffi_call-.\n"
6045 "\t.uleb128 0\n" /* augmentation length */
6046 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
6047 "\t.byte 0x8e\n\t.uleb128 2\n"
6048 "\t.byte 0xd\n\t.uleb128 0xe\n"
6050 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);