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

http://unladen-swallow.googlecode.com/ · C · 284 lines · 181 code · 50 blank · 53 comment · 18 complexity · 6c0cd4327058ec8585b09041f1501c2c MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998, 2001, 2007, 2008 Red Hat, Inc.
  3. Alpha 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. /* Force FFI_TYPE_LONGDOUBLE to be different than FFI_TYPE_DOUBLE;
  26. all further uses in this file will refer to the 128-bit type. */
  27. #if defined(__LONG_DOUBLE_128__)
  28. # if FFI_TYPE_LONGDOUBLE != 4
  29. # error FFI_TYPE_LONGDOUBLE out of date
  30. # endif
  31. #else
  32. # undef FFI_TYPE_LONGDOUBLE
  33. # define FFI_TYPE_LONGDOUBLE 4
  34. #endif
  35. extern void ffi_call_osf(void *, unsigned long, unsigned, void *, void (*)(void))
  36. FFI_HIDDEN;
  37. extern void ffi_closure_osf(void) FFI_HIDDEN;
  38. ffi_status
  39. ffi_prep_cif_machdep(ffi_cif *cif)
  40. {
  41. /* Adjust cif->bytes to represent a minimum 6 words for the temporary
  42. register argument loading area. */
  43. if (cif->bytes < 6*FFI_SIZEOF_ARG)
  44. cif->bytes = 6*FFI_SIZEOF_ARG;
  45. /* Set the return type flag */
  46. switch (cif->rtype->type)
  47. {
  48. case FFI_TYPE_STRUCT:
  49. case FFI_TYPE_FLOAT:
  50. case FFI_TYPE_DOUBLE:
  51. cif->flags = cif->rtype->type;
  52. break;
  53. case FFI_TYPE_LONGDOUBLE:
  54. /* 128-bit long double is returned in memory, like a struct. */
  55. cif->flags = FFI_TYPE_STRUCT;
  56. break;
  57. default:
  58. cif->flags = FFI_TYPE_INT;
  59. break;
  60. }
  61. return FFI_OK;
  62. }
  63. void
  64. ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  65. {
  66. unsigned long *stack, *argp;
  67. long i, avn;
  68. ffi_type **arg_types;
  69. /* If the return value is a struct and we don't have a return
  70. value address then we need to make one. */
  71. if (rvalue == NULL && cif->flags == FFI_TYPE_STRUCT)
  72. rvalue = alloca(cif->rtype->size);
  73. /* Allocate the space for the arguments, plus 4 words of temp
  74. space for ffi_call_osf. */
  75. argp = stack = alloca(cif->bytes + 4*FFI_SIZEOF_ARG);
  76. if (cif->flags == FFI_TYPE_STRUCT)
  77. *(void **) argp++ = rvalue;
  78. i = 0;
  79. avn = cif->nargs;
  80. arg_types = cif->arg_types;
  81. while (i < avn)
  82. {
  83. size_t size = (*arg_types)->size;
  84. switch ((*arg_types)->type)
  85. {
  86. case FFI_TYPE_SINT8:
  87. *(SINT64 *) argp = *(SINT8 *)(* avalue);
  88. break;
  89. case FFI_TYPE_UINT8:
  90. *(SINT64 *) argp = *(UINT8 *)(* avalue);
  91. break;
  92. case FFI_TYPE_SINT16:
  93. *(SINT64 *) argp = *(SINT16 *)(* avalue);
  94. break;
  95. case FFI_TYPE_UINT16:
  96. *(SINT64 *) argp = *(UINT16 *)(* avalue);
  97. break;
  98. case FFI_TYPE_SINT32:
  99. case FFI_TYPE_UINT32:
  100. /* Note that unsigned 32-bit quantities are sign extended. */
  101. *(SINT64 *) argp = *(SINT32 *)(* avalue);
  102. break;
  103. case FFI_TYPE_SINT64:
  104. case FFI_TYPE_UINT64:
  105. case FFI_TYPE_POINTER:
  106. *(UINT64 *) argp = *(UINT64 *)(* avalue);
  107. break;
  108. case FFI_TYPE_FLOAT:
  109. if (argp - stack < 6)
  110. {
  111. /* Note the conversion -- all the fp regs are loaded as
  112. doubles. The in-register format is the same. */
  113. *(double *) argp = *(float *)(* avalue);
  114. }
  115. else
  116. *(float *) argp = *(float *)(* avalue);
  117. break;
  118. case FFI_TYPE_DOUBLE:
  119. *(double *) argp = *(double *)(* avalue);
  120. break;
  121. case FFI_TYPE_LONGDOUBLE:
  122. /* 128-bit long double is passed by reference. */
  123. *(long double **) argp = (long double *)(* avalue);
  124. size = sizeof (long double *);
  125. break;
  126. case FFI_TYPE_STRUCT:
  127. memcpy(argp, *avalue, (*arg_types)->size);
  128. break;
  129. default:
  130. FFI_ASSERT(0);
  131. }
  132. argp += ALIGN(size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  133. i++, arg_types++, avalue++;
  134. }
  135. ffi_call_osf(stack, cif->bytes, cif->flags, rvalue, fn);
  136. }
  137. ffi_status
  138. ffi_prep_closure_loc (ffi_closure* closure,
  139. ffi_cif* cif,
  140. void (*fun)(ffi_cif*, void*, void**, void*),
  141. void *user_data,
  142. void *codeloc)
  143. {
  144. unsigned int *tramp;
  145. tramp = (unsigned int *) &closure->tramp[0];
  146. tramp[0] = 0x47fb0401; /* mov $27,$1 */
  147. tramp[1] = 0xa77b0010; /* ldq $27,16($27) */
  148. tramp[2] = 0x6bfb0000; /* jmp $31,($27),0 */
  149. tramp[3] = 0x47ff041f; /* nop */
  150. *(void **) &tramp[4] = ffi_closure_osf;
  151. closure->cif = cif;
  152. closure->fun = fun;
  153. closure->user_data = user_data;
  154. /* Flush the Icache.
  155. Tru64 UNIX as doesn't understand the imb mnemonic, so use call_pal
  156. instead, since both Compaq as and gas can handle it.
  157. 0x86 is PAL_imb in Tru64 UNIX <alpha/pal.h>. */
  158. asm volatile ("call_pal 0x86" : : : "memory");
  159. return FFI_OK;
  160. }
  161. long FFI_HIDDEN
  162. ffi_closure_osf_inner(ffi_closure *closure, void *rvalue, unsigned long *argp)
  163. {
  164. ffi_cif *cif;
  165. void **avalue;
  166. ffi_type **arg_types;
  167. long i, avn, argn;
  168. cif = closure->cif;
  169. avalue = alloca(cif->nargs * sizeof(void *));
  170. argn = 0;
  171. /* Copy the caller's structure return address to that the closure
  172. returns the data directly to the caller. */
  173. if (cif->flags == FFI_TYPE_STRUCT)
  174. {
  175. rvalue = (void *) argp[0];
  176. argn = 1;
  177. }
  178. i = 0;
  179. avn = cif->nargs;
  180. arg_types = cif->arg_types;
  181. /* Grab the addresses of the arguments from the stack frame. */
  182. while (i < avn)
  183. {
  184. size_t size = arg_types[i]->size;
  185. switch (arg_types[i]->type)
  186. {
  187. case FFI_TYPE_SINT8:
  188. case FFI_TYPE_UINT8:
  189. case FFI_TYPE_SINT16:
  190. case FFI_TYPE_UINT16:
  191. case FFI_TYPE_SINT32:
  192. case FFI_TYPE_UINT32:
  193. case FFI_TYPE_SINT64:
  194. case FFI_TYPE_UINT64:
  195. case FFI_TYPE_POINTER:
  196. case FFI_TYPE_STRUCT:
  197. avalue[i] = &argp[argn];
  198. break;
  199. case FFI_TYPE_FLOAT:
  200. if (argn < 6)
  201. {
  202. /* Floats coming from registers need conversion from double
  203. back to float format. */
  204. *(float *)&argp[argn - 6] = *(double *)&argp[argn - 6];
  205. avalue[i] = &argp[argn - 6];
  206. }
  207. else
  208. avalue[i] = &argp[argn];
  209. break;
  210. case FFI_TYPE_DOUBLE:
  211. avalue[i] = &argp[argn - (argn < 6 ? 6 : 0)];
  212. break;
  213. case FFI_TYPE_LONGDOUBLE:
  214. /* 128-bit long double is passed by reference. */
  215. avalue[i] = (long double *) argp[argn];
  216. size = sizeof (long double *);
  217. break;
  218. default:
  219. abort ();
  220. }
  221. argn += ALIGN(size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  222. i++;
  223. }
  224. /* Invoke the closure. */
  225. closure->fun (cif, rvalue, avalue, closure->user_data);
  226. /* Tell ffi_closure_osf how to perform return type promotions. */
  227. return cif->rtype->type;
  228. }