OSX/iOS: Fix SDK incompatibility.
[luajit-2.0.git] / src / vm_arm.dasc
blobfc08c658f0dfddff1e5aa9c368c3095214580541
1 |// Low-level VM code for ARM CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
5 |.arch arm
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
9 |.globals GLOB_
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.
33 |.define RB,            r12
34 |.define OP,            r12     // Overlaps RB, must not be lr.
35 |.define INS,           lr
37 |// Calling conventions. Also used as temporaries.
38 |.define CARG1,         r0
39 |.define CARG2,         r1
40 |.define CARG3,         r2
41 |.define CARG4,         r3
42 |.define CARG12,        r0      // For 1st soft-fp double.
43 |.define CARG34,        r2      // For 2nd soft-fp double.
45 |.define CRET1,         r0
46 |.define CRET2,         r1
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]
57 |.define ARG5,          [sp]
59 |.define TMPDhi,        [sp, #4]
60 |.define TMPDlo,        [sp]
61 |.define TMPD,          [sp]
62 |.define TMPDp,         sp
64 |.if FPU
65 |.macro saveregs
66 |  push {r5, r6, r7, r8, r9, r10, r11, lr}
67 |  vpush {d8-d15}
68 |  sub sp, sp, CFRAME_SPACE+4
69 |  str r4, SAVE_R4
70 |.endmacro
71 |.macro restoreregs_ret
72 |  ldr r4, SAVE_R4
73 |  add sp, sp, CFRAME_SPACE+4
74 |  vpop {d8-d15}
75 |  pop {r5, r6, r7, r8, r9, r10, r11, pc}
76 |.endmacro
77 |.else
78 |.macro saveregs
79 |  push {r4, r5, r6, r7, r8, r9, r10, r11, lr}
80 |  sub sp, sp, CFRAME_SPACE
81 |.endmacro
82 |.macro restoreregs_ret
83 |  add sp, sp, CFRAME_SPACE
84 |  pop {r4, r5, r6, r7, r8, r9, r10, r11, pc}
85 |.endmacro
86 |.endif
88 |// Type definitions. Some of these are only used for documentation.
89 |.type L,               lua_State,      LREG
90 |.type GL,              global_State
91 |.type TVALUE,          TValue
92 |.type GCOBJ,           GCobj
93 |.type STR,             GCstr
94 |.type TAB,             GCtab
95 |.type LFUNC,           GCfuncL
96 |.type CFUNC,           GCfuncC
97 |.type PROTO,           GCproto
98 |.type UPVAL,           GCupval
99 |.type NODE,            Node
100 |.type NARGS8,          int
101 |.type TRACE,           GCtrace
102 |.type SBUF,            SBuf
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.
122 |.macro ins_NEXT1
123 |  ldrb OP, [PC]
124 |.endmacro
125 |.macro ins_NEXT2
126 |   ldr INS, [PC], #4
127 |.endmacro
128 |// Instruction decode+dispatch.
129 |.macro ins_NEXT3
130 |  ldr OP, [DISPATCH, OP, lsl #2]
131 |   decode_RA8 RA, INS
132 |   decode_RD RC, INS
133 |  bx OP
134 |.endmacro
135 |.macro ins_NEXT
136 |  ins_NEXT1
137 |  ins_NEXT2
138 |  ins_NEXT3
139 |.endmacro
141 |// Instruction footer.
142 |.if 1
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
149 |.else
150 |  // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
151 |  // Affects only certain kinds of benchmarks (and only with -j off).
152 |  .macro ins_next
153 |    b ->ins_next
154 |  .endmacro
155 |  .macro ins_next1
156 |  .endmacro
157 |  .macro ins_next2
158 |  .endmacro
159 |  .macro ins_next3
160 |    b ->ins_next
161 |  .endmacro
162 |  .macro ins_next_
163 |  ->ins_next:
164 |    ins_NEXT
165 |  .endmacro
166 |.endif
168 |// Avoid register name substitution for field name.
169 #define field_pc        pc
171 |// Call decode and dispatch.
172 |.macro ins_callt
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.
176 |   ldr INS, [PC], #4
177 |  ldr OP, [DISPATCH, OP, lsl #2]  // STALL: load OP. early OP.
178 |   decode_RA8 RA, INS
179 |   add RA, RA, BASE
180 |  bx OP
181 |.endmacro
183 |.macro ins_call
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.
187 |.endmacro
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
206 |  lsr CARG1, PC, #1
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]
212 |.endmacro
214 |.macro hotloop
215 |  hotcheck HOTCOUNT_LOOP
216 |  blo ->vm_hotloop
217 |.endmacro
219 |.macro hotcall
220 |  hotcheck HOTCOUNT_CALL
221 |  blo ->vm_hotcall
222 |.endmacro
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
235 |.endmacro
237 |.macro .IOS, a, b
238 |.if IOS
239 |  a, b
240 |.endif
241 |.endmacro
243 |//-----------------------------------------------------------------------
245 #if !LJ_DUALNUM
246 #error "Only dual-number mode supported for ARM target"
247 #endif
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)
253   |.code_sub
254   |
255   |//-----------------------------------------------------------------------
256   |//-- Return handling ----------------------------------------------------
257   |//-----------------------------------------------------------------------
258   |
259   |->vm_returnp:
260   |  // See vm_return. Also: RB = previous base.
261   |  tst PC, #FRAME_P
262   |  beq ->cont_dispatch
263   |
264   |  // Return from pcall or xpcall fast func.
265   |  ldr PC, [RB, FRAME_PC]             // Fetch PC of previous frame.
266   |   mvn CARG2, #~LJ_TTRUE
267   |  mov BASE, RB
268   |  // Prepending may overwrite the pcall frame, so do it at the end.
269   |   str CARG2, [RA, FRAME_PC]         // Prepend true to results.
270   |  sub RA, RA, #8
271   |
272   |->vm_returnc:
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.
279   |
280   |->vm_return:
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.
286   |   bne ->vm_returnp
287   |
288   |  str RB, L->base
289   |   ldr KBASE, SAVE_NRES
290   |    mv_vmstate CARG4, C
291   |   sub BASE, BASE, #8
292   |  subs CARG3, RC, #8
293   |   lsl KBASE, KBASE, #3              // KBASE = (nresults_wanted+1)*8
294   |    st_vmstate CARG4
295   |  beq >2
296   |1:
297   |  subs CARG3, CARG3, #8
298   |   ldrd CARG12, [RA], #8
299   |   strd CARG12, [BASE], #8
300   |  bne <1
301   |2:
302   |  cmp KBASE, RC                      // More/less results wanted?
303   |  bne >6
304   |3:
305   |  str BASE, L->top                   // Store new top.
306   |
307   |->vm_leave_cp:
308   |  ldr RC, SAVE_CFRAME                // Restore previous C frame.
309   |   mov CRET1, #0                     // Ok return status for vm_pcall.
310   |  str RC, L->cframe
311   |
312   |->vm_leave_unw:
313   |  restoreregs_ret
314   |
315   |6:
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
320   |  cmp BASE, CARG3
321   |  bhs >8
322   |   str CARG2, [BASE, #4]
323   |  add RC, RC, #8
324   |  add BASE, BASE, #8
325   |  b <2
326   |
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.
331   |  b <3
332   |
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
340   |  mov CARG1, L
341   |  bl extern lj_state_growstack       // (lua_State *L, int n)
342   |  ldr BASE, L->top                   // Need the (realloced) L->top in BASE.
343   |  b <2
344   |
345   |->vm_unwind_c:                       // Unwind C stack, return from vm_pcall.
346   |  // (void *cframe, int errcode)
347   |  mov sp, CARG1
348   |  mov CRET1, CARG2
349   |->vm_unwind_c_eh:                    // Landing pad for external unwinder.
350   |  ldr L, SAVE_L
351   |   mv_vmstate CARG4, C
352   |  ldr GL:CARG3, L->glref
353   |   str CARG4, GL:CARG3->vmstate
354   |  b ->vm_leave_unw
355   |
356   |->vm_unwind_ff:                      // Unwind C stack, return from ff pcall.
357   |  // (void *cframe)
358   |  bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
359   |  mov sp, CARG1
360   |->vm_unwind_ff_eh:                   // Landing pad for external unwinder.
361   |  ldr L, SAVE_L
362   |   mov MASKR8, #255
363   |    mov RC, #16                      // 2 results: false + error message.
364   |   lsl MASKR8, MASKR8, #3            // MASKR8 = 255*8.
365   |  ldr BASE, L->base
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.
373   |   st_vmstate CARG2
374   |  b ->vm_returnc
375   |
376   |->vm_unwind_ext:                     // Complete external unwind.
377 #if !LJ_NO_UNWIND
378   |  push {r0, r1, r2, lr}
379   |  bl extern _Unwind_Complete
380   |  ldr r0, [sp]
381   |  bl extern _Unwind_DeleteException
382   |  pop {r0, r1, r2, lr}
383   |  mov r0, r1
384   |  bx r2
385 #endif
386   |
387   |//-----------------------------------------------------------------------
388   |//-- Grow stack for calls -----------------------------------------------
389   |//-----------------------------------------------------------------------
390   |
391   |->vm_growstack_c:                    // Grow stack for C function.
392   |  // CARG1 = L
393   |  mov CARG2, #LUA_MINSTACK
394   |  b >2
395   |
396   |->vm_growstack_l:                    // Grow stack for Lua function.
397   |  // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
398   |  add RC, BASE, RC
399   |   sub RA, RA, BASE
400   |    mov CARG1, L
401   |  str BASE, L->base
402   |   add PC, PC, #4                    // Must point after first instruction.
403   |  str RC, L->top
404   |   lsr CARG2, RA, #3
405   |2:
406   |  // L->base = new base, L->top = top
407   |  str PC, SAVE_PC
408   |  bl extern lj_state_growstack       // (lua_State *L, int n)
409   |  ldr BASE, L->base
410   |   ldr RC, L->top
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.
415   |
416   |//-----------------------------------------------------------------------
417   |//-- Entry points into the assembler VM ---------------------------------
418   |//-----------------------------------------------------------------------
419   |
420   |->vm_resume:                         // Setup C frame and resume thread.
421   |  // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
422   |  saveregs
423   |  mov L, CARG1
424   |    ldr DISPATCH, L:CARG1->glref     // Setup pointer to dispatch table.
425   |  mov BASE, CARG2
426   |    add DISPATCH, DISPATCH, #GG_G2DISP
427   |   str L, SAVE_L
428   |  mov PC, #FRAME_CP
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
435   |  cmp CARG1, #0
436   |    str CARG2, L->cframe
437   |  beq >3
438   |
439   |  // Resume after yield (like a return).
440   |  str L, [DISPATCH, #DISPATCH_GL(cur_L)]
441   |  mov RA, BASE
442   |   ldr BASE, L->base
443   |   ldr CARG1, L->top
444   |    mov MASKR8, #255
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
450   |   add RC, RC, #8
451   |  ands CARG1, PC, #FRAME_TYPE
452   |     st_vmstate CARG2
453   |   str RC, SAVE_MULTRES
454   |  beq ->BC_RET_Z
455   |  b ->vm_return
456   |
457   |->vm_pcall:                          // Setup protected C frame and enter VM.
458   |  // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
459   |  saveregs
460   |  mov PC, #FRAME_CP
461   |  str CARG4, SAVE_ERRF
462   |  b >1
463   |
464   |->vm_call:                           // Setup C frame and enter VM.
465   |  // (lua_State *L, TValue *base, int nres1)
466   |  saveregs
467   |  mov PC, #FRAME_C
468   |
469   |1:  // Entry point for vm_pcall above (PC = ftype).
470   |  ldr RC, L:CARG1->cframe
471   |   str CARG3, SAVE_NRES
472   |    mov L, CARG1
473   |   str CARG1, SAVE_L
474   |    ldr DISPATCH, L->glref           // Setup pointer to dispatch table.
475   |     mov BASE, CARG2
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.
480   |
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).
484   |   ldr CARG1, L->top
485   |    mov MASKR8, #255
486   |  add PC, PC, BASE
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
491   |    st_vmstate CARG2
492   |
493   |->vm_call_dispatch:
494   |  // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
495   |  ldrd CARG34, [BASE, FRAME_FUNC]
496   |  checkfunc CARG4, ->vmeta_call
497   |
498   |->vm_call_dispatch_f:
499   |  ins_call
500   |  // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC
501   |
502   |->vm_cpcall:                         // Setup protected C frame, call C.
503   |  // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
504   |  saveregs
505   |  mov L, CARG1
506   |   ldr RA, L:CARG1->stack
507   |  str CARG1, SAVE_L
508   |    ldr DISPATCH, L->glref           // Setup pointer to dispatch table.
509   |   ldr RB, L->top
510   |  str CARG1, SAVE_PC                 // Any value outside of bytecode is ok.
511   |  ldr RC, L->cframe
512   |    add DISPATCH, DISPATCH, #GG_G2DISP
513   |   sub RA, RA, RB                    // Compute -savestack(L, L->top).
514   |  mov RB, #0
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)
521   |  movs BASE, CRET1
522   |   mov PC, #FRAME_CP
523   |  bne <3                             // Else continue with the call.
524   |  b ->vm_leave_cp                    // No base? Just remove C frame.
525   |
526   |//-----------------------------------------------------------------------
527   |//-- Metamethod handling ------------------------------------------------
528   |//-----------------------------------------------------------------------
529   |
530   |//-- Continuation dispatch ----------------------------------------------
531   |
532   |->cont_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.
536   |   mov CARG4, BASE
537   |   mov BASE, RB                      // Restore caller BASE.
538   |.if FFI
539   |    cmp CARG1, #1
540   |.endif
541   |   ldr PC, [CARG4, #-12]             // Restore PC from [cont|PC].
542   |    mvn INS, #~LJ_TNIL
543   |    add CARG2, RA, RC
544   |    str INS, [CARG2, #-4]            // Ensure one valid arg.
545   |.if FFI
546   |    bls >1
547   |.endif
548   |  ldr CARG3, LFUNC:CARG3->field_pc
549   |  ldr KBASE, [CARG3, #PC2PROTO(k)]
550   |  // BASE = base, RA = resultptr, CARG4 = meta base
551   |    bx CARG1
552   |
553   |.if FFI
554   |1:
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
559   |  b ->vm_call_tail
560   |.endif
561   |
562   |->cont_cat:                          // RA = resultptr, CARG4 = meta base
563   |  ldr INS, [PC, #-4]
564   |   sub CARG2, CARG4, #16
565   |   ldrd CARG34, [RA]
566   |     str BASE, L->base
567   |  decode_RB8 RC, INS
568   |   decode_RA8 RA, INS
569   |  add CARG1, BASE, RC
570   |  subs CARG1, CARG2, CARG1
571   |   strdne CARG34, [CARG2]
572   |   movne CARG3, CARG1
573   |  bne ->BC_CAT_Z
574   |   strd CARG34, [BASE, RA]
575   |  b ->cont_nop
576   |
577   |//-- Table indexing metamethods -----------------------------------------
578   |
579   |->vmeta_tgets1:
580   |  add CARG2, BASE, RB
581   |  b >2
582   |
583   |->vmeta_tgets:
584   |  sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
585   |   mvn CARG4, #~LJ_TTAB
586   |  str TAB:RB, [CARG2]
587   |   str CARG4, [CARG2, #4]
588   |2:
589   |   mvn CARG4, #~LJ_TSTR
590   |  str STR:RC, TMPDlo
591   |   str CARG4, TMPDhi
592   |  mov CARG3, TMPDp
593   |  b >1
594   |
595   |->vmeta_tgetb:                       // RC = index
596   |  decode_RB8 RB, INS
597   |   str RC, TMPDlo
598   |   mvn CARG4, #~LJ_TISNUM
599   |  add CARG2, BASE, RB
600   |   str CARG4, TMPDhi
601   |  mov CARG3, TMPDp
602   |  b >1
603   |
604   |->vmeta_tgetv:
605   |  add CARG2, BASE, RB
606   |   add CARG3, BASE, RC
607   |1:
608   |   str BASE, L->base
609   |  mov CARG1, L
610   |   str PC, SAVE_PC
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
614   |  cmp CRET1, #0
615   |  beq >3
616   |  ldrd CARG34, [CRET1]
617   |   ins_next1
618   |   ins_next2
619   |  strd CARG34, [BASE, RA]
620   |   ins_next3
621   |
622   |3:  // Call __index metamethod.
623   |  // BASE = base, L->top = new base, stack = cont/func/t/k
624   |   rsb CARG1, BASE, #FRAME_CONT
625   |  ldr BASE, L->top
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
631   |
632   |->vmeta_tgetr:
633   |  .IOS mov RC, BASE
634   |  bl extern lj_tab_getinth           // (GCtab *t, int32_t key)
635   |  // Returns cTValue * or NULL.
636   |  .IOS mov BASE, RC
637   |  cmp CRET1, #0
638   |  ldrdne CARG12, [CRET1]
639   |  mvneq CARG2, #~LJ_TNIL
640   |  b ->BC_TGETR_Z
641   |
642   |//-----------------------------------------------------------------------
643   |
644   |->vmeta_tsets1:
645   |  add CARG2, BASE, RB
646   |  b >2
647   |
648   |->vmeta_tsets:
649   |  sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
650   |   mvn CARG4, #~LJ_TTAB
651   |  str TAB:RB, [CARG2]
652   |   str CARG4, [CARG2, #4]
653   |2:
654   |   mvn CARG4, #~LJ_TSTR
655   |  str STR:RC, TMPDlo
656   |   str CARG4, TMPDhi
657   |  mov CARG3, TMPDp
658   |  b >1
659   |
660   |->vmeta_tsetb:                       // RC = index
661   |  decode_RB8 RB, INS
662   |   str RC, TMPDlo
663   |   mvn CARG4, #~LJ_TISNUM
664   |  add CARG2, BASE, RB
665   |   str CARG4, TMPDhi
666   |  mov CARG3, TMPDp
667   |  b >1
668   |
669   |->vmeta_tsetv:
670   |  add CARG2, BASE, RB
671   |   add CARG3, BASE, RC
672   |1:
673   |   str BASE, L->base
674   |  mov CARG1, L
675   |   str PC, SAVE_PC
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
679   |  cmp CRET1, #0
680   |   ldrd CARG34, [BASE, RA]
681   |  beq >3
682   |   ins_next1
683   |  // NOBARRIER: lj_meta_tset ensures the table is not black.
684   |  strd CARG34, [CRET1]
685   |   ins_next2
686   |   ins_next3
687   |
688   |3:  // Call __newindex metamethod.
689   |  // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
690   |   rsb CARG1, BASE, #FRAME_CONT
691   |  ldr BASE, L->top
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
698   |
699   |->vmeta_tsetr:
700   |  str BASE, L->base
701   |  .IOS mov RC, BASE
702   |  mov CARG1, L
703   |  str PC, SAVE_PC
704   |  bl extern lj_tab_setinth  // (lua_State *L, GCtab *t, int32_t key)
705   |  // Returns TValue *.
706   |  .IOS mov BASE, RC
707   |  b ->BC_TSETR_Z
708   |
709   |//-- Comparison metamethods ---------------------------------------------
710   |
711   |->vmeta_comp:
712   |  mov CARG1, L
713   |   sub PC, PC, #4
714   |  mov CARG2, RA
715   |   str BASE, L->base
716   |  mov CARG3, RC
717   |   str PC, SAVE_PC
718   |  decode_OP CARG4, INS
719   |  bl extern lj_meta_comp  // (lua_State *L, TValue *o1, *o2, int op)
720   |  // Returns 0/1 or TValue * (metamethod).
721   |3:
722   |  .IOS ldr BASE, L->base
723   |  cmp CRET1, #1
724   |  bhi ->vmeta_binop
725   |4:
726   |  ldrh RB, [PC, #2]
727   |   add PC, PC, #4
728   |  add RB, PC, RB, lsl #2
729   |  subhs PC, RB, #0x20000
730   |->cont_nop:
731   |  ins_next
732   |
733   |->cont_ra:                           // RA = resultptr
734   |  ldr INS, [PC, #-4]
735   |   ldrd CARG12, [RA]
736   |  decode_RA8 CARG3, INS
737   |   strd CARG12, [BASE, CARG3]
738   |  b ->cont_nop
739   |
740   |->cont_condt:                        // RA = resultptr
741   |  ldr CARG2, [RA, #4]
742   |   mvn CARG1, #~LJ_TTRUE
743   |  cmp CARG1, CARG2                   // Branch if result is true.
744   |  b <4
745   |
746   |->cont_condf:                        // RA = resultptr
747   |  ldr CARG2, [RA, #4]
748   |  checktp CARG2, LJ_TFALSE           // Branch if result is false.
749   |  b <4
750   |
751   |->vmeta_equal:
752   |  // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
753   |  sub PC, PC, #4
754   |   str BASE, L->base
755   |   mov CARG1, L
756   |  str PC, SAVE_PC
757   |  bl extern lj_meta_equal  // (lua_State *L, GCobj *o1, *o2, int ne)
758   |  // Returns 0/1 or TValue * (metamethod).
759   |  b <3
760   |
761   |->vmeta_equal_cd:
762   |.if FFI
763   |  sub PC, PC, #4
764   |   str BASE, L->base
765   |   mov CARG1, L
766   |   mov CARG2, INS
767   |  str PC, SAVE_PC
768   |  bl extern lj_meta_equal_cd         // (lua_State *L, BCIns op)
769   |  // Returns 0/1 or TValue * (metamethod).
770   |  b <3
771   |.endif
772   |
773   |->vmeta_istype:
774   |  sub PC, PC, #4
775   |   str BASE, L->base
776   |   mov CARG1, L
777   |   lsr CARG2, RA, #3
778   |   mov CARG3, RC
779   |  str PC, SAVE_PC
780   |  bl extern lj_meta_istype  // (lua_State *L, BCReg ra, BCReg tp)
781   |  .IOS ldr BASE, L->base
782   |  b ->cont_nop
783   |
784   |//-- Arithmetic metamethods ---------------------------------------------
785   |
786   |->vmeta_arith_vn:
787   |  decode_RB8 RB, INS
788   |   decode_RC8 RC, INS
789   |  add CARG3, BASE, RB
790   |   add CARG4, KBASE, RC
791   |  b >1
792   |
793   |->vmeta_arith_nv:
794   |  decode_RB8 RB, INS
795   |   decode_RC8 RC, INS
796   |  add CARG4, BASE, RB
797   |   add CARG3, KBASE, RC
798   |  b >1
799   |
800   |->vmeta_unm:
801   |  ldr INS, [PC, #-8]
802   |   sub PC, PC, #4
803   |  add CARG3, BASE, RC
804   |  add CARG4, BASE, RC
805   |  b >1
806   |
807   |->vmeta_arith_vv:
808   |  decode_RB8 RB, INS
809   |   decode_RC8 RC, INS
810   |  add CARG3, BASE, RB
811   |   add CARG4, BASE, RC
812   |1:
813   |  decode_OP OP, INS
814   |   add CARG2, BASE, RA
815   |    str BASE, L->base
816   |   mov CARG1, L
817   |    str PC, SAVE_PC
818   |  str OP, ARG5
819   |  bl extern lj_meta_arith  // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
820   |  // Returns NULL (finished) or TValue * (metamethod).
821   |  .IOS ldr BASE, L->base
822   |  cmp CRET1, #0
823   |  beq ->cont_nop
824   |
825   |  // Call metamethod for binary op.
826   |->vmeta_binop:
827   |  // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
828   |  sub CARG2, CRET1, BASE
829   |   str PC, [CRET1, #-12]             // [cont|PC]
830   |  add PC, CARG2, #FRAME_CONT
831   |   mov BASE, CRET1
832   |    mov NARGS8:RC, #16               // 2 args for func(o1, o2).
833   |  b ->vm_call_dispatch
834   |
835   |->vmeta_len:
836   |  add CARG2, BASE, RC
837   |   str BASE, L->base
838   |  mov CARG1, L
839   |   str PC, SAVE_PC
840   |  bl extern lj_meta_len              // (lua_State *L, TValue *o)
841   |  // Returns NULL (retry) or TValue * (metamethod base).
842   |  .IOS ldr BASE, L->base
843 #if LJ_52
844   |  cmp CRET1, #0
845   |  bne ->vmeta_binop                  // Binop call for compatibility.
846   |  ldr TAB:CARG1, [BASE, RC]
847   |  b ->BC_LEN_Z
848 #else
849   |  b ->vmeta_binop                    // Binop call for compatibility.
850 #endif
851   |
852   |//-- Call metamethod ----------------------------------------------------
853   |
854   |->vmeta_call:                        // Resolve and call __call metamethod.
855   |  // RB = old base, BASE = new base, RC = nargs*8
856   |  mov CARG1, L
857   |   str RB, L->base                   // This is the callers base!
858   |  sub CARG2, BASE, #8
859   |   str PC, SAVE_PC
860   |  add CARG3, BASE, NARGS8:RC
861   |  .IOS mov RA, BASE
862   |  bl extern lj_meta_call     // (lua_State *L, TValue *func, TValue *top)
863   |  .IOS mov BASE, RA
864   |  ldr LFUNC:CARG3, [BASE, FRAME_FUNC]  // Guaranteed to be a function here.
865   |   add NARGS8:RC, NARGS8:RC, #8      // Got one more argument now.
866   |  ins_call
867   |
868   |->vmeta_callt:                       // Resolve __call for BC_CALLT.
869   |  // BASE = old base, RA = new base, RC = nargs*8
870   |  mov CARG1, L
871   |   str BASE, L->base
872   |  sub CARG2, RA, #8
873   |   str PC, SAVE_PC
874   |  add CARG3, RA, NARGS8:RC
875   |  bl extern lj_meta_call     // (lua_State *L, TValue *func, TValue *top)
876   |  .IOS ldr BASE, L->base
877   |  ldr LFUNC:CARG3, [RA, FRAME_FUNC]  // Guaranteed to be a function here.
878   |   ldr PC, [BASE, FRAME_PC]
879   |    add NARGS8:RC, NARGS8:RC, #8     // Got one more argument now.
880   |  b ->BC_CALLT2_Z
881   |
882   |//-- Argument coercion for 'for' statement ------------------------------
883   |
884   |->vmeta_for:
885   |  mov CARG1, L
886   |   str BASE, L->base
887   |  mov CARG2, RA
888   |   str PC, SAVE_PC
889   |  bl extern lj_meta_for      // (lua_State *L, TValue *base)
890   |  .IOS ldr BASE, L->base
891   |.if JIT
892   |   ldrb OP, [PC, #-4]
893   |.endif
894   |  ldr INS, [PC, #-4]
895   |.if JIT
896   |   cmp OP, #BC_JFORI
897   |.endif
898   |  decode_RA8 RA, INS
899   |  decode_RD RC, INS
900   |.if JIT
901   |   beq =>BC_JFORI
902   |.endif
903   |  b =>BC_FORI
904   |
905   |//-----------------------------------------------------------------------
906   |//-- Fast functions -----------------------------------------------------
907   |//-----------------------------------------------------------------------
908   |
909   |.macro .ffunc, name
910   |->ff_ .. name:
911   |.endmacro
912   |
913   |.macro .ffunc_1, name
914   |->ff_ .. name:
915   |  ldrd CARG12, [BASE]
916   |   cmp NARGS8:RC, #8
917   |   blo ->fff_fallback
918   |.endmacro
919   |
920   |.macro .ffunc_2, name
921   |->ff_ .. name:
922   |  ldrd CARG12, [BASE]
923   |   ldrd CARG34, [BASE, #8]
924   |    cmp NARGS8:RC, #16
925   |    blo ->fff_fallback
926   |.endmacro
927   |
928   |.macro .ffunc_n, name
929   |  .ffunc_1 name
930   |  checktp CARG2, LJ_TISNUM
931   |  bhs ->fff_fallback
932   |.endmacro
933   |
934   |.macro .ffunc_nn, name
935   |  .ffunc_2 name
936   |  checktp CARG2, LJ_TISNUM
937   |  cmnlo CARG4, #-LJ_TISNUM
938   |  bhs ->fff_fallback
939   |.endmacro
940   |
941   |.macro .ffunc_d, name
942   |  .ffunc name
943   |  ldr CARG2, [BASE, #4]
944   |   cmp NARGS8:RC, #8
945   |  vldr d0, [BASE]
946   |   blo ->fff_fallback
947   |  checktp CARG2, LJ_TISNUM
948   |  bhs ->fff_fallback
949   |.endmacro
950   |
951   |.macro .ffunc_dd, name
952   |  .ffunc name
953   |  ldr CARG2, [BASE, #4]
954   |  ldr CARG4, [BASE, #12]
955   |   cmp NARGS8:RC, #16
956   |  vldr d0, [BASE]
957   |  vldr d1, [BASE, #8]
958   |   blo ->fff_fallback
959   |  checktp CARG2, LJ_TISNUM
960   |  cmnlo CARG4, #-LJ_TISNUM
961   |  bhs ->fff_fallback
962   |.endmacro
963   |
964   |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
965   |.macro ffgccheck
966   |  ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
967   |  ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
968   |  cmp CARG1, CARG2
969   |  blge ->fff_gcstep
970   |.endmacro
971   |
972   |//-- Base library: checks -----------------------------------------------
973   |
974   |.ffunc_1 assert
975   |  checktp CARG2, LJ_TTRUE
976   |  bhi ->fff_fallback
977   |   ldr PC, [BASE, FRAME_PC]
978   |  strd CARG12, [BASE, #-8]
979   |  mov RB, BASE
980   |  subs RA, NARGS8:RC, #8
981   |   add RC, NARGS8:RC, #8             // Compute (nresults+1)*8.
982   |  beq ->fff_res                      // Done if exactly 1 argument.
983   |1:
984   |   ldrd CARG12, [RB, #8]
985   |  subs RA, RA, #8
986   |   strd CARG12, [RB], #8
987   |  bne <1
988   |  b ->fff_res
989   |
990   |.ffunc type
991   |  ldr CARG2, [BASE, #4]
992   |   cmp NARGS8:RC, #8
993   |   blo ->fff_fallback
994   |  checktp CARG2, LJ_TISNUM
995   |  mvnlo CARG2, #~LJ_TISNUM
996   |  rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
997   |  lsl CARG4, CARG4, #3
998   |  ldrd CARG12, [CFUNC:CARG3, CARG4]
999   |  b ->fff_restv
1000   |
1001   |//-- Base library: getters and setters ---------------------------------
1002   |
1003   |.ffunc_1 getmetatable
1004   |  checktp CARG2, LJ_TTAB
1005   |  cmnne CARG2, #-LJ_TUDATA
1006   |  bne >6
1007   |1:  // Field metatable must be at same offset for GCtab and GCudata!
1008   |  ldr TAB:RB, TAB:CARG1->metatable
1009   |2:
1010   |   mvn CARG2, #~LJ_TNIL
1011   |   ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
1012   |  cmp TAB:RB, #0
1013   |  beq ->fff_restv
1014   |  ldr CARG3, TAB:RB->hmask
1015   |   ldr CARG4, STR:RC->sid
1016   |    ldr NODE:INS, TAB:RB->node
1017   |  and CARG3, CARG3, CARG4            // idx = str->sid & tab->hmask
1018   |  add CARG3, CARG3, CARG3, lsl #1
1019   |    add NODE:INS, NODE:INS, CARG3, lsl #3    // node = tab->node + idx*3*8
1020   |3:  // Rearranged logic, because we expect _not_ to find the key.
1021   |  ldrd CARG34, NODE:INS->key  // STALL: early NODE:INS.
1022   |   ldrd CARG12, NODE:INS->val
1023   |    ldr NODE:INS, NODE:INS->next
1024   |  checktp CARG4, LJ_TSTR
1025   |  cmpeq CARG3, STR:RC
1026   |  beq >5
1027   |  cmp NODE:INS, #0
1028   |  bne <3
1029   |4:
1030   |  mov CARG1, RB                      // Use metatable as default result.
1031   |  mvn CARG2, #~LJ_TTAB
1032   |  b ->fff_restv
1033   |5:
1034   |  checktp CARG2, LJ_TNIL
1035   |  bne ->fff_restv
1036   |  b <4
1037   |
1038   |6:
1039   |  checktp CARG2, LJ_TISNUM
1040   |  mvnhs CARG2, CARG2
1041   |  movlo CARG2, #~LJ_TISNUM
1042   |  add CARG4, DISPATCH, CARG2, lsl #2
1043   |  ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1044   |  b <2
1045   |
1046   |.ffunc_2 setmetatable
1047   |  // Fast path: no mt for table yet and not clearing the mt.
1048   |  checktp CARG2, LJ_TTAB
1049   |   ldreq TAB:RB, TAB:CARG1->metatable
1050   |  checktpeq CARG4, LJ_TTAB
1051   |    ldrbeq CARG4, TAB:CARG1->marked
1052   |   cmpeq TAB:RB, #0
1053   |  bne ->fff_fallback
1054   |    tst CARG4, #LJ_GC_BLACK          // isblack(table)
1055   |     str TAB:CARG3, TAB:CARG1->metatable
1056   |    beq ->fff_restv
1057   |  barrierback TAB:CARG1, CARG4, CARG3
1058   |  b ->fff_restv
1059   |
1060   |.ffunc rawget
1061   |  ldrd CARG34, [BASE]
1062   |   cmp NARGS8:RC, #16
1063   |   blo ->fff_fallback
1064   |   mov CARG2, CARG3
1065   |  checktab CARG4, ->fff_fallback
1066   |   mov CARG1, L
1067   |   add CARG3, BASE, #8
1068   |  .IOS mov RA, BASE
1069   |  bl extern lj_tab_get  // (lua_State *L, GCtab *t, cTValue *key)
1070   |  // Returns cTValue *.
1071   |  .IOS mov BASE, RA
1072   |  ldrd CARG12, [CRET1]
1073   |  b ->fff_restv
1074   |
1075   |//-- Base library: conversions ------------------------------------------
1076   |
1077   |.ffunc tonumber
1078   |  // Only handles the number case inline (without a base argument).
1079   |  ldrd CARG12, [BASE]
1080   |   cmp NARGS8:RC, #8
1081   |   bne ->fff_fallback
1082   |  checktp CARG2, LJ_TISNUM
1083   |  bls ->fff_restv
1084   |  b ->fff_fallback
1085   |
1086   |.ffunc_1 tostring
1087   |  // Only handles the string or number case inline.
1088   |  checktp CARG2, LJ_TSTR
1089   |  // A __tostring method in the string base metatable is ignored.
1090   |  beq ->fff_restv
1091   |  // Handle numbers inline, unless a number base metatable is present.
1092   |  ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])]
1093   |   str BASE, L->base
1094   |  checktp CARG2, LJ_TISNUM
1095   |  cmpls CARG4, #0
1096   |   str PC, SAVE_PC                   // Redundant (but a defined value).
1097   |  bhi ->fff_fallback
1098   |  ffgccheck
1099   |  mov CARG1, L
1100   |  mov CARG2, BASE
1101   |  bl extern lj_strfmt_number         // (lua_State *L, cTValue *o)
1102   |  // Returns GCstr *.
1103   |  ldr BASE, L->base
1104   |  mvn CARG2, #~LJ_TSTR
1105   |  b ->fff_restv
1106   |
1107   |//-- Base library: iterators -------------------------------------------
1108   |
1109   |.ffunc_1 next
1110   |   mvn CARG4, #~LJ_TNIL
1111   |  checktab CARG2, ->fff_fallback
1112   |   strd CARG34, [BASE, NARGS8:RC]    // Set missing 2nd arg to nil.
1113   |   ldr PC, [BASE, FRAME_PC]
1114   |  add CARG2, BASE, #8
1115   |  sub CARG3, BASE, #8
1116   |  bl extern lj_tab_next              // (GCtab *t, cTValue *key, TValue *o)
1117   |  // Returns 1=found, 0=end, -1=error.
1118   |  .IOS ldr BASE, L->base
1119   |  cmp CRET1, #0
1120   |   mov RC, #(2+1)*8
1121   |  bgt ->fff_res                      // Found key/value.
1122   |  bmi ->fff_fallback                 // Invalid key.
1123   |  // End of traversal: return nil.
1124   |  mvn CRET2, #~LJ_TNIL
1125   |  b ->fff_restv
1126   |
1127   |.ffunc_1 pairs
1128   |  checktab CARG2, ->fff_fallback
1129 #if LJ_52
1130   |  ldr TAB:RB, TAB:CARG1->metatable
1131 #endif
1132   |   ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1133   |    ldr PC, [BASE, FRAME_PC]
1134 #if LJ_52
1135   |  cmp TAB:RB, #0
1136   |  bne ->fff_fallback
1137 #endif
1138   |  mvn CARG2, #~LJ_TNIL
1139   |    mov RC, #(3+1)*8
1140   |   strd CFUNC:CARG34, [BASE, #-8]
1141   |  str CARG2, [BASE, #12]
1142   |  b ->fff_res
1143   |
1144   |.ffunc_2 ipairs_aux
1145   |  checktp CARG2, LJ_TTAB
1146   |  checktpeq CARG4, LJ_TISNUM
1147   |  bne ->fff_fallback
1148   |  ldr RB, TAB:CARG1->asize
1149   |   ldr RC, TAB:CARG1->array
1150   |  add CARG3, CARG3, #1
1151   |    ldr PC, [BASE, FRAME_PC]
1152   |  cmp CARG3, RB
1153   |   add RC, RC, CARG3, lsl #3
1154   |  strd CARG34, [BASE, #-8]
1155   |   ldrdlo CARG12, [RC]
1156   |   mov RC, #(0+1)*8
1157   |  bhs >2                             // Not in array part?
1158   |1:
1159   |   checktp CARG2, LJ_TNIL
1160   |   movne RC, #(2+1)*8
1161   |   strdne CARG12, [BASE]
1162   |  b ->fff_res
1163   |2:  // Check for empty hash part first. Otherwise call C function.
1164   |  ldr RB, TAB:CARG1->hmask
1165   |   mov CARG2, CARG3
1166   |  cmp RB, #0
1167   |  beq ->fff_res
1168   |  .IOS mov RA, BASE
1169   |  bl extern lj_tab_getinth           // (GCtab *t, int32_t key)
1170   |  // Returns cTValue * or NULL.
1171   |  .IOS mov BASE, RA
1172   |  cmp CRET1, #0
1173   |  beq ->fff_res
1174   |  ldrd CARG12, [CRET1]
1175   |  b <1
1176   |
1177   |.ffunc_1 ipairs
1178   |  checktab CARG2, ->fff_fallback
1179 #if LJ_52
1180   |  ldr TAB:RB, TAB:CARG1->metatable
1181 #endif
1182   |   ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0]
1183   |    ldr PC, [BASE, FRAME_PC]
1184 #if LJ_52
1185   |  cmp TAB:RB, #0
1186   |  bne ->fff_fallback
1187 #endif
1188   |  mov CARG1, #0
1189   |  mvn CARG2, #~LJ_TISNUM
1190   |    mov RC, #(3+1)*8
1191   |   strd CFUNC:CARG34, [BASE, #-8]
1192   |  strd CARG12, [BASE, #8]
1193   |  b ->fff_res
1194   |
1195   |//-- Base library: catch errors ----------------------------------------
1196   |
1197   |.ffunc pcall
1198   |   ldr RB, L->maxstack
1199   |   add INS, BASE, NARGS8:RC
1200   |  ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1201   |   cmp NARGS8:RC, #8
1202   |   cmphs RB, INS
1203   |   blo ->fff_fallback
1204   |  tst RA, #HOOK_ACTIVE               // Remember active hook before pcall.
1205   |   mov RB, BASE
1206   |   add BASE, BASE, #8
1207   |  moveq PC, #8+FRAME_PCALL
1208   |  movne PC, #8+FRAME_PCALLH
1209   |   sub NARGS8:RC, NARGS8:RC, #8
1210   |  b ->vm_call_dispatch
1211   |
1212   |.ffunc_2 xpcall
1213   |   ldr RB, L->maxstack
1214   |   add INS, BASE, NARGS8:RC
1215   |  ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)]
1216   |   cmp RB, INS
1217   |   blo ->fff_fallback
1218   |  checkfunc CARG4, ->fff_fallback    // Traceback must be a function.
1219   |   mov RB, BASE
1220   |  strd CARG12, [BASE, #8]            // Swap function and traceback.
1221   |   strd CARG34, [BASE]
1222   |  tst RA, #HOOK_ACTIVE               // Remember active hook before pcall.
1223   |   add BASE, BASE, #16
1224   |  moveq PC, #16+FRAME_PCALL
1225   |  movne PC, #16+FRAME_PCALLH
1226   |   sub NARGS8:RC, NARGS8:RC, #16
1227   |  b ->vm_call_dispatch
1228   |
1229   |//-- Coroutine library --------------------------------------------------
1230   |
1231   |.macro coroutine_resume_wrap, resume
1232   |.if resume
1233   |.ffunc_1 coroutine_resume
1234   |  checktp CARG2, LJ_TTHREAD
1235   |  bne ->fff_fallback
1236   |.else
1237   |.ffunc coroutine_wrap_aux
1238   |  ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr
1239   |.endif
1240   |   ldr PC, [BASE, FRAME_PC]
1241   |     str BASE, L->base
1242   |  ldr CARG2, L:CARG1->top
1243   |   ldrb RA, L:CARG1->status
1244   |    ldr RB, L:CARG1->base
1245   |  add CARG3, CARG2, NARGS8:RC
1246   |  add CARG4, CARG2, RA
1247   |   str PC, SAVE_PC
1248   |  cmp CARG4, RB
1249   |  beq ->fff_fallback
1250   |   ldr CARG4, L:CARG1->maxstack
1251   |    ldr RB, L:CARG1->cframe
1252   |   cmp RA, #LUA_YIELD
1253   |   cmpls CARG3, CARG4
1254   |    cmpls RB, #0
1255   |    bhi ->fff_fallback
1256   |1:
1257   |.if resume
1258   |  sub CARG3, CARG3, #8               // Keep resumed thread in stack for GC.
1259   |  add BASE, BASE, #8
1260   |  sub NARGS8:RC, NARGS8:RC, #8
1261   |.endif
1262   |  str CARG3, L:CARG1->top
1263   |  str BASE, L->top
1264   |2:  // Move args to coroutine.
1265   |   ldrd CARG34, [BASE, RB]
1266   |  cmp RB, NARGS8:RC
1267   |   strdne CARG34, [CARG2, RB]
1268   |  add RB, RB, #8
1269   |  bne <2
1270   |
1271   |  mov CARG3, #0
1272   |   mov L:RA, L:CARG1
1273   |  mov CARG4, #0
1274   |  bl ->vm_resume                     // (lua_State *L, TValue *base, 0, 0)
1275   |  // Returns thread status.
1276   |4:
1277   |  ldr CARG3, L:RA->base
1278   |    mv_vmstate CARG2, INTERP
1279   |  ldr CARG4, L:RA->top
1280   |   cmp CRET1, #LUA_YIELD
1281   |  ldr BASE, L->base
1282   |    str L, [DISPATCH, #DISPATCH_GL(cur_L)]
1283   |    st_vmstate CARG2
1284   |   bhi >8
1285   |  subs RC, CARG4, CARG3
1286   |   ldr CARG1, L->maxstack
1287   |   add CARG2, BASE, RC
1288   |  beq >6                             // No results?
1289   |  cmp CARG2, CARG1
1290   |   mov RB, #0
1291   |  bhi >9                             // Need to grow stack?
1292   |
1293   |  sub CARG4, RC, #8
1294   |   str CARG3, L:RA->top              // Clear coroutine stack.
1295   |5:  // Move results from coroutine.
1296   |   ldrd CARG12, [CARG3, RB]
1297   |  cmp RB, CARG4
1298   |   strd CARG12, [BASE, RB]
1299   |  add RB, RB, #8
1300   |  bne <5
1301   |6:
1302   |.if resume
1303   |  mvn CARG3, #~LJ_TTRUE
1304   |   add RC, RC, #16
1305   |7:
1306   |  str CARG3, [BASE, #-4]             // Prepend true/false to results.
1307   |   sub RA, BASE, #8
1308   |.else
1309   |   mov RA, BASE
1310   |   add RC, RC, #8
1311   |.endif
1312   |  ands CARG1, PC, #FRAME_TYPE
1313   |   str PC, SAVE_PC
1314   |   str RC, SAVE_MULTRES
1315   |  beq ->BC_RET_Z
1316   |  b ->vm_return
1317   |
1318   |8:  // Coroutine returned with error (at co->top-1).
1319   |.if resume
1320   |  ldrd CARG12, [CARG4, #-8]!
1321   |   mvn CARG3, #~LJ_TFALSE
1322   |    mov RC, #(2+1)*8
1323   |  str CARG4, L:RA->top               // Remove error from coroutine stack.
1324   |  strd CARG12, [BASE]                // Copy error message.
1325   |  b <7
1326   |.else
1327   |  mov CARG1, L
1328   |  mov CARG2, L:RA
1329   |  bl extern lj_ffh_coroutine_wrap_err  // (lua_State *L, lua_State *co)
1330   |  // Never returns.
1331   |.endif
1332   |
1333   |9:  // Handle stack expansion on return from yield.
1334   |  mov CARG1, L
1335   |  lsr CARG2, RC, #3
1336   |  bl extern lj_state_growstack       // (lua_State *L, int n)
1337   |  mov CRET1, #0
1338   |  b <4
1339   |.endmacro
1340   |
1341   |  coroutine_resume_wrap 1            // coroutine.resume
1342   |  coroutine_resume_wrap 0            // coroutine.wrap
1343   |
1344   |.ffunc coroutine_yield
1345   |  ldr CARG1, L->cframe
1346   |   add CARG2, BASE, NARGS8:RC
1347   |   str BASE, L->base
1348   |  tst CARG1, #CFRAME_RESUME
1349   |   str CARG2, L->top
1350   |    mov CRET1, #LUA_YIELD
1351   |   mov CARG3, #0
1352   |  beq ->fff_fallback
1353   |   str CARG3, L->cframe
1354   |    strb CRET1, L->status
1355   |  b ->vm_leave_unw
1356   |
1357   |//-- Math library -------------------------------------------------------
1358   |
1359   |.macro math_round, func
1360   |  .ffunc_1 math_ .. func
1361   |  checktp CARG2, LJ_TISNUM
1362   |  beq ->fff_restv
1363   |  bhi ->fff_fallback
1364   |  // Round FP value and normalize result.
1365   |  lsl CARG3, CARG2, #1
1366   |  adds RB, CARG3, #0x00200000
1367   |  bpl >2                             // |x| < 1?
1368   |  mvn CARG4, #0x3e0
1369   |    subs RB, CARG4, RB, asr #21
1370   |  lsl CARG4, CARG2, #11
1371   |   lsl CARG3, CARG1, #11
1372   |  orr CARG4, CARG4, #0x80000000
1373   |   rsb INS, RB, #32
1374   |  orr CARG4, CARG4, CARG1, lsr #21
1375   |    bls >3                           // |x| >= 2^31?
1376   |   orr CARG3, CARG3, CARG4, lsl INS
1377   |  lsr CARG1, CARG4, RB
1378   |.if "func" == "floor"
1379   |   tst CARG3, CARG2, asr #31
1380   |   addne CARG1, CARG1, #1
1381   |.else
1382   |   bics CARG3, CARG3, CARG2, asr #31
1383   |   addsne CARG1, CARG1, #1
1384   |   ldrdvs CARG12, >9
1385   |   bvs ->fff_restv
1386   |.endif
1387   |    cmp CARG2, #0
1388   |    rsblt CARG1, CARG1, #0
1389   |1:
1390   |   mvn CARG2, #~LJ_TISNUM
1391   |  b ->fff_restv
1392   |
1393   |2:  // |x| < 1
1394   |  bcs ->fff_restv                    // |x| is not finite.
1395   |  orr CARG3, CARG3, CARG1            // ztest = abs(hi) | lo
1396   |.if "func" == "floor"
1397   |  tst CARG3, CARG2, asr #31          // return (ztest & sign) == 0 ? 0 : -1
1398   |  moveq CARG1, #0
1399   |  mvnne CARG1, #0
1400   |.else
1401   |  bics CARG3, CARG3, CARG2, asr #31  // return (ztest & ~sign) == 0 ? 0 : 1
1402   |  moveq CARG1, #0
1403   |  movne CARG1, #1
1404   |.endif
1405   |  mvn CARG2, #~LJ_TISNUM
1406   |  b ->fff_restv
1407   |
1408   |3:  // |x| >= 2^31. Check for x == -(2^31).
1409   |  cmpeq CARG4, #0x80000000
1410   |.if "func" == "floor"
1411   |  cmpeq CARG3, #0
1412   |.endif
1413   |  bne >4
1414   |  cmp CARG2, #0
1415   |  movmi CARG1, #0x80000000
1416   |  bmi <1
1417   |4:
1418   |  bl ->vm_..func.._sf
1419   |  b ->fff_restv
1420   |.endmacro
1421   |
1422   |  math_round floor
1423   |  math_round ceil
1424   |
1425   |.align 8
1426   |9:
1427   |  .long 0x00000000, 0x41e00000       // 2^31.
1428   |
1429   |.ffunc_1 math_abs
1430   |  checktp CARG2, LJ_TISNUM
1431   |  bhi ->fff_fallback
1432   |  bicne CARG2, CARG2, #0x80000000
1433   |  bne ->fff_restv
1434   |  cmp CARG1, #0
1435   |  rsbslt CARG1, CARG1, #0
1436   |  ldrdvs CARG12, <9
1437   |  // Fallthrough.
1438   |
1439   |->fff_restv:
1440   |  // CARG12 = TValue result.
1441   |  ldr PC, [BASE, FRAME_PC]
1442   |  strd CARG12, [BASE, #-8]
1443   |->fff_res1:
1444   |  // PC = return.
1445   |  mov RC, #(1+1)*8
1446   |->fff_res:
1447   |  // RC = (nresults+1)*8, PC = return.
1448   |  ands CARG1, PC, #FRAME_TYPE
1449   |  ldreq INS, [PC, #-4]
1450   |   str RC, SAVE_MULTRES
1451   |  sub RA, BASE, #8
1452   |  bne ->vm_return
1453   |  decode_RB8 RB, INS
1454   |5:
1455   |  cmp RB, RC                         // More results expected?
1456   |  bhi >6
1457   |  decode_RA8 CARG1, INS
1458   |   ins_next1
1459   |   ins_next2
1460   |  // Adjust BASE. KBASE is assumed to be set for the calling frame.
1461   |  sub BASE, RA, CARG1
1462   |   ins_next3
1463   |
1464   |6:  // Fill up results with nil.
1465   |  add CARG2, RA, RC
1466   |  mvn CARG1, #~LJ_TNIL
1467   |   add RC, RC, #8
1468   |  str CARG1, [CARG2, #-4]
1469   |  b <5
1470   |
1471   |.macro math_extern, func
1472   |.if HFABI
1473   |  .ffunc_d math_ .. func
1474   |.else
1475   |  .ffunc_n math_ .. func
1476   |.endif
1477   |  .IOS mov RA, BASE
1478   |  bl extern func
1479   |  .IOS mov BASE, RA
1480   |.if HFABI
1481   |  b ->fff_resd
1482   |.else
1483   |  b ->fff_restv
1484   |.endif
1485   |.endmacro
1486   |
1487   |.macro math_extern2, func
1488   |.if HFABI
1489   |  .ffunc_dd math_ .. func
1490   |.else
1491   |  .ffunc_nn math_ .. func
1492   |.endif
1493   |  .IOS mov RA, BASE
1494   |  bl extern func
1495   |  .IOS mov BASE, RA
1496   |.if HFABI
1497   |  b ->fff_resd
1498   |.else
1499   |  b ->fff_restv
1500   |.endif
1501   |.endmacro
1502   |
1503   |.if FPU
1504   |  .ffunc_d math_sqrt
1505   |  vsqrt.f64 d0, d0
1506   |->fff_resd:
1507   |  ldr PC, [BASE, FRAME_PC]
1508   |  vstr d0, [BASE, #-8]
1509   |  b ->fff_res1
1510   |.else
1511   |  math_extern sqrt
1512   |.endif
1513   |
1514   |.ffunc math_log
1515   |.if HFABI
1516   |  ldr CARG2, [BASE, #4]
1517   |   cmp NARGS8:RC, #8                 // Need exactly 1 argument.
1518   |  vldr d0, [BASE]
1519   |   bne ->fff_fallback
1520   |.else
1521   |  ldrd CARG12, [BASE]
1522   |   cmp NARGS8:RC, #8                 // Need exactly 1 argument.
1523   |   bne ->fff_fallback
1524   |.endif
1525   |  checktp CARG2, LJ_TISNUM
1526   |  bhs ->fff_fallback
1527   |  .IOS mov RA, BASE
1528   |  bl extern log
1529   |  .IOS mov BASE, RA
1530   |.if HFABI
1531   |  b ->fff_resd
1532   |.else
1533   |  b ->fff_restv
1534   |.endif
1535   |
1536   |  math_extern log10
1537   |  math_extern exp
1538   |  math_extern sin
1539   |  math_extern cos
1540   |  math_extern tan
1541   |  math_extern asin
1542   |  math_extern acos
1543   |  math_extern atan
1544   |  math_extern sinh
1545   |  math_extern cosh
1546   |  math_extern tanh
1547   |  math_extern2 pow
1548   |  math_extern2 atan2
1549   |  math_extern2 fmod
1550   |
1551   |.if HFABI
1552   |  .ffunc math_ldexp
1553   |  ldr CARG4, [BASE, #4]
1554   |  ldrd CARG12, [BASE, #8]
1555   |   cmp NARGS8:RC, #16
1556   |   blo ->fff_fallback
1557   |  vldr d0, [BASE]
1558   |  checktp CARG4, LJ_TISNUM
1559   |  bhs ->fff_fallback
1560   |  checktp CARG2, LJ_TISNUM
1561   |  bne ->fff_fallback
1562   |  .IOS mov RA, BASE
1563   |  bl extern ldexp                    // (double x, int exp)
1564   |  .IOS mov BASE, RA
1565   |  b ->fff_resd
1566   |.else
1567   |.ffunc_2 math_ldexp
1568   |  checktp CARG2, LJ_TISNUM
1569   |  bhs ->fff_fallback
1570   |  checktp CARG4, LJ_TISNUM
1571   |  bne ->fff_fallback
1572   |  .IOS mov RA, BASE
1573   |  bl extern ldexp                    // (double x, int exp)
1574   |  .IOS mov BASE, RA
1575   |  b ->fff_restv
1576   |.endif
1577   |
1578   |.if HFABI
1579   |.ffunc_d math_frexp
1580   |  mov CARG1, sp
1581   |  .IOS mov RA, BASE
1582   |  bl extern frexp
1583   |  .IOS mov BASE, RA
1584   |   ldr CARG3, [sp]
1585   |   mvn CARG4, #~LJ_TISNUM
1586   |    ldr PC, [BASE, FRAME_PC]
1587   |  vstr d0, [BASE, #-8]
1588   |    mov RC, #(2+1)*8
1589   |   strd CARG34, [BASE]
1590   |  b ->fff_res
1591   |.else
1592   |.ffunc_n math_frexp
1593   |  mov CARG3, sp
1594   |  .IOS mov RA, BASE
1595   |  bl extern frexp
1596   |  .IOS mov BASE, RA
1597   |   ldr CARG3, [sp]
1598   |   mvn CARG4, #~LJ_TISNUM
1599   |    ldr PC, [BASE, FRAME_PC]
1600   |  strd CARG12, [BASE, #-8]
1601   |    mov RC, #(2+1)*8
1602   |   strd CARG34, [BASE]
1603   |  b ->fff_res
1604   |.endif
1605   |
1606   |.if HFABI
1607   |.ffunc_d math_modf
1608   |  sub CARG1, BASE, #8
1609   |   ldr PC, [BASE, FRAME_PC]
1610   |  .IOS mov RA, BASE
1611   |  bl extern modf
1612   |  .IOS mov BASE, RA
1613   |   mov RC, #(2+1)*8
1614   |  vstr d0, [BASE]
1615   |  b ->fff_res
1616   |.else
1617   |.ffunc_n math_modf
1618   |  sub CARG3, BASE, #8
1619   |   ldr PC, [BASE, FRAME_PC]
1620   |  .IOS mov RA, BASE
1621   |  bl extern modf
1622   |  .IOS mov BASE, RA
1623   |   mov RC, #(2+1)*8
1624   |  strd CARG12, [BASE]
1625   |  b ->fff_res
1626   |.endif
1627   |
1628   |.macro math_minmax, name, cond, fcond
1629   |.if FPU
1630   |  .ffunc_1 name
1631   |   add RB, BASE, RC
1632   |  checktp CARG2, LJ_TISNUM
1633   |   add RA, BASE, #8
1634   |  bne >4
1635   |1:  // Handle integers.
1636   |  ldrd CARG34, [RA]
1637   |   cmp RA, RB
1638   |   bhs ->fff_restv
1639   |  checktp CARG4, LJ_TISNUM
1640   |  bne >3
1641   |  cmp CARG1, CARG3
1642   |   add RA, RA, #8
1643   |  mov..cond CARG1, CARG3
1644   |  b <1
1645   |3:  // Convert intermediate result to number and continue below.
1646   |  vmov s4, CARG1
1647   |  bhi ->fff_fallback
1648   |  vldr d1, [RA]
1649   |  vcvt.f64.s32 d0, s4
1650   |  b >6
1651   |
1652   |4:
1653   |  vldr d0, [BASE]
1654   |  bhi ->fff_fallback
1655   |5:  // Handle numbers.
1656   |  ldrd CARG34, [RA]
1657   |  vldr d1, [RA]
1658   |   cmp RA, RB
1659   |   bhs ->fff_resd
1660   |  checktp CARG4, LJ_TISNUM
1661   |  bhs >7
1662   |6:
1663   |  vcmp.f64 d0, d1
1664   |  vmrs
1665   |   add RA, RA, #8
1666   |  vmov..fcond.f64 d0, d1
1667   |  b <5
1668   |7:  // Convert integer to number and continue above.
1669   |  vmov s4, CARG3
1670   |  bhi ->fff_fallback
1671   |  vcvt.f64.s32 d1, s4
1672   |  b <6
1673   |
1674   |.else
1675   |
1676   |  .ffunc_1 name
1677   |  checktp CARG2, LJ_TISNUM
1678   |   mov RA, #8
1679   |  bne >4
1680   |1:  // Handle integers.
1681   |  ldrd CARG34, [BASE, RA]
1682   |   cmp RA, RC
1683   |   bhs ->fff_restv
1684   |  checktp CARG4, LJ_TISNUM
1685   |  bne >3
1686   |  cmp CARG1, CARG3
1687   |   add RA, RA, #8
1688   |  mov..cond CARG1, CARG3
1689   |  b <1
1690   |3:  // Convert intermediate result to number and continue below.
1691   |  bhi ->fff_fallback
1692   |  bl extern __aeabi_i2d
1693   |  ldrd CARG34, [BASE, RA]
1694   |  b >6
1695   |
1696   |4:
1697   |  bhi ->fff_fallback
1698   |5:  // Handle numbers.
1699   |  ldrd CARG34, [BASE, RA]
1700   |   cmp RA, RC
1701   |   bhs ->fff_restv
1702   |  checktp CARG4, LJ_TISNUM
1703   |  bhs >7
1704   |6:
1705   |  bl extern __aeabi_cdcmple
1706   |   add RA, RA, #8
1707   |  mov..fcond CARG1, CARG3
1708   |  mov..fcond CARG2, CARG4
1709   |  b <5
1710   |7:  // Convert integer to number and continue above.
1711   |  bhi ->fff_fallback
1712   |  strd CARG12, TMPD
1713   |  mov CARG1, CARG3
1714   |  bl extern __aeabi_i2d
1715   |  ldrd CARG34, TMPD
1716   |  b <6
1717   |.endif
1718   |.endmacro
1719   |
1720   |  math_minmax math_min, gt, pl
1721   |  math_minmax math_max, lt, le
1722   |
1723   |//-- String library -----------------------------------------------------
1724   |
1725   |.ffunc string_byte                   // Only handle the 1-arg case here.
1726   |  ldrd CARG12, [BASE]
1727   |    ldr PC, [BASE, FRAME_PC]
1728   |   cmp NARGS8:RC, #8
1729   |   checktpeq CARG2, LJ_TSTR          // Need exactly 1 argument.
1730   |   bne ->fff_fallback
1731   |  ldr CARG3, STR:CARG1->len
1732   |   ldrb CARG1, STR:CARG1[1]          // Access is always ok (NUL at end).
1733   |   mvn CARG2, #~LJ_TISNUM
1734   |  cmp CARG3, #0
1735   |  moveq RC, #(0+1)*8
1736   |  movne RC, #(1+1)*8
1737   |   strd CARG12, [BASE, #-8]
1738   |  b ->fff_res
1739   |
1740   |.ffunc string_char                   // Only handle the 1-arg case here.
1741   |  ffgccheck
1742   |  ldrd CARG12, [BASE]
1743   |    ldr PC, [BASE, FRAME_PC]
1744   |   cmp NARGS8:RC, #8                 // Need exactly 1 argument.
1745   |   checktpeq CARG2, LJ_TISNUM
1746   |   bicseq CARG4, CARG1, #255
1747   |  mov CARG3, #1
1748   |   bne ->fff_fallback
1749   |  str CARG1, TMPD
1750   |  mov CARG2, TMPDp                   // Points to stack. Little-endian.
1751   |->fff_newstr:
1752   |  // CARG2 = str, CARG3 = len.
1753   |   str BASE, L->base
1754   |  mov CARG1, L
1755   |   str PC, SAVE_PC
1756   |  bl extern lj_str_new               // (lua_State *L, char *str, size_t l)
1757   |->fff_resstr:
1758   |  // Returns GCstr *.
1759   |  ldr BASE, L->base
1760   |   mvn CARG2, #~LJ_TSTR
1761   |  b ->fff_restv
1762   |
1763   |.ffunc string_sub
1764   |  ffgccheck
1765   |  ldrd CARG12, [BASE]
1766   |   ldrd CARG34, [BASE, #16]
1767   |    cmp NARGS8:RC, #16
1768   |     mvn RB, #0
1769   |    beq >1
1770   |    blo ->fff_fallback
1771   |   checktp CARG4, LJ_TISNUM
1772   |    mov RB, CARG3
1773   |   bne ->fff_fallback
1774   |1:
1775   |  ldrd CARG34, [BASE, #8]
1776   |  checktp CARG2, LJ_TSTR
1777   |   ldreq CARG2, STR:CARG1->len
1778   |  checktpeq CARG4, LJ_TISNUM
1779   |  bne ->fff_fallback
1780   |  // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end
1781   |  add CARG4, CARG2, #1
1782   |  cmp CARG3, #0                      // if (start < 0) start += len+1
1783   |  addlt CARG3, CARG3, CARG4
1784   |  cmp CARG3, #1                      // if (start < 1) start = 1
1785   |  movlt CARG3, #1
1786   |  cmp RB, #0                         // if (end < 0) end += len+1
1787   |  addlt RB, RB, CARG4
1788   |  bic RB, RB, RB, asr #31            // if (end < 0) end = 0
1789   |  cmp RB, CARG2                      // if (end > len) end = len
1790   |   add CARG1, STR:CARG1, #sizeof(GCstr)-1
1791   |  movgt RB, CARG2
1792   |   add CARG2, CARG1, CARG3
1793   |  subs CARG3, RB, CARG3              // len = end - start
1794   |   add CARG3, CARG3, #1              // len += 1
1795   |  bge ->fff_newstr
1796   |->fff_emptystr:
1797   |  sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty)
1798   |  mvn CARG2, #~LJ_TSTR
1799   |  b ->fff_restv
1800   |
1801   |.macro ffstring_op, name
1802   |  .ffunc string_ .. name
1803   |  ffgccheck
1804   |  ldr CARG3, [BASE, #4]
1805   |   cmp NARGS8:RC, #8
1806   |  ldr STR:CARG2, [BASE]
1807   |   blo ->fff_fallback
1808   |  sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf)
1809   |  checkstr CARG3, ->fff_fallback
1810   |  ldr CARG4, SBUF:CARG1->b
1811   |   str BASE, L->base
1812   |   str PC, SAVE_PC
1813   |   str L, SBUF:CARG1->L
1814   |  str CARG4, SBUF:CARG1->w
1815   |  bl extern lj_buf_putstr_ .. name
1816   |  bl extern lj_buf_tostr
1817   |  b ->fff_resstr
1818   |.endmacro
1819   |
1820   |ffstring_op reverse
1821   |ffstring_op lower
1822   |ffstring_op upper
1823   |
1824   |//-- Bit library --------------------------------------------------------
1825   |
1826   |// FP number to bit conversion for soft-float. Clobbers r0-r3.
1827   |->vm_tobit_fb:
1828   |  bhi ->fff_fallback
1829   |->vm_tobit:
1830   |  lsl RB, CARG2, #1
1831   |  adds RB, RB, #0x00200000
1832   |  movpl CARG1, #0                    // |x| < 1?
1833   |  bxpl lr
1834   |  mvn CARG4, #0x3e0
1835   |  subs RB, CARG4, RB, asr #21
1836   |  bmi >1                             // |x| >= 2^32?
1837   |  lsl CARG4, CARG2, #11
1838   |  orr CARG4, CARG4, #0x80000000
1839   |  orr CARG4, CARG4, CARG1, lsr #21
1840   |   cmp CARG2, #0
1841   |  lsr CARG1, CARG4, RB
1842   |   rsblt CARG1, CARG1, #0
1843   |  bx lr
1844   |1:
1845   |  add RB, RB, #21
1846   |  lsr CARG4, CARG1, RB
1847   |  rsb RB, RB, #20
1848   |  lsl CARG1, CARG2, #12
1849   |   cmp CARG2, #0
1850   |  orr CARG1, CARG4, CARG1, lsl RB
1851   |   rsblt CARG1, CARG1, #0
1852   |  bx lr
1853   |
1854   |.macro .ffunc_bit, name
1855   |  .ffunc_1 bit_..name
1856   |  checktp CARG2, LJ_TISNUM
1857   |  blne ->vm_tobit_fb
1858   |.endmacro
1859   |
1860   |.ffunc_bit tobit
1861   |  mvn CARG2, #~LJ_TISNUM
1862   |  b ->fff_restv
1863   |
1864   |.macro .ffunc_bit_op, name, ins
1865   |  .ffunc_bit name
1866   |  mov CARG3, CARG1
1867   |  mov RA, #8
1868   |1:
1869   |  ldrd CARG12, [BASE, RA]
1870   |   cmp RA, NARGS8:RC
1871   |    add RA, RA, #8
1872   |   bge >2
1873   |  checktp CARG2, LJ_TISNUM
1874   |  blne ->vm_tobit_fb
1875   |  ins CARG3, CARG3, CARG1
1876   |  b <1
1877   |.endmacro
1878   |
1879   |.ffunc_bit_op band, and
1880   |.ffunc_bit_op bor, orr
1881   |.ffunc_bit_op bxor, eor
1882   |
1883   |2:
1884   |  mvn CARG4, #~LJ_TISNUM
1885   |   ldr PC, [BASE, FRAME_PC]
1886   |  strd CARG34, [BASE, #-8]
1887   |  b ->fff_res1
1888   |
1889   |.ffunc_bit bswap
1890   |  eor CARG3, CARG1, CARG1, ror #16
1891   |  bic CARG3, CARG3, #0x00ff0000
1892   |  ror CARG1, CARG1, #8
1893   |   mvn CARG2, #~LJ_TISNUM
1894   |  eor CARG1, CARG1, CARG3, lsr #8
1895   |  b ->fff_restv
1896   |
1897   |.ffunc_bit bnot
1898   |  mvn CARG1, CARG1
1899   |  mvn CARG2, #~LJ_TISNUM
1900   |  b ->fff_restv
1901   |
1902   |.macro .ffunc_bit_sh, name, ins, shmod
1903   |  .ffunc bit_..name
1904   |  ldrd CARG12, [BASE, #8]
1905   |   cmp NARGS8:RC, #16
1906   |   blo ->fff_fallback
1907   |  checktp CARG2, LJ_TISNUM
1908   |  blne ->vm_tobit_fb
1909   |.if shmod == 0
1910   |  and RA, CARG1, #31
1911   |.else
1912   |  rsb RA, CARG1, #0
1913   |.endif
1914   |  ldrd CARG12, [BASE]
1915   |  checktp CARG2, LJ_TISNUM
1916   |  blne ->vm_tobit_fb
1917   |  ins CARG1, CARG1, RA
1918   |  mvn CARG2, #~LJ_TISNUM
1919   |  b ->fff_restv
1920   |.endmacro
1921   |
1922   |.ffunc_bit_sh lshift, lsl, 0
1923   |.ffunc_bit_sh rshift, lsr, 0
1924   |.ffunc_bit_sh arshift, asr, 0
1925   |.ffunc_bit_sh rol, ror, 1
1926   |.ffunc_bit_sh ror, ror, 0
1927   |
1928   |//-----------------------------------------------------------------------
1929   |
1930   |->fff_fallback:                      // Call fast function fallback handler.
1931   |  // BASE = new base, RC = nargs*8
1932   |   ldr CARG3, [BASE, FRAME_FUNC]
1933   |  ldr CARG2, L->maxstack
1934   |  add CARG1, BASE, NARGS8:RC
1935   |    ldr PC, [BASE, FRAME_PC]         // Fallback may overwrite PC.
1936   |  str CARG1, L->top
1937   |   ldr CARG3, CFUNC:CARG3->f
1938   |    str BASE, L->base
1939   |  add CARG1, CARG1, #8*LUA_MINSTACK
1940   |    str PC, SAVE_PC                  // Redundant (but a defined value).
1941   |  cmp CARG1, CARG2
1942   |   mov CARG1, L
1943   |  bhi >5                             // Need to grow stack.
1944   |   blx CARG3                         // (lua_State *L)
1945   |  // Either throws an error, or recovers and returns -1, 0 or nresults+1.
1946   |   ldr BASE, L->base
1947   |  cmp CRET1, #0
1948   |   lsl RC, CRET1, #3
1949   |   sub RA, BASE, #8
1950   |  bgt ->fff_res                      // Returned nresults+1?
1951   |1:  // Returned 0 or -1: retry fast path.
1952   |   ldr CARG1, L->top
1953   |    ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
1954   |   sub NARGS8:RC, CARG1, BASE
1955   |  bne ->vm_call_tail                 // Returned -1?
1956   |  ins_callt                          // Returned 0: retry fast path.
1957   |
1958   |// Reconstruct previous base for vmeta_call during tailcall.
1959   |->vm_call_tail:
1960   |  ands CARG1, PC, #FRAME_TYPE
1961   |   bic CARG2, PC, #FRAME_TYPEP
1962   |  ldreq INS, [PC, #-4]
1963   |  andeq CARG2, MASKR8, INS, lsr #5   // Conditional decode_RA8.
1964   |  addeq CARG2, CARG2, #8
1965   |  sub RB, BASE, CARG2
1966   |  b ->vm_call_dispatch               // Resolve again for tailcall.
1967   |
1968   |5:  // Grow stack for fallback handler.
1969   |  mov CARG2, #LUA_MINSTACK
1970   |  bl extern lj_state_growstack       // (lua_State *L, int n)
1971   |  ldr BASE, L->base
1972   |  cmp CARG1, CARG1                   // Set zero-flag to force retry.
1973   |  b <1
1974   |
1975   |->fff_gcstep:                        // Call GC step function.
1976   |  // BASE = new base, RC = nargs*8
1977   |  mov RA, lr
1978   |   str BASE, L->base
1979   |  add CARG2, BASE, NARGS8:RC
1980   |   str PC, SAVE_PC                   // Redundant (but a defined value).
1981   |  str CARG2, L->top
1982   |  mov CARG1, L
1983   |  bl extern lj_gc_step               // (lua_State *L)
1984   |   ldr BASE, L->base
1985   |  mov lr, RA                         // Help return address predictor.
1986   |   ldr CFUNC:CARG3, [BASE, FRAME_FUNC]
1987   |  bx lr
1988   |
1989   |//-----------------------------------------------------------------------
1990   |//-- Special dispatch targets -------------------------------------------
1991   |//-----------------------------------------------------------------------
1992   |
1993   |->vm_record:                         // Dispatch target for recording phase.
1994   |.if JIT
1995   |  ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
1996   |  tst CARG1, #HOOK_VMEVENT           // No recording while in vmevent.
1997   |  bne >5
1998   |  // Decrement the hookcount for consistency, but always do the call.
1999   |   ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2000   |  tst CARG1, #HOOK_ACTIVE
2001   |  bne >1
2002   |   sub CARG2, CARG2, #1
2003   |  tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2004   |   strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2005   |  b >1
2006   |.endif
2007   |
2008   |->vm_rethook:                        // Dispatch target for return hooks.
2009   |  ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2010   |  tst CARG1, #HOOK_ACTIVE            // Hook already active?
2011   |  beq >1
2012   |5:  // Re-dispatch to static ins.
2013   |  decode_OP OP, INS
2014   |  add OP, DISPATCH, OP, lsl #2
2015   |  ldr pc, [OP, #GG_DISP2STATIC]
2016   |
2017   |->vm_inshook:                        // Dispatch target for instr/line hooks.
2018   |  ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)]
2019   |   ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2020   |  tst CARG1, #HOOK_ACTIVE            // Hook already active?
2021   |  bne <5
2022   |  tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT
2023   |  beq <5
2024   |   subs CARG2, CARG2, #1
2025   |   str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)]
2026   |   beq >1
2027   |  tst CARG1, #LUA_MASKLINE
2028   |  beq <5
2029   |1:
2030   |  mov CARG1, L
2031   |   str BASE, L->base
2032   |  mov CARG2, PC
2033   |  // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2034   |  bl extern lj_dispatch_ins          // (lua_State *L, const BCIns *pc)
2035   |3:
2036   |  ldr BASE, L->base
2037   |4:  // Re-dispatch to static ins.
2038   |  ldrb OP, [PC, #-4]
2039   |   ldr INS, [PC, #-4]
2040   |  add OP, DISPATCH, OP, lsl #2
2041   |  ldr OP, [OP, #GG_DISP2STATIC]
2042   |   decode_RA8 RA, INS
2043   |   decode_RD RC, INS
2044   |  bx OP
2045   |
2046   |->cont_hook:                         // Continue from hook yield.
2047   |  ldr CARG1, [CARG4, #-24]
2048   |   add PC, PC, #4
2049   |  str CARG1, SAVE_MULTRES            // Restore MULTRES for *M ins.
2050   |  b <4
2051   |
2052   |->vm_hotloop:                        // Hot loop counter underflow.
2053   |.if JIT
2054   |  ldr LFUNC:CARG3, [BASE, FRAME_FUNC]  // Same as curr_topL(L).
2055   |   sub CARG1, DISPATCH, #-GG_DISP2J
2056   |   str PC, SAVE_PC
2057   |  ldr CARG3, LFUNC:CARG3->field_pc
2058   |   mov CARG2, PC
2059   |   str L, [DISPATCH, #DISPATCH_J(L)]
2060   |  ldrb CARG3, [CARG3, #PC2PROTO(framesize)]
2061   |   str BASE, L->base
2062   |  add CARG3, BASE, CARG3, lsl #3
2063   |  str CARG3, L->top
2064   |  bl extern lj_trace_hot             // (jit_State *J, const BCIns *pc)
2065   |  b <3
2066   |.endif
2067   |
2068   |->vm_callhook:                       // Dispatch target for call hooks.
2069   |  mov CARG2, PC
2070   |.if JIT
2071   |  b >1
2072   |.endif
2073   |
2074   |->vm_hotcall:                        // Hot call counter underflow.
2075   |.if JIT
2076   |  orr CARG2, PC, #1
2077   |1:
2078   |.endif
2079   |  add CARG4, BASE, RC
2080   |   str PC, SAVE_PC
2081   |    mov CARG1, L
2082   |   str BASE, L->base
2083   |    sub RA, RA, BASE
2084   |  str CARG4, L->top
2085   |  bl extern lj_dispatch_call         // (lua_State *L, const BCIns *pc)
2086   |  // Returns ASMFunction.
2087   |  ldr BASE, L->base
2088   |   ldr CARG4, L->top
2089   |    mov CARG2, #0
2090   |  add RA, BASE, RA
2091   |   sub NARGS8:RC, CARG4, BASE
2092   |    str CARG2, SAVE_PC               // Invalidate for subsequent line hook.
2093   |  ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2094   |   ldr INS, [PC, #-4]
2095   |  bx CRET1
2096   |
2097   |->cont_stitch:                       // Trace stitching.
2098   |.if JIT
2099   |  // RA = resultptr, CARG4 = meta base
2100   |   ldr RB, SAVE_MULTRES
2101   |  ldr INS, [PC, #-4]
2102   |    ldr TRACE:CARG3, [CARG4, #-24]   // Save previous trace.
2103   |   subs RB, RB, #8
2104   |  decode_RA8 RC, INS                 // Call base.
2105   |   beq >2
2106   |1:  // Move results down.
2107   |  ldrd CARG12, [RA]
2108   |    add RA, RA, #8
2109   |   subs RB, RB, #8
2110   |  strd CARG12, [BASE, RC]
2111   |    add RC, RC, #8
2112   |   bne <1
2113   |2:
2114   |   decode_RA8 RA, INS
2115   |   decode_RB8 RB, INS
2116   |   add RA, RA, RB
2117   |3:
2118   |   cmp RA, RC
2119   |  mvn CARG2, #~LJ_TNIL
2120   |   bhi >9                            // More results wanted?
2121   |
2122   |  ldrh RA, TRACE:CARG3->traceno
2123   |  ldrh RC, TRACE:CARG3->link
2124   |  cmp RC, RA
2125   |  beq ->cont_nop                     // Blacklisted.
2126   |  cmp RC, #0
2127   |  bne =>BC_JLOOP                     // Jump to stitched trace.
2128   |
2129   |  // Stitch a new trace to the previous trace.
2130   |  str RA, [DISPATCH, #DISPATCH_J(exitno)]
2131   |  str L, [DISPATCH, #DISPATCH_J(L)]
2132   |  str BASE, L->base
2133   |  sub CARG1, DISPATCH, #-GG_DISP2J
2134   |  mov CARG2, PC
2135   |  bl extern lj_dispatch_stitch       // (jit_State *J, const BCIns *pc)
2136   |  ldr BASE, L->base
2137   |  b ->cont_nop
2138   |
2139   |9:  // Fill up results with nil.
2140   |  strd CARG12, [BASE, RC]
2141   |  add RC, RC, #8
2142   |  b <3
2143   |.endif
2144   |
2145   |->vm_profhook:                       // Dispatch target for profiler hook.
2146 #if LJ_HASPROFILE
2147   |  mov CARG1, L
2148   |   str BASE, L->base
2149   |  mov CARG2, PC
2150   |  bl extern lj_dispatch_profile      // (lua_State *L, const BCIns *pc)
2151   |  // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2152   |  ldr BASE, L->base
2153   |  sub PC, PC, #4
2154   |  b ->cont_nop
2155 #endif
2156   |
2157   |//-----------------------------------------------------------------------
2158   |//-- Trace exit handler -------------------------------------------------
2159   |//-----------------------------------------------------------------------
2160   |
2161   |->vm_exit_handler:
2162   |.if JIT
2163   |  sub sp, sp, #12
2164   |  push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12}
2165   |  ldr CARG1, [sp, #64]       // Load original value of lr.
2166   |   ldr DISPATCH, [lr]        // Load DISPATCH.
2167   |    add CARG3, sp, #64       // Recompute original value of sp.
2168   |   mv_vmstate CARG4, EXIT
2169   |    str CARG3, [sp, #52]     // Store sp in RID_SP
2170   |   st_vmstate CARG4
2171   |  ldr CARG2, [CARG1, #-4]!   // Get exit instruction.
2172   |   str CARG1, [sp, #56]      // Store exit pc in RID_LR and RID_PC.
2173   |   str CARG1, [sp, #60]
2174   |.if FPU
2175   |  vpush {d0-d15}
2176   |.endif
2177   |  lsl CARG2, CARG2, #8
2178   |  add CARG1, CARG1, CARG2, asr #6
2179   |   ldr CARG2, [lr, #4]       // Load exit stub group offset.
2180   |   sub CARG1, CARG1, lr
2181   |  ldr L, [DISPATCH, #DISPATCH_GL(cur_L)]
2182   |   add CARG1, CARG2, CARG1, lsr #2   // Compute exit number.
2183   |    ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
2184   |   str CARG1, [DISPATCH, #DISPATCH_J(exitno)]
2185   |   mov CARG4, #0
2186   |    str BASE, L->base
2187   |  str L, [DISPATCH, #DISPATCH_J(L)]
2188   |   str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)]
2189   |  sub CARG1, DISPATCH, #-GG_DISP2J
2190   |  mov CARG2, sp
2191   |  bl extern lj_trace_exit            // (jit_State *J, ExitState *ex)
2192   |  // Returns MULTRES (unscaled) or negated error code.
2193   |  ldr CARG2, L->cframe
2194   |   ldr BASE, L->base
2195   |  bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
2196   |  mov sp, CARG2
2197   |   ldr PC, SAVE_PC                   // Get SAVE_PC.
2198   |  str L, SAVE_L                      // Set SAVE_L (on-trace resume/yield).
2199   |  b >1
2200   |.endif
2201   |->vm_exit_interp:
2202   |  // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set.
2203   |.if JIT
2204   |  ldr L, SAVE_L
2205   |1:
2206   |  cmn CARG1, #LUA_ERRERR
2207   |  bhs >9                             // Check for error from exit.
2208   |   lsl RC, CARG1, #3
2209   |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
2210   |   str RC, SAVE_MULTRES
2211   |   mov CARG3, #0
2212   |   str BASE, L->base
2213   |  ldr CARG2, LFUNC:CARG2->field_pc
2214   |   str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)]
2215   |    mv_vmstate CARG4, INTERP
2216   |  ldr KBASE, [CARG2, #PC2PROTO(k)]
2217   |  // Modified copy of ins_next which handles function header dispatch, too.
2218   |  ldrb OP, [PC]
2219   |     mov MASKR8, #255
2220   |   ldr INS, [PC], #4
2221   |     lsl MASKR8, MASKR8, #3          // MASKR8 = 255*8.
2222   |    st_vmstate CARG4
2223   |  cmn CARG1, #17                     // Static dispatch?
2224   |  beq >5
2225   |  cmp OP, #BC_FUNCC+2                // Fast function?
2226   |  bhs >4
2227   |2:
2228   |  cmp OP, #BC_FUNCF                  // Function header?
2229   |  ldr OP, [DISPATCH, OP, lsl #2]
2230   |   decode_RA8 RA, INS
2231   |   lsrlo RC, INS, #16        // No: Decode operands A*8 and D.
2232   |   subhs RC, RC, #8
2233   |   addhs RA, RA, BASE        // Yes: RA = BASE+framesize*8, RC = nargs*8
2234   |   ldrhs CARG3, [BASE, FRAME_FUNC]
2235   |  bx OP
2236   |
2237   |4:  // Check frame below fast function.
2238   |  ldr CARG1, [BASE, FRAME_PC]
2239   |  ands CARG2, CARG1, #FRAME_TYPE
2240   |  bne <2                     // Trace stitching continuation?
2241   |  // Otherwise set KBASE for Lua function below fast function.
2242   |  ldr CARG3, [CARG1, #-4]
2243   |  decode_RA8 CARG1, CARG3
2244   |  sub CARG2, BASE, CARG1
2245   |  ldr LFUNC:CARG3, [CARG2, #-16]
2246   |  ldr CARG3, LFUNC:CARG3->field_pc
2247   |  ldr KBASE, [CARG3, #PC2PROTO(k)]
2248   |  b <2
2249   |
2250   |5:  // Dispatch to static entry of original ins replaced by BC_JLOOP.
2251   |  ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
2252   |  decode_RD RC, INS
2253   |  ldr TRACE:CARG1, [CARG1, RC, lsl #2]
2254   |  ldr INS, TRACE:CARG1->startins
2255   |  decode_OP OP, INS
2256   |   decode_RA8 RA, INS
2257   |  add OP, DISPATCH, OP, lsl #2
2258   |   decode_RD RC, INS
2259   |  ldr pc, [OP, #GG_DISP2STATIC]
2260   |
2261   |9:  // Rethrow error from the right C frame.
2262   |  rsb CARG2, CARG1, #0
2263   |  mov CARG1, L
2264   |  bl extern lj_err_trace             // (lua_State *L, int errcode)
2265   |.endif
2266   |
2267   |//-----------------------------------------------------------------------
2268   |//-- Math helper functions ----------------------------------------------
2269   |//-----------------------------------------------------------------------
2270   |
2271   |// FP value rounding. Called from JIT code.
2272   |//
2273   |// double lj_vm_floor/ceil/trunc(double x);
2274   |.macro vm_round, func, hf
2275   |.if hf == 1
2276   |  vmov CARG1, CARG2, d0
2277   |.endif
2278   |  lsl CARG3, CARG2, #1
2279   |  adds RB, CARG3, #0x00200000
2280   |  bpl >2                             // |x| < 1?
2281   |  mvn CARG4, #0x3cc
2282   |  subs RB, CARG4, RB, asr #21        // 2^0: RB = 51, 2^51: RB = 0.
2283   |  bxlo lr                            // |x| >= 2^52: done.
2284   |  mvn CARG4, #1
2285   |   bic CARG3, CARG1, CARG4, lsl RB   // ztest = lo & ~lomask
2286   |  and CARG1, CARG1, CARG4, lsl RB    // lo &= lomask
2287   |  subs RB, RB, #32
2288   |   bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask
2289   |   orrpl CARG3, CARG3, CARG4
2290   |   mvnpl CARG4, #1
2291   |  andpl CARG2, CARG2, CARG4, lsl RB  // |x| <= 2^20: hi &= himask
2292   |.if "func" == "floor"
2293   |   tst CARG3, CARG2, asr #31         // iszero = ((ztest & signmask) == 0)
2294   |.else
2295   |   bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0)
2296   |.endif
2297   |.if hf == 1
2298   |  vmoveq d0, CARG1, CARG2
2299   |.endif
2300   |  bxeq lr                            // iszero: done.
2301   |  mvn CARG4, #1
2302   |  cmp RB, #0
2303   |  lslpl CARG3, CARG4, RB
2304   |  mvnmi CARG3, #0
2305   |  add RB, RB, #32
2306   |  subs CARG1, CARG1, CARG4, lsl RB   // lo = lo-lomask
2307   |  sbc CARG2, CARG2, CARG3            // hi = hi-himask+carry
2308   |.if hf == 1
2309   |  vmov d0, CARG1, CARG2
2310   |.endif
2311   |  bx lr
2312   |
2313   |2:  // |x| < 1:
2314   |  bxcs lr                            // |x| is not finite.
2315   |  orr CARG3, CARG3, CARG1            // ztest = (2*hi) | lo
2316   |.if "func" == "floor"
2317   |  tst CARG3, CARG2, asr #31          // iszero = ((ztest & signmask) == 0)
2318   |.else
2319   |  bics CARG3, CARG3, CARG2, asr #31  // iszero = ((ztest & ~signmask) == 0)
2320   |.endif
2321   |  mov CARG1, #0                      // lo = 0
2322   |  and CARG2, CARG2, #0x80000000
2323   |  ldrne CARG4, <9                    // hi = sign(x) | (iszero ? 0.0 : 1.0)
2324   |  orrne CARG2, CARG2, CARG4
2325   |.if hf == 1
2326   |  vmov d0, CARG1, CARG2
2327   |.endif
2328   |  bx lr
2329   |.endmacro
2330   |
2331   |9:
2332   |  .long 0x3ff00000                   // hiword(+1.0)
2333   |
2334   |->vm_floor:
2335   |.if HFABI
2336   |  vm_round floor, 1
2337   |.endif
2338   |->vm_floor_sf:
2339   |  vm_round floor, 0
2340   |
2341   |->vm_ceil:
2342   |.if HFABI
2343   |  vm_round ceil, 1
2344   |.endif
2345   |->vm_ceil_sf:
2346   |  vm_round ceil, 0
2347   |
2348   |.macro vm_trunc, hf
2349   |.if JIT
2350   |.if hf == 1
2351   |  vmov CARG1, CARG2, d0
2352   |.endif
2353   |  lsl CARG3, CARG2, #1
2354   |  adds RB, CARG3, #0x00200000
2355   |  andpl CARG2, CARG2, #0x80000000    // |x| < 1? hi = sign(x), lo = 0.
2356   |  movpl CARG1, #0
2357   |.if hf == 1
2358   |  vmovpl d0, CARG1, CARG2
2359   |.endif
2360   |  bxpl lr
2361   |  mvn CARG4, #0x3cc
2362   |  subs RB, CARG4, RB, asr #21        // 2^0: RB = 51, 2^51: RB = 0.
2363   |  bxlo lr                            // |x| >= 2^52: already done.
2364   |  mvn CARG4, #1
2365   |  and CARG1, CARG1, CARG4, lsl RB    // lo &= lomask
2366   |  subs RB, RB, #32
2367   |  andpl CARG2, CARG2, CARG4, lsl RB  // |x| <= 2^20: hi &= himask
2368   |.if hf == 1
2369   |  vmov d0, CARG1, CARG2
2370   |.endif
2371   |  bx lr
2372   |.endif
2373   |.endmacro
2374   |
2375   |->vm_trunc:
2376   |.if HFABI
2377   |  vm_trunc 1
2378   |.endif
2379   |->vm_trunc_sf:
2380   |  vm_trunc 0
2381   |
2382   |  // double lj_vm_mod(double dividend, double divisor);
2383   |->vm_mod:
2384   |.if FPU
2385   |  // Special calling convention. Also, RC (r11) is not preserved.
2386   |  vdiv.f64 d0, d6, d7
2387   |   mov RC, lr
2388   |  vmov CARG1, CARG2, d0
2389   |  bl ->vm_floor_sf
2390   |  vmov d0, CARG1, CARG2
2391   |  vmul.f64 d0, d0, d7
2392   |   mov lr, RC
2393   |  vsub.f64 d6, d6, d0
2394   |  bx lr
2395   |.else
2396   |  push {r0, r1, r2, r3, r4, lr}
2397   |  bl extern __aeabi_ddiv
2398   |  bl ->vm_floor_sf
2399   |  ldrd CARG34, [sp, #8]
2400   |  bl extern __aeabi_dmul
2401   |  ldrd CARG34, [sp]
2402   |  eor CARG2, CARG2, #0x80000000
2403   |  bl extern __aeabi_dadd
2404   |  add sp, sp, #20
2405   |  pop {pc}
2406   |.endif
2407   |
2408   |  // int lj_vm_modi(int dividend, int divisor);
2409   |->vm_modi:
2410   |  ands RB, CARG1, #0x80000000
2411   |  rsbmi CARG1, CARG1, #0             // a = |dividend|
2412   |  eor RB, RB, CARG2, asr #1          // Keep signdiff and sign(divisor).
2413   |  cmp CARG2, #0
2414   |  rsbmi CARG2, CARG2, #0             // b = |divisor|
2415   |  subs CARG4, CARG2, #1
2416   |  cmpne CARG1, CARG2
2417   |  moveq CARG1, #0                    // if (b == 1 || a == b) a = 0
2418   |  tsthi CARG2, CARG4
2419   |  andeq CARG1, CARG1, CARG4          // else if ((b & (b-1)) == 0) a &= b-1
2420   |  bls >1
2421   |  // Use repeated subtraction to get the remainder.
2422   |  clz CARG3, CARG1
2423   |  clz CARG4, CARG2
2424   |  sub CARG4, CARG4, CARG3
2425   |  rsbs CARG3, CARG4, #31             // entry = (31-(clz(b)-clz(a)))*8
2426   |  addne pc, pc, CARG3, lsl #3        // Duff's device.
2427   |  nop
2428   {
2429     int i;
2430     for (i = 31; i >= 0; i--) {
2431       |  cmp CARG1, CARG2, lsl #i
2432       |  subhs CARG1, CARG1, CARG2, lsl #i
2433     }
2434   }
2435   |1:
2436   |  cmp CARG1, #0
2437   |  cmpne RB, #0
2438   |  submi CARG1, CARG1, CARG2          // if (y != 0 && signdiff) y = y - b
2439   |  eors CARG2, CARG1, RB, lsl #1
2440   |  rsbmi CARG1, CARG1, #0             // if (sign(divisor) != sign(y)) y = -y
2441   |  bx lr
2442   |
2443   |//-----------------------------------------------------------------------
2444   |//-- Miscellaneous functions --------------------------------------------
2445   |//-----------------------------------------------------------------------
2446   |
2447   |.define NEXT_TAB,            TAB:CARG1
2448   |.define NEXT_RES,            CARG1
2449   |.define NEXT_IDX,            CARG2
2450   |.define NEXT_TMP0,           CARG3
2451   |.define NEXT_TMP1,           CARG4
2452   |.define NEXT_LIM,            r12
2453   |.define NEXT_RES_PTR,        sp
2454   |.define NEXT_RES_VAL,        [sp]
2455   |.define NEXT_RES_KEY_I,      [sp, #8]
2456   |.define NEXT_RES_KEY_IT,     [sp, #12]
2457   |
2458   |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2459   |// Next idx returned in CRET2.
2460   |->vm_next:
2461   |.if JIT
2462   |  ldr NEXT_TMP0, NEXT_TAB->array
2463   |   ldr NEXT_LIM, NEXT_TAB->asize
2464   |  add NEXT_TMP0, NEXT_TMP0, NEXT_IDX, lsl #3
2465   |1:  // Traverse array part.
2466   |   subs NEXT_TMP1, NEXT_IDX, NEXT_LIM
2467   |   bhs >5
2468   |  ldr NEXT_TMP1, [NEXT_TMP0, #4]
2469   |   str NEXT_IDX, NEXT_RES_KEY_I
2470   |   add NEXT_TMP0, NEXT_TMP0, #8
2471   |   add NEXT_IDX, NEXT_IDX, #1
2472   |  checktp NEXT_TMP1, LJ_TNIL
2473   |  beq <1                             // Skip holes in array part.
2474   |  ldr NEXT_TMP0, [NEXT_TMP0, #-8]
2475   |   mov NEXT_RES, NEXT_RES_PTR
2476   |  strd NEXT_TMP0, NEXT_RES_VAL       // Stores NEXT_TMP1, too.
2477   |  mvn NEXT_TMP0, #~LJ_TISNUM
2478   |  str NEXT_TMP0, NEXT_RES_KEY_IT
2479   |  bx lr
2480   |
2481   |5:  // Traverse hash part.
2482   |  ldr NEXT_TMP0, NEXT_TAB->hmask
2483   |   ldr NODE:NEXT_RES, NEXT_TAB->node
2484   |   add NEXT_TMP1, NEXT_TMP1, NEXT_TMP1, lsl #1
2485   |  add NEXT_LIM, NEXT_LIM, NEXT_TMP0
2486   |   add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP1, lsl #3
2487   |6:
2488   |  cmp NEXT_IDX, NEXT_LIM
2489   |  bhi >9
2490   |  ldr NEXT_TMP1, NODE:NEXT_RES->val.it
2491   |  checktp NEXT_TMP1, LJ_TNIL
2492   |   add NEXT_IDX, NEXT_IDX, #1
2493   |  bxne lr
2494   |  // Skip holes in hash part.
2495   |  add NEXT_RES, NEXT_RES, #sizeof(Node)
2496   |  b <6
2497   |
2498   |9:  // End of iteration. Set the key to nil (not the value).
2499   |  mvn NEXT_TMP0, #0
2500   |   mov NEXT_RES, NEXT_RES_PTR
2501   |  str NEXT_TMP0, NEXT_RES_KEY_IT
2502   |  bx lr
2503   |.endif
2504   |
2505   |//-----------------------------------------------------------------------
2506   |//-- FFI helper functions -----------------------------------------------
2507   |//-----------------------------------------------------------------------
2508   |
2509   |// Handler for callback functions.
2510   |// Saveregs already performed. Callback slot number in [sp], g in r12.
2511   |->vm_ffi_callback:
2512   |.if FFI
2513   |.type CTSTATE, CTState, PC
2514   |  ldr CTSTATE, GL:r12->ctype_state
2515   |   add DISPATCH, r12, #GG_G2DISP
2516   |.if FPU
2517   |  str r4, SAVE_R4
2518   |  add r4, sp, CFRAME_SPACE+4+8*8
2519   |  vstmdb r4!, {d8-d15}
2520   |.endif
2521   |.if HFABI
2522   |  add r12, CTSTATE, #offsetof(CTState, cb.fpr[8])
2523   |.endif
2524   |  strd CARG34, CTSTATE->cb.gpr[2]
2525   |  strd CARG12, CTSTATE->cb.gpr[0]
2526   |.if HFABI
2527   |  vstmdb r12!, {d0-d7}
2528   |.endif
2529   |  ldr CARG4, [sp]
2530   |   add CARG3, sp, #CFRAME_SIZE
2531   |    mov CARG1, CTSTATE
2532   |  lsr CARG4, CARG4, #3
2533   |   str CARG3, CTSTATE->cb.stack
2534   |    mov CARG2, sp
2535   |  str CARG4, CTSTATE->cb.slot
2536   |  str CTSTATE, SAVE_PC               // Any value outside of bytecode is ok.
2537   |  bl extern lj_ccallback_enter       // (CTState *cts, void *cf)
2538   |  // Returns lua_State *.
2539   |  ldr BASE, L:CRET1->base
2540   |    mv_vmstate CARG2, INTERP
2541   |  ldr RC, L:CRET1->top
2542   |    mov MASKR8, #255
2543   |   ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
2544   |    mov L, CRET1
2545   |  sub RC, RC, BASE
2546   |    lsl MASKR8, MASKR8, #3           // MASKR8 = 255*8.
2547   |    st_vmstate CARG2
2548   |  ins_callt
2549   |.endif
2550   |
2551   |->cont_ffi_callback:                 // Return from FFI callback.
2552   |.if FFI
2553   |  ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)]
2554   |   str BASE, L->base
2555   |   str CARG4, L->top
2556   |  str L, CTSTATE->L
2557   |  mov CARG1, CTSTATE
2558   |  mov CARG2, RA
2559   |  bl extern lj_ccallback_leave       // (CTState *cts, TValue *o)
2560   |  ldrd CARG12, CTSTATE->cb.gpr[0]
2561   |.if HFABI
2562   |  vldr d0, CTSTATE->cb.fpr[0]
2563   |.endif
2564   |  b ->vm_leave_unw
2565   |.endif
2566   |
2567   |->vm_ffi_call:                       // Call C function via FFI.
2568   |  // Caveat: needs special frame unwinding, see below.
2569   |.if FFI
2570   |  .type CCSTATE, CCallState, r4
2571   |  push {CCSTATE, r5, r11, lr}
2572   |  mov CCSTATE, CARG1
2573   |  ldr CARG1, CCSTATE:CARG1->spadj
2574   |   ldrb CARG2, CCSTATE->nsp
2575   |    add CARG3, CCSTATE, #offsetof(CCallState, stack)
2576   |.if HFABI
2577   |  add RB, CCSTATE, #offsetof(CCallState, fpr[0])
2578   |.endif
2579   |  mov r11, sp
2580   |  sub sp, sp, CARG1                  // Readjust stack.
2581   |   subs CARG2, CARG2, #4
2582   |.if HFABI
2583   |  vldm RB, {d0-d7}
2584   |.endif
2585   |    ldr RB, CCSTATE->func
2586   |   bmi >2
2587   |1:  // Copy stack slots.
2588   |  ldr CARG4, [CARG3, CARG2]
2589   |  str CARG4, [sp, CARG2]
2590   |  subs CARG2, CARG2, #4
2591   |  bpl <1
2592   |2:
2593   |  ldrd CARG12, CCSTATE->gpr[0]
2594   |  ldrd CARG34, CCSTATE->gpr[2]
2595   |  blx RB
2596   |  mov sp, r11
2597   |.if HFABI
2598   |  add r12, CCSTATE, #offsetof(CCallState, fpr[4])
2599   |.endif
2600   |  strd CRET1, CCSTATE->gpr[0]
2601   |.if HFABI
2602   |  vstmdb r12!, {d0-d3}
2603   |.endif
2604   |  pop {CCSTATE, r5, r11, pc}
2605   |.endif
2606   |// Note: vm_ffi_call must be the last function in this object file!
2607   |
2608   |//-----------------------------------------------------------------------
2611 /* Generate the code for a single instruction. */
2612 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2614   int vk = 0;
2615   |=>defop:
2617   switch (op) {
2619   /* -- Comparison ops ---------------------------------------------------- */
2621   /* Remember: all ops branch for a true comparison, fall through otherwise. */
2623   case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2624     |  // RA = src1*8, RC = src2, JMP with RC = target
2625     |   lsl RC, RC, #3
2626     |  ldrd CARG12, [RA, BASE]!
2627     |    ldrh RB, [PC, #2]
2628     |   ldrd CARG34, [RC, BASE]!
2629     |    add PC, PC, #4
2630     |    add RB, PC, RB, lsl #2
2631     |  checktp CARG2, LJ_TISNUM
2632     |  bne >3
2633     |  checktp CARG4, LJ_TISNUM
2634     |  bne >4
2635     |  cmp CARG1, CARG3
2636     if (op == BC_ISLT) {
2637       |  sublt PC, RB, #0x20000
2638     } else if (op == BC_ISGE) {
2639       |  subge PC, RB, #0x20000
2640     } else if (op == BC_ISLE) {
2641       |  suble PC, RB, #0x20000
2642     } else {
2643       |  subgt PC, RB, #0x20000
2644     }
2645     |1:
2646     |  ins_next
2647     |
2648     |3: // CARG12 is not an integer.
2649     |.if FPU
2650     |   vldr d0, [RA]
2651     |  bhi ->vmeta_comp
2652     |  // d0 is a number.
2653     |  checktp CARG4, LJ_TISNUM
2654     |   vldr d1, [RC]
2655     |  blo >5
2656     |  bhi ->vmeta_comp
2657     |  // d0 is a number, CARG3 is an integer.
2658     |  vmov s4, CARG3
2659     |  vcvt.f64.s32 d1, s4
2660     |  b >5
2661     |4:  // CARG1 is an integer, CARG34 is not an integer.
2662     |   vldr d1, [RC]
2663     |  bhi ->vmeta_comp
2664     |  // CARG1 is an integer, d1 is a number.
2665     |  vmov s4, CARG1
2666     |  vcvt.f64.s32 d0, s4
2667     |5:  // d0 and d1 are numbers.
2668     |  vcmp.f64 d0, d1
2669     |  vmrs
2670     |  // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2671     if (op == BC_ISLT) {
2672       |  sublo PC, RB, #0x20000
2673     } else if (op == BC_ISGE) {
2674       |  subhs PC, RB, #0x20000
2675     } else if (op == BC_ISLE) {
2676       |  subls PC, RB, #0x20000
2677     } else {
2678       |  subhi PC, RB, #0x20000
2679     }
2680     |  b <1
2681     |.else
2682     |  bhi ->vmeta_comp
2683     |  // CARG12 is a number.
2684     |  checktp CARG4, LJ_TISNUM
2685     |  movlo RA, RB                     // Save RB.
2686     |  blo >5
2687     |  bhi ->vmeta_comp
2688     |  // CARG12 is a number, CARG3 is an integer.
2689     |  mov CARG1, CARG3
2690     |  mov RC, RA
2691     |  mov RA, RB                       // Save RB.
2692     |  bl extern __aeabi_i2d
2693     |  mov CARG3, CARG1
2694     |  mov CARG4, CARG2
2695     |  ldrd CARG12, [RC]                // Restore first operand.
2696     |  b >5
2697     |4:  // CARG1 is an integer, CARG34 is not an integer.
2698     |  bhi ->vmeta_comp
2699     |  // CARG1 is an integer, CARG34 is a number.
2700     |  mov RA, RB                       // Save RB.
2701     |  bl extern __aeabi_i2d
2702     |  ldrd CARG34, [RC]                // Restore second operand.
2703     |5:  // CARG12 and CARG34 are numbers.
2704     |  bl extern __aeabi_cdcmple
2705     |  // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2706     if (op == BC_ISLT) {
2707       |  sublo PC, RA, #0x20000
2708     } else if (op == BC_ISGE) {
2709       |  subhs PC, RA, #0x20000
2710     } else if (op == BC_ISLE) {
2711       |  subls PC, RA, #0x20000
2712     } else {
2713       |  subhi PC, RA, #0x20000
2714     }
2715     |  b <1
2716     |.endif
2717     break;
2719   case BC_ISEQV: case BC_ISNEV:
2720     vk = op == BC_ISEQV;
2721     |  // RA = src1*8, RC = src2, JMP with RC = target
2722     |   lsl RC, RC, #3
2723     |  ldrd CARG12, [RA, BASE]!
2724     |    ldrh RB, [PC, #2]
2725     |   ldrd CARG34, [RC, BASE]!
2726     |    add PC, PC, #4
2727     |    add RB, PC, RB, lsl #2
2728     |  checktp CARG2, LJ_TISNUM
2729     |  cmnls CARG4, #-LJ_TISNUM
2730     if (vk) {
2731       |  bls ->BC_ISEQN_Z
2732     } else {
2733       |  bls ->BC_ISNEN_Z
2734     }
2735     |  // Either or both types are not numbers.
2736     |.if FFI
2737     |  checktp CARG2, LJ_TCDATA
2738     |  checktpne CARG4, LJ_TCDATA
2739     |  beq ->vmeta_equal_cd
2740     |.endif
2741     |  cmp CARG2, CARG4                 // Compare types.
2742     |  bne >2                           // Not the same type?
2743     |  checktp CARG2, LJ_TISPRI
2744     |  bhs >1                           // Same type and primitive type?
2745     |
2746     |  // Same types and not a primitive type. Compare GCobj or pvalue.
2747     |  cmp CARG1, CARG3
2748     if (vk) {
2749       |  bne >3                         // Different GCobjs or pvalues?
2750       |1:  // Branch if same.
2751       |  sub PC, RB, #0x20000
2752       |2:  // Different.
2753       |  ins_next
2754       |3:
2755       |  checktp CARG2, LJ_TISTABUD
2756       |  bhi <2                         // Different objects and not table/ud?
2757     } else {
2758       |  beq >1                         // Same GCobjs or pvalues?
2759       |  checktp CARG2, LJ_TISTABUD
2760       |  bhi >2                         // Different objects and not table/ud?
2761     }
2762     |  // Different tables or userdatas. Need to check __eq metamethod.
2763     |  // Field metatable must be at same offset for GCtab and GCudata!
2764     |  ldr TAB:RA, TAB:CARG1->metatable
2765     |  cmp TAB:RA, #0
2766     if (vk) {
2767       |  beq <2                 // No metatable?
2768     } else {
2769       |  beq >2                 // No metatable?
2770     }
2771     |  ldrb RA, TAB:RA->nomm
2772     |   mov CARG4, #1-vk                // ne = 0 or 1.
2773     |   mov CARG2, CARG1
2774     |  tst RA, #1<<MM_eq
2775     |  beq ->vmeta_equal                // 'no __eq' flag not set?
2776     if (vk) {
2777       |  b <2
2778     } else {
2779       |2:  // Branch if different.
2780       |  sub PC, RB, #0x20000
2781       |1:  // Same.
2782       |  ins_next
2783     }
2784     break;
2786   case BC_ISEQS: case BC_ISNES:
2787     vk = op == BC_ISEQS;
2788     |  // RA = src*8, RC = str_const (~), JMP with RC = target
2789     |   mvn RC, RC
2790     |  ldrd CARG12, [BASE, RA]
2791     |    ldrh RB, [PC, #2]
2792     |   ldr STR:CARG3, [KBASE, RC, lsl #2]
2793     |    add PC, PC, #4
2794     |    add RB, PC, RB, lsl #2
2795     |  checktp CARG2, LJ_TSTR
2796     |.if FFI
2797     |  bne >7
2798     |  cmp CARG1, CARG3
2799     |.else
2800     |  cmpeq CARG1, CARG3
2801     |.endif
2802     if (vk) {
2803       |  subeq PC, RB, #0x20000
2804       |1:
2805     } else {
2806       |1:
2807       |  subne PC, RB, #0x20000
2808     }
2809     |  ins_next
2810     |
2811     |.if FFI
2812     |7:
2813     |  checktp CARG2, LJ_TCDATA
2814     |  bne <1
2815     |  b ->vmeta_equal_cd
2816     |.endif
2817     break;
2819   case BC_ISEQN: case BC_ISNEN:
2820     vk = op == BC_ISEQN;
2821     |  // RA = src*8, RC = num_const (~), JMP with RC = target
2822     |   lsl RC, RC, #3
2823     |  ldrd CARG12, [RA, BASE]!
2824     |    ldrh RB, [PC, #2]
2825     |   ldrd CARG34, [RC, KBASE]!
2826     |    add PC, PC, #4
2827     |    add RB, PC, RB, lsl #2
2828     if (vk) {
2829       |->BC_ISEQN_Z:
2830     } else {
2831       |->BC_ISNEN_Z:
2832     }
2833     |  checktp CARG2, LJ_TISNUM
2834     |  bne >3
2835     |  checktp CARG4, LJ_TISNUM
2836     |  bne >4
2837     |  cmp CARG1, CARG3
2838     if (vk) {
2839       |  subeq PC, RB, #0x20000
2840       |1:
2841     } else {
2842       |1:
2843       |  subne PC, RB, #0x20000
2844     }
2845     |2:
2846     |  ins_next
2847     |
2848     |3:  // CARG12 is not an integer.
2849     |.if FFI
2850     |  bhi >7
2851     |.else
2852     if (!vk) {
2853       |  subhi PC, RB, #0x20000
2854     }
2855     |  bhi <2
2856     |.endif
2857     |.if FPU
2858     |  checktp CARG4, LJ_TISNUM
2859     |  vmov s4, CARG3
2860     |   vldr d0, [RA]
2861     |  vldrlo d1, [RC]
2862     |  vcvths.f64.s32 d1, s4
2863     |  b >5
2864     |4:  // CARG1 is an integer, d1 is a number.
2865     |  vmov s4, CARG1
2866     |   vldr d1, [RC]
2867     |  vcvt.f64.s32 d0, s4
2868     |5:  // d0 and d1 are numbers.
2869     |  vcmp.f64 d0, d1
2870     |  vmrs
2871     if (vk) {
2872       |  subeq PC, RB, #0x20000
2873     } else {
2874       |  subne PC, RB, #0x20000
2875     }
2876     |  b <2
2877     |.else
2878     |  // CARG12 is a number.
2879     |  checktp CARG4, LJ_TISNUM
2880     |  movlo RA, RB                     // Save RB.
2881     |  blo >5
2882     |  // CARG12 is a number, CARG3 is an integer.
2883     |  mov CARG1, CARG3
2884     |  mov RC, RA
2885     |4:  // CARG1 is an integer, CARG34 is a number.
2886     |  mov RA, RB                       // Save RB.
2887     |  bl extern __aeabi_i2d
2888     |  ldrd CARG34, [RC]                // Restore other operand.
2889     |5:  // CARG12 and CARG34 are numbers.
2890     |  bl extern __aeabi_cdcmpeq
2891     if (vk) {
2892       |  subeq PC, RA, #0x20000
2893     } else {
2894       |  subne PC, RA, #0x20000
2895     }
2896     |  b <2
2897     |.endif
2898     |
2899     |.if FFI
2900     |7:
2901     |  checktp CARG2, LJ_TCDATA
2902     |  bne <1
2903     |  b ->vmeta_equal_cd
2904     |.endif
2905     break;
2907   case BC_ISEQP: case BC_ISNEP:
2908     vk = op == BC_ISEQP;
2909     |  // RA = src*8, RC = primitive_type (~), JMP with RC = target
2910     |  ldrd CARG12, [BASE, RA]
2911     |   ldrh RB, [PC, #2]
2912     |   add PC, PC, #4
2913     |  mvn RC, RC
2914     |   add RB, PC, RB, lsl #2
2915     |.if FFI
2916     |  checktp CARG2, LJ_TCDATA
2917     |  beq ->vmeta_equal_cd
2918     |.endif
2919     |  cmp CARG2, RC
2920     if (vk) {
2921       |  subeq PC, RB, #0x20000
2922     } else {
2923       |  subne PC, RB, #0x20000
2924     }
2925     |  ins_next
2926     break;
2928   /* -- Unary test and copy ops ------------------------------------------- */
2930   case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
2931     |  // RA = dst*8 or unused, RC = src, JMP with RC = target
2932     |  add RC, BASE, RC, lsl #3
2933     |   ldrh RB, [PC, #2]
2934     |  ldrd CARG12, [RC]
2935     |   add PC, PC, #4
2936     |   add RB, PC, RB, lsl #2
2937     |  checktp CARG2, LJ_TTRUE
2938     if (op == BC_ISTC || op == BC_IST) {
2939       |  subls PC, RB, #0x20000
2940       if (op == BC_ISTC) {
2941         |  strdls CARG12, [BASE, RA]
2942       }
2943     } else {
2944       |  subhi PC, RB, #0x20000
2945       if (op == BC_ISFC) {
2946         |  strdhi CARG12, [BASE, RA]
2947       }
2948     }
2949     |  ins_next
2950     break;
2952   case BC_ISTYPE:
2953     |  // RA = src*8, RC = -type
2954     |  ldrd CARG12, [BASE, RA]
2955     |   ins_next1
2956     |  cmn CARG2, RC
2957     |   ins_next2
2958     |  bne ->vmeta_istype
2959     |   ins_next3
2960     break;
2961   case BC_ISNUM:
2962     |  // RA = src*8, RC = -(TISNUM-1)
2963     |  ldrd CARG12, [BASE, RA]
2964     |   ins_next1
2965     |  checktp CARG2, LJ_TISNUM
2966     |   ins_next2
2967     |  bhs ->vmeta_istype
2968     |   ins_next3
2969     break;
2971   /* -- Unary ops --------------------------------------------------------- */
2973   case BC_MOV:
2974     |  // RA = dst*8, RC = src
2975     |  lsl RC, RC, #3
2976     |   ins_next1
2977     |  ldrd CARG12, [BASE, RC]
2978     |   ins_next2
2979     |  strd CARG12, [BASE, RA]
2980     |   ins_next3
2981     break;
2982   case BC_NOT:
2983     |  // RA = dst*8, RC = src
2984     |  add RC, BASE, RC, lsl #3
2985     |   ins_next1
2986     |  ldr CARG1, [RC, #4]
2987     |   add RA, BASE, RA
2988     |   ins_next2
2989     |  checktp CARG1, LJ_TTRUE
2990     |  mvnls CARG2, #~LJ_TFALSE
2991     |  mvnhi CARG2, #~LJ_TTRUE
2992     |  str CARG2, [RA, #4]
2993     |   ins_next3
2994     break;
2995   case BC_UNM:
2996     |  // RA = dst*8, RC = src
2997     |  lsl RC, RC, #3
2998     |  ldrd CARG12, [BASE, RC]
2999     |   ins_next1
3000     |   ins_next2
3001     |  checktp CARG2, LJ_TISNUM
3002     |  bhi ->vmeta_unm
3003     |  eorne CARG2, CARG2, #0x80000000
3004     |  bne >5
3005     |  rsbseq CARG1, CARG1, #0
3006     |  ldrdvs CARG12, >9
3007     |5:
3008     |  strd CARG12, [BASE, RA]
3009     |   ins_next3
3010     |
3011     |.align 8
3012     |9:
3013     |  .long 0x00000000, 0x41e00000     // 2^31.
3014     break;
3015   case BC_LEN:
3016     |  // RA = dst*8, RC = src
3017     |  lsl RC, RC, #3
3018     |  ldrd CARG12, [BASE, RC]
3019     |  checkstr CARG2, >2
3020     |  ldr CARG1, STR:CARG1->len
3021     |1:
3022     |  mvn CARG2, #~LJ_TISNUM
3023     |   ins_next1
3024     |   ins_next2
3025     |  strd CARG12, [BASE, RA]
3026     |   ins_next3
3027     |2:
3028     |  checktab CARG2, ->vmeta_len
3029 #if LJ_52
3030     |  ldr TAB:CARG3, TAB:CARG1->metatable
3031     |  cmp TAB:CARG3, #0
3032     |  bne >9
3033     |3:
3034 #endif
3035     |->BC_LEN_Z:
3036     |  .IOS mov RC, BASE
3037     |  bl extern lj_tab_len             // (GCtab *t)
3038     |  // Returns uint32_t (but less than 2^31).
3039     |  .IOS mov BASE, RC
3040     |  b <1
3041 #if LJ_52
3042     |9:
3043     |  ldrb CARG4, TAB:CARG3->nomm
3044     |  tst CARG4, #1<<MM_len
3045     |  bne <3                           // 'no __len' flag set: done.
3046     |  b ->vmeta_len
3047 #endif
3048     break;
3050   /* -- Binary ops -------------------------------------------------------- */
3052     |.macro ins_arithcheck, cond, ncond, target
3053     ||if (vk == 1) {
3054     |   cmn CARG4, #-LJ_TISNUM
3055     |    cmn..cond CARG2, #-LJ_TISNUM
3056     ||} else {
3057     |   cmn CARG2, #-LJ_TISNUM
3058     |    cmn..cond CARG4, #-LJ_TISNUM
3059     ||}
3060     |  b..ncond target
3061     |.endmacro
3062     |.macro ins_arithcheck_int, target
3063     |  ins_arithcheck eq, ne, target
3064     |.endmacro
3065     |.macro ins_arithcheck_num, target
3066     |  ins_arithcheck lo, hs, target
3067     |.endmacro
3068     |
3069     |.macro ins_arithpre
3070     |  decode_RB8 RB, INS
3071     |   decode_RC8 RC, INS
3072     |  // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8
3073     ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3074     ||switch (vk) {
3075     ||case 0:
3076     |   .if FPU
3077     |   ldrd CARG12, [RB, BASE]!
3078     |    ldrd CARG34, [RC, KBASE]!
3079     |   .else
3080     |   ldrd CARG12, [BASE, RB]
3081     |    ldrd CARG34, [KBASE, RC]
3082     |   .endif
3083     ||  break;
3084     ||case 1:
3085     |   .if FPU
3086     |   ldrd CARG34, [RB, BASE]!
3087     |    ldrd CARG12, [RC, KBASE]!
3088     |   .else
3089     |   ldrd CARG34, [BASE, RB]
3090     |    ldrd CARG12, [KBASE, RC]
3091     |   .endif
3092     ||  break;
3093     ||default:
3094     |   .if FPU
3095     |   ldrd CARG12, [RB, BASE]!
3096     |    ldrd CARG34, [RC, BASE]!
3097     |   .else
3098     |   ldrd CARG12, [BASE, RB]
3099     |    ldrd CARG34, [BASE, RC]
3100     |   .endif
3101     ||  break;
3102     ||}
3103     |.endmacro
3104     |
3105     |.macro ins_arithpre_fpu, reg1, reg2
3106     |.if FPU
3107     ||if (vk == 1) {
3108     |  vldr reg2, [RB]
3109     |  vldr reg1, [RC]
3110     ||} else {
3111     |  vldr reg1, [RB]
3112     |  vldr reg2, [RC]
3113     ||}
3114     |.endif
3115     |.endmacro
3116     |
3117     |.macro ins_arithpost_fpu, reg
3118     |   ins_next1
3119     |  add RA, BASE, RA
3120     |   ins_next2
3121     |  vstr reg, [RA]
3122     |   ins_next3
3123     |.endmacro
3124     |
3125     |.macro ins_arithfallback, ins
3126     ||switch (vk) {
3127     ||case 0:
3128     |   ins ->vmeta_arith_vn
3129     ||  break;
3130     ||case 1:
3131     |   ins ->vmeta_arith_nv
3132     ||  break;
3133     ||default:
3134     |   ins ->vmeta_arith_vv
3135     ||  break;
3136     ||}
3137     |.endmacro
3138     |
3139     |.macro ins_arithdn, intins, fpins, fpcall
3140     |  ins_arithpre
3141     |.if "intins" ~= "vm_modi" and not FPU
3142     |   ins_next1
3143     |.endif
3144     |  ins_arithcheck_int >5
3145     |.if "intins" == "smull"
3146     |  smull CARG1, RC, CARG3, CARG1
3147     |  cmp RC, CARG1, asr #31
3148     |  ins_arithfallback bne
3149     |.elif "intins" == "vm_modi"
3150     |  movs CARG2, CARG3
3151     |  ins_arithfallback beq
3152     |  bl ->vm_modi
3153     |  mvn CARG2, #~LJ_TISNUM
3154     |.else
3155     |  intins CARG1, CARG1, CARG3
3156     |  ins_arithfallback bvs
3157     |.endif
3158     |4:
3159     |.if "intins" == "vm_modi" or FPU
3160     |   ins_next1
3161     |.endif
3162     |   ins_next2
3163     |  strd CARG12, [BASE, RA]
3164     |   ins_next3
3165     |5:  // FP variant.
3166     |  ins_arithpre_fpu d6, d7
3167     |  ins_arithfallback ins_arithcheck_num
3168     |.if FPU
3169     |.if "intins" == "vm_modi"
3170     |  bl fpcall
3171     |.else
3172     |  fpins d6, d6, d7
3173     |.endif
3174     |  ins_arithpost_fpu d6
3175     |.else
3176     |  bl fpcall
3177     |.if "intins" ~= "vm_modi"
3178     |  ins_next1
3179     |.endif
3180     |  b <4
3181     |.endif
3182     |.endmacro
3183     |
3184     |.macro ins_arithfp, fpins, fpcall
3185     |  ins_arithpre
3186     |.if "fpins" ~= "extern" or HFABI
3187     |  ins_arithpre_fpu d0, d1
3188     |.endif
3189     |  ins_arithfallback ins_arithcheck_num
3190     |.if "fpins" == "extern"
3191     |  .IOS mov RC, BASE
3192     |  bl fpcall
3193     |  .IOS mov BASE, RC
3194     |.elif FPU
3195     |  fpins d0, d0, d1
3196     |.else
3197     |  bl fpcall
3198     |.endif
3199     |.if ("fpins" ~= "extern" or HFABI) and FPU
3200     |  ins_arithpost_fpu d0
3201     |.else
3202     |   ins_next1
3203     |   ins_next2
3204     |  strd CARG12, [BASE, RA]
3205     |   ins_next3
3206     |.endif
3207     |.endmacro
3209   case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3210     |  ins_arithdn adds, vadd.f64, extern __aeabi_dadd
3211     break;
3212   case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3213     |  ins_arithdn subs, vsub.f64, extern __aeabi_dsub
3214     break;
3215   case BC_MULVN: case BC_MULNV: case BC_MULVV:
3216     |  ins_arithdn smull, vmul.f64, extern __aeabi_dmul
3217     break;
3218   case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3219     |  ins_arithfp vdiv.f64, extern __aeabi_ddiv
3220     break;
3221   case BC_MODVN: case BC_MODNV: case BC_MODVV:
3222     |  ins_arithdn vm_modi, vm_mod, ->vm_mod
3223     break;
3224   case BC_POW:
3225     |  // NYI: (partial) integer arithmetic.
3226     |  ins_arithfp extern, extern pow
3227     break;
3229   case BC_CAT:
3230     |  decode_RB8 RC, INS
3231     |   decode_RC8 RB, INS
3232     |  // RA = dst*8, RC = src_start*8, RB = src_end*8  (note: RB/RC swapped!)
3233     |  sub CARG3, RB, RC
3234     |   str BASE, L->base
3235     |  add CARG2, BASE, RB
3236     |->BC_CAT_Z:
3237     |  // RA = dst*8, RC = src_start*8, CARG2 = top-1
3238     |  mov CARG1, L
3239     |   str PC, SAVE_PC
3240     |  lsr CARG3, CARG3, #3
3241     |  bl extern lj_meta_cat            // (lua_State *L, TValue *top, int left)
3242     |  // Returns NULL (finished) or TValue * (metamethod).
3243     |  ldr BASE, L->base
3244     |  cmp CRET1, #0
3245     |  bne ->vmeta_binop
3246     |  ldrd CARG34, [BASE, RC]
3247     |   ins_next1
3248     |   ins_next2
3249     |  strd CARG34, [BASE, RA]          // Copy result to RA.
3250     |   ins_next3
3251     break;
3253   /* -- Constant ops ------------------------------------------------------ */
3255   case BC_KSTR:
3256     |  // RA = dst*8, RC = str_const (~)
3257     |  mvn RC, RC
3258     |   ins_next1
3259     |  ldr CARG1, [KBASE, RC, lsl #2]
3260     |  mvn CARG2, #~LJ_TSTR
3261     |   ins_next2
3262     |  strd CARG12, [BASE, RA]
3263     |   ins_next3
3264     break;
3265   case BC_KCDATA:
3266     |.if FFI
3267     |  // RA = dst*8, RC = cdata_const (~)
3268     |  mvn RC, RC
3269     |   ins_next1
3270     |  ldr CARG1, [KBASE, RC, lsl #2]
3271     |  mvn CARG2, #~LJ_TCDATA
3272     |   ins_next2
3273     |  strd CARG12, [BASE, RA]
3274     |   ins_next3
3275     |.endif
3276     break;
3277   case BC_KSHORT:
3278     |  // RA = dst*8, (RC = int16_literal)
3279     |  mov CARG1, INS, asr #16                  // Refetch sign-extended reg.
3280     |  mvn CARG2, #~LJ_TISNUM
3281     |   ins_next1
3282     |   ins_next2
3283     |  strd CARG12, [BASE, RA]
3284     |   ins_next3
3285     break;
3286   case BC_KNUM:
3287     |  // RA = dst*8, RC = num_const
3288     |  lsl RC, RC, #3
3289     |   ins_next1
3290     |  ldrd CARG12, [KBASE, RC]
3291     |   ins_next2
3292     |  strd CARG12, [BASE, RA]
3293     |   ins_next3
3294     break;
3295   case BC_KPRI:
3296     |  // RA = dst*8, RC = primitive_type (~)
3297     |  add RA, BASE, RA
3298     |  mvn RC, RC
3299     |   ins_next1
3300     |   ins_next2
3301     |  str RC, [RA, #4]
3302     |   ins_next3
3303     break;
3304   case BC_KNIL:
3305     |  // RA = base*8, RC = end
3306     |  add RA, BASE, RA
3307     |   add RC, BASE, RC, lsl #3
3308     |  mvn CARG1, #~LJ_TNIL
3309     |  str CARG1, [RA, #4]
3310     |   add RA, RA, #8
3311     |1:
3312     |  str CARG1, [RA, #4]
3313     |  cmp RA, RC
3314     |   add RA, RA, #8
3315     |  blt <1
3316     |  ins_next_
3317     break;
3319   /* -- Upvalue and function ops ------------------------------------------ */
3321   case BC_UGET:
3322     |  // RA = dst*8, RC = uvnum
3323     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3324     |   lsl RC, RC, #2
3325     |   add RC, RC, #offsetof(GCfuncL, uvptr)
3326     |  ldr UPVAL:CARG2, [LFUNC:CARG2, RC]
3327     |  ldr CARG2, UPVAL:CARG2->v
3328     |  ldrd CARG34, [CARG2]
3329     |   ins_next1
3330     |   ins_next2
3331     |  strd CARG34, [BASE, RA]
3332     |   ins_next3
3333     break;
3334   case BC_USETV:
3335     |  // RA = uvnum*8, RC = src
3336     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3337     |   lsr RA, RA, #1
3338     |   add RA, RA, #offsetof(GCfuncL, uvptr)
3339     |    lsl RC, RC, #3
3340     |  ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3341     |    ldrd CARG34, [BASE, RC]
3342     |  ldrb RB, UPVAL:CARG2->marked
3343     |  ldrb RC, UPVAL:CARG2->closed
3344     |    ldr CARG2, UPVAL:CARG2->v
3345     |  tst RB, #LJ_GC_BLACK             // isblack(uv)
3346     |   add RB, CARG4, #-LJ_TISGCV
3347     |  cmpne RC, #0
3348     |   strd CARG34, [CARG2]
3349     |  bne >2                           // Upvalue is closed and black?
3350     |1:
3351     |   ins_next
3352     |
3353     |2:  // Check if new value is collectable.
3354     |  cmn RB, #-(LJ_TNUMX - LJ_TISGCV)
3355     |   ldrbhi RC, GCOBJ:CARG3->gch.marked
3356     |  bls <1                           // tvisgcv(v)
3357     |    sub CARG1, DISPATCH, #-GG_DISP2G
3358     |   tst RC, #LJ_GC_WHITES
3359     |  // Crossed a write barrier. Move the barrier forward.
3360     |.if IOS
3361     |  beq <1
3362     |  mov RC, BASE
3363     |  bl extern lj_gc_barrieruv        // (global_State *g, TValue *tv)
3364     |  mov BASE, RC
3365     |.else
3366     |  blne extern lj_gc_barrieruv      // (global_State *g, TValue *tv)
3367     |.endif
3368     |  b <1
3369     break;
3370   case BC_USETS:
3371     |  // RA = uvnum*8, RC = str_const (~)
3372     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3373     |   lsr RA, RA, #1
3374     |   add RA, RA, #offsetof(GCfuncL, uvptr)
3375     |    mvn RC, RC
3376     |  ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3377     |    ldr STR:CARG3, [KBASE, RC, lsl #2]
3378     |  ldrb RB, UPVAL:CARG2->marked
3379     |     ldrb RC, UPVAL:CARG2->closed
3380     |   ldr CARG2, UPVAL:CARG2->v
3381     |    mvn CARG4, #~LJ_TSTR
3382     |  tst RB, #LJ_GC_BLACK             // isblack(uv)
3383     |    ldrb RB, STR:CARG3->marked
3384     |   strd CARG34, [CARG2]
3385     |  bne >2
3386     |1:
3387     |   ins_next
3388     |
3389     |2:  // Check if string is white and ensure upvalue is closed.
3390     |  tst RB, #LJ_GC_WHITES            // iswhite(str)
3391     |  cmpne RC, #0
3392     |   sub CARG1, DISPATCH, #-GG_DISP2G
3393     |  // Crossed a write barrier. Move the barrier forward.
3394     |.if IOS
3395     |  beq <1
3396     |  mov RC, BASE
3397     |  bl extern lj_gc_barrieruv        // (global_State *g, TValue *tv)
3398     |  mov BASE, RC
3399     |.else
3400     |  blne extern lj_gc_barrieruv      // (global_State *g, TValue *tv)
3401     |.endif
3402     |  b <1
3403     break;
3404   case BC_USETN:
3405     |  // RA = uvnum*8, RC = num_const
3406     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3407     |   lsr RA, RA, #1
3408     |   add RA, RA, #offsetof(GCfuncL, uvptr)
3409     |    lsl RC, RC, #3
3410     |  ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3411     |    ldrd CARG34, [KBASE, RC]
3412     |  ldr CARG2, UPVAL:CARG2->v
3413     |   ins_next1
3414     |   ins_next2
3415     |  strd CARG34, [CARG2]
3416     |   ins_next3
3417     break;
3418   case BC_USETP:
3419     |  // RA = uvnum*8, RC = primitive_type (~)
3420     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3421     |   lsr RA, RA, #1
3422     |   add RA, RA, #offsetof(GCfuncL, uvptr)
3423     |  ldr UPVAL:CARG2, [LFUNC:CARG2, RA]
3424     |   mvn RC, RC
3425     |  ldr CARG2, UPVAL:CARG2->v
3426     |   ins_next1
3427     |   ins_next2
3428     |  str RC, [CARG2, #4]
3429     |   ins_next3
3430     break;
3432   case BC_UCLO:
3433     |  // RA = level*8, RC = target
3434     |  ldr CARG3, L->openupval
3435     |   add RC, PC, RC, lsl #2
3436     |   str BASE, L->base
3437     |  cmp CARG3, #0
3438     |   sub PC, RC, #0x20000
3439     |  beq >1
3440     |   mov CARG1, L
3441     |   add CARG2, BASE, RA
3442     |  bl extern lj_func_closeuv        // (lua_State *L, TValue *level)
3443     |  ldr BASE, L->base
3444     |1:
3445     |  ins_next
3446     break;
3448   case BC_FNEW:
3449     |  // RA = dst*8, RC = proto_const (~) (holding function prototype)
3450     |  mvn RC, RC
3451     |   str BASE, L->base
3452     |  ldr CARG2, [KBASE, RC, lsl #2]
3453     |   str PC, SAVE_PC
3454     |  ldr CARG3, [BASE, FRAME_FUNC]
3455     |   mov CARG1, L
3456     |  // (lua_State *L, GCproto *pt, GCfuncL *parent)
3457     |  bl extern lj_func_newL_gc
3458     |  // Returns GCfuncL *.
3459     |  ldr BASE, L->base
3460     |  mvn CARG2, #~LJ_TFUNC
3461     |   ins_next1
3462     |   ins_next2
3463     |  strd CARG12, [BASE, RA]
3464     |   ins_next3
3465     break;
3467   /* -- Table ops --------------------------------------------------------- */
3469   case BC_TNEW:
3470   case BC_TDUP:
3471     |  // RA = dst*8, RC = (hbits|asize) | tab_const (~)
3472     if (op == BC_TDUP) {
3473       |  mvn RC, RC
3474     }
3475     |  ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)]
3476     |   ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)]
3477     |    str BASE, L->base
3478     |    str PC, SAVE_PC
3479     |  cmp CARG3, CARG4
3480     |   mov CARG1, L
3481     |  bhs >5
3482     |1:
3483     if (op == BC_TNEW) {
3484       |  lsl CARG2, RC, #21
3485       |   lsr CARG3, RC, #11
3486       |  asr RC, CARG2, #21
3487       |  lsr CARG2, CARG2, #21
3488       |  cmn RC, #1
3489       |  addeq CARG2, CARG2, #2
3490       |  bl extern lj_tab_new  // (lua_State *L, int32_t asize, uint32_t hbits)
3491       |  // Returns GCtab *.
3492     } else {
3493       |  ldr CARG2, [KBASE, RC, lsl #2]
3494       |  bl extern lj_tab_dup  // (lua_State *L, Table *kt)
3495       |  // Returns GCtab *.
3496     }
3497     |  ldr BASE, L->base
3498     |  mvn CARG2, #~LJ_TTAB
3499     |   ins_next1
3500     |   ins_next2
3501     |  strd CARG12, [BASE, RA]
3502     |   ins_next3
3503     |5:
3504     |  bl extern lj_gc_step_fixtop  // (lua_State *L)
3505     |  mov CARG1, L
3506     |  b <1
3507     break;
3509   case BC_GGET:
3510     |  // RA = dst*8, RC = str_const (~)
3511   case BC_GSET:
3512     |  // RA = dst*8, RC = str_const (~)
3513     |  ldr LFUNC:CARG2, [BASE, FRAME_FUNC]
3514     |   mvn RC, RC
3515     |  ldr TAB:CARG1, LFUNC:CARG2->env
3516     |   ldr STR:RC, [KBASE, RC, lsl #2]
3517     if (op == BC_GGET) {
3518       |  b ->BC_TGETS_Z
3519     } else {
3520       |  b ->BC_TSETS_Z
3521     }
3522     break;
3524   case BC_TGETV:
3525     |  decode_RB8 RB, INS
3526     |   decode_RC8 RC, INS
3527     |  // RA = dst*8, RB = table*8, RC = key*8
3528     |  ldrd TAB:CARG12, [BASE, RB]
3529     |   ldrd CARG34, [BASE, RC]
3530     |  checktab CARG2, ->vmeta_tgetv  // STALL: load CARG12.
3531     |   checktp CARG4, LJ_TISNUM        // Integer key?
3532     |  ldreq CARG4, TAB:CARG1->array
3533     |    ldreq CARG2, TAB:CARG1->asize
3534     |   bne >9
3535     |
3536     |  add CARG4, CARG4, CARG3, lsl #3
3537     |    cmp CARG3, CARG2               // In array part?
3538     |  ldrdlo CARG34, [CARG4]
3539     |    bhs ->vmeta_tgetv
3540     |   ins_next1  // Overwrites RB!
3541     |  checktp CARG4, LJ_TNIL
3542     |  beq >5
3543     |1:
3544     |   ins_next2
3545     |  strd CARG34, [BASE, RA]
3546     |   ins_next3
3547     |
3548     |5:  // Check for __index if table value is nil.
3549     |  ldr TAB:CARG2, TAB:CARG1->metatable
3550     |  cmp TAB:CARG2, #0
3551     |  beq <1                           // No metatable: done.
3552     |  ldrb CARG2, TAB:CARG2->nomm
3553     |  tst CARG2, #1<<MM_index
3554     |  bne <1                           // 'no __index' flag set: done.
3555     |  decode_RB8 RB, INS               // Restore RB.
3556     |  b ->vmeta_tgetv
3557     |
3558     |9:
3559     |  checktp CARG4, LJ_TSTR           // String key?
3560     |   moveq STR:RC, CARG3
3561     |  beq ->BC_TGETS_Z
3562     |  b ->vmeta_tgetv
3563     break;
3564   case BC_TGETS:
3565     |  decode_RB8 RB, INS
3566     |   and RC, RC, #255
3567     |  // RA = dst*8, RB = table*8, RC = str_const (~)
3568     |  ldrd CARG12, [BASE, RB]
3569     |   mvn RC, RC
3570     |   ldr STR:RC, [KBASE, RC, lsl #2]  // STALL: early RC.
3571     |  checktab CARG2, ->vmeta_tgets1
3572     |->BC_TGETS_Z:
3573     |  // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3574     |  ldr CARG3, TAB:CARG1->hmask
3575     |   ldr CARG4, STR:RC->sid
3576     |    ldr NODE:INS, TAB:CARG1->node
3577     |     mov TAB:RB, TAB:CARG1
3578     |  and CARG3, CARG3, CARG4                  // idx = str->sid & tab->hmask
3579     |  add CARG3, CARG3, CARG3, lsl #1
3580     |    add NODE:INS, NODE:INS, CARG3, lsl #3  // node = tab->node + idx*3*8
3581     |1:
3582     |  ldrd CARG12, NODE:INS->key  // STALL: early NODE:INS.
3583     |   ldrd CARG34, NODE:INS->val
3584     |    ldr NODE:INS, NODE:INS->next
3585     |  checktp CARG2, LJ_TSTR
3586     |  cmpeq CARG1, STR:RC
3587     |  bne >4
3588     |   checktp CARG4, LJ_TNIL
3589     |   beq >5
3590     |3:
3591     |   ins_next1
3592     |   ins_next2
3593     |  strd CARG34, [BASE, RA]
3594     |   ins_next3
3595     |
3596     |4:  // Follow hash chain.
3597     |  cmp NODE:INS, #0
3598     |  bne <1
3599     |  // End of hash chain: key not found, nil result.
3600     |
3601     |5:  // Check for __index if table value is nil.
3602     |  ldr TAB:CARG1, TAB:RB->metatable
3603     |   mov CARG3, #0  // Optional clear of undef. value (during load stall).
3604     |   mvn CARG4, #~LJ_TNIL
3605     |  cmp TAB:CARG1, #0
3606     |  beq <3                           // No metatable: done.
3607     |  ldrb CARG2, TAB:CARG1->nomm
3608     |  tst CARG2, #1<<MM_index
3609     |  bne <3                           // 'no __index' flag set: done.
3610     |  b ->vmeta_tgets
3611     break;
3612   case BC_TGETB:
3613     |  decode_RB8 RB, INS
3614     |   and RC, RC, #255
3615     |  // RA = dst*8, RB = table*8, RC = index
3616     |  ldrd CARG12, [BASE, RB]
3617     |  checktab CARG2, ->vmeta_tgetb  // STALL: load CARG12.
3618     |   ldr CARG3, TAB:CARG1->asize
3619     |  ldr CARG4, TAB:CARG1->array
3620     |  lsl CARG2, RC, #3
3621     |   cmp RC, CARG3
3622     |  ldrdlo CARG34, [CARG4, CARG2]
3623     |   bhs ->vmeta_tgetb
3624     |   ins_next1  // Overwrites RB!
3625     |  checktp CARG4, LJ_TNIL
3626     |  beq >5
3627     |1:
3628     |   ins_next2
3629     |  strd CARG34, [BASE, RA]
3630     |   ins_next3
3631     |
3632     |5:  // Check for __index if table value is nil.
3633     |  ldr TAB:CARG2, TAB:CARG1->metatable
3634     |  cmp TAB:CARG2, #0
3635     |  beq <1                           // No metatable: done.
3636     |  ldrb CARG2, TAB:CARG2->nomm
3637     |  tst CARG2, #1<<MM_index
3638     |  bne <1                           // 'no __index' flag set: done.
3639     |  b ->vmeta_tgetb
3640     break;
3641   case BC_TGETR:
3642     |  decode_RB8 RB, INS
3643     |   decode_RC8 RC, INS
3644     |  // RA = dst*8, RB = table*8, RC = key*8
3645     |  ldr TAB:CARG1, [BASE, RB]
3646     |   ldr CARG2, [BASE, RC]
3647     |  ldr CARG4, TAB:CARG1->array
3648     |    ldr CARG3, TAB:CARG1->asize
3649     |  add CARG4, CARG4, CARG2, lsl #3
3650     |    cmp CARG2, CARG3               // In array part?
3651     |    bhs ->vmeta_tgetr
3652     |  ldrd CARG12, [CARG4]
3653     |->BC_TGETR_Z:
3654     |   ins_next1
3655     |   ins_next2
3656     |  strd CARG12, [BASE, RA]
3657     |   ins_next3
3658     break;
3660   case BC_TSETV:
3661     |  decode_RB8 RB, INS
3662     |   decode_RC8 RC, INS
3663     |  // RA = src*8, RB = table*8, RC = key*8
3664     |  ldrd TAB:CARG12, [BASE, RB]
3665     |   ldrd CARG34, [BASE, RC]
3666     |  checktab CARG2, ->vmeta_tsetv  // STALL: load CARG12.
3667     |   checktp CARG4, LJ_TISNUM        // Integer key?
3668     |  ldreq CARG2, TAB:CARG1->array
3669     |    ldreq CARG4, TAB:CARG1->asize
3670     |   bne >9
3671     |
3672     |  add CARG2, CARG2, CARG3, lsl #3
3673     |    cmp CARG3, CARG4               // In array part?
3674     |  ldrlo INS, [CARG2, #4]
3675     |    bhs ->vmeta_tsetv
3676     |   ins_next1  // Overwrites RB!
3677     |  checktp INS, LJ_TNIL
3678     |  ldrb INS, TAB:CARG1->marked
3679     |   ldrd CARG34, [BASE, RA]
3680     |  beq >5
3681     |1:
3682     |  tst INS, #LJ_GC_BLACK            // isblack(table)
3683     |   strd CARG34, [CARG2]
3684     |  bne >7
3685     |2:
3686     |   ins_next2
3687     |   ins_next3
3688     |
3689     |5:  // Check for __newindex if previous value is nil.
3690     |  ldr TAB:RA, TAB:CARG1->metatable
3691     |  cmp TAB:RA, #0
3692     |  beq <1                           // No metatable: done.
3693     |  ldrb RA, TAB:RA->nomm
3694     |  tst RA, #1<<MM_newindex
3695     |  bne <1                           // 'no __newindex' flag set: done.
3696     |  ldr INS, [PC, #-4]               // Restore RA and RB.
3697     |  decode_RB8 RB, INS
3698     |  decode_RA8 RA, INS
3699     |  b ->vmeta_tsetv
3700     |
3701     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3702     |  barrierback TAB:CARG1, INS, CARG3
3703     |  b <2
3704     |
3705     |9:
3706     |  checktp CARG4, LJ_TSTR           // String key?
3707     |   moveq STR:RC, CARG3
3708     |  beq ->BC_TSETS_Z
3709     |  b ->vmeta_tsetv
3710     break;
3711   case BC_TSETS:
3712     |  decode_RB8 RB, INS
3713     |   and RC, RC, #255
3714     |  // RA = src*8, RB = table*8, RC = str_const (~)
3715     |  ldrd CARG12, [BASE, RB]
3716     |   mvn RC, RC
3717     |   ldr STR:RC, [KBASE, RC, lsl #2]  // STALL: early RC.
3718     |  checktab CARG2, ->vmeta_tsets1
3719     |->BC_TSETS_Z:
3720     |  // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8
3721     |  ldr CARG3, TAB:CARG1->hmask
3722     |   ldr CARG4, STR:RC->sid
3723     |    ldr NODE:INS, TAB:CARG1->node
3724     |     mov TAB:RB, TAB:CARG1
3725     |  and CARG3, CARG3, CARG4                  // idx = str->sid & tab->hmask
3726     |  add CARG3, CARG3, CARG3, lsl #1
3727     |   mov CARG4, #0
3728     |    add NODE:INS, NODE:INS, CARG3, lsl #3  // node = tab->node + idx*3*8
3729     |   strb CARG4, TAB:RB->nomm                // Clear metamethod cache.
3730     |1:
3731     |  ldrd CARG12, NODE:INS->key
3732     |   ldr CARG4, NODE:INS->val.it
3733     |    ldr NODE:CARG3, NODE:INS->next
3734     |  checktp CARG2, LJ_TSTR
3735     |  cmpeq CARG1, STR:RC
3736     |  bne >5
3737     |  ldrb CARG2, TAB:RB->marked
3738     |   checktp CARG4, LJ_TNIL          // Key found, but nil value?
3739     |    ldrd CARG34, [BASE, RA]
3740     |   beq >4
3741     |2:
3742     |  tst CARG2, #LJ_GC_BLACK          // isblack(table)
3743     |    strd CARG34, NODE:INS->val
3744     |  bne >7
3745     |3:
3746     |   ins_next
3747     |
3748     |4:  // Check for __newindex if previous value is nil.
3749     |  ldr TAB:CARG1, TAB:RB->metatable
3750     |  cmp TAB:CARG1, #0
3751     |  beq <2                           // No metatable: done.
3752     |  ldrb CARG1, TAB:CARG1->nomm
3753     |  tst CARG1, #1<<MM_newindex
3754     |  bne <2                           // 'no __newindex' flag set: done.
3755     |  b ->vmeta_tsets
3756     |
3757     |5:  // Follow hash chain.
3758     |  movs NODE:INS, NODE:CARG3
3759     |  bne <1
3760     |  // End of hash chain: key not found, add a new one.
3761     |
3762     |  // But check for __newindex first.
3763     |  ldr TAB:CARG1, TAB:RB->metatable
3764     |   mov CARG3, TMPDp
3765     |   str PC, SAVE_PC
3766     |  cmp TAB:CARG1, #0                // No metatable: continue.
3767     |   str BASE, L->base
3768     |  ldrbne CARG2, TAB:CARG1->nomm
3769     |   mov CARG1, L
3770     |  beq >6
3771     |  tst CARG2, #1<<MM_newindex
3772     |  beq ->vmeta_tsets                // 'no __newindex' flag NOT set: check.
3773     |6:
3774     |  mvn CARG4, #~LJ_TSTR
3775     |   str STR:RC, TMPDlo
3776     |   mov CARG2, TAB:RB
3777     |  str CARG4, TMPDhi
3778     |  bl extern lj_tab_newkey          // (lua_State *L, GCtab *t, TValue *k)
3779     |  // Returns TValue *.
3780     |  ldr BASE, L->base
3781     |  ldrd CARG34, [BASE, RA]
3782     |  strd CARG34, [CRET1]
3783     |  b <3                             // No 2nd write barrier needed.
3784     |
3785     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3786     |  barrierback TAB:RB, CARG2, CARG3
3787     |  b <3
3788     break;
3789   case BC_TSETB:
3790     |  decode_RB8 RB, INS
3791     |   and RC, RC, #255
3792     |  // RA = src*8, RB = table*8, RC = index
3793     |  ldrd CARG12, [BASE, RB]
3794     |  checktab CARG2, ->vmeta_tsetb  // STALL: load CARG12.
3795     |   ldr CARG3, TAB:CARG1->asize
3796     |  ldr RB, TAB:CARG1->array
3797     |  lsl CARG2, RC, #3
3798     |   cmp RC, CARG3
3799     |  ldrdlo CARG34, [CARG2, RB]!
3800     |   bhs ->vmeta_tsetb
3801     |   ins_next1  // Overwrites RB!
3802     |  checktp CARG4, LJ_TNIL
3803     |  ldrb INS, TAB:CARG1->marked
3804     |   ldrd CARG34, [BASE, RA]
3805     |  beq >5
3806     |1:
3807     |  tst INS, #LJ_GC_BLACK            // isblack(table)
3808     |    strd CARG34, [CARG2]
3809     |  bne >7
3810     |2:
3811     |   ins_next2
3812     |   ins_next3
3813     |
3814     |5:  // Check for __newindex if previous value is nil.
3815     |  ldr TAB:RA, TAB:CARG1->metatable
3816     |  cmp TAB:RA, #0
3817     |  beq <1                           // No metatable: done.
3818     |  ldrb RA, TAB:RA->nomm
3819     |  tst RA, #1<<MM_newindex
3820     |  bne <1                           // 'no __newindex' flag set: done.
3821     |  ldr INS, [PC, #-4]               // Restore INS.
3822     |  decode_RA8 RA, INS
3823     |  b ->vmeta_tsetb
3824     |
3825     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3826     |  barrierback TAB:CARG1, INS, CARG3
3827     |  b <2
3828     break;
3829   case BC_TSETR:
3830     |  decode_RB8 RB, INS
3831     |   decode_RC8 RC, INS
3832     |  // RA = src*8, RB = table*8, RC = key*8
3833     |  ldr TAB:CARG2, [BASE, RB]
3834     |   ldr CARG3, [BASE, RC]
3835     |     ldrb INS, TAB:CARG2->marked
3836     |  ldr CARG1, TAB:CARG2->array
3837     |    ldr CARG4, TAB:CARG2->asize
3838     |     tst INS, #LJ_GC_BLACK         // isblack(table)
3839     |  add CARG1, CARG1, CARG3, lsl #3
3840     |     bne >7
3841     |2:
3842     |    cmp CARG3, CARG4               // In array part?
3843     |    bhs ->vmeta_tsetr
3844     |->BC_TSETR_Z:
3845     |  ldrd CARG34, [BASE, RA]
3846     |   ins_next1
3847     |   ins_next2
3848     |  strd CARG34, [CARG1]
3849     |   ins_next3
3850     |
3851     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3852     |  barrierback TAB:CARG2, INS, RB
3853     |  b <2
3854     break;
3856   case BC_TSETM:
3857     |  // RA = base*8 (table at base-1), RC = num_const (start index)
3858     |  add RA, BASE, RA
3859     |1:
3860     |   ldr RB, SAVE_MULTRES
3861     |  ldr TAB:CARG2, [RA, #-8]         // Guaranteed to be a table.
3862     |  ldr CARG1, [KBASE, RC, lsl #3]   // Integer constant is in lo-word.
3863     |   subs RB, RB, #8
3864     |  ldr CARG4, TAB:CARG2->asize
3865     |   beq >4                          // Nothing to copy?
3866     |  add CARG3, CARG1, RB, lsr #3
3867     |  cmp CARG3, CARG4
3868     |   ldr CARG4, TAB:CARG2->array
3869     |    add RB, RA, RB
3870     |  bhi >5
3871     |   add INS, CARG4, CARG1, lsl #3
3872     |    ldrb CARG1, TAB:CARG2->marked
3873     |3:  // Copy result slots to table.
3874     |   ldrd CARG34, [RA], #8
3875     |   strd CARG34, [INS], #8
3876     |  cmp RA, RB
3877     |  blo <3
3878     |    tst CARG1, #LJ_GC_BLACK        // isblack(table)
3879     |    bne >7
3880     |4:
3881     |  ins_next
3882     |
3883     |5:  // Need to resize array part.
3884     |   str BASE, L->base
3885     |  mov CARG1, L
3886     |   str PC, SAVE_PC
3887     |  bl extern lj_tab_reasize         // (lua_State *L, GCtab *t, int nasize)
3888     |  // Must not reallocate the stack.
3889     |  .IOS ldr BASE, L->base
3890     |  b <1
3891     |
3892     |7:  // Possible table write barrier for any value. Skip valiswhite check.
3893     |  barrierback TAB:CARG2, CARG1, CARG3
3894     |  b <4
3895     break;
3897   /* -- Calls and vararg handling ----------------------------------------- */
3899   case BC_CALLM:
3900     |  // RA = base*8, (RB = nresults+1,) RC = extra_nargs
3901     |  ldr CARG1, SAVE_MULTRES
3902     |  decode_RC8 NARGS8:RC, INS
3903     |  add NARGS8:RC, NARGS8:RC, CARG1
3904     |  b ->BC_CALL_Z
3905     break;
3906   case BC_CALL:
3907     |  decode_RC8 NARGS8:RC, INS
3908     |  // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8
3909     |->BC_CALL_Z:
3910     |  mov RB, BASE                     // Save old BASE for vmeta_call.
3911     |  ldrd CARG34, [BASE, RA]!
3912     |   sub NARGS8:RC, NARGS8:RC, #8
3913     |   add BASE, BASE, #8
3914     |  checkfunc CARG4, ->vmeta_call
3915     |  ins_call
3916     break;
3918   case BC_CALLMT:
3919     |  // RA = base*8, (RB = 0,) RC = extra_nargs
3920     |  ldr CARG1, SAVE_MULTRES
3921     |  add NARGS8:RC, CARG1, RC, lsl #3
3922     |  b ->BC_CALLT1_Z
3923     break;
3924   case BC_CALLT:
3925     |  lsl NARGS8:RC, RC, #3
3926     |  // RA = base*8, (RB = 0,) RC = (nargs+1)*8
3927     |->BC_CALLT1_Z:
3928     |  ldrd LFUNC:CARG34, [RA, BASE]!
3929     |   sub NARGS8:RC, NARGS8:RC, #8
3930     |   add RA, RA, #8
3931     |  checkfunc CARG4, ->vmeta_callt
3932     |  ldr PC, [BASE, FRAME_PC]
3933     |->BC_CALLT2_Z:
3934     |   mov RB, #0
3935     |   ldrb CARG4, LFUNC:CARG3->ffid
3936     |  tst PC, #FRAME_TYPE
3937     |  bne >7
3938     |1:
3939     |  str LFUNC:CARG3, [BASE, FRAME_FUNC]  // Copy function down, but keep PC.
3940     |  cmp NARGS8:RC, #0
3941     |  beq >3
3942     |2:
3943     |  ldrd CARG12, [RA, RB]
3944     |   add INS, RB, #8
3945     |   cmp INS, NARGS8:RC
3946     |  strd CARG12, [BASE, RB]
3947     |    mov RB, INS
3948     |   bne <2
3949     |3:
3950     |  cmp CARG4, #1                    // (> FF_C) Calling a fast function?
3951     |  bhi >5
3952     |4:
3953     |  ins_callt
3954     |
3955     |5:  // Tailcall to a fast function with a Lua frame below.
3956     |  ldr INS, [PC, #-4]
3957     |  decode_RA8 RA, INS
3958     |  sub CARG1, BASE, RA
3959     |  ldr LFUNC:CARG1, [CARG1, #-16]
3960     |  ldr CARG1, LFUNC:CARG1->field_pc
3961     |  ldr KBASE, [CARG1, #PC2PROTO(k)]
3962     |  b <4
3963     |
3964     |7:  // Tailcall from a vararg function.
3965     |  eor PC, PC, #FRAME_VARG
3966     |  tst PC, #FRAME_TYPEP             // Vararg frame below?
3967     |  movne CARG4, #0                  // Clear ffid if no Lua function below.
3968     |  bne <1
3969     |  sub BASE, BASE, PC
3970     |  ldr PC, [BASE, FRAME_PC]
3971     |  tst PC, #FRAME_TYPE
3972     |  movne CARG4, #0                  // Clear ffid if no Lua function below.
3973     |  b <1
3974     break;
3976   case BC_ITERC:
3977     |  // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
3978     |  add RA, BASE, RA
3979     |   mov RB, BASE                    // Save old BASE for vmeta_call.
3980     |  ldrd CARG34, [RA, #-16]
3981     |   ldrd CARG12, [RA, #-8]
3982     |    add BASE, RA, #8
3983     |  strd CARG34, [RA, #8]            // Copy state.
3984     |   strd CARG12, [RA, #16]          // Copy control var.
3985     |  // STALL: locked CARG34.
3986     |  ldrd LFUNC:CARG34, [RA, #-24]
3987     |    mov NARGS8:RC, #16             // Iterators get 2 arguments.
3988     |  // STALL: load CARG34.
3989     |  strd LFUNC:CARG34, [RA]          // Copy callable.
3990     |  checkfunc CARG4, ->vmeta_call
3991     |  ins_call
3992     break;
3994   case BC_ITERN:
3995     |.if JIT
3996     |  hotloop
3997     |.endif
3998     |->vm_IITERN:
3999     |  // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1))
4000     |  add RA, BASE, RA
4001     |  ldr TAB:RB, [RA, #-16]
4002     |  ldr CARG1, [RA, #-8]             // Get index from control var.
4003     |  ldr INS, TAB:RB->asize
4004     |   ldr CARG2, TAB:RB->array
4005     |    add PC, PC, #4
4006     |1:  // Traverse array part.
4007     |  subs RC, CARG1, INS
4008     |   add CARG3, CARG2, CARG1, lsl #3
4009     |  bhs >5                           // Index points after array part?
4010     |   ldrd CARG34, [CARG3]
4011     |   checktp CARG4, LJ_TNIL
4012     |   addeq CARG1, CARG1, #1          // Skip holes in array part.
4013     |   beq <1
4014     |  ldrh RC, [PC, #-2]
4015     |   mvn CARG2, #~LJ_TISNUM
4016     |    strd CARG34, [RA, #8]
4017     |  add RC, PC, RC, lsl #2
4018     |    add RB, CARG1, #1
4019     |   strd CARG12, [RA]
4020     |  sub PC, RC, #0x20000
4021     |    str RB, [RA, #-8]              // Update control var.
4022     |3:
4023     |  ins_next
4024     |
4025     |5:  // Traverse hash part.
4026     |  ldr CARG4, TAB:RB->hmask
4027     |   ldr NODE:RB, TAB:RB->node
4028     |6:
4029     |   add CARG1, RC, RC, lsl #1
4030     |  cmp RC, CARG4                    // End of iteration? Branch to ITERL+1.
4031     |   add NODE:CARG3, NODE:RB, CARG1, lsl #3  // node = tab->node + idx*3*8
4032     |  bhi <3
4033     |   ldrd CARG12, NODE:CARG3->val
4034     |   checktp CARG2, LJ_TNIL
4035     |   add RC, RC, #1
4036     |   beq <6                          // Skip holes in hash part.
4037     |  ldrh RB, [PC, #-2]
4038     |   add RC, RC, INS
4039     |    ldrd CARG34, NODE:CARG3->key
4040     |   str RC, [RA, #-8]               // Update control var.
4041     |   strd CARG12, [RA, #8]
4042     |  add RC, PC, RB, lsl #2
4043     |  sub PC, RC, #0x20000
4044     |    strd CARG34, [RA]
4045     |  b <3
4046     break;
4048   case BC_ISNEXT:
4049     |  // RA = base*8, RC = target (points to ITERN)
4050     |  add RA, BASE, RA
4051     |     add RC, PC, RC, lsl #2
4052     |  ldrd CFUNC:CARG12, [RA, #-24]
4053     |   ldr CARG3, [RA, #-12]
4054     |    ldr CARG4, [RA, #-4]
4055     |  checktp CARG2, LJ_TFUNC
4056     |  ldrbeq CARG1, CFUNC:CARG1->ffid
4057     |   checktpeq CARG3, LJ_TTAB
4058     |    checktpeq CARG4, LJ_TNIL
4059     |  cmpeq CARG1, #FF_next_N
4060     |     subeq PC, RC, #0x20000
4061     |  bne >5
4062     |   ins_next1
4063     |   ins_next2
4064     |  mov CARG1, #0
4065     |  mvn CARG2, #~LJ_KEYINDEX
4066     |  strd CARG1, [RA, #-8]            // Initialize control var.
4067     |1:
4068     |   ins_next3
4069     |5:  // Despecialize bytecode if any of the checks fail.
4070     |  mov CARG1, #BC_JMP
4071     |   mov OP, #BC_ITERC
4072     |  strb CARG1, [PC, #-4]
4073     |   sub PC, RC, #0x20000
4074     |.if JIT
4075     |   ldrb CARG1, [PC]
4076     |   cmp CARG1, #BC_ITERN
4077     |   bne >6
4078     |.endif
4079     |   strb OP, [PC]                   // Subsumes ins_next1.
4080     |   ins_next2
4081     |  b <1
4082     |.if JIT
4083     |6:  // Unpatch JLOOP.
4084     |  ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4085     |  ldrh CARG2, [PC, #2]
4086     |  ldr TRACE:CARG1, [CARG1, CARG2, lsl #2]
4087     |  // Subsumes ins_next1 and ins_next2.
4088     |  ldr INS, TRACE:CARG1->startins
4089     |  bfi INS, OP, #0, #8
4090     |  str INS, [PC], #4
4091     |  b <1
4092     |.endif
4093     break;
4095   case BC_VARG:
4096     |  decode_RB8 RB, INS
4097     |   decode_RC8 RC, INS
4098     |  // RA = base*8, RB = (nresults+1)*8, RC = numparams*8
4099     |  ldr CARG1, [BASE, FRAME_PC]
4100     |  add RC, BASE, RC
4101     |   add RA, BASE, RA
4102     |  add RC, RC, #FRAME_VARG
4103     |   add CARG4, RA, RB
4104     |  sub CARG3, BASE, #8              // CARG3 = vtop
4105     |  sub RC, RC, CARG1                // RC = vbase
4106     |  // Note: RC may now be even _above_ BASE if nargs was < numparams.
4107     |  cmp RB, #0
4108     |   sub CARG1, CARG3, RC
4109     |  beq >5                           // Copy all varargs?
4110     |   sub CARG4, CARG4, #16
4111     |1:  // Copy vararg slots to destination slots.
4112     |  cmp RC, CARG3
4113     |  ldrdlo CARG12, [RC], #8
4114     |  mvnhs CARG2, #~LJ_TNIL
4115     |   cmp RA, CARG4
4116     |  strd CARG12, [RA], #8
4117     |   blo <1
4118     |2:
4119     |  ins_next
4120     |
4121     |5:  // Copy all varargs.
4122     |  ldr CARG4, L->maxstack
4123     |   cmp CARG1, #0
4124     |   movle RB, #8                    // MULTRES = (0+1)*8
4125     |   addgt RB, CARG1, #8
4126     |  add CARG2, RA, CARG1
4127     |   str RB, SAVE_MULTRES
4128     |   ble <2
4129     |  cmp CARG2, CARG4
4130     |  bhi >7
4131     |6:
4132     |   ldrd CARG12, [RC], #8
4133     |   strd CARG12, [RA], #8
4134     |  cmp RC, CARG3
4135     |  blo <6
4136     |  b <2
4137     |
4138     |7:  // Grow stack for varargs.
4139     |  lsr CARG2, CARG1, #3
4140     |   str RA, L->top
4141     |  mov CARG1, L
4142     |   str BASE, L->base
4143     |  sub RC, RC, BASE                 // Need delta, because BASE may change.
4144     |   str PC, SAVE_PC
4145     |  sub RA, RA, BASE
4146     |  bl extern lj_state_growstack     // (lua_State *L, int n)
4147     |  ldr BASE, L->base
4148     |  add RA, BASE, RA
4149     |  add RC, BASE, RC
4150     |  sub CARG3, BASE, #8
4151     |  b <6
4152     break;
4154   /* -- Returns ----------------------------------------------------------- */
4156   case BC_RETM:
4157     |  // RA = results*8, RC = extra results
4158     |  ldr CARG1, SAVE_MULTRES
4159     |   ldr PC, [BASE, FRAME_PC]
4160     |    add RA, BASE, RA
4161     |  add RC, CARG1, RC, lsl #3
4162     |  b ->BC_RETM_Z
4163     break;
4165   case BC_RET:
4166     |  // RA = results*8, RC = nresults+1
4167     |  ldr PC, [BASE, FRAME_PC]
4168     |   lsl RC, RC, #3
4169     |    add RA, BASE, RA
4170     |->BC_RETM_Z:
4171     |   str RC, SAVE_MULTRES
4172     |1:
4173     |  ands CARG1, PC, #FRAME_TYPE
4174     |   eor CARG2, PC, #FRAME_VARG
4175     |  bne ->BC_RETV2_Z
4176     |
4177     |->BC_RET_Z:
4178     |  // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return
4179     |  ldr INS, [PC, #-4]
4180     |  subs CARG4, RC, #8
4181     |   sub CARG3, BASE, #8
4182     |  beq >3
4183     |2:
4184     |  ldrd CARG12, [RA], #8
4185     |   add BASE, BASE, #8
4186     |   subs CARG4, CARG4, #8
4187     |  strd CARG12, [BASE, #-16]
4188     |   bne <2
4189     |3:
4190     |  decode_RA8 RA, INS
4191     |  sub CARG4, CARG3, RA
4192     |   decode_RB8 RB, INS
4193     |  ldr LFUNC:CARG1, [CARG4, FRAME_FUNC]
4194     |5:
4195     |  cmp RB, RC                       // More results expected?
4196     |  bhi >6
4197     |  mov BASE, CARG4
4198     |  ldr CARG2, LFUNC:CARG1->field_pc
4199     |   ins_next1
4200     |   ins_next2
4201     |  ldr KBASE, [CARG2, #PC2PROTO(k)]
4202     |   ins_next3
4203     |
4204     |6:  // Fill up results with nil.
4205     |  mvn CARG2, #~LJ_TNIL
4206     |  add BASE, BASE, #8
4207     |   add RC, RC, #8
4208     |  str CARG2, [BASE, #-12]
4209     |  b <5
4210     |
4211     |->BC_RETV1_Z:  // Non-standard return case.
4212     |  add RA, BASE, RA
4213     |->BC_RETV2_Z:
4214     |  tst CARG2, #FRAME_TYPEP
4215     |  bne ->vm_return
4216     |  // Return from vararg function: relocate BASE down.
4217     |  sub BASE, BASE, CARG2
4218     |  ldr PC, [BASE, FRAME_PC]
4219     |  b <1
4220     break;
4222   case BC_RET0: case BC_RET1:
4223     |  // RA = results*8, RC = nresults+1
4224     |  ldr PC, [BASE, FRAME_PC]
4225     |   lsl RC, RC, #3
4226     |   str RC, SAVE_MULTRES
4227     |  ands CARG1, PC, #FRAME_TYPE
4228     |   eor CARG2, PC, #FRAME_VARG
4229     |   ldreq INS, [PC, #-4]
4230     |  bne ->BC_RETV1_Z
4231     if (op == BC_RET1) {
4232       |  ldrd CARG12, [BASE, RA]
4233     }
4234     |  sub CARG4, BASE, #8
4235     |   decode_RA8 RA, INS
4236     if (op == BC_RET1) {
4237       |  strd CARG12, [CARG4]
4238     }
4239     |  sub BASE, CARG4, RA
4240     |   decode_RB8 RB, INS
4241     |  ldr LFUNC:CARG1, [BASE, FRAME_FUNC]
4242     |5:
4243     |  cmp RB, RC
4244     |  bhi >6
4245     |  ldr CARG2, LFUNC:CARG1->field_pc
4246     |   ins_next1
4247     |   ins_next2
4248     |  ldr KBASE, [CARG2, #PC2PROTO(k)]
4249     |   ins_next3
4250     |
4251     |6:  // Fill up results with nil.
4252     |  sub CARG2, CARG4, #4
4253     |  mvn CARG3, #~LJ_TNIL
4254     |  str CARG3, [CARG2, RC]
4255     |  add RC, RC, #8
4256     |  b <5
4257     break;
4259   /* -- Loops and branches ------------------------------------------------ */
4261   |.define FOR_IDX,  [RA];      .define FOR_TIDX,  [RA, #4]
4262   |.define FOR_STOP, [RA, #8];  .define FOR_TSTOP, [RA, #12]
4263   |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20]
4264   |.define FOR_EXT,  [RA, #24]; .define FOR_TEXT,  [RA, #28]
4266   case BC_FORL:
4267     |.if JIT
4268     |  hotloop
4269     |.endif
4270     |  // Fall through. Assumes BC_IFORL follows.
4271     break;
4273   case BC_JFORI:
4274   case BC_JFORL:
4275 #if !LJ_HASJIT
4276     break;
4277 #endif
4278   case BC_FORI:
4279   case BC_IFORL:
4280     |  // RA = base*8, RC = target (after end of loop or start of loop)
4281     vk = (op == BC_IFORL || op == BC_JFORL);
4282     |  ldrd CARG12, [RA, BASE]!
4283     if (op != BC_JFORL) {
4284       |   add RC, PC, RC, lsl #2
4285     }
4286     if (!vk) {
4287       |  ldrd CARG34, FOR_STOP
4288       |   checktp CARG2, LJ_TISNUM
4289       |  ldr RB, FOR_TSTEP
4290       |   bne >5
4291       |  checktp CARG4, LJ_TISNUM
4292       |   ldr CARG4, FOR_STEP
4293       |  checktpeq RB, LJ_TISNUM
4294       |  bne ->vmeta_for
4295       |  cmp CARG4, #0
4296       |  blt >4
4297       |  cmp CARG1, CARG3
4298     } else {
4299       |  ldrd CARG34, FOR_STEP
4300       |   checktp CARG2, LJ_TISNUM
4301       |   bne >5
4302       |  adds CARG1, CARG1, CARG3
4303       |   ldr CARG4, FOR_STOP
4304       if (op == BC_IFORL) {
4305         |  addvs RC, PC, #0x20000               // Overflow: prevent branch.
4306       } else {
4307         |  bvs >2                               // Overflow: do not enter mcode.
4308       }
4309       |  cmp CARG3, #0
4310       |  blt >4
4311       |  cmp CARG1, CARG4
4312     }
4313     |1:
4314     if (op == BC_FORI) {
4315       |  subgt PC, RC, #0x20000
4316     } else if (op == BC_JFORI) {
4317       |  sub PC, RC, #0x20000
4318       |  ldrhle RC, [PC, #-2]
4319     } else if (op == BC_IFORL) {
4320       |  suble PC, RC, #0x20000
4321     }
4322     if (vk) {
4323       |  strd CARG12, FOR_IDX
4324     }
4325     |2:
4326     |   ins_next1
4327     |   ins_next2
4328     |  strd CARG12, FOR_EXT
4329     if (op == BC_JFORI || op == BC_JFORL) {
4330       |  ble =>BC_JLOOP
4331     }
4332     |3:
4333     |   ins_next3
4334     |
4335     |4:  // Invert check for negative step.
4336     if (!vk) {
4337       |  cmp CARG3, CARG1
4338     } else {
4339       |  cmp CARG4, CARG1
4340     }
4341     |  b <1
4342     |
4343     |5:  // FP loop.
4344     if (!vk) {
4345       |  cmnlo CARG4, #-LJ_TISNUM
4346       |  cmnlo RB, #-LJ_TISNUM
4347       |  bhs ->vmeta_for
4348       |.if FPU
4349       |  vldr d0, FOR_IDX
4350       |  vldr d1, FOR_STOP
4351       |  cmp RB, #0
4352       |  vstr d0, FOR_EXT
4353       |.else
4354       |  cmp RB, #0
4355       |   strd CARG12, FOR_EXT
4356       |  blt >8
4357       |.endif
4358     } else {
4359       |.if FPU
4360       |  vldr d0, FOR_IDX
4361       |  vldr d2, FOR_STEP
4362       |  vldr d1, FOR_STOP
4363       |  cmp CARG4, #0
4364       |  vadd.f64 d0, d0, d2
4365       |.else
4366       |  cmp CARG4, #0
4367       |  blt >8
4368       |  bl extern __aeabi_dadd
4369       |   strd CARG12, FOR_IDX
4370       |  ldrd CARG34, FOR_STOP
4371       |   strd CARG12, FOR_EXT
4372       |.endif
4373     }
4374     |6:
4375     |.if FPU
4376     |  vcmpge.f64 d0, d1
4377     |  vcmplt.f64 d1, d0
4378     |  vmrs
4379     |.else
4380     |  bl extern __aeabi_cdcmple
4381     |.endif
4382     if (vk) {
4383       |.if FPU
4384       |  vstr d0, FOR_IDX
4385       |  vstr d0, FOR_EXT
4386       |.endif
4387     }
4388     if (op == BC_FORI) {
4389       |  subhi PC, RC, #0x20000
4390     } else if (op == BC_JFORI) {
4391       |  sub PC, RC, #0x20000
4392       |  ldrhls RC, [PC, #-2]
4393       |  bls =>BC_JLOOP
4394     } else if (op == BC_IFORL) {
4395       |  subls PC, RC, #0x20000
4396     } else {
4397       |  bls =>BC_JLOOP
4398     }
4399     |  ins_next1
4400     |  ins_next2
4401     |  b <3
4402     |
4403     |.if not FPU
4404     |8:  // Invert check for negative step.
4405     if (vk) {
4406       |  bl extern __aeabi_dadd
4407       |  strd CARG12, FOR_IDX
4408       |  strd CARG12, FOR_EXT
4409     }
4410     |  mov CARG3, CARG1
4411     |  mov CARG4, CARG2
4412     |  ldrd CARG12, FOR_STOP
4413     |  b <6
4414     |.endif
4415     break;
4417   case BC_ITERL:
4418     |.if JIT
4419     |  hotloop
4420     |.endif
4421     |  // Fall through. Assumes BC_IITERL follows.
4422     break;
4424   case BC_JITERL:
4425 #if !LJ_HASJIT
4426     break;
4427 #endif
4428   case BC_IITERL:
4429     |  // RA = base*8, RC = target
4430     |  ldrd CARG12, [RA, BASE]!
4431     if (op == BC_JITERL) {
4432       |  cmn CARG2, #-LJ_TNIL           // Stop if iterator returned nil.
4433       |  strdne CARG12, [RA, #-8]
4434       |  bne =>BC_JLOOP
4435     } else {
4436       |   add RC, PC, RC, lsl #2
4437       |  // STALL: load CARG12.
4438       |  cmn CARG2, #-LJ_TNIL           // Stop if iterator returned nil.
4439       |  subne PC, RC, #0x20000         // Otherwise save control var + branch.
4440       |  strdne CARG12, [RA, #-8]
4441     }
4442     |  ins_next
4443     break;
4445   case BC_LOOP:
4446     |  // RA = base*8, RC = target (loop extent)
4447     |  // Note: RA/RC is only used by trace recorder to determine scope/extent
4448     |  // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4449     |.if JIT
4450     |  hotloop
4451     |.endif
4452     |  // Fall through. Assumes BC_ILOOP follows.
4453     break;
4455   case BC_ILOOP:
4456     |  // RA = base*8, RC = target (loop extent)
4457     |  ins_next
4458     break;
4460   case BC_JLOOP:
4461     |.if JIT
4462     |  // RA = base (ignored), RC = traceno
4463     |  ldr CARG1, [DISPATCH, #DISPATCH_J(trace)]
4464     |   mov CARG2, #0  // Traces on ARM don't store the trace number, so use 0.
4465     |  ldr TRACE:RC, [CARG1, RC, lsl #2]
4466     |   st_vmstate CARG2
4467     |  ldr RA, TRACE:RC->mcode
4468     |   str BASE, [DISPATCH, #DISPATCH_GL(jit_base)]
4469     |   str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)]
4470     |  bx RA
4471     |.endif
4472     break;
4474   case BC_JMP:
4475     |  // RA = base*8 (only used by trace recorder), RC = target
4476     |  add RC, PC, RC, lsl #2
4477     |  sub PC, RC, #0x20000
4478     |  ins_next
4479     break;
4481   /* -- Function headers -------------------------------------------------- */
4483   case BC_FUNCF:
4484     |.if JIT
4485     |  hotcall
4486     |.endif
4487   case BC_FUNCV:  /* NYI: compiled vararg functions. */
4488     |  // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow.
4489     break;
4491   case BC_JFUNCF:
4492 #if !LJ_HASJIT
4493     break;
4494 #endif
4495   case BC_IFUNCF:
4496     |  // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4497     |  ldr CARG1, L->maxstack
4498     |   ldrb CARG2, [PC, #-4+PC2PROTO(numparams)]
4499     |    ldr KBASE, [PC, #-4+PC2PROTO(k)]
4500     |  cmp RA, CARG1
4501     |  bhi ->vm_growstack_l
4502     if (op != BC_JFUNCF) {
4503       |  ins_next1
4504       |  ins_next2
4505     }
4506     |2:
4507     |  cmp NARGS8:RC, CARG2, lsl #3     // Check for missing parameters.
4508     |   mvn CARG4, #~LJ_TNIL
4509     |  blo >3
4510     if (op == BC_JFUNCF) {
4511       |  decode_RD RC, INS
4512       |  b =>BC_JLOOP
4513     } else {
4514       |  ins_next3
4515     }
4516     |
4517     |3:  // Clear missing parameters.
4518     |  strd CARG34, [BASE, NARGS8:RC]
4519     |  add NARGS8:RC, NARGS8:RC, #8
4520     |  b <2
4521     break;
4523   case BC_JFUNCV:
4524 #if !LJ_HASJIT
4525     break;
4526 #endif
4527     |  NYI  // NYI: compiled vararg functions
4528     break;  /* NYI: compiled vararg functions. */
4530   case BC_IFUNCV:
4531     |  // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8
4532     |  ldr CARG1, L->maxstack
4533     |   add CARG4, BASE, RC
4534     |  add RA, RA, RC
4535     |   str LFUNC:CARG3, [CARG4]        // Store copy of LFUNC.
4536     |   add CARG2, RC, #8+FRAME_VARG
4537     |    ldr KBASE, [PC, #-4+PC2PROTO(k)]
4538     |  cmp RA, CARG1
4539     |   str CARG2, [CARG4, #4]          // Store delta + FRAME_VARG.
4540     |  bhs ->vm_growstack_l
4541     |  ldrb RB, [PC, #-4+PC2PROTO(numparams)]
4542     |   mov RA, BASE
4543     |   mov RC, CARG4
4544     |  cmp RB, #0
4545     |   add BASE, CARG4, #8
4546     |  beq >3
4547     |  mvn CARG3, #~LJ_TNIL
4548     |1:
4549     |  cmp RA, RC                       // Less args than parameters?
4550     |   ldrdlo CARG12, [RA], #8
4551     |   movhs CARG2, CARG3
4552     |    strlo CARG3, [RA, #-4]         // Clear old fixarg slot (help the GC).
4553     |2:
4554     |  subs RB, RB, #1
4555     |   strd CARG12, [CARG4, #8]!
4556     |  bne <1
4557     |3:
4558     |  ins_next
4559     break;
4561   case BC_FUNCC:
4562   case BC_FUNCCW:
4563     |  // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8
4564     if (op == BC_FUNCC) {
4565       |  ldr CARG4, CFUNC:CARG3->f
4566     } else {
4567       |  ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)]
4568     }
4569     |   add CARG2, RA, NARGS8:RC
4570     |   ldr CARG1, L->maxstack
4571     |  add RC, BASE, NARGS8:RC
4572     |    str BASE, L->base
4573     |   cmp CARG2, CARG1
4574     |  str RC, L->top
4575     if (op == BC_FUNCCW) {
4576       |  ldr CARG2, CFUNC:CARG3->f
4577     }
4578     |    mv_vmstate CARG3, C
4579     |  mov CARG1, L
4580     |   bhi ->vm_growstack_c            // Need to grow stack.
4581     |    st_vmstate CARG3
4582     |  blx CARG4                        // (lua_State *L [, lua_CFunction f])
4583     |  // Returns nresults.
4584     |  ldr BASE, L->base
4585     |    mv_vmstate CARG3, INTERP
4586     |   ldr CRET2, L->top
4587     |    str L, [DISPATCH, #DISPATCH_GL(cur_L)]
4588     |   lsl RC, CRET1, #3
4589     |    st_vmstate CARG3
4590     |  ldr PC, [BASE, FRAME_PC]
4591     |   sub RA, CRET2, RC               // RA = L->top - nresults*8
4592     |  b ->vm_returnc
4593     break;
4595   /* ---------------------------------------------------------------------- */
4597   default:
4598     fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4599     exit(2);
4600     break;
4601   }
4604 static int build_backend(BuildCtx *ctx)
4606   int op;
4608   dasm_growpc(Dst, BC__MAX);
4610   build_subroutines(ctx);
4612   |.code_op
4613   for (op = 0; op < BC__MAX; op++)
4614     build_ins(ctx, (BCOp)op, op);
4616   return BC__MAX;
4619 /* Emit pseudo frame-info for all assembler functions. */
4620 static void emit_asm_debug(BuildCtx *ctx)
4622   int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4623   int i;
4624   switch (ctx->mode) {
4625   case BUILD_elfasm:
4626     fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n");
4627     fprintf(ctx->fp,
4628         ".Lframe0:\n"
4629         "\t.long .LECIE0-.LSCIE0\n"
4630         ".LSCIE0:\n"
4631         "\t.long 0xffffffff\n"
4632         "\t.byte 0x1\n"
4633         "\t.string \"\"\n"
4634         "\t.uleb128 0x1\n"
4635         "\t.sleb128 -4\n"
4636         "\t.byte 0xe\n"                         /* Return address is in lr. */
4637         "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n"   /* def_cfa sp */
4638         "\t.align 2\n"
4639         ".LECIE0:\n\n");
4640     fprintf(ctx->fp,
4641         ".LSFDE0:\n"
4642         "\t.long .LEFDE0-.LASFDE0\n"
4643         ".LASFDE0:\n"
4644         "\t.long .Lframe0\n"
4645         "\t.long .Lbegin\n"
4646         "\t.long %d\n"
4647         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
4648         "\t.byte 0x8e\n\t.uleb128 1\n",         /* offset lr */
4649         fcofs, CFRAME_SIZE);
4650     for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--)  /* offset r4-r11 */
4651       fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i));
4652 #if LJ_ARCH_HASFPU
4653     for (i = 15; i >= 8; i--)  /* offset d8-d15 */
4654       fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n",
4655         64+2*i, 10+2*(15-i));
4656     fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25);  /* offset r4 */
4657 #endif
4658     fprintf(ctx->fp,
4659         "\t.align 2\n"
4660         ".LEFDE0:\n\n");
4661 #if LJ_HASFFI
4662     fprintf(ctx->fp,
4663         ".LSFDE1:\n"
4664         "\t.long .LEFDE1-.LASFDE1\n"
4665         ".LASFDE1:\n"
4666         "\t.long .Lframe0\n"
4667         "\t.long lj_vm_ffi_call\n"
4668         "\t.long %d\n"
4669         "\t.byte 0xe\n\t.uleb128 16\n"          /* def_cfa_offset */
4670         "\t.byte 0x8e\n\t.uleb128 1\n"          /* offset lr */
4671         "\t.byte 0x8b\n\t.uleb128 2\n"          /* offset r11 */
4672         "\t.byte 0x85\n\t.uleb128 3\n"          /* offset r5 */
4673         "\t.byte 0x84\n\t.uleb128 4\n"          /* offset r4 */
4674         "\t.byte 0xd\n\t.uleb128 0xb\n"         /* def_cfa_register r11 */
4675         "\t.align 2\n"
4676         ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
4677 #endif
4678     break;
4679   default:
4680     break;
4681   }