1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996 Cygnus Solutions
4 MIPS Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
31 #if _MIPS_SIM == _MIPS_SIM_NABI32
33 FFI_ASSERT(argp <= &stack[bytes]); \
34 if (argp == &stack[bytes]) \
44 /* ffi_prep_args is called by the assembly routine once stack space
45 has been allocated for the function's arguments */
47 static void ffi_prep_args(char *stack
,
54 register void **p_argv
;
56 register ffi_type
**p_arg
;
58 #if _MIPS_SIM == _MIPS_SIM_NABI32
59 /* If more than 8 double words are used, the remainder go
60 on the stack. We reorder stuff on the stack here to
61 support this easily. */
62 if (bytes
> 8 * SIZEOF_ARG
)
63 argp
= &stack
[bytes
- (8 * SIZEOF_ARG
)];
70 memset(stack
, 0, bytes
);
72 #if _MIPS_SIM == _MIPS_SIM_NABI32
73 if ( ecif
->cif
->rstruct_flag
!= 0 )
75 if ( ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
)
78 *(SLOT_TYPE_UNSIGNED
*) argp
= (SLOT_TYPE_UNSIGNED
) ecif
->rvalue
;
79 argp
+= sizeof(SLOT_TYPE_UNSIGNED
);
83 avn
= ecif
->cif
->nargs
;
84 p_argv
= ecif
->avalue
;
86 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
92 /* Align if necessary */
93 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
94 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
98 #if _MIPS_SIM == _MIPS_SIM_ABI32
101 #define OFFSET sizeof(int)
108 if (z
< sizeof(SLOT_TYPE_UNSIGNED
))
110 z
= sizeof(SLOT_TYPE_UNSIGNED
);
112 switch ((*p_arg
)->type
)
115 *(SINT32
*) &argp
[OFFSET
] = (SINT32
)*(SINT8
*)(* p_argv
);
119 *(UINT32
*) &argp
[OFFSET
] = (UINT32
)*(UINT8
*)(* p_argv
);
122 case FFI_TYPE_SINT16
:
123 *(SINT32
*) &argp
[OFFSET
] = (SINT32
)*(SINT16
*)(* p_argv
);
126 case FFI_TYPE_UINT16
:
127 *(UINT32
*) &argp
[OFFSET
] = (UINT32
)*(UINT16
*)(* p_argv
);
130 case FFI_TYPE_SINT32
:
131 *(SINT32
*) &argp
[OFFSET
] = (SINT32
)*(SINT32
*)(* p_argv
);
134 case FFI_TYPE_UINT32
:
135 case FFI_TYPE_POINTER
:
136 *(UINT32
*) &argp
[OFFSET
] = (UINT32
)*(UINT32
*)(* p_argv
);
139 /* This can only happen with 64bit slots */
141 *(float *) argp
= *(float *)(* p_argv
);
144 /* Handle small structures */
145 case FFI_TYPE_STRUCT
:
146 memcpy(argp
, *p_argv
, (*p_arg
)->size
);
155 #if _MIPS_SIM == _MIPS_SIM_ABI32
156 memcpy(argp
, *p_argv
, z
);
159 unsigned end
= (unsigned) argp
+z
;
160 unsigned cap
= (unsigned) stack
+bytes
;
162 /* Check if the data will fit within the register
163 space. Handle it if it doesn't. */
166 memcpy(argp
, *p_argv
, z
);
169 unsigned portion
= end
- cap
;
171 memcpy(argp
, *p_argv
, portion
);
174 (void*)((unsigned)(*p_argv
)+portion
), z
- portion
);
188 #if _MIPS_SIM == _MIPS_SIM_NABI32
190 /* The n32 spec says that if "a chunk consists solely of a double
191 float field (but not a double, which is part of a union), it
192 is passed in a floating point register. Any other chunk is
193 passed in an integer register". This code traverses structure
194 definitions and generates the appropriate flags. */
196 unsigned calc_n32_struct_flags(ffi_type
*arg
, unsigned *shift
)
203 while (e
= arg
->elements
[index
])
205 if (e
->type
== FFI_TYPE_DOUBLE
)
207 flags
+= (FFI_TYPE_DOUBLE
<< *shift
);
208 *shift
+= FFI_FLAG_BITS
;
210 else if (e
->type
== FFI_TYPE_STRUCT
)
211 flags
+= calc_n32_struct_flags(e
, shift
);
213 *shift
+= FFI_FLAG_BITS
;
221 unsigned calc_n32_return_struct_flags(ffi_type
*arg
)
225 unsigned small
= FFI_TYPE_SMALLSTRUCT
;
228 /* Returning structures under n32 is a tricky thing.
229 A struct with only one or two floating point fields
230 is returned in $f0 (and $f2 if necessary). Any other
231 struct results at most 128 bits are returned in $2
232 (the first 64 bits) and $3 (remainder, if necessary).
233 Larger structs are handled normally. */
239 small
= FFI_TYPE_SMALLSTRUCT2
;
241 e
= arg
->elements
[0];
242 if (e
->type
== FFI_TYPE_DOUBLE
)
243 flags
= FFI_TYPE_DOUBLE
<< FFI_FLAG_BITS
;
244 else if (e
->type
== FFI_TYPE_FLOAT
)
245 flags
= FFI_TYPE_FLOAT
<< FFI_FLAG_BITS
;
247 if (flags
&& (e
= arg
->elements
[1]))
249 if (e
->type
== FFI_TYPE_DOUBLE
)
250 flags
+= FFI_TYPE_DOUBLE
;
251 else if (e
->type
== FFI_TYPE_FLOAT
)
252 flags
+= FFI_TYPE_FLOAT
;
256 if (flags
&& (arg
->elements
[2]))
258 /* There are three arguments and the first two are
259 floats! This must be passed the old way. */
272 /* Perform machine dependent cif processing */
273 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
277 #if _MIPS_SIM == _MIPS_SIM_ABI32
278 /* Set the flags necessary for O32 processing */
280 if (cif
->rtype
->type
!= FFI_TYPE_STRUCT
)
284 switch ((cif
->arg_types
)[0]->type
)
287 case FFI_TYPE_DOUBLE
:
288 cif
->flags
+= (cif
->arg_types
)[0]->type
;
297 /* Only handle the second argument if the first
298 is a float or double. */
301 switch ((cif
->arg_types
)[1]->type
)
304 case FFI_TYPE_DOUBLE
:
305 cif
->flags
+= (cif
->arg_types
)[1]->type
<< FFI_FLAG_BITS
;
316 /* Set the return type flag */
317 switch (cif
->rtype
->type
)
320 case FFI_TYPE_STRUCT
:
322 case FFI_TYPE_DOUBLE
:
323 cif
->flags
+= cif
->rtype
->type
<< (FFI_FLAG_BITS
* 2);
327 cif
->flags
+= FFI_TYPE_INT
<< (FFI_FLAG_BITS
* 2);
332 #if _MIPS_SIM == _MIPS_SIM_NABI32
333 /* Set the flags necessary for N32 processing */
336 unsigned count
= (cif
->nargs
< 8) ? cif
->nargs
: 8;
339 unsigned struct_flags
= 0;
341 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
343 struct_flags
= calc_n32_return_struct_flags(cif
->rtype
);
345 if (struct_flags
== 0)
347 /* This means that the structure is being passed as
350 shift
= FFI_FLAG_BITS
;
351 count
= (cif
->nargs
< 7) ? cif
->nargs
: 7;
353 cif
->rstruct_flag
= !0;
356 cif
->rstruct_flag
= 0;
359 cif
->rstruct_flag
= 0;
363 switch ((cif
->arg_types
)[index
]->type
)
366 case FFI_TYPE_DOUBLE
:
367 cif
->flags
+= ((cif
->arg_types
)[index
]->type
<< shift
);
368 shift
+= FFI_FLAG_BITS
;
371 case FFI_TYPE_STRUCT
:
372 cif
->flags
+= calc_n32_struct_flags((cif
->arg_types
)[index
],
377 shift
+= FFI_FLAG_BITS
;
383 /* Set the return type flag */
384 switch (cif
->rtype
->type
)
386 case FFI_TYPE_STRUCT
:
388 if (struct_flags
== 0)
390 /* The structure is returned through a hidden
391 first argument. Do nothing, 'cause FFI_TYPE_VOID
396 /* The structure is returned via some tricky
398 cif
->flags
+= FFI_TYPE_STRUCT
<< (FFI_FLAG_BITS
* 8);
399 cif
->flags
+= struct_flags
<< (4 + (FFI_FLAG_BITS
* 8));
405 /* Do nothing, 'cause FFI_TYPE_VOID is 0 */
409 case FFI_TYPE_DOUBLE
:
410 cif
->flags
+= cif
->rtype
->type
<< (FFI_FLAG_BITS
* 8);
414 cif
->flags
+= FFI_TYPE_INT
<< (FFI_FLAG_BITS
* 8);
423 /* Low level routine for calling O32 functions */
424 extern int ffi_call_O32(void (*)(char *, extended_cif
*, int, int),
425 extended_cif
*, unsigned,
426 unsigned, unsigned *, void (*)());
428 /* Low level routine for calling N32 functions */
429 extern int ffi_call_N32(void (*)(char *, extended_cif
*, int, int),
430 extended_cif
*, unsigned,
431 unsigned, unsigned *, void (*)());
433 void ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
438 ecif
.avalue
= avalue
;
440 /* If the return value is a struct and we don't have a return */
441 /* value address then we need to make one */
443 if ((rvalue
== NULL
) &&
444 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
445 ecif
.rvalue
= alloca(cif
->rtype
->size
);
447 ecif
.rvalue
= rvalue
;
451 #if _MIPS_SIM == _MIPS_SIM_ABI32
453 ffi_call_O32(ffi_prep_args
, &ecif
, cif
->bytes
,
454 cif
->flags
, ecif
.rvalue
, fn
);
458 #if _MIPS_SIM == _MIPS_SIM_NABI32
460 ffi_call_N32(ffi_prep_args
, &ecif
, cif
->bytes
,
461 cif
->flags
, ecif
.rvalue
, fn
);