Back to index

plt-scheme  4.2.1
prep_cif.c
Go to the documentation of this file.
00001 /* -----------------------------------------------------------------------
00002    prep_cif.c - Copyright (c) 1996, 1998, 2007  Red Hat, Inc.
00003 
00004    Permission is hereby granted, free of charge, to any person obtaining
00005    a copy of this software and associated documentation files (the
00006    ``Software''), to deal in the Software without restriction, including
00007    without limitation the rights to use, copy, modify, merge, publish,
00008    distribute, sublicense, and/or sell copies of the Software, and to
00009    permit persons to whom the Software is furnished to do so, subject to
00010    the following conditions:
00011 
00012    The above copyright notice and this permission notice shall be included
00013    in all copies or substantial portions of the Software.
00014 
00015    THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018    IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00019    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00020    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00021    OTHER DEALINGS IN THE SOFTWARE.
00022    ----------------------------------------------------------------------- */
00023 
00024 #include <ffi.h>
00025 #include <ffi_common.h>
00026 #include <stdlib.h>
00027 
00028 /* Round up to FFI_SIZEOF_ARG. */
00029 
00030 #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
00031 
00032 /* Perform machine independent initialization of aggregate type
00033    specifications. */
00034 
00035 static ffi_status initialize_aggregate(ffi_type *arg)
00036 {
00037   ffi_type **ptr;
00038 
00039   FFI_ASSERT(arg != NULL);
00040 
00041   FFI_ASSERT(arg->elements != NULL);
00042   FFI_ASSERT(arg->size == 0);
00043   FFI_ASSERT(arg->alignment == 0);
00044 
00045   ptr = &(arg->elements[0]);
00046 
00047   while ((*ptr) != NULL)
00048     {
00049       if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
00050        return FFI_BAD_TYPEDEF;
00051 
00052       /* Perform a sanity check on the argument type */
00053       FFI_ASSERT_VALID_TYPE(*ptr);
00054 
00055       arg->size = ALIGN(arg->size, (*ptr)->alignment);
00056       arg->size += (*ptr)->size;
00057 
00058       arg->alignment = (arg->alignment > (*ptr)->alignment) ?
00059        arg->alignment : (*ptr)->alignment;
00060 
00061       ptr++;
00062     }
00063 
00064   /* Structure size includes tail padding.  This is important for
00065      structures that fit in one register on ABIs like the PowerPC64
00066      Linux ABI that right justify small structs in a register.
00067      It's also needed for nested structure layout, for example
00068      struct A { long a; char b; }; struct B { struct A x; char y; };
00069      should find y at an offset of 2*sizeof(long) and result in a
00070      total size of 3*sizeof(long).  */
00071   arg->size = ALIGN (arg->size, arg->alignment);
00072 
00073   if (arg->size == 0)
00074     return FFI_BAD_TYPEDEF;
00075   else
00076     return FFI_OK;
00077 }
00078 
00079 #ifndef __CRIS__
00080 /* The CRIS ABI specifies structure elements to have byte
00081    alignment only, so it completely overrides this functions,
00082    which assumes "natural" alignment and padding.  */
00083 
00084 /* Perform machine independent ffi_cif preparation, then call
00085    machine dependent routine. */
00086 
00087 ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs,
00088                      ffi_type *rtype, ffi_type **atypes)
00089 {
00090   unsigned bytes = 0;
00091   unsigned int i;
00092   ffi_type **ptr;
00093 
00094   FFI_ASSERT(cif != NULL);
00095   FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
00096 
00097   cif->abi = abi;
00098   cif->arg_types = atypes;
00099   cif->nargs = nargs;
00100   cif->rtype = rtype;
00101 
00102   cif->flags = 0;
00103 
00104   /* Initialize the return type if necessary */
00105   if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
00106     return FFI_BAD_TYPEDEF;
00107 
00108   /* Perform a sanity check on the return type */
00109   FFI_ASSERT_VALID_TYPE(cif->rtype);
00110 
00111   /* x86-64 and s390 stack space allocation is handled in prep_machdep.  */
00112 #if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
00113   /* Make space for the return structure pointer */
00114   if (cif->rtype->type == FFI_TYPE_STRUCT
00115 #ifdef SPARC
00116       && (cif->abi != FFI_V9 || cif->rtype->size > 32)
00117 #endif
00118 #ifdef X86_DARWIN
00119       && (cif->rtype->size > 8)
00120 #endif
00121      )
00122     bytes = STACK_ARG_SIZE(sizeof(void*));
00123 #endif
00124 
00125   for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
00126     {
00127 
00128       /* Initialize any uninitialized aggregate type definitions */
00129       if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
00130        return FFI_BAD_TYPEDEF;
00131 
00132       /* Perform a sanity check on the argument type, do this
00133         check after the initialization.  */
00134       FFI_ASSERT_VALID_TYPE(*ptr);
00135 
00136 #if !defined __x86_64__ && !defined S390 && !defined PA
00137 #ifdef SPARC
00138       if (((*ptr)->type == FFI_TYPE_STRUCT
00139           && ((*ptr)->size > 16 || cif->abi != FFI_V9))
00140          || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
00141              && cif->abi != FFI_V9))
00142        bytes += sizeof(void*);
00143       else
00144 #endif
00145        {
00146          /* Add any padding if necessary */
00147          if (((*ptr)->alignment - 1) & bytes)
00148            bytes = ALIGN(bytes, (*ptr)->alignment);
00149 
00150          bytes += STACK_ARG_SIZE((*ptr)->size);
00151        }
00152 #endif
00153     }
00154 
00155   cif->bytes = bytes;
00156 
00157   /* Perform machine dependent cif processing */
00158   return ffi_prep_cif_machdep(cif);
00159 }
00160 #endif /* not __CRIS__ */
00161 
00162 #if FFI_CLOSURES
00163 
00164 ffi_status
00165 ffi_prep_closure (ffi_closure* closure,
00166                 ffi_cif* cif,
00167                 void (*fun)(ffi_cif*,void*,void**,void*),
00168                 void *user_data)
00169 {
00170   return ffi_prep_closure_loc (closure, cif, fun, user_data, closure);
00171 }
00172 
00173 #endif