Debian Codegraph @

src/mips/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2011 Anthony Green
3 Copyright (c) 2008 David Daney
4 Copyright (c) 1996, 2007, 2008, 2011 Red Hat, Inc.
5
6 MIPS Foreign Function Interface
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
28
29 #include <ffi.h>
30 #include <ffi_common.h>
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 #ifdef __GNUC__
37 # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
38 # define USE__BUILTIN___CLEAR_CACHE 1
39 # endif
40 #endif
41
42 #ifndef USE__BUILTIN___CLEAR_CACHE
43 # if defined(__FreeBSD__)
44 # include <machine/sysarch.h>
45 # elif defined(__OpenBSD__)
46 # include <mips64/sysarch.h>
47 # else
48 # include <sys/cachectl.h>
49 # endif
50 #endif
51
52 #ifdef FFI_DEBUG
53 # define FFI_MIPS_STOP_HERE() ffi_stop_here()
54 #else
55 # define FFI_MIPS_STOP_HERE() do {} while(0)
56 #endif
57
58 #ifdef FFI_MIPS_N32
59 #define FIX_ARGP \
60 FFI_ASSERT(argp <= &stack[bytes]); \
61 if (argp == &stack[bytes]) \
62 { \
63 argp = stack; \
64 FFI_MIPS_STOP_HERE(); \
65 }
66 #else
67 #define FIX_ARGP
68 #endif
69
70
71 /* ffi_prep_args is called by the assembly routine once stack space
72 has been allocated for the function's arguments */
73
74 static void ffi_prep_args(char *stack,
75 extended_cif *ecif,
76 int bytes,
77 int flags)
78 {
79 int i;
80 void **p_argv;
81 char *argp, *argp_f;
82 ffi_type **p_arg;
83
84 memset(stack, 0, bytes);
85
86 #ifdef FFI_MIPS_N32
87 int soft_float = (ecif->cif->abi == FFI_N32_SOFT_FLOAT
88 || ecif->cif->abi == FFI_N64_SOFT_FLOAT);
89 /* If more than 8 double words are used, the remainder go
90 on the stack. We reorder stuff on the stack here to
91 support this easily. */
92 /* if ret is _Complex long double, args reg shift2, and a0 should holds pointer to rvalue */
93 if (ecif->cif->rtype->type == FFI_TYPE_COMPLEX && ecif->cif->rtype->elements[0]->type == FFI_TYPE_LONGDOUBLE)
94 {
95 if (bytes + 16 > 8 * sizeof(ffi_arg))
96 argp = &stack[bytes - (8 * sizeof(ffi_arg))];
97 else
98 argp = stack;
99 * (unsigned long *) argp = (unsigned long) ecif->rvalue;
100 argp += 16;
101 }
102 else
103 {
104 if (bytes > 8 * sizeof(ffi_arg))
105 argp = &stack[bytes - (8 * sizeof(ffi_arg))];
106 else
107 argp = stack;
108 }
109 #else
110 argp = stack;
111 #endif
112
113 argp_f = argp;
114
115 #ifdef FFI_MIPS_N32
116 if ( ecif->cif->rstruct_flag != 0 )
117 #else
118 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT )
119 #endif
120 {
121 *(ffi_arg *) argp = (ffi_arg) ecif->rvalue;
122 argp += sizeof(ffi_arg);
123 FIX_ARGP;
124 }
125
126 p_argv = ecif->avalue;
127
128 for (i = 0, p_arg = ecif->cif->arg_types; i < ecif->cif->nargs; i++, p_arg++)
129 {
130 size_t z;
131 unsigned int a;
132
133 /* Align if necessary. */
134 a = (*p_arg)->alignment;
135 if (a < sizeof(ffi_arg))
136 a = sizeof(ffi_arg);
137
138 if ((a - 1) & (unsigned long) argp)
139 {
140 argp = (char *) FFI_ALIGN(argp, a);
141 FIX_ARGP;
142 }
143
144 z = (*p_arg)->size;
145 if (z <= sizeof(ffi_arg))
146 {
147 int type = (*p_arg)->type;
148 z = sizeof(ffi_arg);
149
150 /* The size of a pointer depends on the ABI */
151 if (type == FFI_TYPE_POINTER)
152 type = (ecif->cif->abi == FFI_N64
153 || ecif->cif->abi == FFI_N64_SOFT_FLOAT)
154 ? FFI_TYPE_SINT64 : FFI_TYPE_UINT32;
155
156 if (i < 8 && (ecif->cif->abi == FFI_N32_SOFT_FLOAT
157 || ecif->cif->abi == FFI_N64_SOFT_FLOAT))
158 {
159 switch (type)
160 {
161 case FFI_TYPE_FLOAT:
162 type = FFI_TYPE_UINT32;
163 break;
164 case FFI_TYPE_DOUBLE:
165 type = FFI_TYPE_UINT64;
166 break;
167 default:
168 break;
169 }
170 }
171 switch (type)
172 {
173 case FFI_TYPE_SINT8:
174 *(ffi_arg *)argp = *(SINT8 *)(* p_argv);
175 break;
176
177 case FFI_TYPE_UINT8:
178 *(ffi_arg *)argp = *(UINT8 *)(* p_argv);
179 break;
180
181 case FFI_TYPE_SINT16:
182 *(ffi_arg *)argp = *(SINT16 *)(* p_argv);
183 break;
184
185 case FFI_TYPE_UINT16:
186 *(ffi_arg *)argp = *(UINT16 *)(* p_argv);
187 break;
188
189 case FFI_TYPE_SINT32:
190 *(ffi_arg *)argp = *(SINT32 *)(* p_argv);
191 break;
192
193 case FFI_TYPE_UINT32:
194 #ifdef FFI_MIPS_N32
195 /* The N32 ABI requires that 32-bit integers
196 be sign-extended to 64-bits, regardless of
197 whether they are signed or unsigned. */
198 *(ffi_arg *)argp = *(SINT32 *)(* p_argv);
199 #else
200 *(ffi_arg *)argp = *(UINT32 *)(* p_argv);
201 #endif
202 break;
203
204 #ifdef FFI_MIPS_N32
205 case FFI_TYPE_COMPLEX:
206 /* expand from 4+4 to 8+8 if pass with fpr reg */
207 /* argp will wind back to stack when we process all of reg args */
208 /* all var_args passed with gpr, should be expand */
209 if(!soft_float
210 && (*p_arg)->elements[0]->type == FFI_TYPE_FLOAT
211 && argp>=argp_f
212 && i < ecif->cif->mips_nfixedargs)
213 {
214 *(float *) argp = *(float *)(* p_argv);
215 argp += z;
216 char *tmp = (void *) (*p_argv);
217 *(float *) argp = *(float *)(tmp+4);
218 }
219 else
220 memcpy(argp, *p_argv, (*p_arg)->size);
221 break;
222 #endif
223 /* This can only happen with 64bit slots. */
224 case FFI_TYPE_FLOAT:
225 *(float *) argp = *(float *)(* p_argv);
226 break;
227
228 /* Handle structures. */
229 default:
230 memcpy(argp, *p_argv, (*p_arg)->size);
231 break;
232 }
233 }
234 else
235 {
236 #ifdef FFI_MIPS_O32
237 memcpy(argp, *p_argv, z);
238 #else
239 {
240 unsigned long end = (unsigned long) argp + z;
241 unsigned long cap = (unsigned long) stack + bytes;
242
243 /* Check if the data will fit within the register space.
244 Handle it if it doesn't. */
245
246 if (end <= cap)
247 memcpy(argp, *p_argv, z);
248 else
249 {
250 unsigned long portion = cap - (unsigned long)argp;
251
252 memcpy(argp, *p_argv, portion);
253 argp = stack;
254 z -= portion;
255 memcpy(argp, (void*)((unsigned long)(*p_argv) + portion),
256 z);
257 }
258 }
259 #endif
260 }
261 p_argv++;
262 argp += z;
263 FIX_ARGP;
264 }
265 }
266
267 #ifdef FFI_MIPS_N32
268
269 /* The n32 spec says that if "a chunk consists solely of a double
270 float field (but not a double, which is part of a union), it
271 is passed in a floating point register. Any other chunk is
272 passed in an integer register". This code traverses structure
273 definitions and generates the appropriate flags. */
274
275 static int
276 calc_n32_struct_flags_element(unsigned *flags, ffi_type *e,
277 unsigned *loc, unsigned *arg_reg)
278 {
279 /* Align this object. */
280 *loc = FFI_ALIGN(*loc, e->alignment);
281 if (e->type == FFI_TYPE_DOUBLE)
282 {
283 /* Already aligned to FFI_SIZEOF_ARG. */
284 *arg_reg = *loc / FFI_SIZEOF_ARG;
285 if (*arg_reg > 7)
286 return 1;
287 *flags += (FFI_TYPE_DOUBLE << (*arg_reg * FFI_FLAG_BITS));
288 }
289 *loc += e->size;
290 return 0;
291 }
292
293 static unsigned
294 calc_n32_struct_flags(int soft_float, ffi_type *arg,
295 unsigned *loc, unsigned *arg_reg)
296 {
297 unsigned flags = 0;
298 unsigned index = 0;
299
300 ffi_type *e;
301
302 if (soft_float)
303 return 0;
304
305 while ((e = arg->elements[index]))
306 {
307 if (e->type == FFI_TYPE_COMPLEX)
308 {
309 if (calc_n32_struct_flags_element(&flags, e->elements[0], loc, arg_reg))
310 break;
311 if (calc_n32_struct_flags_element(&flags, e->elements[0], loc, arg_reg))
312 break;
313 }
314 else
315 if (calc_n32_struct_flags_element(&flags, e, loc, arg_reg))
316 break;
317 index++;
318 }
319 /* Next Argument register at alignment of FFI_SIZEOF_ARG. */
320 *arg_reg = FFI_ALIGN(*loc, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
321
322 return flags;
323 }
324
325 static unsigned
326 calc_n32_return_struct_flags(int soft_float, ffi_type *arg)
327 {
328 unsigned flags;
329 unsigned small = FFI_TYPE_SMALLSTRUCT;
330 ffi_type *e;
331
332 /* Returning structures under n32 is a tricky thing.
333 A struct with only one or two floating point fields
334 is returned in $f0 (and $f2 if necessary). Any other
335 struct results at most 128 bits are returned in $2
336 (the first 64 bits) and $3 (remainder, if necessary).
337 Larger structs are handled normally. */
338
339 if (arg->size > 16)
340 return 0;
341
342 if (arg->size > 8)
343 small = FFI_TYPE_SMALLSTRUCT2;
344
345 e = arg->elements[0];
346
347 if (e->type == FFI_TYPE_COMPLEX)
348 {
349 int type = e->elements[0]->type;
350
351 if (type != FFI_TYPE_DOUBLE && type != FFI_TYPE_FLOAT)
352 return small;
353
354 if (arg->elements[1])
355 {
356 /* Two floating point fields with more fields!
357 This must be passed the old way. */
358 return small;
359 }
360
361 flags = (type << FFI_FLAG_BITS) + type;
362 }
363 else
364 {
365 if (e->type != FFI_TYPE_DOUBLE && e->type != FFI_TYPE_FLOAT)
366 return small;
367
368 flags = e->type;
369
370 if (arg->elements[1])
371 {
372 e = arg->elements[1];
373 if (e->type != FFI_TYPE_DOUBLE && e->type != FFI_TYPE_FLOAT)
374 return small;
375
376 if (arg->elements[2])
377 {
378 /* There are three arguments and the first two are
379 floats! This must be passed the old way. */
380 return small;
381 }
382
383 flags += e->type << FFI_FLAG_BITS;
384 }
385 }
386
387 if (soft_float)
388 flags += FFI_TYPE_STRUCT_SOFT;
389 return flags;
390 }
391
392 #endif
393
394 /* Perform machine dependent cif processing */
395 static ffi_status ffi_prep_cif_machdep_int(ffi_cif *cif, unsigned nfixedargs)
396 {
397 cif->flags = 0;
398 cif->mips_nfixedargs = nfixedargs;
399
400 #ifdef FFI_MIPS_O32
401 /* Set the flags necessary for O32 processing. FFI_O32_SOFT_FLOAT
402 * does not have special handling for floating point args.
403 */
404
405 if (cif->rtype->type != FFI_TYPE_STRUCT && cif->rtype->type != FFI_TYPE_COMPLEX && cif->abi == FFI_O32)
406 {
407 if (cif->nargs > 0 && cif->nargs == nfixedargs)
408 {
409 switch ((cif->arg_types)[0]->type)
410 {
411 case FFI_TYPE_FLOAT:
412 case FFI_TYPE_DOUBLE:
413 cif->flags += (cif->arg_types)[0]->type;
414 break;
415
416 default:
417 break;
418 }
419
420 if (cif->nargs > 1)
421 {
422 /* Only handle the second argument if the first
423 is a float or double. */
424 if (cif->flags)
425 {
426 switch ((cif->arg_types)[1]->type)
427 {
428 case FFI_TYPE_FLOAT:
429 case FFI_TYPE_DOUBLE:
430 cif->flags += (cif->arg_types)[1]->type << FFI_FLAG_BITS;
431 break;
432
433 default:
434 break;
435 }
436 }
437 }
438 }
439 }
440
441 /* Set the return type flag */
442
443 if (cif->abi == FFI_O32_SOFT_FLOAT)
444 {
445 switch (cif->rtype->type)
446 {
447 case FFI_TYPE_VOID:
448 case FFI_TYPE_STRUCT:
449 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
450 break;
451
452 case FFI_TYPE_SINT64:
453 case FFI_TYPE_UINT64:
454 case FFI_TYPE_DOUBLE:
455 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
456 break;
457
458 case FFI_TYPE_FLOAT:
459 default:
460 cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
461 break;
462 }
463 }
464 else
465 {
466 /* FFI_O32 */
467 switch (cif->rtype->type)
468 {
469 case FFI_TYPE_VOID:
470 case FFI_TYPE_STRUCT:
471 case FFI_TYPE_FLOAT:
472 case FFI_TYPE_DOUBLE:
473 case FFI_TYPE_COMPLEX:
474 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
475 if (cif->rtype->type == FFI_TYPE_COMPLEX)
476 cif->flags += ((*cif->rtype->elements[0]).type) << (FFI_FLAG_BITS * 4);
477 break;
478
479 case FFI_TYPE_SINT64:
480 case FFI_TYPE_UINT64:
481 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
482 break;
483
484 default:
485 cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
486 break;
487 }
488 }
489 #endif
490
491 #ifdef FFI_MIPS_N32
492 /* Set the flags necessary for N32 processing */
493 {
494 unsigned arg_reg = 0;
495 unsigned loc = 0;
496 unsigned count = (cif->nargs < 8) ? cif->nargs : 8;
497 unsigned index = 0;
498
499 unsigned struct_flags = 0;
500 int soft_float = (cif->abi == FFI_N32_SOFT_FLOAT
501 || cif->abi == FFI_N64_SOFT_FLOAT);
502
503 if (cif->rtype->type == FFI_TYPE_STRUCT)
504 {
505 struct_flags = calc_n32_return_struct_flags(soft_float, cif->rtype);
506
507 if (struct_flags == 0)
508 {
509 /* This means that the structure is being passed as
510 a hidden argument */
511
512 arg_reg = 1;
513 count = (cif->nargs < 7) ? cif->nargs : 7;
514
515 cif->rstruct_flag = !0;
516 }
517 else
518 cif->rstruct_flag = 0;
519 }
520 else
521 cif->rstruct_flag = 0;
522
523 while (count-- > 0 && arg_reg < 8)
524 {
525 ffi_type *t = cif->arg_types[index];
526
527 switch (t->type)
528 {
529 case FFI_TYPE_FLOAT:
530 case FFI_TYPE_DOUBLE:
531 if (!soft_float && index < nfixedargs)
532 cif->flags += t->type << (arg_reg * FFI_FLAG_BITS);
533 arg_reg++;
534 break;
535 case FFI_TYPE_LONGDOUBLE:
536 /* Align it. */
537 arg_reg = FFI_ALIGN(arg_reg, 2);
538 /* Treat it as two adjacent doubles. */
539 if (soft_float || index >= nfixedargs)
540 {
541 arg_reg += 2;
542 }
543 else
544 {
545 cif->flags +=
546 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
547 arg_reg++;
548 if (arg_reg >= 8)
549 continue;
550 cif->flags +=
551 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
552 arg_reg++;
553 }
554 break;
555
556 case FFI_TYPE_COMPLEX:
557 switch (t->elements[0]->type)
558 {
559 case FFI_TYPE_LONGDOUBLE:
560 arg_reg = FFI_ALIGN(arg_reg, 2);
561 if (soft_float || index >= nfixedargs)
562 {
563 arg_reg += 2;
564 }
565 else
566 {
567 cif->flags +=
568 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
569 arg_reg++;
570 if (arg_reg >= 8)
571 continue;
572 cif->flags +=
573 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
574 arg_reg++;
575 if (arg_reg >= 8)
576 continue;
577 }
578 /* passthrough */
579 case FFI_TYPE_FLOAT:
580 // one fpr can only holds one arg even it is single
581 cif->bytes += 16;
582 /* passthrough */
583 case FFI_TYPE_SINT32:
584 case FFI_TYPE_UINT32:
585 case FFI_TYPE_DOUBLE:
586 if (soft_float || index >= nfixedargs)
587 {
588 arg_reg += 2;
589 }
590 else
591 {
592 uint32_t type = t->elements[0]->type != FFI_TYPE_LONGDOUBLE? t->elements[0]->type: FFI_TYPE_DOUBLE;
593 cif->flags +=
594 (type << (arg_reg * FFI_FLAG_BITS));
595 arg_reg++;
596 if (arg_reg >= 8)
597 continue;
598 cif->flags +=
599 (type << (arg_reg * FFI_FLAG_BITS));
600 arg_reg++;
601 }
602 break;
603 default:
604 arg_reg += 2;
605 break;
606 }
607 break;
608
609 case FFI_TYPE_STRUCT:
610 loc = arg_reg * FFI_SIZEOF_ARG;
611 cif->flags += calc_n32_struct_flags(soft_float || index >= nfixedargs,
612 t, &loc, &arg_reg);
613 break;
614
615 default:
616 arg_reg++;
617 break;
618 }
619
620 index++;
621 }
622
623 /* Set the return type flag */
624 switch (cif->rtype->type)
625 {
626 case FFI_TYPE_STRUCT:
627 {
628 if (struct_flags == 0)
629 {
630 /* The structure is returned through a hidden
631 first argument. Do nothing, 'cause FFI_TYPE_VOID
632 is 0 */
633 }
634 else
635 {
636 /* The structure is returned via some tricky
637 mechanism */
638 cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
639 cif->flags += struct_flags << (4 + (FFI_FLAG_BITS * 8));
640 }
641 break;
642 }
643
644 case FFI_TYPE_VOID:
645 /* Do nothing, 'cause FFI_TYPE_VOID is 0 */
646 break;
647
648 case FFI_TYPE_POINTER:
649 if (cif->abi == FFI_N32_SOFT_FLOAT || cif->abi == FFI_N32)
650 cif->flags += FFI_TYPE_SINT32 << (FFI_FLAG_BITS * 8);
651 else
652 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 8);
653 break;
654
655 case FFI_TYPE_FLOAT:
656 if (soft_float)
657 {
658 cif->flags += FFI_TYPE_SINT32 << (FFI_FLAG_BITS * 8);
659 break;
660 }
661 /* else fall through */
662 case FFI_TYPE_DOUBLE:
663 if (soft_float)
664 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 8);
665 else
666 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 8);
667 break;
668
669 case FFI_TYPE_LONGDOUBLE:
670 /* Long double is returned as if it were a struct containing
671 two doubles. */
672 if (soft_float)
673 {
674 /* if ret is long double, the ret is given by v0 and a0, no idea why
675 * Let's us VOID | VOID | LONGDOUBLE for it*/
676 cif->flags += FFI_TYPE_LONGDOUBLE << (FFI_FLAG_BITS * 8);
677 }
678 else
679 {
680 cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
681 cif->flags += (FFI_TYPE_DOUBLE
682 + (FFI_TYPE_DOUBLE << FFI_FLAG_BITS))
683 << (4 + (FFI_FLAG_BITS * 8));
684 }
685 break;
686 case FFI_TYPE_COMPLEX:
687 {
688 int type = cif->rtype->elements[0]->type;
689
690 cif->flags += (FFI_TYPE_COMPLEX << (FFI_FLAG_BITS * 8));
691 if (soft_float || (type != FFI_TYPE_FLOAT && type != FFI_TYPE_DOUBLE && type != FFI_TYPE_LONGDOUBLE))
692 {
693 switch (type)
694 {
695 case FFI_TYPE_DOUBLE:
696 case FFI_TYPE_SINT64:
697 case FFI_TYPE_UINT64:
698 case FFI_TYPE_INT:
699 type = FFI_TYPE_SMALLSTRUCT2;
700 break;
701 case FFI_TYPE_LONGDOUBLE:
702 type = FFI_TYPE_LONGDOUBLE;
703 break;
704 case FFI_TYPE_FLOAT:
705 default:
706 type = FFI_TYPE_SMALLSTRUCT;
707 }
708 cif->flags += type << (4 + (FFI_FLAG_BITS * 8));
709 }
710 else
711 {
712 //cif->flags += (type + (type << FFI_FLAG_BITS))
713 // << (4 + (FFI_FLAG_BITS * 8));
714 cif->flags += type << (4 + (FFI_FLAG_BITS * 8));
715 }
716 break;
717 }
718 case FFI_TYPE_UINT32:
719 /* In the N32 or N64 ABI unsigned 32-bit integer should be
720 *sign*-extended. */
721 cif->flags += FFI_TYPE_SINT32 << (FFI_FLAG_BITS * 8);
722 break;
723 case FFI_TYPE_SINT64:
724 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 8);
725 break;
726 default:
727 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 8);
728 break;
729 }
730 }
731 #endif
732 return FFI_OK;
733 }
734
735 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
736 {
737 return ffi_prep_cif_machdep_int(cif, cif->nargs);
738 }
739
740 ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif,
741 unsigned nfixedargs,
742 unsigned ntotalargs MAYBE_UNUSED)
743 {
744 return ffi_prep_cif_machdep_int(cif, nfixedargs);
745 }
746
747 /* Low level routine for calling O32 functions */
748 extern int ffi_call_O32(void (*)(char *, extended_cif *, int, int),
749 extended_cif *, unsigned,
750 unsigned, unsigned *, void (*)(void), void *closure);
751
752 /* Low level routine for calling N32 functions */
753 extern int ffi_call_N32(void (*)(char *, extended_cif *, int, int),
754 extended_cif *, unsigned,
755 unsigned, void *, void (*)(void), void *closure);
756
757 void ffi_call_int(ffi_cif *cif, void (*fn)(void), void *rvalue,
758 void **avalue, void *closure)
759 {
760 extended_cif ecif;
761
762 ecif.cif = cif;
763 ecif.avalue = avalue;
764
765 /* If the return value is a struct and we don't have a return */
766 /* value address then we need to make one */
767
768 if ((rvalue == NULL) &&
769 (cif->rtype->type == FFI_TYPE_STRUCT || cif->rtype->type == FFI_TYPE_COMPLEX))
770 ecif.rvalue = alloca(cif->rtype->size);
771 else
772 ecif.rvalue = rvalue;
773
774 switch (cif->abi)
775 {
776 #ifdef FFI_MIPS_O32
777 case FFI_O32:
778 case FFI_O32_SOFT_FLOAT:
779 ffi_call_O32(ffi_prep_args, &ecif, cif->bytes,
780 cif->flags, ecif.rvalue, fn, closure);
781 break;
782 #endif
783
784 #ifdef FFI_MIPS_N32
785 case FFI_N32:
786 case FFI_N32_SOFT_FLOAT:
787 case FFI_N64:
788 case FFI_N64_SOFT_FLOAT:
789 {
790 int copy_rvalue = 0;
791 int copy_offset = 0;
792 char *rvalue_copy = ecif.rvalue;
793 if (cif->rtype->type == FFI_TYPE_STRUCT && cif->rtype->size < 16)
794 {
795 /* For structures smaller than 16 bytes we clobber memory
796 in 8 byte increments. Make a copy so we don't clobber
797 the callers memory outside of the struct bounds. */
798 rvalue_copy = alloca(16);
799 copy_rvalue = 1;
800 }
801 else if (cif->rtype->type == FFI_TYPE_FLOAT
802 && (cif->abi == FFI_N64_SOFT_FLOAT
803 || cif->abi == FFI_N32_SOFT_FLOAT))
804 {
805 rvalue_copy = alloca (8);
806 copy_rvalue = 1;
807 #if defined(__MIPSEB__) || defined(_MIPSEB)
808 copy_offset = 4;
809 #endif
810 }
811 ffi_call_N32(ffi_prep_args, &ecif, cif->bytes,
812 cif->flags, rvalue_copy, fn, closure);
813 if (copy_rvalue)
814 memcpy(ecif.rvalue, rvalue_copy + copy_offset, cif->rtype->size);
815 }
816 break;
817 #endif
818
819 default:
820 FFI_ASSERT(0);
821 break;
822 }
823 }
824
825 void
826 ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
827 {
828 ffi_call_int (cif, fn, rvalue, avalue, NULL);
829 }
830
831 void
832 ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
833 void **avalue, void *closure)
834 {
835 ffi_call_int (cif, fn, rvalue, avalue, closure);
836 }
837
838
839 #if FFI_CLOSURES
840 #if defined(FFI_MIPS_O32)
841 extern void ffi_closure_O32(void);
842 extern void ffi_go_closure_O32(void);
843 #else
844 extern void ffi_closure_N32(void);
845 extern void ffi_go_closure_N32(void);
846 #endif /* FFI_MIPS_O32 */
847
848 ffi_status
849 ffi_prep_closure_loc (ffi_closure *closure,
850 ffi_cif *cif,
851 void (*fun)(ffi_cif*,void*,void**,void*),
852 void *user_data,
853 void *codeloc)
854 {
855 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
856 void * fn;
857 char *clear_location = (char *) codeloc;
858
859 #if defined(FFI_MIPS_O32)
860 if (cif->abi != FFI_O32 && cif->abi != FFI_O32_SOFT_FLOAT)
861 return FFI_BAD_ABI;
862 fn = ffi_closure_O32;
863 #else
864 #if _MIPS_SIM ==_ABIN32
865 if (cif->abi != FFI_N32
866 && cif->abi != FFI_N32_SOFT_FLOAT)
867 return FFI_BAD_ABI;
868 #else
869 if (cif->abi != FFI_N64
870 && cif->abi != FFI_N64_SOFT_FLOAT)
871 return FFI_BAD_ABI;
872 #endif
873 fn = ffi_closure_N32;
874 #endif /* FFI_MIPS_O32 */
875
876 #if defined(FFI_MIPS_O32) || (_MIPS_SIM ==_ABIN32)
877 /* lui $25,high(fn) */
878 tramp[0] = 0x3c190000 | ((unsigned)fn >> 16);
879 /* ori $25,low(fn) */
880 tramp[1] = 0x37390000 | ((unsigned)fn & 0xffff);
881 /* lui $12,high(codeloc) */
882 tramp[2] = 0x3c0c0000 | ((unsigned)codeloc >> 16);
883 /* jr $25 */
884 #if !defined(__mips_isa_rev) || (__mips_isa_rev<6)
885 tramp[3] = 0x03200008;
886 #else
887 tramp[3] = 0x03200009;
888 #endif
889 /* ori $12,low(codeloc) */
890 tramp[4] = 0x358c0000 | ((unsigned)codeloc & 0xffff);
891 #else
892 /* N64 has a somewhat larger trampoline. */
893 /* lui $25,high(fn) */
894 tramp[0] = 0x3c190000 | ((unsigned long)fn >> 48);
895 /* lui $12,high(codeloc) */
896 tramp[1] = 0x3c0c0000 | ((unsigned long)codeloc >> 48);
897 /* ori $25,mid-high(fn) */
898 tramp[2] = 0x37390000 | (((unsigned long)fn >> 32 ) & 0xffff);
899 /* ori $12,mid-high(codeloc) */
900 tramp[3] = 0x358c0000 | (((unsigned long)codeloc >> 32) & 0xffff);
901 /* dsll $25,$25,16 */
902 tramp[4] = 0x0019cc38;
903 /* dsll $12,$12,16 */
904 tramp[5] = 0x000c6438;
905 /* ori $25,mid-low(fn) */
906 tramp[6] = 0x37390000 | (((unsigned long)fn >> 16 ) & 0xffff);
907 /* ori $12,mid-low(codeloc) */
908 tramp[7] = 0x358c0000 | (((unsigned long)codeloc >> 16) & 0xffff);
909 /* dsll $25,$25,16 */
910 tramp[8] = 0x0019cc38;
911 /* dsll $12,$12,16 */
912 tramp[9] = 0x000c6438;
913 /* ori $25,low(fn) */
914 tramp[10] = 0x37390000 | ((unsigned long)fn & 0xffff);
915 /* jr $25 */
916 #if !defined(__mips_isa_rev) || (__mips_isa_rev<6)
917 tramp[11] = 0x03200008;
918 #else
919 tramp[11] = 0x03200009;
920 #endif
921 /* ori $12,low(codeloc) */
922 tramp[12] = 0x358c0000 | ((unsigned long)codeloc & 0xffff);
923
924 #endif
925
926 closure->cif = cif;
927 closure->fun = fun;
928 closure->user_data = user_data;
929
930 #if !defined(__FreeBSD__)
931 #ifdef USE__BUILTIN___CLEAR_CACHE
932 __builtin___clear_cache(clear_location, clear_location + FFI_TRAMPOLINE_SIZE);
933 #else
934 cacheflush (clear_location, FFI_TRAMPOLINE_SIZE, ICACHE);
935 #endif
936 #endif /* ! __FreeBSD__ */
937 return FFI_OK;
938 }
939
940 /*
941 * Decodes the arguments to a function, which will be stored on the
942 * stack. AR is the pointer to the beginning of the integer arguments
943 * (and, depending upon the arguments, some floating-point arguments
944 * as well). FPR is a pointer to the area where floating point
945 * registers have been saved, if any.
946 *
947 * RVALUE is the location where the function return value will be
948 * stored. CLOSURE is the prepared closure to invoke.
949 *
950 * This function should only be called from assembly, which is in
951 * turn called from a trampoline.
952 *
953 * Returns the function return type.
954 *
955 * Based on the similar routine for sparc.
956 */
957 int
958 ffi_closure_mips_inner_O32 (ffi_cif *cif,
959 void (*fun)(ffi_cif*, void*, void**, void*),
960 void *user_data,
961 void *rvalue, ffi_arg *ar,
962 double *fpr)
963 {
964 void **avaluep;
965 ffi_arg *avalue;
966 ffi_type **arg_types;
967 int i, avn, argn, seen_int;
968
969 avalue = alloca (cif->nargs * sizeof (ffi_arg));
970 avaluep = alloca (cif->nargs * sizeof (ffi_arg));
971
972 seen_int = (cif->abi == FFI_O32_SOFT_FLOAT) || (cif->mips_nfixedargs != cif->nargs);
973 argn = 0;
974
975 if ((cif->flags >> (FFI_FLAG_BITS * 2)) == FFI_TYPE_STRUCT)
976 {
977 rvalue = (void *)(uintptr_t)ar[0];
978 argn = 1;
979 seen_int = 1;
980 }
981 if ((cif->flags >> (FFI_FLAG_BITS * 2)) == FFI_TYPE_COMPLEX)
982 {
983 rvalue = fpr;
984 argn = 1;
985 }
986
987 i = 0;
988 avn = cif->nargs;
989 arg_types = cif->arg_types;
990
991 while (i < avn)
992 {
993 if (arg_types[i]->alignment == 8 && (argn & 0x1))
994 argn++;
995 if (i < 2 && !seen_int &&
996 (arg_types[i]->type == FFI_TYPE_FLOAT ||
997 arg_types[i]->type == FFI_TYPE_DOUBLE ||
998 arg_types[i]->type == FFI_TYPE_LONGDOUBLE))
999 {
1000 #if defined(__MIPSEB__) || defined(_MIPSEB)
1001 if (arg_types[i]->type == FFI_TYPE_FLOAT)
1002 avaluep[i] = ((char *) &fpr[i]) + sizeof (float);
1003 else
1004 #endif
1005 avaluep[i] = (char *) &fpr[i];
1006 }
1007 else
1008 {
1009 switch (arg_types[i]->type)
1010 {
1011 case FFI_TYPE_SINT8:
1012 avaluep[i] = &avalue[i];
1013 *(SINT8 *) &avalue[i] = (SINT8) ar[argn];
1014 break;
1015
1016 case FFI_TYPE_UINT8:
1017 avaluep[i] = &avalue[i];
1018 *(UINT8 *) &avalue[i] = (UINT8) ar[argn];
1019 break;
1020
1021 case FFI_TYPE_SINT16:
1022 avaluep[i] = &avalue[i];
1023 *(SINT16 *) &avalue[i] = (SINT16) ar[argn];
1024 break;
1025
1026 case FFI_TYPE_UINT16:
1027 avaluep[i] = &avalue[i];
1028 *(UINT16 *) &avalue[i] = (UINT16) ar[argn];
1029 break;
1030
1031 default:
1032 avaluep[i] = (char *) &ar[argn];
1033 break;
1034 }
1035 seen_int = 1;
1036 }
1037 argn += FFI_ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
1038 i++;
1039 }
1040
1041 /* Invoke the closure. */
1042 fun(cif, rvalue, avaluep, user_data);
1043
1044 if (cif->abi == FFI_O32_SOFT_FLOAT)
1045 {
1046 switch (cif->rtype->type)
1047 {
1048 case FFI_TYPE_FLOAT:
1049 return FFI_TYPE_INT;
1050 case FFI_TYPE_DOUBLE:
1051 return FFI_TYPE_UINT64;
1052 default:
1053 return cif->rtype->type;
1054 }
1055 }
1056 else
1057 {
1058 if (cif->rtype->type == FFI_TYPE_COMPLEX) {
1059 __asm__ volatile ("move $v1, %0" : : "r"(cif->rtype->size));
1060 }
1061 return cif->rtype->type;
1062 }
1063 }
1064
1065 #if defined(FFI_MIPS_N32)
1066
1067 static void
1068 copy_struct_N32(char *target, unsigned offset, ffi_abi abi, ffi_type *type,
1069 int argn, unsigned arg_offset, ffi_arg *ar,
1070 ffi_arg *fpr, int soft_float)
1071 {
1072 ffi_type **elt_typep = type->elements;
1073 while(*elt_typep)
1074 {
1075 ffi_type *elt_type = *elt_typep;
1076 unsigned o;
1077 char *tp;
1078 char *argp;
1079 char *fpp;
1080
1081 o = FFI_ALIGN(offset, elt_type->alignment);
1082 arg_offset += o - offset;
1083 offset = o;
1084 argn += arg_offset / sizeof(ffi_arg);
1085 arg_offset = arg_offset % sizeof(ffi_arg);
1086
1087 argp = (char *)(ar + argn);
1088 fpp = (char *)(argn >= 8 ? ar + argn : fpr + argn);
1089
1090 tp = target + offset;
1091
1092 if (elt_type->type == FFI_TYPE_DOUBLE && !soft_float)
1093 *(double *)tp = *(double *)fpp;
1094 else
1095 memcpy(tp, argp + arg_offset, elt_type->size);
1096
1097 offset += elt_type->size;
1098 arg_offset += elt_type->size;
1099 elt_typep++;
1100 argn += arg_offset / sizeof(ffi_arg);
1101 arg_offset = arg_offset % sizeof(ffi_arg);
1102 }
1103 }
1104
1105 /*
1106 * Decodes the arguments to a function, which will be stored on the
1107 * stack. AR is the pointer to the beginning of the integer
1108 * arguments. FPR is a pointer to the area where floating point
1109 * registers have been saved.
1110 *
1111 * RVALUE is the location where the function return value will be
1112 * stored. CLOSURE is the prepared closure to invoke.
1113 *
1114 * This function should only be called from assembly, which is in
1115 * turn called from a trampoline.
1116 *
1117 * Returns the function return flags.
1118 *
1119 */
1120 int
1121 ffi_closure_mips_inner_N32 (ffi_cif *cif,
1122 void (*fun)(ffi_cif*, void*, void**, void*),
1123 void *user_data,
1124 void *rvalue, ffi_arg *ar,
1125 ffi_arg *fpr)
1126 {
1127 void **avaluep;
1128 ffi_arg *avalue;
1129 ffi_type **arg_types;
1130 int i, avn, argn;
1131 int soft_float;
1132 ffi_arg *argp;
1133
1134 soft_float = cif->abi == FFI_N64_SOFT_FLOAT
1135 || cif->abi == FFI_N32_SOFT_FLOAT;
1136 avalue = alloca (cif->nargs * sizeof (ffi_arg));
1137 avaluep = alloca (cif->nargs * sizeof (ffi_arg));
1138
1139 argn = 0;
1140
1141 if (cif->rstruct_flag)
1142 {
1143 #if _MIPS_SIM==_ABIN32
1144 rvalue = (void *)(UINT32)ar[0];
1145 #else /* N64 */
1146 rvalue = (void *)ar[0];
1147 #endif
1148 argn = 1;
1149 }
1150 if (cif->rtype->type == FFI_TYPE_COMPLEX && cif->rtype->elements[0]->type == FFI_TYPE_LONGDOUBLE)
1151 argn = 2;
1152
1153 i = 0;
1154 avn = cif->nargs;
1155 arg_types = cif->arg_types;
1156
1157 while (i < avn)
1158 {
1159 if (arg_types[i]->type == FFI_TYPE_FLOAT
1160 || arg_types[i]->type == FFI_TYPE_DOUBLE
1161 || arg_types[i]->type == FFI_TYPE_LONGDOUBLE)
1162 {
1163 argp = (argn >= 8 || i >= cif->mips_nfixedargs || soft_float) ? ar + argn : fpr + argn;
1164 if ((arg_types[i]->type == FFI_TYPE_LONGDOUBLE) && ((uintptr_t)argp & (arg_types[i]->alignment-1)))
1165 {
1166 argp=(ffi_arg*)FFI_ALIGN(argp,arg_types[i]->alignment);
1167 argn++;
1168 }
1169 #if defined(__MIPSEB__) || defined(_MIPSEB)
1170 if (arg_types[i]->type == FFI_TYPE_FLOAT && argn < 8)
1171 avaluep[i] = ((char *) argp) + sizeof (float);
1172 else
1173 #endif
1174 avaluep[i] = (char *) argp;
1175 }
1176 else if (arg_types[i]->type == FFI_TYPE_COMPLEX && arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
1177 {
1178 argp = (argn >= 8 || i >= cif->mips_nfixedargs || soft_float) ? ar + argn : fpr + argn;
1179 avaluep[i] = (char *) argp;
1180 }
1181 else if (arg_types[i]->type == FFI_TYPE_COMPLEX && arg_types[i]->elements[0]->type == FFI_TYPE_LONGDOUBLE)
1182 {
1183 /* align long double */
1184 argn += ((argn & 0x1)? 1 : 0);
1185 argp = (argn >= 8 || i >= cif->mips_nfixedargs || soft_float) ? ar + argn : fpr + argn;
1186 avaluep[i] = (char *) argp;
1187 }
1188 else if (arg_types[i]->type == FFI_TYPE_COMPLEX && arg_types[i]->elements[0]->type == FFI_TYPE_FLOAT)
1189 {
1190 if (argn >= 8 || i >= cif->mips_nfixedargs || soft_float)
1191 argp = ar + argn;
1192 else
1193 {
1194 argp = fpr + argn;
1195 /* the normal args for function holds 8bytes, while here we convert it to ptr */
1196 uint32_t *tmp = (uint32_t *)argp;
1197 tmp[1] = tmp[2];
1198 }
1199 avaluep[i] = (char *) argp;
1200 }
1201 else
1202 {
1203 unsigned type = arg_types[i]->type;
1204
1205 if (arg_types[i]->alignment > sizeof(ffi_arg))
1206 argn = FFI_ALIGN(argn, arg_types[i]->alignment / sizeof(ffi_arg));
1207
1208 argp = ar + argn;
1209
1210 /* The size of a pointer depends on the ABI */
1211 if (type == FFI_TYPE_POINTER)
1212 type = (cif->abi == FFI_N64 || cif->abi == FFI_N64_SOFT_FLOAT)
1213 ? FFI_TYPE_SINT64 : FFI_TYPE_UINT32;
1214
1215 if (soft_float && type == FFI_TYPE_FLOAT)
1216 type = FFI_TYPE_SINT32;
1217
1218 switch (type)
1219 {
1220 case FFI_TYPE_SINT8:
1221 avaluep[i] = &avalue[i];
1222 *(SINT8 *) &avalue[i] = (SINT8) *argp;
1223 break;
1224
1225 case FFI_TYPE_UINT8:
1226 avaluep[i] = &avalue[i];
1227 *(UINT8 *) &avalue[i] = (UINT8) *argp;
1228 break;
1229
1230 case FFI_TYPE_SINT16:
1231 avaluep[i] = &avalue[i];
1232 *(SINT16 *) &avalue[i] = (SINT16) *argp;
1233 break;
1234
1235 case FFI_TYPE_UINT16:
1236 avaluep[i] = &avalue[i];
1237 *(UINT16 *) &avalue[i] = (UINT16) *argp;
1238 break;
1239
1240 case FFI_TYPE_SINT32:
1241 avaluep[i] = &avalue[i];
1242 *(SINT32 *) &avalue[i] = (SINT32) *argp;
1243 break;
1244
1245 case FFI_TYPE_UINT32:
1246 avaluep[i] = &avalue[i];
1247 *(UINT32 *) &avalue[i] = (UINT32) *argp;
1248 break;
1249
1250 case FFI_TYPE_STRUCT:
1251 if (argn < 8)
1252 {
1253 /* Allocate space for the struct as at least part of
1254 it was passed in registers. */
1255 avaluep[i] = alloca(arg_types[i]->size);
1256 copy_struct_N32(avaluep[i], 0, cif->abi, arg_types[i],
1257 argn, 0, ar, fpr, i >= cif->mips_nfixedargs || soft_float);
1258
1259 break;
1260 }
1261 /* Else fall through. */
1262 default:
1263 avaluep[i] = (char *) argp;
1264 break;
1265 }
1266 }
1267 argn += FFI_ALIGN(arg_types[i]->size, sizeof(ffi_arg)) / sizeof(ffi_arg);
1268 i++;
1269 }
1270
1271 /* Invoke the closure. */
1272 fun (cif, rvalue, avaluep, user_data);
1273
1274 return cif->flags >> (FFI_FLAG_BITS * 8);
1275 }
1276
1277 #endif /* FFI_MIPS_N32 */
1278
1279 #if defined(FFI_MIPS_O32)
1280 extern void ffi_closure_O32(void);
1281 extern void ffi_go_closure_O32(void);
1282 #else
1283 extern void ffi_closure_N32(void);
1284 extern void ffi_go_closure_N32(void);
1285 #endif /* FFI_MIPS_O32 */
1286
1287 ffi_status
1288 ffi_prep_go_closure (ffi_go_closure* closure, ffi_cif* cif,
1289 void (*fun)(ffi_cif*,void*,void**,void*))
1290 {
1291 void * fn;
1292
1293 #if defined(FFI_MIPS_O32)
1294 if (cif->abi != FFI_O32 && cif->abi != FFI_O32_SOFT_FLOAT)
1295 return FFI_BAD_ABI;
1296 fn = ffi_go_closure_O32;
1297 #else
1298 #if _MIPS_SIM ==_ABIN32
1299 if (cif->abi != FFI_N32
1300 && cif->abi != FFI_N32_SOFT_FLOAT)
1301 return FFI_BAD_ABI;
1302 #else
1303 if (cif->abi != FFI_N64
1304 && cif->abi != FFI_N64_SOFT_FLOAT)
1305 return FFI_BAD_ABI;
1306 #endif
1307 fn = ffi_go_closure_N32;
1308 #endif /* FFI_MIPS_O32 */
1309
1310 closure->tramp = (void *)fn;
1311 closure->cif = cif;
1312 closure->fun = fun;
1313
1314 return FFI_OK;
1315 }
1316
1317 #endif /* FFI_CLOSURES */