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 |//-----------------------------------------------------------------------
27 |// Fixed register assignments for the interpreter.
28 |// This is very fragile and has many dependencies. Caveat emptor.
29 |.define BASE, edx // Not C callee-save, refetched anyway.
31 |.define KBASE, edi // Must be C callee-save.
32 |.define KBASEa, KBASE
33 |.define PC, esi // Must be C callee-save.
35 |.define DISPATCH, ebx // Must be C callee-save.
37 |.define KBASE, edi // Must be C callee-save.
39 |.define PC, esi // Must be C callee-save.
41 |.define DISPATCH, ebx // Must be C callee-save.
43 |.define KBASE, r15d // Must be C callee-save.
45 |.define PC, ebx // Must be C callee-save.
47 |.define DISPATCH, r14d // Must be C callee-save.
53 |.define RB, ebp // Must be ebp (C callee-save).
54 |.define RC, eax // Must be eax.
75 |.define FCARG1, ecx // x86 fastcall arguments.
78 |.define CARG1, rcx // x64/WIN64 C call arguments.
86 |.define FCARG1, CARG1d // Upwards compatible to x86 fastcall.
87 |.define FCARG2, CARG2d
89 |.define CARG1, rdi // x64/POSIX C call arguments.
101 |.define FCARG1, CARG1d // Simulate x86 fastcall.
102 |.define FCARG2, CARG2d
105 |// Type definitions. Some of these are only used for documentation.
107 |.type GL, global_State
108 |.type TVALUE, TValue
112 |.type LFUNC, GCfuncL
113 |.type CFUNC, GCfuncC
114 |.type PROTO, GCproto
115 |.type UPVAL, GCupval
118 |.type TRACE, GCtrace
120 |// Stack layout while in interpreter. Must match with lj_frame.h.
121 |//-----------------------------------------------------------------------
122 |.if not X64 // x86 stack layout.
124 |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
126 | push edi; push esi; push ebx
127 | sub esp, CFRAME_SPACE
130 | push ebp; saveregs_
133 | add esp, CFRAME_SPACE
134 | pop ebx; pop esi; pop edi; pop ebp
137 |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
138 |.define SAVE_NRES, aword [esp+aword*14]
139 |.define SAVE_CFRAME, aword [esp+aword*13]
140 |.define SAVE_L, aword [esp+aword*12]
141 |//----- 16 byte aligned, ^^^ arguments from C caller
142 |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
143 |.define SAVE_R4, aword [esp+aword*10]
144 |.define SAVE_R3, aword [esp+aword*9]
145 |.define SAVE_R2, aword [esp+aword*8]
146 |//----- 16 byte aligned
147 |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
148 |.define SAVE_PC, aword [esp+aword*6]
149 |.define TMP2, aword [esp+aword*5]
150 |.define TMP1, aword [esp+aword*4]
151 |//----- 16 byte aligned
152 |.define ARG4, aword [esp+aword*3]
153 |.define ARG3, aword [esp+aword*2]
154 |.define ARG2, aword [esp+aword*1]
155 |.define ARG1, aword [esp] //<-- esp while in interpreter.
156 |//----- 16 byte aligned, ^^^ arguments for C callee
158 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
159 |.define FPARG3, qword [esp+qword*1]
160 |.define FPARG1, qword [esp]
161 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
162 |.define TMPQ, qword [esp+aword*4]
166 |.define MULTRES, TMP2
168 |// Arguments for vm_call and vm_pcall.
169 |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
171 |// Arguments for vm_cpcall.
172 |.define INARG_CP_CALL, SAVE_ERRF
173 |.define INARG_CP_UD, SAVE_NRES
174 |.define INARG_CP_FUNC, SAVE_CFRAME
176 |//-----------------------------------------------------------------------
177 |.elif X64WIN // x64/Windows stack layout
179 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
181 | push rdi; push rsi; push rbx
182 | sub rsp, CFRAME_SPACE
185 | push rbp; saveregs_
188 | add rsp, CFRAME_SPACE
189 | pop rbx; pop rsi; pop rdi; pop rbp
192 |.define SAVE_CFRAME, aword [rsp+aword*13]
193 |.define SAVE_PC, dword [rsp+dword*25]
194 |.define SAVE_L, dword [rsp+dword*24]
195 |.define SAVE_ERRF, dword [rsp+dword*23]
196 |.define SAVE_NRES, dword [rsp+dword*22]
197 |.define TMP2, dword [rsp+dword*21]
198 |.define TMP1, dword [rsp+dword*20]
199 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
200 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
201 |.define SAVE_R4, aword [rsp+aword*8]
202 |.define SAVE_R3, aword [rsp+aword*7]
203 |.define SAVE_R2, aword [rsp+aword*6]
204 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
205 |.define ARG5, aword [rsp+aword*4]
206 |.define CSAVE_4, aword [rsp+aword*3]
207 |.define CSAVE_3, aword [rsp+aword*2]
208 |.define CSAVE_2, aword [rsp+aword*1]
209 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
210 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
212 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
213 |.define TMPQ, qword [rsp+aword*10]
214 |.define MULTRES, TMP2
216 |.define ARG5d, dword [rsp+aword*4]
219 |//-----------------------------------------------------------------------
220 |.else // x64/POSIX stack layout
222 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
224 | push rbx; push r15; push r14
225 | sub rsp, CFRAME_SPACE
228 | push rbp; saveregs_
231 | add rsp, CFRAME_SPACE
232 | pop r14; pop r15; pop rbx; pop rbp
235 |//----- 16 byte aligned,
236 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
237 |.define SAVE_R4, aword [rsp+aword*8]
238 |.define SAVE_R3, aword [rsp+aword*7]
239 |.define SAVE_R2, aword [rsp+aword*6]
240 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
241 |.define SAVE_CFRAME, aword [rsp+aword*4]
242 |.define SAVE_PC, dword [rsp+dword*7]
243 |.define SAVE_L, dword [rsp+dword*6]
244 |.define SAVE_ERRF, dword [rsp+dword*5]
245 |.define SAVE_NRES, dword [rsp+dword*4]
246 |.define TMPa, aword [rsp+aword*1]
247 |.define TMP2, dword [rsp+dword*1]
248 |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
249 |//----- 16 byte aligned
251 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
252 |.define TMPQ, qword [rsp]
253 |.define TMP3, dword [rsp+aword*1]
254 |.define MULTRES, TMP2
258 |//-----------------------------------------------------------------------
260 |// Instruction headers.
261 |.macro ins_A; .endmacro
262 |.macro ins_AD; .endmacro
263 |.macro ins_AJ; .endmacro
264 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
265 |.macro ins_AB_; movzx RB, RCH; .endmacro
266 |.macro ins_A_C; movzx RC, RCL; .endmacro
267 |.macro ins_AND; not RDa; .endmacro
269 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
277 | jmp aword [DISPATCH+OP*8]
279 | jmp aword [DISPATCH+OP*4]
283 |// Instruction footer.
285 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
286 | .define ins_next, ins_NEXT
287 | .define ins_next_, ins_NEXT
289 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
290 | // Affects only certain kinds of benchmarks (and only with -j off).
291 | // Around 10%-30% slower on Core2, a lot more slower on P4.
301 |// Call decode and dispatch.
303 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
304 | mov PC, LFUNC:RB->pc
310 | jmp aword [DISPATCH+OP*8]
312 | jmp aword [DISPATCH+OP*4]
317 | // BASE = new base, RB = LFUNC, RD = nargs+1
322 |//-----------------------------------------------------------------------
324 |// Macros to test operand types.
325 |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
326 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
327 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
328 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
329 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
331 |// These operands must be used with movzx.
332 |.define PC_OP, byte [PC-4]
333 |.define PC_RA, byte [PC-3]
334 |.define PC_RB, byte [PC-1]
335 |.define PC_RC, byte [PC-2]
336 |.define PC_RD, word [PC-2]
338 |.macro branchPC, reg
339 | lea PC, [PC+reg*4-BCBIAS_J*4]
342 |// Assumes DISPATCH is relative to GL.
343 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
344 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
346 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
348 |// Decrement hashed hotcount and trigger trace recorder if zero.
352 | and reg, HOTCOUNT_PCMASK
353 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
360 | and reg, HOTCOUNT_PCMASK
361 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
365 |// Set current VM state.
366 |.macro set_vmstate, st
367 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
371 |.macro fcomparepp // Compare and pop st0 >< st1.
376 |.macro fdup; fld st0; .endmacro
377 |.macro fpop1; fstp st1; .endmacro
379 |// Synthesize SSE FP constants.
380 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
382 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
384 | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
388 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
390 | mov64 tmp, U64x(val,00000000); movd reg, tmp
392 | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
396 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
397 | sseconst_hi reg, tmp, 80000000
399 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
400 | sseconst_hi reg, tmp, 3ff00000
402 |.macro sseconst_m1, reg, tmp // Synthesize -1.0.
403 | sseconst_hi reg, tmp, bff00000
405 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
406 | sseconst_hi reg, tmp, 43300000
408 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
409 | sseconst_hi reg, tmp, 43380000
412 |// Move table write barrier back. Overwrites reg.
413 |.macro barrierback, tab, reg
414 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
415 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
416 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
417 | mov tab->gclist, reg
420 |//-----------------------------------------------------------------------
422 /* Generate subroutines used by opcodes and other parts of the VM. */
423 /* The .code_sub section should be last to help static branch prediction. */
424 static void build_subroutines(BuildCtx *ctx)
428 |//-----------------------------------------------------------------------
429 |//-- Return handling ----------------------------------------------------
430 |//-----------------------------------------------------------------------
436 | // Return from pcall or xpcall fast func.
438 | sub BASE, PC // Restore caller base.
439 | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
440 | mov PC, [BASE-4] // Fetch PC of previous frame.
441 | // Prepending may overwrite the pcall frame, so do it at the end.
442 | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
445 | add RD, 1 // RD = nresults+1
447 | test PC, FRAME_TYPE
448 | jz ->BC_RET_Z // Handle regular return to Lua.
451 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
453 | test PC, FRAME_TYPE
460 | neg PC // Previous base = BASE - delta.
464 |1: // Move results down.
471 | mov RB, [BASE+RA+4]
482 | mov RA, SAVE_NRES // RA = wanted nresults+1
485 | jne >6 // More/less results wanted?
488 | mov L:RB->top, BASE
491 | mov RAa, SAVE_CFRAME // Restore previous C frame.
492 | mov L:RB->cframe, RAa
493 | xor eax, eax // Ok return status for vm_pcall.
500 | jb >7 // Less results wanted?
501 | // More results wanted. Check stack size and fill up results with nil.
502 | cmp BASE, L:RB->maxstack
504 | mov dword [BASE-4], LJ_TNIL
509 |7: // Less results wanted.
511 | jz <5 // But check for LUA_MULTRET+1.
512 | sub RA, RD // Negative result!
513 | lea BASE, [BASE+RA*8] // Correct top.
516 |8: // Corner case: need to grow stack for filling up results.
517 | // This can happen if:
518 | // - A C function grows the stack (a lot).
519 | // - The GC shrinks the stack in between.
520 | // - A return back from a lua_call() with (high) nresults adjustment.
521 | mov L:RB->top, BASE // Save current top held in BASE (yes).
522 | mov MULTRES, RD // Need to fill only remainder with nil.
525 | call extern lj_state_growstack@8 // (lua_State *L, int n)
526 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
529 |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
530 | // (void *cframe, int errcode)
532 | mov eax, CARG2d // Error return status for vm_pcall.
535 | mov eax, FCARG2 // Error return status for vm_pcall.
538 |->vm_unwind_c_eh: // Landing pad for external unwinder.
540 | mov GL:RB, L:RB->glref
541 | mov dword GL:RB->vmstate, ~LJ_VMST_C
544 |->vm_unwind_rethrow:
545 |.if X64 and not X64WIN
549 | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
552 |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
555 | and CARG1, CFRAME_RAWMASK
558 | and FCARG1, CFRAME_RAWMASK
561 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
563 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
564 | mov RD, 1+1 // Really 1+2 results, incr. later.
565 | mov BASE, L:RB->base
566 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
567 | add DISPATCH, GG_G2DISP
568 | mov PC, [BASE-4] // Fetch PC of previous frame.
569 | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
571 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
573 |//-----------------------------------------------------------------------
574 |//-- Grow stack for calls -----------------------------------------------
575 |//-----------------------------------------------------------------------
577 |->vm_growstack_c: // Grow stack for C function.
578 | mov FCARG2, LUA_MINSTACK
581 |->vm_growstack_v: // Grow stack for vararg Lua function.
585 |->vm_growstack_f: // Grow stack for fixarg Lua function.
586 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
587 | lea RD, [BASE+NARGS:RD*8-8]
589 | movzx RA, byte [PC-4+PC2PROTO(framesize)]
590 | add PC, 4 // Must point after first instruction.
591 | mov L:RB->base, BASE
596 | // RB = L, L->base = new base, L->top = top
598 | call extern lj_state_growstack@8 // (lua_State *L, int n)
599 | mov BASE, L:RB->base
601 | mov LFUNC:RB, [BASE-8]
605 | // BASE = new base, RB = LFUNC, RD = nargs+1
606 | ins_callt // Just retry the call.
608 |//-----------------------------------------------------------------------
609 |//-- Entry points into the assembler VM ---------------------------------
610 |//-----------------------------------------------------------------------
612 |->vm_resume: // Setup C frame and resume thread.
613 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
616 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
621 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
625 | lea KBASEa, [esp+CFRAME_RESUME]
626 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
627 | add DISPATCH, GG_G2DISP
628 | mov L:RB->cframe, KBASEa
629 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
630 | mov SAVE_CFRAME, RDa
635 | cmp byte L:RB->status, RDL
636 | je >3 // Initial resume (like a call).
638 | // Resume after yield (like a return).
640 | mov byte L:RB->status, RDL
641 | mov BASE, L:RB->base
645 | add RD, 1 // RD = nresults+1
646 | sub RA, BASE // RA = resultofs
649 | test PC, FRAME_TYPE
653 |->vm_pcall: // Setup protected C frame and enter VM.
654 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
658 | mov SAVE_ERRF, CARG4d
662 |->vm_call: // Setup C frame and enter VM.
663 | // (lua_State *L, TValue *base, int nres1)
667 |1: // Entry point for vm_pcall above (PC = ftype).
669 | mov SAVE_NRES, CARG3d
670 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
675 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
678 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
679 | mov SAVE_CFRAME, KBASEa
680 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
682 | mov L:RB->cframe, rsp
684 | mov L:RB->cframe, esp
687 |2: // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype).
688 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
689 | add DISPATCH, GG_G2DISP
691 |3: // Entry point for vm_resume above (RA = base, RB = L, PC = ftype).
693 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
695 | sub PC, BASE // PC = frame delta + frame type
700 | add NARGS:RD, 1 // RD = nargs+1
703 | mov LFUNC:RB, [RA-8]
704 | cmp dword [RA-4], LJ_TFUNC
705 | jne ->vmeta_call // Ensure KBASE defined and != BASE.
707 |->vm_call_dispatch_f:
710 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
712 |->vm_cpcall: // Setup protected C frame, call C.
713 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
716 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
720 | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
721 | mov RC, INARG_CP_UD // Get args before they are overwritten.
722 | mov RA, INARG_CP_FUNC
723 | mov BASE, INARG_CP_CALL
725 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
727 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
728 | sub KBASE, L:RB->top
729 | mov SAVE_ERRF, 0 // No error function.
730 | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
731 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
734 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
735 | mov SAVE_CFRAME, KBASEa
736 | mov L:RB->cframe, rsp
738 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
740 | mov ARG3, RC // Have to copy args downwards.
744 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
745 | mov SAVE_CFRAME, KBASE
746 | mov L:RB->cframe, esp
748 | call BASE // (lua_State *L, lua_CFunction func, void *ud)
750 | // TValue * (new base) or NULL returned in eax (RC).
752 | jz ->vm_leave_cp // No base? Just remove C frame.
755 | jmp <2 // Else continue with the call.
757 |//-----------------------------------------------------------------------
758 |//-- Metamethod handling ------------------------------------------------
759 |//-----------------------------------------------------------------------
761 |//-- Continuation dispatch ----------------------------------------------
764 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
768 | sub BASE, PC // Restore caller BASE.
769 | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
770 | mov RC, RA // ... in [RC]
771 | mov PC, [RB-12] // Restore PC from [cont|PC].
773 | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
778 | lea KBASEa, qword [=>0]
781 | mov RA, dword [RB-16]
787 | mov LFUNC:KBASE, [BASE-8]
788 | mov KBASE, LFUNC:KBASE->pc
789 | mov KBASE, [KBASE+PC2PROTO(k)]
790 | // BASE = base, RC = result, RB = meta base
791 | jmp RAa // Jump to continuation.
795 | je ->cont_ffi_callback // cont = 1: return from FFI callback.
796 | // cont = 0: Tail call from C function.
803 |->cont_cat: // BASE = base, RC = result, RB = mbase
806 | lea RA, [BASE+RA*8]
813 | mov L:CARG1d, SAVE_L
814 | mov L:CARG1d->base, BASE
819 | mov L:CARG1d, SAVE_L
820 | mov L:CARG1d->base, BASE
835 |//-- Table indexing metamethods -----------------------------------------
838 | mov TMP1, RC // RC = GCstr *
840 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
843 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
844 | mov [RA], TAB:RB // RB = GCtab *
845 | mov dword [RA+4], LJ_TTAB
852 | 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
941 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
945 | movzx RC, PC_RC // Reload TValue *k from RC.
946 | lea RC, [BASE+RC*8]
948 | movzx RB, PC_RB // Reload TValue *t from RB.
949 | lea RB, [BASE+RB*8]
952 | mov L:CARG1d, SAVE_L
953 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
955 | mov CARG3, RCa // May be 64 bit ptr to stack.
962 | mov L:RB->base, BASE
965 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
966 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
967 | mov BASE, L:RB->base
970 | // NOBARRIER: lj_meta_tset ensures the table is not black.
973 | mov RBa, [BASE+RA*8]
976 | mov RB, [BASE+RA*8+4]
977 | mov RA, [BASE+RA*8]
981 |->cont_nop: // BASE = base, (RC = result)
984 |3: // Call __newindex metamethod.
985 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
987 | mov [RA-12], PC // [cont|PC]
989 | // Copy value to third argument.
991 | mov RBa, [BASE+RC*8]
994 | mov RB, [BASE+RC*8+4]
995 | mov RC, [BASE+RC*8]
999 | lea PC, [RA+FRAME_CONT]
1001 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
1002 | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
1003 | jmp ->vm_call_dispatch_f
1005 |//-- Comparison metamethods ---------------------------------------------
1010 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
1012 | lea CARG3d, [BASE+RD*8]
1013 | lea CARG2d, [BASE+RA*8]
1015 | lea CARG2d, [BASE+RA*8]
1016 | lea CARG3d, [BASE+RD*8]
1018 | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
1019 | movzx CARG4d, PC_OP
1022 | lea RD, [BASE+RD*8]
1023 | lea RA, [BASE+RA*8]
1029 | mov L:RB->base, BASE
1032 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1033 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1035 | mov BASE, L:RB->base
1047 |->cont_condt: // BASE = base, RC = result
1049 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
1053 |->cont_condf: // BASE = base, RC = result
1054 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
1063 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1065 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1068 | mov CARG4d, RB // Caveat: CARG4d == RA.
1070 | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
1079 | mov L:RB->base, BASE
1082 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1083 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1090 | mov L:RB->base, BASE
1092 | mov FCARG2, dword [PC-4]
1094 | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
1095 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1099 |//-- Arithmetic metamethods ---------------------------------------------
1106 | lea RC, [KBASE+RC*8]
1114 | lea RC, [KBASE+RC*8]
1115 | lea RB, [BASE+RB*8]
1120 | lea RC, [BASE+RD*8]
1129 | lea RC, [BASE+RC*8]
1131 | lea RB, [BASE+RB*8]
1133 | lea RA, [BASE+RA*8]
1140 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1142 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1144 | movzx CARG5d, PC_OP
1146 | mov CARG4d, RC // Caveat: CARG4d == RA.
1147 | mov L:CARG1d, SAVE_L
1148 | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
1150 | mov L:RB, L:CARG1d
1159 | mov L:RB->base, BASE
1162 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1163 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1164 | mov BASE, L:RB->base
1168 | // Call metamethod for binary op.
1170 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1173 | mov [RA-12], PC // [cont|PC]
1174 | lea PC, [RC+FRAME_CONT]
1175 | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
1176 | jmp ->vm_call_dispatch
1180 | mov L:RB->base, BASE
1181 | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
1182 | mov L:FCARG1, L:RB
1184 | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
1185 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1186 | mov BASE, L:RB->base
1187 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1189 | jne ->vmeta_binop // Binop call for compatibility.
1191 | mov TAB:FCARG1, [BASE+RD*8]
1194 | jmp ->vmeta_binop // Binop call for compatibility.
1197 |//-- Call metamethod ----------------------------------------------------
1200 | lea RA, [BASE+RA*8+8]
1201 |->vmeta_call: // Resolve and call __call metamethod.
1202 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1203 | mov TMP2, RA // Save RA, RC for us.
1204 | mov TMP1, NARGS:RD
1208 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1210 | lea CARG3d, [RA+NARGS:RD*8]
1211 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
1213 | lea RC, [RA+NARGS:RD*8]
1218 | mov L:RB->base, BASE // This is the callers base!
1221 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1222 | mov BASE, L:RB->base
1224 | mov NARGS:RD, TMP1
1225 | mov LFUNC:RB, [RA-8]
1227 | // This is fragile. L->base must not move, KBASE must always be defined.
1228 | cmp KBASE, BASE // Continue with CALLT if flag set.
1231 | ins_call // Otherwise call resolved metamethod.
1233 |//-- Argument coercion for 'for' statement ------------------------------
1237 | mov L:RB->base, BASE
1238 | mov FCARG2, RA // Caveat: FCARG2 == BASE
1239 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
1241 | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
1242 | mov BASE, L:RB->base
1248 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1250 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
1253 |//-----------------------------------------------------------------------
1254 |//-- Fast functions -----------------------------------------------------
1255 |//-----------------------------------------------------------------------
1257 |.macro .ffunc, name
1261 |.macro .ffunc_1, name
1263 | cmp NARGS:RD, 1+1; jb ->fff_fallback
1266 |.macro .ffunc_2, name
1268 | cmp NARGS:RD, 2+1; jb ->fff_fallback
1271 |.macro .ffunc_n, name
1273 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1277 |.macro .ffunc_n, name, op
1279 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1284 |.macro .ffunc_nsse, name, op
1286 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1287 | op xmm0, qword [BASE]
1290 |.macro .ffunc_nsse, name
1291 | .ffunc_nsse name, movsd
1294 |.macro .ffunc_nn, name
1296 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1297 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1299 | fld qword [BASE+8]
1302 |.macro .ffunc_nnsse, name
1304 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1305 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1306 | movsd xmm0, qword [BASE]
1307 | movsd xmm1, qword [BASE+8]
1310 |.macro .ffunc_nnr, name
1312 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1313 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1314 | fld qword [BASE+8]
1318 |// Inlined GC threshold check. Caveat: uses label 1.
1320 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1321 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1327 |//-- Base library: checks -----------------------------------------------
1331 | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
1370 | mov CFUNC:RB, [BASE-8]
1371 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1373 | mov dword [BASE-4], LJ_TSTR
1374 | mov [BASE-8], STR:RC
1378 | mov RC, ~LJ_TLIGHTUD
1382 |//-- Base library: getters and setters ---------------------------------
1384 |.ffunc_1 getmetatable
1387 | cmp RB, LJ_TTAB; jne >6
1388 |1: // Field metatable must be at same offset for GCtab and GCudata!
1389 | mov TAB:RB, [BASE]
1390 | mov TAB:RB, TAB:RB->metatable
1392 | test TAB:RB, TAB:RB
1393 | mov dword [BASE-4], LJ_TNIL
1395 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1396 | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
1397 | mov [BASE-8], TAB:RB
1398 | mov RA, TAB:RB->hmask
1399 | and RA, STR:RC->hash
1401 | add NODE:RA, TAB:RB->node
1402 |3: // Rearranged logic, because we expect _not_ to find the key.
1403 | cmp dword NODE:RA->key.it, LJ_TSTR
1405 | cmp dword NODE:RA->key.gcr, STR:RC
1408 | mov NODE:RA, NODE:RA->next
1409 | test NODE:RA, NODE:RA
1411 | jmp ->fff_res1 // Not found, keep default result.
1414 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1416 | mov [BASE-4], RB // Return value of mt.__metatable.
1421 | cmp RB, LJ_TUDATA; je <1
1423 | cmp RB, LJ_TNUMX; ja >8
1424 | cmp RB, LJ_TISNUM; jbe >7
1425 | mov RB, LJ_TLIGHTUD
1429 | cmp RB, LJ_TISNUM; ja >8
1434 | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1437 |.ffunc_2 setmetatable
1438 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1439 | // Fast path: no mt for table yet and not clearing the mt.
1440 | mov TAB:RB, [BASE]
1441 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1442 | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
1443 | mov TAB:RC, [BASE+8]
1444 | mov TAB:RB->metatable, TAB:RC
1446 | mov dword [BASE-4], LJ_TTAB // Return original table.
1447 | mov [BASE-8], TAB:RB
1448 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1450 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1451 | barrierback TAB:RB, RC
1456 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1458 | mov RB, BASE // Save BASE.
1459 | lea CARG3d, [BASE+8]
1460 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1461 | mov CARG1d, SAVE_L
1463 | mov RB, BASE // Save BASE.
1464 | mov CARG2d, [BASE]
1465 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1466 | mov CARG1d, SAVE_L
1468 | mov TAB:RD, [BASE]
1472 | mov RB, BASE // Save BASE.
1476 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1477 | // cTValue * returned in eax (RD).
1478 | mov BASE, RB // Restore BASE.
1479 | // Copy table slot.
1493 |//-- Base library: conversions ------------------------------------------
1496 | // Only handles the number case inline (without a base argument).
1497 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
1498 | cmp dword [BASE+4], LJ_TISNUM
1501 | mov RB, dword [BASE]; jmp ->fff_resi
1505 | jae ->fff_fallback
1508 | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1510 | fld qword [BASE]; jmp ->fff_resn
1514 | // Only handles the string or number case inline.
1516 | cmp dword [BASE+4], LJ_TSTR; jne >3
1517 | // A __tostring method in the string base metatable is ignored.
1518 | mov STR:RD, [BASE]
1520 | mov dword [BASE-4], LJ_TSTR
1521 | mov [BASE-8], STR:RD
1523 |3: // Handle numbers inline, unless a number base metatable is present.
1524 | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
1525 | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1526 | jne ->fff_fallback
1527 | ffgccheck // Caveat: uses label 1.
1529 | mov L:RB->base, BASE // Add frame since C call can throw.
1530 | mov SAVE_PC, PC // Redundant (but a defined value).
1531 |.if X64 and not X64WIN
1532 | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
1534 | mov L:FCARG1, L:RB
1536 | call extern lj_str_fromnumber@8 // (lua_State *L, cTValue *o)
1538 | call extern lj_str_fromnum@8 // (lua_State *L, lua_Number *np)
1540 | // GCstr returned in eax (RD).
1541 | mov BASE, L:RB->base
1544 |//-- Base library: iterators -------------------------------------------
1547 | je >2 // Missing 2nd arg?
1549 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1551 | mov L:RB->base, BASE // Add frame since C call can throw.
1552 | mov L:RB->top, BASE // Dummy frame length is ok.
1555 | lea CARG3d, [BASE+8]
1556 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1559 | mov CARG2d, [BASE]
1560 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1563 | mov TAB:RD, [BASE]
1569 | mov SAVE_PC, PC // Redundant (but a defined value).
1570 | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1571 | // Flag returned in eax (RD).
1572 | mov BASE, L:RB->base
1573 | test RD, RD; jz >3 // End of traversal?
1574 | // Copy key and value to results.
1577 | mov RDa, [BASE+16]
1593 |2: // Set missing 2nd arg to nil.
1594 | mov dword [BASE+12], LJ_TNIL
1596 |3: // End of traversal: return nil.
1597 | mov dword [BASE-4], LJ_TNIL
1601 | mov TAB:RB, [BASE]
1602 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1603 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1604 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1606 | mov CFUNC:RB, [BASE-8]
1607 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1609 | mov dword [BASE-4], LJ_TFUNC
1610 | mov [BASE-8], CFUNC:RD
1611 | mov dword [BASE+12], LJ_TNIL
1615 |.ffunc_1 ipairs_aux
1616 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1617 | cmp dword [BASE+12], LJ_TISNUM
1619 | jne ->fff_fallback
1621 | jae ->fff_fallback
1625 | mov RD, dword [BASE+8]
1627 | mov dword [BASE-4], LJ_TISNUM
1628 | mov dword [BASE-8], RD
1630 | movsd xmm0, qword [BASE+8]
1631 | sseconst_1 xmm1, RBa
1634 | movsd qword [BASE-8], xmm0
1636 | fld qword [BASE+8]
1640 | fstp qword [BASE-8]
1643 | mov TAB:RB, [BASE]
1644 | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
1646 | add RD, TAB:RB->array
1648 | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
1649 | // Copy array slot.
1660 |2: // Check for empty hash part first. Otherwise call C function.
1661 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1662 | mov FCARG1, TAB:RB
1663 | mov RB, BASE // Save BASE.
1664 | mov FCARG2, RD // Caveat: FCARG2 == BASE
1665 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1666 | // cTValue * or NULL returned in eax (RD).
1675 | mov TAB:RB, [BASE]
1676 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1677 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1678 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1680 | mov CFUNC:RB, [BASE-8]
1681 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1683 | mov dword [BASE-4], LJ_TFUNC
1684 | mov [BASE-8], CFUNC:RD
1686 | mov dword [BASE+12], LJ_TISNUM
1687 | mov dword [BASE+8], 0
1690 | movsd qword [BASE+8], xmm0
1693 | fstp qword [BASE+8]
1698 |//-- Base library: catch errors ----------------------------------------
1703 | mov PC, 8+FRAME_PCALL
1705 | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1706 | shr RB, HOOK_ACTIVE_SHIFT
1708 | add PC, RB // Remember active hook before pcall.
1709 | jmp ->vm_call_dispatch
1712 | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
1713 | mov RB, [BASE+4] // Swap function and traceback.
1715 | mov dword [BASE+4], LJ_TFUNC
1716 | mov LFUNC:RB, [BASE]
1718 | mov [BASE+8], LFUNC:RB
1722 | mov PC, 16+FRAME_PCALL
1725 |//-- Coroutine library --------------------------------------------------
1727 |.macro coroutine_resume_wrap, resume
1729 |.ffunc_1 coroutine_resume
1732 |.ffunc coroutine_wrap_aux
1733 | mov CFUNC:RB, [BASE-8]
1734 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1744 | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
1746 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1747 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1749 | je >1 // Status != LUA_YIELD (i.e. 0)?
1750 | cmp RA, L:RB->base // Check for presence of initial func.
1754 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1756 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1758 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1762 | mov L:RB->base, BASE
1764 | add BASE, 8 // Keep resumed thread in stack for GC.
1766 | mov L:RB->top, BASE
1768 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1770 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1772 | sub RBa, PCa // Relative to PC.
1776 |2: // Move args to coroutine.
1799 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1800 | set_vmstate INTERP
1806 | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
1808 | mov BASE, L:RB->base
1809 | cmp eax, LUA_YIELD
1812 | mov RA, L:PC->base
1813 | mov KBASE, L:PC->top
1814 | mov L:PC->top, RA // Clear coroutine stack.
1817 | je >6 // No results?
1820 | cmp RD, L:RB->maxstack
1821 | ja >9 // Need to grow stack?
1825 |5: // Move results from coroutine.
1840 | lea RD, [PC+2] // nresults+1 = 1 + true + results.
1841 | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
1843 | lea RD, [PC+1] // nresults+1 = 1 + results.
1853 | test PC, FRAME_TYPE
1857 |8: // Coroutine returned with error (at co->top-1).
1859 | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
1862 | mov L:PC->top, RA // Clear error from coroutine stack.
1863 | // Copy error message.
1873 | mov RD, 1+2 // nresults+1 = 1 + false + error.
1878 | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
1879 | // Error function does not return.
1882 |9: // Handle stack expansion on return from yield.
1886 | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
1888 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1891 | call extern lj_state_growstack@8 // (lua_State *L, int n)
1897 | mov BASE, L:RB->base
1898 | jmp <4 // Retry the stack move.
1901 | coroutine_resume_wrap 1 // coroutine.resume
1902 | coroutine_resume_wrap 0 // coroutine.wrap
1904 |.ffunc coroutine_yield
1906 | test aword L:RB->cframe, CFRAME_RESUME
1908 | mov L:RB->base, BASE
1909 | lea RD, [BASE+NARGS:RD*8-8]
1912 | mov aword L:RB->cframe, RDa
1914 | mov byte L:RB->status, al
1915 | jmp ->vm_leave_unw
1917 |//-- Math library -------------------------------------------------------
1920 |->fff_resi: // Dummy.
1926 | fstp qword [BASE-8]
1932 | cmp dword [BASE+4], LJ_TISNUM; jne >2
1933 | mov RB, dword [BASE]
1934 | cmp RB, 0; jns ->fff_resi
1939 | mov dword [BASE-4], LJ_TISNUM
1940 | mov dword [BASE-8], RB
1944 | mov dword [BASE-4], 0x41e00000 // 2^31.
1945 | mov dword [BASE-8], 0
1950 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1954 | movsd xmm0, qword [BASE]
1955 | sseconst_abs xmm1, RDa
1959 | movsd qword [BASE-8], xmm0
1965 |->fff_resxmm0: // Dummy.
1968 | fstp qword [BASE-8]
1976 | test PC, FRAME_TYPE
1979 | cmp PC_RB, RDL // More results expected?
1981 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1983 | not RAa // Note: ~RA = -(RA+1)
1984 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
1987 |6: // Fill up results with nil.
1988 | mov dword [BASE+RD*8-12], LJ_TNIL
1992 |7: // Non-standard return case.
1993 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
1996 |.macro math_round, func
1997 | .ffunc math_ .. func
1999 | cmp dword [BASE+4], LJ_TISNUM; jne >1
2000 | mov RB, dword [BASE]; jmp ->fff_resi
2004 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2007 | movsd xmm0, qword [BASE]
2008 | call ->vm_ .. func
2011 | cmp RB, 0x80000000
2014 | ucomisd xmm0, xmm1
2021 | call ->vm_ .. func
2025 | cmp RB, 0x80000000; jne >2
2044 |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2046 |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
2048 |.ffunc_n math_log, fldln2; fyl2x; jmp ->fff_resn
2049 |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn
2050 |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn
2052 |.ffunc_n math_sin; fsin; jmp ->fff_resn
2053 |.ffunc_n math_cos; fcos; jmp ->fff_resn
2054 |.ffunc_n math_tan; fptan; fpop; jmp ->fff_resn
2057 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan
2060 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan
2062 |.ffunc_n math_atan; fld1; fpatan; jmp ->fff_resn
2064 |.macro math_extern, func
2066 | .ffunc_nsse math_ .. func
2068 | movsd FPARG1, xmm0
2071 | .ffunc_n math_ .. func
2075 | call extern lj_vm_ .. func
2090 |.ffunc_nsse math_rad
2091 | mov CFUNC:RB, [BASE-8]
2092 | mulsd xmm0, qword CFUNC:RB->upvalue[0]
2096 | mov CFUNC:RB, [BASE-8]
2097 | fmul qword CFUNC:RB->upvalue[0]
2101 |.ffunc_nn math_atan2; fpatan; jmp ->fff_resn
2102 |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
2104 |.ffunc_1 math_frexp
2106 | cmp RB, LJ_TISNUM; jae ->fff_fallback
2109 | mov [BASE-4], RB; mov [BASE-8], RC
2110 | shl RB, 1; cmp RB, 0xffe00000; jae >3
2113 | cmp RB, 0x00200000; jb >4
2115 | shr RB, 21; sub RB, RC // Extract and unbias exponent.
2119 | mov TMP1, RB; fild TMP1
2122 | and RB, 0x800fffff // Mask off exponent.
2123 | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
2127 | movsd qword [BASE], xmm0
2133 |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2135 | xorps xmm0, xmm0; jmp <2
2139 |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
2141 | movsd xmm0, qword [BASE]
2142 | sseconst_hi xmm1, RBa, 43500000 // 2^54.
2144 | movsd qword [BASE-8], xmm0
2147 | mov TMP1, 0x5a800000; fmul TMP1 // x = x*2^54
2148 | fstp qword [BASE-8]
2150 | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2153 |.ffunc_nsse math_modf
2159 | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
2165 | movsd qword [BASE-8], xmm0
2166 | movsd qword [BASE], xmm4
2172 | fstp qword [BASE-8]
2175 | mov RC, [BASE-4]; mov RB, [BASE+4]
2176 | xor RC, RB; js >3 // Need to adjust sign?
2181 | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
2185 | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
2187 | fldz; fxch; jmp <1 // Return +-Inf and +-0.
2190 |.ffunc_nnr math_fmod
2191 |1: ; fprem; fnstsw ax; sahf; jp <1
2196 |.ffunc_nnsse math_pow; call ->vm_pow; jmp ->fff_resxmm0
2198 |.ffunc_nn math_pow; call ->vm_pow; jmp ->fff_resn
2201 |.macro math_minmax, name, cmovop, fcmovop, sseop
2204 | cmp dword [BASE+4], LJ_TISNUM
2207 | mov RB, dword [BASE]
2208 |1: // Handle integers.
2209 | cmp RA, RD; jae ->fff_resi
2210 | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2211 | cmp RB, dword [BASE+RA*8-8]
2212 | cmovop RB, dword [BASE+RA*8-8]
2217 | // Convert intermediate result to number and continue below.
2228 | jae ->fff_fallback
2232 | movsd xmm0, qword [BASE]
2233 |5: // Handle numbers or integers.
2234 | cmp RA, RD; jae ->fff_resxmm0
2235 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2239 | cvtsi2sd xmm1, dword [BASE+RA*8-8]
2242 | jae ->fff_fallback
2245 | movsd xmm1, qword [BASE+RA*8-8]
2252 |5: // Handle numbers or integers.
2253 | cmp RA, RD; jae ->fff_resn
2254 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2258 | fild dword [BASE+RA*8-8]
2264 | fld qword [BASE+RA*8-8]
2266 | fucomi st1; fcmovop st1; fpop1
2272 | math_minmax math_min, cmovg, fcmovnbe, minsd
2273 | math_minmax math_max, cmovl, fcmovbe, maxsd
2276 | fpop; jmp ->fff_fallback
2279 |//-- String library -----------------------------------------------------
2281 |.ffunc_1 string_len
2282 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2283 | mov STR:RB, [BASE]
2285 | mov RB, dword STR:RB->len; jmp ->fff_resi
2287 | cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0
2289 | fild dword STR:RB->len; jmp ->fff_resn
2292 |.ffunc string_byte // Only handle the 1-arg case here.
2293 | cmp NARGS:RD, 1+1; jne ->fff_fallback
2294 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2295 | mov STR:RB, [BASE]
2297 | cmp dword STR:RB->len, 1
2298 | jb ->fff_res0 // Return no results for empty string.
2299 | movzx RB, byte STR:RB[1]
2303 | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2305 | mov TMP1, RB; fild TMP1; jmp ->fff_resn
2308 |.ffunc string_char // Only handle the 1-arg case here.
2310 | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
2311 | cmp dword [BASE+4], LJ_TISNUM
2313 | jne ->fff_fallback
2314 | mov RB, dword [BASE]
2315 | cmp RB, 255; ja ->fff_fallback
2318 | jae ->fff_fallback
2319 | cvttsd2si RB, qword [BASE]
2320 | cmp RB, 255; ja ->fff_fallback
2323 | jae ->fff_fallback
2326 | cmp TMP2, 255; ja ->fff_fallback
2333 | lea RDa, TMP2 // Points to stack. Little-endian.
2336 | mov L:RB->base, BASE
2338 | mov CARG3d, TMP3 // Zero-extended to size_t.
2339 | mov CARG2, RDa // May be 64 bit ptr to stack.
2346 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
2347 | // GCstr * returned in eax (RD).
2348 | mov BASE, L:RB->base
2350 | mov dword [BASE-4], LJ_TSTR
2351 | mov [BASE-8], STR:RD
2357 | cmp NARGS:RD, 1+2; jb ->fff_fallback
2359 | cmp dword [BASE+20], LJ_TISNUM
2361 | jne ->fff_fallback
2362 | mov RB, dword [BASE+16]
2365 | jae ->fff_fallback
2366 | cvttsd2si RB, qword [BASE+16]
2369 | jae ->fff_fallback
2370 | fld qword [BASE+16]
2374 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2375 | cmp dword [BASE+12], LJ_TISNUM
2377 | jne ->fff_fallback
2379 | jae ->fff_fallback
2381 | mov STR:RB, [BASE]
2383 | mov RB, STR:RB->len
2385 | mov RA, dword [BASE+8]
2387 | cvttsd2si RA, qword [BASE+8]
2389 | fld qword [BASE+8]
2394 | cmp RB, RC // len < end? (unsigned compare)
2397 | test RA, RA // start <= 0?
2401 | sub RC, RA // start > end?
2403 | lea RB, [STR:RB+RA+#STR-1]
2414 |5: // Negative end or overflow.
2416 | lea RC, [RC+RB+1] // end = end+(len+1)
2419 | mov RC, RB // end = len
2422 |7: // Negative start or underflow.
2424 | add RA, RB // start = start+(len+1)
2426 | jg <3 // start > 0?
2428 | mov RA, 1 // start = 1
2431 |->fff_emptystr: // Range underflow.
2432 | xor RC, RC // Zero length. Any ptr in RB is ok.
2435 |.ffunc_2 string_rep // Only handle the 1-char case inline.
2437 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2438 | cmp dword [BASE+12], LJ_TISNUM
2439 | mov STR:RB, [BASE]
2441 | jne ->fff_fallback
2442 | mov RC, dword [BASE+8]
2444 | jae ->fff_fallback
2445 | cvttsd2si RC, qword [BASE+8]
2447 | jae ->fff_fallback
2448 | fld qword [BASE+8]
2453 | jle ->fff_emptystr // Count <= 0? (or non-int)
2454 | cmp dword STR:RB->len, 1
2455 | jb ->fff_emptystr // Zero length string?
2456 | jne ->fff_fallback_2 // Fallback for > 1-char strings.
2457 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_2
2458 | movzx RA, byte STR:RB[1]
2459 | mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2465 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2470 | mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2473 |.ffunc_1 string_reverse
2475 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2476 | mov STR:RB, [BASE]
2477 | mov RC, STR:RB->len
2479 | jz ->fff_emptystr // Zero length string?
2480 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2482 | mov TMP2, PC // Need another temp register.
2488 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2490 | movzx RA, byte [RB]
2499 |.macro ffstring_case, name, lo, hi
2502 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2503 | mov STR:RB, [BASE]
2504 | mov RC, STR:RB->len
2505 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2507 | mov TMP2, PC // Need another temp register.
2513 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2515 |1: // ASCII case conversion. Yes, this is suboptimal code (do you care?).
2516 | movzx RA, byte [RB+RC]
2532 |ffstring_case string_lower, 0x41, 0x5a
2533 |ffstring_case string_upper, 0x61, 0x7a
2535 |//-- Table library ------------------------------------------------------
2537 |.ffunc_1 table_getn
2538 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
2539 | mov RB, BASE // Save BASE.
2540 | mov TAB:FCARG1, [BASE]
2541 | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t)
2542 | // Length of table returned in eax (RD).
2543 | mov BASE, RB // Restore BASE.
2545 | mov RB, RD; jmp ->fff_resi
2547 | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
2549 | mov ARG1, RD; fild ARG1; jmp ->fff_resn
2552 |//-- Bit library --------------------------------------------------------
2554 |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).
2556 |.macro .ffunc_bit, name, kind
2560 | sseconst_tobit xmm1, RBa
2562 | mov TMP1, TOBIT_BIAS
2565 | cmp dword [BASE+4], LJ_TISNUM
2568 | mov RB, dword [BASE]
2577 | jae ->fff_fallback
2580 | movsd xmm0, qword [BASE]
2582 | sseconst_tobit xmm1, RBa
2589 | mov TMP1, TOBIT_BIAS
2600 |.ffunc_bit bit_tobit, 0
2611 |.macro .ffunc_bit_op, name, ins
2612 | .ffunc_bit name, 2
2613 | mov TMP2, NARGS:RD // Save for fallback.
2614 | lea RD, [BASE+NARGS:RD*8-16]
2618 | cmp dword [RD+4], LJ_TISNUM
2621 | ins RB, dword [RD]
2625 | ja ->fff_fallback_bit_op
2627 | jae ->fff_fallback_bit_op
2630 | movsd xmm0, qword [RD]
2644 |.ffunc_bit_op bit_band, and
2645 |.ffunc_bit_op bit_bor, or
2646 |.ffunc_bit_op bit_bxor, xor
2648 |.ffunc_bit bit_bswap, 1
2652 |.ffunc_bit bit_bnot, 1
2667 |->fff_fallback_bit_op:
2668 | mov NARGS:RD, TMP2 // Restore for fallback
2669 | jmp ->fff_fallback
2671 |.macro .ffunc_bit_sh, name, ins
2673 | .ffunc_bit name, 1
2674 | // Note: no inline conversion from number for 2nd argument!
2675 | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2676 | mov RA, dword [BASE+8]
2679 | sseconst_tobit xmm2, RBa
2686 | mov TMP1, TOBIT_BIAS
2694 | ins RB, cl // Assumes RA is ecx.
2698 |.ffunc_bit_sh bit_lshift, shl
2699 |.ffunc_bit_sh bit_rshift, shr
2700 |.ffunc_bit_sh bit_arshift, sar
2701 |.ffunc_bit_sh bit_rol, rol
2702 |.ffunc_bit_sh bit_ror, ror
2704 |//-----------------------------------------------------------------------
2707 | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
2708 | jmp ->fff_fallback
2710 | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
2711 |->fff_fallback: // Call fast function fallback handler.
2712 | // BASE = new base, RD = nargs+1
2714 | mov PC, [BASE-4] // Fallback may overwrite PC.
2715 | mov SAVE_PC, PC // Redundant (but a defined value).
2716 | mov L:RB->base, BASE
2717 | lea RD, [BASE+NARGS:RD*8-8]
2718 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2720 | mov CFUNC:RD, [BASE-8]
2721 | cmp RA, L:RB->maxstack
2722 | ja >5 // Need to grow stack.
2728 | call aword CFUNC:RD->f // (lua_State *L)
2729 | mov BASE, L:RB->base
2730 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2731 | test RD, RD; jg ->fff_res // Returned nresults+1?
2737 | lea NARGS:RD, [RA+1]
2738 | mov LFUNC:RB, [BASE-8]
2739 | jne ->vm_call_tail // Returned -1?
2740 | ins_callt // Returned 0: retry fast path.
2742 |// Reconstruct previous base for vmeta_call during tailcall.
2745 | test PC, FRAME_TYPE
2748 | not RBa // Note: ~RB = -(RB+1)
2749 | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
2750 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2755 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2757 |5: // Grow stack for fallback handler.
2758 | mov FCARG2, LUA_MINSTACK
2760 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2761 | mov BASE, L:RB->base
2762 | xor RD, RD // Simulate a return 0.
2763 | jmp <1 // Dumb retry (goes through ff first).
2765 |->fff_gcstep: // Call GC step function.
2766 | // BASE = new base, RD = nargs+1
2767 | pop RBa // Must keep stack at same level.
2768 | mov TMPa, RBa // Save return address
2770 | mov SAVE_PC, PC // Redundant (but a defined value).
2771 | mov L:RB->base, BASE
2772 | lea RD, [BASE+NARGS:RD*8-8]
2775 | call extern lj_gc_step@4 // (lua_State *L)
2776 | mov BASE, L:RB->base
2782 | push RBa // Restore return address.
2785 |//-----------------------------------------------------------------------
2786 |//-- Special dispatch targets -------------------------------------------
2787 |//-----------------------------------------------------------------------
2789 |->vm_record: // Dispatch target for recording phase.
2791 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2792 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2794 | // Decrement the hookcount for consistency, but always do the call.
2795 | test RDL, HOOK_ACTIVE
2797 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2799 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2803 |->vm_rethook: // Dispatch target for return hooks.
2804 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2805 | test RDL, HOOK_ACTIVE // Hook already active?
2809 |->vm_inshook: // Dispatch target for instr/line hooks.
2810 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2811 | test RDL, HOOK_ACTIVE // Hook already active?
2814 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2816 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2818 | test RDL, LUA_MASKLINE
2822 | mov L:RB->base, BASE
2823 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2825 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2826 | call extern lj_dispatch_ins@8 // (lua_State *L, BCIns *pc)
2828 | mov BASE, L:RB->base
2835 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2837 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
2840 |->cont_hook: // Continue from hook yield.
2843 | mov MULTRES, RA // Restore MULTRES for *M ins.
2846 |->vm_hotloop: // Hot loop counter underflow.
2848 | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
2849 | mov RB, LFUNC:RB->pc
2850 | movzx RD, byte [RB+PC2PROTO(framesize)]
2851 | lea RD, [BASE+RD*8]
2853 | mov L:RB->base, BASE
2856 | lea FCARG1, [DISPATCH+GG_DISP2J]
2857 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2859 | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
2863 |->vm_callhook: // Dispatch target for call hooks.
2869 |->vm_hotcall: // Hot call counter underflow.
2872 | or PC, 1 // Marker for hot call.
2875 | lea RD, [BASE+NARGS:RD*8-8]
2877 | mov L:RB->base, BASE
2881 | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
2882 | // ASMFunction returned in eax/rax (RDa).
2883 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2887 | mov BASE, L:RB->base
2897 |//-----------------------------------------------------------------------
2898 |//-- Trace exit handler -------------------------------------------------
2899 |//-----------------------------------------------------------------------
2901 |// Called from an exit stub with the exit number on the stack.
2902 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2906 | push r13; push r12
2907 | push r11; push r10; push r9; push r8
2908 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2909 | push rbx; push rdx; push rcx; push rax
2910 | movzx RC, byte [rbp-8] // Reconstruct exit number.
2911 | mov RCH, byte [rbp-16]
2912 | mov [rbp-8], r15; mov [rbp-16], r14
2914 | push ebp; lea ebp, [esp+12]; push ebp
2915 | push ebx; push edx; push ecx; push eax
2916 | movzx RC, byte [ebp-4] // Reconstruct exit number.
2917 | mov RCH, byte [ebp-8]
2918 | mov [ebp-4], edi; mov [ebp-8], esi
2920 | // Caveat: DISPATCH is ebx.
2921 | mov DISPATCH, [ebp]
2922 | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2924 | mov [DISPATCH+DISPATCH_J(exitno)], RC
2925 | mov [DISPATCH+DISPATCH_J(parent)], RA
2928 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2930 | sub rsp, 16*8 // Room for SSE regs.
2933 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2934 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2935 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2936 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2937 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2938 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2939 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2940 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2942 | sub esp, 8*8+16 // Room for SSE regs + args.
2943 | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2944 | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2945 | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2946 | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2948 | // Caveat: RB is ebp.
2949 | mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)]
2950 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2951 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2952 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
2953 | mov L:RB->base, BASE
2955 | lea CARG2, [rsp+4*8]
2959 | lea FCARG2, [esp+16]
2961 | lea FCARG1, [DISPATCH+GG_DISP2J]
2962 | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
2963 | // MULTRES or negated error code returned in eax (RD).
2964 | mov RAa, L:RB->cframe
2965 | and RAa, CFRAME_RAWMASK
2967 | // Reposition stack later.
2969 | mov rsp, RAa // Reposition stack to C frame.
2971 | mov esp, RAa // Reposition stack to C frame.
2973 | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
2974 | mov BASE, L:RB->base
2975 | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
2981 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2984 | // Restore additional callee-save registers only used in compiled code.
2986 | lea RAa, [rsp+9*16+4*8]
2988 | movdqa xmm15, [RAa-9*16]
2989 | movdqa xmm14, [RAa-8*16]
2990 | movdqa xmm13, [RAa-7*16]
2991 | movdqa xmm12, [RAa-6*16]
2992 | movdqa xmm11, [RAa-5*16]
2993 | movdqa xmm10, [RAa-4*16]
2994 | movdqa xmm9, [RAa-3*16]
2995 | movdqa xmm8, [RAa-2*16]
2996 | movdqa xmm7, [RAa-1*16]
2997 | mov rsp, RAa // Reposition stack to C frame.
2998 | movdqa xmm6, [RAa]
3002 | add rsp, 16 // Reposition stack to C frame.
3008 | test RD, RD; js >3 // Check for error from exit.
3010 | mov LFUNC:KBASE, [BASE-8]
3011 | mov KBASE, LFUNC:KBASE->pc
3012 | mov KBASE, [KBASE+PC2PROTO(k)]
3013 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
3014 | set_vmstate INTERP
3015 | // Modified copy of ins_next which handles function header dispatch, too.
3021 | cmp OP, BC_FUNCF // Function header?
3023 | mov RC, MULTRES // RC/RD holds nres+1.
3026 | jmp aword [DISPATCH+OP*8]
3028 | jmp aword [DISPATCH+OP*4]
3031 |3: // Rethrow error from the right C frame.
3035 | call extern lj_err_throw@8 // (lua_State *L, int errcode)
3038 |//-----------------------------------------------------------------------
3039 |//-- Math helper functions ----------------------------------------------
3040 |//-----------------------------------------------------------------------
3042 |// FP value rounding. Called by math.floor/math.ceil fast functions
3043 |// and from JIT code.
3045 |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified.
3046 |.macro vm_round_x87, mode1, mode2
3047 | fnstcw word [esp+4] // Caveat: overwrites ARG1 and ARG2.
3051 |.if mode2 ~= 0xffff
3055 | fldcw word [esp+6]
3057 | fldcw word [esp+4]
3062 |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
3063 |.macro vm_round_sse, mode
3064 | sseconst_abs xmm2, RDa
3065 | sseconst_2p52 xmm3, RDa
3067 | andpd xmm1, xmm2 // |x|
3068 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
3070 | andnpd xmm2, xmm0 // Isolate sign bit.
3071 |.if mode == 2 // trunc(x)?
3073 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3075 | sseconst_1 xmm3, RDa
3076 | cmpsd xmm0, xmm1, 1 // |x| < result?
3078 | subsd xmm1, xmm0 // If yes, subtract -1.
3079 | orpd xmm1, xmm2 // Merge sign bit back in.
3081 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3083 | orpd xmm1, xmm2 // Merge sign bit back in.
3084 | .if mode == 1 // ceil(x)?
3085 | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0.
3086 | cmpsd xmm0, xmm1, 6 // x > result?
3087 | .else // floor(x)?
3088 | sseconst_1 xmm2, RDa
3089 | cmpsd xmm0, xmm1, 1 // x < result?
3092 | subsd xmm1, xmm0 // If yes, subtract +-1.
3099 |.macro vm_round, name, ssemode, mode1, mode2
3102 | vm_round_x87 mode1, mode2
3105 | vm_round_sse ssemode
3108 | vm_round vm_floor, 0, 0x0400, 0xf7ff
3109 | vm_round vm_ceil, 1, 0x0800, 0xfbff
3110 | vm_round vm_trunc, 2, 0x0c00, 0xffff
3112 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
3115 |// Args in xmm0/xmm1, return value in xmm0.
3116 |// Caveat: xmm0-xmm5 and RC (eax) modified!
3119 | sseconst_abs xmm2, RDa
3120 | sseconst_2p52 xmm3, RDa
3122 | andpd xmm4, xmm2 // |x/y|
3123 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
3125 | andnpd xmm2, xmm0 // Isolate sign bit.
3126 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
3128 | orpd xmm4, xmm2 // Merge sign bit back in.
3129 | sseconst_1 xmm2, RDa
3130 | cmpsd xmm0, xmm4, 1 // x/y < result?
3132 | subsd xmm4, xmm0 // If yes, subtract 1.0.
3143 |// Args/ret on x87 stack (y on top). No xmm registers modified.
3144 |// Caveat: needs 3 slots on x87 stack! RC (eax) modified!
3147 | fnstcw word [esp+4]
3152 | fldcw word [esp+6]
3154 | fldcw word [esp+4]
3160 |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
3161 |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
3162 |// Caveat: needs 3 slots on x87 stack!
3164 | fldl2e; fmulp st1 // e^x ==> 2^(x*log2(e))
3167 | .define expscratch, dword [rsp+8] // Use scratch area.
3169 | .define expscratch, dword [rsp-8] // Use red zone.
3171 | .define expscratch, dword [esp+4] // Needs 4 byte scratch area.
3173 | fst expscratch // Caveat: overwrites ARG1.
3174 | cmp expscratch, 0x7f800000; je >1 // Special case: e^+Inf = +Inf
3175 | cmp expscratch, 0xff800000; je >2 // Special case: e^-Inf = 0
3176 |->vm_exp2raw: // Entry point for vm_pow. Without +-Inf check.
3177 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3178 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3184 |// Generic power function x^y. Called by BC_POW, math.pow fast function,
3186 |// Args/ret on x87 stack (y on top). RC (eax) modified.
3187 |// Caveat: needs 3 slots on x87 stack!
3190 | fist dword [esp+4] // Store/reload int before comparison.
3191 | fild dword [esp+4] // Integral exponent used in vm_powi.
3193 | jnz >8 // Branch for FP exponents.
3194 | jp >9 // Branch for NaN exponent.
3195 | fpop // Pop y and fallthrough to vm_powi.
3197 |// FP/int power function x^i. Arg1/ret on x87 stack.
3198 |// Arg2 (int) on C stack. RC (eax) modified.
3199 |// Caveat: needs 2 slots on x87 stack!
3201 | cmp eax, 1; jle >6 // i<=1?
3202 | // Now 1 < (unsigned)i <= 0x80000000.
3203 |1: // Handle leading zeros.
3204 | test eax, 1; jnz >2
3211 |3: // Handle trailing bits.
3222 | je <5 // x^1 ==> x
3226 | cmp eax, 1; je <5 // x^-1 ==> 1/x
3227 | jmp <1 // x^-i ==> (1/x)^i
3229 | fpop; fld1 // x^0 ==> 1
3232 |8: // FP/FP power function x^y.
3236 | mov eax, [esp+4]; shl eax, 1
3237 | cmp eax, 0xff000000; je >2 // x^+-Inf?
3238 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3239 | cmp eax, 0xff000000; je >4 // +-Inf^y?
3243 |9: // Handle x^NaN.
3246 | je >1 // 1^NaN ==> 1
3247 | fxch // x^NaN ==> NaN
3252 |2: // Handle x^+-Inf.
3256 | je >3 // +-1^+-Inf ==> 1
3257 | fpop; fabs; fldz; mov eax, 0; setc al
3258 | ror eax, 1; xor eax, [esp+4]; jns >3 // |x|<>1, x^+-Inf ==> +Inf/0
3264 |4: // Handle +-0^y or +-Inf^y.
3265 | cmp dword [esp+4], 0; jge <3 // y >= 0, x^y ==> |x|
3267 | test eax, eax; jz >5 // y < 0, +-0^y ==> +Inf
3268 | fldz // y < 0, +-Inf^y ==> 0
3271 | mov dword [esp+4], 0x7f800000 // Return +Inf.
3276 |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified.
3277 |// Needs 16 byte scratch area for x86. Also called from JIT code.
3279 | cvtsd2si eax, xmm1
3280 | cvtsi2sd xmm2, eax
3281 | ucomisd xmm1, xmm2
3282 | jnz >8 // Branch for FP exponents.
3283 | jp >9 // Branch for NaN exponent.
3284 | // Fallthrough to vm_powi_sse.
3286 |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
3288 | cmp eax, 1; jle >6 // i<=1?
3289 | // Now 1 < (unsigned)i <= 0x80000000.
3290 |1: // Handle leading zeros.
3291 | test eax, 1; jnz >2
3298 |3: // Handle trailing bits.
3309 | je <5 // x^1 ==> x
3310 | jb >7 // x^0 ==> 1
3313 | sseconst_1 xmm1, RDa
3318 | sseconst_1 xmm0, RDa
3321 |8: // FP/FP power function x^y.
3323 | movd rax, xmm1; shl rax, 1
3324 | rol rax, 12; cmp rax, 0xffe; je >2 // x^+-Inf?
3325 | movd rax, xmm0; shl rax, 1; je >4 // +-0^y?
3326 | rol rax, 12; cmp rax, 0xffe; je >5 // +-Inf^y?
3328 | movsd qword [rsp+16], xmm1 // Use scratch area.
3329 | movsd qword [rsp+8], xmm0
3330 | fld qword [rsp+16]
3333 | movsd qword [rsp-16], xmm1 // Use red zone.
3334 | movsd qword [rsp-8], xmm0
3335 | fld qword [rsp-16]
3339 | movsd qword [esp+12], xmm1 // Needs 16 byte scratch area.
3340 | movsd qword [esp+4], xmm0
3341 | cmp dword [esp+12], 0; jne >1
3342 | mov eax, [esp+16]; shl eax, 1
3343 | cmp eax, 0xffe00000; je >2 // x^+-Inf?
3345 | cmp dword [esp+4], 0; jne >1
3346 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3347 | cmp eax, 0xffe00000; je >5 // +-Inf^y?
3349 | fld qword [esp+12]
3352 | fyl2x // y*log2(x)
3353 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3354 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3356 | fstp qword [rsp+8] // Use scratch area.
3357 | movsd xmm0, qword [rsp+8]
3359 | fstp qword [rsp-8] // Use red zone.
3360 | movsd xmm0, qword [rsp-8]
3362 | fstp qword [esp+4] // Needs 8 byte scratch area.
3363 | movsd xmm0, qword [esp+4]
3367 |9: // Handle x^NaN.
3368 | sseconst_1 xmm2, RDa
3369 | ucomisd xmm0, xmm2; je >1 // 1^NaN ==> 1
3370 | movaps xmm0, xmm1 // x^NaN ==> NaN
3374 |2: // Handle x^+-Inf.
3375 | sseconst_abs xmm2, RDa
3376 | andpd xmm0, xmm2 // |x|
3377 | sseconst_1 xmm2, RDa
3378 | ucomisd xmm0, xmm2; je <1 // +-1^+-Inf ==> 1
3379 | movmskpd eax, xmm1
3381 | mov ah, al; setc al; xor al, ah; jne <1 // |x|<>1, x^+-Inf ==> +Inf/0
3383 | sseconst_hi xmm0, RDa, 7ff00000 // +Inf
3386 |4: // Handle +-0^y.
3387 | movmskpd eax, xmm1; test eax, eax; jnz <3 // y < 0, +-0^y ==> +Inf
3388 | xorps xmm0, xmm0 // y >= 0, +-0^y ==> 0
3391 |5: // Handle +-Inf^y.
3392 | movmskpd eax, xmm1; test eax, eax; jz <3 // y >= 0, +-Inf^y ==> +Inf
3393 | xorps xmm0, xmm0 // y < 0, +-Inf^y ==> 0
3396 |// Callable from C: double lj_vm_foldfpm(double x, int fpm)
3397 |// Computes fpm(x) for extended math functions. ORDER FPM.
3402 | .define fpmop, CARG2d
3404 | .define fpmop, CARG1d
3406 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3407 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3408 | sqrtsd xmm0, xmm0; ret
3411 | movsd qword [rsp+8], xmm0 // Use scratch area.
3414 | movsd qword [rsp-8], xmm0 // Use red zone.
3417 | cmp fpmop, 5; ja >2
3418 | .if X64WIN; pop rax; .endif
3421 | .if X64WIN; push rax; .endif
3424 | call ->vm_exp2_x87
3425 | .if X64WIN; push rax; .endif
3427 |2: ; cmp fpmop, 7; je >1; ja >2
3428 | fldln2; fxch; fyl2x; jmp >7
3429 |1: ; fld1; fxch; fyl2x; jmp >7
3430 |2: ; cmp fpmop, 9; je >1; ja >2
3431 | fldlg2; fxch; fyl2x; jmp >7
3433 |2: ; cmp fpmop, 11; je >1; ja >9
3438 | fstp qword [rsp+8] // Use scratch area.
3439 | movsd xmm0, qword [rsp+8]
3441 | fstp qword [rsp-8] // Use red zone.
3442 | movsd xmm0, qword [rsp-8]
3445 |.else // x86 calling convention.
3446 | .define fpmop, eax
3448 | mov fpmop, [esp+12]
3449 | movsd xmm0, qword [esp+4]
3450 | cmp fpmop, 1; je >1; ja >2
3451 | call ->vm_floor; jmp >7
3452 |1: ; call ->vm_ceil; jmp >7
3453 |2: ; cmp fpmop, 3; je >1; ja >2
3454 | call ->vm_trunc; jmp >7
3458 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3461 |2: ; fld qword [esp+4]
3462 | cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3463 |2: ; cmp fpmop, 7; je >1; ja >2
3464 | fldln2; fxch; fyl2x; ret
3465 |1: ; fld1; fxch; fyl2x; ret
3466 |2: ; cmp fpmop, 9; je >1; ja >2
3467 | fldlg2; fxch; fyl2x; ret
3469 |2: ; cmp fpmop, 11; je >1; ja >9
3471 |1: ; fptan; fpop; ret
3473 | mov fpmop, [esp+12]
3475 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3476 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3478 |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3479 | cmp fpmop, 7; je >1; ja >2
3480 | fldln2; fxch; fyl2x; ret
3481 |1: ; fld1; fxch; fyl2x; ret
3482 |2: ; cmp fpmop, 9; je >1; ja >2
3483 | fldlg2; fxch; fyl2x; ret
3485 |2: ; cmp fpmop, 11; je >1; ja >9
3487 |1: ; fptan; fpop; ret
3490 |9: ; int3 // Bad fpm.
3493 |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
3494 |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
3495 |// and basic math functions. ORDER ARITH
3500 | .define foldop, CARG3d
3502 | .define foldop, CARG1d
3504 | cmp foldop, 1; je >1; ja >2
3505 | addsd xmm0, xmm1; ret
3506 |1: ; subsd xmm0, xmm1; ret
3507 |2: ; cmp foldop, 3; je >1; ja >2
3508 | mulsd xmm0, xmm1; ret
3509 |1: ; divsd xmm0, xmm1; ret
3510 |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow
3511 | cmp foldop, 7; je >1; ja >2
3512 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret
3513 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret
3514 |2: ; cmp foldop, 9; ja >2
3516 | movsd qword [rsp+8], xmm0 // Use scratch area.
3517 | movsd qword [rsp+16], xmm1
3519 | fld qword [rsp+16]
3521 | movsd qword [rsp-8], xmm0 // Use red zone.
3522 | movsd qword [rsp-16], xmm1
3524 | fld qword [rsp-16]
3530 | fstp qword [rsp+8] // Use scratch area.
3531 | movsd xmm0, qword [rsp+8]
3533 | fstp qword [rsp-8] // Use red zone.
3534 | movsd xmm0, qword [rsp-8]
3537 |1: ; fxch; fscale; fpop1; jmp <7
3538 |2: ; cmp foldop, 11; je >1; ja >9
3539 | minsd xmm0, xmm1; ret
3540 |1: ; maxsd xmm0, xmm1; ret
3541 |9: ; int3 // Bad op.
3543 |.elif SSE // x86 calling convention with SSE ops.
3545 | .define foldop, eax
3546 | mov foldop, [esp+20]
3547 | movsd xmm0, qword [esp+4]
3548 | movsd xmm1, qword [esp+12]
3549 | cmp foldop, 1; je >1; ja >2
3552 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3555 |1: ; subsd xmm0, xmm1; jmp <7
3556 |2: ; cmp foldop, 3; je >1; ja >2
3557 | mulsd xmm0, xmm1; jmp <7
3558 |1: ; divsd xmm0, xmm1; jmp <7
3561 | call ->vm_mod; jmp <7
3562 |1: ; pop edx; call ->vm_pow; push edx; jmp <7 // Writes to scratch area.
3563 |2: ; cmp foldop, 7; je >1; ja >2
3564 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7
3565 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7
3566 |2: ; cmp foldop, 9; ja >2
3567 | fld qword [esp+4] // Reload from stack
3568 | fld qword [esp+12]
3571 |1: ; fxch; fscale; fpop1; ret
3572 |2: ; cmp foldop, 11; je >1; ja >9
3573 | minsd xmm0, xmm1; jmp <7
3574 |1: ; maxsd xmm0, xmm1; jmp <7
3575 |9: ; int3 // Bad op.
3577 |.else // x86 calling convention with x87 ops.
3581 | fld qword [esp+12]
3582 | cmp eax, 1; je >1; ja >2
3584 |1: ; fsubp st1; ret
3585 |2: ; cmp eax, 3; je >1; ja >2
3587 |1: ; fdivp st1; ret
3588 |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow
3589 | cmp eax, 7; je >1; ja >2
3591 |1: ; fpop; fabs; ret
3592 |2: ; cmp eax, 9; je >1; ja >2
3594 |1: ; fxch; fscale; fpop1; ret
3595 |2: ; cmp eax, 11; je >1; ja >9
3596 | fucomi st1; fcmovnbe st1; fpop1; ret
3597 |1: ; fucomi st1; fcmovbe st1; fpop1; ret
3598 |9: ; int3 // Bad op.
3602 |//-----------------------------------------------------------------------
3603 |//-- Miscellaneous functions --------------------------------------------
3604 |//-----------------------------------------------------------------------
3606 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3610 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
3618 | .if X64WIN; pop rsi; .endif
3624 | xor edx, 0x00200000 // Toggle ID bit in flags.
3629 | xor eax, eax // Zero means no features supported.
3631 | jz >1 // No ID toggle means no CPUID support.
3632 | mov eax, [esp+4] // Argument 1 is function number.
3636 | mov edi, [esp+16] // Argument 2 is result area.
3647 |//-----------------------------------------------------------------------
3648 |//-- Assertions ---------------------------------------------------------
3649 |//-----------------------------------------------------------------------
3651 |->assert_bad_for_arg_type:
3652 #ifdef LUA_USE_ASSERT
3657 |//-----------------------------------------------------------------------
3658 |//-- FFI helper functions -----------------------------------------------
3659 |//-----------------------------------------------------------------------
3661 |// Handler for callback functions. Callback slot number in ah/al.
3664 |.type CTSTATE, CTState, PC
3666 | sub esp, 16 // Leave room for SAVE_ERRF etc.
3668 | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
3669 | lea DISPATCH, [ebp+GG_G2DISP]
3670 | mov CTSTATE, GL:ebp->ctype_state
3672 | mov CTSTATE->cb.slot, eax
3674 | mov CTSTATE->cb.gpr[0], CARG1
3675 | mov CTSTATE->cb.gpr[1], CARG2
3676 | mov CTSTATE->cb.gpr[2], CARG3
3677 | mov CTSTATE->cb.gpr[3], CARG4
3678 | movsd qword CTSTATE->cb.fpr[0], xmm0
3679 | movsd qword CTSTATE->cb.fpr[1], xmm1
3680 | movsd qword CTSTATE->cb.fpr[2], xmm2
3681 | movsd qword CTSTATE->cb.fpr[3], xmm3
3683 | lea rax, [rsp+CFRAME_SIZE+4*8]
3685 | lea rax, [rsp+CFRAME_SIZE]
3686 | mov CTSTATE->cb.gpr[4], CARG5
3687 | mov CTSTATE->cb.gpr[5], CARG6
3688 | movsd qword CTSTATE->cb.fpr[4], xmm4
3689 | movsd qword CTSTATE->cb.fpr[5], xmm5
3690 | movsd qword CTSTATE->cb.fpr[6], xmm6
3691 | movsd qword CTSTATE->cb.fpr[7], xmm7
3693 | mov CTSTATE->cb.stack, rax
3696 | lea eax, [esp+CFRAME_SIZE+16]
3697 | mov CTSTATE->cb.gpr[0], FCARG1
3698 | mov CTSTATE->cb.gpr[1], FCARG2
3699 | mov CTSTATE->cb.stack, eax
3700 | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp.
3701 | mov FCARG2, [esp+CFRAME_SIZE+8]
3702 | mov SAVE_RET, FCARG1
3703 | mov SAVE_R4, FCARG2
3706 | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
3707 | mov FCARG1, CTSTATE
3708 | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf)
3709 | // lua_State * returned in eax (RD).
3710 | set_vmstate INTERP
3711 | mov BASE, L:RD->base
3714 | mov LFUNC:RB, [BASE-8]
3720 |->cont_ffi_callback: // Return from FFI callback.
3723 | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
3724 | mov aword CTSTATE->L, L:RAa
3725 | mov L:RA->base, BASE
3727 | mov FCARG1, CTSTATE
3729 | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o)
3731 | mov rax, CTSTATE->cb.gpr[0]
3732 | movsd xmm0, qword CTSTATE->cb.fpr[0]
3733 | jmp ->vm_leave_unw
3736 | mov eax, CTSTATE->cb.gpr[0]
3737 | mov edx, CTSTATE->cb.gpr[1]
3738 | cmp dword CTSTATE->cb.gpr[2], 1
3741 | fld qword CTSTATE->cb.fpr[0].d
3744 | fld dword CTSTATE->cb.fpr[0].f
3746 | mov ecx, L:RB->top
3747 | movzx ecx, word [ecx+6] // Get stack adjustment and copy up.
3748 | mov SAVE_L, ecx // Must be one slot above SAVE_RET
3750 | pop ecx // Move return addr from SAVE_RET.
3751 | add esp, [esp] // Adjust stack.
3758 |->vm_ffi_call@4: // Call C function via FFI.
3759 | // Caveat: needs special frame unwinding, see below.
3762 | .type CCSTATE, CCallState, rbx
3763 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3765 | .type CCSTATE, CCallState, ebx
3766 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3769 | // Readjust stack.
3771 | mov eax, CCSTATE->spadj
3774 | sub esp, CCSTATE->spadj
3776 | mov CCSTATE->spadj, esp
3780 | // Copy stack slots.
3781 | movzx ecx, byte CCSTATE->nsp
3786 | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3787 | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3789 | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3790 | mov [esp+ecx*4], eax
3797 | movzx eax, byte CCSTATE->nfpr
3798 | mov CARG1, CCSTATE->gpr[0]
3799 | mov CARG2, CCSTATE->gpr[1]
3800 | mov CARG3, CCSTATE->gpr[2]
3801 | mov CARG4, CCSTATE->gpr[3]
3803 | mov CARG5, CCSTATE->gpr[4]
3804 | mov CARG6, CCSTATE->gpr[5]
3806 | test eax, eax; jz >5
3807 | movaps xmm0, CCSTATE->fpr[0]
3808 | movaps xmm1, CCSTATE->fpr[1]
3809 | movaps xmm2, CCSTATE->fpr[2]
3810 | movaps xmm3, CCSTATE->fpr[3]
3812 | cmp eax, 4; jbe >5
3813 | movaps xmm4, CCSTATE->fpr[4]
3814 | movaps xmm5, CCSTATE->fpr[5]
3815 | movaps xmm6, CCSTATE->fpr[6]
3816 | movaps xmm7, CCSTATE->fpr[7]
3820 | mov FCARG1, CCSTATE->gpr[0]
3821 | mov FCARG2, CCSTATE->gpr[1]
3824 | call aword CCSTATE->func
3827 | mov CCSTATE->gpr[0], rax
3828 | movaps CCSTATE->fpr[0], xmm0
3830 | mov CCSTATE->gpr[1], rdx
3831 | movaps CCSTATE->fpr[1], xmm1
3834 | mov CCSTATE->gpr[0], eax
3835 | mov CCSTATE->gpr[1], edx
3836 | cmp byte CCSTATE->resx87, 1
3839 | fstp qword CCSTATE->fpr[0].d[0]
3842 | fstp dword CCSTATE->fpr[0].f[0]
3845 | sub CCSTATE->spadj, esp
3850 | mov rbx, [rbp-8]; leave; ret
3852 | mov ebx, [ebp-4]; leave; ret
3855 |// Note: vm_ffi_call must be the last function in this object file!
3857 |//-----------------------------------------------------------------------
3860 /* Generate the code for a single instruction. */
3861 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3864 |// Note: aligning all instructions does not pay off.
3869 /* -- Comparison ops ---------------------------------------------------- */
3871 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3873 |.macro jmp_comp, lt, ge, le, gt, target
3887 ||default: break; /* Shut up GCC. */
3891 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3892 | // RA = src1, RD = src2, JMP with RD = target
3897 | mov RB, dword [BASE+RA*8]
3899 | cmp RB, dword [BASE+RD*8]
3900 | jmp_comp jge, jl, jg, jle, >9
3907 |7: // RA is not an integer.
3909 | // RA is a number.
3910 | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3911 | // RA is a number, RD is an integer.
3913 | cvtsi2sd xmm0, dword [BASE+RD*8]
3916 | fld qword [BASE+RA*8]
3917 | fild dword [BASE+RD*8]
3921 |8: // RA is an integer, RD is not an integer.
3923 | // RA is an integer, RD is a number.
3925 | cvtsi2sd xmm1, dword [BASE+RA*8]
3926 | movsd xmm0, qword [BASE+RD*8]
3928 | ucomisd xmm0, xmm1
3929 | jmp_comp jbe, ja, jb, jae, <9
3932 | fild dword [BASE+RA*8]
3936 | checknum RA, ->vmeta_comp
3937 | checknum RD, ->vmeta_comp
3941 | movsd xmm0, qword [BASE+RD*8]
3944 | ucomisd xmm0, qword [BASE+RA*8]
3948 | fld qword [BASE+RA*8] // Reverse order, i.e like cmp D, A.
3950 | fld qword [BASE+RD*8]
3955 | // Unordered: all of ZF CF PF set, ordered: PF clear.
3956 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
3958 | jmp_comp jbe, ja, jb, jae, <9
3961 | jmp_comp jbe, ja, jb, jae, >1
3969 case BC_ISEQV: case BC_ISNEV:
3970 vk = op == BC_ISEQV;
3971 | ins_AD // RA = src1, RD = src2, JMP with RD = target
3972 | mov RB, [BASE+RD*8+4]
3975 | cmp RB, LJ_TISNUM; jne >7
3977 | mov RB, dword [BASE+RD*8]
3978 | cmp RB, dword [BASE+RA*8]
3989 |7: // RD is not an integer.
3991 | // RD is a number.
3992 | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
3993 | // RD is a number, RA is an integer.
3995 | cvtsi2sd xmm0, dword [BASE+RA*8]
3997 | fild dword [BASE+RA*8]
4001 |8: // RD is an integer, RA is not an integer.
4003 | // RD is an integer, RA is a number.
4005 | cvtsi2sd xmm0, dword [BASE+RD*8]
4006 | ucomisd xmm0, qword [BASE+RA*8]
4008 | fild dword [BASE+RD*8]
4009 | fld qword [BASE+RA*8]
4014 | cmp RB, LJ_TISNUM; jae >5
4019 | movsd xmm0, qword [BASE+RA*8]
4021 | ucomisd xmm0, qword [BASE+RD*8]
4025 | fld qword [BASE+RA*8]
4027 | fld qword [BASE+RD*8]
4033 | jp >2 // Unordered means not equal.
4036 | jp >2 // Unordered means not equal.
4041 |1: // EQ: Branch to the target.
4044 |2: // NE: Fallthrough to next instruction.
4052 |2: // NE: Branch to the target.
4055 |1: // EQ: Fallthrough to next instruction.
4057 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
4058 op == BC_ISEQN || op == BC_ISNEN)) {
4064 if (op == BC_ISEQV || op == BC_ISNEV) {
4065 |5: // Either or both types are not numbers.
4067 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4068 | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
4070 | checktp RA, RB // Compare types.
4071 | jne <2 // Not the same type?
4073 | jae <1 // Same type and primitive type?
4075 | // Same types and not a primitive type. Compare GCobj or pvalue.
4076 | mov RA, [BASE+RA*8]
4077 | mov RD, [BASE+RD*8]
4079 | je <1 // Same GCobjs or pvalues?
4080 | cmp RB, LJ_TISTABUD
4081 | ja <2 // Different objects and not table/ud?
4083 | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
4087 | // Different tables or userdatas. Need to check __eq metamethod.
4088 | // Field metatable must be at same offset for GCtab and GCudata!
4089 | mov TAB:RB, TAB:RA->metatable
4090 | test TAB:RB, TAB:RB
4091 | jz <2 // No metatable?
4092 | test byte TAB:RB->nomm, 1<<MM_eq
4093 | jnz <2 // Or 'no __eq' flag set?
4095 | xor RB, RB // ne = 0
4097 | mov RB, 1 // ne = 1
4099 | jmp ->vmeta_equal // Handle __eq metamethod.
4104 if (LJ_DUALNUM && vk) {
4109 | jmp ->vmeta_equal_cd
4113 case BC_ISEQS: case BC_ISNES:
4114 vk = op == BC_ISEQS;
4115 | ins_AND // RA = src, RD = str const, JMP with RD = target
4116 | mov RB, [BASE+RA*8+4]
4118 | cmp RB, LJ_TSTR; jne >3
4119 | mov RA, [BASE+RA*8]
4120 | cmp RA, [KBASE+RD*4]
4128 case BC_ISEQN: case BC_ISNEN:
4129 vk = op == BC_ISEQN;
4130 | ins_AD // RA = src, RD = num const, JMP with RD = target
4131 | mov RB, [BASE+RA*8+4]
4134 | cmp RB, LJ_TISNUM; jne >7
4135 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
4136 | mov RB, dword [KBASE+RD*8]
4137 | cmp RB, dword [BASE+RA*8]
4148 |7: // RA is not an integer.
4150 | // RA is a number.
4151 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
4152 | // RA is a number, RD is an integer.
4154 | cvtsi2sd xmm0, dword [KBASE+RD*8]
4156 | fild dword [KBASE+RD*8]
4160 |8: // RA is an integer, RD is a number.
4162 | cvtsi2sd xmm0, dword [BASE+RA*8]
4163 | ucomisd xmm0, qword [KBASE+RD*8]
4165 | fild dword [BASE+RA*8]
4166 | fld qword [KBASE+RD*8]
4170 | cmp RB, LJ_TISNUM; jae >3
4174 | movsd xmm0, qword [KBASE+RD*8]
4176 | ucomisd xmm0, qword [BASE+RA*8]
4180 | fld qword [KBASE+RD*8]
4182 | fld qword [BASE+RA*8]
4187 case BC_ISEQP: case BC_ISNEP:
4188 vk = op == BC_ISEQP;
4189 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
4190 | mov RB, [BASE+RA*8+4]
4193 if (!LJ_HASFFI) goto iseqne_test;
4201 | cmp RB, LJ_TCDATA; jne <2
4202 | jmp ->vmeta_equal_cd
4205 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4213 /* -- Unary test and copy ops ------------------------------------------- */
4215 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
4216 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
4217 | mov RB, [BASE+RD*8+4]
4219 | cmp RB, LJ_TISTRUECOND
4220 if (op == BC_IST || op == BC_ISTC) {
4225 if (op == BC_ISTC || op == BC_ISFC) {
4226 | mov [BASE+RA*8+4], RB
4227 | mov RB, [BASE+RD*8]
4228 | mov [BASE+RA*8], RB
4232 |1: // Fallthrough to the next instruction.
4236 /* -- Unary ops --------------------------------------------------------- */
4239 | ins_AD // RA = dst, RD = src
4241 | mov RBa, [BASE+RD*8]
4242 | mov [BASE+RA*8], RBa
4244 | mov RB, [BASE+RD*8+4]
4245 | mov RD, [BASE+RD*8]
4246 | mov [BASE+RA*8+4], RB
4247 | mov [BASE+RA*8], RD
4252 | ins_AD // RA = dst, RD = src
4254 | checktp RD, LJ_TISTRUECOND
4256 | mov [BASE+RA*8+4], RB
4260 | ins_AD // RA = dst, RD = src
4263 | mov RB, [BASE+RD*8]
4266 | mov dword [BASE+RA*8+4], LJ_TISNUM
4267 | mov dword [BASE+RA*8], RB
4271 | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
4272 | mov dword [BASE+RA*8], 0
4277 | checknum RD, ->vmeta_unm
4280 | movsd xmm0, qword [BASE+RD*8]
4281 | sseconst_sign xmm1, RDa
4283 | movsd qword [BASE+RA*8], xmm0
4285 | fld qword [BASE+RD*8]
4287 | fstp qword [BASE+RA*8]
4296 | ins_AD // RA = dst, RD = src
4298 | mov STR:RD, [BASE+RD*8]
4300 | mov RD, dword STR:RD->len
4302 | mov dword [BASE+RA*8+4], LJ_TISNUM
4303 | mov dword [BASE+RA*8], RD
4306 | cvtsi2sd xmm0, dword STR:RD->len
4308 | movsd qword [BASE+RA*8], xmm0
4310 | fild dword STR:RD->len
4312 | fstp qword [BASE+RA*8]
4316 | checktab RD, ->vmeta_len
4317 | mov TAB:FCARG1, [BASE+RD*8]
4318 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4319 | mov TAB:RB, TAB:FCARG1->metatable
4325 | mov RB, BASE // Save BASE.
4326 | call extern lj_tab_len@4 // (GCtab *t)
4327 | // Length of table returned in eax (RD).
4336 | mov BASE, RB // Restore BASE.
4339 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4340 |9: // Check for __len.
4341 | test byte TAB:RB->nomm, 1<<MM_len
4343 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
4347 /* -- Binary ops -------------------------------------------------------- */
4349 |.macro ins_arithpre, x87ins, sseins, ssereg
4351 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4354 | checknum RB, ->vmeta_arith_vn
4356 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
4359 | movsd xmm0, qword [BASE+RB*8]
4360 | sseins ssereg, qword [KBASE+RC*8]
4362 | fld qword [BASE+RB*8]
4363 | x87ins qword [KBASE+RC*8]
4367 | checknum RB, ->vmeta_arith_nv
4369 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
4372 | movsd xmm0, qword [KBASE+RC*8]
4373 | sseins ssereg, qword [BASE+RB*8]
4375 | fld qword [KBASE+RC*8]
4376 | x87ins qword [BASE+RB*8]
4380 | checknum RB, ->vmeta_arith_vv
4381 | checknum RC, ->vmeta_arith_vv
4383 | movsd xmm0, qword [BASE+RB*8]
4384 | sseins ssereg, qword [BASE+RC*8]
4386 | fld qword [BASE+RB*8]
4387 | x87ins qword [BASE+RC*8]
4393 |.macro ins_arithdn, intins
4395 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4398 | checkint RB, ->vmeta_arith_vn
4399 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
4400 | mov RB, [BASE+RB*8]
4401 | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
4404 | checkint RB, ->vmeta_arith_nv
4405 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
4406 | mov RC, [KBASE+RC*8]
4407 | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
4410 | checkint RB, ->vmeta_arith_vv
4411 | checkint RC, ->vmeta_arith_vv
4412 | mov RB, [BASE+RB*8]
4413 | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
4416 | mov dword [BASE+RA*8+4], LJ_TISNUM
4418 | mov dword [BASE+RA*8], RC
4420 | mov dword [BASE+RA*8], RB
4425 |.macro ins_arithpost
4427 | movsd qword [BASE+RA*8], xmm0
4429 | fstp qword [BASE+RA*8]
4433 |.macro ins_arith, x87ins, sseins
4434 | ins_arithpre x87ins, sseins, xmm0
4439 |.macro ins_arith, intins, x87ins, sseins
4441 | ins_arithdn intins
4443 | ins_arith, x87ins, sseins
4447 | // RA = dst, RB = src1 or num const, RC = src2 or num const
4448 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
4449 | ins_arith add, fadd, addsd
4451 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
4452 | ins_arith sub, fsub, subsd
4454 case BC_MULVN: case BC_MULNV: case BC_MULVV:
4455 | ins_arith imul, fmul, mulsd
4457 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
4458 | ins_arith fdiv, divsd
4461 | ins_arithpre fld, movsd, xmm1
4467 case BC_MODNV: case BC_MODVV:
4468 | ins_arithpre fld, movsd, xmm1
4469 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
4472 | ins_arithpre fld, movsd, xmm1
4479 | ins_ABC // RA = dst, RB = src_start, RC = src_end
4481 | mov L:CARG1d, SAVE_L
4482 | mov L:CARG1d->base, BASE
4483 | lea CARG2d, [BASE+RC*8]
4487 | mov L:RB, L:CARG1d
4489 | lea RA, [BASE+RC*8]
4496 | mov L:RB->base, BASE
4499 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
4500 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
4501 | mov BASE, L:RB->base
4504 | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
4507 | mov RCa, [BASE+RB*8]
4508 | mov [BASE+RA*8], RCa
4510 | mov RC, [BASE+RB*8+4]
4511 | mov RB, [BASE+RB*8]
4512 | mov [BASE+RA*8+4], RC
4513 | mov [BASE+RA*8], RB
4518 /* -- Constant ops ------------------------------------------------------ */
4521 | ins_AND // RA = dst, RD = str const (~)
4522 | mov RD, [KBASE+RD*4]
4523 | mov dword [BASE+RA*8+4], LJ_TSTR
4524 | mov [BASE+RA*8], RD
4529 | ins_AND // RA = dst, RD = cdata const (~)
4530 | mov RD, [KBASE+RD*4]
4531 | mov dword [BASE+RA*8+4], LJ_TCDATA
4532 | mov [BASE+RA*8], RD
4537 | ins_AD // RA = dst, RD = signed int16 literal
4540 | mov dword [BASE+RA*8+4], LJ_TISNUM
4541 | mov dword [BASE+RA*8], RD
4543 | movsx RD, RDW // Sign-extend literal.
4545 | movsd qword [BASE+RA*8], xmm0
4547 | fild PC_RD // Refetch signed RD from instruction.
4548 | fstp qword [BASE+RA*8]
4553 | ins_AD // RA = dst, RD = num const
4555 | movsd xmm0, qword [KBASE+RD*8]
4556 | movsd qword [BASE+RA*8], xmm0
4558 | fld qword [KBASE+RD*8]
4559 | fstp qword [BASE+RA*8]
4564 | ins_AND // RA = dst, RD = primitive type (~)
4565 | mov [BASE+RA*8+4], RD
4569 | ins_AD // RA = dst_start, RD = dst_end
4570 | lea RA, [BASE+RA*8+12]
4571 | lea RD, [BASE+RD*8+4]
4573 | mov [RA-8], RB // Sets minimum 2 slots.
4582 /* -- Upvalue and function ops ------------------------------------------ */
4585 | ins_AD // RA = dst, RD = upvalue #
4586 | mov LFUNC:RB, [BASE-8]
4587 | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
4588 | mov RB, UPVAL:RB->v
4591 | mov [BASE+RA*8], RDa
4595 | mov [BASE+RA*8+4], RD
4596 | mov [BASE+RA*8], RB
4601 #define TV2MARKOFS \
4602 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
4603 | ins_AD // RA = upvalue #, RD = src
4604 | mov LFUNC:RB, [BASE-8]
4605 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4606 | cmp byte UPVAL:RB->closed, 0
4607 | mov RB, UPVAL:RB->v
4608 | mov RA, [BASE+RD*8]
4609 | mov RD, [BASE+RD*8+4]
4613 | // Check barrier for closed upvalue.
4614 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
4619 |2: // Upvalue is black. Check if new value is collectable and white.
4621 | cmp RD, LJ_TISNUM - LJ_TISGCV // tvisgcv(v)
4623 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
4625 | // Crossed a write barrier. Move the barrier forward.
4626 |.if X64 and not X64WIN
4628 | mov RB, BASE // Save BASE.
4630 | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
4632 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4633 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4634 | mov BASE, RB // Restore BASE.
4639 | ins_AND // RA = upvalue #, RD = str const (~)
4640 | mov LFUNC:RB, [BASE-8]
4641 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4642 | mov GCOBJ:RA, [KBASE+RD*4]
4643 | mov RD, UPVAL:RB->v
4644 | mov [RD], GCOBJ:RA
4645 | mov dword [RD+4], LJ_TSTR
4646 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
4651 |2: // Check if string is white and ensure upvalue is closed.
4652 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
4654 | cmp byte UPVAL:RB->closed, 0
4656 | // Crossed a write barrier. Move the barrier forward.
4657 | mov RB, BASE // Save BASE (FCARG2 == BASE).
4659 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4660 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4661 | mov BASE, RB // Restore BASE.
4665 | ins_AD // RA = upvalue #, RD = num const
4666 | mov LFUNC:RB, [BASE-8]
4668 | movsd xmm0, qword [KBASE+RD*8]
4670 | fld qword [KBASE+RD*8]
4672 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4673 | mov RA, UPVAL:RB->v
4675 | movsd qword [RA], xmm0
4682 | ins_AND // RA = upvalue #, RD = primitive type (~)
4683 | mov LFUNC:RB, [BASE-8]
4684 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4685 | mov RA, UPVAL:RB->v
4690 | ins_AD // RA = level, RD = target
4691 | branchPC RD // Do this first to free RD.
4693 | cmp dword L:RB->openupval, 0
4695 | mov L:RB->base, BASE
4696 | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
4697 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4698 | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
4699 | mov BASE, L:RB->base
4705 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
4708 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4709 | mov CARG3d, [BASE-8]
4710 | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
4713 | mov LFUNC:RA, [BASE-8]
4714 | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
4716 | mov ARG3, LFUNC:RA
4717 | mov ARG2, PROTO:RD
4719 | mov L:RB->base, BASE
4722 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4723 | call extern lj_func_newL_gc
4724 | // GCfuncL * returned in eax (RC).
4725 | mov BASE, L:RB->base
4727 | mov [BASE+RA*8], LFUNC:RC
4728 | mov dword [BASE+RA*8+4], LJ_TFUNC
4732 /* -- Table ops --------------------------------------------------------- */
4735 | ins_AD // RA = dst, RD = hbits|asize
4737 | mov L:RB->base, BASE
4738 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4739 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4757 | mov L:CARG1d, L:RB
4763 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4764 | // Table * returned in eax (RC).
4765 | mov BASE, L:RB->base
4767 | mov [BASE+RA*8], TAB:RC
4768 | mov dword [BASE+RA*8+4], LJ_TTAB
4770 |3: // Turn 0x7ff into 0x801.
4774 | mov L:FCARG1, L:RB
4775 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4780 | ins_AND // RA = dst, RD = table const (~) (holding template table)
4782 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4784 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4785 | mov L:RB->base, BASE
4788 | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
4789 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4790 | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
4791 | // Table * returned in eax (RC).
4792 | mov BASE, L:RB->base
4794 | mov [BASE+RA*8], TAB:RC
4795 | mov dword [BASE+RA*8+4], LJ_TTAB
4798 | mov L:FCARG1, L:RB
4799 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4800 | movzx RD, PC_RD // Need to reload RD.
4806 | ins_AND // RA = dst, RD = str const (~)
4807 | mov LFUNC:RB, [BASE-8]
4808 | mov TAB:RB, LFUNC:RB->env
4809 | mov STR:RC, [KBASE+RD*4]
4813 | ins_AND // RA = src, RD = str const (~)
4814 | mov LFUNC:RB, [BASE-8]
4815 | mov TAB:RB, LFUNC:RB->env
4816 | mov STR:RC, [KBASE+RD*4]
4821 | ins_ABC // RA = dst, RB = table, RC = key
4822 | checktab RB, ->vmeta_tgetv
4823 | mov TAB:RB, [BASE+RB*8]
4828 | mov RC, dword [BASE+RC*8]
4830 | // Convert number to int and back and compare.
4833 | movsd xmm0, qword [BASE+RC*8]
4836 | ucomisd xmm0, xmm1
4838 | fld qword [BASE+RC*8]
4844 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
4846 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4847 | jae ->vmeta_tgetv // Not in array part? Use fallback.
4849 | add RC, TAB:RB->array
4850 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4852 | // Get array slot.
4855 | mov [BASE+RA*8], RBa
4859 | mov [BASE+RA*8], RB
4860 | mov [BASE+RA*8+4], RC
4865 |2: // Check for __index if table value is nil.
4866 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4868 | mov TAB:RA, TAB:RB->metatable
4869 | test byte TAB:RA->nomm, 1<<MM_index
4870 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
4871 | movzx RA, PC_RA // Restore RA.
4873 | mov dword [BASE+RA*8+4], LJ_TNIL
4877 | checkstr RC, ->vmeta_tgetv
4878 | mov STR:RC, [BASE+RC*8]
4882 | ins_ABC // RA = dst, RB = table, RC = str const (~)
4884 | mov STR:RC, [KBASE+RC*4]
4885 | checktab RB, ->vmeta_tgets
4886 | mov TAB:RB, [BASE+RB*8]
4887 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4888 | mov RA, TAB:RB->hmask
4889 | and RA, STR:RC->hash
4891 | add NODE:RA, TAB:RB->node
4893 | cmp dword NODE:RA->key.it, LJ_TSTR
4895 | cmp dword NODE:RA->key.gcr, STR:RC
4897 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4898 | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4899 | je >5 // Key found, but nil value?
4901 | // Get node value.
4904 | mov [BASE+RC*8], RBa
4908 | mov [BASE+RC*8], RB
4909 | mov [BASE+RC*8+4], RA
4916 | mov dword [BASE+RC*8+4], LJ_TNIL
4919 |4: // Follow hash chain.
4920 | mov NODE:RA, NODE:RA->next
4921 | test NODE:RA, NODE:RA
4923 | // End of hash chain: key not found, nil result.
4925 |5: // Check for __index if table value is nil.
4926 | mov TAB:RA, TAB:RB->metatable
4927 | test TAB:RA, TAB:RA
4928 | jz <3 // No metatable: done.
4929 | test byte TAB:RA->nomm, 1<<MM_index
4930 | jnz <3 // 'no __index' flag set: done.
4931 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
4934 | ins_ABC // RA = dst, RB = table, RC = byte literal
4935 | checktab RB, ->vmeta_tgetb
4936 | mov TAB:RB, [BASE+RB*8]
4937 | cmp RC, TAB:RB->asize
4940 | add RC, TAB:RB->array
4941 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4943 | // Get array slot.
4946 | mov [BASE+RA*8], RBa
4950 | mov [BASE+RA*8], RB
4951 | mov [BASE+RA*8+4], RC
4956 |2: // Check for __index if table value is nil.
4957 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4959 | mov TAB:RA, TAB:RB->metatable
4960 | test byte TAB:RA->nomm, 1<<MM_index
4961 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
4962 | movzx RA, PC_RA // Restore RA.
4964 | mov dword [BASE+RA*8+4], LJ_TNIL
4969 | ins_ABC // RA = src, RB = table, RC = key
4970 | checktab RB, ->vmeta_tsetv
4971 | mov TAB:RB, [BASE+RB*8]
4976 | mov RC, dword [BASE+RC*8]
4978 | // Convert number to int and back and compare.
4981 | movsd xmm0, qword [BASE+RC*8]
4984 | ucomisd xmm0, xmm1
4986 | fld qword [BASE+RC*8]
4992 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
4994 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4997 | add RC, TAB:RB->array
4998 | cmp dword [RC+4], LJ_TNIL
4999 | je >3 // Previous value is nil?
5001 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5003 |2: // Set array slot.
5005 | mov RBa, [BASE+RA*8]
5008 | mov RB, [BASE+RA*8+4]
5009 | mov RA, [BASE+RA*8]
5015 |3: // Check for __newindex if previous value is nil.
5016 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5018 | mov TAB:RA, TAB:RB->metatable
5019 | test byte TAB:RA->nomm, 1<<MM_newindex
5020 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
5021 | movzx RA, PC_RA // Restore RA.
5025 | checkstr RC, ->vmeta_tsetv
5026 | mov STR:RC, [BASE+RC*8]
5029 |7: // Possible table write barrier for the value. Skip valiswhite check.
5030 | barrierback TAB:RB, RA
5031 | movzx RA, PC_RA // Restore RA.
5035 | ins_ABC // RA = src, RB = table, RC = str const (~)
5037 | mov STR:RC, [KBASE+RC*4]
5038 | checktab RB, ->vmeta_tsets
5039 | mov TAB:RB, [BASE+RB*8]
5040 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
5041 | mov RA, TAB:RB->hmask
5042 | and RA, STR:RC->hash
5044 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
5045 | add NODE:RA, TAB:RB->node
5047 | cmp dword NODE:RA->key.it, LJ_TSTR
5049 | cmp dword NODE:RA->key.gcr, STR:RC
5051 | // Ok, key found. Assumes: offsetof(Node, val) == 0
5052 | cmp dword [RA+4], LJ_TNIL
5053 | je >4 // Previous value is nil?
5055 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5057 |3: // Set node value.
5060 | mov RBa, [BASE+RC*8]
5063 | mov RB, [BASE+RC*8+4]
5064 | mov RC, [BASE+RC*8]
5070 |4: // Check for __newindex if previous value is nil.
5071 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5073 | mov TMP1, RA // Save RA.
5074 | mov TAB:RA, TAB:RB->metatable
5075 | test byte TAB:RA->nomm, 1<<MM_newindex
5076 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5077 | mov RA, TMP1 // Restore RA.
5080 |5: // Follow hash chain.
5081 | mov NODE:RA, NODE:RA->next
5082 | test NODE:RA, NODE:RA
5084 | // End of hash chain: key not found, add a new one.
5086 | // But check for __newindex first.
5087 | mov TAB:RA, TAB:RB->metatable
5088 | test TAB:RA, TAB:RA
5089 | jz >6 // No metatable: continue.
5090 | test byte TAB:RA->nomm, 1<<MM_newindex
5091 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5095 | mov TMP3, TAB:RB // Save TAB:RB for us.
5097 | mov L:CARG1d, SAVE_L
5098 | mov L:CARG1d->base, BASE
5100 | mov CARG2d, TAB:RB
5101 | mov L:RB, L:CARG1d
5103 | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
5108 | mov L:RB->base, BASE
5111 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
5112 | // Handles write barrier for the new key. TValue * returned in eax (RC).
5113 | mov BASE, L:RB->base
5114 | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
5116 | jmp <2 // Must check write barrier for value.
5118 |7: // Possible table write barrier for the value. Skip valiswhite check.
5119 | barrierback TAB:RB, RC // Destroys STR:RC.
5123 | ins_ABC // RA = src, RB = table, RC = byte literal
5124 | checktab RB, ->vmeta_tsetb
5125 | mov TAB:RB, [BASE+RB*8]
5126 | cmp RC, TAB:RB->asize
5129 | add RC, TAB:RB->array
5130 | cmp dword [RC+4], LJ_TNIL
5131 | je >3 // Previous value is nil?
5133 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5135 |2: // Set array slot.
5137 | mov RAa, [BASE+RA*8]
5140 | mov RB, [BASE+RA*8+4]
5141 | mov RA, [BASE+RA*8]
5147 |3: // Check for __newindex if previous value is nil.
5148 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5150 | mov TAB:RA, TAB:RB->metatable
5151 | test byte TAB:RA->nomm, 1<<MM_newindex
5152 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
5153 | movzx RA, PC_RA // Restore RA.
5156 |7: // Possible table write barrier for the value. Skip valiswhite check.
5157 | barrierback TAB:RB, RA
5158 | movzx RA, PC_RA // Restore RA.
5163 | ins_AD // RA = base (table at base-1), RD = num const (start index)
5164 | mov TMP1, KBASE // Need one more free register.
5165 | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
5167 | lea RA, [BASE+RA*8]
5168 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
5169 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5174 | jz >4 // Nothing to copy?
5175 | add RD, KBASE // Compute needed size.
5176 | cmp RD, TAB:RB->asize
5177 | ja >5 // Doesn't fit into array part?
5180 | add KBASE, TAB:RB->array
5181 |3: // Copy result slots to table.
5200 |5: // Need to resize array part.
5202 | mov L:CARG1d, SAVE_L
5203 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
5204 | mov CARG2d, TAB:RB
5206 | mov L:RB, L:CARG1d
5210 | mov L:RB->base, BASE
5215 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
5216 | mov BASE, L:RB->base
5217 | movzx RA, PC_RA // Restore RA.
5220 |7: // Possible table write barrier for any value. Skip valiswhite check.
5221 | barrierback TAB:RB, RD
5225 /* -- Calls and vararg handling ----------------------------------------- */
5227 case BC_CALL: case BC_CALLM:
5228 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
5229 if (op == BC_CALLM) {
5230 | add NARGS:RD, MULTRES
5232 | cmp dword [BASE+RA*8+4], LJ_TFUNC
5233 | mov LFUNC:RB, [BASE+RA*8]
5234 | jne ->vmeta_call_ra
5235 | lea BASE, [BASE+RA*8+8]
5240 | ins_AD // RA = base, RD = extra_nargs
5241 | add NARGS:RD, MULTRES
5242 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
5245 | ins_AD // RA = base, RD = nargs+1
5246 | lea RA, [BASE+RA*8+8]
5247 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
5248 | mov LFUNC:RB, [RA-8]
5249 | cmp dword [RA-4], LJ_TFUNC
5253 | test PC, FRAME_TYPE
5256 | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
5257 | mov MULTRES, NARGS:RD
5260 |2: // Move args down.
5276 | mov LFUNC:RB, [BASE-8]
5278 | mov NARGS:RD, MULTRES
5279 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
5284 |5: // Tailcall to a fast function.
5285 | test PC, FRAME_TYPE // Lua frame below?
5289 | lea RA, [BASE+RA*8]
5290 | mov LFUNC:KBASE, [RA-8] // Need to prepare KBASE.
5291 | mov KBASE, LFUNC:KBASE->pc
5292 | mov KBASE, [KBASE+PC2PROTO(k)]
5295 |7: // Tailcall from a vararg function.
5296 | sub PC, FRAME_VARG
5297 | test PC, FRAME_TYPEP
5298 | jnz >8 // Vararg frame below?
5299 | sub BASE, PC // Need to relocate BASE/KBASE down.
5304 | add PC, FRAME_VARG
5309 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
5310 | lea RA, [BASE+RA*8+8] // fb = base+1
5312 | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
5313 | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
5317 | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
5321 | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
5326 | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
5328 | mov [RA-8], LFUNC:RB
5330 | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
5338 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
5340 | // NYI: add hotloop, record BC_ITERN.
5342 | mov TMP1, KBASE // Need two more free registers.
5343 | mov TMP2, DISPATCH
5344 | mov TAB:RB, [BASE+RA*8-16]
5345 | mov RC, [BASE+RA*8-8] // Get index from control var.
5346 | mov DISPATCH, TAB:RB->asize
5348 | mov KBASE, TAB:RB->array
5349 |1: // Traverse array part.
5350 | cmp RC, DISPATCH; jae >5 // Index points after array part?
5351 | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
5353 | mov dword [BASE+RA*8+4], LJ_TISNUM
5354 | mov dword [BASE+RA*8], RC
5358 | fild dword [BASE+RA*8-8]
5360 | // Copy array slot to returned value.
5362 | mov RBa, [KBASE+RC*8]
5363 | mov [BASE+RA*8+8], RBa
5365 | mov RB, [KBASE+RC*8+4]
5366 | mov [BASE+RA*8+12], RB
5367 | mov RB, [KBASE+RC*8]
5368 | mov [BASE+RA*8+8], RB
5371 | // Return array index as a numeric key.
5375 | movsd qword [BASE+RA*8], xmm0
5377 | fstp qword [BASE+RA*8]
5379 | mov [BASE+RA*8-8], RC // Update control var.
5381 | movzx RD, PC_RD // Get target from ITERL.
5384 | mov DISPATCH, TMP2
5388 |4: // Skip holes in array part.
5390 |.if not (DUALNUM or SSE)
5391 | mov [BASE+RA*8-8], RC
5395 |5: // Traverse hash part.
5398 | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
5399 | imul KBASE, RC, #NODE
5400 | add NODE:KBASE, TAB:RB->node
5401 | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
5402 | lea DISPATCH, [RC+DISPATCH+1]
5403 | // Copy key and value from hash slot.
5405 | mov RBa, NODE:KBASE->key
5406 | mov RCa, NODE:KBASE->val
5407 | mov [BASE+RA*8], RBa
5408 | mov [BASE+RA*8+8], RCa
5410 | mov RB, NODE:KBASE->key.gcr
5411 | mov RC, NODE:KBASE->key.it
5412 | mov [BASE+RA*8], RB
5413 | mov [BASE+RA*8+4], RC
5414 | mov RB, NODE:KBASE->val.gcr
5415 | mov RC, NODE:KBASE->val.it
5416 | mov [BASE+RA*8+8], RB
5417 | mov [BASE+RA*8+12], RC
5419 | mov [BASE+RA*8-8], DISPATCH
5422 |7: // Skip holes in hash part.
5428 | ins_AD // RA = base, RD = target (points to ITERN)
5429 | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
5430 | mov CFUNC:RB, [BASE+RA*8-24]
5431 | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
5432 | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
5433 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
5435 | mov dword [BASE+RA*8-8], 0 // Initialize control var.
5438 |5: // Despecialize bytecode if any of the checks fail.
5441 | mov byte [PC], BC_ITERC
5446 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
5447 | mov TMP1, KBASE // Need one more free register.
5448 | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
5449 | lea RA, [BASE+RA*8]
5450 | sub KBASE, [BASE-4]
5451 | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
5453 | jz >5 // Copy all varargs?
5454 | lea RB, [RA+RB*8-8]
5455 | cmp KBASE, BASE // No vararg slots?
5457 |1: // Copy vararg slots to destination slots.
5459 | mov RCa, [KBASE-8]
5470 | cmp RA, RB // All destination slots filled?
5472 | cmp KBASE, BASE // No more vararg slots?
5474 |2: // Fill up remainder with nil.
5475 | mov dword [RA+4], LJ_TNIL
5483 |5: // Copy all varargs.
5484 | mov MULTRES, 1 // MULTRES = 0+1
5487 | jbe <3 // No vararg slots?
5491 | mov MULTRES, RB // MULTRES = #varargs+1
5494 | cmp RC, L:RB->maxstack
5495 | ja >7 // Need to grow stack?
5496 |6: // Copy all vararg slots.
5498 | mov RCa, [KBASE-8]
5509 | cmp KBASE, BASE // No more vararg slots?
5513 |7: // Grow stack for varargs.
5514 | mov L:RB->base, BASE
5517 | sub KBASE, BASE // Need delta, because BASE may change.
5518 | mov FCARG2, MULTRES
5521 | call extern lj_state_growstack@8 // (lua_State *L, int n)
5522 | mov BASE, L:RB->base
5528 /* -- Returns ----------------------------------------------------------- */
5531 | ins_AD // RA = results, RD = extra_nresults
5532 | add RD, MULTRES // MULTRES >=1, so RD >=1.
5533 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
5536 case BC_RET: case BC_RET0: case BC_RET1:
5537 | ins_AD // RA = results, RD = nresults+1
5538 if (op != BC_RET0) {
5543 | mov MULTRES, RD // Save nresults+1.
5544 | test PC, FRAME_TYPE // Check frame type marker.
5545 | jnz >7 // Not returning to a fixarg Lua func?
5549 | mov KBASE, BASE // Use KBASE for result move.
5552 |2: // Move results down.
5554 | mov RBa, [KBASE+RA]
5555 | mov [KBASE-8], RBa
5557 | mov RB, [KBASE+RA]
5559 | mov RB, [KBASE+RA+4]
5566 | mov RD, MULTRES // Note: MULTRES may be >255.
5567 | movzx RB, PC_RB // So cannot compare with RDL!
5569 | cmp RB, RD // More results expected?
5574 | mov RBa, [BASE+RA]
5577 | mov RB, [BASE+RA+4]
5585 | cmp PC_RB, RDL // More results expected?
5591 | not RAa // Note: ~RA = -(RA+1)
5592 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
5593 | mov LFUNC:KBASE, [BASE-8]
5594 | mov KBASE, LFUNC:KBASE->pc
5595 | mov KBASE, [KBASE+PC2PROTO(k)]
5598 |6: // Fill up results with nil.
5600 | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
5603 | mov dword [BASE+RD*8-12], LJ_TNIL
5608 |7: // Non-standard return case.
5609 | lea RB, [PC-FRAME_VARG]
5610 | test RB, FRAME_TYPEP
5612 | // Return from vararg function: relocate BASE down and RA up.
5614 if (op != BC_RET0) {
5620 /* -- Loops and branches ------------------------------------------------ */
5622 |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
5623 |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
5624 |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
5625 |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
5631 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5641 vk = (op == BC_IFORL || op == BC_JFORL);
5642 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
5643 | lea RA, [BASE+RA*8]
5645 | cmp FOR_TIDX, LJ_TISNUM; jne >9
5647 | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5648 | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5649 | mov RB, dword FOR_IDX
5650 | cmp dword FOR_STEP, 0; jl >5
5652 #ifdef LUA_USE_ASSERT
5653 | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5654 | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5656 | mov RB, dword FOR_STEP
5657 | test RB, RB; js >5
5658 | add RB, dword FOR_IDX; jo >1
5659 | mov dword FOR_IDX, RB
5661 | cmp RB, dword FOR_STOP
5662 | mov FOR_TEXT, LJ_TISNUM
5663 | mov dword FOR_EXT, RB
5664 if (op == BC_FORI) {
5669 } else if (op == BC_JFORI) {
5675 } else if (op == BC_IFORL) {
5688 |5: // Invert check for negative step.
5690 | add RB, dword FOR_IDX; jo <1
5691 | mov dword FOR_IDX, RB
5693 | cmp RB, dword FOR_STOP
5694 | mov FOR_TEXT, LJ_TISNUM
5695 | mov dword FOR_EXT, RB
5696 if (op == BC_FORI) {
5698 } else if (op == BC_JFORI) {
5702 } else if (op == BC_IFORL) {
5708 |9: // Fallback to FP variant.
5710 | cmp FOR_TIDX, LJ_TISNUM
5714 | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5716 #ifdef LUA_USE_ASSERT
5717 | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5718 | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5721 | mov RB, FOR_TSTEP // Load type/hiword of for step.
5723 | cmp RB, LJ_TISNUM; jae ->vmeta_for
5726 | movsd xmm0, qword FOR_IDX
5727 | movsd xmm1, qword FOR_STOP
5729 | addsd xmm0, qword FOR_STEP
5730 | movsd qword FOR_IDX, xmm0
5731 | test RB, RB; js >3
5735 | ucomisd xmm1, xmm0
5737 | movsd qword FOR_EXT, xmm0
5739 | fld qword FOR_STOP
5742 | fadd qword FOR_STEP // nidx = idx + step
5745 | test RB, RB; js >1
5750 | fxch // Swap lim/(n)idx if step non-negative.
5754 if (op == BC_FORI) {
5761 } else if (op == BC_JFORI) {
5765 } else if (op == BC_IFORL) {
5782 |3: // Invert comparison if step is negative.
5783 | ucomisd xmm0, xmm1
5792 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5800 | ins_AJ // RA = base, RD = target
5801 | lea RA, [BASE+RA*8]
5803 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
5804 if (op == BC_JITERL) {
5810 | branchPC RD // Otherwise save control var + branch.
5820 | ins_A // RA = base, RD = target (loop extent)
5821 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5822 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5826 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5830 | ins_A // RA = base, RD = target (loop extent)
5836 | ins_AD // RA = base (ignored), RD = traceno
5837 | mov RA, [DISPATCH+DISPATCH_J(trace)]
5838 | mov TRACE:RD, [RA+RD*4]
5839 | mov RDa, TRACE:RD->mcode
5841 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5842 | mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB
5843 | // Save additional callee-save registers only used in compiled code.
5851 | movdqa [RAa], xmm6
5852 | movdqa [RAa-1*16], xmm7
5853 | movdqa [RAa-2*16], xmm8
5854 | movdqa [RAa-3*16], xmm9
5855 | movdqa [RAa-4*16], xmm10
5856 | movdqa [RAa-5*16], xmm11
5857 | movdqa [RAa-6*16], xmm12
5858 | movdqa [RAa-7*16], xmm13
5859 | movdqa [RAa-8*16], xmm14
5860 | movdqa [RAa-9*16], xmm15
5871 | ins_AJ // RA = unused, RD = target
5876 /* -- Function headers -------------------------------------------------- */
5879 ** Reminder: A function may be called with func/args above L->maxstack,
5880 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5881 ** too. This means all FUNC* ops (including fast functions) must check
5882 ** for stack overflow _before_ adding more slots!
5889 case BC_FUNCV: /* NYI: compiled vararg functions. */
5890 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5898 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5899 | mov KBASE, [PC-4+PC2PROTO(k)]
5901 | lea RA, [BASE+RA*8] // Top of frame.
5902 | cmp RA, L:RB->maxstack
5903 | ja ->vm_growstack_f
5904 | movzx RA, byte [PC-4+PC2PROTO(numparams)]
5905 | cmp NARGS:RD, RA // Check for missing parameters.
5908 if (op == BC_JFUNCF) {
5915 |3: // Clear missing parameters.
5916 | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5927 | int3 // NYI: compiled vararg functions
5928 break; /* NYI: compiled vararg functions. */
5931 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5932 | lea RB, [NARGS:RD*8+FRAME_VARG]
5933 | lea RD, [BASE+NARGS:RD*8]
5934 | mov LFUNC:KBASE, [BASE-8]
5935 | mov [RD-4], RB // Store delta + FRAME_VARG.
5936 | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
5939 | cmp RA, L:RB->maxstack
5940 | ja ->vm_growstack_v // Need to grow stack.
5943 | movzx RB, byte [PC-4+PC2PROTO(numparams)]
5946 |1: // Copy fixarg slots up to new frame.
5949 | jnb >3 // Less args than parameters?
5955 | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
5959 if (op == BC_JFUNCV) {
5963 | mov KBASE, [PC-4+PC2PROTO(k)]
5967 |3: // Clear missing parameters.
5968 | mov dword [RD+4], LJ_TNIL
5977 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
5978 | mov CFUNC:RB, [BASE-8]
5979 | mov KBASEa, CFUNC:RB->f
5981 | lea RD, [BASE+NARGS:RD*8-8]
5982 | mov L:RB->base, BASE
5983 | lea RA, [RD+8*LUA_MINSTACK]
5984 | cmp RA, L:RB->maxstack
5986 if (op == BC_FUNCC) {
5988 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5995 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
6001 | ja ->vm_growstack_c // Need to grow stack.
6003 if (op == BC_FUNCC) {
6004 | call KBASEa // (lua_State *L)
6006 | // (lua_State *L, lua_CFunction f)
6007 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
6009 | set_vmstate INTERP
6010 | // nresults returned in eax (RD).
6011 | mov BASE, L:RB->base
6012 | lea RA, [BASE+RD*8]
6014 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
6015 | mov PC, [BASE-4] // Fetch PC of caller.
6019 /* ---------------------------------------------------------------------- */
6022 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
6028 static int build_backend(BuildCtx *ctx)
6031 dasm_growpc(Dst, BC__MAX);
6032 build_subroutines(ctx);
6034 for (op = 0; op < BC__MAX; op++)
6035 build_ins(ctx, (BCOp)op, op);
6039 /* Emit pseudo frame-info for all assembler functions. */
6040 static void emit_asm_debug(BuildCtx *ctx)
6042 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
6046 #define REG_SP "0x7"
6047 #define REG_RA "0x10"
6051 #define REG_SP "0x4"
6052 #define REG_RA "0x8"
6054 switch (ctx->mode) {
6056 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
6059 "\t.long .LECIE0-.LSCIE0\n"
6061 "\t.long 0xffffffff\n"
6065 "\t.sleb128 -" SZPTR "\n"
6066 "\t.byte " REG_RA "\n"
6067 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6068 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6069 "\t.align " SZPTR "\n"
6073 "\t.long .LEFDE0-.LASFDE0\n"
6075 "\t.long .Lframe0\n"
6079 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6080 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6081 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6082 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6083 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6087 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6088 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6089 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6090 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6091 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6093 "\t.align " SZPTR "\n"
6094 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
6098 "\t.long .LEFDE1-.LASFDE1\n"
6100 "\t.long .Lframe0\n"
6102 "\t.quad lj_vm_ffi_call\n"
6104 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6105 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6106 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6107 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6109 "\t.long lj_vm_ffi_call\n"
6111 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6112 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6113 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6114 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6116 "\t.align " SZPTR "\n"
6117 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
6119 #if (defined(__sun__) && defined(__svr4__)) || defined(__solaris_)
6120 fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
6122 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
6126 "\t.long .LECIE1-.LSCIE1\n"
6130 "\t.string \"zPR\"\n"
6132 "\t.sleb128 -" SZPTR "\n"
6133 "\t.byte " REG_RA "\n"
6134 "\t.uleb128 6\n" /* augmentation length */
6135 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6136 "\t.long lj_err_unwind_dwarf-.\n"
6137 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6138 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6139 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6140 "\t.align " SZPTR "\n"
6144 "\t.long .LEFDE2-.LASFDE2\n"
6146 "\t.long .LASFDE2-.Lframe1\n"
6147 "\t.long .Lbegin-.\n"
6149 "\t.uleb128 0\n" /* augmentation length */
6150 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6152 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6153 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6154 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6155 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6157 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6158 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6159 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6160 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6162 "\t.align " SZPTR "\n"
6163 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
6167 "\t.long .LECIE2-.LSCIE2\n"
6171 "\t.string \"zR\"\n"
6173 "\t.sleb128 -" SZPTR "\n"
6174 "\t.byte " REG_RA "\n"
6175 "\t.uleb128 1\n" /* augmentation length */
6176 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6177 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6178 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6179 "\t.align " SZPTR "\n"
6183 "\t.long .LEFDE3-.LASFDE3\n"
6185 "\t.long .LASFDE3-.Lframe2\n"
6186 "\t.long lj_vm_ffi_call-.\n"
6188 "\t.uleb128 0\n" /* augmentation length */
6190 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6191 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6192 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6193 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6195 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6196 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6197 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6198 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6200 "\t.align " SZPTR "\n"
6201 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
6204 /* Mental note: never let Apple design an assembler.
6205 ** Or a linker. Or a plastic case. But I digress.
6207 case BUILD_machasm: {
6212 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
6215 "\t.set L$set$x,LECIEX-LSCIEX\n"
6220 "\t.ascii \"zPR\\0\"\n"
6222 "\t.byte 128-" SZPTR "\n"
6223 "\t.byte " REG_RA "\n"
6224 "\t.byte 6\n" /* augmentation length */
6225 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
6227 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
6228 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6229 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6231 "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
6232 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6233 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
6235 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6236 "\t.align " BSZPTR "\n"
6238 for (i = 0; i < ctx->nsym; i++) {
6239 const char *name = ctx->sym[i].name;
6240 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
6241 if (size == 0) continue;
6243 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
6248 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
6249 "\t.long L$set$%d\n"
6251 "\t.long LASFDE%d-EH_frame1\n"
6254 "\t.byte 0\n" /* augmentation length */
6255 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
6257 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6258 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6259 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
6260 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
6262 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6263 "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
6264 "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
6265 "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
6267 "\t.align " BSZPTR "\n"
6269 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
6275 "\t.set L$set$y,LECIEY-LSCIEY\n"
6280 "\t.ascii \"zR\\0\"\n"
6282 "\t.byte 128-" SZPTR "\n"
6283 "\t.byte " REG_RA "\n"
6284 "\t.byte 1\n" /* augmentation length */
6286 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6287 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6289 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6290 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
6292 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6293 "\t.align " BSZPTR "\n"
6296 "_lj_vm_ffi_call.eh:\n"
6298 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
6299 "\t.long L$set$yy\n"
6301 "\t.long LASFDEY-EH_frame2\n"
6302 "\t.long _lj_vm_ffi_call-.\n"
6304 "\t.byte 0\n" /* augmentation length */
6306 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
6307 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6308 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6309 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6311 "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
6312 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6313 "\t.byte 0xd\n\t.uleb128 0x4\n" /* def_cfa_register ebp */
6314 "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
6316 "\t.align " BSZPTR "\n"
6317 "LEFDEY:\n\n", fcsize);
6321 fprintf(ctx->fp, "\t.subsections_via_symbols\n");
6324 "\t.non_lazy_symbol_pointer\n"
6325 "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
6326 ".indirect_symbol _lj_err_unwind_dwarf\n"
6331 default: /* Difficult for other modes. */