1 |// Low-level VM code for PowerPC CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
10 |.globalnames globnames
11 |.externnames extnames
13 |// Note: The ragged indentation of the instructions is intentional.
14 |// The starting columns indicate data dependencies.
16 |//-----------------------------------------------------------------------
18 |// DynASM defines used by the PPC port:
20 |// P64 64 bit pointers (only for GPR64 testing).
21 |// Note: a full PPC64 _LP64 port is not planned.
22 |// GPR64 64 bit registers (but possibly 32 bit pointers, e.g. PS3).
23 |// Affects reg saves, stack layout, carry/overflow/dot flags etc.
24 |// FRAME32 Use 32 bit frame layout, even with GPR64 (Xbox 360).
25 |// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3).
26 |// Function pointers are really a struct: code, TOC, env (optional).
27 |// TOCENV Function pointers have an environment pointer, too (not on PS3).
28 |// PPE Power Processor Element of Cell (PS3) or Xenon (Xbox 360).
29 |// Must avoid (slow) micro-coded instructions.
34 |.macro lpx, a, b, c; ldx a, b, c; .endmacro
35 |.macro lp, a, b; ld a, b; .endmacro
36 |.macro stp, a, b; std a, b; .endmacro
37 |.define decode_OPP, decode_OP8
39 |// Missing: Calling conventions, 64 bit regs, TOC.
40 |.error lib_ffi not yet implemented for PPC64
43 |.macro lpx, a, b, c; lwzx a, b, c; .endmacro
44 |.macro lp, a, b; lwz a, b; .endmacro
45 |.macro stp, a, b; stw a, b; .endmacro
46 |.define decode_OPP, decode_OP4
49 |// Convenience macros for TOC handling.
51 |// Linker needs a TOC patch area for every external call relocation.
52 |.macro blex, target; bl extern target@plt; nop; .endmacro
53 |.macro .toc, a, b; a, b; .endmacro
62 |.macro blex, target; bl extern target@plt; .endmacro
63 |.macro .toc, a, b; .endmacro
65 |.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro
67 |.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro
69 |.macro andix., y, a, i
71 | rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i)
87 |.macro checkov, reg, noov
101 |//-----------------------------------------------------------------------
103 |// Fixed register assignments for the interpreter.
104 |// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA)
106 |// The following must be C callee-save (but BASE is often refetched).
107 |.define BASE, r14 // Base of current Lua stack frame.
108 |.define KBASE, r15 // Constants of current Lua function.
109 |.define PC, r16 // Next PC.
110 |.define DISPATCH, r17 // Opcode dispatch table.
111 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
112 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
113 |.define JGL, r31 // On-trace: global_State + 32768.
115 |// Constants for type-comparisons, stores and conversions. C callee-save.
119 |.define TOBIT, f30 // 2^52 + 2^51.
120 |.define TONUM, f31 // 2^52 + 2^51 + 2^31.
122 |// The following temporaries are not saved across C calls, except for RA.
123 |.define RA, r20 // Callee-save.
127 |.define INS, r7 // Overlaps CARG5.
132 |.define TMP3, r6 // Overlaps CARG4.
134 |// Saved temporaries.
137 |// Calling conventions.
141 |.define CARG4, r6 // Overlaps TMP3.
142 |.define CARG5, r7 // Overlaps INS.
150 |.define TOCREG, r2 // TOC register (only used by C code).
151 |.define ENVREG, r11 // Environment pointer (nested C functions).
153 |// Stack layout while in interpreter. Must match with lj_frame.h.
157 |// 456(sp) // \ 32/64 bit C frame info
158 |.define TONUM_LO, 452(sp) // |
159 |.define TONUM_HI, 448(sp) // |
160 |.define TMPD_LO, 444(sp) // |
161 |.define TMPD_HI, 440(sp) // |
162 |.define SAVE_CR, 432(sp) // | 64 bit CR save.
163 |.define SAVE_ERRF, 424(sp) // > Parameter save area.
164 |.define SAVE_NRES, 420(sp) // |
165 |.define SAVE_L, 416(sp) // |
166 |.define SAVE_PC, 412(sp) // |
167 |.define SAVE_MULTRES, 408(sp) // |
168 |.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain.
169 |// 392(sp) // Reserved.
170 |.define CFRAME_SPACE, 384 // Delta for sp.
171 |// Back chain for sp: 384(sp) <-- sp entering interpreter
172 |.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part.
173 |.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves.
174 |.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves.
175 |// 80(sp) // Needed for 16 byte stack frame alignment.
176 |// 16(sp) // Callee parameter save area (ABI mandated).
177 |// 8(sp) // Reserved
178 |// Back chain for sp: 0(sp) <-- sp while in interpreter
179 |// 32 bit sp stored in hi-part of 0(sp).
181 |.define TMPD_BLO, 447(sp)
182 |.define TMPD, TMPD_HI
183 |.define TONUM_D, TONUM_HI
187 |// 508(sp) // \ 32 bit C frame info.
188 |.define SAVE_ERRF, 472(sp) // |
189 |.define SAVE_NRES, 468(sp) // |
190 |.define SAVE_L, 464(sp) // > Parameter save area.
191 |.define SAVE_PC, 460(sp) // |
192 |.define SAVE_MULTRES, 456(sp) // |
193 |.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain.
194 |.define SAVE_LR, 416(sp)
195 |.define CFRAME_SPACE, 400 // Delta for sp.
196 |// Back chain for sp: 400(sp) <-- sp entering interpreter
197 |.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves.
198 |.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves.
199 |// 48(sp) // Callee parameter save area (ABI mandated).
200 |.define SAVE_TOC, 40(sp) // TOC save area.
201 |.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated).
202 |.define TMPD_HI, 32(sp) // /
203 |.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated).
204 |.define TONUM_HI, 24(sp) // /
205 |// Next frame lr: 16(sp)
206 |.define SAVE_CR, 8(sp) // 64 bit CR save.
207 |// Back chain for sp: 0(sp) <-- sp while in interpreter
209 |.define TMPD_BLO, 39(sp)
210 |.define TMPD, TMPD_HI
211 |.define TONUM_D, TONUM_HI
216 |.define SAVE_LR, 276(sp)
217 |.define CFRAME_SPACE, 272 // Delta for sp.
218 |// Back chain for sp: 272(sp) <-- sp entering interpreter
219 |.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves.
220 |.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves.
221 |.define SAVE_CR, 52(sp) // 32 bit CR save.
222 |.define SAVE_ERRF, 48(sp) // 32 bit C frame info.
223 |.define SAVE_NRES, 44(sp)
224 |.define SAVE_CFRAME, 40(sp)
225 |.define SAVE_L, 36(sp)
226 |.define SAVE_PC, 32(sp)
227 |.define SAVE_MULTRES, 28(sp)
228 |.define UNUSED1, 24(sp)
229 |.define TMPD_LO, 20(sp)
230 |.define TMPD_HI, 16(sp)
231 |.define TONUM_LO, 12(sp)
232 |.define TONUM_HI, 8(sp)
233 |// Next frame lr: 4(sp)
234 |// Back chain for sp: 0(sp) <-- sp while in interpreter
236 |.define TMPD_BLO, 23(sp)
237 |.define TMPD, TMPD_HI
238 |.define TONUM_D, TONUM_HI
244 | std r..reg, SAVE_GPR_+(reg-14)*8(sp)
246 | stw r..reg, SAVE_GPR_+(reg-14)*4(sp)
248 | stfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
252 | ld r..reg, SAVE_GPR_+(reg-14)*8(sp)
254 | lwz r..reg, SAVE_GPR_+(reg-14)*4(sp)
256 | lfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
260 |.if GPR64 and not FRAME32
261 | stdu sp, -CFRAME_SPACE(sp)
263 | stwu sp, -CFRAME_SPACE(sp)
265 | save_ 14; save_ 15; save_ 16
267 | save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22
268 |.if GPR64 and not FRAME32
273 | save_ 23; save_ 24; save_ 25
275 | save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31
281 | .toc std TOCREG, SAVE_TOC
285 |.if GPR64 and not FRAME32
295 | rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19
297 |.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif
298 | rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25
299 |.if PPE; mtocrf 0x10, r12; .endif
300 | rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31
301 |.if PPE; mtocrf 0x08, r12; .endif
302 | addi sp, sp, CFRAME_SPACE
305 |// Type definitions. Some of these are only used for documentation.
306 |.type L, lua_State, LREG
307 |.type GL, global_State
308 |.type TVALUE, TValue
312 |.type LFUNC, GCfuncL
313 |.type CFUNC, GCfuncC
314 |.type PROTO, GCproto
315 |.type UPVAL, GCupval
318 |.type TRACE, GCtrace
320 |//-----------------------------------------------------------------------
322 |// These basic macros should really be part of DynASM.
323 |.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro
324 |.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro
325 |.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro
326 |.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro
327 |.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro
329 |// Trap for not-yet-implemented parts.
330 |.macro NYI; tw 4, sp, sp; .endmacro
332 |// int/FP conversions.
333 |.macro tonum_i, freg, reg
334 | xoris reg, reg, 0x8000
337 | fsub freg, freg, TONUM
340 |.macro tonum_u, freg, reg
343 | fsub freg, freg, TOBIT
346 |.macro toint, reg, freg, tmpfreg
347 | fctiwz tmpfreg, freg
352 |.macro toint, reg, freg
353 | toint reg, freg, freg
356 |//-----------------------------------------------------------------------
358 |// Access to frame relative to BASE.
359 |.define FRAME_PC, -8
360 |.define FRAME_FUNC, -4
362 |// Instruction decode.
363 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
364 |.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro
365 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
366 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
367 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
368 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
370 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
371 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
373 |// Instruction fetch.
378 |// Instruction decode+dispatch. Note: optimized for e300!
380 | decode_OPP TMP1, INS
381 | lpx TMP0, DISPATCH, TMP1
394 |// Instruction footer.
396 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
397 | .define ins_next, ins_NEXT
398 | .define ins_next_, ins_NEXT
399 | .define ins_next1, ins_NEXT1
400 | .define ins_next2, ins_NEXT2
402 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
403 | // Affects only certain kinds of benchmarks (and only with -j off).
418 |// Call decode and dispatch.
420 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
421 | lwz PC, LFUNC:RB->pc
424 | decode_OPP TMP1, INS
426 | lpx TMP0, DISPATCH, TMP1
433 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
434 | stw PC, FRAME_PC(BASE)
438 |//-----------------------------------------------------------------------
440 |// Macros to test operand types.
441 |.macro checknum, reg; cmplw reg, TISNUM; .endmacro
442 |.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro
443 |.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro
444 |.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro
445 |.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro
446 |.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro
450 | addis PC, PC, -(BCBIAS_J*4 >> 16)
454 |// Assumes DISPATCH is relative to GL.
455 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
456 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
458 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
460 |.macro hotcheck, delta, target
461 | rlwinm TMP1, PC, 31, 25, 30
462 | addi TMP1, TMP1, GG_DISP2HOT
463 | lhzx TMP2, DISPATCH, TMP1
464 | addic. TMP2, TMP2, -delta
465 | sthx TMP2, DISPATCH, TMP1
470 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
474 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
477 |// Set current VM state. Uses TMP0.
478 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
479 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
481 |// Move table write barrier back. Overwrites mark and tmp.
482 |.macro barrierback, tab, mark, tmp
483 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
484 | // Assumes LJ_GC_BLACK is 0x04.
485 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
486 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
487 | stb mark, tab->marked
488 | stw tmp, tab->gclist
491 |//-----------------------------------------------------------------------
493 /* Generate subroutines used by opcodes and other parts of the VM. */
494 /* The .code_sub section should be last to help static branch prediction. */
495 static void build_subroutines(BuildCtx *ctx)
499 |//-----------------------------------------------------------------------
500 |//-- Return handling ----------------------------------------------------
501 |//-----------------------------------------------------------------------
504 | // See vm_return. Also: TMP2 = previous base.
505 | andix. TMP0, PC, FRAME_P
507 | beq ->cont_dispatch
509 | // Return from pcall or xpcall fast func.
510 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
511 | mr BASE, TMP2 // Restore caller base.
512 | // Prepending may overwrite the pcall frame, so do it at the end.
513 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
516 | addi RD, RD, 8 // RD = (nresults+1)*8.
517 | andix. TMP0, PC, FRAME_TYPE
519 | li CRET1, LUA_YIELD
520 | beq cr1, ->vm_unwind_c_eh
522 | beq ->BC_RET_Z // Handle regular return to Lua.
525 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
526 | // TMP0 = PC & FRAME_TYPE
527 | cmpwi TMP0, FRAME_C
528 | rlwinm TMP2, PC, 0, 0, 28
530 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
533 | addic. TMP1, RD, -8
535 | lwz TMP2, SAVE_NRES
541 | addic. TMP1, TMP1, -8
549 | cmpw TMP2, RD // More/less results wanted?
552 | stp BASE, L->top // Store new top.
555 | lp TMP0, SAVE_CFRAME // Restore previous C frame.
556 | li CRET1, 0 // Ok return status for vm_pcall.
557 | stp TMP0, L->cframe
564 | ble >7 // Less results wanted?
565 | // More results wanted. Check stack size and fill up results with nil.
566 | lwz TMP1, L->maxstack
569 | stw TISNIL, 0(BASE)
574 |7: // Less results wanted.
575 | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case?
577 | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1
578 | and TMP0, TMP0, TMP1
579 | sub BASE, BASE, TMP0 // Either keep top or shrink it.
582 |8: // Corner case: need to grow stack for filling up results.
583 | // This can happen if:
584 | // - A C function grows the stack (a lot).
585 | // - The GC shrinks the stack in between.
586 | // - A return back from a lua_call() with (high) nresults adjustment.
587 | stp BASE, L->top // Save current top held in BASE (yes).
589 | srwi CARG2, TMP2, 3
591 | bl extern lj_state_growstack // (lua_State *L, int n)
592 | lwz TMP2, SAVE_NRES
595 | lp BASE, L->top // Need the (realloced) L->top in BASE.
598 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
599 | // (void *cframe, int errcode)
602 |->vm_unwind_c_eh: // Landing pad for external unwinder.
604 | .toc ld TOCREG, SAVE_TOC
605 | li TMP0, ~LJ_VMST_C
606 | lwz GL:TMP1, L->glref
607 | stw TMP0, GL:TMP1->vmstate
610 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
613 | rldicr sp, CARG1, 0, 61
615 | rlwinm sp, CARG1, 0, 0, 29
617 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
619 | .toc ld TOCREG, SAVE_TOC
620 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
622 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
623 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
627 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
631 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
632 | la RA, -8(BASE) // Results start at BASE-8.
634 | addi DISPATCH, DISPATCH, GG_G2DISP
635 | stw TMP1, 0(RA) // Prepend false to error message.
636 | li RD, 16 // 2 results: false + error message.
641 |//-----------------------------------------------------------------------
642 |//-- Grow stack for calls -----------------------------------------------
643 |//-----------------------------------------------------------------------
645 |->vm_growstack_c: // Grow stack for C function.
646 | li CARG2, LUA_MINSTACK
649 |->vm_growstack_l: // Grow stack for Lua function.
650 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
654 | addi PC, PC, 4 // Must point after first instruction.
658 | // L->base = new base, L->top = top
661 | bl extern lj_state_growstack // (lua_State *L, int n)
664 | lwz LFUNC:RB, FRAME_FUNC(BASE)
666 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
667 | ins_callt // Just retry the call.
669 |//-----------------------------------------------------------------------
670 |//-- Entry points into the assembler VM ---------------------------------
671 |//-----------------------------------------------------------------------
673 |->vm_resume: // Setup C frame and resume thread.
674 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
677 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
679 | lbz TMP1, L->status
682 | addi TMP0, sp, CFRAME_RESUME
683 | addi DISPATCH, DISPATCH, GG_G2DISP
684 | stw CARG3, SAVE_NRES
686 | stw CARG3, SAVE_ERRF
687 | stp TMP0, L->cframe
688 | stp CARG3, SAVE_CFRAME
689 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
692 | // Resume after yield (like a return).
695 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
697 | lwz PC, FRAME_PC(BASE)
698 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
699 | stb CARG3, L->status
701 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
705 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
711 | andix. TMP0, PC, FRAME_TYPE
718 |->vm_pcall: // Setup protected C frame and enter VM.
719 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
722 | stw CARG4, SAVE_ERRF
725 |->vm_call: // Setup C frame and enter VM.
726 | // (lua_State *L, TValue *base, int nres1)
730 |1: // Entry point for vm_pcall above (PC = ftype).
731 | lp TMP1, L:CARG1->cframe
732 | stw CARG3, SAVE_NRES
736 | stp sp, L->cframe // Add our C frame to cframe chain.
737 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
738 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
739 | stp TMP1, SAVE_CFRAME
740 | addi DISPATCH, DISPATCH, GG_G2DISP
742 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
743 | lp TMP2, L->base // TMP2 = old base (used in vmeta_call).
744 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
746 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
750 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
752 | sub PC, PC, TMP2 // PC = frame delta + frame type
754 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
755 | sub NARGS8:RC, TMP1, BASE
763 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
764 | lwz TMP0, FRAME_PC(BASE)
765 | lwz LFUNC:RB, FRAME_FUNC(BASE)
766 | checkfunc TMP0; bne ->vmeta_call
768 |->vm_call_dispatch_f:
770 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
772 |->vm_cpcall: // Setup protected C frame, call C.
773 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
776 | lwz TMP0, L:CARG1->stack
779 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
780 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
782 | stp sp, L->cframe // Add our C frame to cframe chain.
783 | .toc lp CARG4, 0(CARG4)
785 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
786 | stw TMP2, SAVE_ERRF // No error function.
787 | stp TMP1, SAVE_CFRAME
789 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
796 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
798 | addi DISPATCH, DISPATCH, GG_G2DISP
799 | bne <3 // Else continue with the call.
800 | b ->vm_leave_cp // No base? Just remove C frame.
802 |//-----------------------------------------------------------------------
803 |//-- Metamethod handling ------------------------------------------------
804 |//-----------------------------------------------------------------------
806 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
807 |// stack, so BASE doesn't need to be reloaded across these calls.
809 |//-- Continuation dispatch ----------------------------------------------
812 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
813 | lwz TMP0, -12(BASE) // Continuation.
815 | mr BASE, TMP2 // Restore caller BASE.
816 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
820 | lwz PC, -16(RB) // Restore PC from [cont|PC].
822 | lwz TMP1, LFUNC:TMP1->pc
823 | stwx TISNIL, RA, TMP2 // Ensure one valid arg.
827 | lwz KBASE, PC2PROTO(k)(TMP1)
828 | // BASE = base, RA = resultptr, RB = meta base
830 | bctr // Jump to continuation.
834 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
835 | // cont = 0: tailcall from C function.
841 |->cont_cat: // RA = resultptr, RB = meta base
844 | decode_RB8 SAVE0, INS
846 | add TMP1, BASE, SAVE0
849 | sub CARG3, CARG2, TMP1
856 |//-- Table indexing metamethods -----------------------------------------
859 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
862 | stw STR:RC, 4(CARG3)
863 | add CARG2, BASE, RB
868 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
870 | stw TAB:RB, 4(CARG2)
871 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
874 | stw STR:RC, 4(CARG3)
878 |->vmeta_tgetb: // TMP0 = index
883 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
884 | add CARG2, BASE, RB
886 | stw TISNUM, 0(CARG3)
896 | add CARG2, BASE, RB
897 | add CARG3, BASE, RC
902 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
903 | // Returns TValue * (finished) or NULL (metamethod).
911 |3: // Call __index metamethod.
912 | // BASE = base, L->top = new base, stack = cont/func/t/k
913 | subfic TMP1, BASE, FRAME_CONT
915 | stw PC, -16(BASE) // [cont|PC]
917 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
918 | li NARGS8:RC, 16 // 2 args for func(t, k).
919 | b ->vm_call_dispatch_f
921 |//-----------------------------------------------------------------------
924 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
927 | stw STR:RC, 4(CARG3)
928 | add CARG2, BASE, RB
933 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
935 | stw TAB:RB, 4(CARG2)
936 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
939 | stw STR:RC, 4(CARG3)
943 |->vmeta_tsetb: // TMP0 = index
948 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
949 | add CARG2, BASE, RB
951 | stw TISNUM, 0(CARG3)
961 | add CARG2, BASE, RB
962 | add CARG3, BASE, RC
967 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
968 | // Returns TValue * (finished) or NULL (metamethod).
972 | // NOBARRIER: lj_meta_tset ensures the table is not black.
977 |3: // Call __newindex metamethod.
978 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
979 | subfic TMP1, BASE, FRAME_CONT
981 | stw PC, -16(BASE) // [cont|PC]
983 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
984 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
985 | stfd f0, 16(BASE) // Copy value to third argument.
986 | b ->vm_call_dispatch_f
988 |//-- Comparison metamethods ---------------------------------------------
996 | add CARG2, BASE, RA
1002 | add CARG3, BASE, RD
1005 | decode_OP1 CARG4, INS
1006 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1007 | // Returns 0/1 or TValue * (metamethod).
1011 | subfic CRET1, CRET1, 0
1015 | decode_RD4 TMP2, INS
1016 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
1017 | and TMP2, TMP2, CRET1
1022 |->cont_ra: // RA = resultptr
1025 | decode_RA8 TMP1, INS
1026 | stfdx f0, BASE, TMP1
1029 |->cont_condt: // RA = resultptr
1031 | .gpr64 extsw TMP0, TMP0
1032 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true.
1033 | subfe CRET1, CRET1, CRET1
1037 |->cont_condf: // RA = resultptr
1039 | .gpr64 extsw TMP0, TMP0
1040 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false.
1041 | subfe CRET1, CRET1, CRET1
1045 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
1050 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1051 | // Returns 0/1 or TValue * (metamethod).
1061 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
1062 | // Returns 0/1 or TValue * (metamethod).
1066 |//-- Arithmetic metamethods ---------------------------------------------
1069 | add CARG3, KBASE, RC
1070 | add CARG4, BASE, RB
1085 | add CARG3, BASE, RB
1086 | add CARG4, KBASE, RC
1090 | add CARG3, BASE, RB
1091 | add CARG4, BASE, RC
1102 | add CARG2, BASE, RA
1106 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
1107 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1108 | // Returns NULL (finished) or TValue * (metamethod).
1112 | // Call metamethod for binary op.
1114 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1115 | sub TMP1, CRET1, BASE
1116 | stw PC, -16(CRET1) // [cont|PC]
1118 | addi PC, TMP1, FRAME_CONT
1120 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
1121 | b ->vm_call_dispatch
1131 | bl extern lj_meta_len // (lua_State *L, TValue *o)
1132 | // Returns NULL (retry) or TValue * (metamethod base).
1135 | bne ->vmeta_binop // Binop call for compatibility.
1139 | b ->vmeta_binop // Binop call for compatibility.
1142 |//-- Call metamethod ----------------------------------------------------
1144 |->vmeta_call: // Resolve and call __call metamethod.
1145 | // TMP2 = old base, BASE = new base, RC = nargs*8
1147 | stp TMP2, L->base // This is the callers base!
1148 | subi CARG2, BASE, 8
1150 | add CARG3, BASE, RC
1151 | mr SAVE0, NARGS8:RC
1152 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1153 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1154 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1157 |->vmeta_callt: // Resolve __call for BC_CALLT.
1158 | // BASE = old base, RA = new base, RC = nargs*8
1164 | mr SAVE0, NARGS8:RC
1165 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1166 | lwz TMP1, FRAME_PC(BASE)
1167 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1168 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1171 |//-- Argument coercion for 'for' statement ------------------------------
1179 | bl extern lj_meta_for // (lua_State *L, TValue *base)
1181 | decode_OP1 TMP0, SAVE0
1183 | decode_RA8 RA, SAVE0
1185 | cmpwi TMP0, BC_JFORI
1187 | decode_RD8 RD, SAVE0
1193 |//-----------------------------------------------------------------------
1194 |//-- Fast functions -----------------------------------------------------
1195 |//-----------------------------------------------------------------------
1197 |.macro .ffunc, name
1201 |.macro .ffunc_1, name
1203 | cmplwi NARGS8:RC, 8
1204 | lwz CARG3, 0(BASE)
1205 | lwz CARG1, 4(BASE)
1206 | blt ->fff_fallback
1209 |.macro .ffunc_2, name
1211 | cmplwi NARGS8:RC, 16
1212 | lwz CARG3, 0(BASE)
1213 | lwz CARG4, 8(BASE)
1214 | lwz CARG1, 4(BASE)
1215 | lwz CARG2, 12(BASE)
1216 | blt ->fff_fallback
1219 |.macro .ffunc_n, name
1221 | cmplwi NARGS8:RC, 8
1222 | lwz CARG3, 0(BASE)
1223 | lfd FARG1, 0(BASE)
1224 | blt ->fff_fallback
1225 | checknum CARG3; bge ->fff_fallback
1228 |.macro .ffunc_nn, name
1230 | cmplwi NARGS8:RC, 16
1231 | lwz CARG3, 0(BASE)
1232 | lfd FARG1, 0(BASE)
1233 | lwz CARG4, 8(BASE)
1234 | lfd FARG2, 8(BASE)
1235 | blt ->fff_fallback
1236 | checknum CARG3; bge ->fff_fallback
1237 | checknum CARG4; bge ->fff_fallback
1240 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
1242 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1243 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1248 |//-- Base library: checks -----------------------------------------------
1251 | li TMP1, LJ_TFALSE
1253 | cmplw cr1, CARG3, TMP1
1254 | lwz PC, FRAME_PC(BASE)
1255 | bge cr1, ->fff_fallback
1257 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1259 | beq ->fff_res // Done if exactly 1 argument.
1264 | lfdx f0, BASE, TMP1
1265 | stfdx f0, RA, TMP1
1266 | addi TMP1, TMP1, 8
1271 | cmplwi NARGS8:RC, 8
1272 | lwz CARG1, 0(BASE)
1273 | blt ->fff_fallback
1274 | .gpr64 extsw CARG1, CARG1
1275 | subfc TMP0, TISNUM, CARG1
1276 | subfe TMP2, CARG1, CARG1
1277 | orc TMP1, TMP2, TMP0
1278 | addi TMP1, TMP1, ~LJ_TISNUM+1
1279 | slwi TMP1, TMP1, 3
1280 | la TMP2, CFUNC:RB->upvalue
1281 | lfdx FARG1, TMP2, TMP1
1284 |//-- Base library: getters and setters ---------------------------------
1286 |.ffunc_1 getmetatable
1287 | checktab CARG3; bne >6
1288 |1: // Field metatable must be at same offset for GCtab and GCudata!
1289 | lwz TAB:CARG1, TAB:CARG1->metatable
1292 | cmplwi TAB:CARG1, 0
1293 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1295 | lwz TMP0, TAB:CARG1->hmask
1296 | li CARG3, LJ_TTAB // Use metatable as default result.
1297 | lwz TMP1, STR:RC->hash
1298 | lwz NODE:TMP2, TAB:CARG1->node
1299 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
1300 | slwi TMP0, TMP1, 5
1301 | slwi TMP1, TMP1, 3
1302 | sub TMP1, TMP0, TMP1
1303 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1304 |3: // Rearranged logic, because we expect _not_ to find the key.
1305 | lwz CARG4, NODE:TMP2->key
1306 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
1307 | lwz CARG2, NODE:TMP2->val
1308 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
1309 | checkstr CARG4; bne >4
1310 | cmpw TMP0, STR:RC; beq >5
1312 | lwz NODE:TMP2, NODE:TMP2->next
1313 | cmplwi NODE:TMP2, 0
1314 | beq ->fff_restv // Not found, keep default result.
1318 | beq ->fff_restv // Ditto for nil value.
1319 | mr CARG3, CARG2 // Return value of mt.__metatable.
1324 | cmpwi CARG3, LJ_TUDATA; beq <1
1325 | .gpr64 extsw CARG3, CARG3
1326 | subfc TMP0, TISNUM, CARG3
1327 | subfe TMP2, CARG3, CARG3
1328 | orc TMP1, TMP2, TMP0
1329 | addi TMP1, TMP1, ~LJ_TISNUM+1
1330 | slwi TMP1, TMP1, 2
1331 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1332 | lwzx TAB:CARG1, TMP2, TMP1
1335 |.ffunc_2 setmetatable
1336 | // Fast path: no mt for table yet and not clearing the mt.
1337 | checktab CARG3; bne ->fff_fallback
1338 | lwz TAB:TMP1, TAB:CARG1->metatable
1339 | checktab CARG4; bne ->fff_fallback
1340 | cmplwi TAB:TMP1, 0
1341 | lbz TMP3, TAB:CARG1->marked
1342 | bne ->fff_fallback
1343 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1344 | stw TAB:CARG2, TAB:CARG1->metatable
1346 | barrierback TAB:CARG1, TMP3, TMP0
1350 | cmplwi NARGS8:RC, 16
1351 | lwz CARG4, 0(BASE)
1352 | lwz TAB:CARG2, 4(BASE)
1353 | blt ->fff_fallback
1354 | checktab CARG4; bne ->fff_fallback
1357 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1358 | // Returns cTValue *.
1359 | lfd FARG1, 0(CRET1)
1362 |//-- Base library: conversions ------------------------------------------
1365 | // Only handles the number case inline (without a base argument).
1366 | cmplwi NARGS8:RC, 8
1367 | lwz CARG1, 0(BASE)
1368 | lfd FARG1, 0(BASE)
1369 | bne ->fff_fallback // Exactly one argument.
1370 | checknum CARG1; bgt ->fff_fallback
1374 | // Only handles the string or number case inline.
1376 | // A __tostring method in the string base metatable is ignored.
1377 | beq ->fff_restv // String key?
1378 | // Handle numbers inline, unless a number base metatable is present.
1379 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1381 | cmplwi cr1, TMP0, 0
1382 | stp BASE, L->base // Add frame since C call can throw.
1383 | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq
1384 | stw PC, SAVE_PC // Redundant (but a defined value).
1385 | beq ->fff_fallback
1390 | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o)
1392 | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np)
1394 | // Returns GCstr *.
1398 |//-- Base library: iterators -------------------------------------------
1401 | cmplwi NARGS8:RC, 8
1402 | lwz CARG1, 0(BASE)
1403 | lwz TAB:CARG2, 4(BASE)
1404 | blt ->fff_fallback
1405 | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1407 | lwz PC, FRAME_PC(BASE)
1408 | bne ->fff_fallback
1409 | stp BASE, L->base // Add frame since C call can throw.
1411 | stp BASE, L->top // Dummy frame length is ok.
1414 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1415 | // Returns 0 at end of traversal.
1418 | beq ->fff_restv // End of traversal: return nil.
1419 | lfd f0, 8(BASE) // Copy key and value to results.
1429 | lwz PC, FRAME_PC(BASE)
1430 | bne ->fff_fallback
1432 | lwz TAB:TMP2, TAB:CARG1->metatable
1433 | lfd f0, CFUNC:RB->upvalue[0]
1434 | cmplwi TAB:TMP2, 0
1436 | bne ->fff_fallback
1438 | lfd f0, CFUNC:RB->upvalue[0]
1441 | stw TISNIL, 8(BASE)
1447 | cmplwi NARGS8:RC, 16
1448 | lwz CARG3, 0(BASE)
1449 | lwz TAB:CARG1, 4(BASE)
1450 | lwz CARG4, 8(BASE)
1452 | lwz TMP2, 12(BASE)
1454 | lfd FARG2, 8(BASE)
1456 | blt ->fff_fallback
1458 | checknum cr1, CARG4
1459 | lwz PC, FRAME_PC(BASE)
1461 | bne ->fff_fallback
1462 | bne cr1, ->fff_fallback
1466 | bne ->fff_fallback
1468 | bge cr1, ->fff_fallback
1470 | toint TMP2, FARG2, f0
1472 | lwz TMP0, TAB:CARG1->asize
1473 | lwz TMP1, TAB:CARG1->array
1475 | fadd FARG2, FARG2, FARG1
1477 | addi TMP2, TMP2, 1
1482 | slwi TMP3, TMP2, 3
1485 | slwi TMP3, TMP2, 3
1488 | ble >2 // Not in array part?
1489 | lwzx TMP2, TMP1, TMP3
1490 | lfdx f0, TMP1, TMP3
1494 | beq ->fff_res // End of iteration, return 0 results.
1498 |2: // Check for empty hash part first. Otherwise call C function.
1499 | lwz TMP0, TAB:CARG1->hmask
1504 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1505 | // Returns cTValue * or NULL.
1509 | lwz TMP2, 0(CRET1)
1515 | lwz PC, FRAME_PC(BASE)
1516 | bne ->fff_fallback
1518 | lwz TAB:TMP2, TAB:CARG1->metatable
1519 | lfd f0, CFUNC:RB->upvalue[0]
1520 | cmplwi TAB:TMP2, 0
1522 | bne ->fff_fallback
1524 | lfd f0, CFUNC:RB->upvalue[0]
1528 | stw TISNUM, 8(BASE)
1532 | stw ZERO, 12(BASE)
1537 |//-- Base library: catch errors ----------------------------------------
1540 | cmplwi NARGS8:RC, 8
1541 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1542 | blt ->fff_fallback
1545 | // Remember active hook before pcall.
1546 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1547 | subi NARGS8:RC, NARGS8:RC, 8
1548 | addi PC, TMP3, 8+FRAME_PCALL
1549 | b ->vm_call_dispatch
1552 | cmplwi NARGS8:RC, 16
1553 | lwz CARG4, 8(BASE)
1554 | lfd FARG2, 8(BASE)
1555 | lfd FARG1, 0(BASE)
1556 | blt ->fff_fallback
1557 | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1559 | checkfunc CARG4; bne ->fff_fallback // Traceback must be a function.
1561 | // Remember active hook before pcall.
1562 | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31
1563 | stfd FARG2, 0(TMP2) // Swap function and traceback.
1564 | subi NARGS8:RC, NARGS8:RC, 16
1565 | stfd FARG1, 8(TMP2)
1566 | addi PC, TMP1, 16+FRAME_PCALL
1567 | b ->vm_call_dispatch
1569 |//-- Coroutine library --------------------------------------------------
1571 |.macro coroutine_resume_wrap, resume
1573 |.ffunc_1 coroutine_resume
1574 | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback
1576 |.ffunc coroutine_wrap_aux
1577 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1579 | lbz TMP0, L:CARG1->status
1580 | lp TMP1, L:CARG1->cframe
1581 | lp CARG2, L:CARG1->top
1582 | cmplwi cr0, TMP0, LUA_YIELD
1583 | lp TMP2, L:CARG1->base
1584 | cmplwi cr1, TMP1, 0
1585 | lwz TMP0, L:CARG1->maxstack
1586 | cmplw cr7, CARG2, TMP2
1587 | lwz PC, FRAME_PC(BASE)
1588 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1589 | add TMP2, CARG2, NARGS8:RC
1590 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1591 | cmplw cr1, TMP2, TMP0
1592 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1594 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1596 | blt cr6, ->fff_fallback
1599 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1600 | subi NARGS8:RC, NARGS8:RC, 8
1601 | subi TMP2, TMP2, 8
1603 | stp TMP2, L:CARG1->top
1606 |2: // Move args to coroutine.
1607 | cmpw TMP1, NARGS8:RC
1608 | lfdx f0, BASE, TMP1
1610 | stfdx f0, CARG2, TMP1
1611 | addi TMP1, TMP1, 8
1615 | mr L:SAVE0, L:CARG1
1617 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1618 | // Returns thread status.
1620 | lp TMP2, L:SAVE0->base
1621 | cmplwi CRET1, LUA_YIELD
1622 | lp TMP3, L:SAVE0->top
1627 | sub RD, TMP3, TMP2
1628 | lwz TMP0, L->maxstack
1630 | add TMP1, BASE, RD
1631 | beq >6 // No results?
1634 | bgt >9 // Need to grow stack?
1637 | stp TMP2, L:SAVE0->top // Clear coroutine stack.
1638 |5: // Move results from coroutine.
1640 | lfdx f0, TMP2, TMP1
1641 | stfdx f0, BASE, TMP1
1642 | addi TMP1, TMP1, 8
1645 | andix. TMP0, PC, FRAME_TYPE
1649 | stw TMP1, -8(BASE) // Prepend true to results.
1661 |8: // Coroutine returned with error (at co->top-1).
1663 | andix. TMP0, PC, FRAME_TYPE
1665 | li TMP1, LJ_TFALSE
1667 | stp TMP3, L:SAVE0->top // Remove error from coroutine stack.
1669 | stw TMP1, -8(BASE) // Prepend false to results.
1671 | stfd f0, 0(BASE) // Copy error message.
1676 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1679 |9: // Handle stack expansion on return from yield.
1682 | bl extern lj_state_growstack // (lua_State *L, int n)
1687 | coroutine_resume_wrap 1 // coroutine.resume
1688 | coroutine_resume_wrap 0 // coroutine.wrap
1690 |.ffunc coroutine_yield
1691 | lp TMP0, L->cframe
1692 | add TMP1, BASE, NARGS8:RC
1694 | andix. TMP0, TMP0, CFRAME_RESUME
1696 | li CRET1, LUA_YIELD
1697 | beq ->fff_fallback
1698 | stp ZERO, L->cframe
1699 | stb CRET1, L->status
1702 |//-- Math library -------------------------------------------------------
1708 | srawi TMP1, CARG1, 31
1709 | xor TMP2, TMP1, CARG1
1712 | sub CARG1, TMP2, TMP1
1716 | sub. CARG1, TMP2, TMP1
1720 | lwz PC, FRAME_PC(BASE)
1722 | stw TISNUM, -8(BASE)
1723 | stw CRET1, -4(BASE)
1726 | lus CARG3, 0x41e0 // 2^31.
1731 | bge ->fff_fallback
1732 | rlwinm CARG3, CARG3, 0, 1, 31
1736 | // CARG3/CARG1 = TValue result.
1737 | lwz PC, FRAME_PC(BASE)
1738 | stw CARG3, -8(BASE)
1740 | stw CARG1, -4(BASE)
1742 | // RA = results, PC = return.
1745 | // RA = results, RD = (nresults+1)*8, PC = return.
1746 | andix. TMP0, PC, FRAME_TYPE
1750 | decode_RB8 RB, INS
1752 | cmplw RB, RD // More results expected?
1753 | decode_RA8 TMP0, INS
1756 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1757 | sub BASE, RA, TMP0
1760 |6: // Fill up results with nil.
1763 | stwx TISNIL, RA, TMP1
1766 |.macro math_extern, func
1767 | .ffunc_n math_ .. func
1772 |.macro math_extern2, func
1773 | .ffunc_nn math_ .. func
1778 |.macro math_round, func
1779 | .ffunc_1 math_ .. func
1780 | checknum CARG3; beqy ->fff_restv
1781 | rlwinm TMP2, CARG3, 12, 21, 31
1782 | bge ->fff_fallback
1783 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
1784 | cmplwi cr1, TMP2, 31 // 0 <= exp < 31?
1785 | subfic TMP0, TMP2, 31
1787 | slwi TMP1, CARG3, 11
1788 | srwi TMP3, CARG1, 21
1789 | oris TMP1, TMP1, 0x8000
1790 | addi TMP2, TMP2, 1
1791 | or TMP1, TMP1, TMP3
1792 | slwi CARG2, CARG1, 11
1794 | slw TMP3, TMP1, TMP2
1795 | srw RD, TMP1, TMP0
1796 | or TMP3, TMP3, CARG2
1797 | srawi TMP2, CARG3, 31
1798 |.if "func" == "floor"
1799 | and TMP1, TMP3, TMP2
1800 | addic TMP0, TMP1, -1
1801 | subfe TMP1, TMP0, TMP1
1802 | add CARG1, RD, TMP1
1803 | xor CARG1, CARG1, TMP2
1804 | sub CARG1, CARG1, TMP2
1807 | andc TMP1, TMP3, TMP2
1808 | addic TMP0, TMP1, -1
1809 | subfe TMP1, TMP0, TMP1
1810 | add CARG1, RD, TMP1
1812 | xor CARG1, CARG1, TMP2
1813 | sub CARG1, CARG1, TMP2
1815 | // Overflow to 2^31.
1816 | lus CARG3, 0x41e0 // 2^31.
1821 | slwi TMP2, CARG3, 1
1822 | srawi TMP1, CARG3, 31
1823 | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo
1824 |.if "func" == "floor"
1825 | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1
1826 | subfic TMP2, TMP1, 0
1827 | subfe CARG1, CARG1, CARG1
1829 | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1
1830 | addic TMP2, TMP1, -1
1831 | subfe CARG1, TMP2, TMP1
1834 |4: // exp >= 31. Check for -(2^31).
1835 | xoris TMP1, TMP1, 0x8000
1836 | srawi TMP2, CARG3, 31
1837 |.if "func" == "floor"
1838 | or TMP1, TMP1, CARG2
1841 | orc TMP1, TMP1, TMP2
1844 | orc. TMP1, TMP1, TMP2
1846 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
1847 | lus CARG1, 0x8000 // -(2^31).
1850 | lfd FARG1, 0(BASE)
1859 | // NYI: use internal implementation.
1866 | fsqrt FARG1, FARG1
1873 | cmplwi NARGS8:RC, 8
1874 | lwz CARG3, 0(BASE)
1875 | lfd FARG1, 0(BASE)
1876 | bne ->fff_fallback // Need exactly 1 argument.
1877 | checknum CARG3; bge ->fff_fallback
1893 | math_extern2 atan2
1898 | lfd FARG2, CFUNC:RB->upvalue[0]
1899 | fmul FARG1, FARG1, FARG2
1904 | cmplwi NARGS8:RC, 16
1905 | lwz CARG3, 0(BASE)
1906 | lfd FARG1, 0(BASE)
1907 | lwz CARG4, 8(BASE)
1909 | lwz CARG2, 12(BASE)
1911 | lwz CARG1, 12(BASE)
1913 | blt ->fff_fallback
1914 | checknum CARG3; bge ->fff_fallback
1915 | checknum CARG4; bne ->fff_fallback
1917 |.ffunc_nn math_ldexp
1919 | toint CARG2, FARG2
1921 | toint CARG1, FARG2
1927 |.ffunc_n math_frexp
1929 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1931 | la CARG1, DISPATCH_GL(tmptv)(DISPATCH)
1933 | lwz PC, FRAME_PC(BASE)
1935 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1938 | tonum_i FARG2, TMP1
1952 | la CARG2, -8(BASE)
1954 | la CARG1, -8(BASE)
1956 | lwz PC, FRAME_PC(BASE)
1959 | stfd FARG1, 0(BASE)
1963 |.macro math_minmax, name, ismax
1967 | addi TMP1, BASE, 8
1968 | add TMP2, BASE, NARGS8:RC
1970 |1: // Handle integers.
1971 | lwz CARG4, 0(TMP1)
1972 | cmplw cr1, TMP1, TMP2
1973 | lwz CARG2, 4(TMP1)
1974 | bge cr1, ->fff_resi
1976 | xoris TMP0, CARG1, 0x8000
1977 | xoris TMP3, CARG2, 0x8000
1979 | subfc TMP3, TMP3, TMP0
1980 | subfe TMP0, TMP0, TMP0
1982 | andc TMP3, TMP3, TMP0
1984 | and TMP3, TMP3, TMP0
1986 | add CARG1, TMP3, CARG2
1988 | rldicl CARG1, CARG1, 0, 32
1990 | addi TMP1, TMP1, 8
1993 | bge ->fff_fallback
1994 | // Convert intermediate result to number and continue below.
1995 | tonum_i FARG1, CARG1
1996 | lfd FARG2, 0(TMP1)
1999 | lfd FARG1, 0(BASE)
2000 | bge ->fff_fallback
2001 |5: // Handle numbers.
2002 | lwz CARG4, 0(TMP1)
2003 | cmplw cr1, TMP1, TMP2
2004 | lfd FARG2, 0(TMP1)
2005 | bge cr1, ->fff_resn
2006 | checknum CARG4; bge >7
2008 | fsub f0, FARG1, FARG2
2009 | addi TMP1, TMP1, 8
2011 | fsel FARG1, f0, FARG1, FARG2
2013 | fsel FARG1, f0, FARG2, FARG1
2016 |7: // Convert integer to number and continue above.
2017 | lwz CARG2, 4(TMP1)
2018 | bne ->fff_fallback
2019 | tonum_i FARG2, CARG2
2025 | lwzx CARG2, BASE, TMP1
2026 | lfdx FARG2, BASE, TMP1
2027 | cmplw cr1, TMP1, NARGS8:RC
2029 | bge cr1, ->fff_resn
2030 | bge ->fff_fallback
2031 | fsub f0, FARG1, FARG2
2032 | addi TMP1, TMP1, 8
2034 | fsel FARG1, f0, FARG1, FARG2
2036 | fsel FARG1, f0, FARG2, FARG1
2042 | math_minmax math_min, 0
2043 | math_minmax math_max, 1
2045 |//-- String library -----------------------------------------------------
2047 |.ffunc_1 string_len
2048 | checkstr CARG3; bne ->fff_fallback
2049 | lwz CRET1, STR:CARG1->len
2052 |.ffunc string_byte // Only handle the 1-arg case here.
2053 | cmplwi NARGS8:RC, 8
2054 | lwz CARG3, 0(BASE)
2055 | lwz STR:CARG1, 4(BASE)
2056 | bne ->fff_fallback // Need exactly 1 argument.
2058 | bne ->fff_fallback
2059 | lwz TMP0, STR:CARG1->len
2061 | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
2063 | lwz PC, FRAME_PC(BASE)
2069 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
2070 | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8
2071 | subfe RD, TMP3, TMP0
2072 | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1.
2076 | lwz PC, FRAME_PC(BASE)
2077 | fsub f0, f0, TOBIT
2083 |.ffunc string_char // Only handle the 1-arg case here.
2085 | cmplwi NARGS8:RC, 8
2086 | lwz CARG3, 0(BASE)
2089 | bne ->fff_fallback // Exactly 1 argument.
2090 | checknum CARG3; bne ->fff_fallback
2093 | lfd FARG1, 0(BASE)
2094 | bne ->fff_fallback // Exactly 1 argument.
2095 | checknum CARG3; bge ->fff_fallback
2097 | la CARG2, TMPD_BLO
2100 | cmplwi TMP0, 255; bgt ->fff_fallback
2105 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
2106 | // Returns GCstr *.
2113 | cmplwi NARGS8:RC, 16
2114 | lwz CARG3, 16(BASE)
2119 | lwz STR:CARG1, 4(BASE)
2120 | blt ->fff_fallback
2121 | lwz CARG2, 8(BASE)
2123 | lwz TMP1, 12(BASE)
2131 | lwz TMP2, 20(BASE)
2132 | bne ->fff_fallback
2134 | checknum CARG2; bne ->fff_fallback
2136 | checknum CARG3; bge ->fff_fallback
2139 | checknum CARG2; bge ->fff_fallback
2141 | checkstr TMP0; bne ->fff_fallback
2145 | lwz TMP0, STR:CARG1->len
2146 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
2147 | addi TMP3, TMP2, 1
2150 | cmpwi TMP1, 0 // start <= 0?
2151 | add TMP3, TMP1, TMP0
2154 | sub CARG3, TMP2, TMP1
2155 | addi CARG2, STR:CARG1, #STR-1
2156 | srawi TMP0, CARG3, 31
2157 | addi CARG3, CARG3, 1
2158 | add CARG2, CARG2, TMP1
2159 | andc CARG3, CARG3, TMP0
2161 | rldicl CARG2, CARG2, 0, 32
2162 | rldicl CARG3, CARG3, 0, 32
2166 |5: // Negative end or overflow.
2167 | cmpw TMP0, TMP2 // len >= end? (signed compare)
2168 | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1.
2170 | mr TMP2, TMP0 // Overflow: end = len.
2173 |7: // Negative start or underflow.
2174 | .gpr64 extsw TMP1, TMP1
2175 | addic CARG3, TMP1, -1
2176 | subfe CARG3, CARG3, CARG3
2177 | srawi CARG2, TMP3, 31 // Note: modifies carry.
2178 | andc TMP3, TMP3, CARG3
2179 | andc TMP1, TMP3, CARG2
2180 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
2183 |.ffunc string_rep // Only handle the 1-char case inline.
2185 | cmplwi NARGS8:RC, 16
2187 | lwz STR:CARG1, 4(BASE)
2188 | lwz CARG4, 8(BASE)
2190 | lwz CARG3, 12(BASE)
2192 | lfd FARG2, 8(BASE)
2194 | bne ->fff_fallback // Exactly 2 arguments.
2195 | checkstr TMP0; bne ->fff_fallback
2197 | checknum CARG4; bne ->fff_fallback
2199 | checknum CARG4; bge ->fff_fallback
2200 | toint CARG3, FARG2
2202 | lwz TMP0, STR:CARG1->len
2204 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2205 | ble >2 // Count <= 0? (or non-int)
2207 | subi TMP2, CARG3, 1
2208 | blt >2 // Zero length string?
2209 | cmplw cr1, TMP1, CARG3
2210 | bne ->fff_fallback // Fallback for > 1-char strings.
2211 | lbz TMP0, STR:CARG1[1]
2212 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2213 | blt cr1, ->fff_fallback
2214 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2216 | stbx TMP0, CARG2, TMP2
2217 | subi TMP2, TMP2, 1
2220 |2: // Return empty string.
2221 | la STR:CARG1, DISPATCH_GL(strempty)(DISPATCH)
2225 |.ffunc string_reverse
2227 | cmplwi NARGS8:RC, 8
2228 | lwz CARG3, 0(BASE)
2229 | lwz STR:CARG1, 4(BASE)
2230 | blt ->fff_fallback
2232 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2233 | bne ->fff_fallback
2234 | lwz CARG3, STR:CARG1->len
2235 | la CARG1, #STR(STR:CARG1)
2236 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2239 | subi TMP3, CARG3, 1
2240 | blt ->fff_fallback
2241 |1: // Reverse string copy.
2243 | lbzx TMP1, CARG1, TMP2
2245 | stbx TMP1, CARG2, TMP3
2246 | subi TMP3, TMP3, 1
2247 | addi TMP2, TMP2, 1
2250 |.macro ffstring_case, name, lo
2253 | cmplwi NARGS8:RC, 8
2254 | lwz CARG3, 0(BASE)
2255 | lwz STR:CARG1, 4(BASE)
2256 | blt ->fff_fallback
2258 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2259 | bne ->fff_fallback
2260 | lwz CARG3, STR:CARG1->len
2261 | la CARG1, #STR(STR:CARG1)
2262 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2265 | blt ->fff_fallback
2266 |1: // ASCII case conversion.
2268 | lbzx TMP1, CARG1, TMP2
2270 | subi TMP0, TMP1, lo
2271 | xori TMP3, TMP1, 0x20
2272 | addic TMP0, TMP0, -26
2273 | subfe TMP3, TMP3, TMP3
2274 | rlwinm TMP3, TMP3, 0, 26, 26 // x &= 0x20.
2275 | xor TMP1, TMP1, TMP3
2276 | stbx TMP1, CARG2, TMP2
2277 | addi TMP2, TMP2, 1
2281 |ffstring_case string_lower, 65
2282 |ffstring_case string_upper, 97
2284 |//-- Table library ------------------------------------------------------
2286 |.ffunc_1 table_getn
2287 | checktab CARG3; bne ->fff_fallback
2288 | bl extern lj_tab_len // (GCtab *t)
2289 | // Returns uint32_t (but less than 2^31).
2292 |//-- Bit library --------------------------------------------------------
2294 |.macro .ffunc_bit, name
2296 | .ffunc_1 bit_..name
2297 | checknum CARG3; bnel ->fff_tobit_fb
2299 | .ffunc_n bit_..name
2300 | fadd FARG1, FARG1, TOBIT
2302 | lwz CARG1, TMPD_LO
2306 |.macro .ffunc_bit_op, name, ins
2308 | addi TMP1, BASE, 8
2309 | add TMP2, BASE, NARGS8:RC
2311 | lwz CARG4, 0(TMP1)
2312 | cmplw cr1, TMP1, TMP2
2314 | lwz CARG2, 4(TMP1)
2316 | lfd FARG1, 0(TMP1)
2318 | bgey cr1, ->fff_resi
2321 | bnel ->fff_bitop_fb
2323 | fadd FARG1, FARG1, TOBIT
2324 | bge ->fff_fallback
2326 | lwz CARG2, TMPD_LO
2328 | ins CARG1, CARG1, CARG2
2329 | addi TMP1, TMP1, 8
2333 |.ffunc_bit_op band, and
2334 |.ffunc_bit_op bor, or
2335 |.ffunc_bit_op bxor, xor
2338 | rotlwi TMP0, CARG1, 8
2339 | rlwimi TMP0, CARG1, 24, 0, 7
2340 | rlwimi TMP0, CARG1, 24, 16, 23
2348 |.macro .ffunc_bit_sh, name, ins, shmod
2350 | .ffunc_2 bit_..name
2351 | checknum CARG3; bnel ->fff_tobit_fb
2352 | // Note: no inline conversion from number for 2nd argument!
2353 | checknum CARG4; bne ->fff_fallback
2355 | .ffunc_nn bit_..name
2356 | fadd FARG1, FARG1, TOBIT
2357 | fadd FARG2, FARG2, TOBIT
2359 | lwz CARG1, TMPD_LO
2361 | lwz CARG2, TMPD_LO
2364 | rlwinm CARG2, CARG2, 0, 27, 31
2368 | ins CRET1, CARG1, CARG2
2372 |.ffunc_bit_sh lshift, slw, 1
2373 |.ffunc_bit_sh rshift, srw, 1
2374 |.ffunc_bit_sh arshift, sraw, 1
2375 |.ffunc_bit_sh rol, rotlw, 0
2376 |.ffunc_bit_sh ror, rotlw, 2
2383 | tonum_i FARG1, CRET1
2386 | lwz PC, FRAME_PC(BASE)
2388 | stfd FARG1, -8(BASE)
2391 |// Fallback FP number to bit conversion.
2394 | lfd FARG1, 0(BASE)
2395 | bgt ->fff_fallback
2396 | fadd FARG1, FARG1, TOBIT
2398 | lwz CARG1, TMPD_LO
2403 | lfd FARG1, 0(TMP1)
2404 | bgt ->fff_fallback
2405 | fadd FARG1, FARG1, TOBIT
2407 | lwz CARG2, TMPD_LO
2411 |//-----------------------------------------------------------------------
2413 |->fff_fallback: // Call fast function fallback handler.
2414 | // BASE = new base, RB = CFUNC, RC = nargs*8
2415 | lp TMP3, CFUNC:RB->f
2416 | add TMP1, BASE, NARGS8:RC
2417 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2418 | addi TMP0, TMP1, 8*LUA_MINSTACK
2419 | lwz TMP2, L->maxstack
2420 | stw PC, SAVE_PC // Redundant (but a defined value).
2421 | .toc lp TMP3, 0(TMP3)
2426 | bgt >5 // Need to grow stack.
2428 | bctrl // (lua_State *L)
2429 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2434 | bgt ->fff_res // Returned nresults+1?
2435 |1: // Returned 0 or -1: retry fast path.
2437 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2438 | sub NARGS8:RC, TMP0, BASE
2439 | bne ->vm_call_tail // Returned -1?
2440 | ins_callt // Returned 0: retry fast path.
2442 |// Reconstruct previous base for vmeta_call during tailcall.
2444 | andix. TMP0, PC, FRAME_TYPE
2445 | rlwinm TMP1, PC, 0, 0, 28
2448 | decode_RA8 TMP1, INS
2449 | addi TMP1, TMP1, 8
2451 | sub TMP2, BASE, TMP1
2452 | b ->vm_call_dispatch // Resolve again for tailcall.
2454 |5: // Grow stack for fallback handler.
2455 | li CARG2, LUA_MINSTACK
2456 | bl extern lj_state_growstack // (lua_State *L, int n)
2458 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
2461 |->fff_gcstep: // Call GC step function.
2462 | // BASE = new base, RC = nargs*8
2465 | add TMP0, BASE, NARGS8:RC
2466 | stw PC, SAVE_PC // Redundant (but a defined value).
2469 | bl extern lj_gc_step // (lua_State *L)
2473 | sub NARGS8:RC, TMP0, BASE
2474 | lwz CFUNC:RB, FRAME_FUNC(BASE)
2477 |//-----------------------------------------------------------------------
2478 |//-- Special dispatch targets -------------------------------------------
2479 |//-----------------------------------------------------------------------
2481 |->vm_record: // Dispatch target for recording phase.
2483 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2484 | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2486 | // Decrement the hookcount for consistency, but always do the call.
2487 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2488 | andix. TMP0, TMP3, HOOK_ACTIVE
2490 | subi TMP2, TMP2, 1
2491 | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2493 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2497 |->vm_rethook: // Dispatch target for return hooks.
2498 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2499 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2501 |5: // Re-dispatch to static ins.
2502 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS.
2503 | lpx TMP0, DISPATCH, TMP1
2507 |->vm_inshook: // Dispatch target for instr/line hooks.
2508 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2509 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2510 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2511 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
2514 | cmpwi cr1, TMP0, 0
2515 | addic. TMP2, TMP2, -1
2517 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2522 | stw MULTRES, SAVE_MULTRES
2525 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2526 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2529 |4: // Re-dispatch to static ins.
2531 | decode_OPP TMP1, INS
2532 | decode_RB8 RB, INS
2533 | addi TMP1, TMP1, GG_DISP2STATIC
2534 | decode_RD8 RD, INS
2535 | lpx TMP0, DISPATCH, TMP1
2536 | decode_RA8 RA, INS
2537 | decode_RC8 RC, INS
2541 |->cont_hook: // Continue from hook yield.
2543 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
2546 |->vm_hotloop: // Hot loop counter underflow.
2548 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2549 | addi CARG1, DISPATCH, GG_DISP2J
2551 | lwz TMP1, LFUNC:TMP1->pc
2553 | stw L, DISPATCH_J(L)(DISPATCH)
2554 | lbz TMP1, PC2PROTO(framesize)(TMP1)
2556 | slwi TMP1, TMP1, 3
2557 | add TMP1, BASE, TMP1
2559 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2563 |->vm_callhook: // Dispatch target for call hooks.
2569 |->vm_hotcall: // Hot call counter underflow.
2574 | add TMP0, BASE, RC
2580 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2581 | // Returns ASMFunction.
2584 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
2585 | sub NARGS8:RC, TMP0, BASE
2587 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2592 |//-----------------------------------------------------------------------
2593 |//-- Trace exit handler -------------------------------------------------
2594 |//-----------------------------------------------------------------------
2596 |.macro savex_, a, b, c, d
2597 | stfd f..a, 16+a*8(sp)
2598 | stfd f..b, 16+b*8(sp)
2599 | stfd f..c, 16+c*8(sp)
2600 | stfd f..d, 16+d*8(sp)
2605 | addi sp, sp, -(16+32*8+32*4)
2606 | stmw r2, 16+32*8+2*4(sp)
2607 | addi DISPATCH, JGL, -GG_DISP2G-32768
2608 | li CARG2, ~LJ_VMST_EXIT
2609 | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain.
2610 | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH)
2612 | stw CARG1, 0(sp) // Store extended stack chain.
2615 | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp.
2617 | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP.
2618 | savex_ 12,13,14,15
2621 | savex_ 16,17,18,19
2622 | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP.
2623 | savex_ 20,21,22,23
2624 | lhz CARG4, 2(CARG3) // Load trace number.
2625 | savex_ 24,25,26,27
2626 | lwz L, DISPATCH_GL(jit_L)(DISPATCH)
2627 | savex_ 28,29,30,31
2628 | sub CARG3, TMP0, CARG3 // Compute exit number.
2629 | lp BASE, DISPATCH_GL(jit_base)(DISPATCH)
2630 | srwi CARG3, CARG3, 2
2631 | stw L, DISPATCH_J(L)(DISPATCH)
2632 | subi CARG3, CARG3, 2
2633 | stw TMP1, DISPATCH_GL(jit_L)(DISPATCH)
2634 | stw CARG4, DISPATCH_J(parent)(DISPATCH)
2636 | addi CARG1, DISPATCH, GG_DISP2J
2637 | stw CARG3, DISPATCH_J(exitno)(DISPATCH)
2638 | addi CARG2, sp, 16
2639 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2640 | // Returns MULTRES (unscaled) or negated error code.
2641 | lp TMP1, L->cframe
2645 | rldicr sp, TMP1, 0, 61
2647 | rlwinm sp, TMP1, 0, 0, 29
2649 | lwz PC, SAVE_PC // Get SAVE_PC.
2651 | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2656 | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set.
2658 | addi DISPATCH, JGL, -GG_DISP2G-32768
2661 | blt >3 // Check for error from exit.
2662 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2663 | slwi MULTRES, CARG1, 3
2665 | stw MULTRES, SAVE_MULTRES
2666 | lwz TMP1, LFUNC:TMP1->pc
2667 | stw TMP2, DISPATCH_GL(jit_L)(DISPATCH)
2668 | lwz KBASE, PC2PROTO(k)(TMP1)
2669 | // Setup type comparison constants.
2670 | li TISNUM, LJ_TISNUM
2671 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2674 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2677 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2678 | li TISNIL, LJ_TNIL
2679 | stw TMP0, TONUM_HI
2681 | // Modified copy of ins_next which handles function header dispatch, too.
2684 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1.
2685 | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2686 | decode_OPP TMP1, INS
2687 | decode_RA8 RA, INS
2688 | lpx TMP0, DISPATCH, TMP1
2690 | cmplwi TMP1, BC_FUNCF*4 // Function header?
2692 | decode_RB8 RB, INS
2693 | decode_RD8 RD, INS
2694 | decode_RC8 RC, INS
2697 | subi RC, MULTRES, 8
2701 |3: // Rethrow error from the right C frame.
2704 | bl extern lj_err_throw // (lua_State *L, int errcode)
2707 |//-----------------------------------------------------------------------
2708 |//-- Math helper functions ----------------------------------------------
2709 |//-----------------------------------------------------------------------
2711 |// NYI: Use internal implementations of floor, ceil, trunc.
2714 | divwo. TMP0, CARG1, CARG2
2717 | xor CARG3, CARG1, CARG2
2720 | xor. CARG3, CARG1, CARG2
2722 | mullw TMP0, TMP0, CARG2
2723 | sub CARG1, CARG1, TMP0
2725 | cmpwi CARG1, 0; beqlr
2726 | add CARG1, CARG1, CARG2
2732 | clrso TMP0 // Clear SO for -2147483648 % -1 and return 0.
2735 |//-----------------------------------------------------------------------
2736 |//-- Miscellaneous functions --------------------------------------------
2737 |//-----------------------------------------------------------------------
2739 |// void lj_vm_cachesync(void *start, void *end)
2740 |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size.
2741 |// This is a good lower bound, except for very ancient PPC models.
2744 | // Compute start of first cache line and number of cache lines.
2745 | rlwinm CARG1, CARG1, 0, 0, 26
2746 | sub CARG2, CARG2, CARG1
2747 | addi CARG2, CARG2, 31
2748 | rlwinm. CARG2, CARG2, 27, 5, 31
2752 |1: // Flush D-Cache.
2754 | addi CARG1, CARG1, 32
2758 |1: // Invalidate I-Cache.
2760 | addi CARG3, CARG3, 32
2766 |//-----------------------------------------------------------------------
2767 |//-- FFI helper functions -----------------------------------------------
2768 |//-----------------------------------------------------------------------
2770 |// Handler for callback functions. Callback slot number in r11, g in r12.
2773 |.type CTSTATE, CTState, PC
2775 | lwz CTSTATE, GL:r12->ctype_state
2776 | addi DISPATCH, r12, GG_G2DISP
2777 | stw r11, CTSTATE->cb.slot
2778 | stw r3, CTSTATE->cb.gpr[0]
2779 | stfd f1, CTSTATE->cb.fpr[0]
2780 | stw r4, CTSTATE->cb.gpr[1]
2781 | stfd f2, CTSTATE->cb.fpr[1]
2782 | stw r5, CTSTATE->cb.gpr[2]
2783 | stfd f3, CTSTATE->cb.fpr[2]
2784 | stw r6, CTSTATE->cb.gpr[3]
2785 | stfd f4, CTSTATE->cb.fpr[3]
2786 | stw r7, CTSTATE->cb.gpr[4]
2787 | stfd f5, CTSTATE->cb.fpr[4]
2788 | stw r8, CTSTATE->cb.gpr[5]
2789 | stfd f6, CTSTATE->cb.fpr[5]
2790 | stw r9, CTSTATE->cb.gpr[6]
2791 | stfd f7, CTSTATE->cb.fpr[6]
2792 | stw r10, CTSTATE->cb.gpr[7]
2793 | stfd f8, CTSTATE->cb.fpr[7]
2794 | addi TMP0, sp, CFRAME_SPACE+8
2795 | stw TMP0, CTSTATE->cb.stack
2797 | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2799 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2800 | // Returns lua_State *.
2801 | lp BASE, L:CRET1->base
2802 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2803 | lp RC, L:CRET1->top
2804 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2808 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2809 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2810 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2811 | stw TMP0, TONUM_HI
2812 | li TISNIL, LJ_TNIL
2822 |->cont_ffi_callback: // Return from FFI callback.
2824 | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
2830 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2831 | lwz CRET1, CTSTATE->cb.gpr[0]
2832 | lfd FARG1, CTSTATE->cb.fpr[0]
2833 | lwz CRET2, CTSTATE->cb.gpr[1]
2837 |->vm_ffi_call: // Call C function via FFI.
2838 | // Caveat: needs special frame unwinding, see below.
2840 | .type CCSTATE, CCallState, CARG1
2841 | lwz TMP1, CCSTATE->spadj
2843 | lbz CARG2, CCSTATE->nsp
2844 | lbz CARG3, CCSTATE->nfpr
2847 | cmpwi cr1, CARG3, 0
2849 | addic. CARG2, CARG2, -1
2850 | stwux sp, sp, TMP1
2851 | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls.
2853 | stw CCSTATE, -8(TMP2)
2855 | la TMP1, CCSTATE->stack
2856 | slwi CARG2, CARG2, 2
2860 | lwzx TMP0, TMP1, CARG2
2861 | stwx TMP0, TMP2, CARG2
2862 | addic. CARG2, CARG2, -4
2866 | lfd f1, CCSTATE->fpr[0]
2867 | lfd f2, CCSTATE->fpr[1]
2868 | lfd f3, CCSTATE->fpr[2]
2869 | lfd f4, CCSTATE->fpr[3]
2870 | lfd f5, CCSTATE->fpr[4]
2871 | lfd f6, CCSTATE->fpr[5]
2872 | lfd f7, CCSTATE->fpr[6]
2873 | lfd f8, CCSTATE->fpr[7]
2875 | lp TMP0, CCSTATE->func
2876 | lwz CARG2, CCSTATE->gpr[1]
2877 | lwz CARG3, CCSTATE->gpr[2]
2878 | lwz CARG4, CCSTATE->gpr[3]
2879 | lwz CARG5, CCSTATE->gpr[4]
2881 | lwz r8, CCSTATE->gpr[5]
2882 | lwz r9, CCSTATE->gpr[6]
2883 | lwz r10, CCSTATE->gpr[7]
2884 | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
2886 | lwz CCSTATE:TMP1, -8(r14)
2889 | stw CARG1, CCSTATE:TMP1->gpr[0]
2890 | stfd FARG1, CCSTATE:TMP1->fpr[0]
2891 | stw CARG2, CCSTATE:TMP1->gpr[1]
2893 | stw CARG3, CCSTATE:TMP1->gpr[2]
2895 | stw CARG4, CCSTATE:TMP1->gpr[3]
2899 |// Note: vm_ffi_call must be the last function in this object file!
2901 |//-----------------------------------------------------------------------
2904 /* Generate the code for a single instruction. */
2905 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2912 /* -- Comparison ops ---------------------------------------------------- */
2914 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2916 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2917 | // RA = src1*8, RD = src2*8, JMP with RD = target
2919 | lwzux TMP0, RA, BASE
2922 | lwzux TMP1, RD, BASE
2924 | checknum cr0, TMP0
2926 | decode_RD4 TMP2, TMP2
2927 | checknum cr1, TMP1
2928 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2932 if (op == BC_ISLT) {
2934 } else if (op == BC_ISGE) {
2936 } else if (op == BC_ISLE) {
2946 |7: // RA is not an integer.
2947 | bgt cr0, ->vmeta_comp
2948 | // RA is a number.
2950 | bgt cr1, ->vmeta_comp
2952 | // RA is a number, RD is an integer.
2956 |8: // RA is an integer, RD is not an integer.
2957 | bgt cr1, ->vmeta_comp
2958 | // RA is an integer, RD is a number.
2964 if (op == BC_ISLT) {
2966 } else if (op == BC_ISGE) {
2968 } else if (op == BC_ISLE) {
2969 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2972 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2977 | lwzx TMP0, BASE, RA
2980 | lwzx TMP1, BASE, RD
2981 | checknum cr0, TMP0
2984 | checknum cr1, TMP1
2985 | decode_RD4 TMP2, TMP2
2986 | bge cr0, ->vmeta_comp
2987 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2988 | bge cr1, ->vmeta_comp
2990 if (op == BC_ISLT) {
2992 } else if (op == BC_ISGE) {
2994 } else if (op == BC_ISLE) {
2995 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2998 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
3007 case BC_ISEQV: case BC_ISNEV:
3008 vk = op == BC_ISEQV;
3009 | // RA = src1*8, RD = src2*8, JMP with RD = target
3011 | lwzux TMP0, RA, BASE
3014 | lwzux TMP1, RD, BASE
3015 | checknum cr0, TMP0
3017 | checknum cr1, TMP1
3018 | decode_RD4 TMP2, TMP2
3020 | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt
3021 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3023 | ble cr7, ->BC_ISEQN_Z
3025 | ble cr7, ->BC_ISNEN_Z
3028 | lwzux TMP0, RA, BASE
3032 | lwzux TMP1, RD, BASE
3033 | checknum cr0, TMP0
3034 | decode_RD4 TMP2, TMP2
3036 | checknum cr1, TMP1
3037 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3051 |5: // Either or both types are not numbers.
3057 | cmpwi cr7, TMP0, LJ_TCDATA
3058 | cmpwi cr5, TMP1, LJ_TCDATA
3062 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
3064 | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq
3066 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
3068 | beq cr7, ->vmeta_equal_cd
3070 | cmplw cr5, CARG2, CARG3
3071 | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive.
3072 | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type.
3073 | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv.
3075 | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2.
3076 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2.
3087 | bge cr0, >2 // Done if 1 or 2.
3092 | blt cr0, <1 // Done if 1 or 2.
3094 | blt cr6, <1 // Done if not tab/ud.
3096 | // Different tables or userdatas. Need to check __eq metamethod.
3097 | // Field metatable must be at same offset for GCtab and GCudata!
3098 | lwz TAB:TMP2, TAB:CARG2->metatable
3099 | li CARG4, 1-vk // ne = 0 or 1.
3100 | cmplwi TAB:TMP2, 0
3101 | beq <1 // No metatable?
3102 | lbz TMP2, TAB:TMP2->nomm
3103 | andix. TMP2, TMP2, 1<<MM_eq
3104 | bne <1 // Or 'no __eq' flag set?
3105 | mr PC, SAVE0 // Restore old PC.
3106 | b ->vmeta_equal // Handle __eq metamethod.
3109 case BC_ISEQS: case BC_ISNES:
3110 vk = op == BC_ISEQS;
3111 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3112 | lwzux TMP0, RA, BASE
3114 | lwz STR:TMP3, 4(RA)
3119 | cmpwi TMP0, LJ_TCDATA
3121 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
3122 | .gpr64 extsw TMP0, TMP0
3123 | subfic TMP0, TMP0, LJ_TSTR
3125 | beq ->vmeta_equal_cd
3127 | sub TMP1, STR:TMP1, STR:TMP3
3128 | or TMP0, TMP0, TMP1
3129 | decode_RD4 TMP2, TMP2
3130 | subfic TMP0, TMP0, 0
3131 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3132 | subfe TMP1, TMP1, TMP1
3134 | andc TMP2, TMP2, TMP1
3136 | and TMP2, TMP2, TMP1
3142 case BC_ISEQN: case BC_ISNEN:
3143 vk = op == BC_ISEQN;
3144 | // RA = src*8, RD = num_const*8, JMP with RD = target
3146 | lwzux TMP0, RA, BASE
3149 | lwzux TMP1, RD, KBASE
3150 | checknum cr0, TMP0
3152 | checknum cr1, TMP1
3153 | decode_RD4 TMP2, TMP2
3155 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3167 |->BC_ISEQN_Z: // Dummy label.
3169 |->BC_ISNEN_Z: // Dummy label.
3171 | lwzx TMP0, BASE, RA
3175 | lfdx f1, KBASE, RD
3176 | decode_RD4 TMP2, TMP2
3178 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3201 | cmpwi TMP0, LJ_TCDATA
3202 | beq ->vmeta_equal_cd
3206 |7: // RA is not an integer.
3208 | // RA is a number.
3211 | // RA is a number, RD is an integer.
3215 |8: // RA is an integer, RD is a number.
3225 case BC_ISEQP: case BC_ISNEP:
3226 vk = op == BC_ISEQP;
3227 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3228 | lwzx TMP0, BASE, RA
3234 | cmpwi TMP0, LJ_TCDATA
3236 | sub TMP0, TMP0, TMP1
3238 | beq ->vmeta_equal_cd
3240 | decode_RD4 TMP2, TMP2
3241 | .gpr64 extsw TMP0, TMP0
3242 | addic TMP0, TMP0, -1
3243 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3244 | subfe TMP1, TMP1, TMP1
3246 | and TMP2, TMP2, TMP1
3248 | andc TMP2, TMP2, TMP1
3254 /* -- Unary test and copy ops ------------------------------------------- */
3256 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3257 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3258 | lwzx TMP0, BASE, RD
3261 if (op == BC_IST || op == BC_ISF) {
3262 | .gpr64 extsw TMP0, TMP0
3263 | subfic TMP0, TMP0, LJ_TTRUE
3264 | decode_RD4 TMP2, INS
3265 | subfe TMP1, TMP1, TMP1
3266 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3268 | andc TMP2, TMP2, TMP1
3270 | and TMP2, TMP2, TMP1
3274 | li TMP1, LJ_TFALSE
3277 if (op == BC_ISTC) {
3282 | addis PC, PC, -(BCBIAS_J*4 >> 16)
3283 | decode_RD4 TMP2, INS
3284 | stfdx f0, BASE, RA
3291 /* -- Unary ops --------------------------------------------------------- */
3294 | // RA = dst*8, RD = src*8
3297 | stfdx f0, BASE, RA
3301 | // RA = dst*8, RD = src*8
3303 | lwzx TMP0, BASE, RD
3304 | .gpr64 extsw TMP0, TMP0
3305 | subfic TMP1, TMP0, LJ_TTRUE
3306 | adde TMP0, TMP0, TMP1
3307 | stwx TMP0, BASE, RA
3311 | // RA = dst*8, RD = src*8
3312 | lwzux TMP1, RD, BASE
3328 | stwux TISNUM, RA, BASE
3334 | // Potential overflow.
3335 | checkov TMP1, <1 // Ignore unrelated overflow.
3337 | lus TMP1, 0x41e0 // 2^31.
3343 | xoris TMP1, TMP1, 0x8000
3346 | stwux TMP1, RA, BASE
3355 | // RA = dst*8, RD = src*8
3356 | lwzux TMP0, RD, BASE
3358 | checkstr TMP0; bne >2
3359 | lwz CRET1, STR:CARG1->len
3363 | stwux TISNUM, RA, BASE
3366 | tonum_u f0, CRET1 // Result is a non-negative integer.
3368 | stfdx f0, BASE, RA
3372 | checktab TMP0; bne ->vmeta_len
3374 | lwz TAB:TMP2, TAB:CARG1->metatable
3375 | cmplwi TAB:TMP2, 0
3380 | bl extern lj_tab_len // (GCtab *t)
3381 | // Returns uint32_t (but less than 2^31).
3385 | lbz TMP0, TAB:TMP2->nomm
3386 | andix. TMP0, TMP0, 1<<MM_len
3387 | bne <3 // 'no __len' flag set: done.
3392 /* -- Binary ops -------------------------------------------------------- */
3394 |.macro ins_arithpre
3395 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3396 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3399 | lwzx TMP1, BASE, RB
3401 | lwzx TMP2, KBASE, RC
3403 | lfdx f14, BASE, RB
3404 | lfdx f15, KBASE, RC
3406 | checknum cr0, TMP1
3407 | checknum cr1, TMP2
3408 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3409 | bge ->vmeta_arith_vn
3411 | checknum TMP1; bge ->vmeta_arith_vn
3415 | lwzx TMP1, BASE, RB
3417 | lwzx TMP2, KBASE, RC
3419 | lfdx f15, BASE, RB
3420 | lfdx f14, KBASE, RC
3422 | checknum cr0, TMP1
3423 | checknum cr1, TMP2
3424 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3425 | bge ->vmeta_arith_nv
3427 | checknum TMP1; bge ->vmeta_arith_nv
3431 | lwzx TMP1, BASE, RB
3432 | lwzx TMP2, BASE, RC
3433 | lfdx f14, BASE, RB
3434 | lfdx f15, BASE, RC
3435 | checknum cr0, TMP1
3436 | checknum cr1, TMP2
3437 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3438 | bge ->vmeta_arith_vv
3443 |.macro ins_arithfallback, ins
3446 | ins ->vmeta_arith_vn2
3449 | ins ->vmeta_arith_nv2
3452 | ins ->vmeta_arith_vv2
3457 |.macro intmod, a, b, c
3461 |.macro fpmod, a, b, c
3464 | // NYI: Use internal implementation of floor.
3465 | blex floor // floor(b/c)
3467 | fsub a, b, a // b - floor(b/c)*c
3470 |.macro ins_arithfp, fpins
3472 |.if "fpins" == "fpmod_"
3473 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3475 | fpins f0, f14, f15
3477 | stfdx f0, BASE, RA
3482 |.macro ins_arithdn, intins, fpins
3483 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3484 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3487 | lwzux TMP1, RB, BASE
3488 | lwzux TMP2, RC, KBASE
3490 | checknum cr0, TMP1
3494 | lwzux TMP1, RB, BASE
3495 | lwzux TMP2, RC, KBASE
3497 | checknum cr0, TMP1
3501 | lwzux TMP1, RB, BASE
3502 | lwzux TMP2, RC, BASE
3504 | checknum cr0, TMP1
3508 | checknum cr1, TMP2
3511 | intins CARG1, CARG1, CARG2
3515 | stwux TISNUM, RA, BASE
3520 | checkov TMP0, <1 // Ignore unrelated overflow.
3521 | ins_arithfallback b
3525 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3529 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3532 | ins_arithfallback bge
3533 |.if "fpins" == "fpmod_"
3534 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3536 | fpins f0, f14, f15
3538 | stfdx f0, BASE, RA
3543 |.macro ins_arith, intins, fpins
3545 | ins_arithdn intins, fpins
3551 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3553 |.macro addo32., y, a, b
3554 | // Need to check overflow for (a<<32) + (b<<32).
3555 | rldicr TMP0, a, 32, 31
3556 | rldicr TMP3, b, 32, 31
3557 | addo. TMP0, TMP0, TMP3
3560 | ins_arith addo32., fadd
3562 | ins_arith addo., fadd
3565 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3567 |.macro subo32., y, a, b
3568 | // Need to check overflow for (a<<32) - (b<<32).
3569 | rldicr TMP0, a, 32, 31
3570 | rldicr TMP3, b, 32, 31
3571 | subo. TMP0, TMP0, TMP3
3574 | ins_arith subo32., fsub
3576 | ins_arith subo., fsub
3579 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3580 | ins_arith mullwo., fmul
3582 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3586 | ins_arith intmod, fpmod
3588 case BC_MODNV: case BC_MODVV:
3589 | ins_arith intmod, fpmod_
3592 | // NYI: (partial) integer arithmetic.
3593 | lwzx TMP1, BASE, RB
3594 | lfdx FARG1, BASE, RB
3595 | lwzx TMP2, BASE, RC
3596 | lfdx FARG2, BASE, RC
3597 | checknum cr0, TMP1
3598 | checknum cr1, TMP2
3599 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3600 | bge ->vmeta_arith_vv
3603 | stfdx FARG1, BASE, RA
3608 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3611 | add CARG2, BASE, RC
3616 | srwi CARG3, CARG3, 3
3617 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3618 | // Returns NULL (finished) or TValue * (metamethod).
3623 | lfdx f0, BASE, SAVE0 // Copy result from RB to RA.
3624 | stfdx f0, BASE, RA
3628 /* -- Constant ops ------------------------------------------------------ */
3631 | // RA = dst*8, RD = str_const*8 (~)
3633 | subfic TMP1, TMP1, -4
3635 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
3637 | stwux TMP2, RA, BASE
3643 | // RA = dst*8, RD = cdata_const*8 (~)
3645 | subfic TMP1, TMP1, -4
3647 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
3648 | li TMP2, LJ_TCDATA
3649 | stwux TMP2, RA, BASE
3655 | // RA = dst*8, RD = int16_literal*8
3660 | stwux TISNUM, RA, BASE
3664 | // The soft-float approach is faster.
3666 | srawi TMP1, RD, 31
3667 | xor TMP2, TMP1, RD
3668 | sub TMP2, TMP2, TMP1 // TMP2 = abs(x)
3670 | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1
3671 | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa
3672 | subfic TMP3, RD, 0
3673 | slwi TMP1, TMP1, 20
3674 | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11)
3675 | subfe TMP0, TMP0, TMP0
3676 | add RD, RD, TMP1 // hi = hi + exponent-1
3677 | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi
3679 | stwux RD, RA, BASE
3685 | // RA = dst*8, RD = num_const*8
3687 | lfdx f0, KBASE, RD
3688 | stfdx f0, BASE, RA
3692 | // RA = dst*8, RD = primitive_type*8 (~)
3696 | stwx TMP0, BASE, RA
3700 | // RA = base*8, RD = end*8
3701 | stwx TISNIL, BASE, RA
3704 | stwx TISNIL, BASE, RA
3711 /* -- Upvalue and function ops ------------------------------------------ */
3714 | // RA = dst*8, RD = uvnum*8
3715 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3717 | addi RD, RD, offsetof(GCfuncL, uvptr)
3718 | lwzx UPVAL:RB, LFUNC:RB, RD
3720 | lwz TMP1, UPVAL:RB->v
3722 | stfdx f0, BASE, RA
3726 | // RA = uvnum*8, RD = src*8
3727 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3729 | addi RA, RA, offsetof(GCfuncL, uvptr)
3730 | lfdux f0, RD, BASE
3731 | lwzx UPVAL:RB, LFUNC:RB, RA
3732 | lbz TMP3, UPVAL:RB->marked
3733 | lwz CARG2, UPVAL:RB->v
3734 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3735 | lbz TMP0, UPVAL:RB->closed
3738 | cmplwi cr1, TMP0, 0
3740 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3741 | subi TMP2, TMP2, (LJ_TNUMX+1)
3742 | bne >2 // Upvalue is closed and black?
3746 |2: // Check if new value is collectable.
3747 | cmplwi TMP2, LJ_TISGCV - (LJ_TNUMX+1)
3748 | bge <1 // tvisgcv(v)
3749 | lbz TMP3, GCOBJ:TMP1->gch.marked
3750 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
3751 | la CARG1, GG_DISP2G(DISPATCH)
3752 | // Crossed a write barrier. Move the barrier forward.
3754 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3758 | // RA = uvnum*8, RD = str_const*8 (~)
3759 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3762 | subfic TMP1, TMP1, -4
3763 | addi RA, RA, offsetof(GCfuncL, uvptr)
3764 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
3765 | lwzx UPVAL:RB, LFUNC:RB, RA
3766 | lbz TMP3, UPVAL:RB->marked
3767 | lwz CARG2, UPVAL:RB->v
3768 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3769 | lbz TMP3, STR:TMP1->marked
3770 | lbz TMP2, UPVAL:RB->closed
3772 | stw STR:TMP1, 4(CARG2)
3773 | stw TMP0, 0(CARG2)
3778 |2: // Check if string is white and ensure upvalue is closed.
3779 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
3780 | cmplwi cr1, TMP2, 0
3781 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3782 | la CARG1, GG_DISP2G(DISPATCH)
3783 | // Crossed a write barrier. Move the barrier forward.
3785 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3789 | // RA = uvnum*8, RD = num_const*8
3790 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3792 | addi RA, RA, offsetof(GCfuncL, uvptr)
3793 | lfdx f0, KBASE, RD
3794 | lwzx UPVAL:RB, LFUNC:RB, RA
3796 | lwz TMP1, UPVAL:RB->v
3801 | // RA = uvnum*8, RD = primitive_type*8 (~)
3802 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3805 | addi RA, RA, offsetof(GCfuncL, uvptr)
3807 | lwzx UPVAL:RB, LFUNC:RB, RA
3809 | lwz TMP1, UPVAL:RB->v
3815 | // RA = level*8, RD = target
3816 | lwz TMP1, L->openupval
3817 | branch_RD // Do this first since RD is not saved.
3822 | add CARG2, BASE, RA
3823 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3830 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
3833 | subfic TMP1, TMP1, -4
3835 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3837 | lwz CARG3, FRAME_FUNC(BASE)
3838 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3839 | bl extern lj_func_newL_gc
3840 | // Returns GCfuncL *.
3843 | stwux TMP0, RA, BASE
3844 | stw LFUNC:CRET1, 4(RA)
3848 /* -- Table ops --------------------------------------------------------- */
3852 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
3853 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
3855 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
3861 if (op == BC_TNEW) {
3862 | rlwinm CARG2, RD, 29, 21, 31
3863 | rlwinm CARG3, RD, 18, 27, 31
3864 | cmpwi CARG2, 0x7ff; beq >3
3866 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3867 | // Returns Table *.
3870 | subfic TMP1, TMP1, -4
3871 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3872 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3873 | // Returns Table *.
3877 | stwux TMP0, RA, BASE
3878 | stw TAB:CRET1, 4(RA)
3880 if (op == BC_TNEW) {
3887 | bl extern lj_gc_step_fixtop // (lua_State *L)
3894 | // RA = dst*8, RD = str_const*8 (~)
3896 | // RA = src*8, RD = str_const*8 (~)
3897 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
3899 | lwz TAB:RB, LFUNC:TMP2->env
3900 | subfic TMP1, TMP1, -4
3901 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3902 if (op == BC_GGET) {
3910 | // RA = dst*8, RB = table*8, RC = key*8
3911 | lwzux CARG1, RB, BASE
3912 | lwzux CARG2, RC, BASE
3920 | checknum cr1, CARG2
3923 | lwz TMP0, TAB:RB->asize
3925 | lwz TMP1, TAB:RB->array
3930 | // Convert number key to integer, check for integerness and range.
3932 | fadd f2, f0, TOBIT
3934 | lwz TMP0, TAB:RB->asize
3935 | fsub f2, f2, TOBIT
3937 | lwz TMP1, TAB:RB->array
3939 | cmplw cr0, TMP0, TMP2
3940 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
3941 | slwi TMP2, TMP2, 3
3943 | ble ->vmeta_tgetv // Integer key and in array part?
3944 | lwzx TMP0, TMP1, TMP2
3945 | lfdx f14, TMP1, TMP2
3946 | checknil TMP0; beq >2
3949 | stfdx f14, BASE, RA
3952 |2: // Check for __index if table value is nil.
3953 | lwz TAB:TMP2, TAB:RB->metatable
3954 | cmplwi TAB:TMP2, 0
3955 | beq <1 // No metatable: done.
3956 | lbz TMP0, TAB:TMP2->nomm
3957 | andix. TMP0, TMP0, 1<<MM_index
3958 | bne <1 // 'no __index' flag set: done.
3962 | checkstr CARG2; bne ->vmeta_tgetv
3966 | b ->BC_TGETS_Z // String key?
3969 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
3970 | lwzux CARG1, RB, BASE
3973 | subfic TMP1, TMP1, -4
3975 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3976 | bne ->vmeta_tgets1
3978 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
3979 | lwz TMP0, TAB:RB->hmask
3980 | lwz TMP1, STR:RC->hash
3981 | lwz NODE:TMP2, TAB:RB->node
3982 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
3983 | slwi TMP0, TMP1, 5
3984 | slwi TMP1, TMP1, 3
3985 | sub TMP1, TMP0, TMP1
3986 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
3988 | lwz CARG1, NODE:TMP2->key
3989 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
3990 | lwz CARG2, NODE:TMP2->val
3991 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
3992 | checkstr CARG1; bne >4
3993 | cmpw TMP0, STR:RC; bne >4
3994 | checknil CARG2; beq >5 // Key found, but nil value?
3996 | stwux CARG2, RA, BASE
4000 |4: // Follow hash chain.
4001 | lwz NODE:TMP2, NODE:TMP2->next
4002 | cmplwi NODE:TMP2, 0
4004 | // End of hash chain: key not found, nil result.
4007 |5: // Check for __index if table value is nil.
4008 | lwz TAB:TMP2, TAB:RB->metatable
4009 | cmplwi TAB:TMP2, 0
4010 | beq <3 // No metatable: done.
4011 | lbz TMP0, TAB:TMP2->nomm
4012 | andix. TMP0, TMP0, 1<<MM_index
4013 | bne <3 // 'no __index' flag set: done.
4017 | // RA = dst*8, RB = table*8, RC = index*8
4018 | lwzux CARG1, RB, BASE
4021 | checktab CARG1; bne ->vmeta_tgetb
4022 | lwz TMP1, TAB:RB->asize
4023 | lwz TMP2, TAB:RB->array
4024 | cmplw TMP0, TMP1; bge ->vmeta_tgetb
4025 | lwzx TMP1, TMP2, RC
4027 | checknil TMP1; beq >5
4030 | stfdx f0, BASE, RA
4033 |5: // Check for __index if table value is nil.
4034 | lwz TAB:TMP2, TAB:RB->metatable
4035 | cmplwi TAB:TMP2, 0
4036 | beq <1 // No metatable: done.
4037 | lbz TMP2, TAB:TMP2->nomm
4038 | andix. TMP2, TMP2, 1<<MM_index
4039 | bne <1 // 'no __index' flag set: done.
4040 | b ->vmeta_tgetb // Caveat: preserve TMP0!
4044 | // RA = src*8, RB = table*8, RC = key*8
4045 | lwzux CARG1, RB, BASE
4046 | lwzux CARG2, RC, BASE
4054 | checknum cr1, CARG2
4057 | lwz TMP0, TAB:RB->asize
4059 | lwz TMP1, TAB:RB->array
4064 | // Convert number key to integer, check for integerness and range.
4066 | fadd f2, f0, TOBIT
4068 | lwz TMP0, TAB:RB->asize
4069 | fsub f2, f2, TOBIT
4071 | lwz TMP1, TAB:RB->array
4073 | cmplw cr0, TMP0, TMP2
4074 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4075 | slwi TMP0, TMP2, 3
4077 | ble ->vmeta_tsetv // Integer key and in array part?
4078 | lwzx TMP2, TMP1, TMP0
4079 | lbz TMP3, TAB:RB->marked
4080 | lfdx f14, BASE, RA
4081 | checknil TMP2; beq >3
4083 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4084 | stfdx f14, TMP1, TMP0
4089 |3: // Check for __newindex if previous value is nil.
4090 | lwz TAB:TMP2, TAB:RB->metatable
4091 | cmplwi TAB:TMP2, 0
4092 | beq <1 // No metatable: done.
4093 | lbz TMP2, TAB:TMP2->nomm
4094 | andix. TMP2, TMP2, 1<<MM_newindex
4095 | bne <1 // 'no __newindex' flag set: done.
4099 | checkstr CARG2; bne ->vmeta_tsetv
4103 | b ->BC_TSETS_Z // String key?
4105 |7: // Possible table write barrier for the value. Skip valiswhite check.
4106 | barrierback TAB:RB, TMP3, TMP0
4110 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4111 | lwzux CARG1, RB, BASE
4114 | subfic TMP1, TMP1, -4
4116 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4117 | bne ->vmeta_tsets1
4119 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
4120 | lwz TMP0, TAB:RB->hmask
4121 | lwz TMP1, STR:RC->hash
4122 | lwz NODE:TMP2, TAB:RB->node
4123 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
4124 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
4125 | lfdx f14, BASE, RA
4126 | slwi TMP0, TMP1, 5
4127 | slwi TMP1, TMP1, 3
4128 | sub TMP1, TMP0, TMP1
4129 | lbz TMP3, TAB:RB->marked
4130 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4132 | lwz CARG1, NODE:TMP2->key
4133 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4134 | lwz CARG2, NODE:TMP2->val
4135 | lwz NODE:TMP1, NODE:TMP2->next
4136 | checkstr CARG1; bne >5
4137 | cmpw TMP0, STR:RC; bne >5
4138 | checknil CARG2; beq >4 // Key found, but nil value?
4140 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4141 | stfd f14, NODE:TMP2->val
4146 |4: // Check for __newindex if previous value is nil.
4147 | lwz TAB:TMP1, TAB:RB->metatable
4148 | cmplwi TAB:TMP1, 0
4149 | beq <2 // No metatable: done.
4150 | lbz TMP0, TAB:TMP1->nomm
4151 | andix. TMP0, TMP0, 1<<MM_newindex
4152 | bne <2 // 'no __newindex' flag set: done.
4155 |5: // Follow hash chain.
4156 | cmplwi NODE:TMP1, 0
4157 | mr NODE:TMP2, NODE:TMP1
4159 | // End of hash chain: key not found, add a new one.
4161 | // But check for __newindex first.
4162 | lwz TAB:TMP1, TAB:RB->metatable
4163 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
4166 | cmplwi TAB:TMP1, 0
4168 | beq >6 // No metatable: continue.
4169 | lbz TMP0, TAB:TMP1->nomm
4170 | andix. TMP0, TMP0, 1<<MM_newindex
4171 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4174 | stw STR:RC, 4(CARG3)
4176 | stw TMP0, 0(CARG3)
4177 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4178 | // Returns TValue *.
4180 | stfd f14, 0(CRET1)
4181 | b <3 // No 2nd write barrier needed.
4183 |7: // Possible table write barrier for the value. Skip valiswhite check.
4184 | barrierback TAB:RB, TMP3, TMP0
4188 | // RA = src*8, RB = table*8, RC = index*8
4189 | lwzux CARG1, RB, BASE
4192 | checktab CARG1; bne ->vmeta_tsetb
4193 | lwz TMP1, TAB:RB->asize
4194 | lwz TMP2, TAB:RB->array
4195 | lbz TMP3, TAB:RB->marked
4197 | lfdx f14, BASE, RA
4199 | lwzx TMP1, TMP2, RC
4200 | checknil TMP1; beq >5
4202 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4203 | stfdx f14, TMP2, RC
4208 |5: // Check for __newindex if previous value is nil.
4209 | lwz TAB:TMP1, TAB:RB->metatable
4210 | cmplwi TAB:TMP1, 0
4211 | beq <1 // No metatable: done.
4212 | lbz TMP1, TAB:TMP1->nomm
4213 | andix. TMP1, TMP1, 1<<MM_newindex
4214 | bne <1 // 'no __newindex' flag set: done.
4215 | b ->vmeta_tsetb // Caveat: preserve TMP0!
4217 |7: // Possible table write barrier for the value. Skip valiswhite check.
4218 | barrierback TAB:RB, TMP3, TMP0
4223 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4226 | add TMP3, KBASE, RD
4227 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
4228 | addic. TMP0, MULTRES, -8
4229 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
4230 | srwi CARG3, TMP0, 3
4231 | beq >4 // Nothing to copy?
4232 | add CARG3, CARG3, TMP3
4233 | lwz TMP2, TAB:CARG2->asize
4234 | slwi TMP1, TMP3, 3
4235 | lbz TMP3, TAB:CARG2->marked
4237 | add TMP2, RA, TMP0
4238 | lwz TMP0, TAB:CARG2->array
4240 | add TMP1, TMP1, TMP0
4241 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4242 |3: // Copy result slots to table.
4245 | cmpw cr1, RA, TMP2
4247 | addi TMP1, TMP1, 8
4253 |5: // Need to resize array part.
4258 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4259 | // Must not reallocate the stack.
4263 |7: // Possible table write barrier for any value. Skip valiswhite check.
4264 | barrierback TAB:CARG2, TMP3, TMP0
4268 /* -- Calls and vararg handling ----------------------------------------- */
4271 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4272 | add NARGS8:RC, NARGS8:RC, MULTRES
4273 | // Fall through. Assumes BC_CALL follows.
4276 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4278 | lwzux TMP0, BASE, RA
4279 | lwz LFUNC:RB, 4(BASE)
4280 | subi NARGS8:RC, NARGS8:RC, 8
4281 | addi BASE, BASE, 8
4282 | checkfunc TMP0; bne ->vmeta_call
4287 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4288 | add NARGS8:RC, NARGS8:RC, MULTRES
4289 | // Fall through. Assumes BC_CALLT follows.
4292 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4293 | lwzux TMP0, RA, BASE
4294 | lwz LFUNC:RB, 4(RA)
4295 | subi NARGS8:RC, NARGS8:RC, 8
4296 | lwz TMP1, FRAME_PC(BASE)
4301 | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
4302 | lbz TMP3, LFUNC:RB->ffid
4303 | xori TMP2, TMP1, FRAME_VARG
4304 | cmplwi cr1, NARGS8:RC, 0
4307 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4309 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
4312 | addi TMP3, TMP2, 8
4314 | cmplw cr1, TMP3, NARGS8:RC
4315 | stfdx f0, BASE, TMP2
4319 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
4324 |5: // Tailcall to a fast function with a Lua frame below.
4326 | decode_RA8 RA, INS
4327 | sub TMP1, BASE, RA
4328 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
4329 | lwz TMP1, LFUNC:TMP1->pc
4330 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4333 |7: // Tailcall from a vararg function.
4334 | andix. TMP0, TMP2, FRAME_TYPEP
4335 | bne <1 // Vararg frame below?
4336 | sub BASE, BASE, TMP2 // Relocate BASE down.
4337 | lwz TMP1, FRAME_PC(BASE)
4338 | andix. TMP0, TMP1, FRAME_TYPE
4343 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4345 | add BASE, BASE, RA
4346 | lwz TMP1, -24(BASE)
4347 | lwz LFUNC:RB, -20(BASE)
4350 | stw TMP1, 0(BASE) // Copy callable.
4351 | stw LFUNC:RB, 4(BASE)
4353 | stfd f1, 16(BASE) // Copy control var.
4354 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4355 | stfdu f0, 8(BASE) // Copy state.
4361 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4363 | // NYI: add hotloop, record BC_ITERN.
4366 | lwz TAB:RB, -12(RA)
4367 | lwz RC, -4(RA) // Get index from control var.
4368 | lwz TMP0, TAB:RB->asize
4369 | lwz TMP1, TAB:RB->array
4371 |1: // Traverse array part.
4374 | bge >5 // Index points after array part?
4375 | lwzx TMP2, TMP1, TMP3
4376 | lfdx f0, TMP1, TMP3
4387 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
4389 | decode_RD4 TMP1, INS
4390 | stw RC, -4(RA) // Update control var.
4391 | add PC, TMP1, TMP3
4398 |4: // Skip holes in array part.
4402 |5: // Traverse hash part.
4403 | lwz TMP1, TAB:RB->hmask
4405 | lwz TMP2, TAB:RB->node
4407 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
4411 | sub TMP3, TMP3, RB
4412 | lwzx RB, TMP2, TMP3
4413 | lfdx f0, TMP2, TMP3
4414 | add NODE:TMP3, TMP2, TMP3
4418 | lfd f1, NODE:TMP3->key
4419 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
4422 | decode_RD4 TMP1, INS
4425 | add PC, TMP1, TMP2
4426 | stw RC, -4(RA) // Update control var.
4429 |7: // Skip holes in hash part.
4435 | // RA = base*8, RD = target (points to ITERN)
4438 | lwz CFUNC:TMP1, -20(RA)
4441 | cmpwi cr0, TMP2, LJ_TTAB
4442 | cmpwi cr1, TMP0, LJ_TFUNC
4443 | cmpwi cr6, TMP3, LJ_TNIL
4445 | lbz TMP1, CFUNC:TMP1->ffid
4446 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
4447 | cmpwi cr7, TMP1, FF_next_N
4449 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4450 | add TMP3, PC, TMP0
4453 | ori TMP1, TMP1, 0x7fff
4454 | stw ZERO, -4(RA) // Initialize control var.
4456 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4459 |5: // Despecialize bytecode if any of the checks fail.
4463 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4469 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4470 | lwz TMP0, FRAME_PC(BASE)
4473 | addi RC, RC, FRAME_VARG
4475 | subi TMP3, BASE, 8 // TMP3 = vtop
4476 | sub RC, RC, TMP0 // RC = vbase
4477 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4480 | sub TMP1, TMP3, RC
4483 | sub. TMP1, TMP3, RC
4485 | beq cr1, >5 // Copy all varargs?
4486 | subi TMP2, TMP2, 16
4487 | ble >2 // No vararg slots?
4488 |1: // Copy vararg slots to destination slots.
4493 | cmplw cr1, RC, TMP3
4494 | bge >3 // All destination slots filled?
4496 | blt cr1, <1 // More vararg slots?
4497 |2: // Fill up remainder with nil.
4505 |5: // Copy all varargs.
4506 | lwz TMP0, L->maxstack
4507 | li MULTRES, 8 // MULTRES = (0+1)*8
4508 | bley <3 // No vararg slots?
4509 | add TMP2, RA, TMP1
4511 | addi MULTRES, TMP1, 8
4519 | blt <6 // More vararg slots?
4522 |7: // Grow stack for varargs.
4525 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
4529 | srwi CARG2, TMP1, 3
4530 | bl extern lj_state_growstack // (lua_State *L, int n)
4533 | add RC, BASE, SAVE0
4534 | subi TMP3, BASE, 8
4538 /* -- Returns ----------------------------------------------------------- */
4541 | // RA = results*8, RD = extra_nresults*8
4542 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4543 | // Fall through. Assumes BC_RET follows.
4547 | // RA = results*8, RD = (nresults+1)*8
4548 | lwz PC, FRAME_PC(BASE)
4552 | andix. TMP0, PC, FRAME_TYPE
4553 | xori TMP1, PC, FRAME_VARG
4557 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4560 | subi TMP2, BASE, 8
4562 | decode_RB8 RB, INS
4566 | addi TMP3, TMP1, 8
4569 | stfdx f0, TMP2, TMP1
4571 | addi TMP1, TMP3, 8
4574 | stfdx f1, TMP2, TMP3
4579 | decode_RA8 RA, INS
4581 | sub BASE, TMP2, RA
4582 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4584 | lwz TMP1, LFUNC:TMP1->pc
4585 | lwz KBASE, PC2PROTO(k)(TMP1)
4588 |6: // Fill up results with nil.
4591 | stwx TISNIL, TMP2, TMP1
4594 |->BC_RETV_Z: // Non-standard return case.
4595 | andix. TMP2, TMP1, FRAME_TYPEP
4597 | // Return from vararg function: relocate BASE down.
4598 | sub BASE, BASE, TMP1
4599 | lwz PC, FRAME_PC(BASE)
4603 case BC_RET0: case BC_RET1:
4604 | // RA = results*8, RD = (nresults+1)*8
4605 | lwz PC, FRAME_PC(BASE)
4608 | andix. TMP0, PC, FRAME_TYPE
4609 | xori TMP1, PC, FRAME_VARG
4613 | subi TMP2, BASE, 8
4614 | decode_RB8 RB, INS
4615 if (op == BC_RET1) {
4621 | decode_RA8 RA, INS
4623 | sub BASE, TMP2, RA
4624 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4626 | lwz TMP1, LFUNC:TMP1->pc
4627 | lwz KBASE, PC2PROTO(k)(TMP1)
4630 |6: // Fill up results with nil.
4633 | stwx TISNIL, TMP2, TMP1
4637 /* -- Loops and branches ------------------------------------------------ */
4643 | // Fall through. Assumes BC_IFORL follows.
4653 | // RA = base*8, RD = target (after end of loop or start of loop)
4654 vk = (op == BC_IFORL || op == BC_JFORL);
4657 | lwzux TMP1, RA, BASE
4658 | lwz CARG1, FORL_IDX*8+4(RA)
4659 | cmplw cr0, TMP1, TISNUM
4661 | lwz CARG3, FORL_STEP*8+4(RA)
4664 | // Need to check overflow for (a<<32) + (b<<32).
4665 | rldicr TMP0, CARG1, 32, 31
4666 | rldicr TMP2, CARG3, 32, 31
4667 | add CARG1, CARG1, CARG3
4668 | addo. TMP0, TMP0, TMP2
4670 | addo. CARG1, CARG1, CARG3
4672 | cmpwi cr6, CARG3, 0
4673 | lwz CARG2, FORL_STOP*8+4(RA)
4676 | stw CARG1, FORL_IDX*8+4(RA)
4678 | lwz TMP3, FORL_STEP*8(RA)
4679 | lwz CARG3, FORL_STEP*8+4(RA)
4680 | lwz TMP2, FORL_STOP*8(RA)
4681 | lwz CARG2, FORL_STOP*8+4(RA)
4682 | cmplw cr7, TMP3, TISNUM
4683 | cmplw cr1, TMP2, TISNUM
4684 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4685 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4686 | cmpwi cr6, CARG3, 0
4692 | stw TISNUM, FORL_EXT*8(RA)
4693 if (op != BC_JFORL) {
4696 | stw CARG1, FORL_EXT*8+4(RA)
4697 if (op != BC_JFORL) {
4700 if (op == BC_FORI) {
4701 | bgt >3 // See FP loop below.
4702 } else if (op == BC_JFORI) {
4703 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4705 } else if (op == BC_IFORL) {
4707 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4713 |5: // Invert check for negative step.
4717 |6: // Potential overflow.
4718 | checkov TMP0, <4 // Ignore unrelated overflow.
4725 | lfd f1, FORL_IDX*8(RA)
4727 | lfdux f1, RA, BASE
4729 | lfd f3, FORL_STEP*8(RA)
4730 | lfd f2, FORL_STOP*8(RA)
4731 | lwz TMP3, FORL_STEP*8(RA)
4733 | stfd f1, FORL_IDX*8(RA)
4738 | lwzux TMP1, RA, BASE
4739 | lwz TMP3, FORL_STEP*8(RA)
4740 | lwz TMP2, FORL_STOP*8(RA)
4741 | cmplw cr0, TMP1, TISNUM
4742 | cmplw cr7, TMP3, TISNUM
4743 | cmplw cr1, TMP2, TISNUM
4745 | lfd f1, FORL_IDX*8(RA)
4746 | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
4747 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4748 | lfd f2, FORL_STOP*8(RA)
4751 | cmpwi cr6, TMP3, 0
4752 if (op != BC_JFORL) {
4755 | stfd f1, FORL_EXT*8(RA)
4756 if (op != BC_JFORL) {
4760 if (op == BC_JFORI) {
4761 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4764 if (op == BC_FORI) {
4766 } else if (op == BC_IFORL) {
4773 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4774 } else if (op == BC_JFORI) {
4785 |5: // Negative step.
4786 if (op == BC_FORI) {
4788 |3: // Used by integer loop, too.
4789 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4790 } else if (op == BC_IFORL) {
4792 } else if (op == BC_JFORI) {
4798 if (op == BC_JFORI) {
4801 | decode_RD8 RD, INS
4810 | // Fall through. Assumes BC_IITERL follows.
4818 | // RA = base*8, RD = target
4819 | lwzux TMP1, RA, BASE
4821 | checknil TMP1; beq >1 // Stop if iterator returned nil.
4822 if (op == BC_JITERL) {
4827 | branch_RD // Otherwise save control var + branch.
4836 | // RA = base*8, RD = target (loop extent)
4837 | // Note: RA/RD is only used by trace recorder to determine scope/extent
4838 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4842 | // Fall through. Assumes BC_ILOOP follows.
4846 | // RA = base*8, RD = target (loop extent)
4852 | // RA = base*8 (ignored), RD = traceno*8
4853 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
4855 | // Traces on PPC don't store the trace number, so use 0.
4856 | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH)
4857 | lwzx TRACE:TMP2, TMP1, RD
4859 | lp TMP2, TRACE:TMP2->mcode
4860 | stw BASE, DISPATCH_GL(jit_base)(DISPATCH)
4862 | stw L, DISPATCH_GL(jit_L)(DISPATCH)
4863 | addi JGL, DISPATCH, GG_DISP2G+32768
4869 | // RA = base*8 (only used by trace recorder), RD = target
4874 /* -- Function headers -------------------------------------------------- */
4880 case BC_FUNCV: /* NYI: compiled vararg functions. */
4881 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4889 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4890 | lwz TMP2, L->maxstack
4891 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
4892 | lwz KBASE, -4+PC2PROTO(k)(PC)
4894 | slwi TMP1, TMP1, 3
4895 | bgt ->vm_growstack_l
4896 if (op != BC_JFUNCF) {
4900 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
4902 if (op == BC_JFUNCF) {
4903 | decode_RD8 RD, INS
4909 |3: // Clear missing parameters.
4910 | stwx TISNIL, BASE, NARGS8:RC
4911 | addi NARGS8:RC, NARGS8:RC, 8
4919 | NYI // NYI: compiled vararg functions
4920 break; /* NYI: compiled vararg functions. */
4923 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4924 | lwz TMP2, L->maxstack
4925 | add TMP1, BASE, RC
4927 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
4928 | addi TMP3, RC, 8+FRAME_VARG
4929 | lwz KBASE, -4+PC2PROTO(k)(PC)
4931 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
4932 | bge ->vm_growstack_l
4933 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
4938 | addi BASE, TMP1, 8
4941 | cmplw RA, RC // Less args than parameters?
4945 | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
4948 | addic. TMP2, TMP2, -1
4950 | stw TMP3, 12(TMP1)
4951 | addi TMP1, TMP1, 8
4956 |4: // Clear missing parameters.
4963 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
4964 if (op == BC_FUNCC) {
4965 | lp RD, CFUNC:RB->f
4967 | lp RD, DISPATCH_GL(wrapf)(DISPATCH)
4969 | add TMP1, RA, NARGS8:RC
4970 | lwz TMP2, L->maxstack
4971 | .toc lp TMP3, 0(RD)
4972 | add RC, BASE, NARGS8:RC
4982 if (op == BC_FUNCCW) {
4983 | lp CARG2, CFUNC:RB->f
4986 | bgt ->vm_growstack_c // Need to grow stack.
4987 | .toc lp TOCREG, TOC_OFS(RD)
4988 | .tocenv lp ENVREG, ENV_OFS(RD)
4990 | bctrl // (lua_State *L [, lua_CFunction f])
4991 | // Returns nresults.
4993 | .toc ld TOCREG, SAVE_TOC
4997 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
4998 | sub RA, TMP1, RD // RA = L->top - nresults*8
5003 /* ---------------------------------------------------------------------- */
5006 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5012 static int build_backend(BuildCtx *ctx)
5016 dasm_growpc(Dst, BC__MAX);
5018 build_subroutines(ctx);
5021 for (op = 0; op < BC__MAX; op++)
5022 build_ins(ctx, (BCOp)op, op);
5027 /* Emit pseudo frame-info for all assembler functions. */
5028 static void emit_asm_debug(BuildCtx *ctx)
5030 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5032 switch (ctx->mode) {
5034 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5037 "\t.long .LECIE0-.LSCIE0\n"
5039 "\t.long 0xffffffff\n"
5045 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5050 "\t.long .LEFDE0-.LASFDE0\n"
5052 "\t.long .Lframe0\n"
5055 "\t.byte 0xe\n\t.uleb128 %d\n"
5056 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5057 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5058 fcofs, CFRAME_SIZE);
5059 for (i = 14; i <= 31; i++)
5061 "\t.byte %d\n\t.uleb128 %d\n"
5062 "\t.byte %d\n\t.uleb128 %d\n",
5063 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5070 "\t.long .LEFDE1-.LASFDE1\n"
5072 "\t.long .Lframe0\n"
5074 "\t.long .lj_vm_ffi_call\n"
5076 "\t.long lj_vm_ffi_call\n"
5079 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5080 "\t.byte 0x8e\n\t.uleb128 2\n"
5081 "\t.byte 0xd\n\t.uleb128 0xe\n"
5083 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5086 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5089 "\t.long .LECIE1-.LSCIE1\n"
5093 "\t.string \"zPR\"\n"
5097 "\t.uleb128 6\n" /* augmentation length */
5098 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5099 "\t.long lj_err_unwind_dwarf-.\n"
5100 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5101 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5106 "\t.long .LEFDE2-.LASFDE2\n"
5108 "\t.long .LASFDE2-.Lframe1\n"
5109 "\t.long .Lbegin-.\n"
5111 "\t.uleb128 0\n" /* augmentation length */
5112 "\t.byte 0xe\n\t.uleb128 %d\n"
5113 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5114 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5115 fcofs, CFRAME_SIZE);
5116 for (i = 14; i <= 31; i++)
5118 "\t.byte %d\n\t.uleb128 %d\n"
5119 "\t.byte %d\n\t.uleb128 %d\n",
5120 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5127 "\t.long .LECIE2-.LSCIE2\n"
5131 "\t.string \"zR\"\n"
5135 "\t.uleb128 1\n" /* augmentation length */
5136 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5137 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5142 "\t.long .LEFDE3-.LASFDE3\n"
5144 "\t.long .LASFDE3-.Lframe2\n"
5145 "\t.long lj_vm_ffi_call-.\n"
5147 "\t.uleb128 0\n" /* augmentation length */
5148 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5149 "\t.byte 0x8e\n\t.uleb128 2\n"
5150 "\t.byte 0xd\n\t.uleb128 0xe\n"
5152 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);