/Modules/_ctypes/libffi/src/arm/ffi.c

http://unladen-swallow.googlecode.com/ · C · 309 lines · 195 code · 61 blank · 53 comment · 32 complexity · a0ee6e86ca97fcdd084efc21500ebccd MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998, 2008 Red Hat, Inc.
  3. ARM Foreign Function Interface
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. ``Software''), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. #include <ffi.h>
  23. #include <ffi_common.h>
  24. #include <stdlib.h>
  25. /* ffi_prep_args is called by the assembly routine once stack space
  26. has been allocated for the function's arguments */
  27. void ffi_prep_args(char *stack, extended_cif *ecif)
  28. {
  29. register unsigned int i;
  30. register void **p_argv;
  31. register char *argp;
  32. register ffi_type **p_arg;
  33. argp = stack;
  34. if ( ecif->cif->flags == FFI_TYPE_STRUCT ) {
  35. *(void **) argp = ecif->rvalue;
  36. argp += 4;
  37. }
  38. p_argv = ecif->avalue;
  39. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  40. (i != 0);
  41. i--, p_arg++)
  42. {
  43. size_t z;
  44. /* Align if necessary */
  45. if (((*p_arg)->alignment - 1) & (unsigned) argp) {
  46. argp = (char *) ALIGN(argp, (*p_arg)->alignment);
  47. }
  48. if ((*p_arg)->type == FFI_TYPE_STRUCT)
  49. argp = (char *) ALIGN(argp, 4);
  50. z = (*p_arg)->size;
  51. if (z < sizeof(int))
  52. {
  53. z = sizeof(int);
  54. switch ((*p_arg)->type)
  55. {
  56. case FFI_TYPE_SINT8:
  57. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  58. break;
  59. case FFI_TYPE_UINT8:
  60. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  61. break;
  62. case FFI_TYPE_SINT16:
  63. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  64. break;
  65. case FFI_TYPE_UINT16:
  66. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  67. break;
  68. case FFI_TYPE_STRUCT:
  69. memcpy(argp, *p_argv, (*p_arg)->size);
  70. break;
  71. default:
  72. FFI_ASSERT(0);
  73. }
  74. }
  75. else if (z == sizeof(int))
  76. {
  77. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  78. }
  79. else
  80. {
  81. memcpy(argp, *p_argv, z);
  82. }
  83. p_argv++;
  84. argp += z;
  85. }
  86. return;
  87. }
  88. /* Perform machine dependent cif processing */
  89. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  90. {
  91. /* Round the stack up to a multiple of 8 bytes. This isn't needed
  92. everywhere, but it is on some platforms, and it doesn't harm anything
  93. when it isn't needed. */
  94. cif->bytes = (cif->bytes + 7) & ~7;
  95. /* Set the return type flag */
  96. switch (cif->rtype->type)
  97. {
  98. case FFI_TYPE_VOID:
  99. case FFI_TYPE_FLOAT:
  100. case FFI_TYPE_DOUBLE:
  101. cif->flags = (unsigned) cif->rtype->type;
  102. break;
  103. case FFI_TYPE_SINT64:
  104. case FFI_TYPE_UINT64:
  105. cif->flags = (unsigned) FFI_TYPE_SINT64;
  106. break;
  107. case FFI_TYPE_STRUCT:
  108. if (cif->rtype->size <= 4)
  109. /* A Composite Type not larger than 4 bytes is returned in r0. */
  110. cif->flags = (unsigned)FFI_TYPE_INT;
  111. else
  112. /* A Composite Type larger than 4 bytes, or whose size cannot
  113. be determined statically ... is stored in memory at an
  114. address passed [in r0]. */
  115. cif->flags = (unsigned)FFI_TYPE_STRUCT;
  116. break;
  117. default:
  118. cif->flags = FFI_TYPE_INT;
  119. break;
  120. }
  121. return FFI_OK;
  122. }
  123. extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
  124. unsigned, unsigned, unsigned *, void (*fn)(void));
  125. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  126. {
  127. extended_cif ecif;
  128. int small_struct = (cif->flags == FFI_TYPE_INT
  129. && cif->rtype->type == FFI_TYPE_STRUCT);
  130. ecif.cif = cif;
  131. ecif.avalue = avalue;
  132. unsigned int temp;
  133. /* If the return value is a struct and we don't have a return */
  134. /* value address then we need to make one */
  135. if ((rvalue == NULL) &&
  136. (cif->flags == FFI_TYPE_STRUCT))
  137. {
  138. ecif.rvalue = alloca(cif->rtype->size);
  139. }
  140. else if (small_struct)
  141. ecif.rvalue = &temp;
  142. else
  143. ecif.rvalue = rvalue;
  144. switch (cif->abi)
  145. {
  146. case FFI_SYSV:
  147. ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
  148. fn);
  149. break;
  150. default:
  151. FFI_ASSERT(0);
  152. break;
  153. }
  154. if (small_struct)
  155. memcpy (rvalue, &temp, cif->rtype->size);
  156. }
  157. /** private members **/
  158. static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
  159. void** args, ffi_cif* cif);
  160. void ffi_closure_SYSV (ffi_closure *);
  161. /* This function is jumped to by the trampoline */
  162. unsigned int
  163. ffi_closure_SYSV_inner (closure, respp, args)
  164. ffi_closure *closure;
  165. void **respp;
  166. void *args;
  167. {
  168. // our various things...
  169. ffi_cif *cif;
  170. void **arg_area;
  171. cif = closure->cif;
  172. arg_area = (void**) alloca (cif->nargs * sizeof (void*));
  173. /* this call will initialize ARG_AREA, such that each
  174. * element in that array points to the corresponding
  175. * value on the stack; and if the function returns
  176. * a structure, it will re-set RESP to point to the
  177. * structure return address. */
  178. ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
  179. (closure->fun) (cif, *respp, arg_area, closure->user_data);
  180. return cif->flags;
  181. }
  182. /*@-exportheader@*/
  183. static void
  184. ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
  185. void **avalue, ffi_cif *cif)
  186. /*@=exportheader@*/
  187. {
  188. register unsigned int i;
  189. register void **p_argv;
  190. register char *argp;
  191. register ffi_type **p_arg;
  192. argp = stack;
  193. if ( cif->flags == FFI_TYPE_STRUCT ) {
  194. *rvalue = *(void **) argp;
  195. argp += 4;
  196. }
  197. p_argv = avalue;
  198. for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
  199. {
  200. size_t z;
  201. size_t alignment = (*p_arg)->alignment;
  202. if (alignment < 4)
  203. alignment = 4;
  204. /* Align if necessary */
  205. if ((alignment - 1) & (unsigned) argp) {
  206. argp = (char *) ALIGN(argp, alignment);
  207. }
  208. z = (*p_arg)->size;
  209. /* because we're little endian, this is what it turns into. */
  210. *p_argv = (void*) argp;
  211. p_argv++;
  212. argp += z;
  213. }
  214. return;
  215. }
  216. /* How to make a trampoline. */
  217. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
  218. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  219. unsigned int __fun = (unsigned int)(FUN); \
  220. unsigned int __ctx = (unsigned int)(CTX); \
  221. *(unsigned int*) &__tramp[0] = 0xe92d000f; /* stmfd sp!, {r0-r3} */ \
  222. *(unsigned int*) &__tramp[4] = 0xe59f0000; /* ldr r0, [pc] */ \
  223. *(unsigned int*) &__tramp[8] = 0xe59ff000; /* ldr pc, [pc] */ \
  224. *(unsigned int*) &__tramp[12] = __ctx; \
  225. *(unsigned int*) &__tramp[16] = __fun; \
  226. __clear_cache((&__tramp[0]), (&__tramp[19])); \
  227. })
  228. /* the cif must already be prep'ed */
  229. ffi_status
  230. ffi_prep_closure_loc (ffi_closure* closure,
  231. ffi_cif* cif,
  232. void (*fun)(ffi_cif*,void*,void**,void*),
  233. void *user_data,
  234. void *codeloc)
  235. {
  236. FFI_ASSERT (cif->abi == FFI_SYSV);
  237. FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
  238. &ffi_closure_SYSV, \
  239. codeloc);
  240. closure->cif = cif;
  241. closure->user_data = user_data;
  242. closure->fun = fun;
  243. return FFI_OK;
  244. }