/Modules/_ctypes/libffi_osx/x86/x86-ffi_darwin.c

http://unladen-swallow.googlecode.com/ · C · 436 lines · 296 code · 83 blank · 57 comment · 40 complexity · 183ca90b67e7ce5d3e8a48512b4c93e5 MD5 · raw file

  1. #ifdef __i386__
  2. /* -----------------------------------------------------------------------
  3. ffi.c - Copyright (c) 1996, 1998, 1999, 2001 Red Hat, Inc.
  4. Copyright (c) 2002 Ranjit Mathew
  5. Copyright (c) 2002 Bo Thorsen
  6. Copyright (c) 2002 Roger Sayle
  7. x86 Foreign Function Interface
  8. Permission is hereby granted, free of charge, to any person obtaining
  9. a copy of this software and associated documentation files (the
  10. ``Software''), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, sublicense, and/or sell copies of the Software, and to
  13. permit persons to whom the Software is furnished to do so, subject to
  14. the following conditions:
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. OTHER DEALINGS IN THE SOFTWARE.
  24. ----------------------------------------------------------------------- */
  25. #include <ffi.h>
  26. #include <ffi_common.h>
  27. #include <stdlib.h>
  28. /* ffi_prep_args is called by the assembly routine once stack space
  29. has been allocated for the function's arguments */
  30. void ffi_prep_args(char *stack, extended_cif *ecif)
  31. {
  32. register unsigned int i;
  33. register void **p_argv;
  34. register char *argp;
  35. register ffi_type **p_arg;
  36. argp = stack;
  37. if (ecif->cif->flags == FFI_TYPE_STRUCT)
  38. {
  39. *(void **) argp = ecif->rvalue;
  40. argp += 4;
  41. }
  42. p_argv = ecif->avalue;
  43. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  44. i != 0;
  45. i--, p_arg++)
  46. {
  47. size_t z;
  48. /* Align if necessary */
  49. if ((sizeof(int) - 1) & (unsigned) argp)
  50. argp = (char *) ALIGN(argp, sizeof(int));
  51. z = (*p_arg)->size;
  52. if (z < sizeof(int))
  53. {
  54. z = sizeof(int);
  55. switch ((*p_arg)->type)
  56. {
  57. case FFI_TYPE_SINT8:
  58. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  59. break;
  60. case FFI_TYPE_UINT8:
  61. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  62. break;
  63. case FFI_TYPE_SINT16:
  64. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  65. break;
  66. case FFI_TYPE_UINT16:
  67. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  68. break;
  69. case FFI_TYPE_SINT32:
  70. *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
  71. break;
  72. case FFI_TYPE_UINT32:
  73. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  74. break;
  75. case FFI_TYPE_STRUCT:
  76. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  77. break;
  78. default:
  79. FFI_ASSERT(0);
  80. }
  81. }
  82. else
  83. {
  84. memcpy(argp, *p_argv, z);
  85. }
  86. p_argv++;
  87. argp += z;
  88. }
  89. return;
  90. }
  91. /* Perform machine dependent cif processing */
  92. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  93. {
  94. /* Set the return type flag */
  95. switch (cif->rtype->type)
  96. {
  97. case FFI_TYPE_VOID:
  98. #ifdef X86
  99. case FFI_TYPE_STRUCT:
  100. case FFI_TYPE_UINT8:
  101. case FFI_TYPE_UINT16:
  102. case FFI_TYPE_SINT8:
  103. case FFI_TYPE_SINT16:
  104. #endif
  105. case FFI_TYPE_SINT64:
  106. case FFI_TYPE_FLOAT:
  107. case FFI_TYPE_DOUBLE:
  108. case FFI_TYPE_LONGDOUBLE:
  109. cif->flags = (unsigned) cif->rtype->type;
  110. break;
  111. case FFI_TYPE_UINT64:
  112. cif->flags = FFI_TYPE_SINT64;
  113. break;
  114. #ifndef X86
  115. case FFI_TYPE_STRUCT:
  116. if (cif->rtype->size == 1)
  117. {
  118. cif->flags = FFI_TYPE_SINT8; /* same as char size */
  119. }
  120. else if (cif->rtype->size == 2)
  121. {
  122. cif->flags = FFI_TYPE_SINT16; /* same as short size */
  123. }
  124. else if (cif->rtype->size == 4)
  125. {
  126. cif->flags = FFI_TYPE_INT; /* same as int type */
  127. }
  128. else if (cif->rtype->size == 8)
  129. {
  130. cif->flags = FFI_TYPE_SINT64; /* same as int64 type */
  131. }
  132. else
  133. {
  134. cif->flags = FFI_TYPE_STRUCT;
  135. }
  136. break;
  137. #endif
  138. default:
  139. cif->flags = FFI_TYPE_INT;
  140. break;
  141. }
  142. #ifdef X86_DARWIN
  143. cif->bytes = (cif->bytes + 15) & ~0xF;
  144. #endif
  145. return FFI_OK;
  146. }
  147. extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
  148. unsigned, unsigned, unsigned *, void (*fn)());
  149. #ifdef X86_WIN32
  150. extern void ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *,
  151. unsigned, unsigned, unsigned *, void (*fn)());
  152. #endif /* X86_WIN32 */
  153. void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
  154. {
  155. extended_cif ecif;
  156. ecif.cif = cif;
  157. ecif.avalue = avalue;
  158. /* If the return value is a struct and we don't have a return */
  159. /* value address then we need to make one */
  160. if ((rvalue == NULL) &&
  161. (cif->flags == FFI_TYPE_STRUCT))
  162. {
  163. ecif.rvalue = alloca(cif->rtype->size);
  164. }
  165. else
  166. ecif.rvalue = rvalue;
  167. switch (cif->abi)
  168. {
  169. case FFI_SYSV:
  170. ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
  171. fn);
  172. break;
  173. #ifdef X86_WIN32
  174. case FFI_STDCALL:
  175. ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes, cif->flags,
  176. ecif.rvalue, fn);
  177. break;
  178. #endif /* X86_WIN32 */
  179. default:
  180. FFI_ASSERT(0);
  181. break;
  182. }
  183. }
  184. /** private members **/
  185. static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
  186. void** args, ffi_cif* cif);
  187. void FFI_HIDDEN ffi_closure_SYSV (ffi_closure *)
  188. __attribute__ ((regparm(1)));
  189. unsigned int FFI_HIDDEN ffi_closure_SYSV_inner (ffi_closure *, void **, void *)
  190. __attribute__ ((regparm(1)));
  191. void FFI_HIDDEN ffi_closure_raw_SYSV (ffi_raw_closure *)
  192. __attribute__ ((regparm(1)));
  193. /* This function is jumped to by the trampoline */
  194. unsigned int FFI_HIDDEN
  195. ffi_closure_SYSV_inner (closure, respp, args)
  196. ffi_closure *closure;
  197. void **respp;
  198. void *args;
  199. {
  200. // our various things...
  201. ffi_cif *cif;
  202. void **arg_area;
  203. cif = closure->cif;
  204. arg_area = (void**) alloca (cif->nargs * sizeof (void*));
  205. /* this call will initialize ARG_AREA, such that each
  206. * element in that array points to the corresponding
  207. * value on the stack; and if the function returns
  208. * a structure, it will re-set RESP to point to the
  209. * structure return address. */
  210. ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
  211. (closure->fun) (cif, *respp, arg_area, closure->user_data);
  212. return cif->flags;
  213. }
  214. static void
  215. ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, void **avalue,
  216. ffi_cif *cif)
  217. {
  218. register unsigned int i;
  219. register void **p_argv;
  220. register char *argp;
  221. register ffi_type **p_arg;
  222. argp = stack;
  223. if ( cif->flags == FFI_TYPE_STRUCT ) {
  224. *rvalue = *(void **) argp;
  225. argp += 4;
  226. }
  227. p_argv = avalue;
  228. for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
  229. {
  230. size_t z;
  231. /* Align if necessary */
  232. if ((sizeof(int) - 1) & (unsigned) argp) {
  233. argp = (char *) ALIGN(argp, sizeof(int));
  234. }
  235. z = (*p_arg)->size;
  236. /* because we're little endian, this is what it turns into. */
  237. *p_argv = (void*) argp;
  238. p_argv++;
  239. argp += z;
  240. }
  241. return;
  242. }
  243. /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
  244. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
  245. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  246. unsigned int __fun = (unsigned int)(FUN); \
  247. unsigned int __ctx = (unsigned int)(CTX); \
  248. unsigned int __dis = __fun - (__ctx + FFI_TRAMPOLINE_SIZE); \
  249. *(unsigned char*) &__tramp[0] = 0xb8; \
  250. *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
  251. *(unsigned char *) &__tramp[5] = 0xe9; \
  252. *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \
  253. })
  254. /* the cif must already be prep'ed */
  255. ffi_status
  256. ffi_prep_closure (ffi_closure* closure,
  257. ffi_cif* cif,
  258. void (*fun)(ffi_cif*,void*,void**,void*),
  259. void *user_data)
  260. {
  261. if (cif->abi != FFI_SYSV)
  262. return FFI_BAD_ABI;
  263. FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
  264. &ffi_closure_SYSV, \
  265. (void*)closure);
  266. closure->cif = cif;
  267. closure->user_data = user_data;
  268. closure->fun = fun;
  269. return FFI_OK;
  270. }
  271. /* ------- Native raw API support -------------------------------- */
  272. #if !FFI_NO_RAW_API
  273. ffi_status
  274. ffi_prep_raw_closure_loc (ffi_raw_closure* closure,
  275. ffi_cif* cif,
  276. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  277. void *user_data,
  278. void *codeloc)
  279. {
  280. int i;
  281. FFI_ASSERT (cif->abi == FFI_SYSV);
  282. // we currently don't support certain kinds of arguments for raw
  283. // closures. This should be implemented by a separate assembly language
  284. // routine, since it would require argument processing, something we
  285. // don't do now for performance.
  286. for (i = cif->nargs-1; i >= 0; i--)
  287. {
  288. FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
  289. FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
  290. }
  291. FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
  292. codeloc);
  293. closure->cif = cif;
  294. closure->user_data = user_data;
  295. closure->fun = fun;
  296. return FFI_OK;
  297. }
  298. static void
  299. ffi_prep_args_raw(char *stack, extended_cif *ecif)
  300. {
  301. memcpy (stack, ecif->avalue, ecif->cif->bytes);
  302. }
  303. /* we borrow this routine from libffi (it must be changed, though, to
  304. * actually call the function passed in the first argument. as of
  305. * libffi-1.20, this is not the case.)
  306. */
  307. extern void
  308. ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *, unsigned,
  309. unsigned, unsigned *, void (*fn)());
  310. #ifdef X86_WIN32
  311. extern void
  312. ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *, unsigned,
  313. unsigned, unsigned *, void (*fn)());
  314. #endif /* X86_WIN32 */
  315. void
  316. ffi_raw_call(ffi_cif *cif, void (*fn)(), void *rvalue, ffi_raw *fake_avalue)
  317. {
  318. extended_cif ecif;
  319. void **avalue = (void **)fake_avalue;
  320. ecif.cif = cif;
  321. ecif.avalue = avalue;
  322. /* If the return value is a struct and we don't have a return */
  323. /* value address then we need to make one */
  324. if ((rvalue == NULL) &&
  325. (cif->rtype->type == FFI_TYPE_STRUCT))
  326. {
  327. ecif.rvalue = alloca(cif->rtype->size);
  328. }
  329. else
  330. ecif.rvalue = rvalue;
  331. switch (cif->abi)
  332. {
  333. case FFI_SYSV:
  334. ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
  335. ecif.rvalue, fn);
  336. break;
  337. #ifdef X86_WIN32
  338. case FFI_STDCALL:
  339. ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
  340. ecif.rvalue, fn);
  341. break;
  342. #endif /* X86_WIN32 */
  343. default:
  344. FFI_ASSERT(0);
  345. break;
  346. }
  347. }
  348. #endif
  349. #endif // __i386__