1 |// Low-level VM code for x86 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
10 |.section code_op, code_sub
12 |.actionlist build_actionlist
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.
23 |.define KBASE, edi // Must be C callee-save.
24 |.define KBASEa, KBASE
25 |.define PC, esi // Must be C callee-save.
27 |.define DISPATCH, ebx // Must be C callee-save.
29 |.define KBASE, edi // Must be C callee-save.
31 |.define PC, esi // Must be C callee-save.
33 |.define DISPATCH, ebx // Must be C callee-save.
35 |.define KBASE, r15d // Must be C callee-save.
37 |.define PC, ebx // Must be C callee-save.
39 |.define DISPATCH, r14d // Must be C callee-save.
45 |.define RB, ebp // Must be ebp (C callee-save).
46 |.define RC, eax // Must be eax (fcomparepp and others).
67 |.define FCARG1, ecx // x86 fastcall arguments.
70 |.define CARG1, rcx // x64/WIN64 C call arguments.
78 |.define FCARG1, CARG1d // Upwards compatible to x86 fastcall.
79 |.define FCARG2, CARG2d
81 |.define CARG1, rdi // x64/POSIX C call arguments.
93 |.define FCARG1, CARG1d // Simulate x86 fastcall.
94 |.define FCARG2, CARG2d
97 |// Type definitions. Some of these are only used for documentation.
99 |.type GL, global_State
100 |.type TVALUE, TValue
104 |.type LFUNC, GCfuncL
105 |.type CFUNC, GCfuncC
106 |.type PROTO, GCproto
107 |.type UPVAL, GCupval
110 |.type TRACE, GCtrace
112 |// Stack layout while in interpreter. Must match with lj_frame.h.
113 |//-----------------------------------------------------------------------
114 |.if not X64 // x86 stack layout.
116 |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
118 | push edi; push esi; push ebx
119 | sub esp, CFRAME_SPACE
122 | push ebp; saveregs_
125 | add esp, CFRAME_SPACE
126 | pop ebx; pop esi; pop edi; pop ebp
129 |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
130 |.define SAVE_NRES, aword [esp+aword*14]
131 |.define SAVE_CFRAME, aword [esp+aword*13]
132 |.define SAVE_L, aword [esp+aword*12]
133 |//----- 16 byte aligned, ^^^ arguments from C caller
134 |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
135 |.define SAVE_R4, aword [esp+aword*10]
136 |.define SAVE_R3, aword [esp+aword*9]
137 |.define SAVE_R2, aword [esp+aword*8]
138 |//----- 16 byte aligned
139 |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
140 |.define SAVE_PC, aword [esp+aword*6]
141 |.define TMP2, aword [esp+aword*5]
142 |.define TMP1, aword [esp+aword*4]
143 |//----- 16 byte aligned
144 |.define ARG4, aword [esp+aword*3]
145 |.define ARG3, aword [esp+aword*2]
146 |.define ARG2, aword [esp+aword*1]
147 |.define ARG1, aword [esp] //<-- esp while in interpreter.
148 |//----- 16 byte aligned, ^^^ arguments for C callee
150 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
151 |.define FPARG3, qword [esp+qword*1]
152 |.define FPARG1, qword [esp]
153 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
154 |.define TMPQ, qword [esp+aword*4]
158 |.define MULTRES, TMP2
160 |// Arguments for vm_call and vm_pcall.
161 |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
163 |// Arguments for vm_cpcall.
164 |.define INARG_CP_CALL, SAVE_ERRF
165 |.define INARG_CP_UD, SAVE_NRES
166 |.define INARG_CP_FUNC, SAVE_CFRAME
168 |//-----------------------------------------------------------------------
169 |.elif X64WIN // x64/Windows stack layout
171 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
173 | push rdi; push rsi; push rbx
174 | sub rsp, CFRAME_SPACE
177 | push rbp; saveregs_
180 | add rsp, CFRAME_SPACE
181 | pop rbx; pop rsi; pop rdi; pop rbp
184 |.define SAVE_CFRAME, aword [rsp+aword*13]
185 |.define SAVE_PC, dword [rsp+dword*25]
186 |.define SAVE_L, dword [rsp+dword*24]
187 |.define SAVE_ERRF, dword [rsp+dword*23]
188 |.define SAVE_NRES, dword [rsp+dword*22]
189 |.define TMP2, dword [rsp+dword*21]
190 |.define TMP1, dword [rsp+dword*20]
191 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
192 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
193 |.define SAVE_R4, aword [rsp+aword*8]
194 |.define SAVE_R3, aword [rsp+aword*7]
195 |.define SAVE_R2, aword [rsp+aword*6]
196 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
197 |.define ARG5, aword [rsp+aword*4]
198 |.define CSAVE_4, aword [rsp+aword*3]
199 |.define CSAVE_3, aword [rsp+aword*2]
200 |.define CSAVE_2, aword [rsp+aword*1]
201 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
202 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
204 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
205 |.define TMPQ, qword [rsp+aword*10]
206 |.define MULTRES, TMP2
208 |.define ARG5d, dword [rsp+aword*4]
211 |//-----------------------------------------------------------------------
212 |.else // x64/POSIX stack layout
214 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
216 | push rbx; push r15; push r14
217 | sub rsp, CFRAME_SPACE
220 | push rbp; saveregs_
223 | add rsp, CFRAME_SPACE
224 | pop r14; pop r15; pop rbx; pop rbp
227 |//----- 16 byte aligned,
228 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
229 |.define SAVE_R4, aword [rsp+aword*8]
230 |.define SAVE_R3, aword [rsp+aword*7]
231 |.define SAVE_R2, aword [rsp+aword*6]
232 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
233 |.define SAVE_CFRAME, aword [rsp+aword*4]
234 |.define SAVE_PC, dword [rsp+dword*7]
235 |.define SAVE_L, dword [rsp+dword*6]
236 |.define SAVE_ERRF, dword [rsp+dword*5]
237 |.define SAVE_NRES, dword [rsp+dword*4]
238 |.define TMPa, aword [rsp+aword*1]
239 |.define TMP2, dword [rsp+dword*1]
240 |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
241 |//----- 16 byte aligned
243 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
244 |.define TMPQ, qword [rsp]
245 |.define TMP3, dword [rsp+aword*1]
246 |.define MULTRES, TMP2
250 |//-----------------------------------------------------------------------
252 |// Instruction headers.
253 |.macro ins_A; .endmacro
254 |.macro ins_AD; .endmacro
255 |.macro ins_AJ; .endmacro
256 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
257 |.macro ins_AB_; movzx RB, RCH; .endmacro
258 |.macro ins_A_C; movzx RC, RCL; .endmacro
259 |.macro ins_AND; not RDa; .endmacro
261 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
269 | jmp aword [DISPATCH+OP*8]
271 | jmp aword [DISPATCH+OP*4]
275 |// Instruction footer.
277 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
278 | .define ins_next, ins_NEXT
279 | .define ins_next_, ins_NEXT
281 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
282 | // Affects only certain kinds of benchmarks (and only with -j off).
283 | // Around 10%-30% slower on Core2, a lot more slower on P4.
293 |// Call decode and dispatch.
295 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
296 | mov PC, LFUNC:RB->pc
302 | jmp aword [DISPATCH+OP*8]
304 | jmp aword [DISPATCH+OP*4]
309 | // BASE = new base, RB = LFUNC, RD = nargs+1
314 |//-----------------------------------------------------------------------
316 |// Macros to test operand types.
317 |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
318 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
319 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
320 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
321 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
323 |// These operands must be used with movzx.
324 |.define PC_OP, byte [PC-4]
325 |.define PC_RA, byte [PC-3]
326 |.define PC_RB, byte [PC-1]
327 |.define PC_RC, byte [PC-2]
328 |.define PC_RD, word [PC-2]
330 |.macro branchPC, reg
331 | lea PC, [PC+reg*4-BCBIAS_J*4]
334 |// Assumes DISPATCH is relative to GL.
335 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
336 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
338 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
340 |// Decrement hashed hotcount and trigger trace recorder if zero.
344 | and reg, HOTCOUNT_PCMASK
345 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
352 | and reg, HOTCOUNT_PCMASK
353 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
357 |// Set current VM state.
358 |.macro set_vmstate, st
359 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
362 |// Annoying x87 stuff: support for two compare variants.
363 |.macro fcomparepp // Compare and pop st0 >< st1.
369 | fnstsw ax // eax modified!
374 |.macro fdup; fld st0; .endmacro
375 |.macro fpop1; fstp st1; .endmacro
377 |// Synthesize SSE FP constants.
378 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
380 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
382 | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
386 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
388 | mov64 tmp, U64x(val,00000000); movd reg, tmp
390 | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
394 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
395 | sseconst_hi reg, tmp, 80000000
397 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
398 | sseconst_hi reg, tmp, 3ff00000
400 |.macro sseconst_m1, reg, tmp // Synthesize -1.0.
401 | sseconst_hi reg, tmp, bff00000
403 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
404 | sseconst_hi reg, tmp, 43300000
406 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
407 | sseconst_hi reg, tmp, 43380000
410 |// Move table write barrier back. Overwrites reg.
411 |.macro barrierback, tab, reg
412 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
413 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
414 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
415 | mov tab->gclist, reg
418 |//-----------------------------------------------------------------------
420 /* Generate subroutines used by opcodes and other parts of the VM. */
421 /* The .code_sub section should be last to help static branch prediction. */
422 static void build_subroutines(BuildCtx *ctx, int cmov, int sse)
426 |//-----------------------------------------------------------------------
427 |//-- Return handling ----------------------------------------------------
428 |//-----------------------------------------------------------------------
434 | // Return from pcall or xpcall fast func.
436 | sub BASE, PC // Restore caller base.
437 | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
438 | mov PC, [BASE-4] // Fetch PC of previous frame.
439 | // Prepending may overwrite the pcall frame, so do it at the end.
440 | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
443 | add RD, 1 // RD = nresults+1
445 | test PC, FRAME_TYPE
446 | jz ->BC_RET_Z // Handle regular return to Lua.
449 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
451 | test PC, FRAME_TYPE
458 | neg PC // Previous base = BASE - delta.
462 |1: // Move results down.
469 | mov RB, [BASE+RA+4]
480 | mov RA, SAVE_NRES // RA = wanted nresults+1
483 | jne >6 // More/less results wanted?
486 | mov L:RB->top, BASE
489 | mov RAa, SAVE_CFRAME // Restore previous C frame.
490 | mov L:RB->cframe, RAa
491 | xor eax, eax // Ok return status for vm_pcall.
498 | jb >7 // Less results wanted?
499 | // More results wanted. Check stack size and fill up results with nil.
500 | cmp BASE, L:RB->maxstack
502 | mov dword [BASE-4], LJ_TNIL
507 |7: // Less results wanted.
509 | jz <5 // But check for LUA_MULTRET+1.
510 | sub RA, RD // Negative result!
511 | lea BASE, [BASE+RA*8] // Correct top.
514 |8: // Corner case: need to grow stack for filling up results.
515 | // This can happen if:
516 | // - A C function grows the stack (a lot).
517 | // - The GC shrinks the stack in between.
518 | // - A return back from a lua_call() with (high) nresults adjustment.
519 | mov L:RB->top, BASE // Save current top held in BASE (yes).
520 | mov MULTRES, RD // Need to fill only remainder with nil.
523 | call extern lj_state_growstack@8 // (lua_State *L, int n)
524 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
527 |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
528 | // (void *cframe, int errcode)
530 | mov eax, CARG2d // Error return status for vm_pcall.
533 | mov eax, FCARG2 // Error return status for vm_pcall.
536 |->vm_unwind_c_eh: // Landing pad for external unwinder.
538 | mov GL:RB, L:RB->glref
539 | mov dword GL:RB->vmstate, ~LJ_VMST_C
542 |->vm_unwind_rethrow:
543 |.if X64 and not X64WIN
547 | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
550 |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
553 | and CARG1, CFRAME_RAWMASK
556 | and FCARG1, CFRAME_RAWMASK
559 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
561 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
562 | mov RD, 1+1 // Really 1+2 results, incr. later.
563 | mov BASE, L:RB->base
564 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
565 | add DISPATCH, GG_G2DISP
566 | mov PC, [BASE-4] // Fetch PC of previous frame.
567 | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
569 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
571 |//-----------------------------------------------------------------------
572 |//-- Grow stack for calls -----------------------------------------------
573 |//-----------------------------------------------------------------------
575 |->vm_growstack_c: // Grow stack for C function.
576 | mov FCARG2, LUA_MINSTACK
579 |->vm_growstack_v: // Grow stack for vararg Lua function.
583 |->vm_growstack_f: // Grow stack for fixarg Lua function.
584 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
585 | lea RD, [BASE+NARGS:RD*8-8]
587 | movzx RA, byte [PC-4+PC2PROTO(framesize)]
588 | add PC, 4 // Must point after first instruction.
589 | mov L:RB->base, BASE
594 | // RB = L, L->base = new base, L->top = top
596 | call extern lj_state_growstack@8 // (lua_State *L, int n)
597 | mov BASE, L:RB->base
599 | mov LFUNC:RB, [BASE-8]
603 | // BASE = new base, RB = LFUNC, RD = nargs+1
604 | ins_callt // Just retry the call.
606 |//-----------------------------------------------------------------------
607 |//-- Entry points into the assembler VM ---------------------------------
608 |//-----------------------------------------------------------------------
610 |->vm_resume: // Setup C frame and resume thread.
611 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
614 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
619 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
623 | lea KBASEa, [esp+CFRAME_RESUME]
624 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
625 | add DISPATCH, GG_G2DISP
626 | mov L:RB->cframe, KBASEa
627 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
628 | mov SAVE_CFRAME, RDa
633 | cmp byte L:RB->status, RDL
634 | je >3 // Initial resume (like a call).
636 | // Resume after yield (like a return).
638 | mov byte L:RB->status, RDL
639 | mov BASE, L:RB->base
643 | add RD, 1 // RD = nresults+1
644 | sub RA, BASE // RA = resultofs
647 | test PC, FRAME_TYPE
651 |->vm_pcall: // Setup protected C frame and enter VM.
652 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
656 | mov SAVE_ERRF, CARG4d
660 |->vm_call: // Setup C frame and enter VM.
661 | // (lua_State *L, TValue *base, int nres1)
665 |1: // Entry point for vm_pcall above (PC = ftype).
667 | mov SAVE_NRES, CARG3d
668 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
673 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
676 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
677 | mov SAVE_CFRAME, KBASEa
678 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
680 | mov L:RB->cframe, rsp
682 | mov L:RB->cframe, esp
685 |2: // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype).
686 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
687 | add DISPATCH, GG_G2DISP
689 |3: // Entry point for vm_resume above (RA = base, RB = L, PC = ftype).
691 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
693 | sub PC, BASE // PC = frame delta + frame type
698 | add NARGS:RD, 1 // RD = nargs+1
701 | mov LFUNC:RB, [RA-8]
702 | cmp dword [RA-4], LJ_TFUNC
703 | jne ->vmeta_call // Ensure KBASE defined and != BASE.
705 |->vm_call_dispatch_f:
708 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
710 |->vm_cpcall: // Setup protected C frame, call C.
711 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
714 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
718 | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
719 | mov RC, INARG_CP_UD // Get args before they are overwritten.
720 | mov RA, INARG_CP_FUNC
721 | mov BASE, INARG_CP_CALL
723 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
725 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
726 | sub KBASE, L:RB->top
727 | mov SAVE_ERRF, 0 // No error function.
728 | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
729 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
732 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
733 | mov SAVE_CFRAME, KBASEa
734 | mov L:RB->cframe, rsp
736 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
738 | mov ARG3, RC // Have to copy args downwards.
742 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
743 | mov SAVE_CFRAME, KBASE
744 | mov L:RB->cframe, esp
746 | call BASE // (lua_State *L, lua_CFunction func, void *ud)
748 | // TValue * (new base) or NULL returned in eax (RC).
750 | jz ->vm_leave_cp // No base? Just remove C frame.
753 | jmp <2 // Else continue with the call.
755 |//-----------------------------------------------------------------------
756 |//-- Metamethod handling ------------------------------------------------
757 |//-----------------------------------------------------------------------
759 |//-- Continuation dispatch ----------------------------------------------
762 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
766 | sub BASE, PC // Restore caller BASE.
767 | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
768 | mov RC, RA // ... in [RC]
769 | mov PC, [RB-12] // Restore PC from [cont|PC].
771 | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
776 | lea KBASEa, qword [=>0]
779 | mov RA, dword [RB-16]
785 | mov LFUNC:KBASE, [BASE-8]
786 | mov KBASE, LFUNC:KBASE->pc
787 | mov KBASE, [KBASE+PC2PROTO(k)]
788 | // BASE = base, RC = result, RB = meta base
789 | jmp RAa // Jump to continuation.
793 | je ->cont_ffi_callback // cont = 1: return from FFI callback.
794 | // cont = 0: Tail call from C function.
801 |->cont_cat: // BASE = base, RC = result, RB = mbase
804 | lea RA, [BASE+RA*8]
811 | mov L:CARG1d, SAVE_L
812 | mov L:CARG1d->base, BASE
817 | mov L:CARG1d, SAVE_L
818 | mov L:CARG1d->base, BASE
833 |//-- Table indexing metamethods -----------------------------------------
836 | mov TMP1, RC // RC = GCstr *
838 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
841 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
842 | mov [RA], TAB:RB // RB = GCtab *
843 | mov dword [RA+4], LJ_TTAB
850 | mov TMP2, LJ_TISNUM
862 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
866 | movzx RC, PC_RC // Reload TValue *k from RC.
867 | lea RC, [BASE+RC*8]
869 | movzx RB, PC_RB // Reload TValue *t from RB.
870 | lea RB, [BASE+RB*8]
873 | mov L:CARG1d, SAVE_L
874 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
876 | mov CARG3, RCa // May be 64 bit ptr to stack.
883 | mov L:RB->base, BASE
886 | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
887 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
888 | mov BASE, L:RB->base
891 |->cont_ra: // BASE = base, RC = result
895 | mov [BASE+RA*8], RBa
899 | mov [BASE+RA*8+4], RB
900 | mov [BASE+RA*8], RC
904 |3: // Call __index metamethod.
905 | // BASE = base, L->top = new base, stack = cont/func/t/k
907 | mov [RA-12], PC // [cont|PC]
908 | lea PC, [RA+FRAME_CONT]
910 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
911 | mov NARGS:RD, 2+1 // 2 args for func(t, k).
912 | jmp ->vm_call_dispatch_f
914 |//-----------------------------------------------------------------------
917 | mov TMP1, RC // RC = GCstr *
919 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
922 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
923 | mov [RA], TAB:RB // RB = GCtab *
924 | mov dword [RA+4], LJ_TTAB
931 | mov TMP2, LJ_TISNUM
943 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
947 | movzx RC, PC_RC // Reload TValue *k from RC.
948 | lea RC, [BASE+RC*8]
950 | movzx RB, PC_RB // Reload TValue *t from RB.
951 | lea RB, [BASE+RB*8]
954 | mov L:CARG1d, SAVE_L
955 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
957 | mov CARG3, RCa // May be 64 bit ptr to stack.
964 | mov L:RB->base, BASE
967 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
968 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
969 | mov BASE, L:RB->base
972 | // NOBARRIER: lj_meta_tset ensures the table is not black.
975 | mov RBa, [BASE+RA*8]
978 | mov RB, [BASE+RA*8+4]
979 | mov RA, [BASE+RA*8]
983 |->cont_nop: // BASE = base, (RC = result)
986 |3: // Call __newindex metamethod.
987 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
989 | mov [RA-12], PC // [cont|PC]
991 | // Copy value to third argument.
993 | mov RBa, [BASE+RC*8]
996 | mov RB, [BASE+RC*8+4]
997 | mov RC, [BASE+RC*8]
1001 | lea PC, [RA+FRAME_CONT]
1003 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
1004 | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
1005 | jmp ->vm_call_dispatch_f
1007 |//-- Comparison metamethods ---------------------------------------------
1012 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
1014 | lea CARG3d, [BASE+RD*8]
1015 | lea CARG2d, [BASE+RA*8]
1017 | lea CARG2d, [BASE+RA*8]
1018 | lea CARG3d, [BASE+RD*8]
1020 | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
1021 | movzx CARG4d, PC_OP
1024 | lea RD, [BASE+RD*8]
1025 | lea RA, [BASE+RA*8]
1031 | mov L:RB->base, BASE
1034 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1035 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1037 | mov BASE, L:RB->base
1049 |->cont_condt: // BASE = base, RC = result
1051 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
1055 |->cont_condf: // BASE = base, RC = result
1056 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
1065 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1067 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1070 | mov CARG4d, RB // Caveat: CARG4d == RA.
1072 | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
1081 | mov L:RB->base, BASE
1084 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1085 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1092 | mov L:RB->base, BASE
1094 | mov FCARG2, dword [PC-4]
1096 | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
1097 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1101 |//-- Arithmetic metamethods ---------------------------------------------
1108 | lea RC, [KBASE+RC*8]
1116 | lea RC, [KBASE+RC*8]
1117 | lea RB, [BASE+RB*8]
1122 | lea RC, [BASE+RD*8]
1131 | lea RC, [BASE+RC*8]
1133 | lea RB, [BASE+RB*8]
1135 | lea RA, [BASE+RA*8]
1142 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1144 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1146 | movzx CARG5d, PC_OP
1148 | mov CARG4d, RC // Caveat: CARG4d == RA.
1149 | mov L:CARG1d, SAVE_L
1150 | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
1152 | mov L:RB, L:CARG1d
1161 | mov L:RB->base, BASE
1164 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1165 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1166 | mov BASE, L:RB->base
1170 | // Call metamethod for binary op.
1172 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1175 | mov [RA-12], PC // [cont|PC]
1176 | lea PC, [RC+FRAME_CONT]
1177 | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
1178 | jmp ->vm_call_dispatch
1182 | mov L:RB->base, BASE
1183 | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
1184 | mov L:FCARG1, L:RB
1186 | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
1187 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1188 | mov BASE, L:RB->base
1189 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1191 | jne ->vmeta_binop // Binop call for compatibility.
1193 | mov TAB:FCARG1, [BASE+RD*8]
1196 | jmp ->vmeta_binop // Binop call for compatibility.
1199 |//-- Call metamethod ----------------------------------------------------
1202 | lea RA, [BASE+RA*8+8]
1203 |->vmeta_call: // Resolve and call __call metamethod.
1204 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1205 | mov TMP2, RA // Save RA, RC for us.
1206 | mov TMP1, NARGS:RD
1210 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1212 | lea CARG3d, [RA+NARGS:RD*8]
1213 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
1215 | lea RC, [RA+NARGS:RD*8]
1220 | mov L:RB->base, BASE // This is the callers base!
1223 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1224 | mov BASE, L:RB->base
1226 | mov NARGS:RD, TMP1
1227 | mov LFUNC:RB, [RA-8]
1229 | // This is fragile. L->base must not move, KBASE must always be defined.
1230 | cmp KBASE, BASE // Continue with CALLT if flag set.
1233 | ins_call // Otherwise call resolved metamethod.
1235 |//-- Argument coercion for 'for' statement ------------------------------
1239 | mov L:RB->base, BASE
1240 | mov FCARG2, RA // Caveat: FCARG2 == BASE
1241 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
1243 | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
1244 | mov BASE, L:RB->base
1250 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1252 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
1255 |//-----------------------------------------------------------------------
1256 |//-- Fast functions -----------------------------------------------------
1257 |//-----------------------------------------------------------------------
1259 |.macro .ffunc, name
1263 |.macro .ffunc_1, name
1265 | cmp NARGS:RD, 1+1; jb ->fff_fallback
1268 |.macro .ffunc_2, name
1270 | cmp NARGS:RD, 2+1; jb ->fff_fallback
1273 |.macro .ffunc_n, name
1275 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1279 |.macro .ffunc_n, name, op
1281 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1286 |.macro .ffunc_nsse, name, op
1288 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1289 | op xmm0, qword [BASE]
1292 |.macro .ffunc_nsse, name
1293 | .ffunc_nsse name, movsd
1296 |.macro .ffunc_nn, name
1298 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1299 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1301 | fld qword [BASE+8]
1304 |.macro .ffunc_nnsse, name
1306 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1307 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1308 | movsd xmm0, qword [BASE]
1309 | movsd xmm1, qword [BASE+8]
1312 |.macro .ffunc_nnr, name
1314 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1315 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1316 | fld qword [BASE+8]
1320 |// Inlined GC threshold check. Caveat: uses label 1.
1322 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1323 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1329 |//-- Base library: checks -----------------------------------------------
1333 | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
1373 | jbe >1; mov RC, RB; 1:
1376 | mov CFUNC:RB, [BASE-8]
1377 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1379 | mov dword [BASE-4], LJ_TSTR
1380 | mov [BASE-8], STR:RC
1384 | mov RC, ~LJ_TLIGHTUD
1388 |//-- Base library: getters and setters ---------------------------------
1390 |.ffunc_1 getmetatable
1393 | cmp RB, LJ_TTAB; jne >6
1394 |1: // Field metatable must be at same offset for GCtab and GCudata!
1395 | mov TAB:RB, [BASE]
1396 | mov TAB:RB, TAB:RB->metatable
1398 | test TAB:RB, TAB:RB
1399 | mov dword [BASE-4], LJ_TNIL
1401 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1402 | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
1403 | mov [BASE-8], TAB:RB
1404 | mov RA, TAB:RB->hmask
1405 | and RA, STR:RC->hash
1407 | add NODE:RA, TAB:RB->node
1408 |3: // Rearranged logic, because we expect _not_ to find the key.
1409 | cmp dword NODE:RA->key.it, LJ_TSTR
1411 | cmp dword NODE:RA->key.gcr, STR:RC
1414 | mov NODE:RA, NODE:RA->next
1415 | test NODE:RA, NODE:RA
1417 | jmp ->fff_res1 // Not found, keep default result.
1420 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1422 | mov [BASE-4], RB // Return value of mt.__metatable.
1427 | cmp RB, LJ_TUDATA; je <1
1429 | cmp RB, LJ_TNUMX; ja >8
1430 | cmp RB, LJ_TISNUM; jbe >7
1431 | mov RB, LJ_TLIGHTUD
1435 | cmp RB, LJ_TISNUM; ja >8
1440 | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1443 |.ffunc_2 setmetatable
1444 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1445 | // Fast path: no mt for table yet and not clearing the mt.
1446 | mov TAB:RB, [BASE]
1447 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1448 | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
1449 | mov TAB:RC, [BASE+8]
1450 | mov TAB:RB->metatable, TAB:RC
1452 | mov dword [BASE-4], LJ_TTAB // Return original table.
1453 | mov [BASE-8], TAB:RB
1454 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1456 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1457 | barrierback TAB:RB, RC
1462 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1464 | mov RB, BASE // Save BASE.
1465 | lea CARG3d, [BASE+8]
1466 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1467 | mov CARG1d, SAVE_L
1469 | mov RB, BASE // Save BASE.
1470 | mov CARG2d, [BASE]
1471 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1472 | mov CARG1d, SAVE_L
1474 | mov TAB:RD, [BASE]
1478 | mov RB, BASE // Save BASE.
1482 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1483 | // cTValue * returned in eax (RD).
1484 | mov BASE, RB // Restore BASE.
1485 | // Copy table slot.
1499 |//-- Base library: conversions ------------------------------------------
1502 | // Only handles the number case inline (without a base argument).
1503 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
1504 | cmp dword [BASE+4], LJ_TISNUM
1507 | mov RB, dword [BASE]; jmp ->fff_resi
1511 | jae ->fff_fallback
1514 | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1516 | fld qword [BASE]; jmp ->fff_resn
1520 | // Only handles the string or number case inline.
1522 | cmp dword [BASE+4], LJ_TSTR; jne >3
1523 | // A __tostring method in the string base metatable is ignored.
1524 | mov STR:RD, [BASE]
1526 | mov dword [BASE-4], LJ_TSTR
1527 | mov [BASE-8], STR:RD
1529 |3: // Handle numbers inline, unless a number base metatable is present.
1530 | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
1531 | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1532 | jne ->fff_fallback
1533 | ffgccheck // Caveat: uses label 1.
1535 | mov L:RB->base, BASE // Add frame since C call can throw.
1536 | mov SAVE_PC, PC // Redundant (but a defined value).
1537 |.if X64 and not X64WIN
1538 | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
1540 | mov L:FCARG1, L:RB
1542 | call extern lj_str_fromnumber@8 // (lua_State *L, cTValue *o)
1544 | call extern lj_str_fromnum@8 // (lua_State *L, lua_Number *np)
1546 | // GCstr returned in eax (RD).
1547 | mov BASE, L:RB->base
1550 |//-- Base library: iterators -------------------------------------------
1553 | je >2 // Missing 2nd arg?
1555 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1557 | mov L:RB->base, BASE // Add frame since C call can throw.
1558 | mov L:RB->top, BASE // Dummy frame length is ok.
1561 | lea CARG3d, [BASE+8]
1562 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1565 | mov CARG2d, [BASE]
1566 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1569 | mov TAB:RD, [BASE]
1575 | mov SAVE_PC, PC // Redundant (but a defined value).
1576 | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1577 | // Flag returned in eax (RD).
1578 | mov BASE, L:RB->base
1579 | test RD, RD; jz >3 // End of traversal?
1580 | // Copy key and value to results.
1583 | mov RDa, [BASE+16]
1599 |2: // Set missing 2nd arg to nil.
1600 | mov dword [BASE+12], LJ_TNIL
1602 |3: // End of traversal: return nil.
1603 | mov dword [BASE-4], LJ_TNIL
1607 | mov TAB:RB, [BASE]
1608 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1609 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1610 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1612 | mov CFUNC:RB, [BASE-8]
1613 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1615 | mov dword [BASE-4], LJ_TFUNC
1616 | mov [BASE-8], CFUNC:RD
1617 | mov dword [BASE+12], LJ_TNIL
1621 |.ffunc_1 ipairs_aux
1622 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1623 | cmp dword [BASE+12], LJ_TISNUM
1625 | jne ->fff_fallback
1627 | jae ->fff_fallback
1631 | mov RD, dword [BASE+8]
1633 | mov dword [BASE-4], LJ_TISNUM
1634 | mov dword [BASE-8], RD
1636 | movsd xmm0, qword [BASE+8]
1637 | sseconst_1 xmm1, RBa
1640 | movsd qword [BASE-8], xmm0
1643 | fld qword [BASE+8]
1647 | fstp qword [BASE-8]
1651 | mov TAB:RB, [BASE]
1652 | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
1654 | add RD, TAB:RB->array
1656 | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
1657 | // Copy array slot.
1668 |2: // Check for empty hash part first. Otherwise call C function.
1669 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1670 | mov FCARG1, TAB:RB
1671 | mov RB, BASE // Save BASE.
1672 | mov FCARG2, RD // Caveat: FCARG2 == BASE
1673 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1674 | // cTValue * or NULL returned in eax (RD).
1683 | mov TAB:RB, [BASE]
1684 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1685 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1686 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1688 | mov CFUNC:RB, [BASE-8]
1689 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1691 | mov dword [BASE-4], LJ_TFUNC
1692 | mov [BASE-8], CFUNC:RD
1694 | mov dword [BASE+12], LJ_TISNUM
1695 | mov dword [BASE+8], 0
1698 | movsd qword [BASE+8], xmm0
1701 | fstp qword [BASE+8]
1706 |//-- Base library: catch errors ----------------------------------------
1711 | mov PC, 8+FRAME_PCALL
1713 | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1714 | shr RB, HOOK_ACTIVE_SHIFT
1716 | add PC, RB // Remember active hook before pcall.
1717 | jmp ->vm_call_dispatch
1720 | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
1721 | mov RB, [BASE+4] // Swap function and traceback.
1723 | mov dword [BASE+4], LJ_TFUNC
1724 | mov LFUNC:RB, [BASE]
1726 | mov [BASE+8], LFUNC:RB
1730 | mov PC, 16+FRAME_PCALL
1733 |//-- Coroutine library --------------------------------------------------
1735 |.macro coroutine_resume_wrap, resume
1737 |.ffunc_1 coroutine_resume
1740 |.ffunc coroutine_wrap_aux
1741 | mov CFUNC:RB, [BASE-8]
1742 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1752 | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
1754 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1755 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1757 | je >1 // Status != LUA_YIELD (i.e. 0)?
1758 | cmp RA, L:RB->base // Check for presence of initial func.
1762 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1764 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1766 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1770 | mov L:RB->base, BASE
1772 | add BASE, 8 // Keep resumed thread in stack for GC.
1774 | mov L:RB->top, BASE
1776 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1778 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1780 | sub RBa, PCa // Relative to PC.
1784 |2: // Move args to coroutine.
1807 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1808 | set_vmstate INTERP
1814 | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
1816 | mov BASE, L:RB->base
1817 | cmp eax, LUA_YIELD
1820 | mov RA, L:PC->base
1821 | mov KBASE, L:PC->top
1822 | mov L:PC->top, RA // Clear coroutine stack.
1825 | je >6 // No results?
1828 | cmp RD, L:RB->maxstack
1829 | ja >9 // Need to grow stack?
1833 |5: // Move results from coroutine.
1848 | lea RD, [PC+2] // nresults+1 = 1 + true + results.
1849 | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
1851 | lea RD, [PC+1] // nresults+1 = 1 + results.
1861 | test PC, FRAME_TYPE
1865 |8: // Coroutine returned with error (at co->top-1).
1867 | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
1870 | mov L:PC->top, RA // Clear error from coroutine stack.
1871 | // Copy error message.
1881 | mov RD, 1+2 // nresults+1 = 1 + false + error.
1886 | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
1887 | // Error function does not return.
1890 |9: // Handle stack expansion on return from yield.
1894 | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
1896 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1899 | call extern lj_state_growstack@8 // (lua_State *L, int n)
1905 | mov BASE, L:RB->base
1906 | jmp <4 // Retry the stack move.
1909 | coroutine_resume_wrap 1 // coroutine.resume
1910 | coroutine_resume_wrap 0 // coroutine.wrap
1912 |.ffunc coroutine_yield
1914 | test aword L:RB->cframe, CFRAME_RESUME
1916 | mov L:RB->base, BASE
1917 | lea RD, [BASE+NARGS:RD*8-8]
1920 | mov aword L:RB->cframe, RDa
1922 | mov byte L:RB->status, al
1923 | jmp ->vm_leave_unw
1925 |//-- Math library -------------------------------------------------------
1928 |->fff_resi: // Dummy.
1933 | fstp qword [BASE-8]
1938 | cmp dword [BASE+4], LJ_TISNUM; jne >2
1939 | mov RB, dword [BASE]
1940 | cmp RB, 0; jns ->fff_resi
1945 | mov dword [BASE-4], LJ_TISNUM
1946 | mov dword [BASE-8], RB
1950 | mov dword [BASE-4], 0x41e00000 // 2^31.
1951 | mov dword [BASE-8], 0
1956 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1959 | movsd xmm0, qword [BASE]
1960 | sseconst_abs xmm1, RDa
1964 | movsd qword [BASE-8], xmm0
1970 |->fff_resxmm0: // Dummy.
1973 | fstp qword [BASE-8]
1980 | test PC, FRAME_TYPE
1983 | cmp PC_RB, RDL // More results expected?
1985 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1987 | not RAa // Note: ~RA = -(RA+1)
1988 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
1991 |6: // Fill up results with nil.
1992 | mov dword [BASE+RD*8-12], LJ_TNIL
1996 |7: // Non-standard return case.
1997 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
2000 |.macro math_round, func
2001 | .ffunc math_ .. func
2003 | cmp dword [BASE+4], LJ_TISNUM; jne >1
2004 | mov RB, dword [BASE]; jmp ->fff_resi
2008 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2011 | movsd xmm0, qword [BASE]
2012 | call ->vm_ .. func
2013 || if (LJ_DUALNUM) {
2015 | cmp RB, 0x80000000
2018 | ucomisd xmm0, xmm1
2025 | call ->vm_ .. func
2026 || if (LJ_DUALNUM) {
2030 | cmp RB, 0x80000000; jne >2
2050 |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2052 |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
2054 |.ffunc_n math_log, fldln2; fyl2x; jmp ->fff_resn
2055 |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn
2056 |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn
2058 |.ffunc_n math_sin; fsin; jmp ->fff_resn
2059 |.ffunc_n math_cos; fcos; jmp ->fff_resn
2060 |.ffunc_n math_tan; fptan; fpop; jmp ->fff_resn
2063 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan
2066 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan
2068 |.ffunc_n math_atan; fld1; fpatan; jmp ->fff_resn
2070 |.macro math_extern, func
2072 | .ffunc_nsse math_ .. func
2074 | movsd FPARG1, xmm0
2078 | .ffunc_n math_ .. func
2083 | call extern lj_vm_ .. func
2098 |.ffunc_nsse math_rad
2099 | mov CFUNC:RB, [BASE-8]
2100 | mulsd xmm0, qword CFUNC:RB->upvalue[0]
2104 | mov CFUNC:RB, [BASE-8]
2105 | fmul qword CFUNC:RB->upvalue[0]
2109 |.ffunc_nn math_atan2; fpatan; jmp ->fff_resn
2110 |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
2112 |.ffunc_1 math_frexp
2114 | cmp RB, LJ_TISNUM; jae ->fff_fallback
2117 | mov [BASE-4], RB; mov [BASE-8], RC
2118 | shl RB, 1; cmp RB, 0xffe00000; jae >3
2121 | cmp RB, 0x00200000; jb >4
2123 | shr RB, 21; sub RB, RC // Extract and unbias exponent.
2127 | mov TMP1, RB; fild TMP1
2130 | and RB, 0x800fffff // Mask off exponent.
2131 | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
2135 | movsd qword [BASE], xmm0
2141 |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2143 | xorps xmm0, xmm0; jmp <2
2147 |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
2149 | movsd xmm0, qword [BASE]
2150 | sseconst_hi xmm1, RBa, 43500000 // 2^54.
2152 | movsd qword [BASE-8], xmm0
2155 | mov TMP1, 0x5a800000; fmul TMP1 // x = x*2^54
2156 | fstp qword [BASE-8]
2158 | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2161 |.ffunc_nsse math_modf
2167 | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
2173 | movsd qword [BASE-8], xmm0
2174 | movsd qword [BASE], xmm4
2180 | fstp qword [BASE-8]
2183 | mov RC, [BASE-4]; mov RB, [BASE+4]
2184 | xor RC, RB; js >3 // Need to adjust sign?
2189 | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
2193 | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
2195 | fldz; fxch; jmp <1 // Return +-Inf and +-0.
2198 |.ffunc_nnr math_fmod
2199 |1: ; fprem; fnstsw ax; sahf; jp <1
2204 |.ffunc_nnsse math_pow; call ->vm_pow; jmp ->fff_resxmm0
2206 |.ffunc_nn math_pow; call ->vm_pow; jmp ->fff_resn
2209 |.macro math_minmax, name, cmovop, fcmovop, nofcmovop, sseop
2212 | cmp dword [BASE+4], LJ_TISNUM
2215 | mov RB, dword [BASE]
2216 |1: // Handle integers.
2217 | cmp RA, RD; jae ->fff_resi
2218 | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2219 | cmp RB, dword [BASE+RA*8-8]
2220 | cmovop RB, dword [BASE+RA*8-8]
2225 | // Convert intermediate result to number and continue below.
2238 | jae ->fff_fallback
2242 | movsd xmm0, qword [BASE]
2243 |5: // Handle numbers or integers.
2244 | cmp RA, RD; jae ->fff_resxmm0
2245 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2249 | cvtsi2sd xmm1, dword [BASE+RA*8-8]
2252 | jae ->fff_fallback
2255 | movsd xmm1, qword [BASE+RA*8-8]
2263 |5: // Handle numbers or integers.
2264 | cmp RA, RD; jae ->fff_resn
2265 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2269 | fild dword [BASE+RA*8-8]
2275 | fld qword [BASE+RA*8-8]
2278 | fucomi st1; fcmovop st1; fpop1
2281 | fucom st1; fnstsw ax; test ah, 1; nofcmovop >2; fxch; 2: ; fpop
2290 | math_minmax math_min, cmovg, fcmovnbe, jz, minsd
2291 | math_minmax math_max, cmovl, fcmovbe, jnz, maxsd
2294 | fpop; jmp ->fff_fallback
2297 |//-- String library -----------------------------------------------------
2299 |.ffunc_1 string_len
2300 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2301 | mov STR:RB, [BASE]
2303 | mov RB, dword STR:RB->len; jmp ->fff_resi
2305 | cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0
2307 | fild dword STR:RB->len; jmp ->fff_resn
2310 |.ffunc string_byte // Only handle the 1-arg case here.
2311 | cmp NARGS:RD, 1+1; jne ->fff_fallback
2312 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2313 | mov STR:RB, [BASE]
2315 | cmp dword STR:RB->len, 1
2316 | jb ->fff_res0 // Return no results for empty string.
2317 | movzx RB, byte STR:RB[1]
2321 | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2323 | mov TMP1, RB; fild TMP1; jmp ->fff_resn
2326 |.ffunc string_char // Only handle the 1-arg case here.
2328 | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
2329 | cmp dword [BASE+4], LJ_TISNUM
2331 | jne ->fff_fallback
2332 | mov RB, dword [BASE]
2333 | cmp RB, 255; ja ->fff_fallback
2336 | jae ->fff_fallback
2337 | cvttsd2si RB, qword [BASE]
2338 | cmp RB, 255; ja ->fff_fallback
2341 | jae ->fff_fallback
2344 | cmp TMP2, 255; ja ->fff_fallback
2351 | lea RDa, TMP2 // Points to stack. Little-endian.
2354 | mov L:RB->base, BASE
2356 | mov CARG3d, TMP3 // Zero-extended to size_t.
2357 | mov CARG2, RDa // May be 64 bit ptr to stack.
2364 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
2365 | // GCstr * returned in eax (RD).
2366 | mov BASE, L:RB->base
2368 | mov dword [BASE-4], LJ_TSTR
2369 | mov [BASE-8], STR:RD
2375 | cmp NARGS:RD, 1+2; jb ->fff_fallback
2377 | cmp dword [BASE+20], LJ_TISNUM
2379 | jne ->fff_fallback
2380 | mov RB, dword [BASE+16]
2383 | jae ->fff_fallback
2384 | cvttsd2si RB, qword [BASE+16]
2387 | jae ->fff_fallback
2388 | fld qword [BASE+16]
2392 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2393 | cmp dword [BASE+12], LJ_TISNUM
2395 | jne ->fff_fallback
2397 | jae ->fff_fallback
2399 | mov STR:RB, [BASE]
2401 | mov RB, STR:RB->len
2403 | mov RA, dword [BASE+8]
2405 | cvttsd2si RA, qword [BASE+8]
2408 | fld qword [BASE+8]
2414 | cmp RB, RC // len < end? (unsigned compare)
2417 | test RA, RA // start <= 0?
2421 | sub RC, RA // start > end?
2423 | lea RB, [STR:RB+RA+#STR-1]
2434 |5: // Negative end or overflow.
2436 | lea RC, [RC+RB+1] // end = end+(len+1)
2439 | mov RC, RB // end = len
2442 |7: // Negative start or underflow.
2444 | add RA, RB // start = start+(len+1)
2446 | jg <3 // start > 0?
2448 | mov RA, 1 // start = 1
2451 |->fff_emptystr: // Range underflow.
2452 | xor RC, RC // Zero length. Any ptr in RB is ok.
2455 |.ffunc_2 string_rep // Only handle the 1-char case inline.
2457 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2458 | cmp dword [BASE+12], LJ_TISNUM
2459 | mov STR:RB, [BASE]
2461 | jne ->fff_fallback
2462 | mov RC, dword [BASE+8]
2464 | jae ->fff_fallback
2465 | cvttsd2si RC, qword [BASE+8]
2467 | jae ->fff_fallback
2468 | fld qword [BASE+8]
2473 | jle ->fff_emptystr // Count <= 0? (or non-int)
2474 | cmp dword STR:RB->len, 1
2475 | jb ->fff_emptystr // Zero length string?
2476 | jne ->fff_fallback_2 // Fallback for > 1-char strings.
2477 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_2
2478 | movzx RA, byte STR:RB[1]
2479 | mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2485 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2490 | mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2493 |.ffunc_1 string_reverse
2495 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2496 | mov STR:RB, [BASE]
2497 | mov RC, STR:RB->len
2499 | jz ->fff_emptystr // Zero length string?
2500 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2502 | mov TMP2, PC // Need another temp register.
2508 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2510 | movzx RA, byte [RB]
2519 |.macro ffstring_case, name, lo, hi
2522 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2523 | mov STR:RB, [BASE]
2524 | mov RC, STR:RB->len
2525 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2527 | mov TMP2, PC // Need another temp register.
2533 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2535 |1: // ASCII case conversion. Yes, this is suboptimal code (do you care?).
2536 | movzx RA, byte [RB+RC]
2552 |ffstring_case string_lower, 0x41, 0x5a
2553 |ffstring_case string_upper, 0x61, 0x7a
2555 |//-- Table library ------------------------------------------------------
2557 |.ffunc_1 table_getn
2558 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
2559 | mov RB, BASE // Save BASE.
2560 | mov TAB:FCARG1, [BASE]
2561 | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t)
2562 | // Length of table returned in eax (RD).
2563 | mov BASE, RB // Restore BASE.
2565 | mov RB, RD; jmp ->fff_resi
2567 | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
2570 | mov ARG1, RD; fild ARG1; jmp ->fff_resn
2574 |//-- Bit library --------------------------------------------------------
2576 |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).
2578 |.macro .ffunc_bit, name, kind
2582 | sseconst_tobit xmm1, RBa
2584 | mov TMP1, TOBIT_BIAS
2587 | cmp dword [BASE+4], LJ_TISNUM
2590 | mov RB, dword [BASE]
2599 | jae ->fff_fallback
2602 | movsd xmm0, qword [BASE]
2604 | sseconst_tobit xmm1, RBa
2612 | mov TMP1, TOBIT_BIAS
2624 |.ffunc_bit bit_tobit, 0
2625 if (LJ_DUALNUM || sse) {
2639 |.macro .ffunc_bit_op, name, ins
2640 | .ffunc_bit name, 2
2641 | mov TMP2, NARGS:RD // Save for fallback.
2642 | lea RD, [BASE+NARGS:RD*8-16]
2646 | cmp dword [RD+4], LJ_TISNUM
2649 | ins RB, dword [RD]
2653 | ja ->fff_fallback_bit_op
2655 | jae ->fff_fallback_bit_op
2658 | movsd xmm0, qword [RD]
2674 |.ffunc_bit_op bit_band, and
2675 |.ffunc_bit_op bit_bor, or
2676 |.ffunc_bit_op bit_bxor, xor
2678 |.ffunc_bit bit_bswap, 1
2682 |.ffunc_bit bit_bnot, 1
2699 |->fff_fallback_bit_op:
2700 | mov NARGS:RD, TMP2 // Restore for fallback
2701 | jmp ->fff_fallback
2703 |.macro .ffunc_bit_sh, name, ins
2705 | .ffunc_bit name, 1
2706 | // Note: no inline conversion from number for 2nd argument!
2707 | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2708 | mov RA, dword [BASE+8]
2711 | sseconst_tobit xmm2, RBa
2719 | mov TMP1, TOBIT_BIAS
2728 | ins RB, cl // Assumes RA is ecx.
2732 |.ffunc_bit_sh bit_lshift, shl
2733 |.ffunc_bit_sh bit_rshift, shr
2734 |.ffunc_bit_sh bit_arshift, sar
2735 |.ffunc_bit_sh bit_rol, rol
2736 |.ffunc_bit_sh bit_ror, ror
2738 |//-----------------------------------------------------------------------
2741 | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
2742 | jmp ->fff_fallback
2744 | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
2745 |->fff_fallback: // Call fast function fallback handler.
2746 | // BASE = new base, RD = nargs+1
2748 | mov PC, [BASE-4] // Fallback may overwrite PC.
2749 | mov SAVE_PC, PC // Redundant (but a defined value).
2750 | mov L:RB->base, BASE
2751 | lea RD, [BASE+NARGS:RD*8-8]
2752 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2754 | mov CFUNC:RD, [BASE-8]
2755 | cmp RA, L:RB->maxstack
2756 | ja >5 // Need to grow stack.
2762 | call aword CFUNC:RD->f // (lua_State *L)
2763 | mov BASE, L:RB->base
2764 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2765 | test RD, RD; jg ->fff_res // Returned nresults+1?
2771 | lea NARGS:RD, [RA+1]
2772 | mov LFUNC:RB, [BASE-8]
2773 | jne ->vm_call_tail // Returned -1?
2774 | ins_callt // Returned 0: retry fast path.
2776 |// Reconstruct previous base for vmeta_call during tailcall.
2779 | test PC, FRAME_TYPE
2782 | not RBa // Note: ~RB = -(RB+1)
2783 | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
2784 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2789 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2791 |5: // Grow stack for fallback handler.
2792 | mov FCARG2, LUA_MINSTACK
2794 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2795 | mov BASE, L:RB->base
2796 | xor RD, RD // Simulate a return 0.
2797 | jmp <1 // Dumb retry (goes through ff first).
2799 |->fff_gcstep: // Call GC step function.
2800 | // BASE = new base, RD = nargs+1
2801 | pop RBa // Must keep stack at same level.
2802 | mov TMPa, RBa // Save return address
2804 | mov SAVE_PC, PC // Redundant (but a defined value).
2805 | mov L:RB->base, BASE
2806 | lea RD, [BASE+NARGS:RD*8-8]
2809 | call extern lj_gc_step@4 // (lua_State *L)
2810 | mov BASE, L:RB->base
2816 | push RBa // Restore return address.
2819 |//-----------------------------------------------------------------------
2820 |//-- Special dispatch targets -------------------------------------------
2821 |//-----------------------------------------------------------------------
2823 |->vm_record: // Dispatch target for recording phase.
2825 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2826 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2828 | // Decrement the hookcount for consistency, but always do the call.
2829 | test RDL, HOOK_ACTIVE
2831 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2833 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2837 |->vm_rethook: // Dispatch target for return hooks.
2838 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2839 | test RDL, HOOK_ACTIVE // Hook already active?
2843 |->vm_inshook: // Dispatch target for instr/line hooks.
2844 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2845 | test RDL, HOOK_ACTIVE // Hook already active?
2848 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2850 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2852 | test RDL, LUA_MASKLINE
2856 | mov L:RB->base, BASE
2857 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2859 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2860 | call extern lj_dispatch_ins@8 // (lua_State *L, BCIns *pc)
2862 | mov BASE, L:RB->base
2869 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2871 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
2874 |->cont_hook: // Continue from hook yield.
2877 | mov MULTRES, RA // Restore MULTRES for *M ins.
2880 |->vm_hotloop: // Hot loop counter underflow.
2882 | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
2883 | mov RB, LFUNC:RB->pc
2884 | movzx RD, byte [RB+PC2PROTO(framesize)]
2885 | lea RD, [BASE+RD*8]
2887 | mov L:RB->base, BASE
2890 | lea FCARG1, [DISPATCH+GG_DISP2J]
2891 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2893 | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
2897 |->vm_callhook: // Dispatch target for call hooks.
2903 |->vm_hotcall: // Hot call counter underflow.
2906 | or PC, 1 // Marker for hot call.
2909 | lea RD, [BASE+NARGS:RD*8-8]
2911 | mov L:RB->base, BASE
2915 | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
2916 | // ASMFunction returned in eax/rax (RDa).
2917 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2921 | mov BASE, L:RB->base
2931 |//-----------------------------------------------------------------------
2932 |//-- Trace exit handler -------------------------------------------------
2933 |//-----------------------------------------------------------------------
2935 |// Called from an exit stub with the exit number on the stack.
2936 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2940 | push r13; push r12
2941 | push r11; push r10; push r9; push r8
2942 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2943 | push rbx; push rdx; push rcx; push rax
2944 | movzx RC, byte [rbp-8] // Reconstruct exit number.
2945 | mov RCH, byte [rbp-16]
2946 | mov [rbp-8], r15; mov [rbp-16], r14
2948 | push ebp; lea ebp, [esp+12]; push ebp
2949 | push ebx; push edx; push ecx; push eax
2950 | movzx RC, byte [ebp-4] // Reconstruct exit number.
2951 | mov RCH, byte [ebp-8]
2952 | mov [ebp-4], edi; mov [ebp-8], esi
2954 | // Caveat: DISPATCH is ebx.
2955 | mov DISPATCH, [ebp]
2956 | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2958 | mov [DISPATCH+DISPATCH_J(exitno)], RC
2959 | mov [DISPATCH+DISPATCH_J(parent)], RA
2962 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2964 | sub rsp, 16*8 // Room for SSE regs.
2967 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2968 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2969 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2970 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2971 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2972 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2973 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2974 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2976 | sub esp, 8*8+16 // Room for SSE regs + args.
2977 | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2978 | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2979 | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2980 | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2982 | // Caveat: RB is ebp.
2983 | mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)]
2984 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2985 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2986 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
2987 | mov L:RB->base, BASE
2989 | lea CARG2, [rsp+4*8]
2993 | lea FCARG2, [esp+16]
2995 | lea FCARG1, [DISPATCH+GG_DISP2J]
2996 | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
2997 | // MULTRES or negated error code returned in eax (RD).
2998 | mov RAa, L:RB->cframe
2999 | and RAa, CFRAME_RAWMASK
3001 | // Reposition stack later.
3003 | mov rsp, RAa // Reposition stack to C frame.
3005 | mov esp, RAa // Reposition stack to C frame.
3007 | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
3008 | mov BASE, L:RB->base
3009 | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
3015 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
3018 | // Restore additional callee-save registers only used in compiled code.
3020 | lea RAa, [rsp+9*16+4*8]
3022 | movdqa xmm15, [RAa-9*16]
3023 | movdqa xmm14, [RAa-8*16]
3024 | movdqa xmm13, [RAa-7*16]
3025 | movdqa xmm12, [RAa-6*16]
3026 | movdqa xmm11, [RAa-5*16]
3027 | movdqa xmm10, [RAa-4*16]
3028 | movdqa xmm9, [RAa-3*16]
3029 | movdqa xmm8, [RAa-2*16]
3030 | movdqa xmm7, [RAa-1*16]
3031 | mov rsp, RAa // Reposition stack to C frame.
3032 | movdqa xmm6, [RAa]
3036 | add rsp, 16 // Reposition stack to C frame.
3042 | test RD, RD; js >3 // Check for error from exit.
3044 | mov LFUNC:KBASE, [BASE-8]
3045 | mov KBASE, LFUNC:KBASE->pc
3046 | mov KBASE, [KBASE+PC2PROTO(k)]
3047 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
3048 | set_vmstate INTERP
3049 | // Modified copy of ins_next which handles function header dispatch, too.
3055 | cmp OP, BC_FUNCF // Function header?
3057 | mov RC, MULTRES // RC/RD holds nres+1.
3060 | jmp aword [DISPATCH+OP*8]
3062 | jmp aword [DISPATCH+OP*4]
3065 |3: // Rethrow error from the right C frame.
3069 | call extern lj_err_throw@8 // (lua_State *L, int errcode)
3072 |//-----------------------------------------------------------------------
3073 |//-- Math helper functions ----------------------------------------------
3074 |//-----------------------------------------------------------------------
3076 |// FP value rounding. Called by math.floor/math.ceil fast functions
3077 |// and from JIT code.
3079 |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified.
3080 |.macro vm_round_x87, mode1, mode2
3081 | fnstcw word [esp+4] // Caveat: overwrites ARG1 and ARG2.
3085 |.if mode2 ~= 0xffff
3089 | fldcw word [esp+6]
3091 | fldcw word [esp+4]
3096 |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
3097 |.macro vm_round_sse, mode
3098 | sseconst_abs xmm2, RDa
3099 | sseconst_2p52 xmm3, RDa
3101 | andpd xmm1, xmm2 // |x|
3102 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
3104 | andnpd xmm2, xmm0 // Isolate sign bit.
3105 |.if mode == 2 // trunc(x)?
3107 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3109 | sseconst_1 xmm3, RDa
3110 | cmpsd xmm0, xmm1, 1 // |x| < result?
3112 | subsd xmm1, xmm0 // If yes, subtract -1.
3113 | orpd xmm1, xmm2 // Merge sign bit back in.
3115 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3117 | orpd xmm1, xmm2 // Merge sign bit back in.
3118 | .if mode == 1 // ceil(x)?
3119 | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0.
3120 | cmpsd xmm0, xmm1, 6 // x > result?
3121 | .else // floor(x)?
3122 | sseconst_1 xmm2, RDa
3123 | cmpsd xmm0, xmm1, 1 // x < result?
3126 | subsd xmm1, xmm0 // If yes, subtract +-1.
3133 |.macro vm_round, name, ssemode, mode1, mode2
3136 | vm_round_x87 mode1, mode2
3139 | vm_round_sse ssemode
3142 | vm_round vm_floor, 0, 0x0400, 0xf7ff
3143 | vm_round vm_ceil, 1, 0x0800, 0xfbff
3144 | vm_round vm_trunc, 2, 0x0c00, 0xffff
3146 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
3149 |// Args in xmm0/xmm1, return value in xmm0.
3150 |// Caveat: xmm0-xmm5 and RC (eax) modified!
3153 | sseconst_abs xmm2, RDa
3154 | sseconst_2p52 xmm3, RDa
3156 | andpd xmm4, xmm2 // |x/y|
3157 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
3159 | andnpd xmm2, xmm0 // Isolate sign bit.
3160 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
3162 | orpd xmm4, xmm2 // Merge sign bit back in.
3163 | sseconst_1 xmm2, RDa
3164 | cmpsd xmm0, xmm4, 1 // x/y < result?
3166 | subsd xmm4, xmm0 // If yes, subtract 1.0.
3177 |// Args/ret on x87 stack (y on top). No xmm registers modified.
3178 |// Caveat: needs 3 slots on x87 stack! RC (eax) modified!
3181 | fnstcw word [esp+4]
3186 | fldcw word [esp+6]
3188 | fldcw word [esp+4]
3194 |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
3195 |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
3196 |// Caveat: needs 3 slots on x87 stack!
3198 | fldl2e; fmulp st1 // e^x ==> 2^(x*log2(e))
3201 | .define expscratch, dword [rsp+8] // Use scratch area.
3203 | .define expscratch, dword [rsp-8] // Use red zone.
3205 | .define expscratch, dword [esp+4] // Needs 4 byte scratch area.
3207 | fst expscratch // Caveat: overwrites ARG1.
3208 | cmp expscratch, 0x7f800000; je >1 // Special case: e^+Inf = +Inf
3209 | cmp expscratch, 0xff800000; je >2 // Special case: e^-Inf = 0
3210 |->vm_exp2raw: // Entry point for vm_pow. Without +-Inf check.
3211 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3212 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3218 |// Generic power function x^y. Called by BC_POW, math.pow fast function,
3222 |// Args/ret on x87 stack (y on top). RC (eax) modified.
3223 |// Caveat: needs 3 slots on x87 stack!
3225 | fist dword [esp+4] // Store/reload int before comparison.
3226 | fild dword [esp+4] // Integral exponent used in vm_powi.
3230 | fucomp st1; fnstsw ax; sahf
3232 | jnz >8 // Branch for FP exponents.
3233 | jp >9 // Branch for NaN exponent.
3234 | fpop // Pop y and fallthrough to vm_powi.
3236 |// FP/int power function x^i. Arg1/ret on x87 stack.
3237 |// Arg2 (int) on C stack. RC (eax) modified.
3238 |// Caveat: needs 2 slots on x87 stack!
3240 | cmp eax, 1; jle >6 // i<=1?
3241 | // Now 1 < (unsigned)i <= 0x80000000.
3242 |1: // Handle leading zeros.
3243 | test eax, 1; jnz >2
3250 |3: // Handle trailing bits.
3261 | je <5 // x^1 ==> x
3265 | cmp eax, 1; je <5 // x^-1 ==> 1/x
3266 | jmp <1 // x^-i ==> (1/x)^i
3268 | fpop; fld1 // x^0 ==> 1
3271 |8: // FP/FP power function x^y.
3275 | mov eax, [esp+4]; shl eax, 1
3276 | cmp eax, 0xff000000; je >2 // x^+-Inf?
3277 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3278 | cmp eax, 0xff000000; je >4 // +-Inf^y?
3282 |9: // Handle x^NaN.
3287 | fucomp st2; fnstsw ax; sahf
3289 | je >1 // 1^NaN ==> 1
3290 | fxch // x^NaN ==> NaN
3295 |2: // Handle x^+-Inf.
3301 | fucomp st1; fnstsw ax; sahf
3303 | je >3 // +-1^+-Inf ==> 1
3304 | fpop; fabs; fldz; mov eax, 0; setc al
3305 | ror eax, 1; xor eax, [esp+4]; jns >3 // |x|<>1, x^+-Inf ==> +Inf/0
3311 |4: // Handle +-0^y or +-Inf^y.
3312 | cmp dword [esp+4], 0; jge <3 // y >= 0, x^y ==> |x|
3314 | test eax, eax; jz >5 // y < 0, +-0^y ==> +Inf
3315 | fldz // y < 0, +-Inf^y ==> 0
3318 | mov dword [esp+4], 0x7f800000 // Return +Inf.
3326 |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified.
3327 |// Needs 16 byte scratch area for x86. Also called from JIT code.
3329 | cvtsd2si eax, xmm1
3330 | cvtsi2sd xmm2, eax
3331 | ucomisd xmm1, xmm2
3332 | jnz >8 // Branch for FP exponents.
3333 | jp >9 // Branch for NaN exponent.
3334 | // Fallthrough to vm_powi_sse.
3336 |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
3338 | cmp eax, 1; jle >6 // i<=1?
3339 | // Now 1 < (unsigned)i <= 0x80000000.
3340 |1: // Handle leading zeros.
3341 | test eax, 1; jnz >2
3348 |3: // Handle trailing bits.
3359 | je <5 // x^1 ==> x
3360 | jb >7 // x^0 ==> 1
3363 | sseconst_1 xmm1, RDa
3368 | sseconst_1 xmm0, RDa
3371 |8: // FP/FP power function x^y.
3373 | movd rax, xmm1; shl rax, 1
3374 | rol rax, 12; cmp rax, 0xffe; je >2 // x^+-Inf?
3375 | movd rax, xmm0; shl rax, 1; je >4 // +-0^y?
3376 | rol rax, 12; cmp rax, 0xffe; je >5 // +-Inf^y?
3378 | movsd qword [rsp+16], xmm1 // Use scratch area.
3379 | movsd qword [rsp+8], xmm0
3380 | fld qword [rsp+16]
3383 | movsd qword [rsp-16], xmm1 // Use red zone.
3384 | movsd qword [rsp-8], xmm0
3385 | fld qword [rsp-16]
3389 | movsd qword [esp+12], xmm1 // Needs 16 byte scratch area.
3390 | movsd qword [esp+4], xmm0
3391 | cmp dword [esp+12], 0; jne >1
3392 | mov eax, [esp+16]; shl eax, 1
3393 | cmp eax, 0xffe00000; je >2 // x^+-Inf?
3395 | cmp dword [esp+4], 0; jne >1
3396 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3397 | cmp eax, 0xffe00000; je >5 // +-Inf^y?
3399 | fld qword [esp+12]
3402 | fyl2x // y*log2(x)
3403 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3404 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3406 | fstp qword [rsp+8] // Use scratch area.
3407 | movsd xmm0, qword [rsp+8]
3409 | fstp qword [rsp-8] // Use red zone.
3410 | movsd xmm0, qword [rsp-8]
3412 | fstp qword [esp+4] // Needs 8 byte scratch area.
3413 | movsd xmm0, qword [esp+4]
3417 |9: // Handle x^NaN.
3418 | sseconst_1 xmm2, RDa
3419 | ucomisd xmm0, xmm2; je >1 // 1^NaN ==> 1
3420 | movaps xmm0, xmm1 // x^NaN ==> NaN
3424 |2: // Handle x^+-Inf.
3425 | sseconst_abs xmm2, RDa
3426 | andpd xmm0, xmm2 // |x|
3427 | sseconst_1 xmm2, RDa
3428 | ucomisd xmm0, xmm2; je <1 // +-1^+-Inf ==> 1
3429 | movmskpd eax, xmm1
3431 | mov ah, al; setc al; xor al, ah; jne <1 // |x|<>1, x^+-Inf ==> +Inf/0
3433 | sseconst_hi xmm0, RDa, 7ff00000 // +Inf
3436 |4: // Handle +-0^y.
3437 | movmskpd eax, xmm1; test eax, eax; jnz <3 // y < 0, +-0^y ==> +Inf
3438 | xorps xmm0, xmm0 // y >= 0, +-0^y ==> 0
3441 |5: // Handle +-Inf^y.
3442 | movmskpd eax, xmm1; test eax, eax; jz <3 // y >= 0, +-Inf^y ==> +Inf
3443 | xorps xmm0, xmm0 // y < 0, +-Inf^y ==> 0
3446 |// Callable from C: double lj_vm_foldfpm(double x, int fpm)
3447 |// Computes fpm(x) for extended math functions. ORDER FPM.
3454 | .define fpmop, CARG2d
3456 | .define fpmop, CARG1d
3458 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3459 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3460 | sqrtsd xmm0, xmm0; ret
3463 | movsd qword [rsp+8], xmm0 // Use scratch area.
3466 | movsd qword [rsp-8], xmm0 // Use red zone.
3469 | cmp fpmop, 5; ja >2
3470 | .if X64WIN; pop rax; .endif
3473 | .if X64WIN; push rax; .endif
3476 | call ->vm_exp2_x87
3477 | .if X64WIN; push rax; .endif
3479 |2: ; cmp fpmop, 7; je >1; ja >2
3480 | fldln2; fxch; fyl2x; jmp >7
3481 |1: ; fld1; fxch; fyl2x; jmp >7
3482 |2: ; cmp fpmop, 9; je >1; ja >2
3483 | fldlg2; fxch; fyl2x; jmp >7
3485 |2: ; cmp fpmop, 11; je >1; ja >9
3490 | fstp qword [rsp+8] // Use scratch area.
3491 | movsd xmm0, qword [rsp+8]
3493 | fstp qword [rsp-8] // Use red zone.
3494 | movsd xmm0, qword [rsp-8]
3498 |.else // x86 calling convention.
3500 | .define fpmop, eax
3501 | mov fpmop, [esp+12]
3502 | movsd xmm0, qword [esp+4]
3503 | cmp fpmop, 1; je >1; ja >2
3504 | call ->vm_floor; jmp >7
3505 |1: ; call ->vm_ceil; jmp >7
3506 |2: ; cmp fpmop, 3; je >1; ja >2
3507 | call ->vm_trunc; jmp >7
3511 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3514 |2: ; fld qword [esp+4]
3515 | cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3516 |2: ; cmp fpmop, 7; je >1; ja >2
3517 | fldln2; fxch; fyl2x; ret
3518 |1: ; fld1; fxch; fyl2x; ret
3519 |2: ; cmp fpmop, 9; je >1; ja >2
3520 | fldlg2; fxch; fyl2x; ret
3522 |2: ; cmp fpmop, 11; je >1; ja >9
3524 |1: ; fptan; fpop; ret
3528 | mov fpmop, [esp+12]
3530 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3531 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3533 |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3534 | cmp fpmop, 7; je >1; ja >2
3535 | fldln2; fxch; fyl2x; ret
3536 |1: ; fld1; fxch; fyl2x; ret
3537 |2: ; cmp fpmop, 9; je >1; ja >2
3538 | fldlg2; fxch; fyl2x; ret
3540 |2: ; cmp fpmop, 11; je >1; ja >9
3542 |1: ; fptan; fpop; ret
3544 |9: ; int3 // Bad fpm.
3547 |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
3548 |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
3549 |// and basic math functions. ORDER ARITH
3555 | .define foldop, CARG3d
3557 | .define foldop, CARG1d
3559 | cmp foldop, 1; je >1; ja >2
3560 | addsd xmm0, xmm1; ret
3561 |1: ; subsd xmm0, xmm1; ret
3562 |2: ; cmp foldop, 3; je >1; ja >2
3563 | mulsd xmm0, xmm1; ret
3564 |1: ; divsd xmm0, xmm1; ret
3565 |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow
3566 | cmp foldop, 7; je >1; ja >2
3567 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret
3568 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret
3569 |2: ; cmp foldop, 9; ja >2
3571 | movsd qword [rsp+8], xmm0 // Use scratch area.
3572 | movsd qword [rsp+16], xmm1
3574 | fld qword [rsp+16]
3576 | movsd qword [rsp-8], xmm0 // Use red zone.
3577 | movsd qword [rsp-16], xmm1
3579 | fld qword [rsp-16]
3585 | fstp qword [rsp+8] // Use scratch area.
3586 | movsd xmm0, qword [rsp+8]
3588 | fstp qword [rsp-8] // Use red zone.
3589 | movsd xmm0, qword [rsp-8]
3592 |1: ; fxch; fscale; fpop1; jmp <7
3593 |2: ; cmp foldop, 11; je >1; ja >9
3594 | minsd xmm0, xmm1; ret
3595 |1: ; maxsd xmm0, xmm1; ret
3596 |9: ; int3 // Bad op.
3598 |.else // x86 calling convention.
3600 | .define foldop, eax
3601 | mov foldop, [esp+20]
3602 | movsd xmm0, qword [esp+4]
3603 | movsd xmm1, qword [esp+12]
3604 | cmp foldop, 1; je >1; ja >2
3607 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3610 |1: ; subsd xmm0, xmm1; jmp <7
3611 |2: ; cmp foldop, 3; je >1; ja >2
3612 | mulsd xmm0, xmm1; jmp <7
3613 |1: ; divsd xmm0, xmm1; jmp <7
3616 | call ->vm_mod; jmp <7
3617 |1: ; pop edx; call ->vm_pow; push edx; jmp <7 // Writes to scratch area.
3618 |2: ; cmp foldop, 7; je >1; ja >2
3619 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7
3620 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7
3621 |2: ; cmp foldop, 9; ja >2
3622 | fld qword [esp+4] // Reload from stack
3623 | fld qword [esp+12]
3626 |1: ; fxch; fscale; fpop1; ret
3627 |2: ; cmp foldop, 11; je >1; ja >9
3628 | minsd xmm0, xmm1; jmp <7
3629 |1: ; maxsd xmm0, xmm1; jmp <7
3630 |9: ; int3 // Bad op.
3636 | fld qword [esp+12]
3637 | cmp eax, 1; je >1; ja >2
3639 |1: ; fsubp st1; ret
3640 |2: ; cmp eax, 3; je >1; ja >2
3642 |1: ; fdivp st1; ret
3643 |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow
3644 | cmp eax, 7; je >1; ja >2
3646 |1: ; fpop; fabs; ret
3647 |2: ; cmp eax, 9; je >1; ja >2
3649 |1: ; fxch; fscale; fpop1; ret
3650 |2: ; cmp eax, 11; je >1; ja >9
3652 | fucomi st1; fcmovnbe st1; fpop1; ret
3653 |1: ; fucomi st1; fcmovbe st1; fpop1; ret
3655 | fucom st1; fnstsw ax; test ah, 1; jz >2; fxch; 2: ; fpop; ret
3656 |1: ; fucom st1; fnstsw ax; test ah, 1; jnz >2; fxch; 2: ; fpop; ret
3658 |9: ; int3 // Bad op.
3661 |//-----------------------------------------------------------------------
3662 |//-- Miscellaneous functions --------------------------------------------
3663 |//-----------------------------------------------------------------------
3665 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3669 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
3677 | .if X64WIN; pop rsi; .endif
3683 | xor edx, 0x00200000 // Toggle ID bit in flags.
3688 | xor eax, eax // Zero means no features supported.
3690 | jz >1 // No ID toggle means no CPUID support.
3691 | mov eax, [esp+4] // Argument 1 is function number.
3695 | mov edi, [esp+16] // Argument 2 is result area.
3706 |//-----------------------------------------------------------------------
3707 |//-- Assertions ---------------------------------------------------------
3708 |//-----------------------------------------------------------------------
3710 |->assert_bad_for_arg_type:
3711 #ifdef LUA_USE_ASSERT
3716 |//-----------------------------------------------------------------------
3717 |//-- FFI helper functions -----------------------------------------------
3718 |//-----------------------------------------------------------------------
3720 |// Handler for callback functions. Callback slot number in ah/al.
3723 |.type CTSTATE, CTState, PC
3725 | sub esp, 16 // Leave room for SAVE_ERRF etc.
3727 | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
3728 | lea DISPATCH, [ebp+GG_G2DISP]
3729 | mov CTSTATE, GL:ebp->ctype_state
3731 | mov CTSTATE->cb.slot, eax
3733 | mov CTSTATE->cb.gpr[0], CARG1
3734 | mov CTSTATE->cb.gpr[1], CARG2
3735 | mov CTSTATE->cb.gpr[2], CARG3
3736 | mov CTSTATE->cb.gpr[3], CARG4
3737 | movsd qword CTSTATE->cb.fpr[0], xmm0
3738 | movsd qword CTSTATE->cb.fpr[1], xmm1
3739 | movsd qword CTSTATE->cb.fpr[2], xmm2
3740 | movsd qword CTSTATE->cb.fpr[3], xmm3
3742 | lea rax, [rsp+CFRAME_SIZE+4*8]
3744 | lea rax, [rsp+CFRAME_SIZE]
3745 | mov CTSTATE->cb.gpr[4], CARG5
3746 | mov CTSTATE->cb.gpr[5], CARG6
3747 | movsd qword CTSTATE->cb.fpr[4], xmm4
3748 | movsd qword CTSTATE->cb.fpr[5], xmm5
3749 | movsd qword CTSTATE->cb.fpr[6], xmm6
3750 | movsd qword CTSTATE->cb.fpr[7], xmm7
3752 | mov CTSTATE->cb.stack, rax
3755 | lea eax, [esp+CFRAME_SIZE+16]
3756 | mov CTSTATE->cb.gpr[0], FCARG1
3757 | mov CTSTATE->cb.gpr[1], FCARG2
3758 | mov CTSTATE->cb.stack, eax
3759 | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp.
3760 | mov FCARG2, [esp+CFRAME_SIZE+8]
3761 | mov SAVE_RET, FCARG1
3762 | mov SAVE_R4, FCARG2
3765 | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
3766 | mov FCARG1, CTSTATE
3767 | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf)
3768 | // lua_State * returned in eax (RD).
3769 | set_vmstate INTERP
3770 | mov BASE, L:RD->base
3773 | mov LFUNC:RB, [BASE-8]
3779 |->cont_ffi_callback: // Return from FFI callback.
3782 | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
3783 | mov aword CTSTATE->L, L:RAa
3784 | mov L:RA->base, BASE
3786 | mov FCARG1, CTSTATE
3788 | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o)
3790 | mov rax, CTSTATE->cb.gpr[0]
3791 | movsd xmm0, qword CTSTATE->cb.fpr[0]
3792 | jmp ->vm_leave_unw
3795 | mov eax, CTSTATE->cb.gpr[0]
3796 | mov edx, CTSTATE->cb.gpr[1]
3797 | cmp dword CTSTATE->cb.gpr[2], 1
3800 | fld qword CTSTATE->cb.fpr[0].d
3803 | fld dword CTSTATE->cb.fpr[0].f
3805 | mov ecx, L:RB->top
3806 | movzx ecx, word [ecx+6] // Get stack adjustment and copy up.
3807 | mov SAVE_L, ecx // Must be one slot above SAVE_RET
3809 | pop ecx // Move return addr from SAVE_RET.
3810 | add esp, [esp] // Adjust stack.
3817 |->vm_ffi_call@4: // Call C function via FFI.
3818 | // Caveat: needs special frame unwinding, see below.
3821 | .type CCSTATE, CCallState, rbx
3822 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3824 | .type CCSTATE, CCallState, ebx
3825 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3828 | // Readjust stack.
3830 | mov eax, CCSTATE->spadj
3833 | sub esp, CCSTATE->spadj
3834 #if LJ_TARGET_WINDOWS
3835 | mov CCSTATE->spadj, esp
3839 | // Copy stack slots.
3840 | movzx ecx, byte CCSTATE->nsp
3845 | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3846 | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3848 | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3849 | mov [esp+ecx*4], eax
3856 | movzx eax, byte CCSTATE->nfpr
3857 | mov CARG1, CCSTATE->gpr[0]
3858 | mov CARG2, CCSTATE->gpr[1]
3859 | mov CARG3, CCSTATE->gpr[2]
3860 | mov CARG4, CCSTATE->gpr[3]
3862 | mov CARG5, CCSTATE->gpr[4]
3863 | mov CARG6, CCSTATE->gpr[5]
3865 | test eax, eax; jz >5
3866 | movaps xmm0, CCSTATE->fpr[0]
3867 | movaps xmm1, CCSTATE->fpr[1]
3868 | movaps xmm2, CCSTATE->fpr[2]
3869 | movaps xmm3, CCSTATE->fpr[3]
3871 | cmp eax, 4; jbe >5
3872 | movaps xmm4, CCSTATE->fpr[4]
3873 | movaps xmm5, CCSTATE->fpr[5]
3874 | movaps xmm6, CCSTATE->fpr[6]
3875 | movaps xmm7, CCSTATE->fpr[7]
3879 | mov FCARG1, CCSTATE->gpr[0]
3880 | mov FCARG2, CCSTATE->gpr[1]
3883 | call aword CCSTATE->func
3886 | mov CCSTATE->gpr[0], rax
3887 | movaps CCSTATE->fpr[0], xmm0
3889 | mov CCSTATE->gpr[1], rdx
3890 | movaps CCSTATE->fpr[1], xmm1
3893 | mov CCSTATE->gpr[0], eax
3894 | mov CCSTATE->gpr[1], edx
3895 | cmp byte CCSTATE->resx87, 1
3898 | fstp qword CCSTATE->fpr[0].d[0]
3901 | fstp dword CCSTATE->fpr[0].f[0]
3903 #if LJ_TARGET_WINDOWS
3904 | sub CCSTATE->spadj, esp
3909 | mov rbx, [rbp-8]; leave; ret
3911 | mov ebx, [ebp-4]; leave; ret
3914 |// Note: vm_ffi_call must be the last function in this object file!
3916 |//-----------------------------------------------------------------------
3919 /* Generate the code for a single instruction. */
3920 static void build_ins(BuildCtx *ctx, BCOp op, int defop, int cmov, int sse)
3923 |// Note: aligning all instructions does not pay off.
3928 /* -- Comparison ops ---------------------------------------------------- */
3930 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3932 |.macro jmp_comp, lt, ge, le, gt, target
3946 ||default: break; /* Shut up GCC. */
3950 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3951 | // RA = src1, RD = src2, JMP with RD = target
3956 | mov RB, dword [BASE+RA*8]
3958 | cmp RB, dword [BASE+RD*8]
3959 | jmp_comp jge, jl, jg, jle, >9
3966 |7: // RA is not an integer.
3968 | // RA is a number.
3969 | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3970 | // RA is a number, RD is an integer.
3972 | cvtsi2sd xmm0, dword [BASE+RD*8]
3975 | fld qword [BASE+RA*8]
3976 | fild dword [BASE+RD*8]
3980 |8: // RA is an integer, RD is not an integer.
3982 | // RA is an integer, RD is a number.
3984 | cvtsi2sd xmm1, dword [BASE+RA*8]
3985 | movsd xmm0, qword [BASE+RD*8]
3987 | ucomisd xmm0, xmm1
3988 | jmp_comp jbe, ja, jb, jae, <9
3991 | fild dword [BASE+RA*8]
3995 | checknum RA, ->vmeta_comp
3996 | checknum RD, ->vmeta_comp
4000 | movsd xmm0, qword [BASE+RD*8]
4003 | ucomisd xmm0, qword [BASE+RA*8]
4007 | fld qword [BASE+RA*8] // Reverse order, i.e like cmp D, A.
4009 | fld qword [BASE+RD*8]
4012 | fcomparepp // eax (RD) modified!
4014 | // Unordered: all of ZF CF PF set, ordered: PF clear.
4015 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
4017 | jmp_comp jbe, ja, jb, jae, <9
4020 | jmp_comp jbe, ja, jb, jae, >1
4028 case BC_ISEQV: case BC_ISNEV:
4029 vk = op == BC_ISEQV;
4030 | ins_AD // RA = src1, RD = src2, JMP with RD = target
4031 | mov RB, [BASE+RD*8+4]
4034 | cmp RB, LJ_TISNUM; jne >7
4036 | mov RB, dword [BASE+RD*8]
4037 | cmp RB, dword [BASE+RA*8]
4048 |7: // RD is not an integer.
4050 | // RD is a number.
4051 | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
4052 | // RD is a number, RA is an integer.
4054 | cvtsi2sd xmm0, dword [BASE+RA*8]
4056 | fild dword [BASE+RA*8]
4060 |8: // RD is an integer, RA is not an integer.
4062 | // RD is an integer, RA is a number.
4064 | cvtsi2sd xmm0, dword [BASE+RD*8]
4065 | ucomisd xmm0, qword [BASE+RA*8]
4067 | fild dword [BASE+RD*8]
4068 | fld qword [BASE+RA*8]
4073 | cmp RB, LJ_TISNUM; jae >5
4078 | movsd xmm0, qword [BASE+RA*8]
4080 | ucomisd xmm0, qword [BASE+RD*8]
4084 | fld qword [BASE+RA*8]
4086 | fld qword [BASE+RD*8]
4088 | fcomparepp // eax (RD) modified!
4092 | jp >2 // Unordered means not equal.
4095 | jp >2 // Unordered means not equal.
4100 |1: // EQ: Branch to the target.
4103 |2: // NE: Fallthrough to next instruction.
4111 |2: // NE: Branch to the target.
4114 |1: // EQ: Fallthrough to next instruction.
4116 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
4117 op == BC_ISEQN || op == BC_ISNEN)) {
4123 if (op == BC_ISEQV || op == BC_ISNEV) {
4124 |5: // Either or both types are not numbers.
4126 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4127 | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
4129 | checktp RA, RB // Compare types.
4130 | jne <2 // Not the same type?
4132 | jae <1 // Same type and primitive type?
4134 | // Same types and not a primitive type. Compare GCobj or pvalue.
4135 | mov RA, [BASE+RA*8]
4136 | mov RD, [BASE+RD*8]
4138 | je <1 // Same GCobjs or pvalues?
4139 | cmp RB, LJ_TISTABUD
4140 | ja <2 // Different objects and not table/ud?
4142 | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
4146 | // Different tables or userdatas. Need to check __eq metamethod.
4147 | // Field metatable must be at same offset for GCtab and GCudata!
4148 | mov TAB:RB, TAB:RA->metatable
4149 | test TAB:RB, TAB:RB
4150 | jz <2 // No metatable?
4151 | test byte TAB:RB->nomm, 1<<MM_eq
4152 | jnz <2 // Or 'no __eq' flag set?
4154 | xor RB, RB // ne = 0
4156 | mov RB, 1 // ne = 1
4158 | jmp ->vmeta_equal // Handle __eq metamethod.
4159 } else if (LJ_HASFFI) {
4162 if (LJ_DUALNUM && vk) {
4167 | jmp ->vmeta_equal_cd
4170 case BC_ISEQS: case BC_ISNES:
4171 vk = op == BC_ISEQS;
4172 | ins_AND // RA = src, RD = str const, JMP with RD = target
4173 | mov RB, [BASE+RA*8+4]
4175 | cmp RB, LJ_TSTR; jne >3
4176 | mov RA, [BASE+RA*8]
4177 | cmp RA, [KBASE+RD*4]
4185 case BC_ISEQN: case BC_ISNEN:
4186 vk = op == BC_ISEQN;
4187 | ins_AD // RA = src, RD = num const, JMP with RD = target
4188 | mov RB, [BASE+RA*8+4]
4191 | cmp RB, LJ_TISNUM; jne >7
4192 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
4193 | mov RB, dword [KBASE+RD*8]
4194 | cmp RB, dword [BASE+RA*8]
4205 |7: // RA is not an integer.
4207 | // RA is a number.
4208 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
4209 | // RA is a number, RD is an integer.
4211 | cvtsi2sd xmm0, dword [KBASE+RD*8]
4213 | fild dword [KBASE+RD*8]
4217 |8: // RA is an integer, RD is a number.
4219 | cvtsi2sd xmm0, dword [BASE+RA*8]
4220 | ucomisd xmm0, qword [KBASE+RD*8]
4222 | fild dword [BASE+RA*8]
4223 | fld qword [KBASE+RD*8]
4227 | cmp RB, LJ_TISNUM; jae >3
4231 | movsd xmm0, qword [KBASE+RD*8]
4233 | ucomisd xmm0, qword [BASE+RA*8]
4237 | fld qword [KBASE+RD*8]
4239 | fld qword [BASE+RA*8]
4241 | fcomparepp // eax (RD) modified!
4244 case BC_ISEQP: case BC_ISNEP:
4245 vk = op == BC_ISEQP;
4246 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
4247 | mov RB, [BASE+RA*8+4]
4250 if (!LJ_HASFFI) goto iseqne_test;
4258 | cmp RB, LJ_TCDATA; jne <2
4259 | jmp ->vmeta_equal_cd
4262 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4270 /* -- Unary test and copy ops ------------------------------------------- */
4272 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
4273 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
4274 | mov RB, [BASE+RD*8+4]
4276 | cmp RB, LJ_TISTRUECOND
4277 if (op == BC_IST || op == BC_ISTC) {
4282 if (op == BC_ISTC || op == BC_ISFC) {
4283 | mov [BASE+RA*8+4], RB
4284 | mov RB, [BASE+RD*8]
4285 | mov [BASE+RA*8], RB
4289 |1: // Fallthrough to the next instruction.
4293 /* -- Unary ops --------------------------------------------------------- */
4296 | ins_AD // RA = dst, RD = src
4298 | mov RBa, [BASE+RD*8]
4299 | mov [BASE+RA*8], RBa
4301 | mov RB, [BASE+RD*8+4]
4302 | mov RD, [BASE+RD*8]
4303 | mov [BASE+RA*8+4], RB
4304 | mov [BASE+RA*8], RD
4309 | ins_AD // RA = dst, RD = src
4311 | checktp RD, LJ_TISTRUECOND
4313 | mov [BASE+RA*8+4], RB
4317 | ins_AD // RA = dst, RD = src
4320 | mov RB, [BASE+RD*8]
4323 | mov dword [BASE+RA*8+4], LJ_TISNUM
4324 | mov dword [BASE+RA*8], RB
4328 | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
4329 | mov dword [BASE+RA*8], 0
4334 | checknum RD, ->vmeta_unm
4337 | movsd xmm0, qword [BASE+RD*8]
4338 | sseconst_sign xmm1, RDa
4340 | movsd qword [BASE+RA*8], xmm0
4342 | fld qword [BASE+RD*8]
4344 | fstp qword [BASE+RA*8]
4353 | ins_AD // RA = dst, RD = src
4355 | mov STR:RD, [BASE+RD*8]
4357 | mov RD, dword STR:RD->len
4359 | mov dword [BASE+RA*8+4], LJ_TISNUM
4360 | mov dword [BASE+RA*8], RD
4363 | cvtsi2sd xmm0, dword STR:RD->len
4365 | movsd qword [BASE+RA*8], xmm0
4367 | fild dword STR:RD->len
4369 | fstp qword [BASE+RA*8]
4373 | checktab RD, ->vmeta_len
4374 | mov TAB:FCARG1, [BASE+RD*8]
4375 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4376 | mov TAB:RB, TAB:FCARG1->metatable
4382 | mov RB, BASE // Save BASE.
4383 | call extern lj_tab_len@4 // (GCtab *t)
4384 | // Length of table returned in eax (RD).
4395 | mov BASE, RB // Restore BASE.
4398 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4399 |9: // Check for __len.
4400 | test byte TAB:RB->nomm, 1<<MM_len
4402 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
4406 /* -- Binary ops -------------------------------------------------------- */
4408 |.macro ins_arithpre, x87ins, sseins, ssereg
4410 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4413 | checknum RB, ->vmeta_arith_vn
4415 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
4418 | movsd xmm0, qword [BASE+RB*8]
4419 | sseins ssereg, qword [KBASE+RC*8]
4421 | fld qword [BASE+RB*8]
4422 | x87ins qword [KBASE+RC*8]
4426 | checknum RB, ->vmeta_arith_nv
4428 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
4431 | movsd xmm0, qword [KBASE+RC*8]
4432 | sseins ssereg, qword [BASE+RB*8]
4434 | fld qword [KBASE+RC*8]
4435 | x87ins qword [BASE+RB*8]
4439 | checknum RB, ->vmeta_arith_vv
4440 | checknum RC, ->vmeta_arith_vv
4442 | movsd xmm0, qword [BASE+RB*8]
4443 | sseins ssereg, qword [BASE+RC*8]
4445 | fld qword [BASE+RB*8]
4446 | x87ins qword [BASE+RC*8]
4452 |.macro ins_arithdn, intins
4454 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4457 | checkint RB, ->vmeta_arith_vn
4458 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
4459 | mov RB, [BASE+RB*8]
4460 | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
4463 | checkint RB, ->vmeta_arith_nv
4464 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
4465 | mov RC, [KBASE+RC*8]
4466 | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
4469 | checkint RB, ->vmeta_arith_vv
4470 | checkint RC, ->vmeta_arith_vv
4471 | mov RB, [BASE+RB*8]
4472 | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
4475 | mov dword [BASE+RA*8+4], LJ_TISNUM
4477 | mov dword [BASE+RA*8], RC
4479 | mov dword [BASE+RA*8], RB
4484 |.macro ins_arithpost
4486 | movsd qword [BASE+RA*8], xmm0
4488 | fstp qword [BASE+RA*8]
4492 |.macro ins_arith, x87ins, sseins
4493 | ins_arithpre x87ins, sseins, xmm0
4498 |.macro ins_arith, intins, x87ins, sseins
4500 | ins_arithdn intins
4502 | ins_arith, x87ins, sseins
4506 | // RA = dst, RB = src1 or num const, RC = src2 or num const
4507 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
4508 | ins_arith add, fadd, addsd
4510 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
4511 | ins_arith sub, fsub, subsd
4513 case BC_MULVN: case BC_MULNV: case BC_MULVV:
4514 | ins_arith imul, fmul, mulsd
4516 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
4517 | ins_arith fdiv, divsd
4520 | ins_arithpre fld, movsd, xmm1
4526 case BC_MODNV: case BC_MODVV:
4527 | ins_arithpre fld, movsd, xmm1
4528 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
4531 | ins_arithpre fld, movsd, xmm1
4538 | ins_ABC // RA = dst, RB = src_start, RC = src_end
4540 | mov L:CARG1d, SAVE_L
4541 | mov L:CARG1d->base, BASE
4542 | lea CARG2d, [BASE+RC*8]
4546 | mov L:RB, L:CARG1d
4548 | lea RA, [BASE+RC*8]
4555 | mov L:RB->base, BASE
4558 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
4559 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
4560 | mov BASE, L:RB->base
4563 | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
4566 | mov RCa, [BASE+RB*8]
4567 | mov [BASE+RA*8], RCa
4569 | mov RC, [BASE+RB*8+4]
4570 | mov RB, [BASE+RB*8]
4571 | mov [BASE+RA*8+4], RC
4572 | mov [BASE+RA*8], RB
4577 /* -- Constant ops ------------------------------------------------------ */
4580 | ins_AND // RA = dst, RD = str const (~)
4581 | mov RD, [KBASE+RD*4]
4582 | mov dword [BASE+RA*8+4], LJ_TSTR
4583 | mov [BASE+RA*8], RD
4588 | ins_AND // RA = dst, RD = cdata const (~)
4589 | mov RD, [KBASE+RD*4]
4590 | mov dword [BASE+RA*8+4], LJ_TCDATA
4591 | mov [BASE+RA*8], RD
4596 | ins_AD // RA = dst, RD = signed int16 literal
4599 | mov dword [BASE+RA*8+4], LJ_TISNUM
4600 | mov dword [BASE+RA*8], RD
4602 | movsx RD, RDW // Sign-extend literal.
4604 | movsd qword [BASE+RA*8], xmm0
4606 | fild PC_RD // Refetch signed RD from instruction.
4607 | fstp qword [BASE+RA*8]
4612 | ins_AD // RA = dst, RD = num const
4614 | movsd xmm0, qword [KBASE+RD*8]
4615 | movsd qword [BASE+RA*8], xmm0
4617 | fld qword [KBASE+RD*8]
4618 | fstp qword [BASE+RA*8]
4623 | ins_AND // RA = dst, RD = primitive type (~)
4624 | mov [BASE+RA*8+4], RD
4628 | ins_AD // RA = dst_start, RD = dst_end
4629 | lea RA, [BASE+RA*8+12]
4630 | lea RD, [BASE+RD*8+4]
4632 | mov [RA-8], RB // Sets minimum 2 slots.
4641 /* -- Upvalue and function ops ------------------------------------------ */
4644 | ins_AD // RA = dst, RD = upvalue #
4645 | mov LFUNC:RB, [BASE-8]
4646 | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
4647 | mov RB, UPVAL:RB->v
4650 | mov [BASE+RA*8], RDa
4654 | mov [BASE+RA*8+4], RD
4655 | mov [BASE+RA*8], RB
4660 #define TV2MARKOFS \
4661 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
4662 | ins_AD // RA = upvalue #, RD = src
4663 | mov LFUNC:RB, [BASE-8]
4664 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4665 | cmp byte UPVAL:RB->closed, 0
4666 | mov RB, UPVAL:RB->v
4667 | mov RA, [BASE+RD*8]
4668 | mov RD, [BASE+RD*8+4]
4672 | // Check barrier for closed upvalue.
4673 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
4678 |2: // Upvalue is black. Check if new value is collectable and white.
4680 | cmp RD, LJ_TISNUM - LJ_TISGCV // tvisgcv(v)
4682 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
4684 | // Crossed a write barrier. Move the barrier forward.
4685 |.if X64 and not X64WIN
4687 | mov RB, BASE // Save BASE.
4689 | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
4691 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4692 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4693 | mov BASE, RB // Restore BASE.
4698 | ins_AND // RA = upvalue #, RD = str const (~)
4699 | mov LFUNC:RB, [BASE-8]
4700 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4701 | mov GCOBJ:RA, [KBASE+RD*4]
4702 | mov RD, UPVAL:RB->v
4703 | mov [RD], GCOBJ:RA
4704 | mov dword [RD+4], LJ_TSTR
4705 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
4710 |2: // Check if string is white and ensure upvalue is closed.
4711 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
4713 | cmp byte UPVAL:RB->closed, 0
4715 | // Crossed a write barrier. Move the barrier forward.
4716 | mov RB, BASE // Save BASE (FCARG2 == BASE).
4718 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4719 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4720 | mov BASE, RB // Restore BASE.
4724 | ins_AD // RA = upvalue #, RD = num const
4725 | mov LFUNC:RB, [BASE-8]
4727 | movsd xmm0, qword [KBASE+RD*8]
4729 | fld qword [KBASE+RD*8]
4731 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4732 | mov RA, UPVAL:RB->v
4734 | movsd qword [RA], xmm0
4741 | ins_AND // RA = upvalue #, RD = primitive type (~)
4742 | mov LFUNC:RB, [BASE-8]
4743 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4744 | mov RA, UPVAL:RB->v
4749 | ins_AD // RA = level, RD = target
4750 | branchPC RD // Do this first to free RD.
4752 | cmp dword L:RB->openupval, 0
4754 | mov L:RB->base, BASE
4755 | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
4756 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4757 | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
4758 | mov BASE, L:RB->base
4764 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
4767 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4768 | mov CARG3d, [BASE-8]
4769 | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
4772 | mov LFUNC:RA, [BASE-8]
4773 | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
4775 | mov ARG3, LFUNC:RA
4776 | mov ARG2, PROTO:RD
4778 | mov L:RB->base, BASE
4781 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4782 | call extern lj_func_newL_gc
4783 | // GCfuncL * returned in eax (RC).
4784 | mov BASE, L:RB->base
4786 | mov [BASE+RA*8], LFUNC:RC
4787 | mov dword [BASE+RA*8+4], LJ_TFUNC
4791 /* -- Table ops --------------------------------------------------------- */
4794 | ins_AD // RA = dst, RD = hbits|asize
4796 | mov L:RB->base, BASE
4797 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4798 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4816 | mov L:CARG1d, L:RB
4822 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4823 | // Table * returned in eax (RC).
4824 | mov BASE, L:RB->base
4826 | mov [BASE+RA*8], TAB:RC
4827 | mov dword [BASE+RA*8+4], LJ_TTAB
4829 |3: // Turn 0x7ff into 0x801.
4833 | mov L:FCARG1, L:RB
4834 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4839 | ins_AND // RA = dst, RD = table const (~) (holding template table)
4841 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4843 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4844 | mov L:RB->base, BASE
4847 | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
4848 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4849 | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
4850 | // Table * returned in eax (RC).
4851 | mov BASE, L:RB->base
4853 | mov [BASE+RA*8], TAB:RC
4854 | mov dword [BASE+RA*8+4], LJ_TTAB
4857 | mov L:FCARG1, L:RB
4858 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4859 | movzx RD, PC_RD // Need to reload RD.
4865 | ins_AND // RA = dst, RD = str const (~)
4866 | mov LFUNC:RB, [BASE-8]
4867 | mov TAB:RB, LFUNC:RB->env
4868 | mov STR:RC, [KBASE+RD*4]
4872 | ins_AND // RA = src, RD = str const (~)
4873 | mov LFUNC:RB, [BASE-8]
4874 | mov TAB:RB, LFUNC:RB->env
4875 | mov STR:RC, [KBASE+RD*4]
4880 | ins_ABC // RA = dst, RB = table, RC = key
4881 | checktab RB, ->vmeta_tgetv
4882 | mov TAB:RB, [BASE+RB*8]
4887 | mov RC, dword [BASE+RC*8]
4889 | // Convert number to int and back and compare.
4892 | movsd xmm0, qword [BASE+RC*8]
4895 | ucomisd xmm0, xmm1
4898 | fld qword [BASE+RC*8]
4901 | fcomparepp // eax (RC) modified!
4905 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
4907 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4908 | jae ->vmeta_tgetv // Not in array part? Use fallback.
4910 | add RC, TAB:RB->array
4911 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4913 | // Get array slot.
4916 | mov [BASE+RA*8], RBa
4920 | mov [BASE+RA*8], RB
4921 | mov [BASE+RA*8+4], RC
4926 |2: // Check for __index if table value is nil.
4927 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4929 | mov TAB:RA, TAB:RB->metatable
4930 | test byte TAB:RA->nomm, 1<<MM_index
4931 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
4932 | movzx RA, PC_RA // Restore RA.
4934 | mov dword [BASE+RA*8+4], LJ_TNIL
4938 | checkstr RC, ->vmeta_tgetv
4939 | mov STR:RC, [BASE+RC*8]
4943 | ins_ABC // RA = dst, RB = table, RC = str const (~)
4945 | mov STR:RC, [KBASE+RC*4]
4946 | checktab RB, ->vmeta_tgets
4947 | mov TAB:RB, [BASE+RB*8]
4948 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4949 | mov RA, TAB:RB->hmask
4950 | and RA, STR:RC->hash
4952 | add NODE:RA, TAB:RB->node
4954 | cmp dword NODE:RA->key.it, LJ_TSTR
4956 | cmp dword NODE:RA->key.gcr, STR:RC
4958 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4959 | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4960 | je >5 // Key found, but nil value?
4962 | // Get node value.
4965 | mov [BASE+RC*8], RBa
4969 | mov [BASE+RC*8], RB
4970 | mov [BASE+RC*8+4], RA
4977 | mov dword [BASE+RC*8+4], LJ_TNIL
4980 |4: // Follow hash chain.
4981 | mov NODE:RA, NODE:RA->next
4982 | test NODE:RA, NODE:RA
4984 | // End of hash chain: key not found, nil result.
4986 |5: // Check for __index if table value is nil.
4987 | mov TAB:RA, TAB:RB->metatable
4988 | test TAB:RA, TAB:RA
4989 | jz <3 // No metatable: done.
4990 | test byte TAB:RA->nomm, 1<<MM_index
4991 | jnz <3 // 'no __index' flag set: done.
4992 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
4995 | ins_ABC // RA = dst, RB = table, RC = byte literal
4996 | checktab RB, ->vmeta_tgetb
4997 | mov TAB:RB, [BASE+RB*8]
4998 | cmp RC, TAB:RB->asize
5001 | add RC, TAB:RB->array
5002 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
5004 | // Get array slot.
5007 | mov [BASE+RA*8], RBa
5011 | mov [BASE+RA*8], RB
5012 | mov [BASE+RA*8+4], RC
5017 |2: // Check for __index if table value is nil.
5018 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5020 | mov TAB:RA, TAB:RB->metatable
5021 | test byte TAB:RA->nomm, 1<<MM_index
5022 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
5023 | movzx RA, PC_RA // Restore RA.
5025 | mov dword [BASE+RA*8+4], LJ_TNIL
5030 | ins_ABC // RA = src, RB = table, RC = key
5031 | checktab RB, ->vmeta_tsetv
5032 | mov TAB:RB, [BASE+RB*8]
5037 | mov RC, dword [BASE+RC*8]
5039 | // Convert number to int and back and compare.
5042 | movsd xmm0, qword [BASE+RC*8]
5045 | ucomisd xmm0, xmm1
5048 | fld qword [BASE+RC*8]
5051 | fcomparepp // eax (RC) modified!
5055 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
5057 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
5060 | add RC, TAB:RB->array
5061 | cmp dword [RC+4], LJ_TNIL
5062 | je >3 // Previous value is nil?
5064 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5066 |2: // Set array slot.
5068 | mov RBa, [BASE+RA*8]
5071 | mov RB, [BASE+RA*8+4]
5072 | mov RA, [BASE+RA*8]
5078 |3: // Check for __newindex if previous value is nil.
5079 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5081 | mov TAB:RA, TAB:RB->metatable
5082 | test byte TAB:RA->nomm, 1<<MM_newindex
5083 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
5084 | movzx RA, PC_RA // Restore RA.
5088 | checkstr RC, ->vmeta_tsetv
5089 | mov STR:RC, [BASE+RC*8]
5092 |7: // Possible table write barrier for the value. Skip valiswhite check.
5093 | barrierback TAB:RB, RA
5094 | movzx RA, PC_RA // Restore RA.
5098 | ins_ABC // RA = src, RB = table, RC = str const (~)
5100 | mov STR:RC, [KBASE+RC*4]
5101 | checktab RB, ->vmeta_tsets
5102 | mov TAB:RB, [BASE+RB*8]
5103 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
5104 | mov RA, TAB:RB->hmask
5105 | and RA, STR:RC->hash
5107 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
5108 | add NODE:RA, TAB:RB->node
5110 | cmp dword NODE:RA->key.it, LJ_TSTR
5112 | cmp dword NODE:RA->key.gcr, STR:RC
5114 | // Ok, key found. Assumes: offsetof(Node, val) == 0
5115 | cmp dword [RA+4], LJ_TNIL
5116 | je >4 // Previous value is nil?
5118 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5120 |3: // Set node value.
5123 | mov RBa, [BASE+RC*8]
5126 | mov RB, [BASE+RC*8+4]
5127 | mov RC, [BASE+RC*8]
5133 |4: // Check for __newindex if previous value is nil.
5134 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5136 | mov TMP1, RA // Save RA.
5137 | mov TAB:RA, TAB:RB->metatable
5138 | test byte TAB:RA->nomm, 1<<MM_newindex
5139 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5140 | mov RA, TMP1 // Restore RA.
5143 |5: // Follow hash chain.
5144 | mov NODE:RA, NODE:RA->next
5145 | test NODE:RA, NODE:RA
5147 | // End of hash chain: key not found, add a new one.
5149 | // But check for __newindex first.
5150 | mov TAB:RA, TAB:RB->metatable
5151 | test TAB:RA, TAB:RA
5152 | jz >6 // No metatable: continue.
5153 | test byte TAB:RA->nomm, 1<<MM_newindex
5154 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5158 | mov TMP3, TAB:RB // Save TAB:RB for us.
5160 | mov L:CARG1d, SAVE_L
5161 | mov L:CARG1d->base, BASE
5163 | mov CARG2d, TAB:RB
5164 | mov L:RB, L:CARG1d
5166 | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
5171 | mov L:RB->base, BASE
5174 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
5175 | // Handles write barrier for the new key. TValue * returned in eax (RC).
5176 | mov BASE, L:RB->base
5177 | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
5179 | jmp <2 // Must check write barrier for value.
5181 |7: // Possible table write barrier for the value. Skip valiswhite check.
5182 | barrierback TAB:RB, RC // Destroys STR:RC.
5186 | ins_ABC // RA = src, RB = table, RC = byte literal
5187 | checktab RB, ->vmeta_tsetb
5188 | mov TAB:RB, [BASE+RB*8]
5189 | cmp RC, TAB:RB->asize
5192 | add RC, TAB:RB->array
5193 | cmp dword [RC+4], LJ_TNIL
5194 | je >3 // Previous value is nil?
5196 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5198 |2: // Set array slot.
5200 | mov RAa, [BASE+RA*8]
5203 | mov RB, [BASE+RA*8+4]
5204 | mov RA, [BASE+RA*8]
5210 |3: // Check for __newindex if previous value is nil.
5211 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5213 | mov TAB:RA, TAB:RB->metatable
5214 | test byte TAB:RA->nomm, 1<<MM_newindex
5215 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
5216 | movzx RA, PC_RA // Restore RA.
5219 |7: // Possible table write barrier for the value. Skip valiswhite check.
5220 | barrierback TAB:RB, RA
5221 | movzx RA, PC_RA // Restore RA.
5226 | ins_AD // RA = base (table at base-1), RD = num const (start index)
5227 | mov TMP1, KBASE // Need one more free register.
5228 | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
5230 | lea RA, [BASE+RA*8]
5231 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
5232 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5237 | jz >4 // Nothing to copy?
5238 | add RD, KBASE // Compute needed size.
5239 | cmp RD, TAB:RB->asize
5240 | ja >5 // Doesn't fit into array part?
5243 | add KBASE, TAB:RB->array
5244 |3: // Copy result slots to table.
5263 |5: // Need to resize array part.
5265 | mov L:CARG1d, SAVE_L
5266 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
5267 | mov CARG2d, TAB:RB
5269 | mov L:RB, L:CARG1d
5273 | mov L:RB->base, BASE
5278 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
5279 | mov BASE, L:RB->base
5280 | movzx RA, PC_RA // Restore RA.
5283 |7: // Possible table write barrier for any value. Skip valiswhite check.
5284 | barrierback TAB:RB, RD
5288 /* -- Calls and vararg handling ----------------------------------------- */
5290 case BC_CALL: case BC_CALLM:
5291 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
5292 if (op == BC_CALLM) {
5293 | add NARGS:RD, MULTRES
5295 | cmp dword [BASE+RA*8+4], LJ_TFUNC
5296 | mov LFUNC:RB, [BASE+RA*8]
5297 | jne ->vmeta_call_ra
5298 | lea BASE, [BASE+RA*8+8]
5303 | ins_AD // RA = base, RD = extra_nargs
5304 | add NARGS:RD, MULTRES
5305 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
5308 | ins_AD // RA = base, RD = nargs+1
5309 | lea RA, [BASE+RA*8+8]
5310 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
5311 | mov LFUNC:RB, [RA-8]
5312 | cmp dword [RA-4], LJ_TFUNC
5316 | test PC, FRAME_TYPE
5319 | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
5320 | mov MULTRES, NARGS:RD
5323 |2: // Move args down.
5339 | mov LFUNC:RB, [BASE-8]
5341 | mov NARGS:RD, MULTRES
5342 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
5347 |5: // Tailcall to a fast function.
5348 | test PC, FRAME_TYPE // Lua frame below?
5352 | lea RA, [BASE+RA*8]
5353 | mov LFUNC:KBASE, [RA-8] // Need to prepare KBASE.
5354 | mov KBASE, LFUNC:KBASE->pc
5355 | mov KBASE, [KBASE+PC2PROTO(k)]
5358 |7: // Tailcall from a vararg function.
5359 | sub PC, FRAME_VARG
5360 | test PC, FRAME_TYPEP
5361 | jnz >8 // Vararg frame below?
5362 | sub BASE, PC // Need to relocate BASE/KBASE down.
5367 | add PC, FRAME_VARG
5372 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
5373 | lea RA, [BASE+RA*8+8] // fb = base+1
5375 | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
5376 | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
5380 | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
5384 | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
5389 | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
5391 | mov [RA-8], LFUNC:RB
5393 | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
5401 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
5403 | // NYI: add hotloop, record BC_ITERN.
5405 | mov TMP1, KBASE // Need two more free registers.
5406 | mov TMP2, DISPATCH
5407 | mov TAB:RB, [BASE+RA*8-16]
5408 | mov RC, [BASE+RA*8-8] // Get index from control var.
5409 | mov DISPATCH, TAB:RB->asize
5411 | mov KBASE, TAB:RB->array
5412 |1: // Traverse array part.
5413 | cmp RC, DISPATCH; jae >5 // Index points after array part?
5414 | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
5416 | mov dword [BASE+RA*8+4], LJ_TISNUM
5417 | mov dword [BASE+RA*8], RC
5421 | fild dword [BASE+RA*8-8]
5423 | // Copy array slot to returned value.
5425 | mov RBa, [KBASE+RC*8]
5426 | mov [BASE+RA*8+8], RBa
5428 | mov RB, [KBASE+RC*8+4]
5429 | mov [BASE+RA*8+12], RB
5430 | mov RB, [KBASE+RC*8]
5431 | mov [BASE+RA*8+8], RB
5434 | // Return array index as a numeric key.
5438 | movsd qword [BASE+RA*8], xmm0
5440 | fstp qword [BASE+RA*8]
5442 | mov [BASE+RA*8-8], RC // Update control var.
5444 | movzx RD, PC_RD // Get target from ITERL.
5447 | mov DISPATCH, TMP2
5451 |4: // Skip holes in array part.
5453 if (!LJ_DUALNUM && !sse) {
5454 | mov [BASE+RA*8-8], RC
5458 |5: // Traverse hash part.
5461 | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
5462 | imul KBASE, RC, #NODE
5463 | add NODE:KBASE, TAB:RB->node
5464 | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
5465 | lea DISPATCH, [RC+DISPATCH+1]
5466 | // Copy key and value from hash slot.
5468 | mov RBa, NODE:KBASE->key
5469 | mov RCa, NODE:KBASE->val
5470 | mov [BASE+RA*8], RBa
5471 | mov [BASE+RA*8+8], RCa
5473 | mov RB, NODE:KBASE->key.gcr
5474 | mov RC, NODE:KBASE->key.it
5475 | mov [BASE+RA*8], RB
5476 | mov [BASE+RA*8+4], RC
5477 | mov RB, NODE:KBASE->val.gcr
5478 | mov RC, NODE:KBASE->val.it
5479 | mov [BASE+RA*8+8], RB
5480 | mov [BASE+RA*8+12], RC
5482 | mov [BASE+RA*8-8], DISPATCH
5485 |7: // Skip holes in hash part.
5491 | ins_AD // RA = base, RD = target (points to ITERN)
5492 | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
5493 | mov CFUNC:RB, [BASE+RA*8-24]
5494 | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
5495 | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
5496 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
5498 | mov dword [BASE+RA*8-8], 0 // Initialize control var.
5501 |5: // Despecialize bytecode if any of the checks fail.
5504 | mov byte [PC], BC_ITERC
5509 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
5510 | mov TMP1, KBASE // Need one more free register.
5511 | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
5512 | lea RA, [BASE+RA*8]
5513 | sub KBASE, [BASE-4]
5514 | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
5516 | jz >5 // Copy all varargs?
5517 | lea RB, [RA+RB*8-8]
5518 | cmp KBASE, BASE // No vararg slots?
5520 |1: // Copy vararg slots to destination slots.
5522 | mov RCa, [KBASE-8]
5533 | cmp RA, RB // All destination slots filled?
5535 | cmp KBASE, BASE // No more vararg slots?
5537 |2: // Fill up remainder with nil.
5538 | mov dword [RA+4], LJ_TNIL
5546 |5: // Copy all varargs.
5547 | mov MULTRES, 1 // MULTRES = 0+1
5550 | jbe <3 // No vararg slots?
5554 | mov MULTRES, RB // MULTRES = #varargs+1
5557 | cmp RC, L:RB->maxstack
5558 | ja >7 // Need to grow stack?
5559 |6: // Copy all vararg slots.
5561 | mov RCa, [KBASE-8]
5572 | cmp KBASE, BASE // No more vararg slots?
5576 |7: // Grow stack for varargs.
5577 | mov L:RB->base, BASE
5580 | sub KBASE, BASE // Need delta, because BASE may change.
5581 | mov FCARG2, MULTRES
5584 | call extern lj_state_growstack@8 // (lua_State *L, int n)
5585 | mov BASE, L:RB->base
5591 /* -- Returns ----------------------------------------------------------- */
5594 | ins_AD // RA = results, RD = extra_nresults
5595 | add RD, MULTRES // MULTRES >=1, so RD >=1.
5596 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
5599 case BC_RET: case BC_RET0: case BC_RET1:
5600 | ins_AD // RA = results, RD = nresults+1
5601 if (op != BC_RET0) {
5606 | mov MULTRES, RD // Save nresults+1.
5607 | test PC, FRAME_TYPE // Check frame type marker.
5608 | jnz >7 // Not returning to a fixarg Lua func?
5612 | mov KBASE, BASE // Use KBASE for result move.
5615 |2: // Move results down.
5617 | mov RBa, [KBASE+RA]
5618 | mov [KBASE-8], RBa
5620 | mov RB, [KBASE+RA]
5622 | mov RB, [KBASE+RA+4]
5629 | mov RD, MULTRES // Note: MULTRES may be >255.
5630 | movzx RB, PC_RB // So cannot compare with RDL!
5632 | cmp RB, RD // More results expected?
5637 | mov RBa, [BASE+RA]
5640 | mov RB, [BASE+RA+4]
5648 | cmp PC_RB, RDL // More results expected?
5654 | not RAa // Note: ~RA = -(RA+1)
5655 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
5656 | mov LFUNC:KBASE, [BASE-8]
5657 | mov KBASE, LFUNC:KBASE->pc
5658 | mov KBASE, [KBASE+PC2PROTO(k)]
5661 |6: // Fill up results with nil.
5663 | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
5666 | mov dword [BASE+RD*8-12], LJ_TNIL
5671 |7: // Non-standard return case.
5672 | lea RB, [PC-FRAME_VARG]
5673 | test RB, FRAME_TYPEP
5675 | // Return from vararg function: relocate BASE down and RA up.
5677 if (op != BC_RET0) {
5683 /* -- Loops and branches ------------------------------------------------ */
5685 |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
5686 |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
5687 |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
5688 |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
5694 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5704 vk = (op == BC_IFORL || op == BC_JFORL);
5705 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
5706 | lea RA, [BASE+RA*8]
5708 | cmp FOR_TIDX, LJ_TISNUM; jne >9
5710 | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5711 | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5712 | mov RB, dword FOR_IDX
5713 | cmp dword FOR_STEP, 0; jl >5
5715 #ifdef LUA_USE_ASSERT
5716 | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5717 | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5719 | mov RB, dword FOR_STEP
5720 | test RB, RB; js >5
5721 | add RB, dword FOR_IDX; jo >1
5722 | mov dword FOR_IDX, RB
5724 | cmp RB, dword FOR_STOP
5725 | mov FOR_TEXT, LJ_TISNUM
5726 | mov dword FOR_EXT, RB
5727 if (op == BC_FORI) {
5732 } else if (op == BC_JFORI) {
5738 } else if (op == BC_IFORL) {
5751 |5: // Invert check for negative step.
5753 | add RB, dword FOR_IDX; jo <1
5754 | mov dword FOR_IDX, RB
5756 | cmp RB, dword FOR_STOP
5757 | mov FOR_TEXT, LJ_TISNUM
5758 | mov dword FOR_EXT, RB
5759 if (op == BC_FORI) {
5761 } else if (op == BC_JFORI) {
5765 } else if (op == BC_IFORL) {
5771 |9: // Fallback to FP variant.
5773 | cmp FOR_TIDX, LJ_TISNUM
5777 | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5779 #ifdef LUA_USE_ASSERT
5780 | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5781 | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5784 | mov RB, FOR_TSTEP // Load type/hiword of for step.
5786 | cmp RB, LJ_TISNUM; jae ->vmeta_for
5789 | movsd xmm0, qword FOR_IDX
5790 | movsd xmm1, qword FOR_STOP
5792 | addsd xmm0, qword FOR_STEP
5793 | movsd qword FOR_IDX, xmm0
5794 | test RB, RB; js >3
5798 | ucomisd xmm1, xmm0
5800 | movsd qword FOR_EXT, xmm0
5802 | fld qword FOR_STOP
5805 | fadd qword FOR_STEP // nidx = idx + step
5808 | test RB, RB; js >1
5813 | fxch // Swap lim/(n)idx if step non-negative.
5815 | fcomparepp // eax (RD) modified if !cmov.
5817 | movzx RD, PC_RD // Need to reload RD.
5820 if (op == BC_FORI) {
5827 } else if (op == BC_JFORI) {
5831 } else if (op == BC_IFORL) {
5848 |3: // Invert comparison if step is negative.
5849 | ucomisd xmm0, xmm1
5858 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5866 | ins_AJ // RA = base, RD = target
5867 | lea RA, [BASE+RA*8]
5869 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
5870 if (op == BC_JITERL) {
5876 | branchPC RD // Otherwise save control var + branch.
5886 | ins_A // RA = base, RD = target (loop extent)
5887 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5888 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5892 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5896 | ins_A // RA = base, RD = target (loop extent)
5902 | ins_AD // RA = base (ignored), RD = traceno
5903 | mov RA, [DISPATCH+DISPATCH_J(trace)]
5904 | mov TRACE:RD, [RA+RD*4]
5905 | mov RDa, TRACE:RD->mcode
5907 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5908 | mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB
5909 | // Save additional callee-save registers only used in compiled code.
5917 | movdqa [RAa], xmm6
5918 | movdqa [RAa-1*16], xmm7
5919 | movdqa [RAa-2*16], xmm8
5920 | movdqa [RAa-3*16], xmm9
5921 | movdqa [RAa-4*16], xmm10
5922 | movdqa [RAa-5*16], xmm11
5923 | movdqa [RAa-6*16], xmm12
5924 | movdqa [RAa-7*16], xmm13
5925 | movdqa [RAa-8*16], xmm14
5926 | movdqa [RAa-9*16], xmm15
5937 | ins_AJ // RA = unused, RD = target
5942 /* -- Function headers -------------------------------------------------- */
5945 ** Reminder: A function may be called with func/args above L->maxstack,
5946 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5947 ** too. This means all FUNC* ops (including fast functions) must check
5948 ** for stack overflow _before_ adding more slots!
5955 case BC_FUNCV: /* NYI: compiled vararg functions. */
5956 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5964 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5965 | mov KBASE, [PC-4+PC2PROTO(k)]
5967 | lea RA, [BASE+RA*8] // Top of frame.
5968 | cmp RA, L:RB->maxstack
5969 | ja ->vm_growstack_f
5970 | movzx RA, byte [PC-4+PC2PROTO(numparams)]
5971 | cmp NARGS:RD, RA // Check for missing parameters.
5974 if (op == BC_JFUNCF) {
5981 |3: // Clear missing parameters.
5982 | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5993 | int3 // NYI: compiled vararg functions
5994 break; /* NYI: compiled vararg functions. */
5997 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5998 | lea RB, [NARGS:RD*8+FRAME_VARG]
5999 | lea RD, [BASE+NARGS:RD*8]
6000 | mov LFUNC:KBASE, [BASE-8]
6001 | mov [RD-4], RB // Store delta + FRAME_VARG.
6002 | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
6005 | cmp RA, L:RB->maxstack
6006 | ja ->vm_growstack_v // Need to grow stack.
6009 | movzx RB, byte [PC-4+PC2PROTO(numparams)]
6012 |1: // Copy fixarg slots up to new frame.
6015 | jnb >3 // Less args than parameters?
6021 | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
6025 if (op == BC_JFUNCV) {
6029 | mov KBASE, [PC-4+PC2PROTO(k)]
6033 |3: // Clear missing parameters.
6034 | mov dword [RD+4], LJ_TNIL
6043 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
6044 | mov CFUNC:RB, [BASE-8]
6045 | mov KBASEa, CFUNC:RB->f
6047 | lea RD, [BASE+NARGS:RD*8-8]
6048 | mov L:RB->base, BASE
6049 | lea RA, [RD+8*LUA_MINSTACK]
6050 | cmp RA, L:RB->maxstack
6052 if (op == BC_FUNCC) {
6054 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
6061 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
6067 | ja ->vm_growstack_c // Need to grow stack.
6069 if (op == BC_FUNCC) {
6070 | call KBASEa // (lua_State *L)
6072 | // (lua_State *L, lua_CFunction f)
6073 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
6075 | set_vmstate INTERP
6076 | // nresults returned in eax (RD).
6077 | mov BASE, L:RB->base
6078 | lea RA, [BASE+RD*8]
6080 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
6081 | mov PC, [BASE-4] // Fetch PC of caller.
6085 /* ---------------------------------------------------------------------- */
6088 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
6094 static int build_backend(BuildCtx *ctx)
6099 #ifdef LUAJIT_CPU_NOCMOV
6102 #if defined(LUAJIT_CPU_SSE2) || defined(LJ_TARGET_X64)
6106 dasm_growpc(Dst, BC__MAX);
6108 build_subroutines(ctx, cmov, sse);
6111 for (op = 0; op < BC__MAX; op++)
6112 build_ins(ctx, (BCOp)op, op, cmov, sse);
6117 /* Emit pseudo frame-info for all assembler functions. */
6118 static void emit_asm_debug(BuildCtx *ctx)
6120 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
6124 #define REG_SP "0x7"
6125 #define REG_RA "0x10"
6129 #define REG_SP "0x4"
6130 #define REG_RA "0x8"
6132 switch (ctx->mode) {
6134 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
6137 "\t.long .LECIE0-.LSCIE0\n"
6139 "\t.long 0xffffffff\n"
6143 "\t.sleb128 -" SZPTR "\n"
6144 "\t.byte " REG_RA "\n"
6145 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6146 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6147 "\t.align " SZPTR "\n"
6151 "\t.long .LEFDE0-.LASFDE0\n"
6153 "\t.long .Lframe0\n"
6157 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6158 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6159 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6160 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6161 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6165 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6166 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6167 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6168 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6169 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6171 "\t.align " SZPTR "\n"
6172 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
6176 "\t.long .LEFDE1-.LASFDE1\n"
6178 "\t.long .Lframe0\n"
6180 "\t.quad lj_vm_ffi_call\n"
6182 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6183 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6184 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6185 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6187 "\t.long lj_vm_ffi_call\n"
6189 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6190 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6191 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6192 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6194 "\t.align " SZPTR "\n"
6195 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
6197 #if (defined(__sun__) && defined(__svr4__)) || defined(__solaris_)
6198 fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
6200 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
6204 "\t.long .LECIE1-.LSCIE1\n"
6208 "\t.string \"zPR\"\n"
6210 "\t.sleb128 -" SZPTR "\n"
6211 "\t.byte " REG_RA "\n"
6212 "\t.uleb128 6\n" /* augmentation length */
6213 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6214 "\t.long lj_err_unwind_dwarf-.\n"
6215 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6216 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6217 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6218 "\t.align " SZPTR "\n"
6222 "\t.long .LEFDE2-.LASFDE2\n"
6224 "\t.long .LASFDE2-.Lframe1\n"
6225 "\t.long .Lbegin-.\n"
6227 "\t.uleb128 0\n" /* augmentation length */
6228 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6230 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6231 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6232 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6233 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6235 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6236 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6237 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6238 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6240 "\t.align " SZPTR "\n"
6241 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
6245 "\t.long .LECIE2-.LSCIE2\n"
6249 "\t.string \"zR\"\n"
6251 "\t.sleb128 -" SZPTR "\n"
6252 "\t.byte " REG_RA "\n"
6253 "\t.uleb128 1\n" /* augmentation length */
6254 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6255 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6256 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6257 "\t.align " SZPTR "\n"
6261 "\t.long .LEFDE3-.LASFDE3\n"
6263 "\t.long .LASFDE3-.Lframe2\n"
6264 "\t.long lj_vm_ffi_call-.\n"
6266 "\t.uleb128 0\n" /* augmentation length */
6268 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6269 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6270 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6271 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6273 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6274 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6275 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6276 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6278 "\t.align " SZPTR "\n"
6279 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
6283 fprintf(ctx->fp, "\t.section .eh_frame,\"dr\"\n");
6285 "\t.def %slj_err_unwind_dwarf; .scl 2; .type 32; .endef\n",
6289 "\t.long LECIE1-LSCIE1\n"
6293 "\t.string \"zP\"\n"
6295 "\t.sleb128 -" SZPTR "\n"
6296 "\t.byte " REG_RA "\n"
6297 "\t.uleb128 5\n" /* augmentation length */
6298 "\t.byte 0x00\n" /* absptr */
6299 "\t.long %slj_err_unwind_dwarf\n"
6300 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6301 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6302 "\t.align " SZPTR "\n"
6303 "LECIE1:\n\n", LJ_32 ? "_" : "");
6306 "\t.long LEFDE1-LASFDE1\n"
6308 "\t.long LASFDE1-Lframe1\n"
6309 "\t.long %slj_vm_asm_begin\n"
6311 "\t.uleb128 0\n" /* augmentation length */
6312 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6314 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6315 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6316 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6317 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6319 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6320 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6321 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6322 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6324 "\t.align " SZPTR "\n"
6325 "LEFDE1:\n\n", LJ_32 ? "_" : "", (int)ctx->codesz, CFRAME_SIZE);
6327 /* Mental note: never let Apple design an assembler.
6328 ** Or a linker. Or a plastic case. But I digress.
6330 case BUILD_machasm: {
6335 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
6338 "\t.set L$set$x,LECIEX-LSCIEX\n"
6343 "\t.ascii \"zPR\\0\"\n"
6345 "\t.byte 128-" SZPTR "\n"
6346 "\t.byte " REG_RA "\n"
6347 "\t.byte 6\n" /* augmentation length */
6348 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
6350 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
6351 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6352 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6354 "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
6355 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6356 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
6358 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6359 "\t.align " BSZPTR "\n"
6361 for (i = 0; i < ctx->nsym; i++) {
6362 const char *name = ctx->sym[i].name;
6363 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
6364 if (size == 0) continue;
6366 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
6371 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
6372 "\t.long L$set$%d\n"
6374 "\t.long LASFDE%d-EH_frame1\n"
6377 "\t.byte 0\n" /* augmentation length */
6378 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
6380 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6381 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6382 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
6383 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
6385 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6386 "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
6387 "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
6388 "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
6390 "\t.align " BSZPTR "\n"
6392 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
6398 "\t.set L$set$y,LECIEY-LSCIEY\n"
6403 "\t.ascii \"zR\\0\"\n"
6405 "\t.byte 128-" SZPTR "\n"
6406 "\t.byte " REG_RA "\n"
6407 "\t.byte 1\n" /* augmentation length */
6409 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6410 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6412 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6413 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
6415 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6416 "\t.align " BSZPTR "\n"
6419 "_lj_vm_ffi_call.eh:\n"
6421 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
6422 "\t.long L$set$yy\n"
6424 "\t.long LASFDEY-EH_frame2\n"
6425 "\t.long _lj_vm_ffi_call-.\n"
6427 "\t.byte 0\n" /* augmentation length */
6429 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
6430 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6431 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6432 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6434 "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
6435 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6436 "\t.byte 0xd\n\t.uleb128 0x4\n" /* def_cfa_register ebp */
6437 "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
6439 "\t.align " BSZPTR "\n"
6440 "LEFDEY:\n\n", fcsize);
6444 fprintf(ctx->fp, "\t.subsections_via_symbols\n");
6447 "\t.non_lazy_symbol_pointer\n"
6448 "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
6449 ".indirect_symbol _lj_err_unwind_dwarf\n"
6454 default: /* Difficult for other modes. */