go-ffi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* go-ffi.c -- convert Go type description to libffi.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include "runtime.h"
  9. #include "go-alloc.h"
  10. #include "go-assert.h"
  11. #include "go-type.h"
  12. #ifdef USE_LIBFFI
  13. #include "ffi.h"
  14. /* The functions in this file are only called from reflect_call and
  15. reflect.ffi. As these functions call libffi functions, which will
  16. be compiled without -fsplit-stack, they will always run with a
  17. large stack. */
  18. static ffi_type *go_array_to_ffi (const struct __go_array_type *)
  19. __attribute__ ((no_split_stack));
  20. static ffi_type *go_slice_to_ffi (const struct __go_slice_type *)
  21. __attribute__ ((no_split_stack));
  22. static ffi_type *go_struct_to_ffi (const struct __go_struct_type *)
  23. __attribute__ ((no_split_stack));
  24. static ffi_type *go_string_to_ffi (void) __attribute__ ((no_split_stack));
  25. static ffi_type *go_interface_to_ffi (void) __attribute__ ((no_split_stack));
  26. static ffi_type *go_type_to_ffi (const struct __go_type_descriptor *)
  27. __attribute__ ((no_split_stack));
  28. static ffi_type *go_func_return_ffi (const struct __go_func_type *)
  29. __attribute__ ((no_split_stack));
  30. /* Return an ffi_type for a Go array type. The libffi library does
  31. not have any builtin support for passing arrays as values. We work
  32. around this by pretending that the array is a struct. */
  33. static ffi_type *
  34. go_array_to_ffi (const struct __go_array_type *descriptor)
  35. {
  36. ffi_type *ret;
  37. uintptr_t len;
  38. ffi_type *element;
  39. uintptr_t i;
  40. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  41. ret->type = FFI_TYPE_STRUCT;
  42. len = descriptor->__len;
  43. if (len == 0)
  44. {
  45. /* The libffi library won't accept an empty struct. */
  46. ret->elements = (ffi_type **) __go_alloc (2 * sizeof (ffi_type *));
  47. ret->elements[0] = &ffi_type_void;
  48. ret->elements[1] = NULL;
  49. return ret;
  50. }
  51. ret->elements = (ffi_type **) __go_alloc ((len + 1) * sizeof (ffi_type *));
  52. element = go_type_to_ffi (descriptor->__element_type);
  53. for (i = 0; i < len; ++i)
  54. ret->elements[i] = element;
  55. ret->elements[len] = NULL;
  56. return ret;
  57. }
  58. /* Return an ffi_type for a Go slice type. This describes the
  59. __go_open_array type defines in array.h. */
  60. static ffi_type *
  61. go_slice_to_ffi (
  62. const struct __go_slice_type *descriptor __attribute__ ((unused)))
  63. {
  64. ffi_type *ret;
  65. ffi_type *ffi_intgo;
  66. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  67. ret->type = FFI_TYPE_STRUCT;
  68. ret->elements = (ffi_type **) __go_alloc (4 * sizeof (ffi_type *));
  69. ret->elements[0] = &ffi_type_pointer;
  70. ffi_intgo = sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
  71. ret->elements[1] = ffi_intgo;
  72. ret->elements[2] = ffi_intgo;
  73. ret->elements[3] = NULL;
  74. return ret;
  75. }
  76. /* Return an ffi_type for a Go struct type. */
  77. static ffi_type *
  78. go_struct_to_ffi (const struct __go_struct_type *descriptor)
  79. {
  80. ffi_type *ret;
  81. int field_count;
  82. const struct __go_struct_field *fields;
  83. int i;
  84. field_count = descriptor->__fields.__count;
  85. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  86. ret->type = FFI_TYPE_STRUCT;
  87. if (field_count == 0)
  88. {
  89. /* The libffi library won't accept an empty struct. */
  90. ret->elements = (ffi_type **) __go_alloc (2 * sizeof (ffi_type *));
  91. ret->elements[0] = &ffi_type_void;
  92. ret->elements[1] = NULL;
  93. return ret;
  94. }
  95. fields = (const struct __go_struct_field *) descriptor->__fields.__values;
  96. ret->elements = (ffi_type **) __go_alloc ((field_count + 1)
  97. * sizeof (ffi_type *));
  98. for (i = 0; i < field_count; ++i)
  99. ret->elements[i] = go_type_to_ffi (fields[i].__type);
  100. ret->elements[field_count] = NULL;
  101. return ret;
  102. }
  103. /* Return an ffi_type for a Go string type. This describes the String
  104. struct. */
  105. static ffi_type *
  106. go_string_to_ffi (void)
  107. {
  108. ffi_type *ret;
  109. ffi_type *ffi_intgo;
  110. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  111. ret->type = FFI_TYPE_STRUCT;
  112. ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
  113. ret->elements[0] = &ffi_type_pointer;
  114. ffi_intgo = sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
  115. ret->elements[1] = ffi_intgo;
  116. ret->elements[2] = NULL;
  117. return ret;
  118. }
  119. /* Return an ffi_type for a Go interface type. This describes the
  120. __go_interface and __go_empty_interface structs. */
  121. static ffi_type *
  122. go_interface_to_ffi (void)
  123. {
  124. ffi_type *ret;
  125. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  126. ret->type = FFI_TYPE_STRUCT;
  127. ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
  128. ret->elements[0] = &ffi_type_pointer;
  129. ret->elements[1] = &ffi_type_pointer;
  130. ret->elements[2] = NULL;
  131. return ret;
  132. }
  133. #ifndef FFI_TARGET_HAS_COMPLEX_TYPE
  134. /* If libffi hasn't been updated for this target to support complex,
  135. pretend complex is a structure. Warning: This does not work for
  136. all ABIs. Eventually libffi should be updated for all targets
  137. and this should go away. */
  138. static ffi_type *go_complex_to_ffi (ffi_type *)
  139. __attribute__ ((no_split_stack));
  140. static ffi_type *
  141. go_complex_to_ffi (ffi_type *float_type)
  142. {
  143. ffi_type *ret;
  144. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  145. ret->type = FFI_TYPE_STRUCT;
  146. ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
  147. ret->elements[0] = float_type;
  148. ret->elements[1] = float_type;
  149. ret->elements[2] = NULL;
  150. return ret;
  151. }
  152. #endif
  153. /* Return an ffi_type for a type described by a
  154. __go_type_descriptor. */
  155. static ffi_type *
  156. go_type_to_ffi (const struct __go_type_descriptor *descriptor)
  157. {
  158. switch (descriptor->__code & GO_CODE_MASK)
  159. {
  160. case GO_BOOL:
  161. if (sizeof (_Bool) == 1)
  162. return &ffi_type_uint8;
  163. else if (sizeof (_Bool) == sizeof (int))
  164. return &ffi_type_uint;
  165. abort ();
  166. case GO_FLOAT32:
  167. if (sizeof (float) == 4)
  168. return &ffi_type_float;
  169. abort ();
  170. case GO_FLOAT64:
  171. if (sizeof (double) == 8)
  172. return &ffi_type_double;
  173. abort ();
  174. case GO_COMPLEX64:
  175. if (sizeof (float) == 4)
  176. {
  177. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  178. return &ffi_type_complex_float;
  179. #else
  180. return go_complex_to_ffi (&ffi_type_float);
  181. #endif
  182. }
  183. abort ();
  184. case GO_COMPLEX128:
  185. if (sizeof (double) == 8)
  186. {
  187. #ifdef FFI_TARGET_HAS_COMPLEX_TYPE
  188. return &ffi_type_complex_double;
  189. #else
  190. return go_complex_to_ffi (&ffi_type_double);
  191. #endif
  192. }
  193. abort ();
  194. case GO_INT16:
  195. return &ffi_type_sint16;
  196. case GO_INT32:
  197. return &ffi_type_sint32;
  198. case GO_INT64:
  199. return &ffi_type_sint64;
  200. case GO_INT8:
  201. return &ffi_type_sint8;
  202. case GO_INT:
  203. return sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
  204. case GO_UINT16:
  205. return &ffi_type_uint16;
  206. case GO_UINT32:
  207. return &ffi_type_uint32;
  208. case GO_UINT64:
  209. return &ffi_type_uint64;
  210. case GO_UINT8:
  211. return &ffi_type_uint8;
  212. case GO_UINT:
  213. return sizeof (uintgo) == 4 ? &ffi_type_uint32 : &ffi_type_uint64;
  214. case GO_UINTPTR:
  215. if (sizeof (void *) == 2)
  216. return &ffi_type_uint16;
  217. else if (sizeof (void *) == 4)
  218. return &ffi_type_uint32;
  219. else if (sizeof (void *) == 8)
  220. return &ffi_type_uint64;
  221. abort ();
  222. case GO_ARRAY:
  223. return go_array_to_ffi ((const struct __go_array_type *) descriptor);
  224. case GO_SLICE:
  225. return go_slice_to_ffi ((const struct __go_slice_type *) descriptor);
  226. case GO_STRUCT:
  227. return go_struct_to_ffi ((const struct __go_struct_type *) descriptor);
  228. case GO_STRING:
  229. return go_string_to_ffi ();
  230. case GO_INTERFACE:
  231. return go_interface_to_ffi ();
  232. case GO_CHAN:
  233. case GO_FUNC:
  234. case GO_MAP:
  235. case GO_PTR:
  236. case GO_UNSAFE_POINTER:
  237. /* These types are always pointers, and for FFI purposes nothing
  238. else matters. */
  239. return &ffi_type_pointer;
  240. default:
  241. abort ();
  242. }
  243. }
  244. /* Return the return type for a function, given the number of out
  245. parameters and their types. */
  246. static ffi_type *
  247. go_func_return_ffi (const struct __go_func_type *func)
  248. {
  249. int count;
  250. const struct __go_type_descriptor **types;
  251. ffi_type *ret;
  252. int i;
  253. count = func->__out.__count;
  254. if (count == 0)
  255. return &ffi_type_void;
  256. types = (const struct __go_type_descriptor **) func->__out.__values;
  257. if (count == 1)
  258. return go_type_to_ffi (types[0]);
  259. ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
  260. ret->type = FFI_TYPE_STRUCT;
  261. ret->elements = (ffi_type **) __go_alloc ((count + 1) * sizeof (ffi_type *));
  262. for (i = 0; i < count; ++i)
  263. ret->elements[i] = go_type_to_ffi (types[i]);
  264. ret->elements[count] = NULL;
  265. return ret;
  266. }
  267. /* Build an ffi_cif structure for a function described by a
  268. __go_func_type structure. */
  269. void
  270. __go_func_to_cif (const struct __go_func_type *func, _Bool is_interface,
  271. _Bool is_method, ffi_cif *cif)
  272. {
  273. int num_params;
  274. const struct __go_type_descriptor **in_types;
  275. size_t num_args;
  276. ffi_type **args;
  277. int off;
  278. int i;
  279. ffi_type *rettype;
  280. ffi_status status;
  281. num_params = func->__in.__count;
  282. in_types = ((const struct __go_type_descriptor **)
  283. func->__in.__values);
  284. num_args = num_params + (is_interface ? 1 : 0);
  285. args = (ffi_type **) __go_alloc (num_args * sizeof (ffi_type *));
  286. i = 0;
  287. off = 0;
  288. if (is_interface)
  289. {
  290. args[0] = &ffi_type_pointer;
  291. off = 1;
  292. }
  293. else if (is_method)
  294. {
  295. args[0] = &ffi_type_pointer;
  296. i = 1;
  297. }
  298. for (; i < num_params; ++i)
  299. args[i + off] = go_type_to_ffi (in_types[i]);
  300. rettype = go_func_return_ffi (func);
  301. status = ffi_prep_cif (cif, FFI_DEFAULT_ABI, num_args, rettype, args);
  302. __go_assert (status == FFI_OK);
  303. }
  304. #endif /* defined(USE_LIBFFI) */