/Modules/_ctypes/libffi_arm_wince/ffi.c

http://unladen-swallow.googlecode.com/ · C · 310 lines · 191 code · 55 blank · 64 comment · 25 complexity · b1b44ba73a2612b22815f031ca639604 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998 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, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  17. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. OTHER DEALINGS IN THE SOFTWARE.
  20. ----------------------------------------------------------------------- */
  21. #include <ffi.h>
  22. #include <ffi_common.h>
  23. #include <stdlib.h>
  24. #ifdef _WIN32_WCE
  25. #pragma warning (disable : 4142) /* benign redefinition of type */
  26. #include <windows.h>
  27. #endif
  28. /* ffi_prep_args is called by the assembly routine once stack space
  29. has been allocated for the function's arguments */
  30. /*@-exportheader@*/
  31. void ffi_prep_args(char *stack, extended_cif *ecif)
  32. /*@=exportheader@*/
  33. {
  34. register unsigned int i;
  35. register void **p_argv;
  36. register char *argp;
  37. register ffi_type **p_arg;
  38. argp = stack;
  39. if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) {
  40. *(void **) argp = ecif->rvalue;
  41. argp += 4;
  42. }
  43. p_argv = ecif->avalue;
  44. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  45. (i != 0);
  46. i--, p_arg++)
  47. {
  48. size_t z;
  49. size_t argalign = (*p_arg)->alignment;
  50. #ifdef _WIN32_WCE
  51. if (argalign > 4)
  52. argalign = 4;
  53. #endif
  54. /* Align if necessary */
  55. if ((argalign - 1) & (unsigned) argp) {
  56. argp = (char *) ALIGN(argp, argalign);
  57. }
  58. z = (*p_arg)->size;
  59. if (z < sizeof(int))
  60. {
  61. z = sizeof(int);
  62. switch ((*p_arg)->type)
  63. {
  64. case FFI_TYPE_SINT8:
  65. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  66. break;
  67. case FFI_TYPE_UINT8:
  68. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  69. break;
  70. case FFI_TYPE_SINT16:
  71. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  72. break;
  73. case FFI_TYPE_UINT16:
  74. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  75. break;
  76. case FFI_TYPE_STRUCT:
  77. /* *p_argv may not be aligned for a UINT32 */
  78. memcpy(argp, *p_argv, z);
  79. break;
  80. default:
  81. FFI_ASSERT(0);
  82. }
  83. }
  84. else if (z == sizeof(int))
  85. {
  86. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  87. }
  88. else
  89. {
  90. memcpy(argp, *p_argv, z);
  91. }
  92. p_argv++;
  93. argp += z;
  94. }
  95. return;
  96. }
  97. /* Perform machine dependent cif processing */
  98. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  99. {
  100. /* Set the return type flag */
  101. switch (cif->rtype->type)
  102. {
  103. case FFI_TYPE_VOID:
  104. case FFI_TYPE_STRUCT:
  105. case FFI_TYPE_FLOAT:
  106. case FFI_TYPE_DOUBLE:
  107. case FFI_TYPE_SINT64:
  108. cif->flags = (unsigned) cif->rtype->type;
  109. break;
  110. case FFI_TYPE_UINT64:
  111. cif->flags = FFI_TYPE_SINT64;
  112. break;
  113. default:
  114. cif->flags = FFI_TYPE_INT;
  115. break;
  116. }
  117. return FFI_OK;
  118. }
  119. /*@-declundef@*/
  120. /*@-exportheader@*/
  121. extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
  122. /*@out@*/ extended_cif *,
  123. unsigned, unsigned,
  124. /*@out@*/ unsigned *,
  125. void (*fn)());
  126. /*@=declundef@*/
  127. /*@=exportheader@*/
  128. /* Return type changed from void for ctypes */
  129. int ffi_call(/*@dependent@*/ ffi_cif *cif,
  130. void (*fn)(),
  131. /*@out@*/ void *rvalue,
  132. /*@dependent@*/ void **avalue)
  133. {
  134. extended_cif ecif;
  135. ecif.cif = cif;
  136. ecif.avalue = avalue;
  137. /* If the return value is a struct and we don't have a return */
  138. /* value address then we need to make one */
  139. if ((rvalue == NULL) &&
  140. (cif->rtype->type == FFI_TYPE_STRUCT))
  141. {
  142. /*@-sysunrecog@*/
  143. ecif.rvalue = alloca(cif->rtype->size);
  144. /*@=sysunrecog@*/
  145. }
  146. else
  147. ecif.rvalue = rvalue;
  148. switch (cif->abi)
  149. {
  150. case FFI_SYSV:
  151. /*@-usedef@*/
  152. ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
  153. cif->flags, ecif.rvalue, fn);
  154. /*@=usedef@*/
  155. break;
  156. default:
  157. FFI_ASSERT(0);
  158. break;
  159. }
  160. /* I think calculating the real stack pointer delta is not useful
  161. because stdcall is not supported */
  162. return 0;
  163. }
  164. /** private members **/
  165. static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
  166. void** args, ffi_cif* cif);
  167. /* This function is called by ffi_closure_SYSV in sysv.asm */
  168. unsigned int
  169. ffi_closure_SYSV_inner (ffi_closure *closure, char *in_args, void *rvalue)
  170. {
  171. ffi_cif *cif = closure->cif;
  172. void **out_args;
  173. out_args = (void **) alloca(cif->nargs * sizeof (void *));
  174. /* this call will initialize out_args, such that each
  175. * element in that array points to the corresponding
  176. * value on the stack; and if the function returns
  177. * a structure, it will re-set rvalue to point to the
  178. * structure return address. */
  179. ffi_prep_incoming_args_SYSV(in_args, &rvalue, out_args, cif);
  180. (closure->fun)(cif, rvalue, out_args, closure->user_data);
  181. /* Tell ffi_closure_SYSV what the returntype is */
  182. return cif->flags;
  183. }
  184. /*@-exportheader@*/
  185. static void
  186. ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
  187. void **avalue, ffi_cif *cif)
  188. /*@=exportheader@*/
  189. {
  190. unsigned int i;
  191. void **p_argv;
  192. char *argp;
  193. ffi_type **p_arg;
  194. argp = stack;
  195. if ( cif->rtype->type == FFI_TYPE_STRUCT ) {
  196. *rvalue = *(void **) argp;
  197. argp += 4;
  198. }
  199. p_argv = avalue;
  200. for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
  201. {
  202. size_t z;
  203. size_t argalign = (*p_arg)->alignment;
  204. #ifdef _WIN32_WCE
  205. if (argalign > 4)
  206. argalign = 4;
  207. #endif
  208. /* Align if necessary */
  209. if ((argalign - 1) & (unsigned) argp) {
  210. argp = (char *) ALIGN(argp, argalign);
  211. }
  212. z = (*p_arg)->size;
  213. if (z < sizeof(int))
  214. z = sizeof(int);
  215. *p_argv = (void*) argp;
  216. p_argv++;
  217. argp += z;
  218. }
  219. }
  220. /*
  221. add ip, pc, #-8 ; ip = address of this trampoline == address of ffi_closure
  222. ldr pc, [pc, #-4] ; jump to __fun
  223. DCD __fun
  224. */
  225. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN) \
  226. { \
  227. unsigned int *__tramp = (unsigned int *)(TRAMP); \
  228. __tramp[0] = 0xe24fc008; /* add ip, pc, #-8 */ \
  229. __tramp[1] = 0xe51ff004; /* ldr pc, [pc, #-4] */ \
  230. __tramp[2] = (unsigned int)(FUN); \
  231. }
  232. /* the cif must already be prep'ed */
  233. /* defined in sysv.asm */
  234. void ffi_closure_SYSV(void);
  235. ffi_status
  236. ffi_prep_closure (ffi_closure* closure,
  237. ffi_cif* cif,
  238. void (*fun)(ffi_cif*,void*,void**,void*),
  239. void *user_data)
  240. {
  241. FFI_ASSERT (cif->abi == FFI_SYSV);
  242. FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_SYSV);
  243. closure->cif = cif;
  244. closure->user_data = user_data;
  245. closure->fun = fun;
  246. #ifdef _WIN32_WCE
  247. /* This is important to allow calling the trampoline safely */
  248. FlushInstructionCache(GetCurrentProcess(), 0, 0);
  249. #endif
  250. return FFI_OK;
  251. }