/Modules/_ctypes/libffi_osx/ffi.c

http://unladen-swallow.googlecode.com/ · C · 226 lines · 132 code · 41 blank · 53 comment · 60 complexity · 143e561c7dcfaea64ddb522f6a42eb23 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. prep_cif.c - Copyright (c) 1996, 1998 Red Hat, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. ``Software''), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. OTHER DEALINGS IN THE SOFTWARE.
  19. ----------------------------------------------------------------------- */
  20. #include <ffi.h>
  21. #include <ffi_common.h>
  22. #include <stdbool.h>
  23. #include <stdlib.h>
  24. /* Round up to FFI_SIZEOF_ARG. */
  25. #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
  26. /* Perform machine independent initialization of aggregate type
  27. specifications. */
  28. static ffi_status
  29. initialize_aggregate(
  30. /*@out@*/ ffi_type* arg)
  31. {
  32. /*@-usedef@*/
  33. if (arg == NULL || arg->elements == NULL ||
  34. arg->size != 0 || arg->alignment != 0)
  35. return FFI_BAD_TYPEDEF;
  36. ffi_type** ptr = &(arg->elements[0]);
  37. while ((*ptr) != NULL)
  38. {
  39. if (((*ptr)->size == 0) && (initialize_aggregate(*ptr) != FFI_OK))
  40. return FFI_BAD_TYPEDEF;
  41. /* Perform a sanity check on the argument type */
  42. FFI_ASSERT_VALID_TYPE(*ptr);
  43. #ifdef POWERPC_DARWIN
  44. int curalign = (*ptr)->alignment;
  45. if (ptr != &(arg->elements[0]))
  46. {
  47. if (curalign > 4 && curalign != 16)
  48. curalign = 4;
  49. }
  50. arg->size = ALIGN(arg->size, curalign);
  51. arg->size += (*ptr)->size;
  52. arg->alignment = (arg->alignment > curalign) ?
  53. arg->alignment : curalign;
  54. #else
  55. arg->size = ALIGN(arg->size, (*ptr)->alignment);
  56. arg->size += (*ptr)->size;
  57. arg->alignment = (arg->alignment > (*ptr)->alignment) ?
  58. arg->alignment : (*ptr)->alignment;
  59. #endif
  60. ptr++;
  61. }
  62. /* Structure size includes tail padding. This is important for
  63. structures that fit in one register on ABIs like the PowerPC64
  64. Linux ABI that right justify small structs in a register.
  65. It's also needed for nested structure layout, for example
  66. struct A { long a; char b; }; struct B { struct A x; char y; };
  67. should find y at an offset of 2*sizeof(long) and result in a
  68. total size of 3*sizeof(long). */
  69. arg->size = ALIGN(arg->size, arg->alignment);
  70. if (arg->size == 0)
  71. return FFI_BAD_TYPEDEF;
  72. return FFI_OK;
  73. /*@=usedef@*/
  74. }
  75. #ifndef __CRIS__
  76. /* The CRIS ABI specifies structure elements to have byte
  77. alignment only, so it completely overrides this functions,
  78. which assumes "natural" alignment and padding. */
  79. /* Perform machine independent ffi_cif preparation, then call
  80. machine dependent routine. */
  81. #if defined(X86_DARWIN)
  82. static inline bool
  83. struct_on_stack(
  84. int size)
  85. {
  86. if (size > 8)
  87. return true;
  88. /* This is not what the ABI says, but is what is really implemented */
  89. switch (size)
  90. {
  91. case 1:
  92. case 2:
  93. case 4:
  94. case 8:
  95. return false;
  96. default:
  97. return true;
  98. }
  99. }
  100. #endif // defined(X86_DARWIN)
  101. // Arguments' ffi_type->alignment must be nonzero.
  102. ffi_status
  103. ffi_prep_cif(
  104. /*@out@*/ /*@partial@*/ ffi_cif* cif,
  105. ffi_abi abi,
  106. unsigned int nargs,
  107. /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type* rtype,
  108. /*@dependent@*/ ffi_type** atypes)
  109. {
  110. if (cif == NULL)
  111. return FFI_BAD_TYPEDEF;
  112. if (abi <= FFI_FIRST_ABI || abi > FFI_DEFAULT_ABI)
  113. return FFI_BAD_ABI;
  114. unsigned int bytes = 0;
  115. unsigned int i;
  116. ffi_type** ptr;
  117. cif->abi = abi;
  118. cif->arg_types = atypes;
  119. cif->nargs = nargs;
  120. cif->rtype = rtype;
  121. cif->flags = 0;
  122. /* Initialize the return type if necessary */
  123. /*@-usedef@*/
  124. if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
  125. return FFI_BAD_TYPEDEF;
  126. /*@=usedef@*/
  127. /* Perform a sanity check on the return type */
  128. FFI_ASSERT_VALID_TYPE(cif->rtype);
  129. /* x86-64 and s390 stack space allocation is handled in prep_machdep. */
  130. #if !defined M68K && !defined __x86_64__ && !defined S390 && !defined PA
  131. /* Make space for the return structure pointer */
  132. if (cif->rtype->type == FFI_TYPE_STRUCT
  133. #ifdef SPARC
  134. && (cif->abi != FFI_V9 || cif->rtype->size > 32)
  135. #endif
  136. #ifdef X86_DARWIN
  137. && (struct_on_stack(cif->rtype->size))
  138. #endif
  139. )
  140. bytes = STACK_ARG_SIZE(sizeof(void*));
  141. #endif
  142. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  143. {
  144. /* Initialize any uninitialized aggregate type definitions */
  145. if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
  146. return FFI_BAD_TYPEDEF;
  147. if ((*ptr)->alignment == 0)
  148. return FFI_BAD_TYPEDEF;
  149. /* Perform a sanity check on the argument type, do this
  150. check after the initialization. */
  151. FFI_ASSERT_VALID_TYPE(*ptr);
  152. #if defined(X86_DARWIN)
  153. {
  154. int align = (*ptr)->alignment;
  155. if (align > 4)
  156. align = 4;
  157. if ((align - 1) & bytes)
  158. bytes = ALIGN(bytes, align);
  159. bytes += STACK_ARG_SIZE((*ptr)->size);
  160. }
  161. #elif !defined __x86_64__ && !defined S390 && !defined PA
  162. #ifdef SPARC
  163. if (((*ptr)->type == FFI_TYPE_STRUCT
  164. && ((*ptr)->size > 16 || cif->abi != FFI_V9))
  165. || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
  166. && cif->abi != FFI_V9))
  167. bytes += sizeof(void*);
  168. else
  169. #endif
  170. {
  171. /* Add any padding if necessary */
  172. if (((*ptr)->alignment - 1) & bytes)
  173. bytes = ALIGN(bytes, (*ptr)->alignment);
  174. bytes += STACK_ARG_SIZE((*ptr)->size);
  175. }
  176. #endif
  177. }
  178. cif->bytes = bytes;
  179. /* Perform machine dependent cif processing */
  180. return ffi_prep_cif_machdep(cif);
  181. }
  182. #endif /* not __CRIS__ */