1 |// Low-level VM code for ARM CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
10 |.globalnames globnames
11 |.externnames extnames
13 |// Note: The ragged indentation of the instructions is intentional.
14 |// The starting columns indicate data dependencies.
16 |//-----------------------------------------------------------------------
18 |// Fixed register assignments for the interpreter.
20 |// The following must be C callee-save.
21 |.define MASKR8, r4 // 255*8 constant for fast bytecode decoding.
22 |.define KBASE, r5 // Constants of current Lua function.
23 |.define PC, r6 // Next PC.
24 |.define DISPATCH, r7 // Opcode dispatch table.
25 |.define LREG, r8 // Register holding lua_State (also in SAVE_L).
27 |// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+.
28 |.define BASE, r9 // Base of current Lua stack frame.
30 |// The following temporaries are not saved across C calls, except for RA/RC.
31 |.define RA, r10 // Callee-save.
32 |.define RC, r11 // Callee-save.
34 |.define OP, r12 // Overlaps RB, must not be lr.
37 |// Calling conventions. Also used as temporaries.
42 |.define CARG12, r0 // For 1st soft-fp double.
43 |.define CARG34, r2 // For 2nd soft-fp double.
48 |// Stack layout while in interpreter. Must match with lj_frame.h.
49 |.define SAVE_R4, [sp, #28]
50 |.define CFRAME_SPACE, #28
51 |.define SAVE_ERRF, [sp, #24]
52 |.define SAVE_NRES, [sp, #20]
53 |.define SAVE_CFRAME, [sp, #16]
54 |.define SAVE_L, [sp, #12]
55 |.define SAVE_PC, [sp, #8]
56 |.define SAVE_MULTRES, [sp, #4]
59 |.define TMPDhi, [sp, #4]
66 | push {r5, r6, r7, r8, r9, r10, r11, lr}
68 | sub sp, sp, CFRAME_SPACE+4
71 |.macro restoreregs_ret
73 | add sp, sp, CFRAME_SPACE+4
75 | pop {r5, r6, r7, r8, r9, r10, r11, pc}
79 | push {r4, r5, r6, r7, r8, r9, r10, r11, lr}
80 | sub sp, sp, CFRAME_SPACE
82 |.macro restoreregs_ret
83 | add sp, sp, CFRAME_SPACE
84 | pop {r4, r5, r6, r7, r8, r9, r10, r11, pc}
88 |// Type definitions. Some of these are only used for documentation.
89 |.type L, lua_State, LREG
90 |.type GL, global_State
101 |.type TRACE, GCtrace
104 |//-----------------------------------------------------------------------
106 |// Trap for not-yet-implemented parts.
107 |.macro NYI; ud; .endmacro
109 |//-----------------------------------------------------------------------
111 |// Access to frame relative to BASE.
112 |.define FRAME_FUNC, #-8
113 |.define FRAME_PC, #-4
115 |.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro
116 |.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro
117 |.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro
118 |.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro
119 |.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro
121 |// Instruction fetch.
128 |// Instruction decode+dispatch.
130 | ldr OP, [DISPATCH, OP, lsl #2]
141 |// Instruction footer.
143 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
144 | .define ins_next, ins_NEXT
145 | .define ins_next_, ins_NEXT
146 | .define ins_next1, ins_NEXT1
147 | .define ins_next2, ins_NEXT2
148 | .define ins_next3, ins_NEXT3
150 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
151 | // Affects only certain kinds of benchmarks (and only with -j off).
168 |// Avoid register name substitution for field name.
171 |// Call decode and dispatch.
173 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
174 | ldr PC, LFUNC:CARG3->field_pc
175 | ldrb OP, [PC] // STALL: load PC. early PC.
177 | ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP.
184 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
185 | str PC, [BASE, FRAME_PC]
186 | ins_callt // STALL: locked PC.
189 |//-----------------------------------------------------------------------
191 |// Macros to test operand types.
192 |.macro checktp, reg, tp; cmn reg, #-tp; .endmacro
193 |.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro
194 |.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro
195 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro
196 |.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro
197 |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro
199 |// Assumes DISPATCH is relative to GL.
200 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
201 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
203 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
205 |.macro hotcheck, delta
207 | and CARG1, CARG1, #126
208 | sub CARG1, CARG1, #-GG_DISP2HOT
209 | ldrh CARG2, [DISPATCH, CARG1]
210 | subs CARG2, CARG2, #delta
211 | strh CARG2, [DISPATCH, CARG1]
215 | hotcheck HOTCOUNT_LOOP
220 | hotcheck HOTCOUNT_CALL
224 |// Set current VM state.
225 |.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro
226 |.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro
228 |// Move table write barrier back. Overwrites mark and tmp.
229 |.macro barrierback, tab, mark, tmp
230 | ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
231 | bic mark, mark, #LJ_GC_BLACK // black2gray(tab)
232 | str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
233 | strb mark, tab->marked
234 | str tmp, tab->gclist
243 |//-----------------------------------------------------------------------
246 #error "Only dual-number mode supported for ARM target"
249 /* Generate subroutines used by opcodes and other parts of the VM. */
250 /* The .code_sub section should be last to help static branch prediction. */
251 static void build_subroutines(BuildCtx *ctx)
255 |//-----------------------------------------------------------------------
256 |//-- Return handling ----------------------------------------------------
257 |//-----------------------------------------------------------------------
260 | // See vm_return. Also: RB = previous base.
262 | beq ->cont_dispatch
264 | // Return from pcall or xpcall fast func.
265 | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame.
266 | mvn CARG2, #~LJ_TTRUE
268 | // Prepending may overwrite the pcall frame, so do it at the end.
269 | str CARG2, [RA, FRAME_PC] // Prepend true to results.
273 | adds RC, RC, #8 // RC = (nresults+1)*8.
274 | mov CRET1, #LUA_YIELD
275 | beq ->vm_unwind_c_eh
276 | str RC, SAVE_MULTRES
277 | ands CARG1, PC, #FRAME_TYPE
278 | beq ->BC_RET_Z // Handle regular return to Lua.
281 | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return
282 | // CARG1 = PC & FRAME_TYPE
283 | bic RB, PC, #FRAME_TYPEP
284 | cmp CARG1, #FRAME_C
285 | sub RB, BASE, RB // RB = previous base.
289 | ldr KBASE, SAVE_NRES
290 | mv_vmstate CARG4, C
293 | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8
297 | subs CARG3, CARG3, #8
298 | ldrd CARG12, [RA], #8
299 | strd CARG12, [BASE], #8
302 | cmp KBASE, RC // More/less results wanted?
305 | str BASE, L->top // Store new top.
308 | ldr RC, SAVE_CFRAME // Restore previous C frame.
309 | mov CRET1, #0 // Ok return status for vm_pcall.
316 | blt >7 // Less results wanted?
317 | // More results wanted. Check stack size and fill up results with nil.
318 | ldr CARG3, L->maxstack
319 | mvn CARG2, #~LJ_TNIL
322 | str CARG2, [BASE, #4]
327 |7: // Less results wanted.
328 | sub CARG1, RC, KBASE
329 | cmp KBASE, #0 // LUA_MULTRET+1 case?
330 | subne BASE, BASE, CARG1 // Either keep top or shrink it.
333 |8: // Corner case: need to grow stack for filling up results.
334 | // This can happen if:
335 | // - A C function grows the stack (a lot).
336 | // - The GC shrinks the stack in between.
337 | // - A return back from a lua_call() with (high) nresults adjustment.
338 | str BASE, L->top // Save current top held in BASE (yes).
339 | lsr CARG2, KBASE, #3
341 | bl extern lj_state_growstack // (lua_State *L, int n)
342 | ldr BASE, L->top // Need the (realloced) L->top in BASE.
345 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
346 | // (void *cframe, int errcode)
349 |->vm_unwind_c_eh: // Landing pad for external unwinder.
351 | mv_vmstate CARG4, C
352 | ldr GL:CARG3, L->glref
353 | str CARG4, GL:CARG3->vmstate
356 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
358 | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
360 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
363 | mov RC, #16 // 2 results: false + error message.
364 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
366 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
367 | mvn CARG1, #~LJ_TFALSE
368 | sub RA, BASE, #8 // Results start at BASE-8.
369 | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame.
370 | add DISPATCH, DISPATCH, #GG_G2DISP
371 | mv_vmstate CARG2, INTERP
372 | str CARG1, [BASE, #-4] // Prepend false to error message.
376 |->vm_unwind_ext: // Complete external unwind.
378 | push {r0, r1, r2, lr}
379 | bl extern _Unwind_Complete
381 | bl extern _Unwind_DeleteException
382 | pop {r0, r1, r2, lr}
387 |//-----------------------------------------------------------------------
388 |//-- Grow stack for calls -----------------------------------------------
389 |//-----------------------------------------------------------------------
391 |->vm_growstack_c: // Grow stack for C function.
393 | mov CARG2, #LUA_MINSTACK
396 |->vm_growstack_l: // Grow stack for Lua function.
397 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
402 | add PC, PC, #4 // Must point after first instruction.
406 | // L->base = new base, L->top = top
408 | bl extern lj_state_growstack // (lua_State *L, int n)
411 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
412 | sub NARGS8:RC, RC, BASE
413 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
414 | ins_callt // Just retry the call.
416 |//-----------------------------------------------------------------------
417 |//-- Entry points into the assembler VM ---------------------------------
418 |//-----------------------------------------------------------------------
420 |->vm_resume: // Setup C frame and resume thread.
421 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
424 | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table.
426 | add DISPATCH, DISPATCH, #GG_G2DISP
429 | str CARG3, SAVE_NRES
430 | add CARG2, sp, #CFRAME_RESUME
431 | ldrb CARG1, L->status
432 | str CARG3, SAVE_ERRF
433 | str L, SAVE_PC // Any value outside of bytecode is ok.
434 | str CARG3, SAVE_CFRAME
436 | str CARG2, L->cframe
439 | // Resume after yield (like a return).
440 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
445 | strb CARG3, L->status
446 | sub RC, CARG1, BASE
447 | ldr PC, [BASE, FRAME_PC]
448 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
449 | mv_vmstate CARG2, INTERP
451 | ands CARG1, PC, #FRAME_TYPE
453 | str RC, SAVE_MULTRES
457 |->vm_pcall: // Setup protected C frame and enter VM.
458 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
461 | str CARG4, SAVE_ERRF
464 |->vm_call: // Setup C frame and enter VM.
465 | // (lua_State *L, TValue *base, int nres1)
469 |1: // Entry point for vm_pcall above (PC = ftype).
470 | ldr RC, L:CARG1->cframe
471 | str CARG3, SAVE_NRES
474 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
476 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
477 | str RC, SAVE_CFRAME
478 | add DISPATCH, DISPATCH, #GG_G2DISP
479 | str sp, L->cframe // Add our C frame to cframe chain.
481 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
482 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
483 | ldr RB, L->base // RB = old base (for vmeta_call).
487 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
488 | sub PC, PC, RB // PC = frame delta + frame type
489 | mv_vmstate CARG2, INTERP
490 | sub NARGS8:RC, CARG1, BASE
494 | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
495 | ldrd CARG34, [BASE, FRAME_FUNC]
496 | checkfunc CARG4, ->vmeta_call
498 |->vm_call_dispatch_f:
500 | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC
502 |->vm_cpcall: // Setup protected C frame, call C.
503 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
506 | ldr RA, L:CARG1->stack
508 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
510 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
512 | add DISPATCH, DISPATCH, #GG_G2DISP
513 | sub RA, RA, RB // Compute -savestack(L, L->top).
515 | str RA, SAVE_NRES // Neg. delta means cframe w/o frame.
516 | str RB, SAVE_ERRF // No error function.
517 | str RC, SAVE_CFRAME
518 | str sp, L->cframe // Add our C frame to cframe chain.
519 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
520 | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud)
523 | bne <3 // Else continue with the call.
524 | b ->vm_leave_cp // No base? Just remove C frame.
526 |//-----------------------------------------------------------------------
527 |//-- Metamethod handling ------------------------------------------------
528 |//-----------------------------------------------------------------------
530 |//-- Continuation dispatch ----------------------------------------------
533 | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8
534 | ldr LFUNC:CARG3, [RB, FRAME_FUNC]
535 | ldr CARG1, [BASE, #-16] // Get continuation.
537 | mov BASE, RB // Restore caller BASE.
541 | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC].
544 | str INS, [CARG2, #-4] // Ensure one valid arg.
548 | ldr CARG3, LFUNC:CARG3->field_pc
549 | ldr KBASE, [CARG3, #PC2PROTO(k)]
550 | // BASE = base, RA = resultptr, CARG4 = meta base
555 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
556 | // cont = 0: tailcall from C function.
557 | sub CARG4, CARG4, #16
558 | sub RC, CARG4, BASE
562 |->cont_cat: // RA = resultptr, CARG4 = meta base
564 | sub CARG2, CARG4, #16
569 | add CARG1, BASE, RC
570 | subs CARG1, CARG2, CARG1
571 | strdne CARG34, [CARG2]
574 | strd CARG34, [BASE, RA]
577 |//-- Table indexing metamethods -----------------------------------------
580 | add CARG2, BASE, RB
584 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
585 | mvn CARG4, #~LJ_TTAB
586 | str TAB:RB, [CARG2]
587 | str CARG4, [CARG2, #4]
589 | mvn CARG4, #~LJ_TSTR
595 |->vmeta_tgetb: // RC = index
598 | mvn CARG4, #~LJ_TISNUM
599 | add CARG2, BASE, RB
605 | add CARG2, BASE, RB
606 | add CARG3, BASE, RC
611 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
612 | // Returns TValue * (finished) or NULL (metamethod).
613 | .IOS ldr BASE, L->base
616 | ldrd CARG34, [CRET1]
619 | strd CARG34, [BASE, RA]
622 |3: // Call __index metamethod.
623 | // BASE = base, L->top = new base, stack = cont/func/t/k
624 | rsb CARG1, BASE, #FRAME_CONT
626 | mov NARGS8:RC, #16 // 2 args for func(t, k).
627 | str PC, [BASE, #-12] // [cont|PC]
628 | add PC, CARG1, BASE
629 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
630 | b ->vm_call_dispatch_f
634 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
635 | // Returns cTValue * or NULL.
638 | ldrdne CARG12, [CRET1]
639 | mvneq CARG2, #~LJ_TNIL
642 |//-----------------------------------------------------------------------
645 | add CARG2, BASE, RB
649 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
650 | mvn CARG4, #~LJ_TTAB
651 | str TAB:RB, [CARG2]
652 | str CARG4, [CARG2, #4]
654 | mvn CARG4, #~LJ_TSTR
660 |->vmeta_tsetb: // RC = index
663 | mvn CARG4, #~LJ_TISNUM
664 | add CARG2, BASE, RB
670 | add CARG2, BASE, RB
671 | add CARG3, BASE, RC
676 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
677 | // Returns TValue * (finished) or NULL (metamethod).
678 | .IOS ldr BASE, L->base
680 | ldrd CARG34, [BASE, RA]
683 | // NOBARRIER: lj_meta_tset ensures the table is not black.
684 | strd CARG34, [CRET1]
688 |3: // Call __newindex metamethod.
689 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
690 | rsb CARG1, BASE, #FRAME_CONT
692 | mov NARGS8:RC, #24 // 3 args for func(t, k, v).
693 | strd CARG34, [BASE, #16] // Copy value to third argument.
694 | str PC, [BASE, #-12] // [cont|PC]
695 | add PC, CARG1, BASE
696 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
697 | b ->vm_call_dispatch_f
704 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
705 | // Returns TValue *.
709 |//-- Comparison metamethods ---------------------------------------------
718 | decode_OP CARG4, INS
719 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
720 | // Returns 0/1 or TValue * (metamethod).
722 | .IOS ldr BASE, L->base
728 | add RB, PC, RB, lsl #2
729 | subhs PC, RB, #0x20000
733 |->cont_ra: // RA = resultptr
736 | decode_RA8 CARG3, INS
737 | strd CARG12, [BASE, CARG3]
740 |->cont_condt: // RA = resultptr
741 | ldr CARG2, [RA, #4]
742 | mvn CARG1, #~LJ_TTRUE
743 | cmp CARG1, CARG2 // Branch if result is true.
746 |->cont_condf: // RA = resultptr
747 | ldr CARG2, [RA, #4]
748 | checktp CARG2, LJ_TFALSE // Branch if result is false.
752 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
757 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
758 | // Returns 0/1 or TValue * (metamethod).
768 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
769 | // Returns 0/1 or TValue * (metamethod).
780 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
781 | .IOS ldr BASE, L->base
784 |//-- Arithmetic metamethods ---------------------------------------------
789 | add CARG3, BASE, RB
790 | add CARG4, KBASE, RC
796 | add CARG4, BASE, RB
797 | add CARG3, KBASE, RC
803 | add CARG3, BASE, RC
804 | add CARG4, BASE, RC
810 | add CARG3, BASE, RB
811 | add CARG4, BASE, RC
814 | add CARG2, BASE, RA
819 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
820 | // Returns NULL (finished) or TValue * (metamethod).
821 | .IOS ldr BASE, L->base
825 | // Call metamethod for binary op.
827 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
828 | sub CARG2, CRET1, BASE
829 | str PC, [CRET1, #-12] // [cont|PC]
830 | add PC, CARG2, #FRAME_CONT
832 | mov NARGS8:RC, #16 // 2 args for func(o1, o2).
833 | b ->vm_call_dispatch
836 | add CARG2, BASE, RC
840 | bl extern lj_meta_len // (lua_State *L, TValue *o)
841 | // Returns NULL (retry) or TValue * (metamethod base).
842 | .IOS ldr BASE, L->base
845 | bne ->vmeta_binop // Binop call for compatibility.
846 | ldr TAB:CARG1, [BASE, RC]
849 | b ->vmeta_binop // Binop call for compatibility.
852 |//-- Call metamethod ----------------------------------------------------
854 |->vmeta_call: // Resolve and call __call metamethod.
855 | // RB = old base, BASE = new base, RC = nargs*8
857 | str RB, L->base // This is the callers base!
858 | sub CARG2, BASE, #8
860 | add CARG3, BASE, NARGS8:RC
862 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
864 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
865 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
868 |->vmeta_callt: // Resolve __call for BC_CALLT.
869 | // BASE = old base, RA = new base, RC = nargs*8
874 | add CARG3, RA, NARGS8:RC
875 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
876 | .IOS ldr BASE, L->base
877 | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here.
878 | ldr PC, [BASE, FRAME_PC]
879 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
882 |//-- Argument coercion for 'for' statement ------------------------------
889 | bl extern lj_meta_for // (lua_State *L, TValue *base)
890 | .IOS ldr BASE, L->base
905 |//-----------------------------------------------------------------------
906 |//-- Fast functions -----------------------------------------------------
907 |//-----------------------------------------------------------------------
913 |.macro .ffunc_1, name
915 | ldrd CARG12, [BASE]
920 |.macro .ffunc_2, name
922 | ldrd CARG12, [BASE]
923 | ldrd CARG34, [BASE, #8]
928 |.macro .ffunc_n, name
930 | checktp CARG2, LJ_TISNUM
934 |.macro .ffunc_nn, name
936 | checktp CARG2, LJ_TISNUM
937 | cmnlo CARG4, #-LJ_TISNUM
941 |.macro .ffunc_d, name
943 | ldr CARG2, [BASE, #4]
947 | checktp CARG2, LJ_TISNUM
951 |.macro .ffunc_dd, name
953 | ldr CARG2, [BASE, #4]
954 | ldr CARG4, [BASE, #12]
957 | vldr d1, [BASE, #8]
959 | checktp CARG2, LJ_TISNUM
960 | cmnlo CARG4, #-LJ_TISNUM
964 |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
966 | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
967 | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
972 |//-- Base library: checks -----------------------------------------------
975 | checktp CARG2, LJ_TTRUE
977 | ldr PC, [BASE, FRAME_PC]
978 | strd CARG12, [BASE, #-8]
980 | subs RA, NARGS8:RC, #8
981 | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
982 | beq ->fff_res // Done if exactly 1 argument.
984 | ldrd CARG12, [RB, #8]
986 | strd CARG12, [RB], #8
991 | ldr CARG2, [BASE, #4]
994 | checktp CARG2, LJ_TISNUM
995 | mvnlo CARG2, #~LJ_TISNUM
996 | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
997 | lsl CARG4, CARG4, #3
998 | ldrd CARG12, [CFUNC:CARG3, CARG4]
1001 |//-- Base library: getters and setters ---------------------------------
1003 |.ffunc_1 getmetatable
1004 | checktp CARG2, LJ_TTAB
1005 | cmnne CARG2, #-LJ_TUDATA
1007 |1: // Field metatable must be at same offset for GCtab and GCudata!
1008 | ldr TAB:RB, TAB:CARG1->metatable
1010 | mvn CARG2, #~LJ_TNIL
1011 | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
1014 | ldr CARG3, TAB:RB->hmask
1015 | ldr CARG4, STR:RC->sid
1016 | ldr NODE:INS, TAB:RB->node
1017 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
1018 | add CARG3, CARG3, CARG3, lsl #1
1019 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
1020 |3: // Rearranged logic, because we expect _not_ to find the key.
1021 | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS.
1022 | ldrd CARG12, NODE:INS->val
1023 | ldr NODE:INS, NODE:INS->next
1024 | checktp CARG4, LJ_TSTR
1025 | cmpeq CARG3, STR:RC
1030 | mov CARG1, RB // Use metatable as default result.
1031 | mvn CARG2, #~LJ_TTAB
1034 | checktp CARG2, LJ_TNIL
1039 | checktp CARG2, LJ_TISNUM
1040 | mvnhs CARG2, CARG2
1041 | movlo CARG2, #~LJ_TISNUM
1042 | add CARG4, DISPATCH, CARG2, lsl #2
1043 | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1046 |.ffunc_2 setmetatable
1047 | // Fast path: no mt for table yet and not clearing the mt.
1048 | checktp CARG2, LJ_TTAB
1049 | ldreq TAB:RB, TAB:CARG1->metatable
1050 | checktpeq CARG4, LJ_TTAB
1051 | ldrbeq CARG4, TAB:CARG1->marked
1053 | bne ->fff_fallback
1054 | tst CARG4, #LJ_GC_BLACK // isblack(table)
1055 | str TAB:CARG3, TAB:CARG1->metatable
1057 | barrierback TAB:CARG1, CARG4, CARG3
1061 | ldrd CARG34, [BASE]
1062 | cmp NARGS8:RC, #16
1063 | blo ->fff_fallback
1065 | checktab CARG4, ->fff_fallback
1067 | add CARG3, BASE, #8
1069 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1070 | // Returns cTValue *.
1072 | ldrd CARG12, [CRET1]
1075 |//-- Base library: conversions ------------------------------------------
1078 | // Only handles the number case inline (without a base argument).
1079 | ldrd CARG12, [BASE]
1081 | bne ->fff_fallback
1082 | checktp CARG2, LJ_TISNUM
1087 | // Only handles the string or number case inline.
1088 | checktp CARG2, LJ_TSTR
1089 | // A __tostring method in the string base metatable is ignored.
1091 | // Handle numbers inline, unless a number base metatable is present.
1092 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])]
1094 | checktp CARG2, LJ_TISNUM
1096 | str PC, SAVE_PC // Redundant (but a defined value).
1097 | bhi ->fff_fallback
1101 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1102 | // Returns GCstr *.
1104 | mvn CARG2, #~LJ_TSTR
1107 |//-- Base library: iterators -------------------------------------------
1110 | mvn CARG4, #~LJ_TNIL
1111 | checktab CARG2, ->fff_fallback
1112 | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil.
1113 | ldr PC, [BASE, FRAME_PC]
1114 | add CARG2, BASE, #8
1115 | sub CARG3, BASE, #8
1116 | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1117 | // Returns 1=found, 0=end, -1=error.
1118 | .IOS ldr BASE, L->base
1121 | bgt ->fff_res // Found key/value.
1122 | bmi ->fff_fallback // Invalid key.
1123 | // End of traversal: return nil.
1124 | mvn CRET2, #~LJ_TNIL
1128 | checktab CARG2, ->fff_fallback
1130 | ldr TAB:RB, TAB:CARG1->metatable
1132 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1133 | ldr PC, [BASE, FRAME_PC]
1136 | bne ->fff_fallback
1138 | mvn CARG2, #~LJ_TNIL
1140 | strd CFUNC:CARG34, [BASE, #-8]
1141 | str CARG2, [BASE, #12]
1144 |.ffunc_2 ipairs_aux
1145 | checktp CARG2, LJ_TTAB
1146 | checktpeq CARG4, LJ_TISNUM
1147 | bne ->fff_fallback
1148 | ldr RB, TAB:CARG1->asize
1149 | ldr RC, TAB:CARG1->array
1150 | add CARG3, CARG3, #1
1151 | ldr PC, [BASE, FRAME_PC]
1153 | add RC, RC, CARG3, lsl #3
1154 | strd CARG34, [BASE, #-8]
1155 | ldrdlo CARG12, [RC]
1157 | bhs >2 // Not in array part?
1159 | checktp CARG2, LJ_TNIL
1160 | movne RC, #(2+1)*8
1161 | strdne CARG12, [BASE]
1163 |2: // Check for empty hash part first. Otherwise call C function.
1164 | ldr RB, TAB:CARG1->hmask
1169 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1170 | // Returns cTValue * or NULL.
1174 | ldrd CARG12, [CRET1]
1178 | checktab CARG2, ->fff_fallback
1180 | ldr TAB:RB, TAB:CARG1->metatable
1182 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1183 | ldr PC, [BASE, FRAME_PC]
1186 | bne ->fff_fallback
1189 | mvn CARG2, #~LJ_TISNUM
1191 | strd CFUNC:CARG34, [BASE, #-8]
1192 | strd CARG12, [BASE, #8]
1195 |//-- Base library: catch errors ----------------------------------------
1198 | ldr RB, L->maxstack
1199 | add INS, BASE, NARGS8:RC
1200 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1203 | blo ->fff_fallback
1204 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1206 | add BASE, BASE, #8
1207 | moveq PC, #8+FRAME_PCALL
1208 | movne PC, #8+FRAME_PCALLH
1209 | sub NARGS8:RC, NARGS8:RC, #8
1210 | b ->vm_call_dispatch
1213 | ldr RB, L->maxstack
1214 | add INS, BASE, NARGS8:RC
1215 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1217 | blo ->fff_fallback
1218 | checkfunc CARG4, ->fff_fallback // Traceback must be a function.
1220 | strd CARG12, [BASE, #8] // Swap function and traceback.
1221 | strd CARG34, [BASE]
1222 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1223 | add BASE, BASE, #16
1224 | moveq PC, #16+FRAME_PCALL
1225 | movne PC, #16+FRAME_PCALLH
1226 | sub NARGS8:RC, NARGS8:RC, #16
1227 | b ->vm_call_dispatch
1229 |//-- Coroutine library --------------------------------------------------
1231 |.macro coroutine_resume_wrap, resume
1233 |.ffunc_1 coroutine_resume
1234 | checktp CARG2, LJ_TTHREAD
1235 | bne ->fff_fallback
1237 |.ffunc coroutine_wrap_aux
1238 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1240 | ldr PC, [BASE, FRAME_PC]
1242 | ldr CARG2, L:CARG1->top
1243 | ldrb RA, L:CARG1->status
1244 | ldr RB, L:CARG1->base
1245 | add CARG3, CARG2, NARGS8:RC
1246 | add CARG4, CARG2, RA
1249 | beq ->fff_fallback
1250 | ldr CARG4, L:CARG1->maxstack
1251 | ldr RB, L:CARG1->cframe
1252 | cmp RA, #LUA_YIELD
1253 | cmpls CARG3, CARG4
1255 | bhi ->fff_fallback
1258 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1259 | add BASE, BASE, #8
1260 | sub NARGS8:RC, NARGS8:RC, #8
1262 | str CARG3, L:CARG1->top
1264 |2: // Move args to coroutine.
1265 | ldrd CARG34, [BASE, RB]
1267 | strdne CARG34, [CARG2, RB]
1274 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1275 | // Returns thread status.
1277 | ldr CARG3, L:RA->base
1278 | mv_vmstate CARG2, INTERP
1279 | ldr CARG4, L:RA->top
1280 | cmp CRET1, #LUA_YIELD
1282 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
1285 | subs RC, CARG4, CARG3
1286 | ldr CARG1, L->maxstack
1287 | add CARG2, BASE, RC
1288 | beq >6 // No results?
1291 | bhi >9 // Need to grow stack?
1294 | str CARG3, L:RA->top // Clear coroutine stack.
1295 |5: // Move results from coroutine.
1296 | ldrd CARG12, [CARG3, RB]
1298 | strd CARG12, [BASE, RB]
1303 | mvn CARG3, #~LJ_TTRUE
1306 | str CARG3, [BASE, #-4] // Prepend true/false to results.
1312 | ands CARG1, PC, #FRAME_TYPE
1314 | str RC, SAVE_MULTRES
1318 |8: // Coroutine returned with error (at co->top-1).
1320 | ldrd CARG12, [CARG4, #-8]!
1321 | mvn CARG3, #~LJ_TFALSE
1323 | str CARG4, L:RA->top // Remove error from coroutine stack.
1324 | strd CARG12, [BASE] // Copy error message.
1329 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1333 |9: // Handle stack expansion on return from yield.
1336 | bl extern lj_state_growstack // (lua_State *L, int n)
1341 | coroutine_resume_wrap 1 // coroutine.resume
1342 | coroutine_resume_wrap 0 // coroutine.wrap
1344 |.ffunc coroutine_yield
1345 | ldr CARG1, L->cframe
1346 | add CARG2, BASE, NARGS8:RC
1348 | tst CARG1, #CFRAME_RESUME
1350 | mov CRET1, #LUA_YIELD
1352 | beq ->fff_fallback
1353 | str CARG3, L->cframe
1354 | strb CRET1, L->status
1357 |//-- Math library -------------------------------------------------------
1359 |.macro math_round, func
1360 | .ffunc_1 math_ .. func
1361 | checktp CARG2, LJ_TISNUM
1363 | bhi ->fff_fallback
1364 | // Round FP value and normalize result.
1365 | lsl CARG3, CARG2, #1
1366 | adds RB, CARG3, #0x00200000
1367 | bpl >2 // |x| < 1?
1369 | subs RB, CARG4, RB, asr #21
1370 | lsl CARG4, CARG2, #11
1371 | lsl CARG3, CARG1, #11
1372 | orr CARG4, CARG4, #0x80000000
1374 | orr CARG4, CARG4, CARG1, lsr #21
1375 | bls >3 // |x| >= 2^31?
1376 | orr CARG3, CARG3, CARG4, lsl INS
1377 | lsr CARG1, CARG4, RB
1378 |.if "func" == "floor"
1379 | tst CARG3, CARG2, asr #31
1380 | addne CARG1, CARG1, #1
1382 | bics CARG3, CARG3, CARG2, asr #31
1383 | addsne CARG1, CARG1, #1
1388 | rsblt CARG1, CARG1, #0
1390 | mvn CARG2, #~LJ_TISNUM
1394 | bcs ->fff_restv // |x| is not finite.
1395 | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo
1396 |.if "func" == "floor"
1397 | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1
1401 | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1
1405 | mvn CARG2, #~LJ_TISNUM
1408 |3: // |x| >= 2^31. Check for x == -(2^31).
1409 | cmpeq CARG4, #0x80000000
1410 |.if "func" == "floor"
1415 | movmi CARG1, #0x80000000
1418 | bl ->vm_..func.._sf
1427 | .long 0x00000000, 0x41e00000 // 2^31.
1430 | checktp CARG2, LJ_TISNUM
1431 | bhi ->fff_fallback
1432 | bicne CARG2, CARG2, #0x80000000
1435 | rsbslt CARG1, CARG1, #0
1440 | // CARG12 = TValue result.
1441 | ldr PC, [BASE, FRAME_PC]
1442 | strd CARG12, [BASE, #-8]
1447 | // RC = (nresults+1)*8, PC = return.
1448 | ands CARG1, PC, #FRAME_TYPE
1449 | ldreq INS, [PC, #-4]
1450 | str RC, SAVE_MULTRES
1453 | decode_RB8 RB, INS
1455 | cmp RB, RC // More results expected?
1457 | decode_RA8 CARG1, INS
1460 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1461 | sub BASE, RA, CARG1
1464 |6: // Fill up results with nil.
1466 | mvn CARG1, #~LJ_TNIL
1468 | str CARG1, [CARG2, #-4]
1471 |.macro math_extern, func
1473 | .ffunc_d math_ .. func
1475 | .ffunc_n math_ .. func
1487 |.macro math_extern2, func
1489 | .ffunc_dd math_ .. func
1491 | .ffunc_nn math_ .. func
1504 | .ffunc_d math_sqrt
1507 | ldr PC, [BASE, FRAME_PC]
1508 | vstr d0, [BASE, #-8]
1516 | ldr CARG2, [BASE, #4]
1517 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1519 | bne ->fff_fallback
1521 | ldrd CARG12, [BASE]
1522 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1523 | bne ->fff_fallback
1525 | checktp CARG2, LJ_TISNUM
1526 | bhs ->fff_fallback
1548 | math_extern2 atan2
1553 | ldr CARG4, [BASE, #4]
1554 | ldrd CARG12, [BASE, #8]
1555 | cmp NARGS8:RC, #16
1556 | blo ->fff_fallback
1558 | checktp CARG4, LJ_TISNUM
1559 | bhs ->fff_fallback
1560 | checktp CARG2, LJ_TISNUM
1561 | bne ->fff_fallback
1563 | bl extern ldexp // (double x, int exp)
1567 |.ffunc_2 math_ldexp
1568 | checktp CARG2, LJ_TISNUM
1569 | bhs ->fff_fallback
1570 | checktp CARG4, LJ_TISNUM
1571 | bne ->fff_fallback
1573 | bl extern ldexp // (double x, int exp)
1579 |.ffunc_d math_frexp
1585 | mvn CARG4, #~LJ_TISNUM
1586 | ldr PC, [BASE, FRAME_PC]
1587 | vstr d0, [BASE, #-8]
1589 | strd CARG34, [BASE]
1592 |.ffunc_n math_frexp
1598 | mvn CARG4, #~LJ_TISNUM
1599 | ldr PC, [BASE, FRAME_PC]
1600 | strd CARG12, [BASE, #-8]
1602 | strd CARG34, [BASE]
1608 | sub CARG1, BASE, #8
1609 | ldr PC, [BASE, FRAME_PC]
1618 | sub CARG3, BASE, #8
1619 | ldr PC, [BASE, FRAME_PC]
1624 | strd CARG12, [BASE]
1628 |.macro math_minmax, name, cond, fcond
1632 | checktp CARG2, LJ_TISNUM
1635 |1: // Handle integers.
1639 | checktp CARG4, LJ_TISNUM
1643 | mov..cond CARG1, CARG3
1645 |3: // Convert intermediate result to number and continue below.
1647 | bhi ->fff_fallback
1649 | vcvt.f64.s32 d0, s4
1654 | bhi ->fff_fallback
1655 |5: // Handle numbers.
1660 | checktp CARG4, LJ_TISNUM
1666 | vmov..fcond.f64 d0, d1
1668 |7: // Convert integer to number and continue above.
1670 | bhi ->fff_fallback
1671 | vcvt.f64.s32 d1, s4
1677 | checktp CARG2, LJ_TISNUM
1680 |1: // Handle integers.
1681 | ldrd CARG34, [BASE, RA]
1684 | checktp CARG4, LJ_TISNUM
1688 | mov..cond CARG1, CARG3
1690 |3: // Convert intermediate result to number and continue below.
1691 | bhi ->fff_fallback
1692 | bl extern __aeabi_i2d
1693 | ldrd CARG34, [BASE, RA]
1697 | bhi ->fff_fallback
1698 |5: // Handle numbers.
1699 | ldrd CARG34, [BASE, RA]
1702 | checktp CARG4, LJ_TISNUM
1705 | bl extern __aeabi_cdcmple
1707 | mov..fcond CARG1, CARG3
1708 | mov..fcond CARG2, CARG4
1710 |7: // Convert integer to number and continue above.
1711 | bhi ->fff_fallback
1714 | bl extern __aeabi_i2d
1720 | math_minmax math_min, gt, pl
1721 | math_minmax math_max, lt, le
1723 |//-- String library -----------------------------------------------------
1725 |.ffunc string_byte // Only handle the 1-arg case here.
1726 | ldrd CARG12, [BASE]
1727 | ldr PC, [BASE, FRAME_PC]
1729 | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument.
1730 | bne ->fff_fallback
1731 | ldr CARG3, STR:CARG1->len
1732 | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
1733 | mvn CARG2, #~LJ_TISNUM
1735 | moveq RC, #(0+1)*8
1736 | movne RC, #(1+1)*8
1737 | strd CARG12, [BASE, #-8]
1740 |.ffunc string_char // Only handle the 1-arg case here.
1742 | ldrd CARG12, [BASE]
1743 | ldr PC, [BASE, FRAME_PC]
1744 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1745 | checktpeq CARG2, LJ_TISNUM
1746 | bicseq CARG4, CARG1, #255
1748 | bne ->fff_fallback
1750 | mov CARG2, TMPDp // Points to stack. Little-endian.
1752 | // CARG2 = str, CARG3 = len.
1756 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1758 | // Returns GCstr *.
1760 | mvn CARG2, #~LJ_TSTR
1765 | ldrd CARG12, [BASE]
1766 | ldrd CARG34, [BASE, #16]
1767 | cmp NARGS8:RC, #16
1770 | blo ->fff_fallback
1771 | checktp CARG4, LJ_TISNUM
1773 | bne ->fff_fallback
1775 | ldrd CARG34, [BASE, #8]
1776 | checktp CARG2, LJ_TSTR
1777 | ldreq CARG2, STR:CARG1->len
1778 | checktpeq CARG4, LJ_TISNUM
1779 | bne ->fff_fallback
1780 | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1781 | add CARG4, CARG2, #1
1782 | cmp CARG3, #0 // if (start < 0) start += len+1
1783 | addlt CARG3, CARG3, CARG4
1784 | cmp CARG3, #1 // if (start < 1) start = 1
1786 | cmp RB, #0 // if (end < 0) end += len+1
1787 | addlt RB, RB, CARG4
1788 | bic RB, RB, RB, asr #31 // if (end < 0) end = 0
1789 | cmp RB, CARG2 // if (end > len) end = len
1790 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1792 | add CARG2, CARG1, CARG3
1793 | subs CARG3, RB, CARG3 // len = end - start
1794 | add CARG3, CARG3, #1 // len += 1
1797 | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1798 | mvn CARG2, #~LJ_TSTR
1801 |.macro ffstring_op, name
1802 | .ffunc string_ .. name
1804 | ldr CARG3, [BASE, #4]
1806 | ldr STR:CARG2, [BASE]
1807 | blo ->fff_fallback
1808 | sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf)
1809 | checkstr CARG3, ->fff_fallback
1810 | ldr CARG4, SBUF:CARG1->b
1813 | str L, SBUF:CARG1->L
1814 | str CARG4, SBUF:CARG1->w
1815 | bl extern lj_buf_putstr_ .. name
1816 | bl extern lj_buf_tostr
1820 |ffstring_op reverse
1824 |//-- Bit library --------------------------------------------------------
1827 | bhi ->fff_fallback
1829 |// FP number to bit conversion for hard-float. Clobbers r0, d0-d1.
1831 | vmov d0, CARG1, CARG2
1832 | vadd.f64 d0, d0, d1
1836 | .long 0, 0x43380000 // (double)(2^52 + 2^51).
1838 |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1841 | adds RB, RB, #0x00200000
1842 | movpl CARG1, #0 // |x| < 1?
1845 | subs RB, CARG4, RB, asr #21
1846 | bmi >1 // |x| >= 2^32?
1847 | lsl CARG4, CARG2, #11
1848 | orr CARG4, CARG4, #0x80000000
1849 | orr CARG4, CARG4, CARG1, lsr #21
1851 | lsr CARG1, CARG4, RB
1852 | rsblt CARG1, CARG1, #0
1856 | lsr CARG4, CARG1, RB
1858 | lsl CARG1, CARG2, #12
1860 | orr CARG1, CARG4, CARG1, lsl RB
1861 | rsblt CARG1, CARG1, #0
1865 |.macro .ffunc_bit, name
1866 | .ffunc_1 bit_..name
1867 | checktp CARG2, LJ_TISNUM
1868 | blne ->vm_tobit_fb
1872 | mvn CARG2, #~LJ_TISNUM
1875 |.macro .ffunc_bit_op, name, ins
1880 | ldrd CARG12, [BASE, RA]
1884 | checktp CARG2, LJ_TISNUM
1885 | blne ->vm_tobit_fb
1886 | ins CARG3, CARG3, CARG1
1890 |.ffunc_bit_op band, and
1891 |.ffunc_bit_op bor, orr
1892 |.ffunc_bit_op bxor, eor
1895 | mvn CARG4, #~LJ_TISNUM
1896 | ldr PC, [BASE, FRAME_PC]
1897 | strd CARG34, [BASE, #-8]
1901 | eor CARG3, CARG1, CARG1, ror #16
1902 | bic CARG3, CARG3, #0x00ff0000
1903 | ror CARG1, CARG1, #8
1904 | mvn CARG2, #~LJ_TISNUM
1905 | eor CARG1, CARG1, CARG3, lsr #8
1910 | mvn CARG2, #~LJ_TISNUM
1913 |.macro .ffunc_bit_sh, name, ins, shmod
1915 | ldrd CARG12, [BASE, #8]
1916 | cmp NARGS8:RC, #16
1917 | blo ->fff_fallback
1918 | checktp CARG2, LJ_TISNUM
1919 | blne ->vm_tobit_fb
1921 | and RA, CARG1, #31
1925 | ldrd CARG12, [BASE]
1926 | checktp CARG2, LJ_TISNUM
1927 | blne ->vm_tobit_fb
1928 | ins CARG1, CARG1, RA
1929 | mvn CARG2, #~LJ_TISNUM
1933 |.ffunc_bit_sh lshift, lsl, 0
1934 |.ffunc_bit_sh rshift, lsr, 0
1935 |.ffunc_bit_sh arshift, asr, 0
1936 |.ffunc_bit_sh rol, ror, 1
1937 |.ffunc_bit_sh ror, ror, 0
1939 |//-----------------------------------------------------------------------
1941 |->fff_fallback: // Call fast function fallback handler.
1942 | // BASE = new base, RC = nargs*8
1943 | ldr CARG3, [BASE, FRAME_FUNC]
1944 | ldr CARG2, L->maxstack
1945 | add CARG1, BASE, NARGS8:RC
1946 | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC.
1948 | ldr CARG3, CFUNC:CARG3->f
1950 | add CARG1, CARG1, #8*LUA_MINSTACK
1951 | str PC, SAVE_PC // Redundant (but a defined value).
1954 | bhi >5 // Need to grow stack.
1955 | blx CARG3 // (lua_State *L)
1956 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1961 | bgt ->fff_res // Returned nresults+1?
1962 |1: // Returned 0 or -1: retry fast path.
1964 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1965 | sub NARGS8:RC, CARG1, BASE
1966 | bne ->vm_call_tail // Returned -1?
1967 | ins_callt // Returned 0: retry fast path.
1969 |// Reconstruct previous base for vmeta_call during tailcall.
1971 | ands CARG1, PC, #FRAME_TYPE
1972 | bic CARG2, PC, #FRAME_TYPEP
1973 | ldreq INS, [PC, #-4]
1974 | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8.
1975 | addeq CARG2, CARG2, #8
1976 | sub RB, BASE, CARG2
1977 | b ->vm_call_dispatch // Resolve again for tailcall.
1979 |5: // Grow stack for fallback handler.
1980 | mov CARG2, #LUA_MINSTACK
1981 | bl extern lj_state_growstack // (lua_State *L, int n)
1983 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1986 |->fff_gcstep: // Call GC step function.
1987 | // BASE = new base, RC = nargs*8
1990 | add CARG2, BASE, NARGS8:RC
1991 | str PC, SAVE_PC // Redundant (but a defined value).
1994 | bl extern lj_gc_step // (lua_State *L)
1996 | mov lr, RA // Help return address predictor.
1997 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
2000 |//-----------------------------------------------------------------------
2001 |//-- Special dispatch targets -------------------------------------------
2002 |//-----------------------------------------------------------------------
2004 |->vm_record: // Dispatch target for recording phase.
2006 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2007 | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent.
2009 | // Decrement the hookcount for consistency, but always do the call.
2010 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2011 | tst CARG1, #HOOK_ACTIVE
2013 | sub CARG2, CARG2, #1
2014 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2015 | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2019 |->vm_rethook: // Dispatch target for return hooks.
2020 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2021 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2023 |5: // Re-dispatch to static ins.
2025 | add OP, DISPATCH, OP, lsl #2
2026 | ldr pc, [OP, #GG_DISP2STATIC]
2028 |->vm_inshook: // Dispatch target for instr/line hooks.
2029 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2030 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2031 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2033 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2035 | subs CARG2, CARG2, #1
2036 | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2038 | tst CARG1, #LUA_MASKLINE
2044 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2045 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2048 |4: // Re-dispatch to static ins.
2049 | ldrb OP, [PC, #-4]
2050 | ldr INS, [PC, #-4]
2051 | add OP, DISPATCH, OP, lsl #2
2052 | ldr OP, [OP, #GG_DISP2STATIC]
2053 | decode_RA8 RA, INS
2057 |->cont_hook: // Continue from hook yield.
2058 | ldr CARG1, [CARG4, #-24]
2060 | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins.
2063 |->vm_hotloop: // Hot loop counter underflow.
2065 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L).
2066 | sub CARG1, DISPATCH, #-GG_DISP2J
2068 | ldr CARG3, LFUNC:CARG3->field_pc
2070 | str L, [DISPATCH, #DISPATCH_J(L)]
2071 | ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2073 | add CARG3, BASE, CARG3, lsl #3
2075 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2079 |->vm_callhook: // Dispatch target for call hooks.
2085 |->vm_hotcall: // Hot call counter underflow.
2090 | add CARG4, BASE, RC
2096 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2097 | // Returns ASMFunction.
2102 | sub NARGS8:RC, CARG4, BASE
2103 | str CARG2, SAVE_PC // Invalidate for subsequent line hook.
2104 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2105 | ldr INS, [PC, #-4]
2108 |->cont_stitch: // Trace stitching.
2110 | // RA = resultptr, CARG4 = meta base
2111 | ldr RB, SAVE_MULTRES
2112 | ldr INS, [PC, #-4]
2113 | ldr TRACE:CARG3, [CARG4, #-24] // Save previous trace.
2115 | decode_RA8 RC, INS // Call base.
2117 |1: // Move results down.
2121 | strd CARG12, [BASE, RC]
2125 | decode_RA8 RA, INS
2126 | decode_RB8 RB, INS
2130 | mvn CARG2, #~LJ_TNIL
2131 | bhi >9 // More results wanted?
2133 | ldrh RA, TRACE:CARG3->traceno
2134 | ldrh RC, TRACE:CARG3->link
2136 | beq ->cont_nop // Blacklisted.
2138 | bne =>BC_JLOOP // Jump to stitched trace.
2140 | // Stitch a new trace to the previous trace.
2141 | str RA, [DISPATCH, #DISPATCH_J(exitno)]
2142 | str L, [DISPATCH, #DISPATCH_J(L)]
2144 | sub CARG1, DISPATCH, #-GG_DISP2J
2146 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2150 |9: // Fill up results with nil.
2151 | strd CARG12, [BASE, RC]
2156 |->vm_profhook: // Dispatch target for profiler hook.
2161 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2162 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2168 |//-----------------------------------------------------------------------
2169 |//-- Trace exit handler -------------------------------------------------
2170 |//-----------------------------------------------------------------------
2175 | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2176 | ldr CARG1, [sp, #64] // Load original value of lr.
2177 | ldr DISPATCH, [lr] // Load DISPATCH.
2178 | add CARG3, sp, #64 // Recompute original value of sp.
2179 | mv_vmstate CARG4, EXIT
2180 | str CARG3, [sp, #52] // Store sp in RID_SP
2182 | ldr CARG2, [CARG1, #-4]! // Get exit instruction.
2183 | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC.
2184 | str CARG1, [sp, #60]
2188 | lsl CARG2, CARG2, #8
2189 | add CARG1, CARG1, CARG2, asr #6
2190 | ldr CARG2, [lr, #4] // Load exit stub group offset.
2191 | sub CARG1, CARG1, lr
2192 | ldr L, [DISPATCH, #DISPATCH_GL(cur_L)]
2193 | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number.
2194 | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2195 | str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2198 | str L, [DISPATCH, #DISPATCH_J(L)]
2199 | str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)]
2200 | sub CARG1, DISPATCH, #-GG_DISP2J
2202 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2203 | // Returns MULTRES (unscaled) or negated error code.
2204 | ldr CARG2, L->cframe
2206 | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2208 | ldr PC, SAVE_PC // Get SAVE_PC.
2209 | str L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2213 | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2217 | cmn CARG1, #LUA_ERRERR
2218 | bhs >9 // Check for error from exit.
2220 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2221 | str RC, SAVE_MULTRES
2224 | ldr CARG2, LFUNC:CARG2->field_pc
2225 | str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)]
2226 | mv_vmstate CARG4, INTERP
2227 | ldr KBASE, [CARG2, #PC2PROTO(k)]
2228 | // Modified copy of ins_next which handles function header dispatch, too.
2232 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2234 | cmn CARG1, #17 // Static dispatch?
2236 | cmp OP, #BC_FUNCC+2 // Fast function?
2239 | cmp OP, #BC_FUNCF // Function header?
2240 | ldr OP, [DISPATCH, OP, lsl #2]
2241 | decode_RA8 RA, INS
2242 | lsrlo RC, INS, #16 // No: Decode operands A*8 and D.
2244 | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8
2245 | ldrhs CARG3, [BASE, FRAME_FUNC]
2248 |4: // Check frame below fast function.
2249 | ldr CARG1, [BASE, FRAME_PC]
2250 | ands CARG2, CARG1, #FRAME_TYPE
2251 | bne <2 // Trace stitching continuation?
2252 | // Otherwise set KBASE for Lua function below fast function.
2253 | ldr CARG3, [CARG1, #-4]
2254 | decode_RA8 CARG1, CARG3
2255 | sub CARG2, BASE, CARG1
2256 | ldr LFUNC:CARG3, [CARG2, #-16]
2257 | ldr CARG3, LFUNC:CARG3->field_pc
2258 | ldr KBASE, [CARG3, #PC2PROTO(k)]
2261 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2262 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
2264 | ldr TRACE:CARG1, [CARG1, RC, lsl #2]
2265 | ldr INS, TRACE:CARG1->startins
2267 | decode_RA8 RA, INS
2268 | add OP, DISPATCH, OP, lsl #2
2270 | ldr pc, [OP, #GG_DISP2STATIC]
2272 |9: // Rethrow error from the right C frame.
2273 | rsb CARG2, CARG1, #0
2275 | bl extern lj_err_trace // (lua_State *L, int errcode)
2278 |//-----------------------------------------------------------------------
2279 |//-- Math helper functions ----------------------------------------------
2280 |//-----------------------------------------------------------------------
2282 |// FP value rounding. Called from JIT code.
2284 |// double lj_vm_floor/ceil/trunc(double x);
2285 |.macro vm_round, func, hf
2287 | vmov CARG1, CARG2, d0
2289 | lsl CARG3, CARG2, #1
2290 | adds RB, CARG3, #0x00200000
2291 | bpl >2 // |x| < 1?
2293 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2294 | bxlo lr // |x| >= 2^52: done.
2296 | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask
2297 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2299 | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2300 | orrpl CARG3, CARG3, CARG4
2302 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2303 |.if "func" == "floor"
2304 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2306 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2309 | vmoveq d0, CARG1, CARG2
2311 | bxeq lr // iszero: done.
2314 | lslpl CARG3, CARG4, RB
2317 | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask
2318 | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry
2320 | vmov d0, CARG1, CARG2
2325 | bxcs lr // |x| is not finite.
2326 | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo
2327 |.if "func" == "floor"
2328 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2330 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2332 | mov CARG1, #0 // lo = 0
2333 | and CARG2, CARG2, #0x80000000
2334 | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2335 | orrne CARG2, CARG2, CARG4
2337 | vmov d0, CARG1, CARG2
2343 | .long 0x3ff00000 // hiword(+1.0)
2359 |.macro vm_trunc, hf
2362 | vmov CARG1, CARG2, d0
2364 | lsl CARG3, CARG2, #1
2365 | adds RB, CARG3, #0x00200000
2366 | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0.
2369 | vmovpl d0, CARG1, CARG2
2373 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2374 | bxlo lr // |x| >= 2^52: already done.
2376 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2378 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2380 | vmov d0, CARG1, CARG2
2393 | // double lj_vm_mod(double dividend, double divisor);
2396 | // Special calling convention. Also, RC (r11) is not preserved.
2397 | vdiv.f64 d0, d6, d7
2399 | vmov CARG1, CARG2, d0
2401 | vmov d0, CARG1, CARG2
2402 | vmul.f64 d0, d0, d7
2404 | vsub.f64 d6, d6, d0
2407 | push {r0, r1, r2, r3, r4, lr}
2408 | bl extern __aeabi_ddiv
2410 | ldrd CARG34, [sp, #8]
2411 | bl extern __aeabi_dmul
2413 | eor CARG2, CARG2, #0x80000000
2414 | bl extern __aeabi_dadd
2419 | // int lj_vm_modi(int dividend, int divisor);
2421 | ands RB, CARG1, #0x80000000
2422 | rsbmi CARG1, CARG1, #0 // a = |dividend|
2423 | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor).
2425 | rsbmi CARG2, CARG2, #0 // b = |divisor|
2426 | subs CARG4, CARG2, #1
2427 | cmpne CARG1, CARG2
2428 | moveq CARG1, #0 // if (b == 1 || a == b) a = 0
2429 | tsthi CARG2, CARG4
2430 | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1
2432 | // Use repeated subtraction to get the remainder.
2435 | sub CARG4, CARG4, CARG3
2436 | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8
2437 | addne pc, pc, CARG3, lsl #3 // Duff's device.
2441 for (i = 31; i >= 0; i--) {
2442 | cmp CARG1, CARG2, lsl #i
2443 | subhs CARG1, CARG1, CARG2, lsl #i
2449 | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b
2450 | eors CARG2, CARG1, RB, lsl #1
2451 | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y
2454 |//-----------------------------------------------------------------------
2455 |//-- Miscellaneous functions --------------------------------------------
2456 |//-----------------------------------------------------------------------
2458 |.define NEXT_TAB, TAB:CARG1
2459 |.define NEXT_RES, CARG1
2460 |.define NEXT_IDX, CARG2
2461 |.define NEXT_TMP0, CARG3
2462 |.define NEXT_TMP1, CARG4
2463 |.define NEXT_LIM, r12
2464 |.define NEXT_RES_PTR, sp
2465 |.define NEXT_RES_VAL, [sp]
2466 |.define NEXT_RES_KEY_I, [sp, #8]
2467 |.define NEXT_RES_KEY_IT, [sp, #12]
2469 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2470 |// Next idx returned in CRET2.
2473 | ldr NEXT_TMP0, NEXT_TAB->array
2474 | ldr NEXT_LIM, NEXT_TAB->asize
2475 | add NEXT_TMP0, NEXT_TMP0, NEXT_IDX, lsl #3
2476 |1: // Traverse array part.
2477 | subs NEXT_TMP1, NEXT_IDX, NEXT_LIM
2479 | ldr NEXT_TMP1, [NEXT_TMP0, #4]
2480 | str NEXT_IDX, NEXT_RES_KEY_I
2481 | add NEXT_TMP0, NEXT_TMP0, #8
2482 | add NEXT_IDX, NEXT_IDX, #1
2483 | checktp NEXT_TMP1, LJ_TNIL
2484 | beq <1 // Skip holes in array part.
2485 | ldr NEXT_TMP0, [NEXT_TMP0, #-8]
2486 | mov NEXT_RES, NEXT_RES_PTR
2487 | strd NEXT_TMP0, NEXT_RES_VAL // Stores NEXT_TMP1, too.
2488 | mvn NEXT_TMP0, #~LJ_TISNUM
2489 | str NEXT_TMP0, NEXT_RES_KEY_IT
2492 |5: // Traverse hash part.
2493 | ldr NEXT_TMP0, NEXT_TAB->hmask
2494 | ldr NODE:NEXT_RES, NEXT_TAB->node
2495 | add NEXT_TMP1, NEXT_TMP1, NEXT_TMP1, lsl #1
2496 | add NEXT_LIM, NEXT_LIM, NEXT_TMP0
2497 | add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP1, lsl #3
2499 | cmp NEXT_IDX, NEXT_LIM
2501 | ldr NEXT_TMP1, NODE:NEXT_RES->val.it
2502 | checktp NEXT_TMP1, LJ_TNIL
2503 | add NEXT_IDX, NEXT_IDX, #1
2505 | // Skip holes in hash part.
2506 | add NEXT_RES, NEXT_RES, #sizeof(Node)
2509 |9: // End of iteration. Set the key to nil (not the value).
2511 | mov NEXT_RES, NEXT_RES_PTR
2512 | str NEXT_TMP0, NEXT_RES_KEY_IT
2516 |//-----------------------------------------------------------------------
2517 |//-- FFI helper functions -----------------------------------------------
2518 |//-----------------------------------------------------------------------
2520 |// Handler for callback functions.
2521 |// Saveregs already performed. Callback slot number in [sp], g in r12.
2524 |.type CTSTATE, CTState, PC
2525 | ldr CTSTATE, GL:r12->ctype_state
2526 | add DISPATCH, r12, #GG_G2DISP
2529 | add r4, sp, CFRAME_SPACE+4+8*8
2530 | vstmdb r4!, {d8-d15}
2533 | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2535 | strd CARG34, CTSTATE->cb.gpr[2]
2536 | strd CARG12, CTSTATE->cb.gpr[0]
2538 | vstmdb r12!, {d0-d7}
2541 | add CARG3, sp, #CFRAME_SIZE
2542 | mov CARG1, CTSTATE
2543 | lsr CARG4, CARG4, #3
2544 | str CARG3, CTSTATE->cb.stack
2546 | str CARG4, CTSTATE->cb.slot
2547 | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2548 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2549 | // Returns lua_State *.
2550 | ldr BASE, L:CRET1->base
2551 | mv_vmstate CARG2, INTERP
2552 | ldr RC, L:CRET1->top
2554 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2557 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2562 |->cont_ffi_callback: // Return from FFI callback.
2564 | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2568 | mov CARG1, CTSTATE
2570 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2571 | ldrd CARG12, CTSTATE->cb.gpr[0]
2573 | vldr d0, CTSTATE->cb.fpr[0]
2578 |->vm_ffi_call: // Call C function via FFI.
2579 | // Caveat: needs special frame unwinding, see below.
2581 | .type CCSTATE, CCallState, r4
2582 | push {CCSTATE, r5, r11, lr}
2583 | mov CCSTATE, CARG1
2584 | ldr CARG1, CCSTATE:CARG1->spadj
2585 | ldrb CARG2, CCSTATE->nsp
2586 | add CARG3, CCSTATE, #offsetof(CCallState, stack)
2588 | add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2591 | sub sp, sp, CARG1 // Readjust stack.
2592 | subs CARG2, CARG2, #4
2596 | ldr RB, CCSTATE->func
2598 |1: // Copy stack slots.
2599 | ldr CARG4, [CARG3, CARG2]
2600 | str CARG4, [sp, CARG2]
2601 | subs CARG2, CARG2, #4
2604 | ldrd CARG12, CCSTATE->gpr[0]
2605 | ldrd CARG34, CCSTATE->gpr[2]
2609 | add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2611 | strd CRET1, CCSTATE->gpr[0]
2613 | vstmdb r12!, {d0-d3}
2615 | pop {CCSTATE, r5, r11, pc}
2617 |// Note: vm_ffi_call must be the last function in this object file!
2619 |//-----------------------------------------------------------------------
2622 /* Generate the code for a single instruction. */
2623 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2630 /* -- Comparison ops ---------------------------------------------------- */
2632 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2634 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2635 | // RA = src1*8, RC = src2, JMP with RC = target
2637 | ldrd CARG12, [RA, BASE]!
2639 | ldrd CARG34, [RC, BASE]!
2641 | add RB, PC, RB, lsl #2
2642 | checktp CARG2, LJ_TISNUM
2644 | checktp CARG4, LJ_TISNUM
2647 if (op == BC_ISLT) {
2648 | sublt PC, RB, #0x20000
2649 } else if (op == BC_ISGE) {
2650 | subge PC, RB, #0x20000
2651 } else if (op == BC_ISLE) {
2652 | suble PC, RB, #0x20000
2654 | subgt PC, RB, #0x20000
2659 |3: // CARG12 is not an integer.
2663 | // d0 is a number.
2664 | checktp CARG4, LJ_TISNUM
2668 | // d0 is a number, CARG3 is an integer.
2670 | vcvt.f64.s32 d1, s4
2672 |4: // CARG1 is an integer, CARG34 is not an integer.
2675 | // CARG1 is an integer, d1 is a number.
2677 | vcvt.f64.s32 d0, s4
2678 |5: // d0 and d1 are numbers.
2681 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2682 if (op == BC_ISLT) {
2683 | sublo PC, RB, #0x20000
2684 } else if (op == BC_ISGE) {
2685 | subhs PC, RB, #0x20000
2686 } else if (op == BC_ISLE) {
2687 | subls PC, RB, #0x20000
2689 | subhi PC, RB, #0x20000
2694 | // CARG12 is a number.
2695 | checktp CARG4, LJ_TISNUM
2696 | movlo RA, RB // Save RB.
2699 | // CARG12 is a number, CARG3 is an integer.
2702 | mov RA, RB // Save RB.
2703 | bl extern __aeabi_i2d
2706 | ldrd CARG12, [RC] // Restore first operand.
2708 |4: // CARG1 is an integer, CARG34 is not an integer.
2710 | // CARG1 is an integer, CARG34 is a number.
2711 | mov RA, RB // Save RB.
2712 | bl extern __aeabi_i2d
2713 | ldrd CARG34, [RC] // Restore second operand.
2714 |5: // CARG12 and CARG34 are numbers.
2715 | bl extern __aeabi_cdcmple
2716 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2717 if (op == BC_ISLT) {
2718 | sublo PC, RA, #0x20000
2719 } else if (op == BC_ISGE) {
2720 | subhs PC, RA, #0x20000
2721 } else if (op == BC_ISLE) {
2722 | subls PC, RA, #0x20000
2724 | subhi PC, RA, #0x20000
2730 case BC_ISEQV: case BC_ISNEV:
2731 vk = op == BC_ISEQV;
2732 | // RA = src1*8, RC = src2, JMP with RC = target
2734 | ldrd CARG12, [RA, BASE]!
2736 | ldrd CARG34, [RC, BASE]!
2738 | add RB, PC, RB, lsl #2
2739 | checktp CARG2, LJ_TISNUM
2740 | cmnls CARG4, #-LJ_TISNUM
2746 | // Either or both types are not numbers.
2748 | checktp CARG2, LJ_TCDATA
2749 | checktpne CARG4, LJ_TCDATA
2750 | beq ->vmeta_equal_cd
2752 | cmp CARG2, CARG4 // Compare types.
2753 | bne >2 // Not the same type?
2754 | checktp CARG2, LJ_TISPRI
2755 | bhs >1 // Same type and primitive type?
2757 | // Same types and not a primitive type. Compare GCobj or pvalue.
2760 | bne >3 // Different GCobjs or pvalues?
2761 |1: // Branch if same.
2762 | sub PC, RB, #0x20000
2766 | checktp CARG2, LJ_TISTABUD
2767 | bhi <2 // Different objects and not table/ud?
2769 | beq >1 // Same GCobjs or pvalues?
2770 | checktp CARG2, LJ_TISTABUD
2771 | bhi >2 // Different objects and not table/ud?
2773 | // Different tables or userdatas. Need to check __eq metamethod.
2774 | // Field metatable must be at same offset for GCtab and GCudata!
2775 | ldr TAB:RA, TAB:CARG1->metatable
2778 | beq <2 // No metatable?
2780 | beq >2 // No metatable?
2782 | ldrb RA, TAB:RA->nomm
2783 | mov CARG4, #1-vk // ne = 0 or 1.
2786 | beq ->vmeta_equal // 'no __eq' flag not set?
2790 |2: // Branch if different.
2791 | sub PC, RB, #0x20000
2797 case BC_ISEQS: case BC_ISNES:
2798 vk = op == BC_ISEQS;
2799 | // RA = src*8, RC = str_const (~), JMP with RC = target
2801 | ldrd CARG12, [BASE, RA]
2803 | ldr STR:CARG3, [KBASE, RC, lsl #2]
2805 | add RB, PC, RB, lsl #2
2806 | checktp CARG2, LJ_TSTR
2811 | cmpeq CARG1, CARG3
2814 | subeq PC, RB, #0x20000
2818 | subne PC, RB, #0x20000
2824 | checktp CARG2, LJ_TCDATA
2826 | b ->vmeta_equal_cd
2830 case BC_ISEQN: case BC_ISNEN:
2831 vk = op == BC_ISEQN;
2832 | // RA = src*8, RC = num_const (~), JMP with RC = target
2834 | ldrd CARG12, [RA, BASE]!
2836 | ldrd CARG34, [RC, KBASE]!
2838 | add RB, PC, RB, lsl #2
2844 | checktp CARG2, LJ_TISNUM
2846 | checktp CARG4, LJ_TISNUM
2850 | subeq PC, RB, #0x20000
2854 | subne PC, RB, #0x20000
2859 |3: // CARG12 is not an integer.
2864 | subhi PC, RB, #0x20000
2869 | checktp CARG4, LJ_TISNUM
2873 | vcvths.f64.s32 d1, s4
2875 |4: // CARG1 is an integer, d1 is a number.
2878 | vcvt.f64.s32 d0, s4
2879 |5: // d0 and d1 are numbers.
2883 | subeq PC, RB, #0x20000
2885 | subne PC, RB, #0x20000
2889 | // CARG12 is a number.
2890 | checktp CARG4, LJ_TISNUM
2891 | movlo RA, RB // Save RB.
2893 | // CARG12 is a number, CARG3 is an integer.
2896 |4: // CARG1 is an integer, CARG34 is a number.
2897 | mov RA, RB // Save RB.
2898 | bl extern __aeabi_i2d
2899 | ldrd CARG34, [RC] // Restore other operand.
2900 |5: // CARG12 and CARG34 are numbers.
2901 | bl extern __aeabi_cdcmpeq
2903 | subeq PC, RA, #0x20000
2905 | subne PC, RA, #0x20000
2912 | checktp CARG2, LJ_TCDATA
2914 | b ->vmeta_equal_cd
2918 case BC_ISEQP: case BC_ISNEP:
2919 vk = op == BC_ISEQP;
2920 | // RA = src*8, RC = primitive_type (~), JMP with RC = target
2921 | ldrd CARG12, [BASE, RA]
2925 | add RB, PC, RB, lsl #2
2927 | checktp CARG2, LJ_TCDATA
2928 | beq ->vmeta_equal_cd
2932 | subeq PC, RB, #0x20000
2934 | subne PC, RB, #0x20000
2939 /* -- Unary test and copy ops ------------------------------------------- */
2941 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2942 | // RA = dst*8 or unused, RC = src, JMP with RC = target
2943 | add RC, BASE, RC, lsl #3
2947 | add RB, PC, RB, lsl #2
2948 | checktp CARG2, LJ_TTRUE
2949 if (op == BC_ISTC || op == BC_IST) {
2950 | subls PC, RB, #0x20000
2951 if (op == BC_ISTC) {
2952 | strdls CARG12, [BASE, RA]
2955 | subhi PC, RB, #0x20000
2956 if (op == BC_ISFC) {
2957 | strdhi CARG12, [BASE, RA]
2964 | // RA = src*8, RC = -type
2965 | ldrd CARG12, [BASE, RA]
2969 | bne ->vmeta_istype
2973 | // RA = src*8, RC = -(TISNUM-1)
2974 | ldrd CARG12, [BASE, RA]
2976 | checktp CARG2, LJ_TISNUM
2978 | bhs ->vmeta_istype
2982 /* -- Unary ops --------------------------------------------------------- */
2985 | // RA = dst*8, RC = src
2988 | ldrd CARG12, [BASE, RC]
2990 | strd CARG12, [BASE, RA]
2994 | // RA = dst*8, RC = src
2995 | add RC, BASE, RC, lsl #3
2997 | ldr CARG1, [RC, #4]
3000 | checktp CARG1, LJ_TTRUE
3001 | mvnls CARG2, #~LJ_TFALSE
3002 | mvnhi CARG2, #~LJ_TTRUE
3003 | str CARG2, [RA, #4]
3007 | // RA = dst*8, RC = src
3009 | ldrd CARG12, [BASE, RC]
3012 | checktp CARG2, LJ_TISNUM
3014 | eorne CARG2, CARG2, #0x80000000
3016 | rsbseq CARG1, CARG1, #0
3019 | strd CARG12, [BASE, RA]
3024 | .long 0x00000000, 0x41e00000 // 2^31.
3027 | // RA = dst*8, RC = src
3029 | ldrd CARG12, [BASE, RC]
3030 | checkstr CARG2, >2
3031 | ldr CARG1, STR:CARG1->len
3033 | mvn CARG2, #~LJ_TISNUM
3036 | strd CARG12, [BASE, RA]
3039 | checktab CARG2, ->vmeta_len
3041 | ldr TAB:CARG3, TAB:CARG1->metatable
3048 | bl extern lj_tab_len // (GCtab *t)
3049 | // Returns uint32_t (but less than 2^31).
3054 | ldrb CARG4, TAB:CARG3->nomm
3055 | tst CARG4, #1<<MM_len
3056 | bne <3 // 'no __len' flag set: done.
3061 /* -- Binary ops -------------------------------------------------------- */
3063 |.macro ins_arithcheck, cond, ncond, target
3065 | cmn CARG4, #-LJ_TISNUM
3066 | cmn..cond CARG2, #-LJ_TISNUM
3068 | cmn CARG2, #-LJ_TISNUM
3069 | cmn..cond CARG4, #-LJ_TISNUM
3073 |.macro ins_arithcheck_int, target
3074 | ins_arithcheck eq, ne, target
3076 |.macro ins_arithcheck_num, target
3077 | ins_arithcheck lo, hs, target
3080 |.macro ins_arithpre
3081 | decode_RB8 RB, INS
3082 | decode_RC8 RC, INS
3083 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3084 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3088 | ldrd CARG12, [RB, BASE]!
3089 | ldrd CARG34, [RC, KBASE]!
3091 | ldrd CARG12, [BASE, RB]
3092 | ldrd CARG34, [KBASE, RC]
3097 | ldrd CARG34, [RB, BASE]!
3098 | ldrd CARG12, [RC, KBASE]!
3100 | ldrd CARG34, [BASE, RB]
3101 | ldrd CARG12, [KBASE, RC]
3106 | ldrd CARG12, [RB, BASE]!
3107 | ldrd CARG34, [RC, BASE]!
3109 | ldrd CARG12, [BASE, RB]
3110 | ldrd CARG34, [BASE, RC]
3116 |.macro ins_arithpre_fpu, reg1, reg2
3128 |.macro ins_arithpost_fpu, reg
3136 |.macro ins_arithfallback, ins
3139 | ins ->vmeta_arith_vn
3142 | ins ->vmeta_arith_nv
3145 | ins ->vmeta_arith_vv
3150 |.macro ins_arithdn, intins, fpins, fpcall
3152 |.if "intins" ~= "vm_modi" and not FPU
3155 | ins_arithcheck_int >5
3156 |.if "intins" == "smull"
3157 | smull CARG1, RC, CARG3, CARG1
3158 | cmp RC, CARG1, asr #31
3159 | ins_arithfallback bne
3160 |.elif "intins" == "vm_modi"
3162 | ins_arithfallback beq
3164 | mvn CARG2, #~LJ_TISNUM
3166 | intins CARG1, CARG1, CARG3
3167 | ins_arithfallback bvs
3170 |.if "intins" == "vm_modi" or FPU
3174 | strd CARG12, [BASE, RA]
3177 | ins_arithpre_fpu d6, d7
3178 | ins_arithfallback ins_arithcheck_num
3180 |.if "intins" == "vm_modi"
3185 | ins_arithpost_fpu d6
3188 |.if "intins" ~= "vm_modi"
3195 |.macro ins_arithfp, fpins, fpcall
3197 |.if "fpins" ~= "extern" or HFABI
3198 | ins_arithpre_fpu d0, d1
3200 | ins_arithfallback ins_arithcheck_num
3201 |.if "fpins" == "extern"
3210 |.if ("fpins" ~= "extern" or HFABI) and FPU
3211 | ins_arithpost_fpu d0
3215 | strd CARG12, [BASE, RA]
3220 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3221 | ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3223 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3224 | ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3226 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3227 | ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3229 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3230 | ins_arithfp vdiv.f64, extern __aeabi_ddiv
3232 case BC_MODVN: case BC_MODNV: case BC_MODVV:
3233 | ins_arithdn vm_modi, vm_mod, ->vm_mod
3236 | // NYI: (partial) integer arithmetic.
3237 | ins_arithfp extern, extern pow
3241 | decode_RB8 RC, INS
3242 | decode_RC8 RB, INS
3243 | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!)
3246 | add CARG2, BASE, RB
3248 | // RA = dst*8, RC = src_start*8, CARG2 = top-1
3251 | lsr CARG3, CARG3, #3
3252 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3253 | // Returns NULL (finished) or TValue * (metamethod).
3257 | ldrd CARG34, [BASE, RC]
3260 | strd CARG34, [BASE, RA] // Copy result to RA.
3264 /* -- Constant ops ------------------------------------------------------ */
3267 | // RA = dst*8, RC = str_const (~)
3270 | ldr CARG1, [KBASE, RC, lsl #2]
3271 | mvn CARG2, #~LJ_TSTR
3273 | strd CARG12, [BASE, RA]
3278 | // RA = dst*8, RC = cdata_const (~)
3281 | ldr CARG1, [KBASE, RC, lsl #2]
3282 | mvn CARG2, #~LJ_TCDATA
3284 | strd CARG12, [BASE, RA]
3289 | // RA = dst*8, (RC = int16_literal)
3290 | mov CARG1, INS, asr #16 // Refetch sign-extended reg.
3291 | mvn CARG2, #~LJ_TISNUM
3294 | strd CARG12, [BASE, RA]
3298 | // RA = dst*8, RC = num_const
3301 | ldrd CARG12, [KBASE, RC]
3303 | strd CARG12, [BASE, RA]
3307 | // RA = dst*8, RC = primitive_type (~)
3316 | // RA = base*8, RC = end
3318 | add RC, BASE, RC, lsl #3
3319 | mvn CARG1, #~LJ_TNIL
3320 | str CARG1, [RA, #4]
3323 | str CARG1, [RA, #4]
3330 /* -- Upvalue and function ops ------------------------------------------ */
3333 | // RA = dst*8, RC = uvnum
3334 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3336 | add RC, RC, #offsetof(GCfuncL, uvptr)
3337 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3338 | ldr CARG2, UPVAL:CARG2->v
3339 | ldrd CARG34, [CARG2]
3342 | strd CARG34, [BASE, RA]
3346 | // RA = uvnum*8, RC = src
3347 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3349 | add RA, RA, #offsetof(GCfuncL, uvptr)
3351 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3352 | ldrd CARG34, [BASE, RC]
3353 | ldrb RB, UPVAL:CARG2->marked
3354 | ldrb RC, UPVAL:CARG2->closed
3355 | ldr CARG2, UPVAL:CARG2->v
3356 | tst RB, #LJ_GC_BLACK // isblack(uv)
3357 | add RB, CARG4, #-LJ_TISGCV
3359 | strd CARG34, [CARG2]
3360 | bne >2 // Upvalue is closed and black?
3364 |2: // Check if new value is collectable.
3365 | cmn RB, #-(LJ_TNUMX - LJ_TISGCV)
3366 | ldrbhi RC, GCOBJ:CARG3->gch.marked
3367 | bls <1 // tvisgcv(v)
3368 | sub CARG1, DISPATCH, #-GG_DISP2G
3369 | tst RC, #LJ_GC_WHITES
3370 | // Crossed a write barrier. Move the barrier forward.
3374 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3377 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3382 | // RA = uvnum*8, RC = str_const (~)
3383 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3385 | add RA, RA, #offsetof(GCfuncL, uvptr)
3387 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3388 | ldr STR:CARG3, [KBASE, RC, lsl #2]
3389 | ldrb RB, UPVAL:CARG2->marked
3390 | ldrb RC, UPVAL:CARG2->closed
3391 | ldr CARG2, UPVAL:CARG2->v
3392 | mvn CARG4, #~LJ_TSTR
3393 | tst RB, #LJ_GC_BLACK // isblack(uv)
3394 | ldrb RB, STR:CARG3->marked
3395 | strd CARG34, [CARG2]
3400 |2: // Check if string is white and ensure upvalue is closed.
3401 | tst RB, #LJ_GC_WHITES // iswhite(str)
3403 | sub CARG1, DISPATCH, #-GG_DISP2G
3404 | // Crossed a write barrier. Move the barrier forward.
3408 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3411 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3416 | // RA = uvnum*8, RC = num_const
3417 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3419 | add RA, RA, #offsetof(GCfuncL, uvptr)
3421 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3422 | ldrd CARG34, [KBASE, RC]
3423 | ldr CARG2, UPVAL:CARG2->v
3426 | strd CARG34, [CARG2]
3430 | // RA = uvnum*8, RC = primitive_type (~)
3431 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3433 | add RA, RA, #offsetof(GCfuncL, uvptr)
3434 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3436 | ldr CARG2, UPVAL:CARG2->v
3439 | str RC, [CARG2, #4]
3444 | // RA = level*8, RC = target
3445 | ldr CARG3, L->openupval
3446 | add RC, PC, RC, lsl #2
3449 | sub PC, RC, #0x20000
3452 | add CARG2, BASE, RA
3453 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3460 | // RA = dst*8, RC = proto_const (~) (holding function prototype)
3463 | ldr CARG2, [KBASE, RC, lsl #2]
3465 | ldr CARG3, [BASE, FRAME_FUNC]
3467 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3468 | bl extern lj_func_newL_gc
3469 | // Returns GCfuncL *.
3471 | mvn CARG2, #~LJ_TFUNC
3474 | strd CARG12, [BASE, RA]
3478 /* -- Table ops --------------------------------------------------------- */
3482 | // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3483 if (op == BC_TDUP) {
3486 | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3487 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3494 if (op == BC_TNEW) {
3495 | lsl CARG2, RC, #21
3496 | lsr CARG3, RC, #11
3497 | asr RC, CARG2, #21
3498 | lsr CARG2, CARG2, #21
3500 | addeq CARG2, CARG2, #2
3501 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3502 | // Returns GCtab *.
3504 | ldr CARG2, [KBASE, RC, lsl #2]
3505 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3506 | // Returns GCtab *.
3509 | mvn CARG2, #~LJ_TTAB
3512 | strd CARG12, [BASE, RA]
3515 | bl extern lj_gc_step_fixtop // (lua_State *L)
3521 | // RA = dst*8, RC = str_const (~)
3523 | // RA = dst*8, RC = str_const (~)
3524 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3526 | ldr TAB:CARG1, LFUNC:CARG2->env
3527 | ldr STR:RC, [KBASE, RC, lsl #2]
3528 if (op == BC_GGET) {
3536 | decode_RB8 RB, INS
3537 | decode_RC8 RC, INS
3538 | // RA = dst*8, RB = table*8, RC = key*8
3539 | ldrd TAB:CARG12, [BASE, RB]
3540 | ldrd CARG34, [BASE, RC]
3541 | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12.
3542 | checktp CARG4, LJ_TISNUM // Integer key?
3543 | ldreq CARG4, TAB:CARG1->array
3544 | ldreq CARG2, TAB:CARG1->asize
3547 | add CARG4, CARG4, CARG3, lsl #3
3548 | cmp CARG3, CARG2 // In array part?
3549 | ldrdlo CARG34, [CARG4]
3551 | ins_next1 // Overwrites RB!
3552 | checktp CARG4, LJ_TNIL
3556 | strd CARG34, [BASE, RA]
3559 |5: // Check for __index if table value is nil.
3560 | ldr TAB:CARG2, TAB:CARG1->metatable
3562 | beq <1 // No metatable: done.
3563 | ldrb CARG2, TAB:CARG2->nomm
3564 | tst CARG2, #1<<MM_index
3565 | bne <1 // 'no __index' flag set: done.
3566 | decode_RB8 RB, INS // Restore RB.
3570 | checktp CARG4, LJ_TSTR // String key?
3571 | moveq STR:RC, CARG3
3576 | decode_RB8 RB, INS
3578 | // RA = dst*8, RB = table*8, RC = str_const (~)
3579 | ldrd CARG12, [BASE, RB]
3581 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3582 | checktab CARG2, ->vmeta_tgets1
3584 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3585 | ldr CARG3, TAB:CARG1->hmask
3586 | ldr CARG4, STR:RC->sid
3587 | ldr NODE:INS, TAB:CARG1->node
3588 | mov TAB:RB, TAB:CARG1
3589 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3590 | add CARG3, CARG3, CARG3, lsl #1
3591 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3593 | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS.
3594 | ldrd CARG34, NODE:INS->val
3595 | ldr NODE:INS, NODE:INS->next
3596 | checktp CARG2, LJ_TSTR
3597 | cmpeq CARG1, STR:RC
3599 | checktp CARG4, LJ_TNIL
3604 | strd CARG34, [BASE, RA]
3607 |4: // Follow hash chain.
3610 | // End of hash chain: key not found, nil result.
3612 |5: // Check for __index if table value is nil.
3613 | ldr TAB:CARG1, TAB:RB->metatable
3614 | mov CARG3, #0 // Optional clear of undef. value (during load stall).
3615 | mvn CARG4, #~LJ_TNIL
3617 | beq <3 // No metatable: done.
3618 | ldrb CARG2, TAB:CARG1->nomm
3619 | tst CARG2, #1<<MM_index
3620 | bne <3 // 'no __index' flag set: done.
3624 | decode_RB8 RB, INS
3626 | // RA = dst*8, RB = table*8, RC = index
3627 | ldrd CARG12, [BASE, RB]
3628 | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12.
3629 | ldr CARG3, TAB:CARG1->asize
3630 | ldr CARG4, TAB:CARG1->array
3633 | ldrdlo CARG34, [CARG4, CARG2]
3635 | ins_next1 // Overwrites RB!
3636 | checktp CARG4, LJ_TNIL
3640 | strd CARG34, [BASE, RA]
3643 |5: // Check for __index if table value is nil.
3644 | ldr TAB:CARG2, TAB:CARG1->metatable
3646 | beq <1 // No metatable: done.
3647 | ldrb CARG2, TAB:CARG2->nomm
3648 | tst CARG2, #1<<MM_index
3649 | bne <1 // 'no __index' flag set: done.
3653 | decode_RB8 RB, INS
3654 | decode_RC8 RC, INS
3655 | // RA = dst*8, RB = table*8, RC = key*8
3656 | ldr TAB:CARG1, [BASE, RB]
3657 | ldr CARG2, [BASE, RC]
3658 | ldr CARG4, TAB:CARG1->array
3659 | ldr CARG3, TAB:CARG1->asize
3660 | add CARG4, CARG4, CARG2, lsl #3
3661 | cmp CARG2, CARG3 // In array part?
3663 | ldrd CARG12, [CARG4]
3667 | strd CARG12, [BASE, RA]
3672 | decode_RB8 RB, INS
3673 | decode_RC8 RC, INS
3674 | // RA = src*8, RB = table*8, RC = key*8
3675 | ldrd TAB:CARG12, [BASE, RB]
3676 | ldrd CARG34, [BASE, RC]
3677 | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12.
3678 | checktp CARG4, LJ_TISNUM // Integer key?
3679 | ldreq CARG2, TAB:CARG1->array
3680 | ldreq CARG4, TAB:CARG1->asize
3683 | add CARG2, CARG2, CARG3, lsl #3
3684 | cmp CARG3, CARG4 // In array part?
3685 | ldrlo INS, [CARG2, #4]
3687 | ins_next1 // Overwrites RB!
3688 | checktp INS, LJ_TNIL
3689 | ldrb INS, TAB:CARG1->marked
3690 | ldrd CARG34, [BASE, RA]
3693 | tst INS, #LJ_GC_BLACK // isblack(table)
3694 | strd CARG34, [CARG2]
3700 |5: // Check for __newindex if previous value is nil.
3701 | ldr TAB:RA, TAB:CARG1->metatable
3703 | beq <1 // No metatable: done.
3704 | ldrb RA, TAB:RA->nomm
3705 | tst RA, #1<<MM_newindex
3706 | bne <1 // 'no __newindex' flag set: done.
3707 | ldr INS, [PC, #-4] // Restore RA and RB.
3708 | decode_RB8 RB, INS
3709 | decode_RA8 RA, INS
3712 |7: // Possible table write barrier for the value. Skip valiswhite check.
3713 | barrierback TAB:CARG1, INS, CARG3
3717 | checktp CARG4, LJ_TSTR // String key?
3718 | moveq STR:RC, CARG3
3723 | decode_RB8 RB, INS
3725 | // RA = src*8, RB = table*8, RC = str_const (~)
3726 | ldrd CARG12, [BASE, RB]
3728 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3729 | checktab CARG2, ->vmeta_tsets1
3731 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3732 | ldr CARG3, TAB:CARG1->hmask
3733 | ldr CARG4, STR:RC->sid
3734 | ldr NODE:INS, TAB:CARG1->node
3735 | mov TAB:RB, TAB:CARG1
3736 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3737 | add CARG3, CARG3, CARG3, lsl #1
3739 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3740 | strb CARG4, TAB:RB->nomm // Clear metamethod cache.
3742 | ldrd CARG12, NODE:INS->key
3743 | ldr CARG4, NODE:INS->val.it
3744 | ldr NODE:CARG3, NODE:INS->next
3745 | checktp CARG2, LJ_TSTR
3746 | cmpeq CARG1, STR:RC
3748 | ldrb CARG2, TAB:RB->marked
3749 | checktp CARG4, LJ_TNIL // Key found, but nil value?
3750 | ldrd CARG34, [BASE, RA]
3753 | tst CARG2, #LJ_GC_BLACK // isblack(table)
3754 | strd CARG34, NODE:INS->val
3759 |4: // Check for __newindex if previous value is nil.
3760 | ldr TAB:CARG1, TAB:RB->metatable
3762 | beq <2 // No metatable: done.
3763 | ldrb CARG1, TAB:CARG1->nomm
3764 | tst CARG1, #1<<MM_newindex
3765 | bne <2 // 'no __newindex' flag set: done.
3768 |5: // Follow hash chain.
3769 | movs NODE:INS, NODE:CARG3
3771 | // End of hash chain: key not found, add a new one.
3773 | // But check for __newindex first.
3774 | ldr TAB:CARG1, TAB:RB->metatable
3777 | cmp TAB:CARG1, #0 // No metatable: continue.
3779 | ldrbne CARG2, TAB:CARG1->nomm
3782 | tst CARG2, #1<<MM_newindex
3783 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3785 | mvn CARG4, #~LJ_TSTR
3786 | str STR:RC, TMPDlo
3789 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3790 | // Returns TValue *.
3792 | ldrd CARG34, [BASE, RA]
3793 | strd CARG34, [CRET1]
3794 | b <3 // No 2nd write barrier needed.
3796 |7: // Possible table write barrier for the value. Skip valiswhite check.
3797 | barrierback TAB:RB, CARG2, CARG3
3801 | decode_RB8 RB, INS
3803 | // RA = src*8, RB = table*8, RC = index
3804 | ldrd CARG12, [BASE, RB]
3805 | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12.
3806 | ldr CARG3, TAB:CARG1->asize
3807 | ldr RB, TAB:CARG1->array
3810 | ldrdlo CARG34, [CARG2, RB]!
3812 | ins_next1 // Overwrites RB!
3813 | checktp CARG4, LJ_TNIL
3814 | ldrb INS, TAB:CARG1->marked
3815 | ldrd CARG34, [BASE, RA]
3818 | tst INS, #LJ_GC_BLACK // isblack(table)
3819 | strd CARG34, [CARG2]
3825 |5: // Check for __newindex if previous value is nil.
3826 | ldr TAB:RA, TAB:CARG1->metatable
3828 | beq <1 // No metatable: done.
3829 | ldrb RA, TAB:RA->nomm
3830 | tst RA, #1<<MM_newindex
3831 | bne <1 // 'no __newindex' flag set: done.
3832 | ldr INS, [PC, #-4] // Restore INS.
3833 | decode_RA8 RA, INS
3836 |7: // Possible table write barrier for the value. Skip valiswhite check.
3837 | barrierback TAB:CARG1, INS, CARG3
3841 | decode_RB8 RB, INS
3842 | decode_RC8 RC, INS
3843 | // RA = src*8, RB = table*8, RC = key*8
3844 | ldr TAB:CARG2, [BASE, RB]
3845 | ldr CARG3, [BASE, RC]
3846 | ldrb INS, TAB:CARG2->marked
3847 | ldr CARG1, TAB:CARG2->array
3848 | ldr CARG4, TAB:CARG2->asize
3849 | tst INS, #LJ_GC_BLACK // isblack(table)
3850 | add CARG1, CARG1, CARG3, lsl #3
3853 | cmp CARG3, CARG4 // In array part?
3856 | ldrd CARG34, [BASE, RA]
3859 | strd CARG34, [CARG1]
3862 |7: // Possible table write barrier for the value. Skip valiswhite check.
3863 | barrierback TAB:CARG2, INS, RB
3868 | // RA = base*8 (table at base-1), RC = num_const (start index)
3871 | ldr RB, SAVE_MULTRES
3872 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
3873 | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
3875 | ldr CARG4, TAB:CARG2->asize
3876 | beq >4 // Nothing to copy?
3877 | add CARG3, CARG1, RB, lsr #3
3879 | ldr CARG4, TAB:CARG2->array
3882 | add INS, CARG4, CARG1, lsl #3
3883 | ldrb CARG1, TAB:CARG2->marked
3884 |3: // Copy result slots to table.
3885 | ldrd CARG34, [RA], #8
3886 | strd CARG34, [INS], #8
3889 | tst CARG1, #LJ_GC_BLACK // isblack(table)
3894 |5: // Need to resize array part.
3898 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3899 | // Must not reallocate the stack.
3900 | .IOS ldr BASE, L->base
3903 |7: // Possible table write barrier for any value. Skip valiswhite check.
3904 | barrierback TAB:CARG2, CARG1, CARG3
3908 /* -- Calls and vararg handling ----------------------------------------- */
3911 | // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3912 | ldr CARG1, SAVE_MULTRES
3913 | decode_RC8 NARGS8:RC, INS
3914 | add NARGS8:RC, NARGS8:RC, CARG1
3918 | decode_RC8 NARGS8:RC, INS
3919 | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3921 | mov RB, BASE // Save old BASE for vmeta_call.
3922 | ldrd CARG34, [BASE, RA]!
3923 | sub NARGS8:RC, NARGS8:RC, #8
3924 | add BASE, BASE, #8
3925 | checkfunc CARG4, ->vmeta_call
3930 | // RA = base*8, (RB = 0,) RC = extra_nargs
3931 | ldr CARG1, SAVE_MULTRES
3932 | add NARGS8:RC, CARG1, RC, lsl #3
3936 | lsl NARGS8:RC, RC, #3
3937 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3939 | ldrd LFUNC:CARG34, [RA, BASE]!
3940 | sub NARGS8:RC, NARGS8:RC, #8
3942 | checkfunc CARG4, ->vmeta_callt
3943 | ldr PC, [BASE, FRAME_PC]
3946 | ldrb CARG4, LFUNC:CARG3->ffid
3947 | tst PC, #FRAME_TYPE
3950 | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
3954 | ldrd CARG12, [RA, RB]
3956 | cmp INS, NARGS8:RC
3957 | strd CARG12, [BASE, RB]
3961 | cmp CARG4, #1 // (> FF_C) Calling a fast function?
3966 |5: // Tailcall to a fast function with a Lua frame below.
3967 | ldr INS, [PC, #-4]
3968 | decode_RA8 RA, INS
3969 | sub CARG1, BASE, RA
3970 | ldr LFUNC:CARG1, [CARG1, #-16]
3971 | ldr CARG1, LFUNC:CARG1->field_pc
3972 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3975 |7: // Tailcall from a vararg function.
3976 | eor PC, PC, #FRAME_VARG
3977 | tst PC, #FRAME_TYPEP // Vararg frame below?
3978 | movne CARG4, #0 // Clear ffid if no Lua function below.
3980 | sub BASE, BASE, PC
3981 | ldr PC, [BASE, FRAME_PC]
3982 | tst PC, #FRAME_TYPE
3983 | movne CARG4, #0 // Clear ffid if no Lua function below.
3988 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3990 | mov RB, BASE // Save old BASE for vmeta_call.
3991 | ldrd CARG34, [RA, #-16]
3992 | ldrd CARG12, [RA, #-8]
3994 | strd CARG34, [RA, #8] // Copy state.
3995 | strd CARG12, [RA, #16] // Copy control var.
3996 | // STALL: locked CARG34.
3997 | ldrd LFUNC:CARG34, [RA, #-24]
3998 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3999 | // STALL: load CARG34.
4000 | strd LFUNC:CARG34, [RA] // Copy callable.
4001 | checkfunc CARG4, ->vmeta_call
4010 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
4012 | ldr TAB:RB, [RA, #-16]
4013 | ldr CARG1, [RA, #-8] // Get index from control var.
4014 | ldr INS, TAB:RB->asize
4015 | ldr CARG2, TAB:RB->array
4017 |1: // Traverse array part.
4018 | subs RC, CARG1, INS
4019 | add CARG3, CARG2, CARG1, lsl #3
4020 | bhs >5 // Index points after array part?
4021 | ldrd CARG34, [CARG3]
4022 | checktp CARG4, LJ_TNIL
4023 | addeq CARG1, CARG1, #1 // Skip holes in array part.
4025 | ldrh RC, [PC, #-2]
4026 | mvn CARG2, #~LJ_TISNUM
4027 | strd CARG34, [RA, #8]
4028 | add RC, PC, RC, lsl #2
4031 | sub PC, RC, #0x20000
4032 | str RB, [RA, #-8] // Update control var.
4036 |5: // Traverse hash part.
4037 | ldr CARG4, TAB:RB->hmask
4038 | ldr NODE:RB, TAB:RB->node
4040 | add CARG1, RC, RC, lsl #1
4041 | cmp RC, CARG4 // End of iteration? Branch to ITERL+1.
4042 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
4044 | ldrd CARG12, NODE:CARG3->val
4045 | checktp CARG2, LJ_TNIL
4047 | beq <6 // Skip holes in hash part.
4048 | ldrh RB, [PC, #-2]
4050 | ldrd CARG34, NODE:CARG3->key
4051 | str RC, [RA, #-8] // Update control var.
4052 | strd CARG12, [RA, #8]
4053 | add RC, PC, RB, lsl #2
4054 | sub PC, RC, #0x20000
4060 | // RA = base*8, RC = target (points to ITERN)
4062 | add RC, PC, RC, lsl #2
4063 | ldrd CFUNC:CARG12, [RA, #-24]
4064 | ldr CARG3, [RA, #-12]
4065 | ldr CARG4, [RA, #-4]
4066 | checktp CARG2, LJ_TFUNC
4067 | ldrbeq CARG1, CFUNC:CARG1->ffid
4068 | checktpeq CARG3, LJ_TTAB
4069 | checktpeq CARG4, LJ_TNIL
4070 | cmpeq CARG1, #FF_next_N
4071 | subeq PC, RC, #0x20000
4076 | mvn CARG2, #~LJ_KEYINDEX
4077 | strd CARG1, [RA, #-8] // Initialize control var.
4080 |5: // Despecialize bytecode if any of the checks fail.
4081 | mov CARG1, #BC_JMP
4083 | strb CARG1, [PC, #-4]
4084 | sub PC, RC, #0x20000
4087 | cmp CARG1, #BC_ITERN
4090 | strb OP, [PC] // Subsumes ins_next1.
4094 |6: // Unpatch JLOOP.
4095 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4096 | ldrh CARG2, [PC, #2]
4097 | ldr TRACE:CARG1, [CARG1, CARG2, lsl #2]
4098 | // Subsumes ins_next1 and ins_next2.
4099 | ldr INS, TRACE:CARG1->startins
4100 | bfi INS, OP, #0, #8
4107 | decode_RB8 RB, INS
4108 | decode_RC8 RC, INS
4109 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4110 | ldr CARG1, [BASE, FRAME_PC]
4113 | add RC, RC, #FRAME_VARG
4115 | sub CARG3, BASE, #8 // CARG3 = vtop
4116 | sub RC, RC, CARG1 // RC = vbase
4117 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4119 | sub CARG1, CARG3, RC
4120 | beq >5 // Copy all varargs?
4121 | sub CARG4, CARG4, #16
4122 |1: // Copy vararg slots to destination slots.
4124 | ldrdlo CARG12, [RC], #8
4125 | mvnhs CARG2, #~LJ_TNIL
4127 | strd CARG12, [RA], #8
4132 |5: // Copy all varargs.
4133 | ldr CARG4, L->maxstack
4135 | movle RB, #8 // MULTRES = (0+1)*8
4136 | addgt RB, CARG1, #8
4137 | add CARG2, RA, CARG1
4138 | str RB, SAVE_MULTRES
4143 | ldrd CARG12, [RC], #8
4144 | strd CARG12, [RA], #8
4149 |7: // Grow stack for varargs.
4150 | lsr CARG2, CARG1, #3
4154 | sub RC, RC, BASE // Need delta, because BASE may change.
4157 | bl extern lj_state_growstack // (lua_State *L, int n)
4161 | sub CARG3, BASE, #8
4165 /* -- Returns ----------------------------------------------------------- */
4168 | // RA = results*8, RC = extra results
4169 | ldr CARG1, SAVE_MULTRES
4170 | ldr PC, [BASE, FRAME_PC]
4172 | add RC, CARG1, RC, lsl #3
4177 | // RA = results*8, RC = nresults+1
4178 | ldr PC, [BASE, FRAME_PC]
4182 | str RC, SAVE_MULTRES
4184 | ands CARG1, PC, #FRAME_TYPE
4185 | eor CARG2, PC, #FRAME_VARG
4189 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
4190 | ldr INS, [PC, #-4]
4191 | subs CARG4, RC, #8
4192 | sub CARG3, BASE, #8
4195 | ldrd CARG12, [RA], #8
4196 | add BASE, BASE, #8
4197 | subs CARG4, CARG4, #8
4198 | strd CARG12, [BASE, #-16]
4201 | decode_RA8 RA, INS
4202 | sub CARG4, CARG3, RA
4203 | decode_RB8 RB, INS
4204 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4206 | cmp RB, RC // More results expected?
4209 | ldr CARG2, LFUNC:CARG1->field_pc
4212 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4215 |6: // Fill up results with nil.
4216 | mvn CARG2, #~LJ_TNIL
4217 | add BASE, BASE, #8
4219 | str CARG2, [BASE, #-12]
4222 |->BC_RETV1_Z: // Non-standard return case.
4225 | tst CARG2, #FRAME_TYPEP
4227 | // Return from vararg function: relocate BASE down.
4228 | sub BASE, BASE, CARG2
4229 | ldr PC, [BASE, FRAME_PC]
4233 case BC_RET0: case BC_RET1:
4234 | // RA = results*8, RC = nresults+1
4235 | ldr PC, [BASE, FRAME_PC]
4237 | str RC, SAVE_MULTRES
4238 | ands CARG1, PC, #FRAME_TYPE
4239 | eor CARG2, PC, #FRAME_VARG
4240 | ldreq INS, [PC, #-4]
4242 if (op == BC_RET1) {
4243 | ldrd CARG12, [BASE, RA]
4245 | sub CARG4, BASE, #8
4246 | decode_RA8 RA, INS
4247 if (op == BC_RET1) {
4248 | strd CARG12, [CARG4]
4250 | sub BASE, CARG4, RA
4251 | decode_RB8 RB, INS
4252 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4256 | ldr CARG2, LFUNC:CARG1->field_pc
4259 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4262 |6: // Fill up results with nil.
4263 | sub CARG2, CARG4, #4
4264 | mvn CARG3, #~LJ_TNIL
4265 | str CARG3, [CARG2, RC]
4270 /* -- Loops and branches ------------------------------------------------ */
4272 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
4273 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
4274 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4275 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
4281 | // Fall through. Assumes BC_IFORL follows.
4291 | // RA = base*8, RC = target (after end of loop or start of loop)
4292 vk = (op == BC_IFORL || op == BC_JFORL);
4293 | ldrd CARG12, [RA, BASE]!
4294 if (op != BC_JFORL) {
4295 | add RC, PC, RC, lsl #2
4298 | ldrd CARG34, FOR_STOP
4299 | checktp CARG2, LJ_TISNUM
4302 | checktp CARG4, LJ_TISNUM
4303 | ldr CARG4, FOR_STEP
4304 | checktpeq RB, LJ_TISNUM
4310 | ldrd CARG34, FOR_STEP
4311 | checktp CARG2, LJ_TISNUM
4313 | adds CARG1, CARG1, CARG3
4314 | ldr CARG4, FOR_STOP
4315 if (op == BC_IFORL) {
4316 | addvs RC, PC, #0x20000 // Overflow: prevent branch.
4318 | bvs >2 // Overflow: do not enter mcode.
4325 if (op == BC_FORI) {
4326 | subgt PC, RC, #0x20000
4327 } else if (op == BC_JFORI) {
4328 | sub PC, RC, #0x20000
4329 | ldrhle RC, [PC, #-2]
4330 } else if (op == BC_IFORL) {
4331 | suble PC, RC, #0x20000
4334 | strd CARG12, FOR_IDX
4339 | strd CARG12, FOR_EXT
4340 if (op == BC_JFORI || op == BC_JFORL) {
4346 |4: // Invert check for negative step.
4356 | cmnlo CARG4, #-LJ_TISNUM
4357 | cmnlo RB, #-LJ_TISNUM
4366 | strd CARG12, FOR_EXT
4375 | vadd.f64 d0, d0, d2
4379 | bl extern __aeabi_dadd
4380 | strd CARG12, FOR_IDX
4381 | ldrd CARG34, FOR_STOP
4382 | strd CARG12, FOR_EXT
4391 | bl extern __aeabi_cdcmple
4399 if (op == BC_FORI) {
4400 | subhi PC, RC, #0x20000
4401 } else if (op == BC_JFORI) {
4402 | sub PC, RC, #0x20000
4403 | ldrhls RC, [PC, #-2]
4405 } else if (op == BC_IFORL) {
4406 | subls PC, RC, #0x20000
4415 |8: // Invert check for negative step.
4417 | bl extern __aeabi_dadd
4418 | strd CARG12, FOR_IDX
4419 | strd CARG12, FOR_EXT
4423 | ldrd CARG12, FOR_STOP
4432 | // Fall through. Assumes BC_IITERL follows.
4440 | // RA = base*8, RC = target
4441 | ldrd CARG12, [RA, BASE]!
4442 if (op == BC_JITERL) {
4443 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4444 | strdne CARG12, [RA, #-8]
4447 | add RC, PC, RC, lsl #2
4448 | // STALL: load CARG12.
4449 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4450 | subne PC, RC, #0x20000 // Otherwise save control var + branch.
4451 | strdne CARG12, [RA, #-8]
4457 | // RA = base*8, RC = target (loop extent)
4458 | // Note: RA/RC is only used by trace recorder to determine scope/extent
4459 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4463 | // Fall through. Assumes BC_ILOOP follows.
4467 | // RA = base*8, RC = target (loop extent)
4473 | // RA = base (ignored), RC = traceno
4474 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4475 | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0.
4476 | ldr TRACE:RC, [CARG1, RC, lsl #2]
4478 | ldr RA, TRACE:RC->mcode
4479 | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4480 | str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)]
4486 | // RA = base*8 (only used by trace recorder), RC = target
4487 | add RC, PC, RC, lsl #2
4488 | sub PC, RC, #0x20000
4492 /* -- Function headers -------------------------------------------------- */
4498 case BC_FUNCV: /* NYI: compiled vararg functions. */
4499 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4507 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4508 | ldr CARG1, L->maxstack
4509 | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4510 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4512 | bhi ->vm_growstack_l
4513 if (op != BC_JFUNCF) {
4518 | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters.
4519 | mvn CARG4, #~LJ_TNIL
4521 if (op == BC_JFUNCF) {
4528 |3: // Clear missing parameters.
4529 | strd CARG34, [BASE, NARGS8:RC]
4530 | add NARGS8:RC, NARGS8:RC, #8
4538 | NYI // NYI: compiled vararg functions
4539 break; /* NYI: compiled vararg functions. */
4542 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4543 | ldr CARG1, L->maxstack
4544 | add CARG4, BASE, RC
4546 | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC.
4547 | add CARG2, RC, #8+FRAME_VARG
4548 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4550 | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG.
4551 | bhs ->vm_growstack_l
4552 | ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4556 | add BASE, CARG4, #8
4558 | mvn CARG3, #~LJ_TNIL
4560 | cmp RA, RC // Less args than parameters?
4561 | ldrdlo CARG12, [RA], #8
4562 | movhs CARG2, CARG3
4563 | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC).
4566 | strd CARG12, [CARG4, #8]!
4574 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4575 if (op == BC_FUNCC) {
4576 | ldr CARG4, CFUNC:CARG3->f
4578 | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4580 | add CARG2, RA, NARGS8:RC
4581 | ldr CARG1, L->maxstack
4582 | add RC, BASE, NARGS8:RC
4586 if (op == BC_FUNCCW) {
4587 | ldr CARG2, CFUNC:CARG3->f
4589 | mv_vmstate CARG3, C
4591 | bhi ->vm_growstack_c // Need to grow stack.
4593 | blx CARG4 // (lua_State *L [, lua_CFunction f])
4594 | // Returns nresults.
4596 | mv_vmstate CARG3, INTERP
4598 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
4601 | ldr PC, [BASE, FRAME_PC]
4602 | sub RA, CRET2, RC // RA = L->top - nresults*8
4606 /* ---------------------------------------------------------------------- */
4609 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4615 static int build_backend(BuildCtx *ctx)
4619 dasm_growpc(Dst, BC__MAX);
4621 build_subroutines(ctx);
4624 for (op = 0; op < BC__MAX; op++)
4625 build_ins(ctx, (BCOp)op, op);
4630 /* Emit pseudo frame-info for all assembler functions. */
4631 static void emit_asm_debug(BuildCtx *ctx)
4633 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4635 switch (ctx->mode) {
4637 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4640 "\t.long .LECIE0-.LSCIE0\n"
4642 "\t.long 0xffffffff\n"
4647 "\t.byte 0xe\n" /* Return address is in lr. */
4648 "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */
4653 "\t.long .LEFDE0-.LASFDE0\n"
4655 "\t.long .Lframe0\n"
4658 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4659 "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */
4660 fcofs, CFRAME_SIZE);
4661 for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */
4662 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4664 for (i = 15; i >= 8; i--) /* offset d8-d15 */
4665 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4666 64+2*i, 10+2*(15-i));
4667 fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */
4675 "\t.long .LEFDE1-.LASFDE1\n"
4677 "\t.long .Lframe0\n"
4678 "\t.long lj_vm_ffi_call\n"
4680 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4681 "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */
4682 "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */
4683 "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */
4684 "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */
4685 "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */
4687 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);