Debian Codegraph @

src/arm/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2011 Timothy Wall
3 Copyright (c) 2011 Plausible Labs Cooperative, Inc.
4 Copyright (c) 2011 Anthony Green
5 Copyright (c) 2011 Free Software Foundation
6 Copyright (c) 1998, 2008, 2011 Red Hat, Inc.
7
8 ARM Foreign Function Interface
9
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 ``Software''), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 ----------------------------------------------------------------------- */
30
31 #if defined(__arm__) || defined(_M_ARM)
32 #include <fficonfig.h>
33 #include <ffi.h>
34 #include <ffi_common.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <tramp.h>
38 #include "internal.h"
39
40 #if defined(_WIN32)
41 #define WIN32_LEAN_AND_MEAN
42 #include <windows.h>
43 #endif
44
45 #if FFI_EXEC_TRAMPOLINE_TABLE
46
47 #ifdef __MACH__
48 #include <mach/machine/vm_param.h>
49 #endif
50
51 #else
52 #ifndef _WIN32
53 extern unsigned int ffi_arm_trampoline[2] FFI_HIDDEN;
54 #else
55 // Declare this as an array of char, instead of array of int,
56 // otherwise Clang optimizes out the "& 0xFFFFFFFE" for clearing
57 // the thumb bit.
58 extern unsigned char ffi_arm_trampoline[12] FFI_HIDDEN;
59 #endif
60 #endif
61
62 #if defined(__FreeBSD__) && defined(__arm__)
63 #include <sys/types.h>
64 #include <machine/sysarch.h>
65 #endif
66
67 #if defined(__QNX__)
68 #include <sys/mman.h>
69 #endif
70
71 /* Forward declares. */
72 static int vfp_type_p (const ffi_type *);
73 static void layout_vfp_args (ffi_cif *);
74
75 static void *
76 ffi_align (ffi_type *ty, void *p)
77 {
78 /* Align if necessary */
79 size_t alignment;
80 #ifdef _WIN32_WCE
81 alignment = 4;
82 #else
83 alignment = ty->alignment;
84 if (alignment < 4)
85 alignment = 4;
86 #endif
87 return (void *) FFI_ALIGN (p, alignment);
88 }
89
90 static size_t
91 ffi_put_arg (ffi_type *ty, void *src, void *dst)
92 {
93 size_t z = ty->size;
94
95 switch (ty->type)
96 {
97 case FFI_TYPE_SINT8:
98 *(UINT32 *)dst = *(SINT8 *)src;
99 break;
100 case FFI_TYPE_UINT8:
101 *(UINT32 *)dst = *(UINT8 *)src;
102 break;
103 case FFI_TYPE_SINT16:
104 *(UINT32 *)dst = *(SINT16 *)src;
105 break;
106 case FFI_TYPE_UINT16:
107 *(UINT32 *)dst = *(UINT16 *)src;
108 break;
109
110 case FFI_TYPE_INT:
111 case FFI_TYPE_SINT32:
112 case FFI_TYPE_UINT32:
113 case FFI_TYPE_POINTER:
114 #ifndef _WIN32
115 case FFI_TYPE_FLOAT:
116 #endif
117 *(UINT32 *)dst = *(UINT32 *)src;
118 break;
119
120 #ifdef _WIN32
121 // casting a float* to a UINT32* doesn't work on Windows
122 case FFI_TYPE_FLOAT:
123 *(uintptr_t *)dst = 0;
124 *(float *)dst = *(float *)src;
125 break;
126 #endif
127
128 case FFI_TYPE_SINT64:
129 case FFI_TYPE_UINT64:
130 case FFI_TYPE_DOUBLE:
131 *(UINT64 *)dst = *(UINT64 *)src;
132 break;
133
134 case FFI_TYPE_STRUCT:
135 case FFI_TYPE_COMPLEX:
136 memcpy (dst, src, z);
137 break;
138
139 default:
140 abort();
141 }
142
143 return FFI_ALIGN (z, 4);
144 }
145
146 /* ffi_prep_args is called once stack space has been allocated
147 for the function's arguments.
148
149 The vfp_space parameter is the load area for VFP regs, the return
150 value is cif->vfp_used (word bitset of VFP regs used for passing
151 arguments). These are only used for the VFP hard-float ABI.
152 */
153 static void
154 ffi_prep_args_SYSV (ffi_cif *cif, int flags, void *rvalue,
155 void **avalue, char *argp)
156 {
157 ffi_type **arg_types = cif->arg_types;
158 int i, n;
159
160 if (flags == ARM_TYPE_STRUCT)
161 {
162 *(void **) argp = rvalue;
163 argp += 4;
164 }
165
166 for (i = 0, n = cif->nargs; i < n; i++)
167 {
168 ffi_type *ty = arg_types[i];
169 argp = ffi_align (ty, argp);
170 argp += ffi_put_arg (ty, avalue[i], argp);
171 }
172 }
173
174 static void
175 ffi_prep_args_VFP (ffi_cif *cif, int flags, void *rvalue,
176 void **avalue, char *stack, char *vfp_space)
177 {
178 ffi_type **arg_types = cif->arg_types;
179 int i, n, vi = 0;
180 char *argp, *regp, *eo_regp;
181 char stack_used = 0;
182 char done_with_regs = 0;
183
184 /* The first 4 words on the stack are used for values
185 passed in core registers. */
186 regp = stack;
187 eo_regp = argp = regp + 16;
188
189 /* If the function returns an FFI_TYPE_STRUCT in memory,
190 that address is passed in r0 to the function. */
191 if (flags == ARM_TYPE_STRUCT)
192 {
193 *(void **) regp = rvalue;
194 regp += 4;
195 }
196
197 for (i = 0, n = cif->nargs; i < n; i++)
198 {
199 ffi_type *ty = arg_types[i];
200 void *a = avalue[i];
201 int is_vfp_type = vfp_type_p (ty);
202
203 /* Allocated in VFP registers. */
204 if (vi < cif->vfp_nargs && is_vfp_type)
205 {
206 char *vfp_slot = vfp_space + cif->vfp_args[vi++] * 4;
207 ffi_put_arg (ty, a, vfp_slot);
208 continue;
209 }
210 /* Try allocating in core registers. */
211 else if (!done_with_regs && !is_vfp_type)
212 {
213 char *tregp = ffi_align (ty, regp);
214 size_t size = ty->size;
215 size = (size < 4) ? 4 : size; // pad
216 /* Check if there is space left in the aligned register
217 area to place the argument. */
218 if (tregp + size <= eo_regp)
219 {
220 regp = tregp + ffi_put_arg (ty, a, tregp);
221 done_with_regs = (regp == argp);
222 // ensure we did not write into the stack area
223 FFI_ASSERT (regp <= argp);
224 continue;
225 }
226 /* In case there are no arguments in the stack area yet,
227 the argument is passed in the remaining core registers
228 and on the stack. */
229 else if (!stack_used)
230 {
231 stack_used = 1;
232 done_with_regs = 1;
233 argp = tregp + ffi_put_arg (ty, a, tregp);
234 FFI_ASSERT (eo_regp < argp);
235 continue;
236 }
237 }
238 /* Base case, arguments are passed on the stack */
239 stack_used = 1;
240 argp = ffi_align (ty, argp);
241 argp += ffi_put_arg (ty, a, argp);
242 }
243 }
244
245 /* Perform machine dependent cif processing */
246 ffi_status FFI_HIDDEN
247 ffi_prep_cif_machdep (ffi_cif *cif)
248 {
249 int flags = 0, cabi = cif->abi;
250 size_t bytes = cif->bytes;
251
252 /* Map out the register placements of VFP register args. The VFP
253 hard-float calling conventions are slightly more sophisticated
254 than the base calling conventions, so we do it here instead of
255 in ffi_prep_args(). */
256 if (cabi == FFI_VFP)
257 layout_vfp_args (cif);
258
259 /* Set the return type flag */
260 switch (cif->rtype->type)
261 {
262 case FFI_TYPE_VOID:
263 flags = ARM_TYPE_VOID;
264 break;
265
266 case FFI_TYPE_INT:
267 case FFI_TYPE_UINT8:
268 case FFI_TYPE_SINT8:
269 case FFI_TYPE_UINT16:
270 case FFI_TYPE_SINT16:
271 case FFI_TYPE_UINT32:
272 case FFI_TYPE_SINT32:
273 case FFI_TYPE_POINTER:
274 flags = ARM_TYPE_INT;
275 break;
276
277 case FFI_TYPE_SINT64:
278 case FFI_TYPE_UINT64:
279 flags = ARM_TYPE_INT64;
280 break;
281
282 case FFI_TYPE_FLOAT:
283 flags = (cabi == FFI_VFP ? ARM_TYPE_VFP_S : ARM_TYPE_INT);
284 break;
285 case FFI_TYPE_DOUBLE:
286 flags = (cabi == FFI_VFP ? ARM_TYPE_VFP_D : ARM_TYPE_INT64);
287 break;
288
289 case FFI_TYPE_STRUCT:
290 case FFI_TYPE_COMPLEX:
291 if (cabi == FFI_VFP)
292 {
293 int h = vfp_type_p (cif->rtype);
294
295 flags = ARM_TYPE_VFP_N;
296 if (h == 0x100 + FFI_TYPE_FLOAT)
297 flags = ARM_TYPE_VFP_S;
298 if (h == 0x100 + FFI_TYPE_DOUBLE)
299 flags = ARM_TYPE_VFP_D;
300 if (h != 0)
301 break;
302 }
303
304 /* A Composite Type not larger than 4 bytes is returned in r0.
305 A Composite Type larger than 4 bytes, or whose size cannot
306 be determined statically ... is stored in memory at an
307 address passed [in r0]. */
308 if (cif->rtype->size <= 4)
309 flags = ARM_TYPE_INT;
310 else
311 {
312 flags = ARM_TYPE_STRUCT;
313 bytes += 4;
314 }
315 break;
316
317 default:
318 abort();
319 }
320
321 /* Round the stack up to a multiple of 8 bytes. This isn't needed
322 everywhere, but it is on some platforms, and it doesn't harm anything
323 when it isn't needed. */
324 bytes = FFI_ALIGN (bytes, 8);
325
326 /* Minimum stack space is the 4 register arguments that we pop. */
327 if (bytes < 4*4)
328 bytes = 4*4;
329
330 cif->bytes = bytes;
331 cif->flags = flags;
332
333 return FFI_OK;
334 }
335
336 /* Perform machine dependent cif processing for variadic calls */
337 ffi_status FFI_HIDDEN
338 ffi_prep_cif_machdep_var (ffi_cif * cif,
339 unsigned int nfixedargs, unsigned int ntotalargs)
340 {
341 /* VFP variadic calls actually use the SYSV ABI */
342 if (cif->abi == FFI_VFP)
343 cif->abi = FFI_SYSV;
344
345 return ffi_prep_cif_machdep (cif);
346 }
347
348 /* Prototypes for assembly functions, in sysv.S. */
349
350 struct call_frame
351 {
352 void *fp;
353 void *lr;
354 void *rvalue;
355 int flags;
356 void *closure;
357 };
358
359 extern void ffi_call_SYSV (void *stack, struct call_frame *,
360 void (*fn) (void)) FFI_HIDDEN;
361 extern void ffi_call_VFP (void *vfp_space, struct call_frame *,
362 void (*fn) (void), unsigned vfp_used) FFI_HIDDEN;
363
364 static void
365 ffi_call_int (ffi_cif * cif, void (*fn) (void), void *rvalue,
366 void **avalue, void *closure)
367 {
368 int flags = cif->flags;
369 ffi_type *rtype = cif->rtype;
370 size_t bytes, rsize, vfp_size;
371 char *stack, *vfp_space, *new_rvalue;
372 struct call_frame *frame;
373
374 rsize = 0;
375 if (rvalue == NULL)
376 {
377 /* If the return value is a struct and we don't have a return
378 value address then we need to make one. Otherwise the return
379 value is in registers and we can ignore them. */
380 if (flags == ARM_TYPE_STRUCT)
381 rsize = rtype->size;
382 else
383 flags = ARM_TYPE_VOID;
384 }
385 else if (flags == ARM_TYPE_VFP_N)
386 {
387 /* Largest case is double x 4. */
388 rsize = 32;
389 }
390 else if (flags == ARM_TYPE_INT && rtype->type == FFI_TYPE_STRUCT)
391 rsize = 4;
392
393 /* Largest case. */
394 vfp_size = (cif->abi == FFI_VFP && cif->vfp_used ? 8*8: 0);
395
396 bytes = cif->bytes;
397 stack = alloca (vfp_size + bytes + sizeof(struct call_frame) + rsize);
398
399 vfp_space = NULL;
400 if (vfp_size)
401 {
402 vfp_space = stack;
403 stack += vfp_size;
404 }
405
406 frame = (struct call_frame *)(stack + bytes);
407
408 new_rvalue = rvalue;
409 if (rsize)
410 new_rvalue = (void *)(frame + 1);
411
412 frame->rvalue = new_rvalue;
413 frame->flags = flags;
414 frame->closure = closure;
415
416 if (vfp_space)
417 {
418 ffi_prep_args_VFP (cif, flags, new_rvalue, avalue, stack, vfp_space);
419 ffi_call_VFP (vfp_space, frame, fn, cif->vfp_used);
420 }
421 else
422 {
423 ffi_prep_args_SYSV (cif, flags, new_rvalue, avalue, stack);
424 ffi_call_SYSV (stack, frame, fn);
425 }
426
427 if (rvalue && rvalue != new_rvalue)
428 memcpy (rvalue, new_rvalue, rtype->size);
429 }
430
431 void
432 ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue)
433 {
434 ffi_call_int (cif, fn, rvalue, avalue, NULL);
435 }
436
437 #ifdef FFI_GO_CLOSURES
438 void
439 ffi_call_go (ffi_cif *cif, void (*fn) (void), void *rvalue,
440 void **avalue, void *closure)
441 {
442 ffi_call_int (cif, fn, rvalue, avalue, closure);
443 }
444 #endif
445
446 static void *
447 ffi_prep_incoming_args_SYSV (ffi_cif *cif, void *rvalue,
448 char *argp, void **avalue)
449 {
450 ffi_type **arg_types = cif->arg_types;
451 int i, n;
452
453 if (cif->flags == ARM_TYPE_STRUCT)
454 {
455 rvalue = *(void **) argp;
456 argp += 4;
457 }
458 else
459 {
460 if (cif->rtype->size && cif->rtype->size < 4)
461 *(uint32_t *) rvalue = 0;
462 }
463
464 for (i = 0, n = cif->nargs; i < n; i++)
465 {
466 ffi_type *ty = arg_types[i];
467 size_t z = ty->size;
468
469 argp = ffi_align (ty, argp);
470 avalue[i] = (void *) argp;
471 argp += z;
472 }
473
474 return rvalue;
475 }
476
477 static void *
478 ffi_prep_incoming_args_VFP (ffi_cif *cif, void *rvalue, char *stack,
479 char *vfp_space, void **avalue)
480 {
481 ffi_type **arg_types = cif->arg_types;
482 int i, n, vi = 0;
483 char *argp, *regp, *eo_regp;
484 char done_with_regs = 0;
485 char stack_used = 0;
486
487 regp = stack;
488 eo_regp = argp = regp + 16;
489
490 if (cif->flags == ARM_TYPE_STRUCT)
491 {
492 rvalue = *(void **) regp;
493 regp += 4;
494 }
495
496 for (i = 0, n = cif->nargs; i < n; i++)
497 {
498 ffi_type *ty = arg_types[i];
499 int is_vfp_type = vfp_type_p (ty);
500 size_t z = ty->size;
501
502 if (vi < cif->vfp_nargs && is_vfp_type)
503 {
504 avalue[i] = vfp_space + cif->vfp_args[vi++] * 4;
505 continue;
506 }
507 else if (!done_with_regs && !is_vfp_type)
508 {
509 char *tregp = ffi_align (ty, regp);
510
511 z = (z < 4) ? 4 : z; // pad
512
513 /* If the arguments either fits into the registers or uses registers
514 and stack, while we haven't read other things from the stack */
515 if (tregp + z <= eo_regp || !stack_used)
516 {
517 /* Because we're little endian, this is what it turns into. */
518 avalue[i] = (void *) tregp;
519 regp = tregp + z;
520
521 /* If we read past the last core register, make sure we
522 have not read from the stack before and continue
523 reading after regp. */
524 if (regp > eo_regp)
525 {
526 FFI_ASSERT (!stack_used);
527 argp = regp;
528 }
529 if (regp >= eo_regp)
530 {
531 done_with_regs = 1;
532 stack_used = 1;
533 }
534 continue;
535 }
536 }
537
538 stack_used = 1;
539 argp = ffi_align (ty, argp);
540 avalue[i] = (void *) argp;
541 argp += z;
542 }
543
544 return rvalue;
545 }
546
547 #if FFI_CLOSURES
548
549 struct closure_frame
550 {
551 char vfp_space[8*8] __attribute__((aligned(8)));
552 char result[8*4];
553 char argp[];
554 };
555
556 int FFI_HIDDEN
557 ffi_closure_inner_SYSV (ffi_cif *cif,
558 void (*fun) (ffi_cif *, void *, void **, void *),
559 void *user_data,
560 struct closure_frame *frame)
561 {
562 void **avalue = (void **) alloca (cif->nargs * sizeof (void *));
563 void *rvalue = ffi_prep_incoming_args_SYSV (cif, frame->result,
564 frame->argp, avalue);
565 fun (cif, rvalue, avalue, user_data);
566 return cif->flags;
567 }
568
569 int FFI_HIDDEN
570 ffi_closure_inner_VFP (ffi_cif *cif,
571 void (*fun) (ffi_cif *, void *, void **, void *),
572 void *user_data,
573 struct closure_frame *frame)
574 {
575 void **avalue = (void **) alloca (cif->nargs * sizeof (void *));
576 void *rvalue = ffi_prep_incoming_args_VFP (cif, frame->result, frame->argp,
577 frame->vfp_space, avalue);
578 fun (cif, rvalue, avalue, user_data);
579 return cif->flags;
580 }
581
582 void ffi_closure_SYSV (void) FFI_HIDDEN;
583 void ffi_closure_VFP (void) FFI_HIDDEN;
584 #if defined(FFI_EXEC_STATIC_TRAMP)
585 void ffi_closure_SYSV_alt (void) FFI_HIDDEN;
586 void ffi_closure_VFP_alt (void) FFI_HIDDEN;
587 #endif
588
589 #ifdef FFI_GO_CLOSURES
590 void ffi_go_closure_SYSV (void) FFI_HIDDEN;
591 void ffi_go_closure_VFP (void) FFI_HIDDEN;
592 #endif
593
594 /* the cif must already be prep'ed */
595
596 #if defined(__FreeBSD__) && defined(__arm__)
597 #define __clear_cache(start, end) do { \
598 struct arm_sync_icache_args ua; \
599 \
600 ua.addr = (uintptr_t)(start); \
601 ua.len = (char *)(end) - (char *)start; \
602 sysarch(ARM_SYNC_ICACHE, &ua); \
603 } while (0);
604 #endif
605
606 ffi_status
607 ffi_prep_closure_loc (ffi_closure * closure,
608 ffi_cif * cif,
609 void (*fun) (ffi_cif *, void *, void **, void *),
610 void *user_data, void *codeloc)
611 {
612 void (*closure_func) (void) = ffi_closure_SYSV;
613
614 if (cif->abi == FFI_VFP)
615 {
616 /* We only need take the vfp path if there are vfp arguments. */
617 if (cif->vfp_used)
618 closure_func = ffi_closure_VFP;
619 }
620 else if (cif->abi != FFI_SYSV)
621 return FFI_BAD_ABI;
622
623 #if FFI_EXEC_TRAMPOLINE_TABLE
624 void **config = (void **)((uint8_t *)codeloc - PAGE_MAX_SIZE);
625 config[0] = closure;
626 config[1] = closure_func;
627 #else
628
629 # if defined(FFI_EXEC_STATIC_TRAMP)
630 if (ffi_tramp_is_present(closure))
631 {
632 /* Initialize the static trampoline's parameters. */
633 if (closure_func == ffi_closure_SYSV)
634 closure_func = ffi_closure_SYSV_alt;
635 else
636 closure_func = ffi_closure_VFP_alt;
637 ffi_tramp_set_parms (closure->ftramp, closure_func, closure);
638 goto out;
639 }
640 # endif
641
642 /* Initialize the dynamic trampoline. */
643 # ifndef _WIN32
644 memcpy(closure->tramp, ffi_arm_trampoline, 8);
645 # else
646 // cast away function type so MSVC doesn't set the lower bit of the function pointer
647 memcpy(closure->tramp, (void*)((uintptr_t)ffi_arm_trampoline & 0xFFFFFFFE), FFI_TRAMPOLINE_CLOSURE_OFFSET);
648 # endif
649
650 # if defined(__QNX__)
651 msync (closure->tramp, 8, MS_INVALIDATE_ICACHE); /* clear data map */
652 msync (codeloc, 8, MS_INVALIDATE_ICACHE); /* clear insn map */
653 # elif defined(_WIN32)
654 FlushInstructionCache(GetCurrentProcess(), closure->tramp, FFI_TRAMPOLINE_SIZE);
655 # else
656 __clear_cache(closure->tramp, closure->tramp + 8); /* clear data map */
657 __clear_cache(codeloc, codeloc + 8); /* clear insn map */
658 # endif
659 # ifdef _WIN32
660 *(void(**)(void))(closure->tramp + FFI_TRAMPOLINE_CLOSURE_FUNCTION) = closure_func;
661 # else
662 *(void (**)(void))(closure->tramp + 8) = closure_func;
663 # endif
664 # if defined(FFI_EXEC_STATIC_TRAMP)
665 out:
666 # endif
667 #endif
668
669 closure->cif = cif;
670 closure->fun = fun;
671 closure->user_data = user_data;
672
673 return FFI_OK;
674 }
675
676 #ifdef FFI_GO_CLOSURES
677 ffi_status
678 ffi_prep_go_closure (ffi_go_closure *closure, ffi_cif *cif,
679 void (*fun) (ffi_cif *, void *, void **, void *))
680 {
681 void (*closure_func) (void) = ffi_go_closure_SYSV;
682
683 if (cif->abi == FFI_VFP)
684 {
685 /* We only need take the vfp path if there are vfp arguments. */
686 if (cif->vfp_used)
687 closure_func = ffi_go_closure_VFP;
688 }
689 else if (cif->abi != FFI_SYSV)
690 return FFI_BAD_ABI;
691
692 closure->tramp = closure_func;
693 closure->cif = cif;
694 closure->fun = fun;
695
696 return FFI_OK;
697 }
698 #endif
699
700 #endif /* FFI_CLOSURES */
701
702 /* Below are routines for VFP hard-float support. */
703
704 /* A subroutine of vfp_type_p. Given a structure type, return the type code
705 of the first non-structure element. Recurse for structure elements.
706 Return -1 if the structure is in fact empty, i.e. no nested elements. */
707
708 static int
709 is_hfa0 (const ffi_type *ty)
710 {
711 ffi_type **elements = ty->elements;
712 int i, ret = -1;
713
714 if (elements != NULL)
715 for (i = 0; elements[i]; ++i)
716 {
717 ret = elements[i]->type;
718 if (ret == FFI_TYPE_STRUCT || ret == FFI_TYPE_COMPLEX)
719 {
720 ret = is_hfa0 (elements[i]);
721 if (ret < 0)
722 continue;
723 }
724 break;
725 }
726
727 return ret;
728 }
729
730 /* A subroutine of vfp_type_p. Given a structure type, return true if all
731 of the non-structure elements are the same as CANDIDATE. */
732
733 static int
734 is_hfa1 (const ffi_type *ty, int candidate)
735 {
736 ffi_type **elements = ty->elements;
737 int i;
738
739 if (elements != NULL)
740 for (i = 0; elements[i]; ++i)
741 {
742 int t = elements[i]->type;
743 if (t == FFI_TYPE_STRUCT || t == FFI_TYPE_COMPLEX)
744 {
745 if (!is_hfa1 (elements[i], candidate))
746 return 0;
747 }
748 else if (t != candidate)
749 return 0;
750 }
751
752 return 1;
753 }
754
755 /* Determine if TY is an homogenous floating point aggregate (HFA).
756 That is, a structure consisting of 1 to 4 members of all the same type,
757 where that type is a floating point scalar.
758
759 Returns non-zero iff TY is an HFA. The result is an encoded value where
760 bits 0-7 contain the type code, and bits 8-10 contain the element count. */
761
762 static int
763 vfp_type_p (const ffi_type *ty)
764 {
765 ffi_type **elements;
766 int candidate, i;
767 size_t size, ele_count;
768
769 /* Quickest tests first. */
770 candidate = ty->type;
771 switch (ty->type)
772 {
773 default:
774 return 0;
775 case FFI_TYPE_FLOAT:
776 case FFI_TYPE_DOUBLE:
777 ele_count = 1;
778 goto done;
779 case FFI_TYPE_COMPLEX:
780 candidate = ty->elements[0]->type;
781 if (candidate != FFI_TYPE_FLOAT && candidate != FFI_TYPE_DOUBLE)
782 return 0;
783 ele_count = 2;
784 goto done;
785 case FFI_TYPE_STRUCT:
786 break;
787 }
788
789 /* No HFA types are smaller than 4 bytes, or larger than 32 bytes. */
790 size = ty->size;
791 if (size < 4 || size > 32)
792 return 0;
793
794 /* Find the type of the first non-structure member. */
795 elements = ty->elements;
796 candidate = elements[0]->type;
797 if (candidate == FFI_TYPE_STRUCT || candidate == FFI_TYPE_COMPLEX)
798 {
799 for (i = 0; ; ++i)
800 {
801 candidate = is_hfa0 (elements[i]);
802 if (candidate >= 0)
803 break;
804 }
805 }
806
807 /* If the first member is not a floating point type, it's not an HFA.
808 Also quickly re-check the size of the structure. */
809 switch (candidate)
810 {
811 case FFI_TYPE_FLOAT:
812 ele_count = size / sizeof(float);
813 if (size != ele_count * sizeof(float))
814 return 0;
815 break;
816 case FFI_TYPE_DOUBLE:
817 ele_count = size / sizeof(double);
818 if (size != ele_count * sizeof(double))
819 return 0;
820 break;
821 default:
822 return 0;
823 }
824 if (ele_count > 4)
825 return 0;
826
827 /* Finally, make sure that all scalar elements are the same type. */
828 for (i = 0; elements[i]; ++i)
829 {
830 int t = elements[i]->type;
831 if (t == FFI_TYPE_STRUCT || t == FFI_TYPE_COMPLEX)
832 {
833 if (!is_hfa1 (elements[i], candidate))
834 return 0;
835 }
836 else if (t != candidate)
837 return 0;
838 }
839
840 /* All tests succeeded. Encode the result. */
841 done:
842 return (ele_count << 8) | candidate;
843 }
844
845 static int
846 place_vfp_arg (ffi_cif *cif, int h)
847 {
848 unsigned short reg = cif->vfp_reg_free;
849 int align = 1, nregs = h >> 8;
850
851 if ((h & 0xff) == FFI_TYPE_DOUBLE)
852 align = 2, nregs *= 2;
853
854 /* Align register number. */
855 if ((reg & 1) && align == 2)
856 reg++;
857
858 while (reg + nregs <= 16)
859 {
860 int s, new_used = 0;
861 for (s = reg; s < reg + nregs; s++)
862 {
863 new_used |= (1 << s);
864 if (cif->vfp_used & (1 << s))
865 {
866 reg += align;
867 goto next_reg;
868 }
869 }
870 /* Found regs to allocate. */
871 cif->vfp_used |= new_used;
872 cif->vfp_args[cif->vfp_nargs++] = (signed char)reg;
873
874 /* Update vfp_reg_free. */
875 if (cif->vfp_used & (1 << cif->vfp_reg_free))
876 {
877 reg += nregs;
878 while (cif->vfp_used & (1 << reg))
879 reg += 1;
880 cif->vfp_reg_free = reg;
881 }
882 return 0;
883 next_reg:;
884 }
885 // done, mark all regs as used
886 cif->vfp_reg_free = 16;
887 cif->vfp_used = 0xFFFF;
888 return 1;
889 }
890
891 static void
892 layout_vfp_args (ffi_cif * cif)
893 {
894 unsigned int i;
895 /* Init VFP fields */
896 cif->vfp_used = 0;
897 cif->vfp_nargs = 0;
898 cif->vfp_reg_free = 0;
899 memset (cif->vfp_args, -1, 16); /* Init to -1. */
900
901 for (i = 0; i < cif->nargs; i++)
902 {
903 int h = vfp_type_p (cif->arg_types[i]);
904 if (h && place_vfp_arg (cif, h) == 1)
905 break;
906 }
907 }
908
909 #if defined(FFI_EXEC_STATIC_TRAMP)
910 void *
911 ffi_tramp_arch (size_t *tramp_size, size_t *map_size)
912 {
913 extern void *trampoline_code_table;
914
915 *tramp_size = ARM_TRAMP_SIZE;
916 *map_size = ARM_TRAMP_MAP_SIZE;
917 return &trampoline_code_table;
918 }
919 #endif
920
921 #endif /* __arm__ or _M_ARM */