1 |// Low-level VM code for PowerPC CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2013 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)
78 |//-----------------------------------------------------------------------
80 |// Fixed register assignments for the interpreter.
81 |// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA)
83 |// The following must be C callee-save (but BASE is often refetched).
84 |.define BASE, r14 // Base of current Lua stack frame.
85 |.define KBASE, r15 // Constants of current Lua function.
86 |.define PC, r16 // Next PC.
87 |.define DISPATCH, r17 // Opcode dispatch table.
88 |.define LREG, r18 // Register holding lua_State (also in SAVE_L).
89 |.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
90 |.define JGL, r31 // On-trace: global_State + 32768.
92 |// Constants for type-comparisons, stores and conversions. C callee-save.
96 |.define TOBIT, f30 // 2^52 + 2^51.
97 |.define TONUM, f31 // 2^52 + 2^51 + 2^31.
99 |// The following temporaries are not saved across C calls, except for RA.
100 |.define RA, r20 // Callee-save.
104 |.define INS, r7 // Overlaps CARG5.
109 |.define TMP3, r6 // Overlaps CARG4.
111 |// Saved temporaries.
114 |// Calling conventions.
118 |.define CARG4, r6 // Overlaps TMP3.
119 |.define CARG5, r7 // Overlaps INS.
127 |.define TOCREG, r2 // TOC register (only used by C code).
128 |.define ENVREG, r11 // Environment pointer (nested C functions).
130 |// Stack layout while in interpreter. Must match with lj_frame.h.
134 |// 456(sp) // \ 32/64 bit C frame info
135 |.define TONUM_LO, 452(sp) // |
136 |.define TONUM_HI, 448(sp) // |
137 |.define TMPD_LO, 444(sp) // |
138 |.define TMPD_HI, 440(sp) // |
139 |.define SAVE_CR, 432(sp) // | 64 bit CR save.
140 |.define SAVE_ERRF, 424(sp) // > Parameter save area.
141 |.define SAVE_NRES, 420(sp) // |
142 |.define SAVE_L, 416(sp) // |
143 |.define SAVE_PC, 412(sp) // |
144 |.define SAVE_MULTRES, 408(sp) // |
145 |.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain.
146 |// 392(sp) // Reserved.
147 |.define CFRAME_SPACE, 384 // Delta for sp.
148 |// Back chain for sp: 384(sp) <-- sp entering interpreter
149 |.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part.
150 |.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves.
151 |.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves.
152 |// 80(sp) // Needed for 16 byte stack frame alignment.
153 |// 16(sp) // Callee parameter save area (ABI mandated).
154 |// 8(sp) // Reserved
155 |// Back chain for sp: 0(sp) <-- sp while in interpreter
156 |// 32 bit sp stored in hi-part of 0(sp).
158 |.define TMPD_BLO, 447(sp)
159 |.define TMPD, TMPD_HI
160 |.define TONUM_D, TONUM_HI
164 |// 508(sp) // \ 32 bit C frame info.
165 |.define SAVE_ERRF, 472(sp) // |
166 |.define SAVE_NRES, 468(sp) // |
167 |.define SAVE_L, 464(sp) // > Parameter save area.
168 |.define SAVE_PC, 460(sp) // |
169 |.define SAVE_MULTRES, 456(sp) // |
170 |.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain.
171 |.define SAVE_LR, 416(sp)
172 |.define CFRAME_SPACE, 400 // Delta for sp.
173 |// Back chain for sp: 400(sp) <-- sp entering interpreter
174 |.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves.
175 |.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves.
176 |// 48(sp) // Callee parameter save area (ABI mandated).
177 |.define SAVE_TOC, 40(sp) // TOC save area.
178 |.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated).
179 |.define TMPD_HI, 32(sp) // /
180 |.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated).
181 |.define TONUM_HI, 24(sp) // /
182 |// Next frame lr: 16(sp)
183 |.define SAVE_CR, 8(sp) // 64 bit CR save.
184 |// Back chain for sp: 0(sp) <-- sp while in interpreter
186 |.define TMPD_BLO, 39(sp)
187 |.define TMPD, TMPD_HI
188 |.define TONUM_D, TONUM_HI
193 |.define SAVE_LR, 276(sp)
194 |.define CFRAME_SPACE, 272 // Delta for sp.
195 |// Back chain for sp: 272(sp) <-- sp entering interpreter
196 |.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves.
197 |.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves.
198 |.define SAVE_CR, 52(sp) // 32 bit CR save.
199 |.define SAVE_ERRF, 48(sp) // 32 bit C frame info.
200 |.define SAVE_NRES, 44(sp)
201 |.define SAVE_CFRAME, 40(sp)
202 |.define SAVE_L, 36(sp)
203 |.define SAVE_PC, 32(sp)
204 |.define SAVE_MULTRES, 28(sp)
205 |.define UNUSED1, 24(sp)
206 |.define TMPD_LO, 20(sp)
207 |.define TMPD_HI, 16(sp)
208 |.define TONUM_LO, 12(sp)
209 |.define TONUM_HI, 8(sp)
210 |// Next frame lr: 4(sp)
211 |// Back chain for sp: 0(sp) <-- sp while in interpreter
213 |.define TMPD_BLO, 23(sp)
214 |.define TMPD, TMPD_HI
215 |.define TONUM_D, TONUM_HI
221 | std r..reg, SAVE_GPR_+(reg-14)*8(sp)
223 | stw r..reg, SAVE_GPR_+(reg-14)*4(sp)
225 | stfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
229 | ld r..reg, SAVE_GPR_+(reg-14)*8(sp)
231 | lwz r..reg, SAVE_GPR_+(reg-14)*4(sp)
233 | lfd f..reg, SAVE_FPR_+(reg-14)*8(sp)
237 |.if GPR64 and not FRAME32
238 | stdu sp, -CFRAME_SPACE(sp)
240 | stwu sp, -CFRAME_SPACE(sp)
242 | save_ 14; save_ 15; save_ 16
244 | save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22
245 |.if GPR64 and not FRAME32
250 | save_ 23; save_ 24; save_ 25
252 | save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31
258 | .toc std TOCREG, SAVE_TOC
262 |.if GPR64 and not FRAME32
272 | rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19
274 |.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif
275 | rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25
276 |.if PPE; mtocrf 0x10, r12; .endif
277 | rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31
278 |.if PPE; mtocrf 0x08, r12; .endif
279 | addi sp, sp, CFRAME_SPACE
282 |// Type definitions. Some of these are only used for documentation.
283 |.type L, lua_State, LREG
284 |.type GL, global_State
285 |.type TVALUE, TValue
289 |.type LFUNC, GCfuncL
290 |.type CFUNC, GCfuncC
291 |.type PROTO, GCproto
292 |.type UPVAL, GCupval
295 |.type TRACE, GCtrace
297 |//-----------------------------------------------------------------------
299 |// These basic macros should really be part of DynASM.
300 |.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro
301 |.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro
302 |.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro
303 |.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro
304 |.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro
306 |// Trap for not-yet-implemented parts.
307 |.macro NYI; tw 4, sp, sp; .endmacro
309 |// int/FP conversions.
310 |.macro tonum_i, freg, reg
311 | xoris reg, reg, 0x8000
314 | fsub freg, freg, TONUM
317 |.macro tonum_u, freg, reg
320 | fsub freg, freg, TOBIT
323 |.macro toint, reg, freg, tmpfreg
324 | fctiwz tmpfreg, freg
329 |.macro toint, reg, freg
330 | toint reg, freg, freg
333 |//-----------------------------------------------------------------------
335 |// Access to frame relative to BASE.
336 |.define FRAME_PC, -8
337 |.define FRAME_FUNC, -4
339 |// Instruction decode.
340 |.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
341 |.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro
342 |.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
343 |.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
344 |.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
345 |.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
347 |.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
348 |.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
350 |// Instruction fetch.
355 |// Instruction decode+dispatch. Note: optimized for e300!
357 | decode_OPP TMP1, INS
358 | lpx TMP0, DISPATCH, TMP1
371 |// Instruction footer.
373 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
374 | .define ins_next, ins_NEXT
375 | .define ins_next_, ins_NEXT
376 | .define ins_next1, ins_NEXT1
377 | .define ins_next2, ins_NEXT2
379 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
380 | // Affects only certain kinds of benchmarks (and only with -j off).
395 |// Call decode and dispatch.
397 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
398 | lwz PC, LFUNC:RB->pc
401 | decode_OPP TMP1, INS
403 | lpx TMP0, DISPATCH, TMP1
410 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
411 | stw PC, FRAME_PC(BASE)
415 |//-----------------------------------------------------------------------
417 |// Macros to test operand types.
418 |.macro checknum, reg; cmplw reg, TISNUM; .endmacro
419 |.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro
420 |.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro
421 |.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro
422 |.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro
423 |.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro
427 | addis PC, PC, -(BCBIAS_J*4 >> 16)
431 |// Assumes DISPATCH is relative to GL.
432 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
433 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
435 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
437 |.macro hotcheck, delta, target
438 | rlwinm TMP1, PC, 31, 25, 30
439 | addi TMP1, TMP1, GG_DISP2HOT
440 | lhzx TMP2, DISPATCH, TMP1
441 | addic. TMP2, TMP2, -delta
442 | sthx TMP2, DISPATCH, TMP1
447 | hotcheck HOTCOUNT_LOOP, ->vm_hotloop
451 | hotcheck HOTCOUNT_CALL, ->vm_hotcall
454 |// Set current VM state. Uses TMP0.
455 |.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
456 |.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
458 |// Move table write barrier back. Overwrites mark and tmp.
459 |.macro barrierback, tab, mark, tmp
460 | lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
461 | // Assumes LJ_GC_BLACK is 0x04.
462 | rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
463 | stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
464 | stb mark, tab->marked
465 | stw tmp, tab->gclist
468 |//-----------------------------------------------------------------------
470 /* Generate subroutines used by opcodes and other parts of the VM. */
471 /* The .code_sub section should be last to help static branch prediction. */
472 static void build_subroutines(BuildCtx *ctx)
476 |//-----------------------------------------------------------------------
477 |//-- Return handling ----------------------------------------------------
478 |//-----------------------------------------------------------------------
481 | // See vm_return. Also: TMP2 = previous base.
482 | andix. TMP0, PC, FRAME_P
484 | beq ->cont_dispatch
486 | // Return from pcall or xpcall fast func.
487 | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
488 | mr BASE, TMP2 // Restore caller base.
489 | // Prepending may overwrite the pcall frame, so do it at the end.
490 | stwu TMP1, FRAME_PC(RA) // Prepend true to results.
493 | addi RD, RD, 8 // RD = (nresults+1)*8.
494 | andix. TMP0, PC, FRAME_TYPE
496 | li CRET1, LUA_YIELD
497 | beq cr1, ->vm_unwind_c_eh
499 | beq ->BC_RET_Z // Handle regular return to Lua.
502 | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
503 | // TMP0 = PC & FRAME_TYPE
504 | cmpwi TMP0, FRAME_C
505 | rlwinm TMP2, PC, 0, 0, 28
507 | sub TMP2, BASE, TMP2 // TMP2 = previous base.
510 | addic. TMP1, RD, -8
512 | lwz TMP2, SAVE_NRES
518 | addic. TMP1, TMP1, -8
526 | cmpw TMP2, RD // More/less results wanted?
529 | stp BASE, L->top // Store new top.
532 | lp TMP0, SAVE_CFRAME // Restore previous C frame.
533 | li CRET1, 0 // Ok return status for vm_pcall.
534 | stp TMP0, L->cframe
541 | ble >7 // Less results wanted?
542 | // More results wanted. Check stack size and fill up results with nil.
543 | lwz TMP1, L->maxstack
546 | stw TISNIL, 0(BASE)
551 |7: // Less results wanted.
552 | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case?
554 | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1
555 | and TMP0, TMP0, TMP1
556 | sub BASE, BASE, TMP0 // Either keep top or shrink it.
559 |8: // Corner case: need to grow stack for filling up results.
560 | // This can happen if:
561 | // - A C function grows the stack (a lot).
562 | // - The GC shrinks the stack in between.
563 | // - A return back from a lua_call() with (high) nresults adjustment.
564 | stp BASE, L->top // Save current top held in BASE (yes).
568 | bl extern lj_state_growstack // (lua_State *L, int n)
569 | lwz TMP2, SAVE_NRES
572 | lp BASE, L->top // Need the (realloced) L->top in BASE.
575 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
576 | // (void *cframe, int errcode)
579 |->vm_unwind_c_eh: // Landing pad for external unwinder.
581 | .toc ld TOCREG, SAVE_TOC
582 | li TMP0, ~LJ_VMST_C
583 | lwz GL:TMP1, L->glref
584 | stw TMP0, GL:TMP1->vmstate
587 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
590 | rldicr sp, CARG1, 0, 61
592 | rlwinm sp, CARG1, 0, 0, 29
594 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
596 | .toc ld TOCREG, SAVE_TOC
597 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
599 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
600 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
604 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
608 | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
609 | la RA, -8(BASE) // Results start at BASE-8.
611 | addi DISPATCH, DISPATCH, GG_G2DISP
612 | stw TMP1, 0(RA) // Prepend false to error message.
613 | li RD, 16 // 2 results: false + error message.
618 |//-----------------------------------------------------------------------
619 |//-- Grow stack for calls -----------------------------------------------
620 |//-----------------------------------------------------------------------
622 |->vm_growstack_c: // Grow stack for C function.
623 | li CARG2, LUA_MINSTACK
626 |->vm_growstack_l: // Grow stack for Lua function.
627 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
631 | addi PC, PC, 4 // Must point after first instruction.
635 | // L->base = new base, L->top = top
638 | bl extern lj_state_growstack // (lua_State *L, int n)
641 | lwz LFUNC:RB, FRAME_FUNC(BASE)
643 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
644 | ins_callt // Just retry the call.
646 |//-----------------------------------------------------------------------
647 |//-- Entry points into the assembler VM ---------------------------------
648 |//-----------------------------------------------------------------------
650 |->vm_resume: // Setup C frame and resume thread.
651 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
654 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
656 | lbz TMP1, L->status
659 | addi TMP0, sp, CFRAME_RESUME
660 | addi DISPATCH, DISPATCH, GG_G2DISP
661 | stw CARG3, SAVE_NRES
663 | stw CARG3, SAVE_ERRF
664 | stp TMP0, L->cframe
665 | stp CARG3, SAVE_CFRAME
666 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
669 | // Resume after yield (like a return).
672 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
674 | lwz PC, FRAME_PC(BASE)
675 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
676 | stb CARG3, L->status
678 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
682 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
688 | andix. TMP0, PC, FRAME_TYPE
695 |->vm_pcall: // Setup protected C frame and enter VM.
696 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
699 | stw CARG4, SAVE_ERRF
702 |->vm_call: // Setup C frame and enter VM.
703 | // (lua_State *L, TValue *base, int nres1)
707 |1: // Entry point for vm_pcall above (PC = ftype).
708 | lp TMP1, L:CARG1->cframe
709 | stw CARG3, SAVE_NRES
713 | stp sp, L->cframe // Add our C frame to cframe chain.
714 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
715 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
716 | stp TMP1, SAVE_CFRAME
717 | addi DISPATCH, DISPATCH, GG_G2DISP
719 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
720 | lp TMP2, L->base // TMP2 = old base (used in vmeta_call).
721 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
723 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
727 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
729 | sub PC, PC, TMP2 // PC = frame delta + frame type
731 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
732 | sub NARGS8:RC, TMP1, BASE
740 | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
741 | lwz TMP0, FRAME_PC(BASE)
742 | lwz LFUNC:RB, FRAME_FUNC(BASE)
743 | checkfunc TMP0; bne ->vmeta_call
745 |->vm_call_dispatch_f:
747 | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
749 |->vm_cpcall: // Setup protected C frame, call C.
750 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
753 | lwz TMP0, L:CARG1->stack
756 | stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
757 | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
759 | stp sp, L->cframe // Add our C frame to cframe chain.
760 | .toc lp CARG4, 0(CARG4)
762 | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
763 | stw TMP2, SAVE_ERRF // No error function.
764 | stp TMP1, SAVE_CFRAME
766 | bctrl // (lua_State *L, lua_CFunction func, void *ud)
773 | lwz DISPATCH, L->glref // Setup pointer to dispatch table.
775 | addi DISPATCH, DISPATCH, GG_G2DISP
776 | bne <3 // Else continue with the call.
777 | b ->vm_leave_cp // No base? Just remove C frame.
779 |//-----------------------------------------------------------------------
780 |//-- Metamethod handling ------------------------------------------------
781 |//-----------------------------------------------------------------------
783 |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
784 |// stack, so BASE doesn't need to be reloaded across these calls.
786 |//-- Continuation dispatch ----------------------------------------------
789 | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
790 | lwz TMP0, -12(BASE) // Continuation.
792 | mr BASE, TMP2 // Restore caller BASE.
793 | lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
797 | lwz PC, -16(RB) // Restore PC from [cont|PC].
799 | lwz TMP1, LFUNC:TMP1->pc
800 | stwx TISNIL, RA, TMP2 // Ensure one valid arg.
804 | lwz KBASE, PC2PROTO(k)(TMP1)
805 | // BASE = base, RA = resultptr, RB = meta base
807 | bctr // Jump to continuation.
811 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
812 | // cont = 0: tailcall from C function.
818 |->cont_cat: // RA = resultptr, RB = meta base
821 | decode_RB8 SAVE0, INS
823 | add TMP1, BASE, SAVE0
826 | sub CARG3, CARG2, TMP1
833 |//-- Table indexing metamethods -----------------------------------------
836 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
839 | stw STR:RC, 4(CARG3)
840 | add CARG2, BASE, RB
845 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
847 | stw TAB:RB, 4(CARG2)
848 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
851 | stw STR:RC, 4(CARG3)
855 |->vmeta_tgetb: // TMP0 = index
860 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
861 | add CARG2, BASE, RB
863 | stw TISNUM, 0(CARG3)
873 | add CARG2, BASE, RB
874 | add CARG3, BASE, RC
879 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
880 | // Returns TValue * (finished) or NULL (metamethod).
888 |3: // Call __index metamethod.
889 | // BASE = base, L->top = new base, stack = cont/func/t/k
890 | subfic TMP1, BASE, FRAME_CONT
892 | stw PC, -16(BASE) // [cont|PC]
894 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
895 | li NARGS8:RC, 16 // 2 args for func(t, k).
896 | b ->vm_call_dispatch_f
898 |//-----------------------------------------------------------------------
901 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
904 | stw STR:RC, 4(CARG3)
905 | add CARG2, BASE, RB
910 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
912 | stw TAB:RB, 4(CARG2)
913 | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
916 | stw STR:RC, 4(CARG3)
920 |->vmeta_tsetb: // TMP0 = index
925 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
926 | add CARG2, BASE, RB
928 | stw TISNUM, 0(CARG3)
938 | add CARG2, BASE, RB
939 | add CARG3, BASE, RC
944 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
945 | // Returns TValue * (finished) or NULL (metamethod).
949 | // NOBARRIER: lj_meta_tset ensures the table is not black.
954 |3: // Call __newindex metamethod.
955 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
956 | subfic TMP1, BASE, FRAME_CONT
958 | stw PC, -16(BASE) // [cont|PC]
960 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
961 | li NARGS8:RC, 24 // 3 args for func(t, k, v)
962 | stfd f0, 16(BASE) // Copy value to third argument.
963 | b ->vm_call_dispatch_f
965 |//-- Comparison metamethods ---------------------------------------------
973 | add CARG2, BASE, RA
979 | add CARG3, BASE, RD
982 | decode_OP1 CARG4, INS
983 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
984 | // Returns 0/1 or TValue * (metamethod).
988 | subfic CRET1, CRET1, 0
992 | decode_RD4 TMP2, INS
993 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
994 | and TMP2, TMP2, CRET1
999 |->cont_ra: // RA = resultptr
1002 | decode_RA8 TMP1, INS
1003 | stfdx f0, BASE, TMP1
1006 |->cont_condt: // RA = resultptr
1008 | .gpr64 extsw TMP0, TMP0
1009 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true.
1010 | subfe CRET1, CRET1, CRET1
1014 |->cont_condf: // RA = resultptr
1016 | .gpr64 extsw TMP0, TMP0
1017 | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false.
1018 | subfe CRET1, CRET1, CRET1
1022 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
1027 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1028 | // Returns 0/1 or TValue * (metamethod).
1038 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
1039 | // Returns 0/1 or TValue * (metamethod).
1043 |//-- Arithmetic metamethods ---------------------------------------------
1046 | add CARG3, KBASE, RC
1047 | add CARG4, BASE, RB
1062 | add CARG3, BASE, RB
1063 | add CARG4, KBASE, RC
1067 | add CARG3, BASE, RB
1068 | add CARG4, BASE, RC
1079 | add CARG2, BASE, RA
1083 | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
1084 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1085 | // Returns NULL (finished) or TValue * (metamethod).
1089 | // Call metamethod for binary op.
1091 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
1092 | sub TMP1, CRET1, BASE
1093 | stw PC, -16(CRET1) // [cont|PC]
1095 | addi PC, TMP1, FRAME_CONT
1097 | li NARGS8:RC, 16 // 2 args for func(o1, o2).
1098 | b ->vm_call_dispatch
1108 | bl extern lj_meta_len // (lua_State *L, TValue *o)
1109 | // Returns NULL (retry) or TValue * (metamethod base).
1112 | bne ->vmeta_binop // Binop call for compatibility.
1116 | b ->vmeta_binop // Binop call for compatibility.
1119 |//-- Call metamethod ----------------------------------------------------
1121 |->vmeta_call: // Resolve and call __call metamethod.
1122 | // TMP2 = old base, BASE = new base, RC = nargs*8
1124 | stp TMP2, L->base // This is the callers base!
1125 | subi CARG2, BASE, 8
1127 | add CARG3, BASE, RC
1128 | mr SAVE0, NARGS8:RC
1129 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1130 | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
1131 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1134 |->vmeta_callt: // Resolve __call for BC_CALLT.
1135 | // BASE = old base, RA = new base, RC = nargs*8
1141 | mr SAVE0, NARGS8:RC
1142 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1143 | lwz TMP1, FRAME_PC(BASE)
1144 | addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
1145 | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
1148 |//-- Argument coercion for 'for' statement ------------------------------
1156 | bl extern lj_meta_for // (lua_State *L, TValue *base)
1158 | decode_OP1 TMP0, SAVE0
1160 | decode_RA8 RA, SAVE0
1162 | cmpwi TMP0, BC_JFORI
1164 | decode_RD8 RD, SAVE0
1170 |//-----------------------------------------------------------------------
1171 |//-- Fast functions -----------------------------------------------------
1172 |//-----------------------------------------------------------------------
1174 |.macro .ffunc, name
1178 |.macro .ffunc_1, name
1180 | cmplwi NARGS8:RC, 8
1181 | lwz CARG3, 0(BASE)
1182 | lwz CARG1, 4(BASE)
1183 | blt ->fff_fallback
1186 |.macro .ffunc_2, name
1188 | cmplwi NARGS8:RC, 16
1189 | lwz CARG3, 0(BASE)
1190 | lwz CARG4, 8(BASE)
1191 | lwz CARG1, 4(BASE)
1192 | lwz CARG2, 12(BASE)
1193 | blt ->fff_fallback
1196 |.macro .ffunc_n, name
1198 | cmplwi NARGS8:RC, 8
1199 | lwz CARG3, 0(BASE)
1200 | lfd FARG1, 0(BASE)
1201 | blt ->fff_fallback
1202 | checknum CARG3; bge ->fff_fallback
1205 |.macro .ffunc_nn, name
1207 | cmplwi NARGS8:RC, 16
1208 | lwz CARG3, 0(BASE)
1209 | lfd FARG1, 0(BASE)
1210 | lwz CARG4, 8(BASE)
1211 | lfd FARG2, 8(BASE)
1212 | blt ->fff_fallback
1213 | checknum CARG3; bge ->fff_fallback
1214 | checknum CARG4; bge ->fff_fallback
1217 |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
1219 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
1220 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
1225 |//-- Base library: checks -----------------------------------------------
1228 | li TMP1, LJ_TFALSE
1230 | cmplw cr1, CARG3, TMP1
1231 | lwz PC, FRAME_PC(BASE)
1232 | bge cr1, ->fff_fallback
1234 | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
1236 | beq ->fff_res // Done if exactly 1 argument.
1241 | lfdx f0, BASE, TMP1
1242 | stfdx f0, RA, TMP1
1243 | addi TMP1, TMP1, 8
1248 | cmplwi NARGS8:RC, 8
1249 | lwz CARG1, 0(BASE)
1250 | blt ->fff_fallback
1251 | .gpr64 extsw CARG1, CARG1
1252 | subfc TMP0, TISNUM, CARG1
1253 | subfe TMP2, CARG1, CARG1
1254 | orc TMP1, TMP2, TMP0
1255 | addi TMP1, TMP1, ~LJ_TISNUM+1
1256 | slwi TMP1, TMP1, 3
1257 | la TMP2, CFUNC:RB->upvalue
1258 | lfdx FARG1, TMP2, TMP1
1261 |//-- Base library: getters and setters ---------------------------------
1263 |.ffunc_1 getmetatable
1264 | checktab CARG3; bne >6
1265 |1: // Field metatable must be at same offset for GCtab and GCudata!
1266 | lwz TAB:CARG1, TAB:CARG1->metatable
1269 | cmplwi TAB:CARG1, 0
1270 | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
1272 | lwz TMP0, TAB:CARG1->hmask
1273 | li CARG3, LJ_TTAB // Use metatable as default result.
1274 | lwz TMP1, STR:RC->hash
1275 | lwz NODE:TMP2, TAB:CARG1->node
1276 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
1277 | slwi TMP0, TMP1, 5
1278 | slwi TMP1, TMP1, 3
1279 | sub TMP1, TMP0, TMP1
1280 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
1281 |3: // Rearranged logic, because we expect _not_ to find the key.
1282 | lwz CARG4, NODE:TMP2->key
1283 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
1284 | lwz CARG2, NODE:TMP2->val
1285 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
1286 | checkstr CARG4; bne >4
1287 | cmpw TMP0, STR:RC; beq >5
1289 | lwz NODE:TMP2, NODE:TMP2->next
1290 | cmplwi NODE:TMP2, 0
1291 | beq ->fff_restv // Not found, keep default result.
1295 | beq ->fff_restv // Ditto for nil value.
1296 | mr CARG3, CARG2 // Return value of mt.__metatable.
1301 | cmpwi CARG3, LJ_TUDATA; beq <1
1302 | .gpr64 extsw CARG3, CARG3
1303 | subfc TMP0, TISNUM, CARG3
1304 | subfe TMP2, CARG3, CARG3
1305 | orc TMP1, TMP2, TMP0
1306 | addi TMP1, TMP1, ~LJ_TISNUM+1
1307 | slwi TMP1, TMP1, 2
1308 | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH)
1309 | lwzx TAB:CARG1, TMP2, TMP1
1312 |.ffunc_2 setmetatable
1313 | // Fast path: no mt for table yet and not clearing the mt.
1314 | checktab CARG3; bne ->fff_fallback
1315 | lwz TAB:TMP1, TAB:CARG1->metatable
1316 | checktab CARG4; bne ->fff_fallback
1317 | cmplwi TAB:TMP1, 0
1318 | lbz TMP3, TAB:CARG1->marked
1319 | bne ->fff_fallback
1320 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
1321 | stw TAB:CARG2, TAB:CARG1->metatable
1323 | barrierback TAB:CARG1, TMP3, TMP0
1327 | cmplwi NARGS8:RC, 16
1328 | lwz CARG4, 0(BASE)
1329 | lwz TAB:CARG2, 4(BASE)
1330 | blt ->fff_fallback
1331 | checktab CARG4; bne ->fff_fallback
1334 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1335 | // Returns cTValue *.
1336 | lfd FARG1, 0(CRET1)
1339 |//-- Base library: conversions ------------------------------------------
1342 | // Only handles the number case inline (without a base argument).
1343 | cmplwi NARGS8:RC, 8
1344 | lwz CARG1, 0(BASE)
1345 | lfd FARG1, 0(BASE)
1346 | bne ->fff_fallback // Exactly one argument.
1347 | checknum CARG1; bgt ->fff_fallback
1351 | // Only handles the string or number case inline.
1353 | // A __tostring method in the string base metatable is ignored.
1354 | beq ->fff_restv // String key?
1355 | // Handle numbers inline, unless a number base metatable is present.
1356 | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH)
1358 | cmplwi cr1, TMP0, 0
1359 | stp BASE, L->base // Add frame since C call can throw.
1360 | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq
1361 | stw PC, SAVE_PC // Redundant (but a defined value).
1362 | beq ->fff_fallback
1367 | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o)
1369 | bl extern lj_str_fromnum // (lua_State *L, lua_Number *np)
1371 | // Returns GCstr *.
1375 |//-- Base library: iterators -------------------------------------------
1378 | cmplwi NARGS8:RC, 8
1379 | lwz CARG1, 0(BASE)
1380 | lwz TAB:CARG2, 4(BASE)
1381 | blt ->fff_fallback
1382 | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil.
1384 | lwz PC, FRAME_PC(BASE)
1385 | bne ->fff_fallback
1386 | stp BASE, L->base // Add frame since C call can throw.
1388 | stp BASE, L->top // Dummy frame length is ok.
1391 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1392 | // Returns 0 at end of traversal.
1395 | beq ->fff_restv // End of traversal: return nil.
1396 | lfd f0, 8(BASE) // Copy key and value to results.
1406 | lwz PC, FRAME_PC(BASE)
1407 | bne ->fff_fallback
1409 | lwz TAB:TMP2, TAB:CARG1->metatable
1410 | lfd f0, CFUNC:RB->upvalue[0]
1411 | cmplwi TAB:TMP2, 0
1413 | bne ->fff_fallback
1415 | lfd f0, CFUNC:RB->upvalue[0]
1418 | stw TISNIL, 8(BASE)
1424 | cmplwi NARGS8:RC, 16
1425 | lwz CARG3, 0(BASE)
1426 | lwz TAB:CARG1, 4(BASE)
1427 | lwz CARG4, 8(BASE)
1429 | lwz TMP2, 12(BASE)
1431 | lfd FARG2, 8(BASE)
1433 | blt ->fff_fallback
1435 | checknum cr1, CARG4
1436 | lwz PC, FRAME_PC(BASE)
1438 | bne ->fff_fallback
1439 | bne cr1, ->fff_fallback
1443 | bne ->fff_fallback
1445 | bge cr1, ->fff_fallback
1447 | toint TMP2, FARG2, f0
1449 | lwz TMP0, TAB:CARG1->asize
1450 | lwz TMP1, TAB:CARG1->array
1452 | fadd FARG2, FARG2, FARG1
1454 | addi TMP2, TMP2, 1
1459 | slwi TMP3, TMP2, 3
1462 | slwi TMP3, TMP2, 3
1465 | ble >2 // Not in array part?
1466 | lwzx TMP2, TMP1, TMP3
1467 | lfdx f0, TMP1, TMP3
1471 | beq ->fff_res // End of iteration, return 0 results.
1475 |2: // Check for empty hash part first. Otherwise call C function.
1476 | lwz TMP0, TAB:CARG1->hmask
1481 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1482 | // Returns cTValue * or NULL.
1486 | lwz TMP2, 0(CRET1)
1492 | lwz PC, FRAME_PC(BASE)
1493 | bne ->fff_fallback
1495 | lwz TAB:TMP2, TAB:CARG1->metatable
1496 | lfd f0, CFUNC:RB->upvalue[0]
1497 | cmplwi TAB:TMP2, 0
1499 | bne ->fff_fallback
1501 | lfd f0, CFUNC:RB->upvalue[0]
1505 | stw TISNUM, 8(BASE)
1509 | stw ZERO, 12(BASE)
1514 |//-- Base library: catch errors ----------------------------------------
1517 | cmplwi NARGS8:RC, 8
1518 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
1519 | blt ->fff_fallback
1522 | // Remember active hook before pcall.
1523 | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31
1524 | subi NARGS8:RC, NARGS8:RC, 8
1525 | addi PC, TMP3, 8+FRAME_PCALL
1526 | b ->vm_call_dispatch
1529 | cmplwi NARGS8:RC, 16
1530 | lwz CARG4, 8(BASE)
1531 | lfd FARG2, 8(BASE)
1532 | lfd FARG1, 0(BASE)
1533 | blt ->fff_fallback
1534 | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH)
1536 | checkfunc CARG4; bne ->fff_fallback // Traceback must be a function.
1538 | // Remember active hook before pcall.
1539 | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31
1540 | stfd FARG2, 0(TMP2) // Swap function and traceback.
1541 | subi NARGS8:RC, NARGS8:RC, 16
1542 | stfd FARG1, 8(TMP2)
1543 | addi PC, TMP1, 16+FRAME_PCALL
1544 | b ->vm_call_dispatch
1546 |//-- Coroutine library --------------------------------------------------
1548 |.macro coroutine_resume_wrap, resume
1550 |.ffunc_1 coroutine_resume
1551 | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback
1553 |.ffunc coroutine_wrap_aux
1554 | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr
1556 | lbz TMP0, L:CARG1->status
1557 | lp TMP1, L:CARG1->cframe
1558 | lp CARG2, L:CARG1->top
1559 | cmplwi cr0, TMP0, LUA_YIELD
1560 | lp TMP2, L:CARG1->base
1561 | cmplwi cr1, TMP1, 0
1562 | lwz TMP0, L:CARG1->maxstack
1563 | cmplw cr7, CARG2, TMP2
1564 | lwz PC, FRAME_PC(BASE)
1565 | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0
1566 | add TMP2, CARG2, NARGS8:RC
1567 | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD
1568 | cmplw cr1, TMP2, TMP0
1569 | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt
1571 | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov
1573 | blt cr6, ->fff_fallback
1576 | addi BASE, BASE, 8 // Keep resumed thread in stack for GC.
1577 | subi NARGS8:RC, NARGS8:RC, 8
1578 | subi TMP2, TMP2, 8
1580 | stp TMP2, L:CARG1->top
1583 |2: // Move args to coroutine.
1584 | cmpw TMP1, NARGS8:RC
1585 | lfdx f0, BASE, TMP1
1587 | stfdx f0, CARG2, TMP1
1588 | addi TMP1, TMP1, 8
1592 | mr L:SAVE0, L:CARG1
1594 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1595 | // Returns thread status.
1597 | lp TMP2, L:SAVE0->base
1598 | cmplwi CRET1, LUA_YIELD
1599 | lp TMP3, L:SAVE0->top
1604 | sub RD, TMP3, TMP2
1605 | lwz TMP0, L->maxstack
1607 | add TMP1, BASE, RD
1608 | beq >6 // No results?
1611 | bgt >9 // Need to grow stack?
1614 | stp TMP2, L:SAVE0->top // Clear coroutine stack.
1615 |5: // Move results from coroutine.
1617 | lfdx f0, TMP2, TMP1
1618 | stfdx f0, BASE, TMP1
1619 | addi TMP1, TMP1, 8
1622 | andix. TMP0, PC, FRAME_TYPE
1626 | stw TMP1, -8(BASE) // Prepend true to results.
1638 |8: // Coroutine returned with error (at co->top-1).
1640 | andix. TMP0, PC, FRAME_TYPE
1642 | li TMP1, LJ_TFALSE
1644 | stp TMP3, L:SAVE0->top // Remove error from coroutine stack.
1646 | stw TMP1, -8(BASE) // Prepend false to results.
1648 | stfd f0, 0(BASE) // Copy error message.
1653 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1656 |9: // Handle stack expansion on return from yield.
1659 | bl extern lj_state_growstack // (lua_State *L, int n)
1664 | coroutine_resume_wrap 1 // coroutine.resume
1665 | coroutine_resume_wrap 0 // coroutine.wrap
1667 |.ffunc coroutine_yield
1668 | lp TMP0, L->cframe
1669 | add TMP1, BASE, NARGS8:RC
1671 | andix. TMP0, TMP0, CFRAME_RESUME
1673 | li CRET1, LUA_YIELD
1674 | beq ->fff_fallback
1675 | stp ZERO, L->cframe
1676 | stb CRET1, L->status
1679 |//-- Math library -------------------------------------------------------
1685 | srawi TMP1, CARG1, 31
1686 | xor TMP2, TMP1, CARG1
1689 | sub CARG1, TMP2, TMP1
1693 | sub. CARG1, TMP2, TMP1
1697 | lwz PC, FRAME_PC(BASE)
1699 | stw TISNUM, -8(BASE)
1700 | stw CRET1, -4(BASE)
1703 | lus CARG3, 0x41e0 // 2^31.
1708 | bge ->fff_fallback
1709 | rlwinm CARG3, CARG3, 0, 1, 31
1713 | // CARG3/CARG1 = TValue result.
1714 | lwz PC, FRAME_PC(BASE)
1715 | stw CARG3, -8(BASE)
1717 | stw CARG1, -4(BASE)
1719 | // RA = results, PC = return.
1722 | // RA = results, RD = (nresults+1)*8, PC = return.
1723 | andix. TMP0, PC, FRAME_TYPE
1727 | decode_RB8 RB, INS
1729 | cmplw RB, RD // More results expected?
1730 | decode_RA8 TMP0, INS
1733 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1734 | sub BASE, RA, TMP0
1737 |6: // Fill up results with nil.
1740 | stwx TISNIL, RA, TMP1
1743 |.macro math_extern, func
1744 | .ffunc_n math_ .. func
1749 |.macro math_extern2, func
1750 | .ffunc_nn math_ .. func
1755 |.macro math_round, func
1756 | .ffunc_1 math_ .. func
1757 | checknum CARG3; beqy ->fff_restv
1758 | rlwinm TMP2, CARG3, 12, 21, 31
1759 | bge ->fff_fallback
1760 | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023
1761 | cmplwi cr1, TMP2, 31 // 0 <= exp < 31?
1762 | subfic TMP0, TMP2, 31
1764 | slwi TMP1, CARG3, 11
1765 | srwi TMP3, CARG1, 21
1766 | oris TMP1, TMP1, 0x8000
1767 | addi TMP2, TMP2, 1
1768 | or TMP1, TMP1, TMP3
1769 | slwi CARG2, CARG1, 11
1771 | slw TMP3, TMP1, TMP2
1772 | srw RD, TMP1, TMP0
1773 | or TMP3, TMP3, CARG2
1774 | srawi TMP2, CARG3, 31
1775 |.if "func" == "floor"
1776 | and TMP1, TMP3, TMP2
1777 | addic TMP0, TMP1, -1
1778 | subfe TMP1, TMP0, TMP1
1779 | add CARG1, RD, TMP1
1780 | xor CARG1, CARG1, TMP2
1781 | sub CARG1, CARG1, TMP2
1784 | andc TMP1, TMP3, TMP2
1785 | addic TMP0, TMP1, -1
1786 | subfe TMP1, TMP0, TMP1
1787 | add CARG1, RD, TMP1
1789 | xor CARG1, CARG1, TMP2
1790 | sub CARG1, CARG1, TMP2
1792 | // Overflow to 2^31.
1793 | lus CARG3, 0x41e0 // 2^31.
1798 | slwi TMP2, CARG3, 1
1799 | srawi TMP1, CARG3, 31
1800 | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo
1801 |.if "func" == "floor"
1802 | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1
1803 | subfic TMP2, TMP1, 0
1804 | subfe CARG1, CARG1, CARG1
1806 | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1
1807 | addic TMP2, TMP1, -1
1808 | subfe CARG1, TMP2, TMP1
1811 |4: // exp >= 31. Check for -(2^31).
1812 | xoris TMP1, TMP1, 0x8000
1813 | srawi TMP2, CARG3, 31
1814 |.if "func" == "floor"
1815 | or TMP1, TMP1, CARG2
1818 | orc TMP1, TMP1, TMP2
1821 | orc. TMP1, TMP1, TMP2
1823 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
1824 | lus CARG1, 0x8000 // -(2^31).
1827 | lfd FARG1, 0(BASE)
1836 | // NYI: use internal implementation.
1843 | fsqrt FARG1, FARG1
1850 | cmplwi NARGS8:RC, 8
1851 | lwz CARG3, 0(BASE)
1852 | lfd FARG1, 0(BASE)
1853 | bne ->fff_fallback // Need exactly 1 argument.
1854 | checknum CARG3; bge ->fff_fallback
1870 | math_extern2 atan2
1875 | lfd FARG2, CFUNC:RB->upvalue[0]
1876 | fmul FARG1, FARG1, FARG2
1881 | cmplwi NARGS8:RC, 16
1882 | lwz CARG3, 0(BASE)
1883 | lfd FARG1, 0(BASE)
1884 | lwz CARG4, 8(BASE)
1886 | lwz CARG2, 12(BASE)
1888 | lwz CARG1, 12(BASE)
1890 | blt ->fff_fallback
1891 | checknum CARG3; bge ->fff_fallback
1892 | checknum CARG4; bne ->fff_fallback
1894 |.ffunc_nn math_ldexp
1896 | toint CARG2, FARG2
1898 | toint CARG1, FARG2
1904 |.ffunc_n math_frexp
1906 | la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
1908 | la CARG1, DISPATCH_GL(tmptv)(DISPATCH)
1910 | lwz PC, FRAME_PC(BASE)
1912 | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH)
1915 | tonum_i FARG2, TMP1
1929 | la CARG2, -8(BASE)
1931 | la CARG1, -8(BASE)
1933 | lwz PC, FRAME_PC(BASE)
1936 | stfd FARG1, 0(BASE)
1940 |.macro math_minmax, name, ismax
1944 | addi TMP1, BASE, 8
1945 | add TMP2, BASE, NARGS8:RC
1947 |1: // Handle integers.
1948 | lwz CARG4, 0(TMP1)
1949 | cmplw cr1, TMP1, TMP2
1950 | lwz CARG2, 4(TMP1)
1951 | bge cr1, ->fff_resi
1953 | xoris TMP0, CARG1, 0x8000
1954 | xoris TMP3, CARG2, 0x8000
1956 | subfc TMP3, TMP3, TMP0
1957 | subfe TMP0, TMP0, TMP0
1959 | andc TMP3, TMP3, TMP0
1961 | and TMP3, TMP3, TMP0
1963 | add CARG1, TMP3, CARG2
1965 | rldicl CARG1, CARG1, 0, 32
1967 | addi TMP1, TMP1, 8
1970 | bge ->fff_fallback
1971 | // Convert intermediate result to number and continue below.
1972 | tonum_i FARG1, CARG1
1973 | lfd FARG2, 0(TMP1)
1976 | lfd FARG1, 0(BASE)
1977 | bge ->fff_fallback
1978 |5: // Handle numbers.
1979 | lwz CARG4, 0(TMP1)
1980 | cmplw cr1, TMP1, TMP2
1981 | lfd FARG2, 0(TMP1)
1982 | bge cr1, ->fff_resn
1983 | checknum CARG4; bge >7
1985 | fsub f0, FARG1, FARG2
1986 | addi TMP1, TMP1, 8
1988 | fsel FARG1, f0, FARG1, FARG2
1990 | fsel FARG1, f0, FARG2, FARG1
1993 |7: // Convert integer to number and continue above.
1994 | lwz CARG2, 4(TMP1)
1995 | bne ->fff_fallback
1996 | tonum_i FARG2, CARG2
2002 | lwzx CARG2, BASE, TMP1
2003 | lfdx FARG2, BASE, TMP1
2004 | cmplw cr1, TMP1, NARGS8:RC
2006 | bge cr1, ->fff_resn
2007 | bge ->fff_fallback
2008 | fsub f0, FARG1, FARG2
2009 | addi TMP1, TMP1, 8
2011 | fsel FARG1, f0, FARG1, FARG2
2013 | fsel FARG1, f0, FARG2, FARG1
2019 | math_minmax math_min, 0
2020 | math_minmax math_max, 1
2022 |//-- String library -----------------------------------------------------
2024 |.ffunc_1 string_len
2025 | checkstr CARG3; bne ->fff_fallback
2026 | lwz CRET1, STR:CARG1->len
2029 |.ffunc string_byte // Only handle the 1-arg case here.
2030 | cmplwi NARGS8:RC, 8
2031 | lwz CARG3, 0(BASE)
2032 | lwz STR:CARG1, 4(BASE)
2033 | bne ->fff_fallback // Need exactly 1 argument.
2035 | bne ->fff_fallback
2036 | lwz TMP0, STR:CARG1->len
2038 | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
2040 | lwz PC, FRAME_PC(BASE)
2046 | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end).
2047 | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8
2048 | subfe RD, TMP3, TMP0
2049 | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1.
2053 | lwz PC, FRAME_PC(BASE)
2054 | fsub f0, f0, TOBIT
2060 |.ffunc string_char // Only handle the 1-arg case here.
2062 | cmplwi NARGS8:RC, 8
2063 | lwz CARG3, 0(BASE)
2066 | bne ->fff_fallback // Exactly 1 argument.
2067 | checknum CARG3; bne ->fff_fallback
2070 | lfd FARG1, 0(BASE)
2071 | bne ->fff_fallback // Exactly 1 argument.
2072 | checknum CARG3; bge ->fff_fallback
2074 | la CARG2, TMPD_BLO
2077 | cmplwi TMP0, 255; bgt ->fff_fallback
2082 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
2083 | // Returns GCstr *.
2090 | cmplwi NARGS8:RC, 16
2091 | lwz CARG3, 16(BASE)
2096 | lwz STR:CARG1, 4(BASE)
2097 | blt ->fff_fallback
2098 | lwz CARG2, 8(BASE)
2100 | lwz TMP1, 12(BASE)
2108 | lwz TMP2, 20(BASE)
2109 | bne ->fff_fallback
2111 | checknum CARG2; bne ->fff_fallback
2113 | checknum CARG3; bge ->fff_fallback
2116 | checknum CARG2; bge ->fff_fallback
2118 | checkstr TMP0; bne ->fff_fallback
2122 | lwz TMP0, STR:CARG1->len
2123 | cmplw TMP0, TMP2 // len < end? (unsigned compare)
2124 | addi TMP3, TMP2, 1
2127 | cmpwi TMP1, 0 // start <= 0?
2128 | add TMP3, TMP1, TMP0
2131 | sub CARG3, TMP2, TMP1
2132 | addi CARG2, STR:CARG1, #STR-1
2133 | srawi TMP0, CARG3, 31
2134 | addi CARG3, CARG3, 1
2135 | add CARG2, CARG2, TMP1
2136 | andc CARG3, CARG3, TMP0
2138 | rldicl CARG2, CARG2, 0, 32
2139 | rldicl CARG3, CARG3, 0, 32
2143 |5: // Negative end or overflow.
2144 | cmpw TMP0, TMP2 // len >= end? (signed compare)
2145 | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1.
2147 | mr TMP2, TMP0 // Overflow: end = len.
2150 |7: // Negative start or underflow.
2151 | .gpr64 extsw TMP1, TMP1
2152 | addic CARG3, TMP1, -1
2153 | subfe CARG3, CARG3, CARG3
2154 | srawi CARG2, TMP3, 31 // Note: modifies carry.
2155 | andc TMP3, TMP3, CARG3
2156 | andc TMP1, TMP3, CARG2
2157 | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0)
2160 |.ffunc string_rep // Only handle the 1-char case inline.
2162 | cmplwi NARGS8:RC, 16
2164 | lwz STR:CARG1, 4(BASE)
2165 | lwz CARG4, 8(BASE)
2167 | lwz CARG3, 12(BASE)
2169 | lfd FARG2, 8(BASE)
2171 | bne ->fff_fallback // Exactly 2 arguments.
2172 | checkstr TMP0; bne ->fff_fallback
2174 | checknum CARG4; bne ->fff_fallback
2176 | checknum CARG4; bge ->fff_fallback
2177 | toint CARG3, FARG2
2179 | lwz TMP0, STR:CARG1->len
2181 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2182 | ble >2 // Count <= 0? (or non-int)
2184 | subi TMP2, CARG3, 1
2185 | blt >2 // Zero length string?
2186 | cmplw cr1, TMP1, CARG3
2187 | bne ->fff_fallback // Fallback for > 1-char strings.
2188 | lbz TMP0, STR:CARG1[1]
2189 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2190 | blt cr1, ->fff_fallback
2191 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2193 | stbx TMP0, CARG2, TMP2
2194 | subi TMP2, TMP2, 1
2197 |2: // Return empty string.
2198 | la STR:CARG1, DISPATCH_GL(strempty)(DISPATCH)
2202 |.ffunc string_reverse
2204 | cmplwi NARGS8:RC, 8
2205 | lwz CARG3, 0(BASE)
2206 | lwz STR:CARG1, 4(BASE)
2207 | blt ->fff_fallback
2209 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2210 | bne ->fff_fallback
2211 | lwz CARG3, STR:CARG1->len
2212 | la CARG1, #STR(STR:CARG1)
2213 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2216 | subi TMP3, CARG3, 1
2217 | blt ->fff_fallback
2218 |1: // Reverse string copy.
2220 | lbzx TMP1, CARG1, TMP2
2222 | stbx TMP1, CARG2, TMP3
2223 | subi TMP3, TMP3, 1
2224 | addi TMP2, TMP2, 1
2227 |.macro ffstring_case, name, lo
2230 | cmplwi NARGS8:RC, 8
2231 | lwz CARG3, 0(BASE)
2232 | lwz STR:CARG1, 4(BASE)
2233 | blt ->fff_fallback
2235 | lwz TMP1, DISPATCH_GL(tmpbuf.sz)(DISPATCH)
2236 | bne ->fff_fallback
2237 | lwz CARG3, STR:CARG1->len
2238 | la CARG1, #STR(STR:CARG1)
2239 | lp CARG2, DISPATCH_GL(tmpbuf.buf)(DISPATCH)
2242 | blt ->fff_fallback
2243 |1: // ASCII case conversion.
2245 | lbzx TMP1, CARG1, TMP2
2247 | subi TMP0, TMP1, lo
2248 | xori TMP3, TMP1, 0x20
2249 | addic TMP0, TMP0, -26
2250 | subfe TMP3, TMP3, TMP3
2251 | rlwinm TMP3, TMP3, 0, 26, 26 // x &= 0x20.
2252 | xor TMP1, TMP1, TMP3
2253 | stbx TMP1, CARG2, TMP2
2254 | addi TMP2, TMP2, 1
2258 |ffstring_case string_lower, 65
2259 |ffstring_case string_upper, 97
2261 |//-- Table library ------------------------------------------------------
2263 |.ffunc_1 table_getn
2264 | checktab CARG3; bne ->fff_fallback
2265 | bl extern lj_tab_len // (GCtab *t)
2266 | // Returns uint32_t (but less than 2^31).
2269 |//-- Bit library --------------------------------------------------------
2271 |.macro .ffunc_bit, name
2273 | .ffunc_1 bit_..name
2274 | checknum CARG3; bnel ->fff_tobit_fb
2276 | .ffunc_n bit_..name
2277 | fadd FARG1, FARG1, TOBIT
2279 | lwz CARG1, TMPD_LO
2283 |.macro .ffunc_bit_op, name, ins
2285 | addi TMP1, BASE, 8
2286 | add TMP2, BASE, NARGS8:RC
2288 | lwz CARG4, 0(TMP1)
2289 | cmplw cr1, TMP1, TMP2
2291 | lwz CARG2, 4(TMP1)
2293 | lfd FARG1, 0(TMP1)
2295 | bgey cr1, ->fff_resi
2298 | bnel ->fff_bitop_fb
2300 | fadd FARG1, FARG1, TOBIT
2301 | bge ->fff_fallback
2303 | lwz CARG2, TMPD_LO
2305 | ins CARG1, CARG1, CARG2
2306 | addi TMP1, TMP1, 8
2310 |.ffunc_bit_op band, and
2311 |.ffunc_bit_op bor, or
2312 |.ffunc_bit_op bxor, xor
2315 | rotlwi TMP0, CARG1, 8
2316 | rlwimi TMP0, CARG1, 24, 0, 7
2317 | rlwimi TMP0, CARG1, 24, 16, 23
2325 |.macro .ffunc_bit_sh, name, ins, shmod
2327 | .ffunc_2 bit_..name
2328 | checknum CARG3; bnel ->fff_tobit_fb
2329 | // Note: no inline conversion from number for 2nd argument!
2330 | checknum CARG4; bne ->fff_fallback
2332 | .ffunc_nn bit_..name
2333 | fadd FARG1, FARG1, TOBIT
2334 | fadd FARG2, FARG2, TOBIT
2336 | lwz CARG1, TMPD_LO
2338 | lwz CARG2, TMPD_LO
2341 | rlwinm CARG2, CARG2, 0, 27, 31
2345 | ins CRET1, CARG1, CARG2
2349 |.ffunc_bit_sh lshift, slw, 1
2350 |.ffunc_bit_sh rshift, srw, 1
2351 |.ffunc_bit_sh arshift, sraw, 1
2352 |.ffunc_bit_sh rol, rotlw, 0
2353 |.ffunc_bit_sh ror, rotlw, 2
2360 | tonum_i FARG1, CRET1
2363 | lwz PC, FRAME_PC(BASE)
2365 | stfd FARG1, -8(BASE)
2368 |// Fallback FP number to bit conversion.
2371 | lfd FARG1, 0(BASE)
2372 | bgt ->fff_fallback
2373 | fadd FARG1, FARG1, TOBIT
2375 | lwz CARG1, TMPD_LO
2380 | lfd FARG1, 0(TMP1)
2381 | bgt ->fff_fallback
2382 | fadd FARG1, FARG1, TOBIT
2384 | lwz CARG2, TMPD_LO
2388 |//-----------------------------------------------------------------------
2390 |->fff_fallback: // Call fast function fallback handler.
2391 | // BASE = new base, RB = CFUNC, RC = nargs*8
2392 | lp TMP3, CFUNC:RB->f
2393 | add TMP1, BASE, NARGS8:RC
2394 | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC.
2395 | addi TMP0, TMP1, 8*LUA_MINSTACK
2396 | lwz TMP2, L->maxstack
2397 | stw PC, SAVE_PC // Redundant (but a defined value).
2398 | .toc lp TMP3, 0(TMP3)
2403 | bgt >5 // Need to grow stack.
2405 | bctrl // (lua_State *L)
2406 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2411 | bgt ->fff_res // Returned nresults+1?
2412 |1: // Returned 0 or -1: retry fast path.
2414 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2415 | sub NARGS8:RC, TMP0, BASE
2416 | bne ->vm_call_tail // Returned -1?
2417 | ins_callt // Returned 0: retry fast path.
2419 |// Reconstruct previous base for vmeta_call during tailcall.
2421 | andix. TMP0, PC, FRAME_TYPE
2422 | rlwinm TMP1, PC, 0, 0, 28
2425 | decode_RA8 TMP1, INS
2426 | addi TMP1, TMP1, 8
2428 | sub TMP2, BASE, TMP1
2429 | b ->vm_call_dispatch // Resolve again for tailcall.
2431 |5: // Grow stack for fallback handler.
2432 | li CARG2, LUA_MINSTACK
2433 | bl extern lj_state_growstack // (lua_State *L, int n)
2435 | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry.
2438 |->fff_gcstep: // Call GC step function.
2439 | // BASE = new base, RC = nargs*8
2442 | add TMP0, BASE, NARGS8:RC
2443 | stw PC, SAVE_PC // Redundant (but a defined value).
2446 | bl extern lj_gc_step // (lua_State *L)
2450 | sub NARGS8:RC, TMP0, BASE
2451 | lwz CFUNC:RB, FRAME_FUNC(BASE)
2454 |//-----------------------------------------------------------------------
2455 |//-- Special dispatch targets -------------------------------------------
2456 |//-----------------------------------------------------------------------
2458 |->vm_record: // Dispatch target for recording phase.
2460 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2461 | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent.
2463 | // Decrement the hookcount for consistency, but always do the call.
2464 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2465 | andix. TMP0, TMP3, HOOK_ACTIVE
2467 | subi TMP2, TMP2, 1
2468 | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT
2470 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2474 |->vm_rethook: // Dispatch target for return hooks.
2475 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2476 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2478 |5: // Re-dispatch to static ins.
2479 | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS.
2480 | lpx TMP0, DISPATCH, TMP1
2484 |->vm_inshook: // Dispatch target for instr/line hooks.
2485 | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH)
2486 | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2487 | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active?
2488 | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0
2491 | cmpwi cr1, TMP0, 0
2492 | addic. TMP2, TMP2, -1
2494 | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH)
2499 | stw MULTRES, SAVE_MULTRES
2502 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2503 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2506 |4: // Re-dispatch to static ins.
2508 | decode_OPP TMP1, INS
2509 | decode_RB8 RB, INS
2510 | addi TMP1, TMP1, GG_DISP2STATIC
2511 | decode_RD8 RD, INS
2512 | lpx TMP0, DISPATCH, TMP1
2513 | decode_RA8 RA, INS
2514 | decode_RC8 RC, INS
2518 |->cont_hook: // Continue from hook yield.
2520 | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins.
2523 |->vm_hotloop: // Hot loop counter underflow.
2525 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2526 | addi CARG1, DISPATCH, GG_DISP2J
2528 | lwz TMP1, LFUNC:TMP1->pc
2530 | stw L, DISPATCH_J(L)(DISPATCH)
2531 | lbz TMP1, PC2PROTO(framesize)(TMP1)
2533 | slwi TMP1, TMP1, 3
2534 | add TMP1, BASE, TMP1
2536 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2540 |->vm_callhook: // Dispatch target for call hooks.
2546 |->vm_hotcall: // Hot call counter underflow.
2551 | add TMP0, BASE, RC
2557 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2558 | // Returns ASMFunction.
2561 | stw ZERO, SAVE_PC // Invalidate for subsequent line hook.
2562 | sub NARGS8:RC, TMP0, BASE
2564 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2569 |//-----------------------------------------------------------------------
2570 |//-- Trace exit handler -------------------------------------------------
2571 |//-----------------------------------------------------------------------
2573 |.macro savex_, a, b, c, d
2574 | stfd f..a, 16+a*8(sp)
2575 | stfd f..b, 16+b*8(sp)
2576 | stfd f..c, 16+c*8(sp)
2577 | stfd f..d, 16+d*8(sp)
2582 | addi sp, sp, -(16+32*8+32*4)
2583 | stmw r2, 16+32*8+2*4(sp)
2584 | addi DISPATCH, JGL, -GG_DISP2G-32768
2585 | li CARG2, ~LJ_VMST_EXIT
2586 | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain.
2587 | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH)
2589 | stw CARG1, 0(sp) // Store extended stack chain.
2590 | mcrxr cr0 // Clear SO flag.
2592 | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp.
2594 | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP.
2595 | savex_ 12,13,14,15
2598 | savex_ 16,17,18,19
2599 | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP.
2600 | savex_ 20,21,22,23
2601 | lhz CARG4, 2(CARG3) // Load trace number.
2602 | savex_ 24,25,26,27
2603 | lwz L, DISPATCH_GL(jit_L)(DISPATCH)
2604 | savex_ 28,29,30,31
2605 | sub CARG3, TMP0, CARG3 // Compute exit number.
2606 | lp BASE, DISPATCH_GL(jit_base)(DISPATCH)
2607 | srwi CARG3, CARG3, 2
2608 | stw L, DISPATCH_J(L)(DISPATCH)
2609 | subi CARG3, CARG3, 2
2610 | stw TMP1, DISPATCH_GL(jit_L)(DISPATCH)
2611 | stw CARG4, DISPATCH_J(parent)(DISPATCH)
2613 | addi CARG1, DISPATCH, GG_DISP2J
2614 | stw CARG3, DISPATCH_J(exitno)(DISPATCH)
2615 | addi CARG2, sp, 16
2616 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2617 | // Returns MULTRES (unscaled) or negated error code.
2618 | lp TMP1, L->cframe
2622 | rldicr sp, TMP1, 0, 61
2624 | rlwinm sp, TMP1, 0, 0, 29
2626 | lwz PC, SAVE_PC // Get SAVE_PC.
2628 | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2633 | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set.
2635 | addi DISPATCH, JGL, -GG_DISP2G-32768
2638 | blt >3 // Check for error from exit.
2639 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
2640 | slwi MULTRES, CARG1, 3
2642 | stw MULTRES, SAVE_MULTRES
2643 | lwz TMP1, LFUNC:TMP1->pc
2644 | stw TMP2, DISPATCH_GL(jit_L)(DISPATCH)
2645 | lwz KBASE, PC2PROTO(k)(TMP1)
2646 | // Setup type comparison constants.
2647 | li TISNUM, LJ_TISNUM
2648 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2651 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2654 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2655 | li TISNIL, LJ_TNIL
2656 | stw TMP0, TONUM_HI
2658 | // Modified copy of ins_next which handles function header dispatch, too.
2661 | // Assumes TISNIL == ~LJ_VMST_INTERP == -1.
2662 | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH)
2663 | decode_OPP TMP1, INS
2664 | decode_RA8 RA, INS
2665 | lpx TMP0, DISPATCH, TMP1
2667 | cmplwi TMP1, BC_FUNCF*4 // Function header?
2669 | decode_RB8 RB, INS
2670 | decode_RD8 RD, INS
2671 | decode_RC8 RC, INS
2674 | subi RC, MULTRES, 8
2678 |3: // Rethrow error from the right C frame.
2681 | bl extern lj_err_throw // (lua_State *L, int errcode)
2684 |//-----------------------------------------------------------------------
2685 |//-- Math helper functions ----------------------------------------------
2686 |//-----------------------------------------------------------------------
2688 |// NYI: Use internal implementations of floor, ceil, trunc.
2691 | divwo. TMP0, CARG1, CARG2
2694 | xor CARG3, CARG1, CARG2
2697 | xor. CARG3, CARG1, CARG2
2699 | mullw TMP0, TMP0, CARG2
2700 | sub CARG1, CARG1, TMP0
2702 | cmpwi CARG1, 0; beqlr
2703 | add CARG1, CARG1, CARG2
2709 | mcrxr cr0 // Clear SO for -2147483648 % -1 and return 0.
2712 |//-----------------------------------------------------------------------
2713 |//-- Miscellaneous functions --------------------------------------------
2714 |//-----------------------------------------------------------------------
2716 |// void lj_vm_cachesync(void *start, void *end)
2717 |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size.
2718 |// This is a good lower bound, except for very ancient PPC models.
2721 | // Compute start of first cache line and number of cache lines.
2722 | rlwinm CARG1, CARG1, 0, 0, 26
2723 | sub CARG2, CARG2, CARG1
2724 | addi CARG2, CARG2, 31
2725 | rlwinm. CARG2, CARG2, 27, 5, 31
2729 |1: // Flush D-Cache.
2731 | addi CARG1, CARG1, 32
2735 |1: // Invalidate I-Cache.
2737 | addi CARG3, CARG3, 32
2743 |//-----------------------------------------------------------------------
2744 |//-- FFI helper functions -----------------------------------------------
2745 |//-----------------------------------------------------------------------
2747 |// Handler for callback functions. Callback slot number in r11, g in r12.
2750 |.type CTSTATE, CTState, PC
2752 | lwz CTSTATE, GL:r12->ctype_state
2753 | addi DISPATCH, r12, GG_G2DISP
2754 | stw r11, CTSTATE->cb.slot
2755 | stw r3, CTSTATE->cb.gpr[0]
2756 | stfd f1, CTSTATE->cb.fpr[0]
2757 | stw r4, CTSTATE->cb.gpr[1]
2758 | stfd f2, CTSTATE->cb.fpr[1]
2759 | stw r5, CTSTATE->cb.gpr[2]
2760 | stfd f3, CTSTATE->cb.fpr[2]
2761 | stw r6, CTSTATE->cb.gpr[3]
2762 | stfd f4, CTSTATE->cb.fpr[3]
2763 | stw r7, CTSTATE->cb.gpr[4]
2764 | stfd f5, CTSTATE->cb.fpr[4]
2765 | stw r8, CTSTATE->cb.gpr[5]
2766 | stfd f6, CTSTATE->cb.fpr[5]
2767 | stw r9, CTSTATE->cb.gpr[6]
2768 | stfd f7, CTSTATE->cb.fpr[6]
2769 | stw r10, CTSTATE->cb.gpr[7]
2770 | stfd f8, CTSTATE->cb.fpr[7]
2771 | addi TMP0, sp, CFRAME_SPACE+8
2772 | stw TMP0, CTSTATE->cb.stack
2774 | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2776 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2777 | // Returns lua_State *.
2778 | lp BASE, L:CRET1->base
2779 | li TISNUM, LJ_TISNUM // Setup type comparison constants.
2780 | lp RC, L:CRET1->top
2781 | lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float).
2785 | lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double)
2786 | lwz LFUNC:RB, FRAME_FUNC(BASE)
2787 | ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float).
2788 | stw TMP0, TONUM_HI
2789 | li TISNIL, LJ_TNIL
2799 |->cont_ffi_callback: // Return from FFI callback.
2801 | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH)
2807 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2808 | lwz CRET1, CTSTATE->cb.gpr[0]
2809 | lfd FARG1, CTSTATE->cb.fpr[0]
2810 | lwz CRET2, CTSTATE->cb.gpr[1]
2814 |->vm_ffi_call: // Call C function via FFI.
2815 | // Caveat: needs special frame unwinding, see below.
2817 | .type CCSTATE, CCallState, CARG1
2818 | lwz TMP1, CCSTATE->spadj
2820 | lbz CARG2, CCSTATE->nsp
2821 | lbz CARG3, CCSTATE->nfpr
2824 | cmpwi cr1, CARG3, 0
2826 | addic. CARG2, CARG2, -1
2827 | stwux sp, sp, TMP1
2828 | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls.
2830 | stw CCSTATE, -8(TMP2)
2832 | la TMP1, CCSTATE->stack
2833 | slwi CARG2, CARG2, 2
2837 | lwzx TMP0, TMP1, CARG2
2838 | stwx TMP0, TMP2, CARG2
2839 | addic. CARG2, CARG2, -4
2843 | lfd f1, CCSTATE->fpr[0]
2844 | lfd f2, CCSTATE->fpr[1]
2845 | lfd f3, CCSTATE->fpr[2]
2846 | lfd f4, CCSTATE->fpr[3]
2847 | lfd f5, CCSTATE->fpr[4]
2848 | lfd f6, CCSTATE->fpr[5]
2849 | lfd f7, CCSTATE->fpr[6]
2850 | lfd f8, CCSTATE->fpr[7]
2852 | lp TMP0, CCSTATE->func
2853 | lwz CARG2, CCSTATE->gpr[1]
2854 | lwz CARG3, CCSTATE->gpr[2]
2855 | lwz CARG4, CCSTATE->gpr[3]
2856 | lwz CARG5, CCSTATE->gpr[4]
2858 | lwz r8, CCSTATE->gpr[5]
2859 | lwz r9, CCSTATE->gpr[6]
2860 | lwz r10, CCSTATE->gpr[7]
2861 | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1.
2863 | lwz CCSTATE:TMP1, -8(r14)
2866 | stw CARG1, CCSTATE:TMP1->gpr[0]
2867 | stfd FARG1, CCSTATE:TMP1->fpr[0]
2868 | stw CARG2, CCSTATE:TMP1->gpr[1]
2870 | stw CARG3, CCSTATE:TMP1->gpr[2]
2872 | stw CARG4, CCSTATE:TMP1->gpr[3]
2876 |// Note: vm_ffi_call must be the last function in this object file!
2878 |//-----------------------------------------------------------------------
2881 /* Generate the code for a single instruction. */
2882 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2889 /* -- Comparison ops ---------------------------------------------------- */
2891 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2893 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2894 | // RA = src1*8, RD = src2*8, JMP with RD = target
2896 | lwzux TMP0, RA, BASE
2899 | lwzux TMP1, RD, BASE
2901 | checknum cr0, TMP0
2903 | decode_RD4 TMP2, TMP2
2904 | checknum cr1, TMP1
2905 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2909 if (op == BC_ISLT) {
2911 } else if (op == BC_ISGE) {
2913 } else if (op == BC_ISLE) {
2923 |7: // RA is not an integer.
2924 | bgt cr0, ->vmeta_comp
2925 | // RA is a number.
2927 | bgt cr1, ->vmeta_comp
2929 | // RA is a number, RD is an integer.
2933 |8: // RA is an integer, RD is not an integer.
2934 | bgt cr1, ->vmeta_comp
2935 | // RA is an integer, RD is a number.
2941 if (op == BC_ISLT) {
2943 } else if (op == BC_ISGE) {
2945 } else if (op == BC_ISLE) {
2946 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2949 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2954 | lwzx TMP0, BASE, RA
2957 | lwzx TMP1, BASE, RD
2958 | checknum cr0, TMP0
2961 | checknum cr1, TMP1
2962 | decode_RD4 TMP2, TMP2
2963 | bge cr0, ->vmeta_comp
2964 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
2965 | bge cr1, ->vmeta_comp
2967 if (op == BC_ISLT) {
2969 } else if (op == BC_ISGE) {
2971 } else if (op == BC_ISLE) {
2972 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2975 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq
2984 case BC_ISEQV: case BC_ISNEV:
2985 vk = op == BC_ISEQV;
2986 | // RA = src1*8, RD = src2*8, JMP with RD = target
2988 | lwzux TMP0, RA, BASE
2991 | lwzux TMP1, RD, BASE
2992 | checknum cr0, TMP0
2994 | checknum cr1, TMP1
2995 | decode_RD4 TMP2, TMP2
2997 | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt
2998 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3000 | ble cr7, ->BC_ISEQN_Z
3002 | ble cr7, ->BC_ISNEN_Z
3005 | lwzux TMP0, RA, BASE
3009 | lwzux TMP1, RD, BASE
3010 | checknum cr0, TMP0
3011 | decode_RD4 TMP2, TMP2
3013 | checknum cr1, TMP1
3014 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3028 |5: // Either or both types are not numbers.
3034 | cmpwi cr7, TMP0, LJ_TCDATA
3035 | cmpwi cr5, TMP1, LJ_TCDATA
3039 | cmplwi cr1, TMP3, ~LJ_TISPRI // Primitive?
3041 | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq
3043 | cmplwi cr6, TMP3, ~LJ_TISTABUD // Table or userdata?
3045 | beq cr7, ->vmeta_equal_cd
3047 | cmplw cr5, CARG2, CARG3
3048 | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive.
3049 | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type.
3050 | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv.
3052 | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2.
3053 | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2.
3064 | bge cr0, >2 // Done if 1 or 2.
3069 | blt cr0, <1 // Done if 1 or 2.
3071 | blt cr6, <1 // Done if not tab/ud.
3073 | // Different tables or userdatas. Need to check __eq metamethod.
3074 | // Field metatable must be at same offset for GCtab and GCudata!
3075 | lwz TAB:TMP2, TAB:CARG2->metatable
3076 | li CARG4, 1-vk // ne = 0 or 1.
3077 | cmplwi TAB:TMP2, 0
3078 | beq <1 // No metatable?
3079 | lbz TMP2, TAB:TMP2->nomm
3080 | andix. TMP2, TMP2, 1<<MM_eq
3081 | bne <1 // Or 'no __eq' flag set?
3082 | mr PC, SAVE0 // Restore old PC.
3083 | b ->vmeta_equal // Handle __eq metamethod.
3086 case BC_ISEQS: case BC_ISNES:
3087 vk = op == BC_ISEQS;
3088 | // RA = src*8, RD = str_const*8 (~), JMP with RD = target
3089 | lwzux TMP0, RA, BASE
3091 | lwz STR:TMP3, 4(RA)
3096 | cmpwi TMP0, LJ_TCDATA
3098 | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4
3099 | .gpr64 extsw TMP0, TMP0
3100 | subfic TMP0, TMP0, LJ_TSTR
3102 | beq ->vmeta_equal_cd
3104 | sub TMP1, STR:TMP1, STR:TMP3
3105 | or TMP0, TMP0, TMP1
3106 | decode_RD4 TMP2, TMP2
3107 | subfic TMP0, TMP0, 0
3108 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3109 | subfe TMP1, TMP1, TMP1
3111 | andc TMP2, TMP2, TMP1
3113 | and TMP2, TMP2, TMP1
3119 case BC_ISEQN: case BC_ISNEN:
3120 vk = op == BC_ISEQN;
3121 | // RA = src*8, RD = num_const*8, JMP with RD = target
3123 | lwzux TMP0, RA, BASE
3126 | lwzux TMP1, RD, KBASE
3127 | checknum cr0, TMP0
3129 | checknum cr1, TMP1
3130 | decode_RD4 TMP2, TMP2
3132 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3144 |->BC_ISEQN_Z: // Dummy label.
3146 |->BC_ISNEN_Z: // Dummy label.
3148 | lwzx TMP0, BASE, RA
3152 | lfdx f1, KBASE, RD
3153 | decode_RD4 TMP2, TMP2
3155 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3178 | cmpwi TMP0, LJ_TCDATA
3179 | beq ->vmeta_equal_cd
3183 |7: // RA is not an integer.
3185 | // RA is a number.
3188 | // RA is a number, RD is an integer.
3192 |8: // RA is an integer, RD is a number.
3202 case BC_ISEQP: case BC_ISNEP:
3203 vk = op == BC_ISEQP;
3204 | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target
3205 | lwzx TMP0, BASE, RA
3211 | cmpwi TMP0, LJ_TCDATA
3213 | sub TMP0, TMP0, TMP1
3215 | beq ->vmeta_equal_cd
3217 | decode_RD4 TMP2, TMP2
3218 | .gpr64 extsw TMP0, TMP0
3219 | addic TMP0, TMP0, -1
3220 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3221 | subfe TMP1, TMP1, TMP1
3223 | and TMP2, TMP2, TMP1
3225 | andc TMP2, TMP2, TMP1
3231 /* -- Unary test and copy ops ------------------------------------------- */
3233 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3234 | // RA = dst*8 or unused, RD = src*8, JMP with RD = target
3235 | lwzx TMP0, BASE, RD
3238 if (op == BC_IST || op == BC_ISF) {
3239 | .gpr64 extsw TMP0, TMP0
3240 | subfic TMP0, TMP0, LJ_TTRUE
3241 | decode_RD4 TMP2, INS
3242 | subfe TMP1, TMP1, TMP1
3243 | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16)
3245 | andc TMP2, TMP2, TMP1
3247 | and TMP2, TMP2, TMP1
3251 | li TMP1, LJ_TFALSE
3254 if (op == BC_ISTC) {
3259 | addis PC, PC, -(BCBIAS_J*4 >> 16)
3260 | decode_RD4 TMP2, INS
3261 | stfdx f0, BASE, RA
3268 /* -- Unary ops --------------------------------------------------------- */
3271 | // RA = dst*8, RD = src*8
3274 | stfdx f0, BASE, RA
3278 | // RA = dst*8, RD = src*8
3280 | lwzx TMP0, BASE, RD
3281 | .gpr64 extsw TMP0, TMP0
3282 | subfic TMP1, TMP0, LJ_TTRUE
3283 | adde TMP0, TMP0, TMP1
3284 | stwx TMP0, BASE, RA
3288 | // RA = dst*8, RD = src*8
3289 | lwzux TMP1, RD, BASE
3305 | stwux TISNUM, RA, BASE
3311 | // Potential overflow.
3312 | mcrxr cr0; bley <1 // Ignore unrelated overflow.
3314 | lus TMP1, 0x41e0 // 2^31.
3320 | xoris TMP1, TMP1, 0x8000
3323 | stwux TMP1, RA, BASE
3332 | // RA = dst*8, RD = src*8
3333 | lwzux TMP0, RD, BASE
3335 | checkstr TMP0; bne >2
3336 | lwz CRET1, STR:CARG1->len
3340 | stwux TISNUM, RA, BASE
3343 | tonum_u f0, CRET1 // Result is a non-negative integer.
3345 | stfdx f0, BASE, RA
3349 | checktab TMP0; bne ->vmeta_len
3351 | lwz TAB:TMP2, TAB:CARG1->metatable
3352 | cmplwi TAB:TMP2, 0
3357 | bl extern lj_tab_len // (GCtab *t)
3358 | // Returns uint32_t (but less than 2^31).
3362 | lbz TMP0, TAB:TMP2->nomm
3363 | andix. TMP0, TMP0, 1<<MM_len
3364 | bne <3 // 'no __len' flag set: done.
3369 /* -- Binary ops -------------------------------------------------------- */
3371 |.macro ins_arithpre
3372 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3373 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3376 | lwzx TMP1, BASE, RB
3378 | lwzx TMP2, KBASE, RC
3380 | lfdx f14, BASE, RB
3381 | lfdx f15, KBASE, RC
3383 | checknum cr0, TMP1
3384 | checknum cr1, TMP2
3385 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3386 | bge ->vmeta_arith_vn
3388 | checknum TMP1; bge ->vmeta_arith_vn
3392 | lwzx TMP1, BASE, RB
3394 | lwzx TMP2, KBASE, RC
3396 | lfdx f15, BASE, RB
3397 | lfdx f14, KBASE, RC
3399 | checknum cr0, TMP1
3400 | checknum cr1, TMP2
3401 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3402 | bge ->vmeta_arith_nv
3404 | checknum TMP1; bge ->vmeta_arith_nv
3408 | lwzx TMP1, BASE, RB
3409 | lwzx TMP2, BASE, RC
3410 | lfdx f14, BASE, RB
3411 | lfdx f15, BASE, RC
3412 | checknum cr0, TMP1
3413 | checknum cr1, TMP2
3414 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3415 | bge ->vmeta_arith_vv
3420 |.macro ins_arithfallback, ins
3423 | ins ->vmeta_arith_vn2
3426 | ins ->vmeta_arith_nv2
3429 | ins ->vmeta_arith_vv2
3434 |.macro intmod, a, b, c
3438 |.macro fpmod, a, b, c
3441 | // NYI: Use internal implementation of floor.
3442 | blex floor // floor(b/c)
3444 | fsub a, b, a // b - floor(b/c)*c
3447 |.macro ins_arithfp, fpins
3449 |.if "fpins" == "fpmod_"
3450 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3452 | fpins f0, f14, f15
3454 | stfdx f0, BASE, RA
3459 |.macro ins_arithdn, intins, fpins
3460 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3461 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3464 | lwzux TMP1, RB, BASE
3465 | lwzux TMP2, RC, KBASE
3467 | checknum cr0, TMP1
3471 | lwzux TMP1, RB, BASE
3472 | lwzux TMP2, RC, KBASE
3474 | checknum cr0, TMP1
3478 | lwzux TMP1, RB, BASE
3479 | lwzux TMP2, RC, BASE
3481 | checknum cr0, TMP1
3485 | checknum cr1, TMP2
3488 | intins CARG1, CARG1, CARG2
3492 | stwux TISNUM, RA, BASE
3497 | mcrxr cr0; bley <1 // Ignore unrelated overflow.
3498 | ins_arithfallback b
3502 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3506 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3509 | ins_arithfallback bge
3510 |.if "fpins" == "fpmod_"
3511 | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3513 | fpins f0, f14, f15
3515 | stfdx f0, BASE, RA
3520 |.macro ins_arith, intins, fpins
3522 | ins_arithdn intins, fpins
3528 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3530 |.macro addo32., y, a, b
3531 | // Need to check overflow for (a<<32) + (b<<32).
3532 | rldicr TMP0, a, 32, 31
3533 | rldicr TMP3, b, 32, 31
3534 | addo. TMP0, TMP0, TMP3
3537 | ins_arith addo32., fadd
3539 | ins_arith addo., fadd
3542 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3544 |.macro subo32., y, a, b
3545 | // Need to check overflow for (a<<32) - (b<<32).
3546 | rldicr TMP0, a, 32, 31
3547 | rldicr TMP3, b, 32, 31
3548 | subo. TMP0, TMP0, TMP3
3551 | ins_arith subo32., fsub
3553 | ins_arith subo., fsub
3556 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3557 | ins_arith mullwo., fmul
3559 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3563 | ins_arith intmod, fpmod
3565 case BC_MODNV: case BC_MODVV:
3566 | ins_arith intmod, fpmod_
3569 | // NYI: (partial) integer arithmetic.
3570 | lwzx TMP1, BASE, RB
3571 | lfdx FARG1, BASE, RB
3572 | lwzx TMP2, BASE, RC
3573 | lfdx FARG2, BASE, RC
3574 | checknum cr0, TMP1
3575 | checknum cr1, TMP2
3576 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
3577 | bge ->vmeta_arith_vv
3580 | stfdx FARG1, BASE, RA
3585 | // RA = dst*8, RB = src_start*8, RC = src_end*8
3588 | add CARG2, BASE, RC
3593 | srwi CARG3, CARG3, 3
3594 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3595 | // Returns NULL (finished) or TValue * (metamethod).
3600 | lfdx f0, BASE, SAVE0 // Copy result from RB to RA.
3601 | stfdx f0, BASE, RA
3605 /* -- Constant ops ------------------------------------------------------ */
3608 | // RA = dst*8, RD = str_const*8 (~)
3610 | subfic TMP1, TMP1, -4
3612 | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4
3614 | stwux TMP2, RA, BASE
3620 | // RA = dst*8, RD = cdata_const*8 (~)
3622 | subfic TMP1, TMP1, -4
3624 | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4
3625 | li TMP2, LJ_TCDATA
3626 | stwux TMP2, RA, BASE
3632 | // RA = dst*8, RD = int16_literal*8
3637 | stwux TISNUM, RA, BASE
3641 | // The soft-float approach is faster.
3643 | srawi TMP1, RD, 31
3644 | xor TMP2, TMP1, RD
3645 | sub TMP2, TMP2, TMP1 // TMP2 = abs(x)
3647 | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1
3648 | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa
3649 | subfic TMP3, RD, 0
3650 | slwi TMP1, TMP1, 20
3651 | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11)
3652 | subfe TMP0, TMP0, TMP0
3653 | add RD, RD, TMP1 // hi = hi + exponent-1
3654 | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi
3656 | stwux RD, RA, BASE
3662 | // RA = dst*8, RD = num_const*8
3664 | lfdx f0, KBASE, RD
3665 | stfdx f0, BASE, RA
3669 | // RA = dst*8, RD = primitive_type*8 (~)
3673 | stwx TMP0, BASE, RA
3677 | // RA = base*8, RD = end*8
3678 | stwx TISNIL, BASE, RA
3681 | stwx TISNIL, BASE, RA
3688 /* -- Upvalue and function ops ------------------------------------------ */
3691 | // RA = dst*8, RD = uvnum*8
3692 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3694 | addi RD, RD, offsetof(GCfuncL, uvptr)
3695 | lwzx UPVAL:RB, LFUNC:RB, RD
3697 | lwz TMP1, UPVAL:RB->v
3699 | stfdx f0, BASE, RA
3703 | // RA = uvnum*8, RD = src*8
3704 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3706 | addi RA, RA, offsetof(GCfuncL, uvptr)
3707 | lfdux f0, RD, BASE
3708 | lwzx UPVAL:RB, LFUNC:RB, RA
3709 | lbz TMP3, UPVAL:RB->marked
3710 | lwz CARG2, UPVAL:RB->v
3711 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3712 | lbz TMP0, UPVAL:RB->closed
3715 | cmplwi cr1, TMP0, 0
3717 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3718 | subi TMP2, TMP2, (LJ_TISNUM+1)
3719 | bne >2 // Upvalue is closed and black?
3723 |2: // Check if new value is collectable.
3724 | cmplwi TMP2, LJ_TISGCV - (LJ_TISNUM+1)
3725 | bge <1 // tvisgcv(v)
3726 | lbz TMP3, GCOBJ:TMP1->gch.marked
3727 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v)
3728 | la CARG1, GG_DISP2G(DISPATCH)
3729 | // Crossed a write barrier. Move the barrier forward.
3731 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3735 | // RA = uvnum*8, RD = str_const*8 (~)
3736 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3739 | subfic TMP1, TMP1, -4
3740 | addi RA, RA, offsetof(GCfuncL, uvptr)
3741 | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4
3742 | lwzx UPVAL:RB, LFUNC:RB, RA
3743 | lbz TMP3, UPVAL:RB->marked
3744 | lwz CARG2, UPVAL:RB->v
3745 | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv)
3746 | lbz TMP3, STR:TMP1->marked
3747 | lbz TMP2, UPVAL:RB->closed
3749 | stw STR:TMP1, 4(CARG2)
3750 | stw TMP0, 0(CARG2)
3755 |2: // Check if string is white and ensure upvalue is closed.
3756 | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str)
3757 | cmplwi cr1, TMP2, 0
3758 | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
3759 | la CARG1, GG_DISP2G(DISPATCH)
3760 | // Crossed a write barrier. Move the barrier forward.
3762 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3766 | // RA = uvnum*8, RD = num_const*8
3767 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3769 | addi RA, RA, offsetof(GCfuncL, uvptr)
3770 | lfdx f0, KBASE, RD
3771 | lwzx UPVAL:RB, LFUNC:RB, RA
3773 | lwz TMP1, UPVAL:RB->v
3778 | // RA = uvnum*8, RD = primitive_type*8 (~)
3779 | lwz LFUNC:RB, FRAME_FUNC(BASE)
3782 | addi RA, RA, offsetof(GCfuncL, uvptr)
3784 | lwzx UPVAL:RB, LFUNC:RB, RA
3786 | lwz TMP1, UPVAL:RB->v
3792 | // RA = level*8, RD = target
3793 | lwz TMP1, L->openupval
3794 | branch_RD // Do this first since RD is not saved.
3799 | add CARG2, BASE, RA
3800 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3807 | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype)
3810 | subfic TMP1, TMP1, -4
3812 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3814 | lwz CARG3, FRAME_FUNC(BASE)
3815 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3816 | bl extern lj_func_newL_gc
3817 | // Returns GCfuncL *.
3820 | stwux TMP0, RA, BASE
3821 | stw LFUNC:CRET1, 4(RA)
3825 /* -- Table ops --------------------------------------------------------- */
3829 | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~)
3830 | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
3832 | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
3838 if (op == BC_TNEW) {
3839 | rlwinm CARG2, RD, 29, 21, 31
3840 | rlwinm CARG3, RD, 18, 27, 31
3841 | cmpwi CARG2, 0x7ff; beq >3
3843 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3844 | // Returns Table *.
3847 | subfic TMP1, TMP1, -4
3848 | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4
3849 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3850 | // Returns Table *.
3854 | stwux TMP0, RA, BASE
3855 | stw TAB:CRET1, 4(RA)
3857 if (op == BC_TNEW) {
3864 | bl extern lj_gc_step_fixtop // (lua_State *L)
3871 | // RA = dst*8, RD = str_const*8 (~)
3873 | // RA = src*8, RD = str_const*8 (~)
3874 | lwz LFUNC:TMP2, FRAME_FUNC(BASE)
3876 | lwz TAB:RB, LFUNC:TMP2->env
3877 | subfic TMP1, TMP1, -4
3878 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3879 if (op == BC_GGET) {
3887 | // RA = dst*8, RB = table*8, RC = key*8
3888 | lwzux CARG1, RB, BASE
3889 | lwzux CARG2, RC, BASE
3897 | checknum cr1, CARG2
3900 | lwz TMP0, TAB:RB->asize
3902 | lwz TMP1, TAB:RB->array
3907 | // Convert number key to integer, check for integerness and range.
3909 | fadd f2, f0, TOBIT
3911 | lwz TMP0, TAB:RB->asize
3912 | fsub f2, f2, TOBIT
3914 | lwz TMP1, TAB:RB->array
3916 | cmplw cr0, TMP0, TMP2
3917 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
3918 | slwi TMP2, TMP2, 3
3920 | ble ->vmeta_tgetv // Integer key and in array part?
3921 | lwzx TMP0, TMP1, TMP2
3922 | lfdx f14, TMP1, TMP2
3923 | checknil TMP0; beq >2
3926 | stfdx f14, BASE, RA
3929 |2: // Check for __index if table value is nil.
3930 | lwz TAB:TMP2, TAB:RB->metatable
3931 | cmplwi TAB:TMP2, 0
3932 | beq <1 // No metatable: done.
3933 | lbz TMP0, TAB:TMP2->nomm
3934 | andix. TMP0, TMP0, 1<<MM_index
3935 | bne <1 // 'no __index' flag set: done.
3939 | checkstr CARG2; bne ->vmeta_tgetv
3943 | b ->BC_TGETS_Z // String key?
3946 | // RA = dst*8, RB = table*8, RC = str_const*8 (~)
3947 | lwzux CARG1, RB, BASE
3950 | subfic TMP1, TMP1, -4
3952 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
3953 | bne ->vmeta_tgets1
3955 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8
3956 | lwz TMP0, TAB:RB->hmask
3957 | lwz TMP1, STR:RC->hash
3958 | lwz NODE:TMP2, TAB:RB->node
3959 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
3960 | slwi TMP0, TMP1, 5
3961 | slwi TMP1, TMP1, 3
3962 | sub TMP1, TMP0, TMP1
3963 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
3965 | lwz CARG1, NODE:TMP2->key
3966 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
3967 | lwz CARG2, NODE:TMP2->val
3968 | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2)
3969 | checkstr CARG1; bne >4
3970 | cmpw TMP0, STR:RC; bne >4
3971 | checknil CARG2; beq >5 // Key found, but nil value?
3973 | stwux CARG2, RA, BASE
3977 |4: // Follow hash chain.
3978 | lwz NODE:TMP2, NODE:TMP2->next
3979 | cmplwi NODE:TMP2, 0
3981 | // End of hash chain: key not found, nil result.
3984 |5: // Check for __index if table value is nil.
3985 | lwz TAB:TMP2, TAB:RB->metatable
3986 | cmplwi TAB:TMP2, 0
3987 | beq <3 // No metatable: done.
3988 | lbz TMP0, TAB:TMP2->nomm
3989 | andix. TMP0, TMP0, 1<<MM_index
3990 | bne <3 // 'no __index' flag set: done.
3994 | // RA = dst*8, RB = table*8, RC = index*8
3995 | lwzux CARG1, RB, BASE
3998 | checktab CARG1; bne ->vmeta_tgetb
3999 | lwz TMP1, TAB:RB->asize
4000 | lwz TMP2, TAB:RB->array
4001 | cmplw TMP0, TMP1; bge ->vmeta_tgetb
4002 | lwzx TMP1, TMP2, RC
4004 | checknil TMP1; beq >5
4007 | stfdx f0, BASE, RA
4010 |5: // Check for __index if table value is nil.
4011 | lwz TAB:TMP2, TAB:RB->metatable
4012 | cmplwi TAB:TMP2, 0
4013 | beq <1 // No metatable: done.
4014 | lbz TMP2, TAB:TMP2->nomm
4015 | andix. TMP2, TMP2, 1<<MM_index
4016 | bne <1 // 'no __index' flag set: done.
4017 | b ->vmeta_tgetb // Caveat: preserve TMP0!
4021 | // RA = src*8, RB = table*8, RC = key*8
4022 | lwzux CARG1, RB, BASE
4023 | lwzux CARG2, RC, BASE
4031 | checknum cr1, CARG2
4034 | lwz TMP0, TAB:RB->asize
4036 | lwz TMP1, TAB:RB->array
4041 | // Convert number key to integer, check for integerness and range.
4043 | fadd f2, f0, TOBIT
4045 | lwz TMP0, TAB:RB->asize
4046 | fsub f2, f2, TOBIT
4048 | lwz TMP1, TAB:RB->array
4050 | cmplw cr0, TMP0, TMP2
4051 | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq
4052 | slwi TMP0, TMP2, 3
4054 | ble ->vmeta_tsetv // Integer key and in array part?
4055 | lwzx TMP2, TMP1, TMP0
4056 | lbz TMP3, TAB:RB->marked
4057 | lfdx f14, BASE, RA
4058 | checknil TMP2; beq >3
4060 | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table)
4061 | stfdx f14, TMP1, TMP0
4066 |3: // Check for __newindex if previous value is nil.
4067 | lwz TAB:TMP2, TAB:RB->metatable
4068 | cmplwi TAB:TMP2, 0
4069 | beq <1 // No metatable: done.
4070 | lbz TMP2, TAB:TMP2->nomm
4071 | andix. TMP2, TMP2, 1<<MM_newindex
4072 | bne <1 // 'no __newindex' flag set: done.
4076 | checkstr CARG2; bne ->vmeta_tsetv
4080 | b ->BC_TSETS_Z // String key?
4082 |7: // Possible table write barrier for the value. Skip valiswhite check.
4083 | barrierback TAB:RB, TMP3, TMP0
4087 | // RA = src*8, RB = table*8, RC = str_const*8 (~)
4088 | lwzux CARG1, RB, BASE
4091 | subfic TMP1, TMP1, -4
4093 | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4
4094 | bne ->vmeta_tsets1
4096 | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8
4097 | lwz TMP0, TAB:RB->hmask
4098 | lwz TMP1, STR:RC->hash
4099 | lwz NODE:TMP2, TAB:RB->node
4100 | stb ZERO, TAB:RB->nomm // Clear metamethod cache.
4101 | and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
4102 | lfdx f14, BASE, RA
4103 | slwi TMP0, TMP1, 5
4104 | slwi TMP1, TMP1, 3
4105 | sub TMP1, TMP0, TMP1
4106 | lbz TMP3, TAB:RB->marked
4107 | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
4109 | lwz CARG1, NODE:TMP2->key
4110 | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2)
4111 | lwz CARG2, NODE:TMP2->val
4112 | lwz NODE:TMP1, NODE:TMP2->next
4113 | checkstr CARG1; bne >5
4114 | cmpw TMP0, STR:RC; bne >5
4115 | checknil CARG2; beq >4 // Key found, but nil value?
4117 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4118 | stfd f14, NODE:TMP2->val
4123 |4: // Check for __newindex if previous value is nil.
4124 | lwz TAB:TMP1, TAB:RB->metatable
4125 | cmplwi TAB:TMP1, 0
4126 | beq <2 // No metatable: done.
4127 | lbz TMP0, TAB:TMP1->nomm
4128 | andix. TMP0, TMP0, 1<<MM_newindex
4129 | bne <2 // 'no __newindex' flag set: done.
4132 |5: // Follow hash chain.
4133 | cmplwi NODE:TMP1, 0
4134 | mr NODE:TMP2, NODE:TMP1
4136 | // End of hash chain: key not found, add a new one.
4138 | // But check for __newindex first.
4139 | lwz TAB:TMP1, TAB:RB->metatable
4140 | la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
4143 | cmplwi TAB:TMP1, 0
4145 | beq >6 // No metatable: continue.
4146 | lbz TMP0, TAB:TMP1->nomm
4147 | andix. TMP0, TMP0, 1<<MM_newindex
4148 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4151 | stw STR:RC, 4(CARG3)
4153 | stw TMP0, 0(CARG3)
4154 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4155 | // Returns TValue *.
4157 | stfd f14, 0(CRET1)
4158 | b <3 // No 2nd write barrier needed.
4160 |7: // Possible table write barrier for the value. Skip valiswhite check.
4161 | barrierback TAB:RB, TMP3, TMP0
4165 | // RA = src*8, RB = table*8, RC = index*8
4166 | lwzux CARG1, RB, BASE
4169 | checktab CARG1; bne ->vmeta_tsetb
4170 | lwz TMP1, TAB:RB->asize
4171 | lwz TMP2, TAB:RB->array
4172 | lbz TMP3, TAB:RB->marked
4174 | lfdx f14, BASE, RA
4176 | lwzx TMP1, TMP2, RC
4177 | checknil TMP1; beq >5
4179 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4180 | stfdx f14, TMP2, RC
4185 |5: // Check for __newindex if previous value is nil.
4186 | lwz TAB:TMP1, TAB:RB->metatable
4187 | cmplwi TAB:TMP1, 0
4188 | beq <1 // No metatable: done.
4189 | lbz TMP1, TAB:TMP1->nomm
4190 | andix. TMP1, TMP1, 1<<MM_newindex
4191 | bne <1 // 'no __newindex' flag set: done.
4192 | b ->vmeta_tsetb // Caveat: preserve TMP0!
4194 |7: // Possible table write barrier for the value. Skip valiswhite check.
4195 | barrierback TAB:RB, TMP3, TMP0
4200 | // RA = base*8 (table at base-1), RD = num_const*8 (start index)
4203 | add TMP3, KBASE, RD
4204 | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table.
4205 | addic. TMP0, MULTRES, -8
4206 | lwz TMP3, 4(TMP3) // Integer constant is in lo-word.
4207 | srwi CARG3, TMP0, 3
4208 | beq >4 // Nothing to copy?
4209 | add CARG3, CARG3, TMP3
4210 | lwz TMP2, TAB:CARG2->asize
4211 | slwi TMP1, TMP3, 3
4212 | lbz TMP3, TAB:CARG2->marked
4214 | add TMP2, RA, TMP0
4215 | lwz TMP0, TAB:CARG2->array
4217 | add TMP1, TMP1, TMP0
4218 | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table)
4219 |3: // Copy result slots to table.
4222 | cmpw cr1, RA, TMP2
4224 | addi TMP1, TMP1, 8
4230 |5: // Need to resize array part.
4235 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4236 | // Must not reallocate the stack.
4240 |7: // Possible table write barrier for any value. Skip valiswhite check.
4241 | barrierback TAB:CARG2, TMP3, TMP0
4245 /* -- Calls and vararg handling ----------------------------------------- */
4248 | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8
4249 | add NARGS8:RC, NARGS8:RC, MULTRES
4250 | // Fall through. Assumes BC_CALL follows.
4253 | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8
4255 | lwzux TMP0, BASE, RA
4256 | lwz LFUNC:RB, 4(BASE)
4257 | subi NARGS8:RC, NARGS8:RC, 8
4258 | addi BASE, BASE, 8
4259 | checkfunc TMP0; bne ->vmeta_call
4264 | // RA = base*8, (RB = 0,) RC = extra_nargs*8
4265 | add NARGS8:RC, NARGS8:RC, MULTRES
4266 | // Fall through. Assumes BC_CALLT follows.
4269 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
4270 | lwzux TMP0, RA, BASE
4271 | lwz LFUNC:RB, 4(RA)
4272 | subi NARGS8:RC, NARGS8:RC, 8
4273 | lwz TMP1, FRAME_PC(BASE)
4278 | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand.
4279 | lbz TMP3, LFUNC:RB->ffid
4280 | xori TMP2, TMP1, FRAME_VARG
4281 | cmplwi cr1, NARGS8:RC, 0
4284 | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC.
4286 | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function?
4289 | addi TMP3, TMP2, 8
4291 | cmplw cr1, TMP3, NARGS8:RC
4292 | stfdx f0, BASE, TMP2
4296 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt
4301 |5: // Tailcall to a fast function with a Lua frame below.
4303 | decode_RA8 RA, INS
4304 | sub TMP1, BASE, RA
4305 | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1)
4306 | lwz TMP1, LFUNC:TMP1->pc
4307 | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE.
4310 |7: // Tailcall from a vararg function.
4311 | andix. TMP0, TMP2, FRAME_TYPEP
4312 | bne <1 // Vararg frame below?
4313 | sub BASE, BASE, TMP2 // Relocate BASE down.
4314 | lwz TMP1, FRAME_PC(BASE)
4315 | andix. TMP0, TMP1, FRAME_TYPE
4320 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8))
4322 | add BASE, BASE, RA
4323 | lwz TMP1, -24(BASE)
4324 | lwz LFUNC:RB, -20(BASE)
4327 | stw TMP1, 0(BASE) // Copy callable.
4328 | stw LFUNC:RB, 4(BASE)
4330 | stfd f1, 16(BASE) // Copy control var.
4331 | li NARGS8:RC, 16 // Iterators get 2 arguments.
4332 | stfdu f0, 8(BASE) // Copy state.
4338 | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8)
4340 | // NYI: add hotloop, record BC_ITERN.
4343 | lwz TAB:RB, -12(RA)
4344 | lwz RC, -4(RA) // Get index from control var.
4345 | lwz TMP0, TAB:RB->asize
4346 | lwz TMP1, TAB:RB->array
4348 |1: // Traverse array part.
4351 | bge >5 // Index points after array part?
4352 | lwzx TMP2, TMP1, TMP3
4353 | lfdx f0, TMP1, TMP3
4364 | addis TMP3, PC, -(BCBIAS_J*4 >> 16)
4366 | decode_RD4 TMP1, INS
4367 | stw RC, -4(RA) // Update control var.
4368 | add PC, TMP1, TMP3
4375 |4: // Skip holes in array part.
4379 |5: // Traverse hash part.
4380 | lwz TMP1, TAB:RB->hmask
4382 | lwz TMP2, TAB:RB->node
4384 | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1.
4388 | sub TMP3, TMP3, RB
4389 | lwzx RB, TMP2, TMP3
4390 | lfdx f0, TMP2, TMP3
4391 | add NODE:TMP3, TMP2, TMP3
4395 | lfd f1, NODE:TMP3->key
4396 | addis TMP2, PC, -(BCBIAS_J*4 >> 16)
4399 | decode_RD4 TMP1, INS
4402 | add PC, TMP1, TMP2
4403 | stw RC, -4(RA) // Update control var.
4406 |7: // Skip holes in hash part.
4412 | // RA = base*8, RD = target (points to ITERN)
4415 | lwz CFUNC:TMP1, -20(RA)
4418 | cmpwi cr0, TMP2, LJ_TTAB
4419 | cmpwi cr1, TMP0, LJ_TFUNC
4420 | cmpwi cr6, TMP3, LJ_TNIL
4422 | lbz TMP1, CFUNC:TMP1->ffid
4423 | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq
4424 | cmpwi cr7, TMP1, FF_next_N
4426 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4427 | add TMP3, PC, TMP0
4430 | ori TMP1, TMP1, 0x7fff
4431 | stw ZERO, -4(RA) // Initialize control var.
4433 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4436 |5: // Despecialize bytecode if any of the checks fail.
4440 | addis PC, TMP3, -(BCBIAS_J*4 >> 16)
4446 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4447 | lwz TMP0, FRAME_PC(BASE)
4450 | addi RC, RC, FRAME_VARG
4452 | subi TMP3, BASE, 8 // TMP3 = vtop
4453 | sub RC, RC, TMP0 // RC = vbase
4454 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4457 | sub TMP1, TMP3, RC
4460 | sub. TMP1, TMP3, RC
4462 | beq cr1, >5 // Copy all varargs?
4463 | subi TMP2, TMP2, 16
4464 | ble >2 // No vararg slots?
4465 |1: // Copy vararg slots to destination slots.
4470 | cmplw cr1, RC, TMP3
4471 | bge >3 // All destination slots filled?
4473 | blt cr1, <1 // More vararg slots?
4474 |2: // Fill up remainder with nil.
4482 |5: // Copy all varargs.
4483 | lwz TMP0, L->maxstack
4484 | li MULTRES, 8 // MULTRES = (0+1)*8
4485 | bley <3 // No vararg slots?
4486 | add TMP2, RA, TMP1
4488 | addi MULTRES, TMP1, 8
4496 | blt <6 // More vararg slots?
4499 |7: // Grow stack for varargs.
4502 | sub SAVE0, RC, BASE // Need delta, because BASE may change.
4506 | srwi CARG2, TMP1, 3
4507 | bl extern lj_state_growstack // (lua_State *L, int n)
4510 | add RC, BASE, SAVE0
4511 | subi TMP3, BASE, 8
4515 /* -- Returns ----------------------------------------------------------- */
4518 | // RA = results*8, RD = extra_nresults*8
4519 | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8.
4520 | // Fall through. Assumes BC_RET follows.
4524 | // RA = results*8, RD = (nresults+1)*8
4525 | lwz PC, FRAME_PC(BASE)
4529 | andix. TMP0, PC, FRAME_TYPE
4530 | xori TMP1, PC, FRAME_VARG
4534 | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return
4537 | subi TMP2, BASE, 8
4539 | decode_RB8 RB, INS
4543 | addi TMP3, TMP1, 8
4546 | stfdx f0, TMP2, TMP1
4548 | addi TMP1, TMP3, 8
4551 | stfdx f1, TMP2, TMP3
4556 | decode_RA8 RA, INS
4558 | sub BASE, TMP2, RA
4559 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4561 | lwz TMP1, LFUNC:TMP1->pc
4562 | lwz KBASE, PC2PROTO(k)(TMP1)
4565 |6: // Fill up results with nil.
4568 | stwx TISNIL, TMP2, TMP1
4571 |->BC_RETV_Z: // Non-standard return case.
4572 | andix. TMP2, TMP1, FRAME_TYPEP
4574 | // Return from vararg function: relocate BASE down.
4575 | sub BASE, BASE, TMP1
4576 | lwz PC, FRAME_PC(BASE)
4580 case BC_RET0: case BC_RET1:
4581 | // RA = results*8, RD = (nresults+1)*8
4582 | lwz PC, FRAME_PC(BASE)
4585 | andix. TMP0, PC, FRAME_TYPE
4586 | xori TMP1, PC, FRAME_VARG
4590 | subi TMP2, BASE, 8
4591 | decode_RB8 RB, INS
4592 if (op == BC_RET1) {
4598 | decode_RA8 RA, INS
4600 | sub BASE, TMP2, RA
4601 | lwz LFUNC:TMP1, FRAME_FUNC(BASE)
4603 | lwz TMP1, LFUNC:TMP1->pc
4604 | lwz KBASE, PC2PROTO(k)(TMP1)
4607 |6: // Fill up results with nil.
4610 | stwx TISNIL, TMP2, TMP1
4614 /* -- Loops and branches ------------------------------------------------ */
4620 | // Fall through. Assumes BC_IFORL follows.
4630 | // RA = base*8, RD = target (after end of loop or start of loop)
4631 vk = (op == BC_IFORL || op == BC_JFORL);
4634 | lwzux TMP1, RA, BASE
4635 | lwz CARG1, FORL_IDX*8+4(RA)
4636 | cmplw cr0, TMP1, TISNUM
4638 | lwz CARG3, FORL_STEP*8+4(RA)
4641 | // Need to check overflow for (a<<32) + (b<<32).
4642 | rldicr TMP0, CARG1, 32, 31
4643 | rldicr TMP2, CARG3, 32, 31
4644 | add CARG1, CARG1, CARG3
4645 | addo. TMP0, TMP0, TMP2
4647 | addo. CARG1, CARG1, CARG3
4649 | cmpwi cr6, CARG3, 0
4650 | lwz CARG2, FORL_STOP*8+4(RA)
4653 | stw CARG1, FORL_IDX*8+4(RA)
4655 | lwz TMP3, FORL_STEP*8(RA)
4656 | lwz CARG3, FORL_STEP*8+4(RA)
4657 | lwz TMP2, FORL_STOP*8(RA)
4658 | lwz CARG2, FORL_STOP*8+4(RA)
4659 | cmplw cr7, TMP3, TISNUM
4660 | cmplw cr1, TMP2, TISNUM
4661 | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq
4662 | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq
4663 | cmpwi cr6, CARG3, 0
4669 | stw TISNUM, FORL_EXT*8(RA)
4670 if (op != BC_JFORL) {
4673 | stw CARG1, FORL_EXT*8+4(RA)
4674 if (op != BC_JFORL) {
4677 if (op == BC_FORI) {
4678 | bgt >3 // See FP loop below.
4679 } else if (op == BC_JFORI) {
4680 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4682 } else if (op == BC_IFORL) {
4684 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4690 |5: // Invert check for negative step.
4694 |6: // Potential overflow.
4695 | mcrxr cr0; bley <4 // Ignore unrelated overflow.
4702 | lfd f1, FORL_IDX*8(RA)
4704 | lfdux f1, RA, BASE
4706 | lfd f3, FORL_STEP*8(RA)
4707 | lfd f2, FORL_STOP*8(RA)
4708 | lwz TMP3, FORL_STEP*8(RA)
4710 | stfd f1, FORL_IDX*8(RA)
4715 | lwzux TMP1, RA, BASE
4716 | lwz TMP3, FORL_STEP*8(RA)
4717 | lwz TMP2, FORL_STOP*8(RA)
4718 | cmplw cr0, TMP1, TISNUM
4719 | cmplw cr7, TMP3, TISNUM
4720 | cmplw cr1, TMP2, TISNUM
4722 | lfd f1, FORL_IDX*8(RA)
4723 | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt
4724 | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt
4725 | lfd f2, FORL_STOP*8(RA)
4728 | cmpwi cr6, TMP3, 0
4729 if (op != BC_JFORL) {
4732 | stfd f1, FORL_EXT*8(RA)
4733 if (op != BC_JFORL) {
4737 if (op == BC_JFORI) {
4738 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4741 if (op == BC_FORI) {
4743 } else if (op == BC_IFORL) {
4750 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4751 } else if (op == BC_JFORI) {
4762 |5: // Negative step.
4763 if (op == BC_FORI) {
4765 |3: // Used by integer loop, too.
4766 | addis PC, RD, -(BCBIAS_J*4 >> 16)
4767 } else if (op == BC_IFORL) {
4769 } else if (op == BC_JFORI) {
4775 if (op == BC_JFORI) {
4778 | decode_RD8 RD, INS
4787 | // Fall through. Assumes BC_IITERL follows.
4795 | // RA = base*8, RD = target
4796 | lwzux TMP1, RA, BASE
4798 | checknil TMP1; beq >1 // Stop if iterator returned nil.
4799 if (op == BC_JITERL) {
4804 | branch_RD // Otherwise save control var + branch.
4813 | // RA = base*8, RD = target (loop extent)
4814 | // Note: RA/RD is only used by trace recorder to determine scope/extent
4815 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4819 | // Fall through. Assumes BC_ILOOP follows.
4823 | // RA = base*8, RD = target (loop extent)
4829 | // RA = base*8 (ignored), RD = traceno*8
4830 | lwz TMP1, DISPATCH_J(trace)(DISPATCH)
4832 | // Traces on PPC don't store the trace number, so use 0.
4833 | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH)
4834 | lwzx TRACE:TMP2, TMP1, RD
4835 | mcrxr cr0 // Clear SO flag.
4836 | lp TMP2, TRACE:TMP2->mcode
4837 | stw BASE, DISPATCH_GL(jit_base)(DISPATCH)
4839 | stw L, DISPATCH_GL(jit_L)(DISPATCH)
4840 | addi JGL, DISPATCH, GG_DISP2G+32768
4846 | // RA = base*8 (only used by trace recorder), RD = target
4851 /* -- Function headers -------------------------------------------------- */
4857 case BC_FUNCV: /* NYI: compiled vararg functions. */
4858 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4866 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4867 | lwz TMP2, L->maxstack
4868 | lbz TMP1, -4+PC2PROTO(numparams)(PC)
4869 | lwz KBASE, -4+PC2PROTO(k)(PC)
4871 | slwi TMP1, TMP1, 3
4872 | bgt ->vm_growstack_l
4873 if (op != BC_JFUNCF) {
4877 | cmplw NARGS8:RC, TMP1 // Check for missing parameters.
4879 if (op == BC_JFUNCF) {
4880 | decode_RD8 RD, INS
4886 |3: // Clear missing parameters.
4887 | stwx TISNIL, BASE, NARGS8:RC
4888 | addi NARGS8:RC, NARGS8:RC, 8
4896 | NYI // NYI: compiled vararg functions
4897 break; /* NYI: compiled vararg functions. */
4900 | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8
4901 | lwz TMP2, L->maxstack
4902 | add TMP1, BASE, RC
4904 | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC.
4905 | addi TMP3, RC, 8+FRAME_VARG
4906 | lwz KBASE, -4+PC2PROTO(k)(PC)
4908 | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG.
4909 | bge ->vm_growstack_l
4910 | lbz TMP2, -4+PC2PROTO(numparams)(PC)
4915 | addi BASE, TMP1, 8
4918 | cmplw RA, RC // Less args than parameters?
4922 | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC).
4925 | addic. TMP2, TMP2, -1
4927 | stw TMP3, 12(TMP1)
4928 | addi TMP1, TMP1, 8
4933 |4: // Clear missing parameters.
4940 | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8
4941 if (op == BC_FUNCC) {
4942 | lp RD, CFUNC:RB->f
4944 | lp RD, DISPATCH_GL(wrapf)(DISPATCH)
4946 | add TMP1, RA, NARGS8:RC
4947 | lwz TMP2, L->maxstack
4948 | .toc lp TMP3, 0(RD)
4949 | add RC, BASE, NARGS8:RC
4959 if (op == BC_FUNCCW) {
4960 | lp CARG2, CFUNC:RB->f
4963 | bgt ->vm_growstack_c // Need to grow stack.
4964 | .toc lp TOCREG, TOC_OFS(RD)
4965 | .tocenv lp ENVREG, ENV_OFS(RD)
4967 | bctrl // (lua_State *L [, lua_CFunction f])
4968 | // Returns nresults.
4970 | .toc ld TOCREG, SAVE_TOC
4974 | lwz PC, FRAME_PC(BASE) // Fetch PC of caller.
4975 | sub RA, TMP1, RD // RA = L->top - nresults*8
4980 /* ---------------------------------------------------------------------- */
4983 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4989 static int build_backend(BuildCtx *ctx)
4993 dasm_growpc(Dst, BC__MAX);
4995 build_subroutines(ctx);
4998 for (op = 0; op < BC__MAX; op++)
4999 build_ins(ctx, (BCOp)op, op);
5004 /* Emit pseudo frame-info for all assembler functions. */
5005 static void emit_asm_debug(BuildCtx *ctx)
5007 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5009 switch (ctx->mode) {
5011 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5014 "\t.long .LECIE0-.LSCIE0\n"
5016 "\t.long 0xffffffff\n"
5022 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5027 "\t.long .LEFDE0-.LASFDE0\n"
5029 "\t.long .Lframe0\n"
5032 "\t.byte 0xe\n\t.uleb128 %d\n"
5033 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5034 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5035 fcofs, CFRAME_SIZE);
5036 for (i = 14; i <= 31; i++)
5038 "\t.byte %d\n\t.uleb128 %d\n"
5039 "\t.byte %d\n\t.uleb128 %d\n",
5040 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5047 "\t.long .LEFDE1-.LASFDE1\n"
5049 "\t.long .Lframe0\n"
5051 "\t.long .lj_vm_ffi_call\n"
5053 "\t.long lj_vm_ffi_call\n"
5056 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5057 "\t.byte 0x8e\n\t.uleb128 2\n"
5058 "\t.byte 0xd\n\t.uleb128 0xe\n"
5060 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5063 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5066 "\t.long .LECIE1-.LSCIE1\n"
5070 "\t.string \"zPR\"\n"
5074 "\t.uleb128 6\n" /* augmentation length */
5075 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5076 "\t.long lj_err_unwind_dwarf-.\n"
5077 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5078 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5083 "\t.long .LEFDE2-.LASFDE2\n"
5085 "\t.long .LASFDE2-.Lframe1\n"
5086 "\t.long .Lbegin-.\n"
5088 "\t.uleb128 0\n" /* augmentation length */
5089 "\t.byte 0xe\n\t.uleb128 %d\n"
5090 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5091 "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n",
5092 fcofs, CFRAME_SIZE);
5093 for (i = 14; i <= 31; i++)
5095 "\t.byte %d\n\t.uleb128 %d\n"
5096 "\t.byte %d\n\t.uleb128 %d\n",
5097 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i));
5104 "\t.long .LECIE2-.LSCIE2\n"
5108 "\t.string \"zR\"\n"
5112 "\t.uleb128 1\n" /* augmentation length */
5113 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5114 "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n"
5119 "\t.long .LEFDE3-.LASFDE3\n"
5121 "\t.long .LASFDE3-.Lframe2\n"
5122 "\t.long lj_vm_ffi_call-.\n"
5124 "\t.uleb128 0\n" /* augmentation length */
5125 "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n"
5126 "\t.byte 0x8e\n\t.uleb128 2\n"
5127 "\t.byte 0xd\n\t.uleb128 0xe\n"
5129 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);