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