/Modules/_ctypes/libffi_msvc/prep_cif.c

http://unladen-swallow.googlecode.com/ · C · 175 lines · 81 code · 34 blank · 60 comment · 42 complexity · d04d0da84b8daad705857daff41bd1fd 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 <stdlib.h>
  23. /* Round up to FFI_SIZEOF_ARG. */
  24. #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
  25. /* Perform machine independent initialization of aggregate type
  26. specifications. */
  27. static ffi_status initialize_aggregate(/*@out@*/ ffi_type *arg)
  28. {
  29. ffi_type **ptr;
  30. FFI_ASSERT(arg != NULL);
  31. /*@-usedef@*/
  32. FFI_ASSERT(arg->elements != NULL);
  33. FFI_ASSERT(arg->size == 0);
  34. FFI_ASSERT(arg->alignment == 0);
  35. ptr = &(arg->elements[0]);
  36. while ((*ptr) != NULL)
  37. {
  38. if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
  39. return FFI_BAD_TYPEDEF;
  40. /* Perform a sanity check on the argument type */
  41. FFI_ASSERT_VALID_TYPE(*ptr);
  42. arg->size = ALIGN(arg->size, (*ptr)->alignment);
  43. arg->size += (*ptr)->size;
  44. arg->alignment = (arg->alignment > (*ptr)->alignment) ?
  45. arg->alignment : (*ptr)->alignment;
  46. ptr++;
  47. }
  48. /* Structure size includes tail padding. This is important for
  49. structures that fit in one register on ABIs like the PowerPC64
  50. Linux ABI that right justify small structs in a register.
  51. It's also needed for nested structure layout, for example
  52. struct A { long a; char b; }; struct B { struct A x; char y; };
  53. should find y at an offset of 2*sizeof(long) and result in a
  54. total size of 3*sizeof(long). */
  55. arg->size = ALIGN (arg->size, arg->alignment);
  56. if (arg->size == 0)
  57. return FFI_BAD_TYPEDEF;
  58. else
  59. return FFI_OK;
  60. /*@=usedef@*/
  61. }
  62. /* Perform machine independent ffi_cif preparation, then call
  63. machine dependent routine. */
  64. ffi_status ffi_prep_cif(/*@out@*/ /*@partial@*/ ffi_cif *cif,
  65. ffi_abi abi, unsigned int nargs,
  66. /*@dependent@*/ /*@out@*/ /*@partial@*/ ffi_type *rtype,
  67. /*@dependent@*/ ffi_type **atypes)
  68. {
  69. unsigned bytes = 0;
  70. unsigned int i;
  71. ffi_type **ptr;
  72. FFI_ASSERT(cif != NULL);
  73. FFI_ASSERT((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
  74. cif->abi = abi;
  75. cif->arg_types = atypes;
  76. cif->nargs = nargs;
  77. cif->rtype = rtype;
  78. cif->flags = 0;
  79. /* Initialize the return type if necessary */
  80. /*@-usedef@*/
  81. if ((cif->rtype->size == 0) && (initialize_aggregate(cif->rtype) != FFI_OK))
  82. return FFI_BAD_TYPEDEF;
  83. /*@=usedef@*/
  84. /* Perform a sanity check on the return type */
  85. FFI_ASSERT_VALID_TYPE(cif->rtype);
  86. /* x86-64 and s390 stack space allocation is handled in prep_machdep. */
  87. #if !defined M68K && !defined __x86_64__ && !defined S390
  88. /* Make space for the return structure pointer */
  89. if (cif->rtype->type == FFI_TYPE_STRUCT
  90. /* MSVC returns small structures in registers. But we have a different
  91. workaround: pretend int32 or int64 return type, and converting to
  92. structure afterwards. */
  93. #ifdef SPARC
  94. && (cif->abi != FFI_V9 || cif->rtype->size > 32)
  95. #endif
  96. )
  97. bytes = STACK_ARG_SIZE(sizeof(void*));
  98. #endif
  99. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  100. {
  101. /* Initialize any uninitialized aggregate type definitions */
  102. if (((*ptr)->size == 0) && (initialize_aggregate((*ptr)) != FFI_OK))
  103. return FFI_BAD_TYPEDEF;
  104. /* Perform a sanity check on the argument type, do this
  105. check after the initialization. */
  106. FFI_ASSERT_VALID_TYPE(*ptr);
  107. #if !defined __x86_64__ && !defined S390
  108. #ifdef SPARC
  109. if (((*ptr)->type == FFI_TYPE_STRUCT
  110. && ((*ptr)->size > 16 || cif->abi != FFI_V9))
  111. || ((*ptr)->type == FFI_TYPE_LONGDOUBLE
  112. && cif->abi != FFI_V9))
  113. bytes += sizeof(void*);
  114. else
  115. #endif
  116. {
  117. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  118. /* Don't know if this is a libffi bug or not. At least on
  119. Windows with MSVC, function call parameters are *not*
  120. aligned in the same way as structure fields are, they are
  121. only aligned in integer boundaries.
  122. This doesn't do any harm for cdecl functions and closures,
  123. since the caller cleans up the stack, but it is wrong for
  124. stdcall functions where the callee cleans.
  125. */
  126. /* Add any padding if necessary */
  127. if (((*ptr)->alignment - 1) & bytes)
  128. bytes = ALIGN(bytes, (*ptr)->alignment);
  129. #endif
  130. bytes += STACK_ARG_SIZE((*ptr)->size);
  131. }
  132. #endif
  133. }
  134. cif->bytes = bytes;
  135. /* Perform machine dependent cif processing */
  136. return ffi_prep_cif_machdep(cif);
  137. }