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 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1200 | blo ->fff_fallback
1201 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1203 | add BASE, BASE, #8
1204 | moveq PC, #8+FRAME_PCALL
1205 | movne PC, #8+FRAME_PCALLH
1206 | sub NARGS8:RC, NARGS8:RC, #8
1207 | b ->vm_call_dispatch
1210 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1211 | checkfunc CARG4, ->fff_fallback // Traceback must be a function.
1213 | strd CARG12, [BASE, #8] // Swap function and traceback.
1214 | strd CARG34, [BASE]
1215 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1216 | add BASE, BASE, #16
1217 | moveq PC, #16+FRAME_PCALL
1218 | movne PC, #16+FRAME_PCALLH
1219 | sub NARGS8:RC, NARGS8:RC, #16
1220 | b ->vm_call_dispatch
1222 |//-- Coroutine library --------------------------------------------------
1224 |.macro coroutine_resume_wrap, resume
1226 |.ffunc_1 coroutine_resume
1227 | checktp CARG2, LJ_TTHREAD
1228 | bne ->fff_fallback
1230 |.ffunc coroutine_wrap_aux
1231 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1233 | ldr PC, [BASE, FRAME_PC]
1235 | ldr CARG2, L:CARG1->top
1236 | ldrb RA, L:CARG1->status
1237 | ldr RB, L:CARG1->base
1238 | add CARG3, CARG2, NARGS8:RC
1239 | add CARG4, CARG2, RA
1242 | beq ->fff_fallback
1243 | ldr CARG4, L:CARG1->maxstack
1244 | ldr RB, L:CARG1->cframe
1245 | cmp RA, #LUA_YIELD
1246 | cmpls CARG3, CARG4
1248 | bhi ->fff_fallback
1251 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1252 | add BASE, BASE, #8
1253 | sub NARGS8:RC, NARGS8:RC, #8
1255 | str CARG3, L:CARG1->top
1257 |2: // Move args to coroutine.
1258 | ldrd CARG34, [BASE, RB]
1260 | strdne CARG34, [CARG2, RB]
1267 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1268 | // Returns thread status.
1270 | ldr CARG3, L:RA->base
1271 | mv_vmstate CARG2, INTERP
1272 | ldr CARG4, L:RA->top
1273 | cmp CRET1, #LUA_YIELD
1275 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
1278 | subs RC, CARG4, CARG3
1279 | ldr CARG1, L->maxstack
1280 | add CARG2, BASE, RC
1281 | beq >6 // No results?
1284 | bhi >9 // Need to grow stack?
1287 | str CARG3, L:RA->top // Clear coroutine stack.
1288 |5: // Move results from coroutine.
1289 | ldrd CARG12, [CARG3, RB]
1291 | strd CARG12, [BASE, RB]
1296 | mvn CARG3, #~LJ_TTRUE
1299 | str CARG3, [BASE, #-4] // Prepend true/false to results.
1305 | ands CARG1, PC, #FRAME_TYPE
1307 | str RC, SAVE_MULTRES
1311 |8: // Coroutine returned with error (at co->top-1).
1313 | ldrd CARG12, [CARG4, #-8]!
1314 | mvn CARG3, #~LJ_TFALSE
1316 | str CARG4, L:RA->top // Remove error from coroutine stack.
1317 | strd CARG12, [BASE] // Copy error message.
1322 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1326 |9: // Handle stack expansion on return from yield.
1329 | bl extern lj_state_growstack // (lua_State *L, int n)
1334 | coroutine_resume_wrap 1 // coroutine.resume
1335 | coroutine_resume_wrap 0 // coroutine.wrap
1337 |.ffunc coroutine_yield
1338 | ldr CARG1, L->cframe
1339 | add CARG2, BASE, NARGS8:RC
1341 | tst CARG1, #CFRAME_RESUME
1343 | mov CRET1, #LUA_YIELD
1345 | beq ->fff_fallback
1346 | str CARG3, L->cframe
1347 | strb CRET1, L->status
1350 |//-- Math library -------------------------------------------------------
1352 |.macro math_round, func
1353 | .ffunc_1 math_ .. func
1354 | checktp CARG2, LJ_TISNUM
1356 | bhi ->fff_fallback
1357 | // Round FP value and normalize result.
1358 | lsl CARG3, CARG2, #1
1359 | adds RB, CARG3, #0x00200000
1360 | bpl >2 // |x| < 1?
1362 | subs RB, CARG4, RB, asr #21
1363 | lsl CARG4, CARG2, #11
1364 | lsl CARG3, CARG1, #11
1365 | orr CARG4, CARG4, #0x80000000
1367 | orr CARG4, CARG4, CARG1, lsr #21
1368 | bls >3 // |x| >= 2^31?
1369 | orr CARG3, CARG3, CARG4, lsl INS
1370 | lsr CARG1, CARG4, RB
1371 |.if "func" == "floor"
1372 | tst CARG3, CARG2, asr #31
1373 | addne CARG1, CARG1, #1
1375 | bics CARG3, CARG3, CARG2, asr #31
1376 | addsne CARG1, CARG1, #1
1381 | rsblt CARG1, CARG1, #0
1383 | mvn CARG2, #~LJ_TISNUM
1387 | bcs ->fff_restv // |x| is not finite.
1388 | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo
1389 |.if "func" == "floor"
1390 | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1
1394 | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1
1398 | mvn CARG2, #~LJ_TISNUM
1401 |3: // |x| >= 2^31. Check for x == -(2^31).
1402 | cmpeq CARG4, #0x80000000
1403 |.if "func" == "floor"
1408 | movmi CARG1, #0x80000000
1411 | bl ->vm_..func.._sf
1420 | .long 0x00000000, 0x41e00000 // 2^31.
1423 | checktp CARG2, LJ_TISNUM
1424 | bhi ->fff_fallback
1425 | bicne CARG2, CARG2, #0x80000000
1428 | rsbslt CARG1, CARG1, #0
1433 | // CARG12 = TValue result.
1434 | ldr PC, [BASE, FRAME_PC]
1435 | strd CARG12, [BASE, #-8]
1440 | // RC = (nresults+1)*8, PC = return.
1441 | ands CARG1, PC, #FRAME_TYPE
1442 | ldreq INS, [PC, #-4]
1443 | str RC, SAVE_MULTRES
1446 | decode_RB8 RB, INS
1448 | cmp RB, RC // More results expected?
1450 | decode_RA8 CARG1, INS
1453 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1454 | sub BASE, RA, CARG1
1457 |6: // Fill up results with nil.
1459 | mvn CARG1, #~LJ_TNIL
1461 | str CARG1, [CARG2, #-4]
1464 |.macro math_extern, func
1466 | .ffunc_d math_ .. func
1468 | .ffunc_n math_ .. func
1480 |.macro math_extern2, func
1482 | .ffunc_dd math_ .. func
1484 | .ffunc_nn math_ .. func
1497 | .ffunc_d math_sqrt
1500 | ldr PC, [BASE, FRAME_PC]
1501 | vstr d0, [BASE, #-8]
1509 | ldr CARG2, [BASE, #4]
1510 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1512 | bne ->fff_fallback
1514 | ldrd CARG12, [BASE]
1515 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1516 | bne ->fff_fallback
1518 | checktp CARG2, LJ_TISNUM
1519 | bhs ->fff_fallback
1541 | math_extern2 atan2
1546 | ldr CARG4, [BASE, #4]
1547 | ldrd CARG12, [BASE, #8]
1548 | cmp NARGS8:RC, #16
1549 | blo ->fff_fallback
1551 | checktp CARG4, LJ_TISNUM
1552 | bhs ->fff_fallback
1553 | checktp CARG2, LJ_TISNUM
1554 | bne ->fff_fallback
1556 | bl extern ldexp // (double x, int exp)
1560 |.ffunc_2 math_ldexp
1561 | checktp CARG2, LJ_TISNUM
1562 | bhs ->fff_fallback
1563 | checktp CARG4, LJ_TISNUM
1564 | bne ->fff_fallback
1566 | bl extern ldexp // (double x, int exp)
1572 |.ffunc_d math_frexp
1578 | mvn CARG4, #~LJ_TISNUM
1579 | ldr PC, [BASE, FRAME_PC]
1580 | vstr d0, [BASE, #-8]
1582 | strd CARG34, [BASE]
1585 |.ffunc_n math_frexp
1591 | mvn CARG4, #~LJ_TISNUM
1592 | ldr PC, [BASE, FRAME_PC]
1593 | strd CARG12, [BASE, #-8]
1595 | strd CARG34, [BASE]
1601 | sub CARG1, BASE, #8
1602 | ldr PC, [BASE, FRAME_PC]
1611 | sub CARG3, BASE, #8
1612 | ldr PC, [BASE, FRAME_PC]
1617 | strd CARG12, [BASE]
1621 |.macro math_minmax, name, cond, fcond
1625 | checktp CARG2, LJ_TISNUM
1628 |1: // Handle integers.
1632 | checktp CARG4, LJ_TISNUM
1636 | mov..cond CARG1, CARG3
1638 |3: // Convert intermediate result to number and continue below.
1640 | bhi ->fff_fallback
1642 | vcvt.f64.s32 d0, s4
1647 | bhi ->fff_fallback
1648 |5: // Handle numbers.
1653 | checktp CARG4, LJ_TISNUM
1659 | vmov..fcond.f64 d0, d1
1661 |7: // Convert integer to number and continue above.
1663 | bhi ->fff_fallback
1664 | vcvt.f64.s32 d1, s4
1670 | checktp CARG2, LJ_TISNUM
1673 |1: // Handle integers.
1674 | ldrd CARG34, [BASE, RA]
1677 | checktp CARG4, LJ_TISNUM
1681 | mov..cond CARG1, CARG3
1683 |3: // Convert intermediate result to number and continue below.
1684 | bhi ->fff_fallback
1685 | bl extern __aeabi_i2d
1686 | ldrd CARG34, [BASE, RA]
1690 | bhi ->fff_fallback
1691 |5: // Handle numbers.
1692 | ldrd CARG34, [BASE, RA]
1695 | checktp CARG4, LJ_TISNUM
1698 | bl extern __aeabi_cdcmple
1700 | mov..fcond CARG1, CARG3
1701 | mov..fcond CARG2, CARG4
1703 |7: // Convert integer to number and continue above.
1704 | bhi ->fff_fallback
1707 | bl extern __aeabi_i2d
1713 | math_minmax math_min, gt, pl
1714 | math_minmax math_max, lt, le
1716 |//-- String library -----------------------------------------------------
1718 |.ffunc string_byte // Only handle the 1-arg case here.
1719 | ldrd CARG12, [BASE]
1720 | ldr PC, [BASE, FRAME_PC]
1722 | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument.
1723 | bne ->fff_fallback
1724 | ldr CARG3, STR:CARG1->len
1725 | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
1726 | mvn CARG2, #~LJ_TISNUM
1728 | moveq RC, #(0+1)*8
1729 | movne RC, #(1+1)*8
1730 | strd CARG12, [BASE, #-8]
1733 |.ffunc string_char // Only handle the 1-arg case here.
1735 | ldrd CARG12, [BASE]
1736 | ldr PC, [BASE, FRAME_PC]
1737 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1738 | checktpeq CARG2, LJ_TISNUM
1739 | bicseq CARG4, CARG1, #255
1741 | bne ->fff_fallback
1743 | mov CARG2, TMPDp // Points to stack. Little-endian.
1745 | // CARG2 = str, CARG3 = len.
1749 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1751 | // Returns GCstr *.
1753 | mvn CARG2, #~LJ_TSTR
1758 | ldrd CARG12, [BASE]
1759 | ldrd CARG34, [BASE, #16]
1760 | cmp NARGS8:RC, #16
1763 | blo ->fff_fallback
1764 | checktp CARG4, LJ_TISNUM
1766 | bne ->fff_fallback
1768 | ldrd CARG34, [BASE, #8]
1769 | checktp CARG2, LJ_TSTR
1770 | ldreq CARG2, STR:CARG1->len
1771 | checktpeq CARG4, LJ_TISNUM
1772 | bne ->fff_fallback
1773 | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1774 | add CARG4, CARG2, #1
1775 | cmp CARG3, #0 // if (start < 0) start += len+1
1776 | addlt CARG3, CARG3, CARG4
1777 | cmp CARG3, #1 // if (start < 1) start = 1
1779 | cmp RB, #0 // if (end < 0) end += len+1
1780 | addlt RB, RB, CARG4
1781 | bic RB, RB, RB, asr #31 // if (end < 0) end = 0
1782 | cmp RB, CARG2 // if (end > len) end = len
1783 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1785 | add CARG2, CARG1, CARG3
1786 | subs CARG3, RB, CARG3 // len = end - start
1787 | add CARG3, CARG3, #1 // len += 1
1790 | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1791 | mvn CARG2, #~LJ_TSTR
1794 |.macro ffstring_op, name
1795 | .ffunc string_ .. name
1797 | ldr CARG3, [BASE, #4]
1799 | ldr STR:CARG2, [BASE]
1800 | blo ->fff_fallback
1801 | sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf)
1802 | checkstr CARG3, ->fff_fallback
1803 | ldr CARG4, SBUF:CARG1->b
1806 | str L, SBUF:CARG1->L
1807 | str CARG4, SBUF:CARG1->w
1808 | bl extern lj_buf_putstr_ .. name
1809 | bl extern lj_buf_tostr
1813 |ffstring_op reverse
1817 |//-- Bit library --------------------------------------------------------
1819 |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1821 | bhi ->fff_fallback
1824 | adds RB, RB, #0x00200000
1825 | movpl CARG1, #0 // |x| < 1?
1828 | subs RB, CARG4, RB, asr #21
1829 | bmi >1 // |x| >= 2^32?
1830 | lsl CARG4, CARG2, #11
1831 | orr CARG4, CARG4, #0x80000000
1832 | orr CARG4, CARG4, CARG1, lsr #21
1834 | lsr CARG1, CARG4, RB
1835 | rsblt CARG1, CARG1, #0
1839 | lsr CARG4, CARG1, RB
1841 | lsl CARG1, CARG2, #12
1843 | orr CARG1, CARG4, CARG1, lsl RB
1844 | rsblt CARG1, CARG1, #0
1847 |.macro .ffunc_bit, name
1848 | .ffunc_1 bit_..name
1849 | checktp CARG2, LJ_TISNUM
1850 | blne ->vm_tobit_fb
1854 | mvn CARG2, #~LJ_TISNUM
1857 |.macro .ffunc_bit_op, name, ins
1862 | ldrd CARG12, [BASE, RA]
1866 | checktp CARG2, LJ_TISNUM
1867 | blne ->vm_tobit_fb
1868 | ins CARG3, CARG3, CARG1
1872 |.ffunc_bit_op band, and
1873 |.ffunc_bit_op bor, orr
1874 |.ffunc_bit_op bxor, eor
1877 | mvn CARG4, #~LJ_TISNUM
1878 | ldr PC, [BASE, FRAME_PC]
1879 | strd CARG34, [BASE, #-8]
1883 | eor CARG3, CARG1, CARG1, ror #16
1884 | bic CARG3, CARG3, #0x00ff0000
1885 | ror CARG1, CARG1, #8
1886 | mvn CARG2, #~LJ_TISNUM
1887 | eor CARG1, CARG1, CARG3, lsr #8
1892 | mvn CARG2, #~LJ_TISNUM
1895 |.macro .ffunc_bit_sh, name, ins, shmod
1897 | ldrd CARG12, [BASE, #8]
1898 | cmp NARGS8:RC, #16
1899 | blo ->fff_fallback
1900 | checktp CARG2, LJ_TISNUM
1901 | blne ->vm_tobit_fb
1903 | and RA, CARG1, #31
1907 | ldrd CARG12, [BASE]
1908 | checktp CARG2, LJ_TISNUM
1909 | blne ->vm_tobit_fb
1910 | ins CARG1, CARG1, RA
1911 | mvn CARG2, #~LJ_TISNUM
1915 |.ffunc_bit_sh lshift, lsl, 0
1916 |.ffunc_bit_sh rshift, lsr, 0
1917 |.ffunc_bit_sh arshift, asr, 0
1918 |.ffunc_bit_sh rol, ror, 1
1919 |.ffunc_bit_sh ror, ror, 0
1921 |//-----------------------------------------------------------------------
1923 |->fff_fallback: // Call fast function fallback handler.
1924 | // BASE = new base, RC = nargs*8
1925 | ldr CARG3, [BASE, FRAME_FUNC]
1926 | ldr CARG2, L->maxstack
1927 | add CARG1, BASE, NARGS8:RC
1928 | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC.
1930 | ldr CARG3, CFUNC:CARG3->f
1932 | add CARG1, CARG1, #8*LUA_MINSTACK
1933 | str PC, SAVE_PC // Redundant (but a defined value).
1936 | bhi >5 // Need to grow stack.
1937 | blx CARG3 // (lua_State *L)
1938 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1943 | bgt ->fff_res // Returned nresults+1?
1944 |1: // Returned 0 or -1: retry fast path.
1946 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1947 | sub NARGS8:RC, CARG1, BASE
1948 | bne ->vm_call_tail // Returned -1?
1949 | ins_callt // Returned 0: retry fast path.
1951 |// Reconstruct previous base for vmeta_call during tailcall.
1953 | ands CARG1, PC, #FRAME_TYPE
1954 | bic CARG2, PC, #FRAME_TYPEP
1955 | ldreq INS, [PC, #-4]
1956 | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8.
1957 | addeq CARG2, CARG2, #8
1958 | sub RB, BASE, CARG2
1959 | b ->vm_call_dispatch // Resolve again for tailcall.
1961 |5: // Grow stack for fallback handler.
1962 | mov CARG2, #LUA_MINSTACK
1963 | bl extern lj_state_growstack // (lua_State *L, int n)
1965 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1968 |->fff_gcstep: // Call GC step function.
1969 | // BASE = new base, RC = nargs*8
1972 | add CARG2, BASE, NARGS8:RC
1973 | str PC, SAVE_PC // Redundant (but a defined value).
1976 | bl extern lj_gc_step // (lua_State *L)
1978 | mov lr, RA // Help return address predictor.
1979 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1982 |//-----------------------------------------------------------------------
1983 |//-- Special dispatch targets -------------------------------------------
1984 |//-----------------------------------------------------------------------
1986 |->vm_record: // Dispatch target for recording phase.
1988 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
1989 | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent.
1991 | // Decrement the hookcount for consistency, but always do the call.
1992 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
1993 | tst CARG1, #HOOK_ACTIVE
1995 | sub CARG2, CARG2, #1
1996 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
1997 | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2001 |->vm_rethook: // Dispatch target for return hooks.
2002 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2003 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2005 |5: // Re-dispatch to static ins.
2007 | add OP, DISPATCH, OP, lsl #2
2008 | ldr pc, [OP, #GG_DISP2STATIC]
2010 |->vm_inshook: // Dispatch target for instr/line hooks.
2011 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2012 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2013 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2015 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2017 | subs CARG2, CARG2, #1
2018 | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2020 | tst CARG1, #LUA_MASKLINE
2026 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2027 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2030 |4: // Re-dispatch to static ins.
2031 | ldrb OP, [PC, #-4]
2032 | ldr INS, [PC, #-4]
2033 | add OP, DISPATCH, OP, lsl #2
2034 | ldr OP, [OP, #GG_DISP2STATIC]
2035 | decode_RA8 RA, INS
2039 |->cont_hook: // Continue from hook yield.
2040 | ldr CARG1, [CARG4, #-24]
2042 | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins.
2045 |->vm_hotloop: // Hot loop counter underflow.
2047 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L).
2048 | sub CARG1, DISPATCH, #-GG_DISP2J
2050 | ldr CARG3, LFUNC:CARG3->field_pc
2052 | str L, [DISPATCH, #DISPATCH_J(L)]
2053 | ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2055 | add CARG3, BASE, CARG3, lsl #3
2057 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2061 |->vm_callhook: // Dispatch target for call hooks.
2067 |->vm_hotcall: // Hot call counter underflow.
2072 | add CARG4, BASE, RC
2078 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2079 | // Returns ASMFunction.
2084 | sub NARGS8:RC, CARG4, BASE
2085 | str CARG2, SAVE_PC // Invalidate for subsequent line hook.
2086 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2087 | ldr INS, [PC, #-4]
2090 |->cont_stitch: // Trace stitching.
2092 | // RA = resultptr, CARG4 = meta base
2093 | ldr RB, SAVE_MULTRES
2094 | ldr INS, [PC, #-4]
2095 | ldr TRACE:CARG3, [CARG4, #-24] // Save previous trace.
2097 | decode_RA8 RC, INS // Call base.
2099 |1: // Move results down.
2103 | strd CARG12, [BASE, RC]
2107 | decode_RA8 RA, INS
2108 | decode_RB8 RB, INS
2112 | mvn CARG2, #~LJ_TNIL
2113 | bhi >9 // More results wanted?
2115 | ldrh RA, TRACE:CARG3->traceno
2116 | ldrh RC, TRACE:CARG3->link
2118 | beq ->cont_nop // Blacklisted.
2120 | bne =>BC_JLOOP // Jump to stitched trace.
2122 | // Stitch a new trace to the previous trace.
2123 | str RA, [DISPATCH, #DISPATCH_J(exitno)]
2124 | str L, [DISPATCH, #DISPATCH_J(L)]
2126 | sub CARG1, DISPATCH, #-GG_DISP2J
2128 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2132 |9: // Fill up results with nil.
2133 | strd CARG12, [BASE, RC]
2138 |->vm_profhook: // Dispatch target for profiler hook.
2143 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2144 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2150 |//-----------------------------------------------------------------------
2151 |//-- Trace exit handler -------------------------------------------------
2152 |//-----------------------------------------------------------------------
2157 | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2158 | ldr CARG1, [sp, #64] // Load original value of lr.
2159 | ldr DISPATCH, [lr] // Load DISPATCH.
2160 | add CARG3, sp, #64 // Recompute original value of sp.
2161 | mv_vmstate CARG4, EXIT
2162 | str CARG3, [sp, #52] // Store sp in RID_SP
2164 | ldr CARG2, [CARG1, #-4]! // Get exit instruction.
2165 | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC.
2166 | str CARG1, [sp, #60]
2170 | lsl CARG2, CARG2, #8
2171 | add CARG1, CARG1, CARG2, asr #6
2172 | ldr CARG2, [lr, #4] // Load exit stub group offset.
2173 | sub CARG1, CARG1, lr
2174 | ldr L, [DISPATCH, #DISPATCH_GL(cur_L)]
2175 | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number.
2176 | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2177 | str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2180 | str L, [DISPATCH, #DISPATCH_J(L)]
2181 | str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)]
2182 | sub CARG1, DISPATCH, #-GG_DISP2J
2184 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2185 | // Returns MULTRES (unscaled) or negated error code.
2186 | ldr CARG2, L->cframe
2188 | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2190 | ldr PC, SAVE_PC // Get SAVE_PC.
2191 | str L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2195 | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2199 | cmn CARG1, #LUA_ERRERR
2200 | bhs >9 // Check for error from exit.
2202 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2203 | str RC, SAVE_MULTRES
2206 | ldr CARG2, LFUNC:CARG2->field_pc
2207 | str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)]
2208 | mv_vmstate CARG4, INTERP
2209 | ldr KBASE, [CARG2, #PC2PROTO(k)]
2210 | // Modified copy of ins_next which handles function header dispatch, too.
2214 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2216 | cmn CARG1, #17 // Static dispatch?
2218 | cmp OP, #BC_FUNCC+2 // Fast function?
2221 | cmp OP, #BC_FUNCF // Function header?
2222 | ldr OP, [DISPATCH, OP, lsl #2]
2223 | decode_RA8 RA, INS
2224 | lsrlo RC, INS, #16 // No: Decode operands A*8 and D.
2226 | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8
2227 | ldrhs CARG3, [BASE, FRAME_FUNC]
2230 |4: // Check frame below fast function.
2231 | ldr CARG1, [BASE, FRAME_PC]
2232 | ands CARG2, CARG1, #FRAME_TYPE
2233 | bne <2 // Trace stitching continuation?
2234 | // Otherwise set KBASE for Lua function below fast function.
2235 | ldr CARG3, [CARG1, #-4]
2236 | decode_RA8 CARG1, CARG3
2237 | sub CARG2, BASE, CARG1
2238 | ldr LFUNC:CARG3, [CARG2, #-16]
2239 | ldr CARG3, LFUNC:CARG3->field_pc
2240 | ldr KBASE, [CARG3, #PC2PROTO(k)]
2243 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2244 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
2246 | ldr TRACE:CARG1, [CARG1, RC, lsl #2]
2247 | ldr INS, TRACE:CARG1->startins
2249 | decode_RA8 RA, INS
2250 | add OP, DISPATCH, OP, lsl #2
2252 | ldr pc, [OP, #GG_DISP2STATIC]
2254 |9: // Rethrow error from the right C frame.
2255 | rsb CARG2, CARG1, #0
2257 | bl extern lj_err_trace // (lua_State *L, int errcode)
2260 |//-----------------------------------------------------------------------
2261 |//-- Math helper functions ----------------------------------------------
2262 |//-----------------------------------------------------------------------
2264 |// FP value rounding. Called from JIT code.
2266 |// double lj_vm_floor/ceil/trunc(double x);
2267 |.macro vm_round, func, hf
2269 | vmov CARG1, CARG2, d0
2271 | lsl CARG3, CARG2, #1
2272 | adds RB, CARG3, #0x00200000
2273 | bpl >2 // |x| < 1?
2275 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2276 | bxlo lr // |x| >= 2^52: done.
2278 | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask
2279 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2281 | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2282 | orrpl CARG3, CARG3, CARG4
2284 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2285 |.if "func" == "floor"
2286 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2288 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2291 | vmoveq d0, CARG1, CARG2
2293 | bxeq lr // iszero: done.
2296 | lslpl CARG3, CARG4, RB
2299 | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask
2300 | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry
2302 | vmov d0, CARG1, CARG2
2307 | bxcs lr // |x| is not finite.
2308 | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo
2309 |.if "func" == "floor"
2310 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2312 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2314 | mov CARG1, #0 // lo = 0
2315 | and CARG2, CARG2, #0x80000000
2316 | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2317 | orrne CARG2, CARG2, CARG4
2319 | vmov d0, CARG1, CARG2
2325 | .long 0x3ff00000 // hiword(+1.0)
2341 |.macro vm_trunc, hf
2344 | vmov CARG1, CARG2, d0
2346 | lsl CARG3, CARG2, #1
2347 | adds RB, CARG3, #0x00200000
2348 | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0.
2351 | vmovpl d0, CARG1, CARG2
2355 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2356 | bxlo lr // |x| >= 2^52: already done.
2358 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2360 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2362 | vmov d0, CARG1, CARG2
2375 | // double lj_vm_mod(double dividend, double divisor);
2378 | // Special calling convention. Also, RC (r11) is not preserved.
2379 | vdiv.f64 d0, d6, d7
2381 | vmov CARG1, CARG2, d0
2383 | vmov d0, CARG1, CARG2
2384 | vmul.f64 d0, d0, d7
2386 | vsub.f64 d6, d6, d0
2389 | push {r0, r1, r2, r3, r4, lr}
2390 | bl extern __aeabi_ddiv
2392 | ldrd CARG34, [sp, #8]
2393 | bl extern __aeabi_dmul
2395 | eor CARG2, CARG2, #0x80000000
2396 | bl extern __aeabi_dadd
2401 | // int lj_vm_modi(int dividend, int divisor);
2403 | ands RB, CARG1, #0x80000000
2404 | rsbmi CARG1, CARG1, #0 // a = |dividend|
2405 | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor).
2407 | rsbmi CARG2, CARG2, #0 // b = |divisor|
2408 | subs CARG4, CARG2, #1
2409 | cmpne CARG1, CARG2
2410 | moveq CARG1, #0 // if (b == 1 || a == b) a = 0
2411 | tsthi CARG2, CARG4
2412 | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1
2414 | // Use repeated subtraction to get the remainder.
2417 | sub CARG4, CARG4, CARG3
2418 | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8
2419 | addne pc, pc, CARG3, lsl #3 // Duff's device.
2423 for (i = 31; i >= 0; i--) {
2424 | cmp CARG1, CARG2, lsl #i
2425 | subhs CARG1, CARG1, CARG2, lsl #i
2431 | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b
2432 | eors CARG2, CARG1, RB, lsl #1
2433 | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y
2436 |//-----------------------------------------------------------------------
2437 |//-- Miscellaneous functions --------------------------------------------
2438 |//-----------------------------------------------------------------------
2440 |.define NEXT_TAB, TAB:CARG1
2441 |.define NEXT_RES, CARG1
2442 |.define NEXT_IDX, CARG2
2443 |.define NEXT_TMP0, CARG3
2444 |.define NEXT_TMP1, CARG4
2445 |.define NEXT_LIM, r12
2446 |.define NEXT_RES_PTR, sp
2447 |.define NEXT_RES_VAL, [sp]
2448 |.define NEXT_RES_KEY_I, [sp, #8]
2449 |.define NEXT_RES_KEY_IT, [sp, #12]
2451 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2452 |// Next idx returned in CRET2.
2455 | ldr NEXT_TMP0, NEXT_TAB->array
2456 | ldr NEXT_LIM, NEXT_TAB->asize
2457 | add NEXT_TMP0, NEXT_TMP0, NEXT_IDX, lsl #3
2458 |1: // Traverse array part.
2459 | subs NEXT_TMP1, NEXT_IDX, NEXT_LIM
2461 | ldr NEXT_TMP1, [NEXT_TMP0, #4]
2462 | str NEXT_IDX, NEXT_RES_KEY_I
2463 | add NEXT_TMP0, NEXT_TMP0, #8
2464 | add NEXT_IDX, NEXT_IDX, #1
2465 | checktp NEXT_TMP1, LJ_TNIL
2466 | beq <1 // Skip holes in array part.
2467 | ldr NEXT_TMP0, [NEXT_TMP0, #-8]
2468 | mov NEXT_RES, NEXT_RES_PTR
2469 | strd NEXT_TMP0, NEXT_RES_VAL // Stores NEXT_TMP1, too.
2470 | mvn NEXT_TMP0, #~LJ_TISNUM
2471 | str NEXT_TMP0, NEXT_RES_KEY_IT
2474 |5: // Traverse hash part.
2475 | ldr NEXT_TMP0, NEXT_TAB->hmask
2476 | ldr NODE:NEXT_RES, NEXT_TAB->node
2477 | add NEXT_TMP1, NEXT_TMP1, NEXT_TMP1, lsl #1
2478 | add NEXT_LIM, NEXT_LIM, NEXT_TMP0
2479 | add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP1, lsl #3
2481 | cmp NEXT_IDX, NEXT_LIM
2483 | ldr NEXT_TMP1, NODE:NEXT_RES->val.it
2484 | checktp NEXT_TMP1, LJ_TNIL
2485 | add NEXT_IDX, NEXT_IDX, #1
2487 | // Skip holes in hash part.
2488 | add NEXT_RES, NEXT_RES, #sizeof(Node)
2491 |9: // End of iteration. Set the key to nil (not the value).
2493 | mov NEXT_RES, NEXT_RES_PTR
2494 | str NEXT_TMP0, NEXT_RES_KEY_IT
2498 |//-----------------------------------------------------------------------
2499 |//-- FFI helper functions -----------------------------------------------
2500 |//-----------------------------------------------------------------------
2502 |// Handler for callback functions.
2503 |// Saveregs already performed. Callback slot number in [sp], g in r12.
2506 |.type CTSTATE, CTState, PC
2507 | ldr CTSTATE, GL:r12->ctype_state
2508 | add DISPATCH, r12, #GG_G2DISP
2511 | add r4, sp, CFRAME_SPACE+4+8*8
2512 | vstmdb r4!, {d8-d15}
2515 | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2517 | strd CARG34, CTSTATE->cb.gpr[2]
2518 | strd CARG12, CTSTATE->cb.gpr[0]
2520 | vstmdb r12!, {d0-d7}
2523 | add CARG3, sp, #CFRAME_SIZE
2524 | mov CARG1, CTSTATE
2525 | lsr CARG4, CARG4, #3
2526 | str CARG3, CTSTATE->cb.stack
2528 | str CARG4, CTSTATE->cb.slot
2529 | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2530 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2531 | // Returns lua_State *.
2532 | ldr BASE, L:CRET1->base
2533 | mv_vmstate CARG2, INTERP
2534 | ldr RC, L:CRET1->top
2536 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2539 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2544 |->cont_ffi_callback: // Return from FFI callback.
2546 | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2550 | mov CARG1, CTSTATE
2552 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2553 | ldrd CARG12, CTSTATE->cb.gpr[0]
2555 | vldr d0, CTSTATE->cb.fpr[0]
2560 |->vm_ffi_call: // Call C function via FFI.
2561 | // Caveat: needs special frame unwinding, see below.
2563 | .type CCSTATE, CCallState, r4
2564 | push {CCSTATE, r5, r11, lr}
2565 | mov CCSTATE, CARG1
2566 | ldr CARG1, CCSTATE:CARG1->spadj
2567 | ldrb CARG2, CCSTATE->nsp
2568 | add CARG3, CCSTATE, #offsetof(CCallState, stack)
2570 | add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2573 | sub sp, sp, CARG1 // Readjust stack.
2574 | subs CARG2, CARG2, #4
2578 | ldr RB, CCSTATE->func
2580 |1: // Copy stack slots.
2581 | ldr CARG4, [CARG3, CARG2]
2582 | str CARG4, [sp, CARG2]
2583 | subs CARG2, CARG2, #4
2586 | ldrd CARG12, CCSTATE->gpr[0]
2587 | ldrd CARG34, CCSTATE->gpr[2]
2591 | add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2593 | strd CRET1, CCSTATE->gpr[0]
2595 | vstmdb r12!, {d0-d3}
2597 | pop {CCSTATE, r5, r11, pc}
2599 |// Note: vm_ffi_call must be the last function in this object file!
2601 |//-----------------------------------------------------------------------
2604 /* Generate the code for a single instruction. */
2605 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2612 /* -- Comparison ops ---------------------------------------------------- */
2614 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2616 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2617 | // RA = src1*8, RC = src2, JMP with RC = target
2619 | ldrd CARG12, [RA, BASE]!
2621 | ldrd CARG34, [RC, BASE]!
2623 | add RB, PC, RB, lsl #2
2624 | checktp CARG2, LJ_TISNUM
2626 | checktp CARG4, LJ_TISNUM
2629 if (op == BC_ISLT) {
2630 | sublt PC, RB, #0x20000
2631 } else if (op == BC_ISGE) {
2632 | subge PC, RB, #0x20000
2633 } else if (op == BC_ISLE) {
2634 | suble PC, RB, #0x20000
2636 | subgt PC, RB, #0x20000
2641 |3: // CARG12 is not an integer.
2645 | // d0 is a number.
2646 | checktp CARG4, LJ_TISNUM
2650 | // d0 is a number, CARG3 is an integer.
2652 | vcvt.f64.s32 d1, s4
2654 |4: // CARG1 is an integer, CARG34 is not an integer.
2657 | // CARG1 is an integer, d1 is a number.
2659 | vcvt.f64.s32 d0, s4
2660 |5: // d0 and d1 are numbers.
2663 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2664 if (op == BC_ISLT) {
2665 | sublo PC, RB, #0x20000
2666 } else if (op == BC_ISGE) {
2667 | subhs PC, RB, #0x20000
2668 } else if (op == BC_ISLE) {
2669 | subls PC, RB, #0x20000
2671 | subhi PC, RB, #0x20000
2676 | // CARG12 is a number.
2677 | checktp CARG4, LJ_TISNUM
2678 | movlo RA, RB // Save RB.
2681 | // CARG12 is a number, CARG3 is an integer.
2684 | mov RA, RB // Save RB.
2685 | bl extern __aeabi_i2d
2688 | ldrd CARG12, [RC] // Restore first operand.
2690 |4: // CARG1 is an integer, CARG34 is not an integer.
2692 | // CARG1 is an integer, CARG34 is a number.
2693 | mov RA, RB // Save RB.
2694 | bl extern __aeabi_i2d
2695 | ldrd CARG34, [RC] // Restore second operand.
2696 |5: // CARG12 and CARG34 are numbers.
2697 | bl extern __aeabi_cdcmple
2698 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2699 if (op == BC_ISLT) {
2700 | sublo PC, RA, #0x20000
2701 } else if (op == BC_ISGE) {
2702 | subhs PC, RA, #0x20000
2703 } else if (op == BC_ISLE) {
2704 | subls PC, RA, #0x20000
2706 | subhi PC, RA, #0x20000
2712 case BC_ISEQV: case BC_ISNEV:
2713 vk = op == BC_ISEQV;
2714 | // RA = src1*8, RC = src2, JMP with RC = target
2716 | ldrd CARG12, [RA, BASE]!
2718 | ldrd CARG34, [RC, BASE]!
2720 | add RB, PC, RB, lsl #2
2721 | checktp CARG2, LJ_TISNUM
2722 | cmnls CARG4, #-LJ_TISNUM
2728 | // Either or both types are not numbers.
2730 | checktp CARG2, LJ_TCDATA
2731 | checktpne CARG4, LJ_TCDATA
2732 | beq ->vmeta_equal_cd
2734 | cmp CARG2, CARG4 // Compare types.
2735 | bne >2 // Not the same type?
2736 | checktp CARG2, LJ_TISPRI
2737 | bhs >1 // Same type and primitive type?
2739 | // Same types and not a primitive type. Compare GCobj or pvalue.
2742 | bne >3 // Different GCobjs or pvalues?
2743 |1: // Branch if same.
2744 | sub PC, RB, #0x20000
2748 | checktp CARG2, LJ_TISTABUD
2749 | bhi <2 // Different objects and not table/ud?
2751 | beq >1 // Same GCobjs or pvalues?
2752 | checktp CARG2, LJ_TISTABUD
2753 | bhi >2 // Different objects and not table/ud?
2755 | // Different tables or userdatas. Need to check __eq metamethod.
2756 | // Field metatable must be at same offset for GCtab and GCudata!
2757 | ldr TAB:RA, TAB:CARG1->metatable
2760 | beq <2 // No metatable?
2762 | beq >2 // No metatable?
2764 | ldrb RA, TAB:RA->nomm
2765 | mov CARG4, #1-vk // ne = 0 or 1.
2768 | beq ->vmeta_equal // 'no __eq' flag not set?
2772 |2: // Branch if different.
2773 | sub PC, RB, #0x20000
2779 case BC_ISEQS: case BC_ISNES:
2780 vk = op == BC_ISEQS;
2781 | // RA = src*8, RC = str_const (~), JMP with RC = target
2783 | ldrd CARG12, [BASE, RA]
2785 | ldr STR:CARG3, [KBASE, RC, lsl #2]
2787 | add RB, PC, RB, lsl #2
2788 | checktp CARG2, LJ_TSTR
2793 | cmpeq CARG1, CARG3
2796 | subeq PC, RB, #0x20000
2800 | subne PC, RB, #0x20000
2806 | checktp CARG2, LJ_TCDATA
2808 | b ->vmeta_equal_cd
2812 case BC_ISEQN: case BC_ISNEN:
2813 vk = op == BC_ISEQN;
2814 | // RA = src*8, RC = num_const (~), JMP with RC = target
2816 | ldrd CARG12, [RA, BASE]!
2818 | ldrd CARG34, [RC, KBASE]!
2820 | add RB, PC, RB, lsl #2
2826 | checktp CARG2, LJ_TISNUM
2828 | checktp CARG4, LJ_TISNUM
2832 | subeq PC, RB, #0x20000
2836 | subne PC, RB, #0x20000
2841 |3: // CARG12 is not an integer.
2846 | subhi PC, RB, #0x20000
2851 | checktp CARG4, LJ_TISNUM
2855 | vcvths.f64.s32 d1, s4
2857 |4: // CARG1 is an integer, d1 is a number.
2860 | vcvt.f64.s32 d0, s4
2861 |5: // d0 and d1 are numbers.
2865 | subeq PC, RB, #0x20000
2867 | subne PC, RB, #0x20000
2871 | // CARG12 is a number.
2872 | checktp CARG4, LJ_TISNUM
2873 | movlo RA, RB // Save RB.
2875 | // CARG12 is a number, CARG3 is an integer.
2878 |4: // CARG1 is an integer, CARG34 is a number.
2879 | mov RA, RB // Save RB.
2880 | bl extern __aeabi_i2d
2881 | ldrd CARG34, [RC] // Restore other operand.
2882 |5: // CARG12 and CARG34 are numbers.
2883 | bl extern __aeabi_cdcmpeq
2885 | subeq PC, RA, #0x20000
2887 | subne PC, RA, #0x20000
2894 | checktp CARG2, LJ_TCDATA
2896 | b ->vmeta_equal_cd
2900 case BC_ISEQP: case BC_ISNEP:
2901 vk = op == BC_ISEQP;
2902 | // RA = src*8, RC = primitive_type (~), JMP with RC = target
2903 | ldrd CARG12, [BASE, RA]
2907 | add RB, PC, RB, lsl #2
2909 | checktp CARG2, LJ_TCDATA
2910 | beq ->vmeta_equal_cd
2914 | subeq PC, RB, #0x20000
2916 | subne PC, RB, #0x20000
2921 /* -- Unary test and copy ops ------------------------------------------- */
2923 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2924 | // RA = dst*8 or unused, RC = src, JMP with RC = target
2925 | add RC, BASE, RC, lsl #3
2929 | add RB, PC, RB, lsl #2
2930 | checktp CARG2, LJ_TTRUE
2931 if (op == BC_ISTC || op == BC_IST) {
2932 | subls PC, RB, #0x20000
2933 if (op == BC_ISTC) {
2934 | strdls CARG12, [BASE, RA]
2937 | subhi PC, RB, #0x20000
2938 if (op == BC_ISFC) {
2939 | strdhi CARG12, [BASE, RA]
2946 | // RA = src*8, RC = -type
2947 | ldrd CARG12, [BASE, RA]
2951 | bne ->vmeta_istype
2955 | // RA = src*8, RC = -(TISNUM-1)
2956 | ldrd CARG12, [BASE, RA]
2958 | checktp CARG2, LJ_TISNUM
2960 | bhs ->vmeta_istype
2964 /* -- Unary ops --------------------------------------------------------- */
2967 | // RA = dst*8, RC = src
2970 | ldrd CARG12, [BASE, RC]
2972 | strd CARG12, [BASE, RA]
2976 | // RA = dst*8, RC = src
2977 | add RC, BASE, RC, lsl #3
2979 | ldr CARG1, [RC, #4]
2982 | checktp CARG1, LJ_TTRUE
2983 | mvnls CARG2, #~LJ_TFALSE
2984 | mvnhi CARG2, #~LJ_TTRUE
2985 | str CARG2, [RA, #4]
2989 | // RA = dst*8, RC = src
2991 | ldrd CARG12, [BASE, RC]
2994 | checktp CARG2, LJ_TISNUM
2996 | eorne CARG2, CARG2, #0x80000000
2998 | rsbseq CARG1, CARG1, #0
3001 | strd CARG12, [BASE, RA]
3006 | .long 0x00000000, 0x41e00000 // 2^31.
3009 | // RA = dst*8, RC = src
3011 | ldrd CARG12, [BASE, RC]
3012 | checkstr CARG2, >2
3013 | ldr CARG1, STR:CARG1->len
3015 | mvn CARG2, #~LJ_TISNUM
3018 | strd CARG12, [BASE, RA]
3021 | checktab CARG2, ->vmeta_len
3023 | ldr TAB:CARG3, TAB:CARG1->metatable
3030 | bl extern lj_tab_len // (GCtab *t)
3031 | // Returns uint32_t (but less than 2^31).
3036 | ldrb CARG4, TAB:CARG3->nomm
3037 | tst CARG4, #1<<MM_len
3038 | bne <3 // 'no __len' flag set: done.
3043 /* -- Binary ops -------------------------------------------------------- */
3045 |.macro ins_arithcheck, cond, ncond, target
3047 | cmn CARG4, #-LJ_TISNUM
3048 | cmn..cond CARG2, #-LJ_TISNUM
3050 | cmn CARG2, #-LJ_TISNUM
3051 | cmn..cond CARG4, #-LJ_TISNUM
3055 |.macro ins_arithcheck_int, target
3056 | ins_arithcheck eq, ne, target
3058 |.macro ins_arithcheck_num, target
3059 | ins_arithcheck lo, hs, target
3062 |.macro ins_arithpre
3063 | decode_RB8 RB, INS
3064 | decode_RC8 RC, INS
3065 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3066 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3070 | ldrd CARG12, [RB, BASE]!
3071 | ldrd CARG34, [RC, KBASE]!
3073 | ldrd CARG12, [BASE, RB]
3074 | ldrd CARG34, [KBASE, RC]
3079 | ldrd CARG34, [RB, BASE]!
3080 | ldrd CARG12, [RC, KBASE]!
3082 | ldrd CARG34, [BASE, RB]
3083 | ldrd CARG12, [KBASE, RC]
3088 | ldrd CARG12, [RB, BASE]!
3089 | ldrd CARG34, [RC, BASE]!
3091 | ldrd CARG12, [BASE, RB]
3092 | ldrd CARG34, [BASE, RC]
3098 |.macro ins_arithpre_fpu, reg1, reg2
3110 |.macro ins_arithpost_fpu, reg
3118 |.macro ins_arithfallback, ins
3121 | ins ->vmeta_arith_vn
3124 | ins ->vmeta_arith_nv
3127 | ins ->vmeta_arith_vv
3132 |.macro ins_arithdn, intins, fpins, fpcall
3134 |.if "intins" ~= "vm_modi" and not FPU
3137 | ins_arithcheck_int >5
3138 |.if "intins" == "smull"
3139 | smull CARG1, RC, CARG3, CARG1
3140 | cmp RC, CARG1, asr #31
3141 | ins_arithfallback bne
3142 |.elif "intins" == "vm_modi"
3144 | ins_arithfallback beq
3146 | mvn CARG2, #~LJ_TISNUM
3148 | intins CARG1, CARG1, CARG3
3149 | ins_arithfallback bvs
3152 |.if "intins" == "vm_modi" or FPU
3156 | strd CARG12, [BASE, RA]
3159 | ins_arithpre_fpu d6, d7
3160 | ins_arithfallback ins_arithcheck_num
3162 |.if "intins" == "vm_modi"
3167 | ins_arithpost_fpu d6
3170 |.if "intins" ~= "vm_modi"
3177 |.macro ins_arithfp, fpins, fpcall
3179 |.if "fpins" ~= "extern" or HFABI
3180 | ins_arithpre_fpu d0, d1
3182 | ins_arithfallback ins_arithcheck_num
3183 |.if "fpins" == "extern"
3192 |.if ("fpins" ~= "extern" or HFABI) and FPU
3193 | ins_arithpost_fpu d0
3197 | strd CARG12, [BASE, RA]
3202 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3203 | ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3205 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3206 | ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3208 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3209 | ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3211 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3212 | ins_arithfp vdiv.f64, extern __aeabi_ddiv
3214 case BC_MODVN: case BC_MODNV: case BC_MODVV:
3215 | ins_arithdn vm_modi, vm_mod, ->vm_mod
3218 | // NYI: (partial) integer arithmetic.
3219 | ins_arithfp extern, extern pow
3223 | decode_RB8 RC, INS
3224 | decode_RC8 RB, INS
3225 | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!)
3228 | add CARG2, BASE, RB
3230 | // RA = dst*8, RC = src_start*8, CARG2 = top-1
3233 | lsr CARG3, CARG3, #3
3234 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3235 | // Returns NULL (finished) or TValue * (metamethod).
3239 | ldrd CARG34, [BASE, RC]
3242 | strd CARG34, [BASE, RA] // Copy result to RA.
3246 /* -- Constant ops ------------------------------------------------------ */
3249 | // RA = dst*8, RC = str_const (~)
3252 | ldr CARG1, [KBASE, RC, lsl #2]
3253 | mvn CARG2, #~LJ_TSTR
3255 | strd CARG12, [BASE, RA]
3260 | // RA = dst*8, RC = cdata_const (~)
3263 | ldr CARG1, [KBASE, RC, lsl #2]
3264 | mvn CARG2, #~LJ_TCDATA
3266 | strd CARG12, [BASE, RA]
3271 | // RA = dst*8, (RC = int16_literal)
3272 | mov CARG1, INS, asr #16 // Refetch sign-extended reg.
3273 | mvn CARG2, #~LJ_TISNUM
3276 | strd CARG12, [BASE, RA]
3280 | // RA = dst*8, RC = num_const
3283 | ldrd CARG12, [KBASE, RC]
3285 | strd CARG12, [BASE, RA]
3289 | // RA = dst*8, RC = primitive_type (~)
3298 | // RA = base*8, RC = end
3300 | add RC, BASE, RC, lsl #3
3301 | mvn CARG1, #~LJ_TNIL
3302 | str CARG1, [RA, #4]
3305 | str CARG1, [RA, #4]
3312 /* -- Upvalue and function ops ------------------------------------------ */
3315 | // RA = dst*8, RC = uvnum
3316 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3318 | add RC, RC, #offsetof(GCfuncL, uvptr)
3319 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3320 | ldr CARG2, UPVAL:CARG2->v
3321 | ldrd CARG34, [CARG2]
3324 | strd CARG34, [BASE, RA]
3328 | // RA = uvnum*8, RC = src
3329 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3331 | add RA, RA, #offsetof(GCfuncL, uvptr)
3333 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3334 | ldrd CARG34, [BASE, RC]
3335 | ldrb RB, UPVAL:CARG2->marked
3336 | ldrb RC, UPVAL:CARG2->closed
3337 | ldr CARG2, UPVAL:CARG2->v
3338 | tst RB, #LJ_GC_BLACK // isblack(uv)
3339 | add RB, CARG4, #-LJ_TISGCV
3341 | strd CARG34, [CARG2]
3342 | bne >2 // Upvalue is closed and black?
3346 |2: // Check if new value is collectable.
3347 | cmn RB, #-(LJ_TNUMX - LJ_TISGCV)
3348 | ldrbhi RC, GCOBJ:CARG3->gch.marked
3349 | bls <1 // tvisgcv(v)
3350 | sub CARG1, DISPATCH, #-GG_DISP2G
3351 | tst RC, #LJ_GC_WHITES
3352 | // Crossed a write barrier. Move the barrier forward.
3356 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3359 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3364 | // RA = uvnum*8, RC = str_const (~)
3365 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3367 | add RA, RA, #offsetof(GCfuncL, uvptr)
3369 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3370 | ldr STR:CARG3, [KBASE, RC, lsl #2]
3371 | ldrb RB, UPVAL:CARG2->marked
3372 | ldrb RC, UPVAL:CARG2->closed
3373 | ldr CARG2, UPVAL:CARG2->v
3374 | mvn CARG4, #~LJ_TSTR
3375 | tst RB, #LJ_GC_BLACK // isblack(uv)
3376 | ldrb RB, STR:CARG3->marked
3377 | strd CARG34, [CARG2]
3382 |2: // Check if string is white and ensure upvalue is closed.
3383 | tst RB, #LJ_GC_WHITES // iswhite(str)
3385 | sub CARG1, DISPATCH, #-GG_DISP2G
3386 | // Crossed a write barrier. Move the barrier forward.
3390 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3393 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3398 | // RA = uvnum*8, RC = num_const
3399 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3401 | add RA, RA, #offsetof(GCfuncL, uvptr)
3403 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3404 | ldrd CARG34, [KBASE, RC]
3405 | ldr CARG2, UPVAL:CARG2->v
3408 | strd CARG34, [CARG2]
3412 | // RA = uvnum*8, RC = primitive_type (~)
3413 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3415 | add RA, RA, #offsetof(GCfuncL, uvptr)
3416 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3418 | ldr CARG2, UPVAL:CARG2->v
3421 | str RC, [CARG2, #4]
3426 | // RA = level*8, RC = target
3427 | ldr CARG3, L->openupval
3428 | add RC, PC, RC, lsl #2
3431 | sub PC, RC, #0x20000
3434 | add CARG2, BASE, RA
3435 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3442 | // RA = dst*8, RC = proto_const (~) (holding function prototype)
3445 | ldr CARG2, [KBASE, RC, lsl #2]
3447 | ldr CARG3, [BASE, FRAME_FUNC]
3449 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3450 | bl extern lj_func_newL_gc
3451 | // Returns GCfuncL *.
3453 | mvn CARG2, #~LJ_TFUNC
3456 | strd CARG12, [BASE, RA]
3460 /* -- Table ops --------------------------------------------------------- */
3464 | // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3465 if (op == BC_TDUP) {
3468 | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3469 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3476 if (op == BC_TNEW) {
3477 | lsl CARG2, RC, #21
3478 | lsr CARG3, RC, #11
3479 | asr RC, CARG2, #21
3480 | lsr CARG2, CARG2, #21
3482 | addeq CARG2, CARG2, #2
3483 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3484 | // Returns GCtab *.
3486 | ldr CARG2, [KBASE, RC, lsl #2]
3487 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3488 | // Returns GCtab *.
3491 | mvn CARG2, #~LJ_TTAB
3494 | strd CARG12, [BASE, RA]
3497 | bl extern lj_gc_step_fixtop // (lua_State *L)
3503 | // RA = dst*8, RC = str_const (~)
3505 | // RA = dst*8, RC = str_const (~)
3506 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3508 | ldr TAB:CARG1, LFUNC:CARG2->env
3509 | ldr STR:RC, [KBASE, RC, lsl #2]
3510 if (op == BC_GGET) {
3518 | decode_RB8 RB, INS
3519 | decode_RC8 RC, INS
3520 | // RA = dst*8, RB = table*8, RC = key*8
3521 | ldrd TAB:CARG12, [BASE, RB]
3522 | ldrd CARG34, [BASE, RC]
3523 | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12.
3524 | checktp CARG4, LJ_TISNUM // Integer key?
3525 | ldreq CARG4, TAB:CARG1->array
3526 | ldreq CARG2, TAB:CARG1->asize
3529 | add CARG4, CARG4, CARG3, lsl #3
3530 | cmp CARG3, CARG2 // In array part?
3531 | ldrdlo CARG34, [CARG4]
3533 | ins_next1 // Overwrites RB!
3534 | checktp CARG4, LJ_TNIL
3538 | strd CARG34, [BASE, RA]
3541 |5: // Check for __index if table value is nil.
3542 | ldr TAB:CARG2, TAB:CARG1->metatable
3544 | beq <1 // No metatable: done.
3545 | ldrb CARG2, TAB:CARG2->nomm
3546 | tst CARG2, #1<<MM_index
3547 | bne <1 // 'no __index' flag set: done.
3548 | decode_RB8 RB, INS // Restore RB.
3552 | checktp CARG4, LJ_TSTR // String key?
3553 | moveq STR:RC, CARG3
3558 | decode_RB8 RB, INS
3560 | // RA = dst*8, RB = table*8, RC = str_const (~)
3561 | ldrd CARG12, [BASE, RB]
3563 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3564 | checktab CARG2, ->vmeta_tgets1
3566 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3567 | ldr CARG3, TAB:CARG1->hmask
3568 | ldr CARG4, STR:RC->sid
3569 | ldr NODE:INS, TAB:CARG1->node
3570 | mov TAB:RB, TAB:CARG1
3571 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3572 | add CARG3, CARG3, CARG3, lsl #1
3573 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3575 | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS.
3576 | ldrd CARG34, NODE:INS->val
3577 | ldr NODE:INS, NODE:INS->next
3578 | checktp CARG2, LJ_TSTR
3579 | cmpeq CARG1, STR:RC
3581 | checktp CARG4, LJ_TNIL
3586 | strd CARG34, [BASE, RA]
3589 |4: // Follow hash chain.
3592 | // End of hash chain: key not found, nil result.
3594 |5: // Check for __index if table value is nil.
3595 | ldr TAB:CARG1, TAB:RB->metatable
3596 | mov CARG3, #0 // Optional clear of undef. value (during load stall).
3597 | mvn CARG4, #~LJ_TNIL
3599 | beq <3 // No metatable: done.
3600 | ldrb CARG2, TAB:CARG1->nomm
3601 | tst CARG2, #1<<MM_index
3602 | bne <3 // 'no __index' flag set: done.
3606 | decode_RB8 RB, INS
3608 | // RA = dst*8, RB = table*8, RC = index
3609 | ldrd CARG12, [BASE, RB]
3610 | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12.
3611 | ldr CARG3, TAB:CARG1->asize
3612 | ldr CARG4, TAB:CARG1->array
3615 | ldrdlo CARG34, [CARG4, CARG2]
3617 | ins_next1 // Overwrites RB!
3618 | checktp CARG4, LJ_TNIL
3622 | strd CARG34, [BASE, RA]
3625 |5: // Check for __index if table value is nil.
3626 | ldr TAB:CARG2, TAB:CARG1->metatable
3628 | beq <1 // No metatable: done.
3629 | ldrb CARG2, TAB:CARG2->nomm
3630 | tst CARG2, #1<<MM_index
3631 | bne <1 // 'no __index' flag set: done.
3635 | decode_RB8 RB, INS
3636 | decode_RC8 RC, INS
3637 | // RA = dst*8, RB = table*8, RC = key*8
3638 | ldr TAB:CARG1, [BASE, RB]
3639 | ldr CARG2, [BASE, RC]
3640 | ldr CARG4, TAB:CARG1->array
3641 | ldr CARG3, TAB:CARG1->asize
3642 | add CARG4, CARG4, CARG2, lsl #3
3643 | cmp CARG2, CARG3 // In array part?
3645 | ldrd CARG12, [CARG4]
3649 | strd CARG12, [BASE, RA]
3654 | decode_RB8 RB, INS
3655 | decode_RC8 RC, INS
3656 | // RA = src*8, RB = table*8, RC = key*8
3657 | ldrd TAB:CARG12, [BASE, RB]
3658 | ldrd CARG34, [BASE, RC]
3659 | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12.
3660 | checktp CARG4, LJ_TISNUM // Integer key?
3661 | ldreq CARG2, TAB:CARG1->array
3662 | ldreq CARG4, TAB:CARG1->asize
3665 | add CARG2, CARG2, CARG3, lsl #3
3666 | cmp CARG3, CARG4 // In array part?
3667 | ldrlo INS, [CARG2, #4]
3669 | ins_next1 // Overwrites RB!
3670 | checktp INS, LJ_TNIL
3671 | ldrb INS, TAB:CARG1->marked
3672 | ldrd CARG34, [BASE, RA]
3675 | tst INS, #LJ_GC_BLACK // isblack(table)
3676 | strd CARG34, [CARG2]
3682 |5: // Check for __newindex if previous value is nil.
3683 | ldr TAB:RA, TAB:CARG1->metatable
3685 | beq <1 // No metatable: done.
3686 | ldrb RA, TAB:RA->nomm
3687 | tst RA, #1<<MM_newindex
3688 | bne <1 // 'no __newindex' flag set: done.
3689 | ldr INS, [PC, #-4] // Restore RA and RB.
3690 | decode_RB8 RB, INS
3691 | decode_RA8 RA, INS
3694 |7: // Possible table write barrier for the value. Skip valiswhite check.
3695 | barrierback TAB:CARG1, INS, CARG3
3699 | checktp CARG4, LJ_TSTR // String key?
3700 | moveq STR:RC, CARG3
3705 | decode_RB8 RB, INS
3707 | // RA = src*8, RB = table*8, RC = str_const (~)
3708 | ldrd CARG12, [BASE, RB]
3710 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3711 | checktab CARG2, ->vmeta_tsets1
3713 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3714 | ldr CARG3, TAB:CARG1->hmask
3715 | ldr CARG4, STR:RC->sid
3716 | ldr NODE:INS, TAB:CARG1->node
3717 | mov TAB:RB, TAB:CARG1
3718 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3719 | add CARG3, CARG3, CARG3, lsl #1
3721 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3722 | strb CARG4, TAB:RB->nomm // Clear metamethod cache.
3724 | ldrd CARG12, NODE:INS->key
3725 | ldr CARG4, NODE:INS->val.it
3726 | ldr NODE:CARG3, NODE:INS->next
3727 | checktp CARG2, LJ_TSTR
3728 | cmpeq CARG1, STR:RC
3730 | ldrb CARG2, TAB:RB->marked
3731 | checktp CARG4, LJ_TNIL // Key found, but nil value?
3732 | ldrd CARG34, [BASE, RA]
3735 | tst CARG2, #LJ_GC_BLACK // isblack(table)
3736 | strd CARG34, NODE:INS->val
3741 |4: // Check for __newindex if previous value is nil.
3742 | ldr TAB:CARG1, TAB:RB->metatable
3744 | beq <2 // No metatable: done.
3745 | ldrb CARG1, TAB:CARG1->nomm
3746 | tst CARG1, #1<<MM_newindex
3747 | bne <2 // 'no __newindex' flag set: done.
3750 |5: // Follow hash chain.
3751 | movs NODE:INS, NODE:CARG3
3753 | // End of hash chain: key not found, add a new one.
3755 | // But check for __newindex first.
3756 | ldr TAB:CARG1, TAB:RB->metatable
3759 | cmp TAB:CARG1, #0 // No metatable: continue.
3761 | ldrbne CARG2, TAB:CARG1->nomm
3764 | tst CARG2, #1<<MM_newindex
3765 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3767 | mvn CARG4, #~LJ_TSTR
3768 | str STR:RC, TMPDlo
3771 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3772 | // Returns TValue *.
3774 | ldrd CARG34, [BASE, RA]
3775 | strd CARG34, [CRET1]
3776 | b <3 // No 2nd write barrier needed.
3778 |7: // Possible table write barrier for the value. Skip valiswhite check.
3779 | barrierback TAB:RB, CARG2, CARG3
3783 | decode_RB8 RB, INS
3785 | // RA = src*8, RB = table*8, RC = index
3786 | ldrd CARG12, [BASE, RB]
3787 | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12.
3788 | ldr CARG3, TAB:CARG1->asize
3789 | ldr RB, TAB:CARG1->array
3792 | ldrdlo CARG34, [CARG2, RB]!
3794 | ins_next1 // Overwrites RB!
3795 | checktp CARG4, LJ_TNIL
3796 | ldrb INS, TAB:CARG1->marked
3797 | ldrd CARG34, [BASE, RA]
3800 | tst INS, #LJ_GC_BLACK // isblack(table)
3801 | strd CARG34, [CARG2]
3807 |5: // Check for __newindex if previous value is nil.
3808 | ldr TAB:RA, TAB:CARG1->metatable
3810 | beq <1 // No metatable: done.
3811 | ldrb RA, TAB:RA->nomm
3812 | tst RA, #1<<MM_newindex
3813 | bne <1 // 'no __newindex' flag set: done.
3814 | ldr INS, [PC, #-4] // Restore INS.
3815 | decode_RA8 RA, INS
3818 |7: // Possible table write barrier for the value. Skip valiswhite check.
3819 | barrierback TAB:CARG1, INS, CARG3
3823 | decode_RB8 RB, INS
3824 | decode_RC8 RC, INS
3825 | // RA = src*8, RB = table*8, RC = key*8
3826 | ldr TAB:CARG2, [BASE, RB]
3827 | ldr CARG3, [BASE, RC]
3828 | ldrb INS, TAB:CARG2->marked
3829 | ldr CARG1, TAB:CARG2->array
3830 | ldr CARG4, TAB:CARG2->asize
3831 | tst INS, #LJ_GC_BLACK // isblack(table)
3832 | add CARG1, CARG1, CARG3, lsl #3
3835 | cmp CARG3, CARG4 // In array part?
3838 | ldrd CARG34, [BASE, RA]
3841 | strd CARG34, [CARG1]
3844 |7: // Possible table write barrier for the value. Skip valiswhite check.
3845 | barrierback TAB:CARG2, INS, RB
3850 | // RA = base*8 (table at base-1), RC = num_const (start index)
3853 | ldr RB, SAVE_MULTRES
3854 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
3855 | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
3857 | ldr CARG4, TAB:CARG2->asize
3858 | beq >4 // Nothing to copy?
3859 | add CARG3, CARG1, RB, lsr #3
3861 | ldr CARG4, TAB:CARG2->array
3864 | add INS, CARG4, CARG1, lsl #3
3865 | ldrb CARG1, TAB:CARG2->marked
3866 |3: // Copy result slots to table.
3867 | ldrd CARG34, [RA], #8
3868 | strd CARG34, [INS], #8
3871 | tst CARG1, #LJ_GC_BLACK // isblack(table)
3876 |5: // Need to resize array part.
3880 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3881 | // Must not reallocate the stack.
3882 | .IOS ldr BASE, L->base
3885 |7: // Possible table write barrier for any value. Skip valiswhite check.
3886 | barrierback TAB:CARG2, CARG1, CARG3
3890 /* -- Calls and vararg handling ----------------------------------------- */
3893 | // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3894 | ldr CARG1, SAVE_MULTRES
3895 | decode_RC8 NARGS8:RC, INS
3896 | add NARGS8:RC, NARGS8:RC, CARG1
3900 | decode_RC8 NARGS8:RC, INS
3901 | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3903 | mov RB, BASE // Save old BASE for vmeta_call.
3904 | ldrd CARG34, [BASE, RA]!
3905 | sub NARGS8:RC, NARGS8:RC, #8
3906 | add BASE, BASE, #8
3907 | checkfunc CARG4, ->vmeta_call
3912 | // RA = base*8, (RB = 0,) RC = extra_nargs
3913 | ldr CARG1, SAVE_MULTRES
3914 | add NARGS8:RC, CARG1, RC, lsl #3
3918 | lsl NARGS8:RC, RC, #3
3919 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3921 | ldrd LFUNC:CARG34, [RA, BASE]!
3922 | sub NARGS8:RC, NARGS8:RC, #8
3924 | checkfunc CARG4, ->vmeta_callt
3925 | ldr PC, [BASE, FRAME_PC]
3928 | ldrb CARG4, LFUNC:CARG3->ffid
3929 | tst PC, #FRAME_TYPE
3932 | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
3936 | ldrd CARG12, [RA, RB]
3938 | cmp INS, NARGS8:RC
3939 | strd CARG12, [BASE, RB]
3943 | cmp CARG4, #1 // (> FF_C) Calling a fast function?
3948 |5: // Tailcall to a fast function with a Lua frame below.
3949 | ldr INS, [PC, #-4]
3950 | decode_RA8 RA, INS
3951 | sub CARG1, BASE, RA
3952 | ldr LFUNC:CARG1, [CARG1, #-16]
3953 | ldr CARG1, LFUNC:CARG1->field_pc
3954 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3957 |7: // Tailcall from a vararg function.
3958 | eor PC, PC, #FRAME_VARG
3959 | tst PC, #FRAME_TYPEP // Vararg frame below?
3960 | movne CARG4, #0 // Clear ffid if no Lua function below.
3962 | sub BASE, BASE, PC
3963 | ldr PC, [BASE, FRAME_PC]
3964 | tst PC, #FRAME_TYPE
3965 | movne CARG4, #0 // Clear ffid if no Lua function below.
3970 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3972 | mov RB, BASE // Save old BASE for vmeta_call.
3973 | ldrd CARG34, [RA, #-16]
3974 | ldrd CARG12, [RA, #-8]
3976 | strd CARG34, [RA, #8] // Copy state.
3977 | strd CARG12, [RA, #16] // Copy control var.
3978 | // STALL: locked CARG34.
3979 | ldrd LFUNC:CARG34, [RA, #-24]
3980 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3981 | // STALL: load CARG34.
3982 | strd LFUNC:CARG34, [RA] // Copy callable.
3983 | checkfunc CARG4, ->vmeta_call
3992 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3994 | ldr TAB:RB, [RA, #-16]
3995 | ldr CARG1, [RA, #-8] // Get index from control var.
3996 | ldr INS, TAB:RB->asize
3997 | ldr CARG2, TAB:RB->array
3999 |1: // Traverse array part.
4000 | subs RC, CARG1, INS
4001 | add CARG3, CARG2, CARG1, lsl #3
4002 | bhs >5 // Index points after array part?
4003 | ldrd CARG34, [CARG3]
4004 | checktp CARG4, LJ_TNIL
4005 | addeq CARG1, CARG1, #1 // Skip holes in array part.
4007 | ldrh RC, [PC, #-2]
4008 | mvn CARG2, #~LJ_TISNUM
4009 | strd CARG34, [RA, #8]
4010 | add RC, PC, RC, lsl #2
4013 | sub PC, RC, #0x20000
4014 | str RB, [RA, #-8] // Update control var.
4018 |5: // Traverse hash part.
4019 | ldr CARG4, TAB:RB->hmask
4020 | ldr NODE:RB, TAB:RB->node
4022 | add CARG1, RC, RC, lsl #1
4023 | cmp RC, CARG4 // End of iteration? Branch to ITERL+1.
4024 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
4026 | ldrd CARG12, NODE:CARG3->val
4027 | checktp CARG2, LJ_TNIL
4029 | beq <6 // Skip holes in hash part.
4030 | ldrh RB, [PC, #-2]
4032 | ldrd CARG34, NODE:CARG3->key
4033 | str RC, [RA, #-8] // Update control var.
4034 | strd CARG12, [RA, #8]
4035 | add RC, PC, RB, lsl #2
4036 | sub PC, RC, #0x20000
4042 | // RA = base*8, RC = target (points to ITERN)
4044 | add RC, PC, RC, lsl #2
4045 | ldrd CFUNC:CARG12, [RA, #-24]
4046 | ldr CARG3, [RA, #-12]
4047 | ldr CARG4, [RA, #-4]
4048 | checktp CARG2, LJ_TFUNC
4049 | ldrbeq CARG1, CFUNC:CARG1->ffid
4050 | checktpeq CARG3, LJ_TTAB
4051 | checktpeq CARG4, LJ_TNIL
4052 | cmpeq CARG1, #FF_next_N
4053 | subeq PC, RC, #0x20000
4058 | mvn CARG2, #~LJ_KEYINDEX
4059 | strd CARG1, [RA, #-8] // Initialize control var.
4062 |5: // Despecialize bytecode if any of the checks fail.
4063 | mov CARG1, #BC_JMP
4065 | strb CARG1, [PC, #-4]
4066 | sub PC, RC, #0x20000
4069 | cmp CARG1, #BC_ITERN
4072 | strb OP, [PC] // Subsumes ins_next1.
4076 |6: // Unpatch JLOOP.
4077 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4078 | ldrh CARG2, [PC, #2]
4079 | ldr TRACE:CARG1, [CARG1, CARG2, lsl #2]
4080 | // Subsumes ins_next1 and ins_next2.
4081 | ldr INS, TRACE:CARG1->startins
4082 | bfi INS, OP, #0, #8
4089 | decode_RB8 RB, INS
4090 | decode_RC8 RC, INS
4091 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4092 | ldr CARG1, [BASE, FRAME_PC]
4095 | add RC, RC, #FRAME_VARG
4097 | sub CARG3, BASE, #8 // CARG3 = vtop
4098 | sub RC, RC, CARG1 // RC = vbase
4099 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4101 | sub CARG1, CARG3, RC
4102 | beq >5 // Copy all varargs?
4103 | sub CARG4, CARG4, #16
4104 |1: // Copy vararg slots to destination slots.
4106 | ldrdlo CARG12, [RC], #8
4107 | mvnhs CARG2, #~LJ_TNIL
4109 | strd CARG12, [RA], #8
4114 |5: // Copy all varargs.
4115 | ldr CARG4, L->maxstack
4117 | movle RB, #8 // MULTRES = (0+1)*8
4118 | addgt RB, CARG1, #8
4119 | add CARG2, RA, CARG1
4120 | str RB, SAVE_MULTRES
4125 | ldrd CARG12, [RC], #8
4126 | strd CARG12, [RA], #8
4131 |7: // Grow stack for varargs.
4132 | lsr CARG2, CARG1, #3
4136 | sub RC, RC, BASE // Need delta, because BASE may change.
4139 | bl extern lj_state_growstack // (lua_State *L, int n)
4143 | sub CARG3, BASE, #8
4147 /* -- Returns ----------------------------------------------------------- */
4150 | // RA = results*8, RC = extra results
4151 | ldr CARG1, SAVE_MULTRES
4152 | ldr PC, [BASE, FRAME_PC]
4154 | add RC, CARG1, RC, lsl #3
4159 | // RA = results*8, RC = nresults+1
4160 | ldr PC, [BASE, FRAME_PC]
4164 | str RC, SAVE_MULTRES
4166 | ands CARG1, PC, #FRAME_TYPE
4167 | eor CARG2, PC, #FRAME_VARG
4171 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
4172 | ldr INS, [PC, #-4]
4173 | subs CARG4, RC, #8
4174 | sub CARG3, BASE, #8
4177 | ldrd CARG12, [RA], #8
4178 | add BASE, BASE, #8
4179 | subs CARG4, CARG4, #8
4180 | strd CARG12, [BASE, #-16]
4183 | decode_RA8 RA, INS
4184 | sub CARG4, CARG3, RA
4185 | decode_RB8 RB, INS
4186 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4188 | cmp RB, RC // More results expected?
4191 | ldr CARG2, LFUNC:CARG1->field_pc
4194 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4197 |6: // Fill up results with nil.
4198 | mvn CARG2, #~LJ_TNIL
4199 | add BASE, BASE, #8
4201 | str CARG2, [BASE, #-12]
4204 |->BC_RETV1_Z: // Non-standard return case.
4207 | tst CARG2, #FRAME_TYPEP
4209 | // Return from vararg function: relocate BASE down.
4210 | sub BASE, BASE, CARG2
4211 | ldr PC, [BASE, FRAME_PC]
4215 case BC_RET0: case BC_RET1:
4216 | // RA = results*8, RC = nresults+1
4217 | ldr PC, [BASE, FRAME_PC]
4219 | str RC, SAVE_MULTRES
4220 | ands CARG1, PC, #FRAME_TYPE
4221 | eor CARG2, PC, #FRAME_VARG
4222 | ldreq INS, [PC, #-4]
4224 if (op == BC_RET1) {
4225 | ldrd CARG12, [BASE, RA]
4227 | sub CARG4, BASE, #8
4228 | decode_RA8 RA, INS
4229 if (op == BC_RET1) {
4230 | strd CARG12, [CARG4]
4232 | sub BASE, CARG4, RA
4233 | decode_RB8 RB, INS
4234 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4238 | ldr CARG2, LFUNC:CARG1->field_pc
4241 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4244 |6: // Fill up results with nil.
4245 | sub CARG2, CARG4, #4
4246 | mvn CARG3, #~LJ_TNIL
4247 | str CARG3, [CARG2, RC]
4252 /* -- Loops and branches ------------------------------------------------ */
4254 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
4255 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
4256 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4257 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
4263 | // Fall through. Assumes BC_IFORL follows.
4273 | // RA = base*8, RC = target (after end of loop or start of loop)
4274 vk = (op == BC_IFORL || op == BC_JFORL);
4275 | ldrd CARG12, [RA, BASE]!
4276 if (op != BC_JFORL) {
4277 | add RC, PC, RC, lsl #2
4280 | ldrd CARG34, FOR_STOP
4281 | checktp CARG2, LJ_TISNUM
4284 | checktp CARG4, LJ_TISNUM
4285 | ldr CARG4, FOR_STEP
4286 | checktpeq RB, LJ_TISNUM
4292 | ldrd CARG34, FOR_STEP
4293 | checktp CARG2, LJ_TISNUM
4295 | adds CARG1, CARG1, CARG3
4296 | ldr CARG4, FOR_STOP
4297 if (op == BC_IFORL) {
4298 | addvs RC, PC, #0x20000 // Overflow: prevent branch.
4300 | bvs >2 // Overflow: do not enter mcode.
4307 if (op == BC_FORI) {
4308 | subgt PC, RC, #0x20000
4309 } else if (op == BC_JFORI) {
4310 | sub PC, RC, #0x20000
4311 | ldrhle RC, [PC, #-2]
4312 } else if (op == BC_IFORL) {
4313 | suble PC, RC, #0x20000
4316 | strd CARG12, FOR_IDX
4321 | strd CARG12, FOR_EXT
4322 if (op == BC_JFORI || op == BC_JFORL) {
4328 |4: // Invert check for negative step.
4338 | cmnlo CARG4, #-LJ_TISNUM
4339 | cmnlo RB, #-LJ_TISNUM
4348 | strd CARG12, FOR_EXT
4357 | vadd.f64 d0, d0, d2
4361 | bl extern __aeabi_dadd
4362 | strd CARG12, FOR_IDX
4363 | ldrd CARG34, FOR_STOP
4364 | strd CARG12, FOR_EXT
4373 | bl extern __aeabi_cdcmple
4381 if (op == BC_FORI) {
4382 | subhi PC, RC, #0x20000
4383 } else if (op == BC_JFORI) {
4384 | sub PC, RC, #0x20000
4385 | ldrhls RC, [PC, #-2]
4387 } else if (op == BC_IFORL) {
4388 | subls PC, RC, #0x20000
4397 |8: // Invert check for negative step.
4399 | bl extern __aeabi_dadd
4400 | strd CARG12, FOR_IDX
4401 | strd CARG12, FOR_EXT
4405 | ldrd CARG12, FOR_STOP
4414 | // Fall through. Assumes BC_IITERL follows.
4422 | // RA = base*8, RC = target
4423 | ldrd CARG12, [RA, BASE]!
4424 if (op == BC_JITERL) {
4425 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4426 | strdne CARG12, [RA, #-8]
4429 | add RC, PC, RC, lsl #2
4430 | // STALL: load CARG12.
4431 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4432 | subne PC, RC, #0x20000 // Otherwise save control var + branch.
4433 | strdne CARG12, [RA, #-8]
4439 | // RA = base*8, RC = target (loop extent)
4440 | // Note: RA/RC is only used by trace recorder to determine scope/extent
4441 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4445 | // Fall through. Assumes BC_ILOOP follows.
4449 | // RA = base*8, RC = target (loop extent)
4455 | // RA = base (ignored), RC = traceno
4456 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4457 | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0.
4458 | ldr TRACE:RC, [CARG1, RC, lsl #2]
4460 | ldr RA, TRACE:RC->mcode
4461 | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4462 | str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)]
4468 | // RA = base*8 (only used by trace recorder), RC = target
4469 | add RC, PC, RC, lsl #2
4470 | sub PC, RC, #0x20000
4474 /* -- Function headers -------------------------------------------------- */
4480 case BC_FUNCV: /* NYI: compiled vararg functions. */
4481 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4489 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4490 | ldr CARG1, L->maxstack
4491 | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4492 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4494 | bhi ->vm_growstack_l
4495 if (op != BC_JFUNCF) {
4500 | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters.
4501 | mvn CARG4, #~LJ_TNIL
4503 if (op == BC_JFUNCF) {
4510 |3: // Clear missing parameters.
4511 | strd CARG34, [BASE, NARGS8:RC]
4512 | add NARGS8:RC, NARGS8:RC, #8
4520 | NYI // NYI: compiled vararg functions
4521 break; /* NYI: compiled vararg functions. */
4524 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4525 | ldr CARG1, L->maxstack
4526 | add CARG4, BASE, RC
4528 | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC.
4529 | add CARG2, RC, #8+FRAME_VARG
4530 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4532 | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG.
4533 | bhs ->vm_growstack_l
4534 | ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4538 | add BASE, CARG4, #8
4540 | mvn CARG3, #~LJ_TNIL
4542 | cmp RA, RC // Less args than parameters?
4543 | ldrdlo CARG12, [RA], #8
4544 | movhs CARG2, CARG3
4545 | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC).
4548 | strd CARG12, [CARG4, #8]!
4556 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4557 if (op == BC_FUNCC) {
4558 | ldr CARG4, CFUNC:CARG3->f
4560 | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4562 | add CARG2, RA, NARGS8:RC
4563 | ldr CARG1, L->maxstack
4564 | add RC, BASE, NARGS8:RC
4568 if (op == BC_FUNCCW) {
4569 | ldr CARG2, CFUNC:CARG3->f
4571 | mv_vmstate CARG3, C
4573 | bhi ->vm_growstack_c // Need to grow stack.
4575 | blx CARG4 // (lua_State *L [, lua_CFunction f])
4576 | // Returns nresults.
4578 | mv_vmstate CARG3, INTERP
4580 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
4583 | ldr PC, [BASE, FRAME_PC]
4584 | sub RA, CRET2, RC // RA = L->top - nresults*8
4588 /* ---------------------------------------------------------------------- */
4591 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4597 static int build_backend(BuildCtx *ctx)
4601 dasm_growpc(Dst, BC__MAX);
4603 build_subroutines(ctx);
4606 for (op = 0; op < BC__MAX; op++)
4607 build_ins(ctx, (BCOp)op, op);
4612 /* Emit pseudo frame-info for all assembler functions. */
4613 static void emit_asm_debug(BuildCtx *ctx)
4615 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4617 switch (ctx->mode) {
4619 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4622 "\t.long .LECIE0-.LSCIE0\n"
4624 "\t.long 0xffffffff\n"
4629 "\t.byte 0xe\n" /* Return address is in lr. */
4630 "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */
4635 "\t.long .LEFDE0-.LASFDE0\n"
4637 "\t.long .Lframe0\n"
4640 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4641 "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */
4642 fcofs, CFRAME_SIZE);
4643 for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */
4644 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4646 for (i = 15; i >= 8; i--) /* offset d8-d15 */
4647 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4648 64+2*i, 10+2*(15-i));
4649 fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */
4657 "\t.long .LEFDE1-.LASFDE1\n"
4659 "\t.long .Lframe0\n"
4660 "\t.long lj_vm_ffi_call\n"
4662 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4663 "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */
4664 "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */
4665 "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */
4666 "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */
4667 "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */
4669 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);