Debian Codegraph @

libs/luajit/LuaJIT-src/src/vm_x64.dasc

raw
1 |// Low-level VM code for x64 CPUs in LJ_GC64 mode.
2 |// Bytecode interpreter, fast functions and helper functions.
3 |// Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
4 |
5 |.arch x64
6 |.section code_op, code_sub
7 |
8 |.actionlist build_actionlist
9 |.globals GLOB_
10 |.globalnames globnames
11 |.externnames extnames
12 |
13 |//-----------------------------------------------------------------------
14 |
15 |.if WIN
16 |.define X64WIN, 1 // Windows/x64 calling conventions.
17 |.endif
18 |
19 |// Fixed register assignments for the interpreter.
20 |// This is very fragile and has many dependencies. Caveat emptor.
21 |.define BASE, rdx // Not C callee-save, refetched anyway.
22 |.if X64WIN
23 |.define KBASE, rdi // Must be C callee-save.
24 |.define PC, rsi // Must be C callee-save.
25 |.define DISPATCH, rbx // Must be C callee-save.
26 |.define KBASEd, edi
27 |.define PCd, esi
28 |.define DISPATCHd, ebx
29 |.else
30 |.define KBASE, r15 // Must be C callee-save.
31 |.define PC, rbx // Must be C callee-save.
32 |.define DISPATCH, r14 // Must be C callee-save.
33 |.define KBASEd, r15d
34 |.define PCd, ebx
35 |.define DISPATCHd, r14d
36 |.endif
37 |
38 |.define RA, rcx
39 |.define RAd, ecx
40 |.define RAH, ch
41 |.define RAL, cl
42 |.define RB, rbp // Must be rbp (C callee-save).
43 |.define RBd, ebp
44 |.define RC, rax // Must be rax.
45 |.define RCd, eax
46 |.define RCW, ax
47 |.define RCH, ah
48 |.define RCL, al
49 |.define OP, RBd
50 |.define RD, RC
51 |.define RDd, RCd
52 |.define RDW, RCW
53 |.define RDL, RCL
54 |.define TMPR, r10
55 |.define TMPRd, r10d
56 |.define ITYPE, r11
57 |.define ITYPEd, r11d
58 |
59 |.if X64WIN
60 |.define CARG1, rcx // x64/WIN64 C call arguments.
61 |.define CARG2, rdx
62 |.define CARG3, r8
63 |.define CARG4, r9
64 |.define CARG1d, ecx
65 |.define CARG2d, edx
66 |.define CARG3d, r8d
67 |.define CARG4d, r9d
68 |.else
69 |.define CARG1, rdi // x64/POSIX C call arguments.
70 |.define CARG2, rsi
71 |.define CARG3, rdx
72 |.define CARG4, rcx
73 |.define CARG5, r8
74 |.define CARG6, r9
75 |.define CARG1d, edi
76 |.define CARG2d, esi
77 |.define CARG3d, edx
78 |.define CARG4d, ecx
79 |.define CARG5d, r8d
80 |.define CARG6d, r9d
81 |.endif
82 |
83 |// Type definitions. Some of these are only used for documentation.
84 |.type L, lua_State
85 |.type GL, global_State
86 |.type TVALUE, TValue
87 |.type GCOBJ, GCobj
88 |.type STR, GCstr
89 |.type TAB, GCtab
90 |.type LFUNC, GCfuncL
91 |.type CFUNC, GCfuncC
92 |.type PROTO, GCproto
93 |.type UPVAL, GCupval
94 |.type NODE, Node
95 |.type NARGS, int
96 |.type TRACE, GCtrace
97 |.type SBUF, SBuf
98 |
99 |// Stack layout while in interpreter. Must match with lj_frame.h.
100 |//-----------------------------------------------------------------------
101 |.if X64WIN // x64/Windows stack layout
102 |
103 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
104 |.macro saveregs_
105 | push rdi; push rsi; push rbx
106 | sub rsp, CFRAME_SPACE
107 |.endmacro
108 |.macro saveregs
109 | push rbp; saveregs_
110 |.endmacro
111 |.macro restoreregs
112 | add rsp, CFRAME_SPACE
113 | pop rbx; pop rsi; pop rdi; pop rbp
114 |.endmacro
115 |
116 |.define SAVE_CFRAME, aword [rsp+aword*13]
117 |.define SAVE_PC, aword [rsp+aword*12]
118 |.define SAVE_L, aword [rsp+aword*11]
119 |.define SAVE_ERRF, dword [rsp+dword*21]
120 |.define SAVE_NRES, dword [rsp+dword*20]
121 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
122 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
123 |.define SAVE_R4, aword [rsp+aword*8]
124 |.define SAVE_R3, aword [rsp+aword*7]
125 |.define SAVE_R2, aword [rsp+aword*6]
126 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
127 |.define ARG5, aword [rsp+aword*4]
128 |.define CSAVE_4, aword [rsp+aword*3]
129 |.define CSAVE_3, aword [rsp+aword*2]
130 |.define CSAVE_2, aword [rsp+aword*1]
131 |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
132 |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
133 |
134 |.define ARG5d, dword [rsp+dword*8]
135 |.define TMP1, ARG5 // TMP1 overlaps ARG5
136 |.define TMP1d, ARG5d
137 |.define TMP1hi, dword [rsp+dword*9]
138 |.define MULTRES, TMP1d // MULTRES overlaps TMP1d.
139 |
140 |//-----------------------------------------------------------------------
141 |.else // x64/POSIX stack layout
142 |
143 |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
144 |.macro saveregs_
145 | push rbx; push r15; push r14
146 |.if NO_UNWIND
147 | push r13; push r12
148 |.endif
149 | sub rsp, CFRAME_SPACE
150 |.endmacro
151 |.macro saveregs
152 | push rbp; saveregs_
153 |.endmacro
154 |.macro restoreregs
155 | add rsp, CFRAME_SPACE
156 |.if NO_UNWIND
157 | pop r12; pop r13
158 |.endif
159 | pop r14; pop r15; pop rbx; pop rbp
160 |.endmacro
161 |
162 |//----- 16 byte aligned,
163 |.if NO_UNWIND
164 |.define SAVE_RET, aword [rsp+aword*11] //<-- rsp entering interpreter.
165 |.define SAVE_R4, aword [rsp+aword*10]
166 |.define SAVE_R3, aword [rsp+aword*9]
167 |.define SAVE_R2, aword [rsp+aword*8]
168 |.define SAVE_R1, aword [rsp+aword*7]
169 |.define SAVE_RU2, aword [rsp+aword*6]
170 |.define SAVE_RU1, aword [rsp+aword*5] //<-- rsp after register saves.
171 |.else
172 |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
173 |.define SAVE_R4, aword [rsp+aword*8]
174 |.define SAVE_R3, aword [rsp+aword*7]
175 |.define SAVE_R2, aword [rsp+aword*6]
176 |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
177 |.endif
178 |.define SAVE_CFRAME, aword [rsp+aword*4]
179 |.define SAVE_PC, aword [rsp+aword*3]
180 |.define SAVE_L, aword [rsp+aword*2]
181 |.define SAVE_ERRF, dword [rsp+dword*3]
182 |.define SAVE_NRES, dword [rsp+dword*2]
183 |.define TMP1, aword [rsp] //<-- rsp while in interpreter.
184 |//----- 16 byte aligned
185 |
186 |.define TMP1d, dword [rsp]
187 |.define TMP1hi, dword [rsp+dword*1]
188 |.define MULTRES, TMP1d // MULTRES overlaps TMP1d.
189 |
190 |.endif
191 |
192 |//-----------------------------------------------------------------------
193 |
194 |// Instruction headers.
195 |.macro ins_A; .endmacro
196 |.macro ins_AD; .endmacro
197 |.macro ins_AJ; .endmacro
198 |.macro ins_ABC; movzx RBd, RCH; movzx RCd, RCL; .endmacro
199 |.macro ins_AB_; movzx RBd, RCH; .endmacro
200 |.macro ins_A_C; movzx RCd, RCL; .endmacro
201 |.macro ins_AND; not RD; .endmacro
202 |
203 |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
204 |.macro ins_NEXT
205 | mov RCd, [PC]
206 | movzx RAd, RCH
207 | movzx OP, RCL
208 | add PC, 4
209 | shr RCd, 16
210 | jmp aword [DISPATCH+OP*8]
211 |.endmacro
212 |
213 |// Instruction footer.
214 |.if 1
215 | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
216 | .define ins_next, ins_NEXT
217 | .define ins_next_, ins_NEXT
218 |.else
219 | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
220 | // Affects only certain kinds of benchmarks (and only with -j off).
221 | // Around 10%-30% slower on Core2, a lot more slower on P4.
222 | .macro ins_next
223 | jmp ->ins_next
224 | .endmacro
225 | .macro ins_next_
226 | ->ins_next:
227 | ins_NEXT
228 | .endmacro
229 |.endif
230 |
231 |// Call decode and dispatch.
232 |.macro ins_callt
233 | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-8] = PC
234 | mov PC, LFUNC:RB->pc
235 | mov RAd, [PC]
236 | movzx OP, RAL
237 | movzx RAd, RAH
238 | add PC, 4
239 | jmp aword [DISPATCH+OP*8]
240 |.endmacro
241 |
242 |.macro ins_call
243 | // BASE = new base, RB = LFUNC, RD = nargs+1
244 | mov [BASE-8], PC
245 | ins_callt
246 |.endmacro
247 |
248 |//-----------------------------------------------------------------------
249 |
250 |// Macros to clear or set tags.
251 |.macro cleartp, reg; shl reg, 17; shr reg, 17; .endmacro
252 |.macro settp, reg, tp
253 | mov64 ITYPE, ((uint64_t)tp<<47)
254 | or reg, ITYPE
255 |.endmacro
256 |.macro settp, dst, reg, tp
257 | mov64 dst, ((uint64_t)tp<<47)
258 | or dst, reg
259 |.endmacro
260 |.macro setint, reg
261 | settp reg, LJ_TISNUM
262 |.endmacro
263 |.macro setint, dst, reg
264 | settp dst, reg, LJ_TISNUM
265 |.endmacro
266 |
267 |// Macros to test operand types.
268 |.macro checktp_nc, reg, tp, target
269 | mov ITYPE, reg
270 | sar ITYPE, 47
271 | cmp ITYPEd, tp
272 | jne target
273 |.endmacro
274 |.macro checktp, reg, tp, target
275 | mov ITYPE, reg
276 | cleartp reg
277 | sar ITYPE, 47
278 | cmp ITYPEd, tp
279 | jne target
280 |.endmacro
281 |.macro checktptp, src, tp, target
282 | mov ITYPE, src
283 | sar ITYPE, 47
284 | cmp ITYPEd, tp
285 | jne target
286 |.endmacro
287 |.macro checkstr, reg, target; checktp reg, LJ_TSTR, target; .endmacro
288 |.macro checktab, reg, target; checktp reg, LJ_TTAB, target; .endmacro
289 |.macro checkfunc, reg, target; checktp reg, LJ_TFUNC, target; .endmacro
290 |
291 |.macro checknumx, reg, target, jump
292 | mov ITYPE, reg
293 | sar ITYPE, 47
294 | cmp ITYPEd, LJ_TISNUM
295 | jump target
296 |.endmacro
297 |.macro checkint, reg, target; checknumx reg, target, jne; .endmacro
298 |.macro checkinttp, src, target; checknumx src, target, jne; .endmacro
299 |.macro checknum, reg, target; checknumx reg, target, jae; .endmacro
300 |.macro checknumtp, src, target; checknumx src, target, jae; .endmacro
301 |.macro checknumber, src, target; checknumx src, target, ja; .endmacro
302 |
303 |.macro mov_false, reg; mov64 reg, (int64_t)~((uint64_t)1<<47); .endmacro
304 |.macro mov_true, reg; mov64 reg, (int64_t)~((uint64_t)2<<47); .endmacro
305 |
306 |// These operands must be used with movzx.
307 |.define PC_OP, byte [PC-4]
308 |.define PC_RA, byte [PC-3]
309 |.define PC_RB, byte [PC-1]
310 |.define PC_RC, byte [PC-2]
311 |.define PC_RD, word [PC-2]
312 |
313 |.macro branchPC, reg
314 | lea PC, [PC+reg*4-BCBIAS_J*4]
315 |.endmacro
316 |
317 |// Assumes DISPATCH is relative to GL.
318 #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
319 #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
320 |
321 #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
322 |
323 |// Decrement hashed hotcount and trigger trace recorder if zero.
324 |.macro hotloop, reg
325 | mov reg, PCd
326 | shr reg, 1
327 | and reg, HOTCOUNT_PCMASK
328 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
329 | jb ->vm_hotloop
330 |.endmacro
331 |
332 |.macro hotcall, reg
333 | mov reg, PCd
334 | shr reg, 1
335 | and reg, HOTCOUNT_PCMASK
336 | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
337 | jb ->vm_hotcall
338 |.endmacro
339 |
340 |// Set current VM state.
341 |.macro set_vmstate, st
342 | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
343 |.endmacro
344 |
345 |.macro fpop1; fstp st1; .endmacro
346 |
347 |// Synthesize SSE FP constants.
348 |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
349 | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
350 |.endmacro
351 |
352 |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
353 | mov64 tmp, U64x(val,00000000); movd reg, tmp
354 |.endmacro
355 |
356 |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
357 | sseconst_hi reg, tmp, 80000000
358 |.endmacro
359 |.macro sseconst_1, reg, tmp // Synthesize 1.0.
360 | sseconst_hi reg, tmp, 3ff00000
361 |.endmacro
362 |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
363 | sseconst_hi reg, tmp, 43300000
364 |.endmacro
365 |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
366 | sseconst_hi reg, tmp, 43380000
367 |.endmacro
368 |
369 |// Move table write barrier back. Overwrites reg.
370 |.macro barrierback, tab, reg
371 | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
372 | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
373 | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
374 | mov tab->gclist, reg
375 |.endmacro
376 |
377 |//-----------------------------------------------------------------------
378
379 /* Generate subroutines used by opcodes and other parts of the VM. */
380 /* The .code_sub section should be last to help static branch prediction. */
381 static void build_subroutines(BuildCtx *ctx)
382 {
383 |.code_sub
384 |
385 |//-----------------------------------------------------------------------
386 |//-- Return handling ----------------------------------------------------
387 |//-----------------------------------------------------------------------
388 |
389 |->vm_returnp:
390 | test PCd, FRAME_P
391 | jz ->cont_dispatch
392 |
393 | // Return from pcall or xpcall fast func.
394 | and PC, -8
395 | sub BASE, PC // Restore caller base.
396 | lea RA, [RA+PC-8] // Rebase RA and prepend one result.
397 | mov PC, [BASE-8] // Fetch PC of previous frame.
398 | // Prepending may overwrite the pcall frame, so do it at the end.
399 | mov_true ITYPE
400 | mov aword [BASE+RA], ITYPE // Prepend true to results.
401 |
402 |->vm_returnc:
403 | add RDd, 1 // RD = nresults+1
404 | jz ->vm_unwind_yield
405 | mov MULTRES, RDd
406 | test PC, FRAME_TYPE
407 | jz ->BC_RET_Z // Handle regular return to Lua.
408 |
409 |->vm_return:
410 | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
411 | xor PC, FRAME_C
412 | test PCd, FRAME_TYPE
413 | jnz ->vm_returnp
414 |
415 | // Return to C.
416 | set_vmstate C
417 | and PC, -8
418 | sub PC, BASE
419 | neg PC // Previous base = BASE - delta.
420 |
421 | sub RDd, 1
422 | jz >2
423 |1: // Move results down.
424 | mov RB, [BASE+RA]
425 | mov [BASE-16], RB
426 | add BASE, 8
427 | sub RDd, 1
428 | jnz <1
429 |2:
430 | mov L:RB, SAVE_L
431 | mov L:RB->base, PC
432 |3:
433 | mov RDd, MULTRES
434 | mov RAd, SAVE_NRES // RA = wanted nresults+1
435 |4:
436 | cmp RAd, RDd
437 | jne >6 // More/less results wanted?
438 |5:
439 | sub BASE, 16
440 | mov L:RB->top, BASE
441 |
442 |->vm_leave_cp:
443 | mov RA, SAVE_CFRAME // Restore previous C frame.
444 | mov L:RB->cframe, RA
445 | xor eax, eax // Ok return status for vm_pcall.
446 |
447 |->vm_leave_unw:
448 | restoreregs
449 | ret
450 |
451 |6:
452 | jb >7 // Less results wanted?
453 | // More results wanted. Check stack size and fill up results with nil.
454 | cmp BASE, L:RB->maxstack
455 | ja >8
456 | mov aword [BASE-16], LJ_TNIL
457 | add BASE, 8
458 | add RDd, 1
459 | jmp <4
460 |
461 |7: // Less results wanted.
462 | test RAd, RAd
463 | jz <5 // But check for LUA_MULTRET+1.
464 | sub RA, RD // Negative result!
465 | lea BASE, [BASE+RA*8] // Correct top.
466 | jmp <5
467 |
468 |8: // Corner case: need to grow stack for filling up results.
469 | // This can happen if:
470 | // - A C function grows the stack (a lot).
471 | // - The GC shrinks the stack in between.
472 | // - A return back from a lua_call() with (high) nresults adjustment.
473 | mov L:RB->top, BASE // Save current top held in BASE (yes).
474 | mov MULTRES, RDd // Need to fill only remainder with nil.
475 | mov CARG2d, RAd
476 | mov CARG1, L:RB
477 | call extern lj_state_growstack // (lua_State *L, int n)
478 | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
479 | jmp <3
480 |
481 |->vm_unwind_yield:
482 | mov al, LUA_YIELD
483 | jmp ->vm_unwind_c_eh
484 |
485 |->vm_unwind_c: // Unwind C stack, return from vm_pcall.
486 | // (void *cframe, int errcode)
487 | mov eax, CARG2d // Error return status for vm_pcall.
488 | mov rsp, CARG1
489 |->vm_unwind_c_eh: // Landing pad for external unwinder.
490 | mov L:RB, SAVE_L
491 | mov GL:RB, L:RB->glref
492 | mov dword GL:RB->vmstate, ~LJ_VMST_C
493 | jmp ->vm_leave_unw
494 |
495 |->vm_unwind_rethrow:
496 |.if not X64WIN
497 | mov CARG1, SAVE_L
498 | mov CARG2d, eax
499 | restoreregs
500 | jmp extern lj_err_throw // (lua_State *L, int errcode)
501 |.endif
502 |
503 |->vm_unwind_ff: // Unwind C stack, return from ff pcall.
504 | // (void *cframe)
505 | and CARG1, CFRAME_RAWMASK
506 | mov rsp, CARG1
507 |->vm_unwind_ff_eh: // Landing pad for external unwinder.
508 | mov L:RB, SAVE_L
509 | mov RDd, 1+1 // Really 1+2 results, incr. later.
510 | mov BASE, L:RB->base
511 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
512 | add DISPATCH, GG_G2DISP
513 | mov PC, [BASE-8] // Fetch PC of previous frame.
514 | mov_false RA
515 | mov RB, [BASE]
516 | mov [BASE-16], RA // Prepend false to error message.
517 | mov [BASE-8], RB
518 | mov RA, -16 // Results start at BASE+RA = BASE-16.
519 | set_vmstate INTERP
520 | jmp ->vm_returnc // Increments RD/MULTRES and returns.
521 |
522 |//-----------------------------------------------------------------------
523 |//-- Grow stack for calls -----------------------------------------------
524 |//-----------------------------------------------------------------------
525 |
526 |->vm_growstack_c: // Grow stack for C function.
527 | mov CARG2d, LUA_MINSTACK
528 | jmp >2
529 |
530 |->vm_growstack_v: // Grow stack for vararg Lua function.
531 | sub RD, 16 // LJ_FR2
532 | jmp >1
533 |
534 |->vm_growstack_f: // Grow stack for fixarg Lua function.
535 | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
536 | lea RD, [BASE+NARGS:RD*8-8]
537 |1:
538 | movzx RAd, byte [PC-4+PC2PROTO(framesize)]
539 | add PC, 4 // Must point after first instruction.
540 | mov L:RB->base, BASE
541 | mov L:RB->top, RD
542 | mov SAVE_PC, PC
543 | mov CARG2, RA
544 |2:
545 | // RB = L, L->base = new base, L->top = top
546 | mov CARG1, L:RB
547 | call extern lj_state_growstack // (lua_State *L, int n)
548 | mov BASE, L:RB->base
549 | mov RD, L:RB->top
550 | mov LFUNC:RB, [BASE-16]
551 | cleartp LFUNC:RB
552 | sub RD, BASE
553 | shr RDd, 3
554 | add NARGS:RDd, 1
555 | // BASE = new base, RB = LFUNC, RD = nargs+1
556 | ins_callt // Just retry the call.
557 |
558 |//-----------------------------------------------------------------------
559 |//-- Entry points into the assembler VM ---------------------------------
560 |//-----------------------------------------------------------------------
561 |
562 |->vm_resume: // Setup C frame and resume thread.
563 | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
564 | saveregs
565 | mov L:RB, CARG1 // Caveat: CARG1 may be RA.
566 | mov SAVE_L, CARG1
567 | mov RA, CARG2
568 | mov PCd, FRAME_CP
569 | xor RDd, RDd
570 | lea KBASE, [esp+CFRAME_RESUME]
571 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
572 | add DISPATCH, GG_G2DISP
573 | mov SAVE_PC, RD // Any value outside of bytecode is ok.
574 | mov SAVE_CFRAME, RD
575 | mov SAVE_NRES, RDd
576 | mov SAVE_ERRF, RDd
577 | mov L:RB->cframe, KBASE
578 | cmp byte L:RB->status, RDL
579 | je >2 // Initial resume (like a call).
580 |
581 | // Resume after yield (like a return).
582 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
583 | set_vmstate INTERP
584 | mov byte L:RB->status, RDL
585 | mov BASE, L:RB->base
586 | mov RD, L:RB->top
587 | sub RD, RA
588 | shr RDd, 3
589 | add RDd, 1 // RD = nresults+1
590 | sub RA, BASE // RA = resultofs
591 | mov PC, [BASE-8]
592 | mov MULTRES, RDd
593 | test PCd, FRAME_TYPE
594 | jz ->BC_RET_Z
595 | jmp ->vm_return
596 |
597 |->vm_pcall: // Setup protected C frame and enter VM.
598 | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
599 | saveregs
600 | mov PCd, FRAME_CP
601 | mov SAVE_ERRF, CARG4d
602 | jmp >1
603 |
604 |->vm_call: // Setup C frame and enter VM.
605 | // (lua_State *L, TValue *base, int nres1)
606 | saveregs
607 | mov PCd, FRAME_C
608 |
609 |1: // Entry point for vm_pcall above (PC = ftype).
610 | mov SAVE_NRES, CARG3d
611 | mov L:RB, CARG1 // Caveat: CARG1 may be RA.
612 | mov SAVE_L, CARG1
613 | mov RA, CARG2
614 |
615 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
616 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
617 | mov SAVE_CFRAME, KBASE
618 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
619 | add DISPATCH, GG_G2DISP
620 | mov L:RB->cframe, rsp
621 |
622 |2: // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype).
623 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
624 | set_vmstate INTERP
625 | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
626 | add PC, RA
627 | sub PC, BASE // PC = frame delta + frame type
628 |
629 | mov RD, L:RB->top
630 | sub RD, RA
631 | shr NARGS:RDd, 3
632 | add NARGS:RDd, 1 // RD = nargs+1
633 |
634 |->vm_call_dispatch:
635 | mov LFUNC:RB, [RA-16]
636 | checkfunc LFUNC:RB, ->vmeta_call // Ensure KBASE defined and != BASE.
637 |
638 |->vm_call_dispatch_f:
639 | mov BASE, RA
640 | ins_call
641 | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
642 |
643 |->vm_cpcall: // Setup protected C frame, call C.
644 | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
645 | saveregs
646 | mov L:RB, CARG1 // Caveat: CARG1 may be RA.
647 | mov SAVE_L, CARG1
648 | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
649 |
650 | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
651 | sub KBASE, L:RB->top
652 | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
653 | mov SAVE_ERRF, 0 // No error function.
654 | mov SAVE_NRES, KBASEd // Neg. delta means cframe w/o frame.
655 | add DISPATCH, GG_G2DISP
656 | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
657 |
658 | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
659 | mov SAVE_CFRAME, KBASE
660 | mov L:RB->cframe, rsp
661 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
662 |
663 | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
664 | // TValue * (new base) or NULL returned in eax (RC).
665 | test RC, RC
666 | jz ->vm_leave_cp // No base? Just remove C frame.
667 | mov RA, RC
668 | mov PCd, FRAME_CP
669 | jmp <2 // Else continue with the call.
670 |
671 |//-----------------------------------------------------------------------
672 |//-- Metamethod handling ------------------------------------------------
673 |//-----------------------------------------------------------------------
674 |
675 |//-- Continuation dispatch ----------------------------------------------
676 |
677 |->cont_dispatch:
678 | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
679 | add RA, BASE
680 | and PC, -8
681 | mov RB, BASE
682 | sub BASE, PC // Restore caller BASE.
683 | mov aword [RA+RD*8-8], LJ_TNIL // Ensure one valid arg.
684 | mov RC, RA // ... in [RC]
685 | mov PC, [RB-24] // Restore PC from [cont|PC].
686 | mov RA, qword [RB-32] // May be negative on WIN64 with debug.
687 |.if FFI
688 | cmp RA, 1
689 | jbe >1
690 |.endif
691 | mov LFUNC:KBASE, [BASE-16]
692 | cleartp LFUNC:KBASE
693 | mov KBASE, LFUNC:KBASE->pc
694 | mov KBASE, [KBASE+PC2PROTO(k)]
695 | // BASE = base, RC = result, RB = meta base
696 | jmp RA // Jump to continuation.
697 |
698 |.if FFI
699 |1:
700 | je ->cont_ffi_callback // cont = 1: return from FFI callback.
701 | // cont = 0: Tail call from C function.
702 | sub RB, BASE
703 | shr RBd, 3
704 | lea RDd, [RBd-3]
705 | jmp ->vm_call_tail
706 |.endif
707 |
708 |->cont_cat: // BASE = base, RC = result, RB = mbase
709 | movzx RAd, PC_RB
710 | sub RB, 32
711 | lea RA, [BASE+RA*8]
712 | sub RA, RB
713 | je ->cont_ra
714 | neg RA
715 | shr RAd, 3
716 |.if X64WIN
717 | mov CARG3d, RAd
718 | mov L:CARG1, SAVE_L
719 | mov L:CARG1->base, BASE
720 | mov RC, [RC]
721 | mov [RB], RC
722 | mov CARG2, RB
723 |.else
724 | mov L:CARG1, SAVE_L
725 | mov L:CARG1->base, BASE
726 | mov CARG3d, RAd
727 | mov RA, [RC]
728 | mov [RB], RA
729 | mov CARG2, RB
730 |.endif
731 | jmp ->BC_CAT_Z
732 |
733 |//-- Table indexing metamethods -----------------------------------------
734 |
735 |->vmeta_tgets:
736 | settp STR:RC, LJ_TSTR // STR:RC = GCstr *
737 | mov TMP1, STR:RC
738 | lea RC, TMP1
739 | cmp PC_OP, BC_GGET
740 | jne >1
741 | settp TAB:RA, TAB:RB, LJ_TTAB // TAB:RB = GCtab *
742 | lea RB, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
743 | mov [RB], TAB:RA
744 | jmp >2
745 |
746 |->vmeta_tgetb:
747 | movzx RCd, PC_RC
748 |.if DUALNUM
749 | setint RC
750 | mov TMP1, RC
751 |.else
752 | cvtsi2sd xmm0, RCd
753 | movsd TMP1, xmm0
754 |.endif
755 | lea RC, TMP1
756 | jmp >1
757 |
758 |->vmeta_tgetv:
759 | movzx RCd, PC_RC // Reload TValue *k from RC.
760 | lea RC, [BASE+RC*8]
761 |1:
762 | movzx RBd, PC_RB // Reload TValue *t from RB.
763 | lea RB, [BASE+RB*8]
764 |2:
765 | mov L:CARG1, SAVE_L
766 | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE.
767 | mov CARG2, RB
768 | mov CARG3, RC
769 | mov L:RB, L:CARG1
770 | mov SAVE_PC, PC
771 | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
772 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
773 | mov BASE, L:RB->base
774 | test RC, RC
775 | jz >3
776 |->cont_ra: // BASE = base, RC = result
777 | movzx RAd, PC_RA
778 | mov RB, [RC]
779 | mov [BASE+RA*8], RB
780 | ins_next
781 |
782 |3: // Call __index metamethod.
783 | // BASE = base, L->top = new base, stack = cont/func/t/k
784 | mov RA, L:RB->top
785 | mov [RA-24], PC // [cont|PC]
786 | lea PC, [RA+FRAME_CONT]
787 | sub PC, BASE
788 | mov LFUNC:RB, [RA-16] // Guaranteed to be a function here.
789 | mov NARGS:RDd, 2+1 // 2 args for func(t, k).
790 | cleartp LFUNC:RB
791 | jmp ->vm_call_dispatch_f
792 |
793 |->vmeta_tgetr:
794 | mov CARG1, TAB:RB
795 | mov RB, BASE // Save BASE.
796 | mov CARG2d, RCd // Caveat: CARG2 == BASE
797 | call extern lj_tab_getinth // (GCtab *t, int32_t key)
798 | // cTValue * or NULL returned in eax (RC).
799 | movzx RAd, PC_RA
800 | mov BASE, RB // Restore BASE.
801 | test RC, RC
802 | jnz ->BC_TGETR_Z
803 | mov ITYPE, LJ_TNIL
804 | jmp ->BC_TGETR2_Z
805 |
806 |//-----------------------------------------------------------------------
807 |
808 |->vmeta_tsets:
809 | settp STR:RC, LJ_TSTR // STR:RC = GCstr *
810 | mov TMP1, STR:RC
811 | lea RC, TMP1
812 | cmp PC_OP, BC_GSET
813 | jne >1
814 | settp TAB:RA, TAB:RB, LJ_TTAB // TAB:RB = GCtab *
815 | lea RB, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
816 | mov [RB], TAB:RA
817 | jmp >2
818 |
819 |->vmeta_tsetb:
820 | movzx RCd, PC_RC
821 |.if DUALNUM
822 | setint RC
823 | mov TMP1, RC
824 |.else
825 | cvtsi2sd xmm0, RCd
826 | movsd TMP1, xmm0
827 |.endif
828 | lea RC, TMP1
829 | jmp >1
830 |
831 |->vmeta_tsetv:
832 | movzx RCd, PC_RC // Reload TValue *k from RC.
833 | lea RC, [BASE+RC*8]
834 |1:
835 | movzx RBd, PC_RB // Reload TValue *t from RB.
836 | lea RB, [BASE+RB*8]
837 |2:
838 | mov L:CARG1, SAVE_L
839 | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE.
840 | mov CARG2, RB
841 | mov CARG3, RC
842 | mov L:RB, L:CARG1
843 | mov SAVE_PC, PC
844 | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
845 | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
846 | mov BASE, L:RB->base
847 | test RC, RC
848 | jz >3
849 | // NOBARRIER: lj_meta_tset ensures the table is not black.
850 | movzx RAd, PC_RA
851 | mov RB, [BASE+RA*8]
852 | mov [RC], RB
853 |->cont_nop: // BASE = base, (RC = result)
854 | ins_next
855 |
856 |3: // Call __newindex metamethod.
857 | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
858 | mov RA, L:RB->top
859 | mov [RA-24], PC // [cont|PC]
860 | movzx RCd, PC_RA
861 | // Copy value to third argument.
862 | mov RB, [BASE+RC*8]
863 | mov [RA+16], RB
864 | lea PC, [RA+FRAME_CONT]
865 | sub PC, BASE
866 | mov LFUNC:RB, [RA-16] // Guaranteed to be a function here.
867 | mov NARGS:RDd, 3+1 // 3 args for func(t, k, v).
868 | cleartp LFUNC:RB
869 | jmp ->vm_call_dispatch_f
870 |
871 |->vmeta_tsetr:
872 |.if X64WIN
873 | mov L:CARG1, SAVE_L
874 | mov CARG3d, RCd
875 | mov L:CARG1->base, BASE
876 | xchg CARG2, TAB:RB // Caveat: CARG2 == BASE.
877 |.else
878 | mov L:CARG1, SAVE_L
879 | mov CARG2, TAB:RB
880 | mov L:CARG1->base, BASE
881 | mov RB, BASE // Save BASE.
882 | mov CARG3d, RCd // Caveat: CARG3 == BASE.
883 |.endif
884 | mov SAVE_PC, PC
885 | call extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key)
886 | // TValue * returned in eax (RC).
887 | movzx RAd, PC_RA
888 | mov BASE, RB // Restore BASE.
889 | jmp ->BC_TSETR_Z
890 |
891 |//-- Comparison metamethods ---------------------------------------------
892 |
893 |->vmeta_comp:
894 | movzx RDd, PC_RD
895 | movzx RAd, PC_RA
896 | mov L:RB, SAVE_L
897 | mov L:RB->base, BASE // Caveat: CARG2/CARG3 == BASE.
898 |.if X64WIN
899 | lea CARG3, [BASE+RD*8]
900 | lea CARG2, [BASE+RA*8]
901 |.else
902 | lea CARG2, [BASE+RA*8]
903 | lea CARG3, [BASE+RD*8]
904 |.endif
905 | mov CARG1, L:RB // Caveat: CARG1/CARG4 == RA.
906 | movzx CARG4d, PC_OP
907 | mov SAVE_PC, PC
908 | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
909 | // 0/1 or TValue * (metamethod) returned in eax (RC).
910 |3:
911 | mov BASE, L:RB->base
912 | cmp RC, 1
913 | ja ->vmeta_binop
914 |4:
915 | lea PC, [PC+4]
916 | jb >6
917 |5:
918 | movzx RDd, PC_RD
919 | branchPC RD
920 |6:
921 | ins_next
922 |
923 |->cont_condt: // BASE = base, RC = result
924 | add PC, 4
925 | mov ITYPE, [RC]
926 | sar ITYPE, 47
927 | cmp ITYPEd, LJ_TISTRUECOND // Branch if result is true.
928 | jb <5
929 | jmp <6
930 |
931 |->cont_condf: // BASE = base, RC = result
932 | mov ITYPE, [RC]
933 | sar ITYPE, 47
934 | cmp ITYPEd, LJ_TISTRUECOND // Branch if result is false.
935 | jmp <4
936 |
937 |->vmeta_equal:
938 | cleartp TAB:RD
939 | sub PC, 4
940 |.if X64WIN
941 | mov CARG3, RD
942 | mov CARG4d, RBd
943 | mov L:RB, SAVE_L
944 | mov L:RB->base, BASE // Caveat: CARG2 == BASE.
945 | mov CARG2, RA
946 | mov CARG1, L:RB // Caveat: CARG1 == RA.
947 |.else
948 | mov CARG2, RA
949 | mov CARG4d, RBd // Caveat: CARG4 == RA.
950 | mov L:RB, SAVE_L
951 | mov L:RB->base, BASE // Caveat: CARG3 == BASE.
952 | mov CARG3, RD
953 | mov CARG1, L:RB
954 |.endif
955 | mov SAVE_PC, PC
956 | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
957 | // 0/1 or TValue * (metamethod) returned in eax (RC).
958 | jmp <3
959 |
960 |->vmeta_equal_cd:
961 |.if FFI
962 | sub PC, 4
963 | mov L:RB, SAVE_L
964 | mov L:RB->base, BASE
965 | mov CARG1, L:RB
966 | mov CARG2d, dword [PC-4]
967 | mov SAVE_PC, PC
968 | call extern lj_meta_equal_cd // (lua_State *L, BCIns ins)
969 | // 0/1 or TValue * (metamethod) returned in eax (RC).
970 | jmp <3
971 |.endif
972 |
973 |->vmeta_istype:
974 | mov L:RB, SAVE_L
975 | mov L:RB->base, BASE // Caveat: CARG2/CARG3 may be BASE.
976 | mov CARG2d, RAd
977 | mov CARG3d, RDd
978 | mov L:CARG1, L:RB
979 | mov SAVE_PC, PC
980 | call extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp)
981 | mov BASE, L:RB->base
982 | jmp <6
983 |
984 |//-- Arithmetic metamethods ---------------------------------------------
985 |
986 |->vmeta_arith_vno:
987 |.if DUALNUM
988 | movzx RBd, PC_RB
989 | movzx RCd, PC_RC
990 |.endif
991 |->vmeta_arith_vn:
992 | lea RC, [KBASE+RC*8]
993 | jmp >1
994 |
995 |->vmeta_arith_nvo:
996 |.if DUALNUM
997 | movzx RBd, PC_RB
998 | movzx RCd, PC_RC
999 |.endif
1000 |->vmeta_arith_nv:
1001 | lea TMPR, [KBASE+RC*8]
1002 | lea RC, [BASE+RB*8]
1003 | mov RB, TMPR
1004 | jmp >2
1005 |
1006 |->vmeta_unm:
1007 | lea RC, [BASE+RD*8]
1008 | mov RB, RC
1009 | jmp >2
1010 |
1011 |->vmeta_arith_vvo:
1012 |.if DUALNUM
1013 | movzx RBd, PC_RB
1014 | movzx RCd, PC_RC
1015 |.endif
1016 |->vmeta_arith_vv:
1017 | lea RC, [BASE+RC*8]
1018 |1:
1019 | lea RB, [BASE+RB*8]
1020 |2:
1021 | lea RA, [BASE+RA*8]
1022 |.if X64WIN
1023 | mov CARG3, RB
1024 | mov CARG4, RC
1025 | movzx RCd, PC_OP
1026 | mov ARG5d, RCd
1027 | mov L:RB, SAVE_L
1028 | mov L:RB->base, BASE // Caveat: CARG2 == BASE.
1029 | mov CARG2, RA
1030 | mov CARG1, L:RB // Caveat: CARG1 == RA.
1031 |.else
1032 | movzx CARG5d, PC_OP
1033 | mov CARG2, RA
1034 | mov CARG4, RC // Caveat: CARG4 == RA.
1035 | mov L:CARG1, SAVE_L
1036 | mov L:CARG1->base, BASE // Caveat: CARG3 == BASE.
1037 | mov CARG3, RB
1038 | mov L:RB, L:CARG1
1039 |.endif
1040 | mov SAVE_PC, PC
1041 | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
1042 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
1043 | mov BASE, L:RB->base
1044 | test RC, RC
1045 | jz ->cont_nop
1046 |
1047 | // Call metamethod for binary op.
1048 |->vmeta_binop:
1049 | // BASE = base, RC = new base, stack = cont/func/o1/o2
1050 | mov RA, RC
1051 | sub RC, BASE
1052 | mov [RA-24], PC // [cont|PC]
1053 | lea PC, [RC+FRAME_CONT]
1054 | mov NARGS:RDd, 2+1 // 2 args for func(o1, o2).
1055 | jmp ->vm_call_dispatch
1056 |
1057 |->vmeta_len:
1058 | movzx RDd, PC_RD
1059 | mov L:RB, SAVE_L
1060 | mov L:RB->base, BASE
1061 | lea CARG2, [BASE+RD*8] // Caveat: CARG2 == BASE
1062 | mov L:CARG1, L:RB
1063 | mov SAVE_PC, PC
1064 | call extern lj_meta_len // (lua_State *L, TValue *o)
1065 | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
1066 | mov BASE, L:RB->base
1067 #if LJ_52
1068 | test RC, RC
1069 | jne ->vmeta_binop // Binop call for compatibility.
1070 | movzx RDd, PC_RD
1071 | mov TAB:CARG1, [BASE+RD*8]
1072 | cleartp TAB:CARG1
1073 | jmp ->BC_LEN_Z
1074 #else
1075 | jmp ->vmeta_binop // Binop call for compatibility.
1076 #endif
1077 |
1078 |//-- Call metamethod ----------------------------------------------------
1079 |
1080 |->vmeta_call_ra:
1081 | lea RA, [BASE+RA*8+16]
1082 |->vmeta_call: // Resolve and call __call metamethod.
1083 | // BASE = old base, RA = new base, RC = nargs+1, PC = return
1084 | mov TMP1d, NARGS:RDd // Save RA, RC for us.
1085 | mov RB, RA
1086 |.if X64WIN
1087 | mov L:TMPR, SAVE_L
1088 | mov L:TMPR->base, BASE // Caveat: CARG2 is BASE.
1089 | lea CARG2, [RA-16]
1090 | lea CARG3, [RA+NARGS:RD*8-8]
1091 | mov CARG1, L:TMPR // Caveat: CARG1 is RA.
1092 |.else
1093 | mov L:CARG1, SAVE_L
1094 | mov L:CARG1->base, BASE // Caveat: CARG3 is BASE.
1095 | lea CARG2, [RA-16]
1096 | lea CARG3, [RA+NARGS:RD*8-8]
1097 |.endif
1098 | mov SAVE_PC, PC
1099 | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
1100 | mov RA, RB
1101 | mov L:RB, SAVE_L
1102 | mov BASE, L:RB->base
1103 | mov NARGS:RDd, TMP1d
1104 | mov LFUNC:RB, [RA-16]
1105 | add NARGS:RDd, 1
1106 | // This is fragile. L->base must not move, KBASE must always be defined.
1107 | cmp KBASE, BASE // Continue with CALLT if flag set.
1108 | je ->BC_CALLT_Z
1109 | cleartp LFUNC:RB
1110 | mov BASE, RA
1111 | ins_call // Otherwise call resolved metamethod.
1112 |
1113 |//-- Argument coercion for 'for' statement ------------------------------
1114 |
1115 |->vmeta_for:
1116 | mov L:RB, SAVE_L
1117 | mov L:RB->base, BASE
1118 | mov CARG2, RA // Caveat: CARG2 == BASE
1119 | mov L:CARG1, L:RB // Caveat: CARG1 == RA
1120 | mov SAVE_PC, PC
1121 | call extern lj_meta_for // (lua_State *L, TValue *base)
1122 | mov BASE, L:RB->base
1123 | mov RCd, [PC-4]
1124 | movzx RAd, RCH
1125 | movzx OP, RCL
1126 | shr RCd, 16
1127 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
1128 |
1129 |//-----------------------------------------------------------------------
1130 |//-- Fast functions -----------------------------------------------------
1131 |//-----------------------------------------------------------------------
1132 |
1133 |.macro .ffunc, name
1134 |->ff_ .. name:
1135 |.endmacro
1136 |
1137 |.macro .ffunc_1, name
1138 |->ff_ .. name:
1139 | cmp NARGS:RDd, 1+1; jb ->fff_fallback
1140 |.endmacro
1141 |
1142 |.macro .ffunc_2, name
1143 |->ff_ .. name:
1144 | cmp NARGS:RDd, 2+1; jb ->fff_fallback
1145 |.endmacro
1146 |
1147 |.macro .ffunc_n, name, op
1148 | .ffunc_1 name
1149 | checknumtp [BASE], ->fff_fallback
1150 | op xmm0, qword [BASE]
1151 |.endmacro
1152 |
1153 |.macro .ffunc_n, name
1154 | .ffunc_n name, movsd
1155 |.endmacro
1156 |
1157 |.macro .ffunc_nn, name
1158 | .ffunc_2 name
1159 | checknumtp [BASE], ->fff_fallback
1160 | checknumtp [BASE+8], ->fff_fallback
1161 | movsd xmm0, qword [BASE]
1162 | movsd xmm1, qword [BASE+8]
1163 |.endmacro
1164 |
1165 |// Inlined GC threshold check. Caveat: uses label 1.
1166 |.macro ffgccheck
1167 | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
1168 | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
1169 | jb >1
1170 | call ->fff_gcstep
1171 |1:
1172 |.endmacro
1173 |
1174 |//-- Base library: checks -----------------------------------------------
1175 |
1176 |.ffunc_1 assert
1177 | mov ITYPE, [BASE]
1178 | mov RB, ITYPE
1179 | sar ITYPE, 47
1180 | cmp ITYPEd, LJ_TISTRUECOND; jae ->fff_fallback
1181 | mov PC, [BASE-8]
1182 | mov MULTRES, RDd
1183 | mov RB, [BASE]
1184 | mov [BASE-16], RB
1185 | sub RDd, 2
1186 | jz >2
1187 | mov RA, BASE
1188 |1:
1189 | add RA, 8
1190 | mov RB, [RA]
1191 | mov [RA-16], RB
1192 | sub RDd, 1
1193 | jnz <1
1194 |2:
1195 | mov RDd, MULTRES
1196 | jmp ->fff_res_
1197 |
1198 |.ffunc_1 type
1199 | mov RC, [BASE]
1200 | sar RC, 47
1201 | mov RBd, LJ_TISNUM
1202 | cmp RCd, RBd
1203 | cmovb RCd, RBd
1204 | not RCd
1205 |2:
1206 | mov CFUNC:RB, [BASE-16]
1207 | cleartp CFUNC:RB
1208 | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
1209 | mov PC, [BASE-8]
1210 | settp STR:RC, LJ_TSTR
1211 | mov [BASE-16], STR:RC
1212 | jmp ->fff_res1
1213 |
1214 |//-- Base library: getters and setters ---------------------------------
1215 |
1216 |.ffunc_1 getmetatable
1217 | mov TAB:RB, [BASE]
1218 | mov PC, [BASE-8]
1219 | checktab TAB:RB, >6
1220 |1: // Field metatable must be at same offset for GCtab and GCudata!
1221 | mov TAB:RB, TAB:RB->metatable
1222 |2:
1223 | test TAB:RB, TAB:RB
1224 | mov aword [BASE-16], LJ_TNIL
1225 | jz ->fff_res1
1226 | settp TAB:RC, TAB:RB, LJ_TTAB
1227 | mov [BASE-16], TAB:RC // Store metatable as default result.
1228 | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+8*(GCROOT_MMNAME+MM_metatable)]
1229 | mov RAd, TAB:RB->hmask
1230 | and RAd, STR:RC->sid
1231 | settp STR:RC, LJ_TSTR
1232 | imul RAd, #NODE
1233 | add NODE:RA, TAB:RB->node
1234 |3: // Rearranged logic, because we expect _not_ to find the key.
1235 | cmp NODE:RA->key, STR:RC
1236 | je >5
1237 |4:
1238 | mov NODE:RA, NODE:RA->next
1239 | test NODE:RA, NODE:RA
1240 | jnz <3
1241 | jmp ->fff_res1 // Not found, keep default result.
1242 |5:
1243 | mov RB, NODE:RA->val
1244 | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
1245 | mov [BASE-16], RB // Return value of mt.__metatable.
1246 | jmp ->fff_res1
1247 |
1248 |6:
1249 | cmp ITYPEd, LJ_TUDATA; je <1
1250 | cmp ITYPEd, LJ_TISNUM; ja >7
1251 | mov ITYPEd, LJ_TISNUM
1252 |7:
1253 | not ITYPEd
1254 | mov TAB:RB, [DISPATCH+ITYPE*8+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
1255 | jmp <2
1256 |
1257 |.ffunc_2 setmetatable
1258 | mov TAB:RB, [BASE]
1259 | mov TAB:TMPR, TAB:RB
1260 | checktab TAB:RB, ->fff_fallback
1261 | // Fast path: no mt for table yet and not clearing the mt.
1262 | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1263 | mov TAB:RA, [BASE+8]
1264 | checktab TAB:RA, ->fff_fallback
1265 | mov TAB:RB->metatable, TAB:RA
1266 | mov PC, [BASE-8]
1267 | mov [BASE-16], TAB:TMPR // Return original table.
1268 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
1269 | jz >1
1270 | // Possible write barrier. Table is black, but skip iswhite(mt) check.
1271 | barrierback TAB:RB, RC
1272 |1:
1273 | jmp ->fff_res1
1274 |
1275 |.ffunc_2 rawget
1276 |.if X64WIN
1277 | mov TAB:RA, [BASE]
1278 | checktab TAB:RA, ->fff_fallback
1279 | mov RB, BASE // Save BASE.
1280 | lea CARG3, [BASE+8]
1281 | mov CARG2, TAB:RA // Caveat: CARG2 == BASE.
1282 | mov CARG1, SAVE_L
1283 |.else
1284 | mov TAB:CARG2, [BASE]
1285 | checktab TAB:CARG2, ->fff_fallback
1286 | mov RB, BASE // Save BASE.
1287 | lea CARG3, [BASE+8] // Caveat: CARG3 == BASE.
1288 | mov CARG1, SAVE_L
1289 |.endif
1290 | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
1291 | // cTValue * returned in eax (RD).
1292 | mov BASE, RB // Restore BASE.
1293 | // Copy table slot.
1294 | mov RB, [RD]
1295 | mov PC, [BASE-8]
1296 | mov [BASE-16], RB
1297 | jmp ->fff_res1
1298 |
1299 |//-- Base library: conversions ------------------------------------------
1300 |
1301 |.ffunc tonumber
1302 | // Only handles the number case inline (without a base argument).
1303 | cmp NARGS:RDd, 1+1; jne ->fff_fallback // Exactly one argument.
1304 | mov RB, [BASE]
1305 | checknumber RB, ->fff_fallback
1306 | mov PC, [BASE-8]
1307 | mov [BASE-16], RB
1308 | jmp ->fff_res1
1309 |
1310 |.ffunc_1 tostring
1311 | // Only handles the string or number case inline.
1312 | mov PC, [BASE-8]
1313 | mov STR:RB, [BASE]
1314 | checktp_nc STR:RB, LJ_TSTR, >3
1315 | // A __tostring method in the string base metatable is ignored.
1316 |2:
1317 | mov [BASE-16], STR:RB
1318 | jmp ->fff_res1
1319 |3: // Handle numbers inline, unless a number base metatable is present.
1320 | cmp ITYPEd, LJ_TISNUM; ja ->fff_fallback_1
1321 | cmp aword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
1322 | jne ->fff_fallback
1323 | ffgccheck // Caveat: uses label 1.
1324 | mov L:RB, SAVE_L
1325 | mov L:RB->base, BASE // Add frame since C call can throw.
1326 | mov SAVE_PC, PC // Redundant (but a defined value).
1327 |.if not X64WIN
1328 | mov CARG2, BASE // Otherwise: CARG2 == BASE
1329 |.endif
1330 | mov L:CARG1, L:RB
1331 |.if DUALNUM
1332 | call extern lj_strfmt_number // (lua_State *L, cTValue *o)
1333 |.else
1334 | call extern lj_strfmt_num // (lua_State *L, lua_Number *np)
1335 |.endif
1336 | // GCstr returned in eax (RD).
1337 | mov BASE, L:RB->base
1338 | settp STR:RB, RD, LJ_TSTR
1339 | jmp <2
1340 |
1341 |//-- Base library: iterators -------------------------------------------
1342 |
1343 |.ffunc_1 next
1344 | je >2 // Missing 2nd arg?
1345 |1:
1346 | mov CARG1, [BASE]
1347 | mov PC, [BASE-8]
1348 | checktab CARG1, ->fff_fallback
1349 | mov RB, BASE // Save BASE.
1350 |.if X64WIN
1351 | lea CARG3, [BASE-16]
1352 | lea CARG2, [BASE+8] // Caveat: CARG2 == BASE.
1353 |.else
1354 | lea CARG2, [BASE+8]
1355 | lea CARG3, [BASE-16] // Caveat: CARG3 == BASE.
1356 |.endif
1357 | call extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o)
1358 | // 1=found, 0=end, -1=error returned in eax (RD).
1359 | mov BASE, RB // Restore BASE.
1360 | test RDd, RDd; jg ->fff_res2 // Found key/value.
1361 | js ->fff_fallback_2 // Invalid key.
1362 | // End of traversal: return nil.
1363 | mov aword [BASE-16], LJ_TNIL
1364 | jmp ->fff_res1
1365 |2: // Set missing 2nd arg to nil.
1366 | mov aword [BASE+8], LJ_TNIL
1367 | jmp <1
1368 |
1369 |.ffunc_1 pairs
1370 | mov TAB:RB, [BASE]
1371 | mov TMPR, TAB:RB
1372 | checktab TAB:RB, ->fff_fallback
1373 #if LJ_52
1374 | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1375 #endif
1376 | mov CFUNC:RD, [BASE-16]
1377 | cleartp CFUNC:RD
1378 | mov CFUNC:RD, CFUNC:RD->upvalue[0]
1379 | settp CFUNC:RD, LJ_TFUNC
1380 | mov PC, [BASE-8]
1381 | mov [BASE-16], CFUNC:RD
1382 | mov [BASE-8], TMPR
1383 | mov aword [BASE], LJ_TNIL
1384 | mov RDd, 1+3
1385 | jmp ->fff_res
1386 |
1387 |.ffunc_2 ipairs_aux
1388 | mov TAB:RB, [BASE]
1389 | checktab TAB:RB, ->fff_fallback
1390 |.if DUALNUM
1391 | mov RA, [BASE+8]
1392 | checkint RA, ->fff_fallback
1393 |.else
1394 | checknumtp [BASE+8], ->fff_fallback
1395 | movsd xmm0, qword [BASE+8]
1396 |.endif
1397 | mov PC, [BASE-8]
1398 |.if DUALNUM
1399 | add RAd, 1
1400 | setint ITYPE, RA
1401 | mov [BASE-16], ITYPE
1402 |.else
1403 | sseconst_1 xmm1, TMPR
1404 | addsd xmm0, xmm1
1405 | cvttsd2si RAd, xmm0
1406 | movsd qword [BASE-16], xmm0
1407 |.endif
1408 | cmp RAd, TAB:RB->asize; jae >2 // Not in array part?
1409 | mov RD, TAB:RB->array
1410 | lea RD, [RD+RA*8]
1411 |1:
1412 | cmp aword [RD], LJ_TNIL; je ->fff_res0
1413 | // Copy array slot.
1414 | mov RB, [RD]
1415 | mov [BASE-8], RB
1416 |->fff_res2:
1417 | mov RDd, 1+2
1418 | jmp ->fff_res
1419 |2: // Check for empty hash part first. Otherwise call C function.
1420 | cmp dword TAB:RB->hmask, 0; je ->fff_res0
1421 |.if X64WIN
1422 | mov TMPR, BASE
1423 | mov CARG2d, RAd
1424 | mov CARG1, TAB:RB
1425 | mov RB, TMPR
1426 |.else
1427 | mov CARG1, TAB:RB
1428 | mov RB, BASE // Save BASE.
1429 | mov CARG2d, RAd // Caveat: CARG2 == BASE
1430 |.endif
1431 | call extern lj_tab_getinth // (GCtab *t, int32_t key)
1432 | // cTValue * or NULL returned in eax (RD).
1433 | mov BASE, RB
1434 | test RD, RD
1435 | jnz <1
1436 |->fff_res0:
1437 | mov RDd, 1+0
1438 | jmp ->fff_res
1439 |
1440 |.ffunc_1 ipairs
1441 | mov TAB:RB, [BASE]
1442 | mov TMPR, TAB:RB
1443 | checktab TAB:RB, ->fff_fallback
1444 #if LJ_52
1445 | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback
1446 #endif
1447 | mov CFUNC:RD, [BASE-16]
1448 | cleartp CFUNC:RD
1449 | mov CFUNC:RD, CFUNC:RD->upvalue[0]
1450 | settp CFUNC:RD, LJ_TFUNC
1451 | mov PC, [BASE-8]
1452 | mov [BASE-16], CFUNC:RD
1453 | mov [BASE-8], TMPR
1454 |.if DUALNUM
1455 | mov64 RD, ((uint64_t)LJ_TISNUM<<47)
1456 | mov [BASE], RD
1457 |.else
1458 | mov qword [BASE], 0
1459 |.endif
1460 | mov RDd, 1+3
1461 | jmp ->fff_res
1462 |
1463 |//-- Base library: catch errors ----------------------------------------
1464 |
1465 |.ffunc_1 pcall
1466 | mov L:RB, SAVE_L
1467 | lea RA, [BASE+NARGS:RD*8]
1468 | cmp RA, L:RB->maxstack; ja ->fff_fallback
1469 | lea RA, [BASE+16]
1470 | sub NARGS:RDd, 1
1471 | mov PCd, 16+FRAME_PCALL
1472 |1:
1473 | movzx RBd, byte [DISPATCH+DISPATCH_GL(hookmask)]
1474 | shr RB, HOOK_ACTIVE_SHIFT
1475 | and RB, 1
1476 | add PC, RB // Remember active hook before pcall.
1477 | // Note: this does a (harmless) copy of the function to the PC slot, too.
1478 | mov KBASE, RD
1479 |2:
1480 | mov RB, [RA+KBASE*8-24]
1481 | mov [RA+KBASE*8-16], RB
1482 | sub KBASE, 1
1483 | ja <2
1484 | jmp ->vm_call_dispatch
1485 |
1486 |.ffunc_2 xpcall
1487 | mov L:RB, SAVE_L
1488 | lea RA, [BASE+NARGS:RD*8]
1489 | cmp RA, L:RB->maxstack; ja ->fff_fallback
1490 | mov LFUNC:RA, [BASE+8]
1491 | checktp_nc LFUNC:RA, LJ_TFUNC, ->fff_fallback
1492 | mov LFUNC:RB, [BASE] // Swap function and traceback.
1493 | mov [BASE], LFUNC:RA
1494 | mov [BASE+8], LFUNC:RB
1495 | lea RA, [BASE+24]
1496 | sub NARGS:RDd, 2
1497 | mov PCd, 24+FRAME_PCALL
1498 | jmp <1
1499 |
1500 |//-- Coroutine library --------------------------------------------------
1501 |
1502 |.macro coroutine_resume_wrap, resume
1503 |.if resume
1504 |.ffunc_1 coroutine_resume
1505 | mov L:RB, [BASE]
1506 | cleartp L:RB
1507 |.else
1508 |.ffunc coroutine_wrap_aux
1509 | mov CFUNC:RB, [BASE-16]
1510 | cleartp CFUNC:RB
1511 | mov L:RB, CFUNC:RB->upvalue[0].gcr
1512 | cleartp L:RB
1513 |.endif
1514 | mov PC, [BASE-8]
1515 | mov SAVE_PC, PC
1516 | mov TMP1, L:RB
1517 |.if resume
1518 | checktptp [BASE], LJ_TTHREAD, ->fff_fallback
1519 |.endif
1520 | cmp aword L:RB->cframe, 0; jne ->fff_fallback
1521 | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
1522 | mov RA, L:RB->top
1523 | je >1 // Status != LUA_YIELD (i.e. 0)?
1524 | cmp RA, L:RB->base // Check for presence of initial func.
1525 | je ->fff_fallback
1526 | mov PC, [RA-8] // Move initial function up.
1527 | mov [RA], PC
1528 | add RA, 8
1529 |1:
1530 |.if resume
1531 | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
1532 |.else
1533 | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
1534 |.endif
1535 | cmp PC, L:RB->maxstack; ja ->fff_fallback
1536 | mov L:RB->top, PC
1537 |
1538 | mov L:RB, SAVE_L
1539 | mov L:RB->base, BASE
1540 |.if resume
1541 | add BASE, 8 // Keep resumed thread in stack for GC.
1542 |.endif
1543 | mov L:RB->top, BASE
1544 |.if resume
1545 | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
1546 |.else
1547 | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
1548 |.endif
1549 | sub RB, PC // Relative to PC.
1550 |
1551 | cmp PC, RA
1552 | je >3
1553 |2: // Move args to coroutine.
1554 | mov RC, [PC+RB]
1555 | mov [PC-8], RC
1556 | sub PC, 8
1557 | cmp PC, RA
1558 | jne <2
1559 |3:
1560 | mov CARG2, RA
1561 | mov CARG1, TMP1
1562 | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
1563 |
1564 | mov L:RB, SAVE_L
1565 | mov L:PC, TMP1
1566 | mov BASE, L:RB->base
1567 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
1568 | set_vmstate INTERP
1569 |
1570 | cmp eax, LUA_YIELD
1571 | ja >8
1572 |4:
1573 | mov RA, L:PC->base
1574 | mov KBASE, L:PC->top
1575 | mov L:PC->top, RA // Clear coroutine stack.
1576 | mov PC, KBASE
1577 | sub PC, RA
1578 | je >6 // No results?
1579 | lea RD, [BASE+PC]
1580 | shr PCd, 3
1581 | cmp RD, L:RB->maxstack
1582 | ja >9 // Need to grow stack?
1583 |
1584 | mov RB, BASE
1585 | sub RB, RA
1586 |5: // Move results from coroutine.
1587 | mov RD, [RA]
1588 | mov [RA+RB], RD
1589 | add RA, 8
1590 | cmp RA, KBASE
1591 | jne <5
1592 |6:
1593 |.if resume
1594 | lea RDd, [PCd+2] // nresults+1 = 1 + true + results.
1595 | mov_true ITYPE // Prepend true to results.
1596 | mov [BASE-8], ITYPE
1597 |.else
1598 | lea RDd, [PCd+1] // nresults+1 = 1 + results.
1599 |.endif
1600 |7:
1601 | mov PC, SAVE_PC
1602 | mov MULTRES, RDd
1603 |.if resume
1604 | mov RA, -8
1605 |.else
1606 | xor RAd, RAd
1607 |.endif
1608 | test PCd, FRAME_TYPE
1609 | jz ->BC_RET_Z
1610 | jmp ->vm_return
1611 |
1612 |8: // Coroutine returned with error (at co->top-1).
1613 |.if resume
1614 | mov_false ITYPE // Prepend false to results.
1615 | mov [BASE-8], ITYPE
1616 | mov RA, L:PC->top
1617 | sub RA, 8
1618 | mov L:PC->top, RA // Clear error from coroutine stack.
1619 | // Copy error message.
1620 | mov RD, [RA]
1621 | mov [BASE], RD
1622 | mov RDd, 1+2 // nresults+1 = 1 + false + error.
1623 | jmp <7
1624 |.else
1625 | mov CARG2, L:PC
1626 | mov CARG1, L:RB
1627 | call extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co)
1628 | // Error function does not return.
1629 |.endif
1630 |
1631 |9: // Handle stack expansion on return from yield.
1632 | mov L:RA, TMP1
1633 | mov L:RA->top, KBASE // Undo coroutine stack clearing.
1634 | mov CARG2, PC
1635 | mov CARG1, L:RB
1636 | call extern lj_state_growstack // (lua_State *L, int n)
1637 | mov L:PC, TMP1
1638 | mov BASE, L:RB->base
1639 | jmp <4 // Retry the stack move.
1640 |.endmacro
1641 |
1642 | coroutine_resume_wrap 1 // coroutine.resume
1643 | coroutine_resume_wrap 0 // coroutine.wrap
1644 |
1645 |.ffunc coroutine_yield
1646 | mov L:RB, SAVE_L
1647 | test aword L:RB->cframe, CFRAME_RESUME
1648 | jz ->fff_fallback
1649 | mov L:RB->base, BASE
1650 | lea RD, [BASE+NARGS:RD*8-8]
1651 | mov L:RB->top, RD
1652 | xor RDd, RDd
1653 | mov aword L:RB->cframe, RD
1654 | mov al, LUA_YIELD
1655 | mov byte L:RB->status, al
1656 | jmp ->vm_leave_unw
1657 |
1658 |//-- Math library -------------------------------------------------------
1659 |
1660 | .ffunc_1 math_abs
1661 | mov RB, [BASE]
1662 |.if DUALNUM
1663 | checkint RB, >3
1664 | cmp RBd, 0; jns ->fff_resi
1665 | neg RBd; js >2
1666 |->fff_resbit:
1667 |->fff_resi:
1668 | setint RB
1669 |->fff_resRB:
1670 | mov PC, [BASE-8]
1671 | mov [BASE-16], RB
1672 | jmp ->fff_res1
1673 |2:
1674 | mov64 RB, U64x(41e00000,00000000) // 2^31.
1675 | jmp ->fff_resRB
1676 |3:
1677 | ja ->fff_fallback
1678 |.else
1679 | checknum RB, ->fff_fallback
1680 |.endif
1681 | shl RB, 1
1682 | shr RB, 1
1683 | mov PC, [BASE-8]
1684 | mov [BASE-16], RB
1685 | jmp ->fff_res1
1686 |
1687 |.ffunc_n math_sqrt, sqrtsd
1688 |->fff_resxmm0:
1689 | mov PC, [BASE-8]
1690 | movsd qword [BASE-16], xmm0
1691 | // fallthrough
1692 |
1693 |->fff_res1:
1694 | mov RDd, 1+1
1695 |->fff_res:
1696 | mov MULTRES, RDd
1697 |->fff_res_:
1698 | test PCd, FRAME_TYPE
1699 | jnz >7
1700 |5:
1701 | cmp PC_RB, RDL // More results expected?
1702 | ja >6
1703 | // Adjust BASE. KBASE is assumed to be set for the calling frame.
1704 | movzx RAd, PC_RA
1705 | neg RA
1706 | lea BASE, [BASE+RA*8-16] // base = base - (RA+2)*8
1707 | ins_next
1708 |
1709 |6: // Fill up results with nil.
1710 | mov aword [BASE+RD*8-24], LJ_TNIL
1711 | add RD, 1
1712 | jmp <5
1713 |
1714 |7: // Non-standard return case.
1715 | mov RA, -16 // Results start at BASE+RA = BASE-16.
1716 | jmp ->vm_return
1717 |
1718 |.macro math_round, func
1719 | .ffunc math_ .. func
1720 |.if DUALNUM
1721 | mov RB, [BASE]
1722 | checknumx RB, ->fff_resRB, je
1723 | ja ->fff_fallback
1724 |.else
1725 | checknumtp [BASE], ->fff_fallback
1726 |.endif
1727 | movsd xmm0, qword [BASE]
1728 | call ->vm_ .. func .. _sse
1729 |.if DUALNUM
1730 | cvttsd2si RBd, xmm0
1731 | cmp RBd, 0x80000000
1732 | jne ->fff_resi
1733 | cvtsi2sd xmm1, RBd
1734 | ucomisd xmm0, xmm1
1735 | jp ->fff_resxmm0
1736 | je ->fff_resi
1737 |.endif
1738 | jmp ->fff_resxmm0
1739 |.endmacro
1740 |
1741 | math_round floor
1742 | math_round ceil
1743 |
1744 |.ffunc math_log
1745 | cmp NARGS:RDd, 1+1; jne ->fff_fallback // Exactly one argument.
1746 | checknumtp [BASE], ->fff_fallback
1747 | movsd xmm0, qword [BASE]
1748 | mov RB, BASE
1749 | call extern log
1750 | mov BASE, RB
1751 | jmp ->fff_resxmm0
1752 |
1753 |.macro math_extern, func
1754 | .ffunc_n math_ .. func
1755 | mov RB, BASE
1756 | call extern func
1757 | mov BASE, RB
1758 | jmp ->fff_resxmm0
1759 |.endmacro
1760 |
1761 |.macro math_extern2, func
1762 | .ffunc_nn math_ .. func
1763 | mov RB, BASE
1764 | call extern func
1765 | mov BASE, RB
1766 | jmp ->fff_resxmm0
1767 |.endmacro
1768 |
1769 | math_extern log10
1770 | math_extern exp
1771 | math_extern sin
1772 | math_extern cos
1773 | math_extern tan
1774 | math_extern asin
1775 | math_extern acos
1776 | math_extern atan
1777 | math_extern sinh
1778 | math_extern cosh
1779 | math_extern tanh
1780 | math_extern2 pow
1781 | math_extern2 atan2
1782 | math_extern2 fmod
1783 |
1784 |.ffunc_2 math_ldexp
1785 | checknumtp [BASE], ->fff_fallback
1786 | checknumtp [BASE+8], ->fff_fallback
1787 | fld qword [BASE+8]
1788 | fld qword [BASE]
1789 | fscale
1790 | fpop1
1791 | mov PC, [BASE-8]
1792 | fstp qword [BASE-16]
1793 | jmp ->fff_res1
1794 |
1795 |.ffunc_n math_frexp
1796 | mov RB, BASE
1797 |.if X64WIN
1798 | lea CARG2, TMP1 // Caveat: CARG2 == BASE
1799 |.else
1800 | lea CARG1, TMP1
1801 |.endif
1802 | call extern frexp
1803 | mov BASE, RB
1804 | mov RBd, TMP1d
1805 | mov PC, [BASE-8]
1806 | movsd qword [BASE-16], xmm0
1807 |.if DUALNUM
1808 | setint RB
1809 | mov [BASE-8], RB
1810 |.else
1811 | cvtsi2sd xmm1, RBd
1812 | movsd qword [BASE-8], xmm1
1813 |.endif
1814 | mov RDd, 1+2
1815 | jmp ->fff_res
1816 |
1817 |.ffunc_n math_modf
1818 | mov RB, BASE
1819 |.if X64WIN
1820 | lea CARG2, [BASE-16] // Caveat: CARG2 == BASE
1821 |.else
1822 | lea CARG1, [BASE-16]
1823 |.endif
1824 | call extern modf
1825 | mov BASE, RB
1826 | mov PC, [BASE-8]
1827 | movsd qword [BASE-8], xmm0
1828 | mov RDd, 1+2
1829 | jmp ->fff_res
1830 |
1831 |.macro math_minmax, name, cmovop, sseop
1832 | .ffunc_1 name
1833 | mov RAd, 2
1834 |.if DUALNUM
1835 | mov RB, [BASE]
1836 | checkint RB, >4
1837 |1: // Handle integers.
1838 | cmp RAd, RDd; jae ->fff_resRB
1839 | mov TMPR, [BASE+RA*8-8]
1840 | checkint TMPR, >3
1841 | cmp RBd, TMPRd
1842 | cmovop RB, TMPR
1843 | add RAd, 1
1844 | jmp <1
1845 |3:
1846 | ja ->fff_fallback
1847 | // Convert intermediate result to number and continue below.
1848 | cvtsi2sd xmm0, RBd
1849 | jmp >6
1850 |4:
1851 | ja ->fff_fallback
1852 |.else
1853 | checknumtp [BASE], ->fff_fallback
1854 |.endif
1855 |
1856 | movsd xmm0, qword [BASE]
1857 |5: // Handle numbers or integers.
1858 | cmp RAd, RDd; jae ->fff_resxmm0
1859 |.if DUALNUM
1860 | mov RB, [BASE+RA*8-8]
1861 | checknumx RB, >6, jb
1862 | ja ->fff_fallback
1863 | cvtsi2sd xmm1, RBd
1864 | jmp >7
1865 |.else
1866 | checknumtp [BASE+RA*8-8], ->fff_fallback
1867 |.endif
1868 |6:
1869 | movsd xmm1, qword [BASE+RA*8-8]
1870 |7:
1871 | sseop xmm0, xmm1
1872 | add RAd, 1
1873 | jmp <5
1874 |.endmacro
1875 |
1876 | math_minmax math_min, cmovg, minsd
1877 | math_minmax math_max, cmovl, maxsd
1878 |
1879 |//-- String library -----------------------------------------------------
1880 |
1881 |.ffunc string_byte // Only handle the 1-arg case here.
1882 | cmp NARGS:RDd, 1+1; jne ->fff_fallback
1883 | mov STR:RB, [BASE]
1884 | checkstr STR:RB, ->fff_fallback
1885 | mov PC, [BASE-8]
1886 | cmp dword STR:RB->len, 1
1887 | jb ->fff_res0 // Return no results for empty string.
1888 | movzx RBd, byte STR:RB[1]
1889 |.if DUALNUM
1890 | jmp ->fff_resi
1891 |.else
1892 | cvtsi2sd xmm0, RBd; jmp ->fff_resxmm0
1893 |.endif
1894 |
1895 |.ffunc string_char // Only handle the 1-arg case here.
1896 | ffgccheck
1897 | cmp NARGS:RDd, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
1898 |.if DUALNUM
1899 | mov RB, [BASE]
1900 | checkint RB, ->fff_fallback
1901 |.else
1902 | checknumtp [BASE], ->fff_fallback
1903 | cvttsd2si RBd, qword [BASE]
1904 |.endif
1905 | cmp RBd, 255; ja ->fff_fallback
1906 | mov TMP1d, RBd
1907 | mov TMPRd, 1
1908 | lea RD, TMP1 // Points to stack. Little-endian.
1909 |->fff_newstr:
1910 | mov L:RB, SAVE_L
1911 | mov L:RB->base, BASE
1912 | mov CARG3d, TMPRd // Zero-extended to size_t.
1913 | mov CARG2, RD
1914 | mov CARG1, L:RB
1915 | mov SAVE_PC, PC
1916 | call extern lj_str_new // (lua_State *L, char *str, size_t l)
1917 |->fff_resstr:
1918 | // GCstr * returned in eax (RD).
1919 | mov BASE, L:RB->base
1920 | mov PC, [BASE-8]
1921 | settp STR:RD, LJ_TSTR
1922 | mov [BASE-16], STR:RD
1923 | jmp ->fff_res1
1924 |
1925 |.ffunc string_sub
1926 | ffgccheck
1927 | mov TMPRd, -1
1928 | cmp NARGS:RDd, 1+2; jb ->fff_fallback
1929 | jna >1
1930 |.if DUALNUM
1931 | mov TMPR, [BASE+16]
1932 | checkint TMPR, ->fff_fallback
1933 |.else
1934 | checknumtp [BASE+16], ->fff_fallback
1935 | cvttsd2si TMPRd, qword [BASE+16]
1936 |.endif
1937 |1:
1938 | mov STR:RB, [BASE]
1939 | checkstr STR:RB, ->fff_fallback
1940 |.if DUALNUM
1941 | mov ITYPE, [BASE+8]
1942 | mov RAd, ITYPEd // Must clear hiword for lea below.
1943 | sar ITYPE, 47
1944 | cmp ITYPEd, LJ_TISNUM
1945 | jne ->fff_fallback
1946 |.else
1947 | checknumtp [BASE+8], ->fff_fallback
1948 | cvttsd2si RAd, qword [BASE+8]
1949 |.endif
1950 | mov RCd, STR:RB->len
1951 | cmp RCd, TMPRd // len < end? (unsigned compare)
1952 | jb >5
1953 |2:
1954 | test RAd, RAd // start <= 0?
1955 | jle >7
1956 |3:
1957 | sub TMPRd, RAd // start > end?
1958 | jl ->fff_emptystr
1959 | lea RD, [STR:RB+RAd+#STR-1]
1960 | add TMPRd, 1
1961 |4:
1962 | jmp ->fff_newstr
1963 |
1964 |5: // Negative end or overflow.
1965 | jl >6
1966 | lea TMPRd, [TMPRd+RCd+1] // end = end+(len+1)
1967 | jmp <2
1968 |6: // Overflow.
1969 | mov TMPRd, RCd // end = len
1970 | jmp <2
1971 |
1972 |7: // Negative start or underflow.
1973 | je >8
1974 | add RAd, RCd // start = start+(len+1)
1975 | add RAd, 1
1976 | jg <3 // start > 0?
1977 |8: // Underflow.
1978 | mov RAd, 1 // start = 1
1979 | jmp <3
1980 |
1981 |->fff_emptystr: // Range underflow.
1982 | xor TMPRd, TMPRd // Zero length. Any ptr in RD is ok.
1983 | jmp <4
1984 |
1985 |.macro ffstring_op, name
1986 | .ffunc_1 string_ .. name
1987 | ffgccheck
1988 |.if X64WIN
1989 | mov STR:TMPR, [BASE]
1990 | checkstr STR:TMPR, ->fff_fallback
1991 |.else
1992 | mov STR:CARG2, [BASE]
1993 | checkstr STR:CARG2, ->fff_fallback
1994 |.endif
1995 | mov L:RB, SAVE_L
1996 | lea SBUF:CARG1, [DISPATCH+DISPATCH_GL(tmpbuf)]
1997 | mov L:RB->base, BASE
1998 |.if X64WIN
1999 | mov STR:CARG2, STR:TMPR // Caveat: CARG2 == BASE
2000 |.endif
2001 | mov RC, SBUF:CARG1->b
2002 | mov SBUF:CARG1->L, L:RB
2003 | mov SBUF:CARG1->w, RC
2004 | mov SAVE_PC, PC
2005 | call extern lj_buf_putstr_ .. name
2006 | mov CARG1, rax
2007 | call extern lj_buf_tostr
2008 | jmp ->fff_resstr
2009 |.endmacro
2010 |
2011 |ffstring_op reverse
2012 |ffstring_op lower
2013 |ffstring_op upper
2014 |
2015 |//-- Bit library --------------------------------------------------------
2016 |
2017 |.macro .ffunc_bit, name, kind, fdef
2018 | fdef name
2019 |.if kind == 2
2020 | sseconst_tobit xmm1, RB
2021 |.endif
2022 |.if DUALNUM
2023 | mov RB, [BASE]
2024 | checkint RB, >1
2025 |.if kind > 0
2026 | jmp >2
2027 |.else
2028 | jmp ->fff_resbit
2029 |.endif
2030 |1:
2031 | ja ->fff_fallback
2032 | movd xmm0, RB
2033 |.else
2034 | checknumtp [BASE], ->fff_fallback
2035 | movsd xmm0, qword [BASE]
2036 |.endif
2037 |.if kind < 2
2038 | sseconst_tobit xmm1, RB
2039 |.endif
2040 | addsd xmm0, xmm1
2041 | movd RBd, xmm0
2042 |2:
2043 |.endmacro
2044 |
2045 |.macro .ffunc_bit, name, kind
2046 | .ffunc_bit name, kind, .ffunc_1
2047 |.endmacro
2048 |
2049 |.ffunc_bit bit_tobit, 0
2050 | jmp ->fff_resbit
2051 |
2052 |.macro .ffunc_bit_op, name, ins
2053 | .ffunc_bit name, 2
2054 | mov TMPRd, NARGS:RDd // Save for fallback.
2055 | lea RD, [BASE+NARGS:RD*8-16]
2056 |1:
2057 | cmp RD, BASE
2058 | jbe ->fff_resbit
2059 |.if DUALNUM
2060 | mov RA, [RD]
2061 | checkint RA, >2
2062 | ins RBd, RAd
2063 | sub RD, 8
2064 | jmp <1
2065 |2:
2066 | ja ->fff_fallback_bit_op
2067 | movd xmm0, RA
2068 |.else
2069 | checknumtp [RD], ->fff_fallback_bit_op
2070 | movsd xmm0, qword [RD]
2071 |.endif
2072 | addsd xmm0, xmm1
2073 | movd RAd, xmm0
2074 | ins RBd, RAd
2075 | sub RD, 8
2076 | jmp <1
2077 |.endmacro
2078 |
2079 |.ffunc_bit_op bit_band, and
2080 |.ffunc_bit_op bit_bor, or
2081 |.ffunc_bit_op bit_bxor, xor
2082 |
2083 |.ffunc_bit bit_bswap, 1
2084 | bswap RBd
2085 | jmp ->fff_resbit
2086 |
2087 |.ffunc_bit bit_bnot, 1
2088 | not RBd
2089 |.if DUALNUM
2090 | jmp ->fff_resbit
2091 |.else
2092 |->fff_resbit:
2093 | cvtsi2sd xmm0, RBd
2094 | jmp ->fff_resxmm0
2095 |.endif
2096 |
2097 |->fff_fallback_bit_op:
2098 | mov NARGS:RDd, TMPRd // Restore for fallback
2099 | jmp ->fff_fallback
2100 |
2101 |.macro .ffunc_bit_sh, name, ins
2102 |.if DUALNUM
2103 | .ffunc_bit name, 1, .ffunc_2
2104 | // Note: no inline conversion from number for 2nd argument!
2105 | mov RA, [BASE+8]
2106 | checkint RA, ->fff_fallback
2107 |.else
2108 | .ffunc_nn name
2109 | sseconst_tobit xmm2, RB
2110 | addsd xmm0, xmm2
2111 | addsd xmm1, xmm2
2112 | movd RBd, xmm0
2113 | movd RAd, xmm1
2114 |.endif
2115 | ins RBd, cl // Assumes RA is ecx.
2116 | jmp ->fff_resbit
2117 |.endmacro
2118 |
2119 |.ffunc_bit_sh bit_lshift, shl
2120 |.ffunc_bit_sh bit_rshift, shr
2121 |.ffunc_bit_sh bit_arshift, sar
2122 |.ffunc_bit_sh bit_rol, rol
2123 |.ffunc_bit_sh bit_ror, ror
2124 |
2125 |//-----------------------------------------------------------------------
2126 |
2127 |->fff_fallback_2:
2128 | mov NARGS:RDd, 1+2 // Other args are ignored, anyway.
2129 | jmp ->fff_fallback
2130 |->fff_fallback_1:
2131 | mov NARGS:RDd, 1+1 // Other args are ignored, anyway.
2132 |->fff_fallback: // Call fast function fallback handler.
2133 | // BASE = new base, RD = nargs+1
2134 | mov L:RB, SAVE_L
2135 | mov PC, [BASE-8] // Fallback may overwrite PC.
2136 | mov SAVE_PC, PC // Redundant (but a defined value).
2137 | mov L:RB->base, BASE
2138 | lea RD, [BASE+NARGS:RD*8-8]
2139 | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
2140 | mov L:RB->top, RD
2141 | mov CFUNC:RD, [BASE-16]
2142 | cleartp CFUNC:RD
2143 | cmp RA, L:RB->maxstack
2144 | ja >5 // Need to grow stack.
2145 | mov CARG1, L:RB
2146 | call aword CFUNC:RD->f // (lua_State *L)
2147 | mov BASE, L:RB->base
2148 | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
2149 | test RDd, RDd; jg ->fff_res // Returned nresults+1?
2150 |1:
2151 | mov RA, L:RB->top
2152 | sub RA, BASE
2153 | shr RAd, 3
2154 | test RDd, RDd
2155 | lea NARGS:RDd, [RAd+1]
2156 | mov LFUNC:RB, [BASE-16]
2157 | jne ->vm_call_tail // Returned -1?
2158 | cleartp LFUNC:RB
2159 | ins_callt // Returned 0: retry fast path.
2160 |
2161 |// Reconstruct previous base for vmeta_call during tailcall.
2162 |->vm_call_tail:
2163 | mov RA, BASE
2164 | test PCd, FRAME_TYPE
2165 | jnz >3
2166 | movzx RBd, PC_RA
2167 | neg RB
2168 | lea BASE, [BASE+RB*8-16] // base = base - (RB+2)*8
2169 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2170 |3:
2171 | mov RB, PC
2172 | and RB, -8
2173 | sub BASE, RB
2174 | jmp ->vm_call_dispatch // Resolve again for tailcall.
2175 |
2176 |5: // Grow stack for fallback handler.
2177 | mov CARG2d, LUA_MINSTACK
2178 | mov CARG1, L:RB
2179 | call extern lj_state_growstack // (lua_State *L, int n)
2180 | mov BASE, L:RB->base
2181 | xor RDd, RDd // Simulate a return 0.
2182 | jmp <1 // Dumb retry (goes through ff first).
2183 |
2184 |->fff_gcstep: // Call GC step function.
2185 | // BASE = new base, RD = nargs+1
2186 | pop RB // Must keep stack at same level.
2187 | mov TMP1, RB // Save return address
2188 | mov L:RB, SAVE_L
2189 | mov SAVE_PC, PC // Redundant (but a defined value).
2190 | mov L:RB->base, BASE
2191 | lea RD, [BASE+NARGS:RD*8-8]
2192 | mov CARG1, L:RB
2193 | mov L:RB->top, RD
2194 | call extern lj_gc_step // (lua_State *L)
2195 | mov BASE, L:RB->base
2196 | mov RD, L:RB->top
2197 | sub RD, BASE
2198 | shr RDd, 3
2199 | add NARGS:RDd, 1
2200 | mov RB, TMP1
2201 | push RB // Restore return address.
2202 | ret
2203 |
2204 |//-----------------------------------------------------------------------
2205 |//-- Special dispatch targets -------------------------------------------
2206 |//-----------------------------------------------------------------------
2207 |
2208 |->vm_record: // Dispatch target for recording phase.
2209 |.if JIT
2210 | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2211 | test RDL, HOOK_VMEVENT // No recording while in vmevent.
2212 | jnz >5
2213 | // Decrement the hookcount for consistency, but always do the call.
2214 | test RDL, HOOK_ACTIVE
2215 | jnz >1
2216 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2217 | jz >1
2218 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2219 | jmp >1
2220 |.endif
2221 |
2222 |->vm_rethook: // Dispatch target for return hooks.
2223 | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2224 | test RDL, HOOK_ACTIVE // Hook already active?
2225 | jnz >5
2226 | jmp >1
2227 |
2228 |->vm_inshook: // Dispatch target for instr/line hooks.
2229 | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)]
2230 | test RDL, HOOK_ACTIVE // Hook already active?
2231 | jnz >5
2232 |
2233 | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
2234 | jz >5
2235 | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
2236 | jz >1
2237 | test RDL, LUA_MASKLINE
2238 | jz >5
2239 |1:
2240 | mov L:RB, SAVE_L
2241 | mov L:RB->base, BASE
2242 | mov CARG2, PC // Caveat: CARG2 == BASE
2243 | mov CARG1, L:RB
2244 | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
2245 | call extern lj_dispatch_ins // (lua_State *L, const BCIns *pc)
2246 |3:
2247 | mov BASE, L:RB->base
2248 |4:
2249 | movzx RAd, PC_RA
2250 |5:
2251 | movzx OP, PC_OP
2252 | movzx RDd, PC_RD
2253 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
2254 |
2255 |->cont_hook: // Continue from hook yield.
2256 | add PC, 4
2257 | mov RA, [RB-40]
2258 | mov MULTRES, RAd // Restore MULTRES for *M ins.
2259 | jmp <4
2260 |
2261 |->vm_hotloop: // Hot loop counter underflow.
2262 |.if JIT
2263 | mov LFUNC:RB, [BASE-16] // Same as curr_topL(L).
2264 | cleartp LFUNC:RB
2265 | mov RB, LFUNC:RB->pc
2266 | movzx RDd, byte [RB+PC2PROTO(framesize)]
2267 | lea RD, [BASE+RD*8]
2268 | mov L:RB, SAVE_L
2269 | mov L:RB->base, BASE
2270 | mov L:RB->top, RD
2271 | mov CARG2, PC
2272 | lea CARG1, [DISPATCH+GG_DISP2J]
2273 | mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2274 | mov SAVE_PC, PC
2275 | call extern lj_trace_hot // (jit_State *J, const BCIns *pc)
2276 | jmp <3
2277 |.endif
2278 |
2279 |->vm_callhook: // Dispatch target for call hooks.
2280 | mov SAVE_PC, PC
2281 |.if JIT
2282 | jmp >1
2283 |.endif
2284 |
2285 |->vm_hotcall: // Hot call counter underflow.
2286 |.if JIT
2287 | mov SAVE_PC, PC
2288 | or PC, 1 // Marker for hot call.
2289 |1:
2290 |.endif
2291 | lea RD, [BASE+NARGS:RD*8-8]
2292 | mov L:RB, SAVE_L
2293 | mov L:RB->base, BASE
2294 | mov L:RB->top, RD
2295 | mov CARG2, PC
2296 | mov CARG1, L:RB
2297 | call extern lj_dispatch_call // (lua_State *L, const BCIns *pc)
2298 | // ASMFunction returned in eax/rax (RD).
2299 | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
2300 |.if JIT
2301 | and PC, -2
2302 |.endif
2303 | mov BASE, L:RB->base
2304 | mov RA, RD
2305 | mov RD, L:RB->top
2306 | sub RD, BASE
2307 | mov RB, RA
2308 | movzx RAd, PC_RA
2309 | shr RDd, 3
2310 | add NARGS:RDd, 1
2311 | jmp RB
2312 |
2313 |->cont_stitch: // Trace stitching.
2314 |.if JIT
2315 | // BASE = base, RC = result, RB = mbase
2316 | mov TRACE:ITYPE, [RB-40] // Save previous trace.
2317 | cleartp TRACE:ITYPE
2318 | mov TMPRd, MULTRES
2319 | movzx RAd, PC_RA
2320 | lea RA, [BASE+RA*8] // Call base.
2321 | sub TMPRd, 1
2322 | jz >2
2323 |1: // Move results down.
2324 | mov RB, [RC]
2325 | mov [RA], RB
2326 | add RC, 8
2327 | add RA, 8
2328 | sub TMPRd, 1
2329 | jnz <1
2330 |2:
2331 | movzx RCd, PC_RA
2332 | movzx RBd, PC_RB
2333 | add RC, RB
2334 | lea RC, [BASE+RC*8-8]
2335 |3:
2336 | cmp RC, RA
2337 | ja >9 // More results wanted?
2338 |
2339 | test TRACE:ITYPE, TRACE:ITYPE
2340 | jz ->cont_nop
2341 | movzx RBd, word TRACE:ITYPE->traceno
2342 | movzx RDd, word TRACE:ITYPE->link
2343 | cmp RDd, RBd
2344 | je ->cont_nop // Blacklisted.
2345 | test RDd, RDd
2346 | jne =>BC_JLOOP // Jump to stitched trace.
2347 |
2348 | // Stitch a new trace to the previous trace.
2349 | mov [DISPATCH+DISPATCH_J(exitno)], RB
2350 | mov L:RB, SAVE_L
2351 | mov L:RB->base, BASE
2352 | mov CARG2, PC
2353 | lea CARG1, [DISPATCH+GG_DISP2J]
2354 | mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2355 | call extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc)
2356 | mov BASE, L:RB->base
2357 | jmp ->cont_nop
2358 |
2359 |9: // Fill up results with nil.
2360 | mov aword [RA], LJ_TNIL
2361 | add RA, 8
2362 | jmp <3
2363 |.endif
2364 |
2365 |->vm_profhook: // Dispatch target for profiler hook.
2366 #if LJ_HASPROFILE
2367 | mov L:RB, SAVE_L
2368 | mov L:RB->base, BASE
2369 | mov CARG2, PC // Caveat: CARG2 == BASE
2370 | mov CARG1, L:RB
2371 | call extern lj_dispatch_profile // (lua_State *L, const BCIns *pc)
2372 | mov BASE, L:RB->base
2373 | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction.
2374 | sub PC, 4
2375 | jmp ->cont_nop
2376 #endif
2377 |
2378 |//-----------------------------------------------------------------------
2379 |//-- Trace exit handler -------------------------------------------------
2380 |//-----------------------------------------------------------------------
2381 |
2382 |// Called from an exit stub with the exit number on the stack.
2383 |// The 16 bit exit number is stored with two (sign-extended) push imm8.
2384 |->vm_exit_handler:
2385 |.if JIT
2386 | push r13; push r12
2387 | push r11; push r10; push r9; push r8
2388 | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
2389 | push rbx; push rdx; push rcx; push rax
2390 | movzx RCd, byte [rbp-8] // Reconstruct exit number.
2391 | mov RCH, byte [rbp-16]
2392 | mov [rbp-8], r15; mov [rbp-16], r14
2393 | // DISPATCH is preserved on-trace in LJ_GC64 mode.
2394 | mov RAd, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
2395 | set_vmstate EXIT
2396 | mov [DISPATCH+DISPATCH_J(exitno)], RCd
2397 | mov [DISPATCH+DISPATCH_J(parent)], RAd
2398 |.if X64WIN
2399 | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
2400 |.else
2401 | sub rsp, 16*8 // Room for SSE regs.
2402 |.endif
2403 | add rbp, -128
2404 | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
2405 | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
2406 | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
2407 | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
2408 | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
2409 | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
2410 | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
2411 | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
2412 | // Caveat: RB is rbp.
2413 | mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)]
2414 | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
2415 | mov aword [DISPATCH+DISPATCH_J(L)], L:RB
2416 | mov L:RB->base, BASE
2417 |.if X64WIN
2418 | lea CARG2, [rsp+4*8]
2419 |.else
2420 | mov CARG2, rsp
2421 |.endif
2422 | lea CARG1, [DISPATCH+GG_DISP2J]
2423 | mov qword [DISPATCH+DISPATCH_GL(jit_base)], 0
2424 | call extern lj_trace_exit // (jit_State *J, ExitState *ex)
2425 | // MULTRES or negated error code returned in eax (RD).
2426 | mov RA, L:RB->cframe
2427 | and RA, CFRAME_RAWMASK
2428 | mov [RA+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
2429 | mov BASE, L:RB->base
2430 | mov PC, [RA+CFRAME_OFS_PC] // Get SAVE_PC.
2431 | jmp >1
2432 |.endif
2433 |->vm_exit_interp:
2434 | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
2435 |.if JIT
2436 | // Restore additional callee-save registers only used in compiled code.
2437 |.if X64WIN
2438 | lea RA, [rsp+10*16+4*8]
2439 |1:
2440 | movdqa xmm15, [RA-10*16]
2441 | movdqa xmm14, [RA-9*16]
2442 | movdqa xmm13, [RA-8*16]
2443 | movdqa xmm12, [RA-7*16]
2444 | movdqa xmm11, [RA-6*16]
2445 | movdqa xmm10, [RA-5*16]
2446 | movdqa xmm9, [RA-4*16]
2447 | movdqa xmm8, [RA-3*16]
2448 | movdqa xmm7, [RA-2*16]
2449 | mov rsp, RA // Reposition stack to C frame.
2450 | movdqa xmm6, [RA-1*16]
2451 | mov r15, CSAVE_1
2452 | mov r14, CSAVE_2
2453 | mov r13, CSAVE_3
2454 | mov r12, CSAVE_4
2455 |.else
2456 | lea RA, [rsp+16]
2457 |1:
2458 | mov r13, [RA-8]
2459 | mov r12, [RA]
2460 | mov rsp, RA // Reposition stack to C frame.
2461 |.endif
2462 | cmp RDd, -LUA_ERRERR; jae >9 // Check for error from exit.
2463 | mov L:RB, SAVE_L
2464 | mov MULTRES, RDd
2465 | mov LFUNC:KBASE, [BASE-16]
2466 | cleartp LFUNC:KBASE
2467 | mov KBASE, LFUNC:KBASE->pc
2468 | mov KBASE, [KBASE+PC2PROTO(k)]
2469 | mov L:RB->base, BASE
2470 | mov qword [DISPATCH+DISPATCH_GL(jit_base)], 0
2471 | set_vmstate INTERP
2472 | // Modified copy of ins_next which handles function header dispatch, too.
2473 | mov RCd, [PC]
2474 | movzx RAd, RCH
2475 | movzx OP, RCL
2476 | add PC, 4
2477 | shr RCd, 16
2478 | cmp MULTRES, -17 // Static dispatch?
2479 | je >5
2480 | cmp OP, BC_FUNCF // Function header?
2481 | jb >3
2482 | cmp OP, BC_FUNCC+2 // Fast function?
2483 | jae >4
2484 |2:
2485 | mov RCd, MULTRES // RC/RD holds nres+1.
2486 |3:
2487 | jmp aword [DISPATCH+OP*8]
2488 |
2489 |4: // Check frame below fast function.
2490 | mov RC, [BASE-8]
2491 | test RCd, FRAME_TYPE
2492 | jnz <2 // Trace stitching continuation?
2493 | // Otherwise set KBASE for Lua function below fast function.
2494 | movzx RCd, byte [RC-3]
2495 | neg RC
2496 | mov LFUNC:KBASE, [BASE+RC*8-32]
2497 | cleartp LFUNC:KBASE
2498 | mov KBASE, LFUNC:KBASE->pc
2499 | mov KBASE, [KBASE+PC2PROTO(k)]
2500 | jmp <2
2501 |
2502 |5: // Dispatch to static entry of original ins replaced by BC_JLOOP.
2503 | mov RA, [DISPATCH+DISPATCH_J(trace)]
2504 | mov TRACE:RA, [RA+RD*8]
2505 | mov RCd, TRACE:RA->startins
2506 | movzx RAd, RCH
2507 | movzx OP, RCL
2508 | shr RCd, 16
2509 | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC]
2510 |
2511 |9: // Rethrow error from the right C frame.
2512 | mov CARG2d, RDd
2513 | mov CARG1, L:RB
2514 | neg CARG2d
2515 | call extern lj_err_trace // (lua_State *L, int errcode)
2516 |.endif
2517 |
2518 |//-----------------------------------------------------------------------
2519 |//-- Math helper functions ----------------------------------------------
2520 |//-----------------------------------------------------------------------
2521 |
2522 |// FP value rounding. Called by math.floor/math.ceil fast functions
2523 |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
2524 |.macro vm_round, name, mode, cond
2525 |->name:
2526 |->name .. _sse:
2527 | sseconst_abs xmm2, RD
2528 | sseconst_2p52 xmm3, RD
2529 | movaps xmm1, xmm0
2530 | andpd xmm1, xmm2 // |x|
2531 | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
2532 | jbe >1
2533 | andnpd xmm2, xmm0 // Isolate sign bit.
2534 |.if mode == 2 // trunc(x)?
2535 | movaps xmm0, xmm1
2536 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
2537 | subsd xmm1, xmm3
2538 | sseconst_1 xmm3, RD
2539 | cmpsd xmm0, xmm1, 1 // |x| < result?
2540 | andpd xmm0, xmm3
2541 | subsd xmm1, xmm0 // If yes, subtract -1.
2542 | orpd xmm1, xmm2 // Merge sign bit back in.
2543 |.else
2544 | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
2545 | subsd xmm1, xmm3
2546 | orpd xmm1, xmm2 // Merge sign bit back in.
2547 | sseconst_1 xmm3, RD
2548 | .if mode == 1 // ceil(x)?
2549 | cmpsd xmm0, xmm1, 6 // x > result?
2550 | andpd xmm0, xmm3
2551 | addsd xmm1, xmm0 // If yes, add 1.
2552 | orpd xmm1, xmm2 // Merge sign bit back in (again).
2553 | .else // floor(x)?
2554 | cmpsd xmm0, xmm1, 1 // x < result?
2555 | andpd xmm0, xmm3
2556 | subsd xmm1, xmm0 // If yes, subtract 1.
2557 | .endif
2558 |.endif
2559 | movaps xmm0, xmm1
2560 |1:
2561 | ret
2562 |.endmacro
2563 |
2564 | vm_round vm_floor, 0, 1
2565 | vm_round vm_ceil, 1, JIT
2566 | vm_round vm_trunc, 2, JIT
2567 |
2568 |// FP modulo x%y. Called by BC_MOD* and vm_arith.
2569 |->vm_mod:
2570 |// Args in xmm0/xmm1, return value in xmm0.
2571 |// Caveat: xmm0-xmm5 and RC (eax) modified!
2572 | movaps xmm5, xmm0
2573 | divsd xmm0, xmm1
2574 | sseconst_abs xmm2, RD
2575 | sseconst_2p52 xmm3, RD
2576 | movaps xmm4, xmm0
2577 | andpd xmm4, xmm2 // |x/y|
2578 | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
2579 | jbe >1
2580 | andnpd xmm2, xmm0 // Isolate sign bit.
2581 | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
2582 | subsd xmm4, xmm3
2583 | orpd xmm4, xmm2 // Merge sign bit back in.
2584 | sseconst_1 xmm2, RD
2585 | cmpsd xmm0, xmm4, 1 // x/y < result?
2586 | andpd xmm0, xmm2
2587 | subsd xmm4, xmm0 // If yes, subtract 1.0.
2588 | movaps xmm0, xmm5
2589 | mulsd xmm1, xmm4
2590 | subsd xmm0, xmm1
2591 | ret
2592 |1:
2593 | mulsd xmm1, xmm0
2594 | movaps xmm0, xmm5
2595 | subsd xmm0, xmm1
2596 | ret
2597 |
2598 |//-----------------------------------------------------------------------
2599 |//-- Miscellaneous functions --------------------------------------------
2600 |//-----------------------------------------------------------------------
2601 |
2602 |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
2603 |->vm_cpuid:
2604 | mov eax, CARG1d
2605 | .if X64WIN; push rsi; mov rsi, CARG2; .endif
2606 | push rbx
2607 | xor ecx, ecx
2608 | cpuid
2609 | mov [rsi], eax
2610 | mov [rsi+4], ebx
2611 | mov [rsi+8], ecx
2612 | mov [rsi+12], edx
2613 | pop rbx
2614 | .if X64WIN; pop rsi; .endif
2615 | ret
2616 |
2617 |.define NEXT_TAB, TAB:CARG1
2618 |.define NEXT_IDX, CARG2d
2619 |.define NEXT_IDXa, CARG2
2620 |.define NEXT_PTR, RC
2621 |.define NEXT_PTRd, RCd
2622 |.define NEXT_TMP, CARG3
2623 |.define NEXT_ASIZE, CARG4d
2624 |.macro NEXT_RES_IDXL, op2; lea edx, [NEXT_IDX+op2]; .endmacro
2625 |.if X64WIN
2626 |.define NEXT_RES_PTR, [rsp+aword*5]
2627 |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro
2628 |.else
2629 |.define NEXT_RES_PTR, [rsp+aword*1]
2630 |.macro NEXT_RES_IDX, op2; lea edx, [NEXT_IDX+op2]; .endmacro
2631 |.endif
2632 |
2633 |// TValue *lj_vm_next(GCtab *t, uint32_t idx)
2634 |// Next idx returned in edx.
2635 |->vm_next:
2636 |.if JIT
2637 | mov NEXT_ASIZE, NEXT_TAB->asize
2638 |1: // Traverse array part.
2639 | cmp NEXT_IDX, NEXT_ASIZE; jae >5
2640 | mov NEXT_TMP, NEXT_TAB->array
2641 | mov NEXT_TMP, qword [NEXT_TMP+NEXT_IDX*8]
2642 | cmp NEXT_TMP, LJ_TNIL; je >2
2643 | lea NEXT_PTR, NEXT_RES_PTR
2644 | mov qword [NEXT_PTR], NEXT_TMP
2645 |.if DUALNUM
2646 | setint NEXT_TMP, NEXT_IDXa
2647 | mov qword [NEXT_PTR+qword*1], NEXT_TMP
2648 |.else
2649 | cvtsi2sd xmm0, NEXT_IDX
2650 | movsd qword [NEXT_PTR+qword*1], xmm0
2651 |.endif
2652 | NEXT_RES_IDX 1
2653 | ret
2654 |2: // Skip holes in array part.
2655 | add NEXT_IDX, 1
2656 | jmp <1
2657 |
2658 |5: // Traverse hash part.
2659 | sub NEXT_IDX, NEXT_ASIZE
2660 |6:
2661 | cmp NEXT_IDX, NEXT_TAB->hmask; ja >9
2662 | imul NEXT_PTRd, NEXT_IDX, #NODE
2663 | add NODE:NEXT_PTR, NEXT_TAB->node
2664 | cmp qword NODE:NEXT_PTR->val, LJ_TNIL; je >7
2665 | NEXT_RES_IDXL NEXT_ASIZE+1
2666 | ret
2667 |7: // Skip holes in hash part.
2668 | add NEXT_IDX, 1
2669 | jmp <6
2670 |
2671 |9: // End of iteration. Set the key to nil (not the value).
2672 | NEXT_RES_IDX NEXT_ASIZE
2673 | lea NEXT_PTR, NEXT_RES_PTR
2674 | mov qword [NEXT_PTR+qword*1], LJ_TNIL
2675 | ret
2676 |.endif
2677 |
2678 |//-----------------------------------------------------------------------
2679 |//-- Assertions ---------------------------------------------------------
2680 |//-----------------------------------------------------------------------
2681 |
2682 |->assert_bad_for_arg_type:
2683 #ifdef LUA_USE_ASSERT
2684 | int3
2685 #endif
2686 | int3
2687 |
2688 |//-----------------------------------------------------------------------
2689 |//-- FFI helper functions -----------------------------------------------
2690 |//-----------------------------------------------------------------------
2691 |
2692 |// Handler for callback functions. Callback slot number in ah/al.
2693 |->vm_ffi_callback:
2694 |.if FFI
2695 |.type CTSTATE, CTState, PC
2696 | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
2697 | lea DISPATCH, [ebp+GG_G2DISP]
2698 | mov CTSTATE, GL:ebp->ctype_state
2699 | movzx eax, ax
2700 | mov CTSTATE->cb.slot, eax
2701 | mov CTSTATE->cb.gpr[0], CARG1
2702 | mov CTSTATE->cb.gpr[1], CARG2
2703 | mov CTSTATE->cb.gpr[2], CARG3
2704 | mov CTSTATE->cb.gpr[3], CARG4
2705 | movsd qword CTSTATE->cb.fpr[0], xmm0
2706 | movsd qword CTSTATE->cb.fpr[1], xmm1
2707 | movsd qword CTSTATE->cb.fpr[2], xmm2
2708 | movsd qword CTSTATE->cb.fpr[3], xmm3
2709 |.if X64WIN
2710 | lea rax, [rsp+CFRAME_SIZE+4*8]
2711 |.else
2712 | lea rax, [rsp+CFRAME_SIZE]
2713 | mov CTSTATE->cb.gpr[4], CARG5
2714 | mov CTSTATE->cb.gpr[5], CARG6
2715 | movsd qword CTSTATE->cb.fpr[4], xmm4
2716 | movsd qword CTSTATE->cb.fpr[5], xmm5
2717 | movsd qword CTSTATE->cb.fpr[6], xmm6
2718 | movsd qword CTSTATE->cb.fpr[7], xmm7
2719 |.endif
2720 | mov CTSTATE->cb.stack, rax
2721 | mov CARG2, rsp
2722 | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
2723 | mov CARG1, CTSTATE
2724 | call extern lj_ccallback_enter // (CTState *cts, void *cf)
2725 | // lua_State * returned in eax (RD).
2726 | set_vmstate INTERP
2727 | mov BASE, L:RD->base
2728 | mov RD, L:RD->top
2729 | sub RD, BASE
2730 | mov LFUNC:RB, [BASE-16]
2731 | cleartp LFUNC:RB
2732 | shr RD, 3
2733 | add RD, 1
2734 | ins_callt
2735 |.endif
2736 |
2737 |->cont_ffi_callback: // Return from FFI callback.
2738 |.if FFI
2739 | mov L:RA, SAVE_L
2740 | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
2741 | mov aword CTSTATE->L, L:RA
2742 | mov L:RA->base, BASE
2743 | mov L:RA->top, RB
2744 | mov CARG1, CTSTATE
2745 | mov CARG2, RC
2746 | call extern lj_ccallback_leave // (CTState *cts, TValue *o)
2747 | mov rax, CTSTATE->cb.gpr[0]
2748 | movsd xmm0, qword CTSTATE->cb.fpr[0]
2749 | jmp ->vm_leave_unw
2750 |.endif
2751 |
2752 |->vm_ffi_call: // Call C function via FFI.
2753 | // Caveat: needs special frame unwinding, see below.
2754 |.if FFI
2755 | .type CCSTATE, CCallState, rbx
2756 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
2757 |
2758 | // Readjust stack.
2759 | mov eax, CCSTATE->spadj
2760 | sub rsp, rax
2761 |
2762 | // Copy stack slots.
2763 | movzx ecx, byte CCSTATE->nsp
2764 | sub ecx, 8
2765 | js >2
2766 |1:
2767 | mov rax, [CCSTATE+rcx+offsetof(CCallState, stack)]
2768 | mov [rsp+rcx+CCALL_SPS_EXTRA*8], rax
2769 | sub ecx, 8
2770 | jns <1
2771 |2:
2772 |
2773 | movzx eax, byte CCSTATE->nfpr
2774 | mov CARG1, CCSTATE->gpr[0]
2775 | mov CARG2, CCSTATE->gpr[1]
2776 | mov CARG3, CCSTATE->gpr[2]
2777 | mov CARG4, CCSTATE->gpr[3]
2778 |.if not X64WIN
2779 | mov CARG5, CCSTATE->gpr[4]
2780 | mov CARG6, CCSTATE->gpr[5]
2781 |.endif
2782 | test eax, eax; jz >5
2783 | movaps xmm0, CCSTATE->fpr[0]
2784 | movaps xmm1, CCSTATE->fpr[1]
2785 | movaps xmm2, CCSTATE->fpr[2]
2786 | movaps xmm3, CCSTATE->fpr[3]
2787 |.if not X64WIN
2788 | cmp eax, 4; jbe >5
2789 | movaps xmm4, CCSTATE->fpr[4]
2790 | movaps xmm5, CCSTATE->fpr[5]
2791 | movaps xmm6, CCSTATE->fpr[6]
2792 | movaps xmm7, CCSTATE->fpr[7]
2793 |.endif
2794 |5:
2795 |
2796 | call aword CCSTATE->func
2797 |
2798 | mov CCSTATE->gpr[0], rax
2799 | movaps CCSTATE->fpr[0], xmm0
2800 |.if not X64WIN
2801 | mov CCSTATE->gpr[1], rdx
2802 | movaps CCSTATE->fpr[1], xmm1
2803 |.endif
2804 |
2805 | mov rbx, [rbp-8]; leave; ret
2806 |.endif
2807 |// Note: vm_ffi_call must be the last function in this object file!
2808 |
2809 |//-----------------------------------------------------------------------
2810 }
2811
2812 /* Generate the code for a single instruction. */
2813 static void build_ins(BuildCtx *ctx, BCOp op, int defop)
2814 {
2815 int vk = 0;
2816 |// Note: aligning all instructions does not pay off.
2817 |=>defop:
2818
2819 switch (op) {
2820
2821 /* -- Comparison ops ---------------------------------------------------- */
2822
2823 /* Remember: all ops branch for a true comparison, fall through otherwise. */
2824
2825 |.macro jmp_comp, lt, ge, le, gt, target
2826 ||switch (op) {
2827 ||case BC_ISLT:
2828 | lt target
2829 ||break;
2830 ||case BC_ISGE:
2831 | ge target
2832 ||break;
2833 ||case BC_ISLE:
2834 | le target
2835 ||break;
2836 ||case BC_ISGT:
2837 | gt target
2838 ||break;
2839 ||default: break; /* Shut up GCC. */
2840 ||}
2841 |.endmacro
2842
2843 case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
2844 | // RA = src1, RD = src2, JMP with RD = target
2845 | ins_AD
2846 | mov ITYPE, [BASE+RA*8]
2847 | mov RB, [BASE+RD*8]
2848 | mov RA, ITYPE
2849 | mov RD, RB
2850 | sar ITYPE, 47
2851 | sar RB, 47
2852 |.if DUALNUM
2853 | cmp ITYPEd, LJ_TISNUM; jne >7
2854 | cmp RBd, LJ_TISNUM; jne >8
2855 | add PC, 4
2856 | cmp RAd, RDd
2857 | jmp_comp jge, jl, jg, jle, >9
2858 |6:
2859 | movzx RDd, PC_RD
2860 | branchPC RD
2861 |9:
2862 | ins_next
2863 |
2864 |7: // RA is not an integer.
2865 | ja ->vmeta_comp
2866 | // RA is a number.
2867 | cmp RBd, LJ_TISNUM; jb >1; jne ->vmeta_comp
2868 | // RA is a number, RD is an integer.
2869 | cvtsi2sd xmm0, RDd
2870 | jmp >2
2871 |
2872 |8: // RA is an integer, RD is not an integer.
2873 | ja ->vmeta_comp
2874 | // RA is an integer, RD is a number.
2875 | cvtsi2sd xmm1, RAd
2876 | movd xmm0, RD
2877 | jmp >3
2878 |.else
2879 | cmp ITYPEd, LJ_TISNUM; jae ->vmeta_comp
2880 | cmp RBd, LJ_TISNUM; jae ->vmeta_comp
2881 |.endif
2882 |1:
2883 | movd xmm0, RD
2884 |2:
2885 | movd xmm1, RA
2886 |3:
2887 | add PC, 4
2888 | ucomisd xmm0, xmm1
2889 | // Unordered: all of ZF CF PF set, ordered: PF clear.
2890 | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
2891 |.if DUALNUM
2892 | jmp_comp jbe, ja, jb, jae, <9
2893 | jmp <6
2894 |.else
2895 | jmp_comp jbe, ja, jb, jae, >1
2896 | movzx RDd, PC_RD
2897 | branchPC RD
2898 |1:
2899 | ins_next
2900 |.endif
2901 break;
2902
2903 case BC_ISEQV: case BC_ISNEV:
2904 vk = op == BC_ISEQV;
2905 | ins_AD // RA = src1, RD = src2, JMP with RD = target
2906 | mov RB, [BASE+RD*8]
2907 | mov ITYPE, [BASE+RA*8]
2908 | add PC, 4
2909 | mov RD, RB
2910 | mov RA, ITYPE
2911 | sar RB, 47
2912 | sar ITYPE, 47
2913 |.if DUALNUM
2914 | cmp RBd, LJ_TISNUM; jne >7
2915 | cmp ITYPEd, LJ_TISNUM; jne >8
2916 | cmp RDd, RAd
2917 if (vk) {
2918 | jne >9
2919 } else {
2920 | je >9
2921 }
2922 | movzx RDd, PC_RD
2923 | branchPC RD
2924 |9:
2925 | ins_next
2926 |
2927 |7: // RD is not an integer.
2928 | ja >5
2929 | // RD is a number.
2930 | movd xmm1, RD
2931 | cmp ITYPEd, LJ_TISNUM; jb >1; jne >5
2932 | // RD is a number, RA is an integer.
2933 | cvtsi2sd xmm0, RAd
2934 | jmp >2
2935 |
2936 |8: // RD is an integer, RA is not an integer.
2937 | ja >5
2938 | // RD is an integer, RA is a number.
2939 | cvtsi2sd xmm1, RDd
2940 | jmp >1
2941 |
2942 |.else
2943 | cmp RBd, LJ_TISNUM; jae >5
2944 | cmp ITYPEd, LJ_TISNUM; jae >5
2945 | movd xmm1, RD
2946 |.endif
2947 |1:
2948 | movd xmm0, RA
2949 |2:
2950 | ucomisd xmm0, xmm1
2951 |4:
2952 iseqne_fp:
2953 if (vk) {
2954 | jp >2 // Unordered means not equal.
2955 | jne >2
2956 } else {
2957 | jp >2 // Unordered means not equal.
2958 | je >1
2959 }
2960 iseqne_end:
2961 if (vk) {
2962 |1: // EQ: Branch to the target.
2963 | movzx RDd, PC_RD
2964 | branchPC RD
2965 |2: // NE: Fallthrough to next instruction.
2966 |.if not FFI
2967 |3:
2968 |.endif
2969 } else {
2970 |.if not FFI
2971 |3:
2972 |.endif
2973 |2: // NE: Branch to the target.
2974 | movzx RDd, PC_RD
2975 | branchPC RD
2976 |1: // EQ: Fallthrough to next instruction.
2977 }
2978 if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
2979 op == BC_ISEQN || op == BC_ISNEN)) {
2980 | jmp <9
2981 } else {
2982 | ins_next
2983 }
2984 |
2985 if (op == BC_ISEQV || op == BC_ISNEV) {
2986 |5: // Either or both types are not numbers.
2987 |.if FFI
2988 | cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd
2989 | cmp ITYPEd, LJ_TCDATA; je ->vmeta_equal_cd
2990 |.endif
2991 | cmp RA, RD
2992 | je <1 // Same GCobjs or pvalues?
2993 | cmp RBd, ITYPEd
2994 | jne <2 // Not the same type?
2995 | cmp RBd, LJ_TISTABUD
2996 | ja <2 // Different objects and not table/ud?
2997 |
2998 | // Different tables or userdatas. Need to check __eq metamethod.
2999 | // Field metatable must be at same offset for GCtab and GCudata!
3000 | cleartp TAB:RA
3001 | mov TAB:RB, TAB:RA->metatable
3002 | test TAB:RB, TAB:RB
3003 | jz <2 // No metatable?
3004 | test byte TAB:RB->nomm, 1<<MM_eq
3005 | jnz <2 // Or 'no __eq' flag set?
3006 if (vk) {
3007 | xor RBd, RBd // ne = 0
3008 } else {
3009 | mov RBd, 1 // ne = 1
3010 }
3011 | jmp ->vmeta_equal // Handle __eq metamethod.
3012 } else {
3013 |.if FFI
3014 |3:
3015 | cmp ITYPEd, LJ_TCDATA
3016 if (LJ_DUALNUM && vk) {
3017 | jne <9
3018 } else {
3019 | jne <2
3020 }
3021 | jmp ->vmeta_equal_cd
3022 |.endif
3023 }
3024 break;
3025 case BC_ISEQS: case BC_ISNES:
3026 vk = op == BC_ISEQS;
3027 | ins_AND // RA = src, RD = str const, JMP with RD = target
3028 | mov RB, [BASE+RA*8]
3029 | add PC, 4
3030 | checkstr RB, >3
3031 | cmp RB, [KBASE+RD*8]
3032 iseqne_test:
3033 if (vk) {
3034 | jne >2
3035 } else {
3036 | je >1
3037 }
3038 goto iseqne_end;
3039 case BC_ISEQN: case BC_ISNEN:
3040 vk = op == BC_ISEQN;
3041 | ins_AD // RA = src, RD = num const, JMP with RD = target
3042 | mov RB, [BASE+RA*8]
3043 | add PC, 4
3044 |.if DUALNUM
3045 | checkint RB, >7
3046 | mov RD, [KBASE+RD*8]
3047 | checkint RD, >8
3048 | cmp RBd, RDd
3049 if (vk) {
3050 | jne >9
3051 } else {
3052 | je >9
3053 }
3054 | movzx RDd, PC_RD
3055 | branchPC RD
3056 |9:
3057 | ins_next
3058 |
3059 |7: // RA is not an integer.
3060 | ja >3
3061 | // RA is a number.
3062 | mov RD, [KBASE+RD*8]
3063 | checkint RD, >1
3064 | // RA is a number, RD is an integer.
3065 | cvtsi2sd xmm0, RDd
3066 | jmp >2
3067 |
3068 |8: // RA is an integer, RD is a number.
3069 | cvtsi2sd xmm0, RBd
3070 | movd xmm1, RD
3071 | ucomisd xmm0, xmm1
3072 | jmp >4
3073 |1:
3074 | movd xmm0, RD
3075 |.else
3076 | checknum RB, >3
3077 |1:
3078 | movsd xmm0, qword [KBASE+RD*8]
3079 |.endif
3080 |2:
3081 | ucomisd xmm0, qword [BASE+RA*8]
3082 |4:
3083 goto iseqne_fp;
3084 case BC_ISEQP: case BC_ISNEP:
3085 vk = op == BC_ISEQP;
3086 | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
3087 | mov RB, [BASE+RA*8]
3088 | sar RB, 47
3089 | add PC, 4
3090 | cmp RBd, RDd
3091 if (!LJ_HASFFI) goto iseqne_test;
3092 if (vk) {
3093 | jne >3
3094 | movzx RDd, PC_RD
3095 | branchPC RD
3096 |2:
3097 | ins_next
3098 |3:
3099 | cmp RBd, LJ_TCDATA; jne <2
3100 | jmp ->vmeta_equal_cd
3101 } else {
3102 | je >2
3103 | cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd
3104 | movzx RDd, PC_RD
3105 | branchPC RD
3106 |2:
3107 | ins_next
3108 }
3109 break;
3110
3111 /* -- Unary test and copy ops ------------------------------------------- */
3112
3113 case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
3114 | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
3115 | mov ITYPE, [BASE+RD*8]
3116 | add PC, 4
3117 if (op == BC_ISTC || op == BC_ISFC) {
3118 | mov RB, ITYPE
3119 }
3120 | sar ITYPE, 47
3121 | cmp ITYPEd, LJ_TISTRUECOND
3122 if (op == BC_IST || op == BC_ISTC) {
3123 | jae >1
3124 } else {
3125 | jb >1
3126 }
3127 if (op == BC_ISTC || op == BC_ISFC) {
3128 | mov [BASE+RA*8], RB
3129 }
3130 | movzx RDd, PC_RD
3131 | branchPC RD
3132 |1: // Fallthrough to the next instruction.
3133 | ins_next
3134 break;
3135
3136 case BC_ISTYPE:
3137 | ins_AD // RA = src, RD = -type
3138 | mov RB, [BASE+RA*8]
3139 | sar RB, 47
3140 | add RBd, RDd
3141 | jne ->vmeta_istype
3142 | ins_next
3143 break;
3144 case BC_ISNUM:
3145 | ins_AD // RA = src, RD = -(TISNUM-1)
3146 | checknumtp [BASE+RA*8], ->vmeta_istype
3147 | ins_next
3148 break;
3149
3150 /* -- Unary ops --------------------------------------------------------- */
3151
3152 case BC_MOV:
3153 | ins_AD // RA = dst, RD = src
3154 | mov RB, [BASE+RD*8]
3155 | mov [BASE+RA*8], RB
3156 | ins_next_
3157 break;
3158 case BC_NOT:
3159 | ins_AD // RA = dst, RD = src
3160 | mov RB, [BASE+RD*8]
3161 | sar RB, 47
3162 | mov RCd, 2
3163 | cmp RB, LJ_TISTRUECOND
3164 | sbb RCd, 0
3165 | shl RC, 47
3166 | not RC
3167 | mov [BASE+RA*8], RC
3168 | ins_next
3169 break;
3170 case BC_UNM:
3171 | ins_AD // RA = dst, RD = src
3172 | mov RB, [BASE+RD*8]
3173 |.if DUALNUM
3174 | checkint RB, >5
3175 | neg RBd
3176 | jo >4
3177 | setint RB
3178 |9:
3179 | mov [BASE+RA*8], RB
3180 | ins_next
3181 |4:
3182 | mov64 RB, U64x(41e00000,00000000) // 2^31.
3183 | jmp <9
3184 |5:
3185 | ja ->vmeta_unm
3186 |.else
3187 | checknum RB, ->vmeta_unm
3188 |.endif
3189 | mov64 RD, U64x(80000000,00000000)
3190 | xor RB, RD
3191 |.if DUALNUM
3192 | jmp <9
3193 |.else
3194 | mov [BASE+RA*8], RB
3195 | ins_next
3196 |.endif
3197 break;
3198 case BC_LEN:
3199 | ins_AD // RA = dst, RD = src
3200 | mov RD, [BASE+RD*8]
3201 | checkstr RD, >2
3202 |.if DUALNUM
3203 | mov RDd, dword STR:RD->len
3204 |1:
3205 | setint RD
3206 | mov [BASE+RA*8], RD
3207 |.else
3208 | xorps xmm0, xmm0
3209 | cvtsi2sd xmm0, dword STR:RD->len
3210 |1:
3211 | movsd qword [BASE+RA*8], xmm0
3212 |.endif
3213 | ins_next
3214 |2:
3215 | cmp ITYPEd, LJ_TTAB; jne ->vmeta_len
3216 | mov TAB:CARG1, TAB:RD
3217 #if LJ_52
3218 | mov TAB:RB, TAB:RD->metatable
3219 | cmp TAB:RB, 0
3220 | jnz >9
3221 |3:
3222 #endif
3223 |->BC_LEN_Z:
3224 | mov RB, BASE // Save BASE.
3225 | call extern lj_tab_len // (GCtab *t)
3226 | // Length of table returned in eax (RD).
3227 |.if DUALNUM
3228 | // Nothing to do.
3229 |.else
3230 | cvtsi2sd xmm0, RDd
3231 |.endif
3232 | mov BASE, RB // Restore BASE.
3233 | movzx RAd, PC_RA
3234 | jmp <1
3235 #if LJ_52
3236 |9: // Check for __len.
3237 | test byte TAB:RB->nomm, 1<<MM_len
3238 | jnz <3
3239 | jmp ->vmeta_len // 'no __len' flag NOT set: check.
3240 #endif
3241 break;
3242
3243 /* -- Binary ops -------------------------------------------------------- */
3244
3245 |.macro ins_arithpre, sseins, ssereg
3246 | ins_ABC
3247 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3248 ||switch (vk) {
3249 ||case 0:
3250 | checknumtp [BASE+RB*8], ->vmeta_arith_vn
3251 | .if DUALNUM
3252 | checknumtp [KBASE+RC*8], ->vmeta_arith_vn
3253 | .endif
3254 | movsd xmm0, qword [BASE+RB*8]
3255 | sseins ssereg, qword [KBASE+RC*8]
3256 || break;
3257 ||case 1:
3258 | checknumtp [BASE+RB*8], ->vmeta_arith_nv
3259 | .if DUALNUM
3260 | checknumtp [KBASE+RC*8], ->vmeta_arith_nv
3261 | .endif
3262 | movsd xmm0, qword [KBASE+RC*8]
3263 | sseins ssereg, qword [BASE+RB*8]
3264 || break;
3265 ||default:
3266 | checknumtp [BASE+RB*8], ->vmeta_arith_vv
3267 | checknumtp [BASE+RC*8], ->vmeta_arith_vv
3268 | movsd xmm0, qword [BASE+RB*8]
3269 | sseins ssereg, qword [BASE+RC*8]
3270 || break;
3271 ||}
3272 |.endmacro
3273 |
3274 |.macro ins_arithdn, intins
3275 | ins_ABC
3276 ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
3277 ||switch (vk) {
3278 ||case 0:
3279 | mov RB, [BASE+RB*8]
3280 | mov RC, [KBASE+RC*8]
3281 | checkint RB, ->vmeta_arith_vno
3282 | checkint RC, ->vmeta_arith_vno
3283 | intins RBd, RCd; jo ->vmeta_arith_vno
3284 || break;
3285 ||case 1:
3286 | mov RB, [BASE+RB*8]
3287 | mov RC, [KBASE+RC*8]
3288 | checkint RB, ->vmeta_arith_nvo
3289 | checkint RC, ->vmeta_arith_nvo
3290 | intins RCd, RBd; jo ->vmeta_arith_nvo
3291 || break;
3292 ||default:
3293 | mov RB, [BASE+RB*8]
3294 | mov RC, [BASE+RC*8]
3295 | checkint RB, ->vmeta_arith_vvo
3296 | checkint RC, ->vmeta_arith_vvo
3297 | intins RBd, RCd; jo ->vmeta_arith_vvo
3298 || break;
3299 ||}
3300 ||if (vk == 1) {
3301 | setint RC
3302 | mov [BASE+RA*8], RC
3303 ||} else {
3304 | setint RB
3305 | mov [BASE+RA*8], RB
3306 ||}
3307 | ins_next
3308 |.endmacro
3309 |
3310 |.macro ins_arithpost
3311 | movsd qword [BASE+RA*8], xmm0
3312 |.endmacro
3313 |
3314 |.macro ins_arith, sseins
3315 | ins_arithpre sseins, xmm0
3316 | ins_arithpost
3317 | ins_next
3318 |.endmacro
3319 |
3320 |.macro ins_arith, intins, sseins
3321 |.if DUALNUM
3322 | ins_arithdn intins
3323 |.else
3324 | ins_arith, sseins
3325 |.endif
3326 |.endmacro
3327
3328 | // RA = dst, RB = src1 or num const, RC = src2 or num const
3329 case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
3330 | ins_arith add, addsd
3331 break;
3332 case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
3333 | ins_arith sub, subsd
3334 break;
3335 case BC_MULVN: case BC_MULNV: case BC_MULVV:
3336 | ins_arith imul, mulsd
3337 break;
3338 case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
3339 | ins_arith divsd
3340 break;
3341 case BC_MODVN:
3342 | ins_arithpre movsd, xmm1
3343 |->BC_MODVN_Z:
3344 | call ->vm_mod
3345 | ins_arithpost
3346 | ins_next
3347 break;
3348 case BC_MODNV: case BC_MODVV:
3349 | ins_arithpre movsd, xmm1
3350 | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
3351 break;
3352 case BC_POW:
3353 | ins_arithpre movsd, xmm1
3354 | mov RB, BASE
3355 | call extern pow
3356 | movzx RAd, PC_RA
3357 | mov BASE, RB
3358 | ins_arithpost
3359 | ins_next
3360 break;
3361
3362 case BC_CAT:
3363 | ins_ABC // RA = dst, RB = src_start, RC = src_end
3364 | mov L:CARG1, SAVE_L
3365 | mov L:CARG1->base, BASE
3366 | lea CARG2, [BASE+RC*8]
3367 | mov CARG3d, RCd
3368 | sub CARG3d, RBd
3369 |->BC_CAT_Z:
3370 | mov L:RB, L:CARG1
3371 | mov SAVE_PC, PC
3372 | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
3373 | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
3374 | mov BASE, L:RB->base
3375 | test RC, RC
3376 | jnz ->vmeta_binop
3377 | movzx RBd, PC_RB // Copy result to Stk[RA] from Stk[RB].
3378 | movzx RAd, PC_RA
3379 | mov RC, [BASE+RB*8]
3380 | mov [BASE+RA*8], RC
3381 | ins_next
3382 break;
3383
3384 /* -- Constant ops ------------------------------------------------------ */
3385
3386 case BC_KSTR:
3387 | ins_AND // RA = dst, RD = str const (~)
3388 | mov RD, [KBASE+RD*8]
3389 | settp RD, LJ_TSTR
3390 | mov [BASE+RA*8], RD
3391 | ins_next
3392 break;
3393 case BC_KCDATA:
3394 |.if FFI
3395 | ins_AND // RA = dst, RD = cdata const (~)
3396 | mov RD, [KBASE+RD*8]
3397 | settp RD, LJ_TCDATA
3398 | mov [BASE+RA*8], RD
3399 | ins_next
3400 |.endif
3401 break;
3402 case BC_KSHORT:
3403 | ins_AD // RA = dst, RD = signed int16 literal
3404 |.if DUALNUM
3405 | movsx RDd, RDW
3406 | setint RD
3407 | mov [BASE+RA*8], RD
3408 |.else
3409 | movsx RDd, RDW // Sign-extend literal.
3410 | cvtsi2sd xmm0, RDd
3411 | movsd qword [BASE+RA*8], xmm0
3412 |.endif
3413 | ins_next
3414 break;
3415 case BC_KNUM:
3416 | ins_AD // RA = dst, RD = num const
3417 | movsd xmm0, qword [KBASE+RD*8]
3418 | movsd qword [BASE+RA*8], xmm0
3419 | ins_next
3420 break;
3421 case BC_KPRI:
3422 | ins_AD // RA = dst, RD = primitive type (~)
3423 | shl RD, 47
3424 | not RD
3425 | mov [BASE+RA*8], RD
3426 | ins_next
3427 break;
3428 case BC_KNIL:
3429 | ins_AD // RA = dst_start, RD = dst_end
3430 | lea RA, [BASE+RA*8+8]
3431 | lea RD, [BASE+RD*8]
3432 | mov RB, LJ_TNIL
3433 | mov [RA-8], RB // Sets minimum 2 slots.
3434 |1:
3435 | mov [RA], RB
3436 | add RA, 8
3437 | cmp RA, RD
3438 | jbe <1
3439 | ins_next
3440 break;
3441
3442 /* -- Upvalue and function ops ------------------------------------------ */
3443
3444 case BC_UGET:
3445 | ins_AD // RA = dst, RD = upvalue #
3446 | mov LFUNC:RB, [BASE-16]
3447 | cleartp LFUNC:RB
3448 | mov UPVAL:RB, [LFUNC:RB+RD*8+offsetof(GCfuncL, uvptr)]
3449 | mov RB, UPVAL:RB->v
3450 | mov RD, [RB]
3451 | mov [BASE+RA*8], RD
3452 | ins_next
3453 break;
3454 case BC_USETV:
3455 #define TV2MARKOFS \
3456 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
3457 | ins_AD // RA = upvalue #, RD = src
3458 | mov LFUNC:RB, [BASE-16]
3459 | cleartp LFUNC:RB
3460 | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3461 | cmp byte UPVAL:RB->closed, 0
3462 | mov RB, UPVAL:RB->v
3463 | mov RA, [BASE+RD*8]
3464 | mov [RB], RA
3465 | jz >1
3466 | // Check barrier for closed upvalue.
3467 | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
3468 | jnz >2
3469 |1:
3470 | ins_next
3471 |
3472 |2: // Upvalue is black. Check if new value is collectable and white.
3473 | mov RD, RA
3474 | sar RD, 47
3475 | sub RDd, LJ_TISGCV
3476 | cmp RDd, LJ_TNUMX - LJ_TISGCV // tvisgcv(v)
3477 | jbe <1
3478 | cleartp GCOBJ:RA
3479 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
3480 | jz <1
3481 | // Crossed a write barrier. Move the barrier forward.
3482 |.if not X64WIN
3483 | mov CARG2, RB
3484 | mov RB, BASE // Save BASE.
3485 |.else
3486 | xchg CARG2, RB // Save BASE (CARG2 == BASE).
3487 |.endif
3488 | lea GL:CARG1, [DISPATCH+GG_DISP2G]
3489 | call extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3490 | mov BASE, RB // Restore BASE.
3491 | jmp <1
3492 break;
3493 #undef TV2MARKOFS
3494 case BC_USETS:
3495 | ins_AND // RA = upvalue #, RD = str const (~)
3496 | mov LFUNC:RB, [BASE-16]
3497 | cleartp LFUNC:RB
3498 | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3499 | mov STR:RA, [KBASE+RD*8]
3500 | mov RD, UPVAL:RB->v
3501 | settp STR:ITYPE, STR:RA, LJ_TSTR
3502 | mov [RD], STR:ITYPE
3503 | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
3504 | jnz >2
3505 |1:
3506 | ins_next
3507 |
3508 |2: // Check if string is white and ensure upvalue is closed.
3509 | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
3510 | jz <1
3511 | cmp byte UPVAL:RB->closed, 0
3512 | jz <1
3513 | // Crossed a write barrier. Move the barrier forward.
3514 | mov RB, BASE // Save BASE (CARG2 == BASE).
3515 | mov CARG2, RD
3516 | lea GL:CARG1, [DISPATCH+GG_DISP2G]
3517 | call extern lj_gc_barrieruv // (global_State *g, TValue *tv)
3518 | mov BASE, RB // Restore BASE.
3519 | jmp <1
3520 break;
3521 case BC_USETN:
3522 | ins_AD // RA = upvalue #, RD = num const
3523 | mov LFUNC:RB, [BASE-16]
3524 | cleartp LFUNC:RB
3525 | movsd xmm0, qword [KBASE+RD*8]
3526 | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3527 | mov RA, UPVAL:RB->v
3528 | movsd qword [RA], xmm0
3529 | ins_next
3530 break;
3531 case BC_USETP:
3532 | ins_AD // RA = upvalue #, RD = primitive type (~)
3533 | mov LFUNC:RB, [BASE-16]
3534 | cleartp LFUNC:RB
3535 | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)]
3536 | shl RD, 47
3537 | not RD
3538 | mov RA, UPVAL:RB->v
3539 | mov [RA], RD
3540 | ins_next
3541 break;
3542 case BC_UCLO:
3543 | ins_AD // RA = level, RD = target
3544 | branchPC RD // Do this first to free RD.
3545 | mov L:RB, SAVE_L
3546 | cmp aword L:RB->openupval, 0
3547 | je >1
3548 | mov L:RB->base, BASE
3549 | lea CARG2, [BASE+RA*8] // Caveat: CARG2 == BASE
3550 | mov L:CARG1, L:RB // Caveat: CARG1 == RA
3551 | call extern lj_func_closeuv // (lua_State *L, TValue *level)
3552 | mov BASE, L:RB->base
3553 |1:
3554 | ins_next
3555 break;
3556
3557 case BC_FNEW:
3558 | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
3559 | mov L:RB, SAVE_L
3560 | mov L:RB->base, BASE // Caveat: CARG2/CARG3 may be BASE.
3561 | mov CARG3, [BASE-16]
3562 | cleartp CARG3
3563 | mov CARG2, [KBASE+RD*8] // Fetch GCproto *.
3564 | mov CARG1, L:RB
3565 | mov SAVE_PC, PC
3566 | // (lua_State *L, GCproto *pt, GCfuncL *parent)
3567 | call extern lj_func_newL_gc
3568 | // GCfuncL * returned in eax (RC).
3569 | mov BASE, L:RB->base
3570 | movzx RAd, PC_RA
3571 | settp LFUNC:RC, LJ_TFUNC
3572 | mov [BASE+RA*8], LFUNC:RC
3573 | ins_next
3574 break;
3575
3576 /* -- Table ops --------------------------------------------------------- */
3577
3578 case BC_TNEW:
3579 | ins_AD // RA = dst, RD = hbits|asize
3580 | mov L:RB, SAVE_L
3581 | mov L:RB->base, BASE
3582 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
3583 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
3584 | mov SAVE_PC, PC
3585 | jae >5
3586 |1:
3587 | mov CARG3d, RDd
3588 | and RDd, 0x7ff
3589 | shr CARG3d, 11
3590 | cmp RDd, 0x7ff
3591 | je >3
3592 |2:
3593 | mov L:CARG1, L:RB
3594 | mov CARG2d, RDd
3595 | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
3596 | // Table * returned in eax (RC).
3597 | mov BASE, L:RB->base
3598 | movzx RAd, PC_RA
3599 | settp TAB:RC, LJ_TTAB
3600 | mov [BASE+RA*8], TAB:RC
3601 | ins_next
3602 |3: // Turn 0x7ff into 0x801.
3603 | mov RDd, 0x801
3604 | jmp <2
3605 |5:
3606 | mov L:CARG1, L:RB
3607 | call extern lj_gc_step_fixtop // (lua_State *L)
3608 | movzx RDd, PC_RD
3609 | jmp <1
3610 break;
3611 case BC_TDUP:
3612 | ins_AND // RA = dst, RD = table const (~) (holding template table)
3613 | mov L:RB, SAVE_L
3614 | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
3615 | mov SAVE_PC, PC
3616 | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
3617 | mov L:RB->base, BASE
3618 | jae >3
3619 |2:
3620 | mov TAB:CARG2, [KBASE+RD*8] // Caveat: CARG2 == BASE
3621 | mov L:CARG1, L:RB // Caveat: CARG1 == RA
3622 | call extern lj_tab_dup // (lua_State *L, Table *kt)
3623 | // Table * returned in eax (RC).
3624 | mov BASE, L:RB->base
3625 | movzx RAd, PC_RA
3626 | settp TAB:RC, LJ_TTAB
3627 | mov [BASE+RA*8], TAB:RC
3628 | ins_next
3629 |3:
3630 | mov L:CARG1, L:RB
3631 | call extern lj_gc_step_fixtop // (lua_State *L)
3632 | movzx RDd, PC_RD // Need to reload RD.
3633 | not RD
3634 | jmp <2
3635 break;
3636
3637 case BC_GGET:
3638 | ins_AND // RA = dst, RD = str const (~)
3639 | mov LFUNC:RB, [BASE-16]
3640 | cleartp LFUNC:RB
3641 | mov TAB:RB, LFUNC:RB->env
3642 | mov STR:RC, [KBASE+RD*8]
3643 | jmp ->BC_TGETS_Z
3644 break;
3645 case BC_GSET:
3646 | ins_AND // RA = src, RD = str const (~)
3647 | mov LFUNC:RB, [BASE-16]
3648 | cleartp LFUNC:RB
3649 | mov TAB:RB, LFUNC:RB->env
3650 | mov STR:RC, [KBASE+RD*8]
3651 | jmp ->BC_TSETS_Z
3652 break;
3653
3654 case BC_TGETV:
3655 | ins_ABC // RA = dst, RB = table, RC = key
3656 | mov TAB:RB, [BASE+RB*8]
3657 | mov RC, [BASE+RC*8]
3658 | checktab TAB:RB, ->vmeta_tgetv
3659 |
3660 | // Integer key?
3661 |.if DUALNUM
3662 | checkint RC, >5
3663 |.else
3664 | // Convert number to int and back and compare.
3665 | checknum RC, >5
3666 | movd xmm0, RC
3667 | cvttsd2si RCd, xmm0
3668 | cvtsi2sd xmm1, RCd
3669 | ucomisd xmm0, xmm1
3670 | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
3671 |.endif
3672 | cmp RCd, TAB:RB->asize // Takes care of unordered, too.
3673 | jae ->vmeta_tgetv // Not in array part? Use fallback.
3674 | shl RCd, 3
3675 | add RC, TAB:RB->array
3676 | // Get array slot.
3677 | mov ITYPE, [RC]
3678 | cmp ITYPE, LJ_TNIL // Avoid overwriting RB in fastpath.
3679 | je >2
3680 |1:
3681 | mov [BASE+RA*8], ITYPE
3682 | ins_next
3683 |
3684 |2: // Check for __index if table value is nil.
3685 | mov TAB:TMPR, TAB:RB->metatable
3686 | test TAB:TMPR, TAB:TMPR
3687 | jz <1
3688 | test byte TAB:TMPR->nomm, 1<<MM_index
3689 | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
3690 | jmp <1
3691 |
3692 |5: // String key?
3693 | cmp ITYPEd, LJ_TSTR; jne ->vmeta_tgetv
3694 | cleartp STR:RC
3695 | jmp ->BC_TGETS_Z
3696 break;
3697 case BC_TGETS:
3698 | ins_ABC // RA = dst, RB = table, RC = str const (~)
3699 | mov TAB:RB, [BASE+RB*8]
3700 | not RC
3701 | mov STR:RC, [KBASE+RC*8]
3702 | checktab TAB:RB, ->vmeta_tgets
3703 |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *
3704 | mov TMPRd, TAB:RB->hmask
3705 | and TMPRd, STR:RC->sid
3706 | imul TMPRd, #NODE
3707 | add NODE:TMPR, TAB:RB->node
3708 | settp ITYPE, STR:RC, LJ_TSTR
3709 |1:
3710 | cmp NODE:TMPR->key, ITYPE
3711 | jne >4
3712 | // Get node value.
3713 | mov ITYPE, NODE:TMPR->val
3714 | cmp ITYPE, LJ_TNIL
3715 | je >5 // Key found, but nil value?
3716 |2:
3717 | mov [BASE+RA*8], ITYPE
3718 | ins_next
3719 |
3720 |4: // Follow hash chain.
3721 | mov NODE:TMPR, NODE:TMPR->next
3722 | test NODE:TMPR, NODE:TMPR
3723 | jnz <1
3724 | // End of hash chain: key not found, nil result.
3725 | mov ITYPE, LJ_TNIL
3726 |
3727 |5: // Check for __index if table value is nil.
3728 | mov TAB:TMPR, TAB:RB->metatable
3729 | test TAB:TMPR, TAB:TMPR
3730 | jz <2 // No metatable: done.
3731 | test byte TAB:TMPR->nomm, 1<<MM_index
3732 | jnz <2 // 'no __index' flag set: done.
3733 | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
3734 break;
3735 case BC_TGETB:
3736 | ins_ABC // RA = dst, RB = table, RC = byte literal
3737 | mov TAB:RB, [BASE+RB*8]
3738 | checktab TAB:RB, ->vmeta_tgetb
3739 | cmp RCd, TAB:RB->asize
3740 | jae ->vmeta_tgetb
3741 | shl RCd, 3
3742 | add RC, TAB:RB->array
3743 | // Get array slot.
3744 | mov ITYPE, [RC]
3745 | cmp ITYPE, LJ_TNIL
3746 | je >2
3747 |1:
3748 | mov [BASE+RA*8], ITYPE
3749 | ins_next
3750 |
3751 |2: // Check for __index if table value is nil.
3752 | mov TAB:TMPR, TAB:RB->metatable
3753 | test TAB:TMPR, TAB:TMPR
3754 | jz <1
3755 | test byte TAB:TMPR->nomm, 1<<MM_index
3756 | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
3757 | jmp <1
3758 break;
3759 case BC_TGETR:
3760 | ins_ABC // RA = dst, RB = table, RC = key
3761 | mov TAB:RB, [BASE+RB*8]
3762 | cleartp TAB:RB
3763 |.if DUALNUM
3764 | mov RCd, dword [BASE+RC*8]
3765 |.else
3766 | cvttsd2si RCd, qword [BASE+RC*8]
3767 |.endif
3768 | cmp RCd, TAB:RB->asize
3769 | jae ->vmeta_tgetr // Not in array part? Use fallback.
3770 | shl RCd, 3
3771 | add RC, TAB:RB->array
3772 | // Get array slot.
3773 |->BC_TGETR_Z:
3774 | mov ITYPE, [RC]
3775 |->BC_TGETR2_Z:
3776 | mov [BASE+RA*8], ITYPE
3777 | ins_next
3778 break;
3779
3780 case BC_TSETV:
3781 | ins_ABC // RA = src, RB = table, RC = key
3782 | mov TAB:RB, [BASE+RB*8]
3783 | mov RC, [BASE+RC*8]
3784 | checktab TAB:RB, ->vmeta_tsetv
3785 |
3786 | // Integer key?
3787 |.if DUALNUM
3788 | checkint RC, >5
3789 |.else
3790 | // Convert number to int and back and compare.
3791 | checknum RC, >5
3792 | movd xmm0, RC
3793 | cvttsd2si RCd, xmm0
3794 | cvtsi2sd xmm1, RCd
3795 | ucomisd xmm0, xmm1
3796 | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
3797 |.endif
3798 | cmp RCd, TAB:RB->asize // Takes care of unordered, too.
3799 | jae ->vmeta_tsetv
3800 | shl RCd, 3
3801 | add RC, TAB:RB->array
3802 | cmp aword [RC], LJ_TNIL
3803 | je >3 // Previous value is nil?
3804 |1:
3805 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
3806 | jnz >7
3807 |2: // Set array slot.
3808 | mov RB, [BASE+RA*8]
3809 | mov [RC], RB
3810 | ins_next
3811 |
3812 |3: // Check for __newindex if previous value is nil.
3813 | mov TAB:TMPR, TAB:RB->metatable
3814 | test TAB:TMPR, TAB:TMPR
3815 | jz <1
3816 | test byte TAB:TMPR->nomm, 1<<MM_newindex
3817 | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
3818 | jmp <1
3819 |
3820 |5: // String key?
3821 | cmp ITYPEd, LJ_TSTR; jne ->vmeta_tsetv
3822 | cleartp STR:RC
3823 | jmp ->BC_TSETS_Z
3824 |
3825 |7: // Possible table write barrier for the value. Skip valiswhite check.
3826 | barrierback TAB:RB, TMPR
3827 | jmp <2
3828 break;
3829 case BC_TSETS:
3830 | ins_ABC // RA = src, RB = table, RC = str const (~)
3831 | mov TAB:RB, [BASE+RB*8]
3832 | not RC
3833 | mov STR:RC, [KBASE+RC*8]
3834 | checktab TAB:RB, ->vmeta_tsets
3835 |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *
3836 | mov TMPRd, TAB:RB->hmask
3837 | and TMPRd, STR:RC->sid
3838 | imul TMPRd, #NODE
3839 | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
3840 | add NODE:TMPR, TAB:RB->node
3841 | settp ITYPE, STR:RC, LJ_TSTR
3842 |1:
3843 | cmp NODE:TMPR->key, ITYPE
3844 | jne >5
3845 | // Ok, key found. Assumes: offsetof(Node, val) == 0
3846 | cmp aword [TMPR], LJ_TNIL
3847 | je >4 // Previous value is nil?
3848 |2:
3849 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
3850 | jnz >7
3851 |3: // Set node value.
3852 | mov ITYPE, [BASE+RA*8]
3853 | mov [TMPR], ITYPE
3854 | ins_next
3855 |
3856 |4: // Check for __newindex if previous value is nil.
3857 | mov TAB:ITYPE, TAB:RB->metatable
3858 | test TAB:ITYPE, TAB:ITYPE
3859 | jz <2
3860 | test byte TAB:ITYPE->nomm, 1<<MM_newindex
3861 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3862 | jmp <2
3863 |
3864 |5: // Follow hash chain.
3865 | mov NODE:TMPR, NODE:TMPR->next
3866 | test NODE:TMPR, NODE:TMPR
3867 | jnz <1
3868 | // End of hash chain: key not found, add a new one.
3869 |
3870 | // But check for __newindex first.
3871 | mov TAB:TMPR, TAB:RB->metatable
3872 | test TAB:TMPR, TAB:TMPR
3873 | jz >6 // No metatable: continue.
3874 | test byte TAB:TMPR->nomm, 1<<MM_newindex
3875 | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
3876 |6:
3877 | mov TMP1, ITYPE
3878 | mov L:CARG1, SAVE_L
3879 | mov L:CARG1->base, BASE
3880 | lea CARG3, TMP1
3881 | mov CARG2, TAB:RB
3882 | mov SAVE_PC, PC
3883 | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
3884 | // Handles write barrier for the new key. TValue * returned in eax (RC).
3885 | mov L:CARG1, SAVE_L
3886 | mov BASE, L:CARG1->base
3887 | mov TMPR, rax
3888 | movzx RAd, PC_RA
3889 | jmp <2 // Must check write barrier for value.
3890 |
3891 |7: // Possible table write barrier for the value. Skip valiswhite check.
3892 | barrierback TAB:RB, ITYPE
3893 | jmp <3
3894 break;
3895 case BC_TSETB:
3896 | ins_ABC // RA = src, RB = table, RC = byte literal
3897 | mov TAB:RB, [BASE+RB*8]
3898 | checktab TAB:RB, ->vmeta_tsetb
3899 | cmp RCd, TAB:RB->asize
3900 | jae ->vmeta_tsetb
3901 | shl RCd, 3
3902 | add RC, TAB:RB->array
3903 | cmp aword [RC], LJ_TNIL
3904 | je >3 // Previous value is nil?
3905 |1:
3906 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
3907 | jnz >7
3908 |2: // Set array slot.
3909 | mov ITYPE, [BASE+RA*8]
3910 | mov [RC], ITYPE
3911 | ins_next
3912 |
3913 |3: // Check for __newindex if previous value is nil.
3914 | mov TAB:TMPR, TAB:RB->metatable
3915 | test TAB:TMPR, TAB:TMPR
3916 | jz <1
3917 | test byte TAB:TMPR->nomm, 1<<MM_newindex
3918 | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
3919 | jmp <1
3920 |
3921 |7: // Possible table write barrier for the value. Skip valiswhite check.
3922 | barrierback TAB:RB, TMPR
3923 | jmp <2
3924 break;
3925 case BC_TSETR:
3926 | ins_ABC // RA = src, RB = table, RC = key
3927 | mov TAB:RB, [BASE+RB*8]
3928 | cleartp TAB:RB
3929 |.if DUALNUM
3930 | mov RC, [BASE+RC*8]
3931 |.else
3932 | cvttsd2si RCd, qword [BASE+RC*8]
3933 |.endif
3934 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
3935 | jnz >7
3936 |2:
3937 | cmp RCd, TAB:RB->asize
3938 | jae ->vmeta_tsetr
3939 | shl RCd, 3
3940 | add RC, TAB:RB->array
3941 | // Set array slot.
3942 |->BC_TSETR_Z:
3943 | mov ITYPE, [BASE+RA*8]
3944 | mov [RC], ITYPE
3945 | ins_next
3946 |
3947 |7: // Possible table write barrier for the value. Skip valiswhite check.
3948 | barrierback TAB:RB, TMPR
3949 | jmp <2
3950 break;
3951
3952 case BC_TSETM:
3953 | ins_AD // RA = base (table at base-1), RD = num const (start index)
3954 |1:
3955 | mov TMPRd, dword [KBASE+RD*8] // Integer constant is in lo-word.
3956 | lea RA, [BASE+RA*8]
3957 | mov TAB:RB, [RA-8] // Guaranteed to be a table.
3958 | cleartp TAB:RB
3959 | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
3960 | jnz >7
3961 |2:
3962 | mov RDd, MULTRES
3963 | sub RDd, 1
3964 | jz >4 // Nothing to copy?
3965 | add RDd, TMPRd // Compute needed size.
3966 | cmp RDd, TAB:RB->asize
3967 | ja >5 // Doesn't fit into array part?
3968 | sub RDd, TMPRd
3969 | shl TMPRd, 3
3970 | add TMPR, TAB:RB->array
3971 |3: // Copy result slots to table.
3972 | mov RB, [RA]
3973 | add RA, 8
3974 | mov [TMPR], RB
3975 | add TMPR, 8
3976 | sub RDd, 1
3977 | jnz <3
3978 |4:
3979 | ins_next
3980 |
3981 |5: // Need to resize array part.
3982 | mov L:CARG1, SAVE_L
3983 | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE.
3984 | mov CARG2, TAB:RB
3985 | mov CARG3d, RDd
3986 | mov L:RB, L:CARG1
3987 | mov SAVE_PC, PC
3988 | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
3989 | mov BASE, L:RB->base
3990 | movzx RAd, PC_RA // Restore RA.
3991 | movzx RDd, PC_RD // Restore RD.
3992 | jmp <1 // Retry.
3993 |
3994 |7: // Possible table write barrier for any value. Skip valiswhite check.
3995 | barrierback TAB:RB, RD
3996 | jmp <2
3997 break;
3998
3999 /* -- Calls and vararg handling ----------------------------------------- */
4000
4001 case BC_CALL: case BC_CALLM:
4002 | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
4003 if (op == BC_CALLM) {
4004 | add NARGS:RDd, MULTRES
4005 }
4006 | mov LFUNC:RB, [BASE+RA*8]
4007 | checkfunc LFUNC:RB, ->vmeta_call_ra
4008 | lea BASE, [BASE+RA*8+16]
4009 | ins_call
4010 break;
4011
4012 case BC_CALLMT:
4013 | ins_AD // RA = base, RD = extra_nargs
4014 | add NARGS:RDd, MULTRES
4015 | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
4016 break;
4017 case BC_CALLT:
4018 | ins_AD // RA = base, RD = nargs+1
4019 | lea RA, [BASE+RA*8+16]
4020 | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
4021 | mov LFUNC:RB, [RA-16]
4022 | checktp_nc LFUNC:RB, LJ_TFUNC, ->vmeta_call
4023 |->BC_CALLT_Z:
4024 | mov PC, [BASE-8]
4025 | test PCd, FRAME_TYPE
4026 | jnz >7
4027 |1:
4028 | mov [BASE-16], LFUNC:RB // Copy func+tag down, reloaded below.
4029 | mov MULTRES, NARGS:RDd
4030 | sub NARGS:RDd, 1
4031 | jz >3
4032 |2: // Move args down.
4033 | mov RB, [RA]
4034 | add RA, 8
4035 | mov [KBASE], RB
4036 | add KBASE, 8
4037 | sub NARGS:RDd, 1
4038 | jnz <2
4039 |
4040 | mov LFUNC:RB, [BASE-16]
4041 |3:
4042 | cleartp LFUNC:RB
4043 | mov NARGS:RDd, MULTRES
4044 | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
4045 | ja >5
4046 |4:
4047 | ins_callt
4048 |
4049 |5: // Tailcall to a fast function.
4050 | test PCd, FRAME_TYPE // Lua frame below?
4051 | jnz <4
4052 | movzx RAd, PC_RA
4053 | neg RA
4054 | mov LFUNC:KBASE, [BASE+RA*8-32] // Need to prepare KBASE.
4055 | cleartp LFUNC:KBASE
4056 | mov KBASE, LFUNC:KBASE->pc
4057 | mov KBASE, [KBASE+PC2PROTO(k)]
4058 | jmp <4
4059 |
4060 |7: // Tailcall from a vararg function.
4061 | sub PC, FRAME_VARG
4062 | test PCd, FRAME_TYPEP
4063 | jnz >8 // Vararg frame below?
4064 | sub BASE, PC // Need to relocate BASE/KBASE down.
4065 | mov KBASE, BASE
4066 | mov PC, [BASE-8]
4067 | jmp <1
4068 |8:
4069 | add PCd, FRAME_VARG
4070 | jmp <1
4071 break;
4072
4073 case BC_ITERC:
4074 | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
4075 | lea RA, [BASE+RA*8+16] // fb = base+2
4076 | mov RB, [RA-32] // Copy state. fb[0] = fb[-4].
4077 | mov RC, [RA-24] // Copy control var. fb[1] = fb[-3].
4078 | mov [RA], RB
4079 | mov [RA+8], RC
4080 | mov LFUNC:RB, [RA-40] // Copy callable. fb[-2] = fb[-5]
4081 | mov [RA-16], LFUNC:RB
4082 | mov NARGS:RDd, 2+1 // Handle like a regular 2-arg call.
4083 | checkfunc LFUNC:RB, ->vmeta_call
4084 | mov BASE, RA
4085 | ins_call
4086 break;
4087
4088 case BC_ITERN:
4089 |.if JIT
4090 | hotloop RBd
4091 |.endif
4092 |->vm_IITERN:
4093 | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
4094 | mov TAB:RB, [BASE+RA*8-16]
4095 | cleartp TAB:RB
4096 | mov RCd, [BASE+RA*8-8] // Get index from control var.
4097 | mov TMPRd, TAB:RB->asize
4098 | add PC, 4
4099 | mov ITYPE, TAB:RB->array
4100 |1: // Traverse array part.
4101 | cmp RCd, TMPRd; jae >5 // Index points after array part?
4102 | cmp aword [ITYPE+RC*8], LJ_TNIL; je >4
4103 |.if not DUALNUM
4104 | cvtsi2sd xmm0, RCd
4105 |.endif
4106 | // Copy array slot to returned value.
4107 | mov RB, [ITYPE+RC*8]
4108 | mov [BASE+RA*8+8], RB
4109 | // Return array index as a numeric key.
4110 |.if DUALNUM
4111 | setint ITYPE, RC
4112 | mov [BASE+RA*8], ITYPE
4113 |.else
4114 | movsd qword [BASE+RA*8], xmm0
4115 |.endif
4116 | add RCd, 1
4117 | mov [BASE+RA*8-8], RCd // Update control var.
4118 |2:
4119 | movzx RDd, PC_RD // Get target from ITERL.
4120 | branchPC RD
4121 |3:
4122 | ins_next
4123 |
4124 |4: // Skip holes in array part.
4125 | add RCd, 1
4126 | jmp <1
4127 |
4128 |5: // Traverse hash part.
4129 | sub RCd, TMPRd
4130 |6:
4131 | cmp RCd, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
4132 | imul ITYPEd, RCd, #NODE
4133 | add NODE:ITYPE, TAB:RB->node
4134 | cmp aword NODE:ITYPE->val, LJ_TNIL; je >7
4135 | lea TMPRd, [RCd+TMPRd+1]
4136 | // Copy key and value from hash slot.
4137 | mov RB, NODE:ITYPE->key
4138 | mov RC, NODE:ITYPE->val
4139 | mov [BASE+RA*8], RB
4140 | mov [BASE+RA*8+8], RC
4141 | mov [BASE+RA*8-8], TMPRd
4142 | jmp <2
4143 |
4144 |7: // Skip holes in hash part.
4145 | add RCd, 1
4146 | jmp <6
4147 break;
4148
4149 case BC_ISNEXT:
4150 | ins_AD // RA = base, RD = target (points to ITERN)
4151 | mov CFUNC:RB, [BASE+RA*8-24]
4152 | checkfunc CFUNC:RB, >5
4153 | checktptp [BASE+RA*8-16], LJ_TTAB, >5
4154 | cmp aword [BASE+RA*8-8], LJ_TNIL; jne >5
4155 | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
4156 | branchPC RD
4157 | mov64 TMPR, ((uint64_t)LJ_KEYINDEX << 32)
4158 | mov [BASE+RA*8-8], TMPR // Initialize control var.
4159 |1:
4160 | ins_next
4161 |5: // Despecialize bytecode if any of the checks fail.
4162 | mov PC_OP, BC_JMP
4163 | branchPC RD
4164 |.if JIT
4165 | cmp byte [PC], BC_ITERN
4166 | jne >6
4167 |.endif
4168 | mov byte [PC], BC_ITERC
4169 | jmp <1
4170 |.if JIT
4171 |6: // Unpatch JLOOP.
4172 | mov RA, [DISPATCH+DISPATCH_J(trace)]
4173 | movzx RCd, word [PC+2]
4174 | mov TRACE:RA, [RA+RC*8]
4175 | mov eax, TRACE:RA->startins
4176 | mov al, BC_ITERC
4177 | mov dword [PC], eax
4178 | jmp <1
4179 |.endif
4180 break;
4181
4182 case BC_VARG:
4183 | ins_ABC // RA = base, RB = nresults+1, RC = numparams
4184 | lea TMPR, [BASE+RC*8+(16+FRAME_VARG)]
4185 | lea RA, [BASE+RA*8]
4186 | sub TMPR, [BASE-8]
4187 | // Note: TMPR may now be even _above_ BASE if nargs was < numparams.
4188 | test RB, RB
4189 | jz >5 // Copy all varargs?
4190 | lea RB, [RA+RB*8-8]
4191 | cmp TMPR, BASE // No vararg slots?
4192 | jnb >2
4193 |1: // Copy vararg slots to destination slots.
4194 | mov RC, [TMPR-16]
4195 | add TMPR, 8
4196 | mov [RA], RC
4197 | add RA, 8
4198 | cmp RA, RB // All destination slots filled?
4199 | jnb >3
4200 | cmp TMPR, BASE // No more vararg slots?
4201 | jb <1
4202 |2: // Fill up remainder with nil.
4203 | mov aword [RA], LJ_TNIL
4204 | add RA, 8
4205 | cmp RA, RB
4206 | jb <2
4207 |3:
4208 | ins_next
4209 |
4210 |5: // Copy all varargs.
4211 | mov MULTRES, 1 // MULTRES = 0+1
4212 | mov RC, BASE
4213 | sub RC, TMPR
4214 | jbe <3 // No vararg slots?
4215 | mov RBd, RCd
4216 | shr RBd, 3
4217 | add RBd, 1
4218 | mov MULTRES, RBd // MULTRES = #varargs+1
4219 | mov L:RB, SAVE_L
4220 | add RC, RA
4221 | cmp RC, L:RB->maxstack
4222 | ja >7 // Need to grow stack?
4223 |6: // Copy all vararg slots.
4224 | mov RC, [TMPR-16]
4225 | add TMPR, 8
4226 | mov [RA], RC
4227 | add RA, 8
4228 | cmp TMPR, BASE // No more vararg slots?
4229 | jb <6
4230 | jmp <3
4231 |
4232 |7: // Grow stack for varargs.
4233 | mov L:RB->base, BASE
4234 | mov L:RB->top, RA
4235 | mov SAVE_PC, PC
4236 | sub TMPR, BASE // Need delta, because BASE may change.
4237 | mov TMP1hi, TMPRd
4238 | mov CARG2d, MULTRES
4239 | sub CARG2d, 1
4240 | mov CARG1, L:RB
4241 | call extern lj_state_growstack // (lua_State *L, int n)
4242 | mov BASE, L:RB->base
4243 | movsxd TMPR, TMP1hi
4244 | mov RA, L:RB->top
4245 | add TMPR, BASE
4246 | jmp <6
4247 break;
4248
4249 /* -- Returns ----------------------------------------------------------- */
4250
4251 case BC_RETM:
4252 | ins_AD // RA = results, RD = extra_nresults
4253 | add RDd, MULTRES // MULTRES >=1, so RD >=1.
4254 | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
4255 break;
4256
4257 case BC_RET: case BC_RET0: case BC_RET1:
4258 | ins_AD // RA = results, RD = nresults+1
4259 if (op != BC_RET0) {
4260 | shl RAd, 3
4261 }
4262 |1:
4263 | mov PC, [BASE-8]
4264 | mov MULTRES, RDd // Save nresults+1.
4265 | test PCd, FRAME_TYPE // Check frame type marker.
4266 | jnz >7 // Not returning to a fixarg Lua func?
4267 switch (op) {
4268 case BC_RET:
4269 |->BC_RET_Z:
4270 | mov KBASE, BASE // Use KBASE for result move.
4271 | sub RDd, 1
4272 | jz >3
4273 |2: // Move results down.
4274 | mov RB, [KBASE+RA]
4275 | mov [KBASE-16], RB
4276 | add KBASE, 8
4277 | sub RDd, 1
4278 | jnz <2
4279 |3:
4280 | mov RDd, MULTRES // Note: MULTRES may be >255.
4281 | movzx RBd, PC_RB // So cannot compare with RDL!
4282 |5:
4283 | cmp RBd, RDd // More results expected?
4284 | ja >6
4285 break;
4286 case BC_RET1:
4287 | mov RB, [BASE+RA]
4288 | mov [BASE-16], RB
4289 /* fallthrough */
4290 case BC_RET0:
4291 |5:
4292 | cmp PC_RB, RDL // More results expected?
4293 | ja >6
4294 default:
4295 break;
4296 }
4297 | movzx RAd, PC_RA
4298 | neg RA
4299 | lea BASE, [BASE+RA*8-16] // base = base - (RA+2)*8
4300 | mov LFUNC:KBASE, [BASE-16]
4301 | cleartp LFUNC:KBASE
4302 | mov KBASE, LFUNC:KBASE->pc
4303 | mov KBASE, [KBASE+PC2PROTO(k)]
4304 | ins_next
4305 |
4306 |6: // Fill up results with nil.
4307 if (op == BC_RET) {
4308 | mov aword [KBASE-16], LJ_TNIL // Note: relies on shifted base.
4309 | add KBASE, 8
4310 } else {
4311 | mov aword [BASE+RD*8-24], LJ_TNIL
4312 }
4313 | add RD, 1
4314 | jmp <5
4315 |
4316 |7: // Non-standard return case.
4317 | lea RB, [PC-FRAME_VARG]
4318 | test RBd, FRAME_TYPEP
4319 | jnz ->vm_return
4320 | // Return from vararg function: relocate BASE down and RA up.
4321 | sub BASE, RB
4322 if (op != BC_RET0) {
4323 | add RA, RB
4324 }
4325 | jmp <1
4326 break;
4327
4328 /* -- Loops and branches ------------------------------------------------ */
4329
4330 |.define FOR_IDX, [RA]
4331 |.define FOR_STOP, [RA+8]
4332 |.define FOR_STEP, [RA+16]
4333 |.define FOR_EXT, [RA+24]
4334
4335 case BC_FORL:
4336 |.if JIT
4337 | hotloop RBd
4338 |.endif
4339 | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
4340 break;
4341
4342 case BC_JFORI:
4343 case BC_JFORL:
4344 #if !LJ_HASJIT
4345 break;
4346 #endif
4347 case BC_FORI:
4348 case BC_IFORL:
4349 vk = (op == BC_IFORL || op == BC_JFORL);
4350 | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
4351 | lea RA, [BASE+RA*8]
4352 if (LJ_DUALNUM) {
4353 | mov RB, FOR_IDX
4354 | checkint RB, >9
4355 | mov TMPR, FOR_STOP
4356 if (!vk) {
4357 | checkint TMPR, ->vmeta_for
4358 | mov ITYPE, FOR_STEP
4359 | test ITYPEd, ITYPEd; js >5
4360 | sar ITYPE, 47;
4361 | cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for
4362 } else {
4363 #ifdef LUA_USE_ASSERT
4364 | checkinttp FOR_STOP, ->assert_bad_for_arg_type
4365 | checkinttp FOR_STEP, ->assert_bad_for_arg_type
4366 #endif
4367 | mov ITYPE, FOR_STEP
4368 | test ITYPEd, ITYPEd; js >5
4369 | add RBd, ITYPEd; jo >1
4370 | setint RB
4371 | mov FOR_IDX, RB
4372 }
4373 | cmp RBd, TMPRd
4374 | mov FOR_EXT, RB
4375 if (op == BC_FORI) {
4376 | jle >7
4377 |1:
4378 |6:
4379 | branchPC RD
4380 } else if (op == BC_JFORI) {
4381 | branchPC RD
4382 | movzx RDd, PC_RD
4383 | jle =>BC_JLOOP
4384 |1:
4385 |6:
4386 } else if (op == BC_IFORL) {
4387 | jg >7
4388 |6:
4389 | branchPC RD
4390 |1:
4391 } else {
4392 | jle =>BC_JLOOP
4393 |1:
4394 |6:
4395 }
4396 |7:
4397 | ins_next
4398 |
4399 |5: // Invert check for negative step.
4400 if (!vk) {
4401 | sar ITYPE, 47;
4402 | cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for
4403 } else {
4404 | add RBd, ITYPEd; jo <1
4405 | setint RB
4406 | mov FOR_IDX, RB
4407 }
4408 | cmp RBd, TMPRd
4409 | mov FOR_EXT, RB
4410 if (op == BC_FORI) {
4411 | jge <7
4412 } else if (op == BC_JFORI) {
4413 | branchPC RD
4414 | movzx RDd, PC_RD
4415 | jge =>BC_JLOOP
4416 } else if (op == BC_IFORL) {
4417 | jl <7
4418 } else {
4419 | jge =>BC_JLOOP
4420 }
4421 | jmp <6
4422 |9: // Fallback to FP variant.
4423 if (!vk) {
4424 | jae ->vmeta_for
4425 }
4426 } else if (!vk) {
4427 | checknumtp FOR_IDX, ->vmeta_for
4428 }
4429 if (!vk) {
4430 | checknumtp FOR_STOP, ->vmeta_for
4431 } else {
4432 #ifdef LUA_USE_ASSERT
4433 | checknumtp FOR_STOP, ->assert_bad_for_arg_type
4434 | checknumtp FOR_STEP, ->assert_bad_for_arg_type
4435 #endif
4436 }
4437 | mov RB, FOR_STEP
4438 if (!vk) {
4439 | checknum RB, ->vmeta_for
4440 }
4441 | movsd xmm0, qword FOR_IDX
4442 | movsd xmm1, qword FOR_STOP
4443 if (vk) {
4444 | addsd xmm0, qword FOR_STEP
4445 | movsd qword FOR_IDX, xmm0
4446 | test RB, RB; js >3
4447 } else {
4448 | jl >3
4449 }
4450 | ucomisd xmm1, xmm0
4451 |1:
4452 | movsd qword FOR_EXT, xmm0
4453 if (op == BC_FORI) {
4454 |.if DUALNUM
4455 | jnb <7
4456 |.else
4457 | jnb >2
4458 | branchPC RD
4459 |.endif
4460 } else if (op == BC_JFORI) {
4461 | branchPC RD
4462 | movzx RDd, PC_RD
4463 | jnb =>BC_JLOOP
4464 } else if (op == BC_IFORL) {
4465 |.if DUALNUM
4466 | jb <7
4467 |.else
4468 | jb >2
4469 | branchPC RD
4470 |.endif
4471 } else {
4472 | jnb =>BC_JLOOP
4473 }
4474 |.if DUALNUM
4475 | jmp <6
4476 |.else
4477 |2:
4478 | ins_next
4479 |.endif
4480 |
4481 |3: // Invert comparison if step is negative.
4482 | ucomisd xmm0, xmm1
4483 | jmp <1
4484 break;
4485
4486 case BC_ITERL:
4487 |.if JIT
4488 | hotloop RBd
4489 |.endif
4490 | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
4491 break;
4492
4493 case BC_JITERL:
4494 #if !LJ_HASJIT
4495 break;
4496 #endif
4497 case BC_IITERL:
4498 | ins_AJ // RA = base, RD = target
4499 | lea RA, [BASE+RA*8]
4500 | mov RB, [RA]
4501 | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
4502 if (op == BC_JITERL) {
4503 | mov [RA-8], RB
4504 | jmp =>BC_JLOOP
4505 } else {
4506 | branchPC RD // Otherwise save control var + branch.
4507 | mov [RA-8], RB
4508 }
4509 |1:
4510 | ins_next
4511 break;
4512
4513 case BC_LOOP:
4514 | ins_A // RA = base, RD = target (loop extent)
4515 | // Note: RA/RD is only used by trace recorder to determine scope/extent
4516 | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
4517 |.if JIT
4518 | hotloop RBd
4519 |.endif
4520 | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
4521 break;
4522
4523 case BC_ILOOP:
4524 | ins_A // RA = base, RD = target (loop extent)
4525 | ins_next
4526 break;
4527
4528 case BC_JLOOP:
4529 |.if JIT
4530 | ins_AD // RA = base (ignored), RD = traceno
4531 | mov RA, [DISPATCH+DISPATCH_J(trace)]
4532 | mov TRACE:RD, [RA+RD*8]
4533 | mov RD, TRACE:RD->mcode
4534 | mov L:RB, SAVE_L
4535 | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
4536 | mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB
4537 | // Save additional callee-save registers only used in compiled code.
4538 |.if X64WIN
4539 | mov CSAVE_4, r12
4540 | mov CSAVE_3, r13
4541 | mov CSAVE_2, r14
4542 | mov CSAVE_1, r15
4543 | mov RA, rsp
4544 | sub rsp, 10*16+4*8
4545 | movdqa [RA-1*16], xmm6
4546 | movdqa [RA-2*16], xmm7
4547 | movdqa [RA-3*16], xmm8
4548 | movdqa [RA-4*16], xmm9
4549 | movdqa [RA-5*16], xmm10
4550 | movdqa [RA-6*16], xmm11
4551 | movdqa [RA-7*16], xmm12
4552 | movdqa [RA-8*16], xmm13
4553 | movdqa [RA-9*16], xmm14
4554 | movdqa [RA-10*16], xmm15
4555 |.else
4556 | sub rsp, 16
4557 | mov [rsp+16], r12
4558 | mov [rsp+8], r13
4559 |.endif
4560 | jmp RD
4561 |.endif
4562 break;
4563
4564 case BC_JMP:
4565 | ins_AJ // RA = unused, RD = target
4566 | branchPC RD
4567 | ins_next
4568 break;
4569
4570 /* -- Function headers -------------------------------------------------- */
4571
4572 /*
4573 ** Reminder: A function may be called with func/args above L->maxstack,
4574 ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
4575 ** too. This means all FUNC* ops (including fast functions) must check
4576 ** for stack overflow _before_ adding more slots!
4577 */
4578
4579 case BC_FUNCF:
4580 |.if JIT
4581 | hotcall RBd
4582 |.endif
4583 case BC_FUNCV: /* NYI: compiled vararg functions. */
4584 | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
4585 break;
4586
4587 case BC_JFUNCF:
4588 #if !LJ_HASJIT
4589 break;
4590 #endif
4591 case BC_IFUNCF:
4592 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
4593 | mov KBASE, [PC-4+PC2PROTO(k)]
4594 | mov L:RB, SAVE_L
4595 | lea RA, [BASE+RA*8] // Top of frame.
4596 | cmp RA, L:RB->maxstack
4597 | ja ->vm_growstack_f
4598 | movzx RAd, byte [PC-4+PC2PROTO(numparams)]
4599 | cmp NARGS:RDd, RAd // Check for missing parameters.
4600 | jbe >3
4601 |2:
4602 if (op == BC_JFUNCF) {
4603 | movzx RDd, PC_RD
4604 | jmp =>BC_JLOOP
4605 } else {
4606 | ins_next
4607 }
4608 |
4609 |3: // Clear missing parameters.
4610 | mov aword [BASE+NARGS:RD*8-8], LJ_TNIL
4611 | add NARGS:RDd, 1
4612 | cmp NARGS:RDd, RAd
4613 | jbe <3
4614 | jmp <2
4615 break;
4616
4617 case BC_JFUNCV:
4618 #if !LJ_HASJIT
4619 break;
4620 #endif
4621 | int3 // NYI: compiled vararg functions
4622 break; /* NYI: compiled vararg functions. */
4623
4624 case BC_IFUNCV:
4625 | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
4626 | lea RBd, [NARGS:RD*8+FRAME_VARG+8]
4627 | lea RD, [BASE+NARGS:RD*8+8]
4628 | mov LFUNC:KBASE, [BASE-16]
4629 | mov [RD-8], RB // Store delta + FRAME_VARG.
4630 | mov [RD-16], LFUNC:KBASE // Store copy of LFUNC.
4631 | mov L:RB, SAVE_L
4632 | lea RA, [RD+RA*8]
4633 | cmp RA, L:RB->maxstack
4634 | ja ->vm_growstack_v // Need to grow stack.
4635 | mov RA, BASE
4636 | mov BASE, RD
4637 | movzx RBd, byte [PC-4+PC2PROTO(numparams)]
4638 | test RBd, RBd
4639 | jz >2
4640 | add RA, 8
4641 |1: // Copy fixarg slots up to new frame.
4642 | add RA, 8
4643 | cmp RA, BASE
4644 | jnb >3 // Less args than parameters?
4645 | mov KBASE, [RA-16]
4646 | mov [RD], KBASE
4647 | add RD, 8
4648 | mov aword [RA-16], LJ_TNIL // Clear old fixarg slot (help the GC).
4649 | sub RBd, 1
4650 | jnz <1
4651 |2:
4652 if (op == BC_JFUNCV) {
4653 | movzx RDd, PC_RD
4654 | jmp =>BC_JLOOP
4655 } else {
4656 | mov KBASE, [PC-4+PC2PROTO(k)]
4657 | ins_next
4658 }
4659 |
4660 |3: // Clear missing parameters.
4661 | mov aword [RD], LJ_TNIL
4662 | add RD, 8
4663 | sub RBd, 1
4664 | jnz <3
4665 | jmp <2
4666 break;
4667
4668 case BC_FUNCC:
4669 case BC_FUNCCW:
4670 | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
4671 | mov CFUNC:RB, [BASE-16]
4672 | cleartp CFUNC:RB
4673 | mov KBASE, CFUNC:RB->f
4674 | mov L:RB, SAVE_L
4675 | lea RD, [BASE+NARGS:RD*8-8]
4676 | mov L:RB->base, BASE
4677 | lea RA, [RD+8*LUA_MINSTACK]
4678 | cmp RA, L:RB->maxstack
4679 | mov L:RB->top, RD
4680 if (op == BC_FUNCC) {
4681 | mov CARG1, L:RB // Caveat: CARG1 may be RA.
4682 } else {
4683 | mov CARG2, KBASE
4684 | mov CARG1, L:RB // Caveat: CARG1 may be RA.
4685 }
4686 | ja ->vm_growstack_c // Need to grow stack.
4687 | set_vmstate C
4688 if (op == BC_FUNCC) {
4689 | call KBASE // (lua_State *L)
4690 } else {
4691 | // (lua_State *L, lua_CFunction f)
4692 | call aword [DISPATCH+DISPATCH_GL(wrapf)]
4693 }
4694 | // nresults returned in eax (RD).
4695 | mov BASE, L:RB->base
4696 | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB
4697 | set_vmstate INTERP
4698 | lea RA, [BASE+RD*8]
4699 | neg RA
4700 | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
4701 | mov PC, [BASE-8] // Fetch PC of caller.
4702 | jmp ->vm_returnc
4703 break;
4704
4705 /* ---------------------------------------------------------------------- */
4706
4707 default:
4708 fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
4709 exit(2);
4710 break;
4711 }
4712 }
4713
4714 static int build_backend(BuildCtx *ctx)
4715 {
4716 int op;
4717 dasm_growpc(Dst, BC__MAX);
4718 build_subroutines(ctx);
4719 |.code_op
4720 for (op = 0; op < BC__MAX; op++)
4721 build_ins(ctx, (BCOp)op, op);
4722 return BC__MAX;
4723 }
4724
4725 /* Emit pseudo frame-info for all assembler functions. */
4726 static void emit_asm_debug(BuildCtx *ctx)
4727 {
4728 int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
4729 switch (ctx->mode) {
4730 case BUILD_elfasm:
4731 fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
4732 fprintf(ctx->fp,
4733 ".Lframe0:\n"
4734 "\t.long .LECIE0-.LSCIE0\n"
4735 ".LSCIE0:\n"
4736 "\t.long 0xffffffff\n"
4737 "\t.byte 0x1\n"
4738 "\t.string \"\"\n"
4739 "\t.uleb128 0x1\n"
4740 "\t.sleb128 -8\n"
4741 "\t.byte 0x10\n"
4742 "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4743 "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4744 "\t.align 8\n"
4745 ".LECIE0:\n\n");
4746 fprintf(ctx->fp,
4747 ".LSFDE0:\n"
4748 "\t.long .LEFDE0-.LASFDE0\n"
4749 ".LASFDE0:\n"
4750 "\t.long .Lframe0\n"
4751 "\t.quad .Lbegin\n"
4752 "\t.quad %d\n"
4753 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4754 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
4755 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
4756 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
4757 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
4758 #if LJ_NO_UNWIND
4759 "\t.byte 0x8d\n\t.uleb128 0x6\n" /* offset r13 */
4760 "\t.byte 0x8c\n\t.uleb128 0x7\n" /* offset r12 */
4761 #endif
4762 "\t.align 8\n"
4763 ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
4764 #if LJ_HASFFI
4765 fprintf(ctx->fp,
4766 ".LSFDE1:\n"
4767 "\t.long .LEFDE1-.LASFDE1\n"
4768 ".LASFDE1:\n"
4769 "\t.long .Lframe0\n"
4770 "\t.quad lj_vm_ffi_call\n"
4771 "\t.quad %d\n"
4772 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4773 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
4774 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
4775 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
4776 "\t.align 8\n"
4777 ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
4778 #endif
4779 #if !LJ_NO_UNWIND
4780 #if LJ_TARGET_SOLARIS
4781 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");
4782 #else
4783 fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
4784 #endif
4785 fprintf(ctx->fp,
4786 ".Lframe1:\n"
4787 "\t.long .LECIE1-.LSCIE1\n"
4788 ".LSCIE1:\n"
4789 "\t.long 0\n"
4790 "\t.byte 0x1\n"
4791 "\t.string \"zPR\"\n"
4792 "\t.uleb128 0x1\n"
4793 "\t.sleb128 -8\n"
4794 "\t.byte 0x10\n"
4795 "\t.uleb128 6\n" /* augmentation length */
4796 "\t.byte 0x1b\n" /* pcrel|sdata4 */
4797 "\t.long lj_err_unwind_dwarf-.\n"
4798 "\t.byte 0x1b\n" /* pcrel|sdata4 */
4799 "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4800 "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4801 "\t.align 8\n"
4802 ".LECIE1:\n\n");
4803 fprintf(ctx->fp,
4804 ".LSFDE2:\n"
4805 "\t.long .LEFDE2-.LASFDE2\n"
4806 ".LASFDE2:\n"
4807 "\t.long .LASFDE2-.Lframe1\n"
4808 "\t.long .Lbegin-.\n"
4809 "\t.long %d\n"
4810 "\t.uleb128 0\n" /* augmentation length */
4811 "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
4812 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
4813 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
4814 "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
4815 "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
4816 "\t.align 8\n"
4817 ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
4818 #if LJ_HASFFI
4819 fprintf(ctx->fp,
4820 ".Lframe2:\n"
4821 "\t.long .LECIE2-.LSCIE2\n"
4822 ".LSCIE2:\n"
4823 "\t.long 0\n"
4824 "\t.byte 0x1\n"
4825 "\t.string \"zR\"\n"
4826 "\t.uleb128 0x1\n"
4827 "\t.sleb128 -8\n"
4828 "\t.byte 0x10\n"
4829 "\t.uleb128 1\n" /* augmentation length */
4830 "\t.byte 0x1b\n" /* pcrel|sdata4 */
4831 "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n"
4832 "\t.byte 0x80+0x10\n\t.uleb128 0x1\n"
4833 "\t.align 8\n"
4834 ".LECIE2:\n\n");
4835 fprintf(ctx->fp,
4836 ".LSFDE3:\n"
4837 "\t.long .LEFDE3-.LASFDE3\n"
4838 ".LASFDE3:\n"
4839 "\t.long .LASFDE3-.Lframe2\n"
4840 "\t.long lj_vm_ffi_call-.\n"
4841 "\t.long %d\n"
4842 "\t.uleb128 0\n" /* augmentation length */
4843 "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
4844 "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
4845 "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
4846 "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
4847 "\t.align 8\n"
4848 ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
4849 #endif
4850 #endif
4851 break;
4852 #if !LJ_NO_UNWIND
4853 /* Mental note: never let Apple design an assembler.
4854 ** Or a linker. Or a plastic case. But I digress.
4855 */
4856 case BUILD_machasm: {
4857 #if LJ_HASFFI
4858 int fcsize = 0;
4859 #endif
4860 int i;
4861 fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
4862 fprintf(ctx->fp,
4863 "EH_frame1:\n"
4864 "\t.set L$set$x,LECIEX-LSCIEX\n"
4865 "\t.long L$set$x\n"
4866 "LSCIEX:\n"
4867 "\t.long 0\n"
4868 "\t.byte 0x1\n"
4869 "\t.ascii \"zPR\\0\"\n"
4870 "\t.byte 0x1\n"
4871 "\t.byte 128-8\n"
4872 "\t.byte 0x10\n"
4873 "\t.byte 6\n" /* augmentation length */
4874 "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
4875 "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
4876 "\t.byte 0x1b\n" /* pcrel|sdata4 */
4877 "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n"
4878 "\t.byte 0x80+0x10\n\t.byte 0x1\n"
4879 "\t.align 3\n"
4880 "LECIEX:\n\n");
4881 for (i = 0; i < ctx->nsym; i++) {
4882 const char *name = ctx->sym[i].name;
4883 int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
4884 if (size == 0) continue;
4885 #if LJ_HASFFI
4886 if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
4887 #endif
4888 fprintf(ctx->fp,
4889 "%s.eh:\n"
4890 "LSFDE%d:\n"
4891 "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
4892 "\t.long L$set$%d\n"
4893 "LASFDE%d:\n"
4894 "\t.long LASFDE%d-EH_frame1\n"
4895 "\t.long %s-.\n"
4896 "\t.long %d\n"
4897 "\t.byte 0\n" /* augmentation length */
4898 "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
4899 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
4900 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
4901 "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
4902 "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
4903 "\t.align 3\n"
4904 "LEFDE%d:\n\n",
4905 name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
4906 }
4907 #if LJ_HASFFI
4908 if (fcsize) {
4909 fprintf(ctx->fp,
4910 "EH_frame2:\n"
4911 "\t.set L$set$y,LECIEY-LSCIEY\n"
4912 "\t.long L$set$y\n"
4913 "LSCIEY:\n"
4914 "\t.long 0\n"
4915 "\t.byte 0x1\n"
4916 "\t.ascii \"zR\\0\"\n"
4917 "\t.byte 0x1\n"
4918 "\t.byte 128-8\n"
4919 "\t.byte 0x10\n"
4920 "\t.byte 1\n" /* augmentation length */
4921 "\t.byte 0x1b\n" /* pcrel|sdata4 */
4922 "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n"
4923 "\t.byte 0x80+0x10\n\t.byte 0x1\n"
4924 "\t.align 3\n"
4925 "LECIEY:\n\n");
4926 fprintf(ctx->fp,
4927 "_lj_vm_ffi_call.eh:\n"
4928 "LSFDEY:\n"
4929 "\t.set L$set$yy,LEFDEY-LASFDEY\n"
4930 "\t.long L$set$yy\n"
4931 "LASFDEY:\n"
4932 "\t.long LASFDEY-EH_frame2\n"
4933 "\t.long _lj_vm_ffi_call-.\n"
4934 "\t.long %d\n"
4935 "\t.byte 0\n" /* augmentation length */
4936 "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
4937 "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
4938 "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */
4939 "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
4940 "\t.align 3\n"
4941 "LEFDEY:\n\n", fcsize);
4942 }
4943 #endif
4944 }
4945 break;
4946 #endif
4947 default: /* Difficult for other modes. */
4948 break;
4949 }
4950 }
4951