Skip to content

Commit

Permalink
2014-07-22 Dominik Vogt <vogt@linux.vnet.ibm.com>
Browse files Browse the repository at this point in the history
	* src/types.c (FFI_TYPEDEF, FFI_NONCONST_TYPEDEF): Merge the macros by
	adding another argument that controls whether the result is const or not
	(FFI_LDBL_CONST): Temporary macro to reduce ifdef confusion
	* src/prep_cif.c (ffi_prep_cif_core): Replace list of systems with new
	macro FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
	* src/pa/ffitarget.h (FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION):
	Define.
	* src/s390/ffitarget.h (FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION):
	Define.
	* src/x86/ffitarget.h (FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION):
	Define.

2014-07-22  Dominik Vogt  <vogt@linux.vnet.ibm.com>

	* doc/libffi.texi (Primitive Types): Document ffi_type_complex_float,
	ffi_type_complex_double and ffi_type_complex_longdouble
	(Complex Types): New subsection.
	(Complex Type Example): Ditto.
	* testsuite/libffi.call/cls_align_complex_double.c: New
	FFI_TYPE_COMPLEX test.
	* testsuite/libffi.call/cls_align_complex_float.c: Ditto.
	* testsuite/libffi.call/cls_align_complex_longdouble.c: Ditto.
	* testsuite/libffi.call/cls_complex_double.c: Ditto.
	* testsuite/libffi.call/cls_complex_float.c: Ditto.
	* testsuite/libffi.call/cls_complex_longdouble.c: Ditto.
	* testsuite/libffi.call/cls_complex_struct_double.c: Ditto.
	* testsuite/libffi.call/cls_complex_struct_float.c: Ditto.
	* testsuite/libffi.call/cls_complex_struct_longdouble.c: Ditto.
	* testsuite/libffi.call/cls_complex_va_double.c: Ditto.
	* testsuite/libffi.call/cls_complex_va_float.c: Ditto.
	* testsuite/libffi.call/cls_complex_va_longdouble.c: Ditto.
	* testsuite/libffi.call/complex_double.c: Ditto.
	* testsuite/libffi.call/complex_defs_double.c: Ditto.
	* testsuite/libffi.call/complex_float.c: Ditto.
	* testsuite/libffi.call/complex_defs_float.c: Ditto.
	* testsuite/libffi.call/complex_longdouble.c: Ditto.
	* testsuite/libffi.call/complex_defs_longdouble.c: Ditto.
	* testsuite/libffi.call/complex_int.c: Ditto.
	* testsuite/libffi.call/many_complex_double.c: Ditto.
	* testsuite/libffi.call/many_complex_float.c: Ditto.
	* testsuite/libffi.call/many_complex_longdouble.c: Ditto.
	* testsuite/libffi.call/return_complex1_double.c: Ditto.
	* testsuite/libffi.call/return_complex1_float.c: Ditto.
	* testsuite/libffi.call/return_complex1_longdouble.c: Ditto.
	* testsuite/libffi.call/return_complex2_double.c: Ditto.
	* testsuite/libffi.call/return_complex2_float.c: Ditto.
	* testsuite/libffi.call/return_complex2_longdouble.c: Ditto.
	* testsuite/libffi.call/return_complex_double.c: Ditto.
	* testsuite/libffi.call/return_complex_float.c: Ditto.
	* testsuite/libffi.call/return_complex_longdouble.c: Ditto.
	* src/raw_api.c (ffi_raw_to_ptrarray): Handle FFI_TYPE_COMPLEX
	(ffi_ptrarray_to_raw): Ditto.
	* src/prep_cif.c (ffi_prep_cif_core): Abort if FFI_TYPE_COMPLEX is not
	implemented in libffi for the target.
	* src/java_raw_api.c (ffi_java_raw_size): FFI_TYPE_COMPLEX not supported
	yet (abort).
	(ffi_java_raw_to_ptrarray): Ditto.
	(ffi_java_rvalue_to_raw): Ditto.
	(ffi_java_raw_to_rvalue):  Ditto.
	* src/debug.c (ffi_type_test): Add debug tests for complex types.
	* include/ffi.h.in (FFI_TYPE_COMPLEX): Add new FFI_TYPE_COMPLEX.
	(FFI_TYPE_LAST): Bump.
	(ffi_type_complex_float): Add new ffi_type_....
	(ffi_type_complex_double): Ditto.
	(ffi_type_complex_longdouble): Ditto.

