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