1 |// Low-level VM code for x86 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2023 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 |//-----------------------------------------------------------------------
26 |// Fixed register assignments for the interpreter.
27 |// This is very fragile and has many dependencies. Caveat emptor.
28 |.define BASE, edx // Not C callee-save, refetched anyway.
30 |.define KBASE, edi // Must be C callee-save.
31 |.define KBASEa, KBASE
32 |.define PC, esi // Must be C callee-save.
34 |.define DISPATCH, ebx // Must be C callee-save.
36 |.define KBASE, edi // Must be C callee-save.
38 |.define PC, esi // Must be C callee-save.
40 |.define DISPATCH, ebx // Must be C callee-save.
42 |.define KBASE, r15d // Must be C callee-save.
44 |.define PC, ebx // Must be C callee-save.
46 |.define DISPATCH, r14d // Must be C callee-save.
52 |.define RB, ebp // Must be ebp (C callee-save).
53 |.define RC, eax // Must be eax.
74 |.define FCARG1, ecx // x86 fastcall arguments.
77 |.define CARG1, rcx // x64/WIN64 C call arguments.
85 |.define FCARG1, CARG1d // Upwards compatible to x86 fastcall.
86 |.define FCARG2, CARG2d
88 |.define CARG1, rdi // x64/POSIX C call arguments.
100 |.define FCARG1, CARG1d // Simulate x86 fastcall.
101 |.define FCARG2, CARG2d
104 |// Type definitions. Some of these are only used for documentation.
106 |.type GL, global_State
107 |.type TVALUE, TValue
111 |.type LFUNC, GCfuncL
112 |.type CFUNC, GCfuncC
113 |.type PROTO, GCproto
114 |.type UPVAL, GCupval
117 |.type TRACE, GCtrace
120 |// Stack layout while in interpreter. Must match with lj_frame.h.
121 |//-----------------------------------------------------------------------
122 |.if not X64 // x86 stack layout.
126 |.define CFRAME_SPACE, aword*9 // Delta for esp (see <--).
128 | push edi; push esi; push ebx
129 | push extern lj_err_unwind_win
132 | sub esp, CFRAME_SPACE
135 | add esp, CFRAME_SPACE
137 | pop edi // Short for esp += 4.
138 | pop ebx; pop esi; pop edi; pop ebp
143 |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
145 | push edi; push esi; push ebx
146 | sub esp, CFRAME_SPACE
149 | add esp, CFRAME_SPACE
150 | pop ebx; pop esi; pop edi; pop ebp
156 | push ebp; saveregs_
160 |.define SAVE_ERRF, aword [esp+aword*19] // vm_pcall/vm_cpcall only.
161 |.define SAVE_NRES, aword [esp+aword*18]
162 |.define SAVE_CFRAME, aword [esp+aword*17]
163 |.define SAVE_L, aword [esp+aword*16]
164 |//----- 16 byte aligned, ^^^ arguments from C caller
165 |.define SAVE_RET, aword [esp+aword*15] //<-- esp entering interpreter.
166 |.define SAVE_R4, aword [esp+aword*14]
167 |.define SAVE_R3, aword [esp+aword*13]
168 |.define SAVE_R2, aword [esp+aword*12]
169 |//----- 16 byte aligned
170 |.define SAVE_R1, aword [esp+aword*11]
171 |.define SEH_FUNC, aword [esp+aword*10]
172 |.define SEH_NEXT, aword [esp+aword*9] //<-- esp after register saves.
173 |.define UNUSED2, aword [esp+aword*8]
174 |//----- 16 byte aligned
175 |.define UNUSED1, aword [esp+aword*7]
176 |.define SAVE_PC, aword [esp+aword*6]
177 |.define TMP2, aword [esp+aword*5]
178 |.define TMP1, aword [esp+aword*4]
179 |//----- 16 byte aligned
180 |.define ARG4, aword [esp+aword*3]
181 |.define ARG3, aword [esp+aword*2]
182 |.define ARG2, aword [esp+aword*1]
183 |.define ARG1, aword [esp] //<-- esp while in interpreter.
184 |//----- 16 byte aligned, ^^^ arguments for C callee
186 |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
187 |.define SAVE_NRES, aword [esp+aword*14]
188 |.define SAVE_CFRAME, aword [esp+aword*13]
189 |.define SAVE_L, aword [esp+aword*12]
190 |//----- 16 byte aligned, ^^^ arguments from C caller
191 |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
192 |.define SAVE_R4, aword [esp+aword*10]
193 |.define SAVE_R3, aword [esp+aword*9]
194 |.define SAVE_R2, aword [esp+aword*8]
195 |//----- 16 byte aligned
196 |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
197 |.define SAVE_PC, aword [esp+aword*6]
198 |.define TMP2, aword [esp+aword*5]
199 |.define TMP1, aword [esp+aword*4]
200 |//----- 16 byte aligned
201 |.define ARG4, aword [esp+aword*3]
202 |.define ARG3, aword [esp+aword*2]
203 |.define ARG2, aword [esp+aword*1]
204 |.define ARG1, aword [esp] //<-- esp while in interpreter.
205 |//----- 16 byte aligned, ^^^ arguments for C callee
208 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
209 |.define FPARG3, qword [esp+qword*1]
210 |.define FPARG1, qword [esp]
211 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
212 |.define TMPQ, qword [esp+aword*4]
216 |.define MULTRES, TMP2
218 |// Arguments for vm_call and vm_pcall.
219 |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
221 |// Arguments for vm_cpcall.
222 |.define INARG_CP_CALL, SAVE_ERRF
223 |.define INARG_CP_UD, SAVE_NRES
224 |.define INARG_CP_FUNC, SAVE_CFRAME
226 |//-----------------------------------------------------------------------
227 |.elif X64WIN // x64/Windows stack layout
229 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
231 | push rdi; push rsi; push rbx
232 | sub rsp, CFRAME_SPACE
235 | push rbp; saveregs_
238 | add rsp, CFRAME_SPACE
239 | pop rbx; pop rsi; pop rdi; pop rbp
242 |.define SAVE_CFRAME, aword [rsp+aword*13]
243 |.define SAVE_PC, dword [rsp+dword*25]
244 |.define SAVE_L, dword [rsp+dword*24]
245 |.define SAVE_ERRF, dword [rsp+dword*23]
246 |.define SAVE_NRES, dword [rsp+dword*22]
247 |.define TMP2, dword [rsp+dword*21]
248 |.define TMP1, dword [rsp+dword*20]
249 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
250 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
251 |.define SAVE_R4, aword [rsp+aword*8]
252 |.define SAVE_R3, aword [rsp+aword*7]
253 |.define SAVE_R2, aword [rsp+aword*6]
254 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
255 |.define ARG5, aword [rsp+aword*4]
256 |.define CSAVE_4, aword [rsp+aword*3]
257 |.define CSAVE_3, aword [rsp+aword*2]
258 |.define CSAVE_2, aword [rsp+aword*1]
259 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
260 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
262 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
263 |.define TMPQ, qword [rsp+aword*10]
264 |.define MULTRES, TMP2
266 |.define ARG5d, dword [rsp+aword*4]
269 |//-----------------------------------------------------------------------
270 |.else // x64/POSIX stack layout
272 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
274 | push rbx; push r15; push r14
278 | sub rsp, CFRAME_SPACE
281 | push rbp; saveregs_
284 | add rsp, CFRAME_SPACE
288 | pop r14; pop r15; pop rbx; pop rbp
291 |//----- 16 byte aligned,
293 |.define SAVE_RET, aword [rsp+aword*11] //<-- rsp entering interpreter.
294 |.define SAVE_R4, aword [rsp+aword*10]
295 |.define SAVE_R3, aword [rsp+aword*9]
296 |.define SAVE_R2, aword [rsp+aword*8]
297 |.define SAVE_R1, aword [rsp+aword*7]
298 |.define SAVE_RU2, aword [rsp+aword*6]
299 |.define SAVE_RU1, aword [rsp+aword*5] //<-- rsp after register saves.
301 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
302 |.define SAVE_R4, aword [rsp+aword*8]
303 |.define SAVE_R3, aword [rsp+aword*7]
304 |.define SAVE_R2, aword [rsp+aword*6]
305 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
307 |.define SAVE_CFRAME, aword [rsp+aword*4]
308 |.define SAVE_PC, dword [rsp+dword*7]
309 |.define SAVE_L, dword [rsp+dword*6]
310 |.define SAVE_ERRF, dword [rsp+dword*5]
311 |.define SAVE_NRES, dword [rsp+dword*4]
312 |.define TMPa, aword [rsp+aword*1]
313 |.define TMP2, dword [rsp+dword*1]
314 |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
315 |//----- 16 byte aligned
317 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
318 |.define TMPQ, qword [rsp]
319 |.define TMP3, dword [rsp+aword*1]
320 |.define MULTRES, TMP2
324 |//-----------------------------------------------------------------------
326 |// Instruction headers.
327 |.macro ins_A; .endmacro
328 |.macro ins_AD; .endmacro
329 |.macro ins_AJ; .endmacro
330 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
331 |.macro ins_AB_; movzx RB, RCH; .endmacro
332 |.macro ins_A_C; movzx RC, RCL; .endmacro
333 |.macro ins_AND; not RDa; .endmacro
335 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
343 | jmp aword [DISPATCH+OP*8]
345 | jmp aword [DISPATCH+OP*4]
349 |// Instruction footer.
351 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
352 | .define ins_next, ins_NEXT
353 | .define ins_next_, ins_NEXT
355 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
356 | // Affects only certain kinds of benchmarks (and only with -j off).
357 | // Around 10%-30% slower on Core2, a lot more slower on P4.
367 |// Call decode and dispatch.
369 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
370 | mov PC, LFUNC:RB->pc
376 | jmp aword [DISPATCH+OP*8]
378 | jmp aword [DISPATCH+OP*4]
383 | // BASE = new base, RB = LFUNC, RD = nargs+1
388 |//-----------------------------------------------------------------------
390 |// Macros to test operand types.
391 |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
392 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
393 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
394 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
395 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
397 |// These operands must be used with movzx.
398 |.define PC_OP, byte [PC-4]
399 |.define PC_RA, byte [PC-3]
400 |.define PC_RB, byte [PC-1]
401 |.define PC_RC, byte [PC-2]
402 |.define PC_RD, word [PC-2]
404 |.macro branchPC, reg
405 | lea PC, [PC+reg*4-BCBIAS_J*4]
408 |// Assumes DISPATCH is relative to GL.
409 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
410 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
412 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
414 |// Decrement hashed hotcount and trigger trace recorder if zero.
418 | and reg, HOTCOUNT_PCMASK
419 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
426 | and reg, HOTCOUNT_PCMASK
427 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
431 |// Set current VM state.
432 |.macro set_vmstate, st
433 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
437 |.macro fcomparepp // Compare and pop st0 >< st1.
442 |.macro fpop1; fstp st1; .endmacro
444 |// Synthesize SSE FP constants.
445 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
447 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
449 | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
453 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
455 | mov64 tmp, U64x(val,00000000); movd reg, tmp
457 | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
461 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
462 | sseconst_hi reg, tmp, 80000000
464 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
465 | sseconst_hi reg, tmp, 3ff00000
467 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
468 | sseconst_hi reg, tmp, 43300000
470 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
471 | sseconst_hi reg, tmp, 43380000
474 |// Move table write barrier back. Overwrites reg.
475 |.macro barrierback, tab, reg
476 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
477 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
478 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
479 | mov tab->gclist, reg
482 |//-----------------------------------------------------------------------
484 /* Generate subroutines used by opcodes and other parts of the VM. */
485 /* The .code_sub section should be last to help static branch prediction. */
486 static void build_subroutines(BuildCtx *ctx)
490 |//-----------------------------------------------------------------------
491 |//-- Return handling ----------------------------------------------------
492 |//-----------------------------------------------------------------------
498 | // Return from pcall or xpcall fast func.
500 | sub BASE, PC // Restore caller base.
501 | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
502 | mov PC, [BASE-4] // Fetch PC of previous frame.
503 | // Prepending may overwrite the pcall frame, so do it at the end.
504 | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
507 | add RD, 1 // RD = nresults+1
508 | jz ->vm_unwind_yield
510 | test PC, FRAME_TYPE
511 | jz ->BC_RET_Z // Handle regular return to Lua.
514 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
516 | test PC, FRAME_TYPE
523 | neg PC // Previous base = BASE - delta.
527 |1: // Move results down.
534 | mov RB, [BASE+RA+4]
545 | mov RA, SAVE_NRES // RA = wanted nresults+1
548 | jne >6 // More/less results wanted?
551 | mov L:RB->top, BASE
554 | mov RAa, SAVE_CFRAME // Restore previous C frame.
555 | mov L:RB->cframe, RAa
556 | xor eax, eax // Ok return status for vm_pcall.
563 | jb >7 // Less results wanted?
564 | // More results wanted. Check stack size and fill up results with nil.
565 | cmp BASE, L:RB->maxstack
567 | mov dword [BASE-4], LJ_TNIL
572 |7: // Less results wanted.
574 | jz <5 // But check for LUA_MULTRET+1.
575 | sub RA, RD // Negative result!
576 | lea BASE, [BASE+RA*8] // Correct top.
579 |8: // Corner case: need to grow stack for filling up results.
580 | // This can happen if:
581 | // - A C function grows the stack (a lot).
582 | // - The GC shrinks the stack in between.
583 | // - A return back from a lua_call() with (high) nresults adjustment.
584 | mov L:RB->top, BASE // Save current top held in BASE (yes).
585 | mov MULTRES, RD // Need to fill only remainder with nil.
588 | call extern lj_state_growstack@8 // (lua_State *L, int n)
589 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
594 | jmp ->vm_unwind_c_eh
596 |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
597 | // (void *cframe, int errcode)
599 | mov eax, CARG2d // Error return status for vm_pcall.
602 | mov eax, FCARG2 // Error return status for vm_pcall.
605 | lea FCARG1, SEH_NEXT
606 | fs; mov [0], FCARG1
609 |->vm_unwind_c_eh: // Landing pad for external unwinder.
611 | mov GL:RB, L:RB->glref
612 | mov dword GL:RB->vmstate, ~LJ_VMST_C
615 |->vm_unwind_rethrow:
616 |.if X64 and not X64WIN
620 | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
623 |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
626 | and CARG1, CFRAME_RAWMASK
629 | and FCARG1, CFRAME_RAWMASK
632 | lea FCARG1, SEH_NEXT
633 | fs; mov [0], FCARG1
636 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
638 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
639 | mov RD, 1+1 // Really 1+2 results, incr. later.
640 | mov BASE, L:RB->base
641 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
642 | add DISPATCH, GG_G2DISP
643 | mov PC, [BASE-4] // Fetch PC of previous frame.
644 | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
646 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
649 |->vm_rtlunwind@16: // Thin layer around RtlUnwind.
650 | // (void *cframe, void *excptrec, void *unwinder, int errcode)
651 | mov [esp], FCARG1 // Return value for RtlUnwind.
652 | push FCARG2 // Exception record for RtlUnwind.
653 | push 0 // Ignored by RtlUnwind.
654 | push dword [FCARG1+CFRAME_OFS_SEH]
655 | call extern RtlUnwind@16 // Violates ABI (clobbers too much).
657 | mov FCARG2, [esp+4] // errcode (for vm_unwind_c).
658 | ret // Jump to unwinder.
661 |//-----------------------------------------------------------------------
662 |//-- Grow stack for calls -----------------------------------------------
663 |//-----------------------------------------------------------------------
665 |->vm_growstack_c: // Grow stack for C function.
666 | mov FCARG2, LUA_MINSTACK
669 |->vm_growstack_v: // Grow stack for vararg Lua function.
673 |->vm_growstack_f: // Grow stack for fixarg Lua function.
674 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
675 | lea RD, [BASE+NARGS:RD*8-8]
677 | movzx RA, byte [PC-4+PC2PROTO(framesize)]
678 | add PC, 4 // Must point after first instruction.
679 | mov L:RB->base, BASE
684 | // RB = L, L->base = new base, L->top = top
686 | call extern lj_state_growstack@8 // (lua_State *L, int n)
687 | mov BASE, L:RB->base
689 | mov LFUNC:RB, [BASE-8]
693 | // BASE = new base, RB = LFUNC, RD = nargs+1
694 | ins_callt // Just retry the call.
696 |//-----------------------------------------------------------------------
697 |//-- Entry points into the assembler VM ---------------------------------
698 |//-----------------------------------------------------------------------
700 |->vm_resume: // Setup C frame and resume thread.
701 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
704 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
709 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
713 | lea KBASEa, [esp+CFRAME_RESUME]
714 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
715 | add DISPATCH, GG_G2DISP
716 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
717 | mov SAVE_CFRAME, RDa
722 | mov L:RB->cframe, KBASEa
723 | cmp byte L:RB->status, RDL
724 | je >2 // Initial resume (like a call).
726 | // Resume after yield (like a return).
727 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
729 | mov byte L:RB->status, RDL
730 | mov BASE, L:RB->base
734 | add RD, 1 // RD = nresults+1
735 | sub RA, BASE // RA = resultofs
738 | test PC, FRAME_TYPE
742 |->vm_pcall: // Setup protected C frame and enter VM.
743 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
747 | mov SAVE_ERRF, CARG4d
751 |->vm_call: // Setup C frame and enter VM.
752 | // (lua_State *L, TValue *base, int nres1)
756 |1: // Entry point for vm_pcall above (PC = ftype).
758 | mov SAVE_NRES, CARG3d
759 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
764 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
767 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
768 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
769 | mov SAVE_CFRAME, KBASEa
770 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
771 | add DISPATCH, GG_G2DISP
773 | mov L:RB->cframe, rsp
775 | mov L:RB->cframe, esp
778 |2: // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype).
779 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
781 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
783 | sub PC, BASE // PC = frame delta + frame type
788 | add NARGS:RD, 1 // RD = nargs+1
791 | mov LFUNC:RB, [RA-8]
792 | cmp dword [RA-4], LJ_TFUNC
793 | jne ->vmeta_call // Ensure KBASE defined and != BASE.
795 |->vm_call_dispatch_f:
798 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
800 |->vm_cpcall: // Setup protected C frame, call C.
801 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
804 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
808 | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
809 | mov RC, INARG_CP_UD // Get args before they are overwritten.
810 | mov RA, INARG_CP_FUNC
811 | mov BASE, INARG_CP_CALL
813 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
815 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
816 | sub KBASE, L:RB->top
817 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
818 | mov SAVE_ERRF, 0 // No error function.
819 | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
820 | add DISPATCH, GG_G2DISP
821 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
824 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
825 | mov SAVE_CFRAME, KBASEa
826 | mov L:RB->cframe, rsp
827 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
829 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
831 | mov ARG3, RC // Have to copy args downwards.
835 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
836 | mov SAVE_CFRAME, KBASE
837 | mov L:RB->cframe, esp
838 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
840 | call BASE // (lua_State *L, lua_CFunction func, void *ud)
842 | // TValue * (new base) or NULL returned in eax (RC).
844 | jz ->vm_leave_cp // No base? Just remove C frame.
847 | jmp <2 // Else continue with the call.
849 |//-----------------------------------------------------------------------
850 |//-- Metamethod handling ------------------------------------------------
851 |//-----------------------------------------------------------------------
853 |//-- Continuation dispatch ----------------------------------------------
856 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
860 | sub BASE, PC // Restore caller BASE.
861 | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
862 | mov RC, RA // ... in [RC]
863 | mov PC, [RB-12] // Restore PC from [cont|PC].
865 | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
870 | lea KBASEa, qword [=>0]
873 | mov RA, dword [RB-16]
879 | mov LFUNC:KBASE, [BASE-8]
880 | mov KBASE, LFUNC:KBASE->pc
881 | mov KBASE, [KBASE+PC2PROTO(k)]
882 | // BASE = base, RC = result, RB = meta base
883 | jmp RAa // Jump to continuation.
887 | je ->cont_ffi_callback // cont = 1: return from FFI callback.
888 | // cont = 0: Tail call from C function.
895 |->cont_cat: // BASE = base, RC = result, RB = mbase
898 | lea RA, [BASE+RA*8]
905 | mov L:CARG1d, SAVE_L
906 | mov L:CARG1d->base, BASE
911 | mov L:CARG1d, SAVE_L
912 | mov L:CARG1d->base, BASE
927 |//-- Table indexing metamethods -----------------------------------------
930 | mov TMP1, RC // RC = GCstr *
932 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
935 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
936 | mov [RA], TAB:RB // RB = GCtab *
937 | mov dword [RA+4], LJ_TTAB
944 | mov TMP2, LJ_TISNUM
950 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
954 | movzx RC, PC_RC // Reload TValue *k from RC.
955 | lea RC, [BASE+RC*8]
957 | movzx RB, PC_RB // Reload TValue *t from RB.
958 | lea RB, [BASE+RB*8]
961 | mov L:CARG1d, SAVE_L
962 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
964 | mov CARG3, RCa // May be 64 bit ptr to stack.
971 | mov L:RB->base, BASE
974 | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
975 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
976 | mov BASE, L:RB->base
979 |->cont_ra: // BASE = base, RC = result
983 | mov [BASE+RA*8], RBa
987 | mov [BASE+RA*8+4], RB
988 | mov [BASE+RA*8], RC
992 |3: // Call __index metamethod.
993 | // BASE = base, L->top = new base, stack = cont/func/t/k
995 | mov [RA-12], PC // [cont|PC]
996 | lea PC, [RA+FRAME_CONT]
998 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
999 | mov NARGS:RD, 2+1 // 2 args for func(t, k).
1000 | jmp ->vm_call_dispatch_f
1003 | mov FCARG1, TAB:RB
1004 | mov RB, BASE // Save BASE.
1005 | mov FCARG2, RC // Caveat: FCARG2 == BASE
1006 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1007 | // cTValue * or NULL returned in eax (RC).
1009 | mov BASE, RB // Restore BASE.
1012 | mov dword [BASE+RA*8+4], LJ_TNIL
1015 |//-----------------------------------------------------------------------
1018 | mov TMP1, RC // RC = GCstr *
1020 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
1021 | cmp PC_OP, BC_GSET
1023 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
1024 | mov [RA], TAB:RB // RB = GCtab *
1025 | mov dword [RA+4], LJ_TTAB
1032 | mov TMP2, LJ_TISNUM
1038 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
1042 | movzx RC, PC_RC // Reload TValue *k from RC.
1043 | lea RC, [BASE+RC*8]
1045 | movzx RB, PC_RB // Reload TValue *t from RB.
1046 | lea RB, [BASE+RB*8]
1049 | mov L:CARG1d, SAVE_L
1050 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1052 | mov CARG3, RCa // May be 64 bit ptr to stack.
1053 | mov L:RB, L:CARG1d
1059 | mov L:RB->base, BASE
1062 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
1063 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
1064 | mov BASE, L:RB->base
1067 | // NOBARRIER: lj_meta_tset ensures the table is not black.
1070 | mov RBa, [BASE+RA*8]
1073 | mov RB, [BASE+RA*8+4]
1074 | mov RA, [BASE+RA*8]
1078 |->cont_nop: // BASE = base, (RC = result)
1081 |3: // Call __newindex metamethod.
1082 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
1084 | mov [RA-12], PC // [cont|PC]
1086 | // Copy value to third argument.
1088 | mov RBa, [BASE+RC*8]
1091 | mov RB, [BASE+RC*8+4]
1092 | mov RC, [BASE+RC*8]
1096 | lea PC, [RA+FRAME_CONT]
1098 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
1099 | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
1100 | jmp ->vm_call_dispatch_f
1104 | mov L:CARG1d, SAVE_L
1106 | mov L:CARG1d->base, BASE
1107 | xchg CARG2d, TAB:RB // Caveat: CARG2d == BASE.
1109 | mov L:CARG1d, SAVE_L
1110 | mov CARG2d, TAB:RB
1111 | mov L:CARG1d->base, BASE
1112 | mov RB, BASE // Save BASE.
1113 | mov CARG3d, RC // Caveat: CARG3d == BASE.
1117 | mov RB, BASE // Save BASE.
1120 | mov L:RA->base, BASE
1123 | call extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
1124 | // TValue * returned in eax (RC).
1126 | mov BASE, RB // Restore BASE.
1129 |//-- Comparison metamethods ---------------------------------------------
1134 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
1136 | lea CARG3d, [BASE+RD*8]
1137 | lea CARG2d, [BASE+RA*8]
1139 | lea CARG2d, [BASE+RA*8]
1140 | lea CARG3d, [BASE+RD*8]
1142 | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
1143 | movzx CARG4d, PC_OP
1146 | lea RD, [BASE+RD*8]
1147 | lea RA, [BASE+RA*8]
1153 | mov L:RB->base, BASE
1156 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1157 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1159 | mov BASE, L:RB->base
1171 |->cont_condt: // BASE = base, RC = result
1173 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
1177 |->cont_condf: // BASE = base, RC = result
1178 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
1187 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1189 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1192 | mov CARG4d, RB // Caveat: CARG4d == RA.
1194 | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
1203 | mov L:RB->base, BASE
1206 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1207 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1214 | mov L:RB->base, BASE
1216 | mov FCARG2, dword [PC-4]
1218 | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
1219 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1226 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1228 | movzx CARG3d, PC_RD
1229 | mov L:CARG1d, L:RB
1236 | mov L:RB->base, BASE
1239 | call extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
1240 | mov BASE, L:RB->base
1243 |//-- Arithmetic metamethods ---------------------------------------------
1250 | lea RC, [KBASE+RC*8]
1258 | lea RC, [KBASE+RC*8]
1259 | lea RB, [BASE+RB*8]
1264 | lea RC, [BASE+RD*8]
1273 | lea RC, [BASE+RC*8]
1275 | lea RB, [BASE+RB*8]
1277 | lea RA, [BASE+RA*8]
1284 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1286 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1288 | movzx CARG5d, PC_OP
1290 | mov CARG4d, RC // Caveat: CARG4d == RA.
1291 | mov L:CARG1d, SAVE_L
1292 | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
1294 | mov L:RB, L:CARG1d
1303 | mov L:RB->base, BASE
1306 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1307 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1308 | mov BASE, L:RB->base
1312 | // Call metamethod for binary op.
1314 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1317 | mov [RA-12], PC // [cont|PC]
1318 | lea PC, [RC+FRAME_CONT]
1319 | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
1320 | jmp ->vm_call_dispatch
1324 | mov L:RB->base, BASE
1325 | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
1326 | mov L:FCARG1, L:RB
1328 | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
1329 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1330 | mov BASE, L:RB->base
1333 | jne ->vmeta_binop // Binop call for compatibility.
1335 | mov TAB:FCARG1, [BASE+RD*8]
1338 | jmp ->vmeta_binop // Binop call for compatibility.
1341 |//-- Call metamethod ----------------------------------------------------
1344 | lea RA, [BASE+RA*8+8]
1345 |->vmeta_call: // Resolve and call __call metamethod.
1346 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1347 | mov TMP2, RA // Save RA, RC for us.
1348 | mov TMP1, NARGS:RD
1352 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1354 | lea CARG3d, [RA+NARGS:RD*8]
1355 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
1357 | lea RC, [RA+NARGS:RD*8]
1362 | mov L:RB->base, BASE // This is the callers base!
1365 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1366 | mov BASE, L:RB->base
1368 | mov NARGS:RD, TMP1
1369 | mov LFUNC:RB, [RA-8]
1371 | // This is fragile. L->base must not move, KBASE must always be defined.
1373 | cmp KBASEa, rdx // Continue with CALLT if flag set.
1375 | cmp KBASE, BASE // Continue with CALLT if flag set.
1379 | ins_call // Otherwise call resolved metamethod.
1381 |//-- Argument coercion for 'for' statement ------------------------------
1385 | mov L:RB->base, BASE
1386 | mov FCARG2, RA // Caveat: FCARG2 == BASE
1387 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
1389 | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
1390 | mov BASE, L:RB->base
1396 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1398 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
1401 |//-----------------------------------------------------------------------
1402 |//-- Fast functions -----------------------------------------------------
1403 |//-----------------------------------------------------------------------
1405 |.macro .ffunc, name
1409 |.macro .ffunc_1, name
1411 | cmp NARGS:RD, 1+1; jb ->fff_fallback
1414 |.macro .ffunc_2, name
1416 | cmp NARGS:RD, 2+1; jb ->fff_fallback
1419 |.macro .ffunc_nsse, name, op
1421 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1422 | op xmm0, qword [BASE]
1425 |.macro .ffunc_nsse, name
1426 | .ffunc_nsse name, movsd
1429 |.macro .ffunc_nnsse, name
1431 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1432 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1433 | movsd xmm0, qword [BASE]
1434 | movsd xmm1, qword [BASE+8]
1437 |.macro .ffunc_nnr, name
1439 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1440 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1441 | fld qword [BASE+8]
1445 |// Inlined GC threshold check. Caveat: uses label 1.
1447 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1448 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1454 |//-- Base library: checks -----------------------------------------------
1458 | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
1497 | mov CFUNC:RB, [BASE-8]
1498 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1500 | mov dword [BASE-4], LJ_TSTR
1501 | mov [BASE-8], STR:RC
1505 | mov RC, ~LJ_TLIGHTUD
1509 |//-- Base library: getters and setters ---------------------------------
1511 |.ffunc_1 getmetatable
1514 | cmp RB, LJ_TTAB; jne >6
1515 |1: // Field metatable must be at same offset for GCtab and GCudata!
1516 | mov TAB:RB, [BASE]
1517 | mov TAB:RB, TAB:RB->metatable
1519 | test TAB:RB, TAB:RB
1520 | mov dword [BASE-4], LJ_TNIL
1522 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1523 | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
1524 | mov [BASE-8], TAB:RB
1525 | mov RA, TAB:RB->hmask
1526 | and RA, STR:RC->sid
1528 | add NODE:RA, TAB:RB->node
1529 |3: // Rearranged logic, because we expect _not_ to find the key.
1530 | cmp dword NODE:RA->key.it, LJ_TSTR
1532 | cmp dword NODE:RA->key.gcr, STR:RC
1535 | mov NODE:RA, NODE:RA->next
1536 | test NODE:RA, NODE:RA
1538 | jmp ->fff_res1 // Not found, keep default result.
1541 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1543 | mov [BASE-4], RB // Return value of mt.__metatable.
1548 | cmp RB, LJ_TUDATA; je <1
1550 | cmp RB, LJ_TNUMX; ja >8
1551 | cmp RB, LJ_TISNUM; jbe >7
1552 | mov RB, LJ_TLIGHTUD
1556 | cmp RB, LJ_TISNUM; ja >8
1561 | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1564 |.ffunc_2 setmetatable
1565 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1566 | // Fast path: no mt for table yet and not clearing the mt.
1567 | mov TAB:RB, [BASE]
1568 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1569 | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
1570 | mov TAB:RC, [BASE+8]
1571 | mov TAB:RB->metatable, TAB:RC
1573 | mov dword [BASE-4], LJ_TTAB // Return original table.
1574 | mov [BASE-8], TAB:RB
1575 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1577 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1578 | barrierback TAB:RB, RC
1583 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1585 | mov RB, BASE // Save BASE.
1586 | lea CARG3d, [BASE+8]
1587 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1588 | mov CARG1d, SAVE_L
1590 | mov RB, BASE // Save BASE.
1591 | mov CARG2d, [BASE]
1592 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1593 | mov CARG1d, SAVE_L
1595 | mov TAB:RD, [BASE]
1599 | mov RB, BASE // Save BASE.
1603 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1604 | // cTValue * returned in eax (RD).
1605 | mov BASE, RB // Restore BASE.
1606 | // Copy table slot.
1620 |//-- Base library: conversions ------------------------------------------
1623 | // Only handles the number case inline (without a base argument).
1624 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
1625 | cmp dword [BASE+4], LJ_TISNUM
1628 | mov RB, dword [BASE]; jmp ->fff_resi
1632 | jae ->fff_fallback
1634 | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1637 | // Only handles the string or number case inline.
1639 | cmp dword [BASE+4], LJ_TSTR; jne >3
1640 | // A __tostring method in the string base metatable is ignored.
1641 | mov STR:RD, [BASE]
1643 | mov dword [BASE-4], LJ_TSTR
1644 | mov [BASE-8], STR:RD
1646 |3: // Handle numbers inline, unless a number base metatable is present.
1647 | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
1648 | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1649 | jne ->fff_fallback
1650 | ffgccheck // Caveat: uses label 1.
1652 | mov L:RB->base, BASE // Add frame since C call can throw.
1653 | mov SAVE_PC, PC // Redundant (but a defined value).
1654 |.if X64 and not X64WIN
1655 | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
1657 | mov L:FCARG1, L:RB
1659 | call extern lj_strfmt_number@8 // (lua_State *L, cTValue *o)
1661 | call extern lj_strfmt_num@8 // (lua_State *L, lua_Number *np)
1663 | // GCstr returned in eax (RD).
1664 | mov BASE, L:RB->base
1667 |//-- Base library: iterators -------------------------------------------
1670 | je >2 // Missing 2nd arg?
1672 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1674 | mov RB, BASE // Save BASE.
1676 | mov CARG1d, [BASE]
1677 | lea CARG3d, [BASE-8]
1678 | lea CARG2d, [BASE+8] // Caveat: CARG2d == BASE.
1680 | mov CARG1d, [BASE]
1681 | lea CARG2d, [BASE+8]
1682 | lea CARG3d, [BASE-8] // Caveat: CARG3d == BASE.
1684 | mov TAB:RD, [BASE]
1691 | call extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1692 | // 1=found, 0=end, -1=error returned in eax (RD).
1693 | mov BASE, RB // Restore BASE.
1694 | test RD, RD; jg ->fff_res2 // Found key/value.
1695 | js ->fff_fallback_2 // Invalid key.
1696 | // End of traversal: return nil.
1697 | mov dword [BASE-4], LJ_TNIL
1699 |2: // Set missing 2nd arg to nil.
1700 | mov dword [BASE+12], LJ_TNIL
1704 | mov TAB:RB, [BASE]
1705 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1707 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1709 | mov CFUNC:RB, [BASE-8]
1710 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1712 | mov dword [BASE-4], LJ_TFUNC
1713 | mov [BASE-8], CFUNC:RD
1714 | mov dword [BASE+12], LJ_TNIL
1718 |.ffunc_2 ipairs_aux
1719 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1720 | cmp dword [BASE+12], LJ_TISNUM
1722 | jne ->fff_fallback
1724 | jae ->fff_fallback
1728 | mov RD, dword [BASE+8]
1730 | mov dword [BASE-4], LJ_TISNUM
1731 | mov dword [BASE-8], RD
1733 | movsd xmm0, qword [BASE+8]
1734 | sseconst_1 xmm1, RBa
1736 | cvttsd2si RD, xmm0
1737 | movsd qword [BASE-8], xmm0
1739 | mov TAB:RB, [BASE]
1740 | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
1742 | add RD, TAB:RB->array
1744 | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
1745 | // Copy array slot.
1758 |2: // Check for empty hash part first. Otherwise call C function.
1759 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1760 | mov FCARG1, TAB:RB
1761 | mov RB, BASE // Save BASE.
1762 | mov FCARG2, RD // Caveat: FCARG2 == BASE
1763 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1764 | // cTValue * or NULL returned in eax (RD).
1773 | mov TAB:RB, [BASE]
1774 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1776 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1778 | mov CFUNC:RB, [BASE-8]
1779 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1781 | mov dword [BASE-4], LJ_TFUNC
1782 | mov [BASE-8], CFUNC:RD
1784 | mov dword [BASE+12], LJ_TISNUM
1785 | mov dword [BASE+8], 0
1788 | movsd qword [BASE+8], xmm0
1793 |//-- Base library: catch errors ----------------------------------------
1797 | lea RA, [BASE+NARGS:RD*8]
1798 | cmp RA, L:RB->maxstack; ja ->fff_fallback
1801 | mov PC, 8+FRAME_PCALL
1803 | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1804 | shr RB, HOOK_ACTIVE_SHIFT
1806 | add PC, RB // Remember active hook before pcall.
1807 | jmp ->vm_call_dispatch
1811 | lea RA, [BASE+NARGS:RD*8]
1812 | cmp RA, L:RB->maxstack; ja ->fff_fallback
1813 | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
1814 | mov RB, [BASE+4] // Swap function and traceback.
1816 | mov dword [BASE+4], LJ_TFUNC
1817 | mov LFUNC:RB, [BASE]
1819 | mov [BASE+8], LFUNC:RB
1823 | mov PC, 16+FRAME_PCALL
1826 |//-- Coroutine library --------------------------------------------------
1828 |.macro coroutine_resume_wrap, resume
1830 |.ffunc_1 coroutine_resume
1833 |.ffunc coroutine_wrap_aux
1834 | mov CFUNC:RB, [BASE-8]
1835 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1845 | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
1847 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1848 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1850 | je >1 // Status != LUA_YIELD (i.e. 0)?
1851 | cmp RA, L:RB->base // Check for presence of initial func.
1855 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1857 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1859 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1863 | mov L:RB->base, BASE
1865 | add BASE, 8 // Keep resumed thread in stack for GC.
1867 | mov L:RB->top, BASE
1869 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1871 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1873 | sub RBa, PCa // Relative to PC.
1877 |2: // Move args to coroutine.
1900 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1906 | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
1908 | mov BASE, L:RB->base
1909 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
1910 | set_vmstate INTERP
1912 | cmp eax, LUA_YIELD
1915 | mov RA, L:PC->base
1916 | mov KBASE, L:PC->top
1917 | mov L:PC->top, RA // Clear coroutine stack.
1920 | je >6 // No results?
1923 | cmp RD, L:RB->maxstack
1924 | ja >9 // Need to grow stack?
1928 |5: // Move results from coroutine.
1943 | lea RD, [PC+2] // nresults+1 = 1 + true + results.
1944 | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
1946 | lea RD, [PC+1] // nresults+1 = 1 + results.
1956 | test PC, FRAME_TYPE
1960 |8: // Coroutine returned with error (at co->top-1).
1962 | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
1965 | mov L:PC->top, RA // Clear error from coroutine stack.
1966 | // Copy error message.
1976 | mov RD, 1+2 // nresults+1 = 1 + false + error.
1981 | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
1982 | // Error function does not return.
1985 |9: // Handle stack expansion on return from yield.
1989 | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
1991 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1994 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2000 | mov BASE, L:RB->base
2001 | jmp <4 // Retry the stack move.
2004 | coroutine_resume_wrap 1 // coroutine.resume
2005 | coroutine_resume_wrap 0 // coroutine.wrap
2007 |.ffunc coroutine_yield
2009 | test aword L:RB->cframe, CFRAME_RESUME
2011 | mov L:RB->base, BASE
2012 | lea RD, [BASE+NARGS:RD*8-8]
2015 | mov aword L:RB->cframe, RDa
2017 | mov byte L:RB->status, al
2018 | jmp ->vm_leave_unw
2020 |//-- Math library -------------------------------------------------------
2023 |->fff_resi: // Dummy.
2028 | fstp qword [BASE-8]
2033 | cmp dword [BASE+4], LJ_TISNUM; jne >2
2034 | mov RB, dword [BASE]
2035 | cmp RB, 0; jns ->fff_resi
2040 | mov dword [BASE-4], LJ_TISNUM
2041 | mov dword [BASE-8], RB
2045 | mov dword [BASE-4], 0x41e00000 // 2^31.
2046 | mov dword [BASE-8], 0
2051 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2053 | movsd xmm0, qword [BASE]
2054 | sseconst_abs xmm1, RDa
2058 | movsd qword [BASE-8], xmm0
2066 | test PC, FRAME_TYPE
2069 | cmp PC_RB, RDL // More results expected?
2071 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
2073 | not RAa // Note: ~RA = -(RA+1)
2074 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
2077 |6: // Fill up results with nil.
2078 | mov dword [BASE+RD*8-12], LJ_TNIL
2082 |7: // Non-standard return case.
2083 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
2087 |.define fff_resfp, fff_resxmm0
2089 |.define fff_resfp, fff_resn
2092 |.macro math_round, func
2093 | .ffunc math_ .. func
2095 | cmp dword [BASE+4], LJ_TISNUM; jne >1
2096 | mov RB, dword [BASE]; jmp ->fff_resi
2100 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2102 | movsd xmm0, qword [BASE]
2103 | call ->vm_ .. func .. _sse
2105 | cvttsd2si RB, xmm0
2106 | cmp RB, 0x80000000
2109 | ucomisd xmm0, xmm1
2119 |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2122 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
2123 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2124 | movsd xmm0, qword [BASE]
2126 | movsd FPARG1, xmm0
2133 |.macro math_extern, func
2134 | .ffunc_nsse math_ .. func
2136 | movsd FPARG1, xmm0
2144 |.macro math_extern2, func
2145 | .ffunc_nnsse math_ .. func
2147 | movsd FPARG1, xmm0
2148 | movsd FPARG3, xmm1
2168 | math_extern2 atan2
2171 |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
2173 |.ffunc_1 math_frexp
2175 | cmp RB, LJ_TISNUM; jae ->fff_fallback
2178 | mov [BASE-4], RB; mov [BASE-8], RC
2179 | shl RB, 1; cmp RB, 0xffe00000; jae >3
2182 | cmp RB, 0x00200000; jb >4
2184 | shr RB, 21; sub RB, RC // Extract and unbias exponent.
2187 | and RB, 0x800fffff // Mask off exponent.
2188 | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
2191 | movsd qword [BASE], xmm0
2194 |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2195 | xorps xmm0, xmm0; jmp <2
2196 |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
2197 | movsd xmm0, qword [BASE]
2198 | sseconst_hi xmm1, RBa, 43500000 // 2^54.
2200 | movsd qword [BASE-8], xmm0
2201 | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2203 |.ffunc_nsse math_modf
2206 | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
2208 | call ->vm_trunc_sse
2211 | movsd qword [BASE-8], xmm0
2212 | movsd qword [BASE], xmm4
2213 | mov RC, [BASE-4]; mov RB, [BASE+4]
2214 | xor RC, RB; js >3 // Need to adjust sign?
2219 | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
2222 | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
2224 |.macro math_minmax, name, cmovop, sseop
2227 | cmp dword [BASE+4], LJ_TISNUM
2230 | mov RB, dword [BASE]
2231 |1: // Handle integers.
2232 | cmp RA, RD; jae ->fff_resi
2233 | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2234 | cmp RB, dword [BASE+RA*8-8]
2235 | cmovop RB, dword [BASE+RA*8-8]
2240 | // Convert intermediate result to number and continue below.
2246 | jae ->fff_fallback
2249 | movsd xmm0, qword [BASE]
2250 |5: // Handle numbers or integers.
2251 | cmp RA, RD; jae ->fff_resxmm0
2252 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2256 | cvtsi2sd xmm1, dword [BASE+RA*8-8]
2259 | jae ->fff_fallback
2262 | movsd xmm1, qword [BASE+RA*8-8]
2269 | math_minmax math_min, cmovg, minsd
2270 | math_minmax math_max, cmovl, maxsd
2272 |//-- String library -----------------------------------------------------
2274 |.ffunc string_byte // Only handle the 1-arg case here.
2275 | cmp NARGS:RD, 1+1; jne ->fff_fallback
2276 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2277 | mov STR:RB, [BASE]
2279 | cmp dword STR:RB->len, 1
2280 | jb ->fff_res0 // Return no results for empty string.
2281 | movzx RB, byte STR:RB[1]
2285 | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2288 |.ffunc string_char // Only handle the 1-arg case here.
2290 | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
2291 | cmp dword [BASE+4], LJ_TISNUM
2293 | jne ->fff_fallback
2294 | mov RB, dword [BASE]
2295 | cmp RB, 255; ja ->fff_fallback
2298 | jae ->fff_fallback
2299 | cvttsd2si RB, qword [BASE]
2300 | cmp RB, 255; ja ->fff_fallback
2308 | lea RDa, TMP2 // Points to stack. Little-endian.
2311 | mov L:RB->base, BASE
2313 | mov CARG3d, TMP3 // Zero-extended to size_t.
2314 | mov CARG2, RDa // May be 64 bit ptr to stack.
2321 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
2323 | // GCstr * returned in eax (RD).
2324 | mov BASE, L:RB->base
2326 | mov dword [BASE-4], LJ_TSTR
2327 | mov [BASE-8], STR:RD
2333 | cmp NARGS:RD, 1+2; jb ->fff_fallback
2335 | cmp dword [BASE+20], LJ_TISNUM
2337 | jne ->fff_fallback
2338 | mov RB, dword [BASE+16]
2341 | jae ->fff_fallback
2342 | cvttsd2si RB, qword [BASE+16]
2346 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2347 | cmp dword [BASE+12], LJ_TISNUM
2349 | jne ->fff_fallback
2351 | jae ->fff_fallback
2353 | mov STR:RB, [BASE]
2355 | mov RB, STR:RB->len
2357 | mov RA, dword [BASE+8]
2359 | cvttsd2si RA, qword [BASE+8]
2362 | cmp RB, RC // len < end? (unsigned compare)
2365 | test RA, RA // start <= 0?
2369 | sub RC, RA // start > end?
2371 | lea RB, [STR:RB+RA+#STR-1]
2382 |5: // Negative end or overflow.
2384 | lea RC, [RC+RB+1] // end = end+(len+1)
2387 | mov RC, RB // end = len
2390 |7: // Negative start or underflow.
2392 | add RA, RB // start = start+(len+1)
2394 | jg <3 // start > 0?
2396 | mov RA, 1 // start = 1
2399 |->fff_emptystr: // Range underflow.
2400 | xor RC, RC // Zero length. Any ptr in RB is ok.
2403 |.macro ffstring_op, name
2404 | .ffunc_1 string_ .. name
2406 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2408 | lea SBUF:FCARG1, [DISPATCH+DISPATCH_GL(tmpbuf)]
2409 | mov L:RB->base, BASE
2410 | mov STR:FCARG2, [BASE] // Caveat: FCARG2 == BASE
2411 | mov RCa, SBUF:FCARG1->b
2412 | mov SBUF:FCARG1->L, L:RB
2413 | mov SBUF:FCARG1->w, RCa
2415 | call extern lj_buf_putstr_ .. name .. @8
2417 | call extern lj_buf_tostr@4
2421 |ffstring_op reverse
2425 |//-- Bit library --------------------------------------------------------
2427 |.macro .ffunc_bit, name, kind, fdef
2430 | sseconst_tobit xmm1, RBa
2432 | cmp dword [BASE+4], LJ_TISNUM
2435 | mov RB, dword [BASE]
2444 | jae ->fff_fallback
2446 | movsd xmm0, qword [BASE]
2448 | sseconst_tobit xmm1, RBa
2455 |.macro .ffunc_bit, name, kind
2456 | .ffunc_bit name, kind, .ffunc_1
2459 |.ffunc_bit bit_tobit, 0
2462 |.macro .ffunc_bit_op, name, ins
2463 | .ffunc_bit name, 2
2464 | mov TMP2, NARGS:RD // Save for fallback.
2465 | lea RD, [BASE+NARGS:RD*8-16]
2469 | cmp dword [RD+4], LJ_TISNUM
2472 | ins RB, dword [RD]
2476 | ja ->fff_fallback_bit_op
2478 | jae ->fff_fallback_bit_op
2480 | movsd xmm0, qword [RD]
2488 |.ffunc_bit_op bit_band, and
2489 |.ffunc_bit_op bit_bor, or
2490 |.ffunc_bit_op bit_bxor, xor
2492 |.ffunc_bit bit_bswap, 1
2496 |.ffunc_bit bit_bnot, 1
2506 |->fff_fallback_bit_op:
2507 | mov NARGS:RD, TMP2 // Restore for fallback
2508 | jmp ->fff_fallback
2510 |.macro .ffunc_bit_sh, name, ins
2512 | .ffunc_bit name, 1, .ffunc_2
2513 | // Note: no inline conversion from number for 2nd argument!
2514 | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2515 | mov RA, dword [BASE+8]
2518 | sseconst_tobit xmm2, RBa
2524 | ins RB, cl // Assumes RA is ecx.
2528 |.ffunc_bit_sh bit_lshift, shl
2529 |.ffunc_bit_sh bit_rshift, shr
2530 |.ffunc_bit_sh bit_arshift, sar
2531 |.ffunc_bit_sh bit_rol, rol
2532 |.ffunc_bit_sh bit_ror, ror
2534 |//-----------------------------------------------------------------------
2537 | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
2538 | jmp ->fff_fallback
2540 | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
2541 |->fff_fallback: // Call fast function fallback handler.
2542 | // BASE = new base, RD = nargs+1
2544 | mov PC, [BASE-4] // Fallback may overwrite PC.
2545 | mov SAVE_PC, PC // Redundant (but a defined value).
2546 | mov L:RB->base, BASE
2547 | lea RD, [BASE+NARGS:RD*8-8]
2548 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2550 | mov CFUNC:RD, [BASE-8]
2551 | cmp RA, L:RB->maxstack
2552 | ja >5 // Need to grow stack.
2558 | call aword CFUNC:RD->f // (lua_State *L)
2559 | mov BASE, L:RB->base
2560 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2561 | test RD, RD; jg ->fff_res // Returned nresults+1?
2567 | lea NARGS:RD, [RA+1]
2568 | mov LFUNC:RB, [BASE-8]
2569 | jne ->vm_call_tail // Returned -1?
2570 | ins_callt // Returned 0: retry fast path.
2572 |// Reconstruct previous base for vmeta_call during tailcall.
2575 | test PC, FRAME_TYPE
2578 | not RBa // Note: ~RB = -(RB+1)
2579 | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
2580 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2585 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2587 |5: // Grow stack for fallback handler.
2588 | mov FCARG2, LUA_MINSTACK
2590 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2591 | mov BASE, L:RB->base
2592 | xor RD, RD // Simulate a return 0.
2593 | jmp <1 // Dumb retry (goes through ff first).
2595 |->fff_gcstep: // Call GC step function.
2596 | // BASE = new base, RD = nargs+1
2597 | pop RBa // Must keep stack at same level.
2598 | mov TMPa, RBa // Save return address
2600 | mov SAVE_PC, PC // Redundant (but a defined value).
2601 | mov L:RB->base, BASE
2602 | lea RD, [BASE+NARGS:RD*8-8]
2605 | call extern lj_gc_step@4 // (lua_State *L)
2606 | mov BASE, L:RB->base
2612 | push RBa // Restore return address.
2615 |//-----------------------------------------------------------------------
2616 |//-- Special dispatch targets -------------------------------------------
2617 |//-----------------------------------------------------------------------
2619 |->vm_record: // Dispatch target for recording phase.
2621 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2622 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2624 | // Decrement the hookcount for consistency, but always do the call.
2625 | test RDL, HOOK_ACTIVE
2627 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2629 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2633 |->vm_rethook: // Dispatch target for return hooks.
2634 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2635 | test RDL, HOOK_ACTIVE // Hook already active?
2639 |->vm_inshook: // Dispatch target for instr/line hooks.
2640 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2641 | test RDL, HOOK_ACTIVE // Hook already active?
2644 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2646 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2648 | test RDL, LUA_MASKLINE
2652 | mov L:RB->base, BASE
2653 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2655 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2656 | call extern lj_dispatch_ins@8 // (lua_State *L, const BCIns *pc)
2658 | mov BASE, L:RB->base
2665 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2667 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
2670 |->cont_hook: // Continue from hook yield.
2673 | mov MULTRES, RA // Restore MULTRES for *M ins.
2676 |->vm_hotloop: // Hot loop counter underflow.
2678 | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
2679 | mov RB, LFUNC:RB->pc
2680 | movzx RD, byte [RB+PC2PROTO(framesize)]
2681 | lea RD, [BASE+RD*8]
2683 | mov L:RB->base, BASE
2686 | lea FCARG1, [DISPATCH+GG_DISP2J]
2687 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2689 | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
2693 |->vm_callhook: // Dispatch target for call hooks.
2699 |->vm_hotcall: // Hot call counter underflow.
2702 | or PC, 1 // Marker for hot call.
2705 | lea RD, [BASE+NARGS:RD*8-8]
2707 | mov L:RB->base, BASE
2711 | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
2712 | // ASMFunction returned in eax/rax (RDa).
2713 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2717 | mov BASE, L:RB->base
2727 |->cont_stitch: // Trace stitching.
2729 | // BASE = base, RC = result, RB = mbase
2730 | mov TRACE:RA, [RB-24] // Save previous trace.
2731 | mov TMP1, TRACE:RA
2732 | mov TMP3, DISPATCH // Need one more register.
2733 | mov DISPATCH, MULTRES
2735 | lea RA, [BASE+RA*8] // Call base.
2738 |1: // Move results down.
2756 | lea RC, [BASE+RC*8-8]
2759 | ja >9 // More results wanted?
2761 | mov DISPATCH, TMP3
2762 | mov TRACE:RD, TMP1 // Get previous trace.
2763 | movzx RB, word TRACE:RD->traceno
2764 | movzx RD, word TRACE:RD->link
2766 | je ->cont_nop // Blacklisted.
2768 | jne =>BC_JLOOP // Jump to stitched trace.
2770 | // Stitch a new trace to the previous trace.
2771 | mov [DISPATCH+DISPATCH_J(exitno)], RB
2773 | mov L:RB->base, BASE
2775 | lea FCARG1, [DISPATCH+GG_DISP2J]
2776 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2777 | call extern lj_dispatch_stitch@8 // (jit_State *J, const BCIns *pc)
2778 | mov BASE, L:RB->base
2781 |9: // Fill up results with nil.
2782 | mov dword [RA+4], LJ_TNIL
2787 |->vm_profhook: // Dispatch target for profiler hook.
2790 | mov L:RB->base, BASE
2791 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2793 | call extern lj_dispatch_profile@8 // (lua_State *L, const BCIns *pc)
2794 | mov BASE, L:RB->base
2795 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2800 |//-----------------------------------------------------------------------
2801 |//-- Trace exit handler -------------------------------------------------
2802 |//-----------------------------------------------------------------------
2804 |// Called from an exit stub with the exit number on the stack.
2805 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2809 | push r13; push r12
2810 | push r11; push r10; push r9; push r8
2811 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2812 | push rbx; push rdx; push rcx; push rax
2813 | movzx RC, byte [rbp-8] // Reconstruct exit number.
2814 | mov RCH, byte [rbp-16]
2815 | mov [rbp-8], r15; mov [rbp-16], r14
2817 | push ebp; lea ebp, [esp+12]; push ebp
2818 | push ebx; push edx; push ecx; push eax
2819 | movzx RC, byte [ebp-4] // Reconstruct exit number.
2820 | mov RCH, byte [ebp-8]
2821 | mov [ebp-4], edi; mov [ebp-8], esi
2823 | // Caveat: DISPATCH is ebx.
2824 | mov DISPATCH, [ebp]
2825 | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2827 | mov [DISPATCH+DISPATCH_J(exitno)], RC
2828 | mov [DISPATCH+DISPATCH_J(parent)], RA
2831 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2833 | sub rsp, 16*8 // Room for SSE regs.
2836 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2837 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2838 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2839 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2840 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2841 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2842 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2843 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2845 | sub esp, 8*8+16 // Room for SSE regs + args.
2846 | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2847 | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2848 | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2849 | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2851 | // Caveat: RB is ebp.
2852 | mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)]
2853 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2854 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2855 | mov L:RB->base, BASE
2857 | lea CARG2, [rsp+4*8]
2861 | lea FCARG2, [esp+16]
2863 | lea FCARG1, [DISPATCH+GG_DISP2J]
2864 | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2865 | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
2866 | // MULTRES or negated error code returned in eax (RD).
2867 | mov RAa, L:RB->cframe
2868 | and RAa, CFRAME_RAWMASK
2870 | // Reposition stack later.
2872 | mov rsp, RAa // Reposition stack to C frame.
2874 | mov esp, RAa // Reposition stack to C frame.
2876 | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
2877 | mov BASE, L:RB->base
2878 | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
2884 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2887 | // Restore additional callee-save registers only used in compiled code.
2889 | lea RAa, [rsp+9*16+4*8]
2891 | movdqa xmm15, [RAa-9*16]
2892 | movdqa xmm14, [RAa-8*16]
2893 | movdqa xmm13, [RAa-7*16]
2894 | movdqa xmm12, [RAa-6*16]
2895 | movdqa xmm11, [RAa-5*16]
2896 | movdqa xmm10, [RAa-4*16]
2897 | movdqa xmm9, [RAa-3*16]
2898 | movdqa xmm8, [RAa-2*16]
2899 | movdqa xmm7, [RAa-1*16]
2900 | mov rsp, RAa // Reposition stack to C frame.
2901 | movdqa xmm6, [RAa]
2905 | add rsp, 16 // Reposition stack to C frame.
2911 | cmp RD, -LUA_ERRERR; jae >9 // Check for error from exit.
2914 | mov LFUNC:KBASE, [BASE-8]
2915 | mov KBASE, LFUNC:KBASE->pc
2916 | mov KBASE, [KBASE+PC2PROTO(k)]
2917 | mov L:RB->base, BASE
2918 | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2919 | set_vmstate INTERP
2920 | // Modified copy of ins_next which handles function header dispatch, too.
2926 | cmp MULTRES, -17 // Static dispatch?
2928 | cmp OP, BC_FUNCF // Function header?
2930 | cmp OP, BC_FUNCC+2 // Fast function?
2933 | mov RC, MULTRES // RC/RD holds nres+1.
2936 | jmp aword [DISPATCH+OP*8]
2938 | jmp aword [DISPATCH+OP*4]
2941 |4: // Check frame below fast function.
2943 | test RC, FRAME_TYPE
2944 | jnz <2 // Trace stitching continuation?
2945 | // Otherwise set KBASE for Lua function below fast function.
2946 | movzx RC, byte [RC-3]
2948 | mov LFUNC:KBASE, [BASE+RC*8-8]
2949 | mov KBASE, LFUNC:KBASE->pc
2950 | mov KBASE, [KBASE+PC2PROTO(k)]
2953 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2954 | mov RA, [DISPATCH+DISPATCH_J(trace)]
2955 | mov TRACE:RA, [RA+RD*4]
2956 | mov RC, TRACE:RA->startins
2961 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]
2963 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC]
2966 |9: // Rethrow error from the right C frame.
2970 | call extern lj_err_trace@8 // (lua_State *L, int errcode)
2973 |//-----------------------------------------------------------------------
2974 |//-- Math helper functions ----------------------------------------------
2975 |//-----------------------------------------------------------------------
2977 |// FP value rounding. Called by math.floor/math.ceil fast functions
2978 |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
2979 |.macro vm_round, name, mode, cond
2981 |.if not X64 and cond
2982 | movsd xmm0, qword [esp+4]
2983 | call ->name .. _sse
2984 | movsd qword [esp+4], xmm0 // Overwrite callee-owned arg.
2990 | sseconst_abs xmm2, RDa
2991 | sseconst_2p52 xmm3, RDa
2993 | andpd xmm1, xmm2 // |x|
2994 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
2996 | andnpd xmm2, xmm0 // Isolate sign bit.
2997 |.if mode == 2 // trunc(x)?
2999 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3001 | sseconst_1 xmm3, RDa
3002 | cmpsd xmm0, xmm1, 1 // |x| < result?
3004 | subsd xmm1, xmm0 // If yes, subtract -1.
3005 | orpd xmm1, xmm2 // Merge sign bit back in.
3007 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
3009 | orpd xmm1, xmm2 // Merge sign bit back in.
3010 | sseconst_1 xmm3, RDa
3011 | .if mode == 1 // ceil(x)?
3012 | cmpsd xmm0, xmm1, 6 // x > result?
3014 | addsd xmm1, xmm0 // If yes, add 1.
3015 | orpd xmm1, xmm2 // Merge sign bit back in (again).
3016 | .else // floor(x)?
3017 | cmpsd xmm0, xmm1, 1 // x < result?
3019 | subsd xmm1, xmm0 // If yes, subtract 1.
3027 | vm_round vm_floor, 0, 1
3028 | vm_round vm_ceil, 1, JIT
3029 | vm_round vm_trunc, 2, JIT
3031 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
3033 |// Args in xmm0/xmm1, return value in xmm0.
3034 |// Caveat: xmm0-xmm5 and RC (eax) modified!
3037 | sseconst_abs xmm2, RDa
3038 | sseconst_2p52 xmm3, RDa
3040 | andpd xmm4, xmm2 // |x/y|
3041 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
3043 | andnpd xmm2, xmm0 // Isolate sign bit.
3044 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
3046 | orpd xmm4, xmm2 // Merge sign bit back in.
3047 | sseconst_1 xmm2, RDa
3048 | cmpsd xmm0, xmm4, 1 // x/y < result?
3050 | subsd xmm4, xmm0 // If yes, subtract 1.0.
3061 |//-----------------------------------------------------------------------
3062 |//-- Miscellaneous functions --------------------------------------------
3063 |//-----------------------------------------------------------------------
3065 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3069 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
3078 | .if X64WIN; pop rsi; .endif
3084 | xor edx, 0x00200000 // Toggle ID bit in flags.
3089 | xor eax, eax // Zero means no features supported.
3091 | jz >1 // No ID toggle means no CPUID support.
3092 | mov eax, [esp+4] // Argument 1 is function number.
3097 | mov edi, [esp+16] // Argument 2 is result area.
3108 |.define NEXT_TAB, TAB:FCARG1
3109 |.define NEXT_IDX, FCARG2
3110 |.define NEXT_PTR, RCa
3111 |.define NEXT_PTRd, RC
3112 |.macro NEXT_RES_IDXL, op2; lea edx, [NEXT_IDX+op2]; .endmacro
3114 |.define NEXT_TMP, CARG3d
3115 |.define NEXT_TMPq, CARG3
3116 |.define NEXT_ASIZE, CARG4d
3117 |.macro NEXT_ENTER; .endmacro
3118 |.macro NEXT_LEAVE; ret; .endmacro
3120 |.define NEXT_RES_PTR, [rsp+aword*5]
3121 |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro
3123 |.define NEXT_RES_PTR, [rsp+aword*1]
3124 |.macro NEXT_RES_IDX, op2; lea edx, [NEXT_IDX+op2]; .endmacro
3127 |.define NEXT_ASIZE, esi
3128 |.define NEXT_TMP, edi
3129 |.macro NEXT_ENTER; push esi; push edi; .endmacro
3130 |.macro NEXT_LEAVE; pop edi; pop esi; ret; .endmacro
3131 |.define NEXT_RES_PTR, [esp+dword*3]
3132 |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro
3135 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
3136 |// Next idx returned in edx.
3140 | mov NEXT_ASIZE, NEXT_TAB->asize
3141 |1: // Traverse array part.
3142 | cmp NEXT_IDX, NEXT_ASIZE; jae >5
3143 | mov NEXT_TMP, NEXT_TAB->array
3144 | cmp dword [NEXT_TMP+NEXT_IDX*8+4], LJ_TNIL; je >2
3145 | lea NEXT_PTR, NEXT_RES_PTR
3147 | mov NEXT_TMPq, qword [NEXT_TMP+NEXT_IDX*8]
3148 | mov qword [NEXT_PTR], NEXT_TMPq
3150 | mov NEXT_ASIZE, dword [NEXT_TMP+NEXT_IDX*8+4]
3151 | mov NEXT_TMP, dword [NEXT_TMP+NEXT_IDX*8]
3152 | mov dword [NEXT_PTR+4], NEXT_ASIZE
3153 | mov dword [NEXT_PTR], NEXT_TMP
3156 | mov dword [NEXT_PTR+dword*3], LJ_TISNUM
3157 | mov dword [NEXT_PTR+dword*2], NEXT_IDX
3159 | cvtsi2sd xmm0, NEXT_IDX
3160 | movsd qword [NEXT_PTR+dword*2], xmm0
3164 |2: // Skip holes in array part.
3168 |5: // Traverse hash part.
3169 | sub NEXT_IDX, NEXT_ASIZE
3171 | cmp NEXT_IDX, NEXT_TAB->hmask; ja >9
3172 | imul NEXT_PTRd, NEXT_IDX, #NODE
3173 | add NODE:NEXT_PTRd, dword NEXT_TAB->node
3174 | cmp dword NODE:NEXT_PTR->val.it, LJ_TNIL; je >7
3175 | NEXT_RES_IDXL NEXT_ASIZE+1
3177 |7: // Skip holes in hash part.
3181 |9: // End of iteration. Set the key to nil (not the value).
3182 | NEXT_RES_IDX NEXT_ASIZE
3183 | lea NEXT_PTR, NEXT_RES_PTR
3184 | mov dword [NEXT_PTR+dword*3], LJ_TNIL
3188 |//-----------------------------------------------------------------------
3189 |//-- Assertions ---------------------------------------------------------
3190 |//-----------------------------------------------------------------------
3192 |->assert_bad_for_arg_type:
3193 #ifdef LUA_USE_ASSERT
3198 |//-----------------------------------------------------------------------
3199 |//-- FFI helper functions -----------------------------------------------
3200 |//-----------------------------------------------------------------------
3202 |// Handler for callback functions. Callback slot number in ah/al.
3205 |.type CTSTATE, CTState, PC
3207 | sub esp, 16 // Leave room for SAVE_ERRF etc.
3209 | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
3210 | lea DISPATCH, [ebp+GG_G2DISP]
3211 | mov CTSTATE, GL:ebp->ctype_state
3213 | mov CTSTATE->cb.slot, eax
3215 | mov CTSTATE->cb.gpr[0], CARG1
3216 | mov CTSTATE->cb.gpr[1], CARG2
3217 | mov CTSTATE->cb.gpr[2], CARG3
3218 | mov CTSTATE->cb.gpr[3], CARG4
3219 | movsd qword CTSTATE->cb.fpr[0], xmm0
3220 | movsd qword CTSTATE->cb.fpr[1], xmm1
3221 | movsd qword CTSTATE->cb.fpr[2], xmm2
3222 | movsd qword CTSTATE->cb.fpr[3], xmm3
3224 | lea rax, [rsp+CFRAME_SIZE+4*8]
3226 | lea rax, [rsp+CFRAME_SIZE]
3227 | mov CTSTATE->cb.gpr[4], CARG5
3228 | mov CTSTATE->cb.gpr[5], CARG6
3229 | movsd qword CTSTATE->cb.fpr[4], xmm4
3230 | movsd qword CTSTATE->cb.fpr[5], xmm5
3231 | movsd qword CTSTATE->cb.fpr[6], xmm6
3232 | movsd qword CTSTATE->cb.fpr[7], xmm7
3234 | mov CTSTATE->cb.stack, rax
3237 | lea eax, [esp+CFRAME_SIZE+16]
3238 | mov CTSTATE->cb.gpr[0], FCARG1
3239 | mov CTSTATE->cb.gpr[1], FCARG2
3240 | mov CTSTATE->cb.stack, eax
3241 | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp.
3242 | mov FCARG2, [esp+CFRAME_SIZE+8]
3243 | mov SAVE_RET, FCARG1
3244 | mov SAVE_R4, FCARG2
3247 | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
3248 | mov FCARG1, CTSTATE
3249 | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf)
3250 | // lua_State * returned in eax (RD).
3251 | set_vmstate INTERP
3252 | mov BASE, L:RD->base
3255 | mov LFUNC:RB, [BASE-8]
3261 |->cont_ffi_callback: // Return from FFI callback.
3264 | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
3265 | mov aword CTSTATE->L, L:RAa
3266 | mov L:RA->base, BASE
3268 | mov FCARG1, CTSTATE
3270 | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o)
3272 | mov rax, CTSTATE->cb.gpr[0]
3273 | movsd xmm0, qword CTSTATE->cb.fpr[0]
3274 | jmp ->vm_leave_unw
3277 | mov eax, CTSTATE->cb.gpr[0]
3278 | mov edx, CTSTATE->cb.gpr[1]
3279 | cmp dword CTSTATE->cb.gpr[2], 1
3282 | fld qword CTSTATE->cb.fpr[0].d
3285 | fld dword CTSTATE->cb.fpr[0].f
3287 | mov ecx, L:RB->top
3288 | movzx ecx, word [ecx+6] // Get stack adjustment and copy up.
3289 | mov SAVE_L, ecx // Must be one slot above SAVE_RET
3291 | pop ecx // Move return addr from SAVE_RET.
3292 | add esp, [esp] // Adjust stack.
3299 |->vm_ffi_call@4: // Call C function via FFI.
3300 | // Caveat: needs special frame unwinding, see below.
3303 | .type CCSTATE, CCallState, rbx
3304 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3306 | .type CCSTATE, CCallState, ebx
3307 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3310 | // Readjust stack.
3312 | mov eax, CCSTATE->spadj
3315 | sub esp, CCSTATE->spadj
3317 | mov CCSTATE->spadj, esp
3321 | // Copy stack slots.
3322 | movzx ecx, byte CCSTATE->nsp
3327 | mov rax, [CCSTATE+rcx+offsetof(CCallState, stack)]
3328 | mov [rsp+rcx+CCALL_SPS_EXTRA*8], rax
3336 | mov eax, [CCSTATE+ecx+offsetof(CCallState, stack)]
3337 | mov [esp+ecx], eax
3344 | movzx eax, byte CCSTATE->nfpr
3345 | mov CARG1, CCSTATE->gpr[0]
3346 | mov CARG2, CCSTATE->gpr[1]
3347 | mov CARG3, CCSTATE->gpr[2]
3348 | mov CARG4, CCSTATE->gpr[3]
3350 | mov CARG5, CCSTATE->gpr[4]
3351 | mov CARG6, CCSTATE->gpr[5]
3353 | test eax, eax; jz >5
3354 | movaps xmm0, CCSTATE->fpr[0]
3355 | movaps xmm1, CCSTATE->fpr[1]
3356 | movaps xmm2, CCSTATE->fpr[2]
3357 | movaps xmm3, CCSTATE->fpr[3]
3359 | cmp eax, 4; jbe >5
3360 | movaps xmm4, CCSTATE->fpr[4]
3361 | movaps xmm5, CCSTATE->fpr[5]
3362 | movaps xmm6, CCSTATE->fpr[6]
3363 | movaps xmm7, CCSTATE->fpr[7]
3367 | mov FCARG1, CCSTATE->gpr[0]
3368 | mov FCARG2, CCSTATE->gpr[1]
3371 | call aword CCSTATE->func
3374 | mov CCSTATE->gpr[0], rax
3375 | movaps CCSTATE->fpr[0], xmm0
3377 | mov CCSTATE->gpr[1], rdx
3378 | movaps CCSTATE->fpr[1], xmm1
3381 | mov CCSTATE->gpr[0], eax
3382 | mov CCSTATE->gpr[1], edx
3383 | cmp byte CCSTATE->resx87, 1
3386 | fstp qword CCSTATE->fpr[0].d[0]
3389 | fstp dword CCSTATE->fpr[0].f[0]
3392 | sub CCSTATE->spadj, esp
3397 | mov rbx, [rbp-8]; leave; ret
3399 | mov ebx, [ebp-4]; leave; ret
3402 |// Note: vm_ffi_call must be the last function in this object file!
3404 |//-----------------------------------------------------------------------
3407 /* Generate the code for a single instruction. */
3408 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3411 |// Note: aligning all instructions does not pay off.
3416 /* -- Comparison ops ---------------------------------------------------- */
3418 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3420 |.macro jmp_comp, lt, ge, le, gt, target
3434 ||default: break; /* Shut up GCC. */
3438 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3439 | // RA = src1, RD = src2, JMP with RD = target
3444 | mov RB, dword [BASE+RA*8]
3446 | cmp RB, dword [BASE+RD*8]
3447 | jmp_comp jge, jl, jg, jle, >9
3454 |7: // RA is not an integer.
3456 | // RA is a number.
3457 | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3458 | // RA is a number, RD is an integer.
3459 | cvtsi2sd xmm0, dword [BASE+RD*8]
3462 |8: // RA is an integer, RD is not an integer.
3464 | // RA is an integer, RD is a number.
3465 | cvtsi2sd xmm1, dword [BASE+RA*8]
3466 | movsd xmm0, qword [BASE+RD*8]
3468 | ucomisd xmm0, xmm1
3469 | jmp_comp jbe, ja, jb, jae, <9
3472 | checknum RA, ->vmeta_comp
3473 | checknum RD, ->vmeta_comp
3476 | movsd xmm0, qword [BASE+RD*8]
3479 | ucomisd xmm0, qword [BASE+RA*8]
3481 | // Unordered: all of ZF CF PF set, ordered: PF clear.
3482 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
3484 | jmp_comp jbe, ja, jb, jae, <9
3487 | jmp_comp jbe, ja, jb, jae, >1
3495 case BC_ISEQV: case BC_ISNEV:
3496 vk = op == BC_ISEQV;
3497 | ins_AD // RA = src1, RD = src2, JMP with RD = target
3498 | mov RB, [BASE+RD*8+4]
3501 | cmp RB, LJ_TISNUM; jne >7
3503 | mov RB, dword [BASE+RD*8]
3504 | cmp RB, dword [BASE+RA*8]
3515 |7: // RD is not an integer.
3517 | // RD is a number.
3518 | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
3519 | // RD is a number, RA is an integer.
3520 | cvtsi2sd xmm0, dword [BASE+RA*8]
3523 |8: // RD is an integer, RA is not an integer.
3525 | // RD is an integer, RA is a number.
3526 | cvtsi2sd xmm0, dword [BASE+RD*8]
3527 | ucomisd xmm0, qword [BASE+RA*8]
3531 | cmp RB, LJ_TISNUM; jae >5
3535 | movsd xmm0, qword [BASE+RA*8]
3537 | ucomisd xmm0, qword [BASE+RD*8]
3541 | jp >2 // Unordered means not equal.
3544 | jp >2 // Unordered means not equal.
3549 |1: // EQ: Branch to the target.
3552 |2: // NE: Fallthrough to next instruction.
3560 |2: // NE: Branch to the target.
3563 |1: // EQ: Fallthrough to next instruction.
3565 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
3566 op == BC_ISEQN || op == BC_ISNEN)) {
3572 if (op == BC_ISEQV || op == BC_ISNEV) {
3573 |5: // Either or both types are not numbers.
3575 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
3576 | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
3578 | checktp RA, RB // Compare types.
3579 | jne <2 // Not the same type?
3581 | jae <1 // Same type and primitive type?
3583 | // Same types and not a primitive type. Compare GCobj or pvalue.
3584 | mov RA, [BASE+RA*8]
3585 | mov RD, [BASE+RD*8]
3587 | je <1 // Same GCobjs or pvalues?
3588 | cmp RB, LJ_TISTABUD
3589 | ja <2 // Different objects and not table/ud?
3591 | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
3595 | // Different tables or userdatas. Need to check __eq metamethod.
3596 | // Field metatable must be at same offset for GCtab and GCudata!
3597 | mov TAB:RB, TAB:RA->metatable
3598 | test TAB:RB, TAB:RB
3599 | jz <2 // No metatable?
3600 | test byte TAB:RB->nomm, 1<<MM_eq
3601 | jnz <2 // Or 'no __eq' flag set?
3603 | xor RB, RB // ne = 0
3605 | mov RB, 1 // ne = 1
3607 | jmp ->vmeta_equal // Handle __eq metamethod.
3612 if (LJ_DUALNUM && vk) {
3617 | jmp ->vmeta_equal_cd
3621 case BC_ISEQS: case BC_ISNES:
3622 vk = op == BC_ISEQS;
3623 | ins_AND // RA = src, RD = str const, JMP with RD = target
3624 | mov RB, [BASE+RA*8+4]
3626 | cmp RB, LJ_TSTR; jne >3
3627 | mov RA, [BASE+RA*8]
3628 | cmp RA, [KBASE+RD*4]
3636 case BC_ISEQN: case BC_ISNEN:
3637 vk = op == BC_ISEQN;
3638 | ins_AD // RA = src, RD = num const, JMP with RD = target
3639 | mov RB, [BASE+RA*8+4]
3642 | cmp RB, LJ_TISNUM; jne >7
3643 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
3644 | mov RB, dword [KBASE+RD*8]
3645 | cmp RB, dword [BASE+RA*8]
3656 |7: // RA is not an integer.
3658 | // RA is a number.
3659 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
3660 | // RA is a number, RD is an integer.
3661 | cvtsi2sd xmm0, dword [KBASE+RD*8]
3664 |8: // RA is an integer, RD is a number.
3665 | cvtsi2sd xmm0, dword [BASE+RA*8]
3666 | ucomisd xmm0, qword [KBASE+RD*8]
3669 | cmp RB, LJ_TISNUM; jae >3
3672 | movsd xmm0, qword [KBASE+RD*8]
3674 | ucomisd xmm0, qword [BASE+RA*8]
3677 case BC_ISEQP: case BC_ISNEP:
3678 vk = op == BC_ISEQP;
3679 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
3680 | mov RB, [BASE+RA*8+4]
3683 if (!LJ_HASFFI) goto iseqne_test;
3691 | cmp RB, LJ_TCDATA; jne <2
3692 | jmp ->vmeta_equal_cd
3695 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
3703 /* -- Unary test and copy ops ------------------------------------------- */
3705 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3706 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
3707 | mov RB, [BASE+RD*8+4]
3709 | cmp RB, LJ_TISTRUECOND
3710 if (op == BC_IST || op == BC_ISTC) {
3715 if (op == BC_ISTC || op == BC_ISFC) {
3716 | mov [BASE+RA*8+4], RB
3717 | mov RB, [BASE+RD*8]
3718 | mov [BASE+RA*8], RB
3722 |1: // Fallthrough to the next instruction.
3727 | ins_AD // RA = src, RD = -type
3728 | add RD, [BASE+RA*8+4]
3729 | jne ->vmeta_istype
3733 | ins_AD // RA = src, RD = -(TISNUM-1)
3734 | checknum RA, ->vmeta_istype
3738 /* -- Unary ops --------------------------------------------------------- */
3741 | ins_AD // RA = dst, RD = src
3743 | mov RBa, [BASE+RD*8]
3744 | mov [BASE+RA*8], RBa
3746 | mov RB, [BASE+RD*8+4]
3747 | mov RD, [BASE+RD*8]
3748 | mov [BASE+RA*8+4], RB
3749 | mov [BASE+RA*8], RD
3754 | ins_AD // RA = dst, RD = src
3756 | checktp RD, LJ_TISTRUECOND
3758 | mov [BASE+RA*8+4], RB
3762 | ins_AD // RA = dst, RD = src
3765 | mov RB, [BASE+RD*8]
3768 | mov dword [BASE+RA*8+4], LJ_TISNUM
3769 | mov dword [BASE+RA*8], RB
3773 | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
3774 | mov dword [BASE+RA*8], 0
3779 | checknum RD, ->vmeta_unm
3781 | movsd xmm0, qword [BASE+RD*8]
3782 | sseconst_sign xmm1, RDa
3784 | movsd qword [BASE+RA*8], xmm0
3792 | ins_AD // RA = dst, RD = src
3794 | mov STR:RD, [BASE+RD*8]
3796 | mov RD, dword STR:RD->len
3798 | mov dword [BASE+RA*8+4], LJ_TISNUM
3799 | mov dword [BASE+RA*8], RD
3802 | cvtsi2sd xmm0, dword STR:RD->len
3804 | movsd qword [BASE+RA*8], xmm0
3808 | checktab RD, ->vmeta_len
3809 | mov TAB:FCARG1, [BASE+RD*8]
3811 | mov TAB:RB, TAB:FCARG1->metatable
3817 | mov RB, BASE // Save BASE.
3818 | call extern lj_tab_len@4 // (GCtab *t)
3819 | // Length of table returned in eax (RD).
3825 | mov BASE, RB // Restore BASE.
3829 |9: // Check for __len.
3830 | test byte TAB:RB->nomm, 1<<MM_len
3832 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
3836 /* -- Binary ops -------------------------------------------------------- */
3838 |.macro ins_arithpre, sseins, ssereg
3840 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3843 | checknum RB, ->vmeta_arith_vn
3845 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
3847 | movsd xmm0, qword [BASE+RB*8]
3848 | sseins ssereg, qword [KBASE+RC*8]
3851 | checknum RB, ->vmeta_arith_nv
3853 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
3855 | movsd xmm0, qword [KBASE+RC*8]
3856 | sseins ssereg, qword [BASE+RB*8]
3859 | checknum RB, ->vmeta_arith_vv
3860 | checknum RC, ->vmeta_arith_vv
3861 | movsd xmm0, qword [BASE+RB*8]
3862 | sseins ssereg, qword [BASE+RC*8]
3867 |.macro ins_arithdn, intins
3869 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3872 | checkint RB, ->vmeta_arith_vn
3873 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
3874 | mov RB, [BASE+RB*8]
3875 | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
3878 | checkint RB, ->vmeta_arith_nv
3879 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
3880 | mov RC, [KBASE+RC*8]
3881 | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
3884 | checkint RB, ->vmeta_arith_vv
3885 | checkint RC, ->vmeta_arith_vv
3886 | mov RB, [BASE+RB*8]
3887 | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
3890 | mov dword [BASE+RA*8+4], LJ_TISNUM
3892 | mov dword [BASE+RA*8], RC
3894 | mov dword [BASE+RA*8], RB
3899 |.macro ins_arithpost
3900 | movsd qword [BASE+RA*8], xmm0
3903 |.macro ins_arith, sseins
3904 | ins_arithpre sseins, xmm0
3909 |.macro ins_arith, intins, sseins
3911 | ins_arithdn intins
3917 | // RA = dst, RB = src1 or num const, RC = src2 or num const
3918 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3919 | ins_arith add, addsd
3921 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3922 | ins_arith sub, subsd
3924 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3925 | ins_arith imul, mulsd
3927 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3931 | ins_arithpre movsd, xmm1
3937 case BC_MODNV: case BC_MODVV:
3938 | ins_arithpre movsd, xmm1
3939 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3942 | ins_arithpre movsd, xmm1
3945 | movsd FPARG1, xmm0
3946 | movsd FPARG3, xmm1
3954 | fstp qword [BASE+RA*8]
3960 | ins_ABC // RA = dst, RB = src_start, RC = src_end
3962 | mov L:CARG1d, SAVE_L
3963 | mov L:CARG1d->base, BASE
3964 | lea CARG2d, [BASE+RC*8]
3968 | mov L:RB, L:CARG1d
3970 | lea RA, [BASE+RC*8]
3977 | mov L:RB->base, BASE
3980 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3981 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
3982 | mov BASE, L:RB->base
3985 | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
3988 | mov RCa, [BASE+RB*8]
3989 | mov [BASE+RA*8], RCa
3991 | mov RC, [BASE+RB*8+4]
3992 | mov RB, [BASE+RB*8]
3993 | mov [BASE+RA*8+4], RC
3994 | mov [BASE+RA*8], RB
3999 /* -- Constant ops ------------------------------------------------------ */
4002 | ins_AND // RA = dst, RD = str const (~)
4003 | mov RD, [KBASE+RD*4]
4004 | mov dword [BASE+RA*8+4], LJ_TSTR
4005 | mov [BASE+RA*8], RD
4010 | ins_AND // RA = dst, RD = cdata const (~)
4011 | mov RD, [KBASE+RD*4]
4012 | mov dword [BASE+RA*8+4], LJ_TCDATA
4013 | mov [BASE+RA*8], RD
4018 | ins_AD // RA = dst, RD = signed int16 literal
4021 | mov dword [BASE+RA*8+4], LJ_TISNUM
4022 | mov dword [BASE+RA*8], RD
4024 | movsx RD, RDW // Sign-extend literal.
4026 | movsd qword [BASE+RA*8], xmm0
4031 | ins_AD // RA = dst, RD = num const
4032 | movsd xmm0, qword [KBASE+RD*8]
4033 | movsd qword [BASE+RA*8], xmm0
4037 | ins_AND // RA = dst, RD = primitive type (~)
4038 | mov [BASE+RA*8+4], RD
4042 | ins_AD // RA = dst_start, RD = dst_end
4043 | lea RA, [BASE+RA*8+12]
4044 | lea RD, [BASE+RD*8+4]
4046 | mov [RA-8], RB // Sets minimum 2 slots.
4055 /* -- Upvalue and function ops ------------------------------------------ */
4058 | ins_AD // RA = dst, RD = upvalue #
4059 | mov LFUNC:RB, [BASE-8]
4060 | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
4061 | mov RB, UPVAL:RB->v
4064 | mov [BASE+RA*8], RDa
4068 | mov [BASE+RA*8+4], RD
4069 | mov [BASE+RA*8], RB
4074 #define TV2MARKOFS \
4075 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
4076 | ins_AD // RA = upvalue #, RD = src
4077 | mov LFUNC:RB, [BASE-8]
4078 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4079 | cmp byte UPVAL:RB->closed, 0
4080 | mov RB, UPVAL:RB->v
4081 | mov RA, [BASE+RD*8]
4082 | mov RD, [BASE+RD*8+4]
4086 | // Check barrier for closed upvalue.
4087 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
4092 |2: // Upvalue is black. Check if new value is collectable and white.
4094 | cmp RD, LJ_TNUMX - LJ_TISGCV // tvisgcv(v)
4096 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
4098 | // Crossed a write barrier. Move the barrier forward.
4099 |.if X64 and not X64WIN
4101 | mov RB, BASE // Save BASE.
4103 | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
4105 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4106 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4107 | mov BASE, RB // Restore BASE.
4112 | ins_AND // RA = upvalue #, RD = str const (~)
4113 | mov LFUNC:RB, [BASE-8]
4114 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4115 | mov GCOBJ:RA, [KBASE+RD*4]
4116 | mov RD, UPVAL:RB->v
4117 | mov [RD], GCOBJ:RA
4118 | mov dword [RD+4], LJ_TSTR
4119 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
4124 |2: // Check if string is white and ensure upvalue is closed.
4125 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
4127 | cmp byte UPVAL:RB->closed, 0
4129 | // Crossed a write barrier. Move the barrier forward.
4130 | mov RB, BASE // Save BASE (FCARG2 == BASE).
4132 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4133 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4134 | mov BASE, RB // Restore BASE.
4138 | ins_AD // RA = upvalue #, RD = num const
4139 | mov LFUNC:RB, [BASE-8]
4140 | movsd xmm0, qword [KBASE+RD*8]
4141 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4142 | mov RA, UPVAL:RB->v
4143 | movsd qword [RA], xmm0
4147 | ins_AND // RA = upvalue #, RD = primitive type (~)
4148 | mov LFUNC:RB, [BASE-8]
4149 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4150 | mov RA, UPVAL:RB->v
4155 | ins_AD // RA = level, RD = target
4156 | branchPC RD // Do this first to free RD.
4158 | cmp dword L:RB->openupval, 0
4160 | mov L:RB->base, BASE
4161 | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
4162 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4163 | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
4164 | mov BASE, L:RB->base
4170 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
4173 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4174 | mov CARG3d, [BASE-8]
4175 | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
4178 | mov LFUNC:RA, [BASE-8]
4179 | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
4181 | mov ARG3, LFUNC:RA
4182 | mov ARG2, PROTO:RD
4184 | mov L:RB->base, BASE
4187 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4188 | call extern lj_func_newL_gc
4189 | // GCfuncL * returned in eax (RC).
4190 | mov BASE, L:RB->base
4192 | mov [BASE+RA*8], LFUNC:RC
4193 | mov dword [BASE+RA*8+4], LJ_TFUNC
4197 /* -- Table ops --------------------------------------------------------- */
4200 | ins_AD // RA = dst, RD = hbits|asize
4202 | mov L:RB->base, BASE
4203 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4204 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4222 | mov L:CARG1d, L:RB
4228 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4229 | // Table * returned in eax (RC).
4230 | mov BASE, L:RB->base
4232 | mov [BASE+RA*8], TAB:RC
4233 | mov dword [BASE+RA*8+4], LJ_TTAB
4235 |3: // Turn 0x7ff into 0x801.
4239 | mov L:FCARG1, L:RB
4240 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4245 | ins_AND // RA = dst, RD = table const (~) (holding template table)
4247 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4249 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4250 | mov L:RB->base, BASE
4253 | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
4254 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4255 | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
4256 | // Table * returned in eax (RC).
4257 | mov BASE, L:RB->base
4259 | mov [BASE+RA*8], TAB:RC
4260 | mov dword [BASE+RA*8+4], LJ_TTAB
4263 | mov L:FCARG1, L:RB
4264 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4265 | movzx RD, PC_RD // Need to reload RD.
4271 | ins_AND // RA = dst, RD = str const (~)
4272 | mov LFUNC:RB, [BASE-8]
4273 | mov TAB:RB, LFUNC:RB->env
4274 | mov STR:RC, [KBASE+RD*4]
4278 | ins_AND // RA = src, RD = str const (~)
4279 | mov LFUNC:RB, [BASE-8]
4280 | mov TAB:RB, LFUNC:RB->env
4281 | mov STR:RC, [KBASE+RD*4]
4286 | ins_ABC // RA = dst, RB = table, RC = key
4287 | checktab RB, ->vmeta_tgetv
4288 | mov TAB:RB, [BASE+RB*8]
4293 | mov RC, dword [BASE+RC*8]
4295 | // Convert number to int and back and compare.
4297 | movsd xmm0, qword [BASE+RC*8]
4298 | cvttsd2si RC, xmm0
4300 | ucomisd xmm0, xmm1
4301 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
4303 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4304 | jae ->vmeta_tgetv // Not in array part? Use fallback.
4306 | add RC, TAB:RB->array
4307 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4309 | // Get array slot.
4312 | mov [BASE+RA*8], RBa
4316 | mov [BASE+RA*8], RB
4317 | mov [BASE+RA*8+4], RC
4322 |2: // Check for __index if table value is nil.
4323 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4325 | mov TAB:RA, TAB:RB->metatable
4326 | test byte TAB:RA->nomm, 1<<MM_index
4327 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
4328 | movzx RA, PC_RA // Restore RA.
4330 | mov dword [BASE+RA*8+4], LJ_TNIL
4334 | checkstr RC, ->vmeta_tgetv
4335 | mov STR:RC, [BASE+RC*8]
4339 | ins_ABC // RA = dst, RB = table, RC = str const (~)
4341 | mov STR:RC, [KBASE+RC*4]
4342 | checktab RB, ->vmeta_tgets
4343 | mov TAB:RB, [BASE+RB*8]
4344 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4345 | mov RA, TAB:RB->hmask
4346 | and RA, STR:RC->sid
4348 | add NODE:RA, TAB:RB->node
4350 | cmp dword NODE:RA->key.it, LJ_TSTR
4352 | cmp dword NODE:RA->key.gcr, STR:RC
4354 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4355 | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4356 | je >5 // Key found, but nil value?
4358 | // Get node value.
4361 | mov [BASE+RC*8], RBa
4365 | mov [BASE+RC*8], RB
4366 | mov [BASE+RC*8+4], RA
4373 | mov dword [BASE+RC*8+4], LJ_TNIL
4376 |4: // Follow hash chain.
4377 | mov NODE:RA, NODE:RA->next
4378 | test NODE:RA, NODE:RA
4380 | // End of hash chain: key not found, nil result.
4382 |5: // Check for __index if table value is nil.
4383 | mov TAB:RA, TAB:RB->metatable
4384 | test TAB:RA, TAB:RA
4385 | jz <3 // No metatable: done.
4386 | test byte TAB:RA->nomm, 1<<MM_index
4387 | jnz <3 // 'no __index' flag set: done.
4388 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
4391 | ins_ABC // RA = dst, RB = table, RC = byte literal
4392 | checktab RB, ->vmeta_tgetb
4393 | mov TAB:RB, [BASE+RB*8]
4394 | cmp RC, TAB:RB->asize
4397 | add RC, TAB:RB->array
4398 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4400 | // Get array slot.
4403 | mov [BASE+RA*8], RBa
4407 | mov [BASE+RA*8], RB
4408 | mov [BASE+RA*8+4], RC
4413 |2: // Check for __index if table value is nil.
4414 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4416 | mov TAB:RA, TAB:RB->metatable
4417 | test byte TAB:RA->nomm, 1<<MM_index
4418 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
4419 | movzx RA, PC_RA // Restore RA.
4421 | mov dword [BASE+RA*8+4], LJ_TNIL
4425 | ins_ABC // RA = dst, RB = table, RC = key
4426 | mov TAB:RB, [BASE+RB*8]
4428 | mov RC, dword [BASE+RC*8]
4430 | cvttsd2si RC, qword [BASE+RC*8]
4432 | cmp RC, TAB:RB->asize
4433 | jae ->vmeta_tgetr // Not in array part? Use fallback.
4435 | add RC, TAB:RB->array
4436 | // Get array slot.
4440 | mov [BASE+RA*8], RBa
4444 | mov [BASE+RA*8], RB
4445 | mov [BASE+RA*8+4], RC
4452 | ins_ABC // RA = src, RB = table, RC = key
4453 | checktab RB, ->vmeta_tsetv
4454 | mov TAB:RB, [BASE+RB*8]
4459 | mov RC, dword [BASE+RC*8]
4461 | // Convert number to int and back and compare.
4463 | movsd xmm0, qword [BASE+RC*8]
4464 | cvttsd2si RC, xmm0
4466 | ucomisd xmm0, xmm1
4467 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
4469 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4472 | add RC, TAB:RB->array
4473 | cmp dword [RC+4], LJ_TNIL
4474 | je >3 // Previous value is nil?
4476 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4478 |2: // Set array slot.
4480 | mov RBa, [BASE+RA*8]
4483 | mov RB, [BASE+RA*8+4]
4484 | mov RA, [BASE+RA*8]
4490 |3: // Check for __newindex if previous value is nil.
4491 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4493 | mov TAB:RA, TAB:RB->metatable
4494 | test byte TAB:RA->nomm, 1<<MM_newindex
4495 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
4496 | movzx RA, PC_RA // Restore RA.
4500 | checkstr RC, ->vmeta_tsetv
4501 | mov STR:RC, [BASE+RC*8]
4504 |7: // Possible table write barrier for the value. Skip valiswhite check.
4505 | barrierback TAB:RB, RA
4506 | movzx RA, PC_RA // Restore RA.
4510 | ins_ABC // RA = src, RB = table, RC = str const (~)
4512 | mov STR:RC, [KBASE+RC*4]
4513 | checktab RB, ->vmeta_tsets
4514 | mov TAB:RB, [BASE+RB*8]
4515 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4516 | mov RA, TAB:RB->hmask
4517 | and RA, STR:RC->sid
4519 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
4520 | add NODE:RA, TAB:RB->node
4522 | cmp dword NODE:RA->key.it, LJ_TSTR
4524 | cmp dword NODE:RA->key.gcr, STR:RC
4526 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4527 | cmp dword [RA+4], LJ_TNIL
4528 | je >4 // Previous value is nil?
4530 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4532 |3: // Set node value.
4535 | mov RBa, [BASE+RC*8]
4538 | mov RB, [BASE+RC*8+4]
4539 | mov RC, [BASE+RC*8]
4545 |4: // Check for __newindex if previous value is nil.
4546 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4548 | mov TMP1, RA // Save RA.
4549 | mov TAB:RA, TAB:RB->metatable
4550 | test byte TAB:RA->nomm, 1<<MM_newindex
4551 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4552 | mov RA, TMP1 // Restore RA.
4555 |5: // Follow hash chain.
4556 | mov NODE:RA, NODE:RA->next
4557 | test NODE:RA, NODE:RA
4559 | // End of hash chain: key not found, add a new one.
4561 | // But check for __newindex first.
4562 | mov TAB:RA, TAB:RB->metatable
4563 | test TAB:RA, TAB:RA
4564 | jz >6 // No metatable: continue.
4565 | test byte TAB:RA->nomm, 1<<MM_newindex
4566 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4570 | mov TMP3, TAB:RB // Save TAB:RB for us.
4572 | mov L:CARG1d, SAVE_L
4573 | mov L:CARG1d->base, BASE
4575 | mov CARG2d, TAB:RB
4576 | mov L:RB, L:CARG1d
4578 | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
4583 | mov L:RB->base, BASE
4586 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4587 | // Handles write barrier for the new key. TValue * returned in eax (RC).
4588 | mov BASE, L:RB->base
4589 | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
4591 | jmp <2 // Must check write barrier for value.
4593 |7: // Possible table write barrier for the value. Skip valiswhite check.
4594 | barrierback TAB:RB, RC // Destroys STR:RC.
4598 | ins_ABC // RA = src, RB = table, RC = byte literal
4599 | checktab RB, ->vmeta_tsetb
4600 | mov TAB:RB, [BASE+RB*8]
4601 | cmp RC, TAB:RB->asize
4604 | add RC, TAB:RB->array
4605 | cmp dword [RC+4], LJ_TNIL
4606 | je >3 // Previous value is nil?
4608 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4610 |2: // Set array slot.
4612 | mov RAa, [BASE+RA*8]
4615 | mov RB, [BASE+RA*8+4]
4616 | mov RA, [BASE+RA*8]
4622 |3: // Check for __newindex if previous value is nil.
4623 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4625 | mov TAB:RA, TAB:RB->metatable
4626 | test byte TAB:RA->nomm, 1<<MM_newindex
4627 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
4628 | movzx RA, PC_RA // Restore RA.
4631 |7: // Possible table write barrier for the value. Skip valiswhite check.
4632 | barrierback TAB:RB, RA
4633 | movzx RA, PC_RA // Restore RA.
4637 | ins_ABC // RA = src, RB = table, RC = key
4638 | mov TAB:RB, [BASE+RB*8]
4640 | mov RC, dword [BASE+RC*8]
4642 | cvttsd2si RC, qword [BASE+RC*8]
4644 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4647 | cmp RC, TAB:RB->asize
4650 | add RC, TAB:RB->array
4651 | // Set array slot.
4654 | mov RBa, [BASE+RA*8]
4657 | mov RB, [BASE+RA*8+4]
4658 | mov RA, [BASE+RA*8]
4664 |7: // Possible table write barrier for the value. Skip valiswhite check.
4665 | barrierback TAB:RB, RA
4666 | movzx RA, PC_RA // Restore RA.
4671 | ins_AD // RA = base (table at base-1), RD = num const (start index)
4672 | mov TMP1, KBASE // Need one more free register.
4673 | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
4675 | lea RA, [BASE+RA*8]
4676 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
4677 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4682 | jz >4 // Nothing to copy?
4683 | add RD, KBASE // Compute needed size.
4684 | cmp RD, TAB:RB->asize
4685 | ja >5 // Doesn't fit into array part?
4688 | add KBASE, TAB:RB->array
4689 |3: // Copy result slots to table.
4708 |5: // Need to resize array part.
4710 | mov L:CARG1d, SAVE_L
4711 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4712 | mov CARG2d, TAB:RB
4714 | mov L:RB, L:CARG1d
4718 | mov L:RB->base, BASE
4723 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4724 | mov BASE, L:RB->base
4725 | movzx RA, PC_RA // Restore RA.
4728 |7: // Possible table write barrier for any value. Skip valiswhite check.
4729 | barrierback TAB:RB, RD
4733 /* -- Calls and vararg handling ----------------------------------------- */
4735 case BC_CALL: case BC_CALLM:
4736 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
4737 if (op == BC_CALLM) {
4738 | add NARGS:RD, MULTRES
4740 | cmp dword [BASE+RA*8+4], LJ_TFUNC
4741 | mov LFUNC:RB, [BASE+RA*8]
4742 | jne ->vmeta_call_ra
4743 | lea BASE, [BASE+RA*8+8]
4748 | ins_AD // RA = base, RD = extra_nargs
4749 | add NARGS:RD, MULTRES
4750 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
4753 | ins_AD // RA = base, RD = nargs+1
4754 | lea RA, [BASE+RA*8+8]
4755 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
4756 | mov LFUNC:RB, [RA-8]
4757 | cmp dword [RA-4], LJ_TFUNC
4761 | test PC, FRAME_TYPE
4764 | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
4765 | mov MULTRES, NARGS:RD
4768 |2: // Move args down.
4784 | mov LFUNC:RB, [BASE-8]
4786 | mov NARGS:RD, MULTRES
4787 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
4792 |5: // Tailcall to a fast function.
4793 | test PC, FRAME_TYPE // Lua frame below?
4797 | mov LFUNC:KBASE, [BASE+RA*8-8] // Need to prepare KBASE.
4798 | mov KBASE, LFUNC:KBASE->pc
4799 | mov KBASE, [KBASE+PC2PROTO(k)]
4802 |7: // Tailcall from a vararg function.
4803 | sub PC, FRAME_VARG
4804 | test PC, FRAME_TYPEP
4805 | jnz >8 // Vararg frame below?
4806 | sub BASE, PC // Need to relocate BASE/KBASE down.
4811 | add PC, FRAME_VARG
4816 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
4817 | lea RA, [BASE+RA*8+8] // fb = base+1
4819 | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
4820 | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
4824 | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
4828 | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
4833 | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
4835 | mov [RA-8], LFUNC:RB
4837 | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
4849 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
4850 | mov TMP1, KBASE // Need two more free registers.
4851 | mov TMP2, DISPATCH
4852 | mov TAB:RB, [BASE+RA*8-16]
4853 | mov RC, [BASE+RA*8-8] // Get index from control var.
4854 | mov DISPATCH, TAB:RB->asize
4856 | mov KBASE, TAB:RB->array
4857 |1: // Traverse array part.
4858 | cmp RC, DISPATCH; jae >5 // Index points after array part?
4859 | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
4861 | mov dword [BASE+RA*8+4], LJ_TISNUM
4862 | mov dword [BASE+RA*8], RC
4866 | // Copy array slot to returned value.
4868 | mov RBa, [KBASE+RC*8]
4869 | mov [BASE+RA*8+8], RBa
4871 | mov RB, [KBASE+RC*8+4]
4872 | mov [BASE+RA*8+12], RB
4873 | mov RB, [KBASE+RC*8]
4874 | mov [BASE+RA*8+8], RB
4877 | // Return array index as a numeric key.
4881 | movsd qword [BASE+RA*8], xmm0
4883 | mov [BASE+RA*8-8], RC // Update control var.
4885 | movzx RD, PC_RD // Get target from ITERL.
4888 | mov DISPATCH, TMP2
4892 |4: // Skip holes in array part.
4896 |5: // Traverse hash part.
4899 | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
4900 | imul KBASE, RC, #NODE
4901 | add NODE:KBASE, TAB:RB->node
4902 | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
4903 | lea DISPATCH, [RC+DISPATCH+1]
4904 | // Copy key and value from hash slot.
4906 | mov RBa, NODE:KBASE->key
4907 | mov RCa, NODE:KBASE->val
4908 | mov [BASE+RA*8], RBa
4909 | mov [BASE+RA*8+8], RCa
4911 | mov RB, NODE:KBASE->key.gcr
4912 | mov RC, NODE:KBASE->key.it
4913 | mov [BASE+RA*8], RB
4914 | mov [BASE+RA*8+4], RC
4915 | mov RB, NODE:KBASE->val.gcr
4916 | mov RC, NODE:KBASE->val.it
4917 | mov [BASE+RA*8+8], RB
4918 | mov [BASE+RA*8+12], RC
4920 | mov [BASE+RA*8-8], DISPATCH
4923 |7: // Skip holes in hash part.
4929 | ins_AD // RA = base, RD = target (points to ITERN)
4930 | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
4931 | mov CFUNC:RB, [BASE+RA*8-24]
4932 | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
4933 | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
4934 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
4936 | mov dword [BASE+RA*8-8], 0 // Initialize control var.
4937 | mov dword [BASE+RA*8-4], LJ_KEYINDEX
4940 |5: // Despecialize bytecode if any of the checks fail.
4944 | cmp byte [PC], BC_ITERN
4947 | mov byte [PC], BC_ITERC
4950 |6: // Unpatch JLOOP.
4951 | mov RA, [DISPATCH+DISPATCH_J(trace)]
4952 | movzx RC, word [PC+2]
4953 | mov TRACE:RA, [RA+RC*4]
4954 | mov eax, TRACE:RA->startins
4956 | mov dword [PC], eax
4962 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
4963 | mov TMP1, KBASE // Need one more free register.
4964 | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
4965 | lea RA, [BASE+RA*8]
4966 | sub KBASE, [BASE-4]
4967 | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
4969 | jz >5 // Copy all varargs?
4970 | lea RB, [RA+RB*8-8]
4971 | cmp KBASE, BASE // No vararg slots?
4973 |1: // Copy vararg slots to destination slots.
4975 | mov RCa, [KBASE-8]
4986 | cmp RA, RB // All destination slots filled?
4988 | cmp KBASE, BASE // No more vararg slots?
4990 |2: // Fill up remainder with nil.
4991 | mov dword [RA+4], LJ_TNIL
4999 |5: // Copy all varargs.
5000 | mov MULTRES, 1 // MULTRES = 0+1
5003 | jbe <3 // No vararg slots?
5007 | mov MULTRES, RB // MULTRES = #varargs+1
5010 | cmp RC, L:RB->maxstack
5011 | ja >7 // Need to grow stack?
5012 |6: // Copy all vararg slots.
5014 | mov RCa, [KBASE-8]
5025 | cmp KBASE, BASE // No more vararg slots?
5029 |7: // Grow stack for varargs.
5030 | mov L:RB->base, BASE
5033 | sub KBASE, BASE // Need delta, because BASE may change.
5034 | mov FCARG2, MULTRES
5037 | call extern lj_state_growstack@8 // (lua_State *L, int n)
5038 | mov BASE, L:RB->base
5044 /* -- Returns ----------------------------------------------------------- */
5047 | ins_AD // RA = results, RD = extra_nresults
5048 | add RD, MULTRES // MULTRES >=1, so RD >=1.
5049 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
5052 case BC_RET: case BC_RET0: case BC_RET1:
5053 | ins_AD // RA = results, RD = nresults+1
5054 if (op != BC_RET0) {
5059 | mov MULTRES, RD // Save nresults+1.
5060 | test PC, FRAME_TYPE // Check frame type marker.
5061 | jnz >7 // Not returning to a fixarg Lua func?
5065 | mov KBASE, BASE // Use KBASE for result move.
5068 |2: // Move results down.
5070 | mov RBa, [KBASE+RA]
5071 | mov [KBASE-8], RBa
5073 | mov RB, [KBASE+RA]
5075 | mov RB, [KBASE+RA+4]
5082 | mov RD, MULTRES // Note: MULTRES may be >255.
5083 | movzx RB, PC_RB // So cannot compare with RDL!
5085 | cmp RB, RD // More results expected?
5090 | mov RBa, [BASE+RA]
5093 | mov RB, [BASE+RA+4]
5101 | cmp PC_RB, RDL // More results expected?
5107 | not RAa // Note: ~RA = -(RA+1)
5108 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
5109 | mov LFUNC:KBASE, [BASE-8]
5110 | mov KBASE, LFUNC:KBASE->pc
5111 | mov KBASE, [KBASE+PC2PROTO(k)]
5114 |6: // Fill up results with nil.
5116 | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
5119 | mov dword [BASE+RD*8-12], LJ_TNIL
5124 |7: // Non-standard return case.
5125 | lea RB, [PC-FRAME_VARG]
5126 | test RB, FRAME_TYPEP
5128 | // Return from vararg function: relocate BASE down and RA up.
5130 if (op != BC_RET0) {
5136 /* -- Loops and branches ------------------------------------------------ */
5138 |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
5139 |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
5140 |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
5141 |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
5147 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5157 vk = (op == BC_IFORL || op == BC_JFORL);
5158 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
5159 | lea RA, [BASE+RA*8]
5161 | cmp FOR_TIDX, LJ_TISNUM; jne >9
5163 | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5164 | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5165 | mov RB, dword FOR_IDX
5166 | cmp dword FOR_STEP, 0; jl >5
5168 #ifdef LUA_USE_ASSERT
5169 | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5170 | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5172 | mov RB, dword FOR_STEP
5173 | test RB, RB; js >5
5174 | add RB, dword FOR_IDX; jo >1
5175 | mov dword FOR_IDX, RB
5177 | cmp RB, dword FOR_STOP
5178 | mov FOR_TEXT, LJ_TISNUM
5179 | mov dword FOR_EXT, RB
5180 if (op == BC_FORI) {
5185 } else if (op == BC_JFORI) {
5191 } else if (op == BC_IFORL) {
5204 |5: // Invert check for negative step.
5206 | add RB, dword FOR_IDX; jo <1
5207 | mov dword FOR_IDX, RB
5209 | cmp RB, dword FOR_STOP
5210 | mov FOR_TEXT, LJ_TISNUM
5211 | mov dword FOR_EXT, RB
5212 if (op == BC_FORI) {
5214 } else if (op == BC_JFORI) {
5218 } else if (op == BC_IFORL) {
5224 |9: // Fallback to FP variant.
5226 | cmp FOR_TIDX, LJ_TISNUM
5230 | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5232 #ifdef LUA_USE_ASSERT
5233 | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5234 | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5237 | mov RB, FOR_TSTEP // Load type/hiword of for step.
5239 | cmp RB, LJ_TISNUM; jae ->vmeta_for
5241 | movsd xmm0, qword FOR_IDX
5242 | movsd xmm1, qword FOR_STOP
5244 | addsd xmm0, qword FOR_STEP
5245 | movsd qword FOR_IDX, xmm0
5246 | test RB, RB; js >3
5250 | ucomisd xmm1, xmm0
5252 | movsd qword FOR_EXT, xmm0
5253 if (op == BC_FORI) {
5260 } else if (op == BC_JFORI) {
5264 } else if (op == BC_IFORL) {
5281 |3: // Invert comparison if step is negative.
5282 | ucomisd xmm0, xmm1
5290 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5298 | ins_AJ // RA = base, RD = target
5299 | lea RA, [BASE+RA*8]
5301 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
5302 if (op == BC_JITERL) {
5308 | branchPC RD // Otherwise save control var + branch.
5318 | ins_A // RA = base, RD = target (loop extent)
5319 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5320 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5324 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5328 | ins_A // RA = base, RD = target (loop extent)
5334 | ins_AD // RA = base (ignored), RD = traceno
5335 | mov RA, [DISPATCH+DISPATCH_J(trace)]
5336 | mov TRACE:RD, [RA+RD*4]
5337 | mov RDa, TRACE:RD->mcode
5339 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5340 | mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB
5341 | // Save additional callee-save registers only used in compiled code.
5349 | movdqa [RAa], xmm6
5350 | movdqa [RAa-1*16], xmm7
5351 | movdqa [RAa-2*16], xmm8
5352 | movdqa [RAa-3*16], xmm9
5353 | movdqa [RAa-4*16], xmm10
5354 | movdqa [RAa-5*16], xmm11
5355 | movdqa [RAa-6*16], xmm12
5356 | movdqa [RAa-7*16], xmm13
5357 | movdqa [RAa-8*16], xmm14
5358 | movdqa [RAa-9*16], xmm15
5369 | ins_AJ // RA = unused, RD = target
5374 /* -- Function headers -------------------------------------------------- */
5377 ** Reminder: A function may be called with func/args above L->maxstack,
5378 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5379 ** too. This means all FUNC* ops (including fast functions) must check
5380 ** for stack overflow _before_ adding more slots!
5387 case BC_FUNCV: /* NYI: compiled vararg functions. */
5388 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5396 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5397 | mov KBASE, [PC-4+PC2PROTO(k)]
5399 | lea RA, [BASE+RA*8] // Top of frame.
5400 | cmp RA, L:RB->maxstack
5401 | ja ->vm_growstack_f
5402 | movzx RA, byte [PC-4+PC2PROTO(numparams)]
5403 | cmp NARGS:RD, RA // Check for missing parameters.
5406 if (op == BC_JFUNCF) {
5413 |3: // Clear missing parameters.
5414 | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5425 | int3 // NYI: compiled vararg functions
5426 break; /* NYI: compiled vararg functions. */
5429 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5430 | lea RB, [NARGS:RD*8+FRAME_VARG]
5431 | lea RD, [BASE+NARGS:RD*8]
5432 | mov LFUNC:KBASE, [BASE-8]
5433 | mov [RD-4], RB // Store delta + FRAME_VARG.
5434 | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
5437 | cmp RA, L:RB->maxstack
5438 | ja ->vm_growstack_v // Need to grow stack.
5441 | movzx RB, byte [PC-4+PC2PROTO(numparams)]
5444 |1: // Copy fixarg slots up to new frame.
5447 | jnb >3 // Less args than parameters?
5453 | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
5457 if (op == BC_JFUNCV) {
5461 | mov KBASE, [PC-4+PC2PROTO(k)]
5465 |3: // Clear missing parameters.
5466 | mov dword [RD+4], LJ_TNIL
5475 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
5476 | mov CFUNC:RB, [BASE-8]
5477 | mov KBASEa, CFUNC:RB->f
5479 | lea RD, [BASE+NARGS:RD*8-8]
5480 | mov L:RB->base, BASE
5481 | lea RA, [RD+8*LUA_MINSTACK]
5482 | cmp RA, L:RB->maxstack
5484 if (op == BC_FUNCC) {
5486 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5493 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5499 | ja ->vm_growstack_c // Need to grow stack.
5501 if (op == BC_FUNCC) {
5502 | call KBASEa // (lua_State *L)
5504 | // (lua_State *L, lua_CFunction f)
5505 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
5507 | // nresults returned in eax (RD).
5508 | mov BASE, L:RB->base
5509 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
5510 | set_vmstate INTERP
5511 | lea RA, [BASE+RD*8]
5513 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
5514 | mov PC, [BASE-4] // Fetch PC of caller.
5518 /* ---------------------------------------------------------------------- */
5521 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5527 static int build_backend(BuildCtx *ctx)
5530 dasm_growpc(Dst, BC__MAX);
5531 build_subroutines(ctx);
5533 for (op = 0; op < BC__MAX; op++)
5534 build_ins(ctx, (BCOp)op, op);
5538 /* Emit pseudo frame-info for all assembler functions. */
5539 static void emit_asm_debug(BuildCtx *ctx)
5541 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5545 #define REG_SP "0x7"
5546 #define REG_RA "0x10"
5550 #define REG_SP "0x4"
5551 #define REG_RA "0x8"
5553 switch (ctx->mode) {
5555 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5558 "\t.long .LECIE0-.LSCIE0\n"
5560 "\t.long 0xffffffff\n"
5564 "\t.sleb128 -" SZPTR "\n"
5565 "\t.byte " REG_RA "\n"
5566 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5567 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5568 "\t.align " SZPTR "\n"
5572 "\t.long .LEFDE0-.LASFDE0\n"
5574 "\t.long .Lframe0\n"
5578 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5579 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5580 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5581 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
5582 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
5584 "\t.byte 0x8d\n\t.uleb128 0x6\n" /* offset r13 */
5585 "\t.byte 0x8c\n\t.uleb128 0x7\n" /* offset r12 */
5590 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5591 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5592 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
5593 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
5594 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
5596 "\t.align " SZPTR "\n"
5597 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
5601 "\t.long .LEFDE1-.LASFDE1\n"
5603 "\t.long .Lframe0\n"
5605 "\t.quad lj_vm_ffi_call\n"
5607 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
5608 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5609 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
5610 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5612 "\t.long lj_vm_ffi_call\n"
5614 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
5615 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5616 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
5617 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
5619 "\t.align " SZPTR "\n"
5620 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5623 #if LJ_TARGET_SOLARIS
5625 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");
5627 fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
5630 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5634 "\t.long .LECIE1-.LSCIE1\n"
5638 "\t.string \"zPR\"\n"
5640 "\t.sleb128 -" SZPTR "\n"
5641 "\t.byte " REG_RA "\n"
5642 "\t.uleb128 6\n" /* augmentation length */
5643 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5644 "\t.long lj_err_unwind_dwarf-.\n"
5645 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5646 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5647 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5648 "\t.align " SZPTR "\n"
5652 "\t.long .LEFDE2-.LASFDE2\n"
5654 "\t.long .LASFDE2-.Lframe1\n"
5655 "\t.long .Lbegin-.\n"
5657 "\t.uleb128 0\n" /* augmentation length */
5658 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5660 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5661 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5662 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
5663 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
5665 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5666 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
5667 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
5668 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
5670 "\t.align " SZPTR "\n"
5671 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
5675 "\t.long .LECIE2-.LSCIE2\n"
5679 "\t.string \"zR\"\n"
5681 "\t.sleb128 -" SZPTR "\n"
5682 "\t.byte " REG_RA "\n"
5683 "\t.uleb128 1\n" /* augmentation length */
5684 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5685 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5686 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5687 "\t.align " SZPTR "\n"
5691 "\t.long .LEFDE3-.LASFDE3\n"
5693 "\t.long .LASFDE3-.Lframe2\n"
5694 "\t.long lj_vm_ffi_call-.\n"
5696 "\t.uleb128 0\n" /* augmentation length */
5698 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
5699 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5700 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
5701 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5703 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
5704 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5705 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
5706 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
5708 "\t.align " SZPTR "\n"
5709 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
5714 /* Mental note: never let Apple design an assembler.
5715 ** Or a linker. Or a plastic case. But I digress.
5717 case BUILD_machasm: {
5722 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
5725 "\t.set L$set$x,LECIEX-LSCIEX\n"
5730 "\t.ascii \"zPR\\0\"\n"
5732 "\t.byte 128-" SZPTR "\n"
5733 "\t.byte " REG_RA "\n"
5734 "\t.byte 6\n" /* augmentation length */
5735 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
5737 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
5738 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5739 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
5741 "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
5742 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5743 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
5745 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
5746 "\t.align " BSZPTR "\n"
5748 for (i = 0; i < ctx->nsym; i++) {
5749 const char *name = ctx->sym[i].name;
5750 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
5751 if (size == 0) continue;
5753 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
5758 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
5759 "\t.long L$set$%d\n"
5761 "\t.long LASFDE%d-EH_frame1\n"
5764 "\t.byte 0\n" /* augmentation length */
5765 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
5767 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
5768 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
5769 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
5770 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
5772 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
5773 "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
5774 "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
5775 "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
5777 "\t.align " BSZPTR "\n"
5779 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
5785 "\t.set L$set$y,LECIEY-LSCIEY\n"
5790 "\t.ascii \"zR\\0\"\n"
5792 "\t.byte 128-" SZPTR "\n"
5793 "\t.byte " REG_RA "\n"
5794 "\t.byte 1\n" /* augmentation length */
5796 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5797 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
5799 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5800 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
5802 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
5803 "\t.align " BSZPTR "\n"
5806 "_lj_vm_ffi_call.eh:\n"
5808 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
5809 "\t.long L$set$yy\n"
5811 "\t.long LASFDEY-EH_frame2\n"
5812 "\t.long _lj_vm_ffi_call-.\n"
5814 "\t.byte 0\n" /* augmentation length */
5816 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
5817 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
5818 "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */
5819 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
5821 "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
5822 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
5823 "\t.byte 0xd\n\t.byte 0x4\n" /* def_cfa_register ebp */
5824 "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
5826 "\t.align " BSZPTR "\n"
5827 "LEFDEY:\n\n", fcsize);
5832 "\t.non_lazy_symbol_pointer\n"
5833 "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
5834 ".indirect_symbol _lj_err_unwind_dwarf\n"
5836 fprintf(ctx->fp, "\t.section __IMPORT,__jump_table,symbol_stubs,pure_instructions+self_modifying_code,5\n");
5838 const char *const *xn;
5839 for (xn = ctx->extnames; *xn; xn++)
5840 if (strncmp(*xn, LABEL_PREFIX, sizeof(LABEL_PREFIX)-1))
5841 fprintf(ctx->fp, "L_%s$stub:\n\t.indirect_symbol _%s\n\t.ascii \"\\364\\364\\364\\364\\364\"\n", *xn, *xn);
5844 fprintf(ctx->fp, ".subsections_via_symbols\n");
5848 default: /* Difficult for other modes. */