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