Fix underflow handling in builtin string to number conversion.
[luajit-2.0/celess22.git] / src / vm_x86.dasc
blobc455795e472e25bd9c79ed84fff7df35b6797882
1 |// Low-level VM code for x86 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
5 |.if P64
6 |.arch x64
7 |.else
8 |.arch x86
9 |.endif
10 |.section code_op, code_sub
12 |.actionlist build_actionlist
13 |.globals GLOB_
14 |.globalnames globnames
15 |.externnames extnames
17 |//-----------------------------------------------------------------------
19 |.if P64
20 |.define X64, 1
21 |.define SSE, 1
22 |.if WIN
23 |.define X64WIN, 1
24 |.endif
25 |.endif
27 |// Fixed register assignments for the interpreter.
28 |// This is very fragile and has many dependencies. Caveat emptor.
29 |.define BASE,          edx             // Not C callee-save, refetched anyway.
30 |.if not X64
31 |.define KBASE,         edi             // Must be C callee-save.
32 |.define KBASEa,        KBASE
33 |.define PC,            esi             // Must be C callee-save.
34 |.define PCa,           PC
35 |.define DISPATCH,      ebx             // Must be C callee-save.
36 |.elif X64WIN
37 |.define KBASE,         edi             // Must be C callee-save.
38 |.define KBASEa,        rdi
39 |.define PC,            esi             // Must be C callee-save.
40 |.define PCa,           rsi
41 |.define DISPATCH,      ebx             // Must be C callee-save.
42 |.else
43 |.define KBASE,         r15d            // Must be C callee-save.
44 |.define KBASEa,        r15
45 |.define PC,            ebx             // Must be C callee-save.
46 |.define PCa,           rbx
47 |.define DISPATCH,      r14d            // Must be C callee-save.
48 |.endif
50 |.define RA,            ecx
51 |.define RAH,           ch
52 |.define RAL,           cl
53 |.define RB,            ebp             // Must be ebp (C callee-save).
54 |.define RC,            eax             // Must be eax.
55 |.define RCW,           ax
56 |.define RCH,           ah
57 |.define RCL,           al
58 |.define OP,            RB
59 |.define RD,            RC
60 |.define RDW,           RCW
61 |.define RDL,           RCL
62 |.if X64
63 |.define RAa, rcx
64 |.define RBa, rbp
65 |.define RCa, rax
66 |.define RDa, rax
67 |.else
68 |.define RAa, RA
69 |.define RBa, RB
70 |.define RCa, RC
71 |.define RDa, RD
72 |.endif
74 |.if not X64
75 |.define FCARG1,        ecx             // x86 fastcall arguments.
76 |.define FCARG2,        edx
77 |.elif X64WIN
78 |.define CARG1,         rcx             // x64/WIN64 C call arguments.
79 |.define CARG2,         rdx
80 |.define CARG3,         r8
81 |.define CARG4,         r9
82 |.define CARG1d,        ecx
83 |.define CARG2d,        edx
84 |.define CARG3d,        r8d
85 |.define CARG4d,        r9d
86 |.define FCARG1,        CARG1d          // Upwards compatible to x86 fastcall.
87 |.define FCARG2,        CARG2d
88 |.else
89 |.define CARG1,         rdi             // x64/POSIX C call arguments.
90 |.define CARG2,         rsi
91 |.define CARG3,         rdx
92 |.define CARG4,         rcx
93 |.define CARG5,         r8
94 |.define CARG6,         r9
95 |.define CARG1d,        edi
96 |.define CARG2d,        esi
97 |.define CARG3d,        edx
98 |.define CARG4d,        ecx
99 |.define CARG5d,        r8d
100 |.define CARG6d,        r9d
101 |.define FCARG1,        CARG1d          // Simulate x86 fastcall.
102 |.define FCARG2,        CARG2d
103 |.endif
105 |// Type definitions. Some of these are only used for documentation.
106 |.type L,               lua_State
107 |.type GL,              global_State
108 |.type TVALUE,          TValue
109 |.type GCOBJ,           GCobj
110 |.type STR,             GCstr
111 |.type TAB,             GCtab
112 |.type LFUNC,           GCfuncL
113 |.type CFUNC,           GCfuncC
114 |.type PROTO,           GCproto
115 |.type UPVAL,           GCupval
116 |.type NODE,            Node
117 |.type NARGS,           int
118 |.type TRACE,           GCtrace
120 |// Stack layout while in interpreter. Must match with lj_frame.h.
121 |//-----------------------------------------------------------------------
122 |.if not X64            // x86 stack layout.
124 |.define CFRAME_SPACE,  aword*7                 // Delta for esp (see <--).
125 |.macro saveregs_
126 |  push edi; push esi; push ebx
127 |  sub esp, CFRAME_SPACE
128 |.endmacro
129 |.macro saveregs
130 |  push ebp; saveregs_
131 |.endmacro
132 |.macro restoreregs
133 |  add esp, CFRAME_SPACE
134 |  pop ebx; pop esi; pop edi; pop ebp
135 |.endmacro
137 |.define SAVE_ERRF,     aword [esp+aword*15]    // vm_pcall/vm_cpcall only.
138 |.define SAVE_NRES,     aword [esp+aword*14]
139 |.define SAVE_CFRAME,   aword [esp+aword*13]
140 |.define SAVE_L,        aword [esp+aword*12]
141 |//----- 16 byte aligned, ^^^ arguments from C caller
142 |.define SAVE_RET,      aword [esp+aword*11]    //<-- esp entering interpreter.
143 |.define SAVE_R4,       aword [esp+aword*10]
144 |.define SAVE_R3,       aword [esp+aword*9]
145 |.define SAVE_R2,       aword [esp+aword*8]
146 |//----- 16 byte aligned
147 |.define SAVE_R1,       aword [esp+aword*7]     //<-- esp after register saves.
148 |.define SAVE_PC,       aword [esp+aword*6]
149 |.define TMP2,          aword [esp+aword*5]
150 |.define TMP1,          aword [esp+aword*4]
151 |//----- 16 byte aligned
152 |.define ARG4,          aword [esp+aword*3]
153 |.define ARG3,          aword [esp+aword*2]
154 |.define ARG2,          aword [esp+aword*1]
155 |.define ARG1,          aword [esp]             //<-- esp while in interpreter.
156 |//----- 16 byte aligned, ^^^ arguments for C callee
158 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
159 |.define FPARG3,        qword [esp+qword*1]
160 |.define FPARG1,        qword [esp]
161 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
162 |.define TMPQ,          qword [esp+aword*4]
163 |.define TMP3,          ARG4
164 |.define ARG5,          TMP1
165 |.define TMPa,          TMP1
166 |.define MULTRES,       TMP2
168 |// Arguments for vm_call and vm_pcall.
169 |.define INARG_BASE,    SAVE_CFRAME             // Overwritten by SAVE_CFRAME!
171 |// Arguments for vm_cpcall.
172 |.define INARG_CP_CALL, SAVE_ERRF
173 |.define INARG_CP_UD,   SAVE_NRES
174 |.define INARG_CP_FUNC, SAVE_CFRAME
176 |//-----------------------------------------------------------------------
177 |.elif X64WIN           // x64/Windows stack layout
179 |.define CFRAME_SPACE,  aword*5                 // Delta for rsp (see <--).
180 |.macro saveregs_
181 |  push rdi; push rsi; push rbx
182 |  sub rsp, CFRAME_SPACE
183 |.endmacro
184 |.macro saveregs
185 |  push rbp; saveregs_
186 |.endmacro
187 |.macro restoreregs
188 |  add rsp, CFRAME_SPACE
189 |  pop rbx; pop rsi; pop rdi; pop rbp
190 |.endmacro
192 |.define SAVE_CFRAME,   aword [rsp+aword*13]
193 |.define SAVE_PC,       dword [rsp+dword*25]
194 |.define SAVE_L,        dword [rsp+dword*24]
195 |.define SAVE_ERRF,     dword [rsp+dword*23]
196 |.define SAVE_NRES,     dword [rsp+dword*22]
197 |.define TMP2,          dword [rsp+dword*21]
198 |.define TMP1,          dword [rsp+dword*20]
199 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
200 |.define SAVE_RET,      aword [rsp+aword*9]     //<-- rsp entering interpreter.
201 |.define SAVE_R4,       aword [rsp+aword*8]
202 |.define SAVE_R3,       aword [rsp+aword*7]
203 |.define SAVE_R2,       aword [rsp+aword*6]
204 |.define SAVE_R1,       aword [rsp+aword*5]     //<-- rsp after register saves.
205 |.define ARG5,          aword [rsp+aword*4]
206 |.define CSAVE_4,       aword [rsp+aword*3]
207 |.define CSAVE_3,       aword [rsp+aword*2]
208 |.define CSAVE_2,       aword [rsp+aword*1]
209 |.define CSAVE_1,       aword [rsp]             //<-- rsp while in interpreter.
210 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
212 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
213 |.define TMPQ,          qword [rsp+aword*10]
214 |.define MULTRES,       TMP2
215 |.define TMPa,          ARG5
216 |.define ARG5d,         dword [rsp+aword*4]
217 |.define TMP3,          ARG5d
219 |//-----------------------------------------------------------------------
220 |.else                  // x64/POSIX stack layout
222 |.define CFRAME_SPACE,  aword*5                 // Delta for rsp (see <--).
223 |.macro saveregs_
224 |  push rbx; push r15; push r14
225 |  sub rsp, CFRAME_SPACE
226 |.endmacro
227 |.macro saveregs
228 |  push rbp; saveregs_
229 |.endmacro
230 |.macro restoreregs
231 |  add rsp, CFRAME_SPACE
232 |  pop r14; pop r15; pop rbx; pop rbp
233 |.endmacro
235 |//----- 16 byte aligned,
236 |.define SAVE_RET,      aword [rsp+aword*9]     //<-- rsp entering interpreter.
237 |.define SAVE_R4,       aword [rsp+aword*8]
238 |.define SAVE_R3,       aword [rsp+aword*7]
239 |.define SAVE_R2,       aword [rsp+aword*6]
240 |.define SAVE_R1,       aword [rsp+aword*5]     //<-- rsp after register saves.
241 |.define SAVE_CFRAME,   aword [rsp+aword*4]
242 |.define SAVE_PC,       dword [rsp+dword*7]
243 |.define SAVE_L,        dword [rsp+dword*6]
244 |.define SAVE_ERRF,     dword [rsp+dword*5]
245 |.define SAVE_NRES,     dword [rsp+dword*4]
246 |.define TMPa,          aword [rsp+aword*1]
247 |.define TMP2,          dword [rsp+dword*1]
248 |.define TMP1,          dword [rsp]             //<-- rsp while in interpreter.
249 |//----- 16 byte aligned
251 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
252 |.define TMPQ,          qword [rsp]
253 |.define TMP3,          dword [rsp+aword*1]
254 |.define MULTRES,       TMP2
256 |.endif
258 |//-----------------------------------------------------------------------
260 |// Instruction headers.
261 |.macro ins_A; .endmacro
262 |.macro ins_AD; .endmacro
263 |.macro ins_AJ; .endmacro
264 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
265 |.macro ins_AB_; movzx RB, RCH; .endmacro
266 |.macro ins_A_C; movzx RC, RCL; .endmacro
267 |.macro ins_AND; not RDa; .endmacro
269 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
270 |.macro ins_NEXT
271 |  mov RC, [PC]
272 |  movzx RA, RCH
273 |  movzx OP, RCL
274 |  add PC, 4
275 |  shr RC, 16
276 |.if X64
277 |  jmp aword [DISPATCH+OP*8]
278 |.else
279 |  jmp aword [DISPATCH+OP*4]
280 |.endif
281 |.endmacro
283 |// Instruction footer.
284 |.if 1
285 |  // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
286 |  .define ins_next, ins_NEXT
287 |  .define ins_next_, ins_NEXT
288 |.else
289 |  // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
290 |  // Affects only certain kinds of benchmarks (and only with -j off).
291 |  // Around 10%-30% slower on Core2, a lot more slower on P4.
292 |  .macro ins_next
293 |    jmp ->ins_next
294 |  .endmacro
295 |  .macro ins_next_
296 |  ->ins_next:
297 |    ins_NEXT
298 |  .endmacro
299 |.endif
301 |// Call decode and dispatch.
302 |.macro ins_callt
303 |  // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
304 |  mov PC, LFUNC:RB->pc
305 |  mov RA, [PC]
306 |  movzx OP, RAL
307 |  movzx RA, RAH
308 |  add PC, 4
309 |.if X64
310 |  jmp aword [DISPATCH+OP*8]
311 |.else
312 |  jmp aword [DISPATCH+OP*4]
313 |.endif
314 |.endmacro
316 |.macro ins_call
317 |  // BASE = new base, RB = LFUNC, RD = nargs+1
318 |  mov [BASE-4], PC
319 |  ins_callt
320 |.endmacro
322 |//-----------------------------------------------------------------------
324 |// Macros to test operand types.
325 |.macro checktp, reg, tp;  cmp dword [BASE+reg*8+4], tp; .endmacro
326 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
327 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
328 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
329 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
331 |// These operands must be used with movzx.
332 |.define PC_OP, byte [PC-4]
333 |.define PC_RA, byte [PC-3]
334 |.define PC_RB, byte [PC-1]
335 |.define PC_RC, byte [PC-2]
336 |.define PC_RD, word [PC-2]
338 |.macro branchPC, reg
339 |  lea PC, [PC+reg*4-BCBIAS_J*4]
340 |.endmacro
342 |// Assumes DISPATCH is relative to GL.
343 #define DISPATCH_GL(field)      (GG_DISP2G + (int)offsetof(global_State, field))
344 #define DISPATCH_J(field)       (GG_DISP2J + (int)offsetof(jit_State, field))
346 #define PC2PROTO(field)  ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
348 |// Decrement hashed hotcount and trigger trace recorder if zero.
349 |.macro hotloop, reg
350 |  mov reg, PC
351 |  shr reg, 1
352 |  and reg, HOTCOUNT_PCMASK
353 |  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
354 |  jb ->vm_hotloop
355 |.endmacro
357 |.macro hotcall, reg
358 |  mov reg, PC
359 |  shr reg, 1
360 |  and reg, HOTCOUNT_PCMASK
361 |  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
362 |  jb ->vm_hotcall
363 |.endmacro
365 |// Set current VM state.
366 |.macro set_vmstate, st
367 |  mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
368 |.endmacro
370 |// x87 compares.
371 |.macro fcomparepp                      // Compare and pop st0 >< st1.
372 |  fucomip st1
373 |  fpop
374 |.endmacro
376 |.macro fdup; fld st0; .endmacro
377 |.macro fpop1; fstp st1; .endmacro
379 |// Synthesize SSE FP constants.
380 |.macro sseconst_abs, reg, tmp          // Synthesize abs mask.
381 |.if X64
382 |  mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
383 |.else
384 |  pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
385 |.endif
386 |.endmacro
388 |.macro sseconst_hi, reg, tmp, val      // Synthesize hi-32 bit const.
389 |.if X64
390 |  mov64 tmp, U64x(val,00000000); movd reg, tmp
391 |.else
392 |  mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
393 |.endif
394 |.endmacro
396 |.macro sseconst_sign, reg, tmp         // Synthesize sign mask.
397 |  sseconst_hi reg, tmp, 80000000
398 |.endmacro
399 |.macro sseconst_1, reg, tmp            // Synthesize 1.0.
400 |  sseconst_hi reg, tmp, 3ff00000
401 |.endmacro
402 |.macro sseconst_m1, reg, tmp           // Synthesize -1.0.
403 |  sseconst_hi reg, tmp, bff00000
404 |.endmacro
405 |.macro sseconst_2p52, reg, tmp         // Synthesize 2^52.
406 |  sseconst_hi reg, tmp, 43300000
407 |.endmacro
408 |.macro sseconst_tobit, reg, tmp        // Synthesize 2^52 + 2^51.
409 |  sseconst_hi reg, tmp, 43380000
410 |.endmacro
412 |// Move table write barrier back. Overwrites reg.
413 |.macro barrierback, tab, reg
414 |  and byte tab->marked, (uint8_t)~LJ_GC_BLACK  // black2gray(tab)
415 |  mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
416 |  mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
417 |  mov tab->gclist, reg
418 |.endmacro
420 |//-----------------------------------------------------------------------
422 /* Generate subroutines used by opcodes and other parts of the VM. */
423 /* The .code_sub section should be last to help static branch prediction. */
424 static void build_subroutines(BuildCtx *ctx)
426   |.code_sub
427   |
428   |//-----------------------------------------------------------------------
429   |//-- Return handling ----------------------------------------------------
430   |//-----------------------------------------------------------------------
431   |
432   |->vm_returnp:
433   |  test PC, FRAME_P
434   |  jz ->cont_dispatch
435   |
436   |  // Return from pcall or xpcall fast func.
437   |  and PC, -8
438   |  sub BASE, PC                       // Restore caller base.
439   |  lea RAa, [RA+PC-8]                 // Rebase RA and prepend one result.
440   |  mov PC, [BASE-4]                   // Fetch PC of previous frame.
441   |  // Prepending may overwrite the pcall frame, so do it at the end.
442   |  mov dword [BASE+RA+4], LJ_TTRUE    // Prepend true to results.
443   |
444   |->vm_returnc:
445   |  add RD, 1                          // RD = nresults+1
446   |  mov MULTRES, RD
447   |  test PC, FRAME_TYPE
448   |  jz ->BC_RET_Z                      // Handle regular return to Lua.
449   |
450   |->vm_return:
451   |  // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
452   |  xor PC, FRAME_C
453   |  test PC, FRAME_TYPE
454   |  jnz ->vm_returnp
455   |
456   |  // Return to C.
457   |  set_vmstate C
458   |  and PC, -8
459   |  sub PC, BASE
460   |  neg PC                             // Previous base = BASE - delta.
461   |
462   |  sub RD, 1
463   |  jz >2
464   |1:  // Move results down.
465   |.if X64
466   |  mov RBa, [BASE+RA]
467   |  mov [BASE-8], RBa
468   |.else
469   |  mov RB, [BASE+RA]
470   |  mov [BASE-8], RB
471   |  mov RB, [BASE+RA+4]
472   |  mov [BASE-4], RB
473   |.endif
474   |  add BASE, 8
475   |  sub RD, 1
476   |  jnz <1
477   |2:
478   |  mov L:RB, SAVE_L
479   |  mov L:RB->base, PC
480   |3:
481   |  mov RD, MULTRES
482   |  mov RA, SAVE_NRES                  // RA = wanted nresults+1
483   |4:
484   |  cmp RA, RD
485   |  jne >6                             // More/less results wanted?
486   |5:
487   |  sub BASE, 8
488   |  mov L:RB->top, BASE
489   |
490   |->vm_leave_cp:
491   |  mov RAa, SAVE_CFRAME               // Restore previous C frame.
492   |  mov L:RB->cframe, RAa
493   |  xor eax, eax                       // Ok return status for vm_pcall.
494   |
495   |->vm_leave_unw:
496   |  restoreregs
497   |  ret
498   |
499   |6:
500   |  jb >7                              // Less results wanted?
501   |  // More results wanted. Check stack size and fill up results with nil.
502   |  cmp BASE, L:RB->maxstack
503   |  ja >8
504   |  mov dword [BASE-4], LJ_TNIL
505   |  add BASE, 8
506   |  add RD, 1
507   |  jmp <4
508   |
509   |7:  // Less results wanted.
510   |  test RA, RA
511   |  jz <5                              // But check for LUA_MULTRET+1.
512   |  sub RA, RD                         // Negative result!
513   |  lea BASE, [BASE+RA*8]              // Correct top.
514   |  jmp <5
515   |
516   |8:  // Corner case: need to grow stack for filling up results.
517   |  // This can happen if:
518   |  // - A C function grows the stack (a lot).
519   |  // - The GC shrinks the stack in between.
520   |  // - A return back from a lua_call() with (high) nresults adjustment.
521   |  mov L:RB->top, BASE                // Save current top held in BASE (yes).
522   |  mov MULTRES, RD                    // Need to fill only remainder with nil.
523   |  mov FCARG2, RA
524   |  mov FCARG1, L:RB
525   |  call extern lj_state_growstack@8   // (lua_State *L, int n)
526   |  mov BASE, L:RB->top                // Need the (realloced) L->top in BASE.
527   |  jmp <3
528   |
529   |->vm_unwind_c@8:                     // Unwind C stack, return from vm_pcall.
530   |  // (void *cframe, int errcode)
531   |.if X64
532   |  mov eax, CARG2d                    // Error return status for vm_pcall.
533   |  mov rsp, CARG1
534   |.else
535   |  mov eax, FCARG2                    // Error return status for vm_pcall.
536   |  mov esp, FCARG1
537   |.endif
538   |->vm_unwind_c_eh:                    // Landing pad for external unwinder.
539   |  mov L:RB, SAVE_L
540   |  mov GL:RB, L:RB->glref
541   |  mov dword GL:RB->vmstate, ~LJ_VMST_C
542   |  jmp ->vm_leave_unw
543   |
544   |->vm_unwind_rethrow:
545   |.if X64 and not X64WIN
546   |  mov FCARG1, SAVE_L
547   |  mov FCARG2, eax
548   |  restoreregs
549   |  jmp extern lj_err_throw@8          // (lua_State *L, int errcode)
550   |.endif
551   |
552   |->vm_unwind_ff@4:                    // Unwind C stack, return from ff pcall.
553   |  // (void *cframe)
554   |.if X64
555   |  and CARG1, CFRAME_RAWMASK
556   |  mov rsp, CARG1
557   |.else
558   |  and FCARG1, CFRAME_RAWMASK
559   |  mov esp, FCARG1
560   |.endif
561   |->vm_unwind_ff_eh:                   // Landing pad for external unwinder.
562   |  mov L:RB, SAVE_L
563   |  mov RAa, -8                        // Results start at BASE+RA = BASE-8.
564   |  mov RD, 1+1                        // Really 1+2 results, incr. later.
565   |  mov BASE, L:RB->base
566   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
567   |  add DISPATCH, GG_G2DISP
568   |  mov PC, [BASE-4]                   // Fetch PC of previous frame.
569   |  mov dword [BASE-4], LJ_TFALSE      // Prepend false to error message.
570   |  set_vmstate INTERP
571   |  jmp ->vm_returnc                   // Increments RD/MULTRES and returns.
572   |
573   |//-----------------------------------------------------------------------
574   |//-- Grow stack for calls -----------------------------------------------
575   |//-----------------------------------------------------------------------
576   |
577   |->vm_growstack_c:                    // Grow stack for C function.
578   |  mov FCARG2, LUA_MINSTACK
579   |  jmp >2
580   |
581   |->vm_growstack_v:                    // Grow stack for vararg Lua function.
582   |  sub RD, 8
583   |  jmp >1
584   |
585   |->vm_growstack_f:                    // Grow stack for fixarg Lua function.
586   |  // BASE = new base, RD = nargs+1, RB = L, PC = first PC
587   |  lea RD, [BASE+NARGS:RD*8-8]
588   |1:
589   |  movzx RA, byte [PC-4+PC2PROTO(framesize)]
590   |  add PC, 4                          // Must point after first instruction.
591   |  mov L:RB->base, BASE
592   |  mov L:RB->top, RD
593   |  mov SAVE_PC, PC
594   |  mov FCARG2, RA
595   |2:
596   |  // RB = L, L->base = new base, L->top = top
597   |  mov FCARG1, L:RB
598   |  call extern lj_state_growstack@8   // (lua_State *L, int n)
599   |  mov BASE, L:RB->base
600   |  mov RD, L:RB->top
601   |  mov LFUNC:RB, [BASE-8]
602   |  sub RD, BASE
603   |  shr RD, 3
604   |  add NARGS:RD, 1
605   |  // BASE = new base, RB = LFUNC, RD = nargs+1
606   |  ins_callt                          // Just retry the call.
607   |
608   |//-----------------------------------------------------------------------
609   |//-- Entry points into the assembler VM ---------------------------------
610   |//-----------------------------------------------------------------------
611   |
612   |->vm_resume:                         // Setup C frame and resume thread.
613   |  // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
614   |  saveregs
615   |.if X64
616   |  mov L:RB, CARG1d                   // Caveat: CARG1d may be RA.
617   |  mov SAVE_L, CARG1d
618   |  mov RA, CARG2d
619   |.else
620   |  mov L:RB, SAVE_L
621   |  mov RA, INARG_BASE                 // Caveat: overlaps SAVE_CFRAME!
622   |.endif
623   |  mov PC, FRAME_CP
624   |  xor RD, RD
625   |  lea KBASEa, [esp+CFRAME_RESUME]
626   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
627   |  add DISPATCH, GG_G2DISP
628   |  mov L:RB->cframe, KBASEa
629   |  mov SAVE_PC, RD                    // Any value outside of bytecode is ok.
630   |  mov SAVE_CFRAME, RDa
631   |.if X64
632   |  mov SAVE_NRES, RD
633   |  mov SAVE_ERRF, RD
634   |.endif
635   |  cmp byte L:RB->status, RDL
636   |  je >3                              // Initial resume (like a call).
637   |
638   |  // Resume after yield (like a return).
639   |  set_vmstate INTERP
640   |  mov byte L:RB->status, RDL
641   |  mov BASE, L:RB->base
642   |  mov RD, L:RB->top
643   |  sub RD, RA
644   |  shr RD, 3
645   |  add RD, 1                          // RD = nresults+1
646   |  sub RA, BASE                       // RA = resultofs
647   |  mov PC, [BASE-4]
648   |  mov MULTRES, RD
649   |  test PC, FRAME_TYPE
650   |  jz ->BC_RET_Z
651   |  jmp ->vm_return
652   |
653   |->vm_pcall:                          // Setup protected C frame and enter VM.
654   |  // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
655   |  saveregs
656   |  mov PC, FRAME_CP
657   |.if X64
658   |  mov SAVE_ERRF, CARG4d
659   |.endif
660   |  jmp >1
661   |
662   |->vm_call:                           // Setup C frame and enter VM.
663   |  // (lua_State *L, TValue *base, int nres1)
664   |  saveregs
665   |  mov PC, FRAME_C
666   |
667   |1:  // Entry point for vm_pcall above (PC = ftype).
668   |.if X64
669   |  mov SAVE_NRES, CARG3d
670   |  mov L:RB, CARG1d                   // Caveat: CARG1d may be RA.
671   |  mov SAVE_L, CARG1d
672   |  mov RA, CARG2d
673   |.else
674   |  mov L:RB, SAVE_L
675   |  mov RA, INARG_BASE                 // Caveat: overlaps SAVE_CFRAME!
676   |.endif
677   |
678   |  mov KBASEa, L:RB->cframe           // Add our C frame to cframe chain.
679   |  mov SAVE_CFRAME, KBASEa
680   |  mov SAVE_PC, L:RB                  // Any value outside of bytecode is ok.
681   |.if X64
682   |  mov L:RB->cframe, rsp
683   |.else
684   |  mov L:RB->cframe, esp
685   |.endif
686   |
687   |2:  // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype).
688   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
689   |  add DISPATCH, GG_G2DISP
690   |
691   |3:  // Entry point for vm_resume above (RA = base, RB = L, PC = ftype).
692   |  set_vmstate INTERP
693   |  mov BASE, L:RB->base               // BASE = old base (used in vmeta_call).
694   |  add PC, RA
695   |  sub PC, BASE                       // PC = frame delta + frame type
696   |
697   |  mov RD, L:RB->top
698   |  sub RD, RA
699   |  shr NARGS:RD, 3
700   |  add NARGS:RD, 1                    // RD = nargs+1
701   |
702   |->vm_call_dispatch:
703   |  mov LFUNC:RB, [RA-8]
704   |  cmp dword [RA-4], LJ_TFUNC
705   |  jne ->vmeta_call                   // Ensure KBASE defined and != BASE.
706   |
707   |->vm_call_dispatch_f:
708   |  mov BASE, RA
709   |  ins_call
710   |  // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
711   |
712   |->vm_cpcall:                         // Setup protected C frame, call C.
713   |  // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
714   |  saveregs
715   |.if X64
716   |  mov L:RB, CARG1d                   // Caveat: CARG1d may be RA.
717   |  mov SAVE_L, CARG1d
718   |.else
719   |  mov L:RB, SAVE_L
720   |  // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
721   |  mov RC, INARG_CP_UD                // Get args before they are overwritten.
722   |  mov RA, INARG_CP_FUNC
723   |  mov BASE, INARG_CP_CALL
724   |.endif
725   |  mov SAVE_PC, L:RB                  // Any value outside of bytecode is ok.
726   |
727   |  mov KBASE, L:RB->stack             // Compute -savestack(L, L->top).
728   |  sub KBASE, L:RB->top
729   |  mov SAVE_ERRF, 0                   // No error function.
730   |  mov SAVE_NRES, KBASE               // Neg. delta means cframe w/o frame.
731   |  // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
732   |
733   |.if X64
734   |  mov KBASEa, L:RB->cframe           // Add our C frame to cframe chain.
735   |  mov SAVE_CFRAME, KBASEa
736   |  mov L:RB->cframe, rsp
737   |
738   |  call CARG4                 // (lua_State *L, lua_CFunction func, void *ud)
739   |.else
740   |  mov ARG3, RC                       // Have to copy args downwards.
741   |  mov ARG2, RA
742   |  mov ARG1, L:RB
743   |
744   |  mov KBASE, L:RB->cframe            // Add our C frame to cframe chain.
745   |  mov SAVE_CFRAME, KBASE
746   |  mov L:RB->cframe, esp
747   |
748   |  call BASE                  // (lua_State *L, lua_CFunction func, void *ud)
749   |.endif
750   |  // TValue * (new base) or NULL returned in eax (RC).
751   |  test RC, RC
752   |  jz ->vm_leave_cp                   // No base? Just remove C frame.
753   |  mov RA, RC
754   |  mov PC, FRAME_CP
755   |  jmp <2                             // Else continue with the call.
756   |
757   |//-----------------------------------------------------------------------
758   |//-- Metamethod handling ------------------------------------------------
759   |//-----------------------------------------------------------------------
760   |
761   |//-- Continuation dispatch ----------------------------------------------
762   |
763   |->cont_dispatch:
764   |  // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
765   |  add RA, BASE
766   |  and PC, -8
767   |  mov RB, BASE
768   |  sub BASE, PC                       // Restore caller BASE.
769   |  mov dword [RA+RD*8-4], LJ_TNIL     // Ensure one valid arg.
770   |  mov RC, RA                         // ... in [RC]
771   |  mov PC, [RB-12]                    // Restore PC from [cont|PC].
772   |.if X64
773   |  movsxd RAa, dword [RB-16]          // May be negative on WIN64 with debug.
774   |.if FFI
775   |  cmp RA, 1
776   |  jbe >1
777   |.endif
778   |  lea KBASEa, qword [=>0]
779   |  add RAa, KBASEa
780   |.else
781   |  mov RA, dword [RB-16]
782   |.if FFI
783   |  cmp RA, 1
784   |  jbe >1
785   |.endif
786   |.endif
787   |  mov LFUNC:KBASE, [BASE-8]
788   |  mov KBASE, LFUNC:KBASE->pc
789   |  mov KBASE, [KBASE+PC2PROTO(k)]
790   |  // BASE = base, RC = result, RB = meta base
791   |  jmp RAa                            // Jump to continuation.
792   |
793   |.if FFI
794   |1:
795   |  je ->cont_ffi_callback             // cont = 1: return from FFI callback.
796   |  // cont = 0: Tail call from C function.
797   |  sub RB, BASE
798   |  shr RB, 3
799   |  lea RD, [RB-1]
800   |  jmp ->vm_call_tail
801   |.endif
802   |
803   |->cont_cat:                          // BASE = base, RC = result, RB = mbase
804   |  movzx RA, PC_RB
805   |  sub RB, 16
806   |  lea RA, [BASE+RA*8]
807   |  sub RA, RB
808   |  je ->cont_ra
809   |  neg RA
810   |  shr RA, 3
811   |.if X64WIN
812   |  mov CARG3d, RA
813   |  mov L:CARG1d, SAVE_L
814   |  mov L:CARG1d->base, BASE
815   |  mov RCa, [RC]
816   |  mov [RB], RCa
817   |  mov CARG2d, RB
818   |.elif X64
819   |  mov L:CARG1d, SAVE_L
820   |  mov L:CARG1d->base, BASE
821   |  mov CARG3d, RA
822   |  mov RAa, [RC]
823   |  mov [RB], RAa
824   |  mov CARG2d, RB
825   |.else
826   |  mov ARG3, RA
827   |  mov RA, [RC+4]
828   |  mov RC, [RC]
829   |  mov [RB+4], RA
830   |  mov [RB], RC
831   |  mov ARG2, RB
832   |.endif
833   |  jmp ->BC_CAT_Z
834   |
835   |//-- Table indexing metamethods -----------------------------------------
836   |
837   |->vmeta_tgets:
838   |  mov TMP1, RC                       // RC = GCstr *
839   |  mov TMP2, LJ_TSTR
840   |  lea RCa, TMP1                      // Store temp. TValue in TMP1/TMP2.
841   |  cmp PC_OP, BC_GGET
842   |  jne >1
843   |  lea RA, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.
844   |  mov [RA], TAB:RB                   // RB = GCtab *
845   |  mov dword [RA+4], LJ_TTAB
846   |  mov RB, RA
847   |  jmp >2
848   |
849   |->vmeta_tgetb:
850   |  movzx RC, PC_RC
851   |.if DUALNUM
852   |  mov TMP2, LJ_TISNUM
853   |  mov TMP1, RC
854   |.elif SSE
855   |  cvtsi2sd xmm0, RC
856   |  movsd TMPQ, xmm0
857   |.else
858   |  mov ARG4, RC
859   |  fild ARG4
860   |  fstp TMPQ
861   |.endif
862   |  lea RCa, TMPQ                      // Store temp. TValue in TMPQ.
863   |  jmp >1
864   |
865   |->vmeta_tgetv:
866   |  movzx RC, PC_RC                    // Reload TValue *k from RC.
867   |  lea RC, [BASE+RC*8]
868   |1:
869   |  movzx RB, PC_RB                    // Reload TValue *t from RB.
870   |  lea RB, [BASE+RB*8]
871   |2:
872   |.if X64
873   |  mov L:CARG1d, SAVE_L
874   |  mov L:CARG1d->base, BASE           // Caveat: CARG2d/CARG3d may be BASE.
875   |  mov CARG2d, RB
876   |  mov CARG3, RCa                     // May be 64 bit ptr to stack.
877   |  mov L:RB, L:CARG1d
878   |.else
879   |  mov ARG2, RB
880   |  mov L:RB, SAVE_L
881   |  mov ARG3, RC
882   |  mov ARG1, L:RB
883   |  mov L:RB->base, BASE
884   |.endif
885   |  mov SAVE_PC, PC
886   |  call extern lj_meta_tget           // (lua_State *L, TValue *o, TValue *k)
887   |  // TValue * (finished) or NULL (metamethod) returned in eax (RC).
888   |  mov BASE, L:RB->base
889   |  test RC, RC
890   |  jz >3
891   |->cont_ra:                           // BASE = base, RC = result
892   |  movzx RA, PC_RA
893   |.if X64
894   |  mov RBa, [RC]
895   |  mov [BASE+RA*8], RBa
896   |.else
897   |  mov RB, [RC+4]
898   |  mov RC, [RC]
899   |  mov [BASE+RA*8+4], RB
900   |  mov [BASE+RA*8], RC
901   |.endif
902   |  ins_next
903   |
904   |3:  // Call __index metamethod.
905   |  // BASE = base, L->top = new base, stack = cont/func/t/k
906   |  mov RA, L:RB->top
907   |  mov [RA-12], PC                    // [cont|PC]
908   |  lea PC, [RA+FRAME_CONT]
909   |  sub PC, BASE
910   |  mov LFUNC:RB, [RA-8]               // Guaranteed to be a function here.
911   |  mov NARGS:RD, 2+1                  // 2 args for func(t, k).
912   |  jmp ->vm_call_dispatch_f
913   |
914   |//-----------------------------------------------------------------------
915   |
916   |->vmeta_tsets:
917   |  mov TMP1, RC                       // RC = GCstr *
918   |  mov TMP2, LJ_TSTR
919   |  lea RCa, TMP1                      // Store temp. TValue in TMP1/TMP2.
920   |  cmp PC_OP, BC_GSET
921   |  jne >1
922   |  lea RA, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.
923   |  mov [RA], TAB:RB                   // RB = GCtab *
924   |  mov dword [RA+4], LJ_TTAB
925   |  mov RB, RA
926   |  jmp >2
927   |
928   |->vmeta_tsetb:
929   |  movzx RC, PC_RC
930   |.if DUALNUM
931   |  mov TMP2, LJ_TISNUM
932   |  mov TMP1, RC
933   |.elif SSE
934   |  cvtsi2sd xmm0, RC
935   |  movsd TMPQ, xmm0
936   |.else
937   |  mov ARG4, RC
938   |  fild ARG4
939   |  fstp TMPQ
940   |.endif
941   |  lea RCa, TMPQ                      // Store temp. TValue in TMPQ.
942   |  jmp >1
943   |
944   |->vmeta_tsetv:
945   |  movzx RC, PC_RC                    // Reload TValue *k from RC.
946   |  lea RC, [BASE+RC*8]
947   |1:
948   |  movzx RB, PC_RB                    // Reload TValue *t from RB.
949   |  lea RB, [BASE+RB*8]
950   |2:
951   |.if X64
952   |  mov L:CARG1d, SAVE_L
953   |  mov L:CARG1d->base, BASE           // Caveat: CARG2d/CARG3d may be BASE.
954   |  mov CARG2d, RB
955   |  mov CARG3, RCa                     // May be 64 bit ptr to stack.
956   |  mov L:RB, L:CARG1d
957   |.else
958   |  mov ARG2, RB
959   |  mov L:RB, SAVE_L
960   |  mov ARG3, RC
961   |  mov ARG1, L:RB
962   |  mov L:RB->base, BASE
963   |.endif
964   |  mov SAVE_PC, PC
965   |  call extern lj_meta_tset           // (lua_State *L, TValue *o, TValue *k)
966   |  // TValue * (finished) or NULL (metamethod) returned in eax (RC).
967   |  mov BASE, L:RB->base
968   |  test RC, RC
969   |  jz >3
970   |  // NOBARRIER: lj_meta_tset ensures the table is not black.
971   |  movzx RA, PC_RA
972   |.if X64
973   |  mov RBa, [BASE+RA*8]
974   |  mov [RC], RBa
975   |.else
976   |  mov RB, [BASE+RA*8+4]
977   |  mov RA, [BASE+RA*8]
978   |  mov [RC+4], RB
979   |  mov [RC], RA
980   |.endif
981   |->cont_nop:                          // BASE = base, (RC = result)
982   |  ins_next
983   |
984   |3:  // Call __newindex metamethod.
985   |  // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
986   |  mov RA, L:RB->top
987   |  mov [RA-12], PC                    // [cont|PC]
988   |  movzx RC, PC_RA
989   |  // Copy value to third argument.
990   |.if X64
991   |  mov RBa, [BASE+RC*8]
992   |  mov [RA+16], RBa
993   |.else
994   |  mov RB, [BASE+RC*8+4]
995   |  mov RC, [BASE+RC*8]
996   |  mov [RA+20], RB
997   |  mov [RA+16], RC
998   |.endif
999   |  lea PC, [RA+FRAME_CONT]
1000   |  sub PC, BASE
1001   |  mov LFUNC:RB, [RA-8]               // Guaranteed to be a function here.
1002   |  mov NARGS:RD, 3+1                  // 3 args for func(t, k, v).
1003   |  jmp ->vm_call_dispatch_f
1004   |
1005   |//-- Comparison metamethods ---------------------------------------------
1006   |
1007   |->vmeta_comp:
1008   |.if X64
1009   |  mov L:RB, SAVE_L
1010   |  mov L:RB->base, BASE               // Caveat: CARG2d/CARG3d == BASE.
1011   |.if X64WIN
1012   |  lea CARG3d, [BASE+RD*8]
1013   |  lea CARG2d, [BASE+RA*8]
1014   |.else
1015   |  lea CARG2d, [BASE+RA*8]
1016   |  lea CARG3d, [BASE+RD*8]
1017   |.endif
1018   |  mov CARG1d, L:RB                   // Caveat: CARG1d/CARG4d == RA.
1019   |  movzx CARG4d, PC_OP
1020   |.else
1021   |  movzx RB, PC_OP
1022   |  lea RD, [BASE+RD*8]
1023   |  lea RA, [BASE+RA*8]
1024   |  mov ARG4, RB
1025   |  mov L:RB, SAVE_L
1026   |  mov ARG3, RD
1027   |  mov ARG2, RA
1028   |  mov ARG1, L:RB
1029   |  mov L:RB->base, BASE
1030   |.endif
1031   |  mov SAVE_PC, PC
1032   |  call extern lj_meta_comp   // (lua_State *L, TValue *o1, *o2, int op)
1033   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
1034   |3:
1035   |  mov BASE, L:RB->base
1036   |  cmp RC, 1
1037   |  ja ->vmeta_binop
1038   |4:
1039   |  lea PC, [PC+4]
1040   |  jb >6
1041   |5:
1042   |  movzx RD, PC_RD
1043   |  branchPC RD
1044   |6:
1045   |  ins_next
1046   |
1047   |->cont_condt:                        // BASE = base, RC = result
1048   |  add PC, 4
1049   |  cmp dword [RC+4], LJ_TISTRUECOND   // Branch if result is true.
1050   |  jb <5
1051   |  jmp <6
1052   |
1053   |->cont_condf:                        // BASE = base, RC = result
1054   |  cmp dword [RC+4], LJ_TISTRUECOND   // Branch if result is false.
1055   |  jmp <4
1056   |
1057   |->vmeta_equal:
1058   |  sub PC, 4
1059   |.if X64WIN
1060   |  mov CARG3d, RD
1061   |  mov CARG4d, RB
1062   |  mov L:RB, SAVE_L
1063   |  mov L:RB->base, BASE               // Caveat: CARG2d == BASE.
1064   |  mov CARG2d, RA
1065   |  mov CARG1d, L:RB                   // Caveat: CARG1d == RA.
1066   |.elif X64
1067   |  mov CARG2d, RA
1068   |  mov CARG4d, RB                     // Caveat: CARG4d == RA.
1069   |  mov L:RB, SAVE_L
1070   |  mov L:RB->base, BASE               // Caveat: CARG3d == BASE.
1071   |  mov CARG3d, RD
1072   |  mov CARG1d, L:RB
1073   |.else
1074   |  mov ARG4, RB
1075   |  mov L:RB, SAVE_L
1076   |  mov ARG3, RD
1077   |  mov ARG2, RA
1078   |  mov ARG1, L:RB
1079   |  mov L:RB->base, BASE
1080   |.endif
1081   |  mov SAVE_PC, PC
1082   |  call extern lj_meta_equal  // (lua_State *L, GCobj *o1, *o2, int ne)
1083   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
1084   |  jmp <3
1085   |
1086   |->vmeta_equal_cd:
1087   |.if FFI
1088   |  sub PC, 4
1089   |  mov L:RB, SAVE_L
1090   |  mov L:RB->base, BASE
1091   |  mov FCARG1, L:RB
1092   |  mov FCARG2, dword [PC-4]
1093   |  mov SAVE_PC, PC
1094   |  call extern lj_meta_equal_cd@8     // (lua_State *L, BCIns ins)
1095   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
1096   |  jmp <3
1097   |.endif
1098   |
1099   |//-- Arithmetic metamethods ---------------------------------------------
1100   |
1101   |->vmeta_arith_vno:
1102   |.if DUALNUM
1103   |  movzx RB, PC_RB
1104   |.endif
1105   |->vmeta_arith_vn:
1106   |  lea RC, [KBASE+RC*8]
1107   |  jmp >1
1108   |
1109   |->vmeta_arith_nvo:
1110   |.if DUALNUM
1111   |  movzx RC, PC_RC
1112   |.endif
1113   |->vmeta_arith_nv:
1114   |  lea RC, [KBASE+RC*8]
1115   |  lea RB, [BASE+RB*8]
1116   |  xchg RB, RC
1117   |  jmp >2
1118   |
1119   |->vmeta_unm:
1120   |  lea RC, [BASE+RD*8]
1121   |  mov RB, RC
1122   |  jmp >2
1123   |
1124   |->vmeta_arith_vvo:
1125   |.if DUALNUM
1126   |  movzx RB, PC_RB
1127   |.endif
1128   |->vmeta_arith_vv:
1129   |  lea RC, [BASE+RC*8]
1130   |1:
1131   |  lea RB, [BASE+RB*8]
1132   |2:
1133   |  lea RA, [BASE+RA*8]
1134   |.if X64WIN
1135   |  mov CARG3d, RB
1136   |  mov CARG4d, RC
1137   |  movzx RC, PC_OP
1138   |  mov ARG5d, RC
1139   |  mov L:RB, SAVE_L
1140   |  mov L:RB->base, BASE               // Caveat: CARG2d == BASE.
1141   |  mov CARG2d, RA
1142   |  mov CARG1d, L:RB                   // Caveat: CARG1d == RA.
1143   |.elif X64
1144   |  movzx CARG5d, PC_OP
1145   |  mov CARG2d, RA
1146   |  mov CARG4d, RC                     // Caveat: CARG4d == RA.
1147   |  mov L:CARG1d, SAVE_L
1148   |  mov L:CARG1d->base, BASE           // Caveat: CARG3d == BASE.
1149   |  mov CARG3d, RB
1150   |  mov L:RB, L:CARG1d
1151   |.else
1152   |  mov ARG3, RB
1153   |  mov L:RB, SAVE_L
1154   |  mov ARG4, RC
1155   |  movzx RC, PC_OP
1156   |  mov ARG2, RA
1157   |  mov ARG5, RC
1158   |  mov ARG1, L:RB
1159   |  mov L:RB->base, BASE
1160   |.endif
1161   |  mov SAVE_PC, PC
1162   |  call extern lj_meta_arith  // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1163   |  // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1164   |  mov BASE, L:RB->base
1165   |  test RC, RC
1166   |  jz ->cont_nop
1167   |
1168   |  // Call metamethod for binary op.
1169   |->vmeta_binop:
1170   |  // BASE = base, RC = new base, stack = cont/func/o1/o2
1171   |  mov RA, RC
1172   |  sub RC, BASE
1173   |  mov [RA-12], PC                    // [cont|PC]
1174   |  lea PC, [RC+FRAME_CONT]
1175   |  mov NARGS:RD, 2+1                  // 2 args for func(o1, o2).
1176   |  jmp ->vm_call_dispatch
1177   |
1178   |->vmeta_len:
1179   |  mov L:RB, SAVE_L
1180   |  mov L:RB->base, BASE
1181   |  lea FCARG2, [BASE+RD*8]            // Caveat: FCARG2 == BASE
1182   |  mov L:FCARG1, L:RB
1183   |  mov SAVE_PC, PC
1184   |  call extern lj_meta_len@8          // (lua_State *L, TValue *o)
1185   |  // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1186   |  mov BASE, L:RB->base
1187 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1188   |  test RC, RC
1189   |  jne ->vmeta_binop                  // Binop call for compatibility.
1190   |  movzx RD, PC_RD
1191   |  mov TAB:FCARG1, [BASE+RD*8]
1192   |  jmp ->BC_LEN_Z
1193 #else
1194   |  jmp ->vmeta_binop                  // Binop call for compatibility.
1195 #endif
1196   |
1197   |//-- Call metamethod ----------------------------------------------------
1198   |
1199   |->vmeta_call_ra:
1200   |  lea RA, [BASE+RA*8+8]
1201   |->vmeta_call:                        // Resolve and call __call metamethod.
1202   |  // BASE = old base, RA = new base, RC = nargs+1, PC = return
1203   |  mov TMP2, RA                       // Save RA, RC for us.
1204   |  mov TMP1, NARGS:RD
1205   |  sub RA, 8
1206   |.if X64
1207   |  mov L:RB, SAVE_L
1208   |  mov L:RB->base, BASE               // Caveat: CARG2d/CARG3d may be BASE.
1209   |  mov CARG2d, RA
1210   |  lea CARG3d, [RA+NARGS:RD*8]
1211   |  mov CARG1d, L:RB                   // Caveat: CARG1d may be RA.
1212   |.else
1213   |  lea RC, [RA+NARGS:RD*8]
1214   |  mov L:RB, SAVE_L
1215   |  mov ARG2, RA
1216   |  mov ARG3, RC
1217   |  mov ARG1, L:RB
1218   |  mov L:RB->base, BASE               // This is the callers base!
1219   |.endif
1220   |  mov SAVE_PC, PC
1221   |  call extern lj_meta_call   // (lua_State *L, TValue *func, TValue *top)
1222   |  mov BASE, L:RB->base
1223   |  mov RA, TMP2
1224   |  mov NARGS:RD, TMP1
1225   |  mov LFUNC:RB, [RA-8]
1226   |  add NARGS:RD, 1
1227   |  // This is fragile. L->base must not move, KBASE must always be defined.
1228   |  cmp KBASE, BASE                    // Continue with CALLT if flag set.
1229   |  je ->BC_CALLT_Z
1230   |  mov BASE, RA
1231   |  ins_call                           // Otherwise call resolved metamethod.
1232   |
1233   |//-- Argument coercion for 'for' statement ------------------------------
1234   |
1235   |->vmeta_for:
1236   |  mov L:RB, SAVE_L
1237   |  mov L:RB->base, BASE
1238   |  mov FCARG2, RA                     // Caveat: FCARG2 == BASE
1239   |  mov L:FCARG1, L:RB                 // Caveat: FCARG1 == RA
1240   |  mov SAVE_PC, PC
1241   |  call extern lj_meta_for@8  // (lua_State *L, TValue *base)
1242   |  mov BASE, L:RB->base
1243   |  mov RC, [PC-4]
1244   |  movzx RA, RCH
1245   |  movzx OP, RCL
1246   |  shr RC, 16
1247   |.if X64
1248   |  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]   // Retry FORI or JFORI.
1249   |.else
1250   |  jmp aword [DISPATCH+OP*4+GG_DISP2STATIC]   // Retry FORI or JFORI.
1251   |.endif
1252   |
1253   |//-----------------------------------------------------------------------
1254   |//-- Fast functions -----------------------------------------------------
1255   |//-----------------------------------------------------------------------
1256   |
1257   |.macro .ffunc, name
1258   |->ff_ .. name:
1259   |.endmacro
1260   |
1261   |.macro .ffunc_1, name
1262   |->ff_ .. name:
1263   |  cmp NARGS:RD, 1+1;  jb ->fff_fallback
1264   |.endmacro
1265   |
1266   |.macro .ffunc_2, name
1267   |->ff_ .. name:
1268   |  cmp NARGS:RD, 2+1;  jb ->fff_fallback
1269   |.endmacro
1270   |
1271   |.macro .ffunc_n, name
1272   |  .ffunc_1 name
1273   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1274   |  fld qword [BASE]
1275   |.endmacro
1276   |
1277   |.macro .ffunc_n, name, op
1278   |  .ffunc_1 name
1279   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1280   |  op
1281   |  fld qword [BASE]
1282   |.endmacro
1283   |
1284   |.macro .ffunc_nsse, name, op
1285   |  .ffunc_1 name
1286   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1287   |  op xmm0, qword [BASE]
1288   |.endmacro
1289   |
1290   |.macro .ffunc_nsse, name
1291   |  .ffunc_nsse name, movsd
1292   |.endmacro
1293   |
1294   |.macro .ffunc_nn, name
1295   |  .ffunc_2 name
1296   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1297   |  cmp dword [BASE+12], LJ_TISNUM;  jae ->fff_fallback
1298   |  fld qword [BASE]
1299   |  fld qword [BASE+8]
1300   |.endmacro
1301   |
1302   |.macro .ffunc_nnsse, name
1303   |  .ffunc_2 name
1304   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1305   |  cmp dword [BASE+12], LJ_TISNUM;  jae ->fff_fallback
1306   |  movsd xmm0, qword [BASE]
1307   |  movsd xmm1, qword [BASE+8]
1308   |.endmacro
1309   |
1310   |.macro .ffunc_nnr, name
1311   |  .ffunc_2 name
1312   |  cmp dword [BASE+4], LJ_TISNUM;  jae ->fff_fallback
1313   |  cmp dword [BASE+12], LJ_TISNUM;  jae ->fff_fallback
1314   |  fld qword [BASE+8]
1315   |  fld qword [BASE]
1316   |.endmacro
1317   |
1318   |// Inlined GC threshold check. Caveat: uses label 1.
1319   |.macro ffgccheck
1320   |  mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1321   |  cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1322   |  jb >1
1323   |  call ->fff_gcstep
1324   |1:
1325   |.endmacro
1326   |
1327   |//-- Base library: checks -----------------------------------------------
1328   |
1329   |.ffunc_1 assert
1330   |  mov RB, [BASE+4]
1331   |  cmp RB, LJ_TISTRUECOND;  jae ->fff_fallback
1332   |  mov PC, [BASE-4]
1333   |  mov MULTRES, RD
1334   |  mov [BASE-4], RB
1335   |  mov RB, [BASE]
1336   |  mov [BASE-8], RB
1337   |  sub RD, 2
1338   |  jz >2
1339   |  mov RA, BASE
1340   |1:
1341   |  add RA, 8
1342   |.if X64
1343   |  mov RBa, [RA]
1344   |  mov [RA-8], RBa
1345   |.else
1346   |  mov RB, [RA+4]
1347   |  mov [RA-4], RB
1348   |  mov RB, [RA]
1349   |  mov [RA-8], RB
1350   |.endif
1351   |  sub RD, 1
1352   |  jnz <1
1353   |2:
1354   |  mov RD, MULTRES
1355   |  jmp ->fff_res_
1356   |
1357   |.ffunc_1 type
1358   |  mov RB, [BASE+4]
1359   |.if X64
1360   |  mov RA, RB
1361   |  sar RA, 15
1362   |  cmp RA, -2
1363   |  je >3
1364   |.endif
1365   |  mov RC, ~LJ_TNUMX
1366   |  not RB
1367   |  cmp RC, RB
1368   |  cmova RC, RB
1369   |2:
1370   |  mov CFUNC:RB, [BASE-8]
1371   |  mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1372   |  mov PC, [BASE-4]
1373   |  mov dword [BASE-4], LJ_TSTR
1374   |  mov [BASE-8], STR:RC
1375   |  jmp ->fff_res1
1376   |.if X64
1377   |3:
1378   |  mov RC, ~LJ_TLIGHTUD
1379   |  jmp <2
1380   |.endif
1381   |
1382   |//-- Base library: getters and setters ---------------------------------
1383   |
1384   |.ffunc_1 getmetatable
1385   |  mov RB, [BASE+4]
1386   |  mov PC, [BASE-4]
1387   |  cmp RB, LJ_TTAB;  jne >6
1388   |1:  // Field metatable must be at same offset for GCtab and GCudata!
1389   |  mov TAB:RB, [BASE]
1390   |  mov TAB:RB, TAB:RB->metatable
1391   |2:
1392   |  test TAB:RB, TAB:RB
1393   |  mov dword [BASE-4], LJ_TNIL
1394   |  jz ->fff_res1
1395   |  mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1396   |  mov dword [BASE-4], LJ_TTAB        // Store metatable as default result.
1397   |  mov [BASE-8], TAB:RB
1398   |  mov RA, TAB:RB->hmask
1399   |  and RA, STR:RC->hash
1400   |  imul RA, #NODE
1401   |  add NODE:RA, TAB:RB->node
1402   |3:  // Rearranged logic, because we expect _not_ to find the key.
1403   |  cmp dword NODE:RA->key.it, LJ_TSTR
1404   |  jne >4
1405   |  cmp dword NODE:RA->key.gcr, STR:RC
1406   |  je >5
1407   |4:
1408   |  mov NODE:RA, NODE:RA->next
1409   |  test NODE:RA, NODE:RA
1410   |  jnz <3
1411   |  jmp ->fff_res1                     // Not found, keep default result.
1412   |5:
1413   |  mov RB, [RA+4]
1414   |  cmp RB, LJ_TNIL;  je ->fff_res1    // Ditto for nil value.
1415   |  mov RC, [RA]
1416   |  mov [BASE-4], RB                   // Return value of mt.__metatable.
1417   |  mov [BASE-8], RC
1418   |  jmp ->fff_res1
1419   |
1420   |6:
1421   |  cmp RB, LJ_TUDATA;  je <1
1422   |.if X64
1423   |  cmp RB, LJ_TNUMX;  ja >8
1424   |  cmp RB, LJ_TISNUM;  jbe >7
1425   |  mov RB, LJ_TLIGHTUD
1426   |  jmp >8
1427   |7:
1428   |.else
1429   |  cmp RB, LJ_TISNUM;  ja >8
1430   |.endif
1431   |  mov RB, LJ_TNUMX
1432   |8:
1433   |  not RB
1434   |  mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1435   |  jmp <2
1436   |
1437   |.ffunc_2 setmetatable
1438   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1439   |  // Fast path: no mt for table yet and not clearing the mt.
1440   |  mov TAB:RB, [BASE]
1441   |  cmp dword TAB:RB->metatable, 0;  jne ->fff_fallback
1442   |  cmp dword [BASE+12], LJ_TTAB;  jne ->fff_fallback
1443   |  mov TAB:RC, [BASE+8]
1444   |  mov TAB:RB->metatable, TAB:RC
1445   |  mov PC, [BASE-4]
1446   |  mov dword [BASE-4], LJ_TTAB                // Return original table.
1447   |  mov [BASE-8], TAB:RB
1448   |  test byte TAB:RB->marked, LJ_GC_BLACK      // isblack(table)
1449   |  jz >1
1450   |  // Possible write barrier. Table is black, but skip iswhite(mt) check.
1451   |  barrierback TAB:RB, RC
1452   |1:
1453   |  jmp ->fff_res1
1454   |
1455   |.ffunc_2 rawget
1456   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1457   |.if X64WIN
1458   |  mov RB, BASE                       // Save BASE.
1459   |  lea CARG3d, [BASE+8]
1460   |  mov CARG2d, [BASE]                 // Caveat: CARG2d == BASE.
1461   |  mov CARG1d, SAVE_L
1462   |.elif X64
1463   |  mov RB, BASE                       // Save BASE.
1464   |  mov CARG2d, [BASE]
1465   |  lea CARG3d, [BASE+8]               // Caveat: CARG3d == BASE.
1466   |  mov CARG1d, SAVE_L
1467   |.else
1468   |  mov TAB:RD, [BASE]
1469   |  mov L:RB, SAVE_L
1470   |  mov ARG2, TAB:RD
1471   |  mov ARG1, L:RB
1472   |  mov RB, BASE                       // Save BASE.
1473   |  add BASE, 8
1474   |  mov ARG3, BASE
1475   |.endif
1476   |  call extern lj_tab_get     // (lua_State *L, GCtab *t, cTValue *key)
1477   |  // cTValue * returned in eax (RD).
1478   |  mov BASE, RB                       // Restore BASE.
1479   |  // Copy table slot.
1480   |.if X64
1481   |  mov RBa, [RD]
1482   |  mov PC, [BASE-4]
1483   |  mov [BASE-8], RBa
1484   |.else
1485   |  mov RB, [RD]
1486   |  mov RD, [RD+4]
1487   |  mov PC, [BASE-4]
1488   |  mov [BASE-8], RB
1489   |  mov [BASE-4], RD
1490   |.endif
1491   |  jmp ->fff_res1
1492   |
1493   |//-- Base library: conversions ------------------------------------------
1494   |
1495   |.ffunc tonumber
1496   |  // Only handles the number case inline (without a base argument).
1497   |  cmp NARGS:RD, 1+1;  jne ->fff_fallback     // Exactly one argument.
1498   |  cmp dword [BASE+4], LJ_TISNUM
1499   |.if DUALNUM
1500   |  jne >1
1501   |  mov RB, dword [BASE]; jmp ->fff_resi
1502   |1:
1503   |  ja ->fff_fallback
1504   |.else
1505   |  jae ->fff_fallback
1506   |.endif
1507   |.if SSE
1508   |  movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1509   |.else
1510   |  fld qword [BASE]; jmp ->fff_resn
1511   |.endif
1512   |
1513   |.ffunc_1 tostring
1514   |  // Only handles the string or number case inline.
1515   |  mov PC, [BASE-4]
1516   |  cmp dword [BASE+4], LJ_TSTR;  jne >3
1517   |  // A __tostring method in the string base metatable is ignored.
1518   |  mov STR:RD, [BASE]
1519   |2:
1520   |  mov dword [BASE-4], LJ_TSTR
1521   |  mov [BASE-8], STR:RD
1522   |  jmp ->fff_res1
1523   |3:  // Handle numbers inline, unless a number base metatable is present.
1524   |  cmp dword [BASE+4], LJ_TISNUM;  ja ->fff_fallback
1525   |  cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1526   |  jne ->fff_fallback
1527   |  ffgccheck                          // Caveat: uses label 1.
1528   |  mov L:RB, SAVE_L
1529   |  mov L:RB->base, BASE               // Add frame since C call can throw.
1530   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
1531   |.if X64 and not X64WIN
1532   |  mov FCARG2, BASE                   // Otherwise: FCARG2 == BASE
1533   |.endif
1534   |  mov L:FCARG1, L:RB
1535   |.if DUALNUM
1536   |  call extern lj_str_fromnumber@8    // (lua_State *L, cTValue *o)
1537   |.else
1538   |  call extern lj_str_fromnum@8       // (lua_State *L, lua_Number *np)
1539   |.endif
1540   |  // GCstr returned in eax (RD).
1541   |  mov BASE, L:RB->base
1542   |  jmp <2
1543   |
1544   |//-- Base library: iterators -------------------------------------------
1545   |
1546   |.ffunc_1 next
1547   |  je >2                              // Missing 2nd arg?
1548   |1:
1549   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1550   |  mov L:RB, SAVE_L
1551   |  mov L:RB->base, BASE               // Add frame since C call can throw.
1552   |  mov L:RB->top, BASE                // Dummy frame length is ok.
1553   |  mov PC, [BASE-4]
1554   |.if X64WIN
1555   |  lea CARG3d, [BASE+8]
1556   |  mov CARG2d, [BASE]                 // Caveat: CARG2d == BASE.
1557   |  mov CARG1d, L:RB
1558   |.elif X64
1559   |  mov CARG2d, [BASE]
1560   |  lea CARG3d, [BASE+8]               // Caveat: CARG3d == BASE.
1561   |  mov CARG1d, L:RB
1562   |.else
1563   |  mov TAB:RD, [BASE]
1564   |  mov ARG2, TAB:RD
1565   |  mov ARG1, L:RB
1566   |  add BASE, 8
1567   |  mov ARG3, BASE
1568   |.endif
1569   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
1570   |  call extern lj_tab_next    // (lua_State *L, GCtab *t, TValue *key)
1571   |  // Flag returned in eax (RD).
1572   |  mov BASE, L:RB->base
1573   |  test RD, RD;  jz >3                // End of traversal?
1574   |  // Copy key and value to results.
1575   |.if X64
1576   |  mov RBa, [BASE+8]
1577   |  mov RDa, [BASE+16]
1578   |  mov [BASE-8], RBa
1579   |  mov [BASE], RDa
1580   |.else
1581   |  mov RB, [BASE+8]
1582   |  mov RD, [BASE+12]
1583   |  mov [BASE-8], RB
1584   |  mov [BASE-4], RD
1585   |  mov RB, [BASE+16]
1586   |  mov RD, [BASE+20]
1587   |  mov [BASE], RB
1588   |  mov [BASE+4], RD
1589   |.endif
1590   |->fff_res2:
1591   |  mov RD, 1+2
1592   |  jmp ->fff_res
1593   |2:  // Set missing 2nd arg to nil.
1594   |  mov dword [BASE+12], LJ_TNIL
1595   |  jmp <1
1596   |3:  // End of traversal: return nil.
1597   |  mov dword [BASE-4], LJ_TNIL
1598   |  jmp ->fff_res1
1599   |
1600   |.ffunc_1 pairs
1601   |  mov TAB:RB, [BASE]
1602   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1603 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1604   |  cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1605 #endif
1606   |  mov CFUNC:RB, [BASE-8]
1607   |  mov CFUNC:RD, CFUNC:RB->upvalue[0]
1608   |  mov PC, [BASE-4]
1609   |  mov dword [BASE-4], LJ_TFUNC
1610   |  mov [BASE-8], CFUNC:RD
1611   |  mov dword [BASE+12], LJ_TNIL
1612   |  mov RD, 1+3
1613   |  jmp ->fff_res
1614   |
1615   |.ffunc_1 ipairs_aux
1616   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1617   |  cmp dword [BASE+12], LJ_TISNUM
1618   |.if DUALNUM
1619   |  jne ->fff_fallback
1620   |.else
1621   |  jae ->fff_fallback
1622   |.endif
1623   |  mov PC, [BASE-4]
1624   |.if DUALNUM
1625   |  mov RD, dword [BASE+8]
1626   |  add RD, 1
1627   |  mov dword [BASE-4], LJ_TISNUM
1628   |  mov dword [BASE-8], RD
1629   |.elif SSE
1630   |  movsd xmm0, qword [BASE+8]
1631   |  sseconst_1 xmm1, RBa
1632   |  addsd xmm0, xmm1
1633   |  cvtsd2si RD, xmm0
1634   |  movsd qword [BASE-8], xmm0
1635   |.else
1636   |  fld qword [BASE+8]
1637   |  fld1
1638   |  faddp st1
1639   |  fist ARG1
1640   |  fstp qword [BASE-8]
1641   |  mov RD, ARG1
1642   |.endif
1643   |  mov TAB:RB, [BASE]
1644   |  cmp RD, TAB:RB->asize;  jae >2     // Not in array part?
1645   |  shl RD, 3
1646   |  add RD, TAB:RB->array
1647   |1:
1648   |  cmp dword [RD+4], LJ_TNIL;  je ->fff_res0
1649   |  // Copy array slot.
1650   |.if X64
1651   |  mov RBa, [RD]
1652   |  mov [BASE], RBa
1653   |.else
1654   |  mov RB, [RD]
1655   |  mov RD, [RD+4]
1656   |  mov [BASE], RB
1657   |  mov [BASE+4], RD
1658   |.endif
1659   |  jmp ->fff_res2
1660   |2:  // Check for empty hash part first. Otherwise call C function.
1661   |  cmp dword TAB:RB->hmask, 0; je ->fff_res0
1662   |  mov FCARG1, TAB:RB
1663   |  mov RB, BASE                       // Save BASE.
1664   |  mov FCARG2, RD                     // Caveat: FCARG2 == BASE
1665   |  call extern lj_tab_getinth@8       // (GCtab *t, int32_t key)
1666   |  // cTValue * or NULL returned in eax (RD).
1667   |  mov BASE, RB
1668   |  test RD, RD
1669   |  jnz <1
1670   |->fff_res0:
1671   |  mov RD, 1+0
1672   |  jmp ->fff_res
1673   |
1674   |.ffunc_1 ipairs
1675   |  mov TAB:RB, [BASE]
1676   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
1677 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1678   |  cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1679 #endif
1680   |  mov CFUNC:RB, [BASE-8]
1681   |  mov CFUNC:RD, CFUNC:RB->upvalue[0]
1682   |  mov PC, [BASE-4]
1683   |  mov dword [BASE-4], LJ_TFUNC
1684   |  mov [BASE-8], CFUNC:RD
1685   |.if DUALNUM
1686   |  mov dword [BASE+12], LJ_TISNUM
1687   |  mov dword [BASE+8], 0
1688   |.elif SSE
1689   |  xorps xmm0, xmm0
1690   |  movsd qword [BASE+8], xmm0
1691   |.else
1692   |  fldz
1693   |  fstp qword [BASE+8]
1694   |.endif
1695   |  mov RD, 1+3
1696   |  jmp ->fff_res
1697   |
1698   |//-- Base library: catch errors ----------------------------------------
1699   |
1700   |.ffunc_1 pcall
1701   |  lea RA, [BASE+8]
1702   |  sub NARGS:RD, 1
1703   |  mov PC, 8+FRAME_PCALL
1704   |1:
1705   |  movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1706   |  shr RB, HOOK_ACTIVE_SHIFT
1707   |  and RB, 1
1708   |  add PC, RB                         // Remember active hook before pcall.
1709   |  jmp ->vm_call_dispatch
1710   |
1711   |.ffunc_2 xpcall
1712   |  cmp dword [BASE+12], LJ_TFUNC;  jne ->fff_fallback
1713   |  mov RB, [BASE+4]                   // Swap function and traceback.
1714   |  mov [BASE+12], RB
1715   |  mov dword [BASE+4], LJ_TFUNC
1716   |  mov LFUNC:RB, [BASE]
1717   |  mov PC, [BASE+8]
1718   |  mov [BASE+8], LFUNC:RB
1719   |  mov [BASE], PC
1720   |  lea RA, [BASE+16]
1721   |  sub NARGS:RD, 2
1722   |  mov PC, 16+FRAME_PCALL
1723   |  jmp <1
1724   |
1725   |//-- Coroutine library --------------------------------------------------
1726   |
1727   |.macro coroutine_resume_wrap, resume
1728   |.if resume
1729   |.ffunc_1 coroutine_resume
1730   |  mov L:RB, [BASE]
1731   |.else
1732   |.ffunc coroutine_wrap_aux
1733   |  mov CFUNC:RB, [BASE-8]
1734   |  mov L:RB, CFUNC:RB->upvalue[0].gcr
1735   |.endif
1736   |  mov PC, [BASE-4]
1737   |  mov SAVE_PC, PC
1738   |.if X64
1739   |  mov TMP1, L:RB
1740   |.else
1741   |  mov ARG1, L:RB
1742   |.endif
1743   |.if resume
1744   |  cmp dword [BASE+4], LJ_TTHREAD;  jne ->fff_fallback
1745   |.endif
1746   |  cmp aword L:RB->cframe, 0; jne ->fff_fallback
1747   |  cmp byte L:RB->status, LUA_YIELD;  ja ->fff_fallback
1748   |  mov RA, L:RB->top
1749   |  je >1                              // Status != LUA_YIELD (i.e. 0)?
1750   |  cmp RA, L:RB->base                 // Check for presence of initial func.
1751   |  je ->fff_fallback
1752   |1:
1753   |.if resume
1754   |  lea PC, [RA+NARGS:RD*8-16]         // Check stack space (-1-thread).
1755   |.else
1756   |  lea PC, [RA+NARGS:RD*8-8]          // Check stack space (-1).
1757   |.endif
1758   |  cmp PC, L:RB->maxstack; ja ->fff_fallback
1759   |  mov L:RB->top, PC
1760   |
1761   |  mov L:RB, SAVE_L
1762   |  mov L:RB->base, BASE
1763   |.if resume
1764   |  add BASE, 8                        // Keep resumed thread in stack for GC.
1765   |.endif
1766   |  mov L:RB->top, BASE
1767   |.if resume
1768   |  lea RB, [BASE+NARGS:RD*8-24]       // RB = end of source for stack move.
1769   |.else
1770   |  lea RB, [BASE+NARGS:RD*8-16]       // RB = end of source for stack move.
1771   |.endif
1772   |  sub RBa, PCa                       // Relative to PC.
1773   |
1774   |  cmp PC, RA
1775   |  je >3
1776   |2:  // Move args to coroutine.
1777   |.if X64
1778   |  mov RCa, [PC+RB]
1779   |  mov [PC-8], RCa
1780   |.else
1781   |  mov RC, [PC+RB+4]
1782   |  mov [PC-4], RC
1783   |  mov RC, [PC+RB]
1784   |  mov [PC-8], RC
1785   |.endif
1786   |  sub PC, 8
1787   |  cmp PC, RA
1788   |  jne <2
1789   |3:
1790   |.if X64
1791   |  mov CARG2d, RA
1792   |  mov CARG1d, TMP1
1793   |.else
1794   |  mov ARG2, RA
1795   |  xor RA, RA
1796   |  mov ARG4, RA
1797   |  mov ARG3, RA
1798   |.endif
1799   |  call ->vm_resume                   // (lua_State *L, TValue *base, 0, 0)
1800   |  set_vmstate INTERP
1801   |
1802   |  mov L:RB, SAVE_L
1803   |.if X64
1804   |  mov L:PC, TMP1
1805   |.else
1806   |  mov L:PC, ARG1                     // The callee doesn't modify SAVE_L.
1807   |.endif
1808   |  mov BASE, L:RB->base
1809   |  cmp eax, LUA_YIELD
1810   |  ja >8
1811   |4:
1812   |  mov RA, L:PC->base
1813   |  mov KBASE, L:PC->top
1814   |  mov L:PC->top, RA                  // Clear coroutine stack.
1815   |  mov PC, KBASE
1816   |  sub PC, RA
1817   |  je >6                              // No results?
1818   |  lea RD, [BASE+PC]
1819   |  shr PC, 3
1820   |  cmp RD, L:RB->maxstack
1821   |  ja >9                              // Need to grow stack?
1822   |
1823   |  mov RB, BASE
1824   |  sub RBa, RAa
1825   |5:  // Move results from coroutine.
1826   |.if X64
1827   |  mov RDa, [RA]
1828   |  mov [RA+RB], RDa
1829   |.else
1830   |  mov RD, [RA]
1831   |  mov [RA+RB], RD
1832   |  mov RD, [RA+4]
1833   |  mov [RA+RB+4], RD
1834   |.endif
1835   |  add RA, 8
1836   |  cmp RA, KBASE
1837   |  jne <5
1838   |6:
1839   |.if resume
1840   |  lea RD, [PC+2]                     // nresults+1 = 1 + true + results.
1841   |  mov dword [BASE-4], LJ_TTRUE       // Prepend true to results.
1842   |.else
1843   |  lea RD, [PC+1]                     // nresults+1 = 1 + results.
1844   |.endif
1845   |7:
1846   |  mov PC, SAVE_PC
1847   |  mov MULTRES, RD
1848   |.if resume
1849   |  mov RAa, -8
1850   |.else
1851   |  xor RA, RA
1852   |.endif
1853   |  test PC, FRAME_TYPE
1854   |  jz ->BC_RET_Z
1855   |  jmp ->vm_return
1856   |
1857   |8:  // Coroutine returned with error (at co->top-1).
1858   |.if resume
1859   |  mov dword [BASE-4], LJ_TFALSE      // Prepend false to results.
1860   |  mov RA, L:PC->top
1861   |  sub RA, 8
1862   |  mov L:PC->top, RA                  // Clear error from coroutine stack.
1863   |  // Copy error message.
1864   |.if X64
1865   |  mov RDa, [RA]
1866   |  mov [BASE], RDa
1867   |.else
1868   |  mov RD, [RA]
1869   |  mov [BASE], RD
1870   |  mov RD, [RA+4]
1871   |  mov [BASE+4], RD
1872   |.endif
1873   |  mov RD, 1+2                        // nresults+1 = 1 + false + error.
1874   |  jmp <7
1875   |.else
1876   |  mov FCARG2, L:PC
1877   |  mov FCARG1, L:RB
1878   |  call extern lj_ffh_coroutine_wrap_err@8  // (lua_State *L, lua_State *co)
1879   |  // Error function does not return.
1880   |.endif
1881   |
1882   |9:  // Handle stack expansion on return from yield.
1883   |.if X64
1884   |  mov L:RA, TMP1
1885   |.else
1886   |  mov L:RA, ARG1                     // The callee doesn't modify SAVE_L.
1887   |.endif
1888   |  mov L:RA->top, KBASE               // Undo coroutine stack clearing.
1889   |  mov FCARG2, PC
1890   |  mov FCARG1, L:RB
1891   |  call extern lj_state_growstack@8   // (lua_State *L, int n)
1892   |.if X64
1893   |  mov L:PC, TMP1
1894   |.else
1895   |  mov L:PC, ARG1
1896   |.endif
1897   |  mov BASE, L:RB->base
1898   |  jmp <4                             // Retry the stack move.
1899   |.endmacro
1900   |
1901   |  coroutine_resume_wrap 1            // coroutine.resume
1902   |  coroutine_resume_wrap 0            // coroutine.wrap
1903   |
1904   |.ffunc coroutine_yield
1905   |  mov L:RB, SAVE_L
1906   |  test aword L:RB->cframe, CFRAME_RESUME
1907   |  jz ->fff_fallback
1908   |  mov L:RB->base, BASE
1909   |  lea RD, [BASE+NARGS:RD*8-8]
1910   |  mov L:RB->top, RD
1911   |  xor RD, RD
1912   |  mov aword L:RB->cframe, RDa
1913   |  mov al, LUA_YIELD
1914   |  mov byte L:RB->status, al
1915   |  jmp ->vm_leave_unw
1916   |
1917   |//-- Math library -------------------------------------------------------
1918   |
1919   |.if not DUALNUM
1920   |->fff_resi:  // Dummy.
1921   |.endif
1922   |
1923   |.if SSE
1924   |->fff_resn:
1925   |  mov PC, [BASE-4]
1926   |  fstp qword [BASE-8]
1927   |  jmp ->fff_res1
1928   |.endif
1929   |
1930   |  .ffunc_1 math_abs
1931   |.if DUALNUM
1932   |  cmp dword [BASE+4], LJ_TISNUM; jne >2
1933   |  mov RB, dword [BASE]
1934   |  cmp RB, 0; jns ->fff_resi
1935   |  neg RB; js >1
1936   |->fff_resbit:
1937   |->fff_resi:
1938   |  mov PC, [BASE-4]
1939   |  mov dword [BASE-4], LJ_TISNUM
1940   |  mov dword [BASE-8], RB
1941   |  jmp ->fff_res1
1942   |1:
1943   |  mov PC, [BASE-4]
1944   |  mov dword [BASE-4], 0x41e00000  // 2^31.
1945   |  mov dword [BASE-8], 0
1946   |  jmp ->fff_res1
1947   |2:
1948   |  ja ->fff_fallback
1949   |.else
1950   |  cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1951   |.endif
1952   |
1953   |.if SSE
1954   |  movsd xmm0, qword [BASE]
1955   |  sseconst_abs xmm1, RDa
1956   |  andps xmm0, xmm1
1957   |->fff_resxmm0:
1958   |  mov PC, [BASE-4]
1959   |  movsd qword [BASE-8], xmm0
1960   |  // fallthrough
1961   |.else
1962   |  fld qword [BASE]
1963   |  fabs
1964   |  // fallthrough
1965   |->fff_resxmm0:  // Dummy.
1966   |->fff_resn:
1967   |  mov PC, [BASE-4]
1968   |  fstp qword [BASE-8]
1969   |.endif
1970   |
1971   |->fff_res1:
1972   |  mov RD, 1+1
1973   |->fff_res:
1974   |  mov MULTRES, RD
1975   |->fff_res_:
1976   |  test PC, FRAME_TYPE
1977   |  jnz >7
1978   |5:
1979   |  cmp PC_RB, RDL                     // More results expected?
1980   |  ja >6
1981   |  // Adjust BASE. KBASE is assumed to be set for the calling frame.
1982   |  movzx RA, PC_RA
1983   |  not RAa                            // Note: ~RA = -(RA+1)
1984   |  lea BASE, [BASE+RA*8]              // base = base - (RA+1)*8
1985   |  ins_next
1986   |
1987   |6:  // Fill up results with nil.
1988   |  mov dword [BASE+RD*8-12], LJ_TNIL
1989   |  add RD, 1
1990   |  jmp <5
1991   |
1992   |7:  // Non-standard return case.
1993   |  mov RAa, -8                        // Results start at BASE+RA = BASE-8.
1994   |  jmp ->vm_return
1995   |
1996   |.macro math_round, func
1997   |  .ffunc math_ .. func
1998   |.if DUALNUM
1999   |  cmp dword [BASE+4], LJ_TISNUM; jne >1
2000   |  mov RB, dword [BASE]; jmp ->fff_resi
2001   |1:
2002   |  ja ->fff_fallback
2003   |.else
2004   |  cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2005   |.endif
2006   |.if SSE
2007   |  movsd xmm0, qword [BASE]
2008   |  call ->vm_ .. func
2009   |  .if DUALNUM
2010   |    cvtsd2si RB, xmm0
2011   |    cmp RB, 0x80000000
2012   |    jne ->fff_resi
2013   |    cvtsi2sd xmm1, RB
2014   |    ucomisd xmm0, xmm1
2015   |    jp ->fff_resxmm0
2016   |    je ->fff_resi
2017   |  .endif
2018   |  jmp ->fff_resxmm0
2019   |.else
2020   |  fld qword [BASE]
2021   |  call ->vm_ .. func
2022   |  .if DUALNUM
2023   |    fist ARG1
2024   |    mov RB, ARG1
2025   |    cmp RB, 0x80000000; jne >2
2026   |    fdup
2027   |    fild ARG1
2028   |    fcomparepp
2029   |    jp ->fff_resn
2030   |    jne ->fff_resn
2031   |2:
2032   |    fpop
2033   |    jmp ->fff_resi
2034   | .else
2035   |    jmp ->fff_resn
2036   | .endif
2037   |.endif
2038   |.endmacro
2039   |
2040   |  math_round floor
2041   |  math_round ceil
2042   |
2043   |.if SSE
2044   |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2045   |.else
2046   |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
2047   |.endif
2048   |.ffunc_n math_log, fldln2;   fyl2x;          jmp ->fff_resn
2049   |.ffunc_n math_log10, fldlg2; fyl2x;          jmp ->fff_resn
2050   |.ffunc_n math_exp;   call ->vm_exp_x87;      jmp ->fff_resn
2051   |
2052   |.ffunc_n math_sin;   fsin;                   jmp ->fff_resn
2053   |.ffunc_n math_cos;   fcos;                   jmp ->fff_resn
2054   |.ffunc_n math_tan;   fptan; fpop;            jmp ->fff_resn
2055   |
2056   |.ffunc_n math_asin
2057   |  fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan
2058   |  jmp ->fff_resn
2059   |.ffunc_n math_acos
2060   |  fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan
2061   |  jmp ->fff_resn
2062   |.ffunc_n math_atan;  fld1; fpatan;           jmp ->fff_resn
2063   |
2064   |.macro math_extern, func
2065   |.if SSE
2066   |  .ffunc_nsse math_ .. func
2067   |  .if not X64
2068   |    movsd FPARG1, xmm0
2069   |  .endif
2070   |.else
2071   |  .ffunc_n math_ .. func
2072   |  fstp FPARG1
2073   |.endif
2074   |  mov RB, BASE
2075   |  call extern lj_vm_ .. func
2076   |  mov BASE, RB
2077   |  .if X64
2078   |    jmp ->fff_resxmm0
2079   |  .else
2080   |    jmp ->fff_resn
2081   |  .endif
2082   |.endmacro
2083   |
2084   |  math_extern sinh
2085   |  math_extern cosh
2086   |  math_extern tanh
2087   |
2088   |->ff_math_deg:
2089   |.if SSE
2090   |.ffunc_nsse math_rad
2091   |  mov CFUNC:RB, [BASE-8]
2092   |  mulsd xmm0, qword CFUNC:RB->upvalue[0]
2093   |  jmp ->fff_resxmm0
2094   |.else
2095   |.ffunc_n math_rad
2096   |  mov CFUNC:RB, [BASE-8]
2097   |  fmul qword CFUNC:RB->upvalue[0]
2098   |  jmp ->fff_resn
2099   |.endif
2100   |
2101   |.ffunc_nn math_atan2;        fpatan;         jmp ->fff_resn
2102   |.ffunc_nnr math_ldexp;       fscale; fpop1;  jmp ->fff_resn
2103   |
2104   |.ffunc_1 math_frexp
2105   |  mov RB, [BASE+4]
2106   |  cmp RB, LJ_TISNUM;  jae ->fff_fallback
2107   |  mov PC, [BASE-4]
2108   |  mov RC, [BASE]
2109   |  mov [BASE-4], RB; mov [BASE-8], RC
2110   |  shl RB, 1; cmp RB, 0xffe00000; jae >3
2111   |  or RC, RB; jz >3
2112   |  mov RC, 1022
2113   |  cmp RB, 0x00200000; jb >4
2114   |1:
2115   |  shr RB, 21; sub RB, RC             // Extract and unbias exponent.
2116   |.if SSE
2117   |  cvtsi2sd xmm0, RB
2118   |.else
2119   |  mov TMP1, RB; fild TMP1
2120   |.endif
2121   |  mov RB, [BASE-4]
2122   |  and RB, 0x800fffff                 // Mask off exponent.
2123   |  or RB, 0x3fe00000                  // Put mantissa in range [0.5,1) or 0.
2124   |  mov [BASE-4], RB
2125   |2:
2126   |.if SSE
2127   |  movsd qword [BASE], xmm0
2128   |.else
2129   |  fstp qword [BASE]
2130   |.endif
2131   |  mov RD, 1+2
2132   |  jmp ->fff_res
2133   |3:  // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2134   |.if SSE
2135   |  xorps xmm0, xmm0; jmp <2
2136   |.else
2137   |  fldz; jmp <2
2138   |.endif
2139   |4:  // Handle denormals by multiplying with 2^54 and adjusting the bias.
2140   |.if SSE
2141   |  movsd xmm0, qword [BASE]
2142   |  sseconst_hi xmm1, RBa, 43500000  // 2^54.
2143   |  mulsd xmm0, xmm1
2144   |  movsd qword [BASE-8], xmm0
2145   |.else
2146   |  fld qword [BASE]
2147   |  mov TMP1, 0x5a800000; fmul TMP1    // x = x*2^54
2148   |  fstp qword [BASE-8]
2149   |.endif
2150   |  mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2151   |
2152   |.if SSE
2153   |.ffunc_nsse math_modf
2154   |.else
2155   |.ffunc_n math_modf
2156   |.endif
2157   |  mov RB, [BASE+4]
2158   |  mov PC, [BASE-4]
2159   |  shl RB, 1; cmp RB, 0xffe00000; je >4       // +-Inf?
2160   |.if SSE
2161   |  movaps xmm4, xmm0
2162   |  call ->vm_trunc
2163   |  subsd xmm4, xmm0
2164   |1:
2165   |  movsd qword [BASE-8], xmm0
2166   |  movsd qword [BASE], xmm4
2167   |.else
2168   |  fdup
2169   |  call ->vm_trunc
2170   |  fsub st1, st0
2171   |1:
2172   |  fstp qword [BASE-8]
2173   |  fstp qword [BASE]
2174   |.endif
2175   |  mov RC, [BASE-4]; mov RB, [BASE+4]
2176   |  xor RC, RB; js >3                          // Need to adjust sign?
2177   |2:
2178   |  mov RD, 1+2
2179   |  jmp ->fff_res
2180   |3:
2181   |  xor RB, 0x80000000; mov [BASE+4], RB       // Flip sign of fraction.
2182   |  jmp <2
2183   |4:
2184   |.if SSE
2185   |  xorps xmm4, xmm4; jmp <1                   // Return +-Inf and +-0.
2186   |.else
2187   |  fldz; fxch; jmp <1                         // Return +-Inf and +-0.
2188   |.endif
2189   |
2190   |.ffunc_nnr math_fmod
2191   |1: ; fprem; fnstsw ax; sahf; jp <1
2192   |  fpop1
2193   |  jmp ->fff_resn
2194   |
2195   |.if SSE
2196   |.ffunc_nnsse math_pow;       call ->vm_pow;  jmp ->fff_resxmm0
2197   |.else
2198   |.ffunc_nn math_pow;          call ->vm_pow;  jmp ->fff_resn
2199   |.endif
2200   |
2201   |.macro math_minmax, name, cmovop, fcmovop, sseop
2202   |  .ffunc name
2203   |  mov RA, 2
2204   |  cmp dword [BASE+4], LJ_TISNUM
2205   |.if DUALNUM
2206   |  jne >4
2207   |  mov RB, dword [BASE]
2208   |1:  // Handle integers.
2209   |  cmp RA, RD; jae ->fff_resi
2210   |  cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2211   |  cmp RB, dword [BASE+RA*8-8]
2212   |  cmovop RB, dword [BASE+RA*8-8]
2213   |  add RA, 1
2214   |  jmp <1
2215   |3:
2216   |  ja ->fff_fallback
2217   |  // Convert intermediate result to number and continue below.
2218   |.if SSE
2219   |  cvtsi2sd xmm0, RB
2220   |.else
2221   |  mov TMP1, RB
2222   |  fild TMP1
2223   |.endif
2224   |  jmp >6
2225   |4:
2226   |  ja ->fff_fallback
2227   |.else
2228   |  jae ->fff_fallback
2229   |.endif
2230   |
2231   |.if SSE
2232   |  movsd xmm0, qword [BASE]
2233   |5:  // Handle numbers or integers.
2234   |  cmp RA, RD; jae ->fff_resxmm0
2235   |  cmp dword [BASE+RA*8-4], LJ_TISNUM
2236   |.if DUALNUM
2237   |  jb >6
2238   |  ja ->fff_fallback
2239   |  cvtsi2sd xmm1, dword [BASE+RA*8-8]
2240   |  jmp >7
2241   |.else
2242   |  jae ->fff_fallback
2243   |.endif
2244   |6:
2245   |  movsd xmm1, qword [BASE+RA*8-8]
2246   |7:
2247   |  sseop xmm0, xmm1
2248   |  add RA, 1
2249   |  jmp <5
2250   |.else
2251   |  fld qword [BASE]
2252   |5:  // Handle numbers or integers.
2253   |  cmp RA, RD; jae ->fff_resn
2254   |  cmp dword [BASE+RA*8-4], LJ_TISNUM
2255   |.if DUALNUM
2256   |  jb >6
2257   |  ja >9
2258   |  fild dword [BASE+RA*8-8]
2259   |  jmp >7
2260   |.else
2261   |  jae >9
2262   |.endif
2263   |6:
2264   |  fld qword [BASE+RA*8-8]
2265   |7:
2266   |  fucomi st1; fcmovop st1; fpop1
2267   |  add RA, 1
2268   |  jmp <5
2269   |.endif
2270   |.endmacro
2271   |
2272   |  math_minmax math_min, cmovg, fcmovnbe, minsd
2273   |  math_minmax math_max, cmovl, fcmovbe, maxsd
2274   |.if not SSE
2275   |9:
2276   |  fpop; jmp ->fff_fallback
2277   |.endif
2278   |
2279   |//-- String library -----------------------------------------------------
2280   |
2281   |.ffunc_1 string_len
2282   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2283   |  mov STR:RB, [BASE]
2284   |.if DUALNUM
2285   |  mov RB, dword STR:RB->len; jmp ->fff_resi
2286   |.elif SSE
2287   |  cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0
2288   |.else
2289   |  fild dword STR:RB->len; jmp ->fff_resn
2290   |.endif
2291   |
2292   |.ffunc string_byte                   // Only handle the 1-arg case here.
2293   |  cmp NARGS:RD, 1+1;  jne ->fff_fallback
2294   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2295   |  mov STR:RB, [BASE]
2296   |  mov PC, [BASE-4]
2297   |  cmp dword STR:RB->len, 1
2298   |  jb ->fff_res0                      // Return no results for empty string.
2299   |  movzx RB, byte STR:RB[1]
2300   |.if DUALNUM
2301   |  jmp ->fff_resi
2302   |.elif SSE
2303   |  cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2304   |.else
2305   |  mov TMP1, RB; fild TMP1; jmp ->fff_resn
2306   |.endif
2307   |
2308   |.ffunc string_char                   // Only handle the 1-arg case here.
2309   |  ffgccheck
2310   |  cmp NARGS:RD, 1+1;  jne ->fff_fallback     // *Exactly* 1 arg.
2311   |  cmp dword [BASE+4], LJ_TISNUM
2312   |.if DUALNUM
2313   |  jne ->fff_fallback
2314   |  mov RB, dword [BASE]
2315   |  cmp RB, 255;  ja ->fff_fallback
2316   |  mov TMP2, RB
2317   |.elif SSE
2318   |  jae ->fff_fallback
2319   |  cvttsd2si RB, qword [BASE]
2320   |  cmp RB, 255;  ja ->fff_fallback
2321   |  mov TMP2, RB
2322   |.else
2323   |  jae ->fff_fallback
2324   |  fld qword [BASE]
2325   |  fistp TMP2
2326   |  cmp TMP2, 255;  ja ->fff_fallback
2327   |.endif
2328   |.if X64
2329   |  mov TMP3, 1
2330   |.else
2331   |  mov ARG3, 1
2332   |.endif
2333   |  lea RDa, TMP2                      // Points to stack. Little-endian.
2334   |->fff_newstr:
2335   |  mov L:RB, SAVE_L
2336   |  mov L:RB->base, BASE
2337   |.if X64
2338   |  mov CARG3d, TMP3                   // Zero-extended to size_t.
2339   |  mov CARG2, RDa                     // May be 64 bit ptr to stack.
2340   |  mov CARG1d, L:RB
2341   |.else
2342   |  mov ARG2, RD
2343   |  mov ARG1, L:RB
2344   |.endif
2345   |  mov SAVE_PC, PC
2346   |  call extern lj_str_new             // (lua_State *L, char *str, size_t l)
2347   |  // GCstr * returned in eax (RD).
2348   |  mov BASE, L:RB->base
2349   |  mov PC, [BASE-4]
2350   |  mov dword [BASE-4], LJ_TSTR
2351   |  mov [BASE-8], STR:RD
2352   |  jmp ->fff_res1
2353   |
2354   |.ffunc string_sub
2355   |  ffgccheck
2356   |  mov TMP2, -1
2357   |  cmp NARGS:RD, 1+2;  jb ->fff_fallback
2358   |  jna >1
2359   |  cmp dword [BASE+20], LJ_TISNUM
2360   |.if DUALNUM
2361   |  jne ->fff_fallback
2362   |  mov RB, dword [BASE+16]
2363   |  mov TMP2, RB
2364   |.elif SSE
2365   |  jae ->fff_fallback
2366   |  cvttsd2si RB, qword [BASE+16]
2367   |  mov TMP2, RB
2368   |.else
2369   |  jae ->fff_fallback
2370   |  fld qword [BASE+16]
2371   |  fistp TMP2
2372   |.endif
2373   |1:
2374   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2375   |  cmp dword [BASE+12], LJ_TISNUM
2376   |.if DUALNUM
2377   |  jne ->fff_fallback
2378   |.else
2379   |  jae ->fff_fallback
2380   |.endif
2381   |  mov STR:RB, [BASE]
2382   |  mov TMP3, STR:RB
2383   |  mov RB, STR:RB->len
2384   |.if DUALNUM
2385   |  mov RA, dword [BASE+8]
2386   |.elif SSE
2387   |  cvttsd2si RA, qword [BASE+8]
2388   |.else
2389   |  fld qword [BASE+8]
2390   |  fistp ARG3
2391   |  mov RA, ARG3
2392   |.endif
2393   |  mov RC, TMP2
2394   |  cmp RB, RC                         // len < end? (unsigned compare)
2395   |  jb >5
2396   |2:
2397   |  test RA, RA                        // start <= 0?
2398   |  jle >7
2399   |3:
2400   |  mov STR:RB, TMP3
2401   |  sub RC, RA                         // start > end?
2402   |  jl ->fff_emptystr
2403   |  lea RB, [STR:RB+RA+#STR-1]
2404   |  add RC, 1
2405   |4:
2406   |.if X64
2407   |  mov TMP3, RC
2408   |.else
2409   |  mov ARG3, RC
2410   |.endif
2411   |  mov RD, RB
2412   |  jmp ->fff_newstr
2413   |
2414   |5:  // Negative end or overflow.
2415   |  jl >6
2416   |  lea RC, [RC+RB+1]                  // end = end+(len+1)
2417   |  jmp <2
2418   |6:  // Overflow.
2419   |  mov RC, RB                         // end = len
2420   |  jmp <2
2421   |
2422   |7:  // Negative start or underflow.
2423   |  je >8
2424   |  add RA, RB                         // start = start+(len+1)
2425   |  add RA, 1
2426   |  jg <3                              // start > 0?
2427   |8:  // Underflow.
2428   |  mov RA, 1                          // start = 1
2429   |  jmp <3
2430   |
2431   |->fff_emptystr:  // Range underflow.
2432   |  xor RC, RC                         // Zero length. Any ptr in RB is ok.
2433   |  jmp <4
2434   |
2435   |.ffunc_2 string_rep                  // Only handle the 1-char case inline.
2436   |  ffgccheck
2437   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2438   |  cmp dword [BASE+12], LJ_TISNUM
2439   |  mov STR:RB, [BASE]
2440   |.if DUALNUM
2441   |  jne ->fff_fallback
2442   |  mov RC, dword [BASE+8]
2443   |.elif SSE
2444   |  jae ->fff_fallback
2445   |  cvttsd2si RC, qword [BASE+8]
2446   |.else
2447   |  jae ->fff_fallback
2448   |  fld qword [BASE+8]
2449   |  fistp TMP2
2450   |  mov RC, TMP2
2451   |.endif
2452   |  test RC, RC
2453   |  jle ->fff_emptystr                 // Count <= 0? (or non-int)
2454   |  cmp dword STR:RB->len, 1
2455   |  jb ->fff_emptystr                  // Zero length string?
2456   |  jne ->fff_fallback_2               // Fallback for > 1-char strings.
2457   |  cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC;  jb ->fff_fallback_2
2458   |  movzx RA, byte STR:RB[1]
2459   |  mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2460   |.if X64
2461   |  mov TMP3, RC
2462   |.else
2463   |  mov ARG3, RC
2464   |.endif
2465   |1:  // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2466   |  mov [RB], RAL
2467   |  add RB, 1
2468   |  sub RC, 1
2469   |  jnz <1
2470   |  mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2471   |  jmp ->fff_newstr
2472   |
2473   |.ffunc_1 string_reverse
2474   |  ffgccheck
2475   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2476   |  mov STR:RB, [BASE]
2477   |  mov RC, STR:RB->len
2478   |  test RC, RC
2479   |  jz ->fff_emptystr                  // Zero length string?
2480   |  cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC;  jb ->fff_fallback_1
2481   |  add RB, #STR
2482   |  mov TMP2, PC                       // Need another temp register.
2483   |.if X64
2484   |  mov TMP3, RC
2485   |.else
2486   |  mov ARG3, RC
2487   |.endif
2488   |  mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2489   |1:
2490   |  movzx RA, byte [RB]
2491   |  add RB, 1
2492   |  sub RC, 1
2493   |  mov [PC+RC], RAL
2494   |  jnz <1
2495   |  mov RD, PC
2496   |  mov PC, TMP2
2497   |  jmp ->fff_newstr
2498   |
2499   |.macro ffstring_case, name, lo, hi
2500   |  .ffunc_1 name
2501   |  ffgccheck
2502   |  cmp dword [BASE+4], LJ_TSTR;  jne ->fff_fallback
2503   |  mov STR:RB, [BASE]
2504   |  mov RC, STR:RB->len
2505   |  cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC;  jb ->fff_fallback_1
2506   |  add RB, #STR
2507   |  mov TMP2, PC                       // Need another temp register.
2508   |.if X64
2509   |  mov TMP3, RC
2510   |.else
2511   |  mov ARG3, RC
2512   |.endif
2513   |  mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2514   |  jmp >3
2515   |1:  // ASCII case conversion. Yes, this is suboptimal code (do you care?).
2516   |  movzx RA, byte [RB+RC]
2517   |  cmp RA, lo
2518   |  jb >2
2519   |  cmp RA, hi
2520   |  ja >2
2521   |  xor RA, 0x20
2522   |2:
2523   |  mov [PC+RC], RAL
2524   |3:
2525   |  sub RC, 1
2526   |  jns <1
2527   |  mov RD, PC
2528   |  mov PC, TMP2
2529   |  jmp ->fff_newstr
2530   |.endmacro
2531   |
2532   |ffstring_case string_lower, 0x41, 0x5a
2533   |ffstring_case string_upper, 0x61, 0x7a
2534   |
2535   |//-- Table library ------------------------------------------------------
2536   |
2537   |.ffunc_1 table_getn
2538   |  cmp dword [BASE+4], LJ_TTAB;  jne ->fff_fallback
2539   |  mov RB, BASE                       // Save BASE.
2540   |  mov TAB:FCARG1, [BASE]
2541   |  call extern lj_tab_len@4           // LJ_FASTCALL (GCtab *t)
2542   |  // Length of table returned in eax (RD).
2543   |  mov BASE, RB                       // Restore BASE.
2544   |.if DUALNUM
2545   |  mov RB, RD; jmp ->fff_resi
2546   |.elif SSE
2547   |  cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
2548   |.else
2549   |  mov ARG1, RD; fild ARG1; jmp ->fff_resn
2550   |.endif
2551   |
2552   |//-- Bit library --------------------------------------------------------
2553   |
2554   |.define TOBIT_BIAS, 0x59c00000       // 2^52 + 2^51 (float, not double!).
2555   |
2556   |.macro .ffunc_bit, name, kind
2557   |  .ffunc_1 name
2558   |.if kind == 2
2559   |.if SSE
2560   |  sseconst_tobit xmm1, RBa
2561   |.else
2562   |  mov TMP1, TOBIT_BIAS
2563   |.endif
2564   |.endif
2565   |  cmp dword [BASE+4], LJ_TISNUM
2566   |.if DUALNUM
2567   |  jne >1
2568   |  mov RB, dword [BASE]
2569   |.if kind > 0
2570   |  jmp >2
2571   |.else
2572   |  jmp ->fff_resbit
2573   |.endif
2574   |1:
2575   |  ja ->fff_fallback
2576   |.else
2577   |  jae ->fff_fallback
2578   |.endif
2579   |.if SSE
2580   |  movsd xmm0, qword [BASE]
2581   |.if kind < 2
2582   |  sseconst_tobit xmm1, RBa
2583   |.endif
2584   |  addsd xmm0, xmm1
2585   |  movd RB, xmm0
2586   |.else
2587   |  fld qword [BASE]
2588   |.if kind < 2
2589   |  mov TMP1, TOBIT_BIAS
2590   |.endif
2591   |  fadd TMP1
2592   |  fstp FPARG1
2593   |.if kind > 0
2594   |  mov RB, ARG1
2595   |.endif
2596   |.endif
2597   |2:
2598   |.endmacro
2599   |
2600   |.ffunc_bit bit_tobit, 0
2601   |.if DUALNUM or SSE
2602   |.if not SSE
2603   |  mov RB, ARG1
2604   |.endif
2605   |  jmp ->fff_resbit
2606   |.else
2607   |  fild ARG1
2608   |  jmp ->fff_resn
2609   |.endif
2610   |
2611   |.macro .ffunc_bit_op, name, ins
2612   |  .ffunc_bit name, 2
2613   |  mov TMP2, NARGS:RD                 // Save for fallback.
2614   |  lea RD, [BASE+NARGS:RD*8-16]
2615   |1:
2616   |  cmp RD, BASE
2617   |  jbe ->fff_resbit
2618   |  cmp dword [RD+4], LJ_TISNUM
2619   |.if DUALNUM
2620   |  jne >2
2621   |  ins RB, dword [RD]
2622   |  sub RD, 8
2623   |  jmp <1
2624   |2:
2625   |  ja ->fff_fallback_bit_op
2626   |.else
2627   |  jae ->fff_fallback_bit_op
2628   |.endif
2629   |.if SSE
2630   |  movsd xmm0, qword [RD]
2631   |  addsd xmm0, xmm1
2632   |  movd RA, xmm0
2633   |  ins RB, RA
2634   |.else
2635   |  fld qword [RD]
2636   |  fadd TMP1
2637   |  fstp FPARG1
2638   |  ins RB, ARG1
2639   |.endif
2640   |  sub RD, 8
2641   |  jmp <1
2642   |.endmacro
2643   |
2644   |.ffunc_bit_op bit_band, and
2645   |.ffunc_bit_op bit_bor, or
2646   |.ffunc_bit_op bit_bxor, xor
2647   |
2648   |.ffunc_bit bit_bswap, 1
2649   |  bswap RB
2650   |  jmp ->fff_resbit
2651   |
2652   |.ffunc_bit bit_bnot, 1
2653   |  not RB
2654   |.if DUALNUM
2655   |  jmp ->fff_resbit
2656   |.elif SSE
2657   |->fff_resbit:
2658   |  cvtsi2sd xmm0, RB
2659   |  jmp ->fff_resxmm0
2660   |.else
2661   |->fff_resbit:
2662   |  mov ARG1, RB
2663   |  fild ARG1
2664   |  jmp ->fff_resn
2665   |.endif
2666   |
2667   |->fff_fallback_bit_op:
2668   |  mov NARGS:RD, TMP2                 // Restore for fallback
2669   |  jmp ->fff_fallback
2670   |
2671   |.macro .ffunc_bit_sh, name, ins
2672   |.if DUALNUM
2673   |  .ffunc_bit name, 1
2674   |  // Note: no inline conversion from number for 2nd argument!
2675   |  cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2676   |  mov RA, dword [BASE+8]
2677   |.elif SSE
2678   |  .ffunc_nnsse name
2679   |  sseconst_tobit xmm2, RBa
2680   |  addsd xmm0, xmm2
2681   |  addsd xmm1, xmm2
2682   |  movd RB, xmm0
2683   |  movd RA, xmm1
2684   |.else
2685   |  .ffunc_nn name
2686   |  mov TMP1, TOBIT_BIAS
2687   |  fadd TMP1
2688   |  fstp FPARG3
2689   |  fadd TMP1
2690   |  fstp FPARG1
2691   |  mov RA, ARG3
2692   |  mov RB, ARG1
2693   |.endif
2694   |  ins RB, cl                         // Assumes RA is ecx.
2695   |  jmp ->fff_resbit
2696   |.endmacro
2697   |
2698   |.ffunc_bit_sh bit_lshift, shl
2699   |.ffunc_bit_sh bit_rshift, shr
2700   |.ffunc_bit_sh bit_arshift, sar
2701   |.ffunc_bit_sh bit_rol, rol
2702   |.ffunc_bit_sh bit_ror, ror
2703   |
2704   |//-----------------------------------------------------------------------
2705   |
2706   |->fff_fallback_2:
2707   |  mov NARGS:RD, 1+2                  // Other args are ignored, anyway.
2708   |  jmp ->fff_fallback
2709   |->fff_fallback_1:
2710   |  mov NARGS:RD, 1+1                  // Other args are ignored, anyway.
2711   |->fff_fallback:                      // Call fast function fallback handler.
2712   |  // BASE = new base, RD = nargs+1
2713   |  mov L:RB, SAVE_L
2714   |  mov PC, [BASE-4]                   // Fallback may overwrite PC.
2715   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
2716   |  mov L:RB->base, BASE
2717   |  lea RD, [BASE+NARGS:RD*8-8]
2718   |  lea RA, [RD+8*LUA_MINSTACK]        // Ensure enough space for handler.
2719   |  mov L:RB->top, RD
2720   |  mov CFUNC:RD, [BASE-8]
2721   |  cmp RA, L:RB->maxstack
2722   |  ja >5                              // Need to grow stack.
2723   |.if X64
2724   |  mov CARG1d, L:RB
2725   |.else
2726   |  mov ARG1, L:RB
2727   |.endif
2728   |  call aword CFUNC:RD->f             // (lua_State *L)
2729   |  mov BASE, L:RB->base
2730   |  // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2731   |  test RD, RD;  jg ->fff_res         // Returned nresults+1?
2732   |1:
2733   |  mov RA, L:RB->top
2734   |  sub RA, BASE
2735   |  shr RA, 3
2736   |  test RD, RD
2737   |  lea NARGS:RD, [RA+1]
2738   |  mov LFUNC:RB, [BASE-8]
2739   |  jne ->vm_call_tail                 // Returned -1?
2740   |  ins_callt                          // Returned 0: retry fast path.
2741   |
2742   |// Reconstruct previous base for vmeta_call during tailcall.
2743   |->vm_call_tail:
2744   |  mov RA, BASE
2745   |  test PC, FRAME_TYPE
2746   |  jnz >3
2747   |  movzx RB, PC_RA
2748   |  not RBa                            // Note: ~RB = -(RB+1)
2749   |  lea BASE, [BASE+RB*8]              // base = base - (RB+1)*8
2750   |  jmp ->vm_call_dispatch             // Resolve again for tailcall.
2751   |3:
2752   |  mov RB, PC
2753   |  and RB, -8
2754   |  sub BASE, RB
2755   |  jmp ->vm_call_dispatch             // Resolve again for tailcall.
2756   |
2757   |5:  // Grow stack for fallback handler.
2758   |  mov FCARG2, LUA_MINSTACK
2759   |  mov FCARG1, L:RB
2760   |  call extern lj_state_growstack@8   // (lua_State *L, int n)
2761   |  mov BASE, L:RB->base
2762   |  xor RD, RD                         // Simulate a return 0.
2763   |  jmp <1                             // Dumb retry (goes through ff first).
2764   |
2765   |->fff_gcstep:                        // Call GC step function.
2766   |  // BASE = new base, RD = nargs+1
2767   |  pop RBa                            // Must keep stack at same level.
2768   |  mov TMPa, RBa                      // Save return address
2769   |  mov L:RB, SAVE_L
2770   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
2771   |  mov L:RB->base, BASE
2772   |  lea RD, [BASE+NARGS:RD*8-8]
2773   |  mov FCARG1, L:RB
2774   |  mov L:RB->top, RD
2775   |  call extern lj_gc_step@4           // (lua_State *L)
2776   |  mov BASE, L:RB->base
2777   |  mov RD, L:RB->top
2778   |  sub RD, BASE
2779   |  shr RD, 3
2780   |  add NARGS:RD, 1
2781   |  mov RBa, TMPa
2782   |  push RBa                           // Restore return address.
2783   |  ret
2784   |
2785   |//-----------------------------------------------------------------------
2786   |//-- Special dispatch targets -------------------------------------------
2787   |//-----------------------------------------------------------------------
2788   |
2789   |->vm_record:                         // Dispatch target for recording phase.
2790   |.if JIT
2791   |  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2792   |  test RDL, HOOK_VMEVENT             // No recording while in vmevent.
2793   |  jnz >5
2794   |  // Decrement the hookcount for consistency, but always do the call.
2795   |  test RDL, HOOK_ACTIVE
2796   |  jnz >1
2797   |  test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2798   |  jz >1
2799   |  dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2800   |  jmp >1
2801   |.endif
2802   |
2803   |->vm_rethook:                        // Dispatch target for return hooks.
2804   |  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2805   |  test RDL, HOOK_ACTIVE              // Hook already active?
2806   |  jnz >5
2807   |  jmp >1
2808   |
2809   |->vm_inshook:                        // Dispatch target for instr/line hooks.
2810   |  movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2811   |  test RDL, HOOK_ACTIVE              // Hook already active?
2812   |  jnz >5
2813   |
2814   |  test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2815   |  jz >5
2816   |  dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2817   |  jz >1
2818   |  test RDL, LUA_MASKLINE
2819   |  jz >5
2820   |1:
2821   |  mov L:RB, SAVE_L
2822   |  mov L:RB->base, BASE
2823   |  mov FCARG2, PC                     // Caveat: FCARG2 == BASE
2824   |  mov FCARG1, L:RB
2825   |  // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2826   |  call extern lj_dispatch_ins@8      // (lua_State *L, BCIns *pc)
2827   |3:
2828   |  mov BASE, L:RB->base
2829   |4:
2830   |  movzx RA, PC_RA
2831   |5:
2832   |  movzx OP, PC_OP
2833   |  movzx RD, PC_RD
2834   |.if X64
2835   |  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]   // Re-dispatch to static ins.
2836   |.else
2837   |  jmp aword [DISPATCH+OP*4+GG_DISP2STATIC]   // Re-dispatch to static ins.
2838   |.endif
2839   |
2840   |->cont_hook:                         // Continue from hook yield.
2841   |  add PC, 4
2842   |  mov RA, [RB-24]
2843   |  mov MULTRES, RA                    // Restore MULTRES for *M ins.
2844   |  jmp <4
2845   |
2846   |->vm_hotloop:                        // Hot loop counter underflow.
2847   |.if JIT
2848   |  mov LFUNC:RB, [BASE-8]             // Same as curr_topL(L).
2849   |  mov RB, LFUNC:RB->pc
2850   |  movzx RD, byte [RB+PC2PROTO(framesize)]
2851   |  lea RD, [BASE+RD*8]
2852   |  mov L:RB, SAVE_L
2853   |  mov L:RB->base, BASE
2854   |  mov L:RB->top, RD
2855   |  mov FCARG2, PC
2856   |  lea FCARG1, [DISPATCH+GG_DISP2J]
2857   |  mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2858   |  mov SAVE_PC, PC
2859   |  call extern lj_trace_hot@8         // (jit_State *J, const BCIns *pc)
2860   |  jmp <3
2861   |.endif
2862   |
2863   |->vm_callhook:                       // Dispatch target for call hooks.
2864   |  mov SAVE_PC, PC
2865   |.if JIT
2866   |  jmp >1
2867   |.endif
2868   |
2869   |->vm_hotcall:                        // Hot call counter underflow.
2870   |.if JIT
2871   |  mov SAVE_PC, PC
2872   |  or PC, 1                           // Marker for hot call.
2873   |1:
2874   |.endif
2875   |  lea RD, [BASE+NARGS:RD*8-8]
2876   |  mov L:RB, SAVE_L
2877   |  mov L:RB->base, BASE
2878   |  mov L:RB->top, RD
2879   |  mov FCARG2, PC
2880   |  mov FCARG1, L:RB
2881   |  call extern lj_dispatch_call@8     // (lua_State *L, const BCIns *pc)
2882   |  // ASMFunction returned in eax/rax (RDa).
2883   |  mov SAVE_PC, 0                     // Invalidate for subsequent line hook.
2884   |.if JIT
2885   |  and PC, -2
2886   |.endif
2887   |  mov BASE, L:RB->base
2888   |  mov RAa, RDa
2889   |  mov RD, L:RB->top
2890   |  sub RD, BASE
2891   |  mov RBa, RAa
2892   |  movzx RA, PC_RA
2893   |  shr RD, 3
2894   |  add NARGS:RD, 1
2895   |  jmp RBa
2896   |
2897   |//-----------------------------------------------------------------------
2898   |//-- Trace exit handler -------------------------------------------------
2899   |//-----------------------------------------------------------------------
2900   |
2901   |// Called from an exit stub with the exit number on the stack.
2902   |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2903   |->vm_exit_handler:
2904   |.if JIT
2905   |.if X64
2906   |  push r13; push r12
2907   |  push r11; push r10; push r9; push r8
2908   |  push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2909   |  push rbx; push rdx; push rcx; push rax
2910   |  movzx RC, byte [rbp-8]             // Reconstruct exit number.
2911   |  mov RCH, byte [rbp-16]
2912   |  mov [rbp-8], r15; mov [rbp-16], r14
2913   |.else
2914   |  push ebp; lea ebp, [esp+12]; push ebp
2915   |  push ebx; push edx; push ecx; push eax
2916   |  movzx RC, byte [ebp-4]             // Reconstruct exit number.
2917   |  mov RCH, byte [ebp-8]
2918   |  mov [ebp-4], edi; mov [ebp-8], esi
2919   |.endif
2920   |  // Caveat: DISPATCH is ebx.
2921   |  mov DISPATCH, [ebp]
2922   |  mov RA, [DISPATCH+DISPATCH_GL(vmstate)]    // Get trace number.
2923   |  set_vmstate EXIT
2924   |  mov [DISPATCH+DISPATCH_J(exitno)], RC
2925   |  mov [DISPATCH+DISPATCH_J(parent)], RA
2926   |.if X64
2927   |.if X64WIN
2928   |  sub rsp, 16*8+4*8                  // Room for SSE regs + save area.
2929   |.else
2930   |  sub rsp, 16*8                      // Room for SSE regs.
2931   |.endif
2932   |  add rbp, -128
2933   |  movsd qword [rbp-8],   xmm15; movsd qword [rbp-16],  xmm14
2934   |  movsd qword [rbp-24],  xmm13; movsd qword [rbp-32],  xmm12
2935   |  movsd qword [rbp-40],  xmm11; movsd qword [rbp-48],  xmm10
2936   |  movsd qword [rbp-56],  xmm9;  movsd qword [rbp-64],  xmm8
2937   |  movsd qword [rbp-72],  xmm7;  movsd qword [rbp-80],  xmm6
2938   |  movsd qword [rbp-88],  xmm5;  movsd qword [rbp-96],  xmm4
2939   |  movsd qword [rbp-104], xmm3;  movsd qword [rbp-112], xmm2
2940   |  movsd qword [rbp-120], xmm1;  movsd qword [rbp-128], xmm0
2941   |.else
2942   |  sub esp, 8*8+16                    // Room for SSE regs + args.
2943   |  movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2944   |  movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2945   |  movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2946   |  movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2947   |.endif
2948   |  // Caveat: RB is ebp.
2949   |  mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)]
2950   |  mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2951   |  mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2952   |  mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
2953   |  mov L:RB->base, BASE
2954   |.if X64WIN
2955   |  lea CARG2, [rsp+4*8]
2956   |.elif X64
2957   |  mov CARG2, rsp
2958   |.else
2959   |  lea FCARG2, [esp+16]
2960   |.endif
2961   |  lea FCARG1, [DISPATCH+GG_DISP2J]
2962   |  call extern lj_trace_exit@8        // (jit_State *J, ExitState *ex)
2963   |  // MULTRES or negated error code returned in eax (RD).
2964   |  mov RAa, L:RB->cframe
2965   |  and RAa, CFRAME_RAWMASK
2966   |.if X64WIN
2967   |  // Reposition stack later.
2968   |.elif X64
2969   |  mov rsp, RAa                       // Reposition stack to C frame.
2970   |.else
2971   |  mov esp, RAa                       // Reposition stack to C frame.
2972   |.endif
2973   |  mov [RAa+CFRAME_OFS_L], L:RB       // Set SAVE_L (on-trace resume/yield).
2974   |  mov BASE, L:RB->base
2975   |  mov PC, [RAa+CFRAME_OFS_PC]        // Get SAVE_PC.
2976   |.if X64
2977   |  jmp >1
2978   |.endif
2979   |.endif
2980   |->vm_exit_interp:
2981   |  // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2982   |.if JIT
2983   |.if X64
2984   |  // Restore additional callee-save registers only used in compiled code.
2985   |.if X64WIN
2986   |  lea RAa, [rsp+9*16+4*8]
2987   |1:
2988   |  movdqa xmm15, [RAa-9*16]
2989   |  movdqa xmm14, [RAa-8*16]
2990   |  movdqa xmm13, [RAa-7*16]
2991   |  movdqa xmm12, [RAa-6*16]
2992   |  movdqa xmm11, [RAa-5*16]
2993   |  movdqa xmm10, [RAa-4*16]
2994   |  movdqa xmm9, [RAa-3*16]
2995   |  movdqa xmm8, [RAa-2*16]
2996   |  movdqa xmm7, [RAa-1*16]
2997   |  mov rsp, RAa                       // Reposition stack to C frame.
2998   |  movdqa xmm6, [RAa]
2999   |  mov r15, CSAVE_3
3000   |  mov r14, CSAVE_4
3001   |.else
3002   |  add rsp, 16                        // Reposition stack to C frame.
3003   |1:
3004   |.endif
3005   |  mov r13, TMPa
3006   |  mov r12, TMPQ
3007   |.endif
3008   |  test RD, RD; js >3                 // Check for error from exit.
3009   |  mov MULTRES, RD
3010   |  mov LFUNC:KBASE, [BASE-8]
3011   |  mov KBASE, LFUNC:KBASE->pc
3012   |  mov KBASE, [KBASE+PC2PROTO(k)]
3013   |  mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
3014   |  set_vmstate INTERP
3015   |  // Modified copy of ins_next which handles function header dispatch, too.
3016   |  mov RC, [PC]
3017   |  movzx RA, RCH
3018   |  movzx OP, RCL
3019   |  add PC, 4
3020   |  shr RC, 16
3021   |  cmp OP, BC_FUNCF                   // Function header?
3022   |  jb >2
3023   |  mov RC, MULTRES                    // RC/RD holds nres+1.
3024   |2:
3025   |.if X64
3026   |  jmp aword [DISPATCH+OP*8]
3027   |.else
3028   |  jmp aword [DISPATCH+OP*4]
3029   |.endif
3030   |
3031   |3:  // Rethrow error from the right C frame.
3032   |  neg RD
3033   |  mov FCARG1, L:RB
3034   |  mov FCARG2, RD
3035   |  call extern lj_err_throw@8         // (lua_State *L, int errcode)
3036   |.endif
3037   |
3038   |//-----------------------------------------------------------------------
3039   |//-- Math helper functions ----------------------------------------------
3040   |//-----------------------------------------------------------------------
3041   |
3042   |// FP value rounding. Called by math.floor/math.ceil fast functions
3043   |// and from JIT code.
3044   |
3045   |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified.
3046   |.macro vm_round_x87, mode1, mode2
3047   |  fnstcw word [esp+4]                // Caveat: overwrites ARG1 and ARG2.
3048   |  mov [esp+8], eax
3049   |  mov ax, mode1
3050   |  or ax, [esp+4]
3051   |.if mode2 ~= 0xffff
3052   |  and ax, mode2
3053   |.endif
3054   |  mov [esp+6], ax
3055   |  fldcw word [esp+6]
3056   |  frndint
3057   |  fldcw word [esp+4]
3058   |  mov eax, [esp+8]
3059   |  ret
3060   |.endmacro
3061   |
3062   |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
3063   |.macro vm_round_sse, mode
3064   |  sseconst_abs xmm2, RDa
3065   |  sseconst_2p52 xmm3, RDa
3066   |  movaps xmm1, xmm0
3067   |  andpd xmm1, xmm2                   // |x|
3068   |  ucomisd xmm3, xmm1                 // No truncation if 2^52 <= |x|.
3069   |  jbe >1
3070   |  andnpd xmm2, xmm0                  // Isolate sign bit.
3071   |.if mode == 2                // trunc(x)?
3072   |  movaps xmm0, xmm1
3073   |  addsd xmm1, xmm3                   // (|x| + 2^52) - 2^52
3074   |  subsd xmm1, xmm3
3075   |  sseconst_1 xmm3, RDa
3076   |  cmpsd xmm0, xmm1, 1                // |x| < result?
3077   |  andpd xmm0, xmm3
3078   |  subsd xmm1, xmm0                   // If yes, subtract -1.
3079   |  orpd xmm1, xmm2                    // Merge sign bit back in.
3080   |.else
3081   |  addsd xmm1, xmm3                   // (|x| + 2^52) - 2^52
3082   |  subsd xmm1, xmm3
3083   |  orpd xmm1, xmm2                    // Merge sign bit back in.
3084   |  .if mode == 1              // ceil(x)?
3085   |    sseconst_m1 xmm2, RDa            // Must subtract -1 to preserve -0.
3086   |    cmpsd xmm0, xmm1, 6              // x > result?
3087   |  .else                      // floor(x)?
3088   |    sseconst_1 xmm2, RDa
3089   |    cmpsd xmm0, xmm1, 1              // x < result?
3090   |  .endif
3091   |  andpd xmm0, xmm2
3092   |  subsd xmm1, xmm0                   // If yes, subtract +-1.
3093   |.endif
3094   |  movaps xmm0, xmm1
3095   |1:
3096   |  ret
3097   |.endmacro
3098   |
3099   |.macro vm_round, name, ssemode, mode1, mode2
3100   |->name:
3101   |.if not SSE
3102   |  vm_round_x87 mode1, mode2
3103   |.endif
3104   |->name .. _sse:
3105   |  vm_round_sse ssemode
3106   |.endmacro
3107   |
3108   |  vm_round vm_floor, 0, 0x0400, 0xf7ff
3109   |  vm_round vm_ceil,  1, 0x0800, 0xfbff
3110   |  vm_round vm_trunc, 2, 0x0c00, 0xffff
3111   |
3112   |// FP modulo x%y. Called by BC_MOD* and vm_arith.
3113   |->vm_mod:
3114   |.if SSE
3115   |// Args in xmm0/xmm1, return value in xmm0.
3116   |// Caveat: xmm0-xmm5 and RC (eax) modified!
3117   |  movaps xmm5, xmm0
3118   |  divsd xmm0, xmm1
3119   |  sseconst_abs xmm2, RDa
3120   |  sseconst_2p52 xmm3, RDa
3121   |  movaps xmm4, xmm0
3122   |  andpd xmm4, xmm2                   // |x/y|
3123   |  ucomisd xmm3, xmm4                 // No truncation if 2^52 <= |x/y|.
3124   |  jbe >1
3125   |  andnpd xmm2, xmm0                  // Isolate sign bit.
3126   |  addsd xmm4, xmm3                   // (|x/y| + 2^52) - 2^52
3127   |  subsd xmm4, xmm3
3128   |  orpd xmm4, xmm2                    // Merge sign bit back in.
3129   |  sseconst_1 xmm2, RDa
3130   |  cmpsd xmm0, xmm4, 1                // x/y < result?
3131   |  andpd xmm0, xmm2
3132   |  subsd xmm4, xmm0                   // If yes, subtract 1.0.
3133   |  movaps xmm0, xmm5
3134   |  mulsd xmm1, xmm4
3135   |  subsd xmm0, xmm1
3136   |  ret
3137   |1:
3138   |  mulsd xmm1, xmm0
3139   |  movaps xmm0, xmm5
3140   |  subsd xmm0, xmm1
3141   |  ret
3142   |.else
3143   |// Args/ret on x87 stack (y on top). No xmm registers modified.
3144   |// Caveat: needs 3 slots on x87 stack! RC (eax) modified!
3145   |  fld st1
3146   |  fdiv st1
3147   |  fnstcw word [esp+4]
3148   |  mov ax, 0x0400
3149   |  or ax, [esp+4]
3150   |  and ax, 0xf7ff
3151   |  mov [esp+6], ax
3152   |  fldcw word [esp+6]
3153   |  frndint
3154   |  fldcw word [esp+4]
3155   |  fmulp st1
3156   |  fsubp st1
3157   |  ret
3158   |.endif
3159   |
3160   |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
3161   |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
3162   |// Caveat: needs 3 slots on x87 stack!
3163   |->vm_exp_x87:
3164   |  fldl2e; fmulp st1                          // e^x ==> 2^(x*log2(e))
3165   |->vm_exp2_x87:
3166   |  .if X64WIN
3167   |    .define expscratch, dword [rsp+8]        // Use scratch area.
3168   |  .elif X64
3169   |    .define expscratch, dword [rsp-8]        // Use red zone.
3170   |  .else
3171   |    .define expscratch, dword [esp+4]        // Needs 4 byte scratch area.
3172   |  .endif
3173   |  fst expscratch                             // Caveat: overwrites ARG1.
3174   |  cmp expscratch, 0x7f800000; je >1          // Special case: e^+Inf = +Inf
3175   |  cmp expscratch, 0xff800000; je >2          // Special case: e^-Inf = 0
3176   |->vm_exp2raw:  // Entry point for vm_pow. Without +-Inf check.
3177   |  fdup; frndint; fsub st1, st0; fxch         // Split into frac/int part.
3178   |  f2xm1; fld1; faddp st1; fscale; fpop1      // ==> (2^frac-1 +1) << int
3179   |1:
3180   |  ret
3181   |2:
3182   |  fpop; fldz; ret
3183   |
3184   |// Generic power function x^y. Called by BC_POW, math.pow fast function,
3185   |// and vm_arith.
3186   |// Args/ret on x87 stack (y on top). RC (eax) modified.
3187   |// Caveat: needs 3 slots on x87 stack!
3188   |->vm_pow:
3189   |.if not SSE
3190   |  fist dword [esp+4]                 // Store/reload int before comparison.
3191   |  fild dword [esp+4]                 // Integral exponent used in vm_powi.
3192   |  fucomip st1
3193   |  jnz >8                             // Branch for FP exponents.
3194   |  jp >9                              // Branch for NaN exponent.
3195   |  fpop                               // Pop y and fallthrough to vm_powi.
3196   |
3197   |// FP/int power function x^i. Arg1/ret on x87 stack.
3198   |// Arg2 (int) on C stack. RC (eax) modified.
3199   |// Caveat: needs 2 slots on x87 stack!
3200   |  mov eax, [esp+4]
3201   |  cmp eax, 1; jle >6                 // i<=1?
3202   |  // Now 1 < (unsigned)i <= 0x80000000.
3203   |1:  // Handle leading zeros.
3204   |  test eax, 1; jnz >2
3205   |  fmul st0
3206   |  shr eax, 1
3207   |  jmp <1
3208   |2:
3209   |  shr eax, 1; jz >5
3210   |  fdup
3211   |3:  // Handle trailing bits.
3212   |  fmul st0
3213   |  shr eax, 1; jz >4
3214   |  jnc <3
3215   |  fmul st1, st0
3216   |  jmp <3
3217   |4:
3218   |  fmulp st1
3219   |5:
3220   |  ret
3221   |6:
3222   |  je <5                              // x^1 ==> x
3223   |  jb >7
3224   |  fld1; fdivrp st1
3225   |  neg eax
3226   |  cmp eax, 1; je <5                  // x^-1 ==> 1/x
3227   |  jmp <1                             // x^-i ==> (1/x)^i
3228   |7:
3229   |  fpop; fld1                         // x^0 ==> 1
3230   |  ret
3231   |
3232   |8:  // FP/FP power function x^y.
3233   |  fst dword [esp+4]
3234   |  fxch
3235   |  fst dword [esp+8]
3236   |  mov eax, [esp+4]; shl eax, 1
3237   |  cmp eax, 0xff000000; je >2                 // x^+-Inf?
3238   |  mov eax, [esp+8]; shl eax, 1; je >4        // +-0^y?
3239   |  cmp eax, 0xff000000; je >4                 // +-Inf^y?
3240   |  fyl2x
3241   |  jmp ->vm_exp2raw
3242   |
3243   |9:  // Handle x^NaN.
3244   |  fld1
3245   |  fucomip st2
3246   |  je >1                              // 1^NaN ==> 1
3247   |  fxch                               // x^NaN ==> NaN
3248   |1:
3249   |  fpop
3250   |  ret
3251   |
3252   |2:  // Handle x^+-Inf.
3253   |  fabs
3254   |  fld1
3255   |  fucomip st1
3256   |  je >3                                      // +-1^+-Inf ==> 1
3257   |  fpop; fabs; fldz; mov eax, 0; setc al
3258   |  ror eax, 1; xor eax, [esp+4]; jns >3       // |x|<>1, x^+-Inf ==> +Inf/0
3259   |  fxch
3260   |3:
3261   |  fpop1; fabs
3262   |  ret
3263   |
3264   |4:  // Handle +-0^y or +-Inf^y.
3265   |  cmp dword [esp+4], 0; jge <3               // y >= 0, x^y ==> |x|
3266   |  fpop; fpop
3267   |  test eax, eax; jz >5                       // y < 0, +-0^y ==> +Inf
3268   |  fldz                                       // y < 0, +-Inf^y ==> 0
3269   |  ret
3270   |5:
3271   |  mov dword [esp+4], 0x7f800000              // Return +Inf.
3272   |  fld dword [esp+4]
3273   |  ret
3274   |.endif
3275   |
3276   |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified.
3277   |// Needs 16 byte scratch area for x86. Also called from JIT code.
3278   |->vm_pow_sse:
3279   |  cvtsd2si eax, xmm1
3280   |  cvtsi2sd xmm2, eax
3281   |  ucomisd xmm1, xmm2
3282   |  jnz >8                             // Branch for FP exponents.
3283   |  jp >9                              // Branch for NaN exponent.
3284   |  // Fallthrough to vm_powi_sse.
3285   |
3286   |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
3287   |->vm_powi_sse:
3288   |  cmp eax, 1; jle >6                 // i<=1?
3289   |  // Now 1 < (unsigned)i <= 0x80000000.
3290   |1:  // Handle leading zeros.
3291   |  test eax, 1; jnz >2
3292   |  mulsd xmm0, xmm0
3293   |  shr eax, 1
3294   |  jmp <1
3295   |2:
3296   |  shr eax, 1; jz >5
3297   |  movaps xmm1, xmm0
3298   |3:  // Handle trailing bits.
3299   |  mulsd xmm0, xmm0
3300   |  shr eax, 1; jz >4
3301   |  jnc <3
3302   |  mulsd xmm1, xmm0
3303   |  jmp <3
3304   |4:
3305   |  mulsd xmm0, xmm1
3306   |5:
3307   |  ret
3308   |6:
3309   |  je <5                              // x^1 ==> x
3310   |  jb >7                              // x^0 ==> 1
3311   |  neg eax
3312   |  call <1
3313   |  sseconst_1 xmm1, RDa
3314   |  divsd xmm1, xmm0
3315   |  movaps xmm0, xmm1
3316   |  ret
3317   |7:
3318   |  sseconst_1 xmm0, RDa
3319   |  ret
3320   |
3321   |8:  // FP/FP power function x^y.
3322   |.if X64
3323   |  movd rax, xmm1; shl rax, 1
3324   |  rol rax, 12; cmp rax, 0xffe; je >2         // x^+-Inf?
3325   |  movd rax, xmm0; shl rax, 1; je >4          // +-0^y?
3326   |  rol rax, 12; cmp rax, 0xffe; je >5         // +-Inf^y?
3327   |  .if X64WIN
3328   |    movsd qword [rsp+16], xmm1               // Use scratch area.
3329   |    movsd qword [rsp+8], xmm0
3330   |    fld qword [rsp+16]
3331   |    fld qword [rsp+8]
3332   |  .else
3333   |    movsd qword [rsp-16], xmm1               // Use red zone.
3334   |    movsd qword [rsp-8], xmm0
3335   |    fld qword [rsp-16]
3336   |    fld qword [rsp-8]
3337   |  .endif
3338   |.else
3339   |  movsd qword [esp+12], xmm1                 // Needs 16 byte scratch area.
3340   |  movsd qword [esp+4], xmm0
3341   |  cmp dword [esp+12], 0; jne >1
3342   |  mov eax, [esp+16]; shl eax, 1
3343   |  cmp eax, 0xffe00000; je >2                 // x^+-Inf?
3344   |1:
3345   |  cmp dword [esp+4], 0; jne >1
3346   |  mov eax, [esp+8]; shl eax, 1; je >4        // +-0^y?
3347   |  cmp eax, 0xffe00000; je >5                 // +-Inf^y?
3348   |1:
3349   |  fld qword [esp+12]
3350   |  fld qword [esp+4]
3351   |.endif
3352   |  fyl2x                                      // y*log2(x)
3353   |  fdup; frndint; fsub st1, st0; fxch         // Split into frac/int part.
3354   |  f2xm1; fld1; faddp st1; fscale; fpop1      // ==> (2^frac-1 +1) << int
3355   |.if X64WIN
3356   |  fstp qword [rsp+8]                         // Use scratch area.
3357   |  movsd xmm0, qword [rsp+8]
3358   |.elif X64
3359   |  fstp qword [rsp-8]                         // Use red zone.
3360   |  movsd xmm0, qword [rsp-8]
3361   |.else
3362   |  fstp qword [esp+4]                         // Needs 8 byte scratch area.
3363   |  movsd xmm0, qword [esp+4]
3364   |.endif
3365   |  ret
3366   |
3367   |9:  // Handle x^NaN.
3368   |  sseconst_1 xmm2, RDa
3369   |  ucomisd xmm0, xmm2; je >1                  // 1^NaN ==> 1
3370   |  movaps xmm0, xmm1                          // x^NaN ==> NaN
3371   |1:
3372   |  ret
3373   |
3374   |2:  // Handle x^+-Inf.
3375   |  sseconst_abs xmm2, RDa
3376   |  andpd xmm0, xmm2                           // |x|
3377   |  sseconst_1 xmm2, RDa
3378   |  ucomisd xmm0, xmm2; je <1                  // +-1^+-Inf ==> 1
3379   |  movmskpd eax, xmm1
3380   |  xorps xmm0, xmm0
3381   |  mov ah, al; setc al; xor al, ah; jne <1    // |x|<>1, x^+-Inf ==> +Inf/0
3382   |3:
3383   |  sseconst_hi xmm0, RDa, 7ff00000  // +Inf
3384   |  ret
3385   |
3386   |4:  // Handle +-0^y.
3387   |  movmskpd eax, xmm1; test eax, eax; jnz <3  // y < 0, +-0^y ==> +Inf
3388   |  xorps xmm0, xmm0                           // y >= 0, +-0^y ==> 0
3389   |  ret
3390   |
3391   |5:  // Handle +-Inf^y.
3392   |  movmskpd eax, xmm1; test eax, eax; jz <3   // y >= 0, +-Inf^y ==> +Inf
3393   |  xorps xmm0, xmm0                           // y < 0, +-Inf^y ==> 0
3394   |  ret
3395   |
3396   |// Callable from C: double lj_vm_foldfpm(double x, int fpm)
3397   |// Computes fpm(x) for extended math functions. ORDER FPM.
3398   |->vm_foldfpm:
3399   |.if JIT
3400   |.if X64
3401   |  .if X64WIN
3402   |    .define fpmop, CARG2d
3403   |  .else
3404   |    .define fpmop, CARG1d
3405   |  .endif
3406   |  cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3407   |  cmp fpmop, 3; jb ->vm_trunc; ja >2
3408   |  sqrtsd xmm0, xmm0; ret
3409   |2:
3410   |  .if X64WIN
3411   |    movsd qword [rsp+8], xmm0        // Use scratch area.
3412   |    fld qword [rsp+8]
3413   |  .else
3414   |    movsd qword [rsp-8], xmm0        // Use red zone.
3415   |    fld qword [rsp-8]
3416   |  .endif
3417   |  cmp fpmop, 5; ja >2
3418   |  .if X64WIN; pop rax; .endif
3419   |  je >1
3420   |  call ->vm_exp_x87
3421   |  .if X64WIN; push rax; .endif
3422   |  jmp >7
3423   |1:
3424   |  call ->vm_exp2_x87
3425   |  .if X64WIN; push rax; .endif
3426   |  jmp >7
3427   |2: ; cmp fpmop, 7; je >1; ja >2
3428   |  fldln2; fxch; fyl2x; jmp >7
3429   |1: ; fld1; fxch; fyl2x; jmp >7
3430   |2: ; cmp fpmop, 9; je >1; ja >2
3431   |  fldlg2; fxch; fyl2x; jmp >7
3432   |1: ; fsin; jmp >7
3433   |2: ; cmp fpmop, 11; je >1; ja >9
3434   |   fcos; jmp >7
3435   |1: ; fptan; fpop
3436   |7:
3437   |  .if X64WIN
3438   |    fstp qword [rsp+8]               // Use scratch area.
3439   |    movsd xmm0, qword [rsp+8]
3440   |  .else
3441   |    fstp qword [rsp-8]               // Use red zone.
3442   |    movsd xmm0, qword [rsp-8]
3443   |  .endif
3444   |  ret
3445   |.else  // x86 calling convention.
3446   |  .define fpmop, eax
3447   |.if SSE
3448   |  mov fpmop, [esp+12]
3449   |  movsd xmm0, qword [esp+4]
3450   |  cmp fpmop, 1; je >1; ja >2
3451   |  call ->vm_floor; jmp >7
3452   |1: ; call ->vm_ceil; jmp >7
3453   |2: ; cmp fpmop, 3; je >1; ja >2
3454   |  call ->vm_trunc; jmp >7
3455   |1:
3456   |  sqrtsd xmm0, xmm0
3457   |7:
3458   |  movsd qword [esp+4], xmm0  // Overwrite callee-owned args.
3459   |  fld qword [esp+4]
3460   |  ret
3461   |2: ; fld qword [esp+4]
3462   |  cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3463   |2: ; cmp fpmop, 7; je >1; ja >2
3464   |  fldln2; fxch; fyl2x; ret
3465   |1: ; fld1; fxch; fyl2x; ret
3466   |2: ; cmp fpmop, 9; je >1; ja >2
3467   |  fldlg2; fxch; fyl2x; ret
3468   |1: ; fsin; ret
3469   |2: ; cmp fpmop, 11; je >1; ja >9
3470   |   fcos; ret
3471   |1: ; fptan; fpop; ret
3472   |.else
3473   |  mov fpmop, [esp+12]
3474   |  fld qword [esp+4]
3475   |  cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3476   |  cmp fpmop, 3; jb ->vm_trunc; ja >2
3477   |  fsqrt; ret
3478   |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3479   |  cmp fpmop, 7; je >1; ja >2
3480   |  fldln2; fxch; fyl2x; ret
3481   |1: ; fld1; fxch; fyl2x; ret
3482   |2: ; cmp fpmop, 9; je >1; ja >2
3483   |  fldlg2; fxch; fyl2x; ret
3484   |1: ; fsin; ret
3485   |2: ; cmp fpmop, 11; je >1; ja >9
3486   |   fcos; ret
3487   |1: ; fptan; fpop; ret
3488   |.endif
3489   |.endif
3490   |9: ; int3                                    // Bad fpm.
3491   |.endif
3492   |
3493   |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
3494   |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
3495   |// and basic math functions. ORDER ARITH
3496   |->vm_foldarith:
3497   |.if X64
3498   |
3499   |  .if X64WIN
3500   |    .define foldop, CARG3d
3501   |  .else
3502   |    .define foldop, CARG1d
3503   |  .endif
3504   |  cmp foldop, 1; je >1; ja >2
3505   |  addsd xmm0, xmm1; ret
3506   |1: ; subsd xmm0, xmm1; ret
3507   |2: ; cmp foldop, 3; je >1; ja >2
3508   |  mulsd xmm0, xmm1; ret
3509   |1: ; divsd xmm0, xmm1; ret
3510   |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow
3511   |  cmp foldop, 7; je >1; ja >2
3512   |  sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret
3513   |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret
3514   |2: ; cmp foldop, 9; ja >2
3515   |.if X64WIN
3516   |  movsd qword [rsp+8], xmm0  // Use scratch area.
3517   |  movsd qword [rsp+16], xmm1
3518   |  fld qword [rsp+8]
3519   |  fld qword [rsp+16]
3520   |.else
3521   |  movsd qword [rsp-8], xmm0  // Use red zone.
3522   |  movsd qword [rsp-16], xmm1
3523   |  fld qword [rsp-8]
3524   |  fld qword [rsp-16]
3525   |.endif
3526   |  je >1
3527   |  fpatan
3528   |7:
3529   |.if X64WIN
3530   |  fstp qword [rsp+8]         // Use scratch area.
3531   |  movsd xmm0, qword [rsp+8]
3532   |.else
3533   |  fstp qword [rsp-8]         // Use red zone.
3534   |  movsd xmm0, qword [rsp-8]
3535   |.endif
3536   |  ret
3537   |1: ; fxch; fscale; fpop1; jmp <7
3538   |2: ; cmp foldop, 11; je >1; ja >9
3539   |  minsd xmm0, xmm1; ret
3540   |1: ; maxsd xmm0, xmm1; ret
3541   |9: ; int3                            // Bad op.
3542   |
3543   |.elif SSE  // x86 calling convention with SSE ops.
3544   |
3545   |  .define foldop, eax
3546   |  mov foldop, [esp+20]
3547   |  movsd xmm0, qword [esp+4]
3548   |  movsd xmm1, qword [esp+12]
3549   |  cmp foldop, 1; je >1; ja >2
3550   |  addsd xmm0, xmm1
3551   |7:
3552   |  movsd qword [esp+4], xmm0  // Overwrite callee-owned args.
3553   |  fld qword [esp+4]
3554   |  ret
3555   |1: ; subsd xmm0, xmm1; jmp <7
3556   |2: ; cmp foldop, 3; je >1; ja >2
3557   |  mulsd xmm0, xmm1; jmp <7
3558   |1: ; divsd xmm0, xmm1; jmp <7
3559   |2: ; cmp foldop, 5
3560   |  je >1; ja >2
3561   |  call ->vm_mod; jmp <7
3562   |1: ; pop edx; call ->vm_pow; push edx; jmp <7  // Writes to scratch area.
3563   |2: ; cmp foldop, 7; je >1; ja >2
3564   |  sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7
3565   |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7
3566   |2: ; cmp foldop, 9; ja >2
3567   |  fld qword [esp+4]          // Reload from stack
3568   |  fld qword [esp+12]
3569   |  je >1
3570   |  fpatan; ret
3571   |1: ; fxch; fscale; fpop1; ret
3572   |2: ; cmp foldop, 11; je >1; ja >9
3573   |  minsd xmm0, xmm1; jmp <7
3574   |1: ; maxsd xmm0, xmm1; jmp <7
3575   |9: ; int3                            // Bad op.
3576   |
3577   |.else  // x86 calling convention with x87 ops.
3578   |
3579   |  mov eax, [esp+20]
3580   |  fld qword [esp+4]
3581   |  fld qword [esp+12]
3582   |  cmp eax, 1; je >1; ja >2
3583   |  faddp st1; ret
3584   |1: ; fsubp st1; ret
3585   |2: ; cmp eax, 3; je >1; ja >2
3586   |  fmulp st1; ret
3587   |1: ; fdivp st1; ret
3588   |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow
3589   |  cmp eax, 7; je >1; ja >2
3590   |  fpop; fchs; ret
3591   |1: ; fpop; fabs; ret
3592   |2: ; cmp eax, 9; je >1; ja >2
3593   |  fpatan; ret
3594   |1: ; fxch; fscale; fpop1; ret
3595   |2: ; cmp eax, 11; je >1; ja >9
3596   |  fucomi st1; fcmovnbe st1; fpop1; ret
3597   |1: ; fucomi st1; fcmovbe st1; fpop1; ret
3598   |9: ; int3                            // Bad op.
3599   |
3600   |.endif
3601   |
3602   |//-----------------------------------------------------------------------
3603   |//-- Miscellaneous functions --------------------------------------------
3604   |//-----------------------------------------------------------------------
3605   |
3606   |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3607   |->vm_cpuid:
3608   |.if X64
3609   |  mov eax, CARG1d
3610   |  .if X64WIN; push rsi; mov rsi, CARG2; .endif
3611   |  push rbx
3612   |  cpuid
3613   |  mov [rsi], eax
3614   |  mov [rsi+4], ebx
3615   |  mov [rsi+8], ecx
3616   |  mov [rsi+12], edx
3617   |  pop rbx
3618   |  .if X64WIN; pop rsi; .endif
3619   |  ret
3620   |.else
3621   |  pushfd
3622   |  pop edx
3623   |  mov ecx, edx
3624   |  xor edx, 0x00200000                // Toggle ID bit in flags.
3625   |  push edx
3626   |  popfd
3627   |  pushfd
3628   |  pop edx
3629   |  xor eax, eax                       // Zero means no features supported.
3630   |  cmp ecx, edx
3631   |  jz >1                              // No ID toggle means no CPUID support.
3632   |  mov eax, [esp+4]                   // Argument 1 is function number.
3633   |  push edi
3634   |  push ebx
3635   |  cpuid
3636   |  mov edi, [esp+16]                  // Argument 2 is result area.
3637   |  mov [edi], eax
3638   |  mov [edi+4], ebx
3639   |  mov [edi+8], ecx
3640   |  mov [edi+12], edx
3641   |  pop ebx
3642   |  pop edi
3643   |1:
3644   |  ret
3645   |.endif
3646   |
3647   |//-----------------------------------------------------------------------
3648   |//-- Assertions ---------------------------------------------------------
3649   |//-----------------------------------------------------------------------
3650   |
3651   |->assert_bad_for_arg_type:
3652 #ifdef LUA_USE_ASSERT
3653   |  int3
3654 #endif
3655   |  int3
3656   |
3657   |//-----------------------------------------------------------------------
3658   |//-- FFI helper functions -----------------------------------------------
3659   |//-----------------------------------------------------------------------
3660   |
3661   |// Handler for callback functions. Callback slot number in ah/al.
3662   |->vm_ffi_callback:
3663   |.if FFI
3664   |.type CTSTATE, CTState, PC
3665   |.if not X64
3666   |  sub esp, 16                        // Leave room for SAVE_ERRF etc.
3667   |.endif
3668   |  saveregs_  // ebp/rbp already saved. ebp now holds global_State *.
3669   |  lea DISPATCH, [ebp+GG_G2DISP]
3670   |  mov CTSTATE, GL:ebp->ctype_state
3671   |  movzx eax, ax
3672   |  mov CTSTATE->cb.slot, eax
3673   |.if X64
3674   |  mov CTSTATE->cb.gpr[0], CARG1
3675   |  mov CTSTATE->cb.gpr[1], CARG2
3676   |  mov CTSTATE->cb.gpr[2], CARG3
3677   |  mov CTSTATE->cb.gpr[3], CARG4
3678   |  movsd qword CTSTATE->cb.fpr[0], xmm0
3679   |  movsd qword CTSTATE->cb.fpr[1], xmm1
3680   |  movsd qword CTSTATE->cb.fpr[2], xmm2
3681   |  movsd qword CTSTATE->cb.fpr[3], xmm3
3682   |.if X64WIN
3683   |  lea rax, [rsp+CFRAME_SIZE+4*8]
3684   |.else
3685   |  lea rax, [rsp+CFRAME_SIZE]
3686   |  mov CTSTATE->cb.gpr[4], CARG5
3687   |  mov CTSTATE->cb.gpr[5], CARG6
3688   |  movsd qword CTSTATE->cb.fpr[4], xmm4
3689   |  movsd qword CTSTATE->cb.fpr[5], xmm5
3690   |  movsd qword CTSTATE->cb.fpr[6], xmm6
3691   |  movsd qword CTSTATE->cb.fpr[7], xmm7
3692   |.endif
3693   |  mov CTSTATE->cb.stack, rax
3694   |  mov CARG2, rsp
3695   |.else
3696   |  lea eax, [esp+CFRAME_SIZE+16]
3697   |  mov CTSTATE->cb.gpr[0], FCARG1
3698   |  mov CTSTATE->cb.gpr[1], FCARG2
3699   |  mov CTSTATE->cb.stack, eax
3700   |  mov FCARG1, [esp+CFRAME_SIZE+12]   // Move around misplaced retaddr/ebp.
3701   |  mov FCARG2, [esp+CFRAME_SIZE+8]
3702   |  mov SAVE_RET, FCARG1
3703   |  mov SAVE_R4, FCARG2
3704   |  mov FCARG2, esp
3705   |.endif
3706   |  mov SAVE_PC, CTSTATE               // Any value outside of bytecode is ok.
3707   |  mov FCARG1, CTSTATE
3708   |  call extern lj_ccallback_enter@8   // (CTState *cts, void *cf)
3709   |  // lua_State * returned in eax (RD).
3710   |  set_vmstate INTERP
3711   |  mov BASE, L:RD->base
3712   |  mov RD, L:RD->top
3713   |  sub RD, BASE
3714   |  mov LFUNC:RB, [BASE-8]
3715   |  shr RD, 3
3716   |  add RD, 1
3717   |  ins_callt
3718   |.endif
3719   |
3720   |->cont_ffi_callback:                 // Return from FFI callback.
3721   |.if FFI
3722   |  mov L:RA, SAVE_L
3723   |  mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
3724   |  mov aword CTSTATE->L, L:RAa
3725   |  mov L:RA->base, BASE
3726   |  mov L:RA->top, RB
3727   |  mov FCARG1, CTSTATE
3728   |  mov FCARG2, RC
3729   |  call extern lj_ccallback_leave@8   // (CTState *cts, TValue *o)
3730   |.if X64
3731   |  mov rax, CTSTATE->cb.gpr[0]
3732   |  movsd xmm0, qword CTSTATE->cb.fpr[0]
3733   |  jmp ->vm_leave_unw
3734   |.else
3735   |  mov L:RB, SAVE_L
3736   |  mov eax, CTSTATE->cb.gpr[0]
3737   |  mov edx, CTSTATE->cb.gpr[1]
3738   |  cmp dword CTSTATE->cb.gpr[2], 1
3739   |  jb >7
3740   |  je >6
3741   |  fld qword CTSTATE->cb.fpr[0].d
3742   |  jmp >7
3743   |6:
3744   |  fld dword CTSTATE->cb.fpr[0].f
3745   |7:
3746   |  mov ecx, L:RB->top
3747   |  movzx ecx, word [ecx+6]            // Get stack adjustment and copy up.
3748   |  mov SAVE_L, ecx                    // Must be one slot above SAVE_RET
3749   |  restoreregs
3750   |  pop ecx                            // Move return addr from SAVE_RET.
3751   |  add esp, [esp]                     // Adjust stack.
3752   |  add esp, 16
3753   |  push ecx
3754   |  ret
3755   |.endif
3756   |.endif
3757   |
3758   |->vm_ffi_call@4:                     // Call C function via FFI.
3759   |  // Caveat: needs special frame unwinding, see below.
3760   |.if FFI
3761   |.if X64
3762   |  .type CCSTATE, CCallState, rbx
3763   |  push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3764   |.else
3765   |  .type CCSTATE, CCallState, ebx
3766   |  push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3767   |.endif
3768   |
3769   |  // Readjust stack.
3770   |.if X64
3771   |  mov eax, CCSTATE->spadj
3772   |  sub rsp, rax
3773   |.else
3774   |  sub esp, CCSTATE->spadj
3775   |.if WIN
3776   |  mov CCSTATE->spadj, esp
3777   |.endif
3778   |.endif
3779   |
3780   |  // Copy stack slots.
3781   |  movzx ecx, byte CCSTATE->nsp
3782   |  sub ecx, 1
3783   |  js >2
3784   |1:
3785   |.if X64
3786   |  mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3787   |  mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3788   |.else
3789   |  mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3790   |  mov [esp+ecx*4], eax
3791   |.endif
3792   |  sub ecx, 1
3793   |  jns <1
3794   |2:
3795   |
3796   |.if X64
3797   |  movzx eax, byte CCSTATE->nfpr
3798   |  mov CARG1, CCSTATE->gpr[0]
3799   |  mov CARG2, CCSTATE->gpr[1]
3800   |  mov CARG3, CCSTATE->gpr[2]
3801   |  mov CARG4, CCSTATE->gpr[3]
3802   |.if not X64WIN
3803   |  mov CARG5, CCSTATE->gpr[4]
3804   |  mov CARG6, CCSTATE->gpr[5]
3805   |.endif
3806   |  test eax, eax; jz >5
3807   |  movaps xmm0, CCSTATE->fpr[0]
3808   |  movaps xmm1, CCSTATE->fpr[1]
3809   |  movaps xmm2, CCSTATE->fpr[2]
3810   |  movaps xmm3, CCSTATE->fpr[3]
3811   |.if not X64WIN
3812   |  cmp eax, 4; jbe >5
3813   |  movaps xmm4, CCSTATE->fpr[4]
3814   |  movaps xmm5, CCSTATE->fpr[5]
3815   |  movaps xmm6, CCSTATE->fpr[6]
3816   |  movaps xmm7, CCSTATE->fpr[7]
3817   |.endif
3818   |5:
3819   |.else
3820   |  mov FCARG1, CCSTATE->gpr[0]
3821   |  mov FCARG2, CCSTATE->gpr[1]
3822   |.endif
3823   |
3824   |  call aword CCSTATE->func
3825   |
3826   |.if X64
3827   |  mov CCSTATE->gpr[0], rax
3828   |  movaps CCSTATE->fpr[0], xmm0
3829   |.if not X64WIN
3830   |  mov CCSTATE->gpr[1], rdx
3831   |  movaps CCSTATE->fpr[1], xmm1
3832   |.endif
3833   |.else
3834   |  mov CCSTATE->gpr[0], eax
3835   |  mov CCSTATE->gpr[1], edx
3836   |  cmp byte CCSTATE->resx87, 1
3837   |  jb >7
3838   |  je >6
3839   |  fstp qword CCSTATE->fpr[0].d[0]
3840   |  jmp >7
3841   |6:
3842   |  fstp dword CCSTATE->fpr[0].f[0]
3843   |7:
3844   |.if WIN
3845   |  sub CCSTATE->spadj, esp
3846   |.endif
3847   |.endif
3848   |
3849   |.if X64
3850   |  mov rbx, [rbp-8]; leave; ret
3851   |.else
3852   |  mov ebx, [ebp-4]; leave; ret
3853   |.endif
3854   |.endif
3855   |// Note: vm_ffi_call must be the last function in this object file!
3856   |
3857   |//-----------------------------------------------------------------------
3860 /* Generate the code for a single instruction. */
3861 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3863   int vk = 0;
3864   |// Note: aligning all instructions does not pay off.
3865   |=>defop:
3867   switch (op) {
3869   /* -- Comparison ops ---------------------------------------------------- */
3871   /* Remember: all ops branch for a true comparison, fall through otherwise. */
3873   |.macro jmp_comp, lt, ge, le, gt, target
3874   ||switch (op) {
3875   ||case BC_ISLT:
3876   |   lt target
3877   ||break;
3878   ||case BC_ISGE:
3879   |   ge target
3880   ||break;
3881   ||case BC_ISLE:
3882   |   le target
3883   ||break;
3884   ||case BC_ISGT:
3885   |   gt target
3886   ||break;
3887   ||default: break;  /* Shut up GCC. */
3888   ||}
3889   |.endmacro
3891   case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3892     |  // RA = src1, RD = src2, JMP with RD = target
3893     |  ins_AD
3894     |.if DUALNUM
3895     |  checkint RA, >7
3896     |  checkint RD, >8
3897     |  mov RB, dword [BASE+RA*8]
3898     |  add PC, 4
3899     |  cmp RB, dword [BASE+RD*8]
3900     |  jmp_comp jge, jl, jg, jle, >9
3901     |6:
3902     |  movzx RD, PC_RD
3903     |  branchPC RD
3904     |9:
3905     |  ins_next
3906     |
3907     |7:  // RA is not an integer.
3908     |  ja ->vmeta_comp
3909     |  // RA is a number.
3910     |  cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3911     |  // RA is a number, RD is an integer.
3912     |.if SSE
3913     |  cvtsi2sd xmm0, dword [BASE+RD*8]
3914     |  jmp >2
3915     |.else
3916     |  fld qword [BASE+RA*8]
3917     |  fild dword [BASE+RD*8]
3918     |  jmp >3
3919     |.endif
3920     |
3921     |8:  // RA is an integer, RD is not an integer.
3922     |  ja ->vmeta_comp
3923     |  // RA is an integer, RD is a number.
3924     |.if SSE
3925     |  cvtsi2sd xmm1, dword [BASE+RA*8]
3926     |  movsd xmm0, qword [BASE+RD*8]
3927     |  add PC, 4
3928     |  ucomisd xmm0, xmm1
3929     |  jmp_comp jbe, ja, jb, jae, <9
3930     |  jmp <6
3931     |.else
3932     |  fild dword [BASE+RA*8]
3933     |  jmp >2
3934     |.endif
3935     |.else
3936     |  checknum RA, ->vmeta_comp
3937     |  checknum RD, ->vmeta_comp
3938     |.endif
3939     |.if SSE
3940     |1:
3941     |  movsd xmm0, qword [BASE+RD*8]
3942     |2:
3943     |  add PC, 4
3944     |  ucomisd xmm0, qword [BASE+RA*8]
3945     |3:
3946     |.else
3947     |1:
3948     |  fld qword [BASE+RA*8]            // Reverse order, i.e like cmp D, A.
3949     |2:
3950     |  fld qword [BASE+RD*8]
3951     |3:
3952     |  add PC, 4
3953     |  fcomparepp
3954     |.endif
3955     |  // Unordered: all of ZF CF PF set, ordered: PF clear.
3956     |  // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
3957     |.if DUALNUM
3958     |  jmp_comp jbe, ja, jb, jae, <9
3959     |  jmp <6
3960     |.else
3961     |  jmp_comp jbe, ja, jb, jae, >1
3962     |  movzx RD, PC_RD
3963     |  branchPC RD
3964     |1:
3965     |  ins_next
3966     |.endif
3967     break;
3969   case BC_ISEQV: case BC_ISNEV:
3970     vk = op == BC_ISEQV;
3971     |  ins_AD   // RA = src1, RD = src2, JMP with RD = target
3972     |  mov RB, [BASE+RD*8+4]
3973     |  add PC, 4
3974     |.if DUALNUM
3975     |  cmp RB, LJ_TISNUM; jne >7
3976     |  checkint RA, >8
3977     |  mov RB, dword [BASE+RD*8]
3978     |  cmp RB, dword [BASE+RA*8]
3979     if (vk) {
3980       |  jne >9
3981     } else {
3982       |  je >9
3983     }
3984     |  movzx RD, PC_RD
3985     |  branchPC RD
3986     |9:
3987     |  ins_next
3988     |
3989     |7:  // RD is not an integer.
3990     |  ja >5
3991     |  // RD is a number.
3992     |  cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
3993     |  // RD is a number, RA is an integer.
3994     |.if SSE
3995     |  cvtsi2sd xmm0, dword [BASE+RA*8]
3996     |.else
3997     |  fild dword [BASE+RA*8]
3998     |.endif
3999     |  jmp >2
4000     |
4001     |8:  // RD is an integer, RA is not an integer.
4002     |  ja >5
4003     |  // RD is an integer, RA is a number.
4004     |.if SSE
4005     |  cvtsi2sd xmm0, dword [BASE+RD*8]
4006     |  ucomisd xmm0, qword [BASE+RA*8]
4007     |.else
4008     |  fild dword [BASE+RD*8]
4009     |  fld qword [BASE+RA*8]
4010     |.endif
4011     |  jmp >4
4012     |
4013     |.else
4014     |  cmp RB, LJ_TISNUM; jae >5
4015     |  checknum RA, >5
4016     |.endif
4017     |.if SSE
4018     |1:
4019     |  movsd xmm0, qword [BASE+RA*8]
4020     |2:
4021     |  ucomisd xmm0, qword [BASE+RD*8]
4022     |4:
4023     |.else
4024     |1:
4025     |  fld qword [BASE+RA*8]
4026     |2:
4027     |  fld qword [BASE+RD*8]
4028     |4:
4029     |  fcomparepp
4030     |.endif
4031   iseqne_fp:
4032     if (vk) {
4033       |  jp >2                          // Unordered means not equal.
4034       |  jne >2
4035     } else {
4036       |  jp >2                          // Unordered means not equal.
4037       |  je >1
4038     }
4039   iseqne_end:
4040     if (vk) {
4041       |1:                               // EQ: Branch to the target.
4042       |  movzx RD, PC_RD
4043       |  branchPC RD
4044       |2:                               // NE: Fallthrough to next instruction.
4045       |.if not FFI
4046       |3:
4047       |.endif
4048     } else {
4049       |.if not FFI
4050       |3:
4051       |.endif
4052       |2:                               // NE: Branch to the target.
4053       |  movzx RD, PC_RD
4054       |  branchPC RD
4055       |1:                               // EQ: Fallthrough to next instruction.
4056     }
4057     if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
4058                        op == BC_ISEQN || op == BC_ISNEN)) {
4059       |  jmp <9
4060     } else {
4061       |  ins_next
4062     }
4063     |
4064     if (op == BC_ISEQV || op == BC_ISNEV) {
4065       |5:  // Either or both types are not numbers.
4066       |.if FFI
4067       |  cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4068       |  checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
4069       |.endif
4070       |  checktp RA, RB                 // Compare types.
4071       |  jne <2                         // Not the same type?
4072       |  cmp RB, LJ_TISPRI
4073       |  jae <1                         // Same type and primitive type?
4074       |
4075       |  // Same types and not a primitive type. Compare GCobj or pvalue.
4076       |  mov RA, [BASE+RA*8]
4077       |  mov RD, [BASE+RD*8]
4078       |  cmp RA, RD
4079       |  je <1                          // Same GCobjs or pvalues?
4080       |  cmp RB, LJ_TISTABUD
4081       |  ja <2                          // Different objects and not table/ud?
4082       |.if X64
4083       |  cmp RB, LJ_TUDATA              // And not 64 bit lightuserdata.
4084       |  jb <2
4085       |.endif
4086       |
4087       |  // Different tables or userdatas. Need to check __eq metamethod.
4088       |  // Field metatable must be at same offset for GCtab and GCudata!
4089       |  mov TAB:RB, TAB:RA->metatable
4090       |  test TAB:RB, TAB:RB
4091       |  jz <2                          // No metatable?
4092       |  test byte TAB:RB->nomm, 1<<MM_eq
4093       |  jnz <2                         // Or 'no __eq' flag set?
4094       if (vk) {
4095         |  xor RB, RB                   // ne = 0
4096       } else {
4097         |  mov RB, 1                    // ne = 1
4098       }
4099       |  jmp ->vmeta_equal              // Handle __eq metamethod.
4100     } else {
4101       |.if FFI
4102       |3:
4103       |  cmp RB, LJ_TCDATA
4104       if (LJ_DUALNUM && vk) {
4105         |  jne <9
4106       } else {
4107         |  jne <2
4108       }
4109       |  jmp ->vmeta_equal_cd
4110       |.endif
4111     }
4112     break;
4113   case BC_ISEQS: case BC_ISNES:
4114     vk = op == BC_ISEQS;
4115     |  ins_AND  // RA = src, RD = str const, JMP with RD = target
4116     |  mov RB, [BASE+RA*8+4]
4117     |  add PC, 4
4118     |  cmp RB, LJ_TSTR; jne >3
4119     |  mov RA, [BASE+RA*8]
4120     |  cmp RA, [KBASE+RD*4]
4121   iseqne_test:
4122     if (vk) {
4123       |  jne >2
4124     } else {
4125       |  je >1
4126     }
4127     goto iseqne_end;
4128   case BC_ISEQN: case BC_ISNEN:
4129     vk = op == BC_ISEQN;
4130     |  ins_AD   // RA = src, RD = num const, JMP with RD = target
4131     |  mov RB, [BASE+RA*8+4]
4132     |  add PC, 4
4133     |.if DUALNUM
4134     |  cmp RB, LJ_TISNUM; jne >7
4135     |  cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
4136     |  mov RB, dword [KBASE+RD*8]
4137     |  cmp RB, dword [BASE+RA*8]
4138     if (vk) {
4139       |  jne >9
4140     } else {
4141       |  je >9
4142     }
4143     |  movzx RD, PC_RD
4144     |  branchPC RD
4145     |9:
4146     |  ins_next
4147     |
4148     |7:  // RA is not an integer.
4149     |  ja >3
4150     |  // RA is a number.
4151     |  cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
4152     |  // RA is a number, RD is an integer.
4153     |.if SSE
4154     |  cvtsi2sd xmm0, dword [KBASE+RD*8]
4155     |.else
4156     |  fild dword [KBASE+RD*8]
4157     |.endif
4158     |  jmp >2
4159     |
4160     |8:  // RA is an integer, RD is a number.
4161     |.if SSE
4162     |  cvtsi2sd xmm0, dword [BASE+RA*8]
4163     |  ucomisd xmm0, qword [KBASE+RD*8]
4164     |.else
4165     |  fild dword [BASE+RA*8]
4166     |  fld qword [KBASE+RD*8]
4167     |.endif
4168     |  jmp >4
4169     |.else
4170     |  cmp RB, LJ_TISNUM; jae >3
4171     |.endif
4172     |.if SSE
4173     |1:
4174     |  movsd xmm0, qword [KBASE+RD*8]
4175     |2:
4176     |  ucomisd xmm0, qword [BASE+RA*8]
4177     |4:
4178     |.else
4179     |1:
4180     |  fld qword [KBASE+RD*8]
4181     |2:
4182     |  fld qword [BASE+RA*8]
4183     |4:
4184     |  fcomparepp
4185     |.endif
4186     goto iseqne_fp;
4187   case BC_ISEQP: case BC_ISNEP:
4188     vk = op == BC_ISEQP;
4189     |  ins_AND  // RA = src, RD = primitive type (~), JMP with RD = target
4190     |  mov RB, [BASE+RA*8+4]
4191     |  add PC, 4
4192     |  cmp RB, RD
4193     if (!LJ_HASFFI) goto iseqne_test;
4194     if (vk) {
4195       |  jne >3
4196       |  movzx RD, PC_RD
4197       |  branchPC RD
4198       |2:
4199       |  ins_next
4200       |3:
4201       |  cmp RB, LJ_TCDATA; jne <2
4202       |  jmp ->vmeta_equal_cd
4203     } else {
4204       |  je >2
4205       |  cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4206       |  movzx RD, PC_RD
4207       |  branchPC RD
4208       |2:
4209       |  ins_next
4210     }
4211     break;
4213   /* -- Unary test and copy ops ------------------------------------------- */
4215   case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
4216     |  ins_AD   // RA = dst or unused, RD = src, JMP with RD = target
4217     |  mov RB, [BASE+RD*8+4]
4218     |  add PC, 4
4219     |  cmp RB, LJ_TISTRUECOND
4220     if (op == BC_IST || op == BC_ISTC) {
4221       |  jae >1
4222     } else {
4223       |  jb >1
4224     }
4225     if (op == BC_ISTC || op == BC_ISFC) {
4226       |  mov [BASE+RA*8+4], RB
4227       |  mov RB, [BASE+RD*8]
4228       |  mov [BASE+RA*8], RB
4229     }
4230     |  movzx RD, PC_RD
4231     |  branchPC RD
4232     |1:                                 // Fallthrough to the next instruction.
4233     |  ins_next
4234     break;
4236   /* -- Unary ops --------------------------------------------------------- */
4238   case BC_MOV:
4239     |  ins_AD   // RA = dst, RD = src
4240     |.if X64
4241     |  mov RBa, [BASE+RD*8]
4242     |  mov [BASE+RA*8], RBa
4243     |.else
4244     |  mov RB, [BASE+RD*8+4]
4245     |  mov RD, [BASE+RD*8]
4246     |  mov [BASE+RA*8+4], RB
4247     |  mov [BASE+RA*8], RD
4248     |.endif
4249     |  ins_next_
4250     break;
4251   case BC_NOT:
4252     |  ins_AD   // RA = dst, RD = src
4253     |  xor RB, RB
4254     |  checktp RD, LJ_TISTRUECOND
4255     |  adc RB, LJ_TTRUE
4256     |  mov [BASE+RA*8+4], RB
4257     |  ins_next
4258     break;
4259   case BC_UNM:
4260     |  ins_AD   // RA = dst, RD = src
4261     |.if DUALNUM
4262     |  checkint RD, >5
4263     |  mov RB, [BASE+RD*8]
4264     |  neg RB
4265     |  jo >4
4266     |  mov dword [BASE+RA*8+4], LJ_TISNUM
4267     |  mov dword [BASE+RA*8], RB
4268     |9:
4269     |  ins_next
4270     |4:
4271     |  mov dword [BASE+RA*8+4], 0x41e00000  // 2^31.
4272     |  mov dword [BASE+RA*8], 0
4273     |  jmp <9
4274     |5:
4275     |  ja ->vmeta_unm
4276     |.else
4277     |  checknum RD, ->vmeta_unm
4278     |.endif
4279     |.if SSE
4280     |  movsd xmm0, qword [BASE+RD*8]
4281     |  sseconst_sign xmm1, RDa
4282     |  xorps xmm0, xmm1
4283     |  movsd qword [BASE+RA*8], xmm0
4284     |.else
4285     |  fld qword [BASE+RD*8]
4286     |  fchs
4287     |  fstp qword [BASE+RA*8]
4288     |.endif
4289     |.if DUALNUM
4290     |  jmp <9
4291     |.else
4292     |  ins_next
4293     |.endif
4294     break;
4295   case BC_LEN:
4296     |  ins_AD   // RA = dst, RD = src
4297     |  checkstr RD, >2
4298     |  mov STR:RD, [BASE+RD*8]
4299     |.if DUALNUM
4300     |  mov RD, dword STR:RD->len
4301     |1:
4302     |  mov dword [BASE+RA*8+4], LJ_TISNUM
4303     |  mov dword [BASE+RA*8], RD
4304     |.elif SSE
4305     |  xorps xmm0, xmm0
4306     |  cvtsi2sd xmm0, dword STR:RD->len
4307     |1:
4308     |  movsd qword [BASE+RA*8], xmm0
4309     |.else
4310     |  fild dword STR:RD->len
4311     |1:
4312     |  fstp qword [BASE+RA*8]
4313     |.endif
4314     |  ins_next
4315     |2:
4316     |  checktab RD, ->vmeta_len
4317     |  mov TAB:FCARG1, [BASE+RD*8]
4318 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4319     |  mov TAB:RB, TAB:FCARG1->metatable
4320     |  cmp TAB:RB, 0
4321     |  jnz >9
4322     |3:
4323 #endif
4324     |->BC_LEN_Z:
4325     |  mov RB, BASE                     // Save BASE.
4326     |  call extern lj_tab_len@4         // (GCtab *t)
4327     |  // Length of table returned in eax (RD).
4328     |.if DUALNUM
4329     |  // Nothing to do.
4330     |.elif SSE
4331     |  cvtsi2sd xmm0, RD
4332     |.else
4333     |  mov ARG1, RD
4334     |  fild ARG1
4335     |.endif
4336     |  mov BASE, RB                     // Restore BASE.
4337     |  movzx RA, PC_RA
4338     |  jmp <1
4339 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4340     |9:  // Check for __len.
4341     |  test byte TAB:RB->nomm, 1<<MM_len
4342     |  jnz <3
4343     |  jmp ->vmeta_len                  // 'no __len' flag NOT set: check.
4344 #endif
4345     break;
4347   /* -- Binary ops -------------------------------------------------------- */
4349     |.macro ins_arithpre, x87ins, sseins, ssereg
4350     |  ins_ABC
4351     ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4352     ||switch (vk) {
4353     ||case 0:
4354     |   checknum RB, ->vmeta_arith_vn
4355     |   .if DUALNUM
4356     |     cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
4357     |   .endif
4358     |   .if SSE
4359     |     movsd xmm0, qword [BASE+RB*8]
4360     |     sseins ssereg, qword [KBASE+RC*8]
4361     |   .else
4362     |     fld qword [BASE+RB*8]
4363     |     x87ins qword [KBASE+RC*8]
4364     |   .endif
4365     ||  break;
4366     ||case 1:
4367     |   checknum RB, ->vmeta_arith_nv
4368     |   .if DUALNUM
4369     |     cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
4370     |   .endif
4371     |   .if SSE
4372     |     movsd xmm0, qword [KBASE+RC*8]
4373     |     sseins ssereg, qword [BASE+RB*8]
4374     |   .else
4375     |     fld qword [KBASE+RC*8]
4376     |     x87ins qword [BASE+RB*8]
4377     |   .endif
4378     ||  break;
4379     ||default:
4380     |   checknum RB, ->vmeta_arith_vv
4381     |   checknum RC, ->vmeta_arith_vv
4382     |   .if SSE
4383     |     movsd xmm0, qword [BASE+RB*8]
4384     |     sseins ssereg, qword [BASE+RC*8]
4385     |   .else
4386     |     fld qword [BASE+RB*8]
4387     |     x87ins qword [BASE+RC*8]
4388     |   .endif
4389     ||  break;
4390     ||}
4391     |.endmacro
4392     |
4393     |.macro ins_arithdn, intins
4394     |  ins_ABC
4395     ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4396     ||switch (vk) {
4397     ||case 0:
4398     |   checkint RB, ->vmeta_arith_vn
4399     |   cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
4400     |   mov RB, [BASE+RB*8]
4401     |   intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
4402     ||  break;
4403     ||case 1:
4404     |   checkint RB, ->vmeta_arith_nv
4405     |   cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
4406     |   mov RC, [KBASE+RC*8]
4407     |   intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
4408     ||  break;
4409     ||default:
4410     |   checkint RB, ->vmeta_arith_vv
4411     |   checkint RC, ->vmeta_arith_vv
4412     |   mov RB, [BASE+RB*8]
4413     |   intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
4414     ||  break;
4415     ||}
4416     |  mov dword [BASE+RA*8+4], LJ_TISNUM
4417     ||if (vk == 1) {
4418     |   mov dword [BASE+RA*8], RC
4419     ||} else {
4420     |   mov dword [BASE+RA*8], RB
4421     ||}
4422     |  ins_next
4423     |.endmacro
4424     |
4425     |.macro ins_arithpost
4426     |.if SSE
4427     |  movsd qword [BASE+RA*8], xmm0
4428     |.else
4429     |  fstp qword [BASE+RA*8]
4430     |.endif
4431     |.endmacro
4432     |
4433     |.macro ins_arith, x87ins, sseins
4434     |  ins_arithpre x87ins, sseins, xmm0
4435     |  ins_arithpost
4436     |  ins_next
4437     |.endmacro
4438     |
4439     |.macro ins_arith, intins, x87ins, sseins
4440     |.if DUALNUM
4441     |  ins_arithdn intins
4442     |.else
4443     |  ins_arith, x87ins, sseins
4444     |.endif
4445     |.endmacro
4447     |  // RA = dst, RB = src1 or num const, RC = src2 or num const
4448   case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
4449     |  ins_arith add, fadd, addsd
4450     break;
4451   case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
4452     |  ins_arith sub, fsub, subsd
4453     break;
4454   case BC_MULVN: case BC_MULNV: case BC_MULVV:
4455     |  ins_arith imul, fmul, mulsd
4456     break;
4457   case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
4458     |  ins_arith fdiv, divsd
4459     break;
4460   case BC_MODVN:
4461     |  ins_arithpre fld, movsd, xmm1
4462     |->BC_MODVN_Z:
4463     |  call ->vm_mod
4464     |  ins_arithpost
4465     |  ins_next
4466     break;
4467   case BC_MODNV: case BC_MODVV:
4468     |  ins_arithpre fld, movsd, xmm1
4469     |  jmp ->BC_MODVN_Z                 // Avoid 3 copies. It's slow anyway.
4470     break;
4471   case BC_POW:
4472     |  ins_arithpre fld, movsd, xmm1
4473     |  call ->vm_pow
4474     |  ins_arithpost
4475     |  ins_next
4476     break;
4478   case BC_CAT:
4479     |  ins_ABC  // RA = dst, RB = src_start, RC = src_end
4480     |.if X64
4481     |  mov L:CARG1d, SAVE_L
4482     |  mov L:CARG1d->base, BASE
4483     |  lea CARG2d, [BASE+RC*8]
4484     |  mov CARG3d, RC
4485     |  sub CARG3d, RB
4486     |->BC_CAT_Z:
4487     |  mov L:RB, L:CARG1d
4488     |.else
4489     |  lea RA, [BASE+RC*8]
4490     |  sub RC, RB
4491     |  mov ARG2, RA
4492     |  mov ARG3, RC
4493     |->BC_CAT_Z:
4494     |  mov L:RB, SAVE_L
4495     |  mov ARG1, L:RB
4496     |  mov L:RB->base, BASE
4497     |.endif
4498     |  mov SAVE_PC, PC
4499     |  call extern lj_meta_cat          // (lua_State *L, TValue *top, int left)
4500     |  // NULL (finished) or TValue * (metamethod) returned in eax (RC).
4501     |  mov BASE, L:RB->base
4502     |  test RC, RC
4503     |  jnz ->vmeta_binop
4504     |  movzx RB, PC_RB                  // Copy result to Stk[RA] from Stk[RB].
4505     |  movzx RA, PC_RA
4506     |.if X64
4507     |  mov RCa, [BASE+RB*8]
4508     |  mov [BASE+RA*8], RCa
4509     |.else
4510     |  mov RC, [BASE+RB*8+4]
4511     |  mov RB, [BASE+RB*8]
4512     |  mov [BASE+RA*8+4], RC
4513     |  mov [BASE+RA*8], RB
4514     |.endif
4515     |  ins_next
4516     break;
4518   /* -- Constant ops ------------------------------------------------------ */
4520   case BC_KSTR:
4521     |  ins_AND  // RA = dst, RD = str const (~)
4522     |  mov RD, [KBASE+RD*4]
4523     |  mov dword [BASE+RA*8+4], LJ_TSTR
4524     |  mov [BASE+RA*8], RD
4525     |  ins_next
4526     break;
4527   case BC_KCDATA:
4528     |.if FFI
4529     |  ins_AND  // RA = dst, RD = cdata const (~)
4530     |  mov RD, [KBASE+RD*4]
4531     |  mov dword [BASE+RA*8+4], LJ_TCDATA
4532     |  mov [BASE+RA*8], RD
4533     |  ins_next
4534     |.endif
4535     break;
4536   case BC_KSHORT:
4537     |  ins_AD   // RA = dst, RD = signed int16 literal
4538     |.if DUALNUM
4539     |  movsx RD, RDW
4540     |  mov dword [BASE+RA*8+4], LJ_TISNUM
4541     |  mov dword [BASE+RA*8], RD
4542     |.elif SSE
4543     |  movsx RD, RDW                    // Sign-extend literal.
4544     |  cvtsi2sd xmm0, RD
4545     |  movsd qword [BASE+RA*8], xmm0
4546     |.else
4547     |  fild PC_RD                       // Refetch signed RD from instruction.
4548     |  fstp qword [BASE+RA*8]
4549     |.endif
4550     |  ins_next
4551     break;
4552   case BC_KNUM:
4553     |  ins_AD   // RA = dst, RD = num const
4554     |.if SSE
4555     |  movsd xmm0, qword [KBASE+RD*8]
4556     |  movsd qword [BASE+RA*8], xmm0
4557     |.else
4558     |  fld qword [KBASE+RD*8]
4559     |  fstp qword [BASE+RA*8]
4560     |.endif
4561     |  ins_next
4562     break;
4563   case BC_KPRI:
4564     |  ins_AND  // RA = dst, RD = primitive type (~)
4565     |  mov [BASE+RA*8+4], RD
4566     |  ins_next
4567     break;
4568   case BC_KNIL:
4569     |  ins_AD   // RA = dst_start, RD = dst_end
4570     |  lea RA, [BASE+RA*8+12]
4571     |  lea RD, [BASE+RD*8+4]
4572     |  mov RB, LJ_TNIL
4573     |  mov [RA-8], RB                   // Sets minimum 2 slots.
4574     |1:
4575     |  mov [RA], RB
4576     |  add RA, 8
4577     |  cmp RA, RD
4578     |  jbe <1
4579     |  ins_next
4580     break;
4582   /* -- Upvalue and function ops ------------------------------------------ */
4584   case BC_UGET:
4585     |  ins_AD   // RA = dst, RD = upvalue #
4586     |  mov LFUNC:RB, [BASE-8]
4587     |  mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
4588     |  mov RB, UPVAL:RB->v
4589     |.if X64
4590     |  mov RDa, [RB]
4591     |  mov [BASE+RA*8], RDa
4592     |.else
4593     |  mov RD, [RB+4]
4594     |  mov RB, [RB]
4595     |  mov [BASE+RA*8+4], RD
4596     |  mov [BASE+RA*8], RB
4597     |.endif
4598     |  ins_next
4599     break;
4600   case BC_USETV:
4601 #define TV2MARKOFS \
4602  ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
4603     |  ins_AD   // RA = upvalue #, RD = src
4604     |  mov LFUNC:RB, [BASE-8]
4605     |  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4606     |  cmp byte UPVAL:RB->closed, 0
4607     |  mov RB, UPVAL:RB->v
4608     |  mov RA, [BASE+RD*8]
4609     |  mov RD, [BASE+RD*8+4]
4610     |  mov [RB], RA
4611     |  mov [RB+4], RD
4612     |  jz >1
4613     |  // Check barrier for closed upvalue.
4614     |  test byte [RB+TV2MARKOFS], LJ_GC_BLACK           // isblack(uv)
4615     |  jnz >2
4616     |1:
4617     |  ins_next
4618     |
4619     |2:  // Upvalue is black. Check if new value is collectable and white.
4620     |  sub RD, LJ_TISGCV
4621     |  cmp RD, LJ_TISNUM - LJ_TISGCV                    // tvisgcv(v)
4622     |  jbe <1
4623     |  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES     // iswhite(v)
4624     |  jz <1
4625     |  // Crossed a write barrier. Move the barrier forward.
4626     |.if X64 and not X64WIN
4627     |  mov FCARG2, RB
4628     |  mov RB, BASE                     // Save BASE.
4629     |.else
4630     |  xchg FCARG2, RB                  // Save BASE (FCARG2 == BASE).
4631     |.endif
4632     |  lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4633     |  call extern lj_gc_barrieruv@8    // (global_State *g, TValue *tv)
4634     |  mov BASE, RB                     // Restore BASE.
4635     |  jmp <1
4636     break;
4637 #undef TV2MARKOFS
4638   case BC_USETS:
4639     |  ins_AND  // RA = upvalue #, RD = str const (~)
4640     |  mov LFUNC:RB, [BASE-8]
4641     |  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4642     |  mov GCOBJ:RA, [KBASE+RD*4]
4643     |  mov RD, UPVAL:RB->v
4644     |  mov [RD], GCOBJ:RA
4645     |  mov dword [RD+4], LJ_TSTR
4646     |  test byte UPVAL:RB->marked, LJ_GC_BLACK          // isblack(uv)
4647     |  jnz >2
4648     |1:
4649     |  ins_next
4650     |
4651     |2:  // Check if string is white and ensure upvalue is closed.
4652     |  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES     // iswhite(str)
4653     |  jz <1
4654     |  cmp byte UPVAL:RB->closed, 0
4655     |  jz <1
4656     |  // Crossed a write barrier. Move the barrier forward.
4657     |  mov RB, BASE                     // Save BASE (FCARG2 == BASE).
4658     |  mov FCARG2, RD
4659     |  lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4660     |  call extern lj_gc_barrieruv@8    // (global_State *g, TValue *tv)
4661     |  mov BASE, RB                     // Restore BASE.
4662     |  jmp <1
4663     break;
4664   case BC_USETN:
4665     |  ins_AD   // RA = upvalue #, RD = num const
4666     |  mov LFUNC:RB, [BASE-8]
4667     |.if SSE
4668     |  movsd xmm0, qword [KBASE+RD*8]
4669     |.else
4670     |  fld qword [KBASE+RD*8]
4671     |.endif
4672     |  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4673     |  mov RA, UPVAL:RB->v
4674     |.if SSE
4675     |  movsd qword [RA], xmm0
4676     |.else
4677     |  fstp qword [RA]
4678     |.endif
4679     |  ins_next
4680     break;
4681   case BC_USETP:
4682     |  ins_AND  // RA = upvalue #, RD = primitive type (~)
4683     |  mov LFUNC:RB, [BASE-8]
4684     |  mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4685     |  mov RA, UPVAL:RB->v
4686     |  mov [RA+4], RD
4687     |  ins_next
4688     break;
4689   case BC_UCLO:
4690     |  ins_AD   // RA = level, RD = target
4691     |  branchPC RD                      // Do this first to free RD.
4692     |  mov L:RB, SAVE_L
4693     |  cmp dword L:RB->openupval, 0
4694     |  je >1
4695     |  mov L:RB->base, BASE
4696     |  lea FCARG2, [BASE+RA*8]          // Caveat: FCARG2 == BASE
4697     |  mov L:FCARG1, L:RB               // Caveat: FCARG1 == RA
4698     |  call extern lj_func_closeuv@8    // (lua_State *L, TValue *level)
4699     |  mov BASE, L:RB->base
4700     |1:
4701     |  ins_next
4702     break;
4704   case BC_FNEW:
4705     |  ins_AND  // RA = dst, RD = proto const (~) (holding function prototype)
4706     |.if X64
4707     |  mov L:RB, SAVE_L
4708     |  mov L:RB->base, BASE             // Caveat: CARG2d/CARG3d may be BASE.
4709     |  mov CARG3d, [BASE-8]
4710     |  mov CARG2d, [KBASE+RD*4]         // Fetch GCproto *.
4711     |  mov CARG1d, L:RB
4712     |.else
4713     |  mov LFUNC:RA, [BASE-8]
4714     |  mov PROTO:RD, [KBASE+RD*4]       // Fetch GCproto *.
4715     |  mov L:RB, SAVE_L
4716     |  mov ARG3, LFUNC:RA
4717     |  mov ARG2, PROTO:RD
4718     |  mov ARG1, L:RB
4719     |  mov L:RB->base, BASE
4720     |.endif
4721     |  mov SAVE_PC, PC
4722     |  // (lua_State *L, GCproto *pt, GCfuncL *parent)
4723     |  call extern lj_func_newL_gc
4724     |  // GCfuncL * returned in eax (RC).
4725     |  mov BASE, L:RB->base
4726     |  movzx RA, PC_RA
4727     |  mov [BASE+RA*8], LFUNC:RC
4728     |  mov dword [BASE+RA*8+4], LJ_TFUNC
4729     |  ins_next
4730     break;
4732   /* -- Table ops --------------------------------------------------------- */
4734   case BC_TNEW:
4735     |  ins_AD   // RA = dst, RD = hbits|asize
4736     |  mov L:RB, SAVE_L
4737     |  mov L:RB->base, BASE
4738     |  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4739     |  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4740     |  mov SAVE_PC, PC
4741     |  jae >5
4742     |1:
4743     |.if X64
4744     |  mov CARG3d, RD
4745     |  and RD, 0x7ff
4746     |  shr CARG3d, 11
4747     |.else
4748     |  mov RA, RD
4749     |  and RD, 0x7ff
4750     |  shr RA, 11
4751     |  mov ARG3, RA
4752     |.endif
4753     |  cmp RD, 0x7ff
4754     |  je >3
4755     |2:
4756     |.if X64
4757     |  mov L:CARG1d, L:RB
4758     |  mov CARG2d, RD
4759     |.else
4760     |  mov ARG1, L:RB
4761     |  mov ARG2, RD
4762     |.endif
4763     |  call extern lj_tab_new  // (lua_State *L, int32_t asize, uint32_t hbits)
4764     |  // Table * returned in eax (RC).
4765     |  mov BASE, L:RB->base
4766     |  movzx RA, PC_RA
4767     |  mov [BASE+RA*8], TAB:RC
4768     |  mov dword [BASE+RA*8+4], LJ_TTAB
4769     |  ins_next
4770     |3:  // Turn 0x7ff into 0x801.
4771     |  mov RD, 0x801
4772     |  jmp <2
4773     |5:
4774     |  mov L:FCARG1, L:RB
4775     |  call extern lj_gc_step_fixtop@4  // (lua_State *L)
4776     |  movzx RD, PC_RD
4777     |  jmp <1
4778     break;
4779   case BC_TDUP:
4780     |  ins_AND  // RA = dst, RD = table const (~) (holding template table)
4781     |  mov L:RB, SAVE_L
4782     |  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4783     |  mov SAVE_PC, PC
4784     |  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4785     |  mov L:RB->base, BASE
4786     |  jae >3
4787     |2:
4788     |  mov TAB:FCARG2, [KBASE+RD*4]     // Caveat: FCARG2 == BASE
4789     |  mov L:FCARG1, L:RB               // Caveat: FCARG1 == RA
4790     |  call extern lj_tab_dup@8         // (lua_State *L, Table *kt)
4791     |  // Table * returned in eax (RC).
4792     |  mov BASE, L:RB->base
4793     |  movzx RA, PC_RA
4794     |  mov [BASE+RA*8], TAB:RC
4795     |  mov dword [BASE+RA*8+4], LJ_TTAB
4796     |  ins_next
4797     |3:
4798     |  mov L:FCARG1, L:RB
4799     |  call extern lj_gc_step_fixtop@4  // (lua_State *L)
4800     |  movzx RD, PC_RD                  // Need to reload RD.
4801     |  not RDa
4802     |  jmp <2
4803     break;
4805   case BC_GGET:
4806     |  ins_AND  // RA = dst, RD = str const (~)
4807     |  mov LFUNC:RB, [BASE-8]
4808     |  mov TAB:RB, LFUNC:RB->env
4809     |  mov STR:RC, [KBASE+RD*4]
4810     |  jmp ->BC_TGETS_Z
4811     break;
4812   case BC_GSET:
4813     |  ins_AND  // RA = src, RD = str const (~)
4814     |  mov LFUNC:RB, [BASE-8]
4815     |  mov TAB:RB, LFUNC:RB->env
4816     |  mov STR:RC, [KBASE+RD*4]
4817     |  jmp ->BC_TSETS_Z
4818     break;
4820   case BC_TGETV:
4821     |  ins_ABC  // RA = dst, RB = table, RC = key
4822     |  checktab RB, ->vmeta_tgetv
4823     |  mov TAB:RB, [BASE+RB*8]
4824     |
4825     |  // Integer key?
4826     |.if DUALNUM
4827     |  checkint RC, >5
4828     |  mov RC, dword [BASE+RC*8]
4829     |.else
4830     |  // Convert number to int and back and compare.
4831     |  checknum RC, >5
4832     |.if SSE
4833     |  movsd xmm0, qword [BASE+RC*8]
4834     |  cvtsd2si RC, xmm0
4835     |  cvtsi2sd xmm1, RC
4836     |  ucomisd xmm0, xmm1
4837     |.else
4838     |  fld qword [BASE+RC*8]
4839     |  fist ARG1
4840     |  fild ARG1
4841     |  fcomparepp
4842     |  mov RC, ARG1
4843     |.endif
4844     |  jne ->vmeta_tgetv                // Generic numeric key? Use fallback.
4845     |.endif
4846     |  cmp RC, TAB:RB->asize    // Takes care of unordered, too.
4847     |  jae ->vmeta_tgetv                // Not in array part? Use fallback.
4848     |  shl RC, 3
4849     |  add RC, TAB:RB->array
4850     |  cmp dword [RC+4], LJ_TNIL        // Avoid overwriting RB in fastpath.
4851     |  je >2
4852     |  // Get array slot.
4853     |.if X64
4854     |  mov RBa, [RC]
4855     |  mov [BASE+RA*8], RBa
4856     |.else
4857     |  mov RB, [RC]
4858     |  mov RC, [RC+4]
4859     |  mov [BASE+RA*8], RB
4860     |  mov [BASE+RA*8+4], RC
4861     |.endif
4862     |1:
4863     |  ins_next
4864     |
4865     |2:  // Check for __index if table value is nil.
4866     |  cmp dword TAB:RB->metatable, 0   // Shouldn't overwrite RA for fastpath.
4867     |  jz >3
4868     |  mov TAB:RA, TAB:RB->metatable
4869     |  test byte TAB:RA->nomm, 1<<MM_index
4870     |  jz ->vmeta_tgetv                 // 'no __index' flag NOT set: check.
4871     |  movzx RA, PC_RA                  // Restore RA.
4872     |3:
4873     |  mov dword [BASE+RA*8+4], LJ_TNIL
4874     |  jmp <1
4875     |
4876     |5:  // String key?
4877     |  checkstr RC, ->vmeta_tgetv
4878     |  mov STR:RC, [BASE+RC*8]
4879     |  jmp ->BC_TGETS_Z
4880     break;
4881   case BC_TGETS:
4882     |  ins_ABC  // RA = dst, RB = table, RC = str const (~)
4883     |  not RCa
4884     |  mov STR:RC, [KBASE+RC*4]
4885     |  checktab RB, ->vmeta_tgets
4886     |  mov TAB:RB, [BASE+RB*8]
4887     |->BC_TGETS_Z:      // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4888     |  mov RA, TAB:RB->hmask
4889     |  and RA, STR:RC->hash
4890     |  imul RA, #NODE
4891     |  add NODE:RA, TAB:RB->node
4892     |1:
4893     |  cmp dword NODE:RA->key.it, LJ_TSTR
4894     |  jne >4
4895     |  cmp dword NODE:RA->key.gcr, STR:RC
4896     |  jne >4
4897     |  // Ok, key found. Assumes: offsetof(Node, val) == 0
4898     |  cmp dword [RA+4], LJ_TNIL        // Avoid overwriting RB in fastpath.
4899     |  je >5                            // Key found, but nil value?
4900     |  movzx RC, PC_RA
4901     |  // Get node value.
4902     |.if X64
4903     |  mov RBa, [RA]
4904     |  mov [BASE+RC*8], RBa
4905     |.else
4906     |  mov RB, [RA]
4907     |  mov RA, [RA+4]
4908     |  mov [BASE+RC*8], RB
4909     |  mov [BASE+RC*8+4], RA
4910     |.endif
4911     |2:
4912     |  ins_next
4913     |
4914     |3:
4915     |  movzx RC, PC_RA
4916     |  mov dword [BASE+RC*8+4], LJ_TNIL
4917     |  jmp <2
4918     |
4919     |4:  // Follow hash chain.
4920     |  mov NODE:RA, NODE:RA->next
4921     |  test NODE:RA, NODE:RA
4922     |  jnz <1
4923     |  // End of hash chain: key not found, nil result.
4924     |
4925     |5:  // Check for __index if table value is nil.
4926     |  mov TAB:RA, TAB:RB->metatable
4927     |  test TAB:RA, TAB:RA
4928     |  jz <3                            // No metatable: done.
4929     |  test byte TAB:RA->nomm, 1<<MM_index
4930     |  jnz <3                           // 'no __index' flag set: done.
4931     |  jmp ->vmeta_tgets                // Caveat: preserve STR:RC.
4932     break;
4933   case BC_TGETB:
4934     |  ins_ABC  // RA = dst, RB = table, RC = byte literal
4935     |  checktab RB, ->vmeta_tgetb
4936     |  mov TAB:RB, [BASE+RB*8]
4937     |  cmp RC, TAB:RB->asize
4938     |  jae ->vmeta_tgetb
4939     |  shl RC, 3
4940     |  add RC, TAB:RB->array
4941     |  cmp dword [RC+4], LJ_TNIL        // Avoid overwriting RB in fastpath.
4942     |  je >2
4943     |  // Get array slot.
4944     |.if X64
4945     |  mov RBa, [RC]
4946     |  mov [BASE+RA*8], RBa
4947     |.else
4948     |  mov RB, [RC]
4949     |  mov RC, [RC+4]
4950     |  mov [BASE+RA*8], RB
4951     |  mov [BASE+RA*8+4], RC
4952     |.endif
4953     |1:
4954     |  ins_next
4955     |
4956     |2:  // Check for __index if table value is nil.
4957     |  cmp dword TAB:RB->metatable, 0   // Shouldn't overwrite RA for fastpath.
4958     |  jz >3
4959     |  mov TAB:RA, TAB:RB->metatable
4960     |  test byte TAB:RA->nomm, 1<<MM_index
4961     |  jz ->vmeta_tgetb                 // 'no __index' flag NOT set: check.
4962     |  movzx RA, PC_RA                  // Restore RA.
4963     |3:
4964     |  mov dword [BASE+RA*8+4], LJ_TNIL
4965     |  jmp <1
4966     break;
4968   case BC_TSETV:
4969     |  ins_ABC  // RA = src, RB = table, RC = key
4970     |  checktab RB, ->vmeta_tsetv
4971     |  mov TAB:RB, [BASE+RB*8]
4972     |
4973     |  // Integer key?
4974     |.if DUALNUM
4975     |  checkint RC, >5
4976     |  mov RC, dword [BASE+RC*8]
4977     |.else
4978     |  // Convert number to int and back and compare.
4979     |  checknum RC, >5
4980     |.if SSE
4981     |  movsd xmm0, qword [BASE+RC*8]
4982     |  cvtsd2si RC, xmm0
4983     |  cvtsi2sd xmm1, RC
4984     |  ucomisd xmm0, xmm1
4985     |.else
4986     |  fld qword [BASE+RC*8]
4987     |  fist ARG1
4988     |  fild ARG1
4989     |  fcomparepp
4990     |  mov RC, ARG1
4991     |.endif
4992     |  jne ->vmeta_tsetv                // Generic numeric key? Use fallback.
4993     |.endif
4994     |  cmp RC, TAB:RB->asize            // Takes care of unordered, too.
4995     |  jae ->vmeta_tsetv
4996     |  shl RC, 3
4997     |  add RC, TAB:RB->array
4998     |  cmp dword [RC+4], LJ_TNIL
4999     |  je >3                            // Previous value is nil?
5000     |1:
5001     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
5002     |  jnz >7
5003     |2:  // Set array slot.
5004     |.if X64
5005     |  mov RBa, [BASE+RA*8]
5006     |  mov [RC], RBa
5007     |.else
5008     |  mov RB, [BASE+RA*8+4]
5009     |  mov RA, [BASE+RA*8]
5010     |  mov [RC+4], RB
5011     |  mov [RC], RA
5012     |.endif
5013     |  ins_next
5014     |
5015     |3:  // Check for __newindex if previous value is nil.
5016     |  cmp dword TAB:RB->metatable, 0   // Shouldn't overwrite RA for fastpath.
5017     |  jz <1
5018     |  mov TAB:RA, TAB:RB->metatable
5019     |  test byte TAB:RA->nomm, 1<<MM_newindex
5020     |  jz ->vmeta_tsetv                 // 'no __newindex' flag NOT set: check.
5021     |  movzx RA, PC_RA                  // Restore RA.
5022     |  jmp <1
5023     |
5024     |5:  // String key?
5025     |  checkstr RC, ->vmeta_tsetv
5026     |  mov STR:RC, [BASE+RC*8]
5027     |  jmp ->BC_TSETS_Z
5028     |
5029     |7:  // Possible table write barrier for the value. Skip valiswhite check.
5030     |  barrierback TAB:RB, RA
5031     |  movzx RA, PC_RA                  // Restore RA.
5032     |  jmp <2
5033     break;
5034   case BC_TSETS:
5035     |  ins_ABC  // RA = src, RB = table, RC = str const (~)
5036     |  not RCa
5037     |  mov STR:RC, [KBASE+RC*4]
5038     |  checktab RB, ->vmeta_tsets
5039     |  mov TAB:RB, [BASE+RB*8]
5040     |->BC_TSETS_Z:      // RB = GCtab *, RC = GCstr *, refetches PC_RA.
5041     |  mov RA, TAB:RB->hmask
5042     |  and RA, STR:RC->hash
5043     |  imul RA, #NODE
5044     |  mov byte TAB:RB->nomm, 0         // Clear metamethod cache.
5045     |  add NODE:RA, TAB:RB->node
5046     |1:
5047     |  cmp dword NODE:RA->key.it, LJ_TSTR
5048     |  jne >5
5049     |  cmp dword NODE:RA->key.gcr, STR:RC
5050     |  jne >5
5051     |  // Ok, key found. Assumes: offsetof(Node, val) == 0
5052     |  cmp dword [RA+4], LJ_TNIL
5053     |  je >4                            // Previous value is nil?
5054     |2:
5055     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
5056     |  jnz >7
5057     |3:  // Set node value.
5058     |  movzx RC, PC_RA
5059     |.if X64
5060     |  mov RBa, [BASE+RC*8]
5061     |  mov [RA], RBa
5062     |.else
5063     |  mov RB, [BASE+RC*8+4]
5064     |  mov RC, [BASE+RC*8]
5065     |  mov [RA+4], RB
5066     |  mov [RA], RC
5067     |.endif
5068     |  ins_next
5069     |
5070     |4:  // Check for __newindex if previous value is nil.
5071     |  cmp dword TAB:RB->metatable, 0   // Shouldn't overwrite RA for fastpath.
5072     |  jz <2
5073     |  mov TMP1, RA                     // Save RA.
5074     |  mov TAB:RA, TAB:RB->metatable
5075     |  test byte TAB:RA->nomm, 1<<MM_newindex
5076     |  jz ->vmeta_tsets                 // 'no __newindex' flag NOT set: check.
5077     |  mov RA, TMP1                     // Restore RA.
5078     |  jmp <2
5079     |
5080     |5:  // Follow hash chain.
5081     |  mov NODE:RA, NODE:RA->next
5082     |  test NODE:RA, NODE:RA
5083     |  jnz <1
5084     |  // End of hash chain: key not found, add a new one.
5085     |
5086     |  // But check for __newindex first.
5087     |  mov TAB:RA, TAB:RB->metatable
5088     |  test TAB:RA, TAB:RA
5089     |  jz >6                            // No metatable: continue.
5090     |  test byte TAB:RA->nomm, 1<<MM_newindex
5091     |  jz ->vmeta_tsets                 // 'no __newindex' flag NOT set: check.
5092     |6:
5093     |  mov TMP1, STR:RC
5094     |  mov TMP2, LJ_TSTR
5095     |  mov TMP3, TAB:RB                 // Save TAB:RB for us.
5096     |.if X64
5097     |  mov L:CARG1d, SAVE_L
5098     |  mov L:CARG1d->base, BASE
5099     |  lea CARG3, TMP1
5100     |  mov CARG2d, TAB:RB
5101     |  mov L:RB, L:CARG1d
5102     |.else
5103     |  lea RC, TMP1                     // Store temp. TValue in TMP1/TMP2.
5104     |  mov ARG2, TAB:RB
5105     |  mov L:RB, SAVE_L
5106     |  mov ARG3, RC
5107     |  mov ARG1, L:RB
5108     |  mov L:RB->base, BASE
5109     |.endif
5110     |  mov SAVE_PC, PC
5111     |  call extern lj_tab_newkey        // (lua_State *L, GCtab *t, TValue *k)
5112     |  // Handles write barrier for the new key. TValue * returned in eax (RC).
5113     |  mov BASE, L:RB->base
5114     |  mov TAB:RB, TMP3                 // Need TAB:RB for barrier.
5115     |  mov RA, eax
5116     |  jmp <2                           // Must check write barrier for value.
5117     |
5118     |7:  // Possible table write barrier for the value. Skip valiswhite check.
5119     |  barrierback TAB:RB, RC           // Destroys STR:RC.
5120     |  jmp <3
5121     break;
5122   case BC_TSETB:
5123     |  ins_ABC  // RA = src, RB = table, RC = byte literal
5124     |  checktab RB, ->vmeta_tsetb
5125     |  mov TAB:RB, [BASE+RB*8]
5126     |  cmp RC, TAB:RB->asize
5127     |  jae ->vmeta_tsetb
5128     |  shl RC, 3
5129     |  add RC, TAB:RB->array
5130     |  cmp dword [RC+4], LJ_TNIL
5131     |  je >3                            // Previous value is nil?
5132     |1:
5133     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
5134     |  jnz >7
5135     |2:  // Set array slot.
5136     |.if X64
5137     |  mov RAa, [BASE+RA*8]
5138     |  mov [RC], RAa
5139     |.else
5140     |  mov RB, [BASE+RA*8+4]
5141     |  mov RA, [BASE+RA*8]
5142     |  mov [RC+4], RB
5143     |  mov [RC], RA
5144     |.endif
5145     |  ins_next
5146     |
5147     |3:  // Check for __newindex if previous value is nil.
5148     |  cmp dword TAB:RB->metatable, 0   // Shouldn't overwrite RA for fastpath.
5149     |  jz <1
5150     |  mov TAB:RA, TAB:RB->metatable
5151     |  test byte TAB:RA->nomm, 1<<MM_newindex
5152     |  jz ->vmeta_tsetb                 // 'no __newindex' flag NOT set: check.
5153     |  movzx RA, PC_RA                  // Restore RA.
5154     |  jmp <1
5155     |
5156     |7:  // Possible table write barrier for the value. Skip valiswhite check.
5157     |  barrierback TAB:RB, RA
5158     |  movzx RA, PC_RA                  // Restore RA.
5159     |  jmp <2
5160     break;
5162   case BC_TSETM:
5163     |  ins_AD   // RA = base (table at base-1), RD = num const (start index)
5164     |  mov TMP1, KBASE                  // Need one more free register.
5165     |  mov KBASE, dword [KBASE+RD*8]    // Integer constant is in lo-word.
5166     |1:
5167     |  lea RA, [BASE+RA*8]
5168     |  mov TAB:RB, [RA-8]               // Guaranteed to be a table.
5169     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
5170     |  jnz >7
5171     |2:
5172     |  mov RD, MULTRES
5173     |  sub RD, 1
5174     |  jz >4                            // Nothing to copy?
5175     |  add RD, KBASE                    // Compute needed size.
5176     |  cmp RD, TAB:RB->asize
5177     |  ja >5                            // Doesn't fit into array part?
5178     |  sub RD, KBASE
5179     |  shl KBASE, 3
5180     |  add KBASE, TAB:RB->array
5181     |3:  // Copy result slots to table.
5182     |.if X64
5183     |  mov RBa, [RA]
5184     |  add RA, 8
5185     |  mov [KBASE], RBa
5186     |.else
5187     |  mov RB, [RA]
5188     |  mov [KBASE], RB
5189     |  mov RB, [RA+4]
5190     |  add RA, 8
5191     |  mov [KBASE+4], RB
5192     |.endif
5193     |  add KBASE, 8
5194     |  sub RD, 1
5195     |  jnz <3
5196     |4:
5197     |  mov KBASE, TMP1
5198     |  ins_next
5199     |
5200     |5:  // Need to resize array part.
5201     |.if X64
5202     |  mov L:CARG1d, SAVE_L
5203     |  mov L:CARG1d->base, BASE         // Caveat: CARG2d/CARG3d may be BASE.
5204     |  mov CARG2d, TAB:RB
5205     |  mov CARG3d, RD
5206     |  mov L:RB, L:CARG1d
5207     |.else
5208     |  mov ARG2, TAB:RB
5209     |  mov L:RB, SAVE_L
5210     |  mov L:RB->base, BASE
5211     |  mov ARG3, RD
5212     |  mov ARG1, L:RB
5213     |.endif
5214     |  mov SAVE_PC, PC
5215     |  call extern lj_tab_reasize       // (lua_State *L, GCtab *t, int nasize)
5216     |  mov BASE, L:RB->base
5217     |  movzx RA, PC_RA                  // Restore RA.
5218     |  jmp <1                           // Retry.
5219     |
5220     |7:  // Possible table write barrier for any value. Skip valiswhite check.
5221     |  barrierback TAB:RB, RD
5222     |  jmp <2
5223     break;
5225   /* -- Calls and vararg handling ----------------------------------------- */
5227   case BC_CALL: case BC_CALLM:
5228     |  ins_A_C  // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
5229     if (op == BC_CALLM) {
5230       |  add NARGS:RD, MULTRES
5231     }
5232     |  cmp dword [BASE+RA*8+4], LJ_TFUNC
5233     |  mov LFUNC:RB, [BASE+RA*8]
5234     |  jne ->vmeta_call_ra
5235     |  lea BASE, [BASE+RA*8+8]
5236     |  ins_call
5237     break;
5239   case BC_CALLMT:
5240     |  ins_AD   // RA = base, RD = extra_nargs
5241     |  add NARGS:RD, MULTRES
5242     |  // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
5243     break;
5244   case BC_CALLT:
5245     |  ins_AD   // RA = base, RD = nargs+1
5246     |  lea RA, [BASE+RA*8+8]
5247     |  mov KBASE, BASE                  // Use KBASE for move + vmeta_call hint.
5248     |  mov LFUNC:RB, [RA-8]
5249     |  cmp dword [RA-4], LJ_TFUNC
5250     |  jne ->vmeta_call
5251     |->BC_CALLT_Z:
5252     |  mov PC, [BASE-4]
5253     |  test PC, FRAME_TYPE
5254     |  jnz >7
5255     |1:
5256     |  mov [BASE-8], LFUNC:RB           // Copy function down, reloaded below.
5257     |  mov MULTRES, NARGS:RD
5258     |  sub NARGS:RD, 1
5259     |  jz >3
5260     |2:  // Move args down.
5261     |.if X64
5262     |  mov RBa, [RA]
5263     |  add RA, 8
5264     |  mov [KBASE], RBa
5265     |.else
5266     |  mov RB, [RA]
5267     |  mov [KBASE], RB
5268     |  mov RB, [RA+4]
5269     |  add RA, 8
5270     |  mov [KBASE+4], RB
5271     |.endif
5272     |  add KBASE, 8
5273     |  sub NARGS:RD, 1
5274     |  jnz <2
5275     |
5276     |  mov LFUNC:RB, [BASE-8]
5277     |3:
5278     |  mov NARGS:RD, MULTRES
5279     |  cmp byte LFUNC:RB->ffid, 1       // (> FF_C) Calling a fast function?
5280     |  ja >5
5281     |4:
5282     |  ins_callt
5283     |
5284     |5:  // Tailcall to a fast function.
5285     |  test PC, FRAME_TYPE              // Lua frame below?
5286     |  jnz <4
5287     |  movzx RA, PC_RA
5288     |  not RAa
5289     |  lea RA, [BASE+RA*8]
5290     |  mov LFUNC:KBASE, [RA-8]          // Need to prepare KBASE.
5291     |  mov KBASE, LFUNC:KBASE->pc
5292     |  mov KBASE, [KBASE+PC2PROTO(k)]
5293     |  jmp <4
5294     |
5295     |7:  // Tailcall from a vararg function.
5296     |  sub PC, FRAME_VARG
5297     |  test PC, FRAME_TYPEP
5298     |  jnz >8                           // Vararg frame below?
5299     |  sub BASE, PC                     // Need to relocate BASE/KBASE down.
5300     |  mov KBASE, BASE
5301     |  mov PC, [BASE-4]
5302     |  jmp <1
5303     |8:
5304     |  add PC, FRAME_VARG
5305     |  jmp <1
5306     break;
5308   case BC_ITERC:
5309     |  ins_A    // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
5310     |  lea RA, [BASE+RA*8+8]            // fb = base+1
5311     |.if X64
5312     |  mov RBa, [RA-24]                 // Copy state. fb[0] = fb[-3].
5313     |  mov RCa, [RA-16]                 // Copy control var. fb[1] = fb[-2].
5314     |  mov [RA], RBa
5315     |  mov [RA+8], RCa
5316     |.else
5317     |  mov RB, [RA-24]                  // Copy state. fb[0] = fb[-3].
5318     |  mov RC, [RA-20]
5319     |  mov [RA], RB
5320     |  mov [RA+4], RC
5321     |  mov RB, [RA-16]                  // Copy control var. fb[1] = fb[-2].
5322     |  mov RC, [RA-12]
5323     |  mov [RA+8], RB
5324     |  mov [RA+12], RC
5325     |.endif
5326     |  mov LFUNC:RB, [RA-32]            // Copy callable. fb[-1] = fb[-4]
5327     |  mov RC, [RA-28]
5328     |  mov [RA-8], LFUNC:RB
5329     |  mov [RA-4], RC
5330     |  cmp RC, LJ_TFUNC                 // Handle like a regular 2-arg call.
5331     |  mov NARGS:RD, 2+1
5332     |  jne ->vmeta_call
5333     |  mov BASE, RA
5334     |  ins_call
5335     break;
5337   case BC_ITERN:
5338     |  ins_A    // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
5339     |.if JIT
5340     |  // NYI: add hotloop, record BC_ITERN.
5341     |.endif
5342     |  mov TMP1, KBASE                  // Need two more free registers.
5343     |  mov TMP2, DISPATCH
5344     |  mov TAB:RB, [BASE+RA*8-16]
5345     |  mov RC, [BASE+RA*8-8]            // Get index from control var.
5346     |  mov DISPATCH, TAB:RB->asize
5347     |  add PC, 4
5348     |  mov KBASE, TAB:RB->array
5349     |1:  // Traverse array part.
5350     |  cmp RC, DISPATCH; jae >5         // Index points after array part?
5351     |  cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
5352     |.if DUALNUM
5353     |  mov dword [BASE+RA*8+4], LJ_TISNUM
5354     |  mov dword [BASE+RA*8], RC
5355     |.elif SSE
5356     |  cvtsi2sd xmm0, RC
5357     |.else
5358     |  fild dword [BASE+RA*8-8]
5359     |.endif
5360     |  // Copy array slot to returned value.
5361     |.if X64
5362     |  mov RBa, [KBASE+RC*8]
5363     |  mov [BASE+RA*8+8], RBa
5364     |.else
5365     |  mov RB, [KBASE+RC*8+4]
5366     |  mov [BASE+RA*8+12], RB
5367     |  mov RB, [KBASE+RC*8]
5368     |  mov [BASE+RA*8+8], RB
5369     |.endif
5370     |  add RC, 1
5371     |  // Return array index as a numeric key.
5372     |.if DUALNUM
5373     |  // See above.
5374     |.elif SSE
5375     |  movsd qword [BASE+RA*8], xmm0
5376     |.else
5377     |  fstp qword [BASE+RA*8]
5378     |.endif
5379     |  mov [BASE+RA*8-8], RC            // Update control var.
5380     |2:
5381     |  movzx RD, PC_RD                  // Get target from ITERL.
5382     |  branchPC RD
5383     |3:
5384     |  mov DISPATCH, TMP2
5385     |  mov KBASE, TMP1
5386     |  ins_next
5387     |
5388     |4:  // Skip holes in array part.
5389     |  add RC, 1
5390     |.if not (DUALNUM or SSE)
5391     |  mov [BASE+RA*8-8], RC
5392     |.endif
5393     |  jmp <1
5394     |
5395     |5:  // Traverse hash part.
5396     |  sub RC, DISPATCH
5397     |6:
5398     |  cmp RC, TAB:RB->hmask; ja <3     // End of iteration? Branch to ITERL+1.
5399     |  imul KBASE, RC, #NODE
5400     |  add NODE:KBASE, TAB:RB->node
5401     |  cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
5402     |  lea DISPATCH, [RC+DISPATCH+1]
5403     |  // Copy key and value from hash slot.
5404     |.if X64
5405     |  mov RBa, NODE:KBASE->key
5406     |  mov RCa, NODE:KBASE->val
5407     |  mov [BASE+RA*8], RBa
5408     |  mov [BASE+RA*8+8], RCa
5409     |.else
5410     |  mov RB, NODE:KBASE->key.gcr
5411     |  mov RC, NODE:KBASE->key.it
5412     |  mov [BASE+RA*8], RB
5413     |  mov [BASE+RA*8+4], RC
5414     |  mov RB, NODE:KBASE->val.gcr
5415     |  mov RC, NODE:KBASE->val.it
5416     |  mov [BASE+RA*8+8], RB
5417     |  mov [BASE+RA*8+12], RC
5418     |.endif
5419     |  mov [BASE+RA*8-8], DISPATCH
5420     |  jmp <2
5421     |
5422     |7:  // Skip holes in hash part.
5423     |  add RC, 1
5424     |  jmp <6
5425     break;
5427   case BC_ISNEXT:
5428     |  ins_AD   // RA = base, RD = target (points to ITERN)
5429     |  cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
5430     |  mov CFUNC:RB, [BASE+RA*8-24]
5431     |  cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
5432     |  cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
5433     |  cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
5434     |  branchPC RD
5435     |  mov dword [BASE+RA*8-8], 0       // Initialize control var.
5436     |1:
5437     |  ins_next
5438     |5:  // Despecialize bytecode if any of the checks fail.
5439     |  mov PC_OP, BC_JMP
5440     |  branchPC RD
5441     |  mov byte [PC], BC_ITERC
5442     |  jmp <1
5443     break;
5445   case BC_VARG:
5446     |  ins_ABC  // RA = base, RB = nresults+1, RC = numparams
5447     |  mov TMP1, KBASE                  // Need one more free register.
5448     |  lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
5449     |  lea RA, [BASE+RA*8]
5450     |  sub KBASE, [BASE-4]
5451     |  // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
5452     |  test RB, RB
5453     |  jz >5                            // Copy all varargs?
5454     |  lea RB, [RA+RB*8-8]
5455     |  cmp KBASE, BASE                  // No vararg slots?
5456     |  jnb >2
5457     |1:  // Copy vararg slots to destination slots.
5458     |.if X64
5459     |  mov RCa, [KBASE-8]
5460     |  add KBASE, 8
5461     |  mov [RA], RCa
5462     |.else
5463     |  mov RC, [KBASE-8]
5464     |  mov [RA], RC
5465     |  mov RC, [KBASE-4]
5466     |  add KBASE, 8
5467     |  mov [RA+4], RC
5468     |.endif
5469     |  add RA, 8
5470     |  cmp RA, RB                       // All destination slots filled?
5471     |  jnb >3
5472     |  cmp KBASE, BASE                  // No more vararg slots?
5473     |  jb <1
5474     |2:  // Fill up remainder with nil.
5475     |  mov dword [RA+4], LJ_TNIL
5476     |  add RA, 8
5477     |  cmp RA, RB
5478     |  jb <2
5479     |3:
5480     |  mov KBASE, TMP1
5481     |  ins_next
5482     |
5483     |5:  // Copy all varargs.
5484     |  mov MULTRES, 1                   // MULTRES = 0+1
5485     |  mov RC, BASE
5486     |  sub RC, KBASE
5487     |  jbe <3                           // No vararg slots?
5488     |  mov RB, RC
5489     |  shr RB, 3
5490     |  add RB, 1
5491     |  mov MULTRES, RB                  // MULTRES = #varargs+1
5492     |  mov L:RB, SAVE_L
5493     |  add RC, RA
5494     |  cmp RC, L:RB->maxstack
5495     |  ja >7                            // Need to grow stack?
5496     |6:  // Copy all vararg slots.
5497     |.if X64
5498     |  mov RCa, [KBASE-8]
5499     |  add KBASE, 8
5500     |  mov [RA], RCa
5501     |.else
5502     |  mov RC, [KBASE-8]
5503     |  mov [RA], RC
5504     |  mov RC, [KBASE-4]
5505     |  add KBASE, 8
5506     |  mov [RA+4], RC
5507     |.endif
5508     |  add RA, 8
5509     |  cmp KBASE, BASE                  // No more vararg slots?
5510     |  jb <6
5511     |  jmp <3
5512     |
5513     |7:  // Grow stack for varargs.
5514     |  mov L:RB->base, BASE
5515     |  mov L:RB->top, RA
5516     |  mov SAVE_PC, PC
5517     |  sub KBASE, BASE                  // Need delta, because BASE may change.
5518     |  mov FCARG2, MULTRES
5519     |  sub FCARG2, 1
5520     |  mov FCARG1, L:RB
5521     |  call extern lj_state_growstack@8 // (lua_State *L, int n)
5522     |  mov BASE, L:RB->base
5523     |  mov RA, L:RB->top
5524     |  add KBASE, BASE
5525     |  jmp <6
5526     break;
5528   /* -- Returns ----------------------------------------------------------- */
5530   case BC_RETM:
5531     |  ins_AD   // RA = results, RD = extra_nresults
5532     |  add RD, MULTRES                  // MULTRES >=1, so RD >=1.
5533     |  // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
5534     break;
5536   case BC_RET: case BC_RET0: case BC_RET1:
5537     |  ins_AD   // RA = results, RD = nresults+1
5538     if (op != BC_RET0) {
5539       |  shl RA, 3
5540     }
5541     |1:
5542     |  mov PC, [BASE-4]
5543     |  mov MULTRES, RD                  // Save nresults+1.
5544     |  test PC, FRAME_TYPE              // Check frame type marker.
5545     |  jnz >7                           // Not returning to a fixarg Lua func?
5546     switch (op) {
5547     case BC_RET:
5548       |->BC_RET_Z:
5549       |  mov KBASE, BASE                // Use KBASE for result move.
5550       |  sub RD, 1
5551       |  jz >3
5552       |2:  // Move results down.
5553       |.if X64
5554       |  mov RBa, [KBASE+RA]
5555       |  mov [KBASE-8], RBa
5556       |.else
5557       |  mov RB, [KBASE+RA]
5558       |  mov [KBASE-8], RB
5559       |  mov RB, [KBASE+RA+4]
5560       |  mov [KBASE-4], RB
5561       |.endif
5562       |  add KBASE, 8
5563       |  sub RD, 1
5564       |  jnz <2
5565       |3:
5566       |  mov RD, MULTRES                // Note: MULTRES may be >255.
5567       |  movzx RB, PC_RB                // So cannot compare with RDL!
5568       |5:
5569       |  cmp RB, RD                     // More results expected?
5570       |  ja >6
5571       break;
5572     case BC_RET1:
5573       |.if X64
5574       |  mov RBa, [BASE+RA]
5575       |  mov [BASE-8], RBa
5576       |.else
5577       |  mov RB, [BASE+RA+4]
5578       |  mov [BASE-4], RB
5579       |  mov RB, [BASE+RA]
5580       |  mov [BASE-8], RB
5581       |.endif
5582       /* fallthrough */
5583     case BC_RET0:
5584       |5:
5585       |  cmp PC_RB, RDL                 // More results expected?
5586       |  ja >6
5587     default:
5588       break;
5589     }
5590     |  movzx RA, PC_RA
5591     |  not RAa                          // Note: ~RA = -(RA+1)
5592     |  lea BASE, [BASE+RA*8]            // base = base - (RA+1)*8
5593     |  mov LFUNC:KBASE, [BASE-8]
5594     |  mov KBASE, LFUNC:KBASE->pc
5595     |  mov KBASE, [KBASE+PC2PROTO(k)]
5596     |  ins_next
5597     |
5598     |6:  // Fill up results with nil.
5599     if (op == BC_RET) {
5600       |  mov dword [KBASE-4], LJ_TNIL   // Note: relies on shifted base.
5601       |  add KBASE, 8
5602     } else {
5603       |  mov dword [BASE+RD*8-12], LJ_TNIL
5604     }
5605     |  add RD, 1
5606     |  jmp <5
5607     |
5608     |7:  // Non-standard return case.
5609     |  lea RB, [PC-FRAME_VARG]
5610     |  test RB, FRAME_TYPEP
5611     |  jnz ->vm_return
5612     |  // Return from vararg function: relocate BASE down and RA up.
5613     |  sub BASE, RB
5614     if (op != BC_RET0) {
5615       |  add RA, RB
5616     }
5617     |  jmp <1
5618     break;
5620   /* -- Loops and branches ------------------------------------------------ */
5622   |.define FOR_IDX,  [RA];    .define FOR_TIDX,  dword [RA+4]
5623   |.define FOR_STOP, [RA+8];  .define FOR_TSTOP, dword [RA+12]
5624   |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
5625   |.define FOR_EXT,  [RA+24]; .define FOR_TEXT,  dword [RA+28]
5627   case BC_FORL:
5628     |.if JIT
5629     |  hotloop RB
5630     |.endif
5631     | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5632     break;
5634   case BC_JFORI:
5635   case BC_JFORL:
5636 #if !LJ_HASJIT
5637     break;
5638 #endif
5639   case BC_FORI:
5640   case BC_IFORL:
5641     vk = (op == BC_IFORL || op == BC_JFORL);
5642     |  ins_AJ   // RA = base, RD = target (after end of loop or start of loop)
5643     |  lea RA, [BASE+RA*8]
5644     if (LJ_DUALNUM) {
5645       |  cmp FOR_TIDX, LJ_TISNUM; jne >9
5646       if (!vk) {
5647         |  cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5648         |  cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5649         |  mov RB, dword FOR_IDX
5650         |  cmp dword FOR_STEP, 0; jl >5
5651       } else {
5652 #ifdef LUA_USE_ASSERT
5653         |  cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5654         |  cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5655 #endif
5656         |  mov RB, dword FOR_STEP
5657         |  test RB, RB; js >5
5658         |  add RB, dword FOR_IDX; jo >1
5659         |  mov dword FOR_IDX, RB
5660       }
5661       |  cmp RB, dword FOR_STOP
5662       |  mov FOR_TEXT, LJ_TISNUM
5663       |  mov dword FOR_EXT, RB
5664       if (op == BC_FORI) {
5665         |  jle >7
5666         |1:
5667         |6:
5668         |  branchPC RD
5669       } else if (op == BC_JFORI) {
5670         |  branchPC RD
5671         |  movzx RD, PC_RD
5672         |  jle =>BC_JLOOP
5673         |1:
5674         |6:
5675       } else if (op == BC_IFORL) {
5676         |  jg >7
5677         |6:
5678         |  branchPC RD
5679         |1:
5680       } else {
5681         |  jle =>BC_JLOOP
5682         |1:
5683         |6:
5684       }
5685       |7:
5686       |  ins_next
5687       |
5688       |5:  // Invert check for negative step.
5689       if (vk) {
5690         |  add RB, dword FOR_IDX; jo <1
5691         |  mov dword FOR_IDX, RB
5692       }
5693       |  cmp RB, dword FOR_STOP
5694       |  mov FOR_TEXT, LJ_TISNUM
5695       |  mov dword FOR_EXT, RB
5696       if (op == BC_FORI) {
5697         |  jge <7
5698       } else if (op == BC_JFORI) {
5699         |  branchPC RD
5700         |  movzx RD, PC_RD
5701         |  jge =>BC_JLOOP
5702       } else if (op == BC_IFORL) {
5703         |  jl <7
5704       } else {
5705         |  jge =>BC_JLOOP
5706       }
5707       |  jmp <6
5708       |9:  // Fallback to FP variant.
5709     } else if (!vk) {
5710       |  cmp FOR_TIDX, LJ_TISNUM
5711     }
5712     if (!vk) {
5713       |  jae ->vmeta_for
5714       |  cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5715     } else {
5716 #ifdef LUA_USE_ASSERT
5717       |  cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5718       |  cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5719 #endif
5720     }
5721     |  mov RB, FOR_TSTEP                // Load type/hiword of for step.
5722     if (!vk) {
5723       |  cmp RB, LJ_TISNUM; jae ->vmeta_for
5724     }
5725     |.if SSE
5726     |  movsd xmm0, qword FOR_IDX
5727     |  movsd xmm1, qword FOR_STOP
5728     if (vk) {
5729       |  addsd xmm0, qword FOR_STEP
5730       |  movsd qword FOR_IDX, xmm0
5731       |  test RB, RB; js >3
5732     } else {
5733       |  jl >3
5734     }
5735     |  ucomisd xmm1, xmm0
5736     |1:
5737     |  movsd qword FOR_EXT, xmm0
5738     |.else
5739     |  fld qword FOR_STOP
5740     |  fld qword FOR_IDX
5741     if (vk) {
5742       |  fadd qword FOR_STEP            // nidx = idx + step
5743       |  fst qword FOR_IDX
5744       |  fst qword FOR_EXT
5745       |  test RB, RB; js >1
5746     } else {
5747       |  fst qword FOR_EXT
5748       |  jl >1
5749     }
5750     |  fxch                             // Swap lim/(n)idx if step non-negative.
5751     |1:
5752     |  fcomparepp
5753     |.endif
5754     if (op == BC_FORI) {
5755       |.if DUALNUM
5756       |  jnb <7
5757       |.else
5758       |  jnb >2
5759       |  branchPC RD
5760       |.endif
5761     } else if (op == BC_JFORI) {
5762       |  branchPC RD
5763       |  movzx RD, PC_RD
5764       |  jnb =>BC_JLOOP
5765     } else if (op == BC_IFORL) {
5766       |.if DUALNUM
5767       |  jb <7
5768       |.else
5769       |  jb >2
5770       |  branchPC RD
5771       |.endif
5772     } else {
5773       |  jnb =>BC_JLOOP
5774     }
5775     |.if DUALNUM
5776     |  jmp <6
5777     |.else
5778     |2:
5779     |  ins_next
5780     |.endif
5781     |.if SSE
5782     |3:  // Invert comparison if step is negative.
5783     |  ucomisd xmm0, xmm1
5784     |  jmp <1
5785     |.endif
5786     break;
5788   case BC_ITERL:
5789     |.if JIT
5790     |  hotloop RB
5791     |.endif
5792     | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5793     break;
5795   case BC_JITERL:
5796 #if !LJ_HASJIT
5797     break;
5798 #endif
5799   case BC_IITERL:
5800     |  ins_AJ   // RA = base, RD = target
5801     |  lea RA, [BASE+RA*8]
5802     |  mov RB, [RA+4]
5803     |  cmp RB, LJ_TNIL; je >1           // Stop if iterator returned nil.
5804     if (op == BC_JITERL) {
5805       |  mov [RA-4], RB
5806       |  mov RB, [RA]
5807       |  mov [RA-8], RB
5808       |  jmp =>BC_JLOOP
5809     } else {
5810       |  branchPC RD                    // Otherwise save control var + branch.
5811       |  mov RD, [RA]
5812       |  mov [RA-4], RB
5813       |  mov [RA-8], RD
5814     }
5815     |1:
5816     |  ins_next
5817     break;
5819   case BC_LOOP:
5820     |  ins_A    // RA = base, RD = target (loop extent)
5821     |  // Note: RA/RD is only used by trace recorder to determine scope/extent
5822     |  // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5823   |.if JIT
5824     |  hotloop RB
5825     |.endif
5826     | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5827     break;
5829   case BC_ILOOP:
5830     |  ins_A    // RA = base, RD = target (loop extent)
5831     |  ins_next
5832     break;
5834   case BC_JLOOP:
5835     |.if JIT
5836     |  ins_AD   // RA = base (ignored), RD = traceno
5837     |  mov RA, [DISPATCH+DISPATCH_J(trace)]
5838     |  mov TRACE:RD, [RA+RD*4]
5839     |  mov RDa, TRACE:RD->mcode
5840     |  mov L:RB, SAVE_L
5841     |  mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5842     |  mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB
5843     |  // Save additional callee-save registers only used in compiled code.
5844     |.if X64WIN
5845     |  mov TMPQ, r12
5846     |  mov TMPa, r13
5847     |  mov CSAVE_4, r14
5848     |  mov CSAVE_3, r15
5849     |  mov RAa, rsp
5850     |  sub rsp, 9*16+4*8
5851     |  movdqa [RAa], xmm6
5852     |  movdqa [RAa-1*16], xmm7
5853     |  movdqa [RAa-2*16], xmm8
5854     |  movdqa [RAa-3*16], xmm9
5855     |  movdqa [RAa-4*16], xmm10
5856     |  movdqa [RAa-5*16], xmm11
5857     |  movdqa [RAa-6*16], xmm12
5858     |  movdqa [RAa-7*16], xmm13
5859     |  movdqa [RAa-8*16], xmm14
5860     |  movdqa [RAa-9*16], xmm15
5861     |.elif X64
5862     |  mov TMPQ, r12
5863     |  mov TMPa, r13
5864     |  sub rsp, 16
5865     |.endif
5866     |  jmp RDa
5867     |.endif
5868     break;
5870   case BC_JMP:
5871     |  ins_AJ   // RA = unused, RD = target
5872     |  branchPC RD
5873     |  ins_next
5874     break;
5876   /* -- Function headers -------------------------------------------------- */
5878    /*
5879    ** Reminder: A function may be called with func/args above L->maxstack,
5880    ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5881    ** too. This means all FUNC* ops (including fast functions) must check
5882    ** for stack overflow _before_ adding more slots!
5883    */
5885   case BC_FUNCF:
5886     |.if JIT
5887     |  hotcall RB
5888     |.endif
5889   case BC_FUNCV:  /* NYI: compiled vararg functions. */
5890     | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5891     break;
5893   case BC_JFUNCF:
5894 #if !LJ_HASJIT
5895     break;
5896 #endif
5897   case BC_IFUNCF:
5898     |  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1
5899     |  mov KBASE, [PC-4+PC2PROTO(k)]
5900     |  mov L:RB, SAVE_L
5901     |  lea RA, [BASE+RA*8]              // Top of frame.
5902     |  cmp RA, L:RB->maxstack
5903     |  ja ->vm_growstack_f
5904     |  movzx RA, byte [PC-4+PC2PROTO(numparams)]
5905     |  cmp NARGS:RD, RA                 // Check for missing parameters.
5906     |  jbe >3
5907     |2:
5908     if (op == BC_JFUNCF) {
5909       |  movzx RD, PC_RD
5910       |  jmp =>BC_JLOOP
5911     } else {
5912       |  ins_next
5913     }
5914     |
5915     |3:  // Clear missing parameters.
5916     |  mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5917     |  add NARGS:RD, 1
5918     |  cmp NARGS:RD, RA
5919     |  jbe <3
5920     |  jmp <2
5921     break;
5923   case BC_JFUNCV:
5924 #if !LJ_HASJIT
5925     break;
5926 #endif
5927     | int3  // NYI: compiled vararg functions
5928     break;  /* NYI: compiled vararg functions. */
5930   case BC_IFUNCV:
5931     |  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1
5932     |  lea RB, [NARGS:RD*8+FRAME_VARG]
5933     |  lea RD, [BASE+NARGS:RD*8]
5934     |  mov LFUNC:KBASE, [BASE-8]
5935     |  mov [RD-4], RB                   // Store delta + FRAME_VARG.
5936     |  mov [RD-8], LFUNC:KBASE          // Store copy of LFUNC.
5937     |  mov L:RB, SAVE_L
5938     |  lea RA, [RD+RA*8]
5939     |  cmp RA, L:RB->maxstack
5940     |  ja ->vm_growstack_v              // Need to grow stack.
5941     |  mov RA, BASE
5942     |  mov BASE, RD
5943     |  movzx RB, byte [PC-4+PC2PROTO(numparams)]
5944     |  test RB, RB
5945     |  jz >2
5946     |1:  // Copy fixarg slots up to new frame.
5947     |  add RA, 8
5948     |  cmp RA, BASE
5949     |  jnb >3                           // Less args than parameters?
5950     |  mov KBASE, [RA-8]
5951     |  mov [RD], KBASE
5952     |  mov KBASE, [RA-4]
5953     |  mov [RD+4], KBASE
5954     |  add RD, 8
5955     |  mov dword [RA-4], LJ_TNIL        // Clear old fixarg slot (help the GC).
5956     |  sub RB, 1
5957     |  jnz <1
5958     |2:
5959     if (op == BC_JFUNCV) {
5960       |  movzx RD, PC_RD
5961       |  jmp =>BC_JLOOP
5962     } else {
5963       |  mov KBASE, [PC-4+PC2PROTO(k)]
5964       |  ins_next
5965     }
5966     |
5967     |3:  // Clear missing parameters.
5968     |  mov dword [RD+4], LJ_TNIL
5969     |  add RD, 8
5970     |  sub RB, 1
5971     |  jnz <3
5972     |  jmp <2
5973     break;
5975   case BC_FUNCC:
5976   case BC_FUNCCW:
5977     |  ins_AD  // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
5978     |  mov CFUNC:RB, [BASE-8]
5979     |  mov KBASEa, CFUNC:RB->f
5980     |  mov L:RB, SAVE_L
5981     |  lea RD, [BASE+NARGS:RD*8-8]
5982     |  mov L:RB->base, BASE
5983     |  lea RA, [RD+8*LUA_MINSTACK]
5984     |  cmp RA, L:RB->maxstack
5985     |  mov L:RB->top, RD
5986     if (op == BC_FUNCC) {
5987       |.if X64
5988       |  mov CARG1d, L:RB                       // Caveat: CARG1d may be RA.
5989       |.else
5990       |  mov ARG1, L:RB
5991       |.endif
5992     } else {
5993       |.if X64
5994       |  mov CARG2, KBASEa
5995       |  mov CARG1d, L:RB                       // Caveat: CARG1d may be RA.
5996       |.else
5997       |  mov ARG2, KBASEa
5998       |  mov ARG1, L:RB
5999       |.endif
6000     }
6001     |  ja ->vm_growstack_c              // Need to grow stack.
6002     |  set_vmstate C
6003     if (op == BC_FUNCC) {
6004       |  call KBASEa                    // (lua_State *L)
6005     } else {
6006       |  // (lua_State *L, lua_CFunction f)
6007       |  call aword [DISPATCH+DISPATCH_GL(wrapf)]
6008     }
6009     |  set_vmstate INTERP
6010     |  // nresults returned in eax (RD).
6011     |  mov BASE, L:RB->base
6012     |  lea RA, [BASE+RD*8]
6013     |  neg RA
6014     |  add RA, L:RB->top                // RA = (L->top-(L->base+nresults))*8
6015     |  mov PC, [BASE-4]                 // Fetch PC of caller.
6016     |  jmp ->vm_returnc
6017     break;
6019   /* ---------------------------------------------------------------------- */
6021   default:
6022     fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
6023     exit(2);
6024     break;
6025   }
6028 static int build_backend(BuildCtx *ctx)
6030   int op;
6031   dasm_growpc(Dst, BC__MAX);
6032   build_subroutines(ctx);
6033   |.code_op
6034   for (op = 0; op < BC__MAX; op++)
6035     build_ins(ctx, (BCOp)op, op);
6036   return BC__MAX;
6039 /* Emit pseudo frame-info for all assembler functions. */
6040 static void emit_asm_debug(BuildCtx *ctx)
6042   int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
6043 #if LJ_64
6044 #define SZPTR   "8"
6045 #define BSZPTR  "3"
6046 #define REG_SP  "0x7"
6047 #define REG_RA  "0x10"
6048 #else
6049 #define SZPTR   "4"
6050 #define BSZPTR  "2"
6051 #define REG_SP  "0x4"
6052 #define REG_RA  "0x8"
6053 #endif
6054   switch (ctx->mode) {
6055   case BUILD_elfasm:
6056     fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
6057     fprintf(ctx->fp,
6058         ".Lframe0:\n"
6059         "\t.long .LECIE0-.LSCIE0\n"
6060         ".LSCIE0:\n"
6061         "\t.long 0xffffffff\n"
6062         "\t.byte 0x1\n"
6063         "\t.string \"\"\n"
6064         "\t.uleb128 0x1\n"
6065         "\t.sleb128 -" SZPTR "\n"
6066         "\t.byte " REG_RA "\n"
6067         "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6068         "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6069         "\t.align " SZPTR "\n"
6070         ".LECIE0:\n\n");
6071     fprintf(ctx->fp,
6072         ".LSFDE0:\n"
6073         "\t.long .LEFDE0-.LASFDE0\n"
6074         ".LASFDE0:\n"
6075         "\t.long .Lframe0\n"
6076 #if LJ_64
6077         "\t.quad .Lbegin\n"
6078         "\t.quad %d\n"
6079         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
6080         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
6081         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
6082         "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */
6083         "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */
6084 #else
6085         "\t.long .Lbegin\n"
6086         "\t.long %d\n"
6087         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
6088         "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */
6089         "\t.byte 0x87\n\t.uleb128 0x3\n"        /* offset edi */
6090         "\t.byte 0x86\n\t.uleb128 0x4\n"        /* offset esi */
6091         "\t.byte 0x83\n\t.uleb128 0x5\n"        /* offset ebx */
6092 #endif
6093         "\t.align " SZPTR "\n"
6094         ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
6095 #if LJ_HASFFI
6096     fprintf(ctx->fp,
6097         ".LSFDE1:\n"
6098         "\t.long .LEFDE1-.LASFDE1\n"
6099         ".LASFDE1:\n"
6100         "\t.long .Lframe0\n"
6101 #if LJ_64
6102         "\t.quad lj_vm_ffi_call\n"
6103         "\t.quad %d\n"
6104         "\t.byte 0xe\n\t.uleb128 16\n"          /* def_cfa_offset */
6105         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
6106         "\t.byte 0xd\n\t.uleb128 0x6\n"         /* def_cfa_register rbp */
6107         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
6108 #else
6109         "\t.long lj_vm_ffi_call\n"
6110         "\t.long %d\n"
6111         "\t.byte 0xe\n\t.uleb128 8\n"           /* def_cfa_offset */
6112         "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */
6113         "\t.byte 0xd\n\t.uleb128 0x5\n"         /* def_cfa_register ebp */
6114         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset ebx */
6115 #endif
6116         "\t.align " SZPTR "\n"
6117         ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
6118 #endif
6119 #if (defined(__sun__) && defined(__svr4__)) || defined(__solaris_)
6120     fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
6121 #else
6122     fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
6123 #endif
6124     fprintf(ctx->fp,
6125         ".Lframe1:\n"
6126         "\t.long .LECIE1-.LSCIE1\n"
6127         ".LSCIE1:\n"
6128         "\t.long 0\n"
6129         "\t.byte 0x1\n"
6130         "\t.string \"zPR\"\n"
6131         "\t.uleb128 0x1\n"
6132         "\t.sleb128 -" SZPTR "\n"
6133         "\t.byte " REG_RA "\n"
6134         "\t.uleb128 6\n"                        /* augmentation length */
6135         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
6136         "\t.long lj_err_unwind_dwarf-.\n"
6137         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
6138         "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6139         "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6140         "\t.align " SZPTR "\n"
6141         ".LECIE1:\n\n");
6142     fprintf(ctx->fp,
6143         ".LSFDE2:\n"
6144         "\t.long .LEFDE2-.LASFDE2\n"
6145         ".LASFDE2:\n"
6146         "\t.long .LASFDE2-.Lframe1\n"
6147         "\t.long .Lbegin-.\n"
6148         "\t.long %d\n"
6149         "\t.uleb128 0\n"                        /* augmentation length */
6150         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
6151 #if LJ_64
6152         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
6153         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
6154         "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */
6155         "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */
6156 #else
6157         "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */
6158         "\t.byte 0x87\n\t.uleb128 0x3\n"        /* offset edi */
6159         "\t.byte 0x86\n\t.uleb128 0x4\n"        /* offset esi */
6160         "\t.byte 0x83\n\t.uleb128 0x5\n"        /* offset ebx */
6161 #endif
6162         "\t.align " SZPTR "\n"
6163         ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
6164 #if LJ_HASFFI
6165     fprintf(ctx->fp,
6166         ".Lframe2:\n"
6167         "\t.long .LECIE2-.LSCIE2\n"
6168         ".LSCIE2:\n"
6169         "\t.long 0\n"
6170         "\t.byte 0x1\n"
6171         "\t.string \"zR\"\n"
6172         "\t.uleb128 0x1\n"
6173         "\t.sleb128 -" SZPTR "\n"
6174         "\t.byte " REG_RA "\n"
6175         "\t.uleb128 1\n"                        /* augmentation length */
6176         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
6177         "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6178         "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6179         "\t.align " SZPTR "\n"
6180         ".LECIE2:\n\n");
6181     fprintf(ctx->fp,
6182         ".LSFDE3:\n"
6183         "\t.long .LEFDE3-.LASFDE3\n"
6184         ".LASFDE3:\n"
6185         "\t.long .LASFDE3-.Lframe2\n"
6186         "\t.long lj_vm_ffi_call-.\n"
6187         "\t.long %d\n"
6188         "\t.uleb128 0\n"                        /* augmentation length */
6189 #if LJ_64
6190         "\t.byte 0xe\n\t.uleb128 16\n"          /* def_cfa_offset */
6191         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
6192         "\t.byte 0xd\n\t.uleb128 0x6\n"         /* def_cfa_register rbp */
6193         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
6194 #else
6195         "\t.byte 0xe\n\t.uleb128 8\n"           /* def_cfa_offset */
6196         "\t.byte 0x85\n\t.uleb128 0x2\n"        /* offset ebp */
6197         "\t.byte 0xd\n\t.uleb128 0x5\n"         /* def_cfa_register ebp */
6198         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset ebx */
6199 #endif
6200         "\t.align " SZPTR "\n"
6201         ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
6202 #endif
6203     break;
6204   /* Mental note: never let Apple design an assembler.
6205   ** Or a linker. Or a plastic case. But I digress.
6206   */
6207   case BUILD_machasm: {
6208 #if LJ_HASFFI
6209     int fcsize = 0;
6210 #endif
6211     int i;
6212     fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
6213     fprintf(ctx->fp,
6214         "EH_frame1:\n"
6215         "\t.set L$set$x,LECIEX-LSCIEX\n"
6216         "\t.long L$set$x\n"
6217         "LSCIEX:\n"
6218         "\t.long 0\n"
6219         "\t.byte 0x1\n"
6220         "\t.ascii \"zPR\\0\"\n"
6221         "\t.byte 0x1\n"
6222         "\t.byte 128-" SZPTR "\n"
6223         "\t.byte " REG_RA "\n"
6224         "\t.byte 6\n"                           /* augmentation length */
6225         "\t.byte 0x9b\n"                        /* indirect|pcrel|sdata4 */
6226 #if LJ_64
6227         "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
6228         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
6229         "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6230 #else
6231         "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
6232         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
6233         "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n"  /* esp=5 on 32 bit MACH-O. */
6234 #endif
6235         "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6236         "\t.align " BSZPTR "\n"
6237         "LECIEX:\n\n");
6238     for (i = 0; i < ctx->nsym; i++) {
6239       const char *name = ctx->sym[i].name;
6240       int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
6241       if (size == 0) continue;
6242 #if LJ_HASFFI
6243       if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
6244 #endif
6245       fprintf(ctx->fp,
6246           "%s.eh:\n"
6247           "LSFDE%d:\n"
6248           "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
6249           "\t.long L$set$%d\n"
6250           "LASFDE%d:\n"
6251           "\t.long LASFDE%d-EH_frame1\n"
6252           "\t.long %s-.\n"
6253           "\t.long %d\n"
6254           "\t.byte 0\n"                         /* augmentation length */
6255           "\t.byte 0xe\n\t.byte %d\n"           /* def_cfa_offset */
6256 #if LJ_64
6257           "\t.byte 0x86\n\t.byte 0x2\n"         /* offset rbp */
6258           "\t.byte 0x83\n\t.byte 0x3\n"         /* offset rbx */
6259           "\t.byte 0x8f\n\t.byte 0x4\n"         /* offset r15 */
6260           "\t.byte 0x8e\n\t.byte 0x5\n"         /* offset r14 */
6261 #else
6262           "\t.byte 0x84\n\t.byte 0x2\n"         /* offset ebp (4 for MACH-O)*/
6263           "\t.byte 0x87\n\t.byte 0x3\n"         /* offset edi */
6264           "\t.byte 0x86\n\t.byte 0x4\n"         /* offset esi */
6265           "\t.byte 0x83\n\t.byte 0x5\n"         /* offset ebx */
6266 #endif
6267           "\t.align " BSZPTR "\n"
6268           "LEFDE%d:\n\n",
6269           name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
6270     }
6271 #if LJ_HASFFI
6272     if (fcsize) {
6273       fprintf(ctx->fp,
6274           "EH_frame2:\n"
6275           "\t.set L$set$y,LECIEY-LSCIEY\n"
6276           "\t.long L$set$y\n"
6277           "LSCIEY:\n"
6278           "\t.long 0\n"
6279           "\t.byte 0x1\n"
6280           "\t.ascii \"zR\\0\"\n"
6281           "\t.byte 0x1\n"
6282           "\t.byte 128-" SZPTR "\n"
6283           "\t.byte " REG_RA "\n"
6284           "\t.byte 1\n"                         /* augmentation length */
6285 #if LJ_64
6286           "\t.byte 0x1b\n"                      /* pcrel|sdata4 */
6287           "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6288 #else
6289           "\t.byte 0x1b\n"                      /* pcrel|sdata4 */
6290           "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n"  /* esp=5 on 32 bit MACH. */
6291 #endif
6292           "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6293           "\t.align " BSZPTR "\n"
6294           "LECIEY:\n\n");
6295       fprintf(ctx->fp,
6296           "_lj_vm_ffi_call.eh:\n"
6297           "LSFDEY:\n"
6298           "\t.set L$set$yy,LEFDEY-LASFDEY\n"
6299           "\t.long L$set$yy\n"
6300           "LASFDEY:\n"
6301           "\t.long LASFDEY-EH_frame2\n"
6302           "\t.long _lj_vm_ffi_call-.\n"
6303           "\t.long %d\n"
6304           "\t.byte 0\n"                         /* augmentation length */
6305 #if LJ_64
6306           "\t.byte 0xe\n\t.byte 16\n"           /* def_cfa_offset */
6307           "\t.byte 0x86\n\t.byte 0x2\n"         /* offset rbp */
6308           "\t.byte 0xd\n\t.uleb128 0x6\n"       /* def_cfa_register rbp */
6309           "\t.byte 0x83\n\t.byte 0x3\n"         /* offset rbx */
6310 #else
6311           "\t.byte 0xe\n\t.byte 8\n"            /* def_cfa_offset */
6312           "\t.byte 0x84\n\t.byte 0x2\n"         /* offset ebp (4 for MACH-O)*/
6313           "\t.byte 0xd\n\t.uleb128 0x4\n"       /* def_cfa_register ebp */
6314           "\t.byte 0x83\n\t.byte 0x3\n"         /* offset ebx */
6315 #endif
6316           "\t.align " BSZPTR "\n"
6317           "LEFDEY:\n\n", fcsize);
6318     }
6319 #endif
6320 #if LJ_64
6321     fprintf(ctx->fp, "\t.subsections_via_symbols\n");
6322 #else
6323     fprintf(ctx->fp,
6324       "\t.non_lazy_symbol_pointer\n"
6325       "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
6326       ".indirect_symbol _lj_err_unwind_dwarf\n"
6327       ".long 0\n");
6328 #endif
6329     }
6330     break;
6331   default:  /* Difficult for other modes. */
6332     break;
6333   }