2014-07-22  Dominik Vogt  <vogt@linux.vnet.ibm.com>

	* src/s390/ffitarget.h (FFI_TARGET_HAS_COMPLEX_TYPE): Define to provide
	FFI_TYPE_COMPLEX support.
	* src/s390/ffi.c (ffi_check_struct_type): Implement FFI_TYPE_COMPLEX
	(ffi_prep_args): Ditto.
	(ffi_prep_cif_machdep): Ditto.
	(ffi_closure_helper_SYSV): Ditto.
  • Loading branch information
vogtd authored and atgreen committed Sep 20, 2014
1 parent 4c5c408 commit 6e8a446
Show file tree
Hide file tree
Showing 51 changed files with 1,177 additions and 38 deletions.
145 changes: 145 additions & 0 deletions doc/libffi.texi
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ int main()
* Primitive Types:: Built-in types.
* Structures:: Structure types.
* Type Example:: Structure type example.
* Complex:: Complex types.
* Complex Type Example:: Complex type example.
@end menu

@node Primitive Types
Expand Down Expand Up @@ -345,6 +347,20 @@ On other platforms, it is not.
@tindex ffi_type_pointer
A generic @code{void *} pointer. You should use this for all
pointers, regardless of their real type.

@item ffi_type_complex_float
@tindex ffi_type_complex_float
The C @code{_Complex float} type.

@item ffi_type_complex_double
@tindex ffi_type_complex_double
The C @code{_Complex double} type.

@item ffi_type_complex_longdouble
@tindex ffi_type_complex_longdouble
The C @code{_Complex long double} type.
On platforms that have a C @code{long double} type, this is defined.
On other platforms, it is not.
@end table

Each of these is of type @code{ffi_type}, so you must take the address
Expand Down Expand Up @@ -429,6 +445,135 @@ Here is the corresponding code to describe this struct to
@}
@end example

@node Complex
@subsection Complex Types

@samp{libffi} supports the complex types defined by the C99
standard (@code{_Complex float}, @code{_Complex double} and
@code{_Complex long double} with the built-in type descriptors
@code{ffi_type_complex_float}, @code{ffi_type_complex_double} and
@code{ffi_type_complex_longdouble}.

Custom complex types like @code{_Complex int} can also be used.
An @code{ffi_type} object has to be defined to describe the
complex type to @samp{libffi}.

@tindex ffi_type
@deftp {Data type} ffi_type
@table @code
@item size_t size
This must be manually set to the size of the complex type.

@item unsigned short alignment
This must be manually set to the alignment of the complex type.

@item unsigned short type
For a complex type, this must be set to @code{FFI_TYPE_COMPLEX}.

@item ffi_type **elements

This is a @samp{NULL}-terminated array of pointers to
@code{ffi_type} objects. The first element is set to the
@code{ffi_type} of the complex's base type. The second element
must be set to @code{NULL}.
@end table
@end deftp

The section @ref{Complex Type Example} shows a way to determine
the @code{size} and @code{alignment} members in a platform
independent way.

For platforms that have no complex support in @code{libffi} yet,
the functions @code{ffi_prep_cif} and @code{ffi_prep_args} abort
the program if they encounter a complex type.

@node Complex Type Example
@subsection Complex Type Example

This example demonstrates how to use complex types:

@example
#include <stdio.h>
#include <ffi.h>
#include <complex.h>
void complex_fn(_Complex float cf,
_Complex double cd,
_Complex long double cld)
@{
printf("cf=%f+%fi\ncd=%f+%fi\ncld=%f+%fi\n",
(float)creal (cf), (float)cimag (cf),
(float)creal (cd), (float)cimag (cd),
(float)creal (cld), (float)cimag (cld));
@}
int main()
@{
ffi_cif cif;
ffi_type *args[3];
void *values[3];
_Complex float cf;
_Complex double cd;
_Complex long double cld;
/* Initialize the argument info vectors */
args[0] = &ffi_type_complex_float;
args[1] = &ffi_type_complex_double;
args[2] = &ffi_type_complex_longdouble;
values[0] = &cf;
values[1] = &cd;
values[2] = &cld;
/* Initialize the cif */
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3,
&ffi_type_void, args) == FFI_OK)
@{
cf = 1.0 + 20.0 * I;
cd = 300.0 + 4000.0 * I;
cld = 50000.0 + 600000.0 * I;
/* Call the function */
ffi_call(&cif, (void (*)(void))complex_fn, 0, values);
@}
return 0;
@}
@end example

