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