1 |// Low-level VM code for ARM CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
10 |.globalnames globnames
11 |.externnames extnames
13 |// Note: The ragged indentation of the instructions is intentional.
14 |// The starting columns indicate data dependencies.
16 |//-----------------------------------------------------------------------
18 |// 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].
542 | ldr CARG3, LFUNC:CARG3->field_pc
545 | str INS, [CARG2, #-4] // Ensure one valid arg.
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
703 | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
704 | // Returns TValue *.
708 |//-- Comparison metamethods ---------------------------------------------
717 | decode_OP CARG4, INS
718 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
719 | // Returns 0/1 or TValue * (metamethod).
721 | .IOS ldr BASE, L->base
727 | add RB, PC, RB, lsl #2
728 | subhs PC, RB, #0x20000
732 |->cont_ra: // RA = resultptr
735 | decode_RA8 CARG3, INS
736 | strd CARG12, [BASE, CARG3]
739 |->cont_condt: // RA = resultptr
740 | ldr CARG2, [RA, #4]
741 | mvn CARG1, #~LJ_TTRUE
742 | cmp CARG1, CARG2 // Branch if result is true.
745 |->cont_condf: // RA = resultptr
746 | ldr CARG2, [RA, #4]
747 | checktp CARG2, LJ_TFALSE // Branch if result is false.
751 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
756 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
757 | // Returns 0/1 or TValue * (metamethod).
767 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
768 | // Returns 0/1 or TValue * (metamethod).
779 | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
780 | .IOS ldr BASE, L->base
783 |//-- Arithmetic metamethods ---------------------------------------------
788 | add CARG3, BASE, RB
789 | add CARG4, KBASE, RC
795 | add CARG4, BASE, RB
796 | add CARG3, KBASE, RC
802 | add CARG3, BASE, RC
803 | add CARG4, BASE, RC
809 | add CARG3, BASE, RB
810 | add CARG4, BASE, RC
813 | add CARG2, BASE, RA
818 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
819 | // Returns NULL (finished) or TValue * (metamethod).
820 | .IOS ldr BASE, L->base
824 | // Call metamethod for binary op.
826 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
827 | sub CARG2, CRET1, BASE
828 | str PC, [CRET1, #-12] // [cont|PC]
829 | add PC, CARG2, #FRAME_CONT
831 | mov NARGS8:RC, #16 // 2 args for func(o1, o2).
832 | b ->vm_call_dispatch
835 | add CARG2, BASE, RC
839 | bl extern lj_meta_len // (lua_State *L, TValue *o)
840 | // Returns NULL (retry) or TValue * (metamethod base).
841 | .IOS ldr BASE, L->base
844 | bne ->vmeta_binop // Binop call for compatibility.
845 | ldr TAB:CARG1, [BASE, RC]
848 | b ->vmeta_binop // Binop call for compatibility.
851 |//-- Call metamethod ----------------------------------------------------
853 |->vmeta_call: // Resolve and call __call metamethod.
854 | // RB = old base, BASE = new base, RC = nargs*8
856 | str RB, L->base // This is the callers base!
857 | sub CARG2, BASE, #8
859 | add CARG3, BASE, NARGS8:RC
861 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
863 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
864 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
867 |->vmeta_callt: // Resolve __call for BC_CALLT.
868 | // BASE = old base, RA = new base, RC = nargs*8
873 | add CARG3, RA, NARGS8:RC
874 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
875 | .IOS ldr BASE, L->base
876 | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here.
877 | ldr PC, [BASE, FRAME_PC]
878 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
881 |//-- Argument coercion for 'for' statement ------------------------------
888 | bl extern lj_meta_for // (lua_State *L, TValue *base)
889 | .IOS ldr BASE, L->base
904 |//-----------------------------------------------------------------------
905 |//-- Fast functions -----------------------------------------------------
906 |//-----------------------------------------------------------------------
912 |.macro .ffunc_1, name
914 | ldrd CARG12, [BASE]
919 |.macro .ffunc_2, name
921 | ldrd CARG12, [BASE]
922 | ldrd CARG34, [BASE, #8]
927 |.macro .ffunc_n, name
929 | checktp CARG2, LJ_TISNUM
933 |.macro .ffunc_nn, name
935 | checktp CARG2, LJ_TISNUM
936 | cmnlo CARG4, #-LJ_TISNUM
940 |.macro .ffunc_d, name
942 | ldr CARG2, [BASE, #4]
946 | checktp CARG2, LJ_TISNUM
950 |.macro .ffunc_dd, name
952 | ldr CARG2, [BASE, #4]
953 | ldr CARG4, [BASE, #12]
956 | vldr d1, [BASE, #8]
958 | checktp CARG2, LJ_TISNUM
959 | cmnlo CARG4, #-LJ_TISNUM
963 |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
965 | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
966 | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
971 |//-- Base library: checks -----------------------------------------------
974 | checktp CARG2, LJ_TTRUE
976 | ldr PC, [BASE, FRAME_PC]
977 | strd CARG12, [BASE, #-8]
979 | subs RA, NARGS8:RC, #8
980 | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
981 | beq ->fff_res // Done if exactly 1 argument.
983 | ldrd CARG12, [RB, #8]
985 | strd CARG12, [RB], #8
990 | ldr CARG2, [BASE, #4]
993 | checktp CARG2, LJ_TISNUM
994 | mvnlo CARG2, #~LJ_TISNUM
995 | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
996 | lsl CARG4, CARG4, #3
997 | ldrd CARG12, [CFUNC:CARG3, CARG4]
1000 |//-- Base library: getters and setters ---------------------------------
1002 |.ffunc_1 getmetatable
1003 | checktp CARG2, LJ_TTAB
1004 | cmnne CARG2, #-LJ_TUDATA
1006 |1: // Field metatable must be at same offset for GCtab and GCudata!
1007 | ldr TAB:RB, TAB:CARG1->metatable
1009 | mvn CARG2, #~LJ_TNIL
1010 | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
1013 | ldr CARG3, TAB:RB->hmask
1014 | ldr CARG4, STR:RC->hash
1015 | ldr NODE:INS, TAB:RB->node
1016 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
1017 | add CARG3, CARG3, CARG3, lsl #1
1018 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
1019 |3: // Rearranged logic, because we expect _not_ to find the key.
1020 | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS.
1021 | ldrd CARG12, NODE:INS->val
1022 | ldr NODE:INS, NODE:INS->next
1023 | checktp CARG4, LJ_TSTR
1024 | cmpeq CARG3, STR:RC
1029 | mov CARG1, RB // Use metatable as default result.
1030 | mvn CARG2, #~LJ_TTAB
1033 | checktp CARG2, LJ_TNIL
1038 | checktp CARG2, LJ_TISNUM
1039 | mvnhs CARG2, CARG2
1040 | movlo CARG2, #~LJ_TISNUM
1041 | add CARG4, DISPATCH, CARG2, lsl #2
1042 | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1045 |.ffunc_2 setmetatable
1046 | // Fast path: no mt for table yet and not clearing the mt.
1047 | checktp CARG2, LJ_TTAB
1048 | ldreq TAB:RB, TAB:CARG1->metatable
1049 | checktpeq CARG4, LJ_TTAB
1050 | ldrbeq CARG4, TAB:CARG1->marked
1052 | bne ->fff_fallback
1053 | tst CARG4, #LJ_GC_BLACK // isblack(table)
1054 | str TAB:CARG3, TAB:CARG1->metatable
1056 | barrierback TAB:CARG1, CARG4, CARG3
1060 | ldrd CARG34, [BASE]
1061 | cmp NARGS8:RC, #16
1062 | blo ->fff_fallback
1064 | checktab CARG4, ->fff_fallback
1066 | add CARG3, BASE, #8
1068 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1069 | // Returns cTValue *.
1071 | ldrd CARG12, [CRET1]
1074 |//-- Base library: conversions ------------------------------------------
1077 | // Only handles the number case inline (without a base argument).
1078 | ldrd CARG12, [BASE]
1080 | bne ->fff_fallback
1081 | checktp CARG2, LJ_TISNUM
1086 | // Only handles the string or number case inline.
1087 | checktp CARG2, LJ_TSTR
1088 | // A __tostring method in the string base metatable is ignored.
1090 | // Handle numbers inline, unless a number base metatable is present.
1091 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])]
1093 | checktp CARG2, LJ_TISNUM
1095 | str PC, SAVE_PC // Redundant (but a defined value).
1096 | bhi ->fff_fallback
1100 | bl extern lj_strfmt_number // (lua_State *L, cTValue *o)
1101 | // Returns GCstr *.
1103 | mvn CARG2, #~LJ_TSTR
1106 |//-- Base library: iterators -------------------------------------------
1109 | mvn CARG4, #~LJ_TNIL
1110 | checktab CARG2, ->fff_fallback
1111 | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil.
1112 | ldr PC, [BASE, FRAME_PC]
1114 | str BASE, L->base // Add frame since C call can throw.
1116 | str BASE, L->top // Dummy frame length is ok.
1117 | add CARG3, BASE, #8
1119 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1120 | // Returns 0 at end of traversal.
1121 | .IOS ldr BASE, L->base
1123 | mvneq CRET2, #~LJ_TNIL
1124 | beq ->fff_restv // End of traversal: return nil.
1125 | ldrd CARG12, [BASE, #8] // Copy key and value to results.
1126 | ldrd CARG34, [BASE, #16]
1128 | strd CARG12, [BASE, #-8]
1129 | strd CARG34, [BASE]
1133 | checktab CARG2, ->fff_fallback
1135 | ldr TAB:RB, TAB:CARG1->metatable
1137 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1138 | ldr PC, [BASE, FRAME_PC]
1141 | bne ->fff_fallback
1143 | mvn CARG2, #~LJ_TNIL
1145 | strd CFUNC:CARG34, [BASE, #-8]
1146 | str CARG2, [BASE, #12]
1149 |.ffunc_2 ipairs_aux
1150 | checktp CARG2, LJ_TTAB
1151 | checktpeq CARG4, LJ_TISNUM
1152 | bne ->fff_fallback
1153 | ldr RB, TAB:CARG1->asize
1154 | ldr RC, TAB:CARG1->array
1155 | add CARG3, CARG3, #1
1156 | ldr PC, [BASE, FRAME_PC]
1158 | add RC, RC, CARG3, lsl #3
1159 | strd CARG34, [BASE, #-8]
1160 | ldrdlo CARG12, [RC]
1162 | bhs >2 // Not in array part?
1164 | checktp CARG2, LJ_TNIL
1165 | movne RC, #(2+1)*8
1166 | strdne CARG12, [BASE]
1168 |2: // Check for empty hash part first. Otherwise call C function.
1169 | ldr RB, TAB:CARG1->hmask
1174 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1175 | // Returns cTValue * or NULL.
1179 | ldrd CARG12, [CRET1]
1183 | checktab CARG2, ->fff_fallback
1185 | ldr TAB:RB, TAB:CARG1->metatable
1187 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1188 | ldr PC, [BASE, FRAME_PC]
1191 | bne ->fff_fallback
1194 | mvn CARG2, #~LJ_TISNUM
1196 | strd CFUNC:CARG34, [BASE, #-8]
1197 | strd CARG12, [BASE, #8]
1200 |//-- Base library: catch errors ----------------------------------------
1203 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1205 | blo ->fff_fallback
1206 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1208 | add BASE, BASE, #8
1209 | moveq PC, #8+FRAME_PCALL
1210 | movne PC, #8+FRAME_PCALLH
1211 | sub NARGS8:RC, NARGS8:RC, #8
1212 | b ->vm_call_dispatch
1215 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1216 | checkfunc CARG4, ->fff_fallback // Traceback must be a function.
1218 | strd CARG12, [BASE, #8] // Swap function and traceback.
1219 | strd CARG34, [BASE]
1220 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1221 | add BASE, BASE, #16
1222 | moveq PC, #16+FRAME_PCALL
1223 | movne PC, #16+FRAME_PCALLH
1224 | sub NARGS8:RC, NARGS8:RC, #16
1225 | b ->vm_call_dispatch
1227 |//-- Coroutine library --------------------------------------------------
1229 |.macro coroutine_resume_wrap, resume
1231 |.ffunc_1 coroutine_resume
1232 | checktp CARG2, LJ_TTHREAD
1233 | bne ->fff_fallback
1235 |.ffunc coroutine_wrap_aux
1236 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1238 | ldr PC, [BASE, FRAME_PC]
1240 | ldr CARG2, L:CARG1->top
1241 | ldrb RA, L:CARG1->status
1242 | ldr RB, L:CARG1->base
1243 | add CARG3, CARG2, NARGS8:RC
1244 | add CARG4, CARG2, RA
1247 | beq ->fff_fallback
1248 | ldr CARG4, L:CARG1->maxstack
1249 | ldr RB, L:CARG1->cframe
1250 | cmp RA, #LUA_YIELD
1251 | cmpls CARG3, CARG4
1253 | bhi ->fff_fallback
1256 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1257 | add BASE, BASE, #8
1258 | sub NARGS8:RC, NARGS8:RC, #8
1260 | str CARG3, L:CARG1->top
1262 |2: // Move args to coroutine.
1263 | ldrd CARG34, [BASE, RB]
1265 | strdne CARG34, [CARG2, RB]
1272 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1273 | // Returns thread status.
1275 | ldr CARG3, L:RA->base
1276 | mv_vmstate CARG2, INTERP
1277 | ldr CARG4, L:RA->top
1278 | cmp CRET1, #LUA_YIELD
1280 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
1283 | subs RC, CARG4, CARG3
1284 | ldr CARG1, L->maxstack
1285 | add CARG2, BASE, RC
1286 | beq >6 // No results?
1289 | bhi >9 // Need to grow stack?
1292 | str CARG3, L:RA->top // Clear coroutine stack.
1293 |5: // Move results from coroutine.
1294 | ldrd CARG12, [CARG3, RB]
1296 | strd CARG12, [BASE, RB]
1301 | mvn CARG3, #~LJ_TTRUE
1304 | str CARG3, [BASE, #-4] // Prepend true/false to results.
1310 | ands CARG1, PC, #FRAME_TYPE
1312 | str RC, SAVE_MULTRES
1316 |8: // Coroutine returned with error (at co->top-1).
1318 | ldrd CARG12, [CARG4, #-8]!
1319 | mvn CARG3, #~LJ_TFALSE
1321 | str CARG4, L:RA->top // Remove error from coroutine stack.
1322 | strd CARG12, [BASE] // Copy error message.
1327 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1331 |9: // Handle stack expansion on return from yield.
1334 | bl extern lj_state_growstack // (lua_State *L, int n)
1339 | coroutine_resume_wrap 1 // coroutine.resume
1340 | coroutine_resume_wrap 0 // coroutine.wrap
1342 |.ffunc coroutine_yield
1343 | ldr CARG1, L->cframe
1344 | add CARG2, BASE, NARGS8:RC
1346 | tst CARG1, #CFRAME_RESUME
1348 | mov CRET1, #LUA_YIELD
1350 | beq ->fff_fallback
1351 | str CARG3, L->cframe
1352 | strb CRET1, L->status
1355 |//-- Math library -------------------------------------------------------
1357 |.macro math_round, func
1358 | .ffunc_1 math_ .. func
1359 | checktp CARG2, LJ_TISNUM
1361 | bhi ->fff_fallback
1362 | // Round FP value and normalize result.
1363 | lsl CARG3, CARG2, #1
1364 | adds RB, CARG3, #0x00200000
1365 | bpl >2 // |x| < 1?
1367 | subs RB, CARG4, RB, asr #21
1368 | lsl CARG4, CARG2, #11
1369 | lsl CARG3, CARG1, #11
1370 | orr CARG4, CARG4, #0x80000000
1372 | orr CARG4, CARG4, CARG1, lsr #21
1373 | bls >3 // |x| >= 2^31?
1374 | orr CARG3, CARG3, CARG4, lsl INS
1375 | lsr CARG1, CARG4, RB
1376 |.if "func" == "floor"
1377 | tst CARG3, CARG2, asr #31
1378 | addne CARG1, CARG1, #1
1380 | bics CARG3, CARG3, CARG2, asr #31
1381 | addsne CARG1, CARG1, #1
1386 | rsblt CARG1, CARG1, #0
1388 | mvn CARG2, #~LJ_TISNUM
1392 | bcs ->fff_restv // |x| is not finite.
1393 | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo
1394 |.if "func" == "floor"
1395 | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1
1399 | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1
1403 | mvn CARG2, #~LJ_TISNUM
1406 |3: // |x| >= 2^31. Check for x == -(2^31).
1407 | cmpeq CARG4, #0x80000000
1408 |.if "func" == "floor"
1413 | movmi CARG1, #0x80000000
1416 | bl ->vm_..func.._sf
1425 | .long 0x00000000, 0x41e00000 // 2^31.
1428 | checktp CARG2, LJ_TISNUM
1429 | bhi ->fff_fallback
1430 | bicne CARG2, CARG2, #0x80000000
1433 | rsbslt CARG1, CARG1, #0
1438 | // CARG12 = TValue result.
1439 | ldr PC, [BASE, FRAME_PC]
1440 | strd CARG12, [BASE, #-8]
1445 | // RC = (nresults+1)*8, PC = return.
1446 | ands CARG1, PC, #FRAME_TYPE
1447 | ldreq INS, [PC, #-4]
1448 | str RC, SAVE_MULTRES
1451 | decode_RB8 RB, INS
1453 | cmp RB, RC // More results expected?
1455 | decode_RA8 CARG1, INS
1458 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1459 | sub BASE, RA, CARG1
1462 |6: // Fill up results with nil.
1464 | mvn CARG1, #~LJ_TNIL
1466 | str CARG1, [CARG2, #-4]
1469 |.macro math_extern, func
1471 | .ffunc_d math_ .. func
1473 | .ffunc_n math_ .. func
1485 |.macro math_extern2, func
1487 | .ffunc_dd math_ .. func
1489 | .ffunc_nn math_ .. func
1502 | .ffunc_d math_sqrt
1505 | ldr PC, [BASE, FRAME_PC]
1506 | vstr d0, [BASE, #-8]
1514 | ldr CARG2, [BASE, #4]
1515 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1517 | bne ->fff_fallback
1519 | ldrd CARG12, [BASE]
1520 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1521 | bne ->fff_fallback
1523 | checktp CARG2, LJ_TISNUM
1524 | bhs ->fff_fallback
1546 | math_extern2 atan2
1551 | ldr CARG4, [BASE, #4]
1552 | ldrd CARG12, [BASE, #8]
1553 | cmp NARGS8:RC, #16
1554 | blo ->fff_fallback
1556 | checktp CARG4, LJ_TISNUM
1557 | bhs ->fff_fallback
1558 | checktp CARG2, LJ_TISNUM
1559 | bne ->fff_fallback
1561 | bl extern ldexp // (double x, int exp)
1565 |.ffunc_2 math_ldexp
1566 | checktp CARG2, LJ_TISNUM
1567 | bhs ->fff_fallback
1568 | checktp CARG4, LJ_TISNUM
1569 | bne ->fff_fallback
1571 | bl extern ldexp // (double x, int exp)
1577 |.ffunc_d math_frexp
1583 | mvn CARG4, #~LJ_TISNUM
1584 | ldr PC, [BASE, FRAME_PC]
1585 | vstr d0, [BASE, #-8]
1587 | strd CARG34, [BASE]
1590 |.ffunc_n math_frexp
1596 | mvn CARG4, #~LJ_TISNUM
1597 | ldr PC, [BASE, FRAME_PC]
1598 | strd CARG12, [BASE, #-8]
1600 | strd CARG34, [BASE]
1606 | sub CARG1, BASE, #8
1607 | ldr PC, [BASE, FRAME_PC]
1616 | sub CARG3, BASE, #8
1617 | ldr PC, [BASE, FRAME_PC]
1622 | strd CARG12, [BASE]
1626 |.macro math_minmax, name, cond, fcond
1630 | checktp CARG2, LJ_TISNUM
1633 |1: // Handle integers.
1637 | checktp CARG4, LJ_TISNUM
1641 | mov..cond CARG1, CARG3
1643 |3: // Convert intermediate result to number and continue below.
1645 | bhi ->fff_fallback
1647 | vcvt.f64.s32 d0, s4
1652 | bhi ->fff_fallback
1653 |5: // Handle numbers.
1658 | checktp CARG4, LJ_TISNUM
1664 | vmov..fcond.f64 d0, d1
1666 |7: // Convert integer to number and continue above.
1668 | bhi ->fff_fallback
1669 | vcvt.f64.s32 d1, s4
1675 | checktp CARG2, LJ_TISNUM
1678 |1: // Handle integers.
1679 | ldrd CARG34, [BASE, RA]
1682 | checktp CARG4, LJ_TISNUM
1686 | mov..cond CARG1, CARG3
1688 |3: // Convert intermediate result to number and continue below.
1689 | bhi ->fff_fallback
1690 | bl extern __aeabi_i2d
1691 | ldrd CARG34, [BASE, RA]
1695 | bhi ->fff_fallback
1696 |5: // Handle numbers.
1697 | ldrd CARG34, [BASE, RA]
1700 | checktp CARG4, LJ_TISNUM
1703 | bl extern __aeabi_cdcmple
1705 | mov..fcond CARG1, CARG3
1706 | mov..fcond CARG2, CARG4
1708 |7: // Convert integer to number and continue above.
1709 | bhi ->fff_fallback
1712 | bl extern __aeabi_i2d
1718 | math_minmax math_min, gt, hi
1719 | math_minmax math_max, lt, lo
1721 |//-- String library -----------------------------------------------------
1723 |.ffunc string_byte // Only handle the 1-arg case here.
1724 | ldrd CARG12, [BASE]
1725 | ldr PC, [BASE, FRAME_PC]
1727 | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument.
1728 | bne ->fff_fallback
1729 | ldr CARG3, STR:CARG1->len
1730 | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
1731 | mvn CARG2, #~LJ_TISNUM
1733 | moveq RC, #(0+1)*8
1734 | movne RC, #(1+1)*8
1735 | strd CARG12, [BASE, #-8]
1738 |.ffunc string_char // Only handle the 1-arg case here.
1740 | ldrd CARG12, [BASE]
1741 | ldr PC, [BASE, FRAME_PC]
1742 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1743 | checktpeq CARG2, LJ_TISNUM
1744 | bicseq CARG4, CARG1, #255
1746 | bne ->fff_fallback
1748 | mov CARG2, TMPDp // Points to stack. Little-endian.
1750 | // CARG2 = str, CARG3 = len.
1754 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1756 | // Returns GCstr *.
1758 | mvn CARG2, #~LJ_TSTR
1763 | ldrd CARG12, [BASE]
1764 | ldrd CARG34, [BASE, #16]
1765 | cmp NARGS8:RC, #16
1768 | blo ->fff_fallback
1769 | checktp CARG4, LJ_TISNUM
1771 | bne ->fff_fallback
1773 | ldrd CARG34, [BASE, #8]
1774 | checktp CARG2, LJ_TSTR
1775 | ldreq CARG2, STR:CARG1->len
1776 | checktpeq CARG4, LJ_TISNUM
1777 | bne ->fff_fallback
1778 | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1779 | add CARG4, CARG2, #1
1780 | cmp CARG3, #0 // if (start < 0) start += len+1
1781 | addlt CARG3, CARG3, CARG4
1782 | cmp CARG3, #1 // if (start < 1) start = 1
1784 | cmp RB, #0 // if (end < 0) end += len+1
1785 | addlt RB, RB, CARG4
1786 | bic RB, RB, RB, asr #31 // if (end < 0) end = 0
1787 | cmp RB, CARG2 // if (end > len) end = len
1788 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1790 | add CARG2, CARG1, CARG3
1791 | subs CARG3, RB, CARG3 // len = end - start
1792 | add CARG3, CARG3, #1 // len += 1
1795 | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1796 | mvn CARG2, #~LJ_TSTR
1799 |.macro ffstring_op, name
1800 | .ffunc string_ .. name
1802 | ldr CARG3, [BASE, #4]
1804 | ldr STR:CARG2, [BASE]
1805 | blo ->fff_fallback
1806 | sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf)
1807 | checkstr CARG3, ->fff_fallback
1808 | ldr CARG4, SBUF:CARG1->b
1811 | str L, SBUF:CARG1->L
1812 | str CARG4, SBUF:CARG1->p
1813 | bl extern lj_buf_putstr_ .. name
1814 | bl extern lj_buf_tostr
1818 |ffstring_op reverse
1822 |//-- Bit library --------------------------------------------------------
1824 |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1826 | bhi ->fff_fallback
1829 | adds RB, RB, #0x00200000
1830 | movpl CARG1, #0 // |x| < 1?
1833 | subs RB, CARG4, RB, asr #21
1834 | bmi >1 // |x| >= 2^32?
1835 | lsl CARG4, CARG2, #11
1836 | orr CARG4, CARG4, #0x80000000
1837 | orr CARG4, CARG4, CARG1, lsr #21
1839 | lsr CARG1, CARG4, RB
1840 | rsblt CARG1, CARG1, #0
1844 | lsr CARG4, CARG1, RB
1846 | lsl CARG1, CARG2, #12
1848 | orr CARG1, CARG4, CARG1, lsl RB
1849 | rsblt CARG1, CARG1, #0
1852 |.macro .ffunc_bit, name
1853 | .ffunc_1 bit_..name
1854 | checktp CARG2, LJ_TISNUM
1855 | blne ->vm_tobit_fb
1859 | mvn CARG2, #~LJ_TISNUM
1862 |.macro .ffunc_bit_op, name, ins
1867 | ldrd CARG12, [BASE, RA]
1871 | checktp CARG2, LJ_TISNUM
1872 | blne ->vm_tobit_fb
1873 | ins CARG3, CARG3, CARG1
1877 |.ffunc_bit_op band, and
1878 |.ffunc_bit_op bor, orr
1879 |.ffunc_bit_op bxor, eor
1882 | mvn CARG4, #~LJ_TISNUM
1883 | ldr PC, [BASE, FRAME_PC]
1884 | strd CARG34, [BASE, #-8]
1888 | eor CARG3, CARG1, CARG1, ror #16
1889 | bic CARG3, CARG3, #0x00ff0000
1890 | ror CARG1, CARG1, #8
1891 | mvn CARG2, #~LJ_TISNUM
1892 | eor CARG1, CARG1, CARG3, lsr #8
1897 | mvn CARG2, #~LJ_TISNUM
1900 |.macro .ffunc_bit_sh, name, ins, shmod
1902 | ldrd CARG12, [BASE, #8]
1903 | cmp NARGS8:RC, #16
1904 | blo ->fff_fallback
1905 | checktp CARG2, LJ_TISNUM
1906 | blne ->vm_tobit_fb
1908 | and RA, CARG1, #31
1912 | ldrd CARG12, [BASE]
1913 | checktp CARG2, LJ_TISNUM
1914 | blne ->vm_tobit_fb
1915 | ins CARG1, CARG1, RA
1916 | mvn CARG2, #~LJ_TISNUM
1920 |.ffunc_bit_sh lshift, lsl, 0
1921 |.ffunc_bit_sh rshift, lsr, 0
1922 |.ffunc_bit_sh arshift, asr, 0
1923 |.ffunc_bit_sh rol, ror, 1
1924 |.ffunc_bit_sh ror, ror, 0
1926 |//-----------------------------------------------------------------------
1928 |->fff_fallback: // Call fast function fallback handler.
1929 | // BASE = new base, RC = nargs*8
1930 | ldr CARG3, [BASE, FRAME_FUNC]
1931 | ldr CARG2, L->maxstack
1932 | add CARG1, BASE, NARGS8:RC
1933 | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC.
1935 | ldr CARG3, CFUNC:CARG3->f
1937 | add CARG1, CARG1, #8*LUA_MINSTACK
1938 | str PC, SAVE_PC // Redundant (but a defined value).
1941 | bhi >5 // Need to grow stack.
1942 | blx CARG3 // (lua_State *L)
1943 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1948 | bgt ->fff_res // Returned nresults+1?
1949 |1: // Returned 0 or -1: retry fast path.
1951 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1952 | sub NARGS8:RC, CARG1, BASE
1953 | bne ->vm_call_tail // Returned -1?
1954 | ins_callt // Returned 0: retry fast path.
1956 |// Reconstruct previous base for vmeta_call during tailcall.
1958 | ands CARG1, PC, #FRAME_TYPE
1959 | bic CARG2, PC, #FRAME_TYPEP
1960 | ldreq INS, [PC, #-4]
1961 | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8.
1962 | addeq CARG2, CARG2, #8
1963 | sub RB, BASE, CARG2
1964 | b ->vm_call_dispatch // Resolve again for tailcall.
1966 |5: // Grow stack for fallback handler.
1967 | mov CARG2, #LUA_MINSTACK
1968 | bl extern lj_state_growstack // (lua_State *L, int n)
1970 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1973 |->fff_gcstep: // Call GC step function.
1974 | // BASE = new base, RC = nargs*8
1977 | add CARG2, BASE, NARGS8:RC
1978 | str PC, SAVE_PC // Redundant (but a defined value).
1981 | bl extern lj_gc_step // (lua_State *L)
1983 | mov lr, RA // Help return address predictor.
1984 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1987 |//-----------------------------------------------------------------------
1988 |//-- Special dispatch targets -------------------------------------------
1989 |//-----------------------------------------------------------------------
1991 |->vm_record: // Dispatch target for recording phase.
1993 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
1994 | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent.
1996 | // Decrement the hookcount for consistency, but always do the call.
1997 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
1998 | tst CARG1, #HOOK_ACTIVE
2000 | sub CARG2, CARG2, #1
2001 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2002 | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2006 |->vm_rethook: // Dispatch target for return hooks.
2007 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2008 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2010 |5: // Re-dispatch to static ins.
2012 | add OP, DISPATCH, OP, lsl #2
2013 | ldr pc, [OP, #GG_DISP2STATIC]
2015 |->vm_inshook: // Dispatch target for instr/line hooks.
2016 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2017 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2018 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2020 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2022 | subs CARG2, CARG2, #1
2023 | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2025 | tst CARG1, #LUA_MASKLINE
2031 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2032 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2035 |4: // Re-dispatch to static ins.
2036 | ldrb OP, [PC, #-4]
2037 | ldr INS, [PC, #-4]
2038 | add OP, DISPATCH, OP, lsl #2
2039 | ldr OP, [OP, #GG_DISP2STATIC]
2040 | decode_RA8 RA, INS
2044 |->cont_hook: // Continue from hook yield.
2045 | ldr CARG1, [CARG4, #-24]
2047 | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins.
2050 |->vm_hotloop: // Hot loop counter underflow.
2052 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L).
2053 | sub CARG1, DISPATCH, #-GG_DISP2J
2055 | ldr CARG3, LFUNC:CARG3->field_pc
2057 | str L, [DISPATCH, #DISPATCH_J(L)]
2058 | ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2060 | add CARG3, BASE, CARG3, lsl #3
2062 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2066 |->vm_callhook: // Dispatch target for call hooks.
2072 |->vm_hotcall: // Hot call counter underflow.
2077 | add CARG4, BASE, RC
2083 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2084 | // Returns ASMFunction.
2089 | sub NARGS8:RC, CARG4, BASE
2090 | str CARG2, SAVE_PC // Invalidate for subsequent line hook.
2091 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2092 | ldr INS, [PC, #-4]
2095 |->cont_stitch: // Trace stitching.
2097 | // RA = resultptr, CARG4 = meta base
2098 | ldr RB, SAVE_MULTRES
2099 | ldr INS, [PC, #-4]
2100 | ldr TRACE:CARG3, [CARG4, #-24] // Save previous trace.
2102 | decode_RA8 RC, INS // Call base.
2104 |1: // Move results down.
2108 | strd CARG12, [BASE, RC]
2112 | decode_RA8 RA, INS
2113 | decode_RB8 RB, INS
2117 | mvn CARG2, #~LJ_TNIL
2118 | bhi >9 // More results wanted?
2120 | ldrh RA, TRACE:CARG3->traceno
2121 | ldrh RC, TRACE:CARG3->link
2123 | beq ->cont_nop // Blacklisted.
2125 | bne =>BC_JLOOP // Jump to stitched trace.
2127 | // Stitch a new trace to the previous trace.
2128 | str RA, [DISPATCH, #DISPATCH_J(exitno)]
2129 | str L, [DISPATCH, #DISPATCH_J(L)]
2131 | sub CARG1, DISPATCH, #-GG_DISP2J
2133 | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2137 |9: // Fill up results with nil.
2138 | strd CARG12, [BASE, RC]
2143 |->vm_profhook: // Dispatch target for profiler hook.
2148 | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2149 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2155 |//-----------------------------------------------------------------------
2156 |//-- Trace exit handler -------------------------------------------------
2157 |//-----------------------------------------------------------------------
2162 | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2163 | ldr CARG1, [sp, #64] // Load original value of lr.
2164 | ldr DISPATCH, [lr] // Load DISPATCH.
2165 | add CARG3, sp, #64 // Recompute original value of sp.
2166 | mv_vmstate CARG4, EXIT
2167 | str CARG3, [sp, #52] // Store sp in RID_SP
2169 | ldr CARG2, [CARG1, #-4]! // Get exit instruction.
2170 | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC.
2171 | str CARG1, [sp, #60]
2175 | lsl CARG2, CARG2, #8
2176 | add CARG1, CARG1, CARG2, asr #6
2177 | ldr CARG2, [lr, #4] // Load exit stub group offset.
2178 | sub CARG1, CARG1, lr
2179 | ldr L, [DISPATCH, #DISPATCH_GL(cur_L)]
2180 | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number.
2181 | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2182 | str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2185 | str L, [DISPATCH, #DISPATCH_J(L)]
2186 | str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)]
2187 | sub CARG1, DISPATCH, #-GG_DISP2J
2189 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2190 | // Returns MULTRES (unscaled) or negated error code.
2191 | ldr CARG2, L->cframe
2193 | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2195 | ldr PC, SAVE_PC // Get SAVE_PC.
2196 | str L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2200 | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2205 | blt >9 // Check for error from exit.
2207 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2208 | str RC, SAVE_MULTRES
2211 | ldr CARG2, LFUNC:CARG2->field_pc
2212 | str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)]
2213 | mv_vmstate CARG4, INTERP
2214 | ldr KBASE, [CARG2, #PC2PROTO(k)]
2215 | // Modified copy of ins_next which handles function header dispatch, too.
2219 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2221 | cmp OP, #BC_FUNCC+2 // Fast function?
2224 | cmp OP, #BC_FUNCF // Function header?
2225 | ldr OP, [DISPATCH, OP, lsl #2]
2226 | decode_RA8 RA, INS
2227 | lsrlo RC, INS, #16 // No: Decode operands A*8 and D.
2229 | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8
2230 | ldrhs CARG3, [BASE, FRAME_FUNC]
2233 |4: // Check frame below fast function.
2234 | ldr CARG1, [BASE, FRAME_PC]
2235 | ands CARG2, CARG1, #FRAME_TYPE
2236 | bne <2 // Trace stitching continuation?
2237 | // Otherwise set KBASE for Lua function below fast function.
2238 | ldr CARG3, [CARG1, #-4]
2239 | decode_RA8 CARG1, CARG3
2240 | sub CARG2, BASE, CARG1
2241 | ldr LFUNC:CARG3, [CARG2, #-16]
2242 | ldr CARG3, LFUNC:CARG3->field_pc
2243 | ldr KBASE, [CARG3, #PC2PROTO(k)]
2246 |9: // Rethrow error from the right C frame.
2247 | rsb CARG2, CARG1, #0
2249 | bl extern lj_err_throw // (lua_State *L, int errcode)
2252 |//-----------------------------------------------------------------------
2253 |//-- Math helper functions ----------------------------------------------
2254 |//-----------------------------------------------------------------------
2256 |// FP value rounding. Called from JIT code.
2258 |// double lj_vm_floor/ceil/trunc(double x);
2259 |.macro vm_round, func, hf
2261 | vmov CARG1, CARG2, d0
2263 | lsl CARG3, CARG2, #1
2264 | adds RB, CARG3, #0x00200000
2265 | bpl >2 // |x| < 1?
2267 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2268 | bxlo lr // |x| >= 2^52: done.
2270 | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask
2271 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2273 | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2274 | orrpl CARG3, CARG3, CARG4
2276 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2277 |.if "func" == "floor"
2278 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2280 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2283 | vmoveq d0, CARG1, CARG2
2285 | bxeq lr // iszero: done.
2288 | lslpl CARG3, CARG4, RB
2291 | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask
2292 | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry
2294 | vmov d0, CARG1, CARG2
2299 | bxcs lr // |x| is not finite.
2300 | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo
2301 |.if "func" == "floor"
2302 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2304 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2306 | mov CARG1, #0 // lo = 0
2307 | and CARG2, CARG2, #0x80000000
2308 | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2309 | orrne CARG2, CARG2, CARG4
2311 | vmov d0, CARG1, CARG2
2317 | .long 0x3ff00000 // hiword(+1.0)
2333 |.macro vm_trunc, hf
2336 | vmov CARG1, CARG2, d0
2338 | lsl CARG3, CARG2, #1
2339 | adds RB, CARG3, #0x00200000
2340 | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0.
2343 | vmovpl d0, CARG1, CARG2
2347 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2348 | bxlo lr // |x| >= 2^52: already done.
2350 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2352 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2354 | vmov d0, CARG1, CARG2
2367 | // double lj_vm_mod(double dividend, double divisor);
2370 | // Special calling convention. Also, RC (r11) is not preserved.
2371 | vdiv.f64 d0, d6, d7
2373 | vmov CARG1, CARG2, d0
2375 | vmov d0, CARG1, CARG2
2376 | vmul.f64 d0, d0, d7
2378 | vsub.f64 d6, d6, d0
2381 | push {r0, r1, r2, r3, r4, lr}
2382 | bl extern __aeabi_ddiv
2384 | ldrd CARG34, [sp, #8]
2385 | bl extern __aeabi_dmul
2387 | eor CARG2, CARG2, #0x80000000
2388 | bl extern __aeabi_dadd
2393 | // int lj_vm_modi(int dividend, int divisor);
2395 | ands RB, CARG1, #0x80000000
2396 | rsbmi CARG1, CARG1, #0 // a = |dividend|
2397 | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor).
2399 | rsbmi CARG2, CARG2, #0 // b = |divisor|
2400 | subs CARG4, CARG2, #1
2401 | cmpne CARG1, CARG2
2402 | moveq CARG1, #0 // if (b == 1 || a == b) a = 0
2403 | tsthi CARG2, CARG4
2404 | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1
2406 | // Use repeated subtraction to get the remainder.
2409 | sub CARG4, CARG4, CARG3
2410 | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8
2411 | addne pc, pc, CARG3, lsl #3 // Duff's device.
2415 for (i = 31; i >= 0; i--) {
2416 | cmp CARG1, CARG2, lsl #i
2417 | subhs CARG1, CARG1, CARG2, lsl #i
2423 | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b
2424 | eors CARG2, CARG1, RB, lsl #1
2425 | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y
2428 |//-----------------------------------------------------------------------
2429 |//-- Miscellaneous functions --------------------------------------------
2430 |//-----------------------------------------------------------------------
2432 |//-----------------------------------------------------------------------
2433 |//-- FFI helper functions -----------------------------------------------
2434 |//-----------------------------------------------------------------------
2436 |// Handler for callback functions.
2437 |// Saveregs already performed. Callback slot number in [sp], g in r12.
2440 |.type CTSTATE, CTState, PC
2441 | ldr CTSTATE, GL:r12->ctype_state
2442 | add DISPATCH, r12, #GG_G2DISP
2445 | add r4, sp, CFRAME_SPACE+4+8*8
2446 | vstmdb r4!, {d8-d15}
2449 | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2451 | strd CARG34, CTSTATE->cb.gpr[2]
2452 | strd CARG12, CTSTATE->cb.gpr[0]
2454 | vstmdb r12!, {d0-d7}
2457 | add CARG3, sp, #CFRAME_SIZE
2458 | mov CARG1, CTSTATE
2459 | lsr CARG4, CARG4, #3
2460 | str CARG3, CTSTATE->cb.stack
2462 | str CARG4, CTSTATE->cb.slot
2463 | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2464 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2465 | // Returns lua_State *.
2466 | ldr BASE, L:CRET1->base
2467 | mv_vmstate CARG2, INTERP
2468 | ldr RC, L:CRET1->top
2470 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2473 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2478 |->cont_ffi_callback: // Return from FFI callback.
2480 | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2484 | mov CARG1, CTSTATE
2486 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2487 | ldrd CARG12, CTSTATE->cb.gpr[0]
2489 | vldr d0, CTSTATE->cb.fpr[0]
2494 |->vm_ffi_call: // Call C function via FFI.
2495 | // Caveat: needs special frame unwinding, see below.
2497 | .type CCSTATE, CCallState, r4
2498 | push {CCSTATE, r5, r11, lr}
2499 | mov CCSTATE, CARG1
2500 | ldr CARG1, CCSTATE:CARG1->spadj
2501 | ldrb CARG2, CCSTATE->nsp
2502 | add CARG3, CCSTATE, #offsetof(CCallState, stack)
2504 | add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2507 | sub sp, sp, CARG1 // Readjust stack.
2508 | subs CARG2, CARG2, #1
2512 | ldr RB, CCSTATE->func
2514 |1: // Copy stack slots.
2515 | ldr CARG4, [CARG3, CARG2, lsl #2]
2516 | str CARG4, [sp, CARG2, lsl #2]
2517 | subs CARG2, CARG2, #1
2520 | ldrd CARG12, CCSTATE->gpr[0]
2521 | ldrd CARG34, CCSTATE->gpr[2]
2525 | add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2527 | strd CRET1, CCSTATE->gpr[0]
2529 | vstmdb r12!, {d0-d3}
2531 | pop {CCSTATE, r5, r11, pc}
2533 |// Note: vm_ffi_call must be the last function in this object file!
2535 |//-----------------------------------------------------------------------
2538 /* Generate the code for a single instruction. */
2539 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2546 /* -- Comparison ops ---------------------------------------------------- */
2548 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2550 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2551 | // RA = src1*8, RC = src2, JMP with RC = target
2553 | ldrd CARG12, [RA, BASE]!
2555 | ldrd CARG34, [RC, BASE]!
2557 | add RB, PC, RB, lsl #2
2558 | checktp CARG2, LJ_TISNUM
2560 | checktp CARG4, LJ_TISNUM
2563 if (op == BC_ISLT) {
2564 | sublt PC, RB, #0x20000
2565 } else if (op == BC_ISGE) {
2566 | subge PC, RB, #0x20000
2567 } else if (op == BC_ISLE) {
2568 | suble PC, RB, #0x20000
2570 | subgt PC, RB, #0x20000
2575 |3: // CARG12 is not an integer.
2579 | // d0 is a number.
2580 | checktp CARG4, LJ_TISNUM
2584 | // d0 is a number, CARG3 is an integer.
2586 | vcvt.f64.s32 d1, s4
2588 |4: // CARG1 is an integer, CARG34 is not an integer.
2591 | // CARG1 is an integer, d1 is a number.
2593 | vcvt.f64.s32 d0, s4
2594 |5: // d0 and d1 are numbers.
2597 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2598 if (op == BC_ISLT) {
2599 | sublo PC, RB, #0x20000
2600 } else if (op == BC_ISGE) {
2601 | subhs PC, RB, #0x20000
2602 } else if (op == BC_ISLE) {
2603 | subls PC, RB, #0x20000
2605 | subhi PC, RB, #0x20000
2610 | // CARG12 is a number.
2611 | checktp CARG4, LJ_TISNUM
2612 | movlo RA, RB // Save RB.
2615 | // CARG12 is a number, CARG3 is an integer.
2618 | mov RA, RB // Save RB.
2619 | bl extern __aeabi_i2d
2622 | ldrd CARG12, [RC] // Restore first operand.
2624 |4: // CARG1 is an integer, CARG34 is not an integer.
2626 | // CARG1 is an integer, CARG34 is a number.
2627 | mov RA, RB // Save RB.
2628 | bl extern __aeabi_i2d
2629 | ldrd CARG34, [RC] // Restore second operand.
2630 |5: // CARG12 and CARG34 are numbers.
2631 | bl extern __aeabi_cdcmple
2632 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2633 if (op == BC_ISLT) {
2634 | sublo PC, RA, #0x20000
2635 } else if (op == BC_ISGE) {
2636 | subhs PC, RA, #0x20000
2637 } else if (op == BC_ISLE) {
2638 | subls PC, RA, #0x20000
2640 | subhi PC, RA, #0x20000
2646 case BC_ISEQV: case BC_ISNEV:
2647 vk = op == BC_ISEQV;
2648 | // RA = src1*8, RC = src2, JMP with RC = target
2650 | ldrd CARG12, [RA, BASE]!
2652 | ldrd CARG34, [RC, BASE]!
2654 | add RB, PC, RB, lsl #2
2655 | checktp CARG2, LJ_TISNUM
2656 | cmnls CARG4, #-LJ_TISNUM
2662 | // Either or both types are not numbers.
2664 | checktp CARG2, LJ_TCDATA
2665 | checktpne CARG4, LJ_TCDATA
2666 | beq ->vmeta_equal_cd
2668 | cmp CARG2, CARG4 // Compare types.
2669 | bne >2 // Not the same type?
2670 | checktp CARG2, LJ_TISPRI
2671 | bhs >1 // Same type and primitive type?
2673 | // Same types and not a primitive type. Compare GCobj or pvalue.
2676 | bne >3 // Different GCobjs or pvalues?
2677 |1: // Branch if same.
2678 | sub PC, RB, #0x20000
2682 | checktp CARG2, LJ_TISTABUD
2683 | bhi <2 // Different objects and not table/ud?
2685 | beq >1 // Same GCobjs or pvalues?
2686 | checktp CARG2, LJ_TISTABUD
2687 | bhi >2 // Different objects and not table/ud?
2689 | // Different tables or userdatas. Need to check __eq metamethod.
2690 | // Field metatable must be at same offset for GCtab and GCudata!
2691 | ldr TAB:RA, TAB:CARG1->metatable
2694 | beq <2 // No metatable?
2696 | beq >2 // No metatable?
2698 | ldrb RA, TAB:RA->nomm
2699 | mov CARG4, #1-vk // ne = 0 or 1.
2702 | beq ->vmeta_equal // 'no __eq' flag not set?
2706 |2: // Branch if different.
2707 | sub PC, RB, #0x20000
2713 case BC_ISEQS: case BC_ISNES:
2714 vk = op == BC_ISEQS;
2715 | // RA = src*8, RC = str_const (~), JMP with RC = target
2717 | ldrd CARG12, [BASE, RA]
2719 | ldr STR:CARG3, [KBASE, RC, lsl #2]
2721 | add RB, PC, RB, lsl #2
2722 | checktp CARG2, LJ_TSTR
2727 | cmpeq CARG1, CARG3
2730 | subeq PC, RB, #0x20000
2734 | subne PC, RB, #0x20000
2740 | checktp CARG2, LJ_TCDATA
2742 | b ->vmeta_equal_cd
2746 case BC_ISEQN: case BC_ISNEN:
2747 vk = op == BC_ISEQN;
2748 | // RA = src*8, RC = num_const (~), JMP with RC = target
2750 | ldrd CARG12, [RA, BASE]!
2752 | ldrd CARG34, [RC, KBASE]!
2754 | add RB, PC, RB, lsl #2
2760 | checktp CARG2, LJ_TISNUM
2762 | checktp CARG4, LJ_TISNUM
2766 | subeq PC, RB, #0x20000
2770 | subne PC, RB, #0x20000
2775 |3: // CARG12 is not an integer.
2780 | subhi PC, RB, #0x20000
2785 | checktp CARG4, LJ_TISNUM
2789 | vcvths.f64.s32 d1, s4
2791 |4: // CARG1 is an integer, d1 is a number.
2794 | vcvt.f64.s32 d0, s4
2795 |5: // d0 and d1 are numbers.
2799 | subeq PC, RB, #0x20000
2801 | subne PC, RB, #0x20000
2805 | // CARG12 is a number.
2806 | checktp CARG4, LJ_TISNUM
2807 | movlo RA, RB // Save RB.
2809 | // CARG12 is a number, CARG3 is an integer.
2812 |4: // CARG1 is an integer, CARG34 is a number.
2813 | mov RA, RB // Save RB.
2814 | bl extern __aeabi_i2d
2815 | ldrd CARG34, [RC] // Restore other operand.
2816 |5: // CARG12 and CARG34 are numbers.
2817 | bl extern __aeabi_cdcmpeq
2819 | subeq PC, RA, #0x20000
2821 | subne PC, RA, #0x20000
2828 | checktp CARG2, LJ_TCDATA
2830 | b ->vmeta_equal_cd
2834 case BC_ISEQP: case BC_ISNEP:
2835 vk = op == BC_ISEQP;
2836 | // RA = src*8, RC = primitive_type (~), JMP with RC = target
2837 | ldrd CARG12, [BASE, RA]
2841 | add RB, PC, RB, lsl #2
2843 | checktp CARG2, LJ_TCDATA
2844 | beq ->vmeta_equal_cd
2848 | subeq PC, RB, #0x20000
2850 | subne PC, RB, #0x20000
2855 /* -- Unary test and copy ops ------------------------------------------- */
2857 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2858 | // RA = dst*8 or unused, RC = src, JMP with RC = target
2859 | add RC, BASE, RC, lsl #3
2863 | add RB, PC, RB, lsl #2
2864 | checktp CARG2, LJ_TTRUE
2865 if (op == BC_ISTC || op == BC_IST) {
2866 | subls PC, RB, #0x20000
2867 if (op == BC_ISTC) {
2868 | strdls CARG12, [BASE, RA]
2871 | subhi PC, RB, #0x20000
2872 if (op == BC_ISFC) {
2873 | strdhi CARG12, [BASE, RA]
2880 | // RA = src*8, RC = -type
2881 | ldrd CARG12, [BASE, RA]
2885 | bne ->vmeta_istype
2889 | // RA = src*8, RC = -(TISNUM-1)
2890 | ldrd CARG12, [BASE, RA]
2892 | checktp CARG2, LJ_TISNUM
2894 | bhs ->vmeta_istype
2898 /* -- Unary ops --------------------------------------------------------- */
2901 | // RA = dst*8, RC = src
2904 | ldrd CARG12, [BASE, RC]
2906 | strd CARG12, [BASE, RA]
2910 | // RA = dst*8, RC = src
2911 | add RC, BASE, RC, lsl #3
2913 | ldr CARG1, [RC, #4]
2916 | checktp CARG1, LJ_TTRUE
2917 | mvnls CARG2, #~LJ_TFALSE
2918 | mvnhi CARG2, #~LJ_TTRUE
2919 | str CARG2, [RA, #4]
2923 | // RA = dst*8, RC = src
2925 | ldrd CARG12, [BASE, RC]
2928 | checktp CARG2, LJ_TISNUM
2930 | eorne CARG2, CARG2, #0x80000000
2932 | rsbseq CARG1, CARG1, #0
2935 | strd CARG12, [BASE, RA]
2940 | .long 0x00000000, 0x41e00000 // 2^31.
2943 | // RA = dst*8, RC = src
2945 | ldrd CARG12, [BASE, RC]
2946 | checkstr CARG2, >2
2947 | ldr CARG1, STR:CARG1->len
2949 | mvn CARG2, #~LJ_TISNUM
2952 | strd CARG12, [BASE, RA]
2955 | checktab CARG2, ->vmeta_len
2957 | ldr TAB:CARG3, TAB:CARG1->metatable
2964 | bl extern lj_tab_len // (GCtab *t)
2965 | // Returns uint32_t (but less than 2^31).
2970 | ldrb CARG4, TAB:CARG3->nomm
2971 | tst CARG4, #1<<MM_len
2972 | bne <3 // 'no __len' flag set: done.
2977 /* -- Binary ops -------------------------------------------------------- */
2979 |.macro ins_arithcheck, cond, ncond, target
2981 | cmn CARG4, #-LJ_TISNUM
2982 | cmn..cond CARG2, #-LJ_TISNUM
2984 | cmn CARG2, #-LJ_TISNUM
2985 | cmn..cond CARG4, #-LJ_TISNUM
2989 |.macro ins_arithcheck_int, target
2990 | ins_arithcheck eq, ne, target
2992 |.macro ins_arithcheck_num, target
2993 | ins_arithcheck lo, hs, target
2996 |.macro ins_arithpre
2997 | decode_RB8 RB, INS
2998 | decode_RC8 RC, INS
2999 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3000 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3004 | ldrd CARG12, [RB, BASE]!
3005 | ldrd CARG34, [RC, KBASE]!
3007 | ldrd CARG12, [BASE, RB]
3008 | ldrd CARG34, [KBASE, RC]
3013 | ldrd CARG34, [RB, BASE]!
3014 | ldrd CARG12, [RC, KBASE]!
3016 | ldrd CARG34, [BASE, RB]
3017 | ldrd CARG12, [KBASE, RC]
3022 | ldrd CARG12, [RB, BASE]!
3023 | ldrd CARG34, [RC, BASE]!
3025 | ldrd CARG12, [BASE, RB]
3026 | ldrd CARG34, [BASE, RC]
3032 |.macro ins_arithpre_fpu, reg1, reg2
3044 |.macro ins_arithpost_fpu, reg
3052 |.macro ins_arithfallback, ins
3055 | ins ->vmeta_arith_vn
3058 | ins ->vmeta_arith_nv
3061 | ins ->vmeta_arith_vv
3066 |.macro ins_arithdn, intins, fpins, fpcall
3068 |.if "intins" ~= "vm_modi" and not FPU
3071 | ins_arithcheck_int >5
3072 |.if "intins" == "smull"
3073 | smull CARG1, RC, CARG3, CARG1
3074 | cmp RC, CARG1, asr #31
3075 | ins_arithfallback bne
3076 |.elif "intins" == "vm_modi"
3078 | ins_arithfallback beq
3080 | mvn CARG2, #~LJ_TISNUM
3082 | intins CARG1, CARG1, CARG3
3083 | ins_arithfallback bvs
3086 |.if "intins" == "vm_modi" or FPU
3090 | strd CARG12, [BASE, RA]
3093 | ins_arithpre_fpu d6, d7
3094 | ins_arithfallback ins_arithcheck_num
3096 |.if "intins" == "vm_modi"
3101 | ins_arithpost_fpu d6
3104 |.if "intins" ~= "vm_modi"
3111 |.macro ins_arithfp, fpins, fpcall
3113 |.if "fpins" ~= "extern" or HFABI
3114 | ins_arithpre_fpu d0, d1
3116 | ins_arithfallback ins_arithcheck_num
3117 |.if "fpins" == "extern"
3126 |.if ("fpins" ~= "extern" or HFABI) and FPU
3127 | ins_arithpost_fpu d0
3131 | strd CARG12, [BASE, RA]
3136 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3137 | ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3139 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3140 | ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3142 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3143 | ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3145 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3146 | ins_arithfp vdiv.f64, extern __aeabi_ddiv
3148 case BC_MODVN: case BC_MODNV: case BC_MODVV:
3149 | ins_arithdn vm_modi, vm_mod, ->vm_mod
3152 | // NYI: (partial) integer arithmetic.
3153 | ins_arithfp extern, extern pow
3157 | decode_RB8 RC, INS
3158 | decode_RC8 RB, INS
3159 | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!)
3162 | add CARG2, BASE, RB
3164 | // RA = dst*8, RC = src_start*8, CARG2 = top-1
3167 | lsr CARG3, CARG3, #3
3168 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3169 | // Returns NULL (finished) or TValue * (metamethod).
3173 | ldrd CARG34, [BASE, RC]
3176 | strd CARG34, [BASE, RA] // Copy result to RA.
3180 /* -- Constant ops ------------------------------------------------------ */
3183 | // RA = dst*8, RC = str_const (~)
3186 | ldr CARG1, [KBASE, RC, lsl #2]
3187 | mvn CARG2, #~LJ_TSTR
3189 | strd CARG12, [BASE, RA]
3194 | // RA = dst*8, RC = cdata_const (~)
3197 | ldr CARG1, [KBASE, RC, lsl #2]
3198 | mvn CARG2, #~LJ_TCDATA
3200 | strd CARG12, [BASE, RA]
3205 | // RA = dst*8, (RC = int16_literal)
3206 | mov CARG1, INS, asr #16 // Refetch sign-extended reg.
3207 | mvn CARG2, #~LJ_TISNUM
3210 | strd CARG12, [BASE, RA]
3214 | // RA = dst*8, RC = num_const
3217 | ldrd CARG12, [KBASE, RC]
3219 | strd CARG12, [BASE, RA]
3223 | // RA = dst*8, RC = primitive_type (~)
3232 | // RA = base*8, RC = end
3234 | add RC, BASE, RC, lsl #3
3235 | mvn CARG1, #~LJ_TNIL
3236 | str CARG1, [RA, #4]
3239 | str CARG1, [RA, #4]
3246 /* -- Upvalue and function ops ------------------------------------------ */
3249 | // RA = dst*8, RC = uvnum
3250 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3252 | add RC, RC, #offsetof(GCfuncL, uvptr)
3253 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3254 | ldr CARG2, UPVAL:CARG2->v
3255 | ldrd CARG34, [CARG2]
3258 | strd CARG34, [BASE, RA]
3262 | // RA = uvnum*8, RC = src
3263 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3265 | add RA, RA, #offsetof(GCfuncL, uvptr)
3267 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3268 | ldrd CARG34, [BASE, RC]
3269 | ldrb RB, UPVAL:CARG2->marked
3270 | ldrb RC, UPVAL:CARG2->closed
3271 | ldr CARG2, UPVAL:CARG2->v
3272 | tst RB, #LJ_GC_BLACK // isblack(uv)
3273 | add RB, CARG4, #-LJ_TISGCV
3275 | strd CARG34, [CARG2]
3276 | bne >2 // Upvalue is closed and black?
3280 |2: // Check if new value is collectable.
3281 | cmn RB, #-(LJ_TNUMX - LJ_TISGCV)
3282 | ldrbhi RC, GCOBJ:CARG3->gch.marked
3283 | bls <1 // tvisgcv(v)
3284 | sub CARG1, DISPATCH, #-GG_DISP2G
3285 | tst RC, #LJ_GC_WHITES
3286 | // Crossed a write barrier. Move the barrier forward.
3290 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3293 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3298 | // RA = uvnum*8, RC = str_const (~)
3299 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3301 | add RA, RA, #offsetof(GCfuncL, uvptr)
3303 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3304 | ldr STR:CARG3, [KBASE, RC, lsl #2]
3305 | ldrb RB, UPVAL:CARG2->marked
3306 | ldrb RC, UPVAL:CARG2->closed
3307 | ldr CARG2, UPVAL:CARG2->v
3308 | mvn CARG4, #~LJ_TSTR
3309 | tst RB, #LJ_GC_BLACK // isblack(uv)
3310 | ldrb RB, STR:CARG3->marked
3311 | strd CARG34, [CARG2]
3316 |2: // Check if string is white and ensure upvalue is closed.
3317 | tst RB, #LJ_GC_WHITES // iswhite(str)
3319 | sub CARG1, DISPATCH, #-GG_DISP2G
3320 | // Crossed a write barrier. Move the barrier forward.
3324 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3327 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3332 | // RA = uvnum*8, RC = num_const
3333 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3335 | add RA, RA, #offsetof(GCfuncL, uvptr)
3337 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3338 | ldrd CARG34, [KBASE, RC]
3339 | ldr CARG2, UPVAL:CARG2->v
3342 | strd CARG34, [CARG2]
3346 | // RA = uvnum*8, RC = primitive_type (~)
3347 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3349 | add RA, RA, #offsetof(GCfuncL, uvptr)
3350 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3352 | ldr CARG2, UPVAL:CARG2->v
3355 | str RC, [CARG2, #4]
3360 | // RA = level*8, RC = target
3361 | ldr CARG3, L->openupval
3362 | add RC, PC, RC, lsl #2
3365 | sub PC, RC, #0x20000
3368 | add CARG2, BASE, RA
3369 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3376 | // RA = dst*8, RC = proto_const (~) (holding function prototype)
3379 | ldr CARG2, [KBASE, RC, lsl #2]
3381 | ldr CARG3, [BASE, FRAME_FUNC]
3383 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3384 | bl extern lj_func_newL_gc
3385 | // Returns GCfuncL *.
3387 | mvn CARG2, #~LJ_TFUNC
3390 | strd CARG12, [BASE, RA]
3394 /* -- Table ops --------------------------------------------------------- */
3398 | // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3399 if (op == BC_TDUP) {
3402 | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3403 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3410 if (op == BC_TNEW) {
3411 | lsl CARG2, RC, #21
3412 | lsr CARG3, RC, #11
3413 | asr RC, CARG2, #21
3414 | lsr CARG2, CARG2, #21
3416 | addeq CARG2, CARG2, #2
3417 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3418 | // Returns GCtab *.
3420 | ldr CARG2, [KBASE, RC, lsl #2]
3421 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3422 | // Returns GCtab *.
3425 | mvn CARG2, #~LJ_TTAB
3428 | strd CARG12, [BASE, RA]
3431 | bl extern lj_gc_step_fixtop // (lua_State *L)
3437 | // RA = dst*8, RC = str_const (~)
3439 | // RA = dst*8, RC = str_const (~)
3440 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3442 | ldr TAB:CARG1, LFUNC:CARG2->env
3443 | ldr STR:RC, [KBASE, RC, lsl #2]
3444 if (op == BC_GGET) {
3452 | decode_RB8 RB, INS
3453 | decode_RC8 RC, INS
3454 | // RA = dst*8, RB = table*8, RC = key*8
3455 | ldrd TAB:CARG12, [BASE, RB]
3456 | ldrd CARG34, [BASE, RC]
3457 | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12.
3458 | checktp CARG4, LJ_TISNUM // Integer key?
3459 | ldreq CARG4, TAB:CARG1->array
3460 | ldreq CARG2, TAB:CARG1->asize
3463 | add CARG4, CARG4, CARG3, lsl #3
3464 | cmp CARG3, CARG2 // In array part?
3465 | ldrdlo CARG34, [CARG4]
3467 | ins_next1 // Overwrites RB!
3468 | checktp CARG4, LJ_TNIL
3472 | strd CARG34, [BASE, RA]
3475 |5: // Check for __index if table value is nil.
3476 | ldr TAB:CARG2, TAB:CARG1->metatable
3478 | beq <1 // No metatable: done.
3479 | ldrb CARG2, TAB:CARG2->nomm
3480 | tst CARG2, #1<<MM_index
3481 | bne <1 // 'no __index' flag set: done.
3482 | decode_RB8 RB, INS // Restore RB.
3486 | checktp CARG4, LJ_TSTR // String key?
3487 | moveq STR:RC, CARG3
3492 | decode_RB8 RB, INS
3494 | // RA = dst*8, RB = table*8, RC = str_const (~)
3495 | ldrd CARG12, [BASE, RB]
3497 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3498 | checktab CARG2, ->vmeta_tgets1
3500 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3501 | ldr CARG3, TAB:CARG1->hmask
3502 | ldr CARG4, STR:RC->hash
3503 | ldr NODE:INS, TAB:CARG1->node
3504 | mov TAB:RB, TAB:CARG1
3505 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
3506 | add CARG3, CARG3, CARG3, lsl #1
3507 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3509 | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS.
3510 | ldrd CARG34, NODE:INS->val
3511 | ldr NODE:INS, NODE:INS->next
3512 | checktp CARG2, LJ_TSTR
3513 | cmpeq CARG1, STR:RC
3515 | checktp CARG4, LJ_TNIL
3520 | strd CARG34, [BASE, RA]
3523 |4: // Follow hash chain.
3526 | // End of hash chain: key not found, nil result.
3528 |5: // Check for __index if table value is nil.
3529 | ldr TAB:CARG1, TAB:RB->metatable
3530 | mov CARG3, #0 // Optional clear of undef. value (during load stall).
3531 | mvn CARG4, #~LJ_TNIL
3533 | beq <3 // No metatable: done.
3534 | ldrb CARG2, TAB:CARG1->nomm
3535 | tst CARG2, #1<<MM_index
3536 | bne <3 // 'no __index' flag set: done.
3540 | decode_RB8 RB, INS
3542 | // RA = dst*8, RB = table*8, RC = index
3543 | ldrd CARG12, [BASE, RB]
3544 | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12.
3545 | ldr CARG3, TAB:CARG1->asize
3546 | ldr CARG4, TAB:CARG1->array
3549 | ldrdlo CARG34, [CARG4, CARG2]
3551 | ins_next1 // Overwrites RB!
3552 | checktp CARG4, LJ_TNIL
3556 | strd CARG34, [BASE, RA]
3559 |5: // Check for __index if table value is nil.
3560 | ldr TAB:CARG2, TAB:CARG1->metatable
3562 | beq <1 // No metatable: done.
3563 | ldrb CARG2, TAB:CARG2->nomm
3564 | tst CARG2, #1<<MM_index
3565 | bne <1 // 'no __index' flag set: done.
3569 | decode_RB8 RB, INS
3570 | decode_RC8 RC, INS
3571 | // RA = dst*8, RB = table*8, RC = key*8
3572 | ldr TAB:CARG1, [BASE, RB]
3573 | ldr CARG2, [BASE, RC]
3574 | ldr CARG4, TAB:CARG1->array
3575 | ldr CARG3, TAB:CARG1->asize
3576 | add CARG4, CARG4, CARG2, lsl #3
3577 | cmp CARG2, CARG3 // In array part?
3579 | ldrd CARG12, [CARG4]
3583 | strd CARG12, [BASE, RA]
3588 | decode_RB8 RB, INS
3589 | decode_RC8 RC, INS
3590 | // RA = src*8, RB = table*8, RC = key*8
3591 | ldrd TAB:CARG12, [BASE, RB]
3592 | ldrd CARG34, [BASE, RC]
3593 | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12.
3594 | checktp CARG4, LJ_TISNUM // Integer key?
3595 | ldreq CARG2, TAB:CARG1->array
3596 | ldreq CARG4, TAB:CARG1->asize
3599 | add CARG2, CARG2, CARG3, lsl #3
3600 | cmp CARG3, CARG4 // In array part?
3601 | ldrlo INS, [CARG2, #4]
3603 | ins_next1 // Overwrites RB!
3604 | checktp INS, LJ_TNIL
3605 | ldrb INS, TAB:CARG1->marked
3606 | ldrd CARG34, [BASE, RA]
3609 | tst INS, #LJ_GC_BLACK // isblack(table)
3610 | strd CARG34, [CARG2]
3616 |5: // Check for __newindex if previous value is nil.
3617 | ldr TAB:RA, TAB:CARG1->metatable
3619 | beq <1 // No metatable: done.
3620 | ldrb RA, TAB:RA->nomm
3621 | tst RA, #1<<MM_newindex
3622 | bne <1 // 'no __newindex' flag set: done.
3623 | ldr INS, [PC, #-4] // Restore RA and RB.
3624 | decode_RB8 RB, INS
3625 | decode_RA8 RA, INS
3628 |7: // Possible table write barrier for the value. Skip valiswhite check.
3629 | barrierback TAB:CARG1, INS, CARG3
3633 | checktp CARG4, LJ_TSTR // String key?
3634 | moveq STR:RC, CARG3
3639 | decode_RB8 RB, INS
3641 | // RA = src*8, RB = table*8, RC = str_const (~)
3642 | ldrd CARG12, [BASE, RB]
3644 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3645 | checktab CARG2, ->vmeta_tsets1
3647 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3648 | ldr CARG3, TAB:CARG1->hmask
3649 | ldr CARG4, STR:RC->hash
3650 | ldr NODE:INS, TAB:CARG1->node
3651 | mov TAB:RB, TAB:CARG1
3652 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
3653 | add CARG3, CARG3, CARG3, lsl #1
3655 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3656 | strb CARG4, TAB:RB->nomm // Clear metamethod cache.
3658 | ldrd CARG12, NODE:INS->key
3659 | ldr CARG4, NODE:INS->val.it
3660 | ldr NODE:CARG3, NODE:INS->next
3661 | checktp CARG2, LJ_TSTR
3662 | cmpeq CARG1, STR:RC
3664 | ldrb CARG2, TAB:RB->marked
3665 | checktp CARG4, LJ_TNIL // Key found, but nil value?
3666 | ldrd CARG34, [BASE, RA]
3669 | tst CARG2, #LJ_GC_BLACK // isblack(table)
3670 | strd CARG34, NODE:INS->val
3675 |4: // Check for __newindex if previous value is nil.
3676 | ldr TAB:CARG1, TAB:RB->metatable
3678 | beq <2 // No metatable: done.
3679 | ldrb CARG1, TAB:CARG1->nomm
3680 | tst CARG1, #1<<MM_newindex
3681 | bne <2 // 'no __newindex' flag set: done.
3684 |5: // Follow hash chain.
3685 | movs NODE:INS, NODE:CARG3
3687 | // End of hash chain: key not found, add a new one.
3689 | // But check for __newindex first.
3690 | ldr TAB:CARG1, TAB:RB->metatable
3693 | cmp TAB:CARG1, #0 // No metatable: continue.
3695 | ldrbne CARG2, TAB:CARG1->nomm
3698 | tst CARG2, #1<<MM_newindex
3699 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3701 | mvn CARG4, #~LJ_TSTR
3702 | str STR:RC, TMPDlo
3705 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3706 | // Returns TValue *.
3708 | ldrd CARG34, [BASE, RA]
3709 | strd CARG34, [CRET1]
3710 | b <3 // No 2nd write barrier needed.
3712 |7: // Possible table write barrier for the value. Skip valiswhite check.
3713 | barrierback TAB:RB, CARG2, CARG3
3717 | decode_RB8 RB, INS
3719 | // RA = src*8, RB = table*8, RC = index
3720 | ldrd CARG12, [BASE, RB]
3721 | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12.
3722 | ldr CARG3, TAB:CARG1->asize
3723 | ldr RB, TAB:CARG1->array
3726 | ldrdlo CARG34, [CARG2, RB]!
3728 | ins_next1 // Overwrites RB!
3729 | checktp CARG4, LJ_TNIL
3730 | ldrb INS, TAB:CARG1->marked
3731 | ldrd CARG34, [BASE, RA]
3734 | tst INS, #LJ_GC_BLACK // isblack(table)
3735 | strd CARG34, [CARG2]
3741 |5: // Check for __newindex if previous value is nil.
3742 | ldr TAB:RA, TAB:CARG1->metatable
3744 | beq <1 // No metatable: done.
3745 | ldrb RA, TAB:RA->nomm
3746 | tst RA, #1<<MM_newindex
3747 | bne <1 // 'no __newindex' flag set: done.
3748 | ldr INS, [PC, #-4] // Restore INS.
3749 | decode_RA8 RA, INS
3752 |7: // Possible table write barrier for the value. Skip valiswhite check.
3753 | barrierback TAB:CARG1, INS, CARG3
3757 | decode_RB8 RB, INS
3758 | decode_RC8 RC, INS
3759 | // RA = src*8, RB = table*8, RC = key*8
3760 | ldr TAB:CARG2, [BASE, RB]
3761 | ldr CARG3, [BASE, RC]
3762 | ldrb INS, TAB:CARG2->marked
3763 | ldr CARG1, TAB:CARG2->array
3764 | ldr CARG4, TAB:CARG2->asize
3765 | tst INS, #LJ_GC_BLACK // isblack(table)
3766 | add CARG1, CARG1, CARG3, lsl #3
3769 | cmp CARG3, CARG4 // In array part?
3772 | ldrd CARG34, [BASE, RA]
3775 | strd CARG34, [CARG1]
3778 |7: // Possible table write barrier for the value. Skip valiswhite check.
3779 | barrierback TAB:CARG2, INS, RB
3784 | // RA = base*8 (table at base-1), RC = num_const (start index)
3787 | ldr RB, SAVE_MULTRES
3788 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
3789 | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
3791 | ldr CARG4, TAB:CARG2->asize
3792 | beq >4 // Nothing to copy?
3793 | add CARG3, CARG1, RB, lsr #3
3795 | ldr CARG4, TAB:CARG2->array
3798 | add INS, CARG4, CARG1, lsl #3
3799 | ldrb CARG1, TAB:CARG2->marked
3800 |3: // Copy result slots to table.
3801 | ldrd CARG34, [RA], #8
3802 | strd CARG34, [INS], #8
3805 | tst CARG1, #LJ_GC_BLACK // isblack(table)
3810 |5: // Need to resize array part.
3814 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3815 | // Must not reallocate the stack.
3816 | .IOS ldr BASE, L->base
3819 |7: // Possible table write barrier for any value. Skip valiswhite check.
3820 | barrierback TAB:CARG2, CARG1, CARG3
3824 /* -- Calls and vararg handling ----------------------------------------- */
3827 | // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3828 | ldr CARG1, SAVE_MULTRES
3829 | decode_RC8 NARGS8:RC, INS
3830 | add NARGS8:RC, NARGS8:RC, CARG1
3834 | decode_RC8 NARGS8:RC, INS
3835 | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3837 | mov RB, BASE // Save old BASE for vmeta_call.
3838 | ldrd CARG34, [BASE, RA]!
3839 | sub NARGS8:RC, NARGS8:RC, #8
3840 | add BASE, BASE, #8
3841 | checkfunc CARG4, ->vmeta_call
3846 | // RA = base*8, (RB = 0,) RC = extra_nargs
3847 | ldr CARG1, SAVE_MULTRES
3848 | add NARGS8:RC, CARG1, RC, lsl #3
3852 | lsl NARGS8:RC, RC, #3
3853 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3855 | ldrd LFUNC:CARG34, [RA, BASE]!
3856 | sub NARGS8:RC, NARGS8:RC, #8
3858 | checkfunc CARG4, ->vmeta_callt
3859 | ldr PC, [BASE, FRAME_PC]
3862 | ldrb CARG4, LFUNC:CARG3->ffid
3863 | tst PC, #FRAME_TYPE
3866 | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
3870 | ldrd CARG12, [RA, RB]
3872 | cmp INS, NARGS8:RC
3873 | strd CARG12, [BASE, RB]
3877 | cmp CARG4, #1 // (> FF_C) Calling a fast function?
3882 |5: // Tailcall to a fast function with a Lua frame below.
3883 | ldr INS, [PC, #-4]
3884 | decode_RA8 RA, INS
3885 | sub CARG1, BASE, RA
3886 | ldr LFUNC:CARG1, [CARG1, #-16]
3887 | ldr CARG1, LFUNC:CARG1->field_pc
3888 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3891 |7: // Tailcall from a vararg function.
3892 | eor PC, PC, #FRAME_VARG
3893 | tst PC, #FRAME_TYPEP // Vararg frame below?
3894 | movne CARG4, #0 // Clear ffid if no Lua function below.
3896 | sub BASE, BASE, PC
3897 | ldr PC, [BASE, FRAME_PC]
3898 | tst PC, #FRAME_TYPE
3899 | movne CARG4, #0 // Clear ffid if no Lua function below.
3904 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3906 | mov RB, BASE // Save old BASE for vmeta_call.
3907 | ldrd CARG34, [RA, #-16]
3908 | ldrd CARG12, [RA, #-8]
3910 | strd CARG34, [RA, #8] // Copy state.
3911 | strd CARG12, [RA, #16] // Copy control var.
3912 | // STALL: locked CARG34.
3913 | ldrd LFUNC:CARG34, [RA, #-24]
3914 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3915 | // STALL: load CARG34.
3916 | strd LFUNC:CARG34, [RA] // Copy callable.
3917 | checkfunc CARG4, ->vmeta_call
3922 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3924 | // NYI: add hotloop, record BC_ITERN.
3927 | ldr TAB:RB, [RA, #-16]
3928 | ldr CARG1, [RA, #-8] // Get index from control var.
3929 | ldr INS, TAB:RB->asize
3930 | ldr CARG2, TAB:RB->array
3932 |1: // Traverse array part.
3933 | subs RC, CARG1, INS
3934 | add CARG3, CARG2, CARG1, lsl #3
3935 | bhs >5 // Index points after array part?
3936 | ldrd CARG34, [CARG3]
3937 | checktp CARG4, LJ_TNIL
3938 | addeq CARG1, CARG1, #1 // Skip holes in array part.
3940 | ldrh RC, [PC, #-2]
3941 | mvn CARG2, #~LJ_TISNUM
3942 | strd CARG34, [RA, #8]
3943 | add RC, PC, RC, lsl #2
3946 | sub PC, RC, #0x20000
3947 | str RB, [RA, #-8] // Update control var.
3951 |5: // Traverse hash part.
3952 | ldr CARG4, TAB:RB->hmask
3953 | ldr NODE:RB, TAB:RB->node
3955 | add CARG1, RC, RC, lsl #1
3956 | cmp RC, CARG4 // End of iteration? Branch to ITERL+1.
3957 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
3959 | ldrd CARG12, NODE:CARG3->val
3960 | checktp CARG2, LJ_TNIL
3962 | beq <6 // Skip holes in hash part.
3963 | ldrh RB, [PC, #-2]
3965 | ldrd CARG34, NODE:CARG3->key
3966 | str RC, [RA, #-8] // Update control var.
3967 | strd CARG12, [RA, #8]
3968 | add RC, PC, RB, lsl #2
3969 | sub PC, RC, #0x20000
3975 | // RA = base*8, RC = target (points to ITERN)
3977 | add RC, PC, RC, lsl #2
3978 | ldrd CFUNC:CARG12, [RA, #-24]
3979 | ldr CARG3, [RA, #-12]
3980 | ldr CARG4, [RA, #-4]
3981 | checktp CARG2, LJ_TFUNC
3982 | ldrbeq CARG1, CFUNC:CARG1->ffid
3983 | checktpeq CARG3, LJ_TTAB
3984 | checktpeq CARG4, LJ_TNIL
3985 | cmpeq CARG1, #FF_next_N
3986 | subeq PC, RC, #0x20000
3991 | mvn CARG2, #0x00018000
3992 | strd CARG1, [RA, #-8] // Initialize control var.
3995 |5: // Despecialize bytecode if any of the checks fail.
3996 | mov CARG1, #BC_JMP
3998 | strb CARG1, [PC, #-4]
3999 | sub PC, RC, #0x20000
4000 | strb OP, [PC] // Subsumes ins_next1.
4006 | decode_RB8 RB, INS
4007 | decode_RC8 RC, INS
4008 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4009 | ldr CARG1, [BASE, FRAME_PC]
4012 | add RC, RC, #FRAME_VARG
4014 | sub CARG3, BASE, #8 // CARG3 = vtop
4015 | sub RC, RC, CARG1 // RC = vbase
4016 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
4018 | sub CARG1, CARG3, RC
4019 | beq >5 // Copy all varargs?
4020 | sub CARG4, CARG4, #16
4021 |1: // Copy vararg slots to destination slots.
4023 | ldrdlo CARG12, [RC], #8
4024 | mvnhs CARG2, #~LJ_TNIL
4026 | strd CARG12, [RA], #8
4031 |5: // Copy all varargs.
4032 | ldr CARG4, L->maxstack
4034 | movle RB, #8 // MULTRES = (0+1)*8
4035 | addgt RB, CARG1, #8
4036 | add CARG2, RA, CARG1
4037 | str RB, SAVE_MULTRES
4042 | ldrd CARG12, [RC], #8
4043 | strd CARG12, [RA], #8
4048 |7: // Grow stack for varargs.
4049 | lsr CARG2, CARG1, #3
4053 | sub RC, RC, BASE // Need delta, because BASE may change.
4056 | bl extern lj_state_growstack // (lua_State *L, int n)
4060 | sub CARG3, BASE, #8
4064 /* -- Returns ----------------------------------------------------------- */
4067 | // RA = results*8, RC = extra results
4068 | ldr CARG1, SAVE_MULTRES
4069 | ldr PC, [BASE, FRAME_PC]
4071 | add RC, CARG1, RC, lsl #3
4076 | // RA = results*8, RC = nresults+1
4077 | ldr PC, [BASE, FRAME_PC]
4081 | str RC, SAVE_MULTRES
4083 | ands CARG1, PC, #FRAME_TYPE
4084 | eor CARG2, PC, #FRAME_VARG
4088 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
4089 | ldr INS, [PC, #-4]
4090 | subs CARG4, RC, #8
4091 | sub CARG3, BASE, #8
4094 | ldrd CARG12, [RA], #8
4095 | add BASE, BASE, #8
4096 | subs CARG4, CARG4, #8
4097 | strd CARG12, [BASE, #-16]
4100 | decode_RA8 RA, INS
4101 | sub CARG4, CARG3, RA
4102 | decode_RB8 RB, INS
4103 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4105 | cmp RB, RC // More results expected?
4108 | ldr CARG2, LFUNC:CARG1->field_pc
4111 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4114 |6: // Fill up results with nil.
4115 | mvn CARG2, #~LJ_TNIL
4116 | add BASE, BASE, #8
4118 | str CARG2, [BASE, #-12]
4121 |->BC_RETV1_Z: // Non-standard return case.
4124 | tst CARG2, #FRAME_TYPEP
4126 | // Return from vararg function: relocate BASE down.
4127 | sub BASE, BASE, CARG2
4128 | ldr PC, [BASE, FRAME_PC]
4132 case BC_RET0: case BC_RET1:
4133 | // RA = results*8, RC = nresults+1
4134 | ldr PC, [BASE, FRAME_PC]
4136 | str RC, SAVE_MULTRES
4137 | ands CARG1, PC, #FRAME_TYPE
4138 | eor CARG2, PC, #FRAME_VARG
4139 | ldreq INS, [PC, #-4]
4141 if (op == BC_RET1) {
4142 | ldrd CARG12, [BASE, RA]
4144 | sub CARG4, BASE, #8
4145 | decode_RA8 RA, INS
4146 if (op == BC_RET1) {
4147 | strd CARG12, [CARG4]
4149 | sub BASE, CARG4, RA
4150 | decode_RB8 RB, INS
4151 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4155 | ldr CARG2, LFUNC:CARG1->field_pc
4158 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4161 |6: // Fill up results with nil.
4162 | sub CARG2, CARG4, #4
4163 | mvn CARG3, #~LJ_TNIL
4164 | str CARG3, [CARG2, RC]
4169 /* -- Loops and branches ------------------------------------------------ */
4171 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
4172 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
4173 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4174 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
4180 | // Fall through. Assumes BC_IFORL follows.
4190 | // RA = base*8, RC = target (after end of loop or start of loop)
4191 vk = (op == BC_IFORL || op == BC_JFORL);
4192 | ldrd CARG12, [RA, BASE]!
4193 if (op != BC_JFORL) {
4194 | add RC, PC, RC, lsl #2
4197 | ldrd CARG34, FOR_STOP
4198 | checktp CARG2, LJ_TISNUM
4201 | checktp CARG4, LJ_TISNUM
4202 | ldr CARG4, FOR_STEP
4203 | checktpeq RB, LJ_TISNUM
4209 | ldrd CARG34, FOR_STEP
4210 | checktp CARG2, LJ_TISNUM
4212 | adds CARG1, CARG1, CARG3
4213 | ldr CARG4, FOR_STOP
4214 if (op == BC_IFORL) {
4215 | addvs RC, PC, #0x20000 // Overflow: prevent branch.
4217 | bvs >2 // Overflow: do not enter mcode.
4224 if (op == BC_FORI) {
4225 | subgt PC, RC, #0x20000
4226 } else if (op == BC_JFORI) {
4227 | sub PC, RC, #0x20000
4228 | ldrhle RC, [PC, #-2]
4229 } else if (op == BC_IFORL) {
4230 | suble PC, RC, #0x20000
4233 | strd CARG12, FOR_IDX
4238 | strd CARG12, FOR_EXT
4239 if (op == BC_JFORI || op == BC_JFORL) {
4245 |4: // Invert check for negative step.
4255 | cmnlo CARG4, #-LJ_TISNUM
4256 | cmnlo RB, #-LJ_TISNUM
4265 | strd CARG12, FOR_EXT
4274 | vadd.f64 d0, d0, d2
4278 | bl extern __aeabi_dadd
4279 | strd CARG12, FOR_IDX
4280 | ldrd CARG34, FOR_STOP
4281 | strd CARG12, FOR_EXT
4290 | bl extern __aeabi_cdcmple
4298 if (op == BC_FORI) {
4299 | subhi PC, RC, #0x20000
4300 } else if (op == BC_JFORI) {
4301 | sub PC, RC, #0x20000
4302 | ldrhls RC, [PC, #-2]
4304 } else if (op == BC_IFORL) {
4305 | subls PC, RC, #0x20000
4314 |8: // Invert check for negative step.
4316 | bl extern __aeabi_dadd
4317 | strd CARG12, FOR_IDX
4318 | strd CARG12, FOR_EXT
4322 | ldrd CARG12, FOR_STOP
4331 | // Fall through. Assumes BC_IITERL follows.
4339 | // RA = base*8, RC = target
4340 | ldrd CARG12, [RA, BASE]!
4341 if (op == BC_JITERL) {
4342 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4343 | strdne CARG12, [RA, #-8]
4346 | add RC, PC, RC, lsl #2
4347 | // STALL: load CARG12.
4348 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4349 | subne PC, RC, #0x20000 // Otherwise save control var + branch.
4350 | strdne CARG12, [RA, #-8]
4356 | // RA = base*8, RC = target (loop extent)
4357 | // Note: RA/RC is only used by trace recorder to determine scope/extent
4358 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4362 | // Fall through. Assumes BC_ILOOP follows.
4366 | // RA = base*8, RC = target (loop extent)
4372 | // RA = base (ignored), RC = traceno
4373 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4374 | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0.
4375 | ldr TRACE:RC, [CARG1, RC, lsl #2]
4377 | ldr RA, TRACE:RC->mcode
4378 | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4379 | str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)]
4385 | // RA = base*8 (only used by trace recorder), RC = target
4386 | add RC, PC, RC, lsl #2
4387 | sub PC, RC, #0x20000
4391 /* -- Function headers -------------------------------------------------- */
4397 case BC_FUNCV: /* NYI: compiled vararg functions. */
4398 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4406 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4407 | ldr CARG1, L->maxstack
4408 | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4409 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4411 | bhi ->vm_growstack_l
4412 if (op != BC_JFUNCF) {
4417 | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters.
4418 | mvn CARG4, #~LJ_TNIL
4420 if (op == BC_JFUNCF) {
4427 |3: // Clear missing parameters.
4428 | strd CARG34, [BASE, NARGS8:RC]
4429 | add NARGS8:RC, NARGS8:RC, #8
4437 | NYI // NYI: compiled vararg functions
4438 break; /* NYI: compiled vararg functions. */
4441 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4442 | ldr CARG1, L->maxstack
4443 | add CARG4, BASE, RC
4445 | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC.
4446 | add CARG2, RC, #8+FRAME_VARG
4447 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4449 | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG.
4450 | bhs ->vm_growstack_l
4451 | ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4455 | add BASE, CARG4, #8
4457 | mvn CARG3, #~LJ_TNIL
4459 | cmp RA, RC // Less args than parameters?
4460 | ldrdlo CARG12, [RA], #8
4461 | movhs CARG2, CARG3
4462 | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC).
4465 | strd CARG12, [CARG4, #8]!
4473 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4474 if (op == BC_FUNCC) {
4475 | ldr CARG4, CFUNC:CARG3->f
4477 | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4479 | add CARG2, RA, NARGS8:RC
4480 | ldr CARG1, L->maxstack
4481 | add RC, BASE, NARGS8:RC
4485 if (op == BC_FUNCCW) {
4486 | ldr CARG2, CFUNC:CARG3->f
4488 | mv_vmstate CARG3, C
4490 | bhi ->vm_growstack_c // Need to grow stack.
4492 | blx CARG4 // (lua_State *L [, lua_CFunction f])
4493 | // Returns nresults.
4495 | mv_vmstate CARG3, INTERP
4497 | str L, [DISPATCH, #DISPATCH_GL(cur_L)]
4500 | ldr PC, [BASE, FRAME_PC]
4501 | sub RA, CRET2, RC // RA = L->top - nresults*8
4505 /* ---------------------------------------------------------------------- */
4508 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4514 static int build_backend(BuildCtx *ctx)
4518 dasm_growpc(Dst, BC__MAX);
4520 build_subroutines(ctx);
4523 for (op = 0; op < BC__MAX; op++)
4524 build_ins(ctx, (BCOp)op, op);
4529 /* Emit pseudo frame-info for all assembler functions. */
4530 static void emit_asm_debug(BuildCtx *ctx)
4532 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4534 switch (ctx->mode) {
4536 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4539 "\t.long .LECIE0-.LSCIE0\n"
4541 "\t.long 0xffffffff\n"
4546 "\t.byte 0xe\n" /* Return address is in lr. */
4547 "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */
4552 "\t.long .LEFDE0-.LASFDE0\n"
4554 "\t.long .Lframe0\n"
4557 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4558 "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */
4559 fcofs, CFRAME_SIZE);
4560 for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */
4561 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4563 for (i = 15; i >= 8; i--) /* offset d8-d15 */
4564 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4565 64+2*i, 10+2*(15-i));
4566 fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */
4574 "\t.long .LEFDE1-.LASFDE1\n"
4576 "\t.long .Lframe0\n"
4577 "\t.long lj_vm_ffi_call\n"
4579 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4580 "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */
4581 "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */
4582 "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */
4583 "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */
4584 "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */
4586 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);