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