1 |// Low-level VM code for ARM CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2012 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
103 |//-----------------------------------------------------------------------
105 |// Trap for not-yet-implemented parts.
106 |.macro NYI; ud; .endmacro
108 |//-----------------------------------------------------------------------
110 |// Access to frame relative to BASE.
111 |.define FRAME_FUNC, #-8
112 |.define FRAME_PC, #-4
114 |.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro
115 |.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro
116 |.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro
117 |.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro
118 |.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro
120 |// Instruction fetch.
127 |// Instruction decode+dispatch.
129 | ldr OP, [DISPATCH, OP, lsl #2]
140 |// Instruction footer.
142 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
143 | .define ins_next, ins_NEXT
144 | .define ins_next_, ins_NEXT
145 | .define ins_next1, ins_NEXT1
146 | .define ins_next2, ins_NEXT2
147 | .define ins_next3, ins_NEXT3
149 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
150 | // Affects only certain kinds of benchmarks (and only with -j off).
167 |// Avoid register name substitution for field name.
170 |// Call decode and dispatch.
172 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
173 | ldr PC, LFUNC:CARG3->field_pc
174 | ldrb OP, [PC] // STALL: load PC. early PC.
176 | ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP.
183 | // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
184 | str PC, [BASE, FRAME_PC]
185 | ins_callt // STALL: locked PC.
188 |//-----------------------------------------------------------------------
190 |// Macros to test operand types.
191 |.macro checktp, reg, tp; cmn reg, #-tp; .endmacro
192 |.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro
193 |.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro
194 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro
195 |.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro
196 |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro
198 |// Assumes DISPATCH is relative to GL.
199 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
200 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
202 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
204 |.macro hotcheck, delta
206 | and CARG1, CARG1, #126
207 | sub CARG1, CARG1, #-GG_DISP2HOT
208 | ldrh CARG2, [DISPATCH, CARG1]
209 | subs CARG2, CARG2, #delta
210 | strh CARG2, [DISPATCH, CARG1]
214 | hotcheck HOTCOUNT_LOOP
219 | hotcheck HOTCOUNT_CALL
223 |// Set current VM state.
224 |.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro
225 |.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro
227 |// Move table write barrier back. Overwrites mark and tmp.
228 |.macro barrierback, tab, mark, tmp
229 | ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
230 | bic mark, mark, #LJ_GC_BLACK // black2gray(tab)
231 | str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
232 | strb mark, tab->marked
233 | str tmp, tab->gclist
242 |//-----------------------------------------------------------------------
245 #error "Only dual-number mode supported for ARM target"
248 /* Generate subroutines used by opcodes and other parts of the VM. */
249 /* The .code_sub section should be last to help static branch prediction. */
250 static void build_subroutines(BuildCtx *ctx)
254 |//-----------------------------------------------------------------------
255 |//-- Return handling ----------------------------------------------------
256 |//-----------------------------------------------------------------------
259 | // See vm_return. Also: RB = previous base.
261 | beq ->cont_dispatch
263 | // Return from pcall or xpcall fast func.
264 | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame.
265 | mvn CARG2, #~LJ_TTRUE
267 | // Prepending may overwrite the pcall frame, so do it at the end.
268 | str CARG2, [RA, FRAME_PC] // Prepend true to results.
272 | add RC, RC, #8 // RC = (nresults+1)*8.
273 | ands CARG1, PC, #FRAME_TYPE
274 | str RC, SAVE_MULTRES
275 | beq ->BC_RET_Z // Handle regular return to Lua.
278 | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return
279 | // CARG1 = PC & FRAME_TYPE
280 | bic RB, PC, #FRAME_TYPEP
281 | cmp CARG1, #FRAME_C
282 | sub RB, BASE, RB // RB = previous base.
286 | ldr KBASE, SAVE_NRES
287 | mv_vmstate CARG4, C
290 | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8
294 | subs CARG3, CARG3, #8
295 | ldrd CARG12, [RA], #8
296 | strd CARG12, [BASE], #8
299 | cmp KBASE, RC // More/less results wanted?
302 | str BASE, L->top // Store new top.
305 | ldr RC, SAVE_CFRAME // Restore previous C frame.
306 | mov CRET1, #0 // Ok return status for vm_pcall.
313 | blt >7 // Less results wanted?
314 | // More results wanted. Check stack size and fill up results with nil.
315 | ldr CARG3, L->maxstack
316 | mvn CARG2, #~LJ_TNIL
319 | str CARG2, [BASE, #4]
324 |7: // Less results wanted.
325 | sub CARG1, RC, KBASE
326 | cmp KBASE, #0 // LUA_MULTRET+1 case?
327 | subne BASE, BASE, CARG1 // Either keep top or shrink it.
330 |8: // Corner case: need to grow stack for filling up results.
331 | // This can happen if:
332 | // - A C function grows the stack (a lot).
333 | // - The GC shrinks the stack in between.
334 | // - A return back from a lua_call() with (high) nresults adjustment.
335 | str BASE, L->top // Save current top held in BASE (yes).
338 | bl extern lj_state_growstack // (lua_State *L, int n)
339 | ldr BASE, L->top // Need the (realloced) L->top in BASE.
342 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
343 | // (void *cframe, int errcode)
346 |->vm_unwind_c_eh: // Landing pad for external unwinder.
348 | mv_vmstate CARG4, C
349 | ldr GL:CARG3, L->glref
350 | str CARG4, GL:CARG3->vmstate
353 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
355 | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
357 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
360 | mov RC, #16 // 2 results: false + error message.
361 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
363 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
364 | mvn CARG1, #~LJ_TFALSE
365 | sub RA, BASE, #8 // Results start at BASE-8.
366 | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame.
367 | add DISPATCH, DISPATCH, #GG_G2DISP
368 | mv_vmstate CARG2, INTERP
369 | str CARG1, [BASE, #-4] // Prepend false to error message.
373 |//-----------------------------------------------------------------------
374 |//-- Grow stack for calls -----------------------------------------------
375 |//-----------------------------------------------------------------------
377 |->vm_growstack_c: // Grow stack for C function.
379 | mov CARG2, #LUA_MINSTACK
382 |->vm_growstack_l: // Grow stack for Lua function.
383 | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
388 | add PC, PC, #4 // Must point after first instruction.
392 | // L->base = new base, L->top = top
394 | bl extern lj_state_growstack // (lua_State *L, int n)
397 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
398 | sub NARGS8:RC, RC, BASE
399 | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
400 | ins_callt // Just retry the call.
402 |//-----------------------------------------------------------------------
403 |//-- Entry points into the assembler VM ---------------------------------
404 |//-----------------------------------------------------------------------
406 |->vm_resume: // Setup C frame and resume thread.
407 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
410 | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table.
412 | add DISPATCH, DISPATCH, #GG_G2DISP
415 | str CARG3, SAVE_NRES
416 | add CARG2, sp, #CFRAME_RESUME
417 | ldrb CARG1, L->status
418 | str CARG3, SAVE_ERRF
419 | str CARG2, L->cframe
420 | str CARG3, SAVE_CFRAME
422 | str L, SAVE_PC // Any value outside of bytecode is ok.
425 | // Resume after yield (like a return).
430 | strb CARG3, L->status
431 | sub RC, CARG1, BASE
432 | ldr PC, [BASE, FRAME_PC]
433 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
434 | mv_vmstate CARG2, INTERP
436 | ands CARG1, PC, #FRAME_TYPE
438 | str RC, SAVE_MULTRES
442 |->vm_pcall: // Setup protected C frame and enter VM.
443 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
446 | str CARG4, SAVE_ERRF
449 |->vm_call: // Setup C frame and enter VM.
450 | // (lua_State *L, TValue *base, int nres1)
454 |1: // Entry point for vm_pcall above (PC = ftype).
455 | ldr RC, L:CARG1->cframe
456 | str CARG3, SAVE_NRES
460 | str sp, L->cframe // Add our C frame to cframe chain.
461 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
462 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
463 | str RC, SAVE_CFRAME
464 | add DISPATCH, DISPATCH, #GG_G2DISP
466 |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
467 | ldr RB, L->base // RB = old base (for vmeta_call).
471 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
472 | sub PC, PC, RB // PC = frame delta + frame type
473 | mv_vmstate CARG2, INTERP
474 | sub NARGS8:RC, CARG1, BASE
478 | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
479 | ldrd CARG34, [BASE, FRAME_FUNC]
480 | checkfunc CARG4, ->vmeta_call
482 |->vm_call_dispatch_f:
484 | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC
486 |->vm_cpcall: // Setup protected C frame, call C.
487 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
490 | ldr RA, L:CARG1->stack
493 | str CARG1, SAVE_PC // Any value outside of bytecode is ok.
495 | sub RA, RA, RB // Compute -savestack(L, L->top).
496 | str sp, L->cframe // Add our C frame to cframe chain.
498 | str RA, SAVE_NRES // Neg. delta means cframe w/o frame.
499 | str RB, SAVE_ERRF // No error function.
500 | str RC, SAVE_CFRAME
501 | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud)
502 | ldr DISPATCH, L->glref // Setup pointer to dispatch table.
505 | add DISPATCH, DISPATCH, #GG_G2DISP
506 | bne <3 // Else continue with the call.
507 | b ->vm_leave_cp // No base? Just remove C frame.
509 |//-----------------------------------------------------------------------
510 |//-- Metamethod handling ------------------------------------------------
511 |//-----------------------------------------------------------------------
513 |//-- Continuation dispatch ----------------------------------------------
516 | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8
517 | ldr LFUNC:CARG3, [RB, FRAME_FUNC]
518 | ldr CARG1, [BASE, #-16] // Get continuation.
520 | mov BASE, RB // Restore caller BASE.
524 | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC].
525 | ldr CARG3, LFUNC:CARG3->field_pc
528 | str INS, [CARG2, #-4] // Ensure one valid arg.
532 | ldr KBASE, [CARG3, #PC2PROTO(k)]
533 | // BASE = base, RA = resultptr, CARG4 = meta base
538 | beq ->cont_ffi_callback // cont = 1: return from FFI callback.
539 | // cont = 0: tailcall from C function.
540 | ldr CARG3, [BASE, FRAME_FUNC]
541 | sub CARG4, CARG4, #16
542 | sub RC, CARG4, BASE
546 |->cont_cat: // RA = resultptr, CARG4 = meta base
548 | sub CARG2, CARG4, #16
553 | add CARG1, BASE, RC
554 | subs CARG1, CARG2, CARG1
555 | strdne CARG34, [CARG2]
558 | strd CARG34, [BASE, RA]
561 |//-- Table indexing metamethods -----------------------------------------
564 | add CARG2, BASE, RB
568 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
569 | mvn CARG4, #~LJ_TTAB
570 | str TAB:RB, [CARG2]
571 | str CARG4, [CARG2, #4]
573 | mvn CARG4, #~LJ_TSTR
579 |->vmeta_tgetb: // RC = index
582 | mvn CARG4, #~LJ_TISNUM
583 | add CARG2, BASE, RB
589 | add CARG2, BASE, RB
590 | add CARG3, BASE, RC
595 | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
596 | // Returns TValue * (finished) or NULL (metamethod).
597 | .IOS ldr BASE, L->base
600 | ldrd CARG34, [CRET1]
603 | strd CARG34, [BASE, RA]
606 |3: // Call __index metamethod.
607 | // BASE = base, L->top = new base, stack = cont/func/t/k
608 | rsb CARG1, BASE, #FRAME_CONT
610 | mov NARGS8:RC, #16 // 2 args for func(t, k).
611 | str PC, [BASE, #-12] // [cont|PC]
612 | add PC, CARG1, BASE
613 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
614 | b ->vm_call_dispatch_f
616 |//-----------------------------------------------------------------------
619 | add CARG2, BASE, RB
623 | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
624 | mvn CARG4, #~LJ_TTAB
625 | str TAB:RB, [CARG2]
626 | str CARG4, [CARG2, #4]
628 | mvn CARG4, #~LJ_TSTR
634 |->vmeta_tsetb: // RC = index
637 | mvn CARG4, #~LJ_TISNUM
638 | add CARG2, BASE, RB
644 | add CARG2, BASE, RB
645 | add CARG3, BASE, RC
650 | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
651 | // Returns TValue * (finished) or NULL (metamethod).
652 | .IOS ldr BASE, L->base
654 | ldrd CARG34, [BASE, RA]
657 | // NOBARRIER: lj_meta_tset ensures the table is not black.
658 | strd CARG34, [CRET1]
662 |3: // Call __newindex metamethod.
663 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
664 | rsb CARG1, BASE, #FRAME_CONT
666 | mov NARGS8:RC, #24 // 3 args for func(t, k, v).
667 | strd CARG34, [BASE, #16] // Copy value to third argument.
668 | str PC, [BASE, #-12] // [cont|PC]
669 | add PC, CARG1, BASE
670 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
671 | b ->vm_call_dispatch_f
673 |//-- Comparison metamethods ---------------------------------------------
682 | decode_OP CARG4, INS
683 | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
684 | // Returns 0/1 or TValue * (metamethod).
686 | .IOS ldr BASE, L->base
692 | add RB, PC, RB, lsl #2
693 | subhs PC, RB, #0x20000
697 |->cont_ra: // RA = resultptr
700 | decode_RA8 CARG3, INS
701 | strd CARG12, [BASE, CARG3]
704 |->cont_condt: // RA = resultptr
705 | ldr CARG2, [RA, #4]
706 | mvn CARG1, #~LJ_TTRUE
707 | cmp CARG1, CARG2 // Branch if result is true.
710 |->cont_condf: // RA = resultptr
711 | ldr CARG2, [RA, #4]
712 | checktp CARG2, LJ_TFALSE // Branch if result is false.
716 | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
721 | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
722 | // Returns 0/1 or TValue * (metamethod).
732 | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
733 | // Returns 0/1 or TValue * (metamethod).
737 |//-- Arithmetic metamethods ---------------------------------------------
742 | add CARG3, BASE, RB
743 | add CARG4, KBASE, RC
749 | add CARG4, BASE, RB
750 | add CARG3, KBASE, RC
756 | add CARG3, BASE, RC
757 | add CARG4, BASE, RC
763 | add CARG3, BASE, RB
764 | add CARG4, BASE, RC
767 | add CARG2, BASE, RA
772 | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
773 | // Returns NULL (finished) or TValue * (metamethod).
774 | .IOS ldr BASE, L->base
778 | // Call metamethod for binary op.
780 | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
781 | sub CARG2, CRET1, BASE
782 | str PC, [CRET1, #-12] // [cont|PC]
783 | add PC, CARG2, #FRAME_CONT
785 | mov NARGS8:RC, #16 // 2 args for func(o1, o2).
786 | b ->vm_call_dispatch
789 | add CARG2, BASE, RC
793 | bl extern lj_meta_len // (lua_State *L, TValue *o)
794 | // Returns NULL (retry) or TValue * (metamethod base).
795 | .IOS ldr BASE, L->base
796 #ifdef LUAJIT_ENABLE_LUA52COMPAT
798 | bne ->vmeta_binop // Binop call for compatibility.
799 | ldr TAB:CARG1, [BASE, RC]
802 | b ->vmeta_binop // Binop call for compatibility.
805 |//-- Call metamethod ----------------------------------------------------
807 |->vmeta_call: // Resolve and call __call metamethod.
808 | // RB = old base, BASE = new base, RC = nargs*8
810 | str RB, L->base // This is the callers base!
811 | sub CARG2, BASE, #8
813 | add CARG3, BASE, NARGS8:RC
815 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
817 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
818 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
821 |->vmeta_callt: // Resolve __call for BC_CALLT.
822 | // BASE = old base, RA = new base, RC = nargs*8
827 | add CARG3, RA, NARGS8:RC
828 | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
829 | .IOS ldr BASE, L->base
830 | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here.
831 | ldr PC, [BASE, FRAME_PC]
832 | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
835 |//-- Argument coercion for 'for' statement ------------------------------
842 | bl extern lj_meta_for // (lua_State *L, TValue *base)
843 | .IOS ldr BASE, L->base
858 |//-----------------------------------------------------------------------
859 |//-- Fast functions -----------------------------------------------------
860 |//-----------------------------------------------------------------------
866 |.macro .ffunc_1, name
868 | ldrd CARG12, [BASE]
873 |.macro .ffunc_2, name
875 | ldrd CARG12, [BASE]
876 | ldrd CARG34, [BASE, #8]
881 |.macro .ffunc_n, name
883 | checktp CARG2, LJ_TISNUM
887 |.macro .ffunc_nn, name
889 | checktp CARG2, LJ_TISNUM
890 | cmnlo CARG4, #-LJ_TISNUM
894 |.macro .ffunc_d, name
896 | ldr CARG2, [BASE, #4]
900 | checktp CARG2, LJ_TISNUM
904 |.macro .ffunc_dd, name
906 | ldr CARG2, [BASE, #4]
907 | ldr CARG4, [BASE, #12]
910 | vldr d1, [BASE, #8]
912 | checktp CARG2, LJ_TISNUM
913 | cmnlo CARG4, #-LJ_TISNUM
917 |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
919 | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
920 | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
925 |//-- Base library: checks -----------------------------------------------
928 | checktp CARG2, LJ_TTRUE
930 | ldr PC, [BASE, FRAME_PC]
931 | strd CARG12, [BASE, #-8]
933 | subs RA, NARGS8:RC, #8
934 | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
935 | beq ->fff_res // Done if exactly 1 argument.
937 | ldrd CARG12, [RB, #8]
939 | strd CARG12, [RB], #8
944 | ldr CARG2, [BASE, #4]
947 | checktp CARG2, LJ_TISNUM
948 | mvnlo CARG2, #~LJ_TISNUM
949 | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
950 | lsl CARG4, CARG4, #3
951 | ldrd CARG12, [CFUNC:CARG3, CARG4]
954 |//-- Base library: getters and setters ---------------------------------
956 |.ffunc_1 getmetatable
957 | checktp CARG2, LJ_TTAB
958 | cmnne CARG2, #-LJ_TUDATA
960 |1: // Field metatable must be at same offset for GCtab and GCudata!
961 | ldr TAB:RB, TAB:CARG1->metatable
963 | mvn CARG2, #~LJ_TNIL
964 | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
967 | ldr CARG3, TAB:RB->hmask
968 | ldr CARG4, STR:RC->hash
969 | ldr NODE:INS, TAB:RB->node
970 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
971 | add CARG3, CARG3, CARG3, lsl #1
972 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
973 |3: // Rearranged logic, because we expect _not_ to find the key.
974 | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS.
975 | ldrd CARG12, NODE:INS->val
976 | ldr NODE:INS, NODE:INS->next
977 | checktp CARG4, LJ_TSTR
978 | cmpeq CARG3, STR:RC
983 | mov CARG1, RB // Use metatable as default result.
984 | mvn CARG2, #~LJ_TTAB
987 | checktp CARG2, LJ_TNIL
992 | checktp CARG2, LJ_TISNUM
994 | movlo CARG2, #~LJ_TISNUM
995 | add CARG4, DISPATCH, CARG2, lsl #2
996 | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
999 |.ffunc_2 setmetatable
1000 | // Fast path: no mt for table yet and not clearing the mt.
1001 | checktp CARG2, LJ_TTAB
1002 | ldreq TAB:RB, TAB:CARG1->metatable
1003 | checktpeq CARG4, LJ_TTAB
1004 | ldrbeq CARG4, TAB:CARG1->marked
1006 | bne ->fff_fallback
1007 | tst CARG4, #LJ_GC_BLACK // isblack(table)
1008 | str TAB:CARG3, TAB:CARG1->metatable
1010 | barrierback TAB:CARG1, CARG4, CARG3
1014 | ldrd CARG34, [BASE]
1015 | cmp NARGS8:RC, #16
1016 | blo ->fff_fallback
1018 | checktab CARG4, ->fff_fallback
1020 | add CARG3, BASE, #8
1022 | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1023 | // Returns cTValue *.
1025 | ldrd CARG12, [CRET1]
1028 |//-- Base library: conversions ------------------------------------------
1031 | // Only handles the number case inline (without a base argument).
1032 | ldrd CARG12, [BASE]
1034 | bne ->fff_fallback
1035 | checktp CARG2, LJ_TISNUM
1040 | // Only handles the string or number case inline.
1041 | checktp CARG2, LJ_TSTR
1042 | // A __tostring method in the string base metatable is ignored.
1044 | // Handle numbers inline, unless a number base metatable is present.
1045 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])]
1047 | checktp CARG2, LJ_TISNUM
1049 | str PC, SAVE_PC // Redundant (but a defined value).
1050 | bhi ->fff_fallback
1054 | bl extern lj_str_fromnumber // (lua_State *L, cTValue *o)
1055 | // Returns GCstr *.
1057 | mvn CARG2, #~LJ_TSTR
1060 |//-- Base library: iterators -------------------------------------------
1063 | mvn CARG4, #~LJ_TNIL
1064 | checktab CARG2, ->fff_fallback
1065 | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil.
1066 | ldr PC, [BASE, FRAME_PC]
1068 | str BASE, L->base // Add frame since C call can throw.
1070 | str BASE, L->top // Dummy frame length is ok.
1071 | add CARG3, BASE, #8
1073 | bl extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1074 | // Returns 0 at end of traversal.
1075 | .IOS ldr BASE, L->base
1077 | mvneq CRET2, #~LJ_TNIL
1078 | beq ->fff_restv // End of traversal: return nil.
1079 | ldrd CARG12, [BASE, #8] // Copy key and value to results.
1080 | ldrd CARG34, [BASE, #16]
1082 | strd CARG12, [BASE, #-8]
1083 | strd CARG34, [BASE]
1087 | checktab CARG2, ->fff_fallback
1088 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1089 | ldr TAB:RB, TAB:CARG1->metatable
1091 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1092 | ldr PC, [BASE, FRAME_PC]
1093 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1095 | bne ->fff_fallback
1097 | mvn CARG2, #~LJ_TNIL
1099 | strd CFUNC:CARG34, [BASE, #-8]
1100 | str CARG2, [BASE, #12]
1103 |.ffunc_2 ipairs_aux
1104 | checktp CARG2, LJ_TTAB
1105 | checktpeq CARG4, LJ_TISNUM
1106 | bne ->fff_fallback
1107 | ldr RB, TAB:CARG1->asize
1108 | ldr RC, TAB:CARG1->array
1109 | add CARG3, CARG3, #1
1110 | ldr PC, [BASE, FRAME_PC]
1112 | add RC, RC, CARG3, lsl #3
1113 | strd CARG34, [BASE, #-8]
1114 | ldrdlo CARG12, [RC]
1116 | bhs >2 // Not in array part?
1118 | checktp CARG2, LJ_TNIL
1119 | movne RC, #(2+1)*8
1120 | strdne CARG12, [BASE]
1122 |2: // Check for empty hash part first. Otherwise call C function.
1123 | ldr RB, TAB:CARG1->hmask
1128 | bl extern lj_tab_getinth // (GCtab *t, int32_t key)
1129 | // Returns cTValue * or NULL.
1133 | ldrd CARG12, [CRET1]
1137 | checktab CARG2, ->fff_fallback
1138 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1139 | ldr TAB:RB, TAB:CARG1->metatable
1141 | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1142 | ldr PC, [BASE, FRAME_PC]
1143 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1145 | bne ->fff_fallback
1148 | mvn CARG2, #~LJ_TISNUM
1150 | strd CFUNC:CARG34, [BASE, #-8]
1151 | strd CARG12, [BASE, #8]
1154 |//-- Base library: catch errors ----------------------------------------
1157 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1159 | blo ->fff_fallback
1160 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1162 | add BASE, BASE, #8
1163 | moveq PC, #8+FRAME_PCALL
1164 | movne PC, #8+FRAME_PCALLH
1165 | sub NARGS8:RC, NARGS8:RC, #8
1166 | b ->vm_call_dispatch
1169 | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1170 | checkfunc CARG4, ->fff_fallback // Traceback must be a function.
1172 | strd CARG12, [BASE, #8] // Swap function and traceback.
1173 | strd CARG34, [BASE]
1174 | tst RA, #HOOK_ACTIVE // Remember active hook before pcall.
1175 | add BASE, BASE, #16
1176 | moveq PC, #16+FRAME_PCALL
1177 | movne PC, #16+FRAME_PCALLH
1178 | sub NARGS8:RC, NARGS8:RC, #16
1179 | b ->vm_call_dispatch
1181 |//-- Coroutine library --------------------------------------------------
1183 |.macro coroutine_resume_wrap, resume
1185 |.ffunc_1 coroutine_resume
1186 | checktp CARG2, LJ_TTHREAD
1187 | bne ->fff_fallback
1189 |.ffunc coroutine_wrap_aux
1190 | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1192 | ldr PC, [BASE, FRAME_PC]
1194 | ldr CARG2, L:CARG1->top
1195 | ldrb RA, L:CARG1->status
1196 | ldr RB, L:CARG1->base
1197 | add CARG3, CARG2, NARGS8:RC
1198 | add CARG4, CARG2, RA
1201 | beq ->fff_fallback
1202 | ldr CARG4, L:CARG1->maxstack
1203 | ldr RB, L:CARG1->cframe
1204 | cmp RA, #LUA_YIELD
1205 | cmpls CARG3, CARG4
1207 | bhi ->fff_fallback
1210 | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC.
1211 | add BASE, BASE, #8
1212 | sub NARGS8:RC, NARGS8:RC, #8
1214 | str CARG3, L:CARG1->top
1216 |2: // Move args to coroutine.
1217 | ldrd CARG34, [BASE, RB]
1219 | strdne CARG34, [CARG2, RB]
1226 | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1227 | // Returns thread status.
1229 | ldr CARG3, L:RA->base
1230 | mv_vmstate CARG2, INTERP
1231 | ldr CARG4, L:RA->top
1233 | cmp CRET1, #LUA_YIELD
1236 | subs RC, CARG4, CARG3
1237 | ldr CARG1, L->maxstack
1238 | add CARG2, BASE, RC
1239 | beq >6 // No results?
1242 | bhi >9 // Need to grow stack?
1245 | str CARG3, L:RA->top // Clear coroutine stack.
1246 |5: // Move results from coroutine.
1247 | ldrd CARG12, [CARG3, RB]
1249 | strd CARG12, [BASE, RB]
1254 | mvn CARG3, #~LJ_TTRUE
1257 | str CARG3, [BASE, #-4] // Prepend true/false to results.
1263 | ands CARG1, PC, #FRAME_TYPE
1265 | str RC, SAVE_MULTRES
1269 |8: // Coroutine returned with error (at co->top-1).
1271 | ldrd CARG12, [CARG4, #-8]!
1272 | mvn CARG3, #~LJ_TFALSE
1274 | str CARG4, L:RA->top // Remove error from coroutine stack.
1275 | strd CARG12, [BASE] // Copy error message.
1280 | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1284 |9: // Handle stack expansion on return from yield.
1287 | bl extern lj_state_growstack // (lua_State *L, int n)
1292 | coroutine_resume_wrap 1 // coroutine.resume
1293 | coroutine_resume_wrap 0 // coroutine.wrap
1295 |.ffunc coroutine_yield
1296 | ldr CARG1, L->cframe
1297 | add CARG2, BASE, NARGS8:RC
1299 | tst CARG1, #CFRAME_RESUME
1301 | mov CRET1, #LUA_YIELD
1303 | beq ->fff_fallback
1304 | str CARG3, L->cframe
1305 | strb CRET1, L->status
1308 |//-- Math library -------------------------------------------------------
1310 |.macro math_round, func
1311 | .ffunc_1 math_ .. func
1312 | checktp CARG2, LJ_TISNUM
1314 | bhi ->fff_fallback
1315 | // Round FP value and normalize result.
1316 | lsl CARG3, CARG2, #1
1317 | adds RB, CARG3, #0x00200000
1318 | bpl >2 // |x| < 1?
1320 | subs RB, CARG4, RB, asr #21
1321 | lsl CARG4, CARG2, #11
1322 | lsl CARG3, CARG1, #11
1323 | orr CARG4, CARG4, #0x80000000
1325 | orr CARG4, CARG4, CARG1, lsr #21
1326 | bls >3 // |x| >= 2^31?
1327 | orr CARG3, CARG3, CARG4, lsl INS
1328 | lsr CARG1, CARG4, RB
1329 |.if "func" == "floor"
1330 | tst CARG3, CARG2, asr #31
1331 | addne CARG1, CARG1, #1
1333 | bics CARG3, CARG3, CARG2, asr #31
1334 | addsne CARG1, CARG1, #1
1339 | rsblt CARG1, CARG1, #0
1341 | mvn CARG2, #~LJ_TISNUM
1345 | bcs ->fff_restv // |x| is not finite.
1346 | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo
1347 |.if "func" == "floor"
1348 | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1
1352 | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1
1356 | mvn CARG2, #~LJ_TISNUM
1359 |3: // |x| >= 2^31. Check for x == -(2^31).
1360 | cmpeq CARG4, #0x80000000
1361 |.if "func" == "floor"
1366 | movmi CARG1, #0x80000000
1370 | vmov d0, CARG1, CARG2
1371 | bl ->vm_..func.._hf
1384 | .long 0x00000000, 0x41e00000 // 2^31.
1387 | checktp CARG2, LJ_TISNUM
1388 | bhi ->fff_fallback
1389 | bicne CARG2, CARG2, #0x80000000
1392 | rsbslt CARG1, CARG1, #0
1397 | // CARG12 = TValue result.
1398 | ldr PC, [BASE, FRAME_PC]
1399 | strd CARG12, [BASE, #-8]
1404 | // RC = (nresults+1)*8, PC = return.
1405 | ands CARG1, PC, #FRAME_TYPE
1406 | ldreq INS, [PC, #-4]
1407 | str RC, SAVE_MULTRES
1410 | decode_RB8 RB, INS
1412 | cmp RB, RC // More results expected?
1414 | decode_RA8 CARG1, INS
1417 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1418 | sub BASE, RA, CARG1
1421 |6: // Fill up results with nil.
1423 | mvn CARG1, #~LJ_TNIL
1425 | str CARG1, [CARG2, #-4]
1428 |.macro math_extern, func
1430 | .ffunc_d math_ .. func
1432 | .ffunc_n math_ .. func
1444 |.macro math_extern2, func
1446 | .ffunc_dd math_ .. func
1448 | .ffunc_nn math_ .. func
1461 | .ffunc_d math_sqrt
1464 | ldr PC, [BASE, FRAME_PC]
1465 | vstr d0, [BASE, #-8]
1484 | math_extern2 atan2
1490 | vldr d1, CFUNC:CARG3->upvalue[0]
1491 | vmul.f64 d0, d0, d1
1495 | ldrd CARG34, CFUNC:CARG3->upvalue[0]
1496 | bl extern __aeabi_dmul
1502 | ldr CARG4, [BASE, #4]
1503 | ldrd CARG12, [BASE, #8]
1504 | cmp NARGS8:RC, #16
1505 | blo ->fff_fallback
1507 | checktp CARG4, LJ_TISNUM
1508 | bhs ->fff_fallback
1509 | checktp CARG2, LJ_TISNUM
1510 | bne ->fff_fallback
1512 | bl extern ldexp // (double x, int exp)
1516 |.ffunc_2 math_ldexp
1517 | checktp CARG2, LJ_TISNUM
1518 | bhs ->fff_fallback
1519 | checktp CARG4, LJ_TISNUM
1520 | bne ->fff_fallback
1522 | bl extern ldexp // (double x, int exp)
1528 |.ffunc_d math_frexp
1534 | mvn CARG4, #~LJ_TISNUM
1535 | ldr PC, [BASE, FRAME_PC]
1536 | vstr d0, [BASE, #-8]
1538 | strd CARG34, [BASE]
1541 |.ffunc_n math_frexp
1547 | mvn CARG4, #~LJ_TISNUM
1548 | ldr PC, [BASE, FRAME_PC]
1549 | strd CARG12, [BASE, #-8]
1551 | strd CARG34, [BASE]
1557 | sub CARG1, BASE, #8
1558 | ldr PC, [BASE, FRAME_PC]
1567 | sub CARG3, BASE, #8
1568 | ldr PC, [BASE, FRAME_PC]
1573 | strd CARG12, [BASE]
1577 |.macro math_minmax, name, cond, fcond
1581 | checktp CARG2, LJ_TISNUM
1584 |1: // Handle integers.
1588 | checktp CARG4, LJ_TISNUM
1592 | mov..cond CARG1, CARG3
1594 |3: // Convert intermediate result to number and continue below.
1596 | bhi ->fff_fallback
1598 | vcvt.f64.s32 d0, s4
1603 | bhi ->fff_fallback
1604 |5: // Handle numbers.
1609 | checktp CARG4, LJ_TISNUM
1615 | vmov..fcond.f64 d0, d1
1617 |7: // Convert integer to number and continue above.
1619 | bhi ->fff_fallback
1620 | vcvt.f64.s32 d1, s4
1626 | checktp CARG2, LJ_TISNUM
1629 |1: // Handle integers.
1630 | ldrd CARG34, [BASE, RA]
1633 | checktp CARG4, LJ_TISNUM
1637 | mov..cond CARG1, CARG3
1639 |3: // Convert intermediate result to number and continue below.
1640 | bhi ->fff_fallback
1641 | bl extern __aeabi_i2d
1642 | ldrd CARG34, [BASE, RA]
1646 | bhi ->fff_fallback
1647 |5: // Handle numbers.
1648 | ldrd CARG34, [BASE, RA]
1651 | checktp CARG4, LJ_TISNUM
1654 | bl extern __aeabi_cdcmple
1656 | mov..fcond CARG1, CARG3
1657 | mov..fcond CARG2, CARG4
1659 |7: // Convert integer to number and continue above.
1660 | bhi ->fff_fallback
1663 | bl extern __aeabi_i2d
1669 | math_minmax math_min, gt, hi
1670 | math_minmax math_max, lt, lo
1672 |//-- String library -----------------------------------------------------
1674 |.ffunc_1 string_len
1675 | checkstr CARG2, ->fff_fallback
1676 | ldr CARG1, STR:CARG1->len
1677 | mvn CARG2, #~LJ_TISNUM
1680 |.ffunc string_byte // Only handle the 1-arg case here.
1681 | ldrd CARG12, [BASE]
1682 | ldr PC, [BASE, FRAME_PC]
1684 | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument.
1685 | bne ->fff_fallback
1686 | ldr CARG3, STR:CARG1->len
1687 | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end).
1688 | mvn CARG2, #~LJ_TISNUM
1690 | moveq RC, #(0+1)*8
1691 | movne RC, #(1+1)*8
1692 | strd CARG12, [BASE, #-8]
1695 |.ffunc string_char // Only handle the 1-arg case here.
1697 | ldrd CARG12, [BASE]
1698 | ldr PC, [BASE, FRAME_PC]
1699 | cmp NARGS8:RC, #8 // Need exactly 1 argument.
1700 | checktpeq CARG2, LJ_TISNUM
1701 | bicseq CARG4, CARG1, #255
1703 | bne ->fff_fallback
1705 | mov CARG2, TMPDp // Points to stack. Little-endian.
1707 | // CARG2 = str, CARG3 = len.
1711 | bl extern lj_str_new // (lua_State *L, char *str, size_t l)
1712 | // Returns GCstr *.
1714 | mvn CARG2, #~LJ_TSTR
1719 | ldrd CARG12, [BASE]
1720 | ldrd CARG34, [BASE, #16]
1721 | cmp NARGS8:RC, #16
1724 | blo ->fff_fallback
1725 | checktp CARG4, LJ_TISNUM
1727 | bne ->fff_fallback
1729 | ldrd CARG34, [BASE, #8]
1730 | checktp CARG2, LJ_TSTR
1731 | ldreq CARG2, STR:CARG1->len
1732 | checktpeq CARG4, LJ_TISNUM
1733 | bne ->fff_fallback
1734 | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1735 | add CARG4, CARG2, #1
1736 | cmp CARG3, #0 // if (start < 0) start += len+1
1737 | addlt CARG3, CARG3, CARG4
1738 | cmp CARG3, #1 // if (start < 1) start = 1
1740 | cmp RB, #0 // if (end < 0) end += len+1
1741 | addlt RB, RB, CARG4
1742 | bic RB, RB, RB, asr #31 // if (end < 0) end = 0
1743 | cmp RB, CARG2 // if (end > len) end = len
1744 | add CARG1, STR:CARG1, #sizeof(GCstr)-1
1746 | add CARG2, CARG1, CARG3
1747 | subs CARG3, RB, CARG3 // len = end - start
1748 | add CARG3, CARG3, #1 // len += 1
1751 | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1752 | mvn CARG2, #~LJ_TSTR
1755 |.ffunc string_rep // Only handle the 1-char case inline.
1757 | ldrd CARG12, [BASE]
1758 | ldrd CARG34, [BASE, #8]
1759 | cmp NARGS8:RC, #16
1760 | blo ->fff_fallback
1761 | checktp CARG2, LJ_TSTR
1762 | checktpeq CARG4, LJ_TISNUM
1763 | bne ->fff_fallback
1764 | subs CARG4, CARG3, #1
1765 | ldr CARG2, STR:CARG1->len
1766 | blt ->fff_emptystr // Count <= 0?
1768 | blo ->fff_emptystr // Zero-length string?
1769 | bne ->fff_fallback // Fallback for > 1-char strings.
1770 | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)]
1771 | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)]
1772 | ldr CARG1, STR:CARG1[1]
1774 | blo ->fff_fallback
1775 |1: // Fill buffer with char.
1776 | strb CARG1, [CARG2, CARG4]
1777 | subs CARG4, CARG4, #1
1781 |.ffunc string_reverse
1783 | ldrd CARG12, [BASE]
1785 | blo ->fff_fallback
1786 | checkstr CARG2, ->fff_fallback
1787 | ldr CARG3, STR:CARG1->len
1788 | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)]
1789 | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)]
1791 | add CARG1, STR:CARG1, #sizeof(GCstr)
1793 | blo ->fff_fallback
1794 |1: // Reverse string copy.
1795 | ldrb RB, [CARG1], #1
1796 | subs CARG4, CARG4, #1
1798 | strb RB, [CARG2, CARG4]
1801 |.macro ffstring_case, name, lo
1804 | ldrd CARG12, [BASE]
1806 | blo ->fff_fallback
1807 | checkstr CARG2, ->fff_fallback
1808 | ldr CARG3, STR:CARG1->len
1809 | ldr RB, [DISPATCH, #DISPATCH_GL(tmpbuf.sz)]
1810 | ldr CARG2, [DISPATCH, #DISPATCH_GL(tmpbuf.buf)]
1812 | add CARG1, STR:CARG1, #sizeof(GCstr)
1814 | blo ->fff_fallback
1815 |1: // ASCII case conversion.
1816 | ldrb RB, [CARG1, CARG4]
1821 | eorlo RB, RB, #0x20
1822 | strb RB, [CARG2, CARG4]
1823 | add CARG4, CARG4, #1
1827 |ffstring_case string_lower, 65
1828 |ffstring_case string_upper, 97
1830 |//-- Table library ------------------------------------------------------
1832 |.ffunc_1 table_getn
1833 | checktab CARG2, ->fff_fallback
1835 | bl extern lj_tab_len // (GCtab *t)
1836 | // Returns uint32_t (but less than 2^31).
1838 | mvn CARG2, #~LJ_TISNUM
1841 |//-- Bit library --------------------------------------------------------
1843 |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1845 | bhi ->fff_fallback
1848 | adds RB, RB, #0x00200000
1849 | movpl CARG1, #0 // |x| < 1?
1852 | subs RB, CARG4, RB, asr #21
1853 | bmi >1 // |x| >= 2^32?
1854 | lsl CARG4, CARG2, #11
1855 | orr CARG4, CARG4, #0x80000000
1856 | orr CARG4, CARG4, CARG1, lsr #21
1858 | lsr CARG1, CARG4, RB
1859 | rsblt CARG1, CARG1, #0
1863 | lsr CARG4, CARG1, RB
1865 | lsl CARG1, CARG2, #12
1867 | orr CARG1, CARG4, CARG1, lsl RB
1868 | rsblt CARG1, CARG1, #0
1871 |.macro .ffunc_bit, name
1872 | .ffunc_1 bit_..name
1873 | checktp CARG2, LJ_TISNUM
1874 | blne ->vm_tobit_fb
1878 | mvn CARG2, #~LJ_TISNUM
1881 |.macro .ffunc_bit_op, name, ins
1886 | ldrd CARG12, [BASE, RA]
1890 | checktp CARG2, LJ_TISNUM
1891 | blne ->vm_tobit_fb
1892 | ins CARG3, CARG3, CARG1
1896 |.ffunc_bit_op band, and
1897 |.ffunc_bit_op bor, orr
1898 |.ffunc_bit_op bxor, eor
1901 | mvn CARG4, #~LJ_TISNUM
1902 | ldr PC, [BASE, FRAME_PC]
1903 | strd CARG34, [BASE, #-8]
1907 | eor CARG3, CARG1, CARG1, ror #16
1908 | bic CARG3, CARG3, #0x00ff0000
1909 | ror CARG1, CARG1, #8
1910 | mvn CARG2, #~LJ_TISNUM
1911 | eor CARG1, CARG1, CARG3, lsr #8
1916 | mvn CARG2, #~LJ_TISNUM
1919 |.macro .ffunc_bit_sh, name, ins, shmod
1921 | ldrd CARG12, [BASE, #8]
1922 | cmp NARGS8:RC, #16
1923 | blo ->fff_fallback
1924 | checktp CARG2, LJ_TISNUM
1925 | blne ->vm_tobit_fb
1927 | and RA, CARG1, #31
1931 | ldrd CARG12, [BASE]
1932 | checktp CARG2, LJ_TISNUM
1933 | blne ->vm_tobit_fb
1934 | ins CARG1, CARG1, RA
1935 | mvn CARG2, #~LJ_TISNUM
1939 |.ffunc_bit_sh lshift, lsl, 0
1940 |.ffunc_bit_sh rshift, lsr, 0
1941 |.ffunc_bit_sh arshift, asr, 0
1942 |.ffunc_bit_sh rol, ror, 1
1943 |.ffunc_bit_sh ror, ror, 0
1945 |//-----------------------------------------------------------------------
1947 |->fff_fallback: // Call fast function fallback handler.
1948 | // BASE = new base, RC = nargs*8
1949 | ldr CARG3, [BASE, FRAME_FUNC]
1950 | ldr CARG2, L->maxstack
1951 | add CARG1, BASE, NARGS8:RC
1952 | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC.
1954 | ldr CARG3, CFUNC:CARG3->f
1956 | add CARG1, CARG1, #8*LUA_MINSTACK
1957 | str PC, SAVE_PC // Redundant (but a defined value).
1960 | bhi >5 // Need to grow stack.
1961 | blx CARG3 // (lua_State *L)
1962 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1967 | bgt ->fff_res // Returned nresults+1?
1968 |1: // Returned 0 or -1: retry fast path.
1970 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1971 | sub NARGS8:RC, CARG1, BASE
1972 | bne ->vm_call_tail // Returned -1?
1973 | ins_callt // Returned 0: retry fast path.
1975 |// Reconstruct previous base for vmeta_call during tailcall.
1977 | ands CARG1, PC, #FRAME_TYPE
1978 | bic CARG2, PC, #FRAME_TYPEP
1979 | ldreq INS, [PC, #-4]
1980 | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8.
1981 | addeq CARG2, CARG2, #8
1982 | sub RB, BASE, CARG2
1983 | b ->vm_call_dispatch // Resolve again for tailcall.
1985 |5: // Grow stack for fallback handler.
1986 | mov CARG2, #LUA_MINSTACK
1987 | bl extern lj_state_growstack // (lua_State *L, int n)
1989 | cmp CARG1, CARG1 // Set zero-flag to force retry.
1992 |->fff_gcstep: // Call GC step function.
1993 | // BASE = new base, RC = nargs*8
1996 | add CARG2, BASE, NARGS8:RC
1997 | str PC, SAVE_PC // Redundant (but a defined value).
2000 | bl extern lj_gc_step // (lua_State *L)
2002 | mov lr, RA // Help return address predictor.
2003 | ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
2006 |//-----------------------------------------------------------------------
2007 |//-- Special dispatch targets -------------------------------------------
2008 |//-----------------------------------------------------------------------
2010 |->vm_record: // Dispatch target for recording phase.
2012 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2013 | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent.
2015 | // Decrement the hookcount for consistency, but always do the call.
2016 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2017 | tst CARG1, #HOOK_ACTIVE
2019 | sub CARG2, CARG2, #1
2020 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2021 | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2025 |->vm_rethook: // Dispatch target for return hooks.
2026 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2027 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2029 |5: // Re-dispatch to static ins.
2031 | add OP, DISPATCH, OP, lsl #2
2032 | ldr pc, [OP, #GG_DISP2STATIC]
2034 |->vm_inshook: // Dispatch target for instr/line hooks.
2035 | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2036 | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2037 | tst CARG1, #HOOK_ACTIVE // Hook already active?
2039 | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2041 | subs CARG2, CARG2, #1
2042 | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2044 | tst CARG1, #LUA_MASKLINE
2050 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2051 | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2054 |4: // Re-dispatch to static ins.
2055 | ldrb OP, [PC, #-4]
2056 | ldr INS, [PC, #-4]
2057 | add OP, DISPATCH, OP, lsl #2
2058 | ldr OP, [OP, #GG_DISP2STATIC]
2059 | decode_RA8 RA, INS
2063 |->cont_hook: // Continue from hook yield.
2064 | ldr CARG1, [CARG4, #-24]
2066 | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins.
2069 |->vm_hotloop: // Hot loop counter underflow.
2071 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L).
2072 | sub CARG1, DISPATCH, #-GG_DISP2J
2074 | ldr CARG3, LFUNC:CARG3->field_pc
2076 | str L, [DISPATCH, #DISPATCH_J(L)]
2077 | ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2079 | add CARG3, BASE, CARG3, lsl #3
2081 | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2085 |->vm_callhook: // Dispatch target for call hooks.
2091 |->vm_hotcall: // Hot call counter underflow.
2096 | add CARG4, BASE, RC
2102 | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2103 | // Returns ASMFunction.
2108 | sub NARGS8:RC, CARG4, BASE
2109 | str CARG2, SAVE_PC // Invalidate for subsequent line hook.
2110 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2111 | ldr INS, [PC, #-4]
2114 |//-----------------------------------------------------------------------
2115 |//-- Trace exit handler -------------------------------------------------
2116 |//-----------------------------------------------------------------------
2121 | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2122 | ldr CARG1, [sp, #64] // Load original value of lr.
2123 | ldr DISPATCH, [lr] // Load DISPATCH.
2124 | add CARG3, sp, #64 // Recompute original value of sp.
2125 | mv_vmstate CARG4, EXIT
2126 | str CARG3, [sp, #52] // Store sp in RID_SP
2128 | ldr CARG2, [CARG1, #-4]! // Get exit instruction.
2129 | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC.
2130 | str CARG1, [sp, #60]
2134 | lsl CARG2, CARG2, #8
2135 | add CARG1, CARG1, CARG2, asr #6
2136 | ldr CARG2, [lr, #4] // Load exit stub group offset.
2137 | sub CARG1, CARG1, lr
2138 | ldr L, [DISPATCH, #DISPATCH_GL(jit_L)]
2139 | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number.
2140 | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2141 | str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2143 | str L, [DISPATCH, #DISPATCH_J(L)]
2145 | str CARG4, [DISPATCH, #DISPATCH_GL(jit_L)]
2146 | sub CARG1, DISPATCH, #-GG_DISP2J
2148 | bl extern lj_trace_exit // (jit_State *J, ExitState *ex)
2149 | // Returns MULTRES (unscaled) or negated error code.
2150 | ldr CARG2, L->cframe
2152 | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2154 | ldr PC, SAVE_PC // Get SAVE_PC.
2155 | str L, SAVE_L // Set SAVE_L (on-trace resume/yield).
2159 | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2164 | blt >3 // Check for error from exit.
2166 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2167 | str RC, SAVE_MULTRES
2169 | ldr CARG2, LFUNC:CARG2->field_pc
2170 | str CARG3, [DISPATCH, #DISPATCH_GL(jit_L)]
2171 | mv_vmstate CARG4, INTERP
2172 | ldr KBASE, [CARG2, #PC2PROTO(k)]
2173 | // Modified copy of ins_next which handles function header dispatch, too.
2177 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2179 | cmp OP, #BC_FUNCF // Function header?
2180 | ldr OP, [DISPATCH, OP, lsl #2]
2181 | decode_RA8 RA, INS
2182 | lsrlo RC, INS, #16 // No: Decode operands A*8 and D.
2184 | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8
2187 |3: // Rethrow error from the right C frame.
2188 | rsb CARG2, CARG1, #0
2190 | bl extern lj_err_throw // (lua_State *L, int errcode)
2193 |//-----------------------------------------------------------------------
2194 |//-- Math helper functions ----------------------------------------------
2195 |//-----------------------------------------------------------------------
2197 |// FP value rounding. Called from JIT code.
2199 |// double lj_vm_floor/ceil/trunc(double x);
2200 |.macro vm_round, func, hf
2203 | vmov d0, CARG1, CARG2
2204 | vldr d2, <8 // 2^52
2206 | vldr d2, <8 // 2^52
2207 | vmov CARG1, CARG2, d0
2210 | vcmp.f64 d1, d2 // |x| >= 2^52 or NaN?
2212 |.if "func" == "trunc"
2213 | bxpl lr // Return argument unchanged.
2214 | vadd.f64 d0, d1, d2
2215 | vsub.f64 d0, d0, d2 // (|x| + 2^52) - 2^52
2216 | vldr d2, <9 // +1.0
2217 | vcmp.f64 d1, d0 // |x| < result: subtract +1.0
2219 | vsubmi.f64 d0, d0, d2
2221 | vnegmi.f64 d0, d0 // Merge sign bit back in.
2223 | vadd.f64 d1, d1, d2
2224 | bxpl lr // Return argument unchanged.
2226 | vsub.f64 d1, d1, d2 // (|x| + 2^52) - 2^52
2227 | vldr d2, <9 // +1.0
2228 | vnegmi.f64 d1, d1 // Merge sign bit back in.
2229 |.if "func" == "floor"
2230 | vcmp.f64 d0, d1 // x < result: subtract +1.0.
2232 | vsubmi.f64 d0, d1, d2
2234 | vcmp.f64 d1, d0 // x > result: add +1.0.
2236 | vaddmi.f64 d0, d1, d2
2241 | vmov CARG1, CARG2, d0
2247 | lsl CARG3, CARG2, #1
2248 | adds RB, CARG3, #0x00200000
2249 | bpl >2 // |x| < 1?
2251 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2252 | bxlo lr // |x| >= 2^52: done.
2254 | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask
2255 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2257 | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2258 | orrpl CARG3, CARG3, CARG4
2260 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2261 |.if "func" == "floor"
2262 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2264 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2266 | bxeq lr // iszero: done.
2269 | lslpl CARG3, CARG4, RB
2272 | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask
2273 | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry
2277 | bxcs lr // |x| is not finite.
2278 | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo
2279 |.if "func" == "floor"
2280 | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0)
2282 | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2284 | mov CARG1, #0 // lo = 0
2285 | and CARG2, CARG2, #0x80000000
2286 | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0)
2287 | orrne CARG2, CARG2, CARG4
2295 | .long 0, 0x3ff00000 // +1.0
2297 | .long 0, 0x43300000 // 2^52
2300 | .long 0x3ff00000 // hiword(+1.0)
2322 |.if JIT and not HFABI
2323 | lsl CARG3, CARG2, #1
2324 | adds RB, CARG3, #0x00200000
2325 | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0.
2329 | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0.
2330 | bxlo lr // |x| >= 2^52: already done.
2332 | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask
2334 | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask
2343 | // double lj_vm_mod(double dividend, double divisor);
2346 | // Special calling convention. Also, RC (r11) is not preserved.
2347 | vdiv.f64 d0, d6, d7
2350 | vmul.f64 d0, d0, d7
2352 | vsub.f64 d6, d6, d0
2355 | push {r0, r1, r2, r3, r4, lr}
2356 | bl extern __aeabi_ddiv
2358 | ldrd CARG34, [sp, #8]
2359 | bl extern __aeabi_dmul
2361 | eor CARG2, CARG2, #0x80000000
2362 | bl extern __aeabi_dadd
2367 | // int lj_vm_modi(int dividend, int divisor);
2369 | ands RB, CARG1, #0x80000000
2370 | rsbmi CARG1, CARG1, #0 // a = |dividend|
2371 | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor).
2373 | rsbmi CARG2, CARG2, #0 // b = |divisor|
2374 | subs CARG4, CARG2, #1
2375 | cmpne CARG1, CARG2
2376 | moveq CARG1, #0 // if (b == 1 || a == b) a = 0
2377 | tsthi CARG2, CARG4
2378 | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1
2380 | // Use repeated subtraction to get the remainder.
2383 | sub CARG4, CARG4, CARG3
2384 | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8
2385 | addne pc, pc, CARG3, lsl #3 // Duff's device.
2389 for (i = 31; i >= 0; i--) {
2390 | cmp CARG1, CARG2, lsl #i
2391 | subhs CARG1, CARG1, CARG2, lsl #i
2397 | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b
2398 | eors CARG2, CARG1, RB, lsl #1
2399 | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y
2402 |//-----------------------------------------------------------------------
2403 |//-- Miscellaneous functions --------------------------------------------
2404 |//-----------------------------------------------------------------------
2406 |//-----------------------------------------------------------------------
2407 |//-- FFI helper functions -----------------------------------------------
2408 |//-----------------------------------------------------------------------
2410 |// Handler for callback functions.
2411 |// Saveregs already performed. Callback slot number in [sp], g in r12.
2414 |.type CTSTATE, CTState, PC
2415 | ldr CTSTATE, GL:r12->ctype_state
2416 | add DISPATCH, r12, #GG_G2DISP
2419 | add r4, sp, CFRAME_SPACE+4+8*8
2420 | vstmdb r4!, {d8-d15}
2423 | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2425 | strd CARG34, CTSTATE->cb.gpr[2]
2426 | strd CARG12, CTSTATE->cb.gpr[0]
2428 | vstmdb r12!, {d0-d7}
2431 | add CARG3, sp, #CFRAME_SIZE
2432 | mov CARG1, CTSTATE
2433 | lsr CARG4, CARG4, #3
2434 | str CARG3, CTSTATE->cb.stack
2436 | str CARG4, CTSTATE->cb.slot
2437 | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok.
2438 | bl extern lj_ccallback_enter // (CTState *cts, void *cf)
2439 | // Returns lua_State *.
2440 | ldr BASE, L:CRET1->base
2441 | mv_vmstate CARG2, INTERP
2442 | ldr RC, L:CRET1->top
2444 | ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2447 | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
2452 |->cont_ffi_callback: // Return from FFI callback.
2454 | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2458 | mov CARG1, CTSTATE
2460 | bl extern lj_ccallback_leave // (CTState *cts, TValue *o)
2461 | ldrd CARG12, CTSTATE->cb.gpr[0]
2463 | vldr d0, CTSTATE->cb.fpr[0]
2468 |->vm_ffi_call: // Call C function via FFI.
2469 | // Caveat: needs special frame unwinding, see below.
2471 | .type CCSTATE, CCallState, r4
2472 | push {CCSTATE, r5, r11, lr}
2473 | mov CCSTATE, CARG1
2474 | ldr CARG1, CCSTATE:CARG1->spadj
2475 | ldrb CARG2, CCSTATE->nsp
2476 | add CARG3, CCSTATE, #offsetof(CCallState, stack)
2478 | add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2481 | sub sp, sp, CARG1 // Readjust stack.
2482 | subs CARG2, CARG2, #1
2486 | ldr RB, CCSTATE->func
2488 |1: // Copy stack slots.
2489 | ldr CARG4, [CARG3, CARG2, lsl #2]
2490 | str CARG4, [sp, CARG2, lsl #2]
2491 | subs CARG2, CARG2, #1
2494 | ldrd CARG12, CCSTATE->gpr[0]
2495 | ldrd CARG34, CCSTATE->gpr[2]
2499 | add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2501 | strd CRET1, CCSTATE->gpr[0]
2503 | vstmdb r12!, {d0-d3}
2505 | pop {CCSTATE, r5, r11, pc}
2507 |// Note: vm_ffi_call must be the last function in this object file!
2509 |//-----------------------------------------------------------------------
2512 /* Generate the code for a single instruction. */
2513 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2520 /* -- Comparison ops ---------------------------------------------------- */
2522 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2524 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2525 | // RA = src1*8, RC = src2, JMP with RC = target
2527 | ldrd CARG12, [RA, BASE]!
2529 | ldrd CARG34, [RC, BASE]!
2531 | add RB, PC, RB, lsl #2
2532 | checktp CARG2, LJ_TISNUM
2534 | checktp CARG4, LJ_TISNUM
2537 if (op == BC_ISLT) {
2538 | sublt PC, RB, #0x20000
2539 } else if (op == BC_ISGE) {
2540 | subge PC, RB, #0x20000
2541 } else if (op == BC_ISLE) {
2542 | suble PC, RB, #0x20000
2544 | subgt PC, RB, #0x20000
2549 |3: // CARG12 is not an integer.
2553 | // d0 is a number.
2554 | checktp CARG4, LJ_TISNUM
2557 | // d0 is a number, CARG3 is an integer.
2559 | vcvt.f64.s32 d1, s4
2561 |4: // CARG1 is an integer, CARG34 is not an integer.
2564 | // CARG1 is an integer, d1 is a number.
2566 | vcvt.f64.s32 d0, s4
2567 |5: // d0 and d1 are numbers.
2570 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2571 if (op == BC_ISLT) {
2572 | sublo PC, RB, #0x20000
2573 } else if (op == BC_ISGE) {
2574 | subhs PC, RB, #0x20000
2575 } else if (op == BC_ISLE) {
2576 | subls PC, RB, #0x20000
2578 | subhi PC, RB, #0x20000
2583 | // CARG12 is a number.
2584 | checktp CARG4, LJ_TISNUM
2585 | movlo RA, RB // Save RB.
2587 | // CARG12 is a number, CARG3 is an integer.
2590 | mov RA, RB // Save RB.
2591 | bl extern __aeabi_i2d
2594 | ldrd CARG12, [RC] // Restore first operand.
2596 |4: // CARG1 is an integer, CARG34 is not an integer.
2598 | // CARG1 is an integer, CARG34 is a number.
2599 | mov RA, RB // Save RB.
2600 | bl extern __aeabi_i2d
2601 | ldrd CARG34, [RC] // Restore second operand.
2602 |5: // CARG12 and CARG34 are numbers.
2603 | bl extern __aeabi_cdcmple
2604 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2605 if (op == BC_ISLT) {
2606 | sublo PC, RA, #0x20000
2607 } else if (op == BC_ISGE) {
2608 | subhs PC, RA, #0x20000
2609 } else if (op == BC_ISLE) {
2610 | subls PC, RA, #0x20000
2612 | subhi PC, RA, #0x20000
2618 case BC_ISEQV: case BC_ISNEV:
2619 vk = op == BC_ISEQV;
2620 | // RA = src1*8, RC = src2, JMP with RC = target
2622 | ldrd CARG12, [RA, BASE]!
2624 | ldrd CARG34, [RC, BASE]!
2626 | add RB, PC, RB, lsl #2
2627 | checktp CARG2, LJ_TISNUM
2628 | cmnls CARG4, #-LJ_TISNUM
2634 | // Either or both types are not numbers.
2636 | checktp CARG2, LJ_TCDATA
2637 | checktpne CARG4, LJ_TCDATA
2638 | beq ->vmeta_equal_cd
2640 | cmp CARG2, CARG4 // Compare types.
2641 | bne >2 // Not the same type?
2642 | checktp CARG2, LJ_TISPRI
2643 | bhs >1 // Same type and primitive type?
2645 | // Same types and not a primitive type. Compare GCobj or pvalue.
2648 | bne >3 // Different GCobjs or pvalues?
2649 |1: // Branch if same.
2650 | sub PC, RB, #0x20000
2654 | checktp CARG2, LJ_TISTABUD
2655 | bhi <2 // Different objects and not table/ud?
2657 | beq >1 // Same GCobjs or pvalues?
2658 | checktp CARG2, LJ_TISTABUD
2659 | bhi >2 // Different objects and not table/ud?
2661 | // Different tables or userdatas. Need to check __eq metamethod.
2662 | // Field metatable must be at same offset for GCtab and GCudata!
2663 | ldr TAB:RA, TAB:CARG1->metatable
2666 | beq <2 // No metatable?
2668 | beq >2 // No metatable?
2670 | ldrb RA, TAB:RA->nomm
2671 | mov CARG4, #1-vk // ne = 0 or 1.
2674 | beq ->vmeta_equal // 'no __eq' flag not set?
2678 |2: // Branch if different.
2679 | sub PC, RB, #0x20000
2685 case BC_ISEQS: case BC_ISNES:
2686 vk = op == BC_ISEQS;
2687 | // RA = src*8, RC = str_const (~), JMP with RC = target
2689 | ldrd CARG12, [BASE, RA]
2691 | ldr STR:CARG3, [KBASE, RC, lsl #2]
2693 | add RB, PC, RB, lsl #2
2694 | checktp CARG2, LJ_TSTR
2699 | cmpeq CARG1, CARG3
2702 | subeq PC, RB, #0x20000
2706 | subne PC, RB, #0x20000
2712 | checktp CARG2, LJ_TCDATA
2714 | b ->vmeta_equal_cd
2718 case BC_ISEQN: case BC_ISNEN:
2719 vk = op == BC_ISEQN;
2720 | // RA = src*8, RC = num_const (~), JMP with RC = target
2722 | ldrd CARG12, [RA, BASE]!
2724 | ldrd CARG34, [RC, KBASE]!
2726 | add RB, PC, RB, lsl #2
2732 | checktp CARG2, LJ_TISNUM
2734 | checktp CARG4, LJ_TISNUM
2738 | subeq PC, RB, #0x20000
2742 | subne PC, RB, #0x20000
2747 |3: // CARG12 is not an integer.
2752 | subhi PC, RB, #0x20000
2757 | checktp CARG4, LJ_TISNUM
2761 | vcvths.f64.s32 d1, s4
2763 |4: // CARG1 is an integer, d1 is a number.
2766 | vcvt.f64.s32 d0, s4
2767 |5: // d0 and d1 are numbers.
2771 | subeq PC, RB, #0x20000
2773 | subne PC, RB, #0x20000
2777 | // CARG12 is a number.
2778 | checktp CARG4, LJ_TISNUM
2779 | movlo RA, RB // Save RB.
2781 | // CARG12 is a number, CARG3 is an integer.
2784 |4: // CARG1 is an integer, CARG34 is a number.
2785 | mov RA, RB // Save RB.
2786 | bl extern __aeabi_i2d
2787 | ldrd CARG34, [RC] // Restore other operand.
2788 |5: // CARG12 and CARG34 are numbers.
2789 | bl extern __aeabi_cdcmpeq
2791 | subeq PC, RA, #0x20000
2793 | subne PC, RA, #0x20000
2800 | checktp CARG2, LJ_TCDATA
2802 | b ->vmeta_equal_cd
2806 case BC_ISEQP: case BC_ISNEP:
2807 vk = op == BC_ISEQP;
2808 | // RA = src*8, RC = primitive_type (~), JMP with RC = target
2809 | ldrd CARG12, [BASE, RA]
2813 | add RB, PC, RB, lsl #2
2815 | checktp CARG2, LJ_TCDATA
2816 | beq ->vmeta_equal_cd
2820 | subeq PC, RB, #0x20000
2822 | subne PC, RB, #0x20000
2827 /* -- Unary test and copy ops ------------------------------------------- */
2829 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2830 | // RA = dst*8 or unused, RC = src, JMP with RC = target
2831 | add RC, BASE, RC, lsl #3
2835 | add RB, PC, RB, lsl #2
2836 | checktp CARG2, LJ_TTRUE
2837 if (op == BC_ISTC || op == BC_IST) {
2838 | subls PC, RB, #0x20000
2839 if (op == BC_ISTC) {
2840 | strdls CARG12, [BASE, RA]
2843 | subhi PC, RB, #0x20000
2844 if (op == BC_ISFC) {
2845 | strdhi CARG12, [BASE, RA]
2851 /* -- Unary ops --------------------------------------------------------- */
2854 | // RA = dst*8, RC = src
2857 | ldrd CARG12, [BASE, RC]
2859 | strd CARG12, [BASE, RA]
2863 | // RA = dst*8, RC = src
2864 | add RC, BASE, RC, lsl #3
2866 | ldr CARG1, [RC, #4]
2869 | checktp CARG1, LJ_TTRUE
2870 | mvnls CARG2, #~LJ_TFALSE
2871 | mvnhi CARG2, #~LJ_TTRUE
2872 | str CARG2, [RA, #4]
2876 | // RA = dst*8, RC = src
2878 | ldrd CARG12, [BASE, RC]
2881 | checktp CARG2, LJ_TISNUM
2883 | eorne CARG2, CARG2, #0x80000000
2885 | rsbseq CARG1, CARG1, #0
2888 | strd CARG12, [BASE, RA]
2893 | .long 0x00000000, 0x41e00000 // 2^31.
2896 | // RA = dst*8, RC = src
2898 | ldrd CARG12, [BASE, RC]
2899 | checkstr CARG2, >2
2900 | ldr CARG1, STR:CARG1->len
2902 | mvn CARG2, #~LJ_TISNUM
2905 | strd CARG12, [BASE, RA]
2908 | checktab CARG2, ->vmeta_len
2909 #ifdef LUAJIT_ENABLE_LUA52COMPAT
2910 | ldr TAB:CARG3, TAB:CARG1->metatable
2917 | bl extern lj_tab_len // (GCtab *t)
2918 | // Returns uint32_t (but less than 2^31).
2921 #ifdef LUAJIT_ENABLE_LUA52COMPAT
2923 | ldrb CARG4, TAB:CARG3->nomm
2924 | tst CARG4, #1<<MM_len
2925 | bne <3 // 'no __len' flag set: done.
2930 /* -- Binary ops -------------------------------------------------------- */
2932 |.macro ins_arithcheck, cond, ncond, target
2934 | cmn CARG4, #-LJ_TISNUM
2935 | cmn..cond CARG2, #-LJ_TISNUM
2937 | cmn CARG2, #-LJ_TISNUM
2938 | cmn..cond CARG4, #-LJ_TISNUM
2942 |.macro ins_arithcheck_int, target
2943 | ins_arithcheck eq, ne, target
2945 |.macro ins_arithcheck_num, target
2946 | ins_arithcheck lo, hs, target
2949 |.macro ins_arithpre
2950 | decode_RB8 RB, INS
2951 | decode_RC8 RC, INS
2952 | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
2953 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
2957 | ldrd CARG12, [RB, BASE]!
2958 | ldrd CARG34, [RC, KBASE]!
2960 | ldrd CARG12, [BASE, RB]
2961 | ldrd CARG34, [KBASE, RC]
2966 | ldrd CARG34, [RB, BASE]!
2967 | ldrd CARG12, [RC, KBASE]!
2969 | ldrd CARG34, [BASE, RB]
2970 | ldrd CARG12, [KBASE, RC]
2975 | ldrd CARG12, [RB, BASE]!
2976 | ldrd CARG34, [RC, BASE]!
2978 | ldrd CARG12, [BASE, RB]
2979 | ldrd CARG34, [BASE, RC]
2985 |.macro ins_arithpre_fpu, reg1, reg2
2997 |.macro ins_arithpost_fpu, reg
3005 |.macro ins_arithfallback, ins
3008 | ins ->vmeta_arith_vn
3011 | ins ->vmeta_arith_nv
3014 | ins ->vmeta_arith_vv
3019 |.macro ins_arithdn, intins, fpins, fpcall
3021 |.if "intins" ~= "vm_modi" and not FPU
3024 | ins_arithcheck_int >5
3025 |.if "intins" == "smull"
3026 | smull CARG1, RC, CARG3, CARG1
3027 | cmp RC, CARG1, asr #31
3028 | ins_arithfallback bne
3029 |.elif "intins" == "vm_modi"
3031 | ins_arithfallback beq
3033 | mvn CARG2, #~LJ_TISNUM
3035 | intins CARG1, CARG1, CARG3
3036 | ins_arithfallback bvs
3039 |.if "intins" == "vm_modi" or FPU
3043 | strd CARG12, [BASE, RA]
3046 | ins_arithpre_fpu d6, d7
3047 | ins_arithfallback ins_arithcheck_num
3049 |.if "intins" == "vm_modi"
3054 | ins_arithpost_fpu d6
3057 |.if "intins" ~= "vm_modi"
3064 |.macro ins_arithfp, fpins, fpcall
3066 |.if "fpins" ~= "extern" or HFABI
3067 | ins_arithpre_fpu d0, d1
3069 | ins_arithfallback ins_arithcheck_num
3070 |.if "fpins" == "extern"
3079 |.if ("fpins" ~= "extern" or HFABI) and FPU
3080 | ins_arithpost_fpu d0
3084 | strd CARG12, [BASE, RA]
3089 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3090 | ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3092 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3093 | ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3095 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3096 | ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3098 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3099 | ins_arithfp vdiv.f64, extern __aeabi_ddiv
3101 case BC_MODVN: case BC_MODNV: case BC_MODVV:
3102 | ins_arithdn vm_modi, vm_mod, ->vm_mod
3105 | // NYI: (partial) integer arithmetic.
3106 | ins_arithfp extern, extern pow
3110 | decode_RB8 RC, INS
3111 | decode_RC8 RB, INS
3112 | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!)
3115 | add CARG2, BASE, RB
3117 | // RA = dst*8, RC = src_start*8, CARG2 = top-1
3120 | lsr CARG3, CARG3, #3
3121 | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3122 | // Returns NULL (finished) or TValue * (metamethod).
3126 | ldrd CARG34, [BASE, RC]
3129 | strd CARG34, [BASE, RA] // Copy result to RA.
3133 /* -- Constant ops ------------------------------------------------------ */
3136 | // RA = dst*8, RC = str_const (~)
3139 | ldr CARG1, [KBASE, RC, lsl #2]
3140 | mvn CARG2, #~LJ_TSTR
3142 | strd CARG12, [BASE, RA]
3147 | // RA = dst*8, RC = cdata_const (~)
3150 | ldr CARG1, [KBASE, RC, lsl #2]
3151 | mvn CARG2, #~LJ_TCDATA
3153 | strd CARG12, [BASE, RA]
3158 | // RA = dst*8, (RC = int16_literal)
3159 | mov CARG1, INS, asr #16 // Refetch sign-extended reg.
3160 | mvn CARG2, #~LJ_TISNUM
3163 | strd CARG12, [BASE, RA]
3167 | // RA = dst*8, RC = num_const
3170 | ldrd CARG12, [KBASE, RC]
3172 | strd CARG12, [BASE, RA]
3176 | // RA = dst*8, RC = primitive_type (~)
3185 | // RA = base*8, RC = end
3187 | add RC, BASE, RC, lsl #3
3188 | mvn CARG1, #~LJ_TNIL
3189 | str CARG1, [RA, #4]
3192 | str CARG1, [RA, #4]
3199 /* -- Upvalue and function ops ------------------------------------------ */
3202 | // RA = dst*8, RC = uvnum
3203 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3205 | add RC, RC, #offsetof(GCfuncL, uvptr)
3206 | ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3207 | ldr CARG2, UPVAL:CARG2->v
3208 | ldrd CARG34, [CARG2]
3211 | strd CARG34, [BASE, RA]
3215 | // RA = uvnum*8, RC = src
3216 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3218 | add RA, RA, #offsetof(GCfuncL, uvptr)
3220 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3221 | ldrd CARG34, [BASE, RC]
3222 | ldrb RB, UPVAL:CARG2->marked
3223 | ldrb RC, UPVAL:CARG2->closed
3224 | ldr CARG2, UPVAL:CARG2->v
3225 | tst RB, #LJ_GC_BLACK // isblack(uv)
3226 | add RB, CARG4, #-LJ_TISGCV
3228 | strd CARG34, [CARG2]
3229 | bne >2 // Upvalue is closed and black?
3233 |2: // Check if new value is collectable.
3234 | cmn RB, #-(LJ_TISNUM - LJ_TISGCV)
3235 | ldrbhi RC, GCOBJ:CARG3->gch.marked
3236 | bls <1 // tvisgcv(v)
3237 | sub CARG1, DISPATCH, #-GG_DISP2G
3238 | tst RC, #LJ_GC_WHITES
3239 | // Crossed a write barrier. Move the barrier forward.
3243 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3246 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3251 | // RA = uvnum*8, RC = str_const (~)
3252 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3254 | add RA, RA, #offsetof(GCfuncL, uvptr)
3256 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3257 | ldr STR:CARG3, [KBASE, RC, lsl #2]
3258 | mvn CARG4, #~LJ_TSTR
3259 | ldrb RB, UPVAL:CARG2->marked
3260 | ldr CARG2, UPVAL:CARG2->v
3261 | ldrb RC, UPVAL:CARG2->closed
3262 | tst RB, #LJ_GC_BLACK // isblack(uv)
3263 | ldrb RB, STR:CARG3->marked
3264 | strd CARG34, [CARG2]
3269 |2: // Check if string is white and ensure upvalue is closed.
3270 | tst RB, #LJ_GC_WHITES // iswhite(str)
3272 | sub CARG1, DISPATCH, #-GG_DISP2G
3273 | // Crossed a write barrier. Move the barrier forward.
3277 | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3280 | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3285 | // RA = uvnum*8, RC = num_const
3286 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3288 | add RA, RA, #offsetof(GCfuncL, uvptr)
3290 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3291 | ldrd CARG34, [KBASE, RC]
3292 | ldr CARG2, UPVAL:CARG2->v
3295 | strd CARG34, [CARG2]
3299 | // RA = uvnum*8, RC = primitive_type (~)
3300 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3302 | add RA, RA, #offsetof(GCfuncL, uvptr)
3303 | ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3305 | ldr CARG2, UPVAL:CARG2->v
3308 | str RC, [CARG2, #4]
3313 | // RA = level*8, RC = target
3314 | ldr CARG3, L->openupval
3315 | add RC, PC, RC, lsl #2
3318 | sub PC, RC, #0x20000
3321 | add CARG2, BASE, RA
3322 | bl extern lj_func_closeuv // (lua_State *L, TValue *level)
3329 | // RA = dst*8, RC = proto_const (~) (holding function prototype)
3332 | ldr CARG2, [KBASE, RC, lsl #2]
3334 | ldr CARG3, [BASE, FRAME_FUNC]
3336 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3337 | bl extern lj_func_newL_gc
3338 | // Returns GCfuncL *.
3340 | mvn CARG2, #~LJ_TFUNC
3343 | strd CARG12, [BASE, RA]
3347 /* -- Table ops --------------------------------------------------------- */
3351 | // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3352 if (op == BC_TDUP) {
3355 | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3356 | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3363 if (op == BC_TNEW) {
3364 | lsl CARG2, RC, #21
3365 | lsr CARG3, RC, #11
3366 | asr RC, CARG2, #21
3367 | lsr CARG2, CARG2, #21
3369 | addeq CARG2, CARG2, #2
3370 | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3371 | // Returns GCtab *.
3373 | ldr CARG2, [KBASE, RC, lsl #2]
3374 | bl extern lj_tab_dup // (lua_State *L, Table *kt)
3375 | // Returns GCtab *.
3378 | mvn CARG2, #~LJ_TTAB
3381 | strd CARG12, [BASE, RA]
3384 | bl extern lj_gc_step_fixtop // (lua_State *L)
3390 | // RA = dst*8, RC = str_const (~)
3392 | // RA = dst*8, RC = str_const (~)
3393 | ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3395 | ldr TAB:CARG1, LFUNC:CARG2->env
3396 | ldr STR:RC, [KBASE, RC, lsl #2]
3397 if (op == BC_GGET) {
3405 | decode_RB8 RB, INS
3406 | decode_RC8 RC, INS
3407 | // RA = dst*8, RB = table*8, RC = key*8
3408 | ldrd TAB:CARG12, [BASE, RB]
3409 | ldrd CARG34, [BASE, RC]
3410 | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12.
3411 | checktp CARG4, LJ_TISNUM // Integer key?
3412 | ldreq CARG4, TAB:CARG1->array
3413 | ldreq CARG2, TAB:CARG1->asize
3416 | add CARG4, CARG4, CARG3, lsl #3
3417 | cmp CARG3, CARG2 // In array part?
3418 | ldrdlo CARG34, [CARG4]
3420 | ins_next1 // Overwrites RB!
3421 | checktp CARG4, LJ_TNIL
3425 | strd CARG34, [BASE, RA]
3428 |5: // Check for __index if table value is nil.
3429 | ldr TAB:CARG2, TAB:CARG1->metatable
3431 | beq <1 // No metatable: done.
3432 | ldrb CARG2, TAB:CARG2->nomm
3433 | tst CARG2, #1<<MM_index
3434 | bne <1 // 'no __index' flag set: done.
3435 | decode_RB8 RB, INS // Restore RB.
3439 | checktp CARG4, LJ_TSTR // String key?
3440 | moveq STR:RC, CARG3
3445 | decode_RB8 RB, INS
3447 | // RA = dst*8, RB = table*8, RC = str_const (~)
3448 | ldrd CARG12, [BASE, RB]
3450 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3451 | checktab CARG2, ->vmeta_tgets1
3453 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3454 | ldr CARG3, TAB:CARG1->hmask
3455 | ldr CARG4, STR:RC->hash
3456 | ldr NODE:INS, TAB:CARG1->node
3457 | mov TAB:RB, TAB:CARG1
3458 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
3459 | add CARG3, CARG3, CARG3, lsl #1
3460 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3462 | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS.
3463 | ldrd CARG34, NODE:INS->val
3464 | ldr NODE:INS, NODE:INS->next
3465 | checktp CARG2, LJ_TSTR
3466 | cmpeq CARG1, STR:RC
3468 | checktp CARG4, LJ_TNIL
3473 | strd CARG34, [BASE, RA]
3476 |4: // Follow hash chain.
3479 | // End of hash chain: key not found, nil result.
3481 |5: // Check for __index if table value is nil.
3482 | ldr TAB:CARG1, TAB:RB->metatable
3483 | mov CARG3, #0 // Optional clear of undef. value (during load stall).
3484 | mvn CARG4, #~LJ_TNIL
3486 | beq <3 // No metatable: done.
3487 | ldrb CARG2, TAB:CARG1->nomm
3488 | tst CARG2, #1<<MM_index
3489 | bne <3 // 'no __index' flag set: done.
3493 | decode_RB8 RB, INS
3495 | // RA = dst*8, RB = table*8, RC = index
3496 | ldrd CARG12, [BASE, RB]
3497 | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12.
3498 | ldr CARG3, TAB:CARG1->asize
3499 | ldr CARG4, TAB:CARG1->array
3502 | ldrdlo CARG34, [CARG4, CARG2]
3504 | ins_next1 // Overwrites RB!
3505 | checktp CARG4, LJ_TNIL
3509 | strd CARG34, [BASE, RA]
3512 |5: // Check for __index if table value is nil.
3513 | ldr TAB:CARG2, TAB:CARG1->metatable
3515 | beq <1 // No metatable: done.
3516 | ldrb CARG2, TAB:CARG2->nomm
3517 | tst CARG2, #1<<MM_index
3518 | bne <1 // 'no __index' flag set: done.
3523 | decode_RB8 RB, INS
3524 | decode_RC8 RC, INS
3525 | // RA = src*8, RB = table*8, RC = key*8
3526 | ldrd TAB:CARG12, [BASE, RB]
3527 | ldrd CARG34, [BASE, RC]
3528 | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12.
3529 | checktp CARG4, LJ_TISNUM // Integer key?
3530 | ldreq CARG2, TAB:CARG1->array
3531 | ldreq CARG4, TAB:CARG1->asize
3534 | add CARG2, CARG2, CARG3, lsl #3
3535 | cmp CARG3, CARG4 // In array part?
3536 | ldrlo INS, [CARG2, #4]
3538 | ins_next1 // Overwrites RB!
3539 | checktp INS, LJ_TNIL
3540 | ldrb INS, TAB:CARG1->marked
3541 | ldrd CARG34, [BASE, RA]
3544 | tst INS, #LJ_GC_BLACK // isblack(table)
3545 | strd CARG34, [CARG2]
3551 |5: // Check for __newindex if previous value is nil.
3552 | ldr TAB:RA, TAB:CARG1->metatable
3554 | beq <1 // No metatable: done.
3555 | ldrb RA, TAB:RA->nomm
3556 | tst RA, #1<<MM_newindex
3557 | bne <1 // 'no __newindex' flag set: done.
3558 | ldr INS, [PC, #-4] // Restore RA and RB.
3559 | decode_RB8 RB, INS
3560 | decode_RA8 RA, INS
3563 |7: // Possible table write barrier for the value. Skip valiswhite check.
3564 | barrierback TAB:CARG1, INS, CARG3
3568 | checktp CARG4, LJ_TSTR // String key?
3569 | moveq STR:RC, CARG3
3574 | decode_RB8 RB, INS
3576 | // RA = src*8, RB = table*8, RC = str_const (~)
3577 | ldrd CARG12, [BASE, RB]
3579 | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC.
3580 | checktab CARG2, ->vmeta_tsets1
3582 | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3583 | ldr CARG3, TAB:CARG1->hmask
3584 | ldr CARG4, STR:RC->hash
3585 | ldr NODE:INS, TAB:CARG1->node
3586 | mov TAB:RB, TAB:CARG1
3587 | and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
3588 | add CARG3, CARG3, CARG3, lsl #1
3590 | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
3591 | strb CARG4, TAB:RB->nomm // Clear metamethod cache.
3593 | ldrd CARG12, NODE:INS->key
3594 | ldr CARG4, NODE:INS->val.it
3595 | ldr NODE:CARG3, NODE:INS->next
3596 | checktp CARG2, LJ_TSTR
3597 | cmpeq CARG1, STR:RC
3599 | ldrb CARG2, TAB:RB->marked
3600 | checktp CARG4, LJ_TNIL // Key found, but nil value?
3601 | ldrd CARG34, [BASE, RA]
3604 | tst CARG2, #LJ_GC_BLACK // isblack(table)
3605 | strd CARG34, NODE:INS->val
3610 |4: // Check for __newindex if previous value is nil.
3611 | ldr TAB:CARG1, TAB:RB->metatable
3613 | beq <2 // No metatable: done.
3614 | ldrb CARG1, TAB:CARG1->nomm
3615 | tst CARG1, #1<<MM_newindex
3616 | bne <2 // 'no __newindex' flag set: done.
3619 |5: // Follow hash chain.
3620 | movs NODE:INS, NODE:CARG3
3622 | // End of hash chain: key not found, add a new one.
3624 | // But check for __newindex first.
3625 | ldr TAB:CARG1, TAB:RB->metatable
3628 | cmp TAB:CARG1, #0 // No metatable: continue.
3630 | ldrbne CARG2, TAB:CARG1->nomm
3633 | tst CARG2, #1<<MM_newindex
3634 | beq ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3636 | mvn CARG4, #~LJ_TSTR
3637 | str STR:RC, TMPDlo
3640 | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3641 | // Returns TValue *.
3643 | ldrd CARG34, [BASE, RA]
3644 | strd CARG34, [CRET1]
3645 | b <3 // No 2nd write barrier needed.
3647 |7: // Possible table write barrier for the value. Skip valiswhite check.
3648 | barrierback TAB:RB, CARG2, CARG3
3652 | decode_RB8 RB, INS
3654 | // RA = src*8, RB = table*8, RC = index
3655 | ldrd CARG12, [BASE, RB]
3656 | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12.
3657 | ldr CARG3, TAB:CARG1->asize
3658 | ldr RB, TAB:CARG1->array
3661 | ldrdlo CARG34, [CARG2, RB]!
3663 | ins_next1 // Overwrites RB!
3664 | checktp CARG4, LJ_TNIL
3665 | ldrb INS, TAB:CARG1->marked
3666 | ldrd CARG34, [BASE, RA]
3669 | tst INS, #LJ_GC_BLACK // isblack(table)
3670 | strd CARG34, [CARG2]
3676 |5: // Check for __newindex if previous value is nil.
3677 | ldr TAB:RA, TAB:CARG1->metatable
3679 | beq <1 // No metatable: done.
3680 | ldrb RA, TAB:RA->nomm
3681 | tst RA, #1<<MM_newindex
3682 | bne <1 // 'no __newindex' flag set: done.
3683 | ldr INS, [PC, #-4] // Restore INS.
3684 | decode_RA8 RA, INS
3687 |7: // Possible table write barrier for the value. Skip valiswhite check.
3688 | barrierback TAB:CARG1, INS, CARG3
3693 | // RA = base*8 (table at base-1), RC = num_const (start index)
3696 | ldr RB, SAVE_MULTRES
3697 | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table.
3698 | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word.
3700 | ldr CARG4, TAB:CARG2->asize
3701 | beq >4 // Nothing to copy?
3702 | add CARG3, CARG1, RB, lsr #3
3704 | ldr CARG4, TAB:CARG2->array
3707 | add INS, CARG4, CARG1, lsl #3
3708 | ldrb CARG1, TAB:CARG2->marked
3709 |3: // Copy result slots to table.
3710 | ldrd CARG34, [RA], #8
3711 | strd CARG34, [INS], #8
3714 | tst CARG1, #LJ_GC_BLACK // isblack(table)
3719 |5: // Need to resize array part.
3723 | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3724 | // Must not reallocate the stack.
3725 | .IOS ldr BASE, L->base
3728 |7: // Possible table write barrier for any value. Skip valiswhite check.
3729 | barrierback TAB:CARG2, CARG1, CARG3
3733 /* -- Calls and vararg handling ----------------------------------------- */
3736 | // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3737 | ldr CARG1, SAVE_MULTRES
3738 | decode_RC8 NARGS8:RC, INS
3739 | add NARGS8:RC, NARGS8:RC, CARG1
3743 | decode_RC8 NARGS8:RC, INS
3744 | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3746 | mov RB, BASE // Save old BASE for vmeta_call.
3747 | ldrd CARG34, [BASE, RA]!
3748 | sub NARGS8:RC, NARGS8:RC, #8
3749 | add BASE, BASE, #8
3750 | checkfunc CARG4, ->vmeta_call
3755 | // RA = base*8, (RB = 0,) RC = extra_nargs
3756 | ldr CARG1, SAVE_MULTRES
3757 | add NARGS8:RC, CARG1, RC, lsl #3
3761 | lsl NARGS8:RC, RC, #3
3762 | // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3764 | ldrd LFUNC:CARG34, [RA, BASE]!
3765 | sub NARGS8:RC, NARGS8:RC, #8
3767 | checkfunc CARG4, ->vmeta_callt
3768 | ldr PC, [BASE, FRAME_PC]
3771 | ldrb CARG4, LFUNC:CARG3->ffid
3772 | tst PC, #FRAME_TYPE
3775 | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC.
3779 | ldrd CARG12, [RA, RB]
3781 | cmp INS, NARGS8:RC
3782 | strd CARG12, [BASE, RB]
3786 | cmp CARG4, #1 // (> FF_C) Calling a fast function?
3791 |5: // Tailcall to a fast function with a Lua frame below.
3792 | ldr INS, [PC, #-4]
3793 | decode_RA8 RA, INS
3794 | sub CARG1, BASE, RA
3795 | ldr LFUNC:CARG1, [CARG1, #-16]
3796 | ldr CARG1, LFUNC:CARG1->field_pc
3797 | ldr KBASE, [CARG1, #PC2PROTO(k)]
3800 |7: // Tailcall from a vararg function.
3801 | eor PC, PC, #FRAME_VARG
3802 | tst PC, #FRAME_TYPEP // Vararg frame below?
3803 | movne CARG4, #0 // Clear ffid if no Lua function below.
3805 | sub BASE, BASE, PC
3806 | ldr PC, [BASE, FRAME_PC]
3807 | tst PC, #FRAME_TYPE
3808 | movne CARG4, #0 // Clear ffid if no Lua function below.
3813 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3815 | mov RB, BASE // Save old BASE for vmeta_call.
3816 | ldrd CARG34, [RA, #-16]
3817 | ldrd CARG12, [RA, #-8]
3819 | strd CARG34, [RA, #8] // Copy state.
3820 | strd CARG12, [RA, #16] // Copy control var.
3821 | // STALL: locked CARG34.
3822 | ldrd LFUNC:CARG34, [RA, #-24]
3823 | mov NARGS8:RC, #16 // Iterators get 2 arguments.
3824 | // STALL: load CARG34.
3825 | strd LFUNC:CARG34, [RA] // Copy callable.
3826 | checkfunc CARG4, ->vmeta_call
3831 | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3833 | // NYI: add hotloop, record BC_ITERN.
3836 | ldr TAB:RB, [RA, #-16]
3837 | ldr CARG1, [RA, #-8] // Get index from control var.
3838 | ldr INS, TAB:RB->asize
3839 | ldr CARG2, TAB:RB->array
3841 |1: // Traverse array part.
3842 | subs RC, CARG1, INS
3843 | add CARG3, CARG2, CARG1, lsl #3
3844 | bhs >5 // Index points after array part?
3845 | ldrd CARG34, [CARG3]
3846 | checktp CARG4, LJ_TNIL
3847 | addeq CARG1, CARG1, #1 // Skip holes in array part.
3849 | ldrh RC, [PC, #-2]
3850 | mvn CARG2, #~LJ_TISNUM
3851 | strd CARG34, [RA, #8]
3852 | add RC, PC, RC, lsl #2
3855 | sub PC, RC, #0x20000
3856 | str RB, [RA, #-8] // Update control var.
3860 |5: // Traverse hash part.
3861 | ldr CARG4, TAB:RB->hmask
3862 | ldr NODE:RB, TAB:RB->node
3864 | add CARG1, RC, RC, lsl #1
3865 | cmp RC, CARG4 // End of iteration? Branch to ITERL+1.
3866 | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8
3868 | ldrd CARG12, NODE:CARG3->val
3869 | checktp CARG2, LJ_TNIL
3871 | beq <6 // Skip holes in hash part.
3872 | ldrh RB, [PC, #-2]
3874 | ldrd CARG34, NODE:CARG3->key
3875 | str RC, [RA, #-8] // Update control var.
3876 | strd CARG12, [RA, #8]
3877 | add RC, PC, RB, lsl #2
3878 | sub PC, RC, #0x20000
3884 | // RA = base*8, RC = target (points to ITERN)
3886 | add RC, PC, RC, lsl #2
3887 | ldrd CFUNC:CARG12, [RA, #-24]
3888 | ldr CARG3, [RA, #-12]
3889 | ldr CARG4, [RA, #-4]
3890 | checktp CARG2, LJ_TFUNC
3891 | ldrbeq CARG1, CFUNC:CARG1->ffid
3892 | checktpeq CARG3, LJ_TTAB
3893 | checktpeq CARG4, LJ_TNIL
3894 | cmpeq CARG1, #FF_next_N
3895 | subeq PC, RC, #0x20000
3900 | str CARG1, [RA, #-8] // Initialize control var.
3903 |5: // Despecialize bytecode if any of the checks fail.
3904 | mov CARG1, #BC_JMP
3906 | strb CARG1, [PC, #-4]
3907 | sub PC, RC, #0x20000
3908 | strb OP, [PC] // Subsumes ins_next1.
3914 | decode_RB8 RB, INS
3915 | decode_RC8 RC, INS
3916 | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
3917 | ldr CARG1, [BASE, FRAME_PC]
3920 | add RC, RC, #FRAME_VARG
3922 | sub CARG3, BASE, #8 // CARG3 = vtop
3923 | sub RC, RC, CARG1 // RC = vbase
3924 | // Note: RC may now be even _above_ BASE if nargs was < numparams.
3926 | sub CARG1, CARG3, RC
3927 | beq >5 // Copy all varargs?
3928 | sub CARG4, CARG4, #16
3929 |1: // Copy vararg slots to destination slots.
3931 | ldrdlo CARG12, [RC], #8
3932 | mvnhs CARG2, #~LJ_TNIL
3934 | strd CARG12, [RA], #8
3939 |5: // Copy all varargs.
3940 | ldr CARG4, L->maxstack
3942 | movle RB, #8 // MULTRES = (0+1)*8
3943 | addgt RB, CARG1, #8
3944 | add CARG2, RA, CARG1
3945 | str RB, SAVE_MULTRES
3950 | ldrd CARG12, [RC], #8
3951 | strd CARG12, [RA], #8
3956 |7: // Grow stack for varargs.
3957 | lsr CARG2, CARG1, #3
3961 | sub RC, RC, BASE // Need delta, because BASE may change.
3964 | bl extern lj_state_growstack // (lua_State *L, int n)
3968 | sub CARG3, BASE, #8
3972 /* -- Returns ----------------------------------------------------------- */
3975 | // RA = results*8, RC = extra results
3976 | ldr CARG1, SAVE_MULTRES
3977 | ldr PC, [BASE, FRAME_PC]
3979 | add RC, CARG1, RC, lsl #3
3984 | // RA = results*8, RC = nresults+1
3985 | ldr PC, [BASE, FRAME_PC]
3989 | str RC, SAVE_MULTRES
3991 | ands CARG1, PC, #FRAME_TYPE
3992 | eor CARG2, PC, #FRAME_VARG
3996 | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
3997 | ldr INS, [PC, #-4]
3998 | subs CARG4, RC, #8
3999 | sub CARG3, BASE, #8
4002 | ldrd CARG12, [RA], #8
4003 | add BASE, BASE, #8
4004 | subs CARG4, CARG4, #8
4005 | strd CARG12, [BASE, #-16]
4008 | decode_RA8 RA, INS
4009 | sub CARG4, CARG3, RA
4010 | decode_RB8 RB, INS
4011 | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4013 | cmp RB, RC // More results expected?
4016 | ldr CARG2, LFUNC:CARG1->field_pc
4019 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4022 |6: // Fill up results with nil.
4023 | mvn CARG2, #~LJ_TNIL
4024 | add BASE, BASE, #8
4026 | str CARG2, [BASE, #-12]
4029 |->BC_RETV1_Z: // Non-standard return case.
4032 | tst CARG2, #FRAME_TYPEP
4034 | // Return from vararg function: relocate BASE down.
4035 | sub BASE, BASE, CARG2
4036 | ldr PC, [BASE, FRAME_PC]
4040 case BC_RET0: case BC_RET1:
4041 | // RA = results*8, RC = nresults+1
4042 | ldr PC, [BASE, FRAME_PC]
4044 | str RC, SAVE_MULTRES
4045 | ands CARG1, PC, #FRAME_TYPE
4046 | eor CARG2, PC, #FRAME_VARG
4047 | ldreq INS, [PC, #-4]
4049 if (op == BC_RET1) {
4050 | ldrd CARG12, [BASE, RA]
4052 | sub CARG4, BASE, #8
4053 | decode_RA8 RA, INS
4054 if (op == BC_RET1) {
4055 | strd CARG12, [CARG4]
4057 | sub BASE, CARG4, RA
4058 | decode_RB8 RB, INS
4059 | ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4063 | ldr CARG2, LFUNC:CARG1->field_pc
4066 | ldr KBASE, [CARG2, #PC2PROTO(k)]
4069 |6: // Fill up results with nil.
4070 | sub CARG2, CARG4, #4
4071 | mvn CARG3, #~LJ_TNIL
4072 | str CARG3, [CARG2, RC]
4077 /* -- Loops and branches ------------------------------------------------ */
4079 |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4]
4080 |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12]
4081 |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4082 |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28]
4088 | // Fall through. Assumes BC_IFORL follows.
4098 | // RA = base*8, RC = target (after end of loop or start of loop)
4099 vk = (op == BC_IFORL || op == BC_JFORL);
4100 | ldrd CARG12, [RA, BASE]!
4101 if (op != BC_JFORL) {
4102 | add RC, PC, RC, lsl #2
4105 | ldrd CARG34, FOR_STOP
4106 | checktp CARG2, LJ_TISNUM
4109 | checktp CARG4, LJ_TISNUM
4110 | ldr CARG4, FOR_STEP
4111 | checktpeq RB, LJ_TISNUM
4117 | ldrd CARG34, FOR_STEP
4118 | checktp CARG2, LJ_TISNUM
4120 | adds CARG1, CARG1, CARG3
4121 | ldr CARG4, FOR_STOP
4122 if (op == BC_IFORL) {
4123 | addvs RC, PC, #0x20000 // Overflow: prevent branch.
4125 | bvs >2 // Overflow: do not enter mcode.
4132 if (op == BC_FORI) {
4133 | subgt PC, RC, #0x20000
4134 } else if (op == BC_JFORI) {
4135 | sub PC, RC, #0x20000
4136 | ldrhle RC, [PC, #-2]
4137 } else if (op == BC_IFORL) {
4138 | suble PC, RC, #0x20000
4141 | strd CARG12, FOR_IDX
4146 | strd CARG12, FOR_EXT
4147 if (op == BC_JFORI || op == BC_JFORL) {
4153 |4: // Invert check for negative step.
4163 | cmnlo CARG4, #-LJ_TISNUM
4164 | cmnlo RB, #-LJ_TISNUM
4173 | strd CARG12, FOR_EXT
4182 | vadd.f64 d0, d0, d2
4186 | bl extern __aeabi_dadd
4187 | strd CARG12, FOR_IDX
4188 | ldrd CARG34, FOR_STOP
4189 | strd CARG12, FOR_EXT
4198 | bl extern __aeabi_cdcmple
4206 if (op == BC_FORI) {
4207 | subhi PC, RC, #0x20000
4208 } else if (op == BC_JFORI) {
4209 | sub PC, RC, #0x20000
4210 | ldrhls RC, [PC, #-2]
4212 } else if (op == BC_IFORL) {
4213 | subls PC, RC, #0x20000
4222 |8: // Invert check for negative step.
4224 | bl extern __aeabi_dadd
4225 | strd CARG12, FOR_IDX
4226 | strd CARG12, FOR_EXT
4230 | ldrd CARG12, FOR_STOP
4239 | // Fall through. Assumes BC_IITERL follows.
4247 | // RA = base*8, RC = target
4248 | ldrd CARG12, [RA, BASE]!
4249 if (op == BC_JITERL) {
4250 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4251 | strdne CARG12, [RA, #-8]
4254 | add RC, PC, RC, lsl #2
4255 | // STALL: load CARG12.
4256 | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil.
4257 | subne PC, RC, #0x20000 // Otherwise save control var + branch.
4258 | strdne CARG12, [RA, #-8]
4264 | // RA = base*8, RC = target (loop extent)
4265 | // Note: RA/RC is only used by trace recorder to determine scope/extent
4266 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4270 | // Fall through. Assumes BC_ILOOP follows.
4274 | // RA = base*8, RC = target (loop extent)
4280 | // RA = base (ignored), RC = traceno
4281 | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4282 | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0.
4283 | ldr TRACE:RC, [CARG1, RC, lsl #2]
4285 | ldr RA, TRACE:RC->mcode
4286 | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4287 | str L, [DISPATCH, #DISPATCH_GL(jit_L)]
4293 | // RA = base*8 (only used by trace recorder), RC = target
4294 | add RC, PC, RC, lsl #2
4295 | sub PC, RC, #0x20000
4299 /* -- Function headers -------------------------------------------------- */
4305 case BC_FUNCV: /* NYI: compiled vararg functions. */
4306 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4314 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4315 | ldr CARG1, L->maxstack
4316 | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4317 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4319 | bhi ->vm_growstack_l
4320 if (op != BC_JFUNCF) {
4325 | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters.
4326 | mvn CARG4, #~LJ_TNIL
4328 if (op == BC_JFUNCF) {
4335 |3: // Clear missing parameters.
4336 | strd CARG34, [BASE, NARGS8:RC]
4337 | add NARGS8:RC, NARGS8:RC, #8
4345 | NYI // NYI: compiled vararg functions
4346 break; /* NYI: compiled vararg functions. */
4349 | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4350 | ldr CARG1, L->maxstack
4351 | add CARG4, BASE, RC
4353 | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC.
4354 | add CARG2, RC, #8+FRAME_VARG
4355 | ldr KBASE, [PC, #-4+PC2PROTO(k)]
4357 | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG.
4358 | bhs ->vm_growstack_l
4359 | ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4363 | add BASE, CARG4, #8
4365 | mvn CARG3, #~LJ_TNIL
4367 | cmp RA, RC // Less args than parameters?
4368 | ldrdlo CARG12, [RA], #8
4369 | movhs CARG2, CARG3
4370 | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC).
4373 | strd CARG12, [CARG4, #8]!
4381 | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4382 if (op == BC_FUNCC) {
4383 | ldr CARG4, CFUNC:CARG3->f
4385 | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4387 | add CARG2, RA, NARGS8:RC
4388 | ldr CARG1, L->maxstack
4389 | add RC, BASE, NARGS8:RC
4393 if (op == BC_FUNCCW) {
4394 | ldr CARG2, CFUNC:CARG3->f
4396 | mv_vmstate CARG3, C
4398 | bhi ->vm_growstack_c // Need to grow stack.
4400 | blx CARG4 // (lua_State *L [, lua_CFunction f])
4401 | // Returns nresults.
4403 | mv_vmstate CARG3, INTERP
4407 | ldr PC, [BASE, FRAME_PC]
4408 | sub RA, CRET2, RC // RA = L->top - nresults*8
4412 /* ---------------------------------------------------------------------- */
4415 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4421 static int build_backend(BuildCtx *ctx)
4425 dasm_growpc(Dst, BC__MAX);
4427 build_subroutines(ctx);
4430 for (op = 0; op < BC__MAX; op++)
4431 build_ins(ctx, (BCOp)op, op);
4436 /* Emit pseudo frame-info for all assembler functions. */
4437 static void emit_asm_debug(BuildCtx *ctx)
4439 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4441 switch (ctx->mode) {
4443 fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4446 "\t.long .LECIE0-.LSCIE0\n"
4448 "\t.long 0xffffffff\n"
4453 "\t.byte 0xe\n" /* Return address is in lr. */
4454 "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */
4459 "\t.long .LEFDE0-.LASFDE0\n"
4461 "\t.long .Lframe0\n"
4464 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4465 "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */
4466 fcofs, CFRAME_SIZE);
4467 for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */
4468 fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4470 for (i = 15; i >= 8; i--) /* offset d8-d15 */
4471 fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4472 64+2*i, 10+2*(15-i));
4473 fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */
4481 "\t.long .LEFDE1-.LASFDE1\n"
4483 "\t.long .Lframe0\n"
4484 "\t.long lj_vm_ffi_call\n"
4486 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4487 "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */
4488 "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */
4489 "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */
4490 "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */
4491 "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */
4493 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);