1 |// Low-level VM code for x86 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
10 |.section code_op, code_sub
12 |.actionlist build_actionlist
14 |.globalnames globnames
15 |.externnames extnames
17 |//-----------------------------------------------------------------------
19 |// Fixed register assignments for the interpreter.
20 |// This is very fragile and has many dependencies. Caveat emptor.
21 |.define BASE, edx // Not C callee-save, refetched anyway.
23 |.define KBASE, edi // Must be C callee-save.
24 |.define KBASEa, KBASE
25 |.define PC, esi // Must be C callee-save.
27 |.define DISPATCH, ebx // Must be C callee-save.
29 |.define KBASE, edi // Must be C callee-save.
31 |.define PC, esi // Must be C callee-save.
33 |.define DISPATCH, ebx // Must be C callee-save.
35 |.define KBASE, r15d // Must be C callee-save.
37 |.define PC, ebx // Must be C callee-save.
39 |.define DISPATCH, r14d // Must be C callee-save.
45 |.define RB, ebp // Must be ebp (C callee-save).
46 |.define RC, eax // Must be eax (fcomparepp and others).
67 |.define FCARG1, ecx // x86 fastcall arguments.
70 |.define CARG1, rcx // x64/WIN64 C call arguments.
78 |.define FCARG1, CARG1d // Upwards compatible to x86 fastcall.
79 |.define FCARG2, CARG2d
81 |.define CARG1, rdi // x64/POSIX C call arguments.
93 |.define FCARG1, CARG1d // Simulate x86 fastcall.
94 |.define FCARG2, CARG2d
97 |// Type definitions. Some of these are only used for documentation.
99 |.type GL, global_State
100 |.type TVALUE, TValue
104 |.type LFUNC, GCfuncL
105 |.type CFUNC, GCfuncC
106 |.type PROTO, GCproto
107 |.type UPVAL, GCupval
110 |.type TRACE, GCtrace
112 |// Stack layout while in interpreter. Must match with lj_frame.h.
113 |//-----------------------------------------------------------------------
114 |.if not X64 // x86 stack layout.
116 |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
118 | push ebp; push edi; push esi; push ebx
119 | sub esp, CFRAME_SPACE
122 | add esp, CFRAME_SPACE
123 | pop ebx; pop esi; pop edi; pop ebp
126 |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
127 |.define SAVE_NRES, aword [esp+aword*14]
128 |.define SAVE_CFRAME, aword [esp+aword*13]
129 |.define SAVE_L, aword [esp+aword*12]
130 |//----- 16 byte aligned, ^^^ arguments from C caller
131 |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
132 |.define SAVE_R4, aword [esp+aword*10]
133 |.define SAVE_R3, aword [esp+aword*9]
134 |.define SAVE_R2, aword [esp+aword*8]
135 |//----- 16 byte aligned
136 |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
137 |.define SAVE_PC, aword [esp+aword*6]
138 |.define TMP2, aword [esp+aword*5]
139 |.define TMP1, aword [esp+aword*4]
140 |//----- 16 byte aligned
141 |.define ARG4, aword [esp+aword*3]
142 |.define ARG3, aword [esp+aword*2]
143 |.define ARG2, aword [esp+aword*1]
144 |.define ARG1, aword [esp] //<-- esp while in interpreter.
145 |//----- 16 byte aligned, ^^^ arguments for C callee
147 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
148 |.define FPARG3, qword [esp+qword*1]
149 |.define FPARG1, qword [esp]
150 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
151 |.define TMPQ, qword [esp+aword*4]
155 |.define MULTRES, TMP2
157 |// Arguments for vm_call and vm_pcall.
158 |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
160 |// Arguments for vm_cpcall.
161 |.define INARG_CP_CALL, SAVE_ERRF
162 |.define INARG_CP_UD, SAVE_NRES
163 |.define INARG_CP_FUNC, SAVE_CFRAME
165 |//-----------------------------------------------------------------------
166 |.elif X64WIN // x64/Windows stack layout
168 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
170 | push rbp; push rdi; push rsi; push rbx
171 | sub rsp, CFRAME_SPACE
174 | add rsp, CFRAME_SPACE
175 | pop rbx; pop rsi; pop rdi; pop rbp
178 |.define SAVE_CFRAME, aword [rsp+aword*13]
179 |.define SAVE_PC, dword [rsp+dword*25]
180 |.define SAVE_L, dword [rsp+dword*24]
181 |.define SAVE_ERRF, dword [rsp+dword*23]
182 |.define SAVE_NRES, dword [rsp+dword*22]
183 |.define TMP2, dword [rsp+dword*21]
184 |.define TMP1, dword [rsp+dword*20]
185 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
186 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
187 |.define SAVE_R4, aword [rsp+aword*8]
188 |.define SAVE_R3, aword [rsp+aword*7]
189 |.define SAVE_R2, aword [rsp+aword*6]
190 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
191 |.define ARG5, aword [rsp+aword*4]
192 |.define CSAVE_4, aword [rsp+aword*3]
193 |.define CSAVE_3, aword [rsp+aword*2]
194 |.define CSAVE_2, aword [rsp+aword*1]
195 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
196 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
198 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
199 |.define TMPQ, qword [rsp+aword*10]
200 |.define MULTRES, TMP2
202 |.define ARG5d, dword [rsp+aword*4]
205 |//-----------------------------------------------------------------------
206 |.else // x64/POSIX stack layout
208 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
210 | push rbp; push rbx; push r15; push r14
211 | sub rsp, CFRAME_SPACE
214 | add rsp, CFRAME_SPACE
215 | pop r14; pop r15; pop rbx; pop rbp
218 |//----- 16 byte aligned,
219 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
220 |.define SAVE_R4, aword [rsp+aword*8]
221 |.define SAVE_R3, aword [rsp+aword*7]
222 |.define SAVE_R2, aword [rsp+aword*6]
223 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
224 |.define SAVE_CFRAME, aword [rsp+aword*4]
225 |.define SAVE_PC, dword [rsp+dword*7]
226 |.define SAVE_L, dword [rsp+dword*6]
227 |.define SAVE_ERRF, dword [rsp+dword*5]
228 |.define SAVE_NRES, dword [rsp+dword*4]
229 |.define TMPa, aword [rsp+aword*1]
230 |.define TMP2, dword [rsp+dword*1]
231 |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
232 |//----- 16 byte aligned
234 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
235 |.define TMPQ, qword [rsp]
236 |.define TMP3, dword [rsp+aword*1]
237 |.define MULTRES, TMP2
241 |//-----------------------------------------------------------------------
243 |// Instruction headers.
244 |.macro ins_A; .endmacro
245 |.macro ins_AD; .endmacro
246 |.macro ins_AJ; .endmacro
247 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
248 |.macro ins_AB_; movzx RB, RCH; .endmacro
249 |.macro ins_A_C; movzx RC, RCL; .endmacro
250 |.macro ins_AND; not RDa; .endmacro
252 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
260 | jmp aword [DISPATCH+OP*8]
262 | jmp aword [DISPATCH+OP*4]
266 |// Instruction footer.
268 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
269 | .define ins_next, ins_NEXT
270 | .define ins_next_, ins_NEXT
272 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
273 | // Affects only certain kinds of benchmarks (and only with -j off).
274 | // Around 10%-30% slower on Core2, a lot more slower on P4.
284 |// Call decode and dispatch.
286 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
287 | mov PC, LFUNC:RB->pc
293 | jmp aword [DISPATCH+OP*8]
295 | jmp aword [DISPATCH+OP*4]
300 | // BASE = new base, RB = LFUNC, RD = nargs+1
305 |//-----------------------------------------------------------------------
307 |// Macros to test operand types.
308 |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
309 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
310 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
311 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
312 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
314 |// These operands must be used with movzx.
315 |.define PC_OP, byte [PC-4]
316 |.define PC_RA, byte [PC-3]
317 |.define PC_RB, byte [PC-1]
318 |.define PC_RC, byte [PC-2]
319 |.define PC_RD, word [PC-2]
321 |.macro branchPC, reg
322 | lea PC, [PC+reg*4-BCBIAS_J*4]
325 |// Assumes DISPATCH is relative to GL.
326 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
327 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
329 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
331 |// Decrement hashed hotcount and trigger trace recorder if zero.
335 | and reg, HOTCOUNT_PCMASK
336 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
343 | and reg, HOTCOUNT_PCMASK
344 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
348 |// Set current VM state.
349 |.macro set_vmstate, st
350 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
353 |// Annoying x87 stuff: support for two compare variants.
354 |.macro fcomparepp // Compare and pop st0 >< st1.
360 | fnstsw ax // eax modified!
365 |.macro fdup; fld st0; .endmacro
366 |.macro fpop1; fstp st1; .endmacro
368 |// Synthesize SSE FP constants.
369 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
371 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
373 | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
377 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
379 | mov64 tmp, U64x(val,00000000); movd reg, tmp
381 | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
385 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
386 | sseconst_hi reg, tmp, 80000000
388 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
389 | sseconst_hi reg, tmp, 3ff00000
391 |.macro sseconst_m1, reg, tmp // Synthesize -1.0.
392 | sseconst_hi reg, tmp, bff00000
394 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
395 | sseconst_hi reg, tmp, 43300000
397 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
398 | sseconst_hi reg, tmp, 43380000
401 |// Move table write barrier back. Overwrites reg.
402 |.macro barrierback, tab, reg
403 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
404 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
405 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
406 | mov tab->gclist, reg
409 |//-----------------------------------------------------------------------
411 /* Generate subroutines used by opcodes and other parts of the VM. */
412 /* The .code_sub section should be last to help static branch prediction. */
413 static void build_subroutines(BuildCtx *ctx, int cmov, int sse)
417 |//-----------------------------------------------------------------------
418 |//-- Return handling ----------------------------------------------------
419 |//-----------------------------------------------------------------------
425 | // Return from pcall or xpcall fast func.
427 | sub BASE, PC // Restore caller base.
428 | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
429 | mov PC, [BASE-4] // Fetch PC of previous frame.
430 | // Prepending may overwrite the pcall frame, so do it at the end.
431 | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
434 | add RD, 1 // RD = nresults+1
436 | test PC, FRAME_TYPE
437 | jz ->BC_RET_Z // Handle regular return to Lua.
440 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
442 | test PC, FRAME_TYPE
449 | neg PC // Previous base = BASE - delta.
453 |1: // Move results down.
460 | mov RB, [BASE+RA+4]
471 | mov RA, SAVE_NRES // RA = wanted nresults+1
474 | jne >6 // More/less results wanted?
477 | mov L:RB->top, BASE
480 | mov RAa, SAVE_CFRAME // Restore previous C frame.
481 | mov L:RB->cframe, RAa
482 | xor eax, eax // Ok return status for vm_pcall.
489 | jb >7 // Less results wanted?
490 | // More results wanted. Check stack size and fill up results with nil.
491 | cmp BASE, L:RB->maxstack
493 | mov dword [BASE-4], LJ_TNIL
498 |7: // Less results wanted.
500 | jz <5 // But check for LUA_MULTRET+1.
501 | sub RA, RD // Negative result!
502 | lea BASE, [BASE+RA*8] // Correct top.
505 |8: // Corner case: need to grow stack for filling up results.
506 | // This can happen if:
507 | // - A C function grows the stack (a lot).
508 | // - The GC shrinks the stack in between.
509 | // - A return back from a lua_call() with (high) nresults adjustment.
510 | mov L:RB->top, BASE // Save current top held in BASE (yes).
511 | mov MULTRES, RD // Need to fill only remainder with nil.
514 | call extern lj_state_growstack@8 // (lua_State *L, int n)
515 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
518 |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
519 | // (void *cframe, int errcode)
521 | mov eax, CARG2d // Error return status for vm_pcall.
524 | mov eax, FCARG2 // Error return status for vm_pcall.
527 |->vm_unwind_c_eh: // Landing pad for external unwinder.
529 | mov GL:RB, L:RB->glref
530 | mov dword GL:RB->vmstate, ~LJ_VMST_C
533 |->vm_unwind_rethrow:
534 |.if X64 and not X64WIN
538 | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
541 |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
544 | and CARG1, CFRAME_RAWMASK
547 | and FCARG1, CFRAME_RAWMASK
550 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
552 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
553 | mov RD, 1+1 // Really 1+2 results, incr. later.
554 | mov BASE, L:RB->base
555 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
556 | add DISPATCH, GG_G2DISP
557 | mov PC, [BASE-4] // Fetch PC of previous frame.
558 | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
560 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
562 |//-----------------------------------------------------------------------
563 |//-- Grow stack for calls -----------------------------------------------
564 |//-----------------------------------------------------------------------
566 |->vm_growstack_c: // Grow stack for C function.
567 | mov FCARG2, LUA_MINSTACK
570 |->vm_growstack_v: // Grow stack for vararg Lua function.
574 |->vm_growstack_f: // Grow stack for fixarg Lua function.
575 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
576 | lea RD, [BASE+NARGS:RD*8-8]
578 | movzx RA, byte [PC-4+PC2PROTO(framesize)]
579 | add PC, 4 // Must point after first instruction.
580 | mov L:RB->base, BASE
585 | // RB = L, L->base = new base, L->top = top
587 | call extern lj_state_growstack@8 // (lua_State *L, int n)
588 | mov BASE, L:RB->base
590 | mov LFUNC:RB, [BASE-8]
594 | // BASE = new base, RB = LFUNC, RD = nargs+1
595 | ins_callt // Just retry the call.
597 |//-----------------------------------------------------------------------
598 |//-- Entry points into the assembler VM ---------------------------------
599 |//-----------------------------------------------------------------------
601 |->vm_resume: // Setup C frame and resume thread.
602 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
605 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
610 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
614 | lea KBASEa, [esp+CFRAME_RESUME]
615 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
616 | add DISPATCH, GG_G2DISP
617 | mov L:RB->cframe, KBASEa
618 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
619 | mov SAVE_CFRAME, RDa
624 | cmp byte L:RB->status, RDL
625 | je >3 // Initial resume (like a call).
627 | // Resume after yield (like a return).
629 | mov byte L:RB->status, RDL
630 | mov BASE, L:RB->base
634 | add RD, 1 // RD = nresults+1
635 | sub RA, BASE // RA = resultofs
638 | test PC, FRAME_TYPE
642 |->vm_pcall: // Setup protected C frame and enter VM.
643 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
647 | mov SAVE_ERRF, CARG4d
651 |->vm_call: // Setup C frame and enter VM.
652 | // (lua_State *L, TValue *base, int nres1)
656 |1: // Entry point for vm_pcall above (PC = ftype).
658 | mov SAVE_NRES, CARG3d
659 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
664 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
667 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
668 | mov SAVE_CFRAME, KBASEa
669 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
671 | mov L:RB->cframe, rsp
673 | mov L:RB->cframe, esp
676 |2: // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype).
677 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
678 | add DISPATCH, GG_G2DISP
680 |3: // Entry point for vm_resume above (RA = base, RB = L, PC = ftype).
682 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
684 | sub PC, BASE // PC = frame delta + frame type
689 | add NARGS:RD, 1 // RD = nargs+1
692 | mov LFUNC:RB, [RA-8]
693 | cmp dword [RA-4], LJ_TFUNC
694 | jne ->vmeta_call // Ensure KBASE defined and != BASE.
696 |->vm_call_dispatch_f:
699 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
701 |->vm_cpcall: // Setup protected C frame, call C.
702 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
705 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
709 | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
710 | mov RC, INARG_CP_UD // Get args before they are overwritten.
711 | mov RA, INARG_CP_FUNC
712 | mov BASE, INARG_CP_CALL
714 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
716 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
717 | sub KBASE, L:RB->top
718 | mov SAVE_ERRF, 0 // No error function.
719 | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
720 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
723 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
724 | mov SAVE_CFRAME, KBASEa
725 | mov L:RB->cframe, rsp
727 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
729 | mov ARG3, RC // Have to copy args downwards.
733 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
734 | mov SAVE_CFRAME, KBASE
735 | mov L:RB->cframe, esp
737 | call BASE // (lua_State *L, lua_CFunction func, void *ud)
739 | // TValue * (new base) or NULL returned in eax (RC).
741 | jz ->vm_leave_cp // No base? Just remove C frame.
744 | jmp <2 // Else continue with the call.
746 |//-----------------------------------------------------------------------
747 |//-- Metamethod handling ------------------------------------------------
748 |//-----------------------------------------------------------------------
750 |//-- Continuation dispatch ----------------------------------------------
753 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
757 | sub BASE, PC // Restore caller BASE.
758 | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
759 | mov RC, RA // ... in [RC]
760 | mov PC, [RB-12] // Restore PC from [cont|PC].
762 | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
765 | lea KBASEa, qword [=>0]
768 | mov RA, dword [RB-16]
772 | mov LFUNC:KBASE, [BASE-8]
773 | mov KBASE, LFUNC:KBASE->pc
774 | mov KBASE, [KBASE+PC2PROTO(k)]
775 | // BASE = base, RC = result, RB = meta base
776 | jmp RAa // Jump to continuation.
778 |1: // Tail call from C function.
784 |->cont_cat: // BASE = base, RC = result, RB = mbase
787 | lea RA, [BASE+RA*8]
794 | mov L:CARG1d, SAVE_L
795 | mov L:CARG1d->base, BASE
800 | mov L:CARG1d, SAVE_L
801 | mov L:CARG1d->base, BASE
816 |//-- Table indexing metamethods -----------------------------------------
819 | mov TMP1, RC // RC = GCstr *
821 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
824 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
825 | mov [RA], TAB:RB // RB = GCtab *
826 | mov dword [RA+4], LJ_TTAB
833 | mov TMP2, LJ_TISNUM
845 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
849 | movzx RC, PC_RC // Reload TValue *k from RC.
850 | lea RC, [BASE+RC*8]
852 | movzx RB, PC_RB // Reload TValue *t from RB.
853 | lea RB, [BASE+RB*8]
856 | mov L:CARG1d, SAVE_L
857 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
859 | mov CARG3, RCa // May be 64 bit ptr to stack.
866 | mov L:RB->base, BASE
869 | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
870 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
871 | mov BASE, L:RB->base
874 |->cont_ra: // BASE = base, RC = result
878 | mov [BASE+RA*8], RBa
882 | mov [BASE+RA*8+4], RB
883 | mov [BASE+RA*8], RC
887 |3: // Call __index metamethod.
888 | // BASE = base, L->top = new base, stack = cont/func/t/k
890 | mov [RA-12], PC // [cont|PC]
891 | lea PC, [RA+FRAME_CONT]
893 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
894 | mov NARGS:RD, 2+1 // 2 args for func(t, k).
895 | jmp ->vm_call_dispatch_f
897 |//-----------------------------------------------------------------------
900 | mov TMP1, RC // RC = GCstr *
902 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
905 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
906 | mov [RA], TAB:RB // RB = GCtab *
907 | mov dword [RA+4], LJ_TTAB
914 | mov TMP2, LJ_TISNUM
926 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
930 | movzx RC, PC_RC // Reload TValue *k from RC.
931 | lea RC, [BASE+RC*8]
933 | movzx RB, PC_RB // Reload TValue *t from RB.
934 | lea RB, [BASE+RB*8]
937 | mov L:CARG1d, SAVE_L
938 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
940 | mov CARG3, RCa // May be 64 bit ptr to stack.
947 | mov L:RB->base, BASE
950 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
951 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
952 | mov BASE, L:RB->base
955 | // NOBARRIER: lj_meta_tset ensures the table is not black.
958 | mov RBa, [BASE+RA*8]
961 | mov RB, [BASE+RA*8+4]
962 | mov RA, [BASE+RA*8]
966 |->cont_nop: // BASE = base, (RC = result)
969 |3: // Call __newindex metamethod.
970 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
972 | mov [RA-12], PC // [cont|PC]
974 | // Copy value to third argument.
976 | mov RBa, [BASE+RC*8]
979 | mov RB, [BASE+RC*8+4]
980 | mov RC, [BASE+RC*8]
984 | lea PC, [RA+FRAME_CONT]
986 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
987 | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
988 | jmp ->vm_call_dispatch_f
990 |//-- Comparison metamethods ---------------------------------------------
995 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
997 | lea CARG3d, [BASE+RD*8]
998 | lea CARG2d, [BASE+RA*8]
1000 | lea CARG2d, [BASE+RA*8]
1001 | lea CARG3d, [BASE+RD*8]
1003 | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
1004 | movzx CARG4d, PC_OP
1007 | lea RD, [BASE+RD*8]
1008 | lea RA, [BASE+RA*8]
1014 | mov L:RB->base, BASE
1017 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1018 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1020 | mov BASE, L:RB->base
1032 |->cont_condt: // BASE = base, RC = result
1034 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
1038 |->cont_condf: // BASE = base, RC = result
1039 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
1048 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1050 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1053 | mov CARG4d, RB // Caveat: CARG4d == RA.
1055 | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
1064 | mov L:RB->base, BASE
1067 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1068 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1075 | mov L:RB->base, BASE
1077 | mov FCARG2, dword [PC-4]
1079 | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
1080 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1084 |//-- Arithmetic metamethods ---------------------------------------------
1091 | lea RC, [KBASE+RC*8]
1099 | lea RC, [KBASE+RC*8]
1100 | lea RB, [BASE+RB*8]
1105 | lea RC, [BASE+RD*8]
1114 | lea RC, [BASE+RC*8]
1116 | lea RB, [BASE+RB*8]
1118 | lea RA, [BASE+RA*8]
1125 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1127 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1129 | movzx CARG5d, PC_OP
1131 | mov CARG4d, RC // Caveat: CARG4d == RA.
1132 | mov L:CARG1d, SAVE_L
1133 | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
1135 | mov L:RB, L:CARG1d
1144 | mov L:RB->base, BASE
1147 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1148 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1149 | mov BASE, L:RB->base
1153 | // Call metamethod for binary op.
1155 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1158 | mov [RA-12], PC // [cont|PC]
1159 | lea PC, [RC+FRAME_CONT]
1160 | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
1161 | jmp ->vm_call_dispatch
1165 | mov L:RB->base, BASE
1166 | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
1167 | mov L:FCARG1, L:RB
1169 | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
1170 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1171 | mov BASE, L:RB->base
1172 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1174 | jne ->vmeta_binop // Binop call for compatibility.
1176 | mov TAB:FCARG1, [BASE+RD*8]
1179 | jmp ->vmeta_binop // Binop call for compatibility.
1182 |//-- Call metamethod ----------------------------------------------------
1185 | lea RA, [BASE+RA*8+8]
1186 |->vmeta_call: // Resolve and call __call metamethod.
1187 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1188 | mov TMP2, RA // Save RA, RC for us.
1189 | mov TMP1, NARGS:RD
1193 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1195 | lea CARG3d, [RA+NARGS:RD*8]
1196 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
1198 | lea RC, [RA+NARGS:RD*8]
1203 | mov L:RB->base, BASE // This is the callers base!
1206 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1207 | mov BASE, L:RB->base
1209 | mov NARGS:RD, TMP1
1210 | mov LFUNC:RB, [RA-8]
1212 | // This is fragile. L->base must not move, KBASE must always be defined.
1213 | cmp KBASE, BASE // Continue with CALLT if flag set.
1216 | ins_call // Otherwise call resolved metamethod.
1218 |//-- Argument coercion for 'for' statement ------------------------------
1222 | mov L:RB->base, BASE
1223 | mov FCARG2, RA // Caveat: FCARG2 == BASE
1224 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
1226 | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
1227 | mov BASE, L:RB->base
1233 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1235 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
1238 |//-----------------------------------------------------------------------
1239 |//-- Fast functions -----------------------------------------------------
1240 |//-----------------------------------------------------------------------
1242 |.macro .ffunc, name
1246 |.macro .ffunc_1, name
1248 | cmp NARGS:RD, 1+1; jb ->fff_fallback
1251 |.macro .ffunc_2, name
1253 | cmp NARGS:RD, 2+1; jb ->fff_fallback
1256 |.macro .ffunc_n, name
1258 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1262 |.macro .ffunc_n, name, op
1264 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1269 |.macro .ffunc_nsse, name, op
1271 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1272 | op xmm0, qword [BASE]
1275 |.macro .ffunc_nsse, name
1276 | .ffunc_nsse name, movsd
1279 |.macro .ffunc_nn, name
1281 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1282 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1284 | fld qword [BASE+8]
1287 |.macro .ffunc_nnsse, name
1289 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1290 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1291 | movsd xmm0, qword [BASE]
1292 | movsd xmm1, qword [BASE+8]
1295 |.macro .ffunc_nnr, name
1297 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1298 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1299 | fld qword [BASE+8]
1303 |// Inlined GC threshold check. Caveat: uses label 1.
1305 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1306 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1312 |//-- Base library: checks -----------------------------------------------
1316 | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
1356 | jbe >1; mov RC, RB; 1:
1359 | mov CFUNC:RB, [BASE-8]
1360 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1362 | mov dword [BASE-4], LJ_TSTR
1363 | mov [BASE-8], STR:RC
1367 | mov RC, ~LJ_TLIGHTUD
1371 |//-- Base library: getters and setters ---------------------------------
1373 |.ffunc_1 getmetatable
1376 | cmp RB, LJ_TTAB; jne >6
1377 |1: // Field metatable must be at same offset for GCtab and GCudata!
1378 | mov TAB:RB, [BASE]
1379 | mov TAB:RB, TAB:RB->metatable
1381 | test TAB:RB, TAB:RB
1382 | mov dword [BASE-4], LJ_TNIL
1384 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1385 | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
1386 | mov [BASE-8], TAB:RB
1387 | mov RA, TAB:RB->hmask
1388 | and RA, STR:RC->hash
1390 | add NODE:RA, TAB:RB->node
1391 |3: // Rearranged logic, because we expect _not_ to find the key.
1392 | cmp dword NODE:RA->key.it, LJ_TSTR
1394 | cmp dword NODE:RA->key.gcr, STR:RC
1397 | mov NODE:RA, NODE:RA->next
1398 | test NODE:RA, NODE:RA
1400 | jmp ->fff_res1 // Not found, keep default result.
1403 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1405 | mov [BASE-4], RB // Return value of mt.__metatable.
1410 | cmp RB, LJ_TUDATA; je <1
1412 | cmp RB, LJ_TNUMX; ja >8
1413 | cmp RB, LJ_TISNUM; jbe >7
1414 | mov RB, LJ_TLIGHTUD
1418 | cmp RB, LJ_TISNUM; ja >8
1423 | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1426 |.ffunc_2 setmetatable
1427 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1428 | // Fast path: no mt for table yet and not clearing the mt.
1429 | mov TAB:RB, [BASE]
1430 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1431 | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
1432 | mov TAB:RC, [BASE+8]
1433 | mov TAB:RB->metatable, TAB:RC
1435 | mov dword [BASE-4], LJ_TTAB // Return original table.
1436 | mov [BASE-8], TAB:RB
1437 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1439 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1440 | barrierback TAB:RB, RC
1445 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1447 | mov RB, BASE // Save BASE.
1448 | lea CARG3d, [BASE+8]
1449 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1450 | mov CARG1d, SAVE_L
1452 | mov RB, BASE // Save BASE.
1453 | mov CARG2d, [BASE]
1454 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1455 | mov CARG1d, SAVE_L
1457 | mov TAB:RD, [BASE]
1461 | mov RB, BASE // Save BASE.
1465 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1466 | // cTValue * returned in eax (RD).
1467 | mov BASE, RB // Restore BASE.
1468 | // Copy table slot.
1482 |//-- Base library: conversions ------------------------------------------
1485 | // Only handles the number case inline (without a base argument).
1486 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
1487 | cmp dword [BASE+4], LJ_TISNUM
1490 | mov RB, dword [BASE]; jmp ->fff_resi
1494 | jae ->fff_fallback
1497 | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1499 | fld qword [BASE]; jmp ->fff_resn
1503 | // Only handles the string or number case inline.
1505 | cmp dword [BASE+4], LJ_TSTR; jne >3
1506 | // A __tostring method in the string base metatable is ignored.
1507 | mov STR:RD, [BASE]
1509 | mov dword [BASE-4], LJ_TSTR
1510 | mov [BASE-8], STR:RD
1512 |3: // Handle numbers inline, unless a number base metatable is present.
1513 | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
1514 | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1515 | jne ->fff_fallback
1516 | ffgccheck // Caveat: uses label 1.
1518 | mov L:RB->base, BASE // Add frame since C call can throw.
1519 | mov SAVE_PC, PC // Redundant (but a defined value).
1520 |.if X64 and not X64WIN
1521 | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
1523 | mov L:FCARG1, L:RB
1525 | call extern lj_str_fromnumber@8 // (lua_State *L, cTValue *o)
1527 | call extern lj_str_fromnum@8 // (lua_State *L, lua_Number *np)
1529 | // GCstr returned in eax (RD).
1530 | mov BASE, L:RB->base
1533 |//-- Base library: iterators -------------------------------------------
1536 | je >2 // Missing 2nd arg?
1538 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1540 | mov L:RB->base, BASE // Add frame since C call can throw.
1541 | mov L:RB->top, BASE // Dummy frame length is ok.
1544 | lea CARG3d, [BASE+8]
1545 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1548 | mov CARG2d, [BASE]
1549 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1552 | mov TAB:RD, [BASE]
1558 | mov SAVE_PC, PC // Redundant (but a defined value).
1559 | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1560 | // Flag returned in eax (RD).
1561 | mov BASE, L:RB->base
1562 | test RD, RD; jz >3 // End of traversal?
1563 | // Copy key and value to results.
1566 | mov RDa, [BASE+16]
1582 |2: // Set missing 2nd arg to nil.
1583 | mov dword [BASE+12], LJ_TNIL
1585 |3: // End of traversal: return nil.
1586 | mov dword [BASE-4], LJ_TNIL
1590 | mov TAB:RB, [BASE]
1591 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1592 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1593 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1595 | mov CFUNC:RB, [BASE-8]
1596 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1598 | mov dword [BASE-4], LJ_TFUNC
1599 | mov [BASE-8], CFUNC:RD
1600 | mov dword [BASE+12], LJ_TNIL
1604 |.ffunc_1 ipairs_aux
1605 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1606 | cmp dword [BASE+12], LJ_TISNUM
1608 | jne ->fff_fallback
1610 | jae ->fff_fallback
1614 | mov RD, dword [BASE+8]
1616 | mov dword [BASE-4], LJ_TISNUM
1617 | mov dword [BASE-8], RD
1619 | movsd xmm0, qword [BASE+8]
1620 | sseconst_1 xmm1, RBa
1623 | movsd qword [BASE-8], xmm0
1626 | fld qword [BASE+8]
1630 | fstp qword [BASE-8]
1634 | mov TAB:RB, [BASE]
1635 | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
1637 | add RD, TAB:RB->array
1639 | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
1640 | // Copy array slot.
1651 |2: // Check for empty hash part first. Otherwise call C function.
1652 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1653 | mov FCARG1, TAB:RB
1654 | mov RB, BASE // Save BASE.
1655 | mov FCARG2, RD // Caveat: FCARG2 == BASE
1656 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1657 | // cTValue * or NULL returned in eax (RD).
1666 | mov TAB:RB, [BASE]
1667 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1668 #ifdef LUAJIT_ENABLE_LUA52COMPAT
1669 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1671 | mov CFUNC:RB, [BASE-8]
1672 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1674 | mov dword [BASE-4], LJ_TFUNC
1675 | mov [BASE-8], CFUNC:RD
1677 | mov dword [BASE+12], LJ_TISNUM
1678 | mov dword [BASE+8], 0
1681 | movsd qword [BASE+8], xmm0
1684 | fstp qword [BASE+8]
1689 |//-- Base library: catch errors ----------------------------------------
1694 | mov PC, 8+FRAME_PCALL
1696 | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1697 | shr RB, HOOK_ACTIVE_SHIFT
1699 | add PC, RB // Remember active hook before pcall.
1700 | jmp ->vm_call_dispatch
1703 | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
1704 | mov RB, [BASE+4] // Swap function and traceback.
1706 | mov dword [BASE+4], LJ_TFUNC
1707 | mov LFUNC:RB, [BASE]
1709 | mov [BASE+8], LFUNC:RB
1713 | mov PC, 16+FRAME_PCALL
1716 |//-- Coroutine library --------------------------------------------------
1718 |.macro coroutine_resume_wrap, resume
1720 |.ffunc_1 coroutine_resume
1723 |.ffunc coroutine_wrap_aux
1724 | mov CFUNC:RB, [BASE-8]
1725 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1735 | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
1737 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1738 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1740 | je >1 // Status != LUA_YIELD (i.e. 0)?
1741 | cmp RA, L:RB->base // Check for presence of initial func.
1745 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1747 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1749 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1753 | mov L:RB->base, BASE
1755 | add BASE, 8 // Keep resumed thread in stack for GC.
1757 | mov L:RB->top, BASE
1759 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1761 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1763 | sub RBa, PCa // Relative to PC.
1767 |2: // Move args to coroutine.
1790 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1791 | set_vmstate INTERP
1797 | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
1799 | mov BASE, L:RB->base
1800 | cmp eax, LUA_YIELD
1803 | mov RA, L:PC->base
1804 | mov KBASE, L:PC->top
1805 | mov L:PC->top, RA // Clear coroutine stack.
1808 | je >6 // No results?
1811 | cmp RD, L:RB->maxstack
1812 | ja >9 // Need to grow stack?
1816 |5: // Move results from coroutine.
1831 | lea RD, [PC+2] // nresults+1 = 1 + true + results.
1832 | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
1834 | lea RD, [PC+1] // nresults+1 = 1 + results.
1844 | test PC, FRAME_TYPE
1848 |8: // Coroutine returned with error (at co->top-1).
1850 | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
1853 | mov L:PC->top, RA // Clear error from coroutine stack.
1854 | // Copy error message.
1864 | mov RD, 1+2 // nresults+1 = 1 + false + error.
1869 | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
1870 | // Error function does not return.
1873 |9: // Handle stack expansion on return from yield.
1877 | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
1879 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1882 | call extern lj_state_growstack@8 // (lua_State *L, int n)
1888 | mov BASE, L:RB->base
1889 | jmp <4 // Retry the stack move.
1892 | coroutine_resume_wrap 1 // coroutine.resume
1893 | coroutine_resume_wrap 0 // coroutine.wrap
1895 |.ffunc coroutine_yield
1897 | test aword L:RB->cframe, CFRAME_RESUME
1899 | mov L:RB->base, BASE
1900 | lea RD, [BASE+NARGS:RD*8-8]
1903 | mov aword L:RB->cframe, RDa
1905 | mov byte L:RB->status, al
1906 | jmp ->vm_leave_unw
1908 |//-- Math library -------------------------------------------------------
1911 |->fff_resi: // Dummy.
1916 | fstp qword [BASE-8]
1921 | cmp dword [BASE+4], LJ_TISNUM; jne >2
1922 | mov RB, dword [BASE]
1923 | cmp RB, 0; jns ->fff_resi
1928 | mov dword [BASE-4], LJ_TISNUM
1929 | mov dword [BASE-8], RB
1933 | mov dword [BASE-4], 0x41e00000 // 2^31.
1934 | mov dword [BASE-8], 0
1939 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1942 | movsd xmm0, qword [BASE]
1943 | sseconst_abs xmm1, RDa
1947 | movsd qword [BASE-8], xmm0
1953 |->fff_resxmm0: // Dummy.
1956 | fstp qword [BASE-8]
1963 | test PC, FRAME_TYPE
1966 | cmp PC_RB, RDL // More results expected?
1968 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1970 | not RAa // Note: ~RA = -(RA+1)
1971 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
1974 |6: // Fill up results with nil.
1975 | mov dword [BASE+RD*8-12], LJ_TNIL
1979 |7: // Non-standard return case.
1980 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
1983 |.macro math_round, func
1984 | .ffunc math_ .. func
1986 | cmp dword [BASE+4], LJ_TISNUM; jne >1
1987 | mov RB, dword [BASE]; jmp ->fff_resi
1991 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1994 | movsd xmm0, qword [BASE]
1995 | call ->vm_ .. func
1996 || if (LJ_DUALNUM) {
1998 | cmp RB, 0x80000000
2001 | ucomisd xmm0, xmm1
2008 | call ->vm_ .. func
2009 || if (LJ_DUALNUM) {
2013 | cmp RB, 0x80000000; jne >2
2033 |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2035 |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
2037 |.ffunc_n math_log, fldln2; fyl2x; jmp ->fff_resn
2038 |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn
2039 |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn
2041 |.ffunc_n math_sin; fsin; jmp ->fff_resn
2042 |.ffunc_n math_cos; fcos; jmp ->fff_resn
2043 |.ffunc_n math_tan; fptan; fpop; jmp ->fff_resn
2046 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan
2049 | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan
2051 |.ffunc_n math_atan; fld1; fpatan; jmp ->fff_resn
2053 |.macro math_extern, func
2055 | .ffunc_nsse math_ .. func
2057 | movsd FPARG1, xmm0
2061 | .ffunc_n math_ .. func
2066 | call extern lj_vm_ .. func
2081 |.ffunc_nsse math_rad
2082 | mov CFUNC:RB, [BASE-8]
2083 | mulsd xmm0, qword CFUNC:RB->upvalue[0]
2087 | mov CFUNC:RB, [BASE-8]
2088 | fmul qword CFUNC:RB->upvalue[0]
2092 |.ffunc_nn math_atan2; fpatan; jmp ->fff_resn
2093 |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
2095 |.ffunc_1 math_frexp
2097 | cmp RB, LJ_TISNUM; jae ->fff_fallback
2100 | mov [BASE-4], RB; mov [BASE-8], RC
2101 | shl RB, 1; cmp RB, 0xffe00000; jae >3
2104 | cmp RB, 0x00200000; jb >4
2106 | shr RB, 21; sub RB, RC // Extract and unbias exponent.
2110 | mov TMP1, RB; fild TMP1
2113 | and RB, 0x800fffff // Mask off exponent.
2114 | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
2118 | movsd qword [BASE], xmm0
2124 |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2126 | xorps xmm0, xmm0; jmp <2
2130 |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
2132 | movsd xmm0, qword [BASE]
2133 | sseconst_hi xmm1, RBa, 43500000 // 2^54.
2135 | movsd qword [BASE-8], xmm0
2138 | mov TMP1, 0x5a800000; fmul TMP1 // x = x*2^54
2139 | fstp qword [BASE-8]
2141 | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2144 |.ffunc_nsse math_modf
2150 | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
2156 | movsd qword [BASE-8], xmm0
2157 | movsd qword [BASE], xmm4
2163 | fstp qword [BASE-8]
2166 | mov RC, [BASE-4]; mov RB, [BASE+4]
2167 | xor RC, RB; js >3 // Need to adjust sign?
2172 | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
2176 | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
2178 | fldz; fxch; jmp <1 // Return +-Inf and +-0.
2181 |.ffunc_nnr math_fmod
2182 |1: ; fprem; fnstsw ax; sahf; jp <1
2187 |.ffunc_nnsse math_pow; call ->vm_pow; jmp ->fff_resxmm0
2189 |.ffunc_nn math_pow; call ->vm_pow; jmp ->fff_resn
2192 |.macro math_minmax, name, cmovop, fcmovop, nofcmovop, sseop
2195 | cmp dword [BASE+4], LJ_TISNUM
2198 | mov RB, dword [BASE]
2199 |1: // Handle integers.
2200 | cmp RA, RD; jae ->fff_resi
2201 | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2202 | cmp RB, dword [BASE+RA*8-8]
2203 | cmovop RB, dword [BASE+RA*8-8]
2208 | // Convert intermediate result to number and continue below.
2221 | jae ->fff_fallback
2225 | movsd xmm0, qword [BASE]
2226 |5: // Handle numbers or integers.
2227 | cmp RA, RD; jae ->fff_resxmm0
2228 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2232 | cvtsi2sd xmm1, dword [BASE+RA*8-8]
2235 | jae ->fff_fallback
2238 | movsd xmm1, qword [BASE+RA*8-8]
2246 |5: // Handle numbers or integers.
2247 | cmp RA, RD; jae ->fff_resn
2248 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2252 | fild dword [BASE+RA*8-8]
2258 | fld qword [BASE+RA*8-8]
2261 | fucomi st1; fcmovop st1; fpop1
2264 | fucom st1; fnstsw ax; test ah, 1; nofcmovop >2; fxch; 2: ; fpop
2273 | math_minmax math_min, cmovg, fcmovnbe, jz, minsd
2274 | math_minmax math_max, cmovl, fcmovbe, jnz, maxsd
2277 | fpop; jmp ->fff_fallback
2280 |//-- String library -----------------------------------------------------
2282 |.ffunc_1 string_len
2283 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2284 | mov STR:RB, [BASE]
2286 | mov RB, dword STR:RB->len; jmp ->fff_resi
2288 | cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0
2290 | fild dword STR:RB->len; jmp ->fff_resn
2293 |.ffunc string_byte // Only handle the 1-arg case here.
2294 | cmp NARGS:RD, 1+1; jne ->fff_fallback
2295 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2296 | mov STR:RB, [BASE]
2298 | cmp dword STR:RB->len, 1
2299 | jb ->fff_res0 // Return no results for empty string.
2300 | movzx RB, byte STR:RB[1]
2304 | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2306 | mov TMP1, RB; fild TMP1; jmp ->fff_resn
2309 |.ffunc string_char // Only handle the 1-arg case here.
2311 | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
2312 | cmp dword [BASE+4], LJ_TISNUM
2314 | jne ->fff_fallback
2315 | mov RB, dword [BASE]
2316 | cmp RB, 255; ja ->fff_fallback
2319 | jae ->fff_fallback
2320 | cvttsd2si RB, qword [BASE]
2321 | cmp RB, 255; ja ->fff_fallback
2324 | jae ->fff_fallback
2327 | cmp TMP2, 255; ja ->fff_fallback
2334 | lea RDa, TMP2 // Points to stack. Little-endian.
2337 | mov L:RB->base, BASE
2339 | mov CARG3d, TMP3 // Zero-extended to size_t.
2340 | mov CARG2, RDa // May be 64 bit ptr to stack.
2347 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
2348 | // GCstr * returned in eax (RD).
2349 | mov BASE, L:RB->base
2351 | mov dword [BASE-4], LJ_TSTR
2352 | mov [BASE-8], STR:RD
2358 | cmp NARGS:RD, 1+2; jb ->fff_fallback
2360 | cmp dword [BASE+20], LJ_TISNUM
2362 | jne ->fff_fallback
2363 | mov RB, dword [BASE+16]
2366 | jae ->fff_fallback
2367 | cvttsd2si RB, qword [BASE+16]
2370 | jae ->fff_fallback
2371 | fld qword [BASE+16]
2375 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2376 | cmp dword [BASE+12], LJ_TISNUM
2378 | jne ->fff_fallback
2380 | jae ->fff_fallback
2382 | mov STR:RB, [BASE]
2384 | mov RB, STR:RB->len
2386 | mov RA, dword [BASE+8]
2388 | cvttsd2si RA, qword [BASE+8]
2391 | fld qword [BASE+8]
2397 | cmp RB, RC // len < end? (unsigned compare)
2400 | test RA, RA // start <= 0?
2404 | sub RC, RA // start > end?
2406 | lea RB, [STR:RB+RA+#STR-1]
2417 |5: // Negative end or overflow.
2419 | lea RC, [RC+RB+1] // end = end+(len+1)
2422 | mov RC, RB // end = len
2425 |7: // Negative start or underflow.
2427 | add RA, RB // start = start+(len+1)
2429 | jg <3 // start > 0?
2431 | mov RA, 1 // start = 1
2434 |->fff_emptystr: // Range underflow.
2435 | xor RC, RC // Zero length. Any ptr in RB is ok.
2438 |.ffunc_2 string_rep // Only handle the 1-char case inline.
2440 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2441 | cmp dword [BASE+12], LJ_TISNUM
2442 | mov STR:RB, [BASE]
2444 | jne ->fff_fallback
2445 | mov RC, dword [BASE+8]
2447 | jae ->fff_fallback
2448 | cvttsd2si RC, qword [BASE+8]
2450 | jae ->fff_fallback
2451 | fld qword [BASE+8]
2456 | jle ->fff_emptystr // Count <= 0? (or non-int)
2457 | cmp dword STR:RB->len, 1
2458 | jb ->fff_emptystr // Zero length string?
2459 | jne ->fff_fallback_2 // Fallback for > 1-char strings.
2460 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_2
2461 | movzx RA, byte STR:RB[1]
2462 | mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2468 |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
2473 | mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2476 |.ffunc_1 string_reverse
2478 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2479 | mov STR:RB, [BASE]
2480 | mov RC, STR:RB->len
2482 | jz ->fff_emptystr // Zero length string?
2483 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2485 | mov TMP2, PC // Need another temp register.
2491 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2493 | movzx RA, byte [RB]
2502 |.macro ffstring_case, name, lo, hi
2505 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2506 | mov STR:RB, [BASE]
2507 | mov RC, STR:RB->len
2508 | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
2510 | mov TMP2, PC // Need another temp register.
2516 | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
2518 |1: // ASCII case conversion. Yes, this is suboptimal code (do you care?).
2519 | movzx RA, byte [RB+RC]
2535 |ffstring_case string_lower, 0x41, 0x5a
2536 |ffstring_case string_upper, 0x61, 0x7a
2538 |//-- Table library ------------------------------------------------------
2540 |.ffunc_1 table_getn
2541 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
2542 | mov RB, BASE // Save BASE.
2543 | mov TAB:FCARG1, [BASE]
2544 | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t)
2545 | // Length of table returned in eax (RD).
2546 | mov BASE, RB // Restore BASE.
2548 | mov RB, RD; jmp ->fff_resi
2550 | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
2553 | mov ARG1, RD; fild ARG1; jmp ->fff_resn
2557 |//-- Bit library --------------------------------------------------------
2559 |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).
2561 |.macro .ffunc_bit, name, kind
2565 | sseconst_tobit xmm1, RBa
2567 | mov TMP1, TOBIT_BIAS
2570 | cmp dword [BASE+4], LJ_TISNUM
2573 | mov RB, dword [BASE]
2582 | jae ->fff_fallback
2585 | movsd xmm0, qword [BASE]
2587 | sseconst_tobit xmm1, RBa
2595 | mov TMP1, TOBIT_BIAS
2607 |.ffunc_bit bit_tobit, 0
2608 if (LJ_DUALNUM || sse) {
2622 |.macro .ffunc_bit_op, name, ins
2623 | .ffunc_bit name, 2
2624 | mov TMP2, NARGS:RD // Save for fallback.
2625 | lea RD, [BASE+NARGS:RD*8-16]
2629 | cmp dword [RD+4], LJ_TISNUM
2632 | ins RB, dword [RD]
2636 | ja ->fff_fallback_bit_op
2638 | jae ->fff_fallback_bit_op
2641 | movsd xmm0, qword [RD]
2657 |.ffunc_bit_op bit_band, and
2658 |.ffunc_bit_op bit_bor, or
2659 |.ffunc_bit_op bit_bxor, xor
2661 |.ffunc_bit bit_bswap, 1
2665 |.ffunc_bit bit_bnot, 1
2682 |->fff_fallback_bit_op:
2683 | mov NARGS:RD, TMP2 // Restore for fallback
2684 | jmp ->fff_fallback
2686 |.macro .ffunc_bit_sh, name, ins
2688 | .ffunc_bit name, 1
2689 | // Note: no inline conversion from number for 2nd argument!
2690 | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2691 | mov RA, dword [BASE+8]
2694 | sseconst_tobit xmm2, RBa
2702 | mov TMP1, TOBIT_BIAS
2711 | ins RB, cl // Assumes RA is ecx.
2715 |.ffunc_bit_sh bit_lshift, shl
2716 |.ffunc_bit_sh bit_rshift, shr
2717 |.ffunc_bit_sh bit_arshift, sar
2718 |.ffunc_bit_sh bit_rol, rol
2719 |.ffunc_bit_sh bit_ror, ror
2721 |//-----------------------------------------------------------------------
2724 | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
2725 | jmp ->fff_fallback
2727 | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
2728 |->fff_fallback: // Call fast function fallback handler.
2729 | // BASE = new base, RD = nargs+1
2731 | mov PC, [BASE-4] // Fallback may overwrite PC.
2732 | mov SAVE_PC, PC // Redundant (but a defined value).
2733 | mov L:RB->base, BASE
2734 | lea RD, [BASE+NARGS:RD*8-8]
2735 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2737 | mov CFUNC:RD, [BASE-8]
2738 | cmp RA, L:RB->maxstack
2739 | ja >5 // Need to grow stack.
2745 | call aword CFUNC:RD->f // (lua_State *L)
2746 | mov BASE, L:RB->base
2747 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2748 | test RD, RD; jg ->fff_res // Returned nresults+1?
2754 | lea NARGS:RD, [RA+1]
2755 | mov LFUNC:RB, [BASE-8]
2756 | jne ->vm_call_tail // Returned -1?
2757 | ins_callt // Returned 0: retry fast path.
2759 |// Reconstruct previous base for vmeta_call during tailcall.
2762 | test PC, FRAME_TYPE
2765 | not RBa // Note: ~RB = -(RB+1)
2766 | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
2767 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2772 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2774 |5: // Grow stack for fallback handler.
2775 | mov FCARG2, LUA_MINSTACK
2777 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2778 | mov BASE, L:RB->base
2779 | xor RD, RD // Simulate a return 0.
2780 | jmp <1 // Dumb retry (goes through ff first).
2782 |->fff_gcstep: // Call GC step function.
2783 | // BASE = new base, RD = nargs+1
2784 | pop RBa // Must keep stack at same level.
2785 | mov TMPa, RBa // Save return address
2787 | mov SAVE_PC, PC // Redundant (but a defined value).
2788 | mov L:RB->base, BASE
2789 | lea RD, [BASE+NARGS:RD*8-8]
2792 | call extern lj_gc_step@4 // (lua_State *L)
2793 | mov BASE, L:RB->base
2799 | push RBa // Restore return address.
2802 |//-----------------------------------------------------------------------
2803 |//-- Special dispatch targets -------------------------------------------
2804 |//-----------------------------------------------------------------------
2806 |->vm_record: // Dispatch target for recording phase.
2808 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2809 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2811 | // Decrement the hookcount for consistency, but always do the call.
2812 | test RDL, HOOK_ACTIVE
2814 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2816 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2820 |->vm_rethook: // Dispatch target for return hooks.
2821 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2822 | test RDL, HOOK_ACTIVE // Hook already active?
2826 |->vm_inshook: // Dispatch target for instr/line hooks.
2827 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2828 | test RDL, HOOK_ACTIVE // Hook already active?
2831 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2833 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2835 | test RDL, LUA_MASKLINE
2839 | mov L:RB->base, BASE
2840 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2842 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2843 | call extern lj_dispatch_ins@8 // (lua_State *L, BCIns *pc)
2845 | mov BASE, L:RB->base
2852 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2854 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
2857 |->cont_hook: // Continue from hook yield.
2860 | mov MULTRES, RA // Restore MULTRES for *M ins.
2863 |->vm_hotloop: // Hot loop counter underflow.
2865 | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
2866 | mov RB, LFUNC:RB->pc
2867 | movzx RD, byte [RB+PC2PROTO(framesize)]
2868 | lea RD, [BASE+RD*8]
2870 | mov L:RB->base, BASE
2873 | lea FCARG1, [DISPATCH+GG_DISP2J]
2874 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2876 | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
2880 |->vm_callhook: // Dispatch target for call hooks.
2886 |->vm_hotcall: // Hot call counter underflow.
2889 | or PC, 1 // Marker for hot call.
2892 | lea RD, [BASE+NARGS:RD*8-8]
2894 | mov L:RB->base, BASE
2898 | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
2899 | // ASMFunction returned in eax/rax (RDa).
2900 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2904 | mov BASE, L:RB->base
2914 |//-----------------------------------------------------------------------
2915 |//-- Trace exit handler -------------------------------------------------
2916 |//-----------------------------------------------------------------------
2918 |// Called from an exit stub with the exit number on the stack.
2919 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2923 | push r13; push r12
2924 | push r11; push r10; push r9; push r8
2925 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2926 | push rbx; push rdx; push rcx; push rax
2927 | movzx RC, byte [rbp-8] // Reconstruct exit number.
2928 | mov RCH, byte [rbp-16]
2929 | mov [rbp-8], r15; mov [rbp-16], r14
2931 | push ebp; lea ebp, [esp+12]; push ebp
2932 | push ebx; push edx; push ecx; push eax
2933 | movzx RC, byte [ebp-4] // Reconstruct exit number.
2934 | mov RCH, byte [ebp-8]
2935 | mov [ebp-4], edi; mov [ebp-8], esi
2937 | // Caveat: DISPATCH is ebx.
2938 | mov DISPATCH, [ebp]
2939 | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2941 | mov [DISPATCH+DISPATCH_J(exitno)], RC
2942 | mov [DISPATCH+DISPATCH_J(parent)], RA
2945 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2947 | sub rsp, 16*8 // Room for SSE regs.
2950 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2951 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2952 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2953 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2954 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2955 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2956 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2957 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2959 | sub esp, 8*8+16 // Room for SSE regs + args.
2960 | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2961 | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2962 | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2963 | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2965 | // Caveat: RB is ebp.
2966 | mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)]
2967 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2968 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2969 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
2970 | mov L:RB->base, BASE
2972 | lea CARG2, [rsp+4*8]
2976 | lea FCARG2, [esp+16]
2978 | lea FCARG1, [DISPATCH+GG_DISP2J]
2979 | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
2980 | // MULTRES or negated error code returned in eax (RD).
2981 | mov RAa, L:RB->cframe
2982 | and RAa, CFRAME_RAWMASK
2984 | // Reposition stack later.
2986 | mov rsp, RAa // Reposition stack to C frame.
2988 | mov esp, RAa // Reposition stack to C frame.
2990 | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
2991 | mov BASE, L:RB->base
2992 | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
2998 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
3001 | // Restore additional callee-save registers only used in compiled code.
3003 | lea RAa, [rsp+9*16+4*8]
3005 | movdqa xmm15, [RAa-9*16]
3006 | movdqa xmm14, [RAa-8*16]
3007 | movdqa xmm13, [RAa-7*16]
3008 | movdqa xmm12, [RAa-6*16]
3009 | movdqa xmm11, [RAa-5*16]
3010 | movdqa xmm10, [RAa-4*16]
3011 | movdqa xmm9, [RAa-3*16]
3012 | movdqa xmm8, [RAa-2*16]
3013 | movdqa xmm7, [RAa-1*16]
3014 | mov rsp, RAa // Reposition stack to C frame.
3015 | movdqa xmm6, [RAa]
3019 | add rsp, 16 // Reposition stack to C frame.
3025 | test RD, RD; js >3 // Check for error from exit.
3027 | mov LFUNC:KBASE, [BASE-8]
3028 | mov KBASE, LFUNC:KBASE->pc
3029 | mov KBASE, [KBASE+PC2PROTO(k)]
3030 | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
3031 | set_vmstate INTERP
3032 | // Modified copy of ins_next which handles function header dispatch, too.
3038 | cmp OP, BC_FUNCF // Function header?
3040 | mov RC, MULTRES // RC/RD holds nres+1.
3043 | jmp aword [DISPATCH+OP*8]
3045 | jmp aword [DISPATCH+OP*4]
3048 |3: // Rethrow error from the right C frame.
3052 | call extern lj_err_throw@8 // (lua_State *L, int errcode)
3055 |//-----------------------------------------------------------------------
3056 |//-- Math helper functions ----------------------------------------------
3057 |//-----------------------------------------------------------------------
3059 |// FP value rounding. Called by math.floor/math.ceil fast functions
3060 |// and from JIT code.
3062 |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified.
3063 |.macro vm_round_x87, mode1, mode2
3064 | fnstcw word [esp+4] // Caveat: overwrites ARG1 and ARG2.
3068 |.if mode2 ~= 0xffff
3072 | fldcw word [esp+6]
3074 | fldcw word [esp+4]
3079 |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
3080 |.macro vm_round_sse, mode
3081 | sseconst_abs xmm2, RDa
3082 | sseconst_2p52 xmm3, RDa
3084 | andpd xmm1, xmm2 // |x|
3085 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
3087 | andnpd xmm2, xmm0 // Isolate sign bit.
3088 |.if mode == 2 // trunc(x)?
3090 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3092 | sseconst_1 xmm3, RDa
3093 | cmpsd xmm0, xmm1, 1 // |x| < result?
3095 | subsd xmm1, xmm0 // If yes, subtract -1.
3096 | orpd xmm1, xmm2 // Merge sign bit back in.
3098 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3100 | orpd xmm1, xmm2 // Merge sign bit back in.
3101 | .if mode == 1 // ceil(x)?
3102 | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0.
3103 | cmpsd xmm0, xmm1, 6 // x > result?
3104 | .else // floor(x)?
3105 | sseconst_1 xmm2, RDa
3106 | cmpsd xmm0, xmm1, 1 // x < result?
3109 | subsd xmm1, xmm0 // If yes, subtract +-1.
3116 |.macro vm_round, name, ssemode, mode1, mode2
3119 | vm_round_x87 mode1, mode2
3122 | vm_round_sse ssemode
3125 | vm_round vm_floor, 0, 0x0400, 0xf7ff
3126 | vm_round vm_ceil, 1, 0x0800, 0xfbff
3127 | vm_round vm_trunc, 2, 0x0c00, 0xffff
3129 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
3132 |// Args in xmm0/xmm1, return value in xmm0.
3133 |// Caveat: xmm0-xmm5 and RC (eax) modified!
3136 | sseconst_abs xmm2, RDa
3137 | sseconst_2p52 xmm3, RDa
3139 | andpd xmm4, xmm2 // |x/y|
3140 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
3142 | andnpd xmm2, xmm0 // Isolate sign bit.
3143 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
3145 | orpd xmm4, xmm2 // Merge sign bit back in.
3146 | sseconst_1 xmm2, RDa
3147 | cmpsd xmm0, xmm4, 1 // x/y < result?
3149 | subsd xmm4, xmm0 // If yes, subtract 1.0.
3160 |// Args/ret on x87 stack (y on top). No xmm registers modified.
3161 |// Caveat: needs 3 slots on x87 stack! RC (eax) modified!
3164 | fnstcw word [esp+4]
3169 | fldcw word [esp+6]
3171 | fldcw word [esp+4]
3177 |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
3178 |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
3179 |// Caveat: needs 3 slots on x87 stack!
3181 | fldl2e; fmulp st1 // e^x ==> 2^(x*log2(e))
3184 | .define expscratch, dword [rsp+8] // Use scratch area.
3186 | .define expscratch, dword [rsp-8] // Use red zone.
3188 | .define expscratch, dword [esp+4] // Needs 4 byte scratch area.
3190 | fst expscratch // Caveat: overwrites ARG1.
3191 | cmp expscratch, 0x7f800000; je >1 // Special case: e^+Inf = +Inf
3192 | cmp expscratch, 0xff800000; je >2 // Special case: e^-Inf = 0
3193 |->vm_exp2raw: // Entry point for vm_pow. Without +-Inf check.
3194 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3195 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3201 |// Generic power function x^y. Called by BC_POW, math.pow fast function,
3205 |// Args/ret on x87 stack (y on top). RC (eax) modified.
3206 |// Caveat: needs 3 slots on x87 stack!
3208 | fist dword [esp+4] // Store/reload int before comparison.
3209 | fild dword [esp+4] // Integral exponent used in vm_powi.
3213 | fucomp st1; fnstsw ax; sahf
3215 | jnz >8 // Branch for FP exponents.
3216 | jp >9 // Branch for NaN exponent.
3217 | fpop // Pop y and fallthrough to vm_powi.
3219 |// FP/int power function x^i. Arg1/ret on x87 stack.
3220 |// Arg2 (int) on C stack. RC (eax) modified.
3221 |// Caveat: needs 2 slots on x87 stack!
3223 | cmp eax, 1; jle >6 // i<=1?
3224 | // Now 1 < (unsigned)i <= 0x80000000.
3225 |1: // Handle leading zeros.
3226 | test eax, 1; jnz >2
3233 |3: // Handle trailing bits.
3244 | je <5 // x^1 ==> x
3248 | cmp eax, 1; je <5 // x^-1 ==> 1/x
3249 | jmp <1 // x^-i ==> (1/x)^i
3251 | fpop; fld1 // x^0 ==> 1
3254 |8: // FP/FP power function x^y.
3258 | mov eax, [esp+4]; shl eax, 1
3259 | cmp eax, 0xff000000; je >2 // x^+-Inf?
3260 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3261 | cmp eax, 0xff000000; je >4 // +-Inf^y?
3265 |9: // Handle x^NaN.
3270 | fucomp st2; fnstsw ax; sahf
3272 | je >1 // 1^NaN ==> 1
3273 | fxch // x^NaN ==> NaN
3278 |2: // Handle x^+-Inf.
3284 | fucomp st1; fnstsw ax; sahf
3286 | je >3 // +-1^+-Inf ==> 1
3287 | fpop; fabs; fldz; mov eax, 0; setc al
3288 | ror eax, 1; xor eax, [esp+4]; jns >3 // |x|<>1, x^+-Inf ==> +Inf/0
3294 |4: // Handle +-0^y or +-Inf^y.
3295 | cmp dword [esp+4], 0; jge <3 // y >= 0, x^y ==> |x|
3297 | test eax, eax; jz >5 // y < 0, +-0^y ==> +Inf
3298 | fldz // y < 0, +-Inf^y ==> 0
3301 | mov dword [esp+4], 0x7f800000 // Return +Inf.
3309 |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified.
3310 |// Needs 16 byte scratch area for x86. Also called from JIT code.
3312 | cvtsd2si eax, xmm1
3313 | cvtsi2sd xmm2, eax
3314 | ucomisd xmm1, xmm2
3315 | jnz >8 // Branch for FP exponents.
3316 | jp >9 // Branch for NaN exponent.
3317 | // Fallthrough to vm_powi_sse.
3319 |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
3321 | cmp eax, 1; jle >6 // i<=1?
3322 | // Now 1 < (unsigned)i <= 0x80000000.
3323 |1: // Handle leading zeros.
3324 | test eax, 1; jnz >2
3331 |3: // Handle trailing bits.
3342 | je <5 // x^1 ==> x
3343 | jb >7 // x^0 ==> 1
3346 | sseconst_1 xmm1, RDa
3351 | sseconst_1 xmm0, RDa
3354 |8: // FP/FP power function x^y.
3356 | movd rax, xmm1; shl rax, 1
3357 | rol rax, 12; cmp rax, 0xffe; je >2 // x^+-Inf?
3358 | movd rax, xmm0; shl rax, 1; je >4 // +-0^y?
3359 | rol rax, 12; cmp rax, 0xffe; je >5 // +-Inf^y?
3361 | movsd qword [rsp+16], xmm1 // Use scratch area.
3362 | movsd qword [rsp+8], xmm0
3363 | fld qword [rsp+16]
3366 | movsd qword [rsp-16], xmm1 // Use red zone.
3367 | movsd qword [rsp-8], xmm0
3368 | fld qword [rsp-16]
3372 | movsd qword [esp+12], xmm1 // Needs 16 byte scratch area.
3373 | movsd qword [esp+4], xmm0
3374 | cmp dword [esp+12], 0; jne >1
3375 | mov eax, [esp+16]; shl eax, 1
3376 | cmp eax, 0xffe00000; je >2 // x^+-Inf?
3378 | cmp dword [esp+4], 0; jne >1
3379 | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
3380 | cmp eax, 0xffe00000; je >5 // +-Inf^y?
3382 | fld qword [esp+12]
3385 | fyl2x // y*log2(x)
3386 | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
3387 | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
3389 | fstp qword [rsp+8] // Use scratch area.
3390 | movsd xmm0, qword [rsp+8]
3392 | fstp qword [rsp-8] // Use red zone.
3393 | movsd xmm0, qword [rsp-8]
3395 | fstp qword [esp+4] // Needs 8 byte scratch area.
3396 | movsd xmm0, qword [esp+4]
3400 |9: // Handle x^NaN.
3401 | sseconst_1 xmm2, RDa
3402 | ucomisd xmm0, xmm2; je >1 // 1^NaN ==> 1
3403 | movaps xmm0, xmm1 // x^NaN ==> NaN
3407 |2: // Handle x^+-Inf.
3408 | sseconst_abs xmm2, RDa
3409 | andpd xmm0, xmm2 // |x|
3410 | sseconst_1 xmm2, RDa
3411 | ucomisd xmm0, xmm2; je <1 // +-1^+-Inf ==> 1
3412 | movmskpd eax, xmm1
3414 | mov ah, al; setc al; xor al, ah; jne <1 // |x|<>1, x^+-Inf ==> +Inf/0
3416 | sseconst_hi xmm0, RDa, 7ff00000 // +Inf
3419 |4: // Handle +-0^y.
3420 | movmskpd eax, xmm1; test eax, eax; jnz <3 // y < 0, +-0^y ==> +Inf
3421 | xorps xmm0, xmm0 // y >= 0, +-0^y ==> 0
3424 |5: // Handle +-Inf^y.
3425 | movmskpd eax, xmm1; test eax, eax; jz <3 // y >= 0, +-Inf^y ==> +Inf
3426 | xorps xmm0, xmm0 // y < 0, +-Inf^y ==> 0
3429 |// Callable from C: double lj_vm_foldfpm(double x, int fpm)
3430 |// Computes fpm(x) for extended math functions. ORDER FPM.
3437 | .define fpmop, CARG2d
3439 | .define fpmop, CARG1d
3441 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3442 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3443 | sqrtsd xmm0, xmm0; ret
3446 | movsd qword [rsp+8], xmm0 // Use scratch area.
3449 | movsd qword [rsp-8], xmm0 // Use red zone.
3452 | cmp fpmop, 5; ja >2
3453 | .if X64WIN; pop rax; .endif
3456 | .if X64WIN; push rax; .endif
3459 | call ->vm_exp2_x87
3460 | .if X64WIN; push rax; .endif
3462 |2: ; cmp fpmop, 7; je >1; ja >2
3463 | fldln2; fxch; fyl2x; jmp >7
3464 |1: ; fld1; fxch; fyl2x; jmp >7
3465 |2: ; cmp fpmop, 9; je >1; ja >2
3466 | fldlg2; fxch; fyl2x; jmp >7
3468 |2: ; cmp fpmop, 11; je >1; ja >9
3473 | fstp qword [rsp+8] // Use scratch area.
3474 | movsd xmm0, qword [rsp+8]
3476 | fstp qword [rsp-8] // Use red zone.
3477 | movsd xmm0, qword [rsp-8]
3481 |.else // x86 calling convention.
3483 | .define fpmop, eax
3484 | mov fpmop, [esp+12]
3485 | movsd xmm0, qword [esp+4]
3486 | cmp fpmop, 1; je >1; ja >2
3487 | call ->vm_floor; jmp >7
3488 |1: ; call ->vm_ceil; jmp >7
3489 |2: ; cmp fpmop, 3; je >1; ja >2
3490 | call ->vm_trunc; jmp >7
3494 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3497 |2: ; fld qword [esp+4]
3498 | cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3499 |2: ; cmp fpmop, 7; je >1; ja >2
3500 | fldln2; fxch; fyl2x; ret
3501 |1: ; fld1; fxch; fyl2x; ret
3502 |2: ; cmp fpmop, 9; je >1; ja >2
3503 | fldlg2; fxch; fyl2x; ret
3505 |2: ; cmp fpmop, 11; je >1; ja >9
3507 |1: ; fptan; fpop; ret
3511 | mov fpmop, [esp+12]
3513 | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
3514 | cmp fpmop, 3; jb ->vm_trunc; ja >2
3516 |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
3517 | cmp fpmop, 7; je >1; ja >2
3518 | fldln2; fxch; fyl2x; ret
3519 |1: ; fld1; fxch; fyl2x; ret
3520 |2: ; cmp fpmop, 9; je >1; ja >2
3521 | fldlg2; fxch; fyl2x; ret
3523 |2: ; cmp fpmop, 11; je >1; ja >9
3525 |1: ; fptan; fpop; ret
3527 |9: ; int3 // Bad fpm.
3530 |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
3531 |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
3532 |// and basic math functions. ORDER ARITH
3538 | .define foldop, CARG3d
3540 | .define foldop, CARG1d
3542 | cmp foldop, 1; je >1; ja >2
3543 | addsd xmm0, xmm1; ret
3544 |1: ; subsd xmm0, xmm1; ret
3545 |2: ; cmp foldop, 3; je >1; ja >2
3546 | mulsd xmm0, xmm1; ret
3547 |1: ; divsd xmm0, xmm1; ret
3548 |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow
3549 | cmp foldop, 7; je >1; ja >2
3550 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret
3551 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret
3552 |2: ; cmp foldop, 9; ja >2
3554 | movsd qword [rsp+8], xmm0 // Use scratch area.
3555 | movsd qword [rsp+16], xmm1
3557 | fld qword [rsp+16]
3559 | movsd qword [rsp-8], xmm0 // Use red zone.
3560 | movsd qword [rsp-16], xmm1
3562 | fld qword [rsp-16]
3568 | fstp qword [rsp+8] // Use scratch area.
3569 | movsd xmm0, qword [rsp+8]
3571 | fstp qword [rsp-8] // Use red zone.
3572 | movsd xmm0, qword [rsp-8]
3575 |1: ; fxch; fscale; fpop1; jmp <7
3576 |2: ; cmp foldop, 11; je >1; ja >9
3577 | minsd xmm0, xmm1; ret
3578 |1: ; maxsd xmm0, xmm1; ret
3579 |9: ; int3 // Bad op.
3581 |.else // x86 calling convention.
3583 | .define foldop, eax
3584 | mov foldop, [esp+20]
3585 | movsd xmm0, qword [esp+4]
3586 | movsd xmm1, qword [esp+12]
3587 | cmp foldop, 1; je >1; ja >2
3590 | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
3593 |1: ; subsd xmm0, xmm1; jmp <7
3594 |2: ; cmp foldop, 3; je >1; ja >2
3595 | mulsd xmm0, xmm1; jmp <7
3596 |1: ; divsd xmm0, xmm1; jmp <7
3599 | call ->vm_mod; jmp <7
3600 |1: ; pop edx; call ->vm_pow; push edx; jmp <7 // Writes to scratch area.
3601 |2: ; cmp foldop, 7; je >1; ja >2
3602 | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7
3603 |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7
3604 |2: ; cmp foldop, 9; ja >2
3605 | fld qword [esp+4] // Reload from stack
3606 | fld qword [esp+12]
3609 |1: ; fxch; fscale; fpop1; ret
3610 |2: ; cmp foldop, 11; je >1; ja >9
3611 | minsd xmm0, xmm1; jmp <7
3612 |1: ; maxsd xmm0, xmm1; jmp <7
3613 |9: ; int3 // Bad op.
3619 | fld qword [esp+12]
3620 | cmp eax, 1; je >1; ja >2
3622 |1: ; fsubp st1; ret
3623 |2: ; cmp eax, 3; je >1; ja >2
3625 |1: ; fdivp st1; ret
3626 |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow
3627 | cmp eax, 7; je >1; ja >2
3629 |1: ; fpop; fabs; ret
3630 |2: ; cmp eax, 9; je >1; ja >2
3632 |1: ; fxch; fscale; fpop1; ret
3633 |2: ; cmp eax, 11; je >1; ja >9
3635 | fucomi st1; fcmovnbe st1; fpop1; ret
3636 |1: ; fucomi st1; fcmovbe st1; fpop1; ret
3638 | fucom st1; fnstsw ax; test ah, 1; jz >2; fxch; 2: ; fpop; ret
3639 |1: ; fucom st1; fnstsw ax; test ah, 1; jnz >2; fxch; 2: ; fpop; ret
3641 |9: ; int3 // Bad op.
3644 |//-----------------------------------------------------------------------
3645 |//-- Miscellaneous functions --------------------------------------------
3646 |//-----------------------------------------------------------------------
3648 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3652 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
3660 | .if X64WIN; pop rsi; .endif
3666 | xor edx, 0x00200000 // Toggle ID bit in flags.
3671 | xor eax, eax // Zero means no features supported.
3673 | jz >1 // No ID toggle means no CPUID support.
3674 | mov eax, [esp+4] // Argument 1 is function number.
3678 | mov edi, [esp+16] // Argument 2 is result area.
3689 |//-----------------------------------------------------------------------
3690 |//-- Assertions ---------------------------------------------------------
3691 |//-----------------------------------------------------------------------
3693 |->assert_bad_for_arg_type:
3694 #ifdef LUA_USE_ASSERT
3699 |//-----------------------------------------------------------------------
3700 |//-- FFI helper functions -----------------------------------------------
3701 |//-----------------------------------------------------------------------
3703 |->vm_ffi_call@4: // Call C function via FFI.
3704 | // Caveat: needs special frame unwinding, see below.
3707 | .type CCSTATE, CCallState, rbx
3708 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3710 | .type CCSTATE, CCallState, ebx
3711 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3714 | // Readjust stack.
3716 | mov eax, CCSTATE->spadj
3719 | sub esp, CCSTATE->spadj
3720 #if LJ_TARGET_WINDOWS
3721 | mov CCSTATE->spadj, esp
3725 | // Copy stack slots.
3726 | movzx ecx, byte CCSTATE->nsp
3731 | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3732 | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3734 | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3735 | mov [esp+ecx*4], eax
3742 | movzx eax, byte CCSTATE->nfpr
3743 | mov CARG1, CCSTATE->gpr[0]
3744 | mov CARG2, CCSTATE->gpr[1]
3745 | mov CARG3, CCSTATE->gpr[2]
3746 | mov CARG4, CCSTATE->gpr[3]
3748 | mov CARG5, CCSTATE->gpr[4]
3749 | mov CARG6, CCSTATE->gpr[5]
3751 | test eax, eax; jz >5
3752 | movaps xmm0, CCSTATE->fpr[0]
3753 | movaps xmm1, CCSTATE->fpr[1]
3754 | movaps xmm2, CCSTATE->fpr[2]
3755 | movaps xmm3, CCSTATE->fpr[3]
3757 | cmp eax, 4; jbe >5
3758 | movaps xmm4, CCSTATE->fpr[4]
3759 | movaps xmm5, CCSTATE->fpr[5]
3760 | movaps xmm6, CCSTATE->fpr[6]
3761 | movaps xmm7, CCSTATE->fpr[7]
3765 | mov FCARG1, CCSTATE->gpr[0]
3766 | mov FCARG2, CCSTATE->gpr[1]
3769 | call aword CCSTATE->func
3772 | mov CCSTATE->gpr[0], rax
3773 | movaps CCSTATE->fpr[0], xmm0
3775 | mov CCSTATE->gpr[1], rdx
3776 | movaps CCSTATE->fpr[1], xmm1
3779 | mov CCSTATE->gpr[0], eax
3780 | mov CCSTATE->gpr[1], edx
3781 | cmp byte CCSTATE->resx87, 1
3784 | fstp qword CCSTATE->fpr[0].d[0]
3787 | fstp dword CCSTATE->fpr[0].f[0]
3789 #if LJ_TARGET_WINDOWS
3790 | sub CCSTATE->spadj, esp
3795 | mov rbx, [rbp-8]; leave; ret
3797 | mov ebx, [ebp-4]; leave; ret
3800 |// Note: vm_ffi_call must be the last function in this object file!
3802 |//-----------------------------------------------------------------------
3805 /* Generate the code for a single instruction. */
3806 static void build_ins(BuildCtx *ctx, BCOp op, int defop, int cmov, int sse)
3809 |// Note: aligning all instructions does not pay off.
3814 /* -- Comparison ops ---------------------------------------------------- */
3816 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3818 |.macro jmp_comp, lt, ge, le, gt, target
3832 ||default: break; /* Shut up GCC. */
3836 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3837 | // RA = src1, RD = src2, JMP with RD = target
3842 | mov RB, dword [BASE+RA*8]
3844 | cmp RB, dword [BASE+RD*8]
3845 | jmp_comp jge, jl, jg, jle, >9
3852 |7: // RA is not an integer.
3854 | // RA is a number.
3855 | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3856 | // RA is a number, RD is an integer.
3858 | cvtsi2sd xmm0, dword [BASE+RD*8]
3861 | fld qword [BASE+RA*8]
3862 | fild dword [BASE+RD*8]
3866 |8: // RA is an integer, RD is not an integer.
3868 | // RA is an integer, RD is a number.
3870 | cvtsi2sd xmm1, dword [BASE+RA*8]
3871 | movsd xmm0, qword [BASE+RD*8]
3873 | ucomisd xmm0, xmm1
3874 | jmp_comp jbe, ja, jb, jae, <9
3877 | fild dword [BASE+RA*8]
3881 | checknum RA, ->vmeta_comp
3882 | checknum RD, ->vmeta_comp
3886 | movsd xmm0, qword [BASE+RD*8]
3889 | ucomisd xmm0, qword [BASE+RA*8]
3893 | fld qword [BASE+RA*8] // Reverse order, i.e like cmp D, A.
3895 | fld qword [BASE+RD*8]
3898 | fcomparepp // eax (RD) modified!
3900 | // Unordered: all of ZF CF PF set, ordered: PF clear.
3901 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
3903 | jmp_comp jbe, ja, jb, jae, <9
3906 | jmp_comp jbe, ja, jb, jae, >1
3914 case BC_ISEQV: case BC_ISNEV:
3915 vk = op == BC_ISEQV;
3916 | ins_AD // RA = src1, RD = src2, JMP with RD = target
3917 | mov RB, [BASE+RD*8+4]
3920 | cmp RB, LJ_TISNUM; jne >7
3922 | mov RB, dword [BASE+RD*8]
3923 | cmp RB, dword [BASE+RA*8]
3934 |7: // RD is not an integer.
3936 | // RD is a number.
3937 | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
3938 | // RD is a number, RA is an integer.
3940 | cvtsi2sd xmm0, dword [BASE+RA*8]
3942 | fild dword [BASE+RA*8]
3946 |8: // RD is an integer, RA is not an integer.
3948 | // RD is an integer, RA is a number.
3950 | cvtsi2sd xmm0, dword [BASE+RD*8]
3951 | ucomisd xmm0, qword [BASE+RA*8]
3953 | fild dword [BASE+RD*8]
3954 | fld qword [BASE+RA*8]
3959 | cmp RB, LJ_TISNUM; jae >5
3964 | movsd xmm0, qword [BASE+RA*8]
3966 | ucomisd xmm0, qword [BASE+RD*8]
3970 | fld qword [BASE+RA*8]
3972 | fld qword [BASE+RD*8]
3974 | fcomparepp // eax (RD) modified!
3978 | jp >2 // Unordered means not equal.
3981 | jp >2 // Unordered means not equal.
3986 |1: // EQ: Branch to the target.
3989 |2: // NE: Fallthrough to next instruction.
3997 |2: // NE: Branch to the target.
4000 |1: // EQ: Fallthrough to next instruction.
4002 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
4003 op == BC_ISEQN || op == BC_ISNEN)) {
4009 if (op == BC_ISEQV || op == BC_ISNEV) {
4010 |5: // Either or both types are not numbers.
4012 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4013 | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
4015 | checktp RA, RB // Compare types.
4016 | jne <2 // Not the same type?
4018 | jae <1 // Same type and primitive type?
4020 | // Same types and not a primitive type. Compare GCobj or pvalue.
4021 | mov RA, [BASE+RA*8]
4022 | mov RD, [BASE+RD*8]
4024 | je <1 // Same GCobjs or pvalues?
4025 | cmp RB, LJ_TISTABUD
4026 | ja <2 // Different objects and not table/ud?
4028 | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
4032 | // Different tables or userdatas. Need to check __eq metamethod.
4033 | // Field metatable must be at same offset for GCtab and GCudata!
4034 | mov TAB:RB, TAB:RA->metatable
4035 | test TAB:RB, TAB:RB
4036 | jz <2 // No metatable?
4037 | test byte TAB:RB->nomm, 1<<MM_eq
4038 | jnz <2 // Or 'no __eq' flag set?
4040 | xor RB, RB // ne = 0
4042 | mov RB, 1 // ne = 1
4044 | jmp ->vmeta_equal // Handle __eq metamethod.
4045 } else if (LJ_HASFFI) {
4048 if (LJ_DUALNUM && vk) {
4053 | jmp ->vmeta_equal_cd
4056 case BC_ISEQS: case BC_ISNES:
4057 vk = op == BC_ISEQS;
4058 | ins_AND // RA = src, RD = str const, JMP with RD = target
4059 | mov RB, [BASE+RA*8+4]
4061 | cmp RB, LJ_TSTR; jne >3
4062 | mov RA, [BASE+RA*8]
4063 | cmp RA, [KBASE+RD*4]
4071 case BC_ISEQN: case BC_ISNEN:
4072 vk = op == BC_ISEQN;
4073 | ins_AD // RA = src, RD = num const, JMP with RD = target
4074 | mov RB, [BASE+RA*8+4]
4077 | cmp RB, LJ_TISNUM; jne >7
4078 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
4079 | mov RB, dword [KBASE+RD*8]
4080 | cmp RB, dword [BASE+RA*8]
4091 |7: // RA is not an integer.
4093 | // RA is a number.
4094 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
4095 | // RA is a number, RD is an integer.
4097 | cvtsi2sd xmm0, dword [KBASE+RD*8]
4099 | fild dword [KBASE+RD*8]
4103 |8: // RA is an integer, RD is a number.
4105 | cvtsi2sd xmm0, dword [BASE+RA*8]
4106 | ucomisd xmm0, qword [KBASE+RD*8]
4108 | fild dword [BASE+RA*8]
4109 | fld qword [KBASE+RD*8]
4113 | cmp RB, LJ_TISNUM; jae >3
4117 | movsd xmm0, qword [KBASE+RD*8]
4119 | ucomisd xmm0, qword [BASE+RA*8]
4123 | fld qword [KBASE+RD*8]
4125 | fld qword [BASE+RA*8]
4127 | fcomparepp // eax (RD) modified!
4130 case BC_ISEQP: case BC_ISNEP:
4131 vk = op == BC_ISEQP;
4132 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
4133 | mov RB, [BASE+RA*8+4]
4136 if (!LJ_HASFFI) goto iseqne_test;
4144 | cmp RB, LJ_TCDATA; jne <2
4145 | jmp ->vmeta_equal_cd
4148 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
4156 /* -- Unary test and copy ops ------------------------------------------- */
4158 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
4159 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
4160 | mov RB, [BASE+RD*8+4]
4162 | cmp RB, LJ_TISTRUECOND
4163 if (op == BC_IST || op == BC_ISTC) {
4168 if (op == BC_ISTC || op == BC_ISFC) {
4169 | mov [BASE+RA*8+4], RB
4170 | mov RB, [BASE+RD*8]
4171 | mov [BASE+RA*8], RB
4175 |1: // Fallthrough to the next instruction.
4179 /* -- Unary ops --------------------------------------------------------- */
4182 | ins_AD // RA = dst, RD = src
4184 | mov RBa, [BASE+RD*8]
4185 | mov [BASE+RA*8], RBa
4187 | mov RB, [BASE+RD*8+4]
4188 | mov RD, [BASE+RD*8]
4189 | mov [BASE+RA*8+4], RB
4190 | mov [BASE+RA*8], RD
4195 | ins_AD // RA = dst, RD = src
4197 | checktp RD, LJ_TISTRUECOND
4199 | mov [BASE+RA*8+4], RB
4203 | ins_AD // RA = dst, RD = src
4206 | mov RB, [BASE+RD*8]
4209 | mov dword [BASE+RA*8+4], LJ_TISNUM
4210 | mov dword [BASE+RA*8], RB
4214 | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
4215 | mov dword [BASE+RA*8], 0
4220 | checknum RD, ->vmeta_unm
4223 | movsd xmm0, qword [BASE+RD*8]
4224 | sseconst_sign xmm1, RDa
4226 | movsd qword [BASE+RA*8], xmm0
4228 | fld qword [BASE+RD*8]
4230 | fstp qword [BASE+RA*8]
4239 | ins_AD // RA = dst, RD = src
4241 | mov STR:RD, [BASE+RD*8]
4243 | mov RD, dword STR:RD->len
4245 | mov dword [BASE+RA*8+4], LJ_TISNUM
4246 | mov dword [BASE+RA*8], RD
4249 | cvtsi2sd xmm0, dword STR:RD->len
4251 | movsd qword [BASE+RA*8], xmm0
4253 | fild dword STR:RD->len
4255 | fstp qword [BASE+RA*8]
4259 | checktab RD, ->vmeta_len
4260 | mov TAB:FCARG1, [BASE+RD*8]
4261 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4262 | mov TAB:RB, TAB:FCARG1->metatable
4268 | mov RB, BASE // Save BASE.
4269 | call extern lj_tab_len@4 // (GCtab *t)
4270 | // Length of table returned in eax (RD).
4281 | mov BASE, RB // Restore BASE.
4284 #ifdef LUAJIT_ENABLE_LUA52COMPAT
4285 |9: // Check for __len.
4286 | test byte TAB:RB->nomm, 1<<MM_len
4288 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
4292 /* -- Binary ops -------------------------------------------------------- */
4294 |.macro ins_arithpre, x87ins, sseins, ssereg
4296 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4299 | checknum RB, ->vmeta_arith_vn
4301 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
4304 | movsd xmm0, qword [BASE+RB*8]
4305 | sseins ssereg, qword [KBASE+RC*8]
4307 | fld qword [BASE+RB*8]
4308 | x87ins qword [KBASE+RC*8]
4312 | checknum RB, ->vmeta_arith_nv
4314 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
4317 | movsd xmm0, qword [KBASE+RC*8]
4318 | sseins ssereg, qword [BASE+RB*8]
4320 | fld qword [KBASE+RC*8]
4321 | x87ins qword [BASE+RB*8]
4325 | checknum RB, ->vmeta_arith_vv
4326 | checknum RC, ->vmeta_arith_vv
4328 | movsd xmm0, qword [BASE+RB*8]
4329 | sseins ssereg, qword [BASE+RC*8]
4331 | fld qword [BASE+RB*8]
4332 | x87ins qword [BASE+RC*8]
4338 |.macro ins_arithdn, intins
4340 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
4343 | checkint RB, ->vmeta_arith_vn
4344 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
4345 | mov RB, [BASE+RB*8]
4346 | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
4349 | checkint RB, ->vmeta_arith_nv
4350 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
4351 | mov RC, [KBASE+RC*8]
4352 | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
4355 | checkint RB, ->vmeta_arith_vv
4356 | checkint RC, ->vmeta_arith_vv
4357 | mov RB, [BASE+RB*8]
4358 | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
4361 | mov dword [BASE+RA*8+4], LJ_TISNUM
4363 | mov dword [BASE+RA*8], RC
4365 | mov dword [BASE+RA*8], RB
4370 |.macro ins_arithpost
4372 | movsd qword [BASE+RA*8], xmm0
4374 | fstp qword [BASE+RA*8]
4378 |.macro ins_arith, x87ins, sseins
4379 | ins_arithpre x87ins, sseins, xmm0
4384 |.macro ins_arith, intins, x87ins, sseins
4386 | ins_arithdn intins
4388 | ins_arith, x87ins, sseins
4392 | // RA = dst, RB = src1 or num const, RC = src2 or num const
4393 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
4394 | ins_arith add, fadd, addsd
4396 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
4397 | ins_arith sub, fsub, subsd
4399 case BC_MULVN: case BC_MULNV: case BC_MULVV:
4400 | ins_arith imul, fmul, mulsd
4402 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
4403 | ins_arith fdiv, divsd
4406 | ins_arithpre fld, movsd, xmm1
4412 case BC_MODNV: case BC_MODVV:
4413 | ins_arithpre fld, movsd, xmm1
4414 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
4417 | ins_arithpre fld, movsd, xmm1
4424 | ins_ABC // RA = dst, RB = src_start, RC = src_end
4426 | mov L:CARG1d, SAVE_L
4427 | mov L:CARG1d->base, BASE
4428 | lea CARG2d, [BASE+RC*8]
4432 | mov L:RB, L:CARG1d
4434 | lea RA, [BASE+RC*8]
4441 | mov L:RB->base, BASE
4444 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
4445 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
4446 | mov BASE, L:RB->base
4449 | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
4452 | mov RCa, [BASE+RB*8]
4453 | mov [BASE+RA*8], RCa
4455 | mov RC, [BASE+RB*8+4]
4456 | mov RB, [BASE+RB*8]
4457 | mov [BASE+RA*8+4], RC
4458 | mov [BASE+RA*8], RB
4463 /* -- Constant ops ------------------------------------------------------ */
4466 | ins_AND // RA = dst, RD = str const (~)
4467 | mov RD, [KBASE+RD*4]
4468 | mov dword [BASE+RA*8+4], LJ_TSTR
4469 | mov [BASE+RA*8], RD
4474 | ins_AND // RA = dst, RD = cdata const (~)
4475 | mov RD, [KBASE+RD*4]
4476 | mov dword [BASE+RA*8+4], LJ_TCDATA
4477 | mov [BASE+RA*8], RD
4482 | ins_AD // RA = dst, RD = signed int16 literal
4485 | mov dword [BASE+RA*8+4], LJ_TISNUM
4486 | mov dword [BASE+RA*8], RD
4488 | movsx RD, RDW // Sign-extend literal.
4490 | movsd qword [BASE+RA*8], xmm0
4492 | fild PC_RD // Refetch signed RD from instruction.
4493 | fstp qword [BASE+RA*8]
4498 | ins_AD // RA = dst, RD = num const
4500 | movsd xmm0, qword [KBASE+RD*8]
4501 | movsd qword [BASE+RA*8], xmm0
4503 | fld qword [KBASE+RD*8]
4504 | fstp qword [BASE+RA*8]
4509 | ins_AND // RA = dst, RD = primitive type (~)
4510 | mov [BASE+RA*8+4], RD
4514 | ins_AD // RA = dst_start, RD = dst_end
4515 | lea RA, [BASE+RA*8+12]
4516 | lea RD, [BASE+RD*8+4]
4518 | mov [RA-8], RB // Sets minimum 2 slots.
4527 /* -- Upvalue and function ops ------------------------------------------ */
4530 | ins_AD // RA = dst, RD = upvalue #
4531 | mov LFUNC:RB, [BASE-8]
4532 | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
4533 | mov RB, UPVAL:RB->v
4536 | mov [BASE+RA*8], RDa
4540 | mov [BASE+RA*8+4], RD
4541 | mov [BASE+RA*8], RB
4546 #define TV2MARKOFS \
4547 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
4548 | ins_AD // RA = upvalue #, RD = src
4549 | mov LFUNC:RB, [BASE-8]
4550 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4551 | cmp byte UPVAL:RB->closed, 0
4552 | mov RB, UPVAL:RB->v
4553 | mov RA, [BASE+RD*8]
4554 | mov RD, [BASE+RD*8+4]
4558 | // Check barrier for closed upvalue.
4559 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
4564 |2: // Upvalue is black. Check if new value is collectable and white.
4566 | cmp RD, LJ_TISNUM - LJ_TISGCV // tvisgcv(v)
4568 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
4570 | // Crossed a write barrier. Move the barrier forward.
4571 |.if X64 and not X64WIN
4573 | mov RB, BASE // Save BASE.
4575 | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
4577 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4578 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4579 | mov BASE, RB // Restore BASE.
4584 | ins_AND // RA = upvalue #, RD = str const (~)
4585 | mov LFUNC:RB, [BASE-8]
4586 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4587 | mov GCOBJ:RA, [KBASE+RD*4]
4588 | mov RD, UPVAL:RB->v
4589 | mov [RD], GCOBJ:RA
4590 | mov dword [RD+4], LJ_TSTR
4591 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
4596 |2: // Check if string is white and ensure upvalue is closed.
4597 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
4599 | cmp byte UPVAL:RB->closed, 0
4601 | // Crossed a write barrier. Move the barrier forward.
4602 | mov RB, BASE // Save BASE (FCARG2 == BASE).
4604 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4605 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4606 | mov BASE, RB // Restore BASE.
4610 | ins_AD // RA = upvalue #, RD = num const
4611 | mov LFUNC:RB, [BASE-8]
4613 | movsd xmm0, qword [KBASE+RD*8]
4615 | fld qword [KBASE+RD*8]
4617 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4618 | mov RA, UPVAL:RB->v
4620 | movsd qword [RA], xmm0
4627 | ins_AND // RA = upvalue #, RD = primitive type (~)
4628 | mov LFUNC:RB, [BASE-8]
4629 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4630 | mov RA, UPVAL:RB->v
4635 | ins_AD // RA = level, RD = target
4636 | branchPC RD // Do this first to free RD.
4638 | cmp dword L:RB->openupval, 0
4640 | mov L:RB->base, BASE
4641 | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
4642 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4643 | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
4644 | mov BASE, L:RB->base
4650 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
4653 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4654 | mov CARG3d, [BASE-8]
4655 | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
4658 | mov LFUNC:RA, [BASE-8]
4659 | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
4661 | mov ARG3, LFUNC:RA
4662 | mov ARG2, PROTO:RD
4664 | mov L:RB->base, BASE
4667 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4668 | call extern lj_func_newL_gc
4669 | // GCfuncL * returned in eax (RC).
4670 | mov BASE, L:RB->base
4672 | mov [BASE+RA*8], LFUNC:RC
4673 | mov dword [BASE+RA*8+4], LJ_TFUNC
4677 /* -- Table ops --------------------------------------------------------- */
4680 | ins_AD // RA = dst, RD = hbits|asize
4682 | mov L:RB->base, BASE
4683 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4684 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4702 | mov L:CARG1d, L:RB
4708 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4709 | // Table * returned in eax (RC).
4710 | mov BASE, L:RB->base
4712 | mov [BASE+RA*8], TAB:RC
4713 | mov dword [BASE+RA*8+4], LJ_TTAB
4715 |3: // Turn 0x7ff into 0x801.
4719 | mov L:FCARG1, L:RB
4720 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4725 | ins_AND // RA = dst, RD = table const (~) (holding template table)
4727 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4729 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4730 | mov L:RB->base, BASE
4733 | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
4734 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4735 | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
4736 | // Table * returned in eax (RC).
4737 | mov BASE, L:RB->base
4739 | mov [BASE+RA*8], TAB:RC
4740 | mov dword [BASE+RA*8+4], LJ_TTAB
4743 | mov L:FCARG1, L:RB
4744 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4745 | movzx RD, PC_RD // Need to reload RD.
4751 | ins_AND // RA = dst, RD = str const (~)
4752 | mov LFUNC:RB, [BASE-8]
4753 | mov TAB:RB, LFUNC:RB->env
4754 | mov STR:RC, [KBASE+RD*4]
4758 | ins_AND // RA = src, RD = str const (~)
4759 | mov LFUNC:RB, [BASE-8]
4760 | mov TAB:RB, LFUNC:RB->env
4761 | mov STR:RC, [KBASE+RD*4]
4766 | ins_ABC // RA = dst, RB = table, RC = key
4767 | checktab RB, ->vmeta_tgetv
4768 | mov TAB:RB, [BASE+RB*8]
4773 | mov RC, dword [BASE+RC*8]
4775 | // Convert number to int and back and compare.
4778 | movsd xmm0, qword [BASE+RC*8]
4781 | ucomisd xmm0, xmm1
4784 | fld qword [BASE+RC*8]
4787 | fcomparepp // eax (RC) modified!
4791 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
4793 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4794 | jae ->vmeta_tgetv // Not in array part? Use fallback.
4796 | add RC, TAB:RB->array
4797 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4799 | // Get array slot.
4802 | mov [BASE+RA*8], RBa
4806 | mov [BASE+RA*8], RB
4807 | mov [BASE+RA*8+4], RC
4812 |2: // Check for __index if table value is nil.
4813 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4815 | mov TAB:RA, TAB:RB->metatable
4816 | test byte TAB:RA->nomm, 1<<MM_index
4817 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
4818 | movzx RA, PC_RA // Restore RA.
4820 | mov dword [BASE+RA*8+4], LJ_TNIL
4824 | checkstr RC, ->vmeta_tgetv
4825 | mov STR:RC, [BASE+RC*8]
4829 | ins_ABC // RA = dst, RB = table, RC = str const (~)
4831 | mov STR:RC, [KBASE+RC*4]
4832 | checktab RB, ->vmeta_tgets
4833 | mov TAB:RB, [BASE+RB*8]
4834 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4835 | mov RA, TAB:RB->hmask
4836 | and RA, STR:RC->hash
4838 | add NODE:RA, TAB:RB->node
4840 | cmp dword NODE:RA->key.it, LJ_TSTR
4842 | cmp dword NODE:RA->key.gcr, STR:RC
4844 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4845 | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4846 | je >5 // Key found, but nil value?
4848 | // Get node value.
4851 | mov [BASE+RC*8], RBa
4855 | mov [BASE+RC*8], RB
4856 | mov [BASE+RC*8+4], RA
4863 | mov dword [BASE+RC*8+4], LJ_TNIL
4866 |4: // Follow hash chain.
4867 | mov NODE:RA, NODE:RA->next
4868 | test NODE:RA, NODE:RA
4870 | // End of hash chain: key not found, nil result.
4872 |5: // Check for __index if table value is nil.
4873 | mov TAB:RA, TAB:RB->metatable
4874 | test TAB:RA, TAB:RA
4875 | jz <3 // No metatable: done.
4876 | test byte TAB:RA->nomm, 1<<MM_index
4877 | jnz <3 // 'no __index' flag set: done.
4878 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
4881 | ins_ABC // RA = dst, RB = table, RC = byte literal
4882 | checktab RB, ->vmeta_tgetb
4883 | mov TAB:RB, [BASE+RB*8]
4884 | cmp RC, TAB:RB->asize
4887 | add RC, TAB:RB->array
4888 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4890 | // Get array slot.
4893 | mov [BASE+RA*8], RBa
4897 | mov [BASE+RA*8], RB
4898 | mov [BASE+RA*8+4], RC
4903 |2: // Check for __index if table value is nil.
4904 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4906 | mov TAB:RA, TAB:RB->metatable
4907 | test byte TAB:RA->nomm, 1<<MM_index
4908 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
4909 | movzx RA, PC_RA // Restore RA.
4911 | mov dword [BASE+RA*8+4], LJ_TNIL
4916 | ins_ABC // RA = src, RB = table, RC = key
4917 | checktab RB, ->vmeta_tsetv
4918 | mov TAB:RB, [BASE+RB*8]
4923 | mov RC, dword [BASE+RC*8]
4925 | // Convert number to int and back and compare.
4928 | movsd xmm0, qword [BASE+RC*8]
4931 | ucomisd xmm0, xmm1
4934 | fld qword [BASE+RC*8]
4937 | fcomparepp // eax (RC) modified!
4941 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
4943 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4946 | add RC, TAB:RB->array
4947 | cmp dword [RC+4], LJ_TNIL
4948 | je >3 // Previous value is nil?
4950 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4952 |2: // Set array slot.
4954 | mov RBa, [BASE+RA*8]
4957 | mov RB, [BASE+RA*8+4]
4958 | mov RA, [BASE+RA*8]
4964 |3: // Check for __newindex if previous value is nil.
4965 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4967 | mov TAB:RA, TAB:RB->metatable
4968 | test byte TAB:RA->nomm, 1<<MM_newindex
4969 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
4970 | movzx RA, PC_RA // Restore RA.
4974 | checkstr RC, ->vmeta_tsetv
4975 | mov STR:RC, [BASE+RC*8]
4978 |7: // Possible table write barrier for the value. Skip valiswhite check.
4979 | barrierback TAB:RB, RA
4980 | movzx RA, PC_RA // Restore RA.
4984 | ins_ABC // RA = src, RB = table, RC = str const (~)
4986 | mov STR:RC, [KBASE+RC*4]
4987 | checktab RB, ->vmeta_tsets
4988 | mov TAB:RB, [BASE+RB*8]
4989 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4990 | mov RA, TAB:RB->hmask
4991 | and RA, STR:RC->hash
4993 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
4994 | add NODE:RA, TAB:RB->node
4996 | cmp dword NODE:RA->key.it, LJ_TSTR
4998 | cmp dword NODE:RA->key.gcr, STR:RC
5000 | // Ok, key found. Assumes: offsetof(Node, val) == 0
5001 | cmp dword [RA+4], LJ_TNIL
5002 | je >4 // Previous value is nil?
5004 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5006 |3: // Set node value.
5009 | mov RBa, [BASE+RC*8]
5012 | mov RB, [BASE+RC*8+4]
5013 | mov RC, [BASE+RC*8]
5019 |4: // Check for __newindex if previous value is nil.
5020 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5022 | mov TMP1, RA // Save RA.
5023 | mov TAB:RA, TAB:RB->metatable
5024 | test byte TAB:RA->nomm, 1<<MM_newindex
5025 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5026 | mov RA, TMP1 // Restore RA.
5029 |5: // Follow hash chain.
5030 | mov NODE:RA, NODE:RA->next
5031 | test NODE:RA, NODE:RA
5033 | // End of hash chain: key not found, add a new one.
5035 | // But check for __newindex first.
5036 | mov TAB:RA, TAB:RB->metatable
5037 | test TAB:RA, TAB:RA
5038 | jz >6 // No metatable: continue.
5039 | test byte TAB:RA->nomm, 1<<MM_newindex
5040 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
5044 | mov TMP3, TAB:RB // Save TAB:RB for us.
5046 | mov L:CARG1d, SAVE_L
5047 | mov L:CARG1d->base, BASE
5049 | mov CARG2d, TAB:RB
5050 | mov L:RB, L:CARG1d
5052 | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
5057 | mov L:RB->base, BASE
5060 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
5061 | // Handles write barrier for the new key. TValue * returned in eax (RC).
5062 | mov BASE, L:RB->base
5063 | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
5065 | jmp <2 // Must check write barrier for value.
5067 |7: // Possible table write barrier for the value. Skip valiswhite check.
5068 | barrierback TAB:RB, RC // Destroys STR:RC.
5072 | ins_ABC // RA = src, RB = table, RC = byte literal
5073 | checktab RB, ->vmeta_tsetb
5074 | mov TAB:RB, [BASE+RB*8]
5075 | cmp RC, TAB:RB->asize
5078 | add RC, TAB:RB->array
5079 | cmp dword [RC+4], LJ_TNIL
5080 | je >3 // Previous value is nil?
5082 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5084 |2: // Set array slot.
5086 | mov RAa, [BASE+RA*8]
5089 | mov RB, [BASE+RA*8+4]
5090 | mov RA, [BASE+RA*8]
5096 |3: // Check for __newindex if previous value is nil.
5097 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
5099 | mov TAB:RA, TAB:RB->metatable
5100 | test byte TAB:RA->nomm, 1<<MM_newindex
5101 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
5102 | movzx RA, PC_RA // Restore RA.
5105 |7: // Possible table write barrier for the value. Skip valiswhite check.
5106 | barrierback TAB:RB, RA
5107 | movzx RA, PC_RA // Restore RA.
5112 | ins_AD // RA = base (table at base-1), RD = num const (start index)
5113 | mov TMP1, KBASE // Need one more free register.
5114 | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
5116 | lea RA, [BASE+RA*8]
5117 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
5118 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
5123 | jz >4 // Nothing to copy?
5124 | add RD, KBASE // Compute needed size.
5125 | cmp RD, TAB:RB->asize
5126 | ja >5 // Doesn't fit into array part?
5129 | add KBASE, TAB:RB->array
5130 |3: // Copy result slots to table.
5149 |5: // Need to resize array part.
5151 | mov L:CARG1d, SAVE_L
5152 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
5153 | mov CARG2d, TAB:RB
5155 | mov L:RB, L:CARG1d
5159 | mov L:RB->base, BASE
5164 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
5165 | mov BASE, L:RB->base
5166 | movzx RA, PC_RA // Restore RA.
5169 |7: // Possible table write barrier for any value. Skip valiswhite check.
5170 | barrierback TAB:RB, RD
5174 /* -- Calls and vararg handling ----------------------------------------- */
5176 case BC_CALL: case BC_CALLM:
5177 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
5178 if (op == BC_CALLM) {
5179 | add NARGS:RD, MULTRES
5181 | cmp dword [BASE+RA*8+4], LJ_TFUNC
5182 | mov LFUNC:RB, [BASE+RA*8]
5183 | jne ->vmeta_call_ra
5184 | lea BASE, [BASE+RA*8+8]
5189 | ins_AD // RA = base, RD = extra_nargs
5190 | add NARGS:RD, MULTRES
5191 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
5194 | ins_AD // RA = base, RD = nargs+1
5195 | lea RA, [BASE+RA*8+8]
5196 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
5197 | mov LFUNC:RB, [RA-8]
5198 | cmp dword [RA-4], LJ_TFUNC
5202 | test PC, FRAME_TYPE
5205 | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
5206 | mov MULTRES, NARGS:RD
5209 |2: // Move args down.
5225 | mov LFUNC:RB, [BASE-8]
5227 | mov NARGS:RD, MULTRES
5228 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
5233 |5: // Tailcall to a fast function.
5234 | test PC, FRAME_TYPE // Lua frame below?
5238 | lea RA, [BASE+RA*8]
5239 | mov LFUNC:KBASE, [RA-8] // Need to prepare KBASE.
5240 | mov KBASE, LFUNC:KBASE->pc
5241 | mov KBASE, [KBASE+PC2PROTO(k)]
5244 |7: // Tailcall from a vararg function.
5245 | sub PC, FRAME_VARG
5246 | test PC, FRAME_TYPEP
5247 | jnz >8 // Vararg frame below?
5248 | sub BASE, PC // Need to relocate BASE/KBASE down.
5253 | add PC, FRAME_VARG
5258 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
5259 | lea RA, [BASE+RA*8+8] // fb = base+1
5261 | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
5262 | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
5266 | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
5270 | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
5275 | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
5277 | mov [RA-8], LFUNC:RB
5279 | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
5287 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
5289 | // NYI: add hotloop, record BC_ITERN.
5291 | mov TMP1, KBASE // Need two more free registers.
5292 | mov TMP2, DISPATCH
5293 | mov TAB:RB, [BASE+RA*8-16]
5294 | mov RC, [BASE+RA*8-8] // Get index from control var.
5295 | mov DISPATCH, TAB:RB->asize
5297 | mov KBASE, TAB:RB->array
5298 |1: // Traverse array part.
5299 | cmp RC, DISPATCH; jae >5 // Index points after array part?
5300 | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
5302 | mov dword [BASE+RA*8+4], LJ_TISNUM
5303 | mov dword [BASE+RA*8], RC
5307 | fild dword [BASE+RA*8-8]
5309 | // Copy array slot to returned value.
5311 | mov RBa, [KBASE+RC*8]
5312 | mov [BASE+RA*8+8], RBa
5314 | mov RB, [KBASE+RC*8+4]
5315 | mov [BASE+RA*8+12], RB
5316 | mov RB, [KBASE+RC*8]
5317 | mov [BASE+RA*8+8], RB
5320 | // Return array index as a numeric key.
5324 | movsd qword [BASE+RA*8], xmm0
5326 | fstp qword [BASE+RA*8]
5328 | mov [BASE+RA*8-8], RC // Update control var.
5330 | movzx RD, PC_RD // Get target from ITERL.
5333 | mov DISPATCH, TMP2
5337 |4: // Skip holes in array part.
5339 if (!LJ_DUALNUM && !sse) {
5340 | mov [BASE+RA*8-8], RC
5344 |5: // Traverse hash part.
5347 | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
5348 | imul KBASE, RC, #NODE
5349 | add NODE:KBASE, TAB:RB->node
5350 | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
5351 | lea DISPATCH, [RC+DISPATCH+1]
5352 | // Copy key and value from hash slot.
5354 | mov RBa, NODE:KBASE->key
5355 | mov RCa, NODE:KBASE->val
5356 | mov [BASE+RA*8], RBa
5357 | mov [BASE+RA*8+8], RCa
5359 | mov RB, NODE:KBASE->key.gcr
5360 | mov RC, NODE:KBASE->key.it
5361 | mov [BASE+RA*8], RB
5362 | mov [BASE+RA*8+4], RC
5363 | mov RB, NODE:KBASE->val.gcr
5364 | mov RC, NODE:KBASE->val.it
5365 | mov [BASE+RA*8+8], RB
5366 | mov [BASE+RA*8+12], RC
5368 | mov [BASE+RA*8-8], DISPATCH
5371 |7: // Skip holes in hash part.
5377 | ins_AD // RA = base, RD = target (points to ITERN)
5378 | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
5379 | mov CFUNC:RB, [BASE+RA*8-24]
5380 | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
5381 | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
5382 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
5384 | mov dword [BASE+RA*8-8], 0 // Initialize control var.
5387 |5: // Despecialize bytecode if any of the checks fail.
5390 | mov byte [PC], BC_ITERC
5395 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
5396 | mov TMP1, KBASE // Need one more free register.
5397 | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
5398 | lea RA, [BASE+RA*8]
5399 | sub KBASE, [BASE-4]
5400 | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
5402 | jz >5 // Copy all varargs?
5403 | lea RB, [RA+RB*8-8]
5404 | cmp KBASE, BASE // No vararg slots?
5406 |1: // Copy vararg slots to destination slots.
5408 | mov RCa, [KBASE-8]
5419 | cmp RA, RB // All destination slots filled?
5421 | cmp KBASE, BASE // No more vararg slots?
5423 |2: // Fill up remainder with nil.
5424 | mov dword [RA+4], LJ_TNIL
5432 |5: // Copy all varargs.
5433 | mov MULTRES, 1 // MULTRES = 0+1
5436 | jbe <3 // No vararg slots?
5440 | mov MULTRES, RB // MULTRES = #varargs+1
5443 | cmp RC, L:RB->maxstack
5444 | ja >7 // Need to grow stack?
5445 |6: // Copy all vararg slots.
5447 | mov RCa, [KBASE-8]
5458 | cmp KBASE, BASE // No more vararg slots?
5462 |7: // Grow stack for varargs.
5463 | mov L:RB->base, BASE
5466 | sub KBASE, BASE // Need delta, because BASE may change.
5467 | mov FCARG2, MULTRES
5470 | call extern lj_state_growstack@8 // (lua_State *L, int n)
5471 | mov BASE, L:RB->base
5477 /* -- Returns ----------------------------------------------------------- */
5480 | ins_AD // RA = results, RD = extra_nresults
5481 | add RD, MULTRES // MULTRES >=1, so RD >=1.
5482 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
5485 case BC_RET: case BC_RET0: case BC_RET1:
5486 | ins_AD // RA = results, RD = nresults+1
5487 if (op != BC_RET0) {
5492 | mov MULTRES, RD // Save nresults+1.
5493 | test PC, FRAME_TYPE // Check frame type marker.
5494 | jnz >7 // Not returning to a fixarg Lua func?
5498 | mov KBASE, BASE // Use KBASE for result move.
5501 |2: // Move results down.
5503 | mov RBa, [KBASE+RA]
5504 | mov [KBASE-8], RBa
5506 | mov RB, [KBASE+RA]
5508 | mov RB, [KBASE+RA+4]
5515 | mov RD, MULTRES // Note: MULTRES may be >255.
5516 | movzx RB, PC_RB // So cannot compare with RDL!
5518 | cmp RB, RD // More results expected?
5523 | mov RBa, [BASE+RA]
5526 | mov RB, [BASE+RA+4]
5534 | cmp PC_RB, RDL // More results expected?
5540 | not RAa // Note: ~RA = -(RA+1)
5541 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
5542 | mov LFUNC:KBASE, [BASE-8]
5543 | mov KBASE, LFUNC:KBASE->pc
5544 | mov KBASE, [KBASE+PC2PROTO(k)]
5547 |6: // Fill up results with nil.
5549 | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
5552 | mov dword [BASE+RD*8-12], LJ_TNIL
5557 |7: // Non-standard return case.
5558 | lea RB, [PC-FRAME_VARG]
5559 | test RB, FRAME_TYPEP
5561 | // Return from vararg function: relocate BASE down and RA up.
5563 if (op != BC_RET0) {
5569 /* -- Loops and branches ------------------------------------------------ */
5571 |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
5572 |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
5573 |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
5574 |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
5580 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5590 vk = (op == BC_IFORL || op == BC_JFORL);
5591 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
5592 | lea RA, [BASE+RA*8]
5594 | cmp FOR_TIDX, LJ_TISNUM; jne >9
5596 | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5597 | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5598 | mov RB, dword FOR_IDX
5599 | cmp dword FOR_STEP, 0; jl >5
5601 #ifdef LUA_USE_ASSERT
5602 | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5603 | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5605 | mov RB, dword FOR_STEP
5606 | test RB, RB; js >5
5607 | add RB, dword FOR_IDX; jo >1
5608 | mov dword FOR_IDX, RB
5610 | cmp RB, dword FOR_STOP
5611 | mov FOR_TEXT, LJ_TISNUM
5612 | mov dword FOR_EXT, RB
5613 if (op == BC_FORI) {
5618 } else if (op == BC_JFORI) {
5624 } else if (op == BC_IFORL) {
5637 |5: // Invert check for negative step.
5639 | add RB, dword FOR_IDX; jo <1
5640 | mov dword FOR_IDX, RB
5642 | cmp RB, dword FOR_STOP
5643 | mov FOR_TEXT, LJ_TISNUM
5644 | mov dword FOR_EXT, RB
5645 if (op == BC_FORI) {
5647 } else if (op == BC_JFORI) {
5651 } else if (op == BC_IFORL) {
5657 |9: // Fallback to FP variant.
5659 | cmp FOR_TIDX, LJ_TISNUM
5663 | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5665 #ifdef LUA_USE_ASSERT
5666 | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5667 | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5670 | mov RB, FOR_TSTEP // Load type/hiword of for step.
5672 | cmp RB, LJ_TISNUM; jae ->vmeta_for
5675 | movsd xmm0, qword FOR_IDX
5676 | movsd xmm1, qword FOR_STOP
5678 | addsd xmm0, qword FOR_STEP
5679 | movsd qword FOR_IDX, xmm0
5680 | test RB, RB; js >3
5684 | ucomisd xmm1, xmm0
5686 | movsd qword FOR_EXT, xmm0
5688 | fld qword FOR_STOP
5691 | fadd qword FOR_STEP // nidx = idx + step
5694 | test RB, RB; js >1
5699 | fxch // Swap lim/(n)idx if step non-negative.
5701 | fcomparepp // eax (RD) modified if !cmov.
5703 | movzx RD, PC_RD // Need to reload RD.
5706 if (op == BC_FORI) {
5713 } else if (op == BC_JFORI) {
5717 } else if (op == BC_IFORL) {
5734 |3: // Invert comparison if step is negative.
5735 | ucomisd xmm0, xmm1
5744 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5752 | ins_AJ // RA = base, RD = target
5753 | lea RA, [BASE+RA*8]
5755 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
5756 if (op == BC_JITERL) {
5762 | branchPC RD // Otherwise save control var + branch.
5772 | ins_A // RA = base, RD = target (loop extent)
5773 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5774 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5778 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5782 | ins_A // RA = base, RD = target (loop extent)
5788 | ins_AD // RA = base (ignored), RD = traceno
5789 | mov RA, [DISPATCH+DISPATCH_J(trace)]
5790 | mov TRACE:RD, [RA+RD*4]
5791 | mov RDa, TRACE:RD->mcode
5793 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5794 | mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB
5795 | // Save additional callee-save registers only used in compiled code.
5803 | movdqa [RAa], xmm6
5804 | movdqa [RAa-1*16], xmm7
5805 | movdqa [RAa-2*16], xmm8
5806 | movdqa [RAa-3*16], xmm9
5807 | movdqa [RAa-4*16], xmm10
5808 | movdqa [RAa-5*16], xmm11
5809 | movdqa [RAa-6*16], xmm12
5810 | movdqa [RAa-7*16], xmm13
5811 | movdqa [RAa-8*16], xmm14
5812 | movdqa [RAa-9*16], xmm15
5823 | ins_AJ // RA = unused, RD = target
5828 /* -- Function headers -------------------------------------------------- */
5831 ** Reminder: A function may be called with func/args above L->maxstack,
5832 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5833 ** too. This means all FUNC* ops (including fast functions) must check
5834 ** for stack overflow _before_ adding more slots!
5841 case BC_FUNCV: /* NYI: compiled vararg functions. */
5842 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5850 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5851 | mov KBASE, [PC-4+PC2PROTO(k)]
5853 | lea RA, [BASE+RA*8] // Top of frame.
5854 | cmp RA, L:RB->maxstack
5855 | ja ->vm_growstack_f
5856 | movzx RA, byte [PC-4+PC2PROTO(numparams)]
5857 | cmp NARGS:RD, RA // Check for missing parameters.
5860 if (op == BC_JFUNCF) {
5867 |3: // Clear missing parameters.
5868 | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5879 | int3 // NYI: compiled vararg functions
5880 break; /* NYI: compiled vararg functions. */
5883 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5884 | lea RB, [NARGS:RD*8+FRAME_VARG]
5885 | lea RD, [BASE+NARGS:RD*8]
5886 | mov LFUNC:KBASE, [BASE-8]
5887 | mov [RD-4], RB // Store delta + FRAME_VARG.
5888 | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
5891 | cmp RA, L:RB->maxstack
5892 | ja ->vm_growstack_v // Need to grow stack.
5895 | movzx RB, byte [PC-4+PC2PROTO(numparams)]
5898 |1: // Copy fixarg slots up to new frame.
5901 | jnb >3 // Less args than parameters?
5907 | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
5911 if (op == BC_JFUNCV) {
5915 | mov KBASE, [PC-4+PC2PROTO(k)]
5919 |3: // Clear missing parameters.
5920 | mov dword [RD+4], LJ_TNIL
5929 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
5930 | mov CFUNC:RB, [BASE-8]
5931 | mov KBASEa, CFUNC:RB->f
5933 | lea RD, [BASE+NARGS:RD*8-8]
5934 | mov L:RB->base, BASE
5935 | lea RA, [RD+8*LUA_MINSTACK]
5936 | cmp RA, L:RB->maxstack
5938 if (op == BC_FUNCC) {
5940 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5947 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5953 | ja ->vm_growstack_c // Need to grow stack.
5955 if (op == BC_FUNCC) {
5956 | call KBASEa // (lua_State *L)
5958 | // (lua_State *L, lua_CFunction f)
5959 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
5961 | set_vmstate INTERP
5962 | // nresults returned in eax (RD).
5963 | mov BASE, L:RB->base
5964 | lea RA, [BASE+RD*8]
5966 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
5967 | mov PC, [BASE-4] // Fetch PC of caller.
5971 /* ---------------------------------------------------------------------- */
5974 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5980 static int build_backend(BuildCtx *ctx)
5985 #ifdef LUAJIT_CPU_NOCMOV
5988 #if defined(LUAJIT_CPU_SSE2) || defined(LJ_TARGET_X64)
5992 dasm_growpc(Dst, BC__MAX);
5994 build_subroutines(ctx, cmov, sse);
5997 for (op = 0; op < BC__MAX; op++)
5998 build_ins(ctx, (BCOp)op, op, cmov, sse);
6003 /* Emit pseudo frame-info for all assembler functions. */
6004 static void emit_asm_debug(BuildCtx *ctx)
6006 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
6010 #define REG_SP "0x7"
6011 #define REG_RA "0x10"
6015 #define REG_SP "0x4"
6016 #define REG_RA "0x8"
6018 switch (ctx->mode) {
6020 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
6023 "\t.long .LECIE0-.LSCIE0\n"
6025 "\t.long 0xffffffff\n"
6029 "\t.sleb128 -" SZPTR "\n"
6030 "\t.byte " REG_RA "\n"
6031 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6032 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6033 "\t.align " SZPTR "\n"
6037 "\t.long .LEFDE0-.LASFDE0\n"
6039 "\t.long .Lframe0\n"
6043 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6044 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6045 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6046 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6047 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6051 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6052 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6053 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6054 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6055 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6057 "\t.align " SZPTR "\n"
6058 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
6062 "\t.long .LEFDE1-.LASFDE1\n"
6064 "\t.long .Lframe0\n"
6066 "\t.quad lj_vm_ffi_call\n"
6068 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6069 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6070 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6071 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6073 "\t.long lj_vm_ffi_call\n"
6075 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6076 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6077 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6078 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6080 "\t.align " SZPTR "\n"
6081 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
6083 #if (defined(__sun__) && defined(__svr4__)) || defined(__solaris_)
6084 fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
6086 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
6090 "\t.long .LECIE1-.LSCIE1\n"
6094 "\t.string \"zPR\"\n"
6096 "\t.sleb128 -" SZPTR "\n"
6097 "\t.byte " REG_RA "\n"
6098 "\t.uleb128 6\n" /* augmentation length */
6099 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6100 "\t.long lj_err_unwind_dwarf-.\n"
6101 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6102 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6103 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6104 "\t.align " SZPTR "\n"
6108 "\t.long .LEFDE2-.LASFDE2\n"
6110 "\t.long .LASFDE2-.Lframe1\n"
6111 "\t.long .Lbegin-.\n"
6113 "\t.uleb128 0\n" /* augmentation length */
6114 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6116 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6117 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6118 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6119 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6121 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6122 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6123 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6124 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6126 "\t.align " SZPTR "\n"
6127 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
6131 "\t.long .LECIE2-.LSCIE2\n"
6135 "\t.string \"zR\"\n"
6137 "\t.sleb128 -" SZPTR "\n"
6138 "\t.byte " REG_RA "\n"
6139 "\t.uleb128 1\n" /* augmentation length */
6140 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6141 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6142 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6143 "\t.align " SZPTR "\n"
6147 "\t.long .LEFDE3-.LASFDE3\n"
6149 "\t.long .LASFDE3-.Lframe2\n"
6150 "\t.long lj_vm_ffi_call-.\n"
6152 "\t.uleb128 0\n" /* augmentation length */
6154 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
6155 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6156 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6157 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6159 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
6160 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6161 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
6162 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
6164 "\t.align " SZPTR "\n"
6165 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
6169 fprintf(ctx->fp, "\t.section .eh_frame,\"dr\"\n");
6171 "\t.def %slj_err_unwind_dwarf; .scl 2; .type 32; .endef\n",
6175 "\t.long LECIE1-LSCIE1\n"
6179 "\t.string \"zP\"\n"
6181 "\t.sleb128 -" SZPTR "\n"
6182 "\t.byte " REG_RA "\n"
6183 "\t.uleb128 5\n" /* augmentation length */
6184 "\t.byte 0x00\n" /* absptr */
6185 "\t.long %slj_err_unwind_dwarf\n"
6186 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
6187 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
6188 "\t.align " SZPTR "\n"
6189 "LECIE1:\n\n", LJ_32 ? "_" : "");
6192 "\t.long LEFDE1-LASFDE1\n"
6194 "\t.long LASFDE1-Lframe1\n"
6195 "\t.long %slj_vm_asm_begin\n"
6197 "\t.uleb128 0\n" /* augmentation length */
6198 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
6200 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
6201 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
6202 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
6203 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
6205 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
6206 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
6207 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
6208 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
6210 "\t.align " SZPTR "\n"
6211 "LEFDE1:\n\n", LJ_32 ? "_" : "", (int)ctx->codesz, CFRAME_SIZE);
6213 /* Mental note: never let Apple design an assembler.
6214 ** Or a linker. Or a plastic case. But I digress.
6216 case BUILD_machasm: {
6221 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
6224 "\t.set L$set$x,LECIEX-LSCIEX\n"
6229 "\t.ascii \"zPR\\0\"\n"
6231 "\t.byte 128-" SZPTR "\n"
6232 "\t.byte " REG_RA "\n"
6233 "\t.byte 6\n" /* augmentation length */
6234 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
6236 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
6237 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6238 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6240 "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
6241 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6242 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
6244 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6245 "\t.align " BSZPTR "\n"
6247 for (i = 0; i < ctx->nsym; i++) {
6248 const char *name = ctx->sym[i].name;
6249 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
6250 if (size == 0) continue;
6252 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
6257 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
6258 "\t.long L$set$%d\n"
6260 "\t.long LASFDE%d-EH_frame1\n"
6263 "\t.byte 0\n" /* augmentation length */
6264 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
6266 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6267 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6268 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
6269 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
6271 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6272 "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
6273 "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
6274 "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
6276 "\t.align " BSZPTR "\n"
6278 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
6284 "\t.set L$set$y,LECIEY-LSCIEY\n"
6289 "\t.ascii \"zR\\0\"\n"
6291 "\t.byte 128-" SZPTR "\n"
6292 "\t.byte " REG_RA "\n"
6293 "\t.byte 1\n" /* augmentation length */
6295 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6296 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
6298 "\t.byte 0x1b\n" /* pcrel|sdata4 */
6299 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
6301 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
6302 "\t.align " BSZPTR "\n"
6305 "_lj_vm_ffi_call.eh:\n"
6307 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
6308 "\t.long L$set$yy\n"
6310 "\t.long LASFDEY-EH_frame2\n"
6311 "\t.long _lj_vm_ffi_call-.\n"
6313 "\t.byte 0\n" /* augmentation length */
6315 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
6316 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
6317 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
6318 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
6320 "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
6321 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
6322 "\t.byte 0xd\n\t.uleb128 0x4\n" /* def_cfa_register ebp */
6323 "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
6325 "\t.align " BSZPTR "\n"
6326 "LEFDEY:\n\n", fcsize);
6330 fprintf(ctx->fp, "\t.subsections_via_symbols\n");
6333 "\t.non_lazy_symbol_pointer\n"
6334 "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
6335 ".indirect_symbol _lj_err_unwind_dwarf\n"
6340 default: /* Difficult for other modes. */