This is an example for defining a custom complex type descriptor
for compilers that support them:

@example
/*
* This macro can be used to define new complex type descriptors
* in a platform independent way.
*
* name: Name of the new descriptor is ffi_type_complex_<name>.
* type: The C base type of the complex type.
*/
#define FFI_COMPLEX_TYPEDEF(name, type, ffitype) \
static ffi_type *ffi_elements_complex_##name [2] = @{ \
(ffi_type *)(&ffitype), NULL \
@}; \
struct struct_align_complex_##name @{ \
char c; \
_Complex type x; \
@}; \
ffi_type ffi_type_complex_##name = @{ \
sizeof(_Complex type), \
offsetof(struct struct_align_complex_##name, x), \
FFI_TYPE_COMPLEX, \
(ffi_type **)ffi_elements_complex_##name \
@}
/* Define new complex type descriptors using the macro: */
/* ffi_type_complex_sint */
FFI_COMPLEX_TYPEDEF(sint, int, ffi_type_sint);
/* ffi_type_complex_uchar */
FFI_COMPLEX_TYPEDEF(uchar, unsigned char, ffi_type_uint8);
@end example

The new type descriptors can then be used like one of the built-in
type descriptors in the previous example.

@node Multiple ABIs
@section Multiple ABIs
Expand Down
13 changes: 12 additions & 1 deletion include/ffi.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ FFI_EXTERN ffi_type ffi_type_longdouble;
#else
#define ffi_type_longdouble ffi_type_double
#endif

#ifdef FFI_TARGET_HAS_COMPLEX_TYPE
FFI_EXTERN ffi_type ffi_type_complex_float;
FFI_EXTERN ffi_type ffi_type_complex_double;
#if @HAVE_LONG_DOUBLE@
FFI_EXTERN ffi_type ffi_type_complex_longdouble;
#else
#define ffi_type_complex_longdouble ffi_type_complex_double
#endif
#endif
#endif /* LIBFFI_HIDE_BASIC_TYPES */

typedef enum {
Expand Down Expand Up @@ -465,9 +475,10 @@ void ffi_call(ffi_cif *cif,
#define FFI_TYPE_SINT64 12
#define FFI_TYPE_STRUCT 13
#define FFI_TYPE_POINTER 14
#define FFI_TYPE_COMPLEX 15

/* This should always refer to the last type code (for sanity checks) */
#define FFI_TYPE_LAST FFI_TYPE_POINTER
#define FFI_TYPE_LAST FFI_TYPE_COMPLEX

#ifdef __cplusplus
}
Expand Down
7 changes: 6 additions & 1 deletion src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void ffi_type_test(ffi_type *a, char *file, int line)
FFI_ASSERT_AT(a->type <= FFI_TYPE_LAST, file, line);
FFI_ASSERT_AT(a->type == FFI_TYPE_VOID || a->size > 0, file, line);
FFI_ASSERT_AT(a->type == FFI_TYPE_VOID || a->alignment > 0, file, line);
FFI_ASSERT_AT(a->type != FFI_TYPE_STRUCT || a->elements != NULL, file, line);
FFI_ASSERT_AT((a->type != FFI_TYPE_STRUCT && a->type != FFI_TYPE_COMPLEX)
|| a->elements != NULL, file, line);
FFI_ASSERT_AT(a->type != FFI_TYPE_COMPLEX
|| (a->elements != NULL
&& a->elements[0] != NULL && a->elements[1] == NULL),
file, line);

}
18 changes: 18 additions & 0 deletions src/java_raw_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ ffi_java_raw_size (ffi_cif *cif)
case FFI_TYPE_STRUCT:
/* No structure parameters in Java. */
abort();
case FFI_TYPE_COMPLEX:
/* Not supported yet. */
abort();
default:
result += FFI_SIZEOF_JAVA_RAW;
}
Expand Down Expand Up @@ -104,6 +107,10 @@ ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args)
*args = (void*) &(raw++)->ptr;
break;

