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

http://unladen-swallow.googlecode.com/ · C · 292 lines · 193 code · 37 blank · 62 comment · 24 complexity · d33de3ffb6993f186789971950f7b79b MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (C) 2004 Anthony Green
  3. Copyright (C) 2007 Free Software Foundation, Inc.
  4. Copyright (C) 2008 Red Hat, Inc.
  5. FR-V Foreign Function Interface
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. ``Software''), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be included
  14. in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. ----------------------------------------------------------------------- */
  24. #include <ffi.h>
  25. #include <ffi_common.h>
  26. #include <stdlib.h>
  27. /* ffi_prep_args is called by the assembly routine once stack space
  28. has been allocated for the function's arguments */
  29. void *ffi_prep_args(char *stack, extended_cif *ecif)
  30. {
  31. register unsigned int i;
  32. register void **p_argv;
  33. register char *argp;
  34. register ffi_type **p_arg;
  35. register int count = 0;
  36. p_argv = ecif->avalue;
  37. argp = stack;
  38. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  39. (i != 0);
  40. i--, p_arg++)
  41. {
  42. size_t z;
  43. z = (*p_arg)->size;
  44. if ((*p_arg)->type == FFI_TYPE_STRUCT)
  45. {
  46. z = sizeof(void*);
  47. *(void **) argp = *p_argv;
  48. }
  49. /* if ((*p_arg)->type == FFI_TYPE_FLOAT)
  50. {
  51. if (count > 24)
  52. {
  53. // This is going on the stack. Turn it into a double.
  54. *(double *) argp = (double) *(float*)(* p_argv);
  55. z = sizeof(double);
  56. }
  57. else
  58. *(void **) argp = *(void **)(* p_argv);
  59. } */
  60. else if (z < sizeof(int))
  61. {
  62. z = sizeof(int);
  63. switch ((*p_arg)->type)
  64. {
  65. case FFI_TYPE_SINT8:
  66. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  67. break;
  68. case FFI_TYPE_UINT8:
  69. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  70. break;
  71. case FFI_TYPE_SINT16:
  72. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  73. break;
  74. case FFI_TYPE_UINT16:
  75. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  76. break;
  77. default:
  78. FFI_ASSERT(0);
  79. }
  80. }
  81. else if (z == sizeof(int))
  82. {
  83. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  84. }
  85. else
  86. {
  87. memcpy(argp, *p_argv, z);
  88. }
  89. p_argv++;
  90. argp += z;
  91. count += z;
  92. }
  93. return (stack + ((count > 24) ? 24 : ALIGN_DOWN(count, 8)));
  94. }
  95. /* Perform machine dependent cif processing */
  96. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  97. {
  98. if (cif->rtype->type == FFI_TYPE_STRUCT)
  99. cif->flags = -1;
  100. else
  101. cif->flags = cif->rtype->size;
  102. cif->bytes = ALIGN (cif->bytes, 8);
  103. return FFI_OK;
  104. }
  105. extern void ffi_call_EABI(void *(*)(char *, extended_cif *),
  106. extended_cif *,
  107. unsigned, unsigned,
  108. unsigned *,
  109. void (*fn)(void));
  110. void ffi_call(ffi_cif *cif,
  111. void (*fn)(void),
  112. void *rvalue,
  113. void **avalue)
  114. {
  115. extended_cif ecif;
  116. ecif.cif = cif;
  117. ecif.avalue = avalue;
  118. /* If the return value is a struct and we don't have a return */
  119. /* value address then we need to make one */
  120. if ((rvalue == NULL) &&
  121. (cif->rtype->type == FFI_TYPE_STRUCT))
  122. {
  123. ecif.rvalue = alloca(cif->rtype->size);
  124. }
  125. else
  126. ecif.rvalue = rvalue;
  127. switch (cif->abi)
  128. {
  129. case FFI_EABI:
  130. ffi_call_EABI(ffi_prep_args, &ecif, cif->bytes,
  131. cif->flags, ecif.rvalue, fn);
  132. break;
  133. default:
  134. FFI_ASSERT(0);
  135. break;
  136. }
  137. }
  138. void ffi_closure_eabi (unsigned arg1, unsigned arg2, unsigned arg3,
  139. unsigned arg4, unsigned arg5, unsigned arg6)
  140. {
  141. /* This function is called by a trampoline. The trampoline stows a
  142. pointer to the ffi_closure object in gr7. We must save this
  143. pointer in a place that will persist while we do our work. */
  144. register ffi_closure *creg __asm__ ("gr7");
  145. ffi_closure *closure = creg;
  146. /* Arguments that don't fit in registers are found on the stack
  147. at a fixed offset above the current frame pointer. */
  148. register char *frame_pointer __asm__ ("fp");
  149. char *stack_args = frame_pointer + 16;
  150. /* Lay the register arguments down in a continuous chunk of memory. */
  151. unsigned register_args[6] =
  152. { arg1, arg2, arg3, arg4, arg5, arg6 };
  153. ffi_cif *cif = closure->cif;
  154. ffi_type **arg_types = cif->arg_types;
  155. void **avalue = alloca (cif->nargs * sizeof(void *));
  156. char *ptr = (char *) register_args;
  157. int i;
  158. /* Find the address of each argument. */
  159. for (i = 0; i < cif->nargs; i++)
  160. {
  161. switch (arg_types[i]->type)
  162. {
  163. case FFI_TYPE_SINT8:
  164. case FFI_TYPE_UINT8:
  165. avalue[i] = ptr + 3;
  166. break;
  167. case FFI_TYPE_SINT16:
  168. case FFI_TYPE_UINT16:
  169. avalue[i] = ptr + 2;
  170. break;
  171. case FFI_TYPE_SINT32:
  172. case FFI_TYPE_UINT32:
  173. case FFI_TYPE_FLOAT:
  174. avalue[i] = ptr;
  175. break;
  176. case FFI_TYPE_STRUCT:
  177. avalue[i] = *(void**)ptr;
  178. break;
  179. default:
  180. /* This is an 8-byte value. */
  181. avalue[i] = ptr;
  182. ptr += 4;
  183. break;
  184. }
  185. ptr += 4;
  186. /* If we've handled more arguments than fit in registers,
  187. start looking at the those passed on the stack. */
  188. if (ptr == ((char *)register_args + (6*4)))
  189. ptr = stack_args;
  190. }
  191. /* Invoke the closure. */
  192. if (cif->rtype->type == FFI_TYPE_STRUCT)
  193. {
  194. /* The caller allocates space for the return structure, and
  195. passes a pointer to this space in gr3. Use this value directly
  196. as the return value. */
  197. register void *return_struct_ptr __asm__("gr3");
  198. (closure->fun) (cif, return_struct_ptr, avalue, closure->user_data);
  199. }
  200. else
  201. {
  202. /* Allocate space for the return value and call the function. */
  203. long long rvalue;
  204. (closure->fun) (cif, &rvalue, avalue, closure->user_data);
  205. /* Functions return 4-byte or smaller results in gr8. 8-byte
  206. values also use gr9. We fill the both, even for small return
  207. values, just to avoid a branch. */
  208. asm ("ldi @(%0, #0), gr8" : : "r" (&rvalue));
  209. asm ("ldi @(%0, #0), gr9" : : "r" (&((int *) &rvalue)[1]));
  210. }
  211. }
  212. ffi_status
  213. ffi_prep_closure_loc (ffi_closure* closure,
  214. ffi_cif* cif,
  215. void (*fun)(ffi_cif*, void*, void**, void*),
  216. void *user_data,
  217. void *codeloc)
  218. {
  219. unsigned int *tramp = (unsigned int *) &closure->tramp[0];
  220. unsigned long fn = (long) ffi_closure_eabi;
  221. unsigned long cls = (long) codeloc;
  222. #ifdef __FRV_FDPIC__
  223. register void *got __asm__("gr15");
  224. #endif
  225. int i;
  226. fn = (unsigned long) ffi_closure_eabi;
  227. #ifdef __FRV_FDPIC__
  228. tramp[0] = &((unsigned int *)codeloc)[2];
  229. tramp[1] = got;
  230. tramp[2] = 0x8cfc0000 + (fn & 0xffff); /* setlos lo(fn), gr6 */
  231. tramp[3] = 0x8efc0000 + (cls & 0xffff); /* setlos lo(cls), gr7 */
  232. tramp[4] = 0x8cf80000 + (fn >> 16); /* sethi hi(fn), gr6 */
  233. tramp[5] = 0x8ef80000 + (cls >> 16); /* sethi hi(cls), gr7 */
  234. tramp[6] = 0x9cc86000; /* ldi @(gr6, #0), gr14 */
  235. tramp[7] = 0x8030e000; /* jmpl @(gr14, gr0) */
  236. #else
  237. tramp[0] = 0x8cfc0000 + (fn & 0xffff); /* setlos lo(fn), gr6 */
  238. tramp[1] = 0x8efc0000 + (cls & 0xffff); /* setlos lo(cls), gr7 */
  239. tramp[2] = 0x8cf80000 + (fn >> 16); /* sethi hi(fn), gr6 */
  240. tramp[3] = 0x8ef80000 + (cls >> 16); /* sethi hi(cls), gr7 */
  241. tramp[4] = 0x80300006; /* jmpl @(gr0, gr6) */
  242. #endif
  243. closure->cif = cif;
  244. closure->fun = fun;
  245. closure->user_data = user_data;
  246. /* Cache flushing. */
  247. for (i = 0; i < FFI_TRAMPOLINE_SIZE; i++)
  248. __asm__ volatile ("dcf @(%0,%1)\n\tici @(%2,%1)" :: "r" (tramp), "r" (i),
  249. "r" (codeloc));
  250. return FFI_OK;
  251. }