fix getsup (HH)
[luatex.git] / source / libs / luajit / LuaJIT-src / src / vm_x64.dasc
bloba6b6853e1e948dc56242cc739591aa7128118d74
1 |// Low-level VM code for x64 CPUs in LJ_GC64 mode.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
5 |.arch x64
6 |.section code_op, code_sub
8 |.actionlist build_actionlist
9 |.globals GLOB_
10 |.globalnames globnames
11 |.externnames extnames
13 |//-----------------------------------------------------------------------
15 |.if WIN
16 |.define X64WIN, 1                      // Windows/x64 calling conventions.
17 |.endif
19 |// Fixed register assignments for the interpreter.
20 |// This is very fragile and has many dependencies. Caveat emptor.
21 |.define BASE,          rdx             // Not C callee-save, refetched anyway.
22 |.if X64WIN
23 |.define KBASE,         rdi             // Must be C callee-save.
24 |.define PC,            rsi             // Must be C callee-save.
25 |.define DISPATCH,      rbx             // Must be C callee-save.
26 |.define KBASEd,        edi
27 |.define PCd,           esi
28 |.define DISPATCHd,     ebx
29 |.else
30 |.define KBASE,         r15             // Must be C callee-save.
31 |.define PC,            rbx             // Must be C callee-save.
32 |.define DISPATCH,      r14             // Must be C callee-save.
33 |.define KBASEd,        r15d
34 |.define PCd,           ebx
35 |.define DISPATCHd,     r14d
36 |.endif
38 |.define RA,            rcx
39 |.define RAd,           ecx
40 |.define RAH,           ch
41 |.define RAL,           cl
42 |.define RB,            rbp             // Must be rbp (C callee-save).
43 |.define RBd,           ebp
44 |.define RC,            rax             // Must be rax.
45 |.define RCd,           eax
46 |.define RCW,           ax
47 |.define RCH,           ah
48 |.define RCL,           al
49 |.define OP,            RBd
50 |.define RD,            RC
51 |.define RDd,           RCd
52 |.define RDW,           RCW
53 |.define RDL,           RCL
54 |.define TMPR,          r10
55 |.define TMPRd,         r10d
56 |.define ITYPE,         r11
57 |.define ITYPEd,        r11d
59 |.if X64WIN
60 |.define CARG1,         rcx             // x64/WIN64 C call arguments.
61 |.define CARG2,         rdx
62 |.define CARG3,         r8
63 |.define CARG4,         r9
64 |.define CARG1d,        ecx
65 |.define CARG2d,        edx
66 |.define CARG3d,        r8d
67 |.define CARG4d,        r9d
68 |.else
69 |.define CARG1,         rdi             // x64/POSIX C call arguments.
70 |.define CARG2,         rsi
71 |.define CARG3,         rdx
72 |.define CARG4,         rcx
73 |.define CARG5,         r8
74 |.define CARG6,         r9
75 |.define CARG1d,        edi
76 |.define CARG2d,        esi
77 |.define CARG3d,        edx
78 |.define CARG4d,        ecx
79 |.define CARG5d,        r8d
80 |.define CARG6d,        r9d
81 |.endif
83 |// Type definitions. Some of these are only used for documentation.
84 |.type L,               lua_State
85 |.type GL,              global_State
86 |.type TVALUE,          TValue
87 |.type GCOBJ,           GCobj
88 |.type STR,             GCstr
89 |.type TAB,             GCtab
90 |.type LFUNC,           GCfuncL
91 |.type CFUNC,           GCfuncC
92 |.type PROTO,           GCproto
93 |.type UPVAL,           GCupval
94 |.type NODE,            Node
95 |.type NARGS,           int
96 |.type TRACE,           GCtrace
97 |.type SBUF,            SBuf
99 |// Stack layout while in interpreter. Must match with lj_frame.h.
100 |//-----------------------------------------------------------------------
101 |.if X64WIN             // x64/Windows stack layout
103 |.define CFRAME_SPACE,  aword*5                 // Delta for rsp (see <--).
104 |.macro saveregs_
105 |  push rdi; push rsi; push rbx
106 |  sub rsp, CFRAME_SPACE
107 |.endmacro
108 |.macro saveregs
109 |  push rbp; saveregs_
110 |.endmacro
111 |.macro restoreregs
112 |  add rsp, CFRAME_SPACE
113 |  pop rbx; pop rsi; pop rdi; pop rbp
114 |.endmacro
116 |.define SAVE_CFRAME,   aword [rsp+aword*13]
117 |.define SAVE_PC,       aword [rsp+aword*12]
118 |.define SAVE_L,        aword [rsp+aword*11]
119 |.define SAVE_ERRF,     dword [rsp+dword*21]
120 |.define SAVE_NRES,     dword [rsp+dword*20]
121 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
122 |.define SAVE_RET,      aword [rsp+aword*9]     //<-- rsp entering interpreter.
123 |.define SAVE_R4,       aword [rsp+aword*8]
124 |.define SAVE_R3,       aword [rsp+aword*7]
125 |.define SAVE_R2,       aword [rsp+aword*6]
126 |.define SAVE_R1,       aword [rsp+aword*5]     //<-- rsp after register saves.
127 |.define ARG5,          aword [rsp+aword*4]
128 |.define CSAVE_4,       aword [rsp+aword*3]
129 |.define CSAVE_3,       aword [rsp+aword*2]
130 |.define CSAVE_2,       aword [rsp+aword*1]
131 |.define CSAVE_1,       aword [rsp]             //<-- rsp while in interpreter.
132 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
134 |.define ARG5d,         dword [rsp+dword*8]
135 |.define TMP1,          ARG5                    // TMP1 overlaps ARG5
136 |.define TMP1d,         ARG5d
137 |.define TMP1hi,        dword [rsp+dword*9]
138 |.define MULTRES,       TMP1d                   // MULTRES overlaps TMP1d.
140 |//-----------------------------------------------------------------------
141 |.else                  // x64/POSIX stack layout
143 |.define CFRAME_SPACE,  aword*5                 // Delta for rsp (see <--).
144 |.macro saveregs_
145 |  push rbx; push r15; push r14
146 |.if NO_UNWIND
147 |  push r13; push r12
148 |.endif
149 |  sub rsp, CFRAME_SPACE
150 |.endmacro
151 |.macro saveregs
152 |  push rbp; saveregs_
153 |.endmacro
154 |.macro restoreregs
155 |  add rsp, CFRAME_SPACE
156 |.if NO_UNWIND
157 |  pop r12; pop r13
158 |.endif
159 |  pop r14; pop r15; pop rbx; pop rbp
160 |.endmacro
162 |//----- 16 byte aligned,
163 |.if NO_UNWIND
164 |.define SAVE_RET,      aword [rsp+aword*11]    //<-- rsp entering interpreter.
165 |.define SAVE_R4,       aword [rsp+aword*10]
166 |.define SAVE_R3,       aword [rsp+aword*9]
167 |.define SAVE_R2,       aword [rsp+aword*8]
168 |.define SAVE_R1,       aword [rsp+aword*7]
169 |.define SAVE_RU2,      aword [rsp+aword*6]
170 |.define SAVE_RU1,      aword [rsp+aword*5]     //<-- rsp after register saves.
171 |.else
172 |.define SAVE_RET,      aword [rsp+aword*9]     //<-- rsp entering interpreter.
173 |.define SAVE_R4,       aword [rsp+aword*8]
174 |.define SAVE_R3,       aword [rsp+aword*7]
175 |.define SAVE_R2,       aword [rsp+aword*6]
176 |.define SAVE_R1,       aword [rsp+aword*5]     //<-- rsp after register saves.
177 |.endif
178 |.define SAVE_CFRAME,   aword [rsp+aword*4]
179 |.define SAVE_PC,       aword [rsp+aword*3]
180 |.define SAVE_L,        aword [rsp+aword*2]
181 |.define SAVE_ERRF,     dword [rsp+dword*3]
182 |.define SAVE_NRES,     dword [rsp+dword*2]
183 |.define TMP1,          aword [rsp]             //<-- rsp while in interpreter.
184 |//----- 16 byte aligned
186 |.define TMP1d,         dword [rsp]
187 |.define TMP1hi,        dword [rsp+dword*1]
188 |.define MULTRES,       TMP1d                   // MULTRES overlaps TMP1d.
190 |.endif
192 |//-----------------------------------------------------------------------
194 |// Instruction headers.
195 |.macro ins_A; .endmacro
196 |.macro ins_AD; .endmacro
197 |.macro ins_AJ; .endmacro
198 |.macro ins_ABC; movzx RBd, RCH; movzx RCd, RCL; .endmacro
199 |.macro ins_AB_; movzx RBd, RCH; .endmacro
200 |.macro ins_A_C; movzx RCd, RCL; .endmacro
201 |.macro ins_AND; not RD; .endmacro
203 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
204 |.macro ins_NEXT
205 |  mov RCd, [PC]
206 |  movzx RAd, RCH
207 |  movzx OP, RCL
208 |  add PC, 4
209 |  shr RCd, 16
210 |  jmp aword [DISPATCH+OP*8]
211 |.endmacro
213 |// Instruction footer.
214 |.if 1
215 |  // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
216 |  .define ins_next, ins_NEXT
217 |  .define ins_next_, ins_NEXT
218 |.else
219 |  // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
220 |  // Affects only certain kinds of benchmarks (and only with -j off).
221 |  // Around 10%-30% slower on Core2, a lot more slower on P4.
222 |  .macro ins_next
223 |    jmp ->ins_next
224 |  .endmacro
225 |  .macro ins_next_
226 |  ->ins_next:
227 |    ins_NEXT
228 |  .endmacro
229 |.endif
231 |// Call decode and dispatch.
232 |.macro ins_callt
233 |  // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-8] = PC
234 |  mov PC, LFUNC:RB->pc
235 |  mov RAd, [PC]
236 |  movzx OP, RAL
237 |  movzx RAd, RAH
238 |  add PC, 4
239 |  jmp aword [DISPATCH+OP*8]
240 |.endmacro
242 |.macro ins_call
243 |  // BASE = new base, RB = LFUNC, RD = nargs+1
244 |  mov [BASE-8], PC
245 |  ins_callt
246 |.endmacro
248 |//-----------------------------------------------------------------------
250 |// Macros to clear or set tags.
251 |.macro cleartp, reg; shl reg, 17; shr reg, 17; .endmacro
252 |.macro settp, reg, tp
253 |  mov64 ITYPE, ((int64_t)tp<<47)
254 |  or reg, ITYPE
255 |.endmacro
256 |.macro settp, dst, reg, tp
257 |  mov64 dst, ((int64_t)tp<<47)
258 |  or dst, reg
259 |.endmacro
260 |.macro setint, reg
261 |  settp reg, LJ_TISNUM
262 |.endmacro
263 |.macro setint, dst, reg
264 |  settp dst, reg, LJ_TISNUM
265 |.endmacro
267 |// Macros to test operand types.
268 |.macro checktp_nc, reg, tp, target
269 |  mov ITYPE, reg
270 |  sar ITYPE, 47
271 |  cmp ITYPEd, tp
272 |  jne target
273 |.endmacro
274 |.macro checktp, reg, tp, target
275 |  mov ITYPE, reg
276 |  cleartp reg
277 |  sar ITYPE, 47
278 |  cmp ITYPEd, tp
279 |  jne target
280 |.endmacro
281 |.macro checktptp, src, tp, target
282 |  mov ITYPE, src
283 |  sar ITYPE, 47
284 |  cmp ITYPEd, tp
285 |  jne target
286 |.endmacro
287 |.macro checkstr, reg, target; checktp reg, LJ_TSTR, target; .endmacro
288 |.macro checktab, reg, target; checktp reg, LJ_TTAB, target; .endmacro
289 |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC, target; .endmacro
291 |.macro checknumx, reg, target, jump
292 |  mov ITYPE, reg
293 |  sar ITYPE, 47
294 |  cmp ITYPEd, LJ_TISNUM
295 |  jump target
296 |.endmacro
297 |.macro checkint, reg, target; checknumx reg, target, jne; .endmacro
298 |.macro checkinttp, src, target; checknumx src, target, jne; .endmacro
299 |.macro checknum, reg, target; checknumx reg, target, jae; .endmacro
300 |.macro checknumtp, src, target; checknumx src, target, jae; .endmacro
301 |.macro checknumber, src, target; checknumx src, target, ja; .endmacro
303 |.macro mov_false, reg; mov64 reg, (int64_t)~((uint64_t)1<<47); .endmacro
304 |.macro mov_true, reg; mov64 reg, (int64_t)~((uint64_t)2<<47); .endmacro
306 |// These operands must be used with movzx.
307 |.define PC_OP, byte [PC-4]
308 |.define PC_RA, byte [PC-3]
309 |.define PC_RB, byte [PC-1]
310 |.define PC_RC, byte [PC-2]
311 |.define PC_RD, word [PC-2]
313 |.macro branchPC, reg
314 |  lea PC, [PC+reg*4-BCBIAS_J*4]
315 |.endmacro
317 |// Assumes DISPATCH is relative to GL.
318 #define DISPATCH_GL(field)      (GG_DISP2G + (int)offsetof(global_State, field))
319 #define DISPATCH_J(field)       (GG_DISP2J + (int)offsetof(jit_State, field))
321 #define PC2PROTO(field)  ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
323 |// Decrement hashed hotcount and trigger trace recorder if zero.
324 |.macro hotloop, reg
325 |  mov reg, PCd
326 |  shr reg, 1
327 |  and reg, HOTCOUNT_PCMASK
328 |  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
329 |  jb ->vm_hotloop
330 |.endmacro
332 |.macro hotcall, reg
333 |  mov reg, PCd
334 |  shr reg, 1
335 |  and reg, HOTCOUNT_PCMASK
336 |  sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
337 |  jb ->vm_hotcall
338 |.endmacro
340 |// Set current VM state.
341 |.macro set_vmstate, st
342 |  mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
343 |.endmacro
345 |.macro fpop1; fstp st1; .endmacro
347 |// Synthesize SSE FP constants.
348 |.macro sseconst_abs, reg, tmp          // Synthesize abs mask.
349 |  mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
350 |.endmacro
352 |.macro sseconst_hi, reg, tmp, val      // Synthesize hi-32 bit const.
353 |  mov64 tmp, U64x(val,00000000); movd reg, tmp
354 |.endmacro
356 |.macro sseconst_sign, reg, tmp         // Synthesize sign mask.
357 |  sseconst_hi reg, tmp, 80000000
358 |.endmacro
359 |.macro sseconst_1, reg, tmp            // Synthesize 1.0.
360 |  sseconst_hi reg, tmp, 3ff00000
361 |.endmacro
362 |.macro sseconst_m1, reg, tmp           // Synthesize -1.0.
363 |  sseconst_hi reg, tmp, bff00000
364 |.endmacro
365 |.macro sseconst_2p52, reg, tmp         // Synthesize 2^52.
366 |  sseconst_hi reg, tmp, 43300000
367 |.endmacro
368 |.macro sseconst_tobit, reg, tmp        // Synthesize 2^52 + 2^51.
369 |  sseconst_hi reg, tmp, 43380000
370 |.endmacro
372 |// Move table write barrier back. Overwrites reg.
373 |.macro barrierback, tab, reg
374 |  and byte tab->marked, (uint8_t)~LJ_GC_BLACK  // black2gray(tab)
375 |  mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
376 |  mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
377 |  mov tab->gclist, reg
378 |.endmacro
380 |//-----------------------------------------------------------------------
382 /* Generate subroutines used by opcodes and other parts of the VM. */
383 /* The .code_sub section should be last to help static branch prediction. */
384 static void build_subroutines(BuildCtx *ctx)
386   |.code_sub
387   |
388   |//-----------------------------------------------------------------------
389   |//-- Return handling ----------------------------------------------------
390   |//-----------------------------------------------------------------------
391   |
392   |->vm_returnp:
393   |  test PCd, FRAME_P
394   |  jz ->cont_dispatch
395   |
396   |  // Return from pcall or xpcall fast func.
397   |  and PC, -8
398   |  sub BASE, PC                       // Restore caller base.
399   |  lea RA, [RA+PC-8]                  // Rebase RA and prepend one result.
400   |  mov PC, [BASE-8]                   // Fetch PC of previous frame.
401   |  // Prepending may overwrite the pcall frame, so do it at the end.
402   |  mov_true ITYPE
403   |  mov aword [BASE+RA], ITYPE         // Prepend true to results.
404   |
405   |->vm_returnc:
406   |  add RDd, 1                         // RD = nresults+1
407   |  jz ->vm_unwind_yield
408   |  mov MULTRES, RDd
409   |  test PC, FRAME_TYPE
410   |  jz ->BC_RET_Z                      // Handle regular return to Lua.
411   |
412   |->vm_return:
413   |  // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
414   |  xor PC, FRAME_C
415   |  test PCd, FRAME_TYPE
416   |  jnz ->vm_returnp
417   |
418   |  // Return to C.
419   |  set_vmstate C
420   |  and PC, -8
421   |  sub PC, BASE
422   |  neg PC                             // Previous base = BASE - delta.
423   |
424   |  sub RDd, 1
425   |  jz >2
426   |1:  // Move results down.
427   |  mov RB, [BASE+RA]
428   |  mov [BASE-16], RB
429   |  add BASE, 8
430   |  sub RDd, 1
431   |  jnz <1
432   |2:
433   |  mov L:RB, SAVE_L
434   |  mov L:RB->base, PC
435   |3:
436   |  mov RDd, MULTRES
437   |  mov RAd, SAVE_NRES                 // RA = wanted nresults+1
438   |4:
439   |  cmp RAd, RDd
440   |  jne >6                             // More/less results wanted?
441   |5:
442   |  sub BASE, 16
443   |  mov L:RB->top, BASE
444   |
445   |->vm_leave_cp:
446   |  mov RA, SAVE_CFRAME                // Restore previous C frame.
447   |  mov L:RB->cframe, RA
448   |  xor eax, eax                       // Ok return status for vm_pcall.
449   |
450   |->vm_leave_unw:
451   |  restoreregs
452   |  ret
453   |
454   |6:
455   |  jb >7                              // Less results wanted?
456   |  // More results wanted. Check stack size and fill up results with nil.
457   |  cmp BASE, L:RB->maxstack
458   |  ja >8
459   |  mov aword [BASE-16], LJ_TNIL
460   |  add BASE, 8
461   |  add RDd, 1
462   |  jmp <4
463   |
464   |7:  // Less results wanted.
465   |  test RAd, RAd
466   |  jz <5                              // But check for LUA_MULTRET+1.
467   |  sub RA, RD                         // Negative result!
468   |  lea BASE, [BASE+RA*8]              // Correct top.
469   |  jmp <5
470   |
471   |8:  // Corner case: need to grow stack for filling up results.
472   |  // This can happen if:
473   |  // - A C function grows the stack (a lot).
474   |  // - The GC shrinks the stack in between.
475   |  // - A return back from a lua_call() with (high) nresults adjustment.
476   |  mov L:RB->top, BASE                // Save current top held in BASE (yes).
477   |  mov MULTRES, RDd                   // Need to fill only remainder with nil.
478   |  mov CARG2d, RAd
479   |  mov CARG1, L:RB
480   |  call extern lj_state_growstack     // (lua_State *L, int n)
481   |  mov BASE, L:RB->top                // Need the (realloced) L->top in BASE.
482   |  jmp <3
483   |
484   |->vm_unwind_yield:
485   |  mov al, LUA_YIELD
486   |  jmp ->vm_unwind_c_eh
487   |
488   |->vm_unwind_c:                       // Unwind C stack, return from vm_pcall.
489   |  // (void *cframe, int errcode)
490   |  mov eax, CARG2d                    // Error return status for vm_pcall.
491   |  mov rsp, CARG1
492   |->vm_unwind_c_eh:                    // Landing pad for external unwinder.
493   |  mov L:RB, SAVE_L
494   |  mov GL:RB, L:RB->glref
495   |  mov dword GL:RB->vmstate, ~LJ_VMST_C
496   |  jmp ->vm_leave_unw
497   |
498   |->vm_unwind_rethrow:
499   |.if not X64WIN
500   |  mov CARG1, SAVE_L
501   |  mov CARG2d, eax
502   |  restoreregs
503   |  jmp extern lj_err_throw            // (lua_State *L, int errcode)
504   |.endif
505   |
506   |->vm_unwind_ff:                      // Unwind C stack, return from ff pcall.
507   |  // (void *cframe)
508   |  and CARG1, CFRAME_RAWMASK
509   |  mov rsp, CARG1
510   |->vm_unwind_ff_eh:                   // Landing pad for external unwinder.
511   |  mov L:RB, SAVE_L
512   |  mov RDd, 1+1                       // Really 1+2 results, incr. later.
513   |  mov BASE, L:RB->base
514   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
515   |  add DISPATCH, GG_G2DISP
516   |  mov PC, [BASE-8]                   // Fetch PC of previous frame.
517   |  mov_false RA
518   |  mov RB, [BASE]
519   |  mov [BASE-16], RA                  // Prepend false to error message.
520   |  mov [BASE-8], RB
521   |  mov RA, -16                        // Results start at BASE+RA = BASE-16.
522   |  set_vmstate INTERP
523   |  jmp ->vm_returnc                   // Increments RD/MULTRES and returns.
524   |
525   |//-----------------------------------------------------------------------
526   |//-- Grow stack for calls -----------------------------------------------
527   |//-----------------------------------------------------------------------
528   |
529   |->vm_growstack_c:                    // Grow stack for C function.
530   |  mov CARG2d, LUA_MINSTACK
531   |  jmp >2
532   |
533   |->vm_growstack_v:                    // Grow stack for vararg Lua function.
534   |  sub RD, 16                         // LJ_FR2
535   |  jmp >1
536   |
537   |->vm_growstack_f:                    // Grow stack for fixarg Lua function.
538   |  // BASE = new base, RD = nargs+1, RB = L, PC = first PC
539   |  lea RD, [BASE+NARGS:RD*8-8]
540   |1:
541   |  movzx RAd, byte [PC-4+PC2PROTO(framesize)]
542   |  add PC, 4                          // Must point after first instruction.
543   |  mov L:RB->base, BASE
544   |  mov L:RB->top, RD
545   |  mov SAVE_PC, PC
546   |  mov CARG2, RA
547   |2:
548   |  // RB = L, L->base = new base, L->top = top
549   |  mov CARG1, L:RB
550   |  call extern lj_state_growstack     // (lua_State *L, int n)
551   |  mov BASE, L:RB->base
552   |  mov RD, L:RB->top
553   |  mov LFUNC:RB, [BASE-16]
554   |  cleartp LFUNC:RB
555   |  sub RD, BASE
556   |  shr RDd, 3
557   |  add NARGS:RDd, 1
558   |  // BASE = new base, RB = LFUNC, RD = nargs+1
559   |  ins_callt                          // Just retry the call.
560   |
561   |//-----------------------------------------------------------------------
562   |//-- Entry points into the assembler VM ---------------------------------
563   |//-----------------------------------------------------------------------
564   |
565   |->vm_resume:                         // Setup C frame and resume thread.
566   |  // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
567   |  saveregs
568   |  mov L:RB, CARG1                    // Caveat: CARG1 may be RA.
569   |  mov SAVE_L, CARG1
570   |  mov RA, CARG2
571   |  mov PCd, FRAME_CP
572   |  xor RDd, RDd
573   |  lea KBASE, [esp+CFRAME_RESUME]
574   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
575   |  add DISPATCH, GG_G2DISP
576   |  mov SAVE_PC, RD                    // Any value outside of bytecode is ok.
577   |  mov SAVE_CFRAME, RD
578   |  mov SAVE_NRES, RDd
579   |  mov SAVE_ERRF, RDd
580   |  mov L:RB->cframe, KBASE
581   |  cmp byte L:RB->status, RDL
582   |  je >2                              // Initial resume (like a call).
583   |
584   |  // Resume after yield (like a return).
585   |  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
586   |  set_vmstate INTERP
587   |  mov byte L:RB->status, RDL
588   |  mov BASE, L:RB->base
589   |  mov RD, L:RB->top
590   |  sub RD, RA
591   |  shr RDd, 3
592   |  add RDd, 1                         // RD = nresults+1
593   |  sub RA, BASE                       // RA = resultofs
594   |  mov PC, [BASE-8]
595   |  mov MULTRES, RDd
596   |  test PCd, FRAME_TYPE
597   |  jz ->BC_RET_Z
598   |  jmp ->vm_return
599   |
600   |->vm_pcall:                          // Setup protected C frame and enter VM.
601   |  // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
602   |  saveregs
603   |  mov PCd, FRAME_CP
604   |  mov SAVE_ERRF, CARG4d
605   |  jmp >1
606   |
607   |->vm_call:                           // Setup C frame and enter VM.
608   |  // (lua_State *L, TValue *base, int nres1)
609   |  saveregs
610   |  mov PCd, FRAME_C
611   |
612   |1:  // Entry point for vm_pcall above (PC = ftype).
613   |  mov SAVE_NRES, CARG3d
614   |  mov L:RB, CARG1                    // Caveat: CARG1 may be RA.
615   |  mov SAVE_L, CARG1
616   |  mov RA, CARG2
617   |
618   |  mov DISPATCH, L:RB->glref          // Setup pointer to dispatch table.
619   |  mov KBASE, L:RB->cframe            // Add our C frame to cframe chain.
620   |  mov SAVE_CFRAME, KBASE
621   |  mov SAVE_PC, L:RB                  // Any value outside of bytecode is ok.
622   |  add DISPATCH, GG_G2DISP
623   |  mov L:RB->cframe, rsp
624   |
625   |2:  // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype).
626   |  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
627   |  set_vmstate INTERP
628   |  mov BASE, L:RB->base               // BASE = old base (used in vmeta_call).
629   |  add PC, RA
630   |  sub PC, BASE                       // PC = frame delta + frame type
631   |
632   |  mov RD, L:RB->top
633   |  sub RD, RA
634   |  shr NARGS:RDd, 3
635   |  add NARGS:RDd, 1                   // RD = nargs+1
636   |
637   |->vm_call_dispatch:
638   |  mov LFUNC:RB, [RA-16]
639   |  checkfunc LFUNC:RB, ->vmeta_call   // Ensure KBASE defined and != BASE.
640   |
641   |->vm_call_dispatch_f:
642   |  mov BASE, RA
643   |  ins_call
644   |  // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
645   |
646   |->vm_cpcall:                         // Setup protected C frame, call C.
647   |  // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
648   |  saveregs
649   |  mov L:RB, CARG1                    // Caveat: CARG1 may be RA.
650   |  mov SAVE_L, CARG1
651   |  mov SAVE_PC, L:RB                  // Any value outside of bytecode is ok.
652   |
653   |  mov KBASE, L:RB->stack             // Compute -savestack(L, L->top).
654   |  sub KBASE, L:RB->top
655   |   mov DISPATCH, L:RB->glref         // Setup pointer to dispatch table.
656   |  mov SAVE_ERRF, 0                   // No error function.
657   |  mov SAVE_NRES, KBASEd              // Neg. delta means cframe w/o frame.
658   |   add DISPATCH, GG_G2DISP
659   |  // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
660   |
661   |  mov KBASE, L:RB->cframe            // Add our C frame to cframe chain.
662   |  mov SAVE_CFRAME, KBASE
663   |  mov L:RB->cframe, rsp
664   |  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
665   |
666   |  call CARG4                 // (lua_State *L, lua_CFunction func, void *ud)
667   |  // TValue * (new base) or NULL returned in eax (RC).
668   |  test RC, RC
669   |  jz ->vm_leave_cp                   // No base? Just remove C frame.
670   |  mov RA, RC
671   |  mov PCd, FRAME_CP
672   |  jmp <2                             // Else continue with the call.
673   |
674   |//-----------------------------------------------------------------------
675   |//-- Metamethod handling ------------------------------------------------
676   |//-----------------------------------------------------------------------
677   |
678   |//-- Continuation dispatch ----------------------------------------------
679   |
680   |->cont_dispatch:
681   |  // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
682   |  add RA, BASE
683   |  and PC, -8
684   |  mov RB, BASE
685   |  sub BASE, PC                       // Restore caller BASE.
686   |  mov aword [RA+RD*8-8], LJ_TNIL     // Ensure one valid arg.
687   |  mov RC, RA                         // ... in [RC]
688   |  mov PC, [RB-24]                    // Restore PC from [cont|PC].
689   |  mov RA, qword [RB-32]              // May be negative on WIN64 with debug.
690   |.if FFI
691   |  cmp RA, 1
692   |  jbe >1
693   |.endif
694   |  mov LFUNC:KBASE, [BASE-16]
695   |  cleartp LFUNC:KBASE
696   |  mov KBASE, LFUNC:KBASE->pc
697   |  mov KBASE, [KBASE+PC2PROTO(k)]
698   |  // BASE = base, RC = result, RB = meta base
699   |  jmp RA                             // Jump to continuation.
700   |
701   |.if FFI
702   |1:
703   |  je ->cont_ffi_callback             // cont = 1: return from FFI callback.
704   |  // cont = 0: Tail call from C function.
705   |  sub RB, BASE
706   |  shr RBd, 3
707   |  lea RDd, [RBd-3]
708   |  jmp ->vm_call_tail
709   |.endif
710   |
711   |->cont_cat:                          // BASE = base, RC = result, RB = mbase
712   |  movzx RAd, PC_RB
713   |  sub RB, 32
714   |  lea RA, [BASE+RA*8]
715   |  sub RA, RB
716   |  je ->cont_ra
717   |  neg RA
718   |  shr RAd, 3
719   |.if X64WIN
720   |  mov CARG3d, RAd
721   |  mov L:CARG1, SAVE_L
722   |  mov L:CARG1->base, BASE
723   |  mov RC, [RC]
724   |  mov [RB], RC
725   |  mov CARG2, RB
726   |.else
727   |  mov L:CARG1, SAVE_L
728   |  mov L:CARG1->base, BASE
729   |  mov CARG3d, RAd
730   |  mov RA, [RC]
731   |  mov [RB], RA
732   |  mov CARG2, RB
733   |.endif
734   |  jmp ->BC_CAT_Z
735   |
736   |//-- Table indexing metamethods -----------------------------------------
737   |
738   |->vmeta_tgets:
739   |  settp STR:RC, LJ_TSTR              // STR:RC = GCstr *
740   |  mov TMP1, STR:RC
741   |  lea RC, TMP1
742   |  cmp PC_OP, BC_GGET
743   |  jne >1
744   |  settp TAB:RA, TAB:RB, LJ_TTAB      // TAB:RB = GCtab *
745   |  lea RB, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.
746   |  mov [RB], TAB:RA
747   |  jmp >2
748   |
749   |->vmeta_tgetb:
750   |  movzx RCd, PC_RC
751   |.if DUALNUM
752   |  setint RC
753   |  mov TMP1, RC
754   |.else
755   |  cvtsi2sd xmm0, RCd
756   |  movsd TMP1, xmm0
757   |.endif
758   |  lea RC, TMP1
759   |  jmp >1
760   |
761   |->vmeta_tgetv:
762   |  movzx RCd, PC_RC                   // Reload TValue *k from RC.
763   |  lea RC, [BASE+RC*8]
764   |1:
765   |  movzx RBd, PC_RB                   // Reload TValue *t from RB.
766   |  lea RB, [BASE+RB*8]
767   |2:
768   |  mov L:CARG1, SAVE_L
769   |  mov L:CARG1->base, BASE            // Caveat: CARG2/CARG3 may be BASE.
770   |  mov CARG2, RB
771   |  mov CARG3, RC
772   |  mov L:RB, L:CARG1
773   |  mov SAVE_PC, PC
774   |  call extern lj_meta_tget           // (lua_State *L, TValue *o, TValue *k)
775   |  // TValue * (finished) or NULL (metamethod) returned in eax (RC).
776   |  mov BASE, L:RB->base
777   |  test RC, RC
778   |  jz >3
779   |->cont_ra:                           // BASE = base, RC = result
780   |  movzx RAd, PC_RA
781   |  mov RB, [RC]
782   |  mov [BASE+RA*8], RB
783   |  ins_next
784   |
785   |3:  // Call __index metamethod.
786   |  // BASE = base, L->top = new base, stack = cont/func/t/k
787   |  mov RA, L:RB->top
788   |  mov [RA-24], PC                    // [cont|PC]
789   |  lea PC, [RA+FRAME_CONT]
790   |  sub PC, BASE
791   |  mov LFUNC:RB, [RA-16]              // Guaranteed to be a function here.
792   |  mov NARGS:RDd, 2+1                 // 2 args for func(t, k).
793   |  cleartp LFUNC:RB
794   |  jmp ->vm_call_dispatch_f
795   |
796   |->vmeta_tgetr:
797   |  mov CARG1, TAB:RB
798   |  mov RB, BASE                       // Save BASE.
799   |  mov CARG2d, RCd                    // Caveat: CARG2 == BASE
800   |  call extern lj_tab_getinth         // (GCtab *t, int32_t key)
801   |  // cTValue * or NULL returned in eax (RC).
802   |  movzx RAd, PC_RA
803   |  mov BASE, RB                       // Restore BASE.
804   |  test RC, RC
805   |  jnz ->BC_TGETR_Z
806   |  mov ITYPE, LJ_TNIL
807   |  jmp ->BC_TGETR2_Z
808   |
809   |//-----------------------------------------------------------------------
810   |
811   |->vmeta_tsets:
812   |  settp STR:RC, LJ_TSTR              // STR:RC = GCstr *
813   |  mov TMP1, STR:RC
814   |  lea RC, TMP1
815   |  cmp PC_OP, BC_GSET
816   |  jne >1
817   |  settp TAB:RA, TAB:RB, LJ_TTAB      // TAB:RB = GCtab *
818   |  lea RB, [DISPATCH+DISPATCH_GL(tmptv)]  // Store fn->l.env in g->tmptv.
819   |  mov [RB], TAB:RA
820   |  jmp >2
821   |
822   |->vmeta_tsetb:
823   |  movzx RCd, PC_RC
824   |.if DUALNUM
825   |  setint RC
826   |  mov TMP1, RC
827   |.else
828   |  cvtsi2sd xmm0, RCd
829   |  movsd TMP1, xmm0
830   |.endif
831   |  lea RC, TMP1
832   |  jmp >1
833   |
834   |->vmeta_tsetv:
835   |  movzx RCd, PC_RC                   // Reload TValue *k from RC.
836   |  lea RC, [BASE+RC*8]
837   |1:
838   |  movzx RBd, PC_RB                   // Reload TValue *t from RB.
839   |  lea RB, [BASE+RB*8]
840   |2:
841   |  mov L:CARG1, SAVE_L
842   |  mov L:CARG1->base, BASE            // Caveat: CARG2/CARG3 may be BASE.
843   |  mov CARG2, RB
844   |  mov CARG3, RC
845   |  mov L:RB, L:CARG1
846   |  mov SAVE_PC, PC
847   |  call extern lj_meta_tset           // (lua_State *L, TValue *o, TValue *k)
848   |  // TValue * (finished) or NULL (metamethod) returned in eax (RC).
849   |  mov BASE, L:RB->base
850   |  test RC, RC
851   |  jz >3
852   |  // NOBARRIER: lj_meta_tset ensures the table is not black.
853   |  movzx RAd, PC_RA
854   |  mov RB, [BASE+RA*8]
855   |  mov [RC], RB
856   |->cont_nop:                          // BASE = base, (RC = result)
857   |  ins_next
858   |
859   |3:  // Call __newindex metamethod.
860   |  // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
861   |  mov RA, L:RB->top
862   |  mov [RA-24], PC                    // [cont|PC]
863   |  movzx RCd, PC_RA
864   |  // Copy value to third argument.
865   |  mov RB, [BASE+RC*8]
866   |  mov [RA+16], RB
867   |  lea PC, [RA+FRAME_CONT]
868   |  sub PC, BASE
869   |  mov LFUNC:RB, [RA-16]              // Guaranteed to be a function here.
870   |  mov NARGS:RDd, 3+1                 // 3 args for func(t, k, v).
871   |  cleartp LFUNC:RB
872   |  jmp ->vm_call_dispatch_f
873   |
874   |->vmeta_tsetr:
875   |.if X64WIN
876   |  mov L:CARG1, SAVE_L
877   |  mov CARG3d, RCd
878   |  mov L:CARG1->base, BASE
879   |  xchg CARG2, TAB:RB                 // Caveat: CARG2 == BASE.
880   |.else
881   |  mov L:CARG1, SAVE_L
882   |  mov CARG2, TAB:RB
883   |  mov L:CARG1->base, BASE
884   |  mov RB, BASE                       // Save BASE.
885   |  mov CARG3d, RCd                    // Caveat: CARG3 == BASE.
886   |.endif
887   |  mov SAVE_PC, PC
888   |  call extern lj_tab_setinth  // (lua_State *L, GCtab *t, int32_t key)
889   |  // TValue * returned in eax (RC).
890   |  movzx RAd, PC_RA
891   |  mov BASE, RB                       // Restore BASE.
892   |  jmp ->BC_TSETR_Z
893   |
894   |//-- Comparison metamethods ---------------------------------------------
895   |
896   |->vmeta_comp:
897   |  movzx RDd, PC_RD
898   |  movzx RAd, PC_RA
899   |  mov L:RB, SAVE_L
900   |  mov L:RB->base, BASE               // Caveat: CARG2/CARG3 == BASE.
901   |.if X64WIN
902   |  lea CARG3, [BASE+RD*8]
903   |  lea CARG2, [BASE+RA*8]
904   |.else
905   |  lea CARG2, [BASE+RA*8]
906   |  lea CARG3, [BASE+RD*8]
907   |.endif
908   |  mov CARG1, L:RB                    // Caveat: CARG1/CARG4 == RA.
909   |  movzx CARG4d, PC_OP
910   |  mov SAVE_PC, PC
911   |  call extern lj_meta_comp   // (lua_State *L, TValue *o1, *o2, int op)
912   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
913   |3:
914   |  mov BASE, L:RB->base
915   |  cmp RC, 1
916   |  ja ->vmeta_binop
917   |4:
918   |  lea PC, [PC+4]
919   |  jb >6
920   |5:
921   |  movzx RDd, PC_RD
922   |  branchPC RD
923   |6:
924   |  ins_next
925   |
926   |->cont_condt:                        // BASE = base, RC = result
927   |  add PC, 4
928   |  mov ITYPE, [RC]
929   |  sar ITYPE, 47
930   |  cmp ITYPEd, LJ_TISTRUECOND         // Branch if result is true.
931   |  jb <5
932   |  jmp <6
933   |
934   |->cont_condf:                        // BASE = base, RC = result
935   |  mov ITYPE, [RC]
936   |  sar ITYPE, 47
937   |  cmp ITYPEd, LJ_TISTRUECOND         // Branch if result is false.
938   |  jmp <4
939   |
940   |->vmeta_equal:
941   |  cleartp TAB:RD
942   |  sub PC, 4
943   |.if X64WIN
944   |  mov CARG3, RD
945   |  mov CARG4d, RBd
946   |  mov L:RB, SAVE_L
947   |  mov L:RB->base, BASE               // Caveat: CARG2 == BASE.
948   |  mov CARG2, RA
949   |  mov CARG1, L:RB                    // Caveat: CARG1 == RA.
950   |.else
951   |  mov CARG2, RA
952   |  mov CARG4d, RBd                    // Caveat: CARG4 == RA.
953   |  mov L:RB, SAVE_L
954   |  mov L:RB->base, BASE               // Caveat: CARG3 == BASE.
955   |  mov CARG3, RD
956   |  mov CARG1, L:RB
957   |.endif
958   |  mov SAVE_PC, PC
959   |  call extern lj_meta_equal  // (lua_State *L, GCobj *o1, *o2, int ne)
960   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
961   |  jmp <3
962   |
963   |->vmeta_equal_cd:
964   |.if FFI
965   |  sub PC, 4
966   |  mov L:RB, SAVE_L
967   |  mov L:RB->base, BASE
968   |  mov CARG1, L:RB
969   |  mov CARG2d, dword [PC-4]
970   |  mov SAVE_PC, PC
971   |  call extern lj_meta_equal_cd       // (lua_State *L, BCIns ins)
972   |  // 0/1 or TValue * (metamethod) returned in eax (RC).
973   |  jmp <3
974   |.endif
975   |
976   |->vmeta_istype:
977   |  mov L:RB, SAVE_L
978   |  mov L:RB->base, BASE               // Caveat: CARG2/CARG3 may be BASE.
979   |  mov CARG2d, RAd
980   |  mov CARG3d, RDd
981   |  mov L:CARG1, L:RB
982   |  mov SAVE_PC, PC
983   |  call extern lj_meta_istype  // (lua_State *L, BCReg ra, BCReg tp)
984   |  mov BASE, L:RB->base
985   |  jmp <6
986   |
987   |//-- Arithmetic metamethods ---------------------------------------------
988   |
989   |->vmeta_arith_vno:
990   |.if DUALNUM
991   |  movzx RBd, PC_RB
992   |  movzx RCd, PC_RC
993   |.endif
994   |->vmeta_arith_vn:
995   |  lea RC, [KBASE+RC*8]
996   |  jmp >1
997   |
998   |->vmeta_arith_nvo:
999   |.if DUALNUM
1000   |  movzx RBd, PC_RB
1001   |  movzx RCd, PC_RC
1002   |.endif
1003   |->vmeta_arith_nv:
1004   |  lea TMPR, [KBASE+RC*8]
1005   |  lea RC, [BASE+RB*8]
1006   |  mov RB, TMPR
1007   |  jmp >2
1008   |
1009   |->vmeta_unm:
1010   |  lea RC, [BASE+RD*8]
1011   |  mov RB, RC
1012   |  jmp >2
1013   |
1014   |->vmeta_arith_vvo:
1015   |.if DUALNUM
1016   |  movzx RBd, PC_RB
1017   |  movzx RCd, PC_RC
1018   |.endif
1019   |->vmeta_arith_vv:
1020   |  lea RC, [BASE+RC*8]
1021   |1:
1022   |  lea RB, [BASE+RB*8]
1023   |2:
1024   |  lea RA, [BASE+RA*8]
1025   |.if X64WIN
1026   |  mov CARG3, RB
1027   |  mov CARG4, RC
1028   |  movzx RCd, PC_OP
1029   |  mov ARG5d, RCd
1030   |  mov L:RB, SAVE_L
1031   |  mov L:RB->base, BASE               // Caveat: CARG2 == BASE.
1032   |  mov CARG2, RA
1033   |  mov CARG1, L:RB                    // Caveat: CARG1 == RA.
1034   |.else
1035   |  movzx CARG5d, PC_OP
1036   |  mov CARG2, RA
1037   |  mov CARG4, RC                      // Caveat: CARG4 == RA.
1038   |  mov L:CARG1, SAVE_L
1039   |  mov L:CARG1->base, BASE            // Caveat: CARG3 == BASE.
1040   |  mov CARG3, RB
1041   |  mov L:RB, L:CARG1
1042   |.endif
1043   |  mov SAVE_PC, PC
1044   |  call extern lj_meta_arith  // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1045   |  // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1046   |  mov BASE, L:RB->base
1047   |  test RC, RC
1048   |  jz ->cont_nop
1049   |
1050   |  // Call metamethod for binary op.
1051   |->vmeta_binop:
1052   |  // BASE = base, RC = new base, stack = cont/func/o1/o2
1053   |  mov RA, RC
1054   |  sub RC, BASE
1055   |  mov [RA-24], PC                    // [cont|PC]
1056   |  lea PC, [RC+FRAME_CONT]
1057   |  mov NARGS:RDd, 2+1                 // 2 args for func(o1, o2).
1058   |  jmp ->vm_call_dispatch
1059   |
1060   |->vmeta_len:
1061   |  movzx RDd, PC_RD
1062   |  mov L:RB, SAVE_L
1063   |  mov L:RB->base, BASE
1064   |  lea CARG2, [BASE+RD*8]             // Caveat: CARG2 == BASE
1065   |  mov L:CARG1, L:RB
1066   |  mov SAVE_PC, PC
1067   |  call extern lj_meta_len            // (lua_State *L, TValue *o)
1068   |  // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1069   |  mov BASE, L:RB->base
1070 #if LJ_52
1071   |  test RC, RC
1072   |  jne ->vmeta_binop                  // Binop call for compatibility.
1073   |  movzx RDd, PC_RD
1074   |  mov TAB:CARG1, [BASE+RD*8]
1075   |  cleartp TAB:CARG1
1076   |  jmp ->BC_LEN_Z
1077 #else
1078   |  jmp ->vmeta_binop                  // Binop call for compatibility.
1079 #endif
1080   |
1081   |//-- Call metamethod ----------------------------------------------------
1082   |
1083   |->vmeta_call_ra:
1084   |  lea RA, [BASE+RA*8+16]
1085   |->vmeta_call:                        // Resolve and call __call metamethod.
1086   |  // BASE = old base, RA = new base, RC = nargs+1, PC = return
1087   |  mov TMP1d, NARGS:RDd               // Save RA, RC for us.
1088   |  mov RB, RA
1089   |.if X64WIN
1090   |  mov L:TMPR, SAVE_L
1091   |  mov L:TMPR->base, BASE             // Caveat: CARG2 is BASE.
1092   |  lea CARG2, [RA-16]
1093   |  lea CARG3, [RA+NARGS:RD*8-8]
1094   |  mov CARG1, L:TMPR                  // Caveat: CARG1 is RA.
1095   |.else
1096   |  mov L:CARG1, SAVE_L
1097   |  mov L:CARG1->base, BASE            // Caveat: CARG3 is BASE.
1098   |  lea CARG2, [RA-16]
1099   |  lea CARG3, [RA+NARGS:RD*8-8]
1100   |.endif
1101   |  mov SAVE_PC, PC
1102   |  call extern lj_meta_call   // (lua_State *L, TValue *func, TValue *top)
1103   |  mov RA, RB
1104   |  mov L:RB, SAVE_L
1105   |  mov BASE, L:RB->base
1106   |  mov NARGS:RDd, TMP1d
1107   |  mov LFUNC:RB, [RA-16]
1108   |  cleartp LFUNC:RB
1109   |  add NARGS:RDd, 1
1110   |  // This is fragile. L->base must not move, KBASE must always be defined.
1111   |  cmp KBASE, BASE                    // Continue with CALLT if flag set.
1112   |  je ->BC_CALLT_Z
1113   |  mov BASE, RA
1114   |  ins_call                           // Otherwise call resolved metamethod.
1115   |
1116   |//-- Argument coercion for 'for' statement ------------------------------
1117   |
1118   |->vmeta_for:
1119   |  mov L:RB, SAVE_L
1120   |  mov L:RB->base, BASE
1121   |  mov CARG2, RA                      // Caveat: CARG2 == BASE
1122   |  mov L:CARG1, L:RB                  // Caveat: CARG1 == RA
1123   |  mov SAVE_PC, PC
1124   |  call extern lj_meta_for    // (lua_State *L, TValue *base)
1125   |  mov BASE, L:RB->base
1126   |  mov RCd, [PC-4]
1127   |  movzx RAd, RCH
1128   |  movzx OP, RCL
1129   |  shr RCd, 16
1130   |  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]   // Retry FORI or JFORI.
1131   |
1132   |//-----------------------------------------------------------------------
1133   |//-- Fast functions -----------------------------------------------------
1134   |//-----------------------------------------------------------------------
1135   |
1136   |.macro .ffunc, name
1137   |->ff_ .. name:
1138   |.endmacro
1139   |
1140   |.macro .ffunc_1, name
1141   |->ff_ .. name:
1142   |  cmp NARGS:RDd, 1+1;  jb ->fff_fallback
1143   |.endmacro
1144   |
1145   |.macro .ffunc_2, name
1146   |->ff_ .. name:
1147   |  cmp NARGS:RDd, 2+1;  jb ->fff_fallback
1148   |.endmacro
1149   |
1150   |.macro .ffunc_n, name, op
1151   |  .ffunc_1 name
1152   |  checknumtp [BASE], ->fff_fallback
1153   |  op xmm0, qword [BASE]
1154   |.endmacro
1155   |
1156   |.macro .ffunc_n, name
1157   |  .ffunc_n name, movsd
1158   |.endmacro
1159   |
1160   |.macro .ffunc_nn, name
1161   |  .ffunc_2 name
1162   |  checknumtp [BASE], ->fff_fallback
1163   |  checknumtp [BASE+8], ->fff_fallback
1164   |  movsd xmm0, qword [BASE]
1165   |  movsd xmm1, qword [BASE+8]
1166   |.endmacro
1167   |
1168   |// Inlined GC threshold check. Caveat: uses label 1.
1169   |.macro ffgccheck
1170   |  mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1171   |  cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1172   |  jb >1
1173   |  call ->fff_gcstep
1174   |1:
1175   |.endmacro
1176   |
1177   |//-- Base library: checks -----------------------------------------------
1178   |
1179   |.ffunc_1 assert
1180   |  mov ITYPE, [BASE]
1181   |  mov RB, ITYPE
1182   |  sar ITYPE, 47
1183   |  cmp ITYPEd, LJ_TISTRUECOND; jae ->fff_fallback
1184   |  mov PC, [BASE-8]
1185   |  mov MULTRES, RDd
1186   |  mov RB, [BASE]
1187   |  mov [BASE-16], RB
1188   |  sub RDd, 2
1189   |  jz >2
1190   |  mov RA, BASE
1191   |1:
1192   |  add RA, 8
1193   |  mov RB, [RA]
1194   |  mov [RA-16], RB
1195   |  sub RDd, 1
1196   |  jnz <1
1197   |2:
1198   |  mov RDd, MULTRES
1199   |  jmp ->fff_res_
1200   |
1201   |.ffunc_1 type
1202   |  mov RC, [BASE]
1203   |  sar RC, 47
1204   |  mov RBd, LJ_TISNUM
1205   |  cmp RCd, RBd
1206   |  cmovb RCd, RBd
1207   |  not RCd
1208   |2:
1209   |  mov CFUNC:RB, [BASE-16]
1210   |  cleartp CFUNC:RB
1211   |  mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1212   |  mov PC, [BASE-8]
1213   |  settp STR:RC, LJ_TSTR
1214   |  mov [BASE-16], STR:RC
1215   |  jmp ->fff_res1
1216   |
1217   |//-- Base library: getters and setters ---------------------------------
1218   |
1219   |.ffunc_1 getmetatable
1220   |  mov TAB:RB, [BASE]
1221   |  mov PC, [BASE-8]
1222   |  checktab TAB:RB, >6
1223   |1:  // Field metatable must be at same offset for GCtab and GCudata!
1224   |  mov TAB:RB, TAB:RB->metatable
1225   |2:
1226   |  test TAB:RB, TAB:RB
1227   |  mov aword [BASE-16], LJ_TNIL
1228   |  jz ->fff_res1
1229   |  settp TAB:RC, TAB:RB, LJ_TTAB
1230   |  mov [BASE-16], TAB:RC              // Store metatable as default result.
1231   |  mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+8*(GCROOT_MMNAME+MM_metatable)]
1232   |  mov RAd, TAB:RB->hmask
1233   |  and RAd, STR:RC->hash
1234   |  settp STR:RC, LJ_TSTR
1235   |  imul RAd, #NODE
1236   |  add NODE:RA, TAB:RB->node
1237   |3:  // Rearranged logic, because we expect _not_ to find the key.
1238   |  cmp NODE:RA->key, STR:RC
1239   |  je >5
1240   |4:
1241   |  mov NODE:RA, NODE:RA->next
1242   |  test NODE:RA, NODE:RA
1243   |  jnz <3
1244   |  jmp ->fff_res1                     // Not found, keep default result.
1245   |5:
1246   |  mov RB, NODE:RA->val
1247   |  cmp RB, LJ_TNIL; je ->fff_res1     // Ditto for nil value.
1248   |  mov [BASE-16], RB                  // Return value of mt.__metatable.
1249   |  jmp ->fff_res1
1250   |
1251   |6:
1252   |  cmp ITYPEd, LJ_TUDATA; je <1
1253   |  cmp ITYPEd, LJ_TISNUM; ja >7
1254   |  mov ITYPEd, LJ_TISNUM
1255   |7:
1256   |  not ITYPEd
1257   |  mov TAB:RB, [DISPATCH+ITYPE*8+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1258   |  jmp <2
1259   |
1260   |.ffunc_2 setmetatable
1261   |  mov TAB:RB, [BASE]
1262   |  mov TAB:TMPR, TAB:RB
1263   |  checktab TAB:RB, ->fff_fallback
1264   |  // Fast path: no mt for table yet and not clearing the mt.
1265   |  cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1266   |  mov TAB:RA, [BASE+8]
1267   |  checktab TAB:RA, ->fff_fallback
1268   |  mov TAB:RB->metatable, TAB:RA
1269   |  mov PC, [BASE-8]
1270   |  mov [BASE-16], TAB:TMPR                    // Return original table.
1271   |  test byte TAB:RB->marked, LJ_GC_BLACK      // isblack(table)
1272   |  jz >1
1273   |  // Possible write barrier. Table is black, but skip iswhite(mt) check.
1274   |  barrierback TAB:RB, RC
1275   |1:
1276   |  jmp ->fff_res1
1277   |
1278   |.ffunc_2 rawget
1279   |.if X64WIN
1280   |  mov TAB:RA, [BASE]
1281   |  checktab TAB:RA, ->fff_fallback
1282   |  mov RB, BASE                       // Save BASE.
1283   |  lea CARG3, [BASE+8]
1284   |  mov CARG2, TAB:RA                  // Caveat: CARG2 == BASE.
1285   |  mov CARG1, SAVE_L
1286   |.else
1287   |  mov TAB:CARG2, [BASE]
1288   |  checktab TAB:CARG2, ->fff_fallback
1289   |  mov RB, BASE                       // Save BASE.
1290   |  lea CARG3, [BASE+8]                // Caveat: CARG3 == BASE.
1291   |  mov CARG1, SAVE_L
1292   |.endif
1293   |  call extern lj_tab_get     // (lua_State *L, GCtab *t, cTValue *key)
1294   |  // cTValue * returned in eax (RD).
1295   |  mov BASE, RB                       // Restore BASE.
1296   |  // Copy table slot.
1297   |  mov RB, [RD]
1298   |  mov PC, [BASE-8]
1299   |  mov [BASE-16], RB
1300   |  jmp ->fff_res1
1301   |
1302   |//-- Base library: conversions ------------------------------------------
1303   |
1304   |.ffunc tonumber
1305   |  // Only handles the number case inline (without a base argument).
1306   |  cmp NARGS:RDd, 1+1;  jne ->fff_fallback    // Exactly one argument.
1307   |  mov RB, [BASE]
1308   |  checknumber RB, ->fff_fallback
1309   |  mov PC, [BASE-8]
1310   |  mov [BASE-16], RB
1311   |  jmp ->fff_res1
1312   |
1313   |.ffunc_1 tostring
1314   |  // Only handles the string or number case inline.
1315   |  mov PC, [BASE-8]
1316   |  mov STR:RB, [BASE]
1317   |  checktp_nc STR:RB, LJ_TSTR, >3
1318   |  // A __tostring method in the string base metatable is ignored.
1319   |2:
1320   |  mov [BASE-16], STR:RB
1321   |  jmp ->fff_res1
1322   |3:  // Handle numbers inline, unless a number base metatable is present.
1323   |  cmp ITYPEd, LJ_TISNUM;  ja ->fff_fallback_1
1324   |  cmp aword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1325   |  jne ->fff_fallback
1326   |  ffgccheck                          // Caveat: uses label 1.
1327   |  mov L:RB, SAVE_L
1328   |  mov L:RB->base, BASE               // Add frame since C call can throw.
1329   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
1330   |.if not X64WIN
1331   |  mov CARG2, BASE                    // Otherwise: CARG2 == BASE
1332   |.endif
1333   |  mov L:CARG1, L:RB
1334   |.if DUALNUM
1335   |  call extern lj_strfmt_number       // (lua_State *L, cTValue *o)
1336   |.else
1337   |  call extern lj_strfmt_num          // (lua_State *L, lua_Number *np)
1338   |.endif
1339   |  // GCstr returned in eax (RD).
1340   |  mov BASE, L:RB->base
1341   |  settp STR:RB, RD, LJ_TSTR
1342   |  jmp <2
1343   |
1344   |//-- Base library: iterators -------------------------------------------
1345   |
1346   |.ffunc_1 next
1347   |  je >2                              // Missing 2nd arg?
1348   |1:
1349   |.if X64WIN
1350   |  mov RA, [BASE]
1351   |  checktab RA, ->fff_fallback
1352   |.else
1353   |  mov CARG2, [BASE]
1354   |  checktab CARG2, ->fff_fallback
1355   |.endif
1356   |  mov L:RB, SAVE_L
1357   |  mov L:RB->base, BASE               // Add frame since C call can throw.
1358   |  mov L:RB->top, BASE                // Dummy frame length is ok.
1359   |  mov PC, [BASE-8]
1360   |.if X64WIN
1361   |  lea CARG3, [BASE+8]
1362   |  mov CARG2, RA                      // Caveat: CARG2 == BASE.
1363   |  mov CARG1, L:RB
1364   |.else
1365   |  lea CARG3, [BASE+8]                // Caveat: CARG3 == BASE.
1366   |  mov CARG1, L:RB
1367   |.endif
1368   |  mov SAVE_PC, PC                    // Needed for ITERN fallback.
1369   |  call extern lj_tab_next    // (lua_State *L, GCtab *t, TValue *key)
1370   |  // Flag returned in eax (RD).
1371   |  mov BASE, L:RB->base
1372   |  test RDd, RDd;  jz >3              // End of traversal?
1373   |  // Copy key and value to results.
1374   |  mov RB, [BASE+8]
1375   |  mov RD, [BASE+16]
1376   |  mov [BASE-16], RB
1377   |  mov [BASE-8], RD
1378   |->fff_res2:
1379   |  mov RDd, 1+2
1380   |  jmp ->fff_res
1381   |2:  // Set missing 2nd arg to nil.
1382   |  mov aword [BASE+8], LJ_TNIL
1383   |  jmp <1
1384   |3:  // End of traversal: return nil.
1385   |  mov aword [BASE-16], LJ_TNIL
1386   |  jmp ->fff_res1
1387   |
1388   |.ffunc_1 pairs
1389   |  mov TAB:RB, [BASE]
1390   |  mov TMPR, TAB:RB
1391   |  checktab TAB:RB, ->fff_fallback
1392 #if LJ_52
1393   |  cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1394 #endif
1395   |  mov CFUNC:RD, [BASE-16]
1396   |  cleartp CFUNC:RD
1397   |  mov CFUNC:RD, CFUNC:RD->upvalue[0]
1398   |  settp CFUNC:RD, LJ_TFUNC
1399   |  mov PC, [BASE-8]
1400   |  mov [BASE-16], CFUNC:RD
1401   |  mov [BASE-8], TMPR
1402   |  mov aword [BASE], LJ_TNIL
1403   |  mov RDd, 1+3
1404   |  jmp ->fff_res
1405   |
1406   |.ffunc_2 ipairs_aux
1407   |  mov TAB:RB, [BASE]
1408   |  checktab TAB:RB, ->fff_fallback
1409   |.if DUALNUM
1410   |  mov RA, [BASE+8]
1411   |  checkint RA, ->fff_fallback
1412   |.else
1413   |  checknumtp [BASE+8], ->fff_fallback
1414   |  movsd xmm0, qword [BASE+8]
1415   |.endif
1416   |  mov PC, [BASE-8]
1417   |.if DUALNUM
1418   |  add RAd, 1
1419   |  setint ITYPE, RA
1420   |  mov [BASE-16], ITYPE
1421   |.else
1422   |  sseconst_1 xmm1, TMPR
1423   |  addsd xmm0, xmm1
1424   |  cvttsd2si RAd, xmm0
1425   |  movsd qword [BASE-16], xmm0
1426   |.endif
1427   |  cmp RAd, TAB:RB->asize;  jae >2    // Not in array part?
1428   |  mov RD, TAB:RB->array
1429   |  lea RD, [RD+RA*8]
1430   |1:
1431   |  cmp aword [RD], LJ_TNIL;  je ->fff_res0
1432   |  // Copy array slot.
1433   |  mov RB, [RD]
1434   |  mov [BASE-8], RB
1435   |  jmp ->fff_res2
1436   |2:  // Check for empty hash part first. Otherwise call C function.
1437   |  cmp dword TAB:RB->hmask, 0; je ->fff_res0
1438   |.if X64WIN
1439   |  mov TMPR, BASE
1440   |  mov CARG2d, RAd
1441   |  mov CARG1, TAB:RB
1442   |  mov RB, TMPR
1443   |.else
1444   |  mov CARG1, TAB:RB
1445   |  mov RB, BASE                       // Save BASE.
1446   |  mov CARG2d, RAd                    // Caveat: CARG2 == BASE
1447   |.endif
1448   |  call extern lj_tab_getinth         // (GCtab *t, int32_t key)
1449   |  // cTValue * or NULL returned in eax (RD).
1450   |  mov BASE, RB
1451   |  test RD, RD
1452   |  jnz <1
1453   |->fff_res0:
1454   |  mov RDd, 1+0
1455   |  jmp ->fff_res
1456   |
1457   |.ffunc_1 ipairs
1458   |  mov TAB:RB, [BASE]
1459   |  mov TMPR, TAB:RB
1460   |  checktab TAB:RB, ->fff_fallback
1461 #if LJ_52
1462   |  cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1463 #endif
1464   |  mov CFUNC:RD, [BASE-16]
1465   |  cleartp CFUNC:RD
1466   |  mov CFUNC:RD, CFUNC:RD->upvalue[0]
1467   |  settp CFUNC:RD, LJ_TFUNC
1468   |  mov PC, [BASE-8]
1469   |  mov [BASE-16], CFUNC:RD
1470   |  mov [BASE-8], TMPR
1471   |.if DUALNUM
1472   |  mov64 RD, ((int64_t)LJ_TISNUM<<47)
1473   |  mov [BASE], RD
1474   |.else
1475   |  mov qword [BASE], 0
1476   |.endif
1477   |  mov RDd, 1+3
1478   |  jmp ->fff_res
1479   |
1480   |//-- Base library: catch errors ----------------------------------------
1481   |
1482   |.ffunc_1 pcall
1483   |  lea RA, [BASE+16]
1484   |  sub NARGS:RDd, 1
1485   |  mov PCd, 16+FRAME_PCALL
1486   |1:
1487   |  movzx RBd, byte [DISPATCH+DISPATCH_GL(hookmask)]
1488   |  shr RB, HOOK_ACTIVE_SHIFT
1489   |  and RB, 1
1490   |  add PC, RB                         // Remember active hook before pcall.
1491   |  // Note: this does a (harmless) copy of the function to the PC slot, too.
1492   |  mov KBASE, RD
1493   |2:
1494   |  mov RB, [RA+KBASE*8-24]
1495   |  mov [RA+KBASE*8-16], RB
1496   |  sub KBASE, 1
1497   |  ja <2
1498   |  jmp ->vm_call_dispatch
1499   |
1500   |.ffunc_2 xpcall
1501   |  mov LFUNC:RA, [BASE+8]
1502   |  checktp_nc LFUNC:RA, LJ_TFUNC, ->fff_fallback
1503   |  mov LFUNC:RB, [BASE]               // Swap function and traceback.
1504   |  mov [BASE], LFUNC:RA
1505   |  mov [BASE+8], LFUNC:RB
1506   |  lea RA, [BASE+24]
1507   |  sub NARGS:RDd, 2
1508   |  mov PCd, 24+FRAME_PCALL
1509   |  jmp <1
1510   |
1511   |//-- Coroutine library --------------------------------------------------
1512   |
1513   |.macro coroutine_resume_wrap, resume
1514   |.if resume
1515   |.ffunc_1 coroutine_resume
1516   |  mov L:RB, [BASE]
1517   |  cleartp L:RB
1518   |.else
1519   |.ffunc coroutine_wrap_aux
1520   |  mov CFUNC:RB, [BASE-16]
1521   |  cleartp CFUNC:RB
1522   |  mov L:RB, CFUNC:RB->upvalue[0].gcr
1523   |  cleartp L:RB
1524   |.endif
1525   |  mov PC, [BASE-8]
1526   |  mov SAVE_PC, PC
1527   |  mov TMP1, L:RB
1528   |.if resume
1529   |  checktptp [BASE], LJ_TTHREAD, ->fff_fallback
1530   |.endif
1531   |  cmp aword L:RB->cframe, 0; jne ->fff_fallback
1532   |  cmp byte L:RB->status, LUA_YIELD;  ja ->fff_fallback
1533   |  mov RA, L:RB->top
1534   |  je >1                              // Status != LUA_YIELD (i.e. 0)?
1535   |  cmp RA, L:RB->base                 // Check for presence of initial func.
1536   |  je ->fff_fallback
1537   |  mov PC, [RA-8]                     // Move initial function up.
1538   |  mov [RA], PC
1539   |  add RA, 8
1540   |1:
1541   |.if resume
1542   |  lea PC, [RA+NARGS:RD*8-16]         // Check stack space (-1-thread).
1543   |.else
1544   |  lea PC, [RA+NARGS:RD*8-8]          // Check stack space (-1).
1545   |.endif
1546   |  cmp PC, L:RB->maxstack; ja ->fff_fallback
1547   |  mov L:RB->top, PC
1548   |
1549   |  mov L:RB, SAVE_L
1550   |  mov L:RB->base, BASE
1551   |.if resume
1552   |  add BASE, 8                        // Keep resumed thread in stack for GC.
1553   |.endif
1554   |  mov L:RB->top, BASE
1555   |.if resume
1556   |  lea RB, [BASE+NARGS:RD*8-24]       // RB = end of source for stack move.
1557   |.else
1558   |  lea RB, [BASE+NARGS:RD*8-16]       // RB = end of source for stack move.
1559   |.endif
1560   |  sub RB, PC                 // Relative to PC.
1561   |
1562   |  cmp PC, RA
1563   |  je >3
1564   |2:  // Move args to coroutine.
1565   |  mov RC, [PC+RB]
1566   |  mov [PC-8], RC
1567   |  sub PC, 8
1568   |  cmp PC, RA
1569   |  jne <2
1570   |3:
1571   |  mov CARG2, RA
1572   |  mov CARG1, TMP1
1573   |  call ->vm_resume                   // (lua_State *L, TValue *base, 0, 0)
1574   |
1575   |  mov L:RB, SAVE_L
1576   |  mov L:PC, TMP1
1577   |  mov BASE, L:RB->base
1578   |  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
1579   |  set_vmstate INTERP
1580   |
1581   |  cmp eax, LUA_YIELD
1582   |  ja >8
1583   |4:
1584   |  mov RA, L:PC->base
1585   |  mov KBASE, L:PC->top
1586   |  mov L:PC->top, RA                  // Clear coroutine stack.
1587   |  mov PC, KBASE
1588   |  sub PC, RA
1589   |  je >6                              // No results?
1590   |  lea RD, [BASE+PC]
1591   |  shr PCd, 3
1592   |  cmp RD, L:RB->maxstack
1593   |  ja >9                              // Need to grow stack?
1594   |
1595   |  mov RB, BASE
1596   |  sub RB, RA
1597   |5:  // Move results from coroutine.
1598   |  mov RD, [RA]
1599   |  mov [RA+RB], RD
1600   |  add RA, 8
1601   |  cmp RA, KBASE
1602   |  jne <5
1603   |6:
1604   |.if resume
1605   |  lea RDd, [PCd+2]                   // nresults+1 = 1 + true + results.
1606   |  mov_true ITYPE                     // Prepend true to results.
1607   |  mov [BASE-8], ITYPE
1608   |.else
1609   |  lea RDd, [PCd+1]                   // nresults+1 = 1 + results.
1610   |.endif
1611   |7:
1612   |  mov PC, SAVE_PC
1613   |  mov MULTRES, RDd
1614   |.if resume
1615   |  mov RA, -8
1616   |.else
1617   |  xor RAd, RAd
1618   |.endif
1619   |  test PCd, FRAME_TYPE
1620   |  jz ->BC_RET_Z
1621   |  jmp ->vm_return
1622   |
1623   |8:  // Coroutine returned with error (at co->top-1).
1624   |.if resume
1625   |  mov_false ITYPE                    // Prepend false to results.
1626   |  mov [BASE-8], ITYPE
1627   |  mov RA, L:PC->top
1628   |  sub RA, 8
1629   |  mov L:PC->top, RA                  // Clear error from coroutine stack.
1630   |  // Copy error message.
1631   |  mov RD, [RA]
1632   |  mov [BASE], RD
1633   |  mov RDd, 1+2                       // nresults+1 = 1 + false + error.
1634   |  jmp <7
1635   |.else
1636   |  mov CARG2, L:PC
1637   |  mov CARG1, L:RB
1638   |  call extern lj_ffh_coroutine_wrap_err  // (lua_State *L, lua_State *co)
1639   |  // Error function does not return.
1640   |.endif
1641   |
1642   |9:  // Handle stack expansion on return from yield.
1643   |  mov L:RA, TMP1
1644   |  mov L:RA->top, KBASE               // Undo coroutine stack clearing.
1645   |  mov CARG2, PC
1646   |  mov CARG1, L:RB
1647   |  call extern lj_state_growstack     // (lua_State *L, int n)
1648   |  mov L:PC, TMP1
1649   |  mov BASE, L:RB->base
1650   |  jmp <4                             // Retry the stack move.
1651   |.endmacro
1652   |
1653   |  coroutine_resume_wrap 1            // coroutine.resume
1654   |  coroutine_resume_wrap 0            // coroutine.wrap
1655   |
1656   |.ffunc coroutine_yield
1657   |  mov L:RB, SAVE_L
1658   |  test aword L:RB->cframe, CFRAME_RESUME
1659   |  jz ->fff_fallback
1660   |  mov L:RB->base, BASE
1661   |  lea RD, [BASE+NARGS:RD*8-8]
1662   |  mov L:RB->top, RD
1663   |  xor RDd, RDd
1664   |  mov aword L:RB->cframe, RD
1665   |  mov al, LUA_YIELD
1666   |  mov byte L:RB->status, al
1667   |  jmp ->vm_leave_unw
1668   |
1669   |//-- Math library -------------------------------------------------------
1670   |
1671   |  .ffunc_1 math_abs
1672   |  mov RB, [BASE]
1673   |.if DUALNUM
1674   |  checkint RB, >3
1675   |  cmp RBd, 0; jns ->fff_resi
1676   |  neg RBd; js >2
1677   |->fff_resbit:
1678   |->fff_resi:
1679   |  setint RB
1680   |->fff_resRB:
1681   |  mov PC, [BASE-8]
1682   |  mov [BASE-16], RB
1683   |  jmp ->fff_res1
1684   |2:
1685   |  mov64 RB, U64x(41e00000,00000000)  // 2^31.
1686   |  jmp ->fff_resRB
1687   |3:
1688   |  ja ->fff_fallback
1689   |.else
1690   |  checknum RB, ->fff_fallback
1691   |.endif
1692   |  shl RB, 1
1693   |  shr RB, 1
1694   |  mov PC, [BASE-8]
1695   |  mov [BASE-16], RB
1696   |  jmp ->fff_res1
1697   |
1698   |.ffunc_n math_sqrt, sqrtsd
1699   |->fff_resxmm0:
1700   |  mov PC, [BASE-8]
1701   |  movsd qword [BASE-16], xmm0
1702   |  // fallthrough
1703   |
1704   |->fff_res1:
1705   |  mov RDd, 1+1
1706   |->fff_res:
1707   |  mov MULTRES, RDd
1708   |->fff_res_:
1709   |  test PCd, FRAME_TYPE
1710   |  jnz >7
1711   |5:
1712   |  cmp PC_RB, RDL                     // More results expected?
1713   |  ja >6
1714   |  // Adjust BASE. KBASE is assumed to be set for the calling frame.
1715   |  movzx RAd, PC_RA
1716   |  neg RA
1717   |  lea BASE, [BASE+RA*8-16]           // base = base - (RA+2)*8
1718   |  ins_next
1719   |
1720   |6:  // Fill up results with nil.
1721   |  mov aword [BASE+RD*8-24], LJ_TNIL
1722   |  add RD, 1
1723   |  jmp <5
1724   |
1725   |7:  // Non-standard return case.
1726   |  mov RA, -16                        // Results start at BASE+RA = BASE-16.
1727   |  jmp ->vm_return
1728   |
1729   |.macro math_round, func
1730   |  .ffunc math_ .. func
1731   |.if DUALNUM
1732   |  mov RB, [BASE]
1733   |  checknumx RB, ->fff_resRB, je
1734   |  ja ->fff_fallback
1735   |.else
1736   |  checknumtp [BASE], ->fff_fallback
1737   |.endif
1738   |  movsd xmm0, qword [BASE]
1739   |  call ->vm_ .. func .. _sse
1740   |.if DUALNUM
1741   |  cvttsd2si RBd, xmm0
1742   |  cmp RBd, 0x80000000
1743   |  jne ->fff_resi
1744   |  cvtsi2sd xmm1, RBd
1745   |  ucomisd xmm0, xmm1
1746   |  jp ->fff_resxmm0
1747   |  je ->fff_resi
1748   |.endif
1749   |  jmp ->fff_resxmm0
1750   |.endmacro
1751   |
1752   |  math_round floor
1753   |  math_round ceil
1754   |
1755   |.ffunc math_log
1756   |  cmp NARGS:RDd, 1+1; jne ->fff_fallback     // Exactly one argument.
1757   |  checknumtp [BASE], ->fff_fallback
1758   |  movsd xmm0, qword [BASE]
1759   |  mov RB, BASE
1760   |  call extern log
1761   |  mov BASE, RB
1762   |  jmp ->fff_resxmm0
1763   |
1764   |.macro math_extern, func
1765   |  .ffunc_n math_ .. func
1766   |  mov RB, BASE
1767   |  call extern func
1768   |  mov BASE, RB
1769   |  jmp ->fff_resxmm0
1770   |.endmacro
1771   |
1772   |.macro math_extern2, func
1773   |  .ffunc_nn math_ .. func
1774   |  mov RB, BASE
1775   |  call extern func
1776   |  mov BASE, RB
1777   |  jmp ->fff_resxmm0
1778   |.endmacro
1779   |
1780   |  math_extern log10
1781   |  math_extern exp
1782   |  math_extern sin
1783   |  math_extern cos
1784   |  math_extern tan
1785   |  math_extern asin
1786   |  math_extern acos
1787   |  math_extern atan
1788   |  math_extern sinh
1789   |  math_extern cosh
1790   |  math_extern tanh
1791   |  math_extern2 pow
1792   |  math_extern2 atan2
1793   |  math_extern2 fmod
1794   |
1795   |.ffunc_2 math_ldexp
1796   |  checknumtp [BASE], ->fff_fallback
1797   |  checknumtp [BASE+8], ->fff_fallback
1798   |  fld qword [BASE+8]
1799   |  fld qword [BASE]
1800   |  fscale
1801   |  fpop1
1802   |  mov PC, [BASE-8]
1803   |  fstp qword [BASE-16]
1804   |  jmp ->fff_res1
1805   |
1806   |.ffunc_n math_frexp
1807   |.if X64WIN
1808   |  lea CARG2, TMP1
1809   |.else
1810   |  lea CARG1, TMP1
1811   |.endif
1812   |  mov RB, BASE
1813   |  call extern frexp
1814   |  mov BASE, RB
1815   |  mov RBd, TMP1d
1816   |  mov PC, [BASE-8]
1817   |  movsd qword [BASE-16], xmm0
1818   |.if DUALNUM
1819   |  setint RB
1820   |  mov [BASE-8], RB
1821   |.else
1822   |  cvtsi2sd xmm1, RBd
1823   |  movsd qword [BASE-8], xmm1
1824   |.endif
1825   |  mov RDd, 1+2
1826   |  jmp ->fff_res
1827   |
1828   |.ffunc_n math_modf
1829   |.if X64WIN
1830   |  lea CARG2, [BASE-16]
1831   |.else
1832   |  lea CARG1, [BASE-16]
1833   |.endif
1834   |  mov PC, [BASE-8]
1835   |  mov RB, BASE
1836   |  call extern modf
1837   |  mov BASE, RB
1838   |  mov PC, [BASE-8]
1839   |  movsd qword [BASE-8], xmm0
1840   |  mov RDd, 1+2
1841   |  jmp ->fff_res
1842   |
1843   |.macro math_minmax, name, cmovop, sseop
1844   |  .ffunc name
1845   |  mov RAd, 2
1846   |.if DUALNUM
1847   |  mov RB, [BASE]
1848   |  checkint RB, >4
1849   |1:  // Handle integers.
1850   |  cmp RAd, RDd; jae ->fff_resRB
1851   |  mov TMPR, [BASE+RA*8-8]
1852   |  checkint TMPR, >3
1853   |  cmp RBd, TMPRd
1854   |  cmovop RB, TMPR
1855   |  add RAd, 1
1856   |  jmp <1
1857   |3:
1858   |  ja ->fff_fallback
1859   |  // Convert intermediate result to number and continue below.
1860   |  cvtsi2sd xmm0, RBd
1861   |  jmp >6
1862   |4:
1863   |  ja ->fff_fallback
1864   |.else
1865   |  checknumtp [BASE], ->fff_fallback
1866   |.endif
1867   |
1868   |  movsd xmm0, qword [BASE]
1869   |5:  // Handle numbers or integers.
1870   |  cmp RAd, RDd; jae ->fff_resxmm0
1871   |.if DUALNUM
1872   |  mov RB, [BASE+RA*8-8]
1873   |  checknumx RB, >6, jb
1874   |  ja ->fff_fallback
1875   |  cvtsi2sd xmm1, RBd
1876   |  jmp >7
1877   |.else
1878   |  checknumtp [BASE+RA*8-8], ->fff_fallback
1879   |.endif
1880   |6:
1881   |  movsd xmm1, qword [BASE+RA*8-8]
1882   |7:
1883   |  sseop xmm0, xmm1
1884   |  add RAd, 1
1885   |  jmp <5
1886   |.endmacro
1887   |
1888   |  math_minmax math_min, cmovg, minsd
1889   |  math_minmax math_max, cmovl, maxsd
1890   |
1891   |//-- String library -----------------------------------------------------
1892   |
1893   |.ffunc string_byte                   // Only handle the 1-arg case here.
1894   |  cmp NARGS:RDd, 1+1;  jne ->fff_fallback
1895   |  mov STR:RB, [BASE]
1896   |  checkstr STR:RB, ->fff_fallback
1897   |  mov PC, [BASE-8]
1898   |  cmp dword STR:RB->len, 1
1899   |  jb ->fff_res0                      // Return no results for empty string.
1900   |  movzx RBd, byte STR:RB[1]
1901   |.if DUALNUM
1902   |  jmp ->fff_resi
1903   |.else
1904   |  cvtsi2sd xmm0, RBd; jmp ->fff_resxmm0
1905   |.endif
1906   |
1907   |.ffunc string_char                   // Only handle the 1-arg case here.
1908   |  ffgccheck
1909   |  cmp NARGS:RDd, 1+1;  jne ->fff_fallback    // *Exactly* 1 arg.
1910   |.if DUALNUM
1911   |  mov RB, [BASE]
1912   |  checkint RB, ->fff_fallback
1913   |.else
1914   |  checknumtp [BASE], ->fff_fallback
1915   |  cvttsd2si RBd, qword [BASE]
1916   |.endif
1917   |  cmp RBd, 255;  ja ->fff_fallback
1918   |  mov TMP1d, RBd
1919   |  mov TMPRd, 1
1920   |  lea RD, TMP1                       // Points to stack. Little-endian.
1921   |->fff_newstr:
1922   |  mov L:RB, SAVE_L
1923   |  mov L:RB->base, BASE
1924   |  mov CARG3d, TMPRd                  // Zero-extended to size_t.
1925   |  mov CARG2, RD
1926   |  mov CARG1, L:RB
1927   |  mov SAVE_PC, PC
1928   |  call extern lj_str_new             // (lua_State *L, char *str, size_t l)
1929   |->fff_resstr:
1930   |  // GCstr * returned in eax (RD).
1931   |  mov BASE, L:RB->base
1932   |  mov PC, [BASE-8]
1933   |  settp STR:RD, LJ_TSTR
1934   |  mov [BASE-16], STR:RD
1935   |  jmp ->fff_res1
1936   |
1937   |.ffunc string_sub
1938   |  ffgccheck
1939   |  mov TMPRd, -1
1940   |  cmp NARGS:RDd, 1+2;  jb ->fff_fallback
1941   |  jna >1
1942   |.if DUALNUM
1943   |  mov TMPR, [BASE+16]
1944   |  checkint TMPR, ->fff_fallback
1945   |.else
1946   |  checknumtp [BASE+16], ->fff_fallback
1947   |  cvttsd2si TMPRd, qword [BASE+16]
1948   |.endif
1949   |1:
1950   |  mov STR:RB, [BASE]
1951   |  checkstr STR:RB, ->fff_fallback
1952   |.if DUALNUM
1953   |  mov ITYPE, [BASE+8]
1954   |  mov RAd, ITYPEd                    // Must clear hiword for lea below.
1955   |  sar ITYPE, 47
1956   |  cmp ITYPEd, LJ_TISNUM
1957   |  jne ->fff_fallback
1958   |.else
1959   |  checknumtp [BASE+8], ->fff_fallback
1960   |  cvttsd2si RAd, qword [BASE+8]
1961   |.endif
1962   |  mov RCd, STR:RB->len
1963   |  cmp RCd, TMPRd                     // len < end? (unsigned compare)
1964   |  jb >5
1965   |2:
1966   |  test RAd, RAd                      // start <= 0?
1967   |  jle >7
1968   |3:
1969   |  sub TMPRd, RAd                     // start > end?
1970   |  jl ->fff_emptystr
1971   |  lea RD, [STR:RB+RAd+#STR-1]
1972   |  add TMPRd, 1
1973   |4:
1974   |  jmp ->fff_newstr
1975   |
1976   |5:  // Negative end or overflow.
1977   |  jl >6
1978   |  lea TMPRd, [TMPRd+RCd+1]           // end = end+(len+1)
1979   |  jmp <2
1980   |6:  // Overflow.
1981   |  mov TMPRd, RCd                     // end = len
1982   |  jmp <2
1983   |
1984   |7:  // Negative start or underflow.
1985   |  je >8
1986   |  add RAd, RCd                       // start = start+(len+1)
1987   |  add RAd, 1
1988   |  jg <3                              // start > 0?
1989   |8:  // Underflow.
1990   |  mov RAd, 1                         // start = 1
1991   |  jmp <3
1992   |
1993   |->fff_emptystr:  // Range underflow.
1994   |  xor TMPRd, TMPRd                   // Zero length. Any ptr in RD is ok.
1995   |  jmp <4
1996   |
1997   |.macro ffstring_op, name
1998   |  .ffunc_1 string_ .. name
1999   |  ffgccheck
2000   |.if X64WIN
2001   |  mov STR:TMPR, [BASE]
2002   |  checkstr STR:TMPR, ->fff_fallback
2003   |.else
2004   |  mov STR:CARG2, [BASE]
2005   |  checkstr STR:CARG2, ->fff_fallback
2006   |.endif
2007   |  mov L:RB, SAVE_L
2008   |   lea SBUF:CARG1, [DISPATCH+DISPATCH_GL(tmpbuf)]
2009   |  mov L:RB->base, BASE
2010   |.if X64WIN
2011   |  mov STR:CARG2, STR:TMPR            // Caveat: CARG2 == BASE
2012   |.endif
2013   |   mov RC, SBUF:CARG1->b
2014   |   mov SBUF:CARG1->L, L:RB
2015   |   mov SBUF:CARG1->p, RC
2016   |  mov SAVE_PC, PC
2017   |  call extern lj_buf_putstr_ .. name
2018   |  mov CARG1, rax
2019   |  call extern lj_buf_tostr
2020   |  jmp ->fff_resstr
2021   |.endmacro
2022   |
2023   |ffstring_op reverse
2024   |ffstring_op lower
2025   |ffstring_op upper
2026   |
2027   |//-- Bit library --------------------------------------------------------
2028   |
2029   |.macro .ffunc_bit, name, kind, fdef
2030   |  fdef name
2031   |.if kind == 2
2032   |  sseconst_tobit xmm1, RB
2033   |.endif
2034   |.if DUALNUM
2035   |  mov RB, [BASE]
2036   |  checkint RB, >1
2037   |.if kind > 0
2038   |  jmp >2
2039   |.else
2040   |  jmp ->fff_resbit
2041   |.endif
2042   |1:
2043   |  ja ->fff_fallback
2044   |  movd xmm0, RB
2045   |.else
2046   |  checknumtp [BASE], ->fff_fallback
2047   |  movsd xmm0, qword [BASE]
2048   |.endif
2049   |.if kind < 2
2050   |  sseconst_tobit xmm1, RB
2051   |.endif
2052   |  addsd xmm0, xmm1
2053   |  movd RBd, xmm0
2054   |2:
2055   |.endmacro
2056   |
2057   |.macro .ffunc_bit, name, kind
2058   |  .ffunc_bit name, kind, .ffunc_1
2059   |.endmacro
2060   |
2061   |.ffunc_bit bit_tobit, 0
2062   |  jmp ->fff_resbit
2063   |
2064   |.macro .ffunc_bit_op, name, ins
2065   |  .ffunc_bit name, 2
2066   |  mov TMPRd, NARGS:RDd               // Save for fallback.
2067   |  lea RD, [BASE+NARGS:RD*8-16]
2068   |1:
2069   |  cmp RD, BASE
2070   |  jbe ->fff_resbit
2071   |.if DUALNUM
2072   |  mov RA, [RD]
2073   |  checkint RA, >2
2074   |  ins RBd, RAd
2075   |  sub RD, 8
2076   |  jmp <1
2077   |2:
2078   |  ja ->fff_fallback_bit_op
2079   |  movd xmm0, RA
2080   |.else
2081   |  checknumtp [RD], ->fff_fallback_bit_op
2082   |  movsd xmm0, qword [RD]
2083   |.endif
2084   |  addsd xmm0, xmm1
2085   |  movd RAd, xmm0
2086   |  ins RBd, RAd
2087   |  sub RD, 8
2088   |  jmp <1
2089   |.endmacro
2090   |
2091   |.ffunc_bit_op bit_band, and
2092   |.ffunc_bit_op bit_bor, or
2093   |.ffunc_bit_op bit_bxor, xor
2094   |
2095   |.ffunc_bit bit_bswap, 1
2096   |  bswap RBd
2097   |  jmp ->fff_resbit
2098   |
2099   |.ffunc_bit bit_bnot, 1
2100   |  not RBd
2101   |.if DUALNUM
2102   |  jmp ->fff_resbit
2103   |.else
2104   |->fff_resbit:
2105   |  cvtsi2sd xmm0, RBd
2106   |  jmp ->fff_resxmm0
2107   |.endif
2108   |
2109   |->fff_fallback_bit_op:
2110   |  mov NARGS:RDd, TMPRd               // Restore for fallback
2111   |  jmp ->fff_fallback
2112   |
2113   |.macro .ffunc_bit_sh, name, ins
2114   |.if DUALNUM
2115   |  .ffunc_bit name, 1, .ffunc_2
2116   |  // Note: no inline conversion from number for 2nd argument!
2117   |  mov RA, [BASE+8]
2118   |  checkint RA, ->fff_fallback
2119   |.else
2120   |  .ffunc_nn name
2121   |  sseconst_tobit xmm2, RB
2122   |  addsd xmm0, xmm2
2123   |  addsd xmm1, xmm2
2124   |  movd RBd, xmm0
2125   |  movd RAd, xmm1
2126   |.endif
2127   |  ins RBd, cl                        // Assumes RA is ecx.
2128   |  jmp ->fff_resbit
2129   |.endmacro
2130   |
2131   |.ffunc_bit_sh bit_lshift, shl
2132   |.ffunc_bit_sh bit_rshift, shr
2133   |.ffunc_bit_sh bit_arshift, sar
2134   |.ffunc_bit_sh bit_rol, rol
2135   |.ffunc_bit_sh bit_ror, ror
2136   |
2137   |//-----------------------------------------------------------------------
2138   |
2139   |->fff_fallback_2:
2140   |  mov NARGS:RDd, 1+2                 // Other args are ignored, anyway.
2141   |  jmp ->fff_fallback
2142   |->fff_fallback_1:
2143   |  mov NARGS:RDd, 1+1                 // Other args are ignored, anyway.
2144   |->fff_fallback:                      // Call fast function fallback handler.
2145   |  // BASE = new base, RD = nargs+1
2146   |  mov L:RB, SAVE_L
2147   |  mov PC, [BASE-8]                   // Fallback may overwrite PC.
2148   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
2149   |  mov L:RB->base, BASE
2150   |  lea RD, [BASE+NARGS:RD*8-8]
2151   |  lea RA, [RD+8*LUA_MINSTACK]        // Ensure enough space for handler.
2152   |  mov L:RB->top, RD
2153   |  mov CFUNC:RD, [BASE-16]
2154   |  cleartp CFUNC:RD
2155   |  cmp RA, L:RB->maxstack
2156   |  ja >5                              // Need to grow stack.
2157   |  mov CARG1, L:RB
2158   |  call aword CFUNC:RD->f             // (lua_State *L)
2159   |  mov BASE, L:RB->base
2160   |  // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2161   |  test RDd, RDd; jg ->fff_res        // Returned nresults+1?
2162   |1:
2163   |  mov RA, L:RB->top
2164   |  sub RA, BASE
2165   |  shr RAd, 3
2166   |  test RDd, RDd
2167   |  lea NARGS:RDd, [RAd+1]
2168   |  mov LFUNC:RB, [BASE-16]
2169   |  jne ->vm_call_tail                 // Returned -1?
2170   |  cleartp LFUNC:RB
2171   |  ins_callt                          // Returned 0: retry fast path.
2172   |
2173   |// Reconstruct previous base for vmeta_call during tailcall.
2174   |->vm_call_tail:
2175   |  mov RA, BASE
2176   |  test PCd, FRAME_TYPE
2177   |  jnz >3
2178   |  movzx RBd, PC_RA
2179   |  neg RB
2180   |  lea BASE, [BASE+RB*8-16]           // base = base - (RB+2)*8
2181   |  jmp ->vm_call_dispatch             // Resolve again for tailcall.
2182   |3:
2183   |  mov RB, PC
2184   |  and RB, -8
2185   |  sub BASE, RB
2186   |  jmp ->vm_call_dispatch             // Resolve again for tailcall.
2187   |
2188   |5:  // Grow stack for fallback handler.
2189   |  mov CARG2d, LUA_MINSTACK
2190   |  mov CARG1, L:RB
2191   |  call extern lj_state_growstack     // (lua_State *L, int n)
2192   |  mov BASE, L:RB->base
2193   |  xor RDd, RDd                       // Simulate a return 0.
2194   |  jmp <1                             // Dumb retry (goes through ff first).
2195   |
2196   |->fff_gcstep:                        // Call GC step function.
2197   |  // BASE = new base, RD = nargs+1
2198   |  pop RB                             // Must keep stack at same level.
2199   |  mov TMP1, RB                       // Save return address
2200   |  mov L:RB, SAVE_L
2201   |  mov SAVE_PC, PC                    // Redundant (but a defined value).
2202   |  mov L:RB->base, BASE
2203   |  lea RD, [BASE+NARGS:RD*8-8]
2204   |  mov CARG1, L:RB
2205   |  mov L:RB->top, RD
2206   |  call extern lj_gc_step             // (lua_State *L)
2207   |  mov BASE, L:RB->base
2208   |  mov RD, L:RB->top
2209   |  sub RD, BASE
2210   |  shr RDd, 3
2211   |  add NARGS:RDd, 1
2212   |  mov RB, TMP1
2213   |  push RB                            // Restore return address.
2214   |  ret
2215   |
2216   |//-----------------------------------------------------------------------
2217   |//-- Special dispatch targets -------------------------------------------
2218   |//-----------------------------------------------------------------------
2219   |
2220   |->vm_record:                         // Dispatch target for recording phase.
2221   |.if JIT
2222   |  movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2223   |  test RDL, HOOK_VMEVENT             // No recording while in vmevent.
2224   |  jnz >5
2225   |  // Decrement the hookcount for consistency, but always do the call.
2226   |  test RDL, HOOK_ACTIVE
2227   |  jnz >1
2228   |  test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2229   |  jz >1
2230   |  dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2231   |  jmp >1
2232   |.endif
2233   |
2234   |->vm_rethook:                        // Dispatch target for return hooks.
2235   |  movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2236   |  test RDL, HOOK_ACTIVE              // Hook already active?
2237   |  jnz >5
2238   |  jmp >1
2239   |
2240   |->vm_inshook:                        // Dispatch target for instr/line hooks.
2241   |  movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2242   |  test RDL, HOOK_ACTIVE              // Hook already active?
2243   |  jnz >5
2244   |
2245   |  test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2246   |  jz >5
2247   |  dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2248   |  jz >1
2249   |  test RDL, LUA_MASKLINE
2250   |  jz >5
2251   |1:
2252   |  mov L:RB, SAVE_L
2253   |  mov L:RB->base, BASE
2254   |  mov CARG2, PC                      // Caveat: CARG2 == BASE
2255   |  mov CARG1, L:RB
2256   |  // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2257   |  call extern lj_dispatch_ins        // (lua_State *L, const BCIns *pc)
2258   |3:
2259   |  mov BASE, L:RB->base
2260   |4:
2261   |  movzx RAd, PC_RA
2262   |5:
2263   |  movzx OP, PC_OP
2264   |  movzx RDd, PC_RD
2265   |  jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]   // Re-dispatch to static ins.
2266   |
2267   |->cont_hook:                         // Continue from hook yield.
2268   |  add PC, 4
2269   |  mov RA, [RB-40]
2270   |  mov MULTRES, RAd                   // Restore MULTRES for *M ins.
2271   |  jmp <4
2272   |
2273   |->vm_hotloop:                        // Hot loop counter underflow.
2274   |.if JIT
2275   |  mov LFUNC:RB, [BASE-16]            // Same as curr_topL(L).
2276   |  cleartp LFUNC:RB
2277   |  mov RB, LFUNC:RB->pc
2278   |  movzx RDd, byte [RB+PC2PROTO(framesize)]
2279   |  lea RD, [BASE+RD*8]
2280   |  mov L:RB, SAVE_L
2281   |  mov L:RB->base, BASE
2282   |  mov L:RB->top, RD
2283   |  mov CARG2, PC
2284   |  lea CARG1, [DISPATCH+GG_DISP2J]
2285   |  mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2286   |  mov SAVE_PC, PC
2287   |  call extern lj_trace_hot           // (jit_State *J, const BCIns *pc)
2288   |  jmp <3
2289   |.endif
2290   |
2291   |->vm_callhook:                       // Dispatch target for call hooks.
2292   |  mov SAVE_PC, PC
2293   |.if JIT
2294   |  jmp >1
2295   |.endif
2296   |
2297   |->vm_hotcall:                        // Hot call counter underflow.
2298   |.if JIT
2299   |  mov SAVE_PC, PC
2300   |  or PC, 1                           // Marker for hot call.
2301   |1:
2302   |.endif
2303   |  lea RD, [BASE+NARGS:RD*8-8]
2304   |  mov L:RB, SAVE_L
2305   |  mov L:RB->base, BASE
2306   |  mov L:RB->top, RD
2307   |  mov CARG2, PC
2308   |  mov CARG1, L:RB
2309   |  call extern lj_dispatch_call       // (lua_State *L, const BCIns *pc)
2310   |  // ASMFunction returned in eax/rax (RD).
2311   |  mov SAVE_PC, 0                     // Invalidate for subsequent line hook.
2312   |.if JIT
2313   |  and PC, -2
2314   |.endif
2315   |  mov BASE, L:RB->base
2316   |  mov RA, RD
2317   |  mov RD, L:RB->top
2318   |  sub RD, BASE
2319   |  mov RB, RA
2320   |  movzx RAd, PC_RA
2321   |  shr RDd, 3
2322   |  add NARGS:RDd, 1
2323   |  jmp RB
2324   |
2325   |->cont_stitch:                       // Trace stitching.
2326   |.if JIT
2327   |  // BASE = base, RC = result, RB = mbase
2328   |  mov ITYPEd, [RB-24]                // Save previous trace number.
2329   |  mov TMPRd, MULTRES
2330   |  movzx RAd, PC_RA
2331   |  lea RA, [BASE+RA*8]                // Call base.
2332   |  sub TMPRd, 1
2333   |  jz >2
2334   |1:  // Move results down.
2335   |  mov RB, [RC]
2336   |  mov [RA], RB
2337   |  add RC, 8
2338   |  add RA, 8
2339   |  sub TMPRd, 1
2340   |  jnz <1
2341   |2:
2342   |  movzx RCd, PC_RA
2343   |  movzx RBd, PC_RB
2344   |  add RC, RB
2345   |  lea RC, [BASE+RC*8-8]
2346   |3:
2347   |  cmp RC, RA
2348   |  ja >9                              // More results wanted?
2349   |
2350   |  mov RA, [DISPATCH+DISPATCH_J(trace)]
2351   |  mov TRACE:RD, [RA+ITYPE*8]
2352   |  test TRACE:RD, TRACE:RD
2353   |  jz ->cont_nop
2354   |  movzx RDd, word TRACE:RD->link
2355   |  cmp RDd, RBd
2356   |  je ->cont_nop                      // Blacklisted.
2357   |  test RDd, RDd
2358   |  jne =>BC_JLOOP                     // Jump to stitched trace.
2359   |
2360   |  // Stitch a new trace to the previous trace.
2361   |  mov [DISPATCH+DISPATCH_J(exitno)], RB
2362   |  mov L:RB, SAVE_L
2363   |  mov L:RB->base, BASE
2364   |  mov CARG2, PC
2365   |  lea CARG1, [DISPATCH+GG_DISP2J]
2366   |  mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2367   |  call extern lj_dispatch_stitch     // (jit_State *J, const BCIns *pc)
2368   |  mov BASE, L:RB->base
2369   |  jmp ->cont_nop
2370   |
2371   |9:  // Fill up results with nil.
2372   |  mov aword [RA], LJ_TNIL
2373   |  add RA, 8
2374   |  jmp <3
2375   |.endif
2376   |
2377   |->vm_profhook:                       // Dispatch target for profiler hook.
2378 #if LJ_HASPROFILE
2379   |  mov L:RB, SAVE_L
2380   |  mov L:RB->base, BASE
2381   |  mov CARG2, PC                      // Caveat: CARG2 == BASE
2382   |  mov CARG1, L:RB
2383   |  call extern lj_dispatch_profile    // (lua_State *L, const BCIns *pc)
2384   |  mov BASE, L:RB->base
2385   |  // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2386   |  sub PC, 4
2387   |  jmp ->cont_nop
2388 #endif
2389   |
2390   |//-----------------------------------------------------------------------
2391   |//-- Trace exit handler -------------------------------------------------
2392   |//-----------------------------------------------------------------------
2393   |
2394   |// Called from an exit stub with the exit number on the stack.
2395   |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2396   |->vm_exit_handler:
2397   |.if JIT
2398   |  push r13; push r12
2399   |  push r11; push r10; push r9; push r8
2400   |  push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2401   |  push rbx; push rdx; push rcx; push rax
2402   |  movzx RCd, byte [rbp-8]            // Reconstruct exit number.
2403   |  mov RCH, byte [rbp-16]
2404   |  mov [rbp-8], r15; mov [rbp-16], r14
2405   |  // Caveat: DISPATCH is rbx.
2406   |  mov DISPATCH, [ebp]
2407   |  mov RA, [DISPATCH+DISPATCH_GL(vmstate)]    // Get trace number.
2408   |  set_vmstate EXIT
2409   |  mov [DISPATCH+DISPATCH_J(exitno)], RC
2410   |  mov [DISPATCH+DISPATCH_J(parent)], RA
2411   |.if X64WIN
2412   |  sub rsp, 16*8+4*8                  // Room for SSE regs + save area.
2413   |.else
2414   |  sub rsp, 16*8                      // Room for SSE regs.
2415   |.endif
2416   |  add rbp, -128
2417   |  movsd qword [rbp-8],   xmm15; movsd qword [rbp-16],  xmm14
2418   |  movsd qword [rbp-24],  xmm13; movsd qword [rbp-32],  xmm12
2419   |  movsd qword [rbp-40],  xmm11; movsd qword [rbp-48],  xmm10
2420   |  movsd qword [rbp-56],  xmm9;  movsd qword [rbp-64],  xmm8
2421   |  movsd qword [rbp-72],  xmm7;  movsd qword [rbp-80],  xmm6
2422   |  movsd qword [rbp-88],  xmm5;  movsd qword [rbp-96],  xmm4
2423   |  movsd qword [rbp-104], xmm3;  movsd qword [rbp-112], xmm2
2424   |  movsd qword [rbp-120], xmm1;  movsd qword [rbp-128], xmm0
2425   |  // Caveat: RB is rbp.
2426   |  mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)]
2427   |  mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2428   |  mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2429   |  mov L:RB->base, BASE
2430   |.if X64WIN
2431   |  lea CARG2, [rsp+4*8]
2432   |.else
2433   |  mov CARG2, rsp
2434   |.endif
2435   |  lea CARG1, [DISPATCH+GG_DISP2J]
2436   |  mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2437   |  call extern lj_trace_exit          // (jit_State *J, ExitState *ex)
2438   |  // MULTRES or negated error code returned in eax (RD).
2439   |  mov RA, L:RB->cframe
2440   |  and RA, CFRAME_RAWMASK
2441   |  mov [RA+CFRAME_OFS_L], L:RB        // Set SAVE_L (on-trace resume/yield).
2442   |  mov BASE, L:RB->base
2443   |  mov PC, [RA+CFRAME_OFS_PC] // Get SAVE_PC.
2444   |  jmp >1
2445   |.endif
2446   |->vm_exit_interp:
2447   |  // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2448   |.if JIT
2449   |  // Restore additional callee-save registers only used in compiled code.
2450   |.if X64WIN
2451   |  lea RA, [rsp+10*16+4*8]
2452   |1:
2453   |  movdqa xmm15, [RA-10*16]
2454   |  movdqa xmm14, [RA-9*16]
2455   |  movdqa xmm13, [RA-8*16]
2456   |  movdqa xmm12, [RA-7*16]
2457   |  movdqa xmm11, [RA-6*16]
2458   |  movdqa xmm10, [RA-5*16]
2459   |  movdqa xmm9, [RA-4*16]
2460   |  movdqa xmm8, [RA-3*16]
2461   |  movdqa xmm7, [RA-2*16]
2462   |  mov rsp, RA                        // Reposition stack to C frame.
2463   |  movdqa xmm6, [RA-1*16]
2464   |  mov r15, CSAVE_1
2465   |  mov r14, CSAVE_2
2466   |  mov r13, CSAVE_3
2467   |  mov r12, CSAVE_4
2468   |.else
2469   |  lea RA, [rsp+16]
2470   |1:
2471   |  mov r13, [RA-8]
2472   |  mov r12, [RA]
2473   |  mov rsp, RA                        // Reposition stack to C frame.
2474   |.endif
2475   |  test RDd, RDd; js >9               // Check for error from exit.
2476   |  mov L:RB, SAVE_L
2477   |  mov MULTRES, RDd
2478   |  mov LFUNC:KBASE, [BASE-16]
2479   |  cleartp LFUNC:KBASE
2480   |  mov KBASE, LFUNC:KBASE->pc
2481   |  mov KBASE, [KBASE+PC2PROTO(k)]
2482   |  mov L:RB->base, BASE
2483   |  mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2484   |  set_vmstate INTERP
2485   |  // Modified copy of ins_next which handles function header dispatch, too.
2486   |  mov RCd, [PC]
2487   |  movzx RAd, RCH
2488   |  movzx OP, RCL
2489   |  add PC, 4
2490   |  shr RCd, 16
2491   |  cmp OP, BC_FUNCF                   // Function header?
2492   |  jb >3
2493   |  cmp OP, BC_FUNCC+2                 // Fast function?
2494   |  jae >4
2495   |2:
2496   |  mov RCd, MULTRES                   // RC/RD holds nres+1.
2497   |3:
2498   |  jmp aword [DISPATCH+OP*8]
2499   |
2500   |4:  // Check frame below fast function.
2501   |  mov RC, [BASE-8]
2502   |  test RCd, FRAME_TYPE
2503   |  jnz <2                             // Trace stitching continuation?
2504   |  // Otherwise set KBASE for Lua function below fast function.
2505   |  movzx RCd, byte [RC-3]
2506   |  neg RC
2507   |  mov LFUNC:KBASE, [BASE+RC*8-24]
2508   |  cleartp LFUNC:KBASE
2509   |  mov KBASE, LFUNC:KBASE->pc
2510   |  mov KBASE, [KBASE+PC2PROTO(k)]
2511   |  jmp <2
2512   |
2513   |9:  // Rethrow error from the right C frame.
2514   |  neg RD
2515   |  mov CARG1, L:RB
2516   |  mov CARG2, RD
2517   |  call extern lj_err_throw           // (lua_State *L, int errcode)
2518   |.endif
2519   |
2520   |//-----------------------------------------------------------------------
2521   |//-- Math helper functions ----------------------------------------------
2522   |//-----------------------------------------------------------------------
2523   |
2524   |// FP value rounding. Called by math.floor/math.ceil fast functions
2525   |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
2526   |.macro vm_round, name, mode, cond
2527   |->name:
2528   |->name .. _sse:
2529   |  sseconst_abs xmm2, RD
2530   |  sseconst_2p52 xmm3, RD
2531   |  movaps xmm1, xmm0
2532   |  andpd xmm1, xmm2                   // |x|
2533   |  ucomisd xmm3, xmm1                 // No truncation if 2^52 <= |x|.
2534   |  jbe >1
2535   |  andnpd xmm2, xmm0                  // Isolate sign bit.
2536   |.if mode == 2                // trunc(x)?
2537   |  movaps xmm0, xmm1
2538   |  addsd xmm1, xmm3                   // (|x| + 2^52) - 2^52
2539   |  subsd xmm1, xmm3
2540   |  sseconst_1 xmm3, RD
2541   |  cmpsd xmm0, xmm1, 1                // |x| < result?
2542   |  andpd xmm0, xmm3
2543   |  subsd xmm1, xmm0                   // If yes, subtract -1.
2544   |  orpd xmm1, xmm2                    // Merge sign bit back in.
2545   |.else
2546   |  addsd xmm1, xmm3                   // (|x| + 2^52) - 2^52
2547   |  subsd xmm1, xmm3
2548   |  orpd xmm1, xmm2                    // Merge sign bit back in.
2549   |  .if mode == 1              // ceil(x)?
2550   |    sseconst_m1 xmm2, RD             // Must subtract -1 to preserve -0.
2551   |    cmpsd xmm0, xmm1, 6              // x > result?
2552   |  .else                      // floor(x)?
2553   |    sseconst_1 xmm2, RD
2554   |    cmpsd xmm0, xmm1, 1              // x < result?
2555   |  .endif
2556   |  andpd xmm0, xmm2
2557   |  subsd xmm1, xmm0                   // If yes, subtract +-1.
2558   |.endif
2559   |  movaps xmm0, xmm1
2560   |1:
2561   |  ret
2562   |.endmacro
2563   |
2564   |  vm_round vm_floor, 0, 1
2565   |  vm_round vm_ceil,  1, JIT
2566   |  vm_round vm_trunc, 2, JIT
2567   |
2568   |// FP modulo x%y. Called by BC_MOD* and vm_arith.
2569   |->vm_mod:
2570   |// Args in xmm0/xmm1, return value in xmm0.
2571   |// Caveat: xmm0-xmm5 and RC (eax) modified!
2572   |  movaps xmm5, xmm0
2573   |  divsd xmm0, xmm1
2574   |  sseconst_abs xmm2, RD
2575   |  sseconst_2p52 xmm3, RD
2576   |  movaps xmm4, xmm0
2577   |  andpd xmm4, xmm2                   // |x/y|
2578   |  ucomisd xmm3, xmm4                 // No truncation if 2^52 <= |x/y|.
2579   |  jbe >1
2580   |  andnpd xmm2, xmm0                  // Isolate sign bit.
2581   |  addsd xmm4, xmm3                   // (|x/y| + 2^52) - 2^52
2582   |  subsd xmm4, xmm3
2583   |  orpd xmm4, xmm2                    // Merge sign bit back in.
2584   |  sseconst_1 xmm2, RD
2585   |  cmpsd xmm0, xmm4, 1                // x/y < result?
2586   |  andpd xmm0, xmm2
2587   |  subsd xmm4, xmm0                   // If yes, subtract 1.0.
2588   |  movaps xmm0, xmm5
2589   |  mulsd xmm1, xmm4
2590   |  subsd xmm0, xmm1
2591   |  ret
2592   |1:
2593   |  mulsd xmm1, xmm0
2594   |  movaps xmm0, xmm5
2595   |  subsd xmm0, xmm1
2596   |  ret
2597   |
2598   |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
2599   |->vm_powi_sse:
2600   |  cmp eax, 1; jle >6                 // i<=1?
2601   |  // Now 1 < (unsigned)i <= 0x80000000.
2602   |1:  // Handle leading zeros.
2603   |  test eax, 1; jnz >2
2604   |  mulsd xmm0, xmm0
2605   |  shr eax, 1
2606   |  jmp <1
2607   |2:
2608   |  shr eax, 1; jz >5
2609   |  movaps xmm1, xmm0
2610   |3:  // Handle trailing bits.
2611   |  mulsd xmm0, xmm0
2612   |  shr eax, 1; jz >4
2613   |  jnc <3
2614   |  mulsd xmm1, xmm0
2615   |  jmp <3
2616   |4:
2617   |  mulsd xmm0, xmm1
2618   |5:
2619   |  ret
2620   |6:
2621   |  je <5                              // x^1 ==> x
2622   |  jb >7                              // x^0 ==> 1
2623   |  neg eax
2624   |  call <1
2625   |  sseconst_1 xmm1, RD
2626   |  divsd xmm1, xmm0
2627   |  movaps xmm0, xmm1
2628   |  ret
2629   |7:
2630   |  sseconst_1 xmm0, RD
2631   |  ret
2632   |
2633   |//-----------------------------------------------------------------------
2634   |//-- Miscellaneous functions --------------------------------------------
2635   |//-----------------------------------------------------------------------
2636   |
2637   |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
2638   |->vm_cpuid:
2639   |  mov eax, CARG1d
2640   |  .if X64WIN; push rsi; mov rsi, CARG2; .endif
2641   |  push rbx
2642   |  cpuid
2643   |  mov [rsi], eax
2644   |  mov [rsi+4], ebx
2645   |  mov [rsi+8], ecx
2646   |  mov [rsi+12], edx
2647   |  pop rbx
2648   |  .if X64WIN; pop rsi; .endif
2649   |  ret
2650   |
2651   |//-----------------------------------------------------------------------
2652   |//-- Assertions ---------------------------------------------------------
2653   |//-----------------------------------------------------------------------
2654   |
2655   |->assert_bad_for_arg_type:
2656 #ifdef LUA_USE_ASSERT
2657   |  int3
2658 #endif
2659   |  int3
2660   |
2661   |//-----------------------------------------------------------------------
2662   |//-- FFI helper functions -----------------------------------------------
2663   |//-----------------------------------------------------------------------
2664   |
2665   |// Handler for callback functions. Callback slot number in ah/al.
2666   |->vm_ffi_callback:
2667   |.if FFI
2668   |.type CTSTATE, CTState, PC
2669   |  saveregs_  // ebp/rbp already saved. ebp now holds global_State *.
2670   |  lea DISPATCH, [ebp+GG_G2DISP]
2671   |  mov CTSTATE, GL:ebp->ctype_state
2672   |  movzx eax, ax
2673   |  mov CTSTATE->cb.slot, eax
2674   |  mov CTSTATE->cb.gpr[0], CARG1
2675   |  mov CTSTATE->cb.gpr[1], CARG2
2676   |  mov CTSTATE->cb.gpr[2], CARG3
2677   |  mov CTSTATE->cb.gpr[3], CARG4
2678   |  movsd qword CTSTATE->cb.fpr[0], xmm0
2679   |  movsd qword CTSTATE->cb.fpr[1], xmm1
2680   |  movsd qword CTSTATE->cb.fpr[2], xmm2
2681   |  movsd qword CTSTATE->cb.fpr[3], xmm3
2682   |.if X64WIN
2683   |  lea rax, [rsp+CFRAME_SIZE+4*8]
2684   |.else
2685   |  lea rax, [rsp+CFRAME_SIZE]
2686   |  mov CTSTATE->cb.gpr[4], CARG5
2687   |  mov CTSTATE->cb.gpr[5], CARG6
2688   |  movsd qword CTSTATE->cb.fpr[4], xmm4
2689   |  movsd qword CTSTATE->cb.fpr[5], xmm5
2690   |  movsd qword CTSTATE->cb.fpr[6], xmm6
2691   |  movsd qword CTSTATE->cb.fpr[7], xmm7
2692   |.endif
2693   |  mov CTSTATE->cb.stack, rax
2694   |  mov CARG2, rsp
2695   |  mov SAVE_PC, CTSTATE               // Any value outside of bytecode is ok.
2696   |  mov CARG1, CTSTATE
2697   |  call extern lj_ccallback_enter     // (CTState *cts, void *cf)
2698   |  // lua_State * returned in eax (RD).
2699   |  set_vmstate INTERP
2700   |  mov BASE, L:RD->base
2701   |  mov RD, L:RD->top
2702   |  sub RD, BASE
2703   |  mov LFUNC:RB, [BASE-16]
2704   |  cleartp LFUNC:RB
2705   |  shr RD, 3
2706   |  add RD, 1
2707   |  ins_callt
2708   |.endif
2709   |
2710   |->cont_ffi_callback:                 // Return from FFI callback.
2711   |.if FFI
2712   |  mov L:RA, SAVE_L
2713   |  mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
2714   |  mov aword CTSTATE->L, L:RA
2715   |  mov L:RA->base, BASE
2716   |  mov L:RA->top, RB
2717   |  mov CARG1, CTSTATE
2718   |  mov CARG2, RC
2719   |  call extern lj_ccallback_leave     // (CTState *cts, TValue *o)
2720   |  mov rax, CTSTATE->cb.gpr[0]
2721   |  movsd xmm0, qword CTSTATE->cb.fpr[0]
2722   |  jmp ->vm_leave_unw
2723   |.endif
2724   |
2725   |->vm_ffi_call:                       // Call C function via FFI.
2726   |  // Caveat: needs special frame unwinding, see below.
2727   |.if FFI
2728   |  .type CCSTATE, CCallState, rbx
2729   |  push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
2730   |
2731   |  // Readjust stack.
2732   |  mov eax, CCSTATE->spadj
2733   |  sub rsp, rax
2734   |
2735   |  // Copy stack slots.
2736   |  movzx ecx, byte CCSTATE->nsp
2737   |  sub ecx, 1
2738   |  js >2
2739   |1:
2740   |  mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
2741   |  mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
2742   |  sub ecx, 1
2743   |  jns <1
2744   |2:
2745   |
2746   |  movzx eax, byte CCSTATE->nfpr
2747   |  mov CARG1, CCSTATE->gpr[0]
2748   |  mov CARG2, CCSTATE->gpr[1]
2749   |  mov CARG3, CCSTATE->gpr[2]
2750   |  mov CARG4, CCSTATE->gpr[3]
2751   |.if not X64WIN
2752   |  mov CARG5, CCSTATE->gpr[4]
2753   |  mov CARG6, CCSTATE->gpr[5]
2754   |.endif
2755   |  test eax, eax; jz >5
2756   |  movaps xmm0, CCSTATE->fpr[0]
2757   |  movaps xmm1, CCSTATE->fpr[1]
2758   |  movaps xmm2, CCSTATE->fpr[2]
2759   |  movaps xmm3, CCSTATE->fpr[3]
2760   |.if not X64WIN
2761   |  cmp eax, 4; jbe >5
2762   |  movaps xmm4, CCSTATE->fpr[4]
2763   |  movaps xmm5, CCSTATE->fpr[5]
2764   |  movaps xmm6, CCSTATE->fpr[6]
2765   |  movaps xmm7, CCSTATE->fpr[7]
2766   |.endif
2767   |5:
2768   |
2769   |  call aword CCSTATE->func
2770   |
2771   |  mov CCSTATE->gpr[0], rax
2772   |  movaps CCSTATE->fpr[0], xmm0
2773   |.if not X64WIN
2774   |  mov CCSTATE->gpr[1], rdx
2775   |  movaps CCSTATE->fpr[1], xmm1
2776   |.endif
2777   |
2778   |  mov rbx, [rbp-8]; leave; ret
2779   |.endif
2780   |// Note: vm_ffi_call must be the last function in this object file!
2781   |
2782   |//-----------------------------------------------------------------------
2785 /* Generate the code for a single instruction. */
2786 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2788   int vk = 0;
2789   |// Note: aligning all instructions does not pay off.
2790   |=>defop:
2792   switch (op) {
2794   /* -- Comparison ops ---------------------------------------------------- */
2796   /* Remember: all ops branch for a true comparison, fall through otherwise. */
2798   |.macro jmp_comp, lt, ge, le, gt, target
2799   ||switch (op) {
2800   ||case BC_ISLT:
2801   |   lt target
2802   ||break;
2803   ||case BC_ISGE:
2804   |   ge target
2805   ||break;
2806   ||case BC_ISLE:
2807   |   le target
2808   ||break;
2809   ||case BC_ISGT:
2810   |   gt target
2811   ||break;
2812   ||default: break;  /* Shut up GCC. */
2813   ||}
2814   |.endmacro
2816   case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2817     |  // RA = src1, RD = src2, JMP with RD = target
2818     |  ins_AD
2819     |  mov ITYPE, [BASE+RA*8]
2820     |  mov RB, [BASE+RD*8]
2821     |  mov RA, ITYPE
2822     |  mov RD, RB
2823     |  sar ITYPE, 47
2824     |  sar RB, 47
2825     |.if DUALNUM
2826     |  cmp ITYPEd, LJ_TISNUM; jne >7
2827     |  cmp RBd, LJ_TISNUM; jne >8
2828     |  add PC, 4
2829     |  cmp RAd, RDd
2830     |  jmp_comp jge, jl, jg, jle, >9
2831     |6:
2832     |  movzx RDd, PC_RD
2833     |  branchPC RD
2834     |9:
2835     |  ins_next
2836     |
2837     |7:  // RA is not an integer.
2838     |  ja ->vmeta_comp
2839     |  // RA is a number.
2840     |  cmp RBd, LJ_TISNUM; jb >1; jne ->vmeta_comp
2841     |  // RA is a number, RD is an integer.
2842     |  cvtsi2sd xmm0, RDd
2843     |  jmp >2
2844     |
2845     |8:  // RA is an integer, RD is not an integer.
2846     |  ja ->vmeta_comp
2847     |  // RA is an integer, RD is a number.
2848     |  cvtsi2sd xmm1, RAd
2849     |  movd xmm0, RD
2850     |  jmp >3
2851     |.else
2852     |  cmp ITYPEd, LJ_TISNUM; jae ->vmeta_comp
2853     |  cmp RBd, LJ_TISNUM; jae ->vmeta_comp
2854     |.endif
2855     |1:
2856     |  movd xmm0, RD
2857     |2:
2858     |  movd xmm1, RA
2859     |3:
2860     |  add PC, 4
2861     |  ucomisd xmm0, xmm1
2862     |  // Unordered: all of ZF CF PF set, ordered: PF clear.
2863     |  // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2864     |.if DUALNUM
2865     |  jmp_comp jbe, ja, jb, jae, <9
2866     |  jmp <6
2867     |.else
2868     |  jmp_comp jbe, ja, jb, jae, >1
2869     |  movzx RDd, PC_RD
2870     |  branchPC RD
2871     |1:
2872     |  ins_next
2873     |.endif
2874     break;
2876   case BC_ISEQV: case BC_ISNEV:
2877     vk = op == BC_ISEQV;
2878     |  ins_AD   // RA = src1, RD = src2, JMP with RD = target
2879     |  mov RB, [BASE+RD*8]
2880     |  mov ITYPE, [BASE+RA*8]
2881     |  add PC, 4
2882     |  mov RD, RB
2883     |  mov RA, ITYPE
2884     |  sar RB, 47
2885     |  sar ITYPE, 47
2886     |.if DUALNUM
2887     |  cmp RBd, LJ_TISNUM; jne >7
2888     |  cmp ITYPEd, LJ_TISNUM; jne >8
2889     |  cmp RDd, RAd
2890     if (vk) {
2891       |  jne >9
2892     } else {
2893       |  je >9
2894     }
2895     |  movzx RDd, PC_RD
2896     |  branchPC RD
2897     |9:
2898     |  ins_next
2899     |
2900     |7:  // RD is not an integer.
2901     |  ja >5
2902     |  // RD is a number.
2903     |  movd xmm1, RD
2904     |  cmp ITYPEd, LJ_TISNUM; jb >1; jne >5
2905     |  // RD is a number, RA is an integer.
2906     |  cvtsi2sd xmm0, RAd
2907     |  jmp >2
2908     |
2909     |8:  // RD is an integer, RA is not an integer.
2910     |  ja >5
2911     |  // RD is an integer, RA is a number.
2912     |  cvtsi2sd xmm1, RDd
2913     |  jmp >1
2914     |
2915     |.else
2916     |  cmp RBd, LJ_TISNUM; jae >5
2917     |  cmp ITYPEd, LJ_TISNUM; jae >5
2918     |  movd xmm1, RD
2919     |.endif
2920     |1:
2921     |  movd xmm0, RA
2922     |2:
2923     |  ucomisd xmm0, xmm1
2924     |4:
2925   iseqne_fp:
2926     if (vk) {
2927       |  jp >2                          // Unordered means not equal.
2928       |  jne >2
2929     } else {
2930       |  jp >2                          // Unordered means not equal.
2931       |  je >1
2932     }
2933   iseqne_end:
2934     if (vk) {
2935       |1:                               // EQ: Branch to the target.
2936       |  movzx RDd, PC_RD
2937       |  branchPC RD
2938       |2:                               // NE: Fallthrough to next instruction.
2939       |.if not FFI
2940       |3:
2941       |.endif
2942     } else {
2943       |.if not FFI
2944       |3:
2945       |.endif
2946       |2:                               // NE: Branch to the target.
2947       |  movzx RDd, PC_RD
2948       |  branchPC RD
2949       |1:                               // EQ: Fallthrough to next instruction.
2950     }
2951     if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
2952                        op == BC_ISEQN || op == BC_ISNEN)) {
2953       |  jmp <9
2954     } else {
2955       |  ins_next
2956     }
2957     |
2958     if (op == BC_ISEQV || op == BC_ISNEV) {
2959       |5:  // Either or both types are not numbers.
2960       |.if FFI
2961       |  cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd
2962       |  cmp ITYPEd, LJ_TCDATA; je ->vmeta_equal_cd
2963       |.endif
2964       |  cmp RA, RD
2965       |  je <1                          // Same GCobjs or pvalues?
2966       |  cmp RBd, ITYPEd
2967       |  jne <2                         // Not the same type?
2968       |  cmp RBd, LJ_TISTABUD
2969       |  ja <2                          // Different objects and not table/ud?
2970       |
2971       |  // Different tables or userdatas. Need to check __eq metamethod.
2972       |  // Field metatable must be at same offset for GCtab and GCudata!
2973       |  cleartp TAB:RA
2974       |  mov TAB:RB, TAB:RA->metatable
2975       |  test TAB:RB, TAB:RB
2976       |  jz <2                          // No metatable?
2977       |  test byte TAB:RB->nomm, 1<<MM_eq
2978       |  jnz <2                         // Or 'no __eq' flag set?
2979       if (vk) {
2980         |  xor RBd, RBd                 // ne = 0
2981       } else {
2982         |  mov RBd, 1                   // ne = 1
2983       }
2984       |  jmp ->vmeta_equal              // Handle __eq metamethod.
2985     } else {
2986       |.if FFI
2987       |3:
2988       |  cmp ITYPEd, LJ_TCDATA
2989       if (LJ_DUALNUM && vk) {
2990         |  jne <9
2991       } else {
2992         |  jne <2
2993       }
2994       |  jmp ->vmeta_equal_cd
2995       |.endif
2996     }
2997     break;
2998   case BC_ISEQS: case BC_ISNES:
2999     vk = op == BC_ISEQS;
3000     |  ins_AND  // RA = src, RD = str const, JMP with RD = target
3001     |  mov RB, [BASE+RA*8]
3002     |  add PC, 4
3003     |  checkstr RB, >3
3004     |  cmp RB, [KBASE+RD*8]
3005   iseqne_test:
3006     if (vk) {
3007       |  jne >2
3008     } else {
3009       |  je >1
3010     }
3011     goto iseqne_end;
3012   case BC_ISEQN: case BC_ISNEN:
3013     vk = op == BC_ISEQN;
3014     |  ins_AD   // RA = src, RD = num const, JMP with RD = target
3015     |  mov RB, [BASE+RA*8]
3016     |  add PC, 4
3017     |.if DUALNUM
3018     |  checkint RB, >7
3019     |  mov RD, [KBASE+RD*8]
3020     |  checkint RD, >8
3021     |  cmp RBd, RDd
3022     if (vk) {
3023       |  jne >9
3024     } else {
3025       |  je >9
3026     }
3027     |  movzx RDd, PC_RD
3028     |  branchPC RD
3029     |9:
3030     |  ins_next
3031     |
3032     |7:  // RA is not an integer.
3033     |  ja >3
3034     |  // RA is a number.
3035     |  mov RD, [KBASE+RD*8]
3036     |  checkint RD, >1
3037     |  // RA is a number, RD is an integer.
3038     |  cvtsi2sd xmm0, RDd
3039     |  jmp >2
3040     |
3041     |8:  // RA is an integer, RD is a number.
3042     |  cvtsi2sd xmm0, RBd
3043     |  movd xmm1, RD
3044     |  ucomisd xmm0, xmm1
3045     |  jmp >4
3046     |1:
3047     |  movd xmm0, RD
3048     |.else
3049     |  checknum RB, >3
3050     |1:
3051     |  movsd xmm0, qword [KBASE+RD*8]
3052     |.endif
3053     |2:
3054     |  ucomisd xmm0, qword [BASE+RA*8]
3055     |4:
3056     goto iseqne_fp;
3057   case BC_ISEQP: case BC_ISNEP:
3058     vk = op == BC_ISEQP;
3059     |  ins_AND  // RA = src, RD = primitive type (~), JMP with RD = target
3060     |  mov RB, [BASE+RA*8]
3061     |  sar RB, 47
3062     |  add PC, 4
3063     |  cmp RBd, RDd
3064     if (!LJ_HASFFI) goto iseqne_test;
3065     if (vk) {
3066       |  jne >3
3067       |  movzx RDd, PC_RD
3068       |  branchPC RD
3069       |2:
3070       |  ins_next
3071       |3:
3072       |  cmp RBd, LJ_TCDATA; jne <2
3073       |  jmp ->vmeta_equal_cd
3074     } else {
3075       |  je >2
3076       |  cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd
3077       |  movzx RDd, PC_RD
3078       |  branchPC RD
3079       |2:
3080       |  ins_next
3081     }
3082     break;
3084   /* -- Unary test and copy ops ------------------------------------------- */
3086   case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3087     |  ins_AD   // RA = dst or unused, RD = src, JMP with RD = target
3088     |  mov ITYPE, [BASE+RD*8]
3089     |  add PC, 4
3090     if (op == BC_ISTC || op == BC_ISFC) {
3091       |  mov RB, ITYPE
3092     }
3093     |  sar ITYPE, 47
3094     |  cmp ITYPEd, LJ_TISTRUECOND
3095     if (op == BC_IST || op == BC_ISTC) {
3096       |  jae >1
3097     } else {
3098       |  jb >1
3099     }
3100     if (op == BC_ISTC || op == BC_ISFC) {
3101       |  mov [BASE+RA*8], RB
3102     }
3103     |  movzx RDd, PC_RD
3104     |  branchPC RD
3105     |1:                                 // Fallthrough to the next instruction.
3106     |  ins_next
3107     break;
3109   case BC_ISTYPE:
3110     |  ins_AD   // RA = src, RD = -type
3111     |  mov RB, [BASE+RA*8]
3112     |  sar RB, 47
3113     |  add RBd, RDd
3114     |  jne ->vmeta_istype
3115     |  ins_next
3116     break;
3117   case BC_ISNUM:
3118     |  ins_AD   // RA = src, RD = -(TISNUM-1)
3119     |  checknumtp [BASE+RA*8], ->vmeta_istype
3120     |  ins_next
3121     break;
3123   /* -- Unary ops --------------------------------------------------------- */
3125   case BC_MOV:
3126     |  ins_AD   // RA = dst, RD = src
3127     |  mov RB, [BASE+RD*8]
3128     |  mov [BASE+RA*8], RB
3129     |  ins_next_
3130     break;
3131   case BC_NOT:
3132     |  ins_AD   // RA = dst, RD = src
3133     |  mov RB, [BASE+RD*8]
3134     |  sar RB, 47
3135     |  mov RCd, 2
3136     |  cmp RB, LJ_TISTRUECOND
3137     |  sbb RCd, 0
3138     |  shl RC, 47
3139     |  not RC
3140     |  mov [BASE+RA*8], RC
3141     |  ins_next
3142     break;
3143   case BC_UNM:
3144     |  ins_AD   // RA = dst, RD = src
3145     |  mov RB, [BASE+RD*8]
3146     |.if DUALNUM
3147     |  checkint RB, >5
3148     |  neg RBd
3149     |  jo >4
3150     |  setint RB
3151     |9:
3152     |  mov [BASE+RA*8], RB
3153     |  ins_next
3154     |4:
3155     |  mov64 RB, U64x(41e00000,00000000)  // 2^31.
3156     |  jmp <9
3157     |5:
3158     |  ja ->vmeta_unm
3159     |.else
3160     |  checknum RB, ->vmeta_unm
3161     |.endif
3162     |  mov64 RD, U64x(80000000,00000000)
3163     |  xor RB, RD
3164     |.if DUALNUM
3165     |  jmp <9
3166     |.else
3167     |  mov [BASE+RA*8], RB
3168     |  ins_next
3169     |.endif
3170     break;
3171   case BC_LEN:
3172     |  ins_AD   // RA = dst, RD = src
3173     |  mov RD, [BASE+RD*8]
3174     |  checkstr RD, >2
3175     |.if DUALNUM
3176     |  mov RDd, dword STR:RD->len
3177     |1:
3178     |  setint RD
3179     |  mov [BASE+RA*8], RD
3180     |.else
3181     |  xorps xmm0, xmm0
3182     |  cvtsi2sd xmm0, dword STR:RD->len
3183     |1:
3184     |  movsd qword [BASE+RA*8], xmm0
3185     |.endif
3186     |  ins_next
3187     |2:
3188     |  cmp ITYPEd, LJ_TTAB; jne ->vmeta_len
3189     |  mov TAB:CARG1, TAB:RD
3190 #if LJ_52
3191     |  mov TAB:RB, TAB:RD->metatable
3192     |  cmp TAB:RB, 0
3193     |  jnz >9
3194     |3:
3195 #endif
3196     |->BC_LEN_Z:
3197     |  mov RB, BASE                     // Save BASE.
3198     |  call extern lj_tab_len           // (GCtab *t)
3199     |  // Length of table returned in eax (RD).
3200     |.if DUALNUM
3201     |  // Nothing to do.
3202     |.else
3203     |  cvtsi2sd xmm0, RDd
3204     |.endif
3205     |  mov BASE, RB                     // Restore BASE.
3206     |  movzx RAd, PC_RA
3207     |  jmp <1
3208 #if LJ_52
3209     |9:  // Check for __len.
3210     |  test byte TAB:RB->nomm, 1<<MM_len
3211     |  jnz <3
3212     |  jmp ->vmeta_len                  // 'no __len' flag NOT set: check.
3213 #endif
3214     break;
3216   /* -- Binary ops -------------------------------------------------------- */
3218     |.macro ins_arithpre, sseins, ssereg
3219     |  ins_ABC
3220     ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3221     ||switch (vk) {
3222     ||case 0:
3223     |   checknumtp [BASE+RB*8], ->vmeta_arith_vn
3224     |   .if DUALNUM
3225     |     checknumtp [KBASE+RC*8], ->vmeta_arith_vn
3226     |   .endif
3227     |   movsd xmm0, qword [BASE+RB*8]
3228     |   sseins ssereg, qword [KBASE+RC*8]
3229     ||  break;
3230     ||case 1:
3231     |   checknumtp [BASE+RB*8], ->vmeta_arith_nv
3232     |   .if DUALNUM
3233     |     checknumtp [KBASE+RC*8], ->vmeta_arith_nv
3234     |   .endif
3235     |   movsd xmm0, qword [KBASE+RC*8]
3236     |   sseins ssereg, qword [BASE+RB*8]
3237     ||  break;
3238     ||default:
3239     |   checknumtp [BASE+RB*8], ->vmeta_arith_vv
3240     |   checknumtp [BASE+RC*8], ->vmeta_arith_vv
3241     |   movsd xmm0, qword [BASE+RB*8]
3242     |   sseins ssereg, qword [BASE+RC*8]
3243     ||  break;
3244     ||}
3245     |.endmacro
3246     |
3247     |.macro ins_arithdn, intins
3248     |  ins_ABC
3249     ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3250     ||switch (vk) {
3251     ||case 0:
3252     |   mov RB, [BASE+RB*8]
3253     |   mov RC, [KBASE+RC*8]
3254     |   checkint RB, ->vmeta_arith_vno
3255     |   checkint RC, ->vmeta_arith_vno
3256     |   intins RBd, RCd; jo ->vmeta_arith_vno
3257     ||  break;
3258     ||case 1:
3259     |   mov RB, [BASE+RB*8]
3260     |   mov RC, [KBASE+RC*8]
3261     |   checkint RB, ->vmeta_arith_nvo
3262     |   checkint RC, ->vmeta_arith_nvo
3263     |   intins RCd, RBd; jo ->vmeta_arith_nvo
3264     ||  break;
3265     ||default:
3266     |   mov RB, [BASE+RB*8]
3267     |   mov RC, [BASE+RC*8]
3268     |   checkint RB, ->vmeta_arith_vvo
3269     |   checkint RC, ->vmeta_arith_vvo
3270     |   intins RBd, RCd; jo ->vmeta_arith_vvo
3271     ||  break;
3272     ||}
3273     ||if (vk == 1) {
3274     |   setint RC
3275     |   mov [BASE+RA*8], RC
3276     ||} else {
3277     |   setint RB
3278     |   mov [BASE+RA*8], RB
3279     ||}
3280     |  ins_next
3281     |.endmacro
3282     |
3283     |.macro ins_arithpost
3284     |  movsd qword [BASE+RA*8], xmm0
3285     |.endmacro
3286     |
3287     |.macro ins_arith, sseins
3288     |  ins_arithpre sseins, xmm0
3289     |  ins_arithpost
3290     |  ins_next
3291     |.endmacro
3292     |
3293     |.macro ins_arith, intins, sseins
3294     |.if DUALNUM
3295     |  ins_arithdn intins
3296     |.else
3297     |  ins_arith, sseins
3298     |.endif
3299     |.endmacro
3301     |  // RA = dst, RB = src1 or num const, RC = src2 or num const
3302   case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3303     |  ins_arith add, addsd
3304     break;
3305   case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3306     |  ins_arith sub, subsd
3307     break;
3308   case BC_MULVN: case BC_MULNV: case BC_MULVV:
3309     |  ins_arith imul, mulsd
3310     break;
3311   case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3312     |  ins_arith divsd
3313     break;
3314   case BC_MODVN:
3315     |  ins_arithpre movsd, xmm1
3316     |->BC_MODVN_Z:
3317     |  call ->vm_mod
3318     |  ins_arithpost
3319     |  ins_next
3320     break;
3321   case BC_MODNV: case BC_MODVV:
3322     |  ins_arithpre movsd, xmm1
3323     |  jmp ->BC_MODVN_Z                 // Avoid 3 copies. It's slow anyway.
3324     break;
3325   case BC_POW:
3326     |  ins_arithpre movsd, xmm1
3327     |  mov RB, BASE
3328     |  call extern pow
3329     |  movzx RAd, PC_RA
3330     |  mov BASE, RB
3331     |  ins_arithpost
3332     |  ins_next
3333     break;
3335   case BC_CAT:
3336     |  ins_ABC  // RA = dst, RB = src_start, RC = src_end
3337     |  mov L:CARG1, SAVE_L
3338     |  mov L:CARG1->base, BASE
3339     |  lea CARG2, [BASE+RC*8]
3340     |  mov CARG3d, RCd
3341     |  sub CARG3d, RBd
3342     |->BC_CAT_Z:
3343     |  mov L:RB, L:CARG1
3344     |  mov SAVE_PC, PC
3345     |  call extern lj_meta_cat          // (lua_State *L, TValue *top, int left)
3346     |  // NULL (finished) or TValue * (metamethod) returned in eax (RC).
3347     |  mov BASE, L:RB->base
3348     |  test RC, RC
3349     |  jnz ->vmeta_binop
3350     |  movzx RBd, PC_RB                 // Copy result to Stk[RA] from Stk[RB].
3351     |  movzx RAd, PC_RA
3352     |  mov RC, [BASE+RB*8]
3353     |  mov [BASE+RA*8], RC
3354     |  ins_next
3355     break;
3357   /* -- Constant ops ------------------------------------------------------ */
3359   case BC_KSTR:
3360     |  ins_AND  // RA = dst, RD = str const (~)
3361     |  mov RD, [KBASE+RD*8]
3362     |  settp RD, LJ_TSTR
3363     |  mov [BASE+RA*8], RD
3364     |  ins_next
3365     break;
3366   case BC_KCDATA:
3367     |.if FFI
3368     |  ins_AND  // RA = dst, RD = cdata const (~)
3369     |  mov RD, [KBASE+RD*8]
3370     |  settp RD, LJ_TCDATA
3371     |  mov [BASE+RA*8], RD
3372     |  ins_next
3373     |.endif
3374     break;
3375   case BC_KSHORT:
3376     |  ins_AD   // RA = dst, RD = signed int16 literal
3377     |.if DUALNUM
3378     |  movsx RDd, RDW
3379     |  setint RD
3380     |  mov [BASE+RA*8], RD
3381     |.else
3382     |  movsx RDd, RDW                   // Sign-extend literal.
3383     |  cvtsi2sd xmm0, RDd
3384     |  movsd qword [BASE+RA*8], xmm0
3385     |.endif
3386     |  ins_next
3387     break;
3388   case BC_KNUM:
3389     |  ins_AD   // RA = dst, RD = num const
3390     |  movsd xmm0, qword [KBASE+RD*8]
3391     |  movsd qword [BASE+RA*8], xmm0
3392     |  ins_next
3393     break;
3394   case BC_KPRI:
3395     |  ins_AD   // RA = dst, RD = primitive type (~)
3396     |  shl RD, 47
3397     |  not RD
3398     |  mov [BASE+RA*8], RD
3399     |  ins_next
3400     break;
3401   case BC_KNIL:
3402     |  ins_AD   // RA = dst_start, RD = dst_end
3403     |  lea RA, [BASE+RA*8+8]
3404     |  lea RD, [BASE+RD*8]
3405     |  mov RB, LJ_TNIL
3406     |  mov [RA-8], RB                   // Sets minimum 2 slots.
3407     |1:
3408     |  mov [RA], RB
3409     |  add RA, 8
3410     |  cmp RA, RD
3411     |  jbe <1
3412     |  ins_next
3413     break;
3415   /* -- Upvalue and function ops ------------------------------------------ */
3417   case BC_UGET:
3418     |  ins_AD   // RA = dst, RD = upvalue #
3419     |  mov LFUNC:RB, [BASE-16]
3420     |  cleartp LFUNC:RB
3421     |  mov UPVAL:RB, [LFUNC:RB+RD*8+offsetof(GCfuncL, uvptr)]
3422     |  mov RB, UPVAL:RB->v
3423     |  mov RD, [RB]
3424     |  mov [BASE+RA*8], RD
3425     |  ins_next
3426     break;
3427   case BC_USETV:
3428 #define TV2MARKOFS \
3429  ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
3430     |  ins_AD   // RA = upvalue #, RD = src
3431     |  mov LFUNC:RB, [BASE-16]
3432     |  cleartp LFUNC:RB
3433     |  mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3434     |  cmp byte UPVAL:RB->closed, 0
3435     |  mov RB, UPVAL:RB->v
3436     |  mov RA, [BASE+RD*8]
3437     |  mov [RB], RA
3438     |  jz >1
3439     |  // Check barrier for closed upvalue.
3440     |  test byte [RB+TV2MARKOFS], LJ_GC_BLACK           // isblack(uv)
3441     |  jnz >2
3442     |1:
3443     |  ins_next
3444     |
3445     |2:  // Upvalue is black. Check if new value is collectable and white.
3446     |  mov RD, RA
3447     |  sar RD, 47
3448     |  sub RDd, LJ_TISGCV
3449     |  cmp RDd, LJ_TNUMX - LJ_TISGCV                    // tvisgcv(v)
3450     |  jbe <1
3451     |  cleartp GCOBJ:RA
3452     |  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES     // iswhite(v)
3453     |  jz <1
3454     |  // Crossed a write barrier. Move the barrier forward.
3455     |.if not X64WIN
3456     |  mov CARG2, RB
3457     |  mov RB, BASE                     // Save BASE.
3458     |.else
3459     |  xchg CARG2, RB                   // Save BASE (CARG2 == BASE).
3460     |.endif
3461     |  lea GL:CARG1, [DISPATCH+GG_DISP2G]
3462     |  call extern lj_gc_barrieruv      // (global_State *g, TValue *tv)
3463     |  mov BASE, RB                     // Restore BASE.
3464     |  jmp <1
3465     break;
3466 #undef TV2MARKOFS
3467   case BC_USETS:
3468     |  ins_AND  // RA = upvalue #, RD = str const (~)
3469     |  mov LFUNC:RB, [BASE-16]
3470     |  cleartp LFUNC:RB
3471     |  mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3472     |  mov STR:RA, [KBASE+RD*8]
3473     |  mov RD, UPVAL:RB->v
3474     |  settp STR:ITYPE, STR:RA, LJ_TSTR
3475     |  mov [RD], STR:ITYPE
3476     |  test byte UPVAL:RB->marked, LJ_GC_BLACK          // isblack(uv)
3477     |  jnz >2
3478     |1:
3479     |  ins_next
3480     |
3481     |2:  // Check if string is white and ensure upvalue is closed.
3482     |  test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES     // iswhite(str)
3483     |  jz <1
3484     |  cmp byte UPVAL:RB->closed, 0
3485     |  jz <1
3486     |  // Crossed a write barrier. Move the barrier forward.
3487     |  mov RB, BASE                     // Save BASE (CARG2 == BASE).
3488     |  mov CARG2, RD
3489     |  lea GL:CARG1, [DISPATCH+GG_DISP2G]
3490     |  call extern lj_gc_barrieruv      // (global_State *g, TValue *tv)
3491     |  mov BASE, RB                     // Restore BASE.
3492     |  jmp <1
3493     break;
3494   case BC_USETN:
3495     |  ins_AD   // RA = upvalue #, RD = num const
3496     |  mov LFUNC:RB, [BASE-16]
3497     |  cleartp LFUNC:RB
3498     |  movsd xmm0, qword [KBASE+RD*8]
3499     |  mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3500     |  mov RA, UPVAL:RB->v
3501     |  movsd qword [RA], xmm0
3502     |  ins_next
3503     break;
3504   case BC_USETP:
3505     |  ins_AD   // RA = upvalue #, RD = primitive type (~)
3506     |  mov LFUNC:RB, [BASE-16]
3507     |  cleartp LFUNC:RB
3508     |  mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3509     |  shl RD, 47
3510     |  not RD
3511     |  mov RA, UPVAL:RB->v
3512     |  mov [RA], RD
3513     |  ins_next
3514     break;
3515   case BC_UCLO:
3516     |  ins_AD   // RA = level, RD = target
3517     |  branchPC RD                      // Do this first to free RD.
3518     |  mov L:RB, SAVE_L
3519     |  cmp dword L:RB->openupval, 0
3520     |  je >1
3521     |  mov L:RB->base, BASE
3522     |  lea CARG2, [BASE+RA*8]           // Caveat: CARG2 == BASE
3523     |  mov L:CARG1, L:RB                // Caveat: CARG1 == RA
3524     |  call extern lj_func_closeuv      // (lua_State *L, TValue *level)
3525     |  mov BASE, L:RB->base
3526     |1:
3527     |  ins_next
3528     break;
3530   case BC_FNEW:
3531     |  ins_AND  // RA = dst, RD = proto const (~) (holding function prototype)
3532     |  mov L:RB, SAVE_L
3533     |  mov L:RB->base, BASE             // Caveat: CARG2/CARG3 may be BASE.
3534     |  mov CARG3, [BASE-16]
3535     |  cleartp CARG3
3536     |  mov CARG2, [KBASE+RD*8]          // Fetch GCproto *.
3537     |  mov CARG1, L:RB
3538     |  mov SAVE_PC, PC
3539     |  // (lua_State *L, GCproto *pt, GCfuncL *parent)
3540     |  call extern lj_func_newL_gc
3541     |  // GCfuncL * returned in eax (RC).
3542     |  mov BASE, L:RB->base
3543     |  movzx RAd, PC_RA
3544     |  settp LFUNC:RC, LJ_TFUNC
3545     |  mov [BASE+RA*8], LFUNC:RC
3546     |  ins_next
3547     break;
3549   /* -- Table ops --------------------------------------------------------- */
3551   case BC_TNEW:
3552     |  ins_AD   // RA = dst, RD = hbits|asize
3553     |  mov L:RB, SAVE_L
3554     |  mov L:RB->base, BASE
3555     |  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
3556     |  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
3557     |  mov SAVE_PC, PC
3558     |  jae >5
3559     |1:
3560     |  mov CARG3d, RDd
3561     |  and RDd, 0x7ff
3562     |  shr CARG3d, 11
3563     |  cmp RDd, 0x7ff
3564     |  je >3
3565     |2:
3566     |  mov L:CARG1, L:RB
3567     |  mov CARG2d, RDd
3568     |  call extern lj_tab_new  // (lua_State *L, int32_t asize, uint32_t hbits)
3569     |  // Table * returned in eax (RC).
3570     |  mov BASE, L:RB->base
3571     |  movzx RAd, PC_RA
3572     |  settp TAB:RC, LJ_TTAB
3573     |  mov [BASE+RA*8], TAB:RC
3574     |  ins_next
3575     |3:  // Turn 0x7ff into 0x801.
3576     |  mov RDd, 0x801
3577     |  jmp <2
3578     |5:
3579     |  mov L:CARG1, L:RB
3580     |  call extern lj_gc_step_fixtop    // (lua_State *L)
3581     |  movzx RDd, PC_RD
3582     |  jmp <1
3583     break;
3584   case BC_TDUP:
3585     |  ins_AND  // RA = dst, RD = table const (~) (holding template table)
3586     |  mov L:RB, SAVE_L
3587     |  mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
3588     |  mov SAVE_PC, PC
3589     |  cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
3590     |  mov L:RB->base, BASE
3591     |  jae >3
3592     |2:
3593     |  mov TAB:CARG2, [KBASE+RD*8]      // Caveat: CARG2 == BASE
3594     |  mov L:CARG1, L:RB                // Caveat: CARG1 == RA
3595     |  call extern lj_tab_dup           // (lua_State *L, Table *kt)
3596     |  // Table * returned in eax (RC).
3597     |  mov BASE, L:RB->base
3598     |  movzx RAd, PC_RA
3599     |  settp TAB:RC, LJ_TTAB
3600     |  mov [BASE+RA*8], TAB:RC
3601     |  ins_next
3602     |3:
3603     |  mov L:CARG1, L:RB
3604     |  call extern lj_gc_step_fixtop    // (lua_State *L)
3605     |  movzx RDd, PC_RD                 // Need to reload RD.
3606     |  not RD
3607     |  jmp <2
3608     break;
3610   case BC_GGET:
3611     |  ins_AND  // RA = dst, RD = str const (~)
3612     |  mov LFUNC:RB, [BASE-16]
3613     |  cleartp LFUNC:RB
3614     |  mov TAB:RB, LFUNC:RB->env
3615     |  mov STR:RC, [KBASE+RD*8]
3616     |  jmp ->BC_TGETS_Z
3617     break;
3618   case BC_GSET:
3619     |  ins_AND  // RA = src, RD = str const (~)
3620     |  mov LFUNC:RB, [BASE-16]
3621     |  cleartp LFUNC:RB
3622     |  mov TAB:RB, LFUNC:RB->env
3623     |  mov STR:RC, [KBASE+RD*8]
3624     |  jmp ->BC_TSETS_Z
3625     break;
3627   case BC_TGETV:
3628     |  ins_ABC  // RA = dst, RB = table, RC = key
3629     |  mov TAB:RB, [BASE+RB*8]
3630     |  mov RC, [BASE+RC*8]
3631     |  checktab TAB:RB, ->vmeta_tgetv
3632     |
3633     |  // Integer key?
3634     |.if DUALNUM
3635     |  checkint RC, >5
3636     |.else
3637     |  // Convert number to int and back and compare.
3638     |  checknum RC, >5
3639     |  movd xmm0, RC
3640     |  cvttsd2si RCd, xmm0
3641     |  cvtsi2sd xmm1, RCd
3642     |  ucomisd xmm0, xmm1
3643     |  jne ->vmeta_tgetv                // Generic numeric key? Use fallback.
3644     |.endif
3645     |  cmp RCd, TAB:RB->asize           // Takes care of unordered, too.
3646     |  jae ->vmeta_tgetv                // Not in array part? Use fallback.
3647     |  shl RCd, 3
3648     |  add RC, TAB:RB->array
3649     |  // Get array slot.
3650     |  mov ITYPE, [RC]
3651     |  cmp ITYPE, LJ_TNIL               // Avoid overwriting RB in fastpath.
3652     |  je >2
3653     |1:
3654     |  mov [BASE+RA*8], ITYPE
3655     |  ins_next
3656     |
3657     |2:  // Check for __index if table value is nil.
3658     |  mov TAB:TMPR, TAB:RB->metatable
3659     |  test TAB:TMPR, TAB:TMPR
3660     |  jz <1
3661     |  test byte TAB:TMPR->nomm, 1<<MM_index
3662     |  jz ->vmeta_tgetv                 // 'no __index' flag NOT set: check.
3663     |  jmp <1
3664     |
3665     |5:  // String key?
3666     |  cmp ITYPEd, LJ_TSTR; jne ->vmeta_tgetv
3667     |  cleartp STR:RC
3668     |  jmp ->BC_TGETS_Z
3669     break;
3670   case BC_TGETS:
3671     |  ins_ABC  // RA = dst, RB = table, RC = str const (~)
3672     |  mov TAB:RB, [BASE+RB*8]
3673     |  not RC
3674     |  mov STR:RC, [KBASE+RC*8]
3675     |  checktab TAB:RB, ->vmeta_tgets
3676     |->BC_TGETS_Z:      // RB = GCtab *, RC = GCstr *
3677     |  mov TMPRd, TAB:RB->hmask
3678     |  and TMPRd, STR:RC->hash
3679     |  imul TMPRd, #NODE
3680     |  add NODE:TMPR, TAB:RB->node
3681     |  settp ITYPE, STR:RC, LJ_TSTR
3682     |1:
3683     |  cmp NODE:TMPR->key, ITYPE
3684     |  jne >4
3685     |  // Get node value.
3686     |  mov ITYPE, NODE:TMPR->val
3687     |  cmp ITYPE, LJ_TNIL
3688     |  je >5                            // Key found, but nil value?
3689     |2:
3690     |  mov [BASE+RA*8], ITYPE
3691     |  ins_next
3692     |
3693     |4:  // Follow hash chain.
3694     |  mov NODE:TMPR, NODE:TMPR->next
3695     |  test NODE:TMPR, NODE:TMPR
3696     |  jnz <1
3697     |  // End of hash chain: key not found, nil result.
3698     |  mov ITYPE, LJ_TNIL
3699     |
3700     |5:  // Check for __index if table value is nil.
3701     |  mov TAB:TMPR, TAB:RB->metatable
3702     |  test TAB:TMPR, TAB:TMPR
3703     |  jz <2                            // No metatable: done.
3704     |  test byte TAB:TMPR->nomm, 1<<MM_index
3705     |  jnz <2                           // 'no __index' flag set: done.
3706     |  jmp ->vmeta_tgets                // Caveat: preserve STR:RC.
3707     break;
3708   case BC_TGETB:
3709     |  ins_ABC  // RA = dst, RB = table, RC = byte literal
3710     |  mov TAB:RB, [BASE+RB*8]
3711     |  checktab TAB:RB, ->vmeta_tgetb
3712     |  cmp RCd, TAB:RB->asize
3713     |  jae ->vmeta_tgetb
3714     |  shl RCd, 3
3715     |  add RC, TAB:RB->array
3716     |  // Get array slot.
3717     |  mov ITYPE, [RC]
3718     |  cmp ITYPE, LJ_TNIL
3719     |  je >2
3720     |1:
3721     |  mov [BASE+RA*8], ITYPE
3722     |  ins_next
3723     |
3724     |2:  // Check for __index if table value is nil.
3725     |  mov TAB:TMPR, TAB:RB->metatable
3726     |  test TAB:TMPR, TAB:TMPR
3727     |  jz <1
3728     |  test byte TAB:TMPR->nomm, 1<<MM_index
3729     |  jz ->vmeta_tgetb                 // 'no __index' flag NOT set: check.
3730     |  jmp <1
3731     break;
3732   case BC_TGETR:
3733     |  ins_ABC  // RA = dst, RB = table, RC = key
3734     |  mov TAB:RB, [BASE+RB*8]
3735     |  cleartp TAB:RB
3736     |.if DUALNUM
3737     |  mov RCd, dword [BASE+RC*8]
3738     |.else
3739     |  cvttsd2si RCd, qword [BASE+RC*8]
3740     |.endif
3741     |  cmp RCd, TAB:RB->asize
3742     |  jae ->vmeta_tgetr                // Not in array part? Use fallback.
3743     |  shl RCd, 3
3744     |  add RC, TAB:RB->array
3745     |  // Get array slot.
3746     |->BC_TGETR_Z:
3747     |  mov ITYPE, [RC]
3748     |->BC_TGETR2_Z:
3749     |  mov [BASE+RA*8], ITYPE
3750     |  ins_next
3751     break;
3753   case BC_TSETV:
3754     |  ins_ABC  // RA = src, RB = table, RC = key
3755     |  mov TAB:RB, [BASE+RB*8]
3756     |  mov RC, [BASE+RC*8]
3757     |  checktab TAB:RB, ->vmeta_tsetv
3758     |
3759     |  // Integer key?
3760     |.if DUALNUM
3761     |  checkint RC, >5
3762     |.else
3763     |  // Convert number to int and back and compare.
3764     |  checknum RC, >5
3765     |  movd xmm0, RC
3766     |  cvttsd2si RCd, xmm0
3767     |  cvtsi2sd xmm1, RCd
3768     |  ucomisd xmm0, xmm1
3769     |  jne ->vmeta_tsetv                // Generic numeric key? Use fallback.
3770     |.endif
3771     |  cmp RCd, TAB:RB->asize           // Takes care of unordered, too.
3772     |  jae ->vmeta_tsetv
3773     |  shl RCd, 3
3774     |  add RC, TAB:RB->array
3775     |  cmp aword [RC], LJ_TNIL
3776     |  je >3                            // Previous value is nil?
3777     |1:
3778     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
3779     |  jnz >7
3780     |2:  // Set array slot.
3781     |  mov RB, [BASE+RA*8]
3782     |  mov [RC], RB
3783     |  ins_next
3784     |
3785     |3:  // Check for __newindex if previous value is nil.
3786     |  mov TAB:TMPR, TAB:RB->metatable
3787     |  test TAB:TMPR, TAB:TMPR
3788     |  jz <1
3789     |  test byte TAB:TMPR->nomm, 1<<MM_newindex
3790     |  jz ->vmeta_tsetv                 // 'no __newindex' flag NOT set: check.
3791     |  jmp <1
3792     |
3793     |5:  // String key?
3794     |  cmp ITYPEd, LJ_TSTR; jne ->vmeta_tsetv
3795     |  cleartp STR:RC
3796     |  jmp ->BC_TSETS_Z
3797     |
3798     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3799     |  barrierback TAB:RB, TMPR
3800     |  jmp <2
3801     break;
3802   case BC_TSETS:
3803     |  ins_ABC  // RA = src, RB = table, RC = str const (~)
3804     |  mov TAB:RB, [BASE+RB*8]
3805     |  not RC
3806     |  mov STR:RC, [KBASE+RC*8]
3807     |  checktab TAB:RB, ->vmeta_tsets
3808     |->BC_TSETS_Z:      // RB = GCtab *, RC = GCstr *
3809     |  mov TMPRd, TAB:RB->hmask
3810     |  and TMPRd, STR:RC->hash
3811     |  imul TMPRd, #NODE
3812     |  mov byte TAB:RB->nomm, 0         // Clear metamethod cache.
3813     |  add NODE:TMPR, TAB:RB->node
3814     |  settp ITYPE, STR:RC, LJ_TSTR
3815     |1:
3816     |  cmp NODE:TMPR->key, ITYPE
3817     |  jne >5
3818     |  // Ok, key found. Assumes: offsetof(Node, val) == 0
3819     |  cmp aword [TMPR], LJ_TNIL
3820     |  je >4                            // Previous value is nil?
3821     |2:
3822     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
3823     |  jnz >7
3824     |3:  // Set node value.
3825     |  mov ITYPE, [BASE+RA*8]
3826     |  mov [TMPR], ITYPE
3827     |  ins_next
3828     |
3829     |4:  // Check for __newindex if previous value is nil.
3830     |  mov TAB:ITYPE, TAB:RB->metatable
3831     |  test TAB:ITYPE, TAB:ITYPE
3832     |  jz <2
3833     |  test byte TAB:ITYPE->nomm, 1<<MM_newindex
3834     |  jz ->vmeta_tsets                 // 'no __newindex' flag NOT set: check.
3835     |  jmp <2
3836     |
3837     |5:  // Follow hash chain.
3838     |  mov NODE:TMPR, NODE:TMPR->next
3839     |  test NODE:TMPR, NODE:TMPR
3840     |  jnz <1
3841     |  // End of hash chain: key not found, add a new one.
3842     |
3843     |  // But check for __newindex first.
3844     |  mov TAB:TMPR, TAB:RB->metatable
3845     |  test TAB:TMPR, TAB:TMPR
3846     |  jz >6                            // No metatable: continue.
3847     |  test byte TAB:TMPR->nomm, 1<<MM_newindex
3848     |  jz ->vmeta_tsets                 // 'no __newindex' flag NOT set: check.
3849     |6:
3850     |  mov TMP1, ITYPE
3851     |  mov L:CARG1, SAVE_L
3852     |  mov L:CARG1->base, BASE
3853     |  lea CARG3, TMP1
3854     |  mov CARG2, TAB:RB
3855     |  mov SAVE_PC, PC
3856     |  call extern lj_tab_newkey        // (lua_State *L, GCtab *t, TValue *k)
3857     |  // Handles write barrier for the new key. TValue * returned in eax (RC).
3858     |  mov L:CARG1, SAVE_L
3859     |  mov BASE, L:CARG1->base
3860     |  mov TMPR, rax
3861     |  movzx RAd, PC_RA
3862     |  jmp <2                           // Must check write barrier for value.
3863     |
3864     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3865     |  barrierback TAB:RB, ITYPE
3866     |  jmp <3
3867     break;
3868   case BC_TSETB:
3869     |  ins_ABC  // RA = src, RB = table, RC = byte literal
3870     |  mov TAB:RB, [BASE+RB*8]
3871     |  checktab TAB:RB, ->vmeta_tsetb
3872     |  cmp RCd, TAB:RB->asize
3873     |  jae ->vmeta_tsetb
3874     |  shl RCd, 3
3875     |  add RC, TAB:RB->array
3876     |  cmp aword [RC], LJ_TNIL
3877     |  je >3                            // Previous value is nil?
3878     |1:
3879     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
3880     |  jnz >7
3881     |2:  // Set array slot.
3882     |  mov ITYPE, [BASE+RA*8]
3883     |  mov [RC], ITYPE
3884     |  ins_next
3885     |
3886     |3:  // Check for __newindex if previous value is nil.
3887     |  mov TAB:TMPR, TAB:RB->metatable
3888     |  test TAB:TMPR, TAB:TMPR
3889     |  jz <1
3890     |  test byte TAB:TMPR->nomm, 1<<MM_newindex
3891     |  jz ->vmeta_tsetb                 // 'no __newindex' flag NOT set: check.
3892     |  jmp <1
3893     |
3894     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3895     |  barrierback TAB:RB, TMPR
3896     |  jmp <2
3897     break;
3898   case BC_TSETR:
3899     |  ins_ABC  // RA = src, RB = table, RC = key
3900     |  mov TAB:RB, [BASE+RB*8]
3901     |  cleartp TAB:RB
3902     |.if DUALNUM
3903     |  mov RC, [BASE+RC*8]
3904     |.else
3905     |  cvttsd2si RCd, qword [BASE+RC*8]
3906     |.endif
3907     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
3908     |  jnz >7
3909     |2:
3910     |  cmp RCd, TAB:RB->asize
3911     |  jae ->vmeta_tsetr
3912     |  shl RCd, 3
3913     |  add RC, TAB:RB->array
3914     |  // Set array slot.
3915     |->BC_TSETR_Z:
3916     |  mov ITYPE, [BASE+RA*8]
3917     |  mov [RC], ITYPE
3918     |  ins_next
3919     |
3920     |7:  // Possible table write barrier for the value. Skip valiswhite check.
3921     |  barrierback TAB:RB, TMPR
3922     |  jmp <2
3923     break;
3925   case BC_TSETM:
3926     |  ins_AD   // RA = base (table at base-1), RD = num const (start index)
3927     |1:
3928     |  mov TMPRd, dword [KBASE+RD*8]    // Integer constant is in lo-word.
3929     |  lea RA, [BASE+RA*8]
3930     |  mov TAB:RB, [RA-8]               // Guaranteed to be a table.
3931     |  cleartp TAB:RB
3932     |  test byte TAB:RB->marked, LJ_GC_BLACK    // isblack(table)
3933     |  jnz >7
3934     |2:
3935     |  mov RDd, MULTRES
3936     |  sub RDd, 1
3937     |  jz >4                            // Nothing to copy?
3938     |  add RDd, TMPRd                   // Compute needed size.
3939     |  cmp RDd, TAB:RB->asize
3940     |  ja >5                            // Doesn't fit into array part?
3941     |  sub RDd, TMPRd
3942     |  shl TMPRd, 3
3943     |  add TMPR, TAB:RB->array
3944     |3:  // Copy result slots to table.
3945     |  mov RB, [RA]
3946     |  add RA, 8
3947     |  mov [TMPR], RB
3948     |  add TMPR, 8
3949     |  sub RDd, 1
3950     |  jnz <3
3951     |4:
3952     |  ins_next
3953     |
3954     |5:  // Need to resize array part.
3955     |  mov L:CARG1, SAVE_L
3956     |  mov L:CARG1->base, BASE          // Caveat: CARG2/CARG3 may be BASE.
3957     |  mov CARG2, TAB:RB
3958     |  mov CARG3d, RDd
3959     |  mov L:RB, L:CARG1
3960     |  mov SAVE_PC, PC
3961     |  call extern lj_tab_reasize       // (lua_State *L, GCtab *t, int nasize)
3962     |  mov BASE, L:RB->base
3963     |  movzx RAd, PC_RA                 // Restore RA.
3964     |  movzx RDd, PC_RD                 // Restore RD.
3965     |  jmp <1                           // Retry.
3966     |
3967     |7:  // Possible table write barrier for any value. Skip valiswhite check.
3968     |  barrierback TAB:RB, RD
3969     |  jmp <2
3970     break;
3972   /* -- Calls and vararg handling ----------------------------------------- */
3974   case BC_CALL: case BC_CALLM:
3975     |  ins_A_C  // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
3976     if (op == BC_CALLM) {
3977       |  add NARGS:RDd, MULTRES
3978     }
3979     |  mov LFUNC:RB, [BASE+RA*8]
3980     |  checkfunc LFUNC:RB, ->vmeta_call_ra
3981     |  lea BASE, [BASE+RA*8+16]
3982     |  ins_call
3983     break;
3985   case BC_CALLMT:
3986     |  ins_AD   // RA = base, RD = extra_nargs
3987     |  add NARGS:RDd, MULTRES
3988     |  // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
3989     break;
3990   case BC_CALLT:
3991     |  ins_AD   // RA = base, RD = nargs+1
3992     |  lea RA, [BASE+RA*8+16]
3993     |  mov KBASE, BASE                  // Use KBASE for move + vmeta_call hint.
3994     |  mov LFUNC:RB, [RA-16]
3995     |  checktp_nc LFUNC:RB, LJ_TFUNC, ->vmeta_call
3996     |->BC_CALLT_Z:
3997     |  mov PC, [BASE-8]
3998     |  test PCd, FRAME_TYPE
3999     |  jnz >7
4000     |1:
4001     |  mov [BASE-16], LFUNC:RB          // Copy func+tag down, reloaded below.
4002     |  mov MULTRES, NARGS:RDd
4003     |  sub NARGS:RDd, 1
4004     |  jz >3
4005     |2:  // Move args down.
4006     |  mov RB, [RA]
4007     |  add RA, 8
4008     |  mov [KBASE], RB
4009     |  add KBASE, 8
4010     |  sub NARGS:RDd, 1
4011     |  jnz <2
4012     |
4013     |  mov LFUNC:RB, [BASE-16]
4014     |3:
4015     |  cleartp LFUNC:RB
4016     |  mov NARGS:RDd, MULTRES
4017     |  cmp byte LFUNC:RB->ffid, 1       // (> FF_C) Calling a fast function?
4018     |  ja >5
4019     |4:
4020     |  ins_callt
4021     |
4022     |5:  // Tailcall to a fast function.
4023     |  test PCd, FRAME_TYPE             // Lua frame below?
4024     |  jnz <4
4025     |  movzx RAd, PC_RA
4026     |  neg RA
4027     |  mov LFUNC:KBASE, [BASE+RA*8-32]  // Need to prepare KBASE.
4028     |  cleartp LFUNC:KBASE
4029     |  mov KBASE, LFUNC:KBASE->pc
4030     |  mov KBASE, [KBASE+PC2PROTO(k)]
4031     |  jmp <4
4032     |
4033     |7:  // Tailcall from a vararg function.
4034     |  sub PC, FRAME_VARG
4035     |  test PCd, FRAME_TYPEP
4036     |  jnz >8                           // Vararg frame below?
4037     |  sub BASE, PC                     // Need to relocate BASE/KBASE down.
4038     |  mov KBASE, BASE
4039     |  mov PC, [BASE-8]
4040     |  jmp <1
4041     |8:
4042     |  add PCd, FRAME_VARG
4043     |  jmp <1
4044     break;
4046   case BC_ITERC:
4047     |  ins_A    // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
4048     |  lea RA, [BASE+RA*8+16]           // fb = base+2
4049     |  mov RB, [RA-32]                  // Copy state. fb[0] = fb[-4].
4050     |  mov RC, [RA-24]                  // Copy control var. fb[1] = fb[-3].
4051     |  mov [RA], RB
4052     |  mov [RA+8], RC
4053     |  mov LFUNC:RB, [RA-40]            // Copy callable. fb[-1] = fb[-5]
4054     |  mov [RA-16], LFUNC:RB
4055     |  mov NARGS:RDd, 2+1               // Handle like a regular 2-arg call.
4056     |  checkfunc LFUNC:RB, ->vmeta_call
4057     |  mov BASE, RA
4058     |  ins_call
4059     break;
4061   case BC_ITERN:
4062     |  ins_A    // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
4063     |.if JIT
4064     |  // NYI: add hotloop, record BC_ITERN.
4065     |.endif
4066     |  mov TAB:RB, [BASE+RA*8-16]
4067     |  cleartp TAB:RB
4068     |  mov RCd, [BASE+RA*8-8]           // Get index from control var.
4069     |  mov TMPRd, TAB:RB->asize
4070     |  add PC, 4
4071     |  mov ITYPE, TAB:RB->array
4072     |1:  // Traverse array part.
4073     |  cmp RCd, TMPRd; jae >5           // Index points after array part?
4074     |  cmp aword [ITYPE+RC*8], LJ_TNIL; je >4
4075     |.if not DUALNUM
4076     |  cvtsi2sd xmm0, RCd
4077     |.endif
4078     |  // Copy array slot to returned value.
4079     |  mov RB, [ITYPE+RC*8]
4080     |  mov [BASE+RA*8+8], RB
4081     |  // Return array index as a numeric key.
4082     |.if DUALNUM
4083     |  setint ITYPE, RC
4084     |  mov [BASE+RA*8], ITYPE
4085     |.else
4086     |  movsd qword [BASE+RA*8], xmm0
4087     |.endif
4088     |  add RCd, 1
4089     |  mov [BASE+RA*8-8], RCd           // Update control var.
4090     |2:
4091     |  movzx RDd, PC_RD                 // Get target from ITERL.
4092     |  branchPC RD
4093     |3:
4094     |  ins_next
4095     |
4096     |4:  // Skip holes in array part.
4097     |  add RCd, 1
4098     |  jmp <1
4099     |
4100     |5:  // Traverse hash part.
4101     |  sub RCd, TMPRd
4102     |6:
4103     |  cmp RCd, TAB:RB->hmask; ja <3    // End of iteration? Branch to ITERL+1.
4104     |  imul ITYPEd, RCd, #NODE
4105     |  add NODE:ITYPE, TAB:RB->node
4106     |  cmp aword NODE:ITYPE->val, LJ_TNIL; je >7
4107     |  lea TMPRd, [RCd+TMPRd+1]
4108     |  // Copy key and value from hash slot.
4109     |  mov RB, NODE:ITYPE->key
4110     |  mov RC, NODE:ITYPE->val
4111     |  mov [BASE+RA*8], RB
4112     |  mov [BASE+RA*8+8], RC
4113     |  mov [BASE+RA*8-8], TMPRd
4114     |  jmp <2
4115     |
4116     |7:  // Skip holes in hash part.
4117     |  add RCd, 1
4118     |  jmp <6
4119     break;
4121   case BC_ISNEXT:
4122     |  ins_AD   // RA = base, RD = target (points to ITERN)
4123     |  mov CFUNC:RB, [BASE+RA*8-24]
4124     |  checkfunc CFUNC:RB, >5
4125     |  checktptp [BASE+RA*8-16], LJ_TTAB, >5
4126     |  cmp aword [BASE+RA*8-8], LJ_TNIL; jne >5
4127     |  cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
4128     |  branchPC RD
4129     |  mov64 TMPR, U64x(fffe7fff, 00000000)
4130     |  mov [BASE+RA*8-8], TMPR          // Initialize control var.
4131     |1:
4132     |  ins_next
4133     |5:  // Despecialize bytecode if any of the checks fail.
4134     |  mov PC_OP, BC_JMP
4135     |  branchPC RD
4136     |  mov byte [PC], BC_ITERC
4137     |  jmp <1
4138     break;
4140   case BC_VARG:
4141     |  ins_ABC  // RA = base, RB = nresults+1, RC = numparams
4142     |  lea TMPR, [BASE+RC*8+(16+FRAME_VARG)]
4143     |  lea RA, [BASE+RA*8]
4144     |  sub TMPR, [BASE-8]
4145     |  // Note: TMPR may now be even _above_ BASE if nargs was < numparams.
4146     |  test RB, RB
4147     |  jz >5                            // Copy all varargs?
4148     |  lea RB, [RA+RB*8-8]
4149     |  cmp TMPR, BASE                   // No vararg slots?
4150     |  jnb >2
4151     |1:  // Copy vararg slots to destination slots.
4152     |  mov RC, [TMPR-16]
4153     |  add TMPR, 8
4154     |  mov [RA], RC
4155     |  add RA, 8
4156     |  cmp RA, RB                       // All destination slots filled?
4157     |  jnb >3
4158     |  cmp TMPR, BASE                   // No more vararg slots?
4159     |  jb <1
4160     |2:  // Fill up remainder with nil.
4161     |  mov aword [RA], LJ_TNIL
4162     |  add RA, 8
4163     |  cmp RA, RB
4164     |  jb <2
4165     |3:
4166     |  ins_next
4167     |
4168     |5:  // Copy all varargs.
4169     |  mov MULTRES, 1                   // MULTRES = 0+1
4170     |  mov RC, BASE
4171     |  sub RC, TMPR
4172     |  jbe <3                           // No vararg slots?
4173     |  mov RBd, RCd
4174     |  shr RBd, 3
4175     |  add RBd, 1
4176     |  mov MULTRES, RBd                 // MULTRES = #varargs+1
4177     |  mov L:RB, SAVE_L
4178     |  add RC, RA
4179     |  cmp RC, L:RB->maxstack
4180     |  ja >7                            // Need to grow stack?
4181     |6:  // Copy all vararg slots.
4182     |  mov RC, [TMPR-16]
4183     |  add TMPR, 8
4184     |  mov [RA], RC
4185     |  add RA, 8
4186     |  cmp TMPR, BASE                   // No more vararg slots?
4187     |  jb <6
4188     |  jmp <3
4189     |
4190     |7:  // Grow stack for varargs.
4191     |  mov L:RB->base, BASE
4192     |  mov L:RB->top, RA
4193     |  mov SAVE_PC, PC
4194     |  sub TMPR, BASE                   // Need delta, because BASE may change.
4195     |  mov TMP1hi, TMPRd
4196     |  mov CARG2d, MULTRES
4197     |  sub CARG2d, 1
4198     |  mov CARG1, L:RB
4199     |  call extern lj_state_growstack   // (lua_State *L, int n)
4200     |  mov BASE, L:RB->base
4201     |  movsxd TMPR, TMP1hi
4202     |  mov RA, L:RB->top
4203     |  add TMPR, BASE
4204     |  jmp <6
4205     break;
4207   /* -- Returns ----------------------------------------------------------- */
4209   case BC_RETM:
4210     |  ins_AD   // RA = results, RD = extra_nresults
4211     |  add RDd, MULTRES                 // MULTRES >=1, so RD >=1.
4212     |  // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
4213     break;
4215   case BC_RET: case BC_RET0: case BC_RET1:
4216     |  ins_AD   // RA = results, RD = nresults+1
4217     if (op != BC_RET0) {
4218       |  shl RAd, 3
4219     }
4220     |1:
4221     |  mov PC, [BASE-8]
4222     |  mov MULTRES, RDd                 // Save nresults+1.
4223     |  test PCd, FRAME_TYPE             // Check frame type marker.
4224     |  jnz >7                           // Not returning to a fixarg Lua func?
4225     switch (op) {
4226     case BC_RET:
4227       |->BC_RET_Z:
4228       |  mov KBASE, BASE                // Use KBASE for result move.
4229       |  sub RDd, 1
4230       |  jz >3
4231       |2:  // Move results down.
4232       |  mov RB, [KBASE+RA]
4233       |  mov [KBASE-16], RB
4234       |  add KBASE, 8
4235       |  sub RDd, 1
4236       |  jnz <2
4237       |3:
4238       |  mov RDd, MULTRES               // Note: MULTRES may be >255.
4239       |  movzx RBd, PC_RB               // So cannot compare with RDL!
4240       |5:
4241       |  cmp RBd, RDd                   // More results expected?
4242       |  ja >6
4243       break;
4244     case BC_RET1:
4245       |  mov RB, [BASE+RA]
4246       |  mov [BASE-16], RB
4247       /* fallthrough */
4248     case BC_RET0:
4249       |5:
4250       |  cmp PC_RB, RDL                 // More results expected?
4251       |  ja >6
4252     default:
4253       break;
4254     }
4255     |  movzx RAd, PC_RA
4256     |  neg RA
4257     |  lea BASE, [BASE+RA*8-16]         // base = base - (RA+2)*8
4258     |  mov LFUNC:KBASE, [BASE-16]
4259     |  cleartp LFUNC:KBASE
4260     |  mov KBASE, LFUNC:KBASE->pc
4261     |  mov KBASE, [KBASE+PC2PROTO(k)]
4262     |  ins_next
4263     |
4264     |6:  // Fill up results with nil.
4265     if (op == BC_RET) {
4266       |  mov aword [KBASE-16], LJ_TNIL  // Note: relies on shifted base.
4267       |  add KBASE, 8
4268     } else {
4269       |  mov aword [BASE+RD*8-24], LJ_TNIL
4270     }
4271     |  add RD, 1
4272     |  jmp <5
4273     |
4274     |7:  // Non-standard return case.
4275     |  lea RB, [PC-FRAME_VARG]
4276     |  test RBd, FRAME_TYPEP
4277     |  jnz ->vm_return
4278     |  // Return from vararg function: relocate BASE down and RA up.
4279     |  sub BASE, RB
4280     if (op != BC_RET0) {
4281       |  add RA, RB
4282     }
4283     |  jmp <1
4284     break;
4286   /* -- Loops and branches ------------------------------------------------ */
4288   |.define FOR_IDX,  [RA]
4289   |.define FOR_STOP, [RA+8]
4290   |.define FOR_STEP, [RA+16]
4291   |.define FOR_EXT,  [RA+24]
4293   case BC_FORL:
4294     |.if JIT
4295     |  hotloop RBd
4296     |.endif
4297     | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
4298     break;
4300   case BC_JFORI:
4301   case BC_JFORL:
4302 #if !LJ_HASJIT
4303     break;
4304 #endif
4305   case BC_FORI:
4306   case BC_IFORL:
4307     vk = (op == BC_IFORL || op == BC_JFORL);
4308     |  ins_AJ   // RA = base, RD = target (after end of loop or start of loop)
4309     |  lea RA, [BASE+RA*8]
4310     if (LJ_DUALNUM) {
4311       |  mov RB, FOR_IDX
4312       |  checkint RB, >9
4313       |  mov TMPR, FOR_STOP
4314       if (!vk) {
4315         |  checkint TMPR, ->vmeta_for
4316         |  mov ITYPE, FOR_STEP
4317         |  test ITYPEd, ITYPEd; js >5
4318         |  sar ITYPE, 47;
4319         |  cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for
4320       } else {
4321 #ifdef LUA_USE_ASSERT
4322         |  checkinttp FOR_STOP, ->assert_bad_for_arg_type
4323         |  checkinttp FOR_STEP, ->assert_bad_for_arg_type
4324 #endif
4325         |  mov ITYPE, FOR_STEP
4326         |  test ITYPEd, ITYPEd; js >5
4327         |  add RBd, ITYPEd; jo >1
4328         |  setint RB
4329         |  mov FOR_IDX, RB
4330       }
4331       |  cmp RBd, TMPRd
4332       |  mov FOR_EXT, RB
4333       if (op == BC_FORI) {
4334         |  jle >7
4335         |1:
4336         |6:
4337         |  branchPC RD
4338       } else if (op == BC_JFORI) {
4339         |  branchPC RD
4340         |  movzx RDd, PC_RD
4341         |  jle =>BC_JLOOP
4342         |1:
4343         |6:
4344       } else if (op == BC_IFORL) {
4345         |  jg >7
4346         |6:
4347         |  branchPC RD
4348         |1:
4349       } else {
4350         |  jle =>BC_JLOOP
4351         |1:
4352         |6:
4353       }
4354       |7:
4355       |  ins_next
4356       |
4357       |5:  // Invert check for negative step.
4358       if (!vk) {
4359         |  sar ITYPE, 47;
4360         |  cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for
4361       } else {
4362         |  add RBd, ITYPEd; jo <1
4363         |  setint RB
4364         |  mov FOR_IDX, RB
4365       }
4366       |  cmp RBd, TMPRd
4367       |  mov FOR_EXT, RB
4368       if (op == BC_FORI) {
4369         |  jge <7
4370       } else if (op == BC_JFORI) {
4371         |  branchPC RD
4372         |  movzx RDd, PC_RD
4373         |  jge =>BC_JLOOP
4374       } else if (op == BC_IFORL) {
4375         |  jl <7
4376       } else {
4377         |  jge =>BC_JLOOP
4378       }
4379       |  jmp <6
4380       |9:  // Fallback to FP variant.
4381       if (!vk) {
4382         |  jae ->vmeta_for
4383       }
4384     } else if (!vk) {
4385       |  checknumtp FOR_IDX, ->vmeta_for
4386     }
4387     if (!vk) {
4388       |  checknumtp FOR_STOP, ->vmeta_for
4389     } else {
4390 #ifdef LUA_USE_ASSERT
4391       |  checknumtp FOR_STOP, ->assert_bad_for_arg_type
4392       |  checknumtp FOR_STEP, ->assert_bad_for_arg_type
4393 #endif
4394     }
4395     |  mov RB, FOR_STEP
4396     if (!vk) {
4397       |  checknum RB, ->vmeta_for
4398     }
4399     |  movsd xmm0, qword FOR_IDX
4400     |  movsd xmm1, qword FOR_STOP
4401     if (vk) {
4402       |  addsd xmm0, qword FOR_STEP
4403       |  movsd qword FOR_IDX, xmm0
4404       |  test RB, RB; js >3
4405     } else {
4406       |  jl >3
4407     }
4408     |  ucomisd xmm1, xmm0
4409     |1:
4410     |  movsd qword FOR_EXT, xmm0
4411     if (op == BC_FORI) {
4412       |.if DUALNUM
4413       |  jnb <7
4414       |.else
4415       |  jnb >2
4416       |  branchPC RD
4417       |.endif
4418     } else if (op == BC_JFORI) {
4419       |  branchPC RD
4420       |  movzx RDd, PC_RD
4421       |  jnb =>BC_JLOOP
4422     } else if (op == BC_IFORL) {
4423       |.if DUALNUM
4424       |  jb <7
4425       |.else
4426       |  jb >2
4427       |  branchPC RD
4428       |.endif
4429     } else {
4430       |  jnb =>BC_JLOOP
4431     }
4432     |.if DUALNUM
4433     |  jmp <6
4434     |.else
4435     |2:
4436     |  ins_next
4437     |.endif
4438     |
4439     |3:  // Invert comparison if step is negative.
4440     |  ucomisd xmm0, xmm1
4441     |  jmp <1
4442     break;
4444   case BC_ITERL:
4445     |.if JIT
4446     |  hotloop RBd
4447     |.endif
4448     | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
4449     break;
4451   case BC_JITERL:
4452 #if !LJ_HASJIT
4453     break;
4454 #endif
4455   case BC_IITERL:
4456     |  ins_AJ   // RA = base, RD = target
4457     |  lea RA, [BASE+RA*8]
4458     |  mov RB, [RA]
4459     |  cmp RB, LJ_TNIL; je >1           // Stop if iterator returned nil.
4460     if (op == BC_JITERL) {
4461       |  mov [RA-8], RB
4462       |  jmp =>BC_JLOOP
4463     } else {
4464       |  branchPC RD                    // Otherwise save control var + branch.
4465       |  mov [RA-8], RB
4466     }
4467     |1:
4468     |  ins_next
4469     break;
4471   case BC_LOOP:
4472     |  ins_A    // RA = base, RD = target (loop extent)
4473     |  // Note: RA/RD is only used by trace recorder to determine scope/extent
4474     |  // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4475     |.if JIT
4476     |  hotloop RBd
4477     |.endif
4478     | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
4479     break;
4481   case BC_ILOOP:
4482     |  ins_A    // RA = base, RD = target (loop extent)
4483     |  ins_next
4484     break;
4486   case BC_JLOOP:
4487     |.if JIT
4488     |  ins_AD   // RA = base (ignored), RD = traceno
4489     |  mov RA, [DISPATCH+DISPATCH_J(trace)]
4490     |  mov TRACE:RD, [RA+RD*8]
4491     |  mov RD, TRACE:RD->mcode
4492     |  mov L:RB, SAVE_L
4493     |  mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
4494     |  mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB
4495     |  // Save additional callee-save registers only used in compiled code.
4496     |.if X64WIN
4497     |  mov CSAVE_4, r12
4498     |  mov CSAVE_3, r13
4499     |  mov CSAVE_2, r14
4500     |  mov CSAVE_1, r15
4501     |  mov RA, rsp
4502     |  sub rsp, 10*16+4*8
4503     |  movdqa [RA-1*16], xmm6
4504     |  movdqa [RA-2*16], xmm7
4505     |  movdqa [RA-3*16], xmm8
4506     |  movdqa [RA-4*16], xmm9
4507     |  movdqa [RA-5*16], xmm10
4508     |  movdqa [RA-6*16], xmm11
4509     |  movdqa [RA-7*16], xmm12
4510     |  movdqa [RA-8*16], xmm13
4511     |  movdqa [RA-9*16], xmm14
4512     |  movdqa [RA-10*16], xmm15
4513     |.else
4514     |  sub rsp, 16
4515     |  mov [rsp+16], r12
4516     |  mov [rsp+8], r13
4517     |.endif
4518     |  jmp RD
4519     |.endif
4520     break;
4522   case BC_JMP:
4523     |  ins_AJ   // RA = unused, RD = target
4524     |  branchPC RD
4525     |  ins_next
4526     break;
4528   /* -- Function headers -------------------------------------------------- */
4530    /*
4531    ** Reminder: A function may be called with func/args above L->maxstack,
4532    ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
4533    ** too. This means all FUNC* ops (including fast functions) must check
4534    ** for stack overflow _before_ adding more slots!
4535    */
4537   case BC_FUNCF:
4538     |.if JIT
4539     |  hotcall RBd
4540     |.endif
4541   case BC_FUNCV:  /* NYI: compiled vararg functions. */
4542     | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
4543     break;
4545   case BC_JFUNCF:
4546 #if !LJ_HASJIT
4547     break;
4548 #endif
4549   case BC_IFUNCF:
4550     |  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1
4551     |  mov KBASE, [PC-4+PC2PROTO(k)]
4552     |  mov L:RB, SAVE_L
4553     |  lea RA, [BASE+RA*8]              // Top of frame.
4554     |  cmp RA, L:RB->maxstack
4555     |  ja ->vm_growstack_f
4556     |  movzx RAd, byte [PC-4+PC2PROTO(numparams)]
4557     |  cmp NARGS:RDd, RAd               // Check for missing parameters.
4558     |  jbe >3
4559     |2:
4560     if (op == BC_JFUNCF) {
4561       |  movzx RDd, PC_RD
4562       |  jmp =>BC_JLOOP
4563     } else {
4564       |  ins_next
4565     }
4566     |
4567     |3:  // Clear missing parameters.
4568     |  mov aword [BASE+NARGS:RD*8-8], LJ_TNIL
4569     |  add NARGS:RDd, 1
4570     |  cmp NARGS:RDd, RAd
4571     |  jbe <3
4572     |  jmp <2
4573     break;
4575   case BC_JFUNCV:
4576 #if !LJ_HASJIT
4577     break;
4578 #endif
4579     | int3  // NYI: compiled vararg functions
4580     break;  /* NYI: compiled vararg functions. */
4582   case BC_IFUNCV:
4583     |  ins_AD  // BASE = new base, RA = framesize, RD = nargs+1
4584     |  lea RBd, [NARGS:RD*8+FRAME_VARG+8]
4585     |  lea RD, [BASE+NARGS:RD*8+8]
4586     |  mov LFUNC:KBASE, [BASE-16]
4587     |  mov [RD-8], RB                   // Store delta + FRAME_VARG.
4588     |  mov [RD-16], LFUNC:KBASE         // Store copy of LFUNC.
4589     |  mov L:RB, SAVE_L
4590     |  lea RA, [RD+RA*8]
4591     |  cmp RA, L:RB->maxstack
4592     |  ja ->vm_growstack_v              // Need to grow stack.
4593     |  mov RA, BASE
4594     |  mov BASE, RD
4595     |  movzx RBd, byte [PC-4+PC2PROTO(numparams)]
4596     |  test RBd, RBd
4597     |  jz >2
4598     |  add RA, 8
4599     |1:  // Copy fixarg slots up to new frame.
4600     |  add RA, 8
4601     |  cmp RA, BASE
4602     |  jnb >3                           // Less args than parameters?
4603     |  mov KBASE, [RA-16]
4604     |  mov [RD], KBASE
4605     |  add RD, 8
4606     |  mov aword [RA-16], LJ_TNIL       // Clear old fixarg slot (help the GC).
4607     |  sub RBd, 1
4608     |  jnz <1
4609     |2:
4610     if (op == BC_JFUNCV) {
4611       |  movzx RDd, PC_RD
4612       |  jmp =>BC_JLOOP
4613     } else {
4614       |  mov KBASE, [PC-4+PC2PROTO(k)]
4615       |  ins_next
4616     }
4617     |
4618     |3:  // Clear missing parameters.
4619     |  mov aword [RD], LJ_TNIL
4620     |  add RD, 8
4621     |  sub RBd, 1
4622     |  jnz <3
4623     |  jmp <2
4624     break;
4626   case BC_FUNCC:
4627   case BC_FUNCCW:
4628     |  ins_AD  // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
4629     |  mov CFUNC:RB, [BASE-16]
4630     |  cleartp CFUNC:RB
4631     |  mov KBASE, CFUNC:RB->f
4632     |  mov L:RB, SAVE_L
4633     |  lea RD, [BASE+NARGS:RD*8-8]
4634     |  mov L:RB->base, BASE
4635     |  lea RA, [RD+8*LUA_MINSTACK]
4636     |  cmp RA, L:RB->maxstack
4637     |  mov L:RB->top, RD
4638     if (op == BC_FUNCC) {
4639       |  mov CARG1, L:RB                // Caveat: CARG1 may be RA.
4640     } else {
4641       |  mov CARG2, KBASE
4642       |  mov CARG1, L:RB                // Caveat: CARG1 may be RA.
4643     }
4644     |  ja ->vm_growstack_c              // Need to grow stack.
4645     |  set_vmstate C
4646     if (op == BC_FUNCC) {
4647       |  call KBASE                     // (lua_State *L)
4648     } else {
4649       |  // (lua_State *L, lua_CFunction f)
4650       |  call aword [DISPATCH+DISPATCH_GL(wrapf)]
4651     }
4652     |  // nresults returned in eax (RD).
4653     |  mov BASE, L:RB->base
4654     |  mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
4655     |  set_vmstate INTERP
4656     |  lea RA, [BASE+RD*8]
4657     |  neg RA
4658     |  add RA, L:RB->top                // RA = (L->top-(L->base+nresults))*8
4659     |  mov PC, [BASE-8]                 // Fetch PC of caller.
4660     |  jmp ->vm_returnc
4661     break;
4663   /* ---------------------------------------------------------------------- */
4665   default:
4666     fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4667     exit(2);
4668     break;
4669   }
4672 static int build_backend(BuildCtx *ctx)
4674   int op;
4675   dasm_growpc(Dst, BC__MAX);
4676   build_subroutines(ctx);
4677   |.code_op
4678   for (op = 0; op < BC__MAX; op++)
4679     build_ins(ctx, (BCOp)op, op);
4680   return BC__MAX;
4683 /* Emit pseudo frame-info for all assembler functions. */
4684 static void emit_asm_debug(BuildCtx *ctx)
4686   int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4687   switch (ctx->mode) {
4688   case BUILD_elfasm:
4689     fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
4690     fprintf(ctx->fp,
4691         ".Lframe0:\n"
4692         "\t.long .LECIE0-.LSCIE0\n"
4693         ".LSCIE0:\n"
4694         "\t.long 0xffffffff\n"
4695         "\t.byte 0x1\n"
4696         "\t.string \"\"\n"
4697         "\t.uleb128 0x1\n"
4698         "\t.sleb128 -8\n"
4699         "\t.byte 0x10\n"
4700         "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4701         "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4702         "\t.align 8\n"
4703         ".LECIE0:\n\n");
4704     fprintf(ctx->fp,
4705         ".LSFDE0:\n"
4706         "\t.long .LEFDE0-.LASFDE0\n"
4707         ".LASFDE0:\n"
4708         "\t.long .Lframe0\n"
4709         "\t.quad .Lbegin\n"
4710         "\t.quad %d\n"
4711         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
4712         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
4713         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
4714         "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */
4715         "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */
4716 #if LJ_NO_UNWIND
4717         "\t.byte 0x8d\n\t.uleb128 0x6\n"        /* offset r13 */
4718         "\t.byte 0x8c\n\t.uleb128 0x7\n"        /* offset r12 */
4719 #endif
4720         "\t.align 8\n"
4721         ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
4722 #if LJ_HASFFI
4723     fprintf(ctx->fp,
4724         ".LSFDE1:\n"
4725         "\t.long .LEFDE1-.LASFDE1\n"
4726         ".LASFDE1:\n"
4727         "\t.long .Lframe0\n"
4728         "\t.quad lj_vm_ffi_call\n"
4729         "\t.quad %d\n"
4730         "\t.byte 0xe\n\t.uleb128 16\n"          /* def_cfa_offset */
4731         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
4732         "\t.byte 0xd\n\t.uleb128 0x6\n"         /* def_cfa_register rbp */
4733         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
4734         "\t.align 8\n"
4735         ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
4736 #endif
4737 #if !LJ_NO_UNWIND
4738 #if (defined(__sun__) && defined(__svr4__))
4739     fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");
4740 #else
4741     fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
4742 #endif
4743     fprintf(ctx->fp,
4744         ".Lframe1:\n"
4745         "\t.long .LECIE1-.LSCIE1\n"
4746         ".LSCIE1:\n"
4747         "\t.long 0\n"
4748         "\t.byte 0x1\n"
4749         "\t.string \"zPR\"\n"
4750         "\t.uleb128 0x1\n"
4751         "\t.sleb128 -8\n"
4752         "\t.byte 0x10\n"
4753         "\t.uleb128 6\n"                        /* augmentation length */
4754         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
4755         "\t.long lj_err_unwind_dwarf-.\n"
4756         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
4757         "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4758         "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4759         "\t.align 8\n"
4760         ".LECIE1:\n\n");
4761     fprintf(ctx->fp,
4762         ".LSFDE2:\n"
4763         "\t.long .LEFDE2-.LASFDE2\n"
4764         ".LASFDE2:\n"
4765         "\t.long .LASFDE2-.Lframe1\n"
4766         "\t.long .Lbegin-.\n"
4767         "\t.long %d\n"
4768         "\t.uleb128 0\n"                        /* augmentation length */
4769         "\t.byte 0xe\n\t.uleb128 %d\n"          /* def_cfa_offset */
4770         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
4771         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
4772         "\t.byte 0x8f\n\t.uleb128 0x4\n"        /* offset r15 */
4773         "\t.byte 0x8e\n\t.uleb128 0x5\n"        /* offset r14 */
4774         "\t.align 8\n"
4775         ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
4776 #if LJ_HASFFI
4777     fprintf(ctx->fp,
4778         ".Lframe2:\n"
4779         "\t.long .LECIE2-.LSCIE2\n"
4780         ".LSCIE2:\n"
4781         "\t.long 0\n"
4782         "\t.byte 0x1\n"
4783         "\t.string \"zR\"\n"
4784         "\t.uleb128 0x1\n"
4785         "\t.sleb128 -8\n"
4786         "\t.byte 0x10\n"
4787         "\t.uleb128 1\n"                        /* augmentation length */
4788         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
4789         "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4790         "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4791         "\t.align 8\n"
4792         ".LECIE2:\n\n");
4793     fprintf(ctx->fp,
4794         ".LSFDE3:\n"
4795         "\t.long .LEFDE3-.LASFDE3\n"
4796         ".LASFDE3:\n"
4797         "\t.long .LASFDE3-.Lframe2\n"
4798         "\t.long lj_vm_ffi_call-.\n"
4799         "\t.long %d\n"
4800         "\t.uleb128 0\n"                        /* augmentation length */
4801         "\t.byte 0xe\n\t.uleb128 16\n"          /* def_cfa_offset */
4802         "\t.byte 0x86\n\t.uleb128 0x2\n"        /* offset rbp */
4803         "\t.byte 0xd\n\t.uleb128 0x6\n"         /* def_cfa_register rbp */
4804         "\t.byte 0x83\n\t.uleb128 0x3\n"        /* offset rbx */
4805         "\t.align 8\n"
4806         ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
4807 #endif
4808 #endif
4809     break;
4810 #if !LJ_NO_UNWIND
4811   /* Mental note: never let Apple design an assembler.
4812   ** Or a linker. Or a plastic case. But I digress.
4813   */
4814   case BUILD_machasm: {
4815 #if LJ_HASFFI
4816     int fcsize = 0;
4817 #endif
4818     int i;
4819     fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
4820     fprintf(ctx->fp,
4821         "EH_frame1:\n"
4822         "\t.set L$set$x,LECIEX-LSCIEX\n"
4823         "\t.long L$set$x\n"
4824         "LSCIEX:\n"
4825         "\t.long 0\n"
4826         "\t.byte 0x1\n"
4827         "\t.ascii \"zPR\\0\"\n"
4828         "\t.byte 0x1\n"
4829         "\t.byte 128-8\n"
4830         "\t.byte 0x10\n"
4831         "\t.byte 6\n"                           /* augmentation length */
4832         "\t.byte 0x9b\n"                        /* indirect|pcrel|sdata4 */
4833         "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
4834         "\t.byte 0x1b\n"                        /* pcrel|sdata4 */
4835         "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n"
4836         "\t.byte 0x80+0x10\n\t.byte 0x1\n"
4837         "\t.align 3\n"
4838         "LECIEX:\n\n");
4839     for (i = 0; i < ctx->nsym; i++) {
4840       const char *name = ctx->sym[i].name;
4841       int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
4842       if (size == 0) continue;
4843 #if LJ_HASFFI
4844       if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
4845 #endif
4846       fprintf(ctx->fp,
4847           "%s.eh:\n"
4848           "LSFDE%d:\n"
4849           "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
4850           "\t.long L$set$%d\n"
4851           "LASFDE%d:\n"
4852           "\t.long LASFDE%d-EH_frame1\n"
4853           "\t.long %s-.\n"
4854           "\t.long %d\n"
4855           "\t.byte 0\n"                         /* augmentation length */
4856           "\t.byte 0xe\n\t.byte %d\n"           /* def_cfa_offset */
4857           "\t.byte 0x86\n\t.byte 0x2\n"         /* offset rbp */
4858           "\t.byte 0x83\n\t.byte 0x3\n"         /* offset rbx */
4859           "\t.byte 0x8f\n\t.byte 0x4\n"         /* offset r15 */
4860           "\t.byte 0x8e\n\t.byte 0x5\n"         /* offset r14 */
4861           "\t.align 3\n"
4862           "LEFDE%d:\n\n",
4863           name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
4864     }
4865 #if LJ_HASFFI
4866     if (fcsize) {
4867       fprintf(ctx->fp,
4868           "EH_frame2:\n"
4869           "\t.set L$set$y,LECIEY-LSCIEY\n"
4870           "\t.long L$set$y\n"
4871           "LSCIEY:\n"
4872           "\t.long 0\n"
4873           "\t.byte 0x1\n"
4874           "\t.ascii \"zR\\0\"\n"
4875           "\t.byte 0x1\n"
4876           "\t.byte 128-8\n"
4877           "\t.byte 0x10\n"
4878           "\t.byte 1\n"                         /* augmentation length */
4879           "\t.byte 0x1b\n"                      /* pcrel|sdata4 */
4880           "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n"
4881           "\t.byte 0x80+0x10\n\t.byte 0x1\n"
4882           "\t.align 3\n"
4883           "LECIEY:\n\n");
4884       fprintf(ctx->fp,
4885           "_lj_vm_ffi_call.eh:\n"
4886           "LSFDEY:\n"
4887           "\t.set L$set$yy,LEFDEY-LASFDEY\n"
4888           "\t.long L$set$yy\n"
4889           "LASFDEY:\n"
4890           "\t.long LASFDEY-EH_frame2\n"
4891           "\t.long _lj_vm_ffi_call-.\n"
4892           "\t.long %d\n"
4893           "\t.byte 0\n"                         /* augmentation length */
4894           "\t.byte 0xe\n\t.byte 16\n"           /* def_cfa_offset */
4895           "\t.byte 0x86\n\t.byte 0x2\n"         /* offset rbp */
4896           "\t.byte 0xd\n\t.byte 0x6\n"          /* def_cfa_register rbp */
4897           "\t.byte 0x83\n\t.byte 0x3\n"         /* offset rbx */
4898           "\t.align 3\n"
4899           "LEFDEY:\n\n", fcsize);
4900     }
4901 #endif
4902     fprintf(ctx->fp, ".subsections_via_symbols\n");
4903     }
4904     break;
4905 #endif
4906   default:  /* Difficult for other modes. */
4907     break;
4908   }