case FFI_TYPE_COMPLEX:
/* Not supported yet. */
abort();

default:
*args = raw;
raw +=
Expand All @@ -126,6 +133,9 @@ ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args)
*args = (void*) raw;
raw += 2;
break;
case FFI_TYPE_COMPLEX:
/* Not supported yet. */
abort();
default:
*args = (void*) raw++;
}
Expand Down Expand Up @@ -254,6 +264,10 @@ ffi_java_rvalue_to_raw (ffi_cif *cif, void *rvalue)
*(SINT64 *)rvalue <<= 32;
break;

case FFI_TYPE_COMPLEX:
/* Not supported yet. */
abort();

default:
break;
}
Expand All @@ -279,6 +293,10 @@ ffi_java_raw_to_rvalue (ffi_cif *cif, void *rvalue)
*(SINT64 *)rvalue >>= 32;
break;

case FFI_TYPE_COMPLEX:
/* Not supported yet. */
abort();

default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/pa/ffitarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef enum ffi_abi {
} ffi_abi;
#endif

#define FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION

/* ---- Definitions for closures ----------------------------------------- */

#define FFI_CLOSURES 1
Expand Down
12 changes: 10 additions & 2 deletions src/prep_cif.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ ffi_status FFI_HIDDEN ffi_prep_cif_core(ffi_cif *cif, ffi_abi abi,
if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
return FFI_BAD_TYPEDEF;

#ifndef FFI_TARGET_HAS_COMPLEX_TYPE
if (rtype->type == FFI_TYPE_COMPLEX)
abort();
#endif
/* Perform a sanity check on the return type */
FFI_ASSERT_VALID_TYPE(cif->rtype);

/* x86, x86-64 and s390 stack space allocation is handled in prep_machdep. */
#if !defined M68K && !defined X86_ANY && !defined S390 && !defined PA
#if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
/* Make space for the return structure pointer */
if (cif->rtype->type == FFI_TYPE_STRUCT
#ifdef SPARC
Expand All @@ -166,11 +170,15 @@ ffi_status FFI_HIDDEN ffi_prep_cif_core(ffi_cif *cif, ffi_abi abi,
if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
return FFI_BAD_TYPEDEF;

#ifndef FFI_TARGET_HAS_COMPLEX_TYPE
if ((*ptr)->type == FFI_TYPE_COMPLEX)
abort();
#endif
/* Perform a sanity check on the argument type, do this
check after the initialization. */
FFI_ASSERT_VALID_TYPE(*ptr);

#if !defined X86_ANY && !defined S390 && !defined PA
#if !defined FFI_TARGET_SPECIFIC_STACK_SPACE_ALLOCATION
#ifdef SPARC
if (((*ptr)->type == FFI_TYPE_STRUCT
&& ((*ptr)->size > 16 || cif->abi != FFI_V9))
Expand Down
13 changes: 13 additions & 0 deletions src/raw_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
break;
#endif

case FFI_TYPE_COMPLEX:
*args = (raw++)->ptr;
break;

case FFI_TYPE_POINTER:
*args = (void*) &(raw++)->ptr;
break;
Expand All @@ -112,6 +116,11 @@ ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args)
}
else
#endif
if ((*tp)->type == FFI_TYPE_COMPLEX)
{
*args = (raw++)->ptr;
}
else
{
*args = (void*) raw;
raw += ALIGN ((*tp)->size, sizeof (void*)) / sizeof (void*);
Expand Down Expand Up @@ -167,6 +176,10 @@ ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw)
break;
#endif

case FFI_TYPE_COMPLEX:
(raw++)->ptr = *args;
break;

case FFI_TYPE_POINTER:
(raw++)->ptr = **(void***) args;
break;
Expand Down
Loading

0 comments on commit 6e8a446

Please sign in to comment.