Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_x64.dasc

raw
1 /* vim: ts=4 sw=4 sts=4 et tw=78
2 * Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
3 * Portions copyright (c) 2011 James R. McKaskill.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9 |.arch x64
10
11 |.actionlist build_actionlist
12 |.globalnames globnames
13 |.externnames extnames
14
15 |.define L_ARG, r12
16 |.define TOP, r13
17
18 | // the 5 and 6 arg forms are only used on posix x64
19 |.macro call_rrrrrr, func, arg0, arg1, arg2, arg3, arg4, arg5
20 | mov r9, arg5
21 | mov r8, arg4
22 | mov rcx, arg3
23 | mov rdx, arg2
24 | mov rsi, arg1
25 | mov rdi, arg0
26 | call func
27 |.endmacro
28 |.macro call_rrrrr, func, arg0, arg1, arg2, arg3, arg4
29 | mov r8, arg4
30 | mov rcx, arg3
31 | mov rdx, arg2
32 | mov rsi, arg1
33 | mov rdi, arg0
34 | call func
35 |.endmacro
36 |
37 |.macro call_rrrp, func, arg0, arg1, arg2, arg3
38 | mov64 rcx, arg3
39 | mov rdx, arg2
40 | mov rsi, arg1
41 | mov rdi, arg0
42 | call func
43 |.endmacro
44 |.macro call_rrrr, func, arg0, arg1, arg2, arg3
45 | mov rcx, arg3
46 | mov rdx, arg2
47 | mov rsi, arg1
48 | mov rdi, arg0
49 | call func
50 |.endmacro
51 |
52 |.macro call_rrp, func, arg0, arg1, arg2
53 | mov64 rdx, arg2
54 | mov rsi, arg1
55 | mov rdi, arg0
56 | call func
57 |.endmacro
58 |.macro call_rrr, func, arg0, arg1, arg2
59 | mov rdx, arg2
60 | mov rsi, arg1
61 | mov rdi, arg0
62 | call func
63 |.endmacro
64 |
65 |.macro call_rp, func, arg0, arg1
66 | mov64 rsi, arg1
67 | mov rdi, arg0
68 | call func
69 |.endmacro
70 |.macro call_rr, func, arg0, arg1
71 | mov rsi, arg1
72 | mov rdi, arg0
73 | call func
74 |.endmacro
75 |
76 |.macro call_r, func, arg0
77 | mov rdi, arg0
78 | call func
79 |.endmacro
80
81
82 |.macro epilog
83 | mov TOP, [rbp-16]
84 | mov L_ARG, [rbp-8]
85 | mov rsp, rbp
86 | pop rbp
87 | ret
88 |.endmacro
89
90 |.macro get_errno // note trashes registers
91 | call extern GetLastError
92 | mov64 rcx, perr
93 | mov dword [rcx], eax
94 |.endmacro
95
96 |.macro too_few_arguments
97 | mov ax, 0
98 | call_rp extern luaL_error, L_ARG, &"too few arguments"
99 |.endmacro
100
101 |.macro too_many_arguments
102 | mov ax, 0
103 | call_rp extern luaL_error, L_ARG, &"too many arguments"
104 |.endmacro
105
106 |.macro lua_return_arg
107 | mov eax, 1
108 | epilog
109 |.endmacro
110
111 |.macro lua_return_void
112 | get_errno
113 | mov eax, 0
114 | epilog
115 |.endmacro
116
117 |.macro lua_return_double
118 | movq qword [rsp+32], xmm0
119 | get_errno
120 | movq xmm0, qword [rsp+32]
121 | mov rdi, L_ARG
122 | call extern lua_pushnumber
123 | lua_return_arg
124 |.endmacro
125
126 |.macro lua_return_bool
127 | movzx eax, al
128 | mov [rsp+32], eax
129 | get_errno
130 | mov eax, [rsp+32]
131 | call_rr extern lua_pushboolean, L_ARG, rax
132 | lua_return_arg
133 |.endmacro
134
135 |.macro lua_return_int
136 | mov [rsp+32], eax
137 | get_errno
138 | mov eax, [rsp+32]
139 | call_rr extern push_int, L_ARG, rax
140 | lua_return_arg
141 |.endmacro
142
143 |.macro lua_return_uint
144 | mov [rsp+32], eax
145 | get_errno
146 | mov eax, [rsp+32]
147 | call_rr extern push_uint, L_ARG, rax
148 | lua_return_arg
149 |.endmacro
150
151 |.macro lua_return_long
152 | mov [rsp+32], rax
153 | get_errno
154 | mov rax, [rsp+32]
155 | call_rr extern lua_pushinteger, L_ARG, rax
156 | lua_return_arg
157 |.endmacro
158
159 |.macro lua_return_ulong
160 | mov [rsp+32], rax
161 | get_errno
162 | mov rax, [rsp+32]
163 | call_rr extern lua_pushinteger, L_ARG, rax
164 | lua_return_arg
165 |.endmacro
166
167 |.macro save_registers
168 | // use rbp relative so we store values in the outer stack frame
169 | movq qword [rbp-16], xmm0
170 | movq qword [rbp-24], xmm1
171 | movq qword [rbp-32], xmm2
172 | movq qword [rbp-40], xmm3
173 | movq qword [rbp-48], xmm4
174 | movq qword [rbp-56], xmm5
175 | movq qword [rbp-64], xmm6
176 | movq qword [rbp-72], xmm7
177 | mov [rbp-80], rdi
178 | mov [rbp-88], rsi
179 | mov [rbp-96], rdx
180 | mov [rbp-104], rcx
181 | mov [rbp-112], r8
182 | mov [rbp-120], r9
183 |.endmacro
184
185 #define JUMP_SIZE 14
186 #define MIN_BRANCH INT32_MIN
187 #define MAX_BRANCH INT32_MAX
188 #define BRANCH_OFF 4
189
190 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
191 {
192 /* The jump code is the function pointer followed by a stub to call the
193 * function pointer. The stub exists in 64 bit so we can jump to functions
194 * with an offset greater than 2 GB.
195 *
196 * Note we have to manually set this up since there are commands buffered
197 * in the jit state and dynasm doesn't support rip relative addressing.
198 *
199 * eg on 64 bit:
200 * 0-8: function ptr
201 * 8-14: jmp aword [rip-14]
202 *
203 */
204 *(cfunction*) code = func;
205 code[8] = 0xFF; /* FF /4 operand for jmp */
206 code[9] = 0x25; /* RIP displacement */
207 *(int32_t*) &code[10] = -14;
208 }
209
210 void compile_globals(struct jit* jit, lua_State* L)
211 {
212 struct jit* Dst = jit;
213 int* perr = &jit->last_errno;
214 dasm_setup(Dst, build_actionlist);
215
216 /* Note: since the return code uses EBP to reset the stack pointer, we
217 * don't have to track the amount of stack space used. It also means we
218 * can handle stdcall and cdecl with the same code.
219 */
220
221 /* Note the various call_* functions want 32 bytes of 16 byte aligned
222 * stack
223 */
224
225 compile(Dst, L, NULL, LUA_NOREF);
226 }
227
228 int x86_return_size(lua_State* L, int usr, const struct ctype* ct)
229 {
230 int ret = 0;
231 const struct ctype* mt;
232
233 if (ct->calling_convention != C_CALL) {
234 size_t i;
235 size_t argn = lua_rawlen(L, usr);
236 for (i = 1; i <= argn; i++) {
237 lua_rawgeti(L, usr, (int) i);
238 mt = (const struct ctype*) lua_touserdata(L, -1);
239
240 if (mt->pointers || mt->is_reference) {
241 ret += sizeof(void*);
242 } else {
243 switch (mt->type) {
244 case DOUBLE_TYPE:
245 case COMPLEX_FLOAT_TYPE:
246 case INT64_TYPE:
247 ret += 8;
248 break;
249 case COMPLEX_DOUBLE_TYPE:
250 ret += 16;
251 break;
252 case INTPTR_TYPE:
253 ret += sizeof(intptr_t);
254 break;
255 case FUNCTION_PTR_TYPE:
256 ret += sizeof(cfunction);
257 break;
258 case BOOL_TYPE:
259 case FLOAT_TYPE:
260 case INT8_TYPE:
261 case INT16_TYPE:
262 case INT32_TYPE:
263 case ENUM_TYPE:
264 ret += 4;
265 break;
266 default:
267 return luaL_error(L, "NYI - argument type");
268 }
269 }
270
271 lua_pop(L, 1);
272 }
273 }
274
275 return ret;
276 }
277
278 #define MAX_INT_REGISTERS(ct) 6 /* rdi, rsi, rdx, rcx, r8, r9 */
279 #define MAX_FLOAT_REGISTERS(ct) 8 /* xmm0-7 */
280
281 struct reg_alloc {
282 int floats;
283 int ints;
284 int off;
285 };
286
287 #define REGISTER_STACK_SPACE(ct) (14*8)
288
289 /* Fastcall:
290 * Uses ecx, edx as first two int registers
291 * Everything else on stack (include 64bit ints)
292 * No overflow stack space
293 * Pops the stack before returning
294 * Returns int in eax, float in ST0
295 * We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs
296 */
297
298 static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
299 {
300 /* grab the register from the shadow space */
301 if (reg->ints < MAX_INT_REGISTERS(ct)) {
302 | mov rcx, [rbp - 80 - 8*reg->ints]
303 reg->ints++;
304 }
305 else if (is_int64) {
306 | mov rcx, [rbp + reg->off]
307 reg->off += 8;
308 } else {
309 | mov ecx, [rbp + reg->off]
310 /* The parameters to a function on stack are always 8 byte aligned. */
311 reg->off += 8;
312 }
313 }
314
315 static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
316 {
317 if (reg->ints < MAX_INT_REGISTERS(ct)) {
318 | mov [rsp + 32 + 8*reg->ints], rax
319 reg->ints++;
320 }
321 else {
322 if (reg->off % 8 != 0) {
323 reg->off += 8 - (reg->off % 8);
324 }
325 if (is_int64) {
326 | mov [rsp + reg->off], rax
327 reg->off += 8;
328 } else {
329 | mov [rsp+reg->off], eax
330 reg->off += 4;
331 }
332 }
333 }
334
335 static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
336 {
337 int off;
338
339 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
340 off = -16 - 8*reg->floats;
341 reg->floats++;
342 }
343 else {
344 off = reg->off;
345 reg->off += is_double ? 8 : 4;
346 }
347
348 if (is_double) {
349 | movq xmm0, qword [rbp + off]
350 } else {
351 | cvtss2sd xmm0, dword [rbp + off]
352 }
353 }
354
355 static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
356 {
357 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
358 if (is_double) {
359 | movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
360 } else {
361 | cvtsd2ss xmm0, xmm0
362 | movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
363 }
364 reg->floats++;
365 }
366 else {
367 if (reg->off % 8 != 0) {
368 reg->off += 8 - (reg->off % 8);
369 }
370 if (is_double) {
371 | movq qword [rsp + reg->off], xmm0
372 reg->off += 8;
373 } else {
374 | cvtsd2ss xmm0, xmm0
375 | movd dword [rsp + reg->off], xmm0
376 reg->off += 8;
377 }
378 }
379 }
380
381 #define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 1)
382 #define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 1)
383
384 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
385 {
386 int i, nargs;
387 cfunction* pf;
388 struct ctype ct2 = *ct;
389 const struct ctype* mt;
390 struct reg_alloc reg;
391 int num_upvals = 0;
392 int top = lua_gettop(L);
393 struct jit* Dst = get_jit(L);
394 int ref;
395 int hidden_arg_off = 0;
396
397 ct_usr = lua_absindex(L, ct_usr);
398 fidx = lua_absindex(L, fidx);
399
400 assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx));
401
402 memset(&reg, 0, sizeof(reg));
403 reg.off = 16;
404
405 dasm_setup(Dst, build_actionlist);
406
407 // add a table to store ctype and function upvalues
408 // callback_set assumes the first value is the lua function
409 nargs = (int) lua_rawlen(L, ct_usr);
410 lua_newtable(L);
411 lua_pushvalue(L, -1);
412 ref = luaL_ref(L, LUA_REGISTRYINDEX);
413
414 if (ct->has_var_arg) {
415 luaL_error(L, "can't create callbacks with varargs");
416 }
417
418 // setup a stack frame to hold args for the call into lua_call
419
420 | push rbp
421 | mov rbp, rsp
422 | push L_ARG
423 | // stack is 4 or 8 (mod 16) (L_ARG, rbp, rip)
424 | // 8 to realign, 16 for return vars, 32 for local calls, rest to save registers
425 | sub rsp, 8 + 16 + 32 + REGISTER_STACK_SPACE(ct)
426 | save_registers
427 if (ct->calling_convention == FAST_CALL) {
428 }
429
430 // hardcode the lua_State* value into the assembly
431 | mov64 L_ARG, L
432
433 /* get the upval table */
434 | call_rrr extern lua_rawgeti, L_ARG, LUA_REGISTRYINDEX, ref
435
436 /* get the lua function */
437 lua_pushvalue(L, fidx);
438 lua_rawseti(L, -2, ++num_upvals);
439 assert(num_upvals == CALLBACK_FUNC_USR_IDX);
440 | call_rrr extern lua_rawgeti, L_ARG, -1, num_upvals
441
442 (void) hidden_arg_off;
443
444 for (i = 1; i <= nargs; i++) {
445 lua_rawgeti(L, ct_usr, i);
446 mt = (const struct ctype*) lua_touserdata(L, -1);
447
448 if (mt->pointers || mt->is_reference) {
449 lua_getuservalue(L, -1);
450 lua_rawseti(L, -3, ++num_upvals); /* usr value */
451 lua_rawseti(L, -2, ++num_upvals); /* mt */
452 /* on the lua stack in the callback:
453 * upval tbl, lua func, i-1 args
454 */
455 | call_rrr extern lua_rawgeti, L_ARG, -i-1, num_upvals-1
456 | call_rrp extern push_cdata, L_ARG, -1, mt
457 get_pointer(Dst, ct, &reg);
458 | mov [rax], rcx
459 | call_rr, extern lua_remove, L_ARG, -2
460 } else {
461 switch (mt->type) {
462 case INT64_TYPE:
463 lua_getuservalue(L, -1);
464 lua_rawseti(L, -3, ++num_upvals); /* mt */
465 lua_pop(L, 1);
466 | call_rrp extern push_cdata, L_ARG, 0, mt
467 get_int(Dst, ct, &reg, 1);
468 | mov [rax], rcx
469 break;
470
471 case INTPTR_TYPE:
472 lua_getuservalue(L, -1);
473 lua_rawseti(L, -3, ++num_upvals); /* mt */
474 lua_pop(L, 1);
475 | call_rrp extern push_cdata, L_ARG, 0, mt
476 get_pointer(Dst, ct, &reg);
477 | mov [rax], rcx
478 break;
479
480 case COMPLEX_FLOAT_TYPE:
481 lua_pop(L, 1);
482 /* complex floats are two floats packed into a double */
483 | call_rrp extern push_cdata, L_ARG, 0, mt
484 get_float(Dst, ct, &reg, 1);
485 | movq qword [rax], xmm0
486 break;
487
488 case COMPLEX_DOUBLE_TYPE:
489 lua_pop(L, 1);
490 | call_rrp extern push_cdata, L_ARG, 0, mt
491 /* real */
492 get_float(Dst, ct, &reg, 1);
493 | movq qword [rax], xmm0
494 /* imag */
495 get_float(Dst, ct, &reg, 1);
496 | movq qword [rax+8], xmm0
497 break;
498
499 case FLOAT_TYPE:
500 case DOUBLE_TYPE:
501 lua_pop(L, 1);
502 get_float(Dst, ct, &reg, mt->type == DOUBLE_TYPE);
503 | // for 64bit xmm0 is already set
504 | mov rdi, L_ARG
505 | call extern lua_pushnumber
506 break;
507
508 case BOOL_TYPE:
509 lua_pop(L, 1);
510 get_int(Dst, ct, &reg, 0);
511 | movzx ecx, cl
512 | call_rr extern lua_pushboolean, L_ARG, rcx
513 break;
514
515 case INT8_TYPE:
516 lua_pop(L, 1);
517 get_int(Dst, ct, &reg, 0);
518 if (mt->is_unsigned) {
519 | movzx ecx, cl
520 } else {
521 | movsx ecx, cl
522 }
523 | call_rr extern push_int, L_ARG, rcx
524 break;
525
526 case INT16_TYPE:
527 lua_pop(L, 1);
528 get_int(Dst, ct, &reg, 0);
529 if (mt->is_unsigned) {
530 | movzx ecx, cx
531 } else {
532 | movsx ecx, cx
533 }
534 | call_rr extern push_int, L_ARG, rcx
535 break;
536
537 case ENUM_TYPE:
538 case INT32_TYPE:
539 lua_pop(L, 1);
540 get_int(Dst, ct, &reg, 0);
541 if (mt->is_unsigned) {
542 | call_rr extern push_uint, L_ARG, rcx
543 } else {
544 | call_rr extern push_int, L_ARG, rcx
545 }
546 break;
547
548 default:
549 luaL_error(L, "NYI: callback arg type");
550 }
551 }
552 }
553
554 lua_rawgeti(L, ct_usr, 0);
555 mt = (const struct ctype*) lua_touserdata(L, -1);
556
557 | call_rrrp extern lua_callk, L_ARG, nargs, (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, 0
558
559 // Unpack the return argument if not "void", also clean-up the lua stack
560 // to remove the return argument and bind table. Use lua_settop rather
561 // than lua_pop as lua_pop is implemented as a macro.
562 if (mt->pointers || mt->is_reference) {
563 lua_getuservalue(L, -1);
564 lua_rawseti(L, -3, ++num_upvals); /* usr value */
565 lua_rawseti(L, -2, ++num_upvals); /* mt */
566 | call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
567 | call_rrrp extern check_typed_pointer, L_ARG, -2, -1, mt
568 | mov [rsp+32], rax
569 | call_rr extern lua_settop, L_ARG, -4
570 | mov rax, [rsp+32]
571
572 } else {
573 switch (mt->type) {
574 case ENUM_TYPE:
575 lua_getuservalue(L, -1);
576 lua_rawseti(L, -3, ++num_upvals); /* usr value */
577 lua_rawseti(L, -2, ++num_upvals); /* mt */
578 | call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
579 | call_rrrp, extern check_enum, L_ARG, -2, -1, mt
580 | mov [rsp+32], eax
581 | call_rr extern lua_settop, L_ARG, -4
582 | mov eax, [rsp+32]
583 break;
584
585 case VOID_TYPE:
586 lua_pop(L, 1);
587 | call_rr extern lua_settop, L_ARG, -2
588 break;
589
590 case BOOL_TYPE:
591 case INT8_TYPE:
592 case INT16_TYPE:
593 case INT32_TYPE:
594 lua_pop(L, 1);
595 if (mt->is_unsigned) {
596 | call_rr extern check_uint32, L_ARG, -1
597 } else {
598 | call_rr extern check_int32, L_ARG, -1
599 }
600 | mov [rsp+32], eax
601 | call_rr extern lua_settop, L_ARG, -3
602 | mov eax, [rsp+32]
603 break;
604
605 case INT64_TYPE:
606 lua_pop(L, 1);
607
608 if (mt->is_unsigned) {
609 | call_rr extern check_uint64, L_ARG, -1
610 } else {
611 | call_rr extern check_int64, L_ARG, -1
612 }
613
614 | mov [rsp+32], rax
615 | call_rr extern lua_settop, L_ARG, -3
616 | mov rax, [rsp+32]
617 break;
618
619 case INTPTR_TYPE:
620 lua_pop(L, 1);
621 | call_rr extern check_uintptr, L_ARG, -1
622 | mov [rsp+32], rax
623 | call_rr extern lua_settop, L_ARG, -3
624 | mov rax, [rsp+32]
625 break;
626
627 case FLOAT_TYPE:
628 case DOUBLE_TYPE:
629 lua_pop(L, 1);
630 | call_rr extern check_double, L_ARG, -1
631 | movq qword [rsp+32], xmm0
632 | call_rr extern lua_settop, L_ARG, -3
633 if (mt->type == FLOAT_TYPE) {
634 | cvtsd2ss xmm0, qword [rsp+32]
635 } else {
636 | movq xmm0, qword [rsp+32]
637 }
638 break;
639
640 case COMPLEX_FLOAT_TYPE:
641 lua_pop(L, 1);
642 #if !defined HAVE_COMPLEX
643 luaL_error(L, "ffi lib compiled without complex number support");
644 #endif
645 /* on 64 bit complex floats are two floats packed into a double,
646 * on 32 bit returned complex floats use eax and edx */
647 | call_rr extern check_complex_float, L_ARG, -1
648 |
649 | movq qword [rsp+32], xmm0
650 |
651 | call_rr extern lua_settop, L_ARG, -3
652 |
653 | movq xmm0, qword [rsp+32]
654 break;
655
656 case COMPLEX_DOUBLE_TYPE:
657 lua_pop(L, 1);
658 #if !defined HAVE_COMPLEX
659 luaL_error(L, "ffi lib compiled without complex number support");
660 #endif
661 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
662 * there is a hidden first parameter that points to 16 bytes where
663 * the returned arg is stored which is popped by the called
664 * function */
665 | call_rr extern check_complex_double, L_ARG, -1
666 | movq qword [rsp+32], xmm0
667 | movq qword [rsp+40], xmm1
668 | call_rr extern lua_settop, L_ARG, -3
669 | movq xmm0, qword [rsp+32]
670 | movq xmm1, qword [rsp+40]
671 break;
672
673 default:
674 luaL_error(L, "NYI: callback return type");
675 }
676 }
677
678 | mov L_ARG, [rbp-8]
679 | mov rsp, rbp
680 | pop rbp
681 | ret x86_return_size(L, ct_usr, ct)
682
683 lua_pop(L, 1); /* upval table - already in registry */
684 assert(lua_gettop(L) == top);
685
686 ct2.is_jitted = 1;
687 pf = (cfunction*) push_cdata(L, ct_usr, &ct2);
688 *pf = compile(Dst, L, NULL, ref);
689
690 assert(lua_gettop(L) == top + 1);
691
692 return *pf;
693 }
694
695 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
696 {
697 size_t i, nargs;
698 int num_upvals;
699 int orig_top;
700 const struct ctype* mbr_ct;
701 struct jit* Dst = get_jit(L);
702 struct reg_alloc reg;
703 void* p;
704 int top = lua_gettop(L);
705 int* perr = &Dst->last_errno;
706
707 ct_usr = lua_absindex(L, ct_usr);
708
709 memset(&reg, 0, sizeof(reg));
710 reg.off = 32 + REGISTER_STACK_SPACE(ct);
711
712 dasm_setup(Dst, build_actionlist);
713
714 orig_top = lua_gettop(L);
715 p = push_cdata(L, ct_usr, ct);
716 *(cfunction*) p = func;
717
718 nargs = lua_rawlen(L, ct_usr);
719
720 if (ct->calling_convention != C_CALL && ct->has_var_arg) {
721 luaL_error(L, "vararg is only allowed with the c calling convention");
722 }
723
724 | push rbp
725 | mov rbp, rsp
726 | push L_ARG
727 | push TOP
728 | // stack is 0 (mod 16) (TOP, L_ARG, rbp, rip)
729 |
730 | // Get L from our arguments and allocate some stack for lua_gettop
731 | mov L_ARG, rdi
732 |
733 | call_r extern lua_gettop, L_ARG
734 | mov TOP, rax // no need for movzxd rax, eax - high word guarenteed to be zero by x86-64
735 | cmp rax, nargs
736 if (!ct->has_var_arg) {
737 | jge >2
738 | too_few_arguments
739 | 2:
740 | jle >1
741 | too_many_arguments
742 } else {
743 | jge >1
744 | too_few_arguments
745 }
746
747 | 1:
748
749 /* no need to zero extend eax returned by lua_gettop to rax as x86-64
750 * preguarentees that the upper 32 bits will be zero */
751 | shl rax, 4 // reserve 16 bytes per argument - this maintains the alignment mod 16
752 | sub rsp, rax
753 | sub rsp, 32 + REGISTER_STACK_SPACE(ct) // reserve an extra 32 to call local functions
754
755 for (i = 1; i <= nargs; i++) {
756 lua_rawgeti(L, ct_usr, (int) i);
757 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
758
759 if (mbr_ct->pointers || mbr_ct->is_reference) {
760 lua_getuservalue(L, -1);
761 num_upvals = lua_gettop(L) - orig_top;
762 | call_rrrp extern check_typed_pointer, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
763 add_pointer(Dst, ct, &reg);
764 } else {
765 switch (mbr_ct->type) {
766 case FUNCTION_PTR_TYPE:
767 lua_getuservalue(L, -1);
768 num_upvals = lua_gettop(L) - orig_top;
769 | call_rrrp extern check_typed_cfunction, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
770 add_pointer(Dst, ct, &reg);
771 break;
772
773 case ENUM_TYPE:
774 lua_getuservalue(L, -1);
775 num_upvals = lua_gettop(L) - orig_top;
776 | call_rrrp, extern check_enum, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
777 add_int(Dst, ct, &reg, 0);
778 break;
779
780 case INT8_TYPE:
781 | call_rr extern check_int32, L_ARG, i
782 if (mbr_ct->is_unsigned) {
783 | movzx eax, al
784 } else {
785 | movsx eax, al
786 }
787 add_int(Dst, ct, &reg, 0);
788 lua_pop(L, 1);
789 break;
790
791 case INT16_TYPE:
792 | call_rr extern check_int32, L_ARG, i
793 if (mbr_ct->is_unsigned) {
794 | movzx eax, ax
795 } else {
796 | movsx eax, ax
797 }
798 add_int(Dst, ct, &reg, 0);
799 lua_pop(L, 1);
800 break;
801
802 case BOOL_TYPE:
803 | call_rr extern check_int32, L_ARG, i
804 | cmp eax, 0
805 | setne al
806 | movzx eax, al
807 add_int(Dst, ct, &reg, 0);
808 lua_pop(L, 1);
809 break;
810
811 case INT32_TYPE:
812 if (mbr_ct->is_unsigned) {
813 | call_rr extern check_uint32, L_ARG, i
814 } else {
815 | call_rr extern check_int32, L_ARG, i
816 }
817 add_int(Dst, ct, &reg, 0);
818 lua_pop(L, 1);
819 break;
820
821 case INTPTR_TYPE:
822 | call_rr extern check_uintptr, L_ARG, i
823 add_pointer(Dst, ct, &reg);
824 lua_pop(L, 1);
825 break;
826
827 case INT64_TYPE:
828 if (mbr_ct->is_unsigned) {
829 | call_rr extern check_uint64, L_ARG, i
830 } else {
831 | call_rr extern check_int64, L_ARG, i
832 }
833 add_int(Dst, ct, &reg, 1);
834 lua_pop(L, 1);
835 break;
836
837 case DOUBLE_TYPE:
838 | call_rr extern check_double, L_ARG, i
839 add_float(Dst, ct, &reg, 1);
840 lua_pop(L, 1);
841 break;
842
843 case COMPLEX_DOUBLE_TYPE:
844 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
845 * there is a hidden first parameter that points to 16 bytes where
846 * the returned arg is stored (this is popped by the called
847 * function) */
848 #if defined _WIN64 || defined __amd64__
849 | call_rr extern check_complex_double, L_ARG, i
850 add_float(Dst, ct, &reg, 1);
851 | movq xmm0, xmm1
852 add_float(Dst, ct, &reg, 1);
853 #else
854 | lea rax, [rsp+reg.off]
855 | sub rsp, 4
856 | call_rrr extern check_complex_double, rax, L_ARG, i
857 reg.off += 16;
858 #endif
859 lua_pop(L, 1);
860 break;
861
862 case FLOAT_TYPE:
863 | call_rr extern check_double, L_ARG, i
864 add_float(Dst, ct, &reg, 0);
865 lua_pop(L, 1);
866 break;
867
868 case COMPLEX_FLOAT_TYPE:
869 #if defined _WIN64 || defined __amd64__
870 | call_rr extern check_complex_float, L_ARG, i
871 /* complex floats are two floats packed into a double */
872 add_float(Dst, ct, &reg, 1);
873 #else
874 /* returned complex floats use eax and edx */
875 | call_rr extern check_complex_float, L_ARG, i
876 | mov [rsp], eax
877 | fld dword [rsp]
878 add_float(Dst, ct, &reg, 0);
879 | mov [rsp], edx
880 | fld dword [rsp]
881 add_float(Dst, ct, &reg, 0);
882 #endif
883 lua_pop(L, 1);
884 break;
885
886 default:
887 luaL_error(L, "NYI: call arg type");
888 }
889 }
890 }
891
892 if (ct->has_var_arg) {
893 if (reg.floats < MAX_FLOAT_REGISTERS(ct)) {
894 | mov rax, rsp
895 | add rax, 32 + 8*(MAX_INT_REGISTERS(ct) + reg.floats)
896 | call_rrrrr extern unpack_varargs_float, L_ARG, nargs+1, TOP, MAX_FLOAT_REGISTERS(ct) - reg.floats, rax
897 }
898
899 if (reg.ints < MAX_INT_REGISTERS(ct)) {
900 | mov rax, rsp
901 | add rax, 32 + 8*(reg.ints)
902 | call_rrrrr extern unpack_varargs_int, L_ARG, nargs+1, TOP, MAX_INT_REGISTERS(ct) - reg.ints, rax
903 }
904
905 | mov rax, rsp
906 | add rax, reg.off
907 | call_rrrrrr extern unpack_varargs_stack_skip, L_ARG, nargs+1, TOP, MAX_INT_REGISTERS(ct) - reg.ints, MAX_FLOAT_REGISTERS(ct) - reg.floats, rax
908
909 reg.floats = MAX_FLOAT_REGISTERS(ct);
910 reg.ints = MAX_INT_REGISTERS(ct);
911 }
912
913 | mov64 rcx, perr
914 | mov eax, dword [rcx]
915 | call_r extern SetLastError, rax
916
917 /* remove the stack space to call local functions */
918 | add rsp, 32
919
920 switch (reg.floats) {
921 case 8:
922 | movq xmm7, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+7)]
923 case 7:
924 | movq xmm6, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+6)]
925 case 6:
926 | movq xmm5, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+5)]
927 case 5:
928 | movq xmm4, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+4)]
929 case 4:
930 | movq xmm3, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+3)]
931 case 3:
932 | movq xmm2, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+2)]
933 case 2:
934 | movq xmm1, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+1)]
935 case 1:
936 | movq xmm0, qword [rsp + 8*(MAX_INT_REGISTERS(ct))]
937 case 0:
938 break;
939 }
940
941 switch (reg.ints) {
942 case 6:
943 | mov r9, [rsp + 8*5]
944 case 5:
945 | mov r8, [rsp + 8*4]
946 case 4:
947 | mov rcx, [rsp + 8*3]
948 case 3:
949 | mov rdx, [rsp + 8*2]
950 case 2:
951 | mov rsi, [rsp + 8*1]
952 case 1:
953 | mov rdi, [rsp]
954 case 0:
955 break;
956 }
957
958 | add rsp, REGISTER_STACK_SPACE(ct)
959
960 if (ct->has_var_arg) {
961 /* al stores an upper limit on the number of float register, note that
962 * its allowed to be more than the actual number of float registers used as
963 * long as its 0-8 */
964 | mov al, 8
965 }
966
967 | call extern FUNCTION
968 | sub rsp, 48 // 32 to be able to call local functions, 16 so we can store some local variables
969
970 /* note on windows X86 the stack may be only aligned to 4 (stdcall will
971 * have popped a multiple of 4 bytes), but we don't need 16 byte alignment on
972 * that platform
973 */
974
975 lua_rawgeti(L, ct_usr, 0);
976 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
977
978 if (mbr_ct->pointers || mbr_ct->is_reference || mbr_ct->type == INTPTR_TYPE) {
979 lua_getuservalue(L, -1);
980 num_upvals = lua_gettop(L) - orig_top;
981 | mov [rsp+32], rax // save the pointer
982 | get_errno
983 | call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
984 | mov rcx, [rsp+32]
985 | mov [rax], rcx // *(void**) cdata = val
986 | lua_return_arg
987
988 } else {
989 switch (mbr_ct->type) {
990 case FUNCTION_PTR_TYPE:
991 lua_getuservalue(L, -1);
992 num_upvals = lua_gettop(L) - orig_top;
993 | mov [rsp+32], rax // save the function pointer
994 | get_errno
995 | call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
996 | mov rcx, [rsp+32]
997 | mov [rax], rcx // *(cfunction**) cdata = val
998 | lua_return_arg
999 break;
1000
1001 case INT64_TYPE:
1002 lua_pop(L, 1);
1003 if (mbr_ct->is_unsigned) {
1004 | lua_return_ulong
1005 } else {
1006 | lua_return_long
1007 }
1008 break;
1009
1010 case COMPLEX_FLOAT_TYPE:
1011 lua_getuservalue(L, -1);
1012 num_upvals = lua_gettop(L) - orig_top;
1013 | // complex floats are returned as two floats packed into xmm0
1014 | movq qword [rsp+32], xmm0
1015 |
1016 | get_errno
1017 | call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1018 |
1019 | // ((complex_float*) cdata) = val
1020 | mov rcx, [rsp+32]
1021 | mov [rax], rcx
1022 |
1023 | lua_return_arg
1024 break;
1025
1026 case COMPLEX_DOUBLE_TYPE:
1027 lua_getuservalue(L, -1);
1028 num_upvals = lua_gettop(L) - orig_top;
1029 | // complex doubles are returned as xmm0 and xmm1
1030 | movq qword [rsp+40], xmm1
1031 | movq qword [rsp+32], xmm0
1032 |
1033 | get_errno
1034 | call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1035 |
1036 | // ((complex_double*) cdata)->real = val0
1037 | // ((complex_double*) cdata)->imag = val1
1038 | mov rcx, [rsp+40]
1039 | mov [rax+8], rcx
1040 | mov rcx, [rsp+32]
1041 | mov [rax], rcx
1042 |
1043 |
1044 | lua_return_arg
1045 break;
1046
1047 case VOID_TYPE:
1048 lua_pop(L, 1);
1049 | lua_return_void
1050 break;
1051
1052 case BOOL_TYPE:
1053 lua_pop(L, 1);
1054 | lua_return_bool
1055 break;
1056
1057 case INT8_TYPE:
1058 lua_pop(L, 1);
1059 if (mbr_ct->is_unsigned) {
1060 | movzx eax, al
1061 } else {
1062 | movsx eax, al
1063 }
1064 | lua_return_int
1065 break;
1066
1067 case INT16_TYPE:
1068 lua_pop(L, 1);
1069 if (mbr_ct->is_unsigned) {
1070 | movzx eax, ax
1071 } else {
1072 | movsx eax, ax
1073 }
1074 | lua_return_int
1075 break;
1076
1077 case INT32_TYPE:
1078 case ENUM_TYPE:
1079 lua_pop(L, 1);
1080 if (mbr_ct->is_unsigned) {
1081 | lua_return_uint
1082 } else {
1083 | lua_return_int
1084 }
1085 break;
1086
1087 case FLOAT_TYPE:
1088 lua_pop(L, 1);
1089 | cvtss2sd xmm0, xmm0
1090 | lua_return_double
1091 break;
1092
1093 case DOUBLE_TYPE:
1094 lua_pop(L, 1);
1095 | lua_return_double
1096 break;
1097
1098 default:
1099 luaL_error(L, "NYI: call return type");
1100 }
1101 }
1102
1103 {
1104 cfunction f = compile(Dst, L, func, LUA_NOREF);
1105 /* add a callback as an upval so that the jitted code gets cleaned up when
1106 * the function gets gc'd */
1107 push_callback(L, f, func);
1108 num_upvals = lua_gettop(L) - orig_top;
1109 lua_pushcclosure(L, (lua_CFunction) f, num_upvals);
1110 }
1111 }
1112