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

http://unladen-swallow.googlecode.com/ · C · 383 lines · 269 code · 59 blank · 55 comment · 54 complexity · 5d7af3480697d2ceab7f4ec35baa9d26 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998 Cygnus Solutions
  3. Copyright (c) 2004 Simon Posnjak
  4. Copyright (c) 2005 Axis Communications AB
  5. Copyright (C) 2007 Free Software Foundation, Inc.
  6. CRIS Foreign Function Interface
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. ``Software''), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be included
  15. in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL SIMON POSNJAK BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. OTHER DEALINGS IN THE SOFTWARE.
  23. ----------------------------------------------------------------------- */
  24. #include <ffi.h>
  25. #include <ffi_common.h>
  26. #define STACK_ARG_SIZE(x) ALIGN(x, FFI_SIZEOF_ARG)
  27. static ffi_status
  28. initialize_aggregate_packed_struct (ffi_type * arg)
  29. {
  30. ffi_type **ptr;
  31. FFI_ASSERT (arg != NULL);
  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)
  39. && (initialize_aggregate_packed_struct ((*ptr)) != FFI_OK))
  40. return FFI_BAD_TYPEDEF;
  41. FFI_ASSERT (ffi_type_test ((*ptr)));
  42. arg->size += (*ptr)->size;
  43. arg->alignment = (arg->alignment > (*ptr)->alignment) ?
  44. arg->alignment : (*ptr)->alignment;
  45. ptr++;
  46. }
  47. if (arg->size == 0)
  48. return FFI_BAD_TYPEDEF;
  49. else
  50. return FFI_OK;
  51. }
  52. int
  53. ffi_prep_args (char *stack, extended_cif * ecif)
  54. {
  55. unsigned int i;
  56. unsigned int struct_count = 0;
  57. void **p_argv;
  58. char *argp;
  59. ffi_type **p_arg;
  60. argp = stack;
  61. p_argv = ecif->avalue;
  62. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  63. (i != 0); i--, p_arg++)
  64. {
  65. size_t z;
  66. switch ((*p_arg)->type)
  67. {
  68. case FFI_TYPE_STRUCT:
  69. {
  70. z = (*p_arg)->size;
  71. if (z <= 4)
  72. {
  73. memcpy (argp, *p_argv, z);
  74. z = 4;
  75. }
  76. else if (z <= 8)
  77. {
  78. memcpy (argp, *p_argv, z);
  79. z = 8;
  80. }
  81. else
  82. {
  83. unsigned int uiLocOnStack;
  84. z = sizeof (void *);
  85. uiLocOnStack = 4 * ecif->cif->nargs + struct_count;
  86. struct_count = struct_count + (*p_arg)->size;
  87. *(unsigned int *) argp =
  88. (unsigned int) (UINT32 *) (stack + uiLocOnStack);
  89. memcpy ((stack + uiLocOnStack), *p_argv, (*p_arg)->size);
  90. }
  91. break;
  92. }
  93. default:
  94. z = (*p_arg)->size;
  95. if (z < sizeof (int))
  96. {
  97. switch ((*p_arg)->type)
  98. {
  99. case FFI_TYPE_SINT8:
  100. *(signed int *) argp = (signed int) *(SINT8 *) (*p_argv);
  101. break;
  102. case FFI_TYPE_UINT8:
  103. *(unsigned int *) argp =
  104. (unsigned int) *(UINT8 *) (*p_argv);
  105. break;
  106. case FFI_TYPE_SINT16:
  107. *(signed int *) argp = (signed int) *(SINT16 *) (*p_argv);
  108. break;
  109. case FFI_TYPE_UINT16:
  110. *(unsigned int *) argp =
  111. (unsigned int) *(UINT16 *) (*p_argv);
  112. break;
  113. default:
  114. FFI_ASSERT (0);
  115. }
  116. z = sizeof (int);
  117. }
  118. else if (z == sizeof (int))
  119. *(unsigned int *) argp = (unsigned int) *(UINT32 *) (*p_argv);
  120. else
  121. memcpy (argp, *p_argv, z);
  122. break;
  123. }
  124. p_argv++;
  125. argp += z;
  126. }
  127. return (struct_count);
  128. }
  129. ffi_status
  130. ffi_prep_cif (ffi_cif * cif,
  131. ffi_abi abi, unsigned int nargs,
  132. ffi_type * rtype, ffi_type ** atypes)
  133. {
  134. unsigned bytes = 0;
  135. unsigned int i;
  136. ffi_type **ptr;
  137. FFI_ASSERT (cif != NULL);
  138. FFI_ASSERT ((abi > FFI_FIRST_ABI) && (abi <= FFI_DEFAULT_ABI));
  139. cif->abi = abi;
  140. cif->arg_types = atypes;
  141. cif->nargs = nargs;
  142. cif->rtype = rtype;
  143. cif->flags = 0;
  144. if ((cif->rtype->size == 0)
  145. && (initialize_aggregate_packed_struct (cif->rtype) != FFI_OK))
  146. return FFI_BAD_TYPEDEF;
  147. FFI_ASSERT_VALID_TYPE (cif->rtype);
  148. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  149. {
  150. if (((*ptr)->size == 0)
  151. && (initialize_aggregate_packed_struct ((*ptr)) != FFI_OK))
  152. return FFI_BAD_TYPEDEF;
  153. FFI_ASSERT_VALID_TYPE (*ptr);
  154. if (((*ptr)->alignment - 1) & bytes)
  155. bytes = ALIGN (bytes, (*ptr)->alignment);
  156. if ((*ptr)->type == FFI_TYPE_STRUCT)
  157. {
  158. if ((*ptr)->size > 8)
  159. {
  160. bytes += (*ptr)->size;
  161. bytes += sizeof (void *);
  162. }
  163. else
  164. {
  165. if ((*ptr)->size > 4)
  166. bytes += 8;
  167. else
  168. bytes += 4;
  169. }
  170. }
  171. else
  172. bytes += STACK_ARG_SIZE ((*ptr)->size);
  173. }
  174. cif->bytes = bytes;
  175. return ffi_prep_cif_machdep (cif);
  176. }
  177. ffi_status
  178. ffi_prep_cif_machdep (ffi_cif * cif)
  179. {
  180. switch (cif->rtype->type)
  181. {
  182. case FFI_TYPE_VOID:
  183. case FFI_TYPE_STRUCT:
  184. case FFI_TYPE_FLOAT:
  185. case FFI_TYPE_DOUBLE:
  186. case FFI_TYPE_SINT64:
  187. case FFI_TYPE_UINT64:
  188. cif->flags = (unsigned) cif->rtype->type;
  189. break;
  190. default:
  191. cif->flags = FFI_TYPE_INT;
  192. break;
  193. }
  194. return FFI_OK;
  195. }
  196. extern void ffi_call_SYSV (int (*)(char *, extended_cif *),
  197. extended_cif *,
  198. unsigned, unsigned, unsigned *, void (*fn) ())
  199. __attribute__ ((__visibility__ ("hidden")));
  200. void
  201. ffi_call (ffi_cif * cif, void (*fn) (), void *rvalue, void **avalue)
  202. {
  203. extended_cif ecif;
  204. ecif.cif = cif;
  205. ecif.avalue = avalue;
  206. if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
  207. {
  208. ecif.rvalue = alloca (cif->rtype->size);
  209. }
  210. else
  211. ecif.rvalue = rvalue;
  212. switch (cif->abi)
  213. {
  214. case FFI_SYSV:
  215. ffi_call_SYSV (ffi_prep_args, &ecif, cif->bytes,
  216. cif->flags, ecif.rvalue, fn);
  217. break;
  218. default:
  219. FFI_ASSERT (0);
  220. break;
  221. }
  222. }
  223. /* Because the following variables are not exported outside libffi, we
  224. mark them hidden. */
  225. /* Assembly code for the jump stub. */
  226. extern const char ffi_cris_trampoline_template[]
  227. __attribute__ ((__visibility__ ("hidden")));
  228. /* Offset into ffi_cris_trampoline_template of where to put the
  229. ffi_prep_closure_inner function. */
  230. extern const int ffi_cris_trampoline_fn_offset
  231. __attribute__ ((__visibility__ ("hidden")));
  232. /* Offset into ffi_cris_trampoline_template of where to put the
  233. closure data. */
  234. extern const int ffi_cris_trampoline_closure_offset
  235. __attribute__ ((__visibility__ ("hidden")));
  236. /* This function is sibling-called (jumped to) by the closure
  237. trampoline. We get R10..R13 at PARAMS[0..3] and a copy of [SP] at
  238. PARAMS[4] to simplify handling of a straddling parameter. A copy
  239. of R9 is at PARAMS[5] and SP at PARAMS[6]. These parameters are
  240. put at the appropriate place in CLOSURE which is then executed and
  241. the return value is passed back to the caller. */
  242. static unsigned long long
  243. ffi_prep_closure_inner (void **params, ffi_closure* closure)
  244. {
  245. char *register_args = (char *) params;
  246. void *struct_ret = params[5];
  247. char *stack_args = params[6];
  248. char *ptr = register_args;
  249. ffi_cif *cif = closure->cif;
  250. ffi_type **arg_types = cif->arg_types;
  251. /* Max room needed is number of arguments as 64-bit values. */
  252. void **avalue = alloca (closure->cif->nargs * sizeof(void *));
  253. int i;
  254. int doing_regs;
  255. long long llret = 0;
  256. /* Find the address of each argument. */
  257. for (i = 0, doing_regs = 1; i < cif->nargs; i++)
  258. {
  259. /* Types up to and including 8 bytes go by-value. */
  260. if (arg_types[i]->size <= 4)
  261. {
  262. avalue[i] = ptr;
  263. ptr += 4;
  264. }
  265. else if (arg_types[i]->size <= 8)
  266. {
  267. avalue[i] = ptr;
  268. ptr += 8;
  269. }
  270. else
  271. {
  272. FFI_ASSERT (arg_types[i]->type == FFI_TYPE_STRUCT);
  273. /* Passed by-reference, so copy the pointer. */
  274. avalue[i] = *(void **) ptr;
  275. ptr += 4;
  276. }
  277. /* If we've handled more arguments than fit in registers, start
  278. looking at the those passed on the stack. Step over the
  279. first one if we had a straddling parameter. */
  280. if (doing_regs && ptr >= register_args + 4*4)
  281. {
  282. ptr = stack_args + ((ptr > register_args + 4*4) ? 4 : 0);
  283. doing_regs = 0;
  284. }
  285. }
  286. /* Invoke the closure. */
  287. (closure->fun) (cif,
  288. cif->rtype->type == FFI_TYPE_STRUCT
  289. /* The caller allocated space for the return
  290. structure, and passed a pointer to this space in
  291. R9. */
  292. ? struct_ret
  293. /* We take advantage of being able to ignore that
  294. the high part isn't set if the return value is
  295. not in R10:R11, but in R10 only. */
  296. : (void *) &llret,
  297. avalue, closure->user_data);
  298. return llret;
  299. }
  300. /* API function: Prepare the trampoline. */
  301. ffi_status
  302. ffi_prep_closure_loc (ffi_closure* closure,
  303. ffi_cif* cif,
  304. void (*fun)(ffi_cif *, void *, void **, void*),
  305. void *user_data,
  306. void *codeloc)
  307. {
  308. void *innerfn = ffi_prep_closure_inner;
  309. FFI_ASSERT (cif->abi == FFI_SYSV);
  310. closure->cif = cif;
  311. closure->user_data = user_data;
  312. closure->fun = fun;
  313. memcpy (closure->tramp, ffi_cris_trampoline_template,
  314. FFI_CRIS_TRAMPOLINE_CODE_PART_SIZE);
  315. memcpy (closure->tramp + ffi_cris_trampoline_fn_offset,
  316. &innerfn, sizeof (void *));
  317. memcpy (closure->tramp + ffi_cris_trampoline_closure_offset,
  318. &codeloc, sizeof (void *));
  319. return FFI_OK;
  320. }