Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_arm64.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 arm64
10
11 |.actionlist build_actionlist
12 |.globalnames globnames
13 |.externnames extnames
14
15 |.define L_ARG, x19
16 |.define TOP, x20
17 |.define rbp, x21
18 |.define EXTRA_REG_1, x22
19 |.define EXTRA_REG_2, x23
20 |.define EXTRA_REG_3, x24
21
22 #define ZERO_TO_FIFTEEN_OF_64(x) (0x000000000000FFFF & (int64_t)x)
23 #define SIXTEEN_TO_THIRTYONE_OF_64(x) ((0x00000000FFFF0000 & (int64_t)x) >> 16)
24 #define THIRTYTWO_TO_FORTYSEVEN_OF_64(x) ((0x0000FFFF00000000 & (int64_t)x) >> 32)
25 #define FORTYEIGHT_TO_SIXTYTHREE_OF_64(x) ((0xFFFF000000000000 & (int64_t)x) >> 48)
26
27 |.macro mov64, reg, imm
28 | movz reg, #(ZERO_TO_FIFTEEN_OF_64(imm))
29 | movk reg, #(SIXTEEN_TO_THIRTYONE_OF_64(imm)), lsl #16
30 | movk reg, #(THIRTYTWO_TO_FORTYSEVEN_OF_64(imm)), lsl #32
31 | movk reg, #(FORTYEIGHT_TO_SIXTYTHREE_OF_64(imm)), lsl #48
32 |.endmacro
33
34 |.macro mov32, reg, imm
35 | movz reg, #(ZERO_TO_FIFTEEN_OF_64(imm))
36 | movk reg, #(SIXTEEN_TO_THIRTYONE_OF_64(imm)), lsl #16
37 |.endmacro
38
39 | // the 5 and 6 arg forms are only used on posix x64
40 |.macro call_ririir, func, arg0, arg1, arg2, arg3, arg4, arg5
41 | mov x5, arg5
42 | mov x4, #arg4
43 | mov x3, #arg3
44 | mov x2, arg2
45 | mov x1, #arg1
46 | mov x0, arg0
47 | bl func
48 |.endmacro
49 |.macro call_rrrrrr, func, arg0, arg1, arg2, arg3, arg4, arg5
50 | mov x5, arg5
51 | mov x4, arg4
52 | mov x3, arg3
53 | mov x2, arg2
54 | mov x1, arg1
55 | mov x0, arg0
56 | bl func
57 |.endmacro
58 |.macro call_ririr, func, arg0, arg1, arg2, arg3, arg4
59 | mov x4, arg4
60 | mov x3, #arg3
61 | mov x2, arg2
62 | mov x1, #arg1
63 | mov x0, arg0
64 | bl func
65 |.endmacro
66 |.macro call_rrrrr, func, arg0, arg1, arg2, arg3, arg4
67 | mov x4, arg4
68 | mov x3, arg3
69 | mov x2, arg2
70 | mov x1, arg1
71 | mov x0, arg0
72 | bl func
73 |.endmacro
74 |
75 |.macro call_riwp, func, arg0, arg1, arg2, arg3
76 | mov64 x3, arg3
77 | mov w2, arg2
78 | mov x1, #arg1
79 | mov x0, arg0
80 | bl func
81 |.endmacro
82 |.macro call_rwwp, func, arg0, arg1, arg2, arg3
83 | mov64 x3, (arg3)
84 | mov w2, arg2
85 | mov w1, arg1
86 | mov x0, arg0
87 | bl func
88 |.endmacro
89 |.macro call_riip, func, arg0, arg1, arg2, arg3
90 | mov64 x3, (arg3)
91 | mov x2, #(arg2)
92 | mov x1, #(arg1)
93 | mov x0, arg0
94 | bl func
95 |.endmacro
96 |.macro call_rrrp, func, arg0, arg1, arg2, arg3
97 | mov64 x3, arg3
98 | mov x2, arg2
99 | mov x1, arg1
100 | mov x0, arg0
101 | bl func
102 |.endmacro
103 |.macro call_rrrr, func, arg0, arg1, arg2, arg3
104 | mov x3, arg3
105 | mov x2, arg2
106 | mov x1, arg1
107 | mov x0, arg0
108 | bl func
109 |.endmacro
110 |
111 |.macro call_rwp, func, arg0, arg1, arg2
112 | mov64 x2, arg2
113 | mov w1, arg1
114 | mov x0, arg0
115 | bl func
116 |.endmacro
117 |.macro call_rip, func, arg0, arg1, arg2
118 | mov64 x2, arg2
119 | mov x1, #arg1
120 | mov x0, arg0
121 | bl func
122 |.endmacro
123 |.macro call_rrp, func, arg0, arg1, arg2
124 | mov64 x2, arg2
125 | mov x1, arg1
126 | mov x0, arg0
127 | bl func
128 |.endmacro
129 |.macro call_rww, func, arg0, arg1, arg2
130 | mov w2, arg2
131 | mov w1, arg1
132 | mov x0, arg0
133 | bl func
134 |.endmacro
135 |.macro call_rwi, func, arg0, arg1, arg2
136 | mov x2, #(arg2)
137 | mov w1, arg1
138 | mov x0, arg0
139 | bl func
140 |.endmacro
141 |.macro call_rii, func, arg0, arg1, arg2
142 | mov x2, #(arg2)
143 | mov x1, #(arg1)
144 | mov x0, arg0
145 | bl func
146 |.endmacro
147 |.macro call_rrr, func, arg0, arg1, arg2
148 | mov x2, arg2
149 | mov x1, arg1
150 | mov x0, arg0
151 | bl func
152 |.endmacro
153 |
154 |.macro call_rp, func, arg0, arg1
155 | mov64 x1, arg1
156 | mov x0, arg0
157 | bl func
158 |.endmacro
159 |.macro call_ri, func, arg0, arg1
160 | mov x1, #(arg1)
161 | mov x0, arg0
162 | bl func
163 |.endmacro
164 |.macro call_rw, func, arg0, arg1
165 | mov w1, arg1
166 | mov x0, arg0
167 | bl func
168 |.endmacro
169 |.macro call_rr, func, arg0, arg1
170 | mov x1, arg1
171 | mov x0, arg0
172 | bl func
173 |.endmacro
174 |
175 |.macro call_w, func, arg0
176 | mov w0, arg0
177 | bl func
178 |.endmacro
179 |.macro call_r, func, arg0
180 | mov x0, arg0
181 | bl func
182 |.endmacro
183 |
184
185 |.macro call_debug
186 | sub sp, sp, #(16*8)
187 | str x0, [sp]
188 | str x1, [sp,#8]
189 | str x2, [sp,#16]
190 | str x3, [sp,#24]
191 | str x4, [sp,#32]
192 | str x5, [sp,#40]
193 | str x6, [sp,#48]
194 | str x7, [sp,#56]
195 | str x8, [sp,#64]
196 | str x9, [sp,#72]
197 | str x10, [sp,#80]
198 | str x11, [sp,#88]
199 | str x12, [sp,#96]
200 | str x13, [sp,#104]
201 | str x14, [sp,#112]
202 | str x15, [sp,#120]
203 | bl extern debug_statement
204 | ldr x0, [sp]
205 | ldr x1, [sp,#8]
206 | ldr x2, [sp,#16]
207 | ldr x3, [sp,#24]
208 | ldr x4, [sp,#32]
209 | ldr x5, [sp,#40]
210 | ldr x6, [sp,#48]
211 | ldr x7, [sp,#56]
212 | ldr x8, [sp,#64]
213 | ldr x9, [sp,#72]
214 | ldr x10, [sp,#80]
215 | ldr x11, [sp,#88]
216 | ldr x12, [sp,#96]
217 | ldr x13, [sp,#104]
218 | ldr x14, [sp,#112]
219 | ldr x15, [sp,#120]
220 | add sp, sp, #(16*8)
221 |.endmacro
222
223 |.macro call_debug_float, one
224 | sub sp, sp, #(16*8)
225 | str d0, [sp]
226 | str d1, [sp,#16]
227 | str d2, [sp,#32]
228 | str d3, [sp,#48]
229 | str d4, [sp,#64]
230 | str d5, [sp,#80]
231 | str d6, [sp,#96]
232 | str d7, [sp,#112]
233 | fmov s0, one
234 | bl extern debug_float
235 | ldr d0, [sp]
236 | ldr d1, [sp,#16]
237 | ldr d2, [sp,#32]
238 | ldr d3, [sp,#48]
239 | ldr d4, [sp,#64]
240 | ldr d5, [sp,#80]
241 | ldr d6, [sp,#96]
242 | ldr d7, [sp,#112]
243 | add sp, sp, #(16*8)
244 |.endmacro
245
246 |.macro call_debug_double, one
247 | sub sp, sp, #(16*8)
248 | str d0, [sp]
249 | str d1, [sp,#16]
250 | str d2, [sp,#32]
251 | str d3, [sp,#48]
252 | str d4, [sp,#64]
253 | str d5, [sp,#80]
254 | str d6, [sp,#96]
255 | str d7, [sp,#112]
256 | fmov d0, one
257 | bl extern debug_double
258 | ldr d0, [sp]
259 | ldr d1, [sp,#16]
260 | ldr d2, [sp,#32]
261 | ldr d3, [sp,#48]
262 | ldr d4, [sp,#64]
263 | ldr d5, [sp,#80]
264 | ldr d6, [sp,#96]
265 | ldr d7, [sp,#112]
266 | add sp, sp, #(16*8)
267 |.endmacro
268
269 |.macro call_debug_int, one
270 | sub sp, sp, #(16*8)
271 | str x0, [sp]
272 | str x1, [sp,#8]
273 | str x2, [sp,#16]
274 | str x3, [sp,#24]
275 | str x4, [sp,#32]
276 | str x5, [sp,#40]
277 | str x6, [sp,#48]
278 | str x7, [sp,#56]
279 | str x8, [sp,#64]
280 | str x9, [sp,#72]
281 | str x10, [sp,#80]
282 | str x11, [sp,#88]
283 | str x12, [sp,#96]
284 | str x13, [sp,#104]
285 | str x14, [sp,#112]
286 | str x15, [sp,#120]
287 | mov x0, one
288 | bl extern debug_int
289 | ldr x0, [sp]
290 | ldr x1, [sp,#8]
291 | ldr x2, [sp,#16]
292 | ldr x3, [sp,#24]
293 | ldr x4, [sp,#32]
294 | ldr x5, [sp,#40]
295 | ldr x6, [sp,#48]
296 | ldr x7, [sp,#56]
297 | ldr x8, [sp,#64]
298 | ldr x9, [sp,#72]
299 | ldr x10, [sp,#80]
300 | ldr x11, [sp,#88]
301 | ldr x12, [sp,#96]
302 | ldr x13, [sp,#104]
303 | ldr x14, [sp,#112]
304 | ldr x15, [sp,#120]
305 | add sp, sp, #(16*8)
306 |.endmacro
307
308 |.macro call_debug_1, one
309 | sub sp, sp, #(16*8)
310 | str x0, [sp]
311 | str x1, [sp,#8]
312 | str x2, [sp,#16]
313 | str x3, [sp,#24]
314 | str x4, [sp,#32]
315 | str x5, [sp,#40]
316 | str x6, [sp,#48]
317 | str x7, [sp,#56]
318 | str x8, [sp,#64]
319 | str x9, [sp,#72]
320 | str x10, [sp,#80]
321 | str x11, [sp,#88]
322 | str x12, [sp,#96]
323 | str x13, [sp,#104]
324 | str x14, [sp,#112]
325 | str x15, [sp,#120]
326 | mov x0, one
327 | bl extern debug_statement_with_arg
328 | ldr x0, [sp]
329 | ldr x1, [sp,#8]
330 | ldr x2, [sp,#16]
331 | ldr x3, [sp,#24]
332 | ldr x4, [sp,#32]
333 | ldr x5, [sp,#40]
334 | ldr x6, [sp,#48]
335 | ldr x7, [sp,#56]
336 | ldr x8, [sp,#64]
337 | ldr x9, [sp,#72]
338 | ldr x10, [sp,#80]
339 | ldr x11, [sp,#88]
340 | ldr x12, [sp,#96]
341 | ldr x13, [sp,#104]
342 | ldr x14, [sp,#112]
343 | ldr x15, [sp,#120]
344 | add sp, sp, #(16*8)
345 |.endmacro
346
347 |.macro call_debug_sp
348 | mov x15, sp
349 | call_debug_1 x15
350 |.endmacro
351
352 |.macro epilog
353 | //ldr L_ARG, [rbp, #(-16)]
354 | //ldr TOP, [rbp, #(-8)]
355 | ldp L_ARG, TOP, [rbp, #(-16)]
356 | mov sp, rbp
357 | ldp rbp, EXTRA_REG_1, [sp]
358 | add sp, sp, #16
359 | ldp EXTRA_REG_2, EXTRA_REG_3, [sp]
360 | add sp, sp, #16
361 | ldp x29, x30, [sp]
362 | add sp, sp, #16
363 | ret
364 |.endmacro
365
366 |.macro get_errno // note trashes registers
367 | bl extern GetLastError
368 | mov64 x9, perr
369 | str w0, [x9]
370 |.endmacro
371
372 #define MESSAGE_1 "Too few arguments"
373 #define MESSAGE_2 "Too many arguments"
374
375 |.macro too_few_arguments
376 | mov64 x9, MESSAGE_1
377 | call_rr extern luaL_error, L_ARG, x9
378 |.endmacro
379
380 |.macro too_many_arguments
381 | mov64 x9, MESSAGE_2
382 | call_rr extern luaL_error, L_ARG, x9
383 |.endmacro
384
385 |.macro lua_return_arg
386 | mov x0, #1
387 | epilog
388 |.endmacro
389
390 |.macro lua_return_void
391 | get_errno
392 | mov x0, xzr
393 | epilog
394 |.endmacro
395
396 |.macro lua_return_double
397 | str d0, [sp, #32]
398 | get_errno
399 | ldr d0, [sp, #32]
400 | mov x0, L_ARG
401 | bl extern lua_pushnumber
402 | lua_return_arg
403 |.endmacro
404
405 |.macro lua_return_bool
406 | ubfm x0, x0, #0, #7
407 | str x0, [sp,#32]
408 | get_errno
409 | ldr w9, [sp,#32]
410 | call_rw extern lua_pushboolean, L_ARG, w9
411 | lua_return_arg
412 |.endmacro
413
414 |.macro lua_return_int
415 | //mov [sp,#32], eax
416 | str x0, [sp,#32]
417 | get_errno
418 | ldr w9, [sp,#32]
419 | call_rw extern push_int, L_ARG, w9
420 | lua_return_arg
421 |.endmacro
422
423 |.macro lua_return_uint
424 | str x0, [sp,#32]
425 | get_errno
426 | ldr w9, [sp,#32]
427 | call_rw extern push_uint, L_ARG, w9
428 | lua_return_arg
429 |.endmacro
430
431 |.macro lua_return_long
432 | str x0, [sp,#32]
433 | get_errno
434 | ldr x0, [sp,#32]
435 | call_rr extern lua_pushinteger, L_ARG, x0
436 | lua_return_arg
437 |.endmacro
438
439 |.macro lua_return_ulong
440 | str x0, [sp,#32]
441 | get_errno
442 | ldr x0, [sp,#32]
443 | call_rr extern lua_pushinteger, L_ARG, x0
444 | lua_return_arg
445 |.endmacro
446
447 |.macro save_registers
448 | // use rbp relative so we store values in the outer stack frame
449 | // Floating point registers
450 | str d0, [rbp, #-32]
451 | str d1, [rbp, #-48]
452 | str d2, [rbp, #-64]
453 | str d3, [rbp, #-80]
454 | str d4, [rbp, #-88]
455 | str d5, [rbp, #-96]
456 | str d6, [rbp, #-112]
457 | str d7, [rbp, #-128]
458
459 | // Integral registers
460 | str x0, [rbp, #-136]
461 | str x1, [rbp, #-144]
462 | str x2, [rbp, #-152]
463 | str x3, [rbp, #-160]
464 | str x4, [rbp, #-168]
465 | str x5, [rbp, #-176]
466 | str x6, [rbp, #-184]
467 | str x7, [rbp, #-192]
468 |.endmacro
469
470 #define JUMP_SIZE 24
471 #define MIN_BRANCH ((INT32_MIN) >> 6)
472 #define MAX_BRANCH ((INT32_MAX) >> 6)
473 #define BRANCH_OFF -4
474
475 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
476 {
477 /* The jump code is the function pointer followed by a stub to call the
478 * function pointer. The stub exists so we can jump to functions with an
479 * offset farther than +/-128MB.
480 *
481 * Note we have to manually set this up since there are commands buffered
482 * in the jit state.
483 */
484 //printf("%s:%d code = [%p]\n", __FILE__, __LINE__, code);
485 *((cfunction*) (&code[0])) = func;
486 *((uint32_t*) (&code[8])) = (uint32_t)0x10FFFFC9U; // ADR X9, #-8
487 *((uint32_t*) (&code[12])) = (uint32_t)0xF940012aU; // LDR X10, [X9]
488 *((uint32_t*) (&code[16])) = (uint32_t)0xD61F0140U; // BR X10
489 *((uint32_t*) (&code[20])) = (uint32_t)0x0; // NULL
490 //printf("%s:%d addr = [%p] sizeof(cfunction) = [%zu] function=[%p]\n",
491 //__FILE__, __LINE__, &(code[8]), sizeof(cfunction), (*((cfunction*)(&code[0]))));
492 }
493
494 void compile_globals(struct jit* jit, lua_State* L)
495 {
496 struct jit* Dst = jit;
497 int* perr = &jit->last_errno;
498 dasm_setup(Dst, build_actionlist);
499
500 /* Note: since the return code uses EBP to reset the stack pointer, we
501 * don't have to track the amount of stack space used. It also means we
502 * can handle stdcall and cdecl with the same code.
503 */
504
505 /* Note the various call_* functions want 32 bytes of 16 byte aligned
506 * stack
507 */
508
509 compile(Dst, L, NULL, LUA_NOREF);
510 }
511
512 int arm64_return_size(lua_State* L, int usr, const struct ctype* ct)
513 {
514 int ret = 0;
515 const struct ctype* mt;
516
517 if (ct->calling_convention != C_CALL) {
518 size_t i;
519 size_t argn = lua_rawlen(L, usr);
520 for (i = 1; i <= argn; i++) {
521 lua_rawgeti(L, usr, (int) i);
522 mt = (const struct ctype*) lua_touserdata(L, -1);
523
524 if (mt->pointers || mt->is_reference) {
525 ret += sizeof(void*);
526 } else {
527 switch (mt->type) {
528 case DOUBLE_TYPE:
529 case COMPLEX_FLOAT_TYPE:
530 case INT64_TYPE:
531 ret += 8;
532 break;
533 case COMPLEX_DOUBLE_TYPE:
534 ret += 16;
535 break;
536 case INTPTR_TYPE:
537 ret += sizeof(intptr_t);
538 break;
539 case FUNCTION_PTR_TYPE:
540 ret += sizeof(cfunction);
541 break;
542 case BOOL_TYPE:
543 case FLOAT_TYPE:
544 case INT8_TYPE:
545 case INT16_TYPE:
546 case INT32_TYPE:
547 case ENUM_TYPE:
548 ret += 4;
549 break;
550 default:
551 return luaL_error(L, "NYI - argument type");
552 }
553 }
554
555 lua_pop(L, 1);
556 }
557 }
558
559 return ret;
560 }
561
562 #define MAX_INT_REGISTERS(ct) 8 /* x0-x7 */
563 #define MAX_FLOAT_REGISTERS(ct) 8 /* q0-q7 */
564
565 struct reg_alloc {
566 int floats;
567 int ints;
568 int off;
569 };
570
571 #define REGISTER_STACK_SPACE(ct) ((8*8) + (128*8)) /* 8, 64 bit registers x0-x7 and 8, 128 bit registers v0-v7 */
572
573 /* Fastcall:
574 * Uses ecx, edx as first two int registers
575 * Everything else on stack (include 64bit ints)
576 * No overflow stack space
577 * Pops the stack before returning
578 * Returns int in eax, float in ST0
579 * We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs
580 */
581
582 typedef enum {
583 int8,
584 int16,
585 int32,
586 int64,
587 int128
588 } int_type_enum;
589
590 static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int_type_enum int_type)
591 {
592 /* grab the register from the shadow space */
593 if (reg->ints < MAX_INT_REGISTERS(ct)) {
594 int x = -(136 + 8*reg->ints);
595 | ldr x9, [rbp , #(x)]
596 reg->ints++;
597 }
598 else {
599 switch (int_type) {
600 case int8:
601 #ifdef OS_OSX
602 | ldrb w9, [rbp , #(reg->off)]
603 reg->off += 1;
604 break;
605 #endif
606 case int16:
607 #ifdef OS_OSX
608 | ldrh w9, [rbp , #(reg->off)]
609 reg->off += 2;
610 break;
611 #endif
612 case int32:
613 | ldr w9, [rbp , #(reg->off)]
614 #ifdef OS_OSX
615 reg->off += 4;
616 #else
617 reg->off += 8;
618 #endif
619 break;
620 case int64:
621 | ldr x9, [rbp , #(reg->off)] // x9 is a scratch register
622 reg->off += 8;
623 break;
624 case int128:
625 default:
626 fprintf(stderr, "Unsupported data type\n");
627 abort();
628 break;
629 }
630 }
631 }
632
633 static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int_type_enum int_type)
634 {
635 if (reg->ints < MAX_INT_REGISTERS(ct)) {
636 | str x0, [sp , #(32 + 8*reg->ints)]
637 reg->ints++;
638 }
639 else {
640 #ifndef OS_OSX
641 if (reg->off % 8 != 0) {
642 reg->off += 8 - (reg->off % 8);
643 }
644 #endif
645 switch (int_type) {
646 case int8:
647 #ifdef OS_OSX
648 | strb w0, [sp , #(reg->off)]
649 reg->off += 1;
650 break;
651 #endif
652 case int16:
653 #ifdef OS_OSX
654 | strh w0, [sp , #(reg->off)]
655 reg->off += 2;
656 break;
657 #endif
658 case int32:
659 | str w0, [sp , #(reg->off)]
660 #ifdef OS_OSX
661 reg->off += 4;
662 #else
663 reg->off += 8;
664 #endif
665 break;
666 case int64:
667 | str x0, [sp , #(reg->off)]
668 reg->off += 8;
669 break;
670 case int128:
671 default:
672 fprintf(stderr, "Unsupported data type\n");
673 abort();
674 break;
675 }
676 }
677 }
678
679 static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
680 {
681 int off;
682
683 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
684 off = -32 - 16*reg->floats;
685 reg->floats++;
686 }
687 else {
688 off = reg->off;
689 if (is_double) {
690 reg->off += 8;
691 }
692 else {
693 #ifdef OS_OSX
694 reg->off += 4;
695 #else
696 reg->off += 8;
697 #endif
698 }
699 }
700
701 if (is_double) {
702 | ldr d0, [rbp, #off]
703 } else {
704 | ldr s16, [rbp, #off] // s16 is a scratch register
705 | fcvt d0, s16
706 }
707 }
708
709 static void get_complex_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
710 {
711 if (reg->floats < (MAX_FLOAT_REGISTERS(ct)-1)) {
712 int off = 0;
713 if (is_double) {
714 off = -32 - 16*reg->floats;
715 | ldr d0, [rbp, #off]
716 reg->floats++;
717 off = -32 - 16*reg->floats;
718 | ldr d1, [rbp, #off]
719 reg->floats++;
720 } else {
721 off = -32 - 16*reg->floats;
722 | ldr s0, [rbp, #off] // s16 is a scratch register
723 reg->floats++;
724 off = -32 - 16*reg->floats;
725 | ldr s1, [rbp, #off] // s16 is a scratch register
726 reg->floats++;
727 }
728 }
729 else {
730 if (reg->floats == (MAX_FLOAT_REGISTERS(ct)-1)) {
731 reg->floats = MAX_FLOAT_REGISTERS(ct);
732 }
733 if (is_double) {
734 | ldr d0, [rbp, #(reg->off)]
735 reg->off += 8;
736 | ldr d1, [rbp, #(reg->off)]
737 reg->off += 8;
738 } else {
739 | ldr s0, [rbp, #(reg->off)] // s16 is a scratch register
740 #ifdef OS_OSX
741 reg->off += 4;
742 #else
743 reg->off += 8;
744 #endif
745 | ldr s1, [rbp, #(reg->off)] // s16 is a scratch register
746 #ifdef OS_OSX
747 reg->off += 4;
748 #else
749 reg->off += 8;
750 #endif
751 }
752 }
753
754 }
755
756 static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
757 {
758 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
759 if (is_double) {
760 | str d0, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
761 } else {
762 | fcvt s0, d0 // This is necessary because in lua float is also
763 // handled as doube
764 | str s0, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
765 }
766 reg->floats++;
767 }
768 else if (is_double) {
769 | str d0, [sp, #(reg->off)]
770 reg->off += 8;
771 } else {
772 | fcvt s0, d0
773 | str s0, [sp, #(reg->off)]
774 #ifdef OS_OSX
775 reg->off += 4;
776 #else
777 reg->off += 8;
778 #endif
779 }
780 }
781
782 static void add_complex_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
783 {
784 //printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
785 if (reg->floats < (MAX_FLOAT_REGISTERS(ct)-1)) {
786 if (is_double) {
787 | str d0, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
788 reg->floats++;
789 | str d1, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
790 reg->floats++;
791 } else {
792 | str s0, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
793 reg->floats++;
794 | str s1, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
795 reg->floats++;
796 }
797 if (reg->floats > (MAX_FLOAT_REGISTERS(ct)-1)) {
798 reg->floats = MAX_FLOAT_REGISTERS(ct);
799 }
800 }
801 else {
802 //printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
803 if (reg->floats == (MAX_FLOAT_REGISTERS(ct)-1)) {
804 reg->floats = MAX_FLOAT_REGISTERS(ct);
805 }
806 //printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
807 if (is_double) {
808 | str d0, [sp, #(reg->off)]
809 reg->off += 8;
810 | str d1, [sp, #(reg->off)]
811 reg->off += 8;
812 } else {
813 | str s0, [sp, #(reg->off)]
814 #ifdef OS_OSX
815 reg->off += 4;
816 #else
817 reg->off += 8;
818 #endif
819 | str s1, [sp, #(reg->off)]
820 #ifdef OS_OSX
821 reg->off += 4;
822 #else
823 reg->off += 8;
824 #endif
825 }
826 }
827 }
828
829 #define add_pointer(jit, ct, reg) add_int(jit, ct, reg, int64)
830 #define get_pointer(jit, ct, reg) get_int(jit, ct, reg, int64)
831
832 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
833 {
834 int i, nargs;
835 cfunction* pf;
836 struct ctype ct2 = *ct;
837 const struct ctype* mt;
838 struct reg_alloc reg;
839 int num_upvals = 0;
840 int top = lua_gettop(L);
841 struct jit* Dst = get_jit(L);
842 int ref;
843 int hidden_arg_off = 0;
844
845 ct_usr = lua_absindex(L, ct_usr);
846 fidx = lua_absindex(L, fidx);
847
848 assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx));
849
850 memset(&reg, 0, sizeof(reg));
851 reg.off = 48;
852
853 dasm_setup(Dst, build_actionlist);
854
855 // add a table to store ctype and function upvalues
856 // callback_set assumes the first value is the lua function
857 nargs = (int) lua_rawlen(L, ct_usr);
858 lua_newtable(L);
859 lua_pushvalue(L, -1);
860 ref = luaL_ref(L, LUA_REGISTRYINDEX);
861
862 if (ct->has_var_arg) {
863 luaL_error(L, "can't create callbacks with varargs");
864 }
865
866 // setup a stack frame to hold args for the call into lua_call
867
868 | sub sp, sp, #64 // = -64
869 | stp x29, x30, [sp, #48] // 16-byte Folded Spill
870 | stp EXTRA_REG_2, EXTRA_REG_3, [sp, #32] // 16-byte Folded Spill
871 | stp rbp, EXTRA_REG_1, [sp, #16] // 16-byte Folded Spill
872 | stp L_ARG, TOP, [sp]
873 | // -64 -56 -48 -40 -32 -24 -16 -8
874 | // stack is -64 (mod 16) (L_ARG, TOP, rbp, EXTRA_REG_1, EXTRA_REG_2, EXTRA_REG_3, x29, x30)
875 | // L_ARG -> -64
876 | // TOP -> -56
877 | // rbp -> -48
878 | // EXTRA_REG_1 -> -40
879 | // EXTRA_REG_2 -> -32
880 | // EXTRA_REG_3 -> -24
881 | // x29 -> -16
882 | // x30 -> -8
883 | //
884 | add rbp, sp, #16
885 |
886
887 | // stack -64 (mod 16) (L_ARG, rbp, rip)
888 | // 16 for return vars, 32 for local calls, rest to save registers
889 | sub sp, sp, #(16 + 32 + REGISTER_STACK_SPACE(ct))
890 | save_registers
891
892 | mov64 L_ARG, L
893
894 /* get the upval table */
895 | mov32 w9, LUA_REGISTRYINDEX
896 | mov32 w10, ref
897 | call_rww extern lua_rawgeti, L_ARG, w9, w10
898
899 /* get the lua function */
900 lua_pushvalue(L, fidx);
901 lua_rawseti(L, -2, ++num_upvals);
902 assert(num_upvals == CALLBACK_FUNC_USR_IDX);
903 | mov32 w9, -1
904 | call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals
905
906 (void) hidden_arg_off;
907
908 for (i = 1; i <= nargs; i++) {
909 lua_rawgeti(L, ct_usr, i);
910 mt = (const struct ctype*) lua_touserdata(L, -1);
911
912 if (mt->pointers || mt->is_reference) {
913 lua_getuservalue(L, -1);
914 lua_rawseti(L, -3, ++num_upvals); /* usr value */
915 lua_rawseti(L, -2, ++num_upvals); /* mt */
916 /* on the lua stack in the callback:
917 * upval tbl, lua func, i-1 args
918 */
919 | mov32 w9, -i-1
920 | call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals-1
921 | mov32 w9, -1
922 | call_rwp extern push_cdata, L_ARG, w9, mt
923 get_pointer(Dst, ct, &reg);
924 | str x9, [x0]
925 | mov32 w9, -2
926 | call_rw, extern lua_remove, L_ARG, w9
927 } else {
928 switch (mt->type) {
929 case INT64_TYPE:
930 lua_getuservalue(L, -1);
931 lua_rawseti(L, -3, ++num_upvals); /* mt */
932 lua_pop(L, 1);
933 | call_rip extern push_cdata, L_ARG, 0, mt
934 get_int(Dst, ct, &reg, int64);
935 | str x9, [x0]
936 break;
937
938 case INTPTR_TYPE:
939 lua_getuservalue(L, -1);
940 lua_rawseti(L, -3, ++num_upvals); /* mt */
941 lua_pop(L, 1);
942 | call_rip extern push_cdata, L_ARG, 0, mt
943 get_pointer(Dst, ct, &reg);
944 | str x9, [x0]
945 break;
946
947 case COMPLEX_FLOAT_TYPE:
948 lua_pop(L, 1);
949 /* complex floats are two floats packed into a double */
950 | call_rip extern push_cdata, L_ARG, 0, mt
951 get_complex_float(Dst, ct, &reg, 0);
952 | str s0, [x0]
953 | str s1, [x0, #4]
954 break;
955
956 case COMPLEX_DOUBLE_TYPE:
957 lua_pop(L, 1);
958 | call_rip extern push_cdata, L_ARG, 0, mt
959 get_complex_float(Dst, ct, &reg, 1);
960 | str d0, [x0]
961 | str d1, [x0, #8]
962 break;
963
964 case FLOAT_TYPE:
965 lua_pop(L, 1);
966 get_float(Dst, ct, &reg, 0);
967 | // for 64bit xmm0 is already set
968 | mov x0, L_ARG
969 | bl extern lua_pushnumber
970 break;
971
972 case DOUBLE_TYPE:
973 lua_pop(L, 1);
974 get_float(Dst, ct, &reg, 1);
975 | // for 64bit xmm0 is already set
976 | mov x0, L_ARG
977 | bl extern lua_pushnumber
978 break;
979
980 case BOOL_TYPE:
981 lua_pop(L, 1);
982 get_int(Dst, ct, &reg, int8);
983 | //movzx ecx, cl
984 | cmp w9, #0
985 | beq >1
986 | mov b9, #1
987 | 1:
988 | ubfm x9, x9, #0, #7
989 | call_rr extern lua_pushboolean, L_ARG, x9
990 break;
991
992 case INT8_TYPE:
993 lua_pop(L, 1);
994 get_int(Dst, ct, &reg, int8);
995 if (mt->is_unsigned) {
996 | ubfm x9, x9, #0, #7
997 } else {
998 | sbfm x9, x9, #0, #7
999 }
1000 | call_rr extern push_int, L_ARG, x9
1001 break;
1002
1003 case INT16_TYPE:
1004 lua_pop(L, 1);
1005 get_int(Dst, ct, &reg, int16);
1006 if (mt->is_unsigned) {
1007 | ubfm x9, x9, #0, #15
1008 } else {
1009 | sbfm x9, x9, #0, #15
1010 }
1011 | call_rr extern push_int, L_ARG, x9
1012 break;
1013
1014 case ENUM_TYPE:
1015 case INT32_TYPE:
1016 lua_pop(L, 1);
1017 get_int(Dst, ct, &reg, int32);
1018 if (mt->is_unsigned) {
1019 | call_rw extern push_uint, L_ARG, w9
1020 } else {
1021 | call_rw extern push_int, L_ARG, w9
1022 }
1023 break;
1024
1025 default:
1026 luaL_error(L, "NYI: callback arg type");
1027 }
1028 }
1029 }
1030
1031 lua_rawgeti(L, ct_usr, 0);
1032 mt = (const struct ctype*) lua_touserdata(L, -1);
1033
1034 | call_riip extern lua_callk, L_ARG, nargs, ((mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0), 0
1035
1036 // Unpack the return argument if not "void", also clean-up the lua stack
1037 // to remove the return argument and bind table. Use lua_settop rather
1038 // than lua_pop as lua_pop is implemented as a macro.
1039 if (mt->pointers || mt->is_reference) {
1040 lua_getuservalue(L, -1);
1041 lua_rawseti(L, -3, ++num_upvals); /* usr value */
1042 lua_rawseti(L, -2, ++num_upvals); /* mt */
1043 | mov32 w9, -2
1044 | call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals-1
1045 | mov32 w9, -2
1046 | mov32 w10, -1
1047 | call_rwwp extern check_typed_pointer, L_ARG, w9, w10, mt
1048 | str x0, [sp, #32]
1049 | mov32 w9, -4
1050 | call_rw extern lua_settop, L_ARG, w9
1051 | ldr x0, [sp, #32]
1052
1053 } else {
1054 switch (mt->type) {
1055 case ENUM_TYPE:
1056 lua_getuservalue(L, -1);
1057 lua_rawseti(L, -3, ++num_upvals); /* usr value */
1058 lua_rawseti(L, -2, ++num_upvals); /* mt */
1059 | mov32 w9, -2
1060 | call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals-1
1061 | mov32 w9, -2
1062 | mov32 w10, -1
1063 | call_rwwp, extern check_enum, L_ARG, w9, w10, mt
1064 | str w0, [sp, #32]
1065 | mov32 w9, -4
1066 | call_rw extern lua_settop, L_ARG, w9
1067 | ldr w0, [sp, #32]
1068 break;
1069
1070 case VOID_TYPE:
1071 lua_pop(L, 1);
1072 | mov32 w9, -2
1073 | call_rw extern lua_settop, L_ARG, w9
1074 break;
1075
1076 case BOOL_TYPE:
1077 case INT8_TYPE:
1078 case INT16_TYPE:
1079 case INT32_TYPE:
1080 lua_pop(L, 1);
1081 if (mt->is_unsigned) {
1082 | mov32 w9, -1
1083 | call_rw extern check_uint32, L_ARG, w9
1084 } else {
1085 | mov32 w9, -1
1086 | call_rw extern check_int32, L_ARG, w9
1087 }
1088 | str w0, [sp, #32]
1089 | mov32 w9, -3
1090 | call_rw extern lua_settop, L_ARG, w9
1091 | ldr w0, [sp, #32]
1092 break;
1093
1094 case INT64_TYPE:
1095 lua_pop(L, 1);
1096
1097 if (mt->is_unsigned) {
1098 | mov32 w9, -1
1099 | call_rw extern check_uint64, L_ARG, w9
1100 } else {
1101 | mov32 w9, -1
1102 | call_rw extern check_int64, L_ARG, w9
1103 }
1104
1105 | str x0, [sp, #32]
1106 | mov32 w9, -4
1107 | call_rw extern lua_settop, L_ARG, w9
1108 | ldr x0, [sp, #32]
1109 break;
1110
1111 case INTPTR_TYPE:
1112 lua_pop(L, 1);
1113 | mov32 w9, -1
1114 | call_rw extern check_uintptr, L_ARG, w9
1115 | str x0, [sp, #32]
1116 | mov32 w9, -3
1117 | call_rw extern lua_settop, L_ARG, w9
1118 | ldr x0, [sp, #32]
1119 break;
1120
1121 case FLOAT_TYPE:
1122 lua_pop(L, 1);
1123 | mov32 w9, -1
1124 | call_rw extern check_double, L_ARG, w9
1125 | str d0, [sp, #32]
1126 | mov32 w9, -3
1127 | call_rw extern lua_settop, L_ARG, w9
1128 | ldr d1, [sp, #32]
1129 | fcvt s0, d1
1130 break;
1131
1132 case DOUBLE_TYPE:
1133 lua_pop(L, 1);
1134 | mov32 w9, -1
1135 | call_rw extern check_double, L_ARG, w9
1136 | str d0, [sp, #32]
1137 | mov32 w9, -3
1138 | call_rw extern lua_settop, L_ARG, w9
1139 | ldr d0, [sp, #32]
1140 break;
1141
1142 case COMPLEX_FLOAT_TYPE:
1143 lua_pop(L, 1);
1144 #if !defined HAVE_COMPLEX
1145 luaL_error(L, "ffi lib compiled without complex number support");
1146 #endif
1147 | mov32 w9, -1
1148 | call_rw extern check_complex_float, L_ARG, w9
1149 |
1150 | str s0, [sp,#32]
1151 | str s1, [sp,#36]
1152 |
1153 | mov32 w9, -3
1154 | call_rw extern lua_settop, L_ARG, w9
1155 |
1156 | ldr s0, [sp,#32]
1157 | ldr s1, [sp,#36]
1158 |
1159 break;
1160
1161 case COMPLEX_DOUBLE_TYPE:
1162 lua_pop(L, 1);
1163 #if !defined HAVE_COMPLEX
1164 luaL_error(L, "ffi lib compiled without complex number support");
1165 #endif
1166 | mov32 w9, -1
1167 | call_rw extern check_complex_double, L_ARG, w9
1168 |
1169 | str d0, [sp,#32]
1170 | str d1, [sp,#40]
1171 |
1172 | mov32 w9, -3
1173 | call_rw extern lua_settop, L_ARG, w9
1174 |
1175 | ldr d0, [sp,#32]
1176 | ldr d1, [sp,#40]
1177 |
1178 break;
1179
1180 default:
1181 luaL_error(L, "NYI: callback return type");
1182 }
1183 }
1184
1185 | epilog
1186 | ret
1187
1188 lua_pop(L, 1); /* upval table - already in registry */
1189 assert(lua_gettop(L) == top);
1190
1191 ct2.is_jitted = 1;
1192 pf = (cfunction*) push_cdata(L, ct_usr, &ct2);
1193 *pf = compile(Dst, L, NULL, ref);
1194
1195 assert(lua_gettop(L) == top + 1);
1196
1197 return *pf;
1198 }
1199
1200 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
1201 {
1202 size_t i, nargs;
1203 int num_upvals;
1204 int orig_top;
1205 const struct ctype* mbr_ct;
1206 struct jit* Dst = get_jit(L);
1207 struct reg_alloc reg;
1208 void* p = NULL;
1209 int top = lua_gettop(L);
1210 int* perr = &Dst->last_errno;
1211
1212 ct_usr = lua_absindex(L, ct_usr);
1213
1214 memset(&reg, 0, sizeof(reg));
1215 reg.off = 32 + REGISTER_STACK_SPACE(ct);
1216
1217 dasm_setup(Dst, build_actionlist);
1218
1219 orig_top = lua_gettop(L);
1220 p = push_cdata(L, ct_usr, ct);
1221 memcpy(p,func, ctype_size(L, ct));
1222
1223 nargs = lua_rawlen(L, ct_usr);
1224 assert(nargs <= ((unsigned int)(0xFFE))); // <= 4094 s.t. 4095 can fit into imm12
1225
1226 if (ct->calling_convention != C_CALL && ct->has_var_arg) {
1227 luaL_error(L, "vararg is only allowed with the c calling convention");
1228 }
1229
1230 | sub sp, sp, #64 // = -64
1231 | stp x29, x30, [sp, #48] // 16-byte Folded Spill
1232 | stp EXTRA_REG_2, EXTRA_REG_3, [sp, #32] // 16-byte Folded Spill
1233 | stp rbp, EXTRA_REG_1, [sp, #16] // 16-byte Folded Spill
1234 | stp L_ARG, TOP, [sp]
1235 | // -64 -56 -48 -40 -32 -24 -16 -8
1236 | // stack is -64 (mod 16) (L_ARG, TOP, rbp, EXTRA_REG_1, EXTRA_REG_2, EXTRA_REG_3, x29, x30)
1237 | // L_ARG -> -64
1238 | // TOP -> -56
1239 | // rbp -> -48
1240 | // EXTRA_REG_1 -> -40
1241 | // EXTRA_REG_2 -> -32
1242 | // EXTRA_REG_3 -> -24
1243 | // x29 -> -16
1244 | // x30 -> -8
1245 | add rbp, sp, #16
1246 |
1247 | // Get L from our arguments and allocate some stack for lua_gettop
1248 | mov L_ARG, x0 // First argument to the lua function int f(lua_State * L);
1249
1250 |
1251 | call_r extern lua_gettop, L_ARG
1252 | mov TOP, x0
1253 | cmp x0, #(nargs)
1254 if (!ct->has_var_arg) {
1255 | bge >2
1256 | too_few_arguments
1257 | 2:
1258 | ble >1
1259 | too_many_arguments
1260 } else {
1261 | bge >1
1262 | too_few_arguments
1263 }
1264
1265 | 1:
1266
1267 | sub sp, sp, TOP, lsl #4 // reserve 16 bytes per argument - this maintains the alignment mod 16
1268 | sub sp, sp, #(32 + REGISTER_STACK_SPACE(ct)) // reserve an extra 32 to call local functions
1269
1270 for (i = 1; i <= nargs; i++) {
1271 lua_rawgeti(L, ct_usr, (int) i);
1272 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1273
1274 if (mbr_ct->pointers || mbr_ct->is_reference) {
1275 lua_getuservalue(L, -1);
1276 num_upvals = lua_gettop(L) - orig_top;
1277 //printf("%s:%d lua_upvalueindex(num_upvals) = [%d] \n", __FILE__, __LINE__, lua_upvalueindex(num_upvals));
1278 | mov32 w9, lua_upvalueindex(num_upvals)
1279 | sxtw x9, w9
1280 | call_riwp extern check_typed_pointer, L_ARG, i, w9, mbr_ct
1281 add_pointer(Dst, ct, &reg);
1282 } else {
1283 switch (mbr_ct->type) {
1284 case FUNCTION_PTR_TYPE:
1285 lua_getuservalue(L, -1);
1286 num_upvals = lua_gettop(L) - orig_top;
1287 | mov32 w9, lua_upvalueindex(num_upvals)
1288 | sxtw x9, w9
1289 | call_riwp extern check_typed_cfunction, L_ARG, i, w9, mbr_ct
1290 add_pointer(Dst, ct, &reg);
1291 break;
1292
1293 case ENUM_TYPE:
1294 lua_getuservalue(L, -1);
1295 num_upvals = lua_gettop(L) - orig_top;
1296 | mov32 w9, lua_upvalueindex(num_upvals)
1297 | sxtw x9, w9
1298 | call_riwp, extern check_enum, L_ARG, i, w9, mbr_ct
1299 add_int(Dst, ct, &reg, int32);
1300 break;
1301
1302 case INT8_TYPE:
1303 //| call_ri extern check_int32, L_ARG, i
1304 if (mbr_ct->is_unsigned) {
1305 //| call_ri extern check_uint8, L_ARG, i
1306 | call_ri extern check_uint32, L_ARG, i
1307 | ubfm x0, x0, #0, #7
1308 } else {
1309 //| call_ri extern check_int8, L_ARG, i
1310 | call_ri extern check_int32, L_ARG, i
1311 | sbfm x0, x0, #0, #7
1312 }
1313 add_int(Dst, ct, &reg, int8);
1314 lua_pop(L, 1);
1315 break;
1316
1317 case INT16_TYPE:
1318 //| call_ri extern check_int32, L_ARG, i
1319 if (mbr_ct->is_unsigned) {
1320 //| call_ri extern check_uint16, L_ARG, i
1321 | call_ri extern check_uint32, L_ARG, i
1322 | ubfm x0, x0, #0, #15
1323 } else {
1324 //| call_ri extern check_int16, L_ARG, i
1325 | call_ri extern check_int32, L_ARG, i
1326 | sbfm x0, x0, #0, #15
1327 }
1328 add_int(Dst, ct, &reg, int16);
1329 lua_pop(L, 1);
1330 break;
1331
1332 case BOOL_TYPE:
1333 | call_ri extern check_int32, L_ARG, i
1334 | cmp x0, #0
1335 | beq >1
1336 | mov b0, #1
1337 | 1:
1338 | ubfm x0, x0, #0, #7
1339 add_int(Dst, ct, &reg, int8);
1340 lua_pop(L, 1);
1341 break;
1342
1343 case INT32_TYPE:
1344 if (mbr_ct->is_unsigned) {
1345 | call_ri extern check_uint32, L_ARG, i
1346 } else {
1347 | call_ri extern check_int32, L_ARG, i
1348 }
1349 add_int(Dst, ct, &reg, int32);
1350 lua_pop(L, 1);
1351 break;
1352
1353 case INTPTR_TYPE:
1354 | call_ri extern check_uintptr, L_ARG, i
1355 add_pointer(Dst, ct, &reg);
1356 lua_pop(L, 1);
1357 break;
1358
1359 case INT64_TYPE:
1360 if (mbr_ct->is_unsigned) {
1361 | call_ri extern check_uint64, L_ARG, i
1362 } else {
1363 | call_ri extern check_int64, L_ARG, i
1364 }
1365 add_int(Dst, ct, &reg, int64);
1366 lua_pop(L, 1);
1367 break;
1368
1369 case DOUBLE_TYPE:
1370 | call_ri extern check_double, L_ARG, i
1371 add_float(Dst, ct, &reg, 1);
1372 lua_pop(L, 1);
1373 break;
1374
1375 case COMPLEX_DOUBLE_TYPE:
1376 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
1377 * there is a hidden first parameter that points to 16 bytes where
1378 * the returned arg is stored (this is popped by the called
1379 * function) */
1380 | call_ri extern check_complex_double, L_ARG, i
1381 add_complex_float(Dst, ct, &reg, 1);
1382 lua_pop(L, 1);
1383 break;
1384
1385 case FLOAT_TYPE:
1386 | call_ri extern check_double, L_ARG, i
1387 add_float(Dst, ct, &reg, 0);
1388 lua_pop(L, 1);
1389 break;
1390
1391 case COMPLEX_FLOAT_TYPE:
1392 /* complex floats are returned as s0 and s1 */
1393 | call_ri extern check_complex_float, L_ARG, i
1394 add_complex_float(Dst, ct, &reg, 0);
1395 lua_pop(L, 1);
1396 break;
1397
1398 default:
1399 luaL_error(L, "NYI: call arg type");
1400 }
1401 }
1402 }
1403
1404 if (ct->has_var_arg) {
1405 #ifndef OS_OSX //{
1406 if (reg.floats < MAX_FLOAT_REGISTERS(ct)) {
1407 | mov x9, sp
1408 | add x9, x9, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg.floats))
1409 | call_ririr extern unpack_varargs_float, L_ARG, (nargs+1), TOP, (MAX_FLOAT_REGISTERS(ct) - reg.floats), x9
1410 }
1411
1412 if (reg.ints < MAX_INT_REGISTERS(ct)) {
1413 | mov x9, sp
1414 | add x9, x9, #(32 + 8*(reg.ints))
1415 | call_ririr extern unpack_varargs_int, L_ARG, (nargs+1), TOP, (MAX_INT_REGISTERS(ct) - reg.ints), x9
1416 }
1417
1418 | mov x9, sp
1419 | add x9, x9, #(reg.off)
1420 | call_ririir extern unpack_varargs_stack_skip, L_ARG, (nargs+1), TOP, (MAX_INT_REGISTERS(ct) - reg.ints), (MAX_FLOAT_REGISTERS(ct) - reg.floats), x9
1421
1422 reg.floats = MAX_FLOAT_REGISTERS(ct);
1423 reg.ints = MAX_INT_REGISTERS(ct);
1424 #else //}{
1425 /* On OSX M1 all variadic acruments on stack are 8 byte aligned */
1426 if (reg.off % 8 != 0) {
1427 reg.off += 8 - (reg.off % 8);
1428 }
1429 | mov x9, sp
1430 | add x9, x9, #(reg.off)
1431 | call_ririir extern unpack_varargs_stack_skip, L_ARG, (nargs+1), TOP, 0, 0, x9
1432
1433 reg.floats = MAX_FLOAT_REGISTERS(ct);
1434 reg.ints = MAX_INT_REGISTERS(ct);
1435 #endif //}
1436 }
1437
1438 | mov64 x9, perr
1439 | mov x10, xzr
1440 | ldr w10, [x9]
1441 | call_w extern SetLastError, w10
1442
1443 /* remove the stack space to call local functions */
1444 | add sp, sp, #(32)
1445
1446 switch (reg.floats) {
1447 case 8:
1448 | ldr d7, [sp, #(8*(MAX_INT_REGISTERS(ct)+7))]
1449 case 7:
1450 | ldr d6, [sp, #(8*(MAX_INT_REGISTERS(ct)+6))]
1451 case 6:
1452 | ldr d5, [sp, #(8*(MAX_INT_REGISTERS(ct)+5))]
1453 case 5:
1454 | ldr d4, [sp, #(8*(MAX_INT_REGISTERS(ct)+4))]
1455 case 4:
1456 | ldr d3, [sp, #(8*(MAX_INT_REGISTERS(ct)+3))]
1457 case 3:
1458 | ldr d2, [sp, #(8*(MAX_INT_REGISTERS(ct)+2))]
1459 case 2:
1460 | ldr d1, [sp, #(8*(MAX_INT_REGISTERS(ct)+1))]
1461 case 1:
1462 | ldr d0, [sp, #(8*(MAX_INT_REGISTERS(ct)))]
1463 case 0:
1464 break;
1465 }
1466
1467 switch (reg.ints) {
1468 case 8:
1469 | ldr x7, [sp, #(8*7)]
1470 case 7:
1471 | ldr x6, [sp, #(8*6)]
1472 case 6:
1473 | ldr x5, [sp, #(8*5)]
1474 case 5:
1475 | ldr x4, [sp, #(8*4)]
1476 case 4:
1477 | ldr x3, [sp, #(8*3)]
1478 case 3:
1479 | ldr x2, [sp, #(8*2)]
1480 case 2:
1481 | ldr x1, [sp, #(8*1)]
1482 case 1:
1483 | ldr x0, [sp]
1484 case 0:
1485 break;
1486 }
1487
1488 | add sp, sp, #(REGISTER_STACK_SPACE(ct))
1489
1490 //printf("%s:%d compiling call \n", __FILE__, __LINE__);
1491 | bl extern FUNCTION
1492 |
1493 | add sp, sp, TOP, lsl #4 // Free up the stack space reserved for FUNCTION call
1494 | sub sp, sp, #48 // 32 to be able to call local functions, 16 so we can store some local variables
1495
1496 lua_rawgeti(L, ct_usr, 0); /* i = 0 is the return type */
1497 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1498
1499 if (mbr_ct->pointers || mbr_ct->is_reference || mbr_ct->type == INTPTR_TYPE) {
1500 lua_getuservalue(L, -1);
1501 num_upvals = lua_gettop(L) - orig_top;
1502 | str x0, [sp, #32] // save the pointer
1503 | get_errno
1504 | mov32 w9, lua_upvalueindex(num_upvals)
1505 | sxtw x9, w9
1506 | call_rwp extern push_cdata, L_ARG, w9, mbr_ct
1507 | ldr x9, [sp, #32]
1508 | str x9, [x0] // *(void**) cdata = val
1509 | lua_return_arg
1510 } else {
1511 switch (mbr_ct->type) {
1512 case FUNCTION_PTR_TYPE:
1513 lua_getuservalue(L, -1);
1514 num_upvals = lua_gettop(L) - orig_top;
1515 | str x0, [sp, #32] // save the function pointer
1516 | get_errno
1517 | mov32 w9, lua_upvalueindex(num_upvals)
1518 | sxtw x9, w9
1519 | call_rwp extern push_cdata, L_ARG, w9, mbr_ct
1520 | ldr x9, [sp, #32]
1521 | str x9, [x0] // *(void**) cdata = val
1522 | lua_return_arg
1523 break;
1524
1525 case INT64_TYPE:
1526 lua_pop(L, 1);
1527 if (mbr_ct->is_unsigned) {
1528 | lua_return_ulong
1529 } else {
1530 | lua_return_long
1531 }
1532 break;
1533
1534 case COMPLEX_FLOAT_TYPE:
1535 lua_getuservalue(L, -1);
1536 num_upvals = lua_gettop(L) - orig_top;
1537 | // complex floats are returned as two floats packed into s0 and s1
1538 | str s0, [sp, #40]
1539 | str s1, [sp, #44]
1540 |
1541 | get_errno
1542 | mov32 w9, lua_upvalueindex(num_upvals)
1543 | sxtw x9, w9
1544 | call_rwp extern push_cdata, L_ARG, w9, mbr_ct
1545 |
1546 | // ((complex_float*) cdata) = val
1547 | ldr s0, [sp, #40]
1548 | ldr s1, [sp, #44]
1549 | str s0, [x0]
1550 | str s1, [x0, #4]
1551 |
1552 | lua_return_arg
1553 break;
1554
1555 case COMPLEX_DOUBLE_TYPE:
1556 lua_getuservalue(L, -1);
1557 num_upvals = lua_gettop(L) - orig_top;
1558 | // complex doubles are returned as d0 and d1
1559 | str d0, [sp, #32]
1560 | str d1, [sp, #40]
1561 |
1562 | get_errno
1563 | mov32 w9, lua_upvalueindex(num_upvals)
1564 | sxtw x9, w9
1565 | call_rwp extern push_cdata, L_ARG, w9, mbr_ct
1566 |
1567 | // ((complex_float*) cdata) = val
1568 | ldr d0, [sp, #32]
1569 | ldr d1, [sp, #40]
1570 | str d0, [x0]
1571 | str d1, [x0, #8]
1572 |
1573 | lua_return_arg
1574 break;
1575
1576 case VOID_TYPE:
1577 lua_pop(L, 1);
1578 | lua_return_void
1579 break;
1580
1581 case BOOL_TYPE:
1582 lua_pop(L, 1);
1583 | lua_return_bool
1584 break;
1585
1586 case INT8_TYPE:
1587 lua_pop(L, 1);
1588 if (mbr_ct->is_unsigned) {
1589 | // movzx eax, al
1590 | ubfm x0, x0, #0, #7
1591 } else {
1592 | //movsx eax, al
1593 | sbfm x0, x0, #0, #7
1594 }
1595 | lua_return_int
1596 break;
1597
1598 case INT16_TYPE:
1599 lua_pop(L, 1);
1600 if (mbr_ct->is_unsigned) {
1601 | //movzx eax, ax
1602 | ubfm x0, x0, #0, #15
1603 } else {
1604 | //movsx eax, ax
1605 | sbfm x0, x0, #0, #15
1606 }
1607 | lua_return_int
1608 break;
1609
1610 case INT32_TYPE:
1611 case ENUM_TYPE:
1612 lua_pop(L, 1);
1613 if (mbr_ct->is_unsigned) {
1614 | lua_return_uint
1615 } else {
1616 | lua_return_int
1617 }
1618 break;
1619
1620 case FLOAT_TYPE:
1621 lua_pop(L, 1);
1622 | fcvt d0, s0
1623 | lua_return_double
1624 break;
1625
1626 case DOUBLE_TYPE:
1627 lua_pop(L, 1);
1628 | lua_return_double
1629 break;
1630
1631 default:
1632 luaL_error(L, "NYI: call return type");
1633 }
1634 }
1635
1636 {
1637 cfunction f = compile(Dst, L, func, LUA_NOREF);
1638 /* add a callback as an upval so that the jitted code gets cleaned up when
1639 * the function gets gc'd */
1640 push_callback(L, f, func);
1641 num_upvals = lua_gettop(L) - orig_top;
1642 lua_pushcclosure(L, (lua_CFunction) f, num_upvals);
1643 }
1644 }
1645