1 |// Low-level VM code for x86 CPUs.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2015 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.
124 |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
126 | push edi; push esi; push ebx
127 | sub esp, CFRAME_SPACE
130 | push ebp; saveregs_
133 | add esp, CFRAME_SPACE
134 | pop ebx; pop esi; pop edi; pop ebp
137 |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
138 |.define SAVE_NRES, aword [esp+aword*14]
139 |.define SAVE_CFRAME, aword [esp+aword*13]
140 |.define SAVE_L, aword [esp+aword*12]
141 |//----- 16 byte aligned, ^^^ arguments from C caller
142 |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
143 |.define SAVE_R4, aword [esp+aword*10]
144 |.define SAVE_R3, aword [esp+aword*9]
145 |.define SAVE_R2, aword [esp+aword*8]
146 |//----- 16 byte aligned
147 |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
148 |.define SAVE_PC, aword [esp+aword*6]
149 |.define TMP2, aword [esp+aword*5]
150 |.define TMP1, aword [esp+aword*4]
151 |//----- 16 byte aligned
152 |.define ARG4, aword [esp+aword*3]
153 |.define ARG3, aword [esp+aword*2]
154 |.define ARG2, aword [esp+aword*1]
155 |.define ARG1, aword [esp] //<-- esp while in interpreter.
156 |//----- 16 byte aligned, ^^^ arguments for C callee
158 |// FPARGx overlaps ARGx and ARG(x+1) on x86.
159 |.define FPARG3, qword [esp+qword*1]
160 |.define FPARG1, qword [esp]
161 |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
162 |.define TMPQ, qword [esp+aword*4]
166 |.define MULTRES, TMP2
168 |// Arguments for vm_call and vm_pcall.
169 |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
171 |// Arguments for vm_cpcall.
172 |.define INARG_CP_CALL, SAVE_ERRF
173 |.define INARG_CP_UD, SAVE_NRES
174 |.define INARG_CP_FUNC, SAVE_CFRAME
176 |//-----------------------------------------------------------------------
177 |.elif X64WIN // x64/Windows stack layout
179 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
181 | push rdi; push rsi; push rbx
182 | sub rsp, CFRAME_SPACE
185 | push rbp; saveregs_
188 | add rsp, CFRAME_SPACE
189 | pop rbx; pop rsi; pop rdi; pop rbp
192 |.define SAVE_CFRAME, aword [rsp+aword*13]
193 |.define SAVE_PC, dword [rsp+dword*25]
194 |.define SAVE_L, dword [rsp+dword*24]
195 |.define SAVE_ERRF, dword [rsp+dword*23]
196 |.define SAVE_NRES, dword [rsp+dword*22]
197 |.define TMP2, dword [rsp+dword*21]
198 |.define TMP1, dword [rsp+dword*20]
199 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
200 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
201 |.define SAVE_R4, aword [rsp+aword*8]
202 |.define SAVE_R3, aword [rsp+aword*7]
203 |.define SAVE_R2, aword [rsp+aword*6]
204 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
205 |.define ARG5, aword [rsp+aword*4]
206 |.define CSAVE_4, aword [rsp+aword*3]
207 |.define CSAVE_3, aword [rsp+aword*2]
208 |.define CSAVE_2, aword [rsp+aword*1]
209 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
210 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
212 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
213 |.define TMPQ, qword [rsp+aword*10]
214 |.define MULTRES, TMP2
216 |.define ARG5d, dword [rsp+aword*4]
219 |//-----------------------------------------------------------------------
220 |.else // x64/POSIX stack layout
222 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
224 | push rbx; push r15; push r14
228 | sub rsp, CFRAME_SPACE
231 | push rbp; saveregs_
234 | add rsp, CFRAME_SPACE
238 | pop r14; pop r15; pop rbx; pop rbp
241 |//----- 16 byte aligned,
243 |.define SAVE_RET, aword [rsp+aword*11] //<-- rsp entering interpreter.
244 |.define SAVE_R4, aword [rsp+aword*10]
245 |.define SAVE_R3, aword [rsp+aword*9]
246 |.define SAVE_R2, aword [rsp+aword*8]
247 |.define SAVE_R1, aword [rsp+aword*7]
248 |.define SAVE_RU2, aword [rsp+aword*6]
249 |.define SAVE_RU1, aword [rsp+aword*5] //<-- rsp after register saves.
251 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
252 |.define SAVE_R4, aword [rsp+aword*8]
253 |.define SAVE_R3, aword [rsp+aword*7]
254 |.define SAVE_R2, aword [rsp+aword*6]
255 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
257 |.define SAVE_CFRAME, aword [rsp+aword*4]
258 |.define SAVE_PC, dword [rsp+dword*7]
259 |.define SAVE_L, dword [rsp+dword*6]
260 |.define SAVE_ERRF, dword [rsp+dword*5]
261 |.define SAVE_NRES, dword [rsp+dword*4]
262 |.define TMPa, aword [rsp+aword*1]
263 |.define TMP2, dword [rsp+dword*1]
264 |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
265 |//----- 16 byte aligned
267 |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
268 |.define TMPQ, qword [rsp]
269 |.define TMP3, dword [rsp+aword*1]
270 |.define MULTRES, TMP2
274 |//-----------------------------------------------------------------------
276 |// Instruction headers.
277 |.macro ins_A; .endmacro
278 |.macro ins_AD; .endmacro
279 |.macro ins_AJ; .endmacro
280 |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
281 |.macro ins_AB_; movzx RB, RCH; .endmacro
282 |.macro ins_A_C; movzx RC, RCL; .endmacro
283 |.macro ins_AND; not RDa; .endmacro
285 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
293 | jmp aword [DISPATCH+OP*8]
295 | jmp aword [DISPATCH+OP*4]
299 |// Instruction footer.
301 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
302 | .define ins_next, ins_NEXT
303 | .define ins_next_, ins_NEXT
305 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
306 | // Affects only certain kinds of benchmarks (and only with -j off).
307 | // Around 10%-30% slower on Core2, a lot more slower on P4.
317 |// Call decode and dispatch.
319 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
320 | mov PC, LFUNC:RB->pc
326 | jmp aword [DISPATCH+OP*8]
328 | jmp aword [DISPATCH+OP*4]
333 | // BASE = new base, RB = LFUNC, RD = nargs+1
338 |//-----------------------------------------------------------------------
340 |// Macros to test operand types.
341 |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
342 |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
343 |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
344 |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
345 |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
347 |// These operands must be used with movzx.
348 |.define PC_OP, byte [PC-4]
349 |.define PC_RA, byte [PC-3]
350 |.define PC_RB, byte [PC-1]
351 |.define PC_RC, byte [PC-2]
352 |.define PC_RD, word [PC-2]
354 |.macro branchPC, reg
355 | lea PC, [PC+reg*4-BCBIAS_J*4]
358 |// Assumes DISPATCH is relative to GL.
359 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
360 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
362 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
364 |// Decrement hashed hotcount and trigger trace recorder if zero.
368 | and reg, HOTCOUNT_PCMASK
369 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
376 | and reg, HOTCOUNT_PCMASK
377 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
381 |// Set current VM state.
382 |.macro set_vmstate, st
383 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
387 |.macro fcomparepp // Compare and pop st0 >< st1.
392 |.macro fpop1; fstp st1; .endmacro
394 |// Synthesize SSE FP constants.
395 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
397 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
399 | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
403 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
405 | mov64 tmp, U64x(val,00000000); movd reg, tmp
407 | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
411 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
412 | sseconst_hi reg, tmp, 80000000
414 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
415 | sseconst_hi reg, tmp, 3ff00000
417 |.macro sseconst_m1, reg, tmp // Synthesize -1.0.
418 | sseconst_hi reg, tmp, bff00000
420 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
421 | sseconst_hi reg, tmp, 43300000
423 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
424 | sseconst_hi reg, tmp, 43380000
427 |// Move table write barrier back. Overwrites reg.
428 |.macro barrierback, tab, reg
429 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
430 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
431 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
432 | mov tab->gclist, reg
435 |//-----------------------------------------------------------------------
437 /* Generate subroutines used by opcodes and other parts of the VM. */
438 /* The .code_sub section should be last to help static branch prediction. */
439 static void build_subroutines(BuildCtx *ctx)
443 |//-----------------------------------------------------------------------
444 |//-- Return handling ----------------------------------------------------
445 |//-----------------------------------------------------------------------
451 | // Return from pcall or xpcall fast func.
453 | sub BASE, PC // Restore caller base.
454 | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
455 | mov PC, [BASE-4] // Fetch PC of previous frame.
456 | // Prepending may overwrite the pcall frame, so do it at the end.
457 | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
460 | add RD, 1 // RD = nresults+1
461 | jz ->vm_unwind_yield
463 | test PC, FRAME_TYPE
464 | jz ->BC_RET_Z // Handle regular return to Lua.
467 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
469 | test PC, FRAME_TYPE
476 | neg PC // Previous base = BASE - delta.
480 |1: // Move results down.
487 | mov RB, [BASE+RA+4]
498 | mov RA, SAVE_NRES // RA = wanted nresults+1
501 | jne >6 // More/less results wanted?
504 | mov L:RB->top, BASE
507 | mov RAa, SAVE_CFRAME // Restore previous C frame.
508 | mov L:RB->cframe, RAa
509 | xor eax, eax // Ok return status for vm_pcall.
516 | jb >7 // Less results wanted?
517 | // More results wanted. Check stack size and fill up results with nil.
518 | cmp BASE, L:RB->maxstack
520 | mov dword [BASE-4], LJ_TNIL
525 |7: // Less results wanted.
527 | jz <5 // But check for LUA_MULTRET+1.
528 | sub RA, RD // Negative result!
529 | lea BASE, [BASE+RA*8] // Correct top.
532 |8: // Corner case: need to grow stack for filling up results.
533 | // This can happen if:
534 | // - A C function grows the stack (a lot).
535 | // - The GC shrinks the stack in between.
536 | // - A return back from a lua_call() with (high) nresults adjustment.
537 | mov L:RB->top, BASE // Save current top held in BASE (yes).
538 | mov MULTRES, RD // Need to fill only remainder with nil.
541 | call extern lj_state_growstack@8 // (lua_State *L, int n)
542 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
547 | jmp ->vm_unwind_c_eh
549 |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
550 | // (void *cframe, int errcode)
552 | mov eax, CARG2d // Error return status for vm_pcall.
555 | mov eax, FCARG2 // Error return status for vm_pcall.
558 |->vm_unwind_c_eh: // Landing pad for external unwinder.
560 | mov GL:RB, L:RB->glref
561 | mov dword GL:RB->vmstate, ~LJ_VMST_C
564 |->vm_unwind_rethrow:
565 |.if X64 and not X64WIN
569 | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
572 |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
575 | and CARG1, CFRAME_RAWMASK
578 | and FCARG1, CFRAME_RAWMASK
581 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
583 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
584 | mov RD, 1+1 // Really 1+2 results, incr. later.
585 | mov BASE, L:RB->base
586 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
587 | add DISPATCH, GG_G2DISP
588 | mov PC, [BASE-4] // Fetch PC of previous frame.
589 | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
591 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
593 |//-----------------------------------------------------------------------
594 |//-- Grow stack for calls -----------------------------------------------
595 |//-----------------------------------------------------------------------
597 |->vm_growstack_c: // Grow stack for C function.
598 | mov FCARG2, LUA_MINSTACK
601 |->vm_growstack_v: // Grow stack for vararg Lua function.
605 |->vm_growstack_f: // Grow stack for fixarg Lua function.
606 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
607 | lea RD, [BASE+NARGS:RD*8-8]
609 | movzx RA, byte [PC-4+PC2PROTO(framesize)]
610 | add PC, 4 // Must point after first instruction.
611 | mov L:RB->base, BASE
616 | // RB = L, L->base = new base, L->top = top
618 | call extern lj_state_growstack@8 // (lua_State *L, int n)
619 | mov BASE, L:RB->base
621 | mov LFUNC:RB, [BASE-8]
625 | // BASE = new base, RB = LFUNC, RD = nargs+1
626 | ins_callt // Just retry the call.
628 |//-----------------------------------------------------------------------
629 |//-- Entry points into the assembler VM ---------------------------------
630 |//-----------------------------------------------------------------------
632 |->vm_resume: // Setup C frame and resume thread.
633 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
636 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
641 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
645 | lea KBASEa, [esp+CFRAME_RESUME]
646 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
647 | add DISPATCH, GG_G2DISP
648 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
649 | mov SAVE_CFRAME, RDa
654 | mov L:RB->cframe, KBASEa
655 | cmp byte L:RB->status, RDL
656 | je >2 // Initial resume (like a call).
658 | // Resume after yield (like a return).
659 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
661 | mov byte L:RB->status, RDL
662 | mov BASE, L:RB->base
666 | add RD, 1 // RD = nresults+1
667 | sub RA, BASE // RA = resultofs
670 | test PC, FRAME_TYPE
674 |->vm_pcall: // Setup protected C frame and enter VM.
675 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
679 | mov SAVE_ERRF, CARG4d
683 |->vm_call: // Setup C frame and enter VM.
684 | // (lua_State *L, TValue *base, int nres1)
688 |1: // Entry point for vm_pcall above (PC = ftype).
690 | mov SAVE_NRES, CARG3d
691 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
696 | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
699 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
700 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
701 | mov SAVE_CFRAME, KBASEa
702 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
703 | add DISPATCH, GG_G2DISP
705 | mov L:RB->cframe, rsp
707 | mov L:RB->cframe, esp
710 |2: // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype).
711 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
713 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
715 | sub PC, BASE // PC = frame delta + frame type
720 | add NARGS:RD, 1 // RD = nargs+1
723 | mov LFUNC:RB, [RA-8]
724 | cmp dword [RA-4], LJ_TFUNC
725 | jne ->vmeta_call // Ensure KBASE defined and != BASE.
727 |->vm_call_dispatch_f:
730 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
732 |->vm_cpcall: // Setup protected C frame, call C.
733 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
736 | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
740 | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
741 | mov RC, INARG_CP_UD // Get args before they are overwritten.
742 | mov RA, INARG_CP_FUNC
743 | mov BASE, INARG_CP_CALL
745 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
747 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
748 | sub KBASE, L:RB->top
749 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
750 | mov SAVE_ERRF, 0 // No error function.
751 | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
752 | add DISPATCH, GG_G2DISP
753 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
756 | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
757 | mov SAVE_CFRAME, KBASEa
758 | mov L:RB->cframe, rsp
759 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
761 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
763 | mov ARG3, RC // Have to copy args downwards.
767 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
768 | mov SAVE_CFRAME, KBASE
769 | mov L:RB->cframe, esp
770 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
772 | call BASE // (lua_State *L, lua_CFunction func, void *ud)
774 | // TValue * (new base) or NULL returned in eax (RC).
776 | jz ->vm_leave_cp // No base? Just remove C frame.
779 | jmp <2 // Else continue with the call.
781 |//-----------------------------------------------------------------------
782 |//-- Metamethod handling ------------------------------------------------
783 |//-----------------------------------------------------------------------
785 |//-- Continuation dispatch ----------------------------------------------
788 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
792 | sub BASE, PC // Restore caller BASE.
793 | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
794 | mov RC, RA // ... in [RC]
795 | mov PC, [RB-12] // Restore PC from [cont|PC].
797 | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
802 | lea KBASEa, qword [=>0]
805 | mov RA, dword [RB-16]
811 | mov LFUNC:KBASE, [BASE-8]
812 | mov KBASE, LFUNC:KBASE->pc
813 | mov KBASE, [KBASE+PC2PROTO(k)]
814 | // BASE = base, RC = result, RB = meta base
815 | jmp RAa // Jump to continuation.
819 | je ->cont_ffi_callback // cont = 1: return from FFI callback.
820 | // cont = 0: Tail call from C function.
827 |->cont_cat: // BASE = base, RC = result, RB = mbase
830 | lea RA, [BASE+RA*8]
837 | mov L:CARG1d, SAVE_L
838 | mov L:CARG1d->base, BASE
843 | mov L:CARG1d, SAVE_L
844 | mov L:CARG1d->base, BASE
859 |//-- Table indexing metamethods -----------------------------------------
862 | mov TMP1, RC // RC = GCstr *
864 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
867 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
868 | mov [RA], TAB:RB // RB = GCtab *
869 | mov dword [RA+4], LJ_TTAB
876 | mov TMP2, LJ_TISNUM
882 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
886 | movzx RC, PC_RC // Reload TValue *k from RC.
887 | lea RC, [BASE+RC*8]
889 | movzx RB, PC_RB // Reload TValue *t from RB.
890 | lea RB, [BASE+RB*8]
893 | mov L:CARG1d, SAVE_L
894 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
896 | mov CARG3, RCa // May be 64 bit ptr to stack.
903 | mov L:RB->base, BASE
906 | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
907 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
908 | mov BASE, L:RB->base
911 |->cont_ra: // BASE = base, RC = result
915 | mov [BASE+RA*8], RBa
919 | mov [BASE+RA*8+4], RB
920 | mov [BASE+RA*8], RC
924 |3: // Call __index metamethod.
925 | // BASE = base, L->top = new base, stack = cont/func/t/k
927 | mov [RA-12], PC // [cont|PC]
928 | lea PC, [RA+FRAME_CONT]
930 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
931 | mov NARGS:RD, 2+1 // 2 args for func(t, k).
932 | jmp ->vm_call_dispatch_f
936 | mov RB, BASE // Save BASE.
937 | mov FCARG2, RC // Caveat: FCARG2 == BASE
938 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
939 | // cTValue * or NULL returned in eax (RC).
941 | mov BASE, RB // Restore BASE.
944 | mov dword [BASE+RA*8+4], LJ_TNIL
947 |//-----------------------------------------------------------------------
950 | mov TMP1, RC // RC = GCstr *
952 | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
955 | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
956 | mov [RA], TAB:RB // RB = GCtab *
957 | mov dword [RA+4], LJ_TTAB
964 | mov TMP2, LJ_TISNUM
970 | lea RCa, TMPQ // Store temp. TValue in TMPQ.
974 | movzx RC, PC_RC // Reload TValue *k from RC.
975 | lea RC, [BASE+RC*8]
977 | movzx RB, PC_RB // Reload TValue *t from RB.
978 | lea RB, [BASE+RB*8]
981 | mov L:CARG1d, SAVE_L
982 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
984 | mov CARG3, RCa // May be 64 bit ptr to stack.
991 | mov L:RB->base, BASE
994 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
995 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
996 | mov BASE, L:RB->base
999 | // NOBARRIER: lj_meta_tset ensures the table is not black.
1002 | mov RBa, [BASE+RA*8]
1005 | mov RB, [BASE+RA*8+4]
1006 | mov RA, [BASE+RA*8]
1010 |->cont_nop: // BASE = base, (RC = result)
1013 |3: // Call __newindex metamethod.
1014 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
1016 | mov [RA-12], PC // [cont|PC]
1018 | // Copy value to third argument.
1020 | mov RBa, [BASE+RC*8]
1023 | mov RB, [BASE+RC*8+4]
1024 | mov RC, [BASE+RC*8]
1028 | lea PC, [RA+FRAME_CONT]
1030 | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
1031 | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
1032 | jmp ->vm_call_dispatch_f
1036 | mov L:CARG1d, SAVE_L
1038 | mov L:CARG1d->base, BASE
1039 | xchg CARG2d, TAB:RB // Caveat: CARG2d == BASE.
1041 | mov L:CARG1d, SAVE_L
1042 | mov CARG2d, TAB:RB
1043 | mov L:CARG1d->base, BASE
1044 | mov RB, BASE // Save BASE.
1045 | mov CARG3d, RC // Caveat: CARG3d == BASE.
1049 | mov RB, BASE // Save BASE.
1052 | mov L:RA->base, BASE
1055 | call extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
1056 | // TValue * returned in eax (RC).
1058 | mov BASE, RB // Restore BASE.
1061 |//-- Comparison metamethods ---------------------------------------------
1066 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
1068 | lea CARG3d, [BASE+RD*8]
1069 | lea CARG2d, [BASE+RA*8]
1071 | lea CARG2d, [BASE+RA*8]
1072 | lea CARG3d, [BASE+RD*8]
1074 | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
1075 | movzx CARG4d, PC_OP
1078 | lea RD, [BASE+RD*8]
1079 | lea RA, [BASE+RA*8]
1085 | mov L:RB->base, BASE
1088 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
1089 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1091 | mov BASE, L:RB->base
1103 |->cont_condt: // BASE = base, RC = result
1105 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
1109 |->cont_condf: // BASE = base, RC = result
1110 | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
1119 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1121 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1124 | mov CARG4d, RB // Caveat: CARG4d == RA.
1126 | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
1135 | mov L:RB->base, BASE
1138 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
1139 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1146 | mov L:RB->base, BASE
1148 | mov FCARG2, dword [PC-4]
1150 | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
1151 | // 0/1 or TValue * (metamethod) returned in eax (RC).
1158 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1160 | movzx CARG3d, PC_RD
1161 | mov L:CARG1d, L:RB
1168 | mov L:RB->base, BASE
1171 | call extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
1172 | mov BASE, L:RB->base
1175 |//-- Arithmetic metamethods ---------------------------------------------
1182 | lea RC, [KBASE+RC*8]
1190 | lea RC, [KBASE+RC*8]
1191 | lea RB, [BASE+RB*8]
1196 | lea RC, [BASE+RD*8]
1205 | lea RC, [BASE+RC*8]
1207 | lea RB, [BASE+RB*8]
1209 | lea RA, [BASE+RA*8]
1216 | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
1218 | mov CARG1d, L:RB // Caveat: CARG1d == RA.
1220 | movzx CARG5d, PC_OP
1222 | mov CARG4d, RC // Caveat: CARG4d == RA.
1223 | mov L:CARG1d, SAVE_L
1224 | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
1226 | mov L:RB, L:CARG1d
1235 | mov L:RB->base, BASE
1238 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1239 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1240 | mov BASE, L:RB->base
1244 | // Call metamethod for binary op.
1246 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1249 | mov [RA-12], PC // [cont|PC]
1250 | lea PC, [RC+FRAME_CONT]
1251 | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
1252 | jmp ->vm_call_dispatch
1256 | mov L:RB->base, BASE
1257 | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
1258 | mov L:FCARG1, L:RB
1260 | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
1261 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1262 | mov BASE, L:RB->base
1265 | jne ->vmeta_binop // Binop call for compatibility.
1267 | mov TAB:FCARG1, [BASE+RD*8]
1270 | jmp ->vmeta_binop // Binop call for compatibility.
1273 |//-- Call metamethod ----------------------------------------------------
1276 | lea RA, [BASE+RA*8+8]
1277 |->vmeta_call: // Resolve and call __call metamethod.
1278 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1279 | mov TMP2, RA // Save RA, RC for us.
1280 | mov TMP1, NARGS:RD
1284 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
1286 | lea CARG3d, [RA+NARGS:RD*8]
1287 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
1289 | lea RC, [RA+NARGS:RD*8]
1294 | mov L:RB->base, BASE // This is the callers base!
1297 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1298 | mov BASE, L:RB->base
1300 | mov NARGS:RD, TMP1
1301 | mov LFUNC:RB, [RA-8]
1303 | // This is fragile. L->base must not move, KBASE must always be defined.
1304 | cmp KBASE, BASE // Continue with CALLT if flag set.
1307 | ins_call // Otherwise call resolved metamethod.
1309 |//-- Argument coercion for 'for' statement ------------------------------
1313 | mov L:RB->base, BASE
1314 | mov FCARG2, RA // Caveat: FCARG2 == BASE
1315 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
1317 | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
1318 | mov BASE, L:RB->base
1324 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1326 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
1329 |//-----------------------------------------------------------------------
1330 |//-- Fast functions -----------------------------------------------------
1331 |//-----------------------------------------------------------------------
1333 |.macro .ffunc, name
1337 |.macro .ffunc_1, name
1339 | cmp NARGS:RD, 1+1; jb ->fff_fallback
1342 |.macro .ffunc_2, name
1344 | cmp NARGS:RD, 2+1; jb ->fff_fallback
1347 |.macro .ffunc_nsse, name, op
1349 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1350 | op xmm0, qword [BASE]
1353 |.macro .ffunc_nsse, name
1354 | .ffunc_nsse name, movsd
1357 |.macro .ffunc_nnsse, name
1359 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1360 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1361 | movsd xmm0, qword [BASE]
1362 | movsd xmm1, qword [BASE+8]
1365 |.macro .ffunc_nnr, name
1367 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1368 | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
1369 | fld qword [BASE+8]
1373 |// Inlined GC threshold check. Caveat: uses label 1.
1375 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1376 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1382 |//-- Base library: checks -----------------------------------------------
1386 | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
1425 | mov CFUNC:RB, [BASE-8]
1426 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1428 | mov dword [BASE-4], LJ_TSTR
1429 | mov [BASE-8], STR:RC
1433 | mov RC, ~LJ_TLIGHTUD
1437 |//-- Base library: getters and setters ---------------------------------
1439 |.ffunc_1 getmetatable
1442 | cmp RB, LJ_TTAB; jne >6
1443 |1: // Field metatable must be at same offset for GCtab and GCudata!
1444 | mov TAB:RB, [BASE]
1445 | mov TAB:RB, TAB:RB->metatable
1447 | test TAB:RB, TAB:RB
1448 | mov dword [BASE-4], LJ_TNIL
1450 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
1451 | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
1452 | mov [BASE-8], TAB:RB
1453 | mov RA, TAB:RB->hmask
1454 | and RA, STR:RC->hash
1456 | add NODE:RA, TAB:RB->node
1457 |3: // Rearranged logic, because we expect _not_ to find the key.
1458 | cmp dword NODE:RA->key.it, LJ_TSTR
1460 | cmp dword NODE:RA->key.gcr, STR:RC
1463 | mov NODE:RA, NODE:RA->next
1464 | test NODE:RA, NODE:RA
1466 | jmp ->fff_res1 // Not found, keep default result.
1469 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1471 | mov [BASE-4], RB // Return value of mt.__metatable.
1476 | cmp RB, LJ_TUDATA; je <1
1478 | cmp RB, LJ_TNUMX; ja >8
1479 | cmp RB, LJ_TISNUM; jbe >7
1480 | mov RB, LJ_TLIGHTUD
1484 | cmp RB, LJ_TISNUM; ja >8
1489 | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1492 |.ffunc_2 setmetatable
1493 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1494 | // Fast path: no mt for table yet and not clearing the mt.
1495 | mov TAB:RB, [BASE]
1496 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1497 | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
1498 | mov TAB:RC, [BASE+8]
1499 | mov TAB:RB->metatable, TAB:RC
1501 | mov dword [BASE-4], LJ_TTAB // Return original table.
1502 | mov [BASE-8], TAB:RB
1503 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1505 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1506 | barrierback TAB:RB, RC
1511 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1513 | mov RB, BASE // Save BASE.
1514 | lea CARG3d, [BASE+8]
1515 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1516 | mov CARG1d, SAVE_L
1518 | mov RB, BASE // Save BASE.
1519 | mov CARG2d, [BASE]
1520 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1521 | mov CARG1d, SAVE_L
1523 | mov TAB:RD, [BASE]
1527 | mov RB, BASE // Save BASE.
1531 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1532 | // cTValue * returned in eax (RD).
1533 | mov BASE, RB // Restore BASE.
1534 | // Copy table slot.
1548 |//-- Base library: conversions ------------------------------------------
1551 | // Only handles the number case inline (without a base argument).
1552 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
1553 | cmp dword [BASE+4], LJ_TISNUM
1556 | mov RB, dword [BASE]; jmp ->fff_resi
1560 | jae ->fff_fallback
1562 | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
1565 | // Only handles the string or number case inline.
1567 | cmp dword [BASE+4], LJ_TSTR; jne >3
1568 | // A __tostring method in the string base metatable is ignored.
1569 | mov STR:RD, [BASE]
1571 | mov dword [BASE-4], LJ_TSTR
1572 | mov [BASE-8], STR:RD
1574 |3: // Handle numbers inline, unless a number base metatable is present.
1575 | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
1576 | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1577 | jne ->fff_fallback
1578 | ffgccheck // Caveat: uses label 1.
1580 | mov L:RB->base, BASE // Add frame since C call can throw.
1581 | mov SAVE_PC, PC // Redundant (but a defined value).
1582 |.if X64 and not X64WIN
1583 | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
1585 | mov L:FCARG1, L:RB
1587 | call extern lj_strfmt_number@8 // (lua_State *L, cTValue *o)
1589 | call extern lj_strfmt_num@8 // (lua_State *L, lua_Number *np)
1591 | // GCstr returned in eax (RD).
1592 | mov BASE, L:RB->base
1595 |//-- Base library: iterators -------------------------------------------
1598 | je >2 // Missing 2nd arg?
1600 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1602 | mov L:RB->base, BASE // Add frame since C call can throw.
1603 | mov L:RB->top, BASE // Dummy frame length is ok.
1606 | lea CARG3d, [BASE+8]
1607 | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
1610 | mov CARG2d, [BASE]
1611 | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
1614 | mov TAB:RD, [BASE]
1620 | mov SAVE_PC, PC // Needed for ITERN fallback.
1621 | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
1622 | // Flag returned in eax (RD).
1623 | mov BASE, L:RB->base
1624 | test RD, RD; jz >3 // End of traversal?
1625 | // Copy key and value to results.
1628 | mov RDa, [BASE+16]
1644 |2: // Set missing 2nd arg to nil.
1645 | mov dword [BASE+12], LJ_TNIL
1647 |3: // End of traversal: return nil.
1648 | mov dword [BASE-4], LJ_TNIL
1652 | mov TAB:RB, [BASE]
1653 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1655 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1657 | mov CFUNC:RB, [BASE-8]
1658 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1660 | mov dword [BASE-4], LJ_TFUNC
1661 | mov [BASE-8], CFUNC:RD
1662 | mov dword [BASE+12], LJ_TNIL
1666 |.ffunc_2 ipairs_aux
1667 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1668 | cmp dword [BASE+12], LJ_TISNUM
1670 | jne ->fff_fallback
1672 | jae ->fff_fallback
1676 | mov RD, dword [BASE+8]
1678 | mov dword [BASE-4], LJ_TISNUM
1679 | mov dword [BASE-8], RD
1681 | movsd xmm0, qword [BASE+8]
1682 | sseconst_1 xmm1, RBa
1684 | cvttsd2si RD, xmm0
1685 | movsd qword [BASE-8], xmm0
1687 | mov TAB:RB, [BASE]
1688 | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
1690 | add RD, TAB:RB->array
1692 | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
1693 | // Copy array slot.
1704 |2: // Check for empty hash part first. Otherwise call C function.
1705 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1706 | mov FCARG1, TAB:RB
1707 | mov RB, BASE // Save BASE.
1708 | mov FCARG2, RD // Caveat: FCARG2 == BASE
1709 | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
1710 | // cTValue * or NULL returned in eax (RD).
1719 | mov TAB:RB, [BASE]
1720 | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
1722 | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
1724 | mov CFUNC:RB, [BASE-8]
1725 | mov CFUNC:RD, CFUNC:RB->upvalue[0]
1727 | mov dword [BASE-4], LJ_TFUNC
1728 | mov [BASE-8], CFUNC:RD
1730 | mov dword [BASE+12], LJ_TISNUM
1731 | mov dword [BASE+8], 0
1734 | movsd qword [BASE+8], xmm0
1739 |//-- Base library: catch errors ----------------------------------------
1744 | mov PC, 8+FRAME_PCALL
1746 | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
1747 | shr RB, HOOK_ACTIVE_SHIFT
1749 | add PC, RB // Remember active hook before pcall.
1750 | jmp ->vm_call_dispatch
1753 | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
1754 | mov RB, [BASE+4] // Swap function and traceback.
1756 | mov dword [BASE+4], LJ_TFUNC
1757 | mov LFUNC:RB, [BASE]
1759 | mov [BASE+8], LFUNC:RB
1763 | mov PC, 16+FRAME_PCALL
1766 |//-- Coroutine library --------------------------------------------------
1768 |.macro coroutine_resume_wrap, resume
1770 |.ffunc_1 coroutine_resume
1773 |.ffunc coroutine_wrap_aux
1774 | mov CFUNC:RB, [BASE-8]
1775 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1785 | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
1787 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1788 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1790 | je >1 // Status != LUA_YIELD (i.e. 0)?
1791 | cmp RA, L:RB->base // Check for presence of initial func.
1795 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1797 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1799 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1803 | mov L:RB->base, BASE
1805 | add BASE, 8 // Keep resumed thread in stack for GC.
1807 | mov L:RB->top, BASE
1809 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1811 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1813 | sub RBa, PCa // Relative to PC.
1817 |2: // Move args to coroutine.
1840 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1846 | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
1848 | mov BASE, L:RB->base
1849 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
1850 | set_vmstate INTERP
1852 | cmp eax, LUA_YIELD
1855 | mov RA, L:PC->base
1856 | mov KBASE, L:PC->top
1857 | mov L:PC->top, RA // Clear coroutine stack.
1860 | je >6 // No results?
1863 | cmp RD, L:RB->maxstack
1864 | ja >9 // Need to grow stack?
1868 |5: // Move results from coroutine.
1883 | lea RD, [PC+2] // nresults+1 = 1 + true + results.
1884 | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
1886 | lea RD, [PC+1] // nresults+1 = 1 + results.
1896 | test PC, FRAME_TYPE
1900 |8: // Coroutine returned with error (at co->top-1).
1902 | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
1905 | mov L:PC->top, RA // Clear error from coroutine stack.
1906 | // Copy error message.
1916 | mov RD, 1+2 // nresults+1 = 1 + false + error.
1921 | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
1922 | // Error function does not return.
1925 |9: // Handle stack expansion on return from yield.
1929 | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
1931 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1934 | call extern lj_state_growstack@8 // (lua_State *L, int n)
1940 | mov BASE, L:RB->base
1941 | jmp <4 // Retry the stack move.
1944 | coroutine_resume_wrap 1 // coroutine.resume
1945 | coroutine_resume_wrap 0 // coroutine.wrap
1947 |.ffunc coroutine_yield
1949 | test aword L:RB->cframe, CFRAME_RESUME
1951 | mov L:RB->base, BASE
1952 | lea RD, [BASE+NARGS:RD*8-8]
1955 | mov aword L:RB->cframe, RDa
1957 | mov byte L:RB->status, al
1958 | jmp ->vm_leave_unw
1960 |//-- Math library -------------------------------------------------------
1963 |->fff_resi: // Dummy.
1968 | fstp qword [BASE-8]
1973 | cmp dword [BASE+4], LJ_TISNUM; jne >2
1974 | mov RB, dword [BASE]
1975 | cmp RB, 0; jns ->fff_resi
1980 | mov dword [BASE-4], LJ_TISNUM
1981 | mov dword [BASE-8], RB
1985 | mov dword [BASE-4], 0x41e00000 // 2^31.
1986 | mov dword [BASE-8], 0
1991 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
1993 | movsd xmm0, qword [BASE]
1994 | sseconst_abs xmm1, RDa
1998 | movsd qword [BASE-8], xmm0
2006 | test PC, FRAME_TYPE
2009 | cmp PC_RB, RDL // More results expected?
2011 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
2013 | not RAa // Note: ~RA = -(RA+1)
2014 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
2017 |6: // Fill up results with nil.
2018 | mov dword [BASE+RD*8-12], LJ_TNIL
2022 |7: // Non-standard return case.
2023 | mov RAa, -8 // Results start at BASE+RA = BASE-8.
2027 |.define fff_resfp, fff_resxmm0
2029 |.define fff_resfp, fff_resn
2032 |.macro math_round, func
2033 | .ffunc math_ .. func
2035 | cmp dword [BASE+4], LJ_TISNUM; jne >1
2036 | mov RB, dword [BASE]; jmp ->fff_resi
2040 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2042 | movsd xmm0, qword [BASE]
2043 | call ->vm_ .. func .. _sse
2045 | cvttsd2si RB, xmm0
2046 | cmp RB, 0x80000000
2049 | ucomisd xmm0, xmm1
2059 |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
2062 | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
2063 | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
2064 | movsd xmm0, qword [BASE]
2066 | movsd FPARG1, xmm0
2073 |.macro math_extern, func
2074 | .ffunc_nsse math_ .. func
2076 | movsd FPARG1, xmm0
2084 |.macro math_extern2, func
2085 | .ffunc_nnsse math_ .. func
2087 | movsd FPARG1, xmm0
2088 | movsd FPARG3, xmm1
2108 | math_extern2 atan2
2111 |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
2113 |.ffunc_1 math_frexp
2115 | cmp RB, LJ_TISNUM; jae ->fff_fallback
2118 | mov [BASE-4], RB; mov [BASE-8], RC
2119 | shl RB, 1; cmp RB, 0xffe00000; jae >3
2122 | cmp RB, 0x00200000; jb >4
2124 | shr RB, 21; sub RB, RC // Extract and unbias exponent.
2127 | and RB, 0x800fffff // Mask off exponent.
2128 | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
2131 | movsd qword [BASE], xmm0
2134 |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
2135 | xorps xmm0, xmm0; jmp <2
2136 |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
2137 | movsd xmm0, qword [BASE]
2138 | sseconst_hi xmm1, RBa, 43500000 // 2^54.
2140 | movsd qword [BASE-8], xmm0
2141 | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
2143 |.ffunc_nsse math_modf
2146 | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
2148 | call ->vm_trunc_sse
2151 | movsd qword [BASE-8], xmm0
2152 | movsd qword [BASE], xmm4
2153 | mov RC, [BASE-4]; mov RB, [BASE+4]
2154 | xor RC, RB; js >3 // Need to adjust sign?
2159 | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
2162 | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
2164 |.macro math_minmax, name, cmovop, sseop
2167 | cmp dword [BASE+4], LJ_TISNUM
2170 | mov RB, dword [BASE]
2171 |1: // Handle integers.
2172 | cmp RA, RD; jae ->fff_resi
2173 | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
2174 | cmp RB, dword [BASE+RA*8-8]
2175 | cmovop RB, dword [BASE+RA*8-8]
2180 | // Convert intermediate result to number and continue below.
2186 | jae ->fff_fallback
2189 | movsd xmm0, qword [BASE]
2190 |5: // Handle numbers or integers.
2191 | cmp RA, RD; jae ->fff_resxmm0
2192 | cmp dword [BASE+RA*8-4], LJ_TISNUM
2196 | cvtsi2sd xmm1, dword [BASE+RA*8-8]
2199 | jae ->fff_fallback
2202 | movsd xmm1, qword [BASE+RA*8-8]
2209 | math_minmax math_min, cmovg, minsd
2210 | math_minmax math_max, cmovl, maxsd
2212 |//-- String library -----------------------------------------------------
2214 |.ffunc string_byte // Only handle the 1-arg case here.
2215 | cmp NARGS:RD, 1+1; jne ->fff_fallback
2216 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2217 | mov STR:RB, [BASE]
2219 | cmp dword STR:RB->len, 1
2220 | jb ->fff_res0 // Return no results for empty string.
2221 | movzx RB, byte STR:RB[1]
2225 | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
2228 |.ffunc string_char // Only handle the 1-arg case here.
2230 | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
2231 | cmp dword [BASE+4], LJ_TISNUM
2233 | jne ->fff_fallback
2234 | mov RB, dword [BASE]
2235 | cmp RB, 255; ja ->fff_fallback
2238 | jae ->fff_fallback
2239 | cvttsd2si RB, qword [BASE]
2240 | cmp RB, 255; ja ->fff_fallback
2248 | lea RDa, TMP2 // Points to stack. Little-endian.
2251 | mov L:RB->base, BASE
2253 | mov CARG3d, TMP3 // Zero-extended to size_t.
2254 | mov CARG2, RDa // May be 64 bit ptr to stack.
2261 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
2263 | // GCstr * returned in eax (RD).
2264 | mov BASE, L:RB->base
2266 | mov dword [BASE-4], LJ_TSTR
2267 | mov [BASE-8], STR:RD
2273 | cmp NARGS:RD, 1+2; jb ->fff_fallback
2275 | cmp dword [BASE+20], LJ_TISNUM
2277 | jne ->fff_fallback
2278 | mov RB, dword [BASE+16]
2281 | jae ->fff_fallback
2282 | cvttsd2si RB, qword [BASE+16]
2286 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2287 | cmp dword [BASE+12], LJ_TISNUM
2289 | jne ->fff_fallback
2291 | jae ->fff_fallback
2293 | mov STR:RB, [BASE]
2295 | mov RB, STR:RB->len
2297 | mov RA, dword [BASE+8]
2299 | cvttsd2si RA, qword [BASE+8]
2302 | cmp RB, RC // len < end? (unsigned compare)
2305 | test RA, RA // start <= 0?
2309 | sub RC, RA // start > end?
2311 | lea RB, [STR:RB+RA+#STR-1]
2322 |5: // Negative end or overflow.
2324 | lea RC, [RC+RB+1] // end = end+(len+1)
2327 | mov RC, RB // end = len
2330 |7: // Negative start or underflow.
2332 | add RA, RB // start = start+(len+1)
2334 | jg <3 // start > 0?
2336 | mov RA, 1 // start = 1
2339 |->fff_emptystr: // Range underflow.
2340 | xor RC, RC // Zero length. Any ptr in RB is ok.
2343 |.macro ffstring_op, name
2344 | .ffunc_1 string_ .. name
2346 | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
2348 | lea SBUF:FCARG1, [DISPATCH+DISPATCH_GL(tmpbuf)]
2349 | mov L:RB->base, BASE
2350 | mov STR:FCARG2, [BASE] // Caveat: FCARG2 == BASE
2351 | mov RC, SBUF:FCARG1->b
2352 | mov SBUF:FCARG1->L, L:RB
2353 | mov SBUF:FCARG1->p, RC
2355 | call extern lj_buf_putstr_ .. name .. @8
2357 | call extern lj_buf_tostr@4
2361 |ffstring_op reverse
2365 |//-- Bit library --------------------------------------------------------
2367 |.macro .ffunc_bit, name, kind, fdef
2370 | sseconst_tobit xmm1, RBa
2372 | cmp dword [BASE+4], LJ_TISNUM
2375 | mov RB, dword [BASE]
2384 | jae ->fff_fallback
2386 | movsd xmm0, qword [BASE]
2388 | sseconst_tobit xmm1, RBa
2395 |.macro .ffunc_bit, name, kind
2396 | .ffunc_bit name, kind, .ffunc_1
2399 |.ffunc_bit bit_tobit, 0
2402 |.macro .ffunc_bit_op, name, ins
2403 | .ffunc_bit name, 2
2404 | mov TMP2, NARGS:RD // Save for fallback.
2405 | lea RD, [BASE+NARGS:RD*8-16]
2409 | cmp dword [RD+4], LJ_TISNUM
2412 | ins RB, dword [RD]
2416 | ja ->fff_fallback_bit_op
2418 | jae ->fff_fallback_bit_op
2420 | movsd xmm0, qword [RD]
2428 |.ffunc_bit_op bit_band, and
2429 |.ffunc_bit_op bit_bor, or
2430 |.ffunc_bit_op bit_bxor, xor
2432 |.ffunc_bit bit_bswap, 1
2436 |.ffunc_bit bit_bnot, 1
2446 |->fff_fallback_bit_op:
2447 | mov NARGS:RD, TMP2 // Restore for fallback
2448 | jmp ->fff_fallback
2450 |.macro .ffunc_bit_sh, name, ins
2452 | .ffunc_bit name, 1, .ffunc_2
2453 | // Note: no inline conversion from number for 2nd argument!
2454 | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
2455 | mov RA, dword [BASE+8]
2458 | sseconst_tobit xmm2, RBa
2464 | ins RB, cl // Assumes RA is ecx.
2468 |.ffunc_bit_sh bit_lshift, shl
2469 |.ffunc_bit_sh bit_rshift, shr
2470 |.ffunc_bit_sh bit_arshift, sar
2471 |.ffunc_bit_sh bit_rol, rol
2472 |.ffunc_bit_sh bit_ror, ror
2474 |//-----------------------------------------------------------------------
2477 | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
2478 | jmp ->fff_fallback
2480 | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
2481 |->fff_fallback: // Call fast function fallback handler.
2482 | // BASE = new base, RD = nargs+1
2484 | mov PC, [BASE-4] // Fallback may overwrite PC.
2485 | mov SAVE_PC, PC // Redundant (but a defined value).
2486 | mov L:RB->base, BASE
2487 | lea RD, [BASE+NARGS:RD*8-8]
2488 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2490 | mov CFUNC:RD, [BASE-8]
2491 | cmp RA, L:RB->maxstack
2492 | ja >5 // Need to grow stack.
2498 | call aword CFUNC:RD->f // (lua_State *L)
2499 | mov BASE, L:RB->base
2500 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2501 | test RD, RD; jg ->fff_res // Returned nresults+1?
2507 | lea NARGS:RD, [RA+1]
2508 | mov LFUNC:RB, [BASE-8]
2509 | jne ->vm_call_tail // Returned -1?
2510 | ins_callt // Returned 0: retry fast path.
2512 |// Reconstruct previous base for vmeta_call during tailcall.
2515 | test PC, FRAME_TYPE
2518 | not RBa // Note: ~RB = -(RB+1)
2519 | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
2520 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2525 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2527 |5: // Grow stack for fallback handler.
2528 | mov FCARG2, LUA_MINSTACK
2530 | call extern lj_state_growstack@8 // (lua_State *L, int n)
2531 | mov BASE, L:RB->base
2532 | xor RD, RD // Simulate a return 0.
2533 | jmp <1 // Dumb retry (goes through ff first).
2535 |->fff_gcstep: // Call GC step function.
2536 | // BASE = new base, RD = nargs+1
2537 | pop RBa // Must keep stack at same level.
2538 | mov TMPa, RBa // Save return address
2540 | mov SAVE_PC, PC // Redundant (but a defined value).
2541 | mov L:RB->base, BASE
2542 | lea RD, [BASE+NARGS:RD*8-8]
2545 | call extern lj_gc_step@4 // (lua_State *L)
2546 | mov BASE, L:RB->base
2552 | push RBa // Restore return address.
2555 |//-----------------------------------------------------------------------
2556 |//-- Special dispatch targets -------------------------------------------
2557 |//-----------------------------------------------------------------------
2559 |->vm_record: // Dispatch target for recording phase.
2561 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2562 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2564 | // Decrement the hookcount for consistency, but always do the call.
2565 | test RDL, HOOK_ACTIVE
2567 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2569 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2573 |->vm_rethook: // Dispatch target for return hooks.
2574 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2575 | test RDL, HOOK_ACTIVE // Hook already active?
2579 |->vm_inshook: // Dispatch target for instr/line hooks.
2580 | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
2581 | test RDL, HOOK_ACTIVE // Hook already active?
2584 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2586 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2588 | test RDL, LUA_MASKLINE
2592 | mov L:RB->base, BASE
2593 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2595 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2596 | call extern lj_dispatch_ins@8 // (lua_State *L, const BCIns *pc)
2598 | mov BASE, L:RB->base
2605 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2607 | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
2610 |->cont_hook: // Continue from hook yield.
2613 | mov MULTRES, RA // Restore MULTRES for *M ins.
2616 |->vm_hotloop: // Hot loop counter underflow.
2618 | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
2619 | mov RB, LFUNC:RB->pc
2620 | movzx RD, byte [RB+PC2PROTO(framesize)]
2621 | lea RD, [BASE+RD*8]
2623 | mov L:RB->base, BASE
2626 | lea FCARG1, [DISPATCH+GG_DISP2J]
2627 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2629 | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
2633 |->vm_callhook: // Dispatch target for call hooks.
2639 |->vm_hotcall: // Hot call counter underflow.
2642 | or PC, 1 // Marker for hot call.
2645 | lea RD, [BASE+NARGS:RD*8-8]
2647 | mov L:RB->base, BASE
2651 | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
2652 | // ASMFunction returned in eax/rax (RDa).
2653 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2657 | mov BASE, L:RB->base
2667 |->cont_stitch: // Trace stitching.
2669 | // BASE = base, RC = result, RB = mbase
2670 | mov RA, [RB-24] // Save previous trace number.
2672 | mov TMP3, DISPATCH // Need one more register.
2673 | mov DISPATCH, MULTRES
2675 | lea RA, [BASE+RA*8] // Call base.
2678 |1: // Move results down.
2696 | lea RC, [BASE+RC*8-8]
2699 | ja >9 // More results wanted?
2701 | mov DISPATCH, TMP3
2702 | mov RB, TMP1 // Get previous trace number.
2703 | mov RA, [DISPATCH+DISPATCH_J(trace)]
2704 | mov TRACE:RD, [RA+RB*4]
2705 | test TRACE:RD, TRACE:RD
2707 | movzx RD, word TRACE:RD->link
2709 | je ->cont_nop // Blacklisted.
2711 | jne =>BC_JLOOP // Jump to stitched trace.
2713 | // Stitch a new trace to the previous trace.
2714 | mov [DISPATCH+DISPATCH_J(exitno)], RB
2716 | mov L:RB->base, BASE
2718 | lea FCARG1, [DISPATCH+GG_DISP2J]
2719 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2720 | call extern lj_dispatch_stitch@8 // (jit_State *J, const BCIns *pc)
2721 | mov BASE, L:RB->base
2724 |9: // Fill up results with nil.
2725 | mov dword [RA+4], LJ_TNIL
2730 |->vm_profhook: // Dispatch target for profiler hook.
2733 | mov L:RB->base, BASE
2734 | mov FCARG2, PC // Caveat: FCARG2 == BASE
2736 | call extern lj_dispatch_profile@8 // (lua_State *L, const BCIns *pc)
2737 | mov BASE, L:RB->base
2738 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2743 |//-----------------------------------------------------------------------
2744 |//-- Trace exit handler -------------------------------------------------
2745 |//-----------------------------------------------------------------------
2747 |// Called from an exit stub with the exit number on the stack.
2748 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2752 | push r13; push r12
2753 | push r11; push r10; push r9; push r8
2754 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2755 | push rbx; push rdx; push rcx; push rax
2756 | movzx RC, byte [rbp-8] // Reconstruct exit number.
2757 | mov RCH, byte [rbp-16]
2758 | mov [rbp-8], r15; mov [rbp-16], r14
2760 | push ebp; lea ebp, [esp+12]; push ebp
2761 | push ebx; push edx; push ecx; push eax
2762 | movzx RC, byte [ebp-4] // Reconstruct exit number.
2763 | mov RCH, byte [ebp-8]
2764 | mov [ebp-4], edi; mov [ebp-8], esi
2766 | // Caveat: DISPATCH is ebx.
2767 | mov DISPATCH, [ebp]
2768 | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2770 | mov [DISPATCH+DISPATCH_J(exitno)], RC
2771 | mov [DISPATCH+DISPATCH_J(parent)], RA
2774 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2776 | sub rsp, 16*8 // Room for SSE regs.
2779 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2780 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2781 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2782 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2783 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2784 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2785 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2786 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2788 | sub esp, 8*8+16 // Room for SSE regs + args.
2789 | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
2790 | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
2791 | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
2792 | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
2794 | // Caveat: RB is ebp.
2795 | mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)]
2796 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2797 | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
2798 | mov L:RB->base, BASE
2800 | lea CARG2, [rsp+4*8]
2804 | lea FCARG2, [esp+16]
2806 | lea FCARG1, [DISPATCH+GG_DISP2J]
2807 | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2808 | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
2809 | // MULTRES or negated error code returned in eax (RD).
2810 | mov RAa, L:RB->cframe
2811 | and RAa, CFRAME_RAWMASK
2813 | // Reposition stack later.
2815 | mov rsp, RAa // Reposition stack to C frame.
2817 | mov esp, RAa // Reposition stack to C frame.
2819 | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
2820 | mov BASE, L:RB->base
2821 | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
2827 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2830 | // Restore additional callee-save registers only used in compiled code.
2832 | lea RAa, [rsp+9*16+4*8]
2834 | movdqa xmm15, [RAa-9*16]
2835 | movdqa xmm14, [RAa-8*16]
2836 | movdqa xmm13, [RAa-7*16]
2837 | movdqa xmm12, [RAa-6*16]
2838 | movdqa xmm11, [RAa-5*16]
2839 | movdqa xmm10, [RAa-4*16]
2840 | movdqa xmm9, [RAa-3*16]
2841 | movdqa xmm8, [RAa-2*16]
2842 | movdqa xmm7, [RAa-1*16]
2843 | mov rsp, RAa // Reposition stack to C frame.
2844 | movdqa xmm6, [RAa]
2848 | add rsp, 16 // Reposition stack to C frame.
2854 | test RD, RD; js >9 // Check for error from exit.
2857 | mov LFUNC:KBASE, [BASE-8]
2858 | mov KBASE, LFUNC:KBASE->pc
2859 | mov KBASE, [KBASE+PC2PROTO(k)]
2860 | mov L:RB->base, BASE
2861 | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0
2862 | set_vmstate INTERP
2863 | // Modified copy of ins_next which handles function header dispatch, too.
2869 | cmp OP, BC_FUNCF // Function header?
2871 | cmp OP, BC_FUNCC+2 // Fast function?
2874 | mov RC, MULTRES // RC/RD holds nres+1.
2877 | jmp aword [DISPATCH+OP*8]
2879 | jmp aword [DISPATCH+OP*4]
2882 |4: // Check frame below fast function.
2884 | test RC, FRAME_TYPE
2885 | jnz <2 // Trace stitching continuation?
2886 | // Otherwise set KBASE for Lua function below fast function.
2887 | movzx RC, byte [RC-3]
2889 | mov LFUNC:KBASE, [BASE+RC*8-8]
2890 | mov KBASE, LFUNC:KBASE->pc
2891 | mov KBASE, [KBASE+PC2PROTO(k)]
2894 |9: // Rethrow error from the right C frame.
2898 | call extern lj_err_throw@8 // (lua_State *L, int errcode)
2901 |//-----------------------------------------------------------------------
2902 |//-- Math helper functions ----------------------------------------------
2903 |//-----------------------------------------------------------------------
2905 |// FP value rounding. Called by math.floor/math.ceil fast functions
2906 |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
2907 |.macro vm_round, name, mode, cond
2909 |.if not X64 and cond
2910 | movsd xmm0, qword [esp+4]
2911 | call ->name .. _sse
2912 | movsd qword [esp+4], xmm0 // Overwrite callee-owned arg.
2918 | sseconst_abs xmm2, RDa
2919 | sseconst_2p52 xmm3, RDa
2921 | andpd xmm1, xmm2 // |x|
2922 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
2924 | andnpd xmm2, xmm0 // Isolate sign bit.
2925 |.if mode == 2 // trunc(x)?
2927 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
2929 | sseconst_1 xmm3, RDa
2930 | cmpsd xmm0, xmm1, 1 // |x| < result?
2932 | subsd xmm1, xmm0 // If yes, subtract -1.
2933 | orpd xmm1, xmm2 // Merge sign bit back in.
2935 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
2937 | orpd xmm1, xmm2 // Merge sign bit back in.
2938 | .if mode == 1 // ceil(x)?
2939 | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0.
2940 | cmpsd xmm0, xmm1, 6 // x > result?
2941 | .else // floor(x)?
2942 | sseconst_1 xmm2, RDa
2943 | cmpsd xmm0, xmm1, 1 // x < result?
2946 | subsd xmm1, xmm0 // If yes, subtract +-1.
2953 | vm_round vm_floor, 0, 1
2954 | vm_round vm_ceil, 1, JIT
2955 | vm_round vm_trunc, 2, JIT
2957 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
2959 |// Args in xmm0/xmm1, return value in xmm0.
2960 |// Caveat: xmm0-xmm5 and RC (eax) modified!
2963 | sseconst_abs xmm2, RDa
2964 | sseconst_2p52 xmm3, RDa
2966 | andpd xmm4, xmm2 // |x/y|
2967 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
2969 | andnpd xmm2, xmm0 // Isolate sign bit.
2970 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
2972 | orpd xmm4, xmm2 // Merge sign bit back in.
2973 | sseconst_1 xmm2, RDa
2974 | cmpsd xmm0, xmm4, 1 // x/y < result?
2976 | subsd xmm4, xmm0 // If yes, subtract 1.0.
2987 |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
2989 | cmp eax, 1; jle >6 // i<=1?
2990 | // Now 1 < (unsigned)i <= 0x80000000.
2991 |1: // Handle leading zeros.
2992 | test eax, 1; jnz >2
2999 |3: // Handle trailing bits.
3010 | je <5 // x^1 ==> x
3011 | jb >7 // x^0 ==> 1
3014 | sseconst_1 xmm1, RDa
3019 | sseconst_1 xmm0, RDa
3022 |//-----------------------------------------------------------------------
3023 |//-- Miscellaneous functions --------------------------------------------
3024 |//-----------------------------------------------------------------------
3026 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
3030 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
3038 | .if X64WIN; pop rsi; .endif
3044 | xor edx, 0x00200000 // Toggle ID bit in flags.
3049 | xor eax, eax // Zero means no features supported.
3051 | jz >1 // No ID toggle means no CPUID support.
3052 | mov eax, [esp+4] // Argument 1 is function number.
3056 | mov edi, [esp+16] // Argument 2 is result area.
3067 |//-----------------------------------------------------------------------
3068 |//-- Assertions ---------------------------------------------------------
3069 |//-----------------------------------------------------------------------
3071 |->assert_bad_for_arg_type:
3072 #ifdef LUA_USE_ASSERT
3077 |//-----------------------------------------------------------------------
3078 |//-- FFI helper functions -----------------------------------------------
3079 |//-----------------------------------------------------------------------
3081 |// Handler for callback functions. Callback slot number in ah/al.
3084 |.type CTSTATE, CTState, PC
3086 | sub esp, 16 // Leave room for SAVE_ERRF etc.
3088 | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
3089 | lea DISPATCH, [ebp+GG_G2DISP]
3090 | mov CTSTATE, GL:ebp->ctype_state
3092 | mov CTSTATE->cb.slot, eax
3094 | mov CTSTATE->cb.gpr[0], CARG1
3095 | mov CTSTATE->cb.gpr[1], CARG2
3096 | mov CTSTATE->cb.gpr[2], CARG3
3097 | mov CTSTATE->cb.gpr[3], CARG4
3098 | movsd qword CTSTATE->cb.fpr[0], xmm0
3099 | movsd qword CTSTATE->cb.fpr[1], xmm1
3100 | movsd qword CTSTATE->cb.fpr[2], xmm2
3101 | movsd qword CTSTATE->cb.fpr[3], xmm3
3103 | lea rax, [rsp+CFRAME_SIZE+4*8]
3105 | lea rax, [rsp+CFRAME_SIZE]
3106 | mov CTSTATE->cb.gpr[4], CARG5
3107 | mov CTSTATE->cb.gpr[5], CARG6
3108 | movsd qword CTSTATE->cb.fpr[4], xmm4
3109 | movsd qword CTSTATE->cb.fpr[5], xmm5
3110 | movsd qword CTSTATE->cb.fpr[6], xmm6
3111 | movsd qword CTSTATE->cb.fpr[7], xmm7
3113 | mov CTSTATE->cb.stack, rax
3116 | lea eax, [esp+CFRAME_SIZE+16]
3117 | mov CTSTATE->cb.gpr[0], FCARG1
3118 | mov CTSTATE->cb.gpr[1], FCARG2
3119 | mov CTSTATE->cb.stack, eax
3120 | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp.
3121 | mov FCARG2, [esp+CFRAME_SIZE+8]
3122 | mov SAVE_RET, FCARG1
3123 | mov SAVE_R4, FCARG2
3126 | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
3127 | mov FCARG1, CTSTATE
3128 | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf)
3129 | // lua_State * returned in eax (RD).
3130 | set_vmstate INTERP
3131 | mov BASE, L:RD->base
3134 | mov LFUNC:RB, [BASE-8]
3140 |->cont_ffi_callback: // Return from FFI callback.
3143 | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
3144 | mov aword CTSTATE->L, L:RAa
3145 | mov L:RA->base, BASE
3147 | mov FCARG1, CTSTATE
3149 | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o)
3151 | mov rax, CTSTATE->cb.gpr[0]
3152 | movsd xmm0, qword CTSTATE->cb.fpr[0]
3153 | jmp ->vm_leave_unw
3156 | mov eax, CTSTATE->cb.gpr[0]
3157 | mov edx, CTSTATE->cb.gpr[1]
3158 | cmp dword CTSTATE->cb.gpr[2], 1
3161 | fld qword CTSTATE->cb.fpr[0].d
3164 | fld dword CTSTATE->cb.fpr[0].f
3166 | mov ecx, L:RB->top
3167 | movzx ecx, word [ecx+6] // Get stack adjustment and copy up.
3168 | mov SAVE_L, ecx // Must be one slot above SAVE_RET
3170 | pop ecx // Move return addr from SAVE_RET.
3171 | add esp, [esp] // Adjust stack.
3178 |->vm_ffi_call@4: // Call C function via FFI.
3179 | // Caveat: needs special frame unwinding, see below.
3182 | .type CCSTATE, CCallState, rbx
3183 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3185 | .type CCSTATE, CCallState, ebx
3186 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3189 | // Readjust stack.
3191 | mov eax, CCSTATE->spadj
3194 | sub esp, CCSTATE->spadj
3196 | mov CCSTATE->spadj, esp
3200 | // Copy stack slots.
3201 | movzx ecx, byte CCSTATE->nsp
3206 | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3207 | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3209 | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3210 | mov [esp+ecx*4], eax
3217 | movzx eax, byte CCSTATE->nfpr
3218 | mov CARG1, CCSTATE->gpr[0]
3219 | mov CARG2, CCSTATE->gpr[1]
3220 | mov CARG3, CCSTATE->gpr[2]
3221 | mov CARG4, CCSTATE->gpr[3]
3223 | mov CARG5, CCSTATE->gpr[4]
3224 | mov CARG6, CCSTATE->gpr[5]
3226 | test eax, eax; jz >5
3227 | movaps xmm0, CCSTATE->fpr[0]
3228 | movaps xmm1, CCSTATE->fpr[1]
3229 | movaps xmm2, CCSTATE->fpr[2]
3230 | movaps xmm3, CCSTATE->fpr[3]
3232 | cmp eax, 4; jbe >5
3233 | movaps xmm4, CCSTATE->fpr[4]
3234 | movaps xmm5, CCSTATE->fpr[5]
3235 | movaps xmm6, CCSTATE->fpr[6]
3236 | movaps xmm7, CCSTATE->fpr[7]
3240 | mov FCARG1, CCSTATE->gpr[0]
3241 | mov FCARG2, CCSTATE->gpr[1]
3244 | call aword CCSTATE->func
3247 | mov CCSTATE->gpr[0], rax
3248 | movaps CCSTATE->fpr[0], xmm0
3250 | mov CCSTATE->gpr[1], rdx
3251 | movaps CCSTATE->fpr[1], xmm1
3254 | mov CCSTATE->gpr[0], eax
3255 | mov CCSTATE->gpr[1], edx
3256 | cmp byte CCSTATE->resx87, 1
3259 | fstp qword CCSTATE->fpr[0].d[0]
3262 | fstp dword CCSTATE->fpr[0].f[0]
3265 | sub CCSTATE->spadj, esp
3270 | mov rbx, [rbp-8]; leave; ret
3272 | mov ebx, [ebp-4]; leave; ret
3275 |// Note: vm_ffi_call must be the last function in this object file!
3277 |//-----------------------------------------------------------------------
3280 /* Generate the code for a single instruction. */
3281 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
3284 |// Note: aligning all instructions does not pay off.
3289 /* -- Comparison ops ---------------------------------------------------- */
3291 /* Remember: all ops branch for a true comparison, fall through otherwise. */
3293 |.macro jmp_comp, lt, ge, le, gt, target
3307 ||default: break; /* Shut up GCC. */
3311 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
3312 | // RA = src1, RD = src2, JMP with RD = target
3317 | mov RB, dword [BASE+RA*8]
3319 | cmp RB, dword [BASE+RD*8]
3320 | jmp_comp jge, jl, jg, jle, >9
3327 |7: // RA is not an integer.
3329 | // RA is a number.
3330 | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
3331 | // RA is a number, RD is an integer.
3332 | cvtsi2sd xmm0, dword [BASE+RD*8]
3335 |8: // RA is an integer, RD is not an integer.
3337 | // RA is an integer, RD is a number.
3338 | cvtsi2sd xmm1, dword [BASE+RA*8]
3339 | movsd xmm0, qword [BASE+RD*8]
3341 | ucomisd xmm0, xmm1
3342 | jmp_comp jbe, ja, jb, jae, <9
3345 | checknum RA, ->vmeta_comp
3346 | checknum RD, ->vmeta_comp
3349 | movsd xmm0, qword [BASE+RD*8]
3352 | ucomisd xmm0, qword [BASE+RA*8]
3354 | // Unordered: all of ZF CF PF set, ordered: PF clear.
3355 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
3357 | jmp_comp jbe, ja, jb, jae, <9
3360 | jmp_comp jbe, ja, jb, jae, >1
3368 case BC_ISEQV: case BC_ISNEV:
3369 vk = op == BC_ISEQV;
3370 | ins_AD // RA = src1, RD = src2, JMP with RD = target
3371 | mov RB, [BASE+RD*8+4]
3374 | cmp RB, LJ_TISNUM; jne >7
3376 | mov RB, dword [BASE+RD*8]
3377 | cmp RB, dword [BASE+RA*8]
3388 |7: // RD is not an integer.
3390 | // RD is a number.
3391 | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
3392 | // RD is a number, RA is an integer.
3393 | cvtsi2sd xmm0, dword [BASE+RA*8]
3396 |8: // RD is an integer, RA is not an integer.
3398 | // RD is an integer, RA is a number.
3399 | cvtsi2sd xmm0, dword [BASE+RD*8]
3400 | ucomisd xmm0, qword [BASE+RA*8]
3404 | cmp RB, LJ_TISNUM; jae >5
3408 | movsd xmm0, qword [BASE+RA*8]
3410 | ucomisd xmm0, qword [BASE+RD*8]
3414 | jp >2 // Unordered means not equal.
3417 | jp >2 // Unordered means not equal.
3422 |1: // EQ: Branch to the target.
3425 |2: // NE: Fallthrough to next instruction.
3433 |2: // NE: Branch to the target.
3436 |1: // EQ: Fallthrough to next instruction.
3438 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
3439 op == BC_ISEQN || op == BC_ISNEN)) {
3445 if (op == BC_ISEQV || op == BC_ISNEV) {
3446 |5: // Either or both types are not numbers.
3448 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
3449 | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
3451 | checktp RA, RB // Compare types.
3452 | jne <2 // Not the same type?
3454 | jae <1 // Same type and primitive type?
3456 | // Same types and not a primitive type. Compare GCobj or pvalue.
3457 | mov RA, [BASE+RA*8]
3458 | mov RD, [BASE+RD*8]
3460 | je <1 // Same GCobjs or pvalues?
3461 | cmp RB, LJ_TISTABUD
3462 | ja <2 // Different objects and not table/ud?
3464 | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
3468 | // Different tables or userdatas. Need to check __eq metamethod.
3469 | // Field metatable must be at same offset for GCtab and GCudata!
3470 | mov TAB:RB, TAB:RA->metatable
3471 | test TAB:RB, TAB:RB
3472 | jz <2 // No metatable?
3473 | test byte TAB:RB->nomm, 1<<MM_eq
3474 | jnz <2 // Or 'no __eq' flag set?
3476 | xor RB, RB // ne = 0
3478 | mov RB, 1 // ne = 1
3480 | jmp ->vmeta_equal // Handle __eq metamethod.
3485 if (LJ_DUALNUM && vk) {
3490 | jmp ->vmeta_equal_cd
3494 case BC_ISEQS: case BC_ISNES:
3495 vk = op == BC_ISEQS;
3496 | ins_AND // RA = src, RD = str const, JMP with RD = target
3497 | mov RB, [BASE+RA*8+4]
3499 | cmp RB, LJ_TSTR; jne >3
3500 | mov RA, [BASE+RA*8]
3501 | cmp RA, [KBASE+RD*4]
3509 case BC_ISEQN: case BC_ISNEN:
3510 vk = op == BC_ISEQN;
3511 | ins_AD // RA = src, RD = num const, JMP with RD = target
3512 | mov RB, [BASE+RA*8+4]
3515 | cmp RB, LJ_TISNUM; jne >7
3516 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
3517 | mov RB, dword [KBASE+RD*8]
3518 | cmp RB, dword [BASE+RA*8]
3529 |7: // RA is not an integer.
3531 | // RA is a number.
3532 | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
3533 | // RA is a number, RD is an integer.
3534 | cvtsi2sd xmm0, dword [KBASE+RD*8]
3537 |8: // RA is an integer, RD is a number.
3538 | cvtsi2sd xmm0, dword [BASE+RA*8]
3539 | ucomisd xmm0, qword [KBASE+RD*8]
3542 | cmp RB, LJ_TISNUM; jae >3
3545 | movsd xmm0, qword [KBASE+RD*8]
3547 | ucomisd xmm0, qword [BASE+RA*8]
3550 case BC_ISEQP: case BC_ISNEP:
3551 vk = op == BC_ISEQP;
3552 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
3553 | mov RB, [BASE+RA*8+4]
3556 if (!LJ_HASFFI) goto iseqne_test;
3564 | cmp RB, LJ_TCDATA; jne <2
3565 | jmp ->vmeta_equal_cd
3568 | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
3576 /* -- Unary test and copy ops ------------------------------------------- */
3578 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3579 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
3580 | mov RB, [BASE+RD*8+4]
3582 | cmp RB, LJ_TISTRUECOND
3583 if (op == BC_IST || op == BC_ISTC) {
3588 if (op == BC_ISTC || op == BC_ISFC) {
3589 | mov [BASE+RA*8+4], RB
3590 | mov RB, [BASE+RD*8]
3591 | mov [BASE+RA*8], RB
3595 |1: // Fallthrough to the next instruction.
3600 | ins_AD // RA = src, RD = -type
3601 | add RD, [BASE+RA*8+4]
3602 | jne ->vmeta_istype
3606 | ins_AD // RA = src, RD = -(TISNUM-1)
3607 | checknum RA, ->vmeta_istype
3611 /* -- Unary ops --------------------------------------------------------- */
3614 | ins_AD // RA = dst, RD = src
3616 | mov RBa, [BASE+RD*8]
3617 | mov [BASE+RA*8], RBa
3619 | mov RB, [BASE+RD*8+4]
3620 | mov RD, [BASE+RD*8]
3621 | mov [BASE+RA*8+4], RB
3622 | mov [BASE+RA*8], RD
3627 | ins_AD // RA = dst, RD = src
3629 | checktp RD, LJ_TISTRUECOND
3631 | mov [BASE+RA*8+4], RB
3635 | ins_AD // RA = dst, RD = src
3638 | mov RB, [BASE+RD*8]
3641 | mov dword [BASE+RA*8+4], LJ_TISNUM
3642 | mov dword [BASE+RA*8], RB
3646 | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
3647 | mov dword [BASE+RA*8], 0
3652 | checknum RD, ->vmeta_unm
3654 | movsd xmm0, qword [BASE+RD*8]
3655 | sseconst_sign xmm1, RDa
3657 | movsd qword [BASE+RA*8], xmm0
3665 | ins_AD // RA = dst, RD = src
3667 | mov STR:RD, [BASE+RD*8]
3669 | mov RD, dword STR:RD->len
3671 | mov dword [BASE+RA*8+4], LJ_TISNUM
3672 | mov dword [BASE+RA*8], RD
3675 | cvtsi2sd xmm0, dword STR:RD->len
3677 | movsd qword [BASE+RA*8], xmm0
3681 | checktab RD, ->vmeta_len
3682 | mov TAB:FCARG1, [BASE+RD*8]
3684 | mov TAB:RB, TAB:FCARG1->metatable
3690 | mov RB, BASE // Save BASE.
3691 | call extern lj_tab_len@4 // (GCtab *t)
3692 | // Length of table returned in eax (RD).
3698 | mov BASE, RB // Restore BASE.
3702 |9: // Check for __len.
3703 | test byte TAB:RB->nomm, 1<<MM_len
3705 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
3709 /* -- Binary ops -------------------------------------------------------- */
3711 |.macro ins_arithpre, sseins, ssereg
3713 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3716 | checknum RB, ->vmeta_arith_vn
3718 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
3720 | movsd xmm0, qword [BASE+RB*8]
3721 | sseins ssereg, qword [KBASE+RC*8]
3724 | checknum RB, ->vmeta_arith_nv
3726 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
3728 | movsd xmm0, qword [KBASE+RC*8]
3729 | sseins ssereg, qword [BASE+RB*8]
3732 | checknum RB, ->vmeta_arith_vv
3733 | checknum RC, ->vmeta_arith_vv
3734 | movsd xmm0, qword [BASE+RB*8]
3735 | sseins ssereg, qword [BASE+RC*8]
3740 |.macro ins_arithdn, intins
3742 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3745 | checkint RB, ->vmeta_arith_vn
3746 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
3747 | mov RB, [BASE+RB*8]
3748 | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
3751 | checkint RB, ->vmeta_arith_nv
3752 | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
3753 | mov RC, [KBASE+RC*8]
3754 | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
3757 | checkint RB, ->vmeta_arith_vv
3758 | checkint RC, ->vmeta_arith_vv
3759 | mov RB, [BASE+RB*8]
3760 | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
3763 | mov dword [BASE+RA*8+4], LJ_TISNUM
3765 | mov dword [BASE+RA*8], RC
3767 | mov dword [BASE+RA*8], RB
3772 |.macro ins_arithpost
3773 | movsd qword [BASE+RA*8], xmm0
3776 |.macro ins_arith, sseins
3777 | ins_arithpre sseins, xmm0
3782 |.macro ins_arith, intins, sseins
3784 | ins_arithdn intins
3790 | // RA = dst, RB = src1 or num const, RC = src2 or num const
3791 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3792 | ins_arith add, addsd
3794 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3795 | ins_arith sub, subsd
3797 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3798 | ins_arith imul, mulsd
3800 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3804 | ins_arithpre movsd, xmm1
3810 case BC_MODNV: case BC_MODVV:
3811 | ins_arithpre movsd, xmm1
3812 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3815 | ins_arithpre movsd, xmm1
3818 | movsd FPARG1, xmm0
3819 | movsd FPARG3, xmm1
3827 | fstp qword [BASE+RA*8]
3833 | ins_ABC // RA = dst, RB = src_start, RC = src_end
3835 | mov L:CARG1d, SAVE_L
3836 | mov L:CARG1d->base, BASE
3837 | lea CARG2d, [BASE+RC*8]
3841 | mov L:RB, L:CARG1d
3843 | lea RA, [BASE+RC*8]
3850 | mov L:RB->base, BASE
3853 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3854 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
3855 | mov BASE, L:RB->base
3858 | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
3861 | mov RCa, [BASE+RB*8]
3862 | mov [BASE+RA*8], RCa
3864 | mov RC, [BASE+RB*8+4]
3865 | mov RB, [BASE+RB*8]
3866 | mov [BASE+RA*8+4], RC
3867 | mov [BASE+RA*8], RB
3872 /* -- Constant ops ------------------------------------------------------ */
3875 | ins_AND // RA = dst, RD = str const (~)
3876 | mov RD, [KBASE+RD*4]
3877 | mov dword [BASE+RA*8+4], LJ_TSTR
3878 | mov [BASE+RA*8], RD
3883 | ins_AND // RA = dst, RD = cdata const (~)
3884 | mov RD, [KBASE+RD*4]
3885 | mov dword [BASE+RA*8+4], LJ_TCDATA
3886 | mov [BASE+RA*8], RD
3891 | ins_AD // RA = dst, RD = signed int16 literal
3894 | mov dword [BASE+RA*8+4], LJ_TISNUM
3895 | mov dword [BASE+RA*8], RD
3897 | movsx RD, RDW // Sign-extend literal.
3899 | movsd qword [BASE+RA*8], xmm0
3904 | ins_AD // RA = dst, RD = num const
3905 | movsd xmm0, qword [KBASE+RD*8]
3906 | movsd qword [BASE+RA*8], xmm0
3910 | ins_AND // RA = dst, RD = primitive type (~)
3911 | mov [BASE+RA*8+4], RD
3915 | ins_AD // RA = dst_start, RD = dst_end
3916 | lea RA, [BASE+RA*8+12]
3917 | lea RD, [BASE+RD*8+4]
3919 | mov [RA-8], RB // Sets minimum 2 slots.
3928 /* -- Upvalue and function ops ------------------------------------------ */
3931 | ins_AD // RA = dst, RD = upvalue #
3932 | mov LFUNC:RB, [BASE-8]
3933 | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
3934 | mov RB, UPVAL:RB->v
3937 | mov [BASE+RA*8], RDa
3941 | mov [BASE+RA*8+4], RD
3942 | mov [BASE+RA*8], RB
3947 #define TV2MARKOFS \
3948 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
3949 | ins_AD // RA = upvalue #, RD = src
3950 | mov LFUNC:RB, [BASE-8]
3951 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
3952 | cmp byte UPVAL:RB->closed, 0
3953 | mov RB, UPVAL:RB->v
3954 | mov RA, [BASE+RD*8]
3955 | mov RD, [BASE+RD*8+4]
3959 | // Check barrier for closed upvalue.
3960 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
3965 |2: // Upvalue is black. Check if new value is collectable and white.
3967 | cmp RD, LJ_TNUMX - LJ_TISGCV // tvisgcv(v)
3969 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
3971 | // Crossed a write barrier. Move the barrier forward.
3972 |.if X64 and not X64WIN
3974 | mov RB, BASE // Save BASE.
3976 | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
3978 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
3979 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
3980 | mov BASE, RB // Restore BASE.
3985 | ins_AND // RA = upvalue #, RD = str const (~)
3986 | mov LFUNC:RB, [BASE-8]
3987 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
3988 | mov GCOBJ:RA, [KBASE+RD*4]
3989 | mov RD, UPVAL:RB->v
3990 | mov [RD], GCOBJ:RA
3991 | mov dword [RD+4], LJ_TSTR
3992 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
3997 |2: // Check if string is white and ensure upvalue is closed.
3998 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
4000 | cmp byte UPVAL:RB->closed, 0
4002 | // Crossed a write barrier. Move the barrier forward.
4003 | mov RB, BASE // Save BASE (FCARG2 == BASE).
4005 | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
4006 | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
4007 | mov BASE, RB // Restore BASE.
4011 | ins_AD // RA = upvalue #, RD = num const
4012 | mov LFUNC:RB, [BASE-8]
4013 | movsd xmm0, qword [KBASE+RD*8]
4014 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4015 | mov RA, UPVAL:RB->v
4016 | movsd qword [RA], xmm0
4020 | ins_AND // RA = upvalue #, RD = primitive type (~)
4021 | mov LFUNC:RB, [BASE-8]
4022 | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
4023 | mov RA, UPVAL:RB->v
4028 | ins_AD // RA = level, RD = target
4029 | branchPC RD // Do this first to free RD.
4031 | cmp dword L:RB->openupval, 0
4033 | mov L:RB->base, BASE
4034 | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
4035 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4036 | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
4037 | mov BASE, L:RB->base
4043 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
4046 | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4047 | mov CARG3d, [BASE-8]
4048 | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
4051 | mov LFUNC:RA, [BASE-8]
4052 | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
4054 | mov ARG3, LFUNC:RA
4055 | mov ARG2, PROTO:RD
4057 | mov L:RB->base, BASE
4060 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
4061 | call extern lj_func_newL_gc
4062 | // GCfuncL * returned in eax (RC).
4063 | mov BASE, L:RB->base
4065 | mov [BASE+RA*8], LFUNC:RC
4066 | mov dword [BASE+RA*8+4], LJ_TFUNC
4070 /* -- Table ops --------------------------------------------------------- */
4073 | ins_AD // RA = dst, RD = hbits|asize
4075 | mov L:RB->base, BASE
4076 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4077 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4095 | mov L:CARG1d, L:RB
4101 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
4102 | // Table * returned in eax (RC).
4103 | mov BASE, L:RB->base
4105 | mov [BASE+RA*8], TAB:RC
4106 | mov dword [BASE+RA*8+4], LJ_TTAB
4108 |3: // Turn 0x7ff into 0x801.
4112 | mov L:FCARG1, L:RB
4113 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4118 | ins_AND // RA = dst, RD = table const (~) (holding template table)
4120 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
4122 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
4123 | mov L:RB->base, BASE
4126 | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
4127 | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
4128 | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
4129 | // Table * returned in eax (RC).
4130 | mov BASE, L:RB->base
4132 | mov [BASE+RA*8], TAB:RC
4133 | mov dword [BASE+RA*8+4], LJ_TTAB
4136 | mov L:FCARG1, L:RB
4137 | call extern lj_gc_step_fixtop@4 // (lua_State *L)
4138 | movzx RD, PC_RD // Need to reload RD.
4144 | ins_AND // RA = dst, RD = str const (~)
4145 | mov LFUNC:RB, [BASE-8]
4146 | mov TAB:RB, LFUNC:RB->env
4147 | mov STR:RC, [KBASE+RD*4]
4151 | ins_AND // RA = src, RD = str const (~)
4152 | mov LFUNC:RB, [BASE-8]
4153 | mov TAB:RB, LFUNC:RB->env
4154 | mov STR:RC, [KBASE+RD*4]
4159 | ins_ABC // RA = dst, RB = table, RC = key
4160 | checktab RB, ->vmeta_tgetv
4161 | mov TAB:RB, [BASE+RB*8]
4166 | mov RC, dword [BASE+RC*8]
4168 | // Convert number to int and back and compare.
4170 | movsd xmm0, qword [BASE+RC*8]
4171 | cvttsd2si RC, xmm0
4173 | ucomisd xmm0, xmm1
4174 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
4176 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4177 | jae ->vmeta_tgetv // Not in array part? Use fallback.
4179 | add RC, TAB:RB->array
4180 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4182 | // Get array slot.
4185 | mov [BASE+RA*8], RBa
4189 | mov [BASE+RA*8], RB
4190 | mov [BASE+RA*8+4], RC
4195 |2: // Check for __index if table value is nil.
4196 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4198 | mov TAB:RA, TAB:RB->metatable
4199 | test byte TAB:RA->nomm, 1<<MM_index
4200 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
4201 | movzx RA, PC_RA // Restore RA.
4203 | mov dword [BASE+RA*8+4], LJ_TNIL
4207 | checkstr RC, ->vmeta_tgetv
4208 | mov STR:RC, [BASE+RC*8]
4212 | ins_ABC // RA = dst, RB = table, RC = str const (~)
4214 | mov STR:RC, [KBASE+RC*4]
4215 | checktab RB, ->vmeta_tgets
4216 | mov TAB:RB, [BASE+RB*8]
4217 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4218 | mov RA, TAB:RB->hmask
4219 | and RA, STR:RC->hash
4221 | add NODE:RA, TAB:RB->node
4223 | cmp dword NODE:RA->key.it, LJ_TSTR
4225 | cmp dword NODE:RA->key.gcr, STR:RC
4227 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4228 | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4229 | je >5 // Key found, but nil value?
4231 | // Get node value.
4234 | mov [BASE+RC*8], RBa
4238 | mov [BASE+RC*8], RB
4239 | mov [BASE+RC*8+4], RA
4246 | mov dword [BASE+RC*8+4], LJ_TNIL
4249 |4: // Follow hash chain.
4250 | mov NODE:RA, NODE:RA->next
4251 | test NODE:RA, NODE:RA
4253 | // End of hash chain: key not found, nil result.
4255 |5: // Check for __index if table value is nil.
4256 | mov TAB:RA, TAB:RB->metatable
4257 | test TAB:RA, TAB:RA
4258 | jz <3 // No metatable: done.
4259 | test byte TAB:RA->nomm, 1<<MM_index
4260 | jnz <3 // 'no __index' flag set: done.
4261 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
4264 | ins_ABC // RA = dst, RB = table, RC = byte literal
4265 | checktab RB, ->vmeta_tgetb
4266 | mov TAB:RB, [BASE+RB*8]
4267 | cmp RC, TAB:RB->asize
4270 | add RC, TAB:RB->array
4271 | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
4273 | // Get array slot.
4276 | mov [BASE+RA*8], RBa
4280 | mov [BASE+RA*8], RB
4281 | mov [BASE+RA*8+4], RC
4286 |2: // Check for __index if table value is nil.
4287 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4289 | mov TAB:RA, TAB:RB->metatable
4290 | test byte TAB:RA->nomm, 1<<MM_index
4291 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
4292 | movzx RA, PC_RA // Restore RA.
4294 | mov dword [BASE+RA*8+4], LJ_TNIL
4298 | ins_ABC // RA = dst, RB = table, RC = key
4299 | mov TAB:RB, [BASE+RB*8]
4301 | mov RC, dword [BASE+RC*8]
4303 | cvttsd2si RC, qword [BASE+RC*8]
4305 | cmp RC, TAB:RB->asize
4306 | jae ->vmeta_tgetr // Not in array part? Use fallback.
4308 | add RC, TAB:RB->array
4309 | // Get array slot.
4313 | mov [BASE+RA*8], RBa
4317 | mov [BASE+RA*8], RB
4318 | mov [BASE+RA*8+4], RC
4325 | ins_ABC // RA = src, RB = table, RC = key
4326 | checktab RB, ->vmeta_tsetv
4327 | mov TAB:RB, [BASE+RB*8]
4332 | mov RC, dword [BASE+RC*8]
4334 | // Convert number to int and back and compare.
4336 | movsd xmm0, qword [BASE+RC*8]
4337 | cvttsd2si RC, xmm0
4339 | ucomisd xmm0, xmm1
4340 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
4342 | cmp RC, TAB:RB->asize // Takes care of unordered, too.
4345 | add RC, TAB:RB->array
4346 | cmp dword [RC+4], LJ_TNIL
4347 | je >3 // Previous value is nil?
4349 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4351 |2: // Set array slot.
4353 | mov RBa, [BASE+RA*8]
4356 | mov RB, [BASE+RA*8+4]
4357 | mov RA, [BASE+RA*8]
4363 |3: // Check for __newindex if previous value is nil.
4364 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4366 | mov TAB:RA, TAB:RB->metatable
4367 | test byte TAB:RA->nomm, 1<<MM_newindex
4368 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
4369 | movzx RA, PC_RA // Restore RA.
4373 | checkstr RC, ->vmeta_tsetv
4374 | mov STR:RC, [BASE+RC*8]
4377 |7: // Possible table write barrier for the value. Skip valiswhite check.
4378 | barrierback TAB:RB, RA
4379 | movzx RA, PC_RA // Restore RA.
4383 | ins_ABC // RA = src, RB = table, RC = str const (~)
4385 | mov STR:RC, [KBASE+RC*4]
4386 | checktab RB, ->vmeta_tsets
4387 | mov TAB:RB, [BASE+RB*8]
4388 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
4389 | mov RA, TAB:RB->hmask
4390 | and RA, STR:RC->hash
4392 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
4393 | add NODE:RA, TAB:RB->node
4395 | cmp dword NODE:RA->key.it, LJ_TSTR
4397 | cmp dword NODE:RA->key.gcr, STR:RC
4399 | // Ok, key found. Assumes: offsetof(Node, val) == 0
4400 | cmp dword [RA+4], LJ_TNIL
4401 | je >4 // Previous value is nil?
4403 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4405 |3: // Set node value.
4408 | mov RBa, [BASE+RC*8]
4411 | mov RB, [BASE+RC*8+4]
4412 | mov RC, [BASE+RC*8]
4418 |4: // Check for __newindex if previous value is nil.
4419 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4421 | mov TMP1, RA // Save RA.
4422 | mov TAB:RA, TAB:RB->metatable
4423 | test byte TAB:RA->nomm, 1<<MM_newindex
4424 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4425 | mov RA, TMP1 // Restore RA.
4428 |5: // Follow hash chain.
4429 | mov NODE:RA, NODE:RA->next
4430 | test NODE:RA, NODE:RA
4432 | // End of hash chain: key not found, add a new one.
4434 | // But check for __newindex first.
4435 | mov TAB:RA, TAB:RB->metatable
4436 | test TAB:RA, TAB:RA
4437 | jz >6 // No metatable: continue.
4438 | test byte TAB:RA->nomm, 1<<MM_newindex
4439 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
4443 | mov TMP3, TAB:RB // Save TAB:RB for us.
4445 | mov L:CARG1d, SAVE_L
4446 | mov L:CARG1d->base, BASE
4448 | mov CARG2d, TAB:RB
4449 | mov L:RB, L:CARG1d
4451 | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
4456 | mov L:RB->base, BASE
4459 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
4460 | // Handles write barrier for the new key. TValue * returned in eax (RC).
4461 | mov BASE, L:RB->base
4462 | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
4464 | jmp <2 // Must check write barrier for value.
4466 |7: // Possible table write barrier for the value. Skip valiswhite check.
4467 | barrierback TAB:RB, RC // Destroys STR:RC.
4471 | ins_ABC // RA = src, RB = table, RC = byte literal
4472 | checktab RB, ->vmeta_tsetb
4473 | mov TAB:RB, [BASE+RB*8]
4474 | cmp RC, TAB:RB->asize
4477 | add RC, TAB:RB->array
4478 | cmp dword [RC+4], LJ_TNIL
4479 | je >3 // Previous value is nil?
4481 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4483 |2: // Set array slot.
4485 | mov RAa, [BASE+RA*8]
4488 | mov RB, [BASE+RA*8+4]
4489 | mov RA, [BASE+RA*8]
4495 |3: // Check for __newindex if previous value is nil.
4496 | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
4498 | mov TAB:RA, TAB:RB->metatable
4499 | test byte TAB:RA->nomm, 1<<MM_newindex
4500 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
4501 | movzx RA, PC_RA // Restore RA.
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 = key
4511 | mov TAB:RB, [BASE+RB*8]
4513 | mov RC, dword [BASE+RC*8]
4515 | cvttsd2si RC, qword [BASE+RC*8]
4517 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4520 | cmp RC, TAB:RB->asize
4523 | add RC, TAB:RB->array
4524 | // Set array slot.
4527 | mov RBa, [BASE+RA*8]
4530 | mov RB, [BASE+RA*8+4]
4531 | mov RA, [BASE+RA*8]
4537 |7: // Possible table write barrier for the value. Skip valiswhite check.
4538 | barrierback TAB:RB, RA
4539 | movzx RA, PC_RA // Restore RA.
4544 | ins_AD // RA = base (table at base-1), RD = num const (start index)
4545 | mov TMP1, KBASE // Need one more free register.
4546 | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
4548 | lea RA, [BASE+RA*8]
4549 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
4550 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
4555 | jz >4 // Nothing to copy?
4556 | add RD, KBASE // Compute needed size.
4557 | cmp RD, TAB:RB->asize
4558 | ja >5 // Doesn't fit into array part?
4561 | add KBASE, TAB:RB->array
4562 |3: // Copy result slots to table.
4581 |5: // Need to resize array part.
4583 | mov L:CARG1d, SAVE_L
4584 | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
4585 | mov CARG2d, TAB:RB
4587 | mov L:RB, L:CARG1d
4591 | mov L:RB->base, BASE
4596 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
4597 | mov BASE, L:RB->base
4598 | movzx RA, PC_RA // Restore RA.
4601 |7: // Possible table write barrier for any value. Skip valiswhite check.
4602 | barrierback TAB:RB, RD
4606 /* -- Calls and vararg handling ----------------------------------------- */
4608 case BC_CALL: case BC_CALLM:
4609 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
4610 if (op == BC_CALLM) {
4611 | add NARGS:RD, MULTRES
4613 | cmp dword [BASE+RA*8+4], LJ_TFUNC
4614 | mov LFUNC:RB, [BASE+RA*8]
4615 | jne ->vmeta_call_ra
4616 | lea BASE, [BASE+RA*8+8]
4621 | ins_AD // RA = base, RD = extra_nargs
4622 | add NARGS:RD, MULTRES
4623 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
4626 | ins_AD // RA = base, RD = nargs+1
4627 | lea RA, [BASE+RA*8+8]
4628 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
4629 | mov LFUNC:RB, [RA-8]
4630 | cmp dword [RA-4], LJ_TFUNC
4634 | test PC, FRAME_TYPE
4637 | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
4638 | mov MULTRES, NARGS:RD
4641 |2: // Move args down.
4657 | mov LFUNC:RB, [BASE-8]
4659 | mov NARGS:RD, MULTRES
4660 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
4665 |5: // Tailcall to a fast function.
4666 | test PC, FRAME_TYPE // Lua frame below?
4670 | mov LFUNC:KBASE, [BASE+RA*8-8] // Need to prepare KBASE.
4671 | mov KBASE, LFUNC:KBASE->pc
4672 | mov KBASE, [KBASE+PC2PROTO(k)]
4675 |7: // Tailcall from a vararg function.
4676 | sub PC, FRAME_VARG
4677 | test PC, FRAME_TYPEP
4678 | jnz >8 // Vararg frame below?
4679 | sub BASE, PC // Need to relocate BASE/KBASE down.
4684 | add PC, FRAME_VARG
4689 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
4690 | lea RA, [BASE+RA*8+8] // fb = base+1
4692 | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
4693 | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
4697 | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
4701 | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
4706 | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
4708 | mov [RA-8], LFUNC:RB
4710 | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
4718 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
4720 | // NYI: add hotloop, record BC_ITERN.
4722 | mov TMP1, KBASE // Need two more free registers.
4723 | mov TMP2, DISPATCH
4724 | mov TAB:RB, [BASE+RA*8-16]
4725 | mov RC, [BASE+RA*8-8] // Get index from control var.
4726 | mov DISPATCH, TAB:RB->asize
4728 | mov KBASE, TAB:RB->array
4729 |1: // Traverse array part.
4730 | cmp RC, DISPATCH; jae >5 // Index points after array part?
4731 | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
4733 | mov dword [BASE+RA*8+4], LJ_TISNUM
4734 | mov dword [BASE+RA*8], RC
4738 | // Copy array slot to returned value.
4740 | mov RBa, [KBASE+RC*8]
4741 | mov [BASE+RA*8+8], RBa
4743 | mov RB, [KBASE+RC*8+4]
4744 | mov [BASE+RA*8+12], RB
4745 | mov RB, [KBASE+RC*8]
4746 | mov [BASE+RA*8+8], RB
4749 | // Return array index as a numeric key.
4753 | movsd qword [BASE+RA*8], xmm0
4755 | mov [BASE+RA*8-8], RC // Update control var.
4757 | movzx RD, PC_RD // Get target from ITERL.
4760 | mov DISPATCH, TMP2
4764 |4: // Skip holes in array part.
4768 |5: // Traverse hash part.
4771 | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
4772 | imul KBASE, RC, #NODE
4773 | add NODE:KBASE, TAB:RB->node
4774 | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
4775 | lea DISPATCH, [RC+DISPATCH+1]
4776 | // Copy key and value from hash slot.
4778 | mov RBa, NODE:KBASE->key
4779 | mov RCa, NODE:KBASE->val
4780 | mov [BASE+RA*8], RBa
4781 | mov [BASE+RA*8+8], RCa
4783 | mov RB, NODE:KBASE->key.gcr
4784 | mov RC, NODE:KBASE->key.it
4785 | mov [BASE+RA*8], RB
4786 | mov [BASE+RA*8+4], RC
4787 | mov RB, NODE:KBASE->val.gcr
4788 | mov RC, NODE:KBASE->val.it
4789 | mov [BASE+RA*8+8], RB
4790 | mov [BASE+RA*8+12], RC
4792 | mov [BASE+RA*8-8], DISPATCH
4795 |7: // Skip holes in hash part.
4801 | ins_AD // RA = base, RD = target (points to ITERN)
4802 | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
4803 | mov CFUNC:RB, [BASE+RA*8-24]
4804 | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
4805 | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
4806 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
4808 | mov dword [BASE+RA*8-8], 0 // Initialize control var.
4809 | mov dword [BASE+RA*8-4], 0xfffe7fff
4812 |5: // Despecialize bytecode if any of the checks fail.
4815 | mov byte [PC], BC_ITERC
4820 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
4821 | mov TMP1, KBASE // Need one more free register.
4822 | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
4823 | lea RA, [BASE+RA*8]
4824 | sub KBASE, [BASE-4]
4825 | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
4827 | jz >5 // Copy all varargs?
4828 | lea RB, [RA+RB*8-8]
4829 | cmp KBASE, BASE // No vararg slots?
4831 |1: // Copy vararg slots to destination slots.
4833 | mov RCa, [KBASE-8]
4844 | cmp RA, RB // All destination slots filled?
4846 | cmp KBASE, BASE // No more vararg slots?
4848 |2: // Fill up remainder with nil.
4849 | mov dword [RA+4], LJ_TNIL
4857 |5: // Copy all varargs.
4858 | mov MULTRES, 1 // MULTRES = 0+1
4861 | jbe <3 // No vararg slots?
4865 | mov MULTRES, RB // MULTRES = #varargs+1
4868 | cmp RC, L:RB->maxstack
4869 | ja >7 // Need to grow stack?
4870 |6: // Copy all vararg slots.
4872 | mov RCa, [KBASE-8]
4883 | cmp KBASE, BASE // No more vararg slots?
4887 |7: // Grow stack for varargs.
4888 | mov L:RB->base, BASE
4891 | sub KBASE, BASE // Need delta, because BASE may change.
4892 | mov FCARG2, MULTRES
4895 | call extern lj_state_growstack@8 // (lua_State *L, int n)
4896 | mov BASE, L:RB->base
4902 /* -- Returns ----------------------------------------------------------- */
4905 | ins_AD // RA = results, RD = extra_nresults
4906 | add RD, MULTRES // MULTRES >=1, so RD >=1.
4907 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
4910 case BC_RET: case BC_RET0: case BC_RET1:
4911 | ins_AD // RA = results, RD = nresults+1
4912 if (op != BC_RET0) {
4917 | mov MULTRES, RD // Save nresults+1.
4918 | test PC, FRAME_TYPE // Check frame type marker.
4919 | jnz >7 // Not returning to a fixarg Lua func?
4923 | mov KBASE, BASE // Use KBASE for result move.
4926 |2: // Move results down.
4928 | mov RBa, [KBASE+RA]
4929 | mov [KBASE-8], RBa
4931 | mov RB, [KBASE+RA]
4933 | mov RB, [KBASE+RA+4]
4940 | mov RD, MULTRES // Note: MULTRES may be >255.
4941 | movzx RB, PC_RB // So cannot compare with RDL!
4943 | cmp RB, RD // More results expected?
4948 | mov RBa, [BASE+RA]
4951 | mov RB, [BASE+RA+4]
4959 | cmp PC_RB, RDL // More results expected?
4965 | not RAa // Note: ~RA = -(RA+1)
4966 | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
4967 | mov LFUNC:KBASE, [BASE-8]
4968 | mov KBASE, LFUNC:KBASE->pc
4969 | mov KBASE, [KBASE+PC2PROTO(k)]
4972 |6: // Fill up results with nil.
4974 | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
4977 | mov dword [BASE+RD*8-12], LJ_TNIL
4982 |7: // Non-standard return case.
4983 | lea RB, [PC-FRAME_VARG]
4984 | test RB, FRAME_TYPEP
4986 | // Return from vararg function: relocate BASE down and RA up.
4988 if (op != BC_RET0) {
4994 /* -- Loops and branches ------------------------------------------------ */
4996 |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
4997 |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
4998 |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
4999 |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
5005 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
5015 vk = (op == BC_IFORL || op == BC_JFORL);
5016 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
5017 | lea RA, [BASE+RA*8]
5019 | cmp FOR_TIDX, LJ_TISNUM; jne >9
5021 | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
5022 | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
5023 | mov RB, dword FOR_IDX
5024 | cmp dword FOR_STEP, 0; jl >5
5026 #ifdef LUA_USE_ASSERT
5027 | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5028 | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
5030 | mov RB, dword FOR_STEP
5031 | test RB, RB; js >5
5032 | add RB, dword FOR_IDX; jo >1
5033 | mov dword FOR_IDX, RB
5035 | cmp RB, dword FOR_STOP
5036 | mov FOR_TEXT, LJ_TISNUM
5037 | mov dword FOR_EXT, RB
5038 if (op == BC_FORI) {
5043 } else if (op == BC_JFORI) {
5049 } else if (op == BC_IFORL) {
5062 |5: // Invert check for negative step.
5064 | add RB, dword FOR_IDX; jo <1
5065 | mov dword FOR_IDX, RB
5067 | cmp RB, dword FOR_STOP
5068 | mov FOR_TEXT, LJ_TISNUM
5069 | mov dword FOR_EXT, RB
5070 if (op == BC_FORI) {
5072 } else if (op == BC_JFORI) {
5076 } else if (op == BC_IFORL) {
5082 |9: // Fallback to FP variant.
5084 | cmp FOR_TIDX, LJ_TISNUM
5088 | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
5090 #ifdef LUA_USE_ASSERT
5091 | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5092 | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
5095 | mov RB, FOR_TSTEP // Load type/hiword of for step.
5097 | cmp RB, LJ_TISNUM; jae ->vmeta_for
5099 | movsd xmm0, qword FOR_IDX
5100 | movsd xmm1, qword FOR_STOP
5102 | addsd xmm0, qword FOR_STEP
5103 | movsd qword FOR_IDX, xmm0
5104 | test RB, RB; js >3
5108 | ucomisd xmm1, xmm0
5110 | movsd qword FOR_EXT, xmm0
5111 if (op == BC_FORI) {
5118 } else if (op == BC_JFORI) {
5122 } else if (op == BC_IFORL) {
5139 |3: // Invert comparison if step is negative.
5140 | ucomisd xmm0, xmm1
5148 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
5156 | ins_AJ // RA = base, RD = target
5157 | lea RA, [BASE+RA*8]
5159 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
5160 if (op == BC_JITERL) {
5166 | branchPC RD // Otherwise save control var + branch.
5176 | ins_A // RA = base, RD = target (loop extent)
5177 | // Note: RA/RD is only used by trace recorder to determine scope/extent
5178 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
5182 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
5186 | ins_A // RA = base, RD = target (loop extent)
5192 | ins_AD // RA = base (ignored), RD = traceno
5193 | mov RA, [DISPATCH+DISPATCH_J(trace)]
5194 | mov TRACE:RD, [RA+RD*4]
5195 | mov RDa, TRACE:RD->mcode
5197 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
5198 | mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB
5199 | // Save additional callee-save registers only used in compiled code.
5207 | movdqa [RAa], xmm6
5208 | movdqa [RAa-1*16], xmm7
5209 | movdqa [RAa-2*16], xmm8
5210 | movdqa [RAa-3*16], xmm9
5211 | movdqa [RAa-4*16], xmm10
5212 | movdqa [RAa-5*16], xmm11
5213 | movdqa [RAa-6*16], xmm12
5214 | movdqa [RAa-7*16], xmm13
5215 | movdqa [RAa-8*16], xmm14
5216 | movdqa [RAa-9*16], xmm15
5227 | ins_AJ // RA = unused, RD = target
5232 /* -- Function headers -------------------------------------------------- */
5235 ** Reminder: A function may be called with func/args above L->maxstack,
5236 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
5237 ** too. This means all FUNC* ops (including fast functions) must check
5238 ** for stack overflow _before_ adding more slots!
5245 case BC_FUNCV: /* NYI: compiled vararg functions. */
5246 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
5254 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5255 | mov KBASE, [PC-4+PC2PROTO(k)]
5257 | lea RA, [BASE+RA*8] // Top of frame.
5258 | cmp RA, L:RB->maxstack
5259 | ja ->vm_growstack_f
5260 | movzx RA, byte [PC-4+PC2PROTO(numparams)]
5261 | cmp NARGS:RD, RA // Check for missing parameters.
5264 if (op == BC_JFUNCF) {
5271 |3: // Clear missing parameters.
5272 | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
5283 | int3 // NYI: compiled vararg functions
5284 break; /* NYI: compiled vararg functions. */
5287 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
5288 | lea RB, [NARGS:RD*8+FRAME_VARG]
5289 | lea RD, [BASE+NARGS:RD*8]
5290 | mov LFUNC:KBASE, [BASE-8]
5291 | mov [RD-4], RB // Store delta + FRAME_VARG.
5292 | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
5295 | cmp RA, L:RB->maxstack
5296 | ja ->vm_growstack_v // Need to grow stack.
5299 | movzx RB, byte [PC-4+PC2PROTO(numparams)]
5302 |1: // Copy fixarg slots up to new frame.
5305 | jnb >3 // Less args than parameters?
5311 | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
5315 if (op == BC_JFUNCV) {
5319 | mov KBASE, [PC-4+PC2PROTO(k)]
5323 |3: // Clear missing parameters.
5324 | mov dword [RD+4], LJ_TNIL
5333 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
5334 | mov CFUNC:RB, [BASE-8]
5335 | mov KBASEa, CFUNC:RB->f
5337 | lea RD, [BASE+NARGS:RD*8-8]
5338 | mov L:RB->base, BASE
5339 | lea RA, [RD+8*LUA_MINSTACK]
5340 | cmp RA, L:RB->maxstack
5342 if (op == BC_FUNCC) {
5344 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5351 | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
5357 | ja ->vm_growstack_c // Need to grow stack.
5359 if (op == BC_FUNCC) {
5360 | call KBASEa // (lua_State *L)
5362 | // (lua_State *L, lua_CFunction f)
5363 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
5365 | // nresults returned in eax (RD).
5366 | mov BASE, L:RB->base
5367 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
5368 | set_vmstate INTERP
5369 | lea RA, [BASE+RD*8]
5371 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
5372 | mov PC, [BASE-4] // Fetch PC of caller.
5376 /* ---------------------------------------------------------------------- */
5379 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
5385 static int build_backend(BuildCtx *ctx)
5388 dasm_growpc(Dst, BC__MAX);
5389 build_subroutines(ctx);
5391 for (op = 0; op < BC__MAX; op++)
5392 build_ins(ctx, (BCOp)op, op);
5396 /* Emit pseudo frame-info for all assembler functions. */
5397 static void emit_asm_debug(BuildCtx *ctx)
5399 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
5403 #define REG_SP "0x7"
5404 #define REG_RA "0x10"
5408 #define REG_SP "0x4"
5409 #define REG_RA "0x8"
5411 switch (ctx->mode) {
5413 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
5416 "\t.long .LECIE0-.LSCIE0\n"
5418 "\t.long 0xffffffff\n"
5422 "\t.sleb128 -" SZPTR "\n"
5423 "\t.byte " REG_RA "\n"
5424 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5425 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5426 "\t.align " SZPTR "\n"
5430 "\t.long .LEFDE0-.LASFDE0\n"
5432 "\t.long .Lframe0\n"
5436 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5437 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5438 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5439 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
5440 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
5442 "\t.byte 0x8d\n\t.uleb128 0x6\n" /* offset r13 */
5443 "\t.byte 0x8c\n\t.uleb128 0x7\n" /* offset r12 */
5448 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5449 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5450 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
5451 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
5452 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
5454 "\t.align " SZPTR "\n"
5455 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
5459 "\t.long .LEFDE1-.LASFDE1\n"
5461 "\t.long .Lframe0\n"
5463 "\t.quad lj_vm_ffi_call\n"
5465 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
5466 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5467 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
5468 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5470 "\t.long lj_vm_ffi_call\n"
5472 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
5473 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5474 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
5475 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
5477 "\t.align " SZPTR "\n"
5478 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
5481 #if (defined(__sun__) && defined(__svr4__))
5483 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");
5485 fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
5488 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
5492 "\t.long .LECIE1-.LSCIE1\n"
5496 "\t.string \"zPR\"\n"
5498 "\t.sleb128 -" SZPTR "\n"
5499 "\t.byte " REG_RA "\n"
5500 "\t.uleb128 6\n" /* augmentation length */
5501 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5502 "\t.long lj_err_unwind_dwarf-.\n"
5503 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5504 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5505 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5506 "\t.align " SZPTR "\n"
5510 "\t.long .LEFDE2-.LASFDE2\n"
5512 "\t.long .LASFDE2-.Lframe1\n"
5513 "\t.long .Lbegin-.\n"
5515 "\t.uleb128 0\n" /* augmentation length */
5516 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
5518 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5519 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5520 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
5521 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
5523 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5524 "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
5525 "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
5526 "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
5528 "\t.align " SZPTR "\n"
5529 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
5533 "\t.long .LECIE2-.LSCIE2\n"
5537 "\t.string \"zR\"\n"
5539 "\t.sleb128 -" SZPTR "\n"
5540 "\t.byte " REG_RA "\n"
5541 "\t.uleb128 1\n" /* augmentation length */
5542 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5543 "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
5544 "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
5545 "\t.align " SZPTR "\n"
5549 "\t.long .LEFDE3-.LASFDE3\n"
5551 "\t.long .LASFDE3-.Lframe2\n"
5552 "\t.long lj_vm_ffi_call-.\n"
5554 "\t.uleb128 0\n" /* augmentation length */
5556 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
5557 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
5558 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
5559 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
5561 "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
5562 "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
5563 "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
5564 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
5566 "\t.align " SZPTR "\n"
5567 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
5572 /* Mental note: never let Apple design an assembler.
5573 ** Or a linker. Or a plastic case. But I digress.
5575 case BUILD_machasm: {
5580 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
5583 "\t.set L$set$x,LECIEX-LSCIEX\n"
5588 "\t.ascii \"zPR\\0\"\n"
5590 "\t.byte 128-" SZPTR "\n"
5591 "\t.byte " REG_RA "\n"
5592 "\t.byte 6\n" /* augmentation length */
5593 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
5595 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
5596 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5597 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
5599 "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
5600 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5601 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
5603 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
5604 "\t.align " BSZPTR "\n"
5606 for (i = 0; i < ctx->nsym; i++) {
5607 const char *name = ctx->sym[i].name;
5608 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
5609 if (size == 0) continue;
5611 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
5616 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
5617 "\t.long L$set$%d\n"
5619 "\t.long LASFDE%d-EH_frame1\n"
5622 "\t.byte 0\n" /* augmentation length */
5623 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
5625 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
5626 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
5627 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
5628 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
5630 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
5631 "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
5632 "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
5633 "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
5635 "\t.align " BSZPTR "\n"
5637 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
5643 "\t.set L$set$y,LECIEY-LSCIEY\n"
5648 "\t.ascii \"zR\\0\"\n"
5650 "\t.byte 128-" SZPTR "\n"
5651 "\t.byte " REG_RA "\n"
5652 "\t.byte 1\n" /* augmentation length */
5654 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5655 "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
5657 "\t.byte 0x1b\n" /* pcrel|sdata4 */
5658 "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
5660 "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
5661 "\t.align " BSZPTR "\n"
5664 "_lj_vm_ffi_call.eh:\n"
5666 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
5667 "\t.long L$set$yy\n"
5669 "\t.long LASFDEY-EH_frame2\n"
5670 "\t.long _lj_vm_ffi_call-.\n"
5672 "\t.byte 0\n" /* augmentation length */
5674 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
5675 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
5676 "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */
5677 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
5679 "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
5680 "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
5681 "\t.byte 0xd\n\t.byte 0x4\n" /* def_cfa_register ebp */
5682 "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
5684 "\t.align " BSZPTR "\n"
5685 "LEFDEY:\n\n", fcsize);
5690 "\t.non_lazy_symbol_pointer\n"
5691 "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
5692 ".indirect_symbol _lj_err_unwind_dwarf\n"
5694 fprintf(ctx->fp, "\t.section __IMPORT,__jump_table,symbol_stubs,pure_instructions+self_modifying_code,5\n");
5696 const char *const *xn;
5697 for (xn = ctx->extnames; *xn; xn++)
5698 if (strncmp(*xn, LABEL_PREFIX, sizeof(LABEL_PREFIX)-1))
5699 fprintf(ctx->fp, "L_%s$stub:\n\t.indirect_symbol _%s\n\t.ascii \"\\364\\364\\364\\364\\364\"\n", *xn, *xn);
5702 fprintf(ctx->fp, ".subsections_via_symbols\n");
5706 default: /* Difficult for other modes. */