1 |// Low-level VM code for ARM CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
10 |.globalnames globnames
11 |.externnames extnames
13 |// Note: The ragged indentation of the instructions is intentional.
14 |// The starting columns indicate data dependencies.
16 |//-----------------------------------------------------------------------
18 |// Fixed register assignments for the interpreter.
20 |// The following must be C callee-save.
21 |.define MASKR8, r4 // 255*8 constant for fast bytecode decoding.
22 |.define KBASE, r5 // Constants of current Lua function.
23 |.define PC, r6 // Next PC.
24 |.define DISPATCH, r7 // Opcode dispatch table.
25 |.define LREG, r8 // Register holding lua_State (also in SAVE_L).
27 |// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+.
28 |.define BASE, r9 // Base of current Lua stack frame.
30 |// The following temporaries are not saved across C calls, except for RA/RC.
31 |.define RA, r10 // Callee-save.
32 |.define RC, r11 // Callee-save.
34 |.define OP, r12 // Overlaps RB, must not be lr.
37 |// Calling conventions. Also used as temporaries.
42 |.define CARG12, r0 // For 1st soft-fp double.
43 |.define CARG34, r2 // For 2nd soft-fp double.
48 |// Stack layout while in interpreter. Must match with lj_frame.h.
49 |.define SAVE_R4, [sp, #28]
50 |.define CFRAME_SPACE, #28
51 |.define SAVE_ERRF, [sp, #24]
52 |.define SAVE_NRES, [sp, #20]
53 |.define SAVE_CFRAME, [sp, #16]
54 |.define SAVE_L, [sp, #12]
55 |.define SAVE_PC, [sp, #8]
56 |.define SAVE_MULTRES, [sp, #4]
59 |.define TMPDhi, [sp, #4]
66 | push {r5, r6, r7, r8, r9, r10, r11, lr}
68 | sub sp, sp, CFRAME_SPACE+4
71 |.macro restoreregs_ret
73 | add sp, sp, CFRAME_SPACE+4
75 | pop {r5, r6, r7, r8, r9, r10, r11, pc}
79 | push {r4, r5, r6, r7, r8, r9, r10, r11, lr}
80 | sub sp, sp, CFRAME_SPACE
82 |.macro restoreregs_ret
83 | add sp, sp, CFRAME_SPACE
84 | pop {r4, r5, r6, r7, r8, r9, r10, r11, pc}
88 |// Type definitions. Some of these are only used for documentation.
89 |.type L, lua_State, LREG
90 |.type GL, global_State
101 |.type TRACE, GCtrace
104 |//-----------------------------------------------------------------------
106 |// Trap for not-yet-implemented parts.
107 |.macro NYI; ud; .endmacro
109 |//-----------------------------------------------------------------------
111 |// Access to frame relative to BASE.
112 |.define FRAME_FUNC, #-8
113 |.define FRAME_PC, #-4
115 |.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro
116 |.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro
117 |.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro
118 |.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro
119 |.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro
121 |// Instruction fetch.
128 |// Instruction decode+dispatch.
130 | ldr OP, [DISPATCH, OP, lsl #2]
141 |// Instruction footer.
143 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
144 | .define ins_next, ins_NEXT
145 | .define ins_next_, ins_NEXT
146 | .define ins_next1, ins_NEXT1
147 | .define ins_next2, ins_NEXT2
148 | .define ins_next3, ins_NEXT3
150 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
151 | // Affects only certain kinds of benchmarks (and only with -j off).
168 |// Avoid register name substitution for field name.
171 |// Call decode and dispatch.
173 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
174 | ldr PC, LFUNC:CARG3->field_pc
175 | ldrb OP, [PC] // STALL: load PC. early PC.
177 | ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP.
184 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
185 | str PC, [BASE, FRAME_PC]
186 | ins_callt // STALL: locked PC.
189 |//-----------------------------------------------------------------------
191 |// Macros to test operand types.
192 |.macro checktp, reg, tp; cmn reg, #-tp; .endmacro
193 |.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro
194 |.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro
195 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro
196 |.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro
197 |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro
199 |// Assumes DISPATCH is relative to GL.
200 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
201 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
203 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
205 |.macro hotcheck, delta
207 | and CARG1, CARG1, #126
208 | sub CARG1, CARG1, #-GG_DISP2HOT
209 | ldrh CARG2, [DISPATCH, CARG1]
210 | subs CARG2, CARG2, #delta
211 | strh CARG2, [DISPATCH, CARG1]
215 | hotcheck HOTCOUNT_LOOP
220 | hotcheck HOTCOUNT_CALL
224 |// Set current VM state.
225 |.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro
226 |.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro
228 |// Move table write barrier back. Overwrites mark and tmp.
229 |.macro barrierback, tab, mark, tmp
230 | ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
231 | bic mark, mark, #LJ_GC_BLACK // black2gray(tab)
232 | str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
233 | strb mark, tab->marked
234 | str tmp, tab->gclist
243 |//-----------------------------------------------------------------------
246 #error "Only dual-number mode supported for ARM target"
249 /* Generate subroutines used by opcodes and other parts of the VM. */
250 /* The .code_sub section should be last to help static branch prediction. */
251 static void build_subroutines(BuildCtx *ctx)
255 |//-----------------------------------------------------------------------
256 |//-- Return handling ----------------------------------------------------
257 |//-----------------------------------------------------------------------
260 | // See vm_return. Also: RB = previous base.
262 | beq ->cont_dispatch
264 | // Return from pcall or xpcall fast func.
265 | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame.
266 | mvn CARG2, #~LJ_TTRUE
268 | // Prepending may overwrite the pcall frame, so do it at the end.
269 | str CARG2, [RA, FRAME_PC] // Prepend true to results.
273 | adds RC, RC, #8 // RC = (nresults+1)*8.
274 | mov CRET1, #LUA_YIELD
275 | beq ->vm_unwind_c_eh
276 | str RC, SAVE_MULTRES
277 | ands CARG1, PC, #FRAME_TYPE
278 | beq ->BC_RET_Z // Handle regular return to Lua.
281 | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return
282 | // CARG1 = PC & FRAME_TYPE
283 | bic RB, PC, #FRAME_TYPEP
284 | cmp CARG1, #FRAME_C
285 | sub RB, BASE, RB // RB = previous base.
289 | ldr KBASE, SAVE_NRES
290 | mv_vmstate CARG4, C
293 | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8
297 | subs CARG3, CARG3, #8
298 | ldrd CARG12, [RA], #8
299 | strd CARG12, [BASE], #8
302 | cmp KBASE, RC // More/less results wanted?
305 | str BASE, L->top // Store new top.
308 | ldr RC, SAVE_CFRAME // Restore previous C frame.
309 | mov CRET1, #0 // Ok return status for vm_pcall.
316 | blt >7 // Less results wanted?
317 | // More results wanted. Check stack size and fill up results with nil.
318 | ldr CARG3, L->maxstack
319 | mvn CARG2, #~LJ_TNIL
322 | str CARG2, [BASE, #4]
327 |7: // Less results wanted.
328 | sub CARG1, RC, KBASE
329 | cmp KBASE, #0 // LUA_MULTRET+1 case?
330 | subne BASE, BASE, CARG1 // Either keep top or shrink it.
333 |8: // Corner case: need to grow stack for filling up results.
334 | // This can happen if:
335 | // - A C function grows the stack (a lot).
336 | // - The GC shrinks the stack in between.
337 | // - A return back from a lua_call() with (high) nresults adjustment.
338 | str BASE, L->top // Save current top held in BASE (yes).
339 | lsr CARG2, KBASE, #3
341 | bl extern lj_state_growstack // (lua_State *L, int n)
342 | ldr BASE, L->top // Need the (realloced) L->top in BASE.
345 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
346 | // (void *cframe, int errcode)
349 |->vm_unwind_c_eh: // Landing pad for external unwinder.
351 | mv_vmstate CARG4, C
352 | ldr GL:CARG3, L->glref
353 | str CARG4, GL:CARG3->vmstate
356 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
358 | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
360 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
363 | mov RC, #16 // 2 results: false + error message.
364 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
366 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
367 | mvn CARG1, #~LJ_TFALSE
368 | sub RA, BASE, #8 // Results start at BASE-8.
369 | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame.
370 | add DISPATCH, DISPATCH, #GG_G2DISP
371 | mv_vmstate CARG2, INTERP
372 | str CARG1, [BASE, #-4] // Prepend false to error message.
376 |->vm_unwind_ext: // Complete external unwind.
378 | push {r0, r1, r2, lr}
379 | bl extern _Unwind_Complete
381 | bl extern _Unwind_DeleteException
382 | pop {r0, r1, r2, lr}
387 |//-----------------------------------------------------------------------
388 |//-- Grow stack for calls -----------------------------------------------
389 |//-----------------------------------------------------------------------
391 |->vm_growstack_c: // Grow stack for C function.
393 | mov CARG2, #LUA_MINSTACK
396 |->vm_growstack_l: // Grow stack for Lua function.
397 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
402 | add PC, PC, #4 // Must point after first instruction.
406 | // L->base = new base, L->top = top
408 | bl extern lj_state_growstack // (lua_State *L, int n)
411 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
412 | sub NARGS8:RC, RC, BASE
413 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
414 | ins_callt // Just retry the call.
416 |//-----------------------------------------------------------------------
417 |//-- Entry points into the assembler VM ---------------------------------
418 |//-----------------------------------------------------------------------
420 |->vm_resume: // Setup C frame and resume thread.
421 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
424 | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table.
426 | add DISPATCH, DISPATCH, #GG_G2DISP
429 | str CARG3, SAVE_NRES
430 | add CARG2, sp, #CFRAME_RESUME
431 | ldrb CARG1, L->status
432 | str CARG3, SAVE_ERRF
433 | str L, SAVE_PC // Any value outside of bytecode is ok.
434 | str CARG3, SAVE_CFRAME
436 | str CARG2, L->cframe
439 | // Resume after yield (like a return).
440 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
445 | strb CARG3, L->status
446 | sub RC, CARG1, BASE
447 | ldr PC, [BASE, FRAME_PC]
448 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
449 | mv_vmstate CARG2, INTERP
451 | ands CARG1, PC, #FRAME_TYPE
453 | str RC, SAVE_MULTRES
457 |->vm_pcall: // Setup protected C frame and enter VM.
458 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
461 | str CARG4, SAVE_ERRF
464 |->vm_call: // Setup C frame and enter VM.
465 | // (lua_State *L, TValue *base, int nres1)
469 |1: // Entry point for vm_pcall above (PC = ftype).
470 | ldr RC, L:CARG1->cframe
471 | str CARG3, SAVE_NRES
474 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
476 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
477 | str RC, SAVE_CFRAME
478 | add DISPATCH, DISPATCH, #GG_G2DISP
479 | str sp, L->cframe // Add our C frame to cframe chain.
481 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
482 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
483 | ldr RB, L->base // RB = old base (for vmeta_call).
487 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
488 | sub PC, PC, RB // PC = frame delta + frame type
489 | mv_vmstate CARG2, INTERP
490 | sub NARGS8:RC, CARG1, BASE
494 | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
495 | ldrd CARG34, [BASE, FRAME_FUNC]
496 | checkfunc CARG4, ->vmeta_call
498 |->vm_call_dispatch_f:
500 | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC
502 |->vm_cpcall: // Setup protected C frame, call C.
503 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
506 | ldr RA, L:CARG1->stack
508 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
510 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
512 | add DISPATCH, DISPATCH, #GG_G2DISP
513 | sub RA, RA, RB // Compute -savestack(L, L->top).
515 | str RA, SAVE_NRES // Neg. delta means cframe w/o frame.
516 | str RB, SAVE_ERRF // No error function.
517 | str RC, SAVE_CFRAME
518 | str sp, L->cframe // Add our C frame to cframe chain.
519 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
520 | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud)
523 | bne <3 // Else continue with the call.
524 | b ->vm_leave_cp // No base? Just remove C frame.
526 |//-----------------------------------------------------------------------
527 |//-- Metamethod handling ------------------------------------------------
528 |//-----------------------------------------------------------------------
530 |//-- Continuation dispatch ----------------------------------------------
533 | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8
534 | ldr LFUNC:CARG3, [RB, FRAME_FUNC]
535 | ldr CARG1, [BASE, #-16] // Get continuation.
537 | mov BASE, RB // Restore caller BASE.
541 | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC].
544 | str INS, [CARG2, #-4] // Ensure one valid arg.
548 | ldr CARG3, LFUNC:CARG3->field_pc
549 | ldr KBASE, [CARG3, #PC2PROTO(k)]
550 | // BASE = base, RA = resultptr, CARG4 = meta base
555 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
556 | // cont = 0: tailcall from C function.
557 | sub CARG4, CARG4, #16
558 | sub RC, CARG4, BASE
562 |->cont_cat: // RA = resultptr, CARG4 = meta base
564 | sub CARG2, CARG4, #16
569 | add CARG1, BASE, RC
570 | subs CARG1, CARG2, CARG1
571 | strdne CARG34, [CARG2]
574 | strd CARG34, [BASE, RA]
577 |//-- Table indexing metamethods -----------------------------------------
580 | add CARG2, BASE, RB
584 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
585 | mvn CARG4, #~LJ_TTAB
586 | str TAB:RB, [CARG2]
587 | str CARG4, [CARG2, #4]
589 | mvn CARG4, #~LJ_TSTR
595 |->vmeta_tgetb: // RC = index
598 | mvn CARG4, #~LJ_TISNUM
599 | add CARG2, BASE, RB
605 | add CARG2, BASE, RB
606 | add CARG3, BASE, RC
611 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
612 | // Returns TValue * (finished) or NULL (metamethod).
613 | .IOS ldr BASE, L->base
616 | ldrd CARG34, [CRET1]
619 | strd CARG34, [BASE, RA]
622 |3: // Call __index metamethod.
623 | // BASE = base, L->top = new base, stack = cont/func/t/k
624 | rsb CARG1, BASE, #FRAME_CONT
626 | mov NARGS8:RC, #16 // 2 args for func(t, k).
627 | str PC, [BASE, #-12] // [cont|PC]
628 | add PC, CARG1, BASE
629 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
630 | b ->vm_call_dispatch_f
634 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
635 | // Returns cTValue * or NULL.
638 | ldrdne CARG12, [CRET1]
639 | mvneq CARG2, #~LJ_TNIL
642 |//-----------------------------------------------------------------------
645 | add CARG2, BASE, RB
649 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
650 | mvn CARG4, #~LJ_TTAB
651 | str TAB:RB, [CARG2]
652 | str CARG4, [CARG2, #4]
654 | mvn CARG4, #~LJ_TSTR
660 |->vmeta_tsetb: // RC = index
663 | mvn CARG4, #~LJ_TISNUM
664 | add CARG2, BASE, RB
670 | add CARG2, BASE, RB
671 | add CARG3, BASE, RC
676 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
677 | // Returns TValue * (finished) or NULL (metamethod).
678 | .IOS ldr BASE, L->base
680 | ldrd CARG34, [BASE, RA]
683 | // NOBARRIER: lj_meta_tset ensures the table is not black.
684 | strd CARG34, [CRET1]
688 |3: // Call __newindex metamethod.
689 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
690 | rsb CARG1, BASE, #FRAME_CONT
692 | mov NARGS8:RC, #24 // 3 args for func(t, k, v).
693 | strd CARG34, [BASE, #16] // Copy value to third argument.
694 | str PC, [BASE, #-12] // [cont|PC]
695 | add PC, CARG1, BASE
696 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
697 | b ->vm_call_dispatch_f
704 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
705 | // Returns TValue *.
709 |//-- Comparison metamethods ---------------------------------------------
718 | decode_OP CARG4, INS
719 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
720 | // Returns 0/1 or TValue * (metamethod).
722 | .IOS ldr BASE, L->base
728 | add RB, PC, RB, lsl #2
729 | subhs PC, RB, #0x20000
733 |->cont_ra: // RA = resultptr
736 | decode_RA8 CARG3, INS
737 | strd CARG12, [BASE, CARG3]
740 |->cont_condt: // RA = resultptr
741 | ldr CARG2, [RA, #4]
742 | mvn CARG1, #~LJ_TTRUE
743 | cmp CARG1, CARG2 // Branch if result is true.
746 |->cont_condf: // RA = resultptr
747 | ldr CARG2, [RA, #4]
748 | checktp CARG2, LJ_TFALSE // Branch if result is false.
752 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
757 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
758 | // Returns 0/1 or TValue * (metamethod).
768 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
769 | // Returns 0/1 or TValue * (metamethod).
780 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
781 | .IOS ldr BASE, L->base
784 |//-- Arithmetic metamethods ---------------------------------------------
789 | add CARG3, BASE, RB
790 | add CARG4, KBASE, RC
796 | add CARG4, BASE, RB
797 | add CARG3, KBASE, RC
803 | add CARG3, BASE, RC
804 | add CARG4, BASE, RC
810 | add CARG3, BASE, RB
811 | add CARG4, BASE, RC
814 | add CARG2, BASE, RA
819 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
820 | // Returns NULL (finished) or TValue * (metamethod).
821 | .IOS ldr BASE, L->base
825 | // Call metamethod for binary op.
827 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
828 | sub CARG2, CRET1, BASE
829 | str PC, [CRET1, #-12] // [cont|PC]
830 | add PC, CARG2, #FRAME_CONT
832 | mov NARGS8:RC, #16 // 2 args for func(o1, o2).
833 | b ->vm_call_dispatch
836 | add CARG2, BASE, RC
840 | bl extern lj_meta_len // (lua_State *L, TValue *o)
841 | // Returns NULL (retry) or TValue * (metamethod base).
842 | .IOS ldr BASE, L->base
845 | bne ->vmeta_binop // Binop call for compatibility.
846 | ldr TAB:CARG1, [BASE, RC]
849 | b ->vmeta_binop // Binop call for compatibility.
852 |//-- Call metamethod ----------------------------------------------------
854 |->vmeta_call: // Resolve and call __call metamethod.
855 | // RB = old base, BASE = new base, RC = nargs*8
857 | str RB, L->base // This is the callers base!
858 | sub CARG2, BASE, #8
860 | add CARG3, BASE, NARGS8:RC
862 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
864 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
865 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
868 |->vmeta_callt: // Resolve __call for BC_CALLT.
869 | // BASE = old base, RA = new base, RC = nargs*8
874 | add CARG3, RA, NARGS8:RC
875 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
876 | .IOS ldr BASE, L->base
877 | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here.
878 | ldr PC, [BASE, FRAME_PC]
879 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
882 |//-- Argument coercion for 'for' statement ------------------------------
889 | bl extern lj_meta_for // (lua_State *L, TValue *base)
890 | .IOS ldr BASE, L->base
905 |//-----------------------------------------------------------------------
906 |//-- Fast functions -----------------------------------------------------
907 |//-----------------------------------------------------------------------
913 |.macro .ffunc_1, name
915 | ldrd CARG12, [BASE]
920 |.macro .ffunc_2, name
922 | ldrd CARG12, [BASE]
923 | ldrd CARG34, [BASE, #8]
928 |.macro .ffunc_n, name
930 | checktp CARG2, LJ_TISNUM
934 |.macro .ffunc_nn, name
936 | checktp CARG2, LJ_TISNUM
937 | cmnlo CARG4, #-LJ_TISNUM
941 |.macro .ffunc_d, name
943 | ldr CARG2, [BASE, #4]
947 | checktp CARG2, LJ_TISNUM
951 |.macro .ffunc_dd, name
953 | ldr CARG2, [BASE, #4]
954 | ldr CARG4, [BASE, #12]
957 | vldr d1, [BASE, #8]
959 | checktp CARG2, LJ_TISNUM
960 | cmnlo CARG4, #-LJ_TISNUM
964 |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
966 | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
967 | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
972 |//-- Base library: checks -----------------------------------------------
975 | checktp CARG2, LJ_TTRUE
977 | ldr PC, [BASE, FRAME_PC]
978 | strd CARG12, [BASE, #-8]
980 | subs RA, NARGS8:RC, #8
981 | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
982 | beq ->fff_res // Done if exactly 1 argument.
984 | ldrd CARG12, [RB, #8]
986 | strd CARG12, [RB], #8
991 | ldr CARG2, [BASE, #4]
994 | checktp CARG2, LJ_TISNUM
995 | mvnlo CARG2, #~LJ_TISNUM
996 | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
997 | lsl CARG4, CARG4, #3
998 | ldrd CARG12, [CFUNC:CARG3, CARG4]
1001 |//-- Base library: getters and setters ---------------------------------
1003 |.ffunc_1 getmetatable
1004 | checktp CARG2, LJ_TTAB
1005 | cmnne CARG2, #-LJ_TUDATA
1007 |1: // Field metatable must be at same offset for GCtab and GCudata!
1008 | ldr TAB:RB, TAB:CARG1->metatable
1010 | mvn CARG2, #~LJ_TNIL
1011 | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
1014 | ldr CARG3, TAB:RB->hmask
1015 | ldr CARG4, STR:RC->sid
1016 | ldr NODE:INS, TAB:RB->node
1017 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
1018 | add CARG3, CARG3, CARG3, lsl #1
1019 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
1020 |3: // Rearranged logic, because we expect _not_ to find the key.
1021 | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS.
1022 | ldrd CARG12, NODE:INS->val
1023 | ldr NODE:INS, NODE:INS->next
1024 | checktp CARG4, LJ_TSTR
1025 | cmpeq CARG3, STR:RC
1030 | mov CARG1, RB // Use metatable as default result.
1031 | mvn CARG2, #~LJ_TTAB
1034 | checktp CARG2, LJ_TNIL
1039 | checktp CARG2, LJ_TISNUM
1040 | mvnhs CARG2, CARG2
1041 | movlo CARG2, #~LJ_TISNUM
1042 | add CARG4, DISPATCH, CARG2, lsl #2
1043 | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1046 |.ffunc_2 setmetatable
1047 | // Fast path: no mt for table yet and not clearing the mt.
1048 | checktp CARG2, LJ_TTAB
1049 | ldreq TAB:RB, TAB:CARG1->metatable
1050 | checktpeq CARG4, LJ_TTAB
1051 | ldrbeq CARG4, TAB:CARG1->marked
1053 | bne ->fff_fallback
1054 | tst CARG4, #LJ_GC_BLACK // isblack(table)
1055 | str TAB:CARG3, TAB:CARG1->metatable
1057 | barrierback TAB:CARG1, CARG4, CARG3
1061 | ldrd CARG34, [BASE]
1062 | cmp NARGS8:RC, #16
1063 | blo ->fff_fallback
1065 | checktab CARG4, ->fff_fallback
1067 | add CARG3, BASE, #8
1069 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1070 | // Returns cTValue *.
1072 | ldrd CARG12, [CRET1]
1075 |//-- Base library: conversions ------------------------------------------
1078 | // Only handles the number case inline (without a base argument).
1079 | ldrd CARG12, [BASE]
1081 | bne ->fff_fallback
1082 | checktp CARG2, LJ_TISNUM
1087 | // Only handles the string or number case inline.
1088 | checktp CARG2, LJ_TSTR
1089 | // A __tostring method in the string base metatable is ignored.
1091 | // Handle numbers inline, unless a number base metatable is present.
1092 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])]
1094 | checktp CARG2, LJ_TISNUM
1096 | str PC, SAVE_PC // Redundant (but a defined value).
1097 | bhi ->fff_fallback
1101 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1102 | // Returns GCstr *.
1104 | mvn CARG2, #~LJ_TSTR
1107 |//-- Base library: iterators -------------------------------------------
1110 | mvn CARG4, #~LJ_TNIL
1111 | checktab CARG2, ->fff_fallback
1112 | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil.
1113 | ldr PC, [BASE, FRAME_PC]
1114 | add CARG2, BASE, #8
1115 | sub CARG3, BASE, #8
1116 | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1117 | // Returns 1=found, 0=end, -1=error.
1118 | .IOS ldr BASE, L->base
1121 | bgt ->fff_res // Found key/value.
1122 | bmi ->fff_fallback // Invalid key.
1123 | // End of traversal: return nil.
1124 | mvn CRET2, #~LJ_TNIL
1128 | checktab CARG2, ->fff_fallback
1130 | ldr TAB:RB, TAB:CARG1->metatable
1132 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1133 | ldr PC, [BASE, FRAME_PC]
1136 | bne ->fff_fallback
1138 | mvn CARG2, #~LJ_TNIL
1140 | strd CFUNC:CARG34, [BASE, #-8]
1141 | str CARG2, [BASE, #12]
1144 |.ffunc_2 ipairs_aux
1145 | checktp CARG2, LJ_TTAB
1146 | checktpeq CARG4, LJ_TISNUM
1147 | bne ->fff_fallback
1148 | ldr RB, TAB:CARG1->asize
1149 | ldr RC, TAB:CARG1->array
1150 | add CARG3, CARG3, #1
1151 | ldr PC, [BASE, FRAME_PC]
1153 | add RC, RC, CARG3, lsl #3
1154 | strd CARG34, [BASE, #-8]
1155 | ldrdlo CARG12, [RC]
1157 | bhs >2 // Not in array part?
1159 | checktp CARG2, LJ_TNIL
1160 | movne RC, #(2+1)*8
1161 | strdne CARG12, [BASE]
1163 |2: // Check for empty hash part first. Otherwise call C function.
1164 | ldr RB, TAB:CARG1->hmask
1169 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1170 | // Returns cTValue * or NULL.
1174 | ldrd CARG12, [CRET1]
1178 | checktab CARG2, ->fff_fallback
1180 | ldr TAB:RB, TAB:CARG1->metatable
1182 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1183 | ldr PC, [BASE, FRAME_PC]
1186 | bne ->fff_fallback
1189 | mvn CARG2, #~LJ_TISNUM
1191 | strd CFUNC:CARG34, [BASE, #-8]
1192 | strd CARG12, [BASE, #8]
1195 |//-- Base library: catch errors ----------------------------------------
1198 | ldr RB, L->maxstack
1199 | add INS, BASE, NARGS8:RC
1200 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1203 | blo ->fff_fallback
1204 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1206 | add BASE, BASE, #8
1207 | moveq PC, #8+FRAME_PCALL
1208 | movne PC, #8+FRAME_PCALLH
1209 | sub NARGS8:RC, NARGS8:RC, #8
1210 | b ->vm_call_dispatch
1213 | ldr RB, L->maxstack
1214 | add INS, BASE, NARGS8:RC
1215 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1217 | blo ->fff_fallback
1218 | checkfunc CARG4, ->fff_fallback // Traceback must be a function.
1220 | strd CARG12, [BASE, #8] // Swap function and traceback.
1221 | strd CARG34, [BASE]
1222 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1223 | add BASE, BASE, #16
1224 | moveq PC, #16+FRAME_PCALL
1225 | movne PC, #16+FRAME_PCALLH
1226 | sub NARGS8:RC, NARGS8:RC, #16
1227 | b ->vm_call_dispatch
1229 |//-- Coroutine library --------------------------------------------------
1231 |.macro coroutine_resume_wrap, resume
1233 |.ffunc_1 coroutine_resume
1234 | checktp CARG2, LJ_TTHREAD
1235 | bne ->fff_fallback
1237 |.ffunc coroutine_wrap_aux
1238 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1240 | ldr PC, [BASE, FRAME_PC]
1242 | ldr CARG2, L:CARG1->top
1243 | ldrb RA, L:CARG1->status
1244 | ldr RB, L:CARG1->base
1245 | add CARG3, CARG2, NARGS8:RC
1246 | add CARG4, CARG2, RA
1249 | beq ->fff_fallback
1250 | ldr CARG4, L:CARG1->maxstack
1251 | ldr RB, L:CARG1->cframe
1252 | cmp RA, #LUA_YIELD
1253 | cmpls CARG3, CARG4
1255 | bhi ->fff_fallback
1258 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1259 | add BASE, BASE, #8
1260 | sub NARGS8:RC, NARGS8:RC, #8
1262 | str CARG3, L:CARG1->top
1264 |2: // Move args to coroutine.
1265 | ldrd CARG34, [BASE, RB]
1267 | strdne CARG34, [CARG2, RB]
1274 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1275 | // Returns thread status.
1277 | ldr CARG3, L:RA->base
1278 | mv_vmstate CARG2, INTERP
1279 | ldr CARG4, L:RA->top
1280 | cmp CRET1, #LUA_YIELD
1282 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
1285 | subs RC, CARG4, CARG3
1286 | ldr CARG1, L->maxstack
1287 | add CARG2, BASE, RC
1288 | beq >6 // No results?
1291 | bhi >9 // Need to grow stack?
1294 | str CARG3, L:RA->top // Clear coroutine stack.
1295 |5: // Move results from coroutine.
1296 | ldrd CARG12, [CARG3, RB]
1298 | strd CARG12, [BASE, RB]
1303 | mvn CARG3, #~LJ_TTRUE
1306 | str CARG3, [BASE, #-4] // Prepend true/false to results.
1312 | ands CARG1, PC, #FRAME_TYPE
1314 | str RC, SAVE_MULTRES
1318 |8: // Coroutine returned with error (at co->top-1).
1320 | ldrd CARG12, [CARG4, #-8]!
1321 | mvn CARG3, #~LJ_TFALSE
1323 | str CARG4, L:RA->top // Remove error from coroutine stack.
1324 | strd CARG12, [BASE] // Copy error message.
1329 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1333 |9: // Handle stack expansion on return from yield.
1336 | bl extern lj_state_growstack // (lua_State *L, int n)
1341 | coroutine_resume_wrap 1 // coroutine.resume
1342 | coroutine_resume_wrap 0 // coroutine.wrap
1344 |.ffunc coroutine_yield
1345 | ldr CARG1, L->cframe
1346 | add CARG2, BASE, NARGS8:RC
1348 | tst CARG1, #CFRAME_RESUME
1350 | mov CRET1, #LUA_YIELD
1352 | beq ->fff_fallback
1353 | str CARG3, L->cframe
1354 | strb CRET1, L->status
1357 |//-- Math library -------------------------------------------------------
1359 |.macro math_round, func
1360 | .ffunc_1 math_ .. func
1361 | checktp CARG2, LJ_TISNUM
1363 | bhi ->fff_fallback
1364 | // Round FP value and normalize result.
1365 | lsl CARG3, CARG2, #1
1366 | adds RB, CARG3, #0x00200000
1367 | bpl >2 // |x| < 1?
1369 | subs RB, CARG4, RB, asr #21
1370 | lsl CARG4, CARG2, #11
1371 | lsl CARG3, CARG1, #11
1372 | orr CARG4, CARG4, #0x80000000
1374 | orr CARG4, CARG4, CARG1, lsr #21
1375 | bls >3 // |x| >= 2^31?
1376 | orr CARG3, CARG3, CARG4, lsl INS
1377 | lsr CARG1, CARG4, RB
1378 |.if "func" == "floor"
1379 | tst CARG3, CARG2, asr #31
1380 | addne CARG1, CARG1, #1
1382 | bics CARG3, CARG3, CARG2, asr #31
1383 | addsne CARG1, CARG1, #1
1388 | rsblt CARG1, CARG1, #0
1390 | mvn CARG2, #~LJ_TISNUM
1394 | bcs ->fff_restv // |x| is not finite.
1395 | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo
1396 |.if "func" == "floor"
1397 | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1
1401 | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1
1405 | mvn CARG2, #~LJ_TISNUM
1408 |3: // |x| >= 2^31. Check for x == -(2^31).
1409 | cmpeq CARG4, #0x80000000
1410 |.if "func" == "floor"
1415 | movmi CARG1, #0x80000000
1418 | bl ->vm_..func.._sf
1427 | .long 0x00000000, 0x41e00000 // 2^31.
1430 | checktp CARG2, LJ_TISNUM
1431 | bhi ->fff_fallback
1432 | bicne CARG2, CARG2, #0x80000000
1435 | rsbslt CARG1, CARG1, #0
1440 | // CARG12 = TValue result.
1441 | ldr PC, [BASE, FRAME_PC]
1442 | strd CARG12, [BASE, #-8]
1447 | // RC = (nresults+1)*8, PC = return.
1448 | ands CARG1, PC, #FRAME_TYPE
1449 | ldreq INS, [PC, #-4]
1450 | str RC, SAVE_MULTRES
1453 | decode_RB8 RB, INS
1455 | cmp RB, RC // More results expected?
1457 | decode_RA8 CARG1, INS
1460 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1461 | sub BASE, RA, CARG1
1464 |6: // Fill up results with nil.
1466 | mvn CARG1, #~LJ_TNIL
1468 | str CARG1, [CARG2, #-4]
1471 |.macro math_extern, func
1473 | .ffunc_d math_ .. func
1475 | .ffunc_n math_ .. func
1487 |.macro math_extern2, func
1489 | .ffunc_dd math_ .. func
1491 | .ffunc_nn math_ .. func
1504 | .ffunc_d math_sqrt
1507 | ldr PC, [BASE, FRAME_PC]
1508 | vstr d0, [BASE, #-8]
1516 | ldr CARG2, [BASE, #4]
1517 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1519 | bne ->fff_fallback
1521 | ldrd CARG12, [BASE]
1522 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1523 | bne ->fff_fallback
1525 | checktp CARG2, LJ_TISNUM
1526 | bhs ->fff_fallback
1548 | math_extern2 atan2
1553 | ldr CARG4, [BASE, #4]
1554 | ldrd CARG12, [BASE, #8]
1555 | cmp NARGS8:RC, #16
1556 | blo ->fff_fallback
1558 | checktp CARG4, LJ_TISNUM
1559 | bhs ->fff_fallback
1560 | checktp CARG2, LJ_TISNUM
1561 | bne ->fff_fallback
1563 | bl extern ldexp // (double x, int exp)
1567 |.ffunc_2 math_ldexp
1568 | checktp CARG2, LJ_TISNUM
1569 | bhs ->fff_fallback
1570 | checktp CARG4, LJ_TISNUM
1571 | bne ->fff_fallback
1573 | bl extern ldexp // (double x, int exp)
1579 |.ffunc_d math_frexp
1585 | mvn CARG4, #~LJ_TISNUM
1586 | ldr PC, [BASE, FRAME_PC]
1587 | vstr d0, [BASE, #-8]
1589 | strd CARG34, [BASE]
1592 |.ffunc_n math_frexp
1598 | mvn CARG4, #~LJ_TISNUM
1599 | ldr PC, [BASE, FRAME_PC]
1600 | strd CARG12, [BASE, #-8]
1602 | strd CARG34, [BASE]
1608 | sub CARG1, BASE, #8
1609 | ldr PC, [BASE, FRAME_PC]
1618 | sub CARG3, BASE, #8
1619 | ldr PC, [BASE, FRAME_PC]
1624 | strd CARG12, [BASE]
1628 |.macro math_minmax, name, cond, fcond
1632 | checktp CARG2, LJ_TISNUM
1635 |1: // Handle integers.
1639 | checktp CARG4, LJ_TISNUM
1643 | mov..cond CARG1, CARG3
1645 |3: // Convert intermediate result to number and continue below.
1647 | bhi ->fff_fallback
1649 | vcvt.f64.s32 d0, s4
1654 | bhi ->fff_fallback
1655 |5: // Handle numbers.
1660 | checktp CARG4, LJ_TISNUM
1666 | vmov..fcond.f64 d0, d1
1668 |7: // Convert integer to number and continue above.
1670 | bhi ->fff_fallback
1671 | vcvt.f64.s32 d1, s4
1677 | checktp CARG2, LJ_TISNUM
1680 |1: // Handle integers.
1681 | ldrd CARG34, [BASE, RA]
1684 | checktp CARG4, LJ_TISNUM
1688 | mov..cond CARG1, CARG3
1690 |3: // Convert intermediate result to number and continue below.
1691 | bhi ->fff_fallback
1692 | bl extern __aeabi_i2d
1693 | ldrd CARG34, [BASE, RA]
1697 | bhi ->fff_fallback
1698 |5: // Handle numbers.
1699 | ldrd CARG34, [BASE, RA]
1702 | checktp CARG4, LJ_TISNUM
1705 | bl extern __aeabi_cdcmple
1707 | mov..fcond CARG1, CARG3
1708 | mov..fcond CARG2, CARG4
1710 |7: // Convert integer to number and continue above.
1711 | bhi ->fff_fallback
1714 | bl extern __aeabi_i2d
1720 | math_minmax math_min, gt, pl
1721 | math_minmax math_max, lt, le
1723 |//-- String library -----------------------------------------------------
1725 |.ffunc string_byte // Only handle the 1-arg case here.
1726 | ldrd CARG12, [BASE]
1727 | ldr PC, [BASE, FRAME_PC]
1729 | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument.
1730 | bne ->fff_fallback
1731 | ldr CARG3, STR:CARG1->len
1732 | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
1733 | mvn CARG2, #~LJ_TISNUM
1735 | moveq RC, #(0+1)*8
1736 | movne RC, #(1+1)*8
1737 | strd CARG12, [BASE, #-8]
1740 |.ffunc string_char // Only handle the 1-arg case here.
1742 | ldrd CARG12, [BASE]
1743 | ldr PC, [BASE, FRAME_PC]
1744 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1745 | checktpeq CARG2, LJ_TISNUM
1746 | bicseq CARG4, CARG1, #255
1748 | bne ->fff_fallback
1750 | mov CARG2, TMPDp // Points to stack. Little-endian.
1752 | // CARG2 = str, CARG3 = len.
1756 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1758 | // Returns GCstr *.
1760 | mvn CARG2, #~LJ_TSTR
1765 | ldrd CARG12, [BASE]
1766 | ldrd CARG34, [BASE, #16]
1767 | cmp NARGS8:RC, #16
1770 | blo ->fff_fallback
1771 | checktp CARG4, LJ_TISNUM
1773 | bne ->fff_fallback
1775 | ldrd CARG34, [BASE, #8]
1776 | checktp CARG2, LJ_TSTR
1777 | ldreq CARG2, STR:CARG1->len
1778 | checktpeq CARG4, LJ_TISNUM
1779 | bne ->fff_fallback
1780 | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1781 | add CARG4, CARG2, #1
1782 | cmp CARG3, #0 // if (start < 0) start += len+1
1783 | addlt CARG3, CARG3, CARG4
1784 | cmp CARG3, #1 // if (start < 1) start = 1
1786 | cmp RB, #0 // if (end < 0) end += len+1
1787 | addlt RB, RB, CARG4
1788 | bic RB, RB, RB, asr #31 // if (end < 0) end = 0
1789 | cmp RB, CARG2 // if (end > len) end = len
1790 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1792 | add CARG2, CARG1, CARG3
1793 | subs CARG3, RB, CARG3 // len = end - start
1794 | add CARG3, CARG3, #1 // len += 1
1797 | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1798 | mvn CARG2, #~LJ_TSTR
1801 |.macro ffstring_op, name
1802 | .ffunc string_ .. name
1804 | ldr CARG3, [BASE, #4]
1806 | ldr STR:CARG2, [BASE]
1807 | blo ->fff_fallback
1808 | sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf)
1809 | checkstr CARG3, ->fff_fallback
1810 | ldr CARG4, SBUF:CARG1->b
1813 | str L, SBUF:CARG1->L
1814 | str CARG4, SBUF:CARG1->w
1815 | bl extern lj_buf_putstr_ .. name
1816 | bl extern lj_buf_tostr
1820 |ffstring_op reverse
1824 |//-- Bit library --------------------------------------------------------
1826 |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1828 | bhi ->fff_fallback
1831 | adds RB, RB, #0x00200000
1832 | movpl CARG1, #0 // |x| < 1?
1835 | subs RB, CARG4, RB, asr #21
1836 | bmi >1 // |x| >= 2^32?
1837 | lsl CARG4, CARG2, #11
1838 | orr CARG4, CARG4, #0x80000000
1839 | orr CARG4, CARG4, CARG1, lsr #21
1841 | lsr CARG1, CARG4, RB
1842 | rsblt CARG1, CARG1, #0
1846 | lsr CARG4, CARG1, RB
1848 | lsl CARG1, CARG2, #12
1850 | orr CARG1, CARG4, CARG1, lsl RB
1851 | rsblt CARG1, CARG1, #0
1854 |.macro .ffunc_bit, name
1855 | .ffunc_1 bit_..name
1856 | checktp CARG2, LJ_TISNUM
1857 | blne ->vm_tobit_fb
1861 | mvn CARG2, #~LJ_TISNUM
1864 |.macro .ffunc_bit_op, name, ins
1869 | ldrd CARG12, [BASE, RA]
1873 | checktp CARG2, LJ_TISNUM
1874 | blne ->vm_tobit_fb
1875 | ins CARG3, CARG3, CARG1
1879 |.ffunc_bit_op band, and
1880 |.ffunc_bit_op bor, orr
1881 |.ffunc_bit_op bxor, eor
1884 | mvn CARG4, #~LJ_TISNUM
1885 | ldr PC, [BASE, FRAME_PC]
1886 | strd CARG34, [BASE, #-8]
1890 | eor CARG3, CARG1, CARG1, ror #16
1891 | bic CARG3, CARG3, #0x00ff0000
1892 | ror CARG1, CARG1, #8
1893 | mvn CARG2, #~LJ_TISNUM
1894 | eor CARG1, CARG1, CARG3, lsr #8
1899 | mvn CARG2, #~LJ_TISNUM
1902 |.macro .ffunc_bit_sh, name, ins, shmod
1904 | ldrd CARG12, [BASE, #8]
1905 | cmp NARGS8:RC, #16
1906 | blo ->fff_fallback
1907 | checktp CARG2, LJ_TISNUM
1908 | blne ->vm_tobit_fb
1910 | and RA, CARG1, #31
1914 | ldrd CARG12, [BASE]
1915 | checktp CARG2, LJ_TISNUM
1916 | blne ->vm_tobit_fb
1917 | ins CARG1, CARG1, RA
1918 | mvn CARG2, #~LJ_TISNUM
1922 |.ffunc_bit_sh lshift, lsl, 0
1923 |.ffunc_bit_sh rshift, lsr, 0
1924 |.ffunc_bit_sh arshift, asr, 0
1925 |.ffunc_bit_sh rol, ror, 1
1926 |.ffunc_bit_sh ror, ror, 0
1928 |//-----------------------------------------------------------------------
1930 |->fff_fallback: // Call fast function fallback handler.
1931 | // BASE = new base, RC = nargs*8
1932 | ldr CARG3, [BASE, FRAME_FUNC]
1933 | ldr CARG2, L->maxstack
1934 | add CARG1, BASE, NARGS8:RC
1935 | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC.
1937 | ldr CARG3, CFUNC:CARG3->f
1939 | add CARG1, CARG1, #8*LUA_MINSTACK
1940 | str PC, SAVE_PC // Redundant (but a defined value).
1943 | bhi >5 // Need to grow stack.
1944 | blx CARG3 // (lua_State *L)
1945 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1950 | bgt ->fff_res // Returned nresults+1?
1951 |1: // Returned 0 or -1: retry fast path.
1953 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1954 | sub NARGS8:RC, CARG1, BASE
1955 | bne ->vm_call_tail // Returned -1?
1956 | ins_callt // Returned 0: retry fast path.
1958 |// Reconstruct previous base for vmeta_call during tailcall.
1960 | ands CARG1, PC, #FRAME_TYPE
1961 | bic CARG2, PC, #FRAME_TYPEP
1962 | ldreq INS, [PC, #-4]
1963 | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8.
1964 | addeq CARG2, CARG2, #8
1965 | sub RB, BASE, CARG2
1966 | b ->vm_call_dispatch // Resolve again for tailcall.
1968 |5: // Grow stack for fallback handler.
1969 | mov CARG2, #LUA_MINSTACK
1970 | bl extern lj_state_growstack // (lua_State *L, int n)
1972 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1975 |->fff_gcstep: // Call GC step function.
1976 | // BASE = new base, RC = nargs*8
1979 | add CARG2, BASE, NARGS8:RC
1980 | str PC, SAVE_PC // Redundant (but a defined value).
1983 | bl extern lj_gc_step // (lua_State *L)
1985 | mov lr, RA // Help return address predictor.
1986 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1989 |//-----------------------------------------------------------------------
1990 |//-- Special dispatch targets -------------------------------------------
1991 |//-----------------------------------------------------------------------
1993 |->vm_record: // Dispatch target for recording phase.
1995 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
1996 | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent.
1998 | // Decrement the hookcount for consistency, but always do the call.
1999 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2000 | tst CARG1, #HOOK_ACTIVE
2002 | sub CARG2, CARG2, #1
2003 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2004 | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2008 |->vm_rethook: // Dispatch target for return hooks.
2009 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2010 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2012 |5: // Re-dispatch to static ins.
2014 | add OP, DISPATCH, OP, lsl #2
2015 | ldr pc, [OP, #GG_DISP2STATIC]
2017 |->vm_inshook: // Dispatch target for instr/line hooks.
2018 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2019 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2020 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2022 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2024 | subs CARG2, CARG2, #1
2025 | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2027 | tst CARG1, #LUA_MASKLINE
2033 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2034 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2037 |4: // Re-dispatch to static ins.
2038 | ldrb OP, [PC, #-4]
2039 | ldr INS, [PC, #-4]
2040 | add OP, DISPATCH, OP, lsl #2
2041 | ldr OP, [OP, #GG_DISP2STATIC]
2042 | decode_RA8 RA, INS
2046 |->cont_hook: // Continue from hook yield.
2047 | ldr CARG1, [CARG4, #-24]
2049 | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins.
2052 |->vm_hotloop: // Hot loop counter underflow.
2054 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L).
2055 | sub CARG1, DISPATCH, #-GG_DISP2J
2057 | ldr CARG3, LFUNC:CARG3->field_pc
2059 | str L, [DISPATCH, #DISPATCH_J(L)]
2060 | ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2062 | add CARG3, BASE, CARG3, lsl #3
2064 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2068 |->vm_callhook: // Dispatch target for call hooks.
2074 |->vm_hotcall: // Hot call counter underflow.
2079 | add CARG4, BASE, RC
2085 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2086 | // Returns ASMFunction.
2091 | sub NARGS8:RC, CARG4, BASE
2092 | str CARG2, SAVE_PC // Invalidate for subsequent line hook.
2093 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2094 | ldr INS, [PC, #-4]
2097 |->cont_stitch: // Trace stitching.
2099 | // RA = resultptr, CARG4 = meta base
2100 | ldr RB, SAVE_MULTRES
2101 | ldr INS, [PC, #-4]
2102 | ldr TRACE:CARG3, [CARG4, #-24] // Save previous trace.
2104 | decode_RA8 RC, INS // Call base.
2106 |1: // Move results down.
2110 | strd CARG12, [BASE, RC]
2114 | decode_RA8 RA, INS
2115 | decode_RB8 RB, INS
2119 | mvn CARG2, #~LJ_TNIL
2120 | bhi >9 // More results wanted?
2122 | ldrh RA, TRACE:CARG3->traceno
2123 | ldrh RC, TRACE:CARG3->link
2125 | beq ->cont_nop // Blacklisted.
2127 | bne =>BC_JLOOP // Jump to stitched trace.
2129 | // Stitch a new trace to the previous trace.
2130 | str RA, [DISPATCH, #DISPATCH_J(exitno)]
2131 | str L, [DISPATCH, #DISPATCH_J(L)]
2133 | sub CARG1, DISPATCH, #-GG_DISP2J
2135 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2139 |9: // Fill up results with nil.
2140 | strd CARG12, [BASE, RC]
2145 |->vm_profhook: // Dispatch target for profiler hook.
2150 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2151 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2157 |//-----------------------------------------------------------------------
2158 |//-- Trace exit handler -------------------------------------------------
2159 |//-----------------------------------------------------------------------
2164 | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2165 | ldr CARG1, [sp, #64] // Load original value of lr.
2166 | ldr DISPATCH, [lr] // Load DISPATCH.
2167 | add CARG3, sp, #64 // Recompute original value of sp.
2168 | mv_vmstate CARG4, EXIT
2169 | str CARG3, [sp, #52] // Store sp in RID_SP
2171 | ldr CARG2, [CARG1, #-4]! // Get exit instruction.
2172 | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC.
2173 | str CARG1, [sp, #60]
2177 | lsl CARG2, CARG2, #8
2178 | add CARG1, CARG1, CARG2, asr #6
2179 | ldr CARG2, [lr, #4] // Load exit stub group offset.
2180 | sub CARG1, CARG1, lr
2181 | ldr L, [DISPATCH, #DISPATCH_GL(cur_L)]
2182 | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number.
2183 | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2184 | str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2187 | str L, [DISPATCH, #DISPATCH_J(L)]
2188 | str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)]
2189 | sub CARG1, DISPATCH, #-GG_DISP2J
2191 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2192 | // Returns MULTRES (unscaled) or negated error code.
2193 | ldr CARG2, L->cframe
2195 | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2197 | ldr PC, SAVE_PC // Get SAVE_PC.
2198 | str L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2202 | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2206 | cmn CARG1, #LUA_ERRERR
2207 | bhs >9 // Check for error from exit.
2209 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2210 | str RC, SAVE_MULTRES
2213 | ldr CARG2, LFUNC:CARG2->field_pc
2214 | str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)]
2215 | mv_vmstate CARG4, INTERP
2216 | ldr KBASE, [CARG2, #PC2PROTO(k)]
2217 | // Modified copy of ins_next which handles function header dispatch, too.
2221 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2223 | cmn CARG1, #17 // Static dispatch?
2225 | cmp OP, #BC_FUNCC+2 // Fast function?
2228 | cmp OP, #BC_FUNCF // Function header?
2229 | ldr OP, [DISPATCH, OP, lsl #2]
2230 | decode_RA8 RA, INS
2231 | lsrlo RC, INS, #16 // No: Decode operands A*8 and D.
2233 | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8
2234 | ldrhs CARG3, [BASE, FRAME_FUNC]
2237 |4: // Check frame below fast function.
2238 | ldr CARG1, [BASE, FRAME_PC]
2239 | ands CARG2, CARG1, #FRAME_TYPE
2240 | bne <2 // Trace stitching continuation?
2241 | // Otherwise set KBASE for Lua function below fast function.
2242 | ldr CARG3, [CARG1, #-4]
2243 | decode_RA8 CARG1, CARG3
2244 | sub CARG2, BASE, CARG1
2245 | ldr LFUNC:CARG3, [CARG2, #-16]
2246 | ldr CARG3, LFUNC:CARG3->field_pc
2247 | ldr KBASE, [CARG3, #PC2PROTO(k)]
2250 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2251 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
2253 | ldr TRACE:CARG1, [CARG1, RC, lsl #2]
2254 | ldr INS, TRACE:CARG1->startins
2256 | decode_RA8 RA, INS
2257 | add OP, DISPATCH, OP, lsl #2
2259 | ldr pc, [OP, #GG_DISP2STATIC]
2261 |9: // Rethrow error from the right C frame.
2262 | rsb CARG2, CARG1, #0
2264 | bl extern lj_err_trace // (lua_State *L, int errcode)
2267 |//-----------------------------------------------------------------------
2268 |//-- Math helper functions ----------------------------------------------
2269 |//-----------------------------------------------------------------------
2271 |// FP value rounding. Called from JIT code.
2273 |// double lj_vm_floor/ceil/trunc(double x);
2274 |.macro vm_round, func, hf
2276 | vmov CARG1, CARG2, d0
2278 | lsl CARG3, CARG2, #1
2279 | adds RB, CARG3, #0x00200000
2280 | bpl >2 // |x| < 1?
2282 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2283 | bxlo lr // |x| >= 2^52: done.
2285 | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask
2286 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2288 | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2289 | orrpl CARG3, CARG3, CARG4
2291 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2292 |.if "func" == "floor"
2293 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2295 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2298 | vmoveq d0, CARG1, CARG2
2300 | bxeq lr // iszero: done.
2303 | lslpl CARG3, CARG4, RB
2306 | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask
2307 | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry
2309 | vmov d0, CARG1, CARG2
2314 | bxcs lr // |x| is not finite.
2315 | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo
2316 |.if "func" == "floor"
2317 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2319 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2321 | mov CARG1, #0 // lo = 0
2322 | and CARG2, CARG2, #0x80000000
2323 | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2324 | orrne CARG2, CARG2, CARG4
2326 | vmov d0, CARG1, CARG2
2332 | .long 0x3ff00000 // hiword(+1.0)
2348 |.macro vm_trunc, hf
2351 | vmov CARG1, CARG2, d0
2353 | lsl CARG3, CARG2, #1
2354 | adds RB, CARG3, #0x00200000
2355 | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0.
2358 | vmovpl d0, CARG1, CARG2
2362 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2363 | bxlo lr // |x| >= 2^52: already done.
2365 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2367 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2369 | vmov d0, CARG1, CARG2
2382 | // double lj_vm_mod(double dividend, double divisor);
2385 | // Special calling convention. Also, RC (r11) is not preserved.
2386 | vdiv.f64 d0, d6, d7
2388 | vmov CARG1, CARG2, d0
2390 | vmov d0, CARG1, CARG2
2391 | vmul.f64 d0, d0, d7
2393 | vsub.f64 d6, d6, d0
2396 | push {r0, r1, r2, r3, r4, lr}
2397 | bl extern __aeabi_ddiv
2399 | ldrd CARG34, [sp, #8]
2400 | bl extern __aeabi_dmul
2402 | eor CARG2, CARG2, #0x80000000
2403 | bl extern __aeabi_dadd
2408 | // int lj_vm_modi(int dividend, int divisor);
2410 | ands RB, CARG1, #0x80000000
2411 | rsbmi CARG1, CARG1, #0 // a = |dividend|
2412 | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor).
2414 | rsbmi CARG2, CARG2, #0 // b = |divisor|
2415 | subs CARG4, CARG2, #1
2416 | cmpne CARG1, CARG2
2417 | moveq CARG1, #0 // if (b == 1 || a == b) a = 0
2418 | tsthi CARG2, CARG4
2419 | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1
2421 | // Use repeated subtraction to get the remainder.
2424 | sub CARG4, CARG4, CARG3
2425 | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8
2426 | addne pc, pc, CARG3, lsl #3 // Duff's device.
2430 for (i = 31; i >= 0; i--) {
2431 | cmp CARG1, CARG2, lsl #i
2432 | subhs CARG1, CARG1, CARG2, lsl #i
2438 | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b
2439 | eors CARG2, CARG1, RB, lsl #1
2440 | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y
2443 |//-----------------------------------------------------------------------
2444 |//-- Miscellaneous functions --------------------------------------------
2445 |//-----------------------------------------------------------------------
2447 |.define NEXT_TAB, TAB:CARG1
2448 |.define NEXT_RES, CARG1
2449 |.define NEXT_IDX, CARG2
2450 |.define NEXT_TMP0, CARG3
2451 |.define NEXT_TMP1, CARG4
2452 |.define NEXT_LIM, r12
2453 |.define NEXT_RES_PTR, sp
2454 |.define NEXT_RES_VAL, [sp]
2455 |.define NEXT_RES_KEY_I, [sp, #8]
2456 |.define NEXT_RES_KEY_IT, [sp, #12]
2458 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2459 |// Next idx returned in CRET2.
2462 | ldr NEXT_TMP0, NEXT_TAB->array
2463 | ldr NEXT_LIM, NEXT_TAB->asize
2464 | add NEXT_TMP0, NEXT_TMP0, NEXT_IDX, lsl #3
2465 |1: // Traverse array part.
2466 | subs NEXT_TMP1, NEXT_IDX, NEXT_LIM
2468 | ldr NEXT_TMP1, [NEXT_TMP0, #4]
2469 | str NEXT_IDX, NEXT_RES_KEY_I
2470 | add NEXT_TMP0, NEXT_TMP0, #8
2471 | add NEXT_IDX, NEXT_IDX, #1
2472 | checktp NEXT_TMP1, LJ_TNIL
2473 | beq <1 // Skip holes in array part.
2474 | ldr NEXT_TMP0, [NEXT_TMP0, #-8]
2475 | mov NEXT_RES, NEXT_RES_PTR
2476 | strd NEXT_TMP0, NEXT_RES_VAL // Stores NEXT_TMP1, too.
2477 | mvn NEXT_TMP0, #~LJ_TISNUM
2478 | str NEXT_TMP0, NEXT_RES_KEY_IT
2481 |5: // Traverse hash part.
2482 | ldr NEXT_TMP0, NEXT_TAB->hmask
2483 | ldr NODE:NEXT_RES, NEXT_TAB->node
2484 | add NEXT_TMP1, NEXT_TMP1, NEXT_TMP1, lsl #1
2485 | add NEXT_LIM, NEXT_LIM, NEXT_TMP0
2486 | add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP1, lsl #3
2488 | cmp NEXT_IDX, NEXT_LIM
2490 | ldr NEXT_TMP1, NODE:NEXT_RES->val.it
2491 | checktp NEXT_TMP1, LJ_TNIL
2492 | add NEXT_IDX, NEXT_IDX, #1
2494 | // Skip holes in hash part.
2495 | add NEXT_RES, NEXT_RES, #sizeof(Node)
2498 |9: // End of iteration. Set the key to nil (not the value).
2500 | mov NEXT_RES, NEXT_RES_PTR
2501 | str NEXT_TMP0, NEXT_RES_KEY_IT
2505 |//-----------------------------------------------------------------------
2506 |//-- FFI helper functions -----------------------------------------------
2507 |//-----------------------------------------------------------------------
2509 |// Handler for callback functions.
2510 |// Saveregs already performed. Callback slot number in [sp], g in r12.
2513 |.type CTSTATE, CTState, PC
2514 | ldr CTSTATE, GL:r12->ctype_state
2515 | add DISPATCH, r12, #GG_G2DISP
2518 | add r4, sp, CFRAME_SPACE+4+8*8
2519 | vstmdb r4!, {d8-d15}
2522 | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2524 | strd CARG34, CTSTATE->cb.gpr[2]
2525 | strd CARG12, CTSTATE->cb.gpr[0]
2527 | vstmdb r12!, {d0-d7}
2530 | add CARG3, sp, #CFRAME_SIZE
2531 | mov CARG1, CTSTATE
2532 | lsr CARG4, CARG4, #3
2533 | str CARG3, CTSTATE->cb.stack
2535 | str CARG4, CTSTATE->cb.slot
2536 | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2537 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2538 | // Returns lua_State *.
2539 | ldr BASE, L:CRET1->base
2540 | mv_vmstate CARG2, INTERP
2541 | ldr RC, L:CRET1->top
2543 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2546 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2551 |->cont_ffi_callback: // Return from FFI callback.
2553 | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2557 | mov CARG1, CTSTATE
2559 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2560 | ldrd CARG12, CTSTATE->cb.gpr[0]
2562 | vldr d0, CTSTATE->cb.fpr[0]
2567 |->vm_ffi_call: // Call C function via FFI.
2568 | // Caveat: needs special frame unwinding, see below.
2570 | .type CCSTATE, CCallState, r4
2571 | push {CCSTATE, r5, r11, lr}
2572 | mov CCSTATE, CARG1
2573 | ldr CARG1, CCSTATE:CARG1->spadj
2574 | ldrb CARG2, CCSTATE->nsp
2575 | add CARG3, CCSTATE, #offsetof(CCallState, stack)
2577 | add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2580 | sub sp, sp, CARG1 // Readjust stack.
2581 | subs CARG2, CARG2, #4
2585 | ldr RB, CCSTATE->func
2587 |1: // Copy stack slots.
2588 | ldr CARG4, [CARG3, CARG2]
2589 | str CARG4, [sp, CARG2]
2590 | subs CARG2, CARG2, #4
2593 | ldrd CARG12, CCSTATE->gpr[0]
2594 | ldrd CARG34, CCSTATE->gpr[2]
2598 | add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2600 | strd CRET1, CCSTATE->gpr[0]
2602 | vstmdb r12!, {d0-d3}
2604 | pop {CCSTATE, r5, r11, pc}
2606 |// Note: vm_ffi_call must be the last function in this object file!
2608 |//-----------------------------------------------------------------------
2611 /* Generate the code for a single instruction. */
2612 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2619 /* -- Comparison ops ---------------------------------------------------- */
2621 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2623 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2624 | // RA = src1*8, RC = src2, JMP with RC = target
2626 | ldrd CARG12, [RA, BASE]!
2628 | ldrd CARG34, [RC, BASE]!
2630 | add RB, PC, RB, lsl #2
2631 | checktp CARG2, LJ_TISNUM
2633 | checktp CARG4, LJ_TISNUM
2636 if (op == BC_ISLT) {
2637 | sublt PC, RB, #0x20000
2638 } else if (op == BC_ISGE) {
2639 | subge PC, RB, #0x20000
2640 } else if (op == BC_ISLE) {
2641 | suble PC, RB, #0x20000
2643 | subgt PC, RB, #0x20000
2648 |3: // CARG12 is not an integer.
2652 | // d0 is a number.
2653 | checktp CARG4, LJ_TISNUM
2657 | // d0 is a number, CARG3 is an integer.
2659 | vcvt.f64.s32 d1, s4
2661 |4: // CARG1 is an integer, CARG34 is not an integer.
2664 | // CARG1 is an integer, d1 is a number.
2666 | vcvt.f64.s32 d0, s4
2667 |5: // d0 and d1 are numbers.
2670 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2671 if (op == BC_ISLT) {
2672 | sublo PC, RB, #0x20000
2673 } else if (op == BC_ISGE) {
2674 | subhs PC, RB, #0x20000
2675 } else if (op == BC_ISLE) {
2676 | subls PC, RB, #0x20000
2678 | subhi PC, RB, #0x20000
2683 | // CARG12 is a number.
2684 | checktp CARG4, LJ_TISNUM
2685 | movlo RA, RB // Save RB.
2688 | // CARG12 is a number, CARG3 is an integer.
2691 | mov RA, RB // Save RB.
2692 | bl extern __aeabi_i2d
2695 | ldrd CARG12, [RC] // Restore first operand.
2697 |4: // CARG1 is an integer, CARG34 is not an integer.
2699 | // CARG1 is an integer, CARG34 is a number.
2700 | mov RA, RB // Save RB.
2701 | bl extern __aeabi_i2d
2702 | ldrd CARG34, [RC] // Restore second operand.
2703 |5: // CARG12 and CARG34 are numbers.
2704 | bl extern __aeabi_cdcmple
2705 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2706 if (op == BC_ISLT) {
2707 | sublo PC, RA, #0x20000
2708 } else if (op == BC_ISGE) {
2709 | subhs PC, RA, #0x20000
2710 } else if (op == BC_ISLE) {
2711 | subls PC, RA, #0x20000
2713 | subhi PC, RA, #0x20000
2719 case BC_ISEQV: case BC_ISNEV:
2720 vk = op == BC_ISEQV;
2721 | // RA = src1*8, RC = src2, JMP with RC = target
2723 | ldrd CARG12, [RA, BASE]!
2725 | ldrd CARG34, [RC, BASE]!
2727 | add RB, PC, RB, lsl #2
2728 | checktp CARG2, LJ_TISNUM
2729 | cmnls CARG4, #-LJ_TISNUM
2735 | // Either or both types are not numbers.
2737 | checktp CARG2, LJ_TCDATA
2738 | checktpne CARG4, LJ_TCDATA
2739 | beq ->vmeta_equal_cd
2741 | cmp CARG2, CARG4 // Compare types.
2742 | bne >2 // Not the same type?
2743 | checktp CARG2, LJ_TISPRI
2744 | bhs >1 // Same type and primitive type?
2746 | // Same types and not a primitive type. Compare GCobj or pvalue.
2749 | bne >3 // Different GCobjs or pvalues?
2750 |1: // Branch if same.
2751 | sub PC, RB, #0x20000
2755 | checktp CARG2, LJ_TISTABUD
2756 | bhi <2 // Different objects and not table/ud?
2758 | beq >1 // Same GCobjs or pvalues?
2759 | checktp CARG2, LJ_TISTABUD
2760 | bhi >2 // Different objects and not table/ud?
2762 | // Different tables or userdatas. Need to check __eq metamethod.
2763 | // Field metatable must be at same offset for GCtab and GCudata!
2764 | ldr TAB:RA, TAB:CARG1->metatable
2767 | beq <2 // No metatable?
2769 | beq >2 // No metatable?
2771 | ldrb RA, TAB:RA->nomm
2772 | mov CARG4, #1-vk // ne = 0 or 1.
2775 | beq ->vmeta_equal // 'no __eq' flag not set?
2779 |2: // Branch if different.
2780 | sub PC, RB, #0x20000
2786 case BC_ISEQS: case BC_ISNES:
2787 vk = op == BC_ISEQS;
2788 | // RA = src*8, RC = str_const (~), JMP with RC = target
2790 | ldrd CARG12, [BASE, RA]
2792 | ldr STR:CARG3, [KBASE, RC, lsl #2]
2794 | add RB, PC, RB, lsl #2
2795 | checktp CARG2, LJ_TSTR
2800 | cmpeq CARG1, CARG3
2803 | subeq PC, RB, #0x20000
2807 | subne PC, RB, #0x20000
2813 | checktp CARG2, LJ_TCDATA
2815 | b ->vmeta_equal_cd
2819 case BC_ISEQN: case BC_ISNEN:
2820 vk = op == BC_ISEQN;
2821 | // RA = src*8, RC = num_const (~), JMP with RC = target
2823 | ldrd CARG12, [RA, BASE]!
2825 | ldrd CARG34, [RC, KBASE]!
2827 | add RB, PC, RB, lsl #2
2833 | checktp CARG2, LJ_TISNUM
2835 | checktp CARG4, LJ_TISNUM
2839 | subeq PC, RB, #0x20000
2843 | subne PC, RB, #0x20000
2848 |3: // CARG12 is not an integer.
2853 | subhi PC, RB, #0x20000
2858 | checktp CARG4, LJ_TISNUM
2862 | vcvths.f64.s32 d1, s4
2864 |4: // CARG1 is an integer, d1 is a number.
2867 | vcvt.f64.s32 d0, s4
2868 |5: // d0 and d1 are numbers.
2872 | subeq PC, RB, #0x20000
2874 | subne PC, RB, #0x20000
2878 | // CARG12 is a number.
2879 | checktp CARG4, LJ_TISNUM
2880 | movlo RA, RB // Save RB.
2882 | // CARG12 is a number, CARG3 is an integer.
2885 |4: // CARG1 is an integer, CARG34 is a number.
2886 | mov RA, RB // Save RB.
2887 | bl extern __aeabi_i2d
2888 | ldrd CARG34, [RC] // Restore other operand.
2889 |5: // CARG12 and CARG34 are numbers.
2890 | bl extern __aeabi_cdcmpeq
2892 | subeq PC, RA, #0x20000
2894 | subne PC, RA, #0x20000
2901 | checktp CARG2, LJ_TCDATA
2903 | b ->vmeta_equal_cd
2907 case BC_ISEQP: case BC_ISNEP:
2908 vk = op == BC_ISEQP;
2909 | // RA = src*8, RC = primitive_type (~), JMP with RC = target
2910 | ldrd CARG12, [BASE, RA]
2914 | add RB, PC, RB, lsl #2
2916 | checktp CARG2, LJ_TCDATA
2917 | beq ->vmeta_equal_cd
2921 | subeq PC, RB, #0x20000
2923 | subne PC, RB, #0x20000
2928 /* -- Unary test and copy ops ------------------------------------------- */
2930 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2931 | // RA = dst*8 or unused, RC = src, JMP with RC = target
2932 | add RC, BASE, RC, lsl #3
2936 | add RB, PC, RB, lsl #2
2937 | checktp CARG2, LJ_TTRUE
2938 if (op == BC_ISTC || op == BC_IST) {
2939 | subls PC, RB, #0x20000
2940 if (op == BC_ISTC) {
2941 | strdls CARG12, [BASE, RA]
2944 | subhi PC, RB, #0x20000
2945 if (op == BC_ISFC) {
2946 | strdhi CARG12, [BASE, RA]
2953 | // RA = src*8, RC = -type
2954 | ldrd CARG12, [BASE, RA]
2958 | bne ->vmeta_istype
2962 | // RA = src*8, RC = -(TISNUM-1)
2963 | ldrd CARG12, [BASE, RA]
2965 | checktp CARG2, LJ_TISNUM
2967 | bhs ->vmeta_istype
2971 /* -- Unary ops --------------------------------------------------------- */
2974 | // RA = dst*8, RC = src
2977 | ldrd CARG12, [BASE, RC]
2979 | strd CARG12, [BASE, RA]
2983 | // RA = dst*8, RC = src
2984 | add RC, BASE, RC, lsl #3
2986 | ldr CARG1, [RC, #4]
2989 | checktp CARG1, LJ_TTRUE
2990 | mvnls CARG2, #~LJ_TFALSE
2991 | mvnhi CARG2, #~LJ_TTRUE
2992 | str CARG2, [RA, #4]
2996 | // RA = dst*8, RC = src
2998 | ldrd CARG12, [BASE, RC]
3001 | checktp CARG2, LJ_TISNUM
3003 | eorne CARG2, CARG2, #0x80000000
3005 | rsbseq CARG1, CARG1, #0
3008 | strd CARG12, [BASE, RA]
3013 | .long 0x00000000, 0x41e00000 // 2^31.
3016 | // RA = dst*8, RC = src
3018 | ldrd CARG12, [BASE, RC]
3019 | checkstr CARG2, >2
3020 | ldr CARG1, STR:CARG1->len
3022 | mvn CARG2, #~LJ_TISNUM
3025 | strd CARG12, [BASE, RA]
3028 | checktab CARG2, ->vmeta_len
3030 | ldr TAB:CARG3, TAB:CARG1->metatable
3037 | bl extern lj_tab_len // (GCtab *t)
3038 | // Returns uint32_t (but less than 2^31).
3043 | ldrb CARG4, TAB:CARG3->nomm
3044 | tst CARG4, #1<<MM_len
3045 | bne <3 // 'no __len' flag set: done.
3050 /* -- Binary ops -------------------------------------------------------- */
3052 |.macro ins_arithcheck, cond, ncond, target
3054 | cmn CARG4, #-LJ_TISNUM
3055 | cmn..cond CARG2, #-LJ_TISNUM
3057 | cmn CARG2, #-LJ_TISNUM
3058 | cmn..cond CARG4, #-LJ_TISNUM
3062 |.macro ins_arithcheck_int, target
3063 | ins_arithcheck eq, ne, target
3065 |.macro ins_arithcheck_num, target
3066 | ins_arithcheck lo, hs, target
3069 |.macro ins_arithpre
3070 | decode_RB8 RB, INS
3071 | decode_RC8 RC, INS
3072 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3073 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3077 | ldrd CARG12, [RB, BASE]!
3078 | ldrd CARG34, [RC, KBASE]!
3080 | ldrd CARG12, [BASE, RB]
3081 | ldrd CARG34, [KBASE, RC]
3086 | ldrd CARG34, [RB, BASE]!
3087 | ldrd CARG12, [RC, KBASE]!
3089 | ldrd CARG34, [BASE, RB]
3090 | ldrd CARG12, [KBASE, RC]
3095 | ldrd CARG12, [RB, BASE]!
3096 | ldrd CARG34, [RC, BASE]!
3098 | ldrd CARG12, [BASE, RB]
3099 | ldrd CARG34, [BASE, RC]
3105 |.macro ins_arithpre_fpu, reg1, reg2
3117 |.macro ins_arithpost_fpu, reg
3125 |.macro ins_arithfallback, ins
3128 | ins ->vmeta_arith_vn
3131 | ins ->vmeta_arith_nv
3134 | ins ->vmeta_arith_vv
3139 |.macro ins_arithdn, intins, fpins, fpcall
3141 |.if "intins" ~= "vm_modi" and not FPU
3144 | ins_arithcheck_int >5
3145 |.if "intins" == "smull"
3146 | smull CARG1, RC, CARG3, CARG1
3147 | cmp RC, CARG1, asr #31
3148 | ins_arithfallback bne
3149 |.elif "intins" == "vm_modi"
3151 | ins_arithfallback beq
3153 | mvn CARG2, #~LJ_TISNUM
3155 | intins CARG1, CARG1, CARG3
3156 | ins_arithfallback bvs
3159 |.if "intins" == "vm_modi" or FPU
3163 | strd CARG12, [BASE, RA]
3166 | ins_arithpre_fpu d6, d7
3167 | ins_arithfallback ins_arithcheck_num
3169 |.if "intins" == "vm_modi"
3174 | ins_arithpost_fpu d6
3177 |.if "intins" ~= "vm_modi"
3184 |.macro ins_arithfp, fpins, fpcall
3186 |.if "fpins" ~= "extern" or HFABI
3187 | ins_arithpre_fpu d0, d1
3189 | ins_arithfallback ins_arithcheck_num
3190 |.if "fpins" == "extern"
3199 |.if ("fpins" ~= "extern" or HFABI) and FPU
3200 | ins_arithpost_fpu d0
3204 | strd CARG12, [BASE, RA]
3209 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3210 | ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3212 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3213 | ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3215 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3216 | ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3218 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3219 | ins_arithfp vdiv.f64, extern __aeabi_ddiv
3221 case BC_MODVN: case BC_MODNV: case BC_MODVV:
3222 | ins_arithdn vm_modi, vm_mod, ->vm_mod
3225 | // NYI: (partial) integer arithmetic.
3226 | ins_arithfp extern, extern pow
3230 | decode_RB8 RC, INS
3231 | decode_RC8 RB, INS
3232 | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!)
3235 | add CARG2, BASE, RB
3237 | // RA = dst*8, RC = src_start*8, CARG2 = top-1
3240 | lsr CARG3, CARG3, #3
3241 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3242 | // Returns NULL (finished) or TValue * (metamethod).
3246 | ldrd CARG34, [BASE, RC]
3249 | strd CARG34, [BASE, RA] // Copy result to RA.
3253 /* -- Constant ops ------------------------------------------------------ */
3256 | // RA = dst*8, RC = str_const (~)
3259 | ldr CARG1, [KBASE, RC, lsl #2]
3260 | mvn CARG2, #~LJ_TSTR
3262 | strd CARG12, [BASE, RA]
3267 | // RA = dst*8, RC = cdata_const (~)
3270 | ldr CARG1, [KBASE, RC, lsl #2]
3271 | mvn CARG2, #~LJ_TCDATA
3273 | strd CARG12, [BASE, RA]
3278 | // RA = dst*8, (RC = int16_literal)
3279 | mov CARG1, INS, asr #16 // Refetch sign-extended reg.
3280 | mvn CARG2, #~LJ_TISNUM
3283 | strd CARG12, [BASE, RA]
3287 | // RA = dst*8, RC = num_const
3290 | ldrd CARG12, [KBASE, RC]
3292 | strd CARG12, [BASE, RA]
3296 | // RA = dst*8, RC = primitive_type (~)
3305 | // RA = base*8, RC = end
3307 | add RC, BASE, RC, lsl #3
3308 | mvn CARG1, #~LJ_TNIL
3309 | str CARG1, [RA, #4]
3312 | str CARG1, [RA, #4]
3319 /* -- Upvalue and function ops ------------------------------------------ */
3322 | // RA = dst*8, RC = uvnum
3323 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3325 | add RC, RC, #offsetof(GCfuncL, uvptr)
3326 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3327 | ldr CARG2, UPVAL:CARG2->v
3328 | ldrd CARG34, [CARG2]
3331 | strd CARG34, [BASE, RA]
3335 | // RA = uvnum*8, RC = src
3336 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3338 | add RA, RA, #offsetof(GCfuncL, uvptr)
3340 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3341 | ldrd CARG34, [BASE, RC]
3342 | ldrb RB, UPVAL:CARG2->marked
3343 | ldrb RC, UPVAL:CARG2->closed
3344 | ldr CARG2, UPVAL:CARG2->v
3345 | tst RB, #LJ_GC_BLACK // isblack(uv)
3346 | add RB, CARG4, #-LJ_TISGCV
3348 | strd CARG34, [CARG2]
3349 | bne >2 // Upvalue is closed and black?
3353 |2: // Check if new value is collectable.
3354 | cmn RB, #-(LJ_TNUMX - LJ_TISGCV)
3355 | ldrbhi RC, GCOBJ:CARG3->gch.marked
3356 | bls <1 // tvisgcv(v)
3357 | sub CARG1, DISPATCH, #-GG_DISP2G
3358 | tst RC, #LJ_GC_WHITES
3359 | // Crossed a write barrier. Move the barrier forward.
3363 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3366 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3371 | // RA = uvnum*8, RC = str_const (~)
3372 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3374 | add RA, RA, #offsetof(GCfuncL, uvptr)
3376 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3377 | ldr STR:CARG3, [KBASE, RC, lsl #2]
3378 | ldrb RB, UPVAL:CARG2->marked
3379 | ldrb RC, UPVAL:CARG2->closed
3380 | ldr CARG2, UPVAL:CARG2->v
3381 | mvn CARG4, #~LJ_TSTR
3382 | tst RB, #LJ_GC_BLACK // isblack(uv)
3383 | ldrb RB, STR:CARG3->marked
3384 | strd CARG34, [CARG2]
3389 |2: // Check if string is white and ensure upvalue is closed.
3390 | tst RB, #LJ_GC_WHITES // iswhite(str)
3392 | sub CARG1, DISPATCH, #-GG_DISP2G
3393 | // Crossed a write barrier. Move the barrier forward.
3397 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3400 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3405 | // RA = uvnum*8, RC = num_const
3406 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3408 | add RA, RA, #offsetof(GCfuncL, uvptr)
3410 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3411 | ldrd CARG34, [KBASE, RC]
3412 | ldr CARG2, UPVAL:CARG2->v
3415 | strd CARG34, [CARG2]
3419 | // RA = uvnum*8, RC = primitive_type (~)
3420 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3422 | add RA, RA, #offsetof(GCfuncL, uvptr)
3423 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3425 | ldr CARG2, UPVAL:CARG2->v
3428 | str RC, [CARG2, #4]
3433 | // RA = level*8, RC = target
3434 | ldr CARG3, L->openupval
3435 | add RC, PC, RC, lsl #2
3438 | sub PC, RC, #0x20000
3441 | add CARG2, BASE, RA
3442 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3449 | // RA = dst*8, RC = proto_const (~) (holding function prototype)
3452 | ldr CARG2, [KBASE, RC, lsl #2]
3454 | ldr CARG3, [BASE, FRAME_FUNC]
3456 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3457 | bl extern lj_func_newL_gc
3458 | // Returns GCfuncL *.
3460 | mvn CARG2, #~LJ_TFUNC
3463 | strd CARG12, [BASE, RA]
3467 /* -- Table ops --------------------------------------------------------- */
3471 | // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3472 if (op == BC_TDUP) {
3475 | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3476 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3483 if (op == BC_TNEW) {
3484 | lsl CARG2, RC, #21
3485 | lsr CARG3, RC, #11
3486 | asr RC, CARG2, #21
3487 | lsr CARG2, CARG2, #21
3489 | addeq CARG2, CARG2, #2
3490 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3491 | // Returns GCtab *.
3493 | ldr CARG2, [KBASE, RC, lsl #2]
3494 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3495 | // Returns GCtab *.
3498 | mvn CARG2, #~LJ_TTAB
3501 | strd CARG12, [BASE, RA]
3504 | bl extern lj_gc_step_fixtop // (lua_State *L)
3510 | // RA = dst*8, RC = str_const (~)
3512 | // RA = dst*8, RC = str_const (~)
3513 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3515 | ldr TAB:CARG1, LFUNC:CARG2->env
3516 | ldr STR:RC, [KBASE, RC, lsl #2]
3517 if (op == BC_GGET) {
3525 | decode_RB8 RB, INS
3526 | decode_RC8 RC, INS
3527 | // RA = dst*8, RB = table*8, RC = key*8
3528 | ldrd TAB:CARG12, [BASE, RB]
3529 | ldrd CARG34, [BASE, RC]
3530 | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12.
3531 | checktp CARG4, LJ_TISNUM // Integer key?
3532 | ldreq CARG4, TAB:CARG1->array
3533 | ldreq CARG2, TAB:CARG1->asize
3536 | add CARG4, CARG4, CARG3, lsl #3
3537 | cmp CARG3, CARG2 // In array part?
3538 | ldrdlo CARG34, [CARG4]
3540 | ins_next1 // Overwrites RB!
3541 | checktp CARG4, LJ_TNIL
3545 | strd CARG34, [BASE, RA]
3548 |5: // Check for __index if table value is nil.
3549 | ldr TAB:CARG2, TAB:CARG1->metatable
3551 | beq <1 // No metatable: done.
3552 | ldrb CARG2, TAB:CARG2->nomm
3553 | tst CARG2, #1<<MM_index
3554 | bne <1 // 'no __index' flag set: done.
3555 | decode_RB8 RB, INS // Restore RB.
3559 | checktp CARG4, LJ_TSTR // String key?
3560 | moveq STR:RC, CARG3
3565 | decode_RB8 RB, INS
3567 | // RA = dst*8, RB = table*8, RC = str_const (~)
3568 | ldrd CARG12, [BASE, RB]
3570 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3571 | checktab CARG2, ->vmeta_tgets1
3573 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3574 | ldr CARG3, TAB:CARG1->hmask
3575 | ldr CARG4, STR:RC->sid
3576 | ldr NODE:INS, TAB:CARG1->node
3577 | mov TAB:RB, TAB:CARG1
3578 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3579 | add CARG3, CARG3, CARG3, lsl #1
3580 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3582 | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS.
3583 | ldrd CARG34, NODE:INS->val
3584 | ldr NODE:INS, NODE:INS->next
3585 | checktp CARG2, LJ_TSTR
3586 | cmpeq CARG1, STR:RC
3588 | checktp CARG4, LJ_TNIL
3593 | strd CARG34, [BASE, RA]
3596 |4: // Follow hash chain.
3599 | // End of hash chain: key not found, nil result.
3601 |5: // Check for __index if table value is nil.
3602 | ldr TAB:CARG1, TAB:RB->metatable
3603 | mov CARG3, #0 // Optional clear of undef. value (during load stall).
3604 | mvn CARG4, #~LJ_TNIL
3606 | beq <3 // No metatable: done.
3607 | ldrb CARG2, TAB:CARG1->nomm
3608 | tst CARG2, #1<<MM_index
3609 | bne <3 // 'no __index' flag set: done.
3613 | decode_RB8 RB, INS
3615 | // RA = dst*8, RB = table*8, RC = index
3616 | ldrd CARG12, [BASE, RB]
3617 | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12.
3618 | ldr CARG3, TAB:CARG1->asize
3619 | ldr CARG4, TAB:CARG1->array
3622 | ldrdlo CARG34, [CARG4, CARG2]
3624 | ins_next1 // Overwrites RB!
3625 | checktp CARG4, LJ_TNIL
3629 | strd CARG34, [BASE, RA]
3632 |5: // Check for __index if table value is nil.
3633 | ldr TAB:CARG2, TAB:CARG1->metatable
3635 | beq <1 // No metatable: done.
3636 | ldrb CARG2, TAB:CARG2->nomm
3637 | tst CARG2, #1<<MM_index
3638 | bne <1 // 'no __index' flag set: done.
3642 | decode_RB8 RB, INS
3643 | decode_RC8 RC, INS
3644 | // RA = dst*8, RB = table*8, RC = key*8
3645 | ldr TAB:CARG1, [BASE, RB]
3646 | ldr CARG2, [BASE, RC]
3647 | ldr CARG4, TAB:CARG1->array
3648 | ldr CARG3, TAB:CARG1->asize
3649 | add CARG4, CARG4, CARG2, lsl #3
3650 | cmp CARG2, CARG3 // In array part?
3652 | ldrd CARG12, [CARG4]
3656 | strd CARG12, [BASE, RA]
3661 | decode_RB8 RB, INS
3662 | decode_RC8 RC, INS
3663 | // RA = src*8, RB = table*8, RC = key*8
3664 | ldrd TAB:CARG12, [BASE, RB]
3665 | ldrd CARG34, [BASE, RC]
3666 | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12.
3667 | checktp CARG4, LJ_TISNUM // Integer key?
3668 | ldreq CARG2, TAB:CARG1->array
3669 | ldreq CARG4, TAB:CARG1->asize
3672 | add CARG2, CARG2, CARG3, lsl #3
3673 | cmp CARG3, CARG4 // In array part?
3674 | ldrlo INS, [CARG2, #4]
3676 | ins_next1 // Overwrites RB!
3677 | checktp INS, LJ_TNIL
3678 | ldrb INS, TAB:CARG1->marked
3679 | ldrd CARG34, [BASE, RA]
3682 | tst INS, #LJ_GC_BLACK // isblack(table)
3683 | strd CARG34, [CARG2]
3689 |5: // Check for __newindex if previous value is nil.
3690 | ldr TAB:RA, TAB:CARG1->metatable
3692 | beq <1 // No metatable: done.
3693 | ldrb RA, TAB:RA->nomm
3694 | tst RA, #1<<MM_newindex
3695 | bne <1 // 'no __newindex' flag set: done.
3696 | ldr INS, [PC, #-4] // Restore RA and RB.
3697 | decode_RB8 RB, INS
3698 | decode_RA8 RA, INS
3701 |7: // Possible table write barrier for the value. Skip valiswhite check.
3702 | barrierback TAB:CARG1, INS, CARG3
3706 | checktp CARG4, LJ_TSTR // String key?
3707 | moveq STR:RC, CARG3
3712 | decode_RB8 RB, INS
3714 | // RA = src*8, RB = table*8, RC = str_const (~)
3715 | ldrd CARG12, [BASE, RB]
3717 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3718 | checktab CARG2, ->vmeta_tsets1
3720 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3721 | ldr CARG3, TAB:CARG1->hmask
3722 | ldr CARG4, STR:RC->sid
3723 | ldr NODE:INS, TAB:CARG1->node
3724 | mov TAB:RB, TAB:CARG1
3725 | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask
3726 | add CARG3, CARG3, CARG3, lsl #1
3728 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3729 | strb CARG4, TAB:RB->nomm // Clear metamethod cache.
3731 | ldrd CARG12, NODE:INS->key
3732 | ldr CARG4, NODE:INS->val.it
3733 | ldr NODE:CARG3, NODE:INS->next
3734 | checktp CARG2, LJ_TSTR
3735 | cmpeq CARG1, STR:RC
3737 | ldrb CARG2, TAB:RB->marked
3738 | checktp CARG4, LJ_TNIL // Key found, but nil value?
3739 | ldrd CARG34, [BASE, RA]
3742 | tst CARG2, #LJ_GC_BLACK // isblack(table)
3743 | strd CARG34, NODE:INS->val
3748 |4: // Check for __newindex if previous value is nil.
3749 | ldr TAB:CARG1, TAB:RB->metatable
3751 | beq <2 // No metatable: done.
3752 | ldrb CARG1, TAB:CARG1->nomm
3753 | tst CARG1, #1<<MM_newindex
3754 | bne <2 // 'no __newindex' flag set: done.
3757 |5: // Follow hash chain.
3758 | movs NODE:INS, NODE:CARG3
3760 | // End of hash chain: key not found, add a new one.
3762 | // But check for __newindex first.
3763 | ldr TAB:CARG1, TAB:RB->metatable
3766 | cmp TAB:CARG1, #0 // No metatable: continue.
3768 | ldrbne CARG2, TAB:CARG1->nomm
3771 | tst CARG2, #1<<MM_newindex
3772 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3774 | mvn CARG4, #~LJ_TSTR
3775 | str STR:RC, TMPDlo
3778 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3779 | // Returns TValue *.
3781 | ldrd CARG34, [BASE, RA]
3782 | strd CARG34, [CRET1]
3783 | b <3 // No 2nd write barrier needed.
3785 |7: // Possible table write barrier for the value. Skip valiswhite check.
3786 | barrierback TAB:RB, CARG2, CARG3
3790 | decode_RB8 RB, INS
3792 | // RA = src*8, RB = table*8, RC = index
3793 | ldrd CARG12, [BASE, RB]
3794 | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12.
3795 | ldr CARG3, TAB:CARG1->asize
3796 | ldr RB, TAB:CARG1->array
3799 | ldrdlo CARG34, [CARG2, RB]!
3801 | ins_next1 // Overwrites RB!
3802 | checktp CARG4, LJ_TNIL
3803 | ldrb INS, TAB:CARG1->marked
3804 | ldrd CARG34, [BASE, RA]
3807 | tst INS, #LJ_GC_BLACK // isblack(table)
3808 | strd CARG34, [CARG2]
3814 |5: // Check for __newindex if previous value is nil.
3815 | ldr TAB:RA, TAB:CARG1->metatable
3817 | beq <1 // No metatable: done.
3818 | ldrb RA, TAB:RA->nomm
3819 | tst RA, #1<<MM_newindex
3820 | bne <1 // 'no __newindex' flag set: done.
3821 | ldr INS, [PC, #-4] // Restore INS.
3822 | decode_RA8 RA, INS
3825 |7: // Possible table write barrier for the value. Skip valiswhite check.
3826 | barrierback TAB:CARG1, INS, CARG3
3830 | decode_RB8 RB, INS
3831 | decode_RC8 RC, INS
3832 | // RA = src*8, RB = table*8, RC = key*8
3833 | ldr TAB:CARG2, [BASE, RB]
3834 | ldr CARG3, [BASE, RC]
3835 | ldrb INS, TAB:CARG2->marked
3836 | ldr CARG1, TAB:CARG2->array
3837 | ldr CARG4, TAB:CARG2->asize
3838 | tst INS, #LJ_GC_BLACK // isblack(table)
3839 | add CARG1, CARG1, CARG3, lsl #3
3842 | cmp CARG3, CARG4 // In array part?
3845 | ldrd CARG34, [BASE, RA]
3848 | strd CARG34, [CARG1]
3851 |7: // Possible table write barrier for the value. Skip valiswhite check.
3852 | barrierback TAB:CARG2, INS, RB
3857 | // RA = base*8 (table at base-1), RC = num_const (start index)
3860 | ldr RB, SAVE_MULTRES
3861 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
3862 | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
3864 | ldr CARG4, TAB:CARG2->asize
3865 | beq >4 // Nothing to copy?
3866 | add CARG3, CARG1, RB, lsr #3
3868 | ldr CARG4, TAB:CARG2->array
3871 | add INS, CARG4, CARG1, lsl #3
3872 | ldrb CARG1, TAB:CARG2->marked
3873 |3: // Copy result slots to table.
3874 | ldrd CARG34, [RA], #8
3875 | strd CARG34, [INS], #8
3878 | tst CARG1, #LJ_GC_BLACK // isblack(table)
3883 |5: // Need to resize array part.
3887 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3888 | // Must not reallocate the stack.
3889 | .IOS ldr BASE, L->base
3892 |7: // Possible table write barrier for any value. Skip valiswhite check.
3893 | barrierback TAB:CARG2, CARG1, CARG3
3897 /* -- Calls and vararg handling ----------------------------------------- */
3900 | // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3901 | ldr CARG1, SAVE_MULTRES
3902 | decode_RC8 NARGS8:RC, INS
3903 | add NARGS8:RC, NARGS8:RC, CARG1
3907 | decode_RC8 NARGS8:RC, INS
3908 | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3910 | mov RB, BASE // Save old BASE for vmeta_call.
3911 | ldrd CARG34, [BASE, RA]!
3912 | sub NARGS8:RC, NARGS8:RC, #8
3913 | add BASE, BASE, #8
3914 | checkfunc CARG4, ->vmeta_call
3919 | // RA = base*8, (RB = 0,) RC = extra_nargs
3920 | ldr CARG1, SAVE_MULTRES
3921 | add NARGS8:RC, CARG1, RC, lsl #3
3925 | lsl NARGS8:RC, RC, #3
3926 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3928 | ldrd LFUNC:CARG34, [RA, BASE]!
3929 | sub NARGS8:RC, NARGS8:RC, #8
3931 | checkfunc CARG4, ->vmeta_callt
3932 | ldr PC, [BASE, FRAME_PC]
3935 | ldrb CARG4, LFUNC:CARG3->ffid
3936 | tst PC, #FRAME_TYPE
3939 | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
3943 | ldrd CARG12, [RA, RB]
3945 | cmp INS, NARGS8:RC
3946 | strd CARG12, [BASE, RB]
3950 | cmp CARG4, #1 // (> FF_C) Calling a fast function?
3955 |5: // Tailcall to a fast function with a Lua frame below.
3956 | ldr INS, [PC, #-4]
3957 | decode_RA8 RA, INS
3958 | sub CARG1, BASE, RA
3959 | ldr LFUNC:CARG1, [CARG1, #-16]
3960 | ldr CARG1, LFUNC:CARG1->field_pc
3961 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3964 |7: // Tailcall from a vararg function.
3965 | eor PC, PC, #FRAME_VARG
3966 | tst PC, #FRAME_TYPEP // Vararg frame below?
3967 | movne CARG4, #0 // Clear ffid if no Lua function below.
3969 | sub BASE, BASE, PC
3970 | ldr PC, [BASE, FRAME_PC]
3971 | tst PC, #FRAME_TYPE
3972 | movne CARG4, #0 // Clear ffid if no Lua function below.
3977 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3979 | mov RB, BASE // Save old BASE for vmeta_call.
3980 | ldrd CARG34, [RA, #-16]
3981 | ldrd CARG12, [RA, #-8]
3983 | strd CARG34, [RA, #8] // Copy state.
3984 | strd CARG12, [RA, #16] // Copy control var.
3985 | // STALL: locked CARG34.
3986 | ldrd LFUNC:CARG34, [RA, #-24]
3987 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3988 | // STALL: load CARG34.
3989 | strd LFUNC:CARG34, [RA] // Copy callable.
3990 | checkfunc CARG4, ->vmeta_call
3999 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
4001 | ldr TAB:RB, [RA, #-16]
4002 | ldr CARG1, [RA, #-8] // Get index from control var.
4003 | ldr INS, TAB:RB->asize
4004 | ldr CARG2, TAB:RB->array
4006 |1: // Traverse array part.
4007 | subs RC, CARG1, INS
4008 | add CARG3, CARG2, CARG1, lsl #3
4009 | bhs >5 // Index points after array part?
4010 | ldrd CARG34, [CARG3]
4011 | checktp CARG4, LJ_TNIL
4012 | addeq CARG1, CARG1, #1 // Skip holes in array part.
4014 | ldrh RC, [PC, #-2]
4015 | mvn CARG2, #~LJ_TISNUM
4016 | strd CARG34, [RA, #8]
4017 | add RC, PC, RC, lsl #2
4020 | sub PC, RC, #0x20000
4021 | str RB, [RA, #-8] // Update control var.
4025 |5: // Traverse hash part.
4026 | ldr CARG4, TAB:RB->hmask
4027 | ldr NODE:RB, TAB:RB->node
4029 | add CARG1, RC, RC, lsl #1
4030 | cmp RC, CARG4 // End of iteration? Branch to ITERL+1.
4031 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
4033 | ldrd CARG12, NODE:CARG3->val
4034 | checktp CARG2, LJ_TNIL
4036 | beq <6 // Skip holes in hash part.
4037 | ldrh RB, [PC, #-2]
4039 | ldrd CARG34, NODE:CARG3->key
4040 | str RC, [RA, #-8] // Update control var.
4041 | strd CARG12, [RA, #8]
4042 | add RC, PC, RB, lsl #2
4043 | sub PC, RC, #0x20000
4049 | // RA = base*8, RC = target (points to ITERN)
4051 | add RC, PC, RC, lsl #2
4052 | ldrd CFUNC:CARG12, [RA, #-24]
4053 | ldr CARG3, [RA, #-12]
4054 | ldr CARG4, [RA, #-4]
4055 | checktp CARG2, LJ_TFUNC
4056 | ldrbeq CARG1, CFUNC:CARG1->ffid
4057 | checktpeq CARG3, LJ_TTAB
4058 | checktpeq CARG4, LJ_TNIL
4059 | cmpeq CARG1, #FF_next_N
4060 | subeq PC, RC, #0x20000
4065 | mvn CARG2, #~LJ_KEYINDEX
4066 | strd CARG1, [RA, #-8] // Initialize control var.
4069 |5: // Despecialize bytecode if any of the checks fail.
4070 | mov CARG1, #BC_JMP
4072 | strb CARG1, [PC, #-4]
4073 | sub PC, RC, #0x20000
4076 | cmp CARG1, #BC_ITERN
4079 | strb OP, [PC] // Subsumes ins_next1.
4083 |6: // Unpatch JLOOP.
4084 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4085 | ldrh CARG2, [PC, #2]
4086 | ldr TRACE:CARG1, [CARG1, CARG2, lsl #2]
4087 | // Subsumes ins_next1 and ins_next2.
4088 | ldr INS, TRACE:CARG1->startins
4089 | bfi INS, OP, #0, #8
4096 | decode_RB8 RB, INS
4097 | decode_RC8 RC, INS
4098 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4099 | ldr CARG1, [BASE, FRAME_PC]
4102 | add RC, RC, #FRAME_VARG
4104 | sub CARG3, BASE, #8 // CARG3 = vtop
4105 | sub RC, RC, CARG1 // RC = vbase
4106 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4108 | sub CARG1, CARG3, RC
4109 | beq >5 // Copy all varargs?
4110 | sub CARG4, CARG4, #16
4111 |1: // Copy vararg slots to destination slots.
4113 | ldrdlo CARG12, [RC], #8
4114 | mvnhs CARG2, #~LJ_TNIL
4116 | strd CARG12, [RA], #8
4121 |5: // Copy all varargs.
4122 | ldr CARG4, L->maxstack
4124 | movle RB, #8 // MULTRES = (0+1)*8
4125 | addgt RB, CARG1, #8
4126 | add CARG2, RA, CARG1
4127 | str RB, SAVE_MULTRES
4132 | ldrd CARG12, [RC], #8
4133 | strd CARG12, [RA], #8
4138 |7: // Grow stack for varargs.
4139 | lsr CARG2, CARG1, #3
4143 | sub RC, RC, BASE // Need delta, because BASE may change.
4146 | bl extern lj_state_growstack // (lua_State *L, int n)
4150 | sub CARG3, BASE, #8
4154 /* -- Returns ----------------------------------------------------------- */
4157 | // RA = results*8, RC = extra results
4158 | ldr CARG1, SAVE_MULTRES
4159 | ldr PC, [BASE, FRAME_PC]
4161 | add RC, CARG1, RC, lsl #3
4166 | // RA = results*8, RC = nresults+1
4167 | ldr PC, [BASE, FRAME_PC]
4171 | str RC, SAVE_MULTRES
4173 | ands CARG1, PC, #FRAME_TYPE
4174 | eor CARG2, PC, #FRAME_VARG
4178 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
4179 | ldr INS, [PC, #-4]
4180 | subs CARG4, RC, #8
4181 | sub CARG3, BASE, #8
4184 | ldrd CARG12, [RA], #8
4185 | add BASE, BASE, #8
4186 | subs CARG4, CARG4, #8
4187 | strd CARG12, [BASE, #-16]
4190 | decode_RA8 RA, INS
4191 | sub CARG4, CARG3, RA
4192 | decode_RB8 RB, INS
4193 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4195 | cmp RB, RC // More results expected?
4198 | ldr CARG2, LFUNC:CARG1->field_pc
4201 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4204 |6: // Fill up results with nil.
4205 | mvn CARG2, #~LJ_TNIL
4206 | add BASE, BASE, #8
4208 | str CARG2, [BASE, #-12]
4211 |->BC_RETV1_Z: // Non-standard return case.
4214 | tst CARG2, #FRAME_TYPEP
4216 | // Return from vararg function: relocate BASE down.
4217 | sub BASE, BASE, CARG2
4218 | ldr PC, [BASE, FRAME_PC]
4222 case BC_RET0: case BC_RET1:
4223 | // RA = results*8, RC = nresults+1
4224 | ldr PC, [BASE, FRAME_PC]
4226 | str RC, SAVE_MULTRES
4227 | ands CARG1, PC, #FRAME_TYPE
4228 | eor CARG2, PC, #FRAME_VARG
4229 | ldreq INS, [PC, #-4]
4231 if (op == BC_RET1) {
4232 | ldrd CARG12, [BASE, RA]
4234 | sub CARG4, BASE, #8
4235 | decode_RA8 RA, INS
4236 if (op == BC_RET1) {
4237 | strd CARG12, [CARG4]
4239 | sub BASE, CARG4, RA
4240 | decode_RB8 RB, INS
4241 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4245 | ldr CARG2, LFUNC:CARG1->field_pc
4248 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4251 |6: // Fill up results with nil.
4252 | sub CARG2, CARG4, #4
4253 | mvn CARG3, #~LJ_TNIL
4254 | str CARG3, [CARG2, RC]
4259 /* -- Loops and branches ------------------------------------------------ */
4261 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
4262 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
4263 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4264 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
4270 | // Fall through. Assumes BC_IFORL follows.
4280 | // RA = base*8, RC = target (after end of loop or start of loop)
4281 vk = (op == BC_IFORL || op == BC_JFORL);
4282 | ldrd CARG12, [RA, BASE]!
4283 if (op != BC_JFORL) {
4284 | add RC, PC, RC, lsl #2
4287 | ldrd CARG34, FOR_STOP
4288 | checktp CARG2, LJ_TISNUM
4291 | checktp CARG4, LJ_TISNUM
4292 | ldr CARG4, FOR_STEP
4293 | checktpeq RB, LJ_TISNUM
4299 | ldrd CARG34, FOR_STEP
4300 | checktp CARG2, LJ_TISNUM
4302 | adds CARG1, CARG1, CARG3
4303 | ldr CARG4, FOR_STOP
4304 if (op == BC_IFORL) {
4305 | addvs RC, PC, #0x20000 // Overflow: prevent branch.
4307 | bvs >2 // Overflow: do not enter mcode.
4314 if (op == BC_FORI) {
4315 | subgt PC, RC, #0x20000
4316 } else if (op == BC_JFORI) {
4317 | sub PC, RC, #0x20000
4318 | ldrhle RC, [PC, #-2]
4319 } else if (op == BC_IFORL) {
4320 | suble PC, RC, #0x20000
4323 | strd CARG12, FOR_IDX
4328 | strd CARG12, FOR_EXT
4329 if (op == BC_JFORI || op == BC_JFORL) {
4335 |4: // Invert check for negative step.
4345 | cmnlo CARG4, #-LJ_TISNUM
4346 | cmnlo RB, #-LJ_TISNUM
4355 | strd CARG12, FOR_EXT
4364 | vadd.f64 d0, d0, d2
4368 | bl extern __aeabi_dadd
4369 | strd CARG12, FOR_IDX
4370 | ldrd CARG34, FOR_STOP
4371 | strd CARG12, FOR_EXT
4380 | bl extern __aeabi_cdcmple
4388 if (op == BC_FORI) {
4389 | subhi PC, RC, #0x20000
4390 } else if (op == BC_JFORI) {
4391 | sub PC, RC, #0x20000
4392 | ldrhls RC, [PC, #-2]
4394 } else if (op == BC_IFORL) {
4395 | subls PC, RC, #0x20000
4404 |8: // Invert check for negative step.
4406 | bl extern __aeabi_dadd
4407 | strd CARG12, FOR_IDX
4408 | strd CARG12, FOR_EXT
4412 | ldrd CARG12, FOR_STOP
4421 | // Fall through. Assumes BC_IITERL follows.
4429 | // RA = base*8, RC = target
4430 | ldrd CARG12, [RA, BASE]!
4431 if (op == BC_JITERL) {
4432 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4433 | strdne CARG12, [RA, #-8]
4436 | add RC, PC, RC, lsl #2
4437 | // STALL: load CARG12.
4438 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4439 | subne PC, RC, #0x20000 // Otherwise save control var + branch.
4440 | strdne CARG12, [RA, #-8]
4446 | // RA = base*8, RC = target (loop extent)
4447 | // Note: RA/RC is only used by trace recorder to determine scope/extent
4448 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4452 | // Fall through. Assumes BC_ILOOP follows.
4456 | // RA = base*8, RC = target (loop extent)
4462 | // RA = base (ignored), RC = traceno
4463 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4464 | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0.
4465 | ldr TRACE:RC, [CARG1, RC, lsl #2]
4467 | ldr RA, TRACE:RC->mcode
4468 | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4469 | str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)]
4475 | // RA = base*8 (only used by trace recorder), RC = target
4476 | add RC, PC, RC, lsl #2
4477 | sub PC, RC, #0x20000
4481 /* -- Function headers -------------------------------------------------- */
4487 case BC_FUNCV: /* NYI: compiled vararg functions. */
4488 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4496 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4497 | ldr CARG1, L->maxstack
4498 | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4499 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4501 | bhi ->vm_growstack_l
4502 if (op != BC_JFUNCF) {
4507 | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters.
4508 | mvn CARG4, #~LJ_TNIL
4510 if (op == BC_JFUNCF) {
4517 |3: // Clear missing parameters.
4518 | strd CARG34, [BASE, NARGS8:RC]
4519 | add NARGS8:RC, NARGS8:RC, #8
4527 | NYI // NYI: compiled vararg functions
4528 break; /* NYI: compiled vararg functions. */
4531 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4532 | ldr CARG1, L->maxstack
4533 | add CARG4, BASE, RC
4535 | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC.
4536 | add CARG2, RC, #8+FRAME_VARG
4537 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4539 | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG.
4540 | bhs ->vm_growstack_l
4541 | ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4545 | add BASE, CARG4, #8
4547 | mvn CARG3, #~LJ_TNIL
4549 | cmp RA, RC // Less args than parameters?
4550 | ldrdlo CARG12, [RA], #8
4551 | movhs CARG2, CARG3
4552 | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC).
4555 | strd CARG12, [CARG4, #8]!
4563 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4564 if (op == BC_FUNCC) {
4565 | ldr CARG4, CFUNC:CARG3->f
4567 | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4569 | add CARG2, RA, NARGS8:RC
4570 | ldr CARG1, L->maxstack
4571 | add RC, BASE, NARGS8:RC
4575 if (op == BC_FUNCCW) {
4576 | ldr CARG2, CFUNC:CARG3->f
4578 | mv_vmstate CARG3, C
4580 | bhi ->vm_growstack_c // Need to grow stack.
4582 | blx CARG4 // (lua_State *L [, lua_CFunction f])
4583 | // Returns nresults.
4585 | mv_vmstate CARG3, INTERP
4587 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
4590 | ldr PC, [BASE, FRAME_PC]
4591 | sub RA, CRET2, RC // RA = L->top - nresults*8
4595 /* ---------------------------------------------------------------------- */
4598 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4604 static int build_backend(BuildCtx *ctx)
4608 dasm_growpc(Dst, BC__MAX);
4610 build_subroutines(ctx);
4613 for (op = 0; op < BC__MAX; op++)
4614 build_ins(ctx, (BCOp)op, op);
4619 /* Emit pseudo frame-info for all assembler functions. */
4620 static void emit_asm_debug(BuildCtx *ctx)
4622 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4624 switch (ctx->mode) {
4626 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4629 "\t.long .LECIE0-.LSCIE0\n"
4631 "\t.long 0xffffffff\n"
4636 "\t.byte 0xe\n" /* Return address is in lr. */
4637 "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */
4642 "\t.long .LEFDE0-.LASFDE0\n"
4644 "\t.long .Lframe0\n"
4647 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4648 "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */
4649 fcofs, CFRAME_SIZE);
4650 for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */
4651 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4653 for (i = 15; i >= 8; i--) /* offset d8-d15 */
4654 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4655 64+2*i, 10+2*(15-i));
4656 fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */
4664 "\t.long .LEFDE1-.LASFDE1\n"
4666 "\t.long .Lframe0\n"
4667 "\t.long lj_vm_ffi_call\n"
4669 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4670 "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */
4671 "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */
4672 "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */
4673 "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */
4674 "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */
4676 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);