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